1 /*--------------------------------------------------------------------
3 * drivers\serial\NIOSserial.c
5 * Serial port driver for builtin NIOS UART.
7 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
8 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
9 * Copyright (C) 1998-2000 D. Jeff Dionne <jeff@lineo.ca>
10 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
11 * Copyright (C) 2001 Vic Phillips <vic@microtronix.com>
12 * Copyright (C) 2001 Wentao Xu <wentao@microtronix.com>
13 * Copyright (C) 2004 Microtronix Datacom Ltd
15 * This program is free software; you can redistribute it and/or modify
16 * it under the terms of the GNU General Public License as published by
17 * the Free Software Foundation; either version 2 of the License, or
18 * (at your option) any later version.
20 * This program is distributed in the hope that it will be useful,
21 * but WITHOUT ANY WARRANTY; without even the implied warranty of
22 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 * GNU General Public License for more details.
26 * Jan/20/2004 dgt NiosII
28 ---------------------------------------------------------------------*/
31 #include <linux/module.h>
32 #include <linux/errno.h>
33 #include <linux/signal.h>
34 #include <linux/sched.h>
35 #include <linux/timer.h>
36 #include <linux/interrupt.h>
37 #include <linux/tty.h>
38 #include <linux/tty_flip.h>
39 #include <linux/major.h>
40 #include <linux/string.h>
41 #include <linux/fcntl.h>
43 #include <linux/kernel.h>
44 #include <linux/console.h>
45 #include <linux/reboot.h>
46 #include <linux/keyboard.h>
47 #include <linux/init.h>
48 #include <linux/smp_lock.h>
49 #include <linux/serial.h>
50 #include <linux/irq.h>
53 #include <asm/system.h>
54 #include <asm/segment.h>
55 #include <asm/bitops.h>
56 #include <asm/delay.h>
57 #include <asm/uaccess.h>
59 #include "NIOSserial.h"
63 //struct tty_struct NIOS_ttys;
64 //struct NIOS_serial *NIOS_consinfo = 0;
67 extern wait_queue_head_t keypress_wait
;
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 void change_speed(struct NIOS_serial
*info
);
92 * Configuration table, UARTs to look for at startup.
94 static struct NIOS_serial nios_soft
[] = {
95 // { 0,0,1,0,0,0,0, (nasys_printf_uart), (nasys_printf_uart_irq) }, /* ttyS0 */
96 { 0,0,1,0,0,0,0, (int) (na_uart0
), (na_uart0_irq
) }, /* ttyS0 */
98 // { 0,0,0,0,0,0,0, (nasys_gdb_uart), (nasys_gdb_uart_irq) }, /* ttyS1 */
99 { 0,0,0,0,0,0,0, (int) (na_uart1
), (na_uart1_irq
) }, /* ttyS1 */
102 { 0,0,0,0,0,0,0, (int) (na_uart2
), (na_uart2_irq
) }, /* ttyS2 */
105 { 0,0,0,0,0,0,0, (int) (na_uart3
), (na_uart3_irq
) }, /* ttyS3 */
109 #define NR_PORTS (sizeof(nios_soft) / sizeof(struct NIOS_serial))
113 * tmp_buf is used as a temporary buffer by serial_write. We need to
114 * lock it in case the memcpy_fromfs blocks while swapping in a page,
115 * and some other program tries to do a serial write at the same time.
116 * Since the lock will only come under contention when the system is
117 * swapping and available memory is low, it makes sense to share one
118 * buffer across all the serial ports, since it significantly saves
119 * memory if large numbers of serial ports are open.
121 static unsigned char tmp_buf
[SERIAL_XMIT_SIZE
]; /* This is cheating */
122 DECLARE_MUTEX(tmp_buf_sem
);
124 static inline int serial_paranoia_check(struct NIOS_serial
*info
,
125 char *name
, const char *routine
)
127 #ifdef SERIAL_PARANOIA_CHECK
128 static const char *badmagic
=
129 "Warning: bad magic number for serial struct %s in %s\n";
130 static const char *badinfo
=
131 "Warning: null nios_serial for %s in %s\n";
134 printk(badinfo
, name
, routine
);
137 if (info
->magic
!= SERIAL_MAGIC
) {
138 printk(badmagic
, name
, routine
);
146 * This is used to figure out the divisor speeds and the timeouts
148 static int baud_table
[] = {
149 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
150 9600, 19200, 38400, 57600, 115200, 0 };
152 /* Sets or clears DTR/RTS on the requested line */
153 static inline void NIOS_rtsdtr(struct NIOS_serial
*ss
, int set
)
156 /* set the RTS/CTS line */
163 /* Utility routines */
164 static inline int get_baud(struct NIOS_serial
*ss
)
166 return 0; /* not implemented yet */
170 * ------------------------------------------------------------
171 * rs_stop() and rs_start()
173 * This routines are called before setting or resetting tty->stopped.
174 * They enable or disable transmitter interrupts, as necessary.
175 * ------------------------------------------------------------
177 static void rs_stop(struct tty_struct
*tty
)
179 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
180 np_uart
* uart
= (np_uart
*)(info
->port
);
183 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
186 local_irq_save(flags
);
187 uart
->np_uartcontrol
&= ~np_uartcontrol_itrdy_mask
;
188 local_irq_restore(flags
);
191 static void rs_put_char(char ch
, struct NIOS_serial
*info
)
193 int flags
, loops
= 0;
194 np_uart
* uart
= (np_uart
*)(info
->port
);
196 local_irq_save(flags
);
198 while (!(uart
->np_uartstatus
& np_uartstatus_trdy_mask
) && (loops
< 100000)) {
202 uart
->np_uarttxdata
= ch
;
203 local_irq_restore(flags
);
206 static void rs_start(struct tty_struct
*tty
)
208 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
210 np_uart
* uart
= (np_uart
*)(info
->port
);
212 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
215 local_irq_save(flags
);
216 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->np_uartcontrol
& np_uartcontrol_itrdy_mask
)) {
218 uart
->np_uartcontrol
&= np_uartcontrol_itrdy_mask
;
221 local_irq_restore(flags
);
224 /* Drop into either the boot monitor or gdb upon receiving a break
225 * from keyboard/console input.
227 #ifdef CONFIG_MAGIC_SYSRQ
228 static void batten_down_hatches(void)
230 /* Drop into the debugger */
231 // nios_gdb_breakpoint();
233 #endif // CONFIG_MAGIC_SYSRQ
235 static _INLINE_
void status_handle(struct NIOS_serial
*info
, unsigned short status
)
240 static _INLINE_
void receive_chars(struct NIOS_serial
*info
, unsigned short rx
)
242 struct tty_struct
*tty
= info
->tty
;
243 unsigned char ch
, flag
;
244 np_uart
* uart
= (np_uart
*)(info
->port
);
247 * This do { } while() loop will get ALL chars out of Rx FIFO
250 ch
= uart
->np_uartrxdata
;
253 #ifdef CONFIG_MAGIC_SYSRQ
254 if(rx
& np_uartstatus_brk_mask
) {
255 batten_down_hatches();
257 } else if (ch
== 0x10) { /* ^P */
262 } else if (ch
== 0x02) { /* ^B */
263 batten_down_hatches();
267 #endif /* CONFIG_MAGIC_SYSRQ */
268 /* It is a 'keyboard interrupt' ;-) */
269 #ifdef CONFIG_CONSOLE
270 wake_up(&keypress_wait
);
279 if(rx
& np_uartstatus_pe_mask
) {
281 status_handle(info
, rx
);
282 } else if(rx
& np_uartstatus_roe_mask
) {
284 status_handle(info
, rx
);
285 } else if(rx
& np_uartstatus_fe_mask
) {
287 status_handle(info
, rx
);
288 } else if(rx
& np_uartstatus_brk_mask
) {
290 status_handle(info
, rx
);
292 tty_insert_flip_char(tty
, ch
, flag
);
294 } while((rx
= uart
->np_uartstatus
) & np_uartstatus_rrdy_mask
);
296 tty_schedule_flip(tty
);
302 static _INLINE_
void transmit_chars(struct NIOS_serial
*info
)
304 struct tty_struct
*tty
= info
->tty
;
305 np_uart
* uart
= (np_uart
*)(info
->port
);
308 uart
->np_uarttxdata
= info
->x_char
;
310 goto clear_and_return
;
313 if((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
314 /* That's peculiar... TX ints off */
315 uart
->np_uartcontrol
&= ~np_uartcontrol_itrdy_mask
;
316 goto clear_and_return
;
320 uart
->np_uarttxdata
= info
->xmit_buf
[info
->xmit_tail
++];
321 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
324 if (info
->xmit_cnt
< WAKEUP_CHARS
)
325 schedule_work(&info
->tqueue
);
327 if(info
->xmit_cnt
<= 0) {
328 /* All done for now... TX ints off */
329 uart
->np_uartcontrol
&= ~np_uartcontrol_itrdy_mask
;
330 wake_up_interruptible(&tty
->write_wait
);
331 goto clear_and_return
;
335 /* Clear interrupt (should be auto)*/
340 * This is the serial driver's generic interrupt routine
342 irqreturn_t
rs_interrupt(int irq
, void *dev_id
)
344 struct NIOS_serial
* info
= (struct NIOS_serial
*) dev_id
;
345 np_uart
* uart
= (np_uart
*)(info
->port
);
346 unsigned short stat
= uart
->np_uartstatus
;
347 uart
->np_uartstatus
= 0; /* clear any error status */
349 if (stat
& np_uartstatus_rrdy_mask
) receive_chars(info
, stat
);
350 if (stat
& np_uartstatus_trdy_mask
) transmit_chars(info
);
354 static void do_softint(void *private)
356 struct NIOS_serial
*info
= (struct NIOS_serial
*) private;
357 struct tty_struct
*tty
;
366 * This routine is called from the scheduler tqueue when the interrupt
367 * routine has signalled that a hangup has occurred. The path of
368 * hangup processing is:
370 * serial interrupt routine -> (scheduler tqueue) ->
371 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
374 static void do_serial_hangup(void *private_
)
376 struct NIOS_serial
*info
= (struct NIOS_serial
*) private_
;
377 struct tty_struct
*tty
;
388 static int startup(struct NIOS_serial
* info
)
391 np_uart
* uart
= (np_uart
*)(info
->port
);
393 if (info
->flags
& S_INITIALIZED
)
396 if (!info
->xmit_buf
) {
397 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
402 local_irq_save(flags
);
405 * Clear the FIFO buffers and disable them
406 * (they will be reenabled in change_speed())
411 info
->xmit_fifo_size
= 1;
412 uart
->np_uartcontrol
= np_uartcontrol_itrdy_mask
413 | np_uartcontrol_irrdy_mask
414 | np_uartcontrol_ibrk_mask
;
418 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
419 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
421 info
->flags
|= S_INITIALIZED
;
422 local_irq_restore(flags
);
427 * This routine will shutdown a serial port; interrupts are disabled, and
428 * DTR is dropped if the hangup on close termio flag is on.
430 static void shutdown(struct NIOS_serial
* info
)
433 np_uart
* uart
= (np_uart
*)(info
->port
);
435 uart
->np_uartcontrol
= 0; /* All off! */
436 if (!(info
->flags
& S_INITIALIZED
))
439 local_irq_save(flags
); /* Disable interrupts */
441 if (info
->xmit_buf
) {
442 free_page((unsigned long) info
->xmit_buf
);
447 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
449 info
->flags
&= ~S_INITIALIZED
;
450 local_irq_restore(flags
);
454 * This routine is called to set the UART divisor registers to match
455 * the specified baud rate for a serial port.
457 static void change_speed(struct NIOS_serial
*info
)
459 #if 1 /* NIOS usually has a fixed speed */
461 // unsigned long ustcnt;
465 np_uart
* uart
= (np_uart
*)(info
->port
);
467 if (!info
->tty
|| !info
->tty
->termios
)
469 cflag
= info
->tty
->termios
->c_cflag
;
471 // ustcnt = uart->np_uartcontrol;
472 // uart->np_uartcontrol = ustcnt & ~UCTRL_TE;
476 i
= (i
& ~CBAUDEX
) + B38400
;
479 divisor
= (unsigned)(((unsigned)nasys_clock_freq
*1.0 / baud_table
[i
]) + 1) -1;
480 uart
->np_uartdivisor
= divisor
;
481 if (uart
->np_uartdivisor
== divisor
)
482 info
->baud
= baud_table
[i
];
484 // ustcnt &= ~(UCTRL_PE | UCTRL_PS);
485 // if ((cflag & CSIZE) == CS8)
486 // ustcnt |= USTCNT_8_7;
487 // if (cflag & CSTOPB)
488 // ustcnt |= USTCNT_STOP;
489 // if (cflag & PARENB)
490 // ustcnt |= UCTRL_PE;
491 // if (!(cflag & PARODD))
492 // ustcnt |= UCTRL_PS;
493 // if (cflag & CRTSCTS) {
494 // ustcnt |= UCTRL_FL;
496 // ustcnt &= ~UCTRL_FL;
498 // ustcnt |= UCTRL_TE;
499 // uart->np_uartcontrol = ustcnt;
505 * Fair output driver allows a process to speak.
508 static void rs_fair_output(void)
510 int left
; /* Output no more than that */
512 struct NIOS_serial
*info
= &nios_soft
;
515 if (info
== 0) return;
516 if (info
->xmit_buf
== 0) return;
518 local_irq_save(flags
);
519 left
= info
->xmit_cnt
;
521 c
= info
->xmit_buf
[info
->xmit_tail
];
522 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
524 local_irq_restore(flags
);
526 rs_put_char(c
, info
);
528 local_irq_save(flags
);
529 left
= min(info
->xmit_cnt
, left
-1);
532 /* Last character is being transmitted now (hopefully). */
535 local_irq_restore(flags
);
541 * console_print_NIOS is registered for printk.
543 void console_print_NIOS(const char *p
)
547 while((c
=*(p
++)) != 0) {
549 rs_put_char('\r', nios_soft
);
550 rs_put_char(c
, nios_soft
);
553 /* Comment this if you want to have a strict interrupt-driven output */
554 /* rs_fair_output(); */
559 static void rs_set_ldisc(struct tty_struct
*tty
)
561 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
563 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
566 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
568 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
571 static void rs_flush_chars(struct tty_struct
*tty
)
573 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
575 np_uart
* uart
= (np_uart
*)(info
->port
);
577 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
580 local_irq_save(flags
);
582 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
586 /* Enable transmitter */
588 uart
->np_uartcontrol
|= np_uartcontrol_itrdy_mask
;
590 if (uart
->np_uartstatus
& np_uartstatus_trdy_mask
) {
592 uart
->np_uarttxdata
= info
->xmit_buf
[info
->xmit_tail
++];
593 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
598 local_irq_restore(flags
);
601 extern void console_printn(const char * b
, int count
);
603 static int rs_write(struct tty_struct
* tty
,
604 const unsigned char *buf
, int count
)
607 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
609 np_uart
* uart
= (np_uart
*)(info
->port
);
611 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
614 if (!tty
|| !info
->xmit_buf
)
617 local_save_flags(flags
);
620 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
621 SERIAL_XMIT_SIZE
- info
->xmit_head
));
625 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
626 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
628 local_irq_restore(flags
);
635 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
636 /* Enable transmitter */
639 uart
->np_uartcontrol
|= np_uartcontrol_itrdy_mask
;
641 if (uart
->np_uartstatus
& np_uartstatus_trdy_mask
) {
642 uart
->np_uarttxdata
= info
->xmit_buf
[info
->xmit_tail
++];
643 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
647 local_irq_restore(flags
);
649 local_irq_restore(flags
);
653 static int rs_write_room(struct tty_struct
*tty
)
655 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
658 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
660 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
666 static int rs_chars_in_buffer(struct tty_struct
*tty
)
668 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
670 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
672 return info
->xmit_cnt
;
675 static void rs_flush_buffer(struct tty_struct
*tty
)
677 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
679 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
682 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
684 wake_up_interruptible(&tty
->write_wait
);
685 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
686 tty
->ldisc
.write_wakeup
)
687 (tty
->ldisc
.write_wakeup
)(tty
);
691 * ------------------------------------------------------------
694 * This routine is called by the upper-layer tty layer to signal that
695 * incoming characters should be throttled.
696 * ------------------------------------------------------------
698 static void rs_throttle(struct tty_struct
* tty
)
700 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
702 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
706 info
->x_char
= STOP_CHAR(tty
);
708 /* Turn off RTS line (do this atomic) */
711 static void rs_unthrottle(struct tty_struct
* tty
)
713 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
715 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
722 info
->x_char
= START_CHAR(tty
);
725 /* Assert RTS line (do this atomic) */
729 * ------------------------------------------------------------
730 * rs_ioctl() and friends
731 * ------------------------------------------------------------
734 static int get_serial_info(struct NIOS_serial
* info
,
735 struct serial_struct
* retinfo
)
737 struct serial_struct tmp
;
741 memset(&tmp
, 0, sizeof(tmp
));
742 tmp
.type
= info
->type
;
743 tmp
.line
= info
->line
;
744 tmp
.port
= info
->port
;
746 tmp
.flags
= info
->flags
;
747 tmp
.baud_base
= info
->baud_base
;
748 tmp
.close_delay
= info
->close_delay
;
749 tmp
.closing_wait
= info
->closing_wait
;
750 tmp
.custom_divisor
= info
->custom_divisor
;
751 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
755 static int set_serial_info(struct NIOS_serial
* info
,
756 struct serial_struct
* new_info
)
758 struct serial_struct new_serial
;
759 struct NIOS_serial old_info
;
764 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
767 if (!capable(CAP_SYS_ADMIN
)) {
768 if ((new_serial
.baud_base
!= info
->baud_base
) ||
769 (new_serial
.type
!= info
->type
) ||
770 (new_serial
.close_delay
!= info
->close_delay
) ||
771 ((new_serial
.flags
& ~S_USR_MASK
) !=
772 (info
->flags
& ~S_USR_MASK
)))
774 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
775 (new_serial
.flags
& S_USR_MASK
));
776 info
->custom_divisor
= new_serial
.custom_divisor
;
784 * OK, past this point, all the error checking has been done.
785 * At this point, we start making changes.....
788 info
->baud_base
= new_serial
.baud_base
;
789 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
790 (new_serial
.flags
& S_FLAGS
));
791 info
->type
= new_serial
.type
;
792 info
->close_delay
= new_serial
.close_delay
;
793 info
->closing_wait
= new_serial
.closing_wait
;
796 retval
= startup(info
);
801 * get_lsr_info - get line status register info
803 * Purpose: Let user call ioctl() to get info when the UART physically
804 * is emptied. On bus types like RS485, the transmitter must
805 * release the bus after transmitting. This must be done when
806 * the transmit shift register is empty, not be done when the
807 * transmit holding register is empty. This functionality
808 * allows an RS485 driver to be written in user space.
810 static int get_lsr_info(struct NIOS_serial
* info
, unsigned int *value
)
812 unsigned char status
;
815 put_user(status
,value
);
820 * This routine sends a break character out the serial port.
822 static void send_break( struct NIOS_serial
* info
, int duration
)
826 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
827 unsigned int cmd
, unsigned long arg
)
830 struct NIOS_serial
* info
= (struct NIOS_serial
*)tty
->driver_data
;
833 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
836 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
837 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
838 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
839 if (tty
->flags
& (1 << TTY_IO_ERROR
))
844 case TCSBRK
: /* SVID version: non-zero arg --> no break */
845 retval
= tty_check_change(tty
);
848 tty_wait_until_sent(tty
, 0);
850 send_break(info
, HZ
/4); /* 1/4 second */
852 case TCSBRKP
: /* support for POSIX tcsendbreak() */
853 retval
= tty_check_change(tty
);
856 tty_wait_until_sent(tty
, 0);
857 send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
860 error
= put_user(C_CLOCAL(tty
) ? 1 : 0,
861 (unsigned long *) arg
);
866 get_user(arg
, (unsigned long *) arg
);
867 tty
->termios
->c_cflag
=
868 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
872 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
873 sizeof(struct serial_struct
));
876 return get_serial_info(info
,
877 (struct serial_struct
*) arg
);
879 return set_serial_info(info
,
880 (struct serial_struct
*) arg
);
881 case TIOCSERGETLSR
: /* Get line status register */
882 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
883 sizeof(unsigned int));
887 return get_lsr_info(info
, (unsigned int *) arg
);
890 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
891 sizeof(struct NIOS_serial
));
894 copy_to_user((struct NIOS_serial
*) arg
,
895 info
, sizeof(struct NIOS_serial
));
904 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
906 struct NIOS_serial
*info
= (struct NIOS_serial
*)tty
->driver_data
;
907 int oldbaud
= info
->baud
;
909 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
914 if (info
->baud
== oldbaud
) /* can not change */
915 tty
->termios
->c_cflag
= old_termios
->c_cflag
;
916 else /* only speed can be changed */
917 tty
->termios
->c_cflag
= (tty
->termios
->c_cflag
& CBAUD
) | CS8
| CREAD
| HUPCL
| CLOCAL
;
919 if ((old_termios
->c_cflag
& CRTSCTS
) &&
920 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
928 * ------------------------------------------------------------
931 * This routine is called when the serial port gets closed. First, we
932 * wait for the last remaining data to be sent. Then, we unlink its
933 * S structure from the interrupt chain if necessary, and we free
934 * that IRQ if nothing is left in the chain.
935 * ------------------------------------------------------------
937 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
939 struct NIOS_serial
* info
= (struct NIOS_serial
*)tty
->driver_data
;
941 np_uart
* uart
= (np_uart
*)(info
->port
);
943 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
946 local_irq_save(flags
);
948 if (tty_hung_up_p(filp
)) {
949 local_irq_restore(flags
);
953 if ((tty
->count
== 1) && (info
->count
!= 1)) {
955 * Uh, oh. tty->count is 1, which means that the tty
956 * structure will be freed. Info->count should always
957 * be one in these conditions. If it's greater than
958 * one, we've got real problems, since it means the
959 * serial port won't be shutdown.
961 printk("rs_close: bad serial port count; tty->count is 1, "
962 "info->count is %d\n", info
->count
);
965 if (--info
->count
< 0) {
966 printk("rs_close: bad serial port count for ttyS%d: %d\n",
967 info
->line
, info
->count
);
971 local_irq_restore(flags
);
974 info
->flags
|= S_CLOSING
;
976 * Now we wait for the transmit buffer to clear; and we notify
977 * the line discipline to only process XON/XOFF characters.
980 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
981 tty_wait_until_sent(tty
, info
->closing_wait
);
983 * At this point we stop accepting input. To do this, we
984 * disable the receive line status interrupts, and tell the
985 * interrupt driver to stop checking the data ready bit in the
986 * line status register.
989 uart
->np_uartcontrol
&= ~(np_uartcontrol_irrdy_mask
);
992 if (tty
->driver
->flush_buffer
)
993 tty
->driver
->flush_buffer(tty
);
994 if (tty
->ldisc
.flush_buffer
)
995 tty
->ldisc
.flush_buffer(tty
);
1000 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1001 if (tty
->ldisc
.close
)
1002 (tty
->ldisc
.close
)(tty
);
1003 tty
->ldisc
= ldiscs
[N_TTY
];
1004 tty
->termios
->c_line
= N_TTY
;
1005 if (tty
->ldisc
.open
)
1006 (tty
->ldisc
.open
)(tty
);
1009 if (info
->blocked_open
) {
1010 if (info
->close_delay
) {
1011 current
->state
= TASK_INTERRUPTIBLE
;
1012 schedule_timeout(info
->close_delay
);
1014 wake_up_interruptible(&info
->open_wait
);
1016 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1017 wake_up_interruptible(&info
->close_wait
);
1018 local_irq_restore(flags
);
1022 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1024 void rs_hangup(struct tty_struct
*tty
)
1026 struct NIOS_serial
* info
= (struct NIOS_serial
*)tty
->driver_data
;
1028 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1031 rs_flush_buffer(tty
);
1035 info
->flags
&= ~S_NORMAL_ACTIVE
;
1037 wake_up_interruptible(&info
->open_wait
);
1041 * ------------------------------------------------------------
1042 * rs_open() and friends
1043 * ------------------------------------------------------------
1045 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1046 struct NIOS_serial
*info
)
1048 DECLARE_WAITQUEUE(wait
, current
);
1053 * If the device is in the middle of being closed, then block
1054 * until it's done, and then try again.
1056 if (info
->flags
& S_CLOSING
) {
1057 interruptible_sleep_on(&info
->close_wait
);
1058 #ifdef SERIAL_DO_RESTART
1059 if (info
->flags
& S_HUP_NOTIFY
)
1062 return -ERESTARTSYS
;
1069 * If non-blocking mode is set, or the port is not enabled,
1070 * then make the check up front and then exit.
1072 if ((filp
->f_flags
& O_NONBLOCK
) ||
1073 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1074 info
->flags
|= S_NORMAL_ACTIVE
;
1078 if (tty
->termios
->c_cflag
& CLOCAL
)
1082 * Block waiting for the carrier detect and the line to become
1083 * free (i.e., not in use by the callout). While we are in
1084 * this loop, info->count is dropped by one, so that
1085 * rs_close() knows when to free things. We restore it upon
1086 * exit, either normal or abnormal.
1089 add_wait_queue(&info
->open_wait
, &wait
);
1092 info
->blocked_open
++;
1094 local_irq_disable();
1095 NIOS_rtsdtr(info
, 1);
1097 current
->state
= TASK_INTERRUPTIBLE
;
1098 if (tty_hung_up_p(filp
) ||
1099 !(info
->flags
& S_INITIALIZED
)) {
1100 #ifdef SERIAL_DO_RESTART
1101 if (info
->flags
& S_HUP_NOTIFY
)
1104 retval
= -ERESTARTSYS
;
1110 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1112 if (signal_pending(current
)) {
1113 retval
= -ERESTARTSYS
;
1118 current
->state
= TASK_RUNNING
;
1119 remove_wait_queue(&info
->open_wait
, &wait
);
1120 if (!tty_hung_up_p(filp
))
1122 info
->blocked_open
--;
1126 info
->flags
|= S_NORMAL_ACTIVE
;
1131 * This routine is called whenever a serial port is opened. It
1132 * enables interrupts for a serial port, linking in its S structure into
1133 * the IRQ chain. It also performs the serial-specific
1134 * initialization for the tty structure.
1136 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1138 struct NIOS_serial
*info
;
1143 if (line
>=serial_driver
->num
) /* too many */
1146 info
= &nios_soft
[line
];
1148 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1152 tty
->driver_data
= info
;
1156 * Start up serial port
1158 retval
= startup(info
);
1162 return block_til_ready(tty
, filp
, info
);
1165 /* Finally, routines used to initialize the serial driver. */
1167 static void show_serial_version(void)
1169 printk("NIOS serial driver version 0.0\n");
1174 static struct tty_operations rs_ops
= {
1178 .flush_chars
= rs_flush_chars
,
1179 .write_room
= rs_write_room
,
1180 .chars_in_buffer
= rs_chars_in_buffer
,
1181 .flush_buffer
= rs_flush_buffer
,
1183 .throttle
= rs_throttle
,
1184 .unthrottle
= rs_unthrottle
,
1185 .set_termios
= rs_set_termios
,
1188 .hangup
= rs_hangup
,
1189 .set_ldisc
= rs_set_ldisc
,
1192 /* rs_init inits the driver */
1193 static int __init
rs_nios_init(void)
1196 struct NIOS_serial
*info
;
1199 serial_driver
= alloc_tty_driver(NR_PORTS
);
1203 show_serial_version();
1205 /* Initialize the tty_driver structure */
1206 /* SPARC: Not all of this is exactly right for us. */
1208 serial_driver
->name
= "ttyS";
1209 serial_driver
->major
= TTY_MAJOR
;
1210 serial_driver
->minor_start
= 64;
1211 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1212 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1213 serial_driver
->init_termios
= tty_std_termios
;
1214 serial_driver
->init_termios
.c_cflag
=
1215 B115200
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1216 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1217 tty_set_operations(serial_driver
, &rs_ops
);
1219 if (tty_register_driver(serial_driver
)) {
1220 put_tty_driver(serial_driver
);
1221 printk(KERN_ERR
"Couldn't register serial driver\n");
1225 local_irq_save(flags
);
1228 * Configure all the attached serial ports.
1230 for (i
= 0, info
= nios_soft
; (i
< NR_PORTS
); i
++, info
++) {
1231 info
->magic
= SERIAL_MAGIC
;
1233 info
->custom_divisor
= 16;
1234 info
->close_delay
= 50;
1235 info
->closing_wait
= 3000;
1239 info
->blocked_open
= 0;
1240 INIT_WORK(&info
->tqueue
, do_softint
, info
);
1241 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
, info
);
1242 init_waitqueue_head(&info
->open_wait
);
1243 init_waitqueue_head(&info
->close_wait
);
1246 printk("%s%d (irq = %d) is a builtin NIOS UART\n",
1247 serial_driver
->name
, info
->line
, info
->irq
);
1249 // rs_setsignals(info, 0, 0);
1250 if (request_irq(info
->irq
, rs_interrupt
, 0, "NIOS serial", info
))
1251 panic("Unable to attach NIOS serial interrupt\n");
1254 local_irq_restore(flags
);
1259 * register_serial and unregister_serial allows for serial ports to be
1260 * configured at run-time, to support PCMCIA modems.
1262 /* NIOS: Unused at this time, just here to make things link. */
1263 int register_serial(struct serial_struct
*req
)
1268 void unregister_serial(int line
)
1273 module_init(rs_nios_init
);
1275 #ifdef CONFIG_NIOS_SERIAL_CONSOLE
1276 int nios_console_setup(struct console
*cp
, char *arg
)
1282 static struct tty_driver
*nios_console_device(struct console
*c
, int *index
)
1285 return serial_driver
;
1289 void nios_console_write (struct console
*co
, const char *str
,
1294 rs_put_char('\r', nios_soft
);
1295 rs_put_char( *str
++, nios_soft
);
1300 static struct console nios_driver
= {
1302 .write
= nios_console_write
,
1303 .device
= nios_console_device
,
1304 .setup
= nios_console_setup
,
1305 .flags
= CON_PRINTBUFFER
,
1310 static int __init
nios_console_init(void)
1312 register_console(&nios_driver
);
1316 console_initcall(nios_console_init
);
1317 #endif /* CONFIG_NIOS_SERIAL_CONSOLE */