apb.c revision 192117
145501Sjdp/*- 262801Sjdp * Copyright (c) 2009, Oleksandr Tymoshenko <gonzo@FreeBSD.org> 345501Sjdp * All rights reserved. 445501Sjdp * 545501Sjdp * Redistribution and use in source and binary forms, with or without 645501Sjdp * modification, are permitted provided that the following conditions 745501Sjdp * are met: 845501Sjdp * 1. Redistributions of source code must retain the above copyright 945501Sjdp * notice unmodified, this list of conditions, and the following 1045501Sjdp * disclaimer. 1145501Sjdp * 2. Redistributions in binary form must reproduce the above copyright 1245501Sjdp * notice, this list of conditions and the following disclaimer in the 1345501Sjdp * documentation and/or other materials provided with the distribution. 1445501Sjdp * 1545501Sjdp * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND 1645501Sjdp * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1745501Sjdp * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 1845501Sjdp * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE 1945501Sjdp * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 2045501Sjdp * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 2145501Sjdp * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 2245501Sjdp * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 2345501Sjdp * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 2445501Sjdp * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 2545501Sjdp * SUCH DAMAGE. 2650476Speter */ 2745501Sjdp 2845501Sjdp#include <sys/cdefs.h> 2945501Sjdp__FBSDID("$FreeBSD$"); 3045501Sjdp 3145501Sjdp#include <sys/param.h> 32115396Skan#include <sys/systm.h> 33115396Skan#include <sys/bus.h> 34115396Skan#include <sys/interrupt.h> 3585004Sdfr#include <sys/kernel.h> 3685004Sdfr#include <sys/module.h> 3745501Sjdp#include <sys/rman.h> 3845501Sjdp#include <sys/malloc.h> 3945501Sjdp 4045501Sjdp#include <machine/bus.h> 4148205Sjdp 4285004Sdfr#include <mips/atheros/apbvar.h> 4385004Sdfr#include <mips/atheros/ar71xxreg.h> 44107071Stmm 45107071Stmm#undef APB_DEBUG 4685004Sdfr#ifdef APB_DEBUG 47157220Sdes#define dprintf printf 4885004Sdfr#else 4985004Sdfr#define dprintf(x, arg...) 50157220Sdes#endif /* APB_DEBUG */ 5185004Sdfr 5285004Sdfrstatic int apb_activate_resource(device_t, device_t, int, int, 5385004Sdfr struct resource *); 5448205Sjdpstatic device_t apb_add_child(device_t, int, const char *, int); 5585004Sdfrstatic struct resource * 5685004Sdfr apb_alloc_resource(device_t, device_t, int, int *, u_long, 5785004Sdfr u_long, u_long, u_int); 5885677Speterstatic int apb_attach(device_t); 5985677Speterstatic int apb_deactivate_resource(device_t, device_t, int, int, 6085677Speter struct resource *); 61133063Sdfrstatic struct resource_list * 62133063Sdfr apb_get_resource_list(device_t, device_t); 63133063Sdfrstatic void apb_hinted_child(device_t, const char *, int); 64133063Sdfrstatic int apb_intr(void *); 65133063Sdfrstatic int apb_probe(device_t); 66133063Sdfrstatic int apb_release_resource(device_t, device_t, int, int, 67133063Sdfr struct resource *); 68133063Sdfrstatic int apb_setup_intr(device_t, device_t, struct resource *, int, 69133063Sdfr driver_filter_t *, driver_intr_t *, void *, void **); 70133063Sdfrstatic int apb_teardown_intr(device_t, device_t, struct resource *, 71133063Sdfr void *); 72133063Sdfr 73133063Sdfrstatic void apb_mask_irq(unsigned int irq) 74133063Sdfr{ 75133063Sdfr uint32_t reg; 76133063Sdfr 77217851Skib reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 78217851Skib ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg & ~(1 << irq)); 79217851Skib 8045501Sjdp} 81 82static void apb_unmask_irq(unsigned int irq) 83{ 84 uint32_t reg; 85 86 reg = ATH_READ_REG(AR71XX_MISC_INTR_MASK); 87 ATH_WRITE_REG(AR71XX_MISC_INTR_MASK, reg | (1 << irq)); 88} 89 90static int 91apb_probe(device_t dev) 92{ 93 94 return (0); 95} 96 97static int 98apb_attach(device_t dev) 99{ 100 struct apb_softc *sc = device_get_softc(dev); 101 int rid = 0; 102 103 device_set_desc(dev, "APB Bus bridge"); 104 105 sc->apb_mem_rman.rm_type = RMAN_ARRAY; 106 sc->apb_mem_rman.rm_descr = "APB memory window"; 107 108 if (rman_init(&sc->apb_mem_rman) != 0 || 109 rman_manage_region(&sc->apb_mem_rman, 110 AR71XX_APB_BASE, 111 AR71XX_APB_BASE + AR71XX_APB_SIZE - 1) != 0) 112 panic("apb_attach: failed to set up memory rman"); 113 114 sc->apb_irq_rman.rm_type = RMAN_ARRAY; 115 sc->apb_irq_rman.rm_descr = "APB IRQ"; 116 117 if (rman_init(&sc->apb_irq_rman) != 0 || 118 rman_manage_region(&sc->apb_irq_rman, 119 APB_IRQ_BASE, APB_IRQ_END) != 0) 120 panic("apb_attach: failed to set up IRQ rman"); 121 122 if ((sc->sc_misc_irq = bus_alloc_resource_any(dev, SYS_RES_IRQ, &rid, 123 RF_SHAREABLE | RF_ACTIVE)) == NULL) { 124 device_printf(dev, "unable to allocate IRQ resource\n"); 125 return (ENXIO); 126 } 127 128 if ((bus_setup_intr(dev, sc->sc_misc_irq, INTR_TYPE_MISC, 129 apb_intr, NULL, sc, &sc->sc_misc_ih))) { 130 device_printf(dev, 131 "WARNING: unable to register interrupt handler\n"); 132 return (ENXIO); 133 } 134 135 bus_generic_probe(dev); 136 bus_enumerate_hinted_children(dev); 137 bus_generic_attach(dev); 138 139 return (0); 140} 141 142static struct resource * 143apb_alloc_resource(device_t bus, device_t child, int type, int *rid, 144 u_long start, u_long end, u_long count, u_int flags) 145{ 146 struct apb_softc *sc = device_get_softc(bus); 147 struct apb_ivar *ivar = device_get_ivars(child); 148 struct resource *rv; 149 struct resource_list_entry *rle; 150 struct rman *rm; 151 int isdefault, needactivate, passthrough; 152 153 isdefault = (start == 0UL && end == ~0UL); 154 needactivate = flags & RF_ACTIVE; 155 /* 156 * Pass memory requests to nexus device 157 */ 158 passthrough = (device_get_parent(child) != bus); 159 rle = NULL; 160 161 dprintf("%s: entry (%p, %p, %d, %d, %p, %p, %ld, %d)\n", 162 __func__, bus, child, type, *rid, (void *)(intptr_t)start, 163 (void *)(intptr_t)end, count, flags); 164 165 if (passthrough) 166 return (BUS_ALLOC_RESOURCE(device_get_parent(bus), child, type, 167 rid, start, end, count, flags)); 168 169 /* 170 * If this is an allocation of the "default" range for a given RID, 171 * and we know what the resources for this device are (ie. they aren't 172 * maintained by a child bus), then work out the start/end values. 173 */ 174 175 if (isdefault) { 176 rle = resource_list_find(&ivar->resources, type, *rid); 177 if (rle == NULL) { 178 return (NULL); 179 } 180 181 if (rle->res != NULL) { 182 panic("%s: resource entry is busy", __func__); 183 } 184 start = rle->start; 185 end = rle->end; 186 count = rle->count; 187 188 dprintf("%s: default resource (%p, %p, %ld)\n", 189 __func__, (void *)(intptr_t)start, 190 (void *)(intptr_t)end, count); 191 } 192 193 switch (type) { 194 case SYS_RES_IRQ: 195 rm = &sc->apb_irq_rman; 196 break; 197 case SYS_RES_MEMORY: 198 rm = &sc->apb_mem_rman; 199 break; 200 default: 201 printf("%s: unknown resource type %d\n", __func__, type); 202 return (0); 203 } 204 205 rv = rman_reserve_resource(rm, start, end, count, flags, child); 206 if (rv == 0) { 207 printf("%s: could not reserve resource\n", __func__); 208 return (0); 209 } 210 211 rman_set_rid(rv, *rid); 212 213 if (needactivate) { 214 if (bus_activate_resource(child, type, *rid, rv)) { 215 printf("%s: could not activate resource\n", __func__); 216 rman_release_resource(rv); 217 return (0); 218 } 219 } 220 221 return (rv); 222} 223 224static int 225apb_activate_resource(device_t bus, device_t child, int type, int rid, 226 struct resource *r) 227{ 228 229 /* XXX: should we mask/unmask IRQ here? */ 230 return (BUS_ACTIVATE_RESOURCE(device_get_parent(bus), child, 231 type, rid, r)); 232} 233 234static int 235apb_deactivate_resource(device_t bus, device_t child, int type, int rid, 236 struct resource *r) 237{ 238 239 /* XXX: should we mask/unmask IRQ here? */ 240 return (BUS_DEACTIVATE_RESOURCE(device_get_parent(bus), child, 241 type, rid, r)); 242} 243 244static int 245apb_release_resource(device_t dev, device_t child, int type, 246 int rid, struct resource *r) 247{ 248 struct resource_list *rl; 249 struct resource_list_entry *rle; 250 251 rl = apb_get_resource_list(dev, child); 252 if (rl == NULL) 253 return (EINVAL); 254 rle = resource_list_find(rl, type, rid); 255 if (rle == NULL) 256 return (EINVAL); 257 rman_release_resource(r); 258 rle->res = NULL; 259 260 return (0); 261} 262 263static int 264apb_setup_intr(device_t bus, device_t child, struct resource *ires, 265 int flags, driver_filter_t *filt, driver_intr_t *handler, 266 void *arg, void **cookiep) 267{ 268 struct apb_softc *sc = device_get_softc(bus); 269 struct intr_event *event; 270 int irq, error; 271 272 irq = rman_get_start(ires); 273 274 if (irq > APB_IRQ_END) 275 panic("%s: bad irq %d", __func__, irq); 276 277 event = sc->sc_eventstab[irq]; 278 if (event == NULL) { 279 error = intr_event_create(&event, (void *)irq, 0, irq, 280 (mask_fn)apb_mask_irq, (mask_fn)apb_unmask_irq, 281 NULL, NULL, 282 "apb intr%d:", irq); 283 284 sc->sc_eventstab[irq] = event; 285 } 286 287 intr_event_add_handler(event, device_get_nameunit(child), filt, 288 handler, arg, intr_priority(flags), flags, cookiep); 289 290 apb_unmask_irq(irq); 291 292 return (0); 293} 294 295static int 296apb_teardown_intr(device_t dev, device_t child, struct resource *ires, 297 void *cookie) 298{ 299 struct apb_softc *sc = device_get_softc(dev); 300 int irq, result; 301 302 irq = rman_get_start(ires); 303 if (irq > APB_IRQ_END) 304 panic("%s: bad irq %d", __func__, irq); 305 306 if (sc->sc_eventstab[irq] == NULL) 307 panic("Trying to teardown unoccupied IRQ"); 308 309 apb_mask_irq(irq); 310 311 result = intr_event_remove_handler(cookie); 312 if (!result) 313 sc->sc_eventstab[irq] = NULL; 314 315 return (result); 316} 317 318static int 319apb_intr(void *arg) 320{ 321 struct apb_softc *sc = arg; 322 struct intr_event *event; 323 uint32_t reg, irq; 324 325 reg = ATH_READ_REG(AR71XX_MISC_INTR_STATUS); 326 for (irq = 0; irq < APB_NIRQS; irq++) { 327 if (reg & (1 << irq)) { 328 event = sc->sc_eventstab[irq]; 329 if (!event || TAILQ_EMPTY(&event->ie_handlers)) { 330 /* Ignore timer interrupts */ 331 if (irq != 0) 332 printf("Stray IRQ %d\n", irq); 333 continue; 334 } 335 336 /* TODO: frame instead of NULL? */ 337 intr_event_handle(event, NULL); 338 } 339 } 340 341 return (FILTER_HANDLED); 342} 343 344static void 345apb_hinted_child(device_t bus, const char *dname, int dunit) 346{ 347 device_t child; 348 long maddr; 349 int msize; 350 int irq; 351 int result; 352 int mem_hints_count; 353 354 child = BUS_ADD_CHILD(bus, 0, dname, dunit); 355 356 /* 357 * Set hard-wired resources for hinted child using 358 * specific RIDs. 359 */ 360 mem_hints_count = 0; 361 if (resource_long_value(dname, dunit, "maddr", &maddr) == 0) 362 mem_hints_count++; 363 if (resource_int_value(dname, dunit, "msize", &msize) == 0) 364 mem_hints_count++; 365 366 /* check if all info for mem resource has been provided */ 367 if ((mem_hints_count > 0) && (mem_hints_count < 2)) { 368 printf("Either maddr or msize hint is missing for %s%d\n", 369 dname, dunit); 370 } else if (mem_hints_count) { 371 result = bus_set_resource(child, SYS_RES_MEMORY, 0, 372 maddr, msize); 373 if (result != 0) 374 device_printf(bus, 375 "warning: bus_set_resource() failed\n"); 376 } 377 378 if (resource_int_value(dname, dunit, "irq", &irq) == 0) { 379 result = bus_set_resource(child, SYS_RES_IRQ, 0, irq, 1); 380 if (result != 0) 381 device_printf(bus, 382 "warning: bus_set_resource() failed\n"); 383 } 384} 385 386static device_t 387apb_add_child(device_t bus, int order, const char *name, int unit) 388{ 389 device_t child; 390 struct apb_ivar *ivar; 391 392 ivar = malloc(sizeof(struct apb_ivar), M_DEVBUF, M_WAITOK | M_ZERO); 393 if (ivar == NULL) { 394 printf("Failed to allocate ivar\n"); 395 return (0); 396 } 397 resource_list_init(&ivar->resources); 398 399 child = device_add_child_ordered(bus, order, name, unit); 400 if (child == NULL) { 401 printf("Can't add child %s%d ordered\n", name, unit); 402 return (0); 403 } 404 405 device_set_ivars(child, ivar); 406 407 return (child); 408} 409 410/* 411 * Helper routine for bus_generic_rl_get_resource/bus_generic_rl_set_resource 412 * Provides pointer to resource_list for these routines 413 */ 414static struct resource_list * 415apb_get_resource_list(device_t dev, device_t child) 416{ 417 struct apb_ivar *ivar; 418 419 ivar = device_get_ivars(child); 420 return (&(ivar->resources)); 421} 422 423static device_method_t apb_methods[] = { 424 DEVMETHOD(bus_activate_resource, apb_activate_resource), 425 DEVMETHOD(bus_add_child, apb_add_child), 426 DEVMETHOD(bus_alloc_resource, apb_alloc_resource), 427 DEVMETHOD(bus_deactivate_resource, apb_deactivate_resource), 428 DEVMETHOD(bus_get_resource_list, apb_get_resource_list), 429 DEVMETHOD(bus_hinted_child, apb_hinted_child), 430 DEVMETHOD(bus_print_child, bus_generic_print_child), 431 DEVMETHOD(bus_release_resource, apb_release_resource), 432 DEVMETHOD(bus_setup_intr, apb_setup_intr), 433 DEVMETHOD(bus_teardown_intr, apb_teardown_intr), 434 DEVMETHOD(device_attach, apb_attach), 435 DEVMETHOD(device_probe, apb_probe), 436 DEVMETHOD(bus_get_resource, bus_generic_rl_get_resource), 437 DEVMETHOD(bus_set_resource, bus_generic_rl_set_resource), 438 439 {0, 0}, 440}; 441 442static driver_t apb_driver = { 443 "apb", 444 apb_methods, 445 sizeof(struct apb_softc), 446}; 447static devclass_t apb_devclass; 448 449DRIVER_MODULE(apb, nexus, apb_driver, apb_devclass, 0, 0); 450