Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.15.6.2
      1  1.15.6.2  nathanw /*	$NetBSD: zs_kgdb.c,v 1.15.6.2 2002/09/17 21:18:19 nathanw Exp $	*/
      2  1.15.6.2  nathanw 
      3  1.15.6.2  nathanw /*-
      4  1.15.6.2  nathanw  * Copyright (c) 1996 The NetBSD Foundation, Inc.
      5  1.15.6.2  nathanw  * All rights reserved.
      6  1.15.6.2  nathanw  *
      7  1.15.6.2  nathanw  * This code is derived from software contributed to The NetBSD Foundation
      8  1.15.6.2  nathanw  * by Gordon W. Ross.
      9  1.15.6.2  nathanw  *
     10  1.15.6.2  nathanw  * Redistribution and use in source and binary forms, with or without
     11  1.15.6.2  nathanw  * modification, are permitted provided that the following conditions
     12  1.15.6.2  nathanw  * are met:
     13  1.15.6.2  nathanw  * 1. Redistributions of source code must retain the above copyright
     14  1.15.6.2  nathanw  *    notice, this list of conditions and the following disclaimer.
     15  1.15.6.2  nathanw  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.15.6.2  nathanw  *    notice, this list of conditions and the following disclaimer in the
     17  1.15.6.2  nathanw  *    documentation and/or other materials provided with the distribution.
     18  1.15.6.2  nathanw  * 3. All advertising materials mentioning features or use of this software
     19  1.15.6.2  nathanw  *    must display the following acknowledgement:
     20  1.15.6.2  nathanw  *        This product includes software developed by the NetBSD
     21  1.15.6.2  nathanw  *        Foundation, Inc. and its contributors.
     22  1.15.6.2  nathanw  * 4. Neither the name of The NetBSD Foundation nor the names of its
     23  1.15.6.2  nathanw  *    contributors may be used to endorse or promote products derived
     24  1.15.6.2  nathanw  *    from this software without specific prior written permission.
     25  1.15.6.2  nathanw  *
     26  1.15.6.2  nathanw  * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
     27  1.15.6.2  nathanw  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
     28  1.15.6.2  nathanw  * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
     29  1.15.6.2  nathanw  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
     30  1.15.6.2  nathanw  * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
     31  1.15.6.2  nathanw  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
     32  1.15.6.2  nathanw  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
     33  1.15.6.2  nathanw  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
     34  1.15.6.2  nathanw  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
     35  1.15.6.2  nathanw  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
     36  1.15.6.2  nathanw  * POSSIBILITY OF SUCH DAMAGE.
     37  1.15.6.2  nathanw  */
     38  1.15.6.2  nathanw 
     39  1.15.6.2  nathanw /*
     40  1.15.6.2  nathanw  * Hooks for kgdb when attached via the z8530 driver
     41  1.15.6.2  nathanw  *
     42  1.15.6.2  nathanw  * To use this, build a kernel with: option KGDB, and
     43  1.15.6.2  nathanw  * boot that kernel with "-d".  (The kernel will call
     44  1.15.6.2  nathanw  * zs_kgdb_init, kgdb_connect.)  When the console prints
     45  1.15.6.2  nathanw  * "kgdb waiting..." you run "gdb -k kernel" and do:
     46  1.15.6.2  nathanw  *   (gdb) set remotebaud 19200
     47  1.15.6.2  nathanw  *   (gdb) target remote /dev/ttyb
     48  1.15.6.2  nathanw  */
     49  1.15.6.2  nathanw 
     50  1.15.6.2  nathanw #include <sys/param.h>
     51  1.15.6.2  nathanw #include <sys/systm.h>
     52  1.15.6.2  nathanw #include <sys/proc.h>
     53  1.15.6.2  nathanw #include <sys/device.h>
     54  1.15.6.2  nathanw #include <sys/conf.h>
     55  1.15.6.2  nathanw #include <sys/ioctl.h>
     56  1.15.6.2  nathanw #include <sys/kernel.h>
     57  1.15.6.2  nathanw #include <sys/syslog.h>
     58  1.15.6.2  nathanw #include <sys/kgdb.h>
     59  1.15.6.2  nathanw 
     60  1.15.6.2  nathanw #include <dev/ic/z8530reg.h>
     61  1.15.6.2  nathanw #include <machine/z8530var.h>
     62  1.15.6.2  nathanw #include <sun3/dev/zs_cons.h>
     63  1.15.6.2  nathanw 
     64  1.15.6.2  nathanw /* The Sun3 provides a 4.9152 MHz clock to the ZS chips. */
     65  1.15.6.2  nathanw #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     66  1.15.6.2  nathanw #define ZSHARD_PRI	6	/* Wired on the CPU board... */
     67  1.15.6.2  nathanw 
     68  1.15.6.2  nathanw #define ZS_DELAY()			delay(2)
     69  1.15.6.2  nathanw 
     70  1.15.6.2  nathanw /* The layout of this is hardware-dependent (padding, order). */
     71  1.15.6.2  nathanw struct zschan {
     72  1.15.6.2  nathanw 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     73  1.15.6.2  nathanw 	u_char		zc_xxx0;
     74  1.15.6.2  nathanw 	volatile u_char	zc_data;	/* data */
     75  1.15.6.2  nathanw 	u_char		zc_xxx1;
     76  1.15.6.2  nathanw };
     77  1.15.6.2  nathanw 
     78  1.15.6.2  nathanw static void zs_setparam __P((struct zs_chanstate *, int, int));
     79  1.15.6.2  nathanw static void zskgdb __P((struct zs_chanstate *));
     80  1.15.6.2  nathanw 
     81  1.15.6.2  nathanw struct zsops zsops_kgdb;
     82  1.15.6.2  nathanw 
     83  1.15.6.2  nathanw static u_char zs_kgdb_regs[16] = {
     84  1.15.6.2  nathanw 	0,	/* 0: CMD (reset, etc.) */
     85  1.15.6.2  nathanw 	0,	/* 1: ~(ZSWR1_RIE | ZSWR1_TIE | ZSWR1_SIE) */
     86  1.15.6.2  nathanw 	0x18 + ZSHARD_PRI,	/* IVECT */
     87  1.15.6.2  nathanw 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     88  1.15.6.2  nathanw 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     89  1.15.6.2  nathanw 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     90  1.15.6.2  nathanw 	0,	/* 6: TXSYNC/SYNCLO */
     91  1.15.6.2  nathanw 	0,	/* 7: RXSYNC/SYNCHI */
     92  1.15.6.2  nathanw 	0,	/* 8: alias for data port */
     93  1.15.6.2  nathanw 	ZSWR9_MASTER_IE,
     94  1.15.6.2  nathanw 	0,	/*10: Misc. TX/RX control bits */
     95  1.15.6.2  nathanw 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
     96  1.15.6.2  nathanw 	14,	/*12: BAUDLO (default=9600) */
     97  1.15.6.2  nathanw 	0,	/*13: BAUDHI (default=9600) */
     98  1.15.6.2  nathanw 	ZSWR14_BAUD_FROM_PCLK | ZSWR14_BAUD_ENA,
     99  1.15.6.2  nathanw 	ZSWR15_BREAK_IE,
    100  1.15.6.2  nathanw };
    101  1.15.6.2  nathanw 
    102  1.15.6.2  nathanw /*
    103  1.15.6.2  nathanw  * This replaces "zs_reset()" in the sparc driver.
    104  1.15.6.2  nathanw  */
    105  1.15.6.2  nathanw static void
    106  1.15.6.2  nathanw zs_setparam(cs, iena, rate)
    107  1.15.6.2  nathanw 	struct zs_chanstate *cs;
    108  1.15.6.2  nathanw 	int iena;
    109  1.15.6.2  nathanw 	int rate;
    110  1.15.6.2  nathanw {
    111  1.15.6.2  nathanw 	int s, tconst;
    112  1.15.6.2  nathanw 
    113  1.15.6.2  nathanw 	memcpy(cs->cs_preg, zs_kgdb_regs, 16);
    114  1.15.6.2  nathanw 
    115  1.15.6.2  nathanw 	if (iena) {
    116  1.15.6.2  nathanw 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    117  1.15.6.2  nathanw 	}
    118  1.15.6.2  nathanw 
    119  1.15.6.2  nathanw 	/* Initialize the speed, etc. */
    120  1.15.6.2  nathanw 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    121  1.15.6.2  nathanw 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    122  1.15.6.2  nathanw 	cs->cs_preg[12] = tconst;
    123  1.15.6.2  nathanw 	cs->cs_preg[13] = tconst >> 8;
    124  1.15.6.2  nathanw 
    125  1.15.6.2  nathanw 	s = splhigh();
    126  1.15.6.2  nathanw 	zs_loadchannelregs(cs);
    127  1.15.6.2  nathanw 	splx(s);
    128  1.15.6.2  nathanw }
    129  1.15.6.2  nathanw 
    130  1.15.6.2  nathanw /*
    131  1.15.6.2  nathanw  * Set up for kgdb; called at boot time before configuration.
    132  1.15.6.2  nathanw  * KGDB interrupts will be enabled later when zs0 is configured.
    133  1.15.6.2  nathanw  * Called after cninit(), so printf() etc. works.
    134  1.15.6.2  nathanw  */
    135  1.15.6.2  nathanw void
    136  1.15.6.2  nathanw zs_kgdb_init()
    137  1.15.6.2  nathanw {
    138  1.15.6.2  nathanw 	struct zs_chanstate cs;
    139  1.15.6.2  nathanw 	volatile struct zschan *zc;
    140  1.15.6.2  nathanw 	int channel, zsc_unit;
    141  1.15.6.2  nathanw 	extern const struct cdevsw zstty_cdevsw;
    142  1.15.6.2  nathanw 
    143  1.15.6.2  nathanw 	/* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
    144  1.15.6.2  nathanw 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
    145  1.15.6.2  nathanw 		return;
    146  1.15.6.2  nathanw 
    147  1.15.6.2  nathanw 	/* Note: (ttya,ttyb) on zsc1, and (ttyc,ttyd) on zsc0 */
    148  1.15.6.2  nathanw 	zsc_unit = (kgdb_dev & 2) ? 0 : 1;
    149  1.15.6.2  nathanw 	channel  =  kgdb_dev & 1;
    150  1.15.6.2  nathanw 	printf("zs_kgdb_init: attaching tty%c at %d baud\n",
    151  1.15.6.2  nathanw 		   'a' + (kgdb_dev & 3), kgdb_rate);
    152  1.15.6.2  nathanw 
    153  1.15.6.2  nathanw 	/* Setup temporary chanstate. */
    154  1.15.6.2  nathanw 	memset((caddr_t)&cs, 0, sizeof(cs));
    155  1.15.6.2  nathanw 	zc = zs_get_chan_addr(zsc_unit, channel);
    156  1.15.6.2  nathanw 	if (zc == NULL) {
    157  1.15.6.2  nathanw 		printf("zs_kgdb_init: zs not mapped.\n");
    158  1.15.6.2  nathanw 		kgdb_dev = -1;
    159  1.15.6.2  nathanw 		return;
    160  1.15.6.2  nathanw 	}
    161  1.15.6.2  nathanw 
    162  1.15.6.2  nathanw 	cs.cs_channel = channel;
    163  1.15.6.2  nathanw 	cs.cs_brg_clk = PCLK / 16;
    164  1.15.6.2  nathanw 	cs.cs_reg_csr  = &zc->zc_csr;
    165  1.15.6.2  nathanw 	cs.cs_reg_data = &zc->zc_data;
    166  1.15.6.2  nathanw 
    167  1.15.6.2  nathanw 	/* Now set parameters. (interrupts disabled) */
    168  1.15.6.2  nathanw 	zs_setparam(&cs, 0, kgdb_rate);
    169  1.15.6.2  nathanw 
    170  1.15.6.2  nathanw 	/* Store the getc/putc functions and arg. */
    171  1.15.6.2  nathanw 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
    172  1.15.6.2  nathanw }
    173  1.15.6.2  nathanw 
    174  1.15.6.2  nathanw /*
    175  1.15.6.2  nathanw  * This is a "hook" called by zstty_attach to allow the tty
    176  1.15.6.2  nathanw  * to be "taken over" for exclusive use by kgdb.
    177  1.15.6.2  nathanw  * Return non-zero if this is the kgdb port.
    178  1.15.6.2  nathanw  *
    179  1.15.6.2  nathanw  * Set the speed to kgdb_rate, CS8, etc.
    180  1.15.6.2  nathanw  */
    181  1.15.6.2  nathanw int
    182  1.15.6.2  nathanw zs_check_kgdb(cs, dev)
    183  1.15.6.2  nathanw 	struct zs_chanstate *cs;
    184  1.15.6.2  nathanw 	int dev;
    185  1.15.6.2  nathanw {
    186  1.15.6.2  nathanw 
    187  1.15.6.2  nathanw 	if (dev != kgdb_dev)
    188  1.15.6.2  nathanw 		return (0);
    189  1.15.6.2  nathanw 
    190  1.15.6.2  nathanw 	/*
    191  1.15.6.2  nathanw 	 * Yes, this is port in use by kgdb.
    192  1.15.6.2  nathanw 	 */
    193  1.15.6.2  nathanw 	cs->cs_private = NULL;
    194  1.15.6.2  nathanw 	cs->cs_ops = &zsops_kgdb;
    195  1.15.6.2  nathanw 
    196  1.15.6.2  nathanw 	/* Now set parameters. (interrupts enabled) */
    197  1.15.6.2  nathanw 	zs_setparam(cs, 1, kgdb_rate);
    198  1.15.6.2  nathanw 
    199  1.15.6.2  nathanw 	return (1);
    200  1.15.6.2  nathanw }
    201  1.15.6.2  nathanw 
    202  1.15.6.2  nathanw /*
    203  1.15.6.2  nathanw  * KGDB framing character received: enter kernel debugger.  This probably
    204  1.15.6.2  nathanw  * should time out after a few seconds to avoid hanging on spurious input.
    205  1.15.6.2  nathanw  */
    206  1.15.6.2  nathanw static void
    207  1.15.6.2  nathanw zskgdb(cs)
    208  1.15.6.2  nathanw 	struct zs_chanstate *cs;
    209  1.15.6.2  nathanw {
    210  1.15.6.2  nathanw 	int unit = minor(kgdb_dev);
    211  1.15.6.2  nathanw 
    212  1.15.6.2  nathanw 	printf("zstty%d: kgdb interrupt\n", unit);
    213  1.15.6.2  nathanw 	/* This will trap into the debugger. */
    214  1.15.6.2  nathanw 	kgdb_connect(1);
    215  1.15.6.2  nathanw }
    216  1.15.6.2  nathanw 
    217  1.15.6.2  nathanw 
    218  1.15.6.2  nathanw /****************************************************************
    219  1.15.6.2  nathanw  * Interface to the lower layer (zscc)
    220  1.15.6.2  nathanw  ****************************************************************/
    221  1.15.6.2  nathanw 
    222  1.15.6.2  nathanw static void zs_kgdb_rxint __P((struct zs_chanstate *));
    223  1.15.6.2  nathanw static void zs_kgdb_stint __P((struct zs_chanstate *, int));
    224  1.15.6.2  nathanw static void zs_kgdb_txint __P((struct zs_chanstate *));
    225  1.15.6.2  nathanw static void zs_kgdb_softint __P((struct zs_chanstate *));
    226  1.15.6.2  nathanw 
    227  1.15.6.2  nathanw int kgdb_input_lost;
    228  1.15.6.2  nathanw 
    229  1.15.6.2  nathanw static void
    230  1.15.6.2  nathanw zs_kgdb_rxint(cs)
    231  1.15.6.2  nathanw 	struct zs_chanstate *cs;
    232  1.15.6.2  nathanw {
    233  1.15.6.2  nathanw 	u_char c, rr1;
    234  1.15.6.2  nathanw 
    235  1.15.6.2  nathanw 	/*
    236  1.15.6.2  nathanw 	 * First read the status, because reading the received char
    237  1.15.6.2  nathanw 	 * destroys the status of this char.
    238  1.15.6.2  nathanw 	 */
    239  1.15.6.2  nathanw 	rr1 = zs_read_reg(cs, 1);
    240  1.15.6.2  nathanw 	c = zs_read_data(cs);
    241  1.15.6.2  nathanw 
    242  1.15.6.2  nathanw 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    243  1.15.6.2  nathanw 		/* Clear the receive error. */
    244  1.15.6.2  nathanw 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    245  1.15.6.2  nathanw 	}
    246  1.15.6.2  nathanw 
    247  1.15.6.2  nathanw 	if (c == KGDB_START) {
    248  1.15.6.2  nathanw 		zskgdb(cs);
    249  1.15.6.2  nathanw 	} else {
    250  1.15.6.2  nathanw 		kgdb_input_lost++;
    251  1.15.6.2  nathanw 	}
    252  1.15.6.2  nathanw }
    253  1.15.6.2  nathanw 
    254  1.15.6.2  nathanw static void
    255  1.15.6.2  nathanw zs_kgdb_txint(cs)
    256  1.15.6.2  nathanw 	struct zs_chanstate *cs;
    257  1.15.6.2  nathanw {
    258  1.15.6.2  nathanw 	int rr0;
    259  1.15.6.2  nathanw 
    260  1.15.6.2  nathanw 	rr0 = zs_read_csr(cs);
    261  1.15.6.2  nathanw 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    262  1.15.6.2  nathanw }
    263  1.15.6.2  nathanw 
    264  1.15.6.2  nathanw static void
    265  1.15.6.2  nathanw zs_kgdb_stint(cs, force)
    266  1.15.6.2  nathanw 	struct zs_chanstate *cs;
    267  1.15.6.2  nathanw 	int force;
    268  1.15.6.2  nathanw {
    269  1.15.6.2  nathanw 	int rr0;
    270  1.15.6.2  nathanw 
    271  1.15.6.2  nathanw 	rr0 = zs_read_csr(cs);
    272  1.15.6.2  nathanw 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    273  1.15.6.2  nathanw 
    274  1.15.6.2  nathanw 	/*
    275  1.15.6.2  nathanw 	 * Check here for console break, so that we can abort
    276  1.15.6.2  nathanw 	 * even when interrupts are locking up the machine.
    277  1.15.6.2  nathanw 	 */
    278  1.15.6.2  nathanw 	if (rr0 & ZSRR0_BREAK) {
    279  1.15.6.2  nathanw 		zskgdb(cs);
    280  1.15.6.2  nathanw 	}
    281  1.15.6.2  nathanw }
    282  1.15.6.2  nathanw 
    283  1.15.6.2  nathanw static void
    284  1.15.6.2  nathanw zs_kgdb_softint(cs)
    285  1.15.6.2  nathanw 	struct zs_chanstate *cs;
    286  1.15.6.2  nathanw {
    287  1.15.6.2  nathanw 	printf("zs_kgdb_softint?\n");
    288  1.15.6.2  nathanw }
    289  1.15.6.2  nathanw 
    290  1.15.6.2  nathanw struct zsops zsops_kgdb = {
    291  1.15.6.2  nathanw 	zs_kgdb_rxint,	/* receive char available */
    292  1.15.6.2  nathanw 	zs_kgdb_stint,	/* external/status */
    293  1.15.6.2  nathanw 	zs_kgdb_txint,	/* xmit buffer empty */
    294  1.15.6.2  nathanw 	zs_kgdb_softint,	/* process software interrupt */
    295  1.15.6.2  nathanw };
    296