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