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