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_int(int irq
, void *dev_id
)
245 struct bfin_serial_port
*uart
= dev_id
;
246 unsigned short status
;
248 spin_lock(&uart
->port
.lock
);
249 status
= UART_GET_IIR(uart
);
251 if ((status
& IIR_STATUS
) == IIR_TX_READY
)
252 bfin_serial_tx_chars(uart
);
253 if ((status
& IIR_STATUS
) == IIR_RX_READY
)
254 bfin_serial_rx_chars(uart
);
255 status
= UART_GET_IIR(uart
);
256 } while (status
& (IIR_TX_READY
| IIR_RX_READY
));
257 spin_unlock(&uart
->port
.lock
);
261 static void bfin_serial_do_work(struct work_struct
*work
)
263 struct bfin_serial_port
*uart
= container_of(work
, struct bfin_serial_port
, cts_workqueue
);
265 bfin_serial_mctrl_check(uart
);
270 #ifdef CONFIG_SERIAL_BFIN_DMA
271 static void bfin_serial_dma_tx_chars(struct bfin_serial_port
*uart
)
273 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
282 if (uart
->port
.x_char
) {
283 UART_PUT_CHAR(uart
, uart
->port
.x_char
);
284 uart
->port
.icount
.tx
++;
285 uart
->port
.x_char
= 0;
290 * Check the modem control lines before
291 * transmitting anything.
293 bfin_serial_mctrl_check(uart
);
295 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uart
->port
)) {
296 bfin_serial_stop_tx(&uart
->port
);
301 spin_lock_irqsave(&uart
->port
.lock
, flags
);
302 uart
->tx_count
= CIRC_CNT(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
303 if (uart
->tx_count
> (UART_XMIT_SIZE
- xmit
->tail
))
304 uart
->tx_count
= UART_XMIT_SIZE
- xmit
->tail
;
305 blackfin_dcache_flush_range((unsigned long)(xmit
->buf
+xmit
->tail
),
306 (unsigned long)(xmit
->buf
+xmit
->tail
+uart
->tx_count
));
307 set_dma_config(uart
->tx_dma_channel
,
308 set_bfin_dma_config(DIR_READ
, DMA_FLOW_STOP
,
312 set_dma_start_addr(uart
->tx_dma_channel
, (unsigned long)(xmit
->buf
+xmit
->tail
));
313 set_dma_x_count(uart
->tx_dma_channel
, uart
->tx_count
);
314 set_dma_x_modify(uart
->tx_dma_channel
, 1);
315 enable_dma(uart
->tx_dma_channel
);
316 ier
= UART_GET_IER(uart
);
318 UART_PUT_IER(uart
, ier
);
319 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
322 static void bfin_serial_dma_rx_chars(struct bfin_serial_port
* uart
)
324 struct tty_struct
*tty
= uart
->port
.info
->tty
;
327 status
= UART_GET_LSR(uart
);
328 uart
->port
.icount
.rx
+= CIRC_CNT(uart
->rx_dma_buf
.head
, uart
->rx_dma_buf
.tail
, UART_XMIT_SIZE
);;
331 uart
->port
.icount
.brk
++;
332 if (uart_handle_break(&uart
->port
))
333 goto dma_ignore_char
;
335 } else if (status
& PE
) {
337 uart
->port
.icount
.parity
++;
338 } else if (status
& OE
) {
340 uart
->port
.icount
.overrun
++;
341 } else if (status
& FE
) {
343 uart
->port
.icount
.frame
++;
347 for (i
= uart
->rx_dma_buf
.head
; i
< uart
->rx_dma_buf
.tail
; i
++) {
348 if (uart_handle_sysrq_char(&uart
->port
, uart
->rx_dma_buf
.buf
[i
]))
349 goto dma_ignore_char
;
350 uart_insert_char(&uart
->port
, status
, 2, uart
->rx_dma_buf
.buf
[i
], flg
);
353 tty_flip_buffer_push(tty
);
356 void bfin_serial_rx_dma_timeout(struct bfin_serial_port
*uart
)
361 bfin_serial_dma_tx_chars(uart
);
363 spin_lock_irqsave(&uart
->port
.lock
, flags
);
364 x_pos
= DMA_RX_XCOUNT
- get_dma_curr_xcount(uart
->rx_dma_channel
);
365 if (x_pos
== DMA_RX_XCOUNT
)
368 pos
= uart
->rx_dma_nrows
* DMA_RX_XCOUNT
+ x_pos
;
370 if (pos
>uart
->rx_dma_buf
.tail
) {
371 uart
->rx_dma_buf
.tail
= pos
;
372 bfin_serial_dma_rx_chars(uart
);
373 uart
->rx_dma_buf
.head
= uart
->rx_dma_buf
.tail
;
375 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
376 uart
->rx_dma_timer
.expires
= jiffies
+ DMA_RX_FLUSH_JIFFIES
;
377 add_timer(&(uart
->rx_dma_timer
));
380 static irqreturn_t
bfin_serial_dma_tx_int(int irq
, void *dev_id
)
382 struct bfin_serial_port
*uart
= dev_id
;
383 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
386 spin_lock(&uart
->port
.lock
);
387 if (!(get_dma_curr_irqstat(uart
->tx_dma_channel
)&DMA_RUN
)) {
388 clear_dma_irqstat(uart
->tx_dma_channel
);
389 disable_dma(uart
->tx_dma_channel
);
390 ier
= UART_GET_IER(uart
);
392 UART_PUT_IER(uart
, ier
);
393 xmit
->tail
= (xmit
->tail
+uart
->tx_count
) &(UART_XMIT_SIZE
-1);
394 uart
->port
.icount
.tx
+=uart
->tx_count
;
396 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
397 uart_write_wakeup(&uart
->port
);
399 if (uart_circ_empty(xmit
))
400 bfin_serial_stop_tx(&uart
->port
);
404 spin_unlock(&uart
->port
.lock
);
408 static irqreturn_t
bfin_serial_dma_rx_int(int irq
, void *dev_id
)
410 struct bfin_serial_port
*uart
= dev_id
;
411 unsigned short irqstat
;
413 uart
->rx_dma_nrows
++;
414 if (uart
->rx_dma_nrows
== DMA_RX_YCOUNT
) {
415 uart
->rx_dma_nrows
= 0;
416 uart
->rx_dma_buf
.tail
= DMA_RX_XCOUNT
*DMA_RX_YCOUNT
;
417 bfin_serial_dma_rx_chars(uart
);
418 uart
->rx_dma_buf
.head
= uart
->rx_dma_buf
.tail
= 0;
420 spin_lock(&uart
->port
.lock
);
421 irqstat
= get_dma_curr_irqstat(uart
->rx_dma_channel
);
422 clear_dma_irqstat(uart
->rx_dma_channel
);
424 spin_unlock(&uart
->port
.lock
);
430 * Return TIOCSER_TEMT when transmitter is not busy.
432 static unsigned int bfin_serial_tx_empty(struct uart_port
*port
)
434 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
437 lsr
= UART_GET_LSR(uart
);
444 static unsigned int bfin_serial_get_mctrl(struct uart_port
*port
)
446 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
447 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
448 if (uart
->cts_pin
< 0)
449 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
451 if (gpio_get_value(uart
->cts_pin
))
452 return TIOCM_DSR
| TIOCM_CAR
;
455 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
458 static void bfin_serial_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
460 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
461 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
462 if (uart
->rts_pin
< 0)
465 if (mctrl
& TIOCM_RTS
)
466 gpio_set_value(uart
->rts_pin
, 0);
468 gpio_set_value(uart
->rts_pin
, 1);
473 * Handle any change of modem status signal since we were last called.
475 static void bfin_serial_mctrl_check(struct bfin_serial_port
*uart
)
477 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
479 # ifdef CONFIG_SERIAL_BFIN_DMA
480 struct uart_info
*info
= uart
->port
.info
;
481 struct tty_struct
*tty
= info
->tty
;
483 status
= bfin_serial_get_mctrl(&uart
->port
);
484 if (!(status
& TIOCM_CTS
)) {
490 status
= bfin_serial_get_mctrl(&uart
->port
);
491 uart_handle_cts_change(&uart
->port
, status
& TIOCM_CTS
);
492 if (!(status
& TIOCM_CTS
))
493 schedule_work(&uart
->cts_workqueue
);
499 * Interrupts are always disabled.
501 static void bfin_serial_break_ctl(struct uart_port
*port
, int break_state
)
505 static int bfin_serial_startup(struct uart_port
*port
)
507 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
509 #ifdef CONFIG_SERIAL_BFIN_DMA
510 dma_addr_t dma_handle
;
512 if (request_dma(uart
->rx_dma_channel
, "BFIN_UART_RX") < 0) {
513 printk(KERN_NOTICE
"Unable to attach Blackfin UART RX DMA channel\n");
517 if (request_dma(uart
->tx_dma_channel
, "BFIN_UART_TX") < 0) {
518 printk(KERN_NOTICE
"Unable to attach Blackfin UART TX DMA channel\n");
519 free_dma(uart
->rx_dma_channel
);
523 set_dma_callback(uart
->rx_dma_channel
, bfin_serial_dma_rx_int
, uart
);
524 set_dma_callback(uart
->tx_dma_channel
, bfin_serial_dma_tx_int
, uart
);
526 uart
->rx_dma_buf
.buf
= (unsigned char *)dma_alloc_coherent(NULL
, PAGE_SIZE
, &dma_handle
, GFP_DMA
);
527 uart
->rx_dma_buf
.head
= 0;
528 uart
->rx_dma_buf
.tail
= 0;
529 uart
->rx_dma_nrows
= 0;
531 set_dma_config(uart
->rx_dma_channel
,
532 set_bfin_dma_config(DIR_WRITE
, DMA_FLOW_AUTO
,
533 INTR_ON_ROW
, DIMENSION_2D
,
535 set_dma_x_count(uart
->rx_dma_channel
, DMA_RX_XCOUNT
);
536 set_dma_x_modify(uart
->rx_dma_channel
, 1);
537 set_dma_y_count(uart
->rx_dma_channel
, DMA_RX_YCOUNT
);
538 set_dma_y_modify(uart
->rx_dma_channel
, 1);
539 set_dma_start_addr(uart
->rx_dma_channel
, (unsigned long)uart
->rx_dma_buf
.buf
);
540 enable_dma(uart
->rx_dma_channel
);
542 uart
->rx_dma_timer
.data
= (unsigned long)(uart
);
543 uart
->rx_dma_timer
.function
= (void *)bfin_serial_rx_dma_timeout
;
544 uart
->rx_dma_timer
.expires
= jiffies
+ DMA_RX_FLUSH_JIFFIES
;
545 add_timer(&(uart
->rx_dma_timer
));
548 (uart
->port
.irq
, bfin_serial_int
, IRQF_DISABLED
,
549 "BFIN_UART_RX", uart
)) {
550 printk(KERN_NOTICE
"Unable to attach BlackFin UART RX interrupt\n");
555 (uart
->port
.irq
+1, bfin_serial_int
, IRQF_DISABLED
,
556 "BFIN_UART_TX", uart
)) {
557 printk(KERN_NOTICE
"Unable to attach BlackFin UART TX interrupt\n");
558 free_irq(uart
->port
.irq
, uart
);
562 UART_PUT_IER(uart
, UART_GET_IER(uart
) | ERBFI
);
566 static void bfin_serial_shutdown(struct uart_port
*port
)
568 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
570 #ifdef CONFIG_SERIAL_BFIN_DMA
571 disable_dma(uart
->tx_dma_channel
);
572 free_dma(uart
->tx_dma_channel
);
573 disable_dma(uart
->rx_dma_channel
);
574 free_dma(uart
->rx_dma_channel
);
575 del_timer(&(uart
->rx_dma_timer
));
577 free_irq(uart
->port
.irq
, uart
);
578 free_irq(uart
->port
.irq
+1, uart
);
583 bfin_serial_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
584 struct ktermios
*old
)
586 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
588 unsigned int baud
, quot
;
589 unsigned short val
, ier
, lsr
, lcr
= 0;
591 switch (termios
->c_cflag
& CSIZE
) {
605 printk(KERN_ERR
"%s: word lengh not supported\n",
609 if (termios
->c_cflag
& CSTOPB
)
611 if (termios
->c_cflag
& PARENB
) {
613 if (!(termios
->c_cflag
& PARODD
))
617 /* These controls are not implemented for this port */
618 termios
->c_iflag
|= INPCK
| BRKINT
| PARMRK
;
619 termios
->c_iflag
&= ~(IGNPAR
| IGNBRK
);
621 /* These controls are not implemented for this port */
622 termios
->c_iflag
|= INPCK
| BRKINT
| PARMRK
;
623 termios
->c_iflag
&= ~(IGNPAR
| IGNBRK
);
625 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
626 quot
= uart_get_divisor(port
, baud
);
627 spin_lock_irqsave(&uart
->port
.lock
, flags
);
630 lsr
= UART_GET_LSR(uart
);
631 } while (!(lsr
& TEMT
));
634 ier
= UART_GET_IER(uart
);
635 UART_PUT_IER(uart
, 0);
637 /* Set DLAB in LCR to Access DLL and DLH */
638 val
= UART_GET_LCR(uart
);
640 UART_PUT_LCR(uart
, val
);
643 UART_PUT_DLL(uart
, quot
& 0xFF);
645 UART_PUT_DLH(uart
, (quot
>> 8) & 0xFF);
648 /* Clear DLAB in LCR to Access THR RBR IER */
649 val
= UART_GET_LCR(uart
);
651 UART_PUT_LCR(uart
, val
);
654 UART_PUT_LCR(uart
, lcr
);
657 UART_PUT_IER(uart
, ier
);
659 val
= UART_GET_GCTL(uart
);
661 UART_PUT_GCTL(uart
, val
);
663 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
666 static const char *bfin_serial_type(struct uart_port
*port
)
668 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
670 return uart
->port
.type
== PORT_BFIN
? "BFIN-UART" : NULL
;
674 * Release the memory region(s) being used by 'port'.
676 static void bfin_serial_release_port(struct uart_port
*port
)
681 * Request the memory region(s) being used by 'port'.
683 static int bfin_serial_request_port(struct uart_port
*port
)
689 * Configure/autoconfigure the port.
691 static void bfin_serial_config_port(struct uart_port
*port
, int flags
)
693 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
695 if (flags
& UART_CONFIG_TYPE
&&
696 bfin_serial_request_port(&uart
->port
) == 0)
697 uart
->port
.type
= PORT_BFIN
;
701 * Verify the new serial_struct (for TIOCSSERIAL).
702 * The only change we allow are to the flags and type, and
703 * even then only between PORT_BFIN and PORT_UNKNOWN
706 bfin_serial_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
711 static struct uart_ops bfin_serial_pops
= {
712 .tx_empty
= bfin_serial_tx_empty
,
713 .set_mctrl
= bfin_serial_set_mctrl
,
714 .get_mctrl
= bfin_serial_get_mctrl
,
715 .stop_tx
= bfin_serial_stop_tx
,
716 .start_tx
= bfin_serial_start_tx
,
717 .stop_rx
= bfin_serial_stop_rx
,
718 .enable_ms
= bfin_serial_enable_ms
,
719 .break_ctl
= bfin_serial_break_ctl
,
720 .startup
= bfin_serial_startup
,
721 .shutdown
= bfin_serial_shutdown
,
722 .set_termios
= bfin_serial_set_termios
,
723 .type
= bfin_serial_type
,
724 .release_port
= bfin_serial_release_port
,
725 .request_port
= bfin_serial_request_port
,
726 .config_port
= bfin_serial_config_port
,
727 .verify_port
= bfin_serial_verify_port
,
730 static void __init
bfin_serial_init_ports(void)
732 static int first
= 1;
739 for (i
= 0; i
< nr_ports
; i
++) {
740 bfin_serial_ports
[i
].port
.uartclk
= get_sclk();
741 bfin_serial_ports
[i
].port
.ops
= &bfin_serial_pops
;
742 bfin_serial_ports
[i
].port
.line
= i
;
743 bfin_serial_ports
[i
].port
.iotype
= UPIO_MEM
;
744 bfin_serial_ports
[i
].port
.membase
=
745 (void __iomem
*)bfin_serial_resource
[i
].uart_base_addr
;
746 bfin_serial_ports
[i
].port
.mapbase
=
747 bfin_serial_resource
[i
].uart_base_addr
;
748 bfin_serial_ports
[i
].port
.irq
=
749 bfin_serial_resource
[i
].uart_irq
;
750 bfin_serial_ports
[i
].port
.flags
= UPF_BOOT_AUTOCONF
;
751 #ifdef CONFIG_SERIAL_BFIN_DMA
752 bfin_serial_ports
[i
].tx_done
= 1;
753 bfin_serial_ports
[i
].tx_count
= 0;
754 bfin_serial_ports
[i
].tx_dma_channel
=
755 bfin_serial_resource
[i
].uart_tx_dma_channel
;
756 bfin_serial_ports
[i
].rx_dma_channel
=
757 bfin_serial_resource
[i
].uart_rx_dma_channel
;
758 init_timer(&(bfin_serial_ports
[i
].rx_dma_timer
));
760 INIT_WORK(&bfin_serial_ports
[i
].cts_workqueue
, bfin_serial_do_work
);
762 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
763 bfin_serial_ports
[i
].cts_pin
=
764 bfin_serial_resource
[i
].uart_cts_pin
;
765 bfin_serial_ports
[i
].rts_pin
=
766 bfin_serial_resource
[i
].uart_rts_pin
;
768 bfin_serial_hw_init(&bfin_serial_ports
[i
]);
773 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
774 static void bfin_serial_console_putchar(struct uart_port
*port
, int ch
)
776 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
777 while (!(UART_GET_LSR(uart
)))
779 UART_PUT_CHAR(uart
, ch
);
784 * Interrupts are disabled on entering
787 bfin_serial_console_write(struct console
*co
, const char *s
, unsigned int count
)
789 struct bfin_serial_port
*uart
= &bfin_serial_ports
[co
->index
];
792 spin_lock_irqsave(&uart
->port
.lock
, flags
);
793 uart_console_write(&uart
->port
, s
, count
, bfin_serial_console_putchar
);
794 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
799 * If the port was already initialised (eg, by a boot loader),
800 * try to determine the current setup.
803 bfin_serial_console_get_options(struct bfin_serial_port
*uart
, int *baud
,
804 int *parity
, int *bits
)
806 unsigned short status
;
808 status
= UART_GET_IER(uart
) & (ERBFI
| ETBEI
);
809 if (status
== (ERBFI
| ETBEI
)) {
810 /* ok, the port was enabled */
811 unsigned short lcr
, val
;
812 unsigned short dlh
, dll
;
814 lcr
= UART_GET_LCR(uart
);
823 switch (lcr
& 0x03) {
824 case 0: *bits
= 5; break;
825 case 1: *bits
= 6; break;
826 case 2: *bits
= 7; break;
827 case 3: *bits
= 8; break;
829 /* Set DLAB in LCR to Access DLL and DLH */
830 val
= UART_GET_LCR(uart
);
832 UART_PUT_LCR(uart
, val
);
834 dll
= UART_GET_DLL(uart
);
835 dlh
= UART_GET_DLH(uart
);
837 /* Clear DLAB in LCR to Access THR RBR IER */
838 val
= UART_GET_LCR(uart
);
840 UART_PUT_LCR(uart
, val
);
842 *baud
= get_sclk() / (16*(dll
| dlh
<< 8));
844 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__
, *baud
, *parity
, *bits
);
848 bfin_serial_console_setup(struct console
*co
, char *options
)
850 struct bfin_serial_port
*uart
;
854 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
861 * Check whether an invalid uart number has been specified, and
862 * if so, search for the first available port that does have
865 if (co
->index
== -1 || co
->index
>= nr_ports
)
867 uart
= &bfin_serial_ports
[co
->index
];
870 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
872 bfin_serial_console_get_options(uart
, &baud
, &parity
, &bits
);
874 return uart_set_options(&uart
->port
, co
, baud
, parity
, bits
, flow
);
877 static struct uart_driver bfin_serial_reg
;
878 static struct console bfin_serial_console
= {
879 .name
= BFIN_SERIAL_NAME
,
880 .write
= bfin_serial_console_write
,
881 .device
= uart_console_device
,
882 .setup
= bfin_serial_console_setup
,
883 .flags
= CON_PRINTBUFFER
,
885 .data
= &bfin_serial_reg
,
888 static int __init
bfin_serial_rs_console_init(void)
890 bfin_serial_init_ports();
891 register_console(&bfin_serial_console
);
894 console_initcall(bfin_serial_rs_console_init
);
896 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
898 #define BFIN_SERIAL_CONSOLE NULL
901 static struct uart_driver bfin_serial_reg
= {
902 .owner
= THIS_MODULE
,
903 .driver_name
= "bfin-uart",
904 .dev_name
= BFIN_SERIAL_NAME
,
905 .major
= BFIN_SERIAL_MAJOR
,
906 .minor
= BFIN_SERIAL_MINOR
,
908 .cons
= BFIN_SERIAL_CONSOLE
,
911 static int bfin_serial_suspend(struct platform_device
*dev
, pm_message_t state
)
913 struct bfin_serial_port
*uart
= platform_get_drvdata(dev
);
916 uart_suspend_port(&bfin_serial_reg
, &uart
->port
);
921 static int bfin_serial_resume(struct platform_device
*dev
)
923 struct bfin_serial_port
*uart
= platform_get_drvdata(dev
);
926 uart_resume_port(&bfin_serial_reg
, &uart
->port
);
931 static int bfin_serial_probe(struct platform_device
*dev
)
933 struct resource
*res
= dev
->resource
;
936 for (i
= 0; i
< dev
->num_resources
; i
++, res
++)
937 if (res
->flags
& IORESOURCE_MEM
)
940 if (i
< dev
->num_resources
) {
941 for (i
= 0; i
< nr_ports
; i
++, res
++) {
942 if (bfin_serial_ports
[i
].port
.mapbase
!= res
->start
)
944 bfin_serial_ports
[i
].port
.dev
= &dev
->dev
;
945 uart_add_one_port(&bfin_serial_reg
, &bfin_serial_ports
[i
].port
);
946 platform_set_drvdata(dev
, &bfin_serial_ports
[i
]);
953 static int bfin_serial_remove(struct platform_device
*pdev
)
955 struct bfin_serial_port
*uart
= platform_get_drvdata(pdev
);
958 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
959 gpio_free(uart
->cts_pin
);
960 gpio_free(uart
->rts_pin
);
963 platform_set_drvdata(pdev
, NULL
);
966 uart_remove_one_port(&bfin_serial_reg
, &uart
->port
);
971 static struct platform_driver bfin_serial_driver
= {
972 .probe
= bfin_serial_probe
,
973 .remove
= bfin_serial_remove
,
974 .suspend
= bfin_serial_suspend
,
975 .resume
= bfin_serial_resume
,
981 static int __init
bfin_serial_init(void)
985 pr_info("Serial: Blackfin serial driver\n");
987 bfin_serial_init_ports();
989 ret
= uart_register_driver(&bfin_serial_reg
);
991 ret
= platform_driver_register(&bfin_serial_driver
);
993 pr_debug("uart register failed\n");
994 uart_unregister_driver(&bfin_serial_reg
);
1000 static void __exit
bfin_serial_exit(void)
1002 platform_driver_unregister(&bfin_serial_driver
);
1003 uart_unregister_driver(&bfin_serial_reg
);
1006 module_init(bfin_serial_init
);
1007 module_exit(bfin_serial_exit
);
1009 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1010 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1011 MODULE_LICENSE("GPL");
1012 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR
);