1 /*- 2 * Copyright (c) 2013 Rui Paulo <rpaulo@FreeBSD.org> 3 * All rights reserved. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions 7 * are met: 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. 13 * 14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 17 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, 18 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES 19 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 20 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 22 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 23 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 24 * POSSIBILITY OF SUCH DAMAGE. 25 */ 26 #include <sys/cdefs.h> 27 __FBSDID("$FreeBSD$"); 28 29 #include <sys/param.h> 30 #include <sys/systm.h> 31 #include <sys/bus.h> 32 #include <sys/kernel.h> 33 #include <sys/module.h> 34 #include <sys/malloc.h> 35 #include <sys/rman.h> 36 #include <sys/timeet.h> 37 #include <sys/timetc.h> 38 #include <sys/watchdog.h> 39 #include <machine/bus.h> 40 #include <machine/cpu.h> 41 #include <machine/frame.h> 42 #include <machine/intr.h> 43 44 #include <dev/fdt/fdt_common.h> 45 #include <dev/ofw/openfirm.h> 46 #include <dev/ofw/ofw_bus.h> 47 #include <dev/ofw/ofw_bus_subr.h> 48 49 #include <machine/bus.h> 50 #include <machine/fdt.h> 51 52 #include <arm/ti/ti_mbox.h> 53 #include <arm/ti/ti_prcm.h> 54 55 #include "mbox_if.h" 56 57 #define DEBUG 58 #ifdef DEBUG 59 #define DPRINTF(fmt, ...) do { \ 60 printf("%s: ", __func__); \ 61 printf(fmt, __VA_ARGS__); \ 62 } while (0) 63 #else 64 #define DPRINTF(fmt, ...) 65 #endif 66 67 static device_probe_t ti_mbox_probe; 68 static device_attach_t ti_mbox_attach; 69 static device_detach_t ti_mbox_detach; 70 static void ti_mbox_intr(void *); 71 static int ti_mbox_read(device_t, int, uint32_t *); 72 static int ti_mbox_write(device_t, int, uint32_t); 73 74 struct ti_mbox_softc { 75 struct mtx sc_mtx; 76 struct resource *sc_mem_res; 77 struct resource *sc_irq_res; 78 void *sc_intr; 79 bus_space_tag_t sc_bt; 80 bus_space_handle_t sc_bh; 81 }; 82 83 #define TI_MBOX_LOCK(sc) mtx_lock(&(sc)->sc_mtx) 84 #define TI_MBOX_UNLOCK(sc) mtx_unlock(&(sc)->sc_mtx) 85 86 static device_method_t ti_mbox_methods[] = { 87 DEVMETHOD(device_probe, ti_mbox_probe), 88 DEVMETHOD(device_attach, ti_mbox_attach), 89 DEVMETHOD(device_detach, ti_mbox_detach), 90 91 DEVMETHOD(mbox_read, ti_mbox_read), 92 DEVMETHOD(mbox_write, ti_mbox_write), 93 94 DEVMETHOD_END 95 }; 96 97 static driver_t ti_mbox_driver = { 98 "ti_mbox", 99 ti_mbox_methods, 100 sizeof(struct ti_mbox_softc) 101 }; 102 103 static devclass_t ti_mbox_devclass; 104 105 DRIVER_MODULE(ti_mbox, simplebus, ti_mbox_driver, ti_mbox_devclass, 0, 0); 106 107 static __inline uint32_t 108 ti_mbox_reg_read(struct ti_mbox_softc *sc, uint16_t reg) 109 { 110 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg)); 111 } 112 113 static __inline void 114 ti_mbox_reg_write(struct ti_mbox_softc *sc, uint16_t reg, uint32_t val) 115 { 116 bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val); 117 } 118 119 static int 120 ti_mbox_probe(device_t dev) 121 { 122 123 if (!ofw_bus_status_okay(dev)) 124 return (ENXIO); 125 126 if (ofw_bus_is_compatible(dev, "ti,system-mbox")) { 127 device_set_desc(dev, "TI System Mailbox"); 128 return (BUS_PROBE_DEFAULT); 129 } 130 131 return (ENXIO); 132 } 133 134 static int 135 ti_mbox_attach(device_t dev) 136 { 137 struct ti_mbox_softc *sc; 138 int rid, delay, chan; 139 uint32_t rev, sysconfig; 140 141 if (ti_prcm_clk_enable(MAILBOX0_CLK) != 0) { 142 device_printf(dev, "could not enable MBOX clock\n"); 143 return (ENXIO); 144 } 145 sc = device_get_softc(dev); 146 rid = 0; 147 mtx_init(&sc->sc_mtx, "TI mbox", NULL, MTX_DEF); 148 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 149 RF_ACTIVE); 150 if (sc->sc_mem_res == NULL) { 151 device_printf(dev, "could not allocate memory resource\n"); 152 return (ENXIO); 153 } 154 sc->sc_bt = rman_get_bustag(sc->sc_mem_res); 155 sc->sc_bh = rman_get_bushandle(sc->sc_mem_res); 156 rid = 0; 157 sc->sc_irq_res = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 158 RF_ACTIVE); 159 if (sc->sc_irq_res == NULL) { 160 device_printf(dev, "could not allocate interrupt resource\n"); 161 ti_mbox_detach(dev); 162 return (ENXIO); 163 } 164 if (bus_setup_intr(dev, sc->sc_irq_res, INTR_MPSAFE | INTR_TYPE_MISC, 165 NULL, ti_mbox_intr, sc, &sc->sc_intr) != 0) { 166 device_printf(dev, "unable to setup the interrupt handler\n"); 167 ti_mbox_detach(dev); 168 return (ENXIO); 169 } 170 /* 171 * Reset the controller. 172 */ 173 sysconfig = ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG); 174 DPRINTF("initial sysconfig %d\n", sysconfig); 175 sysconfig |= TI_MBOX_SYSCONFIG_SOFTRST; 176 delay = 100; 177 while (ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG) & 178 TI_MBOX_SYSCONFIG_SOFTRST) { 179 delay--; 180 DELAY(10); 181 } 182 if (delay == 0) { 183 device_printf(dev, "controller reset failed\n"); 184 ti_mbox_detach(dev); 185 return (ENXIO); 186 } 187 /* 188 * Enable smart idle mode. 189 */ 190 ti_mbox_reg_write(sc, TI_MBOX_SYSCONFIG, 191 ti_mbox_reg_read(sc, TI_MBOX_SYSCONFIG) | TI_MBOX_SYSCONFIG_SMARTIDLE); 192 rev = ti_mbox_reg_read(sc, TI_MBOX_REVISION); 193 DPRINTF("rev %d\n", rev); 194 device_printf(dev, "revision %d.%d\n", (rev >> 8) & 0x4, rev & 0x40); 195 /* 196 * Enable message interrupts. 197 */ 198 for (chan = 0; chan < 8; chan++) 199 ti_mbox_reg_write(sc, TI_MBOX_IRQENABLE_SET(chan), 1); 200 201 return (0); 202 } 203 204 static int 205 ti_mbox_detach(device_t dev) 206 { 207 struct ti_mbox_softc *sc; 208 209 sc = device_get_softc(dev); 210 if (sc->sc_intr) 211 bus_teardown_intr(dev, sc->sc_irq_res, sc->sc_intr); 212 if (sc->sc_irq_res) 213 bus_release_resource(dev, SYS_RES_IRQ, rman_get_rid(sc->sc_irq_res), 214 sc->sc_irq_res); 215 if (sc->sc_mem_res) 216 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->sc_mem_res), 217 sc->sc_mem_res); 218 219 return (0); 220 } 221 222 static void 223 ti_mbox_intr(void *arg) 224 { 225 struct ti_mbox_softc *sc; 226 227 sc = arg; 228 DPRINTF("interrupt %p", sc); 229 } 230 231 static int 232 ti_mbox_read(device_t dev, int chan, uint32_t *data) 233 { 234 struct ti_mbox_softc *sc; 235 236 if (chan < 0 || chan > 7) 237 return (EINVAL); 238 sc = device_get_softc(dev); 239 240 return (ti_mbox_reg_read(sc, TI_MBOX_MESSAGE(chan))); 241 } 242 243 static int 244 ti_mbox_write(device_t dev, int chan, uint32_t data) 245 { 246 int limit = 500; 247 struct ti_mbox_softc *sc; 248 249 if (chan < 0 || chan > 7) 250 return (EINVAL); 251 sc = device_get_softc(dev); 252 TI_MBOX_LOCK(sc); 253 /* XXX implement interrupt method */ 254 while (ti_mbox_reg_read(sc, TI_MBOX_FIFOSTATUS(chan)) == 1 && 255 limit--) { 256 DELAY(10); 257 } 258 if (limit == 0) { 259 device_printf(dev, "FIFOSTAUS%d stuck\n", chan); 260 TI_MBOX_UNLOCK(sc); 261 return (EAGAIN); 262 } 263 ti_mbox_reg_write(sc, TI_MBOX_MESSAGE(chan), data); 264 265 return (0); 266 } 267