Remove all #inclusions of asm/system.h
[linux-2.6.git] / drivers / tty / serial / 68328serial.c
blob5ce782529d65c5713551b80a0e2fc8572a45c2b9
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
17 #include <asm/dbg.h>
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>
29 #include <linux/mm.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>
35 #include <linux/pm.h>
36 #include <linux/bitops.h>
37 #include <linux/delay.h>
38 #include <linux/gfp.h>
40 #include <asm/io.h>
41 #include <asm/irq.h>
42 #include <asm/delay.h>
43 #include <asm/uaccess.h>
45 /* (es) */
46 /* note: perhaps we can murge these files, so that you can just
47 * define 1 of them, and they can sort that out for themselves
49 #if defined(CONFIG_M68EZ328)
50 #include <asm/MC68EZ328.h>
51 #else
52 #if defined(CONFIG_M68VZ328)
53 #include <asm/MC68VZ328.h>
54 #else
55 #include <asm/MC68328.h>
56 #endif /* CONFIG_M68VZ328 */
57 #endif /* CONFIG_M68EZ328 */
59 #include "68328serial.h"
61 /* Turn off usage of real serial interrupt code, to "support" Copilot */
62 #ifdef CONFIG_XCOPILOT_BUGS
63 #undef USE_INTS
64 #else
65 #define USE_INTS
66 #endif
68 static struct m68k_serial m68k_soft[NR_PORTS];
70 static unsigned int uart_irqs[NR_PORTS] = UART_IRQ_DEFNS;
72 /* multiple ports are contiguous in memory */
73 m68328_uart *uart_addr = (m68328_uart *)USTCNT_ADDR;
75 struct tty_struct m68k_ttys;
76 struct m68k_serial *m68k_consinfo = 0;
78 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
80 struct tty_driver *serial_driver;
82 /* number of characters left in xmit buffer before we ask for more */
83 #define WAKEUP_CHARS 256
85 /* Debugging... DEBUG_INTR is bad to use when one of the zs
86 * lines is your console ;(
88 #undef SERIAL_DEBUG_INTR
89 #undef SERIAL_DEBUG_OPEN
90 #undef SERIAL_DEBUG_FLOW
92 #define RS_ISR_PASS_LIMIT 256
94 static void change_speed(struct m68k_serial *info);
97 * Setup for console. Argument comes from the boot command line.
100 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
101 #ifdef CONFIG_M68VZ328
102 #define CONSOLE_BAUD_RATE 19200
103 #define DEFAULT_CBAUD B19200
104 #endif
107 #ifndef CONSOLE_BAUD_RATE
108 #define CONSOLE_BAUD_RATE 9600
109 #define DEFAULT_CBAUD B9600
110 #endif
113 static int m68328_console_initted = 0;
114 static int m68328_console_baud = CONSOLE_BAUD_RATE;
115 static int m68328_console_cbaud = DEFAULT_CBAUD;
118 static inline int serial_paranoia_check(struct m68k_serial *info,
119 char *name, const char *routine)
121 #ifdef SERIAL_PARANOIA_CHECK
122 static const char *badmagic =
123 "Warning: bad magic number for serial struct %s in %s\n";
124 static const char *badinfo =
125 "Warning: null m68k_serial for %s in %s\n";
127 if (!info) {
128 printk(badinfo, name, routine);
129 return 1;
131 if (info->magic != SERIAL_MAGIC) {
132 printk(badmagic, name, routine);
133 return 1;
135 #endif
136 return 0;
140 * This is used to figure out the divisor speeds and the timeouts
142 static int baud_table[] = {
143 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
144 9600, 19200, 38400, 57600, 115200, 0 };
146 /* Sets or clears DTR/RTS on the requested line */
147 static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
149 if (set) {
150 /* set the RTS/CTS line */
151 } else {
152 /* clear it */
154 return;
157 /* Utility routines */
158 static inline int get_baud(struct m68k_serial *ss)
160 unsigned long result = 115200;
161 unsigned short int baud = uart_addr[ss->line].ubaud;
162 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
163 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
165 return result;
169 * ------------------------------------------------------------
170 * rs_stop() and rs_start()
172 * This routines are called before setting or resetting tty->stopped.
173 * They enable or disable transmitter interrupts, as necessary.
174 * ------------------------------------------------------------
176 static void rs_stop(struct tty_struct *tty)
178 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
179 m68328_uart *uart = &uart_addr[info->line];
180 unsigned long flags;
182 if (serial_paranoia_check(info, tty->name, "rs_stop"))
183 return;
185 local_irq_save(flags);
186 uart->ustcnt &= ~USTCNT_TXEN;
187 local_irq_restore(flags);
190 static int rs_put_char(char ch)
192 int flags, loops = 0;
194 local_irq_save(flags);
196 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
197 loops++;
198 udelay(5);
201 UTX_TXDATA = ch;
202 udelay(5);
203 local_irq_restore(flags);
204 return 1;
207 static void rs_start(struct tty_struct *tty)
209 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
210 m68328_uart *uart = &uart_addr[info->line];
211 unsigned long flags;
213 if (serial_paranoia_check(info, tty->name, "rs_start"))
214 return;
216 local_irq_save(flags);
217 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
218 #ifdef USE_INTS
219 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
220 #else
221 uart->ustcnt |= USTCNT_TXEN;
222 #endif
224 local_irq_restore(flags);
227 /* Drop into either the boot monitor or kadb upon receiving a break
228 * from keyboard/console input.
230 static void batten_down_hatches(void)
232 /* Drop into the debugger */
235 static void status_handle(struct m68k_serial *info, unsigned short status)
237 /* If this is console input and this is a
238 * 'break asserted' status change interrupt
239 * see if we can drop into the debugger
241 if((status & URX_BREAK) && info->break_abort)
242 batten_down_hatches();
243 return;
246 static void receive_chars(struct m68k_serial *info, unsigned short rx)
248 struct tty_struct *tty = info->tty;
249 m68328_uart *uart = &uart_addr[info->line];
250 unsigned char ch, flag;
253 * This do { } while() loop will get ALL chars out of Rx FIFO
255 #ifndef CONFIG_XCOPILOT_BUGS
256 do {
257 #endif
258 ch = GET_FIELD(rx, URX_RXDATA);
260 if(info->is_cons) {
261 if(URX_BREAK & rx) { /* whee, break received */
262 status_handle(info, rx);
263 return;
264 #ifdef CONFIG_MAGIC_SYSRQ
265 } else if (ch == 0x10) { /* ^P */
266 show_state();
267 show_free_areas(0);
268 show_buffers();
269 /* show_net_buffers(); */
270 return;
271 } else if (ch == 0x12) { /* ^R */
272 emergency_restart();
273 return;
274 #endif /* CONFIG_MAGIC_SYSRQ */
278 if(!tty)
279 goto clear_and_exit;
281 flag = TTY_NORMAL;
283 if(rx & URX_PARITY_ERROR) {
284 flag = TTY_PARITY;
285 status_handle(info, rx);
286 } else if(rx & URX_OVRUN) {
287 flag = TTY_OVERRUN;
288 status_handle(info, rx);
289 } else if(rx & URX_FRAME_ERROR) {
290 flag = TTY_FRAME;
291 status_handle(info, rx);
293 tty_insert_flip_char(tty, ch, flag);
294 #ifndef CONFIG_XCOPILOT_BUGS
295 } while((rx = uart->urx.w) & URX_DATA_READY);
296 #endif
298 tty_schedule_flip(tty);
300 clear_and_exit:
301 return;
304 static void transmit_chars(struct m68k_serial *info)
306 m68328_uart *uart = &uart_addr[info->line];
308 if (info->x_char) {
309 /* Send next char */
310 uart->utx.b.txdata = info->x_char;
311 info->x_char = 0;
312 goto clear_and_return;
315 if((info->xmit_cnt <= 0) || info->tty->stopped) {
316 /* That's peculiar... TX ints off */
317 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
318 goto clear_and_return;
321 /* Send char */
322 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
323 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
324 info->xmit_cnt--;
326 if(info->xmit_cnt <= 0) {
327 /* All done for now... TX ints off */
328 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
329 goto clear_and_return;
332 clear_and_return:
333 /* Clear interrupt (should be auto)*/
334 return;
338 * This is the serial driver's generic interrupt routine
340 irqreturn_t rs_interrupt(int irq, void *dev_id)
342 struct m68k_serial *info = dev_id;
343 m68328_uart *uart;
344 unsigned short rx;
345 unsigned short tx;
347 uart = &uart_addr[info->line];
348 rx = uart->urx.w;
350 #ifdef USE_INTS
351 tx = uart->utx.w;
353 if (rx & URX_DATA_READY) receive_chars(info, rx);
354 if (tx & UTX_TX_AVAIL) transmit_chars(info);
355 #else
356 receive_chars(info, rx);
357 #endif
358 return IRQ_HANDLED;
361 static int startup(struct m68k_serial * info)
363 m68328_uart *uart = &uart_addr[info->line];
364 unsigned long flags;
366 if (info->flags & S_INITIALIZED)
367 return 0;
369 if (!info->xmit_buf) {
370 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
371 if (!info->xmit_buf)
372 return -ENOMEM;
375 local_irq_save(flags);
378 * Clear the FIFO buffers and disable them
379 * (they will be reenabled in change_speed())
382 uart->ustcnt = USTCNT_UEN;
383 info->xmit_fifo_size = 1;
384 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
385 (void)uart->urx.w;
388 * Finally, enable sequencing and interrupts
390 #ifdef USE_INTS
391 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
392 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
393 #else
394 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
395 #endif
397 if (info->tty)
398 clear_bit(TTY_IO_ERROR, &info->tty->flags);
399 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
402 * and set the speed of the serial port
405 change_speed(info);
407 info->flags |= S_INITIALIZED;
408 local_irq_restore(flags);
409 return 0;
413 * This routine will shutdown a serial port; interrupts are disabled, and
414 * DTR is dropped if the hangup on close termio flag is on.
416 static void shutdown(struct m68k_serial * info)
418 m68328_uart *uart = &uart_addr[info->line];
419 unsigned long flags;
421 uart->ustcnt = 0; /* All off! */
422 if (!(info->flags & S_INITIALIZED))
423 return;
425 local_irq_save(flags);
427 if (info->xmit_buf) {
428 free_page((unsigned long) info->xmit_buf);
429 info->xmit_buf = 0;
432 if (info->tty)
433 set_bit(TTY_IO_ERROR, &info->tty->flags);
435 info->flags &= ~S_INITIALIZED;
436 local_irq_restore(flags);
439 struct {
440 int divisor, prescale;
442 #ifndef CONFIG_M68VZ328
443 hw_baud_table[18] = {
444 {0,0}, /* 0 */
445 {0,0}, /* 50 */
446 {0,0}, /* 75 */
447 {0,0}, /* 110 */
448 {0,0}, /* 134 */
449 {0,0}, /* 150 */
450 {0,0}, /* 200 */
451 {7,0x26}, /* 300 */
452 {6,0x26}, /* 600 */
453 {5,0x26}, /* 1200 */
454 {0,0}, /* 1800 */
455 {4,0x26}, /* 2400 */
456 {3,0x26}, /* 4800 */
457 {2,0x26}, /* 9600 */
458 {1,0x26}, /* 19200 */
459 {0,0x26}, /* 38400 */
460 {1,0x38}, /* 57600 */
461 {0,0x38}, /* 115200 */
463 #else
464 hw_baud_table[18] = {
465 {0,0}, /* 0 */
466 {0,0}, /* 50 */
467 {0,0}, /* 75 */
468 {0,0}, /* 110 */
469 {0,0}, /* 134 */
470 {0,0}, /* 150 */
471 {0,0}, /* 200 */
472 {0,0}, /* 300 */
473 {7,0x26}, /* 600 */
474 {6,0x26}, /* 1200 */
475 {0,0}, /* 1800 */
476 {5,0x26}, /* 2400 */
477 {4,0x26}, /* 4800 */
478 {3,0x26}, /* 9600 */
479 {2,0x26}, /* 19200 */
480 {1,0x26}, /* 38400 */
481 {0,0x26}, /* 57600 */
482 {1,0x38}, /* 115200 */
484 #endif
485 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
488 * This routine is called to set the UART divisor registers to match
489 * the specified baud rate for a serial port.
491 static void change_speed(struct m68k_serial *info)
493 m68328_uart *uart = &uart_addr[info->line];
494 unsigned short port;
495 unsigned short ustcnt;
496 unsigned cflag;
497 int i;
499 if (!info->tty || !info->tty->termios)
500 return;
501 cflag = info->tty->termios->c_cflag;
502 if (!(port = info->port))
503 return;
505 ustcnt = uart->ustcnt;
506 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
508 i = cflag & CBAUD;
509 if (i & CBAUDEX) {
510 i = (i & ~CBAUDEX) + B38400;
513 info->baud = baud_table[i];
514 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
515 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
517 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
519 if ((cflag & CSIZE) == CS8)
520 ustcnt |= USTCNT_8_7;
522 if (cflag & CSTOPB)
523 ustcnt |= USTCNT_STOP;
525 if (cflag & PARENB)
526 ustcnt |= USTCNT_PARITYEN;
527 if (cflag & PARODD)
528 ustcnt |= USTCNT_ODD_EVEN;
530 #ifdef CONFIG_SERIAL_68328_RTS_CTS
531 if (cflag & CRTSCTS) {
532 uart->utx.w &= ~ UTX_NOCTS;
533 } else {
534 uart->utx.w |= UTX_NOCTS;
536 #endif
538 ustcnt |= USTCNT_TXEN;
540 uart->ustcnt = ustcnt;
541 return;
545 * Fair output driver allows a process to speak.
547 static void rs_fair_output(void)
549 int left; /* Output no more than that */
550 unsigned long flags;
551 struct m68k_serial *info = &m68k_soft[0];
552 char c;
554 if (info == 0) return;
555 if (info->xmit_buf == 0) return;
557 local_irq_save(flags);
558 left = info->xmit_cnt;
559 while (left != 0) {
560 c = info->xmit_buf[info->xmit_tail];
561 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
562 info->xmit_cnt--;
563 local_irq_restore(flags);
565 rs_put_char(c);
567 local_irq_save(flags);
568 left = min(info->xmit_cnt, left-1);
571 /* Last character is being transmitted now (hopefully). */
572 udelay(5);
574 local_irq_restore(flags);
575 return;
579 * m68k_console_print is registered for printk.
581 void console_print_68328(const char *p)
583 char c;
585 while((c=*(p++)) != 0) {
586 if(c == '\n')
587 rs_put_char('\r');
588 rs_put_char(c);
591 /* Comment this if you want to have a strict interrupt-driven output */
592 rs_fair_output();
594 return;
597 static void rs_set_ldisc(struct tty_struct *tty)
599 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
601 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
602 return;
604 info->is_cons = (tty->termios->c_line == N_TTY);
606 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
609 static void rs_flush_chars(struct tty_struct *tty)
611 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
612 m68328_uart *uart = &uart_addr[info->line];
613 unsigned long flags;
615 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
616 return;
617 #ifndef USE_INTS
618 for(;;) {
619 #endif
621 /* Enable transmitter */
622 local_irq_save(flags);
624 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
625 !info->xmit_buf) {
626 local_irq_restore(flags);
627 return;
630 #ifdef USE_INTS
631 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
632 #else
633 uart->ustcnt |= USTCNT_TXEN;
634 #endif
636 #ifdef USE_INTS
637 if (uart->utx.w & UTX_TX_AVAIL) {
638 #else
639 if (1) {
640 #endif
641 /* Send char */
642 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
643 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
644 info->xmit_cnt--;
647 #ifndef USE_INTS
648 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
650 #endif
651 local_irq_restore(flags);
654 extern void console_printn(const char * b, int count);
656 static int rs_write(struct tty_struct * tty,
657 const unsigned char *buf, int count)
659 int c, total = 0;
660 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
661 m68328_uart *uart = &uart_addr[info->line];
662 unsigned long flags;
664 if (serial_paranoia_check(info, tty->name, "rs_write"))
665 return 0;
667 if (!tty || !info->xmit_buf)
668 return 0;
670 local_save_flags(flags);
671 while (1) {
672 local_irq_disable();
673 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
674 SERIAL_XMIT_SIZE - info->xmit_head));
675 local_irq_restore(flags);
677 if (c <= 0)
678 break;
680 memcpy(info->xmit_buf + info->xmit_head, buf, c);
682 local_irq_disable();
683 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
684 info->xmit_cnt += c;
685 local_irq_restore(flags);
686 buf += c;
687 count -= c;
688 total += c;
691 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
692 /* Enable transmitter */
693 local_irq_disable();
694 #ifndef USE_INTS
695 while(info->xmit_cnt) {
696 #endif
698 uart->ustcnt |= USTCNT_TXEN;
699 #ifdef USE_INTS
700 uart->ustcnt |= USTCNT_TX_INTR_MASK;
701 #else
702 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
703 #endif
704 if (uart->utx.w & UTX_TX_AVAIL) {
705 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
706 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
707 info->xmit_cnt--;
710 #ifndef USE_INTS
712 #endif
713 local_irq_restore(flags);
716 return total;
719 static int rs_write_room(struct tty_struct *tty)
721 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
722 int ret;
724 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
725 return 0;
726 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
727 if (ret < 0)
728 ret = 0;
729 return ret;
732 static int rs_chars_in_buffer(struct tty_struct *tty)
734 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
736 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
737 return 0;
738 return info->xmit_cnt;
741 static void rs_flush_buffer(struct tty_struct *tty)
743 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
744 unsigned long flags;
746 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
747 return;
748 local_irq_save(flags);
749 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
750 local_irq_restore(flags);
751 tty_wakeup(tty);
755 * ------------------------------------------------------------
756 * rs_throttle()
758 * This routine is called by the upper-layer tty layer to signal that
759 * incoming characters should be throttled.
760 * ------------------------------------------------------------
762 static void rs_throttle(struct tty_struct * tty)
764 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
766 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
767 return;
769 if (I_IXOFF(tty))
770 info->x_char = STOP_CHAR(tty);
772 /* Turn off RTS line (do this atomic) */
775 static void rs_unthrottle(struct tty_struct * tty)
777 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
779 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
780 return;
782 if (I_IXOFF(tty)) {
783 if (info->x_char)
784 info->x_char = 0;
785 else
786 info->x_char = START_CHAR(tty);
789 /* Assert RTS line (do this atomic) */
793 * ------------------------------------------------------------
794 * rs_ioctl() and friends
795 * ------------------------------------------------------------
798 static int get_serial_info(struct m68k_serial * info,
799 struct serial_struct * retinfo)
801 struct serial_struct tmp;
803 if (!retinfo)
804 return -EFAULT;
805 memset(&tmp, 0, sizeof(tmp));
806 tmp.type = info->type;
807 tmp.line = info->line;
808 tmp.port = info->port;
809 tmp.irq = info->irq;
810 tmp.flags = info->flags;
811 tmp.baud_base = info->baud_base;
812 tmp.close_delay = info->close_delay;
813 tmp.closing_wait = info->closing_wait;
814 tmp.custom_divisor = info->custom_divisor;
815 if (copy_to_user(retinfo, &tmp, sizeof(*retinfo)))
816 return -EFAULT;
818 return 0;
821 static int set_serial_info(struct m68k_serial * info,
822 struct serial_struct * new_info)
824 struct serial_struct new_serial;
825 struct m68k_serial old_info;
826 int retval = 0;
828 if (!new_info)
829 return -EFAULT;
830 if (copy_from_user(&new_serial, new_info, sizeof(new_serial)))
831 return -EFAULT;
832 old_info = *info;
834 if (!capable(CAP_SYS_ADMIN)) {
835 if ((new_serial.baud_base != info->baud_base) ||
836 (new_serial.type != info->type) ||
837 (new_serial.close_delay != info->close_delay) ||
838 ((new_serial.flags & ~S_USR_MASK) !=
839 (info->flags & ~S_USR_MASK)))
840 return -EPERM;
841 info->flags = ((info->flags & ~S_USR_MASK) |
842 (new_serial.flags & S_USR_MASK));
843 info->custom_divisor = new_serial.custom_divisor;
844 goto check_and_exit;
847 if (info->count > 1)
848 return -EBUSY;
851 * OK, past this point, all the error checking has been done.
852 * At this point, we start making changes.....
855 info->baud_base = new_serial.baud_base;
856 info->flags = ((info->flags & ~S_FLAGS) |
857 (new_serial.flags & S_FLAGS));
858 info->type = new_serial.type;
859 info->close_delay = new_serial.close_delay;
860 info->closing_wait = new_serial.closing_wait;
862 check_and_exit:
863 retval = startup(info);
864 return retval;
868 * get_lsr_info - get line status register info
870 * Purpose: Let user call ioctl() to get info when the UART physically
871 * is emptied. On bus types like RS485, the transmitter must
872 * release the bus after transmitting. This must be done when
873 * the transmit shift register is empty, not be done when the
874 * transmit holding register is empty. This functionality
875 * allows an RS485 driver to be written in user space.
877 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
879 #ifdef CONFIG_SERIAL_68328_RTS_CTS
880 m68328_uart *uart = &uart_addr[info->line];
881 #endif
882 unsigned char status;
883 unsigned long flags;
885 local_irq_save(flags);
886 #ifdef CONFIG_SERIAL_68328_RTS_CTS
887 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
888 #else
889 status = 0;
890 #endif
891 local_irq_restore(flags);
892 return put_user(status, value);
896 * This routine sends a break character out the serial port.
898 static void send_break(struct m68k_serial * info, unsigned int duration)
900 m68328_uart *uart = &uart_addr[info->line];
901 unsigned long flags;
902 if (!info->port)
903 return;
904 local_irq_save(flags);
905 #ifdef USE_INTS
906 uart->utx.w |= UTX_SEND_BREAK;
907 msleep_interruptible(duration);
908 uart->utx.w &= ~UTX_SEND_BREAK;
909 #endif
910 local_irq_restore(flags);
913 static int rs_ioctl(struct tty_struct *tty,
914 unsigned int cmd, unsigned long arg)
916 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
917 int retval;
919 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
920 return -ENODEV;
922 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
923 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
924 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
925 if (tty->flags & (1 << TTY_IO_ERROR))
926 return -EIO;
929 switch (cmd) {
930 case TCSBRK: /* SVID version: non-zero arg --> no break */
931 retval = tty_check_change(tty);
932 if (retval)
933 return retval;
934 tty_wait_until_sent(tty, 0);
935 if (!arg)
936 send_break(info, 250); /* 1/4 second */
937 return 0;
938 case TCSBRKP: /* support for POSIX tcsendbreak() */
939 retval = tty_check_change(tty);
940 if (retval)
941 return retval;
942 tty_wait_until_sent(tty, 0);
943 send_break(info, arg ? arg*(100) : 250);
944 return 0;
945 case TIOCGSERIAL:
946 return get_serial_info(info,
947 (struct serial_struct *) arg);
948 case TIOCSSERIAL:
949 return set_serial_info(info,
950 (struct serial_struct *) arg);
951 case TIOCSERGETLSR: /* Get line status register */
952 return get_lsr_info(info, (unsigned int *) arg);
953 case TIOCSERGSTRUCT:
954 if (copy_to_user((struct m68k_serial *) arg,
955 info, sizeof(struct m68k_serial)))
956 return -EFAULT;
957 return 0;
958 default:
959 return -ENOIOCTLCMD;
961 return 0;
964 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
966 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
968 change_speed(info);
970 if ((old_termios->c_cflag & CRTSCTS) &&
971 !(tty->termios->c_cflag & CRTSCTS)) {
972 tty->hw_stopped = 0;
973 rs_start(tty);
979 * ------------------------------------------------------------
980 * rs_close()
982 * This routine is called when the serial port gets closed. First, we
983 * wait for the last remaining data to be sent. Then, we unlink its
984 * S structure from the interrupt chain if necessary, and we free
985 * that IRQ if nothing is left in the chain.
986 * ------------------------------------------------------------
988 static void rs_close(struct tty_struct *tty, struct file * filp)
990 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
991 m68328_uart *uart = &uart_addr[info->line];
992 unsigned long flags;
994 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
995 return;
997 local_irq_save(flags);
999 if (tty_hung_up_p(filp)) {
1000 local_irq_restore(flags);
1001 return;
1004 if ((tty->count == 1) && (info->count != 1)) {
1006 * Uh, oh. tty->count is 1, which means that the tty
1007 * structure will be freed. Info->count should always
1008 * be one in these conditions. If it's greater than
1009 * one, we've got real problems, since it means the
1010 * serial port won't be shutdown.
1012 printk("rs_close: bad serial port count; tty->count is 1, "
1013 "info->count is %d\n", info->count);
1014 info->count = 1;
1016 if (--info->count < 0) {
1017 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1018 info->line, info->count);
1019 info->count = 0;
1021 if (info->count) {
1022 local_irq_restore(flags);
1023 return;
1025 info->flags |= S_CLOSING;
1027 * Now we wait for the transmit buffer to clear; and we notify
1028 * the line discipline to only process XON/XOFF characters.
1030 tty->closing = 1;
1031 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1032 tty_wait_until_sent(tty, info->closing_wait);
1034 * At this point we stop accepting input. To do this, we
1035 * disable the receive line status interrupts, and tell the
1036 * interrupt driver to stop checking the data ready bit in the
1037 * line status register.
1040 uart->ustcnt &= ~USTCNT_RXEN;
1041 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1043 shutdown(info);
1044 rs_flush_buffer(tty);
1046 tty_ldisc_flush(tty);
1047 tty->closing = 0;
1048 info->event = 0;
1049 info->tty = NULL;
1050 #warning "This is not and has never been valid so fix it"
1051 #if 0
1052 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1053 if (tty->ldisc.close)
1054 (tty->ldisc.close)(tty);
1055 tty->ldisc = ldiscs[N_TTY];
1056 tty->termios->c_line = N_TTY;
1057 if (tty->ldisc.open)
1058 (tty->ldisc.open)(tty);
1060 #endif
1061 if (info->blocked_open) {
1062 if (info->close_delay) {
1063 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1065 wake_up_interruptible(&info->open_wait);
1067 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1068 wake_up_interruptible(&info->close_wait);
1069 local_irq_restore(flags);
1073 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1075 void rs_hangup(struct tty_struct *tty)
1077 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1079 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1080 return;
1082 rs_flush_buffer(tty);
1083 shutdown(info);
1084 info->event = 0;
1085 info->count = 0;
1086 info->flags &= ~S_NORMAL_ACTIVE;
1087 info->tty = NULL;
1088 wake_up_interruptible(&info->open_wait);
1092 * ------------------------------------------------------------
1093 * rs_open() and friends
1094 * ------------------------------------------------------------
1096 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1097 struct m68k_serial *info)
1099 DECLARE_WAITQUEUE(wait, current);
1100 int retval;
1101 int do_clocal = 0;
1104 * If the device is in the middle of being closed, then block
1105 * until it's done, and then try again.
1107 if (info->flags & S_CLOSING) {
1108 interruptible_sleep_on(&info->close_wait);
1109 #ifdef SERIAL_DO_RESTART
1110 if (info->flags & S_HUP_NOTIFY)
1111 return -EAGAIN;
1112 else
1113 return -ERESTARTSYS;
1114 #else
1115 return -EAGAIN;
1116 #endif
1120 * If non-blocking mode is set, or the port is not enabled,
1121 * then make the check up front and then exit.
1123 if ((filp->f_flags & O_NONBLOCK) ||
1124 (tty->flags & (1 << TTY_IO_ERROR))) {
1125 info->flags |= S_NORMAL_ACTIVE;
1126 return 0;
1129 if (tty->termios->c_cflag & CLOCAL)
1130 do_clocal = 1;
1133 * Block waiting for the carrier detect and the line to become
1134 * free (i.e., not in use by the callout). While we are in
1135 * this loop, info->count is dropped by one, so that
1136 * rs_close() knows when to free things. We restore it upon
1137 * exit, either normal or abnormal.
1139 retval = 0;
1140 add_wait_queue(&info->open_wait, &wait);
1142 info->count--;
1143 info->blocked_open++;
1144 while (1) {
1145 local_irq_disable();
1146 m68k_rtsdtr(info, 1);
1147 local_irq_enable();
1148 current->state = TASK_INTERRUPTIBLE;
1149 if (tty_hung_up_p(filp) ||
1150 !(info->flags & S_INITIALIZED)) {
1151 #ifdef SERIAL_DO_RESTART
1152 if (info->flags & S_HUP_NOTIFY)
1153 retval = -EAGAIN;
1154 else
1155 retval = -ERESTARTSYS;
1156 #else
1157 retval = -EAGAIN;
1158 #endif
1159 break;
1161 if (!(info->flags & S_CLOSING) && do_clocal)
1162 break;
1163 if (signal_pending(current)) {
1164 retval = -ERESTARTSYS;
1165 break;
1167 tty_unlock();
1168 schedule();
1169 tty_lock();
1171 current->state = TASK_RUNNING;
1172 remove_wait_queue(&info->open_wait, &wait);
1173 if (!tty_hung_up_p(filp))
1174 info->count++;
1175 info->blocked_open--;
1177 if (retval)
1178 return retval;
1179 info->flags |= S_NORMAL_ACTIVE;
1180 return 0;
1184 * This routine is called whenever a serial port is opened. It
1185 * enables interrupts for a serial port, linking in its S structure into
1186 * the IRQ chain. It also performs the serial-specific
1187 * initialization for the tty structure.
1189 int rs_open(struct tty_struct *tty, struct file * filp)
1191 struct m68k_serial *info;
1192 int retval;
1194 info = &m68k_soft[tty->index];
1196 if (serial_paranoia_check(info, tty->name, "rs_open"))
1197 return -ENODEV;
1199 info->count++;
1200 tty->driver_data = info;
1201 info->tty = tty;
1204 * Start up serial port
1206 retval = startup(info);
1207 if (retval)
1208 return retval;
1210 return block_til_ready(tty, filp, info);
1213 /* Finally, routines used to initialize the serial driver. */
1215 static void show_serial_version(void)
1217 printk("MC68328 serial driver version 1.00\n");
1220 static const struct tty_operations rs_ops = {
1221 .open = rs_open,
1222 .close = rs_close,
1223 .write = rs_write,
1224 .flush_chars = rs_flush_chars,
1225 .write_room = rs_write_room,
1226 .chars_in_buffer = rs_chars_in_buffer,
1227 .flush_buffer = rs_flush_buffer,
1228 .ioctl = rs_ioctl,
1229 .throttle = rs_throttle,
1230 .unthrottle = rs_unthrottle,
1231 .set_termios = rs_set_termios,
1232 .stop = rs_stop,
1233 .start = rs_start,
1234 .hangup = rs_hangup,
1235 .set_ldisc = rs_set_ldisc,
1238 /* rs_init inits the driver */
1239 static int __init
1240 rs68328_init(void)
1242 int flags, i;
1243 struct m68k_serial *info;
1245 serial_driver = alloc_tty_driver(NR_PORTS);
1246 if (!serial_driver)
1247 return -ENOMEM;
1249 show_serial_version();
1251 /* Initialize the tty_driver structure */
1252 /* SPARC: Not all of this is exactly right for us. */
1254 serial_driver->name = "ttyS";
1255 serial_driver->major = TTY_MAJOR;
1256 serial_driver->minor_start = 64;
1257 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1258 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1259 serial_driver->init_termios = tty_std_termios;
1260 serial_driver->init_termios.c_cflag =
1261 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1262 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1263 tty_set_operations(serial_driver, &rs_ops);
1265 if (tty_register_driver(serial_driver)) {
1266 put_tty_driver(serial_driver);
1267 printk(KERN_ERR "Couldn't register serial driver\n");
1268 return -ENOMEM;
1271 local_irq_save(flags);
1273 for(i=0;i<NR_PORTS;i++) {
1275 info = &m68k_soft[i];
1276 info->magic = SERIAL_MAGIC;
1277 info->port = (int) &uart_addr[i];
1278 info->tty = NULL;
1279 info->irq = uart_irqs[i];
1280 info->custom_divisor = 16;
1281 info->close_delay = 50;
1282 info->closing_wait = 3000;
1283 info->x_char = 0;
1284 info->event = 0;
1285 info->count = 0;
1286 info->blocked_open = 0;
1287 init_waitqueue_head(&info->open_wait);
1288 init_waitqueue_head(&info->close_wait);
1289 info->line = i;
1290 info->is_cons = 1; /* Means shortcuts work */
1292 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1293 info->port, info->irq);
1294 printk(" is a builtin MC68328 UART\n");
1296 #ifdef CONFIG_M68VZ328
1297 if (i > 0 )
1298 PJSEL &= 0xCF; /* PSW enable second port output */
1299 #endif
1301 if (request_irq(uart_irqs[i],
1302 rs_interrupt,
1304 "M68328_UART", info))
1305 panic("Unable to attach 68328 serial interrupt\n");
1307 local_irq_restore(flags);
1308 return 0;
1311 module_init(rs68328_init);
1315 static void m68328_set_baud(void)
1317 unsigned short ustcnt;
1318 int i;
1320 ustcnt = USTCNT;
1321 USTCNT = ustcnt & ~USTCNT_TXEN;
1323 again:
1324 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1325 if (baud_table[i] == m68328_console_baud)
1326 break;
1327 if (i >= ARRAY_SIZE(baud_table)) {
1328 m68328_console_baud = 9600;
1329 goto again;
1332 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1333 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1334 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1335 ustcnt |= USTCNT_8_7;
1336 ustcnt |= USTCNT_TXEN;
1337 USTCNT = ustcnt;
1338 m68328_console_initted = 1;
1339 return;
1343 int m68328_console_setup(struct console *cp, char *arg)
1345 int i, n = CONSOLE_BAUD_RATE;
1347 if (!cp)
1348 return(-1);
1350 if (arg)
1351 n = simple_strtoul(arg,NULL,0);
1353 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1354 if (baud_table[i] == n)
1355 break;
1356 if (i < ARRAY_SIZE(baud_table)) {
1357 m68328_console_baud = n;
1358 m68328_console_cbaud = 0;
1359 if (i > 15) {
1360 m68328_console_cbaud |= CBAUDEX;
1361 i -= 15;
1363 m68328_console_cbaud |= i;
1366 m68328_set_baud(); /* make sure baud rate changes */
1367 return(0);
1371 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1373 *index = c->index;
1374 return serial_driver;
1378 void m68328_console_write (struct console *co, const char *str,
1379 unsigned int count)
1381 if (!m68328_console_initted)
1382 m68328_set_baud();
1383 while (count--) {
1384 if (*str == '\n')
1385 rs_put_char('\r');
1386 rs_put_char( *str++ );
1391 static struct console m68328_driver = {
1392 .name = "ttyS",
1393 .write = m68328_console_write,
1394 .device = m68328_console_device,
1395 .setup = m68328_console_setup,
1396 .flags = CON_PRINTBUFFER,
1397 .index = -1,
1401 static int __init m68328_console_init(void)
1403 register_console(&m68328_driver);
1404 return 0;
1407 console_initcall(m68328_console_init);