2 * Copyright (c) 2014 Steven Lawrance <stl@koffein.net>
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$");
31 * Access to the Freescale i.MX6 On-Chip One-Time-Programmable Memory
34 #include <sys/param.h>
35 #include <sys/systm.h>
36 #include <sys/kernel.h>
37 #include <sys/module.h>
41 #include <dev/ofw/ofw_bus.h>
42 #include <dev/ofw/ofw_bus_subr.h>
44 #include <machine/bus.h>
46 #include <arm/freescale/fsl_ocotpreg.h>
47 #include <arm/freescale/fsl_ocotpvar.h>
50 * Find the physical address and size of the ocotp registers and devmap them,
51 * returning a pointer to the virtual address of the base.
53 * XXX This is temporary until we've worked out all the details of controlling
54 * the load order of devices. In an ideal world this device would be up and
55 * running before anything that needs it. When we're at a point to make that
56 * happen, this little block of code, and the few lines in fsl_ocotp_read_4()
57 * that refer to it can be deleted.
61 #include <dev/fdt/fdt_common.h>
62 #include <machine/devmap.h>
64 static uint32_t *ocotp_regs;
65 static vm_size_t ocotp_size;
68 fsl_ocotp_devmap(void)
70 phandle_t child, root;
73 if ((root = OF_finddevice("/")) == 0)
75 if ((child = fdt_depth_search_compatible(root, "fsl,imx6q-ocotp", 0)) == 0)
77 if (fdt_regsize(child, &base, &size) != 0)
80 ocotp_size = (vm_size_t)size;
82 if ((ocotp_regs = pmap_mapdev((vm_offset_t)base, ocotp_size)) == NULL)
87 panic("cannot find/map the ocotp registers");
89 /* XXX end of temporary code */
93 struct resource *mem_res;
96 static struct ocotp_softc *ocotp_sc;
98 static inline uint32_t
99 RD4(struct ocotp_softc *sc, bus_size_t off)
102 return (bus_read_4(sc->mem_res, off));
106 ocotp_detach(device_t dev)
109 /* The ocotp registers are always accessible. */
114 ocotp_attach(device_t dev)
116 struct ocotp_softc *sc;
119 sc = device_get_softc(dev);
122 /* Allocate bus_space resources. */
124 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid,
126 if (sc->mem_res == NULL) {
127 device_printf(dev, "Cannot allocate memory resources\n");
134 /* We're done with the temporary mapping now. */
135 if (ocotp_regs != NULL)
136 pmap_unmapdev((vm_offset_t)ocotp_regs, ocotp_size);
148 ocotp_probe(device_t dev)
151 if (!ofw_bus_status_okay(dev))
154 if (ofw_bus_is_compatible(dev, "fsl,imx6q-ocotp") == 0)
158 "Freescale On-Chip One-Time-Programmable Memory");
160 return (BUS_PROBE_DEFAULT);
164 fsl_ocotp_read_4(bus_size_t off)
167 if (off > FSL_OCOTP_LAST_REG)
168 panic("fsl_ocotp_read_4: offset out of range");
170 /* If we have a softcontext use the regular bus_space read. */
171 if (ocotp_sc != NULL)
172 return (RD4(ocotp_sc, off));
175 * Otherwise establish a tempory device mapping if necessary, and read
176 * the device without any help from bus_space.
178 * XXX Eventually the code from there down can be deleted.
180 if (ocotp_regs == NULL)
183 return (ocotp_regs[off / 4]);
186 static device_method_t ocotp_methods[] = {
187 /* Device interface */
188 DEVMETHOD(device_probe, ocotp_probe),
189 DEVMETHOD(device_attach, ocotp_attach),
190 DEVMETHOD(device_detach, ocotp_detach),
195 static driver_t ocotp_driver = {
198 sizeof(struct ocotp_softc)
201 static devclass_t ocotp_devclass;
203 DRIVER_MODULE(ocotp, simplebus, ocotp_driver, ocotp_devclass, 0, 0);