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/param.h>
30 #include <sys/systm.h>
31 #include <sys/kernel.h>
32 #include <sys/module.h>
33 #include <sys/slicer.h>
34
35 #include <dev/fdt/fdt_common.h>
36 #include <dev/ofw/ofw_bus.h>
37 #include <dev/ofw/openfirm.h>
38
39 #ifdef DEBUG
40 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
41 printf(fmt,##args); } while (0)
42 #else
43 #define debugf(fmt, args...)
44 #endif
45
46 static int fill_slices(device_t dev, const char *provider,
47 struct flash_slice *slices, int *slices_num);
48 static void fdt_slicer_init(void);
49
50 static int
fill_slices_from_node(phandle_t node,struct flash_slice * slices,int * count)51 fill_slices_from_node(phandle_t node, struct flash_slice *slices, int *count)
52 {
53 char *label;
54 phandle_t child;
55 u_long base, size;
56 int flags, i;
57 ssize_t nmlen;
58
59 i = 0;
60 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
61 flags = FLASH_SLICES_FLAG_NONE;
62
63 /* Nodes with a compatible property are not slices. */
64 if (OF_hasprop(child, "compatible"))
65 continue;
66
67 if (i == FLASH_SLICES_MAX_NUM) {
68 debugf("not enough buffer for slice i=%d\n", i);
69 break;
70 }
71
72 /* Retrieve start and size of the slice. */
73 if (fdt_regsize(child, &base, &size) != 0) {
74 debugf("error during processing reg property, i=%d\n",
75 i);
76 continue;
77 }
78
79 if (size == 0) {
80 debugf("slice i=%d with no size\n", i);
81 continue;
82 }
83
84 /* Retrieve label. */
85 nmlen = OF_getprop_alloc(child, "label", (void **)&label);
86 if (nmlen <= 0) {
87 /* Use node name if no label defined */
88 nmlen = OF_getprop_alloc(child, "name", (void **)&label);
89 if (nmlen <= 0) {
90 debugf("slice i=%d with no name\n", i);
91 label = NULL;
92 }
93 }
94
95 if (OF_hasprop(child, "read-only"))
96 flags |= FLASH_SLICES_FLAG_RO;
97
98 /* Fill slice entry data. */
99 slices[i].base = base;
100 slices[i].size = size;
101 slices[i].label = label;
102 slices[i].flags = flags;
103 i++;
104 }
105
106 *count = i;
107 return (0);
108 }
109
110 static int
fill_slices(device_t dev,const char * provider __unused,struct flash_slice * slices,int * slices_num)111 fill_slices(device_t dev, const char *provider __unused,
112 struct flash_slice *slices, int *slices_num)
113 {
114 phandle_t child, node;
115
116 /*
117 * We assume the caller provides buffer for FLASH_SLICES_MAX_NUM
118 * flash_slice structures.
119 */
120 if (slices == NULL) {
121 *slices_num = 0;
122 return (ENOMEM);
123 }
124
125 node = ofw_bus_get_node(dev);
126
127 /*
128 * If there is a child node whose compatible is "fixed-partitions" then
129 * we have new-style data where all partitions are the children of that
130 * node. Otherwise we have old-style data where all the children of the
131 * device node are the partitions.
132 */
133 child = fdt_find_compatible(node, "fixed-partitions", false);
134 if (child == 0)
135 return fill_slices_from_node(node, slices, slices_num);
136 else
137 return fill_slices_from_node(child, slices, slices_num);
138 }
139
140 static void
fdt_slicer_init(void)141 fdt_slicer_init(void)
142 {
143
144 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_NAND, false);
145 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_CFI, false);
146 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_SPI, false);
147 }
148
149 static void
fdt_slicer_cleanup(void)150 fdt_slicer_cleanup(void)
151 {
152
153 flash_register_slicer(NULL, FLASH_SLICES_TYPE_NAND, true);
154 flash_register_slicer(NULL, FLASH_SLICES_TYPE_CFI, true);
155 flash_register_slicer(NULL, FLASH_SLICES_TYPE_SPI, true);
156 }
157
158 /*
159 * Must be initialized after GEOM classes (SI_SUB_DRIVERS/SI_ORDER_SECOND),
160 * i. e. after g_init() is called, due to the use of the GEOM topology_lock
161 * in flash_register_slicer(). However, must be before SI_SUB_CONFIGURE.
162 */
163 SYSINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_THIRD, fdt_slicer_init, NULL);
164 SYSUNINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_THIRD, fdt_slicer_cleanup, NULL);
165
166 static int
mod_handler(module_t mod,int type,void * data)167 mod_handler(module_t mod, int type, void *data)
168 {
169
170 /*
171 * Nothing to do here: the SYSINIT/SYSUNINIT defined above run
172 * automatically at module load/unload time.
173 */
174 return (0);
175 }
176
177 static moduledata_t fdt_slicer_mod = {
178 "fdt_slicer", mod_handler, NULL
179 };
180
181 DECLARE_MODULE(fdt_slicer, fdt_slicer_mod, SI_SUB_DRIVERS, SI_ORDER_THIRD);
182 MODULE_DEPEND(fdt_slicer, geom_flashmap, 0, 0, 0);
183 MODULE_VERSION(fdt_slicer, 1);
184