2 * Driver for OMAP-UART controller.
3 * Based on drivers/serial/8250.c
5 * Copyright (C) 2010 Texas Instruments.
8 * Govindraj R <govindraj.raja@ti.com>
9 * Thara Gopinath <thara@ti.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License as published by
13 * the Free Software Foundation; either version 2 of the License, or
14 * (at your option) any later version.
16 * Note: This driver is made separate from 8250 driver as we cannot
17 * over load 8250 driver with omap platform specific configuration for
18 * features like DMA, it makes easier to implement features like DMA and
19 * hardware flow control and software flow control configuration with
20 * this driver as required for the omap-platform.
23 #if defined(CONFIG_SERIAL_OMAP_CONSOLE) && defined(CONFIG_MAGIC_SYSRQ)
27 #include <linux/module.h>
28 #include <linux/init.h>
29 #include <linux/console.h>
30 #include <linux/serial_reg.h>
31 #include <linux/delay.h>
32 #include <linux/slab.h>
33 #include <linux/tty.h>
34 #include <linux/tty_flip.h>
36 #include <linux/dma-mapping.h>
37 #include <linux/clk.h>
38 #include <linux/serial_core.h>
39 #include <linux/irq.h>
42 #include <plat/dmtimer.h>
43 #include <plat/omap-serial.h>
45 /* SCR register bitmasks */
46 #define OMAP_UART_SCR_RX_TRIG_GRANU1_MASK (1 << 7)
48 /* FCR register bitmasks */
49 #define OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT 6
50 #define OMAP_UART_FCR_RX_FIFO_TRIG_MASK (0x3 << 6)
52 static struct uart_omap_port *ui[OMAP_MAX_HSUART_PORTS];
54 /* Forward declaration of functions */
55 static void uart_tx_dma_callback(int lch, u16 ch_status, void *data);
56 static void serial_omap_rx_timeout(unsigned long uart_no);
57 static int serial_omap_start_rxdma(struct uart_omap_port *up);
59 static inline unsigned int serial_in(struct uart_omap_port *up, int offset)
61 offset <<= up->port.regshift;
62 return readw(up->port.membase + offset);
65 static inline void serial_out(struct uart_omap_port *up, int offset, int value)
67 offset <<= up->port.regshift;
68 writew(value, up->port.membase + offset);
71 static inline void serial_omap_clear_fifos(struct uart_omap_port *up)
73 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO);
74 serial_out(up, UART_FCR, UART_FCR_ENABLE_FIFO |
75 UART_FCR_CLEAR_RCVR | UART_FCR_CLEAR_XMIT);
76 serial_out(up, UART_FCR, 0);
80 * serial_omap_get_divisor - calculate divisor value
81 * @port: uart port info
82 * @baud: baudrate for which divisor needs to be calculated.
84 * We have written our own function to get the divisor so as to support
85 * 13x mode. 3Mbps Baudrate as an different divisor.
86 * Reference OMAP TRM Chapter 17:
87 * Table 17-1. UART Mode Baud Rates, Divisor Values, and Error Rates
88 * referring to oversampling - divisor value
89 * baudrate 460,800 to 3,686,400 all have divisor 13
90 * except 3,000,000 which has divisor value 16
93 serial_omap_get_divisor(struct uart_port *port, unsigned int baud)
97 if (baud > OMAP_MODE13X_SPEED && baud != 3000000)
101 return port->uartclk/(baud * divisor);
104 static void serial_omap_stop_rxdma(struct uart_omap_port *up)
106 if (up->uart_dma.rx_dma_used) {
107 del_timer(&up->uart_dma.rx_timer);
108 omap_stop_dma(up->uart_dma.rx_dma_channel);
109 omap_free_dma(up->uart_dma.rx_dma_channel);
110 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
111 up->uart_dma.rx_dma_used = false;
115 static void serial_omap_enable_ms(struct uart_port *port)
117 struct uart_omap_port *up = (struct uart_omap_port *)port;
119 dev_dbg(up->port.dev, "serial_omap_enable_ms+%d\n", up->pdev->id);
120 up->ier |= UART_IER_MSI;
121 serial_out(up, UART_IER, up->ier);
124 static void serial_omap_stop_tx(struct uart_port *port)
126 struct uart_omap_port *up = (struct uart_omap_port *)port;
129 up->uart_dma.tx_dma_channel != OMAP_UART_DMA_CH_FREE) {
131 * Check if dma is still active. If yes do nothing,
132 * return. Else stop dma
134 if (omap_get_dma_active_status(up->uart_dma.tx_dma_channel))
136 omap_stop_dma(up->uart_dma.tx_dma_channel);
137 omap_free_dma(up->uart_dma.tx_dma_channel);
138 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
141 if (up->ier & UART_IER_THRI) {
142 up->ier &= ~UART_IER_THRI;
143 serial_out(up, UART_IER, up->ier);
147 static void serial_omap_stop_rx(struct uart_port *port)
149 struct uart_omap_port *up = (struct uart_omap_port *)port;
152 serial_omap_stop_rxdma(up);
153 up->ier &= ~UART_IER_RLSI;
154 up->port.read_status_mask &= ~UART_LSR_DR;
155 serial_out(up, UART_IER, up->ier);
158 static inline void receive_chars(struct uart_omap_port *up, int *status)
160 struct tty_struct *tty = up->port.state->port.tty;
162 unsigned char ch, lsr = *status;
166 if (likely(lsr & UART_LSR_DR))
167 ch = serial_in(up, UART_RX);
169 up->port.icount.rx++;
171 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
173 * For statistics only
175 if (lsr & UART_LSR_BI) {
176 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
177 up->port.icount.brk++;
179 * We do the SysRQ and SAK checking
180 * here because otherwise the break
181 * may get masked by ignore_status_mask
182 * or read_status_mask.
184 if (uart_handle_break(&up->port))
186 } else if (lsr & UART_LSR_PE) {
187 up->port.icount.parity++;
188 } else if (lsr & UART_LSR_FE) {
189 up->port.icount.frame++;
192 if (lsr & UART_LSR_OE)
193 up->port.icount.overrun++;
196 * Mask off conditions which should be ignored.
198 lsr &= up->port.read_status_mask;
200 #ifdef CONFIG_SERIAL_OMAP_CONSOLE
201 if (up->port.line == up->port.cons->index) {
202 /* Recover the break flag from console xmit */
203 lsr |= up->lsr_break_flag;
206 if (lsr & UART_LSR_BI)
208 else if (lsr & UART_LSR_PE)
210 else if (lsr & UART_LSR_FE)
214 if (uart_handle_sysrq_char(&up->port, ch))
216 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
218 lsr = serial_in(up, UART_LSR);
219 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
220 spin_unlock(&up->port.lock);
221 tty_flip_buffer_push(tty);
222 spin_lock(&up->port.lock);
225 static void transmit_chars(struct uart_omap_port *up)
227 struct circ_buf *xmit = &up->port.state->xmit;
230 if (up->port.x_char) {
231 serial_out(up, UART_TX, up->port.x_char);
232 up->port.icount.tx++;
236 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
237 serial_omap_stop_tx(&up->port);
240 count = up->port.fifosize / 4;
242 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
243 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
244 up->port.icount.tx++;
245 if (uart_circ_empty(xmit))
247 } while (--count > 0);
249 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
250 uart_write_wakeup(&up->port);
252 if (uart_circ_empty(xmit))
253 serial_omap_stop_tx(&up->port);
256 static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
258 if (!(up->ier & UART_IER_THRI)) {
259 up->ier |= UART_IER_THRI;
260 serial_out(up, UART_IER, up->ier);
264 static void serial_omap_start_tx(struct uart_port *port)
266 struct uart_omap_port *up = (struct uart_omap_port *)port;
267 struct circ_buf *xmit;
272 omap_uart_block_sleep_id(up->pdev->id);
273 serial_omap_enable_ier_thri(up);
277 if (up->uart_dma.tx_dma_used)
280 xmit = &up->port.state->xmit;
282 if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
283 ret = omap_request_dma(up->uart_dma.uart_dma_tx,
285 (void *)uart_tx_dma_callback, up,
286 &(up->uart_dma.tx_dma_channel));
289 serial_omap_enable_ier_thri(up);
293 spin_lock(&(up->uart_dma.tx_lock));
294 up->uart_dma.tx_dma_used = true;
295 spin_unlock(&(up->uart_dma.tx_lock));
297 start = up->uart_dma.tx_buf_dma_phys +
298 (xmit->tail & (UART_XMIT_SIZE - 1));
300 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
302 * It is a circular buffer. See if the buffer has wounded back.
303 * If yes it will have to be transferred in two separate dma
306 if (start + up->uart_dma.tx_buf_size >=
307 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
308 up->uart_dma.tx_buf_size =
309 (up->uart_dma.tx_buf_dma_phys +
310 UART_XMIT_SIZE) - start;
312 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
313 OMAP_DMA_AMODE_CONSTANT,
314 up->uart_dma.uart_base, 0, 0);
315 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
316 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
317 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
318 OMAP_DMA_DATA_TYPE_S8,
319 up->uart_dma.tx_buf_size, 1,
320 OMAP_DMA_SYNC_ELEMENT,
321 up->uart_dma.uart_dma_tx, 0);
322 /* FIXME: Cache maintenance needed here? */
323 omap_start_dma(up->uart_dma.tx_dma_channel);
326 static unsigned int check_modem_status(struct uart_omap_port *up)
330 status = serial_in(up, UART_MSR);
331 status |= up->msr_saved_flags;
332 up->msr_saved_flags = 0;
333 if ((status & UART_MSR_ANY_DELTA) == 0)
336 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
337 up->port.state != NULL) {
338 if (status & UART_MSR_TERI)
339 up->port.icount.rng++;
340 if (status & UART_MSR_DDSR)
341 up->port.icount.dsr++;
342 if (status & UART_MSR_DDCD)
343 uart_handle_dcd_change
344 (&up->port, status & UART_MSR_DCD);
345 if (status & UART_MSR_DCTS)
346 uart_handle_cts_change
347 (&up->port, status & UART_MSR_CTS);
348 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
355 * serial_omap_irq() - This handles the interrupt from one port
356 * @irq: uart port irq number
357 * @dev_id: uart port info
359 static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
361 struct uart_omap_port *up = dev_id;
362 unsigned int iir, lsr;
365 iir = serial_in(up, UART_IIR);
366 if (iir & UART_IIR_NO_INT)
369 spin_lock_irqsave(&up->port.lock, flags);
370 lsr = serial_in(up, UART_LSR);
371 if (iir & UART_IIR_RLSI) {
373 if (lsr & UART_LSR_DR)
374 receive_chars(up, &lsr);
376 up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
377 serial_out(up, UART_IER, up->ier);
378 if ((serial_omap_start_rxdma(up) != 0) &&
380 receive_chars(up, &lsr);
384 check_modem_status(up);
385 if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
388 spin_unlock_irqrestore(&up->port.lock, flags);
389 up->port_activity = jiffies;
393 static unsigned int serial_omap_tx_empty(struct uart_port *port)
395 struct uart_omap_port *up = (struct uart_omap_port *)port;
396 unsigned long flags = 0;
397 unsigned int ret = 0;
399 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->pdev->id);
400 spin_lock_irqsave(&up->port.lock, flags);
401 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
402 spin_unlock_irqrestore(&up->port.lock, flags);
407 static unsigned int serial_omap_get_mctrl(struct uart_port *port)
409 struct uart_omap_port *up = (struct uart_omap_port *)port;
410 unsigned char status;
411 unsigned int ret = 0;
413 status = check_modem_status(up);
414 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->pdev->id);
416 if (status & UART_MSR_DCD)
418 if (status & UART_MSR_RI)
420 if (status & UART_MSR_DSR)
422 if (status & UART_MSR_CTS)
427 static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
429 struct uart_omap_port *up = (struct uart_omap_port *)port;
430 unsigned char mcr = 0;
432 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->pdev->id);
433 if (mctrl & TIOCM_RTS)
435 if (mctrl & TIOCM_DTR)
437 if (mctrl & TIOCM_OUT1)
438 mcr |= UART_MCR_OUT1;
439 if (mctrl & TIOCM_OUT2)
440 mcr |= UART_MCR_OUT2;
441 if (mctrl & TIOCM_LOOP)
442 mcr |= UART_MCR_LOOP;
445 serial_out(up, UART_MCR, mcr);
448 static void serial_omap_break_ctl(struct uart_port *port, int break_state)
450 struct uart_omap_port *up = (struct uart_omap_port *)port;
451 unsigned long flags = 0;
453 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
454 spin_lock_irqsave(&up->port.lock, flags);
455 if (break_state == -1)
456 up->lcr |= UART_LCR_SBC;
458 up->lcr &= ~UART_LCR_SBC;
459 serial_out(up, UART_LCR, up->lcr);
460 spin_unlock_irqrestore(&up->port.lock, flags);
463 static int serial_omap_startup(struct uart_port *port)
465 struct uart_omap_port *up = (struct uart_omap_port *)port;
466 unsigned long flags = 0;
472 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
477 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
480 * Clear the FIFO buffers and disable them.
481 * (they will be reenabled in set_termios())
483 serial_omap_clear_fifos(up);
484 /* For Hardware flow control */
485 serial_out(up, UART_MCR, UART_MCR_RTS);
488 * Clear the interrupt registers.
490 (void) serial_in(up, UART_LSR);
491 if (serial_in(up, UART_LSR) & UART_LSR_DR)
492 (void) serial_in(up, UART_RX);
493 (void) serial_in(up, UART_IIR);
494 (void) serial_in(up, UART_MSR);
497 * Now, initialize the UART
499 serial_out(up, UART_LCR, UART_LCR_WLEN8);
500 spin_lock_irqsave(&up->port.lock, flags);
502 * Most PC uarts need OUT2 raised to enable interrupts.
504 up->port.mctrl |= TIOCM_OUT2;
505 serial_omap_set_mctrl(&up->port, up->port.mctrl);
506 spin_unlock_irqrestore(&up->port.lock, flags);
508 up->msr_saved_flags = 0;
510 free_page((unsigned long)up->port.state->xmit.buf);
511 up->port.state->xmit.buf = dma_alloc_coherent(NULL,
513 (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
515 init_timer(&(up->uart_dma.rx_timer));
516 up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
517 up->uart_dma.rx_timer.data = up->pdev->id;
518 /* Currently the buffer size is 4KB. Can increase it */
519 up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
520 up->uart_dma.rx_buf_size,
521 (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
524 * Finally, enable interrupts. Note: Modem status interrupts
525 * are set via set_termios(), which will be occurring imminently
526 * anyway, so we don't enable them here.
528 up->ier = UART_IER_RLSI | UART_IER_RDI;
529 serial_out(up, UART_IER, up->ier);
531 /* Enable module level wake up */
532 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
534 up->port_activity = jiffies;
538 static void serial_omap_shutdown(struct uart_port *port)
540 struct uart_omap_port *up = (struct uart_omap_port *)port;
541 unsigned long flags = 0;
543 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
545 * Disable interrupts from this port
548 serial_out(up, UART_IER, 0);
550 spin_lock_irqsave(&up->port.lock, flags);
551 up->port.mctrl &= ~TIOCM_OUT2;
552 serial_omap_set_mctrl(&up->port, up->port.mctrl);
553 spin_unlock_irqrestore(&up->port.lock, flags);
556 * Disable break condition and FIFOs
558 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
559 serial_omap_clear_fifos(up);
562 * Read data port to reset things, and then free the irq
564 if (serial_in(up, UART_LSR) & UART_LSR_DR)
565 (void) serial_in(up, UART_RX);
567 dma_free_coherent(up->port.dev,
568 UART_XMIT_SIZE, up->port.state->xmit.buf,
569 up->uart_dma.tx_buf_dma_phys);
570 up->port.state->xmit.buf = NULL;
571 serial_omap_stop_rx(port);
572 dma_free_coherent(up->port.dev,
573 up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
574 up->uart_dma.rx_buf_dma_phys);
575 up->uart_dma.rx_buf = NULL;
577 free_irq(up->port.irq, up);
581 serial_omap_configure_xonxoff
582 (struct uart_omap_port *up, struct ktermios *termios)
584 unsigned char efr = 0;
586 up->lcr = serial_in(up, UART_LCR);
587 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
588 up->efr = serial_in(up, UART_EFR);
589 serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
591 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
592 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
594 /* clear SW control mode bits */
596 efr &= OMAP_UART_SW_CLR;
600 * Enable XON/XOFF flow control on output.
601 * Transmit XON1, XOFF1
603 if (termios->c_iflag & IXON)
604 efr |= OMAP_UART_SW_TX;
608 * Enable XON/XOFF flow control on input.
609 * Receiver compares XON1, XOFF1.
611 if (termios->c_iflag & IXOFF)
612 efr |= OMAP_UART_SW_RX;
614 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
615 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
617 up->mcr = serial_in(up, UART_MCR);
621 * Enable any character to restart output.
622 * Operation resumes after receiving any
623 * character after recognition of the XOFF character
625 if (termios->c_iflag & IXANY)
626 up->mcr |= UART_MCR_XONANY;
628 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
629 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
630 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
631 /* Enable special char function UARTi.EFR_REG[5] and
632 * load the new software flow control mode IXON or IXOFF
633 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
635 serial_out(up, UART_EFR, efr | UART_EFR_SCD);
636 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
638 serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
639 serial_out(up, UART_LCR, up->lcr);
643 serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
644 struct ktermios *old)
646 struct uart_omap_port *up = (struct uart_omap_port *)port;
647 unsigned char cval = 0;
648 unsigned char efr = 0;
649 unsigned long flags = 0;
650 unsigned int baud, quot;
652 switch (termios->c_cflag & CSIZE) {
654 cval = UART_LCR_WLEN5;
657 cval = UART_LCR_WLEN6;
660 cval = UART_LCR_WLEN7;
664 cval = UART_LCR_WLEN8;
668 if (termios->c_cflag & CSTOPB)
669 cval |= UART_LCR_STOP;
670 if (termios->c_cflag & PARENB)
671 cval |= UART_LCR_PARITY;
672 if (!(termios->c_cflag & PARODD))
673 cval |= UART_LCR_EPAR;
676 * Ask the core to calculate the divisor for us.
679 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
680 quot = serial_omap_get_divisor(port, baud);
682 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
683 UART_FCR_ENABLE_FIFO;
685 up->fcr |= UART_FCR_DMA_SELECT;
688 * Ok, we're now changing the port state. Do it with
689 * interrupts disabled.
691 spin_lock_irqsave(&up->port.lock, flags);
694 * Update the per-port timeout.
696 uart_update_timeout(port, termios->c_cflag, baud);
698 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
699 if (termios->c_iflag & INPCK)
700 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
701 if (termios->c_iflag & (BRKINT | PARMRK))
702 up->port.read_status_mask |= UART_LSR_BI;
705 * Characters to ignore
707 up->port.ignore_status_mask = 0;
708 if (termios->c_iflag & IGNPAR)
709 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
710 if (termios->c_iflag & IGNBRK) {
711 up->port.ignore_status_mask |= UART_LSR_BI;
713 * If we're ignoring parity and break indicators,
714 * ignore overruns too (for real raw support).
716 if (termios->c_iflag & IGNPAR)
717 up->port.ignore_status_mask |= UART_LSR_OE;
721 * ignore all characters if CREAD is not set
723 if ((termios->c_cflag & CREAD) == 0)
724 up->port.ignore_status_mask |= UART_LSR_DR;
727 * Modem status interrupts
729 up->ier &= ~UART_IER_MSI;
730 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
731 up->ier |= UART_IER_MSI;
732 serial_out(up, UART_IER, up->ier);
733 serial_out(up, UART_LCR, cval); /* reset DLAB */
734 up->scr = OMAP_UART_SCR_TX_EMPTY;
736 /* FIFOs and DMA Settings */
738 /* FCR can be changed only when the
739 * baud clock is not running
740 * DLL_REG and DLH_REG set to 0.
742 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
743 serial_out(up, UART_DLL, 0);
744 serial_out(up, UART_DLM, 0);
745 serial_out(up, UART_LCR, 0);
747 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
749 up->efr = serial_in(up, UART_EFR);
750 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
752 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
753 up->mcr = serial_in(up, UART_MCR);
754 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
755 /* FIFO ENABLE, DMA MODE */
757 up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
760 serial_out(up, UART_TI752_TLR, 0);
761 up->scr |= UART_FCR_TRIGGER_4;
763 /* Set receive FIFO threshold to 1 byte */
764 up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
765 up->fcr |= (0x1 << OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT);
768 serial_out(up, UART_FCR, up->fcr);
769 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
771 serial_out(up, UART_OMAP_SCR, up->scr);
773 serial_out(up, UART_EFR, up->efr);
774 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
775 serial_out(up, UART_MCR, up->mcr);
777 /* Protocol, Baud Rate, and Interrupt Settings */
779 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
780 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
782 up->efr = serial_in(up, UART_EFR);
783 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
785 serial_out(up, UART_LCR, 0);
786 serial_out(up, UART_IER, 0);
787 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
789 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
790 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
792 serial_out(up, UART_LCR, 0);
793 serial_out(up, UART_IER, up->ier);
794 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
796 serial_out(up, UART_EFR, up->efr);
797 serial_out(up, UART_LCR, cval);
799 if (baud > 230400 && baud != 3000000)
800 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_13X_MODE);
802 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
804 /* Hardware Flow Control Configuration */
806 if (termios->c_cflag & CRTSCTS) {
807 efr |= (UART_EFR_CTS | UART_EFR_RTS);
808 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
810 up->mcr = serial_in(up, UART_MCR);
811 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
813 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
814 up->efr = serial_in(up, UART_EFR);
815 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
817 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
818 serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
819 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
820 serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
821 serial_out(up, UART_LCR, cval);
824 serial_omap_set_mctrl(&up->port, up->port.mctrl);
825 /* Software Flow Control Configuration */
826 serial_omap_configure_xonxoff(up, termios);
828 spin_unlock_irqrestore(&up->port.lock, flags);
829 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
833 serial_omap_pm(struct uart_port *port, unsigned int state,
834 unsigned int oldstate)
836 struct uart_omap_port *up = (struct uart_omap_port *)port;
839 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
840 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
841 efr = serial_in(up, UART_EFR);
842 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
843 serial_out(up, UART_LCR, 0);
845 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
846 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
847 serial_out(up, UART_EFR, efr);
848 serial_out(up, UART_LCR, 0);
851 static void serial_omap_release_port(struct uart_port *port)
853 dev_dbg(port->dev, "serial_omap_release_port+\n");
856 static int serial_omap_request_port(struct uart_port *port)
858 dev_dbg(port->dev, "serial_omap_request_port+\n");
862 static void serial_omap_config_port(struct uart_port *port, int flags)
864 struct uart_omap_port *up = (struct uart_omap_port *)port;
866 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
868 up->port.type = PORT_OMAP;
872 serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
874 /* we don't want the core code to modify any port params */
875 dev_dbg(port->dev, "serial_omap_verify_port+\n");
880 serial_omap_type(struct uart_port *port)
882 struct uart_omap_port *up = (struct uart_omap_port *)port;
884 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
888 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
890 static inline void wait_for_xmitr(struct uart_omap_port *up)
892 unsigned int status, tmout = 10000;
894 /* Wait up to 10ms for the character(s) to be sent. */
896 status = serial_in(up, UART_LSR);
898 if (status & UART_LSR_BI)
899 up->lsr_break_flag = UART_LSR_BI;
904 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
906 /* Wait up to 1s for flow control if necessary */
907 if (up->port.flags & UPF_CONS_FLOW) {
909 for (tmout = 1000000; tmout; tmout--) {
910 unsigned int msr = serial_in(up, UART_MSR);
912 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
913 if (msr & UART_MSR_CTS)
921 #ifdef CONFIG_CONSOLE_POLL
923 static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
925 struct uart_omap_port *up = (struct uart_omap_port *)port;
927 serial_out(up, UART_TX, ch);
930 static int serial_omap_poll_get_char(struct uart_port *port)
932 struct uart_omap_port *up = (struct uart_omap_port *)port;
933 unsigned int status = serial_in(up, UART_LSR);
935 if (!(status & UART_LSR_DR))
938 return serial_in(up, UART_RX);
941 #endif /* CONFIG_CONSOLE_POLL */
943 #ifdef CONFIG_SERIAL_OMAP_CONSOLE
945 static struct uart_omap_port *serial_omap_console_ports[4];
947 static struct uart_driver serial_omap_reg;
949 static void serial_omap_console_putchar(struct uart_port *port, int ch)
951 struct uart_omap_port *up = (struct uart_omap_port *)port;
954 serial_out(up, UART_TX, ch);
958 serial_omap_console_write(struct console *co, const char *s,
961 struct uart_omap_port *up = serial_omap_console_ports[co->index];
966 local_irq_save(flags);
969 else if (oops_in_progress)
970 locked = spin_trylock(&up->port.lock);
972 spin_lock(&up->port.lock);
975 * First save the IER then disable the interrupts
977 ier = serial_in(up, UART_IER);
978 serial_out(up, UART_IER, 0);
980 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
983 * Finally, wait for transmitter to become empty
984 * and restore the IER
987 serial_out(up, UART_IER, ier);
989 * The receive handling will happen properly because the
990 * receive ready bit will still be set; it is not cleared
991 * on read. However, modem control will not, we must
992 * call it if we have saved something in the saved flags
993 * while processing with interrupts off.
995 if (up->msr_saved_flags)
996 check_modem_status(up);
999 spin_unlock(&up->port.lock);
1000 local_irq_restore(flags);
1004 serial_omap_console_setup(struct console *co, char *options)
1006 struct uart_omap_port *up;
1012 if (serial_omap_console_ports[co->index] == NULL)
1014 up = serial_omap_console_ports[co->index];
1017 uart_parse_options(options, &baud, &parity, &bits, &flow);
1019 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1022 static struct console serial_omap_console = {
1023 .name = OMAP_SERIAL_NAME,
1024 .write = serial_omap_console_write,
1025 .device = uart_console_device,
1026 .setup = serial_omap_console_setup,
1027 .flags = CON_PRINTBUFFER,
1029 .data = &serial_omap_reg,
1032 static void serial_omap_add_console_port(struct uart_omap_port *up)
1034 serial_omap_console_ports[up->pdev->id] = up;
1037 #define OMAP_CONSOLE (&serial_omap_console)
1041 #define OMAP_CONSOLE NULL
1043 static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1048 static struct uart_ops serial_omap_pops = {
1049 .tx_empty = serial_omap_tx_empty,
1050 .set_mctrl = serial_omap_set_mctrl,
1051 .get_mctrl = serial_omap_get_mctrl,
1052 .stop_tx = serial_omap_stop_tx,
1053 .start_tx = serial_omap_start_tx,
1054 .stop_rx = serial_omap_stop_rx,
1055 .enable_ms = serial_omap_enable_ms,
1056 .break_ctl = serial_omap_break_ctl,
1057 .startup = serial_omap_startup,
1058 .shutdown = serial_omap_shutdown,
1059 .set_termios = serial_omap_set_termios,
1060 .pm = serial_omap_pm,
1061 .type = serial_omap_type,
1062 .release_port = serial_omap_release_port,
1063 .request_port = serial_omap_request_port,
1064 .config_port = serial_omap_config_port,
1065 .verify_port = serial_omap_verify_port,
1066 #ifdef CONFIG_CONSOLE_POLL
1067 .poll_put_char = serial_omap_poll_put_char,
1068 .poll_get_char = serial_omap_poll_get_char,
1072 static struct uart_driver serial_omap_reg = {
1073 .owner = THIS_MODULE,
1074 .driver_name = "OMAP-SERIAL",
1075 .dev_name = OMAP_SERIAL_NAME,
1076 .nr = OMAP_MAX_HSUART_PORTS,
1077 .cons = OMAP_CONSOLE,
1081 serial_omap_suspend(struct platform_device *pdev, pm_message_t state)
1083 struct uart_omap_port *up = platform_get_drvdata(pdev);
1086 uart_suspend_port(&serial_omap_reg, &up->port);
1090 static int serial_omap_resume(struct platform_device *dev)
1092 struct uart_omap_port *up = platform_get_drvdata(dev);
1095 uart_resume_port(&serial_omap_reg, &up->port);
1099 static void serial_omap_rx_timeout(unsigned long uart_no)
1101 struct uart_omap_port *up = ui[uart_no];
1102 unsigned int curr_dma_pos, curr_transmitted_size;
1105 curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
1106 if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
1107 (curr_dma_pos == 0)) {
1108 if (jiffies_to_msecs(jiffies - up->port_activity) <
1110 mod_timer(&up->uart_dma.rx_timer, jiffies +
1111 usecs_to_jiffies(up->uart_dma.rx_timeout));
1113 serial_omap_stop_rxdma(up);
1114 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1115 serial_out(up, UART_IER, up->ier);
1120 curr_transmitted_size = curr_dma_pos -
1121 up->uart_dma.prev_rx_dma_pos;
1122 up->port.icount.rx += curr_transmitted_size;
1123 tty_insert_flip_string(up->port.state->port.tty,
1124 up->uart_dma.rx_buf +
1125 (up->uart_dma.prev_rx_dma_pos -
1126 up->uart_dma.rx_buf_dma_phys),
1127 curr_transmitted_size);
1128 tty_flip_buffer_push(up->port.state->port.tty);
1129 up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
1130 if (up->uart_dma.rx_buf_size +
1131 up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
1132 ret = serial_omap_start_rxdma(up);
1134 serial_omap_stop_rxdma(up);
1135 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1136 serial_out(up, UART_IER, up->ier);
1139 mod_timer(&up->uart_dma.rx_timer, jiffies +
1140 usecs_to_jiffies(up->uart_dma.rx_timeout));
1142 up->port_activity = jiffies;
1145 static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
1150 static int serial_omap_start_rxdma(struct uart_omap_port *up)
1154 if (up->uart_dma.rx_dma_channel == -1) {
1155 ret = omap_request_dma(up->uart_dma.uart_dma_rx,
1157 (void *)uart_rx_dma_callback, up,
1158 &(up->uart_dma.rx_dma_channel));
1162 omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
1163 OMAP_DMA_AMODE_CONSTANT,
1164 up->uart_dma.uart_base, 0, 0);
1165 omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
1166 OMAP_DMA_AMODE_POST_INC,
1167 up->uart_dma.rx_buf_dma_phys, 0, 0);
1168 omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
1169 OMAP_DMA_DATA_TYPE_S8,
1170 up->uart_dma.rx_buf_size, 1,
1171 OMAP_DMA_SYNC_ELEMENT,
1172 up->uart_dma.uart_dma_rx, 0);
1174 up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
1175 /* FIXME: Cache maintenance needed here? */
1176 omap_start_dma(up->uart_dma.rx_dma_channel);
1177 mod_timer(&up->uart_dma.rx_timer, jiffies +
1178 usecs_to_jiffies(up->uart_dma.rx_timeout));
1179 up->uart_dma.rx_dma_used = true;
1183 static void serial_omap_continue_tx(struct uart_omap_port *up)
1185 struct circ_buf *xmit = &up->port.state->xmit;
1186 unsigned int start = up->uart_dma.tx_buf_dma_phys
1187 + (xmit->tail & (UART_XMIT_SIZE - 1));
1189 if (uart_circ_empty(xmit))
1192 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
1194 * It is a circular buffer. See if the buffer has wounded back.
1195 * If yes it will have to be transferred in two separate dma
1198 if (start + up->uart_dma.tx_buf_size >=
1199 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
1200 up->uart_dma.tx_buf_size =
1201 (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
1202 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
1203 OMAP_DMA_AMODE_CONSTANT,
1204 up->uart_dma.uart_base, 0, 0);
1205 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
1206 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
1207 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
1208 OMAP_DMA_DATA_TYPE_S8,
1209 up->uart_dma.tx_buf_size, 1,
1210 OMAP_DMA_SYNC_ELEMENT,
1211 up->uart_dma.uart_dma_tx, 0);
1212 /* FIXME: Cache maintenance needed here? */
1213 omap_start_dma(up->uart_dma.tx_dma_channel);
1216 static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1218 struct uart_omap_port *up = (struct uart_omap_port *)data;
1219 struct circ_buf *xmit = &up->port.state->xmit;
1221 xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
1222 (UART_XMIT_SIZE - 1);
1223 up->port.icount.tx += up->uart_dma.tx_buf_size;
1225 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1226 uart_write_wakeup(&up->port);
1228 if (uart_circ_empty(xmit)) {
1229 spin_lock(&(up->uart_dma.tx_lock));
1230 serial_omap_stop_tx(&up->port);
1231 up->uart_dma.tx_dma_used = false;
1232 spin_unlock(&(up->uart_dma.tx_lock));
1234 omap_stop_dma(up->uart_dma.tx_dma_channel);
1235 serial_omap_continue_tx(up);
1237 up->port_activity = jiffies;
1241 static int serial_omap_probe(struct platform_device *pdev)
1243 struct uart_omap_port *up;
1244 struct resource *mem, *irq, *dma_tx, *dma_rx;
1245 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1248 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1250 dev_err(&pdev->dev, "no mem resource?\n");
1254 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1256 dev_err(&pdev->dev, "no irq resource?\n");
1260 if (!request_mem_region(mem->start, resource_size(mem),
1261 pdev->dev.driver->name)) {
1262 dev_err(&pdev->dev, "memory region already claimed\n");
1266 dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1272 dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1278 up = kzalloc(sizeof(*up), GFP_KERNEL);
1281 goto do_release_region;
1283 sprintf(up->name, "OMAP UART%d", pdev->id);
1285 up->port.dev = &pdev->dev;
1286 up->port.type = PORT_OMAP;
1287 up->port.iotype = UPIO_MEM;
1288 up->port.irq = irq->start;
1290 up->port.regshift = 2;
1291 up->port.fifosize = 64;
1292 up->port.ops = &serial_omap_pops;
1293 up->port.line = pdev->id;
1295 up->port.membase = omap_up_info->membase;
1296 up->port.mapbase = omap_up_info->mapbase;
1297 up->port.flags = omap_up_info->flags;
1298 up->port.irqflags = omap_up_info->irqflags;
1299 up->port.uartclk = omap_up_info->uartclk;
1300 up->uart_dma.uart_base = mem->start;
1302 if (omap_up_info->dma_enabled) {
1303 up->uart_dma.uart_dma_tx = dma_tx->start;
1304 up->uart_dma.uart_dma_rx = dma_rx->start;
1306 up->uart_dma.rx_buf_size = 4096;
1307 up->uart_dma.rx_timeout = 2;
1308 spin_lock_init(&(up->uart_dma.tx_lock));
1309 spin_lock_init(&(up->uart_dma.rx_lock));
1310 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
1311 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
1315 serial_omap_add_console_port(up);
1317 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1319 goto do_release_region;
1321 platform_set_drvdata(pdev, up);
1324 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1325 pdev->id, __func__, ret);
1327 release_mem_region(mem->start, resource_size(mem));
1331 static int serial_omap_remove(struct platform_device *dev)
1333 struct uart_omap_port *up = platform_get_drvdata(dev);
1335 platform_set_drvdata(dev, NULL);
1337 uart_remove_one_port(&serial_omap_reg, &up->port);
1343 static struct platform_driver serial_omap_driver = {
1344 .probe = serial_omap_probe,
1345 .remove = serial_omap_remove,
1347 .suspend = serial_omap_suspend,
1348 .resume = serial_omap_resume,
1350 .name = DRIVER_NAME,
1354 static int __init serial_omap_init(void)
1358 ret = uart_register_driver(&serial_omap_reg);
1361 ret = platform_driver_register(&serial_omap_driver);
1363 uart_unregister_driver(&serial_omap_reg);
1367 static void __exit serial_omap_exit(void)
1369 platform_driver_unregister(&serial_omap_driver);
1370 uart_unregister_driver(&serial_omap_reg);
1373 module_init(serial_omap_init);
1374 module_exit(serial_omap_exit);
1376 MODULE_DESCRIPTION("OMAP High Speed UART driver");
1377 MODULE_LICENSE("GPL");
1378 MODULE_AUTHOR("Texas Instruments Inc");