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 ch = UART_GET_CHAR(uart);
220 status = UART_GET_LSR(uart);
221 uart->port.icount.rx++;
223 #ifdef CONFIG_KGDB_UART
224 if (uart->port.line == CONFIG_KGDB_UART_PORT) {
225 if (uart->port.cons->index == CONFIG_KGDB_UART_PORT && ch == 0x1) { /* Ctrl + A */
226 kgdb_breakkey_pressed(regs);
228 } else if (kgdb_entry_state == 0 && ch == '$') {/* connection from KGDB */
229 kgdb_entry_state = 1;
230 } else if (kgdb_entry_state == 1 && ch == 'q') {
231 kgdb_entry_state = 0;
232 kgdb_breakkey_pressed(regs);
234 } else if (ch == 0x3) {/* Ctrl + C */
235 kgdb_entry_state = 0;
236 kgdb_breakkey_pressed(regs);
239 kgdb_entry_state = 0;
244 if (ANOMALY_05000230) {
245 /* The BF533 family of processors have a nice misbehavior where
246 * they continuously generate characters for a "single" break.
247 * We have to basically ignore this flood until the "next" valid
248 * character comes across. All other Blackfin families operate
250 * Note: While Anomaly 05000230 does not directly address this,
251 * the changes that went in for it also fixed this issue.
256 ch = UART_GET_CHAR(uart);
257 if (bfin_revid() < 5)
265 if (ANOMALY_05000230)
267 uart->port.icount.brk++;
268 if (uart_handle_break(&uart->port))
270 status &= ~(PE | FE);
273 uart->port.icount.parity++;
275 uart->port.icount.overrun++;
277 uart->port.icount.frame++;
279 status &= uart->port.read_status_mask;
283 else if (status & PE)
285 else if (status & FE)
290 if (uart_handle_sysrq_char(&uart->port, ch))
293 uart_insert_char(&uart->port, status, OE, ch, flg);
296 tty_flip_buffer_push(tty);
299 static void bfin_serial_tx_chars(struct bfin_serial_port *uart)
301 struct circ_buf *xmit = &uart->port.info->xmit;
303 if (uart->port.x_char) {
304 UART_PUT_CHAR(uart, uart->port.x_char);
305 uart->port.icount.tx++;
306 uart->port.x_char = 0;
309 * Check the modem control lines before
310 * transmitting anything.
312 bfin_serial_mctrl_check(uart);
314 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
315 bfin_serial_stop_tx(&uart->port);
319 while ((UART_GET_LSR(uart) & THRE) && xmit->tail != xmit->head) {
320 UART_PUT_CHAR(uart, xmit->buf[xmit->tail]);
321 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
322 uart->port.icount.tx++;
326 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
327 uart_write_wakeup(&uart->port);
329 if (uart_circ_empty(xmit))
330 bfin_serial_stop_tx(&uart->port);
333 static irqreturn_t bfin_serial_rx_int(int irq, void *dev_id)
335 struct bfin_serial_port *uart = dev_id;
337 spin_lock(&uart->port.lock);
338 while ((UART_GET_IER(uart) & ERBFI) && (UART_GET_LSR(uart) & DR))
339 bfin_serial_rx_chars(uart);
340 spin_unlock(&uart->port.lock);
345 static irqreturn_t bfin_serial_tx_int(int irq, void *dev_id)
347 struct bfin_serial_port *uart = dev_id;
349 spin_lock(&uart->port.lock);
350 if ((UART_GET_IER(uart) & ETBEI) && (UART_GET_LSR(uart) & THRE))
351 bfin_serial_tx_chars(uart);
352 spin_unlock(&uart->port.lock);
358 static void bfin_serial_do_work(struct work_struct *work)
360 struct bfin_serial_port *uart = container_of(work, struct bfin_serial_port, cts_workqueue);
362 bfin_serial_mctrl_check(uart);
366 #ifdef CONFIG_SERIAL_BFIN_DMA
367 static void bfin_serial_dma_tx_chars(struct bfin_serial_port *uart)
369 struct circ_buf *xmit = &uart->port.info->xmit;
377 if (uart_circ_empty(xmit) || uart_tx_stopped(&uart->port)) {
378 bfin_serial_stop_tx(&uart->port);
383 if (uart->port.x_char) {
384 UART_PUT_CHAR(uart, uart->port.x_char);
385 uart->port.icount.tx++;
386 uart->port.x_char = 0;
390 * Check the modem control lines before
391 * transmitting anything.
393 bfin_serial_mctrl_check(uart);
395 spin_lock_irqsave(&uart->port.lock, flags);
396 uart->tx_count = CIRC_CNT(xmit->head, xmit->tail, UART_XMIT_SIZE);
397 if (uart->tx_count > (UART_XMIT_SIZE - xmit->tail))
398 uart->tx_count = UART_XMIT_SIZE - xmit->tail;
399 blackfin_dcache_flush_range((unsigned long)(xmit->buf+xmit->tail),
400 (unsigned long)(xmit->buf+xmit->tail+uart->tx_count));
401 set_dma_config(uart->tx_dma_channel,
402 set_bfin_dma_config(DIR_READ, DMA_FLOW_STOP,
407 set_dma_start_addr(uart->tx_dma_channel, (unsigned long)(xmit->buf+xmit->tail));
408 set_dma_x_count(uart->tx_dma_channel, uart->tx_count);
409 set_dma_x_modify(uart->tx_dma_channel, 1);
410 enable_dma(uart->tx_dma_channel);
412 xmit->tail = (xmit->tail + uart->tx_count) & (UART_XMIT_SIZE - 1);
413 uart->port.icount.tx += uart->tx_count;
416 UART_SET_IER(uart, ETBEI);
418 ier = UART_GET_IER(uart);
420 UART_PUT_IER(uart, ier);
422 spin_unlock_irqrestore(&uart->port.lock, flags);
425 static void bfin_serial_dma_rx_chars(struct bfin_serial_port *uart)
427 struct tty_struct *tty = uart->port.info->tty;
430 status = UART_GET_LSR(uart);
431 uart->port.icount.rx += CIRC_CNT(uart->rx_dma_buf.head, uart->rx_dma_buf.tail, UART_XMIT_SIZE);;
434 uart->port.icount.brk++;
435 if (uart_handle_break(&uart->port))
436 goto dma_ignore_char;
437 status &= ~(PE | FE);
440 uart->port.icount.parity++;
442 uart->port.icount.overrun++;
444 uart->port.icount.frame++;
446 status &= uart->port.read_status_mask;
450 else if (status & PE)
452 else if (status & FE)
457 for (i = uart->rx_dma_buf.head; i < uart->rx_dma_buf.tail; i++) {
458 if (uart_handle_sysrq_char(&uart->port, uart->rx_dma_buf.buf[i]))
459 goto dma_ignore_char;
460 uart_insert_char(&uart->port, status, OE, uart->rx_dma_buf.buf[i], flg);
464 tty_flip_buffer_push(tty);
467 void bfin_serial_rx_dma_timeout(struct bfin_serial_port *uart)
472 spin_lock_irqsave(&uart->port.lock, flags);
473 x_pos = DMA_RX_XCOUNT - get_dma_curr_xcount(uart->rx_dma_channel);
474 if (x_pos == DMA_RX_XCOUNT)
477 pos = uart->rx_dma_nrows * DMA_RX_XCOUNT + x_pos;
479 if (pos>uart->rx_dma_buf.tail) {
480 uart->rx_dma_buf.tail = pos;
481 bfin_serial_dma_rx_chars(uart);
482 uart->rx_dma_buf.head = uart->rx_dma_buf.tail;
484 spin_unlock_irqrestore(&uart->port.lock, flags);
485 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
486 add_timer(&(uart->rx_dma_timer));
489 static irqreturn_t bfin_serial_dma_tx_int(int irq, void *dev_id)
491 struct bfin_serial_port *uart = dev_id;
492 struct circ_buf *xmit = &uart->port.info->xmit;
495 spin_lock(&uart->port.lock);
496 if (!(get_dma_curr_irqstat(uart->tx_dma_channel)&DMA_RUN)) {
497 clear_dma_irqstat(uart->tx_dma_channel);
498 disable_dma(uart->tx_dma_channel);
500 UART_CLEAR_IER(uart, ETBEI);
502 ier = UART_GET_IER(uart);
504 UART_PUT_IER(uart, ier);
506 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
507 uart_write_wakeup(&uart->port);
511 bfin_serial_dma_tx_chars(uart);
514 spin_unlock(&uart->port.lock);
518 static irqreturn_t bfin_serial_dma_rx_int(int irq, void *dev_id)
520 struct bfin_serial_port *uart = dev_id;
521 unsigned short irqstat;
523 uart->rx_dma_nrows++;
524 if (uart->rx_dma_nrows == DMA_RX_YCOUNT) {
525 uart->rx_dma_nrows = 0;
526 uart->rx_dma_buf.tail = DMA_RX_XCOUNT*DMA_RX_YCOUNT;
527 bfin_serial_dma_rx_chars(uart);
528 uart->rx_dma_buf.head = uart->rx_dma_buf.tail = 0;
530 spin_lock(&uart->port.lock);
531 irqstat = get_dma_curr_irqstat(uart->rx_dma_channel);
532 clear_dma_irqstat(uart->rx_dma_channel);
534 spin_unlock(&uart->port.lock);
540 * Return TIOCSER_TEMT when transmitter is not busy.
542 static unsigned int bfin_serial_tx_empty(struct uart_port *port)
544 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
547 lsr = UART_GET_LSR(uart);
554 static unsigned int bfin_serial_get_mctrl(struct uart_port *port)
556 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
557 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
558 if (uart->cts_pin < 0)
559 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
561 if (gpio_get_value(uart->cts_pin))
562 return TIOCM_DSR | TIOCM_CAR;
565 return TIOCM_CTS | TIOCM_DSR | TIOCM_CAR;
568 static void bfin_serial_set_mctrl(struct uart_port *port, unsigned int mctrl)
570 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
571 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
572 if (uart->rts_pin < 0)
575 if (mctrl & TIOCM_RTS)
576 gpio_set_value(uart->rts_pin, 0);
578 gpio_set_value(uart->rts_pin, 1);
583 * Handle any change of modem status signal since we were last called.
585 static void bfin_serial_mctrl_check(struct bfin_serial_port *uart)
587 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
589 # ifdef CONFIG_SERIAL_BFIN_DMA
590 struct uart_info *info = uart->port.info;
591 struct tty_struct *tty = info->tty;
593 status = bfin_serial_get_mctrl(&uart->port);
594 if (!(status & TIOCM_CTS)) {
600 status = bfin_serial_get_mctrl(&uart->port);
601 uart_handle_cts_change(&uart->port, status & TIOCM_CTS);
602 if (!(status & TIOCM_CTS))
603 schedule_work(&uart->cts_workqueue);
609 * Interrupts are always disabled.
611 static void bfin_serial_break_ctl(struct uart_port *port, int break_state)
613 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
614 u16 lcr = UART_GET_LCR(uart);
619 UART_PUT_LCR(uart, lcr);
623 static int bfin_serial_startup(struct uart_port *port)
625 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
627 #ifdef CONFIG_SERIAL_BFIN_DMA
628 dma_addr_t dma_handle;
630 if (request_dma(uart->rx_dma_channel, "BFIN_UART_RX") < 0) {
631 printk(KERN_NOTICE "Unable to attach Blackfin UART RX DMA channel\n");
635 if (request_dma(uart->tx_dma_channel, "BFIN_UART_TX") < 0) {
636 printk(KERN_NOTICE "Unable to attach Blackfin UART TX DMA channel\n");
637 free_dma(uart->rx_dma_channel);
641 set_dma_callback(uart->rx_dma_channel, bfin_serial_dma_rx_int, uart);
642 set_dma_callback(uart->tx_dma_channel, bfin_serial_dma_tx_int, uart);
644 uart->rx_dma_buf.buf = (unsigned char *)dma_alloc_coherent(NULL, PAGE_SIZE, &dma_handle, GFP_DMA);
645 uart->rx_dma_buf.head = 0;
646 uart->rx_dma_buf.tail = 0;
647 uart->rx_dma_nrows = 0;
649 set_dma_config(uart->rx_dma_channel,
650 set_bfin_dma_config(DIR_WRITE, DMA_FLOW_AUTO,
651 INTR_ON_ROW, DIMENSION_2D,
654 set_dma_x_count(uart->rx_dma_channel, DMA_RX_XCOUNT);
655 set_dma_x_modify(uart->rx_dma_channel, 1);
656 set_dma_y_count(uart->rx_dma_channel, DMA_RX_YCOUNT);
657 set_dma_y_modify(uart->rx_dma_channel, 1);
658 set_dma_start_addr(uart->rx_dma_channel, (unsigned long)uart->rx_dma_buf.buf);
659 enable_dma(uart->rx_dma_channel);
661 uart->rx_dma_timer.data = (unsigned long)(uart);
662 uart->rx_dma_timer.function = (void *)bfin_serial_rx_dma_timeout;
663 uart->rx_dma_timer.expires = jiffies + DMA_RX_FLUSH_JIFFIES;
664 add_timer(&(uart->rx_dma_timer));
666 if (request_irq(uart->port.irq, bfin_serial_rx_int, IRQF_DISABLED,
667 "BFIN_UART_RX", uart)) {
668 # ifdef CONFIG_KGDB_UART
669 if (uart->port.line != CONFIG_KGDB_UART_PORT) {
671 printk(KERN_NOTICE "Unable to attach BlackFin UART RX interrupt\n");
673 # ifdef CONFIG_KGDB_UART
680 (uart->port.irq+1, bfin_serial_tx_int, IRQF_DISABLED,
681 "BFIN_UART_TX", uart)) {
682 printk(KERN_NOTICE "Unable to attach BlackFin UART TX interrupt\n");
683 free_irq(uart->port.irq, uart);
688 UART_SET_IER(uart, ERBFI);
690 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
695 static void bfin_serial_shutdown(struct uart_port *port)
697 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
699 #ifdef CONFIG_SERIAL_BFIN_DMA
700 disable_dma(uart->tx_dma_channel);
701 free_dma(uart->tx_dma_channel);
702 disable_dma(uart->rx_dma_channel);
703 free_dma(uart->rx_dma_channel);
704 del_timer(&(uart->rx_dma_timer));
705 dma_free_coherent(NULL, PAGE_SIZE, uart->rx_dma_buf.buf, 0);
707 #ifdef CONFIG_KGDB_UART
708 if (uart->port.line != CONFIG_KGDB_UART_PORT)
710 free_irq(uart->port.irq, uart);
711 free_irq(uart->port.irq+1, uart);
716 bfin_serial_set_termios(struct uart_port *port, struct ktermios *termios,
717 struct ktermios *old)
719 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
721 unsigned int baud, quot;
722 unsigned short val, ier, lsr, lcr = 0;
724 switch (termios->c_cflag & CSIZE) {
738 printk(KERN_ERR "%s: word lengh not supported\n",
742 if (termios->c_cflag & CSTOPB)
744 if (termios->c_cflag & PARENB)
746 if (!(termios->c_cflag & PARODD))
748 if (termios->c_cflag & CMSPAR)
751 port->read_status_mask = OE;
752 if (termios->c_iflag & INPCK)
753 port->read_status_mask |= (FE | PE);
754 if (termios->c_iflag & (BRKINT | PARMRK))
755 port->read_status_mask |= BI;
758 * Characters to ignore
760 port->ignore_status_mask = 0;
761 if (termios->c_iflag & IGNPAR)
762 port->ignore_status_mask |= FE | PE;
763 if (termios->c_iflag & IGNBRK) {
764 port->ignore_status_mask |= BI;
766 * If we're ignoring parity and break indicators,
767 * ignore overruns too (for real raw support).
769 if (termios->c_iflag & IGNPAR)
770 port->ignore_status_mask |= OE;
773 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/16);
774 quot = uart_get_divisor(port, baud);
775 spin_lock_irqsave(&uart->port.lock, flags);
778 lsr = UART_GET_LSR(uart);
779 } while (!(lsr & TEMT));
782 ier = UART_GET_IER(uart);
784 UART_CLEAR_IER(uart, 0xF);
786 UART_PUT_IER(uart, 0);
790 /* Set DLAB in LCR to Access DLL and DLH */
791 val = UART_GET_LCR(uart);
793 UART_PUT_LCR(uart, val);
797 UART_PUT_DLL(uart, quot & 0xFF);
799 UART_PUT_DLH(uart, (quot >> 8) & 0xFF);
803 /* Clear DLAB in LCR to Access THR RBR IER */
804 val = UART_GET_LCR(uart);
806 UART_PUT_LCR(uart, val);
810 UART_PUT_LCR(uart, lcr);
814 UART_SET_IER(uart, ier);
816 UART_PUT_IER(uart, ier);
819 val = UART_GET_GCTL(uart);
821 UART_PUT_GCTL(uart, val);
823 spin_unlock_irqrestore(&uart->port.lock, flags);
826 static const char *bfin_serial_type(struct uart_port *port)
828 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
830 return uart->port.type == PORT_BFIN ? "BFIN-UART" : NULL;
834 * Release the memory region(s) being used by 'port'.
836 static void bfin_serial_release_port(struct uart_port *port)
841 * Request the memory region(s) being used by 'port'.
843 static int bfin_serial_request_port(struct uart_port *port)
849 * Configure/autoconfigure the port.
851 static void bfin_serial_config_port(struct uart_port *port, int flags)
853 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
855 if (flags & UART_CONFIG_TYPE &&
856 bfin_serial_request_port(&uart->port) == 0)
857 uart->port.type = PORT_BFIN;
861 * Verify the new serial_struct (for TIOCSSERIAL).
862 * The only change we allow are to the flags and type, and
863 * even then only between PORT_BFIN and PORT_UNKNOWN
866 bfin_serial_verify_port(struct uart_port *port, struct serial_struct *ser)
871 static struct uart_ops bfin_serial_pops = {
872 .tx_empty = bfin_serial_tx_empty,
873 .set_mctrl = bfin_serial_set_mctrl,
874 .get_mctrl = bfin_serial_get_mctrl,
875 .stop_tx = bfin_serial_stop_tx,
876 .start_tx = bfin_serial_start_tx,
877 .stop_rx = bfin_serial_stop_rx,
878 .enable_ms = bfin_serial_enable_ms,
879 .break_ctl = bfin_serial_break_ctl,
880 .startup = bfin_serial_startup,
881 .shutdown = bfin_serial_shutdown,
882 .set_termios = bfin_serial_set_termios,
883 .type = bfin_serial_type,
884 .release_port = bfin_serial_release_port,
885 .request_port = bfin_serial_request_port,
886 .config_port = bfin_serial_config_port,
887 .verify_port = bfin_serial_verify_port,
890 static void __init bfin_serial_init_ports(void)
892 static int first = 1;
899 for (i = 0; i < nr_ports; i++) {
900 bfin_serial_ports[i].port.uartclk = get_sclk();
901 bfin_serial_ports[i].port.ops = &bfin_serial_pops;
902 bfin_serial_ports[i].port.line = i;
903 bfin_serial_ports[i].port.iotype = UPIO_MEM;
904 bfin_serial_ports[i].port.membase =
905 (void __iomem *)bfin_serial_resource[i].uart_base_addr;
906 bfin_serial_ports[i].port.mapbase =
907 bfin_serial_resource[i].uart_base_addr;
908 bfin_serial_ports[i].port.irq =
909 bfin_serial_resource[i].uart_irq;
910 bfin_serial_ports[i].port.flags = UPF_BOOT_AUTOCONF;
911 #ifdef CONFIG_SERIAL_BFIN_DMA
912 bfin_serial_ports[i].tx_done = 1;
913 bfin_serial_ports[i].tx_count = 0;
914 bfin_serial_ports[i].tx_dma_channel =
915 bfin_serial_resource[i].uart_tx_dma_channel;
916 bfin_serial_ports[i].rx_dma_channel =
917 bfin_serial_resource[i].uart_rx_dma_channel;
918 init_timer(&(bfin_serial_ports[i].rx_dma_timer));
920 INIT_WORK(&bfin_serial_ports[i].cts_workqueue, bfin_serial_do_work);
922 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
923 bfin_serial_ports[i].cts_pin =
924 bfin_serial_resource[i].uart_cts_pin;
925 bfin_serial_ports[i].rts_pin =
926 bfin_serial_resource[i].uart_rts_pin;
928 bfin_serial_hw_init(&bfin_serial_ports[i]);
933 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
935 * If the port was already initialised (eg, by a boot loader),
936 * try to determine the current setup.
939 bfin_serial_console_get_options(struct bfin_serial_port *uart, int *baud,
940 int *parity, int *bits)
942 unsigned short status;
944 status = UART_GET_IER(uart) & (ERBFI | ETBEI);
945 if (status == (ERBFI | ETBEI)) {
946 /* ok, the port was enabled */
947 unsigned short lcr, val;
948 unsigned short dlh, dll;
950 lcr = UART_GET_LCR(uart);
959 switch (lcr & 0x03) {
960 case 0: *bits = 5; break;
961 case 1: *bits = 6; break;
962 case 2: *bits = 7; break;
963 case 3: *bits = 8; break;
966 /* Set DLAB in LCR to Access DLL and DLH */
967 val = UART_GET_LCR(uart);
969 UART_PUT_LCR(uart, val);
972 dll = UART_GET_DLL(uart);
973 dlh = UART_GET_DLH(uart);
976 /* Clear DLAB in LCR to Access THR RBR IER */
977 val = UART_GET_LCR(uart);
979 UART_PUT_LCR(uart, val);
982 *baud = get_sclk() / (16*(dll | dlh << 8));
984 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__, *baud, *parity, *bits);
988 #if defined(CONFIG_SERIAL_BFIN_CONSOLE) || defined(CONFIG_EARLY_PRINTK)
989 static struct uart_driver bfin_serial_reg;
992 bfin_serial_console_setup(struct console *co, char *options)
994 struct bfin_serial_port *uart;
995 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
999 # ifdef CONFIG_SERIAL_BFIN_CTSRTS
1007 * Check whether an invalid uart number has been specified, and
1008 * if so, search for the first available port that does have
1011 if (co->index == -1 || co->index >= nr_ports)
1013 uart = &bfin_serial_ports[co->index];
1015 # ifdef CONFIG_SERIAL_BFIN_CONSOLE
1017 uart_parse_options(options, &baud, &parity, &bits, &flow);
1019 bfin_serial_console_get_options(uart, &baud, &parity, &bits);
1021 return uart_set_options(&uart->port, co, baud, parity, bits, flow);
1026 #endif /* defined (CONFIG_SERIAL_BFIN_CONSOLE) ||
1027 defined (CONFIG_EARLY_PRINTK) */
1029 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
1030 static void bfin_serial_console_putchar(struct uart_port *port, int ch)
1032 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1033 while (!(UART_GET_LSR(uart) & THRE))
1035 UART_PUT_CHAR(uart, ch);
1040 * Interrupts are disabled on entering
1043 bfin_serial_console_write(struct console *co, const char *s, unsigned int count)
1045 struct bfin_serial_port *uart = &bfin_serial_ports[co->index];
1048 spin_lock_irqsave(&uart->port.lock, flags);
1049 uart_console_write(&uart->port, s, count, bfin_serial_console_putchar);
1050 spin_unlock_irqrestore(&uart->port.lock, flags);
1054 static struct console bfin_serial_console = {
1055 .name = BFIN_SERIAL_NAME,
1056 .write = bfin_serial_console_write,
1057 .device = uart_console_device,
1058 .setup = bfin_serial_console_setup,
1059 .flags = CON_PRINTBUFFER,
1061 .data = &bfin_serial_reg,
1064 static int __init bfin_serial_rs_console_init(void)
1066 bfin_serial_init_ports();
1067 register_console(&bfin_serial_console);
1068 #ifdef CONFIG_KGDB_UART
1069 kgdb_entry_state = 0;
1074 console_initcall(bfin_serial_rs_console_init);
1076 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
1078 #define BFIN_SERIAL_CONSOLE NULL
1079 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1082 #ifdef CONFIG_EARLY_PRINTK
1083 static __init void early_serial_putc(struct uart_port *port, int ch)
1085 unsigned timeout = 0xffff;
1086 struct bfin_serial_port *uart = (struct bfin_serial_port *)port;
1088 while ((!(UART_GET_LSR(uart) & THRE)) && --timeout)
1090 UART_PUT_CHAR(uart, ch);
1093 static __init void early_serial_write(struct console *con, const char *s,
1096 struct bfin_serial_port *uart = &bfin_serial_ports[con->index];
1099 for (i = 0; i < n; i++, s++) {
1101 early_serial_putc(&uart->port, '\r');
1102 early_serial_putc(&uart->port, *s);
1106 static struct __init console bfin_early_serial_console = {
1107 .name = "early_BFuart",
1108 .write = early_serial_write,
1109 .device = uart_console_device,
1110 .flags = CON_PRINTBUFFER,
1111 .setup = bfin_serial_console_setup,
1113 .data = &bfin_serial_reg,
1116 struct console __init *bfin_earlyserial_init(unsigned int port,
1119 struct bfin_serial_port *uart;
1122 if (port == -1 || port >= nr_ports)
1124 bfin_serial_init_ports();
1125 bfin_early_serial_console.index = port;
1126 uart = &bfin_serial_ports[port];
1132 bfin_serial_set_termios(&uart->port, &t, &t);
1133 return &bfin_early_serial_console;
1136 #endif /* CONFIG_SERIAL_BFIN_CONSOLE */
1138 static struct uart_driver bfin_serial_reg = {
1139 .owner = THIS_MODULE,
1140 .driver_name = "bfin-uart",
1141 .dev_name = BFIN_SERIAL_NAME,
1142 .major = BFIN_SERIAL_MAJOR,
1143 .minor = BFIN_SERIAL_MINOR,
1145 .cons = BFIN_SERIAL_CONSOLE,
1148 static int bfin_serial_suspend(struct platform_device *dev, pm_message_t state)
1150 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1153 uart_suspend_port(&bfin_serial_reg, &uart->port);
1158 static int bfin_serial_resume(struct platform_device *dev)
1160 struct bfin_serial_port *uart = platform_get_drvdata(dev);
1163 uart_resume_port(&bfin_serial_reg, &uart->port);
1168 static int bfin_serial_probe(struct platform_device *dev)
1170 struct resource *res = dev->resource;
1173 for (i = 0; i < dev->num_resources; i++, res++)
1174 if (res->flags & IORESOURCE_MEM)
1177 if (i < dev->num_resources) {
1178 for (i = 0; i < nr_ports; i++, res++) {
1179 if (bfin_serial_ports[i].port.mapbase != res->start)
1181 bfin_serial_ports[i].port.dev = &dev->dev;
1182 uart_add_one_port(&bfin_serial_reg, &bfin_serial_ports[i].port);
1183 platform_set_drvdata(dev, &bfin_serial_ports[i]);
1190 static int bfin_serial_remove(struct platform_device *pdev)
1192 struct bfin_serial_port *uart = platform_get_drvdata(pdev);
1195 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1196 gpio_free(uart->cts_pin);
1197 gpio_free(uart->rts_pin);
1200 platform_set_drvdata(pdev, NULL);
1203 uart_remove_one_port(&bfin_serial_reg, &uart->port);
1208 static struct platform_driver bfin_serial_driver = {
1209 .probe = bfin_serial_probe,
1210 .remove = bfin_serial_remove,
1211 .suspend = bfin_serial_suspend,
1212 .resume = bfin_serial_resume,
1214 .name = "bfin-uart",
1218 static int __init bfin_serial_init(void)
1221 #ifdef CONFIG_KGDB_UART
1222 struct bfin_serial_port *uart = &bfin_serial_ports[CONFIG_KGDB_UART_PORT];
1226 pr_info("Serial: Blackfin serial driver\n");
1228 bfin_serial_init_ports();
1230 ret = uart_register_driver(&bfin_serial_reg);
1232 ret = platform_driver_register(&bfin_serial_driver);
1234 pr_debug("uart register failed\n");
1235 uart_unregister_driver(&bfin_serial_reg);
1238 #ifdef CONFIG_KGDB_UART
1239 if (uart->port.cons->index != CONFIG_KGDB_UART_PORT) {
1240 request_irq(uart->port.irq, bfin_serial_rx_int,
1241 IRQF_DISABLED, "BFIN_UART_RX", uart);
1242 pr_info("Request irq for kgdb uart port\n");
1244 UART_SET_IER(uart, ERBFI);
1246 UART_PUT_IER(uart, UART_GET_IER(uart) | ERBFI);
1249 t.c_cflag = CS8|B57600;
1253 t.c_line = CONFIG_KGDB_UART_PORT;
1254 bfin_serial_set_termios(&uart->port, &t, &t);
1260 static void __exit bfin_serial_exit(void)
1262 platform_driver_unregister(&bfin_serial_driver);
1263 uart_unregister_driver(&bfin_serial_reg);
1266 module_init(bfin_serial_init);
1267 module_exit(bfin_serial_exit);
1269 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1270 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1271 MODULE_LICENSE("GPL");
1272 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR);