2 * Copyright (c) 2012 Semihalf.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
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>
36 #include <dev/fdt/fdt_common.h>
37 #include <dev/ofw/ofw_bus.h>
38 #include <dev/ofw/openfirm.h>
41 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
42 printf(fmt,##args); } while (0)
44 #define debugf(fmt, args...)
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);
52 fill_slices_from_node(phandle_t node, struct flash_slice *slices, int *count)
61 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
62 flags = FLASH_SLICES_FLAG_NONE;
64 /* Nodes with a compatible property are not slices. */
65 if (OF_hasprop(child, "compatible"))
68 if (i == FLASH_SLICES_MAX_NUM) {
69 debugf("not enough buffer for slice i=%d\n", i);
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",
81 debugf("slice i=%d with no size\n", i);
86 nmlen = OF_getprop_alloc(child, "label", sizeof(char),
89 /* Use node name if no label defined */
90 nmlen = OF_getprop_alloc(child, "name", sizeof(char),
93 debugf("slice i=%d with no name\n", i);
98 if (OF_hasprop(child, "read-only"))
99 flags |= FLASH_SLICES_FLAG_RO;
101 /* Fill slice entry data. */
102 slices[i].base = base;
103 slices[i].size = size;
104 slices[i].label = label;
105 slices[i].flags = flags;
114 fill_slices(device_t dev, const char *provider __unused,
115 struct flash_slice *slices, int *slices_num)
117 phandle_t child, node;
120 * We assume the caller provides buffer for FLASH_SLICES_MAX_NUM
121 * flash_slice structures.
123 if (slices == NULL) {
128 node = ofw_bus_get_node(dev);
131 * If there is a child node whose compatible is "fixed-partitions" then
132 * we have new-style data where all partitions are the children of that
133 * node. Otherwise we have old-style data where all the children of the
134 * device node are the partitions.
136 child = fdt_find_compatible(node, "fixed-partitions", false);
138 return fill_slices_from_node(node, slices, slices_num);
140 return fill_slices_from_node(child, slices, slices_num);
144 fdt_slicer_init(void)
147 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_NAND, false);
148 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_CFI, false);
149 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_SPI, false);
153 fdt_slicer_cleanup(void)
156 flash_register_slicer(NULL, FLASH_SLICES_TYPE_NAND, true);
157 flash_register_slicer(NULL, FLASH_SLICES_TYPE_CFI, true);
158 flash_register_slicer(NULL, FLASH_SLICES_TYPE_SPI, true);
162 * Must be initialized after GEOM classes (SI_SUB_DRIVERS/SI_ORDER_FIRST),
163 * i. e. after g_init() is called, due to the use of the GEOM topology_lock
164 * in flash_register_slicer(). However, must be before SI_SUB_CONFIGURE.
166 SYSINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_init, NULL);
167 SYSUNINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_cleanup, NULL);
170 mod_handler(module_t mod, int type, void *data)
174 * Nothing to do here: the SYSINIT/SYSUNINIT defined above run
175 * automatically at module load/unload time.
180 static moduledata_t fdt_slicer_mod = {
181 "fdt_slicer", mod_handler, NULL
184 DECLARE_MODULE(fdt_slicer, fdt_slicer_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
185 MODULE_VERSION(fdt_slicer, 1);