2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2012 Semihalf.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
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.
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
29 #include <sys/cdefs.h>
30 __FBSDID("$FreeBSD$");
32 #include <sys/param.h>
33 #include <sys/systm.h>
34 #include <sys/kernel.h>
35 #include <sys/module.h>
36 #include <sys/slicer.h>
38 #include <dev/fdt/fdt_common.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/openfirm.h>
43 #define debugf(fmt, args...) do { printf("%s(): ", __func__); \
44 printf(fmt,##args); } while (0)
46 #define debugf(fmt, args...)
49 static int fill_slices(device_t dev, const char *provider,
50 struct flash_slice *slices, int *slices_num);
51 static void fdt_slicer_init(void);
54 fill_slices(device_t dev, const char *provider __unused,
55 struct flash_slice *slices, int *slices_num)
58 phandle_t node, child;
64 * We assume the caller provides buffer for FLASH_SLICES_MAX_NUM
65 * flash_slice structures.
73 node = ofw_bus_get_node(dev);
74 for (child = OF_child(node); child != 0; child = OF_peer(child)) {
76 /* Nodes with a compatible property are not slices. */
77 if (OF_hasprop(child, "compatible"))
80 if (i == FLASH_SLICES_MAX_NUM) {
81 debugf("not enough buffer for slice i=%d\n", i);
85 /* Retrieve start and size of the slice. */
86 if (fdt_regsize(child, &base, &size) != 0) {
87 debugf("error during processing reg property, i=%d\n",
93 debugf("slice i=%d with no size\n", i);
98 nmlen = OF_getprop_alloc(child, "label", (void **)&slice_name);
100 /* Use node name if no label defined */
101 nmlen = OF_getprop_alloc(child, "name",
102 (void **)&slice_name);
104 debugf("slice i=%d with no name\n", i);
109 /* Fill slice entry data. */
110 slices[i].base = base;
111 slices[i].size = size;
112 slices[i].label = slice_name;
121 fdt_slicer_init(void)
124 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_NAND, false);
125 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_CFI, false);
126 flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_SPI, false);
130 fdt_slicer_cleanup(void)
133 flash_register_slicer(NULL, FLASH_SLICES_TYPE_NAND, true);
134 flash_register_slicer(NULL, FLASH_SLICES_TYPE_CFI, true);
135 flash_register_slicer(NULL, FLASH_SLICES_TYPE_SPI, true);
139 * Must be initialized after GEOM classes (SI_SUB_DRIVERS/SI_ORDER_FIRST),
140 * i. e. after g_init() is called, due to the use of the GEOM topology_lock
141 * in flash_register_slicer(). However, must be before SI_SUB_CONFIGURE.
143 SYSINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_init, NULL);
144 SYSUNINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_cleanup, NULL);
147 mod_handler(module_t mod, int type, void *data)
151 * Nothing to do here: the SYSINIT/SYSUNINIT defined above run
152 * automatically at module load/unload time.
157 static moduledata_t fdt_slicer_mod = {
158 "fdt_slicer", mod_handler, NULL
161 DECLARE_MODULE(fdt_slicer, fdt_slicer_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
162 MODULE_VERSION(fdt_slicer, 1);