1 /* $NetBSD: zs.c,v 1.27 2005/12/11 12:18:25 christos 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 * 3. All advertising materials mentioning features or use of this software 19 * must display the following acknowledgement: 20 * This product includes software developed by the NetBSD 21 * Foundation, Inc. and its contributors. 22 * 4. Neither the name of The NetBSD Foundation nor the names of its 23 * contributors may be used to endorse or promote products derived 24 * from this software without specific prior written permission. 25 * 26 * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 27 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 28 * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 29 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 30 * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 31 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 32 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 33 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 34 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 35 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 36 * POSSIBILITY OF SUCH DAMAGE. 37 */ 38 39 /* 40 * Zilog Z8530 Dual UART driver (machine-dependent part) 41 * 42 * Runs two serial lines per chip using slave drivers. 43 * Plain tty/async lines use the zs_async slave. 44 * Sun keyboard/mouse uses the zs_kbd/zs_ms slaves. 45 */ 46 47 /* This was snarfed from the netbsd sparc/dev/zs.c at version 1.56 48 * and then updated to reflect changes in 1.59 49 * by Darrin B Jewell <jewell@mit.edu> Mon Mar 30 20:24:46 1998 50 */ 51 52 #include <sys/cdefs.h> 53 __KERNEL_RCSID(0, "$NetBSD: zs.c,v 1.27 2005/12/11 12:18:25 christos Exp $"); 54 55 #include "opt_ddb.h" 56 #include "opt_kgdb.h" 57 #include "opt_serial.h" 58 59 #include <sys/param.h> 60 #include <sys/systm.h> 61 #include <sys/conf.h> 62 #include <sys/device.h> 63 #include <sys/file.h> 64 #include <sys/ioctl.h> 65 #include <sys/kernel.h> 66 #include <sys/proc.h> 67 #include <sys/tty.h> 68 #include <sys/time.h> 69 #include <sys/syslog.h> 70 71 #include <machine/autoconf.h> 72 #include <machine/cpu.h> 73 #include <machine/psl.h> 74 75 #include <dev/cons.h> 76 77 #include <dev/ic/z8530reg.h> 78 #include <machine/z8530var.h> 79 80 #include <next68k/next68k/isr.h> 81 82 #include <next68k/dev/intiovar.h> 83 #include <next68k/dev/zs_cons.h> 84 85 #include "zsc.h" /* NZSC */ 86 87 #if (NZSC < 0) 88 #error "No serial controllers?" 89 #endif 90 91 /* 92 * Some warts needed by z8530tty.c - 93 * The default parity REALLY needs to be the same as the PROM uses, 94 * or you can not see messages done with printf during boot-up... 95 */ 96 int zs_def_cflag = (CREAD | CS8 | HUPCL); 97 98 /* 99 * The NeXT provides a 3.686400 MHz clock to the ZS chips. 100 */ 101 #define PCLK (9600 * 384) /* PCLK pin input clock rate */ 102 103 #define ZS_DELAY() delay(2) 104 105 /* The layout of this is hardware-dependent (padding, order). */ 106 struct zschan { 107 volatile u_char zc_csr; /* ctrl,status, and indirect access */ 108 u_char zc_xxx0; 109 volatile u_char zc_data; /* data */ 110 u_char zc_xxx1; 111 }; 112 113 /* Flags from cninit() */ 114 static int zs_hwflags[2]; 115 116 /* Default speed for each channel */ 117 static int zs_defspeed[2] = { 118 9600, /* ttya */ 119 9600, /* ttyb */ 120 }; 121 122 static u_char zs_init_reg[16] = { 123 0, /* 0: CMD (reset, etc.) */ 124 0, /* 1: No interrupts yet. */ 125 0x18 + NEXT_I_IPL(NEXT_I_SCC), /* 2: IVECT */ 126 ZSWR3_RX_8 | ZSWR3_RX_ENABLE, 127 ZSWR4_CLK_X16 | ZSWR4_ONESB | ZSWR4_EVENP, 128 ZSWR5_TX_8 | ZSWR5_TX_ENABLE, 129 0, /* 6: TXSYNC/SYNCLO */ 130 0, /* 7: RXSYNC/SYNCHI */ 131 0, /* 8: alias for data port */ 132 ZSWR9_MASTER_IE, 133 0, /*10: Misc. TX/RX control bits */ 134 ZSWR11_TXCLK_BAUD | ZSWR11_RXCLK_BAUD, 135 ((PCLK/32)/9600)-2, /*12: BAUDLO (default=9600) */ 136 0, /*13: BAUDHI (default=9600) */ 137 ZSWR14_BAUD_ENA | ZSWR14_BAUD_FROM_PCLK, 138 ZSWR15_BREAK_IE, 139 }; 140 141 struct zschan * 142 zs_get_chan_addr(int channel) 143 { 144 char *addr; 145 struct zschan *zc; 146 147 addr = (void *)IIOV(NEXT_P_SCC); 148 if (channel == 0) { 149 /* handle the fact the ports are intertwined. */ 150 zc = (struct zschan *)(addr + 1); 151 } else { 152 zc = (struct zschan *)(addr); 153 } 154 return (zc); 155 } 156 157 158 /**************************************************************** 159 * Autoconfig 160 ****************************************************************/ 161 162 /* Definition of the driver for autoconfig. */ 163 static int zs_match(struct device *, struct cfdata *, void *); 164 static void zs_attach(struct device *, struct device *, void *); 165 static int zs_print(void *, const char *); 166 167 extern int zs_getc(void *); 168 extern void zs_putc(void *, int); 169 170 CFATTACH_DECL(zsc, sizeof(struct zsc_softc), 171 zs_match, zs_attach, NULL, NULL); 172 173 extern struct cfdriver zsc_cd; 174 175 static int zs_attached; 176 177 /* Interrupt handlers. */ 178 static int zshard(void *); 179 static void zssoft(void *); 180 181 static int zs_get_speed(struct zs_chanstate *); 182 183 /* 184 * Is the zs chip present? 185 */ 186 static int 187 zs_match(struct device *parent, struct cfdata *cf, void *aux) 188 { 189 struct intio_attach_args *ia = (struct intio_attach_args *)aux; 190 191 if (zs_attached) 192 return 0; 193 194 ia->ia_addr = (void *)IIOV(NEXT_P_SCC); 195 196 return 1; 197 } 198 199 /* 200 * Attach a found zs. 201 * 202 * USE ROM PROPERTIES port-a-ignore-cd AND port-b-ignore-cd FOR 203 * SOFT CARRIER, AND keyboard PROPERTY FOR KEYBOARD/MOUSE? 204 */ 205 static void 206 zs_attach(struct device *parent, struct device *self, void *aux) 207 { 208 struct zsc_softc *zsc = (void *) self; 209 struct zsc_attach_args zsc_args; 210 volatile struct zschan *zc; 211 struct zs_chanstate *cs; 212 int s, channel, sir; 213 214 zs_attached = 1; 215 216 printf("\n"); 217 218 /* 219 * Initialize software state for each channel. 220 */ 221 for (channel = 0; channel < 2; channel++) { 222 zsc_args.channel = channel; 223 zsc_args.hwflags = zs_hwflags[channel]; 224 cs = &zsc->zsc_cs_store[channel]; 225 zsc->zsc_cs[channel] = cs; 226 227 simple_lock_init(&cs->cs_lock); 228 cs->cs_channel = channel; 229 cs->cs_private = NULL; 230 cs->cs_ops = &zsops_null; 231 cs->cs_brg_clk = PCLK / 16; 232 233 zc = zs_get_chan_addr(channel); 234 cs->cs_reg_csr = &zc->zc_csr; 235 cs->cs_reg_data = &zc->zc_data; 236 237 memcpy(cs->cs_creg, zs_init_reg, 16); 238 memcpy(cs->cs_preg, zs_init_reg, 16); 239 240 /* XXX: Get these from the PROM properties! */ 241 /* XXX: See the mvme167 code. Better. */ 242 if (zsc_args.hwflags & ZS_HWFLAG_CONSOLE) 243 cs->cs_defspeed = zs_get_speed(cs); 244 else 245 cs->cs_defspeed = zs_defspeed[channel]; 246 cs->cs_defcflag = zs_def_cflag; 247 248 /* Make these correspond to cs_defcflag (-crtscts) */ 249 cs->cs_rr0_dcd = ZSRR0_DCD; 250 cs->cs_rr0_cts = 0; 251 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS; 252 cs->cs_wr5_rts = 0; 253 254 /* 255 * Clear the master interrupt enable. 256 * The INTENA is common to both channels, 257 * so just do it on the A channel. 258 */ 259 if (channel == 0) { 260 zs_write_reg(cs, 9, 0); 261 } 262 263 /* 264 * Look for a child driver for this channel. 265 * The child attach will setup the hardware. 266 */ 267 if (!config_found(self, (void *)&zsc_args, zs_print)) { 268 /* No sub-driver. Just reset it. */ 269 u_char reset = (channel == 0) ? 270 ZSWR9_A_RESET : ZSWR9_B_RESET; 271 s = splzs(); 272 zs_write_reg(cs, 9, reset); 273 splx(s); 274 } 275 } 276 277 isrlink_autovec(zshard, NULL, NEXT_I_IPL(NEXT_I_SCC), 0, NULL); 278 INTR_ENABLE(NEXT_I_SCC); 279 280 sir = allocate_sir(zssoft, zsc); 281 if (sir != SIR_SERIAL) { 282 panic("Unexpected zssoft sir"); 283 } 284 285 /* 286 * Set the master interrupt enable and interrupt vector. 287 * (common to both channels, do it on A) 288 */ 289 cs = zsc->zsc_cs[0]; 290 s = splhigh(); 291 /* interrupt vector */ 292 zs_write_reg(cs, 2, zs_init_reg[2]); 293 /* master interrupt control (enable) */ 294 zs_write_reg(cs, 9, zs_init_reg[9]); 295 splx(s); 296 } 297 298 static int 299 zs_print(void *aux, const char *name) 300 { 301 struct zsc_attach_args *args = aux; 302 303 if (name != NULL) 304 aprint_normal("%s: ", name); 305 306 if (args->channel != -1) 307 aprint_normal(" channel %d", args->channel); 308 309 return (UNCONF); 310 } 311 312 static volatile int zssoftpending; 313 314 /* 315 * Our ZS chips all share a common, autovectored interrupt, 316 * so we have to look at all of them on each interrupt. 317 */ 318 static int 319 zshard(void *arg) 320 { 321 struct zsc_softc *zsc; 322 int unit, rr3, rval, softreq; 323 324 if (!INTR_OCCURRED(NEXT_I_SCC)) 325 return 0; 326 327 rval = softreq = 0; 328 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) { 329 zsc = zsc_cd.cd_devs[unit]; 330 if (zsc == NULL) 331 continue; 332 rr3 = zsc_intr_hard(zsc); 333 /* Count up the interrupts. */ 334 if (rr3) { 335 rval |= rr3; 336 zsc->zsc_intrcnt.ev_count++; 337 } 338 softreq |= zsc->zsc_cs[0]->cs_softreq; 339 softreq |= zsc->zsc_cs[1]->cs_softreq; 340 } 341 342 /* We are at splzs here, so no need to lock. */ 343 if (softreq && (zssoftpending == 0)) { 344 zssoftpending = 1; 345 setsoftserial(); 346 } 347 return(1); 348 } 349 350 /* 351 * Similar scheme as for zshard (look at all of them) 352 */ 353 static void 354 zssoft(void *arg) 355 { 356 struct zsc_softc *zsc; 357 int s, unit; 358 359 /* This is not the only ISR on this IPL. */ 360 if (zssoftpending == 0) 361 panic("zssoft not pending"); 362 363 /* 364 * The soft intr. bit will be set by zshard only if 365 * the variable zssoftpending is zero. The order of 366 * these next two statements prevents our clearing 367 * the soft intr bit just after zshard has set it. 368 */ 369 /* ienab_bic(IE_ZSSOFT); */ 370 zssoftpending = 0; 371 372 /* Make sure we call the tty layer at spltty. */ 373 s = spltty(); 374 for (unit = 0; unit < zsc_cd.cd_ndevs; unit++) { 375 zsc = zsc_cd.cd_devs[unit]; 376 if (zsc == NULL) 377 continue; 378 (void)zsc_intr_soft(zsc); 379 } 380 splx(s); 381 } 382 383 384 /* 385 * Compute the current baud rate given a ZS channel. 386 */ 387 static int 388 zs_get_speed(struct zs_chanstate *cs) 389 { 390 int tconst; 391 392 tconst = zs_read_reg(cs, 12); 393 tconst |= zs_read_reg(cs, 13) << 8; 394 return (TCONST_TO_BPS(cs->cs_brg_clk, tconst)); 395 } 396 397 /* 398 * MD functions for setting the baud rate and control modes. 399 */ 400 int 401 zs_set_speed(struct zs_chanstate *cs, int bps) 402 { 403 int tconst, real_bps; 404 405 if (bps == 0) 406 return (0); 407 408 #ifdef DIAGNOSTIC 409 if (cs->cs_brg_clk == 0) 410 panic("zs_set_speed"); 411 #endif 412 413 tconst = BPS_TO_TCONST(cs->cs_brg_clk, bps); 414 if (tconst < 0) 415 return (EINVAL); 416 417 /* Convert back to make sure we can do it. */ 418 real_bps = TCONST_TO_BPS(cs->cs_brg_clk, tconst); 419 420 /* XXX - Allow some tolerance here? */ 421 if (real_bps != bps) 422 return (EINVAL); 423 424 cs->cs_preg[12] = tconst; 425 cs->cs_preg[13] = tconst >> 8; 426 427 /* Caller will stuff the pending registers. */ 428 return (0); 429 } 430 431 int 432 zs_set_modes(struct zs_chanstate *cs, int cflag) 433 { 434 int s; 435 436 /* 437 * Output hardware flow control on the chip is horrendous: 438 * if carrier detect drops, the receiver is disabled, and if 439 * CTS drops, the transmitter is stoped IN MID CHARACTER! 440 * Therefore, NEVER set the HFC bit, and instead use the 441 * status interrupt to detect CTS changes. 442 */ 443 s = splzs(); 444 cs->cs_rr0_pps = 0; 445 if ((cflag & (CLOCAL | MDMBUF)) != 0) { 446 cs->cs_rr0_dcd = 0; 447 if ((cflag & MDMBUF) == 0) 448 cs->cs_rr0_pps = ZSRR0_DCD; 449 } else 450 cs->cs_rr0_dcd = ZSRR0_DCD; 451 if ((cflag & CRTSCTS) != 0) { 452 cs->cs_wr5_dtr = ZSWR5_DTR; 453 cs->cs_wr5_rts = ZSWR5_RTS; 454 cs->cs_rr0_cts = ZSRR0_CTS; 455 } else if ((cflag & CDTRCTS) != 0) { 456 cs->cs_wr5_dtr = 0; 457 cs->cs_wr5_rts = ZSWR5_DTR; 458 cs->cs_rr0_cts = ZSRR0_CTS; 459 } else if ((cflag & MDMBUF) != 0) { 460 cs->cs_wr5_dtr = 0; 461 cs->cs_wr5_rts = ZSWR5_DTR; 462 cs->cs_rr0_cts = ZSRR0_DCD; 463 } else { 464 cs->cs_wr5_dtr = ZSWR5_DTR | ZSWR5_RTS; 465 cs->cs_wr5_rts = 0; 466 cs->cs_rr0_cts = 0; 467 } 468 splx(s); 469 470 /* Caller will stuff the pending registers. */ 471 return (0); 472 } 473 474 /* 475 * Read or write the chip with suitable delays. 476 */ 477 478 u_char 479 zs_read_reg(struct zs_chanstate *cs, u_char reg) 480 { 481 u_char val; 482 483 *cs->cs_reg_csr = reg; 484 ZS_DELAY(); 485 val = *cs->cs_reg_csr; 486 ZS_DELAY(); 487 return (val); 488 } 489 490 void 491 zs_write_reg(struct zs_chanstate *cs, u_char reg, u_char val) 492 { 493 *cs->cs_reg_csr = reg; 494 ZS_DELAY(); 495 *cs->cs_reg_csr = val; 496 ZS_DELAY(); 497 } 498 499 u_char 500 zs_read_csr(struct zs_chanstate *cs) 501 { 502 u_char val; 503 504 val = *cs->cs_reg_csr; 505 ZS_DELAY(); 506 return (val); 507 } 508 509 void 510 zs_write_csr(struct zs_chanstate *cs, u_char val) 511 { 512 *cs->cs_reg_csr = val; 513 ZS_DELAY(); 514 } 515 516 u_char 517 zs_read_data(struct zs_chanstate *cs) 518 { 519 u_char val; 520 521 val = *cs->cs_reg_data; 522 ZS_DELAY(); 523 return (val); 524 } 525 526 void 527 zs_write_data(struct zs_chanstate *cs, u_char val) 528 { 529 *cs->cs_reg_data = val; 530 ZS_DELAY(); 531 } 532 533 /**************************************************************** 534 * Console support functions (Sun specific!) 535 * Note: this code is allowed to know about the layout of 536 * the chip registers, and uses that to keep things simple. 537 * XXX - I think I like the mvme167 code better. -gwr 538 ****************************************************************/ 539 540 extern void Debugger(void); 541 void *zs_conschan; 542 int zs_consunit = 0; 543 544 /* 545 * Handle user request to enter kernel debugger. 546 */ 547 void 548 zs_abort(struct zs_chanstate *cs) 549 { 550 #if defined(ZS_CONSOLE_ABORT) 551 volatile struct zschan *zc = zs_conschan; 552 int rr0; 553 554 /* Wait for end of break to avoid PROM abort. */ 555 /* XXX - Limit the wait? */ 556 do { 557 rr0 = zc->zc_csr; 558 ZS_DELAY(); 559 } while (rr0 & ZSRR0_BREAK); 560 561 #if defined(KGDB) 562 zskgdb(cs); 563 #elif defined(DDB) 564 Debugger(); 565 #else 566 /* XXX eventually, drop into next rom monitor here */ 567 printf("stopping on keyboard abort not supported without DDB or KGDB\n"); 568 #endif 569 #else /* !ZS_CONSOLE_ABORT */ 570 return; 571 #endif 572 } 573 574 /* 575 * Polled input char. 576 */ 577 int 578 zs_getc(void *arg) 579 { 580 volatile struct zschan *zc = arg; 581 int s, c, rr0; 582 583 s = splhigh(); 584 /* Wait for a character to arrive. */ 585 do { 586 rr0 = zc->zc_csr; 587 ZS_DELAY(); 588 } while ((rr0 & ZSRR0_RX_READY) == 0); 589 590 c = zc->zc_data; 591 ZS_DELAY(); 592 splx(s); 593 594 /* 595 * This is used by the kd driver to read scan codes, 596 * so don't translate '\r' ==> '\n' here... 597 */ 598 return (c); 599 } 600 601 /* 602 * Polled output char. 603 */ 604 void 605 zs_putc(void *arg, int c) 606 { 607 volatile struct zschan *zc = arg; 608 int s, rr0; 609 610 s = splhigh(); 611 /* Wait for transmitter to become ready. */ 612 do { 613 rr0 = zc->zc_csr; 614 ZS_DELAY(); 615 } while ((rr0 & ZSRR0_TX_READY) == 0); 616 617 618 zc->zc_data = c; 619 ZS_DELAY(); 620 621 splx(s); 622 } 623 624 /*****************************************************************/ 625 626 void zscninit(struct consdev *); 627 int zscngetc(dev_t); 628 void zscnputc(dev_t, int); 629 void zscnprobe(struct consdev *); 630 631 void 632 zscnprobe(struct consdev *cp) 633 { 634 extern const struct cdevsw zstty_cdevsw; 635 int maj; 636 637 maj = cdevsw_lookup_major(&zstty_cdevsw); 638 if (maj != -1) { 639 #ifdef SERCONSOLE 640 cp->cn_pri = CN_REMOTE; 641 #else 642 cp->cn_pri = CN_NORMAL; /* Lower than CN_INTERNAL */ 643 #endif 644 zs_consunit = 0; 645 cp->cn_dev = makedev(maj, zs_consunit); 646 zs_conschan = zs_get_chan_addr(zs_consunit); 647 } else { 648 cp->cn_pri = CN_DEAD; 649 } 650 } 651 652 void 653 zscninit(struct consdev *cn) 654 { 655 struct zs_chanstate xcs; 656 struct zs_chanstate *cs; 657 volatile struct zschan *zc; 658 int tconst, s; 659 660 zs_hwflags[zs_consunit] = ZS_HWFLAG_CONSOLE; 661 662 /* Setup temporary chanstate. */ 663 memset(&xcs, 0, sizeof(xcs)); 664 cs = &xcs; 665 zc = zs_conschan; 666 cs->cs_reg_csr = &zc->zc_csr; 667 cs->cs_reg_data = &zc->zc_data; 668 cs->cs_channel = zs_consunit; 669 cs->cs_brg_clk = PCLK / 16; 670 671 memcpy(cs->cs_preg, zs_init_reg, 16); 672 cs->cs_preg[5] |= ZSWR5_DTR | ZSWR5_RTS; 673 cs->cs_preg[15] = ZSWR15_BREAK_IE; 674 675 tconst = BPS_TO_TCONST(cs->cs_brg_clk, zs_defspeed[zs_consunit]); 676 cs->cs_preg[12] = tconst; 677 cs->cs_preg[13] = tconst >> 8; 678 679 /* 680 * can't use zs_set_speed as we haven't set up the 681 * signal sources, and it's not worth it for now 682 */ 683 684 cs->cs_preg[9] &= ~ZSWR9_MASTER_IE; 685 /* no interrupts until later, after attach. */ 686 687 s = splhigh(); 688 zs_loadchannelregs(cs); 689 splx(s); 690 691 printf("\nNetBSD/next68k console\n"); 692 } 693 694 /* 695 * Polled console input putchar. 696 */ 697 int 698 zscngetc(dev_t dev) 699 { 700 return (zs_getc(zs_conschan)); 701 } 702 703 /* 704 * Polled console output putchar. 705 */ 706 void 707 zscnputc(dev_t dev, int c) 708 { 709 zs_putc(zs_conschan, c); 710 } 711 712 /*****************************************************************/ 713