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/serial.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/interrupt.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/major.h>
28 #include <linux/string.h>
29 #include <linux/fcntl.h>
31 #include <linux/kernel.h>
32 #include <linux/console.h>
33 #include <linux/reboot.h>
34 #include <linux/keyboard.h>
35 #include <linux/init.h>
37 #include <linux/bitops.h>
38 #include <linux/delay.h>
39 #include <linux/gfp.h>
43 #include <asm/delay.h>
44 #include <asm/uaccess.h>
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
50 #if defined(CONFIG_M68EZ328)
51 #include <asm/MC68EZ328.h>
53 #if defined(CONFIG_M68VZ328)
54 #include <asm/MC68VZ328.h>
56 #include <asm/MC68328.h>
57 #endif /* CONFIG_M68VZ328 */
58 #endif /* CONFIG_M68EZ328 */
60 #include "68328serial.h"
62 /* Turn off usage of real serial interrupt code, to "support" Copilot */
63 #ifdef CONFIG_XCOPILOT_BUGS
69 static struct m68k_serial m68k_soft[NR_PORTS];
71 static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
73 /* multiple ports are contiguous in memory */
74 m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
76 struct tty_driver *serial_driver;
78 static void change_speed(struct m68k_serial *info);
81 * Setup for console. Argument comes from the boot command line.
84 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
85 #ifdef CONFIG_M68VZ328
86 #define CONSOLE_BAUD_RATE 19200
87 #define DEFAULT_CBAUD B19200
91 #ifndef CONSOLE_BAUD_RATE
92 #define CONSOLE_BAUD_RATE 9600
93 #define DEFAULT_CBAUD B9600
97 static int m68328_console_initted = 0;
98 static int m68328_console_baud = CONSOLE_BAUD_RATE;
99 static int m68328_console_cbaud = DEFAULT_CBAUD;
102 static inline int serial_paranoia_check(struct m68k_serial *info,
103 char *name, const char *routine)
105 #ifdef SERIAL_PARANOIA_CHECK
106 static const char *badmagic =
107 "Warning: bad magic number for serial struct %s in %s\n";
108 static const char *badinfo =
109 "Warning: null m68k_serial for %s in %s\n";
112 printk(badinfo, name, routine);
115 if (info->magic != SERIAL_MAGIC) {
116 printk(badmagic, name, routine);
124 * This is used to figure out the divisor speeds and the timeouts
126 static int baud_table[] = {
127 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
128 9600, 19200, 38400, 57600, 115200, 0 };
130 /* Utility routines */
131 static inline int get_baud(struct m68k_serial *ss)
133 unsigned long result = 115200;
134 unsigned short int baud = uart_addr[ss->line].ubaud;
135 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
136 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
142 * ------------------------------------------------------------
143 * rs_stop() and rs_start()
145 * This routines are called before setting or resetting tty->stopped.
146 * They enable or disable transmitter interrupts, as necessary.
147 * ------------------------------------------------------------
149 static void rs_stop(struct tty_struct *tty)
151 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
152 m68328_uart *uart = &uart_addr[info->line];
155 if (serial_paranoia_check(info, tty->name, "rs_stop"))
158 local_irq_save(flags);
159 uart->ustcnt &= ~USTCNT_TXEN;
160 local_irq_restore(flags);
163 static int rs_put_char(char ch)
165 int flags, loops = 0;
167 local_irq_save(flags);
169 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
176 local_irq_restore(flags);
180 static void rs_start(struct tty_struct *tty)
182 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
183 m68328_uart *uart = &uart_addr[info->line];
186 if (serial_paranoia_check(info, tty->name, "rs_start"))
189 local_irq_save(flags);
190 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
192 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
194 uart->ustcnt |= USTCNT_TXEN;
197 local_irq_restore(flags);
200 static void receive_chars(struct m68k_serial *info, unsigned short rx)
202 struct tty_struct *tty = info->tty;
203 m68328_uart *uart = &uart_addr[info->line];
204 unsigned char ch, flag;
207 * This do { } while() loop will get ALL chars out of Rx FIFO
209 #ifndef CONFIG_XCOPILOT_BUGS
212 ch = GET_FIELD(rx, URX_RXDATA);
215 if(URX_BREAK & rx) { /* whee, break received */
217 #ifdef CONFIG_MAGIC_SYSRQ
218 } else if (ch == 0x10) { /* ^P */
222 /* show_net_buffers(); */
224 } else if (ch == 0x12) { /* ^R */
227 #endif /* CONFIG_MAGIC_SYSRQ */
236 if (rx & URX_PARITY_ERROR)
238 else if (rx & URX_OVRUN)
240 else if (rx & URX_FRAME_ERROR)
243 tty_insert_flip_char(tty, ch, flag);
244 #ifndef CONFIG_XCOPILOT_BUGS
245 } while((rx = uart->urx.w) & URX_DATA_READY);
248 tty_schedule_flip(tty);
254 static void transmit_chars(struct m68k_serial *info)
256 m68328_uart *uart = &uart_addr[info->line];
260 uart->utx.b.txdata = info->x_char;
262 goto clear_and_return;
265 if((info->xmit_cnt <= 0) || info->tty->stopped) {
266 /* That's peculiar... TX ints off */
267 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
268 goto clear_and_return;
272 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
273 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
276 if(info->xmit_cnt <= 0) {
277 /* All done for now... TX ints off */
278 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
279 goto clear_and_return;
283 /* Clear interrupt (should be auto)*/
288 * This is the serial driver's generic interrupt routine
290 irqreturn_t rs_interrupt(int irq, void *dev_id)
292 struct m68k_serial *info = dev_id;
297 uart = &uart_addr[info->line];
303 if (rx & URX_DATA_READY) receive_chars(info, rx);
304 if (tx & UTX_TX_AVAIL) transmit_chars(info);
306 receive_chars(info, rx);
311 static int startup(struct m68k_serial * info)
313 m68328_uart *uart = &uart_addr[info->line];
316 if (info->flags & ASYNC_INITIALIZED)
319 if (!info->xmit_buf) {
320 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
325 local_irq_save(flags);
328 * Clear the FIFO buffers and disable them
329 * (they will be reenabled in change_speed())
332 uart->ustcnt = USTCNT_UEN;
333 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
337 * Finally, enable sequencing and interrupts
340 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
341 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
343 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
347 clear_bit(TTY_IO_ERROR, &info->tty->flags);
348 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
351 * and set the speed of the serial port
356 info->flags |= ASYNC_INITIALIZED;
357 local_irq_restore(flags);
362 * This routine will shutdown a serial port; interrupts are disabled, and
363 * DTR is dropped if the hangup on close termio flag is on.
365 static void shutdown(struct m68k_serial * info)
367 m68328_uart *uart = &uart_addr[info->line];
370 uart->ustcnt = 0; /* All off! */
371 if (!(info->flags & ASYNC_INITIALIZED))
374 local_irq_save(flags);
376 if (info->xmit_buf) {
377 free_page((unsigned long) info->xmit_buf);
382 set_bit(TTY_IO_ERROR, &info->tty->flags);
384 info->flags &= ~ASYNC_INITIALIZED;
385 local_irq_restore(flags);
389 int divisor, prescale;
391 #ifndef CONFIG_M68VZ328
392 hw_baud_table[18] = {
407 {1,0x26}, /* 19200 */
408 {0,0x26}, /* 38400 */
409 {1,0x38}, /* 57600 */
410 {0,0x38}, /* 115200 */
413 hw_baud_table[18] = {
428 {2,0x26}, /* 19200 */
429 {1,0x26}, /* 38400 */
430 {0,0x26}, /* 57600 */
431 {1,0x38}, /* 115200 */
434 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
437 * This routine is called to set the UART divisor registers to match
438 * the specified baud rate for a serial port.
440 static void change_speed(struct m68k_serial *info)
442 m68328_uart *uart = &uart_addr[info->line];
444 unsigned short ustcnt;
448 if (!info->tty || !info->tty->termios)
450 cflag = info->tty->termios->c_cflag;
451 if (!(port = info->port))
454 ustcnt = uart->ustcnt;
455 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
459 i = (i & ~CBAUDEX) + B38400;
462 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
463 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
465 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
467 if ((cflag & CSIZE) == CS8)
468 ustcnt |= USTCNT_8_7;
471 ustcnt |= USTCNT_STOP;
474 ustcnt |= USTCNT_PARITYEN;
476 ustcnt |= USTCNT_ODD_EVEN;
478 #ifdef CONFIG_SERIAL_68328_RTS_CTS
479 if (cflag & CRTSCTS) {
480 uart->utx.w &= ~ UTX_NOCTS;
482 uart->utx.w |= UTX_NOCTS;
486 ustcnt |= USTCNT_TXEN;
488 uart->ustcnt = ustcnt;
493 * Fair output driver allows a process to speak.
495 static void rs_fair_output(void)
497 int left; /* Output no more than that */
499 struct m68k_serial *info = &m68k_soft[0];
502 if (info == 0) return;
503 if (info->xmit_buf == 0) return;
505 local_irq_save(flags);
506 left = info->xmit_cnt;
508 c = info->xmit_buf[info->xmit_tail];
509 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
511 local_irq_restore(flags);
515 local_irq_save(flags);
516 left = min(info->xmit_cnt, left-1);
519 /* Last character is being transmitted now (hopefully). */
522 local_irq_restore(flags);
527 * m68k_console_print is registered for printk.
529 void console_print_68328(const char *p)
533 while((c=*(p++)) != 0) {
539 /* Comment this if you want to have a strict interrupt-driven output */
545 static void rs_set_ldisc(struct tty_struct *tty)
547 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
549 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
552 info->is_cons = (tty->termios->c_line == N_TTY);
554 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
557 static void rs_flush_chars(struct tty_struct *tty)
559 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
560 m68328_uart *uart = &uart_addr[info->line];
563 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
569 /* Enable transmitter */
570 local_irq_save(flags);
572 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
574 local_irq_restore(flags);
579 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
581 uart->ustcnt |= USTCNT_TXEN;
585 if (uart->utx.w & UTX_TX_AVAIL) {
590 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
591 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
596 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
599 local_irq_restore(flags);
602 extern void console_printn(const char * b, int count);
604 static int rs_write(struct tty_struct * tty,
605 const unsigned char *buf, int count)
608 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
609 m68328_uart *uart = &uart_addr[info->line];
612 if (serial_paranoia_check(info, tty->name, "rs_write"))
615 if (!tty || !info->xmit_buf)
618 local_save_flags(flags);
621 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
622 SERIAL_XMIT_SIZE - info->xmit_head));
623 local_irq_restore(flags);
628 memcpy(info->xmit_buf + info->xmit_head, buf, c);
631 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
633 local_irq_restore(flags);
639 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
640 /* Enable transmitter */
643 while(info->xmit_cnt) {
646 uart->ustcnt |= USTCNT_TXEN;
648 uart->ustcnt |= USTCNT_TX_INTR_MASK;
650 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
652 if (uart->utx.w & UTX_TX_AVAIL) {
653 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
654 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
661 local_irq_restore(flags);
667 static int rs_write_room(struct tty_struct *tty)
669 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
672 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
674 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
680 static int rs_chars_in_buffer(struct tty_struct *tty)
682 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
684 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
686 return info->xmit_cnt;
689 static void rs_flush_buffer(struct tty_struct *tty)
691 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
694 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
696 local_irq_save(flags);
697 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
698 local_irq_restore(flags);
703 * ------------------------------------------------------------
706 * This routine is called by the upper-layer tty layer to signal that
707 * incoming characters should be throttled.
708 * ------------------------------------------------------------
710 static void rs_throttle(struct tty_struct * tty)
712 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
714 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
718 info->x_char = STOP_CHAR(tty);
720 /* Turn off RTS line (do this atomic) */
723 static void rs_unthrottle(struct tty_struct * tty)
725 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
727 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
734 info->x_char = START_CHAR(tty);
737 /* Assert RTS line (do this atomic) */
741 * ------------------------------------------------------------
742 * rs_ioctl() and friends
743 * ------------------------------------------------------------
746 static int get_serial_info(struct m68k_serial * info,
747 struct serial_struct * retinfo)
749 struct serial_struct tmp;
753 memset(&tmp, 0, sizeof(tmp));
754 tmp.type = info->type;
755 tmp.line = info->line;
756 tmp.port = info->port;
758 tmp.flags = info->flags;
759 tmp.baud_base = info->baud_base;
760 tmp.close_delay = info->close_delay;
761 tmp.closing_wait = info->closing_wait;
762 tmp.custom_divisor = info->custom_divisor;
763 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
769 static int set_serial_info(struct m68k_serial * info,
770 struct serial_struct * new_info)
772 struct serial_struct new_serial;
773 struct m68k_serial old_info;
778 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
782 if (!capable(CAP_SYS_ADMIN)) {
783 if ((new_serial.baud_base != info->baud_base) ||
784 (new_serial.type != info->type) ||
785 (new_serial.close_delay != info->close_delay) ||
786 ((new_serial.flags & ~ASYNC_USR_MASK) !=
787 (info->flags & ~ASYNC_USR_MASK)))
789 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
790 (new_serial.flags & ASYNC_USR_MASK));
791 info->custom_divisor = new_serial.custom_divisor;
799 * OK, past this point, all the error checking has been done.
800 * At this point, we start making changes.....
803 info->baud_base = new_serial.baud_base;
804 info->flags = ((info->flags & ~ASYNC_FLAGS) |
805 (new_serial.flags & ASYNC_FLAGS));
806 info->type = new_serial.type;
807 info->close_delay = new_serial.close_delay;
808 info->closing_wait = new_serial.closing_wait;
811 retval = startup(info);
816 * get_lsr_info - get line status register info
818 * Purpose: Let user call ioctl() to get info when the UART physically
819 * is emptied. On bus types like RS485, the transmitter must
820 * release the bus after transmitting. This must be done when
821 * the transmit shift register is empty, not be done when the
822 * transmit holding register is empty. This functionality
823 * allows an RS485 driver to be written in user space.
825 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
827 #ifdef CONFIG_SERIAL_68328_RTS_CTS
828 m68328_uart *uart = &uart_addr[info->line];
830 unsigned char status;
833 local_irq_save(flags);
834 #ifdef CONFIG_SERIAL_68328_RTS_CTS
835 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
839 local_irq_restore(flags);
840 return put_user(status, value);
844 * This routine sends a break character out the serial port.
846 static void send_break(struct m68k_serial * info, unsigned int duration)
848 m68328_uart *uart = &uart_addr[info->line];
852 local_irq_save(flags);
854 uart->utx.w |= UTX_SEND_BREAK;
855 msleep_interruptible(duration);
856 uart->utx.w &= ~UTX_SEND_BREAK;
858 local_irq_restore(flags);
861 static int rs_ioctl(struct tty_struct *tty,
862 unsigned int cmd, unsigned long arg)
864 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
867 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
870 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
871 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
872 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
873 if (tty->flags & (1 << TTY_IO_ERROR))
878 case TCSBRK: /* SVID version: non-zero arg --> no break */
879 retval = tty_check_change(tty);
882 tty_wait_until_sent(tty, 0);
884 send_break(info, 250); /* 1/4 second */
886 case TCSBRKP: /* support for POSIX tcsendbreak() */
887 retval = tty_check_change(tty);
890 tty_wait_until_sent(tty, 0);
891 send_break(info, arg ? arg*(100) : 250);
894 return get_serial_info(info,
895 (struct serial_struct *) arg);
897 return set_serial_info(info,
898 (struct serial_struct *) arg);
899 case TIOCSERGETLSR: /* Get line status register */
900 return get_lsr_info(info, (unsigned int *) arg);
902 if (copy_to_user((struct m68k_serial *) arg,
903 info, sizeof(struct m68k_serial)))
912 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
914 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
918 if ((old_termios->c_cflag & CRTSCTS) &&
919 !(tty->termios->c_cflag & CRTSCTS)) {
927 * ------------------------------------------------------------
930 * This routine is called when the serial port gets closed. First, we
931 * wait for the last remaining data to be sent. Then, we unlink its
932 * S structure from the interrupt chain if necessary, and we free
933 * that IRQ if nothing is left in the chain.
934 * ------------------------------------------------------------
936 static void rs_close(struct tty_struct *tty, struct file * filp)
938 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
939 m68328_uart *uart = &uart_addr[info->line];
942 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
945 local_irq_save(flags);
947 if (tty_hung_up_p(filp)) {
948 local_irq_restore(flags);
952 if ((tty->count == 1) && (info->count != 1)) {
954 * Uh, oh. tty->count is 1, which means that the tty
955 * structure will be freed. Info->count should always
956 * be one in these conditions. If it's greater than
957 * one, we've got real problems, since it means the
958 * serial port won't be shutdown.
960 printk("rs_close: bad serial port count; tty->count is 1, "
961 "info->count is %d\n", info->count);
964 if (--info->count < 0) {
965 printk("rs_close: bad serial port count for ttyS%d: %d\n",
966 info->line, info->count);
970 local_irq_restore(flags);
973 info->flags |= ASYNC_CLOSING;
975 * Now we wait for the transmit buffer to clear; and we notify
976 * the line discipline to only process XON/XOFF characters.
979 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
980 tty_wait_until_sent(tty, info->closing_wait);
982 * At this point we stop accepting input. To do this, we
983 * disable the receive line status interrupts, and tell the
984 * interrupt driver to stop checking the data ready bit in the
985 * line status register.
988 uart->ustcnt &= ~USTCNT_RXEN;
989 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
992 rs_flush_buffer(tty);
994 tty_ldisc_flush(tty);
997 #warning "This is not and has never been valid so fix it"
999 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1000 if (tty->ldisc.close)
1001 (tty->ldisc.close)(tty);
1002 tty->ldisc = ldiscs[N_TTY];
1003 tty->termios->c_line = N_TTY;
1004 if (tty->ldisc.open)
1005 (tty->ldisc.open)(tty);
1008 if (info->blocked_open) {
1009 if (info->close_delay) {
1010 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1012 wake_up_interruptible(&info->open_wait);
1014 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1015 wake_up_interruptible(&info->close_wait);
1016 local_irq_restore(flags);
1020 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1022 void rs_hangup(struct tty_struct *tty)
1024 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1026 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1029 rs_flush_buffer(tty);
1032 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1034 wake_up_interruptible(&info->open_wait);
1038 * ------------------------------------------------------------
1039 * rs_open() and friends
1040 * ------------------------------------------------------------
1042 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1043 struct m68k_serial *info)
1045 DECLARE_WAITQUEUE(wait, current);
1050 * If the device is in the middle of being closed, then block
1051 * until it's done, and then try again.
1053 if (info->flags & ASYNC_CLOSING) {
1054 interruptible_sleep_on(&info->close_wait);
1055 #ifdef SERIAL_DO_RESTART
1056 if (info->flags & ASYNC_HUP_NOTIFY)
1059 return -ERESTARTSYS;
1066 * If non-blocking mode is set, or the port is not enabled,
1067 * then make the check up front and then exit.
1069 if ((filp->f_flags & O_NONBLOCK) ||
1070 (tty->flags & (1 << TTY_IO_ERROR))) {
1071 info->flags |= ASYNC_NORMAL_ACTIVE;
1075 if (tty->termios->c_cflag & CLOCAL)
1079 * Block waiting for the carrier detect and the line to become
1080 * free (i.e., not in use by the callout). While we are in
1081 * this loop, info->count is dropped by one, so that
1082 * rs_close() knows when to free things. We restore it upon
1083 * exit, either normal or abnormal.
1086 add_wait_queue(&info->open_wait, &wait);
1089 info->blocked_open++;
1091 current->state = TASK_INTERRUPTIBLE;
1092 if (tty_hung_up_p(filp) ||
1093 !(info->flags & ASYNC_INITIALIZED)) {
1094 #ifdef SERIAL_DO_RESTART
1095 if (info->flags & ASYNC_HUP_NOTIFY)
1098 retval = -ERESTARTSYS;
1104 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
1106 if (signal_pending(current)) {
1107 retval = -ERESTARTSYS;
1114 current->state = TASK_RUNNING;
1115 remove_wait_queue(&info->open_wait, &wait);
1116 if (!tty_hung_up_p(filp))
1118 info->blocked_open--;
1122 info->flags |= ASYNC_NORMAL_ACTIVE;
1127 * This routine is called whenever a serial port is opened. It
1128 * enables interrupts for a serial port, linking in its S structure into
1129 * the IRQ chain. It also performs the serial-specific
1130 * initialization for the tty structure.
1132 int rs_open(struct tty_struct *tty, struct file * filp)
1134 struct m68k_serial *info;
1137 info = &m68k_soft[tty->index];
1139 if (serial_paranoia_check(info, tty->name, "rs_open"))
1143 tty->driver_data = info;
1147 * Start up serial port
1149 retval = startup(info);
1153 return block_til_ready(tty, filp, info);
1156 /* Finally, routines used to initialize the serial driver. */
1158 static void show_serial_version(void)
1160 printk("MC68328 serial driver version 1.00\n");
1163 static const struct tty_operations rs_ops = {
1167 .flush_chars = rs_flush_chars,
1168 .write_room = rs_write_room,
1169 .chars_in_buffer = rs_chars_in_buffer,
1170 .flush_buffer = rs_flush_buffer,
1172 .throttle = rs_throttle,
1173 .unthrottle = rs_unthrottle,
1174 .set_termios = rs_set_termios,
1177 .hangup = rs_hangup,
1178 .set_ldisc = rs_set_ldisc,
1181 /* rs_init inits the driver */
1186 struct m68k_serial *info;
1188 serial_driver = alloc_tty_driver(NR_PORTS);
1192 show_serial_version();
1194 /* Initialize the tty_driver structure */
1195 /* SPARC: Not all of this is exactly right for us. */
1197 serial_driver->name = "ttyS";
1198 serial_driver->major = TTY_MAJOR;
1199 serial_driver->minor_start = 64;
1200 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1201 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1202 serial_driver->init_termios = tty_std_termios;
1203 serial_driver->init_termios.c_cflag =
1204 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1205 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1206 tty_set_operations(serial_driver, &rs_ops);
1208 if (tty_register_driver(serial_driver)) {
1209 put_tty_driver(serial_driver);
1210 printk(KERN_ERR "Couldn't register serial driver\n");
1214 local_irq_save(flags);
1216 for(i=0;i<NR_PORTS;i++) {
1218 info = &m68k_soft[i];
1219 info->magic = SERIAL_MAGIC;
1220 info->port = (int) &uart_addr[i];
1222 info->irq = uart_irqs[i];
1223 info->custom_divisor = 16;
1224 info->close_delay = 50;
1225 info->closing_wait = 3000;
1228 info->blocked_open = 0;
1229 init_waitqueue_head(&info->open_wait);
1230 init_waitqueue_head(&info->close_wait);
1232 info->is_cons = 1; /* Means shortcuts work */
1234 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1235 info->port, info->irq);
1236 printk(" is a builtin MC68328 UART\n");
1238 #ifdef CONFIG_M68VZ328
1240 PJSEL &= 0xCF; /* PSW enable second port output */
1243 if (request_irq(uart_irqs[i],
1246 "M68328_UART", info))
1247 panic("Unable to attach 68328 serial interrupt\n");
1249 local_irq_restore(flags);
1253 module_init(rs68328_init);
1257 static void m68328_set_baud(void)
1259 unsigned short ustcnt;
1263 USTCNT = ustcnt & ~USTCNT_TXEN;
1266 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1267 if (baud_table[i] == m68328_console_baud)
1269 if (i >= ARRAY_SIZE(baud_table)) {
1270 m68328_console_baud = 9600;
1274 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1275 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1276 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1277 ustcnt |= USTCNT_8_7;
1278 ustcnt |= USTCNT_TXEN;
1280 m68328_console_initted = 1;
1285 int m68328_console_setup(struct console *cp, char *arg)
1287 int i, n = CONSOLE_BAUD_RATE;
1293 n = simple_strtoul(arg,NULL,0);
1295 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1296 if (baud_table[i] == n)
1298 if (i < ARRAY_SIZE(baud_table)) {
1299 m68328_console_baud = n;
1300 m68328_console_cbaud = 0;
1302 m68328_console_cbaud |= CBAUDEX;
1305 m68328_console_cbaud |= i;
1308 m68328_set_baud(); /* make sure baud rate changes */
1313 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1316 return serial_driver;
1320 void m68328_console_write (struct console *co, const char *str,
1323 if (!m68328_console_initted)
1328 rs_put_char( *str++ );
1333 static struct console m68328_driver = {
1335 .write = m68328_console_write,
1336 .device = m68328_console_device,
1337 .setup = m68328_console_setup,
1338 .flags = CON_PRINTBUFFER,
1343 static int __init m68328_console_init(void)
1345 register_console(&m68328_driver);
1349 console_initcall(m68328_console_init);