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