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 #ifdef CONFIG_SERIAL_BFIN_MODULE
46 # undef CONFIG_EARLY_PRINTK
49 /* UART name and device definitions */
50 #define BFIN_SERIAL_NAME "ttyBF"
51 #define BFIN_SERIAL_MAJOR 204
52 #define BFIN_SERIAL_MINOR 64
54 static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
55 static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
57 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
58 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
60 # ifndef CONFIG_SERIAL_BFIN_PIO
61 # error KGDB only support UART in PIO mode.
64 static int kgdboc_port_line;
65 static int kgdboc_break_enabled;
68 * Setup for console. Argument comes from the menuconfig
70 #define DMA_RX_XCOUNT 512
71 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
73 #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
75 #ifdef CONFIG_SERIAL_BFIN_DMA
76 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
78 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
81 static void bfin_serial_reset_irda(struct uart_port *port);
83 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
84 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
85 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
87 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
88 if (uart->cts_pin < 0)
89 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
91 /* CTS PIN is negative assertive. */
92 if (UART_GET_CTS(uart))
93 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
95 return TIOCM_DSR | TIOCM_CAR;
98 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
100 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
101 if (uart->rts_pin < 0)
104 /* RTS PIN is negative assertive. */
105 if (mctrl & TIOCM_RTS)
106 UART_ENABLE_RTS(uart);
108 UART_DISABLE_RTS(uart);
112 * Handle any change of modem status signal.
114 static irqreturn_t bfin_serial_mctrl_cts_int(int irq, void *dev_id)
116 struct bfin_serial_port *uart = dev_id;
119 status = bfin_serial_get_mctrl(&uart->port);
120 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
121 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
123 UART_CLEAR_SCTS(uart);
124 UART_CLEAR_IER(uart, EDSSI);
130 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
132 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
135 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
141 * interrupts are disabled on entry
143 static void bfin_serial_stop_tx(struct uart_port *port)
145 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
146 #ifdef CONFIG_SERIAL_BFIN_DMA
147 struct circ_buf *xmit = &uart->port.state->xmit;
150 while (!(UART_GET_LSR(uart) & TEMT))
153 #ifdef CONFIG_SERIAL_BFIN_DMA
154 disable_dma(uart->tx_dma_channel);
155 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
156 uart->port.icount.tx += uart->tx_count;
162 UART_PUT_LSR(uart, TFI);
164 UART_CLEAR_IER(uart, ETBEI);
169 * port is locked and interrupts are disabled
171 static void bfin_serial_start_tx(struct uart_port *port)
173 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
174 struct tty_struct *tty = uart->port.state->port.tty;
176 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
177 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
179 uart_handle_cts_change(&uart->port, uart->scts);
184 * To avoid losting RX interrupt, we reset IR function
185 * before sending data.
187 if (tty->termios->c_line == N_IRDA)
188 bfin_serial_reset_irda(port);
190 #ifdef CONFIG_SERIAL_BFIN_DMA
192 bfin_serial_dma_tx_chars(uart);
194 UART_SET_IER(uart, ETBEI);
195 bfin_serial_tx_chars(uart);
200 * Interrupts are enabled
202 static void bfin_serial_stop_rx(struct uart_port *port)
204 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
206 UART_CLEAR_IER(uart, ERBFI);
210 * Set the modem control timer to fire immediately.
212 static void bfin_serial_enable_ms(struct uart_port *port)
217 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
218 # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
219 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
221 # define UART_GET_ANOMALY_THRESHOLD(uart) 0
222 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
225 #ifdef CONFIG_SERIAL_BFIN_PIO
226 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
228 struct tty_struct *tty = NULL;
229 unsigned int status, ch, flg;
230 static struct timeval anomaly_start = { .tv_sec = 0 };
232 status = UART_GET_LSR(uart);
233 UART_CLEAR_LSR(uart);
235 ch = UART_GET_CHAR(uart);
236 uart->port.icount.rx++;
238 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
239 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
240 if (kgdb_connected && kgdboc_port_line == uart->port.line)
241 if (ch == 0x3) {/* Ctrl + C */
246 if (!uart->port.state || !uart->port.state->port.tty)
249 tty = uart->port.state->port.tty;
251 if (ANOMALY_05000363) {
252 /* The BF533 (and BF561) family of processors have a nice anomaly
253 * where they continuously generate characters for a "single" break.
254 * We have to basically ignore this flood until the "next" valid
255 * character comes across. Due to the nature of the flood, it is
256 * not possible to reliably catch bytes that are sent too quickly
257 * after this break. So application code talking to the Blackfin
258 * which sends a break signal must allow at least 1.5 character
259 * times after the end of the break for things to stabilize. This
260 * timeout was picked as it must absolutely be larger than 1
261 * character time +/- some percent. So 1.5 sounds good. All other
262 * Blackfin families operate properly. Woo.
264 if (anomaly_start.tv_sec) {
268 if ((~ch & (~ch + 1)) & 0xff)
269 goto known_good_char;
271 do_gettimeofday(&curr);
272 if (curr.tv_sec - anomaly_start.tv_sec > 1)
273 goto known_good_char;
276 if (curr.tv_sec != anomaly_start.tv_sec)
277 usecs += USEC_PER_SEC;
278 usecs += curr.tv_usec - anomaly_start.tv_usec;
280 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
281 goto known_good_char;
284 anomaly_start.tv_sec = 0;
286 anomaly_start = curr;
292 anomaly_start.tv_sec = 0;
297 if (ANOMALY_05000363)
298 if (bfin_revid() < 5)
299 do_gettimeofday(&anomaly_start);
300 uart->port.icount.brk++;
301 if (uart_handle_break(&uart->port))
303 status &= ~(PE | FE);
306 uart->port.icount.parity++;
308 uart->port.icount.overrun++;
310 uart->port.icount.frame++;
312 status &= uart->port.read_status_mask;
316 else if (status & PE)
318 else if (status & FE)
323 if (uart_handle_sysrq_char(&uart->port, ch))
326 uart_insert_char(&uart->port, status, OE, ch, flg);
329 tty_flip_buffer_push(tty);
332 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
334 struct circ_buf *xmit = &uart->port.state->xmit;
336 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
339 UART_PUT_LSR(uart, TFI);
342 * 05000215 - we always clear ETBEI within last UART TX
343 * interrupt to end a string. It is always set
344 * when start a new tx.
346 UART_CLEAR_IER(uart, ETBEI);
350 if (uart->port.x_char) {
351 UART_PUT_CHAR(uart, uart->port.x_char);
352 uart->port.icount.tx++;
353 uart->port.x_char = 0;
356 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
357 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
358 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
359 uart->port.icount.tx++;
363 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
364 uart_write_wakeup(&uart->port);
367 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
369 struct bfin_serial_port *uart = dev_id;
371 spin_lock(&uart->port.lock);
372 while (UART_GET_LSR(uart) & DR)
373 bfin_serial_rx_chars(uart);
374 spin_unlock(&uart->port.lock);
379 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
381 struct bfin_serial_port *uart = dev_id;
383 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
384 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port) & TIOCM_CTS)) {
386 uart_handle_cts_change(&uart->port, uart->scts);
389 spin_lock(&uart->port.lock);
390 if (UART_GET_LSR(uart) & THRE)
391 bfin_serial_tx_chars(uart);
392 spin_unlock(&uart->port.lock);
398 #ifdef CONFIG_SERIAL_BFIN_DMA
399 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
401 struct circ_buf *xmit = &uart->port.state->xmit;
405 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
411 if (uart->port.x_char) {
412 UART_PUT_CHAR(uart, uart->port.x_char);
413 uart->port.icount.tx++;
414 uart->port.x_char = 0;
417 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
418 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
419 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
420 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
421 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
422 set_dma_config(uart->tx_dma_channel,
423 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
428 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
429 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
430 set_dma_x_modify(uart->tx_dma_channel, 1);
432 enable_dma(uart->tx_dma_channel);
434 UART_SET_IER(uart, ETBEI);
437 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
439 struct tty_struct *tty = uart->port.state->port.tty;
442 status = UART_GET_LSR(uart);
443 UART_CLEAR_LSR(uart);
445 uart->port.icount.rx +=
446 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
450 uart->port.icount.brk++;
451 if (uart_handle_break(&uart->port))
452 goto dma_ignore_char;
453 status &= ~(PE | FE);
456 uart->port.icount.parity++;
458 uart->port.icount.overrun++;
460 uart->port.icount.frame++;
462 status &= uart->port.read_status_mask;
466 else if (status & PE)
468 else if (status & FE)
473 for (i = uart->rx_dma_buf.tail; ; i++) {
474 if (i >= UART_XMIT_SIZE)
476 if (i == uart->rx_dma_buf.head)
478 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
479 uart_insert_char(&uart->port, status, OE,
480 uart->rx_dma_buf.buf[i], flg);
484 tty_flip_buffer_push(tty);
487 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
491 dma_disable_irq(uart->tx_dma_channel);
492 dma_disable_irq(uart->rx_dma_channel);
493 spin_lock_bh(&uart->port.lock);
495 /* 2D DMA RX buffer ring is used. Because curr_y_count and
496 * curr_x_count can't be read as an atomic operation,
497 * curr_y_count should be read before curr_x_count. When
498 * curr_x_count is read, curr_y_count may already indicate
499 * next buffer line. But, the position calculated here is
500 * still indicate the old line. The wrong position data may
501 * be smaller than current buffer tail, which cause garbages
502 * are received if it is not prohibit.
504 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
505 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
506 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
507 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
508 uart->rx_dma_nrows = 0;
509 x_pos = DMA_RX_XCOUNT - x_pos;
510 if (x_pos == DMA_RX_XCOUNT)
513 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
514 /* Ignore receiving data if new position is in the same line of
515 * current buffer tail and small.
517 if (pos > uart->rx_dma_buf.tail ||
518 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
519 uart->rx_dma_buf.head = pos;
520 bfin_serial_dma_rx_chars(uart);
521 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
524 spin_unlock_bh(&uart->port.lock);
525 dma_enable_irq(uart->tx_dma_channel);
526 dma_enable_irq(uart->rx_dma_channel);
528 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
531 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
533 struct bfin_serial_port *uart = dev_id;
534 struct circ_buf *xmit = &uart->port.state->xmit;
536 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
537 if (uart->scts && !(bfin_serial_get_mctrl(&uart->port)&TIOCM_CTS)) {
539 uart_handle_cts_change(&uart->port, uart->scts);
543 spin_lock(&uart->port.lock);
544 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
545 disable_dma(uart->tx_dma_channel);
546 clear_dma_irqstat(uart->tx_dma_channel);
548 * 05000215 - we always clear ETBEI within last UART TX
549 * interrupt to end a string. It is always set
550 * when start a new tx.
552 UART_CLEAR_IER(uart, ETBEI);
553 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
554 uart->port.icount.tx += uart->tx_count;
556 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
557 uart_write_wakeup(&uart->port);
559 bfin_serial_dma_tx_chars(uart);
562 spin_unlock(&uart->port.lock);
566 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
568 struct bfin_serial_port *uart = dev_id;
569 unsigned short irqstat;
572 spin_lock(&uart->port.lock);
573 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
574 clear_dma_irqstat(uart->rx_dma_channel);
576 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
577 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
578 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
579 if (uart->rx_dma_nrows == DMA_RX_YCOUNT || x_pos == 0)
580 uart->rx_dma_nrows = 0;
582 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT;
583 if (pos > uart->rx_dma_buf.tail ||
584 uart->rx_dma_nrows < (uart->rx_dma_buf.tail/DMA_RX_XCOUNT)) {
585 uart->rx_dma_buf.head = pos;
586 bfin_serial_dma_rx_chars(uart);
587 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
590 spin_unlock(&uart->port.lock);
597 * Return TIOCSER_TEMT when transmitter is not busy.
599 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
601 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
604 lsr = UART_GET_LSR(uart);
611 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
613 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
614 u16 lcr = UART_GET_LCR(uart);
619 UART_PUT_LCR(uart, lcr);
623 static int bfin_serial_startup(struct uart_port *port)
625 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
627 #ifdef CONFIG_SERIAL_BFIN_DMA
628 dma_addr_t dma_handle;
630 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
631 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
635 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
636 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
637 free_dma(uart->rx_dma_channel);
641 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
642 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
644 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
645 uart->rx_dma_buf.head = 0;
646 uart->rx_dma_buf.tail = 0;
647 uart->rx_dma_nrows = 0;
649 set_dma_config(uart->rx_dma_channel,
650 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
651 INTR_ON_ROW, DIMENSION_2D,
654 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
655 set_dma_x_modify(uart->rx_dma_channel, 1);
656 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
657 set_dma_y_modify(uart->rx_dma_channel, 1);
658 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
659 enable_dma(uart->rx_dma_channel);
661 uart->rx_dma_timer.data = (unsigned long)(uart);
662 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
663 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
664 add_timer(&(uart->rx_dma_timer));
666 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
667 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
668 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
669 kgdboc_break_enabled = 0;
672 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
673 "BFIN_UART_RX", uart)) {
674 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
679 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
680 "BFIN_UART_TX", uart)) {
681 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
682 free_irq(uart->port.irq, uart);
688 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
690 switch (uart->port.irq) {
692 uart_dma_ch_rx = CH_UART3_RX;
693 uart_dma_ch_tx = CH_UART3_TX;
696 uart_dma_ch_rx = CH_UART2_RX;
697 uart_dma_ch_tx = CH_UART2_TX;
700 uart_dma_ch_rx = uart_dma_ch_tx = 0;
704 if (uart_dma_ch_rx &&
705 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
706 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
707 free_irq(uart->port.irq, uart);
708 free_irq(uart->port.irq + 1, uart);
711 if (uart_dma_ch_tx &&
712 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
713 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
714 free_dma(uart_dma_ch_rx);
715 free_irq(uart->port.irq, uart);
716 free_irq(uart->port.irq + 1, uart);
721 # if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
722 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
727 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
728 if (uart->cts_pin >= 0) {
729 if (request_irq(gpio_to_irq(uart->cts_pin),
730 bfin_serial_mctrl_cts_int,
731 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING |
732 IRQF_DISABLED, "BFIN_UART_CTS", uart)) {
734 pr_info("Unable to attach BlackFin UART CTS interrupt.\
738 if (uart->rts_pin >= 0) {
739 gpio_request(uart->rts_pin, DRIVER_NAME);
740 gpio_direction_output(uart->rts_pin, 0);
743 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
744 if (request_irq(uart->status_irq,
745 bfin_serial_mctrl_cts_int,
746 IRQF_DISABLED, "BFIN_UART_MODEM_STATUS", uart)) {
747 pr_info("Unable to attach BlackFin UART Modem \
748 Status interrupt.\n");
751 /* CTS RTS PINs are negative assertive. */
752 UART_PUT_MCR(uart, ACTS);
753 UART_SET_IER(uart, EDSSI);
756 UART_SET_IER(uart, ERBFI);
760 static void bfin_serial_shutdown(struct uart_port *port)
762 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
764 #ifdef CONFIG_SERIAL_BFIN_DMA
765 disable_dma(uart->tx_dma_channel);
766 free_dma(uart->tx_dma_channel);
767 disable_dma(uart->rx_dma_channel);
768 free_dma(uart->rx_dma_channel);
769 del_timer(&(uart->rx_dma_timer));
770 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
773 switch (uart->port.irq) {
775 free_dma(CH_UART3_RX);
776 free_dma(CH_UART3_TX);
779 free_dma(CH_UART2_RX);
780 free_dma(CH_UART2_TX);
786 free_irq(uart->port.irq, uart);
787 free_irq(uart->port.irq+1, uart);
790 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
791 if (uart->cts_pin >= 0)
792 free_irq(gpio_to_irq(uart->cts_pin), uart);
793 if (uart->rts_pin >= 0)
794 gpio_free(uart->rts_pin);
796 #ifdef CONFIG_SERIAL_BFIN_HARD_CTSRTS
797 if (UART_GET_IER(uart) && EDSSI)
798 free_irq(uart->status_irq, uart);
803 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
804 struct ktermios *old)
806 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
808 unsigned int baud, quot;
809 unsigned short val, ier, lcr = 0;
811 switch (termios->c_cflag & CSIZE) {
825 printk(KERN_ERR "%s: word lengh not supported\n",
830 * 05000231 - STOP bit is always set to 1 whatever the user is set.
832 if (termios->c_cflag & CSTOPB) {
833 if (ANOMALY_05000231)
834 printk(KERN_WARNING "STOP bits other than 1 is not "
835 "supported in case of anomaly 05000231.\n");
839 if (termios->c_cflag & PARENB)
841 if (!(termios->c_cflag & PARODD))
843 if (termios->c_cflag & CMSPAR)
846 port->read_status_mask = OE;
847 if (termios->c_iflag & INPCK)
848 port->read_status_mask |= (FE | PE);
849 if (termios->c_iflag & (BRKINT | PARMRK))
850 port->read_status_mask |= BI;
853 * Characters to ignore
855 port->ignore_status_mask = 0;
856 if (termios->c_iflag & IGNPAR)
857 port->ignore_status_mask |= FE | PE;
858 if (termios->c_iflag & IGNBRK) {
859 port->ignore_status_mask |= BI;
861 * If we're ignoring parity and break indicators,
862 * ignore overruns too (for real raw support).
864 if (termios->c_iflag & IGNPAR)
865 port->ignore_status_mask |= OE;
868 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
869 quot = uart_get_divisor(port, baud) - ANOMALY_05000230;
870 spin_lock_irqsave(&uart->port.lock, flags);
872 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
875 ier = UART_GET_IER(uart);
876 UART_DISABLE_INTS(uart);
878 /* Set DLAB in LCR to Access DLL and DLH */
881 UART_PUT_DLL(uart, quot & 0xFF);
882 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
885 /* Clear DLAB in LCR to Access THR RBR IER */
886 UART_CLEAR_DLAB(uart);
888 UART_PUT_LCR(uart, lcr);
891 UART_ENABLE_INTS(uart, ier);
893 val = UART_GET_GCTL(uart);
895 UART_PUT_GCTL(uart, val);
897 /* Port speed changed, update the per-port timeout. */
898 uart_update_timeout(port, termios->c_cflag, baud);
900 spin_unlock_irqrestore(&uart->port.lock, flags);
903 static const char *bfin_serial_type(struct uart_port *port)
905 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
907 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
911 * Release the memory region(s) being used by 'port'.
913 static void bfin_serial_release_port(struct uart_port *port)
918 * Request the memory region(s) being used by 'port'.
920 static int bfin_serial_request_port(struct uart_port *port)
926 * Configure/autoconfigure the port.
928 static void bfin_serial_config_port(struct uart_port *port, int flags)
930 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
932 if (flags & UART_CONFIG_TYPE &&
933 bfin_serial_request_port(&uart->port) == 0)
934 uart->port.type = PORT_BFIN;
938 * Verify the new serial_struct (for TIOCSSERIAL).
939 * The only change we allow are to the flags and type, and
940 * even then only between PORT_BFIN and PORT_UNKNOWN
943 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
949 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
950 * In other cases, disable IrDA function.
952 static void bfin_serial_set_ldisc(struct uart_port *port)
954 int line = port->line;
957 if (line >= port->state->port.tty->driver->num)
960 switch (port->state->port.tty->termios->c_line) {
962 val = UART_GET_GCTL(&bfin_serial_ports[line]);
963 val |= (IREN | RPOLC);
964 UART_PUT_GCTL(&bfin_serial_ports[line], val);
967 val = UART_GET_GCTL(&bfin_serial_ports[line]);
968 val &= ~(IREN | RPOLC);
969 UART_PUT_GCTL(&bfin_serial_ports[line], val);
973 static void bfin_serial_reset_irda(struct uart_port *port)
975 int line = port->line;
978 val = UART_GET_GCTL(&bfin_serial_ports[line]);
979 val &= ~(IREN | RPOLC);
980 UART_PUT_GCTL(&bfin_serial_ports[line], val);
982 val |= (IREN | RPOLC);
983 UART_PUT_GCTL(&bfin_serial_ports[line], val);
987 #ifdef CONFIG_CONSOLE_POLL
989 * 05000099 - Because we only use THRE in poll_put and DR in poll_get,
990 * losing other bits of UART_LSR is not a problem here.
992 static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
994 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
996 while (!(UART_GET_LSR(uart) & THRE))
999 UART_CLEAR_DLAB(uart);
1000 UART_PUT_CHAR(uart, (unsigned char)chr);
1003 static int bfin_serial_poll_get_char(struct uart_port *port)
1005 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1008 while (!(UART_GET_LSR(uart) & DR))
1011 UART_CLEAR_DLAB(uart);
1012 chr = UART_GET_CHAR(uart);
1018 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1019 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1020 static void bfin_kgdboc_port_shutdown(struct uart_port *port)
1022 if (kgdboc_break_enabled) {
1023 kgdboc_break_enabled = 0;
1024 bfin_serial_shutdown(port);
1028 static int bfin_kgdboc_port_startup(struct uart_port *port)
1030 kgdboc_port_line = port->line;
1031 kgdboc_break_enabled = !bfin_serial_startup(port);
1036 static struct uart_ops bfin_serial_pops = {
1037 .tx_empty = bfin_serial_tx_empty,
1038 .set_mctrl = bfin_serial_set_mctrl,
1039 .get_mctrl = bfin_serial_get_mctrl,
1040 .stop_tx = bfin_serial_stop_tx,
1041 .start_tx = bfin_serial_start_tx,
1042 .stop_rx = bfin_serial_stop_rx,
1043 .enable_ms = bfin_serial_enable_ms,
1044 .break_ctl = bfin_serial_break_ctl,
1045 .startup = bfin_serial_startup,
1046 .shutdown = bfin_serial_shutdown,
1047 .set_termios = bfin_serial_set_termios,
1048 .set_ldisc = bfin_serial_set_ldisc,
1049 .type = bfin_serial_type,
1050 .release_port = bfin_serial_release_port,
1051 .request_port = bfin_serial_request_port,
1052 .config_port = bfin_serial_config_port,
1053 .verify_port = bfin_serial_verify_port,
1054 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
1055 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
1056 .kgdboc_port_startup = bfin_kgdboc_port_startup,
1057 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
1059 #ifdef CONFIG_CONSOLE_POLL
1060 .poll_put_char = bfin_serial_poll_put_char,
1061 .poll_get_char = bfin_serial_poll_get_char,
1065 static void __init bfin_serial_hw_init(void)
1067 #ifdef CONFIG_SERIAL_BFIN_UART0
1068 peripheral_request(P_UART0_TX, DRIVER_NAME);
1069 peripheral_request(P_UART0_RX, DRIVER_NAME);
1072 #ifdef CONFIG_SERIAL_BFIN_UART1
1073 peripheral_request(P_UART1_TX, DRIVER_NAME);
1074 peripheral_request(P_UART1_RX, DRIVER_NAME);
1076 # if defined(CONFIG_BFIN_UART1_CTSRTS) && defined(CONFIG_BF54x)
1077 peripheral_request(P_UART1_RTS, DRIVER_NAME);
1078 peripheral_request(P_UART1_CTS, DRIVER_NAME);
1082 #ifdef CONFIG_SERIAL_BFIN_UART2
1083 peripheral_request(P_UART2_TX, DRIVER_NAME);
1084 peripheral_request(P_UART2_RX, DRIVER_NAME);
1087 #ifdef CONFIG_SERIAL_BFIN_UART3
1088 peripheral_request(P_UART3_TX, DRIVER_NAME);
1089 peripheral_request(P_UART3_RX, DRIVER_NAME);
1091 # if defined(CONFIG_BFIN_UART3_CTSRTS) && defined(CONFIG_BF54x)
1092 peripheral_request(P_UART3_RTS, DRIVER_NAME);
1093 peripheral_request(P_UART3_CTS, DRIVER_NAME);
1098 static void __init bfin_serial_init_ports(void)
1100 static int first = 1;
1107 bfin_serial_hw_init();
1109 for (i = 0; i < nr_active_ports; i++) {
1110 spin_lock_init(&bfin_serial_ports[i].port.lock);
1111 bfin_serial_ports[i].port.uartclk = get_sclk();
1112 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
1113 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
1114 bfin_serial_ports[i].port.line = i;
1115 bfin_serial_ports[i].port.iotype = UPIO_MEM;
1116 bfin_serial_ports[i].port.membase =
1117 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
1118 bfin_serial_ports[i].port.mapbase =
1119 bfin_serial_resource[i].uart_base_addr;
1120 bfin_serial_ports[i].port.irq =
1121 bfin_serial_resource[i].uart_irq;
1122 bfin_serial_ports[i].status_irq =
1123 bfin_serial_resource[i].uart_status_irq;
1124 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
1125 #ifdef CONFIG_SERIAL_BFIN_DMA
1126 bfin_serial_ports[i].tx_done = 1;
1127 bfin_serial_ports[i].tx_count = 0;
1128 bfin_serial_ports[i].tx_dma_channel =
1129 bfin_serial_resource[i].uart_tx_dma_channel;
1130 bfin_serial_ports[i].rx_dma_channel =
1131 bfin_serial_resource[i].uart_rx_dma_channel;
1132 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
1134 #if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1135 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1136 bfin_serial_ports[i].cts_pin =
1137 bfin_serial_resource[i].uart_cts_pin;
1138 bfin_serial_ports[i].rts_pin =
1139 bfin_serial_resource[i].uart_rts_pin;
1144 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1146 * If the port was already initialised (eg, by a boot loader),
1147 * try to determine the current setup.
1150 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1151 int *parity, int *bits)
1153 unsigned short status;
1155 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1156 if (status == (ERBFI | ETBEI)) {
1157 /* ok, the port was enabled */
1160 lcr = UART_GET_LCR(uart);
1169 switch (lcr & 0x03) {
1170 case 0: *bits = 5; break;
1171 case 1: *bits = 6; break;
1172 case 2: *bits = 7; break;
1173 case 3: *bits = 8; break;
1175 /* Set DLAB in LCR to Access DLL and DLH */
1176 UART_SET_DLAB(uart);
1178 dll = UART_GET_DLL(uart);
1179 dlh = UART_GET_DLH(uart);
1181 /* Clear DLAB in LCR to Access THR RBR IER */
1182 UART_CLEAR_DLAB(uart);
1184 *baud = get_sclk() / (16*(dll | dlh << 8));
1186 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1189 static struct uart_driver bfin_serial_reg;
1192 bfin_serial_console_setup(struct console *co, char *options)
1194 struct bfin_serial_port *uart;
1198 # if defined(CONFIG_SERIAL_BFIN_CTSRTS) || \
1199 defined(CONFIG_SERIAL_BFIN_HARD_CTSRTS)
1206 * Check whether an invalid uart number has been specified, and
1207 * if so, search for the first available port that does have
1210 if (co->index == -1 || co->index >= nr_active_ports)
1212 uart = &bfin_serial_ports[co->index];
1215 uart_parse_options(options, &baud, &parity, &bits, &flow);
1217 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1219 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1221 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1222 defined (CONFIG_EARLY_PRINTK) */
1224 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1225 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1227 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1228 while (!(UART_GET_LSR(uart) & THRE))
1230 UART_PUT_CHAR(uart, ch);
1235 * Interrupts are disabled on entering
1238 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1240 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1241 unsigned long flags;
1243 spin_lock_irqsave(&uart->port.lock, flags);
1244 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1245 spin_unlock_irqrestore(&uart->port.lock, flags);
1249 static struct console bfin_serial_console = {
1250 .name = BFIN_SERIAL_NAME,
1251 .write = bfin_serial_console_write,
1252 .device = uart_console_device,
1253 .setup = bfin_serial_console_setup,
1254 .flags = CON_PRINTBUFFER,
1256 .data = &bfin_serial_reg,
1259 static int __init bfin_serial_rs_console_init(void)
1261 bfin_serial_init_ports();
1262 register_console(&bfin_serial_console);
1266 console_initcall(bfin_serial_rs_console_init);
1268 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1270 #define BFIN_SERIAL_CONSOLE NULL
1271 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1274 #ifdef CONFIG_EARLY_PRINTK
1275 static __init void early_serial_putc(struct uart_port *port, int ch)
1277 unsigned timeout = 0xffff;
1278 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1280 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1282 UART_PUT_CHAR(uart, ch);
1285 static __init void early_serial_write(struct console *con, const char *s,
1288 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1291 for (i = 0; i < n; i++, s++) {
1293 early_serial_putc(&uart->port, '\r');
1294 early_serial_putc(&uart->port, *s);
1299 * This should have a .setup or .early_setup in it, but then things get called
1300 * without the command line options, and the baud rate gets messed up - so
1301 * don't let the common infrastructure play with things. (see calls to setup
1302 * & earlysetup in ./kernel/printk.c:register_console()
1304 static struct __initdata console bfin_early_serial_console = {
1305 .name = "early_BFuart",
1306 .write = early_serial_write,
1307 .device = uart_console_device,
1308 .flags = CON_PRINTBUFFER,
1310 .data = &bfin_serial_reg,
1313 struct console __init *bfin_earlyserial_init(unsigned int port,
1316 struct bfin_serial_port *uart;
1319 if (port == -1 || port >= nr_active_ports)
1321 bfin_serial_init_ports();
1322 bfin_early_serial_console.index = port;
1323 uart = &bfin_serial_ports[port];
1329 bfin_serial_set_termios(&uart->port, &t, &t);
1330 return &bfin_early_serial_console;
1333 #endif /* CONFIG_EARLY_PRINTK */
1335 static struct uart_driver bfin_serial_reg = {
1336 .owner = THIS_MODULE,
1337 .driver_name = "bfin-uart",
1338 .dev_name = BFIN_SERIAL_NAME,
1339 .major = BFIN_SERIAL_MAJOR,
1340 .minor = BFIN_SERIAL_MINOR,
1341 .nr = BFIN_UART_NR_PORTS,
1342 .cons = BFIN_SERIAL_CONSOLE,
1345 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1349 for (i = 0; i < nr_active_ports; i++) {
1350 if (bfin_serial_ports[i].port.dev != &dev->dev)
1352 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1358 static int bfin_serial_resume(struct platform_device *dev)
1362 for (i = 0; i < nr_active_ports; i++) {
1363 if (bfin_serial_ports[i].port.dev != &dev->dev)
1365 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1371 static int bfin_serial_probe(struct platform_device *dev)
1373 struct resource *res = dev->resource;
1376 for (i = 0; i < dev->num_resources; i++, res++)
1377 if (res->flags & IORESOURCE_MEM)
1380 if (i < dev->num_resources) {
1381 for (i = 0; i < nr_active_ports; i++, res++) {
1382 if (bfin_serial_ports[i].port.mapbase != res->start)
1384 bfin_serial_ports[i].port.dev = &dev->dev;
1385 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1392 static int bfin_serial_remove(struct platform_device *dev)
1396 for (i = 0; i < nr_active_ports; i++) {
1397 if (bfin_serial_ports[i].port.dev != &dev->dev)
1399 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1400 bfin_serial_ports[i].port.dev = NULL;
1401 #if defined(CONFIG_SERIAL_BFIN_CTSRTS)
1402 gpio_free(bfin_serial_ports[i].cts_pin);
1403 gpio_free(bfin_serial_ports[i].rts_pin);
1410 static struct platform_driver bfin_serial_driver = {
1411 .probe = bfin_serial_probe,
1412 .remove = bfin_serial_remove,
1413 .suspend = bfin_serial_suspend,
1414 .resume = bfin_serial_resume,
1416 .name = "bfin-uart",
1417 .owner = THIS_MODULE,
1421 static int __init bfin_serial_init(void)
1425 pr_info("Serial: Blackfin serial driver\n");
1427 bfin_serial_init_ports();
1429 ret = uart_register_driver(&bfin_serial_reg);
1431 ret = platform_driver_register(&bfin_serial_driver);
1433 pr_debug("uart register failed\n");
1434 uart_unregister_driver(&bfin_serial_reg);
1440 static void __exit bfin_serial_exit(void)
1442 platform_driver_unregister(&bfin_serial_driver);
1443 uart_unregister_driver(&bfin_serial_reg);
1447 module_init(bfin_serial_init);
1448 module_exit(bfin_serial_exit);
1450 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1451 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1452 MODULE_LICENSE("GPL");
1453 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1454 MODULE_ALIAS("platform:bfin-uart");