1 /*        $NetBSD: zs_kgdb.c,v 1.15 2023/10/24 19:05:07 andvar Exp $  */
2 
3 /*-
4  * Copyright (c) 1996 The NetBSD Foundation, Inc.
5  * All rights reserved.
6  *
7  * This code is derived from software contributed to The NetBSD Foundation
8  * by Gordon W. Ross.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in the
17  *    documentation and/or other materials provided with the distribution.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29  * POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 /*
33  * Hooks for kgdb when attached via the z8530 driver
34  *
35  * To use this, build a kernel with: option KGDB, and
36  * boot that kernel with "-d".  (The kernel will call
37  * zs_kgdb_init, kgdb_connect.)  When the console prints
38  * "kgdb waiting..." you run "gdb -k kernel" and do:
39  *   (gdb) set remotebaud 19200
40  *   (gdb) target remote /dev/ttyb
41  */
42 
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: zs_kgdb.c,v 1.15 2023/10/24 19:05:07 andvar Exp $");
45 
46 #include <sys/param.h>
47 #include <sys/systm.h>
48 #include <sys/proc.h>
49 #include <sys/device.h>
50 #include <sys/conf.h>
51 #include <sys/ioctl.h>
52 #include <sys/kernel.h>
53 #include <sys/syslog.h>
54 #include <sys/kgdb.h>
55 
56 #include <dev/ofw/openfirm.h>
57 #include <dev/ic/z8530reg.h>
58 #include <machine/z8530var.h>
59 
60 static void zs_setparam(struct zs_chanstate *, int, int);
61 static void zskgdb(struct zs_chanstate *);
62 
63 struct zsops zsops_kgdb;
64 
65 static u_char zs_kgdb_regs[16] = {
66           0,        /* 0: CMD (reset, etc.) */
67           0,        /* 1: ~(ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE) */
68           0,        /* IVECT */
69           ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
70           ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
71           ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
72           0,        /* 6: TXSYNC/SYNCLO */
73           0,        /* 7: RXSYNC/SYNCHI */
74           0,        /* 8: alias for data port */
75           ZSWR9_MASTER_IE,
76           0,        /*10: Misc. TX/RX control bits */
77           ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
78           ((PCLK/32)/38400)-2,          /*12: BAUDLO (default=38400) */
79           0,                            /*13: BAUDHI (default=38400) */
80           ZSWR14_BAUD_ENA,
81           ZSWR15_BREAK_IE,
82 };
83 
84 /*
85  * This replaces "zs_reset()" in the sparc driver.
86  */
87 static void
zs_setparam(struct zs_chanstate * cs,int iena,int rate)88 zs_setparam(struct zs_chanstate *cs, int iena, int rate)
89 {
90           int s, tconst;
91 
92           memcpy(cs->cs_preg, zs_kgdb_regs, 16);
93 
94           if (iena) {
95                     cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
96           }
97 
98           /* Initialize the speed, etc. */
99           tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
100           cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
101           cs->cs_preg[12] = tconst;
102           cs->cs_preg[13] = tconst >> 8;
103 
104           s = splhigh();
105           zs_loadchannelregs(cs);
106           splx(s);
107 }
108 
109 #ifndef KGDB_DEVNAME
110 #define KGDB_DEVNAME "scca"
111 #endif
112 const char *zs_kgdb_devname = KGDB_DEVNAME;
113 /*
114  * Set up for kgdb; called at boot time before configuration.
115  * KGDB interrupts will be enabled later when zs0 is configured.
116  * Called after cninit(), so printf() etc. works.
117  */
118 void
zs_kgdb_init(void)119 zs_kgdb_init(void)
120 {
121           extern const struct cdevsw zstty_cdevsw;
122           struct zs_chanstate cs;
123           volatile struct zschan *zc;
124           int escc, escc_ch, obio, zs_offset;
125           int channel = 0;
126           u_int32_t reg[5];
127           char name[16];
128 
129           if ((escc_ch = OF_finddevice(zs_kgdb_devname)) == -1)
130                     return;
131 
132           memset(name, 0, sizeof(name));
133           if (OF_getprop(escc_ch, "device_type", name, sizeof(name)) == -1)
134                     return;
135 
136           if (strcmp(name, "serial") != 0)
137                     return;
138 
139           memset(name, 0, sizeof(name));
140           if (OF_getprop(escc_ch, "name", name, sizeof(name)) == -1)
141                     return;
142 
143           if (strcmp(name, "ch-b") == 0)
144                     channel = 1;
145 
146           if (OF_getprop(escc_ch, "reg", reg, sizeof(reg)) < 4)
147                     return;
148           zs_offset = reg[0];
149 
150           escc = OF_parent(escc_ch);
151           obio = OF_parent(escc);
152 
153           if (OF_getprop(obio, "assigned-addresses", reg, sizeof(reg)) < 12)
154                     return;
155           zc = (struct zschan *)(reg[2] + zs_offset);
156 
157           kgdb_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), channel);
158 
159           printf("zs_kgdb_init: attaching tty%02d at %d baud\n",
160                     channel, kgdb_rate);
161 
162           /* Setup temporary chanstate. */
163           memset((void *)&cs, 0, sizeof(cs));
164           cs.cs_channel = channel;
165           cs.cs_brg_clk = PCLK / 16;
166           cs.cs_reg_csr  = &zc->zc_csr;
167           cs.cs_reg_data = &zc->zc_data;
168 
169           /* Now set parameters. (interrupts disabled) */
170           zs_setparam(&cs, 0, kgdb_rate);
171 
172           /* Store the getc/putc functions and arg. */
173           kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
174 }
175 
176 /*
177  * This is a "hook" called by zstty_attach to allow the tty
178  * to be "taken over" for exclusive use by kgdb.
179  * Return non-zero if this is the kgdb port.
180  *
181  * Set the speed to kgdb_rate, CS8, etc.
182  */
183 int
zs_check_kgdb(struct zs_chanstate * cs,int dev)184 zs_check_kgdb(struct zs_chanstate *cs, int dev)
185 {
186 
187           if (dev != kgdb_dev)
188                     return (0);
189 
190           /*
191            * Yes, this is port in use by kgdb.
192            */
193           cs->cs_private = NULL;
194           cs->cs_ops = &zsops_kgdb;
195 
196           /* Now set parameters. (interrupts enabled) */
197           zs_setparam(cs, 1, kgdb_rate);
198 
199           return (1);
200 }
201 
202 /*
203  * KGDB framing character received: enter kernel debugger.  This probably
204  * should time out after a few seconds to avoid hanging on spurious input.
205  */
206 static void
zskgdb(struct zs_chanstate * cs)207 zskgdb(struct zs_chanstate *cs)
208 {
209           int unit = minor(kgdb_dev);
210 
211           printf("zstty%d: kgdb interrupt\n", unit);
212           /* This will trap into the debugger. */
213           kgdb_connect(1);
214 }
215 
216 
217 /****************************************************************
218  * Interface to the lower layer (zscc)
219  ****************************************************************/
220 
221 static void zs_kgdb_rxint(struct zs_chanstate *);
222 static void zs_kgdb_stint(struct zs_chanstate *, int);
223 static void zs_kgdb_txint(struct zs_chanstate *);
224 static void zs_kgdb_softint(struct zs_chanstate *);
225 
226 int kgdb_input_lost;
227 
228 static void
zs_kgdb_rxint(struct zs_chanstate * cs)229 zs_kgdb_rxint(struct zs_chanstate *cs)
230 {
231           register u_char c, rr1;
232 
233           /*
234            * First read the status, because reading the received char
235            * destroys the status of this char.
236            */
237           rr1 = zs_read_reg(cs, 1);
238           c = zs_read_data(cs);
239 
240           if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
241                     /* Clear the receive error. */
242                     zs_write_csr(cs, ZSWR0_RESET_ERRORS);
243           }
244 
245           if (c == KGDB_START) {
246                     zskgdb(cs);
247           } else {
248                     kgdb_input_lost++;
249           }
250 }
251 
252 static void
zs_kgdb_txint(register struct zs_chanstate * cs)253 zs_kgdb_txint(register struct zs_chanstate *cs)
254 {
255           zs_write_csr(cs, ZSWR0_RESET_TXINT);
256 }
257 
258 static void
zs_kgdb_stint(register struct zs_chanstate * cs,int force)259 zs_kgdb_stint(register struct zs_chanstate *cs, int force)
260 {
261           register int rr0;
262 
263           rr0 = zs_read_csr(cs);
264           zs_write_csr(cs, ZSWR0_RESET_STATUS);
265 
266           /*
267            * Check here for console break, so that we can abort
268            * even when interrupts are locking up the machine.
269            */
270           if (rr0 & ZSRR0_BREAK) {
271                     zskgdb(cs);
272           }
273 }
274 
275 static void
zs_kgdb_softint(struct zs_chanstate * cs)276 zs_kgdb_softint(struct zs_chanstate *cs)
277 {
278           printf("zs_kgdb_softint?\n");
279 }
280 
281 struct zsops zsops_kgdb = {
282           zs_kgdb_rxint,      /* receive char available */
283           zs_kgdb_stint,      /* external/status */
284           zs_kgdb_txint,      /* xmit buffer empty */
285           zs_kgdb_softint,    /* process software interrupt */
286 };
287