1 // SPDX-License-Identifier: BSD-2-Clause 2 /* 3 * Copyright (c) 2023, Microchip 4 */ 5 6 #include <drivers/nvmem.h> 7 #include <kernel/dt.h> 8 #include <libfdt.h> 9 10 TEE_Result nvmem_cell_parse_dt(const void *fdt, int nodeoffset, 11 struct nvmem_cell *cell) 12 { 13 if (fdt_reg_info(fdt, nodeoffset, &cell->offset, &cell->len)) 14 return TEE_ERROR_GENERIC; 15 16 return TEE_SUCCESS; 17 } 18 19 TEE_Result nvmem_get_cell_by_name(const void *fdt, int nodeoffset, 20 const char *name, struct nvmem_cell **cell) 21 { 22 int index = 0; 23 24 index = fdt_stringlist_search(fdt, nodeoffset, "nvmem-cell-names", 25 name); 26 if (index < 0) 27 return TEE_ERROR_ITEM_NOT_FOUND; 28 29 return nvmem_get_cell_by_index(fdt, nodeoffset, index, cell); 30 } 31 32 TEE_Result nvmem_get_cell_by_index(const void *fdt, 33 int nodeoffset, 34 unsigned int index, 35 struct nvmem_cell **out_cell) 36 { 37 TEE_Result res = TEE_ERROR_GENERIC; 38 void *cell = NULL; 39 40 res = dt_driver_device_from_node_idx_prop("nvmem-cells", fdt, 41 nodeoffset, index, 42 DT_DRIVER_NVMEM, &cell); 43 if (!res) 44 *out_cell = cell; 45 46 return res; 47 } 48 49 TEE_Result nvmem_cell_malloc_and_read(struct nvmem_cell *cell, 50 uint8_t **out_data) 51 { 52 TEE_Result res = TEE_ERROR_GENERIC; 53 54 if (!cell->ops->read_cell) 55 return TEE_ERROR_NOT_SUPPORTED; 56 57 *out_data = malloc(cell->len); 58 if (!out_data) 59 return TEE_ERROR_OUT_OF_MEMORY; 60 61 res = cell->ops->read_cell(cell, *out_data); 62 if (res) 63 free(*out_data); 64 65 return res; 66 } 67