Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.1
      1  1.1  thorpej /*	$NetBSD: zs_kgdb.c,v 1.1 2001/05/11 04:24:44 thorpej 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.1  thorpej  * by Gordon W. Ross.
      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.1  thorpej 
     50  1.1  thorpej #include <sys/param.h>
     51  1.1  thorpej #include <sys/systm.h>
     52  1.1  thorpej #include <sys/proc.h>
     53  1.1  thorpej #include <sys/device.h>
     54  1.1  thorpej #include <sys/conf.h>
     55  1.1  thorpej #include <sys/ioctl.h>
     56  1.1  thorpej #include <sys/kernel.h>
     57  1.1  thorpej #include <sys/syslog.h>
     58  1.1  thorpej #include <sys/kgdb.h>
     59  1.1  thorpej 
     60  1.1  thorpej #include <dev/ic/z8530reg.h>
     61  1.1  thorpej #include <machine/z8530var.h>
     62  1.1  thorpej #include <machine/promlib.h>
     63  1.1  thorpej #include <sparc/dev/cons.h>
     64  1.1  thorpej 
     65  1.1  thorpej /* The SGI provides a 3.6718 MHz (?) clock to the ZS chips.  */
     66  1.1  thorpej #define PCLK	3671808
     67  1.1  thorpej 
     68  1.1  thorpej /* The layout of this is hardware-dependent (padding, order). */
     69  1.1  thorpej struct zschan {
     70  1.1  thorpej 	u_int32_t	zc_control;
     71  1.1  thorpej 	u_int32_t	zs_data;
     72  1.1  thorpej };
     73  1.1  thorpej 
     74  1.1  thorpej struct zsdevice {
     75  1.1  thorpej 	/* Yes, they are backwards. */
     76  1.1  thorpej 	struct	zschan zs_chan_b;
     77  1.1  thorpej 	struct	zschan zs_chan_a;
     78  1.1  thorpej };
     79  1.1  thorpej 
     80  1.1  thorpej static void zs_setparam __P((struct zs_chanstate *, int, int));
     81  1.1  thorpej static void *findzs __P((int));
     82  1.1  thorpej struct zsops zsops_kgdb;
     83  1.1  thorpej 
     84  1.1  thorpej extern int  zs_getc __P((void *arg));
     85  1.1  thorpej extern void zs_putc __P((void *arg, int c));
     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.1  thorpej 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     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.1  thorpej 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    100  1.1  thorpej 	14,	/*12: BAUDLO (default=9600) */
    101  1.1  thorpej 	0,	/*13: BAUDHI (default=9600) */
    102  1.1  thorpej 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    103  1.1  thorpej 	ZSWR15_BREAK_IE,
    104  1.1  thorpej };
    105  1.1  thorpej 
    106  1.1  thorpej /*
    107  1.1  thorpej  * This replaces "zs_reset()" in the sparc driver.
    108  1.1  thorpej  */
    109  1.1  thorpej static void
    110  1.1  thorpej zs_setparam(cs, iena, rate)
    111  1.1  thorpej 	struct zs_chanstate *cs;
    112  1.1  thorpej 	int iena;
    113  1.1  thorpej 	int rate;
    114  1.1  thorpej {
    115  1.1  thorpej 	int s, tconst;
    116  1.1  thorpej 
    117  1.1  thorpej 	bcopy(zs_kgdb_regs, cs->cs_preg, 16);
    118  1.1  thorpej 
    119  1.1  thorpej 	if (iena) {
    120  1.1  thorpej 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    121  1.1  thorpej 	}
    122  1.1  thorpej 
    123  1.1  thorpej 	/* Initialize the speed, etc. */
    124  1.1  thorpej 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    125  1.1  thorpej 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    126  1.1  thorpej 	cs->cs_preg[12] = tconst;
    127  1.1  thorpej 	cs->cs_preg[13] = tconst >> 8;
    128  1.1  thorpej 
    129  1.1  thorpej 	s = splhigh();
    130  1.1  thorpej 	zs_loadchannelregs(cs);
    131  1.1  thorpej 	splx(s);
    132  1.1  thorpej }
    133  1.1  thorpej 
    134  1.1  thorpej /*
    135  1.1  thorpej  * Set up for kgdb; called at boot time before configuration.
    136  1.1  thorpej  * KGDB interrupts will be enabled later when zs0 is configured.
    137  1.1  thorpej  * Called after cninit(), so printf() etc. works.
    138  1.1  thorpej  */
    139  1.1  thorpej void
    140  1.1  thorpej zs_kgdb_init()
    141  1.1  thorpej {
    142  1.1  thorpej 	struct zs_chanstate cs;
    143  1.1  thorpej 	struct zsdevice *zsd;
    144  1.1  thorpej 	volatile struct zschan *zc;
    145  1.1  thorpej 	int channel, promzs_unit;
    146  1.1  thorpej 
    147  1.1  thorpej 	/* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
    148  1.1  thorpej 	if (major(kgdb_dev) != zs_major)
    149  1.1  thorpej 		return;
    150  1.1  thorpej 
    151  1.1  thorpej 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
    152  1.1  thorpej 	promzs_unit = (kgdb_dev & 2) ? 2 : 0;
    153  1.1  thorpej 	channel  =  kgdb_dev & 1;
    154  1.1  thorpej 	printf("zs_kgdb_init: attaching tty%c at %d baud\n",
    155  1.1  thorpej 		   'a' + (kgdb_dev & 3), kgdb_rate);
    156  1.1  thorpej 
    157  1.1  thorpej 	/* Setup temporary chanstate. */
    158  1.1  thorpej 	bzero((caddr_t)&cs, sizeof(cs));
    159  1.1  thorpej 	zsd = findzs(promzs_unit);
    160  1.1  thorpej 	if (zsd == NULL) {
    161  1.1  thorpej 		printf("zs_kgdb_init: zs not mapped.\n");
    162  1.1  thorpej 		return;
    163  1.1  thorpej 	}
    164  1.1  thorpej 	zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    165  1.1  thorpej 
    166  1.1  thorpej 	cs.cs_channel = channel;
    167  1.1  thorpej 	cs.cs_brg_clk = PCLK / 16;
    168  1.1  thorpej 	cs.cs_reg_csr  = &zc->zc_csr;
    169  1.1  thorpej 	cs.cs_reg_data = &zc->zc_data;
    170  1.1  thorpej 
    171  1.1  thorpej 	/* Now set parameters. (interrupts disabled) */
    172  1.1  thorpej 	zs_setparam(&cs, 0, kgdb_rate);
    173  1.1  thorpej 
    174  1.1  thorpej 	/* Store the getc/putc functions and arg. */
    175  1.1  thorpej 	kgdb_attach(zs_getc, zs_putc, (void *)zc);
    176  1.1  thorpej }
    177  1.1  thorpej 
    178  1.1  thorpej /*
    179  1.1  thorpej  * This is a "hook" called by zstty_attach to allow the tty
    180  1.1  thorpej  * to be "taken over" for exclusive use by kgdb.
    181  1.1  thorpej  * Return non-zero if this is the kgdb port.
    182  1.1  thorpej  *
    183  1.1  thorpej  * Set the speed to kgdb_rate, CS8, etc.
    184  1.1  thorpej  */
    185  1.1  thorpej int
    186  1.1  thorpej zs_check_kgdb(cs, dev)
    187  1.1  thorpej 	struct zs_chanstate *cs;
    188  1.1  thorpej 	int dev;
    189  1.1  thorpej {
    190  1.1  thorpej 
    191  1.1  thorpej 	if (dev != kgdb_dev)
    192  1.1  thorpej 		return (0);
    193  1.1  thorpej 
    194  1.1  thorpej 	/*
    195  1.1  thorpej 	 * Yes, this is port in use by kgdb.
    196  1.1  thorpej 	 */
    197  1.1  thorpej 	cs->cs_private = NULL;
    198  1.1  thorpej 	cs->cs_ops = &zsops_kgdb;
    199  1.1  thorpej 
    200  1.1  thorpej 	/* Now set parameters. (interrupts enabled) */
    201  1.1  thorpej 	zs_setparam(cs, 1, kgdb_rate);
    202  1.1  thorpej 
    203  1.1  thorpej 	return (1);
    204  1.1  thorpej }
    205  1.1  thorpej 
    206  1.1  thorpej /*
    207  1.1  thorpej  * KGDB framing character received: enter kernel debugger.  This probably
    208  1.1  thorpej  * should time out after a few seconds to avoid hanging on spurious input.
    209  1.1  thorpej  */
    210  1.1  thorpej void
    211  1.1  thorpej zskgdb(cs)
    212  1.1  thorpej 	struct zs_chanstate *cs;
    213  1.1  thorpej {
    214  1.1  thorpej 	int unit = minor(kgdb_dev);
    215  1.1  thorpej 
    216  1.1  thorpej 	printf("zstty%d: kgdb interrupt\n", unit);
    217  1.1  thorpej 	/* This will trap into the debugger. */
    218  1.1  thorpej 	kgdb_connect(1);
    219  1.1  thorpej }
    220  1.1  thorpej 
    221  1.1  thorpej 
    222  1.1  thorpej /****************************************************************
    223  1.1  thorpej  * Interface to the lower layer (zscc)
    224  1.1  thorpej  ****************************************************************/
    225  1.1  thorpej 
    226  1.1  thorpej static void zs_kgdb_rxint __P((struct zs_chanstate *));
    227  1.1  thorpej static void zs_kgdb_stint __P((struct zs_chanstate *, int));
    228  1.1  thorpej static void zs_kgdb_txint __P((struct zs_chanstate *));
    229  1.1  thorpej static void zs_kgdb_softint __P((struct zs_chanstate *));
    230  1.1  thorpej 
    231  1.1  thorpej int kgdb_input_lost;
    232  1.1  thorpej 
    233  1.1  thorpej static void
    234  1.1  thorpej zs_kgdb_rxint(cs)
    235  1.1  thorpej 	struct zs_chanstate *cs;
    236  1.1  thorpej {
    237  1.1  thorpej 	register u_char c, rr1;
    238  1.1  thorpej 
    239  1.1  thorpej 	/*
    240  1.1  thorpej 	 * First read the status, because reading the received char
    241  1.1  thorpej 	 * destroys the status of this char.
    242  1.1  thorpej 	 */
    243  1.1  thorpej 	rr1 = zs_read_reg(cs, 1);
    244  1.1  thorpej 	c = zs_read_data(cs);
    245  1.1  thorpej 
    246  1.1  thorpej 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    247  1.1  thorpej 		/* Clear the receive error. */
    248  1.1  thorpej 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    249  1.1  thorpej 	}
    250  1.1  thorpej 
    251  1.1  thorpej 	if (c == KGDB_START) {
    252  1.1  thorpej 		zskgdb(cs);
    253  1.1  thorpej 	} else {
    254  1.1  thorpej 		kgdb_input_lost++;
    255  1.1  thorpej 	}
    256  1.1  thorpej }
    257  1.1  thorpej 
    258  1.1  thorpej static void
    259  1.1  thorpej zs_kgdb_txint(cs)
    260  1.1  thorpej 	register struct zs_chanstate *cs;
    261  1.1  thorpej {
    262  1.1  thorpej 	register int rr0;
    263  1.1  thorpej 
    264  1.1  thorpej 	rr0 = zs_read_csr(cs);
    265  1.1  thorpej 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    266  1.1  thorpej }
    267  1.1  thorpej 
    268  1.1  thorpej static void
    269  1.1  thorpej zs_kgdb_stint(cs, force)
    270  1.1  thorpej 	register struct zs_chanstate *cs;
    271  1.1  thorpej 	int force;
    272  1.1  thorpej {
    273  1.1  thorpej 	register int rr0;
    274  1.1  thorpej 
    275  1.1  thorpej 	rr0 = zs_read_csr(cs);
    276  1.1  thorpej 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    277  1.1  thorpej 
    278  1.1  thorpej 	/*
    279  1.1  thorpej 	 * Check here for console break, so that we can abort
    280  1.1  thorpej 	 * even when interrupts are locking up the machine.
    281  1.1  thorpej 	 */
    282  1.1  thorpej 	if (rr0 & ZSRR0_BREAK) {
    283  1.1  thorpej 		zskgdb(cs);
    284  1.1  thorpej 	}
    285  1.1  thorpej }
    286  1.1  thorpej 
    287  1.1  thorpej static void
    288  1.1  thorpej zs_kgdb_softint(cs)
    289  1.1  thorpej {
    290  1.1  thorpej 	printf("zs_kgdb_softint?\n");
    291  1.1  thorpej }
    292  1.1  thorpej 
    293  1.1  thorpej struct zsops zsops_kgdb = {
    294  1.1  thorpej 	zs_kgdb_rxint,	/* receive char available */
    295  1.1  thorpej 	zs_kgdb_stint,	/* external/status */
    296  1.1  thorpej 	zs_kgdb_txint,	/* xmit buffer empty */
    297  1.1  thorpej 	zs_kgdb_softint,	/* process software interrupt */
    298  1.1  thorpej };
    299  1.1  thorpej 
    300  1.1  thorpej /*
    301  1.1  thorpej  * findzs() should return the address of the given zs channel.
    302  1.1  thorpej  * Here we count on the PROM to map in the required zs chips.
    303  1.1  thorpej  */
    304  1.1  thorpej void *
    305  1.1  thorpej findzs(zs)
    306  1.1  thorpej 	int zs;
    307  1.1  thorpej {
    308  1.1  thorpej 	return (NULL);
    309  1.1  thorpej }
    310