1a16111e6SLeandro Lupori /*-
2*4d846d26SWarner Losh * SPDX-License-Identifier: BSD-2-Clause
3a16111e6SLeandro Lupori *
4a16111e6SLeandro Lupori * Copyright (C) 2019 Leandro Lupori
5a16111e6SLeandro Lupori *
6a16111e6SLeandro Lupori * Redistribution and use in source and binary forms, with or without
7a16111e6SLeandro Lupori * modification, are permitted provided that the following conditions
8a16111e6SLeandro Lupori * are met:
9a16111e6SLeandro Lupori * 1. Redistributions of source code must retain the above copyright
10a16111e6SLeandro Lupori * notice, this list of conditions and the following disclaimer.
11a16111e6SLeandro Lupori * 2. Redistributions in binary form must reproduce the above copyright
12a16111e6SLeandro Lupori * notice, this list of conditions and the following disclaimer in the
13a16111e6SLeandro Lupori * documentation and/or other materials provided with the distribution.
14a16111e6SLeandro Lupori *
15a16111e6SLeandro Lupori * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
16a16111e6SLeandro Lupori * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
17a16111e6SLeandro Lupori * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
18a16111e6SLeandro Lupori * IN NO EVENT SHALL TOOLS GMBH BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
19a16111e6SLeandro Lupori * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
20a16111e6SLeandro Lupori * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
21a16111e6SLeandro Lupori * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
22a16111e6SLeandro Lupori * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23a16111e6SLeandro Lupori * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
24a16111e6SLeandro Lupori * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25a16111e6SLeandro Lupori */
26a16111e6SLeandro Lupori
27a16111e6SLeandro Lupori #include <sys/cdefs.h>
28a16111e6SLeandro Lupori #include <sys/linker_set.h>
29a16111e6SLeandro Lupori #include <sys/param.h>
30a16111e6SLeandro Lupori #include <sys/types.h>
31a16111e6SLeandro Lupori
32a16111e6SLeandro Lupori struct thread;
33a16111e6SLeandro Lupori
34a16111e6SLeandro Lupori #include <vm/vm.h>
35a16111e6SLeandro Lupori #include <vm/pmap.h>
36a16111e6SLeandro Lupori
37a16111e6SLeandro Lupori #include <dev/ofw/openfirm.h>
38a16111e6SLeandro Lupori #include <gdb/gdb.h>
39a16111e6SLeandro Lupori
40a16111e6SLeandro Lupori #include "opal.h"
41a16111e6SLeandro Lupori
42a16111e6SLeandro Lupori static gdb_probe_f uart_opal_dbg_probe;
43a16111e6SLeandro Lupori static gdb_init_f uart_opal_dbg_init;
44a16111e6SLeandro Lupori static gdb_term_f uart_opal_dbg_term;
45a16111e6SLeandro Lupori static gdb_getc_f uart_opal_dbg_getc;
46a16111e6SLeandro Lupori static gdb_putc_f uart_opal_dbg_putc;
47a16111e6SLeandro Lupori
48a16111e6SLeandro Lupori GDB_DBGPORT(uart_opal, uart_opal_dbg_probe,
49a16111e6SLeandro Lupori uart_opal_dbg_init, uart_opal_dbg_term,
50a16111e6SLeandro Lupori uart_opal_dbg_getc, uart_opal_dbg_putc);
51a16111e6SLeandro Lupori
52a16111e6SLeandro Lupori static int64_t termnum;
53a16111e6SLeandro Lupori
54a16111e6SLeandro Lupori static int
uart_opal_dbg_probe(void)55a16111e6SLeandro Lupori uart_opal_dbg_probe(void)
56a16111e6SLeandro Lupori {
57a16111e6SLeandro Lupori char buf[64];
58a16111e6SLeandro Lupori cell_t reg;
59a16111e6SLeandro Lupori phandle_t dev;
60a16111e6SLeandro Lupori
61a16111e6SLeandro Lupori if (!getenv_string("hw.uart.dbgport", buf, sizeof(buf)))
62a16111e6SLeandro Lupori return (-1);
63a16111e6SLeandro Lupori if ((dev = OF_finddevice(buf)) == -1)
64a16111e6SLeandro Lupori return (-1);
65a16111e6SLeandro Lupori
66a16111e6SLeandro Lupori if (OF_getprop(dev, "device_type", buf, sizeof(buf)) == -1)
67a16111e6SLeandro Lupori return (-1);
68a16111e6SLeandro Lupori if (strcmp(buf, "serial") != 0)
69a16111e6SLeandro Lupori return (-1);
70a16111e6SLeandro Lupori
71a16111e6SLeandro Lupori if (OF_getprop(dev, "compatible", buf, sizeof(buf)) == -1)
72a16111e6SLeandro Lupori return (-1);
73a16111e6SLeandro Lupori if (strcmp(buf, "ibm,opal-console-raw") != 0)
74a16111e6SLeandro Lupori return (-1);
75a16111e6SLeandro Lupori
76a16111e6SLeandro Lupori reg = ~0U;
77a16111e6SLeandro Lupori OF_getencprop(dev, "reg", ®, sizeof(reg));
78a16111e6SLeandro Lupori if (reg == ~0U)
79a16111e6SLeandro Lupori return (-1);
80a16111e6SLeandro Lupori termnum = reg;
81a16111e6SLeandro Lupori
82a16111e6SLeandro Lupori return (0);
83a16111e6SLeandro Lupori }
84a16111e6SLeandro Lupori
85a16111e6SLeandro Lupori static void
uart_opal_dbg_init(void)86a16111e6SLeandro Lupori uart_opal_dbg_init(void)
87a16111e6SLeandro Lupori {
88a16111e6SLeandro Lupori }
89a16111e6SLeandro Lupori
90a16111e6SLeandro Lupori static void
uart_opal_dbg_term(void)91a16111e6SLeandro Lupori uart_opal_dbg_term(void)
92a16111e6SLeandro Lupori {
93a16111e6SLeandro Lupori }
94a16111e6SLeandro Lupori
95a16111e6SLeandro Lupori static int
uart_opal_dbg_getc(void)96a16111e6SLeandro Lupori uart_opal_dbg_getc(void)
97a16111e6SLeandro Lupori {
98a16111e6SLeandro Lupori char c;
99a16111e6SLeandro Lupori int err;
100a16111e6SLeandro Lupori int64_t len;
101a16111e6SLeandro Lupori uint64_t lenp, bufp;
102a16111e6SLeandro Lupori
103a16111e6SLeandro Lupori len = 1;
104a16111e6SLeandro Lupori if (pmap_bootstrapped) {
105a16111e6SLeandro Lupori lenp = vtophys(&len);
106a16111e6SLeandro Lupori bufp = vtophys(&c);
107a16111e6SLeandro Lupori } else {
108a16111e6SLeandro Lupori lenp = (uint64_t)&len;
109a16111e6SLeandro Lupori bufp = (uint64_t)&c;
110a16111e6SLeandro Lupori }
111a16111e6SLeandro Lupori
112a16111e6SLeandro Lupori err = opal_call(OPAL_CONSOLE_READ, termnum, lenp, bufp);
113a16111e6SLeandro Lupori if (err != OPAL_SUCCESS || len != 1)
114a16111e6SLeandro Lupori return (-1);
115a16111e6SLeandro Lupori
116a16111e6SLeandro Lupori opal_call(OPAL_POLL_EVENTS, NULL);
117a16111e6SLeandro Lupori
118a16111e6SLeandro Lupori return (c);
119a16111e6SLeandro Lupori }
120a16111e6SLeandro Lupori
121a16111e6SLeandro Lupori static void
uart_opal_dbg_putc(int c)122a16111e6SLeandro Lupori uart_opal_dbg_putc(int c)
123a16111e6SLeandro Lupori {
124a16111e6SLeandro Lupori char ch;
125a16111e6SLeandro Lupori int err;
126a16111e6SLeandro Lupori int64_t len;
127a16111e6SLeandro Lupori uint64_t lenp, bufp;
128a16111e6SLeandro Lupori
129a16111e6SLeandro Lupori ch = (unsigned char)c;
130a16111e6SLeandro Lupori len = 1;
131a16111e6SLeandro Lupori if (pmap_bootstrapped) {
132a16111e6SLeandro Lupori lenp = vtophys(&len);
133a16111e6SLeandro Lupori bufp = vtophys(&ch);
134a16111e6SLeandro Lupori } else {
135a16111e6SLeandro Lupori lenp = (uint64_t)&len;
136a16111e6SLeandro Lupori bufp = (uint64_t)&ch;
137a16111e6SLeandro Lupori }
138a16111e6SLeandro Lupori
139a16111e6SLeandro Lupori for (;;) {
140a16111e6SLeandro Lupori err = opal_call(OPAL_CONSOLE_WRITE, termnum, lenp, bufp);
141a16111e6SLeandro Lupori /* Clear FIFO if needed. */
142a16111e6SLeandro Lupori if (err == OPAL_BUSY_EVENT)
143a16111e6SLeandro Lupori opal_call(OPAL_POLL_EVENTS, NULL);
144a16111e6SLeandro Lupori else
145a16111e6SLeandro Lupori /* break on success or unrecoverable errors */
146a16111e6SLeandro Lupori break;
147a16111e6SLeandro Lupori }
148a16111e6SLeandro Lupori DELAY(100);
149a16111e6SLeandro Lupori }
150