Home | History | Annotate | Line # | Download | only in dev
      1 /*	$NetBSD: zs_kgdb.c,v 1.22 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.22 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/autoconf.h>
     61 #include <machine/promlib.h>
     62 #include <sparc/dev/cons.h>
     63 
     64 /* Suns provide a 4.9152 MHz clock to the ZS chips. */
     65 #define PCLK	(9600 * 512)	/* PCLK pin input clock rate */
     66 
     67 /* The layout of this is hardware-dependent (padding, order). */
     68 struct zschan {
     69 	volatile u_char	zc_csr;		/* ctrl,status, and indirect access */
     70 	u_char		zc_xxx0;
     71 	volatile u_char	zc_data;	/* data */
     72 	u_char		zc_xxx1;
     73 };
     74 struct zsdevice {
     75 	/* Yes, they are backwards. */
     76 	struct	zschan zs_chan_b;
     77 	struct	zschan zs_chan_a;
     78 };
     79 
     80 static void zs_setparam(struct zs_chanstate *, int, int);
     81 static void *findzs(int);
     82 struct zsops zsops_kgdb;
     83 
     84 extern int  zs_getc(void *);
     85 extern void zs_putc(void *, int);
     86 
     87 static u_char zs_kgdb_regs[16] = {
     88 	0,	/* 0: CMD (reset, etc.) */
     89 	0,	/* 1: No interrupts yet. */
     90 	0,	/* 2: IVECT */
     91 	ZSWR3_RX_8 | ZSWR3_RX_ENABLE,
     92 	ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP,
     93 	ZSWR5_TX_8 | ZSWR5_TX_ENABLE,
     94 	0,	/* 6: TXSYNC/SYNCLO */
     95 	0,	/* 7: RXSYNC/SYNCHI */
     96 	0,	/* 8: alias for data port */
     97 	ZSWR9_MASTER_IE | ZSWR9_NO_VECTOR,
     98 	0,	/*10: Misc. TX/RX control bits */
     99 	ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD,
    100 	14,	/*12: BAUDLO (default=9600) */
    101 	0,	/*13: BAUDHI (default=9600) */
    102 	ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK,
    103 	ZSWR15_BREAK_IE,
    104 };
    105 
    106 /*
    107  * This replaces "zs_reset()" in the sparc driver.
    108  */
    109 static void
    110 zs_setparam(struct zs_chanstate *cs, int iena, int rate)
    111 {
    112 	int s, tconst;
    113 
    114 	memcpy(cs->cs_preg, zs_kgdb_regs, 16);
    115 
    116 	if (iena) {
    117 		cs->cs_preg[1] = ZSWR1_RIE | ZSWR1_SIE;
    118 	}
    119 
    120 	/* Initialize the speed, etc. */
    121 	tconst = BPS_TO_TCONST(cs->cs_brg_clk, rate);
    122 	cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS;
    123 	cs->cs_preg[12] = tconst;
    124 	cs->cs_preg[13] = tconst >> 8;
    125 
    126 	s = splhigh();
    127 	zs_loadchannelregs(cs);
    128 	splx(s);
    129 }
    130 
    131 /*
    132  * Set up for kgdb; called at boot time before configuration.
    133  * KGDB interrupts will be enabled later when zs0 is configured.
    134  * Called after cninit(), so printf() etc. works.
    135  */
    136 void
    137 zs_kgdb_init(void)
    138 {
    139 	struct zs_chanstate cs;
    140 	struct zsdevice *zsd;
    141 	volatile struct zschan *zc;
    142 	int channel, promzs_unit;
    143 	extern const struct cdevsw zstty_cdevsw;
    144 
    145 	/* printf("zs_kgdb_init: kgdb_dev=0x%x\n", kgdb_dev); */
    146 	if (cdevsw_lookup(kgdb_dev) != &zstty_cdevsw)
    147 		return;
    148 
    149 	/* Note: (ttya,ttyb) on zs0, and (ttyc,ttyd) on zs2 */
    150 	promzs_unit = (kgdb_dev & 2) ? 2 : 0;
    151 	channel  =  kgdb_dev & 1;
    152 	printf("zs_kgdb_init: attaching Serial(%lld) at %d baud\n",
    153 		   (kgdb_dev & 3), kgdb_rate);
    154 
    155 	/* Setup temporary chanstate. */
    156 	memset((void *)&cs, 0, sizeof(cs));
    157 	zsd = findzs(promzs_unit);
    158 	if (zsd == NULL) {
    159 		printf("zs_kgdb_init: zs not mapped.\n");
    160 		return;
    161 	}
    162 	zc = (channel == 0) ? &zsd->zs_chan_a : &zsd->zs_chan_b;
    163 
    164 	cs.cs_channel = channel;
    165 	cs.cs_brg_clk = PCLK / 16;
    166 	cs.cs_reg_csr  = &zc->zc_csr;
    167 	cs.cs_reg_data = &zc->zc_data;
    168 
    169 	/* Now set parameters. (interrupts disabled) */
    170 	zs_setparam(&cs, 0, kgdb_rate);
    171 
    172 	/* Store the getc/putc functions and arg. */
    173 	kgdb_attach(zs_getc, zs_putc, __UNVOLATILE(zc));
    174 }
    175 
    176 /*
    177  * This is a "hook" called by zstty_attach to allow the tty
    178  * to be "taken over" for exclusive use by kgdb.
    179  * Return non-zero if this is the kgdb port.
    180  *
    181  * Set the speed to kgdb_rate, CS8, etc.
    182  */
    183 int
    184 zs_check_kgdb(struct zs_chanstate *cs, int dev)
    185 {
    186 
    187 	if (dev != kgdb_dev)
    188 		return (0);
    189 
    190 	/*
    191 	 * Yes, this is port in use by kgdb.
    192 	 */
    193 	cs->cs_private = NULL;
    194 	cs->cs_ops = &zsops_kgdb;
    195 
    196 	/* Now set parameters. (interrupts enabled) */
    197 	zs_setparam(cs, 1, kgdb_rate);
    198 
    199 	return (1);
    200 }
    201 
    202 /*
    203  * KGDB framing character received: enter kernel debugger.  This probably
    204  * should time out after a few seconds to avoid hanging on spurious input.
    205  */
    206 void
    207 zskgdb(struct zs_chanstate *cs)
    208 {
    209 	int unit = minor(kgdb_dev);
    210 
    211 	printf("zstty%d: kgdb interrupt\n", unit);
    212 	/* This will trap into the debugger. */
    213 	kgdb_connect(1);
    214 }
    215 
    216 
    217 /****************************************************************
    218  * Interface to the lower layer (zscc)
    219  ****************************************************************/
    220 
    221 static void zs_kgdb_rxint(struct zs_chanstate *);
    222 static void zs_kgdb_stint(struct zs_chanstate *, int);
    223 static void zs_kgdb_txint(struct zs_chanstate *);
    224 static void zs_kgdb_softint(struct zs_chanstate *);
    225 
    226 int kgdb_input_lost;
    227 
    228 static void
    229 zs_kgdb_rxint(struct zs_chanstate *cs)
    230 {
    231 	register u_char c, rr1;
    232 
    233 	/*
    234 	 * First read the status, because reading the received char
    235 	 * destroys the status of this char.
    236 	 */
    237 	rr1 = zs_read_reg(cs, 1);
    238 	c = zs_read_data(cs);
    239 
    240 	if (rr1 & (ZSRR1_FE | ZSRR1_DO | ZSRR1_PE)) {
    241 		/* Clear the receive error. */
    242 		zs_write_csr(cs, ZSWR0_RESET_ERRORS);
    243 	}
    244 
    245 	if (c == KGDB_START) {
    246 		zskgdb(cs);
    247 	} else {
    248 		kgdb_input_lost++;
    249 	}
    250 }
    251 
    252 static void
    253 zs_kgdb_txint(struct zs_chanstate *cs)
    254 {
    255 	zs_write_csr(cs, ZSWR0_RESET_TXINT);
    256 }
    257 
    258 static void
    259 zs_kgdb_stint(struct zs_chanstate *cs, int force)
    260 {
    261 	register int rr0;
    262 
    263 	rr0 = zs_read_csr(cs);
    264 	zs_write_csr(cs, ZSWR0_RESET_STATUS);
    265 
    266 	/*
    267 	 * Check here for console break, so that we can abort
    268 	 * even when interrupts are locking up the machine.
    269 	 */
    270 	if (rr0 & ZSRR0_BREAK) {
    271 		zskgdb(cs);
    272 	}
    273 }
    274 
    275 static void
    276 zs_kgdb_softint(struct zs_chanstate *cs)
    277 {
    278 
    279 	printf("zs_kgdb_softint?\n");
    280 }
    281 
    282 struct zsops zsops_kgdb = {
    283 	zs_kgdb_rxint,	/* receive char available */
    284 	zs_kgdb_stint,	/* external/status */
    285 	zs_kgdb_txint,	/* xmit buffer empty */
    286 	zs_kgdb_softint,	/* process software interrupt */
    287 };
    288 
    289 /*
    290  * findzs() should return the address of the given zs channel.
    291  * Here we count on the PROM to map in the required zs chips.
    292  */
    293 static void *
    294 findzs(int zs)
    295 {
    296 
    297 #if defined(SUN4)
    298 	if (CPU_ISSUN4) {
    299 		/*
    300 		 * On sun4, we use hard-coded physical addresses
    301 		 */
    302 #define ZS0_PHYS	0xf1000000
    303 #define ZS1_PHYS	0xf0000000
    304 #define ZS2_PHYS	0xe0000000
    305 		bus_space_handle_t bh;
    306 		bus_addr_t paddr;
    307 
    308 		switch (zs) {
    309 		case 0:
    310 			paddr = ZS0_PHYS;
    311 			break;
    312 		case 1:
    313 			paddr = ZS1_PHYS;
    314 			break;
    315 		case 2:
    316 			paddr = ZS2_PHYS;
    317 			break;
    318 		default:
    319 			return (NULL);
    320 		}
    321 
    322 		if (cpuinfo.cpu_type == CPUTYP_4_100)
    323 			/* Clear top bits of physical address on 4/100 */
    324 			paddr &= ~0xf0000000;
    325 
    326 		/*
    327 		 * Have the obio module figure out which virtual
    328 		 * address the device is mapped to.
    329 		 */
    330 		if (obio_find_rom_map(paddr, PAGE_SIZE, &bh) != 0)
    331 			return (NULL);
    332 
    333 		return ((void *)bh);
    334 	}
    335 #endif
    336 
    337 #if defined(SUN4C) || defined(SUN4M)
    338 	if (CPU_ISSUN4C || CPU_ISSUN4M) {
    339 		int node;
    340 
    341 		node = firstchild(findroot());
    342 		if (CPU_ISSUN4M) {
    343 			/*
    344 			 * On sun4m machines zs is in "obio" tree.
    345 			 */
    346 			node = findnode(node, "obio");
    347 			if (node == 0)
    348 				panic("findzs: no obio node");
    349 			node = firstchild(node);
    350 		}
    351 		while ((node = findnode(node, "zs")) != 0) {
    352 			int nvaddrs, *vaddrs, vstore[10];
    353 
    354 			if (prom_getpropint(node, "slave", -1) != zs) {
    355 				node = nextsibling(node);
    356 				continue;
    357 			}
    358 
    359 			/*
    360 			 * On some machines (e.g. the Voyager), the zs
    361 			 * device has multi-valued register properties.
    362 			 */
    363 			vaddrs = vstore;
    364 			nvaddrs = sizeof(vstore)/sizeof(vstore[0]);
    365 			if (prom_getprop(node, "address", sizeof(int),
    366 				    &nvaddrs, &vaddrs) != 0)
    367 				return (NULL);
    368 
    369 			return ((void *)vaddrs[0]);
    370 		}
    371 	}
    372 #endif
    373 	return (NULL);
    374 }
    375