1 /* 68328serial.c: Serial port driver for 68328 microcontroller
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port David McCullough
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/fcntl.h>
30 #include <linux/kernel.h>
31 #include <linux/console.h>
32 #include <linux/reboot.h>
33 #include <linux/keyboard.h>
34 #include <linux/init.h>
36 #include <linux/bitops.h>
37 #include <linux/delay.h>
38 #include <linux/gfp.h>
42 #include <asm/system.h>
43 #include <asm/delay.h>
44 #include <asm/uaccess.h>
47 /* note: perhaps we can murge these files, so that you can just
48 * define 1 of them, and they can sort that out for themselves
50 #if defined(CONFIG_M68EZ328)
51 #include <asm/MC68EZ328.h>
53 #if defined(CONFIG_M68VZ328)
54 #include <asm/MC68VZ328.h>
56 #include <asm/MC68328.h>
57 #endif /* CONFIG_M68VZ328 */
58 #endif /* CONFIG_M68EZ328 */
60 #include "68328serial.h"
62 /* Turn off usage of real serial interrupt code, to "support" Copilot */
63 #ifdef CONFIG_XCOPILOT_BUGS
69 static struct m68k_serial m68k_soft
[NR_PORTS
];
71 static unsigned int uart_irqs
[NR_PORTS
] = UART_IRQ_DEFNS
;
73 /* multiple ports are contiguous in memory */
74 m68328_uart
*uart_addr
= (m68328_uart
*)USTCNT_ADDR
;
76 struct tty_struct m68k_ttys
;
77 struct m68k_serial
*m68k_consinfo
= 0;
79 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
81 struct tty_driver
*serial_driver
;
83 /* number of characters left in xmit buffer before we ask for more */
84 #define WAKEUP_CHARS 256
86 /* Debugging... DEBUG_INTR is bad to use when one of the zs
87 * lines is your console ;(
89 #undef SERIAL_DEBUG_INTR
90 #undef SERIAL_DEBUG_OPEN
91 #undef SERIAL_DEBUG_FLOW
93 #define RS_ISR_PASS_LIMIT 256
95 static void change_speed(struct m68k_serial
*info
);
98 * Setup for console. Argument comes from the boot command line.
101 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
102 #define CONSOLE_BAUD_RATE 115200
103 #define DEFAULT_CBAUD B115200
106 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
107 #ifdef CONFIG_M68VZ328
108 #define CONSOLE_BAUD_RATE 19200
109 #define DEFAULT_CBAUD B19200
114 #ifndef CONSOLE_BAUD_RATE
115 #define CONSOLE_BAUD_RATE 9600
116 #define DEFAULT_CBAUD B9600
120 static int m68328_console_initted
= 0;
121 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
122 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
125 static inline int serial_paranoia_check(struct m68k_serial
*info
,
126 char *name
, const char *routine
)
128 #ifdef SERIAL_PARANOIA_CHECK
129 static const char *badmagic
=
130 "Warning: bad magic number for serial struct %s in %s\n";
131 static const char *badinfo
=
132 "Warning: null m68k_serial for %s in %s\n";
135 printk(badinfo
, name
, routine
);
138 if (info
->magic
!= SERIAL_MAGIC
) {
139 printk(badmagic
, name
, routine
);
147 * This is used to figure out the divisor speeds and the timeouts
149 static int baud_table
[] = {
150 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
151 9600, 19200, 38400, 57600, 115200, 0 };
153 /* Sets or clears DTR/RTS on the requested line */
154 static inline void m68k_rtsdtr(struct m68k_serial
*ss
, int set
)
157 /* set the RTS/CTS line */
164 /* Utility routines */
165 static inline int get_baud(struct m68k_serial
*ss
)
167 unsigned long result
= 115200;
168 unsigned short int baud
= uart_addr
[ss
->line
].ubaud
;
169 if (GET_FIELD(baud
, UBAUD_PRESCALER
) == 0x38) result
= 38400;
170 result
>>= GET_FIELD(baud
, UBAUD_DIVIDE
);
176 * ------------------------------------------------------------
177 * rs_stop() and rs_start()
179 * This routines are called before setting or resetting tty->stopped.
180 * They enable or disable transmitter interrupts, as necessary.
181 * ------------------------------------------------------------
183 static void rs_stop(struct tty_struct
*tty
)
185 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
186 m68328_uart
*uart
= &uart_addr
[info
->line
];
189 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
192 local_irq_save(flags
);
193 uart
->ustcnt
&= ~USTCNT_TXEN
;
194 local_irq_restore(flags
);
197 static int rs_put_char(char ch
)
199 int flags
, loops
= 0;
201 local_irq_save(flags
);
203 while (!(UTX
& UTX_TX_AVAIL
) && (loops
< 1000)) {
210 local_irq_restore(flags
);
214 static void rs_start(struct tty_struct
*tty
)
216 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
217 m68328_uart
*uart
= &uart_addr
[info
->line
];
220 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
223 local_irq_save(flags
);
224 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->ustcnt
& USTCNT_TXEN
)) {
226 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
228 uart
->ustcnt
|= USTCNT_TXEN
;
231 local_irq_restore(flags
);
234 /* Drop into either the boot monitor or kadb upon receiving a break
235 * from keyboard/console input.
237 static void batten_down_hatches(void)
239 /* Drop into the debugger */
242 static void status_handle(struct m68k_serial
*info
, unsigned short status
)
246 if((info
->port
.tty
->termios
->c_cflag
& CRTSCTS
) &&
247 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
248 info
->curregs
[3] |= AUTO_ENAB
;
249 info
->pendregs
[3] |= AUTO_ENAB
;
250 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
253 if((info
->curregs
[3] & AUTO_ENAB
)) {
254 info
->curregs
[3] &= ~AUTO_ENAB
;
255 info
->pendregs
[3] &= ~AUTO_ENAB
;
256 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
260 /* If this is console input and this is a
261 * 'break asserted' status change interrupt
262 * see if we can drop into the debugger
264 if((status
& URX_BREAK
) && info
->break_abort
)
265 batten_down_hatches();
269 static void receive_chars(struct m68k_serial
*info
, unsigned short rx
)
271 struct tty_struct
*tty
= info
->port
.tty
;
272 m68328_uart
*uart
= &uart_addr
[info
->line
];
273 unsigned char ch
, flag
;
276 * This do { } while() loop will get ALL chars out of Rx FIFO
278 #ifndef CONFIG_XCOPILOT_BUGS
281 ch
= GET_FIELD(rx
, URX_RXDATA
);
284 if(URX_BREAK
& rx
) { /* whee, break received */
285 status_handle(info
, rx
);
287 #ifdef CONFIG_MAGIC_SYSRQ
288 } else if (ch
== 0x10) { /* ^P */
292 /* show_net_buffers(); */
294 } else if (ch
== 0x12) { /* ^R */
297 #endif /* CONFIG_MAGIC_SYSRQ */
306 if(rx
& URX_PARITY_ERROR
) {
308 status_handle(info
, rx
);
309 } else if(rx
& URX_OVRUN
) {
311 status_handle(info
, rx
);
312 } else if(rx
& URX_FRAME_ERROR
) {
314 status_handle(info
, rx
);
316 tty_insert_flip_char(tty
, ch
, flag
);
317 #ifndef CONFIG_XCOPILOT_BUGS
318 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
321 tty_schedule_flip(tty
);
327 static void transmit_chars(struct m68k_serial
*info
)
329 m68328_uart
*uart
= &uart_addr
[info
->line
];
333 uart
->utx
.b
.txdata
= info
->x_char
;
335 goto clear_and_return
;
338 if((info
->xmit_cnt
<= 0) || info
->port
.tty
->stopped
) {
339 /* That's peculiar... TX ints off */
340 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
341 goto clear_and_return
;
345 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
346 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
349 if (info
->xmit_cnt
< WAKEUP_CHARS
)
350 schedule_work(&info
->tqueue
);
352 if(info
->xmit_cnt
<= 0) {
353 /* All done for now... TX ints off */
354 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
355 goto clear_and_return
;
359 /* Clear interrupt (should be auto)*/
364 * This is the serial driver's generic interrupt routine
366 irqreturn_t
rs_interrupt(int irq
, void *dev_id
)
368 struct m68k_serial
*info
= dev_id
;
373 uart
= &uart_addr
[info
->line
];
379 if (rx
& URX_DATA_READY
) receive_chars(info
, rx
);
380 if (tx
& UTX_TX_AVAIL
) transmit_chars(info
);
382 receive_chars(info
, rx
);
387 static void do_softint(struct work_struct
*work
)
389 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue
);
390 struct tty_struct
*tty
;
392 tty
= info
->port
.tty
;
396 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
403 * This routine is called from the scheduler tqueue when the interrupt
404 * routine has signalled that a hangup has occurred. The path of
405 * hangup processing is:
407 * serial interrupt routine -> (scheduler tqueue) ->
408 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
411 static void do_serial_hangup(struct work_struct
*work
)
413 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue_hangup
);
414 struct tty_struct
*tty
;
416 tty
= info
->port
.tty
;
424 static int startup(struct m68k_serial
* info
)
426 m68328_uart
*uart
= &uart_addr
[info
->line
];
429 if (info
->flags
& S_INITIALIZED
)
432 if (!info
->xmit_buf
) {
433 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
438 local_irq_save(flags
);
441 * Clear the FIFO buffers and disable them
442 * (they will be reenabled in change_speed())
445 uart
->ustcnt
= USTCNT_UEN
;
446 info
->xmit_fifo_size
= 1;
447 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
451 * Finally, enable sequencing and interrupts
454 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
455 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
457 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
461 clear_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
462 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
465 * and set the speed of the serial port
470 info
->flags
|= S_INITIALIZED
;
471 local_irq_restore(flags
);
476 * This routine will shutdown a serial port; interrupts are disabled, and
477 * DTR is dropped if the hangup on close termio flag is on.
479 static void shutdown(struct m68k_serial
* info
)
481 m68328_uart
*uart
= &uart_addr
[info
->line
];
484 uart
->ustcnt
= 0; /* All off! */
485 if (!(info
->flags
& S_INITIALIZED
))
488 local_irq_save(flags
);
490 if (info
->xmit_buf
) {
491 free_page((unsigned long) info
->xmit_buf
);
496 set_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
498 info
->flags
&= ~S_INITIALIZED
;
499 local_irq_restore(flags
);
503 int divisor
, prescale
;
505 #ifndef CONFIG_M68VZ328
506 hw_baud_table
[18] = {
521 {1,0x26}, /* 19200 */
522 {0,0x26}, /* 38400 */
523 {1,0x38}, /* 57600 */
524 {0,0x38}, /* 115200 */
527 hw_baud_table
[18] = {
542 {2,0x26}, /* 19200 */
543 {1,0x26}, /* 38400 */
544 {0,0x26}, /* 57600 */
545 {1,0x38}, /* 115200 */
548 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
551 * This routine is called to set the UART divisor registers to match
552 * the specified baud rate for a serial port.
554 static void change_speed(struct m68k_serial
*info
)
556 m68328_uart
*uart
= &uart_addr
[info
->line
];
558 unsigned short ustcnt
;
562 if (!info
->port
.tty
|| !info
->port
.tty
->termios
)
564 cflag
= info
->port
.tty
->termios
->c_cflag
;
565 if (!(port
= info
->port
))
568 ustcnt
= uart
->ustcnt
;
569 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
573 i
= (i
& ~CBAUDEX
) + B38400
;
576 info
->baud
= baud_table
[i
];
577 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
578 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
580 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
582 if ((cflag
& CSIZE
) == CS8
)
583 ustcnt
|= USTCNT_8_7
;
586 ustcnt
|= USTCNT_STOP
;
589 ustcnt
|= USTCNT_PARITYEN
;
591 ustcnt
|= USTCNT_ODD_EVEN
;
593 #ifdef CONFIG_SERIAL_68328_RTS_CTS
594 if (cflag
& CRTSCTS
) {
595 uart
->utx
.w
&= ~ UTX_NOCTS
;
597 uart
->utx
.w
|= UTX_NOCTS
;
601 ustcnt
|= USTCNT_TXEN
;
603 uart
->ustcnt
= ustcnt
;
608 * Fair output driver allows a process to speak.
610 static void rs_fair_output(void)
612 int left
; /* Output no more than that */
614 struct m68k_serial
*info
= &m68k_soft
[0];
617 if (info
== 0) return;
618 if (info
->xmit_buf
== 0) return;
620 local_irq_save(flags
);
621 left
= info
->xmit_cnt
;
623 c
= info
->xmit_buf
[info
->xmit_tail
];
624 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
626 local_irq_restore(flags
);
630 local_irq_save(flags
);
631 left
= min(info
->xmit_cnt
, left
-1);
634 /* Last character is being transmitted now (hopefully). */
637 local_irq_restore(flags
);
642 * m68k_console_print is registered for printk.
644 void console_print_68328(const char *p
)
648 while((c
=*(p
++)) != 0) {
654 /* Comment this if you want to have a strict interrupt-driven output */
660 static void rs_set_ldisc(struct tty_struct
*tty
)
662 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
664 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
667 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
669 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
672 static void rs_flush_chars(struct tty_struct
*tty
)
674 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
675 m68328_uart
*uart
= &uart_addr
[info
->line
];
678 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
684 /* Enable transmitter */
685 local_irq_save(flags
);
687 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
689 local_irq_restore(flags
);
694 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
696 uart
->ustcnt
|= USTCNT_TXEN
;
700 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
705 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
706 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
711 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
714 local_irq_restore(flags
);
717 extern void console_printn(const char * b
, int count
);
719 static int rs_write(struct tty_struct
* tty
,
720 const unsigned char *buf
, int count
)
723 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
724 m68328_uart
*uart
= &uart_addr
[info
->line
];
727 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
730 if (!tty
|| !info
->xmit_buf
)
733 local_save_flags(flags
);
736 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
737 SERIAL_XMIT_SIZE
- info
->xmit_head
));
738 local_irq_restore(flags
);
743 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
746 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
748 local_irq_restore(flags
);
754 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
755 /* Enable transmitter */
758 while(info
->xmit_cnt
) {
761 uart
->ustcnt
|= USTCNT_TXEN
;
763 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
765 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
767 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
768 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
769 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
776 local_irq_restore(flags
);
782 static int rs_write_room(struct tty_struct
*tty
)
784 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
787 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
789 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
795 static int rs_chars_in_buffer(struct tty_struct
*tty
)
797 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
799 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
801 return info
->xmit_cnt
;
804 static void rs_flush_buffer(struct tty_struct
*tty
)
806 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
809 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
811 local_irq_save(flags
);
812 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
813 local_irq_restore(flags
);
818 * ------------------------------------------------------------
821 * This routine is called by the upper-layer tty layer to signal that
822 * incoming characters should be throttled.
823 * ------------------------------------------------------------
825 static void rs_throttle(struct tty_struct
* tty
)
827 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
829 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
833 info
->x_char
= STOP_CHAR(tty
);
835 /* Turn off RTS line (do this atomic) */
838 static void rs_unthrottle(struct tty_struct
* tty
)
840 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
842 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
849 info
->x_char
= START_CHAR(tty
);
852 /* Assert RTS line (do this atomic) */
856 * ------------------------------------------------------------
857 * rs_ioctl() and friends
858 * ------------------------------------------------------------
861 static int get_serial_info(struct m68k_serial
* info
,
862 struct serial_struct
* retinfo
)
864 struct serial_struct tmp
;
868 memset(&tmp
, 0, sizeof(tmp
));
869 tmp
.type
= info
->type
;
870 tmp
.line
= info
->line
;
871 tmp
.port
= info
->port
;
873 tmp
.flags
= info
->flags
;
874 tmp
.baud_base
= info
->baud_base
;
875 tmp
.close_delay
= info
->close_delay
;
876 tmp
.closing_wait
= info
->closing_wait
;
877 tmp
.custom_divisor
= info
->custom_divisor
;
878 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
882 static int set_serial_info(struct m68k_serial
* info
,
883 struct serial_struct
* new_info
)
885 struct serial_struct new_serial
;
886 struct m68k_serial old_info
;
891 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
894 if (!capable(CAP_SYS_ADMIN
)) {
895 if ((new_serial
.baud_base
!= info
->baud_base
) ||
896 (new_serial
.type
!= info
->type
) ||
897 (new_serial
.close_delay
!= info
->close_delay
) ||
898 ((new_serial
.flags
& ~S_USR_MASK
) !=
899 (info
->flags
& ~S_USR_MASK
)))
901 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
902 (new_serial
.flags
& S_USR_MASK
));
903 info
->custom_divisor
= new_serial
.custom_divisor
;
911 * OK, past this point, all the error checking has been done.
912 * At this point, we start making changes.....
915 info
->baud_base
= new_serial
.baud_base
;
916 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
917 (new_serial
.flags
& S_FLAGS
));
918 info
->type
= new_serial
.type
;
919 info
->close_delay
= new_serial
.close_delay
;
920 info
->closing_wait
= new_serial
.closing_wait
;
923 retval
= startup(info
);
928 * get_lsr_info - get line status register info
930 * Purpose: Let user call ioctl() to get info when the UART physically
931 * is emptied. On bus types like RS485, the transmitter must
932 * release the bus after transmitting. This must be done when
933 * the transmit shift register is empty, not be done when the
934 * transmit holding register is empty. This functionality
935 * allows an RS485 driver to be written in user space.
937 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
939 #ifdef CONFIG_SERIAL_68328_RTS_CTS
940 m68328_uart
*uart
= &uart_addr
[info
->line
];
942 unsigned char status
;
945 local_irq_save(flags
);
946 #ifdef CONFIG_SERIAL_68328_RTS_CTS
947 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
951 local_irq_restore(flags
);
952 put_user(status
,value
);
957 * This routine sends a break character out the serial port.
959 static void send_break(struct m68k_serial
* info
, unsigned int duration
)
961 m68328_uart
*uart
= &uart_addr
[info
->line
];
965 local_irq_save(flags
);
967 uart
->utx
.w
|= UTX_SEND_BREAK
;
968 msleep_interruptible(duration
);
969 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
971 local_irq_restore(flags
);
974 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
975 unsigned int cmd
, unsigned long arg
)
978 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
981 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
984 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
985 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
986 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
987 if (tty
->flags
& (1 << TTY_IO_ERROR
))
992 case TCSBRK
: /* SVID version: non-zero arg --> no break */
993 retval
= tty_check_change(tty
);
996 tty_wait_until_sent(tty
, 0);
998 send_break(info
, 250); /* 1/4 second */
1000 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1001 retval
= tty_check_change(tty
);
1004 tty_wait_until_sent(tty
, 0);
1005 send_break(info
, arg
? arg
*(100) : 250);
1008 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1009 sizeof(struct serial_struct
)))
1010 return get_serial_info(info
,
1011 (struct serial_struct
*) arg
);
1014 return set_serial_info(info
,
1015 (struct serial_struct
*) arg
);
1016 case TIOCSERGETLSR
: /* Get line status register */
1017 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1018 sizeof(unsigned int)))
1019 return get_lsr_info(info
, (unsigned int *) arg
);
1021 case TIOCSERGSTRUCT
:
1022 if (!access_ok(VERIFY_WRITE
, (void *) arg
,
1023 sizeof(struct m68k_serial
)))
1025 copy_to_user((struct m68k_serial
*) arg
,
1026 info
, sizeof(struct m68k_serial
));
1030 return -ENOIOCTLCMD
;
1035 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1037 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
1041 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1042 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1043 tty
->hw_stopped
= 0;
1050 * ------------------------------------------------------------
1053 * This routine is called when the serial port gets closed. First, we
1054 * wait for the last remaining data to be sent. Then, we unlink its
1055 * S structure from the interrupt chain if necessary, and we free
1056 * that IRQ if nothing is left in the chain.
1057 * ------------------------------------------------------------
1059 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1061 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1062 m68328_uart
*uart
= &uart_addr
[info
->line
];
1063 unsigned long flags
;
1065 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1068 local_irq_save(flags
);
1070 if (tty_hung_up_p(filp
)) {
1071 local_irq_restore(flags
);
1075 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1077 * Uh, oh. tty->count is 1, which means that the tty
1078 * structure will be freed. Info->count should always
1079 * be one in these conditions. If it's greater than
1080 * one, we've got real problems, since it means the
1081 * serial port won't be shutdown.
1083 printk("rs_close: bad serial port count; tty->count is 1, "
1084 "info->count is %d\n", info
->count
);
1087 if (--info
->count
< 0) {
1088 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1089 info
->line
, info
->count
);
1093 local_irq_restore(flags
);
1096 info
->flags
|= S_CLOSING
;
1098 * Now we wait for the transmit buffer to clear; and we notify
1099 * the line discipline to only process XON/XOFF characters.
1102 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
1103 tty_wait_until_sent(tty
, info
->closing_wait
);
1105 * At this point we stop accepting input. To do this, we
1106 * disable the receive line status interrupts, and tell the
1107 * interrupt driver to stop checking the data ready bit in the
1108 * line status register.
1111 uart
->ustcnt
&= ~USTCNT_RXEN
;
1112 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1115 rs_flush_buffer(tty
);
1117 tty_ldisc_flush(tty
);
1120 info
->port
.tty
= NULL
;
1121 #warning "This is not and has never been valid so fix it"
1123 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1124 if (tty
->ldisc
.close
)
1125 (tty
->ldisc
.close
)(tty
);
1126 tty
->ldisc
= ldiscs
[N_TTY
];
1127 tty
->termios
->c_line
= N_TTY
;
1128 if (tty
->ldisc
.open
)
1129 (tty
->ldisc
.open
)(tty
);
1132 if (info
->blocked_open
) {
1133 if (info
->close_delay
) {
1134 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1136 wake_up_interruptible(&info
->open_wait
);
1138 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1139 wake_up_interruptible(&info
->close_wait
);
1140 local_irq_restore(flags
);
1144 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1146 void rs_hangup(struct tty_struct
*tty
)
1148 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1150 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1153 rs_flush_buffer(tty
);
1157 info
->flags
&= ~S_NORMAL_ACTIVE
;
1158 info
->port
.tty
= NULL
;
1159 wake_up_interruptible(&info
->open_wait
);
1163 * ------------------------------------------------------------
1164 * rs_open() and friends
1165 * ------------------------------------------------------------
1167 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1168 struct m68k_serial
*info
)
1170 DECLARE_WAITQUEUE(wait
, current
);
1175 * If the device is in the middle of being closed, then block
1176 * until it's done, and then try again.
1178 if (info
->flags
& S_CLOSING
) {
1179 interruptible_sleep_on(&info
->close_wait
);
1180 #ifdef SERIAL_DO_RESTART
1181 if (info
->flags
& S_HUP_NOTIFY
)
1184 return -ERESTARTSYS
;
1191 * If non-blocking mode is set, or the port is not enabled,
1192 * then make the check up front and then exit.
1194 if ((filp
->f_flags
& O_NONBLOCK
) ||
1195 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1196 info
->flags
|= S_NORMAL_ACTIVE
;
1200 if (tty
->termios
->c_cflag
& CLOCAL
)
1204 * Block waiting for the carrier detect and the line to become
1205 * free (i.e., not in use by the callout). While we are in
1206 * this loop, info->count is dropped by one, so that
1207 * rs_close() knows when to free things. We restore it upon
1208 * exit, either normal or abnormal.
1211 add_wait_queue(&info
->open_wait
, &wait
);
1214 info
->blocked_open
++;
1216 local_irq_disable();
1217 m68k_rtsdtr(info
, 1);
1219 current
->state
= TASK_INTERRUPTIBLE
;
1220 if (tty_hung_up_p(filp
) ||
1221 !(info
->flags
& S_INITIALIZED
)) {
1222 #ifdef SERIAL_DO_RESTART
1223 if (info
->flags
& S_HUP_NOTIFY
)
1226 retval
= -ERESTARTSYS
;
1232 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1234 if (signal_pending(current
)) {
1235 retval
= -ERESTARTSYS
;
1240 current
->state
= TASK_RUNNING
;
1241 remove_wait_queue(&info
->open_wait
, &wait
);
1242 if (!tty_hung_up_p(filp
))
1244 info
->blocked_open
--;
1248 info
->flags
|= S_NORMAL_ACTIVE
;
1253 * This routine is called whenever a serial port is opened. It
1254 * enables interrupts for a serial port, linking in its S structure into
1255 * the IRQ chain. It also performs the serial-specific
1256 * initialization for the tty structure.
1258 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1260 struct m68k_serial
*info
;
1265 if (line
>= NR_PORTS
|| line
< 0) /* we have exactly one */
1268 info
= &m68k_soft
[line
];
1270 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1274 tty
->driver_data
= info
;
1275 info
->port
.tty
= tty
;
1278 * Start up serial port
1280 retval
= startup(info
);
1284 return block_til_ready(tty
, filp
, info
);
1287 /* Finally, routines used to initialize the serial driver. */
1289 static void show_serial_version(void)
1291 printk("MC68328 serial driver version 1.00\n");
1294 static const struct tty_operations rs_ops
= {
1298 .flush_chars
= rs_flush_chars
,
1299 .write_room
= rs_write_room
,
1300 .chars_in_buffer
= rs_chars_in_buffer
,
1301 .flush_buffer
= rs_flush_buffer
,
1303 .throttle
= rs_throttle
,
1304 .unthrottle
= rs_unthrottle
,
1305 .set_termios
= rs_set_termios
,
1308 .hangup
= rs_hangup
,
1309 .set_ldisc
= rs_set_ldisc
,
1312 /* rs_init inits the driver */
1317 struct m68k_serial
*info
;
1319 serial_driver
= alloc_tty_driver(NR_PORTS
);
1323 show_serial_version();
1325 /* Initialize the tty_driver structure */
1326 /* SPARC: Not all of this is exactly right for us. */
1328 serial_driver
->name
= "ttyS";
1329 serial_driver
->major
= TTY_MAJOR
;
1330 serial_driver
->minor_start
= 64;
1331 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1332 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1333 serial_driver
->init_termios
= tty_std_termios
;
1334 serial_driver
->init_termios
.c_cflag
=
1335 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1336 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1337 tty_set_operations(serial_driver
, &rs_ops
);
1339 if (tty_register_driver(serial_driver
)) {
1340 put_tty_driver(serial_driver
);
1341 printk(KERN_ERR
"Couldn't register serial driver\n");
1345 local_irq_save(flags
);
1347 for(i
=0;i
<NR_PORTS
;i
++) {
1349 info
= &m68k_soft
[i
];
1350 info
->magic
= SERIAL_MAGIC
;
1351 info
->port
= (int) &uart_addr
[i
];
1352 info
->port
.tty
= NULL
;
1353 info
->irq
= uart_irqs
[i
];
1354 info
->custom_divisor
= 16;
1355 info
->close_delay
= 50;
1356 info
->closing_wait
= 3000;
1360 info
->blocked_open
= 0;
1361 INIT_WORK(&info
->tqueue
, do_softint
);
1362 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
);
1363 init_waitqueue_head(&info
->open_wait
);
1364 init_waitqueue_head(&info
->close_wait
);
1366 info
->is_cons
= 1; /* Means shortcuts work */
1368 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1369 info
->port
, info
->irq
);
1370 printk(" is a builtin MC68328 UART\n");
1372 #ifdef CONFIG_M68VZ328
1374 PJSEL
&= 0xCF; /* PSW enable second port output */
1377 if (request_irq(uart_irqs
[i
],
1380 "M68328_UART", info
))
1381 panic("Unable to attach 68328 serial interrupt\n");
1383 local_irq_restore(flags
);
1387 module_init(rs68328_init
);
1391 static void m68328_set_baud(void)
1393 unsigned short ustcnt
;
1397 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1400 for (i
= 0; i
< ARRAY_SIZE(baud_table
); i
++)
1401 if (baud_table
[i
] == m68328_console_baud
)
1403 if (i
>= ARRAY_SIZE(baud_table
)) {
1404 m68328_console_baud
= 9600;
1408 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1409 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1410 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1411 ustcnt
|= USTCNT_8_7
;
1412 ustcnt
|= USTCNT_TXEN
;
1414 m68328_console_initted
= 1;
1419 int m68328_console_setup(struct console
*cp
, char *arg
)
1421 int i
, n
= CONSOLE_BAUD_RATE
;
1427 n
= simple_strtoul(arg
,NULL
,0);
1429 for (i
= 0; i
< ARRAY_SIZE(baud_table
); i
++)
1430 if (baud_table
[i
] == n
)
1432 if (i
< ARRAY_SIZE(baud_table
)) {
1433 m68328_console_baud
= n
;
1434 m68328_console_cbaud
= 0;
1436 m68328_console_cbaud
|= CBAUDEX
;
1439 m68328_console_cbaud
|= i
;
1442 m68328_set_baud(); /* make sure baud rate changes */
1447 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1450 return serial_driver
;
1454 void m68328_console_write (struct console
*co
, const char *str
,
1457 if (!m68328_console_initted
)
1462 rs_put_char( *str
++ );
1467 static struct console m68328_driver
= {
1469 .write
= m68328_console_write
,
1470 .device
= m68328_console_device
,
1471 .setup
= m68328_console_setup
,
1472 .flags
= CON_PRINTBUFFER
,
1477 static int __init
m68328_console_init(void)
1479 register_console(&m68328_driver
);
1483 console_initcall(m68328_console_init
);
This page took 0.090262 seconds and 5 git commands to generate.