2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (c) 2014 Steven Lawrance <stl@koffein.net>
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$");
33 * Access to the Freescale i.MX6 On-Chip One-Time-Programmable Memory
36 #include <sys/param.h>
37 #include <sys/systm.h>
38 #include <sys/kernel.h>
39 #include <sys/module.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
46 #include <machine/bus.h>
48 #include <arm/freescale/fsl_ocotpreg.h>
49 #include <arm/freescale/fsl_ocotpvar.h>
52 * Find the physical address and size of the ocotp registers and devmap them,
53 * returning a pointer to the virtual address of the base.
55 * XXX This is temporary until we've worked out all the details of controlling
56 * the load order of devices. In an ideal world this device would be up and
57 * running before anything that needs it. When we're at a point to make that
58 * happen, this little block of code, and the few lines in fsl_ocotp_read_4()
59 * that refer to it can be deleted.
63 #include <dev/fdt/fdt_common.h>
64 #include <sys/devmap.h>
66 static uint32_t *ocotp_regs;
67 static vm_size_t ocotp_size;
70 fsl_ocotp_devmap(void)
72 phandle_t child, root;
75 if ((root = OF_finddevice("/")) == -1)
77 if ((child = fdt_depth_search_compatible(root, "fsl,imx6q-ocotp", 0)) == 0)
79 if (fdt_regsize(child, &base, &size) != 0)
82 ocotp_size = (vm_size_t)size;
84 if ((ocotp_regs = pmap_mapdev((vm_offset_t)base, ocotp_size)) == NULL)
89 panic("cannot find/map the ocotp registers");
91 /* XXX end of temporary code */
95 struct resource *mem_res;
98 static struct ocotp_softc *ocotp_sc;
100 static inline uint32_t
101 RD4(struct ocotp_softc *sc, bus_size_t off)
104 return (bus_read_4(sc->mem_res, off));
108 ocotp_detach(device_t dev)
111 /* The ocotp registers are always accessible. */
116 ocotp_attach(device_t dev)
118 struct ocotp_softc *sc;
121 sc = device_get_softc(dev);
124 /* Allocate bus_space resources. */
126 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
128 if (sc->mem_res == NULL) {
129 device_printf(dev, "Cannot allocate memory resources\n");
136 /* We're done with the temporary mapping now. */
137 if (ocotp_regs != NULL)
138 pmap_unmapdev((vm_offset_t)ocotp_regs, ocotp_size);
150 ocotp_probe(device_t dev)
153 if (!ofw_bus_status_okay(dev))
156 if (ofw_bus_is_compatible(dev, "fsl,imx6q-ocotp") == 0)
160 "Freescale On-Chip One-Time-Programmable Memory");
162 return (BUS_PROBE_DEFAULT);
166 fsl_ocotp_read_4(bus_size_t off)
169 if (off > FSL_OCOTP_LAST_REG)
170 panic("fsl_ocotp_read_4: offset out of range");
172 /* If we have a softcontext use the regular bus_space read. */
173 if (ocotp_sc != NULL)
174 return (RD4(ocotp_sc, off));
177 * Otherwise establish a tempory device mapping if necessary, and read
178 * the device without any help from bus_space.
180 * XXX Eventually the code from there down can be deleted.
182 if (ocotp_regs == NULL)
185 return (ocotp_regs[off / 4]);
188 static device_method_t ocotp_methods[] = {
189 /* Device interface */
190 DEVMETHOD(device_probe, ocotp_probe),
191 DEVMETHOD(device_attach, ocotp_attach),
192 DEVMETHOD(device_detach, ocotp_detach),
197 static driver_t ocotp_driver = {
200 sizeof(struct ocotp_softc)
203 static devclass_t ocotp_devclass;
205 EARLY_DRIVER_MODULE(ocotp, simplebus, ocotp_driver, ocotp_devclass, 0, 0,
206 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST);