2 * drivers/serial/sh-sci.c
4 * SuperH on-chip serial module support. (SCI with no FIFO / with FIFO)
6 * Copyright (C) 2002 - 2011 Paul Mundt
7 * Modified to support SH7720 SCIF. Markus Brunner, Mark Jonas (Jul 2007).
9 * based off of the old drivers/char/sh-sci.c by:
11 * Copyright (C) 1999, 2000 Niibe Yutaka
12 * Copyright (C) 2000 Sugioka Toshinobu
13 * Modified to support multiple serial ports. Stuart Menefy (May 2000).
14 * Modified to support SecureEdge. David McCullough (2002)
15 * Modified to support SH7300 SCIF. Takashi Kusuda (Jun 2003).
16 * Removed SH7300 support (Jul 2007).
18 * This file is subject to the terms and conditions of the GNU General Public
19 * License. See the file "COPYING" in the main directory of this archive
22 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
28 #include <linux/module.h>
29 #include <linux/errno.h>
30 #include <linux/timer.h>
31 #include <linux/interrupt.h>
32 #include <linux/tty.h>
33 #include <linux/tty_flip.h>
34 #include <linux/serial.h>
35 #include <linux/major.h>
36 #include <linux/string.h>
37 #include <linux/sysrq.h>
38 #include <linux/ioport.h>
40 #include <linux/init.h>
41 #include <linux/delay.h>
42 #include <linux/console.h>
43 #include <linux/platform_device.h>
44 #include <linux/serial_sci.h>
45 #include <linux/notifier.h>
46 #include <linux/cpufreq.h>
47 #include <linux/clk.h>
48 #include <linux/ctype.h>
49 #include <linux/err.h>
50 #include <linux/list.h>
51 #include <linux/dmaengine.h>
52 #include <linux/scatterlist.h>
53 #include <linux/slab.h>
56 #include <asm/sh_bios.h>
66 struct uart_port port
;
71 /* Port IRQs: ERI, RXI, TXI, BRI (optional) */
72 unsigned int irqs
[SCIx_NR_IRQS
];
74 /* Port enable callback */
75 void (*enable
)(struct uart_port
*port
);
77 /* Port disable callback */
78 void (*disable
)(struct uart_port
*port
);
81 struct timer_list break_timer
;
84 /* SCSCR initialization */
87 /* SCBRR calculation algo */
88 unsigned int scbrr_algo_id
;
95 struct list_head node
;
97 struct dma_chan
*chan_tx
;
98 struct dma_chan
*chan_rx
;
100 #ifdef CONFIG_SERIAL_SH_SCI_DMA
101 struct device
*dma_dev
;
102 unsigned int slave_tx
;
103 unsigned int slave_rx
;
104 struct dma_async_tx_descriptor
*desc_tx
;
105 struct dma_async_tx_descriptor
*desc_rx
[2];
106 dma_cookie_t cookie_tx
;
107 dma_cookie_t cookie_rx
[2];
108 dma_cookie_t active_rx
;
109 struct scatterlist sg_tx
;
110 unsigned int sg_len_tx
;
111 struct scatterlist sg_rx
[2];
113 struct sh_dmae_slave param_tx
;
114 struct sh_dmae_slave param_rx
;
115 struct work_struct work_tx
;
116 struct work_struct work_rx
;
117 struct timer_list rx_timer
;
118 unsigned int rx_timeout
;
124 struct list_head ports
;
125 struct notifier_block clk_nb
;
128 /* Function prototypes */
129 static void sci_stop_tx(struct uart_port
*port
);
131 #define SCI_NPORTS CONFIG_SERIAL_SH_SCI_NR_UARTS
133 static struct sci_port sci_ports
[SCI_NPORTS
];
134 static struct uart_driver sci_uart_driver
;
136 static inline struct sci_port
*
137 to_sci_port(struct uart_port
*uart
)
139 return container_of(uart
, struct sci_port
, port
);
142 #if defined(CONFIG_CONSOLE_POLL) || defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
144 #ifdef CONFIG_CONSOLE_POLL
145 static inline void handle_error(struct uart_port
*port
)
147 /* Clear error flags */
148 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
151 static int sci_poll_get_char(struct uart_port
*port
)
153 unsigned short status
;
157 status
= sci_in(port
, SCxSR
);
158 if (status
& SCxSR_ERRORS(port
)) {
165 if (!(status
& SCxSR_RDxF(port
)))
168 c
= sci_in(port
, SCxRDR
);
172 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
178 static void sci_poll_put_char(struct uart_port
*port
, unsigned char c
)
180 unsigned short status
;
183 status
= sci_in(port
, SCxSR
);
184 } while (!(status
& SCxSR_TDxE(port
)));
186 sci_out(port
, SCxTDR
, c
);
187 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
) & ~SCxSR_TEND(port
));
189 #endif /* CONFIG_CONSOLE_POLL || CONFIG_SERIAL_SH_SCI_CONSOLE */
191 #if defined(__H8300H__) || defined(__H8300S__)
192 static void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
194 int ch
= (port
->mapbase
- SMR0
) >> 3;
197 H8300_GPIO_DDR(h8300_sci_pins
[ch
].port
,
198 h8300_sci_pins
[ch
].rx
,
200 H8300_GPIO_DDR(h8300_sci_pins
[ch
].port
,
201 h8300_sci_pins
[ch
].tx
,
205 H8300_SCI_DR(ch
) |= h8300_sci_pins
[ch
].tx
;
207 #elif defined(CONFIG_CPU_SUBTYPE_SH7710) || defined(CONFIG_CPU_SUBTYPE_SH7712)
208 static inline void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
210 if (port
->mapbase
== 0xA4400000) {
211 __raw_writew(__raw_readw(PACR
) & 0xffc0, PACR
);
212 __raw_writew(__raw_readw(PBCR
) & 0x0fff, PBCR
);
213 } else if (port
->mapbase
== 0xA4410000)
214 __raw_writew(__raw_readw(PBCR
) & 0xf003, PBCR
);
216 #elif defined(CONFIG_CPU_SUBTYPE_SH7720) || defined(CONFIG_CPU_SUBTYPE_SH7721)
217 static inline void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
221 if (cflag
& CRTSCTS
) {
223 if (port
->mapbase
== 0xa4430000) { /* SCIF0 */
224 /* Clear PTCR bit 9-2; enable all scif pins but sck */
225 data
= __raw_readw(PORT_PTCR
);
226 __raw_writew((data
& 0xfc03), PORT_PTCR
);
227 } else if (port
->mapbase
== 0xa4438000) { /* SCIF1 */
228 /* Clear PVCR bit 9-2 */
229 data
= __raw_readw(PORT_PVCR
);
230 __raw_writew((data
& 0xfc03), PORT_PVCR
);
233 if (port
->mapbase
== 0xa4430000) { /* SCIF0 */
234 /* Clear PTCR bit 5-2; enable only tx and rx */
235 data
= __raw_readw(PORT_PTCR
);
236 __raw_writew((data
& 0xffc3), PORT_PTCR
);
237 } else if (port
->mapbase
== 0xa4438000) { /* SCIF1 */
238 /* Clear PVCR bit 5-2 */
239 data
= __raw_readw(PORT_PVCR
);
240 __raw_writew((data
& 0xffc3), PORT_PVCR
);
244 #elif defined(CONFIG_CPU_SH3)
245 /* For SH7705, SH7706, SH7707, SH7709, SH7709A, SH7729 */
246 static inline void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
250 /* We need to set SCPCR to enable RTS/CTS */
251 data
= __raw_readw(SCPCR
);
252 /* Clear out SCP7MD1,0, SCP6MD1,0, SCP4MD1,0*/
253 __raw_writew(data
& 0x0fcf, SCPCR
);
255 if (!(cflag
& CRTSCTS
)) {
256 /* We need to set SCPCR to enable RTS/CTS */
257 data
= __raw_readw(SCPCR
);
258 /* Clear out SCP7MD1,0, SCP4MD1,0,
259 Set SCP6MD1,0 = {01} (output) */
260 __raw_writew((data
& 0x0fcf) | 0x1000, SCPCR
);
262 data
= __raw_readb(SCPDR
);
263 /* Set /RTS2 (bit6) = 0 */
264 __raw_writeb(data
& 0xbf, SCPDR
);
267 #elif defined(CONFIG_CPU_SUBTYPE_SH7722)
268 static inline void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
272 if (port
->mapbase
== 0xffe00000) {
273 data
= __raw_readw(PSCR
);
275 if (!(cflag
& CRTSCTS
))
278 __raw_writew(data
, PSCR
);
281 #elif defined(CONFIG_CPU_SUBTYPE_SH7757) || \
282 defined(CONFIG_CPU_SUBTYPE_SH7763) || \
283 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
284 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
285 defined(CONFIG_CPU_SUBTYPE_SH7786) || \
286 defined(CONFIG_CPU_SUBTYPE_SHX3)
287 static inline void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
289 if (!(cflag
& CRTSCTS
))
290 __raw_writew(0x0080, SCSPTR0
); /* Set RTS = 1 */
292 #elif defined(CONFIG_CPU_SH4) && !defined(CONFIG_CPU_SH4A)
293 static inline void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
295 if (!(cflag
& CRTSCTS
))
296 __raw_writew(0x0080, SCSPTR2
); /* Set RTS = 1 */
299 static inline void sci_init_pins(struct uart_port
*port
, unsigned int cflag
)
305 #if defined(CONFIG_CPU_SUBTYPE_SH7760) || \
306 defined(CONFIG_CPU_SUBTYPE_SH7780) || \
307 defined(CONFIG_CPU_SUBTYPE_SH7785) || \
308 defined(CONFIG_CPU_SUBTYPE_SH7786)
309 static int scif_txfill(struct uart_port
*port
)
311 return sci_in(port
, SCTFDR
) & 0xff;
314 static int scif_txroom(struct uart_port
*port
)
316 return SCIF_TXROOM_MAX
- scif_txfill(port
);
319 static int scif_rxfill(struct uart_port
*port
)
321 return sci_in(port
, SCRFDR
) & 0xff;
323 #elif defined(CONFIG_CPU_SUBTYPE_SH7763)
324 static int scif_txfill(struct uart_port
*port
)
326 if (port
->mapbase
== 0xffe00000 ||
327 port
->mapbase
== 0xffe08000)
329 return sci_in(port
, SCTFDR
) & 0xff;
332 return sci_in(port
, SCFDR
) >> 8;
335 static int scif_txroom(struct uart_port
*port
)
337 if (port
->mapbase
== 0xffe00000 ||
338 port
->mapbase
== 0xffe08000)
340 return SCIF_TXROOM_MAX
- scif_txfill(port
);
343 return SCIF2_TXROOM_MAX
- scif_txfill(port
);
346 static int scif_rxfill(struct uart_port
*port
)
348 if ((port
->mapbase
== 0xffe00000) ||
349 (port
->mapbase
== 0xffe08000)) {
351 return sci_in(port
, SCRFDR
) & 0xff;
354 return sci_in(port
, SCFDR
) & SCIF2_RFDC_MASK
;
357 #elif defined(CONFIG_ARCH_SH7372)
358 static int scif_txfill(struct uart_port
*port
)
360 if (port
->type
== PORT_SCIFA
)
361 return sci_in(port
, SCFDR
) >> 8;
363 return sci_in(port
, SCTFDR
);
366 static int scif_txroom(struct uart_port
*port
)
368 return port
->fifosize
- scif_txfill(port
);
371 static int scif_rxfill(struct uart_port
*port
)
373 if (port
->type
== PORT_SCIFA
)
374 return sci_in(port
, SCFDR
) & SCIF_RFDC_MASK
;
376 return sci_in(port
, SCRFDR
);
379 static int scif_txfill(struct uart_port
*port
)
381 return sci_in(port
, SCFDR
) >> 8;
384 static int scif_txroom(struct uart_port
*port
)
386 return SCIF_TXROOM_MAX
- scif_txfill(port
);
389 static int scif_rxfill(struct uart_port
*port
)
391 return sci_in(port
, SCFDR
) & SCIF_RFDC_MASK
;
395 static int sci_txfill(struct uart_port
*port
)
397 return !(sci_in(port
, SCxSR
) & SCI_TDRE
);
400 static int sci_txroom(struct uart_port
*port
)
402 return !sci_txfill(port
);
405 static int sci_rxfill(struct uart_port
*port
)
407 return (sci_in(port
, SCxSR
) & SCxSR_RDxF(port
)) != 0;
410 /* ********************************************************************** *
411 * the interrupt related routines *
412 * ********************************************************************** */
414 static void sci_transmit_chars(struct uart_port
*port
)
416 struct circ_buf
*xmit
= &port
->state
->xmit
;
417 unsigned int stopped
= uart_tx_stopped(port
);
418 unsigned short status
;
422 status
= sci_in(port
, SCxSR
);
423 if (!(status
& SCxSR_TDxE(port
))) {
424 ctrl
= sci_in(port
, SCSCR
);
425 if (uart_circ_empty(xmit
))
429 sci_out(port
, SCSCR
, ctrl
);
433 if (port
->type
== PORT_SCI
)
434 count
= sci_txroom(port
);
436 count
= scif_txroom(port
);
444 } else if (!uart_circ_empty(xmit
) && !stopped
) {
445 c
= xmit
->buf
[xmit
->tail
];
446 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
451 sci_out(port
, SCxTDR
, c
);
454 } while (--count
> 0);
456 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
458 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
459 uart_write_wakeup(port
);
460 if (uart_circ_empty(xmit
)) {
463 ctrl
= sci_in(port
, SCSCR
);
465 if (port
->type
!= PORT_SCI
) {
466 sci_in(port
, SCxSR
); /* Dummy read */
467 sci_out(port
, SCxSR
, SCxSR_TDxE_CLEAR(port
));
471 sci_out(port
, SCSCR
, ctrl
);
475 /* On SH3, SCIF may read end-of-break as a space->mark char */
476 #define STEPFN(c) ({int __c = (c); (((__c-1)|(__c)) == -1); })
478 static inline void sci_receive_chars(struct uart_port
*port
)
480 struct sci_port
*sci_port
= to_sci_port(port
);
481 struct tty_struct
*tty
= port
->state
->port
.tty
;
482 int i
, count
, copied
= 0;
483 unsigned short status
;
486 status
= sci_in(port
, SCxSR
);
487 if (!(status
& SCxSR_RDxF(port
)))
491 if (port
->type
== PORT_SCI
)
492 count
= sci_rxfill(port
);
494 count
= scif_rxfill(port
);
496 /* Don't copy more bytes than there is room for in the buffer */
497 count
= tty_buffer_request_room(tty
, count
);
499 /* If for any reason we can't copy more data, we're done! */
503 if (port
->type
== PORT_SCI
) {
504 char c
= sci_in(port
, SCxRDR
);
505 if (uart_handle_sysrq_char(port
, c
) ||
506 sci_port
->break_flag
)
509 tty_insert_flip_char(tty
, c
, TTY_NORMAL
);
511 for (i
= 0; i
< count
; i
++) {
512 char c
= sci_in(port
, SCxRDR
);
513 status
= sci_in(port
, SCxSR
);
514 #if defined(CONFIG_CPU_SH3)
515 /* Skip "chars" during break */
516 if (sci_port
->break_flag
) {
518 (status
& SCxSR_FER(port
))) {
523 /* Nonzero => end-of-break */
524 dev_dbg(port
->dev
, "debounce<%02x>\n", c
);
525 sci_port
->break_flag
= 0;
532 #endif /* CONFIG_CPU_SH3 */
533 if (uart_handle_sysrq_char(port
, c
)) {
538 /* Store data and status */
539 if (status
& SCxSR_FER(port
)) {
541 dev_notice(port
->dev
, "frame error\n");
542 } else if (status
& SCxSR_PER(port
)) {
544 dev_notice(port
->dev
, "parity error\n");
548 tty_insert_flip_char(tty
, c
, flag
);
552 sci_in(port
, SCxSR
); /* dummy read */
553 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
556 port
->icount
.rx
+= count
;
560 /* Tell the rest of the system the news. New characters! */
561 tty_flip_buffer_push(tty
);
563 sci_in(port
, SCxSR
); /* dummy read */
564 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
568 #define SCI_BREAK_JIFFIES (HZ/20)
569 /* The sci generates interrupts during the break,
570 * 1 per millisecond or so during the break period, for 9600 baud.
571 * So dont bother disabling interrupts.
572 * But dont want more than 1 break event.
573 * Use a kernel timer to periodically poll the rx line until
574 * the break is finished.
576 static void sci_schedule_break_timer(struct sci_port
*port
)
578 port
->break_timer
.expires
= jiffies
+ SCI_BREAK_JIFFIES
;
579 add_timer(&port
->break_timer
);
581 /* Ensure that two consecutive samples find the break over. */
582 static void sci_break_timer(unsigned long data
)
584 struct sci_port
*port
= (struct sci_port
*)data
;
586 if (sci_rxd_in(&port
->port
) == 0) {
587 port
->break_flag
= 1;
588 sci_schedule_break_timer(port
);
589 } else if (port
->break_flag
== 1) {
591 port
->break_flag
= 2;
592 sci_schedule_break_timer(port
);
594 port
->break_flag
= 0;
597 static inline int sci_handle_errors(struct uart_port
*port
)
600 unsigned short status
= sci_in(port
, SCxSR
);
601 struct tty_struct
*tty
= port
->state
->port
.tty
;
603 if (status
& SCxSR_ORER(port
)) {
605 if (tty_insert_flip_char(tty
, 0, TTY_OVERRUN
))
608 dev_notice(port
->dev
, "overrun error");
611 if (status
& SCxSR_FER(port
)) {
612 if (sci_rxd_in(port
) == 0) {
613 /* Notify of BREAK */
614 struct sci_port
*sci_port
= to_sci_port(port
);
616 if (!sci_port
->break_flag
) {
617 sci_port
->break_flag
= 1;
618 sci_schedule_break_timer(sci_port
);
620 /* Do sysrq handling. */
621 if (uart_handle_break(port
))
624 dev_dbg(port
->dev
, "BREAK detected\n");
626 if (tty_insert_flip_char(tty
, 0, TTY_BREAK
))
632 if (tty_insert_flip_char(tty
, 0, TTY_FRAME
))
635 dev_notice(port
->dev
, "frame error\n");
639 if (status
& SCxSR_PER(port
)) {
641 if (tty_insert_flip_char(tty
, 0, TTY_PARITY
))
644 dev_notice(port
->dev
, "parity error");
648 tty_flip_buffer_push(tty
);
653 static inline int sci_handle_fifo_overrun(struct uart_port
*port
)
655 struct tty_struct
*tty
= port
->state
->port
.tty
;
658 if (port
->type
!= PORT_SCIF
)
661 if ((sci_in(port
, SCLSR
) & SCIF_ORER
) != 0) {
662 sci_out(port
, SCLSR
, 0);
664 tty_insert_flip_char(tty
, 0, TTY_OVERRUN
);
665 tty_flip_buffer_push(tty
);
667 dev_notice(port
->dev
, "overrun error\n");
674 static inline int sci_handle_breaks(struct uart_port
*port
)
677 unsigned short status
= sci_in(port
, SCxSR
);
678 struct tty_struct
*tty
= port
->state
->port
.tty
;
679 struct sci_port
*s
= to_sci_port(port
);
681 if (uart_handle_break(port
))
684 if (!s
->break_flag
&& status
& SCxSR_BRK(port
)) {
685 #if defined(CONFIG_CPU_SH3)
689 /* Notify of BREAK */
690 if (tty_insert_flip_char(tty
, 0, TTY_BREAK
))
693 dev_dbg(port
->dev
, "BREAK detected\n");
697 tty_flip_buffer_push(tty
);
699 copied
+= sci_handle_fifo_overrun(port
);
704 static irqreturn_t
sci_rx_interrupt(int irq
, void *ptr
)
706 #ifdef CONFIG_SERIAL_SH_SCI_DMA
707 struct uart_port
*port
= ptr
;
708 struct sci_port
*s
= to_sci_port(port
);
711 u16 scr
= sci_in(port
, SCSCR
);
712 u16 ssr
= sci_in(port
, SCxSR
);
714 /* Disable future Rx interrupts */
715 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
716 disable_irq_nosync(irq
);
721 sci_out(port
, SCSCR
, scr
);
722 /* Clear current interrupt */
723 sci_out(port
, SCxSR
, ssr
& ~(1 | SCxSR_RDxF(port
)));
724 dev_dbg(port
->dev
, "Rx IRQ %lu: setup t-out in %u jiffies\n",
725 jiffies
, s
->rx_timeout
);
726 mod_timer(&s
->rx_timer
, jiffies
+ s
->rx_timeout
);
732 /* I think sci_receive_chars has to be called irrespective
733 * of whether the I_IXOFF is set, otherwise, how is the interrupt
736 sci_receive_chars(ptr
);
741 static irqreturn_t
sci_tx_interrupt(int irq
, void *ptr
)
743 struct uart_port
*port
= ptr
;
746 spin_lock_irqsave(&port
->lock
, flags
);
747 sci_transmit_chars(port
);
748 spin_unlock_irqrestore(&port
->lock
, flags
);
753 static irqreturn_t
sci_er_interrupt(int irq
, void *ptr
)
755 struct uart_port
*port
= ptr
;
758 if (port
->type
== PORT_SCI
) {
759 if (sci_handle_errors(port
)) {
760 /* discard character in rx buffer */
762 sci_out(port
, SCxSR
, SCxSR_RDxF_CLEAR(port
));
765 sci_handle_fifo_overrun(port
);
766 sci_rx_interrupt(irq
, ptr
);
769 sci_out(port
, SCxSR
, SCxSR_ERROR_CLEAR(port
));
771 /* Kick the transmission */
772 sci_tx_interrupt(irq
, ptr
);
777 static irqreturn_t
sci_br_interrupt(int irq
, void *ptr
)
779 struct uart_port
*port
= ptr
;
782 sci_handle_breaks(port
);
783 sci_out(port
, SCxSR
, SCxSR_BREAK_CLEAR(port
));
788 static inline unsigned long port_rx_irq_mask(struct uart_port
*port
)
791 * Not all ports (such as SCIFA) will support REIE. Rather than
792 * special-casing the port type, we check the port initialization
793 * IRQ enable mask to see whether the IRQ is desired at all. If
794 * it's unset, it's logically inferred that there's no point in
797 return SCSCR_RIE
| (to_sci_port(port
)->scscr
& SCSCR_REIE
);
800 static irqreturn_t
sci_mpxed_interrupt(int irq
, void *ptr
)
802 unsigned short ssr_status
, scr_status
, err_enabled
;
803 struct uart_port
*port
= ptr
;
804 struct sci_port
*s
= to_sci_port(port
);
805 irqreturn_t ret
= IRQ_NONE
;
807 ssr_status
= sci_in(port
, SCxSR
);
808 scr_status
= sci_in(port
, SCSCR
);
809 err_enabled
= scr_status
& port_rx_irq_mask(port
);
812 if ((ssr_status
& SCxSR_TDxE(port
)) && (scr_status
& SCSCR_TIE
) &&
814 ret
= sci_tx_interrupt(irq
, ptr
);
817 * Rx Interrupt: if we're using DMA, the DMA controller clears RDF /
820 if (((ssr_status
& SCxSR_RDxF(port
)) || s
->chan_rx
) &&
821 (scr_status
& SCSCR_RIE
))
822 ret
= sci_rx_interrupt(irq
, ptr
);
824 /* Error Interrupt */
825 if ((ssr_status
& SCxSR_ERRORS(port
)) && err_enabled
)
826 ret
= sci_er_interrupt(irq
, ptr
);
828 /* Break Interrupt */
829 if ((ssr_status
& SCxSR_BRK(port
)) && err_enabled
)
830 ret
= sci_br_interrupt(irq
, ptr
);
836 * Here we define a transistion notifier so that we can update all of our
837 * ports' baud rate when the peripheral clock changes.
839 static int sci_notifier(struct notifier_block
*self
,
840 unsigned long phase
, void *p
)
842 struct sh_sci_priv
*priv
= container_of(self
,
843 struct sh_sci_priv
, clk_nb
);
844 struct sci_port
*sci_port
;
847 if ((phase
== CPUFREQ_POSTCHANGE
) ||
848 (phase
== CPUFREQ_RESUMECHANGE
)) {
849 spin_lock_irqsave(&priv
->lock
, flags
);
850 list_for_each_entry(sci_port
, &priv
->ports
, node
)
851 sci_port
->port
.uartclk
= clk_get_rate(sci_port
->iclk
);
852 spin_unlock_irqrestore(&priv
->lock
, flags
);
858 static void sci_clk_enable(struct uart_port
*port
)
860 struct sci_port
*sci_port
= to_sci_port(port
);
862 clk_enable(sci_port
->iclk
);
863 sci_port
->port
.uartclk
= clk_get_rate(sci_port
->iclk
);
864 clk_enable(sci_port
->fclk
);
867 static void sci_clk_disable(struct uart_port
*port
)
869 struct sci_port
*sci_port
= to_sci_port(port
);
871 clk_disable(sci_port
->fclk
);
872 clk_disable(sci_port
->iclk
);
875 static int sci_request_irq(struct sci_port
*port
)
878 irqreturn_t (*handlers
[4])(int irq
, void *ptr
) = {
879 sci_er_interrupt
, sci_rx_interrupt
, sci_tx_interrupt
,
882 const char *desc
[] = { "SCI Receive Error", "SCI Receive Data Full",
883 "SCI Transmit Data Empty", "SCI Break" };
885 if (port
->irqs
[0] == port
->irqs
[1]) {
886 if (unlikely(!port
->irqs
[0]))
889 if (request_irq(port
->irqs
[0], sci_mpxed_interrupt
,
890 IRQF_DISABLED
, "sci", port
)) {
891 dev_err(port
->port
.dev
, "Can't allocate IRQ\n");
895 for (i
= 0; i
< ARRAY_SIZE(handlers
); i
++) {
896 if (unlikely(!port
->irqs
[i
]))
899 if (request_irq(port
->irqs
[i
], handlers
[i
],
900 IRQF_DISABLED
, desc
[i
], port
)) {
901 dev_err(port
->port
.dev
, "Can't allocate IRQ\n");
910 static void sci_free_irq(struct sci_port
*port
)
914 if (port
->irqs
[0] == port
->irqs
[1])
915 free_irq(port
->irqs
[0], port
);
917 for (i
= 0; i
< ARRAY_SIZE(port
->irqs
); i
++) {
921 free_irq(port
->irqs
[i
], port
);
926 static unsigned int sci_tx_empty(struct uart_port
*port
)
928 unsigned short status
= sci_in(port
, SCxSR
);
929 unsigned short in_tx_fifo
= scif_txfill(port
);
931 return (status
& SCxSR_TEND(port
)) && !in_tx_fifo
? TIOCSER_TEMT
: 0;
934 static void sci_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
936 /* This routine is used for seting signals of: DTR, DCD, CTS/RTS */
937 /* We use SCIF's hardware for CTS/RTS, so don't need any for that. */
938 /* If you have signals for DTR and DCD, please implement here. */
941 static unsigned int sci_get_mctrl(struct uart_port
*port
)
943 /* This routine is used for getting signals of: DTR, DCD, DSR, RI,
946 return TIOCM_DTR
| TIOCM_RTS
| TIOCM_DSR
;
949 #ifdef CONFIG_SERIAL_SH_SCI_DMA
950 static void sci_dma_tx_complete(void *arg
)
952 struct sci_port
*s
= arg
;
953 struct uart_port
*port
= &s
->port
;
954 struct circ_buf
*xmit
= &port
->state
->xmit
;
957 dev_dbg(port
->dev
, "%s(%d)\n", __func__
, port
->line
);
959 spin_lock_irqsave(&port
->lock
, flags
);
961 xmit
->tail
+= sg_dma_len(&s
->sg_tx
);
962 xmit
->tail
&= UART_XMIT_SIZE
- 1;
964 port
->icount
.tx
+= sg_dma_len(&s
->sg_tx
);
966 async_tx_ack(s
->desc_tx
);
967 s
->cookie_tx
= -EINVAL
;
970 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
971 uart_write_wakeup(port
);
973 if (!uart_circ_empty(xmit
)) {
974 schedule_work(&s
->work_tx
);
975 } else if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
976 u16 ctrl
= sci_in(port
, SCSCR
);
977 sci_out(port
, SCSCR
, ctrl
& ~SCSCR_TIE
);
980 spin_unlock_irqrestore(&port
->lock
, flags
);
983 /* Locking: called with port lock held */
984 static int sci_dma_rx_push(struct sci_port
*s
, struct tty_struct
*tty
,
987 struct uart_port
*port
= &s
->port
;
990 room
= tty_buffer_request_room(tty
, count
);
992 if (s
->active_rx
== s
->cookie_rx
[0]) {
994 } else if (s
->active_rx
== s
->cookie_rx
[1]) {
997 dev_err(port
->dev
, "cookie %d not found!\n", s
->active_rx
);
1002 dev_warn(port
->dev
, "Rx overrun: dropping %u bytes\n",
1007 for (i
= 0; i
< room
; i
++)
1008 tty_insert_flip_char(tty
, ((u8
*)sg_virt(&s
->sg_rx
[active
]))[i
],
1011 port
->icount
.rx
+= room
;
1016 static void sci_dma_rx_complete(void *arg
)
1018 struct sci_port
*s
= arg
;
1019 struct uart_port
*port
= &s
->port
;
1020 struct tty_struct
*tty
= port
->state
->port
.tty
;
1021 unsigned long flags
;
1024 dev_dbg(port
->dev
, "%s(%d) active #%d\n", __func__
, port
->line
, s
->active_rx
);
1026 spin_lock_irqsave(&port
->lock
, flags
);
1028 count
= sci_dma_rx_push(s
, tty
, s
->buf_len_rx
);
1030 mod_timer(&s
->rx_timer
, jiffies
+ s
->rx_timeout
);
1032 spin_unlock_irqrestore(&port
->lock
, flags
);
1035 tty_flip_buffer_push(tty
);
1037 schedule_work(&s
->work_rx
);
1040 static void sci_start_rx(struct uart_port
*port
);
1041 static void sci_start_tx(struct uart_port
*port
);
1043 static void sci_rx_dma_release(struct sci_port
*s
, bool enable_pio
)
1045 struct dma_chan
*chan
= s
->chan_rx
;
1046 struct uart_port
*port
= &s
->port
;
1049 s
->cookie_rx
[0] = s
->cookie_rx
[1] = -EINVAL
;
1050 dma_release_channel(chan
);
1051 if (sg_dma_address(&s
->sg_rx
[0]))
1052 dma_free_coherent(port
->dev
, s
->buf_len_rx
* 2,
1053 sg_virt(&s
->sg_rx
[0]), sg_dma_address(&s
->sg_rx
[0]));
1058 static void sci_tx_dma_release(struct sci_port
*s
, bool enable_pio
)
1060 struct dma_chan
*chan
= s
->chan_tx
;
1061 struct uart_port
*port
= &s
->port
;
1064 s
->cookie_tx
= -EINVAL
;
1065 dma_release_channel(chan
);
1070 static void sci_submit_rx(struct sci_port
*s
)
1072 struct dma_chan
*chan
= s
->chan_rx
;
1075 for (i
= 0; i
< 2; i
++) {
1076 struct scatterlist
*sg
= &s
->sg_rx
[i
];
1077 struct dma_async_tx_descriptor
*desc
;
1079 desc
= chan
->device
->device_prep_slave_sg(chan
,
1080 sg
, 1, DMA_FROM_DEVICE
, DMA_PREP_INTERRUPT
);
1083 s
->desc_rx
[i
] = desc
;
1084 desc
->callback
= sci_dma_rx_complete
;
1085 desc
->callback_param
= s
;
1086 s
->cookie_rx
[i
] = desc
->tx_submit(desc
);
1089 if (!desc
|| s
->cookie_rx
[i
] < 0) {
1091 async_tx_ack(s
->desc_rx
[0]);
1092 s
->cookie_rx
[0] = -EINVAL
;
1096 s
->cookie_rx
[i
] = -EINVAL
;
1098 dev_warn(s
->port
.dev
,
1099 "failed to re-start DMA, using PIO\n");
1100 sci_rx_dma_release(s
, true);
1103 dev_dbg(s
->port
.dev
, "%s(): cookie %d to #%d\n", __func__
,
1104 s
->cookie_rx
[i
], i
);
1107 s
->active_rx
= s
->cookie_rx
[0];
1109 dma_async_issue_pending(chan
);
1112 static void work_fn_rx(struct work_struct
*work
)
1114 struct sci_port
*s
= container_of(work
, struct sci_port
, work_rx
);
1115 struct uart_port
*port
= &s
->port
;
1116 struct dma_async_tx_descriptor
*desc
;
1119 if (s
->active_rx
== s
->cookie_rx
[0]) {
1121 } else if (s
->active_rx
== s
->cookie_rx
[1]) {
1124 dev_err(port
->dev
, "cookie %d not found!\n", s
->active_rx
);
1127 desc
= s
->desc_rx
[new];
1129 if (dma_async_is_tx_complete(s
->chan_rx
, s
->active_rx
, NULL
, NULL
) !=
1131 /* Handle incomplete DMA receive */
1132 struct tty_struct
*tty
= port
->state
->port
.tty
;
1133 struct dma_chan
*chan
= s
->chan_rx
;
1134 struct sh_desc
*sh_desc
= container_of(desc
, struct sh_desc
,
1136 unsigned long flags
;
1139 chan
->device
->device_control(chan
, DMA_TERMINATE_ALL
, 0);
1140 dev_dbg(port
->dev
, "Read %u bytes with cookie %d\n",
1141 sh_desc
->partial
, sh_desc
->cookie
);
1143 spin_lock_irqsave(&port
->lock
, flags
);
1144 count
= sci_dma_rx_push(s
, tty
, sh_desc
->partial
);
1145 spin_unlock_irqrestore(&port
->lock
, flags
);
1148 tty_flip_buffer_push(tty
);
1155 s
->cookie_rx
[new] = desc
->tx_submit(desc
);
1156 if (s
->cookie_rx
[new] < 0) {
1157 dev_warn(port
->dev
, "Failed submitting Rx DMA descriptor\n");
1158 sci_rx_dma_release(s
, true);
1162 s
->active_rx
= s
->cookie_rx
[!new];
1164 dev_dbg(port
->dev
, "%s: cookie %d #%d, new active #%d\n", __func__
,
1165 s
->cookie_rx
[new], new, s
->active_rx
);
1168 static void work_fn_tx(struct work_struct
*work
)
1170 struct sci_port
*s
= container_of(work
, struct sci_port
, work_tx
);
1171 struct dma_async_tx_descriptor
*desc
;
1172 struct dma_chan
*chan
= s
->chan_tx
;
1173 struct uart_port
*port
= &s
->port
;
1174 struct circ_buf
*xmit
= &port
->state
->xmit
;
1175 struct scatterlist
*sg
= &s
->sg_tx
;
1179 * Port xmit buffer is already mapped, and it is one page... Just adjust
1180 * offsets and lengths. Since it is a circular buffer, we have to
1181 * transmit till the end, and then the rest. Take the port lock to get a
1182 * consistent xmit buffer state.
1184 spin_lock_irq(&port
->lock
);
1185 sg
->offset
= xmit
->tail
& (UART_XMIT_SIZE
- 1);
1186 sg_dma_address(sg
) = (sg_dma_address(sg
) & ~(UART_XMIT_SIZE
- 1)) +
1188 sg_dma_len(sg
) = min((int)CIRC_CNT(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
),
1189 CIRC_CNT_TO_END(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
));
1190 spin_unlock_irq(&port
->lock
);
1192 BUG_ON(!sg_dma_len(sg
));
1194 desc
= chan
->device
->device_prep_slave_sg(chan
,
1195 sg
, s
->sg_len_tx
, DMA_TO_DEVICE
,
1196 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
1199 sci_tx_dma_release(s
, true);
1203 dma_sync_sg_for_device(port
->dev
, sg
, 1, DMA_TO_DEVICE
);
1205 spin_lock_irq(&port
->lock
);
1207 desc
->callback
= sci_dma_tx_complete
;
1208 desc
->callback_param
= s
;
1209 spin_unlock_irq(&port
->lock
);
1210 s
->cookie_tx
= desc
->tx_submit(desc
);
1211 if (s
->cookie_tx
< 0) {
1212 dev_warn(port
->dev
, "Failed submitting Tx DMA descriptor\n");
1214 sci_tx_dma_release(s
, true);
1218 dev_dbg(port
->dev
, "%s: %p: %d...%d, cookie %d\n", __func__
,
1219 xmit
->buf
, xmit
->tail
, xmit
->head
, s
->cookie_tx
);
1221 dma_async_issue_pending(chan
);
1225 static void sci_start_tx(struct uart_port
*port
)
1227 struct sci_port
*s
= to_sci_port(port
);
1228 unsigned short ctrl
;
1230 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1231 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
1232 u16
new, scr
= sci_in(port
, SCSCR
);
1236 new = scr
& ~0x8000;
1238 sci_out(port
, SCSCR
, new);
1241 if (s
->chan_tx
&& !uart_circ_empty(&s
->port
.state
->xmit
) &&
1243 schedule_work(&s
->work_tx
);
1246 if (!s
->chan_tx
|| port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
1247 /* Set TIE (Transmit Interrupt Enable) bit in SCSCR */
1248 ctrl
= sci_in(port
, SCSCR
);
1249 sci_out(port
, SCSCR
, ctrl
| SCSCR_TIE
);
1253 static void sci_stop_tx(struct uart_port
*port
)
1255 unsigned short ctrl
;
1257 /* Clear TIE (Transmit Interrupt Enable) bit in SCSCR */
1258 ctrl
= sci_in(port
, SCSCR
);
1260 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
1265 sci_out(port
, SCSCR
, ctrl
);
1268 static void sci_start_rx(struct uart_port
*port
)
1270 unsigned short ctrl
;
1272 ctrl
= sci_in(port
, SCSCR
) | port_rx_irq_mask(port
);
1274 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
1277 sci_out(port
, SCSCR
, ctrl
);
1280 static void sci_stop_rx(struct uart_port
*port
)
1282 unsigned short ctrl
;
1284 ctrl
= sci_in(port
, SCSCR
);
1286 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
)
1289 ctrl
&= ~port_rx_irq_mask(port
);
1291 sci_out(port
, SCSCR
, ctrl
);
1294 static void sci_enable_ms(struct uart_port
*port
)
1296 /* Nothing here yet .. */
1299 static void sci_break_ctl(struct uart_port
*port
, int break_state
)
1301 /* Nothing here yet .. */
1304 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1305 static bool filter(struct dma_chan
*chan
, void *slave
)
1307 struct sh_dmae_slave
*param
= slave
;
1309 dev_dbg(chan
->device
->dev
, "%s: slave ID %d\n", __func__
,
1312 if (param
->dma_dev
== chan
->device
->dev
) {
1313 chan
->private = param
;
1320 static void rx_timer_fn(unsigned long arg
)
1322 struct sci_port
*s
= (struct sci_port
*)arg
;
1323 struct uart_port
*port
= &s
->port
;
1324 u16 scr
= sci_in(port
, SCSCR
);
1326 if (port
->type
== PORT_SCIFA
|| port
->type
== PORT_SCIFB
) {
1328 enable_irq(s
->irqs
[1]);
1330 sci_out(port
, SCSCR
, scr
| SCSCR_RIE
);
1331 dev_dbg(port
->dev
, "DMA Rx timed out\n");
1332 schedule_work(&s
->work_rx
);
1335 static void sci_request_dma(struct uart_port
*port
)
1337 struct sci_port
*s
= to_sci_port(port
);
1338 struct sh_dmae_slave
*param
;
1339 struct dma_chan
*chan
;
1340 dma_cap_mask_t mask
;
1343 dev_dbg(port
->dev
, "%s: port %d DMA %p\n", __func__
,
1344 port
->line
, s
->dma_dev
);
1350 dma_cap_set(DMA_SLAVE
, mask
);
1352 param
= &s
->param_tx
;
1354 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_TX */
1355 param
->slave_id
= s
->slave_tx
;
1356 param
->dma_dev
= s
->dma_dev
;
1358 s
->cookie_tx
= -EINVAL
;
1359 chan
= dma_request_channel(mask
, filter
, param
);
1360 dev_dbg(port
->dev
, "%s: TX: got channel %p\n", __func__
, chan
);
1363 sg_init_table(&s
->sg_tx
, 1);
1364 /* UART circular tx buffer is an aligned page. */
1365 BUG_ON((int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
1366 sg_set_page(&s
->sg_tx
, virt_to_page(port
->state
->xmit
.buf
),
1367 UART_XMIT_SIZE
, (int)port
->state
->xmit
.buf
& ~PAGE_MASK
);
1368 nent
= dma_map_sg(port
->dev
, &s
->sg_tx
, 1, DMA_TO_DEVICE
);
1370 sci_tx_dma_release(s
, false);
1372 dev_dbg(port
->dev
, "%s: mapped %d@%p to %x\n", __func__
,
1373 sg_dma_len(&s
->sg_tx
),
1374 port
->state
->xmit
.buf
, sg_dma_address(&s
->sg_tx
));
1376 s
->sg_len_tx
= nent
;
1378 INIT_WORK(&s
->work_tx
, work_fn_tx
);
1381 param
= &s
->param_rx
;
1383 /* Slave ID, e.g., SHDMA_SLAVE_SCIF0_RX */
1384 param
->slave_id
= s
->slave_rx
;
1385 param
->dma_dev
= s
->dma_dev
;
1387 chan
= dma_request_channel(mask
, filter
, param
);
1388 dev_dbg(port
->dev
, "%s: RX: got channel %p\n", __func__
, chan
);
1396 s
->buf_len_rx
= 2 * max(16, (int)port
->fifosize
);
1397 buf
[0] = dma_alloc_coherent(port
->dev
, s
->buf_len_rx
* 2,
1398 &dma
[0], GFP_KERNEL
);
1402 "failed to allocate dma buffer, using PIO\n");
1403 sci_rx_dma_release(s
, true);
1407 buf
[1] = buf
[0] + s
->buf_len_rx
;
1408 dma
[1] = dma
[0] + s
->buf_len_rx
;
1410 for (i
= 0; i
< 2; i
++) {
1411 struct scatterlist
*sg
= &s
->sg_rx
[i
];
1413 sg_init_table(sg
, 1);
1414 sg_set_page(sg
, virt_to_page(buf
[i
]), s
->buf_len_rx
,
1415 (int)buf
[i
] & ~PAGE_MASK
);
1416 sg_dma_address(sg
) = dma
[i
];
1419 INIT_WORK(&s
->work_rx
, work_fn_rx
);
1420 setup_timer(&s
->rx_timer
, rx_timer_fn
, (unsigned long)s
);
1426 static void sci_free_dma(struct uart_port
*port
)
1428 struct sci_port
*s
= to_sci_port(port
);
1434 sci_tx_dma_release(s
, false);
1436 sci_rx_dma_release(s
, false);
1440 static int sci_startup(struct uart_port
*port
)
1442 struct sci_port
*s
= to_sci_port(port
);
1444 dev_dbg(port
->dev
, "%s(%d)\n", __func__
, port
->line
);
1450 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1451 sci_request_dma(port
);
1459 static void sci_shutdown(struct uart_port
*port
)
1461 struct sci_port
*s
= to_sci_port(port
);
1463 dev_dbg(port
->dev
, "%s(%d)\n", __func__
, port
->line
);
1467 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1476 static unsigned int sci_scbrr_calc(unsigned int algo_id
, unsigned int bps
,
1481 return ((freq
+ 16 * bps
) / (16 * bps
) - 1);
1483 return ((freq
+ 16 * bps
) / (32 * bps
) - 1);
1485 return (((freq
* 2) + 16 * bps
) / (16 * bps
) - 1);
1487 return (((freq
* 2) + 16 * bps
) / (32 * bps
) - 1);
1489 return (((freq
* 1000 / 32) / bps
) - 1);
1492 /* Warn, but use a safe default */
1494 return ((freq
+ 16 * bps
) / (32 * bps
) - 1);
1497 static void sci_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
1498 struct ktermios
*old
)
1500 struct sci_port
*s
= to_sci_port(port
);
1501 unsigned int status
, baud
, smr_val
, max_baud
;
1506 * earlyprintk comes here early on with port->uartclk set to zero.
1507 * the clock framework is not up and running at this point so here
1508 * we assume that 115200 is the maximum baud rate. please note that
1509 * the baud rate is not programmed during earlyprintk - it is assumed
1510 * that the previous boot loader has enabled required clocks and
1511 * setup the baud rate generator hardware for us already.
1513 max_baud
= port
->uartclk
? port
->uartclk
/ 16 : 115200;
1515 baud
= uart_get_baud_rate(port
, termios
, old
, 0, max_baud
);
1516 if (likely(baud
&& port
->uartclk
))
1517 t
= sci_scbrr_calc(s
->scbrr_algo_id
, baud
, port
->uartclk
);
1520 status
= sci_in(port
, SCxSR
);
1521 } while (!(status
& SCxSR_TEND(port
)));
1523 sci_out(port
, SCSCR
, 0x00); /* TE=0, RE=0, CKE1=0 */
1525 if (port
->type
!= PORT_SCI
)
1526 sci_out(port
, SCFCR
, scfcr
| SCFCR_RFRST
| SCFCR_TFRST
);
1528 smr_val
= sci_in(port
, SCSMR
) & 3;
1529 if ((termios
->c_cflag
& CSIZE
) == CS7
)
1531 if (termios
->c_cflag
& PARENB
)
1533 if (termios
->c_cflag
& PARODD
)
1535 if (termios
->c_cflag
& CSTOPB
)
1538 uart_update_timeout(port
, termios
->c_cflag
, baud
);
1540 sci_out(port
, SCSMR
, smr_val
);
1542 dev_dbg(port
->dev
, "%s: SMR %x, t %x, SCSCR %x\n", __func__
, smr_val
, t
,
1547 sci_out(port
, SCSMR
, (sci_in(port
, SCSMR
) & ~3) | 1);
1550 sci_out(port
, SCSMR
, sci_in(port
, SCSMR
) & ~3);
1552 sci_out(port
, SCBRR
, t
);
1553 udelay((1000000+(baud
-1)) / baud
); /* Wait one bit interval */
1556 sci_init_pins(port
, termios
->c_cflag
);
1557 sci_out(port
, SCFCR
, scfcr
| ((termios
->c_cflag
& CRTSCTS
) ? SCFCR_MCE
: 0));
1559 sci_out(port
, SCSCR
, s
->scscr
);
1561 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1563 * Calculate delay for 1.5 DMA buffers: see
1564 * drivers/serial/serial_core.c::uart_update_timeout(). With 10 bits
1565 * (CS8), 250Hz, 115200 baud and 64 bytes FIFO, the above function
1566 * calculates 1 jiffie for the data plus 5 jiffies for the "slop(e)."
1567 * Then below we calculate 3 jiffies (12ms) for 1.5 DMA buffers (3 FIFO
1568 * sizes), but it has been found out experimentally, that this is not
1569 * enough: the driver too often needlessly runs on a DMA timeout. 20ms
1570 * as a minimum seem to work perfectly.
1573 s
->rx_timeout
= (port
->timeout
- HZ
/ 50) * s
->buf_len_rx
* 3 /
1576 "DMA Rx t-out %ums, tty t-out %u jiffies\n",
1577 s
->rx_timeout
* 1000 / HZ
, port
->timeout
);
1578 if (s
->rx_timeout
< msecs_to_jiffies(20))
1579 s
->rx_timeout
= msecs_to_jiffies(20);
1583 if ((termios
->c_cflag
& CREAD
) != 0)
1587 static const char *sci_type(struct uart_port
*port
)
1589 switch (port
->type
) {
1605 static void sci_release_port(struct uart_port
*port
)
1607 /* Nothing here yet .. */
1610 static int sci_request_port(struct uart_port
*port
)
1612 /* Nothing here yet .. */
1616 static void sci_config_port(struct uart_port
*port
, int flags
)
1618 struct sci_port
*s
= to_sci_port(port
);
1620 port
->type
= s
->type
;
1625 if (port
->flags
& UPF_IOREMAP
) {
1626 port
->membase
= ioremap_nocache(port
->mapbase
, 0x40);
1628 if (IS_ERR(port
->membase
))
1629 dev_err(port
->dev
, "can't remap port#%d\n", port
->line
);
1632 * For the simple (and majority of) cases where we don't
1633 * need to do any remapping, just cast the cookie
1636 port
->membase
= (void __iomem
*)port
->mapbase
;
1640 static int sci_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
1642 struct sci_port
*s
= to_sci_port(port
);
1644 if (ser
->irq
!= s
->irqs
[SCIx_TXI_IRQ
] || ser
->irq
> nr_irqs
)
1646 if (ser
->baud_base
< 2400)
1647 /* No paper tape reader for Mitch.. */
1653 static struct uart_ops sci_uart_ops
= {
1654 .tx_empty
= sci_tx_empty
,
1655 .set_mctrl
= sci_set_mctrl
,
1656 .get_mctrl
= sci_get_mctrl
,
1657 .start_tx
= sci_start_tx
,
1658 .stop_tx
= sci_stop_tx
,
1659 .stop_rx
= sci_stop_rx
,
1660 .enable_ms
= sci_enable_ms
,
1661 .break_ctl
= sci_break_ctl
,
1662 .startup
= sci_startup
,
1663 .shutdown
= sci_shutdown
,
1664 .set_termios
= sci_set_termios
,
1666 .release_port
= sci_release_port
,
1667 .request_port
= sci_request_port
,
1668 .config_port
= sci_config_port
,
1669 .verify_port
= sci_verify_port
,
1670 #ifdef CONFIG_CONSOLE_POLL
1671 .poll_get_char
= sci_poll_get_char
,
1672 .poll_put_char
= sci_poll_put_char
,
1676 static int __devinit
sci_init_single(struct platform_device
*dev
,
1677 struct sci_port
*sci_port
,
1679 struct plat_sci_port
*p
)
1681 struct uart_port
*port
= &sci_port
->port
;
1683 port
->ops
= &sci_uart_ops
;
1684 port
->iotype
= UPIO_MEM
;
1689 port
->fifosize
= 256;
1692 port
->fifosize
= 64;
1695 port
->fifosize
= 16;
1703 sci_port
->iclk
= clk_get(&dev
->dev
, "sci_ick");
1704 if (IS_ERR(sci_port
->iclk
)) {
1705 sci_port
->iclk
= clk_get(&dev
->dev
, "peripheral_clk");
1706 if (IS_ERR(sci_port
->iclk
)) {
1707 dev_err(&dev
->dev
, "can't get iclk\n");
1708 return PTR_ERR(sci_port
->iclk
);
1713 * The function clock is optional, ignore it if we can't
1716 sci_port
->fclk
= clk_get(&dev
->dev
, "sci_fck");
1717 if (IS_ERR(sci_port
->fclk
))
1718 sci_port
->fclk
= NULL
;
1720 sci_port
->enable
= sci_clk_enable
;
1721 sci_port
->disable
= sci_clk_disable
;
1722 port
->dev
= &dev
->dev
;
1725 sci_port
->break_timer
.data
= (unsigned long)sci_port
;
1726 sci_port
->break_timer
.function
= sci_break_timer
;
1727 init_timer(&sci_port
->break_timer
);
1729 port
->mapbase
= p
->mapbase
;
1730 port
->membase
= p
->membase
;
1732 port
->irq
= p
->irqs
[SCIx_TXI_IRQ
];
1733 port
->flags
= p
->flags
;
1734 sci_port
->type
= port
->type
= p
->type
;
1735 sci_port
->scscr
= p
->scscr
;
1736 sci_port
->scbrr_algo_id
= p
->scbrr_algo_id
;
1738 #ifdef CONFIG_SERIAL_SH_SCI_DMA
1739 sci_port
->dma_dev
= p
->dma_dev
;
1740 sci_port
->slave_tx
= p
->dma_slave_tx
;
1741 sci_port
->slave_rx
= p
->dma_slave_rx
;
1743 dev_dbg(port
->dev
, "%s: DMA device %p, tx %d, rx %d\n", __func__
,
1744 p
->dma_dev
, p
->dma_slave_tx
, p
->dma_slave_rx
);
1747 memcpy(&sci_port
->irqs
, &p
->irqs
, sizeof(p
->irqs
));
1751 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1752 static struct tty_driver
*serial_console_device(struct console
*co
, int *index
)
1754 struct uart_driver
*p
= &sci_uart_driver
;
1756 return p
->tty_driver
;
1759 static void serial_console_putchar(struct uart_port
*port
, int ch
)
1761 sci_poll_put_char(port
, ch
);
1765 * Print a string to the serial port trying not to disturb
1766 * any possible real use of the port...
1768 static void serial_console_write(struct console
*co
, const char *s
,
1771 struct uart_port
*port
= co
->data
;
1772 struct sci_port
*sci_port
= to_sci_port(port
);
1773 unsigned short bits
;
1775 if (sci_port
->enable
)
1776 sci_port
->enable(port
);
1778 uart_console_write(port
, s
, count
, serial_console_putchar
);
1780 /* wait until fifo is empty and last bit has been transmitted */
1781 bits
= SCxSR_TDxE(port
) | SCxSR_TEND(port
);
1782 while ((sci_in(port
, SCxSR
) & bits
) != bits
)
1785 if (sci_port
->disable
)
1786 sci_port
->disable(port
);
1789 static int __devinit
serial_console_setup(struct console
*co
, char *options
)
1791 struct sci_port
*sci_port
;
1792 struct uart_port
*port
;
1800 * Check whether an invalid uart number has been specified, and
1801 * if so, search for the first available port that does have
1804 if (co
->index
>= SCI_NPORTS
)
1809 sci_port
= to_sci_port(port
);
1811 sci_port
= &sci_ports
[co
->index
];
1812 port
= &sci_port
->port
;
1817 * Also need to check port->type, we don't actually have any
1818 * UPIO_PORT ports, but uart_report_port() handily misreports
1819 * it anyways if we don't have a port available by the time this is
1825 sci_config_port(port
, 0);
1827 if (sci_port
->enable
)
1828 sci_port
->enable(port
);
1831 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1833 ret
= uart_set_options(port
, co
, baud
, parity
, bits
, flow
);
1834 #if defined(__H8300H__) || defined(__H8300S__)
1835 /* disable rx interrupt */
1839 /* TODO: disable clock */
1843 static struct console serial_console
= {
1845 .device
= serial_console_device
,
1846 .write
= serial_console_write
,
1847 .setup
= serial_console_setup
,
1848 .flags
= CON_PRINTBUFFER
,
1852 static int __init
sci_console_init(void)
1854 register_console(&serial_console
);
1857 console_initcall(sci_console_init
);
1859 static struct sci_port early_serial_port
;
1860 static struct console early_serial_console
= {
1861 .name
= "early_ttySC",
1862 .write
= serial_console_write
,
1863 .flags
= CON_PRINTBUFFER
,
1865 static char early_serial_buf
[32];
1867 #endif /* CONFIG_SERIAL_SH_SCI_CONSOLE */
1869 #if defined(CONFIG_SERIAL_SH_SCI_CONSOLE)
1870 #define SCI_CONSOLE (&serial_console)
1872 #define SCI_CONSOLE 0
1875 static char banner
[] __initdata
=
1876 KERN_INFO
"SuperH SCI(F) driver initialized\n";
1878 static struct uart_driver sci_uart_driver
= {
1879 .owner
= THIS_MODULE
,
1880 .driver_name
= "sci",
1881 .dev_name
= "ttySC",
1883 .minor
= SCI_MINOR_START
,
1885 .cons
= SCI_CONSOLE
,
1889 static int sci_remove(struct platform_device
*dev
)
1891 struct sh_sci_priv
*priv
= platform_get_drvdata(dev
);
1893 unsigned long flags
;
1895 cpufreq_unregister_notifier(&priv
->clk_nb
, CPUFREQ_TRANSITION_NOTIFIER
);
1897 spin_lock_irqsave(&priv
->lock
, flags
);
1898 list_for_each_entry(p
, &priv
->ports
, node
) {
1899 uart_remove_one_port(&sci_uart_driver
, &p
->port
);
1903 spin_unlock_irqrestore(&priv
->lock
, flags
);
1909 static int __devinit
sci_probe_single(struct platform_device
*dev
,
1911 struct plat_sci_port
*p
,
1912 struct sci_port
*sciport
)
1914 struct sh_sci_priv
*priv
= platform_get_drvdata(dev
);
1915 unsigned long flags
;
1919 if (unlikely(index
>= SCI_NPORTS
)) {
1920 dev_notice(&dev
->dev
, "Attempting to register port "
1921 "%d when only %d are available.\n",
1922 index
+1, SCI_NPORTS
);
1923 dev_notice(&dev
->dev
, "Consider bumping "
1924 "CONFIG_SERIAL_SH_SCI_NR_UARTS!\n");
1928 ret
= sci_init_single(dev
, sciport
, index
, p
);
1932 ret
= uart_add_one_port(&sci_uart_driver
, &sciport
->port
);
1936 INIT_LIST_HEAD(&sciport
->node
);
1938 spin_lock_irqsave(&priv
->lock
, flags
);
1939 list_add(&sciport
->node
, &priv
->ports
);
1940 spin_unlock_irqrestore(&priv
->lock
, flags
);
1946 * Register a set of serial devices attached to a platform device. The
1947 * list is terminated with a zero flags entry, which means we expect
1948 * all entries to have at least UPF_BOOT_AUTOCONF set. Platforms that need
1949 * remapping (such as sh64) should also set UPF_IOREMAP.
1951 static int __devinit
sci_probe(struct platform_device
*dev
)
1953 struct plat_sci_port
*p
= dev
->dev
.platform_data
;
1954 struct sh_sci_priv
*priv
;
1955 int i
, ret
= -EINVAL
;
1957 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
1958 if (is_early_platform_device(dev
)) {
1961 early_serial_console
.index
= dev
->id
;
1962 early_serial_console
.data
= &early_serial_port
.port
;
1963 sci_init_single(NULL
, &early_serial_port
, dev
->id
, p
);
1964 serial_console_setup(&early_serial_console
, early_serial_buf
);
1965 if (!strstr(early_serial_buf
, "keep"))
1966 early_serial_console
.flags
|= CON_BOOT
;
1967 register_console(&early_serial_console
);
1972 priv
= kzalloc(sizeof(*priv
), GFP_KERNEL
);
1976 INIT_LIST_HEAD(&priv
->ports
);
1977 spin_lock_init(&priv
->lock
);
1978 platform_set_drvdata(dev
, priv
);
1980 priv
->clk_nb
.notifier_call
= sci_notifier
;
1981 cpufreq_register_notifier(&priv
->clk_nb
, CPUFREQ_TRANSITION_NOTIFIER
);
1983 if (dev
->id
!= -1) {
1984 ret
= sci_probe_single(dev
, dev
->id
, p
, &sci_ports
[dev
->id
]);
1988 for (i
= 0; p
&& p
->flags
!= 0; p
++, i
++) {
1989 ret
= sci_probe_single(dev
, i
, p
, &sci_ports
[i
]);
1995 #ifdef CONFIG_SH_STANDARD_BIOS
1996 sh_bios_gdb_detach();
2006 static int sci_suspend(struct device
*dev
)
2008 struct sh_sci_priv
*priv
= dev_get_drvdata(dev
);
2010 unsigned long flags
;
2012 spin_lock_irqsave(&priv
->lock
, flags
);
2013 list_for_each_entry(p
, &priv
->ports
, node
)
2014 uart_suspend_port(&sci_uart_driver
, &p
->port
);
2015 spin_unlock_irqrestore(&priv
->lock
, flags
);
2020 static int sci_resume(struct device
*dev
)
2022 struct sh_sci_priv
*priv
= dev_get_drvdata(dev
);
2024 unsigned long flags
;
2026 spin_lock_irqsave(&priv
->lock
, flags
);
2027 list_for_each_entry(p
, &priv
->ports
, node
)
2028 uart_resume_port(&sci_uart_driver
, &p
->port
);
2029 spin_unlock_irqrestore(&priv
->lock
, flags
);
2034 static const struct dev_pm_ops sci_dev_pm_ops
= {
2035 .suspend
= sci_suspend
,
2036 .resume
= sci_resume
,
2039 static struct platform_driver sci_driver
= {
2041 .remove
= sci_remove
,
2044 .owner
= THIS_MODULE
,
2045 .pm
= &sci_dev_pm_ops
,
2049 static int __init
sci_init(void)
2055 ret
= uart_register_driver(&sci_uart_driver
);
2056 if (likely(ret
== 0)) {
2057 ret
= platform_driver_register(&sci_driver
);
2059 uart_unregister_driver(&sci_uart_driver
);
2065 static void __exit
sci_exit(void)
2067 platform_driver_unregister(&sci_driver
);
2068 uart_unregister_driver(&sci_uart_driver
);
2071 #ifdef CONFIG_SERIAL_SH_SCI_CONSOLE
2072 early_platform_init_buffer("earlyprintk", &sci_driver
,
2073 early_serial_buf
, ARRAY_SIZE(early_serial_buf
));
2075 module_init(sci_init
);
2076 module_exit(sci_exit
);
2078 MODULE_LICENSE("GPL");
2079 MODULE_ALIAS("platform:sh-sci");