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