Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.11
      1  1.11  thorpej /*	$NetBSD: zs_kgdb.c,v 1.11 2003/04/02 04:35:28 thorpej Exp $	*/
      2   1.1      gwr 
      3   1.1      gwr /*-
      4   1.1      gwr  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5   1.1      gwr  * All rights reserved.
      6   1.1      gwr  *
      7   1.1      gwr  * This code is derived from software contributed to The NetBSD Foundation
      8   1.1      gwr  * by Gordon W. Ross.
      9   1.1      gwr  *
     10   1.1      gwr  * Redistribution and use in source and binary forms, with or without
     11   1.1      gwr  * modification, are permitted provided that the following conditions
     12   1.1      gwr  * are met:
     13   1.1      gwr  * 1. Redistributions of source code must retain the above copyright
     14   1.1      gwr  *    notice, this list of conditions and the following disclaimer.
     15   1.1      gwr  * 2. Redistributions in binary form must reproduce the above copyright
     16   1.1      gwr  *    notice, this list of conditions and the following disclaimer in the
     17   1.1      gwr  *    documentation and/or other materials provided with the distribution.
     18   1.1      gwr  * 3. All advertising materials mentioning features or use of this software
     19   1.1      gwr  *    must display the following acknowledgement:
     20   1.1      gwr  *        This product includes software developed by the NetBSD
     21   1.1      gwr  *        Foundation, Inc. and its contributors.
     22   1.1      gwr  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23   1.1      gwr  *    contributors may be used to endorse or promote products derived
     24   1.1      gwr  *    from this software without specific prior written permission.
     25   1.1      gwr  *
     26   1.1      gwr  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27   1.1      gwr  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28   1.1      gwr  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29   1.1      gwr  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30   1.1      gwr  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31   1.1      gwr  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32   1.1      gwr  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33   1.1      gwr  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34   1.1      gwr  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35   1.1      gwr  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36   1.1      gwr  * POSSIBILITY OF SUCH DAMAGE.
     37   1.1      gwr  */
     38   1.1      gwr 
     39   1.1      gwr /*
     40   1.1      gwr  * Hooks for kgdb when attached via the z8530 driver
     41   1.1      gwr  *
     42   1.1      gwr  * To use this, build a kernel with: option KGDB, and
     43   1.1      gwr  * boot that kernel with "-d".  (The kernel will call
     44   1.1      gwr  * zs_kgdb_init, kgdb_connect.)  When the console prints
     45   1.1      gwr  * "kgdb waiting..." you run "gdb -k kernel" and do:
     46   1.1      gwr  *   (gdb) set remotebaud 19200
     47   1.1      gwr  *   (gdb) target remote /dev/ttyb
     48   1.1      gwr  */
     49   1.6       pk 
     50   1.6       pk #include "opt_kgdb.h"
     51   1.1      gwr 
     52   1.1      gwr #include <sys/param.h>
     53   1.1      gwr #include <sys/systm.h>
     54   1.1      gwr #include <sys/proc.h>
     55   1.1      gwr #include <sys/device.h>
     56   1.1      gwr #include <sys/conf.h>
     57   1.1      gwr #include <sys/ioctl.h>
     58   1.1      gwr #include <sys/kernel.h>
     59   1.1      gwr #include <sys/syslog.h>
     60   1.1      gwr #include <sys/kgdb.h>
     61   1.1      gwr 
     62   1.1      gwr #include <dev/ic/z8530reg.h>
     63   1.1      gwr #include <machine/z8530var.h>
     64   1.7      mrg #include <machine/autoconf.h>
     65   1.5       pk #include <machine/promlib.h>
     66   1.1      gwr #include <sparc/dev/cons.h>
     67   1.1      gwr 
     68   1.2      gwr /* Suns provide a 4.9152 MHz clock to the ZS chips. */
     69   1.1      gwr #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     70   1.1      gwr 
     71   1.1      gwr /* The layout of this is hardware-dependent (padding, order). */
     72   1.1      gwr struct zschan {
     73   1.1      gwr 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     74   1.1      gwr 	u_char		zc_xxx0;
     75   1.1      gwr 	volatile u_char	zc_data;	/* data */
     76   1.1      gwr 	u_char		zc_xxx1;
     77   1.1      gwr };
     78   1.4       pk struct zsdevice {
     79   1.4       pk 	/* Yes, they are backwards. */
     80   1.4       pk 	struct	zschan zs_chan_b;
     81   1.4       pk 	struct	zschan zs_chan_a;
     82   1.4       pk };
     83   1.1      gwr 
     84   1.1      gwr static void zs_setparam __P((struct zs_chanstate *, int, int));
     85   1.5       pk static void *findzs __P((int));
     86   1.1      gwr struct zsops zsops_kgdb;
     87   1.1      gwr 
     88   1.5       pk extern int  zs_getc __P((void *arg));
     89   1.5       pk extern void zs_putc __P((void *arg, int c));
     90   1.5       pk 
     91   1.1      gwr static u_char zs_kgdb_regs[16] = {
     92   1.1      gwr 	0,	/* 0: CMD (reset, etc.) */
     93   1.2      gwr 	0,	/* 1: No interrupts yet. */
     94   1.1      gwr 	0,	/* 2: IVECT */
     95   1.1      gwr 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     96   1.1      gwr 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     97   1.1      gwr 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     98   1.1      gwr 	0,	/* 6: TXSYNC/SYNCLO */
     99   1.1      gwr 	0,	/* 7: RXSYNC/SYNCHI */
    100   1.1      gwr 	0,	/* 8: alias for data port */
    101   1.1      gwr 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
    102   1.1      gwr 	0,	/*10: Misc. TX/RX control bits */
    103   1.1      gwr 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    104   1.1      gwr 	14,	/*12: BAUDLO (default=9600) */
    105   1.1      gwr 	0,	/*13: BAUDHI (default=9600) */
    106   1.1      gwr 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    107   1.3  mycroft 	ZSWR15_BREAK_IE,
    108   1.1      gwr };
    109   1.1      gwr 
    110   1.1      gwr /*
    111   1.1      gwr  * This replaces "zs_reset()" in the sparc driver.
    112   1.1      gwr  */
    113   1.1      gwr static void
    114   1.1      gwr zs_setparam(cs, iena, rate)
    115   1.1      gwr 	struct zs_chanstate *cs;
    116   1.1      gwr 	int iena;
    117   1.1      gwr 	int rate;
    118   1.1      gwr {
    119   1.1      gwr 	int s, tconst;
    120   1.1      gwr 
    121   1.1      gwr 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
    122   1.1      gwr 
    123   1.1      gwr 	if (iena) {
    124   1.1      gwr 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    125   1.1      gwr 	}
    126   1.1      gwr 
    127   1.1      gwr 	/* Initialize the speed, etc. */
    128   1.1      gwr 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    129   1.1      gwr 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    130   1.1      gwr 	cs->cs_preg[12] = tconst;
    131   1.1      gwr 	cs->cs_preg[13] = tconst >> 8;
    132   1.1      gwr 
    133   1.1      gwr 	s = splhigh();
    134   1.1      gwr 	zs_loadchannelregs(cs);
    135   1.1      gwr 	splx(s);
    136   1.1      gwr }
    137   1.1      gwr 
    138   1.1      gwr /*
    139   1.1      gwr  * Set up for kgdb; called at boot time before configuration.
    140   1.1      gwr  * KGDB interrupts will be enabled later when zs0 is configured.
    141   1.1      gwr  * Called after cninit(), so printf() etc. works.
    142   1.1      gwr  */
    143   1.1      gwr void
    144   1.1      gwr zs_kgdb_init()
    145   1.1      gwr {
    146   1.1      gwr 	struct zs_chanstate cs;
    147   1.4       pk 	struct zsdevice *zsd;
    148   1.1      gwr 	volatile struct zschan *zc;
    149   1.4       pk 	int channel, promzs_unit;
    150  1.10  gehenna 	extern const struct cdevsw zstty_cdevsw;
    151   1.1      gwr 
    152   1.1      gwr 	/* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
    153  1.10  gehenna 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
    154   1.1      gwr 		return;
    155   1.1      gwr 
    156   1.2      gwr 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
    157   1.4       pk 	promzs_unit = (kgdb_dev & 2) ? 2 : 0;
    158   1.1      gwr 	channel  =  kgdb_dev & 1;
    159   1.1      gwr 	printf("zs_kgdb_init: attaching tty%c at %d baud\n",
    160   1.1      gwr 		   'a' + (kgdb_dev & 3), kgdb_rate);
    161   1.1      gwr 
    162   1.1      gwr 	/* Setup temporary chanstate. */
    163   1.1      gwr 	bzero((caddr_t)&cs, sizeof(cs));
    164   1.4       pk 	zsd = findzs(promzs_unit);
    165   1.4       pk 	if (zsd == NULL) {
    166   1.1      gwr 		printf("zs_kgdb_init: zs not mapped.\n");
    167   1.1      gwr 		return;
    168   1.1      gwr 	}
    169   1.4       pk 	zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    170   1.1      gwr 
    171   1.1      gwr 	cs.cs_channel = channel;
    172   1.1      gwr 	cs.cs_brg_clk = PCLK / 16;
    173   1.1      gwr 	cs.cs_reg_csr  = &zc->zc_csr;
    174   1.1      gwr 	cs.cs_reg_data = &zc->zc_data;
    175   1.1      gwr 
    176   1.1      gwr 	/* Now set parameters. (interrupts disabled) */
    177   1.1      gwr 	zs_setparam(&cs, 0, kgdb_rate);
    178   1.1      gwr 
    179   1.1      gwr 	/* Store the getc/putc functions and arg. */
    180   1.1      gwr 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
    181   1.1      gwr }
    182   1.1      gwr 
    183   1.1      gwr /*
    184   1.1      gwr  * This is a "hook" called by zstty_attach to allow the tty
    185   1.1      gwr  * to be "taken over" for exclusive use by kgdb.
    186   1.1      gwr  * Return non-zero if this is the kgdb port.
    187   1.1      gwr  *
    188   1.1      gwr  * Set the speed to kgdb_rate, CS8, etc.
    189   1.1      gwr  */
    190   1.1      gwr int
    191   1.1      gwr zs_check_kgdb(cs, dev)
    192   1.1      gwr 	struct zs_chanstate *cs;
    193   1.1      gwr 	int dev;
    194   1.1      gwr {
    195   1.1      gwr 
    196   1.1      gwr 	if (dev != kgdb_dev)
    197   1.1      gwr 		return (0);
    198   1.1      gwr 
    199   1.1      gwr 	/*
    200   1.1      gwr 	 * Yes, this is port in use by kgdb.
    201   1.1      gwr 	 */
    202   1.1      gwr 	cs->cs_private = NULL;
    203   1.1      gwr 	cs->cs_ops = &zsops_kgdb;
    204   1.1      gwr 
    205   1.1      gwr 	/* Now set parameters. (interrupts enabled) */
    206   1.1      gwr 	zs_setparam(cs, 1, kgdb_rate);
    207   1.1      gwr 
    208   1.1      gwr 	return (1);
    209   1.1      gwr }
    210   1.1      gwr 
    211   1.1      gwr /*
    212   1.1      gwr  * KGDB framing character received: enter kernel debugger.  This probably
    213   1.1      gwr  * should time out after a few seconds to avoid hanging on spurious input.
    214   1.1      gwr  */
    215   1.1      gwr void
    216   1.1      gwr zskgdb(cs)
    217   1.1      gwr 	struct zs_chanstate *cs;
    218   1.1      gwr {
    219   1.1      gwr 	int unit = minor(kgdb_dev);
    220   1.1      gwr 
    221   1.1      gwr 	printf("zstty%d: kgdb interrupt\n", unit);
    222   1.1      gwr 	/* This will trap into the debugger. */
    223   1.1      gwr 	kgdb_connect(1);
    224   1.1      gwr }
    225   1.1      gwr 
    226   1.1      gwr 
    227   1.1      gwr /****************************************************************
    228   1.1      gwr  * Interface to the lower layer (zscc)
    229   1.1      gwr  ****************************************************************/
    230   1.1      gwr 
    231   1.1      gwr static void zs_kgdb_rxint __P((struct zs_chanstate *));
    232   1.3  mycroft static void zs_kgdb_stint __P((struct zs_chanstate *, int));
    233   1.1      gwr static void zs_kgdb_txint __P((struct zs_chanstate *));
    234   1.1      gwr static void zs_kgdb_softint __P((struct zs_chanstate *));
    235   1.1      gwr 
    236   1.1      gwr int kgdb_input_lost;
    237   1.1      gwr 
    238   1.1      gwr static void
    239   1.1      gwr zs_kgdb_rxint(cs)
    240   1.1      gwr 	struct zs_chanstate *cs;
    241   1.1      gwr {
    242   1.1      gwr 	register u_char c, rr1;
    243   1.1      gwr 
    244   1.1      gwr 	/*
    245   1.1      gwr 	 * First read the status, because reading the received char
    246   1.1      gwr 	 * destroys the status of this char.
    247   1.1      gwr 	 */
    248   1.1      gwr 	rr1 = zs_read_reg(cs, 1);
    249   1.1      gwr 	c = zs_read_data(cs);
    250   1.1      gwr 
    251   1.1      gwr 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    252   1.1      gwr 		/* Clear the receive error. */
    253   1.1      gwr 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    254   1.1      gwr 	}
    255   1.1      gwr 
    256   1.1      gwr 	if (c == KGDB_START) {
    257   1.1      gwr 		zskgdb(cs);
    258   1.1      gwr 	} else {
    259   1.1      gwr 		kgdb_input_lost++;
    260   1.1      gwr 	}
    261   1.1      gwr }
    262   1.1      gwr 
    263   1.1      gwr static void
    264   1.1      gwr zs_kgdb_txint(cs)
    265   1.1      gwr 	register struct zs_chanstate *cs;
    266   1.1      gwr {
    267   1.1      gwr 	register int rr0;
    268   1.1      gwr 
    269   1.1      gwr 	rr0 = zs_read_csr(cs);
    270   1.1      gwr 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    271   1.1      gwr }
    272   1.1      gwr 
    273   1.1      gwr static void
    274   1.3  mycroft zs_kgdb_stint(cs, force)
    275   1.1      gwr 	register struct zs_chanstate *cs;
    276   1.3  mycroft 	int force;
    277   1.1      gwr {
    278   1.1      gwr 	register int rr0;
    279   1.1      gwr 
    280   1.1      gwr 	rr0 = zs_read_csr(cs);
    281   1.1      gwr 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    282   1.1      gwr 
    283   1.1      gwr 	/*
    284   1.1      gwr 	 * Check here for console break, so that we can abort
    285   1.1      gwr 	 * even when interrupts are locking up the machine.
    286   1.1      gwr 	 */
    287   1.1      gwr 	if (rr0 & ZSRR0_BREAK) {
    288   1.1      gwr 		zskgdb(cs);
    289   1.1      gwr 	}
    290   1.1      gwr }
    291   1.1      gwr 
    292   1.1      gwr static void
    293   1.1      gwr zs_kgdb_softint(cs)
    294   1.1      gwr 	struct zs_chanstate *cs;
    295   1.1      gwr {
    296   1.1      gwr 	printf("zs_kgdb_softint?\n");
    297   1.1      gwr }
    298   1.1      gwr 
    299   1.1      gwr struct zsops zsops_kgdb = {
    300   1.1      gwr 	zs_kgdb_rxint,	/* receive char available */
    301   1.1      gwr 	zs_kgdb_stint,	/* external/status */
    302   1.1      gwr 	zs_kgdb_txint,	/* xmit buffer empty */
    303   1.1      gwr 	zs_kgdb_softint,	/* process software interrupt */
    304   1.1      gwr };
    305   1.5       pk 
    306   1.5       pk /*
    307   1.5       pk  * findzs() should return the address of the given zs channel.
    308   1.5       pk  * Here we count on the PROM to map in the required zs chips.
    309   1.5       pk  */
    310   1.5       pk void *
    311   1.5       pk findzs(zs)
    312   1.5       pk 	int zs;
    313   1.5       pk {
    314   1.5       pk 
    315   1.5       pk #if defined(SUN4)
    316   1.5       pk 	if (CPU_ISSUN4) {
    317   1.5       pk 		/*
    318   1.5       pk 		 * On sun4, we use hard-coded physical addresses
    319   1.5       pk 		 */
    320   1.5       pk #define ZS0_PHYS	0xf1000000
    321   1.5       pk #define ZS1_PHYS	0xf0000000
    322   1.5       pk #define ZS2_PHYS	0xe0000000
    323   1.5       pk 		bus_space_handle_t bh;
    324   1.5       pk 		bus_addr_t paddr;
    325   1.5       pk 
    326   1.5       pk 		switch (zs) {
    327   1.5       pk 		case 0:
    328   1.5       pk 			paddr = ZS0_PHYS;
    329   1.5       pk 			break;
    330   1.5       pk 		case 1:
    331   1.5       pk 			paddr = ZS1_PHYS;
    332   1.5       pk 			break;
    333   1.5       pk 		case 2:
    334   1.5       pk 			paddr = ZS2_PHYS;
    335   1.5       pk 			break;
    336   1.5       pk 		default:
    337   1.5       pk 			return (NULL);
    338   1.5       pk 		}
    339   1.5       pk 
    340   1.5       pk 		if (cpuinfo.cpu_type == CPUTYP_4_100)
    341   1.5       pk 			/* Clear top bits of physical address on 4/100 */
    342   1.5       pk 			paddr &= ~0xf0000000;
    343   1.5       pk 
    344   1.5       pk 		/*
    345   1.5       pk 		 * Have the obio module figure out which virtual
    346   1.5       pk 		 * address the device is mapped to.
    347   1.5       pk 		 */
    348  1.11  thorpej 		if (obio_find_rom_map(paddr, PMAP_OBIO, PAGE_SIZE, &bh) != 0)
    349   1.5       pk 			return (NULL);
    350   1.5       pk 
    351   1.5       pk 		return ((void *)bh);
    352   1.5       pk 	}
    353   1.5       pk #endif
    354   1.5       pk 
    355   1.5       pk #if defined(SUN4C) || defined(SUN4M)
    356   1.9  thorpej 	if (CPU_ISSUN4C || CPU_ISSUN4M) {
    357   1.5       pk 		int node;
    358   1.5       pk 
    359   1.5       pk 		node = firstchild(findroot());
    360   1.5       pk 		if (CPU_ISSUN4M) {
    361   1.5       pk 			/*
    362   1.5       pk 			 * On sun4m machines zs is in "obio" tree.
    363   1.5       pk 			 */
    364   1.5       pk 			node = findnode(node, "obio");
    365   1.5       pk 			if (node == 0)
    366   1.5       pk 				panic("findzs: no obio node");
    367   1.5       pk 			node = firstchild(node);
    368   1.5       pk 		}
    369   1.5       pk 		while ((node = findnode(node, "zs")) != 0) {
    370   1.5       pk 			int nvaddrs, *vaddrs, vstore[10];
    371   1.5       pk 
    372   1.8      eeh 			if (PROM_getpropint(node, "slave", -1) != zs) {
    373   1.5       pk 				node = nextsibling(node);
    374   1.5       pk 				continue;
    375   1.5       pk 			}
    376   1.5       pk 
    377   1.5       pk 			/*
    378   1.5       pk 			 * On some machines (e.g. the Voyager), the zs
    379   1.5       pk 			 * device has multi-valued register properties.
    380   1.5       pk 			 */
    381   1.5       pk 			vaddrs = vstore;
    382   1.5       pk 			nvaddrs = sizeof(vstore)/sizeof(vstore[0]);
    383   1.8      eeh 			if (PROM_getprop(node, "address", sizeof(int),
    384   1.5       pk 				    &nvaddrs, (void **)&vaddrs) != 0)
    385   1.5       pk 				return (NULL);
    386   1.5       pk 
    387   1.5       pk 			return ((void *)vaddrs[0]);
    388   1.5       pk 		}
    389   1.5       pk 	}
    390   1.5       pk #endif
    391   1.5       pk 	return (NULL);
    392   1.5       pk }
    393