Home | History | Annotate | Line # | Download | only in dev
zs_kgdb.c revision 1.13
      1 /*	$NetBSD: zs_kgdb.c,v 1.13 2023/10/20 11:38:25 andvar 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.13 2023/10/20 11:38:25 andvar 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 <machine/cpu.h>
     59 
     60 #include <dev/ic/z8530reg.h>
     61 #include <machine/z8530var.h>
     62 #include <next68k/dev/zs_cons.h>
     63 
     64 /*
     65  * The NeXT provides a 3.686400 MHz clock to the ZS chips.
     66  */
     67 #define PCLK	(9600 * 384)		/* PCLK pin input clock rate */
     68 
     69 /* The layout of this is hardware-dependent (padding, order). */
     70 struct zschan {
     71 	volatile uint8_t zc_csr;	/* ctrl,status, and indirect access */
     72 	uint8_t		zc_xxx0;
     73 	volatile uint8_t zc_data;	/* data */
     74 	uint8_t		zc_xxx1;
     75 };
     76 
     77 static void zs_setparam(struct zs_chanstate *, int, int);
     78 struct zsops zsops_kgdb;
     79 
     80 static uint8_t zs_kgdb_regs[16] = {
     81 	0,	/* 0: CMD (reset, etc.) */
     82 	0,	/* 1: No interrupts yet. */
     83 	0x18 + NEXT_I_IPL(NEXT_I_SCC), /* 2: IVECT */
     84 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     85 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     86 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     87 	0,	/* 6: TXSYNC/SYNCLO */
     88 	0,	/* 7: RXSYNC/SYNCHI */
     89 	0,	/* 8: alias for data port */
     90 	ZSWR9_MASTER_IE,
     91 	0,	/*10: Misc. TX/RX control bits */
     92 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
     93 	((PCLK/32)/9600)-2,	/*12: BAUDLO (default=9600) */
     94 	0,			/*13: BAUDHI (default=9600) */
     95 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
     96 	ZSWR15_BREAK_IE,
     97 };
     98 
     99 /*
    100  * This replaces "zs_reset()" in the sparc driver.
    101  */
    102 static void
    103 zs_setparam(struct zs_chanstate *cs, int iena, int rate)
    104 {
    105 	int s, tconst;
    106 
    107 	memcpy(cs->cs_preg, zs_kgdb_regs, 16);
    108 
    109 	if (iena) {
    110 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    111 	}
    112 
    113 	/* Initialize the speed, etc. */
    114 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    115 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    116 	cs->cs_preg[12] = tconst;
    117 	cs->cs_preg[13] = tconst >> 8;
    118 
    119 	s = splhigh();
    120 	zs_loadchannelregs(cs);
    121 	splx(s);
    122 }
    123 
    124 /*
    125  * Set up for kgdb; called at boot time before configuration.
    126  * KGDB interrupts will be enabled later when zs0 is configured.
    127  * Called after cninit(), so printf() etc. works.
    128  */
    129 void
    130 zs_kgdb_init(void)
    131 {
    132 	struct zs_chanstate cs;
    133 	volatile struct zschan *zc;
    134 	int channel;
    135 	extern const struct cdevsw zstty_cdevsw;
    136 
    137 	printf("zs_kgdb_init: kgdb_dev=0x%llx\n", kgdb_dev);
    138 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
    139 		return;
    140 
    141 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
    142 	channel  =  kgdb_dev & 1;
    143 	printf("zs_kgdb_init: attaching to Serial(%lld) at %d baud\n",
    144 		   (kgdb_dev & 3), kgdb_rate);
    145 
    146 	/* Setup temporary chanstate. */
    147 	memset(&cs, 0, sizeof(cs));
    148 	zc = zs_get_chan_addr(channel);
    149 
    150 	if (zc == NULL) {
    151 		printf("zs_kgdb_init: zs not mapped.\n");
    152 		kgdb_dev = -1;
    153 		return;
    154 	}
    155 
    156 	cs.cs_channel = channel;
    157 	cs.cs_brg_clk = PCLK / 16;
    158 	cs.cs_reg_csr  = &zc->zc_csr;
    159 	cs.cs_reg_data = &zc->zc_data;
    160 
    161 	/* Now set parameters. (interrupts disabled) */
    162 	zs_setparam(&cs, 0, kgdb_rate);
    163 
    164 	/* Store the getc/putc functions and arg. */
    165 	kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
    166 }
    167 
    168 /*
    169  * This is a "hook" called by zstty_attach to allow the tty
    170  * to be "taken over" for exclusive use by kgdb.
    171  * Return non-zero if this is the kgdb port.
    172  *
    173  * Set the speed to kgdb_rate, CS8, etc.
    174  */
    175 int
    176 zs_check_kgdb(struct zs_chanstate *cs, int dev)
    177 {
    178 
    179 	if (dev != kgdb_dev)
    180 		return (0);
    181 
    182 	/*
    183 	 * Yes, this is port in use by kgdb.
    184 	 */
    185 	cs->cs_private = NULL;
    186 	cs->cs_ops = &zsops_kgdb;
    187 
    188 	/* Now set parameters. (interrupts enabled) */
    189 	zs_setparam(cs, 1, kgdb_rate);
    190 
    191 	return (1);
    192 }
    193 
    194 /*
    195  * KGDB framing character received: enter kernel debugger.  This probably
    196  * should time out after a few seconds to avoid hanging on spurious input.
    197  */
    198 void
    199 zskgdb(struct zs_chanstate *cs)
    200 {
    201 	int unit = minor(kgdb_dev);
    202 
    203 	printf("zstty%d: kgdb interrupt\n", unit);
    204 	/* This will trap into the debugger. */
    205 	kgdb_connect(1);
    206 }
    207 
    208 
    209 /****************************************************************
    210  * Interface to the lower layer (zscc)
    211  ****************************************************************/
    212 
    213 static void zs_kgdb_rxint(struct zs_chanstate *);
    214 static void zs_kgdb_stint(struct zs_chanstate *, int);
    215 static void zs_kgdb_txint(struct zs_chanstate *);
    216 static void zs_kgdb_softint(struct zs_chanstate *);
    217 
    218 int kgdb_input_lost;
    219 
    220 static void
    221 zs_kgdb_rxint(struct zs_chanstate *cs)
    222 {
    223 	uint8_t c, rr1;
    224 
    225 	/*
    226 	 * First read the status, because reading the received char
    227 	 * destroys the status of this char.
    228 	 */
    229 	rr1 = zs_read_reg(cs, 1);
    230 	c = zs_read_data(cs);
    231 
    232 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    233 		/* Clear the receive error. */
    234 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    235 	}
    236 
    237 	if (c == KGDB_START) {
    238 		zskgdb(cs);
    239 	} else {
    240 		kgdb_input_lost++;
    241 	}
    242 }
    243 
    244 static void
    245 zs_kgdb_txint(struct zs_chanstate *cs)
    246 {
    247 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    248 }
    249 
    250 static void
    251 zs_kgdb_stint(struct zs_chanstate *cs, int force)
    252 {
    253 	int rr0;
    254 
    255 	rr0 = zs_read_csr(cs);
    256 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    257 
    258 	/*
    259 	 * Check here for console break, so that we can abort
    260 	 * even when interrupts are locking up the machine.
    261 	 */
    262 	if (rr0 & ZSRR0_BREAK) {
    263 		zskgdb(cs);
    264 	}
    265 }
    266 
    267 static void
    268 zs_kgdb_softint(struct zs_chanstate *cs)
    269 {
    270 	printf("zs_kgdb_softint?\n");
    271 }
    272 
    273 struct zsops zsops_kgdb = {
    274 	zs_kgdb_rxint,	/* receive char available */
    275 	zs_kgdb_stint,	/* external/status */
    276 	zs_kgdb_txint,	/* xmit buffer empty */
    277 	zs_kgdb_softint,	/* process software interrupt */
    278 };
    279