Sync usage with man page.
[netbsd-mini2440.git] / sys / arch / mac68k / dev / zs_kgdb.c
blob33b25b01e6f1fa3e5c72cbccf416e729650b66d5
1 /* $NetBSD: zs_kgdb.c,v 1.10 2007/10/17 19:55:14 garbled Exp $ */
3 /*-
4 * Copyright (c) 1996 The NetBSD Foundation, Inc.
5 * All rights reserved.
7 * This code is derived from software contributed to The NetBSD Foundation
8 * by Gordon W. Ross.
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.
19 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 * POSSIBILITY OF SUCH DAMAGE.
33 * Hooks for kgdb when attached via the z8530 driver
35 * To use this, build a kernel with: option KGDB, and
36 * boot that kernel with "-d". (The kernel will call
37 * zs_kgdb_init, kgdb_connect.) When the console prints
38 * "kgdb waiting..." you run "gdb -k kernel" and do:
39 * (gdb) set remotebaud 19200
40 * (gdb) target remote /dev/ttyb
43 #include <sys/cdefs.h>
44 __KERNEL_RCSID(0, "$NetBSD: zs_kgdb.c,v 1.10 2007/10/17 19:55:14 garbled Exp $");
46 #include "opt_kgdb.h"
47 #include <sys/param.h>
48 #include <sys/systm.h>
49 #include <sys/proc.h>
50 #include <sys/device.h>
51 #include <sys/conf.h>
52 #include <sys/ioctl.h>
53 #include <sys/kernel.h>
54 #include <sys/syslog.h>
55 #include <sys/kgdb.h>
57 #include <dev/ic/z8530reg.h>
58 #include <machine/z8530var.h>
59 #include <mac68k/dev/zs_cons.h>
62 * Macs supposely provide a 3.672 MHz clock to the SCC. Unfortunately,
63 * empirical evidence suggests that it's really a 3.6864 MHz clock.
64 * Oh, what to do, what to do?!
66 #define PCLK (9600 * 384) /* PCLK pin input clock rate */
67 #define ZSHARD_PRI 4 /* Wired on the CPU board... */
69 #define ZS_DELAY() delay(2)
71 /* The layout of this is hardware-dependent (padding, order). */
72 struct zschan {
73 volatile u_char zc_csr; /* ctrl,status, and indirect access */
74 u_char zc_xxx0;
75 u_char zc_xxx1; /* part of the other channel lives here! */
76 u_char zc_xxx2; /* Yea Apple! */
77 volatile u_char zc_data; /* data */
78 u_char zc_xxx3;
79 u_char zc_xxx4;
80 u_char zc_xxx5;
83 static void zs_setparam(struct zs_chanstate *, int, int);
84 static void zskgdb(struct zs_chanstate *);
86 struct zsops zsops_kgdb;
88 static u_char zs_kgdb_regs[16] = {
89 0, /* 0: CMD (reset, etc.) */
90 0, /* 1: ~(ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE) */
91 0x18 + ZSHARD_PRI, /* IVECT */
92 ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
93 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
94 ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
95 0, /* 6: TXSYNC/SYNCLO */
96 0, /* 7: RXSYNC/SYNCHI */
97 0, /* 8: alias for data port */
98 ZSWR9_MASTER_IE,
99 0, /*10: Misc. TX/RX control bits */
100 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
101 14, /*12: BAUDLO (default=9600) */
102 0, /*13: BAUDHI (default=9600) */
103 ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
104 ZSWR15_BREAK_IE,
108 * This replaces "zs_reset()" in the sparc driver.
110 static void
111 zs_setparam(struct zs_chanstate *cs, int iena, int rate)
113 int s, tconst;
115 memcpy(cs->cs_preg, zs_kgdb_regs, 16);
117 if (iena) {
118 cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
121 /* Initialize the speed, etc. */
122 tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
123 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
124 cs->cs_preg[12] = tconst;
125 cs->cs_preg[13] = tconst >> 8;
127 s = splhigh();
128 zs_loadchannelregs(cs);
129 splx(s);
133 * Set up for kgdb; called at boot time before configuration.
134 * KGDB interrupts will be enabled later when zs0 is configured.
135 * Called after cninit(), so printf() etc. works.
137 void
138 zs_kgdb_init(void)
140 struct zs_chanstate cs;
141 volatile struct zschan *zc;
142 int channel;
143 extern const struct cdevsw zstty_cdevsw;
145 /* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
146 if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
147 return;
149 /* Note: (ttya,ttyb) on zsc1, and (ttyc,ttyd) on zsc0 */
150 channel = kgdb_dev & 1;
151 printf("zs_kgdb_init: attaching tty0%c at %d baud\n",
152 '0' + (kgdb_dev & 3), kgdb_rate);
154 if (!zsinited)
155 zs_init();
157 /* Setup temporary chanstate. */
158 memset(&cs, 0, sizeof(cs));
159 zc = zs_get_chan_addr(channel);
160 if (zc == NULL) {
161 printf("zs_kgdb_init: zs not mapped.\n");
162 kgdb_dev = -1;
163 return;
166 cs.cs_channel = channel;
167 cs.cs_brg_clk = PCLK / 16;
168 cs.cs_reg_csr = &zc->zc_csr;
169 cs.cs_reg_data = &zc->zc_data;
171 /* Now set parameters. (interrupts disabled) */
172 zs_setparam(&cs, 0, kgdb_rate);
174 /* Store the getc/putc functions and arg. */
175 kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
179 * This is a "hook" called by zstty_attach to allow the tty
180 * to be "taken over" for exclusive use by kgdb.
181 * Return non-zero if this is the kgdb port.
183 * Set the speed to kgdb_rate, CS8, etc.
186 zs_check_kgdb(struct zs_chanstate *cs, int dev)
189 if (dev != kgdb_dev)
190 return (0);
193 * Yes, this is port in use by kgdb.
195 cs->cs_private = NULL;
196 cs->cs_ops = &zsops_kgdb;
198 /* Now set parameters. (interrupts enabled) */
199 zs_setparam(cs, 1, kgdb_rate);
201 return (1);
205 * KGDB framing character received: enter kernel debugger. This probably
206 * should time out after a few seconds to avoid hanging on spurious input.
208 static void
209 zskgdb(struct zs_chanstate *cs)
211 int unit = minor(kgdb_dev);
213 printf("zstty%d: kgdb interrupt\n", unit);
214 /* This will trap into the debugger. */
215 kgdb_connect(1);
219 /****************************************************************
220 * Interface to the lower layer (zscc)
221 ****************************************************************/
223 static void zs_kgdb_rxint(struct zs_chanstate *);
224 static void zs_kgdb_stint(struct zs_chanstate *, int);
225 static void zs_kgdb_txint(struct zs_chanstate *);
226 static void zs_kgdb_softint(struct zs_chanstate *);
228 int kgdb_input_lost;
230 static void
231 zs_kgdb_rxint(struct zs_chanstate *cs)
233 u_char c, rr1;
236 * First read the status, because reading the received char
237 * destroys the status of this char.
239 rr1 = zs_read_reg(cs, 1);
240 c = zs_read_data(cs);
242 if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
243 /* Clear the receive error. */
244 zs_write_csr(cs, ZSWR0_RESET_ERRORS);
247 if (c == KGDB_START) {
248 zskgdb(cs);
249 } else {
250 kgdb_input_lost++;
254 static void
255 zs_kgdb_txint(struct zs_chanstate *cs)
257 int rr0;
259 rr0 = zs_read_csr(cs);
260 zs_write_csr(cs, ZSWR0_RESET_TXINT);
263 static void
264 zs_kgdb_stint(struct zs_chanstate *cs, int force)
266 int rr0;
268 rr0 = zs_read_csr(cs);
269 zs_write_csr(cs, ZSWR0_RESET_STATUS);
272 * Check here for console break, so that we can abort
273 * even when interrupts are locking up the machine.
275 if (rr0 & ZSRR0_BREAK) {
276 zskgdb(cs);
280 static void
281 zs_kgdb_softint(struct zs_chanstate *cs)
283 printf("zs_kgdb_softint?\n");
286 struct zsops zsops_kgdb = {
287 zs_kgdb_rxint, /* receive char available */
288 zs_kgdb_stint, /* external/status */
289 zs_kgdb_txint, /* xmit buffer empty */
290 zs_kgdb_softint, /* process software interrupt */