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/serial.h>
21 #include <linux/signal.h>
22 #include <linux/sched.h>
23 #include <linux/timer.h>
24 #include <linux/interrupt.h>
25 #include <linux/tty.h>
26 #include <linux/tty_flip.h>
27 #include <linux/major.h>
28 #include <linux/string.h>
29 #include <linux/fcntl.h>
31 #include <linux/kernel.h>
32 #include <linux/console.h>
33 #include <linux/reboot.h>
34 #include <linux/keyboard.h>
35 #include <linux/init.h>
37 #include <linux/bitops.h>
38 #include <linux/delay.h>
39 #include <linux/gfp.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 /* Turn off usage of real serial interrupt code, to "support" Copilot */
61 #ifdef CONFIG_XCOPILOT_BUGS
68 * I believe this is the optimal setting that reduces the number of interrupts.
69 * At high speeds the output might become a little "bursted" (use USTCNT_TXHE
70 * if that bothers you), but in most cases it will not, since we try to
71 * transmit characters every time rs_interrupt is called. Thus, quite often
72 * you'll see that a receive interrupt occures before the transmit one.
73 * -- Vladimir Gurevich
75 #define USTCNT_TX_INTR_MASK (USTCNT_TXEE)
78 * 68328 and 68EZ328 UARTS are a little bit different. EZ328 has special
79 * "Old data interrupt" which occures whenever the data stay in the FIFO
80 * longer than 30 bits time. This allows us to use FIFO without compromising
81 * latency. '328 does not have this feature and without the real 328-based
82 * board I would assume that RXRE is the safest setting.
84 * For EZ328 I use RXHE (Half empty) interrupt to reduce the number of
85 * interrupts. RXFE (receive queue full) causes the system to lose data
86 * at least at 115200 baud
88 * If your board is busy doing other stuff, you might consider to use
89 * RXRE (data ready intrrupt) instead.
91 * The other option is to make these INTR masks run-time configurable, so
92 * that people can dynamically adapt them according to the current usage.
93 * -- Vladimir Gurevich
97 #if defined(CONFIG_M68EZ328) || defined(CONFIG_M68VZ328)
98 #define USTCNT_RX_INTR_MASK (USTCNT_RXHE | USTCNT_ODEN)
99 #elif defined(CONFIG_M68328)
100 #define USTCNT_RX_INTR_MASK (USTCNT_RXRE)
102 #error Please, define the Rx interrupt events for your CPU
107 * This is our internal structure for each serial port's state.
110 struct tty_port tport
;
111 char is_cons
; /* Is this our console. */
116 int type
; /* UART type */
118 int x_char
; /* xon/xoff character */
120 unsigned char *xmit_buf
;
126 #define SERIAL_MAGIC 0x5301
129 * Define the number of ports supported and their irqs.
133 static struct m68k_serial m68k_soft
[NR_PORTS
];
135 static unsigned int uart_irqs
[NR_PORTS
] = { UART_IRQ_NUM
};
137 /* multiple ports are contiguous in memory */
138 m68328_uart
*uart_addr
= (m68328_uart
*)USTCNT_ADDR
;
140 struct tty_driver
*serial_driver
;
142 static void change_speed(struct m68k_serial
*info
, struct tty_struct
*tty
);
145 * Setup for console. Argument comes from the boot command line.
148 /* note: this is messy, but it works, again, perhaps defined somewhere else?*/
149 #ifdef CONFIG_M68VZ328
150 #define CONSOLE_BAUD_RATE 19200
151 #define DEFAULT_CBAUD B19200
155 #ifndef CONSOLE_BAUD_RATE
156 #define CONSOLE_BAUD_RATE 9600
157 #define DEFAULT_CBAUD B9600
161 static int m68328_console_initted
= 0;
162 static int m68328_console_baud
= CONSOLE_BAUD_RATE
;
163 static int m68328_console_cbaud
= DEFAULT_CBAUD
;
166 static inline int serial_paranoia_check(struct m68k_serial
*info
,
167 char *name
, const char *routine
)
169 #ifdef SERIAL_PARANOIA_CHECK
170 static const char *badmagic
=
171 "Warning: bad magic number for serial struct %s in %s\n";
172 static const char *badinfo
=
173 "Warning: null m68k_serial for %s in %s\n";
176 printk(badinfo
, name
, routine
);
179 if (info
->magic
!= SERIAL_MAGIC
) {
180 printk(badmagic
, name
, routine
);
188 * This is used to figure out the divisor speeds and the timeouts
190 static int baud_table
[] = {
191 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
192 9600, 19200, 38400, 57600, 115200, 0 };
194 /* Utility routines */
195 static inline int get_baud(struct m68k_serial
*ss
)
197 unsigned long result
= 115200;
198 unsigned short int baud
= uart_addr
[ss
->line
].ubaud
;
199 if (GET_FIELD(baud
, UBAUD_PRESCALER
) == 0x38) result
= 38400;
200 result
>>= GET_FIELD(baud
, UBAUD_DIVIDE
);
206 * ------------------------------------------------------------
207 * rs_stop() and rs_start()
209 * This routines are called before setting or resetting tty->stopped.
210 * They enable or disable transmitter interrupts, as necessary.
211 * ------------------------------------------------------------
213 static void rs_stop(struct tty_struct
*tty
)
215 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
216 m68328_uart
*uart
= &uart_addr
[info
->line
];
219 if (serial_paranoia_check(info
, tty
->name
, "rs_stop"))
222 local_irq_save(flags
);
223 uart
->ustcnt
&= ~USTCNT_TXEN
;
224 local_irq_restore(flags
);
227 static int rs_put_char(char ch
)
232 local_irq_save(flags
);
234 while (!(UTX
& UTX_TX_AVAIL
) && (loops
< 1000)) {
241 local_irq_restore(flags
);
245 static void rs_start(struct tty_struct
*tty
)
247 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
248 m68328_uart
*uart
= &uart_addr
[info
->line
];
251 if (serial_paranoia_check(info
, tty
->name
, "rs_start"))
254 local_irq_save(flags
);
255 if (info
->xmit_cnt
&& info
->xmit_buf
&& !(uart
->ustcnt
& USTCNT_TXEN
)) {
257 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
259 uart
->ustcnt
|= USTCNT_TXEN
;
262 local_irq_restore(flags
);
265 static void receive_chars(struct m68k_serial
*info
, unsigned short rx
)
267 m68328_uart
*uart
= &uart_addr
[info
->line
];
268 unsigned char ch
, flag
;
271 * This do { } while() loop will get ALL chars out of Rx FIFO
273 #ifndef CONFIG_XCOPILOT_BUGS
276 ch
= GET_FIELD(rx
, URX_RXDATA
);
279 if(URX_BREAK
& rx
) { /* whee, break received */
281 #ifdef CONFIG_MAGIC_SYSRQ
282 } else if (ch
== 0x10) { /* ^P */
286 /* show_net_buffers(); */
288 } else if (ch
== 0x12) { /* ^R */
291 #endif /* CONFIG_MAGIC_SYSRQ */
297 if (rx
& URX_PARITY_ERROR
)
299 else if (rx
& URX_OVRUN
)
301 else if (rx
& URX_FRAME_ERROR
)
304 tty_insert_flip_char(&info
->tport
, ch
, flag
);
305 #ifndef CONFIG_XCOPILOT_BUGS
306 } while((rx
= uart
->urx
.w
) & URX_DATA_READY
);
309 tty_schedule_flip(&info
->tport
);
312 static void transmit_chars(struct m68k_serial
*info
, struct tty_struct
*tty
)
314 m68328_uart
*uart
= &uart_addr
[info
->line
];
318 uart
->utx
.b
.txdata
= info
->x_char
;
320 goto clear_and_return
;
323 if ((info
->xmit_cnt
<= 0) || !tty
|| tty
->stopped
) {
324 /* That's peculiar... TX ints off */
325 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
326 goto clear_and_return
;
330 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
331 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
334 if(info
->xmit_cnt
<= 0) {
335 /* All done for now... TX ints off */
336 uart
->ustcnt
&= ~USTCNT_TX_INTR_MASK
;
337 goto clear_and_return
;
341 /* Clear interrupt (should be auto)*/
346 * This is the serial driver's generic interrupt routine
348 irqreturn_t
rs_interrupt(int irq
, void *dev_id
)
350 struct m68k_serial
*info
= dev_id
;
351 struct tty_struct
*tty
= tty_port_tty_get(&info
->tport
);
356 uart
= &uart_addr
[info
->line
];
362 if (rx
& URX_DATA_READY
)
363 receive_chars(info
, rx
);
364 if (tx
& UTX_TX_AVAIL
)
365 transmit_chars(info
, tty
);
367 receive_chars(info
, rx
);
374 static int startup(struct m68k_serial
*info
, struct tty_struct
*tty
)
376 m68328_uart
*uart
= &uart_addr
[info
->line
];
379 if (info
->tport
.flags
& ASYNC_INITIALIZED
)
382 if (!info
->xmit_buf
) {
383 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
388 local_irq_save(flags
);
391 * Clear the FIFO buffers and disable them
392 * (they will be reenabled in change_speed())
395 uart
->ustcnt
= USTCNT_UEN
;
396 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_TXEN
;
400 * Finally, enable sequencing and interrupts
403 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
|
404 USTCNT_RX_INTR_MASK
| USTCNT_TX_INTR_MASK
;
406 uart
->ustcnt
= USTCNT_UEN
| USTCNT_RXEN
| USTCNT_RX_INTR_MASK
;
410 clear_bit(TTY_IO_ERROR
, &tty
->flags
);
411 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
414 * and set the speed of the serial port
417 change_speed(info
, tty
);
419 info
->tport
.flags
|= ASYNC_INITIALIZED
;
420 local_irq_restore(flags
);
425 * This routine will shutdown a serial port; interrupts are disabled, and
426 * DTR is dropped if the hangup on close termio flag is on.
428 static void shutdown(struct m68k_serial
*info
, struct tty_struct
*tty
)
430 m68328_uart
*uart
= &uart_addr
[info
->line
];
433 uart
->ustcnt
= 0; /* All off! */
434 if (!(info
->tport
.flags
& ASYNC_INITIALIZED
))
437 local_irq_save(flags
);
439 if (info
->xmit_buf
) {
440 free_page((unsigned long) info
->xmit_buf
);
445 set_bit(TTY_IO_ERROR
, &tty
->flags
);
447 info
->tport
.flags
&= ~ASYNC_INITIALIZED
;
448 local_irq_restore(flags
);
452 int divisor
, prescale
;
454 #ifndef CONFIG_M68VZ328
455 hw_baud_table
[18] = {
470 {1,0x26}, /* 19200 */
471 {0,0x26}, /* 38400 */
472 {1,0x38}, /* 57600 */
473 {0,0x38}, /* 115200 */
476 hw_baud_table
[18] = {
491 {2,0x26}, /* 19200 */
492 {1,0x26}, /* 38400 */
493 {0,0x26}, /* 57600 */
494 {1,0x38}, /* 115200 */
497 /* rate = 1036800 / ((65 - prescale) * (1<<divider)) */
500 * This routine is called to set the UART divisor registers to match
501 * the specified baud rate for a serial port.
503 static void change_speed(struct m68k_serial
*info
, struct tty_struct
*tty
)
505 m68328_uart
*uart
= &uart_addr
[info
->line
];
507 unsigned short ustcnt
;
511 cflag
= tty
->termios
.c_cflag
;
512 if (!(port
= info
->port
))
515 ustcnt
= uart
->ustcnt
;
516 uart
->ustcnt
= ustcnt
& ~USTCNT_TXEN
;
520 i
= (i
& ~CBAUDEX
) + B38400
;
523 uart
->ubaud
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
524 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
526 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
528 if ((cflag
& CSIZE
) == CS8
)
529 ustcnt
|= USTCNT_8_7
;
532 ustcnt
|= USTCNT_STOP
;
535 ustcnt
|= USTCNT_PARITYEN
;
537 ustcnt
|= USTCNT_ODD_EVEN
;
539 #ifdef CONFIG_SERIAL_68328_RTS_CTS
540 if (cflag
& CRTSCTS
) {
541 uart
->utx
.w
&= ~ UTX_NOCTS
;
543 uart
->utx
.w
|= UTX_NOCTS
;
547 ustcnt
|= USTCNT_TXEN
;
549 uart
->ustcnt
= ustcnt
;
554 * Fair output driver allows a process to speak.
556 static void rs_fair_output(void)
558 int left
; /* Output no more than that */
560 struct m68k_serial
*info
= &m68k_soft
[0];
563 if (info
== 0) return;
564 if (info
->xmit_buf
== 0) return;
566 local_irq_save(flags
);
567 left
= info
->xmit_cnt
;
569 c
= info
->xmit_buf
[info
->xmit_tail
];
570 info
->xmit_tail
= (info
->xmit_tail
+1) & (SERIAL_XMIT_SIZE
-1);
572 local_irq_restore(flags
);
576 local_irq_save(flags
);
577 left
= min(info
->xmit_cnt
, left
-1);
580 /* Last character is being transmitted now (hopefully). */
583 local_irq_restore(flags
);
588 * m68k_console_print is registered for printk.
590 void console_print_68328(const char *p
)
594 while((c
=*(p
++)) != 0) {
600 /* Comment this if you want to have a strict interrupt-driven output */
606 static void rs_set_ldisc(struct tty_struct
*tty
)
608 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
610 if (serial_paranoia_check(info
, tty
->name
, "rs_set_ldisc"))
613 info
->is_cons
= (tty
->termios
.c_line
== N_TTY
);
615 printk("ttyS%d console mode %s\n", info
->line
, info
->is_cons
? "on" : "off");
618 static void rs_flush_chars(struct tty_struct
*tty
)
620 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
621 m68328_uart
*uart
= &uart_addr
[info
->line
];
624 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_chars"))
630 /* Enable transmitter */
631 local_irq_save(flags
);
633 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| !info
->xmit_buf
) {
634 local_irq_restore(flags
);
639 uart
->ustcnt
|= USTCNT_TXEN
| USTCNT_TX_INTR_MASK
;
641 uart
->ustcnt
|= USTCNT_TXEN
;
645 if (uart
->utx
.w
& UTX_TX_AVAIL
) {
650 uart
->utx
.b
.txdata
= info
->xmit_buf
[info
->xmit_tail
++];
651 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
656 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
659 local_irq_restore(flags
);
662 extern void console_printn(const char * b
, int count
);
664 static int rs_write(struct tty_struct
* tty
,
665 const unsigned char *buf
, int count
)
668 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
669 m68328_uart
*uart
= &uart_addr
[info
->line
];
672 if (serial_paranoia_check(info
, tty
->name
, "rs_write"))
675 if (!tty
|| !info
->xmit_buf
)
678 local_save_flags(flags
);
681 c
= min_t(int, count
, min(SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1,
682 SERIAL_XMIT_SIZE
- info
->xmit_head
));
683 local_irq_restore(flags
);
688 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
691 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
693 local_irq_restore(flags
);
699 if (info
->xmit_cnt
&& !tty
->stopped
) {
700 /* Enable transmitter */
703 while(info
->xmit_cnt
) {
706 uart
->ustcnt
|= USTCNT_TXEN
;
708 uart
->ustcnt
|= USTCNT_TX_INTR_MASK
;
710 while (!(uart
->utx
.w
& UTX_TX_AVAIL
)) udelay(5);
712 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);
721 local_irq_restore(flags
);
727 static int rs_write_room(struct tty_struct
*tty
)
729 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
732 if (serial_paranoia_check(info
, tty
->name
, "rs_write_room"))
734 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
740 static int rs_chars_in_buffer(struct tty_struct
*tty
)
742 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
744 if (serial_paranoia_check(info
, tty
->name
, "rs_chars_in_buffer"))
746 return info
->xmit_cnt
;
749 static void rs_flush_buffer(struct tty_struct
*tty
)
751 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
754 if (serial_paranoia_check(info
, tty
->name
, "rs_flush_buffer"))
756 local_irq_save(flags
);
757 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
758 local_irq_restore(flags
);
763 * ------------------------------------------------------------
766 * This routine is called by the upper-layer tty layer to signal that
767 * incoming characters should be throttled.
768 * ------------------------------------------------------------
770 static void rs_throttle(struct tty_struct
* tty
)
772 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
774 if (serial_paranoia_check(info
, tty
->name
, "rs_throttle"))
778 info
->x_char
= STOP_CHAR(tty
);
780 /* Turn off RTS line (do this atomic) */
783 static void rs_unthrottle(struct tty_struct
* tty
)
785 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
787 if (serial_paranoia_check(info
, tty
->name
, "rs_unthrottle"))
794 info
->x_char
= START_CHAR(tty
);
797 /* Assert RTS line (do this atomic) */
801 * ------------------------------------------------------------
802 * rs_ioctl() and friends
803 * ------------------------------------------------------------
806 static int get_serial_info(struct m68k_serial
* info
,
807 struct serial_struct
* retinfo
)
809 struct serial_struct tmp
;
813 memset(&tmp
, 0, sizeof(tmp
));
814 tmp
.type
= info
->type
;
815 tmp
.line
= info
->line
;
816 tmp
.port
= info
->port
;
818 tmp
.flags
= info
->tport
.flags
;
819 tmp
.baud_base
= info
->baud_base
;
820 tmp
.close_delay
= info
->tport
.close_delay
;
821 tmp
.closing_wait
= info
->tport
.closing_wait
;
822 tmp
.custom_divisor
= info
->custom_divisor
;
823 if (copy_to_user(retinfo
, &tmp
, sizeof(*retinfo
)))
829 static int set_serial_info(struct m68k_serial
*info
, struct tty_struct
*tty
,
830 struct serial_struct
* new_info
)
832 struct tty_port
*port
= &info
->tport
;
833 struct serial_struct new_serial
;
834 struct m68k_serial old_info
;
839 if (copy_from_user(&new_serial
, new_info
, sizeof(new_serial
)))
843 if (!capable(CAP_SYS_ADMIN
)) {
844 if ((new_serial
.baud_base
!= info
->baud_base
) ||
845 (new_serial
.type
!= info
->type
) ||
846 (new_serial
.close_delay
!= port
->close_delay
) ||
847 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
848 (port
->flags
& ~ASYNC_USR_MASK
)))
850 port
->flags
= ((port
->flags
& ~ASYNC_USR_MASK
) |
851 (new_serial
.flags
& ASYNC_USR_MASK
));
852 info
->custom_divisor
= new_serial
.custom_divisor
;
860 * OK, past this point, all the error checking has been done.
861 * At this point, we start making changes.....
864 info
->baud_base
= new_serial
.baud_base
;
865 port
->flags
= ((port
->flags
& ~ASYNC_FLAGS
) |
866 (new_serial
.flags
& ASYNC_FLAGS
));
867 info
->type
= new_serial
.type
;
868 port
->close_delay
= new_serial
.close_delay
;
869 port
->closing_wait
= new_serial
.closing_wait
;
872 retval
= startup(info
, tty
);
877 * get_lsr_info - get line status register info
879 * Purpose: Let user call ioctl() to get info when the UART physically
880 * is emptied. On bus types like RS485, the transmitter must
881 * release the bus after transmitting. This must be done when
882 * the transmit shift register is empty, not be done when the
883 * transmit holding register is empty. This functionality
884 * allows an RS485 driver to be written in user space.
886 static int get_lsr_info(struct m68k_serial
* info
, unsigned int *value
)
888 #ifdef CONFIG_SERIAL_68328_RTS_CTS
889 m68328_uart
*uart
= &uart_addr
[info
->line
];
891 unsigned char status
;
894 local_irq_save(flags
);
895 #ifdef CONFIG_SERIAL_68328_RTS_CTS
896 status
= (uart
->utx
.w
& UTX_CTS_STAT
) ? 1 : 0;
900 local_irq_restore(flags
);
901 return put_user(status
, value
);
905 * This routine sends a break character out the serial port.
907 static void send_break(struct m68k_serial
* info
, unsigned int duration
)
909 m68328_uart
*uart
= &uart_addr
[info
->line
];
913 local_irq_save(flags
);
915 uart
->utx
.w
|= UTX_SEND_BREAK
;
916 msleep_interruptible(duration
);
917 uart
->utx
.w
&= ~UTX_SEND_BREAK
;
919 local_irq_restore(flags
);
922 static int rs_ioctl(struct tty_struct
*tty
,
923 unsigned int cmd
, unsigned long arg
)
925 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
928 if (serial_paranoia_check(info
, tty
->name
, "rs_ioctl"))
931 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
932 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
933 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
934 if (tty
->flags
& (1 << TTY_IO_ERROR
))
939 case TCSBRK
: /* SVID version: non-zero arg --> no break */
940 retval
= tty_check_change(tty
);
943 tty_wait_until_sent(tty
, 0);
945 send_break(info
, 250); /* 1/4 second */
947 case TCSBRKP
: /* support for POSIX tcsendbreak() */
948 retval
= tty_check_change(tty
);
951 tty_wait_until_sent(tty
, 0);
952 send_break(info
, arg
? arg
*(100) : 250);
955 return get_serial_info(info
,
956 (struct serial_struct
*) arg
);
958 return set_serial_info(info
, tty
,
959 (struct serial_struct
*) arg
);
960 case TIOCSERGETLSR
: /* Get line status register */
961 return get_lsr_info(info
, (unsigned int *) arg
);
963 if (copy_to_user((struct m68k_serial
*) arg
,
964 info
, sizeof(struct m68k_serial
)))
973 static void rs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
975 struct m68k_serial
*info
= (struct m68k_serial
*)tty
->driver_data
;
977 change_speed(info
, tty
);
979 if ((old_termios
->c_cflag
& CRTSCTS
) &&
980 !(tty
->termios
.c_cflag
& CRTSCTS
))
986 * ------------------------------------------------------------
989 * This routine is called when the serial port gets closed. First, we
990 * wait for the last remaining data to be sent. Then, we unlink its
991 * S structure from the interrupt chain if necessary, and we free
992 * that IRQ if nothing is left in the chain.
993 * ------------------------------------------------------------
995 static void rs_close(struct tty_struct
*tty
, struct file
* filp
)
997 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
998 struct tty_port
*port
= &info
->tport
;
999 m68328_uart
*uart
= &uart_addr
[info
->line
];
1000 unsigned long flags
;
1002 if (serial_paranoia_check(info
, tty
->name
, "rs_close"))
1005 local_irq_save(flags
);
1007 if (tty_hung_up_p(filp
)) {
1008 local_irq_restore(flags
);
1012 if ((tty
->count
== 1) && (port
->count
!= 1)) {
1014 * Uh, oh. tty->count is 1, which means that the tty
1015 * structure will be freed. Info->count should always
1016 * be one in these conditions. If it's greater than
1017 * one, we've got real problems, since it means the
1018 * serial port won't be shutdown.
1020 printk("rs_close: bad serial port count; tty->count is 1, "
1021 "port->count is %d\n", port
->count
);
1024 if (--port
->count
< 0) {
1025 printk("rs_close: bad serial port count for ttyS%d: %d\n",
1026 info
->line
, port
->count
);
1030 local_irq_restore(flags
);
1033 port
->flags
|= ASYNC_CLOSING
;
1035 * Now we wait for the transmit buffer to clear; and we notify
1036 * the line discipline to only process XON/XOFF characters.
1039 if (port
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
1040 tty_wait_until_sent(tty
, port
->closing_wait
);
1042 * At this point we stop accepting input. To do this, we
1043 * disable the receive line status interrupts, and tell the
1044 * interrupt driver to stop checking the data ready bit in the
1045 * line status register.
1048 uart
->ustcnt
&= ~USTCNT_RXEN
;
1049 uart
->ustcnt
&= ~(USTCNT_RXEN
| USTCNT_RX_INTR_MASK
);
1051 shutdown(info
, tty
);
1052 rs_flush_buffer(tty
);
1054 tty_ldisc_flush(tty
);
1056 tty_port_tty_set(&info
->tport
, NULL
);
1057 #warning "This is not and has never been valid so fix it"
1059 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1060 if (tty
->ldisc
.close
)
1061 (tty
->ldisc
.close
)(tty
);
1062 tty
->ldisc
= ldiscs
[N_TTY
];
1063 tty
->termios
.c_line
= N_TTY
;
1064 if (tty
->ldisc
.open
)
1065 (tty
->ldisc
.open
)(tty
);
1068 if (port
->blocked_open
) {
1069 if (port
->close_delay
)
1070 msleep_interruptible(jiffies_to_msecs(port
->close_delay
));
1071 wake_up_interruptible(&port
->open_wait
);
1073 port
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
1074 wake_up_interruptible(&port
->close_wait
);
1075 local_irq_restore(flags
);
1079 * rs_hangup() --- called by tty_hangup() when a hangup is signaled.
1081 void rs_hangup(struct tty_struct
*tty
)
1083 struct m68k_serial
* info
= (struct m68k_serial
*)tty
->driver_data
;
1085 if (serial_paranoia_check(info
, tty
->name
, "rs_hangup"))
1088 rs_flush_buffer(tty
);
1089 shutdown(info
, tty
);
1090 info
->tport
.count
= 0;
1091 info
->tport
.flags
&= ~ASYNC_NORMAL_ACTIVE
;
1092 tty_port_tty_set(&info
->tport
, NULL
);
1093 wake_up_interruptible(&info
->tport
.open_wait
);
1097 * This routine is called whenever a serial port is opened. It
1098 * enables interrupts for a serial port, linking in its S structure into
1099 * the IRQ chain. It also performs the serial-specific
1100 * initialization for the tty structure.
1102 int rs_open(struct tty_struct
*tty
, struct file
* filp
)
1104 struct m68k_serial
*info
;
1107 info
= &m68k_soft
[tty
->index
];
1109 if (serial_paranoia_check(info
, tty
->name
, "rs_open"))
1112 info
->tport
.count
++;
1113 tty
->driver_data
= info
;
1114 tty_port_tty_set(&info
->tport
, tty
);
1117 * Start up serial port
1119 retval
= startup(info
, tty
);
1123 return tty_port_block_til_ready(&info
->tport
, tty
, filp
);
1126 /* Finally, routines used to initialize the serial driver. */
1128 static void show_serial_version(void)
1130 printk("MC68328 serial driver version 1.00\n");
1133 static const struct tty_operations rs_ops
= {
1137 .flush_chars
= rs_flush_chars
,
1138 .write_room
= rs_write_room
,
1139 .chars_in_buffer
= rs_chars_in_buffer
,
1140 .flush_buffer
= rs_flush_buffer
,
1142 .throttle
= rs_throttle
,
1143 .unthrottle
= rs_unthrottle
,
1144 .set_termios
= rs_set_termios
,
1147 .hangup
= rs_hangup
,
1148 .set_ldisc
= rs_set_ldisc
,
1151 static const struct tty_port_operations rs_port_ops
= {
1154 /* rs_init inits the driver */
1158 unsigned long flags
;
1160 struct m68k_serial
*info
;
1162 serial_driver
= alloc_tty_driver(NR_PORTS
);
1166 show_serial_version();
1168 /* Initialize the tty_driver structure */
1169 /* SPARC: Not all of this is exactly right for us. */
1171 serial_driver
->name
= "ttyS";
1172 serial_driver
->major
= TTY_MAJOR
;
1173 serial_driver
->minor_start
= 64;
1174 serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1175 serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1176 serial_driver
->init_termios
= tty_std_termios
;
1177 serial_driver
->init_termios
.c_cflag
=
1178 m68328_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1179 serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1180 tty_set_operations(serial_driver
, &rs_ops
);
1182 local_irq_save(flags
);
1184 for(i
=0;i
<NR_PORTS
;i
++) {
1186 info
= &m68k_soft
[i
];
1187 tty_port_init(&info
->tport
);
1188 info
->tport
.ops
= &rs_port_ops
;
1189 info
->magic
= SERIAL_MAGIC
;
1190 info
->port
= (int) &uart_addr
[i
];
1191 info
->irq
= uart_irqs
[i
];
1192 info
->custom_divisor
= 16;
1195 info
->is_cons
= 1; /* Means shortcuts work */
1197 printk("%s%d at 0x%08x (irq = %d)", serial_driver
->name
, info
->line
,
1198 info
->port
, info
->irq
);
1199 printk(" is a builtin MC68328 UART\n");
1201 #ifdef CONFIG_M68VZ328
1203 PJSEL
&= 0xCF; /* PSW enable second port output */
1206 if (request_irq(uart_irqs
[i
],
1209 "M68328_UART", info
))
1210 panic("Unable to attach 68328 serial interrupt\n");
1212 tty_port_link_device(&info
->tport
, serial_driver
, i
);
1214 local_irq_restore(flags
);
1216 if (tty_register_driver(serial_driver
)) {
1217 put_tty_driver(serial_driver
);
1218 for (i
= 0; i
< NR_PORTS
; i
++)
1219 tty_port_destroy(&m68k_soft
[i
].tport
);
1220 printk(KERN_ERR
"Couldn't register serial driver\n");
1227 module_init(rs68328_init
);
1231 static void m68328_set_baud(void)
1233 unsigned short ustcnt
;
1237 USTCNT
= ustcnt
& ~USTCNT_TXEN
;
1240 for (i
= 0; i
< ARRAY_SIZE(baud_table
); i
++)
1241 if (baud_table
[i
] == m68328_console_baud
)
1243 if (i
>= ARRAY_SIZE(baud_table
)) {
1244 m68328_console_baud
= 9600;
1248 UBAUD
= PUT_FIELD(UBAUD_DIVIDE
, hw_baud_table
[i
].divisor
) |
1249 PUT_FIELD(UBAUD_PRESCALER
, hw_baud_table
[i
].prescale
);
1250 ustcnt
&= ~(USTCNT_PARITYEN
| USTCNT_ODD_EVEN
| USTCNT_STOP
| USTCNT_8_7
);
1251 ustcnt
|= USTCNT_8_7
;
1252 ustcnt
|= USTCNT_TXEN
;
1254 m68328_console_initted
= 1;
1259 int m68328_console_setup(struct console
*cp
, char *arg
)
1261 int i
, n
= CONSOLE_BAUD_RATE
;
1267 n
= simple_strtoul(arg
,NULL
,0);
1269 for (i
= 0; i
< ARRAY_SIZE(baud_table
); i
++)
1270 if (baud_table
[i
] == n
)
1272 if (i
< ARRAY_SIZE(baud_table
)) {
1273 m68328_console_baud
= n
;
1274 m68328_console_cbaud
= 0;
1276 m68328_console_cbaud
|= CBAUDEX
;
1279 m68328_console_cbaud
|= i
;
1282 m68328_set_baud(); /* make sure baud rate changes */
1287 static struct tty_driver
*m68328_console_device(struct console
*c
, int *index
)
1290 return serial_driver
;
1294 void m68328_console_write (struct console
*co
, const char *str
,
1297 if (!m68328_console_initted
)
1302 rs_put_char( *str
++ );
1307 static struct console m68328_driver
= {
1309 .write
= m68328_console_write
,
1310 .device
= m68328_console_device
,
1311 .setup
= m68328_console_setup
,
1312 .flags
= CON_PRINTBUFFER
,
1317 static int __init
m68328_console_init(void)
1319 register_console(&m68328_driver
);
1323 console_initcall(m68328_console_init
);