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