1 /* 68328serial.c: Serial port driver for 68328 microcontroller
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port David McCullough
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/fcntl.h>
30 #include <linux/kernel.h>
31 #include <linux/console.h>
32 #include <linux/reboot.h>
33 #include <linux/keyboard.h>
34 #include <linux/init.h>
36 #include <linux/bitops.h>
37 #include <linux/delay.h>
38 #include <linux/gfp.h>
42 #include <asm/system.h>
43 #include <asm/delay.h>
44 #include <asm/uaccess.h>
47 /* note: perhaps we can murge these files, so that you can just
48 * define 1 of them, and they can sort that out for themselves
50 #if defined(CONFIG_M68EZ328)
51 #include <asm/MC68EZ328.h>
53 #if defined(CONFIG_M68VZ328)
54 #include <asm/MC68VZ328.h>
56 #include <asm/MC68328.h>
57 #endif /* CONFIG_M68VZ328 */
58 #endif /* CONFIG_M68EZ328 */
60 #include "68328serial.h"
62 /* Turn off usage of real serial interrupt code, to "support" Copilot */
63 #ifdef CONFIG_XCOPILOT_BUGS
69 static struct m68k_serial m68k_soft
[NR_PORTS
];
71 static unsigned int uart_irqs
[NR_PORTS
] = UART_IRQ_DEFNS
;
73 /* multiple ports are contiguous in memory */
74 m68328_uart
*uart_addr
= (m68328_uart
*)USTCNT_ADDR
;
76 struct tty_struct m68k_ttys
;
77 struct m68k_serial
*m68k_consinfo
= 0;
79 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
82 extern wait_queue_head_t keypress_wait
;
85 struct tty_driver
*serial_driver
;
87 /* number of characters left in xmit buffer before we ask for more */
88 #define WAKEUP_CHARS 256
90 /* Debugging... DEBUG_INTR is bad to use when one of the zs
91 * lines is your console ;(
93 #undef SERIAL_DEBUG_INTR
94 #undef SERIAL_DEBUG_OPEN
95 #undef SERIAL_DEBUG_FLOW
97 #define RS_ISR_PASS_LIMIT 256
99 static void change_speed(struct m68k_serial
*info
);
102 * Setup for console. Argument comes from the boot command line.
105 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
106 #define CONSOLE_BAUD_RATE 115200
107 #define DEFAULT_CBAUD B115200
110 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
111 #ifdef CONFIG_M68VZ328
112 #define CONSOLE_BAUD_RATE 19200
113 #define DEFAULT_CBAUD B19200
118 #ifndef CONSOLE_BAUD_RATE
119 #define CONSOLE_BAUD_RATE 9600
120 #define DEFAULT_CBAUD B9600
124 static int m68328_console_initted
= 0;
125 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
126 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
129 static inline int serial_paranoia_check(struct m68k_serial
*info
,
130 char *name
, const char *routine
)
132 #ifdef SERIAL_PARANOIA_CHECK
133 static const char *badmagic
=
134 "Warning: bad magic number for serial struct %s in %s\n";
135 static const char *badinfo
=
136 "Warning: null m68k_serial for %s in %s\n";
139 printk(badinfo
, name
, routine
);
142 if (info
->magic
!= SERIAL_MAGIC
) {
143 printk(badmagic
, name
, routine
);
151 * This is used to figure out the divisor speeds and the timeouts
153 static int baud_table
[] = {
154 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
155 9600, 19200, 38400, 57600, 115200, 0 };
157 /* Sets or clears DTR/RTS on the requested line */
158 static inline void m68k_rtsdtr(struct m68k_serial
*ss
, int set
)
161 /* set the RTS/CTS line */
168 /* Utility routines */
169 static inline int get_baud(struct m68k_serial
*ss
)
171 unsigned long result
= 115200;
172 unsigned short int baud
= uart_addr
[ss
->line
].ubaud
;
173 if (GET_FIELD(baud
, UBAUD_PRESCALER
) == 0x38) result
= 38400;
174 result
>>= GET_FIELD(baud
, UBAUD_DIVIDE
);
180 * ------------------------------------------------------------
181 * rs_stop() and rs_start()
183 * This routines are called before setting or resetting tty->stopped.
184 * They enable or disable transmitter interrupts, as necessary.
185 * ------------------------------------------------------------
187 static void rs_stop(struct tty_struct
*tty
)
189 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
190 m68328_uart
*uart
= &uart_addr
[info
->line
];
193 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
196 local_irq_save(flags
);
197 uart
->ustcnt
&= ~USTCNT_TXEN
;
198 local_irq_restore(flags
);
201 static int rs_put_char(char ch
)
203 int flags
, loops
= 0;
205 local_irq_save(flags
);
207 while (!(UTX
& UTX_TX_AVAIL
) && (loops
< 1000)) {
214 local_irq_restore(flags
);
218 static void rs_start(struct tty_struct
*tty
)
220 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
221 m68328_uart
*uart
= &uart_addr
[info
->line
];
224 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
227 local_irq_save(flags
);
228 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->ustcnt
& USTCNT_TXEN
)) {
230 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
232 uart
->ustcnt
|= USTCNT_TXEN
;
235 local_irq_restore(flags
);
238 /* Drop into either the boot monitor or kadb upon receiving a break
239 * from keyboard/console input.
241 static void batten_down_hatches(void)
243 /* Drop into the debugger */
246 static void status_handle(struct m68k_serial
*info
, unsigned short status
)
250 if((info
->port
.tty
->termios
->c_cflag
& CRTSCTS
) &&
251 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
252 info
->curregs
[3] |= AUTO_ENAB
;
253 info
->pendregs
[3] |= AUTO_ENAB
;
254 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
257 if((info
->curregs
[3] & AUTO_ENAB
)) {
258 info
->curregs
[3] &= ~AUTO_ENAB
;
259 info
->pendregs
[3] &= ~AUTO_ENAB
;
260 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
264 /* If this is console input and this is a
265 * 'break asserted' status change interrupt
266 * see if we can drop into the debugger
268 if((status
& URX_BREAK
) && info
->break_abort
)
269 batten_down_hatches();
273 static void receive_chars(struct m68k_serial
*info
, unsigned short rx
)
275 struct tty_struct
*tty
= info
->port
.tty
;
276 m68328_uart
*uart
= &uart_addr
[info
->line
];
277 unsigned char ch
, flag
;
280 * This do { } while() loop will get ALL chars out of Rx FIFO
282 #ifndef CONFIG_XCOPILOT_BUGS
285 ch
= GET_FIELD(rx
, URX_RXDATA
);
288 if(URX_BREAK
& rx
) { /* whee, break received */
289 status_handle(info
, rx
);
291 #ifdef CONFIG_MAGIC_SYSRQ
292 } else if (ch
== 0x10) { /* ^P */
296 /* show_net_buffers(); */
298 } else if (ch
== 0x12) { /* ^R */
301 #endif /* CONFIG_MAGIC_SYSRQ */
303 /* It is a 'keyboard interrupt' ;-) */
304 #ifdef CONFIG_CONSOLE
305 wake_up(&keypress_wait
);
314 if(rx
& URX_PARITY_ERROR
) {
316 status_handle(info
, rx
);
317 } else if(rx
& URX_OVRUN
) {
319 status_handle(info
, rx
);
320 } else if(rx
& URX_FRAME_ERROR
) {
322 status_handle(info
, rx
);
324 tty_insert_flip_char(tty
, ch
, flag
);
325 #ifndef CONFIG_XCOPILOT_BUGS
326 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
329 tty_schedule_flip(tty
);
335 static void transmit_chars(struct m68k_serial
*info
)
337 m68328_uart
*uart
= &uart_addr
[info
->line
];
341 uart
->utx
.b
.txdata
= info
->x_char
;
343 goto clear_and_return
;
346 if((info
->xmit_cnt
<= 0) || info
->port
.tty
->stopped
) {
347 /* That's peculiar... TX ints off */
348 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
349 goto clear_and_return
;
353 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
354 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
357 if (info
->xmit_cnt
< WAKEUP_CHARS
)
358 schedule_work(&info
->tqueue
);
360 if(info
->xmit_cnt
<= 0) {
361 /* All done for now... TX ints off */
362 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
363 goto clear_and_return
;
367 /* Clear interrupt (should be auto)*/
372 * This is the serial driver's generic interrupt routine
374 irqreturn_t
rs_interrupt(int irq
, void *dev_id
)
376 struct m68k_serial
*info
= dev_id
;
381 uart
= &uart_addr
[info
->line
];
387 if (rx
& URX_DATA_READY
) receive_chars(info
, rx
);
388 if (tx
& UTX_TX_AVAIL
) transmit_chars(info
);
390 receive_chars(info
, rx
);
395 static void do_softint(struct work_struct
*work
)
397 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue
);
398 struct tty_struct
*tty
;
400 tty
= info
->port
.tty
;
404 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
411 * This routine is called from the scheduler tqueue when the interrupt
412 * routine has signalled that a hangup has occurred. The path of
413 * hangup processing is:
415 * serial interrupt routine -> (scheduler tqueue) ->
416 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
419 static void do_serial_hangup(struct work_struct
*work
)
421 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue_hangup
);
422 struct tty_struct
*tty
;
424 tty
= info
->port
.tty
;
432 static int startup(struct m68k_serial
* info
)
434 m68328_uart
*uart
= &uart_addr
[info
->line
];
437 if (info
->flags
& S_INITIALIZED
)
440 if (!info
->xmit_buf
) {
441 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
446 local_irq_save(flags
);
449 * Clear the FIFO buffers and disable them
450 * (they will be reenabled in change_speed())
453 uart
->ustcnt
= USTCNT_UEN
;
454 info
->xmit_fifo_size
= 1;
455 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
459 * Finally, enable sequencing and interrupts
462 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
463 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
465 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
469 clear_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
470 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
473 * and set the speed of the serial port
478 info
->flags
|= S_INITIALIZED
;
479 local_irq_restore(flags
);
484 * This routine will shutdown a serial port; interrupts are disabled, and
485 * DTR is dropped if the hangup on close termio flag is on.
487 static void shutdown(struct m68k_serial
* info
)
489 m68328_uart
*uart
= &uart_addr
[info
->line
];
492 uart
->ustcnt
= 0; /* All off! */
493 if (!(info
->flags
& S_INITIALIZED
))
496 local_irq_save(flags
);
498 if (info
->xmit_buf
) {
499 free_page((unsigned long) info
->xmit_buf
);
504 set_bit(TTY_IO_ERROR
, &info
->port
.tty
->flags
);
506 info
->flags
&= ~S_INITIALIZED
;
507 local_irq_restore(flags
);
511 int divisor
, prescale
;
513 #ifndef CONFIG_M68VZ328
514 hw_baud_table
[18] = {
529 {1,0x26}, /* 19200 */
530 {0,0x26}, /* 38400 */
531 {1,0x38}, /* 57600 */
532 {0,0x38}, /* 115200 */
535 hw_baud_table
[18] = {
550 {2,0x26}, /* 19200 */
551 {1,0x26}, /* 38400 */
552 {0,0x26}, /* 57600 */
553 {1,0x38}, /* 115200 */
556 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
559 * This routine is called to set the UART divisor registers to match
560 * the specified baud rate for a serial port.
562 static void change_speed(struct m68k_serial
*info
)
564 m68328_uart
*uart
= &uart_addr
[info
->line
];
566 unsigned short ustcnt
;
570 if (!info
->port
.tty
|| !info
->port
.tty
->termios
)
572 cflag
= info
->port
.tty
->termios
->c_cflag
;
573 if (!(port
= info
->port
))
576 ustcnt
= uart
->ustcnt
;
577 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
581 i
= (i
& ~CBAUDEX
) + B38400
;
584 info
->baud
= baud_table
[i
];
585 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
586 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
588 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
590 if ((cflag
& CSIZE
) == CS8
)
591 ustcnt
|= USTCNT_8_7
;
594 ustcnt
|= USTCNT_STOP
;
597 ustcnt
|= USTCNT_PARITYEN
;
599 ustcnt
|= USTCNT_ODD_EVEN
;
601 #ifdef CONFIG_SERIAL_68328_RTS_CTS
602 if (cflag
& CRTSCTS
) {
603 uart
->utx
.w
&= ~ UTX_NOCTS
;
605 uart
->utx
.w
|= UTX_NOCTS
;
609 ustcnt
|= USTCNT_TXEN
;
611 uart
->ustcnt
= ustcnt
;
616 * Fair output driver allows a process to speak.
618 static void rs_fair_output(void)
620 int left
; /* Output no more than that */
622 struct m68k_serial
*info
= &m68k_soft
[0];
625 if (info
== 0) return;
626 if (info
->xmit_buf
== 0) return;
628 local_irq_save(flags
);
629 left
= info
->xmit_cnt
;
631 c
= info
->xmit_buf
[info
->xmit_tail
];
632 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
634 local_irq_restore(flags
);
638 local_irq_save(flags
);
639 left
= min(info
->xmit_cnt
, left
-1);
642 /* Last character is being transmitted now (hopefully). */
645 local_irq_restore(flags
);
650 * m68k_console_print is registered for printk.
652 void console_print_68328(const char *p
)
656 while((c
=*(p
++)) != 0) {
662 /* Comment this if you want to have a strict interrupt-driven output */
668 static void rs_set_ldisc(struct tty_struct
*tty
)
670 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
672 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
675 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
677 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
680 static void rs_flush_chars(struct tty_struct
*tty
)
682 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
683 m68328_uart
*uart
= &uart_addr
[info
->line
];
686 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
692 /* Enable transmitter */
693 local_irq_save(flags
);
695 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
697 local_irq_restore(flags
);
702 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
704 uart
->ustcnt
|= USTCNT_TXEN
;
708 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
713 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
714 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
719 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
722 local_irq_restore(flags
);
725 extern void console_printn(const char * b
, int count
);
727 static int rs_write(struct tty_struct
* tty
,
728 const unsigned char *buf
, int count
)
731 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
732 m68328_uart
*uart
= &uart_addr
[info
->line
];
735 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
738 if (!tty
|| !info
->xmit_buf
)
741 local_save_flags(flags
);
744 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
745 SERIAL_XMIT_SIZE
- info
->xmit_head
));
746 local_irq_restore(flags
);
751 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
754 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
756 local_irq_restore(flags
);
762 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
763 /* Enable transmitter */
766 while(info
->xmit_cnt
) {
769 uart
->ustcnt
|= USTCNT_TXEN
;
771 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
773 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
775 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
776 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
777 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
784 local_irq_restore(flags
);
790 static int rs_write_room(struct tty_struct
*tty
)
792 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
795 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
797 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
803 static int rs_chars_in_buffer(struct tty_struct
*tty
)
805 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
807 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
809 return info
->xmit_cnt
;
812 static void rs_flush_buffer(struct tty_struct
*tty
)
814 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
817 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
819 local_irq_save(flags
);
820 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
821 local_irq_restore(flags
);
826 * ------------------------------------------------------------
829 * This routine is called by the upper-layer tty layer to signal that
830 * incoming characters should be throttled.
831 * ------------------------------------------------------------
833 static void rs_throttle(struct tty_struct
* tty
)
835 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
837 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
841 info
->x_char
= STOP_CHAR(tty
);
843 /* Turn off RTS line (do this atomic) */
846 static void rs_unthrottle(struct tty_struct
* tty
)
848 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
850 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
857 info
->x_char
= START_CHAR(tty
);
860 /* Assert RTS line (do this atomic) */
864 * ------------------------------------------------------------
865 * rs_ioctl() and friends
866 * ------------------------------------------------------------
869 static int get_serial_info(struct m68k_serial
* info
,
870 struct serial_struct
* retinfo
)
872 struct serial_struct tmp
;
876 memset(&tmp
, 0, sizeof(tmp
));
877 tmp
.type
= info
->type
;
878 tmp
.line
= info
->line
;
879 tmp
.port
= info
->port
;
881 tmp
.flags
= info
->flags
;
882 tmp
.baud_base
= info
->baud_base
;
883 tmp
.close_delay
= info
->close_delay
;
884 tmp
.closing_wait
= info
->closing_wait
;
885 tmp
.custom_divisor
= info
->custom_divisor
;
886 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
890 static int set_serial_info(struct m68k_serial
* info
,
891 struct serial_struct
* new_info
)
893 struct serial_struct new_serial
;
894 struct m68k_serial old_info
;
899 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
902 if (!capable(CAP_SYS_ADMIN
)) {
903 if ((new_serial
.baud_base
!= info
->baud_base
) ||
904 (new_serial
.type
!= info
->type
) ||
905 (new_serial
.close_delay
!= info
->close_delay
) ||
906 ((new_serial
.flags
& ~S_USR_MASK
) !=
907 (info
->flags
& ~S_USR_MASK
)))
909 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
910 (new_serial
.flags
& S_USR_MASK
));
911 info
->custom_divisor
= new_serial
.custom_divisor
;
919 * OK, past this point, all the error checking has been done.
920 * At this point, we start making changes.....
923 info
->baud_base
= new_serial
.baud_base
;
924 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
925 (new_serial
.flags
& S_FLAGS
));
926 info
->type
= new_serial
.type
;
927 info
->close_delay
= new_serial
.close_delay
;
928 info
->closing_wait
= new_serial
.closing_wait
;
931 retval
= startup(info
);
936 * get_lsr_info - get line status register info
938 * Purpose: Let user call ioctl() to get info when the UART physically
939 * is emptied. On bus types like RS485, the transmitter must
940 * release the bus after transmitting. This must be done when
941 * the transmit shift register is empty, not be done when the
942 * transmit holding register is empty. This functionality
943 * allows an RS485 driver to be written in user space.
945 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
947 #ifdef CONFIG_SERIAL_68328_RTS_CTS
948 m68328_uart
*uart
= &uart_addr
[info
->line
];
950 unsigned char status
;
953 local_irq_save(flags
);
954 #ifdef CONFIG_SERIAL_68328_RTS_CTS
955 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
959 local_irq_restore(flags
);
960 put_user(status
,value
);
965 * This routine sends a break character out the serial port.
967 static void send_break(struct m68k_serial
* info
, unsigned int duration
)
969 m68328_uart
*uart
= &uart_addr
[info
->line
];
973 local_irq_save(flags
);
975 uart
->utx
.w
|= UTX_SEND_BREAK
;
976 msleep_interruptible(duration
);
977 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
979 local_irq_restore(flags
);
982 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
983 unsigned int cmd
, unsigned long arg
)
986 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
989 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
992 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
993 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
994 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
995 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1000 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1001 retval
= tty_check_change(tty
);
1004 tty_wait_until_sent(tty
, 0);
1006 send_break(info
, 250); /* 1/4 second */
1008 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1009 retval
= tty_check_change(tty
);
1012 tty_wait_until_sent(tty
, 0);
1013 send_break(info
, arg
? arg
*(100) : 250);
1016 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1017 sizeof(struct serial_struct
)))
1018 return get_serial_info(info
,
1019 (struct serial_struct
*) arg
);
1022 return set_serial_info(info
,
1023 (struct serial_struct
*) arg
);
1024 case TIOCSERGETLSR
: /* Get line status register */
1025 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1026 sizeof(unsigned int)))
1027 return get_lsr_info(info
, (unsigned int *) arg
);
1029 case TIOCSERGSTRUCT
:
1030 if (!access_ok(VERIFY_WRITE
, (void *) arg
,
1031 sizeof(struct m68k_serial
)))
1033 copy_to_user((struct m68k_serial
*) arg
,
1034 info
, sizeof(struct m68k_serial
));
1038 return -ENOIOCTLCMD
;
1043 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1045 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
1049 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1050 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1051 tty
->hw_stopped
= 0;
1058 * ------------------------------------------------------------
1061 * This routine is called when the serial port gets closed. First, we
1062 * wait for the last remaining data to be sent. Then, we unlink its
1063 * S structure from the interrupt chain if necessary, and we free
1064 * that IRQ if nothing is left in the chain.
1065 * ------------------------------------------------------------
1067 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1069 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1070 m68328_uart
*uart
= &uart_addr
[info
->line
];
1071 unsigned long flags
;
1073 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1076 local_irq_save(flags
);
1078 if (tty_hung_up_p(filp
)) {
1079 local_irq_restore(flags
);
1083 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1085 * Uh, oh. tty->count is 1, which means that the tty
1086 * structure will be freed. Info->count should always
1087 * be one in these conditions. If it's greater than
1088 * one, we've got real problems, since it means the
1089 * serial port won't be shutdown.
1091 printk("rs_close: bad serial port count; tty->count is 1, "
1092 "info->count is %d\n", info
->count
);
1095 if (--info
->count
< 0) {
1096 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1097 info
->line
, info
->count
);
1101 local_irq_restore(flags
);
1104 info
->flags
|= S_CLOSING
;
1106 * Now we wait for the transmit buffer to clear; and we notify
1107 * the line discipline to only process XON/XOFF characters.
1110 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
1111 tty_wait_until_sent(tty
, info
->closing_wait
);
1113 * At this point we stop accepting input. To do this, we
1114 * disable the receive line status interrupts, and tell the
1115 * interrupt driver to stop checking the data ready bit in the
1116 * line status register.
1119 uart
->ustcnt
&= ~USTCNT_RXEN
;
1120 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1123 rs_flush_buffer(tty
);
1125 tty_ldisc_flush(tty
);
1128 info
->port
.tty
= NULL
;
1129 #warning "This is not and has never been valid so fix it"
1131 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1132 if (tty
->ldisc
.close
)
1133 (tty
->ldisc
.close
)(tty
);
1134 tty
->ldisc
= ldiscs
[N_TTY
];
1135 tty
->termios
->c_line
= N_TTY
;
1136 if (tty
->ldisc
.open
)
1137 (tty
->ldisc
.open
)(tty
);
1140 if (info
->blocked_open
) {
1141 if (info
->close_delay
) {
1142 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1144 wake_up_interruptible(&info
->open_wait
);
1146 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1147 wake_up_interruptible(&info
->close_wait
);
1148 local_irq_restore(flags
);
1152 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1154 void rs_hangup(struct tty_struct
*tty
)
1156 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1158 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1161 rs_flush_buffer(tty
);
1165 info
->flags
&= ~S_NORMAL_ACTIVE
;
1166 info
->port
.tty
= NULL
;
1167 wake_up_interruptible(&info
->open_wait
);
1171 * ------------------------------------------------------------
1172 * rs_open() and friends
1173 * ------------------------------------------------------------
1175 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1176 struct m68k_serial
*info
)
1178 DECLARE_WAITQUEUE(wait
, current
);
1183 * If the device is in the middle of being closed, then block
1184 * until it's done, and then try again.
1186 if (info
->flags
& S_CLOSING
) {
1187 interruptible_sleep_on(&info
->close_wait
);
1188 #ifdef SERIAL_DO_RESTART
1189 if (info
->flags
& S_HUP_NOTIFY
)
1192 return -ERESTARTSYS
;
1199 * If non-blocking mode is set, or the port is not enabled,
1200 * then make the check up front and then exit.
1202 if ((filp
->f_flags
& O_NONBLOCK
) ||
1203 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1204 info
->flags
|= S_NORMAL_ACTIVE
;
1208 if (tty
->termios
->c_cflag
& CLOCAL
)
1212 * Block waiting for the carrier detect and the line to become
1213 * free (i.e., not in use by the callout). While we are in
1214 * this loop, info->count is dropped by one, so that
1215 * rs_close() knows when to free things. We restore it upon
1216 * exit, either normal or abnormal.
1219 add_wait_queue(&info
->open_wait
, &wait
);
1222 info
->blocked_open
++;
1224 local_irq_disable();
1225 m68k_rtsdtr(info
, 1);
1227 current
->state
= TASK_INTERRUPTIBLE
;
1228 if (tty_hung_up_p(filp
) ||
1229 !(info
->flags
& S_INITIALIZED
)) {
1230 #ifdef SERIAL_DO_RESTART
1231 if (info
->flags
& S_HUP_NOTIFY
)
1234 retval
= -ERESTARTSYS
;
1240 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1242 if (signal_pending(current
)) {
1243 retval
= -ERESTARTSYS
;
1248 current
->state
= TASK_RUNNING
;
1249 remove_wait_queue(&info
->open_wait
, &wait
);
1250 if (!tty_hung_up_p(filp
))
1252 info
->blocked_open
--;
1256 info
->flags
|= S_NORMAL_ACTIVE
;
1261 * This routine is called whenever a serial port is opened. It
1262 * enables interrupts for a serial port, linking in its S structure into
1263 * the IRQ chain. It also performs the serial-specific
1264 * initialization for the tty structure.
1266 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1268 struct m68k_serial
*info
;
1273 if (line
>= NR_PORTS
|| line
< 0) /* we have exactly one */
1276 info
= &m68k_soft
[line
];
1278 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1282 tty
->driver_data
= info
;
1283 info
->port
.tty
= tty
;
1286 * Start up serial port
1288 retval
= startup(info
);
1292 return block_til_ready(tty
, filp
, info
);
1295 /* Finally, routines used to initialize the serial driver. */
1297 static void show_serial_version(void)
1299 printk("MC68328 serial driver version 1.00\n");
1302 static const struct tty_operations rs_ops
= {
1306 .flush_chars
= rs_flush_chars
,
1307 .write_room
= rs_write_room
,
1308 .chars_in_buffer
= rs_chars_in_buffer
,
1309 .flush_buffer
= rs_flush_buffer
,
1311 .throttle
= rs_throttle
,
1312 .unthrottle
= rs_unthrottle
,
1313 .set_termios
= rs_set_termios
,
1316 .hangup
= rs_hangup
,
1317 .set_ldisc
= rs_set_ldisc
,
1320 /* rs_init inits the driver */
1325 struct m68k_serial
*info
;
1327 serial_driver
= alloc_tty_driver(NR_PORTS
);
1331 show_serial_version();
1333 /* Initialize the tty_driver structure */
1334 /* SPARC: Not all of this is exactly right for us. */
1336 serial_driver
->name
= "ttyS";
1337 serial_driver
->major
= TTY_MAJOR
;
1338 serial_driver
->minor_start
= 64;
1339 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1340 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1341 serial_driver
->init_termios
= tty_std_termios
;
1342 serial_driver
->init_termios
.c_cflag
=
1343 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1344 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1345 tty_set_operations(serial_driver
, &rs_ops
);
1347 if (tty_register_driver(serial_driver
)) {
1348 put_tty_driver(serial_driver
);
1349 printk(KERN_ERR
"Couldn't register serial driver\n");
1353 local_irq_save(flags
);
1355 for(i
=0;i
<NR_PORTS
;i
++) {
1357 info
= &m68k_soft
[i
];
1358 info
->magic
= SERIAL_MAGIC
;
1359 info
->port
= (int) &uart_addr
[i
];
1360 info
->port
.tty
= NULL
;
1361 info
->irq
= uart_irqs
[i
];
1362 info
->custom_divisor
= 16;
1363 info
->close_delay
= 50;
1364 info
->closing_wait
= 3000;
1368 info
->blocked_open
= 0;
1369 INIT_WORK(&info
->tqueue
, do_softint
);
1370 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
);
1371 init_waitqueue_head(&info
->open_wait
);
1372 init_waitqueue_head(&info
->close_wait
);
1374 info
->is_cons
= 1; /* Means shortcuts work */
1376 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1377 info
->port
, info
->irq
);
1378 printk(" is a builtin MC68328 UART\n");
1380 #ifdef CONFIG_M68VZ328
1382 PJSEL
&= 0xCF; /* PSW enable second port output */
1385 if (request_irq(uart_irqs
[i
],
1388 "M68328_UART", info
))
1389 panic("Unable to attach 68328 serial interrupt\n");
1391 local_irq_restore(flags
);
1395 module_init(rs68328_init
);
1399 static void m68328_set_baud(void)
1401 unsigned short ustcnt
;
1405 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1408 for (i
= 0; i
< ARRAY_SIZE(baud_table
); i
++)
1409 if (baud_table
[i
] == m68328_console_baud
)
1411 if (i
>= ARRAY_SIZE(baud_table
)) {
1412 m68328_console_baud
= 9600;
1416 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1417 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1418 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1419 ustcnt
|= USTCNT_8_7
;
1420 ustcnt
|= USTCNT_TXEN
;
1422 m68328_console_initted
= 1;
1427 int m68328_console_setup(struct console
*cp
, char *arg
)
1429 int i
, n
= CONSOLE_BAUD_RATE
;
1435 n
= simple_strtoul(arg
,NULL
,0);
1437 for (i
= 0; i
< ARRAY_SIZE(baud_table
); i
++)
1438 if (baud_table
[i
] == n
)
1440 if (i
< ARRAY_SIZE(baud_table
)) {
1441 m68328_console_baud
= n
;
1442 m68328_console_cbaud
= 0;
1444 m68328_console_cbaud
|= CBAUDEX
;
1447 m68328_console_cbaud
|= i
;
1450 m68328_set_baud(); /* make sure baud rate changes */
1455 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1458 return serial_driver
;
1462 void m68328_console_write (struct console
*co
, const char *str
,
1465 if (!m68328_console_initted
)
1470 rs_put_char( *str
++ );
1475 static struct console m68328_driver
= {
1477 .write
= m68328_console_write
,
1478 .device
= m68328_console_device
,
1479 .setup
= m68328_console_setup
,
1480 .flags
= CON_PRINTBUFFER
,
1485 static int __init
m68328_console_init(void)
1487 register_console(&m68328_driver
);
1491 console_initcall(m68328_console_init
);