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