1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 *
4 * Copyright (c) 2012 Semihalf.
5 * All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 *
16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
29 #include <sys/cdefs.h>
30 #include <sys/param.h>
31 #include <sys/systm.h>
32 #include <sys/kernel.h>
33 #include <sys/module.h>
34 #include <sys/slicer.h>
35
36 #include <dev/fdt/fdt_common.h>
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/openfirm.h>
39
40 #ifdef DEBUG
41 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
42 printf(fmt,##args); } while (0)
43 #else
44 #define debugf(fmt, args...)
45 #endif
46
47 static int fill_slices(device_t dev, const char *provider,
48 struct flash_slice *slices, int *slices_num);
49 static void fdt_slicer_init(void);
50
51 static int
fill_slices_from_node(phandle_t node,struct flash_slice * slices,int * count)52 fill_slices_from_node(phandle_t node, struct flash_slice *slices, int *count)
53 {
54 char *label;
55 phandle_t child;
56 u_long base, size;
57 int flags, i;
58 ssize_t nmlen;
59
60 i = 0;
61 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
62 flags = FLASH_SLICES_FLAG_NONE;
63
64 /* Nodes with a compatible property are not slices. */
65 if (OF_hasprop(child, "compatible"))
66 continue;
67
68 if (i == FLASH_SLICES_MAX_NUM) {
69 debugf("not enough buffer for slice i=%d\n", i);
70 break;
71 }
72
73 /* Retrieve start and size of the slice. */
74 if (fdt_regsize(child, &base, &size) != 0) {
75 debugf("error during processing reg property, i=%d\n",
76 i);
77 continue;
78 }
79
80 if (size == 0) {
81 debugf("slice i=%d with no size\n", i);
82 continue;
83 }
84
85 /* Retrieve label. */
86 nmlen = OF_getprop_alloc(child, "label", (void **)&label);
87 if (nmlen <= 0) {
88 /* Use node name if no label defined */
89 nmlen = OF_getprop_alloc(child, "name", (void **)&label);
90 if (nmlen <= 0) {
91 debugf("slice i=%d with no name\n", i);
92 label = NULL;
93 }
94 }
95
96 if (OF_hasprop(child, "read-only"))
97 flags |= FLASH_SLICES_FLAG_RO;
98
99 /* Fill slice entry data. */
100 slices[i].base = base;
101 slices[i].size = size;
102 slices[i].label = label;
103 slices[i].flags = flags;
104 i++;
105 }
106
107 *count = i;
108 return (0);
109 }
110
111 static int
fill_slices(device_t dev,const char * provider __unused,struct flash_slice * slices,int * slices_num)112 fill_slices(device_t dev, const char *provider __unused,
113 struct flash_slice *slices, int *slices_num)
114 {
115 phandle_t child, node;
116
117 /*
118 * We assume the caller provides buffer for FLASH_SLICES_MAX_NUM
119 * flash_slice structures.
120 */
121 if (slices == NULL) {
122 *slices_num = 0;
123 return (ENOMEM);
124 }
125
126 node = ofw_bus_get_node(dev);
127
128 /*
129 * If there is a child node whose compatible is "fixed-partitions" then
130 * we have new-style data where all partitions are the children of that
131 * node. Otherwise we have old-style data where all the children of the
132 * device node are the partitions.
133 */
134 child = fdt_find_compatible(node, "fixed-partitions", false);
135 if (child == 0)
136 return fill_slices_from_node(node, slices, slices_num);
137 else
138 return fill_slices_from_node(child, slices, slices_num);
139 }
140
141 static void
fdt_slicer_init(void)142 fdt_slicer_init(void)
143 {
144
145 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_NAND, false);
146 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_CFI, false);
147 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_SPI, false);
148 }
149
150 static void
fdt_slicer_cleanup(void)151 fdt_slicer_cleanup(void)
152 {
153
154 flash_register_slicer(NULL, FLASH_SLICES_TYPE_NAND, true);
155 flash_register_slicer(NULL, FLASH_SLICES_TYPE_CFI, true);
156 flash_register_slicer(NULL, FLASH_SLICES_TYPE_SPI, true);
157 }
158
159 /*
160 * Must be initialized after GEOM classes (SI_SUB_DRIVERS/SI_ORDER_SECOND),
161 * i. e. after g_init() is called, due to the use of the GEOM topology_lock
162 * in flash_register_slicer(). However, must be before SI_SUB_CONFIGURE.
163 */
164 SYSINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_THIRD, fdt_slicer_init, NULL);
165 SYSUNINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_THIRD, fdt_slicer_cleanup, NULL);
166
167 static int
mod_handler(module_t mod,int type,void * data)168 mod_handler(module_t mod, int type, void *data)
169 {
170
171 /*
172 * Nothing to do here: the SYSINIT/SYSUNINIT defined above run
173 * automatically at module load/unload time.
174 */
175 return (0);
176 }
177
178 static moduledata_t fdt_slicer_mod = {
179 "fdt_slicer", mod_handler, NULL
180 };
181
182 DECLARE_MODULE(fdt_slicer, fdt_slicer_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
183 MODULE_DEPEND(fdt_slicer, g_flashmap, 0, 0, 0);
184 MODULE_VERSION(fdt_slicer, 1);
185