Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.10.38.1
      1  1.10.38.1  garbled /*	$NetBSD: zs_kgdb.c,v 1.10.38.1 2007/06/26 18:13:29 garbled 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.10.38.1  garbled __KERNEL_RCSID(0, "$NetBSD: zs_kgdb.c,v 1.10.38.1 2007/06/26 18:13:29 garbled 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.9   sekiya zs_setparam(struct zs_chanstate *cs, int iena, int rate)
    108        1.1  thorpej {
    109        1.1  thorpej 	int s, tconst;
    110        1.1  thorpej 
    111        1.3  thorpej 	memcpy(cs->cs_preg, zs_kgdb_regs, 16);
    112        1.1  thorpej 
    113        1.1  thorpej 	if (iena) {
    114        1.1  thorpej 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    115        1.1  thorpej 	}
    116        1.1  thorpej 
    117        1.1  thorpej 	/* Initialize the speed, etc. */
    118        1.1  thorpej 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    119        1.2      wdk 
    120        1.1  thorpej 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    121        1.1  thorpej 	cs->cs_preg[12] = tconst;
    122        1.1  thorpej 	cs->cs_preg[13] = tconst >> 8;
    123        1.1  thorpej 
    124        1.1  thorpej 	s = splhigh();
    125        1.1  thorpej 	zs_loadchannelregs(cs);
    126        1.1  thorpej 	splx(s);
    127        1.1  thorpej }
    128        1.1  thorpej 
    129        1.1  thorpej /*
    130        1.1  thorpej  * Set up for kgdb; called at boot time before configuration.
    131        1.1  thorpej  * KGDB interrupts will be enabled later when zs0 is configured.
    132        1.1  thorpej  * Called after cninit(), so printf() etc. works.
    133        1.1  thorpej  */
    134        1.1  thorpej void
    135        1.1  thorpej zs_kgdb_init()
    136        1.1  thorpej {
    137        1.1  thorpej 	volatile struct zschan *zc;
    138        1.2      wdk 	int channel, unit;
    139        1.5  gehenna 	extern const struct cdevsw zstty_cdevsw;
    140        1.1  thorpej 
    141        1.5  gehenna 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
    142        1.1  thorpej 		return;
    143        1.1  thorpej 
    144        1.8   sekiya 	unit = (kgdb_dev & 2) ? 1 : 0;	/* XXX ??? */
    145        1.2      wdk 	channel = kgdb_dev & 1;
    146        1.2      wdk 	printf("zs_kgdb_init: attaching to Serial(%d) at %d baud\n",
    147        1.2      wdk 		   (kgdb_dev & 3), kgdb_rate);
    148        1.1  thorpej 
    149        1.2      wdk 	zc = zs_get_chan_addr(unit, channel);
    150        1.1  thorpej 
    151        1.2      wdk 	/* Attach KGDB comms functions to this device */
    152  1.10.38.1  garbled 	kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
    153        1.1  thorpej }
    154        1.1  thorpej 
    155        1.1  thorpej /*
    156        1.4   simonb  * This is a "hook" called by the MI zstty_attach driver
    157        1.2      wdk  * to allow the tty to be "taken over" for exclusive use by kgdb.
    158        1.1  thorpej  * Return non-zero if this is the kgdb port.
    159        1.1  thorpej  *
    160        1.1  thorpej  * Set the speed to kgdb_rate, CS8, etc.
    161        1.1  thorpej  */
    162        1.1  thorpej int
    163        1.9   sekiya zs_check_kgdb(struct zs_chanstate *cs, int dev)
    164        1.1  thorpej {
    165        1.1  thorpej 
    166        1.8   sekiya 	if (dev != Kgdb_dev)
    167        1.1  thorpej 		return (0);
    168        1.1  thorpej 
    169        1.1  thorpej 	/*
    170        1.1  thorpej 	 * Yes, this is port in use by kgdb.
    171        1.1  thorpej 	 */
    172        1.1  thorpej 	cs->cs_private = NULL;
    173        1.1  thorpej 	cs->cs_ops = &zsops_kgdb;
    174        1.1  thorpej 
    175        1.1  thorpej 	/* Now set parameters. (interrupts enabled) */
    176        1.1  thorpej 	zs_setparam(cs, 1, kgdb_rate);
    177        1.1  thorpej 
    178        1.1  thorpej 	return (1);
    179        1.1  thorpej }
    180        1.1  thorpej 
    181        1.1  thorpej /*
    182        1.1  thorpej  * KGDB framing character received: enter kernel debugger.  This probably
    183        1.1  thorpej  * should time out after a few seconds to avoid hanging on spurious input.
    184        1.1  thorpej  */
    185        1.1  thorpej void
    186        1.9   sekiya zskgdb(struct zs_chanstate *cs)
    187        1.1  thorpej {
    188        1.1  thorpej 	int unit = minor(kgdb_dev);
    189        1.1  thorpej 
    190        1.1  thorpej 	printf("zstty%d: kgdb interrupt\n", unit);
    191        1.1  thorpej 	/* This will trap into the debugger. */
    192        1.1  thorpej 	kgdb_connect(1);
    193        1.1  thorpej }
    194        1.1  thorpej 
    195        1.1  thorpej 
    196        1.1  thorpej /****************************************************************
    197        1.1  thorpej  * Interface to the lower layer (zscc)
    198        1.1  thorpej  ****************************************************************/
    199        1.1  thorpej 
    200        1.7   sekiya static void zs_kgdb_rxint (struct zs_chanstate *);
    201        1.7   sekiya static void zs_kgdb_stint (struct zs_chanstate *, int);
    202        1.7   sekiya static void zs_kgdb_txint (struct zs_chanstate *);
    203        1.7   sekiya static void zs_kgdb_softint (struct zs_chanstate *);
    204        1.1  thorpej 
    205        1.2      wdk static struct zsops zsops_kgdb = {
    206        1.2      wdk 	zs_kgdb_rxint,		/* receive char available */
    207        1.2      wdk 	zs_kgdb_stint,		/* external/status */
    208        1.2      wdk 	zs_kgdb_txint,		/* xmit buffer empty */
    209        1.2      wdk 	zs_kgdb_softint,	/* process software interrupt */
    210        1.2      wdk };
    211        1.2      wdk 
    212        1.1  thorpej int kgdb_input_lost;
    213        1.1  thorpej 
    214        1.1  thorpej static void
    215        1.9   sekiya zs_kgdb_rxint(struct zs_chanstate *cs)
    216        1.1  thorpej {
    217        1.1  thorpej 	register u_char c, rr1;
    218        1.1  thorpej 
    219        1.1  thorpej 	/*
    220        1.1  thorpej 	 * First read the status, because reading the received char
    221        1.1  thorpej 	 * destroys the status of this char.
    222        1.1  thorpej 	 */
    223        1.1  thorpej 	rr1 = zs_read_reg(cs, 1);
    224        1.1  thorpej 	c = zs_read_data(cs);
    225        1.1  thorpej 
    226        1.1  thorpej 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    227        1.1  thorpej 		/* Clear the receive error. */
    228        1.1  thorpej 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    229        1.1  thorpej 	}
    230        1.1  thorpej 
    231        1.1  thorpej 	if (c == KGDB_START) {
    232        1.1  thorpej 		zskgdb(cs);
    233        1.1  thorpej 	} else {
    234        1.1  thorpej 		kgdb_input_lost++;
    235        1.1  thorpej 	}
    236        1.1  thorpej }
    237        1.1  thorpej 
    238        1.1  thorpej static void
    239        1.9   sekiya zs_kgdb_txint(register struct zs_chanstate *cs)
    240        1.1  thorpej {
    241        1.1  thorpej 	register int rr0;
    242        1.1  thorpej 
    243        1.1  thorpej 	rr0 = zs_read_csr(cs);
    244        1.1  thorpej 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    245        1.1  thorpej }
    246        1.1  thorpej 
    247        1.1  thorpej static void
    248        1.9   sekiya zs_kgdb_stint(register struct zs_chanstate *cs, int force)
    249        1.1  thorpej {
    250        1.1  thorpej 	register int rr0;
    251        1.1  thorpej 
    252        1.1  thorpej 	rr0 = zs_read_csr(cs);
    253        1.1  thorpej 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    254        1.1  thorpej 
    255        1.1  thorpej 	/*
    256        1.1  thorpej 	 * Check here for console break, so that we can abort
    257        1.1  thorpej 	 * even when interrupts are locking up the machine.
    258        1.1  thorpej 	 */
    259        1.1  thorpej 	if (rr0 & ZSRR0_BREAK) {
    260        1.1  thorpej 		zskgdb(cs);
    261        1.1  thorpej 	}
    262        1.1  thorpej }
    263        1.1  thorpej 
    264        1.1  thorpej static void
    265        1.9   sekiya zs_kgdb_softint(struct zs_chanstate *cs)
    266        1.1  thorpej {
    267        1.1  thorpej 	printf("zs_kgdb_softint?\n");
    268        1.1  thorpej }
    269