xref: /netbsd/sys/arch/mipsco/obio/zs_kgdb.c (revision bf9ec67e)
1 /*	$NetBSD: zs_kgdb.c,v 1.2 2001/07/08 04:25:36 wdk 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  * 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 <dev/ic/z8530reg.h>
61 #include <machine/z8530var.h>
62 
63 /*
64  * Hooks for kgdb when attached via the z8530 driver
65  *
66  * To use this, build a kernel with: option KGDB, and
67  * boot that kernel with "-d".  (The kernel will call
68  * zs_kgdb_init, kgdb_connect.)  When the console prints
69  * "kgdb waiting..." you run "gdb -k kernel" and do:
70  *   (gdb) set remotebaud 19200
71  *   (gdb) target remote /dev/ttyb
72  */
73 
74 void zs_kgdb_init __P((void));
75 void zskgdb __P((struct zs_chanstate *cs));
76 
77 static void	zs_setparam __P((struct zs_chanstate *, int, int));
78 static struct	zsops zsops_kgdb;
79 
80 extern struct	zschan *zs_get_chan_addr (int zs_unit, int channel);
81 extern int	zs_getc __P((void *arg));
82 extern void	zs_putc __P((void *arg, int c));
83 
84 static u_char zs_kgdb_regs[16] = {
85 	0,	/* 0: CMD (reset, etc.) */
86 	0,	/* 1: No interrupts yet. */
87 	0,	/* 2: IVECT */
88 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
89 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
90 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
91 	0,	/* 6: TXSYNC/SYNCLO */
92 	0,	/* 7: RXSYNC/SYNCHI */
93 	0,	/* 8: alias for data port */
94 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
95 	0,	/*10: Misc. TX/RX control bits */
96 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
97 	14,	/*12: BAUDLO (default=9600) */
98 	0,	/*13: BAUDHI (default=9600) */
99 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
100 	ZSWR15_BREAK_IE,
101 };
102 
103 static void
104 zs_setparam(cs, iena, rate)
105 	struct zs_chanstate *cs;
106 	int iena;
107 	int rate;
108 {
109 	int s, tconst;
110 
111 	memcpy(cs->cs_preg, zs_kgdb_regs, 16);
112 
113 	if (iena) {
114 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
115 	}
116 
117 	/* Initialize the speed, etc. */
118 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
119 
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 	volatile struct zschan *zc;
138 	int channel, unit;
139 
140 	if (major(kgdb_dev) != zs_major)
141 		return;
142 
143 	unit = (kgdb_dev & 2) ? 2 : 0;
144 	channel = kgdb_dev & 1;
145 	printf("zs_kgdb_init: attaching serial%d at %d baud\n",
146 		   (kgdb_dev & 3), kgdb_rate);
147 
148 	zc = zs_get_chan_addr(unit, channel);
149 
150 	/* Attach KGDB comms functions to this device */
151 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
152 }
153 
154 /*
155  * This is a "hook" called by the MI zstty_attach driver
156  * to allow the tty to be "taken over" for exclusive use by kgdb.
157  * Return non-zero if this is the kgdb port.
158  *
159  * Set the speed to kgdb_rate, CS8, etc.
160  */
161 int
162 zs_check_kgdb(cs, dev)
163 	struct zs_chanstate *cs;
164 	int dev;
165 {
166 
167 	if (dev != kgdb_dev)
168 		return (0);
169 
170 	/*
171 	 * Yes, this is port in use by kgdb.
172 	 */
173 	cs->cs_private = NULL;
174 	cs->cs_ops = &zsops_kgdb;
175 
176 	/* Now set parameters. (interrupts enabled) */
177 	zs_setparam(cs, 1, kgdb_rate);
178 
179 	return (1);
180 }
181 
182 /*
183  * KGDB framing character received: enter kernel debugger.  This probably
184  * should time out after a few seconds to avoid hanging on spurious input.
185  */
186 void
187 zskgdb(cs)
188 	struct zs_chanstate *cs;
189 {
190 	int unit = minor(kgdb_dev);
191 
192 	printf("zstty%d: kgdb interrupt\n", unit);
193 	/* This will trap into the debugger. */
194 	kgdb_connect(1);
195 }
196 
197 
198 /****************************************************************
199  * Interface to the lower layer (zscc)
200  ****************************************************************/
201 
202 static void zs_kgdb_rxint __P((struct zs_chanstate *));
203 static void zs_kgdb_stint __P((struct zs_chanstate *, int));
204 static void zs_kgdb_txint __P((struct zs_chanstate *));
205 static void zs_kgdb_softint __P((struct zs_chanstate *));
206 
207 static struct zsops zsops_kgdb = {
208 	zs_kgdb_rxint,		/* receive char available */
209 	zs_kgdb_stint,		/* external/status */
210 	zs_kgdb_txint,		/* xmit buffer empty */
211 	zs_kgdb_softint,	/* process software interrupt */
212 };
213 
214 int kgdb_input_lost;
215 
216 static void
217 zs_kgdb_rxint(cs)
218 	struct zs_chanstate *cs;
219 {
220 	register u_char c, rr1;
221 
222 	/*
223 	 * First read the status, because reading the received char
224 	 * destroys the status of this char.
225 	 */
226 	rr1 = zs_read_reg(cs, 1);
227 	c = zs_read_data(cs);
228 
229 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
230 		/* Clear the receive error. */
231 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
232 	}
233 
234 	if (c == KGDB_START) {
235 		zskgdb(cs);
236 	} else {
237 		kgdb_input_lost++;
238 	}
239 }
240 
241 static void
242 zs_kgdb_txint(cs)
243 	register struct zs_chanstate *cs;
244 {
245 	register int rr0;
246 
247 	rr0 = zs_read_csr(cs);
248 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
249 }
250 
251 static void
252 zs_kgdb_stint(cs, force)
253 	register struct zs_chanstate *cs;
254 	int force;
255 {
256 	register int rr0;
257 
258 	rr0 = zs_read_csr(cs);
259 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
260 
261 	/*
262 	 * Check here for console break, so that we can abort
263 	 * even when interrupts are locking up the machine.
264 	 */
265 	if (rr0 & ZSRR0_BREAK) {
266 		zskgdb(cs);
267 	}
268 }
269 
270 static void
271 zs_kgdb_softint(cs)
272 	struct zs_chanstate *cs;
273 {
274 	printf("zs_kgdb_softint?\n");
275 }
276 
277 
278