1 /*- 2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD 3 * 4 * Copyright (c) 2014 Steven Lawrance <stl@koffein.net> 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 /* 33 * Access to the Freescale i.MX6 On-Chip One-Time-Programmable Memory 34 */ 35 36 #include <sys/param.h> 37 #include <sys/systm.h> 38 #include <sys/kernel.h> 39 #include <sys/module.h> 40 #include <sys/bus.h> 41 #include <sys/rman.h> 42 43 #include <dev/ofw/ofw_bus.h> 44 #include <dev/ofw/ofw_bus_subr.h> 45 46 #include <machine/bus.h> 47 48 #include <arm/freescale/fsl_ocotpreg.h> 49 #include <arm/freescale/fsl_ocotpvar.h> 50 51 /* 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. 54 * 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. 60 */ 61 #include <vm/vm.h> 62 #include <vm/pmap.h> 63 #include <dev/fdt/fdt_common.h> 64 #include <sys/devmap.h> 65 66 static uint32_t *ocotp_regs; 67 static vm_size_t ocotp_size; 68 69 static void 70 fsl_ocotp_devmap(void) 71 { 72 phandle_t child, root; 73 u_long base, size; 74 75 if ((root = OF_finddevice("/")) == -1) 76 goto fatal; 77 if ((child = fdt_depth_search_compatible(root, "fsl,imx6q-ocotp", 0)) == 0) 78 goto fatal; 79 if (fdt_regsize(child, &base, &size) != 0) 80 goto fatal; 81 82 ocotp_size = (vm_size_t)size; 83 84 if ((ocotp_regs = pmap_mapdev((vm_offset_t)base, ocotp_size)) == NULL) 85 goto fatal; 86 87 return; 88 fatal: 89 panic("cannot find/map the ocotp registers"); 90 } 91 /* XXX end of temporary code */ 92 93 struct ocotp_softc { 94 device_t dev; 95 struct resource *mem_res; 96 }; 97 98 static struct ocotp_softc *ocotp_sc; 99 100 static inline uint32_t 101 RD4(struct ocotp_softc *sc, bus_size_t off) 102 { 103 104 return (bus_read_4(sc->mem_res, off)); 105 } 106 107 static int 108 ocotp_detach(device_t dev) 109 { 110 111 /* The ocotp registers are always accessible. */ 112 return (EBUSY); 113 } 114 115 static int 116 ocotp_attach(device_t dev) 117 { 118 struct ocotp_softc *sc; 119 int err, rid; 120 121 sc = device_get_softc(dev); 122 sc->dev = dev; 123 124 /* Allocate bus_space resources. */ 125 rid = 0; 126 sc->mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 127 RF_ACTIVE); 128 if (sc->mem_res == NULL) { 129 device_printf(dev, "Cannot allocate memory resources\n"); 130 err = ENXIO; 131 goto out; 132 } 133 134 ocotp_sc = sc; 135 136 /* We're done with the temporary mapping now. */ 137 if (ocotp_regs != NULL) 138 pmap_unmapdev((vm_offset_t)ocotp_regs, ocotp_size); 139 140 err = 0; 141 142 out: 143 if (err != 0) 144 ocotp_detach(dev); 145 146 return (err); 147 } 148 149 static int 150 ocotp_probe(device_t dev) 151 { 152 153 if (!ofw_bus_status_okay(dev)) 154 return (ENXIO); 155 156 if (ofw_bus_is_compatible(dev, "fsl,imx6q-ocotp") == 0) 157 return (ENXIO); 158 159 device_set_desc(dev, 160 "Freescale On-Chip One-Time-Programmable Memory"); 161 162 return (BUS_PROBE_DEFAULT); 163 } 164 165 uint32_t 166 fsl_ocotp_read_4(bus_size_t off) 167 { 168 169 if (off > FSL_OCOTP_LAST_REG) 170 panic("fsl_ocotp_read_4: offset out of range"); 171 172 /* If we have a softcontext use the regular bus_space read. */ 173 if (ocotp_sc != NULL) 174 return (RD4(ocotp_sc, off)); 175 176 /* 177 * Otherwise establish a tempory device mapping if necessary, and read 178 * the device without any help from bus_space. 179 * 180 * XXX Eventually the code from there down can be deleted. 181 */ 182 if (ocotp_regs == NULL) 183 fsl_ocotp_devmap(); 184 185 return (ocotp_regs[off / 4]); 186 } 187 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), 193 194 DEVMETHOD_END 195 }; 196 197 static driver_t ocotp_driver = { 198 "ocotp", 199 ocotp_methods, 200 sizeof(struct ocotp_softc) 201 }; 202 203 EARLY_DRIVER_MODULE(ocotp, simplebus, ocotp_driver, 0, 0, 204 BUS_PASS_CPU + BUS_PASS_ORDER_FIRST); 205