]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - sys/dev/fdt/fdt_slicer.c
Make it possible to load fdt_slicer as a module (unloading works too fwiw).
[FreeBSD/FreeBSD.git] / sys / dev / fdt / fdt_slicer.c
1 /*-
2  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
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 __FBSDID("$FreeBSD$");
31
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>
37
38 #include <dev/fdt/fdt_common.h>
39 #include <dev/ofw/ofw_bus.h>
40 #include <dev/ofw/openfirm.h>
41
42 #ifdef DEBUG
43 #define debugf(fmt, args...) do { printf("%s(): ", __func__);   \
44     printf(fmt,##args); } while (0)
45 #else
46 #define debugf(fmt, args...)
47 #endif
48
49 static int fdt_flash_fill_slices(device_t dev, const char *provider,
50     struct flash_slice *slices, int *slices_num);
51 static void fdt_slicer_init(void);
52
53 static int
54 fdt_flash_fill_slices(device_t dev, const char *provider __unused,
55     struct flash_slice *slices, int *slices_num)
56 {
57         char *slice_name;
58         phandle_t dt_node, dt_child;
59         u_long base, size;
60         int i;
61         ssize_t name_len;
62
63         /*
64          * We assume the caller provides buffer for FLASH_SLICES_MAX_NUM
65          * flash_slice structures.
66          */
67         if (slices == NULL) {
68                 *slices_num = 0;
69                 return (ENOMEM);
70         }
71
72         dt_node = ofw_bus_get_node(dev);
73         for (dt_child = OF_child(dt_node), i = 0; dt_child != 0;
74             dt_child = OF_peer(dt_child)) {
75
76                 if (i == FLASH_SLICES_MAX_NUM) {
77                         debugf("not enough buffer for slice i=%d\n", i);
78                         break;
79                 }
80
81                 /*
82                  * Retrieve start and size of the slice.
83                  */
84                 if (fdt_regsize(dt_child, &base, &size) != 0) {
85                         debugf("error during processing reg property, i=%d\n",
86                             i);
87                         continue;
88                 }
89
90                 if (size == 0) {
91                         debugf("slice i=%d with no size\n", i);
92                         continue;
93                 }
94
95                 /*
96                  * Retrieve label.
97                  */
98                 name_len = OF_getprop_alloc(dt_child, "label",
99                     (void **)&slice_name);
100                 if (name_len <= 0) {
101                         /* Use node name if no label defined */
102                         name_len = OF_getprop_alloc(dt_child, "name",
103                             (void **)&slice_name);
104                         if (name_len <= 0) {
105                                 debugf("slice i=%d with no name\n", i);
106                                 slice_name = NULL;
107                         }
108                 }
109
110                 /*
111                  * Fill slice entry data.
112                  */
113                 slices[i].base = base;
114                 slices[i].size = size;
115                 slices[i].label = slice_name;
116                 i++;
117         }
118
119         *slices_num = i;
120         return (0);
121 }
122
123 static void
124 fdt_slicer_init(void)
125 {
126
127         flash_register_slicer(fdt_flash_fill_slices, FLASH_SLICES_TYPE_NAND,
128            FALSE);
129         flash_register_slicer(fdt_flash_fill_slices, FLASH_SLICES_TYPE_CFI,
130            FALSE);
131         flash_register_slicer(fdt_flash_fill_slices, FLASH_SLICES_TYPE_SPI,
132            FALSE);
133 }
134
135 static void
136 fdt_slicer_cleanup(void)
137 {
138
139         flash_register_slicer(NULL, FLASH_SLICES_TYPE_NAND, true);
140         flash_register_slicer(NULL, FLASH_SLICES_TYPE_CFI, true);
141         flash_register_slicer(NULL, FLASH_SLICES_TYPE_SPI, true);
142 }
143
144 /*
145  * Must be initialized after GEOM classes (SI_SUB_DRIVERS/SI_ORDER_FIRST),
146  * i. e. after g_init() is called, due to the use of the GEOM topology_lock
147  * in flash_register_slicer().  However, must be before SI_SUB_CONFIGURE.
148  */
149 SYSINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_init, NULL);
150 SYSUNINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_cleanup, NULL);
151
152 static int
153 mod_handler(module_t mod, int type, void *data)
154 {
155
156         /*
157          * Nothing to do here: the SYSINIT/SYSUNINIT defined above run
158          * automatically at module load/unload time.
159          */
160         return (0);
161 }
162
163 static moduledata_t fdt_slicer_mod = {
164         "fdt_slicer", mod_handler, NULL
165 };
166
167 DECLARE_MODULE(fdt_slicer, fdt_slicer_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
168 MODULE_VERSION(fdt_slicer, 1);