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>
41 #include <asm/system.h>
42 #include <asm/delay.h>
43 #include <asm/uaccess.h>
46 /* note: perhaps we can murge these files, so that you can just
47 * define 1 of them, and they can sort that out for themselves
49 #if defined(CONFIG_M68EZ328)
50 #include <asm/MC68EZ328.h>
52 #if defined(CONFIG_M68VZ328)
53 #include <asm/MC68VZ328.h>
55 #include <asm/MC68328.h>
56 #endif /* CONFIG_M68VZ328 */
57 #endif /* CONFIG_M68EZ328 */
59 #include "68328serial.h"
61 /* Turn off usage of real serial interrupt code, to "support" Copilot */
62 #ifdef CONFIG_XCOPILOT_BUGS
68 static struct m68k_serial m68k_soft
[NR_PORTS
];
69 struct m68k_serial
*IRQ_ports
[NR_IRQS
];
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 */
82 extern wait_queue_head_t keypress_wait
;
85 struct tty_driver
*serial_driver
;
87 /* number of characters left in xmit buffer before we ask for more */
88 #define WAKEUP_CHARS 256
90 /* Debugging... DEBUG_INTR is bad to use when one of the zs
91 * lines is your console ;(
93 #undef SERIAL_DEBUG_INTR
94 #undef SERIAL_DEBUG_OPEN
95 #undef SERIAL_DEBUG_FLOW
97 #define RS_ISR_PASS_LIMIT 256
99 static void change_speed(struct m68k_serial
*info
);
102 * Setup for console. Argument comes from the boot command line.
105 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
106 #define CONSOLE_BAUD_RATE 115200
107 #define DEFAULT_CBAUD B115200
110 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
111 #ifdef CONFIG_M68VZ328
112 #define CONSOLE_BAUD_RATE 19200
113 #define DEFAULT_CBAUD B19200
118 #ifndef CONSOLE_BAUD_RATE
119 #define CONSOLE_BAUD_RATE 9600
120 #define DEFAULT_CBAUD B9600
124 static int m68328_console_initted
= 0;
125 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
126 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
129 static inline int serial_paranoia_check(struct m68k_serial
*info
,
130 char *name
, const char *routine
)
132 #ifdef SERIAL_PARANOIA_CHECK
133 static const char *badmagic
=
134 "Warning: bad magic number for serial struct %s in %s\n";
135 static const char *badinfo
=
136 "Warning: null m68k_serial for %s in %s\n";
139 printk(badinfo
, name
, routine
);
142 if (info
->magic
!= SERIAL_MAGIC
) {
143 printk(badmagic
, name
, routine
);
151 * This is used to figure out the divisor speeds and the timeouts
153 static int baud_table
[] = {
154 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
155 9600, 19200, 38400, 57600, 115200, 0 };
157 #define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
159 /* Sets or clears DTR/RTS on the requested line */
160 static inline void m68k_rtsdtr(struct m68k_serial
*ss
, int set
)
163 /* set the RTS/CTS line */
170 /* Utility routines */
171 static inline int get_baud(struct m68k_serial
*ss
)
173 unsigned long result
= 115200;
174 unsigned short int baud
= uart_addr
[ss
->line
].ubaud
;
175 if (GET_FIELD(baud
, UBAUD_PRESCALER
) == 0x38) result
= 38400;
176 result
>>= GET_FIELD(baud
, UBAUD_DIVIDE
);
182 * ------------------------------------------------------------
183 * rs_stop() and rs_start()
185 * This routines are called before setting or resetting tty->stopped.
186 * They enable or disable transmitter interrupts, as necessary.
187 * ------------------------------------------------------------
189 static void rs_stop(struct tty_struct
*tty
)
191 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
192 m68328_uart
*uart
= &uart_addr
[info
->line
];
195 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
198 local_irq_save(flags
);
199 uart
->ustcnt
&= ~USTCNT_TXEN
;
200 local_irq_restore(flags
);
203 static int rs_put_char(char ch
)
205 int flags
, loops
= 0;
207 local_irq_save(flags
);
209 while (!(UTX
& UTX_TX_AVAIL
) && (loops
< 1000)) {
216 local_irq_restore(flags
);
220 static void rs_start(struct tty_struct
*tty
)
222 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
223 m68328_uart
*uart
= &uart_addr
[info
->line
];
226 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
229 local_irq_save(flags
);
230 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->ustcnt
& USTCNT_TXEN
)) {
232 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
234 uart
->ustcnt
|= USTCNT_TXEN
;
237 local_irq_restore(flags
);
240 /* Drop into either the boot monitor or kadb upon receiving a break
241 * from keyboard/console input.
243 static void batten_down_hatches(void)
245 /* Drop into the debugger */
248 static void status_handle(struct m68k_serial
*info
, unsigned short status
)
252 if((info
->port
.tty
->termios
->c_cflag
& CRTSCTS
) &&
253 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
254 info
->curregs
[3] |= AUTO_ENAB
;
255 info
->pendregs
[3] |= AUTO_ENAB
;
256 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
259 if((info
->curregs
[3] & AUTO_ENAB
)) {
260 info
->curregs
[3] &= ~AUTO_ENAB
;
261 info
->pendregs
[3] &= ~AUTO_ENAB
;
262 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
266 /* If this is console input and this is a
267 * 'break asserted' status change interrupt
268 * see if we can drop into the debugger
270 if((status
& URX_BREAK
) && info
->break_abort
)
271 batten_down_hatches();
275 static void receive_chars(struct m68k_serial
*info
, unsigned short rx
)
277 struct tty_struct
*tty
= info
->port
.tty
;
278 m68328_uart
*uart
= &uart_addr
[info
->line
];
279 unsigned char ch
, flag
;
282 * This do { } while() loop will get ALL chars out of Rx FIFO
284 #ifndef CONFIG_XCOPILOT_BUGS
287 ch
= GET_FIELD(rx
, URX_RXDATA
);
290 if(URX_BREAK
& rx
) { /* whee, break received */
291 status_handle(info
, rx
);
293 #ifdef CONFIG_MAGIC_SYSRQ
294 } else if (ch
== 0x10) { /* ^P */
298 /* show_net_buffers(); */
300 } else if (ch
== 0x12) { /* ^R */
303 #endif /* CONFIG_MAGIC_SYSRQ */
305 /* It is a 'keyboard interrupt' ;-) */
306 #ifdef CONFIG_CONSOLE
307 wake_up(&keypress_wait
);
316 if(rx
& URX_PARITY_ERROR
) {
318 status_handle(info
, rx
);
319 } else if(rx
& URX_OVRUN
) {
321 status_handle(info
, rx
);
322 } else if(rx
& URX_FRAME_ERROR
) {
324 status_handle(info
, rx
);
326 tty_insert_flip_char(tty
, ch
, flag
);
327 #ifndef CONFIG_XCOPILOT_BUGS
328 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
331 tty_schedule_flip(tty
);
337 static void transmit_chars(struct m68k_serial
*info
)
339 m68328_uart
*uart
= &uart_addr
[info
->line
];
343 uart
->utx
.b
.txdata
= info
->x_char
;
345 goto clear_and_return
;
348 if((info
->xmit_cnt
<= 0) || info
->port
.tty
->stopped
) {
349 /* That's peculiar... TX ints off */
350 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
351 goto clear_and_return
;
355 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
356 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
359 if (info
->xmit_cnt
< WAKEUP_CHARS
)
360 schedule_work(&info
->tqueue
);
362 if(info
->xmit_cnt
<= 0) {
363 /* All done for now... TX ints off */
364 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
365 goto clear_and_return
;
369 /* Clear interrupt (should be auto)*/
374 * This is the serial driver's generic interrupt routine
376 irqreturn_t
rs_interrupt(int irq
, void *dev_id
)
378 struct m68k_serial
* info
;
383 info
= IRQ_ports
[irq
];
387 uart
= &uart_addr
[info
->line
];
393 if (rx
& URX_DATA_READY
) receive_chars(info
, rx
);
394 if (tx
& UTX_TX_AVAIL
) transmit_chars(info
);
396 receive_chars(info
, rx
);
401 static void do_softint(struct work_struct
*work
)
403 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue
);
404 struct tty_struct
*tty
;
406 tty
= info
->port
.tty
;
410 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
417 * This routine is called from the scheduler tqueue when the interrupt
418 * routine has signalled that a hangup has occurred. The path of
419 * hangup processing is:
421 * serial interrupt routine -> (scheduler tqueue) ->
422 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
425 static void do_serial_hangup(struct work_struct
*work
)
427 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue_hangup
);
428 struct tty_struct
*tty
;
430 tty
= info
->port
.tty
;
438 static int startup(struct m68k_serial
* info
)
440 m68328_uart
*uart
= &uart_addr
[info
->line
];
443 if (info
->flags
& S_INITIALIZED
)
446 if (!info
->xmit_buf
) {
447 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
452 local_irq_save(flags
);
455 * Clear the FIFO buffers and disable them
456 * (they will be reenabled in change_speed())
459 uart
->ustcnt
= USTCNT_UEN
;
460 info
->xmit_fifo_size
= 1;
461 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
465 * Finally, enable sequencing and interrupts
468 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
469 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
471 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
475 clear_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
476 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
479 * and set the speed of the serial port
484 info
->flags
|= S_INITIALIZED
;
485 local_irq_restore(flags
);
490 * This routine will shutdown a serial port; interrupts are disabled, and
491 * DTR is dropped if the hangup on close termio flag is on.
493 static void shutdown(struct m68k_serial
* info
)
495 m68328_uart
*uart
= &uart_addr
[info
->line
];
498 uart
->ustcnt
= 0; /* All off! */
499 if (!(info
->flags
& S_INITIALIZED
))
502 local_irq_save(flags
);
504 if (info
->xmit_buf
) {
505 free_page((unsigned long) info
->xmit_buf
);
510 set_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
512 info
->flags
&= ~S_INITIALIZED
;
513 local_irq_restore(flags
);
517 int divisor
, prescale
;
519 #ifndef CONFIG_M68VZ328
520 hw_baud_table
[18] = {
535 {1,0x26}, /* 19200 */
536 {0,0x26}, /* 38400 */
537 {1,0x38}, /* 57600 */
538 {0,0x38}, /* 115200 */
541 hw_baud_table
[18] = {
556 {2,0x26}, /* 19200 */
557 {1,0x26}, /* 38400 */
558 {0,0x26}, /* 57600 */
559 {1,0x38}, /* 115200 */
562 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
565 * This routine is called to set the UART divisor registers to match
566 * the specified baud rate for a serial port.
568 static void change_speed(struct m68k_serial
*info
)
570 m68328_uart
*uart
= &uart_addr
[info
->line
];
572 unsigned short ustcnt
;
576 if (!info
->port
.tty
|| !info
->port
.tty
->termios
)
578 cflag
= info
->port
.tty
->termios
->c_cflag
;
579 if (!(port
= info
->port
))
582 ustcnt
= uart
->ustcnt
;
583 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
587 i
= (i
& ~CBAUDEX
) + B38400
;
590 info
->baud
= baud_table
[i
];
591 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
592 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
594 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
596 if ((cflag
& CSIZE
) == CS8
)
597 ustcnt
|= USTCNT_8_7
;
600 ustcnt
|= USTCNT_STOP
;
603 ustcnt
|= USTCNT_PARITYEN
;
605 ustcnt
|= USTCNT_ODD_EVEN
;
607 #ifdef CONFIG_SERIAL_68328_RTS_CTS
608 if (cflag
& CRTSCTS
) {
609 uart
->utx
.w
&= ~ UTX_NOCTS
;
611 uart
->utx
.w
|= UTX_NOCTS
;
615 ustcnt
|= USTCNT_TXEN
;
617 uart
->ustcnt
= ustcnt
;
622 * Fair output driver allows a process to speak.
624 static void rs_fair_output(void)
626 int left
; /* Output no more than that */
628 struct m68k_serial
*info
= &m68k_soft
[0];
631 if (info
== 0) return;
632 if (info
->xmit_buf
== 0) return;
634 local_irq_save(flags
);
635 left
= info
->xmit_cnt
;
637 c
= info
->xmit_buf
[info
->xmit_tail
];
638 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
640 local_irq_restore(flags
);
644 local_irq_save(flags
);
645 left
= min(info
->xmit_cnt
, left
-1);
648 /* Last character is being transmitted now (hopefully). */
651 local_irq_restore(flags
);
656 * m68k_console_print is registered for printk.
658 void console_print_68328(const char *p
)
662 while((c
=*(p
++)) != 0) {
668 /* Comment this if you want to have a strict interrupt-driven output */
674 static void rs_set_ldisc(struct tty_struct
*tty
)
676 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
678 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
681 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
683 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
686 static void rs_flush_chars(struct tty_struct
*tty
)
688 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
689 m68328_uart
*uart
= &uart_addr
[info
->line
];
692 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
698 /* Enable transmitter */
699 local_irq_save(flags
);
701 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
703 local_irq_restore(flags
);
708 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
710 uart
->ustcnt
|= USTCNT_TXEN
;
714 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
719 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
720 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
725 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
728 local_irq_restore(flags
);
731 extern void console_printn(const char * b
, int count
);
733 static int rs_write(struct tty_struct
* tty
,
734 const unsigned char *buf
, int count
)
737 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
738 m68328_uart
*uart
= &uart_addr
[info
->line
];
741 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
744 if (!tty
|| !info
->xmit_buf
)
747 local_save_flags(flags
);
750 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
751 SERIAL_XMIT_SIZE
- info
->xmit_head
));
752 local_irq_restore(flags
);
757 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
760 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
762 local_irq_restore(flags
);
768 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
769 /* Enable transmitter */
772 while(info
->xmit_cnt
) {
775 uart
->ustcnt
|= USTCNT_TXEN
;
777 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
779 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
781 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
782 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
783 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
790 local_irq_restore(flags
);
796 static int rs_write_room(struct tty_struct
*tty
)
798 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
801 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
803 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
809 static int rs_chars_in_buffer(struct tty_struct
*tty
)
811 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
813 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
815 return info
->xmit_cnt
;
818 static void rs_flush_buffer(struct tty_struct
*tty
)
820 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
823 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
825 local_irq_save(flags
);
826 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
827 local_irq_restore(flags
);
832 * ------------------------------------------------------------
835 * This routine is called by the upper-layer tty layer to signal that
836 * incoming characters should be throttled.
837 * ------------------------------------------------------------
839 static void rs_throttle(struct tty_struct
* tty
)
841 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
843 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
847 info
->x_char
= STOP_CHAR(tty
);
849 /* Turn off RTS line (do this atomic) */
852 static void rs_unthrottle(struct tty_struct
* tty
)
854 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
856 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
863 info
->x_char
= START_CHAR(tty
);
866 /* Assert RTS line (do this atomic) */
870 * ------------------------------------------------------------
871 * rs_ioctl() and friends
872 * ------------------------------------------------------------
875 static int get_serial_info(struct m68k_serial
* info
,
876 struct serial_struct
* retinfo
)
878 struct serial_struct tmp
;
882 memset(&tmp
, 0, sizeof(tmp
));
883 tmp
.type
= info
->type
;
884 tmp
.line
= info
->line
;
885 tmp
.port
= info
->port
;
887 tmp
.flags
= info
->flags
;
888 tmp
.baud_base
= info
->baud_base
;
889 tmp
.close_delay
= info
->close_delay
;
890 tmp
.closing_wait
= info
->closing_wait
;
891 tmp
.custom_divisor
= info
->custom_divisor
;
892 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
896 static int set_serial_info(struct m68k_serial
* info
,
897 struct serial_struct
* new_info
)
899 struct serial_struct new_serial
;
900 struct m68k_serial old_info
;
905 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
908 if (!capable(CAP_SYS_ADMIN
)) {
909 if ((new_serial
.baud_base
!= info
->baud_base
) ||
910 (new_serial
.type
!= info
->type
) ||
911 (new_serial
.close_delay
!= info
->close_delay
) ||
912 ((new_serial
.flags
& ~S_USR_MASK
) !=
913 (info
->flags
& ~S_USR_MASK
)))
915 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
916 (new_serial
.flags
& S_USR_MASK
));
917 info
->custom_divisor
= new_serial
.custom_divisor
;
925 * OK, past this point, all the error checking has been done.
926 * At this point, we start making changes.....
929 info
->baud_base
= new_serial
.baud_base
;
930 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
931 (new_serial
.flags
& S_FLAGS
));
932 info
->type
= new_serial
.type
;
933 info
->close_delay
= new_serial
.close_delay
;
934 info
->closing_wait
= new_serial
.closing_wait
;
937 retval
= startup(info
);
942 * get_lsr_info - get line status register info
944 * Purpose: Let user call ioctl() to get info when the UART physically
945 * is emptied. On bus types like RS485, the transmitter must
946 * release the bus after transmitting. This must be done when
947 * the transmit shift register is empty, not be done when the
948 * transmit holding register is empty. This functionality
949 * allows an RS485 driver to be written in user space.
951 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
953 #ifdef CONFIG_SERIAL_68328_RTS_CTS
954 m68328_uart
*uart
= &uart_addr
[info
->line
];
956 unsigned char status
;
959 local_irq_save(flags
);
960 #ifdef CONFIG_SERIAL_68328_RTS_CTS
961 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
965 local_irq_restore(flags
);
966 put_user(status
,value
);
971 * This routine sends a break character out the serial port.
973 static void send_break(struct m68k_serial
* info
, unsigned int duration
)
975 m68328_uart
*uart
= &uart_addr
[info
->line
];
979 local_irq_save(flags
);
981 uart
->utx
.w
|= UTX_SEND_BREAK
;
982 msleep_interruptible(duration
);
983 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
985 local_irq_restore(flags
);
988 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
989 unsigned int cmd
, unsigned long arg
)
992 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
995 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
998 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
999 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1000 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1001 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1006 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1007 retval
= tty_check_change(tty
);
1010 tty_wait_until_sent(tty
, 0);
1012 send_break(info
, 250); /* 1/4 second */
1014 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1015 retval
= tty_check_change(tty
);
1018 tty_wait_until_sent(tty
, 0);
1019 send_break(info
, arg
? arg
*(100) : 250);
1022 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1023 sizeof(struct serial_struct
)))
1024 return get_serial_info(info
,
1025 (struct serial_struct
*) arg
);
1028 return set_serial_info(info
,
1029 (struct serial_struct
*) arg
);
1030 case TIOCSERGETLSR
: /* Get line status register */
1031 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1032 sizeof(unsigned int)))
1033 return get_lsr_info(info
, (unsigned int *) arg
);
1035 case TIOCSERGSTRUCT
:
1036 if (!access_ok(VERIFY_WRITE
, (void *) arg
,
1037 sizeof(struct m68k_serial
)))
1039 copy_to_user((struct m68k_serial
*) arg
,
1040 info
, sizeof(struct m68k_serial
));
1044 return -ENOIOCTLCMD
;
1049 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1051 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
1055 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1056 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1057 tty
->hw_stopped
= 0;
1064 * ------------------------------------------------------------
1067 * This routine is called when the serial port gets closed. First, we
1068 * wait for the last remaining data to be sent. Then, we unlink its
1069 * S structure from the interrupt chain if necessary, and we free
1070 * that IRQ if nothing is left in the chain.
1071 * ------------------------------------------------------------
1073 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1075 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1076 m68328_uart
*uart
= &uart_addr
[info
->line
];
1077 unsigned long flags
;
1079 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1082 local_irq_save(flags
);
1084 if (tty_hung_up_p(filp
)) {
1085 local_irq_restore(flags
);
1089 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1091 * Uh, oh. tty->count is 1, which means that the tty
1092 * structure will be freed. Info->count should always
1093 * be one in these conditions. If it's greater than
1094 * one, we've got real problems, since it means the
1095 * serial port won't be shutdown.
1097 printk("rs_close: bad serial port count; tty->count is 1, "
1098 "info->count is %d\n", info
->count
);
1101 if (--info
->count
< 0) {
1102 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1103 info
->line
, info
->count
);
1107 local_irq_restore(flags
);
1110 info
->flags
|= S_CLOSING
;
1112 * Now we wait for the transmit buffer to clear; and we notify
1113 * the line discipline to only process XON/XOFF characters.
1116 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
1117 tty_wait_until_sent(tty
, info
->closing_wait
);
1119 * At this point we stop accepting input. To do this, we
1120 * disable the receive line status interrupts, and tell the
1121 * interrupt driver to stop checking the data ready bit in the
1122 * line status register.
1125 uart
->ustcnt
&= ~USTCNT_RXEN
;
1126 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1129 rs_flush_buffer(tty
);
1131 tty_ldisc_flush(tty
);
1134 info
->port
.tty
= NULL
;
1135 #warning "This is not and has never been valid so fix it"
1137 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1138 if (tty
->ldisc
.close
)
1139 (tty
->ldisc
.close
)(tty
);
1140 tty
->ldisc
= ldiscs
[N_TTY
];
1141 tty
->termios
->c_line
= N_TTY
;
1142 if (tty
->ldisc
.open
)
1143 (tty
->ldisc
.open
)(tty
);
1146 if (info
->blocked_open
) {
1147 if (info
->close_delay
) {
1148 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1150 wake_up_interruptible(&info
->open_wait
);
1152 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1153 wake_up_interruptible(&info
->close_wait
);
1154 local_irq_restore(flags
);
1158 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1160 void rs_hangup(struct tty_struct
*tty
)
1162 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1164 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1167 rs_flush_buffer(tty
);
1171 info
->flags
&= ~S_NORMAL_ACTIVE
;
1172 info
->port
.tty
= NULL
;
1173 wake_up_interruptible(&info
->open_wait
);
1177 * ------------------------------------------------------------
1178 * rs_open() and friends
1179 * ------------------------------------------------------------
1181 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1182 struct m68k_serial
*info
)
1184 DECLARE_WAITQUEUE(wait
, current
);
1189 * If the device is in the middle of being closed, then block
1190 * until it's done, and then try again.
1192 if (info
->flags
& S_CLOSING
) {
1193 interruptible_sleep_on(&info
->close_wait
);
1194 #ifdef SERIAL_DO_RESTART
1195 if (info
->flags
& S_HUP_NOTIFY
)
1198 return -ERESTARTSYS
;
1205 * If non-blocking mode is set, or the port is not enabled,
1206 * then make the check up front and then exit.
1208 if ((filp
->f_flags
& O_NONBLOCK
) ||
1209 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1210 info
->flags
|= S_NORMAL_ACTIVE
;
1214 if (tty
->termios
->c_cflag
& CLOCAL
)
1218 * Block waiting for the carrier detect and the line to become
1219 * free (i.e., not in use by the callout). While we are in
1220 * this loop, info->count is dropped by one, so that
1221 * rs_close() knows when to free things. We restore it upon
1222 * exit, either normal or abnormal.
1225 add_wait_queue(&info
->open_wait
, &wait
);
1228 info
->blocked_open
++;
1230 local_irq_disable();
1231 m68k_rtsdtr(info
, 1);
1233 current
->state
= TASK_INTERRUPTIBLE
;
1234 if (tty_hung_up_p(filp
) ||
1235 !(info
->flags
& S_INITIALIZED
)) {
1236 #ifdef SERIAL_DO_RESTART
1237 if (info
->flags
& S_HUP_NOTIFY
)
1240 retval
= -ERESTARTSYS
;
1246 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1248 if (signal_pending(current
)) {
1249 retval
= -ERESTARTSYS
;
1254 current
->state
= TASK_RUNNING
;
1255 remove_wait_queue(&info
->open_wait
, &wait
);
1256 if (!tty_hung_up_p(filp
))
1258 info
->blocked_open
--;
1262 info
->flags
|= S_NORMAL_ACTIVE
;
1267 * This routine is called whenever a serial port is opened. It
1268 * enables interrupts for a serial port, linking in its S structure into
1269 * the IRQ chain. It also performs the serial-specific
1270 * initialization for the tty structure.
1272 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1274 struct m68k_serial
*info
;
1279 if (line
>= NR_PORTS
|| line
< 0) /* we have exactly one */
1282 info
= &m68k_soft
[line
];
1284 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1288 tty
->driver_data
= info
;
1289 info
->port
.tty
= tty
;
1292 * Start up serial port
1294 retval
= startup(info
);
1298 return block_til_ready(tty
, filp
, info
);
1301 /* Finally, routines used to initialize the serial driver. */
1303 static void show_serial_version(void)
1305 printk("MC68328 serial driver version 1.00\n");
1308 static const struct tty_operations rs_ops
= {
1312 .flush_chars
= rs_flush_chars
,
1313 .write_room
= rs_write_room
,
1314 .chars_in_buffer
= rs_chars_in_buffer
,
1315 .flush_buffer
= rs_flush_buffer
,
1317 .throttle
= rs_throttle
,
1318 .unthrottle
= rs_unthrottle
,
1319 .set_termios
= rs_set_termios
,
1322 .hangup
= rs_hangup
,
1323 .set_ldisc
= rs_set_ldisc
,
1326 /* rs_init inits the driver */
1331 struct m68k_serial
*info
;
1333 serial_driver
= alloc_tty_driver(NR_PORTS
);
1337 show_serial_version();
1339 /* Initialize the tty_driver structure */
1340 /* SPARC: Not all of this is exactly right for us. */
1342 serial_driver
->name
= "ttyS";
1343 serial_driver
->major
= TTY_MAJOR
;
1344 serial_driver
->minor_start
= 64;
1345 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1346 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1347 serial_driver
->init_termios
= tty_std_termios
;
1348 serial_driver
->init_termios
.c_cflag
=
1349 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1350 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1351 tty_set_operations(serial_driver
, &rs_ops
);
1353 if (tty_register_driver(serial_driver
)) {
1354 put_tty_driver(serial_driver
);
1355 printk(KERN_ERR
"Couldn't register serial driver\n");
1359 local_irq_save(flags
);
1361 for(i
=0;i
<NR_PORTS
;i
++) {
1363 info
= &m68k_soft
[i
];
1364 info
->magic
= SERIAL_MAGIC
;
1365 info
->port
= (int) &uart_addr
[i
];
1366 info
->port
.tty
= NULL
;
1367 info
->irq
= uart_irqs
[i
];
1368 info
->custom_divisor
= 16;
1369 info
->close_delay
= 50;
1370 info
->closing_wait
= 3000;
1374 info
->blocked_open
= 0;
1375 INIT_WORK(&info
->tqueue
, do_softint
);
1376 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
);
1377 init_waitqueue_head(&info
->open_wait
);
1378 init_waitqueue_head(&info
->close_wait
);
1380 info
->is_cons
= 1; /* Means shortcuts work */
1382 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1383 info
->port
, info
->irq
);
1384 printk(" is a builtin MC68328 UART\n");
1386 IRQ_ports
[info
->irq
] = info
; /* waste of space */
1388 #ifdef CONFIG_M68VZ328
1390 PJSEL
&= 0xCF; /* PSW enable second port output */
1393 if (request_irq(uart_irqs
[i
],
1396 "M68328_UART", NULL
))
1397 panic("Unable to attach 68328 serial interrupt\n");
1399 local_irq_restore(flags
);
1403 module_init(rs68328_init
);
1407 static void m68328_set_baud(void)
1409 unsigned short ustcnt
;
1413 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1416 for (i
= 0; i
< sizeof(baud_table
) / sizeof(baud_table
[0]); i
++)
1417 if (baud_table
[i
] == m68328_console_baud
)
1419 if (i
>= sizeof(baud_table
) / sizeof(baud_table
[0])) {
1420 m68328_console_baud
= 9600;
1424 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1425 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1426 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1427 ustcnt
|= USTCNT_8_7
;
1428 ustcnt
|= USTCNT_TXEN
;
1430 m68328_console_initted
= 1;
1435 int m68328_console_setup(struct console
*cp
, char *arg
)
1437 int i
, n
= CONSOLE_BAUD_RATE
;
1443 n
= simple_strtoul(arg
,NULL
,0);
1445 for (i
= 0; i
< BAUD_TABLE_SIZE
; i
++)
1446 if (baud_table
[i
] == n
)
1448 if (i
< BAUD_TABLE_SIZE
) {
1449 m68328_console_baud
= n
;
1450 m68328_console_cbaud
= 0;
1452 m68328_console_cbaud
|= CBAUDEX
;
1455 m68328_console_cbaud
|= i
;
1458 m68328_set_baud(); /* make sure baud rate changes */
1463 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1466 return serial_driver
;
1470 void m68328_console_write (struct console
*co
, const char *str
,
1473 if (!m68328_console_initted
)
1478 rs_put_char( *str
++ );
1483 static struct console m68328_driver
= {
1485 .write
= m68328_console_write
,
1486 .device
= m68328_console_device
,
1487 .setup
= m68328_console_setup
,
1488 .flags
= CON_PRINTBUFFER
,
1493 static int __init
m68328_console_init(void)
1495 register_console(&m68328_driver
);
1499 console_initcall(m68328_console_init
);