xref: /netbsd-src/sys/arch/next68k/dev/zs_kgdb.c (revision d710132b4b8ce7f7cccaaf660cb16aa16b4077a0)
1 /*	$NetBSD: zs_kgdb.c,v 1.3 2002/09/06 13:18:43 gehenna 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.
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 "opt_kgdb.h"
51 
52 #include <sys/param.h>
53 #include <sys/systm.h>
54 #include <sys/proc.h>
55 #include <sys/device.h>
56 #include <sys/conf.h>
57 #include <sys/ioctl.h>
58 #include <sys/kernel.h>
59 #include <sys/syslog.h>
60 #include <sys/kgdb.h>
61 
62 #include <machine/cpu.h>
63 
64 #include <dev/ic/z8530reg.h>
65 #include <machine/z8530var.h>
66 #include <next68k/dev/zs_cons.h>
67 
68 /*
69  * The NeXT provides a 3.686400 MHz clock to the ZS chips.
70  */
71 #define PCLK	(9600 * 384)		/* PCLK pin input clock rate */
72 
73 /* The layout of this is hardware-dependent (padding, order). */
74 struct zschan {
75 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
76 	u_char		zc_xxx0;
77 	volatile u_char	zc_data;	/* data */
78 	u_char		zc_xxx1;
79 };
80 
81 static void zs_setparam __P((struct zs_chanstate *, int, int));
82 struct zsops zsops_kgdb;
83 
84 static u_char zs_kgdb_regs[16] = {
85 	0,	/* 0: CMD (reset, etc.) */
86 	0,	/* 1: No interrupts yet. */
87 	0x18 + NEXT_I_IPL(NEXT_I_SCC), /* 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,
95 	0,	/*10: Misc. TX/RX control bits */
96 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
97 	((PCLK/32)/9600)-2,	/*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 /*
104  * This replaces "zs_reset()" in the sparc driver.
105  */
106 static void
107 zs_setparam(cs, iena, rate)
108 	struct zs_chanstate *cs;
109 	int iena;
110 	int rate;
111 {
112 	int s, tconst;
113 
114 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
115 
116 	if (iena) {
117 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
118 	}
119 
120 	/* Initialize the speed, etc. */
121 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
122 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
123 	cs->cs_preg[12] = tconst;
124 	cs->cs_preg[13] = tconst >> 8;
125 
126 	s = splhigh();
127 	zs_loadchannelregs(cs);
128 	splx(s);
129 }
130 
131 /*
132  * Set up for kgdb; called at boot time before configuration.
133  * KGDB interrupts will be enabled later when zs0 is configured.
134  * Called after cninit(), so printf() etc. works.
135  */
136 void
137 zs_kgdb_init()
138 {
139 	struct zs_chanstate cs;
140 	volatile struct zschan *zc;
141 	int channel, zs_unit;
142 	extern const struct cdevsw zstty_cdevsw;
143 
144 	printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev);
145 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
146 		return;
147 
148 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
149 	zs_unit = (kgdb_dev & 2) ? 2 : 0;	/* XXX - config info! */
150 	channel  =  kgdb_dev & 1;
151 	printf("zs_kgdb_init: attaching tty%c at %d baud\n",
152 		   'a' + (kgdb_dev & 3), kgdb_rate);
153 
154 	/* Setup temporary chanstate. */
155 	bzero((caddr_t)&cs, sizeof(cs));
156 	zc = zs_get_chan_addr(zs_unit, channel);
157 
158 	if (zc == NULL) {
159 		printf("zs_kgdb_init: zs not mapped.\n");
160 		kgdb_dev = -1;
161 		return;
162 	}
163 
164 	cs.cs_channel = channel;
165 	cs.cs_brg_clk = PCLK / 16;
166 	cs.cs_reg_csr  = &zc->zc_csr;
167 	cs.cs_reg_data = &zc->zc_data;
168 
169 	/* Now set parameters. (interrupts disabled) */
170 	zs_setparam(&cs, 0, kgdb_rate);
171 
172 	/* Store the getc/putc functions and arg. */
173 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
174 }
175 
176 /*
177  * This is a "hook" called by zstty_attach to allow the tty
178  * to be "taken over" for exclusive use by kgdb.
179  * Return non-zero if this is the kgdb port.
180  *
181  * Set the speed to kgdb_rate, CS8, etc.
182  */
183 int
184 zs_check_kgdb(cs, dev)
185 	struct zs_chanstate *cs;
186 	int dev;
187 {
188 
189 	if (dev != kgdb_dev)
190 		return (0);
191 
192 	/*
193 	 * Yes, this is port in use by kgdb.
194 	 */
195 	cs->cs_private = NULL;
196 	cs->cs_ops = &zsops_kgdb;
197 
198 	/* Now set parameters. (interrupts enabled) */
199 	zs_setparam(cs, 1, kgdb_rate);
200 
201 	return (1);
202 }
203 
204 /*
205  * KGDB framing character received: enter kernel debugger.  This probably
206  * should time out after a few seconds to avoid hanging on spurious input.
207  */
208 void
209 zskgdb(cs)
210 	struct zs_chanstate *cs;
211 {
212 	int unit = minor(kgdb_dev);
213 
214 	printf("zstty%d: kgdb interrupt\n", unit);
215 	/* This will trap into the debugger. */
216 	kgdb_connect(1);
217 }
218 
219 
220 /****************************************************************
221  * Interface to the lower layer (zscc)
222  ****************************************************************/
223 
224 static void zs_kgdb_rxint __P((struct zs_chanstate *));
225 static void zs_kgdb_stint __P((struct zs_chanstate *, int));
226 static void zs_kgdb_txint __P((struct zs_chanstate *));
227 static void zs_kgdb_softint __P((struct zs_chanstate *));
228 
229 int kgdb_input_lost;
230 
231 static void
232 zs_kgdb_rxint(cs)
233 	struct zs_chanstate *cs;
234 {
235 	register u_char c, rr1;
236 
237 	/*
238 	 * First read the status, because reading the received char
239 	 * destroys the status of this char.
240 	 */
241 	rr1 = zs_read_reg(cs, 1);
242 	c = zs_read_data(cs);
243 
244 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
245 		/* Clear the receive error. */
246 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
247 	}
248 
249 	if (c == KGDB_START) {
250 		zskgdb(cs);
251 	} else {
252 		kgdb_input_lost++;
253 	}
254 }
255 
256 static void
257 zs_kgdb_txint(cs)
258 	register struct zs_chanstate *cs;
259 {
260 	register int rr0;
261 
262 	rr0 = zs_read_csr(cs);
263 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
264 }
265 
266 static void
267 zs_kgdb_stint(cs, force)
268 	register struct zs_chanstate *cs;
269 	int force;
270 {
271 	register int rr0;
272 
273 	rr0 = zs_read_csr(cs);
274 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
275 
276 	/*
277 	 * Check here for console break, so that we can abort
278 	 * even when interrupts are locking up the machine.
279 	 */
280 	if (rr0 & ZSRR0_BREAK) {
281 		zskgdb(cs);
282 	}
283 }
284 
285 static void
286 zs_kgdb_softint(cs)
287 	struct zs_chanstate *cs;
288 {
289 	printf("zs_kgdb_softint?\n");
290 }
291 
292 struct zsops zsops_kgdb = {
293 	zs_kgdb_rxint,	/* receive char available */
294 	zs_kgdb_stint,	/* external/status */
295 	zs_kgdb_txint,	/* xmit buffer empty */
296 	zs_kgdb_softint,	/* process software interrupt */
297 };
298