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