1/* 68328serial.c: Serial port driver for 68328 microcontroller 2 * 3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu> 4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com> 5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org> 6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com> 7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com> 8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com> 9 * 10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca> 11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au> 12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au> 13 * VZ Second Serial Port enable Phil Wilshire 14 * 2.4/2.5 port David McCullough 15 */ 16 17#include <asm/dbg.h> 18#include <linux/module.h> 19#include <linux/errno.h> 20#include <linux/signal.h> 21#include <linux/sched.h> 22#include <linux/timer.h> 23#include <linux/interrupt.h> 24#include <linux/tty.h> 25#include <linux/tty_flip.h> 26#include <linux/major.h> 27#include <linux/string.h> 28#include <linux/fcntl.h> 29#include <linux/mm.h> 30#include <linux/kernel.h> 31#include <linux/console.h> 32#include <linux/reboot.h> 33#include <linux/keyboard.h> 34#include <linux/init.h> 35#include <linux/pm.h> 36#include <linux/bitops.h> 37#include <linux/delay.h> 38#include <linux/gfp.h> 39 40#include <asm/io.h> 41#include <asm/irq.h> 42#include <asm/system.h> 43#include <asm/delay.h> 44#include <asm/uaccess.h> 45 46/* (es) */ 47/* note: perhaps we can murge these files, so that you can just 48 * define 1 of them, and they can sort that out for themselves 49 */ 50#if defined(CONFIG_M68EZ328) 51#include <asm/MC68EZ328.h> 52#else 53#if defined(CONFIG_M68VZ328) 54#include <asm/MC68VZ328.h> 55#else 56#include <asm/MC68328.h> 57#endif /* CONFIG_M68VZ328 */ 58#endif /* CONFIG_M68EZ328 */ 59 60#include "68328serial.h" 61 62/* Turn off usage of real serial interrupt code, to "support" Copilot */ 63#ifdef CONFIG_XCOPILOT_BUGS 64#undef USE_INTS 65#else 66#define USE_INTS 67#endif 68 69static struct m68k_serial m68k_soft[NR_PORTS]; 70 71static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS; 72 73/* multiple ports are contiguous in memory */ 74m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR; 75 76struct tty_struct m68k_ttys; 77struct m68k_serial *m68k_consinfo = 0; 78 79#define M68K_CLOCK (16667000) 80 81struct tty_driver *serial_driver; 82 83/* number of characters left in xmit buffer before we ask for more */ 84#define WAKEUP_CHARS 256 85 86/* Debugging... DEBUG_INTR is bad to use when one of the zs 87 * lines is your console ;( 88 */ 89#undef SERIAL_DEBUG_INTR 90#undef SERIAL_DEBUG_OPEN 91#undef SERIAL_DEBUG_FLOW 92 93#define RS_ISR_PASS_LIMIT 256 94 95static void change_speed(struct m68k_serial *info); 96 97/* 98 * Setup for console. Argument comes from the boot command line. 99 */ 100 101/* note: this is messy, but it works, again, perhaps defined somewhere else?*/ 102#ifdef CONFIG_M68VZ328 103#define CONSOLE_BAUD_RATE 19200 104#define DEFAULT_CBAUD B19200 105#endif 106 107 108#ifndef CONSOLE_BAUD_RATE 109#define CONSOLE_BAUD_RATE 9600 110#define DEFAULT_CBAUD B9600 111#endif 112 113 114static int m68328_console_initted = 0; 115static int m68328_console_baud = CONSOLE_BAUD_RATE; 116static int m68328_console_cbaud = DEFAULT_CBAUD; 117 118 119static inline int serial_paranoia_check(struct m68k_serial *info, 120 char *name, const char *routine) 121{ 122#ifdef SERIAL_PARANOIA_CHECK 123 static const char *badmagic = 124 "Warning: bad magic number for serial struct %s in %s\n"; 125 static const char *badinfo = 126 "Warning: null m68k_serial for %s in %s\n"; 127 128 if (!info) { 129 printk(badinfo, name, routine); 130 return 1; 131 } 132 if (info->magic != SERIAL_MAGIC) { 133 printk(badmagic, name, routine); 134 return 1; 135 } 136#endif 137 return 0; 138} 139 140/* 141 * This is used to figure out the divisor speeds and the timeouts 142 */ 143static int baud_table[] = { 144 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800, 145 9600, 19200, 38400, 57600, 115200, 0 }; 146 147/* Sets or clears DTR/RTS on the requested line */ 148static inline void m68k_rtsdtr(struct m68k_serial *ss, int set) 149{ 150 if (set) { 151 /* set the RTS/CTS line */ 152 } else { 153 /* clear it */ 154 } 155 return; 156} 157 158/* Utility routines */ 159static inline int get_baud(struct m68k_serial *ss) 160{ 161 unsigned long result = 115200; 162 unsigned short int baud = uart_addr[ss->line].ubaud; 163 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400; 164 result >>= GET_FIELD(baud, UBAUD_DIVIDE); 165 166 return result; 167} 168 169/* 170 * ------------------------------------------------------------ 171 * rs_stop() and rs_start() 172 * 173 * This routines are called before setting or resetting tty->stopped. 174 * They enable or disable transmitter interrupts, as necessary. 175 * ------------------------------------------------------------ 176 */ 177static void rs_stop(struct tty_struct *tty) 178{ 179 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 180 m68328_uart *uart = &uart_addr[info->line]; 181 unsigned long flags; 182 183 if (serial_paranoia_check(info, tty->name, "rs_stop")) 184 return; 185 186 local_irq_save(flags); 187 uart->ustcnt &= ~USTCNT_TXEN; 188 local_irq_restore(flags); 189} 190 191static int rs_put_char(char ch) 192{ 193 int flags, loops = 0; 194 195 local_irq_save(flags); 196 197 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) { 198 loops++; 199 udelay(5); 200 } 201 202 UTX_TXDATA = ch; 203 udelay(5); 204 local_irq_restore(flags); 205 return 1; 206} 207 208static void rs_start(struct tty_struct *tty) 209{ 210 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 211 m68328_uart *uart = &uart_addr[info->line]; 212 unsigned long flags; 213 214 if (serial_paranoia_check(info, tty->name, "rs_start")) 215 return; 216 217 local_irq_save(flags); 218 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) { 219#ifdef USE_INTS 220 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK; 221#else 222 uart->ustcnt |= USTCNT_TXEN; 223#endif 224 } 225 local_irq_restore(flags); 226} 227 228/* Drop into either the boot monitor or kadb upon receiving a break 229 * from keyboard/console input. 230 */ 231static void batten_down_hatches(void) 232{ 233 /* Drop into the debugger */ 234} 235 236static void status_handle(struct m68k_serial *info, unsigned short status) 237{ 238 /* If this is console input and this is a 239 * 'break asserted' status change interrupt 240 * see if we can drop into the debugger 241 */ 242 if((status & URX_BREAK) && info->break_abort) 243 batten_down_hatches(); 244 return; 245} 246 247static void receive_chars(struct m68k_serial *info, unsigned short rx) 248{ 249 struct tty_struct *tty = info->port.tty; 250 m68328_uart *uart = &uart_addr[info->line]; 251 unsigned char ch, flag; 252 253 /* 254 * This do { } while() loop will get ALL chars out of Rx FIFO 255 */ 256#ifndef CONFIG_XCOPILOT_BUGS 257 do { 258#endif 259 ch = GET_FIELD(rx, URX_RXDATA); 260 261 if(info->is_cons) { 262 if(URX_BREAK & rx) { /* whee, break received */ 263 status_handle(info, rx); 264 return; 265#ifdef CONFIG_MAGIC_SYSRQ 266 } else if (ch == 0x10) { /* ^P */ 267 show_state(); 268 show_free_areas(); 269 show_buffers(); 270/* show_net_buffers(); */ 271 return; 272 } else if (ch == 0x12) { /* ^R */ 273 emergency_restart(); 274 return; 275#endif /* CONFIG_MAGIC_SYSRQ */ 276 } 277 } 278 279 if(!tty) 280 goto clear_and_exit; 281 282 flag = TTY_NORMAL; 283 284 if(rx & URX_PARITY_ERROR) { 285 flag = TTY_PARITY; 286 status_handle(info, rx); 287 } else if(rx & URX_OVRUN) { 288 flag = TTY_OVERRUN; 289 status_handle(info, rx); 290 } else if(rx & URX_FRAME_ERROR) { 291 flag = TTY_FRAME; 292 status_handle(info, rx); 293 } 294 tty_insert_flip_char(tty, ch, flag); 295#ifndef CONFIG_XCOPILOT_BUGS 296 } while((rx = uart->urx.w) & URX_DATA_READY); 297#endif 298 299 tty_schedule_flip(tty); 300 301clear_and_exit: 302 return; 303} 304 305static void transmit_chars(struct m68k_serial *info) 306{ 307 m68328_uart *uart = &uart_addr[info->line]; 308 309 if (info->x_char) { 310 /* Send next char */ 311 uart->utx.b.txdata = info->x_char; 312 info->x_char = 0; 313 goto clear_and_return; 314 } 315 316 if((info->xmit_cnt <= 0) || info->port.tty->stopped) { 317 /* That's peculiar... TX ints off */ 318 uart->ustcnt &= ~USTCNT_TX_INTR_MASK; 319 goto clear_and_return; 320 } 321 322 /* Send char */ 323 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++]; 324 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1); 325 info->xmit_cnt--; 326 327 if (info->xmit_cnt < WAKEUP_CHARS) 328 schedule_work(&info->tqueue); 329 330 if(info->xmit_cnt <= 0) { 331 /* All done for now... TX ints off */ 332 uart->ustcnt &= ~USTCNT_TX_INTR_MASK; 333 goto clear_and_return; 334 } 335 336clear_and_return: 337 /* Clear interrupt (should be auto)*/ 338 return; 339} 340 341/* 342 * This is the serial driver's generic interrupt routine 343 */ 344irqreturn_t rs_interrupt(int irq, void *dev_id) 345{ 346 struct m68k_serial *info = dev_id; 347 m68328_uart *uart; 348 unsigned short rx; 349 unsigned short tx; 350 351 uart = &uart_addr[info->line]; 352 rx = uart->urx.w; 353 354#ifdef USE_INTS 355 tx = uart->utx.w; 356 357 if (rx & URX_DATA_READY) receive_chars(info, rx); 358 if (tx & UTX_TX_AVAIL) transmit_chars(info); 359#else 360 receive_chars(info, rx); 361#endif 362 return IRQ_HANDLED; 363} 364 365static void do_softint(struct work_struct *work) 366{ 367 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue); 368 struct tty_struct *tty; 369 370 tty = info->port.tty; 371 if (!tty) 372 return; 373} 374 375/* 376 * This routine is called from the scheduler tqueue when the interrupt 377 * routine has signalled that a hangup has occurred. The path of 378 * hangup processing is: 379 * 380 * serial interrupt routine -> (scheduler tqueue) -> 381 * do_serial_hangup() -> tty->hangup() -> rs_hangup() 382 * 383 */ 384static void do_serial_hangup(struct work_struct *work) 385{ 386 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup); 387 struct tty_struct *tty; 388 389 tty = info->port.tty; 390 if (!tty) 391 return; 392 393 tty_hangup(tty); 394} 395 396 397static int startup(struct m68k_serial * info) 398{ 399 m68328_uart *uart = &uart_addr[info->line]; 400 unsigned long flags; 401 402 if (info->flags & S_INITIALIZED) 403 return 0; 404 405 if (!info->xmit_buf) { 406 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL); 407 if (!info->xmit_buf) 408 return -ENOMEM; 409 } 410 411 local_irq_save(flags); 412 413 /* 414 * Clear the FIFO buffers and disable them 415 * (they will be reenabled in change_speed()) 416 */ 417 418 uart->ustcnt = USTCNT_UEN; 419 info->xmit_fifo_size = 1; 420 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN; 421 (void)uart->urx.w; 422 423 /* 424 * Finally, enable sequencing and interrupts 425 */ 426#ifdef USE_INTS 427 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | 428 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK; 429#else 430 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK; 431#endif 432 433 if (info->port.tty) 434 clear_bit(TTY_IO_ERROR, &info->port.tty->flags); 435 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; 436 437 /* 438 * and set the speed of the serial port 439 */ 440 441 change_speed(info); 442 443 info->flags |= S_INITIALIZED; 444 local_irq_restore(flags); 445 return 0; 446} 447 448/* 449 * This routine will shutdown a serial port; interrupts are disabled, and 450 * DTR is dropped if the hangup on close termio flag is on. 451 */ 452static void shutdown(struct m68k_serial * info) 453{ 454 m68328_uart *uart = &uart_addr[info->line]; 455 unsigned long flags; 456 457 uart->ustcnt = 0; /* All off! */ 458 if (!(info->flags & S_INITIALIZED)) 459 return; 460 461 local_irq_save(flags); 462 463 if (info->xmit_buf) { 464 free_page((unsigned long) info->xmit_buf); 465 info->xmit_buf = 0; 466 } 467 468 if (info->port.tty) 469 set_bit(TTY_IO_ERROR, &info->port.tty->flags); 470 471 info->flags &= ~S_INITIALIZED; 472 local_irq_restore(flags); 473} 474 475struct { 476 int divisor, prescale; 477} 478#ifndef CONFIG_M68VZ328 479 hw_baud_table[18] = { 480 {0,0}, /* 0 */ 481 {0,0}, /* 50 */ 482 {0,0}, /* 75 */ 483 {0,0}, /* 110 */ 484 {0,0}, /* 134 */ 485 {0,0}, /* 150 */ 486 {0,0}, /* 200 */ 487 {7,0x26}, /* 300 */ 488 {6,0x26}, /* 600 */ 489 {5,0x26}, /* 1200 */ 490 {0,0}, /* 1800 */ 491 {4,0x26}, /* 2400 */ 492 {3,0x26}, /* 4800 */ 493 {2,0x26}, /* 9600 */ 494 {1,0x26}, /* 19200 */ 495 {0,0x26}, /* 38400 */ 496 {1,0x38}, /* 57600 */ 497 {0,0x38}, /* 115200 */ 498}; 499#else 500 hw_baud_table[18] = { 501 {0,0}, /* 0 */ 502 {0,0}, /* 50 */ 503 {0,0}, /* 75 */ 504 {0,0}, /* 110 */ 505 {0,0}, /* 134 */ 506 {0,0}, /* 150 */ 507 {0,0}, /* 200 */ 508 {0,0}, /* 300 */ 509 {7,0x26}, /* 600 */ 510 {6,0x26}, /* 1200 */ 511 {0,0}, /* 1800 */ 512 {5,0x26}, /* 2400 */ 513 {4,0x26}, /* 4800 */ 514 {3,0x26}, /* 9600 */ 515 {2,0x26}, /* 19200 */ 516 {1,0x26}, /* 38400 */ 517 {0,0x26}, /* 57600 */ 518 {1,0x38}, /* 115200 */ 519}; 520#endif 521/* rate = 1036800 / ((65 - prescale) * (1<<divider)) */ 522 523/* 524 * This routine is called to set the UART divisor registers to match 525 * the specified baud rate for a serial port. 526 */ 527static void change_speed(struct m68k_serial *info) 528{ 529 m68328_uart *uart = &uart_addr[info->line]; 530 unsigned short port; 531 unsigned short ustcnt; 532 unsigned cflag; 533 int i; 534 535 if (!info->port.tty || !info->port.tty->termios) 536 return; 537 cflag = info->port.tty->termios->c_cflag; 538 if (!(port = info->port)) 539 return; 540 541 ustcnt = uart->ustcnt; 542 uart->ustcnt = ustcnt & ~USTCNT_TXEN; 543 544 i = cflag & CBAUD; 545 if (i & CBAUDEX) { 546 i = (i & ~CBAUDEX) + B38400; 547 } 548 549 info->baud = baud_table[i]; 550 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) | 551 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale); 552 553 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7); 554 555 if ((cflag & CSIZE) == CS8) 556 ustcnt |= USTCNT_8_7; 557 558 if (cflag & CSTOPB) 559 ustcnt |= USTCNT_STOP; 560 561 if (cflag & PARENB) 562 ustcnt |= USTCNT_PARITYEN; 563 if (cflag & PARODD) 564 ustcnt |= USTCNT_ODD_EVEN; 565 566#ifdef CONFIG_SERIAL_68328_RTS_CTS 567 if (cflag & CRTSCTS) { 568 uart->utx.w &= ~ UTX_NOCTS; 569 } else { 570 uart->utx.w |= UTX_NOCTS; 571 } 572#endif 573 574 ustcnt |= USTCNT_TXEN; 575 576 uart->ustcnt = ustcnt; 577 return; 578} 579 580/* 581 * Fair output driver allows a process to speak. 582 */ 583static void rs_fair_output(void) 584{ 585 int left; /* Output no more than that */ 586 unsigned long flags; 587 struct m68k_serial *info = &m68k_soft[0]; 588 char c; 589 590 if (info == 0) return; 591 if (info->xmit_buf == 0) return; 592 593 local_irq_save(flags); 594 left = info->xmit_cnt; 595 while (left != 0) { 596 c = info->xmit_buf[info->xmit_tail]; 597 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1); 598 info->xmit_cnt--; 599 local_irq_restore(flags); 600 601 rs_put_char(c); 602 603 local_irq_save(flags); 604 left = min(info->xmit_cnt, left-1); 605 } 606 607 /* Last character is being transmitted now (hopefully). */ 608 udelay(5); 609 610 local_irq_restore(flags); 611 return; 612} 613 614/* 615 * m68k_console_print is registered for printk. 616 */ 617void console_print_68328(const char *p) 618{ 619 char c; 620 621 while((c=*(p++)) != 0) { 622 if(c == '\n') 623 rs_put_char('\r'); 624 rs_put_char(c); 625 } 626 627 /* Comment this if you want to have a strict interrupt-driven output */ 628 rs_fair_output(); 629 630 return; 631} 632 633static void rs_set_ldisc(struct tty_struct *tty) 634{ 635 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 636 637 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc")) 638 return; 639 640 info->is_cons = (tty->termios->c_line == N_TTY); 641 642 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off"); 643} 644 645static void rs_flush_chars(struct tty_struct *tty) 646{ 647 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 648 m68328_uart *uart = &uart_addr[info->line]; 649 unsigned long flags; 650 651 if (serial_paranoia_check(info, tty->name, "rs_flush_chars")) 652 return; 653#ifndef USE_INTS 654 for(;;) { 655#endif 656 657 /* Enable transmitter */ 658 local_irq_save(flags); 659 660 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped || 661 !info->xmit_buf) { 662 local_irq_restore(flags); 663 return; 664 } 665 666#ifdef USE_INTS 667 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK; 668#else 669 uart->ustcnt |= USTCNT_TXEN; 670#endif 671 672#ifdef USE_INTS 673 if (uart->utx.w & UTX_TX_AVAIL) { 674#else 675 if (1) { 676#endif 677 /* Send char */ 678 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++]; 679 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1); 680 info->xmit_cnt--; 681 } 682 683#ifndef USE_INTS 684 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5); 685 } 686#endif 687 local_irq_restore(flags); 688} 689 690extern void console_printn(const char * b, int count); 691 692static int rs_write(struct tty_struct * tty, 693 const unsigned char *buf, int count) 694{ 695 int c, total = 0; 696 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 697 m68328_uart *uart = &uart_addr[info->line]; 698 unsigned long flags; 699 700 if (serial_paranoia_check(info, tty->name, "rs_write")) 701 return 0; 702 703 if (!tty || !info->xmit_buf) 704 return 0; 705 706 local_save_flags(flags); 707 while (1) { 708 local_irq_disable(); 709 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1, 710 SERIAL_XMIT_SIZE - info->xmit_head)); 711 local_irq_restore(flags); 712 713 if (c <= 0) 714 break; 715 716 memcpy(info->xmit_buf + info->xmit_head, buf, c); 717 718 local_irq_disable(); 719 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1); 720 info->xmit_cnt += c; 721 local_irq_restore(flags); 722 buf += c; 723 count -= c; 724 total += c; 725 } 726 727 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) { 728 /* Enable transmitter */ 729 local_irq_disable(); 730#ifndef USE_INTS 731 while(info->xmit_cnt) { 732#endif 733 734 uart->ustcnt |= USTCNT_TXEN; 735#ifdef USE_INTS 736 uart->ustcnt |= USTCNT_TX_INTR_MASK; 737#else 738 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5); 739#endif 740 if (uart->utx.w & UTX_TX_AVAIL) { 741 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++]; 742 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1); 743 info->xmit_cnt--; 744 } 745 746#ifndef USE_INTS 747 } 748#endif 749 local_irq_restore(flags); 750 } 751 752 return total; 753} 754 755static int rs_write_room(struct tty_struct *tty) 756{ 757 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 758 int ret; 759 760 if (serial_paranoia_check(info, tty->name, "rs_write_room")) 761 return 0; 762 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1; 763 if (ret < 0) 764 ret = 0; 765 return ret; 766} 767 768static int rs_chars_in_buffer(struct tty_struct *tty) 769{ 770 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 771 772 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer")) 773 return 0; 774 return info->xmit_cnt; 775} 776 777static void rs_flush_buffer(struct tty_struct *tty) 778{ 779 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 780 unsigned long flags; 781 782 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer")) 783 return; 784 local_irq_save(flags); 785 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0; 786 local_irq_restore(flags); 787 tty_wakeup(tty); 788} 789 790/* 791 * ------------------------------------------------------------ 792 * rs_throttle() 793 * 794 * This routine is called by the upper-layer tty layer to signal that 795 * incoming characters should be throttled. 796 * ------------------------------------------------------------ 797 */ 798static void rs_throttle(struct tty_struct * tty) 799{ 800 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 801 802 if (serial_paranoia_check(info, tty->name, "rs_throttle")) 803 return; 804 805 if (I_IXOFF(tty)) 806 info->x_char = STOP_CHAR(tty); 807 808 /* Turn off RTS line (do this atomic) */ 809} 810 811static void rs_unthrottle(struct tty_struct * tty) 812{ 813 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 814 815 if (serial_paranoia_check(info, tty->name, "rs_unthrottle")) 816 return; 817 818 if (I_IXOFF(tty)) { 819 if (info->x_char) 820 info->x_char = 0; 821 else 822 info->x_char = START_CHAR(tty); 823 } 824 825 /* Assert RTS line (do this atomic) */ 826} 827 828/* 829 * ------------------------------------------------------------ 830 * rs_ioctl() and friends 831 * ------------------------------------------------------------ 832 */ 833 834static int get_serial_info(struct m68k_serial * info, 835 struct serial_struct * retinfo) 836{ 837 struct serial_struct tmp; 838 839 if (!retinfo) 840 return -EFAULT; 841 memset(&tmp, 0, sizeof(tmp)); 842 tmp.type = info->type; 843 tmp.line = info->line; 844 tmp.port = info->port; 845 tmp.irq = info->irq; 846 tmp.flags = info->flags; 847 tmp.baud_base = info->baud_base; 848 tmp.close_delay = info->close_delay; 849 tmp.closing_wait = info->closing_wait; 850 tmp.custom_divisor = info->custom_divisor; 851 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo))) 852 return -EFAULT; 853 854 return 0; 855} 856 857static int set_serial_info(struct m68k_serial * info, 858 struct serial_struct * new_info) 859{ 860 struct serial_struct new_serial; 861 struct m68k_serial old_info; 862 int retval = 0; 863 864 if (!new_info) 865 return -EFAULT; 866 if (copy_from_user(&new_serial, new_info, sizeof(new_serial))) 867 return -EFAULT; 868 old_info = *info; 869 870 if (!capable(CAP_SYS_ADMIN)) { 871 if ((new_serial.baud_base != info->baud_base) || 872 (new_serial.type != info->type) || 873 (new_serial.close_delay != info->close_delay) || 874 ((new_serial.flags & ~S_USR_MASK) != 875 (info->flags & ~S_USR_MASK))) 876 return -EPERM; 877 info->flags = ((info->flags & ~S_USR_MASK) | 878 (new_serial.flags & S_USR_MASK)); 879 info->custom_divisor = new_serial.custom_divisor; 880 goto check_and_exit; 881 } 882 883 if (info->count > 1) 884 return -EBUSY; 885 886 /* 887 * OK, past this point, all the error checking has been done. 888 * At this point, we start making changes..... 889 */ 890 891 info->baud_base = new_serial.baud_base; 892 info->flags = ((info->flags & ~S_FLAGS) | 893 (new_serial.flags & S_FLAGS)); 894 info->type = new_serial.type; 895 info->close_delay = new_serial.close_delay; 896 info->closing_wait = new_serial.closing_wait; 897 898check_and_exit: 899 retval = startup(info); 900 return retval; 901} 902 903/* 904 * get_lsr_info - get line status register info 905 * 906 * Purpose: Let user call ioctl() to get info when the UART physically 907 * is emptied. On bus types like RS485, the transmitter must 908 * release the bus after transmitting. This must be done when 909 * the transmit shift register is empty, not be done when the 910 * transmit holding register is empty. This functionality 911 * allows an RS485 driver to be written in user space. 912 */ 913static int get_lsr_info(struct m68k_serial * info, unsigned int *value) 914{ 915#ifdef CONFIG_SERIAL_68328_RTS_CTS 916 m68328_uart *uart = &uart_addr[info->line]; 917#endif 918 unsigned char status; 919 unsigned long flags; 920 921 local_irq_save(flags); 922#ifdef CONFIG_SERIAL_68328_RTS_CTS 923 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0; 924#else 925 status = 0; 926#endif 927 local_irq_restore(flags); 928 return put_user(status, value); 929} 930 931/* 932 * This routine sends a break character out the serial port. 933 */ 934static void send_break(struct m68k_serial * info, unsigned int duration) 935{ 936 m68328_uart *uart = &uart_addr[info->line]; 937 unsigned long flags; 938 if (!info->port) 939 return; 940 local_irq_save(flags); 941#ifdef USE_INTS 942 uart->utx.w |= UTX_SEND_BREAK; 943 msleep_interruptible(duration); 944 uart->utx.w &= ~UTX_SEND_BREAK; 945#endif 946 local_irq_restore(flags); 947} 948 949static int rs_ioctl(struct tty_struct *tty, struct file * file, 950 unsigned int cmd, unsigned long arg) 951{ 952 int error; 953 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; 954 int retval; 955 956 if (serial_paranoia_check(info, tty->name, "rs_ioctl")) 957 return -ENODEV; 958 959 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) && 960 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) && 961 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) { 962 if (tty->flags & (1 << TTY_IO_ERROR)) 963 return -EIO; 964 } 965 966 switch (cmd) { 967 case TCSBRK: /* SVID version: non-zero arg --> no break */ 968 retval = tty_check_change(tty); 969 if (retval) 970 return retval; 971 tty_wait_until_sent(tty, 0); 972 if (!arg) 973 send_break(info, 250); /* 1/4 second */ 974 return 0; 975 case TCSBRKP: /* support for POSIX tcsendbreak() */ 976 retval = tty_check_change(tty); 977 if (retval) 978 return retval; 979 tty_wait_until_sent(tty, 0); 980 send_break(info, arg ? arg*(100) : 250); 981 return 0; 982 case TIOCGSERIAL: 983 return get_serial_info(info, 984 (struct serial_struct *) arg); 985 case TIOCSSERIAL: 986 return set_serial_info(info, 987 (struct serial_struct *) arg); 988 case TIOCSERGETLSR: /* Get line status register */ 989 return get_lsr_info(info, (unsigned int *) arg); 990 case TIOCSERGSTRUCT: 991 if (copy_to_user((struct m68k_serial *) arg, 992 info, sizeof(struct m68k_serial))) 993 return -EFAULT; 994 return 0; 995 default: 996 return -ENOIOCTLCMD; 997 } 998 return 0; 999} 1000 1001static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios) 1002{ 1003 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data; 1004 1005 change_speed(info); 1006 1007 if ((old_termios->c_cflag & CRTSCTS) && 1008 !(tty->termios->c_cflag & CRTSCTS)) { 1009 tty->hw_stopped = 0; 1010 rs_start(tty); 1011 } 1012 1013} 1014 1015/* 1016 * ------------------------------------------------------------ 1017 * rs_close() 1018 * 1019 * This routine is called when the serial port gets closed. First, we 1020 * wait for the last remaining data to be sent. Then, we unlink its 1021 * S structure from the interrupt chain if necessary, and we free 1022 * that IRQ if nothing is left in the chain. 1023 * ------------------------------------------------------------ 1024 */ 1025static void rs_close(struct tty_struct *tty, struct file * filp) 1026{ 1027 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; 1028 m68328_uart *uart = &uart_addr[info->line]; 1029 unsigned long flags; 1030 1031 if (!info || serial_paranoia_check(info, tty->name, "rs_close")) 1032 return; 1033 1034 local_irq_save(flags); 1035 1036 if (tty_hung_up_p(filp)) { 1037 local_irq_restore(flags); 1038 return; 1039 } 1040 1041 if ((tty->count == 1) && (info->count != 1)) { 1042 /* 1043 * Uh, oh. tty->count is 1, which means that the tty 1044 * structure will be freed. Info->count should always 1045 * be one in these conditions. If it's greater than 1046 * one, we've got real problems, since it means the 1047 * serial port won't be shutdown. 1048 */ 1049 printk("rs_close: bad serial port count; tty->count is 1, " 1050 "info->count is %d\n", info->count); 1051 info->count = 1; 1052 } 1053 if (--info->count < 0) { 1054 printk("rs_close: bad serial port count for ttyS%d: %d\n", 1055 info->line, info->count); 1056 info->count = 0; 1057 } 1058 if (info->count) { 1059 local_irq_restore(flags); 1060 return; 1061 } 1062 info->flags |= S_CLOSING; 1063 /* 1064 * Now we wait for the transmit buffer to clear; and we notify 1065 * the line discipline to only process XON/XOFF characters. 1066 */ 1067 tty->closing = 1; 1068 if (info->closing_wait != S_CLOSING_WAIT_NONE) 1069 tty_wait_until_sent(tty, info->closing_wait); 1070 /* 1071 * At this point we stop accepting input. To do this, we 1072 * disable the receive line status interrupts, and tell the 1073 * interrupt driver to stop checking the data ready bit in the 1074 * line status register. 1075 */ 1076 1077 uart->ustcnt &= ~USTCNT_RXEN; 1078 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK); 1079 1080 shutdown(info); 1081 rs_flush_buffer(tty); 1082 1083 tty_ldisc_flush(tty); 1084 tty->closing = 0; 1085 info->event = 0; 1086 info->port.tty = NULL; 1087#warning "This is not and has never been valid so fix it" 1088 if (info->blocked_open) { 1089 if (info->close_delay) { 1090 msleep_interruptible(jiffies_to_msecs(info->close_delay)); 1091 } 1092 wake_up_interruptible(&info->open_wait); 1093 } 1094 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING); 1095 wake_up_interruptible(&info->close_wait); 1096 local_irq_restore(flags); 1097} 1098 1099/* 1100 * rs_hangup() --- called by tty_hangup() when a hangup is signaled. 1101 */ 1102void rs_hangup(struct tty_struct *tty) 1103{ 1104 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data; 1105 1106 if (serial_paranoia_check(info, tty->name, "rs_hangup")) 1107 return; 1108 1109 rs_flush_buffer(tty); 1110 shutdown(info); 1111 info->event = 0; 1112 info->count = 0; 1113 info->flags &= ~S_NORMAL_ACTIVE; 1114 info->port.tty = NULL; 1115 wake_up_interruptible(&info->open_wait); 1116} 1117 1118/* 1119 * ------------------------------------------------------------ 1120 * rs_open() and friends 1121 * ------------------------------------------------------------ 1122 */ 1123static int block_til_ready(struct tty_struct *tty, struct file * filp, 1124 struct m68k_serial *info) 1125{ 1126 DECLARE_WAITQUEUE(wait, current); 1127 int retval; 1128 int do_clocal = 0; 1129 1130 /* 1131 * If the device is in the middle of being closed, then block 1132 * until it's done, and then try again. 1133 */ 1134 if (info->flags & S_CLOSING) { 1135 interruptible_sleep_on(&info->close_wait); 1136#ifdef SERIAL_DO_RESTART 1137 if (info->flags & S_HUP_NOTIFY) 1138 return -EAGAIN; 1139 else 1140 return -ERESTARTSYS; 1141#else 1142 return -EAGAIN; 1143#endif 1144 } 1145 1146 /* 1147 * If non-blocking mode is set, or the port is not enabled, 1148 * then make the check up front and then exit. 1149 */ 1150 if ((filp->f_flags & O_NONBLOCK) || 1151 (tty->flags & (1 << TTY_IO_ERROR))) { 1152 info->flags |= S_NORMAL_ACTIVE; 1153 return 0; 1154 } 1155 1156 if (tty->termios->c_cflag & CLOCAL) 1157 do_clocal = 1; 1158 1159 /* 1160 * Block waiting for the carrier detect and the line to become 1161 * free (i.e., not in use by the callout). While we are in 1162 * this loop, info->count is dropped by one, so that 1163 * rs_close() knows when to free things. We restore it upon 1164 * exit, either normal or abnormal. 1165 */ 1166 retval = 0; 1167 add_wait_queue(&info->open_wait, &wait); 1168 1169 info->count--; 1170 info->blocked_open++; 1171 while (1) { 1172 local_irq_disable(); 1173 m68k_rtsdtr(info, 1); 1174 local_irq_enable(); 1175 current->state = TASK_INTERRUPTIBLE; 1176 if (tty_hung_up_p(filp) || 1177 !(info->flags & S_INITIALIZED)) { 1178#ifdef SERIAL_DO_RESTART 1179 if (info->flags & S_HUP_NOTIFY) 1180 retval = -EAGAIN; 1181 else 1182 retval = -ERESTARTSYS; 1183#else 1184 retval = -EAGAIN; 1185#endif 1186 break; 1187 } 1188 if (!(info->flags & S_CLOSING) && do_clocal) 1189 break; 1190 if (signal_pending(current)) { 1191 retval = -ERESTARTSYS; 1192 break; 1193 } 1194 tty_unlock(); 1195 schedule(); 1196 tty_lock(); 1197 } 1198 current->state = TASK_RUNNING; 1199 remove_wait_queue(&info->open_wait, &wait); 1200 if (!tty_hung_up_p(filp)) 1201 info->count++; 1202 info->blocked_open--; 1203 1204 if (retval) 1205 return retval; 1206 info->flags |= S_NORMAL_ACTIVE; 1207 return 0; 1208} 1209 1210/* 1211 * This routine is called whenever a serial port is opened. It 1212 * enables interrupts for a serial port, linking in its S structure into 1213 * the IRQ chain. It also performs the serial-specific 1214 * initialization for the tty structure. 1215 */ 1216int rs_open(struct tty_struct *tty, struct file * filp) 1217{ 1218 struct m68k_serial *info; 1219 int retval, line; 1220 1221 line = tty->index; 1222 1223 if (line >= NR_PORTS || line < 0) /* we have exactly one */ 1224 return -ENODEV; 1225 1226 info = &m68k_soft[line]; 1227 1228 if (serial_paranoia_check(info, tty->name, "rs_open")) 1229 return -ENODEV; 1230 1231 info->count++; 1232 tty->driver_data = info; 1233 info->port.tty = tty; 1234 1235 /* 1236 * Start up serial port 1237 */ 1238 retval = startup(info); 1239 if (retval) 1240 return retval; 1241 1242 return block_til_ready(tty, filp, info); 1243} 1244 1245/* Finally, routines used to initialize the serial driver. */ 1246 1247static void show_serial_version(void) 1248{ 1249 printk("MC68328 serial driver version 1.00\n"); 1250} 1251 1252static const struct tty_operations rs_ops = { 1253 .open = rs_open, 1254 .close = rs_close, 1255 .write = rs_write, 1256 .flush_chars = rs_flush_chars, 1257 .write_room = rs_write_room, 1258 .chars_in_buffer = rs_chars_in_buffer, 1259 .flush_buffer = rs_flush_buffer, 1260 .ioctl = rs_ioctl, 1261 .throttle = rs_throttle, 1262 .unthrottle = rs_unthrottle, 1263 .set_termios = rs_set_termios, 1264 .stop = rs_stop, 1265 .start = rs_start, 1266 .hangup = rs_hangup, 1267 .set_ldisc = rs_set_ldisc, 1268}; 1269 1270/* rs_init inits the driver */ 1271static int __init 1272rs68328_init(void) 1273{ 1274 int flags, i; 1275 struct m68k_serial *info; 1276 1277 serial_driver = alloc_tty_driver(NR_PORTS); 1278 if (!serial_driver) 1279 return -ENOMEM; 1280 1281 show_serial_version(); 1282 1283 /* Initialize the tty_driver structure */ 1284 /* SPARC: Not all of this is exactly right for us. */ 1285 1286 serial_driver->name = "ttyS"; 1287 serial_driver->major = TTY_MAJOR; 1288 serial_driver->minor_start = 64; 1289 serial_driver->type = TTY_DRIVER_TYPE_SERIAL; 1290 serial_driver->subtype = SERIAL_TYPE_NORMAL; 1291 serial_driver->init_termios = tty_std_termios; 1292 serial_driver->init_termios.c_cflag = 1293 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL; 1294 serial_driver->flags = TTY_DRIVER_REAL_RAW; 1295 tty_set_operations(serial_driver, &rs_ops); 1296 1297 if (tty_register_driver(serial_driver)) { 1298 put_tty_driver(serial_driver); 1299 printk(KERN_ERR "Couldn't register serial driver\n"); 1300 return -ENOMEM; 1301 } 1302 1303 local_irq_save(flags); 1304 1305 for(i=0;i<NR_PORTS;i++) { 1306 1307 info = &m68k_soft[i]; 1308 info->magic = SERIAL_MAGIC; 1309 info->port = (int) &uart_addr[i]; 1310 info->port.tty = NULL; 1311 info->irq = uart_irqs[i]; 1312 info->custom_divisor = 16; 1313 info->close_delay = 50; 1314 info->closing_wait = 3000; 1315 info->x_char = 0; 1316 info->event = 0; 1317 info->count = 0; 1318 info->blocked_open = 0; 1319 INIT_WORK(&info->tqueue, do_softint); 1320 INIT_WORK(&info->tqueue_hangup, do_serial_hangup); 1321 init_waitqueue_head(&info->open_wait); 1322 init_waitqueue_head(&info->close_wait); 1323 info->line = i; 1324 info->is_cons = 1; /* Means shortcuts work */ 1325 1326 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line, 1327 info->port, info->irq); 1328 printk(" is a builtin MC68328 UART\n"); 1329 1330#ifdef CONFIG_M68VZ328 1331 if (i > 0 ) 1332 PJSEL &= 0xCF; /* PSW enable second port output */ 1333#endif 1334 1335 if (request_irq(uart_irqs[i], 1336 rs_interrupt, 1337 IRQF_DISABLED, 1338 "M68328_UART", info)) 1339 panic("Unable to attach 68328 serial interrupt\n"); 1340 } 1341 local_irq_restore(flags); 1342 return 0; 1343} 1344 1345module_init(rs68328_init); 1346 1347 1348 1349static void m68328_set_baud(void) 1350{ 1351 unsigned short ustcnt; 1352 int i; 1353 1354 ustcnt = USTCNT; 1355 USTCNT = ustcnt & ~USTCNT_TXEN; 1356 1357again: 1358 for (i = 0; i < ARRAY_SIZE(baud_table); i++) 1359 if (baud_table[i] == m68328_console_baud) 1360 break; 1361 if (i >= ARRAY_SIZE(baud_table)) { 1362 m68328_console_baud = 9600; 1363 goto again; 1364 } 1365 1366 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) | 1367 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale); 1368 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7); 1369 ustcnt |= USTCNT_8_7; 1370 ustcnt |= USTCNT_TXEN; 1371 USTCNT = ustcnt; 1372 m68328_console_initted = 1; 1373 return; 1374} 1375 1376 1377int m68328_console_setup(struct console *cp, char *arg) 1378{ 1379 int i, n = CONSOLE_BAUD_RATE; 1380 1381 if (!cp) 1382 return(-1); 1383 1384 if (arg) 1385 n = simple_strtoul(arg,NULL,0); 1386 1387 for (i = 0; i < ARRAY_SIZE(baud_table); i++) 1388 if (baud_table[i] == n) 1389 break; 1390 if (i < ARRAY_SIZE(baud_table)) { 1391 m68328_console_baud = n; 1392 m68328_console_cbaud = 0; 1393 if (i > 15) { 1394 m68328_console_cbaud |= CBAUDEX; 1395 i -= 15; 1396 } 1397 m68328_console_cbaud |= i; 1398 } 1399 1400 m68328_set_baud(); /* make sure baud rate changes */ 1401 return(0); 1402} 1403 1404 1405static struct tty_driver *m68328_console_device(struct console *c, int *index) 1406{ 1407 *index = c->index; 1408 return serial_driver; 1409} 1410 1411 1412void m68328_console_write (struct console *co, const char *str, 1413 unsigned int count) 1414{ 1415 if (!m68328_console_initted) 1416 m68328_set_baud(); 1417 while (count--) { 1418 if (*str == '\n') 1419 rs_put_char('\r'); 1420 rs_put_char( *str++ ); 1421 } 1422} 1423 1424 1425static struct console m68328_driver = { 1426 .name = "ttyS", 1427 .write = m68328_console_write, 1428 .device = m68328_console_device, 1429 .setup = m68328_console_setup, 1430 .flags = CON_PRINTBUFFER, 1431 .index = -1, 1432}; 1433 1434 1435static int __init m68328_console_init(void) 1436{ 1437 register_console(&m68328_driver); 1438 return 0; 1439} 1440 1441console_initcall(m68328_console_init); 1442