MOXA linux-2.6.x / linux-2.6.19-uc1 from UC-7110-LX-BOOTLOADER-1.9_VERSION-4.2.tgz
[linux-2.6.19-moxart.git] / drivers / serial / serial_atmel.c
blobbc9fa653ac0323a4e86e87066077e829b8a95b30
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/version.h>
20 #include <linux/types.h>
21 #include <linux/serial.h>
22 #include <linux/errno.h>
23 #include <linux/signal.h>
24 #include <linux/sched.h>
25 #include <linux/timer.h>
26 #include <linux/interrupt.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/major.h>
30 #include <linux/string.h>
31 #include <linux/fcntl.h>
32 #include <linux/mm.h>
33 #include <linux/kernel.h>
34 #include <linux/init.h>
35 #include <linux/console.h>
37 #include <asm/io.h>
38 #include <asm/irq.h>
39 #include <asm/arch/irq.h>
40 #include <asm/system.h>
41 #include <asm/segment.h>
42 #include <asm/bitops.h>
43 #include <asm/delay.h>
44 #include <asm/uaccess.h>
46 #include "serial_atmel.h"
48 #define USE_INTS 1
50 static volatile struct atmel_usart_regs *usarts[AT91_USART_CNT] = {
51 (volatile struct atmel_usart_regs *) AT91_USART0_BASE,
52 (volatile struct atmel_usart_regs *) AT91_USART1_BASE
55 #define SERIAL_XMIT_SIZE PAGE_SIZE
56 #define RX_SERIAL_SIZE 256
58 static struct atmel_serial atmel_info[AT91_USART_CNT];
59 static struct tty_struct *serial_table[AT91_USART_CNT];
60 struct atmel_serial *atmel_consinfo = 0;
62 #define UART_CLOCK (ARM_CLK/16)
64 static struct work_struct serialpoll;
66 #ifdef CONFIG_CONSOLE
67 extern wait_queue_head_t keypress_wait;
68 #endif
70 struct tty_driver *serial_driver;
72 /* serial subtype definitions */
73 #define SERIAL_TYPE_NORMAL 1
75 /* number of characters left in xmit buffer before we ask for more */
76 #define WAKEUP_CHARS 256
78 /* Debugging... DEBUG_INTR is bad to use when one of the zs
79 * lines is your console ;(
81 #undef SERIAL_DEBUG_INTR
82 #undef SERIAL_DEBUG_OPEN
83 #undef SERIAL_DEBUG_FLOW
85 #define RS_ISR_PASS_LIMIT 256
87 #define _INLINE_ inline
89 static inline int serial_paranoia_check(struct atmel_serial *info,
90 char *name, const char *routine)
92 #ifdef SERIAL_PARANOIA_CHECK
93 static const char *badmagic =
94 "Warning: bad magic number for serial struct %s in %s\n";
95 static const char *badinfo =
96 "Warning: null atmel_serial struct for %s in %s\n";
98 if (!info) {
99 printk(badinfo, name, routine);
100 return 1;
102 if (info->magic != SERIAL_MAGIC) {
103 printk(badmagic, name, routine);
104 return 1;
106 #endif
107 return 0;
110 static unsigned char * rx_buf_table[AT91_USART_CNT];
112 static unsigned char rx_buf1[RX_SERIAL_SIZE];
113 static unsigned char rx_buf2[RX_SERIAL_SIZE];
115 /* Console hooks... */
117 static void serpoll(void *data);
119 static void change_speed(struct atmel_serial *info);
121 static char prompt0;
122 static void xmit_char(struct atmel_serial *info, char ch);
123 static void xmit_string(struct atmel_serial *info, char *p, int len);
124 static void start_rx(struct atmel_serial *info);
125 static void wait_EOT(volatile struct atmel_usart_regs *);
126 static void uart_init(struct atmel_serial *info);
127 static void uart_speed(struct atmel_serial *info, unsigned cflag);
129 static void tx_enable(volatile struct atmel_usart_regs *uart);
130 static void rx_enable(volatile struct atmel_usart_regs *uart);
131 static void tx_disable(volatile struct atmel_usart_regs *uart);
132 static void rx_disable(volatile struct atmel_usart_regs *uart);
133 static void tx_stop(volatile struct atmel_usart_regs *uart);
134 static void tx_start(volatile struct atmel_usart_regs *uart);
135 static void rx_stop(volatile struct atmel_usart_regs *uart);
136 static void rx_start(volatile struct atmel_usart_regs *uart, int ints);
137 static void set_ints_mode(int yes, struct atmel_serial *info);
138 static irqreturn_t rs_interrupt(struct atmel_serial *info);
139 extern void show_net_buffers(void);
140 extern void hard_reset_now(void);
141 static void handle_termios_tcsets(struct termios * ptermios, struct atmel_serial * ptty);
143 static int global;
145 static void coucou1(void)
147 global = 0;
150 static void coucou2(void)
152 global = 1;
154 static void _INLINE_ tx_enable(volatile struct atmel_usart_regs *uart)
156 uart->ier = US_TXEMPTY;
158 static void _INLINE_ rx_enable(volatile struct atmel_usart_regs *uart)
160 uart->ier = US_ENDRX | US_TIMEOUT;
162 static void _INLINE_ tx_disable(volatile struct atmel_usart_regs *uart)
164 uart->idr = US_TXEMPTY;
166 static void _INLINE_ rx_disable(volatile struct atmel_usart_regs *uart)
168 uart->idr = US_ENDRX | US_TIMEOUT;
170 static void _INLINE_ tx_stop(volatile struct atmel_usart_regs *uart)
172 tx_disable(uart);
173 uart->tcr = 0;
174 uart->cr = US_TXDIS;
176 static void _INLINE_ tx_start(volatile struct atmel_usart_regs *uart)
178 tx_enable(uart);
179 uart->cr = US_TXEN;
181 static void _INLINE_ rx_stop(volatile struct atmel_usart_regs *uart)
183 rx_disable(uart);
184 uart->rtor = 0;
185 // PSW fixes slew of ^@ chars on a TCSETA ioctl
186 //uart->rcr = 0;
187 uart->cr = US_RXDIS;
189 static void _INLINE_ rx_start(volatile struct atmel_usart_regs *uart, int ints)
191 uart->cr = US_RXEN | US_STTO;
192 uart->rtor = 20;
193 if (ints) {
194 rx_enable(uart);
197 static void _INLINE_ reset_status(volatile struct atmel_usart_regs *uart)
199 uart->cr = US_RSTSTA;
201 static void set_ints_mode(int yes, struct atmel_serial *info)
203 info->use_ints = yes;
204 // FIXME: check
205 #if 0
206 (yes) ? unmask_irq(info->irq) : mask_irq(info->irq);
207 #endif
210 #ifdef US_RTS
211 static void atmel_cts_off(struct atmel_serial *info)
213 volatile struct atmel_usart_regs *uart;
215 uart = info->usart;
216 uart->mc &= ~(unsigned long) US_RTS;
217 info->cts_state = 0;
219 static void atmel_cts_on(struct atmel_serial *info)
221 volatile struct atmel_usart_regs *uart;
223 uart = info->usart;
224 uart->mc |= US_RTS;
225 info->cts_state = 1;
227 /* Sets or clears DTR/RTS on the requested line */
228 static inline void atmel_rtsdtr(struct atmel_serial *ss, int set)
230 volatile struct atmel_usart_regs *uart;
232 uart = ss->usart;
233 if (set) {
234 uart->mc |= US_DTR | US_RTS;
235 } else {
236 uart->mc &= ~(unsigned long) (US_DTR | US_RTS);
238 return;
240 #endif /* US_RTS */
243 * ------------------------------------------------------------
244 * rs_stop() and rs_start()
246 * This routines are called before setting or resetting tty->stopped.
247 * They enable or disable transmitter interrupts, as necessary.
248 * ------------------------------------------------------------
250 static void rs_stop(struct tty_struct *tty)
252 struct atmel_serial *info = (struct atmel_serial *)tty->driver_data;
253 unsigned long flags;
255 if (serial_paranoia_check(info, tty->name, "rs_stop"))
256 return;
258 local_irq_save(flags);
259 tx_stop(info->usart);
260 rx_stop(info->usart);
261 local_irq_restore(flags);
264 static void rs_put_char(struct atmel_serial *info, char ch)
266 unsigned long flags;
268 local_irq_save(flags);
269 xmit_char(info, ch);
270 wait_EOT(info->usart);
271 local_irq_restore(flags);
274 static void rs_start(struct tty_struct *tty)
276 struct atmel_serial *info = (struct atmel_serial *)tty->driver_data;
277 unsigned long flags;
279 if (serial_paranoia_check(info, tty->name, "rs_start"))
280 return;
282 local_irq_save(flags);
283 tx_start(info->usart);
284 rx_start(info->usart, info->use_ints);
285 /* FIXME */
286 // start_rx(info);
287 local_irq_restore(flags);
290 /* Drop into either the boot monitor or kadb upon receiving a break
291 * from keyboard/console input.
293 static void batten_down_hatches(void)
295 /* Drop into the debugger */
298 static _INLINE_ void status_handle(struct atmel_serial *info, unsigned long status)
300 #if 0
301 if (status & DCD) {
302 if ((info->tty->termios->c_cflag & CRTSCTS) &&
303 ((info->curregs[3] & AUTO_ENAB) == 0)) {
304 info->curregs[3] |= AUTO_ENAB;
305 info->pendregs[3] |= AUTO_ENAB;
306 write_zsreg(info->atmel_channel, 3, info->curregs[3]);
308 } else {
309 if ((info->curregs[3] & AUTO_ENAB)) {
310 info->curregs[3] &= ~AUTO_ENAB;
311 info->pendregs[3] &= ~AUTO_ENAB;
312 write_zsreg(info->atmel_channel, 3, info->curregs[3]);
315 #endif
316 /* Whee, if this is console input and this is a
317 * 'break asserted' status change interrupt, call
318 * the boot prom.
320 if ((status & US_RXBRK) && info->break_abort)
321 batten_down_hatches();
323 /* XXX Whee, put in a buffer somewhere, the status information
324 * XXX whee whee whee... Where does the information go...
326 reset_status(info->usart);
327 return;
330 static _INLINE_ void receive_chars(struct atmel_serial *info, unsigned long status)
332 int count;
333 volatile struct atmel_usart_regs *uart = info->usart;
334 char *rxp, ch, flag;
335 struct tty_struct *tty = info->tty;
337 if (!(info->flags & S_INITIALIZED))
338 return;
339 count = RX_SERIAL_SIZE - uart->rcr;
340 // hack to receive chars by polling only BD fields
341 if (!count) {
342 return;
345 if (!tty)
346 goto clear_and_exit;
348 #if 0
349 if (tty->flip.count >= TTY_FLIPBUF_SIZE)
350 schedule_work(&tty->flip.work);
352 if ((count + tty->flip.count) >= TTY_FLIPBUF_SIZE) {
353 #ifdef US_RTS
354 atmel_cts_off(info);
355 #endif
356 serialpoll.data = (void *) info;
357 schedule_work(&serialpoll);
359 memset(tty->flip.flag_buf_ptr, 0, count);
360 memcpy(tty->flip.char_buf_ptr, info->rx_buf, count);
361 tty->flip.char_buf_ptr += count;
363 if (status & US_PARE)
364 *(tty->flip.flag_buf_ptr - 1) = TTY_PARITY;
365 else if (status & US_OVRE)
366 *(tty->flip.flag_buf_ptr - 1) = TTY_OVERRUN;
367 else if (status & US_FRAME)
368 *(tty->flip.flag_buf_ptr - 1) = TTY_FRAME;
370 tty->flip.count += count;
371 #endif
372 #if 1
373 rxp = info->rx_buf;
374 for (; (count > 0); count--) {
376 ch = *rxp++;
377 flag = TTY_NORMAL;
378 if (status & US_PARE)
379 flag = TTY_PARITY;
380 else if (status & US_OVRE)
381 flag = TTY_OVERRUN;
382 else if (status & US_FRAME)
383 flag = TTY_FRAME;
386 tty_insert_flip_char(tty, ch, flag);
388 #endif
390 tty_schedule_flip(tty);
392 clear_and_exit:
393 start_rx(info);
394 return;
397 static _INLINE_ void transmit_chars(struct atmel_serial *info)
399 if (info->x_char) {
400 /* Send next char */
401 xmit_char(info, info->x_char);
402 info->x_char = 0;
403 goto clear_and_return;
406 if ((info->xmit_cnt <= 0) || info->tty->stopped) {
407 /* That's peculiar... */
408 tx_stop(info->usart);
409 goto clear_and_return;
412 if (info->xmit_tail + info->xmit_cnt < SERIAL_XMIT_SIZE) {
413 xmit_string(info, info->xmit_buf + info->xmit_tail,
414 info->xmit_cnt);
415 info->xmit_tail =
416 (info->xmit_tail + info->xmit_cnt) & (SERIAL_XMIT_SIZE - 1);
417 info->xmit_cnt = 0;
418 } else {
419 coucou1();
420 xmit_string(info, info->xmit_buf + info->xmit_tail,
421 SERIAL_XMIT_SIZE - info->xmit_tail);
422 //xmit_string(info, info->xmit_buf, info->xmit_tail + info->xmit_cnt - SERIAL_XMIT_SIZE);
423 info->xmit_cnt =
424 info->xmit_cnt - (SERIAL_XMIT_SIZE - info->xmit_tail);
425 info->xmit_tail = 0;
428 if (info->xmit_cnt < WAKEUP_CHARS)
429 schedule_work(&info->tqueue);
431 if (info->xmit_cnt <= 0) {
432 //tx_stop(info->usart);
433 goto clear_and_return;
436 clear_and_return:
437 /* Clear interrupt (should be auto) */
438 return;
442 * This is the serial driver's generic interrupt routine
444 static irqreturn_t rs_interrupta(int irq, void *dev_id, struct pt_regs *regs)
446 return rs_interrupt(&atmel_info[0]);
448 static irqreturn_t rs_interruptb(int irq, void *dev_id, struct pt_regs *regs)
450 return rs_interrupt(&atmel_info[1]);
452 static irqreturn_t rs_interrupt(struct atmel_serial *info)
454 unsigned long status;
456 status = info->usart->csr;
457 if (status & (US_ENDRX | US_TIMEOUT)) {
458 receive_chars(info, status);
460 if (status & (US_TXEMPTY)) {
461 transmit_chars(info);
463 status_handle(info, status);
465 #ifdef US_RTS
466 if (!info->cts_state) {
467 if (info->tty->flip.count < TTY_FLIPBUF_SIZE - RX_SERIAL_SIZE) {
468 atmel_cts_on(info);
471 #endif
472 if (!info->use_ints) {
473 serialpoll.data = (void *) info;
474 schedule_work(&serialpoll);
476 return IRQ_HANDLED;
478 static void serpoll(void *data)
480 struct atmel_serial *info = data;
482 rs_interrupt(info);
486 * -------------------------------------------------------------------
487 * Here ends the serial interrupt routines.
488 * -------------------------------------------------------------------
492 static void do_softint(void *private_)
494 struct atmel_serial *info = (struct atmel_serial *) private_;
495 struct tty_struct *tty;
497 tty = info->tty;
498 if (!tty)
499 return;
500 #if 0 // FIXME - CHECK
501 if (clear_bit(RS_EVENT_WRITE_WAKEUP, &info->event)) {
502 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
503 tty->ldisc.write_wakeup) (tty->ldisc.write_wakeup) (tty);
504 wake_up_interruptible(&tty->write_wait);
506 #endif
510 * This routine is called from the scheduler tqueue when the interrupt
511 * routine has signalled that a hangup has occurred. The path of
512 * hangup processing is:
514 * serial interrupt routine -> (scheduler tqueue) ->
515 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
518 static void do_serial_hangup(void *private_)
520 struct atmel_serial *info = (struct atmel_serial *) private_;
521 struct tty_struct *tty;
523 tty = info->tty;
524 if (!tty)
525 return;
527 tty_hangup(tty);
532 * This subroutine is called when the RS_TIMER goes off. It is used
533 * by the serial driver to handle ports that do not have an interrupt
534 * (irq=0). This doesn't work at all for 16450's, as a sun has a Z8530.
536 #if 0
537 static void rs_timer(void)
539 panic("rs_timer called\n");
540 return;
542 #endif
544 static unsigned long calcCD(unsigned long br)
546 return (UART_CLOCK / br);
549 static void uart_init(struct atmel_serial *info)
551 volatile struct atmel_usart_regs *uart;
553 if (info) {
554 uart = info->usart;
555 } else {
556 uart = usarts[0];
559 /* Reset the USART */
560 uart->cr = US_TXDIS | US_RXDIS | US_RSTTX | US_RSTRX;
561 /* clear Rx receive and Tx sent counters */
562 uart->rcr = 0;
563 uart->tcr = 0;
565 /* Disable interrups till we say we want them */
566 tx_disable(info->usart);
567 rx_disable(info->usart);
569 /* Set the serial port into a safe sane state */
570 uart->mr = US_USCLKS(0) | US_CLK0 | US_CHMODE(0) | US_NBSTOP(0) |
571 US_PAR(4) | US_CHRL(3);
573 #ifndef FORCE_57600
574 uart->brgr = calcCD(9600);
575 #else
576 uart->brgr = calcCD(57600);
577 #endif
579 uart->rtor = 20; // timeout = value * 4 *bit period
580 uart->ttgr = 0; // no guard time
581 uart->rcr = 0;
582 uart->rpr = 0;
583 uart->tcr = 0;
584 uart->tpr = 0;
585 #ifdef US_RTS
586 uart->mc = 0;
587 #endif
590 /* It is the responsibilty of whoever calls this function to be sure
591 * that that have called
592 * tx_stop(uart); rx_stop(uart);
593 * before calling the function. Failure to do this will cause messy
594 * things to happen. You have been warned. */
595 static void uart_speed(struct atmel_serial *info, unsigned cflag)
597 unsigned baud = info->baud;
598 volatile struct atmel_usart_regs *uart = info->usart;
600 // disable tx and rx
601 uart->cr = US_TXDIS | US_RXDIS;
603 // disable interrupts
604 tx_disable(uart);
605 rx_disable(uart);
607 #ifndef FORCE_57600
608 uart->brgr = calcCD(baud);
609 #else
610 uart->brgr = calcCD(57600);
611 #endif
612 /* FIXME */
613 #if 0
614 /* probably not needed */
615 uart->US_RTOR = 20; // timeout = value * 4 *bit period
616 uart->US_TTGR = 0; // no guard time
617 uart->US_RPR = 0;
618 uart->US_RCR = 0;
619 uart->US_TPR = 0;
620 uart->US_TCR = 0;
621 #endif
624 /* FIXME */
625 #if 0
626 uart->mc = 0;
627 if (cflag != 0xffff) {
628 uart->mr = US_USCLKS(0) | US_CLK0 | US_CHMODE(0) | US_NBSTOP(0) |
629 US_PAR(0);
631 if ((cflag & CSIZE) == CS8)
632 uart->mr |= US_CHRL(3); // 8 bit char
633 else
634 uart->mr |= US_CHRL(2); // 7 bit char
636 if (cflag & CSTOPB)
637 uart->mr |= US_NBSTOP(2); // 2 stop bits
639 if (!(cflag & PARENB))
640 uart->mr |= US_PAR(4); // parity disabled
641 else if (cflag & PARODD)
642 uart->mr |= US_PAR(1); // odd parity
644 #endif
646 /* FIXME */
647 #if 0
648 // enable tx and rx
649 uart->cr = US_TXEN | US_RXEN;
651 // enable interrupts
652 tx_enable();
653 rx_enable();
654 #endif
655 tx_start(uart);
656 start_rx(info);
659 static void wait_EOT(volatile struct atmel_usart_regs *uart)
661 // make sure tx is enabled
662 uart->cr = US_TXEN;
664 // wait until all chars sent FIXME - is this sane ?
665 while (1) {
666 if (uart->csr & US_TXEMPTY)
667 break;
670 static int startup(struct atmel_serial *info)
672 unsigned long flags;
674 if (info->flags & S_INITIALIZED)
675 return 0;
677 if (!info->xmit_buf) {
678 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
679 if (!info->xmit_buf)
680 return -ENOMEM;
682 if (!info->rx_buf) {
683 //info->rx_buf = (unsigned char *) ))__get_free_page(GFP_KERNEL);
684 //info->rx_buf = rx_buf1;
685 if (!info->rx_buf)
686 return -ENOMEM;
688 local_irq_save(flags);
689 #ifdef SERIAL_DEBUG_OPEN
690 printk("starting up ttyS%d (irq %d)...\n", info->line, info->irq);
691 #endif
693 * Clear the FIFO buffers and disable them
694 * (they will be reenabled in change_speed())
697 if (info->tty)
698 clear_bit(TTY_IO_ERROR, &info->tty->flags);
699 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
702 * and set the speed of the serial port
705 uart_init(info);
706 //set_ints_mode(0, info);
707 change_speed(info);
708 info->flags |= S_INITIALIZED;
709 local_irq_restore(flags);
710 return 0;
714 * This routine will shutdown a serial port; interrupts are disabled, and
715 * DTR is dropped if the hangup on close termio flag is on.
717 static void shutdown(struct atmel_serial *info)
719 unsigned long flags;
721 tx_disable(info->usart);
722 rx_disable(info->usart);
723 rx_stop(info->usart); /* All off! */
724 if (!(info->flags & S_INITIALIZED))
725 return;
727 #ifdef SERIAL_DEBUG_OPEN
728 printk("Shutting down serial port %d (irq %d)....\n", info->line,
729 info->irq);
730 #endif
732 local_irq_save(flags);
734 if (info->xmit_buf) {
735 free_page((unsigned long) info->xmit_buf);
736 info->xmit_buf = 0;
739 if (info->tty)
740 set_bit(TTY_IO_ERROR, &info->tty->flags);
742 info->flags &= ~S_INITIALIZED;
743 local_irq_restore(flags);
746 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
748 static int baud_table[] = {
749 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
750 9600, 19200, 38400, 57600, 115200, 0
754 * This routine is called to set the UART divisor registers to match
755 * the specified baud rate for a serial port.
757 static void change_speed(struct atmel_serial *info)
759 unsigned cflag;
760 int i;
762 if (!info->tty || !info->tty->termios)
763 return;
764 cflag = info->tty->termios->c_cflag;
766 // disable tx and rx
767 info->usart->cr = US_TXDIS | US_RXDIS;
769 /* First disable the interrupts */
770 tx_stop(info->usart);
771 rx_stop(info->usart);
773 /* set the baudrate */
774 i = cflag & CBAUD;
776 info->baud = baud_table[i];
777 uart_speed(info, cflag);
778 tx_start(info->usart);
779 rx_start(info->usart, info->use_ints);
781 // enable tx and rx
782 info->usart->cr = US_TXEN | US_RXEN;
784 return;
787 static void start_rx(struct atmel_serial *info)
789 volatile struct atmel_usart_regs *uart = info->usart;
791 rx_stop(uart);
792 /* FIXME - rehnberg
793 if (info->rx_buf == rx_buf1) {
794 info->rx_buf = rx_buf2;
795 } else {
796 info->rx_buf = rx_buf1;
799 uart->rpr = (unsigned long) info->rx_buf;
800 uart->rcr = (unsigned long) RX_SERIAL_SIZE;
801 rx_start(uart, info->use_ints);
803 static void xmit_char(struct atmel_serial *info, char ch)
805 prompt0 = ch;
806 xmit_string(info, &prompt0, 1);
808 static void xmit_string(struct atmel_serial *info, char *p, int len)
810 info->usart->tcr = 0;
811 info->usart->tpr = (unsigned long) p;
812 info->usart->tcr = (unsigned long) len;
813 tx_start(info->usart);
817 * atmel_console_print is registered for printk.
819 int atmel_console_initialized;
821 static void init_console(struct atmel_serial *info)
823 memset(info, 0, sizeof(struct atmel_serial));
825 #ifdef CONFIG_SWAP_ATMEL_PORTS
826 info->usart = (volatile struct atmel_usart_regs *) AT91_USART1_BASE;
827 info->irqmask = AIC_URT1;
828 info->irq = IRQ_USART1;
829 #else
830 info->usart = (volatile struct atmel_usart_regs *) AT91_USART0_BASE;
831 info->irqmask = 1<<IRQ_USART0;
832 info->irq = IRQ_USART0;
833 #endif
834 info->tty = 0;
835 info->port = 0;
836 info->use_ints = 0;
837 info->cts_state = 1;
838 info->is_cons = 1;
839 atmel_console_initialized = 1;
843 void console_print_atmel(const char *p)
845 char c;
846 struct atmel_serial *info;
848 #ifdef CONFIG_SWAP_ATMEL_PORTS
849 info = &atmel_info[1];
850 #else
851 info = &atmel_info[0];
852 #endif
854 if (!atmel_console_initialized) {
855 init_console(info);
856 uart_init(info);
857 info->baud = 9600;
858 tx_stop(info->usart);
859 rx_stop(info->usart);
860 uart_speed(info, 0xffff);
861 tx_start(info->usart);
862 rx_start(info->usart, info->use_ints);
865 while ((c = *(p++)) != 0) {
866 if (c == '\n')
867 rs_put_char(info, '\r');
868 rs_put_char(info, c);
871 /* Comment this if you want to have a strict interrupt-driven output */
872 #if 0
873 if (!info->use_ints)
874 rs_fair_output(info);
875 #endif
877 return;
880 static void rs_set_ldisc(struct tty_struct *tty)
882 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
884 if (serial_paranoia_check(info, tty->name, "rs_set_ldisc"))
885 return;
887 info->is_cons = (tty->termios->c_line == N_TTY);
889 printk("ttyS%d console mode %s\n", info->line,
890 info->is_cons ? "on" : "off");
893 static void rs_flush_chars(struct tty_struct *tty)
895 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
896 unsigned long flags;
898 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
899 return;
900 if (!info->use_ints) {
901 for (;;) {
902 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
903 !info->xmit_buf) return;
905 /* Enable transmitter */
906 local_irq_save(flags);
907 tx_start(info->usart);
909 } else {
910 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
911 !info->xmit_buf) return;
913 /* Enable transmitter */
914 local_irq_save(flags);
915 tx_start(info->usart);
918 if (!info->use_ints)
919 wait_EOT(info->usart);
920 /* Send char */
921 xmit_char(info, info->xmit_buf[info->xmit_tail++]);
922 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
923 info->xmit_cnt--;
925 local_irq_restore(flags);
928 extern void console_printn(const char *b, int count);
930 static int rs_write(struct tty_struct *tty, const unsigned char *buf, int count)
932 int c, total = 0;
933 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
934 unsigned long flags;
936 if (serial_paranoia_check(info, tty->name, "rs_write"))
937 return 0;
939 if (!tty || !info->xmit_buf)
940 return 0;
942 local_save_flags(flags);
943 while (1) {
944 local_irq_disable();
945 c = min(count, (int) min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
946 SERIAL_XMIT_SIZE - info->xmit_head));
947 local_irq_restore(flags);
949 if (c <= 0)
950 break;
952 memcpy(info->xmit_buf + info->xmit_head, buf, c);
954 local_irq_disable();
955 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
956 info->xmit_cnt += c;
957 local_irq_restore(flags);
959 buf += c;
960 count -= c;
961 total += c;
964 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
965 /* Enable transmitter */
967 local_irq_disable();
968 /*printk("Enabling transmitter\n"); */
970 if (!info->use_ints) {
971 while (info->xmit_cnt) {
972 wait_EOT(info->usart);
973 /* Send char */
974 xmit_char(info, info->xmit_buf[info->xmit_tail++]);
975 wait_EOT(info->usart);
976 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
977 info->xmit_cnt--;
979 } else {
980 if (info->xmit_cnt) {
981 /* Send char */
982 wait_EOT(info->usart);
983 if (info->xmit_tail + info->xmit_cnt < SERIAL_XMIT_SIZE) {
984 xmit_string(info, info->xmit_buf + info->xmit_tail,
985 info->xmit_cnt);
986 info->xmit_tail =
987 (info->xmit_tail +
988 info->xmit_cnt) & (SERIAL_XMIT_SIZE - 1);
989 info->xmit_cnt = 0;
990 } else {
991 coucou2();
992 xmit_string(info, info->xmit_buf + info->xmit_tail,
993 SERIAL_XMIT_SIZE - info->xmit_tail);
994 //xmit_string(info, info->xmit_buf, info->xmit_tail + info->xmit_cnt - SERIAL_XMIT_SIZE);
995 info->xmit_cnt =
996 info->xmit_cnt - (SERIAL_XMIT_SIZE - info->xmit_tail);
997 info->xmit_tail = 0;
1001 } else {
1002 /*printk("Skipping transmit\n"); */
1005 #if 0
1006 printk("Enabling stuff anyhow\n");
1007 tx_start(0);
1009 if (SCC_EOT(0, 0)) {
1010 printk("TX FIFO empty.\n");
1011 /* Send char */
1012 atmel_xmit_char(info->usart, info->xmit_buf[info->xmit_tail++]);
1013 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE - 1);
1014 info->xmit_cnt--;
1016 #endif
1018 local_irq_restore(flags);
1019 return total;
1022 static int rs_write_room(struct tty_struct *tty)
1024 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1025 int ret;
1027 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
1028 return 0;
1029 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
1030 if (ret < 0)
1031 ret = 0;
1032 return ret;
1035 static int rs_chars_in_buffer(struct tty_struct *tty)
1037 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1039 if (serial_paranoia_check(info, tty->name, "rs_chars_in_buffer"))
1040 return 0;
1041 return info->xmit_cnt;
1044 static void rs_flush_buffer(struct tty_struct *tty)
1046 unsigned long flags;
1047 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1049 if (serial_paranoia_check(info, tty->name, "rs_flush_buffer"))
1050 return;
1051 local_irq_save(flags);
1052 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
1053 local_irq_restore(flags);
1054 wake_up_interruptible(&tty->write_wait);
1055 if ((tty->flags & (1 << TTY_DO_WRITE_WAKEUP)) &&
1056 tty->ldisc.write_wakeup) (tty->ldisc.write_wakeup) (tty);
1060 * ------------------------------------------------------------
1061 * rs_throttle()
1063 * This routine is called by the upper-layer tty layer to signal that
1064 * incoming characters should be throttled.
1065 * ------------------------------------------------------------
1067 static void rs_throttle(struct tty_struct *tty)
1069 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1071 #ifdef SERIAL_DEBUG_THROTTLE
1072 char buf[64];
1074 printk("throttle %s: %d....\n", _tty_name(tty, buf),
1075 tty->ldisc.chars_in_buffer(tty));
1076 #endif
1078 if (serial_paranoia_check(info, tty->name, "rs_throttle"))
1079 return;
1081 if (I_IXOFF(tty))
1082 info->x_char = STOP_CHAR(tty);
1084 /* Turn off RTS line (do this atomic) */
1087 static void rs_unthrottle(struct tty_struct *tty)
1089 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1091 #ifdef SERIAL_DEBUG_THROTTLE
1092 char buf[64];
1094 printk("unthrottle %s: %d....\n", _tty_name(tty, buf),
1095 tty->ldisc.chars_in_buffer(tty));
1096 #endif
1098 if (serial_paranoia_check(info, tty->name, "rs_unthrottle"))
1099 return;
1101 if (I_IXOFF(tty)) {
1102 if (info->x_char)
1103 info->x_char = 0;
1104 else
1105 info->x_char = START_CHAR(tty);
1108 /* Assert RTS line (do this atomic) */
1112 * ------------------------------------------------------------
1113 * rs_ioctl() and friends
1114 * ------------------------------------------------------------
1117 static int get_serial_info(struct atmel_serial *info,
1118 struct serial_struct *retinfo)
1120 struct serial_struct tmp;
1122 if (!retinfo)
1123 return -EFAULT;
1124 memset(&tmp, 0, sizeof(tmp));
1125 tmp.type = info->type;
1126 tmp.line = info->line;
1127 tmp.port = info->port;
1128 tmp.irq = info->irq;
1129 tmp.flags = info->flags;
1130 tmp.baud_base = info->baud_base;
1131 tmp.close_delay = info->close_delay;
1132 tmp.closing_wait = info->closing_wait;
1133 tmp.custom_divisor = info->custom_divisor;
1134 copy_to_user(retinfo, &tmp, sizeof(*retinfo));
1135 return 0;
1138 static int set_serial_info(struct atmel_serial *info,
1139 struct serial_struct *new_info)
1141 struct serial_struct new_serial;
1142 struct atmel_serial old_info;
1143 int retval = 0;
1145 if (!new_info)
1146 return -EFAULT;
1147 copy_from_user(&new_serial, new_info, sizeof(new_serial));
1148 old_info = *info;
1150 if (!capable(CAP_SYS_ADMIN)) {
1151 if ((new_serial.baud_base != info->baud_base) ||
1152 (new_serial.type != info->type) ||
1153 (new_serial.close_delay != info->close_delay) ||
1154 ((new_serial.flags & ~S_USR_MASK) !=
1155 (info->flags & ~S_USR_MASK)))
1156 return -EPERM;
1157 info->flags = ((info->flags & ~S_USR_MASK) |
1158 (new_serial.flags & S_USR_MASK));
1159 info->custom_divisor = new_serial.custom_divisor;
1160 goto check_and_exit;
1163 if (info->count > 1)
1164 return -EBUSY;
1167 * OK, past this point, all the error checking has been done.
1168 * At this point, we start making changes.....
1171 info->baud_base = new_serial.baud_base;
1172 info->flags = ((info->flags & ~S_FLAGS) |
1173 (new_serial.flags & S_FLAGS));
1174 info->type = new_serial.type;
1175 info->close_delay = new_serial.close_delay;
1176 info->closing_wait = new_serial.closing_wait;
1178 check_and_exit:
1179 //retval = startup(info);
1180 change_speed(info);
1181 retval = 0;
1182 return retval;
1186 * get_lsr_info - get line status register info
1188 * Purpose: Let user call ioctl() to get info when the UART physically
1189 * is emptied. On bus types like RS485, the transmitter must
1190 * release the bus after transmitting. This must be done when
1191 * the transmit shift register is empty, not be done when the
1192 * transmit holding register is empty. This functionality
1193 * allows an RS485 driver to be written in user space.
1195 static int get_lsr_info(struct atmel_serial *info, unsigned int *value)
1197 unsigned char status;
1198 unsigned long flags;
1200 local_irq_save(flags);
1201 status = info->usart->csr;
1202 status &= US_TXEMPTY;
1203 local_irq_restore(flags);
1204 put_user(status, value);
1205 return 0;
1209 * This routine sends a break character out the serial port.
1211 static void send_break(struct atmel_serial *info, int duration)
1213 unsigned long flags;
1214 if (!info->port) return;
1216 current->state = TASK_INTERRUPTIBLE;
1217 local_irq_save(flags);
1218 info->usart->cr = US_STTBRK;
1219 if (!info->use_ints) {
1220 while (US_TXRDY != (info->usart->csr & US_TXRDY)) {
1221 ; // this takes max 2ms at 9600
1223 info->usart->cr = US_STPBRK;
1225 local_irq_restore(flags);
1228 static int rs_ioctl(struct tty_struct *tty, struct file *file,
1229 unsigned int cmd, unsigned long arg)
1231 int error;
1232 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1233 int retval;
1235 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
1236 return -ENODEV;
1238 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
1239 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
1240 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
1241 if (tty->flags & (1 << TTY_IO_ERROR))
1242 return -EIO;
1245 switch (cmd) {
1246 case TCSBRK: /* SVID version: non-zero arg --> no break */
1247 retval = tty_check_change(tty);
1248 if (retval)
1249 return retval;
1250 tty_wait_until_sent(tty, 0);
1251 if (!arg)
1252 send_break(info, HZ / 4); /* 1/4 second */
1253 return 0;
1254 case TCSBRKP: /* support for POSIX tcsendbreak() */
1255 retval = tty_check_change(tty);
1256 if (retval)
1257 return retval;
1258 tty_wait_until_sent(tty, 0);
1259 send_break(info, arg ? arg * (HZ / 10) : HZ / 4);
1260 return 0;
1261 case TIOCGSOFTCAR:
1262 error = access_ok(VERIFY_WRITE, (void *) arg, sizeof(long));
1263 if (error)
1264 return error;
1265 put_user(C_CLOCAL(tty) ? 1 : 0, (unsigned long *) arg);
1266 return 0;
1267 case TIOCSSOFTCAR:
1268 arg = get_user(arg,(unsigned long *) arg);
1269 tty->termios->c_cflag = ((tty->termios->c_cflag & ~CLOCAL) | (arg ? CLOCAL : 0));
1270 return 0;
1271 case TIOCGSERIAL:
1272 error = access_ok(VERIFY_WRITE, (void *) arg, sizeof(struct serial_struct));
1273 if (error)
1274 return error;
1275 return get_serial_info(info, (struct serial_struct *) arg);
1276 case TIOCSSERIAL:
1277 return set_serial_info(info, (struct serial_struct *) arg);
1278 case TIOCSERGETLSR: /* Get line status register */
1279 error = access_ok(VERIFY_WRITE, (void *) arg,
1280 sizeof(unsigned int));
1281 if (error)
1282 return error;
1283 else
1284 return get_lsr_info(info, (unsigned int *) arg);
1286 case TIOCSERGSTRUCT:
1287 error = access_ok(VERIFY_WRITE, (void *) arg,
1288 sizeof(struct atmel_serial));
1289 if (error)
1290 return error;
1291 copy_to_user((struct atmel_serial *) arg, info,
1292 sizeof(struct atmel_serial));
1293 return 0;
1295 case TCSETS:
1296 handle_termios_tcsets((struct termios *)arg, info);
1297 // return set_serial_info(info, (struct serial_struct *) arg);
1298 break;
1299 default:
1300 return -ENOIOCTLCMD;
1302 return 0;
1305 static void handle_termios_tcsets(struct termios * ptermios, struct atmel_serial * pinfo )
1308 * hmmmm....
1310 if (pinfo->tty->termios->c_cflag != ptermios->c_cflag)
1311 pinfo->tty->termios->c_cflag = ptermios->c_cflag;
1312 change_speed(pinfo);
1315 static void rs_set_termios(struct tty_struct *tty,
1316 struct termios *old_termios)
1318 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1320 if (tty->termios->c_cflag == old_termios->c_cflag)
1321 return;
1323 change_speed(info);
1325 if ((old_termios->c_cflag & CRTSCTS) &&
1326 !(tty->termios->c_cflag & CRTSCTS)) {
1327 tty->hw_stopped = 0;
1328 rs_start(tty);
1334 * ------------------------------------------------------------
1335 * rs_close()
1337 * This routine is called when the serial port gets closed. First, we
1338 * wait for the last remaining data to be sent. Then, we unlink its
1339 * S structure from the interrupt chain if necessary, and we free
1340 * that IRQ if nothing is left in the chain.
1341 * ------------------------------------------------------------
1343 static void rs_close(struct tty_struct *tty, struct file *filp)
1345 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1346 unsigned long flags;
1348 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
1349 return;
1351 local_irq_save(flags);
1353 if (tty_hung_up_p(filp)) {
1354 local_irq_restore(flags);
1355 return;
1357 #ifdef SERIAL_DEBUG_OPEN
1358 printk("rs_close ttyS%d, count = %d\n", info->line, info->count);
1359 #endif
1360 if ((tty->count == 1) && (info->count != 1)) {
1362 * Uh, oh. tty->count is 1, which means that the tty
1363 * structure will be freed. Info->count should always
1364 * be one in these conditions. If it's greater than
1365 * one, we've got real problems, since it means the
1366 * serial port won't be shutdown.
1368 printk("rs_close: bad serial port count; tty->count is 1, "
1369 "info->count is %d\n", info->count);
1370 info->count = 1;
1372 if (--info->count < 0) {
1373 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1374 info->line, info->count);
1375 info->count = 0;
1377 if (info->count) {
1378 local_irq_restore(flags);
1379 return;
1381 // closing port so disable interrupts
1382 set_ints_mode(0, info);
1384 info->flags |= S_CLOSING;
1386 * Now we wait for the transmit buffer to clear; and we notify
1387 * the line discipline to only process XON/XOFF characters.
1389 tty->closing = 1;
1390 if (info->closing_wait != S_CLOSING_WAIT_NONE)
1391 tty_wait_until_sent(tty, info->closing_wait);
1393 * At this point we stop accepting input. To do this, we
1394 * disable the receive line status interrupts, and tell the
1395 * interrupt driver to stop checking the data ready bit in the
1396 * line status register.
1399 shutdown(info);
1400 if (tty->driver->flush_buffer)
1401 tty->driver->flush_buffer(tty);
1402 if (tty->ldisc.flush_buffer)
1403 tty->ldisc.flush_buffer(tty);
1404 tty->closing = 0;
1405 info->event = 0;
1406 info->tty = 0;
1407 #if 0
1408 if (tty->ldisc.num != ldiscs[N_TTY].num) {
1409 if (tty->ldisc.close)
1410 (tty->ldisc.close) (tty);
1411 tty->ldisc = ldiscs[N_TTY];
1412 tty->termios->c_line = N_TTY;
1413 if (tty->ldisc.open)
1414 (tty->ldisc.open) (tty);
1416 #endif
1417 if (info->blocked_open) {
1418 if (info->close_delay) {
1419 current->state = TASK_INTERRUPTIBLE;
1420 schedule_timeout(info->close_delay);
1422 wake_up_interruptible(&info->open_wait);
1424 info->flags &= ~(S_NORMAL_ACTIVE | S_CALLOUT_ACTIVE | S_CLOSING);
1425 wake_up_interruptible(&info->close_wait);
1426 local_irq_restore(flags);
1430 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1432 static void rs_hangup(struct tty_struct *tty)
1434 struct atmel_serial *info = (struct atmel_serial *) tty->driver_data;
1436 if (serial_paranoia_check(info, tty->name, "rs_hangup"))
1437 return;
1439 rs_flush_buffer(tty);
1440 shutdown(info);
1441 info->event = 0;
1442 info->count = 0;
1443 info->flags &= ~S_NORMAL_ACTIVE;
1444 info->tty = 0;
1445 wake_up_interruptible(&info->open_wait);
1449 * ------------------------------------------------------------
1450 * rs_open() and friends
1451 * ------------------------------------------------------------
1453 static int block_til_ready(struct tty_struct *tty, struct file *filp,
1454 struct atmel_serial *info)
1456 DECLARE_WAITQUEUE(wait, current);
1457 int retval;
1458 int do_clocal = 0;
1461 * If the device is in the middle of being closed, then block
1462 * until it's done, and then try again.
1464 if (info->flags & S_CLOSING) {
1465 interruptible_sleep_on(&info->close_wait);
1466 #ifdef SERIAL_DO_RESTART
1467 if (info->flags & S_HUP_NOTIFY)
1468 return -EAGAIN;
1469 else
1470 return -ERESTARTSYS;
1471 #else
1472 return -EAGAIN;
1473 #endif
1477 * If non-blocking mode is set, or the port is not enabled,
1478 * then make the check up front and then exit.
1480 if ((filp->f_flags & O_NONBLOCK) ||
1481 (tty->flags & (1 << TTY_IO_ERROR))) {
1482 info->flags |= S_NORMAL_ACTIVE;
1483 return 0;
1486 if (tty->termios->c_cflag & CLOCAL)
1487 do_clocal = 1;
1490 * Block waiting for the carrier detect and the line to become
1491 * free (i.e., not in use by the callout). While we are in
1492 * this loop, info->count is dropped by one, so that
1493 * rs_close() knows when to free things. We restore it upon
1494 * exit, either normal or abnormal.
1496 retval = 0;
1497 add_wait_queue(&info->open_wait, &wait);
1498 #ifdef SERIAL_DEBUG_OPEN
1499 printk("block_til_ready before block: ttyS%d, count = %d\n",
1500 info->line, info->count);
1501 #endif
1502 info->count--;
1503 info->blocked_open++;
1504 while (1) {
1505 #ifdef US_RTS
1506 local_irq_save(flags);
1507 atmel_rtsdtr(info, 1);
1508 local_irq_restore(flags);
1509 #endif
1510 current->state = TASK_INTERRUPTIBLE;
1511 if (tty_hung_up_p(filp) ||
1512 !(info->flags & S_INITIALIZED)) {
1513 #ifdef SERIAL_DO_RESTART
1514 if (info->flags & S_HUP_NOTIFY)
1515 retval = -EAGAIN;
1516 else
1517 retval = -ERESTARTSYS;
1518 #else
1519 retval = -EAGAIN;
1520 #endif
1521 break;
1523 if (!(info->flags & S_CLOSING) && do_clocal)
1524 break;
1525 if (signal_pending(current)) {
1526 retval = -ERESTARTSYS;
1527 break;
1529 #ifdef SERIAL_DEBUG_OPEN
1530 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1531 info->line, info->count);
1532 #endif
1533 schedule();
1535 current->state = TASK_RUNNING;
1536 remove_wait_queue(&info->open_wait, &wait);
1537 if (!tty_hung_up_p(filp))
1538 info->count++;
1539 info->blocked_open--;
1540 #ifdef SERIAL_DEBUG_OPEN
1541 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1542 info->line, info->count);
1543 #endif
1544 if (retval)
1545 return retval;
1546 info->flags |= S_NORMAL_ACTIVE;
1547 if (!info->use_ints) {
1548 serialpoll.data = (void *) info;
1549 schedule_work(&serialpoll);
1551 return 0;
1555 * This routine is called whenever a serial port is opened. It
1556 * enables interrupts for a serial port, linking in its S structure into
1557 * the IRQ chain. It also performs the serial-specific
1558 * initialization for the tty structure.
1560 int rs_open(struct tty_struct *tty, struct file *filp)
1562 struct atmel_serial *info;
1563 int retval, line;
1565 line = tty->index;
1567 // check if line is sane
1568 if (line < 0 || line >= AT91_USART_CNT)
1569 return -ENODEV;
1571 info = &atmel_info[line];
1572 if (serial_paranoia_check(info, tty->name, "rs_open"))
1573 return -ENODEV;
1575 info->count++;
1576 tty->driver_data = info;
1577 info->tty = tty;
1580 * Start up serial port
1582 set_ints_mode(1, info);
1584 retval = startup(info);
1585 if (retval)
1586 return retval;
1588 return block_til_ready(tty, filp, info);
1592 static struct irqaction irq_usart0 = {
1593 .handler = rs_interrupta,
1594 .name = "usart0",
1596 static struct irqaction irq_usart1 = {
1597 .handler = rs_interruptb,
1598 .name = "usart1",
1601 static void interrupts_init(void)
1603 setup_irq(IRQ_USART0, &irq_usart0);
1604 setup_irq(IRQ_USART1, &irq_usart1);
1607 static void show_serial_version(void)
1609 printk("Atmel USART driver version 0.99\n");
1612 static struct tty_operations rs_ops = {
1613 .open = rs_open,
1614 .close = rs_close,
1615 .write = rs_write,
1616 .flush_chars = rs_flush_chars,
1617 .write_room = rs_write_room,
1618 .chars_in_buffer = rs_chars_in_buffer,
1619 .flush_buffer = rs_flush_buffer,
1620 .ioctl = rs_ioctl,
1621 .throttle = rs_throttle,
1622 .unthrottle = rs_unthrottle,
1623 .set_termios = rs_set_termios,
1624 .stop = rs_stop,
1625 .start = rs_start,
1626 .hangup = rs_hangup,
1627 .set_ldisc = rs_set_ldisc,
1630 /* rs_init inits the driver */
1631 static int __init
1632 rs_atmel_init(void)
1634 struct atmel_serial *info;
1635 unsigned long flags;
1636 int i;
1638 /* initialise PIO for serial port */
1639 HW_AT91_USART_INIT
1641 serial_driver = alloc_tty_driver(2);
1642 if (!serial_driver)
1643 return -ENOMEM;
1645 // FIXME - do this right
1646 rx_buf_table[0] = rx_buf1;
1647 rx_buf_table[1] = rx_buf2;
1649 show_serial_version();
1651 /* Initialize the tty_driver structure */
1653 // set the tty_struct pointers to NULL to let the layer
1654 // above allocate the structs.
1655 for (i=0; i < AT91_USART_CNT; i++)
1656 serial_table[i] = NULL;
1658 serial_driver->name = "ttyS";
1659 serial_driver->major = TTY_MAJOR;
1660 serial_driver->minor_start = 64;
1661 serial_driver->type = TTY_DRIVER_TYPE_SERIAL;
1662 serial_driver->subtype = SERIAL_TYPE_NORMAL;
1663 serial_driver->init_termios = tty_std_termios;
1664 serial_driver->init_termios.c_cflag =
1665 B9600 | CS8 | CREAD | HUPCL | CLOCAL;
1666 serial_driver->flags = TTY_DRIVER_REAL_RAW;
1667 tty_set_operations(serial_driver, &rs_ops);
1669 if (tty_register_driver(serial_driver)) {
1670 put_tty_driver(serial_driver);
1671 printk(KERN_ERR "Couldn't register serial driver\n");
1672 return -ENOMEM;
1675 local_irq_save(flags);
1676 for (i = 0; i < 2; i++) {
1677 info = &atmel_info[i];
1678 info->magic = SERIAL_MAGIC;
1679 info->usart = usarts[i];
1680 info->tty = 0;
1681 info->irqmask = (i) ? (1<<IRQ_USART1) : (1<<IRQ_USART0);
1682 info->irq = (i) ? IRQ_USART1 : IRQ_USART0;
1683 #ifdef CONFIG_SWAP_ATMEL_PORTS
1684 info->port = (i) ? 2 : 1;
1685 info->line = !i;
1686 #ifdef CONFIG_ATMEL_CONSOLE
1687 info->is_cons = i;
1688 #else
1689 info->is_cons = 0;
1690 #endif
1691 #else
1692 info->port = (i) ? 1 : 2;
1693 info->line = i;
1694 #ifdef CONFIG_ATMEL_CONSOLE
1695 info->is_cons = !i;
1696 #else
1697 info->is_cons = 0;
1698 #endif
1699 #endif
1700 #ifdef CONFIG_CONSOLE_ON_SC28L91
1701 info->line += 1;
1702 #endif
1703 set_ints_mode(0, info);
1704 info->custom_divisor = 16;
1705 info->close_delay = 50;
1706 info->closing_wait = 3000;
1707 info->cts_state = 1;
1708 info->x_char = 0;
1709 info->event = 0;
1710 info->count = 0;
1711 info->blocked_open = 0;
1712 INIT_WORK(&info->tqueue, do_softint, info);
1713 INIT_WORK(&info->tqueue_hangup, do_serial_hangup, info);
1714 init_waitqueue_head(&info->open_wait);
1715 init_waitqueue_head(&info->close_wait);
1716 info->rx_buf = rx_buf_table[i];
1718 printk("%s%d at 0x%p (irq = %d)", serial_driver->name, info->line,
1719 info->usart, info->irq);
1720 printk(" is a builtin Atmel APB USART\n");
1723 // FIXME
1724 info->usart->cr = 0x1ac; // reset, disable
1725 info->usart->idr = 0xffffffff; // disable all interrupts
1726 info->usart->tcr = 0; // stop transmit
1727 info->usart->rcr = 0; // stop receive
1729 interrupts_init();
1731 local_irq_restore(flags);
1732 // hack to do polling
1733 serialpoll.func = serpoll;
1734 serialpoll.data = 0;
1736 return 0;
1739 module_init(rs_atmel_init);
1741 #if 0
1743 * register_serial and unregister_serial allows for serial ports to be
1744 * configured at run-time, to support PCMCIA modems.
1746 /* SPARC: Unused at this time, just here to make things link. */
1747 static int register_serial(struct serial_struct *req)
1749 return -1;
1752 static void unregister_serial(int line)
1754 return;
1757 static void dbg_putc(int ch)
1759 static char tmp[2];
1760 #define US_TPR (0x38) /* Transmit Pointer Register */
1761 #define US_TCR (0x3C) /* Transmit Counter Register */
1763 tmp[0] = ch;
1765 outl_t((unsigned long) tmp, (USART0_BASE + US_TPR) );
1766 outl_t(1, (USART0_BASE + US_TCR) );
1768 while (inl_t((USART0_BASE + US_TCR) )) {
1772 static void dbg_print(const char *str)
1774 const char *p;
1776 for (p = str; *p; p++) {
1777 if (*p == '\n') {
1778 dbg_putc('\r');
1780 dbg_putc(*p);
1784 static void dbg_printk(const char *fmt, ...)
1786 char tmp[256];
1787 va_list args;
1789 va_start(args, fmt);
1790 vsprintf(tmp, fmt, args);
1791 va_end(args);
1792 dbg_print(tmp);
1795 static void rs_atmel_print(const char *str)
1797 dbg_printk(str);
1800 static void dump_a(unsigned long a, unsigned int s)
1802 unsigned long q;
1804 for (q = 0; q < s; q++) {
1805 if (q % 16 == 0) {
1806 dbg_printk("%08X: ", q + a);
1808 if (q % 16 == 7) {
1809 dbg_printk("%02X-", *(unsigned char *) (q + a));
1810 } else {
1811 dbg_printk("%02X ", *(unsigned char *) (q + a));
1813 if (q % 16 == 15) {
1814 dbg_printk(" :\n");
1817 if (q % 16) {
1818 dbg_printk(" :\n");
1821 #endif
1823 int atmel_console_setup(struct console *cp, char *arg)
1825 if (!cp)
1826 return(-1);
1827 HW_AT91_USART_INIT
1828 return 0;
1831 static struct tty_driver *atmel_console_device(struct console *c, int *index)
1833 *index = c->index;
1834 return serial_driver;
1837 void atmel_console_write (struct console *co, const char *str,
1838 unsigned int count)
1840 struct atmel_serial *info;
1842 #ifdef CONFIG_SWAP_ATMEL_PORTS
1843 info = &atmel_info[1];
1844 #else
1845 info = &atmel_info[0];
1846 #endif
1848 if (!atmel_console_initialized) {
1849 init_console(info);
1850 uart_init(info);
1851 info->baud = 9600;
1852 tx_stop(info->usart);
1853 rx_stop(info->usart);
1854 uart_speed(info, 0xffff);
1855 tx_start(info->usart);
1856 rx_start(info->usart, info->use_ints);
1859 while (count--) {
1860 if (*str == '\n')
1861 rs_put_char(info,'\r');
1862 rs_put_char(info, *str++ );
1866 static struct console atmel_driver = {
1867 name: "ttyS",
1868 write: atmel_console_write,
1869 device: atmel_console_device,
1870 setup: atmel_console_setup,
1871 flags: CON_PRINTBUFFER,
1872 index: -1,
1876 static int __init atmel_console_init(void)
1878 register_console(&atmel_driver);
1879 return 0;
1882 console_initcall(atmel_console_init);