xref: /netbsd-src/sys/arch/macppc/dev/zs_kgdb.c (revision 8ac07aec990b9d2e483062509d0a9fa5b4f57cf2)
1 /*	$NetBSD: zs_kgdb.c,v 1.7 2007/10/17 19:55:20 garbled 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.7 2007/10/17 19:55:20 garbled Exp $");
52 
53 #include <sys/param.h>
54 #include <sys/systm.h>
55 #include <sys/proc.h>
56 #include <sys/device.h>
57 #include <sys/conf.h>
58 #include <sys/ioctl.h>
59 #include <sys/kernel.h>
60 #include <sys/syslog.h>
61 #include <sys/kgdb.h>
62 
63 #include <dev/ofw/openfirm.h>
64 #include <dev/ic/z8530reg.h>
65 #include <machine/z8530var.h>
66 
67 static void zs_setparam __P((struct zs_chanstate *, int, int));
68 static void zskgdb __P((struct zs_chanstate *));
69 
70 struct zsops zsops_kgdb;
71 
72 static u_char zs_kgdb_regs[16] = {
73 	0,	/* 0: CMD (reset, etc.) */
74 	0,	/* 1: ~(ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE) */
75 	0,	/* IVECT */
76 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
77 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
78 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
79 	0,	/* 6: TXSYNC/SYNCLO */
80 	0,	/* 7: RXSYNC/SYNCHI */
81 	0,	/* 8: alias for data port */
82 	ZSWR9_MASTER_IE,
83 	0,	/*10: Misc. TX/RX control bits */
84 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
85 	((PCLK/32)/38400)-2,	/*12: BAUDLO (default=38400) */
86 	0,			/*13: BAUDHI (default=38400) */
87 	ZSWR14_BAUD_ENA,
88 	ZSWR15_BREAK_IE,
89 };
90 
91 /*
92  * This replaces "zs_reset()" in the sparc driver.
93  */
94 static void
95 zs_setparam(cs, iena, rate)
96 	struct zs_chanstate *cs;
97 	int iena;
98 	int rate;
99 {
100 	int s, tconst;
101 
102 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
103 
104 	if (iena) {
105 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
106 	}
107 
108 	/* Initialize the speed, etc. */
109 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
110 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
111 	cs->cs_preg[12] = tconst;
112 	cs->cs_preg[13] = tconst >> 8;
113 
114 	s = splhigh();
115 	zs_loadchannelregs(cs);
116 	splx(s);
117 }
118 
119 #ifndef KGDB_DEVNAME
120 #define KGDB_DEVNAME "scca"
121 #endif
122 char *zs_kgdb_devname = KGDB_DEVNAME;
123 /*
124  * Set up for kgdb; called at boot time before configuration.
125  * KGDB interrupts will be enabled later when zs0 is configured.
126  * Called after cninit(), so printf() etc. works.
127  */
128 void
129 zs_kgdb_init()
130 {
131 	extern const struct cdevsw zstty_cdevsw;
132 	struct zs_chanstate cs;
133 	volatile struct zschan *zc;
134 	int escc, escc_ch, obio, zs_offset;
135 	int channel = 0;
136 	u_int32_t reg[5];
137 	char name[16];
138 
139 	if ((escc_ch = OF_finddevice(zs_kgdb_devname)) == -1)
140 		return;
141 
142 	memset(name, 0, sizeof(name));
143 	if (OF_getprop(escc_ch, "device_type", name, sizeof(name)) == -1)
144 		return;
145 
146 	if (strcmp(name, "serial") != 0)
147 		return;
148 
149 	memset(name, 0, sizeof(name));
150 	if (OF_getprop(escc_ch, "name", name, sizeof(name)) == -1)
151 		return;
152 
153 	if (strcmp(name, "ch-b") == 0)
154 		channel = 1;
155 
156 	if (OF_getprop(escc_ch, "reg", reg, sizeof(reg)) < 4)
157 		return;
158 	zs_offset = reg[0];
159 
160 	escc = OF_parent(escc_ch);
161 	obio = OF_parent(escc);
162 
163 	if (OF_getprop(obio, "assigned-addresses", reg, sizeof(reg)) < 12)
164 		return;
165 	zc = (struct zschan *)(reg[2] + zs_offset);
166 
167 	kgdb_dev = makedev(cdevsw_lookup_major(&zstty_cdevsw), channel);
168 
169 	printf("zs_kgdb_init: attaching tty%02d at %d baud\n",
170 		channel, kgdb_rate);
171 
172 	/* Setup temporary chanstate. */
173 	bzero((void *)&cs, sizeof(cs));
174 	cs.cs_channel = channel;
175 	cs.cs_brg_clk = PCLK / 16;
176 	cs.cs_reg_csr  = &zc->zc_csr;
177 	cs.cs_reg_data = &zc->zc_data;
178 
179 	/* Now set parameters. (interrupts disabled) */
180 	zs_setparam(&cs, 0, kgdb_rate);
181 
182 	/* Store the getc/putc functions and arg. */
183 	kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
184 }
185 
186 /*
187  * This is a "hook" called by zstty_attach to allow the tty
188  * to be "taken over" for exclusive use by kgdb.
189  * Return non-zero if this is the kgdb port.
190  *
191  * Set the speed to kgdb_rate, CS8, etc.
192  */
193 int
194 zs_check_kgdb(cs, dev)
195 	struct zs_chanstate *cs;
196 	int dev;
197 {
198 
199 	if (dev != kgdb_dev)
200 		return (0);
201 
202 	/*
203 	 * Yes, this is port in use by kgdb.
204 	 */
205 	cs->cs_private = NULL;
206 	cs->cs_ops = &zsops_kgdb;
207 
208 	/* Now set parameters. (interrupts enabled) */
209 	zs_setparam(cs, 1, kgdb_rate);
210 
211 	return (1);
212 }
213 
214 /*
215  * KGDB framing character received: enter kernel debugger.  This probably
216  * should time out after a few seconds to avoid hanging on spurious input.
217  */
218 static void
219 zskgdb(cs)
220 	struct zs_chanstate *cs;
221 {
222 	int unit = minor(kgdb_dev);
223 
224 	printf("zstty%d: kgdb interrupt\n", unit);
225 	/* This will trap into the debugger. */
226 	kgdb_connect(1);
227 }
228 
229 
230 /****************************************************************
231  * Interface to the lower layer (zscc)
232  ****************************************************************/
233 
234 static void zs_kgdb_rxint __P((struct zs_chanstate *));
235 static void zs_kgdb_stint __P((struct zs_chanstate *, int));
236 static void zs_kgdb_txint __P((struct zs_chanstate *));
237 static void zs_kgdb_softint __P((struct zs_chanstate *));
238 
239 int kgdb_input_lost;
240 
241 static void
242 zs_kgdb_rxint(cs)
243 	struct zs_chanstate *cs;
244 {
245 	register u_char c, rr1;
246 
247 	/*
248 	 * First read the status, because reading the received char
249 	 * destroys the status of this char.
250 	 */
251 	rr1 = zs_read_reg(cs, 1);
252 	c = zs_read_data(cs);
253 
254 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
255 		/* Clear the receive error. */
256 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
257 	}
258 
259 	if (c == KGDB_START) {
260 		zskgdb(cs);
261 	} else {
262 		kgdb_input_lost++;
263 	}
264 }
265 
266 static void
267 zs_kgdb_txint(cs)
268 	register struct zs_chanstate *cs;
269 {
270 	register int rr0;
271 
272 	rr0 = zs_read_csr(cs);
273 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
274 }
275 
276 static void
277 zs_kgdb_stint(cs, force)
278 	register struct zs_chanstate *cs;
279 	int force;
280 {
281 	register int rr0;
282 
283 	rr0 = zs_read_csr(cs);
284 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
285 
286 	/*
287 	 * Check here for console break, so that we can abort
288 	 * even when interrupts are locking up the machine.
289 	 */
290 	if (rr0 & ZSRR0_BREAK) {
291 		zskgdb(cs);
292 	}
293 }
294 
295 static void
296 zs_kgdb_softint(cs)
297 	struct zs_chanstate *cs;
298 {
299 	printf("zs_kgdb_softint?\n");
300 }
301 
302 struct zsops zsops_kgdb = {
303 	zs_kgdb_rxint,	/* receive char available */
304 	zs_kgdb_stint,	/* external/status */
305 	zs_kgdb_txint,	/* xmit buffer empty */
306 	zs_kgdb_softint,	/* process software interrupt */
307 };
308