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 / NIOSserial.c
blob0a0d99b62fa81b59eeeae2619f7189605631ebcc
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>
42 #include <linux/mm.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>
52 #include <asm/io.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"
61 #define DEBUG 1
63 //struct tty_struct NIOS_ttys;
64 //struct NIOS_serial *NIOS_consinfo = 0;
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 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 */
97 #ifdef na_uart1
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 */
100 #endif
101 #ifdef na_uart2
102 { 0,0,0,0,0,0,0, (int) (na_uart2), (na_uart2_irq) }, /* ttyS2 */
103 #endif
104 #ifdef na_uart3
105 { 0,0,0,0,0,0,0, (int) (na_uart3), (na_uart3_irq) }, /* ttyS3 */
106 #endif
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";
133 if (!info) {
134 printk(badinfo, name, routine);
135 return 1;
137 if (info->magic != SERIAL_MAGIC) {
138 printk(badmagic, name, routine);
139 return 1;
141 #endif
142 return 0;
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)
155 if (set) {
156 /* set the RTS/CTS line */
157 } else {
158 /* clear it */
160 return;
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);
181 unsigned long flags;
183 if (serial_paranoia_check(info, tty->name, "rs_stop"))
184 return;
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)) {
199 loops++;
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;
209 unsigned long flags;
210 np_uart * uart= (np_uart *)(info->port);
212 if (serial_paranoia_check(info, tty->name, "rs_start"))
213 return;
215 local_irq_save(flags);
216 if (info->xmit_cnt && info->xmit_buf && !(uart->np_uartcontrol & np_uartcontrol_itrdy_mask)) {
217 #ifdef USE_INTS
218 uart->np_uartcontrol &= np_uartcontrol_itrdy_mask;
219 #endif
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)
237 return;
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
249 do {
250 ch = uart->np_uartrxdata;
252 if(info->is_cons) {
253 #ifdef CONFIG_MAGIC_SYSRQ
254 if(rx & np_uartstatus_brk_mask) {
255 batten_down_hatches();
256 return;
257 } else if (ch == 0x10) { /* ^P */
258 show_state();
259 show_mem();
260 return;
261 #ifdef DEBUG
262 } else if (ch == 0x02) { /* ^B */
263 batten_down_hatches();
264 return;
265 #endif
267 #endif /* CONFIG_MAGIC_SYSRQ */
268 /* It is a 'keyboard interrupt' ;-) */
269 #ifdef CONFIG_CONSOLE
270 wake_up(&keypress_wait);
271 #endif
274 if(!tty)
275 goto clear_and_exit;
277 flag = TTY_NORMAL;
279 if(rx & np_uartstatus_pe_mask) {
280 flag = TTY_PARITY;
281 status_handle(info, rx);
282 } else if(rx & np_uartstatus_roe_mask) {
283 flag = TTY_OVERRUN;
284 status_handle(info, rx);
285 } else if(rx & np_uartstatus_fe_mask) {
286 flag = TTY_FRAME;
287 status_handle(info, rx);
288 } else if(rx & np_uartstatus_brk_mask) {
289 flag = TTY_BREAK;
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);
298 clear_and_exit:
299 return;
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);
306 if (info->x_char) {
307 /* Send next char */
308 uart->np_uarttxdata = info->x_char;
309 info->x_char = 0;
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;
319 /* Send char */
320 uart->np_uarttxdata = info->xmit_buf[info->xmit_tail++];
321 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
322 info->xmit_cnt--;
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;
334 clear_and_return:
335 /* Clear interrupt (should be auto)*/
336 return;
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);
351 return IRQ_HANDLED;
354 static void do_softint(void *private)
356 struct NIOS_serial *info = (struct NIOS_serial *) private;
357 struct tty_struct *tty;
359 tty = info->tty;
360 if (!tty)
361 return;
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;
379 tty = info->tty;
380 if (!tty)
381 return;
383 tty_hangup(tty);
388 static int startup(struct NIOS_serial * info)
390 unsigned long flags;
391 np_uart * uart= (np_uart *)(info->port);
393 if (info->flags & S_INITIALIZED)
394 return 0;
396 if (!info->xmit_buf) {
397 info->xmit_buf = (unsigned char *) __get_free_page(GFP_KERNEL);
398 if (!info->xmit_buf)
399 return -ENOMEM;
402 local_irq_save(flags);
405 * Clear the FIFO buffers and disable them
406 * (they will be reenabled in change_speed())
409 change_speed(info);
411 info->xmit_fifo_size = 1;
412 uart->np_uartcontrol = np_uartcontrol_itrdy_mask
413 | np_uartcontrol_irrdy_mask
414 | np_uartcontrol_ibrk_mask;
415 uart->np_uartrxdata;
417 if (info->tty)
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);
423 return 0;
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)
432 unsigned long flags;
433 np_uart * uart= (np_uart *)(info->port);
435 uart->np_uartcontrol = 0; /* All off! */
436 if (!(info->flags & S_INITIALIZED))
437 return;
439 local_irq_save(flags); /* Disable interrupts */
441 if (info->xmit_buf) {
442 free_page((unsigned long) info->xmit_buf);
443 info->xmit_buf = 0;
446 if (info->tty)
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;
462 unsigned cflag;
463 unsigned divisor;
464 int i;
465 np_uart * uart= (np_uart *)(info->port);
467 if (!info->tty || !info->tty->termios)
468 return;
469 cflag = info->tty->termios->c_cflag;
471 // ustcnt = uart->np_uartcontrol;
472 // uart->np_uartcontrol = ustcnt & ~UCTRL_TE;
474 i = cflag & CBAUD;
475 if (i & CBAUDEX) {
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;
495 // } else {
496 // ustcnt &= ~UCTRL_FL;
497 // }
498 // ustcnt |= UCTRL_TE;
499 // uart->np_uartcontrol = ustcnt;
501 #endif
505 * Fair output driver allows a process to speak.
507 #if 0
508 static void rs_fair_output(void)
510 int left; /* Output no more than that */
511 unsigned long flags;
512 struct NIOS_serial *info = &nios_soft;
513 char c;
515 if (info == 0) return;
516 if (info->xmit_buf == 0) return;
518 local_irq_save(flags);
519 left = info->xmit_cnt;
520 while (left != 0) {
521 c = info->xmit_buf[info->xmit_tail];
522 info->xmit_tail = (info->xmit_tail+1) & (SERIAL_XMIT_SIZE-1);
523 info->xmit_cnt--;
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). */
533 udelay(5);
535 local_irq_restore(flags);
536 return;
538 #endif
541 * console_print_NIOS is registered for printk.
543 void console_print_NIOS(const char *p)
545 char c;
547 while((c=*(p++)) != 0) {
548 if(c == '\n')
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(); */
556 return;
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"))
564 return;
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;
574 unsigned long flags;
575 np_uart * uart= (np_uart *)(info->port);
577 if (serial_paranoia_check(info, tty->name, "rs_flush_chars"))
578 return;
580 local_irq_save(flags);
582 if (info->xmit_cnt <= 0 || tty->stopped || tty->hw_stopped ||
583 !info->xmit_buf)
584 goto flush_exit;
586 /* Enable transmitter */
588 uart->np_uartcontrol |= np_uartcontrol_itrdy_mask;
590 if (uart->np_uartstatus & np_uartstatus_trdy_mask) {
591 /* Send char */
592 uart->np_uarttxdata = info->xmit_buf[info->xmit_tail++];
593 info->xmit_tail = info->xmit_tail & (SERIAL_XMIT_SIZE-1);
594 info->xmit_cnt--;
597 flush_exit:
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)
606 int c, total = 0;
607 struct NIOS_serial *info = (struct NIOS_serial *)tty->driver_data;
608 unsigned long flags;
609 np_uart * uart= (np_uart *)(info->port);
611 if (serial_paranoia_check(info, tty->name, "rs_write"))
612 return 0;
614 if (!tty || !info->xmit_buf)
615 return 0;
617 local_save_flags(flags);
618 while (1) {
619 local_irq_disable();
620 c = min_t(int, count, min(SERIAL_XMIT_SIZE - info->xmit_cnt - 1,
621 SERIAL_XMIT_SIZE - info->xmit_head));
622 if (c <= 0)
623 break;
625 memcpy(info->xmit_buf + info->xmit_head, buf, c);
626 info->xmit_head = (info->xmit_head + c) & (SERIAL_XMIT_SIZE-1);
627 info->xmit_cnt += c;
628 local_irq_restore(flags);
629 buf += c;
630 count -= c;
631 total += c;
635 if (info->xmit_cnt && !tty->stopped && !tty->hw_stopped) {
636 /* Enable transmitter */
637 local_irq_disable();
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);
644 info->xmit_cnt--;
647 local_irq_restore(flags);
649 local_irq_restore(flags);
650 return total;
653 static int rs_write_room(struct tty_struct *tty)
655 struct NIOS_serial *info = (struct NIOS_serial *)tty->driver_data;
656 int ret;
658 if (serial_paranoia_check(info, tty->name, "rs_write_room"))
659 return 0;
660 ret = SERIAL_XMIT_SIZE - info->xmit_cnt - 1;
661 if (ret < 0)
662 ret = 0;
663 return ret;
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"))
671 return 0;
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"))
680 return;
681 local_irq_disable();
682 info->xmit_cnt = info->xmit_head = info->xmit_tail = 0;
683 local_irq_enable();
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 * ------------------------------------------------------------
692 * rs_throttle()
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"))
703 return;
705 if (I_IXOFF(tty))
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"))
716 return;
718 if (I_IXOFF(tty)) {
719 if (info->x_char)
720 info->x_char = 0;
721 else
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;
739 if (!retinfo)
740 return -EFAULT;
741 memset(&tmp, 0, sizeof(tmp));
742 tmp.type = info->type;
743 tmp.line = info->line;
744 tmp.port = info->port;
745 tmp.irq = info->irq;
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));
752 return 0;
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;
760 int retval = 0;
762 if (!new_info)
763 return -EFAULT;
764 copy_from_user(&new_serial,new_info,sizeof(new_serial));
765 old_info = *info;
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)))
773 return -EPERM;
774 info->flags = ((info->flags & ~S_USR_MASK) |
775 (new_serial.flags & S_USR_MASK));
776 info->custom_divisor = new_serial.custom_divisor;
777 goto check_and_exit;
780 if (info->count > 1)
781 return -EBUSY;
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;
795 check_and_exit:
796 retval = startup(info);
797 return retval;
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;
814 status = 0;
815 put_user(status,value);
816 return 0;
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)
829 int error;
830 struct NIOS_serial * info = (struct NIOS_serial *)tty->driver_data;
831 int retval;
833 if (serial_paranoia_check(info, tty->name, "rs_ioctl"))
834 return -ENODEV;
836 if ((cmd != TIOCGSERIAL) && (cmd != TIOCSSERIAL) &&
837 (cmd != TIOCSERCONFIG) && (cmd != TIOCSERGWILD) &&
838 (cmd != TIOCSERSWILD) && (cmd != TIOCSERGSTRUCT)) {
839 if (tty->flags & (1 << TTY_IO_ERROR))
840 return -EIO;
843 switch (cmd) {
844 case TCSBRK: /* SVID version: non-zero arg --> no break */
845 retval = tty_check_change(tty);
846 if (retval)
847 return retval;
848 tty_wait_until_sent(tty, 0);
849 if (!arg)
850 send_break(info, HZ/4); /* 1/4 second */
851 return 0;
852 case TCSBRKP: /* support for POSIX tcsendbreak() */
853 retval = tty_check_change(tty);
854 if (retval)
855 return retval;
856 tty_wait_until_sent(tty, 0);
857 send_break(info, arg ? arg*(HZ/10) : HZ/4);
858 return 0;
859 case TIOCGSOFTCAR:
860 error = put_user(C_CLOCAL(tty) ? 1 : 0,
861 (unsigned long *) arg);
862 if (error)
863 return error;
864 return 0;
865 case TIOCSSOFTCAR:
866 get_user(arg, (unsigned long *) arg);
867 tty->termios->c_cflag =
868 ((tty->termios->c_cflag & ~CLOCAL) |
869 (arg ? CLOCAL : 0));
870 return 0;
871 case TIOCGSERIAL:
872 error = verify_area(VERIFY_WRITE, (void *) arg,
873 sizeof(struct serial_struct));
874 if (error)
875 return error;
876 return get_serial_info(info,
877 (struct serial_struct *) arg);
878 case TIOCSSERIAL:
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));
884 if (error)
885 return error;
886 else
887 return get_lsr_info(info, (unsigned int *) arg);
889 case TIOCSERGSTRUCT:
890 error = verify_area(VERIFY_WRITE, (void *) arg,
891 sizeof(struct NIOS_serial));
892 if (error)
893 return error;
894 copy_to_user((struct NIOS_serial *) arg,
895 info, sizeof(struct NIOS_serial));
896 return 0;
898 default:
899 return -ENOIOCTLCMD;
901 return 0;
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)
910 return;
912 change_speed(info);
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;
918 #if 0
919 if ((old_termios->c_cflag & CRTSCTS) &&
920 !(tty->termios->c_cflag & CRTSCTS)) {
921 tty->hw_stopped = 0;
922 rs_start(tty);
924 #endif
928 * ------------------------------------------------------------
929 * rs_close()
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;
940 unsigned long flags;
941 np_uart * uart= (np_uart *)(info->port);
943 if (!info || serial_paranoia_check(info, tty->name, "rs_close"))
944 return;
946 local_irq_save(flags);
948 if (tty_hung_up_p(filp)) {
949 local_irq_restore(flags);
950 return;
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);
963 info->count = 1;
965 if (--info->count < 0) {
966 printk("rs_close: bad serial port count for ttyS%d: %d\n",
967 info->line, info->count);
968 info->count = 0;
970 if (info->count) {
971 local_irq_restore(flags);
972 return;
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.
979 tty->closing = 1;
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);
991 shutdown(info);
992 if (tty->driver->flush_buffer)
993 tty->driver->flush_buffer(tty);
994 if (tty->ldisc.flush_buffer)
995 tty->ldisc.flush_buffer(tty);
996 tty->closing = 0;
997 info->event = 0;
998 info->tty = 0;
999 #if 0
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);
1008 #endif
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"))
1029 return;
1031 rs_flush_buffer(tty);
1032 shutdown(info);
1033 info->event = 0;
1034 info->count = 0;
1035 info->flags &= ~S_NORMAL_ACTIVE;
1036 info->tty = 0;
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);
1049 int retval;
1050 int do_clocal = 0;
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)
1060 return -EAGAIN;
1061 else
1062 return -ERESTARTSYS;
1063 #else
1064 return -EAGAIN;
1065 #endif
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;
1075 return 0;
1078 if (tty->termios->c_cflag & CLOCAL)
1079 do_clocal = 1;
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.
1088 retval = 0;
1089 add_wait_queue(&info->open_wait, &wait);
1091 info->count--;
1092 info->blocked_open++;
1093 while (1) {
1094 local_irq_disable();
1095 NIOS_rtsdtr(info, 1);
1096 local_irq_enable();
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)
1102 retval = -EAGAIN;
1103 else
1104 retval = -ERESTARTSYS;
1105 #else
1106 retval = -EAGAIN;
1107 #endif
1108 break;
1110 if (!(info->flags & S_CLOSING) && do_clocal)
1111 break;
1112 if (signal_pending(current)) {
1113 retval = -ERESTARTSYS;
1114 break;
1116 schedule();
1118 current->state = TASK_RUNNING;
1119 remove_wait_queue(&info->open_wait, &wait);
1120 if (!tty_hung_up_p(filp))
1121 info->count++;
1122 info->blocked_open--;
1124 if (retval)
1125 return retval;
1126 info->flags |= S_NORMAL_ACTIVE;
1127 return 0;
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;
1139 int retval, line;
1141 line = tty->index;
1143 if (line >=serial_driver->num) /* too many */
1144 return -ENODEV;
1146 info = &nios_soft[line];
1148 if (serial_paranoia_check(info, tty->name, "rs_open"))
1149 return -ENODEV;
1151 info->count++;
1152 tty->driver_data = info;
1153 info->tty = tty;
1156 * Start up serial port
1158 retval = startup(info);
1159 if (retval)
1160 return retval;
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 = {
1175 .open = rs_open,
1176 .close = rs_close,
1177 .write = rs_write,
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,
1182 .ioctl = rs_ioctl,
1183 .throttle = rs_throttle,
1184 .unthrottle = rs_unthrottle,
1185 .set_termios = rs_set_termios,
1186 .stop = rs_stop,
1187 .start = rs_start,
1188 .hangup = rs_hangup,
1189 .set_ldisc = rs_set_ldisc,
1192 /* rs_init inits the driver */
1193 static int __init rs_nios_init(void)
1195 int flags;
1196 struct NIOS_serial *info;
1197 int i;
1199 serial_driver = alloc_tty_driver(NR_PORTS);
1200 if (!serial_driver)
1201 return -ENOMEM;
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");
1222 return -ENOMEM;
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;
1232 info->tty = 0;
1233 info->custom_divisor = 16;
1234 info->close_delay = 50;
1235 info->closing_wait = 3000;
1236 info->x_char = 0;
1237 info->event = 0;
1238 info->count = 0;
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);
1244 info->line = i;
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);
1255 return 0;
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)
1265 return -1;
1268 void unregister_serial(int line)
1270 return;
1273 module_init(rs_nios_init);
1275 #ifdef CONFIG_NIOS_SERIAL_CONSOLE
1276 int nios_console_setup(struct console *cp, char *arg)
1278 return 0;
1282 static struct tty_driver *nios_console_device(struct console *c, int *index)
1284 *index = c->index;
1285 return serial_driver;
1289 void nios_console_write (struct console *co, const char *str,
1290 unsigned int count)
1292 while (count--) {
1293 if (*str == '\n')
1294 rs_put_char('\r', nios_soft);
1295 rs_put_char( *str++, nios_soft);
1300 static struct console nios_driver = {
1301 .name = "ttyS",
1302 .write = nios_console_write,
1303 .device = nios_console_device,
1304 .setup = nios_console_setup,
1305 .flags = CON_PRINTBUFFER,
1306 .index = -1,
1310 static int __init nios_console_init(void)
1312 register_console(&nios_driver);
1313 return 0;
1316 console_initcall(nios_console_init);
1317 #endif /* CONFIG_NIOS_SERIAL_CONSOLE */