2 * File: drivers/serial/bfin_5xx.c
3 * Based on: Based on drivers/serial/sa1100.c
4 * Author: Aubrey Li <aubrey.li@analog.com>
7 * Description: Driver for blackfin 5xx serial ports
9 * Rev: $Id: bfin_5xx.c,v 1.19 2006/09/24 02:33:53 aubrey Exp $
12 * Copyright 2006 Analog Devices Inc.
14 * Bugs: Enter bugs at http://blackfin.uclinux.org/
16 * This program is free software; you can redistribute it and/or modify
17 * it under the terms of the GNU General Public License as published by
18 * the Free Software Foundation; either version 2 of the License, or
19 * (at your option) any later version.
21 * This program is distributed in the hope that it will be useful,
22 * but WITHOUT ANY WARRANTY; without even the implied warranty of
23 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 * GNU General Public License for more details.
26 * You should have received a copy of the GNU General Public License
27 * along with this program; if not, see the file COPYING, or write
28 * to the Free Software Foundation, Inc.,
29 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
32 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
36 #include <linux/module.h>
37 #include <linux/ioport.h>
38 #include <linux/init.h>
39 #include <linux/console.h>
40 #include <linux/sysrq.h>
41 #include <linux/platform_device.h>
42 #include <linux/tty.h>
43 #include <linux/tty_flip.h>
44 #include <linux/serial_core.h>
47 #include <asm/mach/bfin_serial_5xx.h>
49 #ifdef CONFIG_SERIAL_BFIN_DMA
50 #include <linux/dma-mapping.h>
53 #include <asm/cacheflush.h>
56 /* UART name and device definitions */
57 #define BFIN_SERIAL_NAME "ttyBF"
58 #define BFIN_SERIAL_MAJOR 204
59 #define BFIN_SERIAL_MINOR 64
62 * Setup for console. Argument comes from the menuconfig
64 #define DMA_RX_XCOUNT 512
65 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
67 #define DMA_RX_FLUSH_JIFFIES 5
69 #ifdef CONFIG_SERIAL_BFIN_DMA
70 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
72 static void bfin_serial_do_work(struct work_struct *work);
73 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
74 static void local_put_char(struct bfin_serial_port *uart, char ch);
77 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
80 * interrupts are disabled on entry
82 static void bfin_serial_stop_tx(struct uart_port *port)
84 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
86 #ifdef CONFIG_SERIAL_BFIN_DMA
87 disable_dma(uart->tx_dma_channel);
91 ier = UART_GET_IER(uart);
93 UART_PUT_IER(uart, ier);
98 * port is locked and interrupts are disabled
100 static void bfin_serial_start_tx(struct uart_port *port)
102 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
104 #ifdef CONFIG_SERIAL_BFIN_DMA
105 bfin_serial_dma_tx_chars(uart);
108 ier = UART_GET_IER(uart);
110 UART_PUT_IER(uart, ier);
111 bfin_serial_tx_chars(uart);
116 * Interrupts are enabled
118 static void bfin_serial_stop_rx(struct uart_port *port)
120 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
123 ier = UART_GET_IER(uart);
125 UART_PUT_IER(uart, ier);
129 * Set the modem control timer to fire immediately.
131 static void bfin_serial_enable_ms(struct uart_port *port)
135 #ifdef CONFIG_SERIAL_BFIN_PIO
136 static void local_put_char(struct bfin_serial_port *uart, char ch)
138 unsigned short status;
141 spin_lock_irqsave(&uart->port.lock, flags);
144 status = UART_GET_LSR(uart);
145 } while (!(status & THRE));
147 UART_PUT_CHAR(uart, ch);
150 spin_unlock_irqrestore(&uart->port.lock, flags);
153 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
155 struct tty_struct *tty = uart->port.info?uart->port.info->tty:0;
156 unsigned int status, ch, flg;
158 static int in_break = 0;
161 status = UART_GET_LSR(uart);
162 ch = UART_GET_CHAR(uart);
163 uart->port.icount.rx++;
166 /* The BF533 family of processors have a nice misbehavior where
167 * they continuously generate characters for a "single" break.
168 * We have to basically ignore this flood until the "next" valid
169 * character comes across. All other Blackfin families operate
175 ch = UART_GET_CHAR(uart);
185 uart->port.icount.brk++;
186 if (uart_handle_break(&uart->port))
189 } else if (status & PE) {
191 uart->port.icount.parity++;
192 } else if (status & OE) {
194 uart->port.icount.overrun++;
195 } else if (status & FE) {
197 uart->port.icount.frame++;
201 if (uart_handle_sysrq_char(&uart->port, ch))
204 uart_insert_char(&uart->port, status, 2, ch, flg);
208 tty_flip_buffer_push(tty);
211 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
213 struct circ_buf *xmit = &uart->port.info->xmit;
215 if (uart->port.x_char) {
216 UART_PUT_CHAR(uart, uart->port.x_char);
217 uart->port.icount.tx++;
218 uart->port.x_char = 0;
222 * Check the modem control lines before
223 * transmitting anything.
225 bfin_serial_mctrl_check(uart);
227 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
228 bfin_serial_stop_tx(&uart->port);
232 local_put_char(uart, xmit->buf[xmit->tail]);
233 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
234 uart->port.icount.tx++;
236 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
237 uart_write_wakeup(&uart->port);
239 if (uart_circ_empty(xmit))
240 bfin_serial_stop_tx(&uart->port);
243 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
245 struct bfin_serial_port *uart = dev_id;
247 spin_lock(&uart->port.lock);
248 while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_RX_READY)
249 bfin_serial_rx_chars(uart);
250 spin_unlock(&uart->port.lock);
254 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
256 struct bfin_serial_port *uart = dev_id;
257 unsigned short status;
259 spin_lock(&uart->port.lock);
260 while ((UART_GET_IIR(uart) & IIR_STATUS) == IIR_TX_READY)
261 bfin_serial_tx_chars(uart);
262 spin_unlock(&uart->port.lock);
267 static void bfin_serial_do_work(struct work_struct *work)
269 struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
271 bfin_serial_mctrl_check(uart);
276 #ifdef CONFIG_SERIAL_BFIN_DMA
277 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
279 struct circ_buf *xmit = &uart->port.info->xmit;
288 if (uart->port.x_char) {
289 UART_PUT_CHAR(uart, uart->port.x_char);
290 uart->port.icount.tx++;
291 uart->port.x_char = 0;
296 * Check the modem control lines before
297 * transmitting anything.
299 bfin_serial_mctrl_check(uart);
301 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
302 bfin_serial_stop_tx(&uart->port);
307 spin_lock_irqsave(&uart->port.lock, flags);
308 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
309 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
310 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
311 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
312 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
313 set_dma_config(uart->tx_dma_channel,
314 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
318 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
319 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
320 set_dma_x_modify(uart->tx_dma_channel, 1);
321 enable_dma(uart->tx_dma_channel);
322 ier = UART_GET_IER(uart);
324 UART_PUT_IER(uart, ier);
325 spin_unlock_irqrestore(&uart->port.lock, flags);
328 static void bfin_serial_dma_rx_chars(struct bfin_serial_port * uart)
330 struct tty_struct *tty = uart->port.info->tty;
333 status = UART_GET_LSR(uart);
334 uart->port.icount.rx += CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail, UART_XMIT_SIZE);;
337 uart->port.icount.brk++;
338 if (uart_handle_break(&uart->port))
339 goto dma_ignore_char;
341 } else if (status & PE) {
343 uart->port.icount.parity++;
344 } else if (status & OE) {
346 uart->port.icount.overrun++;
347 } else if (status & FE) {
349 uart->port.icount.frame++;
353 for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
354 if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
355 goto dma_ignore_char;
356 uart_insert_char(&uart->port, status, 2, uart->rx_dma_buf.buf[i], flg);
359 tty_flip_buffer_push(tty);
362 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
367 bfin_serial_dma_tx_chars(uart);
369 spin_lock_irqsave(&uart->port.lock, flags);
370 x_pos = DMA_RX_XCOUNT - get_dma_curr_xcount(uart->rx_dma_channel);
371 if (x_pos == DMA_RX_XCOUNT)
374 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
376 if (pos>uart->rx_dma_buf.tail) {
377 uart->rx_dma_buf.tail = pos;
378 bfin_serial_dma_rx_chars(uart);
379 uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
381 spin_unlock_irqrestore(&uart->port.lock, flags);
382 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
383 add_timer(&(uart->rx_dma_timer));
386 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
388 struct bfin_serial_port *uart = dev_id;
389 struct circ_buf *xmit = &uart->port.info->xmit;
392 spin_lock(&uart->port.lock);
393 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
394 clear_dma_irqstat(uart->tx_dma_channel);
395 disable_dma(uart->tx_dma_channel);
396 ier = UART_GET_IER(uart);
398 UART_PUT_IER(uart, ier);
399 xmit->tail = (xmit->tail+uart->tx_count) &(UART_XMIT_SIZE -1);
400 uart->port.icount.tx+=uart->tx_count;
402 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
403 uart_write_wakeup(&uart->port);
405 if (uart_circ_empty(xmit))
406 bfin_serial_stop_tx(&uart->port);
410 spin_unlock(&uart->port.lock);
414 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
416 struct bfin_serial_port *uart = dev_id;
417 unsigned short irqstat;
419 uart->rx_dma_nrows++;
420 if (uart->rx_dma_nrows == DMA_RX_YCOUNT) {
421 uart->rx_dma_nrows = 0;
422 uart->rx_dma_buf.tail = DMA_RX_XCOUNT*DMA_RX_YCOUNT;
423 bfin_serial_dma_rx_chars(uart);
424 uart->rx_dma_buf.head = uart->rx_dma_buf.tail = 0;
426 spin_lock(&uart->port.lock);
427 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
428 clear_dma_irqstat(uart->rx_dma_channel);
430 spin_unlock(&uart->port.lock);
436 * Return TIOCSER_TEMT when transmitter is not busy.
438 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
440 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
443 lsr = UART_GET_LSR(uart);
450 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
452 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
453 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
454 if (uart->cts_pin < 0)
455 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
457 if (gpio_get_value(uart->cts_pin))
458 return TIOCM_DSR | TIOCM_CAR;
461 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
464 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
466 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
467 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
468 if (uart->rts_pin < 0)
471 if (mctrl & TIOCM_RTS)
472 gpio_set_value(uart->rts_pin, 0);
474 gpio_set_value(uart->rts_pin, 1);
479 * Handle any change of modem status signal since we were last called.
481 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
483 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
485 # ifdef CONFIG_SERIAL_BFIN_DMA
486 struct uart_info *info = uart->port.info;
487 struct tty_struct *tty = info->tty;
489 status = bfin_serial_get_mctrl(&uart->port);
490 if (!(status & TIOCM_CTS)) {
496 status = bfin_serial_get_mctrl(&uart->port);
497 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
498 if (!(status & TIOCM_CTS))
499 schedule_work(&uart->cts_workqueue);
505 * Interrupts are always disabled.
507 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
511 static int bfin_serial_startup(struct uart_port *port)
513 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
515 #ifdef CONFIG_SERIAL_BFIN_DMA
516 dma_addr_t dma_handle;
518 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
519 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
523 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
524 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
525 free_dma(uart->rx_dma_channel);
529 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
530 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
532 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
533 uart->rx_dma_buf.head = 0;
534 uart->rx_dma_buf.tail = 0;
535 uart->rx_dma_nrows = 0;
537 set_dma_config(uart->rx_dma_channel,
538 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
539 INTR_ON_ROW, DIMENSION_2D,
541 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
542 set_dma_x_modify(uart->rx_dma_channel, 1);
543 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
544 set_dma_y_modify(uart->rx_dma_channel, 1);
545 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
546 enable_dma(uart->rx_dma_channel);
548 uart->rx_dma_timer.data = (unsigned long)(uart);
549 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
550 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
551 add_timer(&(uart->rx_dma_timer));
554 (uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
555 "BFIN_UART_RX", uart)) {
556 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
561 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
562 "BFIN_UART_TX", uart)) {
563 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
564 free_irq(uart->port.irq, uart);
568 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
572 static void bfin_serial_shutdown(struct uart_port *port)
574 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
576 #ifdef CONFIG_SERIAL_BFIN_DMA
577 disable_dma(uart->tx_dma_channel);
578 free_dma(uart->tx_dma_channel);
579 disable_dma(uart->rx_dma_channel);
580 free_dma(uart->rx_dma_channel);
581 del_timer(&(uart->rx_dma_timer));
583 free_irq(uart->port.irq, uart);
584 free_irq(uart->port.irq+1, uart);
589 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
590 struct ktermios *old)
592 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
594 unsigned int baud, quot;
595 unsigned short val, ier, lsr, lcr = 0;
597 switch (termios->c_cflag & CSIZE) {
611 printk(KERN_ERR "%s: word lengh not supported\n",
615 if (termios->c_cflag & CSTOPB)
617 if (termios->c_cflag & PARENB) {
619 if (!(termios->c_cflag & PARODD))
623 /* These controls are not implemented for this port */
624 termios->c_iflag |= INPCK | BRKINT | PARMRK;
625 termios->c_iflag &= ~(IGNPAR | IGNBRK);
627 /* These controls are not implemented for this port */
628 termios->c_iflag |= INPCK | BRKINT | PARMRK;
629 termios->c_iflag &= ~(IGNPAR | IGNBRK);
631 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
632 quot = uart_get_divisor(port, baud);
633 spin_lock_irqsave(&uart->port.lock, flags);
636 lsr = UART_GET_LSR(uart);
637 } while (!(lsr & TEMT));
640 ier = UART_GET_IER(uart);
641 UART_PUT_IER(uart, 0);
643 /* Set DLAB in LCR to Access DLL and DLH */
644 val = UART_GET_LCR(uart);
646 UART_PUT_LCR(uart, val);
649 UART_PUT_DLL(uart, quot & 0xFF);
651 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
654 /* Clear DLAB in LCR to Access THR RBR IER */
655 val = UART_GET_LCR(uart);
657 UART_PUT_LCR(uart, val);
660 UART_PUT_LCR(uart, lcr);
663 UART_PUT_IER(uart, ier);
665 val = UART_GET_GCTL(uart);
667 UART_PUT_GCTL(uart, val);
669 spin_unlock_irqrestore(&uart->port.lock, flags);
672 static const char *bfin_serial_type(struct uart_port *port)
674 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
676 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
680 * Release the memory region(s) being used by 'port'.
682 static void bfin_serial_release_port(struct uart_port *port)
687 * Request the memory region(s) being used by 'port'.
689 static int bfin_serial_request_port(struct uart_port *port)
695 * Configure/autoconfigure the port.
697 static void bfin_serial_config_port(struct uart_port *port, int flags)
699 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
701 if (flags & UART_CONFIG_TYPE &&
702 bfin_serial_request_port(&uart->port) == 0)
703 uart->port.type = PORT_BFIN;
707 * Verify the new serial_struct (for TIOCSSERIAL).
708 * The only change we allow are to the flags and type, and
709 * even then only between PORT_BFIN and PORT_UNKNOWN
712 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
717 static struct uart_ops bfin_serial_pops = {
718 .tx_empty = bfin_serial_tx_empty,
719 .set_mctrl = bfin_serial_set_mctrl,
720 .get_mctrl = bfin_serial_get_mctrl,
721 .stop_tx = bfin_serial_stop_tx,
722 .start_tx = bfin_serial_start_tx,
723 .stop_rx = bfin_serial_stop_rx,
724 .enable_ms = bfin_serial_enable_ms,
725 .break_ctl = bfin_serial_break_ctl,
726 .startup = bfin_serial_startup,
727 .shutdown = bfin_serial_shutdown,
728 .set_termios = bfin_serial_set_termios,
729 .type = bfin_serial_type,
730 .release_port = bfin_serial_release_port,
731 .request_port = bfin_serial_request_port,
732 .config_port = bfin_serial_config_port,
733 .verify_port = bfin_serial_verify_port,
736 static void __init bfin_serial_init_ports(void)
738 static int first = 1;
745 for (i = 0; i < nr_ports; i++) {
746 bfin_serial_ports[i].port.uartclk = get_sclk();
747 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
748 bfin_serial_ports[i].port.line = i;
749 bfin_serial_ports[i].port.iotype = UPIO_MEM;
750 bfin_serial_ports[i].port.membase =
751 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
752 bfin_serial_ports[i].port.mapbase =
753 bfin_serial_resource[i].uart_base_addr;
754 bfin_serial_ports[i].port.irq =
755 bfin_serial_resource[i].uart_irq;
756 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
757 #ifdef CONFIG_SERIAL_BFIN_DMA
758 bfin_serial_ports[i].tx_done = 1;
759 bfin_serial_ports[i].tx_count = 0;
760 bfin_serial_ports[i].tx_dma_channel =
761 bfin_serial_resource[i].uart_tx_dma_channel;
762 bfin_serial_ports[i].rx_dma_channel =
763 bfin_serial_resource[i].uart_rx_dma_channel;
764 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
766 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
768 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
769 bfin_serial_ports[i].cts_pin =
770 bfin_serial_resource[i].uart_cts_pin;
771 bfin_serial_ports[i].rts_pin =
772 bfin_serial_resource[i].uart_rts_pin;
774 bfin_serial_hw_init(&bfin_serial_ports[i]);
779 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
780 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
782 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
783 while (!(UART_GET_LSR(uart)))
785 UART_PUT_CHAR(uart, ch);
790 * Interrupts are disabled on entering
793 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
795 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
798 spin_lock_irqsave(&uart->port.lock, flags);
799 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
800 spin_unlock_irqrestore(&uart->port.lock, flags);
805 * If the port was already initialised (eg, by a boot loader),
806 * try to determine the current setup.
809 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
810 int *parity, int *bits)
812 unsigned short status;
814 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
815 if (status == (ERBFI | ETBEI)) {
816 /* ok, the port was enabled */
817 unsigned short lcr, val;
818 unsigned short dlh, dll;
820 lcr = UART_GET_LCR(uart);
829 switch (lcr & 0x03) {
830 case 0: *bits = 5; break;
831 case 1: *bits = 6; break;
832 case 2: *bits = 7; break;
833 case 3: *bits = 8; break;
835 /* Set DLAB in LCR to Access DLL and DLH */
836 val = UART_GET_LCR(uart);
838 UART_PUT_LCR(uart, val);
840 dll = UART_GET_DLL(uart);
841 dlh = UART_GET_DLH(uart);
843 /* Clear DLAB in LCR to Access THR RBR IER */
844 val = UART_GET_LCR(uart);
846 UART_PUT_LCR(uart, val);
848 *baud = get_sclk() / (16*(dll | dlh << 8));
850 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
854 bfin_serial_console_setup(struct console *co, char *options)
856 struct bfin_serial_port *uart;
860 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
867 * Check whether an invalid uart number has been specified, and
868 * if so, search for the first available port that does have
871 if (co->index == -1 || co->index >= nr_ports)
873 uart = &bfin_serial_ports[co->index];
876 uart_parse_options(options, &baud, &parity, &bits, &flow);
878 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
880 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
883 static struct uart_driver bfin_serial_reg;
884 static struct console bfin_serial_console = {
885 .name = BFIN_SERIAL_NAME,
886 .write = bfin_serial_console_write,
887 .device = uart_console_device,
888 .setup = bfin_serial_console_setup,
889 .flags = CON_PRINTBUFFER,
891 .data = &bfin_serial_reg,
894 static int __init bfin_serial_rs_console_init(void)
896 bfin_serial_init_ports();
897 register_console(&bfin_serial_console);
900 console_initcall(bfin_serial_rs_console_init);
902 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
904 #define BFIN_SERIAL_CONSOLE NULL
907 static struct uart_driver bfin_serial_reg = {
908 .owner = THIS_MODULE,
909 .driver_name = "bfin-uart",
910 .dev_name = BFIN_SERIAL_NAME,
911 .major = BFIN_SERIAL_MAJOR,
912 .minor = BFIN_SERIAL_MINOR,
914 .cons = BFIN_SERIAL_CONSOLE,
917 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
919 struct bfin_serial_port *uart = platform_get_drvdata(dev);
922 uart_suspend_port(&bfin_serial_reg, &uart->port);
927 static int bfin_serial_resume(struct platform_device *dev)
929 struct bfin_serial_port *uart = platform_get_drvdata(dev);
932 uart_resume_port(&bfin_serial_reg, &uart->port);
937 static int bfin_serial_probe(struct platform_device *dev)
939 struct resource *res = dev->resource;
942 for (i = 0; i < dev->num_resources; i++, res++)
943 if (res->flags & IORESOURCE_MEM)
946 if (i < dev->num_resources) {
947 for (i = 0; i < nr_ports; i++, res++) {
948 if (bfin_serial_ports[i].port.mapbase != res->start)
950 bfin_serial_ports[i].port.dev = &dev->dev;
951 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
952 platform_set_drvdata(dev, &bfin_serial_ports[i]);
959 static int bfin_serial_remove(struct platform_device *pdev)
961 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
964 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
965 gpio_free(uart->cts_pin);
966 gpio_free(uart->rts_pin);
969 platform_set_drvdata(pdev, NULL);
972 uart_remove_one_port(&bfin_serial_reg, &uart->port);
977 static struct platform_driver bfin_serial_driver = {
978 .probe = bfin_serial_probe,
979 .remove = bfin_serial_remove,
980 .suspend = bfin_serial_suspend,
981 .resume = bfin_serial_resume,
987 static int __init bfin_serial_init(void)
991 pr_info("Serial: Blackfin serial driver\n");
993 bfin_serial_init_ports();
995 ret = uart_register_driver(&bfin_serial_reg);
997 ret = platform_driver_register(&bfin_serial_driver);
999 pr_debug("uart register failed\n");
1000 uart_unregister_driver(&bfin_serial_reg);
1006 static void __exit bfin_serial_exit(void)
1008 platform_driver_unregister(&bfin_serial_driver);
1009 uart_unregister_driver(&bfin_serial_reg);
1012 module_init(bfin_serial_init);
1013 module_exit(bfin_serial_exit);
1015 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1016 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1017 MODULE_LICENSE("GPL");
1018 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);