ti_pruss.c revision 266152
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: stable/10/sys/arm/ti/ti_pruss.c 266152 2014-05-15 16:11:06Z ian $"); 28 29#include <sys/param.h> 30#include <sys/systm.h> 31#include <sys/bus.h> 32#include <sys/conf.h> 33#include <sys/kernel.h> 34#include <sys/module.h> 35#include <sys/malloc.h> 36#include <sys/rman.h> 37#include <sys/event.h> 38#include <sys/selinfo.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_prcm.h> 53#include <arm/ti/ti_pruss.h> 54 55#define DEBUG 56#ifdef DEBUG 57#define DPRINTF(fmt, ...) do { \ 58 printf("%s: ", __func__); \ 59 printf(fmt, __VA_ARGS__); \ 60} while (0) 61#else 62#define DPRINTF(fmt, ...) 63#endif 64 65static device_probe_t ti_pruss_probe; 66static device_attach_t ti_pruss_attach; 67static device_detach_t ti_pruss_detach; 68static void ti_pruss_intr(void *); 69static d_open_t ti_pruss_open; 70static d_close_t ti_pruss_close; 71static d_mmap_t ti_pruss_mmap; 72static void ti_pruss_kq_read_detach(struct knote *); 73static int ti_pruss_kq_read_event(struct knote *, long); 74static d_kqfilter_t ti_pruss_kqfilter; 75 76#define TI_PRUSS_IRQS 8 77struct ti_pruss_softc { 78 struct mtx sc_mtx; 79 struct resource *sc_mem_res; 80 struct resource *sc_irq_res[TI_PRUSS_IRQS]; 81 void *sc_intr[TI_PRUSS_IRQS]; 82 bus_space_tag_t sc_bt; 83 bus_space_handle_t sc_bh; 84 struct cdev *sc_pdev; 85 struct selinfo sc_selinfo; 86 uint32_t sc_inuse; 87}; 88 89static struct cdevsw ti_pruss_cdevsw = { 90 .d_version = D_VERSION, 91 .d_name = "ti_pruss", 92 .d_open = ti_pruss_open, 93 .d_close = ti_pruss_close, 94 .d_mmap = ti_pruss_mmap, 95 .d_kqfilter = ti_pruss_kqfilter, 96}; 97 98static device_method_t ti_pruss_methods[] = { 99 DEVMETHOD(device_probe, ti_pruss_probe), 100 DEVMETHOD(device_attach, ti_pruss_attach), 101 DEVMETHOD(device_detach, ti_pruss_detach), 102 103 DEVMETHOD_END 104}; 105 106static driver_t ti_pruss_driver = { 107 "ti_pruss", 108 ti_pruss_methods, 109 sizeof(struct ti_pruss_softc) 110}; 111 112static devclass_t ti_pruss_devclass; 113 114DRIVER_MODULE(ti_pruss, simplebus, ti_pruss_driver, ti_pruss_devclass, 0, 0); 115 116static struct resource_spec ti_pruss_irq_spec[] = { 117 { SYS_RES_IRQ, 0, RF_ACTIVE }, 118 { SYS_RES_IRQ, 1, RF_ACTIVE }, 119 { SYS_RES_IRQ, 2, RF_ACTIVE }, 120 { SYS_RES_IRQ, 3, RF_ACTIVE }, 121 { SYS_RES_IRQ, 4, RF_ACTIVE }, 122 { SYS_RES_IRQ, 5, RF_ACTIVE }, 123 { SYS_RES_IRQ, 6, RF_ACTIVE }, 124 { SYS_RES_IRQ, 7, RF_ACTIVE }, 125 { -1, 0, 0 } 126}; 127 128static struct ti_pruss_irq_arg { 129 int irq; 130 struct ti_pruss_softc *sc; 131} ti_pruss_irq_args[TI_PRUSS_IRQS]; 132 133static __inline uint32_t 134ti_pruss_reg_read(struct ti_pruss_softc *sc, uint32_t reg) 135{ 136 return (bus_space_read_4(sc->sc_bt, sc->sc_bh, reg)); 137} 138 139static __inline void 140ti_pruss_reg_write(struct ti_pruss_softc *sc, uint32_t reg, uint32_t val) 141{ 142 bus_space_write_4(sc->sc_bt, sc->sc_bh, reg, val); 143} 144 145static int 146ti_pruss_probe(device_t dev) 147{ 148 149 if (!ofw_bus_status_okay(dev)) 150 return (ENXIO); 151 152 if (ofw_bus_is_compatible(dev, "ti,pruss-v1") || 153 ofw_bus_is_compatible(dev, "ti,pruss-v2")) { 154 device_set_desc(dev, "TI Programmable Realtime Unit Subsystem"); 155 return (BUS_PROBE_DEFAULT); 156 } 157 158 return (ENXIO); 159} 160 161static int 162ti_pruss_attach(device_t dev) 163{ 164 struct ti_pruss_softc *sc; 165 int rid, i; 166 167 if (ti_prcm_clk_enable(PRUSS_CLK) != 0) { 168 device_printf(dev, "could not enable PRUSS clock\n"); 169 return (ENXIO); 170 } 171 sc = device_get_softc(dev); 172 rid = 0; 173 mtx_init(&sc->sc_mtx, "TI PRUSS", NULL, MTX_DEF); 174 sc->sc_mem_res = bus_alloc_resource_any(dev, SYS_RES_MEMORY, &rid, 175 RF_ACTIVE); 176 if (sc->sc_mem_res == NULL) { 177 device_printf(dev, "could not allocate memory resource\n"); 178 return (ENXIO); 179 } 180 sc->sc_bt = rman_get_bustag(sc->sc_mem_res); 181 sc->sc_bh = rman_get_bushandle(sc->sc_mem_res); 182 if (bus_alloc_resources(dev, ti_pruss_irq_spec, sc->sc_irq_res) != 0) { 183 device_printf(dev, "could not allocate interrupt resource\n"); 184 ti_pruss_detach(dev); 185 return (ENXIO); 186 } 187 for (i = 0; i < TI_PRUSS_IRQS; i++) { 188 ti_pruss_irq_args[i].irq = i; 189 ti_pruss_irq_args[i].sc = sc; 190 if (bus_setup_intr(dev, sc->sc_irq_res[i], 191 INTR_MPSAFE | INTR_TYPE_MISC, 192 NULL, ti_pruss_intr, &ti_pruss_irq_args[i], 193 &sc->sc_intr[i]) != 0) { 194 device_printf(dev, 195 "unable to setup the interrupt handler\n"); 196 ti_pruss_detach(dev); 197 return (ENXIO); 198 } 199 } 200 if (ti_pruss_reg_read(sc, PRUSS_AM18XX_INTC) == PRUSS_AM18XX_REV) 201 device_printf(dev, "AM18xx PRU-ICSS\n"); 202 else if (ti_pruss_reg_read(sc, PRUSS_AM33XX_INTC) == PRUSS_AM33XX_REV) 203 device_printf(dev, "AM33xx PRU-ICSS\n"); 204 205 sc->sc_pdev = make_dev(&ti_pruss_cdevsw, 0, UID_ROOT, GID_WHEEL, 206 0600, "pruss%d", device_get_unit(dev)); 207 sc->sc_pdev->si_drv1 = dev; 208 209 return (0); 210} 211 212static int 213ti_pruss_detach(device_t dev) 214{ 215 struct ti_pruss_softc *sc; 216 int i; 217 218 sc = device_get_softc(dev); 219 for (i = 0; i < TI_PRUSS_IRQS; i++) { 220 if (sc->sc_intr[i]) 221 bus_teardown_intr(dev, sc->sc_irq_res[i], sc->sc_intr[i]); 222 if (sc->sc_irq_res[i]) 223 bus_release_resource(dev, SYS_RES_IRQ, 224 rman_get_rid(sc->sc_irq_res[i]), 225 sc->sc_irq_res[i]); 226 } 227 if (sc->sc_mem_res) 228 bus_release_resource(dev, SYS_RES_MEMORY, rman_get_rid(sc->sc_mem_res), 229 sc->sc_mem_res); 230 if (sc->sc_pdev) 231 destroy_dev(sc->sc_pdev); 232 233 return (0); 234} 235 236static void 237ti_pruss_intr(void *arg) 238{ 239 struct ti_pruss_irq_arg *iap; 240 struct ti_pruss_softc *sc; 241 242 iap = arg; 243 sc = iap->sc; 244 DPRINTF("interrupt %p", sc); 245 KNOTE_UNLOCKED(&sc->sc_selinfo.si_note, iap->irq); 246} 247 248static int 249ti_pruss_open(struct cdev *cdev, int oflags, int devtype, struct thread *td) 250{ 251 device_t dev = cdev->si_drv1; 252 struct ti_pruss_softc *sc = device_get_softc(dev); 253 254 if (atomic_cmpset_32(&sc->sc_inuse, 0, 1) == 0) 255 return (EBUSY); 256 else 257 return (0); 258} 259 260static int 261ti_pruss_close(struct cdev *cdev, int fflag, int devtype, struct thread *td) 262{ 263 device_t dev = cdev->si_drv1; 264 struct ti_pruss_softc *sc = device_get_softc(dev); 265 266 sc->sc_inuse = 0; 267 268 return (0); 269} 270 271static int 272ti_pruss_mmap(struct cdev *cdev, vm_ooffset_t offset, vm_paddr_t *paddr, 273 int nprot, vm_memattr_t *memattr) 274{ 275 device_t dev = cdev->si_drv1; 276 struct ti_pruss_softc *sc = device_get_softc(dev); 277 278 if (offset > rman_get_size(sc->sc_mem_res)) 279 return (-1); 280 *paddr = rman_get_start(sc->sc_mem_res) + offset; 281 282 return (0); 283} 284 285static struct filterops ti_pruss_kq_read = { 286 .f_isfd = 1, 287 .f_detach = ti_pruss_kq_read_detach, 288 .f_event = ti_pruss_kq_read_event, 289}; 290 291static void 292ti_pruss_kq_read_detach(struct knote *kn) 293{ 294 struct ti_pruss_softc *sc = kn->kn_hook; 295 296 knlist_remove(&sc->sc_selinfo.si_note, kn, 0); 297} 298 299static int 300ti_pruss_kq_read_event(struct knote *kn, long hint) 301{ 302 kn->kn_data = hint; 303 304 return (hint); 305} 306 307static int 308ti_pruss_kqfilter(struct cdev *cdev, struct knote *kn) 309{ 310 device_t dev = cdev->si_drv1; 311 struct ti_pruss_softc *sc = device_get_softc(dev); 312 313 switch (kn->kn_filter) { 314 case EVFILT_READ: 315 kn->kn_hook = sc; 316 kn->kn_fop = &ti_pruss_kq_read; 317 knlist_add(&sc->sc_selinfo.si_note, kn, 1); 318 break; 319 default: 320 return (EINVAL); 321 } 322 323 return (0); 324} 325