1 /*- 2 * Copyright (c) 2004-2006 Marcel Moolenaar 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 * 9 * 1. Redistributions of source code must retain the above copyright 10 * notice, this list of conditions and the following disclaimer. 11 * 2. Redistributions in binary form must reproduce the above copyright 12 * notice, this list of conditions and the following disclaimer in the 13 * documentation and/or other materials provided with the distribution. 14 * 15 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 16 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 17 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 18 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 19 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 20 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 21 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 22 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 23 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 24 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 25 */ 26 27 #include <sys/cdefs.h> 28 __FBSDID("$FreeBSD$"); 29 30 #include <sys/param.h> 31 #include <sys/systm.h> 32 #include <sys/bus.h> 33 #include <sys/conf.h> 34 #include <sys/kernel.h> 35 #include <sys/malloc.h> 36 #include <sys/queue.h> 37 #include <sys/serial.h> 38 39 #include <machine/bus.h> 40 #include <machine/resource.h> 41 #include <sys/rman.h> 42 43 #include <dev/scc/scc_bfe.h> 44 #include <dev/scc/scc_bus.h> 45 46 #include "scc_if.h" 47 48 devclass_t scc_devclass; 49 char scc_driver_name[] = "scc"; 50 51 MALLOC_DEFINE(M_SCC, "SCC", "SCC driver"); 52 53 static int 54 scc_bfe_intr(void *arg) 55 { 56 struct scc_softc *sc = arg; 57 struct scc_chan *ch; 58 struct scc_class *cl; 59 struct scc_mode *m; 60 int c, i, ipend, isrc; 61 62 cl = sc->sc_class; 63 while (!sc->sc_leaving && (ipend = SCC_IPEND(sc)) != 0) { 64 i = 0, isrc = SER_INT_OVERRUN; 65 while (ipend) { 66 while (i < SCC_ISRCCNT && !(ipend & isrc)) 67 i++, isrc <<= 1; 68 KASSERT(i < SCC_ISRCCNT, ("%s", __func__)); 69 ipend &= ~isrc; 70 for (c = 0; c < cl->cl_channels; c++) { 71 ch = &sc->sc_chan[c]; 72 if (!(ch->ch_ipend & isrc)) 73 continue; 74 m = &ch->ch_mode[0]; 75 if (m->ih_src[i] == NULL) 76 continue; 77 if ((*m->ih_src[i])(m->ih_arg)) 78 ch->ch_ipend &= ~isrc; 79 } 80 } 81 for (c = 0; c < cl->cl_channels; c++) { 82 ch = &sc->sc_chan[c]; 83 if (!ch->ch_ipend) 84 continue; 85 m = &ch->ch_mode[0]; 86 if (m->ih != NULL) 87 (*m->ih)(m->ih_arg); 88 else 89 SCC_ICLEAR(sc, ch); 90 } 91 return (FILTER_HANDLED); 92 } 93 return (FILTER_STRAY); 94 } 95 96 int 97 scc_bfe_attach(device_t dev) 98 { 99 struct resource_list_entry *rle; 100 struct scc_chan *ch; 101 struct scc_class *cl; 102 struct scc_mode *m; 103 struct scc_softc *sc, *sc0; 104 const char *sep; 105 bus_space_handle_t bh; 106 u_long base, size, start, sz; 107 int c, error, mode, sysdev; 108 109 /* 110 * The sc_class field defines the type of SCC we're going to work 111 * with and thus the size of the softc. Replace the generic softc 112 * with one that matches the SCC now that we're certain we handle 113 * the device. 114 */ 115 sc0 = device_get_softc(dev); 116 cl = sc0->sc_class; 117 if (cl->size > sizeof(*sc)) { 118 sc = malloc(cl->size, M_SCC, M_WAITOK|M_ZERO); 119 bcopy(sc0, sc, sizeof(*sc)); 120 device_set_softc(dev, sc); 121 } else 122 sc = sc0; 123 124 size = abs(cl->cl_range) << sc->sc_bas.regshft; 125 126 mtx_init(&sc->sc_hwmtx, "scc_hwmtx", NULL, MTX_SPIN); 127 128 /* 129 * Re-allocate. We expect that the softc contains the information 130 * collected by scc_bfe_probe() intact. 131 */ 132 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 133 0, ~0, cl->cl_channels * size, RF_ACTIVE); 134 if (sc->sc_rres == NULL) 135 return (ENXIO); 136 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 137 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 138 139 /* 140 * Allocate interrupt resources. There may be a different interrupt 141 * per channel. We allocate them all... 142 */ 143 sc->sc_chan = malloc(sizeof(struct scc_chan) * cl->cl_channels, 144 M_SCC, M_WAITOK | M_ZERO); 145 for (c = 0; c < cl->cl_channels; c++) { 146 ch = &sc->sc_chan[c]; 147 ch->ch_irid = c; 148 ch->ch_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, 149 &ch->ch_irid, RF_ACTIVE | RF_SHAREABLE); 150 } 151 152 /* 153 * Create the control structures for our children. Probe devices 154 * and query them to see if we can reset the hardware. 155 */ 156 sysdev = 0; 157 base = rman_get_start(sc->sc_rres); 158 sz = (size != 0) ? size : rman_get_size(sc->sc_rres); 159 start = base + ((cl->cl_range < 0) ? size * (cl->cl_channels - 1) : 0); 160 for (c = 0; c < cl->cl_channels; c++) { 161 ch = &sc->sc_chan[c]; 162 resource_list_init(&ch->ch_rlist); 163 ch->ch_nr = c + 1; 164 165 if (!SCC_ENABLED(sc, ch)) 166 goto next; 167 168 ch->ch_enabled = 1; 169 resource_list_add(&ch->ch_rlist, sc->sc_rtype, 0, start, 170 start + sz - 1, sz); 171 rle = resource_list_find(&ch->ch_rlist, sc->sc_rtype, 0); 172 rle->res = &ch->ch_rres; 173 bus_space_subregion(rman_get_bustag(sc->sc_rres), 174 rman_get_bushandle(sc->sc_rres), start - base, sz, &bh); 175 rman_set_bushandle(rle->res, bh); 176 rman_set_bustag(rle->res, rman_get_bustag(sc->sc_rres)); 177 178 resource_list_add(&ch->ch_rlist, SYS_RES_IRQ, 0, c, c, 1); 179 rle = resource_list_find(&ch->ch_rlist, SYS_RES_IRQ, 0); 180 rle->res = (ch->ch_ires != NULL) ? ch->ch_ires : 181 sc->sc_chan[0].ch_ires; 182 183 for (mode = 0; mode < SCC_NMODES; mode++) { 184 m = &ch->ch_mode[mode]; 185 m->m_chan = ch; 186 m->m_mode = 1U << mode; 187 if ((cl->cl_modes & m->m_mode) == 0 || ch->ch_sysdev) 188 continue; 189 m->m_dev = device_add_child(dev, NULL, -1); 190 device_set_ivars(m->m_dev, (void *)m); 191 error = device_probe_child(dev, m->m_dev); 192 if (!error) { 193 m->m_probed = 1; 194 m->m_sysdev = SERDEV_SYSDEV(m->m_dev) ? 1 : 0; 195 ch->ch_sysdev |= m->m_sysdev; 196 } 197 } 198 199 next: 200 start += (cl->cl_range < 0) ? -size : size; 201 sysdev |= ch->ch_sysdev; 202 } 203 204 /* 205 * Have the hardware driver initialize the hardware. Tell it 206 * whether or not a hardware reset should be performed. 207 */ 208 if (bootverbose) { 209 device_printf(dev, "%sresetting hardware\n", 210 (sysdev) ? "not " : ""); 211 } 212 error = SCC_ATTACH(sc, !sysdev); 213 if (error) 214 goto fail; 215 216 /* 217 * Setup our interrupt handler. Make it FAST under the assumption 218 * that our children's are fast as well. We make it MPSAFE as soon 219 * as a child sets up a MPSAFE interrupt handler. 220 * Of course, if we can't setup a fast handler, we make it MPSAFE 221 * right away. 222 */ 223 for (c = 0; c < cl->cl_channels; c++) { 224 ch = &sc->sc_chan[c]; 225 if (ch->ch_ires == NULL) 226 continue; 227 error = bus_setup_intr(dev, ch->ch_ires, 228 INTR_TYPE_TTY, scc_bfe_intr, NULL, sc, 229 &ch->ch_icookie); 230 if (error) { 231 error = bus_setup_intr(dev, ch->ch_ires, 232 INTR_TYPE_TTY | INTR_MPSAFE, NULL, 233 (driver_intr_t *)scc_bfe_intr, sc, &ch->ch_icookie); 234 } else 235 sc->sc_fastintr = 1; 236 237 if (error) { 238 device_printf(dev, "could not activate interrupt\n"); 239 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid, 240 ch->ch_ires); 241 ch->ch_ires = NULL; 242 } 243 } 244 sc->sc_polled = 1; 245 for (c = 0; c < cl->cl_channels; c++) { 246 if (sc->sc_chan[0].ch_ires != NULL) 247 sc->sc_polled = 0; 248 } 249 250 /* 251 * Attach all child devices that were probed successfully. 252 */ 253 for (c = 0; c < cl->cl_channels; c++) { 254 ch = &sc->sc_chan[c]; 255 for (mode = 0; mode < SCC_NMODES; mode++) { 256 m = &ch->ch_mode[mode]; 257 if (!m->m_probed) 258 continue; 259 error = device_attach(m->m_dev); 260 if (error) 261 continue; 262 m->m_attached = 1; 263 } 264 } 265 266 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) { 267 sep = ""; 268 device_print_prettyname(dev); 269 if (sc->sc_fastintr) { 270 printf("%sfast interrupt", sep); 271 sep = ", "; 272 } 273 if (sc->sc_polled) { 274 printf("%spolled mode", sep); 275 sep = ", "; 276 } 277 printf("\n"); 278 } 279 280 return (0); 281 282 fail: 283 for (c = 0; c < cl->cl_channels; c++) { 284 ch = &sc->sc_chan[c]; 285 if (ch->ch_ires == NULL) 286 continue; 287 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid, 288 ch->ch_ires); 289 } 290 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 291 return (error); 292 } 293 294 int 295 scc_bfe_detach(device_t dev) 296 { 297 struct scc_chan *ch; 298 struct scc_class *cl; 299 struct scc_mode *m; 300 struct scc_softc *sc; 301 int chan, error, mode; 302 303 sc = device_get_softc(dev); 304 cl = sc->sc_class; 305 306 /* Detach our children. */ 307 error = 0; 308 for (chan = 0; chan < cl->cl_channels; chan++) { 309 ch = &sc->sc_chan[chan]; 310 for (mode = 0; mode < SCC_NMODES; mode++) { 311 m = &ch->ch_mode[mode]; 312 if (!m->m_attached) 313 continue; 314 if (device_detach(m->m_dev) != 0) 315 error = ENXIO; 316 else 317 m->m_attached = 0; 318 } 319 } 320 321 if (error) 322 return (error); 323 324 for (chan = 0; chan < cl->cl_channels; chan++) { 325 ch = &sc->sc_chan[chan]; 326 if (ch->ch_ires == NULL) 327 continue; 328 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie); 329 bus_release_resource(dev, SYS_RES_IRQ, ch->ch_irid, 330 ch->ch_ires); 331 } 332 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 333 334 free(sc->sc_chan, M_SCC); 335 336 mtx_destroy(&sc->sc_hwmtx); 337 return (0); 338 } 339 340 int 341 scc_bfe_probe(device_t dev, u_int regshft, u_int rclk, u_int rid) 342 { 343 struct scc_softc *sc; 344 struct scc_class *cl; 345 u_long size, sz; 346 int error; 347 348 /* 349 * Initialize the instance. Note that the instance (=softc) does 350 * not necessarily match the hardware specific softc. We can't do 351 * anything about it now, because we may not attach to the device. 352 * Hardware drivers cannot use any of the class specific fields 353 * while probing. 354 */ 355 sc = device_get_softc(dev); 356 cl = sc->sc_class; 357 kobj_init((kobj_t)sc, (kobj_class_t)cl); 358 sc->sc_dev = dev; 359 if (device_get_desc(dev) == NULL) 360 device_set_desc(dev, cl->name); 361 362 size = abs(cl->cl_range) << regshft; 363 364 /* 365 * Allocate the register resource. We assume that all SCCs have a 366 * single register window in either I/O port space or memory mapped 367 * I/O space. Any SCC that needs multiple windows will consequently 368 * not be supported by this driver as-is. 369 */ 370 sc->sc_rrid = rid; 371 sc->sc_rtype = SYS_RES_MEMORY; 372 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 373 0, ~0, cl->cl_channels * size, RF_ACTIVE); 374 if (sc->sc_rres == NULL) { 375 sc->sc_rrid = rid; 376 sc->sc_rtype = SYS_RES_IOPORT; 377 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, 378 &sc->sc_rrid, 0, ~0, cl->cl_channels * size, RF_ACTIVE); 379 if (sc->sc_rres == NULL) 380 return (ENXIO); 381 } 382 383 /* 384 * Fill in the bus access structure and call the hardware specific 385 * probe method. 386 */ 387 sz = (size != 0) ? size : rman_get_size(sc->sc_rres); 388 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 389 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 390 sc->sc_bas.range = sz; 391 sc->sc_bas.rclk = rclk; 392 sc->sc_bas.regshft = regshft; 393 394 error = SCC_PROBE(sc); 395 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 396 return ((error == 0) ? BUS_PROBE_DEFAULT : error); 397 } 398 399 struct resource * 400 scc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid, 401 u_long start, u_long end, u_long count, u_int flags) 402 { 403 struct resource_list_entry *rle; 404 struct scc_chan *ch; 405 struct scc_mode *m; 406 407 if (device_get_parent(child) != dev) 408 return (NULL); 409 410 /* We only support default allocations. */ 411 if (start != 0UL || end != ~0UL) 412 return (NULL); 413 414 m = device_get_ivars(child); 415 ch = m->m_chan; 416 rle = resource_list_find(&ch->ch_rlist, type, 0); 417 if (rle == NULL) 418 return (NULL); 419 *rid = 0; 420 return (rle->res); 421 } 422 423 int 424 scc_bus_get_resource(device_t dev, device_t child, int type, int rid, 425 u_long *startp, u_long *countp) 426 { 427 struct resource_list_entry *rle; 428 struct scc_chan *ch; 429 struct scc_mode *m; 430 431 if (device_get_parent(child) != dev) 432 return (EINVAL); 433 434 m = device_get_ivars(child); 435 ch = m->m_chan; 436 rle = resource_list_find(&ch->ch_rlist, type, rid); 437 if (rle == NULL) 438 return (EINVAL); 439 440 if (startp != NULL) 441 *startp = rle->start; 442 if (countp != NULL) 443 *countp = rle->count; 444 return (0); 445 } 446 447 int 448 scc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 449 { 450 struct scc_chan *ch; 451 struct scc_class *cl; 452 struct scc_mode *m; 453 struct scc_softc *sc; 454 455 if (device_get_parent(child) != dev) 456 return (EINVAL); 457 458 sc = device_get_softc(dev); 459 cl = sc->sc_class; 460 m = device_get_ivars(child); 461 ch = m->m_chan; 462 463 switch (index) { 464 case SCC_IVAR_CHANNEL: 465 *result = ch->ch_nr; 466 break; 467 case SCC_IVAR_CLASS: 468 *result = cl->cl_class; 469 break; 470 case SCC_IVAR_CLOCK: 471 *result = sc->sc_bas.rclk; 472 break; 473 case SCC_IVAR_MODE: 474 *result = m->m_mode; 475 break; 476 case SCC_IVAR_REGSHFT: 477 *result = sc->sc_bas.regshft; 478 break; 479 case SCC_IVAR_HWMTX: 480 *result = (uintptr_t)&sc->sc_hwmtx; 481 break; 482 default: 483 return (EINVAL); 484 } 485 return (0); 486 } 487 488 int 489 scc_bus_release_resource(device_t dev, device_t child, int type, int rid, 490 struct resource *res) 491 { 492 struct resource_list_entry *rle; 493 struct scc_chan *ch; 494 struct scc_mode *m; 495 496 if (device_get_parent(child) != dev) 497 return (EINVAL); 498 499 m = device_get_ivars(child); 500 ch = m->m_chan; 501 rle = resource_list_find(&ch->ch_rlist, type, rid); 502 return ((rle == NULL) ? EINVAL : 0); 503 } 504 505 int 506 scc_bus_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 507 driver_filter_t *filt, void (*ihand)(void *), void *arg, void **cookiep) 508 { 509 struct scc_chan *ch; 510 struct scc_mode *m; 511 struct scc_softc *sc; 512 int c, i, isrc; 513 514 if (device_get_parent(child) != dev) 515 return (EINVAL); 516 517 /* Interrupt handlers must be FAST or MPSAFE. */ 518 if (filt == NULL && !(flags & INTR_MPSAFE)) 519 return (EINVAL); 520 521 sc = device_get_softc(dev); 522 if (sc->sc_polled) 523 return (ENXIO); 524 525 if (sc->sc_fastintr && filt == NULL) { 526 sc->sc_fastintr = 0; 527 for (c = 0; c < sc->sc_class->cl_channels; c++) { 528 ch = &sc->sc_chan[c]; 529 if (ch->ch_ires == NULL) 530 continue; 531 bus_teardown_intr(dev, ch->ch_ires, ch->ch_icookie); 532 bus_setup_intr(dev, ch->ch_ires, 533 INTR_TYPE_TTY | INTR_MPSAFE, NULL, 534 (driver_intr_t *)scc_bfe_intr, sc, &ch->ch_icookie); 535 } 536 } 537 538 m = device_get_ivars(child); 539 m->m_hasintr = 1; 540 m->m_fastintr = (filt != NULL) ? 1 : 0; 541 m->ih = (filt != NULL) ? filt : (driver_filter_t *)ihand; 542 m->ih_arg = arg; 543 544 i = 0, isrc = SER_INT_OVERRUN; 545 while (i < SCC_ISRCCNT) { 546 m->ih_src[i] = SERDEV_IHAND(child, isrc); 547 if (m->ih_src[i] != NULL) 548 m->ih = NULL; 549 i++, isrc <<= 1; 550 } 551 return (0); 552 } 553 554 int 555 scc_bus_teardown_intr(device_t dev, device_t child, struct resource *r, 556 void *cookie) 557 { 558 struct scc_mode *m; 559 int i; 560 561 if (device_get_parent(child) != dev) 562 return (EINVAL); 563 564 m = device_get_ivars(child); 565 if (!m->m_hasintr) 566 return (EINVAL); 567 568 m->m_hasintr = 0; 569 m->m_fastintr = 0; 570 m->ih = NULL; 571 m->ih_arg = NULL; 572 for (i = 0; i < SCC_ISRCCNT; i++) 573 m->ih_src[i] = NULL; 574 return (0); 575 } 576