2 * Driver for RK-UART controller.
\r
3 * Based on drivers/tty/serial/8250.c
\r
5 * Copyright (C) 2011 Rochchip.
\r
7 * This program is free software; you can redistribute it and/or modify
\r
8 * it under the terms of the GNU General Public License as published by
\r
9 * the Free Software Foundation; either version 2 of the License, or
\r
11 * (at your option) any later version.
\r
13 * Author: hhb@rock-chips.com
\r
17 #ifndef CONFIG_SERIAL_RK_CONSOLE
\r
18 #if defined(CONFIG_SERIAL_RK29_CONSOLE)
\r
19 #define CONFIG_SERIAL_RK_CONSOLE
\r
23 #if defined(CONFIG_SERIAL_RK_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
\r
24 #define SUPPORT_SYSRQ
\r
27 #include <linux/module.h>
\r
28 #include <linux/ioport.h>
\r
29 #include <linux/init.h>
\r
30 #include <linux/console.h>
\r
31 #include <linux/sysrq.h>
\r
32 #include <linux/delay.h>
\r
33 #include <linux/platform_device.h>
\r
34 #include <linux/tty.h>
\r
35 #include <linux/ratelimit.h>
\r
36 #include <linux/tty_flip.h>
\r
37 #include <linux/serial_reg.h>
\r
38 #include <linux/serial_core.h>
\r
39 #include <linux/serial.h>
\r
40 #include <linux/nmi.h>
\r
41 #include <linux/mutex.h>
\r
42 #include <linux/slab.h>
\r
43 #include <linux/clk.h>
\r
44 #include <linux/timer.h>
\r
45 #include <linux/workqueue.h>
\r
46 #include <linux/dma-mapping.h>
\r
49 #include <asm/irq.h>
\r
52 * Driver Version Note
\r
54 *v0.0 : this driver is 2.6.32 kernel driver;
\r
55 *v0.1 : this driver is 3.0.8 kernel driver;
\r
57 * 1.modify dma dirver;
\r
58 * 2.enable Programmable THRE Interrupt Mode, so we can just judge ((up->iir & 0x0f) == 0x02) when transmit
\r
59 * 3.reset uart and set it to loopback state to ensure setting baud rate sucessfully
\r
61 * 1. dma driver:make "when tx dma is only enable" work functionally
\r
63 * 1. dma driver:serial rx use new dma interface rk29_dma_enqueue_ring
\r
65 * 1. When enable Programmable THRE Interrupt Mode, in lsr register, only UART_LSR_TEMT means transmit empty, but
\r
66 UART_LSR_THRE doesn't. So, the macro BOTH_EMPTY should be replaced with UART_LSR_TEMT.
\r
68 #define VERSION_AND_TIME "rk_serial.c v1.3 2012-12-14"
\r
71 #define UART_USR 0x1F /* UART Status Register */
\r
72 #define UART_USR_BUSY (1)
\r
73 #define UART_IER_PTIME 0x80 /* Programmable THRE Interrupt Mode Enable */
\r
74 #define UART_LSR_RFE 0x80 /* receive fifo error */
\r
75 #define UART_SRR 0x22 /* software reset register */
\r
76 #define UART_RESET 0x01
\r
79 //#define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
\r
81 #define UART_NR 4 //uart port number
\r
84 /* configurate whether the port transmit-receive by DMA in menuconfig*/
\r
91 #ifdef CONFIG_UART0_DMA_RK29
\r
92 #define UART0_USE_DMA CONFIG_UART0_DMA_RK29
\r
94 #define UART0_USE_DMA CLOSE_DMA
\r
96 #ifdef CONFIG_UART1_DMA_RK29
\r
97 #define UART1_USE_DMA CONFIG_UART1_DMA_RK29
\r
99 #define UART1_USE_DMA CLOSE_DMA
\r
101 #ifdef CONFIG_UART2_DMA_RK29
\r
102 #define UART2_USE_DMA CONFIG_UART2_DMA_RK29
\r
104 #define UART2_USE_DMA CLOSE_DMA
\r
106 #ifdef CONFIG_UART3_DMA_RK29
\r
107 #define UART3_USE_DMA CONFIG_UART3_DMA_RK29
\r
109 #define UART3_USE_DMA CLOSE_DMA
\r
112 #define USE_TIMER 1 // use timer for dma transport
\r
113 #define POWER_MANEGEMENT 1
\r
114 #define RX_TIMEOUT (3000*3) //uint ms
\r
115 #define DMA_TX_TRRIGE_LEVEL 128
\r
116 #define SERIAL_CIRC_CNT_TO_END(xmit) CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE)
\r
119 #define USE_DMA (UART0_USE_DMA | UART1_USE_DMA | UART2_USE_DMA | UART3_USE_DMA)
\r
121 #ifdef CONFIG_ARCH_RK29
\r
122 #include <mach/dma-pl330.h>
\r
124 #include <plat/dma-pl330.h>
\r
130 static struct uart_driver serial_rk_reg;
\r
135 #ifdef CONFIG_ARCH_RK29
\r
136 #define DBG_PORT 1 //DBG_PORT which uart is used to print log message
\r
138 #ifndef CONFIG_RK_DEBUG_UART //DBG_PORT which uart is used to print log message
\r
141 #define DBG_PORT CONFIG_RK_DEBUG_UART
\r
145 #ifdef CONFIG_SERIAL_CORE_CONSOLE
\r
146 #define uart_console(port) ((port)->cons && (port)->cons->index == (port)->line)
\r
148 #define uart_console(port) (0)
\r
152 extern void printascii(const char *);
\r
153 static void dbg(const char *fmt, ...)
\r
159 vsprintf(buff, fmt, va);
\r
165 //enable log output
\r
167 static int log_port = -1;
\r
168 module_param(log_port, int, S_IRUGO|S_IWUSR);
\r
171 #define DEBUG_INTR(fmt...) if (up->port.line == log_port && !uart_console(&up->port)) dbg(fmt)
\r
173 #define DEBUG_INTR(fmt...) do { } while (0)
\r
178 /* added by hhb@rock-chips.com for uart dma transfer */
\r
180 struct rk_uart_dma {
\r
181 u32 use_dma; //1:used
\r
182 enum dma_ch rx_dmach;
\r
183 enum dma_ch tx_dmach;
\r
185 //receive and transfer buffer
\r
186 char * rx_buffer; //visual memory
\r
188 dma_addr_t rx_phy_addr; //physical memory
\r
189 dma_addr_t tx_phy_addr;
\r
190 u32 rb_size; //buffer size
\r
193 //regard the rx buffer as a circular buffer
\r
198 spinlock_t tx_lock;
\r
199 spinlock_t rx_lock;
\r
201 char tx_dma_inited; //1:dma tx channel has been init
\r
202 char rx_dma_inited; //1:dma rx channel has been init
\r
203 char tx_dma_used; //1:dma tx is working
\r
204 char rx_dma_used; //1:dma rx is working
\r
206 /* timer to poll activity on rx dma */
\r
209 struct timer_list rx_timer;
\r
213 struct uart_rk_port {
\r
214 struct uart_port port;
\r
215 struct platform_device *pdev;
\r
218 unsigned int tx_loadsz; /* transmit fifo load size */
\r
225 * Some bits in registers are cleared on a read, so they must
\r
226 * be saved whenever the register is read but the bits will not
\r
227 * be immediately processed.
\r
229 #define LSR_SAVE_FLAGS UART_LSR_BRK_ERROR_BITS
\r
230 unsigned char lsr_saved_flags;
\r
232 #define MSR_SAVE_FLAGS UART_MSR_ANY_DELTA
\r
233 unsigned char msr_saved_flags;
\r
239 unsigned long port_activity;
\r
240 struct work_struct uart_work;
\r
241 struct work_struct uart_work_rx;
\r
242 struct workqueue_struct *uart_wq;
\r
244 struct rk_uart_dma *dma;
\r
249 static void serial_rk_release_dma_tx(struct uart_port *port);
\r
250 static int serial_rk_start_tx_dma(struct uart_port *port);
\r
251 //static void serial_rk_rx_timeout(unsigned long uart);
\r
252 static void serial_rk_release_dma_rx(struct uart_port *port);
\r
253 static int serial_rk_start_rx_dma(struct uart_port *port);
\r
255 static inline int serial_rk_start_tx_dma(struct uart_port *port) { return 0; }
\r
257 static int serial_rk_startup(struct uart_port *port);
\r
259 static inline unsigned int serial_in(struct uart_rk_port *up, int offset)
\r
261 offset = offset << 2;
\r
262 return __raw_readb(up->port.membase + offset);
\r
265 /* Save the LCR value so it can be re-written when a Busy Detect IRQ occurs. */
\r
266 static inline void dwapb_save_out_value(struct uart_rk_port *up, int offset,
\r
267 unsigned char value)
\r
269 if (offset == UART_LCR)
\r
273 /* Read the IER to ensure any interrupt is cleared before returning from ISR. */
\r
274 static inline void dwapb_check_clear_ier(struct uart_rk_port *up, int offset)
\r
276 if (offset == UART_TX || offset == UART_IER)
\r
277 serial_in(up, UART_IER);
\r
280 static inline void serial_out(struct uart_rk_port *up, int offset, unsigned char value)
\r
282 dwapb_save_out_value(up, offset, value);
\r
283 __raw_writeb(value, up->port.membase + (offset << 2));
\r
284 if (offset != UART_TX)
\r
286 dwapb_check_clear_ier(up, offset);
\r
289 /* Uart divisor latch read */
\r
290 static inline int serial_dl_read(struct uart_rk_port *up)
\r
292 return serial_in(up, UART_DLL) | serial_in(up, UART_DLM) << 8;
\r
295 /* Uart divisor latch write */
\r
296 static int serial_dl_write(struct uart_rk_port *up, unsigned int value)
\r
298 unsigned int tmout = 100;
\r
300 while(!(serial_in(up, UART_LCR) & UART_LCR_DLAB)){
\r
302 if(up->port.line != DBG_PORT)
\r
303 dbg("set serial.%d baudrate fail with DLAB not set\n", up->port.line);
\r
309 while(serial_in(up, UART_USR) & UART_USR_BUSY){
\r
311 if(up->port.line != DBG_PORT)
\r
312 dbg("set serial.%d baudrate timeout\n", up->port.line);
\r
318 serial_out(up, UART_DLL, value & 0xff);
\r
319 serial_out(up, UART_DLM, value >> 8 & 0xff);
\r
325 static int serial_lcr_write(struct uart_rk_port *up, unsigned char value)
\r
327 unsigned int tmout = 15000;
\r
329 while(serial_in(up, UART_USR) & UART_USR_BUSY){
\r
331 if(up->port.line != DBG_PORT)
\r
332 dbg("set serial.%d lc r = 0x%02x timeout\n", up->port.line, value);
\r
338 serial_out(up, UART_LCR, value);
\r
343 static inline void serial_rk_enable_ier_thri(struct uart_rk_port *up)
\r
345 if (!(up->ier & UART_IER_THRI)) {
\r
346 up->ier |= UART_IER_THRI;
\r
347 serial_out(up, UART_IER, up->ier);
\r
352 static inline void serial_rk_disable_ier_thri(struct uart_rk_port *up)
\r
354 if (up->ier & UART_IER_THRI) {
\r
355 up->ier &= ~UART_IER_THRI;
\r
356 serial_out(up, UART_IER, up->ier);
\r
360 static int rk29_uart_dump_register(struct uart_rk_port *up){
\r
362 unsigned int reg_value = 0;
\r
364 reg_value = serial_in(up, UART_IER);
\r
365 dbg("UART_IER = 0x%0x\n", reg_value);
\r
366 reg_value = serial_in(up, UART_IIR);
\r
367 dbg("UART_IIR = 0x%0x\n", reg_value);
\r
368 reg_value = serial_in(up, UART_LSR);
\r
369 dbg("UART_LSR = 0x%0x\n", reg_value);
\r
370 reg_value = serial_in(up, UART_MSR);
\r
371 dbg("UART_MSR = 0x%0x\n", reg_value);
\r
372 reg_value = serial_in(up, UART_MCR);
\r
373 dbg("UART_MCR = 0x%0x\n", reg_value);
\r
374 reg_value = serial_in(up, 0x21);
\r
375 dbg("UART_RFL = 0x%0x\n", reg_value);
\r
376 reg_value = serial_in(up, UART_LCR);
\r
377 dbg("UART_LCR = 0x%0x\n", reg_value);
\r
385 static void serial_rk_clear_fifos(struct uart_rk_port *up)
\r
387 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
\r
388 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
\r
389 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
\r
390 serial_out(up, UART_FCR, 0);
\r
393 static inline void __stop_tx(struct uart_rk_port *p)
\r
395 if (p->ier & UART_IER_THRI) {
\r
396 p->ier &= ~UART_IER_THRI;
\r
397 serial_out(p, UART_IER, p->ier);
\r
401 static void serial_rk_stop_tx(struct uart_port *port)
\r
403 struct uart_rk_port *up =
\r
404 container_of(port, struct uart_rk_port, port);
\r
406 struct rk_uart_dma *uart_dma = up->dma;
\r
407 if(uart_dma->use_dma & TX_DMA){
\r
408 serial_rk_release_dma_tx(port);
\r
416 static void serial_rk_start_tx(struct uart_port *port)
\r
418 struct uart_rk_port *up =
\r
419 container_of(port, struct uart_rk_port, port);
\r
422 if(up->dma->use_dma & TX_DMA) {
\r
423 if(!up->dma->tx_dma_used)
\r
424 serial_rk_enable_ier_thri(up);
\r
426 serial_rk_enable_ier_thri(up);
\r
429 serial_rk_enable_ier_thri(up);
\r
434 static void serial_rk_stop_rx(struct uart_port *port)
\r
436 struct uart_rk_port *up =
\r
437 container_of(port, struct uart_rk_port, port);
\r
439 struct rk_uart_dma *uart_dma = up->dma;
\r
440 if(uart_dma->use_dma & RX_DMA){
\r
441 serial_rk_release_dma_rx(port);
\r
444 up->ier &= ~UART_IER_RLSI;
\r
445 up->port.read_status_mask &= ~UART_LSR_DR;
\r
446 serial_out(up, UART_IER, up->ier);
\r
450 static void serial_rk_enable_ms(struct uart_port *port)
\r
452 /* no MSR capabilities */
\r
454 struct uart_rk_port *up =
\r
455 container_of(port, struct uart_rk_port, port);
\r
457 dev_dbg(port->dev, "%s\n", __func__);
\r
458 up->ier |= UART_IER_MSI;
\r
459 serial_out(up, UART_IER, up->ier);
\r
466 * Start transmitting by dma.
\r
468 #define DMA_SERIAL_BUFFER_SIZE UART_XMIT_SIZE
\r
470 /* added by hhb@rock-chips.com for uart dma transfer*/
\r
471 static struct rk_uart_dma rk29_uart_ports_dma[] = {
\r
472 {UART0_USE_DMA, DMACH_UART0_RX, DMACH_UART0_TX},
\r
473 {UART1_USE_DMA, DMACH_UART1_RX, DMACH_UART1_TX},
\r
474 {UART2_USE_DMA, DMACH_UART2_RX, DMACH_UART2_TX},
\r
475 {UART3_USE_DMA, DMACH_UART3_RX, DMACH_UART3_TX},
\r
479 /* DMAC PL330 add by hhb@rock-chips.com */
\r
480 static struct rk29_dma_client rk29_uart_dma_client = {
\r
481 .name = "rk-uart-dma",
\r
486 static void serial_rk_release_dma_tx(struct uart_port *port)
\r
488 struct uart_rk_port *up =
\r
489 container_of(port, struct uart_rk_port, port);
\r
490 struct rk_uart_dma *uart_dma = up->dma;
\r
494 if(uart_dma && uart_dma->tx_dma_inited) {
\r
495 rk29_dma_free(uart_dma->tx_dmach, &rk29_uart_dma_client);
\r
496 uart_dma->tx_dma_inited = 0;
\r
497 uart_dma->tx_dma_used = 0;
\r
501 /*this function will be called every time after rk29_dma_enqueue() be invoked*/
\r
502 static void serial_rk_dma_txcb(void *buf, int size, enum rk29_dma_buffresult result) {
\r
503 struct uart_port *port = buf;
\r
504 struct uart_rk_port *up = container_of(port, struct uart_rk_port, port);
\r
505 struct circ_buf *xmit = &port->state->xmit;
\r
507 if(result != RK29_RES_OK){
\r
508 printk(">>>>%s:%d result:%d\n", __func__, __LINE__, result);
\r
509 up->dma->tx_dma_used = 0;
\r
513 //spin_lock(&(up->dma->rx_lock));
\r
514 xmit->tail = (xmit->tail + size) & (UART_XMIT_SIZE - 1);
\r
515 port->icount.tx += size;
\r
516 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
\r
517 uart_write_wakeup(&up->port);
\r
519 //spin_lock(&(up->dma->tx_lock));
\r
520 up->dma->tx_dma_used = 0;
\r
521 //spin_unlock(&(up->dma->tx_lock));
\r
522 serial_rk_enable_ier_thri(up);
\r
523 up->port_activity = jiffies;
\r
524 // dev_info(up->port.dev, "s:%d\n", size);
\r
527 static int serial_rk_init_dma_tx(struct uart_port *port) {
\r
529 struct uart_rk_port *up =
\r
530 container_of(port, struct uart_rk_port, port);
\r
531 struct rk_uart_dma *uart_dma = up->dma;
\r
533 if(!port || !uart_dma){
\r
534 dev_info(up->port.dev, "serial_rk_init_dma_tx fail\n");
\r
538 if(uart_dma->tx_dma_inited) {
\r
542 if (rk29_dma_request(uart_dma->tx_dmach, &rk29_uart_dma_client, NULL) == -EBUSY) {
\r
543 dev_info(up->port.dev, "rk29_dma_request tx fail\n");
\r
547 if (rk29_dma_set_buffdone_fn(uart_dma->tx_dmach, serial_rk_dma_txcb)) {
\r
548 dev_info(up->port.dev, "rk29_dma_set_buffdone_fn tx fail\n");
\r
552 if (rk29_dma_devconfig(uart_dma->tx_dmach, RK29_DMASRC_MEM, (unsigned long)(port->iobase + UART_TX))) {
\r
553 dev_info(up->port.dev, "rk29_dma_devconfig tx fail\n");
\r
557 if (rk29_dma_config(uart_dma->tx_dmach, 1, 16)) {
\r
558 dev_info(up->port.dev, "rk29_dma_config tx fail\n");
\r
562 uart_dma->tx_dma_inited = 1;
\r
563 dev_info(up->port.dev, "serial_rk_init_dma_tx sucess\n");
\r
568 static int serial_rk_start_tx_dma(struct uart_port *port)
\r
571 struct circ_buf *xmit = &port->state->xmit;
\r
572 struct uart_rk_port *up = container_of(port, struct uart_rk_port, port);
\r
573 struct rk_uart_dma *uart_dma = up->dma;
\r
575 if(!uart_dma->use_dma)
\r
578 if(-1 == serial_rk_init_dma_tx(port))
\r
581 if (1 == uart_dma->tx_dma_used)
\r
584 // spin_lock(&(uart_dma->tx_lock));
\r
587 count = SERIAL_CIRC_CNT_TO_END(xmit);
\r
589 if(count >= DMA_TX_TRRIGE_LEVEL) {
\r
590 if (rk29_dma_enqueue(uart_dma->tx_dmach, port, uart_dma->tx_phy_addr + xmit->tail , count)) {
\r
593 rk29_dma_ctrl(uart_dma->tx_dmach, RK29_DMAOP_START);
\r
594 up->dma->tx_dma_used = 1;
\r
596 // spin_unlock(&(uart_dma->tx_lock));
\r
599 dev_info(up->port.dev, "-serial_rk_start_tx_dma-error-\n");
\r
606 static void serial_rk_dma_rxcb(void *buf, int size, enum rk29_dma_buffresult result) {
\r
608 //printk(">>%s:%d\n", __func__, result);
\r
611 static void serial_rk_release_dma_rx(struct uart_port *port)
\r
613 struct uart_rk_port *up =
\r
614 container_of(port, struct uart_rk_port, port);
\r
615 struct rk_uart_dma *uart_dma = up->dma;
\r
621 if(uart_dma && uart_dma->rx_dma_inited) {
\r
622 del_timer(&uart_dma->rx_timer);
\r
623 rk29_dma_free(uart_dma->rx_dmach, &rk29_uart_dma_client);
\r
624 uart_dma->rb_tail = 0;
\r
625 uart_dma->rx_dma_inited = 0;
\r
626 uart_dma->rx_dma_used = 0;
\r
631 static int serial_rk_init_dma_rx(struct uart_port *port) {
\r
633 struct uart_rk_port *up =
\r
634 container_of(port, struct uart_rk_port, port);
\r
635 struct rk_uart_dma *uart_dma = up->dma;
\r
637 if(!port || !uart_dma){
\r
638 dev_info(up->port.dev, "serial_rk_init_dma_rx: port fail\n");
\r
642 if(uart_dma->rx_dma_inited) {
\r
646 if (rk29_dma_request(uart_dma->rx_dmach, &rk29_uart_dma_client, NULL) == -EBUSY) {
\r
647 dev_info(up->port.dev, "rk29_dma_request fail rx \n");
\r
651 if (rk29_dma_set_buffdone_fn(uart_dma->rx_dmach, serial_rk_dma_rxcb)) {
\r
652 dev_info(up->port.dev, "rk29_dma_set_buffdone_fn rx fail\n");
\r
656 if (rk29_dma_devconfig(uart_dma->rx_dmach, RK29_DMASRC_HW, (unsigned long)(port->iobase + UART_RX))) {
\r
657 dev_info(up->port.dev, "rk29_dma_devconfig rx fail\n");
\r
661 if (rk29_dma_config(uart_dma->rx_dmach, 1, 1)) {
\r
662 dev_info(up->port.dev, "rk29_dma_config rx fail 1 1 \n");
\r
666 //rk29_dma_setflags(uart_dma->rx_dmach, RK29_DMAF_CIRCULAR);
\r
668 uart_dma->rx_dma_inited = 1;
\r
669 dev_info(up->port.dev, "serial_rk_init_dma_rx sucess\n");
\r
674 static int serial_rk_start_rx_dma(struct uart_port *port)
\r
676 struct uart_rk_port *up =
\r
677 container_of(port, struct uart_rk_port, port);
\r
678 struct rk_uart_dma *uart_dma = up->dma;
\r
679 if(!uart_dma->use_dma)
\r
682 if(uart_dma->rx_dma_used == 1)
\r
685 if(-1 == serial_rk_init_dma_rx(port)){
\r
686 dev_info(up->port.dev, "*******serial_rk_init_dma_rx*******error*******\n");
\r
691 if (rk29_dma_enqueue(uart_dma->rx_dmach, (void *)up, uart_dma->rx_phy_addr,
\r
692 uart_dma->rb_size/2)) {
\r
693 dev_info(up->port.dev, "*******rk29_dma_enqueue fail*****\n");
\r
697 if (rk29_dma_enqueue(uart_dma->rx_dmach, (void *)up,
\r
698 uart_dma->rx_phy_addr+uart_dma->rb_size/2, uart_dma->rb_size/2)) {
\r
699 dev_info(up->port.dev, "*******rk29_dma_enqueue fail*****\n");
\r
704 rk29_dma_enqueue_ring(uart_dma->rx_dmach, (void *)up, uart_dma->rx_phy_addr, uart_dma->rb_size/4, 4, false);
\r
706 rk29_dma_ctrl(uart_dma->rx_dmach, RK29_DMAOP_START);
\r
707 uart_dma->rx_dma_used = 1;
\r
708 if(uart_dma->use_timer == 1){
\r
709 mod_timer(&uart_dma->rx_timer, jiffies + msecs_to_jiffies(uart_dma->rx_timeout));
\r
711 up->port_activity = jiffies;
\r
715 static void serial_rk_update_rb_addr(struct uart_rk_port *up){
\r
716 dma_addr_t current_pos = 0;
\r
717 dma_addr_t rx_current_pos = 0;
\r
718 struct rk_uart_dma *uart_dma = up->dma;
\r
719 //spin_lock(&(up->dma->rx_lock));
\r
720 uart_dma->rx_size = 0;
\r
721 if(uart_dma->rx_dma_used == 1){
\r
722 rk29_dma_getposition(uart_dma->rx_dmach, ¤t_pos, &rx_current_pos);
\r
723 uart_dma->rb_head = (rx_current_pos - uart_dma->rx_phy_addr);
\r
724 uart_dma->rx_size = CIRC_CNT(uart_dma->rb_head, uart_dma->rb_tail, uart_dma->rb_size);
\r
726 //spin_unlock(&(up->dma->rx_lock));
\r
729 static void serial_rk_report_dma_rx(unsigned long uart)
\r
731 int count, flip = 0;
\r
732 struct uart_rk_port *up = (struct uart_rk_port *)uart;
\r
733 struct rk_uart_dma *uart_dma = up->dma;
\r
735 if(!uart_dma->rx_dma_used || !up->port.state->port.tty)
\r
738 serial_rk_update_rb_addr(up);
\r
741 count = CIRC_CNT_TO_END(uart_dma->rb_head, uart_dma->rb_tail, uart_dma->rb_size);
\r
744 up->port.icount.rx += count;
\r
745 flip = tty_insert_flip_string(up->port.state->port.tty, uart_dma->rx_buffer
\r
746 + uart_dma->rb_tail, count);
\r
747 tty_flip_buffer_push(up->port.state->port.tty);
\r
748 uart_dma->rb_tail = (uart_dma->rb_tail + count) & (uart_dma->rb_size - 1);
\r
749 up->port_activity = jiffies;
\r
752 //if (uart_dma->rx_size > 0)
\r
753 // printk("rx_size:%d ADDR:%x\n", uart_dma->rx_size, uart_dma->rb_head);
\r
755 if(uart_dma->use_timer == 1){
\r
756 mod_timer(&uart_dma->rx_timer, jiffies + msecs_to_jiffies(uart_dma->rx_timeout));
\r
761 static void serial_rk_rx_timeout(unsigned long uart)
\r
763 struct uart_rk_port *up = (struct uart_rk_port *)uart;
\r
765 serial_rk_report_dma_rx(up);
\r
766 //queue_work(up->uart_wq, &up->uart_work);
\r
769 static void serial_rk_report_revdata_workfunc(struct work_struct *work)
\r
771 struct uart_rk_port *up =
\r
772 container_of(work, struct uart_rk_port, uart_work);
\r
774 serial_rk_report_dma_rx((unsigned long)up);
\r
776 //spin_lock(&(up->dma->rx_lock));
\r
778 if(up->port.state->port.tty && up->dma->use_timer != 1 && up->fifo_size > 0){
\r
780 tty_insert_flip_string(up->port.state->port.tty, up->fifo, up->fifo_size);
\r
781 tty_flip_buffer_push(up->port.state->port.tty);
\r
782 up->port.icount.rx += up->fifo_size;
\r
783 up->ier |= UART_IER_RDI;
\r
784 serial_out(up, UART_IER, up->ier);
\r
787 //spin_unlock(&(up->dma->rx_lock));
\r
792 static void serial_rk_start_dma_rx(struct work_struct *work)
\r
794 struct uart_rk_port *up =
\r
795 container_of(work, struct uart_rk_port, uart_work_rx);
\r
797 //mod_timer(&up->dma->rx_timer, jiffies + msecs_to_jiffies(up->dma->rx_timeout));
\r
798 //rk29_dma_ctrl(up->dma->rx_dmach, RK29_DMAOP_START);
\r
799 //serial_rk_start_rx_dma(&up->port);
\r
804 #endif /* USE_DMA */
\r
808 receive_chars(struct uart_rk_port *up, unsigned int *status)
\r
810 struct tty_struct *tty = up->port.state->port.tty;
\r
811 unsigned char ch, lsr = *status;
\r
812 int max_count = 256;
\r
816 if (likely(lsr & UART_LSR_DR)){
\r
817 ch = serial_in(up, UART_RX);
\r
821 * Intel 82571 has a Serial Over Lan device that will
\r
822 * set UART_LSR_BI without setting UART_LSR_DR when
\r
823 * it receives a break. To avoid reading from the
\r
824 * receive buffer without UART_LSR_DR bit set, we
\r
825 * just force the read character to be 0
\r
830 up->port.icount.rx++;
\r
832 lsr |= up->lsr_saved_flags;
\r
833 up->lsr_saved_flags = 0;
\r
835 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
\r
837 * For statistics only
\r
839 if (lsr & UART_LSR_BI) {
\r
840 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
\r
841 up->port.icount.brk++;
\r
843 * We do the SysRQ and SAK checking
\r
844 * here because otherwise the break
\r
845 * may get masked by ignore_status_mask
\r
846 * or read_status_mask.
\r
848 if (uart_handle_break(&up->port))
\r
850 } else if (lsr & UART_LSR_PE)
\r
851 up->port.icount.parity++;
\r
852 else if (lsr & UART_LSR_FE)
\r
853 up->port.icount.frame++;
\r
854 if (lsr & UART_LSR_OE)
\r
855 up->port.icount.overrun++;
\r
859 * Mask off conditions which should be ignored.
\r
861 lsr &= up->port.read_status_mask;
\r
863 if (lsr & UART_LSR_BI) {
\r
864 DEBUG_INTR("handling break....");
\r
866 } else if (lsr & UART_LSR_PE)
\r
868 else if (lsr & UART_LSR_FE)
\r
871 if (uart_handle_sysrq_char(&up->port, ch))
\r
874 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
\r
877 lsr = serial_in(up, UART_LSR);
\r
878 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
\r
879 spin_unlock(&up->port.lock);
\r
880 tty_flip_buffer_push(tty);
\r
881 spin_lock(&up->port.lock);
\r
885 static void transmit_chars(struct uart_rk_port *up)
\r
887 struct circ_buf *xmit = &up->port.state->xmit;
\r
890 if (up->port.x_char) {
\r
891 serial_out(up, UART_TX, up->port.x_char);
\r
892 up->port.icount.tx++;
\r
893 up->port.x_char = 0;
\r
896 if (uart_tx_stopped(&up->port)) {
\r
900 if (uart_circ_empty(xmit)) {
\r
906 if(up->dma->use_dma & TX_DMA){
\r
907 if(SERIAL_CIRC_CNT_TO_END(xmit) >= DMA_TX_TRRIGE_LEVEL){
\r
908 serial_rk_start_tx_dma(&up->port);
\r
913 count = up->port.fifosize - serial_in(up , 0x20);
\r
915 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
\r
916 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
\r
917 up->port.icount.tx++;
\r
918 if (uart_circ_empty(xmit))
\r
920 } while (--count > 0);
\r
922 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
\r
923 uart_write_wakeup(&up->port);
\r
925 DEBUG_INTR("THRE...");
\r
927 up->port_activity = jiffies;
\r
929 if (uart_circ_empty(xmit))
\r
933 static unsigned int check_modem_status(struct uart_rk_port *up)
\r
935 unsigned int status = serial_in(up, UART_MSR);
\r
938 status |= up->msr_saved_flags;
\r
939 up->msr_saved_flags = 0;
\r
940 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
\r
941 up->port.state != NULL) {
\r
942 if (status & UART_MSR_TERI)
\r
943 up->port.icount.rng++;
\r
944 if (status & UART_MSR_DDSR)
\r
945 up->port.icount.dsr++;
\r
946 if (status & UART_MSR_DDCD)
\r
947 uart_handle_dcd_change(&up->port, status & UART_MSR_DCD);
\r
948 if (status & UART_MSR_DCTS)
\r
949 uart_handle_cts_change(&up->port, status & UART_MSR_CTS);
\r
951 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
\r
960 * This handles the interrupt from one port.
\r
962 static void serial_rk_handle_port(struct uart_rk_port *up)
\r
964 unsigned int status;
\r
965 unsigned long flags;
\r
966 spin_lock_irqsave(&up->port.lock, flags);
\r
968 /* reading UART_LSR can automatically clears PE FE OE bits, except receive fifo error bit*/
\r
969 status = serial_in(up, UART_LSR);
\r
971 DEBUG_INTR("status = %x...\n", status);
\r
973 /* DMA mode enable */
\r
974 if(up->dma->use_dma) {
\r
976 if (status & UART_LSR_RFE) {
\r
977 if(up->port.line != DBG_PORT){
\r
978 dev_info(up->port.dev, "error:lsr=0x%x\n", status);
\r
979 status = serial_in(up, UART_LSR);
\r
980 DEBUG_INTR("error:lsr=0x%x\n", status);
\r
984 if (status & 0x02) {
\r
985 if(up->port.line != DBG_PORT){
\r
986 dev_info(up->port.dev, "error:lsr=0x%x\n", status);
\r
987 status = serial_in(up, UART_LSR);
\r
988 DEBUG_INTR("error:lsr=0x%x\n", status);
\r
992 if(!(up->dma->use_dma & RX_DMA)) {
\r
993 if (status & (UART_LSR_DR | UART_LSR_BI)) {
\r
994 receive_chars(up, &status);
\r
998 if ((up->iir & 0x0f) == 0x02) {
\r
999 transmit_chars(up);
\r
1003 { //dma mode disable
\r
1006 * when uart receive a serial of data which doesn't have stop bit and so on, that causes frame error,and
\r
1007 * set UART_LSR_RFE to one,what is worse,we couldn't read the data in the receive fifo. So if
\r
1008 * wo don't clear this bit and reset the receive fifo, the received data available interrupt would
\r
1009 * occur continuously. added by hhb@rock-chips.com 2011-08-05
\r
1012 if (status & UART_LSR_RFE) {
\r
1013 if(up->port.line != DBG_PORT){
\r
1014 dev_info(up->port.dev, "error:lsr=0x%x\n", status);
\r
1015 status = serial_in(up, UART_LSR);
\r
1016 DEBUG_INTR("error:lsr=0x%x\n", status);
\r
1017 rk29_uart_dump_register(up);
\r
1021 if (status & (UART_LSR_DR | UART_LSR_BI)) {
\r
1022 receive_chars(up, &status);
\r
1024 check_modem_status(up);
\r
1025 //hhb@rock-chips.com when FIFO and THRE mode both are enabled,and FIFO TX empty trigger is set to larger than 1,
\r
1026 //,we need to add ((up->iir & 0x0f) == 0x02) to transmit_chars,because when entering interrupt,the FIFO and THR
\r
1028 if ((up->iir & 0x0f) == 0x02) {
\r
1029 transmit_chars(up);
\r
1032 spin_unlock_irqrestore(&up->port.lock, flags);
\r
1036 * This is the serial driver's interrupt routine.
\r
1039 static irqreturn_t serial_rk_interrupt(int irq, void *dev_id)
\r
1041 struct uart_rk_port *up = dev_id;
\r
1045 iir = serial_in(up, UART_IIR);
\r
1047 DEBUG_INTR("%s(%d) iir = 0x%02x\n", __func__, irq, iir);
\r
1050 if (!(iir & UART_IIR_NO_INT)) {
\r
1051 serial_rk_handle_port(up);
\r
1053 } else if ((iir & UART_IIR_BUSY) == UART_IIR_BUSY) {
\r
1055 /* The DesignWare APB UART has an Busy Detect (0x07)
\r
1056 * interrupt meaning an LCR write attempt occured while the
\r
1057 * UART was busy. The interrupt must be cleared by reading
\r
1058 * the UART status register (USR) and the LCR re-written. */
\r
1060 if(!(serial_in(up, UART_USR) & UART_USR_BUSY)){
\r
1061 serial_out(up, UART_LCR, up->lcr);
\r
1064 dbg("the serial.%d is busy\n", up->port.line);
\r
1066 DEBUG_INTR("end(%d).\n", handled);
\r
1068 return IRQ_RETVAL(handled);
\r
1071 static unsigned int serial_rk_tx_empty(struct uart_port *port)
\r
1073 struct uart_rk_port *up =
\r
1074 container_of(port, struct uart_rk_port, port);
\r
1075 unsigned long flags;
\r
1078 dev_dbg(port->dev, "%s\n", __func__);
\r
1079 spin_lock_irqsave(&up->port.lock, flags);
\r
1080 lsr = serial_in(up, UART_LSR);
\r
1081 up->lsr_saved_flags |= lsr & LSR_SAVE_FLAGS;
\r
1082 spin_unlock_irqrestore(&up->port.lock, flags);
\r
1084 return (lsr & UART_LSR_TEMT) == UART_LSR_TEMT ? TIOCSER_TEMT : 0;
\r
1087 static unsigned int serial_rk_get_mctrl(struct uart_port *port)
\r
1089 struct uart_rk_port *up =
\r
1090 container_of(port, struct uart_rk_port, port);
\r
1091 unsigned int status;
\r
1094 status = check_modem_status(up);
\r
1097 if (status & UART_MSR_DCD)
\r
1099 if (status & UART_MSR_RI)
\r
1101 if (status & UART_MSR_DSR)
\r
1103 if (status & UART_MSR_CTS)
\r
1105 dev_dbg(port->dev, "%s 0x%08x\n", __func__, ret);
\r
1109 static void serial_rk_set_mctrl(struct uart_port *port, unsigned int mctrl)
\r
1111 struct uart_rk_port *up =
\r
1112 container_of(port, struct uart_rk_port, port);
\r
1113 unsigned char mcr = 0;
\r
1115 dev_dbg(port->dev, "+%s\n", __func__);
\r
1116 if (mctrl & TIOCM_RTS)
\r
1117 mcr |= UART_MCR_RTS;
\r
1118 if (mctrl & TIOCM_DTR)
\r
1119 mcr |= UART_MCR_DTR;
\r
1120 if (mctrl & TIOCM_OUT1)
\r
1121 mcr |= UART_MCR_OUT1;
\r
1122 if (mctrl & TIOCM_OUT2)
\r
1123 mcr |= UART_MCR_OUT2;
\r
1124 if (mctrl & TIOCM_LOOP)
\r
1125 mcr |= UART_MCR_LOOP;
\r
1129 serial_out(up, UART_MCR, mcr);
\r
1130 dev_dbg(port->dev, "-serial.%d %s mcr: 0x%02x\n", port->line, __func__, mcr);
\r
1133 static void serial_rk_break_ctl(struct uart_port *port, int break_state)
\r
1135 struct uart_rk_port *up =
\r
1136 container_of(port, struct uart_rk_port, port);
\r
1137 unsigned long flags;
\r
1139 dev_dbg(port->dev, "+%s\n", __func__);
\r
1140 spin_lock_irqsave(&up->port.lock, flags);
\r
1141 if (break_state == -1)
\r
1142 up->lcr |= UART_LCR_SBC;
\r
1144 up->lcr &= ~UART_LCR_SBC;
\r
1145 serial_lcr_write(up, up->lcr);
\r
1146 spin_unlock_irqrestore(&up->port.lock, flags);
\r
1147 dev_dbg(port->dev, "-%s lcr: 0x%02x\n", __func__, up->lcr);
\r
1150 #if defined(CONFIG_SERIAL_RK_CONSOLE) || defined(CONFIG_CONSOLE_POLL)
\r
1152 * Wait for transmitter & holding register to empty
\r
1154 static void wait_for_xmitr(struct uart_rk_port *up, int bits)
\r
1156 unsigned int status, tmout = 10000;
\r
1158 /* Wait up to 10ms for the character(s) to be sent. */
\r
1160 status = serial_in(up, UART_LSR);
\r
1162 up->lsr_saved_flags |= status & LSR_SAVE_FLAGS;
\r
1164 if ((status & bits) == bits)
\r
1173 #ifdef CONFIG_CONSOLE_POLL
\r
1175 * Console polling routines for writing and reading from the uart while
\r
1176 * in an interrupt or debug context.
\r
1179 static int serial_rk_get_poll_char(struct uart_port *port)
\r
1181 struct uart_rk_port *up =
\r
1182 container_of(port, struct uart_rk_port, port);
\r
1183 unsigned char lsr = serial_in(up, UART_LSR);
\r
1185 while (!(lsr & UART_LSR_DR))
\r
1186 lsr = serial_in(up, UART_LSR);
\r
1188 return serial_in(up, UART_RX);
\r
1191 static void serial_rk_put_poll_char(struct uart_port *port,
\r
1195 struct uart_rk_port *up =
\r
1196 container_of(port, struct uart_rk_port, port);
\r
1199 * First save the IER then disable the interrupts
\r
1201 ier = serial_in(up, UART_IER);
\r
1202 serial_out(up, UART_IER, 0);
\r
1204 wait_for_xmitr(up, UART_LSR_TEMT);
\r
1206 * Send the character out.
\r
1207 * If a LF, also do CR...
\r
1209 serial_out(up, UART_TX, c);
\r
1211 wait_for_xmitr(up, UART_LSR_TEMT);
\r
1212 serial_out(up, UART_TX, 13);
\r
1216 * Finally, wait for transmitter to become empty
\r
1217 * and restore the IER
\r
1219 wait_for_xmitr(up, UART_LSR_TEMT);
\r
1220 serial_out(up, UART_IER, ier);
\r
1223 #endif /* CONFIG_CONSOLE_POLL */
\r
1225 static int serial_rk_startup(struct uart_port *port)
\r
1227 struct uart_rk_port *up =
\r
1228 container_of(port, struct uart_rk_port, port);
\r
1229 unsigned long flags;
\r
1230 int retval, fifosize = 0;
\r
1233 dev_dbg(port->dev, "%s\n", __func__);
\r
1236 * Allocate the IRQ
\r
1238 retval = request_irq(up->port.irq, serial_rk_interrupt, up->port.irqflags,
\r
1245 clk_enable(up->pclk);
\r
1246 clk_enable(up->clk); // enable the config uart clock
\r
1249 * Clear the FIFO buffers and disable them.
\r
1250 * (they will be reenabled in set_termios())
\r
1252 serial_rk_clear_fifos(up);
\r
1254 //read uart fifo size hhb@rock-chips.com
\r
1255 fifosize = __raw_readl(up->port.membase + 0xf4);
\r
1256 up->port.fifosize = ((fifosize >> 16) & 0xff) << 4;
\r
1257 if(up->port.fifosize <= 0)
\r
1258 up->port.fifosize = 32;
\r
1259 //printk("fifo size:%d :%08x\n", up->port.fifosize, fifosize);
\r
1262 * Clear the interrupt registers.
\r
1264 (void) serial_in(up, UART_LSR);
\r
1265 (void) serial_in(up, UART_RX);
\r
1266 (void) serial_in(up, UART_IIR);
\r
1267 (void) serial_in(up, UART_MSR);
\r
1268 (void) serial_in(up, UART_USR);
\r
1271 * Now, initialize the UART
\r
1273 serial_lcr_write(up, UART_LCR_WLEN8 | UART_LCR_EPAR);
\r
1275 spin_lock_irqsave(&up->port.lock, flags);
\r
1278 * Most PC uarts need OUT2 raised to enable interrupts.
\r
1280 // up->port.mctrl |= TIOCM_OUT2;
\r
1282 serial_rk_set_mctrl(&up->port, up->port.mctrl);
\r
1284 spin_unlock_irqrestore(&up->port.lock, flags);
\r
1287 * Clear the interrupt registers again for luck, and clear the
\r
1288 * saved flags to avoid getting false values from polling
\r
1289 * routines or the previous session.
\r
1291 (void) serial_in(up, UART_LSR);
\r
1292 (void) serial_in(up, UART_RX);
\r
1293 (void) serial_in(up, UART_IIR);
\r
1294 (void) serial_in(up, UART_MSR);
\r
1295 (void) serial_in(up, UART_USR);
\r
1296 up->lsr_saved_flags = 0;
\r
1298 up->msr_saved_flags = 0;
\r
1301 if (up->dma->use_dma & TX_DMA) {
\r
1302 if(up->port.state->xmit.buf != up->dma->tx_buffer){
\r
1303 free_page((unsigned long)up->port.state->xmit.buf);
\r
1304 up->port.state->xmit.buf = up->dma->tx_buffer;
\r
1310 serial_out(up, UART_IER, up->ier);
\r
1314 * Finally, enable interrupts. Note: Modem status interrupts
\r
1315 * are set via set_termios(), which will be occurring imminently
\r
1316 * anyway, so we don't enable them here.
\r
1323 static void serial_rk_shutdown(struct uart_port *port)
\r
1325 struct uart_rk_port *up =
\r
1326 container_of(port, struct uart_rk_port, port);
\r
1327 unsigned long flags;
\r
1329 dev_dbg(port->dev, "%s\n", __func__);
\r
1331 * Disable interrupts from this port
\r
1334 serial_out(up, UART_IER, 0);
\r
1336 spin_lock_irqsave(&up->port.lock, flags);
\r
1337 // up->port.mctrl &= ~TIOCM_OUT2;
\r
1338 serial_rk_set_mctrl(&up->port, up->port.mctrl);
\r
1339 spin_unlock_irqrestore(&up->port.lock, flags);
\r
1342 * Disable break condition and FIFOs
\r
1344 serial_lcr_write(up, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
\r
1345 serial_rk_clear_fifos(up);
\r
1348 * Read data port to reset things, and then free the irq
\r
1350 (void) serial_in(up, UART_RX);
\r
1352 //if (up->dma->use_dma & TX_DMA)
\r
1353 // up->port.state->xmit.buf = NULL;
\r
1355 free_irq(up->port.irq, up);
\r
1356 clk_disable(up->clk);
\r
1357 clk_disable(up->pclk);
\r
1361 serial_rk_set_termios(struct uart_port *port, struct ktermios *termios,
\r
1362 struct ktermios *old)
\r
1364 struct uart_rk_port *up =
\r
1365 container_of(port, struct uart_rk_port, port);
\r
1366 unsigned char cval = 0, fcr = 0, mcr = 0;
\r
1367 unsigned long flags;
\r
1368 unsigned int baud, quot;
\r
1369 dev_dbg(port->dev, "+%s\n", __func__);
\r
1371 switch (termios->c_cflag & CSIZE) {
\r
1373 cval = UART_LCR_WLEN5;
\r
1376 cval = UART_LCR_WLEN6;
\r
1379 cval = UART_LCR_WLEN7;
\r
1383 cval = UART_LCR_WLEN8;
\r
1387 if (termios->c_cflag & CSTOPB){
\r
1388 cval |= UART_LCR_STOP;
\r
1390 if (termios->c_cflag & PARENB){
\r
1391 cval |= UART_LCR_PARITY;
\r
1393 if (!(termios->c_cflag & PARODD)){
\r
1394 cval |= UART_LCR_EPAR;
\r
1397 if (termios->c_cflag & CMSPAR)
\r
1398 cval |= UART_LCR_SPAR;
\r
1403 * Ask the core to calculate the divisor for us.
\r
1405 baud = uart_get_baud_rate(port, termios, old,
\r
1406 port->uartclk / 16 / 0xffff,
\r
1407 port->uartclk / 16);
\r
1409 quot = uart_get_divisor(port, baud);
\r
1410 //dev_info(up->port.dev, "uartclk:%d\n", port->uartclk/16);
\r
1411 //dev_info(up->port.dev, "baud:%d\n", baud);
\r
1412 //dev_info(up->port.dev, "quot:%d\n", quot);
\r
1415 fcr = UART_FCR_ENABLE_FIFO | UART_FCR_TRIGGER_1;
\r
1418 fcr = UART_FCR_ENABLE_FIFO;
\r
1420 //added by hhb@rock-chips.com
\r
1421 if(up->dma->use_dma & TX_DMA){
\r
1422 fcr |= UART_FCR_T_TRIG_01;
\r
1426 fcr |= UART_FCR_T_TRIG_01;
\r
1430 //added by hhb@rock-chips.com
\r
1431 if(up->dma->use_dma & RX_DMA){
\r
1432 fcr |= UART_FCR_R_TRIG_00;
\r
1436 if (termios->c_cflag & CRTSCTS)
\r
1437 fcr |= UART_FCR_R_TRIG_11;
\r
1439 fcr |= UART_FCR_R_TRIG_00;
\r
1444 * MCR-based auto flow control. When AFE is enabled, RTS will be
\r
1445 * deasserted when the receive FIFO contains more characters than
\r
1446 * the trigger, or the MCR RTS bit is cleared. In the case where
\r
1447 * the remote UART is not using CTS auto flow control, we must
\r
1448 * have sufficient FIFO entries for the latency of the remote
\r
1449 * UART to respond. IOW, at least 32 bytes of FIFO.
\r
1451 up->mcr &= ~UART_MCR_AFE;
\r
1452 if (termios->c_cflag & CRTSCTS){
\r
1453 up->mcr |= UART_MCR_AFE;
\r
1457 * Ok, we're now changing the port state. Do it with
\r
1458 * interrupts disabled.
\r
1460 spin_lock_irqsave(&up->port.lock, flags);
\r
1463 * Update the per-port timeout.
\r
1465 uart_update_timeout(port, termios->c_cflag, baud);
\r
1467 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
\r
1468 if (termios->c_iflag & INPCK)
\r
1469 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
\r
1470 if (termios->c_iflag & (BRKINT | PARMRK))
\r
1471 up->port.read_status_mask |= UART_LSR_BI;
\r
1474 * Characteres to ignore
\r
1476 up->port.ignore_status_mask = 0;
\r
1477 if (termios->c_iflag & IGNPAR)
\r
1478 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
\r
1479 if (termios->c_iflag & IGNBRK) {
\r
1480 up->port.ignore_status_mask |= UART_LSR_BI;
\r
1482 * If we're ignoring parity and break indicators,
\r
1483 * ignore overruns too (for real raw support).
\r
1485 if (termios->c_iflag & IGNPAR)
\r
1486 up->port.ignore_status_mask |= UART_LSR_OE;
\r
1490 * ignore all characters if CREAD is not set
\r
1492 if ((termios->c_cflag & CREAD) == 0)
\r
1493 up->port.ignore_status_mask |= UART_LSR_DR;
\r
1496 * CTS flow control flag and modem status interrupts
\r
1498 up->ier &= ~UART_IER_MSI;
\r
1500 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
\r
1501 up->ier |= UART_IER_MSI;
\r
1504 //to avoid uart busy when set baud rate hhb@rock-chips.com
\r
1505 serial_out(up, UART_SRR, UART_RESET);
\r
1506 mcr = serial_in(up, UART_MCR);
\r
1507 serial_out(up, UART_MCR, mcr | 0x10); //loopback mode
\r
1509 up->lcr = cval; /* Save LCR */
\r
1511 if(serial_lcr_write(up, cval | UART_LCR_DLAB)) {
\r
1512 if(up->port.line != DBG_PORT)
\r
1513 dbg("serial.%d set DLAB fail\n", up->port.line);
\r
1514 serial_out(up, UART_SRR, UART_RESET);
\r
1518 /* set uart baud rate */
\r
1519 if(serial_dl_write(up, quot)) {
\r
1520 if(up->port.line != DBG_PORT)
\r
1521 dbg("serial.%d set dll fail\n", up->port.line);
\r
1522 serial_out(up, UART_SRR, UART_RESET);
\r
1527 if(serial_lcr_write(up, cval)) {
\r
1528 if(up->port.line != DBG_PORT)
\r
1529 dbg("serial.%d reset DLAB fail\n", up->port.line);
\r
1530 serial_out(up, UART_SRR, UART_RESET);
\r
1534 serial_rk_set_mctrl(&up->port, up->port.mctrl);
\r
1536 serial_out(up, UART_FCR, up->fcr); /* set fcr */
\r
1538 //start serial receive data
\r
1540 if (up->dma->use_dma) {
\r
1541 up->ier |= UART_IER_RLSI;
\r
1542 up->ier |= UART_IER_PTIME; //Programmable THRE Interrupt Mode Enable
\r
1543 if (up->dma->use_dma & RX_DMA)
\r
1544 serial_rk_start_rx_dma(&up->port);
\r
1546 up->ier |= UART_IER_RDI;
\r
1550 // not use dma receive
\r
1551 up->ier |= UART_IER_RDI;
\r
1552 up->ier |= UART_IER_RLSI;
\r
1553 if(up->port.line != DBG_PORT)
\r
1554 up->ier |= UART_IER_PTIME; //Programmable THRE Interrupt Mode Enable
\r
1557 serial_out(up, UART_IER, up->ier);
\r
1560 spin_unlock_irqrestore(&up->port.lock, flags);
\r
1562 /* Don't rewrite B0 */
\r
1563 if (tty_termios_baud_rate(termios))
\r
1564 tty_termios_encode_baud_rate(termios, baud, baud);
\r
1565 dev_dbg(port->dev, "-%s baud %d\n", __func__, baud);
\r
1569 spin_unlock_irqrestore(&up->port.lock, flags);
\r
1575 serial_rk_set_ldisc(struct uart_port *port, int new)
\r
1577 if (new == N_PPS) {
\r
1578 port->flags |= UPF_HARDPPS_CD;
\r
1579 serial_rk_enable_ms(port);
\r
1581 port->flags &= ~UPF_HARDPPS_CD;
\r
1586 serial_rk_pm(struct uart_port *port, unsigned int state,
\r
1587 unsigned int oldstate)
\r
1589 struct uart_rk_port *up =
\r
1590 container_of(port, struct uart_rk_port, port);
\r
1592 dev_dbg(port->dev, "%s: %s\n", __func__, state ? "disable" : "enable");
\r
1594 clk_disable(up->clk);
\r
1595 clk_disable(up->pclk);
\r
1597 clk_enable(up->pclk);
\r
1598 clk_enable(up->clk);
\r
1602 static void serial_rk_release_port(struct uart_port *port)
\r
1604 dev_dbg(port->dev, "%s\n", __func__);
\r
1607 static int serial_rk_request_port(struct uart_port *port)
\r
1609 dev_dbg(port->dev, "%s\n", __func__);
\r
1613 static void serial_rk_config_port(struct uart_port *port, int flags)
\r
1615 dev_dbg(port->dev, "%s\n", __func__);
\r
1616 port->type = PORT_RK;
\r
1620 serial_rk_verify_port(struct uart_port *port, struct serial_struct *ser)
\r
1622 /* we don't want the core code to modify any port params */
\r
1623 dev_dbg(port->dev, "%s\n", __func__);
\r
1627 static const char *
\r
1628 serial_rk_type(struct uart_port *port)
\r
1630 struct uart_rk_port *up =
\r
1631 container_of(port, struct uart_rk_port, port);
\r
1633 dev_dbg(port->dev, "%s: %s\n", __func__, up->name);
\r
1637 static struct uart_ops serial_rk_pops = {
\r
1638 .tx_empty = serial_rk_tx_empty,
\r
1639 .set_mctrl = serial_rk_set_mctrl,
\r
1640 .get_mctrl = serial_rk_get_mctrl,
\r
1641 .stop_tx = serial_rk_stop_tx,
\r
1642 .start_tx = serial_rk_start_tx,
\r
1643 .stop_rx = serial_rk_stop_rx,
\r
1644 .enable_ms = serial_rk_enable_ms,
\r
1645 .break_ctl = serial_rk_break_ctl,
\r
1646 .startup = serial_rk_startup,
\r
1647 .shutdown = serial_rk_shutdown,
\r
1648 .set_termios = serial_rk_set_termios,
\r
1650 .set_ldisc = serial_rk_set_ldisc,
\r
1652 .pm = serial_rk_pm,
\r
1653 .type = serial_rk_type,
\r
1654 .release_port = serial_rk_release_port,
\r
1655 .request_port = serial_rk_request_port,
\r
1656 .config_port = serial_rk_config_port,
\r
1657 .verify_port = serial_rk_verify_port,
\r
1658 #ifdef CONFIG_CONSOLE_POLL
\r
1659 .poll_get_char = serial_rk_get_poll_char,
\r
1660 .poll_put_char = serial_rk_put_poll_char,
\r
1664 #ifdef CONFIG_SERIAL_RK_CONSOLE
\r
1666 static struct uart_rk_port *serial_rk_console_ports[UART_NR];
\r
1668 static void serial_rk_console_putchar(struct uart_port *port, int ch)
\r
1670 struct uart_rk_port *up =
\r
1671 container_of(port, struct uart_rk_port, port);
\r
1673 wait_for_xmitr(up, UART_LSR_THRE);
\r
1674 serial_out(up, UART_TX, ch);
\r
1678 * Print a string to the serial port trying not to disturb
\r
1679 * any possible real use of the port...
\r
1681 * The console_lock must be held when we get here.
\r
1684 serial_rk_console_write(struct console *co, const char *s, unsigned int count)
\r
1686 struct uart_rk_port *up = serial_rk_console_ports[co->index];
\r
1687 unsigned long flags;
\r
1691 touch_nmi_watchdog();
\r
1693 local_irq_save(flags);
\r
1694 if (up->port.sysrq) {
\r
1695 /* serial_rk_handle_port() already took the lock */
\r
1697 } else if (oops_in_progress) {
\r
1698 locked = spin_trylock(&up->port.lock);
\r
1700 spin_lock(&up->port.lock);
\r
1703 * First save the IER then disable the interrupts
\r
1705 ier = serial_in(up, UART_IER);
\r
1707 serial_out(up, UART_IER, 0);
\r
1709 uart_console_write(&up->port, s, count, serial_rk_console_putchar);
\r
1712 * Finally, wait for transmitter to become empty
\r
1713 * and restore the IER
\r
1715 wait_for_xmitr(up, UART_LSR_TEMT);
\r
1716 serial_out(up, UART_IER, ier);
\r
1720 * The receive handling will happen properly because the
\r
1721 * receive ready bit will still be set; it is not cleared
\r
1722 * on read. However, modem control will not, we must
\r
1723 * call it if we have saved something in the saved flags
\r
1724 * while processing with interrupts off.
\r
1726 if (up->msr_saved_flags)
\r
1727 check_modem_status(up);
\r
1731 spin_unlock(&up->port.lock);
\r
1732 local_irq_restore(flags);
\r
1735 static int __init serial_rk_console_setup(struct console *co, char *options)
\r
1737 struct uart_rk_port *up;
\r
1738 int baud = 115200;
\r
1743 if (unlikely(co->index >= UART_NR || co->index < 0))
\r
1746 if (serial_rk_console_ports[co->index] == NULL)
\r
1748 up = serial_rk_console_ports[co->index];
\r
1751 uart_parse_options(options, &baud, &parity, &bits, &flow);
\r
1753 return uart_set_options(&up->port, co, baud, parity, bits, flow);
\r
1756 static struct console serial_rk_console = {
\r
1758 .write = serial_rk_console_write,
\r
1759 .device = uart_console_device,
\r
1760 .setup = serial_rk_console_setup,
\r
1761 .flags = CON_PRINTBUFFER | CON_ANYTIME,
\r
1763 .data = &serial_rk_reg,
\r
1766 static void serial_rk_add_console_port(struct uart_rk_port *up)
\r
1768 serial_rk_console_ports[up->pdev->id] = up;
\r
1771 #define SERIAL_CONSOLE &serial_rk_console
\r
1773 #define SERIAL_CONSOLE NULL
\r
1775 static inline void serial_rk_add_console_port(struct uart_rk_port *up)
\r
1780 static struct uart_driver serial_rk_reg = {
\r
1781 .owner = THIS_MODULE,
\r
1782 .driver_name = "rk29_serial",
\r
1783 .dev_name = "ttyS",
\r
1784 .major = TTY_MAJOR,
\r
1786 .cons = SERIAL_CONSOLE,
\r
1790 static int __devinit serial_rk_probe(struct platform_device *pdev)
\r
1792 struct uart_rk_port *up;
\r
1793 struct resource *mem;
\r
1795 int ret = -ENOSPC;
\r
1797 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
\r
1799 dev_err(&pdev->dev, "no mem resource?\n");
\r
1803 irq = platform_get_irq(pdev, 0);
\r
1805 dev_err(&pdev->dev, "no irq resource?\n");
\r
1809 if (!request_mem_region(mem->start, (mem->end - mem->start) + 1,
\r
1810 pdev->dev.driver->name)) {
\r
1811 dev_err(&pdev->dev, "memory region already claimed\n");
\r
1815 up = kzalloc(sizeof(*up), GFP_KERNEL);
\r
1818 goto do_release_region;
\r
1821 sprintf(up->name, "rk29_serial.%d", pdev->id);
\r
1823 up->pclk = clk_get(&pdev->dev, "pclk_uart");
\r
1824 up->clk = clk_get(&pdev->dev, "uart");
\r
1825 if (unlikely(IS_ERR(up->clk))) {
\r
1826 ret = PTR_ERR(up->clk);
\r
1829 up->tx_loadsz = 30;
\r
1831 up->dma = &rk29_uart_ports_dma[pdev->id];
\r
1833 up->port.dev = &pdev->dev;
\r
1834 up->port.type = PORT_RK;
\r
1835 up->port.irq = irq;
\r
1836 up->port.iotype = UPIO_DWAPB;
\r
1838 up->port.regshift = 2;
\r
1839 //fifo size default is 32, but it will be updated later when start_up
\r
1840 up->port.fifosize = 32;
\r
1841 up->port.ops = &serial_rk_pops;
\r
1842 up->port.line = pdev->id;
\r
1843 up->port.iobase = mem->start;
\r
1844 up->port.membase = ioremap_nocache(mem->start, mem->end - mem->start + 1);
\r
1845 if (!up->port.membase) {
\r
1849 up->port.mapbase = mem->start;
\r
1850 up->port.irqflags = IRQF_DISABLED;
\r
1851 up->port.uartclk = clk_get_rate(up->clk);
\r
1854 /* set dma config */
\r
1855 pdev->dev.coherent_dma_mask = DMA_BIT_MASK(32);
\r
1856 if(up->dma->use_dma & RX_DMA) {
\r
1858 up->dma->use_timer = USE_TIMER;
\r
1859 up->dma->rx_timer.function = serial_rk_report_dma_rx;
\r
1860 up->dma->rx_timer.data = (unsigned long)up;
\r
1861 up->dma->rx_timeout = 10;
\r
1862 up->dma->rx_timer.expires = jiffies + msecs_to_jiffies(up->dma->rx_timeout);
\r
1863 init_timer(&up->dma->rx_timer);
\r
1866 up->dma->rb_size = UART_XMIT_SIZE*2;
\r
1867 up->dma->rx_buffer = dmam_alloc_coherent(up->port.dev, up->dma->rb_size,
\r
1868 &up->dma->rx_phy_addr, DMA_MEMORY_MAP);
\r
1869 up->dma->rb_tail = 0;
\r
1871 if(!up->dma->rx_buffer){
\r
1872 dev_info(up->port.dev, "dmam_alloc_coherent dma_rx_buffer fail\n");
\r
1875 dev_info(up->port.dev, "dma_rx_buffer 0x%08x\n", (unsigned) up->dma->rx_buffer);
\r
1876 dev_info(up->port.dev, "dma_rx_phy 0x%08x\n", (unsigned)up->dma->rx_phy_addr);
\r
1880 //INIT_WORK(&up->uart_work, serial_rk_report_revdata_workfunc);
\r
1881 //INIT_WORK(&up->uart_work_rx, serial_rk_start_dma_rx);
\r
1882 //up->uart_wq = create_singlethread_workqueue("uart_workqueue");
\r
1883 up->dma->rx_dma_used = 0;
\r
1884 spin_lock_init(&(up->dma->rx_lock));
\r
1885 serial_rk_init_dma_rx(&up->port);
\r
1888 if(up->dma->use_dma & TX_DMA){
\r
1890 up->dma->tb_size = UART_XMIT_SIZE;
\r
1891 up->dma->tx_buffer = dmam_alloc_coherent(up->port.dev, up->dma->tb_size,
\r
1892 &up->dma->tx_phy_addr, DMA_MEMORY_MAP);
\r
1893 if(!up->dma->tx_buffer){
\r
1894 dev_info(up->port.dev, "dmam_alloc_coherent dma_tx_buffer fail\n");
\r
1897 dev_info(up->port.dev, "dma_tx_buffer 0x%08x\n", (unsigned) up->dma->tx_buffer);
\r
1898 dev_info(up->port.dev, "dma_tx_phy 0x%08x\n", (unsigned) up->dma->tx_phy_addr);
\r
1900 spin_lock_init(&(up->dma->tx_lock));
\r
1901 serial_rk_init_dma_tx(&up->port);
\r
1905 serial_rk_add_console_port(up);
\r
1906 ret = uart_add_one_port(&serial_rk_reg, &up->port);
\r
1910 platform_set_drvdata(pdev, up);
\r
1911 dev_info(&pdev->dev, "membase 0x%08x\n", (unsigned) up->port.membase);
\r
1916 iounmap(up->port.membase);
\r
1917 up->port.membase = NULL;
\r
1920 clk_put(up->pclk);
\r
1923 do_release_region:
\r
1924 release_mem_region(mem->start, (mem->end - mem->start) + 1);
\r
1928 static int __devexit serial_rk_remove(struct platform_device *pdev)
\r
1930 struct uart_rk_port *up = platform_get_drvdata(pdev);
\r
1932 platform_set_drvdata(pdev, NULL);
\r
1934 struct resource *mem;
\r
1935 destroy_workqueue(up->uart_wq);
\r
1936 uart_remove_one_port(&serial_rk_reg, &up->port);
\r
1937 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
\r
1938 iounmap(up->port.membase);
\r
1939 up->port.membase = NULL;
\r
1941 clk_put(up->pclk);
\r
1943 release_mem_region(mem->start, (mem->end - mem->start) + 1);
\r
1949 static int serial_rk_suspend(struct platform_device *dev, pm_message_t state)
\r
1951 struct uart_rk_port *up = platform_get_drvdata(dev);
\r
1953 if (up && up->port.line != DBG_PORT && POWER_MANEGEMENT){
\r
1954 uart_suspend_port(&serial_rk_reg, &up->port);
\r
1956 if(up->port.line == DBG_PORT && POWER_MANEGEMENT){
\r
1957 serial_rk_pm(&up->port, 1, 0);
\r
1963 static int serial_rk_resume(struct platform_device *dev)
\r
1965 struct uart_rk_port *up = platform_get_drvdata(dev);
\r
1967 if (up && up->port.line != DBG_PORT && POWER_MANEGEMENT){
\r
1968 uart_resume_port(&serial_rk_reg, &up->port);
\r
1970 if(up->port.line == DBG_PORT && POWER_MANEGEMENT){
\r
1971 serial_rk_pm(&up->port, 0, 1);
\r
1976 static struct platform_driver serial_rk_driver = {
\r
1977 .probe = serial_rk_probe,
\r
1978 .remove = __devexit_p(serial_rk_remove),
\r
1979 .suspend = serial_rk_suspend,
\r
1980 .resume = serial_rk_resume,
\r
1982 #if defined(CONFIG_ARCH_RK29)
\r
1983 .name = "rk29_serial",
\r
1984 #elif defined(CONFIG_SERIAL_RK2818)
\r
1985 .name = "rk2818_serial",
\r
1987 .name = "rk_serial",
\r
1989 .owner = THIS_MODULE,
\r
1993 static int __init serial_rk_init(void)
\r
1996 //hhb@rock-chips.com
\r
1997 printk("%s\n", VERSION_AND_TIME);
\r
1999 ret = uart_register_driver(&serial_rk_reg);
\r
2003 ret = platform_driver_register(&serial_rk_driver);
\r
2005 uart_unregister_driver(&serial_rk_reg);
\r
2010 static void __exit serial_rk_exit(void)
\r
2012 platform_driver_unregister(&serial_rk_driver);
\r
2013 uart_unregister_driver(&serial_rk_reg);
\r
2016 module_init(serial_rk_init);
\r
2017 module_exit(serial_rk_exit);
\r
2019 MODULE_LICENSE("GPL");
\r
2020 MODULE_DESCRIPTION("RK UART driver");
\r