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