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 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/config.h>
27 #include <linux/major.h>
28 #include <linux/string.h>
29 #include <linux/fcntl.h>
31 #include <linux/kernel.h>
32 #include <linux/console.h>
33 #include <linux/reboot.h>
34 #include <linux/keyboard.h>
35 #include <linux/init.h>
40 #include <asm/system.h>
41 #include <asm/segment.h>
42 #include <asm/bitops.h>
43 #include <asm/delay.h>
44 #include <asm/uaccess.h>
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
];
70 struct m86k_serial
*IRQ_ports
[NR_IRQS
];
72 static unsigned int uart_irqs
[NR_PORTS
] = UART_IRQ_DEFNS
;
74 /* multiple ports are contiguous in memory */
75 m68328_uart
*uart_addr
= USTCNT_ADDR
;
77 struct tty_struct m68k_ttys
;
78 struct m68k_serial
*m68k_consinfo
= 0;
80 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
83 extern wait_queue_head_t keypress_wait
;
86 struct tty_driver
*serial_driver
;
88 /* serial subtype definitions */
89 #define SERIAL_TYPE_NORMAL 1
91 /* number of characters left in xmit buffer before we ask for more */
92 #define WAKEUP_CHARS 256
94 /* Debugging... DEBUG_INTR is bad to use when one of the zs
95 * lines is your console ;(
97 #undef SERIAL_DEBUG_INTR
98 #undef SERIAL_DEBUG_OPEN
99 #undef SERIAL_DEBUG_FLOW
101 #define RS_ISR_PASS_LIMIT 256
103 #define _INLINE_ inline
105 static void change_speed(struct m68k_serial
*info
);
108 * Setup for console. Argument comes from the boot command line.
111 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
112 #define CONSOLE_BAUD_RATE 115200
113 #define DEFAULT_CBAUD B115200
116 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
117 #ifdef CONFIG_M68VZ328
118 #define CONSOLE_BAUD_RATE 19200
119 #define DEFAULT_CBAUD B19200
124 #ifndef CONSOLE_BAUD_RATE
125 #define CONSOLE_BAUD_RATE 9600
126 #define DEFAULT_CBAUD B9600
130 static int m68328_console_initted
= 0;
131 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
132 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
136 * tmp_buf is used as a temporary buffer by serial_write. We need to
137 * lock it in case the memcpy_fromfs blocks while swapping in a page,
138 * and some other program tries to do a serial write at the same time.
139 * Since the lock will only come under contention when the system is
140 * swapping and available memory is low, it makes sense to share one
141 * buffer across all the serial ports, since it significantly saves
142 * memory if large numbers of serial ports are open.
144 static unsigned char tmp_buf
[SERIAL_XMIT_SIZE
]; /* This is cheating */
145 DECLARE_MUTEX(tmp_buf_sem
);
147 static inline int serial_paranoia_check(struct m68k_serial
*info
,
148 char *name
, const char *routine
)
150 #ifdef SERIAL_PARANOIA_CHECK
151 static const char *badmagic
=
152 "Warning: bad magic number for serial struct %s in %s\n";
153 static const char *badinfo
=
154 "Warning: null m68k_serial for %s in %s\n";
157 printk(badinfo
, name
, routine
);
160 if (info
->magic
!= SERIAL_MAGIC
) {
161 printk(badmagic
, name
, routine
);
169 * This is used to figure out the divisor speeds and the timeouts
171 static int baud_table
[] = {
172 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
173 9600, 19200, 38400, 57600, 115200, 0 };
175 #define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
177 /* Sets or clears DTR/RTS on the requested line */
178 static inline void m68k_rtsdtr(struct m68k_serial
*ss
, int set
)
181 /* set the RTS/CTS line */
188 /* Utility routines */
189 static inline int get_baud(struct m68k_serial
*ss
)
191 unsigned long result
= 115200;
192 unsigned short int baud
= uart_addr
[ss
->line
].ubaud
;
193 if (GET_FIELD(baud
, UBAUD_PRESCALER
) == 0x38) result
= 38400;
194 result
>>= GET_FIELD(baud
, UBAUD_DIVIDE
);
200 * ------------------------------------------------------------
201 * rs_stop() and rs_start()
203 * This routines are called before setting or resetting tty->stopped.
204 * They enable or disable transmitter interrupts, as necessary.
205 * ------------------------------------------------------------
207 static void rs_stop(struct tty_struct
*tty
)
209 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
210 m68328_uart
*uart
= &uart_addr
[info
->line
];
213 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
216 save_flags(flags
); cli();
217 uart
->ustcnt
&= ~USTCNT_TXEN
;
218 restore_flags(flags
);
221 static void rs_put_char(char ch
)
223 int flags
, loops
= 0;
225 save_flags(flags
); cli();
227 while (!(UTX
& UTX_TX_AVAIL
) && (loops
< 1000)) {
234 restore_flags(flags
);
237 static void rs_start(struct tty_struct
*tty
)
239 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
240 m68328_uart
*uart
= &uart_addr
[info
->line
];
243 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
246 save_flags(flags
); cli();
247 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->ustcnt
& USTCNT_TXEN
)) {
249 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
251 uart
->ustcnt
|= USTCNT_TXEN
;
254 restore_flags(flags
);
257 /* Drop into either the boot monitor or kadb upon receiving a break
258 * from keyboard/console input.
260 static void batten_down_hatches(void)
262 /* Drop into the debugger */
265 static _INLINE_
void status_handle(struct m68k_serial
*info
, unsigned short status
)
269 if((info
->tty
->termios
->c_cflag
& CRTSCTS
) &&
270 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
271 info
->curregs
[3] |= AUTO_ENAB
;
272 info
->pendregs
[3] |= AUTO_ENAB
;
273 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
276 if((info
->curregs
[3] & AUTO_ENAB
)) {
277 info
->curregs
[3] &= ~AUTO_ENAB
;
278 info
->pendregs
[3] &= ~AUTO_ENAB
;
279 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
283 /* If this is console input and this is a
284 * 'break asserted' status change interrupt
285 * see if we can drop into the debugger
287 if((status
& URX_BREAK
) && info
->break_abort
)
288 batten_down_hatches();
292 static _INLINE_
void receive_chars(struct m68k_serial
*info
, struct pt_regs
*regs
, unsigned short rx
)
294 struct tty_struct
*tty
= info
->tty
;
295 m68328_uart
*uart
= &uart_addr
[info
->line
];
299 * This do { } while() loop will get ALL chars out of Rx FIFO
301 #ifndef CONFIG_XCOPILOT_BUGS
304 ch
= GET_FIELD(rx
, URX_RXDATA
);
307 if(URX_BREAK
& rx
) { /* whee, break received */
308 status_handle(info
, rx
);
310 #ifdef CONFIG_MAGIC_SYSRQ
311 } else if (ch
== 0x10) { /* ^P */
315 /* show_net_buffers(); */
317 } else if (ch
== 0x12) { /* ^R */
318 machine_restart(NULL
);
320 #endif /* CONFIG_MAGIC_SYSRQ */
322 /* It is a 'keyboard interrupt' ;-) */
323 #ifdef CONFIG_CONSOLE
324 wake_up(&keypress_wait
);
332 * Make sure that we do not overflow the buffer
334 if (tty
->flip
.count
>= TTY_FLIPBUF_SIZE
) {
335 schedule_work(&tty
->flip
.work
);
339 if(rx
& URX_PARITY_ERROR
) {
340 *tty
->flip
.flag_buf_ptr
++ = TTY_PARITY
;
341 status_handle(info
, rx
);
342 } else if(rx
& URX_OVRUN
) {
343 *tty
->flip
.flag_buf_ptr
++ = TTY_OVERRUN
;
344 status_handle(info
, rx
);
345 } else if(rx
& URX_FRAME_ERROR
) {
346 *tty
->flip
.flag_buf_ptr
++ = TTY_FRAME
;
347 status_handle(info
, rx
);
349 *tty
->flip
.flag_buf_ptr
++ = 0; /* XXX */
351 *tty
->flip
.char_buf_ptr
++ = ch
;
354 #ifndef CONFIG_XCOPILOT_BUGS
355 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
358 schedule_work(&tty
->flip
.work
);
364 static _INLINE_
void transmit_chars(struct m68k_serial
*info
)
366 m68328_uart
*uart
= &uart_addr
[info
->line
];
370 uart
->utx
.b
.txdata
= info
->x_char
;
372 goto clear_and_return
;
375 if((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
376 /* That's peculiar... TX ints off */
377 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
378 goto clear_and_return
;
382 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
383 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
386 if (info
->xmit_cnt
< WAKEUP_CHARS
)
387 schedule_work(&info
->tqueue
);
389 if(info
->xmit_cnt
<= 0) {
390 /* All done for now... TX ints off */
391 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
392 goto clear_and_return
;
396 /* Clear interrupt (should be auto)*/
401 * This is the serial driver's generic interrupt routine
403 void rs_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
405 struct m68k_serial
* info
;
410 info
= IRQ_ports
[irq
];
414 uart
= &uart_addr
[info
->line
];
420 if (rx
& URX_DATA_READY
) receive_chars(info
, regs
, rx
);
421 if (tx
& UTX_TX_AVAIL
) transmit_chars(info
);
423 receive_chars(info
, regs
, rx
);
428 static void do_softint(void *private)
430 struct m68k_serial
*info
= (struct m68k_serial
*) private;
431 struct tty_struct
*tty
;
437 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
438 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
439 tty
->ldisc
.write_wakeup
)
440 (tty
->ldisc
.write_wakeup
)(tty
);
441 wake_up_interruptible(&tty
->write_wait
);
447 * This routine is called from the scheduler tqueue when the interrupt
448 * routine has signalled that a hangup has occurred. The path of
449 * hangup processing is:
451 * serial interrupt routine -> (scheduler tqueue) ->
452 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
455 static void do_serial_hangup(void *private)
457 struct m68k_serial
*info
= (struct m68k_serial
*) private;
458 struct tty_struct
*tty
;
468 static int startup(struct m68k_serial
* info
)
470 m68328_uart
*uart
= &uart_addr
[info
->line
];
473 if (info
->flags
& S_INITIALIZED
)
476 if (!info
->xmit_buf
) {
477 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
482 save_flags(flags
); cli();
485 * Clear the FIFO buffers and disable them
486 * (they will be reenabled in change_speed())
489 uart
->ustcnt
= USTCNT_UEN
;
490 info
->xmit_fifo_size
= 1;
491 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
495 * Finally, enable sequencing and interrupts
498 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
499 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
501 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
505 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
506 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
509 * and set the speed of the serial port
514 info
->flags
|= S_INITIALIZED
;
515 restore_flags(flags
);
520 * This routine will shutdown a serial port; interrupts are disabled, and
521 * DTR is dropped if the hangup on close termio flag is on.
523 static void shutdown(struct m68k_serial
* info
)
525 m68328_uart
*uart
= &uart_addr
[info
->line
];
528 uart
->ustcnt
= 0; /* All off! */
529 if (!(info
->flags
& S_INITIALIZED
))
532 save_flags(flags
); cli(); /* Disable interrupts */
534 if (info
->xmit_buf
) {
535 free_page((unsigned long) info
->xmit_buf
);
540 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
542 info
->flags
&= ~S_INITIALIZED
;
543 restore_flags(flags
);
547 int divisor
, prescale
;
549 #ifndef CONFIG_M68VZ328
550 hw_baud_table
[18] = {
565 {1,0x26}, /* 19200 */
566 {0,0x26}, /* 38400 */
567 {1,0x38}, /* 57600 */
568 {0,0x38}, /* 115200 */
571 hw_baud_table
[18] = {
586 {2,0x26}, /* 19200 */
587 {1,0x26}, /* 38400 */
588 {0,0x26}, /* 57600 */
589 {1,0x38}, /* 115200 */
592 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
595 * This routine is called to set the UART divisor registers to match
596 * the specified baud rate for a serial port.
598 static void change_speed(struct m68k_serial
*info
)
600 m68328_uart
*uart
= &uart_addr
[info
->line
];
602 unsigned short ustcnt
;
606 if (!info
->tty
|| !info
->tty
->termios
)
608 cflag
= info
->tty
->termios
->c_cflag
;
609 if (!(port
= info
->port
))
612 ustcnt
= uart
->ustcnt
;
613 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
617 i
= (i
& ~CBAUDEX
) + B38400
;
620 info
->baud
= baud_table
[i
];
621 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
622 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
624 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
626 if ((cflag
& CSIZE
) == CS8
)
627 ustcnt
|= USTCNT_8_7
;
630 ustcnt
|= USTCNT_STOP
;
633 ustcnt
|= USTCNT_PARITYEN
;
635 ustcnt
|= USTCNT_ODD_EVEN
;
637 #ifdef CONFIG_SERIAL_68328_RTS_CTS
638 if (cflag
& CRTSCTS
) {
639 uart
->utx
.w
&= ~ UTX_NOCTS
;
641 uart
->utx
.w
|= UTX_NOCTS
;
645 ustcnt
|= USTCNT_TXEN
;
647 uart
->ustcnt
= ustcnt
;
652 * Fair output driver allows a process to speak.
654 static void rs_fair_output(void)
656 int left
; /* Output no more than that */
658 struct m68k_serial
*info
= &m68k_soft
[0];
661 if (info
== 0) return;
662 if (info
->xmit_buf
== 0) return;
664 save_flags(flags
); cli();
665 left
= info
->xmit_cnt
;
667 c
= info
->xmit_buf
[info
->xmit_tail
];
668 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
670 restore_flags(flags
);
674 save_flags(flags
); cli();
675 left
= min(info
->xmit_cnt
, left
-1);
678 /* Last character is being transmitted now (hopefully). */
681 restore_flags(flags
);
686 * m68k_console_print is registered for printk.
688 void console_print_68328(const char *p
)
692 while((c
=*(p
++)) != 0) {
698 /* Comment this if you want to have a strict interrupt-driven output */
704 static void rs_set_ldisc(struct tty_struct
*tty
)
706 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
708 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
711 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
713 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
716 static void rs_flush_chars(struct tty_struct
*tty
)
718 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
719 m68328_uart
*uart
= &uart_addr
[info
->line
];
722 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
728 /* Enable transmitter */
729 save_flags(flags
); cli();
731 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
733 restore_flags(flags
);
738 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
740 uart
->ustcnt
|= USTCNT_TXEN
;
744 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
749 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
750 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
755 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
758 restore_flags(flags
);
761 extern void console_printn(const char * b
, int count
);
763 static int rs_write(struct tty_struct
* tty
, int from_user
,
764 const unsigned char *buf
, int count
)
767 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
768 m68328_uart
*uart
= &uart_addr
[info
->line
];
771 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
774 if (!tty
|| !info
->xmit_buf
)
780 c
= min(count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
781 SERIAL_XMIT_SIZE
- info
->xmit_head
));
787 copy_from_user(tmp_buf
, buf
, c
);
788 c
= min(c
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
789 SERIAL_XMIT_SIZE
- info
->xmit_head
));
790 memcpy(info
->xmit_buf
+ info
->xmit_head
, tmp_buf
, c
);
793 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
794 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
796 restore_flags(flags
);
802 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
803 /* Enable transmitter */
806 while(info
->xmit_cnt
) {
809 uart
->ustcnt
|= USTCNT_TXEN
;
811 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
813 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
815 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
816 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
817 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
824 restore_flags(flags
);
826 restore_flags(flags
);
830 static int rs_write_room(struct tty_struct
*tty
)
832 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
835 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
837 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
843 static int rs_chars_in_buffer(struct tty_struct
*tty
)
845 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
847 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
849 return info
->xmit_cnt
;
852 static void rs_flush_buffer(struct tty_struct
*tty
)
854 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
856 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
859 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
861 wake_up_interruptible(&tty
->write_wait
);
862 if ((tty
->flags
& (1 << TTY_DO_WRITE_WAKEUP
)) &&
863 tty
->ldisc
.write_wakeup
)
864 (tty
->ldisc
.write_wakeup
)(tty
);
868 * ------------------------------------------------------------
871 * This routine is called by the upper-layer tty layer to signal that
872 * incoming characters should be throttled.
873 * ------------------------------------------------------------
875 static void rs_throttle(struct tty_struct
* tty
)
877 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
879 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
883 info
->x_char
= STOP_CHAR(tty
);
885 /* Turn off RTS line (do this atomic) */
888 static void rs_unthrottle(struct tty_struct
* tty
)
890 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
892 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
899 info
->x_char
= START_CHAR(tty
);
902 /* Assert RTS line (do this atomic) */
906 * ------------------------------------------------------------
907 * rs_ioctl() and friends
908 * ------------------------------------------------------------
911 static int get_serial_info(struct m68k_serial
* info
,
912 struct serial_struct
* retinfo
)
914 struct serial_struct tmp
;
918 memset(&tmp
, 0, sizeof(tmp
));
919 tmp
.type
= info
->type
;
920 tmp
.line
= info
->line
;
921 tmp
.port
= info
->port
;
923 tmp
.flags
= info
->flags
;
924 tmp
.baud_base
= info
->baud_base
;
925 tmp
.close_delay
= info
->close_delay
;
926 tmp
.closing_wait
= info
->closing_wait
;
927 tmp
.custom_divisor
= info
->custom_divisor
;
928 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
932 static int set_serial_info(struct m68k_serial
* info
,
933 struct serial_struct
* new_info
)
935 struct serial_struct new_serial
;
936 struct m68k_serial old_info
;
941 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
944 if (!capable(CAP_SYS_ADMIN
)) {
945 if ((new_serial
.baud_base
!= info
->baud_base
) ||
946 (new_serial
.type
!= info
->type
) ||
947 (new_serial
.close_delay
!= info
->close_delay
) ||
948 ((new_serial
.flags
& ~S_USR_MASK
) !=
949 (info
->flags
& ~S_USR_MASK
)))
951 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
952 (new_serial
.flags
& S_USR_MASK
));
953 info
->custom_divisor
= new_serial
.custom_divisor
;
961 * OK, past this point, all the error checking has been done.
962 * At this point, we start making changes.....
965 info
->baud_base
= new_serial
.baud_base
;
966 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
967 (new_serial
.flags
& S_FLAGS
));
968 info
->type
= new_serial
.type
;
969 info
->close_delay
= new_serial
.close_delay
;
970 info
->closing_wait
= new_serial
.closing_wait
;
973 retval
= startup(info
);
978 * get_lsr_info - get line status register info
980 * Purpose: Let user call ioctl() to get info when the UART physically
981 * is emptied. On bus types like RS485, the transmitter must
982 * release the bus after transmitting. This must be done when
983 * the transmit shift register is empty, not be done when the
984 * transmit holding register is empty. This functionality
985 * allows an RS485 driver to be written in user space.
987 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
989 #ifdef CONFIG_SERIAL_68328_RTS_CTS
990 m68328_uart
*uart
= &uart_addr
[info
->line
];
992 unsigned char status
;
995 #ifdef CONFIG_SERIAL_68328_RTS_CTS
996 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
1001 put_user(status
,value
);
1006 * This routine sends a break character out the serial port.
1008 static void send_break( struct m68k_serial
* info
, int duration
)
1010 m68328_uart
*uart
= &uart_addr
[info
->line
];
1011 unsigned long flags
;
1014 current
->state
= TASK_INTERRUPTIBLE
;
1018 uart
->utx
.w
|= UTX_SEND_BREAK
;
1019 schedule_timeout(duration
);
1020 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
1022 restore_flags(flags
);
1025 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1026 unsigned int cmd
, unsigned long arg
)
1029 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1032 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1035 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1036 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1037 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1038 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1043 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1044 retval
= tty_check_change(tty
);
1047 tty_wait_until_sent(tty
, 0);
1049 send_break(info
, HZ
/4); /* 1/4 second */
1051 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1052 retval
= tty_check_change(tty
);
1055 tty_wait_until_sent(tty
, 0);
1056 send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
1059 error
= verify_area(VERIFY_WRITE
, (void *) arg
,sizeof(long));
1062 put_user(C_CLOCAL(tty
) ? 1 : 0,
1063 (unsigned long *) arg
);
1066 get_user(arg
, (unsigned long *) arg
);
1067 tty
->termios
->c_cflag
=
1068 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1069 (arg
? CLOCAL
: 0));
1072 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1073 sizeof(struct serial_struct
));
1076 return get_serial_info(info
,
1077 (struct serial_struct
*) arg
);
1079 return set_serial_info(info
,
1080 (struct serial_struct
*) arg
);
1081 case TIOCSERGETLSR
: /* Get line status register */
1082 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1083 sizeof(unsigned int));
1087 return get_lsr_info(info
, (unsigned int *) arg
);
1089 case TIOCSERGSTRUCT
:
1090 error
= verify_area(VERIFY_WRITE
, (void *) arg
,
1091 sizeof(struct m68k_serial
));
1094 copy_to_user((struct m68k_serial
*) arg
,
1095 info
, sizeof(struct m68k_serial
));
1099 return -ENOIOCTLCMD
;
1104 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1106 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
1108 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1113 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1114 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1115 tty
->hw_stopped
= 0;
1122 * ------------------------------------------------------------
1125 * This routine is called when the serial port gets closed. First, we
1126 * wait for the last remaining data to be sent. Then, we unlink its
1127 * S structure from the interrupt chain if necessary, and we free
1128 * that IRQ if nothing is left in the chain.
1129 * ------------------------------------------------------------
1131 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1133 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1134 m68328_uart
*uart
= &uart_addr
[info
->line
];
1135 unsigned long flags
;
1137 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1140 save_flags(flags
); cli();
1142 if (tty_hung_up_p(filp
)) {
1143 restore_flags(flags
);
1147 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1149 * Uh, oh. tty->count is 1, which means that the tty
1150 * structure will be freed. Info->count should always
1151 * be one in these conditions. If it's greater than
1152 * one, we've got real problems, since it means the
1153 * serial port won't be shutdown.
1155 printk("rs_close: bad serial port count; tty->count is 1, "
1156 "info->count is %d\n", info
->count
);
1159 if (--info
->count
< 0) {
1160 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1161 info
->line
, info
->count
);
1165 restore_flags(flags
);
1168 info
->flags
|= S_CLOSING
;
1170 * Now we wait for the transmit buffer to clear; and we notify
1171 * the line discipline to only process XON/XOFF characters.
1174 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
1175 tty_wait_until_sent(tty
, info
->closing_wait
);
1177 * At this point we stop accepting input. To do this, we
1178 * disable the receive line status interrupts, and tell the
1179 * interrupt driver to stop checking the data ready bit in the
1180 * line status register.
1183 uart
->ustcnt
&= ~USTCNT_RXEN
;
1184 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1187 if (tty
->driver
->flush_buffer
)
1188 tty
->driver
->flush_buffer(tty
);
1189 if (tty
->ldisc
.flush_buffer
)
1190 tty
->ldisc
.flush_buffer(tty
);
1194 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1195 if (tty
->ldisc
.close
)
1196 (tty
->ldisc
.close
)(tty
);
1197 tty
->ldisc
= ldiscs
[N_TTY
];
1198 tty
->termios
->c_line
= N_TTY
;
1199 if (tty
->ldisc
.open
)
1200 (tty
->ldisc
.open
)(tty
);
1202 if (info
->blocked_open
) {
1203 if (info
->close_delay
) {
1204 current
->state
= TASK_INTERRUPTIBLE
;
1205 schedule_timeout(info
->close_delay
);
1207 wake_up_interruptible(&info
->open_wait
);
1209 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1210 wake_up_interruptible(&info
->close_wait
);
1211 restore_flags(flags
);
1215 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1217 void rs_hangup(struct tty_struct
*tty
)
1219 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1221 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1224 rs_flush_buffer(tty
);
1228 info
->flags
&= ~S_NORMAL_ACTIVE
;
1230 wake_up_interruptible(&info
->open_wait
);
1234 * ------------------------------------------------------------
1235 * rs_open() and friends
1236 * ------------------------------------------------------------
1238 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1239 struct m68k_serial
*info
)
1241 DECLARE_WAITQUEUE(wait
, current
);
1246 * If the device is in the middle of being closed, then block
1247 * until it's done, and then try again.
1249 if (info
->flags
& S_CLOSING
) {
1250 interruptible_sleep_on(&info
->close_wait
);
1251 #ifdef SERIAL_DO_RESTART
1252 if (info
->flags
& S_HUP_NOTIFY
)
1255 return -ERESTARTSYS
;
1262 * If non-blocking mode is set, or the port is not enabled,
1263 * then make the check up front and then exit.
1265 if ((filp
->f_flags
& O_NONBLOCK
) ||
1266 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1267 info
->flags
|= S_NORMAL_ACTIVE
;
1271 if (tty
->termios
->c_cflag
& CLOCAL
)
1275 * Block waiting for the carrier detect and the line to become
1276 * free (i.e., not in use by the callout). While we are in
1277 * this loop, info->count is dropped by one, so that
1278 * rs_close() knows when to free things. We restore it upon
1279 * exit, either normal or abnormal.
1282 add_wait_queue(&info
->open_wait
, &wait
);
1285 info
->blocked_open
++;
1288 m68k_rtsdtr(info
, 1);
1290 current
->state
= TASK_INTERRUPTIBLE
;
1291 if (tty_hung_up_p(filp
) ||
1292 !(info
->flags
& S_INITIALIZED
)) {
1293 #ifdef SERIAL_DO_RESTART
1294 if (info
->flags
& S_HUP_NOTIFY
)
1297 retval
= -ERESTARTSYS
;
1303 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1305 if (signal_pending(current
)) {
1306 retval
= -ERESTARTSYS
;
1311 current
->state
= TASK_RUNNING
;
1312 remove_wait_queue(&info
->open_wait
, &wait
);
1313 if (!tty_hung_up_p(filp
))
1315 info
->blocked_open
--;
1319 info
->flags
|= S_NORMAL_ACTIVE
;
1324 * This routine is called whenever a serial port is opened. It
1325 * enables interrupts for a serial port, linking in its S structure into
1326 * the IRQ chain. It also performs the serial-specific
1327 * initialization for the tty structure.
1329 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1331 struct m68k_serial
*info
;
1336 if (line
>= NR_PORTS
|| line
< 0) /* we have exactly one */
1339 info
= &m68k_soft
[line
];
1341 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1345 tty
->driver_data
= info
;
1349 * Start up serial port
1351 retval
= startup(info
);
1355 return block_til_ready(tty
, filp
, info
);
1358 /* Finally, routines used to initialize the serial driver. */
1360 static void show_serial_version(void)
1362 printk("MC68328 serial driver version 1.00\n");
1366 /* Serial Power management
1367 * The console (currently fixed at line 0) is a special case for power
1368 * management because the kernel is so chatty. The console will be
1369 * explicitly disabled my our power manager as the last minute, so we won't
1370 * mess with it here.
1372 static struct pm_dev
*serial_pm
[NR_PORTS
];
1374 static int serial_pm_callback(struct pm_dev
*dev
, pm_request_t request
, void *data
)
1376 struct m68k_serial
*info
= (struct m68k_serial
*)dev
->data
;
1381 /* special case for line 0 - pm restores it */
1397 void shutdown_console(void)
1399 struct m68k_serial
*info
= &m68k_soft
[0];
1401 /* HACK: wait a bit for any pending printk's to be dumped */
1410 void startup_console(void)
1412 struct m68k_serial
*info
= &m68k_soft
[0];
1418 static struct tty_operations rs_ops
= {
1422 .flush_chars
= rs_flush_chars
,
1423 .write_room
= rs_write_room
,
1424 .chars_in_buffer
= rs_chars_in_buffer
,
1425 .flush_buffer
= rs_flush_buffer
,
1427 .throttle
= rs_throttle
,
1428 .unthrottle
= rs_unthrottle
,
1429 .set_termios
= rs_set_termios
,
1432 .hangup
= rs_hangup
,
1433 .set_ldisc
= rs_set_ldisc
,
1436 /* rs_init inits the driver */
1441 struct m68k_serial
*info
;
1443 serial_driver
= alloc_tty_driver(NR_PORTS
);
1447 show_serial_version();
1449 /* Initialize the tty_driver structure */
1450 /* SPARC: Not all of this is exactly right for us. */
1452 serial_driver
->name
= "ttyS";
1453 serial_driver
->major
= TTY_MAJOR
;
1454 serial_driver
->minor_start
= 64;
1455 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1456 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1457 serial_driver
->init_termios
= tty_std_termios
;
1458 serial_driver
->init_termios
.c_cflag
=
1459 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1460 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1461 tty_set_operations(serial_driver
, &rs_ops
);
1463 if (tty_register_driver(serial_driver
)) {
1464 put_tty_driver(serial_driver
);
1465 printk(KERN_ERR
"Couldn't register serial driver\n");
1469 save_flags(flags
); cli();
1471 for(i
=0;i
<NR_PORTS
;i
++) {
1473 info
= &m68k_soft
[i
];
1474 info
->magic
= SERIAL_MAGIC
;
1475 info
->port
= (int) &uart_addr
[i
];
1477 info
->irq
= uart_irqs
[i
];
1478 info
->custom_divisor
= 16;
1479 info
->close_delay
= 50;
1480 info
->closing_wait
= 3000;
1484 info
->blocked_open
= 0;
1485 INIT_WORK(&info
->tqueue
, do_softint
, info
);
1486 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
, info
);
1487 init_waitqueue_head(&info
->open_wait
);
1488 init_waitqueue_head(&info
->close_wait
);
1490 info
->is_cons
= 1; /* Means shortcuts work */
1492 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1493 info
->port
, info
->irq
);
1494 printk(" is a builtin MC68328 UART\n");
1496 IRQ_ports
[info
->irq
] = info
; /* waste of space */
1498 #ifdef CONFIG_M68VZ328
1500 PJSEL
&= 0xCF; /* PSW enable second port output */
1503 if (request_irq(uart_irqs
[i
],
1506 "M68328_UART", NULL
))
1507 panic("Unable to attach 68328 serial interrupt\n");
1509 serial_pm
[i
] = pm_register(PM_SYS_DEV
, PM_SYS_COM
, serial_pm_callback
);
1511 serial_pm
[i
]->data
= info
;
1514 restore_flags(flags
);
1521 * register_serial and unregister_serial allows for serial ports to be
1522 * configured at run-time, to support PCMCIA modems.
1524 /* SPARC: Unused at this time, just here to make things link. */
1525 int register_serial(struct serial_struct
*req
)
1530 void unregister_serial(int line
)
1535 module_init(rs68328_init
);
1536 /* DAVIDM module_exit(rs68328_fini); */
1540 static void m68328_set_baud(void)
1542 unsigned short ustcnt
;
1546 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1549 for (i
= 0; i
< sizeof(baud_table
) / sizeof(baud_table
[0]); i
++)
1550 if (baud_table
[i
] == m68328_console_baud
)
1552 if (i
>= sizeof(baud_table
) / sizeof(baud_table
[0])) {
1553 m68328_console_baud
= 9600;
1557 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1558 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1559 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1560 ustcnt
|= USTCNT_8_7
;
1561 ustcnt
|= USTCNT_TXEN
;
1563 m68328_console_initted
= 1;
1568 int m68328_console_setup(struct console
*cp
, char *arg
)
1570 int i
, n
= CONSOLE_BAUD_RATE
;
1576 n
= simple_strtoul(arg
,NULL
,0);
1578 for (i
= 0; i
< BAUD_TABLE_SIZE
; i
++)
1579 if (baud_table
[i
] == n
)
1581 if (i
< BAUD_TABLE_SIZE
) {
1582 m68328_console_baud
= n
;
1583 m68328_console_cbaud
= 0;
1585 m68328_console_cbaud
|= CBAUDEX
;
1588 m68328_console_cbaud
|= i
;
1591 m68328_set_baud(); /* make sure baud rate changes */
1596 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1599 return serial_driver
;
1603 void m68328_console_write (struct console
*co
, const char *str
,
1606 if (!m68328_console_initted
)
1611 rs_put_char( *str
++ );
1616 static struct console m68328_driver
= {
1618 .write
= m68328_console_write
,
1619 .device
= m68328_console_device
,
1620 .setup
= m68328_console_setup
,
1621 .flags
= CON_PRINTBUFFER
,
1626 static int __init
m68328_console_init(void)
1628 register_console(&m68328_driver
);
1632 console_initcall(m68328_console_init
);