2 * Copyright (C) 2011,2015 by Nathan Whitehorn. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions
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.
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.
25 #include <sys/cdefs.h>
26 __FBSDID("$FreeBSD$");
28 #include <sys/param.h>
30 #include <sys/kernel.h>
32 #include <sys/systm.h>
33 #include <sys/module.h>
34 #include <sys/types.h>
43 #include <machine/bus.h>
45 #include <dev/ofw/openfirm.h>
46 #include <dev/ofw/ofw_bus.h>
47 #include <dev/ofw/ofw_bus_subr.h>
48 #include <dev/uart/uart.h>
49 #include <dev/uart/uart_cpu.h>
50 #include <dev/uart/uart_bus.h>
55 struct uart_opal_softc {
61 struct resource *irqres;
63 struct callout callout;
75 static struct uart_opal_softc *console_sc = NULL;
78 static int alt_break_state;
85 #define VS_DATA_PACKET_HEADER 0xff
86 #define VS_CONTROL_PACKET_HEADER 0xfe
87 #define VSV_SET_MODEM_CTL 0x01
88 #define VSV_MODEM_CTL_UPDATE 0x02
89 #define VSV_RENEGOTIATE_CONNECTION 0x03
90 #define VS_QUERY_PACKET_HEADER 0xfd
91 #define VSV_SEND_VERSION_NUMBER 0x01
92 #define VSV_SEND_MODEM_CTL_STATUS 0x02
93 #define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
95 static int uart_opal_probe(device_t dev);
96 static int uart_opal_attach(device_t dev);
97 static void uart_opal_intr(void *v);
99 static device_method_t uart_opal_methods[] = {
100 /* Device interface */
101 DEVMETHOD(device_probe, uart_opal_probe),
102 DEVMETHOD(device_attach, uart_opal_attach),
107 static driver_t uart_opal_driver = {
110 sizeof(struct uart_opal_softc),
113 DRIVER_MODULE(uart_opal, opalcons, uart_opal_driver, uart_devclass, 0, 0);
115 static cn_probe_t uart_opal_cnprobe;
116 static cn_init_t uart_opal_cninit;
117 static cn_term_t uart_opal_cnterm;
118 static cn_getc_t uart_opal_cngetc;
119 static cn_putc_t uart_opal_cnputc;
120 static cn_grab_t uart_opal_cngrab;
121 static cn_ungrab_t uart_opal_cnungrab;
123 CONSOLE_DRIVER(uart_opal);
125 static void uart_opal_ttyoutwakeup(struct tty *tp);
127 static struct ttydevsw uart_opal_tty_class = {
128 .tsw_flags = TF_INITLOCK|TF_CALLOUT,
129 .tsw_outwakeup = uart_opal_ttyoutwakeup,
139 uart_opal_real_map_outbuffer(uint64_t *bufferp, uint64_t *lenp)
142 if (!mtx_initialized(&escapehatch.mtx))
143 mtx_init(&escapehatch.mtx, "uart_opal", NULL, MTX_SPIN | MTX_QUIET |
146 if (!pmap_bootstrapped)
149 if (TD_IS_IDLETHREAD(curthread)) {
150 escapehatch.size = *(uint64_t *)(*lenp) =
151 min(sizeof(escapehatch.tmpbuf), *(uint64_t *)(*lenp));
152 mtx_lock_spin(&escapehatch.mtx);
153 memcpy(escapehatch.tmpbuf, (void *)(*bufferp), *(uint64_t *)(*lenp));
154 *bufferp = (uint64_t)escapehatch.tmpbuf;
155 *lenp = (uint64_t)&escapehatch.size;
158 *bufferp = vtophys(*bufferp);
159 *lenp = vtophys(*lenp);
163 uart_opal_real_unmap_outbuffer(uint64_t lenp, uint64_t *origlen)
166 if (!pmap_bootstrapped || !TD_IS_IDLETHREAD(curthread))
169 mtx_assert(&escapehatch.mtx, MA_OWNED);
170 *origlen = escapehatch.size;
171 mtx_unlock_spin(&escapehatch.mtx);
175 uart_opal_probe_node(struct uart_opal_softc *sc)
177 phandle_t node = sc->node;
184 if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
186 if (strcmp(buf, "serial") != 0)
190 OF_getprop(node, "reg", ®, sizeof(reg));
196 if (OF_getprop(node, "compatible", buf, sizeof(buf)) <= 0)
198 if (strcmp(buf, "ibm,opal-console-raw") == 0) {
199 sc->protocol = OPAL_RAW;
201 } else if (strcmp(buf, "ibm,opal-console-hvsi") == 0) {
202 sc->protocol = OPAL_HVSI;
210 uart_opal_probe(device_t dev)
212 struct uart_opal_softc sc;
215 sc.node = ofw_bus_get_node(dev);
216 err = uart_opal_probe_node(&sc);
220 device_set_desc(dev, "OPAL Serial Port");
226 uart_opal_cnprobe(struct consdev *cp)
229 phandle_t input, chosen;
230 static struct uart_opal_softc sc;
232 if (opal_check() != 0)
235 if ((chosen = OF_finddevice("/chosen")) == -1)
238 /* Check if OF has an active stdin/stdout */
239 if (OF_getprop(chosen, "linux,stdout-path", buf, sizeof(buf)) <= 0)
242 input = OF_finddevice(buf);
247 if (uart_opal_probe_node(&sc) != 0)
249 mtx_init(&sc.sc_mtx, "uart_opal", NULL, MTX_SPIN | MTX_QUIET |
252 cp->cn_pri = CN_NORMAL;
257 cp->cn_pri = CN_DEAD;
262 uart_opal_attach(device_t dev)
264 struct uart_opal_softc *sc;
267 sc = device_get_softc(dev);
269 sc->node = ofw_bus_get_node(dev);
270 uart_opal_probe_node(sc);
272 unit = device_get_unit(dev);
273 sc->tp = tty_alloc(&uart_opal_tty_class, sc);
274 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL,
275 MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
277 if (console_sc != NULL && console_sc->vtermid == sc->vtermid) {
278 device_printf(dev, "console\n");
279 sc->outseqno = console_sc->outseqno;
281 sprintf(uart_opal_consdev.cn_name, "ttyu%r", unit);
282 tty_init_console(sc->tp, 0);
286 sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
287 RF_ACTIVE | RF_SHAREABLE);
288 if (sc->irqres != NULL) {
289 bus_setup_intr(dev, sc->irqres, INTR_TYPE_TTY | INTR_MPSAFE,
290 NULL, uart_opal_intr, sc, &sc->sc_icookie);
292 callout_init(&sc->callout, CALLOUT_MPSAFE);
293 sc->polltime = hz / 20;
294 if (sc->polltime < 1)
296 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
299 tty_makedev(sc->tp, NULL, "u%r", unit);
305 uart_opal_cninit(struct consdev *cp)
308 strcpy(cp->cn_name, "opalcons");
312 uart_opal_cnterm(struct consdev *cp)
317 uart_opal_get(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
322 if (sc->protocol == OPAL_RAW) {
323 uint64_t len = bufsize;
324 uint64_t olen = (uint64_t)&len;
325 uint64_t obuf = (uint64_t)buffer;
327 if (pmap_bootstrapped) {
328 olen = vtophys(&len);
329 obuf = vtophys(buffer);
332 err = opal_call(OPAL_CONSOLE_READ, sc->vtermid, olen, obuf);
333 if (err != OPAL_SUCCESS)
338 uart_lock(&sc->sc_mtx);
339 if (sc->inbuflen == 0) {
340 err = opal_call(OPAL_CONSOLE_READ, sc->vtermid,
341 &sc->inbuflen, sc->opal_inbuf);
342 if (err != OPAL_SUCCESS) {
343 uart_unlock(&sc->sc_mtx);
349 if (sc->inbuflen == 0) {
350 uart_unlock(&sc->sc_mtx);
354 if (bufsize > sc->inbuflen)
355 bufsize = sc->inbuflen;
358 sc->inbuflen = sc->inbuflen - 4;
359 /* The HVSI protocol has a 4 byte header, skip it */
360 memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[4],
364 memcpy(buffer, sc->opal_inbuf, bufsize);
365 sc->inbuflen -= bufsize;
366 if (sc->inbuflen > 0)
367 memmove(&sc->opal_inbuf[0], &sc->opal_inbuf[bufsize],
370 uart_unlock(&sc->sc_mtx);
377 uart_opal_put(struct uart_opal_softc *sc, void *buffer, size_t bufsize)
383 uint64_t olen = (uint64_t)&len;
384 uint64_t obuf = (uint64_t)cbuf;
386 if (sc->protocol == OPAL_RAW) {
387 obuf = (uint64_t)buffer;
390 uart_opal_real_map_outbuffer(&obuf, &olen);
391 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
392 uart_opal_real_unmap_outbuffer(olen, &len);
394 uart_lock(&sc->sc_mtx);
397 seqno = sc->outseqno++;
398 cbuf[0] = VS_DATA_PACKET_HEADER;
399 cbuf[1] = 4 + bufsize; /* total length */
400 cbuf[2] = (seqno >> 8) & 0xff;
401 cbuf[3] = seqno & 0xff;
402 memcpy(&cbuf[4], buffer, bufsize);
405 uart_opal_real_map_outbuffer(&obuf, &olen);
406 err = opal_call(OPAL_CONSOLE_WRITE, sc->vtermid, olen, obuf);
407 uart_opal_real_unmap_outbuffer(olen, &len);
409 uart_unlock(&sc->sc_mtx);
415 if (err != OPAL_SUCCESS)
423 uart_opal_cngetc(struct consdev *cp)
428 retval = uart_opal_get(console_sc, &c, 1);
432 kdb_alt_break(c, &alt_break_state);
439 uart_opal_cnputc(struct consdev *cp, int c)
441 unsigned char ch = c;
442 uart_opal_put(console_sc, &ch, 1);
446 uart_opal_cngrab(struct consdev *cp)
451 uart_opal_cnungrab(struct consdev *cp)
456 uart_opal_ttyoutwakeup(struct tty *tp)
458 struct uart_opal_softc *sc;
464 while ((len = ttydisc_getc(tp, buffer, sizeof(buffer))) != 0)
465 uart_opal_put(sc, buffer, len);
469 uart_opal_intr(void *v)
471 struct uart_opal_softc *sc = v;
472 struct tty *tp = sc->tp;
477 while ((len = uart_opal_get(sc, &c, 1)) > 0)
478 ttydisc_rint(tp, c, 0);
479 ttydisc_rint_done(tp);
482 if (sc->irqres == NULL)
483 callout_reset(&sc->callout, sc->polltime, uart_opal_intr, sc);
487 opalcons_probe(device_t dev)
491 name = ofw_bus_get_name(dev);
492 if (name == NULL || strcmp(name, "consoles") != 0)
495 device_set_desc(dev, "OPAL Consoles");
496 return (BUS_PROBE_SPECIFIC);
500 opalcons_attach(device_t dev)
504 struct ofw_bus_devinfo *dinfo;
506 for (child = OF_child(ofw_bus_get_node(dev)); child != 0;
507 child = OF_peer(child)) {
508 dinfo = malloc(sizeof(*dinfo), M_DEVBUF, M_WAITOK | M_ZERO);
509 if (ofw_bus_gen_setup_devinfo(dinfo, child) != 0) {
510 free(dinfo, M_DEVBUF);
513 cdev = device_add_child(dev, NULL, -1);
515 device_printf(dev, "<%s>: device_add_child failed\n",
517 ofw_bus_gen_destroy_devinfo(dinfo);
518 free(dinfo, M_DEVBUF);
521 device_set_ivars(cdev, dinfo);
524 return (bus_generic_attach(dev));
527 static const struct ofw_bus_devinfo *
528 opalcons_get_devinfo(device_t dev, device_t child)
530 return (device_get_ivars(child));
533 static device_method_t opalcons_methods[] = {
534 /* Device interface */
535 DEVMETHOD(device_probe, opalcons_probe),
536 DEVMETHOD(device_attach, opalcons_attach),
538 /* ofw_bus interface */
539 DEVMETHOD(ofw_bus_get_devinfo, opalcons_get_devinfo),
540 DEVMETHOD(ofw_bus_get_compat, ofw_bus_gen_get_compat),
541 DEVMETHOD(ofw_bus_get_model, ofw_bus_gen_get_model),
542 DEVMETHOD(ofw_bus_get_name, ofw_bus_gen_get_name),
543 DEVMETHOD(ofw_bus_get_node, ofw_bus_gen_get_node),
544 DEVMETHOD(ofw_bus_get_type, ofw_bus_gen_get_type),
549 static driver_t opalcons_driver = {
555 static devclass_t opalcons_devclass;
557 DRIVER_MODULE(opalcons, opal, opalcons_driver, opalcons_devclass, 0, 0);