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