2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2011 by Nathan Whitehorn. All rights reserved.
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
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.
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 TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 #include <sys/cdefs.h>
28 __FBSDID("$FreeBSD$");
30 #include <sys/param.h>
32 #include <sys/kernel.h>
34 #include <sys/systm.h>
35 #include <sys/module.h>
36 #include <sys/types.h>
40 #include <machine/bus.h>
42 #include <dev/ofw/openfirm.h>
43 #include <dev/ofw/ofw_bus.h>
44 #include <dev/ofw/ofw_bus_subr.h>
45 #include <dev/uart/uart.h>
46 #include <dev/uart/uart_cpu.h>
47 #include <dev/uart/uart_bus.h>
49 #include "phyp-hvcall.h"
52 struct uart_phyp_softc {
58 struct resource *irqres;
60 struct callout callout;
75 static struct uart_phyp_softc *console_sc = NULL;
77 static int alt_break_state;
84 #define VS_DATA_PACKET_HEADER 0xff
85 #define VS_CONTROL_PACKET_HEADER 0xfe
86 #define VSV_SET_MODEM_CTL 0x01
87 #define VSV_MODEM_CTL_UPDATE 0x02
88 #define VSV_RENEGOTIATE_CONNECTION 0x03
89 #define VS_QUERY_PACKET_HEADER 0xfd
90 #define VSV_SEND_VERSION_NUMBER 0x01
91 #define VSV_SEND_MODEM_CTL_STATUS 0x02
92 #define VS_QUERY_RESPONSE_PACKET_HEADER 0xfc
94 static int uart_phyp_probe(device_t dev);
95 static int uart_phyp_attach(device_t dev);
96 static void uart_phyp_intr(void *v);
98 static device_method_t uart_phyp_methods[] = {
99 /* Device interface */
100 DEVMETHOD(device_probe, uart_phyp_probe),
101 DEVMETHOD(device_attach, uart_phyp_attach),
106 static driver_t uart_phyp_driver = {
109 sizeof(struct uart_phyp_softc),
112 DRIVER_MODULE(uart_phyp, vdevice, uart_phyp_driver, uart_devclass, 0, 0);
114 static cn_probe_t uart_phyp_cnprobe;
115 static cn_init_t uart_phyp_cninit;
116 static cn_term_t uart_phyp_cnterm;
117 static cn_getc_t uart_phyp_cngetc;
118 static cn_putc_t uart_phyp_cnputc;
119 static cn_grab_t uart_phyp_cngrab;
120 static cn_ungrab_t uart_phyp_cnungrab;
122 CONSOLE_DRIVER(uart_phyp);
124 static void uart_phyp_ttyoutwakeup(struct tty *tp);
126 static struct ttydevsw uart_phyp_tty_class = {
127 .tsw_flags = TF_INITLOCK|TF_CALLOUT,
128 .tsw_outwakeup = uart_phyp_ttyoutwakeup,
132 uart_phyp_probe_node(struct uart_phyp_softc *sc)
134 phandle_t node = sc->node;
141 if (OF_getprop(node, "name", buf, sizeof(buf)) <= 0)
143 if (strcmp(buf, "vty") != 0)
146 if (OF_getprop(node, "device_type", buf, sizeof(buf)) <= 0)
148 if (strcmp(buf, "serial") != 0)
152 OF_getencprop(node, "reg", ®, sizeof(reg));
158 if (OF_getprop(node, "compatible", buf, sizeof(buf)) <= 0)
160 if (strcmp(buf, "hvterm1") == 0) {
161 sc->protocol = HVTERM1;
163 } else if (strcmp(buf, "hvterm-protocol") == 0) {
164 sc->protocol = HVTERMPROT;
172 uart_phyp_probe(device_t dev)
175 struct uart_phyp_softc sc;
178 name = ofw_bus_get_name(dev);
179 if (name == NULL || strcmp(name, "vty") != 0)
182 sc.node = ofw_bus_get_node(dev);
183 err = uart_phyp_probe_node(&sc);
187 device_set_desc(dev, "POWER Hypervisor Virtual Serial Port");
193 uart_phyp_cnprobe(struct consdev *cp)
197 phandle_t input, chosen;
198 static struct uart_phyp_softc sc;
200 if ((chosen = OF_finddevice("/chosen")) == -1)
203 /* Check if OF has an active stdin/stdout */
205 if (OF_getencprop(chosen, "stdout", &stdout,
206 sizeof(stdout)) == sizeof(stdout) && stdout != 0)
207 input = OF_instance_to_package(stdout);
211 if (OF_getprop(input, "device_type", buf, sizeof(buf)) == -1)
213 if (strcmp(buf, "serial") != 0)
217 if (uart_phyp_probe_node(&sc) != 0)
219 mtx_init(&sc.sc_mtx, "uart_phyp", NULL, MTX_SPIN | MTX_QUIET |
222 cp->cn_pri = CN_NORMAL;
227 cp->cn_pri = CN_DEAD;
232 uart_phyp_attach(device_t dev)
234 struct uart_phyp_softc *sc;
237 sc = device_get_softc(dev);
239 sc->node = ofw_bus_get_node(dev);
240 uart_phyp_probe_node(sc);
242 unit = device_get_unit(dev);
243 sc->tp = tty_alloc(&uart_phyp_tty_class, sc);
244 mtx_init(&sc->sc_mtx, device_get_nameunit(dev), NULL,
245 MTX_SPIN | MTX_QUIET | MTX_NOWITNESS);
247 if (console_sc != NULL && console_sc->vtermid == sc->vtermid) {
248 sc->outseqno = console_sc->outseqno;
250 sprintf(uart_phyp_consdev.cn_name, "ttyu%r", unit);
251 tty_init_console(sc->tp, 0);
255 sc->irqres = bus_alloc_resource_any(dev, SYS_RES_IRQ, &sc->irqrid,
256 RF_ACTIVE | RF_SHAREABLE);
257 if (sc->irqres != NULL) {
258 bus_setup_intr(dev, sc->irqres, INTR_TYPE_TTY | INTR_MPSAFE,
259 NULL, uart_phyp_intr, sc, &sc->sc_icookie);
261 callout_init(&sc->callout, 1);
262 sc->polltime = hz / 20;
263 if (sc->polltime < 1)
265 callout_reset(&sc->callout, sc->polltime, uart_phyp_intr, sc);
268 tty_makedev(sc->tp, NULL, "u%r", unit);
274 uart_phyp_cninit(struct consdev *cp)
277 strcpy(cp->cn_name, "phypcons");
281 uart_phyp_cnterm(struct consdev *cp)
286 uart_phyp_get(struct uart_phyp_softc *sc, void *buffer, size_t bufsize)
291 uart_lock(&sc->sc_mtx);
292 if (sc->inbuflen == 0) {
293 err = phyp_pft_hcall(H_GET_TERM_CHAR, sc->vtermid,
294 0, 0, 0, &sc->inbuflen, &sc->phyp_inbuf.u64[0],
295 &sc->phyp_inbuf.u64[1]);
296 if (err != H_SUCCESS) {
297 uart_unlock(&sc->sc_mtx);
303 if (sc->inbuflen == 0) {
304 uart_unlock(&sc->sc_mtx);
308 if (bufsize > sc->inbuflen)
309 bufsize = sc->inbuflen;
311 if ((sc->protocol == HVTERMPROT) && (hdr == 1)) {
312 sc->inbuflen = sc->inbuflen - 4;
313 /* The VTERM protocol has a 4 byte header, skip it here. */
314 memmove(&sc->phyp_inbuf.str[0], &sc->phyp_inbuf.str[4],
318 memcpy(buffer, sc->phyp_inbuf.str, bufsize);
319 sc->inbuflen -= bufsize;
320 if (sc->inbuflen > 0)
321 memmove(&sc->phyp_inbuf.str[0], &sc->phyp_inbuf.str[bufsize],
324 uart_unlock(&sc->sc_mtx);
329 uart_phyp_put(struct uart_phyp_softc *sc, void *buffer, size_t bufsize)
340 uart_lock(&sc->sc_mtx);
341 switch (sc->protocol) {
345 memcpy(&cbuf, buffer, bufsize);
351 seqno = sc->outseqno++;
352 cbuf.bytes[0] = VS_DATA_PACKET_HEADER;
353 cbuf.bytes[1] = 4 + bufsize; /* total length, max 16 bytes */
354 cbuf.bytes[2] = (seqno >> 8) & 0xff;
355 cbuf.bytes[3] = seqno & 0xff;
356 memcpy(&cbuf.bytes[4], buffer, bufsize);
362 err = phyp_hcall(H_PUT_TERM_CHAR, sc->vtermid, len, cbuf.u64[0],
365 } while (err == H_BUSY);
367 uart_unlock(&sc->sc_mtx);
373 uart_phyp_cngetc(struct consdev *cp)
378 retval = uart_phyp_get(console_sc, &c, 1);
382 kdb_alt_break(c, &alt_break_state);
389 uart_phyp_cnputc(struct consdev *cp, int c)
391 unsigned char ch = c;
392 uart_phyp_put(console_sc, &ch, 1);
396 uart_phyp_cngrab(struct consdev *cp)
401 uart_phyp_cnungrab(struct consdev *cp)
406 uart_phyp_ttyoutwakeup(struct tty *tp)
408 struct uart_phyp_softc *sc;
414 while ((len = ttydisc_getc(tp, buffer, sizeof(buffer))) != 0)
415 uart_phyp_put(sc, buffer, len);
419 uart_phyp_intr(void *v)
421 struct uart_phyp_softc *sc = v;
422 struct tty *tp = sc->tp;
427 while ((len = uart_phyp_get(sc, &c, 1)) > 0)
428 ttydisc_rint(tp, c, 0);
429 ttydisc_rint_done(tp);
432 if (sc->irqres == NULL)
433 callout_reset(&sc->callout, sc->polltime, uart_phyp_intr, sc);