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