2 * Blackfin On-Chip Serial Driver
4 * Copyright 2006-2011 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 #define DRIVER_NAME "bfin-uart"
16 #define pr_fmt(fmt) DRIVER_NAME ": " fmt
18 #include <linux/module.h>
19 #include <linux/ioport.h>
20 #include <linux/gfp.h>
22 #include <linux/init.h>
23 #include <linux/console.h>
24 #include <linux/sysrq.h>
25 #include <linux/platform_device.h>
26 #include <linux/tty.h>
27 #include <linux/tty_flip.h>
28 #include <linux/serial_core.h>
29 #include <linux/gpio.h>
30 #include <linux/irq.h>
31 #include <linux/kgdb.h>
32 #include <linux/slab.h>
33 #include <linux/dma-mapping.h>
35 #include <asm/portmux.h>
36 #include <asm/cacheflush.h>
38 #include <asm/bfin_serial.h>
40 #ifdef CONFIG_SERIAL_BFIN_MODULE
41 # undef CONFIG_EARLY_PRINTK
44 /* UART name and device definitions */
45 #define BFIN_SERIAL_DEV_NAME "ttyBF"
46 #define BFIN_SERIAL_MAJOR 204
47 #define BFIN_SERIAL_MINOR 64
49 static struct bfin_serial_port *bfin_serial_ports[BFIN_UART_NR_PORTS];
51 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
52 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
54 # ifndef CONFIG_SERIAL_BFIN_PIO
55 # error KGDB only support UART in PIO mode.
58 static int kgdboc_port_line;
59 static int kgdboc_break_enabled;
62 * Setup for console. Argument comes from the menuconfig
64 #define DMA_RX_XCOUNT 512
65 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
67 #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
69 #ifdef CONFIG_SERIAL_BFIN_DMA
70 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
72 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
75 static void bfin_serial_reset_irda(struct uart_port *port);
77 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
78 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
79 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
81 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
82 if (uart->cts_pin < 0)
83 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
85 /* CTS PIN is negative assertive. */
86 if (UART_GET_CTS(uart))
87 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
89 return TIOCM_DSR | TIOCM_CAR;
92 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
94 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
95 if (uart->rts_pin < 0)
98 /* RTS PIN is negative assertive. */
99 if (mctrl & TIOCM_RTS)
100 UART_ENABLE_RTS(uart);
102 UART_DISABLE_RTS(uart);
106 * Handle any change of modem status signal.
108 static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
110 struct bfin_serial_port *uart = dev_id;
111 unsigned int status = bfin_serial_get_mctrl(&uart->port);
112 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
113 struct tty_struct *tty = uart->port.state->port.tty;
115 UART_CLEAR_SCTS(uart);
116 if (tty->hw_stopped) {
119 uart_write_wakeup(&uart->port);
126 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
131 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
133 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
136 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
142 * interrupts are disabled on entry
144 static void bfin_serial_stop_tx(struct uart_port *port)
146 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
147 #ifdef CONFIG_SERIAL_BFIN_DMA
148 struct circ_buf *xmit = &uart->port.state->xmit;
151 while (!(UART_GET_LSR(uart) & TEMT))
154 #ifdef CONFIG_SERIAL_BFIN_DMA
155 disable_dma(uart->tx_dma_channel);
156 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
157 uart->port.icount.tx += uart->tx_count;
161 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
163 UART_PUT_LSR(uart, TFI);
165 UART_CLEAR_IER(uart, ETBEI);
170 * port is locked and interrupts are disabled
172 static void bfin_serial_start_tx(struct uart_port *port)
174 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
175 struct tty_struct *tty = uart->port.state->port.tty;
178 * To avoid losting RX interrupt, we reset IR function
179 * before sending data.
181 if (tty->termios.c_line == N_IRDA)
182 bfin_serial_reset_irda(port);
184 #ifdef CONFIG_SERIAL_BFIN_DMA
186 bfin_serial_dma_tx_chars(uart);
188 UART_SET_IER(uart, ETBEI);
189 bfin_serial_tx_chars(uart);
194 * Interrupts are enabled
196 static void bfin_serial_stop_rx(struct uart_port *port)
198 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
200 UART_CLEAR_IER(uart, ERBFI);
203 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
204 # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
205 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
207 # define UART_GET_ANOMALY_THRESHOLD(uart) 0
208 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
211 #ifdef CONFIG_SERIAL_BFIN_PIO
212 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
214 unsigned int status, ch, flg;
215 static struct timeval anomaly_start = { .tv_sec = 0 };
217 status = UART_GET_LSR(uart);
218 UART_CLEAR_LSR(uart);
220 ch = UART_GET_CHAR(uart);
221 uart->port.icount.rx++;
223 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
224 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
225 if (kgdb_connected && kgdboc_port_line == uart->port.line
226 && kgdboc_break_enabled)
227 if (ch == 0x3) {/* Ctrl + C */
232 if (!uart->port.state)
235 if (ANOMALY_05000363) {
236 /* The BF533 (and BF561) family of processors have a nice anomaly
237 * where they continuously generate characters for a "single" break.
238 * We have to basically ignore this flood until the "next" valid
239 * character comes across. Due to the nature of the flood, it is
240 * not possible to reliably catch bytes that are sent too quickly
241 * after this break. So application code talking to the Blackfin
242 * which sends a break signal must allow at least 1.5 character
243 * times after the end of the break for things to stabilize. This
244 * timeout was picked as it must absolutely be larger than 1
245 * character time +/- some percent. So 1.5 sounds good. All other
246 * Blackfin families operate properly. Woo.
248 if (anomaly_start.tv_sec) {
252 if ((~ch & (~ch + 1)) & 0xff)
253 goto known_good_char;
255 do_gettimeofday(&curr);
256 if (curr.tv_sec - anomaly_start.tv_sec > 1)
257 goto known_good_char;
260 if (curr.tv_sec != anomaly_start.tv_sec)
261 usecs += USEC_PER_SEC;
262 usecs += curr.tv_usec - anomaly_start.tv_usec;
264 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
265 goto known_good_char;
268 anomaly_start.tv_sec = 0;
270 anomaly_start = curr;
276 anomaly_start.tv_sec = 0;
281 if (ANOMALY_05000363)
282 if (bfin_revid() < 5)
283 do_gettimeofday(&anomaly_start);
284 uart->port.icount.brk++;
285 if (uart_handle_break(&uart->port))
287 status &= ~(PE | FE);
290 uart->port.icount.parity++;
292 uart->port.icount.overrun++;
294 uart->port.icount.frame++;
296 status &= uart->port.read_status_mask;
300 else if (status & PE)
302 else if (status & FE)
307 if (uart_handle_sysrq_char(&uart->port, ch))
310 uart_insert_char(&uart->port, status, OE, ch, flg);
313 tty_flip_buffer_push(&uart->port.state->port);
316 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
318 struct circ_buf *xmit = &uart->port.state->xmit;
320 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
321 #if defined(CONFIG_BF54x) || defined(CONFIG_BF60x)
323 UART_PUT_LSR(uart, TFI);
326 * 05000215 - we always clear ETBEI within last UART TX
327 * interrupt to end a string. It is always set
328 * when start a new tx.
330 UART_CLEAR_IER(uart, ETBEI);
334 if (uart->port.x_char) {
335 UART_PUT_CHAR(uart, uart->port.x_char);
336 uart->port.icount.tx++;
337 uart->port.x_char = 0;
340 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
341 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
342 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
343 uart->port.icount.tx++;
346 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
347 uart_write_wakeup(&uart->port);
350 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
352 struct bfin_serial_port *uart = dev_id;
354 while (UART_GET_LSR(uart) & DR)
355 bfin_serial_rx_chars(uart);
360 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
362 struct bfin_serial_port *uart = dev_id;
364 spin_lock(&uart->port.lock);
365 if (UART_GET_LSR(uart) & THRE)
366 bfin_serial_tx_chars(uart);
367 spin_unlock(&uart->port.lock);
373 #ifdef CONFIG_SERIAL_BFIN_DMA
374 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
376 struct circ_buf *xmit = &uart->port.state->xmit;
380 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
386 if (uart->port.x_char) {
387 UART_PUT_CHAR(uart, uart->port.x_char);
388 uart->port.icount.tx++;
389 uart->port.x_char = 0;
392 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
393 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
394 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
395 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
396 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
397 set_dma_config(uart->tx_dma_channel,
398 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
403 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
404 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
405 set_dma_x_modify(uart->tx_dma_channel, 1);
407 enable_dma(uart->tx_dma_channel);
409 UART_SET_IER(uart, ETBEI);
412 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
416 status = UART_GET_LSR(uart);
417 UART_CLEAR_LSR(uart);
419 uart->port.icount.rx +=
420 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
424 uart->port.icount.brk++;
425 if (uart_handle_break(&uart->port))
426 goto dma_ignore_char;
427 status &= ~(PE | FE);
430 uart->port.icount.parity++;
432 uart->port.icount.overrun++;
434 uart->port.icount.frame++;
436 status &= uart->port.read_status_mask;
440 else if (status & PE)
442 else if (status & FE)
447 for (i = uart->rx_dma_buf.tail; ; i++) {
448 if (i >= UART_XMIT_SIZE)
450 if (i == uart->rx_dma_buf.head)
452 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
453 uart_insert_char(&uart->port, status, OE,
454 uart->rx_dma_buf.buf[i], flg);
458 tty_flip_buffer_push(&uart->port.state->port);
461 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
466 spin_lock_irqsave(&uart->rx_lock, flags);
468 /* 2D DMA RX buffer ring is used. Because curr_y_count and
469 * curr_x_count can't be read as an atomic operation,
470 * curr_y_count should be read before curr_x_count. When
471 * curr_x_count is read, curr_y_count may already indicate
472 * next buffer line. But, the position calculated here is
473 * still indicate the old line. The wrong position data may
474 * be smaller than current buffer tail, which cause garbages
475 * are received if it is not prohibit.
477 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
478 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
479 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
480 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
481 uart->rx_dma_nrows = 0;
482 x_pos = DMA_RX_XCOUNT - x_pos;
483 if (x_pos == DMA_RX_XCOUNT)
486 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
487 /* Ignore receiving data if new position is in the same line of
488 * current buffer tail and small.
490 if (pos > uart->rx_dma_buf.tail ||
491 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
492 uart->rx_dma_buf.head = pos;
493 bfin_serial_dma_rx_chars(uart);
494 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
497 spin_unlock_irqrestore(&uart->rx_lock, flags);
499 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
502 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
504 struct bfin_serial_port *uart = dev_id;
505 struct circ_buf *xmit = &uart->port.state->xmit;
507 spin_lock(&uart->port.lock);
508 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
509 disable_dma(uart->tx_dma_channel);
510 clear_dma_irqstat(uart->tx_dma_channel);
512 * 05000215 - we always clear ETBEI within last UART TX
513 * interrupt to end a string. It is always set
514 * when start a new tx.
516 UART_CLEAR_IER(uart, ETBEI);
517 uart->port.icount.tx += uart->tx_count;
518 if (!(xmit->tail == 0 && xmit->head == 0)) {
519 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
521 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
522 uart_write_wakeup(&uart->port);
525 bfin_serial_dma_tx_chars(uart);
528 spin_unlock(&uart->port.lock);
532 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
534 struct bfin_serial_port *uart = dev_id;
535 unsigned int irqstat;
538 spin_lock(&uart->rx_lock);
539 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
540 clear_dma_irqstat(uart->rx_dma_channel);
542 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
543 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
544 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
545 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
546 uart->rx_dma_nrows = 0;
548 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
549 if (pos > uart->rx_dma_buf.tail ||
550 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
551 uart->rx_dma_buf.head = pos;
552 bfin_serial_dma_rx_chars(uart);
553 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
556 spin_unlock(&uart->rx_lock);
563 * Return TIOCSER_TEMT when transmitter is not busy.
565 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
567 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
570 lsr = UART_GET_LSR(uart);
577 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
579 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
580 u32 lcr = UART_GET_LCR(uart);
585 UART_PUT_LCR(uart, lcr);
589 static int bfin_serial_startup(struct uart_port *port)
591 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
593 #ifdef CONFIG_SERIAL_BFIN_DMA
594 dma_addr_t dma_handle;
596 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
597 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
601 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
602 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
603 free_dma(uart->rx_dma_channel);
607 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
608 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
610 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
611 uart->rx_dma_buf.head = 0;
612 uart->rx_dma_buf.tail = 0;
613 uart->rx_dma_nrows = 0;
615 set_dma_config(uart->rx_dma_channel,
616 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
617 INTR_ON_ROW, DIMENSION_2D,
620 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
621 set_dma_x_modify(uart->rx_dma_channel, 1);
622 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
623 set_dma_y_modify(uart->rx_dma_channel, 1);
624 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
625 enable_dma(uart->rx_dma_channel);
627 uart->rx_dma_timer.data = (unsigned long)(uart);
628 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
629 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
630 add_timer(&(uart->rx_dma_timer));
632 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
633 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
634 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
635 kgdboc_break_enabled = 0;
638 if (request_irq(uart->rx_irq, bfin_serial_rx_int, 0,
639 "BFIN_UART_RX", uart)) {
640 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
645 (uart->tx_irq, bfin_serial_tx_int, 0,
646 "BFIN_UART_TX", uart)) {
647 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
648 free_irq(uart->rx_irq, uart);
655 * UART2 and UART3 on BF548 share interrupt PINs and DMA
656 * controllers with SPORT2 and SPORT3. UART rx and tx
657 * interrupts are generated in PIO mode only when configure
658 * their peripheral mapping registers properly, which means
659 * request corresponding DMA channels in PIO mode as well.
661 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
663 switch (uart->rx_irq) {
665 uart_dma_ch_rx = CH_UART3_RX;
666 uart_dma_ch_tx = CH_UART3_TX;
669 uart_dma_ch_rx = CH_UART2_RX;
670 uart_dma_ch_tx = CH_UART2_TX;
673 uart_dma_ch_rx = uart_dma_ch_tx = 0;
677 if (uart_dma_ch_rx &&
678 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
679 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
680 free_irq(uart->rx_irq, uart);
681 free_irq(uart->tx_irq, uart);
684 if (uart_dma_ch_tx &&
685 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
686 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
687 free_dma(uart_dma_ch_rx);
688 free_irq(uart->rx_irq, uart);
689 free_irq(uart->tx_irq, uart);
694 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
695 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
700 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
701 if (uart->cts_pin >= 0) {
702 if (request_irq(gpio_to_irq(uart->cts_pin),
703 bfin_serial_mctrl_cts_int,
704 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
705 0, "BFIN_UART_CTS", uart)) {
707 pr_info("Unable to attach BlackFin UART CTS interrupt. So, disable it.\n");
710 if (uart->rts_pin >= 0) {
711 if (gpio_request(uart->rts_pin, DRIVER_NAME)) {
712 pr_info("fail to request RTS PIN at GPIO_%d\n", uart->rts_pin);
715 gpio_direction_output(uart->rts_pin, 0);
718 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
719 if (uart->cts_pin >= 0) {
720 if (request_irq(uart->status_irq, bfin_serial_mctrl_cts_int,
721 0, "BFIN_UART_MODEM_STATUS", uart)) {
723 dev_info(port->dev, "Unable to attach BlackFin UART Modem Status interrupt.\n");
726 /* CTS RTS PINs are negative assertive. */
727 UART_PUT_MCR(uart, UART_GET_MCR(uart) | ACTS);
728 UART_SET_IER(uart, EDSSI);
732 UART_SET_IER(uart, ERBFI);
736 static void bfin_serial_shutdown(struct uart_port *port)
738 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
740 #ifdef CONFIG_SERIAL_BFIN_DMA
741 disable_dma(uart->tx_dma_channel);
742 free_dma(uart->tx_dma_channel);
743 disable_dma(uart->rx_dma_channel);
744 free_dma(uart->rx_dma_channel);
745 del_timer(&(uart->rx_dma_timer));
746 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
749 switch (uart->port.irq) {
751 free_dma(CH_UART3_RX);
752 free_dma(CH_UART3_TX);
755 free_dma(CH_UART2_RX);
756 free_dma(CH_UART2_TX);
762 free_irq(uart->rx_irq, uart);
763 free_irq(uart->tx_irq, uart);
766 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
767 if (uart->cts_pin >= 0)
768 free_irq(gpio_to_irq(uart->cts_pin), uart);
769 if (uart->rts_pin >= 0)
770 gpio_free(uart->rts_pin);
772 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
773 if (uart->cts_pin >= 0)
774 free_irq(uart->status_irq, uart);
779 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
780 struct ktermios *old)
782 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
784 unsigned int baud, quot;
785 unsigned int ier, lcr = 0;
786 unsigned long timeout;
788 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
789 if (old == NULL && uart->cts_pin != -1)
790 termios->c_cflag |= CRTSCTS;
791 else if (uart->cts_pin == -1)
792 termios->c_cflag &= ~CRTSCTS;
795 switch (termios->c_cflag & CSIZE) {
809 printk(KERN_ERR "%s: word length not supported\n",
814 * 05000231 - STOP bit is always set to 1 whatever the user is set.
816 if (termios->c_cflag & CSTOPB) {
817 if (ANOMALY_05000231)
818 printk(KERN_WARNING "STOP bits other than 1 is not "
819 "supported in case of anomaly 05000231.\n");
823 if (termios->c_cflag & PARENB)
825 if (!(termios->c_cflag & PARODD))
827 if (termios->c_cflag & CMSPAR)
830 spin_lock_irqsave(&uart->port.lock, flags);
832 port->read_status_mask = OE;
833 if (termios->c_iflag & INPCK)
834 port->read_status_mask |= (FE | PE);
835 if (termios->c_iflag & (IGNBRK | BRKINT | PARMRK))
836 port->read_status_mask |= BI;
839 * Characters to ignore
841 port->ignore_status_mask = 0;
842 if (termios->c_iflag & IGNPAR)
843 port->ignore_status_mask |= FE | PE;
844 if (termios->c_iflag & IGNBRK) {
845 port->ignore_status_mask |= BI;
847 * If we're ignoring parity and break indicators,
848 * ignore overruns too (for real raw support).
850 if (termios->c_iflag & IGNPAR)
851 port->ignore_status_mask |= OE;
854 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
855 quot = uart_get_divisor(port, baud);
857 /* If discipline is not IRDA, apply ANOMALY_05000230 */
858 if (termios->c_line != N_IRDA)
859 quot -= ANOMALY_05000230;
861 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
863 /* Wait till the transfer buffer is empty */
864 timeout = jiffies + msecs_to_jiffies(10);
865 while (UART_GET_GCTL(uart) & UCEN && !(UART_GET_LSR(uart) & TEMT))
866 if (time_after(jiffies, timeout)) {
867 dev_warn(port->dev, "timeout waiting for TX buffer empty\n");
872 ier = UART_GET_IER(uart);
873 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) & ~UCEN);
874 UART_DISABLE_INTS(uart);
876 /* Set DLAB in LCR to Access CLK */
879 UART_PUT_CLK(uart, quot);
882 /* Clear DLAB in LCR to Access THR RBR IER */
883 UART_CLEAR_DLAB(uart);
885 UART_PUT_LCR(uart, (UART_GET_LCR(uart) & ~LCR_MASK) | lcr);
888 UART_ENABLE_INTS(uart, ier);
889 UART_PUT_GCTL(uart, UART_GET_GCTL(uart) | UCEN);
891 /* Port speed changed, update the per-port timeout. */
892 uart_update_timeout(port, termios->c_cflag, baud);
894 spin_unlock_irqrestore(&uart->port.lock, flags);
897 static const char *bfin_serial_type(struct uart_port *port)
899 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
901 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
905 * Release the memory region(s) being used by 'port'.
907 static void bfin_serial_release_port(struct uart_port *port)
912 * Request the memory region(s) being used by 'port'.
914 static int bfin_serial_request_port(struct uart_port *port)
920 * Configure/autoconfigure the port.
922 static void bfin_serial_config_port(struct uart_port *port, int flags)
924 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
926 if (flags & UART_CONFIG_TYPE &&
927 bfin_serial_request_port(&uart->port) == 0)
928 uart->port.type = PORT_BFIN;
932 * Verify the new serial_struct (for TIOCSSERIAL).
933 * The only change we allow are to the flags and type, and
934 * even then only between PORT_BFIN and PORT_UNKNOWN
937 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
943 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
944 * In other cases, disable IrDA function.
946 static void bfin_serial_set_ldisc(struct uart_port *port, int ld)
948 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
953 val = UART_GET_GCTL(uart);
954 val |= (UMOD_IRDA | RPOLC);
955 UART_PUT_GCTL(uart, val);
958 val = UART_GET_GCTL(uart);
959 val &= ~(UMOD_MASK | RPOLC);
960 UART_PUT_GCTL(uart, val);
964 static void bfin_serial_reset_irda(struct uart_port *port)
966 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
969 val = UART_GET_GCTL(uart);
970 val &= ~(UMOD_MASK | RPOLC);
971 UART_PUT_GCTL(uart, val);
973 val |= (UMOD_IRDA | RPOLC);
974 UART_PUT_GCTL(uart, val);
978 #ifdef CONFIG_CONSOLE_POLL
980 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
981 * losing other bits of UART_LSR is not a problem here.
983 static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
985 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
987 while (!(UART_GET_LSR(uart) & THRE))
990 UART_CLEAR_DLAB(uart);
991 UART_PUT_CHAR(uart, (unsigned char)chr);
994 static int bfin_serial_poll_get_char(struct uart_port *port)
996 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
999 while (!(UART_GET_LSR(uart) & DR))
1002 UART_CLEAR_DLAB(uart);
1003 chr = UART_GET_CHAR(uart);
1009 static struct uart_ops bfin_serial_pops = {
1010 .tx_empty = bfin_serial_tx_empty,
1011 .set_mctrl = bfin_serial_set_mctrl,
1012 .get_mctrl = bfin_serial_get_mctrl,
1013 .stop_tx = bfin_serial_stop_tx,
1014 .start_tx = bfin_serial_start_tx,
1015 .stop_rx = bfin_serial_stop_rx,
1016 .break_ctl = bfin_serial_break_ctl,
1017 .startup = bfin_serial_startup,
1018 .shutdown = bfin_serial_shutdown,
1019 .set_termios = bfin_serial_set_termios,
1020 .set_ldisc = bfin_serial_set_ldisc,
1021 .type = bfin_serial_type,
1022 .release_port = bfin_serial_release_port,
1023 .request_port = bfin_serial_request_port,
1024 .config_port = bfin_serial_config_port,
1025 .verify_port = bfin_serial_verify_port,
1026 #ifdef CONFIG_CONSOLE_POLL
1027 .poll_put_char = bfin_serial_poll_put_char,
1028 .poll_get_char = bfin_serial_poll_get_char,
1032 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1034 * If the port was already initialised (eg, by a boot loader),
1035 * try to determine the current setup.
1038 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1039 int *parity, int *bits)
1041 unsigned int status;
1043 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1044 if (status == (ERBFI | ETBEI)) {
1045 /* ok, the port was enabled */
1048 lcr = UART_GET_LCR(uart);
1057 *bits = ((lcr & WLS_MASK) >> WLS_OFFSET) + 5;
1059 /* Set DLAB in LCR to Access CLK */
1060 UART_SET_DLAB(uart);
1062 clk = UART_GET_CLK(uart);
1064 /* Clear DLAB in LCR to Access THR RBR IER */
1065 UART_CLEAR_DLAB(uart);
1067 *baud = get_sclk() / (16*clk);
1069 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1072 static struct uart_driver bfin_serial_reg;
1074 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1076 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1077 while (!(UART_GET_LSR(uart) & THRE))
1079 UART_PUT_CHAR(uart, ch);
1082 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1083 defined (CONFIG_EARLY_PRINTK) */
1085 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1086 #define CLASS_BFIN_CONSOLE "bfin-console"
1088 * Interrupts are disabled on entering
1091 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1093 struct bfin_serial_port *uart = bfin_serial_ports[co->index];
1094 unsigned long flags;
1096 spin_lock_irqsave(&uart->port.lock, flags);
1097 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1098 spin_unlock_irqrestore(&uart->port.lock, flags);
1103 bfin_serial_console_setup(struct console *co, char *options)
1105 struct bfin_serial_port *uart;
1109 # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1110 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1117 * Check whether an invalid uart number has been specified, and
1118 * if so, search for the first available port that does have
1121 if (co->index < 0 || co->index >= BFIN_UART_NR_PORTS)
1124 uart = bfin_serial_ports[co->index];
1129 uart_parse_options(options, &baud, &parity, &bits, &flow);
1131 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1133 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1136 static struct console bfin_serial_console = {
1137 .name = BFIN_SERIAL_DEV_NAME,
1138 .write = bfin_serial_console_write,
1139 .device = uart_console_device,
1140 .setup = bfin_serial_console_setup,
1141 .flags = CON_PRINTBUFFER,
1143 .data = &bfin_serial_reg,
1145 #define BFIN_SERIAL_CONSOLE (&bfin_serial_console)
1147 #define BFIN_SERIAL_CONSOLE NULL
1148 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1150 #ifdef CONFIG_EARLY_PRINTK
1151 static struct bfin_serial_port bfin_earlyprintk_port;
1152 #define CLASS_BFIN_EARLYPRINTK "bfin-earlyprintk"
1155 * Interrupts are disabled on entering
1158 bfin_earlyprintk_console_write(struct console *co, const char *s, unsigned int count)
1160 unsigned long flags;
1162 if (bfin_earlyprintk_port.port.line != co->index)
1165 spin_lock_irqsave(&bfin_earlyprintk_port.port.lock, flags);
1166 uart_console_write(&bfin_earlyprintk_port.port, s, count,
1167 bfin_serial_console_putchar);
1168 spin_unlock_irqrestore(&bfin_earlyprintk_port.port.lock, flags);
1172 * This should have a .setup or .early_setup in it, but then things get called
1173 * without the command line options, and the baud rate gets messed up - so
1174 * don't let the common infrastructure play with things. (see calls to setup
1175 * & earlysetup in ./kernel/printk.c:register_console()
1177 static struct console bfin_early_serial_console __initdata = {
1178 .name = "early_BFuart",
1179 .write = bfin_earlyprintk_console_write,
1180 .device = uart_console_device,
1181 .flags = CON_PRINTBUFFER,
1183 .data = &bfin_serial_reg,
1187 static struct uart_driver bfin_serial_reg = {
1188 .owner = THIS_MODULE,
1189 .driver_name = DRIVER_NAME,
1190 .dev_name = BFIN_SERIAL_DEV_NAME,
1191 .major = BFIN_SERIAL_MAJOR,
1192 .minor = BFIN_SERIAL_MINOR,
1193 .nr = BFIN_UART_NR_PORTS,
1194 .cons = BFIN_SERIAL_CONSOLE,
1197 static int bfin_serial_suspend(struct platform_device *pdev, pm_message_t state)
1199 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1201 return uart_suspend_port(&bfin_serial_reg, &uart->port);
1204 static int bfin_serial_resume(struct platform_device *pdev)
1206 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1208 return uart_resume_port(&bfin_serial_reg, &uart->port);
1211 static int bfin_serial_probe(struct platform_device *pdev)
1213 struct resource *res;
1214 struct bfin_serial_port *uart = NULL;
1217 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1218 dev_err(&pdev->dev, "Wrong bfin uart platform device id.\n");
1222 if (bfin_serial_ports[pdev->id] == NULL) {
1224 uart = kzalloc(sizeof(*uart), GFP_KERNEL);
1227 "fail to malloc bfin_serial_port\n");
1230 bfin_serial_ports[pdev->id] = uart;
1232 #ifdef CONFIG_EARLY_PRINTK
1233 if (!(bfin_earlyprintk_port.port.membase
1234 && bfin_earlyprintk_port.port.line == pdev->id)) {
1236 * If the peripheral PINs of current port is allocated
1237 * in earlyprintk probe stage, don't do it again.
1240 ret = peripheral_request_list(
1241 dev_get_platdata(&pdev->dev),
1245 "fail to request bfin serial peripherals\n");
1246 goto out_error_free_mem;
1248 #ifdef CONFIG_EARLY_PRINTK
1252 spin_lock_init(&uart->port.lock);
1253 uart->port.uartclk = get_sclk();
1254 uart->port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1255 uart->port.ops = &bfin_serial_pops;
1256 uart->port.line = pdev->id;
1257 uart->port.iotype = UPIO_MEM;
1258 uart->port.flags = UPF_BOOT_AUTOCONF;
1260 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1262 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1264 goto out_error_free_peripherals;
1267 uart->port.membase = ioremap(res->start, resource_size(res));
1268 if (!uart->port.membase) {
1269 dev_err(&pdev->dev, "Cannot map uart IO\n");
1271 goto out_error_free_peripherals;
1273 uart->port.mapbase = res->start;
1275 uart->tx_irq = platform_get_irq(pdev, 0);
1276 if (uart->tx_irq < 0) {
1277 dev_err(&pdev->dev, "No uart TX IRQ specified\n");
1279 goto out_error_unmap;
1282 uart->rx_irq = platform_get_irq(pdev, 1);
1283 if (uart->rx_irq < 0) {
1284 dev_err(&pdev->dev, "No uart RX IRQ specified\n");
1286 goto out_error_unmap;
1288 uart->port.irq = uart->rx_irq;
1290 uart->status_irq = platform_get_irq(pdev, 2);
1291 if (uart->status_irq < 0) {
1292 dev_err(&pdev->dev, "No uart status IRQ specified\n");
1294 goto out_error_unmap;
1297 #ifdef CONFIG_SERIAL_BFIN_DMA
1298 spin_lock_init(&uart->rx_lock);
1302 res = platform_get_resource(pdev, IORESOURCE_DMA, 0);
1304 dev_err(&pdev->dev, "No uart TX DMA channel specified\n");
1306 goto out_error_unmap;
1308 uart->tx_dma_channel = res->start;
1310 res = platform_get_resource(pdev, IORESOURCE_DMA, 1);
1312 dev_err(&pdev->dev, "No uart RX DMA channel specified\n");
1314 goto out_error_unmap;
1316 uart->rx_dma_channel = res->start;
1318 init_timer(&(uart->rx_dma_timer));
1321 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1322 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1323 res = platform_get_resource(pdev, IORESOURCE_IO, 0);
1327 uart->cts_pin = res->start;
1329 res = platform_get_resource(pdev, IORESOURCE_IO, 1);
1333 uart->rts_pin = res->start;
1337 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1338 if (!is_early_platform_device(pdev)) {
1340 uart = bfin_serial_ports[pdev->id];
1341 uart->port.dev = &pdev->dev;
1342 dev_set_drvdata(&pdev->dev, uart);
1343 ret = uart_add_one_port(&bfin_serial_reg, &uart->port);
1344 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1353 iounmap(uart->port.membase);
1354 out_error_free_peripherals:
1355 peripheral_free_list(dev_get_platdata(&pdev->dev));
1358 bfin_serial_ports[pdev->id] = NULL;
1364 static int bfin_serial_remove(struct platform_device *pdev)
1366 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1368 dev_set_drvdata(&pdev->dev, NULL);
1371 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1372 iounmap(uart->port.membase);
1373 peripheral_free_list(dev_get_platdata(&pdev->dev));
1375 bfin_serial_ports[pdev->id] = NULL;
1381 static struct platform_driver bfin_serial_driver = {
1382 .probe = bfin_serial_probe,
1383 .remove = bfin_serial_remove,
1384 .suspend = bfin_serial_suspend,
1385 .resume = bfin_serial_resume,
1387 .name = DRIVER_NAME,
1388 .owner = THIS_MODULE,
1392 #if defined(CONFIG_SERIAL_BFIN_CONSOLE)
1393 static struct early_platform_driver early_bfin_serial_driver __initdata = {
1394 .class_str = CLASS_BFIN_CONSOLE,
1395 .pdrv = &bfin_serial_driver,
1396 .requested_id = EARLY_PLATFORM_ID_UNSET,
1399 static int __init bfin_serial_rs_console_init(void)
1401 early_platform_driver_register(&early_bfin_serial_driver, DRIVER_NAME);
1403 early_platform_driver_probe(CLASS_BFIN_CONSOLE, BFIN_UART_NR_PORTS, 0);
1405 register_console(&bfin_serial_console);
1409 console_initcall(bfin_serial_rs_console_init);
1412 #ifdef CONFIG_EARLY_PRINTK
1414 * Memory can't be allocated dynamically during earlyprink init stage.
1415 * So, do individual probe for earlyprink with a static uart port variable.
1417 static int bfin_earlyprintk_probe(struct platform_device *pdev)
1419 struct resource *res;
1422 if (pdev->id < 0 || pdev->id >= BFIN_UART_NR_PORTS) {
1423 dev_err(&pdev->dev, "Wrong earlyprintk platform device id.\n");
1427 ret = peripheral_request_list(dev_get_platdata(&pdev->dev),
1431 "fail to request bfin serial peripherals\n");
1435 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1437 dev_err(&pdev->dev, "Cannot get IORESOURCE_MEM\n");
1439 goto out_error_free_peripherals;
1442 bfin_earlyprintk_port.port.membase = ioremap(res->start,
1443 resource_size(res));
1444 if (!bfin_earlyprintk_port.port.membase) {
1445 dev_err(&pdev->dev, "Cannot map uart IO\n");
1447 goto out_error_free_peripherals;
1449 bfin_earlyprintk_port.port.mapbase = res->start;
1450 bfin_earlyprintk_port.port.line = pdev->id;
1451 bfin_earlyprintk_port.port.uartclk = get_sclk();
1452 bfin_earlyprintk_port.port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1453 spin_lock_init(&bfin_earlyprintk_port.port.lock);
1457 out_error_free_peripherals:
1458 peripheral_free_list(dev_get_platdata(&pdev->dev));
1463 static struct platform_driver bfin_earlyprintk_driver = {
1464 .probe = bfin_earlyprintk_probe,
1466 .name = DRIVER_NAME,
1467 .owner = THIS_MODULE,
1471 static struct early_platform_driver early_bfin_earlyprintk_driver __initdata = {
1472 .class_str = CLASS_BFIN_EARLYPRINTK,
1473 .pdrv = &bfin_earlyprintk_driver,
1474 .requested_id = EARLY_PLATFORM_ID_UNSET,
1477 struct console __init *bfin_earlyserial_init(unsigned int port,
1483 if (port < 0 || port >= BFIN_UART_NR_PORTS)
1487 * Only probe resource of the given port in earlyprintk boot arg.
1488 * The expected port id should be indicated in port name string.
1490 snprintf(port_name, 20, DRIVER_NAME ".%d", port);
1491 early_platform_driver_register(&early_bfin_earlyprintk_driver,
1493 early_platform_driver_probe(CLASS_BFIN_EARLYPRINTK, 1, 0);
1495 if (!bfin_earlyprintk_port.port.membase)
1498 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1500 * If we are using early serial, don't let the normal console rewind
1501 * log buffer, since that causes things to be printed multiple times
1503 bfin_serial_console.flags &= ~CON_PRINTBUFFER;
1506 bfin_early_serial_console.index = port;
1512 bfin_serial_set_termios(&bfin_earlyprintk_port.port, &t, &t);
1514 return &bfin_early_serial_console;
1516 #endif /* CONFIG_EARLY_PRINTK */
1518 static int __init bfin_serial_init(void)
1522 pr_info("Blackfin serial driver\n");
1524 ret = uart_register_driver(&bfin_serial_reg);
1526 pr_err("failed to register %s:%d\n",
1527 bfin_serial_reg.driver_name, ret);
1530 ret = platform_driver_register(&bfin_serial_driver);
1532 pr_err("fail to register bfin uart\n");
1533 uart_unregister_driver(&bfin_serial_reg);
1539 static void __exit bfin_serial_exit(void)
1541 platform_driver_unregister(&bfin_serial_driver);
1542 uart_unregister_driver(&bfin_serial_reg);
1546 module_init(bfin_serial_init);
1547 module_exit(bfin_serial_exit);
1549 MODULE_AUTHOR("Sonic Zhang, Aubrey Li");
1550 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1551 MODULE_LICENSE("GPL");
1552 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1553 MODULE_ALIAS("platform:bfin-uart");