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