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 void 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 } 92 } 93 94 int 95 scc_bfe_attach(device_t dev) 96 { 97 struct resource_list_entry *rle; 98 struct scc_chan *ch; 99 struct scc_class *cl; 100 struct scc_mode *m; 101 struct scc_softc *sc, *sc0; 102 const char *sep; 103 bus_space_handle_t bh; 104 u_long base, size, start; 105 int c, error, mode, sysdev; 106 107 /* 108 * The sc_class field defines the type of SCC we're going to work 109 * with and thus the size of the softc. Replace the generic softc 110 * with one that matches the SCC now that we're certain we handle 111 * the device. 112 */ 113 sc0 = device_get_softc(dev); 114 cl = sc0->sc_class; 115 if (cl->size > sizeof(*sc)) { 116 sc = malloc(cl->size, M_SCC, M_WAITOK|M_ZERO); 117 bcopy(sc0, sc, sizeof(*sc)); 118 device_set_softc(dev, sc); 119 } else 120 sc = sc0; 121 122 size = abs(cl->cl_range); 123 124 mtx_init(&sc->sc_hwmtx, "scc_hwmtx", NULL, MTX_SPIN); 125 126 /* 127 * Re-allocate. We expect that the softc contains the information 128 * collected by scc_bfe_probe() intact. 129 */ 130 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 131 0, ~0, cl->cl_channels * size, RF_ACTIVE); 132 if (sc->sc_rres == NULL) 133 return (ENXIO); 134 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 135 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 136 137 sc->sc_irid = 0; 138 sc->sc_ires = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->sc_irid, 139 RF_ACTIVE | RF_SHAREABLE); 140 141 /* 142 * Create the control structures for our children. Probe devices 143 * and query them to see if we can reset the hardware. 144 */ 145 sysdev = 0; 146 sc->sc_chan = malloc(sizeof(struct scc_chan) * cl->cl_channels, 147 M_SCC, M_WAITOK | M_ZERO); 148 base = rman_get_start(sc->sc_rres); 149 start = base + ((cl->cl_range < 0) ? size * (cl->cl_channels - 1) : 0); 150 for (c = 0; c < cl->cl_channels; c++) { 151 ch = &sc->sc_chan[c]; 152 resource_list_init(&ch->ch_rlist); 153 ch->ch_nr = c + 1; 154 155 resource_list_add(&ch->ch_rlist, sc->sc_rtype, 0, start, 156 start + size - 1, size); 157 rle = resource_list_find(&ch->ch_rlist, sc->sc_rtype, 0); 158 rle->res = &ch->ch_rres; 159 bus_space_subregion(rman_get_bustag(sc->sc_rres), 160 rman_get_bushandle(sc->sc_rres), start - base, size, &bh); 161 rman_set_bushandle(rle->res, bh); 162 rman_set_bustag(rle->res, rman_get_bustag(sc->sc_rres)); 163 164 resource_list_add(&ch->ch_rlist, SYS_RES_IRQ, 0, c, c, 1); 165 rle = resource_list_find(&ch->ch_rlist, SYS_RES_IRQ, 0); 166 rle->res = sc->sc_ires; 167 168 for (mode = 0; mode < SCC_NMODES; mode++) { 169 m = &ch->ch_mode[mode]; 170 m->m_chan = ch; 171 m->m_mode = 1U << mode; 172 if ((cl->cl_modes & m->m_mode) == 0 || ch->ch_sysdev) 173 continue; 174 m->m_dev = device_add_child(dev, NULL, -1); 175 device_set_ivars(m->m_dev, (void *)m); 176 error = device_probe_child(dev, m->m_dev); 177 if (!error) { 178 m->m_probed = 1; 179 m->m_sysdev = SERDEV_SYSDEV(m->m_dev) ? 1 : 0; 180 ch->ch_sysdev |= m->m_sysdev; 181 } 182 } 183 184 start += (cl->cl_range < 0) ? -size : size; 185 sysdev |= ch->ch_sysdev; 186 } 187 188 /* 189 * Have the hardware driver initialize the hardware. Tell it 190 * whether or not a hardware reset should be performed. 191 */ 192 if (bootverbose) { 193 device_printf(dev, "%sresetting hardware\n", 194 (sysdev) ? "not " : ""); 195 } 196 error = SCC_ATTACH(sc, !sysdev); 197 if (error) 198 goto fail; 199 200 /* 201 * Setup our interrupt handler. Make it FAST under the assumption 202 * that our children's are fast as well. We make it MPSAFE as soon 203 * as a child sets up a MPSAFE interrupt handler. 204 * Of course, if we can't setup a fast handler, we make it MPSAFE 205 * right away. 206 */ 207 if (sc->sc_ires != NULL) { 208 error = bus_setup_intr(dev, sc->sc_ires, 209 INTR_TYPE_TTY | INTR_FAST, scc_bfe_intr, sc, 210 &sc->sc_icookie); 211 if (error) { 212 error = bus_setup_intr(dev, sc->sc_ires, 213 INTR_TYPE_TTY | INTR_MPSAFE, scc_bfe_intr, sc, 214 &sc->sc_icookie); 215 } else 216 sc->sc_fastintr = 1; 217 218 if (error) { 219 device_printf(dev, "could not activate interrupt\n"); 220 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 221 sc->sc_ires); 222 sc->sc_ires = NULL; 223 } 224 } 225 if (sc->sc_ires == NULL) { 226 /* XXX no interrupt resource. Force polled mode. */ 227 sc->sc_polled = 1; 228 } 229 230 /* 231 * Attach all child devices that were probed successfully. 232 */ 233 for (c = 0; c < cl->cl_channels; c++) { 234 ch = &sc->sc_chan[c]; 235 for (mode = 0; mode < SCC_NMODES; mode++) { 236 m = &ch->ch_mode[mode]; 237 if (!m->m_probed) 238 continue; 239 error = device_attach(m->m_dev); 240 if (error) 241 continue; 242 m->m_attached = 1; 243 } 244 } 245 246 if (bootverbose && (sc->sc_fastintr || sc->sc_polled)) { 247 sep = ""; 248 device_print_prettyname(dev); 249 if (sc->sc_fastintr) { 250 printf("%sfast interrupt", sep); 251 sep = ", "; 252 } 253 if (sc->sc_polled) { 254 printf("%spolled mode", sep); 255 sep = ", "; 256 } 257 printf("\n"); 258 } 259 260 return (0); 261 262 fail: 263 if (sc->sc_ires != NULL) { 264 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 265 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 266 sc->sc_ires); 267 } 268 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 269 return (error); 270 } 271 272 int 273 scc_bfe_detach(device_t dev) 274 { 275 struct scc_chan *ch; 276 struct scc_class *cl; 277 struct scc_mode *m; 278 struct scc_softc *sc; 279 int chan, error, mode; 280 281 sc = device_get_softc(dev); 282 cl = sc->sc_class; 283 284 /* Detach our children. */ 285 error = 0; 286 for (chan = 0; chan < cl->cl_channels; chan++) { 287 ch = &sc->sc_chan[chan]; 288 for (mode = 0; mode < SCC_NMODES; mode++) { 289 m = &ch->ch_mode[mode]; 290 if (!m->m_attached) 291 continue; 292 if (device_detach(m->m_dev) != 0) 293 error = ENXIO; 294 else 295 m->m_attached = 0; 296 } 297 } 298 299 if (error) 300 return (error); 301 302 if (sc->sc_ires != NULL) { 303 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 304 bus_release_resource(dev, SYS_RES_IRQ, sc->sc_irid, 305 sc->sc_ires); 306 } 307 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 308 309 free(sc->sc_chan, M_SCC); 310 311 mtx_destroy(&sc->sc_hwmtx); 312 return (0); 313 } 314 315 int 316 scc_bfe_probe(device_t dev, u_int regshft, u_int rclk) 317 { 318 struct scc_softc *sc; 319 struct scc_class *cl; 320 u_long size; 321 int error; 322 323 /* 324 * Initialize the instance. Note that the instance (=softc) does 325 * not necessarily match the hardware specific softc. We can't do 326 * anything about it now, because we may not attach to the device. 327 * Hardware drivers cannot use any of the class specific fields 328 * while probing. 329 */ 330 sc = device_get_softc(dev); 331 cl = sc->sc_class; 332 kobj_init((kobj_t)sc, (kobj_class_t)cl); 333 sc->sc_dev = dev; 334 if (device_get_desc(dev) == NULL) 335 device_set_desc(dev, cl->name); 336 337 size = abs(cl->cl_range); 338 339 /* 340 * Allocate the register resource. We assume that all SCCs have a 341 * single register window in either I/O port space or memory mapped 342 * I/O space. Any SCC that needs multiple windows will consequently 343 * not be supported by this driver as-is. 344 */ 345 sc->sc_rrid = 0; 346 sc->sc_rtype = SYS_RES_MEMORY; 347 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, &sc->sc_rrid, 348 0, ~0, cl->cl_channels * size, RF_ACTIVE); 349 if (sc->sc_rres == NULL) { 350 sc->sc_rrid = 0; 351 sc->sc_rtype = SYS_RES_IOPORT; 352 sc->sc_rres = bus_alloc_resource(dev, sc->sc_rtype, 353 &sc->sc_rrid, 0, ~0, cl->cl_channels * size, RF_ACTIVE); 354 if (sc->sc_rres == NULL) 355 return (ENXIO); 356 } 357 358 /* 359 * Fill in the bus access structure and call the hardware specific 360 * probe method. 361 */ 362 sc->sc_bas.bsh = rman_get_bushandle(sc->sc_rres); 363 sc->sc_bas.bst = rman_get_bustag(sc->sc_rres); 364 sc->sc_bas.range = size; 365 sc->sc_bas.rclk = rclk; 366 sc->sc_bas.regshft = regshft; 367 368 error = SCC_PROBE(sc); 369 bus_release_resource(dev, sc->sc_rtype, sc->sc_rrid, sc->sc_rres); 370 return ((error == 0) ? BUS_PROBE_DEFAULT : error); 371 } 372 373 struct resource * 374 scc_bus_alloc_resource(device_t dev, device_t child, int type, int *rid, 375 u_long start, u_long end, u_long count, u_int flags) 376 { 377 struct resource_list_entry *rle; 378 struct scc_chan *ch; 379 struct scc_mode *m; 380 381 if (device_get_parent(child) != dev) 382 return (NULL); 383 384 /* We only support default allocations. */ 385 if (start != 0UL || end != ~0UL) 386 return (NULL); 387 388 m = device_get_ivars(child); 389 ch = m->m_chan; 390 rle = resource_list_find(&ch->ch_rlist, type, 0); 391 if (rle == NULL) 392 return (NULL); 393 *rid = 0; 394 return (rle->res); 395 } 396 397 int 398 scc_bus_get_resource(device_t dev, device_t child, int type, int rid, 399 u_long *startp, u_long *countp) 400 { 401 struct resource_list_entry *rle; 402 struct scc_chan *ch; 403 struct scc_mode *m; 404 405 if (device_get_parent(child) != dev) 406 return (EINVAL); 407 408 m = device_get_ivars(child); 409 ch = m->m_chan; 410 rle = resource_list_find(&ch->ch_rlist, type, rid); 411 if (rle == NULL) 412 return (EINVAL); 413 414 if (startp != NULL) 415 *startp = rle->start; 416 if (countp != NULL) 417 *countp = rle->count; 418 return (0); 419 } 420 421 int 422 scc_bus_read_ivar(device_t dev, device_t child, int index, uintptr_t *result) 423 { 424 struct scc_chan *ch; 425 struct scc_class *cl; 426 struct scc_mode *m; 427 struct scc_softc *sc; 428 429 if (device_get_parent(child) != dev) 430 return (EINVAL); 431 432 sc = device_get_softc(dev); 433 cl = sc->sc_class; 434 m = device_get_ivars(child); 435 ch = m->m_chan; 436 437 switch (index) { 438 case SCC_IVAR_CHANNEL: 439 *result = ch->ch_nr; 440 break; 441 case SCC_IVAR_CLASS: 442 *result = cl->cl_class; 443 break; 444 case SCC_IVAR_CLOCK: 445 *result = sc->sc_bas.rclk; 446 break; 447 case SCC_IVAR_MODE: 448 *result = m->m_mode; 449 break; 450 case SCC_IVAR_REGSHFT: 451 *result = sc->sc_bas.regshft; 452 break; 453 case SCC_IVAR_HWMTX: 454 *result = (uintptr_t)&sc->sc_hwmtx; 455 break; 456 default: 457 return (EINVAL); 458 } 459 return (0); 460 } 461 462 int 463 scc_bus_release_resource(device_t dev, device_t child, int type, int rid, 464 struct resource *res) 465 { 466 struct resource_list_entry *rle; 467 struct scc_chan *ch; 468 struct scc_mode *m; 469 470 if (device_get_parent(child) != dev) 471 return (EINVAL); 472 473 m = device_get_ivars(child); 474 ch = m->m_chan; 475 rle = resource_list_find(&ch->ch_rlist, type, rid); 476 return ((rle == NULL) ? EINVAL : 0); 477 } 478 479 int 480 scc_bus_setup_intr(device_t dev, device_t child, struct resource *r, int flags, 481 void (*ihand)(void *), void *arg, void **cookiep) 482 { 483 struct scc_mode *m; 484 struct scc_softc *sc; 485 int i, isrc; 486 487 if (device_get_parent(child) != dev) 488 return (EINVAL); 489 490 /* Interrupt handlers must be FAST or MPSAFE. */ 491 if ((flags & (INTR_FAST|INTR_MPSAFE)) == 0) 492 return (EINVAL); 493 494 sc = device_get_softc(dev); 495 if (sc->sc_polled) 496 return (ENXIO); 497 498 if (sc->sc_fastintr && !(flags & INTR_FAST)) { 499 sc->sc_fastintr = 0; 500 bus_teardown_intr(dev, sc->sc_ires, sc->sc_icookie); 501 bus_setup_intr(dev, sc->sc_ires, INTR_TYPE_TTY | INTR_MPSAFE, 502 scc_bfe_intr, sc, &sc->sc_icookie); 503 } 504 505 m = device_get_ivars(child); 506 m->m_hasintr = 1; 507 m->m_fastintr = (flags & INTR_FAST) ? 1 : 0; 508 m->ih = ihand; 509 m->ih_arg = arg; 510 511 i = 0, isrc = SER_INT_OVERRUN; 512 while (i < SCC_ISRCCNT) { 513 m->ih_src[i] = SERDEV_IHAND(child, isrc); 514 if (m->ih_src[i] != NULL) 515 m->ih = NULL; 516 i++, isrc <<= 1; 517 } 518 return (0); 519 } 520 521 int 522 scc_bus_teardown_intr(device_t dev, device_t child, struct resource *r, 523 void *cookie) 524 { 525 struct scc_mode *m; 526 int i; 527 528 if (device_get_parent(child) != dev) 529 return (EINVAL); 530 531 m = device_get_ivars(child); 532 if (!m->m_hasintr) 533 return (EINVAL); 534 535 m->m_hasintr = 0; 536 m->m_fastintr = 0; 537 m->ih = NULL; 538 m->ih_arg = NULL; 539 for (i = 0; i < SCC_ISRCCNT; i++) 540 m->ih_src[i] = NULL; 541 return (0); 542 } 543