1/* 2 * macserial.c: Serial port driver for Power Macintoshes. 3 * 4 * Derived from drivers/sbus/char/sunserial.c by Paul Mackerras. 5 * 6 * Copyright (C) 1996 Paul Mackerras (Paul.Mackerras@cs.anu.edu.au) 7 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu) 8 * 9 * Receive DMA code by Takashi Oe <toe@unlserve.unl.edu>. 10 * 11 * $Id: macserial.c,v 1.1.1.1 2008/10/15 03:26:34 james26_jang Exp $ 12 */ 13 14#include <linux/config.h> 15#include <linux/errno.h> 16#include <linux/module.h> 17#include <linux/signal.h> 18#include <linux/sched.h> 19#include <linux/timer.h> 20#include <linux/interrupt.h> 21#include <linux/tty.h> 22#include <linux/tty_flip.h> 23#include <linux/major.h> 24#include <linux/string.h> 25#include <linux/fcntl.h> 26#include <linux/mm.h> 27#include <linux/kernel.h> 28#include <linux/delay.h> 29#include <linux/init.h> 30#ifdef CONFIG_SERIAL_CONSOLE 31#include <linux/console.h> 32#endif 33#include <linux/slab.h> 34 35#include <asm/sections.h> 36#include <asm/io.h> 37#include <asm/pgtable.h> 38#include <asm/irq.h> 39#include <asm/prom.h> 40#include <asm/system.h> 41#include <asm/segment.h> 42#include <asm/bitops.h> 43#include <asm/machdep.h> 44#include <asm/pmac_feature.h> 45#include <linux/adb.h> 46#include <linux/pmu.h> 47#ifdef CONFIG_KGDB 48#include <asm/kgdb.h> 49#endif 50#include <asm/dbdma.h> 51 52#include "macserial.h" 53 54#ifdef CONFIG_PMAC_PBOOK 55static int serial_notify_sleep(struct pmu_sleep_notifier *self, int when); 56static struct pmu_sleep_notifier serial_sleep_notifier = { 57 serial_notify_sleep, 58 SLEEP_LEVEL_MISC, 59}; 60#endif 61 62#define SUPPORT_SERIAL_DMA 63#define MACSERIAL_VERSION "2.0" 64 65/* 66 * It would be nice to dynamically allocate everything that 67 * depends on NUM_SERIAL, so we could support any number of 68 * Z8530s, but for now... 69 */ 70#define NUM_SERIAL 2 /* Max number of ZS chips supported */ 71#define NUM_CHANNELS (NUM_SERIAL * 2) /* 2 channels per chip */ 72 73/* On PowerMacs, the hardware takes care of the SCC recovery time, 74 but we need the eieio to make sure that the accesses occur 75 in the order we want. */ 76#define RECOVERY_DELAY eieio() 77 78struct mac_zschannel zs_channels[NUM_CHANNELS]; 79 80struct mac_serial zs_soft[NUM_CHANNELS]; 81int zs_channels_found; 82struct mac_serial *zs_chain; /* list of all channels */ 83 84struct tty_struct zs_ttys[NUM_CHANNELS]; 85 86static int is_powerbook; 87 88#ifdef CONFIG_SERIAL_CONSOLE 89static struct console sercons; 90#endif 91 92#ifdef CONFIG_KGDB 93struct mac_zschannel *zs_kgdbchan; 94static unsigned char scc_inittab[] = { 95 9, 0x80, /* reset A side (CHRA) */ 96 13, 0, /* set baud rate divisor */ 97 12, 1, 98 14, 1, /* baud rate gen enable, src=rtxc (BRENABL) */ 99 11, 0x50, /* clocks = br gen (RCBR | TCBR) */ 100 5, 0x6a, /* tx 8 bits, assert RTS (Tx8 | TxENAB | RTS) */ 101 4, 0x44, /* x16 clock, 1 stop (SB1 | X16CLK)*/ 102 3, 0xc1, /* rx enable, 8 bits (RxENABLE | Rx8)*/ 103}; 104#endif 105#define ZS_CLOCK 3686400 /* Z8530 RTxC input clock rate */ 106 107static DECLARE_TASK_QUEUE(tq_serial); 108 109static struct tty_driver serial_driver, callout_driver; 110static int serial_refcount; 111 112/* serial subtype definitions */ 113#define SERIAL_TYPE_NORMAL 1 114#define SERIAL_TYPE_CALLOUT 2 115 116/* number of characters left in xmit buffer before we ask for more */ 117#define WAKEUP_CHARS 256 118 119/* 120 * Debugging. 121 */ 122#undef SERIAL_DEBUG_INTR 123#undef SERIAL_DEBUG_OPEN 124#undef SERIAL_DEBUG_FLOW 125#undef SERIAL_DEBUG_POWER 126#undef SERIAL_DEBUG_THROTTLE 127#undef SERIAL_DEBUG_STOP 128#undef SERIAL_DEBUG_BAUDS 129 130#define RS_STROBE_TIME 10 131#define RS_ISR_PASS_LIMIT 256 132 133#define _INLINE_ inline 134 135#ifdef SERIAL_DEBUG_OPEN 136#define OPNDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg) 137#else 138#define OPNDBG(fmt, arg...) do { } while (0) 139#endif 140#ifdef SERIAL_DEBUG_POWER 141#define PWRDBG(fmt, arg...) printk(KERN_DEBUG fmt , ## arg) 142#else 143#define PWRDBG(fmt, arg...) do { } while (0) 144#endif 145#ifdef SERIAL_DEBUG_BAUDS 146#define BAUDBG(fmt, arg...) printk(fmt , ## arg) 147#else 148#define BAUDBG(fmt, arg...) do { } while (0) 149#endif 150 151static void probe_sccs(void); 152static void change_speed(struct mac_serial *info, struct termios *old); 153static void rs_wait_until_sent(struct tty_struct *tty, int timeout); 154static int set_scc_power(struct mac_serial * info, int state); 155static int setup_scc(struct mac_serial * info); 156static void dbdma_reset(volatile struct dbdma_regs *dma); 157static void dbdma_flush(volatile struct dbdma_regs *dma); 158static void rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs); 159static void rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs); 160static void dma_init(struct mac_serial * info); 161static void rxdma_start(struct mac_serial * info, int current); 162static void rxdma_to_tty(struct mac_serial * info); 163 164static struct tty_struct *serial_table[NUM_CHANNELS]; 165static struct termios *serial_termios[NUM_CHANNELS]; 166static struct termios *serial_termios_locked[NUM_CHANNELS]; 167 168#ifndef MIN 169#define MIN(a,b) ((a) < (b) ? (a) : (b)) 170#endif 171 172/* 173 * tmp_buf is used as a temporary buffer by serial_write. We need to 174 * lock it in case the copy_from_user blocks while swapping in a page, 175 * and some other program tries to do a serial write at the same time. 176 * Since the lock will only come under contention when the system is 177 * swapping and available memory is low, it makes sense to share one 178 * buffer across all the serial ports, since it significantly saves 179 * memory if large numbers of serial ports are open. 180 */ 181static unsigned char *tmp_buf; 182static DECLARE_MUTEX(tmp_buf_sem); 183 184 185static inline int __pmac 186serial_paranoia_check(struct mac_serial *info, 187 dev_t device, const char *routine) 188{ 189#ifdef SERIAL_PARANOIA_CHECK 190 static const char badmagic[] = KERN_WARNING 191 "Warning: bad magic number for serial struct (%d, %d) in %s\n"; 192 static const char badinfo[] = KERN_WARNING 193 "Warning: null mac_serial for (%d, %d) in %s\n"; 194 195 if (!info) { 196 printk(badinfo, MAJOR(device), MINOR(device), routine); 197 return 1; 198 } 199 if (info->magic != SERIAL_MAGIC) { 200 printk(badmagic, MAJOR(device), MINOR(device), routine); 201 return 1; 202 } 203#endif 204 return 0; 205} 206 207/* 208 * Reading and writing Z8530 registers. 209 */ 210static inline unsigned char __pmac read_zsreg(struct mac_zschannel *channel, 211 unsigned char reg) 212{ 213 unsigned char retval; 214 unsigned long flags; 215 216 /* 217 * We have to make this atomic. 218 */ 219 spin_lock_irqsave(&channel->lock, flags); 220 if (reg != 0) { 221 *channel->control = reg; 222 RECOVERY_DELAY; 223 } 224 retval = *channel->control; 225 RECOVERY_DELAY; 226 spin_unlock_irqrestore(&channel->lock, flags); 227 return retval; 228} 229 230static inline void __pmac write_zsreg(struct mac_zschannel *channel, 231 unsigned char reg, unsigned char value) 232{ 233 unsigned long flags; 234 235 spin_lock_irqsave(&channel->lock, flags); 236 if (reg != 0) { 237 *channel->control = reg; 238 RECOVERY_DELAY; 239 } 240 *channel->control = value; 241 RECOVERY_DELAY; 242 spin_unlock_irqrestore(&channel->lock, flags); 243 return; 244} 245 246static inline unsigned char __pmac read_zsdata(struct mac_zschannel *channel) 247{ 248 unsigned char retval; 249 250 retval = *channel->data; 251 RECOVERY_DELAY; 252 return retval; 253} 254 255static inline void write_zsdata(struct mac_zschannel *channel, 256 unsigned char value) 257{ 258 *channel->data = value; 259 RECOVERY_DELAY; 260 return; 261} 262 263static inline void load_zsregs(struct mac_zschannel *channel, 264 unsigned char *regs) 265{ 266 ZS_CLEARERR(channel); 267 ZS_CLEARFIFO(channel); 268 /* Load 'em up */ 269 write_zsreg(channel, R4, regs[R4]); 270 write_zsreg(channel, R10, regs[R10]); 271 write_zsreg(channel, R3, regs[R3] & ~RxENABLE); 272 write_zsreg(channel, R5, regs[R5] & ~TxENAB); 273 write_zsreg(channel, R1, regs[R1]); 274 write_zsreg(channel, R9, regs[R9]); 275 write_zsreg(channel, R11, regs[R11]); 276 write_zsreg(channel, R12, regs[R12]); 277 write_zsreg(channel, R13, regs[R13]); 278 write_zsreg(channel, R14, regs[R14]); 279 write_zsreg(channel, R15, regs[R15]); 280 write_zsreg(channel, R3, regs[R3]); 281 write_zsreg(channel, R5, regs[R5]); 282 return; 283} 284 285/* Sets or clears DTR/RTS on the requested line */ 286static inline void zs_rtsdtr(struct mac_serial *ss, int set) 287{ 288 if (set) 289 ss->curregs[5] |= (RTS | DTR); 290 else 291 ss->curregs[5] &= ~(RTS | DTR); 292 write_zsreg(ss->zs_channel, 5, ss->curregs[5]); 293 return; 294} 295 296/* Utility routines for the Zilog */ 297static inline int get_zsbaud(struct mac_serial *ss) 298{ 299 struct mac_zschannel *channel = ss->zs_channel; 300 int brg; 301 302 if ((ss->curregs[R11] & TCBR) == 0) { 303 /* higher rates don't use the baud rate generator */ 304 return (ss->curregs[R4] & X32CLK)? ZS_CLOCK/32: ZS_CLOCK/16; 305 } 306 /* The baud rate is split up between two 8-bit registers in 307 * what is termed 'BRG time constant' format in my docs for 308 * the chip, it is a function of the clk rate the chip is 309 * receiving which happens to be constant. 310 */ 311 brg = (read_zsreg(channel, 13) << 8); 312 brg |= read_zsreg(channel, 12); 313 return BRG_TO_BPS(brg, (ZS_CLOCK/(ss->clk_divisor))); 314} 315 316/* On receive, this clears errors and the receiver interrupts */ 317static inline void rs_recv_clear(struct mac_zschannel *zsc) 318{ 319 write_zsreg(zsc, 0, ERR_RES); 320 write_zsreg(zsc, 0, RES_H_IUS); 321} 322 323/* 324 * Reset a Descriptor-Based DMA channel. 325 */ 326static void dbdma_reset(volatile struct dbdma_regs *dma) 327{ 328 int i; 329 330 out_le32(&dma->control, (WAKE|FLUSH|PAUSE|RUN) << 16); 331 332 /* 333 * Yes this looks peculiar, but apparently it needs to be this 334 * way on some machines. (We need to make sure the DBDMA 335 * engine has actually got the write above and responded 336 * to it. - paulus) 337 */ 338 for (i = 200; i > 0; --i) 339 if (ld_le32(&dma->status) & RUN) 340 udelay(1); 341} 342 343/* 344 * Tells a DBDMA channel to stop and write any buffered data 345 * it might have to memory. 346 */ 347static _INLINE_ void dbdma_flush(volatile struct dbdma_regs *dma) 348{ 349 int i = 0; 350 351 out_le32(&dma->control, (FLUSH << 16) | FLUSH); 352 while (((in_le32(&dma->status) & FLUSH) != 0) && (i++ < 100)) 353 udelay(1); 354} 355 356/* 357 * ---------------------------------------------------------------------- 358 * 359 * Here starts the interrupt handling routines. All of the following 360 * subroutines are declared as inline and are folded into 361 * rs_interrupt(). They were separated out for readability's sake. 362 * 363 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93 364 * ----------------------------------------------------------------------- 365 */ 366 367/* 368 * This routine is used by the interrupt handler to schedule 369 * processing in the software interrupt portion of the driver. 370 */ 371static _INLINE_ void rs_sched_event(struct mac_serial *info, 372 int event) 373{ 374 info->event |= 1 << event; 375 queue_task(&info->tqueue, &tq_serial); 376 mark_bh(MACSERIAL_BH); 377} 378 379/* Work out the flag value for a z8530 status value. */ 380static _INLINE_ int stat_to_flag(int stat) 381{ 382 int flag; 383 384 if (stat & Rx_OVR) { 385 flag = TTY_OVERRUN; 386 } else if (stat & FRM_ERR) { 387 flag = TTY_FRAME; 388 } else if (stat & PAR_ERR) { 389 flag = TTY_PARITY; 390 } else 391 flag = 0; 392 return flag; 393} 394 395static _INLINE_ void receive_chars(struct mac_serial *info, 396 struct pt_regs *regs) 397{ 398 struct tty_struct *tty = info->tty; 399 unsigned char ch, stat, flag; 400 401 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) != 0) { 402 403 stat = read_zsreg(info->zs_channel, R1); 404 ch = read_zsdata(info->zs_channel); 405 406#ifdef CONFIG_KGDB 407 if (info->kgdb_channel) { 408 if (ch == 0x03 || ch == '$') 409 breakpoint(); 410 if (stat & (Rx_OVR|FRM_ERR|PAR_ERR)) 411 write_zsreg(info->zs_channel, 0, ERR_RES); 412 return; 413 } 414#endif 415 if (!tty) 416 continue; 417 if (tty->flip.count >= TTY_FLIPBUF_SIZE) 418 tty_flip_buffer_push(tty); 419 420 if (tty->flip.count >= TTY_FLIPBUF_SIZE) { 421 static int flip_buf_ovf; 422 if (++flip_buf_ovf <= 1) 423 printk(KERN_WARNING "FB. overflow: %d\n", 424 flip_buf_ovf); 425 break; 426 } 427 tty->flip.count++; 428 { 429 static int flip_max_cnt; 430 if (flip_max_cnt < tty->flip.count) 431 flip_max_cnt = tty->flip.count; 432 } 433 flag = stat_to_flag(stat); 434 if (flag) 435 /* reset the error indication */ 436 write_zsreg(info->zs_channel, 0, ERR_RES); 437 *tty->flip.flag_buf_ptr++ = flag; 438 *tty->flip.char_buf_ptr++ = ch; 439 } 440 if (tty) 441 tty_flip_buffer_push(tty); 442} 443 444static void transmit_chars(struct mac_serial *info) 445{ 446 unsigned long flags; 447 448 save_flags(flags); 449 cli(); 450 if ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) 451 goto out; 452 info->tx_active = 0; 453 454 if (info->x_char && !info->power_wait) { 455 /* Send next char */ 456 write_zsdata(info->zs_channel, info->x_char); 457 info->x_char = 0; 458 info->tx_active = 1; 459 goto out; 460 } 461 462 if ((info->xmit_cnt <= 0) || info->tty->stopped || info->tx_stopped 463 || info->power_wait) { 464 write_zsreg(info->zs_channel, 0, RES_Tx_P); 465 goto out; 466 } 467 468 /* Send char */ 469 write_zsdata(info->zs_channel, info->xmit_buf[info->xmit_tail++]); 470 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1); 471 info->xmit_cnt--; 472 info->tx_active = 1; 473 474 if (info->xmit_cnt < WAKEUP_CHARS) 475 rs_sched_event(info, RS_EVENT_WRITE_WAKEUP); 476 477 out: 478 restore_flags(flags); 479} 480 481static void powerup_done(unsigned long data) 482{ 483 struct mac_serial *info = (struct mac_serial *) data; 484 485 info->power_wait = 0; 486 transmit_chars(info); 487} 488 489static _INLINE_ void status_handle(struct mac_serial *info) 490{ 491 unsigned char status; 492 493 /* Get status from Read Register 0 */ 494 status = read_zsreg(info->zs_channel, 0); 495 496 /* Check for DCD transitions */ 497 if (((status ^ info->read_reg_zero) & DCD) != 0 498 && info->tty && !C_CLOCAL(info->tty)) { 499 if (status & DCD) { 500 wake_up_interruptible(&info->open_wait); 501 } else if (!(info->flags & ZILOG_CALLOUT_ACTIVE)) { 502 if (info->tty) 503 tty_hangup(info->tty); 504 } 505 } 506 507 /* Check for CTS transitions */ 508 if (info->tty && C_CRTSCTS(info->tty)) { 509 /* 510 * For some reason, on the Power Macintosh, 511 * it seems that the CTS bit is 1 when CTS is 512 * *negated* and 0 when it is asserted. 513 * The DCD bit doesn't seem to be inverted 514 * like this. 515 */ 516 if ((status & CTS) == 0) { 517 if (info->tx_stopped) { 518#ifdef SERIAL_DEBUG_FLOW 519 printk(KERN_DEBUG "CTS up\n"); 520#endif 521 info->tx_stopped = 0; 522 if (!info->tx_active) 523 transmit_chars(info); 524 } 525 } else { 526#ifdef SERIAL_DEBUG_FLOW 527 printk(KERN_DEBUG "CTS down\n"); 528#endif 529 info->tx_stopped = 1; 530 } 531 } 532 533 /* Clear status condition... */ 534 write_zsreg(info->zs_channel, 0, RES_EXT_INT); 535 info->read_reg_zero = status; 536} 537 538static _INLINE_ void receive_special_dma(struct mac_serial *info) 539{ 540 unsigned char stat, flag; 541 volatile struct dbdma_regs *rd = &info->rx->dma; 542 int where = RX_BUF_SIZE; 543 544 spin_lock(&info->rx_dma_lock); 545 if ((ld_le32(&rd->status) & ACTIVE) != 0) 546 dbdma_flush(rd); 547 if (in_le32(&rd->cmdptr) 548 == virt_to_bus(info->rx_cmds[info->rx_cbuf] + 1)) 549 where -= in_le16(&info->rx->res_count); 550 where--; 551 552 stat = read_zsreg(info->zs_channel, R1); 553 554 flag = stat_to_flag(stat); 555 if (flag) { 556 info->rx_flag_buf[info->rx_cbuf][where] = flag; 557 /* reset the error indication */ 558 write_zsreg(info->zs_channel, 0, ERR_RES); 559 } 560 561 spin_unlock(&info->rx_dma_lock); 562} 563 564/* 565 * This is the serial driver's generic interrupt routine 566 */ 567static void rs_interrupt(int irq, void *dev_id, struct pt_regs * regs) 568{ 569 struct mac_serial *info = (struct mac_serial *) dev_id; 570 unsigned char zs_intreg; 571 int shift; 572 573 if (!(info->flags & ZILOG_INITIALIZED)) { 574 printk(KERN_WARNING "rs_interrupt: irq %d, port not " 575 "initialized\n", irq); 576 disable_irq(irq); 577 return; 578 } 579 580 /* NOTE: The read register 3, which holds the irq status, 581 * does so for both channels on each chip. Although 582 * the status value itself must be read from the A 583 * channel and is only valid when read from channel A. 584 * Yes... broken hardware... 585 */ 586#define CHAN_IRQMASK (CHBRxIP | CHBTxIP | CHBEXT) 587 588 if (info->zs_chan_a == info->zs_channel) 589 shift = 3; /* Channel A */ 590 else 591 shift = 0; /* Channel B */ 592 593 for (;;) { 594 zs_intreg = read_zsreg(info->zs_chan_a, 3) >> shift; 595#ifdef SERIAL_DEBUG_INTR 596 printk(KERN_DEBUG "rs_interrupt: irq %d, zs_intreg 0x%x\n", 597 irq, (int)zs_intreg); 598#endif 599 600 if ((zs_intreg & CHAN_IRQMASK) == 0) 601 break; 602 603 if (zs_intreg & CHBRxIP) { 604 /* If we are doing DMA, we only ask for interrupts 605 on characters with errors or special conditions. */ 606 if (info->dma_initted) 607 receive_special_dma(info); 608 else 609 receive_chars(info, regs); 610 } 611 if (zs_intreg & CHBTxIP) 612 transmit_chars(info); 613 if (zs_intreg & CHBEXT) 614 status_handle(info); 615 } 616} 617 618/* Transmit DMA interrupt - not used at present */ 619static void rs_txdma_irq(int irq, void *dev_id, struct pt_regs *regs) 620{ 621} 622 623/* 624 * Receive DMA interrupt. 625 */ 626static void rs_rxdma_irq(int irq, void *dev_id, struct pt_regs *regs) 627{ 628 struct mac_serial *info = (struct mac_serial *) dev_id; 629 volatile struct dbdma_cmd *cd; 630 631 if (!info->dma_initted) 632 return; 633 spin_lock(&info->rx_dma_lock); 634 /* First, confirm that this interrupt is, indeed, coming */ 635 /* from Rx DMA */ 636 cd = info->rx_cmds[info->rx_cbuf] + 2; 637 if ((in_le16(&cd->xfer_status) & (RUN | ACTIVE)) != (RUN | ACTIVE)) { 638 spin_unlock(&info->rx_dma_lock); 639 return; 640 } 641 if (info->rx_fbuf != RX_NO_FBUF) { 642 info->rx_cbuf = info->rx_fbuf; 643 if (++info->rx_fbuf == info->rx_nbuf) 644 info->rx_fbuf = 0; 645 if (info->rx_fbuf == info->rx_ubuf) 646 info->rx_fbuf = RX_NO_FBUF; 647 } 648 spin_unlock(&info->rx_dma_lock); 649} 650 651/* 652 * ------------------------------------------------------------------- 653 * Here ends the serial interrupt routines. 654 * ------------------------------------------------------------------- 655 */ 656 657/* 658 * ------------------------------------------------------------ 659 * rs_stop() and rs_start() 660 * 661 * This routines are called before setting or resetting tty->stopped. 662 * ------------------------------------------------------------ 663 */ 664static void rs_stop(struct tty_struct *tty) 665{ 666 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 667 668#ifdef SERIAL_DEBUG_STOP 669 printk(KERN_DEBUG "rs_stop %ld....\n", 670 tty->ldisc.chars_in_buffer(tty)); 671#endif 672 673 if (serial_paranoia_check(info, tty->device, "rs_stop")) 674 return; 675 676} 677 678static void rs_start(struct tty_struct *tty) 679{ 680 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 681 unsigned long flags; 682 683#ifdef SERIAL_DEBUG_STOP 684 printk(KERN_DEBUG "rs_start %ld....\n", 685 tty->ldisc.chars_in_buffer(tty)); 686#endif 687 688 if (serial_paranoia_check(info, tty->device, "rs_start")) 689 return; 690 691 save_flags(flags); cli(); 692 if (info->xmit_cnt && info->xmit_buf && !info->tx_active) { 693 transmit_chars(info); 694 } 695 restore_flags(flags); 696} 697 698/* 699 * This routine is used to handle the "bottom half" processing for the 700 * serial driver, known also the "software interrupt" processing. 701 * This processing is done at the kernel interrupt level, after the 702 * rs_interrupt() has returned, BUT WITH INTERRUPTS TURNED ON. This 703 * is where time-consuming activities which can not be done in the 704 * interrupt driver proper are done; the interrupt driver schedules 705 * them using rs_sched_event(), and they get done here. 706 */ 707static void do_serial_bh(void) 708{ 709 run_task_queue(&tq_serial); 710} 711 712static void do_softint(void *private_) 713{ 714 struct mac_serial *info = (struct mac_serial *) private_; 715 struct tty_struct *tty; 716 717 tty = info->tty; 718 if (!tty) 719 return; 720 721 if (test_and_clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) { 722 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && 723 tty->ldisc.write_wakeup) 724 (tty->ldisc.write_wakeup)(tty); 725 wake_up_interruptible(&tty->write_wait); 726 } 727} 728 729static int startup(struct mac_serial * info) 730{ 731 int delay; 732 733 OPNDBG("startup() (ttyS%d, irq %d)\n", info->line, info->irq); 734 735 if (info->flags & ZILOG_INITIALIZED) { 736 OPNDBG(" -> already inited\n"); 737 return 0; 738 } 739 740 if (!info->xmit_buf) { 741 info->xmit_buf = (unsigned char *) get_free_page(GFP_KERNEL); 742 if (!info->xmit_buf) 743 return -ENOMEM; 744 } 745 746 OPNDBG("starting up ttyS%d (irq %d)...\n", info->line, info->irq); 747 748 delay = set_scc_power(info, 1); 749 750 setup_scc(info); 751 752 if (delay) { 753 unsigned long flags; 754 755 /* delay is in ms */ 756 save_flags(flags); 757 cli(); 758 info->power_wait = 1; 759 mod_timer(&info->powerup_timer, 760 jiffies + (delay * HZ + 999) / 1000); 761 restore_flags(flags); 762 } 763 764 OPNDBG("enabling IRQ on ttyS%d (irq %d)...\n", info->line, info->irq); 765 766 info->flags |= ZILOG_INITIALIZED; 767 enable_irq(info->irq); 768 if (info->dma_initted) { 769 enable_irq(info->rx_dma_irq); 770 } 771 772 return 0; 773} 774 775static _INLINE_ void rxdma_start(struct mac_serial * info, int current) 776{ 777 volatile struct dbdma_regs *rd = &info->rx->dma; 778 volatile struct dbdma_cmd *cd = info->rx_cmds[current]; 779 780//printk(KERN_DEBUG "SCC: rxdma_start\n"); 781 782 st_le32(&rd->cmdptr, virt_to_bus(cd)); 783 out_le32(&rd->control, (RUN << 16) | RUN); 784} 785 786static void rxdma_to_tty(struct mac_serial *info) 787{ 788 struct tty_struct *tty = info->tty; 789 volatile struct dbdma_regs *rd = &info->rx->dma; 790 unsigned long flags; 791 int residue, available, space, do_queue; 792 793 if (!tty) 794 return; 795 796 do_queue = 0; 797 spin_lock_irqsave(&info->rx_dma_lock, flags); 798more: 799 space = TTY_FLIPBUF_SIZE - tty->flip.count; 800 if (!space) { 801 do_queue++; 802 goto out; 803 } 804 residue = 0; 805 if (info->rx_ubuf == info->rx_cbuf) { 806 if ((ld_le32(&rd->status) & ACTIVE) != 0) { 807 dbdma_flush(rd); 808 if (in_le32(&rd->cmdptr) 809 == virt_to_bus(info->rx_cmds[info->rx_cbuf]+1)) 810 residue = in_le16(&info->rx->res_count); 811 } 812 } 813 available = RX_BUF_SIZE - residue - info->rx_done_bytes; 814 if (available > space) 815 available = space; 816 if (available) { 817 memcpy(tty->flip.char_buf_ptr, 818 info->rx_char_buf[info->rx_ubuf] + info->rx_done_bytes, 819 available); 820 memcpy(tty->flip.flag_buf_ptr, 821 info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes, 822 available); 823 tty->flip.char_buf_ptr += available; 824 tty->flip.count += available; 825 tty->flip.flag_buf_ptr += available; 826 memset(info->rx_flag_buf[info->rx_ubuf] + info->rx_done_bytes, 827 0, available); 828 info->rx_done_bytes += available; 829 do_queue++; 830 } 831 if (info->rx_done_bytes == RX_BUF_SIZE) { 832 volatile struct dbdma_cmd *cd = info->rx_cmds[info->rx_ubuf]; 833 834 if (info->rx_ubuf == info->rx_cbuf) 835 goto out; 836 /* mark rx_char_buf[rx_ubuf] free */ 837 st_le16(&cd->command, DBDMA_NOP); 838 cd++; 839 st_le32(&cd->cmd_dep, 0); 840 st_le32((unsigned int *)&cd->res_count, 0); 841 cd++; 842 st_le16(&cd->xfer_status, 0); 843 844 if (info->rx_fbuf == RX_NO_FBUF) { 845 info->rx_fbuf = info->rx_ubuf; 846 if (!(ld_le32(&rd->status) & ACTIVE)) { 847 dbdma_reset(&info->rx->dma); 848 rxdma_start(info, info->rx_ubuf); 849 info->rx_cbuf = info->rx_ubuf; 850 } 851 } 852 info->rx_done_bytes = 0; 853 if (++info->rx_ubuf == info->rx_nbuf) 854 info->rx_ubuf = 0; 855 if (info->rx_fbuf == info->rx_ubuf) 856 info->rx_fbuf = RX_NO_FBUF; 857 goto more; 858 } 859out: 860 spin_unlock_irqrestore(&info->rx_dma_lock, flags); 861 if (do_queue) 862 queue_task(&tty->flip.tqueue, &tq_timer); 863} 864 865static void poll_rxdma(unsigned long private_) 866{ 867 struct mac_serial *info = (struct mac_serial *) private_; 868 unsigned long flags; 869 870 rxdma_to_tty(info); 871 spin_lock_irqsave(&info->rx_dma_lock, flags); 872 mod_timer(&info->poll_dma_timer, RX_DMA_TIMER); 873 spin_unlock_irqrestore(&info->rx_dma_lock, flags); 874} 875 876static void dma_init(struct mac_serial * info) 877{ 878 int i, size; 879 volatile struct dbdma_cmd *cd; 880 unsigned char *p; 881 882 info->rx_nbuf = 8; 883 884 /* various mem set up */ 885 size = sizeof(struct dbdma_cmd) * (3 * info->rx_nbuf + 2) 886 + (RX_BUF_SIZE * 2 + sizeof(*info->rx_cmds) 887 + sizeof(*info->rx_char_buf) + sizeof(*info->rx_flag_buf)) 888 * info->rx_nbuf; 889 info->dma_priv = kmalloc(size, GFP_KERNEL | GFP_DMA); 890 if (info->dma_priv == NULL) 891 return; 892 memset(info->dma_priv, 0, size); 893 894 info->rx_cmds = (volatile struct dbdma_cmd **)info->dma_priv; 895 info->rx_char_buf = (unsigned char **) (info->rx_cmds + info->rx_nbuf); 896 info->rx_flag_buf = info->rx_char_buf + info->rx_nbuf; 897 p = (unsigned char *) (info->rx_flag_buf + info->rx_nbuf); 898 for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE) 899 info->rx_char_buf[i] = p; 900 for (i = 0; i < info->rx_nbuf; i++, p += RX_BUF_SIZE) 901 info->rx_flag_buf[i] = p; 902 903 /* a bit of DMA programming */ 904 cd = info->rx_cmds[0] = (volatile struct dbdma_cmd *) DBDMA_ALIGN(p); 905 st_le16(&cd->command, DBDMA_NOP); 906 cd++; 907 st_le16(&cd->req_count, RX_BUF_SIZE); 908 st_le16(&cd->command, INPUT_MORE); 909 st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[0])); 910 cd++; 911 st_le16(&cd->req_count, 4); 912 st_le16(&cd->command, STORE_WORD | INTR_ALWAYS); 913 st_le32(&cd->phy_addr, virt_to_bus(cd-2)); 914 st_le32(&cd->cmd_dep, DBDMA_STOP); 915 for (i = 1; i < info->rx_nbuf; i++) { 916 info->rx_cmds[i] = ++cd; 917 st_le16(&cd->command, DBDMA_NOP); 918 cd++; 919 st_le16(&cd->req_count, RX_BUF_SIZE); 920 st_le16(&cd->command, INPUT_MORE); 921 st_le32(&cd->phy_addr, virt_to_bus(info->rx_char_buf[i])); 922 cd++; 923 st_le16(&cd->req_count, 4); 924 st_le16(&cd->command, STORE_WORD | INTR_ALWAYS); 925 st_le32(&cd->phy_addr, virt_to_bus(cd-2)); 926 st_le32(&cd->cmd_dep, DBDMA_STOP); 927 } 928 cd++; 929 st_le16(&cd->command, DBDMA_NOP | BR_ALWAYS); 930 st_le32(&cd->cmd_dep, virt_to_bus(info->rx_cmds[0])); 931 932 /* setup DMA to our liking */ 933 dbdma_reset(&info->rx->dma); 934 st_le32(&info->rx->dma.intr_sel, 0x10001); 935 st_le32(&info->rx->dma.br_sel, 0x10001); 936 out_le32(&info->rx->dma.wait_sel, 0x10001); 937 938 /* set various flags */ 939 info->rx_ubuf = 0; 940 info->rx_cbuf = 0; 941 info->rx_fbuf = info->rx_ubuf + 1; 942 if (info->rx_fbuf == info->rx_nbuf) 943 info->rx_fbuf = RX_NO_FBUF; 944 info->rx_done_bytes = 0; 945 946 /* setup polling */ 947 init_timer(&info->poll_dma_timer); 948 info->poll_dma_timer.function = (void *)&poll_rxdma; 949 info->poll_dma_timer.data = (unsigned long)info; 950 951 info->dma_initted = 1; 952} 953 954static void fix_zero_bug_scc(struct mac_serial * info) 955{ 956 write_zsreg(info->zs_channel, 9, 957 (info->zs_channel == info->zs_chan_a? CHRA: CHRB)); 958 udelay(10); 959 write_zsreg(info->zs_channel, 9, 960 ((info->zs_channel == info->zs_chan_a? CHRA: CHRB) | NV)); 961 962 write_zsreg(info->zs_channel, 4, (X1CLK | EXTSYNC)); 963 964 /* I think this is wrong....but, I just copying code.... 965 */ 966 write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE)); 967 968 write_zsreg(info->zs_channel, 5, (8 & ~TxENAB)); 969 write_zsreg(info->zs_channel, 9, NV); /* Didn't we already do this? */ 970 write_zsreg(info->zs_channel, 11, (RCBR | TCBR)); 971 write_zsreg(info->zs_channel, 12, 0); 972 write_zsreg(info->zs_channel, 13, 0); 973 write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR)); 974 write_zsreg(info->zs_channel, 14, (LOOPBAK | SSBR | BRENABL)); 975 write_zsreg(info->zs_channel, 3, (8 | RxENABLE)); 976 write_zsreg(info->zs_channel, 0, RES_EXT_INT); 977 write_zsreg(info->zs_channel, 0, RES_EXT_INT); /* to kill some time */ 978 979 /* The channel should be OK now, but it is probably receiving 980 * loopback garbage. 981 * Switch to asynchronous mode, disable the receiver, 982 * and discard everything in the receive buffer. 983 */ 984 write_zsreg(info->zs_channel, 9, NV); 985 write_zsreg(info->zs_channel, 4, PAR_ENA); 986 write_zsreg(info->zs_channel, 3, (8 & ~RxENABLE)); 987 988 while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV) { 989 (void)read_zsreg(info->zs_channel, 8); 990 write_zsreg(info->zs_channel, 0, RES_EXT_INT); 991 write_zsreg(info->zs_channel, 0, ERR_RES); 992 } 993} 994 995static int setup_scc(struct mac_serial * info) 996{ 997 unsigned long flags; 998 999 OPNDBG("setting up ttyS%d SCC...\n", info->line); 1000 1001 save_flags(flags); cli(); /* Disable interrupts */ 1002 1003 /* Nice buggy HW ... */ 1004 fix_zero_bug_scc(info); 1005 1006 /* 1007 * Reset the chip. 1008 */ 1009 write_zsreg(info->zs_channel, 9, 1010 (info->zs_channel == info->zs_chan_a? CHRA: CHRB)); 1011 udelay(10); 1012 write_zsreg(info->zs_channel, 9, 0); 1013 1014 /* 1015 * Clear the receive FIFO. 1016 */ 1017 ZS_CLEARFIFO(info->zs_channel); 1018 info->xmit_fifo_size = 1; 1019 1020 /* 1021 * Reset DMAs 1022 */ 1023 if (info->has_dma) 1024 dma_init(info); 1025 1026 /* 1027 * Clear the interrupt registers. 1028 */ 1029 write_zsreg(info->zs_channel, 0, ERR_RES); 1030 write_zsreg(info->zs_channel, 0, RES_H_IUS); 1031 1032 /* 1033 * Turn on RTS and DTR. 1034 */ 1035 if (!info->is_irda) 1036 zs_rtsdtr(info, 1); 1037 1038 /* 1039 * Finally, enable sequencing and interrupts 1040 */ 1041 if (!info->dma_initted) { 1042 /* interrupt on ext/status changes, all received chars, 1043 transmit ready */ 1044 info->curregs[1] = (info->curregs[1] & ~0x18) 1045 | (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB); 1046 } else { 1047 /* interrupt on ext/status changes, W/Req pin is 1048 receive DMA request */ 1049 info->curregs[1] = (info->curregs[1] & ~(0x18 | TxINT_ENAB)) 1050 | (EXT_INT_ENAB | WT_RDY_RT | WT_FN_RDYFN); 1051 write_zsreg(info->zs_channel, 1, info->curregs[1]); 1052 /* enable W/Req pin */ 1053 info->curregs[1] |= WT_RDY_ENAB; 1054 write_zsreg(info->zs_channel, 1, info->curregs[1]); 1055 /* enable interrupts on transmit ready and receive errors */ 1056 info->curregs[1] |= INT_ERR_Rx | TxINT_ENAB; 1057 } 1058 info->pendregs[1] = info->curregs[1]; 1059 info->curregs[3] |= (RxENABLE | Rx8); 1060 info->pendregs[3] = info->curregs[3]; 1061 info->curregs[5] |= (TxENAB | Tx8); 1062 info->pendregs[5] = info->curregs[5]; 1063 info->curregs[9] |= (NV | MIE); 1064 info->pendregs[9] = info->curregs[9]; 1065 write_zsreg(info->zs_channel, 3, info->curregs[3]); 1066 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1067 write_zsreg(info->zs_channel, 9, info->curregs[9]); 1068 1069 if (info->tty) 1070 clear_bit(TTY_IO_ERROR, &info->tty->flags); 1071 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; 1072 1073 /* 1074 * Set the speed of the serial port 1075 */ 1076 change_speed(info, 0); 1077 1078 /* Save the current value of RR0 */ 1079 info->read_reg_zero = read_zsreg(info->zs_channel, 0); 1080 1081 restore_flags(flags); 1082 1083 if (info->dma_initted) { 1084 spin_lock_irqsave(&info->rx_dma_lock, flags); 1085 rxdma_start(info, 0); 1086 info->poll_dma_timer.expires = RX_DMA_TIMER; 1087 add_timer(&info->poll_dma_timer); 1088 spin_unlock_irqrestore(&info->rx_dma_lock, flags); 1089 } 1090 1091 return 0; 1092} 1093 1094/* 1095 * This routine will shutdown a serial port; interrupts are disabled, and 1096 * DTR is dropped if the hangup on close termio flag is on. 1097 */ 1098static void shutdown(struct mac_serial * info) 1099{ 1100 OPNDBG("Shutting down serial port %d (irq %d)....\n", info->line, 1101 info->irq); 1102 1103 if (!(info->flags & ZILOG_INITIALIZED)) { 1104 OPNDBG("(already shutdown)\n"); 1105 return; 1106 } 1107 1108 if (info->has_dma) { 1109 del_timer(&info->poll_dma_timer); 1110 dbdma_reset(info->tx_dma); 1111 dbdma_reset(&info->rx->dma); 1112 disable_irq(info->tx_dma_irq); 1113 disable_irq(info->rx_dma_irq); 1114 } 1115 disable_irq(info->irq); 1116 1117 info->pendregs[1] = info->curregs[1] = 0; 1118 write_zsreg(info->zs_channel, 1, 0); /* no interrupts */ 1119 1120 info->curregs[3] &= ~RxENABLE; 1121 info->pendregs[3] = info->curregs[3]; 1122 write_zsreg(info->zs_channel, 3, info->curregs[3]); 1123 1124 info->curregs[5] &= ~TxENAB; 1125 if (!info->tty || C_HUPCL(info->tty)) 1126 info->curregs[5] &= ~DTR; 1127 info->pendregs[5] = info->curregs[5]; 1128 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1129 1130 if (info->tty) 1131 set_bit(TTY_IO_ERROR, &info->tty->flags); 1132 1133 set_scc_power(info, 0); 1134 1135 if (info->xmit_buf) { 1136 free_page((unsigned long) info->xmit_buf); 1137 info->xmit_buf = 0; 1138 } 1139 1140 if (info->has_dma && info->dma_priv) { 1141 kfree(info->dma_priv); 1142 info->dma_priv = NULL; 1143 info->dma_initted = 0; 1144 } 1145 1146 memset(info->curregs, 0, sizeof(info->curregs)); 1147 memset(info->pendregs, 0, sizeof(info->pendregs)); 1148 1149 info->flags &= ~ZILOG_INITIALIZED; 1150} 1151 1152/* 1153 * Turn power on or off to the SCC and associated stuff 1154 * (port drivers, modem, IR port, etc.) 1155 * Returns the number of milliseconds we should wait before 1156 * trying to use the port. 1157 */ 1158static int set_scc_power(struct mac_serial * info, int state) 1159{ 1160 int delay = 0; 1161 1162 if (state) { 1163 PWRDBG("ttyS%d: powering up hardware\n", info->line); 1164 pmac_call_feature( 1165 PMAC_FTR_SCC_ENABLE, 1166 info->dev_node, info->port_type, 1); 1167 if (info->is_internal_modem) { 1168 pmac_call_feature( 1169 PMAC_FTR_MODEM_ENABLE, 1170 info->dev_node, 0, 1); 1171 delay = 2500; /* wait for 2.5s before using */ 1172 } else if (info->is_irda) 1173 mdelay(50); /* Do better here once the problems 1174 * with blocking have been ironed out 1175 */ 1176 } else { 1177 /* TODO: Make that depend on a timer, don't power down 1178 * immediately 1179 */ 1180 PWRDBG("ttyS%d: shutting down hardware\n", info->line); 1181 if (info->is_internal_modem) { 1182 PWRDBG("ttyS%d: shutting down modem\n", info->line); 1183 pmac_call_feature( 1184 PMAC_FTR_MODEM_ENABLE, 1185 info->dev_node, 0, 0); 1186 } 1187 pmac_call_feature( 1188 PMAC_FTR_SCC_ENABLE, 1189 info->dev_node, info->port_type, 0); 1190 } 1191 return delay; 1192} 1193 1194static void irda_rts_pulses(struct mac_serial *info, int w) 1195{ 1196 unsigned long flags; 1197 1198 udelay(w); 1199 save_flags(flags); cli(); 1200 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB); 1201 udelay(2); 1202 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS); 1203 udelay(8); 1204 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB); 1205 udelay(4); 1206 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS); 1207 restore_flags(flags); 1208} 1209 1210/* 1211 * Set the irda codec on the imac to the specified baud rate. 1212 */ 1213static void irda_setup(struct mac_serial *info) 1214{ 1215 int code, speed, t; 1216 unsigned long flags; 1217 1218 speed = info->tty->termios->c_cflag & CBAUD; 1219 if (speed < B2400 || speed > B115200) 1220 return; 1221 code = 0x4d + B115200 - speed; 1222 1223 /* disable serial interrupts and receive DMA */ 1224 write_zsreg(info->zs_channel, 1, info->curregs[1] & ~0x9f); 1225 1226 /* wait for transmitter to drain */ 1227 t = 10000; 1228 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0 1229 || (read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) { 1230 if (--t <= 0) { 1231 printk(KERN_ERR "transmitter didn't drain\n"); 1232 return; 1233 } 1234 udelay(10); 1235 } 1236 udelay(100); 1237 1238 /* set to 8 bits, no parity, 19200 baud, RTS on, DTR off */ 1239 write_zsreg(info->zs_channel, 4, X16CLK | SB1); 1240 write_zsreg(info->zs_channel, 11, TCBR | RCBR); 1241 t = BPS_TO_BRG(19200, ZS_CLOCK/16); 1242 write_zsreg(info->zs_channel, 12, t); 1243 write_zsreg(info->zs_channel, 13, t >> 8); 1244 write_zsreg(info->zs_channel, 14, BRENABL); 1245 write_zsreg(info->zs_channel, 3, Rx8 | RxENABLE); 1246 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS); 1247 1248 /* set TxD low for ~104us and pulse RTS */ 1249 udelay(1000); 1250 save_flags(flags); cli(); 1251 write_zsdata(info->zs_channel, 0xfe); 1252 irda_rts_pulses(info, 150); 1253 restore_flags(flags); 1254 irda_rts_pulses(info, 180); 1255 irda_rts_pulses(info, 50); 1256 udelay(100); 1257 1258 /* assert DTR, wait 30ms, talk to the chip */ 1259 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS | DTR); 1260 mdelay(30); 1261 while (read_zsreg(info->zs_channel, 0) & Rx_CH_AV) 1262 read_zsdata(info->zs_channel); 1263 1264 write_zsdata(info->zs_channel, 1); 1265 t = 1000; 1266 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) { 1267 if (--t <= 0) { 1268 printk(KERN_ERR "irda_setup timed out on 1st byte\n"); 1269 goto out; 1270 } 1271 udelay(10); 1272 } 1273 t = read_zsdata(info->zs_channel); 1274 if (t != 4) 1275 printk(KERN_ERR "irda_setup 1st byte = %x\n", t); 1276 1277 write_zsdata(info->zs_channel, code); 1278 t = 1000; 1279 while ((read_zsreg(info->zs_channel, 0) & Rx_CH_AV) == 0) { 1280 if (--t <= 0) { 1281 printk(KERN_ERR "irda_setup timed out on 2nd byte\n"); 1282 goto out; 1283 } 1284 udelay(10); 1285 } 1286 t = read_zsdata(info->zs_channel); 1287 if (t != code) 1288 printk(KERN_ERR "irda_setup 2nd byte = %x (%x)\n", t, code); 1289 1290 /* Drop DTR again and do some more RTS pulses */ 1291 out: 1292 udelay(100); 1293 write_zsreg(info->zs_channel, 5, Tx8 | TxENAB | RTS); 1294 irda_rts_pulses(info, 80); 1295 1296 /* We should be right to go now. We assume that load_zsregs 1297 will get called soon to load up the correct baud rate etc. */ 1298 info->curregs[5] = (info->curregs[5] | RTS) & ~DTR; 1299 info->pendregs[5] = info->curregs[5]; 1300} 1301 1302/* 1303 * This routine is called to set the UART divisor registers to match 1304 * the specified baud rate for a serial port. 1305 */ 1306static void change_speed(struct mac_serial *info, struct termios *old_termios) 1307{ 1308 unsigned cflag; 1309 int bits; 1310 int brg, baud; 1311 unsigned long flags; 1312 1313 if (!info->tty || !info->tty->termios) 1314 return; 1315 1316 cflag = info->tty->termios->c_cflag; 1317 baud = tty_get_baud_rate(info->tty); 1318 if (baud == 0) { 1319 if (old_termios) { 1320 info->tty->termios->c_cflag &= ~CBAUD; 1321 info->tty->termios->c_cflag |= (old_termios->c_cflag & CBAUD); 1322 cflag = info->tty->termios->c_cflag; 1323 baud = tty_get_baud_rate(info->tty); 1324 } 1325 else 1326 baud = info->zs_baud; 1327 } 1328 if (baud > 230400) 1329 baud = 230400; 1330 else if (baud == 0) 1331 baud = 38400; 1332 1333 save_flags(flags); cli(); 1334 info->zs_baud = baud; 1335 info->clk_divisor = 16; 1336 1337 BAUDBG(KERN_DEBUG "set speed to %d bds, ", baud); 1338 1339 switch (baud) { 1340 case ZS_CLOCK/16: /* 230400 */ 1341 info->curregs[4] = X16CLK; 1342 info->curregs[11] = 0; 1343 break; 1344 case ZS_CLOCK/32: /* 115200 */ 1345 info->curregs[4] = X32CLK; 1346 info->curregs[11] = 0; 1347 break; 1348 default: 1349 info->curregs[4] = X16CLK; 1350 info->curregs[11] = TCBR | RCBR; 1351 brg = BPS_TO_BRG(baud, ZS_CLOCK/info->clk_divisor); 1352 info->curregs[12] = (brg & 255); 1353 info->curregs[13] = ((brg >> 8) & 255); 1354 info->curregs[14] = BRENABL; 1355 } 1356 1357 /* byte size and parity */ 1358 info->curregs[3] &= ~RxNBITS_MASK; 1359 info->curregs[5] &= ~TxNBITS_MASK; 1360 switch (cflag & CSIZE) { 1361 case CS5: 1362 info->curregs[3] |= Rx5; 1363 info->curregs[5] |= Tx5; 1364 BAUDBG("5 bits, "); 1365 bits = 7; 1366 break; 1367 case CS6: 1368 info->curregs[3] |= Rx6; 1369 info->curregs[5] |= Tx6; 1370 BAUDBG("6 bits, "); 1371 bits = 8; 1372 break; 1373 case CS7: 1374 info->curregs[3] |= Rx7; 1375 info->curregs[5] |= Tx7; 1376 BAUDBG("7 bits, "); 1377 bits = 9; 1378 break; 1379 case CS8: 1380 default: /* defaults to 8 bits */ 1381 info->curregs[3] |= Rx8; 1382 info->curregs[5] |= Tx8; 1383 BAUDBG("8 bits, "); 1384 bits = 10; 1385 break; 1386 } 1387 info->pendregs[3] = info->curregs[3]; 1388 info->pendregs[5] = info->curregs[5]; 1389 1390 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN); 1391 if (cflag & CSTOPB) { 1392 info->curregs[4] |= SB2; 1393 bits++; 1394 BAUDBG("2 stop, "); 1395 } else { 1396 info->curregs[4] |= SB1; 1397 BAUDBG("1 stop, "); 1398 } 1399 if (cflag & PARENB) { 1400 bits++; 1401 info->curregs[4] |= PAR_ENA; 1402 BAUDBG("parity, "); 1403 } 1404 if (!(cflag & PARODD)) { 1405 info->curregs[4] |= PAR_EVEN; 1406 } 1407 info->pendregs[4] = info->curregs[4]; 1408 1409 if (!(cflag & CLOCAL)) { 1410 if (!(info->curregs[15] & DCDIE)) 1411 info->read_reg_zero = read_zsreg(info->zs_channel, 0); 1412 info->curregs[15] |= DCDIE; 1413 } else 1414 info->curregs[15] &= ~DCDIE; 1415 if (cflag & CRTSCTS) { 1416 info->curregs[15] |= CTSIE; 1417 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0) 1418 info->tx_stopped = 1; 1419 } else { 1420 info->curregs[15] &= ~CTSIE; 1421 info->tx_stopped = 0; 1422 } 1423 info->pendregs[15] = info->curregs[15]; 1424 1425 /* Calc timeout value. This is pretty broken with high baud rates with HZ=100. 1426 This code would love a larger HZ and a >1 fifo size, but this is not 1427 a priority. The resulting value must be >HZ/2 1428 */ 1429 info->timeout = ((info->xmit_fifo_size*HZ*bits) / baud); 1430 info->timeout += HZ/50+1; /* Add .02 seconds of slop */ 1431 1432 BAUDBG("timeout=%d/%ds, base:%d\n", (int)info->timeout, (int)HZ, 1433 (int)info->baud_base); 1434 1435 /* set the irda codec to the right rate */ 1436 if (info->is_irda) 1437 irda_setup(info); 1438 1439 /* Load up the new values */ 1440 load_zsregs(info->zs_channel, info->curregs); 1441 1442 restore_flags(flags); 1443} 1444 1445static void rs_flush_chars(struct tty_struct *tty) 1446{ 1447 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1448 1449 if (serial_paranoia_check(info, tty->device, "rs_flush_chars")) 1450 return; 1451 1452 if (info->xmit_cnt <= 0 || tty->stopped || info->tx_stopped || 1453 !info->xmit_buf) 1454 return; 1455 1456 /* Enable transmitter */ 1457 transmit_chars(info); 1458} 1459 1460static int rs_write(struct tty_struct * tty, int from_user, 1461 const unsigned char *buf, int count) 1462{ 1463 int c, ret = 0; 1464 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1465 unsigned long flags; 1466 1467 if (serial_paranoia_check(info, tty->device, "rs_write")) 1468 return 0; 1469 1470 if (!tty || !info->xmit_buf || !tmp_buf) 1471 return 0; 1472 1473 if (from_user) { 1474 down(&tmp_buf_sem); 1475 while (1) { 1476 c = MIN(count, 1477 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, 1478 SERIAL_XMIT_SIZE - info->xmit_head)); 1479 if (c <= 0) 1480 break; 1481 1482 c -= copy_from_user(tmp_buf, buf, c); 1483 if (!c) { 1484 if (!ret) 1485 ret = -EFAULT; 1486 break; 1487 } 1488 save_flags(flags); 1489 cli(); 1490 c = MIN(c, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, 1491 SERIAL_XMIT_SIZE - info->xmit_head)); 1492 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c); 1493 info->xmit_head = ((info->xmit_head + c) & 1494 (SERIAL_XMIT_SIZE-1)); 1495 info->xmit_cnt += c; 1496 restore_flags(flags); 1497 buf += c; 1498 count -= c; 1499 ret += c; 1500 } 1501 up(&tmp_buf_sem); 1502 } else { 1503 while (1) { 1504 save_flags(flags); 1505 cli(); 1506 c = MIN(count, 1507 MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, 1508 SERIAL_XMIT_SIZE - info->xmit_head)); 1509 if (c <= 0) { 1510 restore_flags(flags); 1511 break; 1512 } 1513 memcpy(info->xmit_buf + info->xmit_head, buf, c); 1514 info->xmit_head = ((info->xmit_head + c) & 1515 (SERIAL_XMIT_SIZE-1)); 1516 info->xmit_cnt += c; 1517 restore_flags(flags); 1518 buf += c; 1519 count -= c; 1520 ret += c; 1521 } 1522 } 1523 if (info->xmit_cnt && !tty->stopped && !info->tx_stopped 1524 && !info->tx_active) 1525 transmit_chars(info); 1526 return ret; 1527} 1528 1529static int rs_write_room(struct tty_struct *tty) 1530{ 1531 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1532 int ret; 1533 1534 if (serial_paranoia_check(info, tty->device, "rs_write_room")) 1535 return 0; 1536 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1; 1537 if (ret < 0) 1538 ret = 0; 1539 return ret; 1540} 1541 1542static int rs_chars_in_buffer(struct tty_struct *tty) 1543{ 1544 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1545 1546 if (serial_paranoia_check(info, tty->device, "rs_chars_in_buffer")) 1547 return 0; 1548 return info->xmit_cnt; 1549} 1550 1551static void rs_flush_buffer(struct tty_struct *tty) 1552{ 1553 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1554 unsigned long flags; 1555 1556 if (serial_paranoia_check(info, tty->device, "rs_flush_buffer")) 1557 return; 1558 save_flags(flags); cli(); 1559 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; 1560 restore_flags(flags); 1561 wake_up_interruptible(&tty->write_wait); 1562 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) && 1563 tty->ldisc.write_wakeup) 1564 (tty->ldisc.write_wakeup)(tty); 1565} 1566 1567/* 1568 * ------------------------------------------------------------ 1569 * rs_throttle() 1570 * 1571 * This routine is called by the upper-layer tty layer to signal that 1572 * incoming characters should be throttled. 1573 * ------------------------------------------------------------ 1574 */ 1575static void rs_throttle(struct tty_struct * tty) 1576{ 1577 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1578 unsigned long flags; 1579#ifdef SERIAL_DEBUG_THROTTLE 1580 printk(KERN_DEBUG "throttle %ld....\n",tty->ldisc.chars_in_buffer(tty)); 1581#endif 1582 1583 if (serial_paranoia_check(info, tty->device, "rs_throttle")) 1584 return; 1585 1586 if (I_IXOFF(tty)) { 1587 save_flags(flags); cli(); 1588 info->x_char = STOP_CHAR(tty); 1589 if (!info->tx_active) 1590 transmit_chars(info); 1591 restore_flags(flags); 1592 } 1593 1594 if (C_CRTSCTS(tty)) { 1595 /* 1596 * Here we want to turn off the RTS line. On Macintoshes, 1597 * the external serial ports using a DIN-8 or DIN-9 1598 * connector only have the DTR line (which is usually 1599 * wired to both RTS and DTR on an external modem in 1600 * the cable). RTS doesn't go out to the serial port 1601 * socket, it acts as an output enable for the transmit 1602 * data line. So in this case we don't drop RTS. 1603 * 1604 * Macs with internal modems generally do have both RTS 1605 * and DTR wired to the modem, so in that case we do 1606 * drop RTS. 1607 */ 1608 if (info->is_internal_modem) { 1609 save_flags(flags); cli(); 1610 info->curregs[5] &= ~RTS; 1611 info->pendregs[5] &= ~RTS; 1612 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1613 restore_flags(flags); 1614 } 1615 } 1616 1617#ifdef CDTRCTS 1618 if (tty->termios->c_cflag & CDTRCTS) { 1619 save_flags(flags); cli(); 1620 info->curregs[5] &= ~DTR; 1621 info->pendregs[5] &= ~DTR; 1622 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1623 restore_flags(flags); 1624 } 1625#endif /* CDTRCTS */ 1626} 1627 1628static void rs_unthrottle(struct tty_struct * tty) 1629{ 1630 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1631 unsigned long flags; 1632#ifdef SERIAL_DEBUG_THROTTLE 1633 printk(KERN_DEBUG "unthrottle %s: %d....\n", 1634 tty->ldisc.chars_in_buffer(tty)); 1635#endif 1636 1637 if (serial_paranoia_check(info, tty->device, "rs_unthrottle")) 1638 return; 1639 1640 if (I_IXOFF(tty)) { 1641 save_flags(flags); cli(); 1642 if (info->x_char) 1643 info->x_char = 0; 1644 else { 1645 info->x_char = START_CHAR(tty); 1646 if (!info->tx_active) 1647 transmit_chars(info); 1648 } 1649 restore_flags(flags); 1650 } 1651 1652 if (C_CRTSCTS(tty) && info->is_internal_modem) { 1653 /* Assert RTS line */ 1654 save_flags(flags); cli(); 1655 info->curregs[5] |= RTS; 1656 info->pendregs[5] |= RTS; 1657 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1658 restore_flags(flags); 1659 } 1660 1661#ifdef CDTRCTS 1662 if (tty->termios->c_cflag & CDTRCTS) { 1663 /* Assert DTR line */ 1664 save_flags(flags); cli(); 1665 info->curregs[5] |= DTR; 1666 info->pendregs[5] |= DTR; 1667 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1668 restore_flags(flags); 1669 } 1670#endif 1671} 1672 1673/* 1674 * ------------------------------------------------------------ 1675 * rs_ioctl() and friends 1676 * ------------------------------------------------------------ 1677 */ 1678 1679static int get_serial_info(struct mac_serial * info, 1680 struct serial_struct * retinfo) 1681{ 1682 struct serial_struct tmp; 1683 1684 if (!retinfo) 1685 return -EFAULT; 1686 memset(&tmp, 0, sizeof(tmp)); 1687 tmp.type = info->type; 1688 tmp.line = info->line; 1689 tmp.port = info->port; 1690 tmp.irq = info->irq; 1691 tmp.flags = info->flags; 1692 tmp.baud_base = info->baud_base; 1693 tmp.close_delay = info->close_delay; 1694 tmp.closing_wait = info->closing_wait; 1695 tmp.custom_divisor = info->custom_divisor; 1696 if (copy_to_user(retinfo,&tmp,sizeof(*retinfo))) 1697 return -EFAULT; 1698 return 0; 1699} 1700 1701static int set_serial_info(struct mac_serial * info, 1702 struct serial_struct * new_info) 1703{ 1704 struct serial_struct new_serial; 1705 struct mac_serial old_info; 1706 int retval = 0; 1707 1708 if (copy_from_user(&new_serial,new_info,sizeof(new_serial))) 1709 return -EFAULT; 1710 old_info = *info; 1711 1712 if (!capable(CAP_SYS_ADMIN)) { 1713 if ((new_serial.baud_base != info->baud_base) || 1714 (new_serial.type != info->type) || 1715 (new_serial.close_delay != info->close_delay) || 1716 ((new_serial.flags & ~ZILOG_USR_MASK) != 1717 (info->flags & ~ZILOG_USR_MASK))) 1718 return -EPERM; 1719 info->flags = ((info->flags & ~ZILOG_USR_MASK) | 1720 (new_serial.flags & ZILOG_USR_MASK)); 1721 info->custom_divisor = new_serial.custom_divisor; 1722 goto check_and_exit; 1723 } 1724 1725 if (info->count > 1) 1726 return -EBUSY; 1727 1728 /* 1729 * OK, past this point, all the error checking has been done. 1730 * At this point, we start making changes..... 1731 */ 1732 1733 info->baud_base = new_serial.baud_base; 1734 info->flags = ((info->flags & ~ZILOG_FLAGS) | 1735 (new_serial.flags & ZILOG_FLAGS)); 1736 info->type = new_serial.type; 1737 info->close_delay = new_serial.close_delay; 1738 info->closing_wait = new_serial.closing_wait; 1739 1740check_and_exit: 1741 if (info->flags & ZILOG_INITIALIZED) 1742 retval = setup_scc(info); 1743 return retval; 1744} 1745 1746/* 1747 * get_lsr_info - get line status register info 1748 * 1749 * Purpose: Let user call ioctl() to get info when the UART physically 1750 * is emptied. On bus types like RS485, the transmitter must 1751 * release the bus after transmitting. This must be done when 1752 * the transmit shift register is empty, not be done when the 1753 * transmit holding register is empty. This functionality 1754 * allows an RS485 driver to be written in user space. 1755 */ 1756static int get_lsr_info(struct mac_serial * info, unsigned int *value) 1757{ 1758 unsigned char status; 1759 unsigned long flags; 1760 1761 save_flags(flags); cli(); 1762 status = read_zsreg(info->zs_channel, 0); 1763 restore_flags(flags); 1764 status = (status & Tx_BUF_EMP)? TIOCSER_TEMT: 0; 1765 return put_user(status,value); 1766} 1767 1768static int get_modem_info(struct mac_serial *info, unsigned int *value) 1769{ 1770 unsigned char control, status; 1771 unsigned int result; 1772 unsigned long flags; 1773 1774 save_flags(flags); cli(); 1775 control = info->curregs[5]; 1776 status = read_zsreg(info->zs_channel, 0); 1777 restore_flags(flags); 1778 result = ((control & RTS) ? TIOCM_RTS: 0) 1779 | ((control & DTR) ? TIOCM_DTR: 0) 1780 | ((status & DCD) ? TIOCM_CAR: 0) 1781 | ((status & CTS) ? 0: TIOCM_CTS); 1782 return put_user(result,value); 1783} 1784 1785static int set_modem_info(struct mac_serial *info, unsigned int cmd, 1786 unsigned int *value) 1787{ 1788 unsigned int arg, bits; 1789 unsigned long flags; 1790 1791 if (get_user(arg, value)) 1792 return -EFAULT; 1793 bits = (arg & TIOCM_RTS? RTS: 0) + (arg & TIOCM_DTR? DTR: 0); 1794 save_flags(flags); cli(); 1795 switch (cmd) { 1796 case TIOCMBIS: 1797 info->curregs[5] |= bits; 1798 break; 1799 case TIOCMBIC: 1800 info->curregs[5] &= ~bits; 1801 break; 1802 case TIOCMSET: 1803 info->curregs[5] = (info->curregs[5] & ~(DTR | RTS)) | bits; 1804 break; 1805 default: 1806 restore_flags(flags); 1807 return -EINVAL; 1808 } 1809 info->pendregs[5] = info->curregs[5]; 1810 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1811 restore_flags(flags); 1812 return 0; 1813} 1814 1815/* 1816 * rs_break - turn transmit break condition on/off 1817 */ 1818static void rs_break(struct tty_struct *tty, int break_state) 1819{ 1820 struct mac_serial *info = (struct mac_serial *) tty->driver_data; 1821 unsigned long flags; 1822 1823 if (serial_paranoia_check(info, tty->device, "rs_break")) 1824 return; 1825 1826 save_flags(flags); cli(); 1827 if (break_state == -1) 1828 info->curregs[5] |= SND_BRK; 1829 else 1830 info->curregs[5] &= ~SND_BRK; 1831 write_zsreg(info->zs_channel, 5, info->curregs[5]); 1832 restore_flags(flags); 1833} 1834 1835static int rs_ioctl(struct tty_struct *tty, struct file * file, 1836 unsigned int cmd, unsigned long arg) 1837{ 1838 struct mac_serial * info = (struct mac_serial *)tty->driver_data; 1839 1840#ifdef CONFIG_KGDB 1841 if (info->kgdb_channel) 1842 return -ENODEV; 1843#endif 1844 if (serial_paranoia_check(info, tty->device, "rs_ioctl")) 1845 return -ENODEV; 1846 1847 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && 1848 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGSTRUCT)) { 1849 if (tty->flags & (1 << TTY_IO_ERROR)) 1850 return -EIO; 1851 } 1852 1853 switch (cmd) { 1854 case TIOCMGET: 1855 return get_modem_info(info, (unsigned int *) arg); 1856 case TIOCMBIS: 1857 case TIOCMBIC: 1858 case TIOCMSET: 1859 return set_modem_info(info, cmd, (unsigned int *) arg); 1860 case TIOCGSERIAL: 1861 return get_serial_info(info, 1862 (struct serial_struct *) arg); 1863 case TIOCSSERIAL: 1864 return set_serial_info(info, 1865 (struct serial_struct *) arg); 1866 case TIOCSERGETLSR: /* Get line status register */ 1867 return get_lsr_info(info, (unsigned int *) arg); 1868 1869 case TIOCSERGSTRUCT: 1870 if (copy_to_user((struct mac_serial *) arg, 1871 info, sizeof(struct mac_serial))) 1872 return -EFAULT; 1873 return 0; 1874 1875 default: 1876 return -ENOIOCTLCMD; 1877 } 1878 return 0; 1879} 1880 1881static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios) 1882{ 1883 struct mac_serial *info = (struct mac_serial *)tty->driver_data; 1884 int was_stopped; 1885 1886 if (tty->termios->c_cflag == old_termios->c_cflag) 1887 return; 1888 was_stopped = info->tx_stopped; 1889 1890 change_speed(info, old_termios); 1891 1892 if (was_stopped && !info->tx_stopped) { 1893 tty->hw_stopped = 0; 1894 rs_start(tty); 1895 } 1896} 1897 1898/* 1899 * ------------------------------------------------------------ 1900 * rs_close() 1901 * 1902 * This routine is called when the serial port gets closed. 1903 * Wait for the last remaining data to be sent. 1904 * ------------------------------------------------------------ 1905 */ 1906static void rs_close(struct tty_struct *tty, struct file * filp) 1907{ 1908 struct mac_serial * info = (struct mac_serial *)tty->driver_data; 1909 unsigned long flags; 1910 1911 if (!info || serial_paranoia_check(info, tty->device, "rs_close")) 1912 return; 1913 1914 save_flags(flags); cli(); 1915 1916 if (tty_hung_up_p(filp)) { 1917 MOD_DEC_USE_COUNT; 1918 restore_flags(flags); 1919 return; 1920 } 1921 1922 OPNDBG("rs_close ttyS%d, count = %d\n", info->line, info->count); 1923 if ((tty->count == 1) && (info->count != 1)) { 1924 /* 1925 * Uh, oh. tty->count is 1, which means that the tty 1926 * structure will be freed. Info->count should always 1927 * be one in these conditions. If it's greater than 1928 * one, we've got real problems, since it means the 1929 * serial port won't be shutdown. 1930 */ 1931 printk(KERN_ERR "rs_close: bad serial port count; tty->count " 1932 "is 1, info->count is %d\n", info->count); 1933 info->count = 1; 1934 } 1935 if (--info->count < 0) { 1936 printk(KERN_ERR "rs_close: bad serial port count for " 1937 "ttyS%d: %d\n", info->line, info->count); 1938 info->count = 0; 1939 } 1940 if (info->count) { 1941 MOD_DEC_USE_COUNT; 1942 restore_flags(flags); 1943 return; 1944 } 1945 info->flags |= ZILOG_CLOSING; 1946 /* 1947 * Save the termios structure, since this port may have 1948 * separate termios for callout and dialin. 1949 */ 1950 if (info->flags & ZILOG_NORMAL_ACTIVE) 1951 info->normal_termios = *tty->termios; 1952 if (info->flags & ZILOG_CALLOUT_ACTIVE) 1953 info->callout_termios = *tty->termios; 1954 /* 1955 * Now we wait for the transmit buffer to clear; and we notify 1956 * the line discipline to only process XON/XOFF characters. 1957 */ 1958 OPNDBG("waiting end of Tx... (timeout:%d)\n", info->closing_wait); 1959 tty->closing = 1; 1960 if (info->closing_wait != ZILOG_CLOSING_WAIT_NONE) { 1961 restore_flags(flags); 1962 tty_wait_until_sent(tty, info->closing_wait); 1963 save_flags(flags); cli(); 1964 } 1965 1966 /* 1967 * At this point we stop accepting input. To do this, we 1968 * disable the receiver and receive interrupts. 1969 */ 1970 info->curregs[3] &= ~RxENABLE; 1971 info->pendregs[3] = info->curregs[3]; 1972 write_zsreg(info->zs_channel, 3, info->curregs[3]); 1973 info->curregs[1] &= ~(0x18); /* disable any rx ints */ 1974 info->pendregs[1] = info->curregs[1]; 1975 write_zsreg(info->zs_channel, 1, info->curregs[1]); 1976 ZS_CLEARFIFO(info->zs_channel); 1977 if (info->flags & ZILOG_INITIALIZED) { 1978 /* 1979 * Before we drop DTR, make sure the SCC transmitter 1980 * has completely drained. 1981 */ 1982 OPNDBG("waiting end of Rx...\n"); 1983 restore_flags(flags); 1984 rs_wait_until_sent(tty, info->timeout); 1985 save_flags(flags); cli(); 1986 } 1987 1988 shutdown(info); 1989 /* restore flags now since shutdown() will have disabled this port's 1990 specific irqs */ 1991 restore_flags(flags); 1992 1993 if (tty->driver.flush_buffer) 1994 tty->driver.flush_buffer(tty); 1995 if (tty->ldisc.flush_buffer) 1996 tty->ldisc.flush_buffer(tty); 1997 tty->closing = 0; 1998 info->event = 0; 1999 info->tty = 0; 2000 2001 if (info->blocked_open) { 2002 if (info->close_delay) { 2003 current->state = TASK_INTERRUPTIBLE; 2004 schedule_timeout(info->close_delay); 2005 } 2006 wake_up_interruptible(&info->open_wait); 2007 } 2008 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE| 2009 ZILOG_CLOSING); 2010 wake_up_interruptible(&info->close_wait); 2011 MOD_DEC_USE_COUNT; 2012} 2013 2014/* 2015 * rs_wait_until_sent() --- wait until the transmitter is empty 2016 */ 2017static void rs_wait_until_sent(struct tty_struct *tty, int timeout) 2018{ 2019 struct mac_serial *info = (struct mac_serial *) tty->driver_data; 2020 unsigned long orig_jiffies, char_time; 2021 2022 if (serial_paranoia_check(info, tty->device, "rs_wait_until_sent")) 2023 return; 2024 2025/* printk("rs_wait_until_sent, timeout:%d, tty_stopped:%d, tx_stopped:%d\n", 2026 timeout, tty->stopped, info->tx_stopped); 2027*/ 2028 orig_jiffies = jiffies; 2029 /* 2030 * Set the check interval to be 1/5 of the estimated time to 2031 * send a single character, and make it at least 1. The check 2032 * interval should also be less than the timeout. 2033 */ 2034 if (info->timeout <= HZ/50) { 2035 printk(KERN_INFO "macserial: invalid info->timeout=%d\n", 2036 info->timeout); 2037 info->timeout = HZ/50+1; 2038 } 2039 2040 char_time = (info->timeout - HZ/50) / info->xmit_fifo_size; 2041 char_time = char_time / 5; 2042 if (char_time > HZ) { 2043 printk(KERN_WARNING "macserial: char_time %ld >HZ !!!\n", 2044 char_time); 2045 char_time = 1; 2046 } else if (char_time == 0) 2047 char_time = 1; 2048 if (timeout) 2049 char_time = MIN(char_time, timeout); 2050 while ((read_zsreg(info->zs_channel, 1) & ALL_SNT) == 0) { 2051 current->state = TASK_INTERRUPTIBLE; 2052 schedule_timeout(char_time); 2053 if (signal_pending(current)) 2054 break; 2055 if (timeout && time_after(jiffies, orig_jiffies + timeout)) 2056 break; 2057 } 2058 current->state = TASK_RUNNING; 2059} 2060 2061/* 2062 * rs_hangup() --- called by tty_hangup() when a hangup is signaled. 2063 */ 2064static void rs_hangup(struct tty_struct *tty) 2065{ 2066 struct mac_serial * info = (struct mac_serial *)tty->driver_data; 2067 2068 if (serial_paranoia_check(info, tty->device, "rs_hangup")) 2069 return; 2070 2071 rs_flush_buffer(tty); 2072 shutdown(info); 2073 info->event = 0; 2074 info->count = 0; 2075 info->flags &= ~(ZILOG_NORMAL_ACTIVE|ZILOG_CALLOUT_ACTIVE); 2076 info->tty = 0; 2077 wake_up_interruptible(&info->open_wait); 2078} 2079 2080/* 2081 * ------------------------------------------------------------ 2082 * rs_open() and friends 2083 * ------------------------------------------------------------ 2084 */ 2085static int block_til_ready(struct tty_struct *tty, struct file * filp, 2086 struct mac_serial *info) 2087{ 2088 DECLARE_WAITQUEUE(wait,current); 2089 int retval; 2090 int do_clocal = 0; 2091 2092 /* 2093 * If the device is in the middle of being closed, then block 2094 * until it's done, and then try again. 2095 */ 2096 if (info->flags & ZILOG_CLOSING) { 2097 interruptible_sleep_on(&info->close_wait); 2098#ifdef SERIAL_DO_RESTART 2099 return ((info->flags & ZILOG_HUP_NOTIFY) ? 2100 -EAGAIN : -ERESTARTSYS); 2101#else 2102 return -EAGAIN; 2103#endif 2104 } 2105 2106 /* 2107 * If this is a callout device, then just make sure the normal 2108 * device isn't being used. 2109 */ 2110 if (tty->driver.subtype == SERIAL_TYPE_CALLOUT) { 2111 if (info->flags & ZILOG_NORMAL_ACTIVE) 2112 return -EBUSY; 2113 if ((info->flags & ZILOG_CALLOUT_ACTIVE) && 2114 (info->flags & ZILOG_SESSION_LOCKOUT) && 2115 (info->session != current->session)) 2116 return -EBUSY; 2117 if ((info->flags & ZILOG_CALLOUT_ACTIVE) && 2118 (info->flags & ZILOG_PGRP_LOCKOUT) && 2119 (info->pgrp != current->pgrp)) 2120 return -EBUSY; 2121 info->flags |= ZILOG_CALLOUT_ACTIVE; 2122 return 0; 2123 } 2124 2125 /* 2126 * If non-blocking mode is set, or the port is not enabled, 2127 * then make the check up front and then exit. 2128 */ 2129 if ((filp->f_flags & O_NONBLOCK) || 2130 (tty->flags & (1 << TTY_IO_ERROR))) { 2131 if (info->flags & ZILOG_CALLOUT_ACTIVE) 2132 return -EBUSY; 2133 info->flags |= ZILOG_NORMAL_ACTIVE; 2134 return 0; 2135 } 2136 2137 if (info->flags & ZILOG_CALLOUT_ACTIVE) { 2138 if (info->normal_termios.c_cflag & CLOCAL) 2139 do_clocal = 1; 2140 } else { 2141 if (tty->termios->c_cflag & CLOCAL) 2142 do_clocal = 1; 2143 } 2144 2145 /* 2146 * Block waiting for the carrier detect and the line to become 2147 * free (i.e., not in use by the callout). While we are in 2148 * this loop, info->count is dropped by one, so that 2149 * rs_close() knows when to free things. We restore it upon 2150 * exit, either normal or abnormal. 2151 */ 2152 retval = 0; 2153 add_wait_queue(&info->open_wait, &wait); 2154 OPNDBG("block_til_ready before block: ttyS%d, count = %d\n", 2155 info->line, info->count); 2156 cli(); 2157 if (!tty_hung_up_p(filp)) 2158 info->count--; 2159 sti(); 2160 info->blocked_open++; 2161 while (1) { 2162 cli(); 2163 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) && 2164 (tty->termios->c_cflag & CBAUD) && 2165 !info->is_irda) 2166 zs_rtsdtr(info, 1); 2167 sti(); 2168 set_current_state(TASK_INTERRUPTIBLE); 2169 if (tty_hung_up_p(filp) || 2170 !(info->flags & ZILOG_INITIALIZED)) { 2171#ifdef SERIAL_DO_RESTART 2172 if (info->flags & ZILOG_HUP_NOTIFY) 2173 retval = -EAGAIN; 2174 else 2175 retval = -ERESTARTSYS; 2176#else 2177 retval = -EAGAIN; 2178#endif 2179 break; 2180 } 2181 if (!(info->flags & ZILOG_CALLOUT_ACTIVE) && 2182 !(info->flags & ZILOG_CLOSING) && 2183 (do_clocal || (read_zsreg(info->zs_channel, 0) & DCD))) 2184 break; 2185 if (signal_pending(current)) { 2186 retval = -ERESTARTSYS; 2187 break; 2188 } 2189 OPNDBG("block_til_ready blocking: ttyS%d, count = %d\n", 2190 info->line, info->count); 2191 schedule(); 2192 } 2193 current->state = TASK_RUNNING; 2194 remove_wait_queue(&info->open_wait, &wait); 2195 if (!tty_hung_up_p(filp)) 2196 info->count++; 2197 info->blocked_open--; 2198 OPNDBG("block_til_ready after blocking: ttyS%d, count = %d\n", 2199 info->line, info->count); 2200 if (retval) 2201 return retval; 2202 info->flags |= ZILOG_NORMAL_ACTIVE; 2203 return 0; 2204} 2205 2206/* 2207 * This routine is called whenever a serial port is opened. It 2208 * enables interrupts for a serial port, linking in its ZILOG structure into 2209 * the IRQ chain. It also performs the serial-specific 2210 * initialization for the tty structure. 2211 */ 2212static int rs_open(struct tty_struct *tty, struct file * filp) 2213{ 2214 struct mac_serial *info; 2215 int retval, line; 2216 unsigned long page; 2217 2218 MOD_INC_USE_COUNT; 2219 line = MINOR(tty->device) - tty->driver.minor_start; 2220 if ((line < 0) || (line >= zs_channels_found)) { 2221 MOD_DEC_USE_COUNT; 2222 return -ENODEV; 2223 } 2224 info = zs_soft + line; 2225 2226#ifdef CONFIG_KGDB 2227 if (info->kgdb_channel) { 2228 MOD_DEC_USE_COUNT; 2229 return -ENODEV; 2230 } 2231#endif 2232 if (serial_paranoia_check(info, tty->device, "rs_open")) 2233 return -ENODEV; 2234 OPNDBG("rs_open %s%d, count = %d, tty=%p\n", tty->driver.name, 2235 info->line, info->count, tty); 2236 2237 info->count++; 2238 tty->driver_data = info; 2239 info->tty = tty; 2240 2241 if (!tmp_buf) { 2242 page = get_free_page(GFP_KERNEL); 2243 if (!page) 2244 return -ENOMEM; 2245 if (tmp_buf) 2246 free_page(page); 2247 else 2248 tmp_buf = (unsigned char *) page; 2249 } 2250 2251 /* 2252 * If the port is the middle of closing, bail out now 2253 */ 2254 if (tty_hung_up_p(filp) || 2255 (info->flags & ZILOG_CLOSING)) { 2256 if (info->flags & ZILOG_CLOSING) 2257 interruptible_sleep_on(&info->close_wait); 2258#ifdef SERIAL_DO_RESTART 2259 return ((info->flags & ZILOG_HUP_NOTIFY) ? 2260 -EAGAIN : -ERESTARTSYS); 2261#else 2262 return -EAGAIN; 2263#endif 2264 } 2265 2266 /* 2267 * Start up serial port 2268 */ 2269 2270 retval = startup(info); 2271 if (retval) 2272 return retval; 2273 2274 retval = block_til_ready(tty, filp, info); 2275 if (retval) { 2276 OPNDBG("rs_open returning after block_til_ready with %d\n", 2277 retval); 2278 return retval; 2279 } 2280 2281 if ((info->count == 1) && (info->flags & ZILOG_SPLIT_TERMIOS)) { 2282 if (tty->driver.subtype == SERIAL_TYPE_NORMAL) 2283 *tty->termios = info->normal_termios; 2284 else 2285 *tty->termios = info->callout_termios; 2286 change_speed(info, 0); 2287 } 2288#ifdef CONFIG_SERIAL_CONSOLE 2289 if (sercons.cflag && sercons.index == line) { 2290 tty->termios->c_cflag = sercons.cflag; 2291 sercons.cflag = 0; 2292 change_speed(info, 0); 2293 } 2294#endif 2295 2296 info->session = current->session; 2297 info->pgrp = current->pgrp; 2298 2299 OPNDBG("rs_open ttyS%d successful...\n", info->line); 2300 return 0; 2301} 2302 2303/* Finally, routines used to initialize the serial driver. */ 2304 2305static void show_serial_version(void) 2306{ 2307 printk(KERN_INFO "PowerMac Z8530 serial driver version " MACSERIAL_VERSION "\n"); 2308} 2309 2310/* 2311 * Initialize one channel, both the mac_serial and mac_zschannel 2312 * structs. We use the dev_node field of the mac_serial struct. 2313 */ 2314static int 2315chan_init(struct mac_serial *zss, struct mac_zschannel *zs_chan, 2316 struct mac_zschannel *zs_chan_a) 2317{ 2318 struct device_node *ch = zss->dev_node; 2319 char *conn; 2320 int len; 2321 struct slot_names_prop { 2322 int count; 2323 char name[1]; 2324 } *slots; 2325 2326 zss->irq = ch->intrs[0].line; 2327 zss->has_dma = 0; 2328#if !defined(CONFIG_KGDB) && defined(SUPPORT_SERIAL_DMA) 2329 if (ch->n_addrs >= 3 && ch->n_intrs == 3) 2330 zss->has_dma = 1; 2331#endif 2332 zss->dma_initted = 0; 2333 2334 zs_chan->control = (volatile unsigned char *) 2335 ioremap(ch->addrs[0].address, 0x1000); 2336 zs_chan->data = zs_chan->control + 0x10; 2337 spin_lock_init(&zs_chan->lock); 2338 zs_chan->parent = zss; 2339 zss->zs_channel = zs_chan; 2340 zss->zs_chan_a = zs_chan_a; 2341 2342 /* setup misc varariables */ 2343 zss->kgdb_channel = 0; 2344 2345 /* For now, we assume you either have a slot-names property 2346 * with "Modem" in it, or your channel is compatible with 2347 * "cobalt". Might need additional fixups 2348 */ 2349 zss->is_internal_modem = device_is_compatible(ch, "cobalt"); 2350 conn = get_property(ch, "AAPL,connector", &len); 2351 zss->is_irda = conn && (strcmp(conn, "infrared") == 0); 2352 zss->port_type = PMAC_SCC_ASYNC; 2353 /* 1999 Powerbook G3 has slot-names property instead */ 2354 slots = (struct slot_names_prop *)get_property(ch, "slot-names", &len); 2355 if (slots && slots->count > 0) { 2356 if (strcmp(slots->name, "IrDA") == 0) 2357 zss->is_irda = 1; 2358 else if (strcmp(slots->name, "Modem") == 0) 2359 zss->is_internal_modem = 1; 2360 } 2361 if (zss->is_irda) 2362 zss->port_type = PMAC_SCC_IRDA; 2363 if (zss->is_internal_modem) { 2364 struct device_node* i2c_modem = find_devices("i2c-modem"); 2365 if (i2c_modem) { 2366 char* mid = get_property(i2c_modem, "modem-id", NULL); 2367 if (mid) switch(*mid) { 2368 case 0x04 : 2369 case 0x05 : 2370 case 0x07 : 2371 case 0x08 : 2372 case 0x0b : 2373 case 0x0c : 2374 zss->port_type = PMAC_SCC_I2S1; 2375 } 2376 printk(KERN_INFO "macserial: i2c-modem detected, id: %d\n", 2377 mid ? (*mid) : 0); 2378 } else { 2379 printk(KERN_INFO "macserial: serial modem detected\n"); 2380 } 2381 } 2382 2383 while (zss->has_dma) { 2384 zss->dma_priv = NULL; 2385 /* it seems that the last two addresses are the 2386 DMA controllers */ 2387 zss->tx_dma = (volatile struct dbdma_regs *) 2388 ioremap(ch->addrs[ch->n_addrs - 2].address, 0x100); 2389 zss->rx = (volatile struct mac_dma *) 2390 ioremap(ch->addrs[ch->n_addrs - 1].address, 0x100); 2391 zss->tx_dma_irq = ch->intrs[1].line; 2392 zss->rx_dma_irq = ch->intrs[2].line; 2393 spin_lock_init(&zss->rx_dma_lock); 2394 break; 2395 } 2396 2397 init_timer(&zss->powerup_timer); 2398 zss->powerup_timer.function = powerup_done; 2399 zss->powerup_timer.data = (unsigned long) zss; 2400 return 0; 2401} 2402 2403/* 2404 * /proc fs routines. TODO: Add status lines & error stats 2405 */ 2406static inline int 2407line_info(char *buf, struct mac_serial *info) 2408{ 2409 int ret=0; 2410 unsigned char* connector; 2411 int lenp; 2412 2413 ret += sprintf(buf, "%d: port:0x%X irq:%d", info->line, info->port, info->irq); 2414 2415 connector = get_property(info->dev_node, "AAPL,connector", &lenp); 2416 if (connector) 2417 ret+=sprintf(buf+ret," con:%s ", connector); 2418 if (info->is_internal_modem) { 2419 if (!connector) 2420 ret+=sprintf(buf+ret," con:"); 2421 ret+=sprintf(buf+ret,"%s", " (internal modem)"); 2422 } 2423 if (info->is_irda) { 2424 if (!connector) 2425 ret+=sprintf(buf+ret," con:"); 2426 ret+=sprintf(buf+ret,"%s", " (IrDA)"); 2427 } 2428 ret+=sprintf(buf+ret,"\n"); 2429 2430 return ret; 2431} 2432 2433int macserial_read_proc(char *page, char **start, off_t off, int count, 2434 int *eof, void *data) 2435{ 2436 int l, len = 0; 2437 off_t begin = 0; 2438 struct mac_serial *info; 2439 2440 len += sprintf(page, "serinfo:1.0 driver:" MACSERIAL_VERSION "\n"); 2441 for (info = zs_chain; info && len < 4000; info = info->zs_next) { 2442 l = line_info(page + len, info); 2443 len += l; 2444 if (len+begin > off+count) 2445 goto done; 2446 if (len+begin < off) { 2447 begin += len; 2448 len = 0; 2449 } 2450 } 2451 *eof = 1; 2452done: 2453 if (off >= len+begin) 2454 return 0; 2455 *start = page + (off-begin); 2456 return ((count < begin+len-off) ? count : begin+len-off); 2457} 2458 2459/* Ask the PROM how many Z8530s we have and initialize their zs_channels */ 2460static void 2461probe_sccs() 2462{ 2463 struct device_node *dev, *ch; 2464 struct mac_serial **pp; 2465 int n, chip, nchan; 2466 struct mac_zschannel *zs_chan; 2467 int chan_a_index; 2468 2469 n = 0; 2470 pp = &zs_chain; 2471 zs_chan = zs_channels; 2472 for (dev = find_devices("escc"); dev != 0; dev = dev->next) { 2473 nchan = 0; 2474 chip = n; 2475 if (n >= NUM_CHANNELS) { 2476 printk(KERN_WARNING "Sorry, can't use %s: no more " 2477 "channels\n", dev->full_name); 2478 continue; 2479 } 2480 chan_a_index = 0; 2481 for (ch = dev->child; ch != 0; ch = ch->sibling) { 2482 if (nchan >= 2) { 2483 printk(KERN_WARNING "SCC: Only 2 channels per " 2484 "chip are supported\n"); 2485 break; 2486 } 2487 if (ch->n_addrs < 1 || (ch ->n_intrs < 1)) { 2488 printk("Can't use %s: %d addrs %d intrs\n", 2489 ch->full_name, ch->n_addrs, ch->n_intrs); 2490 continue; 2491 } 2492 2493 /* The channel with the higher address 2494 will be the A side. */ 2495 if (nchan > 0 && 2496 ch->addrs[0].address 2497 > zs_soft[n-1].dev_node->addrs[0].address) 2498 chan_a_index = 1; 2499 2500 /* minimal initialization for now */ 2501 zs_soft[n].dev_node = ch; 2502 *pp = &zs_soft[n]; 2503 pp = &zs_soft[n].zs_next; 2504 ++nchan; 2505 ++n; 2506 } 2507 if (nchan == 0) 2508 continue; 2509 2510 /* set up A side */ 2511 if (chan_init(&zs_soft[chip + chan_a_index], zs_chan, zs_chan)) 2512 continue; 2513 ++zs_chan; 2514 2515 /* set up B side, if it exists */ 2516 if (nchan > 1) 2517 if (chan_init(&zs_soft[chip + 1 - chan_a_index], 2518 zs_chan, zs_chan - 1)) 2519 continue; 2520 ++zs_chan; 2521 } 2522 *pp = 0; 2523 2524 zs_channels_found = n; 2525#ifdef CONFIG_PMAC_PBOOK 2526 if (n) 2527 pmu_register_sleep_notifier(&serial_sleep_notifier); 2528#endif /* CONFIG_PMAC_PBOOK */ 2529} 2530 2531/* rs_init inits the driver */ 2532int macserial_init(void) 2533{ 2534 int channel, i; 2535 unsigned long flags; 2536 struct mac_serial *info; 2537 2538 /* Setup base handler, and timer table. */ 2539 init_bh(MACSERIAL_BH, do_serial_bh); 2540 2541 /* Find out how many Z8530 SCCs we have */ 2542 if (zs_chain == 0) 2543 probe_sccs(); 2544 2545 is_powerbook = find_devices("via-pmu") != 0; 2546 2547 /* Register the interrupt handler for each one 2548 * We also request the OF resources here as probe_sccs() 2549 * might be called too early for that 2550 */ 2551 save_flags(flags); cli(); 2552 for (i = 0; i < zs_channels_found; ++i) { 2553 struct device_node* ch = zs_soft[i].dev_node; 2554 if (!request_OF_resource(ch, 0, NULL)) { 2555 printk(KERN_ERR "macserial: can't request IO resource !\n"); 2556 return -ENODEV; 2557 } 2558 if (zs_soft[i].has_dma) { 2559 if (!request_OF_resource(ch, ch->n_addrs - 2, " (tx dma)")) { 2560 printk(KERN_ERR "macserial: can't request TX DMA resource !\n"); 2561 zs_soft[i].has_dma = 0; 2562 goto no_dma; 2563 } 2564 if (!request_OF_resource(ch, ch->n_addrs - 1, " (rx dma)")) { 2565 release_OF_resource(ch, ch->n_addrs - 2); 2566 printk(KERN_ERR "macserial: can't request RX DMA resource !\n"); 2567 zs_soft[i].has_dma = 0; 2568 goto no_dma; 2569 } 2570 if (request_irq(zs_soft[i].tx_dma_irq, rs_txdma_irq, 0, 2571 "SCC-txdma", &zs_soft[i])) 2572 printk(KERN_ERR "macserial: can't get irq %d\n", 2573 zs_soft[i].tx_dma_irq); 2574 disable_irq(zs_soft[i].tx_dma_irq); 2575 if (request_irq(zs_soft[i].rx_dma_irq, rs_rxdma_irq, 0, 2576 "SCC-rxdma", &zs_soft[i])) 2577 printk(KERN_ERR "macserial: can't get irq %d\n", 2578 zs_soft[i].rx_dma_irq); 2579 disable_irq(zs_soft[i].rx_dma_irq); 2580 } 2581no_dma: 2582 if (request_irq(zs_soft[i].irq, rs_interrupt, 0, 2583 "SCC", &zs_soft[i])) 2584 printk(KERN_ERR "macserial: can't get irq %d\n", 2585 zs_soft[i].irq); 2586 disable_irq(zs_soft[i].irq); 2587 } 2588 restore_flags(flags); 2589 2590 show_serial_version(); 2591 2592 /* Initialize the tty_driver structure */ 2593 /* Not all of this is exactly right for us. */ 2594 2595 memset(&serial_driver, 0, sizeof(struct tty_driver)); 2596 serial_driver.magic = TTY_DRIVER_MAGIC; 2597 serial_driver.driver_name = "macserial"; 2598#ifdef CONFIG_DEVFS_FS 2599 serial_driver.name = "tts/%d"; 2600#else 2601 serial_driver.name = "ttyS"; 2602#endif /* CONFIG_DEVFS_FS */ 2603 serial_driver.major = TTY_MAJOR; 2604 serial_driver.minor_start = 64; 2605 serial_driver.num = zs_channels_found; 2606 serial_driver.type = TTY_DRIVER_TYPE_SERIAL; 2607 serial_driver.subtype = SERIAL_TYPE_NORMAL; 2608 serial_driver.init_termios = tty_std_termios; 2609 2610 serial_driver.init_termios.c_cflag = 2611 B38400 | CS8 | CREAD | HUPCL | CLOCAL; 2612 serial_driver.flags = TTY_DRIVER_REAL_RAW; 2613 serial_driver.refcount = &serial_refcount; 2614 serial_driver.table = serial_table; 2615 serial_driver.termios = serial_termios; 2616 serial_driver.termios_locked = serial_termios_locked; 2617 2618 serial_driver.open = rs_open; 2619 serial_driver.close = rs_close; 2620 serial_driver.write = rs_write; 2621 serial_driver.flush_chars = rs_flush_chars; 2622 serial_driver.write_room = rs_write_room; 2623 serial_driver.chars_in_buffer = rs_chars_in_buffer; 2624 serial_driver.flush_buffer = rs_flush_buffer; 2625 serial_driver.ioctl = rs_ioctl; 2626 serial_driver.throttle = rs_throttle; 2627 serial_driver.unthrottle = rs_unthrottle; 2628 serial_driver.set_termios = rs_set_termios; 2629 serial_driver.stop = rs_stop; 2630 serial_driver.start = rs_start; 2631 serial_driver.hangup = rs_hangup; 2632 serial_driver.break_ctl = rs_break; 2633 serial_driver.wait_until_sent = rs_wait_until_sent; 2634 serial_driver.read_proc = macserial_read_proc; 2635 2636 /* 2637 * The callout device is just like normal device except for 2638 * major number and the subtype code. 2639 */ 2640 callout_driver = serial_driver; 2641#ifdef CONFIG_DEVFS_FS 2642 callout_driver.name = "cua/%d"; 2643#else 2644 callout_driver.name = "cua"; 2645#endif /* CONFIG_DEVFS_FS */ 2646 callout_driver.major = TTYAUX_MAJOR; 2647 callout_driver.subtype = SERIAL_TYPE_CALLOUT; 2648 callout_driver.read_proc = 0; 2649 callout_driver.proc_entry = 0; 2650 2651 if (tty_register_driver(&serial_driver)) 2652 panic("Couldn't register serial driver\n"); 2653 if (tty_register_driver(&callout_driver)) 2654 panic("Couldn't register callout driver\n"); 2655 2656 for (channel = 0; channel < zs_channels_found; ++channel) { 2657#ifdef CONFIG_KGDB 2658 if (zs_soft[channel].kgdb_channel) { 2659 kgdb_interruptible(1); 2660 continue; 2661 } 2662#endif 2663 zs_soft[channel].clk_divisor = 16; 2664/* -- we are not sure the SCC is powered ON at this point 2665 zs_soft[channel].zs_baud = get_zsbaud(&zs_soft[channel]); 2666*/ 2667 zs_soft[channel].zs_baud = 38400; 2668 2669 /* If console serial line, then enable interrupts. */ 2670 if (zs_soft[channel].is_cons) { 2671 printk(KERN_INFO "macserial: console line, enabling " 2672 "interrupt %d\n", zs_soft[channel].irq); 2673 panic("macserial: console not supported yet !"); 2674 write_zsreg(zs_soft[channel].zs_channel, R1, 2675 (EXT_INT_ENAB | INT_ALL_Rx | TxINT_ENAB)); 2676 write_zsreg(zs_soft[channel].zs_channel, R9, 2677 (NV | MIE)); 2678 } 2679 } 2680 2681 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) 2682 { 2683 unsigned char* connector; 2684 int lenp; 2685 2686#ifdef CONFIG_KGDB 2687 if (info->kgdb_channel) { 2688 continue; 2689 } 2690#endif 2691 info->magic = SERIAL_MAGIC; 2692 info->port = (int) info->zs_channel->control; 2693 info->line = i; 2694 info->tty = 0; 2695 info->custom_divisor = 16; 2696 info->timeout = 0; 2697 info->close_delay = 50; 2698 info->closing_wait = 3000; 2699 info->x_char = 0; 2700 info->event = 0; 2701 info->count = 0; 2702 info->blocked_open = 0; 2703 info->tqueue.routine = do_softint; 2704 info->tqueue.data = info; 2705 info->callout_termios =callout_driver.init_termios; 2706 info->normal_termios = serial_driver.init_termios; 2707 init_waitqueue_head(&info->open_wait); 2708 init_waitqueue_head(&info->close_wait); 2709 info->timeout = HZ; 2710 printk(KERN_INFO "tty%02d at 0x%08x (irq = %d)", info->line, 2711 info->port, info->irq); 2712 printk(" is a Z8530 ESCC"); 2713 connector = get_property(info->dev_node, "AAPL,connector", &lenp); 2714 if (connector) 2715 printk(", port = %s", connector); 2716 if (info->is_internal_modem) 2717 printk(" (internal modem)"); 2718 if (info->is_irda) 2719 printk(" (IrDA)"); 2720 printk("\n"); 2721 } 2722 tmp_buf = 0; 2723 2724 return 0; 2725} 2726 2727void macserial_cleanup(void) 2728{ 2729 int i; 2730 unsigned long flags; 2731 struct mac_serial *info; 2732 2733 for (info = zs_chain, i = 0; info; info = info->zs_next, i++) 2734 set_scc_power(info, 0); 2735 save_flags(flags); cli(); 2736 for (i = 0; i < zs_channels_found; ++i) { 2737 free_irq(zs_soft[i].irq, &zs_soft[i]); 2738 if (zs_soft[i].has_dma) { 2739 free_irq(zs_soft[i].tx_dma_irq, &zs_soft[i]); 2740 free_irq(zs_soft[i].rx_dma_irq, &zs_soft[i]); 2741 } 2742 release_OF_resource(zs_soft[i].dev_node, 0); 2743 if (zs_soft[i].has_dma) { 2744 struct device_node* ch = zs_soft[i].dev_node; 2745 release_OF_resource(ch, ch->n_addrs - 2); 2746 release_OF_resource(ch, ch->n_addrs - 1); 2747 } 2748 } 2749 restore_flags(flags); 2750 tty_unregister_driver(&callout_driver); 2751 tty_unregister_driver(&serial_driver); 2752 2753 if (tmp_buf) { 2754 free_page((unsigned long) tmp_buf); 2755 tmp_buf = 0; 2756 } 2757 2758#ifdef CONFIG_PMAC_PBOOK 2759 if (zs_channels_found) 2760 pmu_unregister_sleep_notifier(&serial_sleep_notifier); 2761#endif /* CONFIG_PMAC_PBOOK */ 2762} 2763 2764module_init(macserial_init); 2765module_exit(macserial_cleanup); 2766MODULE_LICENSE("GPL"); 2767EXPORT_NO_SYMBOLS; 2768 2769 2770/* 2771 * ------------------------------------------------------------ 2772 * Serial console driver 2773 * ------------------------------------------------------------ 2774 */ 2775#ifdef CONFIG_SERIAL_CONSOLE 2776 2777/* 2778 * Print a string to the serial port trying not to disturb 2779 * any possible real use of the port... 2780 */ 2781static void serial_console_write(struct console *co, const char *s, 2782 unsigned count) 2783{ 2784 struct mac_serial *info = zs_soft + co->index; 2785 int i; 2786 2787 /* Turn of interrupts and enable the transmitter. */ 2788 write_zsreg(info->zs_channel, R1, info->curregs[1] & ~TxINT_ENAB); 2789 write_zsreg(info->zs_channel, R5, info->curregs[5] | TxENAB | RTS | DTR); 2790 2791 for (i=0; i<count; i++) { 2792 /* Wait for the transmit buffer to empty. */ 2793 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) == 0) { 2794 eieio(); 2795 } 2796 2797 write_zsdata(info->zs_channel, s[i]); 2798 if (s[i] == 10) { 2799 while ((read_zsreg(info->zs_channel, 0) & Tx_BUF_EMP) 2800 == 0) 2801 eieio(); 2802 2803 write_zsdata(info->zs_channel, 13); 2804 } 2805 } 2806 2807 /* Restore the values in the registers. */ 2808 write_zsreg(info->zs_channel, R1, info->curregs[1]); 2809 /* Don't disable the transmitter. */ 2810} 2811 2812static kdev_t serial_console_device(struct console *c) 2813{ 2814 return MKDEV(TTY_MAJOR, 64 + c->index); 2815} 2816 2817/* 2818 * Setup initial baud/bits/parity. We do two things here: 2819 * - construct a cflag setting for the first rs_open() 2820 * - initialize the serial port 2821 * Return non-zero if we didn't find a serial port. 2822 */ 2823static int __init serial_console_setup(struct console *co, char *options) 2824{ 2825 struct mac_serial *info; 2826 int baud = 38400; 2827 int bits = 8; 2828 int parity = 'n'; 2829 int cflag = CREAD | HUPCL | CLOCAL; 2830 int brg; 2831 char *s; 2832 long flags; 2833 2834 /* Find out how many Z8530 SCCs we have */ 2835 if (zs_chain == 0) 2836 probe_sccs(); 2837 2838 if (zs_chain == 0) 2839 return -1; 2840 2841 /* Do we have the device asked for? */ 2842 if (co->index >= zs_channels_found) 2843 return -1; 2844 info = zs_soft + co->index; 2845 2846 set_scc_power(info, 1); 2847 2848 /* Reset the channel */ 2849 write_zsreg(info->zs_channel, R9, CHRA); 2850 2851 if (options) { 2852 baud = simple_strtoul(options, NULL, 10); 2853 s = options; 2854 while(*s >= '0' && *s <= '9') 2855 s++; 2856 if (*s) 2857 parity = *s++; 2858 if (*s) 2859 bits = *s - '0'; 2860 } 2861 2862 /* 2863 * Now construct a cflag setting. 2864 */ 2865 switch(baud) { 2866 case 1200: 2867 cflag |= B1200; 2868 break; 2869 case 2400: 2870 cflag |= B2400; 2871 break; 2872 case 4800: 2873 cflag |= B4800; 2874 break; 2875 case 9600: 2876 cflag |= B9600; 2877 break; 2878 case 19200: 2879 cflag |= B19200; 2880 break; 2881 case 57600: 2882 cflag |= B57600; 2883 break; 2884 case 115200: 2885 cflag |= B115200; 2886 break; 2887 case 38400: 2888 default: 2889 cflag |= B38400; 2890 break; 2891 } 2892 switch(bits) { 2893 case 7: 2894 cflag |= CS7; 2895 break; 2896 default: 2897 case 8: 2898 cflag |= CS8; 2899 break; 2900 } 2901 switch(parity) { 2902 case 'o': case 'O': 2903 cflag |= PARENB | PARODD; 2904 break; 2905 case 'e': case 'E': 2906 cflag |= PARENB; 2907 break; 2908 } 2909 co->cflag = cflag; 2910 2911 save_flags(flags); cli(); 2912 memset(info->curregs, 0, sizeof(info->curregs)); 2913 2914 info->zs_baud = baud; 2915 info->clk_divisor = 16; 2916 switch (info->zs_baud) { 2917 case ZS_CLOCK/16: /* 230400 */ 2918 info->curregs[4] = X16CLK; 2919 info->curregs[11] = 0; 2920 break; 2921 case ZS_CLOCK/32: /* 115200 */ 2922 info->curregs[4] = X32CLK; 2923 info->curregs[11] = 0; 2924 break; 2925 default: 2926 info->curregs[4] = X16CLK; 2927 info->curregs[11] = TCBR | RCBR; 2928 brg = BPS_TO_BRG(info->zs_baud, ZS_CLOCK/info->clk_divisor); 2929 info->curregs[12] = (brg & 255); 2930 info->curregs[13] = ((brg >> 8) & 255); 2931 info->curregs[14] = BRENABL; 2932 } 2933 2934 /* byte size and parity */ 2935 info->curregs[3] &= ~RxNBITS_MASK; 2936 info->curregs[5] &= ~TxNBITS_MASK; 2937 switch (cflag & CSIZE) { 2938 case CS5: 2939 info->curregs[3] |= Rx5; 2940 info->curregs[5] |= Tx5; 2941 break; 2942 case CS6: 2943 info->curregs[3] |= Rx6; 2944 info->curregs[5] |= Tx6; 2945 break; 2946 case CS7: 2947 info->curregs[3] |= Rx7; 2948 info->curregs[5] |= Tx7; 2949 break; 2950 case CS8: 2951 default: /* defaults to 8 bits */ 2952 info->curregs[3] |= Rx8; 2953 info->curregs[5] |= Tx8; 2954 break; 2955 } 2956 info->curregs[5] |= TxENAB | RTS | DTR; 2957 info->pendregs[3] = info->curregs[3]; 2958 info->pendregs[5] = info->curregs[5]; 2959 2960 info->curregs[4] &= ~(SB_MASK | PAR_ENA | PAR_EVEN); 2961 if (cflag & CSTOPB) { 2962 info->curregs[4] |= SB2; 2963 } else { 2964 info->curregs[4] |= SB1; 2965 } 2966 if (cflag & PARENB) { 2967 info->curregs[4] |= PAR_ENA; 2968 if (!(cflag & PARODD)) { 2969 info->curregs[4] |= PAR_EVEN; 2970 } 2971 } 2972 info->pendregs[4] = info->curregs[4]; 2973 2974 if (!(cflag & CLOCAL)) { 2975 if (!(info->curregs[15] & DCDIE)) 2976 info->read_reg_zero = read_zsreg(info->zs_channel, 0); 2977 info->curregs[15] |= DCDIE; 2978 } else 2979 info->curregs[15] &= ~DCDIE; 2980 if (cflag & CRTSCTS) { 2981 info->curregs[15] |= CTSIE; 2982 if ((read_zsreg(info->zs_channel, 0) & CTS) != 0) 2983 info->tx_stopped = 1; 2984 } else { 2985 info->curregs[15] &= ~CTSIE; 2986 info->tx_stopped = 0; 2987 } 2988 info->pendregs[15] = info->curregs[15]; 2989 2990 /* Load up the new values */ 2991 load_zsregs(info->zs_channel, info->curregs); 2992 2993 restore_flags(flags); 2994 2995 return 0; 2996} 2997 2998static struct console sercons = { 2999 name: "ttyS", 3000 write: serial_console_write, 3001 device: serial_console_device, 3002 setup: serial_console_setup, 3003 flags: CON_PRINTBUFFER, 3004 index: -1, 3005}; 3006 3007/* 3008 * Register console. 3009 */ 3010void __init mac_scc_console_init(void) 3011{ 3012 register_console(&sercons); 3013} 3014#endif /* ifdef CONFIG_SERIAL_CONSOLE */ 3015 3016#ifdef CONFIG_KGDB 3017/* These are for receiving and sending characters under the kgdb 3018 * source level kernel debugger. 3019 */ 3020void putDebugChar(char kgdb_char) 3021{ 3022 struct mac_zschannel *chan = zs_kgdbchan; 3023 while ((read_zsreg(chan, 0) & Tx_BUF_EMP) == 0) 3024 udelay(5); 3025 write_zsdata(chan, kgdb_char); 3026} 3027 3028char getDebugChar(void) 3029{ 3030 struct mac_zschannel *chan = zs_kgdbchan; 3031 while((read_zsreg(chan, 0) & Rx_CH_AV) == 0) 3032 eieio(); /*barrier();*/ 3033 return read_zsdata(chan); 3034} 3035 3036void kgdb_interruptible(int yes) 3037{ 3038 struct mac_zschannel *chan = zs_kgdbchan; 3039 int one, nine; 3040 nine = read_zsreg(chan, 9); 3041 if (yes == 1) { 3042 one = EXT_INT_ENAB|INT_ALL_Rx; 3043 nine |= MIE; 3044 printk("turning serial ints on\n"); 3045 } else { 3046 one = RxINT_DISAB; 3047 nine &= ~MIE; 3048 printk("turning serial ints off\n"); 3049 } 3050 write_zsreg(chan, 1, one); 3051 write_zsreg(chan, 9, nine); 3052} 3053 3054/* This sets up the serial port we're using, and turns on 3055 * interrupts for that channel, so kgdb is usable once we're done. 3056 */ 3057static inline void kgdb_chaninit(struct mac_zschannel *ms, int intson, int bps) 3058{ 3059 int brg; 3060 int i, x; 3061 volatile char *sccc = ms->control; 3062 brg = BPS_TO_BRG(bps, ZS_CLOCK/16); 3063 printk("setting bps on kgdb line to %d [brg=%x]\n", bps, brg); 3064 for (i = 20000; i != 0; --i) { 3065 x = *sccc; eieio(); 3066 } 3067 for (i = 0; i < sizeof(scc_inittab); ++i) { 3068 write_zsreg(ms, scc_inittab[i], scc_inittab[i+1]); 3069 i++; 3070 } 3071} 3072 3073void __init zs_kgdb_hook(int tty_num) 3074{ 3075 /* Find out how many Z8530 SCCs we have */ 3076 if (zs_chain == 0) 3077 probe_sccs(); 3078 3079 set_scc_power(&zs_soft[tty_num], 1); 3080 3081 zs_kgdbchan = zs_soft[tty_num].zs_channel; 3082 zs_soft[tty_num].change_needed = 0; 3083 zs_soft[tty_num].clk_divisor = 16; 3084 zs_soft[tty_num].zs_baud = 38400; 3085 zs_soft[tty_num].kgdb_channel = 1; /* This runs kgdb */ 3086 3087 /* Turn on transmitter/receiver at 8-bits/char */ 3088 kgdb_chaninit(zs_soft[tty_num].zs_channel, 1, 38400); 3089 printk("KGDB: on channel %d initialized\n", tty_num); 3090 set_debug_traps(); /* init stub */ 3091} 3092#endif /* ifdef CONFIG_KGDB */ 3093 3094#ifdef CONFIG_PMAC_PBOOK 3095/* 3096 * notify clients before sleep and reset bus afterwards 3097 */ 3098int 3099serial_notify_sleep(struct pmu_sleep_notifier *self, int when) 3100{ 3101 int i; 3102 3103 switch (when) { 3104 case PBOOK_SLEEP_REQUEST: 3105 case PBOOK_SLEEP_REJECT: 3106 break; 3107 3108 case PBOOK_SLEEP_NOW: 3109 for (i=0; i<zs_channels_found; i++) { 3110 struct mac_serial *info = &zs_soft[i]; 3111 if (info->flags & ZILOG_INITIALIZED) { 3112 shutdown(info); 3113 info->flags |= ZILOG_SLEEPING; 3114 } 3115 } 3116 break; 3117 case PBOOK_WAKE: 3118 for (i=0; i<zs_channels_found; i++) { 3119 struct mac_serial *info = &zs_soft[i]; 3120 if (info->flags & ZILOG_SLEEPING) { 3121 info->flags &= ~ZILOG_SLEEPING; 3122 startup(info); 3123 } 3124 } 3125 break; 3126 } 3127 return PBOOK_SLEEP_OK; 3128} 3129#endif /* CONFIG_PMAC_PBOOK */ 3130