1 /* 68328serial.c: Serial port driver for 68328 microcontroller
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>
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
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>
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>
36 #include <linux/bitops.h>
37 #include <linux/delay.h>
38 #include <linux/gfp.h>
42 #include <asm/delay.h>
43 #include <asm/uaccess.h>
46 /* note: perhaps we can murge these files, so that you can just
47 * define 1 of them, and they can sort that out for themselves
49 #if defined(CONFIG_M68EZ328)
50 #include <asm/MC68EZ328.h>
52 #if defined(CONFIG_M68VZ328)
53 #include <asm/MC68VZ328.h>
55 #include <asm/MC68328.h>
56 #endif /* CONFIG_M68VZ328 */
57 #endif /* CONFIG_M68EZ328 */
59 #include "68328serial.h"
61 /* Turn off usage of real serial interrupt code, to "support" Copilot */
62 #ifdef CONFIG_XCOPILOT_BUGS
68 static struct m68k_serial m68k_soft[NR_PORTS];
70 static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
72 /* multiple ports are contiguous in memory */
73 m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
75 struct tty_struct m68k_ttys;
76 struct m68k_serial *m68k_consinfo = 0;
78 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80 struct tty_driver *serial_driver;
82 /* number of characters left in xmit buffer before we ask for more */
83 #define WAKEUP_CHARS 256
85 /* Debugging... DEBUG_INTR is bad to use when one of the zs
86 * lines is your console ;(
88 #undef SERIAL_DEBUG_INTR
89 #undef SERIAL_DEBUG_OPEN
90 #undef SERIAL_DEBUG_FLOW
92 #define RS_ISR_PASS_LIMIT 256
94 static void change_speed(struct m68k_serial *info);
97 * Setup for console. Argument comes from the boot command line.
100 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
101 #ifdef CONFIG_M68VZ328
102 #define CONSOLE_BAUD_RATE 19200
103 #define DEFAULT_CBAUD B19200
107 #ifndef CONSOLE_BAUD_RATE
108 #define CONSOLE_BAUD_RATE 9600
109 #define DEFAULT_CBAUD B9600
113 static int m68328_console_initted = 0;
114 static int m68328_console_baud = CONSOLE_BAUD_RATE;
115 static int m68328_console_cbaud = DEFAULT_CBAUD;
118 static inline int serial_paranoia_check(struct m68k_serial *info,
119 char *name, const char *routine)
121 #ifdef SERIAL_PARANOIA_CHECK
122 static const char *badmagic =
123 "Warning: bad magic number for serial struct %s in %s\n";
124 static const char *badinfo =
125 "Warning: null m68k_serial for %s in %s\n";
128 printk(badinfo, name, routine);
131 if (info->magic != SERIAL_MAGIC) {
132 printk(badmagic, name, routine);
140 * This is used to figure out the divisor speeds and the timeouts
142 static int baud_table[] = {
143 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
144 9600, 19200, 38400, 57600, 115200, 0 };
146 /* Sets or clears DTR/RTS on the requested line */
147 static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
150 /* set the RTS/CTS line */
157 /* Utility routines */
158 static inline int get_baud(struct m68k_serial *ss)
160 unsigned long result = 115200;
161 unsigned short int baud = uart_addr[ss->line].ubaud;
162 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
163 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
169 * ------------------------------------------------------------
170 * rs_stop() and rs_start()
172 * This routines are called before setting or resetting tty->stopped.
173 * They enable or disable transmitter interrupts, as necessary.
174 * ------------------------------------------------------------
176 static void rs_stop(struct tty_struct *tty)
178 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
179 m68328_uart *uart = &uart_addr[info->line];
182 if (serial_paranoia_check(info, tty->name, "rs_stop"))
185 local_irq_save(flags);
186 uart->ustcnt &= ~USTCNT_TXEN;
187 local_irq_restore(flags);
190 static int rs_put_char(char ch)
192 int flags, loops = 0;
194 local_irq_save(flags);
196 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
203 local_irq_restore(flags);
207 static void rs_start(struct tty_struct *tty)
209 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
210 m68328_uart *uart = &uart_addr[info->line];
213 if (serial_paranoia_check(info, tty->name, "rs_start"))
216 local_irq_save(flags);
217 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
219 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
221 uart->ustcnt |= USTCNT_TXEN;
224 local_irq_restore(flags);
227 /* Drop into either the boot monitor or kadb upon receiving a break
228 * from keyboard/console input.
230 static void batten_down_hatches(void)
232 /* Drop into the debugger */
235 static void status_handle(struct m68k_serial *info, unsigned short status)
237 /* If this is console input and this is a
238 * 'break asserted' status change interrupt
239 * see if we can drop into the debugger
241 if((status & URX_BREAK) && info->break_abort)
242 batten_down_hatches();
246 static void receive_chars(struct m68k_serial *info, unsigned short rx)
248 struct tty_struct *tty = info->tty;
249 m68328_uart *uart = &uart_addr[info->line];
250 unsigned char ch, flag;
253 * This do { } while() loop will get ALL chars out of Rx FIFO
255 #ifndef CONFIG_XCOPILOT_BUGS
258 ch = GET_FIELD(rx, URX_RXDATA);
261 if(URX_BREAK & rx) { /* whee, break received */
262 status_handle(info, rx);
264 #ifdef CONFIG_MAGIC_SYSRQ
265 } else if (ch == 0x10) { /* ^P */
269 /* show_net_buffers(); */
271 } else if (ch == 0x12) { /* ^R */
274 #endif /* CONFIG_MAGIC_SYSRQ */
283 if(rx & URX_PARITY_ERROR) {
285 status_handle(info, rx);
286 } else if(rx & URX_OVRUN) {
288 status_handle(info, rx);
289 } else if(rx & URX_FRAME_ERROR) {
291 status_handle(info, rx);
293 tty_insert_flip_char(tty, ch, flag);
294 #ifndef CONFIG_XCOPILOT_BUGS
295 } while((rx = uart->urx.w) & URX_DATA_READY);
298 tty_schedule_flip(tty);
304 static void transmit_chars(struct m68k_serial *info)
306 m68328_uart *uart = &uart_addr[info->line];
310 uart->utx.b.txdata = info->x_char;
312 goto clear_and_return;
315 if((info->xmit_cnt <= 0) || info->tty->stopped) {
316 /* That's peculiar... TX ints off */
317 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
318 goto clear_and_return;
322 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
323 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
326 if(info->xmit_cnt <= 0) {
327 /* All done for now... TX ints off */
328 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
329 goto clear_and_return;
333 /* Clear interrupt (should be auto)*/
338 * This is the serial driver's generic interrupt routine
340 irqreturn_t rs_interrupt(int irq, void *dev_id)
342 struct m68k_serial *info = dev_id;
347 uart = &uart_addr[info->line];
353 if (rx & URX_DATA_READY) receive_chars(info, rx);
354 if (tx & UTX_TX_AVAIL) transmit_chars(info);
356 receive_chars(info, rx);
361 static int startup(struct m68k_serial * info)
363 m68328_uart *uart = &uart_addr[info->line];
366 if (info->flags & S_INITIALIZED)
369 if (!info->xmit_buf) {
370 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
375 local_irq_save(flags);
378 * Clear the FIFO buffers and disable them
379 * (they will be reenabled in change_speed())
382 uart->ustcnt = USTCNT_UEN;
383 info->xmit_fifo_size = 1;
384 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
388 * Finally, enable sequencing and interrupts
391 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
392 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
394 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
398 clear_bit(TTY_IO_ERROR, &info->tty->flags);
399 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
402 * and set the speed of the serial port
407 info->flags |= S_INITIALIZED;
408 local_irq_restore(flags);
413 * This routine will shutdown a serial port; interrupts are disabled, and
414 * DTR is dropped if the hangup on close termio flag is on.
416 static void shutdown(struct m68k_serial * info)
418 m68328_uart *uart = &uart_addr[info->line];
421 uart->ustcnt = 0; /* All off! */
422 if (!(info->flags & S_INITIALIZED))
425 local_irq_save(flags);
427 if (info->xmit_buf) {
428 free_page((unsigned long) info->xmit_buf);
433 set_bit(TTY_IO_ERROR, &info->tty->flags);
435 info->flags &= ~S_INITIALIZED;
436 local_irq_restore(flags);
440 int divisor, prescale;
442 #ifndef CONFIG_M68VZ328
443 hw_baud_table[18] = {
458 {1,0x26}, /* 19200 */
459 {0,0x26}, /* 38400 */
460 {1,0x38}, /* 57600 */
461 {0,0x38}, /* 115200 */
464 hw_baud_table[18] = {
479 {2,0x26}, /* 19200 */
480 {1,0x26}, /* 38400 */
481 {0,0x26}, /* 57600 */
482 {1,0x38}, /* 115200 */
485 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
488 * This routine is called to set the UART divisor registers to match
489 * the specified baud rate for a serial port.
491 static void change_speed(struct m68k_serial *info)
493 m68328_uart *uart = &uart_addr[info->line];
495 unsigned short ustcnt;
499 if (!info->tty || !info->tty->termios)
501 cflag = info->tty->termios->c_cflag;
502 if (!(port = info->port))
505 ustcnt = uart->ustcnt;
506 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
510 i = (i & ~CBAUDEX) + B38400;
513 info->baud = baud_table[i];
514 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
515 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
517 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
519 if ((cflag & CSIZE) == CS8)
520 ustcnt |= USTCNT_8_7;
523 ustcnt |= USTCNT_STOP;
526 ustcnt |= USTCNT_PARITYEN;
528 ustcnt |= USTCNT_ODD_EVEN;
530 #ifdef CONFIG_SERIAL_68328_RTS_CTS
531 if (cflag & CRTSCTS) {
532 uart->utx.w &= ~ UTX_NOCTS;
534 uart->utx.w |= UTX_NOCTS;
538 ustcnt |= USTCNT_TXEN;
540 uart->ustcnt = ustcnt;
545 * Fair output driver allows a process to speak.
547 static void rs_fair_output(void)
549 int left; /* Output no more than that */
551 struct m68k_serial *info = &m68k_soft[0];
554 if (info == 0) return;
555 if (info->xmit_buf == 0) return;
557 local_irq_save(flags);
558 left = info->xmit_cnt;
560 c = info->xmit_buf[info->xmit_tail];
561 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
563 local_irq_restore(flags);
567 local_irq_save(flags);
568 left = min(info->xmit_cnt, left-1);
571 /* Last character is being transmitted now (hopefully). */
574 local_irq_restore(flags);
579 * m68k_console_print is registered for printk.
581 void console_print_68328(const char *p)
585 while((c=*(p++)) != 0) {
591 /* Comment this if you want to have a strict interrupt-driven output */
597 static void rs_set_ldisc(struct tty_struct *tty)
599 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
601 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
604 info->is_cons = (tty->termios->c_line == N_TTY);
606 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
609 static void rs_flush_chars(struct tty_struct *tty)
611 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
612 m68328_uart *uart = &uart_addr[info->line];
615 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
621 /* Enable transmitter */
622 local_irq_save(flags);
624 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
626 local_irq_restore(flags);
631 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
633 uart->ustcnt |= USTCNT_TXEN;
637 if (uart->utx.w & UTX_TX_AVAIL) {
642 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
643 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
648 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
651 local_irq_restore(flags);
654 extern void console_printn(const char * b, int count);
656 static int rs_write(struct tty_struct * tty,
657 const unsigned char *buf, int count)
660 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
661 m68328_uart *uart = &uart_addr[info->line];
664 if (serial_paranoia_check(info, tty->name, "rs_write"))
667 if (!tty || !info->xmit_buf)
670 local_save_flags(flags);
673 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
674 SERIAL_XMIT_SIZE - info->xmit_head));
675 local_irq_restore(flags);
680 memcpy(info->xmit_buf + info->xmit_head, buf, c);
683 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
685 local_irq_restore(flags);
691 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
692 /* Enable transmitter */
695 while(info->xmit_cnt) {
698 uart->ustcnt |= USTCNT_TXEN;
700 uart->ustcnt |= USTCNT_TX_INTR_MASK;
702 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
704 if (uart->utx.w & UTX_TX_AVAIL) {
705 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
706 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
713 local_irq_restore(flags);
719 static int rs_write_room(struct tty_struct *tty)
721 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
724 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
726 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
732 static int rs_chars_in_buffer(struct tty_struct *tty)
734 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
736 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
738 return info->xmit_cnt;
741 static void rs_flush_buffer(struct tty_struct *tty)
743 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
746 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
748 local_irq_save(flags);
749 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
750 local_irq_restore(flags);
755 * ------------------------------------------------------------
758 * This routine is called by the upper-layer tty layer to signal that
759 * incoming characters should be throttled.
760 * ------------------------------------------------------------
762 static void rs_throttle(struct tty_struct * tty)
764 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
766 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
770 info->x_char = STOP_CHAR(tty);
772 /* Turn off RTS line (do this atomic) */
775 static void rs_unthrottle(struct tty_struct * tty)
777 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
786 info->x_char = START_CHAR(tty);
789 /* Assert RTS line (do this atomic) */
793 * ------------------------------------------------------------
794 * rs_ioctl() and friends
795 * ------------------------------------------------------------
798 static int get_serial_info(struct m68k_serial * info,
799 struct serial_struct * retinfo)
801 struct serial_struct tmp;
805 memset(&tmp, 0, sizeof(tmp));
806 tmp.type = info->type;
807 tmp.line = info->line;
808 tmp.port = info->port;
810 tmp.flags = info->flags;
811 tmp.baud_base = info->baud_base;
812 tmp.close_delay = info->close_delay;
813 tmp.closing_wait = info->closing_wait;
814 tmp.custom_divisor = info->custom_divisor;
815 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
821 static int set_serial_info(struct m68k_serial * info,
822 struct serial_struct * new_info)
824 struct serial_struct new_serial;
825 struct m68k_serial old_info;
830 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
834 if (!capable(CAP_SYS_ADMIN)) {
835 if ((new_serial.baud_base != info->baud_base) ||
836 (new_serial.type != info->type) ||
837 (new_serial.close_delay != info->close_delay) ||
838 ((new_serial.flags & ~S_USR_MASK) !=
839 (info->flags & ~S_USR_MASK)))
841 info->flags = ((info->flags & ~S_USR_MASK) |
842 (new_serial.flags & S_USR_MASK));
843 info->custom_divisor = new_serial.custom_divisor;
851 * OK, past this point, all the error checking has been done.
852 * At this point, we start making changes.....
855 info->baud_base = new_serial.baud_base;
856 info->flags = ((info->flags & ~S_FLAGS) |
857 (new_serial.flags & S_FLAGS));
858 info->type = new_serial.type;
859 info->close_delay = new_serial.close_delay;
860 info->closing_wait = new_serial.closing_wait;
863 retval = startup(info);
868 * get_lsr_info - get line status register info
870 * Purpose: Let user call ioctl() to get info when the UART physically
871 * is emptied. On bus types like RS485, the transmitter must
872 * release the bus after transmitting. This must be done when
873 * the transmit shift register is empty, not be done when the
874 * transmit holding register is empty. This functionality
875 * allows an RS485 driver to be written in user space.
877 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
879 #ifdef CONFIG_SERIAL_68328_RTS_CTS
880 m68328_uart *uart = &uart_addr[info->line];
882 unsigned char status;
885 local_irq_save(flags);
886 #ifdef CONFIG_SERIAL_68328_RTS_CTS
887 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
891 local_irq_restore(flags);
892 return put_user(status, value);
896 * This routine sends a break character out the serial port.
898 static void send_break(struct m68k_serial * info, unsigned int duration)
900 m68328_uart *uart = &uart_addr[info->line];
904 local_irq_save(flags);
906 uart->utx.w |= UTX_SEND_BREAK;
907 msleep_interruptible(duration);
908 uart->utx.w &= ~UTX_SEND_BREAK;
910 local_irq_restore(flags);
913 static int rs_ioctl(struct tty_struct *tty,
914 unsigned int cmd, unsigned long arg)
916 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
919 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
922 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
923 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
924 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
925 if (tty->flags & (1 << TTY_IO_ERROR))
930 case TCSBRK: /* SVID version: non-zero arg --> no break */
931 retval = tty_check_change(tty);
934 tty_wait_until_sent(tty, 0);
936 send_break(info, 250); /* 1/4 second */
938 case TCSBRKP: /* support for POSIX tcsendbreak() */
939 retval = tty_check_change(tty);
942 tty_wait_until_sent(tty, 0);
943 send_break(info, arg ? arg*(100) : 250);
946 return get_serial_info(info,
947 (struct serial_struct *) arg);
949 return set_serial_info(info,
950 (struct serial_struct *) arg);
951 case TIOCSERGETLSR: /* Get line status register */
952 return get_lsr_info(info, (unsigned int *) arg);
954 if (copy_to_user((struct m68k_serial *) arg,
955 info, sizeof(struct m68k_serial)))
964 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
966 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
970 if ((old_termios->c_cflag & CRTSCTS) &&
971 !(tty->termios->c_cflag & CRTSCTS)) {
979 * ------------------------------------------------------------
982 * This routine is called when the serial port gets closed. First, we
983 * wait for the last remaining data to be sent. Then, we unlink its
984 * S structure from the interrupt chain if necessary, and we free
985 * that IRQ if nothing is left in the chain.
986 * ------------------------------------------------------------
988 static void rs_close(struct tty_struct *tty, struct file * filp)
990 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
991 m68328_uart *uart = &uart_addr[info->line];
994 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
997 local_irq_save(flags);
999 if (tty_hung_up_p(filp)) {
1000 local_irq_restore(flags);
1004 if ((tty->count == 1) && (info->count != 1)) {
1006 * Uh, oh. tty->count is 1, which means that the tty
1007 * structure will be freed. Info->count should always
1008 * be one in these conditions. If it's greater than
1009 * one, we've got real problems, since it means the
1010 * serial port won't be shutdown.
1012 printk("rs_close: bad serial port count; tty->count is 1, "
1013 "info->count is %d\n", info->count);
1016 if (--info->count < 0) {
1017 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1018 info->line, info->count);
1022 local_irq_restore(flags);
1025 info->flags |= S_CLOSING;
1027 * Now we wait for the transmit buffer to clear; and we notify
1028 * the line discipline to only process XON/XOFF characters.
1031 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1032 tty_wait_until_sent(tty, info->closing_wait);
1034 * At this point we stop accepting input. To do this, we
1035 * disable the receive line status interrupts, and tell the
1036 * interrupt driver to stop checking the data ready bit in the
1037 * line status register.
1040 uart->ustcnt &= ~USTCNT_RXEN;
1041 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1044 rs_flush_buffer(tty);
1046 tty_ldisc_flush(tty);
1050 #warning "This is not and has never been valid so fix it"
1052 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1053 if (tty->ldisc.close)
1054 (tty->ldisc.close)(tty);
1055 tty->ldisc = ldiscs[N_TTY];
1056 tty->termios->c_line = N_TTY;
1057 if (tty->ldisc.open)
1058 (tty->ldisc.open)(tty);
1061 if (info->blocked_open) {
1062 if (info->close_delay) {
1063 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1065 wake_up_interruptible(&info->open_wait);
1067 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1068 wake_up_interruptible(&info->close_wait);
1069 local_irq_restore(flags);
1073 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1075 void rs_hangup(struct tty_struct *tty)
1077 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1079 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1082 rs_flush_buffer(tty);
1086 info->flags &= ~S_NORMAL_ACTIVE;
1088 wake_up_interruptible(&info->open_wait);
1092 * ------------------------------------------------------------
1093 * rs_open() and friends
1094 * ------------------------------------------------------------
1096 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1097 struct m68k_serial *info)
1099 DECLARE_WAITQUEUE(wait, current);
1104 * If the device is in the middle of being closed, then block
1105 * until it's done, and then try again.
1107 if (info->flags & S_CLOSING) {
1108 interruptible_sleep_on(&info->close_wait);
1109 #ifdef SERIAL_DO_RESTART
1110 if (info->flags & S_HUP_NOTIFY)
1113 return -ERESTARTSYS;
1120 * If non-blocking mode is set, or the port is not enabled,
1121 * then make the check up front and then exit.
1123 if ((filp->f_flags & O_NONBLOCK) ||
1124 (tty->flags & (1 << TTY_IO_ERROR))) {
1125 info->flags |= S_NORMAL_ACTIVE;
1129 if (tty->termios->c_cflag & CLOCAL)
1133 * Block waiting for the carrier detect and the line to become
1134 * free (i.e., not in use by the callout). While we are in
1135 * this loop, info->count is dropped by one, so that
1136 * rs_close() knows when to free things. We restore it upon
1137 * exit, either normal or abnormal.
1140 add_wait_queue(&info->open_wait, &wait);
1143 info->blocked_open++;
1145 local_irq_disable();
1146 m68k_rtsdtr(info, 1);
1148 current->state = TASK_INTERRUPTIBLE;
1149 if (tty_hung_up_p(filp) ||
1150 !(info->flags & S_INITIALIZED)) {
1151 #ifdef SERIAL_DO_RESTART
1152 if (info->flags & S_HUP_NOTIFY)
1155 retval = -ERESTARTSYS;
1161 if (!(info->flags & S_CLOSING) && do_clocal)
1163 if (signal_pending(current)) {
1164 retval = -ERESTARTSYS;
1171 current->state = TASK_RUNNING;
1172 remove_wait_queue(&info->open_wait, &wait);
1173 if (!tty_hung_up_p(filp))
1175 info->blocked_open--;
1179 info->flags |= S_NORMAL_ACTIVE;
1184 * This routine is called whenever a serial port is opened. It
1185 * enables interrupts for a serial port, linking in its S structure into
1186 * the IRQ chain. It also performs the serial-specific
1187 * initialization for the tty structure.
1189 int rs_open(struct tty_struct *tty, struct file * filp)
1191 struct m68k_serial *info;
1194 info = &m68k_soft[tty->index];
1196 if (serial_paranoia_check(info, tty->name, "rs_open"))
1200 tty->driver_data = info;
1204 * Start up serial port
1206 retval = startup(info);
1210 return block_til_ready(tty, filp, info);
1213 /* Finally, routines used to initialize the serial driver. */
1215 static void show_serial_version(void)
1217 printk("MC68328 serial driver version 1.00\n");
1220 static const struct tty_operations rs_ops = {
1224 .flush_chars = rs_flush_chars,
1225 .write_room = rs_write_room,
1226 .chars_in_buffer = rs_chars_in_buffer,
1227 .flush_buffer = rs_flush_buffer,
1229 .throttle = rs_throttle,
1230 .unthrottle = rs_unthrottle,
1231 .set_termios = rs_set_termios,
1234 .hangup = rs_hangup,
1235 .set_ldisc = rs_set_ldisc,
1238 /* rs_init inits the driver */
1243 struct m68k_serial *info;
1245 serial_driver = alloc_tty_driver(NR_PORTS);
1249 show_serial_version();
1251 /* Initialize the tty_driver structure */
1252 /* SPARC: Not all of this is exactly right for us. */
1254 serial_driver->name = "ttyS";
1255 serial_driver->major = TTY_MAJOR;
1256 serial_driver->minor_start = 64;
1257 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1258 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1259 serial_driver->init_termios = tty_std_termios;
1260 serial_driver->init_termios.c_cflag =
1261 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1262 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1263 tty_set_operations(serial_driver, &rs_ops);
1265 if (tty_register_driver(serial_driver)) {
1266 put_tty_driver(serial_driver);
1267 printk(KERN_ERR "Couldn't register serial driver\n");
1271 local_irq_save(flags);
1273 for(i=0;i<NR_PORTS;i++) {
1275 info = &m68k_soft[i];
1276 info->magic = SERIAL_MAGIC;
1277 info->port = (int) &uart_addr[i];
1279 info->irq = uart_irqs[i];
1280 info->custom_divisor = 16;
1281 info->close_delay = 50;
1282 info->closing_wait = 3000;
1286 info->blocked_open = 0;
1287 init_waitqueue_head(&info->open_wait);
1288 init_waitqueue_head(&info->close_wait);
1290 info->is_cons = 1; /* Means shortcuts work */
1292 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1293 info->port, info->irq);
1294 printk(" is a builtin MC68328 UART\n");
1296 #ifdef CONFIG_M68VZ328
1298 PJSEL &= 0xCF; /* PSW enable second port output */
1301 if (request_irq(uart_irqs[i],
1304 "M68328_UART", info))
1305 panic("Unable to attach 68328 serial interrupt\n");
1307 local_irq_restore(flags);
1311 module_init(rs68328_init);
1315 static void m68328_set_baud(void)
1317 unsigned short ustcnt;
1321 USTCNT = ustcnt & ~USTCNT_TXEN;
1324 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1325 if (baud_table[i] == m68328_console_baud)
1327 if (i >= ARRAY_SIZE(baud_table)) {
1328 m68328_console_baud = 9600;
1332 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1333 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1334 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1335 ustcnt |= USTCNT_8_7;
1336 ustcnt |= USTCNT_TXEN;
1338 m68328_console_initted = 1;
1343 int m68328_console_setup(struct console *cp, char *arg)
1345 int i, n = CONSOLE_BAUD_RATE;
1351 n = simple_strtoul(arg,NULL,0);
1353 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1354 if (baud_table[i] == n)
1356 if (i < ARRAY_SIZE(baud_table)) {
1357 m68328_console_baud = n;
1358 m68328_console_cbaud = 0;
1360 m68328_console_cbaud |= CBAUDEX;
1363 m68328_console_cbaud |= i;
1366 m68328_set_baud(); /* make sure baud rate changes */
1371 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1374 return serial_driver;
1378 void m68328_console_write (struct console *co, const char *str,
1381 if (!m68328_console_initted)
1386 rs_put_char( *str++ );
1391 static struct console m68328_driver = {
1393 .write = m68328_console_write,
1394 .device = m68328_console_device,
1395 .setup = m68328_console_setup,
1396 .flags = CON_PRINTBUFFER,
1401 static int __init m68328_console_init(void)
1403 register_console(&m68328_driver);
1407 console_initcall(m68328_console_init);