xref: /freebsd/sys/dev/fdt/fdt_slicer.c (revision e5657ef4cf957a7907f6e9a892733897d8fc7b07)
14ffd4dfeSGrzegorz Bernacki /*-
2718cf2ccSPedro F. Giffuni  * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
3718cf2ccSPedro F. Giffuni  *
44ffd4dfeSGrzegorz Bernacki  * Copyright (c) 2012 Semihalf.
54ffd4dfeSGrzegorz Bernacki  * All rights reserved.
64ffd4dfeSGrzegorz Bernacki  *
74ffd4dfeSGrzegorz Bernacki  * Redistribution and use in source and binary forms, with or without
84ffd4dfeSGrzegorz Bernacki  * modification, are permitted provided that the following conditions
94ffd4dfeSGrzegorz Bernacki  * are met:
104ffd4dfeSGrzegorz Bernacki  * 1. Redistributions of source code must retain the above copyright
114ffd4dfeSGrzegorz Bernacki  *    notice, this list of conditions and the following disclaimer.
124ffd4dfeSGrzegorz Bernacki  * 2. Redistributions in binary form must reproduce the above copyright
134ffd4dfeSGrzegorz Bernacki  *    notice, this list of conditions and the following disclaimer in the
144ffd4dfeSGrzegorz Bernacki  *    documentation and/or other materials provided with the distribution.
154ffd4dfeSGrzegorz Bernacki  *
164ffd4dfeSGrzegorz Bernacki  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
174ffd4dfeSGrzegorz Bernacki  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
184ffd4dfeSGrzegorz Bernacki  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
194ffd4dfeSGrzegorz Bernacki  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
204ffd4dfeSGrzegorz Bernacki  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
214ffd4dfeSGrzegorz Bernacki  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
224ffd4dfeSGrzegorz Bernacki  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
234ffd4dfeSGrzegorz Bernacki  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
244ffd4dfeSGrzegorz Bernacki  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
254ffd4dfeSGrzegorz Bernacki  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
264ffd4dfeSGrzegorz Bernacki  * SUCH DAMAGE.
274ffd4dfeSGrzegorz Bernacki  */
284ffd4dfeSGrzegorz Bernacki 
294ffd4dfeSGrzegorz Bernacki #include <sys/cdefs.h>
304ffd4dfeSGrzegorz Bernacki __FBSDID("$FreeBSD$");
314ffd4dfeSGrzegorz Bernacki 
324ffd4dfeSGrzegorz Bernacki #include <sys/param.h>
334ffd4dfeSGrzegorz Bernacki #include <sys/systm.h>
344ffd4dfeSGrzegorz Bernacki #include <sys/kernel.h>
354e462178SIan Lepore #include <sys/module.h>
364ffd4dfeSGrzegorz Bernacki #include <sys/slicer.h>
374ffd4dfeSGrzegorz Bernacki 
384ffd4dfeSGrzegorz Bernacki #include <dev/fdt/fdt_common.h>
394874af73SMarius Strobl #include <dev/ofw/ofw_bus.h>
404874af73SMarius Strobl #include <dev/ofw/openfirm.h>
414ffd4dfeSGrzegorz Bernacki 
424ffd4dfeSGrzegorz Bernacki #ifdef DEBUG
434ffd4dfeSGrzegorz Bernacki #define debugf(fmt, args...) do { printf("%s(): ", __func__);	\
444ffd4dfeSGrzegorz Bernacki     printf(fmt,##args); } while (0)
454ffd4dfeSGrzegorz Bernacki #else
464ffd4dfeSGrzegorz Bernacki #define debugf(fmt, args...)
474ffd4dfeSGrzegorz Bernacki #endif
484ffd4dfeSGrzegorz Bernacki 
49db58d718SIan Lepore static int fill_slices(device_t dev, const char *provider,
504874af73SMarius Strobl     struct flash_slice *slices, int *slices_num);
514874af73SMarius Strobl static void fdt_slicer_init(void);
524874af73SMarius Strobl 
534874af73SMarius Strobl static int
54a6bcabcdSIan Lepore fill_slices_from_node(phandle_t node, struct flash_slice *slices, int *count)
554ffd4dfeSGrzegorz Bernacki {
56a6bcabcdSIan Lepore 	char *label;
57a6bcabcdSIan Lepore 	phandle_t child;
584ffd4dfeSGrzegorz Bernacki 	u_long base, size;
59a6bcabcdSIan Lepore 	int flags, i;
60db58d718SIan Lepore 	ssize_t nmlen;
614ffd4dfeSGrzegorz Bernacki 
62db58d718SIan Lepore 	i = 0;
63db58d718SIan Lepore 	for (child = OF_child(node); child != 0; child = OF_peer(child)) {
64a6bcabcdSIan Lepore 		flags = FLASH_SLICES_FLAG_NONE;
654ffd4dfeSGrzegorz Bernacki 
6685f55a2aSIan Lepore 		/* Nodes with a compatible property are not slices. */
6785f55a2aSIan Lepore 		if (OF_hasprop(child, "compatible"))
6885f55a2aSIan Lepore 			continue;
6985f55a2aSIan Lepore 
704ffd4dfeSGrzegorz Bernacki 		if (i == FLASH_SLICES_MAX_NUM) {
714ffd4dfeSGrzegorz Bernacki 			debugf("not enough buffer for slice i=%d\n", i);
724ffd4dfeSGrzegorz Bernacki 			break;
734ffd4dfeSGrzegorz Bernacki 		}
744ffd4dfeSGrzegorz Bernacki 
75db58d718SIan Lepore 		/* Retrieve start and size of the slice. */
76db58d718SIan Lepore 		if (fdt_regsize(child, &base, &size) != 0) {
774ffd4dfeSGrzegorz Bernacki 			debugf("error during processing reg property, i=%d\n",
784ffd4dfeSGrzegorz Bernacki 			    i);
794ffd4dfeSGrzegorz Bernacki 			continue;
804ffd4dfeSGrzegorz Bernacki 		}
814ffd4dfeSGrzegorz Bernacki 
824ffd4dfeSGrzegorz Bernacki 		if (size == 0) {
834ffd4dfeSGrzegorz Bernacki 			debugf("slice i=%d with no size\n", i);
844ffd4dfeSGrzegorz Bernacki 			continue;
854ffd4dfeSGrzegorz Bernacki 		}
864ffd4dfeSGrzegorz Bernacki 
87db58d718SIan Lepore 		/* Retrieve label. */
88a6bcabcdSIan Lepore 		nmlen = OF_getprop_alloc(child, "label", (void **)&label);
89db58d718SIan Lepore 		if (nmlen <= 0) {
904ffd4dfeSGrzegorz Bernacki 			/* Use node name if no label defined */
91a6bcabcdSIan Lepore 			nmlen = OF_getprop_alloc(child, "name", (void **)&label);
92db58d718SIan Lepore 			if (nmlen <= 0) {
934ffd4dfeSGrzegorz Bernacki 				debugf("slice i=%d with no name\n", i);
94a6bcabcdSIan Lepore 				label = NULL;
954ffd4dfeSGrzegorz Bernacki 			}
964ffd4dfeSGrzegorz Bernacki 		}
974ffd4dfeSGrzegorz Bernacki 
98a6bcabcdSIan Lepore 		if (OF_hasprop(child, "read-only"))
99a6bcabcdSIan Lepore 			flags |= FLASH_SLICES_FLAG_RO;
100a6bcabcdSIan Lepore 
101db58d718SIan Lepore 		/* Fill slice entry data. */
1024ffd4dfeSGrzegorz Bernacki 		slices[i].base = base;
1034ffd4dfeSGrzegorz Bernacki 		slices[i].size = size;
104a6bcabcdSIan Lepore 		slices[i].label = label;
105a6bcabcdSIan Lepore 		slices[i].flags = flags;
1064ffd4dfeSGrzegorz Bernacki 		i++;
1074ffd4dfeSGrzegorz Bernacki 	}
1084ffd4dfeSGrzegorz Bernacki 
109a6bcabcdSIan Lepore 	*count = i;
1104ffd4dfeSGrzegorz Bernacki 	return (0);
1114ffd4dfeSGrzegorz Bernacki }
1124874af73SMarius Strobl 
113a6bcabcdSIan Lepore static int
114a6bcabcdSIan Lepore fill_slices(device_t dev, const char *provider __unused,
115a6bcabcdSIan Lepore     struct flash_slice *slices, int *slices_num)
116a6bcabcdSIan Lepore {
117a6bcabcdSIan Lepore 	phandle_t child, node;
118a6bcabcdSIan Lepore 
119a6bcabcdSIan Lepore 	/*
120a6bcabcdSIan Lepore 	 * We assume the caller provides buffer for FLASH_SLICES_MAX_NUM
121a6bcabcdSIan Lepore 	 * flash_slice structures.
122a6bcabcdSIan Lepore 	 */
123a6bcabcdSIan Lepore 	if (slices == NULL) {
124a6bcabcdSIan Lepore 		*slices_num = 0;
125a6bcabcdSIan Lepore 		return (ENOMEM);
126a6bcabcdSIan Lepore 	}
127a6bcabcdSIan Lepore 
128a6bcabcdSIan Lepore 	node = ofw_bus_get_node(dev);
129a6bcabcdSIan Lepore 
130a6bcabcdSIan Lepore 	/*
131a6bcabcdSIan Lepore 	 * If there is a child node whose compatible is "fixed-partitions" then
132a6bcabcdSIan Lepore 	 * we have new-style data where all partitions are the children of that
133a6bcabcdSIan Lepore 	 * node.  Otherwise we have old-style data where all the children of the
134a6bcabcdSIan Lepore 	 * device node are the partitions.
135a6bcabcdSIan Lepore 	 */
136a6bcabcdSIan Lepore 	child = fdt_find_compatible(node, "fixed-partitions", false);
137a6bcabcdSIan Lepore 	if (child == 0)
138a6bcabcdSIan Lepore 		return fill_slices_from_node(node, slices, slices_num);
139a6bcabcdSIan Lepore 	else
140a6bcabcdSIan Lepore 		return fill_slices_from_node(child, slices, slices_num);
141a6bcabcdSIan Lepore }
142a6bcabcdSIan Lepore 
1434874af73SMarius Strobl static void
1444874af73SMarius Strobl fdt_slicer_init(void)
1454874af73SMarius Strobl {
1464874af73SMarius Strobl 
147db58d718SIan Lepore 	flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_NAND, false);
148db58d718SIan Lepore 	flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_CFI, false);
149db58d718SIan Lepore 	flash_register_slicer(fill_slices, FLASH_SLICES_TYPE_SPI, false);
1504874af73SMarius Strobl }
1514874af73SMarius Strobl 
1524e462178SIan Lepore static void
1534e462178SIan Lepore fdt_slicer_cleanup(void)
1544e462178SIan Lepore {
1554e462178SIan Lepore 
1564e462178SIan Lepore 	flash_register_slicer(NULL, FLASH_SLICES_TYPE_NAND, true);
1574e462178SIan Lepore 	flash_register_slicer(NULL, FLASH_SLICES_TYPE_CFI, true);
1584e462178SIan Lepore 	flash_register_slicer(NULL, FLASH_SLICES_TYPE_SPI, true);
1594e462178SIan Lepore }
1604e462178SIan Lepore 
1614874af73SMarius Strobl /*
1624874af73SMarius Strobl  * Must be initialized after GEOM classes (SI_SUB_DRIVERS/SI_ORDER_FIRST),
1634874af73SMarius Strobl  * i. e. after g_init() is called, due to the use of the GEOM topology_lock
1644874af73SMarius Strobl  * in flash_register_slicer().  However, must be before SI_SUB_CONFIGURE.
1654874af73SMarius Strobl  */
1664e462178SIan Lepore SYSINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_init, NULL);
1674e462178SIan Lepore SYSUNINIT(fdt_slicer, SI_SUB_DRIVERS, SI_ORDER_SECOND, fdt_slicer_cleanup, NULL);
1684e462178SIan Lepore 
1694e462178SIan Lepore static int
1704e462178SIan Lepore mod_handler(module_t mod, int type, void *data)
1714e462178SIan Lepore {
1724e462178SIan Lepore 
1734e462178SIan Lepore 	/*
1744e462178SIan Lepore 	 * Nothing to do here: the SYSINIT/SYSUNINIT defined above run
1754e462178SIan Lepore 	 * automatically at module load/unload time.
1764e462178SIan Lepore 	 */
1774e462178SIan Lepore 	return (0);
1784e462178SIan Lepore }
1794e462178SIan Lepore 
1804e462178SIan Lepore static moduledata_t fdt_slicer_mod = {
1814e462178SIan Lepore 	"fdt_slicer", mod_handler, NULL
1824e462178SIan Lepore };
1834e462178SIan Lepore 
1844e462178SIan Lepore DECLARE_MODULE(fdt_slicer, fdt_slicer_mod, SI_SUB_DRIVERS, SI_ORDER_SECOND);
185*e5657ef4SJustin Hibbits MODULE_DEPEND(fdt_slicer, g_flashmap, 0, 0, 0);
1864e462178SIan Lepore MODULE_VERSION(fdt_slicer, 1);
187