2 * mcfserial.c -- serial driver for ColdFire internal UARTS.
4 * Copyright (C) 1999-2003 Greg Ungerer <gerg@snapgear.com>
5 * Copyright (c) 2000-2001 Lineo, Inc. <www.lineo.com>
6 * Copyright (C) 2001-2002 SnapGear Inc. <www.snapgear.com>
8 * Based on code from 68332serial.c which was:
10 * Copyright (C) 1995 David S. Miller (davem@caip.rutgers.edu)
11 * Copyright (C) 1998 TSHG
12 * Copyright (c) 1999 Rt-Control Inc. <jeff@uclinux.org>
15 * 08/07/2003 Daniele Bellucci <bellucda@tiscali.it>
16 * some cleanups in mcfrs_write.
20 #include <linux/module.h>
21 #include <linux/errno.h>
22 #include <linux/signal.h>
23 #include <linux/sched.h>
24 #include <linux/timer.h>
25 #include <linux/wait.h>
26 #include <linux/interrupt.h>
27 #include <linux/tty.h>
28 #include <linux/tty_flip.h>
29 #include <linux/string.h>
30 #include <linux/fcntl.h>
32 #include <linux/kernel.h>
33 #include <linux/serial.h>
34 #include <linux/serialP.h>
35 #include <linux/console.h>
36 #include <linux/init.h>
37 #include <linux/bitops.h>
38 #include <linux/delay.h>
42 #include <asm/system.h>
43 #include <asm/semaphore.h>
44 #include <asm/delay.h>
45 #include <asm/coldfire.h>
46 #include <asm/mcfsim.h>
47 #include <asm/mcfuart.h>
48 #include <asm/nettel.h>
49 #include <asm/uaccess.h>
50 #include "mcfserial.h"
52 struct timer_list mcfrs_timer_struct
;
55 * Default console baud rate, we use this as the default
56 * for all ports so init can just open /dev/console and
57 * keep going. Perhaps one day the cflag settings for the
58 * console can be used instead.
60 #if defined(CONFIG_HW_FEITH)
61 #define CONSOLE_BAUD_RATE 38400
62 #define DEFAULT_CBAUD B38400
63 #elif defined(CONFIG_MOD5272) || defined(CONFIG_M5208EVB) || \
64 defined(CONFIG_M5329EVB) || defined(CONFIG_GILBARCO)
65 #define CONSOLE_BAUD_RATE 115200
66 #define DEFAULT_CBAUD B115200
67 #elif defined(CONFIG_ARNEWSH) || defined(CONFIG_FREESCALE) || \
68 defined(CONFIG_senTec) || defined(CONFIG_SNEHA) || defined(CONFIG_AVNET)
69 #define CONSOLE_BAUD_RATE 19200
70 #define DEFAULT_CBAUD B19200
73 #ifndef CONSOLE_BAUD_RATE
74 #define CONSOLE_BAUD_RATE 9600
75 #define DEFAULT_CBAUD B9600
78 int mcfrs_console_inited
= 0;
79 int mcfrs_console_port
= -1;
80 int mcfrs_console_baud
= CONSOLE_BAUD_RATE
;
81 int mcfrs_console_cbaud
= DEFAULT_CBAUD
;
84 * Driver data structures.
86 static struct tty_driver
*mcfrs_serial_driver
;
88 /* number of characters left in xmit buffer before we ask for more */
89 #define WAKEUP_CHARS 256
93 #undef SERIAL_DEBUG_OPEN
94 #undef SERIAL_DEBUG_FLOW
96 #if defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x) || \
97 defined(CONFIG_M520x) || defined(CONFIG_M532x)
98 #define IRQBASE (MCFINT_VECBASE+MCFINT_UART0)
104 * Configuration table, UARTs to look for at startup.
106 static struct mcf_serial mcfrs_table
[] = {
109 .addr
= (volatile unsigned char *) (MCF_MBAR
+MCFUART_BASE1
),
111 .flags
= ASYNC_BOOT_AUTOCONF
,
116 .addr
= (volatile unsigned char *) (MCF_MBAR
+MCFUART_BASE2
),
118 .flags
= ASYNC_BOOT_AUTOCONF
,
124 .addr
= (volatile unsigned char *) (MCF_MBAR
+MCFUART_BASE3
),
126 .flags
= ASYNC_BOOT_AUTOCONF
,
132 .addr
= (volatile unsigned char *) (MCF_MBAR
+MCFUART_BASE4
),
134 .flags
= ASYNC_BOOT_AUTOCONF
,
140 #define NR_PORTS (sizeof(mcfrs_table) / sizeof(struct mcf_serial))
143 * This is used to figure out the divisor speeds and the timeouts.
145 static int mcfrs_baud_table
[] = {
146 0, 50, 75, 110, 134, 150, 200, 300, 600, 1200, 1800, 2400, 4800,
147 9600, 19200, 38400, 57600, 115200, 230400, 460800, 0
149 #define MCFRS_BAUD_TABLE_SIZE \
150 (sizeof(mcfrs_baud_table)/sizeof(mcfrs_baud_table[0]))
153 #ifdef CONFIG_MAGIC_SYSRQ
155 * Magic system request keys. Used for debugging...
157 extern int magic_sysrq_key(int ch
);
162 * Forware declarations...
164 static void mcfrs_change_speed(struct mcf_serial
*info
);
165 static void mcfrs_wait_until_sent(struct tty_struct
*tty
, int timeout
);
168 static inline int serial_paranoia_check(struct mcf_serial
*info
,
169 char *name
, const char *routine
)
171 #ifdef SERIAL_PARANOIA_CHECK
172 static const char badmagic
[] =
173 "MCFRS(warning): bad magic number for serial struct %s in %s\n";
174 static const char badinfo
[] =
175 "MCFRS(warning): null mcf_serial for %s in %s\n";
178 printk(badinfo
, name
, routine
);
181 if (info
->magic
!= SERIAL_MAGIC
) {
182 printk(badmagic
, name
, routine
);
190 * Sets or clears DTR and RTS on the requested line.
192 static void mcfrs_setsignals(struct mcf_serial
*info
, int dtr
, int rts
)
194 volatile unsigned char *uartp
;
198 printk("%s(%d): mcfrs_setsignals(info=%x,dtr=%d,rts=%d)\n",
199 __FILE__
, __LINE__
, info
, dtr
, rts
);
202 local_irq_save(flags
);
206 mcf_setppdata(MCFPP_DTR1
, (dtr
? 0 : MCFPP_DTR1
));
208 mcf_setppdata(MCFPP_DTR0
, (dtr
? 0 : MCFPP_DTR0
));
214 info
->sigs
|= TIOCM_RTS
;
215 uartp
[MCFUART_UOP1
] = MCFUART_UOP_RTS
;
217 info
->sigs
&= ~TIOCM_RTS
;
218 uartp
[MCFUART_UOP0
] = MCFUART_UOP_RTS
;
221 local_irq_restore(flags
);
226 * Gets values of serial signals.
228 static int mcfrs_getsignals(struct mcf_serial
*info
)
230 volatile unsigned char *uartp
;
233 #if defined(CONFIG_NETtel) && defined(CONFIG_M5307)
234 unsigned short ppdata
;
238 printk("%s(%d): mcfrs_getsignals(info=%x)\n", __FILE__
, __LINE__
);
241 local_irq_save(flags
);
243 sigs
= (uartp
[MCFUART_UIPR
] & MCFUART_UIPR_CTS
) ? 0 : TIOCM_CTS
;
244 sigs
|= (info
->sigs
& TIOCM_RTS
);
249 ppdata
= mcf_getppdata();
250 if (info
->line
== 0) {
251 sigs
|= (ppdata
& MCFPP_DCD0
) ? 0 : TIOCM_CD
;
252 sigs
|= (ppdata
& MCFPP_DTR0
) ? 0 : TIOCM_DTR
;
253 } else if (info
->line
== 1) {
254 sigs
|= (ppdata
& MCFPP_DCD1
) ? 0 : TIOCM_CD
;
255 sigs
|= (ppdata
& MCFPP_DTR1
) ? 0 : TIOCM_DTR
;
260 local_irq_restore(flags
);
265 * ------------------------------------------------------------
266 * mcfrs_stop() and mcfrs_start()
268 * This routines are called before setting or resetting tty->stopped.
269 * They enable or disable transmitter interrupts, as necessary.
270 * ------------------------------------------------------------
272 static void mcfrs_stop(struct tty_struct
*tty
)
274 volatile unsigned char *uartp
;
275 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
278 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_stop"))
281 local_irq_save(flags
);
283 info
->imr
&= ~MCFUART_UIR_TXREADY
;
284 uartp
[MCFUART_UIMR
] = info
->imr
;
285 local_irq_restore(flags
);
288 static void mcfrs_start(struct tty_struct
*tty
)
290 volatile unsigned char *uartp
;
291 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
294 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_start"))
297 local_irq_save(flags
);
298 if (info
->xmit_cnt
&& info
->xmit_buf
) {
300 info
->imr
|= MCFUART_UIR_TXREADY
;
301 uartp
[MCFUART_UIMR
] = info
->imr
;
303 local_irq_restore(flags
);
307 * ----------------------------------------------------------------------
309 * Here starts the interrupt handling routines. All of the following
310 * subroutines are declared as inline and are folded into
311 * mcfrs_interrupt(). They were separated out for readability's sake.
313 * Note: mcfrs_interrupt() is a "fast" interrupt, which means that it
314 * runs with interrupts turned off. People who may want to modify
315 * mcfrs_interrupt() should try to keep the interrupt handler as fast as
316 * possible. After you are done making modifications, it is not a bad
319 * gcc -S -DKERNEL -Wall -Wstrict-prototypes -O6 -fomit-frame-pointer serial.c
321 * and look at the resulting assemble code in serial.s.
323 * - Ted Ts'o (tytso@mit.edu), 7-Mar-93
324 * -----------------------------------------------------------------------
327 static inline void receive_chars(struct mcf_serial
*info
)
329 volatile unsigned char *uartp
;
330 struct tty_struct
*tty
= info
->tty
;
331 unsigned char status
, ch
, flag
;
338 while ((status
= uartp
[MCFUART_USR
]) & MCFUART_USR_RXREADY
) {
339 ch
= uartp
[MCFUART_URB
];
342 #ifdef CONFIG_MAGIC_SYSRQ
343 if (mcfrs_console_inited
&& (info
->line
== mcfrs_console_port
)) {
344 if (magic_sysrq_key(ch
))
350 if (status
& MCFUART_USR_RXERR
) {
351 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETERR
;
352 if (status
& MCFUART_USR_RXBREAK
) {
353 info
->stats
.rxbreak
++;
355 } else if (status
& MCFUART_USR_RXPARITY
) {
356 info
->stats
.rxparity
++;
358 } else if (status
& MCFUART_USR_RXOVERRUN
) {
359 info
->stats
.rxoverrun
++;
361 } else if (status
& MCFUART_USR_RXFRAMING
) {
362 info
->stats
.rxframing
++;
366 tty_insert_flip_char(tty
, ch
, flag
);
368 tty_schedule_flip(tty
);
372 static inline void transmit_chars(struct mcf_serial
*info
)
374 volatile unsigned char *uartp
;
379 /* Send special char - probably flow control */
380 uartp
[MCFUART_UTB
] = info
->x_char
;
385 if ((info
->xmit_cnt
<= 0) || info
->tty
->stopped
) {
386 info
->imr
&= ~MCFUART_UIR_TXREADY
;
387 uartp
[MCFUART_UIMR
] = info
->imr
;
391 while (uartp
[MCFUART_USR
] & MCFUART_USR_TXREADY
) {
392 uartp
[MCFUART_UTB
] = info
->xmit_buf
[info
->xmit_tail
++];
393 info
->xmit_tail
= info
->xmit_tail
& (SERIAL_XMIT_SIZE
-1);
395 if (--info
->xmit_cnt
<= 0)
399 if (info
->xmit_cnt
< WAKEUP_CHARS
)
400 schedule_work(&info
->tqueue
);
405 * This is the serial driver's generic interrupt routine
407 irqreturn_t
mcfrs_interrupt(int irq
, void *dev_id
)
409 struct mcf_serial
*info
;
412 info
= &mcfrs_table
[(irq
- IRQBASE
)];
413 isr
= info
->addr
[MCFUART_UISR
] & info
->imr
;
415 if (isr
& MCFUART_UIR_RXREADY
)
417 if (isr
& MCFUART_UIR_TXREADY
)
418 transmit_chars(info
);
423 * -------------------------------------------------------------------
424 * Here ends the serial interrupt routines.
425 * -------------------------------------------------------------------
428 static void mcfrs_offintr(struct work_struct
*work
)
430 struct mcf_serial
*info
= container_of(work
, struct mcf_serial
, tqueue
);
431 struct tty_struct
*tty
= info
->tty
;
439 * Change of state on a DCD line.
441 void mcfrs_modem_change(struct mcf_serial
*info
, int dcd
)
443 if (info
->count
== 0)
446 if (info
->flags
& ASYNC_CHECK_CD
) {
448 wake_up_interruptible(&info
->open_wait
);
450 schedule_work(&info
->tqueue_hangup
);
457 unsigned short mcfrs_ppstatus
;
460 * This subroutine is called when the RS_TIMER goes off. It is used
461 * to monitor the state of the DCD lines - since they have no edge
462 * sensors and interrupt generators.
464 static void mcfrs_timer(void)
466 unsigned int ppstatus
, dcdval
, i
;
468 ppstatus
= mcf_getppdata() & (MCFPP_DCD0
| MCFPP_DCD1
);
470 if (ppstatus
!= mcfrs_ppstatus
) {
471 for (i
= 0; (i
< 2); i
++) {
472 dcdval
= (i
? MCFPP_DCD1
: MCFPP_DCD0
);
473 if ((ppstatus
& dcdval
) != (mcfrs_ppstatus
& dcdval
)) {
474 mcfrs_modem_change(&mcfrs_table
[i
],
475 ((ppstatus
& dcdval
) ? 0 : 1));
479 mcfrs_ppstatus
= ppstatus
;
482 mcfrs_timer_struct
.expires
= jiffies
+ HZ
/25;
483 add_timer(&mcfrs_timer_struct
);
486 #endif /* MCFPP_DCD0 */
490 * This routine is called from the scheduler tqueue when the interrupt
491 * routine has signalled that a hangup has occurred. The path of
492 * hangup processing is:
494 * serial interrupt routine -> (scheduler tqueue) ->
495 * do_serial_hangup() -> tty->hangup() -> mcfrs_hangup()
498 static void do_serial_hangup(struct work_struct
*work
)
500 struct mcf_serial
*info
= container_of(work
, struct mcf_serial
, tqueue_hangup
);
501 struct tty_struct
*tty
= info
->tty
;
507 static int startup(struct mcf_serial
* info
)
509 volatile unsigned char *uartp
;
512 if (info
->flags
& ASYNC_INITIALIZED
)
515 if (!info
->xmit_buf
) {
516 info
->xmit_buf
= (unsigned char *) __get_free_page(GFP_KERNEL
);
521 local_irq_save(flags
);
523 #ifdef SERIAL_DEBUG_OPEN
524 printk("starting up ttyS%d (irq %d)...\n", info
->line
, info
->irq
);
528 * Reset UART, get it into known state...
531 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETRX
; /* reset RX */
532 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETTX
; /* reset TX */
533 mcfrs_setsignals(info
, 1, 1);
536 clear_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
537 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
540 * and set the speed of the serial port
542 mcfrs_change_speed(info
);
545 * Lastly enable the UART transmitter and receiver, and
548 info
->imr
= MCFUART_UIR_RXREADY
;
549 uartp
[MCFUART_UCR
] = MCFUART_UCR_RXENABLE
| MCFUART_UCR_TXENABLE
;
550 uartp
[MCFUART_UIMR
] = info
->imr
;
552 info
->flags
|= ASYNC_INITIALIZED
;
553 local_irq_restore(flags
);
558 * This routine will shutdown a serial port; interrupts are disabled, and
559 * DTR is dropped if the hangup on close termio flag is on.
561 static void shutdown(struct mcf_serial
* info
)
563 volatile unsigned char *uartp
;
566 if (!(info
->flags
& ASYNC_INITIALIZED
))
569 #ifdef SERIAL_DEBUG_OPEN
570 printk("Shutting down serial port %d (irq %d)....\n", info
->line
,
574 local_irq_save(flags
);
577 uartp
[MCFUART_UIMR
] = 0; /* mask all interrupts */
578 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETRX
; /* reset RX */
579 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETTX
; /* reset TX */
581 if (!info
->tty
|| (info
->tty
->termios
->c_cflag
& HUPCL
))
582 mcfrs_setsignals(info
, 0, 0);
584 if (info
->xmit_buf
) {
585 free_page((unsigned long) info
->xmit_buf
);
590 set_bit(TTY_IO_ERROR
, &info
->tty
->flags
);
592 info
->flags
&= ~ASYNC_INITIALIZED
;
593 local_irq_restore(flags
);
598 * This routine is called to set the UART divisor registers to match
599 * the specified baud rate for a serial port.
601 static void mcfrs_change_speed(struct mcf_serial
*info
)
603 volatile unsigned char *uartp
;
604 unsigned int baudclk
, cflag
;
606 unsigned char mr1
, mr2
;
609 unsigned int fraction
;
612 if (!info
->tty
|| !info
->tty
->termios
)
614 cflag
= info
->tty
->termios
->c_cflag
;
619 printk("%s(%d): mcfrs_change_speed()\n", __FILE__
, __LINE__
);
626 info
->tty
->termios
->c_cflag
&= ~CBAUDEX
;
631 mcfrs_setsignals(info
, 0, -1);
635 /* compute the baudrate clock */
638 * For the MCF5272, also compute the baudrate fraction.
640 baudclk
= (MCF_BUSCLK
/ mcfrs_baud_table
[i
]) / 32;
641 fraction
= MCF_BUSCLK
- (baudclk
* 32 * mcfrs_baud_table
[i
]);
643 fraction
/= (32 * mcfrs_baud_table
[i
]);
645 baudclk
= ((MCF_BUSCLK
/ mcfrs_baud_table
[i
]) + 16) / 32;
648 info
->baud
= mcfrs_baud_table
[i
];
650 mr1
= MCFUART_MR1_RXIRQRDY
| MCFUART_MR1_RXERRCHAR
;
653 switch (cflag
& CSIZE
) {
654 case CS5
: mr1
|= MCFUART_MR1_CS5
; break;
655 case CS6
: mr1
|= MCFUART_MR1_CS6
; break;
656 case CS7
: mr1
|= MCFUART_MR1_CS7
; break;
658 default: mr1
|= MCFUART_MR1_CS8
; break;
661 if (cflag
& PARENB
) {
662 if (cflag
& CMSPAR
) {
664 mr1
|= MCFUART_MR1_PARITYMARK
;
666 mr1
|= MCFUART_MR1_PARITYSPACE
;
669 mr1
|= MCFUART_MR1_PARITYODD
;
671 mr1
|= MCFUART_MR1_PARITYEVEN
;
674 mr1
|= MCFUART_MR1_PARITYNONE
;
678 mr2
|= MCFUART_MR2_STOP2
;
680 mr2
|= MCFUART_MR2_STOP1
;
682 if (cflag
& CRTSCTS
) {
683 mr1
|= MCFUART_MR1_RXRTS
;
684 mr2
|= MCFUART_MR2_TXCTS
;
688 info
->flags
&= ~ASYNC_CHECK_CD
;
690 info
->flags
|= ASYNC_CHECK_CD
;
694 local_irq_save(flags
);
696 printk("%s(%d): mr1=%x mr2=%x baudclk=%x\n", __FILE__
, __LINE__
,
700 Note: pg 12-16 of MCF5206e User's Manual states that a
701 software reset should be performed prior to changing
702 UMR1,2, UCSR, UACR, bit 7
704 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETRX
; /* reset RX */
705 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETTX
; /* reset TX */
706 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETMRPTR
; /* reset MR pointer */
707 uartp
[MCFUART_UMR
] = mr1
;
708 uartp
[MCFUART_UMR
] = mr2
;
709 uartp
[MCFUART_UBG1
] = (baudclk
& 0xff00) >> 8; /* set msb byte */
710 uartp
[MCFUART_UBG2
] = (baudclk
& 0xff); /* set lsb byte */
712 uartp
[MCFUART_UFPD
] = (fraction
& 0xf); /* set fraction */
714 uartp
[MCFUART_UCSR
] = MCFUART_UCSR_RXCLKTIMER
| MCFUART_UCSR_TXCLKTIMER
;
715 uartp
[MCFUART_UCR
] = MCFUART_UCR_RXENABLE
| MCFUART_UCR_TXENABLE
;
716 mcfrs_setsignals(info
, 1, -1);
717 local_irq_restore(flags
);
721 static void mcfrs_flush_chars(struct tty_struct
*tty
)
723 volatile unsigned char *uartp
;
724 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
727 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_flush_chars"))
730 uartp
= (volatile unsigned char *) info
->addr
;
733 * re-enable receiver interrupt
735 local_irq_save(flags
);
736 if ((!(info
->imr
& MCFUART_UIR_RXREADY
)) &&
737 (info
->flags
& ASYNC_INITIALIZED
) ) {
738 info
->imr
|= MCFUART_UIR_RXREADY
;
739 uartp
[MCFUART_UIMR
] = info
->imr
;
741 local_irq_restore(flags
);
743 if (info
->xmit_cnt
<= 0 || tty
->stopped
|| tty
->hw_stopped
||
747 /* Enable transmitter */
748 local_irq_save(flags
);
749 info
->imr
|= MCFUART_UIR_TXREADY
;
750 uartp
[MCFUART_UIMR
] = info
->imr
;
751 local_irq_restore(flags
);
754 static int mcfrs_write(struct tty_struct
* tty
,
755 const unsigned char *buf
, int count
)
757 volatile unsigned char *uartp
;
758 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
763 printk("%s(%d): mcfrs_write(tty=%x,buf=%x,count=%d)\n",
764 __FILE__
, __LINE__
, (int)tty
, (int)buf
, count
);
767 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_write"))
770 if (!tty
|| !info
->xmit_buf
)
773 local_save_flags(flags
);
776 c
= min(count
, (int) min(((int)SERIAL_XMIT_SIZE
) - info
->xmit_cnt
- 1,
777 ((int)SERIAL_XMIT_SIZE
) - info
->xmit_head
));
778 local_irq_restore(flags
);
783 memcpy(info
->xmit_buf
+ info
->xmit_head
, buf
, c
);
786 info
->xmit_head
= (info
->xmit_head
+ c
) & (SERIAL_XMIT_SIZE
-1);
788 local_irq_restore(flags
);
797 info
->imr
|= MCFUART_UIR_TXREADY
;
798 uartp
[MCFUART_UIMR
] = info
->imr
;
799 local_irq_restore(flags
);
804 static int mcfrs_write_room(struct tty_struct
*tty
)
806 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
809 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_write_room"))
811 ret
= SERIAL_XMIT_SIZE
- info
->xmit_cnt
- 1;
817 static int mcfrs_chars_in_buffer(struct tty_struct
*tty
)
819 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
821 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_chars_in_buffer"))
823 return info
->xmit_cnt
;
826 static void mcfrs_flush_buffer(struct tty_struct
*tty
)
828 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
831 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_flush_buffer"))
834 local_irq_save(flags
);
835 info
->xmit_cnt
= info
->xmit_head
= info
->xmit_tail
= 0;
836 local_irq_restore(flags
);
842 * ------------------------------------------------------------
845 * This routine is called by the upper-layer tty layer to signal that
846 * incoming characters should be throttled.
847 * ------------------------------------------------------------
849 static void mcfrs_throttle(struct tty_struct
* tty
)
851 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
852 #ifdef SERIAL_DEBUG_THROTTLE
855 printk("throttle %s: %d....\n", tty_name(tty
, buf
),
856 tty
->ldisc
.chars_in_buffer(tty
));
859 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_throttle"))
863 info
->x_char
= STOP_CHAR(tty
);
865 /* Turn off RTS line (do this atomic) */
868 static void mcfrs_unthrottle(struct tty_struct
* tty
)
870 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
871 #ifdef SERIAL_DEBUG_THROTTLE
874 printk("unthrottle %s: %d....\n", tty_name(tty
, buf
),
875 tty
->ldisc
.chars_in_buffer(tty
));
878 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_unthrottle"))
885 info
->x_char
= START_CHAR(tty
);
888 /* Assert RTS line (do this atomic) */
892 * ------------------------------------------------------------
893 * mcfrs_ioctl() and friends
894 * ------------------------------------------------------------
897 static int get_serial_info(struct mcf_serial
* info
,
898 struct serial_struct
* retinfo
)
900 struct serial_struct tmp
;
904 memset(&tmp
, 0, sizeof(tmp
));
905 tmp
.type
= info
->type
;
906 tmp
.line
= info
->line
;
907 tmp
.port
= (unsigned int) info
->addr
;
909 tmp
.flags
= info
->flags
;
910 tmp
.baud_base
= info
->baud_base
;
911 tmp
.close_delay
= info
->close_delay
;
912 tmp
.closing_wait
= info
->closing_wait
;
913 tmp
.custom_divisor
= info
->custom_divisor
;
914 return copy_to_user(retinfo
,&tmp
,sizeof(*retinfo
)) ? -EFAULT
: 0;
917 static int set_serial_info(struct mcf_serial
* info
,
918 struct serial_struct
* new_info
)
920 struct serial_struct new_serial
;
921 struct mcf_serial old_info
;
926 if (copy_from_user(&new_serial
,new_info
,sizeof(new_serial
)))
930 if (!capable(CAP_SYS_ADMIN
)) {
931 if ((new_serial
.baud_base
!= info
->baud_base
) ||
932 (new_serial
.type
!= info
->type
) ||
933 (new_serial
.close_delay
!= info
->close_delay
) ||
934 ((new_serial
.flags
& ~ASYNC_USR_MASK
) !=
935 (info
->flags
& ~ASYNC_USR_MASK
)))
937 info
->flags
= ((info
->flags
& ~ASYNC_USR_MASK
) |
938 (new_serial
.flags
& ASYNC_USR_MASK
));
939 info
->custom_divisor
= new_serial
.custom_divisor
;
947 * OK, past this point, all the error checking has been done.
948 * At this point, we start making changes.....
951 info
->baud_base
= new_serial
.baud_base
;
952 info
->flags
= ((info
->flags
& ~ASYNC_FLAGS
) |
953 (new_serial
.flags
& ASYNC_FLAGS
));
954 info
->type
= new_serial
.type
;
955 info
->close_delay
= new_serial
.close_delay
;
956 info
->closing_wait
= new_serial
.closing_wait
;
959 retval
= startup(info
);
964 * get_lsr_info - get line status register info
966 * Purpose: Let user call ioctl() to get info when the UART physically
967 * is emptied. On bus types like RS485, the transmitter must
968 * release the bus after transmitting. This must be done when
969 * the transmit shift register is empty, not be done when the
970 * transmit holding register is empty. This functionality
971 * allows an RS485 driver to be written in user space.
973 static int get_lsr_info(struct mcf_serial
* info
, unsigned int *value
)
975 volatile unsigned char *uartp
;
977 unsigned char status
;
979 local_irq_save(flags
);
981 status
= (uartp
[MCFUART_USR
] & MCFUART_USR_TXEMPTY
) ? TIOCSER_TEMT
: 0;
982 local_irq_restore(flags
);
984 return put_user(status
,value
);
988 * This routine sends a break character out the serial port.
990 static void send_break( struct mcf_serial
* info
, int duration
)
992 volatile unsigned char *uartp
;
997 set_current_state(TASK_INTERRUPTIBLE
);
1000 local_irq_save(flags
);
1001 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDBREAKSTART
;
1002 schedule_timeout(duration
);
1003 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDBREAKSTOP
;
1004 local_irq_restore(flags
);
1007 static int mcfrs_tiocmget(struct tty_struct
*tty
, struct file
*file
)
1009 struct mcf_serial
* info
= (struct mcf_serial
*)tty
->driver_data
;
1011 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_ioctl"))
1013 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1016 return mcfrs_getsignals(info
);
1019 static int mcfrs_tiocmset(struct tty_struct
*tty
, struct file
*file
,
1020 unsigned int set
, unsigned int clear
)
1022 struct mcf_serial
* info
= (struct mcf_serial
*)tty
->driver_data
;
1023 int rts
= -1, dtr
= -1;
1025 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_ioctl"))
1027 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1030 if (set
& TIOCM_RTS
)
1032 if (set
& TIOCM_DTR
)
1034 if (clear
& TIOCM_RTS
)
1036 if (clear
& TIOCM_DTR
)
1039 mcfrs_setsignals(info
, dtr
, rts
);
1044 static int mcfrs_ioctl(struct tty_struct
*tty
, struct file
* file
,
1045 unsigned int cmd
, unsigned long arg
)
1047 struct mcf_serial
* info
= (struct mcf_serial
*)tty
->driver_data
;
1050 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_ioctl"))
1053 if ((cmd
!= TIOCGSERIAL
) && (cmd
!= TIOCSSERIAL
) &&
1054 (cmd
!= TIOCSERCONFIG
) && (cmd
!= TIOCSERGWILD
) &&
1055 (cmd
!= TIOCSERSWILD
) && (cmd
!= TIOCSERGSTRUCT
)) {
1056 if (tty
->flags
& (1 << TTY_IO_ERROR
))
1061 case TCSBRK
: /* SVID version: non-zero arg --> no break */
1062 retval
= tty_check_change(tty
);
1065 tty_wait_until_sent(tty
, 0);
1067 send_break(info
, HZ
/4); /* 1/4 second */
1069 case TCSBRKP
: /* support for POSIX tcsendbreak() */
1070 retval
= tty_check_change(tty
);
1073 tty_wait_until_sent(tty
, 0);
1074 send_break(info
, arg
? arg
*(HZ
/10) : HZ
/4);
1077 error
= put_user(C_CLOCAL(tty
) ? 1 : 0,
1078 (unsigned long *) arg
);
1083 get_user(arg
, (unsigned long *) arg
);
1084 tty
->termios
->c_cflag
=
1085 ((tty
->termios
->c_cflag
& ~CLOCAL
) |
1086 (arg
? CLOCAL
: 0));
1089 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1090 sizeof(struct serial_struct
)))
1091 return get_serial_info(info
,
1092 (struct serial_struct
*) arg
);
1095 return set_serial_info(info
,
1096 (struct serial_struct
*) arg
);
1097 case TIOCSERGETLSR
: /* Get line status register */
1098 if (access_ok(VERIFY_WRITE
, (void *) arg
,
1099 sizeof(unsigned int)))
1100 return get_lsr_info(info
, (unsigned int *) arg
);
1102 case TIOCSERGSTRUCT
:
1103 error
= copy_to_user((struct mcf_serial
*) arg
,
1104 info
, sizeof(struct mcf_serial
));
1112 get_user(val
, (unsigned int *) arg
);
1113 mcf_setpa(MCFPP_PA11
, (val
? 0 : MCFPP_PA11
));
1118 val
= (mcf_getpa() & MCFPP_PA11
) ? 0 : 1;
1119 put_user(val
, (unsigned int *) arg
);
1125 return -ENOIOCTLCMD
;
1130 static void mcfrs_set_termios(struct tty_struct
*tty
, struct ktermios
*old_termios
)
1132 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
1134 if (tty
->termios
->c_cflag
== old_termios
->c_cflag
)
1137 mcfrs_change_speed(info
);
1139 if ((old_termios
->c_cflag
& CRTSCTS
) &&
1140 !(tty
->termios
->c_cflag
& CRTSCTS
)) {
1141 tty
->hw_stopped
= 0;
1142 mcfrs_setsignals(info
, -1, 1);
1150 * ------------------------------------------------------------
1153 * This routine is called when the serial port gets closed. First, we
1154 * wait for the last remaining data to be sent. Then, we unlink its
1155 * S structure from the interrupt chain if necessary, and we free
1156 * that IRQ if nothing is left in the chain.
1157 * ------------------------------------------------------------
1159 static void mcfrs_close(struct tty_struct
*tty
, struct file
* filp
)
1161 volatile unsigned char *uartp
;
1162 struct mcf_serial
*info
= (struct mcf_serial
*)tty
->driver_data
;
1163 unsigned long flags
;
1165 if (!info
|| serial_paranoia_check(info
, tty
->name
, "mcfrs_close"))
1168 local_irq_save(flags
);
1170 if (tty_hung_up_p(filp
)) {
1171 local_irq_restore(flags
);
1175 #ifdef SERIAL_DEBUG_OPEN
1176 printk("mcfrs_close ttyS%d, count = %d\n", info
->line
, info
->count
);
1178 if ((tty
->count
== 1) && (info
->count
!= 1)) {
1180 * Uh, oh. tty->count is 1, which means that the tty
1181 * structure will be freed. Info->count should always
1182 * be one in these conditions. If it's greater than
1183 * one, we've got real problems, since it means the
1184 * serial port won't be shutdown.
1186 printk("MCFRS: bad serial port count; tty->count is 1, "
1187 "info->count is %d\n", info
->count
);
1190 if (--info
->count
< 0) {
1191 printk("MCFRS: bad serial port count for ttyS%d: %d\n",
1192 info
->line
, info
->count
);
1196 local_irq_restore(flags
);
1199 info
->flags
|= ASYNC_CLOSING
;
1202 * Now we wait for the transmit buffer to clear; and we notify
1203 * the line discipline to only process XON/XOFF characters.
1206 if (info
->closing_wait
!= ASYNC_CLOSING_WAIT_NONE
)
1207 tty_wait_until_sent(tty
, info
->closing_wait
);
1210 * At this point we stop accepting input. To do this, we
1211 * disable the receive line status interrupts, and tell the
1212 * interrupt driver to stop checking the data ready bit in the
1213 * line status register.
1215 info
->imr
&= ~MCFUART_UIR_RXREADY
;
1217 uartp
[MCFUART_UIMR
] = info
->imr
;
1220 /* FIXME: do we need to keep this enabled for console?? */
1221 if (mcfrs_console_inited
&& (mcfrs_console_port
== info
->line
)) {
1222 /* Do not disable the UART */ ;
1226 if (tty
->driver
->flush_buffer
)
1227 tty
->driver
->flush_buffer(tty
);
1228 tty_ldisc_flush(tty
);
1234 if (tty
->ldisc
.num
!= ldiscs
[N_TTY
].num
) {
1235 if (tty
->ldisc
.close
)
1236 (tty
->ldisc
.close
)(tty
);
1237 tty
->ldisc
= ldiscs
[N_TTY
];
1238 tty
->termios
->c_line
= N_TTY
;
1239 if (tty
->ldisc
.open
)
1240 (tty
->ldisc
.open
)(tty
);
1243 if (info
->blocked_open
) {
1244 if (info
->close_delay
) {
1245 msleep_interruptible(jiffies_to_msecs(info
->close_delay
));
1247 wake_up_interruptible(&info
->open_wait
);
1249 info
->flags
&= ~(ASYNC_NORMAL_ACTIVE
|ASYNC_CLOSING
);
1250 wake_up_interruptible(&info
->close_wait
);
1251 local_irq_restore(flags
);
1255 * mcfrs_wait_until_sent() --- wait until the transmitter is empty
1258 mcfrs_wait_until_sent(struct tty_struct
*tty
, int timeout
)
1261 #define MCF5272_FIFO_SIZE 25 /* fifo size + shift reg */
1263 struct mcf_serial
* info
= (struct mcf_serial
*)tty
->driver_data
;
1264 volatile unsigned char *uartp
;
1265 unsigned long orig_jiffies
, fifo_time
, char_time
, fifo_cnt
;
1267 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_wait_until_sent"))
1270 orig_jiffies
= jiffies
;
1273 * Set the check interval to be 1/5 of the approximate time
1274 * to send the entire fifo, and make it at least 1. The check
1275 * interval should also be less than the timeout.
1277 * Note: we have to use pretty tight timings here to satisfy
1280 fifo_time
= (MCF5272_FIFO_SIZE
* HZ
* 10) / info
->baud
;
1281 char_time
= fifo_time
/ 5;
1284 if (timeout
&& timeout
< char_time
)
1285 char_time
= timeout
;
1288 * Clamp the timeout period at 2 * the time to empty the
1289 * fifo. Just to be safe, set the minimum at .5 seconds.
1292 if (fifo_time
< (HZ
/2))
1294 if (!timeout
|| timeout
> fifo_time
)
1295 timeout
= fifo_time
;
1298 * Account for the number of bytes in the UART
1299 * transmitter FIFO plus any byte being shifted out.
1301 uartp
= (volatile unsigned char *) info
->addr
;
1303 fifo_cnt
= (uartp
[MCFUART_UTF
] & MCFUART_UTF_TXB
);
1304 if ((uartp
[MCFUART_USR
] & (MCFUART_USR_TXREADY
|
1305 MCFUART_USR_TXEMPTY
)) ==
1306 MCFUART_USR_TXREADY
)
1310 msleep_interruptible(jiffies_to_msecs(char_time
));
1311 if (signal_pending(current
))
1313 if (timeout
&& time_after(jiffies
, orig_jiffies
+ timeout
))
1318 * For the other coldfire models, assume all data has been sent
1324 * mcfrs_hangup() --- called by tty_hangup() when a hangup is signaled.
1326 void mcfrs_hangup(struct tty_struct
*tty
)
1328 struct mcf_serial
* info
= (struct mcf_serial
*)tty
->driver_data
;
1330 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_hangup"))
1333 mcfrs_flush_buffer(tty
);
1337 info
->flags
&= ~ASYNC_NORMAL_ACTIVE
;
1339 wake_up_interruptible(&info
->open_wait
);
1343 * ------------------------------------------------------------
1344 * mcfrs_open() and friends
1345 * ------------------------------------------------------------
1347 static int block_til_ready(struct tty_struct
*tty
, struct file
* filp
,
1348 struct mcf_serial
*info
)
1350 DECLARE_WAITQUEUE(wait
, current
);
1355 * If the device is in the middle of being closed, then block
1356 * until it's done, and then try again.
1358 if (info
->flags
& ASYNC_CLOSING
) {
1359 interruptible_sleep_on(&info
->close_wait
);
1360 #ifdef SERIAL_DO_RESTART
1361 if (info
->flags
& ASYNC_HUP_NOTIFY
)
1364 return -ERESTARTSYS
;
1371 * If non-blocking mode is set, or the port is not enabled,
1372 * then make the check up front and then exit.
1374 if ((filp
->f_flags
& O_NONBLOCK
) ||
1375 (tty
->flags
& (1 << TTY_IO_ERROR
))) {
1376 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1380 if (tty
->termios
->c_cflag
& CLOCAL
)
1384 * Block waiting for the carrier detect and the line to become
1385 * free (i.e., not in use by the callout). While we are in
1386 * this loop, info->count is dropped by one, so that
1387 * mcfrs_close() knows when to free things. We restore it upon
1388 * exit, either normal or abnormal.
1391 add_wait_queue(&info
->open_wait
, &wait
);
1392 #ifdef SERIAL_DEBUG_OPEN
1393 printk("block_til_ready before block: ttyS%d, count = %d\n",
1394 info
->line
, info
->count
);
1397 info
->blocked_open
++;
1399 local_irq_disable();
1400 mcfrs_setsignals(info
, 1, 1);
1402 current
->state
= TASK_INTERRUPTIBLE
;
1403 if (tty_hung_up_p(filp
) ||
1404 !(info
->flags
& ASYNC_INITIALIZED
)) {
1405 #ifdef SERIAL_DO_RESTART
1406 if (info
->flags
& ASYNC_HUP_NOTIFY
)
1409 retval
= -ERESTARTSYS
;
1415 if (!(info
->flags
& ASYNC_CLOSING
) &&
1416 (do_clocal
|| (mcfrs_getsignals(info
) & TIOCM_CD
)))
1418 if (signal_pending(current
)) {
1419 retval
= -ERESTARTSYS
;
1422 #ifdef SERIAL_DEBUG_OPEN
1423 printk("block_til_ready blocking: ttyS%d, count = %d\n",
1424 info
->line
, info
->count
);
1428 current
->state
= TASK_RUNNING
;
1429 remove_wait_queue(&info
->open_wait
, &wait
);
1430 if (!tty_hung_up_p(filp
))
1432 info
->blocked_open
--;
1433 #ifdef SERIAL_DEBUG_OPEN
1434 printk("block_til_ready after blocking: ttyS%d, count = %d\n",
1435 info
->line
, info
->count
);
1439 info
->flags
|= ASYNC_NORMAL_ACTIVE
;
1444 * This routine is called whenever a serial port is opened. It
1445 * enables interrupts for a serial port, linking in its structure into
1446 * the IRQ chain. It also performs the serial-specific
1447 * initialization for the tty structure.
1449 int mcfrs_open(struct tty_struct
*tty
, struct file
* filp
)
1451 struct mcf_serial
*info
;
1455 if ((line
< 0) || (line
>= NR_PORTS
))
1457 info
= mcfrs_table
+ line
;
1458 if (serial_paranoia_check(info
, tty
->name
, "mcfrs_open"))
1460 #ifdef SERIAL_DEBUG_OPEN
1461 printk("mcfrs_open %s, count = %d\n", tty
->name
, info
->count
);
1464 tty
->driver_data
= info
;
1468 * Start up serial port
1470 retval
= startup(info
);
1474 retval
= block_til_ready(tty
, filp
, info
);
1476 #ifdef SERIAL_DEBUG_OPEN
1477 printk("mcfrs_open returning after block_til_ready with %d\n",
1483 #ifdef SERIAL_DEBUG_OPEN
1484 printk("mcfrs_open %s successful...\n", tty
->name
);
1490 * Based on the line number set up the internal interrupt stuff.
1492 static void mcfrs_irqinit(struct mcf_serial
*info
)
1494 #if defined(CONFIG_M5272)
1495 volatile unsigned long *icrp
;
1496 volatile unsigned long *portp
;
1497 volatile unsigned char *uartp
;
1500 icrp
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_ICR2
);
1502 switch (info
->line
) {
1510 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1515 /* Enable the output lines for the serial ports */
1516 portp
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_PBCNT
);
1517 *portp
= (*portp
& ~0x000000ff) | 0x00000055;
1518 portp
= (volatile unsigned long *) (MCF_MBAR
+ MCFSIM_PDCNT
);
1519 *portp
= (*portp
& ~0x000003fc) | 0x000002a8;
1520 #elif defined(CONFIG_M523x) || defined(CONFIG_M527x) || defined(CONFIG_M528x)
1521 volatile unsigned char *icrp
, *uartp
;
1522 volatile unsigned long *imrp
;
1526 icrp
= (volatile unsigned char *) (MCF_MBAR
+ MCFICM_INTC0
+
1527 MCFINTC_ICR0
+ MCFINT_UART0
+ info
->line
);
1528 *icrp
= 0x30 + info
->line
; /* level 6, line based priority */
1530 imrp
= (volatile unsigned long *) (MCF_MBAR
+ MCFICM_INTC0
+
1532 *imrp
&= ~((1 << (info
->irq
- MCFINT_VECBASE
)) | 1);
1533 #if defined(CONFIG_M527x)
1536 * External Pin Mask Setting & Enable External Pin for Interface
1537 * mrcbis@aliceposta.it
1539 u16
*serpin_enable_mask
;
1540 serpin_enable_mask
= (u16
*) (MCF_IPSBAR
+ MCF_GPIO_PAR_UART
);
1541 if (info
->line
== 0)
1542 *serpin_enable_mask
|= UART0_ENABLE_MASK
;
1543 else if (info
->line
== 1)
1544 *serpin_enable_mask
|= UART1_ENABLE_MASK
;
1545 else if (info
->line
== 2)
1546 *serpin_enable_mask
|= UART2_ENABLE_MASK
;
1549 #if defined(CONFIG_M528x)
1550 /* make sure PUAPAR is set for UART0 and UART1 */
1551 if (info
->line
< 2) {
1552 volatile unsigned char *portp
= (volatile unsigned char *) (MCF_MBAR
+ MCF5282_GPIO_PUAPAR
);
1553 *portp
|= (0x03 << (info
->line
* 2));
1556 #elif defined(CONFIG_M520x)
1557 volatile unsigned char *icrp
, *uartp
;
1558 volatile unsigned long *imrp
;
1562 icrp
= (volatile unsigned char *) (MCF_MBAR
+ MCFICM_INTC0
+
1563 MCFINTC_ICR0
+ MCFINT_UART0
+ info
->line
);
1566 imrp
= (volatile unsigned long *) (MCF_MBAR
+ MCFICM_INTC0
+
1568 *imrp
&= ~((1 << (info
->irq
- MCFINT_VECBASE
)) | 1);
1569 if (info
->line
< 2) {
1570 unsigned short *uart_par
;
1571 uart_par
= (unsigned short *)(MCF_IPSBAR
+ MCF_GPIO_PAR_UART
);
1572 if (info
->line
== 0)
1573 *uart_par
|= MCF_GPIO_PAR_UART_PAR_UTXD0
1574 | MCF_GPIO_PAR_UART_PAR_URXD0
;
1575 else if (info
->line
== 1)
1576 *uart_par
|= MCF_GPIO_PAR_UART_PAR_UTXD1
1577 | MCF_GPIO_PAR_UART_PAR_URXD1
;
1578 } else if (info
->line
== 2) {
1579 unsigned char *feci2c_par
;
1580 feci2c_par
= (unsigned char *)(MCF_IPSBAR
+ MCF_GPIO_PAR_FECI2C
);
1581 *feci2c_par
&= ~0x0F;
1582 *feci2c_par
|= MCF_GPIO_PAR_FECI2C_PAR_SCL_UTXD2
1583 | MCF_GPIO_PAR_FECI2C_PAR_SDA_URXD2
;
1585 #elif defined(CONFIG_M532x)
1586 volatile unsigned char *uartp
;
1588 switch (info
->line
) {
1590 MCF_INTC0_ICR26
= 0x3;
1591 MCF_INTC0_CIMR
= 26;
1592 /* GPIO initialization */
1593 MCF_GPIO_PAR_UART
|= 0x000F;
1596 MCF_INTC0_ICR27
= 0x3;
1597 MCF_INTC0_CIMR
= 27;
1598 /* GPIO initialization */
1599 MCF_GPIO_PAR_UART
|= 0x0FF0;
1602 MCF_INTC0_ICR28
= 0x3;
1603 MCF_INTC0_CIMR
= 28;
1604 /* GPIOs also must be initalized, depends on board */
1608 volatile unsigned char *icrp
, *uartp
;
1610 switch (info
->line
) {
1612 icrp
= (volatile unsigned char *) (MCF_MBAR
+ MCFSIM_UART1ICR
);
1613 *icrp
= /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6
|
1615 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART1
);
1618 icrp
= (volatile unsigned char *) (MCF_MBAR
+ MCFSIM_UART2ICR
);
1619 *icrp
= /*MCFSIM_ICR_AUTOVEC |*/ MCFSIM_ICR_LEVEL6
|
1621 mcf_setimr(mcf_getimr() & ~MCFSIM_IMR_UART2
);
1624 printk("MCFRS: don't know how to handle UART %d interrupt?\n",
1630 uartp
[MCFUART_UIVR
] = info
->irq
;
1633 /* Clear mask, so no surprise interrupts. */
1634 uartp
[MCFUART_UIMR
] = 0;
1636 if (request_irq(info
->irq
, mcfrs_interrupt
, IRQF_DISABLED
,
1637 "ColdFire UART", NULL
)) {
1638 printk("MCFRS: Unable to attach ColdFire UART %d interrupt "
1639 "vector=%d\n", info
->line
, info
->irq
);
1646 char *mcfrs_drivername
= "ColdFire internal UART serial driver version 1.00\n";
1650 * Serial stats reporting...
1652 int mcfrs_readproc(char *page
, char **start
, off_t off
, int count
,
1653 int *eof
, void *data
)
1655 struct mcf_serial
*info
;
1659 len
= sprintf(page
, mcfrs_drivername
);
1660 for (i
= 0; (i
< NR_PORTS
); i
++) {
1661 info
= &mcfrs_table
[i
];
1662 len
+= sprintf((page
+ len
), "%d: port:%x irq=%d baud:%d ",
1663 i
, (unsigned int) info
->addr
, info
->irq
, info
->baud
);
1664 if (info
->stats
.rx
|| info
->stats
.tx
)
1665 len
+= sprintf((page
+ len
), "tx:%d rx:%d ",
1666 info
->stats
.tx
, info
->stats
.rx
);
1667 if (info
->stats
.rxframing
)
1668 len
+= sprintf((page
+ len
), "fe:%d ",
1669 info
->stats
.rxframing
);
1670 if (info
->stats
.rxparity
)
1671 len
+= sprintf((page
+ len
), "pe:%d ",
1672 info
->stats
.rxparity
);
1673 if (info
->stats
.rxbreak
)
1674 len
+= sprintf((page
+ len
), "brk:%d ",
1675 info
->stats
.rxbreak
);
1676 if (info
->stats
.rxoverrun
)
1677 len
+= sprintf((page
+ len
), "oe:%d ",
1678 info
->stats
.rxoverrun
);
1680 str
[0] = str
[1] = 0;
1681 if ((sigs
= mcfrs_getsignals(info
))) {
1682 if (sigs
& TIOCM_RTS
)
1683 strcat(str
, "|RTS");
1684 if (sigs
& TIOCM_CTS
)
1685 strcat(str
, "|CTS");
1686 if (sigs
& TIOCM_DTR
)
1687 strcat(str
, "|DTR");
1688 if (sigs
& TIOCM_CD
)
1692 len
+= sprintf((page
+ len
), "%s\n", &str
[1]);
1699 /* Finally, routines used to initialize the serial driver. */
1701 static void show_serial_version(void)
1703 printk(mcfrs_drivername
);
1706 static const struct tty_operations mcfrs_ops
= {
1708 .close
= mcfrs_close
,
1709 .write
= mcfrs_write
,
1710 .flush_chars
= mcfrs_flush_chars
,
1711 .write_room
= mcfrs_write_room
,
1712 .chars_in_buffer
= mcfrs_chars_in_buffer
,
1713 .flush_buffer
= mcfrs_flush_buffer
,
1714 .ioctl
= mcfrs_ioctl
,
1715 .throttle
= mcfrs_throttle
,
1716 .unthrottle
= mcfrs_unthrottle
,
1717 .set_termios
= mcfrs_set_termios
,
1719 .start
= mcfrs_start
,
1720 .hangup
= mcfrs_hangup
,
1721 .read_proc
= mcfrs_readproc
,
1722 .wait_until_sent
= mcfrs_wait_until_sent
,
1723 .tiocmget
= mcfrs_tiocmget
,
1724 .tiocmset
= mcfrs_tiocmset
,
1727 /* mcfrs_init inits the driver */
1731 struct mcf_serial
*info
;
1732 unsigned long flags
;
1735 /* Setup base handler, and timer table. */
1737 init_timer(&mcfrs_timer_struct
);
1738 mcfrs_timer_struct
.function
= mcfrs_timer
;
1739 mcfrs_timer_struct
.data
= 0;
1740 mcfrs_timer_struct
.expires
= jiffies
+ HZ
/25;
1741 add_timer(&mcfrs_timer_struct
);
1742 mcfrs_ppstatus
= mcf_getppdata() & (MCFPP_DCD0
| MCFPP_DCD1
);
1744 mcfrs_serial_driver
= alloc_tty_driver(NR_PORTS
);
1745 if (!mcfrs_serial_driver
)
1748 show_serial_version();
1750 /* Initialize the tty_driver structure */
1751 mcfrs_serial_driver
->owner
= THIS_MODULE
;
1752 mcfrs_serial_driver
->name
= "ttyS";
1753 mcfrs_serial_driver
->driver_name
= "mcfserial";
1754 mcfrs_serial_driver
->major
= TTY_MAJOR
;
1755 mcfrs_serial_driver
->minor_start
= 64;
1756 mcfrs_serial_driver
->type
= TTY_DRIVER_TYPE_SERIAL
;
1757 mcfrs_serial_driver
->subtype
= SERIAL_TYPE_NORMAL
;
1758 mcfrs_serial_driver
->init_termios
= tty_std_termios
;
1760 mcfrs_serial_driver
->init_termios
.c_cflag
=
1761 mcfrs_console_cbaud
| CS8
| CREAD
| HUPCL
| CLOCAL
;
1762 mcfrs_serial_driver
->flags
= TTY_DRIVER_REAL_RAW
;
1764 tty_set_operations(mcfrs_serial_driver
, &mcfrs_ops
);
1766 if (tty_register_driver(mcfrs_serial_driver
)) {
1767 printk("MCFRS: Couldn't register serial driver\n");
1768 put_tty_driver(mcfrs_serial_driver
);
1772 local_irq_save(flags
);
1775 * Configure all the attached serial ports.
1777 for (i
= 0, info
= mcfrs_table
; (i
< NR_PORTS
); i
++, info
++) {
1778 info
->magic
= SERIAL_MAGIC
;
1781 info
->custom_divisor
= 16;
1782 info
->close_delay
= 50;
1783 info
->closing_wait
= 3000;
1787 info
->blocked_open
= 0;
1788 INIT_WORK(&info
->tqueue
, mcfrs_offintr
);
1789 INIT_WORK(&info
->tqueue_hangup
, do_serial_hangup
);
1790 init_waitqueue_head(&info
->open_wait
);
1791 init_waitqueue_head(&info
->close_wait
);
1794 mcfrs_setsignals(info
, 0, 0);
1795 mcfrs_irqinit(info
);
1797 printk("ttyS%d at 0x%04x (irq = %d)", info
->line
,
1798 (unsigned int) info
->addr
, info
->irq
);
1799 printk(" is a builtin ColdFire UART\n");
1802 local_irq_restore(flags
);
1806 module_init(mcfrs_init
);
1808 /****************************************************************************/
1809 /* Serial Console */
1810 /****************************************************************************/
1813 * Quick and dirty UART initialization, for console output.
1816 void mcfrs_init_console(void)
1818 volatile unsigned char *uartp
;
1822 * Reset UART, get it into known state...
1824 uartp
= (volatile unsigned char *) (MCF_MBAR
+
1825 (mcfrs_console_port
? MCFUART_BASE2
: MCFUART_BASE1
));
1827 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETRX
; /* reset RX */
1828 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETTX
; /* reset TX */
1829 uartp
[MCFUART_UCR
] = MCFUART_UCR_CMDRESETMRPTR
; /* reset MR pointer */
1832 * Set port for defined baud , 8 data bits, 1 stop bit, no parity.
1834 uartp
[MCFUART_UMR
] = MCFUART_MR1_PARITYNONE
| MCFUART_MR1_CS8
;
1835 uartp
[MCFUART_UMR
] = MCFUART_MR2_STOP1
;
1840 * For the MCF5272, also compute the baudrate fraction.
1842 int fraction
= MCF_BUSCLK
- (clk
* 32 * mcfrs_console_baud
);
1844 fraction
/= (32 * mcfrs_console_baud
);
1845 uartp
[MCFUART_UFPD
] = (fraction
& 0xf); /* set fraction */
1846 clk
= (MCF_BUSCLK
/ mcfrs_console_baud
) / 32;
1849 clk
= ((MCF_BUSCLK
/ mcfrs_console_baud
) + 16) / 32; /* set baud */
1852 uartp
[MCFUART_UBG1
] = (clk
& 0xff00) >> 8; /* set msb baud */
1853 uartp
[MCFUART_UBG2
] = (clk
& 0xff); /* set lsb baud */
1854 uartp
[MCFUART_UCSR
] = MCFUART_UCSR_RXCLKTIMER
| MCFUART_UCSR_TXCLKTIMER
;
1855 uartp
[MCFUART_UCR
] = MCFUART_UCR_RXENABLE
| MCFUART_UCR_TXENABLE
;
1857 mcfrs_console_inited
++;
1863 * Setup for console. Argument comes from the boot command line.
1866 int mcfrs_console_setup(struct console
*cp
, char *arg
)
1868 int i
, n
= CONSOLE_BAUD_RATE
;
1873 if (!strncmp(cp
->name
, "ttyS", 4))
1874 mcfrs_console_port
= cp
->index
;
1875 else if (!strncmp(cp
->name
, "cua", 3))
1876 mcfrs_console_port
= cp
->index
;
1881 n
= simple_strtoul(arg
,NULL
,0);
1882 for (i
= 0; i
< MCFRS_BAUD_TABLE_SIZE
; i
++)
1883 if (mcfrs_baud_table
[i
] == n
)
1885 if (i
< MCFRS_BAUD_TABLE_SIZE
) {
1886 mcfrs_console_baud
= n
;
1887 mcfrs_console_cbaud
= 0;
1889 mcfrs_console_cbaud
|= CBAUDEX
;
1892 mcfrs_console_cbaud
|= i
;
1894 mcfrs_init_console(); /* make sure baud rate changes */
1899 static struct tty_driver
*mcfrs_console_device(struct console
*c
, int *index
)
1902 return mcfrs_serial_driver
;
1907 * Output a single character, using UART polled mode.
1908 * This is used for console output.
1911 void mcfrs_put_char(char ch
)
1913 volatile unsigned char *uartp
;
1914 unsigned long flags
;
1917 uartp
= (volatile unsigned char *) (MCF_MBAR
+
1918 (mcfrs_console_port
? MCFUART_BASE2
: MCFUART_BASE1
));
1920 local_irq_save(flags
);
1921 for (i
= 0; (i
< 0x10000); i
++) {
1922 if (uartp
[MCFUART_USR
] & MCFUART_USR_TXREADY
)
1926 uartp
[MCFUART_UTB
] = ch
;
1927 for (i
= 0; (i
< 0x10000); i
++)
1928 if (uartp
[MCFUART_USR
] & MCFUART_USR_TXEMPTY
)
1932 mcfrs_init_console(); /* try and get it back */
1933 local_irq_restore(flags
);
1940 * rs_console_write is registered for printk output.
1943 void mcfrs_console_write(struct console
*cp
, const char *p
, unsigned len
)
1945 if (!mcfrs_console_inited
)
1946 mcfrs_init_console();
1949 mcfrs_put_char('\r');
1950 mcfrs_put_char(*p
++);
1955 * declare our consoles
1958 struct console mcfrs_console
= {
1960 .write
= mcfrs_console_write
,
1961 .device
= mcfrs_console_device
,
1962 .setup
= mcfrs_console_setup
,
1963 .flags
= CON_PRINTBUFFER
,
1967 static int __init
mcfrs_console_init(void)
1969 register_console(&mcfrs_console
);
1973 console_initcall(mcfrs_console_init
);
1975 /****************************************************************************/