2 * Blackfin On-Chip Serial Driver
4 * Copyright 2006-2007 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 #ifdef CONFIG_KGDB_UART
26 #include <linux/kgdb.h>
27 #include <asm/irq_regs.h>
31 #include <asm/mach/bfin_serial_5xx.h>
33 #ifdef CONFIG_SERIAL_BFIN_DMA
34 #include <linux/dma-mapping.h>
37 #include <asm/cacheflush.h>
40 /* UART name and device definitions */
41 #define BFIN_SERIAL_NAME "ttyBF"
42 #define BFIN_SERIAL_MAJOR 204
43 #define BFIN_SERIAL_MINOR 64
46 * Setup for console. Argument comes from the menuconfig
48 #define DMA_RX_XCOUNT 512
49 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
51 #define DMA_RX_FLUSH_JIFFIES (HZ / 50)
53 #ifdef CONFIG_SERIAL_BFIN_DMA
54 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
56 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
59 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
62 * interrupts are disabled on entry
64 static void bfin_serial_stop_tx(struct uart_port *port)
66 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
67 struct circ_buf *xmit = &uart->port.info->xmit;
68 #if !defined(CONFIG_BF54x) && !defined(CONFIG_SERIAL_BFIN_DMA)
72 while (!(UART_GET_LSR(uart) & TEMT))
75 #ifdef CONFIG_SERIAL_BFIN_DMA
76 disable_dma(uart->tx_dma_channel);
77 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
78 uart->port.icount.tx += uart->tx_count;
84 UART_PUT_LSR(uart, TFI);
85 UART_CLEAR_IER(uart, ETBEI);
87 ier = UART_GET_IER(uart);
89 UART_PUT_IER(uart, ier);
95 * port is locked and interrupts are disabled
97 static void bfin_serial_start_tx(struct uart_port *port)
99 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
101 #ifdef CONFIG_SERIAL_BFIN_DMA
103 bfin_serial_dma_tx_chars(uart);
106 UART_SET_IER(uart, ETBEI);
109 ier = UART_GET_IER(uart);
111 UART_PUT_IER(uart, ier);
113 bfin_serial_tx_chars(uart);
118 * Interrupts are enabled
120 static void bfin_serial_stop_rx(struct uart_port *port)
122 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
123 #ifdef CONFIG_KGDB_UART
124 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
127 UART_CLEAR_IER(uart, ERBFI);
131 ier = UART_GET_IER(uart);
133 UART_PUT_IER(uart, ier);
135 #ifdef CONFIG_KGDB_UART
141 * Set the modem control timer to fire immediately.
143 static void bfin_serial_enable_ms(struct uart_port *port)
147 #ifdef CONFIG_KGDB_UART
148 static int kgdb_entry_state;
150 void kgdb_put_debug_char(int chr)
152 struct bfin_serial_port *uart;
154 if (CONFIG_KGDB_UART_PORT < 0
155 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
156 uart = &bfin_serial_ports[0];
158 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
160 while (!(UART_GET_LSR(uart) & THRE)) {
165 UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
168 UART_PUT_CHAR(uart, (unsigned char)chr);
172 int kgdb_get_debug_char(void)
174 struct bfin_serial_port *uart;
177 if (CONFIG_KGDB_UART_PORT < 0
178 || CONFIG_KGDB_UART_PORT >= BFIN_UART_NR_PORTS)
179 uart = &bfin_serial_ports[0];
181 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
183 while(!(UART_GET_LSR(uart) & DR)) {
187 UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
190 chr = UART_GET_CHAR(uart);
197 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
198 # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
199 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
201 # define UART_GET_ANOMALY_THRESHOLD(uart) 0
202 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
205 #ifdef CONFIG_SERIAL_BFIN_PIO
206 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
208 struct tty_struct *tty = uart->port.info->tty;
209 unsigned int status, ch, flg;
210 static struct timeval anomaly_start = { .tv_sec = 0 };
211 #ifdef CONFIG_KGDB_UART
212 struct pt_regs *regs = get_irq_regs();
215 status = UART_GET_LSR(uart);
216 UART_CLEAR_LSR(uart);
218 ch = UART_GET_CHAR(uart);
219 uart->port.icount.rx++;
221 #ifdef CONFIG_KGDB_UART
222 if (uart->port.line == CONFIG_KGDB_UART_PORT) {
223 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
224 kgdb_breakkey_pressed(regs);
226 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
227 kgdb_entry_state = 1;
228 } else if (kgdb_entry_state == 1 && ch == 'q') {
229 kgdb_entry_state = 0;
230 kgdb_breakkey_pressed(regs);
232 } else if (ch == 0x3) {/* Ctrl + C */
233 kgdb_entry_state = 0;
234 kgdb_breakkey_pressed(regs);
237 kgdb_entry_state = 0;
242 if (ANOMALY_05000363) {
243 /* The BF533 (and BF561) family of processors have a nice anomaly
244 * where they continuously generate characters for a "single" break.
245 * We have to basically ignore this flood until the "next" valid
246 * character comes across. Due to the nature of the flood, it is
247 * not possible to reliably catch bytes that are sent too quickly
248 * after this break. So application code talking to the Blackfin
249 * which sends a break signal must allow at least 1.5 character
250 * times after the end of the break for things to stabilize. This
251 * timeout was picked as it must absolutely be larger than 1
252 * character time +/- some percent. So 1.5 sounds good. All other
253 * Blackfin families operate properly. Woo.
255 if (anomaly_start.tv_sec) {
259 if ((~ch & (~ch + 1)) & 0xff)
260 goto known_good_char;
262 do_gettimeofday(&curr);
263 if (curr.tv_sec - anomaly_start.tv_sec > 1)
264 goto known_good_char;
267 if (curr.tv_sec != anomaly_start.tv_sec)
268 usecs += USEC_PER_SEC;
269 usecs += curr.tv_usec - anomaly_start.tv_usec;
271 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
272 goto known_good_char;
275 anomaly_start.tv_sec = 0;
277 anomaly_start = curr;
282 anomaly_start.tv_sec = 0;
287 if (ANOMALY_05000363)
288 if (bfin_revid() < 5)
289 do_gettimeofday(&anomaly_start);
290 uart->port.icount.brk++;
291 if (uart_handle_break(&uart->port))
293 status &= ~(PE | FE);
296 uart->port.icount.parity++;
298 uart->port.icount.overrun++;
300 uart->port.icount.frame++;
302 status &= uart->port.read_status_mask;
306 else if (status & PE)
308 else if (status & FE)
313 if (uart_handle_sysrq_char(&uart->port, ch))
316 uart_insert_char(&uart->port, status, OE, ch, flg);
319 tty_flip_buffer_push(tty);
322 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
324 struct circ_buf *xmit = &uart->port.info->xmit;
326 if (uart->port.x_char) {
327 UART_PUT_CHAR(uart, uart->port.x_char);
328 uart->port.icount.tx++;
329 uart->port.x_char = 0;
332 * Check the modem control lines before
333 * transmitting anything.
335 bfin_serial_mctrl_check(uart);
337 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
338 bfin_serial_stop_tx(&uart->port);
342 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
343 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
344 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
345 uart->port.icount.tx++;
349 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
350 uart_write_wakeup(&uart->port);
352 if (uart_circ_empty(xmit))
353 bfin_serial_stop_tx(&uart->port);
356 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
358 struct bfin_serial_port *uart = dev_id;
360 spin_lock(&uart->port.lock);
361 while (UART_GET_LSR(uart) & DR)
362 bfin_serial_rx_chars(uart);
363 spin_unlock(&uart->port.lock);
368 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
370 struct bfin_serial_port *uart = dev_id;
372 spin_lock(&uart->port.lock);
373 if (UART_GET_LSR(uart) & THRE)
374 bfin_serial_tx_chars(uart);
375 spin_unlock(&uart->port.lock);
381 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
382 static void bfin_serial_do_work(struct work_struct *work)
384 struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
386 bfin_serial_mctrl_check(uart);
390 #ifdef CONFIG_SERIAL_BFIN_DMA
391 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
393 struct circ_buf *xmit = &uart->port.info->xmit;
398 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
404 if (uart->port.x_char) {
405 UART_PUT_CHAR(uart, uart->port.x_char);
406 uart->port.icount.tx++;
407 uart->port.x_char = 0;
411 * Check the modem control lines before
412 * transmitting anything.
414 bfin_serial_mctrl_check(uart);
416 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
417 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
418 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
419 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
420 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
421 set_dma_config(uart->tx_dma_channel,
422 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
427 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
428 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
429 set_dma_x_modify(uart->tx_dma_channel, 1);
430 enable_dma(uart->tx_dma_channel);
433 UART_SET_IER(uart, ETBEI);
435 ier = UART_GET_IER(uart);
437 UART_PUT_IER(uart, ier);
441 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
443 struct tty_struct *tty = uart->port.info->tty;
446 status = UART_GET_LSR(uart);
447 UART_CLEAR_LSR(uart);
449 uart->port.icount.rx +=
450 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
454 uart->port.icount.brk++;
455 if (uart_handle_break(&uart->port))
456 goto dma_ignore_char;
457 status &= ~(PE | FE);
460 uart->port.icount.parity++;
462 uart->port.icount.overrun++;
464 uart->port.icount.frame++;
466 status &= uart->port.read_status_mask;
470 else if (status & PE)
472 else if (status & FE)
477 for (i = uart->rx_dma_buf.tail; i != uart->rx_dma_buf.head; i++) {
478 if (i >= UART_XMIT_SIZE)
480 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
481 uart_insert_char(&uart->port, status, OE,
482 uart->rx_dma_buf.buf[i], flg);
486 tty_flip_buffer_push(tty);
489 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
493 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
494 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
495 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
496 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
497 uart->rx_dma_nrows = 0;
498 x_pos = DMA_RX_XCOUNT - x_pos;
499 if (x_pos == DMA_RX_XCOUNT)
502 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
503 if (pos != uart->rx_dma_buf.tail) {
504 uart->rx_dma_buf.head = pos;
505 bfin_serial_dma_rx_chars(uart);
506 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
509 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
512 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
514 struct bfin_serial_port *uart = dev_id;
515 struct circ_buf *xmit = &uart->port.info->xmit;
518 spin_lock(&uart->port.lock);
519 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
520 disable_dma(uart->tx_dma_channel);
521 clear_dma_irqstat(uart->tx_dma_channel);
523 UART_CLEAR_IER(uart, ETBEI);
525 ier = UART_GET_IER(uart);
527 UART_PUT_IER(uart, ier);
529 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
530 uart->port.icount.tx += uart->tx_count;
532 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
533 uart_write_wakeup(&uart->port);
535 bfin_serial_dma_tx_chars(uart);
538 spin_unlock(&uart->port.lock);
542 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
544 struct bfin_serial_port *uart = dev_id;
545 unsigned short irqstat;
547 spin_lock(&uart->port.lock);
548 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
549 clear_dma_irqstat(uart->rx_dma_channel);
550 spin_unlock(&uart->port.lock);
552 mod_timer(&(uart->rx_dma_timer), jiffies);
559 * Return TIOCSER_TEMT when transmitter is not busy.
561 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
563 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
566 lsr = UART_GET_LSR(uart);
573 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
575 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
576 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
577 if (uart->cts_pin < 0)
578 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
581 if (UART_GET_MSR(uart) & CTS)
583 if (gpio_get_value(uart->cts_pin))
585 return TIOCM_DSR | TIOCM_CAR;
588 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
591 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
593 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
594 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
595 if (uart->rts_pin < 0)
598 if (mctrl & TIOCM_RTS)
600 UART_PUT_MCR(uart, UART_GET_MCR(uart) & ~MRTS);
602 gpio_set_value(uart->rts_pin, 0);
606 UART_PUT_MCR(uart, UART_GET_MCR(uart) | MRTS);
608 gpio_set_value(uart->rts_pin, 1);
614 * Handle any change of modem status signal since we were last called.
616 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
618 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
620 struct uart_info *info = uart->port.info;
621 struct tty_struct *tty = info->tty;
623 status = bfin_serial_get_mctrl(&uart->port);
624 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
625 if (!(status & TIOCM_CTS)) {
627 schedule_work(&uart->cts_workqueue);
635 * Interrupts are always disabled.
637 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
639 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
640 u16 lcr = UART_GET_LCR(uart);
645 UART_PUT_LCR(uart, lcr);
649 static int bfin_serial_startup(struct uart_port *port)
651 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
653 #ifdef CONFIG_SERIAL_BFIN_DMA
654 dma_addr_t dma_handle;
656 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
657 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
661 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
662 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
663 free_dma(uart->rx_dma_channel);
667 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
668 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
670 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
671 uart->rx_dma_buf.head = 0;
672 uart->rx_dma_buf.tail = 0;
673 uart->rx_dma_nrows = 0;
675 set_dma_config(uart->rx_dma_channel,
676 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
677 INTR_ON_ROW, DIMENSION_2D,
680 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
681 set_dma_x_modify(uart->rx_dma_channel, 1);
682 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
683 set_dma_y_modify(uart->rx_dma_channel, 1);
684 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
685 enable_dma(uart->rx_dma_channel);
687 uart->rx_dma_timer.data = (unsigned long)(uart);
688 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
689 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
690 add_timer(&(uart->rx_dma_timer));
692 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
693 "BFIN_UART_RX", uart)) {
694 # ifdef CONFIG_KGDB_UART
695 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
697 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
699 # ifdef CONFIG_KGDB_UART
706 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
707 "BFIN_UART_TX", uart)) {
708 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
709 free_irq(uart->port.irq, uart);
714 UART_SET_IER(uart, ERBFI);
716 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
721 static void bfin_serial_shutdown(struct uart_port *port)
723 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
725 #ifdef CONFIG_SERIAL_BFIN_DMA
726 disable_dma(uart->tx_dma_channel);
727 free_dma(uart->tx_dma_channel);
728 disable_dma(uart->rx_dma_channel);
729 free_dma(uart->rx_dma_channel);
730 del_timer(&(uart->rx_dma_timer));
731 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
733 #ifdef CONFIG_KGDB_UART
734 if (uart->port.line != CONFIG_KGDB_UART_PORT)
736 free_irq(uart->port.irq, uart);
737 free_irq(uart->port.irq+1, uart);
742 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
743 struct ktermios *old)
745 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
747 unsigned int baud, quot;
748 unsigned short val, ier, lcr = 0;
750 switch (termios->c_cflag & CSIZE) {
764 printk(KERN_ERR "%s: word lengh not supported\n",
768 if (termios->c_cflag & CSTOPB)
770 if (termios->c_cflag & PARENB)
772 if (!(termios->c_cflag & PARODD))
774 if (termios->c_cflag & CMSPAR)
777 port->read_status_mask = OE;
778 if (termios->c_iflag & INPCK)
779 port->read_status_mask |= (FE | PE);
780 if (termios->c_iflag & (BRKINT | PARMRK))
781 port->read_status_mask |= BI;
784 * Characters to ignore
786 port->ignore_status_mask = 0;
787 if (termios->c_iflag & IGNPAR)
788 port->ignore_status_mask |= FE | PE;
789 if (termios->c_iflag & IGNBRK) {
790 port->ignore_status_mask |= BI;
792 * If we're ignoring parity and break indicators,
793 * ignore overruns too (for real raw support).
795 if (termios->c_iflag & IGNPAR)
796 port->ignore_status_mask |= OE;
799 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
800 quot = uart_get_divisor(port, baud);
801 spin_lock_irqsave(&uart->port.lock, flags);
803 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
806 ier = UART_GET_IER(uart);
808 UART_CLEAR_IER(uart, 0xF);
810 UART_PUT_IER(uart, 0);
814 /* Set DLAB in LCR to Access DLL and DLH */
815 val = UART_GET_LCR(uart);
817 UART_PUT_LCR(uart, val);
821 UART_PUT_DLL(uart, quot & 0xFF);
823 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
827 /* Clear DLAB in LCR to Access THR RBR IER */
828 val = UART_GET_LCR(uart);
830 UART_PUT_LCR(uart, val);
834 UART_PUT_LCR(uart, lcr);
838 UART_SET_IER(uart, ier);
840 UART_PUT_IER(uart, ier);
843 val = UART_GET_GCTL(uart);
845 UART_PUT_GCTL(uart, val);
847 spin_unlock_irqrestore(&uart->port.lock, flags);
850 static const char *bfin_serial_type(struct uart_port *port)
852 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
854 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
858 * Release the memory region(s) being used by 'port'.
860 static void bfin_serial_release_port(struct uart_port *port)
865 * Request the memory region(s) being used by 'port'.
867 static int bfin_serial_request_port(struct uart_port *port)
873 * Configure/autoconfigure the port.
875 static void bfin_serial_config_port(struct uart_port *port, int flags)
877 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
879 if (flags & UART_CONFIG_TYPE &&
880 bfin_serial_request_port(&uart->port) == 0)
881 uart->port.type = PORT_BFIN;
885 * Verify the new serial_struct (for TIOCSSERIAL).
886 * The only change we allow are to the flags and type, and
887 * even then only between PORT_BFIN and PORT_UNKNOWN
890 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
896 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
897 * In other cases, disable IrDA function.
899 static void bfin_set_ldisc(struct tty_struct *tty)
901 int line = tty->index;
904 if (line >= tty->driver->num)
907 switch (tty->ldisc.num) {
909 val = UART_GET_GCTL(&bfin_serial_ports[line]);
910 val |= (IREN | RPOLC);
911 UART_PUT_GCTL(&bfin_serial_ports[line], val);
914 val = UART_GET_GCTL(&bfin_serial_ports[line]);
915 val &= ~(IREN | RPOLC);
916 UART_PUT_GCTL(&bfin_serial_ports[line], val);
920 static struct uart_ops bfin_serial_pops = {
921 .tx_empty = bfin_serial_tx_empty,
922 .set_mctrl = bfin_serial_set_mctrl,
923 .get_mctrl = bfin_serial_get_mctrl,
924 .stop_tx = bfin_serial_stop_tx,
925 .start_tx = bfin_serial_start_tx,
926 .stop_rx = bfin_serial_stop_rx,
927 .enable_ms = bfin_serial_enable_ms,
928 .break_ctl = bfin_serial_break_ctl,
929 .startup = bfin_serial_startup,
930 .shutdown = bfin_serial_shutdown,
931 .set_termios = bfin_serial_set_termios,
932 .type = bfin_serial_type,
933 .release_port = bfin_serial_release_port,
934 .request_port = bfin_serial_request_port,
935 .config_port = bfin_serial_config_port,
936 .verify_port = bfin_serial_verify_port,
939 static void __init bfin_serial_init_ports(void)
941 static int first = 1;
948 for (i = 0; i < nr_ports; i++) {
949 bfin_serial_ports[i].port.uartclk = get_sclk();
950 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
951 bfin_serial_ports[i].port.line = i;
952 bfin_serial_ports[i].port.iotype = UPIO_MEM;
953 bfin_serial_ports[i].port.membase =
954 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
955 bfin_serial_ports[i].port.mapbase =
956 bfin_serial_resource[i].uart_base_addr;
957 bfin_serial_ports[i].port.irq =
958 bfin_serial_resource[i].uart_irq;
959 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
960 #ifdef CONFIG_SERIAL_BFIN_DMA
961 bfin_serial_ports[i].tx_done = 1;
962 bfin_serial_ports[i].tx_count = 0;
963 bfin_serial_ports[i].tx_dma_channel =
964 bfin_serial_resource[i].uart_tx_dma_channel;
965 bfin_serial_ports[i].rx_dma_channel =
966 bfin_serial_resource[i].uart_rx_dma_channel;
967 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
969 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
970 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
971 bfin_serial_ports[i].cts_pin =
972 bfin_serial_resource[i].uart_cts_pin;
973 bfin_serial_ports[i].rts_pin =
974 bfin_serial_resource[i].uart_rts_pin;
976 bfin_serial_hw_init(&bfin_serial_ports[i]);
981 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
983 * If the port was already initialised (eg, by a boot loader),
984 * try to determine the current setup.
987 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
988 int *parity, int *bits)
990 unsigned short status;
992 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
993 if (status == (ERBFI | ETBEI)) {
994 /* ok, the port was enabled */
995 unsigned short lcr, val;
996 unsigned short dlh, dll;
998 lcr = UART_GET_LCR(uart);
1007 switch (lcr & 0x03) {
1008 case 0: *bits = 5; break;
1009 case 1: *bits = 6; break;
1010 case 2: *bits = 7; break;
1011 case 3: *bits = 8; break;
1013 #ifndef CONFIG_BF54x
1014 /* Set DLAB in LCR to Access DLL and DLH */
1015 val = UART_GET_LCR(uart);
1017 UART_PUT_LCR(uart, val);
1020 dll = UART_GET_DLL(uart);
1021 dlh = UART_GET_DLH(uart);
1023 #ifndef CONFIG_BF54x
1024 /* Clear DLAB in LCR to Access THR RBR IER */
1025 val = UART_GET_LCR(uart);
1027 UART_PUT_LCR(uart, val);
1030 *baud = get_sclk() / (16*(dll | dlh << 8));
1032 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
1036 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1037 static struct uart_driver bfin_serial_reg;
1040 bfin_serial_console_setup(struct console *co, char *options)
1042 struct bfin_serial_port *uart;
1043 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1047 # ifdef CONFIG_SERIAL_BFIN_CTSRTS
1055 * Check whether an invalid uart number has been specified, and
1056 * if so, search for the first available port that does have
1059 if (co->index == -1 || co->index >= nr_ports)
1061 uart = &bfin_serial_ports[co->index];
1063 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1065 uart_parse_options(options, &baud, &parity, &bits, &flow);
1067 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1069 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1074 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1075 defined (CONFIG_EARLY_PRINTK) */
1077 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1078 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1080 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1081 while (!(UART_GET_LSR(uart) & THRE))
1083 UART_PUT_CHAR(uart, ch);
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];
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);
1102 static struct console bfin_serial_console = {
1103 .name = BFIN_SERIAL_NAME,
1104 .write = bfin_serial_console_write,
1105 .device = uart_console_device,
1106 .setup = bfin_serial_console_setup,
1107 .flags = CON_PRINTBUFFER,
1109 .data = &bfin_serial_reg,
1112 static int __init bfin_serial_rs_console_init(void)
1114 bfin_serial_init_ports();
1115 register_console(&bfin_serial_console);
1116 #ifdef CONFIG_KGDB_UART
1117 kgdb_entry_state = 0;
1122 console_initcall(bfin_serial_rs_console_init);
1124 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1126 #define BFIN_SERIAL_CONSOLE NULL
1127 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1130 #ifdef CONFIG_EARLY_PRINTK
1131 static __init void early_serial_putc(struct uart_port *port, int ch)
1133 unsigned timeout = 0xffff;
1134 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1136 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1138 UART_PUT_CHAR(uart, ch);
1141 static __init void early_serial_write(struct console *con, const char *s,
1144 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1147 for (i = 0; i < n; i++, s++) {
1149 early_serial_putc(&uart->port, '\r');
1150 early_serial_putc(&uart->port, *s);
1154 static struct __init console bfin_early_serial_console = {
1155 .name = "early_BFuart",
1156 .write = early_serial_write,
1157 .device = uart_console_device,
1158 .flags = CON_PRINTBUFFER,
1159 .setup = bfin_serial_console_setup,
1161 .data = &bfin_serial_reg,
1164 struct console __init *bfin_earlyserial_init(unsigned int port,
1167 struct bfin_serial_port *uart;
1170 if (port == -1 || port >= nr_ports)
1172 bfin_serial_init_ports();
1173 bfin_early_serial_console.index = port;
1174 uart = &bfin_serial_ports[port];
1180 bfin_serial_set_termios(&uart->port, &t, &t);
1181 return &bfin_early_serial_console;
1184 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1186 static struct uart_driver bfin_serial_reg = {
1187 .owner = THIS_MODULE,
1188 .driver_name = "bfin-uart",
1189 .dev_name = BFIN_SERIAL_NAME,
1190 .major = BFIN_SERIAL_MAJOR,
1191 .minor = BFIN_SERIAL_MINOR,
1192 .nr = BFIN_UART_NR_PORTS,
1193 .cons = BFIN_SERIAL_CONSOLE,
1196 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1198 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1201 uart_suspend_port(&bfin_serial_reg, &uart->port);
1206 static int bfin_serial_resume(struct platform_device *dev)
1208 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1211 uart_resume_port(&bfin_serial_reg, &uart->port);
1216 static int bfin_serial_probe(struct platform_device *dev)
1218 struct resource *res = dev->resource;
1221 for (i = 0; i < dev->num_resources; i++, res++)
1222 if (res->flags & IORESOURCE_MEM)
1225 if (i < dev->num_resources) {
1226 for (i = 0; i < nr_ports; i++, res++) {
1227 if (bfin_serial_ports[i].port.mapbase != res->start)
1229 bfin_serial_ports[i].port.dev = &dev->dev;
1230 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1231 platform_set_drvdata(dev, &bfin_serial_ports[i]);
1238 static int bfin_serial_remove(struct platform_device *pdev)
1240 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1243 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1244 gpio_free(uart->cts_pin);
1245 gpio_free(uart->rts_pin);
1248 platform_set_drvdata(pdev, NULL);
1251 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1256 static struct platform_driver bfin_serial_driver = {
1257 .probe = bfin_serial_probe,
1258 .remove = bfin_serial_remove,
1259 .suspend = bfin_serial_suspend,
1260 .resume = bfin_serial_resume,
1262 .name = "bfin-uart",
1263 .owner = THIS_MODULE,
1267 static int __init bfin_serial_init(void)
1270 #ifdef CONFIG_KGDB_UART
1271 struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
1275 pr_info("Serial: Blackfin serial driver\n");
1277 bfin_serial_init_ports();
1279 ret = uart_register_driver(&bfin_serial_reg);
1281 bfin_serial_reg.tty_driver->set_ldisc = bfin_set_ldisc;
1282 ret = platform_driver_register(&bfin_serial_driver);
1284 pr_debug("uart register failed\n");
1285 uart_unregister_driver(&bfin_serial_reg);
1288 #ifdef CONFIG_KGDB_UART
1289 if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
1290 request_irq(uart->port.irq, bfin_serial_rx_int,
1291 IRQF_DISABLED, "BFIN_UART_RX", uart);
1292 pr_info("Request irq for kgdb uart port\n");
1294 UART_SET_IER(uart, ERBFI);
1296 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
1299 t.c_cflag = CS8|B57600;
1303 t.c_line = CONFIG_KGDB_UART_PORT;
1304 bfin_serial_set_termios(&uart->port, &t, &t);
1310 static void __exit bfin_serial_exit(void)
1312 platform_driver_unregister(&bfin_serial_driver);
1313 uart_unregister_driver(&bfin_serial_reg);
1316 module_init(bfin_serial_init);
1317 module_exit(bfin_serial_exit);
1319 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1320 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1321 MODULE_LICENSE("GPL");
1322 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1323 MODULE_ALIAS("platform:bfin-uart");