PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / drivers / serial / 68328serial.c
blobbbf5bc5892c7be21451fa55c6863af71519ffaff
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>
39 #include <asm/io.h>
40 #include <asm/irq.h>
41 #include <asm/system.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];
69 struct m68k_serial *IRQ_ports[NR_IRQS];
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 #define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
159 /* Sets or clears DTR/RTS on the requested line */
160 static inline void m68k_rtsdtr(struct m68k_serial *ss, int set)
162 if (set) {
163 /* set the RTS/CTS line */
164 } else {
165 /* clear it */
167 return;
170 /* Utility routines */
171 static inline int get_baud(struct m68k_serial *ss)
173 unsigned long result = 115200;
174 unsigned short int baud = uart_addr[ss->line].ubaud;
175 if (GET_FIELD(baud, UBAUD_PRESCALER) == 0x38) result = 38400;
176 result >>= GET_FIELD(baud, UBAUD_DIVIDE);
178 return result;
182 * ------------------------------------------------------------
183 * rs_stop() and rs_start()
185 * This routines are called before setting or resetting tty->stopped.
186 * They enable or disable transmitter interrupts, as necessary.
187 * ------------------------------------------------------------
189 static void rs_stop(struct tty_struct *tty)
191 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
192 m68328_uart *uart = &uart_addr[info->line];
193 unsigned long flags;
195 if (serial_paranoia_check(info, tty->name, "rs_stop"))
196 return;
198 local_irq_save(flags);
199 uart->ustcnt &= ~USTCNT_TXEN;
200 local_irq_restore(flags);
203 static int rs_put_char(char ch)
205 int flags, loops = 0;
207 local_irq_save(flags);
209 while (!(UTX & UTX_TX_AVAIL) && (loops < 1000)) {
210 loops++;
211 udelay(5);
214 UTX_TXDATA = ch;
215 udelay(5);
216 local_irq_restore(flags);
217 return 1;
220 static void rs_start(struct tty_struct *tty)
222 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
223 m68328_uart *uart = &uart_addr[info->line];
224 unsigned long flags;
226 if (serial_paranoia_check(info, tty->name, "rs_start"))
227 return;
229 local_irq_save(flags);
230 if (info->xmit_cnt && info->xmit_buf && !(uart->ustcnt & USTCNT_TXEN)) {
231 #ifdef USE_INTS
232 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
233 #else
234 uart->ustcnt |= USTCNT_TXEN;
235 #endif
237 local_irq_restore(flags);
240 /* Drop into either the boot monitor or kadb upon receiving a break
241 * from keyboard/console input.
243 static void batten_down_hatches(void)
245 /* Drop into the debugger */
248 static void status_handle(struct m68k_serial *info, unsigned short status)
250 #if 0
251 if(status & DCD) {
252 if((info->tty->termios->c_cflag & CRTSCTS) &&
253 ((info->curregs[3] & AUTO_ENAB)==0)) {
254 info->curregs[3] |= AUTO_ENAB;
255 info->pendregs[3] |= AUTO_ENAB;
256 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
258 } else {
259 if((info->curregs[3] & AUTO_ENAB)) {
260 info->curregs[3] &= ~AUTO_ENAB;
261 info->pendregs[3] &= ~AUTO_ENAB;
262 write_zsreg(info->m68k_channel, 3, info->curregs[3]);
265 #endif
266 /* If this is console input and this is a
267 * 'break asserted' status change interrupt
268 * see if we can drop into the debugger
270 if((status & URX_BREAK) && info->break_abort)
271 batten_down_hatches();
272 return;
275 static void receive_chars(struct m68k_serial *info, unsigned short rx)
277 struct tty_struct *tty = info->tty;
278 m68328_uart *uart = &uart_addr[info->line];
279 unsigned char ch, flag;
282 * This do { } while() loop will get ALL chars out of Rx FIFO
284 #ifndef CONFIG_XCOPILOT_BUGS
285 do {
286 #endif
287 ch = GET_FIELD(rx, URX_RXDATA);
289 if(info->is_cons) {
290 if(URX_BREAK & rx) { /* whee, break received */
291 status_handle(info, rx);
292 return;
293 #ifdef CONFIG_MAGIC_SYSRQ
294 } else if (ch == 0x10) { /* ^P */
295 show_state();
296 show_free_areas();
297 show_buffers();
298 /* show_net_buffers(); */
299 return;
300 } else if (ch == 0x12) { /* ^R */
301 emergency_restart();
302 return;
303 #endif /* CONFIG_MAGIC_SYSRQ */
305 /* It is a 'keyboard interrupt' ;-) */
306 #ifdef CONFIG_CONSOLE
307 wake_up(&keypress_wait);
308 #endif
311 if(!tty)
312 goto clear_and_exit;
314 flag = TTY_NORMAL;
316 if(rx & URX_PARITY_ERROR) {
317 flag = TTY_PARITY;
318 status_handle(info, rx);
319 } else if(rx & URX_OVRUN) {
320 flag = TTY_OVERRUN;
321 status_handle(info, rx);
322 } else if(rx & URX_FRAME_ERROR) {
323 flag = TTY_FRAME;
324 status_handle(info, rx);
326 tty_insert_flip_char(tty, ch, flag);
327 #ifndef CONFIG_XCOPILOT_BUGS
328 } while((rx = uart->urx.w) & URX_DATA_READY);
329 #endif
331 tty_schedule_flip(tty);
333 clear_and_exit:
334 return;
337 static void transmit_chars(struct m68k_serial *info)
339 m68328_uart *uart = &uart_addr[info->line];
341 if (info->x_char) {
342 /* Send next char */
343 uart->utx.b.txdata = info->x_char;
344 info->x_char = 0;
345 goto clear_and_return;
348 if((info->xmit_cnt <= 0) || info->tty->stopped) {
349 /* That's peculiar... TX ints off */
350 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
351 goto clear_and_return;
354 /* Send char */
355 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
356 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
357 info->xmit_cnt--;
359 if (info->xmit_cnt < WAKEUP_CHARS)
360 schedule_work(&info->tqueue);
362 if(info->xmit_cnt <= 0) {
363 /* All done for now... TX ints off */
364 uart->ustcnt &= ~USTCNT_TX_INTR_MASK;
365 goto clear_and_return;
368 clear_and_return:
369 /* Clear interrupt (should be auto)*/
370 return;
374 * This is the serial driver's generic interrupt routine
376 irqreturn_t rs_interrupt(int irq, void *dev_id)
378 struct m68k_serial * info;
379 m68328_uart *uart;
380 unsigned short rx;
381 unsigned short tx;
383 info = IRQ_ports[irq];
384 if(!info)
385 return IRQ_NONE;
387 uart = &uart_addr[info->line];
388 rx = uart->urx.w;
390 #ifdef USE_INTS
391 tx = uart->utx.w;
393 if (rx & URX_DATA_READY) receive_chars(info, rx);
394 if (tx & UTX_TX_AVAIL) transmit_chars(info);
395 #else
396 receive_chars(info, rx);
397 #endif
398 return IRQ_HANDLED;
401 static void do_softint(struct work_struct *work)
403 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue);
404 struct tty_struct *tty;
406 tty = info->tty;
407 if (!tty)
408 return;
409 #if 0
410 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
411 tty_wakeup(tty);
413 #endif
417 * This routine is called from the scheduler tqueue when the interrupt
418 * routine has signalled that a hangup has occurred. The path of
419 * hangup processing is:
421 * serial interrupt routine -> (scheduler tqueue) ->
422 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
425 static void do_serial_hangup(struct work_struct *work)
427 struct m68k_serial *info = container_of(work, struct m68k_serial, tqueue_hangup);
428 struct tty_struct *tty;
430 tty = info->tty;
431 if (!tty)
432 return;
434 tty_hangup(tty);
438 static int startup(struct m68k_serial * info)
440 m68328_uart *uart = &uart_addr[info->line];
441 unsigned long flags;
443 if (info->flags & S_INITIALIZED)
444 return 0;
446 if (!info->xmit_buf) {
447 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
448 if (!info->xmit_buf)
449 return -ENOMEM;
452 local_irq_save(flags);
455 * Clear the FIFO buffers and disable them
456 * (they will be reenabled in change_speed())
459 uart->ustcnt = USTCNT_UEN;
460 info->xmit_fifo_size = 1;
461 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_TXEN;
462 (void)uart->urx.w;
465 * Finally, enable sequencing and interrupts
467 #ifdef USE_INTS
468 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN |
469 USTCNT_RX_INTR_MASK | USTCNT_TX_INTR_MASK;
470 #else
471 uart->ustcnt = USTCNT_UEN | USTCNT_RXEN | USTCNT_RX_INTR_MASK;
472 #endif
474 if (info->tty)
475 clear_bit(TTY_IO_ERROR, &info->tty->flags);
476 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
479 * and set the speed of the serial port
482 change_speed(info);
484 info->flags |= S_INITIALIZED;
485 local_irq_restore(flags);
486 return 0;
490 * This routine will shutdown a serial port; interrupts are disabled, and
491 * DTR is dropped if the hangup on close termio flag is on.
493 static void shutdown(struct m68k_serial * info)
495 m68328_uart *uart = &uart_addr[info->line];
496 unsigned long flags;
498 uart->ustcnt = 0; /* All off! */
499 if (!(info->flags & S_INITIALIZED))
500 return;
502 local_irq_save(flags);
504 if (info->xmit_buf) {
505 free_page((unsigned long) info->xmit_buf);
506 info->xmit_buf = 0;
509 if (info->tty)
510 set_bit(TTY_IO_ERROR, &info->tty->flags);
512 info->flags &= ~S_INITIALIZED;
513 local_irq_restore(flags);
516 struct {
517 int divisor, prescale;
519 #ifndef CONFIG_M68VZ328
520 hw_baud_table[18] = {
521 {0,0}, /* 0 */
522 {0,0}, /* 50 */
523 {0,0}, /* 75 */
524 {0,0}, /* 110 */
525 {0,0}, /* 134 */
526 {0,0}, /* 150 */
527 {0,0}, /* 200 */
528 {7,0x26}, /* 300 */
529 {6,0x26}, /* 600 */
530 {5,0x26}, /* 1200 */
531 {0,0}, /* 1800 */
532 {4,0x26}, /* 2400 */
533 {3,0x26}, /* 4800 */
534 {2,0x26}, /* 9600 */
535 {1,0x26}, /* 19200 */
536 {0,0x26}, /* 38400 */
537 {1,0x38}, /* 57600 */
538 {0,0x38}, /* 115200 */
540 #else
541 hw_baud_table[18] = {
542 {0,0}, /* 0 */
543 {0,0}, /* 50 */
544 {0,0}, /* 75 */
545 {0,0}, /* 110 */
546 {0,0}, /* 134 */
547 {0,0}, /* 150 */
548 {0,0}, /* 200 */
549 {0,0}, /* 300 */
550 {7,0x26}, /* 600 */
551 {6,0x26}, /* 1200 */
552 {0,0}, /* 1800 */
553 {5,0x26}, /* 2400 */
554 {4,0x26}, /* 4800 */
555 {3,0x26}, /* 9600 */
556 {2,0x26}, /* 19200 */
557 {1,0x26}, /* 38400 */
558 {0,0x26}, /* 57600 */
559 {1,0x38}, /* 115200 */
561 #endif
562 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
565 * This routine is called to set the UART divisor registers to match
566 * the specified baud rate for a serial port.
568 static void change_speed(struct m68k_serial *info)
570 m68328_uart *uart = &uart_addr[info->line];
571 unsigned short port;
572 unsigned short ustcnt;
573 unsigned cflag;
574 int i;
576 if (!info->tty || !info->tty->termios)
577 return;
578 cflag = info->tty->termios->c_cflag;
579 if (!(port = info->port))
580 return;
582 ustcnt = uart->ustcnt;
583 uart->ustcnt = ustcnt & ~USTCNT_TXEN;
585 i = cflag & CBAUD;
586 if (i & CBAUDEX) {
587 i = (i & ~CBAUDEX) + B38400;
590 info->baud = baud_table[i];
591 uart->ubaud = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
592 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
594 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
596 if ((cflag & CSIZE) == CS8)
597 ustcnt |= USTCNT_8_7;
599 if (cflag & CSTOPB)
600 ustcnt |= USTCNT_STOP;
602 if (cflag & PARENB)
603 ustcnt |= USTCNT_PARITYEN;
604 if (cflag & PARODD)
605 ustcnt |= USTCNT_ODD_EVEN;
607 #ifdef CONFIG_SERIAL_68328_RTS_CTS
608 if (cflag & CRTSCTS) {
609 uart->utx.w &= ~ UTX_NOCTS;
610 } else {
611 uart->utx.w |= UTX_NOCTS;
613 #endif
615 ustcnt |= USTCNT_TXEN;
617 uart->ustcnt = ustcnt;
618 return;
622 * Fair output driver allows a process to speak.
624 static void rs_fair_output(void)
626 int left; /* Output no more than that */
627 unsigned long flags;
628 struct m68k_serial *info = &m68k_soft[0];
629 char c;
631 if (info == 0) return;
632 if (info->xmit_buf == 0) return;
634 local_irq_save(flags);
635 left = info->xmit_cnt;
636 while (left != 0) {
637 c = info->xmit_buf[info->xmit_tail];
638 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
639 info->xmit_cnt--;
640 local_irq_restore(flags);
642 rs_put_char(c);
644 local_irq_save(flags);
645 left = min(info->xmit_cnt, left-1);
648 /* Last character is being transmitted now (hopefully). */
649 udelay(5);
651 local_irq_restore(flags);
652 return;
656 * m68k_console_print is registered for printk.
658 void console_print_68328(const char *p)
660 char c;
662 while((c=*(p++)) != 0) {
663 if(c == '\n')
664 rs_put_char('\r');
665 rs_put_char(c);
668 /* Comment this if you want to have a strict interrupt-driven output */
669 rs_fair_output();
671 return;
674 static void rs_set_ldisc(struct tty_struct *tty)
676 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
678 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
679 return;
681 info->is_cons = (tty->termios->c_line == N_TTY);
683 printk("ttyS%d console mode %s\n", info->line, info->is_cons ? "on" : "off");
686 static void rs_flush_chars(struct tty_struct *tty)
688 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
689 m68328_uart *uart = &uart_addr[info->line];
690 unsigned long flags;
692 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
693 return;
694 #ifndef USE_INTS
695 for(;;) {
696 #endif
698 /* Enable transmitter */
699 local_irq_save(flags);
701 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
702 !info->xmit_buf) {
703 local_irq_restore(flags);
704 return;
707 #ifdef USE_INTS
708 uart->ustcnt |= USTCNT_TXEN | USTCNT_TX_INTR_MASK;
709 #else
710 uart->ustcnt |= USTCNT_TXEN;
711 #endif
713 #ifdef USE_INTS
714 if (uart->utx.w & UTX_TX_AVAIL) {
715 #else
716 if (1) {
717 #endif
718 /* Send char */
719 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
720 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
721 info->xmit_cnt--;
724 #ifndef USE_INTS
725 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
727 #endif
728 local_irq_restore(flags);
731 extern void console_printn(const char * b, int count);
733 static int rs_write(struct tty_struct * tty,
734 const unsigned char *buf, int count)
736 int c, total = 0;
737 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
738 m68328_uart *uart = &uart_addr[info->line];
739 unsigned long flags;
741 if (serial_paranoia_check(info, tty->name, "rs_write"))
742 return 0;
744 if (!tty || !info->xmit_buf)
745 return 0;
747 local_save_flags(flags);
748 while (1) {
749 local_irq_disable();
750 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
751 SERIAL_XMIT_SIZE - info->xmit_head));
752 local_irq_restore(flags);
754 if (c <= 0)
755 break;
757 memcpy(info->xmit_buf + info->xmit_head, buf, c);
759 local_irq_disable();
760 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
761 info->xmit_cnt += c;
762 local_irq_restore(flags);
763 buf += c;
764 count -= c;
765 total += c;
768 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
769 /* Enable transmitter */
770 local_irq_disable();
771 #ifndef USE_INTS
772 while(info->xmit_cnt) {
773 #endif
775 uart->ustcnt |= USTCNT_TXEN;
776 #ifdef USE_INTS
777 uart->ustcnt |= USTCNT_TX_INTR_MASK;
778 #else
779 while (!(uart->utx.w & UTX_TX_AVAIL)) udelay(5);
780 #endif
781 if (uart->utx.w & UTX_TX_AVAIL) {
782 uart->utx.b.txdata = info->xmit_buf[info->xmit_tail++];
783 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
784 info->xmit_cnt--;
787 #ifndef USE_INTS
789 #endif
790 local_irq_restore(flags);
793 return total;
796 static int rs_write_room(struct tty_struct *tty)
798 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
799 int ret;
801 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
802 return 0;
803 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
804 if (ret < 0)
805 ret = 0;
806 return ret;
809 static int rs_chars_in_buffer(struct tty_struct *tty)
811 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
813 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
814 return 0;
815 return info->xmit_cnt;
818 static void rs_flush_buffer(struct tty_struct *tty)
820 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
821 unsigned long flags;
823 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
824 return;
825 local_irq_save(flags);
826 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
827 local_irq_restore(flags);
828 tty_wakeup(tty);
832 * ------------------------------------------------------------
833 * rs_throttle()
835 * This routine is called by the upper-layer tty layer to signal that
836 * incoming characters should be throttled.
837 * ------------------------------------------------------------
839 static void rs_throttle(struct tty_struct * tty)
841 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
843 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
844 return;
846 if (I_IXOFF(tty))
847 info->x_char = STOP_CHAR(tty);
849 /* Turn off RTS line (do this atomic) */
852 static void rs_unthrottle(struct tty_struct * tty)
854 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
856 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
857 return;
859 if (I_IXOFF(tty)) {
860 if (info->x_char)
861 info->x_char = 0;
862 else
863 info->x_char = START_CHAR(tty);
866 /* Assert RTS line (do this atomic) */
870 * ------------------------------------------------------------
871 * rs_ioctl() and friends
872 * ------------------------------------------------------------
875 static int get_serial_info(struct m68k_serial * info,
876 struct serial_struct * retinfo)
878 struct serial_struct tmp;
880 if (!retinfo)
881 return -EFAULT;
882 memset(&tmp, 0, sizeof(tmp));
883 tmp.type = info->type;
884 tmp.line = info->line;
885 tmp.port = info->port;
886 tmp.irq = info->irq;
887 tmp.flags = info->flags;
888 tmp.baud_base = info->baud_base;
889 tmp.close_delay = info->close_delay;
890 tmp.closing_wait = info->closing_wait;
891 tmp.custom_divisor = info->custom_divisor;
892 copy_to_user(retinfo,&tmp,sizeof(*retinfo));
893 return 0;
896 static int set_serial_info(struct m68k_serial * info,
897 struct serial_struct * new_info)
899 struct serial_struct new_serial;
900 struct m68k_serial old_info;
901 int retval = 0;
903 if (!new_info)
904 return -EFAULT;
905 copy_from_user(&new_serial,new_info,sizeof(new_serial));
906 old_info = *info;
908 if (!capable(CAP_SYS_ADMIN)) {
909 if ((new_serial.baud_base != info->baud_base) ||
910 (new_serial.type != info->type) ||
911 (new_serial.close_delay != info->close_delay) ||
912 ((new_serial.flags & ~S_USR_MASK) !=
913 (info->flags & ~S_USR_MASK)))
914 return -EPERM;
915 info->flags = ((info->flags & ~S_USR_MASK) |
916 (new_serial.flags & S_USR_MASK));
917 info->custom_divisor = new_serial.custom_divisor;
918 goto check_and_exit;
921 if (info->count > 1)
922 return -EBUSY;
925 * OK, past this point, all the error checking has been done.
926 * At this point, we start making changes.....
929 info->baud_base = new_serial.baud_base;
930 info->flags = ((info->flags & ~S_FLAGS) |
931 (new_serial.flags & S_FLAGS));
932 info->type = new_serial.type;
933 info->close_delay = new_serial.close_delay;
934 info->closing_wait = new_serial.closing_wait;
936 check_and_exit:
937 retval = startup(info);
938 return retval;
942 * get_lsr_info - get line status register info
944 * Purpose: Let user call ioctl() to get info when the UART physically
945 * is emptied. On bus types like RS485, the transmitter must
946 * release the bus after transmitting. This must be done when
947 * the transmit shift register is empty, not be done when the
948 * transmit holding register is empty. This functionality
949 * allows an RS485 driver to be written in user space.
951 static int get_lsr_info(struct m68k_serial * info, unsigned int *value)
953 #ifdef CONFIG_SERIAL_68328_RTS_CTS
954 m68328_uart *uart = &uart_addr[info->line];
955 #endif
956 unsigned char status;
957 unsigned long flags;
959 local_irq_save(flags);
960 #ifdef CONFIG_SERIAL_68328_RTS_CTS
961 status = (uart->utx.w & UTX_CTS_STAT) ? 1 : 0;
962 #else
963 status = 0;
964 #endif
965 local_irq_restore(flags);
966 put_user(status,value);
967 return 0;
971 * This routine sends a break character out the serial port.
973 static void send_break(struct m68k_serial * info, unsigned int duration)
975 m68328_uart *uart = &uart_addr[info->line];
976 unsigned long flags;
977 if (!info->port)
978 return;
979 local_irq_save(flags);
980 #ifdef USE_INTS
981 uart->utx.w |= UTX_SEND_BREAK;
982 msleep_interruptible(duration);
983 uart->utx.w &= ~UTX_SEND_BREAK;
984 #endif
985 local_irq_restore(flags);
988 static int rs_ioctl(struct tty_struct *tty, struct file * file,
989 unsigned int cmd, unsigned long arg)
991 int error;
992 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
993 int retval;
995 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
996 return -ENODEV;
998 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
999 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1000 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1001 if (tty->flags & (1 << TTY_IO_ERROR))
1002 return -EIO;
1005 switch (cmd) {
1006 case TCSBRK: /* SVID version: non-zero arg --> no break */
1007 retval = tty_check_change(tty);
1008 if (retval)
1009 return retval;
1010 tty_wait_until_sent(tty, 0);
1011 if (!arg)
1012 send_break(info, 250); /* 1/4 second */
1013 return 0;
1014 case TCSBRKP: /* support for POSIX tcsendbreak() */
1015 retval = tty_check_change(tty);
1016 if (retval)
1017 return retval;
1018 tty_wait_until_sent(tty, 0);
1019 send_break(info, arg ? arg*(100) : 250);
1020 return 0;
1021 case TIOCGSERIAL:
1022 if (access_ok(VERIFY_WRITE, (void *) arg,
1023 sizeof(struct serial_struct)))
1024 return get_serial_info(info,
1025 (struct serial_struct *) arg);
1026 return -EFAULT;
1027 case TIOCSSERIAL:
1028 return set_serial_info(info,
1029 (struct serial_struct *) arg);
1030 case TIOCSERGETLSR: /* Get line status register */
1031 if (access_ok(VERIFY_WRITE, (void *) arg,
1032 sizeof(unsigned int)))
1033 return get_lsr_info(info, (unsigned int *) arg);
1034 return -EFAULT;
1035 case TIOCSERGSTRUCT:
1036 if (!access_ok(VERIFY_WRITE, (void *) arg,
1037 sizeof(struct m68k_serial)))
1038 return -EFAULT;
1039 copy_to_user((struct m68k_serial *) arg,
1040 info, sizeof(struct m68k_serial));
1041 return 0;
1043 default:
1044 return -ENOIOCTLCMD;
1046 return 0;
1049 static void rs_set_termios(struct tty_struct *tty, struct ktermios *old_termios)
1051 struct m68k_serial *info = (struct m68k_serial *)tty->driver_data;
1053 change_speed(info);
1055 if ((old_termios->c_cflag & CRTSCTS) &&
1056 !(tty->termios->c_cflag & CRTSCTS)) {
1057 tty->hw_stopped = 0;
1058 rs_start(tty);
1064 * ------------------------------------------------------------
1065 * rs_close()
1067 * This routine is called when the serial port gets closed. First, we
1068 * wait for the last remaining data to be sent. Then, we unlink its
1069 * S structure from the interrupt chain if necessary, and we free
1070 * that IRQ if nothing is left in the chain.
1071 * ------------------------------------------------------------
1073 static void rs_close(struct tty_struct *tty, struct file * filp)
1075 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1076 m68328_uart *uart = &uart_addr[info->line];
1077 unsigned long flags;
1079 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1080 return;
1082 local_irq_save(flags);
1084 if (tty_hung_up_p(filp)) {
1085 local_irq_restore(flags);
1086 return;
1089 if ((tty->count == 1) && (info->count != 1)) {
1091 * Uh, oh. tty->count is 1, which means that the tty
1092 * structure will be freed. Info->count should always
1093 * be one in these conditions. If it's greater than
1094 * one, we've got real problems, since it means the
1095 * serial port won't be shutdown.
1097 printk("rs_close: bad serial port count; tty->count is 1, "
1098 "info->count is %d\n", info->count);
1099 info->count = 1;
1101 if (--info->count < 0) {
1102 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1103 info->line, info->count);
1104 info->count = 0;
1106 if (info->count) {
1107 local_irq_restore(flags);
1108 return;
1110 info->flags |= S_CLOSING;
1112 * Now we wait for the transmit buffer to clear; and we notify
1113 * the line discipline to only process XON/XOFF characters.
1115 tty->closing = 1;
1116 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1117 tty_wait_until_sent(tty, info->closing_wait);
1119 * At this point we stop accepting input. To do this, we
1120 * disable the receive line status interrupts, and tell the
1121 * interrupt driver to stop checking the data ready bit in the
1122 * line status register.
1125 uart->ustcnt &= ~USTCNT_RXEN;
1126 uart->ustcnt &= ~(USTCNT_RXEN | USTCNT_RX_INTR_MASK);
1128 shutdown(info);
1129 rs_flush_buffer(tty);
1131 tty_ldisc_flush(tty);
1132 tty->closing = 0;
1133 info->event = 0;
1134 info->tty = 0;
1135 #warning "This is not and has never been valid so fix it"
1136 #if 0
1137 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1138 if (tty->ldisc.close)
1139 (tty->ldisc.close)(tty);
1140 tty->ldisc = ldiscs[N_TTY];
1141 tty->termios->c_line = N_TTY;
1142 if (tty->ldisc.open)
1143 (tty->ldisc.open)(tty);
1145 #endif
1146 if (info->blocked_open) {
1147 if (info->close_delay) {
1148 msleep_interruptible(jiffies_to_msecs(info->close_delay));
1150 wake_up_interruptible(&info->open_wait);
1152 info->flags &= ~(S_NORMAL_ACTIVE|S_CLOSING);
1153 wake_up_interruptible(&info->close_wait);
1154 local_irq_restore(flags);
1158 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1160 void rs_hangup(struct tty_struct *tty)
1162 struct m68k_serial * info = (struct m68k_serial *)tty->driver_data;
1164 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1165 return;
1167 rs_flush_buffer(tty);
1168 shutdown(info);
1169 info->event = 0;
1170 info->count = 0;
1171 info->flags &= ~S_NORMAL_ACTIVE;
1172 info->tty = 0;
1173 wake_up_interruptible(&info->open_wait);
1177 * ------------------------------------------------------------
1178 * rs_open() and friends
1179 * ------------------------------------------------------------
1181 static int block_til_ready(struct tty_struct *tty, struct file * filp,
1182 struct m68k_serial *info)
1184 DECLARE_WAITQUEUE(wait, current);
1185 int retval;
1186 int do_clocal = 0;
1189 * If the device is in the middle of being closed, then block
1190 * until it's done, and then try again.
1192 if (info->flags & S_CLOSING) {
1193 interruptible_sleep_on(&info->close_wait);
1194 #ifdef SERIAL_DO_RESTART
1195 if (info->flags & S_HUP_NOTIFY)
1196 return -EAGAIN;
1197 else
1198 return -ERESTARTSYS;
1199 #else
1200 return -EAGAIN;
1201 #endif
1205 * If non-blocking mode is set, or the port is not enabled,
1206 * then make the check up front and then exit.
1208 if ((filp->f_flags & O_NONBLOCK) ||
1209 (tty->flags & (1 << TTY_IO_ERROR))) {
1210 info->flags |= S_NORMAL_ACTIVE;
1211 return 0;
1214 if (tty->termios->c_cflag & CLOCAL)
1215 do_clocal = 1;
1218 * Block waiting for the carrier detect and the line to become
1219 * free (i.e., not in use by the callout). While we are in
1220 * this loop, info->count is dropped by one, so that
1221 * rs_close() knows when to free things. We restore it upon
1222 * exit, either normal or abnormal.
1224 retval = 0;
1225 add_wait_queue(&info->open_wait, &wait);
1227 info->count--;
1228 info->blocked_open++;
1229 while (1) {
1230 local_irq_disable();
1231 m68k_rtsdtr(info, 1);
1232 local_irq_enable();
1233 current->state = TASK_INTERRUPTIBLE;
1234 if (tty_hung_up_p(filp) ||
1235 !(info->flags & S_INITIALIZED)) {
1236 #ifdef SERIAL_DO_RESTART
1237 if (info->flags & S_HUP_NOTIFY)
1238 retval = -EAGAIN;
1239 else
1240 retval = -ERESTARTSYS;
1241 #else
1242 retval = -EAGAIN;
1243 #endif
1244 break;
1246 if (!(info->flags & S_CLOSING) && do_clocal)
1247 break;
1248 if (signal_pending(current)) {
1249 retval = -ERESTARTSYS;
1250 break;
1252 schedule();
1254 current->state = TASK_RUNNING;
1255 remove_wait_queue(&info->open_wait, &wait);
1256 if (!tty_hung_up_p(filp))
1257 info->count++;
1258 info->blocked_open--;
1260 if (retval)
1261 return retval;
1262 info->flags |= S_NORMAL_ACTIVE;
1263 return 0;
1267 * This routine is called whenever a serial port is opened. It
1268 * enables interrupts for a serial port, linking in its S structure into
1269 * the IRQ chain. It also performs the serial-specific
1270 * initialization for the tty structure.
1272 int rs_open(struct tty_struct *tty, struct file * filp)
1274 struct m68k_serial *info;
1275 int retval, line;
1277 line = tty->index;
1279 if (line >= NR_PORTS || line < 0) /* we have exactly one */
1280 return -ENODEV;
1282 info = &m68k_soft[line];
1284 if (serial_paranoia_check(info, tty->name, "rs_open"))
1285 return -ENODEV;
1287 info->count++;
1288 tty->driver_data = info;
1289 info->tty = tty;
1292 * Start up serial port
1294 retval = startup(info);
1295 if (retval)
1296 return retval;
1298 return block_til_ready(tty, filp, info);
1301 /* Finally, routines used to initialize the serial driver. */
1303 static void show_serial_version(void)
1305 printk("MC68328 serial driver version 1.00\n");
1308 static const struct tty_operations rs_ops = {
1309 .open = rs_open,
1310 .close = rs_close,
1311 .write = rs_write,
1312 .flush_chars = rs_flush_chars,
1313 .write_room = rs_write_room,
1314 .chars_in_buffer = rs_chars_in_buffer,
1315 .flush_buffer = rs_flush_buffer,
1316 .ioctl = rs_ioctl,
1317 .throttle = rs_throttle,
1318 .unthrottle = rs_unthrottle,
1319 .set_termios = rs_set_termios,
1320 .stop = rs_stop,
1321 .start = rs_start,
1322 .hangup = rs_hangup,
1323 .set_ldisc = rs_set_ldisc,
1326 /* rs_init inits the driver */
1327 static int __init
1328 rs68328_init(void)
1330 int flags, i;
1331 struct m68k_serial *info;
1333 serial_driver = alloc_tty_driver(NR_PORTS);
1334 if (!serial_driver)
1335 return -ENOMEM;
1337 show_serial_version();
1339 /* Initialize the tty_driver structure */
1340 /* SPARC: Not all of this is exactly right for us. */
1342 serial_driver->name = "ttyS";
1343 serial_driver->major = TTY_MAJOR;
1344 serial_driver->minor_start = 64;
1345 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1346 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1347 serial_driver->init_termios = tty_std_termios;
1348 serial_driver->init_termios.c_cflag =
1349 m68328_console_cbaud | CS8 | CREAD | HUPCL | CLOCAL;
1350 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1351 tty_set_operations(serial_driver, &rs_ops);
1353 if (tty_register_driver(serial_driver)) {
1354 put_tty_driver(serial_driver);
1355 printk(KERN_ERR "Couldn't register serial driver\n");
1356 return -ENOMEM;
1359 local_irq_save(flags);
1361 for(i=0;i<NR_PORTS;i++) {
1363 info = &m68k_soft[i];
1364 info->magic = SERIAL_MAGIC;
1365 info->port = (int) &uart_addr[i];
1366 info->tty = 0;
1367 info->irq = uart_irqs[i];
1368 info->custom_divisor = 16;
1369 info->close_delay = 50;
1370 info->closing_wait = 3000;
1371 info->x_char = 0;
1372 info->event = 0;
1373 info->count = 0;
1374 info->blocked_open = 0;
1375 INIT_WORK(&info->tqueue, do_softint);
1376 INIT_WORK(&info->tqueue_hangup, do_serial_hangup);
1377 init_waitqueue_head(&info->open_wait);
1378 init_waitqueue_head(&info->close_wait);
1379 info->line = i;
1380 info->is_cons = 1; /* Means shortcuts work */
1382 printk("%s%d at 0x%08x (irq = %d)", serial_driver->name, info->line,
1383 info->port, info->irq);
1384 printk(" is a builtin MC68328 UART\n");
1386 IRQ_ports[info->irq] = info; /* waste of space */
1388 #ifdef CONFIG_M68VZ328
1389 if (i > 0 )
1390 PJSEL &= 0xCF; /* PSW enable second port output */
1391 #endif
1393 if (request_irq(uart_irqs[i],
1394 rs_interrupt,
1395 IRQF_DISABLED,
1396 "M68328_UART", NULL))
1397 panic("Unable to attach 68328 serial interrupt\n");
1399 local_irq_restore(flags);
1400 return 0;
1403 module_init(rs68328_init);
1407 static void m68328_set_baud(void)
1409 unsigned short ustcnt;
1410 int i;
1412 ustcnt = USTCNT;
1413 USTCNT = ustcnt & ~USTCNT_TXEN;
1415 again:
1416 for (i = 0; i < sizeof(baud_table) / sizeof(baud_table[0]); i++)
1417 if (baud_table[i] == m68328_console_baud)
1418 break;
1419 if (i >= sizeof(baud_table) / sizeof(baud_table[0])) {
1420 m68328_console_baud = 9600;
1421 goto again;
1424 UBAUD = PUT_FIELD(UBAUD_DIVIDE, hw_baud_table[i].divisor) |
1425 PUT_FIELD(UBAUD_PRESCALER, hw_baud_table[i].prescale);
1426 ustcnt &= ~(USTCNT_PARITYEN | USTCNT_ODD_EVEN | USTCNT_STOP | USTCNT_8_7);
1427 ustcnt |= USTCNT_8_7;
1428 ustcnt |= USTCNT_TXEN;
1429 USTCNT = ustcnt;
1430 m68328_console_initted = 1;
1431 return;
1435 int m68328_console_setup(struct console *cp, char *arg)
1437 int i, n = CONSOLE_BAUD_RATE;
1439 if (!cp)
1440 return(-1);
1442 if (arg)
1443 n = simple_strtoul(arg,NULL,0);
1445 for (i = 0; i < BAUD_TABLE_SIZE; i++)
1446 if (baud_table[i] == n)
1447 break;
1448 if (i < BAUD_TABLE_SIZE) {
1449 m68328_console_baud = n;
1450 m68328_console_cbaud = 0;
1451 if (i > 15) {
1452 m68328_console_cbaud |= CBAUDEX;
1453 i -= 15;
1455 m68328_console_cbaud |= i;
1458 m68328_set_baud(); /* make sure baud rate changes */
1459 return(0);
1463 static struct tty_driver *m68328_console_device(struct console *c, int *index)
1465 *index = c->index;
1466 return serial_driver;
1470 void m68328_console_write (struct console *co, const char *str,
1471 unsigned int count)
1473 if (!m68328_console_initted)
1474 m68328_set_baud();
1475 while (count--) {
1476 if (*str == '\n')
1477 rs_put_char('\r');
1478 rs_put_char( *str++ );
1483 static struct console m68328_driver = {
1484 .name = "ttyS",
1485 .write = m68328_console_write,
1486 .device = m68328_console_device,
1487 .setup = m68328_console_setup,
1488 .flags = CON_PRINTBUFFER,
1489 .index = -1,
1493 static int __init m68328_console_init(void)
1495 register_console(&m68328_driver);
1496 return 0;
1499 console_initcall(m68328_console_init);