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