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
10 * Copyright 2006 Analog Devices Inc.
12 * Bugs: Enter bugs at http://blackfin.uclinux.org/
14 * This program is free software; you can redistribute it and/or modify
15 * it under the terms of the GNU General Public License as published by
16 * the Free Software Foundation; either version 2 of the License, or
17 * (at your option) any later version.
19 * This program is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU General Public License for more details.
24 * You should have received a copy of the GNU General Public License
25 * along with this program; if not, see the file COPYING, or write
26 * to the Free Software Foundation, Inc.,
27 * 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
30 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
34 #include <linux/module.h>
35 #include <linux/ioport.h>
36 #include <linux/init.h>
37 #include <linux/console.h>
38 #include <linux/sysrq.h>
39 #include <linux/platform_device.h>
40 #include <linux/tty.h>
41 #include <linux/tty_flip.h>
42 #include <linux/serial_core.h>
44 #ifdef CONFIG_KGDB_UART
45 #include <linux/kgdb.h>
46 #include <asm/irq_regs.h>
50 #include <asm/mach/bfin_serial_5xx.h>
52 #ifdef CONFIG_SERIAL_BFIN_DMA
53 #include <linux/dma-mapping.h>
56 #include <asm/cacheflush.h>
59 /* UART name and device definitions */
60 #define BFIN_SERIAL_NAME "ttyBF"
61 #define BFIN_SERIAL_MAJOR 204
62 #define BFIN_SERIAL_MINOR 64
65 * Setup for console. Argument comes from the menuconfig
67 #define DMA_RX_XCOUNT 512
68 #define DMA_RX_YCOUNT (PAGE_SIZE / DMA_RX_XCOUNT)
70 #define DMA_RX_FLUSH_JIFFIES 5
72 #ifdef CONFIG_SERIAL_BFIN_DMA
73 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart);
75 static void bfin_serial_do_work(struct work_struct *work);
76 static void bfin_serial_tx_chars(struct bfin_serial_port *uart);
79 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart);
82 * interrupts are disabled on entry
84 static void bfin_serial_stop_tx(struct uart_port *port)
86 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
87 #if !defined(CONFIG_BF54x) && !defined(CONFIG_SERIAL_BFIN_DMA)
91 while (!(UART_GET_LSR(uart) & TEMT))
94 #ifdef CONFIG_SERIAL_BFIN_DMA
95 disable_dma(uart->tx_dma_channel);
99 UART_PUT_LSR(uart, TFI);
100 UART_CLEAR_IER(uart, ETBEI);
102 ier = UART_GET_IER(uart);
104 UART_PUT_IER(uart, ier);
110 * port is locked and interrupts are disabled
112 static void bfin_serial_start_tx(struct uart_port *port)
114 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
116 #ifdef CONFIG_SERIAL_BFIN_DMA
117 bfin_serial_dma_tx_chars(uart);
120 UART_SET_IER(uart, ETBEI);
123 ier = UART_GET_IER(uart);
125 UART_PUT_IER(uart, ier);
127 bfin_serial_tx_chars(uart);
132 * Interrupts are enabled
134 static void bfin_serial_stop_rx(struct uart_port *port)
136 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
137 #ifdef CONFIG_KGDB_UART
138 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
141 UART_CLEAR_IER(uart, ERBFI);
145 ier = UART_GET_IER(uart);
147 UART_PUT_IER(uart, ier);
149 #ifdef CONFIG_KGDB_UART
155 * Set the modem control timer to fire immediately.
157 static void bfin_serial_enable_ms(struct uart_port *port)
161 #ifdef CONFIG_KGDB_UART
162 static int kgdb_entry_state;
164 void kgdb_put_debug_char(int chr)
166 struct bfin_serial_port *uart;
168 if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
169 uart = &bfin_serial_ports[0];
171 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
173 while (!(UART_GET_LSR(uart) & THRE)) {
178 UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
181 UART_PUT_CHAR(uart, (unsigned char)chr);
185 int kgdb_get_debug_char(void)
187 struct bfin_serial_port *uart;
190 if (CONFIG_KGDB_UART_PORT<0 || CONFIG_KGDB_UART_PORT>=NR_PORTS)
191 uart = &bfin_serial_ports[0];
193 uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
195 while(!(UART_GET_LSR(uart) & DR)) {
199 UART_PUT_LCR(uart, UART_GET_LCR(uart)&(~DLAB));
202 chr = UART_GET_CHAR(uart);
209 #ifdef CONFIG_SERIAL_BFIN_PIO
210 static void bfin_serial_rx_chars(struct bfin_serial_port *uart)
212 struct tty_struct *tty = uart->port.info->tty;
213 unsigned int status, ch, flg;
214 static int in_break = 0;
215 #ifdef CONFIG_KGDB_UART
216 struct pt_regs *regs = get_irq_regs();
219 status = UART_GET_LSR(uart);
220 UART_CLEAR_LSR(uart);
222 ch = UART_GET_CHAR(uart);
223 uart->port.icount.rx++;
225 #ifdef CONFIG_KGDB_UART
226 if (uart->port.line == CONFIG_KGDB_UART_PORT) {
227 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
228 kgdb_breakkey_pressed(regs);
230 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
231 kgdb_entry_state = 1;
232 } else if (kgdb_entry_state == 1 && ch == 'q') {
233 kgdb_entry_state = 0;
234 kgdb_breakkey_pressed(regs);
236 } else if (ch == 0x3) {/* Ctrl + C */
237 kgdb_entry_state = 0;
238 kgdb_breakkey_pressed(regs);
241 kgdb_entry_state = 0;
246 if (ANOMALY_05000230) {
247 /* The BF533 family of processors have a nice misbehavior where
248 * they continuously generate characters for a "single" break.
249 * We have to basically ignore this flood until the "next" valid
250 * character comes across. All other Blackfin families operate
252 * Note: While Anomaly 05000230 does not directly address this,
253 * the changes that went in for it also fixed this issue.
258 ch = UART_GET_CHAR(uart);
259 if (bfin_revid() < 5)
267 if (ANOMALY_05000230)
269 uart->port.icount.brk++;
270 if (uart_handle_break(&uart->port))
272 status &= ~(PE | FE);
275 uart->port.icount.parity++;
277 uart->port.icount.overrun++;
279 uart->port.icount.frame++;
281 status &= uart->port.read_status_mask;
285 else if (status & PE)
287 else if (status & FE)
292 if (uart_handle_sysrq_char(&uart->port, ch))
295 uart_insert_char(&uart->port, status, OE, ch, flg);
298 tty_flip_buffer_push(tty);
301 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
303 struct circ_buf *xmit = &uart->port.info->xmit;
305 if (uart->port.x_char) {
306 UART_PUT_CHAR(uart, uart->port.x_char);
307 uart->port.icount.tx++;
308 uart->port.x_char = 0;
311 * Check the modem control lines before
312 * transmitting anything.
314 bfin_serial_mctrl_check(uart);
316 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
317 bfin_serial_stop_tx(&uart->port);
321 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
322 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
323 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
324 uart->port.icount.tx++;
328 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
329 uart_write_wakeup(&uart->port);
331 if (uart_circ_empty(xmit))
332 bfin_serial_stop_tx(&uart->port);
335 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
337 struct bfin_serial_port *uart = dev_id;
339 spin_lock(&uart->port.lock);
340 while (UART_GET_LSR(uart) & DR)
341 bfin_serial_rx_chars(uart);
342 spin_unlock(&uart->port.lock);
347 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
349 struct bfin_serial_port *uart = dev_id;
351 spin_lock(&uart->port.lock);
352 if (UART_GET_LSR(uart) & THRE)
353 bfin_serial_tx_chars(uart);
354 spin_unlock(&uart->port.lock);
360 static void bfin_serial_do_work(struct work_struct *work)
362 struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
364 bfin_serial_mctrl_check(uart);
368 #ifdef CONFIG_SERIAL_BFIN_DMA
369 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
371 struct circ_buf *xmit = &uart->port.info->xmit;
379 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
380 bfin_serial_stop_tx(&uart->port);
385 if (uart->port.x_char) {
386 UART_PUT_CHAR(uart, uart->port.x_char);
387 uart->port.icount.tx++;
388 uart->port.x_char = 0;
392 * Check the modem control lines before
393 * transmitting anything.
395 bfin_serial_mctrl_check(uart);
397 spin_lock_irqsave(&uart->port.lock, flags);
398 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
399 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
400 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
401 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
402 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
403 set_dma_config(uart->tx_dma_channel,
404 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
409 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
410 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
411 set_dma_x_modify(uart->tx_dma_channel, 1);
412 enable_dma(uart->tx_dma_channel);
414 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
415 uart->port.icount.tx += uart->tx_count;
418 UART_SET_IER(uart, ETBEI);
420 ier = UART_GET_IER(uart);
422 UART_PUT_IER(uart, ier);
424 spin_unlock_irqrestore(&uart->port.lock, flags);
427 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
429 struct tty_struct *tty = uart->port.info->tty;
432 status = UART_GET_LSR(uart);
433 UART_CLEAR_LSR(uart);
435 uart->port.icount.rx += CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail, UART_XMIT_SIZE);;
438 uart->port.icount.brk++;
439 if (uart_handle_break(&uart->port))
440 goto dma_ignore_char;
441 status &= ~(PE | FE);
444 uart->port.icount.parity++;
446 uart->port.icount.overrun++;
448 uart->port.icount.frame++;
450 status &= uart->port.read_status_mask;
454 else if (status & PE)
456 else if (status & FE)
461 for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
462 if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
463 goto dma_ignore_char;
464 uart_insert_char(&uart->port, status, OE, uart->rx_dma_buf.buf[i], flg);
468 tty_flip_buffer_push(tty);
471 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
476 spin_lock_irqsave(&uart->port.lock, flags);
477 x_pos = DMA_RX_XCOUNT - get_dma_curr_xcount(uart->rx_dma_channel);
478 if (x_pos == DMA_RX_XCOUNT)
481 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
483 if (pos>uart->rx_dma_buf.tail) {
484 uart->rx_dma_buf.tail = pos;
485 bfin_serial_dma_rx_chars(uart);
486 uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
488 spin_unlock_irqrestore(&uart->port.lock, flags);
489 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
490 add_timer(&(uart->rx_dma_timer));
493 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
495 struct bfin_serial_port *uart = dev_id;
496 struct circ_buf *xmit = &uart->port.info->xmit;
499 spin_lock(&uart->port.lock);
500 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
501 clear_dma_irqstat(uart->tx_dma_channel);
502 disable_dma(uart->tx_dma_channel);
504 UART_CLEAR_IER(uart, ETBEI);
506 ier = UART_GET_IER(uart);
508 UART_PUT_IER(uart, ier);
510 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
511 uart_write_wakeup(&uart->port);
515 bfin_serial_dma_tx_chars(uart);
518 spin_unlock(&uart->port.lock);
522 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
524 struct bfin_serial_port *uart = dev_id;
525 unsigned short irqstat;
527 uart->rx_dma_nrows++;
528 if (uart->rx_dma_nrows == DMA_RX_YCOUNT) {
529 uart->rx_dma_nrows = 0;
530 uart->rx_dma_buf.tail = DMA_RX_XCOUNT*DMA_RX_YCOUNT;
531 bfin_serial_dma_rx_chars(uart);
532 uart->rx_dma_buf.head = uart->rx_dma_buf.tail = 0;
534 spin_lock(&uart->port.lock);
535 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
536 clear_dma_irqstat(uart->rx_dma_channel);
538 spin_unlock(&uart->port.lock);
544 * Return TIOCSER_TEMT when transmitter is not busy.
546 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
548 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
551 lsr = UART_GET_LSR(uart);
558 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
560 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
561 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
562 if (uart->cts_pin < 0)
563 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
565 if (gpio_get_value(uart->cts_pin))
566 return TIOCM_DSR | TIOCM_CAR;
569 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
572 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
574 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
575 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
576 if (uart->rts_pin < 0)
579 if (mctrl & TIOCM_RTS)
580 gpio_set_value(uart->rts_pin, 0);
582 gpio_set_value(uart->rts_pin, 1);
587 * Handle any change of modem status signal since we were last called.
589 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
591 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
593 # ifdef CONFIG_SERIAL_BFIN_DMA
594 struct uart_info *info = uart->port.info;
595 struct tty_struct *tty = info->tty;
597 status = bfin_serial_get_mctrl(&uart->port);
598 if (!(status & TIOCM_CTS)) {
604 status = bfin_serial_get_mctrl(&uart->port);
605 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
606 if (!(status & TIOCM_CTS))
607 schedule_work(&uart->cts_workqueue);
613 * Interrupts are always disabled.
615 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
617 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
618 u16 lcr = UART_GET_LCR(uart);
623 UART_PUT_LCR(uart, lcr);
627 static int bfin_serial_startup(struct uart_port *port)
629 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
631 #ifdef CONFIG_SERIAL_BFIN_DMA
632 dma_addr_t dma_handle;
634 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
635 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
639 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
640 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
641 free_dma(uart->rx_dma_channel);
645 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
646 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
648 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
649 uart->rx_dma_buf.head = 0;
650 uart->rx_dma_buf.tail = 0;
651 uart->rx_dma_nrows = 0;
653 set_dma_config(uart->rx_dma_channel,
654 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
655 INTR_ON_ROW, DIMENSION_2D,
658 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
659 set_dma_x_modify(uart->rx_dma_channel, 1);
660 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
661 set_dma_y_modify(uart->rx_dma_channel, 1);
662 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
663 enable_dma(uart->rx_dma_channel);
665 uart->rx_dma_timer.data = (unsigned long)(uart);
666 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
667 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
668 add_timer(&(uart->rx_dma_timer));
670 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
671 "BFIN_UART_RX", uart)) {
672 # ifdef CONFIG_KGDB_UART
673 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
675 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
677 # ifdef CONFIG_KGDB_UART
684 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
685 "BFIN_UART_TX", uart)) {
686 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
687 free_irq(uart->port.irq, uart);
692 UART_SET_IER(uart, ERBFI);
694 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
699 static void bfin_serial_shutdown(struct uart_port *port)
701 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
703 #ifdef CONFIG_SERIAL_BFIN_DMA
704 disable_dma(uart->tx_dma_channel);
705 free_dma(uart->tx_dma_channel);
706 disable_dma(uart->rx_dma_channel);
707 free_dma(uart->rx_dma_channel);
708 del_timer(&(uart->rx_dma_timer));
709 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
711 #ifdef CONFIG_KGDB_UART
712 if (uart->port.line != CONFIG_KGDB_UART_PORT)
714 free_irq(uart->port.irq, uart);
715 free_irq(uart->port.irq+1, uart);
720 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
721 struct ktermios *old)
723 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
725 unsigned int baud, quot;
726 unsigned short val, ier, lsr, lcr = 0;
728 switch (termios->c_cflag & CSIZE) {
742 printk(KERN_ERR "%s: word lengh not supported\n",
746 if (termios->c_cflag & CSTOPB)
748 if (termios->c_cflag & PARENB)
750 if (!(termios->c_cflag & PARODD))
752 if (termios->c_cflag & CMSPAR)
755 port->read_status_mask = OE;
756 if (termios->c_iflag & INPCK)
757 port->read_status_mask |= (FE | PE);
758 if (termios->c_iflag & (BRKINT | PARMRK))
759 port->read_status_mask |= BI;
762 * Characters to ignore
764 port->ignore_status_mask = 0;
765 if (termios->c_iflag & IGNPAR)
766 port->ignore_status_mask |= FE | PE;
767 if (termios->c_iflag & IGNBRK) {
768 port->ignore_status_mask |= BI;
770 * If we're ignoring parity and break indicators,
771 * ignore overruns too (for real raw support).
773 if (termios->c_iflag & IGNPAR)
774 port->ignore_status_mask |= OE;
777 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
778 quot = uart_get_divisor(port, baud);
779 spin_lock_irqsave(&uart->port.lock, flags);
782 lsr = UART_GET_LSR(uart);
783 } while (!(lsr & TEMT));
786 ier = UART_GET_IER(uart);
788 UART_CLEAR_IER(uart, 0xF);
790 UART_PUT_IER(uart, 0);
794 /* Set DLAB in LCR to Access DLL and DLH */
795 val = UART_GET_LCR(uart);
797 UART_PUT_LCR(uart, val);
801 UART_PUT_DLL(uart, quot & 0xFF);
803 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
807 /* Clear DLAB in LCR to Access THR RBR IER */
808 val = UART_GET_LCR(uart);
810 UART_PUT_LCR(uart, val);
814 UART_PUT_LCR(uart, lcr);
818 UART_SET_IER(uart, ier);
820 UART_PUT_IER(uart, ier);
823 val = UART_GET_GCTL(uart);
825 UART_PUT_GCTL(uart, val);
827 spin_unlock_irqrestore(&uart->port.lock, flags);
830 static const char *bfin_serial_type(struct uart_port *port)
832 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
834 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
838 * Release the memory region(s) being used by 'port'.
840 static void bfin_serial_release_port(struct uart_port *port)
845 * Request the memory region(s) being used by 'port'.
847 static int bfin_serial_request_port(struct uart_port *port)
853 * Configure/autoconfigure the port.
855 static void bfin_serial_config_port(struct uart_port *port, int flags)
857 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
859 if (flags & UART_CONFIG_TYPE &&
860 bfin_serial_request_port(&uart->port) == 0)
861 uart->port.type = PORT_BFIN;
865 * Verify the new serial_struct (for TIOCSSERIAL).
866 * The only change we allow are to the flags and type, and
867 * even then only between PORT_BFIN and PORT_UNKNOWN
870 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
875 static struct uart_ops bfin_serial_pops = {
876 .tx_empty = bfin_serial_tx_empty,
877 .set_mctrl = bfin_serial_set_mctrl,
878 .get_mctrl = bfin_serial_get_mctrl,
879 .stop_tx = bfin_serial_stop_tx,
880 .start_tx = bfin_serial_start_tx,
881 .stop_rx = bfin_serial_stop_rx,
882 .enable_ms = bfin_serial_enable_ms,
883 .break_ctl = bfin_serial_break_ctl,
884 .startup = bfin_serial_startup,
885 .shutdown = bfin_serial_shutdown,
886 .set_termios = bfin_serial_set_termios,
887 .type = bfin_serial_type,
888 .release_port = bfin_serial_release_port,
889 .request_port = bfin_serial_request_port,
890 .config_port = bfin_serial_config_port,
891 .verify_port = bfin_serial_verify_port,
894 static void __init bfin_serial_init_ports(void)
896 static int first = 1;
903 for (i = 0; i < nr_ports; i++) {
904 bfin_serial_ports[i].port.uartclk = get_sclk();
905 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
906 bfin_serial_ports[i].port.line = i;
907 bfin_serial_ports[i].port.iotype = UPIO_MEM;
908 bfin_serial_ports[i].port.membase =
909 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
910 bfin_serial_ports[i].port.mapbase =
911 bfin_serial_resource[i].uart_base_addr;
912 bfin_serial_ports[i].port.irq =
913 bfin_serial_resource[i].uart_irq;
914 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
915 #ifdef CONFIG_SERIAL_BFIN_DMA
916 bfin_serial_ports[i].tx_done = 1;
917 bfin_serial_ports[i].tx_count = 0;
918 bfin_serial_ports[i].tx_dma_channel =
919 bfin_serial_resource[i].uart_tx_dma_channel;
920 bfin_serial_ports[i].rx_dma_channel =
921 bfin_serial_resource[i].uart_rx_dma_channel;
922 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
924 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
926 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
927 bfin_serial_ports[i].cts_pin =
928 bfin_serial_resource[i].uart_cts_pin;
929 bfin_serial_ports[i].rts_pin =
930 bfin_serial_resource[i].uart_rts_pin;
932 bfin_serial_hw_init(&bfin_serial_ports[i]);
937 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
939 * If the port was already initialised (eg, by a boot loader),
940 * try to determine the current setup.
943 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
944 int *parity, int *bits)
946 unsigned short status;
948 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
949 if (status == (ERBFI | ETBEI)) {
950 /* ok, the port was enabled */
951 unsigned short lcr, val;
952 unsigned short dlh, dll;
954 lcr = UART_GET_LCR(uart);
963 switch (lcr & 0x03) {
964 case 0: *bits = 5; break;
965 case 1: *bits = 6; break;
966 case 2: *bits = 7; break;
967 case 3: *bits = 8; break;
970 /* Set DLAB in LCR to Access DLL and DLH */
971 val = UART_GET_LCR(uart);
973 UART_PUT_LCR(uart, val);
976 dll = UART_GET_DLL(uart);
977 dlh = UART_GET_DLH(uart);
980 /* Clear DLAB in LCR to Access THR RBR IER */
981 val = UART_GET_LCR(uart);
983 UART_PUT_LCR(uart, val);
986 *baud = get_sclk() / (16*(dll | dlh << 8));
988 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
992 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
993 static struct uart_driver bfin_serial_reg;
996 bfin_serial_console_setup(struct console *co, char *options)
998 struct bfin_serial_port *uart;
999 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1003 # ifdef CONFIG_SERIAL_BFIN_CTSRTS
1011 * Check whether an invalid uart number has been specified, and
1012 * if so, search for the first available port that does have
1015 if (co->index == -1 || co->index >= nr_ports)
1017 uart = &bfin_serial_ports[co->index];
1019 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1021 uart_parse_options(options, &baud, &parity, &bits, &flow);
1023 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1025 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1030 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1031 defined (CONFIG_EARLY_PRINTK) */
1033 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1034 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1036 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1037 while (!(UART_GET_LSR(uart) & THRE))
1039 UART_PUT_CHAR(uart, ch);
1044 * Interrupts are disabled on entering
1047 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1049 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1052 spin_lock_irqsave(&uart->port.lock, flags);
1053 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1054 spin_unlock_irqrestore(&uart->port.lock, flags);
1058 static struct console bfin_serial_console = {
1059 .name = BFIN_SERIAL_NAME,
1060 .write = bfin_serial_console_write,
1061 .device = uart_console_device,
1062 .setup = bfin_serial_console_setup,
1063 .flags = CON_PRINTBUFFER,
1065 .data = &bfin_serial_reg,
1068 static int __init bfin_serial_rs_console_init(void)
1070 bfin_serial_init_ports();
1071 register_console(&bfin_serial_console);
1072 #ifdef CONFIG_KGDB_UART
1073 kgdb_entry_state = 0;
1078 console_initcall(bfin_serial_rs_console_init);
1080 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1082 #define BFIN_SERIAL_CONSOLE NULL
1083 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1086 #ifdef CONFIG_EARLY_PRINTK
1087 static __init void early_serial_putc(struct uart_port *port, int ch)
1089 unsigned timeout = 0xffff;
1090 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1092 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1094 UART_PUT_CHAR(uart, ch);
1097 static __init void early_serial_write(struct console *con, const char *s,
1100 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1103 for (i = 0; i < n; i++, s++) {
1105 early_serial_putc(&uart->port, '\r');
1106 early_serial_putc(&uart->port, *s);
1110 static struct __init console bfin_early_serial_console = {
1111 .name = "early_BFuart",
1112 .write = early_serial_write,
1113 .device = uart_console_device,
1114 .flags = CON_PRINTBUFFER,
1115 .setup = bfin_serial_console_setup,
1117 .data = &bfin_serial_reg,
1120 struct console __init *bfin_earlyserial_init(unsigned int port,
1123 struct bfin_serial_port *uart;
1126 if (port == -1 || port >= nr_ports)
1128 bfin_serial_init_ports();
1129 bfin_early_serial_console.index = port;
1130 uart = &bfin_serial_ports[port];
1136 bfin_serial_set_termios(&uart->port, &t, &t);
1137 return &bfin_early_serial_console;
1140 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1142 static struct uart_driver bfin_serial_reg = {
1143 .owner = THIS_MODULE,
1144 .driver_name = "bfin-uart",
1145 .dev_name = BFIN_SERIAL_NAME,
1146 .major = BFIN_SERIAL_MAJOR,
1147 .minor = BFIN_SERIAL_MINOR,
1149 .cons = BFIN_SERIAL_CONSOLE,
1152 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1154 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1157 uart_suspend_port(&bfin_serial_reg, &uart->port);
1162 static int bfin_serial_resume(struct platform_device *dev)
1164 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1167 uart_resume_port(&bfin_serial_reg, &uart->port);
1172 static int bfin_serial_probe(struct platform_device *dev)
1174 struct resource *res = dev->resource;
1177 for (i = 0; i < dev->num_resources; i++, res++)
1178 if (res->flags & IORESOURCE_MEM)
1181 if (i < dev->num_resources) {
1182 for (i = 0; i < nr_ports; i++, res++) {
1183 if (bfin_serial_ports[i].port.mapbase != res->start)
1185 bfin_serial_ports[i].port.dev = &dev->dev;
1186 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1187 platform_set_drvdata(dev, &bfin_serial_ports[i]);
1194 static int bfin_serial_remove(struct platform_device *pdev)
1196 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1199 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1200 gpio_free(uart->cts_pin);
1201 gpio_free(uart->rts_pin);
1204 platform_set_drvdata(pdev, NULL);
1207 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1212 static struct platform_driver bfin_serial_driver = {
1213 .probe = bfin_serial_probe,
1214 .remove = bfin_serial_remove,
1215 .suspend = bfin_serial_suspend,
1216 .resume = bfin_serial_resume,
1218 .name = "bfin-uart",
1222 static int __init bfin_serial_init(void)
1225 #ifdef CONFIG_KGDB_UART
1226 struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
1230 pr_info("Serial: Blackfin serial driver\n");
1232 bfin_serial_init_ports();
1234 ret = uart_register_driver(&bfin_serial_reg);
1236 ret = platform_driver_register(&bfin_serial_driver);
1238 pr_debug("uart register failed\n");
1239 uart_unregister_driver(&bfin_serial_reg);
1242 #ifdef CONFIG_KGDB_UART
1243 if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
1244 request_irq(uart->port.irq, bfin_serial_rx_int,
1245 IRQF_DISABLED, "BFIN_UART_RX", uart);
1246 pr_info("Request irq for kgdb uart port\n");
1248 UART_SET_IER(uart, ERBFI);
1250 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
1253 t.c_cflag = CS8|B57600;
1257 t.c_line = CONFIG_KGDB_UART_PORT;
1258 bfin_serial_set_termios(&uart->port, &t, &t);
1264 static void __exit bfin_serial_exit(void)
1266 platform_driver_unregister(&bfin_serial_driver);
1267 uart_unregister_driver(&bfin_serial_reg);
1270 module_init(bfin_serial_init);
1271 module_exit(bfin_serial_exit);
1273 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1274 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1275 MODULE_LICENSE("GPL");
1276 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);