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/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>
37 #include <linux/pm_legacy.h>
38 #include <linux/bitops.h>
39 #include <linux/delay.h>
43 #include <asm/system.h>
44 #include <asm/delay.h>
45 #include <asm/uaccess.h>
48 /* note: perhaps we can murge these files, so that you can just
49 * define 1 of them, and they can sort that out for themselves
51 #if defined(CONFIG_M68EZ328)
52 #include <asm/MC68EZ328.h>
54 #if defined(CONFIG_M68VZ328)
55 #include <asm/MC68VZ328.h>
57 #include <asm/MC68328.h>
58 #endif /* CONFIG_M68VZ328 */
59 #endif /* CONFIG_M68EZ328 */
61 #include "68328serial.h"
63 /* Turn off usage of real serial interrupt code, to "support" Copilot */
64 #ifdef CONFIG_XCOPILOT_BUGS
70 static struct m68k_serial m68k_soft
[NR_PORTS
];
71 struct m68k_serial
*IRQ_ports
[NR_IRQS
];
73 static unsigned int uart_irqs
[NR_PORTS
] = UART_IRQ_DEFNS
;
75 /* multiple ports are contiguous in memory */
76 m68328_uart
*uart_addr
= (m68328_uart
*)USTCNT_ADDR
;
78 struct tty_struct m68k_ttys
;
79 struct m68k_serial
*m68k_consinfo
= 0;
81 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
84 extern wait_queue_head_t keypress_wait
;
87 struct tty_driver
*serial_driver
;
89 /* serial subtype definitions */
90 #define SERIAL_TYPE_NORMAL 1
92 /* number of characters left in xmit buffer before we ask for more */
93 #define WAKEUP_CHARS 256
95 /* Debugging... DEBUG_INTR is bad to use when one of the zs
96 * lines is your console ;(
98 #undef SERIAL_DEBUG_INTR
99 #undef SERIAL_DEBUG_OPEN
100 #undef SERIAL_DEBUG_FLOW
102 #define RS_ISR_PASS_LIMIT 256
104 #define _INLINE_ inline
106 static void change_speed(struct m68k_serial
*info
);
109 * Setup for console. Argument comes from the boot command line.
112 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
113 #define CONSOLE_BAUD_RATE 115200
114 #define DEFAULT_CBAUD B115200
117 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
118 #ifdef CONFIG_M68VZ328
119 #define CONSOLE_BAUD_RATE 19200
120 #define DEFAULT_CBAUD B19200
125 #ifndef CONSOLE_BAUD_RATE
126 #define CONSOLE_BAUD_RATE 9600
127 #define DEFAULT_CBAUD B9600
131 static int m68328_console_initted
= 0;
132 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
133 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
137 * tmp_buf is used as a temporary buffer by serial_write. We need to
138 * lock it in case the memcpy_fromfs blocks while swapping in a page,
139 * and some other program tries to do a serial write at the same time.
140 * Since the lock will only come under contention when the system is
141 * swapping and available memory is low, it makes sense to share one
142 * buffer across all the serial ports, since it significantly saves
143 * memory if large numbers of serial ports are open.
145 static unsigned char tmp_buf
[SERIAL_XMIT_SIZE
]; /* This is cheating */
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
];
296 unsigned char ch
, flag
;
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 */
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_request_buffer_room(tty
, 1) == 0) {
335 tty_schedule_flip(tty
);
341 if(rx
& URX_PARITY_ERROR
) {
343 status_handle(info
, rx
);
344 } else if(rx
& URX_OVRUN
) {
346 status_handle(info
, rx
);
347 } else if(rx
& URX_FRAME_ERROR
) {
349 status_handle(info
, rx
);
351 tty_insert_flip_char(tty
, ch
, flag
);
352 #ifndef CONFIG_XCOPILOT_BUGS
353 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
356 tty_schedule_flip(tty
);
362 static _INLINE_
void transmit_chars(struct m68k_serial
*info
)
364 m68328_uart
*uart
= &uart_addr
[info
->line
];
368 uart
->utx
.b
.txdata
= info
->x_char
;
370 goto clear_and_return
;
373 if((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
374 /* That's peculiar... TX ints off */
375 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
376 goto clear_and_return
;
380 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
381 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
384 if (info
->xmit_cnt
< WAKEUP_CHARS
)
385 schedule_work(&info
->tqueue
);
387 if(info
->xmit_cnt
<= 0) {
388 /* All done for now... TX ints off */
389 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
390 goto clear_and_return
;
394 /* Clear interrupt (should be auto)*/
399 * This is the serial driver's generic interrupt routine
401 irqreturn_t
rs_interrupt(int irq
, void *dev_id
, struct pt_regs
* regs
)
403 struct m68k_serial
* info
;
408 info
= IRQ_ports
[irq
];
412 uart
= &uart_addr
[info
->line
];
418 if (rx
& URX_DATA_READY
) receive_chars(info
, regs
, rx
);
419 if (tx
& UTX_TX_AVAIL
) transmit_chars(info
);
421 receive_chars(info
, regs
, rx
);
426 static void do_softint(void *private)
428 struct m68k_serial
*info
= (struct m68k_serial
*) private;
429 struct tty_struct
*tty
;
435 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
442 * This routine is called from the scheduler tqueue when the interrupt
443 * routine has signalled that a hangup has occurred. The path of
444 * hangup processing is:
446 * serial interrupt routine -> (scheduler tqueue) ->
447 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
450 static void do_serial_hangup(void *private)
452 struct m68k_serial
*info
= (struct m68k_serial
*) private;
453 struct tty_struct
*tty
;
463 static int startup(struct m68k_serial
* info
)
465 m68328_uart
*uart
= &uart_addr
[info
->line
];
468 if (info
->flags
& S_INITIALIZED
)
471 if (!info
->xmit_buf
) {
472 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
477 save_flags(flags
); cli();
480 * Clear the FIFO buffers and disable them
481 * (they will be reenabled in change_speed())
484 uart
->ustcnt
= USTCNT_UEN
;
485 info
->xmit_fifo_size
= 1;
486 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
490 * Finally, enable sequencing and interrupts
493 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
494 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
496 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
500 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
501 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
504 * and set the speed of the serial port
509 info
->flags
|= S_INITIALIZED
;
510 restore_flags(flags
);
515 * This routine will shutdown a serial port; interrupts are disabled, and
516 * DTR is dropped if the hangup on close termio flag is on.
518 static void shutdown(struct m68k_serial
* info
)
520 m68328_uart
*uart
= &uart_addr
[info
->line
];
523 uart
->ustcnt
= 0; /* All off! */
524 if (!(info
->flags
& S_INITIALIZED
))
527 save_flags(flags
); cli(); /* Disable interrupts */
529 if (info
->xmit_buf
) {
530 free_page((unsigned long) info
->xmit_buf
);
535 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
537 info
->flags
&= ~S_INITIALIZED
;
538 restore_flags(flags
);
542 int divisor
, prescale
;
544 #ifndef CONFIG_M68VZ328
545 hw_baud_table
[18] = {
560 {1,0x26}, /* 19200 */
561 {0,0x26}, /* 38400 */
562 {1,0x38}, /* 57600 */
563 {0,0x38}, /* 115200 */
566 hw_baud_table
[18] = {
581 {2,0x26}, /* 19200 */
582 {1,0x26}, /* 38400 */
583 {0,0x26}, /* 57600 */
584 {1,0x38}, /* 115200 */
587 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
590 * This routine is called to set the UART divisor registers to match
591 * the specified baud rate for a serial port.
593 static void change_speed(struct m68k_serial
*info
)
595 m68328_uart
*uart
= &uart_addr
[info
->line
];
597 unsigned short ustcnt
;
601 if (!info
->tty
|| !info
->tty
->termios
)
603 cflag
= info
->tty
->termios
->c_cflag
;
604 if (!(port
= info
->port
))
607 ustcnt
= uart
->ustcnt
;
608 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
612 i
= (i
& ~CBAUDEX
) + B38400
;
615 info
->baud
= baud_table
[i
];
616 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
617 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
619 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
621 if ((cflag
& CSIZE
) == CS8
)
622 ustcnt
|= USTCNT_8_7
;
625 ustcnt
|= USTCNT_STOP
;
628 ustcnt
|= USTCNT_PARITYEN
;
630 ustcnt
|= USTCNT_ODD_EVEN
;
632 #ifdef CONFIG_SERIAL_68328_RTS_CTS
633 if (cflag
& CRTSCTS
) {
634 uart
->utx
.w
&= ~ UTX_NOCTS
;
636 uart
->utx
.w
|= UTX_NOCTS
;
640 ustcnt
|= USTCNT_TXEN
;
642 uart
->ustcnt
= ustcnt
;
647 * Fair output driver allows a process to speak.
649 static void rs_fair_output(void)
651 int left
; /* Output no more than that */
653 struct m68k_serial
*info
= &m68k_soft
[0];
656 if (info
== 0) return;
657 if (info
->xmit_buf
== 0) return;
659 save_flags(flags
); cli();
660 left
= info
->xmit_cnt
;
662 c
= info
->xmit_buf
[info
->xmit_tail
];
663 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
665 restore_flags(flags
);
669 save_flags(flags
); cli();
670 left
= min(info
->xmit_cnt
, left
-1);
673 /* Last character is being transmitted now (hopefully). */
676 restore_flags(flags
);
681 * m68k_console_print is registered for printk.
683 void console_print_68328(const char *p
)
687 while((c
=*(p
++)) != 0) {
693 /* Comment this if you want to have a strict interrupt-driven output */
699 static void rs_set_ldisc(struct tty_struct
*tty
)
701 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
703 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
706 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
708 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
711 static void rs_flush_chars(struct tty_struct
*tty
)
713 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
714 m68328_uart
*uart
= &uart_addr
[info
->line
];
717 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
723 /* Enable transmitter */
724 save_flags(flags
); cli();
726 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
728 restore_flags(flags
);
733 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
735 uart
->ustcnt
|= USTCNT_TXEN
;
739 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
744 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
745 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
750 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
753 restore_flags(flags
);
756 extern void console_printn(const char * b
, int count
);
758 static int rs_write(struct tty_struct
* tty
,
759 const unsigned char *buf
, int count
)
762 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
763 m68328_uart
*uart
= &uart_addr
[info
->line
];
766 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
769 if (!tty
|| !info
->xmit_buf
)
775 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
776 SERIAL_XMIT_SIZE
- info
->xmit_head
));
780 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
781 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
783 restore_flags(flags
);
789 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
790 /* Enable transmitter */
793 while(info
->xmit_cnt
) {
796 uart
->ustcnt
|= USTCNT_TXEN
;
798 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
800 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
802 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
803 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
804 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
811 restore_flags(flags
);
813 restore_flags(flags
);
817 static int rs_write_room(struct tty_struct
*tty
)
819 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
822 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
824 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
830 static int rs_chars_in_buffer(struct tty_struct
*tty
)
832 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
834 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
836 return info
->xmit_cnt
;
839 static void rs_flush_buffer(struct tty_struct
*tty
)
841 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
843 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
846 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
852 * ------------------------------------------------------------
855 * This routine is called by the upper-layer tty layer to signal that
856 * incoming characters should be throttled.
857 * ------------------------------------------------------------
859 static void rs_throttle(struct tty_struct
* tty
)
861 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
863 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
867 info
->x_char
= STOP_CHAR(tty
);
869 /* Turn off RTS line (do this atomic) */
872 static void rs_unthrottle(struct tty_struct
* tty
)
874 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
876 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
883 info
->x_char
= START_CHAR(tty
);
886 /* Assert RTS line (do this atomic) */
890 * ------------------------------------------------------------
891 * rs_ioctl() and friends
892 * ------------------------------------------------------------
895 static int get_serial_info(struct m68k_serial
* info
,
896 struct serial_struct
* retinfo
)
898 struct serial_struct tmp
;
902 memset(&tmp
, 0, sizeof(tmp
));
903 tmp
.type
= info
->type
;
904 tmp
.line
= info
->line
;
905 tmp
.port
= info
->port
;
907 tmp
.flags
= info
->flags
;
908 tmp
.baud_base
= info
->baud_base
;
909 tmp
.close_delay
= info
->close_delay
;
910 tmp
.closing_wait
= info
->closing_wait
;
911 tmp
.custom_divisor
= info
->custom_divisor
;
912 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
916 static int set_serial_info(struct m68k_serial
* info
,
917 struct serial_struct
* new_info
)
919 struct serial_struct new_serial
;
920 struct m68k_serial old_info
;
925 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
928 if (!capable(CAP_SYS_ADMIN
)) {
929 if ((new_serial
.baud_base
!= info
->baud_base
) ||
930 (new_serial
.type
!= info
->type
) ||
931 (new_serial
.close_delay
!= info
->close_delay
) ||
932 ((new_serial
.flags
& ~S_USR_MASK
) !=
933 (info
->flags
& ~S_USR_MASK
)))
935 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
936 (new_serial
.flags
& S_USR_MASK
));
937 info
->custom_divisor
= new_serial
.custom_divisor
;
945 * OK, past this point, all the error checking has been done.
946 * At this point, we start making changes.....
949 info
->baud_base
= new_serial
.baud_base
;
950 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
951 (new_serial
.flags
& S_FLAGS
));
952 info
->type
= new_serial
.type
;
953 info
->close_delay
= new_serial
.close_delay
;
954 info
->closing_wait
= new_serial
.closing_wait
;
957 retval
= startup(info
);
962 * get_lsr_info - get line status register info
964 * Purpose: Let user call ioctl() to get info when the UART physically
965 * is emptied. On bus types like RS485, the transmitter must
966 * release the bus after transmitting. This must be done when
967 * the transmit shift register is empty, not be done when the
968 * transmit holding register is empty. This functionality
969 * allows an RS485 driver to be written in user space.
971 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
973 #ifdef CONFIG_SERIAL_68328_RTS_CTS
974 m68328_uart
*uart
= &uart_addr
[info
->line
];
976 unsigned char status
;
979 #ifdef CONFIG_SERIAL_68328_RTS_CTS
980 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
985 put_user(status
,value
);
990 * This routine sends a break character out the serial port.
992 static void send_break(struct m68k_serial
* info
, unsigned int duration
)
994 m68328_uart
*uart
= &uart_addr
[info
->line
];
1001 uart
->utx
.w
|= UTX_SEND_BREAK
;
1002 msleep_interruptible(duration
);
1003 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
1005 restore_flags(flags
);
1008 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1009 unsigned int cmd
, unsigned long arg
)
1012 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1015 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1018 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1019 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1020 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1021 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1026 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1027 retval
= tty_check_change(tty
);
1030 tty_wait_until_sent(tty
, 0);
1032 send_break(info
, 250); /* 1/4 second */
1034 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1035 retval
= tty_check_change(tty
);
1038 tty_wait_until_sent(tty
, 0);
1039 send_break(info
, arg
? arg
*(100) : 250);
1042 error
= put_user(C_CLOCAL(tty
) ? 1 : 0,
1043 (unsigned long *) arg
);
1048 get_user(arg
, (unsigned long *) arg
);
1049 tty
->termios
->c_cflag
=
1050 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1051 (arg
? CLOCAL
: 0));
1054 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1055 sizeof(struct serial_struct
)))
1056 return get_serial_info(info
,
1057 (struct serial_struct
*) arg
);
1060 return set_serial_info(info
,
1061 (struct serial_struct
*) arg
);
1062 case TIOCSERGETLSR
: /* Get line status register */
1063 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1064 sizeof(unsigned int));
1065 return get_lsr_info(info
, (unsigned int *) arg
);
1067 case TIOCSERGSTRUCT
:
1068 if (!access_ok(VERIFY_WRITE
, (void *) arg
,
1069 sizeof(struct m68k_serial
)))
1071 copy_to_user((struct m68k_serial
*) arg
,
1072 info
, sizeof(struct m68k_serial
));
1076 return -ENOIOCTLCMD
;
1081 static void rs_set_termios(struct tty_struct
*tty
, struct termios
*old_termios
)
1083 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
1085 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1090 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1091 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1092 tty
->hw_stopped
= 0;
1099 * ------------------------------------------------------------
1102 * This routine is called when the serial port gets closed. First, we
1103 * wait for the last remaining data to be sent. Then, we unlink its
1104 * S structure from the interrupt chain if necessary, and we free
1105 * that IRQ if nothing is left in the chain.
1106 * ------------------------------------------------------------
1108 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1110 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1111 m68328_uart
*uart
= &uart_addr
[info
->line
];
1112 unsigned long flags
;
1114 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1117 save_flags(flags
); cli();
1119 if (tty_hung_up_p(filp
)) {
1120 restore_flags(flags
);
1124 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1126 * Uh, oh. tty->count is 1, which means that the tty
1127 * structure will be freed. Info->count should always
1128 * be one in these conditions. If it's greater than
1129 * one, we've got real problems, since it means the
1130 * serial port won't be shutdown.
1132 printk("rs_close: bad serial port count; tty->count is 1, "
1133 "info->count is %d\n", info
->count
);
1136 if (--info
->count
< 0) {
1137 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1138 info
->line
, info
->count
);
1142 restore_flags(flags
);
1145 info
->flags
|= S_CLOSING
;
1147 * Now we wait for the transmit buffer to clear; and we notify
1148 * the line discipline to only process XON/XOFF characters.
1151 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
1152 tty_wait_until_sent(tty
, info
->closing_wait
);
1154 * At this point we stop accepting input. To do this, we
1155 * disable the receive line status interrupts, and tell the
1156 * interrupt driver to stop checking the data ready bit in the
1157 * line status register.
1160 uart
->ustcnt
&= ~USTCNT_RXEN
;
1161 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1164 if (tty
->driver
->flush_buffer
)
1165 tty
->driver
->flush_buffer(tty
);
1167 tty_ldisc_flush(tty
);
1171 #warning "This is not and has never been valid so fix it"
1173 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1174 if (tty
->ldisc
.close
)
1175 (tty
->ldisc
.close
)(tty
);
1176 tty
->ldisc
= ldiscs
[N_TTY
];
1177 tty
->termios
->c_line
= N_TTY
;
1178 if (tty
->ldisc
.open
)
1179 (tty
->ldisc
.open
)(tty
);
1182 if (info
->blocked_open
) {
1183 if (info
->close_delay
) {
1184 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1186 wake_up_interruptible(&info
->open_wait
);
1188 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1189 wake_up_interruptible(&info
->close_wait
);
1190 restore_flags(flags
);
1194 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1196 void rs_hangup(struct tty_struct
*tty
)
1198 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1200 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1203 rs_flush_buffer(tty
);
1207 info
->flags
&= ~S_NORMAL_ACTIVE
;
1209 wake_up_interruptible(&info
->open_wait
);
1213 * ------------------------------------------------------------
1214 * rs_open() and friends
1215 * ------------------------------------------------------------
1217 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1218 struct m68k_serial
*info
)
1220 DECLARE_WAITQUEUE(wait
, current
);
1225 * If the device is in the middle of being closed, then block
1226 * until it's done, and then try again.
1228 if (info
->flags
& S_CLOSING
) {
1229 interruptible_sleep_on(&info
->close_wait
);
1230 #ifdef SERIAL_DO_RESTART
1231 if (info
->flags
& S_HUP_NOTIFY
)
1234 return -ERESTARTSYS
;
1241 * If non-blocking mode is set, or the port is not enabled,
1242 * then make the check up front and then exit.
1244 if ((filp
->f_flags
& O_NONBLOCK
) ||
1245 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1246 info
->flags
|= S_NORMAL_ACTIVE
;
1250 if (tty
->termios
->c_cflag
& CLOCAL
)
1254 * Block waiting for the carrier detect and the line to become
1255 * free (i.e., not in use by the callout). While we are in
1256 * this loop, info->count is dropped by one, so that
1257 * rs_close() knows when to free things. We restore it upon
1258 * exit, either normal or abnormal.
1261 add_wait_queue(&info
->open_wait
, &wait
);
1264 info
->blocked_open
++;
1267 m68k_rtsdtr(info
, 1);
1269 current
->state
= TASK_INTERRUPTIBLE
;
1270 if (tty_hung_up_p(filp
) ||
1271 !(info
->flags
& S_INITIALIZED
)) {
1272 #ifdef SERIAL_DO_RESTART
1273 if (info
->flags
& S_HUP_NOTIFY
)
1276 retval
= -ERESTARTSYS
;
1282 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1284 if (signal_pending(current
)) {
1285 retval
= -ERESTARTSYS
;
1290 current
->state
= TASK_RUNNING
;
1291 remove_wait_queue(&info
->open_wait
, &wait
);
1292 if (!tty_hung_up_p(filp
))
1294 info
->blocked_open
--;
1298 info
->flags
|= S_NORMAL_ACTIVE
;
1303 * This routine is called whenever a serial port is opened. It
1304 * enables interrupts for a serial port, linking in its S structure into
1305 * the IRQ chain. It also performs the serial-specific
1306 * initialization for the tty structure.
1308 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1310 struct m68k_serial
*info
;
1315 if (line
>= NR_PORTS
|| line
< 0) /* we have exactly one */
1318 info
= &m68k_soft
[line
];
1320 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1324 tty
->driver_data
= info
;
1328 * Start up serial port
1330 retval
= startup(info
);
1334 return block_til_ready(tty
, filp
, info
);
1337 /* Finally, routines used to initialize the serial driver. */
1339 static void show_serial_version(void)
1341 printk("MC68328 serial driver version 1.00\n");
1344 #ifdef CONFIG_PM_LEGACY
1345 /* Serial Power management
1346 * The console (currently fixed at line 0) is a special case for power
1347 * management because the kernel is so chatty. The console will be
1348 * explicitly disabled my our power manager as the last minute, so we won't
1349 * mess with it here.
1351 static struct pm_dev
*serial_pm
[NR_PORTS
];
1353 static int serial_pm_callback(struct pm_dev
*dev
, pm_request_t request
, void *data
)
1355 struct m68k_serial
*info
= (struct m68k_serial
*)dev
->data
;
1360 /* special case for line 0 - pm restores it */
1376 void shutdown_console(void)
1378 struct m68k_serial
*info
= &m68k_soft
[0];
1380 /* HACK: wait a bit for any pending printk's to be dumped */
1389 void startup_console(void)
1391 struct m68k_serial
*info
= &m68k_soft
[0];
1394 #endif /* CONFIG_PM_LEGACY */
1397 static struct tty_operations rs_ops
= {
1401 .flush_chars
= rs_flush_chars
,
1402 .write_room
= rs_write_room
,
1403 .chars_in_buffer
= rs_chars_in_buffer
,
1404 .flush_buffer
= rs_flush_buffer
,
1406 .throttle
= rs_throttle
,
1407 .unthrottle
= rs_unthrottle
,
1408 .set_termios
= rs_set_termios
,
1411 .hangup
= rs_hangup
,
1412 .set_ldisc
= rs_set_ldisc
,
1415 /* rs_init inits the driver */
1420 struct m68k_serial
*info
;
1422 serial_driver
= alloc_tty_driver(NR_PORTS
);
1426 show_serial_version();
1428 /* Initialize the tty_driver structure */
1429 /* SPARC: Not all of this is exactly right for us. */
1431 serial_driver
->name
= "ttyS";
1432 serial_driver
->major
= TTY_MAJOR
;
1433 serial_driver
->minor_start
= 64;
1434 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1435 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1436 serial_driver
->init_termios
= tty_std_termios
;
1437 serial_driver
->init_termios
.c_cflag
=
1438 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1439 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1440 tty_set_operations(serial_driver
, &rs_ops
);
1442 if (tty_register_driver(serial_driver
)) {
1443 put_tty_driver(serial_driver
);
1444 printk(KERN_ERR
"Couldn't register serial driver\n");
1448 save_flags(flags
); cli();
1450 for(i
=0;i
<NR_PORTS
;i
++) {
1452 info
= &m68k_soft
[i
];
1453 info
->magic
= SERIAL_MAGIC
;
1454 info
->port
= (int) &uart_addr
[i
];
1456 info
->irq
= uart_irqs
[i
];
1457 info
->custom_divisor
= 16;
1458 info
->close_delay
= 50;
1459 info
->closing_wait
= 3000;
1463 info
->blocked_open
= 0;
1464 INIT_WORK(&info
->tqueue
, do_softint
, info
);
1465 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
, info
);
1466 init_waitqueue_head(&info
->open_wait
);
1467 init_waitqueue_head(&info
->close_wait
);
1469 info
->is_cons
= 1; /* Means shortcuts work */
1471 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1472 info
->port
, info
->irq
);
1473 printk(" is a builtin MC68328 UART\n");
1475 IRQ_ports
[info
->irq
] = info
; /* waste of space */
1477 #ifdef CONFIG_M68VZ328
1479 PJSEL
&= 0xCF; /* PSW enable second port output */
1482 if (request_irq(uart_irqs
[i
],
1485 "M68328_UART", NULL
))
1486 panic("Unable to attach 68328 serial interrupt\n");
1487 #ifdef CONFIG_PM_LEGACY
1488 serial_pm
[i
] = pm_register(PM_SYS_DEV
, PM_SYS_COM
, serial_pm_callback
);
1490 serial_pm
[i
]->data
= info
;
1493 restore_flags(flags
);
1497 module_init(rs68328_init
);
1501 static void m68328_set_baud(void)
1503 unsigned short ustcnt
;
1507 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1510 for (i
= 0; i
< sizeof(baud_table
) / sizeof(baud_table
[0]); i
++)
1511 if (baud_table
[i
] == m68328_console_baud
)
1513 if (i
>= sizeof(baud_table
) / sizeof(baud_table
[0])) {
1514 m68328_console_baud
= 9600;
1518 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1519 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1520 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1521 ustcnt
|= USTCNT_8_7
;
1522 ustcnt
|= USTCNT_TXEN
;
1524 m68328_console_initted
= 1;
1529 int m68328_console_setup(struct console
*cp
, char *arg
)
1531 int i
, n
= CONSOLE_BAUD_RATE
;
1537 n
= simple_strtoul(arg
,NULL
,0);
1539 for (i
= 0; i
< BAUD_TABLE_SIZE
; i
++)
1540 if (baud_table
[i
] == n
)
1542 if (i
< BAUD_TABLE_SIZE
) {
1543 m68328_console_baud
= n
;
1544 m68328_console_cbaud
= 0;
1546 m68328_console_cbaud
|= CBAUDEX
;
1549 m68328_console_cbaud
|= i
;
1552 m68328_set_baud(); /* make sure baud rate changes */
1557 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1560 return serial_driver
;
1564 void m68328_console_write (struct console
*co
, const char *str
,
1567 if (!m68328_console_initted
)
1572 rs_put_char( *str
++ );
1577 static struct console m68328_driver
= {
1579 .write
= m68328_console_write
,
1580 .device
= m68328_console_device
,
1581 .setup
= m68328_console_setup
,
1582 .flags
= CON_PRINTBUFFER
,
1587 static int __init
m68328_console_init(void)
1589 register_console(&m68328_driver
);
1593 console_initcall(m68328_console_init
);