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