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