zs_kgdb.c revision 1.5 1 /* $NetBSD: zs_kgdb.c,v 1.5 2004/06/16 05:33:23 briggs 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 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the NetBSD
21 * Foundation, Inc. and its contributors.
22 * 4. Neither the name of The NetBSD Foundation nor the names of its
23 * contributors may be used to endorse or promote products derived
24 * from this software without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
36 * POSSIBILITY OF SUCH DAMAGE.
37 */
38
39 /*
40 * Hooks for kgdb when attached via the z8530 driver
41 *
42 * To use this, build a kernel with: option KGDB, and
43 * boot that kernel with "-d". (The kernel will call
44 * zs_kgdb_init, kgdb_connect.) When the console prints
45 * "kgdb waiting..." you run "gdb -k kernel" and do:
46 * (gdb) set remotebaud 19200
47 * (gdb) target remote /dev/ttyb
48 */
49
50 #include <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: zs_kgdb.c,v 1.5 2004/06/16 05:33:23 briggs Exp $");
52
53 #include "opt_kgdb.h"
54 #include <sys/param.h>
55 #include <sys/systm.h>
56 #include <sys/proc.h>
57 #include <sys/device.h>
58 #include <sys/conf.h>
59 #include <sys/ioctl.h>
60 #include <sys/kernel.h>
61 #include <sys/syslog.h>
62 #include <sys/kgdb.h>
63
64 #include <dev/ic/z8530reg.h>
65 #include <machine/z8530var.h>
66 #include <mac68k/dev/zs_cons.h>
67
68 /*
69 * Macs supposely provide a 3.672 MHz clock to the SCC. Unfortunately,
70 * empirical evidence suggests that it's really a 3.6864 MHz clock.
71 * Oh, what to do, what to do?!
72 */
73 #define PCLK (9600 * 384) /* PCLK pin input clock rate */
74 #define ZSHARD_PRI 4 /* Wired on the CPU board... */
75
76 #define ZS_DELAY() delay(2)
77
78 /* The layout of this is hardware-dependent (padding, order). */
79 struct zschan {
80 volatile u_char zc_csr; /* ctrl,status, and indirect access */
81 u_char zc_xxx0;
82 u_char zc_xxx1; /* part of the other channel lives here! */
83 u_char zc_xxx2; /* Yea Apple! */
84 volatile u_char zc_data; /* data */
85 u_char zc_xxx3;
86 u_char zc_xxx4;
87 u_char zc_xxx5;
88 };
89
90 static void zs_setparam __P((struct zs_chanstate *, int, int));
91 static void zskgdb __P((struct zs_chanstate *));
92
93 struct zsops zsops_kgdb;
94
95 static u_char zs_kgdb_regs[16] = {
96 0, /* 0: CMD (reset, etc.) */
97 0, /* 1: ~(ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE) */
98 0x18 + ZSHARD_PRI, /* IVECT */
99 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
100 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
101 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
102 0, /* 6: TXSYNC/SYNCLO */
103 0, /* 7: RXSYNC/SYNCHI */
104 0, /* 8: alias for data port */
105 ZSWR9_MASTER_IE,
106 0, /*10: Misc. TX/RX control bits */
107 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
108 14, /*12: BAUDLO (default=9600) */
109 0, /*13: BAUDHI (default=9600) */
110 ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
111 ZSWR15_BREAK_IE,
112 };
113
114 /*
115 * This replaces "zs_reset()" in the sparc driver.
116 */
117 static void
118 zs_setparam(cs, iena, rate)
119 struct zs_chanstate *cs;
120 int iena;
121 int rate;
122 {
123 int s, tconst;
124
125 bcopy(zs_kgdb_regs, cs->cs_preg, 16);
126
127 if (iena) {
128 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
129 }
130
131 /* Initialize the speed, etc. */
132 tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
133 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
134 cs->cs_preg[12] = tconst;
135 cs->cs_preg[13] = tconst >> 8;
136
137 s = splhigh();
138 zs_loadchannelregs(cs);
139 splx(s);
140 }
141
142 /*
143 * Set up for kgdb; called at boot time before configuration.
144 * KGDB interrupts will be enabled later when zs0 is configured.
145 * Called after cninit(), so printf() etc. works.
146 */
147 void
148 zs_kgdb_init(void)
149 {
150 struct zs_chanstate cs;
151 volatile struct zschan *zc;
152 int channel;
153 extern const struct cdevsw zstty_cdevsw;
154
155 /* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
156 if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
157 return;
158
159 /* Note: (ttya,ttyb) on zsc1, and (ttyc,ttyd) on zsc0 */
160 channel = kgdb_dev & 1;
161 printf("zs_kgdb_init: attaching tty0%c at %d baud\n",
162 '0' + (kgdb_dev & 3), kgdb_rate);
163
164 if (!zsinited)
165 zs_init();
166
167 /* Setup temporary chanstate. */
168 bzero((caddr_t)&cs, sizeof(cs));
169 zc = zs_get_chan_addr(0, channel);
170 if (zc == NULL) {
171 printf("zs_kgdb_init: zs not mapped.\n");
172 kgdb_dev = -1;
173 return;
174 }
175
176 cs.cs_channel = channel;
177 cs.cs_brg_clk = PCLK / 16;
178 cs.cs_reg_csr = &zc->zc_csr;
179 cs.cs_reg_data = &zc->zc_data;
180
181 /* Now set parameters. (interrupts disabled) */
182 zs_setparam(&cs, 0, kgdb_rate);
183
184 /* Store the getc/putc functions and arg. */
185 kgdb_attach(zs_getc, zs_putc, (void *)zc);
186 }
187
188 /*
189 * This is a "hook" called by zstty_attach to allow the tty
190 * to be "taken over" for exclusive use by kgdb.
191 * Return non-zero if this is the kgdb port.
192 *
193 * Set the speed to kgdb_rate, CS8, etc.
194 */
195 int
196 zs_check_kgdb(cs, dev)
197 struct zs_chanstate *cs;
198 int dev;
199 {
200
201 if (dev != kgdb_dev)
202 return (0);
203
204 /*
205 * Yes, this is port in use by kgdb.
206 */
207 cs->cs_private = NULL;
208 cs->cs_ops = &zsops_kgdb;
209
210 /* Now set parameters. (interrupts enabled) */
211 zs_setparam(cs, 1, kgdb_rate);
212
213 return (1);
214 }
215
216 /*
217 * KGDB framing character received: enter kernel debugger. This probably
218 * should time out after a few seconds to avoid hanging on spurious input.
219 */
220 static void
221 zskgdb(cs)
222 struct zs_chanstate *cs;
223 {
224 int unit = minor(kgdb_dev);
225
226 printf("zstty%d: kgdb interrupt\n", unit);
227 /* This will trap into the debugger. */
228 kgdb_connect(1);
229 }
230
231
232 /****************************************************************
233 * Interface to the lower layer (zscc)
234 ****************************************************************/
235
236 static void zs_kgdb_rxint __P((struct zs_chanstate *));
237 static void zs_kgdb_stint __P((struct zs_chanstate *, int));
238 static void zs_kgdb_txint __P((struct zs_chanstate *));
239 static void zs_kgdb_softint __P((struct zs_chanstate *));
240
241 int kgdb_input_lost;
242
243 static void
244 zs_kgdb_rxint(cs)
245 struct zs_chanstate *cs;
246 {
247 register u_char c, rr1;
248
249 /*
250 * First read the status, because reading the received char
251 * destroys the status of this char.
252 */
253 rr1 = zs_read_reg(cs, 1);
254 c = zs_read_data(cs);
255
256 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
257 /* Clear the receive error. */
258 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
259 }
260
261 if (c == KGDB_START) {
262 zskgdb(cs);
263 } else {
264 kgdb_input_lost++;
265 }
266 }
267
268 static void
269 zs_kgdb_txint(cs)
270 register struct zs_chanstate *cs;
271 {
272 register int rr0;
273
274 rr0 = zs_read_csr(cs);
275 zs_write_csr(cs, ZSWR0_RESET_TXINT);
276 }
277
278 static void
279 zs_kgdb_stint(cs, force)
280 register struct zs_chanstate *cs;
281 int force;
282 {
283 register int rr0;
284
285 rr0 = zs_read_csr(cs);
286 zs_write_csr(cs, ZSWR0_RESET_STATUS);
287
288 /*
289 * Check here for console break, so that we can abort
290 * even when interrupts are locking up the machine.
291 */
292 if (rr0 & ZSRR0_BREAK) {
293 zskgdb(cs);
294 }
295 }
296
297 static void
298 zs_kgdb_softint(cs)
299 struct zs_chanstate *cs;
300 {
301 printf("zs_kgdb_softint?\n");
302 }
303
304 struct zsops zsops_kgdb = {
305 zs_kgdb_rxint, /* receive char available */
306 zs_kgdb_stint, /* external/status */
307 zs_kgdb_txint, /* xmit buffer empty */
308 zs_kgdb_softint, /* process software interrupt */
309 };
310