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 /* UART name and device definitions */
42 #define BFIN_SERIAL_NAME "ttyBF"
43 #define BFIN_SERIAL_MAJOR 204
44 #define BFIN_SERIAL_MINOR 64
46 static struct bfin_serial_port bfin_serial_ports[BFIN_UART_NR_PORTS];
47 static int nr_active_ports = ARRAY_SIZE(bfin_serial_resource);
49 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
50 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
52 # ifndef CONFIG_SERIAL_BFIN_PIO
53 # error KGDB only support UART in PIO mode.
56 static int kgdboc_port_line;
57 static int kgdboc_break_enabled;
60 * Setup for console. Argument comes from the menuconfig
62 #define DMA_RX_XCOUNT 512
63 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
65 #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
66 #define CTS_CHECK_JIFFIES (HZ / 50)
68 #ifdef CONFIG_SERIAL_BFIN_DMA
69 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
71 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
74 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
76 static void bfin_serial_reset_irda(struct uart_port *port);
79 * interrupts are disabled on entry
81 static void bfin_serial_stop_tx(struct uart_port *port)
83 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
84 struct circ_buf *xmit = &uart->port.info->xmit;
86 while (!(UART_GET_LSR(uart) & TEMT))
89 #ifdef CONFIG_SERIAL_BFIN_DMA
90 disable_dma(uart->tx_dma_channel);
91 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
92 uart->port.icount.tx += uart->tx_count;
98 UART_PUT_LSR(uart, TFI);
100 UART_CLEAR_IER(uart, ETBEI);
105 * port is locked and interrupts are disabled
107 static void bfin_serial_start_tx(struct uart_port *port)
109 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
110 struct tty_struct *tty = uart->port.info->port.tty;
113 * To avoid losting RX interrupt, we reset IR function
114 * before sending data.
116 if (tty->termios->c_line == N_IRDA)
117 bfin_serial_reset_irda(port);
119 #ifdef CONFIG_SERIAL_BFIN_DMA
121 bfin_serial_dma_tx_chars(uart);
123 UART_SET_IER(uart, ETBEI);
124 bfin_serial_tx_chars(uart);
129 * Interrupts are enabled
131 static void bfin_serial_stop_rx(struct uart_port *port)
133 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
135 UART_CLEAR_IER(uart, ERBFI);
139 * Set the modem control timer to fire immediately.
141 static void bfin_serial_enable_ms(struct uart_port *port)
146 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
147 # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
148 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
150 # define UART_GET_ANOMALY_THRESHOLD(uart) 0
151 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
154 #ifdef CONFIG_SERIAL_BFIN_PIO
155 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
157 struct tty_struct *tty = NULL;
158 unsigned int status, ch, flg;
159 static struct timeval anomaly_start = { .tv_sec = 0 };
161 status = UART_GET_LSR(uart);
162 UART_CLEAR_LSR(uart);
164 ch = UART_GET_CHAR(uart);
165 uart->port.icount.rx++;
167 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
168 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
169 if (kgdb_connected && kgdboc_port_line == uart->port.line)
170 if (ch == 0x3) {/* Ctrl + C */
175 if (!uart->port.info || !uart->port.info->tty)
178 tty = uart->port.info->tty;
180 if (ANOMALY_05000363) {
181 /* The BF533 (and BF561) family of processors have a nice anomaly
182 * where they continuously generate characters for a "single" break.
183 * We have to basically ignore this flood until the "next" valid
184 * character comes across. Due to the nature of the flood, it is
185 * not possible to reliably catch bytes that are sent too quickly
186 * after this break. So application code talking to the Blackfin
187 * which sends a break signal must allow at least 1.5 character
188 * times after the end of the break for things to stabilize. This
189 * timeout was picked as it must absolutely be larger than 1
190 * character time +/- some percent. So 1.5 sounds good. All other
191 * Blackfin families operate properly. Woo.
193 if (anomaly_start.tv_sec) {
197 if ((~ch & (~ch + 1)) & 0xff)
198 goto known_good_char;
200 do_gettimeofday(&curr);
201 if (curr.tv_sec - anomaly_start.tv_sec > 1)
202 goto known_good_char;
205 if (curr.tv_sec != anomaly_start.tv_sec)
206 usecs += USEC_PER_SEC;
207 usecs += curr.tv_usec - anomaly_start.tv_usec;
209 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
210 goto known_good_char;
213 anomaly_start.tv_sec = 0;
215 anomaly_start = curr;
220 anomaly_start.tv_sec = 0;
225 if (ANOMALY_05000363)
226 if (bfin_revid() < 5)
227 do_gettimeofday(&anomaly_start);
228 uart->port.icount.brk++;
229 if (uart_handle_break(&uart->port))
231 status &= ~(PE | FE);
234 uart->port.icount.parity++;
236 uart->port.icount.overrun++;
238 uart->port.icount.frame++;
240 status &= uart->port.read_status_mask;
244 else if (status & PE)
246 else if (status & FE)
251 if (uart_handle_sysrq_char(&uart->port, ch))
254 uart_insert_char(&uart->port, status, OE, ch, flg);
257 tty_flip_buffer_push(tty);
260 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
262 struct circ_buf *xmit = &uart->port.info->xmit;
265 * Check the modem control lines before
266 * transmitting anything.
268 bfin_serial_mctrl_check(uart);
270 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
273 UART_PUT_LSR(uart, TFI);
275 UART_CLEAR_IER(uart, ETBEI);
279 if (uart->port.x_char) {
280 UART_PUT_CHAR(uart, uart->port.x_char);
281 uart->port.icount.tx++;
282 uart->port.x_char = 0;
285 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
286 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
287 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
288 uart->port.icount.tx++;
292 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
293 uart_write_wakeup(&uart->port);
296 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
298 struct bfin_serial_port *uart = dev_id;
300 spin_lock(&uart->port.lock);
301 while (UART_GET_LSR(uart) & DR)
302 bfin_serial_rx_chars(uart);
303 spin_unlock(&uart->port.lock);
308 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
310 struct bfin_serial_port *uart = dev_id;
312 spin_lock(&uart->port.lock);
313 if (UART_GET_LSR(uart) & THRE)
314 bfin_serial_tx_chars(uart);
315 spin_unlock(&uart->port.lock);
321 #ifdef CONFIG_SERIAL_BFIN_DMA
322 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
324 struct circ_buf *xmit = &uart->port.info->xmit;
329 * Check the modem control lines before
330 * transmitting anything.
332 bfin_serial_mctrl_check(uart);
334 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
340 if (uart->port.x_char) {
341 UART_PUT_CHAR(uart, uart->port.x_char);
342 uart->port.icount.tx++;
343 uart->port.x_char = 0;
346 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
347 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
348 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
349 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
350 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
351 set_dma_config(uart->tx_dma_channel,
352 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
357 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
358 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
359 set_dma_x_modify(uart->tx_dma_channel, 1);
360 enable_dma(uart->tx_dma_channel);
362 UART_SET_IER(uart, ETBEI);
365 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
367 struct tty_struct *tty = uart->port.info->port.tty;
370 status = UART_GET_LSR(uart);
371 UART_CLEAR_LSR(uart);
373 uart->port.icount.rx +=
374 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
378 uart->port.icount.brk++;
379 if (uart_handle_break(&uart->port))
380 goto dma_ignore_char;
381 status &= ~(PE | FE);
384 uart->port.icount.parity++;
386 uart->port.icount.overrun++;
388 uart->port.icount.frame++;
390 status &= uart->port.read_status_mask;
394 else if (status & PE)
396 else if (status & FE)
401 for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
402 if (i >= UART_XMIT_SIZE)
404 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
405 uart_insert_char(&uart->port, status, OE,
406 uart->rx_dma_buf.buf[i], flg);
410 tty_flip_buffer_push(tty);
413 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
417 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
418 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
419 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
420 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
421 uart->rx_dma_nrows = 0;
422 x_pos = DMA_RX_XCOUNT - x_pos;
423 if (x_pos == DMA_RX_XCOUNT)
426 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
427 if (pos != uart->rx_dma_buf.tail) {
428 uart->rx_dma_buf.head = pos;
429 bfin_serial_dma_rx_chars(uart);
430 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
433 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
436 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
438 struct bfin_serial_port *uart = dev_id;
439 struct circ_buf *xmit = &uart->port.info->xmit;
441 spin_lock(&uart->port.lock);
442 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
443 disable_dma(uart->tx_dma_channel);
444 clear_dma_irqstat(uart->tx_dma_channel);
445 UART_CLEAR_IER(uart, ETBEI);
446 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
447 uart->port.icount.tx += uart->tx_count;
449 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
450 uart_write_wakeup(&uart->port);
452 bfin_serial_dma_tx_chars(uart);
455 spin_unlock(&uart->port.lock);
459 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
461 struct bfin_serial_port *uart = dev_id;
462 unsigned short irqstat;
464 spin_lock(&uart->port.lock);
465 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
466 clear_dma_irqstat(uart->rx_dma_channel);
467 spin_unlock(&uart->port.lock);
469 mod_timer(&(uart->rx_dma_timer), jiffies);
476 * Return TIOCSER_TEMT when transmitter is not busy.
478 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
480 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
483 lsr = UART_GET_LSR(uart);
490 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
492 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
493 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
494 if (uart->cts_pin < 0)
495 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
497 if (UART_GET_CTS(uart))
498 return TIOCM_DSR | TIOCM_CAR;
501 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
504 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
506 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
507 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
508 if (uart->rts_pin < 0)
511 if (mctrl & TIOCM_RTS)
512 UART_CLEAR_RTS(uart);
519 * Handle any change of modem status signal since we were last called.
521 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
523 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
525 struct uart_info *info = uart->port.info;
526 struct tty_struct *tty = info->port.tty;
528 status = bfin_serial_get_mctrl(&uart->port);
529 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
530 if (!(status & TIOCM_CTS)) {
532 uart->cts_timer.data = (unsigned long)(uart);
533 uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
534 uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
535 add_timer(&(uart->cts_timer));
543 * Interrupts are always disabled.
545 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
547 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
548 u16 lcr = UART_GET_LCR(uart);
553 UART_PUT_LCR(uart, lcr);
557 static int bfin_serial_startup(struct uart_port *port)
559 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
561 #ifdef CONFIG_SERIAL_BFIN_DMA
562 dma_addr_t dma_handle;
564 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
565 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
569 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
570 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
571 free_dma(uart->rx_dma_channel);
575 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
576 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
578 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
579 uart->rx_dma_buf.head = 0;
580 uart->rx_dma_buf.tail = 0;
581 uart->rx_dma_nrows = 0;
583 set_dma_config(uart->rx_dma_channel,
584 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
585 INTR_ON_ROW, DIMENSION_2D,
588 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
589 set_dma_x_modify(uart->rx_dma_channel, 1);
590 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
591 set_dma_y_modify(uart->rx_dma_channel, 1);
592 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
593 enable_dma(uart->rx_dma_channel);
595 uart->rx_dma_timer.data = (unsigned long)(uart);
596 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
597 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
598 add_timer(&(uart->rx_dma_timer));
600 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
601 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
602 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
603 kgdboc_break_enabled = 0;
606 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
607 "BFIN_UART_RX", uart)) {
608 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
613 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
614 "BFIN_UART_TX", uart)) {
615 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
616 free_irq(uart->port.irq, uart);
622 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
624 switch (uart->port.irq) {
626 uart_dma_ch_rx = CH_UART3_RX;
627 uart_dma_ch_tx = CH_UART3_TX;
630 uart_dma_ch_rx = CH_UART2_RX;
631 uart_dma_ch_tx = CH_UART2_TX;
634 uart_dma_ch_rx = uart_dma_ch_tx = 0;
638 if (uart_dma_ch_rx &&
639 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
640 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
641 free_irq(uart->port.irq, uart);
642 free_irq(uart->port.irq + 1, uart);
645 if (uart_dma_ch_tx &&
646 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
647 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
648 free_dma(uart_dma_ch_rx);
649 free_irq(uart->port.irq, uart);
650 free_irq(uart->port.irq + 1, uart);
655 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
656 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
660 UART_SET_IER(uart, ERBFI);
664 static void bfin_serial_shutdown(struct uart_port *port)
666 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
668 #ifdef CONFIG_SERIAL_BFIN_DMA
669 disable_dma(uart->tx_dma_channel);
670 free_dma(uart->tx_dma_channel);
671 disable_dma(uart->rx_dma_channel);
672 free_dma(uart->rx_dma_channel);
673 del_timer(&(uart->rx_dma_timer));
674 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
677 switch (uart->port.irq) {
679 free_dma(CH_UART3_RX);
680 free_dma(CH_UART3_TX);
683 free_dma(CH_UART2_RX);
684 free_dma(CH_UART2_TX);
690 free_irq(uart->port.irq, uart);
691 free_irq(uart->port.irq+1, uart);
696 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
697 struct ktermios *old)
699 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
701 unsigned int baud, quot;
702 unsigned short val, ier, lcr = 0;
704 switch (termios->c_cflag & CSIZE) {
718 printk(KERN_ERR "%s: word lengh not supported\n",
722 if (termios->c_cflag & CSTOPB)
724 if (termios->c_cflag & PARENB)
726 if (!(termios->c_cflag & PARODD))
728 if (termios->c_cflag & CMSPAR)
731 port->read_status_mask = OE;
732 if (termios->c_iflag & INPCK)
733 port->read_status_mask |= (FE | PE);
734 if (termios->c_iflag & (BRKINT | PARMRK))
735 port->read_status_mask |= BI;
738 * Characters to ignore
740 port->ignore_status_mask = 0;
741 if (termios->c_iflag & IGNPAR)
742 port->ignore_status_mask |= FE | PE;
743 if (termios->c_iflag & IGNBRK) {
744 port->ignore_status_mask |= BI;
746 * If we're ignoring parity and break indicators,
747 * ignore overruns too (for real raw support).
749 if (termios->c_iflag & IGNPAR)
750 port->ignore_status_mask |= OE;
753 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
754 quot = uart_get_divisor(port, baud);
755 spin_lock_irqsave(&uart->port.lock, flags);
757 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
760 ier = UART_GET_IER(uart);
761 UART_DISABLE_INTS(uart);
763 /* Set DLAB in LCR to Access DLL and DLH */
766 UART_PUT_DLL(uart, quot & 0xFF);
767 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
770 /* Clear DLAB in LCR to Access THR RBR IER */
771 UART_CLEAR_DLAB(uart);
773 UART_PUT_LCR(uart, lcr);
776 UART_ENABLE_INTS(uart, ier);
778 val = UART_GET_GCTL(uart);
780 UART_PUT_GCTL(uart, val);
782 /* Port speed changed, update the per-port timeout. */
783 uart_update_timeout(port, termios->c_cflag, baud);
785 spin_unlock_irqrestore(&uart->port.lock, flags);
788 static const char *bfin_serial_type(struct uart_port *port)
790 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
792 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
796 * Release the memory region(s) being used by 'port'.
798 static void bfin_serial_release_port(struct uart_port *port)
803 * Request the memory region(s) being used by 'port'.
805 static int bfin_serial_request_port(struct uart_port *port)
811 * Configure/autoconfigure the port.
813 static void bfin_serial_config_port(struct uart_port *port, int flags)
815 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
817 if (flags & UART_CONFIG_TYPE &&
818 bfin_serial_request_port(&uart->port) == 0)
819 uart->port.type = PORT_BFIN;
823 * Verify the new serial_struct (for TIOCSSERIAL).
824 * The only change we allow are to the flags and type, and
825 * even then only between PORT_BFIN and PORT_UNKNOWN
828 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
834 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
835 * In other cases, disable IrDA function.
837 static void bfin_serial_set_ldisc(struct uart_port *port)
839 int line = port->line;
842 if (line >= port->info->port.tty->driver->num)
845 switch (port->info->port.tty->termios->c_line) {
847 val = UART_GET_GCTL(&bfin_serial_ports[line]);
848 val |= (IREN | RPOLC);
849 UART_PUT_GCTL(&bfin_serial_ports[line], val);
852 val = UART_GET_GCTL(&bfin_serial_ports[line]);
853 val &= ~(IREN | RPOLC);
854 UART_PUT_GCTL(&bfin_serial_ports[line], val);
858 #ifdef CONFIG_CONSOLE_POLL
859 static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
861 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
863 while (!(UART_GET_LSR(uart) & THRE))
866 UART_CLEAR_DLAB(uart);
867 UART_PUT_CHAR(uart, (unsigned char)chr);
870 static int bfin_serial_poll_get_char(struct uart_port *port)
872 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
875 while (!(UART_GET_LSR(uart) & DR))
878 UART_CLEAR_DLAB(uart);
879 chr = UART_GET_CHAR(uart);
885 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
886 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
887 static void bfin_kgdboc_port_shutdown(struct uart_port *port)
889 if (kgdboc_break_enabled) {
890 kgdboc_break_enabled = 0;
891 bfin_serial_shutdown(port);
895 static int bfin_kgdboc_port_startup(struct uart_port *port)
897 kgdboc_port_line = port->line;
898 kgdboc_break_enabled = !bfin_serial_startup(port);
903 static void bfin_serial_reset_irda(struct uart_port *port)
905 int line = port->line;
908 val = UART_GET_GCTL(&bfin_serial_ports[line]);
909 val &= ~(IREN | RPOLC);
910 UART_PUT_GCTL(&bfin_serial_ports[line], val);
912 val |= (IREN | RPOLC);
913 UART_PUT_GCTL(&bfin_serial_ports[line], val);
917 static struct uart_ops bfin_serial_pops = {
918 .tx_empty = bfin_serial_tx_empty,
919 .set_mctrl = bfin_serial_set_mctrl,
920 .get_mctrl = bfin_serial_get_mctrl,
921 .stop_tx = bfin_serial_stop_tx,
922 .start_tx = bfin_serial_start_tx,
923 .stop_rx = bfin_serial_stop_rx,
924 .enable_ms = bfin_serial_enable_ms,
925 .break_ctl = bfin_serial_break_ctl,
926 .startup = bfin_serial_startup,
927 .shutdown = bfin_serial_shutdown,
928 .set_termios = bfin_serial_set_termios,
929 .set_ldisc = bfin_serial_set_ldisc,
930 .type = bfin_serial_type,
931 .release_port = bfin_serial_release_port,
932 .request_port = bfin_serial_request_port,
933 .config_port = bfin_serial_config_port,
934 .verify_port = bfin_serial_verify_port,
935 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
936 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
937 .kgdboc_port_startup = bfin_kgdboc_port_startup,
938 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
940 #ifdef CONFIG_CONSOLE_POLL
941 .poll_put_char = bfin_serial_poll_put_char,
942 .poll_get_char = bfin_serial_poll_get_char,
946 static void __init bfin_serial_init_ports(void)
948 static int first = 1;
955 for (i = 0; i < nr_active_ports; i++) {
956 bfin_serial_ports[i].port.uartclk = get_sclk();
957 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
958 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
959 bfin_serial_ports[i].port.line = i;
960 bfin_serial_ports[i].port.iotype = UPIO_MEM;
961 bfin_serial_ports[i].port.membase =
962 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
963 bfin_serial_ports[i].port.mapbase =
964 bfin_serial_resource[i].uart_base_addr;
965 bfin_serial_ports[i].port.irq =
966 bfin_serial_resource[i].uart_irq;
967 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
968 #ifdef CONFIG_SERIAL_BFIN_DMA
969 bfin_serial_ports[i].tx_done = 1;
970 bfin_serial_ports[i].tx_count = 0;
971 bfin_serial_ports[i].tx_dma_channel =
972 bfin_serial_resource[i].uart_tx_dma_channel;
973 bfin_serial_ports[i].rx_dma_channel =
974 bfin_serial_resource[i].uart_rx_dma_channel;
975 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
977 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
978 init_timer(&(bfin_serial_ports[i].cts_timer));
979 bfin_serial_ports[i].cts_pin =
980 bfin_serial_resource[i].uart_cts_pin;
981 bfin_serial_ports[i].rts_pin =
982 bfin_serial_resource[i].uart_rts_pin;
984 bfin_serial_hw_init(&bfin_serial_ports[i]);
989 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
991 * If the port was already initialised (eg, by a boot loader),
992 * try to determine the current setup.
995 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
996 int *parity, int *bits)
998 unsigned short status;
1000 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1001 if (status == (ERBFI | ETBEI)) {
1002 /* ok, the port was enabled */
1005 lcr = UART_GET_LCR(uart);
1014 switch (lcr & 0x03) {
1015 case 0: *bits = 5; break;
1016 case 1: *bits = 6; break;
1017 case 2: *bits = 7; break;
1018 case 3: *bits = 8; break;
1020 /* Set DLAB in LCR to Access DLL and DLH */
1021 UART_SET_DLAB(uart);
1023 dll = UART_GET_DLL(uart);
1024 dlh = UART_GET_DLH(uart);
1026 /* Clear DLAB in LCR to Access THR RBR IER */
1027 UART_CLEAR_DLAB(uart);
1029 *baud = get_sclk() / (16*(dll | dlh << 8));
1031 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1035 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1036 static struct uart_driver bfin_serial_reg;
1039 bfin_serial_console_setup(struct console *co, char *options)
1041 struct bfin_serial_port *uart;
1042 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1046 # ifdef CONFIG_SERIAL_BFIN_CTSRTS
1054 * Check whether an invalid uart number has been specified, and
1055 * if so, search for the first available port that does have
1058 if (co->index == -1 || co->index >= nr_active_ports)
1060 uart = &bfin_serial_ports[co->index];
1062 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1064 uart_parse_options(options, &baud, &parity, &bits, &flow);
1066 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1068 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1073 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1074 defined (CONFIG_EARLY_PRINTK) */
1076 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1077 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1079 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1080 while (!(UART_GET_LSR(uart) & THRE))
1082 UART_PUT_CHAR(uart, ch);
1087 * Interrupts are disabled on entering
1090 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1092 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1095 spin_lock_irqsave(&uart->port.lock, flags);
1096 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1097 spin_unlock_irqrestore(&uart->port.lock, flags);
1101 static struct console bfin_serial_console = {
1102 .name = BFIN_SERIAL_NAME,
1103 .write = bfin_serial_console_write,
1104 .device = uart_console_device,
1105 .setup = bfin_serial_console_setup,
1106 .flags = CON_PRINTBUFFER,
1108 .data = &bfin_serial_reg,
1111 static int __init bfin_serial_rs_console_init(void)
1113 bfin_serial_init_ports();
1114 register_console(&bfin_serial_console);
1118 console_initcall(bfin_serial_rs_console_init);
1120 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1122 #define BFIN_SERIAL_CONSOLE NULL
1123 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1126 #ifdef CONFIG_EARLY_PRINTK
1127 static __init void early_serial_putc(struct uart_port *port, int ch)
1129 unsigned timeout = 0xffff;
1130 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1132 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1134 UART_PUT_CHAR(uart, ch);
1137 static __init void early_serial_write(struct console *con, const char *s,
1140 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1143 for (i = 0; i < n; i++, s++) {
1145 early_serial_putc(&uart->port, '\r');
1146 early_serial_putc(&uart->port, *s);
1150 static struct __initdata console bfin_early_serial_console = {
1151 .name = "early_BFuart",
1152 .write = early_serial_write,
1153 .device = uart_console_device,
1154 .flags = CON_PRINTBUFFER,
1155 .setup = bfin_serial_console_setup,
1157 .data = &bfin_serial_reg,
1160 struct console __init *bfin_earlyserial_init(unsigned int port,
1163 struct bfin_serial_port *uart;
1166 if (port == -1 || port >= nr_active_ports)
1168 bfin_serial_init_ports();
1169 bfin_early_serial_console.index = port;
1170 uart = &bfin_serial_ports[port];
1176 bfin_serial_set_termios(&uart->port, &t, &t);
1177 return &bfin_early_serial_console;
1180 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1182 static struct uart_driver bfin_serial_reg = {
1183 .owner = THIS_MODULE,
1184 .driver_name = "bfin-uart",
1185 .dev_name = BFIN_SERIAL_NAME,
1186 .major = BFIN_SERIAL_MAJOR,
1187 .minor = BFIN_SERIAL_MINOR,
1188 .nr = BFIN_UART_NR_PORTS,
1189 .cons = BFIN_SERIAL_CONSOLE,
1192 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1196 for (i = 0; i < nr_active_ports; i++) {
1197 if (bfin_serial_ports[i].port.dev != &dev->dev)
1199 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1205 static int bfin_serial_resume(struct platform_device *dev)
1209 for (i = 0; i < nr_active_ports; i++) {
1210 if (bfin_serial_ports[i].port.dev != &dev->dev)
1212 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1218 static int bfin_serial_probe(struct platform_device *dev)
1220 struct resource *res = dev->resource;
1223 for (i = 0; i < dev->num_resources; i++, res++)
1224 if (res->flags & IORESOURCE_MEM)
1227 if (i < dev->num_resources) {
1228 for (i = 0; i < nr_active_ports; i++, res++) {
1229 if (bfin_serial_ports[i].port.mapbase != res->start)
1231 bfin_serial_ports[i].port.dev = &dev->dev;
1232 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1239 static int bfin_serial_remove(struct platform_device *dev)
1243 for (i = 0; i < nr_active_ports; i++) {
1244 if (bfin_serial_ports[i].port.dev != &dev->dev)
1246 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1247 bfin_serial_ports[i].port.dev = NULL;
1248 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1249 gpio_free(bfin_serial_ports[i].cts_pin);
1250 gpio_free(bfin_serial_ports[i].rts_pin);
1257 static struct platform_driver bfin_serial_driver = {
1258 .probe = bfin_serial_probe,
1259 .remove = bfin_serial_remove,
1260 .suspend = bfin_serial_suspend,
1261 .resume = bfin_serial_resume,
1263 .name = "bfin-uart",
1264 .owner = THIS_MODULE,
1268 static int __init bfin_serial_init(void)
1272 pr_info("Serial: Blackfin serial driver\n");
1274 bfin_serial_init_ports();
1276 ret = uart_register_driver(&bfin_serial_reg);
1278 ret = platform_driver_register(&bfin_serial_driver);
1280 pr_debug("uart register failed\n");
1281 uart_unregister_driver(&bfin_serial_reg);
1287 static void __exit bfin_serial_exit(void)
1289 platform_driver_unregister(&bfin_serial_driver);
1290 uart_unregister_driver(&bfin_serial_reg);
1294 module_init(bfin_serial_init);
1295 module_exit(bfin_serial_exit);
1297 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1298 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1299 MODULE_LICENSE("GPL");
1300 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1301 MODULE_ALIAS("platform:bfin-uart");