Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.7
      1 /*	$NetBSD: zs_kgdb.c,v 1.7 2004/02/08 13:15:42 sekiya 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 and Wayne Knowles.
      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 <sys/cdefs.h>
     51 __KERNEL_RCSID(0, "$NetBSD: zs_kgdb.c,v 1.7 2004/02/08 13:15:42 sekiya Exp $");
     52 
     53 #include <sys/param.h>
     54 #include <sys/systm.h>
     55 #include <sys/proc.h>
     56 #include <sys/device.h>
     57 #include <sys/conf.h>
     58 #include <sys/ioctl.h>
     59 #include <sys/kernel.h>
     60 #include <sys/syslog.h>
     61 #include <sys/kgdb.h>
     62 
     63 #include <dev/ic/z8530reg.h>
     64 #include <machine/z8530var.h>
     65 
     66 /*
     67  * Hooks for kgdb when attached via the z8530 driver
     68  *
     69  * To use this, build a kernel with: option KGDB, and
     70  * boot that kernel with "-d".  (The kernel will call
     71  * zs_kgdb_init, kgdb_connect.)  When the console prints
     72  * "kgdb waiting..." you run "gdb -k kernel" and do:
     73  *   (gdb) set remotebaud 19200
     74  *   (gdb) target remote /dev/ttyb
     75  */
     76 
     77 void zs_kgdb_init (void);
     78 void zskgdb (struct zs_chanstate *);
     79 
     80 static void	zs_setparam (struct zs_chanstate *, int, int);
     81 static struct	zsops zsops_kgdb;
     82 
     83 extern struct	zschan *zs_get_chan_addr (int, int);
     84 extern int	zs_getc (void *);
     85 extern void	zs_putc (void *, int);
     86 
     87 static u_char zs_kgdb_regs[16] = {
     88 	0,	/* 0: CMD (reset, etc.) */
     89 	0,	/* 1: No interrupts yet. */
     90 	0,	/* 2: IVECT */
     91 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     92 	ZSWR4_CLK_X16 | ZSWR4_ONESB,
     93 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     94 	0,	/* 6: TXSYNC/SYNCLO */
     95 	0,	/* 7: RXSYNC/SYNCHI */
     96 	0,	/* 8: alias for data port */
     97 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
     98 	0,	/*10: Misc. TX/RX control bits */
     99 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD | ZSWR11_TRXC_OUT_ENA,
    100 	10,	/*12: BAUDLO (updated by KGDB_RATE) */
    101 	0,	/*13: BAUDHI (updated by KGDB_RATE) */
    102 	ZSWR14_BAUD_ENA,
    103 	ZSWR15_BREAK_IE,
    104 };
    105 
    106 static void
    107 zs_setparam(cs, iena, rate)
    108 	struct zs_chanstate *cs;
    109 	int iena;
    110 	int rate;
    111 {
    112 	int s, tconst;
    113 
    114 	memcpy(cs->cs_preg, zs_kgdb_regs, 16);
    115 
    116 	if (iena) {
    117 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    118 	}
    119 
    120 	/* Initialize the speed, etc. */
    121 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    122 
    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  * Called after cninit(), so printf() etc. works.
    136  */
    137 void
    138 zs_kgdb_init()
    139 {
    140 	volatile struct zschan *zc;
    141 	int channel, unit;
    142 	extern const struct cdevsw zstty_cdevsw;
    143 
    144 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
    145 		return;
    146 
    147 	unit = (kgdb_dev & 2) ? 2 : 0;
    148 	channel = kgdb_dev & 1;
    149 	printf("zs_kgdb_init: attaching to Serial(%d) at %d baud\n",
    150 		   (kgdb_dev & 3), kgdb_rate);
    151 
    152 	zc = zs_get_chan_addr(unit, channel);
    153 
    154 	/* Attach KGDB comms functions to this device */
    155 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
    156 }
    157 
    158 /*
    159  * This is a "hook" called by the MI zstty_attach driver
    160  * to allow the tty to be "taken over" for exclusive use by kgdb.
    161  * Return non-zero if this is the kgdb port.
    162  *
    163  * Set the speed to kgdb_rate, CS8, etc.
    164  */
    165 int
    166 zs_check_kgdb(cs, dev)
    167 	struct zs_chanstate *cs;
    168 	int dev;
    169 {
    170 
    171 	if (dev != kgdb_dev)
    172 		return (0);
    173 
    174 	/*
    175 	 * Yes, this is port in use by kgdb.
    176 	 */
    177 	cs->cs_private = NULL;
    178 	cs->cs_ops = &zsops_kgdb;
    179 
    180 	/* Now set parameters. (interrupts enabled) */
    181 	zs_setparam(cs, 1, kgdb_rate);
    182 
    183 	return (1);
    184 }
    185 
    186 /*
    187  * KGDB framing character received: enter kernel debugger.  This probably
    188  * should time out after a few seconds to avoid hanging on spurious input.
    189  */
    190 void
    191 zskgdb(cs)
    192 	struct zs_chanstate *cs;
    193 {
    194 	int unit = minor(kgdb_dev);
    195 
    196 	printf("zstty%d: kgdb interrupt\n", unit);
    197 	/* This will trap into the debugger. */
    198 	kgdb_connect(1);
    199 }
    200 
    201 
    202 /****************************************************************
    203  * Interface to the lower layer (zscc)
    204  ****************************************************************/
    205 
    206 static void zs_kgdb_rxint (struct zs_chanstate *);
    207 static void zs_kgdb_stint (struct zs_chanstate *, int);
    208 static void zs_kgdb_txint (struct zs_chanstate *);
    209 static void zs_kgdb_softint (struct zs_chanstate *);
    210 
    211 static struct zsops zsops_kgdb = {
    212 	zs_kgdb_rxint,		/* receive char available */
    213 	zs_kgdb_stint,		/* external/status */
    214 	zs_kgdb_txint,		/* xmit buffer empty */
    215 	zs_kgdb_softint,	/* process software interrupt */
    216 };
    217 
    218 int kgdb_input_lost;
    219 
    220 static void
    221 zs_kgdb_rxint(cs)
    222 	struct zs_chanstate *cs;
    223 {
    224 	register u_char c, rr1;
    225 
    226 	/*
    227 	 * First read the status, because reading the received char
    228 	 * destroys the status of this char.
    229 	 */
    230 	rr1 = zs_read_reg(cs, 1);
    231 	c = zs_read_data(cs);
    232 
    233 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    234 		/* Clear the receive error. */
    235 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    236 	}
    237 
    238 	if (c == KGDB_START) {
    239 		zskgdb(cs);
    240 	} else {
    241 		kgdb_input_lost++;
    242 	}
    243 }
    244 
    245 static void
    246 zs_kgdb_txint(cs)
    247 	register struct zs_chanstate *cs;
    248 {
    249 	register int rr0;
    250 
    251 	rr0 = zs_read_csr(cs);
    252 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    253 }
    254 
    255 static void
    256 zs_kgdb_stint(cs, force)
    257 	register struct zs_chanstate *cs;
    258 	int force;
    259 {
    260 	register int rr0;
    261 
    262 	rr0 = zs_read_csr(cs);
    263 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    264 
    265 	/*
    266 	 * Check here for console break, so that we can abort
    267 	 * even when interrupts are locking up the machine.
    268 	 */
    269 	if (rr0 & ZSRR0_BREAK) {
    270 		zskgdb(cs);
    271 	}
    272 }
    273 
    274 static void
    275 zs_kgdb_softint(cs)
    276 	struct zs_chanstate *cs;
    277 {
    278 	printf("zs_kgdb_softint?\n");
    279 }
    280