MOXA linux-2.6.x / linux-2.6.9-uc0 from sdlinux-moxaart.tgz
[linux-2.6.9-moxart.git] / drivers / serial / serial_atmel.c
blobe7b2a42d57feabdd4193e45bd12201a94bca7d28
1 /* serial port driver for the Atmel AT91 series builtin USARTs
3 * Copyright (C) 2000, 2001 Erik Andersen <andersen@lineo.com>
4 * Copyright (C) 2004 Hyok S. Choi <hyok.choi@samsung.com>
6 * Based on:
7 * drivers/char/68302serial.c
8 * and also based on trioserial.c from Aplio, though this driver
9 * has been extensively changed since then. No author was
10 * listed in trioserial.c.
12 * Phil Wilshire 12/31/2002 Fixed multiple ^@ chars on TCSETA
13 * Hyok S. Choi 03/22/2004 2.6 port
16 /* Enable this to force this driver to always operate at 57600 */
17 #undef FORCE_57600
19 #include <linux/config.h>
20 #include <linux/version.h>
21 #include <linux/types.h>
22 #include <linux/serial.h>
23 #include <linux/errno.h>
24 #include <linux/signal.h>
25 #include <linux/sched.h>
26 #include <linux/timer.h>
27 #include <linux/interrupt.h>
28 #include <linux/tty.h>
29 #include <linux/tty_flip.h>
30 #include <linux/config.h>
31 #include <linux/major.h>
32 #include <linux/string.h>
33 #include <linux/fcntl.h>
34 #include <linux/mm.h>
35 #include <linux/kernel.h>
36 #include <linux/init.h>
37 #include <linux/console.h>
39 #include <asm/io.h>
40 #include <asm/irq.h>
41 #include <asm/arch/irq.h>
42 #include <asm/system.h>
43 #include <asm/segment.h>
44 #include <asm/bitops.h>
45 #include <asm/delay.h>
46 #include <asm/uaccess.h>
48 #include "serial_atmel.h"
50 #define USE_INTS 1
52 static volatile struct atmel_usart_regs *usarts[AT91_USART_CNT] = {
53 (volatile struct atmel_usart_regs *) AT91_USART0_BASE,
54 (volatile struct atmel_usart_regs *) AT91_USART1_BASE
57 #define SERIAL_XMIT_SIZE PAGE_SIZE
58 #define RX_SERIAL_SIZE 256
60 static struct atmel_serial atmel_info[AT91_USART_CNT];
61 static struct tty_struct *serial_table[AT91_USART_CNT];
62 struct atmel_serial *atmel_consinfo = 0;
64 #define UART_CLOCK (ARM_CLK/16)
66 static struct work_struct serialpoll;
68 #ifdef CONFIG_CONSOLE
69 extern wait_queue_head_t keypress_wait;
70 #endif
72 struct tty_driver *serial_driver;
74 /* serial subtype definitions */
75 #define SERIAL_TYPE_NORMAL 1
77 /* number of characters left in xmit buffer before we ask for more */
78 #define WAKEUP_CHARS 256
80 /* Debugging... DEBUG_INTR is bad to use when one of the zs
81 * lines is your console ;(
83 #undef SERIAL_DEBUG_INTR
84 #undef SERIAL_DEBUG_OPEN
85 #undef SERIAL_DEBUG_FLOW
87 #define RS_ISR_PASS_LIMIT 256
89 #define _INLINE_ inline
91 #ifndef MIN
92 #define MIN(a,b) ((a) < (b) ? (a) : (b))
93 #endif
98 * tmp_buf is used as a temporary buffer by serial_write. We need to
99 * lock it in case the memcpy_fromfs blocks while swapping in a page,
100 * and some other program tries to do a serial write at the same time.
101 * Since the lock will only come under contention when the system is
102 * swapping and available memory is low, it makes sense to share one
103 * buffer across all the serial ports, since it significantly saves
104 * memory if large numbers of serial ports are open.
106 static unsigned char tmp_buf[SERIAL_XMIT_SIZE]; /* This is cheating */
107 DECLARE_MUTEX(tmp_buf_sem);
109 static inline int serial_paranoia_check(struct atmel_serial *info,
110 char *name, const char *routine)
112 #ifdef SERIAL_PARANOIA_CHECK
113 static const char *badmagic =
114 "Warning: bad magic number for serial struct %s in %s\n";
115 static const char *badinfo =
116 "Warning: null atmel_serial struct for %s in %s\n";
118 if (!info) {
119 printk(badinfo, name, routine);
120 return 1;
122 if (info->magic != SERIAL_MAGIC) {
123 printk(badmagic, name, routine);
124 return 1;
126 #endif
127 return 0;
130 static unsigned char * rx_buf_table[AT91_USART_CNT];
132 static unsigned char rx_buf1[RX_SERIAL_SIZE];
133 static unsigned char rx_buf2[RX_SERIAL_SIZE];
135 /* Console hooks... */
137 static void serpoll(void *data);
139 static void change_speed(struct atmel_serial *info);
141 static char prompt0;
142 static void xmit_char(struct atmel_serial *info, char ch);
143 static void xmit_string(struct atmel_serial *info, char *p, int len);
144 static void start_rx(struct atmel_serial *info);
145 static void wait_EOT(volatile struct atmel_usart_regs *);
146 static void uart_init(struct atmel_serial *info);
147 static void uart_speed(struct atmel_serial *info, unsigned cflag);
149 static void tx_enable(volatile struct atmel_usart_regs *uart);
150 static void rx_enable(volatile struct atmel_usart_regs *uart);
151 static void tx_disable(volatile struct atmel_usart_regs *uart);
152 static void rx_disable(volatile struct atmel_usart_regs *uart);
153 static void tx_stop(volatile struct atmel_usart_regs *uart);
154 static void tx_start(volatile struct atmel_usart_regs *uart, int ints);
155 static void rx_stop(volatile struct atmel_usart_regs *uart);
156 static void rx_start(volatile struct atmel_usart_regs *uart, int ints);
157 static void set_ints_mode(int yes, struct atmel_serial *info);
158 static irqreturn_t rs_interrupt(struct atmel_serial *info);
159 extern void show_net_buffers(void);
160 extern void hard_reset_now(void);
161 static void handle_termios_tcsets(struct termios * ptermios, struct atmel_serial * ptty);
163 static int global;
165 static void coucou1(void)
167 global = 0;
170 static void coucou2(void)
172 global = 1;
174 static void _INLINE_ tx_enable(volatile struct atmel_usart_regs *uart)
176 uart->ier = US_TXEMPTY;
178 static void _INLINE_ rx_enable(volatile struct atmel_usart_regs *uart)
180 uart->ier = US_ENDRX | US_TIMEOUT;
182 static void _INLINE_ tx_disable(volatile struct atmel_usart_regs *uart)
184 uart->idr = US_TXEMPTY;
186 static void _INLINE_ rx_disable(volatile struct atmel_usart_regs *uart)
188 uart->idr = US_ENDRX | US_TIMEOUT;
190 static void _INLINE_ tx_stop(volatile struct atmel_usart_regs *uart)
192 tx_disable(uart);
193 uart->tcr = 0;
194 uart->cr = US_TXDIS;
196 static void _INLINE_ tx_start(volatile struct atmel_usart_regs *uart, int ints)
198 if (ints) {
199 tx_enable(uart);
201 uart->cr = US_TXEN;
203 static void _INLINE_ rx_stop(volatile struct atmel_usart_regs *uart)
205 rx_disable(uart);
206 uart->rtor = 0;
207 // PSW fixes slew of ^@ chars on a TCSETA ioctl
208 //uart->rcr = 0;
209 uart->cr = US_RXDIS;
211 static void _INLINE_ rx_start(volatile struct atmel_usart_regs *uart, int ints)
213 uart->cr = US_RXEN | US_STTO;
214 uart->rtor = 20;
215 if (ints) {
216 rx_enable(uart);
219 static void _INLINE_ reset_status(volatile struct atmel_usart_regs *uart)
221 uart->cr = US_RSTSTA;
223 static void set_ints_mode(int yes, struct atmel_serial *info)
225 info->use_ints = yes;
226 // FIXME: check
227 #if 0
228 (yes) ? unmask_irq(info->irq) : mask_irq(info->irq);
229 #endif
232 #ifdef US_RTS
233 static void atmel_cts_off(struct atmel_serial *info)
235 volatile struct atmel_usart_regs *uart;
237 uart = info->usart;
238 uart->mc &= ~(unsigned long) US_RTS;
239 info->cts_state = 0;
241 static void atmel_cts_on(struct atmel_serial *info)
243 volatile struct atmel_usart_regs *uart;
245 uart = info->usart;
246 uart->mc |= US_RTS;
247 info->cts_state = 1;
249 /* Sets or clears DTR/RTS on the requested line */
250 static inline void atmel_rtsdtr(struct atmel_serial *ss, int set)
252 volatile struct atmel_usart_regs *uart;
254 uart = ss->usart;
255 if (set) {
256 uart->mc |= US_DTR | US_RTS;
257 } else {
258 uart->mc &= ~(unsigned long) (US_DTR | US_RTS);
260 return;
262 #endif /* US_RTS */
265 * ------------------------------------------------------------
266 * rs_stop() and rs_start()
268 * This routines are called before setting or resetting tty->stopped.
269 * They enable or disable transmitter interrupts, as necessary.
270 * ------------------------------------------------------------
272 static void rs_stop(struct tty_struct *tty)
274 struct atmel_serial *info = (struct atmel_serial *)tty->driver_data;
275 unsigned long flags;
277 if (serial_paranoia_check(info, tty->name, "rs_stop"))
278 return;
280 save_flags(flags); cli();
281 tx_stop(info->usart);
282 rx_stop(info->usart);
283 restore_flags(flags);
286 static void rs_put_char(struct atmel_serial *info, char ch)
288 int flags = 0;
290 save_flags(flags); cli();
291 xmit_char(info, ch);
292 wait_EOT(info->usart);
293 restore_flags(flags);
296 static void rs_start(struct tty_struct *tty)
298 struct atmel_serial *info = (struct atmel_serial *)tty->driver_data;
299 unsigned long flags;
301 if (serial_paranoia_check(info, tty->name, "rs_start"))
302 return;
304 save_flags(flags); cli();
305 tx_start(info->usart, info->use_ints);
306 rx_start(info->usart, info->use_ints);
307 /* FIXME */
308 // start_rx(info);
309 restore_flags(flags);
312 /* Drop into either the boot monitor or kadb upon receiving a break
313 * from keyboard/console input.
315 static void batten_down_hatches(void)
317 /* Drop into the debugger */
320 static _INLINE_ void status_handle(struct atmel_serial *info, unsigned long status)
322 #if 0
323 if (status & DCD) {
324 if ((info->tty->termios->c_cflag & CRTSCTS) &&
325 ((info->curregs[3] & AUTO_ENAB) == 0)) {
326 info->curregs[3] |= AUTO_ENAB;
327 info->pendregs[3] |= AUTO_ENAB;
328 write_zsreg(info->atmel_channel, 3, info->curregs[3]);
330 } else {
331 if ((info->curregs[3] & AUTO_ENAB)) {
332 info->curregs[3] &= ~AUTO_ENAB;
333 info->pendregs[3] &= ~AUTO_ENAB;
334 write_zsreg(info->atmel_channel, 3, info->curregs[3]);
337 #endif
338 /* Whee, if this is console input and this is a
339 * 'break asserted' status change interrupt, call
340 * the boot prom.
342 if ((status & US_RXBRK) && info->break_abort)
343 batten_down_hatches();
345 /* XXX Whee, put in a buffer somewhere, the status information
346 * XXX whee whee whee... Where does the information go...
348 reset_status(info->usart);
349 return;
352 static _INLINE_ void receive_chars(struct atmel_serial *info, unsigned long status)
354 int count;
355 volatile struct atmel_usart_regs *uart = info->usart;
357 struct tty_struct *tty = info->tty;
359 if (!(info->flags & S_INITIALIZED))
360 return;
361 count = RX_SERIAL_SIZE - uart->rcr;
362 // hack to receive chars by polling only BD fields
363 if (!count) {
364 return;
367 if (!tty)
368 goto clear_and_exit;
370 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
371 schedule_work(&tty->flip.work);
373 if ((count + tty->flip.count) >= TTY_FLIPBUF_SIZE) {
374 #ifdef US_RTS
375 atmel_cts_off(info);
376 #endif
377 serialpoll.data = (void *) info;
378 schedule_work(&serialpoll);
380 memset(tty->flip.flag_buf_ptr, 0, count);
381 memcpy(tty->flip.char_buf_ptr, info->rx_buf, count);
382 tty->flip.char_buf_ptr += count;
384 if (status & US_PARE)
385 *(tty->flip.flag_buf_ptr - 1) = TTY_PARITY;
386 else if (status & US_OVRE)
387 *(tty->flip.flag_buf_ptr - 1) = TTY_OVERRUN;
388 else if (status & US_FRAME)
389 *(tty->flip.flag_buf_ptr - 1) = TTY_FRAME;
391 tty->flip.count += count;
393 schedule_work(&tty->flip.work);
395 clear_and_exit:
396 start_rx(info);
397 return;
400 static _INLINE_ void transmit_chars(struct atmel_serial *info)
402 if (info->x_char) {
403 /* Send next char */
404 xmit_char(info, info->x_char);
405 info->x_char = 0;
406 goto clear_and_return;
409 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
410 /* That's peculiar... */
411 tx_stop(info->usart);
412 goto clear_and_return;
415 if (info->xmit_tail + info->xmit_cnt < SERIAL_XMIT_SIZE) {
416 xmit_string(info, info->xmit_buf + info->xmit_tail,
417 info->xmit_cnt);
418 info->xmit_tail =
419 (info->xmit_tail + info->xmit_cnt) & (SERIAL_XMIT_SIZE - 1);
420 info->xmit_cnt = 0;
421 } else {
422 coucou1();
423 xmit_string(info, info->xmit_buf + info->xmit_tail,
424 SERIAL_XMIT_SIZE - info->xmit_tail);
425 //xmit_string(info, info->xmit_buf, info->xmit_tail + info->xmit_cnt - SERIAL_XMIT_SIZE);
426 info->xmit_cnt =
427 info->xmit_cnt - (SERIAL_XMIT_SIZE - info->xmit_tail);
428 info->xmit_tail = 0;
431 if (info->xmit_cnt < WAKEUP_CHARS)
432 schedule_work(&info->tqueue);
434 if (info->xmit_cnt <= 0) {
435 //tx_stop(info->usart);
436 goto clear_and_return;
439 clear_and_return:
440 /* Clear interrupt (should be auto) */
441 return;
445 * This is the serial driver's generic interrupt routine
447 static irqreturn_t rs_interrupta(int irq, void *dev_id, struct pt_regs *regs)
449 return rs_interrupt(&atmel_info[0]);
451 static irqreturn_t rs_interruptb(int irq, void *dev_id, struct pt_regs *regs)
453 return rs_interrupt(&atmel_info[1]);
455 static irqreturn_t rs_interrupt(struct atmel_serial *info)
457 unsigned long status;
459 status = info->usart->csr;
460 if (status & (US_ENDRX | US_TIMEOUT)) {
461 receive_chars(info, status);
463 if (status & (US_TXEMPTY)) {
464 transmit_chars(info);
466 status_handle(info, status);
468 #ifdef US_RTS
469 if (!info->cts_state) {
470 if (info->tty->flip.count < TTY_FLIPBUF_SIZE - RX_SERIAL_SIZE) {
471 atmel_cts_on(info);
474 #endif
475 if (!info->use_ints) {
476 serialpoll.data = (void *) info;
477 schedule_work(&serialpoll);
479 return IRQ_HANDLED;
481 static void serpoll(void *data)
483 struct atmel_serial *info = data;
485 rs_interrupt(info);
489 * -------------------------------------------------------------------
490 * Here ends the serial interrupt routines.
491 * -------------------------------------------------------------------
495 static void do_softint(void *private_)
497 struct atmel_serial *info = (struct atmel_serial *) private_;
498 struct tty_struct *tty;
500 tty = info->tty;
501 if (!tty)
502 return;
503 #if 0 // FIXME - CHECK
504 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
505 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
506 tty->ldisc.write_wakeup) (tty->ldisc.write_wakeup) (tty);
507 wake_up_interruptible(&tty->write_wait);
509 #endif
513 * This routine is called from the scheduler tqueue when the interrupt
514 * routine has signalled that a hangup has occurred. The path of
515 * hangup processing is:
517 * serial interrupt routine -> (scheduler tqueue) ->
518 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
521 static void do_serial_hangup(void *private_)
523 struct atmel_serial *info = (struct atmel_serial *) private_;
524 struct tty_struct *tty;
526 tty = info->tty;
527 if (!tty)
528 return;
530 tty_hangup(tty);
535 * This subroutine is called when the RS_TIMER goes off. It is used
536 * by the serial driver to handle ports that do not have an interrupt
537 * (irq=0). This doesn't work at all for 16450's, as a sun has a Z8530.
539 #if 0
540 static void rs_timer(void)
542 panic("rs_timer called\n");
543 return;
545 #endif
547 static unsigned long calcCD(unsigned long br)
549 return (UART_CLOCK / br);
552 static void uart_init(struct atmel_serial *info)
554 volatile struct atmel_usart_regs *uart;
556 if (info) {
557 uart = info->usart;
558 } else {
559 uart = usarts[0];
562 /* Reset the USART */
563 uart->cr = US_TXDIS | US_RXDIS | US_RSTTX | US_RSTRX;
564 /* clear Rx receive and Tx sent counters */
565 uart->rcr = 0;
566 uart->tcr = 0;
568 /* Disable interrups till we say we want them */
569 tx_disable(info->usart);
570 rx_disable(info->usart);
572 /* Set the serial port into a safe sane state */
573 uart->mr = US_USCLKS(0) | US_CLK0 | US_CHMODE(0) | US_NBSTOP(0) |
574 US_PAR(4) | US_CHRL(3);
576 #ifndef FORCE_57600
577 uart->brgr = calcCD(9600);
578 #else
579 uart->brgr = calcCD(57600);
580 #endif
582 uart->rtor = 20; // timeout = value * 4 *bit period
583 uart->ttgr = 0; // no guard time
584 uart->rcr = 0;
585 uart->rpr = 0;
586 uart->tcr = 0;
587 uart->tpr = 0;
588 #ifdef US_RTS
589 uart->mc = 0;
590 #endif
593 /* It is the responsibilty of whoever calls this function to be sure
594 * that that have called
595 * tx_stop(uart); rx_stop(uart);
596 * before calling the function. Failure to do this will cause messy
597 * things to happen. You have been warned. */
598 static void uart_speed(struct atmel_serial *info, unsigned cflag)
600 unsigned baud = info->baud;
601 volatile struct atmel_usart_regs *uart = info->usart;
603 // disable tx and rx
604 uart->cr = US_TXDIS | US_RXDIS;
606 // disable interrupts
607 tx_disable(uart);
608 rx_disable(uart);
610 #ifndef FORCE_57600
611 uart->brgr = calcCD(baud);
612 #else
613 uart->brgr = calcCD(57600);
614 #endif
615 /* FIXME */
616 #if 0
617 /* probably not needed */
618 uart->US_RTOR = 20; // timeout = value * 4 *bit period
619 uart->US_TTGR = 0; // no guard time
620 uart->US_RPR = 0;
621 uart->US_RCR = 0;
622 uart->US_TPR = 0;
623 uart->US_TCR = 0;
624 #endif
627 /* FIXME */
628 #if 0
629 uart->mc = 0;
630 if (cflag != 0xffff) {
631 uart->mr = US_USCLKS(0) | US_CLK0 | US_CHMODE(0) | US_NBSTOP(0) |
632 US_PAR(0);
634 if ((cflag & CSIZE) == CS8)
635 uart->mr |= US_CHRL(3); // 8 bit char
636 else
637 uart->mr |= US_CHRL(2); // 7 bit char
639 if (cflag & CSTOPB)
640 uart->mr |= US_NBSTOP(2); // 2 stop bits
642 if (!(cflag & PARENB))
643 uart->mr |= US_PAR(4); // parity disabled
644 else if (cflag & PARODD)
645 uart->mr |= US_PAR(1); // odd parity
647 #endif
649 /* FIXME */
650 #if 0
651 // enable tx and rx
652 uart->cr = US_TXEN | US_RXEN;
654 // enable interrupts
655 tx_enable();
656 rx_enable();
657 #endif
658 tx_start(uart, info->use_ints);
659 start_rx(info);
662 static void wait_EOT(volatile struct atmel_usart_regs *uart)
664 // make sure tx is enabled
665 uart->cr = US_TXEN;
667 // wait until all chars sent FIXME - is this sane ?
668 while (1) {
669 if (uart->csr & US_TXEMPTY)
670 break;
673 static int startup(struct atmel_serial *info)
675 unsigned long flags;
677 if (info->flags & S_INITIALIZED)
678 return 0;
680 if (!info->xmit_buf) {
681 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
682 if (!info->xmit_buf)
683 return -ENOMEM;
685 if (!info->rx_buf) {
686 //info->rx_buf = (unsigned char *) ))__get_free_page(GFP_KERNEL);
687 //info->rx_buf = rx_buf1;
688 if (!info->rx_buf)
689 return -ENOMEM;
691 save_flags(flags);
692 cli();
693 #ifdef SERIAL_DEBUG_OPEN
694 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
695 #endif
697 * Clear the FIFO buffers and disable them
698 * (they will be reenabled in change_speed())
701 if (info->tty)
702 clear_bit(TTY_IO_ERROR, &info->tty->flags);
703 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
706 * and set the speed of the serial port
709 uart_init(info);
710 //set_ints_mode(0, info);
711 change_speed(info);
712 info->flags |= S_INITIALIZED;
713 restore_flags(flags);
714 return 0;
718 * This routine will shutdown a serial port; interrupts are disabled, and
719 * DTR is dropped if the hangup on close termio flag is on.
721 static void shutdown(struct atmel_serial *info)
723 unsigned long flags;
725 tx_disable(info->usart);
726 rx_disable(info->usart);
727 rx_stop(info->usart); /* All off! */
728 if (!(info->flags & S_INITIALIZED))
729 return;
731 #ifdef SERIAL_DEBUG_OPEN
732 printk("Shutting down serial port %d (irq %d)....\n", info->line,
733 info->irq);
734 #endif
736 save_flags(flags);
737 cli(); /* Disable interrupts */
739 if (info->xmit_buf) {
740 free_page((unsigned long) info->xmit_buf);
741 info->xmit_buf = 0;
744 if (info->tty)
745 set_bit(TTY_IO_ERROR, &info->tty->flags);
747 info->flags &= ~S_INITIALIZED;
748 restore_flags(flags);
751 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
753 static int baud_table[] = {
754 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
755 9600, 19200, 38400, 57600, 115200, 0
759 * This routine is called to set the UART divisor registers to match
760 * the specified baud rate for a serial port.
762 static void change_speed(struct atmel_serial *info)
764 unsigned cflag;
765 int i;
767 if (!info->tty || !info->tty->termios)
768 return;
769 cflag = info->tty->termios->c_cflag;
771 // disable tx and rx
772 info->usart->cr = US_TXDIS | US_RXDIS;
774 /* First disable the interrupts */
775 tx_stop(info->usart);
776 rx_stop(info->usart);
778 /* set the baudrate */
779 i = cflag & CBAUD;
781 info->baud = baud_table[i];
782 uart_speed(info, cflag);
783 tx_start(info->usart, info->use_ints);
784 rx_start(info->usart, info->use_ints);
786 // enable tx and rx
787 info->usart->cr = US_TXEN | US_RXEN;
789 return;
792 static void start_rx(struct atmel_serial *info)
794 volatile struct atmel_usart_regs *uart = info->usart;
796 rx_stop(uart);
797 /* FIXME - rehnberg
798 if (info->rx_buf == rx_buf1) {
799 info->rx_buf = rx_buf2;
800 } else {
801 info->rx_buf = rx_buf1;
804 uart->rpr = (unsigned long) info->rx_buf;
805 uart->rcr = (unsigned long) RX_SERIAL_SIZE;
806 rx_start(uart, info->use_ints);
808 static void xmit_char(struct atmel_serial *info, char ch)
810 prompt0 = ch;
811 xmit_string(info, &prompt0, 1);
813 static void xmit_string(struct atmel_serial *info, char *p, int len)
815 info->usart->tcr = 0;
816 info->usart->tpr = (unsigned long) p;
817 info->usart->tcr = (unsigned long) len;
818 tx_start(info->usart, info->use_ints);
822 * atmel_console_print is registered for printk.
824 int atmel_console_initialized;
826 static void init_console(struct atmel_serial *info)
828 memset(info, 0, sizeof(struct atmel_serial));
830 #ifdef CONFIG_SWAP_ATMEL_PORTS
831 info->usart = (volatile struct atmel_usart_regs *) AT91_USART1_BASE;
832 info->irqmask = AIC_URT1;
833 info->irq = IRQ_USART1;
834 #else
835 info->usart = (volatile struct atmel_usart_regs *) AT91_USART0_BASE;
836 info->irqmask = 1<<IRQ_USART0;
837 info->irq = IRQ_USART0;
838 #endif
839 info->tty = 0;
840 info->port = 0;
841 info->use_ints = 0;
842 info->cts_state = 1;
843 info->is_cons = 1;
844 atmel_console_initialized = 1;
848 void console_print_atmel(const char *p)
850 char c;
851 struct atmel_serial *info;
853 #ifdef CONFIG_SWAP_ATMEL_PORTS
854 info = &atmel_info[1];
855 #else
856 info = &atmel_info[0];
857 #endif
859 if (!atmel_console_initialized) {
860 init_console(info);
861 uart_init(info);
862 info->baud = 9600;
863 tx_stop(info->usart);
864 rx_stop(info->usart);
865 uart_speed(info, 0xffff);
866 tx_start(info->usart, info->use_ints);
867 rx_start(info->usart, info->use_ints);
870 while ((c = *(p++)) != 0) {
871 if (c == '\n')
872 rs_put_char(info, '\r');
873 rs_put_char(info, c);
876 /* Comment this if you want to have a strict interrupt-driven output */
877 #if 0
878 if (!info->use_ints)
879 rs_fair_output(info);
880 #endif
882 return;
885 static void rs_set_ldisc(struct tty_struct *tty)
887 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
889 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
890 return;
892 info->is_cons = (tty->termios->c_line == N_TTY);
894 printk("ttyS%d console mode %s\n", info->line,
895 info->is_cons ? "on" : "off");
898 static void rs_flush_chars(struct tty_struct *tty)
900 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
901 unsigned long flags;
903 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
904 return;
905 if (!info->use_ints) {
906 for (;;) {
907 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
908 !info->xmit_buf) return;
910 /* Enable transmitter */
911 save_flags(flags);
912 cli();
913 tx_start(info->usart, info->use_ints);
915 } else {
916 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
917 !info->xmit_buf) return;
919 /* Enable transmitter */
920 save_flags(flags);
921 cli();
922 tx_start(info->usart, info->use_ints);
925 if (!info->use_ints)
926 wait_EOT(info->usart);
927 /* Send char */
928 xmit_char(info, info->xmit_buf[info->xmit_tail++]);
929 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
930 info->xmit_cnt--;
932 restore_flags(flags);
935 extern void console_printn(const char *b, int count);
937 static int rs_write(struct tty_struct *tty, int from_user,
938 const unsigned char *buf, int count)
940 int c, total = 0;
941 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
942 unsigned long flags;
944 if (serial_paranoia_check(info, tty->name, "rs_write"))
945 return 0;
947 if (!tty || !info->xmit_buf)
948 return 0;
950 save_flags(flags);
951 while (1) {
952 cli();
953 c = MIN(count, MIN(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
954 SERIAL_XMIT_SIZE - info->xmit_head));
955 if (c <= 0)
956 break;
958 if (from_user) {
959 down(&tmp_buf_sem);
960 copy_from_user(tmp_buf, buf, c);
961 memcpy(info->xmit_buf + info->xmit_head, tmp_buf, c);
962 up(&tmp_buf_sem);
963 } else {
964 memcpy(info->xmit_buf + info->xmit_head, buf, c);
966 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE - 1);
967 info->xmit_cnt += c;
968 restore_flags(flags);
969 buf += c;
970 count -= c;
971 total += c;
974 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
975 /* Enable transmitter */
977 cli();
978 /*printk("Enabling transmitter\n"); */
980 if (!info->use_ints) {
981 while (info->xmit_cnt) {
982 wait_EOT(info->usart);
983 /* Send char */
984 xmit_char(info, info->xmit_buf[info->xmit_tail++]);
985 wait_EOT(info->usart);
986 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
987 info->xmit_cnt--;
989 } else {
990 if (info->xmit_cnt) {
991 /* Send char */
992 wait_EOT(info->usart);
993 if (info->xmit_tail + info->xmit_cnt < SERIAL_XMIT_SIZE) {
994 xmit_string(info, info->xmit_buf + info->xmit_tail,
995 info->xmit_cnt);
996 info->xmit_tail =
997 (info->xmit_tail +
998 info->xmit_cnt) & (SERIAL_XMIT_SIZE - 1);
999 info->xmit_cnt = 0;
1000 } else {
1001 coucou2();
1002 xmit_string(info, info->xmit_buf + info->xmit_tail,
1003 SERIAL_XMIT_SIZE - info->xmit_tail);
1004 //xmit_string(info, info->xmit_buf, info->xmit_tail + info->xmit_cnt - SERIAL_XMIT_SIZE);
1005 info->xmit_cnt =
1006 info->xmit_cnt - (SERIAL_XMIT_SIZE - info->xmit_tail);
1007 info->xmit_tail = 0;
1011 } else {
1012 /*printk("Skipping transmit\n"); */
1015 #if 0
1016 printk("Enabling stuff anyhow\n");
1017 tx_start(0);
1019 if (SCC_EOT(0, 0)) {
1020 printk("TX FIFO empty.\n");
1021 /* Send char */
1022 atmel_xmit_char(info->usart, info->xmit_buf[info->xmit_tail++]);
1023 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1024 info->xmit_cnt--;
1026 #endif
1028 restore_flags(flags);
1029 return total;
1032 static int rs_write_room(struct tty_struct *tty)
1034 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1035 int ret;
1037 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1038 return 0;
1039 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1040 if (ret < 0)
1041 ret = 0;
1042 return ret;
1045 static int rs_chars_in_buffer(struct tty_struct *tty)
1047 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1049 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1050 return 0;
1051 return info->xmit_cnt;
1054 static void rs_flush_buffer(struct tty_struct *tty)
1056 unsigned long flags;
1057 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1059 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1060 return;
1061 save_flags(flags);
1062 cli();
1063 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1064 restore_flags(flags);
1065 wake_up_interruptible(&tty->write_wait);
1066 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1067 tty->ldisc.write_wakeup) (tty->ldisc.write_wakeup) (tty);
1071 * ------------------------------------------------------------
1072 * rs_throttle()
1074 * This routine is called by the upper-layer tty layer to signal that
1075 * incoming characters should be throttled.
1076 * ------------------------------------------------------------
1078 static void rs_throttle(struct tty_struct *tty)
1080 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1082 #ifdef SERIAL_DEBUG_THROTTLE
1083 char buf[64];
1085 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1086 tty->ldisc.chars_in_buffer(tty));
1087 #endif
1089 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1090 return;
1092 if (I_IXOFF(tty))
1093 info->x_char = STOP_CHAR(tty);
1095 /* Turn off RTS line (do this atomic) */
1098 static void rs_unthrottle(struct tty_struct *tty)
1100 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1102 #ifdef SERIAL_DEBUG_THROTTLE
1103 char buf[64];
1105 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1106 tty->ldisc.chars_in_buffer(tty));
1107 #endif
1109 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1110 return;
1112 if (I_IXOFF(tty)) {
1113 if (info->x_char)
1114 info->x_char = 0;
1115 else
1116 info->x_char = START_CHAR(tty);
1119 /* Assert RTS line (do this atomic) */
1123 * ------------------------------------------------------------
1124 * rs_ioctl() and friends
1125 * ------------------------------------------------------------
1128 static int get_serial_info(struct atmel_serial *info,
1129 struct serial_struct *retinfo)
1131 struct serial_struct tmp;
1133 if (!retinfo)
1134 return -EFAULT;
1135 memset(&tmp, 0, sizeof(tmp));
1136 tmp.type = info->type;
1137 tmp.line = info->line;
1138 tmp.port = info->port;
1139 tmp.irq = info->irq;
1140 tmp.flags = info->flags;
1141 tmp.baud_base = info->baud_base;
1142 tmp.close_delay = info->close_delay;
1143 tmp.closing_wait = info->closing_wait;
1144 tmp.custom_divisor = info->custom_divisor;
1145 copy_to_user(retinfo, &tmp, sizeof(*retinfo));
1146 return 0;
1149 static int set_serial_info(struct atmel_serial *info,
1150 struct serial_struct *new_info)
1152 struct serial_struct new_serial;
1153 struct atmel_serial old_info;
1154 int retval = 0;
1156 if (!new_info)
1157 return -EFAULT;
1158 copy_from_user(&new_serial, new_info, sizeof(new_serial));
1159 old_info = *info;
1161 if (!capable(CAP_SYS_ADMIN)) {
1162 if ((new_serial.baud_base != info->baud_base) ||
1163 (new_serial.type != info->type) ||
1164 (new_serial.close_delay != info->close_delay) ||
1165 ((new_serial.flags & ~S_USR_MASK) !=
1166 (info->flags & ~S_USR_MASK)))
1167 return -EPERM;
1168 info->flags = ((info->flags & ~S_USR_MASK) |
1169 (new_serial.flags & S_USR_MASK));
1170 info->custom_divisor = new_serial.custom_divisor;
1171 goto check_and_exit;
1174 if (info->count > 1)
1175 return -EBUSY;
1178 * OK, past this point, all the error checking has been done.
1179 * At this point, we start making changes.....
1182 info->baud_base = new_serial.baud_base;
1183 info->flags = ((info->flags & ~S_FLAGS) |
1184 (new_serial.flags & S_FLAGS));
1185 info->type = new_serial.type;
1186 info->close_delay = new_serial.close_delay;
1187 info->closing_wait = new_serial.closing_wait;
1189 check_and_exit:
1190 //retval = startup(info);
1191 change_speed(info);
1192 retval = 0;
1193 return retval;
1197 * get_lsr_info - get line status register info
1199 * Purpose: Let user call ioctl() to get info when the UART physically
1200 * is emptied. On bus types like RS485, the transmitter must
1201 * release the bus after transmitting. This must be done when
1202 * the transmit shift register is empty, not be done when the
1203 * transmit holding register is empty. This functionality
1204 * allows an RS485 driver to be written in user space.
1206 static int get_lsr_info(struct atmel_serial *info, unsigned int *value)
1208 unsigned char status;
1210 cli();
1211 status = info->usart->csr;
1212 status &= US_TXEMPTY;
1213 sti();
1214 put_user(status, value);
1215 return 0;
1219 * This routine sends a break character out the serial port.
1221 static void send_break(struct atmel_serial *info, int duration)
1223 unsigned long flags;
1224 if (!info->port) return;
1226 current->state = TASK_INTERRUPTIBLE;
1227 save_flags(flags); cli();
1228 info->usart->cr = US_STTBRK;
1229 if (!info->use_ints) {
1230 while (US_TXRDY != (info->usart->csr & US_TXRDY)) {
1231 ; // this takes max 2ms at 9600
1233 info->usart->cr = US_STPBRK;
1235 restore_flags(flags);
1238 static int rs_ioctl(struct tty_struct *tty, struct file *file,
1239 unsigned int cmd, unsigned long arg)
1241 int error;
1242 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1243 int retval;
1245 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1246 return -ENODEV;
1248 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1249 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1250 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1251 if (tty->flags & (1 << TTY_IO_ERROR))
1252 return -EIO;
1255 switch (cmd) {
1256 case TCSBRK: /* SVID version: non-zero arg --> no break */
1257 retval = tty_check_change(tty);
1258 if (retval)
1259 return retval;
1260 tty_wait_until_sent(tty, 0);
1261 if (!arg)
1262 send_break(info, HZ / 4); /* 1/4 second */
1263 return 0;
1264 case TCSBRKP: /* support for POSIX tcsendbreak() */
1265 retval = tty_check_change(tty);
1266 if (retval)
1267 return retval;
1268 tty_wait_until_sent(tty, 0);
1269 send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1270 return 0;
1271 case TIOCGSOFTCAR:
1272 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(long));
1273 if (error)
1274 return error;
1275 put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1276 return 0;
1277 case TIOCSSOFTCAR:
1278 arg = get_user(arg,(unsigned long *) arg);
1279 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1280 return 0;
1281 case TIOCGSERIAL:
1282 error = verify_area(VERIFY_WRITE, (void *) arg, sizeof(struct serial_struct));
1283 if (error)
1284 return error;
1285 return get_serial_info(info, (struct serial_struct *) arg);
1286 case TIOCSSERIAL:
1287 return set_serial_info(info, (struct serial_struct *) arg);
1288 case TIOCSERGETLSR: /* Get line status register */
1289 error = verify_area(VERIFY_WRITE, (void *) arg,
1290 sizeof(unsigned int));
1291 if (error)
1292 return error;
1293 else
1294 return get_lsr_info(info, (unsigned int *) arg);
1296 case TIOCSERGSTRUCT:
1297 error = verify_area(VERIFY_WRITE, (void *) arg,
1298 sizeof(struct atmel_serial));
1299 if (error)
1300 return error;
1301 copy_to_user((struct atmel_serial *) arg, info,
1302 sizeof(struct atmel_serial));
1303 return 0;
1305 case TCSETS:
1306 handle_termios_tcsets((struct termios *)arg, info);
1307 // return set_serial_info(info, (struct serial_struct *) arg);
1308 break;
1309 default:
1310 return -ENOIOCTLCMD;
1312 return 0;
1315 static void handle_termios_tcsets(struct termios * ptermios, struct atmel_serial * pinfo )
1318 * hmmmm....
1320 if (pinfo->tty->termios->c_cflag != ptermios->c_cflag)
1321 pinfo->tty->termios->c_cflag = ptermios->c_cflag;
1322 change_speed(pinfo);
1325 static void rs_set_termios(struct tty_struct *tty,
1326 struct termios *old_termios)
1328 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1330 if (tty->termios->c_cflag == old_termios->c_cflag)
1331 return;
1333 change_speed(info);
1335 if ((old_termios->c_cflag & CRTSCTS) &&
1336 !(tty->termios->c_cflag & CRTSCTS)) {
1337 tty->hw_stopped = 0;
1338 rs_start(tty);
1344 * ------------------------------------------------------------
1345 * rs_close()
1347 * This routine is called when the serial port gets closed. First, we
1348 * wait for the last remaining data to be sent. Then, we unlink its
1349 * S structure from the interrupt chain if necessary, and we free
1350 * that IRQ if nothing is left in the chain.
1351 * ------------------------------------------------------------
1353 static void rs_close(struct tty_struct *tty, struct file *filp)
1355 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1356 unsigned long flags;
1358 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1359 return;
1361 save_flags(flags);
1362 cli();
1364 if (tty_hung_up_p(filp)) {
1365 restore_flags(flags);
1366 return;
1368 #ifdef SERIAL_DEBUG_OPEN
1369 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1370 #endif
1371 if ((tty->count == 1) && (info->count != 1)) {
1373 * Uh, oh. tty->count is 1, which means that the tty
1374 * structure will be freed. Info->count should always
1375 * be one in these conditions. If it's greater than
1376 * one, we've got real problems, since it means the
1377 * serial port won't be shutdown.
1379 printk("rs_close: bad serial port count; tty->count is 1, "
1380 "info->count is %d\n", info->count);
1381 info->count = 1;
1383 if (--info->count < 0) {
1384 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1385 info->line, info->count);
1386 info->count = 0;
1388 if (info->count) {
1389 restore_flags(flags);
1390 return;
1392 // closing port so disable interrupts
1393 set_ints_mode(0, info);
1395 info->flags |= S_CLOSING;
1397 * Now we wait for the transmit buffer to clear; and we notify
1398 * the line discipline to only process XON/XOFF characters.
1400 tty->closing = 1;
1401 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1402 tty_wait_until_sent(tty, info->closing_wait);
1404 * At this point we stop accepting input. To do this, we
1405 * disable the receive line status interrupts, and tell the
1406 * interrupt driver to stop checking the data ready bit in the
1407 * line status register.
1410 shutdown(info);
1411 if (tty->driver->flush_buffer)
1412 tty->driver->flush_buffer(tty);
1413 if (tty->ldisc.flush_buffer)
1414 tty->ldisc.flush_buffer(tty);
1415 tty->closing = 0;
1416 info->event = 0;
1417 info->tty = 0;
1418 #warning "This is not and has never been valid so fix it"
1419 #if 0
1420 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1421 if (tty->ldisc.close)
1422 (tty->ldisc.close) (tty);
1423 tty->ldisc = ldiscs[N_TTY];
1424 tty->termios->c_line = N_TTY;
1425 if (tty->ldisc.open)
1426 (tty->ldisc.open) (tty);
1428 #endif
1429 if (info->blocked_open) {
1430 if (info->close_delay) {
1431 current->state = TASK_INTERRUPTIBLE;
1432 schedule_timeout(info->close_delay);
1434 wake_up_interruptible(&info->open_wait);
1436 info->flags &= ~(S_NORMAL_ACTIVE | S_CALLOUT_ACTIVE | S_CLOSING);
1437 wake_up_interruptible(&info->close_wait);
1438 restore_flags(flags);
1442 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1444 static void rs_hangup(struct tty_struct *tty)
1446 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1448 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1449 return;
1451 rs_flush_buffer(tty);
1452 shutdown(info);
1453 info->event = 0;
1454 info->count = 0;
1455 info->flags &= ~S_NORMAL_ACTIVE;
1456 info->tty = 0;
1457 wake_up_interruptible(&info->open_wait);
1461 * ------------------------------------------------------------
1462 * rs_open() and friends
1463 * ------------------------------------------------------------
1465 static int block_til_ready(struct tty_struct *tty, struct file *filp,
1466 struct atmel_serial *info)
1468 DECLARE_WAITQUEUE(wait, current);
1469 int retval;
1470 int do_clocal = 0;
1473 * If the device is in the middle of being closed, then block
1474 * until it's done, and then try again.
1476 if (info->flags & S_CLOSING) {
1477 interruptible_sleep_on(&info->close_wait);
1478 #ifdef SERIAL_DO_RESTART
1479 if (info->flags & S_HUP_NOTIFY)
1480 return -EAGAIN;
1481 else
1482 return -ERESTARTSYS;
1483 #else
1484 return -EAGAIN;
1485 #endif
1489 * If non-blocking mode is set, or the port is not enabled,
1490 * then make the check up front and then exit.
1492 if ((filp->f_flags & O_NONBLOCK) ||
1493 (tty->flags & (1 << TTY_IO_ERROR))) {
1494 info->flags |= S_NORMAL_ACTIVE;
1495 return 0;
1498 if (tty->termios->c_cflag & CLOCAL)
1499 do_clocal = 1;
1502 * Block waiting for the carrier detect and the line to become
1503 * free (i.e., not in use by the callout). While we are in
1504 * this loop, info->count is dropped by one, so that
1505 * rs_close() knows when to free things. We restore it upon
1506 * exit, either normal or abnormal.
1508 retval = 0;
1509 add_wait_queue(&info->open_wait, &wait);
1510 #ifdef SERIAL_DEBUG_OPEN
1511 printk("block_til_ready before block: ttyS%d, count = %d\n",
1512 info->line, info->count);
1513 #endif
1514 info->count--;
1515 info->blocked_open++;
1516 while (1) {
1517 #ifdef US_RTS
1518 save_flags(flags);
1519 cli();
1520 atmel_rtsdtr(info, 1);
1521 restore_flags(flags);
1522 #endif
1523 current->state = TASK_INTERRUPTIBLE;
1524 if (tty_hung_up_p(filp) ||
1525 !(info->flags & S_INITIALIZED)) {
1526 #ifdef SERIAL_DO_RESTART
1527 if (info->flags & S_HUP_NOTIFY)
1528 retval = -EAGAIN;
1529 else
1530 retval = -ERESTARTSYS;
1531 #else
1532 retval = -EAGAIN;
1533 #endif
1534 break;
1536 if (!(info->flags & S_CLOSING) && do_clocal)
1537 break;
1538 if (signal_pending(current)) {
1539 retval = -ERESTARTSYS;
1540 break;
1542 #ifdef SERIAL_DEBUG_OPEN
1543 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1544 info->line, info->count);
1545 #endif
1546 schedule();
1548 current->state = TASK_RUNNING;
1549 remove_wait_queue(&info->open_wait, &wait);
1550 if (!tty_hung_up_p(filp))
1551 info->count++;
1552 info->blocked_open--;
1553 #ifdef SERIAL_DEBUG_OPEN
1554 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1555 info->line, info->count);
1556 #endif
1557 if (retval)
1558 return retval;
1559 info->flags |= S_NORMAL_ACTIVE;
1560 if (!info->use_ints) {
1561 serialpoll.data = (void *) info;
1562 schedule_work(&serialpoll);
1564 return 0;
1568 * This routine is called whenever a serial port is opened. It
1569 * enables interrupts for a serial port, linking in its S structure into
1570 * the IRQ chain. It also performs the serial-specific
1571 * initialization for the tty structure.
1573 int rs_open(struct tty_struct *tty, struct file *filp)
1575 struct atmel_serial *info;
1576 int retval, line;
1578 line = tty->index;
1580 // check if line is sane
1581 if (line < 0 || line >= AT91_USART_CNT)
1582 return -ENODEV;
1584 info = &atmel_info[line];
1585 if (serial_paranoia_check(info, tty->name, "rs_open"))
1586 return -ENODEV;
1588 info->count++;
1589 tty->driver_data = info;
1590 info->tty = tty;
1593 * Start up serial port
1595 set_ints_mode(1, info);
1597 retval = startup(info);
1598 if (retval)
1599 return retval;
1601 return block_til_ready(tty, filp, info);
1605 static struct irqaction irq_usart0 =
1606 { rs_interrupta, 0, 0, "usart0", NULL, NULL };
1607 static struct irqaction irq_usart1 =
1608 { rs_interruptb, 0, 0, "usart1", NULL, NULL };
1610 static void interrupts_init(void)
1612 setup_irq(IRQ_USART0, &irq_usart0);
1613 setup_irq(IRQ_USART1, &irq_usart1);
1616 static void show_serial_version(void)
1618 printk("Atmel USART driver version 0.99\n");
1621 static struct tty_operations rs_ops = {
1622 .open = rs_open,
1623 .close = rs_close,
1624 .write = rs_write,
1625 .flush_chars = rs_flush_chars,
1626 .write_room = rs_write_room,
1627 .chars_in_buffer = rs_chars_in_buffer,
1628 .flush_buffer = rs_flush_buffer,
1629 .ioctl = rs_ioctl,
1630 .throttle = rs_throttle,
1631 .unthrottle = rs_unthrottle,
1632 .set_termios = rs_set_termios,
1633 .stop = rs_stop,
1634 .start = rs_start,
1635 .hangup = rs_hangup,
1636 .set_ldisc = rs_set_ldisc,
1639 /* rs_init inits the driver */
1640 static int __init
1641 rs_atmel_init(void)
1643 int flags, i;
1644 struct atmel_serial *info;
1646 /* initialise PIO for serial port */
1647 HW_AT91_USART_INIT
1649 serial_driver = alloc_tty_driver(2);
1650 if (!serial_driver)
1651 return -ENOMEM;
1653 // FIXME - do this right
1654 rx_buf_table[0] = rx_buf1;
1655 rx_buf_table[1] = rx_buf2;
1657 show_serial_version();
1659 /* Initialize the tty_driver structure */
1661 // set the tty_struct pointers to NULL to let the layer
1662 // above allocate the structs.
1663 for (i=0; i < AT91_USART_CNT; i++)
1664 serial_table[i] = NULL;
1666 serial_driver->name = "ttyS";
1667 serial_driver->major = TTY_MAJOR;
1668 serial_driver->minor_start = 64;
1669 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1670 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1671 serial_driver->init_termios = tty_std_termios;
1672 serial_driver->init_termios.c_cflag =
1673 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1674 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1675 tty_set_operations(serial_driver, &rs_ops);
1677 if (tty_register_driver(serial_driver)) {
1678 put_tty_driver(serial_driver);
1679 printk(KERN_ERR "Couldn't register serial driver\n");
1680 return -ENOMEM;
1683 save_flags(flags); cli();
1684 for (i = 0; i < 2; i++) {
1685 info = &atmel_info[i];
1686 info->magic = SERIAL_MAGIC;
1687 info->usart = usarts[i];
1688 info->tty = 0;
1689 info->irqmask = (i) ? (1<<IRQ_USART1) : (1<<IRQ_USART0);
1690 info->irq = (i) ? IRQ_USART1 : IRQ_USART0;
1691 #ifdef CONFIG_SWAP_ATMEL_PORTS
1692 info->port = (i) ? 2 : 1;
1693 info->line = !i;
1694 #ifdef CONFIG_ATMEL_CONSOLE
1695 info->is_cons = i;
1696 #else
1697 info->is_cons = 0;
1698 #endif
1699 #else
1700 info->port = (i) ? 1 : 2;
1701 info->line = i;
1702 #ifdef CONFIG_ATMEL_CONSOLE
1703 info->is_cons = !i;
1704 #else
1705 info->is_cons = 0;
1706 #endif
1707 #endif
1708 #ifdef CONFIG_CONSOLE_ON_SC28L91
1709 info->line += 1;
1710 #endif
1711 set_ints_mode(0, info);
1712 info->custom_divisor = 16;
1713 info->close_delay = 50;
1714 info->closing_wait = 3000;
1715 info->cts_state = 1;
1716 info->x_char = 0;
1717 info->event = 0;
1718 info->count = 0;
1719 info->blocked_open = 0;
1720 INIT_WORK(&info->tqueue, do_softint, info);
1721 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1722 init_waitqueue_head(&info->open_wait);
1723 init_waitqueue_head(&info->close_wait);
1724 info->rx_buf = rx_buf_table[i];
1726 printk("%s%d at 0x%p (irq = %d)", serial_driver->name, info->line,
1727 info->usart, info->irq);
1728 printk(" is a builtin Atmel APB USART\n");
1731 // FIXME
1732 info->usart->cr = 0x1ac; // reset, disable
1733 info->usart->idr = 0xffffffff; // disable all interrupts
1734 info->usart->tcr = 0; // stop transmit
1735 info->usart->rcr = 0; // stop receive
1737 interrupts_init();
1739 restore_flags(flags);
1740 // hack to do polling
1741 serialpoll.func = serpoll;
1742 serialpoll.data = 0;
1744 return 0;
1747 module_init(rs_atmel_init);
1749 #if 0
1751 * register_serial and unregister_serial allows for serial ports to be
1752 * configured at run-time, to support PCMCIA modems.
1754 /* SPARC: Unused at this time, just here to make things link. */
1755 static int register_serial(struct serial_struct *req)
1757 return -1;
1760 static void unregister_serial(int line)
1762 return;
1765 static void dbg_putc(int ch)
1767 static char tmp[2];
1768 #define US_TPR (0x38) /* Transmit Pointer Register */
1769 #define US_TCR (0x3C) /* Transmit Counter Register */
1771 tmp[0] = ch;
1773 outl_t((unsigned long) tmp, (USART0_BASE + US_TPR) );
1774 outl_t(1, (USART0_BASE + US_TCR) );
1776 while (inl_t((USART0_BASE + US_TCR) )) {
1780 static void dbg_print(const char *str)
1782 const char *p;
1784 for (p = str; *p; p++) {
1785 if (*p == '\n') {
1786 dbg_putc('\r');
1788 dbg_putc(*p);
1792 static void dbg_printk(const char *fmt, ...)
1794 char tmp[256];
1795 va_list args;
1797 va_start(args, fmt);
1798 vsprintf(tmp, fmt, args);
1799 va_end(args);
1800 dbg_print(tmp);
1803 static void rs_atmel_print(const char *str)
1805 dbg_printk(str);
1808 static void dump_a(unsigned long a, unsigned int s)
1810 unsigned long q;
1812 for (q = 0; q < s; q++) {
1813 if (q % 16 == 0) {
1814 dbg_printk("%08X: ", q + a);
1816 if (q % 16 == 7) {
1817 dbg_printk("%02X-", *(unsigned char *) (q + a));
1818 } else {
1819 dbg_printk("%02X ", *(unsigned char *) (q + a));
1821 if (q % 16 == 15) {
1822 dbg_printk(" :\n");
1825 if (q % 16) {
1826 dbg_printk(" :\n");
1829 #endif
1831 int atmel_console_setup(struct console *cp, char *arg)
1833 if (!cp)
1834 return(-1);
1835 HW_AT91_USART_INIT
1836 return 0;
1839 static struct tty_driver *atmel_console_device(struct console *c, int *index)
1841 *index = c->index;
1842 return serial_driver;
1845 void atmel_console_write (struct console *co, const char *str,
1846 unsigned int count)
1848 struct atmel_serial *info;
1850 #ifdef CONFIG_SWAP_ATMEL_PORTS
1851 info = &atmel_info[1];
1852 #else
1853 info = &atmel_info[0];
1854 #endif
1856 if (!atmel_console_initialized) {
1857 init_console(info);
1858 uart_init(info);
1859 info->baud = 9600;
1860 tx_stop(info->usart);
1861 rx_stop(info->usart);
1862 uart_speed(info, 0xffff);
1863 tx_start(info->usart, info->use_ints);
1864 rx_start(info->usart, info->use_ints);
1867 while (count--) {
1868 if (*str == '\n')
1869 rs_put_char(info,'\r');
1870 rs_put_char(info, *str++ );
1874 static struct console atmel_driver = {
1875 name: "ttyS",
1876 write: atmel_console_write,
1877 device: atmel_console_device,
1878 setup: atmel_console_setup,
1879 flags: CON_PRINTBUFFER,
1880 index: -1,
1884 static int __init atmel_console_init(void)
1886 register_console(&atmel_driver);
1887 return 0;
1890 console_initcall(atmel_console_init);