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 #ifdef CONFIG_SERIAL_BFIN_DMA
85 struct circ_buf *xmit = &uart->port.info->xmit;
88 while (!(UART_GET_LSR(uart) & TEMT))
91 #ifdef CONFIG_SERIAL_BFIN_DMA
92 disable_dma(uart->tx_dma_channel);
93 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
94 uart->port.icount.tx += uart->tx_count;
100 UART_PUT_LSR(uart, TFI);
102 UART_CLEAR_IER(uart, ETBEI);
107 * port is locked and interrupts are disabled
109 static void bfin_serial_start_tx(struct uart_port *port)
111 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
112 struct tty_struct *tty = uart->port.info->port.tty;
115 * To avoid losting RX interrupt, we reset IR function
116 * before sending data.
118 if (tty->termios->c_line == N_IRDA)
119 bfin_serial_reset_irda(port);
121 #ifdef CONFIG_SERIAL_BFIN_DMA
123 bfin_serial_dma_tx_chars(uart);
125 UART_SET_IER(uart, ETBEI);
126 bfin_serial_tx_chars(uart);
131 * Interrupts are enabled
133 static void bfin_serial_stop_rx(struct uart_port *port)
135 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
137 UART_CLEAR_IER(uart, ERBFI);
141 * Set the modem control timer to fire immediately.
143 static void bfin_serial_enable_ms(struct uart_port *port)
148 #if ANOMALY_05000363 && defined(CONFIG_SERIAL_BFIN_PIO)
149 # define UART_GET_ANOMALY_THRESHOLD(uart) ((uart)->anomaly_threshold)
150 # define UART_SET_ANOMALY_THRESHOLD(uart, v) ((uart)->anomaly_threshold = (v))
152 # define UART_GET_ANOMALY_THRESHOLD(uart) 0
153 # define UART_SET_ANOMALY_THRESHOLD(uart, v)
156 #ifdef CONFIG_SERIAL_BFIN_PIO
157 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
159 struct tty_struct *tty = NULL;
160 unsigned int status, ch, flg;
161 static struct timeval anomaly_start = { .tv_sec = 0 };
163 status = UART_GET_LSR(uart);
164 UART_CLEAR_LSR(uart);
166 ch = UART_GET_CHAR(uart);
167 uart->port.icount.rx++;
169 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
170 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
171 if (kgdb_connected && kgdboc_port_line == uart->port.line)
172 if (ch == 0x3) {/* Ctrl + C */
177 if (!uart->port.info || !uart->port.info->port.tty)
180 tty = uart->port.info->port.tty;
182 if (ANOMALY_05000363) {
183 /* The BF533 (and BF561) family of processors have a nice anomaly
184 * where they continuously generate characters for a "single" break.
185 * We have to basically ignore this flood until the "next" valid
186 * character comes across. Due to the nature of the flood, it is
187 * not possible to reliably catch bytes that are sent too quickly
188 * after this break. So application code talking to the Blackfin
189 * which sends a break signal must allow at least 1.5 character
190 * times after the end of the break for things to stabilize. This
191 * timeout was picked as it must absolutely be larger than 1
192 * character time +/- some percent. So 1.5 sounds good. All other
193 * Blackfin families operate properly. Woo.
195 if (anomaly_start.tv_sec) {
199 if ((~ch & (~ch + 1)) & 0xff)
200 goto known_good_char;
202 do_gettimeofday(&curr);
203 if (curr.tv_sec - anomaly_start.tv_sec > 1)
204 goto known_good_char;
207 if (curr.tv_sec != anomaly_start.tv_sec)
208 usecs += USEC_PER_SEC;
209 usecs += curr.tv_usec - anomaly_start.tv_usec;
211 if (usecs > UART_GET_ANOMALY_THRESHOLD(uart))
212 goto known_good_char;
215 anomaly_start.tv_sec = 0;
217 anomaly_start = curr;
223 anomaly_start.tv_sec = 0;
228 if (ANOMALY_05000363)
229 if (bfin_revid() < 5)
230 do_gettimeofday(&anomaly_start);
231 uart->port.icount.brk++;
232 if (uart_handle_break(&uart->port))
234 status &= ~(PE | FE);
237 uart->port.icount.parity++;
239 uart->port.icount.overrun++;
241 uart->port.icount.frame++;
243 status &= uart->port.read_status_mask;
247 else if (status & PE)
249 else if (status & FE)
254 if (uart_handle_sysrq_char(&uart->port, ch))
257 uart_insert_char(&uart->port, status, OE, ch, flg);
260 tty_flip_buffer_push(tty);
263 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
265 struct circ_buf *xmit = &uart->port.info->xmit;
268 * Check the modem control lines before
269 * transmitting anything.
271 bfin_serial_mctrl_check(uart);
273 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
276 UART_PUT_LSR(uart, TFI);
278 UART_CLEAR_IER(uart, ETBEI);
282 if (uart->port.x_char) {
283 UART_PUT_CHAR(uart, uart->port.x_char);
284 uart->port.icount.tx++;
285 uart->port.x_char = 0;
288 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
289 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
290 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
291 uart->port.icount.tx++;
295 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
296 uart_write_wakeup(&uart->port);
299 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
301 struct bfin_serial_port *uart = dev_id;
303 spin_lock(&uart->port.lock);
304 while (UART_GET_LSR(uart) & DR)
305 bfin_serial_rx_chars(uart);
306 spin_unlock(&uart->port.lock);
311 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
313 struct bfin_serial_port *uart = dev_id;
315 spin_lock(&uart->port.lock);
316 if (UART_GET_LSR(uart) & THRE)
317 bfin_serial_tx_chars(uart);
318 spin_unlock(&uart->port.lock);
324 #ifdef CONFIG_SERIAL_BFIN_DMA
325 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
327 struct circ_buf *xmit = &uart->port.info->xmit;
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)) {
343 if (uart->port.x_char) {
344 UART_PUT_CHAR(uart, uart->port.x_char);
345 uart->port.icount.tx++;
346 uart->port.x_char = 0;
349 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
350 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
351 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
352 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
353 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
354 set_dma_config(uart->tx_dma_channel,
355 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
360 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
361 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
362 set_dma_x_modify(uart->tx_dma_channel, 1);
363 enable_dma(uart->tx_dma_channel);
365 UART_SET_IER(uart, ETBEI);
368 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
370 struct tty_struct *tty = uart->port.info->port.tty;
373 status = UART_GET_LSR(uart);
374 UART_CLEAR_LSR(uart);
376 uart->port.icount.rx +=
377 CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail,
381 uart->port.icount.brk++;
382 if (uart_handle_break(&uart->port))
383 goto dma_ignore_char;
384 status &= ~(PE | FE);
387 uart->port.icount.parity++;
389 uart->port.icount.overrun++;
391 uart->port.icount.frame++;
393 status &= uart->port.read_status_mask;
397 else if (status & PE)
399 else if (status & FE)
404 for (i = uart->rx_dma_buf.tail; ; i++) {
405 if (i >= UART_XMIT_SIZE)
407 if (i == uart->rx_dma_buf.head)
409 if (!uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
410 uart_insert_char(&uart->port, status, OE,
411 uart->rx_dma_buf.buf[i], flg);
415 tty_flip_buffer_push(tty);
418 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
423 spin_lock_irqsave(&uart->port.lock, flags);
425 uart->rx_dma_nrows = get_dma_curr_ycount(uart->rx_dma_channel);
426 x_pos = get_dma_curr_xcount(uart->rx_dma_channel);
427 uart->rx_dma_nrows = DMA_RX_YCOUNT - uart->rx_dma_nrows;
428 if (uart->rx_dma_nrows == DMA_RX_YCOUNT)
429 uart->rx_dma_nrows = 0;
430 x_pos = DMA_RX_XCOUNT - x_pos;
431 if (x_pos == DMA_RX_XCOUNT)
434 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
435 if (pos != uart->rx_dma_buf.tail) {
436 uart->rx_dma_buf.head = pos;
437 bfin_serial_dma_rx_chars(uart);
438 uart->rx_dma_buf.tail = uart->rx_dma_buf.head;
441 spin_unlock_irqrestore(&uart->port.lock, flags);
443 mod_timer(&(uart->rx_dma_timer), jiffies + DMA_RX_FLUSH_JIFFIES);
446 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
448 struct bfin_serial_port *uart = dev_id;
449 struct circ_buf *xmit = &uart->port.info->xmit;
451 spin_lock(&uart->port.lock);
452 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
453 disable_dma(uart->tx_dma_channel);
454 clear_dma_irqstat(uart->tx_dma_channel);
455 UART_CLEAR_IER(uart, ETBEI);
456 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
457 uart->port.icount.tx += uart->tx_count;
459 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
460 uart_write_wakeup(&uart->port);
462 bfin_serial_dma_tx_chars(uart);
465 spin_unlock(&uart->port.lock);
469 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
471 struct bfin_serial_port *uart = dev_id;
472 unsigned short irqstat;
474 spin_lock(&uart->port.lock);
475 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
476 clear_dma_irqstat(uart->rx_dma_channel);
477 bfin_serial_dma_rx_chars(uart);
478 spin_unlock(&uart->port.lock);
485 * Return TIOCSER_TEMT when transmitter is not busy.
487 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
489 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
492 lsr = UART_GET_LSR(uart);
499 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
501 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
502 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
503 if (uart->cts_pin < 0)
504 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
506 if (UART_GET_CTS(uart))
507 return TIOCM_DSR | TIOCM_CAR;
510 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
513 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
515 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
516 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
517 if (uart->rts_pin < 0)
520 if (mctrl & TIOCM_RTS)
521 UART_CLEAR_RTS(uart);
528 * Handle any change of modem status signal since we were last called.
530 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
532 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
534 struct uart_info *info = uart->port.info;
535 struct tty_struct *tty = info->port.tty;
537 status = bfin_serial_get_mctrl(&uart->port);
538 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
539 if (!(status & TIOCM_CTS)) {
541 uart->cts_timer.data = (unsigned long)(uart);
542 uart->cts_timer.function = (void *)bfin_serial_mctrl_check;
543 uart->cts_timer.expires = jiffies + CTS_CHECK_JIFFIES;
544 add_timer(&(uart->cts_timer));
552 * Interrupts are always disabled.
554 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
556 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
557 u16 lcr = UART_GET_LCR(uart);
562 UART_PUT_LCR(uart, lcr);
566 static int bfin_serial_startup(struct uart_port *port)
568 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
570 #ifdef CONFIG_SERIAL_BFIN_DMA
571 dma_addr_t dma_handle;
573 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
574 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
578 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
579 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
580 free_dma(uart->rx_dma_channel);
584 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
585 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
587 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
588 uart->rx_dma_buf.head = 0;
589 uart->rx_dma_buf.tail = 0;
590 uart->rx_dma_nrows = 0;
592 set_dma_config(uart->rx_dma_channel,
593 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
594 INTR_ON_ROW, DIMENSION_2D,
597 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
598 set_dma_x_modify(uart->rx_dma_channel, 1);
599 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
600 set_dma_y_modify(uart->rx_dma_channel, 1);
601 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
602 enable_dma(uart->rx_dma_channel);
604 uart->rx_dma_timer.data = (unsigned long)(uart);
605 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
606 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
607 add_timer(&(uart->rx_dma_timer));
609 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
610 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
611 if (kgdboc_port_line == uart->port.line && kgdboc_break_enabled)
612 kgdboc_break_enabled = 0;
615 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
616 "BFIN_UART_RX", uart)) {
617 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
622 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
623 "BFIN_UART_TX", uart)) {
624 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
625 free_irq(uart->port.irq, uart);
631 unsigned uart_dma_ch_rx, uart_dma_ch_tx;
633 switch (uart->port.irq) {
635 uart_dma_ch_rx = CH_UART3_RX;
636 uart_dma_ch_tx = CH_UART3_TX;
639 uart_dma_ch_rx = CH_UART2_RX;
640 uart_dma_ch_tx = CH_UART2_TX;
643 uart_dma_ch_rx = uart_dma_ch_tx = 0;
647 if (uart_dma_ch_rx &&
648 request_dma(uart_dma_ch_rx, "BFIN_UART_RX") < 0) {
649 printk(KERN_NOTICE"Fail to attach UART interrupt\n");
650 free_irq(uart->port.irq, uart);
651 free_irq(uart->port.irq + 1, uart);
654 if (uart_dma_ch_tx &&
655 request_dma(uart_dma_ch_tx, "BFIN_UART_TX") < 0) {
656 printk(KERN_NOTICE "Fail to attach UART interrupt\n");
657 free_dma(uart_dma_ch_rx);
658 free_irq(uart->port.irq, uart);
659 free_irq(uart->port.irq + 1, uart);
664 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
665 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
669 UART_SET_IER(uart, ERBFI);
673 static void bfin_serial_shutdown(struct uart_port *port)
675 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
677 #ifdef CONFIG_SERIAL_BFIN_DMA
678 disable_dma(uart->tx_dma_channel);
679 free_dma(uart->tx_dma_channel);
680 disable_dma(uart->rx_dma_channel);
681 free_dma(uart->rx_dma_channel);
682 del_timer(&(uart->rx_dma_timer));
683 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
686 switch (uart->port.irq) {
688 free_dma(CH_UART3_RX);
689 free_dma(CH_UART3_TX);
692 free_dma(CH_UART2_RX);
693 free_dma(CH_UART2_TX);
699 free_irq(uart->port.irq, uart);
700 free_irq(uart->port.irq+1, uart);
705 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
706 struct ktermios *old)
708 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
710 unsigned int baud, quot;
711 unsigned short val, ier, lcr = 0;
713 switch (termios->c_cflag & CSIZE) {
727 printk(KERN_ERR "%s: word lengh not supported\n",
731 if (termios->c_cflag & CSTOPB)
733 if (termios->c_cflag & PARENB)
735 if (!(termios->c_cflag & PARODD))
737 if (termios->c_cflag & CMSPAR)
740 port->read_status_mask = OE;
741 if (termios->c_iflag & INPCK)
742 port->read_status_mask |= (FE | PE);
743 if (termios->c_iflag & (BRKINT | PARMRK))
744 port->read_status_mask |= BI;
747 * Characters to ignore
749 port->ignore_status_mask = 0;
750 if (termios->c_iflag & IGNPAR)
751 port->ignore_status_mask |= FE | PE;
752 if (termios->c_iflag & IGNBRK) {
753 port->ignore_status_mask |= BI;
755 * If we're ignoring parity and break indicators,
756 * ignore overruns too (for real raw support).
758 if (termios->c_iflag & IGNPAR)
759 port->ignore_status_mask |= OE;
762 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
763 quot = uart_get_divisor(port, baud);
764 spin_lock_irqsave(&uart->port.lock, flags);
766 UART_SET_ANOMALY_THRESHOLD(uart, USEC_PER_SEC / baud * 15);
769 ier = UART_GET_IER(uart);
770 UART_DISABLE_INTS(uart);
772 /* Set DLAB in LCR to Access DLL and DLH */
775 UART_PUT_DLL(uart, quot & 0xFF);
776 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
779 /* Clear DLAB in LCR to Access THR RBR IER */
780 UART_CLEAR_DLAB(uart);
782 UART_PUT_LCR(uart, lcr);
785 UART_ENABLE_INTS(uart, ier);
787 val = UART_GET_GCTL(uart);
789 UART_PUT_GCTL(uart, val);
791 /* Port speed changed, update the per-port timeout. */
792 uart_update_timeout(port, termios->c_cflag, baud);
794 spin_unlock_irqrestore(&uart->port.lock, flags);
797 static const char *bfin_serial_type(struct uart_port *port)
799 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
801 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
805 * Release the memory region(s) being used by 'port'.
807 static void bfin_serial_release_port(struct uart_port *port)
812 * Request the memory region(s) being used by 'port'.
814 static int bfin_serial_request_port(struct uart_port *port)
820 * Configure/autoconfigure the port.
822 static void bfin_serial_config_port(struct uart_port *port, int flags)
824 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
826 if (flags & UART_CONFIG_TYPE &&
827 bfin_serial_request_port(&uart->port) == 0)
828 uart->port.type = PORT_BFIN;
832 * Verify the new serial_struct (for TIOCSSERIAL).
833 * The only change we allow are to the flags and type, and
834 * even then only between PORT_BFIN and PORT_UNKNOWN
837 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
843 * Enable the IrDA function if tty->ldisc.num is N_IRDA.
844 * In other cases, disable IrDA function.
846 static void bfin_serial_set_ldisc(struct uart_port *port)
848 int line = port->line;
851 if (line >= port->info->port.tty->driver->num)
854 switch (port->info->port.tty->termios->c_line) {
856 val = UART_GET_GCTL(&bfin_serial_ports[line]);
857 val |= (IREN | RPOLC);
858 UART_PUT_GCTL(&bfin_serial_ports[line], val);
861 val = UART_GET_GCTL(&bfin_serial_ports[line]);
862 val &= ~(IREN | RPOLC);
863 UART_PUT_GCTL(&bfin_serial_ports[line], val);
867 #ifdef CONFIG_CONSOLE_POLL
868 static void bfin_serial_poll_put_char(struct uart_port *port, unsigned char chr)
870 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
872 while (!(UART_GET_LSR(uart) & THRE))
875 UART_CLEAR_DLAB(uart);
876 UART_PUT_CHAR(uart, (unsigned char)chr);
879 static int bfin_serial_poll_get_char(struct uart_port *port)
881 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
884 while (!(UART_GET_LSR(uart) & DR))
887 UART_CLEAR_DLAB(uart);
888 chr = UART_GET_CHAR(uart);
894 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
895 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
896 static void bfin_kgdboc_port_shutdown(struct uart_port *port)
898 if (kgdboc_break_enabled) {
899 kgdboc_break_enabled = 0;
900 bfin_serial_shutdown(port);
904 static int bfin_kgdboc_port_startup(struct uart_port *port)
906 kgdboc_port_line = port->line;
907 kgdboc_break_enabled = !bfin_serial_startup(port);
912 static void bfin_serial_reset_irda(struct uart_port *port)
914 int line = port->line;
917 val = UART_GET_GCTL(&bfin_serial_ports[line]);
918 val &= ~(IREN | RPOLC);
919 UART_PUT_GCTL(&bfin_serial_ports[line], val);
921 val |= (IREN | RPOLC);
922 UART_PUT_GCTL(&bfin_serial_ports[line], val);
926 static struct uart_ops bfin_serial_pops = {
927 .tx_empty = bfin_serial_tx_empty,
928 .set_mctrl = bfin_serial_set_mctrl,
929 .get_mctrl = bfin_serial_get_mctrl,
930 .stop_tx = bfin_serial_stop_tx,
931 .start_tx = bfin_serial_start_tx,
932 .stop_rx = bfin_serial_stop_rx,
933 .enable_ms = bfin_serial_enable_ms,
934 .break_ctl = bfin_serial_break_ctl,
935 .startup = bfin_serial_startup,
936 .shutdown = bfin_serial_shutdown,
937 .set_termios = bfin_serial_set_termios,
938 .set_ldisc = bfin_serial_set_ldisc,
939 .type = bfin_serial_type,
940 .release_port = bfin_serial_release_port,
941 .request_port = bfin_serial_request_port,
942 .config_port = bfin_serial_config_port,
943 .verify_port = bfin_serial_verify_port,
944 #if defined(CONFIG_KGDB_SERIAL_CONSOLE) || \
945 defined(CONFIG_KGDB_SERIAL_CONSOLE_MODULE)
946 .kgdboc_port_startup = bfin_kgdboc_port_startup,
947 .kgdboc_port_shutdown = bfin_kgdboc_port_shutdown,
949 #ifdef CONFIG_CONSOLE_POLL
950 .poll_put_char = bfin_serial_poll_put_char,
951 .poll_get_char = bfin_serial_poll_get_char,
955 static void __init bfin_serial_init_ports(void)
957 static int first = 1;
964 for (i = 0; i < nr_active_ports; i++) {
965 bfin_serial_ports[i].port.uartclk = get_sclk();
966 bfin_serial_ports[i].port.fifosize = BFIN_UART_TX_FIFO_SIZE;
967 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
968 bfin_serial_ports[i].port.line = i;
969 bfin_serial_ports[i].port.iotype = UPIO_MEM;
970 bfin_serial_ports[i].port.membase =
971 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
972 bfin_serial_ports[i].port.mapbase =
973 bfin_serial_resource[i].uart_base_addr;
974 bfin_serial_ports[i].port.irq =
975 bfin_serial_resource[i].uart_irq;
976 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
977 #ifdef CONFIG_SERIAL_BFIN_DMA
978 bfin_serial_ports[i].tx_done = 1;
979 bfin_serial_ports[i].tx_count = 0;
980 bfin_serial_ports[i].tx_dma_channel =
981 bfin_serial_resource[i].uart_tx_dma_channel;
982 bfin_serial_ports[i].rx_dma_channel =
983 bfin_serial_resource[i].uart_rx_dma_channel;
984 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
986 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
987 init_timer(&(bfin_serial_ports[i].cts_timer));
988 bfin_serial_ports[i].cts_pin =
989 bfin_serial_resource[i].uart_cts_pin;
990 bfin_serial_ports[i].rts_pin =
991 bfin_serial_resource[i].uart_rts_pin;
993 bfin_serial_hw_init(&bfin_serial_ports[i]);
998 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
1000 * If the port was already initialised (eg, by a boot loader),
1001 * try to determine the current setup.
1004 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
1005 int *parity, int *bits)
1007 unsigned short status;
1009 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
1010 if (status == (ERBFI | ETBEI)) {
1011 /* ok, the port was enabled */
1014 lcr = UART_GET_LCR(uart);
1023 switch (lcr & 0x03) {
1024 case 0: *bits = 5; break;
1025 case 1: *bits = 6; break;
1026 case 2: *bits = 7; break;
1027 case 3: *bits = 8; break;
1029 /* Set DLAB in LCR to Access DLL and DLH */
1030 UART_SET_DLAB(uart);
1032 dll = UART_GET_DLL(uart);
1033 dlh = UART_GET_DLH(uart);
1035 /* Clear DLAB in LCR to Access THR RBR IER */
1036 UART_CLEAR_DLAB(uart);
1038 *baud = get_sclk() / (16*(dll | dlh << 8));
1040 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __func__, *baud, *parity, *bits);
1043 static struct uart_driver bfin_serial_reg;
1046 bfin_serial_console_setup(struct console *co, char *options)
1048 struct bfin_serial_port *uart;
1052 # ifdef CONFIG_SERIAL_BFIN_CTSRTS
1059 * Check whether an invalid uart number has been specified, and
1060 * if so, search for the first available port that does have
1063 if (co->index == -1 || co->index >= nr_active_ports)
1065 uart = &bfin_serial_ports[co->index];
1068 uart_parse_options(options, &baud, &parity, &bits, &flow);
1070 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1072 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];
1094 unsigned long flags;
1096 spin_lock_irqsave(&uart->port.lock, flags);
1097 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1098 spin_unlock_irqrestore(&uart->port.lock, flags);
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);
1119 console_initcall(bfin_serial_rs_console_init);
1121 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1123 #define BFIN_SERIAL_CONSOLE NULL
1124 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1127 #ifdef CONFIG_EARLY_PRINTK
1128 static __init void early_serial_putc(struct uart_port *port, int ch)
1130 unsigned timeout = 0xffff;
1131 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1133 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1135 UART_PUT_CHAR(uart, ch);
1138 static __init void early_serial_write(struct console *con, const char *s,
1141 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1144 for (i = 0; i < n; i++, s++) {
1146 early_serial_putc(&uart->port, '\r');
1147 early_serial_putc(&uart->port, *s);
1151 static struct __initdata console bfin_early_serial_console = {
1152 .name = "early_BFuart",
1153 .write = early_serial_write,
1154 .device = uart_console_device,
1155 .flags = CON_PRINTBUFFER,
1156 .setup = bfin_serial_console_setup,
1158 .data = &bfin_serial_reg,
1161 struct console __init *bfin_earlyserial_init(unsigned int port,
1164 struct bfin_serial_port *uart;
1167 if (port == -1 || port >= nr_active_ports)
1169 bfin_serial_init_ports();
1170 bfin_early_serial_console.index = port;
1171 uart = &bfin_serial_ports[port];
1177 bfin_serial_set_termios(&uart->port, &t, &t);
1178 return &bfin_early_serial_console;
1181 #endif /* CONFIG_EARLY_PRINTK */
1183 static struct uart_driver bfin_serial_reg = {
1184 .owner = THIS_MODULE,
1185 .driver_name = "bfin-uart",
1186 .dev_name = BFIN_SERIAL_NAME,
1187 .major = BFIN_SERIAL_MAJOR,
1188 .minor = BFIN_SERIAL_MINOR,
1189 .nr = BFIN_UART_NR_PORTS,
1190 .cons = BFIN_SERIAL_CONSOLE,
1193 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1197 for (i = 0; i < nr_active_ports; i++) {
1198 if (bfin_serial_ports[i].port.dev != &dev->dev)
1200 uart_suspend_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1206 static int bfin_serial_resume(struct platform_device *dev)
1210 for (i = 0; i < nr_active_ports; i++) {
1211 if (bfin_serial_ports[i].port.dev != &dev->dev)
1213 uart_resume_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1219 static int bfin_serial_probe(struct platform_device *dev)
1221 struct resource *res = dev->resource;
1224 for (i = 0; i < dev->num_resources; i++, res++)
1225 if (res->flags & IORESOURCE_MEM)
1228 if (i < dev->num_resources) {
1229 for (i = 0; i < nr_active_ports; i++, res++) {
1230 if (bfin_serial_ports[i].port.mapbase != res->start)
1232 bfin_serial_ports[i].port.dev = &dev->dev;
1233 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1240 static int bfin_serial_remove(struct platform_device *dev)
1244 for (i = 0; i < nr_active_ports; i++) {
1245 if (bfin_serial_ports[i].port.dev != &dev->dev)
1247 uart_remove_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1248 bfin_serial_ports[i].port.dev = NULL;
1249 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1250 gpio_free(bfin_serial_ports[i].cts_pin);
1251 gpio_free(bfin_serial_ports[i].rts_pin);
1258 static struct platform_driver bfin_serial_driver = {
1259 .probe = bfin_serial_probe,
1260 .remove = bfin_serial_remove,
1261 .suspend = bfin_serial_suspend,
1262 .resume = bfin_serial_resume,
1264 .name = "bfin-uart",
1265 .owner = THIS_MODULE,
1269 static int __init bfin_serial_init(void)
1273 pr_info("Serial: Blackfin serial driver\n");
1275 bfin_serial_init_ports();
1277 ret = uart_register_driver(&bfin_serial_reg);
1279 ret = platform_driver_register(&bfin_serial_driver);
1281 pr_debug("uart register failed\n");
1282 uart_unregister_driver(&bfin_serial_reg);
1288 static void __exit bfin_serial_exit(void)
1290 platform_driver_unregister(&bfin_serial_driver);
1291 uart_unregister_driver(&bfin_serial_reg);
1295 module_init(bfin_serial_init);
1296 module_exit(bfin_serial_exit);
1298 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1299 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1300 MODULE_LICENSE("GPL");
1301 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);
1302 MODULE_ALIAS("platform:bfin-uart");