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 /* Turn off usage of real serial interrupt code, to "support" Copilot */
61 #ifdef CONFIG_XCOPILOT_BUGS
68 * I believe this is the optimal setting that reduces the number of interrupts.
69 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
70 * if that bothers you), but in most cases it will not, since we try to
71 * transmit characters every time rs_interrupt is called. Thus, quite often
72 * you'll see that a receive interrupt occures before the transmit one.
73 * -- Vladimir Gurevich
75 #define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
78 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
79 * "Old data interrupt" which occures whenever the data stay in the FIFO
80 * longer than 30 bits time. This allows us to use FIFO without compromising
81 * latency. '328 does not have this feature and without the real 328-based
82 * board I would assume that RXRE is the safest setting.
84 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
85 * interrupts. RXFE (receive queue full) causes the system to lose data
86 * at least at 115200 baud
88 * If your board is busy doing other stuff, you might consider to use
89 * RXRE (data ready intrrupt) instead.
91 * The other option is to make these INTR masks run-time configurable, so
92 * that people can dynamically adapt them according to the current usage.
93 * -- Vladimir Gurevich
97 #if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
98 #define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
99 #elif defined(CONFIG_M68328)
100 #define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
102 #error Please, define the Rx interrupt events for your CPU
107 * This is our internal structure for each serial port's state.
109 * For definitions of the flags field, see serial.h
112 struct tty_port tport;
113 char is_cons; /* Is this our console. */
118 int flags; /* defined in tty.h */
119 int type; /* UART type */
120 struct tty_struct *tty;
122 int x_char; /* xon/xoff character */
124 unsigned short closing_wait;
126 unsigned char *xmit_buf;
132 #define SERIAL_MAGIC 0x5301
135 * Define the number of ports supported and their irqs.
139 static struct m68k_serial m68k_soft[NR_PORTS];
141 static unsigned int uart_irqs[NR_PORTS] = { UART_IRQ_NUM };
143 /* multiple ports are contiguous in memory */
144 m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
146 struct tty_driver *serial_driver;
148 static void change_speed(struct m68k_serial *info);
151 * Setup for console. Argument comes from the boot command line.
154 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
155 #ifdef CONFIG_M68VZ328
156 #define CONSOLE_BAUD_RATE 19200
157 #define DEFAULT_CBAUD B19200
161 #ifndef CONSOLE_BAUD_RATE
162 #define CONSOLE_BAUD_RATE 9600
163 #define DEFAULT_CBAUD B9600
167 static int m68328_console_initted = 0;
168 static int m68328_console_baud = CONSOLE_BAUD_RATE;
169 static int m68328_console_cbaud = DEFAULT_CBAUD;
172 static inline int serial_paranoia_check(struct m68k_serial *info,
173 char *name, const char *routine)
175 #ifdef SERIAL_PARANOIA_CHECK
176 static const char *badmagic =
177 "Warning: bad magic number for serial struct %s in %s\n";
178 static const char *badinfo =
179 "Warning: null m68k_serial for %s in %s\n";
182 printk(badinfo, name, routine);
185 if (info->magic != SERIAL_MAGIC) {
186 printk(badmagic, name, routine);
194 * This is used to figure out the divisor speeds and the timeouts
196 static int baud_table[] = {
197 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
198 9600, 19200, 38400, 57600, 115200, 0 };
200 /* Utility routines */
201 static inline int get_baud(struct m68k_serial *ss)
203 unsigned long result = 115200;
204 unsigned short int baud = uart_addr[ss->line].ubaud;
205 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
206 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
212 * ------------------------------------------------------------
213 * rs_stop() and rs_start()
215 * This routines are called before setting or resetting tty->stopped.
216 * They enable or disable transmitter interrupts, as necessary.
217 * ------------------------------------------------------------
219 static void rs_stop(struct tty_struct *tty)
221 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
222 m68328_uart *uart = &uart_addr[info->line];
225 if (serial_paranoia_check(info, tty->name, "rs_stop"))
228 local_irq_save(flags);
229 uart->ustcnt &= ~USTCNT_TXEN;
230 local_irq_restore(flags);
233 static int rs_put_char(char ch)
238 local_irq_save(flags);
240 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
247 local_irq_restore(flags);
251 static void rs_start(struct tty_struct *tty)
253 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
254 m68328_uart *uart = &uart_addr[info->line];
257 if (serial_paranoia_check(info, tty->name, "rs_start"))
260 local_irq_save(flags);
261 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
263 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
265 uart->ustcnt |= USTCNT_TXEN;
268 local_irq_restore(flags);
271 static void receive_chars(struct m68k_serial *info, unsigned short rx)
273 struct tty_struct *tty = info->tty;
274 m68328_uart *uart = &uart_addr[info->line];
275 unsigned char ch, flag;
278 * This do { } while() loop will get ALL chars out of Rx FIFO
280 #ifndef CONFIG_XCOPILOT_BUGS
283 ch = GET_FIELD(rx, URX_RXDATA);
286 if(URX_BREAK & rx) { /* whee, break received */
288 #ifdef CONFIG_MAGIC_SYSRQ
289 } else if (ch == 0x10) { /* ^P */
293 /* show_net_buffers(); */
295 } else if (ch == 0x12) { /* ^R */
298 #endif /* CONFIG_MAGIC_SYSRQ */
307 if (rx & URX_PARITY_ERROR)
309 else if (rx & URX_OVRUN)
311 else if (rx & URX_FRAME_ERROR)
314 tty_insert_flip_char(tty, ch, flag);
315 #ifndef CONFIG_XCOPILOT_BUGS
316 } while((rx = uart->urx.w) & URX_DATA_READY);
319 tty_schedule_flip(tty);
325 static void transmit_chars(struct m68k_serial *info)
327 m68328_uart *uart = &uart_addr[info->line];
331 uart->utx.b.txdata = info->x_char;
333 goto clear_and_return;
336 if((info->xmit_cnt <= 0) || info->tty->stopped) {
337 /* That's peculiar... TX ints off */
338 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
339 goto clear_and_return;
343 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
344 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
347 if(info->xmit_cnt <= 0) {
348 /* All done for now... TX ints off */
349 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
350 goto clear_and_return;
354 /* Clear interrupt (should be auto)*/
359 * This is the serial driver's generic interrupt routine
361 irqreturn_t rs_interrupt(int irq, void *dev_id)
363 struct m68k_serial *info = dev_id;
368 uart = &uart_addr[info->line];
374 if (rx & URX_DATA_READY) receive_chars(info, rx);
375 if (tx & UTX_TX_AVAIL) transmit_chars(info);
377 receive_chars(info, rx);
382 static int startup(struct m68k_serial * info)
384 m68328_uart *uart = &uart_addr[info->line];
387 if (info->flags & ASYNC_INITIALIZED)
390 if (!info->xmit_buf) {
391 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
396 local_irq_save(flags);
399 * Clear the FIFO buffers and disable them
400 * (they will be reenabled in change_speed())
403 uart->ustcnt = USTCNT_UEN;
404 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
408 * Finally, enable sequencing and interrupts
411 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
412 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
414 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
418 clear_bit(TTY_IO_ERROR, &info->tty->flags);
419 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
422 * and set the speed of the serial port
427 info->flags |= ASYNC_INITIALIZED;
428 local_irq_restore(flags);
433 * This routine will shutdown a serial port; interrupts are disabled, and
434 * DTR is dropped if the hangup on close termio flag is on.
436 static void shutdown(struct m68k_serial * info)
438 m68328_uart *uart = &uart_addr[info->line];
441 uart->ustcnt = 0; /* All off! */
442 if (!(info->flags & ASYNC_INITIALIZED))
445 local_irq_save(flags);
447 if (info->xmit_buf) {
448 free_page((unsigned long) info->xmit_buf);
453 set_bit(TTY_IO_ERROR, &info->tty->flags);
455 info->flags &= ~ASYNC_INITIALIZED;
456 local_irq_restore(flags);
460 int divisor, prescale;
462 #ifndef CONFIG_M68VZ328
463 hw_baud_table[18] = {
478 {1,0x26}, /* 19200 */
479 {0,0x26}, /* 38400 */
480 {1,0x38}, /* 57600 */
481 {0,0x38}, /* 115200 */
484 hw_baud_table[18] = {
499 {2,0x26}, /* 19200 */
500 {1,0x26}, /* 38400 */
501 {0,0x26}, /* 57600 */
502 {1,0x38}, /* 115200 */
505 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
508 * This routine is called to set the UART divisor registers to match
509 * the specified baud rate for a serial port.
511 static void change_speed(struct m68k_serial *info)
513 m68328_uart *uart = &uart_addr[info->line];
515 unsigned short ustcnt;
519 if (!info->tty || !info->tty->termios)
521 cflag = info->tty->termios->c_cflag;
522 if (!(port = info->port))
525 ustcnt = uart->ustcnt;
526 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
530 i = (i & ~CBAUDEX) + B38400;
533 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
534 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
536 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
538 if ((cflag & CSIZE) == CS8)
539 ustcnt |= USTCNT_8_7;
542 ustcnt |= USTCNT_STOP;
545 ustcnt |= USTCNT_PARITYEN;
547 ustcnt |= USTCNT_ODD_EVEN;
549 #ifdef CONFIG_SERIAL_68328_RTS_CTS
550 if (cflag & CRTSCTS) {
551 uart->utx.w &= ~ UTX_NOCTS;
553 uart->utx.w |= UTX_NOCTS;
557 ustcnt |= USTCNT_TXEN;
559 uart->ustcnt = ustcnt;
564 * Fair output driver allows a process to speak.
566 static void rs_fair_output(void)
568 int left; /* Output no more than that */
570 struct m68k_serial *info = &m68k_soft[0];
573 if (info == 0) return;
574 if (info->xmit_buf == 0) return;
576 local_irq_save(flags);
577 left = info->xmit_cnt;
579 c = info->xmit_buf[info->xmit_tail];
580 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
582 local_irq_restore(flags);
586 local_irq_save(flags);
587 left = min(info->xmit_cnt, left-1);
590 /* Last character is being transmitted now (hopefully). */
593 local_irq_restore(flags);
598 * m68k_console_print is registered for printk.
600 void console_print_68328(const char *p)
604 while((c=*(p++)) != 0) {
610 /* Comment this if you want to have a strict interrupt-driven output */
616 static void rs_set_ldisc(struct tty_struct *tty)
618 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
620 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
623 info->is_cons = (tty->termios->c_line == N_TTY);
625 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
628 static void rs_flush_chars(struct tty_struct *tty)
630 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
631 m68328_uart *uart = &uart_addr[info->line];
634 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
640 /* Enable transmitter */
641 local_irq_save(flags);
643 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
645 local_irq_restore(flags);
650 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
652 uart->ustcnt |= USTCNT_TXEN;
656 if (uart->utx.w & UTX_TX_AVAIL) {
661 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
662 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
667 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
670 local_irq_restore(flags);
673 extern void console_printn(const char * b, int count);
675 static int rs_write(struct tty_struct * tty,
676 const unsigned char *buf, int count)
679 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
680 m68328_uart *uart = &uart_addr[info->line];
683 if (serial_paranoia_check(info, tty->name, "rs_write"))
686 if (!tty || !info->xmit_buf)
689 local_save_flags(flags);
692 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
693 SERIAL_XMIT_SIZE - info->xmit_head));
694 local_irq_restore(flags);
699 memcpy(info->xmit_buf + info->xmit_head, buf, c);
702 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
704 local_irq_restore(flags);
710 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
711 /* Enable transmitter */
714 while(info->xmit_cnt) {
717 uart->ustcnt |= USTCNT_TXEN;
719 uart->ustcnt |= USTCNT_TX_INTR_MASK;
721 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
723 if (uart->utx.w & UTX_TX_AVAIL) {
724 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
725 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
732 local_irq_restore(flags);
738 static int rs_write_room(struct tty_struct *tty)
740 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
743 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
745 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
751 static int rs_chars_in_buffer(struct tty_struct *tty)
753 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
755 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
757 return info->xmit_cnt;
760 static void rs_flush_buffer(struct tty_struct *tty)
762 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
765 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
767 local_irq_save(flags);
768 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
769 local_irq_restore(flags);
774 * ------------------------------------------------------------
777 * This routine is called by the upper-layer tty layer to signal that
778 * incoming characters should be throttled.
779 * ------------------------------------------------------------
781 static void rs_throttle(struct tty_struct * tty)
783 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
785 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
789 info->x_char = STOP_CHAR(tty);
791 /* Turn off RTS line (do this atomic) */
794 static void rs_unthrottle(struct tty_struct * tty)
796 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
798 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
805 info->x_char = START_CHAR(tty);
808 /* Assert RTS line (do this atomic) */
812 * ------------------------------------------------------------
813 * rs_ioctl() and friends
814 * ------------------------------------------------------------
817 static int get_serial_info(struct m68k_serial * info,
818 struct serial_struct * retinfo)
820 struct serial_struct tmp;
824 memset(&tmp, 0, sizeof(tmp));
825 tmp.type = info->type;
826 tmp.line = info->line;
827 tmp.port = info->port;
829 tmp.flags = info->flags;
830 tmp.baud_base = info->baud_base;
831 tmp.close_delay = info->close_delay;
832 tmp.closing_wait = info->closing_wait;
833 tmp.custom_divisor = info->custom_divisor;
834 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
840 static int set_serial_info(struct m68k_serial * info,
841 struct serial_struct * new_info)
843 struct serial_struct new_serial;
844 struct m68k_serial old_info;
849 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
853 if (!capable(CAP_SYS_ADMIN)) {
854 if ((new_serial.baud_base != info->baud_base) ||
855 (new_serial.type != info->type) ||
856 (new_serial.close_delay != info->close_delay) ||
857 ((new_serial.flags & ~ASYNC_USR_MASK) !=
858 (info->flags & ~ASYNC_USR_MASK)))
860 info->flags = ((info->flags & ~ASYNC_USR_MASK) |
861 (new_serial.flags & ASYNC_USR_MASK));
862 info->custom_divisor = new_serial.custom_divisor;
866 if (info->tport.count > 1)
870 * OK, past this point, all the error checking has been done.
871 * At this point, we start making changes.....
874 info->baud_base = new_serial.baud_base;
875 info->flags = ((info->flags & ~ASYNC_FLAGS) |
876 (new_serial.flags & ASYNC_FLAGS));
877 info->type = new_serial.type;
878 info->close_delay = new_serial.close_delay;
879 info->closing_wait = new_serial.closing_wait;
882 retval = startup(info);
887 * get_lsr_info - get line status register info
889 * Purpose: Let user call ioctl() to get info when the UART physically
890 * is emptied. On bus types like RS485, the transmitter must
891 * release the bus after transmitting. This must be done when
892 * the transmit shift register is empty, not be done when the
893 * transmit holding register is empty. This functionality
894 * allows an RS485 driver to be written in user space.
896 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
898 #ifdef CONFIG_SERIAL_68328_RTS_CTS
899 m68328_uart *uart = &uart_addr[info->line];
901 unsigned char status;
904 local_irq_save(flags);
905 #ifdef CONFIG_SERIAL_68328_RTS_CTS
906 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
910 local_irq_restore(flags);
911 return put_user(status, value);
915 * This routine sends a break character out the serial port.
917 static void send_break(struct m68k_serial * info, unsigned int duration)
919 m68328_uart *uart = &uart_addr[info->line];
923 local_irq_save(flags);
925 uart->utx.w |= UTX_SEND_BREAK;
926 msleep_interruptible(duration);
927 uart->utx.w &= ~UTX_SEND_BREAK;
929 local_irq_restore(flags);
932 static int rs_ioctl(struct tty_struct *tty,
933 unsigned int cmd, unsigned long arg)
935 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
938 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
941 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
942 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
943 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
944 if (tty->flags & (1 << TTY_IO_ERROR))
949 case TCSBRK: /* SVID version: non-zero arg --> no break */
950 retval = tty_check_change(tty);
953 tty_wait_until_sent(tty, 0);
955 send_break(info, 250); /* 1/4 second */
957 case TCSBRKP: /* support for POSIX tcsendbreak() */
958 retval = tty_check_change(tty);
961 tty_wait_until_sent(tty, 0);
962 send_break(info, arg ? arg*(100) : 250);
965 return get_serial_info(info,
966 (struct serial_struct *) arg);
968 return set_serial_info(info,
969 (struct serial_struct *) arg);
970 case TIOCSERGETLSR: /* Get line status register */
971 return get_lsr_info(info, (unsigned int *) arg);
973 if (copy_to_user((struct m68k_serial *) arg,
974 info, sizeof(struct m68k_serial)))
983 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
985 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
989 if ((old_termios->c_cflag & CRTSCTS) &&
990 !(tty->termios->c_cflag & CRTSCTS)) {
998 * ------------------------------------------------------------
1001 * This routine is called when the serial port gets closed. First, we
1002 * wait for the last remaining data to be sent. Then, we unlink its
1003 * S structure from the interrupt chain if necessary, and we free
1004 * that IRQ if nothing is left in the chain.
1005 * ------------------------------------------------------------
1007 static void rs_close(struct tty_struct *tty, struct file * filp)
1009 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1010 struct tty_port *port = &info->tport;
1011 m68328_uart *uart = &uart_addr[info->line];
1012 unsigned long flags;
1014 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1017 local_irq_save(flags);
1019 if (tty_hung_up_p(filp)) {
1020 local_irq_restore(flags);
1024 if ((tty->count == 1) && (port->count != 1)) {
1026 * Uh, oh. tty->count is 1, which means that the tty
1027 * structure will be freed. Info->count should always
1028 * be one in these conditions. If it's greater than
1029 * one, we've got real problems, since it means the
1030 * serial port won't be shutdown.
1032 printk("rs_close: bad serial port count; tty->count is 1, "
1033 "port->count is %d\n", port->count);
1036 if (--port->count < 0) {
1037 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1038 info->line, port->count);
1042 local_irq_restore(flags);
1045 info->flags |= ASYNC_CLOSING;
1047 * Now we wait for the transmit buffer to clear; and we notify
1048 * the line discipline to only process XON/XOFF characters.
1051 if (info->closing_wait != ASYNC_CLOSING_WAIT_NONE)
1052 tty_wait_until_sent(tty, info->closing_wait);
1054 * At this point we stop accepting input. To do this, we
1055 * disable the receive line status interrupts, and tell the
1056 * interrupt driver to stop checking the data ready bit in the
1057 * line status register.
1060 uart->ustcnt &= ~USTCNT_RXEN;
1061 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1064 rs_flush_buffer(tty);
1066 tty_ldisc_flush(tty);
1069 #warning "This is not and has never been valid so fix it"
1071 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1072 if (tty->ldisc.close)
1073 (tty->ldisc.close)(tty);
1074 tty->ldisc = ldiscs[N_TTY];
1075 tty->termios->c_line = N_TTY;
1076 if (tty->ldisc.open)
1077 (tty->ldisc.open)(tty);
1080 if (port->blocked_open) {
1081 if (info->close_delay) {
1082 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1084 wake_up_interruptible(&port->open_wait);
1086 info->flags &= ~(ASYNC_NORMAL_ACTIVE|ASYNC_CLOSING);
1087 wake_up_interruptible(&port->close_wait);
1088 local_irq_restore(flags);
1092 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1094 void rs_hangup(struct tty_struct *tty)
1096 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1098 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1101 rs_flush_buffer(tty);
1103 info->tport.count = 0;
1104 info->flags &= ~ASYNC_NORMAL_ACTIVE;
1106 wake_up_interruptible(&info->tport.open_wait);
1110 * ------------------------------------------------------------
1111 * rs_open() and friends
1112 * ------------------------------------------------------------
1114 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1115 struct m68k_serial *info)
1117 struct tty_port *port = &info->tport;
1118 DECLARE_WAITQUEUE(wait, current);
1123 * If the device is in the middle of being closed, then block
1124 * until it's done, and then try again.
1126 if (info->flags & ASYNC_CLOSING) {
1127 interruptible_sleep_on(&port->close_wait);
1128 #ifdef SERIAL_DO_RESTART
1129 if (info->flags & ASYNC_HUP_NOTIFY)
1132 return -ERESTARTSYS;
1139 * If non-blocking mode is set, or the port is not enabled,
1140 * then make the check up front and then exit.
1142 if ((filp->f_flags & O_NONBLOCK) ||
1143 (tty->flags & (1 << TTY_IO_ERROR))) {
1144 info->flags |= ASYNC_NORMAL_ACTIVE;
1148 if (tty->termios->c_cflag & CLOCAL)
1152 * Block waiting for the carrier detect and the line to become
1153 * free (i.e., not in use by the callout). While we are in
1154 * this loop, port->count is dropped by one, so that
1155 * rs_close() knows when to free things. We restore it upon
1156 * exit, either normal or abnormal.
1159 add_wait_queue(&port->open_wait, &wait);
1162 port->blocked_open++;
1164 current->state = TASK_INTERRUPTIBLE;
1165 if (tty_hung_up_p(filp) ||
1166 !(info->flags & ASYNC_INITIALIZED)) {
1167 #ifdef SERIAL_DO_RESTART
1168 if (info->flags & ASYNC_HUP_NOTIFY)
1171 retval = -ERESTARTSYS;
1177 if (!(info->flags & ASYNC_CLOSING) && do_clocal)
1179 if (signal_pending(current)) {
1180 retval = -ERESTARTSYS;
1187 current->state = TASK_RUNNING;
1188 remove_wait_queue(&port->open_wait, &wait);
1189 if (!tty_hung_up_p(filp))
1191 port->blocked_open--;
1195 info->flags |= ASYNC_NORMAL_ACTIVE;
1200 * This routine is called whenever a serial port is opened. It
1201 * enables interrupts for a serial port, linking in its S structure into
1202 * the IRQ chain. It also performs the serial-specific
1203 * initialization for the tty structure.
1205 int rs_open(struct tty_struct *tty, struct file * filp)
1207 struct m68k_serial *info;
1210 info = &m68k_soft[tty->index];
1212 if (serial_paranoia_check(info, tty->name, "rs_open"))
1215 info->tport.count++;
1216 tty->driver_data = info;
1220 * Start up serial port
1222 retval = startup(info);
1226 return block_til_ready(tty, filp, info);
1229 /* Finally, routines used to initialize the serial driver. */
1231 static void show_serial_version(void)
1233 printk("MC68328 serial driver version 1.00\n");
1236 static const struct tty_operations rs_ops = {
1240 .flush_chars = rs_flush_chars,
1241 .write_room = rs_write_room,
1242 .chars_in_buffer = rs_chars_in_buffer,
1243 .flush_buffer = rs_flush_buffer,
1245 .throttle = rs_throttle,
1246 .unthrottle = rs_unthrottle,
1247 .set_termios = rs_set_termios,
1250 .hangup = rs_hangup,
1251 .set_ldisc = rs_set_ldisc,
1254 /* rs_init inits the driver */
1258 unsigned long flags;
1260 struct m68k_serial *info;
1262 serial_driver = alloc_tty_driver(NR_PORTS);
1266 show_serial_version();
1268 /* Initialize the tty_driver structure */
1269 /* SPARC: Not all of this is exactly right for us. */
1271 serial_driver->name = "ttyS";
1272 serial_driver->major = TTY_MAJOR;
1273 serial_driver->minor_start = 64;
1274 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1275 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1276 serial_driver->init_termios = tty_std_termios;
1277 serial_driver->init_termios.c_cflag =
1278 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1279 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1280 tty_set_operations(serial_driver, &rs_ops);
1282 if (tty_register_driver(serial_driver)) {
1283 put_tty_driver(serial_driver);
1284 printk(KERN_ERR "Couldn't register serial driver\n");
1288 local_irq_save(flags);
1290 for(i=0;i<NR_PORTS;i++) {
1292 info = &m68k_soft[i];
1293 tty_port_init(&info->tport);
1294 info->magic = SERIAL_MAGIC;
1295 info->port = (int) &uart_addr[i];
1297 info->irq = uart_irqs[i];
1298 info->custom_divisor = 16;
1299 info->close_delay = 50;
1300 info->closing_wait = 3000;
1303 info->is_cons = 1; /* Means shortcuts work */
1305 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1306 info->port, info->irq);
1307 printk(" is a builtin MC68328 UART\n");
1309 #ifdef CONFIG_M68VZ328
1311 PJSEL &= 0xCF; /* PSW enable second port output */
1314 if (request_irq(uart_irqs[i],
1317 "M68328_UART", info))
1318 panic("Unable to attach 68328 serial interrupt\n");
1320 local_irq_restore(flags);
1324 module_init(rs68328_init);
1328 static void m68328_set_baud(void)
1330 unsigned short ustcnt;
1334 USTCNT = ustcnt & ~USTCNT_TXEN;
1337 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1338 if (baud_table[i] == m68328_console_baud)
1340 if (i >= ARRAY_SIZE(baud_table)) {
1341 m68328_console_baud = 9600;
1345 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1346 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1347 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1348 ustcnt |= USTCNT_8_7;
1349 ustcnt |= USTCNT_TXEN;
1351 m68328_console_initted = 1;
1356 int m68328_console_setup(struct console *cp, char *arg)
1358 int i, n = CONSOLE_BAUD_RATE;
1364 n = simple_strtoul(arg,NULL,0);
1366 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1367 if (baud_table[i] == n)
1369 if (i < ARRAY_SIZE(baud_table)) {
1370 m68328_console_baud = n;
1371 m68328_console_cbaud = 0;
1373 m68328_console_cbaud |= CBAUDEX;
1376 m68328_console_cbaud |= i;
1379 m68328_set_baud(); /* make sure baud rate changes */
1384 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1387 return serial_driver;
1391 void m68328_console_write (struct console *co, const char *str,
1394 if (!m68328_console_initted)
1399 rs_put_char( *str++ );
1404 static struct console m68328_driver = {
1406 .write = m68328_console_write,
1407 .device = m68328_console_device,
1408 .setup = m68328_console_setup,
1409 .flags = CON_PRINTBUFFER,
1414 static int __init m68328_console_init(void)
1416 register_console(&m68328_driver);
1420 console_initcall(m68328_console_init);