Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.5
      1 /*	$NetBSD: zs_kgdb.c,v 1.5 2000/03/21 12:48:45 pk 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 /* Suns provide a 4.9152 MHz clock to the ZS chips. */
     66 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     67 
     68 /* The layout of this is hardware-dependent (padding, order). */
     69 struct zschan {
     70 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     71 	u_char		zc_xxx0;
     72 	volatile u_char	zc_data;	/* data */
     73 	u_char		zc_xxx1;
     74 };
     75 struct zsdevice {
     76 	/* Yes, they are backwards. */
     77 	struct	zschan zs_chan_b;
     78 	struct	zschan zs_chan_a;
     79 };
     80 
     81 static void zs_setparam __P((struct zs_chanstate *, int, int));
     82 static void *findzs __P((int));
     83 struct zsops zsops_kgdb;
     84 
     85 extern int  zs_getc __P((void *arg));
     86 extern void zs_putc __P((void *arg, int c));
     87 
     88 static u_char zs_kgdb_regs[16] = {
     89 	0,	/* 0: CMD (reset, etc.) */
     90 	0,	/* 1: No interrupts yet. */
     91 	0,	/* 2: 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 | ZSWR9_NO_VECTOR,
     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_ENA | ZSWR14_BAUD_FROM_PCLK,
    104 	ZSWR15_BREAK_IE,
    105 };
    106 
    107 /*
    108  * This replaces "zs_reset()" in the sparc driver.
    109  */
    110 static void
    111 zs_setparam(cs, iena, rate)
    112 	struct zs_chanstate *cs;
    113 	int iena;
    114 	int rate;
    115 {
    116 	int s, tconst;
    117 
    118 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
    119 
    120 	if (iena) {
    121 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    122 	}
    123 
    124 	/* Initialize the speed, etc. */
    125 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    126 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    127 	cs->cs_preg[12] = tconst;
    128 	cs->cs_preg[13] = tconst >> 8;
    129 
    130 	s = splhigh();
    131 	zs_loadchannelregs(cs);
    132 	splx(s);
    133 }
    134 
    135 /*
    136  * Set up for kgdb; called at boot time before configuration.
    137  * KGDB interrupts will be enabled later when zs0 is configured.
    138  * Called after cninit(), so printf() etc. works.
    139  */
    140 void
    141 zs_kgdb_init()
    142 {
    143 	struct zs_chanstate cs;
    144 	struct zsdevice *zsd;
    145 	volatile struct zschan *zc;
    146 	int channel, promzs_unit;
    147 
    148 	/* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
    149 	if (major(kgdb_dev) != zs_major)
    150 		return;
    151 
    152 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
    153 	promzs_unit = (kgdb_dev & 2) ? 2 : 0;
    154 	channel  =  kgdb_dev & 1;
    155 	printf("zs_kgdb_init: attaching tty%c at %d baud\n",
    156 		   'a' + (kgdb_dev & 3), kgdb_rate);
    157 
    158 	/* Setup temporary chanstate. */
    159 	bzero((caddr_t)&cs, sizeof(cs));
    160 	zsd = findzs(promzs_unit);
    161 	if (zsd == NULL) {
    162 		printf("zs_kgdb_init: zs not mapped.\n");
    163 		return;
    164 	}
    165 	zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    166 
    167 	cs.cs_channel = channel;
    168 	cs.cs_brg_clk = PCLK / 16;
    169 	cs.cs_reg_csr  = &zc->zc_csr;
    170 	cs.cs_reg_data = &zc->zc_data;
    171 
    172 	/* Now set parameters. (interrupts disabled) */
    173 	zs_setparam(&cs, 0, kgdb_rate);
    174 
    175 	/* Store the getc/putc functions and arg. */
    176 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
    177 }
    178 
    179 /*
    180  * This is a "hook" called by zstty_attach to allow the tty
    181  * to be "taken over" for exclusive use by kgdb.
    182  * Return non-zero if this is the kgdb port.
    183  *
    184  * Set the speed to kgdb_rate, CS8, etc.
    185  */
    186 int
    187 zs_check_kgdb(cs, dev)
    188 	struct zs_chanstate *cs;
    189 	int dev;
    190 {
    191 
    192 	if (dev != kgdb_dev)
    193 		return (0);
    194 
    195 	/*
    196 	 * Yes, this is port in use by kgdb.
    197 	 */
    198 	cs->cs_private = NULL;
    199 	cs->cs_ops = &zsops_kgdb;
    200 
    201 	/* Now set parameters. (interrupts enabled) */
    202 	zs_setparam(cs, 1, kgdb_rate);
    203 
    204 	return (1);
    205 }
    206 
    207 /*
    208  * KGDB framing character received: enter kernel debugger.  This probably
    209  * should time out after a few seconds to avoid hanging on spurious input.
    210  */
    211 void
    212 zskgdb(cs)
    213 	struct zs_chanstate *cs;
    214 {
    215 	int unit = minor(kgdb_dev);
    216 
    217 	printf("zstty%d: kgdb interrupt\n", unit);
    218 	/* This will trap into the debugger. */
    219 	kgdb_connect(1);
    220 }
    221 
    222 
    223 /****************************************************************
    224  * Interface to the lower layer (zscc)
    225  ****************************************************************/
    226 
    227 static void zs_kgdb_rxint __P((struct zs_chanstate *));
    228 static void zs_kgdb_stint __P((struct zs_chanstate *, int));
    229 static void zs_kgdb_txint __P((struct zs_chanstate *));
    230 static void zs_kgdb_softint __P((struct zs_chanstate *));
    231 
    232 int kgdb_input_lost;
    233 
    234 static void
    235 zs_kgdb_rxint(cs)
    236 	struct zs_chanstate *cs;
    237 {
    238 	register u_char c, rr1;
    239 
    240 	/*
    241 	 * First read the status, because reading the received char
    242 	 * destroys the status of this char.
    243 	 */
    244 	rr1 = zs_read_reg(cs, 1);
    245 	c = zs_read_data(cs);
    246 
    247 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    248 		/* Clear the receive error. */
    249 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    250 	}
    251 
    252 	if (c == KGDB_START) {
    253 		zskgdb(cs);
    254 	} else {
    255 		kgdb_input_lost++;
    256 	}
    257 }
    258 
    259 static void
    260 zs_kgdb_txint(cs)
    261 	register struct zs_chanstate *cs;
    262 {
    263 	register int rr0;
    264 
    265 	rr0 = zs_read_csr(cs);
    266 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    267 }
    268 
    269 static void
    270 zs_kgdb_stint(cs, force)
    271 	register struct zs_chanstate *cs;
    272 	int force;
    273 {
    274 	register int rr0;
    275 
    276 	rr0 = zs_read_csr(cs);
    277 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    278 
    279 	/*
    280 	 * Check here for console break, so that we can abort
    281 	 * even when interrupts are locking up the machine.
    282 	 */
    283 	if (rr0 & ZSRR0_BREAK) {
    284 		zskgdb(cs);
    285 	}
    286 }
    287 
    288 static void
    289 zs_kgdb_softint(cs)
    290 	struct zs_chanstate *cs;
    291 {
    292 	printf("zs_kgdb_softint?\n");
    293 }
    294 
    295 struct zsops zsops_kgdb = {
    296 	zs_kgdb_rxint,	/* receive char available */
    297 	zs_kgdb_stint,	/* external/status */
    298 	zs_kgdb_txint,	/* xmit buffer empty */
    299 	zs_kgdb_softint,	/* process software interrupt */
    300 };
    301 
    302 /*
    303  * findzs() should return the address of the given zs channel.
    304  * Here we count on the PROM to map in the required zs chips.
    305  */
    306 void *
    307 findzs(zs)
    308 	int zs;
    309 {
    310 
    311 #if defined(SUN4)
    312 	if (CPU_ISSUN4) {
    313 		/*
    314 		 * On sun4, we use hard-coded physical addresses
    315 		 */
    316 #define ZS0_PHYS	0xf1000000
    317 #define ZS1_PHYS	0xf0000000
    318 #define ZS2_PHYS	0xe0000000
    319 		bus_space_handle_t bh;
    320 		bus_addr_t paddr;
    321 
    322 		switch (zs) {
    323 		case 0:
    324 			paddr = ZS0_PHYS;
    325 			break;
    326 		case 1:
    327 			paddr = ZS1_PHYS;
    328 			break;
    329 		case 2:
    330 			paddr = ZS2_PHYS;
    331 			break;
    332 		default:
    333 			return (NULL);
    334 		}
    335 
    336 		if (cpuinfo.cpu_type == CPUTYP_4_100)
    337 			/* Clear top bits of physical address on 4/100 */
    338 			paddr &= ~0xf0000000;
    339 
    340 		/*
    341 		 * Have the obio module figure out which virtual
    342 		 * address the device is mapped to.
    343 		 */
    344 		if (obio_find_rom_map(paddr, PMAP_OBIO, NBPG, &bh) != 0)
    345 			return (NULL);
    346 
    347 		return ((void *)bh);
    348 	}
    349 #endif
    350 
    351 #if defined(SUN4C) || defined(SUN4M)
    352 	if (CPU_ISSUN4COR4M) {
    353 		int node;
    354 
    355 		node = firstchild(findroot());
    356 		if (CPU_ISSUN4M) {
    357 			/*
    358 			 * On sun4m machines zs is in "obio" tree.
    359 			 */
    360 			node = findnode(node, "obio");
    361 			if (node == 0)
    362 				panic("findzs: no obio node");
    363 			node = firstchild(node);
    364 		}
    365 		while ((node = findnode(node, "zs")) != 0) {
    366 			int nvaddrs, *vaddrs, vstore[10];
    367 
    368 			if (getpropint(node, "slave", -1) != zs) {
    369 				node = nextsibling(node);
    370 				continue;
    371 			}
    372 
    373 			/*
    374 			 * On some machines (e.g. the Voyager), the zs
    375 			 * device has multi-valued register properties.
    376 			 */
    377 			vaddrs = vstore;
    378 			nvaddrs = sizeof(vstore)/sizeof(vstore[0]);
    379 			if (getprop(node, "address", sizeof(int),
    380 				    &nvaddrs, (void **)&vaddrs) != 0)
    381 				return (NULL);
    382 
    383 			return ((void *)vaddrs[0]);
    384 		}
    385 	}
    386 #endif
    387 	return (NULL);
    388 }
    389