Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.1
      1 /*	$NetBSD: zs_kgdb.c,v 1.1 2001/05/11 04:24:44 thorpej 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 <machine/promlib.h>
     63 #include <sparc/dev/cons.h>
     64 
     65 /* The SGI provides a 3.6718 MHz (?) clock to the ZS chips.  */
     66 #define PCLK	3671808
     67 
     68 /* The layout of this is hardware-dependent (padding, order). */
     69 struct zschan {
     70 	u_int32_t	zc_control;
     71 	u_int32_t	zs_data;
     72 };
     73 
     74 struct zsdevice {
     75 	/* Yes, they are backwards. */
     76 	struct	zschan zs_chan_b;
     77 	struct	zschan zs_chan_a;
     78 };
     79 
     80 static void zs_setparam __P((struct zs_chanstate *, int, int));
     81 static void *findzs __P((int));
     82 struct zsops zsops_kgdb;
     83 
     84 extern int  zs_getc __P((void *arg));
     85 extern void zs_putc __P((void *arg, int c));
     86 
     87 static u_char zs_kgdb_regs[16] = {
     88 	0,	/* 0: CMD (reset, etc.) */
     89 	0,	/* 1: No interrupts yet. */
     90 	0,	/* 2: IVECT */
     91 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     92 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     93 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     94 	0,	/* 6: TXSYNC/SYNCLO */
     95 	0,	/* 7: RXSYNC/SYNCHI */
     96 	0,	/* 8: alias for data port */
     97 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
     98 	0,	/*10: Misc. TX/RX control bits */
     99 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    100 	14,	/*12: BAUDLO (default=9600) */
    101 	0,	/*13: BAUDHI (default=9600) */
    102 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    103 	ZSWR15_BREAK_IE,
    104 };
    105 
    106 /*
    107  * This replaces "zs_reset()" in the sparc driver.
    108  */
    109 static void
    110 zs_setparam(cs, iena, rate)
    111 	struct zs_chanstate *cs;
    112 	int iena;
    113 	int rate;
    114 {
    115 	int s, tconst;
    116 
    117 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
    118 
    119 	if (iena) {
    120 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    121 	}
    122 
    123 	/* Initialize the speed, etc. */
    124 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    125 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    126 	cs->cs_preg[12] = tconst;
    127 	cs->cs_preg[13] = tconst >> 8;
    128 
    129 	s = splhigh();
    130 	zs_loadchannelregs(cs);
    131 	splx(s);
    132 }
    133 
    134 /*
    135  * Set up for kgdb; called at boot time before configuration.
    136  * KGDB interrupts will be enabled later when zs0 is configured.
    137  * Called after cninit(), so printf() etc. works.
    138  */
    139 void
    140 zs_kgdb_init()
    141 {
    142 	struct zs_chanstate cs;
    143 	struct zsdevice *zsd;
    144 	volatile struct zschan *zc;
    145 	int channel, promzs_unit;
    146 
    147 	/* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
    148 	if (major(kgdb_dev) != zs_major)
    149 		return;
    150 
    151 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
    152 	promzs_unit = (kgdb_dev & 2) ? 2 : 0;
    153 	channel  =  kgdb_dev & 1;
    154 	printf("zs_kgdb_init: attaching tty%c at %d baud\n",
    155 		   'a' + (kgdb_dev & 3), kgdb_rate);
    156 
    157 	/* Setup temporary chanstate. */
    158 	bzero((caddr_t)&cs, sizeof(cs));
    159 	zsd = findzs(promzs_unit);
    160 	if (zsd == NULL) {
    161 		printf("zs_kgdb_init: zs not mapped.\n");
    162 		return;
    163 	}
    164 	zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    165 
    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;
    170 
    171 	/* Now set parameters. (interrupts disabled) */
    172 	zs_setparam(&cs, 0, kgdb_rate);
    173 
    174 	/* Store the getc/putc functions and arg. */
    175 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
    176 }
    177 
    178 /*
    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.
    182  *
    183  * Set the speed to kgdb_rate, CS8, etc.
    184  */
    185 int
    186 zs_check_kgdb(cs, dev)
    187 	struct zs_chanstate *cs;
    188 	int dev;
    189 {
    190 
    191 	if (dev != kgdb_dev)
    192 		return (0);
    193 
    194 	/*
    195 	 * Yes, this is port in use by kgdb.
    196 	 */
    197 	cs->cs_private = NULL;
    198 	cs->cs_ops = &zsops_kgdb;
    199 
    200 	/* Now set parameters. (interrupts enabled) */
    201 	zs_setparam(cs, 1, kgdb_rate);
    202 
    203 	return (1);
    204 }
    205 
    206 /*
    207  * KGDB framing character received: enter kernel debugger.  This probably
    208  * should time out after a few seconds to avoid hanging on spurious input.
    209  */
    210 void
    211 zskgdb(cs)
    212 	struct zs_chanstate *cs;
    213 {
    214 	int unit = minor(kgdb_dev);
    215 
    216 	printf("zstty%d: kgdb interrupt\n", unit);
    217 	/* This will trap into the debugger. */
    218 	kgdb_connect(1);
    219 }
    220 
    221 
    222 /****************************************************************
    223  * Interface to the lower layer (zscc)
    224  ****************************************************************/
    225 
    226 static void zs_kgdb_rxint __P((struct zs_chanstate *));
    227 static void zs_kgdb_stint __P((struct zs_chanstate *, int));
    228 static void zs_kgdb_txint __P((struct zs_chanstate *));
    229 static void zs_kgdb_softint __P((struct zs_chanstate *));
    230 
    231 int kgdb_input_lost;
    232 
    233 static void
    234 zs_kgdb_rxint(cs)
    235 	struct zs_chanstate *cs;
    236 {
    237 	register u_char c, rr1;
    238 
    239 	/*
    240 	 * First read the status, because reading the received char
    241 	 * destroys the status of this char.
    242 	 */
    243 	rr1 = zs_read_reg(cs, 1);
    244 	c = zs_read_data(cs);
    245 
    246 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    247 		/* Clear the receive error. */
    248 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    249 	}
    250 
    251 	if (c == KGDB_START) {
    252 		zskgdb(cs);
    253 	} else {
    254 		kgdb_input_lost++;
    255 	}
    256 }
    257 
    258 static void
    259 zs_kgdb_txint(cs)
    260 	register 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(cs, force)
    270 	register struct zs_chanstate *cs;
    271 	int force;
    272 {
    273 	register int rr0;
    274 
    275 	rr0 = zs_read_csr(cs);
    276 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    277 
    278 	/*
    279 	 * Check here for console break, so that we can abort
    280 	 * even when interrupts are locking up the machine.
    281 	 */
    282 	if (rr0 & ZSRR0_BREAK) {
    283 		zskgdb(cs);
    284 	}
    285 }
    286 
    287 static void
    288 zs_kgdb_softint(cs)
    289 {
    290 	printf("zs_kgdb_softint?\n");
    291 }
    292 
    293 struct zsops zsops_kgdb = {
    294 	zs_kgdb_rxint,	/* receive char available */
    295 	zs_kgdb_stint,	/* external/status */
    296 	zs_kgdb_txint,	/* xmit buffer empty */
    297 	zs_kgdb_softint,	/* process software interrupt */
    298 };
    299 
    300 /*
    301  * findzs() should return the address of the given zs channel.
    302  * Here we count on the PROM to map in the required zs chips.
    303  */
    304 void *
    305 findzs(zs)
    306 	int zs;
    307 {
    308 	return (NULL);
    309 }
    310