2 * Copyright 2018 Emmanuel Vadot <manu@FreeBSD.org>
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
7 * 1. Redistributions of source code must retain the above copyright
8 * notice, this list of conditions and the following disclaimer.
9 * 2. Redistributions in binary form must reproduce the above copyright
10 * notice, this list of conditions and the following disclaimer in the
11 * documentation and/or other materials provided with the distribution.
13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
14 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
17 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
19 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
20 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
21 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
22 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 #include <sys/cdefs.h>
27 __FBSDID("$FreeBSD$");
29 #include <sys/param.h>
31 #include <sys/kernel.h>
32 #include <sys/malloc.h>
33 #include <sys/mutex.h>
35 #include <dev/fdt/fdt_common.h>
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/ofw_bus_subr.h>
43 nvmem_get_cell_node(phandle_t node, int idx, phandle_t *cell)
49 if (!OF_hasprop(node, "nvmem-cells") ||
50 !OF_hasprop(node, "nvmem-cell-names"))
53 ncell = OF_getencprop_alloc_multi(node, "nvmem-cells", sizeof(*p_cell), (void **)&p_cell);
57 cell_node = OF_node_from_xref(p_cell[idx]);
58 if (cell_node == p_cell[idx]) {
60 printf("nvmem_get_node: Cannot resolve phandle %x\n",
73 nvmem_get_cell_len(phandle_t node, const char *name)
79 rv = ofw_bus_find_string_index(node, "nvmem-cell-names", name, &idx);
83 rv = nvmem_get_cell_node(node, idx, &cell_node);
87 if (OF_getencprop(cell_node, "reg", reg, sizeof(reg)) != sizeof(reg)) {
89 printf("nvmem_get_cell_len: Cannot parse reg property of cell %s\n",
98 nvmem_read_cell_by_idx(phandle_t node, int idx, void *cell, size_t buflen)
105 rv = nvmem_get_cell_node(node, idx, &cell_node);
109 /* Validate the reg property */
110 if (OF_getencprop(cell_node, "reg", reg, sizeof(reg)) != sizeof(reg)) {
112 printf("nvmem_get_cell_by_name: Cannot parse reg property of cell %d\n",
117 if (buflen != reg[1])
120 provider = OF_device_from_xref(OF_xref_from_node(OF_parent(cell_node)));
121 if (provider == NULL) {
123 printf("nvmem_get_cell_by_idx: Cannot find the nvmem device\n");
127 rv = NVMEM_READ(provider, reg[0], reg[1], cell);
136 nvmem_read_cell_by_name(phandle_t node, const char *name, void *cell, size_t buflen)
140 rv = ofw_bus_find_string_index(node, "nvmem-cell-names", name, &idx);
144 return (nvmem_read_cell_by_idx(node, idx, cell, buflen));
148 nvmem_write_cell_by_idx(phandle_t node, int idx, void *cell, size_t buflen)
150 phandle_t cell_node, prov_node;
155 rv = nvmem_get_cell_node(node, idx, &cell_node);
159 prov_node = OF_parent(cell_node);
160 if (OF_hasprop(prov_node, "read-only"))
163 /* Validate the reg property */
164 if (OF_getencprop(cell_node, "reg", reg, sizeof(reg)) != sizeof(reg)) {
166 printf("nvmem_get_cell_by_idx: Cannot parse reg property of cell %d\n",
171 if (buflen != reg[1])
174 provider = OF_device_from_xref(OF_xref_from_node(prov_node));
175 if (provider == NULL) {
177 printf("nvmem_get_cell_by_idx: Cannot find the nvmem device\n");
181 rv = NVMEM_WRITE(provider, reg[0], reg[1], cell);
190 nvmem_write_cell_by_name(phandle_t node, const char *name, void *cell, size_t buflen)
194 rv = ofw_bus_find_string_index(node, "nvmem-cell-names", name, &idx);
198 return (nvmem_write_cell_by_idx(node, idx, cell, buflen));