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