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 /* serial subtype definitions */
88 #define SERIAL_TYPE_NORMAL 1
90 /* number of characters left in xmit buffer before we ask for more */
91 #define WAKEUP_CHARS 256
93 /* Debugging... DEBUG_INTR is bad to use when one of the zs
94 * lines is your console ;(
96 #undef SERIAL_DEBUG_INTR
97 #undef SERIAL_DEBUG_OPEN
98 #undef SERIAL_DEBUG_FLOW
100 #define RS_ISR_PASS_LIMIT 256
102 static void change_speed(struct m68k_serial
*info
);
105 * Setup for console. Argument comes from the boot command line.
108 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
109 #define CONSOLE_BAUD_RATE 115200
110 #define DEFAULT_CBAUD B115200
113 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
114 #ifdef CONFIG_M68VZ328
115 #define CONSOLE_BAUD_RATE 19200
116 #define DEFAULT_CBAUD B19200
121 #ifndef CONSOLE_BAUD_RATE
122 #define CONSOLE_BAUD_RATE 9600
123 #define DEFAULT_CBAUD B9600
127 static int m68328_console_initted
= 0;
128 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
129 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
132 static inline int serial_paranoia_check(struct m68k_serial
*info
,
133 char *name
, const char *routine
)
135 #ifdef SERIAL_PARANOIA_CHECK
136 static const char *badmagic
=
137 "Warning: bad magic number for serial struct %s in %s\n";
138 static const char *badinfo
=
139 "Warning: null m68k_serial for %s in %s\n";
142 printk(badinfo
, name
, routine
);
145 if (info
->magic
!= SERIAL_MAGIC
) {
146 printk(badmagic
, name
, routine
);
154 * This is used to figure out the divisor speeds and the timeouts
156 static int baud_table
[] = {
157 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
158 9600, 19200, 38400, 57600, 115200, 0 };
160 #define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
162 /* Sets or clears DTR/RTS on the requested line */
163 static inline void m68k_rtsdtr(struct m68k_serial
*ss
, int set
)
166 /* set the RTS/CTS line */
173 /* Utility routines */
174 static inline int get_baud(struct m68k_serial
*ss
)
176 unsigned long result
= 115200;
177 unsigned short int baud
= uart_addr
[ss
->line
].ubaud
;
178 if (GET_FIELD(baud
, UBAUD_PRESCALER
) == 0x38) result
= 38400;
179 result
>>= GET_FIELD(baud
, UBAUD_DIVIDE
);
185 * ------------------------------------------------------------
186 * rs_stop() and rs_start()
188 * This routines are called before setting or resetting tty->stopped.
189 * They enable or disable transmitter interrupts, as necessary.
190 * ------------------------------------------------------------
192 static void rs_stop(struct tty_struct
*tty
)
194 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
195 m68328_uart
*uart
= &uart_addr
[info
->line
];
198 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
201 local_irq_save(flags
);
202 uart
->ustcnt
&= ~USTCNT_TXEN
;
203 local_irq_restore(flags
);
206 static void rs_put_char(char ch
)
208 int flags
, loops
= 0;
210 local_irq_save(flags
);
212 while (!(UTX
& UTX_TX_AVAIL
) && (loops
< 1000)) {
219 local_irq_restore(flags
);
222 static void rs_start(struct tty_struct
*tty
)
224 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
225 m68328_uart
*uart
= &uart_addr
[info
->line
];
228 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
231 local_irq_save(flags
);
232 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->ustcnt
& USTCNT_TXEN
)) {
234 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
236 uart
->ustcnt
|= USTCNT_TXEN
;
239 local_irq_restore(flags
);
242 /* Drop into either the boot monitor or kadb upon receiving a break
243 * from keyboard/console input.
245 static void batten_down_hatches(void)
247 /* Drop into the debugger */
250 static void status_handle(struct m68k_serial
*info
, unsigned short status
)
254 if((info
->tty
->termios
->c_cflag
& CRTSCTS
) &&
255 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
256 info
->curregs
[3] |= AUTO_ENAB
;
257 info
->pendregs
[3] |= AUTO_ENAB
;
258 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
261 if((info
->curregs
[3] & AUTO_ENAB
)) {
262 info
->curregs
[3] &= ~AUTO_ENAB
;
263 info
->pendregs
[3] &= ~AUTO_ENAB
;
264 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
268 /* If this is console input and this is a
269 * 'break asserted' status change interrupt
270 * see if we can drop into the debugger
272 if((status
& URX_BREAK
) && info
->break_abort
)
273 batten_down_hatches();
277 static void receive_chars(struct m68k_serial
*info
, unsigned short rx
)
279 struct tty_struct
*tty
= info
->tty
;
280 m68328_uart
*uart
= &uart_addr
[info
->line
];
281 unsigned char ch
, flag
;
284 * This do { } while() loop will get ALL chars out of Rx FIFO
286 #ifndef CONFIG_XCOPILOT_BUGS
289 ch
= GET_FIELD(rx
, URX_RXDATA
);
292 if(URX_BREAK
& rx
) { /* whee, break received */
293 status_handle(info
, rx
);
295 #ifdef CONFIG_MAGIC_SYSRQ
296 } else if (ch
== 0x10) { /* ^P */
300 /* show_net_buffers(); */
302 } else if (ch
== 0x12) { /* ^R */
305 #endif /* CONFIG_MAGIC_SYSRQ */
307 /* It is a 'keyboard interrupt' ;-) */
308 #ifdef CONFIG_CONSOLE
309 wake_up(&keypress_wait
);
318 if(rx
& URX_PARITY_ERROR
) {
320 status_handle(info
, rx
);
321 } else if(rx
& URX_OVRUN
) {
323 status_handle(info
, rx
);
324 } else if(rx
& URX_FRAME_ERROR
) {
326 status_handle(info
, rx
);
328 tty_insert_flip_char(tty
, ch
, flag
);
329 #ifndef CONFIG_XCOPILOT_BUGS
330 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
333 tty_schedule_flip(tty
);
339 static void transmit_chars(struct m68k_serial
*info
)
341 m68328_uart
*uart
= &uart_addr
[info
->line
];
345 uart
->utx
.b
.txdata
= info
->x_char
;
347 goto clear_and_return
;
350 if((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
351 /* That's peculiar... TX ints off */
352 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
353 goto clear_and_return
;
357 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
358 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
361 if (info
->xmit_cnt
< WAKEUP_CHARS
)
362 schedule_work(&info
->tqueue
);
364 if(info
->xmit_cnt
<= 0) {
365 /* All done for now... TX ints off */
366 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
367 goto clear_and_return
;
371 /* Clear interrupt (should be auto)*/
376 * This is the serial driver's generic interrupt routine
378 irqreturn_t
rs_interrupt(int irq
, void *dev_id
)
380 struct m68k_serial
* info
;
385 info
= IRQ_ports
[irq
];
389 uart
= &uart_addr
[info
->line
];
395 if (rx
& URX_DATA_READY
) receive_chars(info
, rx
);
396 if (tx
& UTX_TX_AVAIL
) transmit_chars(info
);
398 receive_chars(info
, rx
);
403 static void do_softint(struct work_struct
*work
)
405 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue
);
406 struct tty_struct
*tty
;
412 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
419 * This routine is called from the scheduler tqueue when the interrupt
420 * routine has signalled that a hangup has occurred. The path of
421 * hangup processing is:
423 * serial interrupt routine -> (scheduler tqueue) ->
424 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
427 static void do_serial_hangup(struct work_struct
*work
)
429 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue_hangup
);
430 struct tty_struct
*tty
;
440 static int startup(struct m68k_serial
* info
)
442 m68328_uart
*uart
= &uart_addr
[info
->line
];
445 if (info
->flags
& S_INITIALIZED
)
448 if (!info
->xmit_buf
) {
449 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
454 local_irq_save(flags
);
457 * Clear the FIFO buffers and disable them
458 * (they will be reenabled in change_speed())
461 uart
->ustcnt
= USTCNT_UEN
;
462 info
->xmit_fifo_size
= 1;
463 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
467 * Finally, enable sequencing and interrupts
470 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
471 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
473 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
477 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
478 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
481 * and set the speed of the serial port
486 info
->flags
|= S_INITIALIZED
;
487 local_irq_restore(flags
);
492 * This routine will shutdown a serial port; interrupts are disabled, and
493 * DTR is dropped if the hangup on close termio flag is on.
495 static void shutdown(struct m68k_serial
* info
)
497 m68328_uart
*uart
= &uart_addr
[info
->line
];
500 uart
->ustcnt
= 0; /* All off! */
501 if (!(info
->flags
& S_INITIALIZED
))
504 local_irq_save(flags
);
506 if (info
->xmit_buf
) {
507 free_page((unsigned long) info
->xmit_buf
);
512 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
514 info
->flags
&= ~S_INITIALIZED
;
515 local_irq_restore(flags
);
519 int divisor
, prescale
;
521 #ifndef CONFIG_M68VZ328
522 hw_baud_table
[18] = {
537 {1,0x26}, /* 19200 */
538 {0,0x26}, /* 38400 */
539 {1,0x38}, /* 57600 */
540 {0,0x38}, /* 115200 */
543 hw_baud_table
[18] = {
558 {2,0x26}, /* 19200 */
559 {1,0x26}, /* 38400 */
560 {0,0x26}, /* 57600 */
561 {1,0x38}, /* 115200 */
564 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
567 * This routine is called to set the UART divisor registers to match
568 * the specified baud rate for a serial port.
570 static void change_speed(struct m68k_serial
*info
)
572 m68328_uart
*uart
= &uart_addr
[info
->line
];
574 unsigned short ustcnt
;
578 if (!info
->tty
|| !info
->tty
->termios
)
580 cflag
= info
->tty
->termios
->c_cflag
;
581 if (!(port
= info
->port
))
584 ustcnt
= uart
->ustcnt
;
585 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
589 i
= (i
& ~CBAUDEX
) + B38400
;
592 info
->baud
= baud_table
[i
];
593 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
594 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
596 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
598 if ((cflag
& CSIZE
) == CS8
)
599 ustcnt
|= USTCNT_8_7
;
602 ustcnt
|= USTCNT_STOP
;
605 ustcnt
|= USTCNT_PARITYEN
;
607 ustcnt
|= USTCNT_ODD_EVEN
;
609 #ifdef CONFIG_SERIAL_68328_RTS_CTS
610 if (cflag
& CRTSCTS
) {
611 uart
->utx
.w
&= ~ UTX_NOCTS
;
613 uart
->utx
.w
|= UTX_NOCTS
;
617 ustcnt
|= USTCNT_TXEN
;
619 uart
->ustcnt
= ustcnt
;
624 * Fair output driver allows a process to speak.
626 static void rs_fair_output(void)
628 int left
; /* Output no more than that */
630 struct m68k_serial
*info
= &m68k_soft
[0];
633 if (info
== 0) return;
634 if (info
->xmit_buf
== 0) return;
636 local_irq_save(flags
);
637 left
= info
->xmit_cnt
;
639 c
= info
->xmit_buf
[info
->xmit_tail
];
640 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
642 local_irq_restore(flags
);
646 local_irq_save(flags
);
647 left
= min(info
->xmit_cnt
, left
-1);
650 /* Last character is being transmitted now (hopefully). */
653 local_irq_restore(flags
);
658 * m68k_console_print is registered for printk.
660 void console_print_68328(const char *p
)
664 while((c
=*(p
++)) != 0) {
670 /* Comment this if you want to have a strict interrupt-driven output */
676 static void rs_set_ldisc(struct tty_struct
*tty
)
678 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
680 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
683 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
685 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
688 static void rs_flush_chars(struct tty_struct
*tty
)
690 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
691 m68328_uart
*uart
= &uart_addr
[info
->line
];
694 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
700 /* Enable transmitter */
701 local_irq_save(flags
);
703 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
705 local_irq_restore(flags
);
710 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
712 uart
->ustcnt
|= USTCNT_TXEN
;
716 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
721 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
722 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
727 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
730 local_irq_restore(flags
);
733 extern void console_printn(const char * b
, int count
);
735 static int rs_write(struct tty_struct
* tty
,
736 const unsigned char *buf
, int count
)
739 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
740 m68328_uart
*uart
= &uart_addr
[info
->line
];
743 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
746 if (!tty
|| !info
->xmit_buf
)
749 local_save_flags(flags
);
752 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
753 SERIAL_XMIT_SIZE
- info
->xmit_head
));
754 local_irq_restore(flags
);
759 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
762 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
764 local_irq_restore(flags
);
770 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
771 /* Enable transmitter */
774 while(info
->xmit_cnt
) {
777 uart
->ustcnt
|= USTCNT_TXEN
;
779 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
781 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
783 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
784 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
785 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
792 local_irq_restore(flags
);
798 static int rs_write_room(struct tty_struct
*tty
)
800 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
803 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
805 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
811 static int rs_chars_in_buffer(struct tty_struct
*tty
)
813 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
815 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
817 return info
->xmit_cnt
;
820 static void rs_flush_buffer(struct tty_struct
*tty
)
822 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
825 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
827 local_irq_save(flags
);
828 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
829 local_irq_restore(flags
);
834 * ------------------------------------------------------------
837 * This routine is called by the upper-layer tty layer to signal that
838 * incoming characters should be throttled.
839 * ------------------------------------------------------------
841 static void rs_throttle(struct tty_struct
* tty
)
843 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
845 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
849 info
->x_char
= STOP_CHAR(tty
);
851 /* Turn off RTS line (do this atomic) */
854 static void rs_unthrottle(struct tty_struct
* tty
)
856 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
858 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
865 info
->x_char
= START_CHAR(tty
);
868 /* Assert RTS line (do this atomic) */
872 * ------------------------------------------------------------
873 * rs_ioctl() and friends
874 * ------------------------------------------------------------
877 static int get_serial_info(struct m68k_serial
* info
,
878 struct serial_struct
* retinfo
)
880 struct serial_struct tmp
;
884 memset(&tmp
, 0, sizeof(tmp
));
885 tmp
.type
= info
->type
;
886 tmp
.line
= info
->line
;
887 tmp
.port
= info
->port
;
889 tmp
.flags
= info
->flags
;
890 tmp
.baud_base
= info
->baud_base
;
891 tmp
.close_delay
= info
->close_delay
;
892 tmp
.closing_wait
= info
->closing_wait
;
893 tmp
.custom_divisor
= info
->custom_divisor
;
894 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
898 static int set_serial_info(struct m68k_serial
* info
,
899 struct serial_struct
* new_info
)
901 struct serial_struct new_serial
;
902 struct m68k_serial old_info
;
907 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
910 if (!capable(CAP_SYS_ADMIN
)) {
911 if ((new_serial
.baud_base
!= info
->baud_base
) ||
912 (new_serial
.type
!= info
->type
) ||
913 (new_serial
.close_delay
!= info
->close_delay
) ||
914 ((new_serial
.flags
& ~S_USR_MASK
) !=
915 (info
->flags
& ~S_USR_MASK
)))
917 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
918 (new_serial
.flags
& S_USR_MASK
));
919 info
->custom_divisor
= new_serial
.custom_divisor
;
927 * OK, past this point, all the error checking has been done.
928 * At this point, we start making changes.....
931 info
->baud_base
= new_serial
.baud_base
;
932 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
933 (new_serial
.flags
& S_FLAGS
));
934 info
->type
= new_serial
.type
;
935 info
->close_delay
= new_serial
.close_delay
;
936 info
->closing_wait
= new_serial
.closing_wait
;
939 retval
= startup(info
);
944 * get_lsr_info - get line status register info
946 * Purpose: Let user call ioctl() to get info when the UART physically
947 * is emptied. On bus types like RS485, the transmitter must
948 * release the bus after transmitting. This must be done when
949 * the transmit shift register is empty, not be done when the
950 * transmit holding register is empty. This functionality
951 * allows an RS485 driver to be written in user space.
953 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
955 #ifdef CONFIG_SERIAL_68328_RTS_CTS
956 m68328_uart
*uart
= &uart_addr
[info
->line
];
958 unsigned char status
;
961 local_irq_save(flags
);
962 #ifdef CONFIG_SERIAL_68328_RTS_CTS
963 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
967 local_irq_restore(flags
);
968 put_user(status
,value
);
973 * This routine sends a break character out the serial port.
975 static void send_break(struct m68k_serial
* info
, unsigned int duration
)
977 m68328_uart
*uart
= &uart_addr
[info
->line
];
981 local_irq_save(flags
);
983 uart
->utx
.w
|= UTX_SEND_BREAK
;
984 msleep_interruptible(duration
);
985 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
987 local_irq_restore(flags
);
990 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
991 unsigned int cmd
, unsigned long arg
)
994 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
997 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
1000 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1001 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1002 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1003 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1008 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1009 retval
= tty_check_change(tty
);
1012 tty_wait_until_sent(tty
, 0);
1014 send_break(info
, 250); /* 1/4 second */
1016 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1017 retval
= tty_check_change(tty
);
1020 tty_wait_until_sent(tty
, 0);
1021 send_break(info
, arg
? arg
*(100) : 250);
1024 error
= put_user(C_CLOCAL(tty
) ? 1 : 0,
1025 (unsigned long *) arg
);
1030 get_user(arg
, (unsigned long *) arg
);
1031 tty
->termios
->c_cflag
=
1032 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1033 (arg
? CLOCAL
: 0));
1036 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1037 sizeof(struct serial_struct
)))
1038 return get_serial_info(info
,
1039 (struct serial_struct
*) arg
);
1042 return set_serial_info(info
,
1043 (struct serial_struct
*) arg
);
1044 case TIOCSERGETLSR
: /* Get line status register */
1045 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1046 sizeof(unsigned int)))
1047 return get_lsr_info(info
, (unsigned int *) arg
);
1049 case TIOCSERGSTRUCT
:
1050 if (!access_ok(VERIFY_WRITE
, (void *) arg
,
1051 sizeof(struct m68k_serial
)))
1053 copy_to_user((struct m68k_serial
*) arg
,
1054 info
, sizeof(struct m68k_serial
));
1058 return -ENOIOCTLCMD
;
1063 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1065 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
1067 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1072 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1073 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1074 tty
->hw_stopped
= 0;
1081 * ------------------------------------------------------------
1084 * This routine is called when the serial port gets closed. First, we
1085 * wait for the last remaining data to be sent. Then, we unlink its
1086 * S structure from the interrupt chain if necessary, and we free
1087 * that IRQ if nothing is left in the chain.
1088 * ------------------------------------------------------------
1090 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1092 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1093 m68328_uart
*uart
= &uart_addr
[info
->line
];
1094 unsigned long flags
;
1096 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1099 local_irq_save(flags
);
1101 if (tty_hung_up_p(filp
)) {
1102 local_irq_restore(flags
);
1106 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1108 * Uh, oh. tty->count is 1, which means that the tty
1109 * structure will be freed. Info->count should always
1110 * be one in these conditions. If it's greater than
1111 * one, we've got real problems, since it means the
1112 * serial port won't be shutdown.
1114 printk("rs_close: bad serial port count; tty->count is 1, "
1115 "info->count is %d\n", info
->count
);
1118 if (--info
->count
< 0) {
1119 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1120 info
->line
, info
->count
);
1124 local_irq_restore(flags
);
1127 info
->flags
|= S_CLOSING
;
1129 * Now we wait for the transmit buffer to clear; and we notify
1130 * the line discipline to only process XON/XOFF characters.
1133 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
1134 tty_wait_until_sent(tty
, info
->closing_wait
);
1136 * At this point we stop accepting input. To do this, we
1137 * disable the receive line status interrupts, and tell the
1138 * interrupt driver to stop checking the data ready bit in the
1139 * line status register.
1142 uart
->ustcnt
&= ~USTCNT_RXEN
;
1143 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1146 if (tty
->driver
->flush_buffer
)
1147 tty
->driver
->flush_buffer(tty
);
1149 tty_ldisc_flush(tty
);
1153 #warning "This is not and has never been valid so fix it"
1155 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1156 if (tty
->ldisc
.close
)
1157 (tty
->ldisc
.close
)(tty
);
1158 tty
->ldisc
= ldiscs
[N_TTY
];
1159 tty
->termios
->c_line
= N_TTY
;
1160 if (tty
->ldisc
.open
)
1161 (tty
->ldisc
.open
)(tty
);
1164 if (info
->blocked_open
) {
1165 if (info
->close_delay
) {
1166 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1168 wake_up_interruptible(&info
->open_wait
);
1170 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1171 wake_up_interruptible(&info
->close_wait
);
1172 local_irq_restore(flags
);
1176 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1178 void rs_hangup(struct tty_struct
*tty
)
1180 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1182 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1185 rs_flush_buffer(tty
);
1189 info
->flags
&= ~S_NORMAL_ACTIVE
;
1191 wake_up_interruptible(&info
->open_wait
);
1195 * ------------------------------------------------------------
1196 * rs_open() and friends
1197 * ------------------------------------------------------------
1199 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1200 struct m68k_serial
*info
)
1202 DECLARE_WAITQUEUE(wait
, current
);
1207 * If the device is in the middle of being closed, then block
1208 * until it's done, and then try again.
1210 if (info
->flags
& S_CLOSING
) {
1211 interruptible_sleep_on(&info
->close_wait
);
1212 #ifdef SERIAL_DO_RESTART
1213 if (info
->flags
& S_HUP_NOTIFY
)
1216 return -ERESTARTSYS
;
1223 * If non-blocking mode is set, or the port is not enabled,
1224 * then make the check up front and then exit.
1226 if ((filp
->f_flags
& O_NONBLOCK
) ||
1227 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1228 info
->flags
|= S_NORMAL_ACTIVE
;
1232 if (tty
->termios
->c_cflag
& CLOCAL
)
1236 * Block waiting for the carrier detect and the line to become
1237 * free (i.e., not in use by the callout). While we are in
1238 * this loop, info->count is dropped by one, so that
1239 * rs_close() knows when to free things. We restore it upon
1240 * exit, either normal or abnormal.
1243 add_wait_queue(&info
->open_wait
, &wait
);
1246 info
->blocked_open
++;
1248 local_irq_disable();
1249 m68k_rtsdtr(info
, 1);
1251 current
->state
= TASK_INTERRUPTIBLE
;
1252 if (tty_hung_up_p(filp
) ||
1253 !(info
->flags
& S_INITIALIZED
)) {
1254 #ifdef SERIAL_DO_RESTART
1255 if (info
->flags
& S_HUP_NOTIFY
)
1258 retval
= -ERESTARTSYS
;
1264 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1266 if (signal_pending(current
)) {
1267 retval
= -ERESTARTSYS
;
1272 current
->state
= TASK_RUNNING
;
1273 remove_wait_queue(&info
->open_wait
, &wait
);
1274 if (!tty_hung_up_p(filp
))
1276 info
->blocked_open
--;
1280 info
->flags
|= S_NORMAL_ACTIVE
;
1285 * This routine is called whenever a serial port is opened. It
1286 * enables interrupts for a serial port, linking in its S structure into
1287 * the IRQ chain. It also performs the serial-specific
1288 * initialization for the tty structure.
1290 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1292 struct m68k_serial
*info
;
1297 if (line
>= NR_PORTS
|| line
< 0) /* we have exactly one */
1300 info
= &m68k_soft
[line
];
1302 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1306 tty
->driver_data
= info
;
1310 * Start up serial port
1312 retval
= startup(info
);
1316 return block_til_ready(tty
, filp
, info
);
1319 /* Finally, routines used to initialize the serial driver. */
1321 static void show_serial_version(void)
1323 printk("MC68328 serial driver version 1.00\n");
1326 static const struct tty_operations rs_ops
= {
1330 .flush_chars
= rs_flush_chars
,
1331 .write_room
= rs_write_room
,
1332 .chars_in_buffer
= rs_chars_in_buffer
,
1333 .flush_buffer
= rs_flush_buffer
,
1335 .throttle
= rs_throttle
,
1336 .unthrottle
= rs_unthrottle
,
1337 .set_termios
= rs_set_termios
,
1340 .hangup
= rs_hangup
,
1341 .set_ldisc
= rs_set_ldisc
,
1344 /* rs_init inits the driver */
1349 struct m68k_serial
*info
;
1351 serial_driver
= alloc_tty_driver(NR_PORTS
);
1355 show_serial_version();
1357 /* Initialize the tty_driver structure */
1358 /* SPARC: Not all of this is exactly right for us. */
1360 serial_driver
->name
= "ttyS";
1361 serial_driver
->major
= TTY_MAJOR
;
1362 serial_driver
->minor_start
= 64;
1363 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1364 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1365 serial_driver
->init_termios
= tty_std_termios
;
1366 serial_driver
->init_termios
.c_cflag
=
1367 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1368 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1369 tty_set_operations(serial_driver
, &rs_ops
);
1371 if (tty_register_driver(serial_driver
)) {
1372 put_tty_driver(serial_driver
);
1373 printk(KERN_ERR
"Couldn't register serial driver\n");
1377 local_irq_save(flags
);
1379 for(i
=0;i
<NR_PORTS
;i
++) {
1381 info
= &m68k_soft
[i
];
1382 info
->magic
= SERIAL_MAGIC
;
1383 info
->port
= (int) &uart_addr
[i
];
1385 info
->irq
= uart_irqs
[i
];
1386 info
->custom_divisor
= 16;
1387 info
->close_delay
= 50;
1388 info
->closing_wait
= 3000;
1392 info
->blocked_open
= 0;
1393 INIT_WORK(&info
->tqueue
, do_softint
);
1394 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
);
1395 init_waitqueue_head(&info
->open_wait
);
1396 init_waitqueue_head(&info
->close_wait
);
1398 info
->is_cons
= 1; /* Means shortcuts work */
1400 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1401 info
->port
, info
->irq
);
1402 printk(" is a builtin MC68328 UART\n");
1404 IRQ_ports
[info
->irq
] = info
; /* waste of space */
1406 #ifdef CONFIG_M68VZ328
1408 PJSEL
&= 0xCF; /* PSW enable second port output */
1411 if (request_irq(uart_irqs
[i
],
1414 "M68328_UART", NULL
))
1415 panic("Unable to attach 68328 serial interrupt\n");
1417 local_irq_restore(flags
);
1421 module_init(rs68328_init
);
1425 static void m68328_set_baud(void)
1427 unsigned short ustcnt
;
1431 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1434 for (i
= 0; i
< sizeof(baud_table
) / sizeof(baud_table
[0]); i
++)
1435 if (baud_table
[i
] == m68328_console_baud
)
1437 if (i
>= sizeof(baud_table
) / sizeof(baud_table
[0])) {
1438 m68328_console_baud
= 9600;
1442 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1443 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1444 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1445 ustcnt
|= USTCNT_8_7
;
1446 ustcnt
|= USTCNT_TXEN
;
1448 m68328_console_initted
= 1;
1453 int m68328_console_setup(struct console
*cp
, char *arg
)
1455 int i
, n
= CONSOLE_BAUD_RATE
;
1461 n
= simple_strtoul(arg
,NULL
,0);
1463 for (i
= 0; i
< BAUD_TABLE_SIZE
; i
++)
1464 if (baud_table
[i
] == n
)
1466 if (i
< BAUD_TABLE_SIZE
) {
1467 m68328_console_baud
= n
;
1468 m68328_console_cbaud
= 0;
1470 m68328_console_cbaud
|= CBAUDEX
;
1473 m68328_console_cbaud
|= i
;
1476 m68328_set_baud(); /* make sure baud rate changes */
1481 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1484 return serial_driver
;
1488 void m68328_console_write (struct console
*co
, const char *str
,
1491 if (!m68328_console_initted
)
1496 rs_put_char( *str
++ );
1501 static struct console m68328_driver
= {
1503 .write
= m68328_console_write
,
1504 .device
= m68328_console_device
,
1505 .setup
= m68328_console_setup
,
1506 .flags
= CON_PRINTBUFFER
,
1511 static int __init
m68328_console_init(void)
1513 register_console(&m68328_driver
);
1517 console_initcall(m68328_console_init
);