Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.9
      1 /*	$NetBSD: zs_kgdb.c,v 1.9 1996/11/20 18:57:04 gwr 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 REGENTS OR CONTRIBUTORS BE
     30  * 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 vi the z8530 driver
     41  * XXX - not tested yet...
     42  *
     43  * To use this, build a kernel with: option KGDB, and
     44  * boot that kernel with "-d".  (The kernel will call
     45  * zs_kgdb_init, kgdb_connect.)  When the console prints
     46  * "kgdb waiting..." you run "gdb -k kernel" and then
     47  * connect to the remote using: "target remote /dev/ttyX"
     48  */
     49 
     50 #include <sys/param.h>
     51 #include <sys/systm.h>
     52 #include <sys/proc.h>
     53 #include <sys/device.h>
     54 #include <sys/conf.h>
     55 #include <sys/ioctl.h>
     56 #include <sys/kernel.h>
     57 #include <sys/syslog.h>
     58 
     59 #include <dev/ic/z8530reg.h>
     60 #include <machine/z8530var.h>
     61 
     62 #include <machine/remote-sl.h>
     63 
     64 /* The Sun3 provides a 4.9152 MHz clock to the ZS chips. */
     65 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     66 #define ZSHARD_PRI	6	/* Wired on the CPU board... */
     67 
     68 #define ZS_DELAY()			delay(2)
     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 
     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 /*
    108  * This replaces "zs_reset()" in the sparc driver.
    109  */
    110 static void
    111 zs_setparam(cs, iena, rate)
    112 	struct zs_chanstate *cs;
    113 	int iena;
    114 	int rate;
    115 {
    116 	int s, tconst;
    117 
    118 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
    119 
    120 	if (iena == 0) {
    121 		cs->cs_preg[1] = 0;
    122 	}
    123 
    124 	/* Initialize the speed, etc. */
    125 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    126 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    127 	cs->cs_preg[12] = tconst;
    128 	cs->cs_preg[13] = tconst >> 8;
    129 
    130 	s = splhigh();
    131 	zs_loadchannelregs(cs);
    132 	splx(s);
    133 }
    134 
    135 /*
    136  * Set up for kgdb; called at boot time before configuration.
    137  * KGDB interrupts will be enabled later when zs0 is configured.
    138  */
    139 void
    140 zs_kgdb_init()
    141 {
    142 	struct zs_chanstate cs;
    143 	volatile struct zschan *zc;
    144 	int channel, zsc_unit;
    145 
    146 	if (major(kgdb_dev) != ZSTTY_MAJOR)
    147 		return;
    148 
    149 	/* Note: (ttya,ttyb) on zsc1, and (ttyc,ttyd) on zsc0 */
    150 	zsc_unit = 2 - (kgdb_dev & 2);
    151 	channel  =      kgdb_dev & 1;
    152 	printf("zs_kgdb_init: attaching zstty%d at %d baud\n",
    153 		   channel, kgdb_rate);
    154 
    155 	/* Setup temporary chanstate. */
    156 	bzero((caddr_t)&cs, sizeof(cs));
    157 	zc = zs_get_chan_addr(zsc_unit, channel);
    158 	cs.cs_reg_csr  = &zc->zc_csr;
    159 	cs.cs_reg_data = &zc->zc_data;
    160 	cs.cs_channel = channel;
    161 	cs.cs_brg_clk = PCLK / 16;
    162 
    163 	/* Now set parameters. (interrupts disabled) */
    164 	zs_setparam(&cs, 0, kgdb_rate);
    165 
    166 	/* Store the getc/putc functions and arg. */
    167 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
    168 }
    169 
    170 /*
    171  * This is a "hook" called by zstty_attach to allow the tty
    172  * to be "taken over" for exclusive use by kgdb.
    173  * Return non-zero if this is the kgdb port.
    174  *
    175  * Set the speed to kgdb_rate, CS8, etc.
    176  */
    177 int
    178 zs_check_kgdb(cs, dev)
    179 	struct zs_chanstate *cs;
    180 	int dev;
    181 {
    182 	int tconst;
    183 
    184 	if (dev != kgdb_dev)
    185 		return (0);
    186 
    187 	/*
    188 	 * Yes, this is the kgdb port.  Finish the autoconfig
    189 	 * message and set up the port for our exclusive use.
    190 	 */
    191 	printf(" (kgdb)\n");
    192 
    193 	cs->cs_private = NULL;
    194 	cs->cs_ops = &zsops_kgdb;
    195 
    196 	/* Now set parameters. (interrupts enabled) */
    197 	zs_setparam(&cs, 1, kgdb_rate);
    198 
    199 	return (1);
    200 }
    201 
    202 /*
    203  * KGDB framing character received: enter kernel debugger.  This probably
    204  * should time out after a few seconds to avoid hanging on spurious input.
    205  */
    206 zskgdb()
    207 {
    208 	int unit = minor(kgdb_dev);
    209 
    210 	printf("zstty%d: kgdb interrupt\n", unit);
    211 	/* This will trap into the debugger. */
    212 	kgdb_connect(1);
    213 }
    214 
    215 
    216 /****************************************************************
    217  * Interface to the lower layer (zscc)
    218  ****************************************************************/
    219 
    220 int kgdb_input_lost;
    221 
    222 static void
    223 zs_kgdb_rxint(cs)
    224 	register struct zs_chanstate *cs;
    225 {
    226 	register u_char c, rr1;
    227 
    228 	/* Read the input data ASAP. */
    229 	c = zs_read_data(cs);
    230 
    231 	/* Save the status register too. */
    232 	rr1 = zs_read_reg(cs, 1);
    233 
    234 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    235 		/* Clear the receive error. */
    236 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    237 	}
    238 
    239 	if (c == FRAME_START) {
    240 		zskgdb();
    241 	} else {
    242 		kgdb_input_lost++;
    243 	}
    244 }
    245 
    246 static void
    247 zs_kgdb_txint(cs)
    248 	register struct zs_chanstate *cs;
    249 {
    250 	register int rr0;
    251 
    252 	rr0 = zs_read_csr(cs);
    253 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    254 }
    255 
    256 static void
    257 zs_kgdb_stint(cs)
    258 	register struct zs_chanstate *cs;
    259 {
    260 	register int rr0;
    261 
    262 	rr0 = zs_read_csr(cs);
    263 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    264 }
    265 
    266 static void
    267 zs_kgdb_softint(cs)
    268 	struct zs_chanstate *cs;
    269 {
    270 	printf("zs_kgdb_softint?\n");
    271 }
    272 
    273 struct zsops zsops_kgdb = {
    274 	zs_kgdb_rxint,	/* receive char available */
    275 	zs_kgdb_stint,	/* external/status */
    276 	zs_kgdb_txint,	/* xmit buffer empty */
    277 	zs_kgdb_softint,	/* process software interrupt */
    278 };
    279