zs_kgdb.c revision 1.1 1 /* $NetBSD: zs_kgdb.c,v 1.1 1999/03/27 02:59:41 dbj 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/param.h>
51 #include <sys/systm.h>
52 #include <sys/proc.h>
53 #include <sys/device.h>
54 #include <sys/conf.h>
55 #include <sys/ioctl.h>
56 #include <sys/kernel.h>
57 #include <sys/syslog.h>
58 #include <sys/kgdb.h>
59
60 #include <machine/cpu.h>
61
62 #include <dev/ic/z8530reg.h>
63 #include <machine/z8530var.h>
64 #include <next68k/dev/zs_cons.h>
65
66 /*
67 * The NeXT provides a 3.686400 MHz clock to the ZS chips.
68 */
69 #define PCLK (9600 * 384) /* PCLK pin input clock rate */
70
71 /* The layout of this is hardware-dependent (padding, order). */
72 struct zschan {
73 volatile u_char zc_csr; /* ctrl,status, and indirect access */
74 u_char zc_xxx0;
75 volatile u_char zc_data; /* data */
76 u_char zc_xxx1;
77 };
78
79 static void zs_setparam __P((struct zs_chanstate *, int, int));
80 struct zsops zsops_kgdb;
81
82 static u_char zs_kgdb_regs[16] = {
83 0, /* 0: CMD (reset, etc.) */
84 0, /* 1: No interrupts yet. */
85 0x18 + NEXT_I_IPL(NEXT_I_SCC), /* 2: IVECT */
86 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
87 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
88 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
89 0, /* 6: TXSYNC/SYNCLO */
90 0, /* 7: RXSYNC/SYNCHI */
91 0, /* 8: alias for data port */
92 ZSWR9_MASTER_IE,
93 0, /*10: Misc. TX/RX control bits */
94 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
95 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */
96 0, /*13: BAUDHI (default=9600) */
97 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
98 ZSWR15_BREAK_IE,
99 };
100
101 /*
102 * This replaces "zs_reset()" in the sparc driver.
103 */
104 static void
105 zs_setparam(cs, iena, rate)
106 struct zs_chanstate *cs;
107 int iena;
108 int rate;
109 {
110 int s, tconst;
111
112 bcopy(zs_kgdb_regs, cs->cs_preg, 16);
113
114 if (iena) {
115 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
116 }
117
118 /* Initialize the speed, etc. */
119 tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
120 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
121 cs->cs_preg[12] = tconst;
122 cs->cs_preg[13] = tconst >> 8;
123
124 s = splhigh();
125 zs_loadchannelregs(cs);
126 splx(s);
127 }
128
129 /*
130 * Set up for kgdb; called at boot time before configuration.
131 * KGDB interrupts will be enabled later when zs0 is configured.
132 * Called after cninit(), so printf() etc. works.
133 */
134 void
135 zs_kgdb_init()
136 {
137 struct zs_chanstate cs;
138 volatile struct zschan *zc;
139 int channel, zs_unit;
140
141 printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev);
142 if (major(kgdb_dev) != zs_major)
143 return;
144
145 /* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
146 zs_unit = (kgdb_dev & 2) ? 2 : 0; /* XXX - config info! */
147 channel = kgdb_dev & 1;
148 printf("zs_kgdb_init: attaching tty%c at %d baud\n",
149 'a' + (kgdb_dev & 3), kgdb_rate);
150
151 /* Setup temporary chanstate. */
152 bzero((caddr_t)&cs, sizeof(cs));
153 zc = zs_get_chan_addr(zs_unit, channel);
154
155 if (zc == NULL) {
156 printf("zs_kgdb_init: zs not mapped.\n");
157 kgdb_dev = -1;
158 return;
159 }
160
161 cs.cs_channel = channel;
162 cs.cs_brg_clk = PCLK / 16;
163 cs.cs_reg_csr = &zc->zc_csr;
164 cs.cs_reg_data = &zc->zc_data;
165
166 /* Now set parameters. (interrupts disabled) */
167 zs_setparam(&cs, 0, kgdb_rate);
168
169 /* Store the getc/putc functions and arg. */
170 kgdb_attach(zs_getc, zs_putc, (void *)zc);
171 }
172
173 /*
174 * This is a "hook" called by zstty_attach to allow the tty
175 * to be "taken over" for exclusive use by kgdb.
176 * Return non-zero if this is the kgdb port.
177 *
178 * Set the speed to kgdb_rate, CS8, etc.
179 */
180 int
181 zs_check_kgdb(cs, dev)
182 struct zs_chanstate *cs;
183 int dev;
184 {
185
186 if (dev != kgdb_dev)
187 return (0);
188
189 /*
190 * Yes, this is port in use by kgdb.
191 */
192 cs->cs_private = NULL;
193 cs->cs_ops = &zsops_kgdb;
194
195 /* Now set parameters. (interrupts enabled) */
196 zs_setparam(cs, 1, kgdb_rate);
197
198 return (1);
199 }
200
201 /*
202 * KGDB framing character received: enter kernel debugger. This probably
203 * should time out after a few seconds to avoid hanging on spurious input.
204 */
205 void
206 zskgdb(cs)
207 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 __P((struct zs_chanstate *));
222 static void zs_kgdb_stint __P((struct zs_chanstate *, int));
223 static void zs_kgdb_txint __P((struct zs_chanstate *));
224 static void zs_kgdb_softint __P((struct zs_chanstate *));
225
226 int kgdb_input_lost;
227
228 static void
229 zs_kgdb_rxint(cs)
230 struct zs_chanstate *cs;
231 {
232 register u_char c, rr1;
233
234 /*
235 * First read the status, because reading the received char
236 * destroys the status of this char.
237 */
238 rr1 = zs_read_reg(cs, 1);
239 c = zs_read_data(cs);
240
241 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
242 /* Clear the receive error. */
243 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
244 }
245
246 if (c == KGDB_START) {
247 zskgdb(cs);
248 } else {
249 kgdb_input_lost++;
250 }
251 }
252
253 static void
254 zs_kgdb_txint(cs)
255 register struct zs_chanstate *cs;
256 {
257 register int rr0;
258
259 rr0 = zs_read_csr(cs);
260 zs_write_csr(cs, ZSWR0_RESET_TXINT);
261 }
262
263 static void
264 zs_kgdb_stint(cs, force)
265 register struct zs_chanstate *cs;
266 int force;
267 {
268 register int rr0;
269
270 rr0 = zs_read_csr(cs);
271 zs_write_csr(cs, ZSWR0_RESET_STATUS);
272
273 /*
274 * Check here for console break, so that we can abort
275 * even when interrupts are locking up the machine.
276 */
277 if (rr0 & ZSRR0_BREAK) {
278 zskgdb(cs);
279 }
280 }
281
282 static void
283 zs_kgdb_softint(cs)
284 struct zs_chanstate *cs;
285 {
286 printf("zs_kgdb_softint?\n");
287 }
288
289 struct zsops zsops_kgdb = {
290 zs_kgdb_rxint, /* receive char available */
291 zs_kgdb_stint, /* external/status */
292 zs_kgdb_txint, /* xmit buffer empty */
293 zs_kgdb_softint, /* process software interrupt */
294 };
295