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