1 /* 68328serial.c: Serial port driver for 68328 microcontroller
3 * Copyright (C) 1995 David S. Miller <davem@caip.rutgers.edu>
4 * Copyright (C) 1998 Kenneth Albanowski <kjahds@kjahds.com>
5 * Copyright (C) 1998, 1999 D. Jeff Dionne <jeff@uclinux.org>
6 * Copyright (C) 1999 Vladimir Gurevich <vgurevic@cisco.com>
7 * Copyright (C) 2002-2003 David McCullough <davidm@snapgear.com>
8 * Copyright (C) 2002 Greg Ungerer <gerg@snapgear.com>
10 * VZ Support/Fixes Evan Stawnyczy <e@lineo.ca>
11 * Multiple UART support Daniel Potts <danielp@cse.unsw.edu.au>
12 * Power management support Daniel Potts <danielp@cse.unsw.edu.au>
13 * VZ Second Serial Port enable Phil Wilshire
14 * 2.4/2.5 port David McCullough
18 #include <linux/module.h>
19 #include <linux/errno.h>
20 #include <linux/signal.h>
21 #include <linux/sched.h>
22 #include <linux/timer.h>
23 #include <linux/interrupt.h>
24 #include <linux/tty.h>
25 #include <linux/tty_flip.h>
26 #include <linux/major.h>
27 #include <linux/string.h>
28 #include <linux/fcntl.h>
30 #include <linux/kernel.h>
31 #include <linux/console.h>
32 #include <linux/reboot.h>
33 #include <linux/keyboard.h>
34 #include <linux/init.h>
36 #include <linux/bitops.h>
37 #include <linux/delay.h>
41 #include <asm/system.h>
42 #include <asm/delay.h>
43 #include <asm/uaccess.h>
46 /* note: perhaps we can murge these files, so that you can just
47 * define 1 of them, and they can sort that out for themselves
49 #if defined(CONFIG_M68EZ328)
50 #include <asm/MC68EZ328.h>
52 #if defined(CONFIG_M68VZ328)
53 #include <asm/MC68VZ328.h>
55 #include <asm/MC68328.h>
56 #endif /* CONFIG_M68VZ328 */
57 #endif /* CONFIG_M68EZ328 */
59 #include "68328serial.h"
61 /* Turn off usage of real serial interrupt code, to "support" Copilot */
62 #ifdef CONFIG_XCOPILOT_BUGS
68 static struct m68k_serial m68k_soft
[NR_PORTS
];
69 struct m68k_serial
*IRQ_ports
[NR_IRQS
];
71 static unsigned int uart_irqs
[NR_PORTS
] = UART_IRQ_DEFNS
;
73 /* multiple ports are contiguous in memory */
74 m68328_uart
*uart_addr
= (m68328_uart
*)USTCNT_ADDR
;
76 struct tty_struct m68k_ttys
;
77 struct m68k_serial
*m68k_consinfo
= 0;
79 #define M68K_CLOCK (16667000) /* FIXME: 16MHz is likely wrong */
82 extern wait_queue_head_t keypress_wait
;
85 struct tty_driver
*serial_driver
;
87 /* number of characters left in xmit buffer before we ask for more */
88 #define WAKEUP_CHARS 256
90 /* Debugging... DEBUG_INTR is bad to use when one of the zs
91 * lines is your console ;(
93 #undef SERIAL_DEBUG_INTR
94 #undef SERIAL_DEBUG_OPEN
95 #undef SERIAL_DEBUG_FLOW
97 #define RS_ISR_PASS_LIMIT 256
99 static void change_speed(struct m68k_serial
*info
);
102 * Setup for console. Argument comes from the boot command line.
105 #if defined(CONFIG_M68EZ328ADS) || defined(CONFIG_ALMA_ANS) || defined(CONFIG_DRAGONIXVZ)
106 #define CONSOLE_BAUD_RATE 115200
107 #define DEFAULT_CBAUD B115200
110 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
111 #ifdef CONFIG_M68VZ328
112 #define CONSOLE_BAUD_RATE 19200
113 #define DEFAULT_CBAUD B19200
118 #ifndef CONSOLE_BAUD_RATE
119 #define CONSOLE_BAUD_RATE 9600
120 #define DEFAULT_CBAUD B9600
124 static int m68328_console_initted
= 0;
125 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
126 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
129 static inline int serial_paranoia_check(struct m68k_serial
*info
,
130 char *name
, const char *routine
)
132 #ifdef SERIAL_PARANOIA_CHECK
133 static const char *badmagic
=
134 "Warning: bad magic number for serial struct %s in %s\n";
135 static const char *badinfo
=
136 "Warning: null m68k_serial for %s in %s\n";
139 printk(badinfo
, name
, routine
);
142 if (info
->magic
!= SERIAL_MAGIC
) {
143 printk(badmagic
, name
, routine
);
151 * This is used to figure out the divisor speeds and the timeouts
153 static int baud_table
[] = {
154 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
155 9600, 19200, 38400, 57600, 115200, 0 };
157 #define BAUD_TABLE_SIZE (sizeof(baud_table)/sizeof(baud_table[0]))
159 /* Sets or clears DTR/RTS on the requested line */
160 static inline void m68k_rtsdtr(struct m68k_serial
*ss
, int set
)
163 /* set the RTS/CTS line */
170 /* Utility routines */
171 static inline int get_baud(struct m68k_serial
*ss
)
173 unsigned long result
= 115200;
174 unsigned short int baud
= uart_addr
[ss
->line
].ubaud
;
175 if (GET_FIELD(baud
, UBAUD_PRESCALER
) == 0x38) result
= 38400;
176 result
>>= GET_FIELD(baud
, UBAUD_DIVIDE
);
182 * ------------------------------------------------------------
183 * rs_stop() and rs_start()
185 * This routines are called before setting or resetting tty->stopped.
186 * They enable or disable transmitter interrupts, as necessary.
187 * ------------------------------------------------------------
189 static void rs_stop(struct tty_struct
*tty
)
191 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
192 m68328_uart
*uart
= &uart_addr
[info
->line
];
195 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
198 local_irq_save(flags
);
199 uart
->ustcnt
&= ~USTCNT_TXEN
;
200 local_irq_restore(flags
);
203 static void rs_put_char(char ch
)
205 int flags
, loops
= 0;
207 local_irq_save(flags
);
209 while (!(UTX
& UTX_TX_AVAIL
) && (loops
< 1000)) {
216 local_irq_restore(flags
);
219 static void rs_start(struct tty_struct
*tty
)
221 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
222 m68328_uart
*uart
= &uart_addr
[info
->line
];
225 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
228 local_irq_save(flags
);
229 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->ustcnt
& USTCNT_TXEN
)) {
231 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
233 uart
->ustcnt
|= USTCNT_TXEN
;
236 local_irq_restore(flags
);
239 /* Drop into either the boot monitor or kadb upon receiving a break
240 * from keyboard/console input.
242 static void batten_down_hatches(void)
244 /* Drop into the debugger */
247 static void status_handle(struct m68k_serial
*info
, unsigned short status
)
251 if((info
->tty
->termios
->c_cflag
& CRTSCTS
) &&
252 ((info
->curregs
[3] & AUTO_ENAB
)==0)) {
253 info
->curregs
[3] |= AUTO_ENAB
;
254 info
->pendregs
[3] |= AUTO_ENAB
;
255 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
258 if((info
->curregs
[3] & AUTO_ENAB
)) {
259 info
->curregs
[3] &= ~AUTO_ENAB
;
260 info
->pendregs
[3] &= ~AUTO_ENAB
;
261 write_zsreg(info
->m68k_channel
, 3, info
->curregs
[3]);
265 /* If this is console input and this is a
266 * 'break asserted' status change interrupt
267 * see if we can drop into the debugger
269 if((status
& URX_BREAK
) && info
->break_abort
)
270 batten_down_hatches();
274 static void receive_chars(struct m68k_serial
*info
, unsigned short rx
)
276 struct tty_struct
*tty
= info
->tty
;
277 m68328_uart
*uart
= &uart_addr
[info
->line
];
278 unsigned char ch
, flag
;
281 * This do { } while() loop will get ALL chars out of Rx FIFO
283 #ifndef CONFIG_XCOPILOT_BUGS
286 ch
= GET_FIELD(rx
, URX_RXDATA
);
289 if(URX_BREAK
& rx
) { /* whee, break received */
290 status_handle(info
, rx
);
292 #ifdef CONFIG_MAGIC_SYSRQ
293 } else if (ch
== 0x10) { /* ^P */
297 /* show_net_buffers(); */
299 } else if (ch
== 0x12) { /* ^R */
302 #endif /* CONFIG_MAGIC_SYSRQ */
304 /* It is a 'keyboard interrupt' ;-) */
305 #ifdef CONFIG_CONSOLE
306 wake_up(&keypress_wait
);
315 if(rx
& URX_PARITY_ERROR
) {
317 status_handle(info
, rx
);
318 } else if(rx
& URX_OVRUN
) {
320 status_handle(info
, rx
);
321 } else if(rx
& URX_FRAME_ERROR
) {
323 status_handle(info
, rx
);
325 tty_insert_flip_char(tty
, ch
, flag
);
326 #ifndef CONFIG_XCOPILOT_BUGS
327 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
330 tty_schedule_flip(tty
);
336 static void transmit_chars(struct m68k_serial
*info
)
338 m68328_uart
*uart
= &uart_addr
[info
->line
];
342 uart
->utx
.b
.txdata
= info
->x_char
;
344 goto clear_and_return
;
347 if((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
348 /* That's peculiar... TX ints off */
349 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
350 goto clear_and_return
;
354 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
355 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
358 if (info
->xmit_cnt
< WAKEUP_CHARS
)
359 schedule_work(&info
->tqueue
);
361 if(info
->xmit_cnt
<= 0) {
362 /* All done for now... TX ints off */
363 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
364 goto clear_and_return
;
368 /* Clear interrupt (should be auto)*/
373 * This is the serial driver's generic interrupt routine
375 irqreturn_t
rs_interrupt(int irq
, void *dev_id
)
377 struct m68k_serial
* info
;
382 info
= IRQ_ports
[irq
];
386 uart
= &uart_addr
[info
->line
];
392 if (rx
& URX_DATA_READY
) receive_chars(info
, rx
);
393 if (tx
& UTX_TX_AVAIL
) transmit_chars(info
);
395 receive_chars(info
, rx
);
400 static void do_softint(struct work_struct
*work
)
402 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue
);
403 struct tty_struct
*tty
;
409 if (clear_bit(RS_EVENT_WRITE_WAKEUP
, &info
->event
)) {
416 * This routine is called from the scheduler tqueue when the interrupt
417 * routine has signalled that a hangup has occurred. The path of
418 * hangup processing is:
420 * serial interrupt routine -> (scheduler tqueue) ->
421 * do_serial_hangup() -> tty->hangup() -> rs_hangup()
424 static void do_serial_hangup(struct work_struct
*work
)
426 struct m68k_serial
*info
= container_of(work
, struct m68k_serial
, tqueue_hangup
);
427 struct tty_struct
*tty
;
437 static int startup(struct m68k_serial
* info
)
439 m68328_uart
*uart
= &uart_addr
[info
->line
];
442 if (info
->flags
& S_INITIALIZED
)
445 if (!info
->xmit_buf
) {
446 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
451 local_irq_save(flags
);
454 * Clear the FIFO buffers and disable them
455 * (they will be reenabled in change_speed())
458 uart
->ustcnt
= USTCNT_UEN
;
459 info
->xmit_fifo_size
= 1;
460 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
464 * Finally, enable sequencing and interrupts
467 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
468 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
470 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
474 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
475 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
478 * and set the speed of the serial port
483 info
->flags
|= S_INITIALIZED
;
484 local_irq_restore(flags
);
489 * This routine will shutdown a serial port; interrupts are disabled, and
490 * DTR is dropped if the hangup on close termio flag is on.
492 static void shutdown(struct m68k_serial
* info
)
494 m68328_uart
*uart
= &uart_addr
[info
->line
];
497 uart
->ustcnt
= 0; /* All off! */
498 if (!(info
->flags
& S_INITIALIZED
))
501 local_irq_save(flags
);
503 if (info
->xmit_buf
) {
504 free_page((unsigned long) info
->xmit_buf
);
509 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
511 info
->flags
&= ~S_INITIALIZED
;
512 local_irq_restore(flags
);
516 int divisor
, prescale
;
518 #ifndef CONFIG_M68VZ328
519 hw_baud_table
[18] = {
534 {1,0x26}, /* 19200 */
535 {0,0x26}, /* 38400 */
536 {1,0x38}, /* 57600 */
537 {0,0x38}, /* 115200 */
540 hw_baud_table
[18] = {
555 {2,0x26}, /* 19200 */
556 {1,0x26}, /* 38400 */
557 {0,0x26}, /* 57600 */
558 {1,0x38}, /* 115200 */
561 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
564 * This routine is called to set the UART divisor registers to match
565 * the specified baud rate for a serial port.
567 static void change_speed(struct m68k_serial
*info
)
569 m68328_uart
*uart
= &uart_addr
[info
->line
];
571 unsigned short ustcnt
;
575 if (!info
->tty
|| !info
->tty
->termios
)
577 cflag
= info
->tty
->termios
->c_cflag
;
578 if (!(port
= info
->port
))
581 ustcnt
= uart
->ustcnt
;
582 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
586 i
= (i
& ~CBAUDEX
) + B38400
;
589 info
->baud
= baud_table
[i
];
590 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
591 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
593 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
595 if ((cflag
& CSIZE
) == CS8
)
596 ustcnt
|= USTCNT_8_7
;
599 ustcnt
|= USTCNT_STOP
;
602 ustcnt
|= USTCNT_PARITYEN
;
604 ustcnt
|= USTCNT_ODD_EVEN
;
606 #ifdef CONFIG_SERIAL_68328_RTS_CTS
607 if (cflag
& CRTSCTS
) {
608 uart
->utx
.w
&= ~ UTX_NOCTS
;
610 uart
->utx
.w
|= UTX_NOCTS
;
614 ustcnt
|= USTCNT_TXEN
;
616 uart
->ustcnt
= ustcnt
;
621 * Fair output driver allows a process to speak.
623 static void rs_fair_output(void)
625 int left
; /* Output no more than that */
627 struct m68k_serial
*info
= &m68k_soft
[0];
630 if (info
== 0) return;
631 if (info
->xmit_buf
== 0) return;
633 local_irq_save(flags
);
634 left
= info
->xmit_cnt
;
636 c
= info
->xmit_buf
[info
->xmit_tail
];
637 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
639 local_irq_restore(flags
);
643 local_irq_save(flags
);
644 left
= min(info
->xmit_cnt
, left
-1);
647 /* Last character is being transmitted now (hopefully). */
650 local_irq_restore(flags
);
655 * m68k_console_print is registered for printk.
657 void console_print_68328(const char *p
)
661 while((c
=*(p
++)) != 0) {
667 /* Comment this if you want to have a strict interrupt-driven output */
673 static void rs_set_ldisc(struct tty_struct
*tty
)
675 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
677 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
680 info
->is_cons
= (tty
->termios
->c_line
== N_TTY
);
682 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
685 static void rs_flush_chars(struct tty_struct
*tty
)
687 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
688 m68328_uart
*uart
= &uart_addr
[info
->line
];
691 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
697 /* Enable transmitter */
698 local_irq_save(flags
);
700 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
702 local_irq_restore(flags
);
707 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
709 uart
->ustcnt
|= USTCNT_TXEN
;
713 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
718 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
719 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
724 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
727 local_irq_restore(flags
);
730 extern void console_printn(const char * b
, int count
);
732 static int rs_write(struct tty_struct
* tty
,
733 const unsigned char *buf
, int count
)
736 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
737 m68328_uart
*uart
= &uart_addr
[info
->line
];
740 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
743 if (!tty
|| !info
->xmit_buf
)
746 local_save_flags(flags
);
749 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
750 SERIAL_XMIT_SIZE
- info
->xmit_head
));
751 local_irq_restore(flags
);
756 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
759 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
761 local_irq_restore(flags
);
767 if (info
->xmit_cnt
&& !tty
->stopped
&& !tty
->hw_stopped
) {
768 /* Enable transmitter */
771 while(info
->xmit_cnt
) {
774 uart
->ustcnt
|= USTCNT_TXEN
;
776 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
778 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
780 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
781 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
782 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
789 local_irq_restore(flags
);
795 static int rs_write_room(struct tty_struct
*tty
)
797 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
800 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
802 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
808 static int rs_chars_in_buffer(struct tty_struct
*tty
)
810 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
812 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
814 return info
->xmit_cnt
;
817 static void rs_flush_buffer(struct tty_struct
*tty
)
819 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
822 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
824 local_irq_save(flags
);
825 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
826 local_irq_restore(flags
);
831 * ------------------------------------------------------------
834 * This routine is called by the upper-layer tty layer to signal that
835 * incoming characters should be throttled.
836 * ------------------------------------------------------------
838 static void rs_throttle(struct tty_struct
* tty
)
840 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
842 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
846 info
->x_char
= STOP_CHAR(tty
);
848 /* Turn off RTS line (do this atomic) */
851 static void rs_unthrottle(struct tty_struct
* tty
)
853 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
855 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
862 info
->x_char
= START_CHAR(tty
);
865 /* Assert RTS line (do this atomic) */
869 * ------------------------------------------------------------
870 * rs_ioctl() and friends
871 * ------------------------------------------------------------
874 static int get_serial_info(struct m68k_serial
* info
,
875 struct serial_struct
* retinfo
)
877 struct serial_struct tmp
;
881 memset(&tmp
, 0, sizeof(tmp
));
882 tmp
.type
= info
->type
;
883 tmp
.line
= info
->line
;
884 tmp
.port
= info
->port
;
886 tmp
.flags
= info
->flags
;
887 tmp
.baud_base
= info
->baud_base
;
888 tmp
.close_delay
= info
->close_delay
;
889 tmp
.closing_wait
= info
->closing_wait
;
890 tmp
.custom_divisor
= info
->custom_divisor
;
891 copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
));
895 static int set_serial_info(struct m68k_serial
* info
,
896 struct serial_struct
* new_info
)
898 struct serial_struct new_serial
;
899 struct m68k_serial old_info
;
904 copy_from_user(&new_serial
,new_info
,sizeof(new_serial
));
907 if (!capable(CAP_SYS_ADMIN
)) {
908 if ((new_serial
.baud_base
!= info
->baud_base
) ||
909 (new_serial
.type
!= info
->type
) ||
910 (new_serial
.close_delay
!= info
->close_delay
) ||
911 ((new_serial
.flags
& ~S_USR_MASK
) !=
912 (info
->flags
& ~S_USR_MASK
)))
914 info
->flags
= ((info
->flags
& ~S_USR_MASK
) |
915 (new_serial
.flags
& S_USR_MASK
));
916 info
->custom_divisor
= new_serial
.custom_divisor
;
924 * OK, past this point, all the error checking has been done.
925 * At this point, we start making changes.....
928 info
->baud_base
= new_serial
.baud_base
;
929 info
->flags
= ((info
->flags
& ~S_FLAGS
) |
930 (new_serial
.flags
& S_FLAGS
));
931 info
->type
= new_serial
.type
;
932 info
->close_delay
= new_serial
.close_delay
;
933 info
->closing_wait
= new_serial
.closing_wait
;
936 retval
= startup(info
);
941 * get_lsr_info - get line status register info
943 * Purpose: Let user call ioctl() to get info when the UART physically
944 * is emptied. On bus types like RS485, the transmitter must
945 * release the bus after transmitting. This must be done when
946 * the transmit shift register is empty, not be done when the
947 * transmit holding register is empty. This functionality
948 * allows an RS485 driver to be written in user space.
950 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
952 #ifdef CONFIG_SERIAL_68328_RTS_CTS
953 m68328_uart
*uart
= &uart_addr
[info
->line
];
955 unsigned char status
;
958 local_irq_save(flags
);
959 #ifdef CONFIG_SERIAL_68328_RTS_CTS
960 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
964 local_irq_restore(flags
);
965 put_user(status
,value
);
970 * This routine sends a break character out the serial port.
972 static void send_break(struct m68k_serial
* info
, unsigned int duration
)
974 m68328_uart
*uart
= &uart_addr
[info
->line
];
978 local_irq_save(flags
);
980 uart
->utx
.w
|= UTX_SEND_BREAK
;
981 msleep_interruptible(duration
);
982 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
984 local_irq_restore(flags
);
987 static int rs_ioctl(struct tty_struct
*tty
, struct file
* file
,
988 unsigned int cmd
, unsigned long arg
)
991 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
994 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
997 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
998 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
999 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1000 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1005 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1006 retval
= tty_check_change(tty
);
1009 tty_wait_until_sent(tty
, 0);
1011 send_break(info
, 250); /* 1/4 second */
1013 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1014 retval
= tty_check_change(tty
);
1017 tty_wait_until_sent(tty
, 0);
1018 send_break(info
, arg
? arg
*(100) : 250);
1021 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1022 sizeof(struct serial_struct
)))
1023 return get_serial_info(info
,
1024 (struct serial_struct
*) arg
);
1027 return set_serial_info(info
,
1028 (struct serial_struct
*) arg
);
1029 case TIOCSERGETLSR
: /* Get line status register */
1030 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1031 sizeof(unsigned int)))
1032 return get_lsr_info(info
, (unsigned int *) arg
);
1034 case TIOCSERGSTRUCT
:
1035 if (!access_ok(VERIFY_WRITE
, (void *) arg
,
1036 sizeof(struct m68k_serial
)))
1038 copy_to_user((struct m68k_serial
*) arg
,
1039 info
, sizeof(struct m68k_serial
));
1043 return -ENOIOCTLCMD
;
1048 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1050 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
1054 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1055 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1056 tty
->hw_stopped
= 0;
1063 * ------------------------------------------------------------
1066 * This routine is called when the serial port gets closed. First, we
1067 * wait for the last remaining data to be sent. Then, we unlink its
1068 * S structure from the interrupt chain if necessary, and we free
1069 * that IRQ if nothing is left in the chain.
1070 * ------------------------------------------------------------
1072 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
1074 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1075 m68328_uart
*uart
= &uart_addr
[info
->line
];
1076 unsigned long flags
;
1078 if (!info
|| serial_paranoia_check(info
, tty
->name
, "rs_close"))
1081 local_irq_save(flags
);
1083 if (tty_hung_up_p(filp
)) {
1084 local_irq_restore(flags
);
1088 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1090 * Uh, oh. tty->count is 1, which means that the tty
1091 * structure will be freed. Info->count should always
1092 * be one in these conditions. If it's greater than
1093 * one, we've got real problems, since it means the
1094 * serial port won't be shutdown.
1096 printk("rs_close: bad serial port count; tty->count is 1, "
1097 "info->count is %d\n", info
->count
);
1100 if (--info
->count
< 0) {
1101 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1102 info
->line
, info
->count
);
1106 local_irq_restore(flags
);
1109 info
->flags
|= S_CLOSING
;
1111 * Now we wait for the transmit buffer to clear; and we notify
1112 * the line discipline to only process XON/XOFF characters.
1115 if (info
->closing_wait
!= S_CLOSING_WAIT_NONE
)
1116 tty_wait_until_sent(tty
, info
->closing_wait
);
1118 * At this point we stop accepting input. To do this, we
1119 * disable the receive line status interrupts, and tell the
1120 * interrupt driver to stop checking the data ready bit in the
1121 * line status register.
1124 uart
->ustcnt
&= ~USTCNT_RXEN
;
1125 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1128 rs_flush_buffer(tty
);
1130 tty_ldisc_flush(tty
);
1134 #warning "This is not and has never been valid so fix it"
1136 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1137 if (tty
->ldisc
.close
)
1138 (tty
->ldisc
.close
)(tty
);
1139 tty
->ldisc
= ldiscs
[N_TTY
];
1140 tty
->termios
->c_line
= N_TTY
;
1141 if (tty
->ldisc
.open
)
1142 (tty
->ldisc
.open
)(tty
);
1145 if (info
->blocked_open
) {
1146 if (info
->close_delay
) {
1147 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1149 wake_up_interruptible(&info
->open_wait
);
1151 info
->flags
&= ~(S_NORMAL_ACTIVE
|S_CLOSING
);
1152 wake_up_interruptible(&info
->close_wait
);
1153 local_irq_restore(flags
);
1157 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1159 void rs_hangup(struct tty_struct
*tty
)
1161 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1163 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1166 rs_flush_buffer(tty
);
1170 info
->flags
&= ~S_NORMAL_ACTIVE
;
1172 wake_up_interruptible(&info
->open_wait
);
1176 * ------------------------------------------------------------
1177 * rs_open() and friends
1178 * ------------------------------------------------------------
1180 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1181 struct m68k_serial
*info
)
1183 DECLARE_WAITQUEUE(wait
, current
);
1188 * If the device is in the middle of being closed, then block
1189 * until it's done, and then try again.
1191 if (info
->flags
& S_CLOSING
) {
1192 interruptible_sleep_on(&info
->close_wait
);
1193 #ifdef SERIAL_DO_RESTART
1194 if (info
->flags
& S_HUP_NOTIFY
)
1197 return -ERESTARTSYS
;
1204 * If non-blocking mode is set, or the port is not enabled,
1205 * then make the check up front and then exit.
1207 if ((filp
->f_flags
& O_NONBLOCK
) ||
1208 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1209 info
->flags
|= S_NORMAL_ACTIVE
;
1213 if (tty
->termios
->c_cflag
& CLOCAL
)
1217 * Block waiting for the carrier detect and the line to become
1218 * free (i.e., not in use by the callout). While we are in
1219 * this loop, info->count is dropped by one, so that
1220 * rs_close() knows when to free things. We restore it upon
1221 * exit, either normal or abnormal.
1224 add_wait_queue(&info
->open_wait
, &wait
);
1227 info
->blocked_open
++;
1229 local_irq_disable();
1230 m68k_rtsdtr(info
, 1);
1232 current
->state
= TASK_INTERRUPTIBLE
;
1233 if (tty_hung_up_p(filp
) ||
1234 !(info
->flags
& S_INITIALIZED
)) {
1235 #ifdef SERIAL_DO_RESTART
1236 if (info
->flags
& S_HUP_NOTIFY
)
1239 retval
= -ERESTARTSYS
;
1245 if (!(info
->flags
& S_CLOSING
) && do_clocal
)
1247 if (signal_pending(current
)) {
1248 retval
= -ERESTARTSYS
;
1253 current
->state
= TASK_RUNNING
;
1254 remove_wait_queue(&info
->open_wait
, &wait
);
1255 if (!tty_hung_up_p(filp
))
1257 info
->blocked_open
--;
1261 info
->flags
|= S_NORMAL_ACTIVE
;
1266 * This routine is called whenever a serial port is opened. It
1267 * enables interrupts for a serial port, linking in its S structure into
1268 * the IRQ chain. It also performs the serial-specific
1269 * initialization for the tty structure.
1271 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1273 struct m68k_serial
*info
;
1278 if (line
>= NR_PORTS
|| line
< 0) /* we have exactly one */
1281 info
= &m68k_soft
[line
];
1283 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1287 tty
->driver_data
= info
;
1291 * Start up serial port
1293 retval
= startup(info
);
1297 return block_til_ready(tty
, filp
, info
);
1300 /* Finally, routines used to initialize the serial driver. */
1302 static void show_serial_version(void)
1304 printk("MC68328 serial driver version 1.00\n");
1307 static const struct tty_operations rs_ops
= {
1311 .flush_chars
= rs_flush_chars
,
1312 .write_room
= rs_write_room
,
1313 .chars_in_buffer
= rs_chars_in_buffer
,
1314 .flush_buffer
= rs_flush_buffer
,
1316 .throttle
= rs_throttle
,
1317 .unthrottle
= rs_unthrottle
,
1318 .set_termios
= rs_set_termios
,
1321 .hangup
= rs_hangup
,
1322 .set_ldisc
= rs_set_ldisc
,
1325 /* rs_init inits the driver */
1330 struct m68k_serial
*info
;
1332 serial_driver
= alloc_tty_driver(NR_PORTS
);
1336 show_serial_version();
1338 /* Initialize the tty_driver structure */
1339 /* SPARC: Not all of this is exactly right for us. */
1341 serial_driver
->name
= "ttyS";
1342 serial_driver
->major
= TTY_MAJOR
;
1343 serial_driver
->minor_start
= 64;
1344 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1345 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1346 serial_driver
->init_termios
= tty_std_termios
;
1347 serial_driver
->init_termios
.c_cflag
=
1348 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1349 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1350 tty_set_operations(serial_driver
, &rs_ops
);
1352 if (tty_register_driver(serial_driver
)) {
1353 put_tty_driver(serial_driver
);
1354 printk(KERN_ERR
"Couldn't register serial driver\n");
1358 local_irq_save(flags
);
1360 for(i
=0;i
<NR_PORTS
;i
++) {
1362 info
= &m68k_soft
[i
];
1363 info
->magic
= SERIAL_MAGIC
;
1364 info
->port
= (int) &uart_addr
[i
];
1366 info
->irq
= uart_irqs
[i
];
1367 info
->custom_divisor
= 16;
1368 info
->close_delay
= 50;
1369 info
->closing_wait
= 3000;
1373 info
->blocked_open
= 0;
1374 INIT_WORK(&info
->tqueue
, do_softint
);
1375 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
);
1376 init_waitqueue_head(&info
->open_wait
);
1377 init_waitqueue_head(&info
->close_wait
);
1379 info
->is_cons
= 1; /* Means shortcuts work */
1381 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1382 info
->port
, info
->irq
);
1383 printk(" is a builtin MC68328 UART\n");
1385 IRQ_ports
[info
->irq
] = info
; /* waste of space */
1387 #ifdef CONFIG_M68VZ328
1389 PJSEL
&= 0xCF; /* PSW enable second port output */
1392 if (request_irq(uart_irqs
[i
],
1395 "M68328_UART", NULL
))
1396 panic("Unable to attach 68328 serial interrupt\n");
1398 local_irq_restore(flags
);
1402 module_init(rs68328_init
);
1406 static void m68328_set_baud(void)
1408 unsigned short ustcnt
;
1412 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1415 for (i
= 0; i
< sizeof(baud_table
) / sizeof(baud_table
[0]); i
++)
1416 if (baud_table
[i
] == m68328_console_baud
)
1418 if (i
>= sizeof(baud_table
) / sizeof(baud_table
[0])) {
1419 m68328_console_baud
= 9600;
1423 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1424 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1425 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1426 ustcnt
|= USTCNT_8_7
;
1427 ustcnt
|= USTCNT_TXEN
;
1429 m68328_console_initted
= 1;
1434 int m68328_console_setup(struct console
*cp
, char *arg
)
1436 int i
, n
= CONSOLE_BAUD_RATE
;
1442 n
= simple_strtoul(arg
,NULL
,0);
1444 for (i
= 0; i
< BAUD_TABLE_SIZE
; i
++)
1445 if (baud_table
[i
] == n
)
1447 if (i
< BAUD_TABLE_SIZE
) {
1448 m68328_console_baud
= n
;
1449 m68328_console_cbaud
= 0;
1451 m68328_console_cbaud
|= CBAUDEX
;
1454 m68328_console_cbaud
|= i
;
1457 m68328_set_baud(); /* make sure baud rate changes */
1462 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1465 return serial_driver
;
1469 void m68328_console_write (struct console
*co
, const char *str
,
1472 if (!m68328_console_initted
)
1477 rs_put_char( *str
++ );
1482 static struct console m68328_driver
= {
1484 .write
= m68328_console_write
,
1485 .device
= m68328_console_device
,
1486 .setup
= m68328_console_setup
,
1487 .flags
= CON_PRINTBUFFER
,
1492 static int __init
m68328_console_init(void)
1494 register_console(&m68328_driver
);
1498 console_initcall(m68328_console_init
);