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);
338 * 05000215 - we always clear ETBEI within last UART TX
339 * interrupt to end a string. It is always set
340 * when start a new tx.
342 UART_CLEAR_IER(uart, ETBEI);
346 if (uart->port.x_char) {
347 UART_PUT_CHAR(uart, uart->port.x_char);
348 uart->port.icount.tx++;
349 uart->port.x_char = 0;
352 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
353 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
354 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
355 uart->port.icount.tx++;
359 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
360 uart_write_wakeup(&uart->port);
363 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
365 struct bfin_serial_port *uart = dev_id;
367 spin_lock(&uart->port.lock);
368 while (UART_GET_LSR(uart) & DR)
369 bfin_serial_rx_chars(uart);
370 spin_unlock(&uart->port.lock);
375 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
377 struct bfin_serial_port *uart = dev_id;
379 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
380 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
382 uart_handle_cts_change(&uart->port, uart->scts);
385 spin_lock(&uart->port.lock);
386 if (UART_GET_LSR(uart) & THRE)
387 bfin_serial_tx_chars(uart);
388 spin_unlock(&uart->port.lock);
394 #ifdef CONFIG_SERIAL_BFIN_DMA
395 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
397 struct circ_buf *xmit = &uart->port.info->xmit;
401 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
407 if (uart->port.x_char) {
408 UART_PUT_CHAR(uart, uart->port.x_char);
409 uart->port.icount.tx++;
410 uart->port.x_char = 0;
413 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
414 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
415 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
416 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
417 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
418 set_dma_config(uart->tx_dma_channel,
419 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
424 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
425 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
426 set_dma_x_modify(uart->tx_dma_channel, 1);
428 enable_dma(uart->tx_dma_channel);
430 UART_SET_IER(uart, ETBEI);
433 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
435 struct tty_struct *tty = uart->port.info->port.tty;
438 status = UART_GET_LSR(uart);
439 UART_CLEAR_LSR(uart);
441 uart->port.icount.rx +=
442 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
446 uart->port.icount.brk++;
447 if (uart_handle_break(&uart->port))
448 goto dma_ignore_char;
449 status &= ~(PE | FE);
452 uart->port.icount.parity++;
454 uart->port.icount.overrun++;
456 uart->port.icount.frame++;
458 status &= uart->port.read_status_mask;
462 else if (status & PE)
464 else if (status & FE)
469 for (i = uart->rx_dma_buf.tail; ; i++) {
470 if (i >= UART_XMIT_SIZE)
472 if (i == uart->rx_dma_buf.head)
474 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
475 uart_insert_char(&uart->port, status, OE,
476 uart->rx_dma_buf.buf[i], flg);
480 tty_flip_buffer_push(tty);
483 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
487 dma_disable_irq(uart->rx_dma_channel);
488 spin_lock_bh(&uart->port.lock);
490 /* 2D DMA RX buffer ring is used. Because curr_y_count and
491 * curr_x_count can't be read as an atomic operation,
492 * curr_y_count should be read before curr_x_count. When
493 * curr_x_count is read, curr_y_count may already indicate
494 * next buffer line. But, the position calculated here is
495 * still indicate the old line. The wrong position data may
496 * be smaller than current buffer tail, which cause garbages
497 * are received if it is not prohibit.
499 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
500 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
501 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
502 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
503 uart->rx_dma_nrows = 0;
504 x_pos = DMA_RX_XCOUNT - x_pos;
505 if (x_pos == DMA_RX_XCOUNT)
508 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
509 /* Ignore receiving data if new position is in the same line of
510 * current buffer tail and small.
512 if (pos > uart->rx_dma_buf.tail ||
513 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
514 uart->rx_dma_buf.head = pos;
515 bfin_serial_dma_rx_chars(uart);
516 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
519 spin_unlock_bh(&uart->port.lock);
520 dma_enable_irq(uart->rx_dma_channel);
522 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
525 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
527 struct bfin_serial_port *uart = dev_id;
528 struct circ_buf *xmit = &uart->port.info->xmit;
530 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
531 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
533 uart_handle_cts_change(&uart->port, uart->scts);
537 spin_lock(&uart->port.lock);
538 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
539 disable_dma(uart->tx_dma_channel);
540 clear_dma_irqstat(uart->tx_dma_channel);
542 * 05000215 - we always clear ETBEI within last UART TX
543 * interrupt to end a string. It is always set
544 * when start a new tx.
546 UART_CLEAR_IER(uart, ETBEI);
547 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
548 uart->port.icount.tx += uart->tx_count;
550 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
551 uart_write_wakeup(&uart->port);
553 bfin_serial_dma_tx_chars(uart);
556 spin_unlock(&uart->port.lock);
560 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
562 struct bfin_serial_port *uart = dev_id;
563 unsigned short irqstat;
566 spin_lock(&uart->port.lock);
567 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
568 clear_dma_irqstat(uart->rx_dma_channel);
570 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
571 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
572 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
573 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
574 uart->rx_dma_nrows = 0;
576 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
577 if (pos > uart->rx_dma_buf.tail ||
578 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
579 uart->rx_dma_buf.head = pos;
580 bfin_serial_dma_rx_chars(uart);
581 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
584 spin_unlock(&uart->port.lock);
591 * Return TIOCSER_TEMT when transmitter is not busy.
593 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
595 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
598 lsr = UART_GET_LSR(uart);
605 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
607 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
608 u16 lcr = UART_GET_LCR(uart);
613 UART_PUT_LCR(uart, lcr);
617 static int bfin_serial_startup(struct uart_port *port)
619 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
621 #ifdef CONFIG_SERIAL_BFIN_DMA
622 dma_addr_t dma_handle;
624 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
625 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
629 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
630 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
631 free_dma(uart->rx_dma_channel);
635 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
636 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
638 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
639 uart->rx_dma_buf.head = 0;
640 uart->rx_dma_buf.tail = 0;
641 uart->rx_dma_nrows = 0;
643 set_dma_config(uart->rx_dma_channel,
644 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
645 INTR_ON_ROW, DIMENSION_2D,
648 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
649 set_dma_x_modify(uart->rx_dma_channel, 1);
650 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
651 set_dma_y_modify(uart->rx_dma_channel, 1);
652 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
653 enable_dma(uart->rx_dma_channel);
655 uart->rx_dma_timer.data = (unsigned long)(uart);
656 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
657 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
658 add_timer(&(uart->rx_dma_timer));
660 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
661 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
662 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
663 kgdboc_break_enabled = 0;
666 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
667 "BFIN_UART_RX", uart)) {
668 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
673 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
674 "BFIN_UART_TX", uart)) {
675 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
676 free_irq(uart->port.irq, uart);
682 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
684 switch (uart->port.irq) {
686 uart_dma_ch_rx = CH_UART3_RX;
687 uart_dma_ch_tx = CH_UART3_TX;
690 uart_dma_ch_rx = CH_UART2_RX;
691 uart_dma_ch_tx = CH_UART2_TX;
694 uart_dma_ch_rx = uart_dma_ch_tx = 0;
698 if (uart_dma_ch_rx &&
699 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
700 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
701 free_irq(uart->port.irq, uart);
702 free_irq(uart->port.irq + 1, uart);
705 if (uart_dma_ch_tx &&
706 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
707 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
708 free_dma(uart_dma_ch_rx);
709 free_irq(uart->port.irq, uart);
710 free_irq(uart->port.irq + 1, uart);
715 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
716 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
721 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
722 if (uart->cts_pin >= 0) {
723 if (request_irq(gpio_to_irq(uart->cts_pin),
724 bfin_serial_mctrl_cts_int,
725 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
726 IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
728 pr_info("Unable to attach BlackFin UART CTS interrupt.\
732 if (uart->rts_pin >= 0) {
733 gpio_request(uart->rts_pin, DRIVER_NAME);
734 gpio_direction_output(uart->rts_pin, 0);
737 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
738 if (request_irq(uart->status_irq,
739 bfin_serial_mctrl_cts_int,
740 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
741 pr_info("Unable to attach BlackFin UART Modem \
742 Status interrupt.\n");
745 if (uart->cts_pin >= 0) {
746 gpio_request(uart->cts_pin, DRIVER_NAME);
747 gpio_direction_output(uart->cts_pin, 1);
749 if (uart->rts_pin >= 0) {
750 gpio_request(uart->rts_pin, DRIVER_NAME);
751 gpio_direction_output(uart->rts_pin, 0);
754 /* CTS RTS PINs are negative assertive. */
755 UART_PUT_MCR(uart, ACTS);
756 UART_SET_IER(uart, EDSSI);
759 UART_SET_IER(uart, ERBFI);
763 static void bfin_serial_shutdown(struct uart_port *port)
765 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
767 #ifdef CONFIG_SERIAL_BFIN_DMA
768 disable_dma(uart->tx_dma_channel);
769 free_dma(uart->tx_dma_channel);
770 disable_dma(uart->rx_dma_channel);
771 free_dma(uart->rx_dma_channel);
772 del_timer(&(uart->rx_dma_timer));
773 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
776 switch (uart->port.irq) {
778 free_dma(CH_UART3_RX);
779 free_dma(CH_UART3_TX);
782 free_dma(CH_UART2_RX);
783 free_dma(CH_UART2_TX);
789 free_irq(uart->port.irq, uart);
790 free_irq(uart->port.irq+1, uart);
793 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
794 if (uart->cts_pin >= 0)
795 free_irq(gpio_to_irq(uart->cts_pin), uart);
796 if (uart->rts_pin >= 0)
797 gpio_free(uart->rts_pin);
799 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
800 if (uart->cts_pin >= 0)
801 gpio_free(uart->cts_pin);
802 if (uart->rts_pin >= 0)
803 gpio_free(uart->rts_pin);
804 if (UART_GET_IER(uart) && EDSSI)
805 free_irq(uart->status_irq, uart);
810 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
811 struct ktermios *old)
813 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
815 unsigned int baud, quot;
816 unsigned short val, ier, lcr = 0;
818 switch (termios->c_cflag & CSIZE) {
832 printk(KERN_ERR "%s: word lengh not supported\n",
837 * 05000231 - STOP bit is always set to 1 whatever the user is set.
839 if (termios->c_cflag & CSTOPB) {
840 if (ANOMALY_05000231)
841 printk(KERN_WARNING "STOP bits other than 1 is not "
842 "supported in case of anomaly 05000231.\n");
846 if (termios->c_cflag & PARENB)
848 if (!(termios->c_cflag & PARODD))
850 if (termios->c_cflag & CMSPAR)
853 port->read_status_mask = OE;
854 if (termios->c_iflag & INPCK)
855 port->read_status_mask |= (FE | PE);
856 if (termios->c_iflag & (BRKINT | PARMRK))
857 port->read_status_mask |= BI;
860 * Characters to ignore
862 port->ignore_status_mask = 0;
863 if (termios->c_iflag & IGNPAR)
864 port->ignore_status_mask |= FE | PE;
865 if (termios->c_iflag & IGNBRK) {
866 port->ignore_status_mask |= BI;
868 * If we're ignoring parity and break indicators,
869 * ignore overruns too (for real raw support).
871 if (termios->c_iflag & IGNPAR)
872 port->ignore_status_mask |= OE;
875 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
876 quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
877 spin_lock_irqsave(&uart->port.lock, flags);
879 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
882 ier = UART_GET_IER(uart);
883 UART_DISABLE_INTS(uart);
885 /* Set DLAB in LCR to Access DLL and DLH */
888 UART_PUT_DLL(uart, quot & 0xFF);
889 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
892 /* Clear DLAB in LCR to Access THR RBR IER */
893 UART_CLEAR_DLAB(uart);
895 UART_PUT_LCR(uart, lcr);
898 UART_ENABLE_INTS(uart, ier);
900 val = UART_GET_GCTL(uart);
902 UART_PUT_GCTL(uart, val);
904 /* Port speed changed, update the per-port timeout. */
905 uart_update_timeout(port, termios->c_cflag, baud);
907 spin_unlock_irqrestore(&uart->port.lock, flags);
910 static const char *bfin_serial_type(struct uart_port *port)
912 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
914 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
918 * Release the memory region(s) being used by 'port'.
920 static void bfin_serial_release_port(struct uart_port *port)
925 * Request the memory region(s) being used by 'port'.
927 static int bfin_serial_request_port(struct uart_port *port)
933 * Configure/autoconfigure the port.
935 static void bfin_serial_config_port(struct uart_port *port, int flags)
937 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
939 if (flags & UART_CONFIG_TYPE &&
940 bfin_serial_request_port(&uart->port) == 0)
941 uart->port.type = PORT_BFIN;
945 * Verify the new serial_struct (for TIOCSSERIAL).
946 * The only change we allow are to the flags and type, and
947 * even then only between PORT_BFIN and PORT_UNKNOWN
950 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
956 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
957 * In other cases, disable IrDA function.
959 static void bfin_serial_set_ldisc(struct uart_port *port)
961 int line = port->line;
964 if (line >= port->info->port.tty->driver->num)
967 switch (port->info->port.tty->termios->c_line) {
969 val = UART_GET_GCTL(&bfin_serial_ports[line]);
970 val |= (IREN | RPOLC);
971 UART_PUT_GCTL(&bfin_serial_ports[line], val);
974 val = UART_GET_GCTL(&bfin_serial_ports[line]);
975 val &= ~(IREN | RPOLC);
976 UART_PUT_GCTL(&bfin_serial_ports[line], val);
980 static void bfin_serial_reset_irda(struct uart_port *port)
982 int line = port->line;
985 val = UART_GET_GCTL(&bfin_serial_ports[line]);
986 val &= ~(IREN | RPOLC);
987 UART_PUT_GCTL(&bfin_serial_ports[line], val);
989 val |= (IREN | RPOLC);
990 UART_PUT_GCTL(&bfin_serial_ports[line], val);
994 #ifdef CONFIG_CONSOLE_POLL
996 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
997 * losing other bits of UART_LSR is not a problem here.
999 static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
1001 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1003 while (!(UART_GET_LSR(uart) & THRE))
1006 UART_CLEAR_DLAB(uart);
1007 UART_PUT_CHAR(uart, (unsigned char)chr);
1010 static int bfin_serial_poll_get_char(struct uart_port *port)
1012 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1015 while (!(UART_GET_LSR(uart) & DR))
1018 UART_CLEAR_DLAB(uart);
1019 chr = UART_GET_CHAR(uart);
1025 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1026 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1027 static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1029 if (kgdboc_break_enabled) {
1030 kgdboc_break_enabled = 0;
1031 bfin_serial_shutdown(port);
1035 static int bfin_kgdboc_port_startup(struct uart_port *port)
1037 kgdboc_port_line = port->line;
1038 kgdboc_break_enabled = !bfin_serial_startup(port);
1043 static struct uart_ops bfin_serial_pops = {
1044 .tx_empty = bfin_serial_tx_empty,
1045 .set_mctrl = bfin_serial_set_mctrl,
1046 .get_mctrl = bfin_serial_get_mctrl,
1047 .stop_tx = bfin_serial_stop_tx,
1048 .start_tx = bfin_serial_start_tx,
1049 .stop_rx = bfin_serial_stop_rx,
1050 .enable_ms = bfin_serial_enable_ms,
1051 .break_ctl = bfin_serial_break_ctl,
1052 .startup = bfin_serial_startup,
1053 .shutdown = bfin_serial_shutdown,
1054 .set_termios = bfin_serial_set_termios,
1055 .set_ldisc = bfin_serial_set_ldisc,
1056 .type = bfin_serial_type,
1057 .release_port = bfin_serial_release_port,
1058 .request_port = bfin_serial_request_port,
1059 .config_port = bfin_serial_config_port,
1060 .verify_port = bfin_serial_verify_port,
1061 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1062 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1063 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1064 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1066 #ifdef CONFIG_CONSOLE_POLL
1067 .poll_put_char = bfin_serial_poll_put_char,
1068 .poll_get_char = bfin_serial_poll_get_char,
1072 static void __init bfin_serial_hw_init(void)
1074 #ifdef CONFIG_SERIAL_BFIN_UART0
1075 peripheral_request(P_UART0_TX, DRIVER_NAME);
1076 peripheral_request(P_UART0_RX, DRIVER_NAME);
1079 #ifdef CONFIG_SERIAL_BFIN_UART1
1080 peripheral_request(P_UART1_TX, DRIVER_NAME);
1081 peripheral_request(P_UART1_RX, DRIVER_NAME);
1083 # if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
1084 peripheral_request(P_UART1_RTS, DRIVER_NAME);
1085 peripheral_request(P_UART1_CTS, DRIVER_NAME);
1089 #ifdef CONFIG_SERIAL_BFIN_UART2
1090 peripheral_request(P_UART2_TX, DRIVER_NAME);
1091 peripheral_request(P_UART2_RX, DRIVER_NAME);
1094 #ifdef CONFIG_SERIAL_BFIN_UART3
1095 peripheral_request(P_UART3_TX, DRIVER_NAME);
1096 peripheral_request(P_UART3_RX, DRIVER_NAME);
1098 # if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
1099 peripheral_request(P_UART3_RTS, DRIVER_NAME);
1100 peripheral_request(P_UART3_CTS, DRIVER_NAME);
1105 static void __init bfin_serial_init_ports(void)
1107 static int first = 1;
1114 bfin_serial_hw_init();
1116 for (i = 0; i < nr_active_ports; i++) {
1117 spin_lock_init(&bfin_serial_ports[i].port.lock);
1118 bfin_serial_ports[i].port.uartclk = get_sclk();
1119 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1120 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
1121 bfin_serial_ports[i].port.line = i;
1122 bfin_serial_ports[i].port.iotype = UPIO_MEM;
1123 bfin_serial_ports[i].port.membase =
1124 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
1125 bfin_serial_ports[i].port.mapbase =
1126 bfin_serial_resource[i].uart_base_addr;
1127 bfin_serial_ports[i].port.irq =
1128 bfin_serial_resource[i].uart_irq;
1129 bfin_serial_ports[i].status_irq =
1130 bfin_serial_resource[i].uart_status_irq;
1131 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
1132 #ifdef CONFIG_SERIAL_BFIN_DMA
1133 bfin_serial_ports[i].tx_done = 1;
1134 bfin_serial_ports[i].tx_count = 0;
1135 bfin_serial_ports[i].tx_dma_channel =
1136 bfin_serial_resource[i].uart_tx_dma_channel;
1137 bfin_serial_ports[i].rx_dma_channel =
1138 bfin_serial_resource[i].uart_rx_dma_channel;
1139 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
1141 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1142 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1143 bfin_serial_ports[i].cts_pin =
1144 bfin_serial_resource[i].uart_cts_pin;
1145 bfin_serial_ports[i].rts_pin =
1146 bfin_serial_resource[i].uart_rts_pin;
1151 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1153 * If the port was already initialised (eg, by a boot loader),
1154 * try to determine the current setup.
1157 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1158 int *parity, int *bits)
1160 unsigned short status;
1162 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1163 if (status == (ERBFI | ETBEI)) {
1164 /* ok, the port was enabled */
1167 lcr = UART_GET_LCR(uart);
1176 switch (lcr & 0x03) {
1177 case 0: *bits = 5; break;
1178 case 1: *bits = 6; break;
1179 case 2: *bits = 7; break;
1180 case 3: *bits = 8; break;
1182 /* Set DLAB in LCR to Access DLL and DLH */
1183 UART_SET_DLAB(uart);
1185 dll = UART_GET_DLL(uart);
1186 dlh = UART_GET_DLH(uart);
1188 /* Clear DLAB in LCR to Access THR RBR IER */
1189 UART_CLEAR_DLAB(uart);
1191 *baud = get_sclk() / (16*(dll | dlh << 8));
1193 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1196 static struct uart_driver bfin_serial_reg;
1199 bfin_serial_console_setup(struct console *co, char *options)
1201 struct bfin_serial_port *uart;
1205 # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1206 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1213 * Check whether an invalid uart number has been specified, and
1214 * if so, search for the first available port that does have
1217 if (co->index == -1 || co->index >= nr_active_ports)
1219 uart = &bfin_serial_ports[co->index];
1222 uart_parse_options(options, &baud, &parity, &bits, &flow);
1224 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1226 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1228 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1229 defined (CONFIG_EARLY_PRINTK) */
1231 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1232 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1234 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1235 while (!(UART_GET_LSR(uart) & THRE))
1237 UART_PUT_CHAR(uart, ch);
1242 * Interrupts are disabled on entering
1245 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1247 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1248 unsigned long flags;
1250 spin_lock_irqsave(&uart->port.lock, flags);
1251 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1252 spin_unlock_irqrestore(&uart->port.lock, flags);
1256 static struct console bfin_serial_console = {
1257 .name = BFIN_SERIAL_NAME,
1258 .write = bfin_serial_console_write,
1259 .device = uart_console_device,
1260 .setup = bfin_serial_console_setup,
1261 .flags = CON_PRINTBUFFER,
1263 .data = &bfin_serial_reg,
1266 static int __init bfin_serial_rs_console_init(void)
1268 bfin_serial_init_ports();
1269 register_console(&bfin_serial_console);
1273 console_initcall(bfin_serial_rs_console_init);
1275 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1277 #define BFIN_SERIAL_CONSOLE NULL
1278 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1281 #ifdef CONFIG_EARLY_PRINTK
1282 static __init void early_serial_putc(struct uart_port *port, int ch)
1284 unsigned timeout = 0xffff;
1285 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1287 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1289 UART_PUT_CHAR(uart, ch);
1292 static __init void early_serial_write(struct console *con, const char *s,
1295 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1298 for (i = 0; i < n; i++, s++) {
1300 early_serial_putc(&uart->port, '\r');
1301 early_serial_putc(&uart->port, *s);
1306 * This should have a .setup or .early_setup in it, but then things get called
1307 * without the command line options, and the baud rate gets messed up - so
1308 * don't let the common infrastructure play with things. (see calls to setup
1309 * & earlysetup in ./kernel/printk.c:register_console()
1311 static struct __initdata console bfin_early_serial_console = {
1312 .name = "early_BFuart",
1313 .write = early_serial_write,
1314 .device = uart_console_device,
1315 .flags = CON_PRINTBUFFER,
1317 .data = &bfin_serial_reg,
1320 struct console __init *bfin_earlyserial_init(unsigned int port,
1323 struct bfin_serial_port *uart;
1326 if (port == -1 || port >= nr_active_ports)
1328 bfin_serial_init_ports();
1329 bfin_early_serial_console.index = port;
1330 uart = &bfin_serial_ports[port];
1336 bfin_serial_set_termios(&uart->port, &t, &t);
1337 return &bfin_early_serial_console;
1340 #endif /* CONFIG_EARLY_PRINTK */
1342 static struct uart_driver bfin_serial_reg = {
1343 .owner = THIS_MODULE,
1344 .driver_name = "bfin-uart",
1345 .dev_name = BFIN_SERIAL_NAME,
1346 .major = BFIN_SERIAL_MAJOR,
1347 .minor = BFIN_SERIAL_MINOR,
1348 .nr = BFIN_UART_NR_PORTS,
1349 .cons = BFIN_SERIAL_CONSOLE,
1352 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1356 for (i = 0; i < nr_active_ports; i++) {
1357 if (bfin_serial_ports[i].port.dev != &dev->dev)
1359 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1365 static int bfin_serial_resume(struct platform_device *dev)
1369 for (i = 0; i < nr_active_ports; i++) {
1370 if (bfin_serial_ports[i].port.dev != &dev->dev)
1372 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1378 static int bfin_serial_probe(struct platform_device *dev)
1380 struct resource *res = dev->resource;
1383 for (i = 0; i < dev->num_resources; i++, res++)
1384 if (res->flags & IORESOURCE_MEM)
1387 if (i < dev->num_resources) {
1388 for (i = 0; i < nr_active_ports; i++, res++) {
1389 if (bfin_serial_ports[i].port.mapbase != res->start)
1391 bfin_serial_ports[i].port.dev = &dev->dev;
1392 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1399 static int bfin_serial_remove(struct platform_device *dev)
1403 for (i = 0; i < nr_active_ports; i++) {
1404 if (bfin_serial_ports[i].port.dev != &dev->dev)
1406 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1407 bfin_serial_ports[i].port.dev = NULL;
1408 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1409 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1410 gpio_free(bfin_serial_ports[i].cts_pin);
1411 gpio_free(bfin_serial_ports[i].rts_pin);
1418 static struct platform_driver bfin_serial_driver = {
1419 .probe = bfin_serial_probe,
1420 .remove = bfin_serial_remove,
1421 .suspend = bfin_serial_suspend,
1422 .resume = bfin_serial_resume,
1424 .name = "bfin-uart",
1425 .owner = THIS_MODULE,
1429 static int __init bfin_serial_init(void)
1433 pr_info("Serial: Blackfin serial driver\n");
1435 bfin_serial_init_ports();
1437 ret = uart_register_driver(&bfin_serial_reg);
1439 ret = platform_driver_register(&bfin_serial_driver);
1441 pr_debug("uart register failed\n");
1442 uart_unregister_driver(&bfin_serial_reg);
1448 static void __exit bfin_serial_exit(void)
1450 platform_driver_unregister(&bfin_serial_driver);
1451 uart_unregister_driver(&bfin_serial_reg);
1455 module_init(bfin_serial_init);
1456 module_exit(bfin_serial_exit);
1458 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1459 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1460 MODULE_LICENSE("GPL");
1461 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1462 MODULE_ALIAS("platform:bfin-uart");