staging: ti dspbridge: remove contributor leftovers
[linux-2.6/btrfs-unstable.git] / drivers / serial / 68328serial.c
blob30463862603b18e819ddcd69ead819dee18f0d44
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/system.h>
43 #include <asm/delay.h>
44 #include <asm/uaccess.h>
46 /* (es) */
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>
52 #else
53 #if defined(CONFIG_M68VZ328)
54 #include <asm/MC68VZ328.h>
55 #else
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
64 #undef USE_INTS
65 #else
66 #define USE_INTS
67 #endif
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 #ifdef CONFIG_CONSOLE
82 extern wait_queue_head_t keypress_wait;
83 #endif
85 struct tty_driver *serial_driver;
87 /* number of characters left in xmit buffer before we ask for more */
88 #define WAKEUP_CHARS 256
90 /* Debugging... DEBUG_INTR is bad to use when one of the zs
91 * lines is your console ;(
93 #undef SERIAL_DEBUG_INTR
94 #undef SERIAL_DEBUG_OPEN
95 #undef SERIAL_DEBUG_FLOW
97 #define RS_ISR_PASS_LIMIT 256
99 static void change_speed(struct m68k_serial *info);
102 * Setup for console. Argument comes from the boot command line.
105 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
106 #define CONSOLE_BAUD_RATE 115200
107 #define DEFAULT_CBAUD B115200
108 #else
109 /* (es) */
110 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
111 #ifdef CONFIG_M68VZ328
112 #define CONSOLE_BAUD_RATE 19200
113 #define DEFAULT_CBAUD B19200
114 #endif
115 /* (/es) */
116 #endif
118 #ifndef CONSOLE_BAUD_RATE
119 #define CONSOLE_BAUD_RATE 9600
120 #define DEFAULT_CBAUD B9600
121 #endif
124 static int m68328_console_initted = 0;
125 static int m68328_console_baud = CONSOLE_BAUD_RATE;
126 static int m68328_console_cbaud = DEFAULT_CBAUD;
129 static inline int serial_paranoia_check(struct m68k_serial *info,
130 char *name, const char *routine)
132 #ifdef SERIAL_PARANOIA_CHECK
133 static const char *badmagic =
134 "Warning: bad magic number for serial struct %s in %s\n";
135 static const char *badinfo =
136 "Warning: null m68k_serial for %s in %s\n";
138 if (!info) {
139 printk(badinfo, name, routine);
140 return 1;
142 if (info->magic != SERIAL_MAGIC) {
143 printk(badmagic, name, routine);
144 return 1;
146 #endif
147 return 0;
151 * This is used to figure out the divisor speeds and the timeouts
153 static int baud_table[] = {
154 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
155 9600, 19200, 38400, 57600, 115200, 0 };
157 /* Sets or clears DTR/RTS on the requested line */
158 static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
160 if (set) {
161 /* set the RTS/CTS line */
162 } else {
163 /* clear it */
165 return;
168 /* Utility routines */
169 static inline int get_baud(struct m68k_serial *ss)
171 unsigned long result = 115200;
172 unsigned short int baud = uart_addr[ss->line].ubaud;
173 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
174 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
176 return result;
180 * ------------------------------------------------------------
181 * rs_stop() and rs_start()
183 * This routines are called before setting or resetting tty->stopped.
184 * They enable or disable transmitter interrupts, as necessary.
185 * ------------------------------------------------------------
187 static void rs_stop(struct tty_struct *tty)
189 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
190 m68328_uart *uart = &uart_addr[info->line];
191 unsigned long flags;
193 if (serial_paranoia_check(info, tty->name, "rs_stop"))
194 return;
196 local_irq_save(flags);
197 uart->ustcnt &= ~USTCNT_TXEN;
198 local_irq_restore(flags);
201 static int rs_put_char(char ch)
203 int flags, loops = 0;
205 local_irq_save(flags);
207 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
208 loops++;
209 udelay(5);
212 UTX_TXDATA = ch;
213 udelay(5);
214 local_irq_restore(flags);
215 return 1;
218 static void rs_start(struct tty_struct *tty)
220 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
221 m68328_uart *uart = &uart_addr[info->line];
222 unsigned long flags;
224 if (serial_paranoia_check(info, tty->name, "rs_start"))
225 return;
227 local_irq_save(flags);
228 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
229 #ifdef USE_INTS
230 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
231 #else
232 uart->ustcnt |= USTCNT_TXEN;
233 #endif
235 local_irq_restore(flags);
238 /* Drop into either the boot monitor or kadb upon receiving a break
239 * from keyboard/console input.
241 static void batten_down_hatches(void)
243 /* Drop into the debugger */
246 static void status_handle(struct m68k_serial *info, unsigned short status)
248 #if 0
249 if(status & DCD) {
250 if((info->port.tty->termios->c_cflag & CRTSCTS) &&
251 ((info->curregs[3] & AUTO_ENAB)==0)) {
252 info->curregs[3] |= AUTO_ENAB;
253 info->pendregs[3] |= AUTO_ENAB;
254 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
256 } else {
257 if((info->curregs[3] & AUTO_ENAB)) {
258 info->curregs[3] &= ~AUTO_ENAB;
259 info->pendregs[3] &= ~AUTO_ENAB;
260 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
263 #endif
264 /* If this is console input and this is a
265 * 'break asserted' status change interrupt
266 * see if we can drop into the debugger
268 if((status & URX_BREAK) && info->break_abort)
269 batten_down_hatches();
270 return;
273 static void receive_chars(struct m68k_serial *info, unsigned short rx)
275 struct tty_struct *tty = info->port.tty;
276 m68328_uart *uart = &uart_addr[info->line];
277 unsigned char ch, flag;
280 * This do { } while() loop will get ALL chars out of Rx FIFO
282 #ifndef CONFIG_XCOPILOT_BUGS
283 do {
284 #endif
285 ch = GET_FIELD(rx, URX_RXDATA);
287 if(info->is_cons) {
288 if(URX_BREAK & rx) { /* whee, break received */
289 status_handle(info, rx);
290 return;
291 #ifdef CONFIG_MAGIC_SYSRQ
292 } else if (ch == 0x10) { /* ^P */
293 show_state();
294 show_free_areas();
295 show_buffers();
296 /* show_net_buffers(); */
297 return;
298 } else if (ch == 0x12) { /* ^R */
299 emergency_restart();
300 return;
301 #endif /* CONFIG_MAGIC_SYSRQ */
303 /* It is a 'keyboard interrupt' ;-) */
304 #ifdef CONFIG_CONSOLE
305 wake_up(&keypress_wait);
306 #endif
309 if(!tty)
310 goto clear_and_exit;
312 flag = TTY_NORMAL;
314 if(rx & URX_PARITY_ERROR) {
315 flag = TTY_PARITY;
316 status_handle(info, rx);
317 } else if(rx & URX_OVRUN) {
318 flag = TTY_OVERRUN;
319 status_handle(info, rx);
320 } else if(rx & URX_FRAME_ERROR) {
321 flag = TTY_FRAME;
322 status_handle(info, rx);
324 tty_insert_flip_char(tty, ch, flag);
325 #ifndef CONFIG_XCOPILOT_BUGS
326 } while((rx = uart->urx.w) & URX_DATA_READY);
327 #endif
329 tty_schedule_flip(tty);
331 clear_and_exit:
332 return;
335 static void transmit_chars(struct m68k_serial *info)
337 m68328_uart *uart = &uart_addr[info->line];
339 if (info->x_char) {
340 /* Send next char */
341 uart->utx.b.txdata = info->x_char;
342 info->x_char = 0;
343 goto clear_and_return;
346 if((info->xmit_cnt <= 0) || info->port.tty->stopped) {
347 /* That's peculiar... TX ints off */
348 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
349 goto clear_and_return;
352 /* Send char */
353 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
354 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
355 info->xmit_cnt--;
357 if (info->xmit_cnt < WAKEUP_CHARS)
358 schedule_work(&info->tqueue);
360 if(info->xmit_cnt <= 0) {
361 /* All done for now... TX ints off */
362 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
363 goto clear_and_return;
366 clear_and_return:
367 /* Clear interrupt (should be auto)*/
368 return;
372 * This is the serial driver's generic interrupt routine
374 irqreturn_t rs_interrupt(int irq, void *dev_id)
376 struct m68k_serial *info = dev_id;
377 m68328_uart *uart;
378 unsigned short rx;
379 unsigned short tx;
381 uart = &uart_addr[info->line];
382 rx = uart->urx.w;
384 #ifdef USE_INTS
385 tx = uart->utx.w;
387 if (rx & URX_DATA_READY) receive_chars(info, rx);
388 if (tx & UTX_TX_AVAIL) transmit_chars(info);
389 #else
390 receive_chars(info, rx);
391 #endif
392 return IRQ_HANDLED;
395 static void do_softint(struct work_struct *work)
397 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
398 struct tty_struct *tty;
400 tty = info->port.tty;
401 if (!tty)
402 return;
403 #if 0
404 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
405 tty_wakeup(tty);
407 #endif
411 * This routine is called from the scheduler tqueue when the interrupt
412 * routine has signalled that a hangup has occurred. The path of
413 * hangup processing is:
415 * serial interrupt routine -> (scheduler tqueue) ->
416 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
419 static void do_serial_hangup(struct work_struct *work)
421 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
422 struct tty_struct *tty;
424 tty = info->port.tty;
425 if (!tty)
426 return;
428 tty_hangup(tty);
432 static int startup(struct m68k_serial * info)
434 m68328_uart *uart = &uart_addr[info->line];
435 unsigned long flags;
437 if (info->flags & S_INITIALIZED)
438 return 0;
440 if (!info->xmit_buf) {
441 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
442 if (!info->xmit_buf)
443 return -ENOMEM;
446 local_irq_save(flags);
449 * Clear the FIFO buffers and disable them
450 * (they will be reenabled in change_speed())
453 uart->ustcnt = USTCNT_UEN;
454 info->xmit_fifo_size = 1;
455 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
456 (void)uart->urx.w;
459 * Finally, enable sequencing and interrupts
461 #ifdef USE_INTS
462 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
463 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
464 #else
465 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
466 #endif
468 if (info->port.tty)
469 clear_bit(TTY_IO_ERROR, &info->port.tty->flags);
470 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
473 * and set the speed of the serial port
476 change_speed(info);
478 info->flags |= S_INITIALIZED;
479 local_irq_restore(flags);
480 return 0;
484 * This routine will shutdown a serial port; interrupts are disabled, and
485 * DTR is dropped if the hangup on close termio flag is on.
487 static void shutdown(struct m68k_serial * info)
489 m68328_uart *uart = &uart_addr[info->line];
490 unsigned long flags;
492 uart->ustcnt = 0; /* All off! */
493 if (!(info->flags & S_INITIALIZED))
494 return;
496 local_irq_save(flags);
498 if (info->xmit_buf) {
499 free_page((unsigned long) info->xmit_buf);
500 info->xmit_buf = 0;
503 if (info->port.tty)
504 set_bit(TTY_IO_ERROR, &info->port.tty->flags);
506 info->flags &= ~S_INITIALIZED;
507 local_irq_restore(flags);
510 struct {
511 int divisor, prescale;
513 #ifndef CONFIG_M68VZ328
514 hw_baud_table[18] = {
515 {0,0}, /* 0 */
516 {0,0}, /* 50 */
517 {0,0}, /* 75 */
518 {0,0}, /* 110 */
519 {0,0}, /* 134 */
520 {0,0}, /* 150 */
521 {0,0}, /* 200 */
522 {7,0x26}, /* 300 */
523 {6,0x26}, /* 600 */
524 {5,0x26}, /* 1200 */
525 {0,0}, /* 1800 */
526 {4,0x26}, /* 2400 */
527 {3,0x26}, /* 4800 */
528 {2,0x26}, /* 9600 */
529 {1,0x26}, /* 19200 */
530 {0,0x26}, /* 38400 */
531 {1,0x38}, /* 57600 */
532 {0,0x38}, /* 115200 */
534 #else
535 hw_baud_table[18] = {
536 {0,0}, /* 0 */
537 {0,0}, /* 50 */
538 {0,0}, /* 75 */
539 {0,0}, /* 110 */
540 {0,0}, /* 134 */
541 {0,0}, /* 150 */
542 {0,0}, /* 200 */
543 {0,0}, /* 300 */
544 {7,0x26}, /* 600 */
545 {6,0x26}, /* 1200 */
546 {0,0}, /* 1800 */
547 {5,0x26}, /* 2400 */
548 {4,0x26}, /* 4800 */
549 {3,0x26}, /* 9600 */
550 {2,0x26}, /* 19200 */
551 {1,0x26}, /* 38400 */
552 {0,0x26}, /* 57600 */
553 {1,0x38}, /* 115200 */
555 #endif
556 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
559 * This routine is called to set the UART divisor registers to match
560 * the specified baud rate for a serial port.
562 static void change_speed(struct m68k_serial *info)
564 m68328_uart *uart = &uart_addr[info->line];
565 unsigned short port;
566 unsigned short ustcnt;
567 unsigned cflag;
568 int i;
570 if (!info->port.tty || !info->port.tty->termios)
571 return;
572 cflag = info->port.tty->termios->c_cflag;
573 if (!(port = info->port))
574 return;
576 ustcnt = uart->ustcnt;
577 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
579 i = cflag & CBAUD;
580 if (i & CBAUDEX) {
581 i = (i & ~CBAUDEX) + B38400;
584 info->baud = baud_table[i];
585 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
586 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
588 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
590 if ((cflag & CSIZE) == CS8)
591 ustcnt |= USTCNT_8_7;
593 if (cflag & CSTOPB)
594 ustcnt |= USTCNT_STOP;
596 if (cflag & PARENB)
597 ustcnt |= USTCNT_PARITYEN;
598 if (cflag & PARODD)
599 ustcnt |= USTCNT_ODD_EVEN;
601 #ifdef CONFIG_SERIAL_68328_RTS_CTS
602 if (cflag & CRTSCTS) {
603 uart->utx.w &= ~ UTX_NOCTS;
604 } else {
605 uart->utx.w |= UTX_NOCTS;
607 #endif
609 ustcnt |= USTCNT_TXEN;
611 uart->ustcnt = ustcnt;
612 return;
616 * Fair output driver allows a process to speak.
618 static void rs_fair_output(void)
620 int left; /* Output no more than that */
621 unsigned long flags;
622 struct m68k_serial *info = &m68k_soft[0];
623 char c;
625 if (info == 0) return;
626 if (info->xmit_buf == 0) return;
628 local_irq_save(flags);
629 left = info->xmit_cnt;
630 while (left != 0) {
631 c = info->xmit_buf[info->xmit_tail];
632 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
633 info->xmit_cnt--;
634 local_irq_restore(flags);
636 rs_put_char(c);
638 local_irq_save(flags);
639 left = min(info->xmit_cnt, left-1);
642 /* Last character is being transmitted now (hopefully). */
643 udelay(5);
645 local_irq_restore(flags);
646 return;
650 * m68k_console_print is registered for printk.
652 void console_print_68328(const char *p)
654 char c;
656 while((c=*(p++)) != 0) {
657 if(c == '\n')
658 rs_put_char('\r');
659 rs_put_char(c);
662 /* Comment this if you want to have a strict interrupt-driven output */
663 rs_fair_output();
665 return;
668 static void rs_set_ldisc(struct tty_struct *tty)
670 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
672 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
673 return;
675 info->is_cons = (tty->termios->c_line == N_TTY);
677 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
680 static void rs_flush_chars(struct tty_struct *tty)
682 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
683 m68328_uart *uart = &uart_addr[info->line];
684 unsigned long flags;
686 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
687 return;
688 #ifndef USE_INTS
689 for(;;) {
690 #endif
692 /* Enable transmitter */
693 local_irq_save(flags);
695 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
696 !info->xmit_buf) {
697 local_irq_restore(flags);
698 return;
701 #ifdef USE_INTS
702 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
703 #else
704 uart->ustcnt |= USTCNT_TXEN;
705 #endif
707 #ifdef USE_INTS
708 if (uart->utx.w & UTX_TX_AVAIL) {
709 #else
710 if (1) {
711 #endif
712 /* Send char */
713 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
714 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
715 info->xmit_cnt--;
718 #ifndef USE_INTS
719 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
721 #endif
722 local_irq_restore(flags);
725 extern void console_printn(const char * b, int count);
727 static int rs_write(struct tty_struct * tty,
728 const unsigned char *buf, int count)
730 int c, total = 0;
731 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
732 m68328_uart *uart = &uart_addr[info->line];
733 unsigned long flags;
735 if (serial_paranoia_check(info, tty->name, "rs_write"))
736 return 0;
738 if (!tty || !info->xmit_buf)
739 return 0;
741 local_save_flags(flags);
742 while (1) {
743 local_irq_disable();
744 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
745 SERIAL_XMIT_SIZE - info->xmit_head));
746 local_irq_restore(flags);
748 if (c <= 0)
749 break;
751 memcpy(info->xmit_buf + info->xmit_head, buf, c);
753 local_irq_disable();
754 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
755 info->xmit_cnt += c;
756 local_irq_restore(flags);
757 buf += c;
758 count -= c;
759 total += c;
762 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
763 /* Enable transmitter */
764 local_irq_disable();
765 #ifndef USE_INTS
766 while(info->xmit_cnt) {
767 #endif
769 uart->ustcnt |= USTCNT_TXEN;
770 #ifdef USE_INTS
771 uart->ustcnt |= USTCNT_TX_INTR_MASK;
772 #else
773 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
774 #endif
775 if (uart->utx.w & UTX_TX_AVAIL) {
776 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
777 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
778 info->xmit_cnt--;
781 #ifndef USE_INTS
783 #endif
784 local_irq_restore(flags);
787 return total;
790 static int rs_write_room(struct tty_struct *tty)
792 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
793 int ret;
795 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
796 return 0;
797 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
798 if (ret < 0)
799 ret = 0;
800 return ret;
803 static int rs_chars_in_buffer(struct tty_struct *tty)
805 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
807 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
808 return 0;
809 return info->xmit_cnt;
812 static void rs_flush_buffer(struct tty_struct *tty)
814 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
815 unsigned long flags;
817 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
818 return;
819 local_irq_save(flags);
820 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
821 local_irq_restore(flags);
822 tty_wakeup(tty);
826 * ------------------------------------------------------------
827 * rs_throttle()
829 * This routine is called by the upper-layer tty layer to signal that
830 * incoming characters should be throttled.
831 * ------------------------------------------------------------
833 static void rs_throttle(struct tty_struct * tty)
835 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
837 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
838 return;
840 if (I_IXOFF(tty))
841 info->x_char = STOP_CHAR(tty);
843 /* Turn off RTS line (do this atomic) */
846 static void rs_unthrottle(struct tty_struct * tty)
848 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
850 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
851 return;
853 if (I_IXOFF(tty)) {
854 if (info->x_char)
855 info->x_char = 0;
856 else
857 info->x_char = START_CHAR(tty);
860 /* Assert RTS line (do this atomic) */
864 * ------------------------------------------------------------
865 * rs_ioctl() and friends
866 * ------------------------------------------------------------
869 static int get_serial_info(struct m68k_serial * info,
870 struct serial_struct * retinfo)
872 struct serial_struct tmp;
874 if (!retinfo)
875 return -EFAULT;
876 memset(&tmp, 0, sizeof(tmp));
877 tmp.type = info->type;
878 tmp.line = info->line;
879 tmp.port = info->port;
880 tmp.irq = info->irq;
881 tmp.flags = info->flags;
882 tmp.baud_base = info->baud_base;
883 tmp.close_delay = info->close_delay;
884 tmp.closing_wait = info->closing_wait;
885 tmp.custom_divisor = info->custom_divisor;
886 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
887 return 0;
890 static int set_serial_info(struct m68k_serial * info,
891 struct serial_struct * new_info)
893 struct serial_struct new_serial;
894 struct m68k_serial old_info;
895 int retval = 0;
897 if (!new_info)
898 return -EFAULT;
899 copy_from_user(&new_serial,new_info,sizeof(new_serial));
900 old_info = *info;
902 if (!capable(CAP_SYS_ADMIN)) {
903 if ((new_serial.baud_base != info->baud_base) ||
904 (new_serial.type != info->type) ||
905 (new_serial.close_delay != info->close_delay) ||
906 ((new_serial.flags & ~S_USR_MASK) !=
907 (info->flags & ~S_USR_MASK)))
908 return -EPERM;
909 info->flags = ((info->flags & ~S_USR_MASK) |
910 (new_serial.flags & S_USR_MASK));
911 info->custom_divisor = new_serial.custom_divisor;
912 goto check_and_exit;
915 if (info->count > 1)
916 return -EBUSY;
919 * OK, past this point, all the error checking has been done.
920 * At this point, we start making changes.....
923 info->baud_base = new_serial.baud_base;
924 info->flags = ((info->flags & ~S_FLAGS) |
925 (new_serial.flags & S_FLAGS));
926 info->type = new_serial.type;
927 info->close_delay = new_serial.close_delay;
928 info->closing_wait = new_serial.closing_wait;
930 check_and_exit:
931 retval = startup(info);
932 return retval;
936 * get_lsr_info - get line status register info
938 * Purpose: Let user call ioctl() to get info when the UART physically
939 * is emptied. On bus types like RS485, the transmitter must
940 * release the bus after transmitting. This must be done when
941 * the transmit shift register is empty, not be done when the
942 * transmit holding register is empty. This functionality
943 * allows an RS485 driver to be written in user space.
945 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
947 #ifdef CONFIG_SERIAL_68328_RTS_CTS
948 m68328_uart *uart = &uart_addr[info->line];
949 #endif
950 unsigned char status;
951 unsigned long flags;
953 local_irq_save(flags);
954 #ifdef CONFIG_SERIAL_68328_RTS_CTS
955 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
956 #else
957 status = 0;
958 #endif
959 local_irq_restore(flags);
960 put_user(status,value);
961 return 0;
965 * This routine sends a break character out the serial port.
967 static void send_break(struct m68k_serial * info, unsigned int duration)
969 m68328_uart *uart = &uart_addr[info->line];
970 unsigned long flags;
971 if (!info->port)
972 return;
973 local_irq_save(flags);
974 #ifdef USE_INTS
975 uart->utx.w |= UTX_SEND_BREAK;
976 msleep_interruptible(duration);
977 uart->utx.w &= ~UTX_SEND_BREAK;
978 #endif
979 local_irq_restore(flags);
982 static int rs_ioctl(struct tty_struct *tty, struct file * file,
983 unsigned int cmd, unsigned long arg)
985 int error;
986 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
987 int retval;
989 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
990 return -ENODEV;
992 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
993 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
994 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
995 if (tty->flags & (1 << TTY_IO_ERROR))
996 return -EIO;
999 switch (cmd) {
1000 case TCSBRK: /* SVID version: non-zero arg --> no break */
1001 retval = tty_check_change(tty);
1002 if (retval)
1003 return retval;
1004 tty_wait_until_sent(tty, 0);
1005 if (!arg)
1006 send_break(info, 250); /* 1/4 second */
1007 return 0;
1008 case TCSBRKP: /* support for POSIX tcsendbreak() */
1009 retval = tty_check_change(tty);
1010 if (retval)
1011 return retval;
1012 tty_wait_until_sent(tty, 0);
1013 send_break(info, arg ? arg*(100) : 250);
1014 return 0;
1015 case TIOCGSERIAL:
1016 if (access_ok(VERIFY_WRITE, (void *) arg,
1017 sizeof(struct serial_struct)))
1018 return get_serial_info(info,
1019 (struct serial_struct *) arg);
1020 return -EFAULT;
1021 case TIOCSSERIAL:
1022 return set_serial_info(info,
1023 (struct serial_struct *) arg);
1024 case TIOCSERGETLSR: /* Get line status register */
1025 if (access_ok(VERIFY_WRITE, (void *) arg,
1026 sizeof(unsigned int)))
1027 return get_lsr_info(info, (unsigned int *) arg);
1028 return -EFAULT;
1029 case TIOCSERGSTRUCT:
1030 if (!access_ok(VERIFY_WRITE, (void *) arg,
1031 sizeof(struct m68k_serial)))
1032 return -EFAULT;
1033 copy_to_user((struct m68k_serial *) arg,
1034 info, sizeof(struct m68k_serial));
1035 return 0;
1037 default:
1038 return -ENOIOCTLCMD;
1040 return 0;
1043 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1045 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1047 change_speed(info);
1049 if ((old_termios->c_cflag & CRTSCTS) &&
1050 !(tty->termios->c_cflag & CRTSCTS)) {
1051 tty->hw_stopped = 0;
1052 rs_start(tty);
1058 * ------------------------------------------------------------
1059 * rs_close()
1061 * This routine is called when the serial port gets closed. First, we
1062 * wait for the last remaining data to be sent. Then, we unlink its
1063 * S structure from the interrupt chain if necessary, and we free
1064 * that IRQ if nothing is left in the chain.
1065 * ------------------------------------------------------------
1067 static void rs_close(struct tty_struct *tty, struct file * filp)
1069 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1070 m68328_uart *uart = &uart_addr[info->line];
1071 unsigned long flags;
1073 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1074 return;
1076 local_irq_save(flags);
1078 if (tty_hung_up_p(filp)) {
1079 local_irq_restore(flags);
1080 return;
1083 if ((tty->count == 1) && (info->count != 1)) {
1085 * Uh, oh. tty->count is 1, which means that the tty
1086 * structure will be freed. Info->count should always
1087 * be one in these conditions. If it's greater than
1088 * one, we've got real problems, since it means the
1089 * serial port won't be shutdown.
1091 printk("rs_close: bad serial port count; tty->count is 1, "
1092 "info->count is %d\n", info->count);
1093 info->count = 1;
1095 if (--info->count < 0) {
1096 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1097 info->line, info->count);
1098 info->count = 0;
1100 if (info->count) {
1101 local_irq_restore(flags);
1102 return;
1104 info->flags |= S_CLOSING;
1106 * Now we wait for the transmit buffer to clear; and we notify
1107 * the line discipline to only process XON/XOFF characters.
1109 tty->closing = 1;
1110 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1111 tty_wait_until_sent(tty, info->closing_wait);
1113 * At this point we stop accepting input. To do this, we
1114 * disable the receive line status interrupts, and tell the
1115 * interrupt driver to stop checking the data ready bit in the
1116 * line status register.
1119 uart->ustcnt &= ~USTCNT_RXEN;
1120 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1122 shutdown(info);
1123 rs_flush_buffer(tty);
1125 tty_ldisc_flush(tty);
1126 tty->closing = 0;
1127 info->event = 0;
1128 info->port.tty = NULL;
1129 #warning "This is not and has never been valid so fix it"
1130 #if 0
1131 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1132 if (tty->ldisc.close)
1133 (tty->ldisc.close)(tty);
1134 tty->ldisc = ldiscs[N_TTY];
1135 tty->termios->c_line = N_TTY;
1136 if (tty->ldisc.open)
1137 (tty->ldisc.open)(tty);
1139 #endif
1140 if (info->blocked_open) {
1141 if (info->close_delay) {
1142 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1144 wake_up_interruptible(&info->open_wait);
1146 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1147 wake_up_interruptible(&info->close_wait);
1148 local_irq_restore(flags);
1152 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1154 void rs_hangup(struct tty_struct *tty)
1156 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1158 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1159 return;
1161 rs_flush_buffer(tty);
1162 shutdown(info);
1163 info->event = 0;
1164 info->count = 0;
1165 info->flags &= ~S_NORMAL_ACTIVE;
1166 info->port.tty = NULL;
1167 wake_up_interruptible(&info->open_wait);
1171 * ------------------------------------------------------------
1172 * rs_open() and friends
1173 * ------------------------------------------------------------
1175 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1176 struct m68k_serial *info)
1178 DECLARE_WAITQUEUE(wait, current);
1179 int retval;
1180 int do_clocal = 0;
1183 * If the device is in the middle of being closed, then block
1184 * until it's done, and then try again.
1186 if (info->flags & S_CLOSING) {
1187 interruptible_sleep_on(&info->close_wait);
1188 #ifdef SERIAL_DO_RESTART
1189 if (info->flags & S_HUP_NOTIFY)
1190 return -EAGAIN;
1191 else
1192 return -ERESTARTSYS;
1193 #else
1194 return -EAGAIN;
1195 #endif
1199 * If non-blocking mode is set, or the port is not enabled,
1200 * then make the check up front and then exit.
1202 if ((filp->f_flags & O_NONBLOCK) ||
1203 (tty->flags & (1 << TTY_IO_ERROR))) {
1204 info->flags |= S_NORMAL_ACTIVE;
1205 return 0;
1208 if (tty->termios->c_cflag & CLOCAL)
1209 do_clocal = 1;
1212 * Block waiting for the carrier detect and the line to become
1213 * free (i.e., not in use by the callout). While we are in
1214 * this loop, info->count is dropped by one, so that
1215 * rs_close() knows when to free things. We restore it upon
1216 * exit, either normal or abnormal.
1218 retval = 0;
1219 add_wait_queue(&info->open_wait, &wait);
1221 info->count--;
1222 info->blocked_open++;
1223 while (1) {
1224 local_irq_disable();
1225 m68k_rtsdtr(info, 1);
1226 local_irq_enable();
1227 current->state = TASK_INTERRUPTIBLE;
1228 if (tty_hung_up_p(filp) ||
1229 !(info->flags & S_INITIALIZED)) {
1230 #ifdef SERIAL_DO_RESTART
1231 if (info->flags & S_HUP_NOTIFY)
1232 retval = -EAGAIN;
1233 else
1234 retval = -ERESTARTSYS;
1235 #else
1236 retval = -EAGAIN;
1237 #endif
1238 break;
1240 if (!(info->flags & S_CLOSING) && do_clocal)
1241 break;
1242 if (signal_pending(current)) {
1243 retval = -ERESTARTSYS;
1244 break;
1246 schedule();
1248 current->state = TASK_RUNNING;
1249 remove_wait_queue(&info->open_wait, &wait);
1250 if (!tty_hung_up_p(filp))
1251 info->count++;
1252 info->blocked_open--;
1254 if (retval)
1255 return retval;
1256 info->flags |= S_NORMAL_ACTIVE;
1257 return 0;
1261 * This routine is called whenever a serial port is opened. It
1262 * enables interrupts for a serial port, linking in its S structure into
1263 * the IRQ chain. It also performs the serial-specific
1264 * initialization for the tty structure.
1266 int rs_open(struct tty_struct *tty, struct file * filp)
1268 struct m68k_serial *info;
1269 int retval, line;
1271 line = tty->index;
1273 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1274 return -ENODEV;
1276 info = &m68k_soft[line];
1278 if (serial_paranoia_check(info, tty->name, "rs_open"))
1279 return -ENODEV;
1281 info->count++;
1282 tty->driver_data = info;
1283 info->port.tty = tty;
1286 * Start up serial port
1288 retval = startup(info);
1289 if (retval)
1290 return retval;
1292 return block_til_ready(tty, filp, info);
1295 /* Finally, routines used to initialize the serial driver. */
1297 static void show_serial_version(void)
1299 printk("MC68328 serial driver version 1.00\n");
1302 static const struct tty_operations rs_ops = {
1303 .open = rs_open,
1304 .close = rs_close,
1305 .write = rs_write,
1306 .flush_chars = rs_flush_chars,
1307 .write_room = rs_write_room,
1308 .chars_in_buffer = rs_chars_in_buffer,
1309 .flush_buffer = rs_flush_buffer,
1310 .ioctl = rs_ioctl,
1311 .throttle = rs_throttle,
1312 .unthrottle = rs_unthrottle,
1313 .set_termios = rs_set_termios,
1314 .stop = rs_stop,
1315 .start = rs_start,
1316 .hangup = rs_hangup,
1317 .set_ldisc = rs_set_ldisc,
1320 /* rs_init inits the driver */
1321 static int __init
1322 rs68328_init(void)
1324 int flags, i;
1325 struct m68k_serial *info;
1327 serial_driver = alloc_tty_driver(NR_PORTS);
1328 if (!serial_driver)
1329 return -ENOMEM;
1331 show_serial_version();
1333 /* Initialize the tty_driver structure */
1334 /* SPARC: Not all of this is exactly right for us. */
1336 serial_driver->name = "ttyS";
1337 serial_driver->major = TTY_MAJOR;
1338 serial_driver->minor_start = 64;
1339 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1340 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1341 serial_driver->init_termios = tty_std_termios;
1342 serial_driver->init_termios.c_cflag =
1343 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1344 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1345 tty_set_operations(serial_driver, &rs_ops);
1347 if (tty_register_driver(serial_driver)) {
1348 put_tty_driver(serial_driver);
1349 printk(KERN_ERR "Couldn't register serial driver\n");
1350 return -ENOMEM;
1353 local_irq_save(flags);
1355 for(i=0;i<NR_PORTS;i++) {
1357 info = &m68k_soft[i];
1358 info->magic = SERIAL_MAGIC;
1359 info->port = (int) &uart_addr[i];
1360 info->port.tty = NULL;
1361 info->irq = uart_irqs[i];
1362 info->custom_divisor = 16;
1363 info->close_delay = 50;
1364 info->closing_wait = 3000;
1365 info->x_char = 0;
1366 info->event = 0;
1367 info->count = 0;
1368 info->blocked_open = 0;
1369 INIT_WORK(&info->tqueue, do_softint);
1370 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1371 init_waitqueue_head(&info->open_wait);
1372 init_waitqueue_head(&info->close_wait);
1373 info->line = i;
1374 info->is_cons = 1; /* Means shortcuts work */
1376 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1377 info->port, info->irq);
1378 printk(" is a builtin MC68328 UART\n");
1380 #ifdef CONFIG_M68VZ328
1381 if (i > 0 )
1382 PJSEL &= 0xCF; /* PSW enable second port output */
1383 #endif
1385 if (request_irq(uart_irqs[i],
1386 rs_interrupt,
1387 IRQF_DISABLED,
1388 "M68328_UART", info))
1389 panic("Unable to attach 68328 serial interrupt\n");
1391 local_irq_restore(flags);
1392 return 0;
1395 module_init(rs68328_init);
1399 static void m68328_set_baud(void)
1401 unsigned short ustcnt;
1402 int i;
1404 ustcnt = USTCNT;
1405 USTCNT = ustcnt & ~USTCNT_TXEN;
1407 again:
1408 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1409 if (baud_table[i] == m68328_console_baud)
1410 break;
1411 if (i >= ARRAY_SIZE(baud_table)) {
1412 m68328_console_baud = 9600;
1413 goto again;
1416 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1417 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1418 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1419 ustcnt |= USTCNT_8_7;
1420 ustcnt |= USTCNT_TXEN;
1421 USTCNT = ustcnt;
1422 m68328_console_initted = 1;
1423 return;
1427 int m68328_console_setup(struct console *cp, char *arg)
1429 int i, n = CONSOLE_BAUD_RATE;
1431 if (!cp)
1432 return(-1);
1434 if (arg)
1435 n = simple_strtoul(arg,NULL,0);
1437 for (i = 0; i < ARRAY_SIZE(baud_table); i++)
1438 if (baud_table[i] == n)
1439 break;
1440 if (i < ARRAY_SIZE(baud_table)) {
1441 m68328_console_baud = n;
1442 m68328_console_cbaud = 0;
1443 if (i > 15) {
1444 m68328_console_cbaud |= CBAUDEX;
1445 i -= 15;
1447 m68328_console_cbaud |= i;
1450 m68328_set_baud(); /* make sure baud rate changes */
1451 return(0);
1455 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1457 *index = c->index;
1458 return serial_driver;
1462 void m68328_console_write (struct console *co, const char *str,
1463 unsigned int count)
1465 if (!m68328_console_initted)
1466 m68328_set_baud();
1467 while (count--) {
1468 if (*str == '\n')
1469 rs_put_char('\r');
1470 rs_put_char( *str++ );
1475 static struct console m68328_driver = {
1476 .name = "ttyS",
1477 .write = m68328_console_write,
1478 .device = m68328_console_device,
1479 .setup = m68328_console_setup,
1480 .flags = CON_PRINTBUFFER,
1481 .index = -1,
1485 static int __init m68328_console_init(void)
1487 register_console(&m68328_driver);
1488 return 0;
1491 console_initcall(m68328_console_init);