2 * Blackfin On-Chip Serial Driver
4 * Copyright 2006-2008 Analog Devices Inc.
6 * Enter bugs at http://blackfin.uclinux.org/
8 * Licensed under the GPL-2 or later.
11 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
15 #include <linux/module.h>
16 #include <linux/ioport.h>
17 #include <linux/init.h>
18 #include <linux/console.h>
19 #include <linux/sysrq.h>
20 #include <linux/platform_device.h>
21 #include <linux/tty.h>
22 #include <linux/tty_flip.h>
23 #include <linux/serial_core.h>
25 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
26 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
27 #include <linux/kgdb.h>
28 #include <asm/irq_regs.h>
32 #include <mach/bfin_serial_5xx.h>
34 #ifdef CONFIG_SERIAL_BFIN_DMA
35 #include <linux/dma-mapping.h>
38 #include <asm/cacheflush.h>
41 #ifdef CONFIG_SERIAL_BFIN_MODULE
42 # undef CONFIG_EARLY_PRINTK
45 /* UART name and device definitions */
46 #define BFIN_SERIAL_NAME "ttyBF"
47 #define BFIN_SERIAL_MAJOR 204
48 #define BFIN_SERIAL_MINOR 64
50 static struct bfin_serial_port bfin_serial_ports
[BFIN_UART_NR_PORTS
];
51 static int nr_active_ports
= ARRAY_SIZE(bfin_serial_resource
);
53 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
54 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
56 # ifndef CONFIG_SERIAL_BFIN_PIO
57 # error KGDB only support UART in PIO mode.
60 static int kgdboc_port_line
;
61 static int kgdboc_break_enabled
;
64 * Setup for console. Argument comes from the menuconfig
66 #define DMA_RX_XCOUNT 512
67 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
69 #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
71 #ifdef CONFIG_SERIAL_BFIN_DMA
72 static void bfin_serial_dma_tx_chars(struct bfin_serial_port
*uart
);
74 static void bfin_serial_tx_chars(struct bfin_serial_port
*uart
);
77 static void bfin_serial_reset_irda(struct uart_port
*port
);
79 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
80 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
81 static unsigned int bfin_serial_get_mctrl(struct uart_port
*port
)
83 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
84 if (uart
->cts_pin
< 0)
85 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
87 /* CTS PIN is negative assertive. */
88 if (UART_GET_CTS(uart
))
89 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
91 return TIOCM_DSR
| TIOCM_CAR
;
94 static void bfin_serial_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
96 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
97 if (uart
->rts_pin
< 0)
100 /* RTS PIN is negative assertive. */
101 if (mctrl
& TIOCM_RTS
)
102 UART_ENABLE_RTS(uart
);
104 UART_DISABLE_RTS(uart
);
108 * Handle any change of modem status signal.
110 static irqreturn_t
bfin_serial_mctrl_cts_int(int irq
, void *dev_id
)
112 struct bfin_serial_port
*uart
= dev_id
;
115 status
= bfin_serial_get_mctrl(&uart
->port
);
116 uart_handle_cts_change(&uart
->port
, status
& TIOCM_CTS
);
117 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
119 UART_CLEAR_SCTS(uart
);
120 UART_CLEAR_IER(uart
, EDSSI
);
126 static unsigned int bfin_serial_get_mctrl(struct uart_port
*port
)
128 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
131 static void bfin_serial_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
137 * interrupts are disabled on entry
139 static void bfin_serial_stop_tx(struct uart_port
*port
)
141 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
142 #ifdef CONFIG_SERIAL_BFIN_DMA
143 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
146 while (!(UART_GET_LSR(uart
) & TEMT
))
149 #ifdef CONFIG_SERIAL_BFIN_DMA
150 disable_dma(uart
->tx_dma_channel
);
151 xmit
->tail
= (xmit
->tail
+ uart
->tx_count
) & (UART_XMIT_SIZE
- 1);
152 uart
->port
.icount
.tx
+= uart
->tx_count
;
158 UART_PUT_LSR(uart
, TFI
);
160 UART_CLEAR_IER(uart
, ETBEI
);
165 * port is locked and interrupts are disabled
167 static void bfin_serial_start_tx(struct uart_port
*port
)
169 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
170 struct tty_struct
*tty
= uart
->port
.info
->port
.tty
;
172 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
173 if (uart
->scts
&& !(bfin_serial_get_mctrl(&uart
->port
) & TIOCM_CTS
)) {
175 uart_handle_cts_change(&uart
->port
, uart
->scts
);
180 * To avoid losting RX interrupt, we reset IR function
181 * before sending data.
183 if (tty
->termios
->c_line
== N_IRDA
)
184 bfin_serial_reset_irda(port
);
186 #ifdef CONFIG_SERIAL_BFIN_DMA
188 bfin_serial_dma_tx_chars(uart
);
190 UART_SET_IER(uart
, ETBEI
);
191 bfin_serial_tx_chars(uart
);
196 * Interrupts are enabled
198 static void bfin_serial_stop_rx(struct uart_port
*port
)
200 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
202 UART_CLEAR_IER(uart
, ERBFI
);
206 * Set the modem control timer to fire immediately.
208 static void bfin_serial_enable_ms(struct uart_port
*port
)
213 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
214 # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
215 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
217 # define UART_GET_ANOMALY_THRESHOLD(uart) 0
218 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
221 #ifdef CONFIG_SERIAL_BFIN_PIO
222 static void bfin_serial_rx_chars(struct bfin_serial_port
*uart
)
224 struct tty_struct
*tty
= NULL
;
225 unsigned int status
, ch
, flg
;
226 static struct timeval anomaly_start
= { .tv_sec
= 0 };
228 status
= UART_GET_LSR(uart
);
229 UART_CLEAR_LSR(uart
);
231 ch
= UART_GET_CHAR(uart
);
232 uart
->port
.icount
.rx
++;
234 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
235 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
236 if (kgdb_connected
&& kgdboc_port_line
== uart
->port
.line
)
237 if (ch
== 0x3) {/* Ctrl + C */
242 if (!uart
->port
.info
|| !uart
->port
.info
->port
.tty
)
245 tty
= uart
->port
.info
->port
.tty
;
247 if (ANOMALY_05000363
) {
248 /* The BF533 (and BF561) family of processors have a nice anomaly
249 * where they continuously generate characters for a "single" break.
250 * We have to basically ignore this flood until the "next" valid
251 * character comes across. Due to the nature of the flood, it is
252 * not possible to reliably catch bytes that are sent too quickly
253 * after this break. So application code talking to the Blackfin
254 * which sends a break signal must allow at least 1.5 character
255 * times after the end of the break for things to stabilize. This
256 * timeout was picked as it must absolutely be larger than 1
257 * character time +/- some percent. So 1.5 sounds good. All other
258 * Blackfin families operate properly. Woo.
260 if (anomaly_start
.tv_sec
) {
264 if ((~ch
& (~ch
+ 1)) & 0xff)
265 goto known_good_char
;
267 do_gettimeofday(&curr
);
268 if (curr
.tv_sec
- anomaly_start
.tv_sec
> 1)
269 goto known_good_char
;
272 if (curr
.tv_sec
!= anomaly_start
.tv_sec
)
273 usecs
+= USEC_PER_SEC
;
274 usecs
+= curr
.tv_usec
- anomaly_start
.tv_usec
;
276 if (usecs
> UART_GET_ANOMALY_THRESHOLD(uart
))
277 goto known_good_char
;
280 anomaly_start
.tv_sec
= 0;
282 anomaly_start
= curr
;
288 anomaly_start
.tv_sec
= 0;
293 if (ANOMALY_05000363
)
294 if (bfin_revid() < 5)
295 do_gettimeofday(&anomaly_start
);
296 uart
->port
.icount
.brk
++;
297 if (uart_handle_break(&uart
->port
))
299 status
&= ~(PE
| FE
);
302 uart
->port
.icount
.parity
++;
304 uart
->port
.icount
.overrun
++;
306 uart
->port
.icount
.frame
++;
308 status
&= uart
->port
.read_status_mask
;
312 else if (status
& PE
)
314 else if (status
& FE
)
319 if (uart_handle_sysrq_char(&uart
->port
, ch
))
322 uart_insert_char(&uart
->port
, status
, OE
, ch
, flg
);
325 tty_flip_buffer_push(tty
);
328 static void bfin_serial_tx_chars(struct bfin_serial_port
*uart
)
330 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
332 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uart
->port
)) {
335 UART_PUT_LSR(uart
, TFI
);
337 UART_CLEAR_IER(uart
, ETBEI
);
341 if (uart
->port
.x_char
) {
342 UART_PUT_CHAR(uart
, uart
->port
.x_char
);
343 uart
->port
.icount
.tx
++;
344 uart
->port
.x_char
= 0;
347 while ((UART_GET_LSR(uart
) & THRE
) && xmit
->tail
!= xmit
->head
) {
348 UART_PUT_CHAR(uart
, xmit
->buf
[xmit
->tail
]);
349 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
350 uart
->port
.icount
.tx
++;
354 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
355 uart_write_wakeup(&uart
->port
);
358 static irqreturn_t
bfin_serial_rx_int(int irq
, void *dev_id
)
360 struct bfin_serial_port
*uart
= dev_id
;
362 spin_lock(&uart
->port
.lock
);
363 while (UART_GET_LSR(uart
) & DR
)
364 bfin_serial_rx_chars(uart
);
365 spin_unlock(&uart
->port
.lock
);
370 static irqreturn_t
bfin_serial_tx_int(int irq
, void *dev_id
)
372 struct bfin_serial_port
*uart
= dev_id
;
374 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
375 if (uart
->scts
&& !(bfin_serial_get_mctrl(&uart
->port
) & TIOCM_CTS
)) {
377 uart_handle_cts_change(&uart
->port
, uart
->scts
);
380 spin_lock(&uart
->port
.lock
);
381 if (UART_GET_LSR(uart
) & THRE
)
382 bfin_serial_tx_chars(uart
);
383 spin_unlock(&uart
->port
.lock
);
389 #ifdef CONFIG_SERIAL_BFIN_DMA
390 static void bfin_serial_dma_tx_chars(struct bfin_serial_port
*uart
)
392 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
396 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uart
->port
)) {
402 if (uart
->port
.x_char
) {
403 UART_PUT_CHAR(uart
, uart
->port
.x_char
);
404 uart
->port
.icount
.tx
++;
405 uart
->port
.x_char
= 0;
408 uart
->tx_count
= CIRC_CNT(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
409 if (uart
->tx_count
> (UART_XMIT_SIZE
- xmit
->tail
))
410 uart
->tx_count
= UART_XMIT_SIZE
- xmit
->tail
;
411 blackfin_dcache_flush_range((unsigned long)(xmit
->buf
+xmit
->tail
),
412 (unsigned long)(xmit
->buf
+xmit
->tail
+uart
->tx_count
));
413 set_dma_config(uart
->tx_dma_channel
,
414 set_bfin_dma_config(DIR_READ
, DMA_FLOW_STOP
,
419 set_dma_start_addr(uart
->tx_dma_channel
, (unsigned long)(xmit
->buf
+xmit
->tail
));
420 set_dma_x_count(uart
->tx_dma_channel
, uart
->tx_count
);
421 set_dma_x_modify(uart
->tx_dma_channel
, 1);
422 enable_dma(uart
->tx_dma_channel
);
424 UART_SET_IER(uart
, ETBEI
);
427 static void bfin_serial_dma_rx_chars(struct bfin_serial_port
*uart
)
429 struct tty_struct
*tty
= uart
->port
.info
->port
.tty
;
432 status
= UART_GET_LSR(uart
);
433 UART_CLEAR_LSR(uart
);
435 uart
->port
.icount
.rx
+=
436 CIRC_CNT(uart
->rx_dma_buf
.head
, uart
->rx_dma_buf
.tail
,
440 uart
->port
.icount
.brk
++;
441 if (uart_handle_break(&uart
->port
))
442 goto dma_ignore_char
;
443 status
&= ~(PE
| FE
);
446 uart
->port
.icount
.parity
++;
448 uart
->port
.icount
.overrun
++;
450 uart
->port
.icount
.frame
++;
452 status
&= uart
->port
.read_status_mask
;
456 else if (status
& PE
)
458 else if (status
& FE
)
463 for (i
= uart
->rx_dma_buf
.tail
; ; i
++) {
464 if (i
>= UART_XMIT_SIZE
)
466 if (i
== uart
->rx_dma_buf
.head
)
468 if (!uart_handle_sysrq_char(&uart
->port
, uart
->rx_dma_buf
.buf
[i
]))
469 uart_insert_char(&uart
->port
, status
, OE
,
470 uart
->rx_dma_buf
.buf
[i
], flg
);
474 tty_flip_buffer_push(tty
);
477 void bfin_serial_rx_dma_timeout(struct bfin_serial_port
*uart
)
482 spin_lock_irqsave(&uart
->port
.lock
, flags
);
484 uart
->rx_dma_nrows
= get_dma_curr_ycount(uart
->rx_dma_channel
);
485 x_pos
= get_dma_curr_xcount(uart
->rx_dma_channel
);
486 uart
->rx_dma_nrows
= DMA_RX_YCOUNT
- uart
->rx_dma_nrows
;
487 if (uart
->rx_dma_nrows
== DMA_RX_YCOUNT
)
488 uart
->rx_dma_nrows
= 0;
489 x_pos
= DMA_RX_XCOUNT
- x_pos
;
490 if (x_pos
== DMA_RX_XCOUNT
)
493 pos
= uart
->rx_dma_nrows
* DMA_RX_XCOUNT
+ x_pos
;
494 if (pos
!= uart
->rx_dma_buf
.tail
) {
495 uart
->rx_dma_buf
.head
= pos
;
496 bfin_serial_dma_rx_chars(uart
);
497 uart
->rx_dma_buf
.tail
= uart
->rx_dma_buf
.head
;
500 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
502 mod_timer(&(uart
->rx_dma_timer
), jiffies
+ DMA_RX_FLUSH_JIFFIES
);
505 static irqreturn_t
bfin_serial_dma_tx_int(int irq
, void *dev_id
)
507 struct bfin_serial_port
*uart
= dev_id
;
508 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
510 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
511 if (uart
->scts
&& !(bfin_serial_get_mctrl(&uart
->port
)&TIOCM_CTS
)) {
513 uart_handle_cts_change(&uart
->port
, uart
->scts
);
517 spin_lock(&uart
->port
.lock
);
518 if (!(get_dma_curr_irqstat(uart
->tx_dma_channel
)&DMA_RUN
)) {
519 disable_dma(uart
->tx_dma_channel
);
520 clear_dma_irqstat(uart
->tx_dma_channel
);
521 UART_CLEAR_IER(uart
, ETBEI
);
522 xmit
->tail
= (xmit
->tail
+ uart
->tx_count
) & (UART_XMIT_SIZE
- 1);
523 uart
->port
.icount
.tx
+= uart
->tx_count
;
525 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
526 uart_write_wakeup(&uart
->port
);
528 bfin_serial_dma_tx_chars(uart
);
531 spin_unlock(&uart
->port
.lock
);
535 static irqreturn_t
bfin_serial_dma_rx_int(int irq
, void *dev_id
)
537 struct bfin_serial_port
*uart
= dev_id
;
538 unsigned short irqstat
;
540 spin_lock(&uart
->port
.lock
);
541 irqstat
= get_dma_curr_irqstat(uart
->rx_dma_channel
);
542 clear_dma_irqstat(uart
->rx_dma_channel
);
543 bfin_serial_dma_rx_chars(uart
);
544 spin_unlock(&uart
->port
.lock
);
551 * Return TIOCSER_TEMT when transmitter is not busy.
553 static unsigned int bfin_serial_tx_empty(struct uart_port
*port
)
555 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
558 lsr
= UART_GET_LSR(uart
);
565 static void bfin_serial_break_ctl(struct uart_port
*port
, int break_state
)
567 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
568 u16 lcr
= UART_GET_LCR(uart
);
573 UART_PUT_LCR(uart
, lcr
);
577 static int bfin_serial_startup(struct uart_port
*port
)
579 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
581 #ifdef CONFIG_SERIAL_BFIN_DMA
582 dma_addr_t dma_handle
;
584 if (request_dma(uart
->rx_dma_channel
, "BFIN_UART_RX") < 0) {
585 printk(KERN_NOTICE
"Unable to attach Blackfin UART RX DMA channel\n");
589 if (request_dma(uart
->tx_dma_channel
, "BFIN_UART_TX") < 0) {
590 printk(KERN_NOTICE
"Unable to attach Blackfin UART TX DMA channel\n");
591 free_dma(uart
->rx_dma_channel
);
595 set_dma_callback(uart
->rx_dma_channel
, bfin_serial_dma_rx_int
, uart
);
596 set_dma_callback(uart
->tx_dma_channel
, bfin_serial_dma_tx_int
, uart
);
598 uart
->rx_dma_buf
.buf
= (unsigned char *)dma_alloc_coherent(NULL
, PAGE_SIZE
, &dma_handle
, GFP_DMA
);
599 uart
->rx_dma_buf
.head
= 0;
600 uart
->rx_dma_buf
.tail
= 0;
601 uart
->rx_dma_nrows
= 0;
603 set_dma_config(uart
->rx_dma_channel
,
604 set_bfin_dma_config(DIR_WRITE
, DMA_FLOW_AUTO
,
605 INTR_ON_ROW
, DIMENSION_2D
,
608 set_dma_x_count(uart
->rx_dma_channel
, DMA_RX_XCOUNT
);
609 set_dma_x_modify(uart
->rx_dma_channel
, 1);
610 set_dma_y_count(uart
->rx_dma_channel
, DMA_RX_YCOUNT
);
611 set_dma_y_modify(uart
->rx_dma_channel
, 1);
612 set_dma_start_addr(uart
->rx_dma_channel
, (unsigned long)uart
->rx_dma_buf
.buf
);
613 enable_dma(uart
->rx_dma_channel
);
615 uart
->rx_dma_timer
.data
= (unsigned long)(uart
);
616 uart
->rx_dma_timer
.function
= (void *)bfin_serial_rx_dma_timeout
;
617 uart
->rx_dma_timer
.expires
= jiffies
+ DMA_RX_FLUSH_JIFFIES
;
618 add_timer(&(uart
->rx_dma_timer
));
620 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
621 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
622 if (kgdboc_port_line
== uart
->port
.line
&& kgdboc_break_enabled
)
623 kgdboc_break_enabled
= 0;
626 if (request_irq(uart
->port
.irq
, bfin_serial_rx_int
, IRQF_DISABLED
,
627 "BFIN_UART_RX", uart
)) {
628 printk(KERN_NOTICE
"Unable to attach BlackFin UART RX interrupt\n");
633 (uart
->port
.irq
+1, bfin_serial_tx_int
, IRQF_DISABLED
,
634 "BFIN_UART_TX", uart
)) {
635 printk(KERN_NOTICE
"Unable to attach BlackFin UART TX interrupt\n");
636 free_irq(uart
->port
.irq
, uart
);
642 unsigned uart_dma_ch_rx
, uart_dma_ch_tx
;
644 switch (uart
->port
.irq
) {
646 uart_dma_ch_rx
= CH_UART3_RX
;
647 uart_dma_ch_tx
= CH_UART3_TX
;
650 uart_dma_ch_rx
= CH_UART2_RX
;
651 uart_dma_ch_tx
= CH_UART2_TX
;
654 uart_dma_ch_rx
= uart_dma_ch_tx
= 0;
658 if (uart_dma_ch_rx
&&
659 request_dma(uart_dma_ch_rx
, "BFIN_UART_RX") < 0) {
660 printk(KERN_NOTICE
"Fail to attach UART interrupt\n");
661 free_irq(uart
->port
.irq
, uart
);
662 free_irq(uart
->port
.irq
+ 1, uart
);
665 if (uart_dma_ch_tx
&&
666 request_dma(uart_dma_ch_tx
, "BFIN_UART_TX") < 0) {
667 printk(KERN_NOTICE
"Fail to attach UART interrupt\n");
668 free_dma(uart_dma_ch_rx
);
669 free_irq(uart
->port
.irq
, uart
);
670 free_irq(uart
->port
.irq
+ 1, uart
);
675 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
676 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
681 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
682 if (uart
->cts_pin
>= 0) {
683 if (request_irq(gpio_to_irq(uart
->cts_pin
),
684 bfin_serial_mctrl_cts_int
,
685 IRQF_TRIGGER_RISING
| IRQF_TRIGGER_FALLING
|
686 IRQF_DISABLED
, "BFIN_UART_CTS", uart
)) {
688 pr_info("Unable to attach BlackFin UART CTS interrupt.\
692 if (uart
->rts_pin
>= 0) {
693 gpio_request(uart
->rts_pin
, DRIVER_NAME
);
694 gpio_direction_output(uart
->rts_pin
, 0);
697 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
698 if (request_irq(uart
->status_irq
,
699 bfin_serial_mctrl_cts_int
,
700 IRQF_DISABLED
, "BFIN_UART_MODEM_STATUS", uart
)) {
701 pr_info("Unable to attach BlackFin UART Modem \
702 Status interrupt.\n");
705 if (uart
->cts_pin
>= 0) {
706 gpio_request(uart
->cts_pin
, DRIVER_NAME
);
707 gpio_direction_output(uart
->cts_pin
, 1);
709 if (uart
->rts_pin
>= 0) {
710 gpio_request(uart
->rts_pin
, DRIVER_NAME
);
711 gpio_direction_output(uart
->rts_pin
, 0);
714 /* CTS RTS PINs are negative assertive. */
715 UART_PUT_MCR(uart
, ACTS
);
716 UART_SET_IER(uart
, EDSSI
);
719 UART_SET_IER(uart
, ERBFI
);
723 static void bfin_serial_shutdown(struct uart_port
*port
)
725 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
727 #ifdef CONFIG_SERIAL_BFIN_DMA
728 disable_dma(uart
->tx_dma_channel
);
729 free_dma(uart
->tx_dma_channel
);
730 disable_dma(uart
->rx_dma_channel
);
731 free_dma(uart
->rx_dma_channel
);
732 del_timer(&(uart
->rx_dma_timer
));
733 dma_free_coherent(NULL
, PAGE_SIZE
, uart
->rx_dma_buf
.buf
, 0);
736 switch (uart
->port
.irq
) {
738 free_dma(CH_UART3_RX
);
739 free_dma(CH_UART3_TX
);
742 free_dma(CH_UART2_RX
);
743 free_dma(CH_UART2_TX
);
749 free_irq(uart
->port
.irq
, uart
);
750 free_irq(uart
->port
.irq
+1, uart
);
753 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
754 if (uart
->cts_pin
>= 0)
755 free_irq(gpio_to_irq(uart
->cts_pin
), uart
);
756 if (uart
->rts_pin
>= 0)
757 gpio_free(uart
->rts_pin
);
759 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
760 if (uart
->cts_pin
>= 0)
761 gpio_free(uart
->cts_pin
);
762 if (uart
->rts_pin
>= 0)
763 gpio_free(uart
->rts_pin
);
764 if (UART_GET_IER(uart
) && EDSSI
)
765 free_irq(uart
->status_irq
, uart
);
770 bfin_serial_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
771 struct ktermios
*old
)
773 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
775 unsigned int baud
, quot
;
776 unsigned short val
, ier
, lcr
= 0;
778 switch (termios
->c_cflag
& CSIZE
) {
792 printk(KERN_ERR
"%s: word lengh not supported\n",
796 if (termios
->c_cflag
& CSTOPB
)
798 if (termios
->c_cflag
& PARENB
)
800 if (!(termios
->c_cflag
& PARODD
))
802 if (termios
->c_cflag
& CMSPAR
)
805 port
->read_status_mask
= OE
;
806 if (termios
->c_iflag
& INPCK
)
807 port
->read_status_mask
|= (FE
| PE
);
808 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
809 port
->read_status_mask
|= BI
;
812 * Characters to ignore
814 port
->ignore_status_mask
= 0;
815 if (termios
->c_iflag
& IGNPAR
)
816 port
->ignore_status_mask
|= FE
| PE
;
817 if (termios
->c_iflag
& IGNBRK
) {
818 port
->ignore_status_mask
|= BI
;
820 * If we're ignoring parity and break indicators,
821 * ignore overruns too (for real raw support).
823 if (termios
->c_iflag
& IGNPAR
)
824 port
->ignore_status_mask
|= OE
;
827 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
828 quot
= uart_get_divisor(port
, baud
) - ANOMALY_05000230
;
829 spin_lock_irqsave(&uart
->port
.lock
, flags
);
831 UART_SET_ANOMALY_THRESHOLD(uart
, USEC_PER_SEC
/ baud
* 15);
834 ier
= UART_GET_IER(uart
);
835 UART_DISABLE_INTS(uart
);
837 /* Set DLAB in LCR to Access DLL and DLH */
840 UART_PUT_DLL(uart
, quot
& 0xFF);
841 UART_PUT_DLH(uart
, (quot
>> 8) & 0xFF);
844 /* Clear DLAB in LCR to Access THR RBR IER */
845 UART_CLEAR_DLAB(uart
);
847 UART_PUT_LCR(uart
, lcr
);
850 UART_ENABLE_INTS(uart
, ier
);
852 val
= UART_GET_GCTL(uart
);
854 UART_PUT_GCTL(uart
, val
);
856 /* Port speed changed, update the per-port timeout. */
857 uart_update_timeout(port
, termios
->c_cflag
, baud
);
859 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
862 static const char *bfin_serial_type(struct uart_port
*port
)
864 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
866 return uart
->port
.type
== PORT_BFIN
? "BFIN-UART" : NULL
;
870 * Release the memory region(s) being used by 'port'.
872 static void bfin_serial_release_port(struct uart_port
*port
)
877 * Request the memory region(s) being used by 'port'.
879 static int bfin_serial_request_port(struct uart_port
*port
)
885 * Configure/autoconfigure the port.
887 static void bfin_serial_config_port(struct uart_port
*port
, int flags
)
889 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
891 if (flags
& UART_CONFIG_TYPE
&&
892 bfin_serial_request_port(&uart
->port
) == 0)
893 uart
->port
.type
= PORT_BFIN
;
897 * Verify the new serial_struct (for TIOCSSERIAL).
898 * The only change we allow are to the flags and type, and
899 * even then only between PORT_BFIN and PORT_UNKNOWN
902 bfin_serial_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
908 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
909 * In other cases, disable IrDA function.
911 static void bfin_serial_set_ldisc(struct uart_port
*port
)
913 int line
= port
->line
;
916 if (line
>= port
->info
->port
.tty
->driver
->num
)
919 switch (port
->info
->port
.tty
->termios
->c_line
) {
921 val
= UART_GET_GCTL(&bfin_serial_ports
[line
]);
922 val
|= (IREN
| RPOLC
);
923 UART_PUT_GCTL(&bfin_serial_ports
[line
], val
);
926 val
= UART_GET_GCTL(&bfin_serial_ports
[line
]);
927 val
&= ~(IREN
| RPOLC
);
928 UART_PUT_GCTL(&bfin_serial_ports
[line
], val
);
932 static void bfin_serial_reset_irda(struct uart_port
*port
)
934 int line
= port
->line
;
937 val
= UART_GET_GCTL(&bfin_serial_ports
[line
]);
938 val
&= ~(IREN
| RPOLC
);
939 UART_PUT_GCTL(&bfin_serial_ports
[line
], val
);
941 val
|= (IREN
| RPOLC
);
942 UART_PUT_GCTL(&bfin_serial_ports
[line
], val
);
946 #ifdef CONFIG_CONSOLE_POLL
947 static void bfin_serial_poll_put_char(struct uart_port
*port
, unsigned char chr
)
949 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
951 while (!(UART_GET_LSR(uart
) & THRE
))
954 UART_CLEAR_DLAB(uart
);
955 UART_PUT_CHAR(uart
, (unsigned char)chr
);
958 static int bfin_serial_poll_get_char(struct uart_port
*port
)
960 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
963 while (!(UART_GET_LSR(uart
) & DR
))
966 UART_CLEAR_DLAB(uart
);
967 chr
= UART_GET_CHAR(uart
);
973 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
974 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
975 static void bfin_kgdboc_port_shutdown(struct uart_port
*port
)
977 if (kgdboc_break_enabled
) {
978 kgdboc_break_enabled
= 0;
979 bfin_serial_shutdown(port
);
983 static int bfin_kgdboc_port_startup(struct uart_port
*port
)
985 kgdboc_port_line
= port
->line
;
986 kgdboc_break_enabled
= !bfin_serial_startup(port
);
991 static struct uart_ops bfin_serial_pops
= {
992 .tx_empty
= bfin_serial_tx_empty
,
993 .set_mctrl
= bfin_serial_set_mctrl
,
994 .get_mctrl
= bfin_serial_get_mctrl
,
995 .stop_tx
= bfin_serial_stop_tx
,
996 .start_tx
= bfin_serial_start_tx
,
997 .stop_rx
= bfin_serial_stop_rx
,
998 .enable_ms
= bfin_serial_enable_ms
,
999 .break_ctl
= bfin_serial_break_ctl
,
1000 .startup
= bfin_serial_startup
,
1001 .shutdown
= bfin_serial_shutdown
,
1002 .set_termios
= bfin_serial_set_termios
,
1003 .set_ldisc
= bfin_serial_set_ldisc
,
1004 .type
= bfin_serial_type
,
1005 .release_port
= bfin_serial_release_port
,
1006 .request_port
= bfin_serial_request_port
,
1007 .config_port
= bfin_serial_config_port
,
1008 .verify_port
= bfin_serial_verify_port
,
1009 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1010 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1011 .kgdboc_port_startup
= bfin_kgdboc_port_startup
,
1012 .kgdboc_port_shutdown
= bfin_kgdboc_port_shutdown
,
1014 #ifdef CONFIG_CONSOLE_POLL
1015 .poll_put_char
= bfin_serial_poll_put_char
,
1016 .poll_get_char
= bfin_serial_poll_get_char
,
1020 static void __init
bfin_serial_hw_init(void)
1022 #ifdef CONFIG_SERIAL_BFIN_UART0
1023 peripheral_request(P_UART0_TX
, DRIVER_NAME
);
1024 peripheral_request(P_UART0_RX
, DRIVER_NAME
);
1027 #ifdef CONFIG_SERIAL_BFIN_UART1
1028 peripheral_request(P_UART1_TX
, DRIVER_NAME
);
1029 peripheral_request(P_UART1_RX
, DRIVER_NAME
);
1031 # if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
1032 peripheral_request(P_UART1_RTS
, DRIVER_NAME
);
1033 peripheral_request(P_UART1_CTS
, DRIVER_NAME
);
1037 #ifdef CONFIG_SERIAL_BFIN_UART2
1038 peripheral_request(P_UART2_TX
, DRIVER_NAME
);
1039 peripheral_request(P_UART2_RX
, DRIVER_NAME
);
1042 #ifdef CONFIG_SERIAL_BFIN_UART3
1043 peripheral_request(P_UART3_TX
, DRIVER_NAME
);
1044 peripheral_request(P_UART3_RX
, DRIVER_NAME
);
1046 # if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
1047 peripheral_request(P_UART3_RTS
, DRIVER_NAME
);
1048 peripheral_request(P_UART3_CTS
, DRIVER_NAME
);
1053 static void __init
bfin_serial_init_ports(void)
1055 static int first
= 1;
1062 bfin_serial_hw_init();
1064 for (i
= 0; i
< nr_active_ports
; i
++) {
1065 spin_lock_init(&bfin_serial_ports
[i
].port
.lock
);
1066 bfin_serial_ports
[i
].port
.uartclk
= get_sclk();
1067 bfin_serial_ports
[i
].port
.fifosize
= BFIN_UART_TX_FIFO_SIZE
;
1068 bfin_serial_ports
[i
].port
.ops
= &bfin_serial_pops
;
1069 bfin_serial_ports
[i
].port
.line
= i
;
1070 bfin_serial_ports
[i
].port
.iotype
= UPIO_MEM
;
1071 bfin_serial_ports
[i
].port
.membase
=
1072 (void __iomem
*)bfin_serial_resource
[i
].uart_base_addr
;
1073 bfin_serial_ports
[i
].port
.mapbase
=
1074 bfin_serial_resource
[i
].uart_base_addr
;
1075 bfin_serial_ports
[i
].port
.irq
=
1076 bfin_serial_resource
[i
].uart_irq
;
1077 bfin_serial_ports
[i
].status_irq
=
1078 bfin_serial_resource
[i
].uart_status_irq
;
1079 bfin_serial_ports
[i
].port
.flags
= UPF_BOOT_AUTOCONF
;
1080 #ifdef CONFIG_SERIAL_BFIN_DMA
1081 bfin_serial_ports
[i
].tx_done
= 1;
1082 bfin_serial_ports
[i
].tx_count
= 0;
1083 bfin_serial_ports
[i
].tx_dma_channel
=
1084 bfin_serial_resource
[i
].uart_tx_dma_channel
;
1085 bfin_serial_ports
[i
].rx_dma_channel
=
1086 bfin_serial_resource
[i
].uart_rx_dma_channel
;
1087 init_timer(&(bfin_serial_ports
[i
].rx_dma_timer
));
1089 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1090 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1091 bfin_serial_ports
[i
].cts_pin
=
1092 bfin_serial_resource
[i
].uart_cts_pin
;
1093 bfin_serial_ports
[i
].rts_pin
=
1094 bfin_serial_resource
[i
].uart_rts_pin
;
1099 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1101 * If the port was already initialised (eg, by a boot loader),
1102 * try to determine the current setup.
1105 bfin_serial_console_get_options(struct bfin_serial_port
*uart
, int *baud
,
1106 int *parity
, int *bits
)
1108 unsigned short status
;
1110 status
= UART_GET_IER(uart
) & (ERBFI
| ETBEI
);
1111 if (status
== (ERBFI
| ETBEI
)) {
1112 /* ok, the port was enabled */
1115 lcr
= UART_GET_LCR(uart
);
1124 switch (lcr
& 0x03) {
1125 case 0: *bits
= 5; break;
1126 case 1: *bits
= 6; break;
1127 case 2: *bits
= 7; break;
1128 case 3: *bits
= 8; break;
1130 /* Set DLAB in LCR to Access DLL and DLH */
1131 UART_SET_DLAB(uart
);
1133 dll
= UART_GET_DLL(uart
);
1134 dlh
= UART_GET_DLH(uart
);
1136 /* Clear DLAB in LCR to Access THR RBR IER */
1137 UART_CLEAR_DLAB(uart
);
1139 *baud
= get_sclk() / (16*(dll
| dlh
<< 8));
1141 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__
, *baud
, *parity
, *bits
);
1144 static struct uart_driver bfin_serial_reg
;
1147 bfin_serial_console_setup(struct console
*co
, char *options
)
1149 struct bfin_serial_port
*uart
;
1153 # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1154 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1161 * Check whether an invalid uart number has been specified, and
1162 * if so, search for the first available port that does have
1165 if (co
->index
== -1 || co
->index
>= nr_active_ports
)
1167 uart
= &bfin_serial_ports
[co
->index
];
1170 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
1172 bfin_serial_console_get_options(uart
, &baud
, &parity
, &bits
);
1174 return uart_set_options(&uart
->port
, co
, baud
, parity
, bits
, flow
);
1176 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1177 defined (CONFIG_EARLY_PRINTK) */
1179 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1180 static void bfin_serial_console_putchar(struct uart_port
*port
, int ch
)
1182 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
1183 while (!(UART_GET_LSR(uart
) & THRE
))
1185 UART_PUT_CHAR(uart
, ch
);
1190 * Interrupts are disabled on entering
1193 bfin_serial_console_write(struct console
*co
, const char *s
, unsigned int count
)
1195 struct bfin_serial_port
*uart
= &bfin_serial_ports
[co
->index
];
1196 unsigned long flags
;
1198 spin_lock_irqsave(&uart
->port
.lock
, flags
);
1199 uart_console_write(&uart
->port
, s
, count
, bfin_serial_console_putchar
);
1200 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
1204 static struct console bfin_serial_console
= {
1205 .name
= BFIN_SERIAL_NAME
,
1206 .write
= bfin_serial_console_write
,
1207 .device
= uart_console_device
,
1208 .setup
= bfin_serial_console_setup
,
1209 .flags
= CON_PRINTBUFFER
,
1211 .data
= &bfin_serial_reg
,
1214 static int __init
bfin_serial_rs_console_init(void)
1216 bfin_serial_init_ports();
1217 register_console(&bfin_serial_console
);
1221 console_initcall(bfin_serial_rs_console_init
);
1223 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1225 #define BFIN_SERIAL_CONSOLE NULL
1226 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1229 #ifdef CONFIG_EARLY_PRINTK
1230 static __init
void early_serial_putc(struct uart_port
*port
, int ch
)
1232 unsigned timeout
= 0xffff;
1233 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
1235 while ((!(UART_GET_LSR(uart
) & THRE
)) && --timeout
)
1237 UART_PUT_CHAR(uart
, ch
);
1240 static __init
void early_serial_write(struct console
*con
, const char *s
,
1243 struct bfin_serial_port
*uart
= &bfin_serial_ports
[con
->index
];
1246 for (i
= 0; i
< n
; i
++, s
++) {
1248 early_serial_putc(&uart
->port
, '\r');
1249 early_serial_putc(&uart
->port
, *s
);
1253 static struct __initdata console bfin_early_serial_console
= {
1254 .name
= "early_BFuart",
1255 .write
= early_serial_write
,
1256 .device
= uart_console_device
,
1257 .flags
= CON_PRINTBUFFER
,
1258 .setup
= bfin_serial_console_setup
,
1260 .data
= &bfin_serial_reg
,
1263 struct console __init
*bfin_earlyserial_init(unsigned int port
,
1266 struct bfin_serial_port
*uart
;
1269 if (port
== -1 || port
>= nr_active_ports
)
1271 bfin_serial_init_ports();
1272 bfin_early_serial_console
.index
= port
;
1273 uart
= &bfin_serial_ports
[port
];
1279 bfin_serial_set_termios(&uart
->port
, &t
, &t
);
1280 return &bfin_early_serial_console
;
1283 #endif /* CONFIG_EARLY_PRINTK */
1285 static struct uart_driver bfin_serial_reg
= {
1286 .owner
= THIS_MODULE
,
1287 .driver_name
= "bfin-uart",
1288 .dev_name
= BFIN_SERIAL_NAME
,
1289 .major
= BFIN_SERIAL_MAJOR
,
1290 .minor
= BFIN_SERIAL_MINOR
,
1291 .nr
= BFIN_UART_NR_PORTS
,
1292 .cons
= BFIN_SERIAL_CONSOLE
,
1295 static int bfin_serial_suspend(struct platform_device
*dev
, pm_message_t state
)
1299 for (i
= 0; i
< nr_active_ports
; i
++) {
1300 if (bfin_serial_ports
[i
].port
.dev
!= &dev
->dev
)
1302 uart_suspend_port(&bfin_serial_reg
, &bfin_serial_ports
[i
].port
);
1308 static int bfin_serial_resume(struct platform_device
*dev
)
1312 for (i
= 0; i
< nr_active_ports
; i
++) {
1313 if (bfin_serial_ports
[i
].port
.dev
!= &dev
->dev
)
1315 uart_resume_port(&bfin_serial_reg
, &bfin_serial_ports
[i
].port
);
1321 static int bfin_serial_probe(struct platform_device
*dev
)
1323 struct resource
*res
= dev
->resource
;
1326 for (i
= 0; i
< dev
->num_resources
; i
++, res
++)
1327 if (res
->flags
& IORESOURCE_MEM
)
1330 if (i
< dev
->num_resources
) {
1331 for (i
= 0; i
< nr_active_ports
; i
++, res
++) {
1332 if (bfin_serial_ports
[i
].port
.mapbase
!= res
->start
)
1334 bfin_serial_ports
[i
].port
.dev
= &dev
->dev
;
1335 uart_add_one_port(&bfin_serial_reg
, &bfin_serial_ports
[i
].port
);
1342 static int bfin_serial_remove(struct platform_device
*dev
)
1346 for (i
= 0; i
< nr_active_ports
; i
++) {
1347 if (bfin_serial_ports
[i
].port
.dev
!= &dev
->dev
)
1349 uart_remove_one_port(&bfin_serial_reg
, &bfin_serial_ports
[i
].port
);
1350 bfin_serial_ports
[i
].port
.dev
= NULL
;
1351 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1352 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1353 gpio_free(bfin_serial_ports
[i
].cts_pin
);
1354 gpio_free(bfin_serial_ports
[i
].rts_pin
);
1361 static struct platform_driver bfin_serial_driver
= {
1362 .probe
= bfin_serial_probe
,
1363 .remove
= bfin_serial_remove
,
1364 .suspend
= bfin_serial_suspend
,
1365 .resume
= bfin_serial_resume
,
1367 .name
= "bfin-uart",
1368 .owner
= THIS_MODULE
,
1372 static int __init
bfin_serial_init(void)
1376 pr_info("Serial: Blackfin serial driver\n");
1378 bfin_serial_init_ports();
1380 ret
= uart_register_driver(&bfin_serial_reg
);
1382 ret
= platform_driver_register(&bfin_serial_driver
);
1384 pr_debug("uart register failed\n");
1385 uart_unregister_driver(&bfin_serial_reg
);
1391 static void __exit
bfin_serial_exit(void)
1393 platform_driver_unregister(&bfin_serial_driver
);
1394 uart_unregister_driver(&bfin_serial_reg
);
1398 module_init(bfin_serial_init
);
1399 module_exit(bfin_serial_exit
);
1401 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1402 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1403 MODULE_LICENSE("GPL");
1404 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR
);
1405 MODULE_ALIAS("platform:bfin-uart");