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>
45 #include <asm/mach/bfin_serial_5xx.h>
47 #ifdef CONFIG_SERIAL_BFIN_DMA
48 #include <linux/dma-mapping.h>
51 #include <asm/cacheflush.h>
54 /* UART name and device definitions */
55 #define BFIN_SERIAL_NAME "ttyBF"
56 #define BFIN_SERIAL_MAJOR 204
57 #define BFIN_SERIAL_MINOR 64
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 5
67 #ifdef CONFIG_SERIAL_BFIN_DMA
68 static void bfin_serial_dma_tx_chars(struct bfin_serial_port
*uart
);
70 static void bfin_serial_do_work(struct work_struct
*work
);
71 static void bfin_serial_tx_chars(struct bfin_serial_port
*uart
);
72 static void local_put_char(struct bfin_serial_port
*uart
, char ch
);
75 static void bfin_serial_mctrl_check(struct bfin_serial_port
*uart
);
78 * interrupts are disabled on entry
80 static void bfin_serial_stop_tx(struct uart_port
*port
)
82 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
84 #ifdef CONFIG_SERIAL_BFIN_DMA
85 disable_dma(uart
->tx_dma_channel
);
89 ier
= UART_GET_IER(uart
);
91 UART_PUT_IER(uart
, ier
);
96 * port is locked and interrupts are disabled
98 static void bfin_serial_start_tx(struct uart_port
*port
)
100 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
102 #ifdef CONFIG_SERIAL_BFIN_DMA
103 bfin_serial_dma_tx_chars(uart
);
106 ier
= UART_GET_IER(uart
);
108 UART_PUT_IER(uart
, ier
);
109 bfin_serial_tx_chars(uart
);
114 * Interrupts are enabled
116 static void bfin_serial_stop_rx(struct uart_port
*port
)
118 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
121 ier
= UART_GET_IER(uart
);
123 UART_PUT_IER(uart
, ier
);
127 * Set the modem control timer to fire immediately.
129 static void bfin_serial_enable_ms(struct uart_port
*port
)
133 #ifdef CONFIG_SERIAL_BFIN_PIO
134 static void local_put_char(struct bfin_serial_port
*uart
, char ch
)
136 unsigned short status
;
139 spin_lock_irqsave(&uart
->port
.lock
, flags
);
142 status
= UART_GET_LSR(uart
);
143 } while (!(status
& THRE
));
145 UART_PUT_CHAR(uart
, ch
);
148 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
151 static void bfin_serial_rx_chars(struct bfin_serial_port
*uart
)
153 struct tty_struct
*tty
= uart
->port
.info
->tty
;
154 unsigned int status
, ch
, flg
;
156 static int in_break
= 0;
159 status
= UART_GET_LSR(uart
);
160 ch
= UART_GET_CHAR(uart
);
161 uart
->port
.icount
.rx
++;
164 /* The BF533 family of processors have a nice misbehavior where
165 * they continuously generate characters for a "single" break.
166 * We have to basically ignore this flood until the "next" valid
167 * character comes across. All other Blackfin families operate
173 ch
= UART_GET_CHAR(uart
);
174 if (bfin_revid() < 5)
185 uart
->port
.icount
.brk
++;
186 if (uart_handle_break(&uart
->port
))
190 uart
->port
.icount
.parity
++;
192 uart
->port
.icount
.overrun
++;
194 uart
->port
.icount
.frame
++;
196 status
&= uart
->port
.read_status_mask
;
200 else if (status
& PE
)
202 else if (status
& FE
)
207 if (uart_handle_sysrq_char(&uart
->port
, ch
))
210 uart_insert_char(&uart
->port
, status
, OE
, ch
, flg
);
213 tty_flip_buffer_push(tty
);
216 static void bfin_serial_tx_chars(struct bfin_serial_port
*uart
)
218 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
220 if (uart
->port
.x_char
) {
221 UART_PUT_CHAR(uart
, uart
->port
.x_char
);
222 uart
->port
.icount
.tx
++;
223 uart
->port
.x_char
= 0;
227 * Check the modem control lines before
228 * transmitting anything.
230 bfin_serial_mctrl_check(uart
);
232 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uart
->port
)) {
233 bfin_serial_stop_tx(&uart
->port
);
237 local_put_char(uart
, xmit
->buf
[xmit
->tail
]);
238 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
239 uart
->port
.icount
.tx
++;
241 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
242 uart_write_wakeup(&uart
->port
);
244 if (uart_circ_empty(xmit
))
245 bfin_serial_stop_tx(&uart
->port
);
248 static irqreturn_t
bfin_serial_rx_int(int irq
, void *dev_id
)
250 struct bfin_serial_port
*uart
= dev_id
;
252 spin_lock(&uart
->port
.lock
);
253 while ((UART_GET_IIR(uart
) & IIR_STATUS
) == IIR_RX_READY
)
254 bfin_serial_rx_chars(uart
);
255 spin_unlock(&uart
->port
.lock
);
259 static irqreturn_t
bfin_serial_tx_int(int irq
, void *dev_id
)
261 struct bfin_serial_port
*uart
= dev_id
;
263 spin_lock(&uart
->port
.lock
);
264 while ((UART_GET_IIR(uart
) & IIR_STATUS
) == IIR_TX_READY
)
265 bfin_serial_tx_chars(uart
);
266 spin_unlock(&uart
->port
.lock
);
271 static void bfin_serial_do_work(struct work_struct
*work
)
273 struct bfin_serial_port
*uart
= container_of(work
, struct bfin_serial_port
, cts_workqueue
);
275 bfin_serial_mctrl_check(uart
);
280 #ifdef CONFIG_SERIAL_BFIN_DMA
281 static void bfin_serial_dma_tx_chars(struct bfin_serial_port
*uart
)
283 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
292 if (uart
->port
.x_char
) {
293 UART_PUT_CHAR(uart
, uart
->port
.x_char
);
294 uart
->port
.icount
.tx
++;
295 uart
->port
.x_char
= 0;
300 * Check the modem control lines before
301 * transmitting anything.
303 bfin_serial_mctrl_check(uart
);
305 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uart
->port
)) {
306 bfin_serial_stop_tx(&uart
->port
);
311 spin_lock_irqsave(&uart
->port
.lock
, flags
);
312 uart
->tx_count
= CIRC_CNT(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
313 if (uart
->tx_count
> (UART_XMIT_SIZE
- xmit
->tail
))
314 uart
->tx_count
= UART_XMIT_SIZE
- xmit
->tail
;
315 blackfin_dcache_flush_range((unsigned long)(xmit
->buf
+xmit
->tail
),
316 (unsigned long)(xmit
->buf
+xmit
->tail
+uart
->tx_count
));
317 set_dma_config(uart
->tx_dma_channel
,
318 set_bfin_dma_config(DIR_READ
, DMA_FLOW_STOP
,
322 set_dma_start_addr(uart
->tx_dma_channel
, (unsigned long)(xmit
->buf
+xmit
->tail
));
323 set_dma_x_count(uart
->tx_dma_channel
, uart
->tx_count
);
324 set_dma_x_modify(uart
->tx_dma_channel
, 1);
325 enable_dma(uart
->tx_dma_channel
);
326 ier
= UART_GET_IER(uart
);
328 UART_PUT_IER(uart
, ier
);
329 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
332 static void bfin_serial_dma_rx_chars(struct bfin_serial_port
*uart
)
334 struct tty_struct
*tty
= uart
->port
.info
->tty
;
337 status
= UART_GET_LSR(uart
);
338 uart
->port
.icount
.rx
+= CIRC_CNT(uart
->rx_dma_buf
.head
, uart
->rx_dma_buf
.tail
, UART_XMIT_SIZE
);;
341 uart
->port
.icount
.brk
++;
342 if (uart_handle_break(&uart
->port
))
343 goto dma_ignore_char
;
346 uart
->port
.icount
.parity
++;
348 uart
->port
.icount
.overrun
++;
350 uart
->port
.icount
.frame
++;
352 status
&= uart
->port
.read_status_mask
;
356 else if (status
& PE
)
358 else if (status
& FE
)
363 for (i
= uart
->rx_dma_buf
.head
; i
< uart
->rx_dma_buf
.tail
; i
++) {
364 if (uart_handle_sysrq_char(&uart
->port
, uart
->rx_dma_buf
.buf
[i
]))
365 goto dma_ignore_char
;
366 uart_insert_char(&uart
->port
, status
, OE
, uart
->rx_dma_buf
.buf
[i
], flg
);
370 tty_flip_buffer_push(tty
);
373 void bfin_serial_rx_dma_timeout(struct bfin_serial_port
*uart
)
378 bfin_serial_dma_tx_chars(uart
);
380 spin_lock_irqsave(&uart
->port
.lock
, flags
);
381 x_pos
= DMA_RX_XCOUNT
- get_dma_curr_xcount(uart
->rx_dma_channel
);
382 if (x_pos
== DMA_RX_XCOUNT
)
385 pos
= uart
->rx_dma_nrows
* DMA_RX_XCOUNT
+ x_pos
;
387 if (pos
>uart
->rx_dma_buf
.tail
) {
388 uart
->rx_dma_buf
.tail
= pos
;
389 bfin_serial_dma_rx_chars(uart
);
390 uart
->rx_dma_buf
.head
= uart
->rx_dma_buf
.tail
;
392 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
393 uart
->rx_dma_timer
.expires
= jiffies
+ DMA_RX_FLUSH_JIFFIES
;
394 add_timer(&(uart
->rx_dma_timer
));
397 static irqreturn_t
bfin_serial_dma_tx_int(int irq
, void *dev_id
)
399 struct bfin_serial_port
*uart
= dev_id
;
400 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
403 spin_lock(&uart
->port
.lock
);
404 if (!(get_dma_curr_irqstat(uart
->tx_dma_channel
)&DMA_RUN
)) {
405 clear_dma_irqstat(uart
->tx_dma_channel
);
406 disable_dma(uart
->tx_dma_channel
);
407 ier
= UART_GET_IER(uart
);
409 UART_PUT_IER(uart
, ier
);
410 xmit
->tail
= (xmit
->tail
+uart
->tx_count
) &(UART_XMIT_SIZE
-1);
411 uart
->port
.icount
.tx
+=uart
->tx_count
;
413 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
414 uart_write_wakeup(&uart
->port
);
416 if (uart_circ_empty(xmit
))
417 bfin_serial_stop_tx(&uart
->port
);
421 spin_unlock(&uart
->port
.lock
);
425 static irqreturn_t
bfin_serial_dma_rx_int(int irq
, void *dev_id
)
427 struct bfin_serial_port
*uart
= dev_id
;
428 unsigned short irqstat
;
430 uart
->rx_dma_nrows
++;
431 if (uart
->rx_dma_nrows
== DMA_RX_YCOUNT
) {
432 uart
->rx_dma_nrows
= 0;
433 uart
->rx_dma_buf
.tail
= DMA_RX_XCOUNT
*DMA_RX_YCOUNT
;
434 bfin_serial_dma_rx_chars(uart
);
435 uart
->rx_dma_buf
.head
= uart
->rx_dma_buf
.tail
= 0;
437 spin_lock(&uart
->port
.lock
);
438 irqstat
= get_dma_curr_irqstat(uart
->rx_dma_channel
);
439 clear_dma_irqstat(uart
->rx_dma_channel
);
441 spin_unlock(&uart
->port
.lock
);
447 * Return TIOCSER_TEMT when transmitter is not busy.
449 static unsigned int bfin_serial_tx_empty(struct uart_port
*port
)
451 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
454 lsr
= UART_GET_LSR(uart
);
461 static unsigned int bfin_serial_get_mctrl(struct uart_port
*port
)
463 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
464 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
465 if (uart
->cts_pin
< 0)
466 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
468 if (gpio_get_value(uart
->cts_pin
))
469 return TIOCM_DSR
| TIOCM_CAR
;
472 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
475 static void bfin_serial_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
477 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
478 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
479 if (uart
->rts_pin
< 0)
482 if (mctrl
& TIOCM_RTS
)
483 gpio_set_value(uart
->rts_pin
, 0);
485 gpio_set_value(uart
->rts_pin
, 1);
490 * Handle any change of modem status signal since we were last called.
492 static void bfin_serial_mctrl_check(struct bfin_serial_port
*uart
)
494 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
496 # ifdef CONFIG_SERIAL_BFIN_DMA
497 struct uart_info
*info
= uart
->port
.info
;
498 struct tty_struct
*tty
= info
->tty
;
500 status
= bfin_serial_get_mctrl(&uart
->port
);
501 if (!(status
& TIOCM_CTS
)) {
507 status
= bfin_serial_get_mctrl(&uart
->port
);
508 uart_handle_cts_change(&uart
->port
, status
& TIOCM_CTS
);
509 if (!(status
& TIOCM_CTS
))
510 schedule_work(&uart
->cts_workqueue
);
516 * Interrupts are always disabled.
518 static void bfin_serial_break_ctl(struct uart_port
*port
, int break_state
)
522 static int bfin_serial_startup(struct uart_port
*port
)
524 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
526 #ifdef CONFIG_SERIAL_BFIN_DMA
527 dma_addr_t dma_handle
;
529 if (request_dma(uart
->rx_dma_channel
, "BFIN_UART_RX") < 0) {
530 printk(KERN_NOTICE
"Unable to attach Blackfin UART RX DMA channel\n");
534 if (request_dma(uart
->tx_dma_channel
, "BFIN_UART_TX") < 0) {
535 printk(KERN_NOTICE
"Unable to attach Blackfin UART TX DMA channel\n");
536 free_dma(uart
->rx_dma_channel
);
540 set_dma_callback(uart
->rx_dma_channel
, bfin_serial_dma_rx_int
, uart
);
541 set_dma_callback(uart
->tx_dma_channel
, bfin_serial_dma_tx_int
, uart
);
543 uart
->rx_dma_buf
.buf
= (unsigned char *)dma_alloc_coherent(NULL
, PAGE_SIZE
, &dma_handle
, GFP_DMA
);
544 uart
->rx_dma_buf
.head
= 0;
545 uart
->rx_dma_buf
.tail
= 0;
546 uart
->rx_dma_nrows
= 0;
548 set_dma_config(uart
->rx_dma_channel
,
549 set_bfin_dma_config(DIR_WRITE
, DMA_FLOW_AUTO
,
550 INTR_ON_ROW
, DIMENSION_2D
,
552 set_dma_x_count(uart
->rx_dma_channel
, DMA_RX_XCOUNT
);
553 set_dma_x_modify(uart
->rx_dma_channel
, 1);
554 set_dma_y_count(uart
->rx_dma_channel
, DMA_RX_YCOUNT
);
555 set_dma_y_modify(uart
->rx_dma_channel
, 1);
556 set_dma_start_addr(uart
->rx_dma_channel
, (unsigned long)uart
->rx_dma_buf
.buf
);
557 enable_dma(uart
->rx_dma_channel
);
559 uart
->rx_dma_timer
.data
= (unsigned long)(uart
);
560 uart
->rx_dma_timer
.function
= (void *)bfin_serial_rx_dma_timeout
;
561 uart
->rx_dma_timer
.expires
= jiffies
+ DMA_RX_FLUSH_JIFFIES
;
562 add_timer(&(uart
->rx_dma_timer
));
565 (uart
->port
.irq
, bfin_serial_rx_int
, IRQF_DISABLED
,
566 "BFIN_UART_RX", uart
)) {
567 printk(KERN_NOTICE
"Unable to attach BlackFin UART RX interrupt\n");
572 (uart
->port
.irq
+1, bfin_serial_tx_int
, IRQF_DISABLED
,
573 "BFIN_UART_TX", uart
)) {
574 printk(KERN_NOTICE
"Unable to attach BlackFin UART TX interrupt\n");
575 free_irq(uart
->port
.irq
, uart
);
579 UART_PUT_IER(uart
, UART_GET_IER(uart
) | ERBFI
);
583 static void bfin_serial_shutdown(struct uart_port
*port
)
585 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
587 #ifdef CONFIG_SERIAL_BFIN_DMA
588 disable_dma(uart
->tx_dma_channel
);
589 free_dma(uart
->tx_dma_channel
);
590 disable_dma(uart
->rx_dma_channel
);
591 free_dma(uart
->rx_dma_channel
);
592 del_timer(&(uart
->rx_dma_timer
));
594 free_irq(uart
->port
.irq
, uart
);
595 free_irq(uart
->port
.irq
+1, uart
);
600 bfin_serial_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
601 struct ktermios
*old
)
603 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
605 unsigned int baud
, quot
;
606 unsigned short val
, ier
, lsr
, lcr
= 0;
608 switch (termios
->c_cflag
& CSIZE
) {
622 printk(KERN_ERR
"%s: word lengh not supported\n",
626 if (termios
->c_cflag
& CSTOPB
)
628 if (termios
->c_cflag
& PARENB
) {
630 if (!(termios
->c_cflag
& PARODD
))
632 if (termios
->c_cflag
& CMSPAR
)
636 port
->read_status_mask
= OE
;
637 if (termios
->c_iflag
& INPCK
)
638 port
->read_status_mask
|= (FE
| PE
);
639 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
640 port
->read_status_mask
|= BI
;
643 * Characters to ignore
645 port
->ignore_status_mask
= 0;
646 if (termios
->c_iflag
& IGNPAR
)
647 port
->ignore_status_mask
|= FE
| PE
;
648 if (termios
->c_iflag
& IGNBRK
) {
649 port
->ignore_status_mask
|= BI
;
651 * If we're ignoring parity and break indicators,
652 * ignore overruns too (for real raw support).
654 if (termios
->c_iflag
& IGNPAR
)
655 port
->ignore_status_mask
|= OE
;
658 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
659 quot
= uart_get_divisor(port
, baud
);
660 spin_lock_irqsave(&uart
->port
.lock
, flags
);
663 lsr
= UART_GET_LSR(uart
);
664 } while (!(lsr
& TEMT
));
667 ier
= UART_GET_IER(uart
);
668 UART_PUT_IER(uart
, 0);
670 /* Set DLAB in LCR to Access DLL and DLH */
671 val
= UART_GET_LCR(uart
);
673 UART_PUT_LCR(uart
, val
);
676 UART_PUT_DLL(uart
, quot
& 0xFF);
678 UART_PUT_DLH(uart
, (quot
>> 8) & 0xFF);
681 /* Clear DLAB in LCR to Access THR RBR IER */
682 val
= UART_GET_LCR(uart
);
684 UART_PUT_LCR(uart
, val
);
687 UART_PUT_LCR(uart
, lcr
);
690 UART_PUT_IER(uart
, ier
);
692 val
= UART_GET_GCTL(uart
);
694 UART_PUT_GCTL(uart
, val
);
696 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
699 static const char *bfin_serial_type(struct uart_port
*port
)
701 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
703 return uart
->port
.type
== PORT_BFIN
? "BFIN-UART" : NULL
;
707 * Release the memory region(s) being used by 'port'.
709 static void bfin_serial_release_port(struct uart_port
*port
)
714 * Request the memory region(s) being used by 'port'.
716 static int bfin_serial_request_port(struct uart_port
*port
)
722 * Configure/autoconfigure the port.
724 static void bfin_serial_config_port(struct uart_port
*port
, int flags
)
726 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
728 if (flags
& UART_CONFIG_TYPE
&&
729 bfin_serial_request_port(&uart
->port
) == 0)
730 uart
->port
.type
= PORT_BFIN
;
734 * Verify the new serial_struct (for TIOCSSERIAL).
735 * The only change we allow are to the flags and type, and
736 * even then only between PORT_BFIN and PORT_UNKNOWN
739 bfin_serial_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
744 static struct uart_ops bfin_serial_pops
= {
745 .tx_empty
= bfin_serial_tx_empty
,
746 .set_mctrl
= bfin_serial_set_mctrl
,
747 .get_mctrl
= bfin_serial_get_mctrl
,
748 .stop_tx
= bfin_serial_stop_tx
,
749 .start_tx
= bfin_serial_start_tx
,
750 .stop_rx
= bfin_serial_stop_rx
,
751 .enable_ms
= bfin_serial_enable_ms
,
752 .break_ctl
= bfin_serial_break_ctl
,
753 .startup
= bfin_serial_startup
,
754 .shutdown
= bfin_serial_shutdown
,
755 .set_termios
= bfin_serial_set_termios
,
756 .type
= bfin_serial_type
,
757 .release_port
= bfin_serial_release_port
,
758 .request_port
= bfin_serial_request_port
,
759 .config_port
= bfin_serial_config_port
,
760 .verify_port
= bfin_serial_verify_port
,
763 static void __init
bfin_serial_init_ports(void)
765 static int first
= 1;
772 for (i
= 0; i
< nr_ports
; i
++) {
773 bfin_serial_ports
[i
].port
.uartclk
= get_sclk();
774 bfin_serial_ports
[i
].port
.ops
= &bfin_serial_pops
;
775 bfin_serial_ports
[i
].port
.line
= i
;
776 bfin_serial_ports
[i
].port
.iotype
= UPIO_MEM
;
777 bfin_serial_ports
[i
].port
.membase
=
778 (void __iomem
*)bfin_serial_resource
[i
].uart_base_addr
;
779 bfin_serial_ports
[i
].port
.mapbase
=
780 bfin_serial_resource
[i
].uart_base_addr
;
781 bfin_serial_ports
[i
].port
.irq
=
782 bfin_serial_resource
[i
].uart_irq
;
783 bfin_serial_ports
[i
].port
.flags
= UPF_BOOT_AUTOCONF
;
784 #ifdef CONFIG_SERIAL_BFIN_DMA
785 bfin_serial_ports
[i
].tx_done
= 1;
786 bfin_serial_ports
[i
].tx_count
= 0;
787 bfin_serial_ports
[i
].tx_dma_channel
=
788 bfin_serial_resource
[i
].uart_tx_dma_channel
;
789 bfin_serial_ports
[i
].rx_dma_channel
=
790 bfin_serial_resource
[i
].uart_rx_dma_channel
;
791 init_timer(&(bfin_serial_ports
[i
].rx_dma_timer
));
793 INIT_WORK(&bfin_serial_ports
[i
].cts_workqueue
, bfin_serial_do_work
);
795 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
796 bfin_serial_ports
[i
].cts_pin
=
797 bfin_serial_resource
[i
].uart_cts_pin
;
798 bfin_serial_ports
[i
].rts_pin
=
799 bfin_serial_resource
[i
].uart_rts_pin
;
801 bfin_serial_hw_init(&bfin_serial_ports
[i
]);
806 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
807 static void bfin_serial_console_putchar(struct uart_port
*port
, int ch
)
809 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
810 while (!(UART_GET_LSR(uart
)))
812 UART_PUT_CHAR(uart
, ch
);
817 * Interrupts are disabled on entering
820 bfin_serial_console_write(struct console
*co
, const char *s
, unsigned int count
)
822 struct bfin_serial_port
*uart
= &bfin_serial_ports
[co
->index
];
825 spin_lock_irqsave(&uart
->port
.lock
, flags
);
826 uart_console_write(&uart
->port
, s
, count
, bfin_serial_console_putchar
);
827 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
832 * If the port was already initialised (eg, by a boot loader),
833 * try to determine the current setup.
836 bfin_serial_console_get_options(struct bfin_serial_port
*uart
, int *baud
,
837 int *parity
, int *bits
)
839 unsigned short status
;
841 status
= UART_GET_IER(uart
) & (ERBFI
| ETBEI
);
842 if (status
== (ERBFI
| ETBEI
)) {
843 /* ok, the port was enabled */
844 unsigned short lcr
, val
;
845 unsigned short dlh
, dll
;
847 lcr
= UART_GET_LCR(uart
);
856 switch (lcr
& 0x03) {
857 case 0: *bits
= 5; break;
858 case 1: *bits
= 6; break;
859 case 2: *bits
= 7; break;
860 case 3: *bits
= 8; break;
862 /* Set DLAB in LCR to Access DLL and DLH */
863 val
= UART_GET_LCR(uart
);
865 UART_PUT_LCR(uart
, val
);
867 dll
= UART_GET_DLL(uart
);
868 dlh
= UART_GET_DLH(uart
);
870 /* Clear DLAB in LCR to Access THR RBR IER */
871 val
= UART_GET_LCR(uart
);
873 UART_PUT_LCR(uart
, val
);
875 *baud
= get_sclk() / (16*(dll
| dlh
<< 8));
877 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__
, *baud
, *parity
, *bits
);
881 bfin_serial_console_setup(struct console
*co
, char *options
)
883 struct bfin_serial_port
*uart
;
887 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
894 * Check whether an invalid uart number has been specified, and
895 * if so, search for the first available port that does have
898 if (co
->index
== -1 || co
->index
>= nr_ports
)
900 uart
= &bfin_serial_ports
[co
->index
];
903 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
905 bfin_serial_console_get_options(uart
, &baud
, &parity
, &bits
);
907 return uart_set_options(&uart
->port
, co
, baud
, parity
, bits
, flow
);
910 static struct uart_driver bfin_serial_reg
;
911 static struct console bfin_serial_console
= {
912 .name
= BFIN_SERIAL_NAME
,
913 .write
= bfin_serial_console_write
,
914 .device
= uart_console_device
,
915 .setup
= bfin_serial_console_setup
,
916 .flags
= CON_PRINTBUFFER
,
918 .data
= &bfin_serial_reg
,
921 static int __init
bfin_serial_rs_console_init(void)
923 bfin_serial_init_ports();
924 register_console(&bfin_serial_console
);
927 console_initcall(bfin_serial_rs_console_init
);
929 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
931 #define BFIN_SERIAL_CONSOLE NULL
934 static struct uart_driver bfin_serial_reg
= {
935 .owner
= THIS_MODULE
,
936 .driver_name
= "bfin-uart",
937 .dev_name
= BFIN_SERIAL_NAME
,
938 .major
= BFIN_SERIAL_MAJOR
,
939 .minor
= BFIN_SERIAL_MINOR
,
941 .cons
= BFIN_SERIAL_CONSOLE
,
944 static int bfin_serial_suspend(struct platform_device
*dev
, pm_message_t state
)
946 struct bfin_serial_port
*uart
= platform_get_drvdata(dev
);
949 uart_suspend_port(&bfin_serial_reg
, &uart
->port
);
954 static int bfin_serial_resume(struct platform_device
*dev
)
956 struct bfin_serial_port
*uart
= platform_get_drvdata(dev
);
959 uart_resume_port(&bfin_serial_reg
, &uart
->port
);
964 static int bfin_serial_probe(struct platform_device
*dev
)
966 struct resource
*res
= dev
->resource
;
969 for (i
= 0; i
< dev
->num_resources
; i
++, res
++)
970 if (res
->flags
& IORESOURCE_MEM
)
973 if (i
< dev
->num_resources
) {
974 for (i
= 0; i
< nr_ports
; i
++, res
++) {
975 if (bfin_serial_ports
[i
].port
.mapbase
!= res
->start
)
977 bfin_serial_ports
[i
].port
.dev
= &dev
->dev
;
978 uart_add_one_port(&bfin_serial_reg
, &bfin_serial_ports
[i
].port
);
979 platform_set_drvdata(dev
, &bfin_serial_ports
[i
]);
986 static int bfin_serial_remove(struct platform_device
*pdev
)
988 struct bfin_serial_port
*uart
= platform_get_drvdata(pdev
);
991 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
992 gpio_free(uart
->cts_pin
);
993 gpio_free(uart
->rts_pin
);
996 platform_set_drvdata(pdev
, NULL
);
999 uart_remove_one_port(&bfin_serial_reg
, &uart
->port
);
1004 static struct platform_driver bfin_serial_driver
= {
1005 .probe
= bfin_serial_probe
,
1006 .remove
= bfin_serial_remove
,
1007 .suspend
= bfin_serial_suspend
,
1008 .resume
= bfin_serial_resume
,
1010 .name
= "bfin-uart",
1014 static int __init
bfin_serial_init(void)
1018 pr_info("Serial: Blackfin serial driver\n");
1020 bfin_serial_init_ports();
1022 ret
= uart_register_driver(&bfin_serial_reg
);
1024 ret
= platform_driver_register(&bfin_serial_driver
);
1026 pr_debug("uart register failed\n");
1027 uart_unregister_driver(&bfin_serial_reg
);
1033 static void __exit
bfin_serial_exit(void)
1035 platform_driver_unregister(&bfin_serial_driver
);
1036 uart_unregister_driver(&bfin_serial_reg
);
1039 module_init(bfin_serial_init
);
1040 module_exit(bfin_serial_exit
);
1042 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1043 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1044 MODULE_LICENSE("GPL");
1045 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR
);