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