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/system.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_struct m68k_ttys;
77 struct m68k_serial *m68k_consinfo = 0;
79 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
81 struct tty_driver *serial_driver;
83 /* number of characters left in xmit buffer before we ask for more */
84 #define WAKEUP_CHARS 256
86 /* Debugging... DEBUG_INTR is bad to use when one of the zs
87 * lines is your console ;(
89 #undef SERIAL_DEBUG_INTR
90 #undef SERIAL_DEBUG_OPEN
91 #undef SERIAL_DEBUG_FLOW
93 #define RS_ISR_PASS_LIMIT 256
95 static void change_speed(struct m68k_serial *info);
98 * Setup for console. Argument comes from the boot command line.
101 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
102 #ifdef CONFIG_M68VZ328
103 #define CONSOLE_BAUD_RATE 19200
104 #define DEFAULT_CBAUD B19200
108 #ifndef CONSOLE_BAUD_RATE
109 #define CONSOLE_BAUD_RATE 9600
110 #define DEFAULT_CBAUD B9600
114 static int m68328_console_initted = 0;
115 static int m68328_console_baud = CONSOLE_BAUD_RATE;
116 static int m68328_console_cbaud = DEFAULT_CBAUD;
119 static inline int serial_paranoia_check(struct m68k_serial *info,
120 char *name, const char *routine)
122 #ifdef SERIAL_PARANOIA_CHECK
123 static const char *badmagic =
124 "Warning: bad magic number for serial struct %s in %s\n";
125 static const char *badinfo =
126 "Warning: null m68k_serial for %s in %s\n";
129 printk(badinfo, name, routine);
132 if (info->magic != SERIAL_MAGIC) {
133 printk(badmagic, name, routine);
141 * This is used to figure out the divisor speeds and the timeouts
143 static int baud_table[] = {
144 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
145 9600, 19200, 38400, 57600, 115200, 0 };
147 /* Sets or clears DTR/RTS on the requested line */
148 static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
151 /* set the RTS/CTS line */
158 /* Utility routines */
159 static inline int get_baud(struct m68k_serial *ss)
161 unsigned long result = 115200;
162 unsigned short int baud = uart_addr[ss->line].ubaud;
163 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
164 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
170 * ------------------------------------------------------------
171 * rs_stop() and rs_start()
173 * This routines are called before setting or resetting tty->stopped.
174 * They enable or disable transmitter interrupts, as necessary.
175 * ------------------------------------------------------------
177 static void rs_stop(struct tty_struct *tty)
179 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
180 m68328_uart *uart = &uart_addr[info->line];
183 if (serial_paranoia_check(info, tty->name, "rs_stop"))
186 local_irq_save(flags);
187 uart->ustcnt &= ~USTCNT_TXEN;
188 local_irq_restore(flags);
191 static int rs_put_char(char ch)
193 int flags, loops = 0;
195 local_irq_save(flags);
197 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
204 local_irq_restore(flags);
208 static void rs_start(struct tty_struct *tty)
210 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
211 m68328_uart *uart = &uart_addr[info->line];
214 if (serial_paranoia_check(info, tty->name, "rs_start"))
217 local_irq_save(flags);
218 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
220 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
222 uart->ustcnt |= USTCNT_TXEN;
225 local_irq_restore(flags);
228 /* Drop into either the boot monitor or kadb upon receiving a break
229 * from keyboard/console input.
231 static void batten_down_hatches(void)
233 /* Drop into the debugger */
236 static void status_handle(struct m68k_serial *info, unsigned short status)
240 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
241 ((info->curregs[3] & AUTO_ENAB)==0)) {
242 info->curregs[3] |= AUTO_ENAB;
243 info->pendregs[3] |= AUTO_ENAB;
244 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
247 if((info->curregs[3] & AUTO_ENAB)) {
248 info->curregs[3] &= ~AUTO_ENAB;
249 info->pendregs[3] &= ~AUTO_ENAB;
250 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
254 /* If this is console input and this is a
255 * 'break asserted' status change interrupt
256 * see if we can drop into the debugger
258 if((status & URX_BREAK) && info->break_abort)
259 batten_down_hatches();
263 static void receive_chars(struct m68k_serial *info, unsigned short rx)
265 struct tty_struct *tty = info->port.tty;
266 m68328_uart *uart = &uart_addr[info->line];
267 unsigned char ch, flag;
270 * This do { } while() loop will get ALL chars out of Rx FIFO
272 #ifndef CONFIG_XCOPILOT_BUGS
275 ch = GET_FIELD(rx, URX_RXDATA);
278 if(URX_BREAK & rx) { /* whee, break received */
279 status_handle(info, rx);
281 #ifdef CONFIG_MAGIC_SYSRQ
282 } else if (ch == 0x10) { /* ^P */
286 /* show_net_buffers(); */
288 } else if (ch == 0x12) { /* ^R */
291 #endif /* CONFIG_MAGIC_SYSRQ */
300 if(rx & URX_PARITY_ERROR) {
302 status_handle(info, rx);
303 } else if(rx & URX_OVRUN) {
305 status_handle(info, rx);
306 } else if(rx & URX_FRAME_ERROR) {
308 status_handle(info, rx);
310 tty_insert_flip_char(tty, ch, flag);
311 #ifndef CONFIG_XCOPILOT_BUGS
312 } while((rx = uart->urx.w) & URX_DATA_READY);
315 tty_schedule_flip(tty);
321 static void transmit_chars(struct m68k_serial *info)
323 m68328_uart *uart = &uart_addr[info->line];
327 uart->utx.b.txdata = info->x_char;
329 goto clear_and_return;
332 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
333 /* That's peculiar... TX ints off */
334 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
335 goto clear_and_return;
339 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
340 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
343 if (info->xmit_cnt < WAKEUP_CHARS)
344 schedule_work(&info->tqueue);
346 if(info->xmit_cnt <= 0) {
347 /* All done for now... TX ints off */
348 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
349 goto clear_and_return;
353 /* Clear interrupt (should be auto)*/
358 * This is the serial driver's generic interrupt routine
360 irqreturn_t rs_interrupt(int irq, void *dev_id)
362 struct m68k_serial *info = dev_id;
367 uart = &uart_addr[info->line];
373 if (rx & URX_DATA_READY) receive_chars(info, rx);
374 if (tx & UTX_TX_AVAIL) transmit_chars(info);
376 receive_chars(info, rx);
381 static void do_softint(struct work_struct *work)
383 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
384 struct tty_struct *tty;
386 tty = info->port.tty;
390 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
397 * This routine is called from the scheduler tqueue when the interrupt
398 * routine has signalled that a hangup has occurred. The path of
399 * hangup processing is:
401 * serial interrupt routine -> (scheduler tqueue) ->
402 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
405 static void do_serial_hangup(struct work_struct *work)
407 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
408 struct tty_struct *tty;
410 tty = info->port.tty;
418 static int startup(struct m68k_serial * info)
420 m68328_uart *uart = &uart_addr[info->line];
423 if (info->flags & S_INITIALIZED)
426 if (!info->xmit_buf) {
427 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
432 local_irq_save(flags);
435 * Clear the FIFO buffers and disable them
436 * (they will be reenabled in change_speed())
439 uart->ustcnt = USTCNT_UEN;
440 info->xmit_fifo_size = 1;
441 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
445 * Finally, enable sequencing and interrupts
448 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
449 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
451 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
455 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
456 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
459 * and set the speed of the serial port
464 info->flags |= S_INITIALIZED;
465 local_irq_restore(flags);
470 * This routine will shutdown a serial port; interrupts are disabled, and
471 * DTR is dropped if the hangup on close termio flag is on.
473 static void shutdown(struct m68k_serial * info)
475 m68328_uart *uart = &uart_addr[info->line];
478 uart->ustcnt = 0; /* All off! */
479 if (!(info->flags & S_INITIALIZED))
482 local_irq_save(flags);
484 if (info->xmit_buf) {
485 free_page((unsigned long) info->xmit_buf);
490 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
492 info->flags &= ~S_INITIALIZED;
493 local_irq_restore(flags);
497 int divisor, prescale;
499 #ifndef CONFIG_M68VZ328
500 hw_baud_table[18] = {
515 {1,0x26}, /* 19200 */
516 {0,0x26}, /* 38400 */
517 {1,0x38}, /* 57600 */
518 {0,0x38}, /* 115200 */
521 hw_baud_table[18] = {
536 {2,0x26}, /* 19200 */
537 {1,0x26}, /* 38400 */
538 {0,0x26}, /* 57600 */
539 {1,0x38}, /* 115200 */
542 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
545 * This routine is called to set the UART divisor registers to match
546 * the specified baud rate for a serial port.
548 static void change_speed(struct m68k_serial *info)
550 m68328_uart *uart = &uart_addr[info->line];
552 unsigned short ustcnt;
556 if (!info->port.tty || !info->port.tty->termios)
558 cflag = info->port.tty->termios->c_cflag;
559 if (!(port = info->port))
562 ustcnt = uart->ustcnt;
563 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
567 i = (i & ~CBAUDEX) + B38400;
570 info->baud = baud_table[i];
571 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
572 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
574 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
576 if ((cflag & CSIZE) == CS8)
577 ustcnt |= USTCNT_8_7;
580 ustcnt |= USTCNT_STOP;
583 ustcnt |= USTCNT_PARITYEN;
585 ustcnt |= USTCNT_ODD_EVEN;
587 #ifdef CONFIG_SERIAL_68328_RTS_CTS
588 if (cflag & CRTSCTS) {
589 uart->utx.w &= ~ UTX_NOCTS;
591 uart->utx.w |= UTX_NOCTS;
595 ustcnt |= USTCNT_TXEN;
597 uart->ustcnt = ustcnt;
602 * Fair output driver allows a process to speak.
604 static void rs_fair_output(void)
606 int left; /* Output no more than that */
608 struct m68k_serial *info = &m68k_soft[0];
611 if (info == 0) return;
612 if (info->xmit_buf == 0) return;
614 local_irq_save(flags);
615 left = info->xmit_cnt;
617 c = info->xmit_buf[info->xmit_tail];
618 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
620 local_irq_restore(flags);
624 local_irq_save(flags);
625 left = min(info->xmit_cnt, left-1);
628 /* Last character is being transmitted now (hopefully). */
631 local_irq_restore(flags);
636 * m68k_console_print is registered for printk.
638 void console_print_68328(const char *p)
642 while((c=*(p++)) != 0) {
648 /* Comment this if you want to have a strict interrupt-driven output */
654 static void rs_set_ldisc(struct tty_struct *tty)
656 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
658 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
661 info->is_cons = (tty->termios->c_line == N_TTY);
663 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
666 static void rs_flush_chars(struct tty_struct *tty)
668 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
669 m68328_uart *uart = &uart_addr[info->line];
672 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
678 /* Enable transmitter */
679 local_irq_save(flags);
681 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
683 local_irq_restore(flags);
688 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
690 uart->ustcnt |= USTCNT_TXEN;
694 if (uart->utx.w & UTX_TX_AVAIL) {
699 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
700 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
705 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
708 local_irq_restore(flags);
711 extern void console_printn(const char * b, int count);
713 static int rs_write(struct tty_struct * tty,
714 const unsigned char *buf, int count)
717 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
718 m68328_uart *uart = &uart_addr[info->line];
721 if (serial_paranoia_check(info, tty->name, "rs_write"))
724 if (!tty || !info->xmit_buf)
727 local_save_flags(flags);
730 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
731 SERIAL_XMIT_SIZE - info->xmit_head));
732 local_irq_restore(flags);
737 memcpy(info->xmit_buf + info->xmit_head, buf, c);
740 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
742 local_irq_restore(flags);
748 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
749 /* Enable transmitter */
752 while(info->xmit_cnt) {
755 uart->ustcnt |= USTCNT_TXEN;
757 uart->ustcnt |= USTCNT_TX_INTR_MASK;
759 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
761 if (uart->utx.w & UTX_TX_AVAIL) {
762 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
763 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
770 local_irq_restore(flags);
776 static int rs_write_room(struct tty_struct *tty)
778 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
781 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
783 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
789 static int rs_chars_in_buffer(struct tty_struct *tty)
791 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
793 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
795 return info->xmit_cnt;
798 static void rs_flush_buffer(struct tty_struct *tty)
800 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
803 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
805 local_irq_save(flags);
806 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
807 local_irq_restore(flags);
812 * ------------------------------------------------------------
815 * This routine is called by the upper-layer tty layer to signal that
816 * incoming characters should be throttled.
817 * ------------------------------------------------------------
819 static void rs_throttle(struct tty_struct * tty)
821 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
823 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
827 info->x_char = STOP_CHAR(tty);
829 /* Turn off RTS line (do this atomic) */
832 static void rs_unthrottle(struct tty_struct * tty)
834 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
836 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
843 info->x_char = START_CHAR(tty);
846 /* Assert RTS line (do this atomic) */
850 * ------------------------------------------------------------
851 * rs_ioctl() and friends
852 * ------------------------------------------------------------
855 static int get_serial_info(struct m68k_serial * info,
856 struct serial_struct * retinfo)
858 struct serial_struct tmp;
862 memset(&tmp, 0, sizeof(tmp));
863 tmp.type = info->type;
864 tmp.line = info->line;
865 tmp.port = info->port;
867 tmp.flags = info->flags;
868 tmp.baud_base = info->baud_base;
869 tmp.close_delay = info->close_delay;
870 tmp.closing_wait = info->closing_wait;
871 tmp.custom_divisor = info->custom_divisor;
872 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
876 static int set_serial_info(struct m68k_serial * info,
877 struct serial_struct * new_info)
879 struct serial_struct new_serial;
880 struct m68k_serial old_info;
885 copy_from_user(&new_serial,new_info,sizeof(new_serial));
888 if (!capable(CAP_SYS_ADMIN)) {
889 if ((new_serial.baud_base != info->baud_base) ||
890 (new_serial.type != info->type) ||
891 (new_serial.close_delay != info->close_delay) ||
892 ((new_serial.flags & ~S_USR_MASK) !=
893 (info->flags & ~S_USR_MASK)))
895 info->flags = ((info->flags & ~S_USR_MASK) |
896 (new_serial.flags & S_USR_MASK));
897 info->custom_divisor = new_serial.custom_divisor;
905 * OK, past this point, all the error checking has been done.
906 * At this point, we start making changes.....
909 info->baud_base = new_serial.baud_base;
910 info->flags = ((info->flags & ~S_FLAGS) |
911 (new_serial.flags & S_FLAGS));
912 info->type = new_serial.type;
913 info->close_delay = new_serial.close_delay;
914 info->closing_wait = new_serial.closing_wait;
917 retval = startup(info);
922 * get_lsr_info - get line status register info
924 * Purpose: Let user call ioctl() to get info when the UART physically
925 * is emptied. On bus types like RS485, the transmitter must
926 * release the bus after transmitting. This must be done when
927 * the transmit shift register is empty, not be done when the
928 * transmit holding register is empty. This functionality
929 * allows an RS485 driver to be written in user space.
931 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
933 #ifdef CONFIG_SERIAL_68328_RTS_CTS
934 m68328_uart *uart = &uart_addr[info->line];
936 unsigned char status;
939 local_irq_save(flags);
940 #ifdef CONFIG_SERIAL_68328_RTS_CTS
941 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
945 local_irq_restore(flags);
946 put_user(status,value);
951 * This routine sends a break character out the serial port.
953 static void send_break(struct m68k_serial * info, unsigned int duration)
955 m68328_uart *uart = &uart_addr[info->line];
959 local_irq_save(flags);
961 uart->utx.w |= UTX_SEND_BREAK;
962 msleep_interruptible(duration);
963 uart->utx.w &= ~UTX_SEND_BREAK;
965 local_irq_restore(flags);
968 static int rs_ioctl(struct tty_struct *tty, struct file * file,
969 unsigned int cmd, unsigned long arg)
972 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
975 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
978 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
979 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
980 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
981 if (tty->flags & (1 << TTY_IO_ERROR))
986 case TCSBRK: /* SVID version: non-zero arg --> no break */
987 retval = tty_check_change(tty);
990 tty_wait_until_sent(tty, 0);
992 send_break(info, 250); /* 1/4 second */
994 case TCSBRKP: /* support for POSIX tcsendbreak() */
995 retval = tty_check_change(tty);
998 tty_wait_until_sent(tty, 0);
999 send_break(info, arg ? arg*(100) : 250);
1002 if (access_ok(VERIFY_WRITE, (void *) arg,
1003 sizeof(struct serial_struct)))
1004 return get_serial_info(info,
1005 (struct serial_struct *) arg);
1008 return set_serial_info(info,
1009 (struct serial_struct *) arg);
1010 case TIOCSERGETLSR: /* Get line status register */
1011 if (access_ok(VERIFY_WRITE, (void *) arg,
1012 sizeof(unsigned int)))
1013 return get_lsr_info(info, (unsigned int *) arg);
1015 case TIOCSERGSTRUCT:
1016 if (!access_ok(VERIFY_WRITE, (void *) arg,
1017 sizeof(struct m68k_serial)))
1019 copy_to_user((struct m68k_serial *) arg,
1020 info, sizeof(struct m68k_serial));
1024 return -ENOIOCTLCMD;
1029 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1031 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1035 if ((old_termios->c_cflag & CRTSCTS) &&
1036 !(tty->termios->c_cflag & CRTSCTS)) {
1037 tty->hw_stopped = 0;
1044 * ------------------------------------------------------------
1047 * This routine is called when the serial port gets closed. First, we
1048 * wait for the last remaining data to be sent. Then, we unlink its
1049 * S structure from the interrupt chain if necessary, and we free
1050 * that IRQ if nothing is left in the chain.
1051 * ------------------------------------------------------------
1053 static void rs_close(struct tty_struct *tty, struct file * filp)
1055 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1056 m68328_uart *uart = &uart_addr[info->line];
1057 unsigned long flags;
1059 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1062 local_irq_save(flags);
1064 if (tty_hung_up_p(filp)) {
1065 local_irq_restore(flags);
1069 if ((tty->count == 1) && (info->count != 1)) {
1071 * Uh, oh. tty->count is 1, which means that the tty
1072 * structure will be freed. Info->count should always
1073 * be one in these conditions. If it's greater than
1074 * one, we've got real problems, since it means the
1075 * serial port won't be shutdown.
1077 printk("rs_close: bad serial port count; tty->count is 1, "
1078 "info->count is %d\n", info->count);
1081 if (--info->count < 0) {
1082 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1083 info->line, info->count);
1087 local_irq_restore(flags);
1090 info->flags |= S_CLOSING;
1092 * Now we wait for the transmit buffer to clear; and we notify
1093 * the line discipline to only process XON/XOFF characters.
1096 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1097 tty_wait_until_sent(tty, info->closing_wait);
1099 * At this point we stop accepting input. To do this, we
1100 * disable the receive line status interrupts, and tell the
1101 * interrupt driver to stop checking the data ready bit in the
1102 * line status register.
1105 uart->ustcnt &= ~USTCNT_RXEN;
1106 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1109 rs_flush_buffer(tty);
1111 tty_ldisc_flush(tty);
1114 info->port.tty = NULL;
1115 #warning "This is not and has never been valid so fix it"
1117 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1118 if (tty->ldisc.close)
1119 (tty->ldisc.close)(tty);
1120 tty->ldisc = ldiscs[N_TTY];
1121 tty->termios->c_line = N_TTY;
1122 if (tty->ldisc.open)
1123 (tty->ldisc.open)(tty);
1126 if (info->blocked_open) {
1127 if (info->close_delay) {
1128 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1130 wake_up_interruptible(&info->open_wait);
1132 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1133 wake_up_interruptible(&info->close_wait);
1134 local_irq_restore(flags);
1138 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1140 void rs_hangup(struct tty_struct *tty)
1142 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1144 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1147 rs_flush_buffer(tty);
1151 info->flags &= ~S_NORMAL_ACTIVE;
1152 info->port.tty = NULL;
1153 wake_up_interruptible(&info->open_wait);
1157 * ------------------------------------------------------------
1158 * rs_open() and friends
1159 * ------------------------------------------------------------
1161 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1162 struct m68k_serial *info)
1164 DECLARE_WAITQUEUE(wait, current);
1169 * If the device is in the middle of being closed, then block
1170 * until it's done, and then try again.
1172 if (info->flags & S_CLOSING) {
1173 interruptible_sleep_on(&info->close_wait);
1174 #ifdef SERIAL_DO_RESTART
1175 if (info->flags & S_HUP_NOTIFY)
1178 return -ERESTARTSYS;
1185 * If non-blocking mode is set, or the port is not enabled,
1186 * then make the check up front and then exit.
1188 if ((filp->f_flags & O_NONBLOCK) ||
1189 (tty->flags & (1 << TTY_IO_ERROR))) {
1190 info->flags |= S_NORMAL_ACTIVE;
1194 if (tty->termios->c_cflag & CLOCAL)
1198 * Block waiting for the carrier detect and the line to become
1199 * free (i.e., not in use by the callout). While we are in
1200 * this loop, info->count is dropped by one, so that
1201 * rs_close() knows when to free things. We restore it upon
1202 * exit, either normal or abnormal.
1205 add_wait_queue(&info->open_wait, &wait);
1208 info->blocked_open++;
1210 local_irq_disable();
1211 m68k_rtsdtr(info, 1);
1213 current->state = TASK_INTERRUPTIBLE;
1214 if (tty_hung_up_p(filp) ||
1215 !(info->flags & S_INITIALIZED)) {
1216 #ifdef SERIAL_DO_RESTART
1217 if (info->flags & S_HUP_NOTIFY)
1220 retval = -ERESTARTSYS;
1226 if (!(info->flags & S_CLOSING) && do_clocal)
1228 if (signal_pending(current)) {
1229 retval = -ERESTARTSYS;
1236 current->state = TASK_RUNNING;
1237 remove_wait_queue(&info->open_wait, &wait);
1238 if (!tty_hung_up_p(filp))
1240 info->blocked_open--;
1244 info->flags |= S_NORMAL_ACTIVE;
1249 * This routine is called whenever a serial port is opened. It
1250 * enables interrupts for a serial port, linking in its S structure into
1251 * the IRQ chain. It also performs the serial-specific
1252 * initialization for the tty structure.
1254 int rs_open(struct tty_struct *tty, struct file * filp)
1256 struct m68k_serial *info;
1261 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1264 info = &m68k_soft[line];
1266 if (serial_paranoia_check(info, tty->name, "rs_open"))
1270 tty->driver_data = info;
1271 info->port.tty = tty;
1274 * Start up serial port
1276 retval = startup(info);
1280 return block_til_ready(tty, filp, info);
1283 /* Finally, routines used to initialize the serial driver. */
1285 static void show_serial_version(void)
1287 printk("MC68328 serial driver version 1.00\n");
1290 static const struct tty_operations rs_ops = {
1294 .flush_chars = rs_flush_chars,
1295 .write_room = rs_write_room,
1296 .chars_in_buffer = rs_chars_in_buffer,
1297 .flush_buffer = rs_flush_buffer,
1299 .throttle = rs_throttle,
1300 .unthrottle = rs_unthrottle,
1301 .set_termios = rs_set_termios,
1304 .hangup = rs_hangup,
1305 .set_ldisc = rs_set_ldisc,
1308 /* rs_init inits the driver */
1313 struct m68k_serial *info;
1315 serial_driver = alloc_tty_driver(NR_PORTS);
1319 show_serial_version();
1321 /* Initialize the tty_driver structure */
1322 /* SPARC: Not all of this is exactly right for us. */
1324 serial_driver->name = "ttyS";
1325 serial_driver->major = TTY_MAJOR;
1326 serial_driver->minor_start = 64;
1327 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1328 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1329 serial_driver->init_termios = tty_std_termios;
1330 serial_driver->init_termios.c_cflag =
1331 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1332 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1333 tty_set_operations(serial_driver, &rs_ops);
1335 if (tty_register_driver(serial_driver)) {
1336 put_tty_driver(serial_driver);
1337 printk(KERN_ERR "Couldn't register serial driver\n");
1341 local_irq_save(flags);
1343 for(i=0;i<NR_PORTS;i++) {
1345 info = &m68k_soft[i];
1346 info->magic = SERIAL_MAGIC;
1347 info->port = (int) &uart_addr[i];
1348 info->port.tty = NULL;
1349 info->irq = uart_irqs[i];
1350 info->custom_divisor = 16;
1351 info->close_delay = 50;
1352 info->closing_wait = 3000;
1356 info->blocked_open = 0;
1357 INIT_WORK(&info->tqueue, do_softint);
1358 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1359 init_waitqueue_head(&info->open_wait);
1360 init_waitqueue_head(&info->close_wait);
1362 info->is_cons = 1; /* Means shortcuts work */
1364 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1365 info->port, info->irq);
1366 printk(" is a builtin MC68328 UART\n");
1368 #ifdef CONFIG_M68VZ328
1370 PJSEL &= 0xCF; /* PSW enable second port output */
1373 if (request_irq(uart_irqs[i],
1376 "M68328_UART", info))
1377 panic("Unable to attach 68328 serial interrupt\n");
1379 local_irq_restore(flags);
1383 module_init(rs68328_init);
1387 static void m68328_set_baud(void)
1389 unsigned short ustcnt;
1393 USTCNT = ustcnt & ~USTCNT_TXEN;
1396 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1397 if (baud_table[i] == m68328_console_baud)
1399 if (i >= ARRAY_SIZE(baud_table)) {
1400 m68328_console_baud = 9600;
1404 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1405 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1406 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1407 ustcnt |= USTCNT_8_7;
1408 ustcnt |= USTCNT_TXEN;
1410 m68328_console_initted = 1;
1415 int m68328_console_setup(struct console *cp, char *arg)
1417 int i, n = CONSOLE_BAUD_RATE;
1423 n = simple_strtoul(arg,NULL,0);
1425 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1426 if (baud_table[i] == n)
1428 if (i < ARRAY_SIZE(baud_table)) {
1429 m68328_console_baud = n;
1430 m68328_console_cbaud = 0;
1432 m68328_console_cbaud |= CBAUDEX;
1435 m68328_console_cbaud |= i;
1438 m68328_set_baud(); /* make sure baud rate changes */
1443 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1446 return serial_driver;
1450 void m68328_console_write (struct console *co, const char *str,
1453 if (!m68328_console_initted)
1458 rs_put_char( *str++ );
1463 static struct console m68328_driver = {
1465 .write = m68328_console_write,
1466 .device = m68328_console_device,
1467 .setup = m68328_console_setup,
1468 .flags = CON_PRINTBUFFER,
1473 static int __init m68328_console_init(void)
1475 register_console(&m68328_driver);
1479 console_initcall(m68328_console_init);