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