1 /*- 2 * Copyright (C) 2011,2015 by Nathan Whitehorn. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions 6 * are met: 7 * 1. Redistributions of source code must retain the above copyright 8 * notice, this list of conditions and the following disclaimer. 9 * 2. Redistributions in binary form must reproduce the above copyright 10 * notice, this list of conditions and the following disclaimer in the 11 * documentation and/or other materials provided with the distribution. 12 * 13 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 14 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 15 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 16 * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 17 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 18 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 19 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 20 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 21 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 22 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 23 */ 24 25 #include <sys/cdefs.h> 26 __FBSDID("$FreeBSD$"); 27 28 #include <sys/param.h> 29 #include <sys/conf.h> 30 #include <sys/cons.h> 31 #include <sys/kdb.h> 32 #include <sys/kernel.h> 33 #include <sys/lock.h> 34 #include <sys/module.h> 35 #include <sys/mutex.h> 36 #include <sys/priv.h> 37 #include <sys/proc.h> 38 #include <sys/systm.h> 39 #include <sys/tty.h> 40 41 #include <vm/vm.h> 42 #include <vm/pmap.h> 43 44 #include <machine/bus.h> 45 46 #include <dev/ofw/openfirm.h> 47 #include <dev/ofw/ofw_bus.h> 48 #include <dev/ofw/ofw_bus_subr.h> 49 #include <dev/uart/uart.h> 50 #include <dev/uart/uart_cpu.h> 51 #include <dev/uart/uart_bus.h> 52 53 #include "opal.h" 54 #include "uart_if.h" 55 56 struct uart_opal_softc { 57 device_t dev; 58 phandle_t node; 59 int vtermid; 60 61 struct tty *tp; 62 struct resource *irqres; 63 int irqrid; 64 struct callout callout; 65 void *sc_icookie; 66 int polltime; 67 68 struct mtx sc_mtx; 69 int protocol; 70 71 char opal_inbuf[16]; 72 uint64_t inbuflen; 73 uint8_t outseqno; 74 #if defined(KDB) 75 int alt_break_state; 76 #endif 77 }; 78 79 static struct uart_opal_softc *console_sc = NULL; 80 static struct consdev *stdout_cp; 81 82 enum { 83 OPAL_RAW, OPAL_HVSI 84 }; 85 86 #define VS_DATA_PACKET_HEADER 0xff 87 #define VS_CONTROL_PACKET_HEADER 0xfe 88 #define VSV_SET_MODEM_CTL 0x01 89 #define VSV_MODEM_CTL_UPDATE 0x02 90 #define VSV_RENEGOTIATE_CONNECTION 0x03 91 #define VS_QUERY_PACKET_HEADER 0xfd 92 #define VSV_SEND_VERSION_NUMBER 0x01 93 #define VSV_SEND_MODEM_CTL_STATUS 0x02 94 #define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc 95 96 static int uart_opal_probe(device_t dev); 97 static int uart_opal_attach(device_t dev); 98 static void uart_opal_intr(void *v); 99 100 static device_method_t uart_opal_methods[] = { 101 /* Device interface */ 102 DEVMETHOD(device_probe, uart_opal_probe), 103 DEVMETHOD(device_attach, uart_opal_attach), 104 105 DEVMETHOD_END 106 }; 107 108 static driver_t uart_opal_driver = { 109 "uart", 110 uart_opal_methods, 111 sizeof(struct uart_opal_softc), 112 }; 113 114 DRIVER_MODULE(uart_opal, opalcons, uart_opal_driver, uart_devclass, 0, 0); 115 116 static int uart_opal_getc(struct uart_opal_softc *sc); 117 static cn_probe_t uart_opal_cnprobe; 118 static cn_init_t uart_opal_cninit; 119 static cn_term_t uart_opal_cnterm; 120 static cn_getc_t uart_opal_cngetc; 121 static cn_putc_t uart_opal_cnputc; 122 static cn_grab_t uart_opal_cngrab; 123 static cn_ungrab_t uart_opal_cnungrab; 124 125 CONSOLE_DRIVER(uart_opal); 126 127 static void uart_opal_ttyoutwakeup(struct tty *tp); 128 129 static struct ttydevsw uart_opal_tty_class = { 130 .tsw_flags = TF_INITLOCK|TF_CALLOUT, 131 .tsw_outwakeup = uart_opal_ttyoutwakeup, 132 }; 133 134 static struct { 135 char tmpbuf[16]; 136 uint64_t size; 137 struct mtx mtx; 138 } opalcons_buffer; 139 140 static void 141 uart_opal_real_map_outbuffer(uint64_t *bufferp, uint64_t *lenp) 142 { 143 144 if (!mtx_initialized(&opalcons_buffer.mtx)) 145 mtx_init(&opalcons_buffer.mtx, "uart_opal", NULL, 146 MTX_SPIN | MTX_QUIET | MTX_NOWITNESS); 147 148 if (!pmap_bootstrapped) 149 return; 150 151 mtx_lock_spin(&opalcons_buffer.mtx); 152 153 opalcons_buffer.size = *(uint64_t *)(*lenp) = 154 min(sizeof(opalcons_buffer.tmpbuf), *(uint64_t *)(*lenp)); 155 memcpy(opalcons_buffer.tmpbuf, (void *)(*bufferp), 156 *(uint64_t *)(*lenp)); 157 *bufferp = (uint64_t)opalcons_buffer.tmpbuf; 158 *lenp = (uint64_t)&opalcons_buffer.size; 159 } 160 161 static void 162 uart_opal_real_unmap_outbuffer(uint64_t *len) 163 { 164 165 if (!pmap_bootstrapped) 166 return; 167 168 mtx_assert(&opalcons_buffer.mtx, MA_OWNED); 169 *len = opalcons_buffer.size; 170 mtx_unlock_spin(&opalcons_buffer.mtx); 171 } 172 173 static int 174 uart_opal_probe_node(struct uart_opal_softc *sc) 175 { 176 phandle_t node = sc->node; 177 uint32_t reg; 178 char buf[64]; 179 180 sc->inbuflen = 0; 181 sc->outseqno = 0; 182 183 if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0) 184 return (ENXIO); 185 if (strcmp(buf, "serial") != 0) 186 return (ENXIO); 187 188 reg = -1; 189 OF_getencprop(node, "reg", ®, sizeof(reg)); 190 if (reg == -1) 191 return (ENXIO); 192 sc->vtermid = reg; 193 sc->node = node; 194 195 if (OF_getprop(node, "compatible", buf, sizeof(buf)) <= 0) 196 return (ENXIO); 197 if (strcmp(buf, "ibm,opal-console-raw") == 0) { 198 sc->protocol = OPAL_RAW; 199 return (0); 200 } else if (strcmp(buf, "ibm,opal-console-hvsi") == 0) { 201 sc->protocol = OPAL_HVSI; 202 return (0); 203 } 204 205 return (ENXIO); 206 } 207 208 static int 209 uart_opal_probe(device_t dev) 210 { 211 struct uart_opal_softc sc; 212 int err; 213 214 sc.node = ofw_bus_get_node(dev); 215 err = uart_opal_probe_node(&sc); 216 if (err != 0) 217 return (err); 218 219 device_set_desc(dev, "OPAL Serial Port"); 220 221 return (err); 222 } 223 224 static void 225 uart_opal_cnprobe(struct consdev *cp) 226 { 227 char buf[64]; 228 phandle_t input, chosen; 229 static struct uart_opal_softc sc; 230 231 if (opal_check() != 0) 232 goto fail; 233 234 if ((chosen = OF_finddevice("/chosen")) == -1) 235 goto fail; 236 237 /* Check if OF has an active stdin/stdout */ 238 if (OF_getprop(chosen, "linux,stdout-path", buf, sizeof(buf)) <= 0) 239 goto fail; 240 241 input = OF_finddevice(buf); 242 if (input == -1) 243 goto fail; 244 245 sc.node = input; 246 if (uart_opal_probe_node(&sc) != 0) 247 goto fail; 248 mtx_init(&sc.sc_mtx, "uart_opal", NULL, MTX_SPIN | MTX_QUIET | 249 MTX_NOWITNESS); 250 251 cp->cn_pri = CN_NORMAL; 252 console_sc = ≻ 253 cp->cn_arg = console_sc; 254 stdout_cp = cp; 255 return; 256 257 fail: 258 cp->cn_pri = CN_DEAD; 259 return; 260 } 261 262 static int 263 uart_opal_attach(device_t dev) 264 { 265 struct uart_opal_softc *sc; 266 int unit; 267 268 sc = device_get_softc(dev); 269 sc->node = ofw_bus_get_node(dev); 270 uart_opal_probe_node(sc); 271 272 unit = device_get_unit(dev); 273 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL, 274 MTX_SPIN | MTX_QUIET | MTX_NOWITNESS); 275 276 if (console_sc != NULL && console_sc->vtermid == sc->vtermid) { 277 device_printf(dev, "console\n"); 278 device_set_softc(dev, console_sc); 279 sc = console_sc; 280 sprintf(uart_opal_consdev.cn_name, "ttyu%r", unit); 281 } 282 sc->tp = tty_alloc(&uart_opal_tty_class, sc); 283 284 if (console_sc == sc) 285 tty_init_console(sc->tp, 0); 286 287 sc->dev = dev; 288 sc->irqrid = 0; 289 sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid, 290 RF_ACTIVE | RF_SHAREABLE); 291 if (sc->irqres != NULL) { 292 bus_setup_intr(dev, sc->irqres, INTR_TYPE_TTY | INTR_MPSAFE, 293 NULL, uart_opal_intr, sc, &sc->sc_icookie); 294 } else { 295 callout_init(&sc->callout, CALLOUT_MPSAFE); 296 sc->polltime = hz / 20; 297 if (sc->polltime < 1) 298 sc->polltime = 1; 299 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc); 300 } 301 302 tty_makedev(sc->tp, NULL, "u%r", unit); 303 304 return (0); 305 } 306 307 static void 308 uart_opal_cninit(struct consdev *cp) 309 { 310 311 strcpy(cp->cn_name, "opalcons"); 312 } 313 314 static void 315 uart_opal_cnterm(struct consdev *cp) 316 { 317 } 318 319 static int 320 uart_opal_get(struct uart_opal_softc *sc, void *buffer, size_t bufsize) 321 { 322 int err; 323 int hdr = 0; 324 325 if (sc->protocol == OPAL_RAW) { 326 uint64_t len = bufsize; 327 uint64_t olen = (uint64_t)&len; 328 uint64_t obuf = (uint64_t)buffer; 329 330 if (pmap_bootstrapped) { 331 olen = vtophys(&len); 332 obuf = vtophys(buffer); 333 } 334 335 err = opal_call(OPAL_CONSOLE_READ, sc->vtermid, olen, obuf); 336 if (err != OPAL_SUCCESS) 337 return (-1); 338 339 bufsize = len; 340 } else { 341 uart_lock(&sc->sc_mtx); 342 if (sc->inbuflen == 0) { 343 err = opal_call(OPAL_CONSOLE_READ, sc->vtermid, 344 &sc->inbuflen, sc->opal_inbuf); 345 if (err != OPAL_SUCCESS) { 346 uart_unlock(&sc->sc_mtx); 347 return (-1); 348 } 349 hdr = 1; 350 } 351 352 if (sc->inbuflen == 0) { 353 uart_unlock(&sc->sc_mtx); 354 return (0); 355 } 356 357 if (bufsize > sc->inbuflen) 358 bufsize = sc->inbuflen; 359 360 if (hdr == 1) { 361 sc->inbuflen = sc->inbuflen - 4; 362 /* The HVSI protocol has a 4 byte header, skip it */ 363 memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[4], 364 sc->inbuflen); 365 } 366 367 memcpy(buffer, sc->opal_inbuf, bufsize); 368 sc->inbuflen -= bufsize; 369 if (sc->inbuflen > 0) 370 memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[bufsize], 371 sc->inbuflen); 372 373 uart_unlock(&sc->sc_mtx); 374 } 375 376 return (bufsize); 377 } 378 379 static int 380 uart_opal_put(struct uart_opal_softc *sc, void *buffer, size_t bufsize) 381 { 382 uint16_t seqno; 383 uint64_t len; 384 char cbuf[16]; 385 int err; 386 uint64_t olen = (uint64_t)&len; 387 uint64_t obuf = (uint64_t)cbuf; 388 389 if (sc->protocol == OPAL_RAW) { 390 obuf = (uint64_t)buffer; 391 len = bufsize; 392 393 uart_opal_real_map_outbuffer(&obuf, &olen); 394 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf); 395 uart_opal_real_unmap_outbuffer(&len); 396 } else { 397 uart_lock(&sc->sc_mtx); 398 if (bufsize > 12) 399 bufsize = 12; 400 seqno = sc->outseqno++; 401 cbuf[0] = VS_DATA_PACKET_HEADER; 402 cbuf[1] = 4 + bufsize; /* total length */ 403 cbuf[2] = (seqno >> 8) & 0xff; 404 cbuf[3] = seqno & 0xff; 405 memcpy(&cbuf[4], buffer, bufsize); 406 len = 4 + bufsize; 407 408 uart_opal_real_map_outbuffer(&obuf, &olen); 409 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf); 410 uart_opal_real_unmap_outbuffer(&len); 411 412 uart_unlock(&sc->sc_mtx); 413 414 len -= 4; 415 } 416 417 #if 0 418 if (err != OPAL_SUCCESS) 419 len = 0; 420 #endif 421 422 return (len); 423 } 424 425 static int 426 uart_opal_cngetc(struct consdev *cp) 427 { 428 return (uart_opal_getc(cp->cn_arg)); 429 } 430 431 static int 432 uart_opal_getc(struct uart_opal_softc *sc) 433 { 434 unsigned char c; 435 int retval; 436 437 retval = uart_opal_get(sc, &c, 1); 438 if (retval != 1) 439 return (-1); 440 #if defined(KDB) 441 kdb_alt_break(c, &sc->alt_break_state); 442 #endif 443 444 return (c); 445 } 446 447 static void 448 uart_opal_cnputc(struct consdev *cp, int c) 449 { 450 unsigned char ch = c; 451 int a; 452 453 if (1) { 454 /* Clear FIFO if needed. Must be repeated few times. */ 455 for (a = 0; a < 20; a++) { 456 opal_call(OPAL_POLL_EVENTS, NULL); 457 } 458 } 459 uart_opal_put(cp->cn_arg, &ch, 1); 460 } 461 462 static void 463 uart_opal_cngrab(struct consdev *cp) 464 { 465 } 466 467 static void 468 uart_opal_cnungrab(struct consdev *cp) 469 { 470 } 471 472 static void 473 uart_opal_ttyoutwakeup(struct tty *tp) 474 { 475 struct uart_opal_softc *sc; 476 char buffer[8]; 477 int len; 478 479 sc = tty_softc(tp); 480 481 while ((len = ttydisc_getc(tp, buffer, sizeof(buffer))) != 0) 482 uart_opal_put(sc, buffer, len); 483 } 484 485 static void 486 uart_opal_intr(void *v) 487 { 488 struct uart_opal_softc *sc = v; 489 struct tty *tp = sc->tp; 490 int c; 491 492 tty_lock(tp); 493 while ((c = uart_opal_getc(sc)) > 0) 494 ttydisc_rint(tp, c, 0); 495 ttydisc_rint_done(tp); 496 tty_unlock(tp); 497 498 opal_call(OPAL_POLL_EVENTS, NULL); 499 500 if (sc->irqres == NULL) 501 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc); 502 } 503 504 static int 505 opalcons_probe(device_t dev) 506 { 507 const char *name; 508 509 name = ofw_bus_get_name(dev); 510 if (name == NULL || strcmp(name, "consoles") != 0) 511 return (ENXIO); 512 513 device_set_desc(dev, "OPAL Consoles"); 514 return (BUS_PROBE_SPECIFIC); 515 } 516 517 static int 518 opalcons_attach(device_t dev) 519 { 520 phandle_t child; 521 device_t cdev; 522 struct ofw_bus_devinfo *dinfo; 523 524 for (child = OF_child(ofw_bus_get_node(dev)); child != 0; 525 child = OF_peer(child)) { 526 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO); 527 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) { 528 free(dinfo, M_DEVBUF); 529 continue; 530 } 531 cdev = device_add_child(dev, NULL, -1); 532 if (cdev == NULL) { 533 device_printf(dev, "<%s>: device_add_child failed\n", 534 dinfo->obd_name); 535 ofw_bus_gen_destroy_devinfo(dinfo); 536 free(dinfo, M_DEVBUF); 537 continue; 538 } 539 device_set_ivars(cdev, dinfo); 540 } 541 542 return (bus_generic_attach(dev)); 543 } 544 545 static const struct ofw_bus_devinfo * 546 opalcons_get_devinfo(device_t dev, device_t child) 547 { 548 return (device_get_ivars(child)); 549 } 550 551 static device_method_t opalcons_methods[] = { 552 /* Device interface */ 553 DEVMETHOD(device_probe, opalcons_probe), 554 DEVMETHOD(device_attach, opalcons_attach), 555 556 /* ofw_bus interface */ 557 DEVMETHOD(ofw_bus_get_devinfo, opalcons_get_devinfo), 558 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat), 559 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model), 560 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name), 561 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node), 562 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type), 563 564 DEVMETHOD_END 565 }; 566 567 static driver_t opalcons_driver = { 568 "opalcons", 569 opalcons_methods, 570 0 571 }; 572 573 static devclass_t opalcons_devclass; 574 575 DRIVER_MODULE(opalcons, opal, opalcons_driver, opalcons_devclass, 0, 0); 576 577