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