xref: /netbsd-src/sys/arch/sparc/dev/zs_kgdb.c (revision 8b0f9554ff8762542c4defc4f70e1eb76fb508fa)
1 /*	$NetBSD: zs_kgdb.c,v 1.17 2007/03/04 06:00:44 christos 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 <sys/cdefs.h>
51 __KERNEL_RCSID(0, "$NetBSD: zs_kgdb.c,v 1.17 2007/03/04 06:00:44 christos Exp $");
52 
53 #include "opt_kgdb.h"
54 
55 #include <sys/param.h>
56 #include <sys/systm.h>
57 #include <sys/proc.h>
58 #include <sys/device.h>
59 #include <sys/conf.h>
60 #include <sys/ioctl.h>
61 #include <sys/kernel.h>
62 #include <sys/syslog.h>
63 #include <sys/kgdb.h>
64 
65 #include <dev/ic/z8530reg.h>
66 #include <machine/z8530var.h>
67 #include <machine/autoconf.h>
68 #include <machine/promlib.h>
69 #include <sparc/dev/cons.h>
70 
71 /* Suns provide a 4.9152 MHz clock to the ZS chips. */
72 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
73 
74 /* The layout of this is hardware-dependent (padding, order). */
75 struct zschan {
76 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
77 	u_char		zc_xxx0;
78 	volatile u_char	zc_data;	/* data */
79 	u_char		zc_xxx1;
80 };
81 struct zsdevice {
82 	/* Yes, they are backwards. */
83 	struct	zschan zs_chan_b;
84 	struct	zschan zs_chan_a;
85 };
86 
87 static void zs_setparam(struct zs_chanstate *, int, int);
88 static void *findzs(int);
89 struct zsops zsops_kgdb;
90 
91 extern int  zs_getc(void *);
92 extern void zs_putc(void *, int);
93 
94 static u_char zs_kgdb_regs[16] = {
95 	0,	/* 0: CMD (reset, etc.) */
96 	0,	/* 1: No interrupts yet. */
97 	0,	/* 2: IVECT */
98 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
99 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
100 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
101 	0,	/* 6: TXSYNC/SYNCLO */
102 	0,	/* 7: RXSYNC/SYNCHI */
103 	0,	/* 8: alias for data port */
104 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
105 	0,	/*10: Misc. TX/RX control bits */
106 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
107 	14,	/*12: BAUDLO (default=9600) */
108 	0,	/*13: BAUDHI (default=9600) */
109 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
110 	ZSWR15_BREAK_IE,
111 };
112 
113 /*
114  * This replaces "zs_reset()" in the sparc driver.
115  */
116 static void
117 zs_setparam(struct zs_chanstate *cs, int iena, int rate)
118 {
119 	int s, tconst;
120 
121 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
122 
123 	if (iena) {
124 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
125 	}
126 
127 	/* Initialize the speed, etc. */
128 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
129 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
130 	cs->cs_preg[12] = tconst;
131 	cs->cs_preg[13] = tconst >> 8;
132 
133 	s = splhigh();
134 	zs_loadchannelregs(cs);
135 	splx(s);
136 }
137 
138 /*
139  * Set up for kgdb; called at boot time before configuration.
140  * KGDB interrupts will be enabled later when zs0 is configured.
141  * Called after cninit(), so printf() etc. works.
142  */
143 void
144 zs_kgdb_init(void)
145 {
146 	struct zs_chanstate cs;
147 	struct zsdevice *zsd;
148 	volatile struct zschan *zc;
149 	int channel, promzs_unit;
150 	extern const struct cdevsw zstty_cdevsw;
151 
152 	/* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
153 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
154 		return;
155 
156 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
157 	promzs_unit = (kgdb_dev & 2) ? 2 : 0;
158 	channel  =  kgdb_dev & 1;
159 	printf("zs_kgdb_init: attaching tty%c at %d baud\n",
160 		   'a' + (kgdb_dev & 3), kgdb_rate);
161 
162 	/* Setup temporary chanstate. */
163 	bzero((void *)&cs, sizeof(cs));
164 	zsd = findzs(promzs_unit);
165 	if (zsd == NULL) {
166 		printf("zs_kgdb_init: zs not mapped.\n");
167 		return;
168 	}
169 	zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
170 
171 	cs.cs_channel = channel;
172 	cs.cs_brg_clk = PCLK / 16;
173 	cs.cs_reg_csr  = &zc->zc_csr;
174 	cs.cs_reg_data = &zc->zc_data;
175 
176 	/* Now set parameters. (interrupts disabled) */
177 	zs_setparam(&cs, 0, kgdb_rate);
178 
179 	/* Store the getc/putc functions and arg. */
180 	kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
181 }
182 
183 /*
184  * This is a "hook" called by zstty_attach to allow the tty
185  * to be "taken over" for exclusive use by kgdb.
186  * Return non-zero if this is the kgdb port.
187  *
188  * Set the speed to kgdb_rate, CS8, etc.
189  */
190 int
191 zs_check_kgdb(struct zs_chanstate *cs, int dev)
192 {
193 
194 	if (dev != kgdb_dev)
195 		return (0);
196 
197 	/*
198 	 * Yes, this is port in use by kgdb.
199 	 */
200 	cs->cs_private = NULL;
201 	cs->cs_ops = &zsops_kgdb;
202 
203 	/* Now set parameters. (interrupts enabled) */
204 	zs_setparam(cs, 1, kgdb_rate);
205 
206 	return (1);
207 }
208 
209 /*
210  * KGDB framing character received: enter kernel debugger.  This probably
211  * should time out after a few seconds to avoid hanging on spurious input.
212  */
213 void
214 zskgdb(struct zs_chanstate *cs)
215 {
216 	int unit = minor(kgdb_dev);
217 
218 	printf("zstty%d: kgdb interrupt\n", unit);
219 	/* This will trap into the debugger. */
220 	kgdb_connect(1);
221 }
222 
223 
224 /****************************************************************
225  * Interface to the lower layer (zscc)
226  ****************************************************************/
227 
228 static void zs_kgdb_rxint(struct zs_chanstate *);
229 static void zs_kgdb_stint(struct zs_chanstate *, int);
230 static void zs_kgdb_txint(struct zs_chanstate *);
231 static void zs_kgdb_softint(struct zs_chanstate *);
232 
233 int kgdb_input_lost;
234 
235 static void
236 zs_kgdb_rxint(struct zs_chanstate *cs)
237 {
238 	register u_char c, rr1;
239 
240 	/*
241 	 * First read the status, because reading the received char
242 	 * destroys the status of this char.
243 	 */
244 	rr1 = zs_read_reg(cs, 1);
245 	c = zs_read_data(cs);
246 
247 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
248 		/* Clear the receive error. */
249 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
250 	}
251 
252 	if (c == KGDB_START) {
253 		zskgdb(cs);
254 	} else {
255 		kgdb_input_lost++;
256 	}
257 }
258 
259 static void
260 zs_kgdb_txint(struct zs_chanstate *cs)
261 {
262 	register int rr0;
263 
264 	rr0 = zs_read_csr(cs);
265 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
266 }
267 
268 static void
269 zs_kgdb_stint(struct zs_chanstate *cs, 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(struct zs_chanstate *cs)
287 {
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 
299 /*
300  * findzs() should return the address of the given zs channel.
301  * Here we count on the PROM to map in the required zs chips.
302  */
303 static void *
304 findzs(int zs)
305 {
306 
307 #if defined(SUN4)
308 	if (CPU_ISSUN4) {
309 		/*
310 		 * On sun4, we use hard-coded physical addresses
311 		 */
312 #define ZS0_PHYS	0xf1000000
313 #define ZS1_PHYS	0xf0000000
314 #define ZS2_PHYS	0xe0000000
315 		bus_space_handle_t bh;
316 		bus_addr_t paddr;
317 
318 		switch (zs) {
319 		case 0:
320 			paddr = ZS0_PHYS;
321 			break;
322 		case 1:
323 			paddr = ZS1_PHYS;
324 			break;
325 		case 2:
326 			paddr = ZS2_PHYS;
327 			break;
328 		default:
329 			return (NULL);
330 		}
331 
332 		if (cpuinfo.cpu_type == CPUTYP_4_100)
333 			/* Clear top bits of physical address on 4/100 */
334 			paddr &= ~0xf0000000;
335 
336 		/*
337 		 * Have the obio module figure out which virtual
338 		 * address the device is mapped to.
339 		 */
340 		if (obio_find_rom_map(paddr, PAGE_SIZE, &bh) != 0)
341 			return (NULL);
342 
343 		return ((void *)bh);
344 	}
345 #endif
346 
347 #if defined(SUN4C) || defined(SUN4M)
348 	if (CPU_ISSUN4C || CPU_ISSUN4M) {
349 		int node;
350 
351 		node = firstchild(findroot());
352 		if (CPU_ISSUN4M) {
353 			/*
354 			 * On sun4m machines zs is in "obio" tree.
355 			 */
356 			node = findnode(node, "obio");
357 			if (node == 0)
358 				panic("findzs: no obio node");
359 			node = firstchild(node);
360 		}
361 		while ((node = findnode(node, "zs")) != 0) {
362 			int nvaddrs, *vaddrs, vstore[10];
363 
364 			if (prom_getpropint(node, "slave", -1) != zs) {
365 				node = nextsibling(node);
366 				continue;
367 			}
368 
369 			/*
370 			 * On some machines (e.g. the Voyager), the zs
371 			 * device has multi-valued register properties.
372 			 */
373 			vaddrs = vstore;
374 			nvaddrs = sizeof(vstore)/sizeof(vstore[0]);
375 			if (prom_getprop(node, "address", sizeof(int),
376 				    &nvaddrs, &vaddrs) != 0)
377 				return (NULL);
378 
379 			return ((void *)vaddrs[0]);
380 		}
381 	}
382 #endif
383 	return (NULL);
384 }
385