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
))
188 status
&= ~(PE
| FE
);
191 uart
->port
.icount
.parity
++;
193 uart
->port
.icount
.overrun
++;
195 uart
->port
.icount
.frame
++;
197 status
&= uart
->port
.read_status_mask
;
201 else if (status
& PE
)
203 else if (status
& FE
)
208 if (uart_handle_sysrq_char(&uart
->port
, ch
))
211 uart_insert_char(&uart
->port
, status
, OE
, ch
, flg
);
214 tty_flip_buffer_push(tty
);
217 static void bfin_serial_tx_chars(struct bfin_serial_port
*uart
)
219 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
221 if (uart
->port
.x_char
) {
222 UART_PUT_CHAR(uart
, uart
->port
.x_char
);
223 uart
->port
.icount
.tx
++;
224 uart
->port
.x_char
= 0;
228 * Check the modem control lines before
229 * transmitting anything.
231 bfin_serial_mctrl_check(uart
);
233 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uart
->port
)) {
234 bfin_serial_stop_tx(&uart
->port
);
238 local_put_char(uart
, xmit
->buf
[xmit
->tail
]);
239 xmit
->tail
= (xmit
->tail
+ 1) & (UART_XMIT_SIZE
- 1);
240 uart
->port
.icount
.tx
++;
242 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
243 uart_write_wakeup(&uart
->port
);
245 if (uart_circ_empty(xmit
))
246 bfin_serial_stop_tx(&uart
->port
);
249 static irqreturn_t
bfin_serial_rx_int(int irq
, void *dev_id
)
251 struct bfin_serial_port
*uart
= dev_id
;
253 spin_lock(&uart
->port
.lock
);
254 while ((UART_GET_IIR(uart
) & IIR_STATUS
) == IIR_RX_READY
)
255 bfin_serial_rx_chars(uart
);
256 spin_unlock(&uart
->port
.lock
);
260 static irqreturn_t
bfin_serial_tx_int(int irq
, void *dev_id
)
262 struct bfin_serial_port
*uart
= dev_id
;
264 spin_lock(&uart
->port
.lock
);
265 while ((UART_GET_IIR(uart
) & IIR_STATUS
) == IIR_TX_READY
)
266 bfin_serial_tx_chars(uart
);
267 spin_unlock(&uart
->port
.lock
);
272 static void bfin_serial_do_work(struct work_struct
*work
)
274 struct bfin_serial_port
*uart
= container_of(work
, struct bfin_serial_port
, cts_workqueue
);
276 bfin_serial_mctrl_check(uart
);
281 #ifdef CONFIG_SERIAL_BFIN_DMA
282 static void bfin_serial_dma_tx_chars(struct bfin_serial_port
*uart
)
284 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
293 if (uart
->port
.x_char
) {
294 UART_PUT_CHAR(uart
, uart
->port
.x_char
);
295 uart
->port
.icount
.tx
++;
296 uart
->port
.x_char
= 0;
301 * Check the modem control lines before
302 * transmitting anything.
304 bfin_serial_mctrl_check(uart
);
306 if (uart_circ_empty(xmit
) || uart_tx_stopped(&uart
->port
)) {
307 bfin_serial_stop_tx(&uart
->port
);
312 spin_lock_irqsave(&uart
->port
.lock
, flags
);
313 uart
->tx_count
= CIRC_CNT(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
314 if (uart
->tx_count
> (UART_XMIT_SIZE
- xmit
->tail
))
315 uart
->tx_count
= UART_XMIT_SIZE
- xmit
->tail
;
316 blackfin_dcache_flush_range((unsigned long)(xmit
->buf
+xmit
->tail
),
317 (unsigned long)(xmit
->buf
+xmit
->tail
+uart
->tx_count
));
318 set_dma_config(uart
->tx_dma_channel
,
319 set_bfin_dma_config(DIR_READ
, DMA_FLOW_STOP
,
323 set_dma_start_addr(uart
->tx_dma_channel
, (unsigned long)(xmit
->buf
+xmit
->tail
));
324 set_dma_x_count(uart
->tx_dma_channel
, uart
->tx_count
);
325 set_dma_x_modify(uart
->tx_dma_channel
, 1);
326 enable_dma(uart
->tx_dma_channel
);
327 ier
= UART_GET_IER(uart
);
329 UART_PUT_IER(uart
, ier
);
330 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
333 static void bfin_serial_dma_rx_chars(struct bfin_serial_port
*uart
)
335 struct tty_struct
*tty
= uart
->port
.info
->tty
;
338 status
= UART_GET_LSR(uart
);
339 uart
->port
.icount
.rx
+= CIRC_CNT(uart
->rx_dma_buf
.head
, uart
->rx_dma_buf
.tail
, UART_XMIT_SIZE
);;
342 uart
->port
.icount
.brk
++;
343 if (uart_handle_break(&uart
->port
))
344 goto dma_ignore_char
;
345 status
&= ~(PE
| FE
);
348 uart
->port
.icount
.parity
++;
350 uart
->port
.icount
.overrun
++;
352 uart
->port
.icount
.frame
++;
354 status
&= uart
->port
.read_status_mask
;
358 else if (status
& PE
)
360 else if (status
& FE
)
365 for (i
= uart
->rx_dma_buf
.head
; i
< uart
->rx_dma_buf
.tail
; i
++) {
366 if (uart_handle_sysrq_char(&uart
->port
, uart
->rx_dma_buf
.buf
[i
]))
367 goto dma_ignore_char
;
368 uart_insert_char(&uart
->port
, status
, OE
, uart
->rx_dma_buf
.buf
[i
], flg
);
372 tty_flip_buffer_push(tty
);
375 void bfin_serial_rx_dma_timeout(struct bfin_serial_port
*uart
)
380 bfin_serial_dma_tx_chars(uart
);
382 spin_lock_irqsave(&uart
->port
.lock
, flags
);
383 x_pos
= DMA_RX_XCOUNT
- get_dma_curr_xcount(uart
->rx_dma_channel
);
384 if (x_pos
== DMA_RX_XCOUNT
)
387 pos
= uart
->rx_dma_nrows
* DMA_RX_XCOUNT
+ x_pos
;
389 if (pos
>uart
->rx_dma_buf
.tail
) {
390 uart
->rx_dma_buf
.tail
= pos
;
391 bfin_serial_dma_rx_chars(uart
);
392 uart
->rx_dma_buf
.head
= uart
->rx_dma_buf
.tail
;
394 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
395 uart
->rx_dma_timer
.expires
= jiffies
+ DMA_RX_FLUSH_JIFFIES
;
396 add_timer(&(uart
->rx_dma_timer
));
399 static irqreturn_t
bfin_serial_dma_tx_int(int irq
, void *dev_id
)
401 struct bfin_serial_port
*uart
= dev_id
;
402 struct circ_buf
*xmit
= &uart
->port
.info
->xmit
;
405 spin_lock(&uart
->port
.lock
);
406 if (!(get_dma_curr_irqstat(uart
->tx_dma_channel
)&DMA_RUN
)) {
407 clear_dma_irqstat(uart
->tx_dma_channel
);
408 disable_dma(uart
->tx_dma_channel
);
409 ier
= UART_GET_IER(uart
);
411 UART_PUT_IER(uart
, ier
);
412 xmit
->tail
= (xmit
->tail
+uart
->tx_count
) &(UART_XMIT_SIZE
-1);
413 uart
->port
.icount
.tx
+=uart
->tx_count
;
415 if (uart_circ_chars_pending(xmit
) < WAKEUP_CHARS
)
416 uart_write_wakeup(&uart
->port
);
418 if (uart_circ_empty(xmit
))
419 bfin_serial_stop_tx(&uart
->port
);
423 spin_unlock(&uart
->port
.lock
);
427 static irqreturn_t
bfin_serial_dma_rx_int(int irq
, void *dev_id
)
429 struct bfin_serial_port
*uart
= dev_id
;
430 unsigned short irqstat
;
432 uart
->rx_dma_nrows
++;
433 if (uart
->rx_dma_nrows
== DMA_RX_YCOUNT
) {
434 uart
->rx_dma_nrows
= 0;
435 uart
->rx_dma_buf
.tail
= DMA_RX_XCOUNT
*DMA_RX_YCOUNT
;
436 bfin_serial_dma_rx_chars(uart
);
437 uart
->rx_dma_buf
.head
= uart
->rx_dma_buf
.tail
= 0;
439 spin_lock(&uart
->port
.lock
);
440 irqstat
= get_dma_curr_irqstat(uart
->rx_dma_channel
);
441 clear_dma_irqstat(uart
->rx_dma_channel
);
443 spin_unlock(&uart
->port
.lock
);
449 * Return TIOCSER_TEMT when transmitter is not busy.
451 static unsigned int bfin_serial_tx_empty(struct uart_port
*port
)
453 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
456 lsr
= UART_GET_LSR(uart
);
463 static unsigned int bfin_serial_get_mctrl(struct uart_port
*port
)
465 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
466 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
467 if (uart
->cts_pin
< 0)
468 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
470 if (gpio_get_value(uart
->cts_pin
))
471 return TIOCM_DSR
| TIOCM_CAR
;
474 return TIOCM_CTS
| TIOCM_DSR
| TIOCM_CAR
;
477 static void bfin_serial_set_mctrl(struct uart_port
*port
, unsigned int mctrl
)
479 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
480 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
481 if (uart
->rts_pin
< 0)
484 if (mctrl
& TIOCM_RTS
)
485 gpio_set_value(uart
->rts_pin
, 0);
487 gpio_set_value(uart
->rts_pin
, 1);
492 * Handle any change of modem status signal since we were last called.
494 static void bfin_serial_mctrl_check(struct bfin_serial_port
*uart
)
496 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
498 # ifdef CONFIG_SERIAL_BFIN_DMA
499 struct uart_info
*info
= uart
->port
.info
;
500 struct tty_struct
*tty
= info
->tty
;
502 status
= bfin_serial_get_mctrl(&uart
->port
);
503 if (!(status
& TIOCM_CTS
)) {
509 status
= bfin_serial_get_mctrl(&uart
->port
);
510 uart_handle_cts_change(&uart
->port
, status
& TIOCM_CTS
);
511 if (!(status
& TIOCM_CTS
))
512 schedule_work(&uart
->cts_workqueue
);
518 * Interrupts are always disabled.
520 static void bfin_serial_break_ctl(struct uart_port
*port
, int break_state
)
522 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
523 u16 lcr
= UART_GET_LCR(uart
);
528 UART_PUT_LCR(uart
, lcr
);
532 static int bfin_serial_startup(struct uart_port
*port
)
534 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
536 #ifdef CONFIG_SERIAL_BFIN_DMA
537 dma_addr_t dma_handle
;
539 if (request_dma(uart
->rx_dma_channel
, "BFIN_UART_RX") < 0) {
540 printk(KERN_NOTICE
"Unable to attach Blackfin UART RX DMA channel\n");
544 if (request_dma(uart
->tx_dma_channel
, "BFIN_UART_TX") < 0) {
545 printk(KERN_NOTICE
"Unable to attach Blackfin UART TX DMA channel\n");
546 free_dma(uart
->rx_dma_channel
);
550 set_dma_callback(uart
->rx_dma_channel
, bfin_serial_dma_rx_int
, uart
);
551 set_dma_callback(uart
->tx_dma_channel
, bfin_serial_dma_tx_int
, uart
);
553 uart
->rx_dma_buf
.buf
= (unsigned char *)dma_alloc_coherent(NULL
, PAGE_SIZE
, &dma_handle
, GFP_DMA
);
554 uart
->rx_dma_buf
.head
= 0;
555 uart
->rx_dma_buf
.tail
= 0;
556 uart
->rx_dma_nrows
= 0;
558 set_dma_config(uart
->rx_dma_channel
,
559 set_bfin_dma_config(DIR_WRITE
, DMA_FLOW_AUTO
,
560 INTR_ON_ROW
, DIMENSION_2D
,
562 set_dma_x_count(uart
->rx_dma_channel
, DMA_RX_XCOUNT
);
563 set_dma_x_modify(uart
->rx_dma_channel
, 1);
564 set_dma_y_count(uart
->rx_dma_channel
, DMA_RX_YCOUNT
);
565 set_dma_y_modify(uart
->rx_dma_channel
, 1);
566 set_dma_start_addr(uart
->rx_dma_channel
, (unsigned long)uart
->rx_dma_buf
.buf
);
567 enable_dma(uart
->rx_dma_channel
);
569 uart
->rx_dma_timer
.data
= (unsigned long)(uart
);
570 uart
->rx_dma_timer
.function
= (void *)bfin_serial_rx_dma_timeout
;
571 uart
->rx_dma_timer
.expires
= jiffies
+ DMA_RX_FLUSH_JIFFIES
;
572 add_timer(&(uart
->rx_dma_timer
));
575 (uart
->port
.irq
, bfin_serial_rx_int
, IRQF_DISABLED
,
576 "BFIN_UART_RX", uart
)) {
577 printk(KERN_NOTICE
"Unable to attach BlackFin UART RX interrupt\n");
582 (uart
->port
.irq
+1, bfin_serial_tx_int
, IRQF_DISABLED
,
583 "BFIN_UART_TX", uart
)) {
584 printk(KERN_NOTICE
"Unable to attach BlackFin UART TX interrupt\n");
585 free_irq(uart
->port
.irq
, uart
);
589 UART_PUT_IER(uart
, UART_GET_IER(uart
) | ERBFI
);
593 static void bfin_serial_shutdown(struct uart_port
*port
)
595 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
597 #ifdef CONFIG_SERIAL_BFIN_DMA
598 disable_dma(uart
->tx_dma_channel
);
599 free_dma(uart
->tx_dma_channel
);
600 disable_dma(uart
->rx_dma_channel
);
601 free_dma(uart
->rx_dma_channel
);
602 del_timer(&(uart
->rx_dma_timer
));
604 free_irq(uart
->port
.irq
, uart
);
605 free_irq(uart
->port
.irq
+1, uart
);
610 bfin_serial_set_termios(struct uart_port
*port
, struct ktermios
*termios
,
611 struct ktermios
*old
)
613 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
615 unsigned int baud
, quot
;
616 unsigned short val
, ier
, lsr
, lcr
= 0;
618 switch (termios
->c_cflag
& CSIZE
) {
632 printk(KERN_ERR
"%s: word lengh not supported\n",
636 if (termios
->c_cflag
& CSTOPB
)
638 if (termios
->c_cflag
& PARENB
) {
640 if (!(termios
->c_cflag
& PARODD
))
642 if (termios
->c_cflag
& CMSPAR
)
646 port
->read_status_mask
= OE
;
647 if (termios
->c_iflag
& INPCK
)
648 port
->read_status_mask
|= (FE
| PE
);
649 if (termios
->c_iflag
& (BRKINT
| PARMRK
))
650 port
->read_status_mask
|= BI
;
653 * Characters to ignore
655 port
->ignore_status_mask
= 0;
656 if (termios
->c_iflag
& IGNPAR
)
657 port
->ignore_status_mask
|= FE
| PE
;
658 if (termios
->c_iflag
& IGNBRK
) {
659 port
->ignore_status_mask
|= BI
;
661 * If we're ignoring parity and break indicators,
662 * ignore overruns too (for real raw support).
664 if (termios
->c_iflag
& IGNPAR
)
665 port
->ignore_status_mask
|= OE
;
668 baud
= uart_get_baud_rate(port
, termios
, old
, 0, port
->uartclk
/16);
669 quot
= uart_get_divisor(port
, baud
);
670 spin_lock_irqsave(&uart
->port
.lock
, flags
);
673 lsr
= UART_GET_LSR(uart
);
674 } while (!(lsr
& TEMT
));
677 ier
= UART_GET_IER(uart
);
678 UART_PUT_IER(uart
, 0);
680 /* Set DLAB in LCR to Access DLL and DLH */
681 val
= UART_GET_LCR(uart
);
683 UART_PUT_LCR(uart
, val
);
686 UART_PUT_DLL(uart
, quot
& 0xFF);
688 UART_PUT_DLH(uart
, (quot
>> 8) & 0xFF);
691 /* Clear DLAB in LCR to Access THR RBR IER */
692 val
= UART_GET_LCR(uart
);
694 UART_PUT_LCR(uart
, val
);
697 UART_PUT_LCR(uart
, lcr
);
700 UART_PUT_IER(uart
, ier
);
702 val
= UART_GET_GCTL(uart
);
704 UART_PUT_GCTL(uart
, val
);
706 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
709 static const char *bfin_serial_type(struct uart_port
*port
)
711 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
713 return uart
->port
.type
== PORT_BFIN
? "BFIN-UART" : NULL
;
717 * Release the memory region(s) being used by 'port'.
719 static void bfin_serial_release_port(struct uart_port
*port
)
724 * Request the memory region(s) being used by 'port'.
726 static int bfin_serial_request_port(struct uart_port
*port
)
732 * Configure/autoconfigure the port.
734 static void bfin_serial_config_port(struct uart_port
*port
, int flags
)
736 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
738 if (flags
& UART_CONFIG_TYPE
&&
739 bfin_serial_request_port(&uart
->port
) == 0)
740 uart
->port
.type
= PORT_BFIN
;
744 * Verify the new serial_struct (for TIOCSSERIAL).
745 * The only change we allow are to the flags and type, and
746 * even then only between PORT_BFIN and PORT_UNKNOWN
749 bfin_serial_verify_port(struct uart_port
*port
, struct serial_struct
*ser
)
754 static struct uart_ops bfin_serial_pops
= {
755 .tx_empty
= bfin_serial_tx_empty
,
756 .set_mctrl
= bfin_serial_set_mctrl
,
757 .get_mctrl
= bfin_serial_get_mctrl
,
758 .stop_tx
= bfin_serial_stop_tx
,
759 .start_tx
= bfin_serial_start_tx
,
760 .stop_rx
= bfin_serial_stop_rx
,
761 .enable_ms
= bfin_serial_enable_ms
,
762 .break_ctl
= bfin_serial_break_ctl
,
763 .startup
= bfin_serial_startup
,
764 .shutdown
= bfin_serial_shutdown
,
765 .set_termios
= bfin_serial_set_termios
,
766 .type
= bfin_serial_type
,
767 .release_port
= bfin_serial_release_port
,
768 .request_port
= bfin_serial_request_port
,
769 .config_port
= bfin_serial_config_port
,
770 .verify_port
= bfin_serial_verify_port
,
773 static void __init
bfin_serial_init_ports(void)
775 static int first
= 1;
782 for (i
= 0; i
< nr_ports
; i
++) {
783 bfin_serial_ports
[i
].port
.uartclk
= get_sclk();
784 bfin_serial_ports
[i
].port
.ops
= &bfin_serial_pops
;
785 bfin_serial_ports
[i
].port
.line
= i
;
786 bfin_serial_ports
[i
].port
.iotype
= UPIO_MEM
;
787 bfin_serial_ports
[i
].port
.membase
=
788 (void __iomem
*)bfin_serial_resource
[i
].uart_base_addr
;
789 bfin_serial_ports
[i
].port
.mapbase
=
790 bfin_serial_resource
[i
].uart_base_addr
;
791 bfin_serial_ports
[i
].port
.irq
=
792 bfin_serial_resource
[i
].uart_irq
;
793 bfin_serial_ports
[i
].port
.flags
= UPF_BOOT_AUTOCONF
;
794 #ifdef CONFIG_SERIAL_BFIN_DMA
795 bfin_serial_ports
[i
].tx_done
= 1;
796 bfin_serial_ports
[i
].tx_count
= 0;
797 bfin_serial_ports
[i
].tx_dma_channel
=
798 bfin_serial_resource
[i
].uart_tx_dma_channel
;
799 bfin_serial_ports
[i
].rx_dma_channel
=
800 bfin_serial_resource
[i
].uart_rx_dma_channel
;
801 init_timer(&(bfin_serial_ports
[i
].rx_dma_timer
));
803 INIT_WORK(&bfin_serial_ports
[i
].cts_workqueue
, bfin_serial_do_work
);
805 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
806 bfin_serial_ports
[i
].cts_pin
=
807 bfin_serial_resource
[i
].uart_cts_pin
;
808 bfin_serial_ports
[i
].rts_pin
=
809 bfin_serial_resource
[i
].uart_rts_pin
;
811 bfin_serial_hw_init(&bfin_serial_ports
[i
]);
816 #ifdef CONFIG_SERIAL_BFIN_CONSOLE
817 static void bfin_serial_console_putchar(struct uart_port
*port
, int ch
)
819 struct bfin_serial_port
*uart
= (struct bfin_serial_port
*)port
;
820 while (!(UART_GET_LSR(uart
)))
822 UART_PUT_CHAR(uart
, ch
);
827 * Interrupts are disabled on entering
830 bfin_serial_console_write(struct console
*co
, const char *s
, unsigned int count
)
832 struct bfin_serial_port
*uart
= &bfin_serial_ports
[co
->index
];
835 spin_lock_irqsave(&uart
->port
.lock
, flags
);
836 uart_console_write(&uart
->port
, s
, count
, bfin_serial_console_putchar
);
837 spin_unlock_irqrestore(&uart
->port
.lock
, flags
);
842 * If the port was already initialised (eg, by a boot loader),
843 * try to determine the current setup.
846 bfin_serial_console_get_options(struct bfin_serial_port
*uart
, int *baud
,
847 int *parity
, int *bits
)
849 unsigned short status
;
851 status
= UART_GET_IER(uart
) & (ERBFI
| ETBEI
);
852 if (status
== (ERBFI
| ETBEI
)) {
853 /* ok, the port was enabled */
854 unsigned short lcr
, val
;
855 unsigned short dlh
, dll
;
857 lcr
= UART_GET_LCR(uart
);
866 switch (lcr
& 0x03) {
867 case 0: *bits
= 5; break;
868 case 1: *bits
= 6; break;
869 case 2: *bits
= 7; break;
870 case 3: *bits
= 8; break;
872 /* Set DLAB in LCR to Access DLL and DLH */
873 val
= UART_GET_LCR(uart
);
875 UART_PUT_LCR(uart
, val
);
877 dll
= UART_GET_DLL(uart
);
878 dlh
= UART_GET_DLH(uart
);
880 /* Clear DLAB in LCR to Access THR RBR IER */
881 val
= UART_GET_LCR(uart
);
883 UART_PUT_LCR(uart
, val
);
885 *baud
= get_sclk() / (16*(dll
| dlh
<< 8));
887 pr_debug("%s:baud = %d, parity = %c, bits= %d\n", __FUNCTION__
, *baud
, *parity
, *bits
);
891 bfin_serial_console_setup(struct console
*co
, char *options
)
893 struct bfin_serial_port
*uart
;
897 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
904 * Check whether an invalid uart number has been specified, and
905 * if so, search for the first available port that does have
908 if (co
->index
== -1 || co
->index
>= nr_ports
)
910 uart
= &bfin_serial_ports
[co
->index
];
913 uart_parse_options(options
, &baud
, &parity
, &bits
, &flow
);
915 bfin_serial_console_get_options(uart
, &baud
, &parity
, &bits
);
917 return uart_set_options(&uart
->port
, co
, baud
, parity
, bits
, flow
);
920 static struct uart_driver bfin_serial_reg
;
921 static struct console bfin_serial_console
= {
922 .name
= BFIN_SERIAL_NAME
,
923 .write
= bfin_serial_console_write
,
924 .device
= uart_console_device
,
925 .setup
= bfin_serial_console_setup
,
926 .flags
= CON_PRINTBUFFER
,
928 .data
= &bfin_serial_reg
,
931 static int __init
bfin_serial_rs_console_init(void)
933 bfin_serial_init_ports();
934 register_console(&bfin_serial_console
);
937 console_initcall(bfin_serial_rs_console_init
);
939 #define BFIN_SERIAL_CONSOLE &bfin_serial_console
941 #define BFIN_SERIAL_CONSOLE NULL
944 static struct uart_driver bfin_serial_reg
= {
945 .owner
= THIS_MODULE
,
946 .driver_name
= "bfin-uart",
947 .dev_name
= BFIN_SERIAL_NAME
,
948 .major
= BFIN_SERIAL_MAJOR
,
949 .minor
= BFIN_SERIAL_MINOR
,
951 .cons
= BFIN_SERIAL_CONSOLE
,
954 static int bfin_serial_suspend(struct platform_device
*dev
, pm_message_t state
)
956 struct bfin_serial_port
*uart
= platform_get_drvdata(dev
);
959 uart_suspend_port(&bfin_serial_reg
, &uart
->port
);
964 static int bfin_serial_resume(struct platform_device
*dev
)
966 struct bfin_serial_port
*uart
= platform_get_drvdata(dev
);
969 uart_resume_port(&bfin_serial_reg
, &uart
->port
);
974 static int bfin_serial_probe(struct platform_device
*dev
)
976 struct resource
*res
= dev
->resource
;
979 for (i
= 0; i
< dev
->num_resources
; i
++, res
++)
980 if (res
->flags
& IORESOURCE_MEM
)
983 if (i
< dev
->num_resources
) {
984 for (i
= 0; i
< nr_ports
; i
++, res
++) {
985 if (bfin_serial_ports
[i
].port
.mapbase
!= res
->start
)
987 bfin_serial_ports
[i
].port
.dev
= &dev
->dev
;
988 uart_add_one_port(&bfin_serial_reg
, &bfin_serial_ports
[i
].port
);
989 platform_set_drvdata(dev
, &bfin_serial_ports
[i
]);
996 static int bfin_serial_remove(struct platform_device
*pdev
)
998 struct bfin_serial_port
*uart
= platform_get_drvdata(pdev
);
1001 #ifdef CONFIG_SERIAL_BFIN_CTSRTS
1002 gpio_free(uart
->cts_pin
);
1003 gpio_free(uart
->rts_pin
);
1006 platform_set_drvdata(pdev
, NULL
);
1009 uart_remove_one_port(&bfin_serial_reg
, &uart
->port
);
1014 static struct platform_driver bfin_serial_driver
= {
1015 .probe
= bfin_serial_probe
,
1016 .remove
= bfin_serial_remove
,
1017 .suspend
= bfin_serial_suspend
,
1018 .resume
= bfin_serial_resume
,
1020 .name
= "bfin-uart",
1024 static int __init
bfin_serial_init(void)
1028 pr_info("Serial: Blackfin serial driver\n");
1030 bfin_serial_init_ports();
1032 ret
= uart_register_driver(&bfin_serial_reg
);
1034 ret
= platform_driver_register(&bfin_serial_driver
);
1036 pr_debug("uart register failed\n");
1037 uart_unregister_driver(&bfin_serial_reg
);
1043 static void __exit
bfin_serial_exit(void)
1045 platform_driver_unregister(&bfin_serial_driver
);
1046 uart_unregister_driver(&bfin_serial_reg
);
1049 module_init(bfin_serial_init
);
1050 module_exit(bfin_serial_exit
);
1052 MODULE_AUTHOR("Aubrey.Li <aubrey.li@analog.com>");
1053 MODULE_DESCRIPTION("Blackfin generic serial port driver");
1054 MODULE_LICENSE("GPL");
1055 MODULE_ALIAS_CHARDEV_MAJOR(BFIN_SERIAL_MAJOR
);