2 * SPDX-License-Identifier: BSD-2-Clause-FreeBSD
4 * Copyright (C) 2019 Leandro Lupori
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/linker_set.h>
31 #include <sys/param.h>
32 #include <sys/types.h>
39 #include <dev/ofw/openfirm.h>
44 static gdb_probe_f uart_opal_dbg_probe;
45 static gdb_init_f uart_opal_dbg_init;
46 static gdb_term_f uart_opal_dbg_term;
47 static gdb_getc_f uart_opal_dbg_getc;
48 static gdb_putc_f uart_opal_dbg_putc;
50 GDB_DBGPORT(uart_opal, uart_opal_dbg_probe,
51 uart_opal_dbg_init, uart_opal_dbg_term,
52 uart_opal_dbg_getc, uart_opal_dbg_putc);
54 static int64_t termnum;
57 uart_opal_dbg_probe(void)
63 if (!getenv_string("hw.uart.dbgport", buf, sizeof(buf)))
65 if ((dev = OF_finddevice(buf)) == -1)
68 if (OF_getprop(dev, "device_type", buf, sizeof(buf)) == -1)
70 if (strcmp(buf, "serial") != 0)
73 if (OF_getprop(dev, "compatible", buf, sizeof(buf)) == -1)
75 if (strcmp(buf, "ibm,opal-console-raw") != 0)
79 OF_getencprop(dev, "reg", ®, sizeof(reg));
88 uart_opal_dbg_init(void)
93 uart_opal_dbg_term(void)
98 uart_opal_dbg_getc(void)
106 if (pmap_bootstrapped) {
107 lenp = vtophys(&len);
110 lenp = (uint64_t)&len;
114 err = opal_call(OPAL_CONSOLE_READ, termnum, lenp, bufp);
115 if (err != OPAL_SUCCESS || len != 1)
118 opal_call(OPAL_POLL_EVENTS, NULL);
124 uart_opal_dbg_putc(int c)
131 ch = (unsigned char)c;
133 if (pmap_bootstrapped) {
134 lenp = vtophys(&len);
137 lenp = (uint64_t)&len;
138 bufp = (uint64_t)&ch;
142 err = opal_call(OPAL_CONSOLE_WRITE, termnum, lenp, bufp);
143 /* Clear FIFO if needed. */
144 if (err == OPAL_BUSY_EVENT)
145 opal_call(OPAL_POLL_EVENTS, NULL);
147 /* break on success or unrecoverable errors */