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/config.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/pm_legacy.h>
38 #include <linux/bitops.h>
39 #include <linux/delay.h>
43 #include <asm/system.h>
44 #include <asm/delay.h>
45 #include <asm/uaccess.h>
48 /* note: perhaps we can murge these files, so that you can just
49 * define 1 of them, and they can sort that out for themselves
51 #if defined(CONFIG_M68EZ328)
52 #include <asm/MC68EZ328.h>
54 #if defined(CONFIG_M68VZ328)
55 #include <asm/MC68VZ328.h>
57 #include <asm/MC68328.h>
58 #endif /* CONFIG_M68VZ328 */
59 #endif /* CONFIG_M68EZ328 */
61 #include "68328serial.h"
63 /* Turn off usage of real serial interrupt code, to "support" Copilot */
64 #ifdef CONFIG_XCOPILOT_BUGS
70 static struct m68k_serial m68k_soft[NR_PORTS];
71 struct m68k_serial *IRQ_ports[NR_IRQS];
73 static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
75 /* multiple ports are contiguous in memory */
76 m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
78 struct tty_struct m68k_ttys;
79 struct m68k_serial *m68k_consinfo = 0;
81 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
84 extern wait_queue_head_t keypress_wait;
87 struct tty_driver *serial_driver;
89 /* serial subtype definitions */
90 #define SERIAL_TYPE_NORMAL 1
92 /* number of characters left in xmit buffer before we ask for more */
93 #define WAKEUP_CHARS 256
95 /* Debugging... DEBUG_INTR is bad to use when one of the zs
96 * lines is your console ;(
98 #undef SERIAL_DEBUG_INTR
99 #undef SERIAL_DEBUG_OPEN
100 #undef SERIAL_DEBUG_FLOW
102 #define RS_ISR_PASS_LIMIT 256
104 static void change_speed(struct m68k_serial *info);
107 * Setup for console. Argument comes from the boot command line.
110 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
111 #define CONSOLE_BAUD_RATE 115200
112 #define DEFAULT_CBAUD B115200
115 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
116 #ifdef CONFIG_M68VZ328
117 #define CONSOLE_BAUD_RATE 19200
118 #define DEFAULT_CBAUD B19200
123 #ifndef CONSOLE_BAUD_RATE
124 #define CONSOLE_BAUD_RATE 9600
125 #define DEFAULT_CBAUD B9600
129 static int m68328_console_initted = 0;
130 static int m68328_console_baud = CONSOLE_BAUD_RATE;
131 static int m68328_console_cbaud = DEFAULT_CBAUD;
135 * tmp_buf is used as a temporary buffer by serial_write. We need to
136 * lock it in case the memcpy_fromfs blocks while swapping in a page,
137 * and some other program tries to do a serial write at the same time.
138 * Since the lock will only come under contention when the system is
139 * swapping and available memory is low, it makes sense to share one
140 * buffer across all the serial ports, since it significantly saves
141 * memory if large numbers of serial ports are open.
143 static unsigned char tmp_buf[SERIAL_XMIT_SIZE]; /* This is cheating */
145 static inline int serial_paranoia_check(struct m68k_serial *info,
146 char *name, const char *routine)
148 #ifdef SERIAL_PARANOIA_CHECK
149 static const char *badmagic =
150 "Warning: bad magic number for serial struct %s in %s\n";
151 static const char *badinfo =
152 "Warning: null m68k_serial for %s in %s\n";
155 printk(badinfo, name, routine);
158 if (info->magic != SERIAL_MAGIC) {
159 printk(badmagic, name, routine);
167 * This is used to figure out the divisor speeds and the timeouts
169 static int baud_table[] = {
170 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
171 9600, 19200, 38400, 57600, 115200, 0 };
173 #define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
175 /* Sets or clears DTR/RTS on the requested line */
176 static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
179 /* set the RTS/CTS line */
186 /* Utility routines */
187 static inline int get_baud(struct m68k_serial *ss)
189 unsigned long result = 115200;
190 unsigned short int baud = uart_addr[ss->line].ubaud;
191 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
192 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
198 * ------------------------------------------------------------
199 * rs_stop() and rs_start()
201 * This routines are called before setting or resetting tty->stopped.
202 * They enable or disable transmitter interrupts, as necessary.
203 * ------------------------------------------------------------
205 static void rs_stop(struct tty_struct *tty)
207 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
208 m68328_uart *uart = &uart_addr[info->line];
211 if (serial_paranoia_check(info, tty->name, "rs_stop"))
214 save_flags(flags); cli();
215 uart->ustcnt &= ~USTCNT_TXEN;
216 restore_flags(flags);
219 static void rs_put_char(char ch)
221 int flags, loops = 0;
223 save_flags(flags); cli();
225 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
232 restore_flags(flags);
235 static void rs_start(struct tty_struct *tty)
237 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
238 m68328_uart *uart = &uart_addr[info->line];
241 if (serial_paranoia_check(info, tty->name, "rs_start"))
244 save_flags(flags); cli();
245 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
247 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
249 uart->ustcnt |= USTCNT_TXEN;
252 restore_flags(flags);
255 /* Drop into either the boot monitor or kadb upon receiving a break
256 * from keyboard/console input.
258 static void batten_down_hatches(void)
260 /* Drop into the debugger */
263 static void status_handle(struct m68k_serial *info, unsigned short status)
267 if((info->tty->termios->c_cflag & CRTSCTS) &&
268 ((info->curregs[3] & AUTO_ENAB)==0)) {
269 info->curregs[3] |= AUTO_ENAB;
270 info->pendregs[3] |= AUTO_ENAB;
271 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
274 if((info->curregs[3] & AUTO_ENAB)) {
275 info->curregs[3] &= ~AUTO_ENAB;
276 info->pendregs[3] &= ~AUTO_ENAB;
277 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
281 /* If this is console input and this is a
282 * 'break asserted' status change interrupt
283 * see if we can drop into the debugger
285 if((status & URX_BREAK) && info->break_abort)
286 batten_down_hatches();
290 static void receive_chars(struct m68k_serial *info, struct pt_regs *regs,
293 struct tty_struct *tty = info->tty;
294 m68328_uart *uart = &uart_addr[info->line];
295 unsigned char ch, flag;
298 * This do { } while() loop will get ALL chars out of Rx FIFO
300 #ifndef CONFIG_XCOPILOT_BUGS
303 ch = GET_FIELD(rx, URX_RXDATA);
306 if(URX_BREAK & rx) { /* whee, break received */
307 status_handle(info, rx);
309 #ifdef CONFIG_MAGIC_SYSRQ
310 } else if (ch == 0x10) { /* ^P */
314 /* show_net_buffers(); */
316 } else if (ch == 0x12) { /* ^R */
319 #endif /* CONFIG_MAGIC_SYSRQ */
321 /* It is a 'keyboard interrupt' ;-) */
322 #ifdef CONFIG_CONSOLE
323 wake_up(&keypress_wait);
331 * Make sure that we do not overflow the buffer
333 if (tty_request_buffer_room(tty, 1) == 0) {
334 tty_schedule_flip(tty);
340 if(rx & URX_PARITY_ERROR) {
342 status_handle(info, rx);
343 } else if(rx & URX_OVRUN) {
345 status_handle(info, rx);
346 } else if(rx & URX_FRAME_ERROR) {
348 status_handle(info, rx);
350 tty_insert_flip_char(tty, ch, flag);
351 #ifndef CONFIG_XCOPILOT_BUGS
352 } while((rx = uart->urx.w) & URX_DATA_READY);
355 tty_schedule_flip(tty);
361 static void transmit_chars(struct m68k_serial *info)
363 m68328_uart *uart = &uart_addr[info->line];
367 uart->utx.b.txdata = info->x_char;
369 goto clear_and_return;
372 if((info->xmit_cnt <= 0) || info->tty->stopped) {
373 /* That's peculiar... TX ints off */
374 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
375 goto clear_and_return;
379 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
380 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
383 if (info->xmit_cnt < WAKEUP_CHARS)
384 schedule_work(&info->tqueue);
386 if(info->xmit_cnt <= 0) {
387 /* All done for now... TX ints off */
388 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
389 goto clear_and_return;
393 /* Clear interrupt (should be auto)*/
398 * This is the serial driver's generic interrupt routine
400 irqreturn_t rs_interrupt(int irq, void *dev_id, struct pt_regs * regs)
402 struct m68k_serial * info;
407 info = IRQ_ports[irq];
411 uart = &uart_addr[info->line];
417 if (rx & URX_DATA_READY) receive_chars(info, regs, rx);
418 if (tx & UTX_TX_AVAIL) transmit_chars(info);
420 receive_chars(info, regs, rx);
425 static void do_softint(void *private)
427 struct m68k_serial *info = (struct m68k_serial *) private;
428 struct tty_struct *tty;
434 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
441 * This routine is called from the scheduler tqueue when the interrupt
442 * routine has signalled that a hangup has occurred. The path of
443 * hangup processing is:
445 * serial interrupt routine -> (scheduler tqueue) ->
446 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
449 static void do_serial_hangup(void *private)
451 struct m68k_serial *info = (struct m68k_serial *) private;
452 struct tty_struct *tty;
462 static int startup(struct m68k_serial * info)
464 m68328_uart *uart = &uart_addr[info->line];
467 if (info->flags & S_INITIALIZED)
470 if (!info->xmit_buf) {
471 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
476 save_flags(flags); cli();
479 * Clear the FIFO buffers and disable them
480 * (they will be reenabled in change_speed())
483 uart->ustcnt = USTCNT_UEN;
484 info->xmit_fifo_size = 1;
485 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
489 * Finally, enable sequencing and interrupts
492 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
493 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
495 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
499 clear_bit(TTY_IO_ERROR, &info->tty->flags);
500 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
503 * and set the speed of the serial port
508 info->flags |= S_INITIALIZED;
509 restore_flags(flags);
514 * This routine will shutdown a serial port; interrupts are disabled, and
515 * DTR is dropped if the hangup on close termio flag is on.
517 static void shutdown(struct m68k_serial * info)
519 m68328_uart *uart = &uart_addr[info->line];
522 uart->ustcnt = 0; /* All off! */
523 if (!(info->flags & S_INITIALIZED))
526 save_flags(flags); cli(); /* Disable interrupts */
528 if (info->xmit_buf) {
529 free_page((unsigned long) info->xmit_buf);
534 set_bit(TTY_IO_ERROR, &info->tty->flags);
536 info->flags &= ~S_INITIALIZED;
537 restore_flags(flags);
541 int divisor, prescale;
543 #ifndef CONFIG_M68VZ328
544 hw_baud_table[18] = {
559 {1,0x26}, /* 19200 */
560 {0,0x26}, /* 38400 */
561 {1,0x38}, /* 57600 */
562 {0,0x38}, /* 115200 */
565 hw_baud_table[18] = {
580 {2,0x26}, /* 19200 */
581 {1,0x26}, /* 38400 */
582 {0,0x26}, /* 57600 */
583 {1,0x38}, /* 115200 */
586 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
589 * This routine is called to set the UART divisor registers to match
590 * the specified baud rate for a serial port.
592 static void change_speed(struct m68k_serial *info)
594 m68328_uart *uart = &uart_addr[info->line];
596 unsigned short ustcnt;
600 if (!info->tty || !info->tty->termios)
602 cflag = info->tty->termios->c_cflag;
603 if (!(port = info->port))
606 ustcnt = uart->ustcnt;
607 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
611 i = (i & ~CBAUDEX) + B38400;
614 info->baud = baud_table[i];
615 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
616 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
618 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
620 if ((cflag & CSIZE) == CS8)
621 ustcnt |= USTCNT_8_7;
624 ustcnt |= USTCNT_STOP;
627 ustcnt |= USTCNT_PARITYEN;
629 ustcnt |= USTCNT_ODD_EVEN;
631 #ifdef CONFIG_SERIAL_68328_RTS_CTS
632 if (cflag & CRTSCTS) {
633 uart->utx.w &= ~ UTX_NOCTS;
635 uart->utx.w |= UTX_NOCTS;
639 ustcnt |= USTCNT_TXEN;
641 uart->ustcnt = ustcnt;
646 * Fair output driver allows a process to speak.
648 static void rs_fair_output(void)
650 int left; /* Output no more than that */
652 struct m68k_serial *info = &m68k_soft[0];
655 if (info == 0) return;
656 if (info->xmit_buf == 0) return;
658 save_flags(flags); cli();
659 left = info->xmit_cnt;
661 c = info->xmit_buf[info->xmit_tail];
662 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
664 restore_flags(flags);
668 save_flags(flags); cli();
669 left = min(info->xmit_cnt, left-1);
672 /* Last character is being transmitted now (hopefully). */
675 restore_flags(flags);
680 * m68k_console_print is registered for printk.
682 void console_print_68328(const char *p)
686 while((c=*(p++)) != 0) {
692 /* Comment this if you want to have a strict interrupt-driven output */
698 static void rs_set_ldisc(struct tty_struct *tty)
700 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
702 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
705 info->is_cons = (tty->termios->c_line == N_TTY);
707 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
710 static void rs_flush_chars(struct tty_struct *tty)
712 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
713 m68328_uart *uart = &uart_addr[info->line];
716 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
722 /* Enable transmitter */
723 save_flags(flags); cli();
725 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
727 restore_flags(flags);
732 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
734 uart->ustcnt |= USTCNT_TXEN;
738 if (uart->utx.w & UTX_TX_AVAIL) {
743 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
744 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
749 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
752 restore_flags(flags);
755 extern void console_printn(const char * b, int count);
757 static int rs_write(struct tty_struct * tty,
758 const unsigned char *buf, int count)
761 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
762 m68328_uart *uart = &uart_addr[info->line];
765 if (serial_paranoia_check(info, tty->name, "rs_write"))
768 if (!tty || !info->xmit_buf)
774 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
775 SERIAL_XMIT_SIZE - info->xmit_head));
779 memcpy(info->xmit_buf + info->xmit_head, buf, c);
780 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
782 restore_flags(flags);
788 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
789 /* Enable transmitter */
792 while(info->xmit_cnt) {
795 uart->ustcnt |= USTCNT_TXEN;
797 uart->ustcnt |= USTCNT_TX_INTR_MASK;
799 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
801 if (uart->utx.w & UTX_TX_AVAIL) {
802 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
803 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
810 restore_flags(flags);
812 restore_flags(flags);
816 static int rs_write_room(struct tty_struct *tty)
818 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
821 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
823 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
829 static int rs_chars_in_buffer(struct tty_struct *tty)
831 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
833 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
835 return info->xmit_cnt;
838 static void rs_flush_buffer(struct tty_struct *tty)
840 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
842 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
845 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
851 * ------------------------------------------------------------
854 * This routine is called by the upper-layer tty layer to signal that
855 * incoming characters should be throttled.
856 * ------------------------------------------------------------
858 static void rs_throttle(struct tty_struct * tty)
860 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
862 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
866 info->x_char = STOP_CHAR(tty);
868 /* Turn off RTS line (do this atomic) */
871 static void rs_unthrottle(struct tty_struct * tty)
873 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
875 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
882 info->x_char = START_CHAR(tty);
885 /* Assert RTS line (do this atomic) */
889 * ------------------------------------------------------------
890 * rs_ioctl() and friends
891 * ------------------------------------------------------------
894 static int get_serial_info(struct m68k_serial * info,
895 struct serial_struct * retinfo)
897 struct serial_struct tmp;
901 memset(&tmp, 0, sizeof(tmp));
902 tmp.type = info->type;
903 tmp.line = info->line;
904 tmp.port = info->port;
906 tmp.flags = info->flags;
907 tmp.baud_base = info->baud_base;
908 tmp.close_delay = info->close_delay;
909 tmp.closing_wait = info->closing_wait;
910 tmp.custom_divisor = info->custom_divisor;
911 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
915 static int set_serial_info(struct m68k_serial * info,
916 struct serial_struct * new_info)
918 struct serial_struct new_serial;
919 struct m68k_serial old_info;
924 copy_from_user(&new_serial,new_info,sizeof(new_serial));
927 if (!capable(CAP_SYS_ADMIN)) {
928 if ((new_serial.baud_base != info->baud_base) ||
929 (new_serial.type != info->type) ||
930 (new_serial.close_delay != info->close_delay) ||
931 ((new_serial.flags & ~S_USR_MASK) !=
932 (info->flags & ~S_USR_MASK)))
934 info->flags = ((info->flags & ~S_USR_MASK) |
935 (new_serial.flags & S_USR_MASK));
936 info->custom_divisor = new_serial.custom_divisor;
944 * OK, past this point, all the error checking has been done.
945 * At this point, we start making changes.....
948 info->baud_base = new_serial.baud_base;
949 info->flags = ((info->flags & ~S_FLAGS) |
950 (new_serial.flags & S_FLAGS));
951 info->type = new_serial.type;
952 info->close_delay = new_serial.close_delay;
953 info->closing_wait = new_serial.closing_wait;
956 retval = startup(info);
961 * get_lsr_info - get line status register info
963 * Purpose: Let user call ioctl() to get info when the UART physically
964 * is emptied. On bus types like RS485, the transmitter must
965 * release the bus after transmitting. This must be done when
966 * the transmit shift register is empty, not be done when the
967 * transmit holding register is empty. This functionality
968 * allows an RS485 driver to be written in user space.
970 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
972 #ifdef CONFIG_SERIAL_68328_RTS_CTS
973 m68328_uart *uart = &uart_addr[info->line];
975 unsigned char status;
978 #ifdef CONFIG_SERIAL_68328_RTS_CTS
979 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
984 put_user(status,value);
989 * This routine sends a break character out the serial port.
991 static void send_break(struct m68k_serial * info, unsigned int duration)
993 m68328_uart *uart = &uart_addr[info->line];
1000 uart->utx.w |= UTX_SEND_BREAK;
1001 msleep_interruptible(duration);
1002 uart->utx.w &= ~UTX_SEND_BREAK;
1004 restore_flags(flags);
1007 static int rs_ioctl(struct tty_struct *tty, struct file * file,
1008 unsigned int cmd, unsigned long arg)
1011 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1014 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1017 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1018 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1019 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1020 if (tty->flags & (1 << TTY_IO_ERROR))
1025 case TCSBRK: /* SVID version: non-zero arg --> no break */
1026 retval = tty_check_change(tty);
1029 tty_wait_until_sent(tty, 0);
1031 send_break(info, 250); /* 1/4 second */
1033 case TCSBRKP: /* support for POSIX tcsendbreak() */
1034 retval = tty_check_change(tty);
1037 tty_wait_until_sent(tty, 0);
1038 send_break(info, arg ? arg*(100) : 250);
1041 error = put_user(C_CLOCAL(tty) ? 1 : 0,
1042 (unsigned long *) arg);
1047 get_user(arg, (unsigned long *) arg);
1048 tty->termios->c_cflag =
1049 ((tty->termios->c_cflag & ~CLOCAL) |
1050 (arg ? CLOCAL : 0));
1053 if (access_ok(VERIFY_WRITE, (void *) arg,
1054 sizeof(struct serial_struct)))
1055 return get_serial_info(info,
1056 (struct serial_struct *) arg);
1059 return set_serial_info(info,
1060 (struct serial_struct *) arg);
1061 case TIOCSERGETLSR: /* Get line status register */
1062 if (access_ok(VERIFY_WRITE, (void *) arg,
1063 sizeof(unsigned int));
1064 return get_lsr_info(info, (unsigned int *) arg);
1066 case TIOCSERGSTRUCT:
1067 if (!access_ok(VERIFY_WRITE, (void *) arg,
1068 sizeof(struct m68k_serial)))
1070 copy_to_user((struct m68k_serial *) arg,
1071 info, sizeof(struct m68k_serial));
1075 return -ENOIOCTLCMD;
1080 static void rs_set_termios(struct tty_struct *tty, struct termios *old_termios)
1082 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1084 if (tty->termios->c_cflag == old_termios->c_cflag)
1089 if ((old_termios->c_cflag & CRTSCTS) &&
1090 !(tty->termios->c_cflag & CRTSCTS)) {
1091 tty->hw_stopped = 0;
1098 * ------------------------------------------------------------
1101 * This routine is called when the serial port gets closed. First, we
1102 * wait for the last remaining data to be sent. Then, we unlink its
1103 * S structure from the interrupt chain if necessary, and we free
1104 * that IRQ if nothing is left in the chain.
1105 * ------------------------------------------------------------
1107 static void rs_close(struct tty_struct *tty, struct file * filp)
1109 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1110 m68328_uart *uart = &uart_addr[info->line];
1111 unsigned long flags;
1113 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1116 save_flags(flags); cli();
1118 if (tty_hung_up_p(filp)) {
1119 restore_flags(flags);
1123 if ((tty->count == 1) && (info->count != 1)) {
1125 * Uh, oh. tty->count is 1, which means that the tty
1126 * structure will be freed. Info->count should always
1127 * be one in these conditions. If it's greater than
1128 * one, we've got real problems, since it means the
1129 * serial port won't be shutdown.
1131 printk("rs_close: bad serial port count; tty->count is 1, "
1132 "info->count is %d\n", info->count);
1135 if (--info->count < 0) {
1136 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1137 info->line, info->count);
1141 restore_flags(flags);
1144 info->flags |= S_CLOSING;
1146 * Now we wait for the transmit buffer to clear; and we notify
1147 * the line discipline to only process XON/XOFF characters.
1150 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1151 tty_wait_until_sent(tty, info->closing_wait);
1153 * At this point we stop accepting input. To do this, we
1154 * disable the receive line status interrupts, and tell the
1155 * interrupt driver to stop checking the data ready bit in the
1156 * line status register.
1159 uart->ustcnt &= ~USTCNT_RXEN;
1160 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1163 if (tty->driver->flush_buffer)
1164 tty->driver->flush_buffer(tty);
1166 tty_ldisc_flush(tty);
1170 #warning "This is not and has never been valid so fix it"
1172 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1173 if (tty->ldisc.close)
1174 (tty->ldisc.close)(tty);
1175 tty->ldisc = ldiscs[N_TTY];
1176 tty->termios->c_line = N_TTY;
1177 if (tty->ldisc.open)
1178 (tty->ldisc.open)(tty);
1181 if (info->blocked_open) {
1182 if (info->close_delay) {
1183 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1185 wake_up_interruptible(&info->open_wait);
1187 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1188 wake_up_interruptible(&info->close_wait);
1189 restore_flags(flags);
1193 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1195 void rs_hangup(struct tty_struct *tty)
1197 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1199 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1202 rs_flush_buffer(tty);
1206 info->flags &= ~S_NORMAL_ACTIVE;
1208 wake_up_interruptible(&info->open_wait);
1212 * ------------------------------------------------------------
1213 * rs_open() and friends
1214 * ------------------------------------------------------------
1216 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1217 struct m68k_serial *info)
1219 DECLARE_WAITQUEUE(wait, current);
1224 * If the device is in the middle of being closed, then block
1225 * until it's done, and then try again.
1227 if (info->flags & S_CLOSING) {
1228 interruptible_sleep_on(&info->close_wait);
1229 #ifdef SERIAL_DO_RESTART
1230 if (info->flags & S_HUP_NOTIFY)
1233 return -ERESTARTSYS;
1240 * If non-blocking mode is set, or the port is not enabled,
1241 * then make the check up front and then exit.
1243 if ((filp->f_flags & O_NONBLOCK) ||
1244 (tty->flags & (1 << TTY_IO_ERROR))) {
1245 info->flags |= S_NORMAL_ACTIVE;
1249 if (tty->termios->c_cflag & CLOCAL)
1253 * Block waiting for the carrier detect and the line to become
1254 * free (i.e., not in use by the callout). While we are in
1255 * this loop, info->count is dropped by one, so that
1256 * rs_close() knows when to free things. We restore it upon
1257 * exit, either normal or abnormal.
1260 add_wait_queue(&info->open_wait, &wait);
1263 info->blocked_open++;
1266 m68k_rtsdtr(info, 1);
1268 current->state = TASK_INTERRUPTIBLE;
1269 if (tty_hung_up_p(filp) ||
1270 !(info->flags & S_INITIALIZED)) {
1271 #ifdef SERIAL_DO_RESTART
1272 if (info->flags & S_HUP_NOTIFY)
1275 retval = -ERESTARTSYS;
1281 if (!(info->flags & S_CLOSING) && do_clocal)
1283 if (signal_pending(current)) {
1284 retval = -ERESTARTSYS;
1289 current->state = TASK_RUNNING;
1290 remove_wait_queue(&info->open_wait, &wait);
1291 if (!tty_hung_up_p(filp))
1293 info->blocked_open--;
1297 info->flags |= S_NORMAL_ACTIVE;
1302 * This routine is called whenever a serial port is opened. It
1303 * enables interrupts for a serial port, linking in its S structure into
1304 * the IRQ chain. It also performs the serial-specific
1305 * initialization for the tty structure.
1307 int rs_open(struct tty_struct *tty, struct file * filp)
1309 struct m68k_serial *info;
1314 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1317 info = &m68k_soft[line];
1319 if (serial_paranoia_check(info, tty->name, "rs_open"))
1323 tty->driver_data = info;
1327 * Start up serial port
1329 retval = startup(info);
1333 return block_til_ready(tty, filp, info);
1336 /* Finally, routines used to initialize the serial driver. */
1338 static void show_serial_version(void)
1340 printk("MC68328 serial driver version 1.00\n");
1343 #ifdef CONFIG_PM_LEGACY
1344 /* Serial Power management
1345 * The console (currently fixed at line 0) is a special case for power
1346 * management because the kernel is so chatty. The console will be
1347 * explicitly disabled my our power manager as the last minute, so we won't
1348 * mess with it here.
1350 static struct pm_dev *serial_pm[NR_PORTS];
1352 static int serial_pm_callback(struct pm_dev *dev, pm_request_t request, void *data)
1354 struct m68k_serial *info = (struct m68k_serial *)dev->data;
1359 /* special case for line 0 - pm restores it */
1375 void shutdown_console(void)
1377 struct m68k_serial *info = &m68k_soft[0];
1379 /* HACK: wait a bit for any pending printk's to be dumped */
1388 void startup_console(void)
1390 struct m68k_serial *info = &m68k_soft[0];
1393 #endif /* CONFIG_PM_LEGACY */
1396 static struct tty_operations rs_ops = {
1400 .flush_chars = rs_flush_chars,
1401 .write_room = rs_write_room,
1402 .chars_in_buffer = rs_chars_in_buffer,
1403 .flush_buffer = rs_flush_buffer,
1405 .throttle = rs_throttle,
1406 .unthrottle = rs_unthrottle,
1407 .set_termios = rs_set_termios,
1410 .hangup = rs_hangup,
1411 .set_ldisc = rs_set_ldisc,
1414 /* rs_init inits the driver */
1419 struct m68k_serial *info;
1421 serial_driver = alloc_tty_driver(NR_PORTS);
1425 show_serial_version();
1427 /* Initialize the tty_driver structure */
1428 /* SPARC: Not all of this is exactly right for us. */
1430 serial_driver->name = "ttyS";
1431 serial_driver->major = TTY_MAJOR;
1432 serial_driver->minor_start = 64;
1433 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1434 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1435 serial_driver->init_termios = tty_std_termios;
1436 serial_driver->init_termios.c_cflag =
1437 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1438 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1439 tty_set_operations(serial_driver, &rs_ops);
1441 if (tty_register_driver(serial_driver)) {
1442 put_tty_driver(serial_driver);
1443 printk(KERN_ERR "Couldn't register serial driver\n");
1447 save_flags(flags); cli();
1449 for(i=0;i<NR_PORTS;i++) {
1451 info = &m68k_soft[i];
1452 info->magic = SERIAL_MAGIC;
1453 info->port = (int) &uart_addr[i];
1455 info->irq = uart_irqs[i];
1456 info->custom_divisor = 16;
1457 info->close_delay = 50;
1458 info->closing_wait = 3000;
1462 info->blocked_open = 0;
1463 INIT_WORK(&info->tqueue, do_softint, info);
1464 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1465 init_waitqueue_head(&info->open_wait);
1466 init_waitqueue_head(&info->close_wait);
1468 info->is_cons = 1; /* Means shortcuts work */
1470 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1471 info->port, info->irq);
1472 printk(" is a builtin MC68328 UART\n");
1474 IRQ_ports[info->irq] = info; /* waste of space */
1476 #ifdef CONFIG_M68VZ328
1478 PJSEL &= 0xCF; /* PSW enable second port output */
1481 if (request_irq(uart_irqs[i],
1484 "M68328_UART", NULL))
1485 panic("Unable to attach 68328 serial interrupt\n");
1486 #ifdef CONFIG_PM_LEGACY
1487 serial_pm[i] = pm_register(PM_SYS_DEV, PM_SYS_COM, serial_pm_callback);
1489 serial_pm[i]->data = info;
1492 restore_flags(flags);
1496 module_init(rs68328_init);
1500 static void m68328_set_baud(void)
1502 unsigned short ustcnt;
1506 USTCNT = ustcnt & ~USTCNT_TXEN;
1509 for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1510 if (baud_table[i] == m68328_console_baud)
1512 if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1513 m68328_console_baud = 9600;
1517 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1518 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1519 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1520 ustcnt |= USTCNT_8_7;
1521 ustcnt |= USTCNT_TXEN;
1523 m68328_console_initted = 1;
1528 int m68328_console_setup(struct console *cp, char *arg)
1530 int i, n = CONSOLE_BAUD_RATE;
1536 n = simple_strtoul(arg,NULL,0);
1538 for (i = 0; i < BAUD_TABLE_SIZE; i++)
1539 if (baud_table[i] == n)
1541 if (i < BAUD_TABLE_SIZE) {
1542 m68328_console_baud = n;
1543 m68328_console_cbaud = 0;
1545 m68328_console_cbaud |= CBAUDEX;
1548 m68328_console_cbaud |= i;
1551 m68328_set_baud(); /* make sure baud rate changes */
1556 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1559 return serial_driver;
1563 void m68328_console_write (struct console *co, const char *str,
1566 if (!m68328_console_initted)
1571 rs_put_char( *str++ );
1576 static struct console m68328_driver = {
1578 .write = m68328_console_write,
1579 .device = m68328_console_device,
1580 .setup = m68328_console_setup,
1581 .flags = CON_PRINTBUFFER,
1586 static int __init m68328_console_init(void)
1588 register_console(&m68328_driver);
1592 console_initcall(m68328_console_init);