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,
159 unsigned int *status)
161 struct tty_struct *tty = up->port.state->port.tty;
162 unsigned int flag, lsr = *status;
163 unsigned char ch = 0;
167 if (likely(lsr & UART_LSR_DR))
168 ch = serial_in(up, UART_RX);
170 up->port.icount.rx++;
172 if (unlikely(lsr & UART_LSR_BRK_ERROR_BITS)) {
174 * For statistics only
176 if (lsr & UART_LSR_BI) {
177 lsr &= ~(UART_LSR_FE | UART_LSR_PE);
178 up->port.icount.brk++;
180 * We do the SysRQ and SAK checking
181 * here because otherwise the break
182 * may get masked by ignore_status_mask
183 * or read_status_mask.
185 if (uart_handle_break(&up->port))
187 } else if (lsr & UART_LSR_PE) {
188 up->port.icount.parity++;
189 } else if (lsr & UART_LSR_FE) {
190 up->port.icount.frame++;
193 if (lsr & UART_LSR_OE)
194 up->port.icount.overrun++;
197 * Mask off conditions which should be ignored.
199 lsr &= up->port.read_status_mask;
201 #ifdef CONFIG_SERIAL_OMAP_CONSOLE
202 if (up->port.line == up->port.cons->index) {
203 /* Recover the break flag from console xmit */
204 lsr |= up->lsr_break_flag;
207 if (lsr & UART_LSR_BI)
209 else if (lsr & UART_LSR_PE)
211 else if (lsr & UART_LSR_FE)
215 if (uart_handle_sysrq_char(&up->port, ch))
217 uart_insert_char(&up->port, lsr, UART_LSR_OE, ch, flag);
219 lsr = serial_in(up, UART_LSR);
220 } while ((lsr & (UART_LSR_DR | UART_LSR_BI)) && (max_count-- > 0));
221 spin_unlock(&up->port.lock);
222 tty_flip_buffer_push(tty);
223 spin_lock(&up->port.lock);
226 static void transmit_chars(struct uart_omap_port *up)
228 struct circ_buf *xmit = &up->port.state->xmit;
231 if (up->port.x_char) {
232 serial_out(up, UART_TX, up->port.x_char);
233 up->port.icount.tx++;
237 if (uart_circ_empty(xmit) || uart_tx_stopped(&up->port)) {
238 serial_omap_stop_tx(&up->port);
241 count = up->port.fifosize / 4;
243 serial_out(up, UART_TX, xmit->buf[xmit->tail]);
244 xmit->tail = (xmit->tail + 1) & (UART_XMIT_SIZE - 1);
245 up->port.icount.tx++;
246 if (uart_circ_empty(xmit))
248 } while (--count > 0);
250 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
251 uart_write_wakeup(&up->port);
253 if (uart_circ_empty(xmit))
254 serial_omap_stop_tx(&up->port);
257 static inline void serial_omap_enable_ier_thri(struct uart_omap_port *up)
259 if (!(up->ier & UART_IER_THRI)) {
260 up->ier |= UART_IER_THRI;
261 serial_out(up, UART_IER, up->ier);
265 static void serial_omap_start_tx(struct uart_port *port)
267 struct uart_omap_port *up = (struct uart_omap_port *)port;
268 struct circ_buf *xmit;
273 omap_uart_block_sleep_id(up->pdev->id);
274 serial_omap_enable_ier_thri(up);
278 if (up->uart_dma.tx_dma_used)
281 xmit = &up->port.state->xmit;
283 if (up->uart_dma.tx_dma_channel == OMAP_UART_DMA_CH_FREE) {
284 ret = omap_request_dma(up->uart_dma.uart_dma_tx,
286 (void *)uart_tx_dma_callback, up,
287 &(up->uart_dma.tx_dma_channel));
290 serial_omap_enable_ier_thri(up);
294 spin_lock(&(up->uart_dma.tx_lock));
295 up->uart_dma.tx_dma_used = true;
296 spin_unlock(&(up->uart_dma.tx_lock));
298 start = up->uart_dma.tx_buf_dma_phys +
299 (xmit->tail & (UART_XMIT_SIZE - 1));
301 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
303 * It is a circular buffer. See if the buffer has wounded back.
304 * If yes it will have to be transferred in two separate dma
307 if (start + up->uart_dma.tx_buf_size >=
308 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
309 up->uart_dma.tx_buf_size =
310 (up->uart_dma.tx_buf_dma_phys +
311 UART_XMIT_SIZE) - start;
313 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
314 OMAP_DMA_AMODE_CONSTANT,
315 up->uart_dma.uart_base, 0, 0);
316 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
317 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
318 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
319 OMAP_DMA_DATA_TYPE_S8,
320 up->uart_dma.tx_buf_size, 1,
321 OMAP_DMA_SYNC_ELEMENT,
322 up->uart_dma.uart_dma_tx, 0);
323 /* FIXME: Cache maintenance needed here? */
324 omap_start_dma(up->uart_dma.tx_dma_channel);
327 static unsigned int check_modem_status(struct uart_omap_port *up)
331 status = serial_in(up, UART_MSR);
332 status |= up->msr_saved_flags;
333 up->msr_saved_flags = 0;
334 if ((status & UART_MSR_ANY_DELTA) == 0)
337 if (status & UART_MSR_ANY_DELTA && up->ier & UART_IER_MSI &&
338 up->port.state != NULL) {
339 if (status & UART_MSR_TERI)
340 up->port.icount.rng++;
341 if (status & UART_MSR_DDSR)
342 up->port.icount.dsr++;
343 if (status & UART_MSR_DDCD)
344 uart_handle_dcd_change
345 (&up->port, status & UART_MSR_DCD);
346 if (status & UART_MSR_DCTS)
347 uart_handle_cts_change
348 (&up->port, status & UART_MSR_CTS);
349 wake_up_interruptible(&up->port.state->port.delta_msr_wait);
356 * serial_omap_irq() - This handles the interrupt from one port
357 * @irq: uart port irq number
358 * @dev_id: uart port info
360 static inline irqreturn_t serial_omap_irq(int irq, void *dev_id)
362 struct uart_omap_port *up = dev_id;
363 unsigned int iir, lsr;
366 iir = serial_in(up, UART_IIR);
367 if (iir & UART_IIR_NO_INT)
370 spin_lock_irqsave(&up->port.lock, flags);
371 lsr = serial_in(up, UART_LSR);
372 if (iir & UART_IIR_RLSI) {
374 if (lsr & UART_LSR_DR)
375 receive_chars(up, &lsr);
377 up->ier &= ~(UART_IER_RDI | UART_IER_RLSI);
378 serial_out(up, UART_IER, up->ier);
379 if ((serial_omap_start_rxdma(up) != 0) &&
381 receive_chars(up, &lsr);
385 check_modem_status(up);
386 if ((lsr & UART_LSR_THRE) && (iir & UART_IIR_THRI))
389 spin_unlock_irqrestore(&up->port.lock, flags);
390 up->port_activity = jiffies;
394 static unsigned int serial_omap_tx_empty(struct uart_port *port)
396 struct uart_omap_port *up = (struct uart_omap_port *)port;
397 unsigned long flags = 0;
398 unsigned int ret = 0;
400 dev_dbg(up->port.dev, "serial_omap_tx_empty+%d\n", up->pdev->id);
401 spin_lock_irqsave(&up->port.lock, flags);
402 ret = serial_in(up, UART_LSR) & UART_LSR_TEMT ? TIOCSER_TEMT : 0;
403 spin_unlock_irqrestore(&up->port.lock, flags);
408 static unsigned int serial_omap_get_mctrl(struct uart_port *port)
410 struct uart_omap_port *up = (struct uart_omap_port *)port;
411 unsigned char status;
412 unsigned int ret = 0;
414 status = check_modem_status(up);
415 dev_dbg(up->port.dev, "serial_omap_get_mctrl+%d\n", up->pdev->id);
417 if (status & UART_MSR_DCD)
419 if (status & UART_MSR_RI)
421 if (status & UART_MSR_DSR)
423 if (status & UART_MSR_CTS)
428 static void serial_omap_set_mctrl(struct uart_port *port, unsigned int mctrl)
430 struct uart_omap_port *up = (struct uart_omap_port *)port;
431 unsigned char mcr = 0;
433 dev_dbg(up->port.dev, "serial_omap_set_mctrl+%d\n", up->pdev->id);
434 if (mctrl & TIOCM_RTS)
436 if (mctrl & TIOCM_DTR)
438 if (mctrl & TIOCM_OUT1)
439 mcr |= UART_MCR_OUT1;
440 if (mctrl & TIOCM_OUT2)
441 mcr |= UART_MCR_OUT2;
442 if (mctrl & TIOCM_LOOP)
443 mcr |= UART_MCR_LOOP;
446 serial_out(up, UART_MCR, mcr);
449 static void serial_omap_break_ctl(struct uart_port *port, int break_state)
451 struct uart_omap_port *up = (struct uart_omap_port *)port;
452 unsigned long flags = 0;
454 dev_dbg(up->port.dev, "serial_omap_break_ctl+%d\n", up->pdev->id);
455 spin_lock_irqsave(&up->port.lock, flags);
456 if (break_state == -1)
457 up->lcr |= UART_LCR_SBC;
459 up->lcr &= ~UART_LCR_SBC;
460 serial_out(up, UART_LCR, up->lcr);
461 spin_unlock_irqrestore(&up->port.lock, flags);
464 static int serial_omap_startup(struct uart_port *port)
466 struct uart_omap_port *up = (struct uart_omap_port *)port;
467 unsigned long flags = 0;
473 retval = request_irq(up->port.irq, serial_omap_irq, up->port.irqflags,
478 dev_dbg(up->port.dev, "serial_omap_startup+%d\n", up->pdev->id);
481 * Clear the FIFO buffers and disable them.
482 * (they will be reenabled in set_termios())
484 serial_omap_clear_fifos(up);
485 /* For Hardware flow control */
486 serial_out(up, UART_MCR, UART_MCR_RTS);
489 * Clear the interrupt registers.
491 (void) serial_in(up, UART_LSR);
492 if (serial_in(up, UART_LSR) & UART_LSR_DR)
493 (void) serial_in(up, UART_RX);
494 (void) serial_in(up, UART_IIR);
495 (void) serial_in(up, UART_MSR);
498 * Now, initialize the UART
500 serial_out(up, UART_LCR, UART_LCR_WLEN8);
501 spin_lock_irqsave(&up->port.lock, flags);
503 * Most PC uarts need OUT2 raised to enable interrupts.
505 up->port.mctrl |= TIOCM_OUT2;
506 serial_omap_set_mctrl(&up->port, up->port.mctrl);
507 spin_unlock_irqrestore(&up->port.lock, flags);
509 up->msr_saved_flags = 0;
511 free_page((unsigned long)up->port.state->xmit.buf);
512 up->port.state->xmit.buf = dma_alloc_coherent(NULL,
514 (dma_addr_t *)&(up->uart_dma.tx_buf_dma_phys),
516 init_timer(&(up->uart_dma.rx_timer));
517 up->uart_dma.rx_timer.function = serial_omap_rx_timeout;
518 up->uart_dma.rx_timer.data = up->pdev->id;
519 /* Currently the buffer size is 4KB. Can increase it */
520 up->uart_dma.rx_buf = dma_alloc_coherent(NULL,
521 up->uart_dma.rx_buf_size,
522 (dma_addr_t *)&(up->uart_dma.rx_buf_dma_phys), 0);
525 * Finally, enable interrupts. Note: Modem status interrupts
526 * are set via set_termios(), which will be occurring imminently
527 * anyway, so we don't enable them here.
529 up->ier = UART_IER_RLSI | UART_IER_RDI;
530 serial_out(up, UART_IER, up->ier);
532 /* Enable module level wake up */
533 serial_out(up, UART_OMAP_WER, OMAP_UART_WER_MOD_WKUP);
535 up->port_activity = jiffies;
539 static void serial_omap_shutdown(struct uart_port *port)
541 struct uart_omap_port *up = (struct uart_omap_port *)port;
542 unsigned long flags = 0;
544 dev_dbg(up->port.dev, "serial_omap_shutdown+%d\n", up->pdev->id);
546 * Disable interrupts from this port
549 serial_out(up, UART_IER, 0);
551 spin_lock_irqsave(&up->port.lock, flags);
552 up->port.mctrl &= ~TIOCM_OUT2;
553 serial_omap_set_mctrl(&up->port, up->port.mctrl);
554 spin_unlock_irqrestore(&up->port.lock, flags);
557 * Disable break condition and FIFOs
559 serial_out(up, UART_LCR, serial_in(up, UART_LCR) & ~UART_LCR_SBC);
560 serial_omap_clear_fifos(up);
563 * Read data port to reset things, and then free the irq
565 if (serial_in(up, UART_LSR) & UART_LSR_DR)
566 (void) serial_in(up, UART_RX);
568 dma_free_coherent(up->port.dev,
569 UART_XMIT_SIZE, up->port.state->xmit.buf,
570 up->uart_dma.tx_buf_dma_phys);
571 up->port.state->xmit.buf = NULL;
572 serial_omap_stop_rx(port);
573 dma_free_coherent(up->port.dev,
574 up->uart_dma.rx_buf_size, up->uart_dma.rx_buf,
575 up->uart_dma.rx_buf_dma_phys);
576 up->uart_dma.rx_buf = NULL;
578 free_irq(up->port.irq, up);
582 serial_omap_configure_xonxoff
583 (struct uart_omap_port *up, struct ktermios *termios)
585 unsigned char efr = 0;
587 up->lcr = serial_in(up, UART_LCR);
588 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
589 up->efr = serial_in(up, UART_EFR);
590 serial_out(up, UART_EFR, up->efr & ~UART_EFR_ECB);
592 serial_out(up, UART_XON1, termios->c_cc[VSTART]);
593 serial_out(up, UART_XOFF1, termios->c_cc[VSTOP]);
595 /* clear SW control mode bits */
597 efr &= OMAP_UART_SW_CLR;
601 * Enable XON/XOFF flow control on output.
602 * Transmit XON1, XOFF1
604 if (termios->c_iflag & IXON)
605 efr |= OMAP_UART_SW_TX;
609 * Enable XON/XOFF flow control on input.
610 * Receiver compares XON1, XOFF1.
612 if (termios->c_iflag & IXOFF)
613 efr |= OMAP_UART_SW_RX;
615 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
616 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
618 up->mcr = serial_in(up, UART_MCR);
622 * Enable any character to restart output.
623 * Operation resumes after receiving any
624 * character after recognition of the XOFF character
626 if (termios->c_iflag & IXANY)
627 up->mcr |= UART_MCR_XONANY;
629 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
630 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
631 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
632 /* Enable special char function UARTi.EFR_REG[5] and
633 * load the new software flow control mode IXON or IXOFF
634 * and restore the UARTi.EFR_REG[4] ENHANCED_EN value.
636 serial_out(up, UART_EFR, efr | UART_EFR_SCD);
637 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
639 serial_out(up, UART_MCR, up->mcr & ~UART_MCR_TCRTLR);
640 serial_out(up, UART_LCR, up->lcr);
644 serial_omap_set_termios(struct uart_port *port, struct ktermios *termios,
645 struct ktermios *old)
647 struct uart_omap_port *up = (struct uart_omap_port *)port;
648 unsigned char cval = 0;
649 unsigned char efr = 0;
650 unsigned long flags = 0;
651 unsigned int baud, quot;
653 switch (termios->c_cflag & CSIZE) {
655 cval = UART_LCR_WLEN5;
658 cval = UART_LCR_WLEN6;
661 cval = UART_LCR_WLEN7;
665 cval = UART_LCR_WLEN8;
669 if (termios->c_cflag & CSTOPB)
670 cval |= UART_LCR_STOP;
671 if (termios->c_cflag & PARENB)
672 cval |= UART_LCR_PARITY;
673 if (!(termios->c_cflag & PARODD))
674 cval |= UART_LCR_EPAR;
677 * Ask the core to calculate the divisor for us.
680 baud = uart_get_baud_rate(port, termios, old, 0, port->uartclk/13);
681 quot = serial_omap_get_divisor(port, baud);
683 up->fcr = UART_FCR_R_TRIG_01 | UART_FCR_T_TRIG_01 |
684 UART_FCR_ENABLE_FIFO;
686 up->fcr |= UART_FCR_DMA_SELECT;
689 * Ok, we're now changing the port state. Do it with
690 * interrupts disabled.
692 spin_lock_irqsave(&up->port.lock, flags);
695 * Update the per-port timeout.
697 uart_update_timeout(port, termios->c_cflag, baud);
699 up->port.read_status_mask = UART_LSR_OE | UART_LSR_THRE | UART_LSR_DR;
700 if (termios->c_iflag & INPCK)
701 up->port.read_status_mask |= UART_LSR_FE | UART_LSR_PE;
702 if (termios->c_iflag & (BRKINT | PARMRK))
703 up->port.read_status_mask |= UART_LSR_BI;
706 * Characters to ignore
708 up->port.ignore_status_mask = 0;
709 if (termios->c_iflag & IGNPAR)
710 up->port.ignore_status_mask |= UART_LSR_PE | UART_LSR_FE;
711 if (termios->c_iflag & IGNBRK) {
712 up->port.ignore_status_mask |= UART_LSR_BI;
714 * If we're ignoring parity and break indicators,
715 * ignore overruns too (for real raw support).
717 if (termios->c_iflag & IGNPAR)
718 up->port.ignore_status_mask |= UART_LSR_OE;
722 * ignore all characters if CREAD is not set
724 if ((termios->c_cflag & CREAD) == 0)
725 up->port.ignore_status_mask |= UART_LSR_DR;
728 * Modem status interrupts
730 up->ier &= ~UART_IER_MSI;
731 if (UART_ENABLE_MS(&up->port, termios->c_cflag))
732 up->ier |= UART_IER_MSI;
733 serial_out(up, UART_IER, up->ier);
734 serial_out(up, UART_LCR, cval); /* reset DLAB */
735 up->scr = OMAP_UART_SCR_TX_EMPTY;
737 /* FIFOs and DMA Settings */
739 /* FCR can be changed only when the
740 * baud clock is not running
741 * DLL_REG and DLH_REG set to 0.
743 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
744 serial_out(up, UART_DLL, 0);
745 serial_out(up, UART_DLM, 0);
746 serial_out(up, UART_LCR, 0);
748 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
750 up->efr = serial_in(up, UART_EFR);
751 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
753 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
754 up->mcr = serial_in(up, UART_MCR);
755 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
756 /* FIFO ENABLE, DMA MODE */
758 up->scr |= OMAP_UART_SCR_RX_TRIG_GRANU1_MASK;
761 serial_out(up, UART_TI752_TLR, 0);
762 up->scr |= UART_FCR_TRIGGER_4;
764 /* Set receive FIFO threshold to 1 byte */
765 up->fcr &= ~OMAP_UART_FCR_RX_FIFO_TRIG_MASK;
766 up->fcr |= (0x1 << OMAP_UART_FCR_RX_FIFO_TRIG_SHIFT);
769 serial_out(up, UART_FCR, up->fcr);
770 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
772 serial_out(up, UART_OMAP_SCR, up->scr);
774 serial_out(up, UART_EFR, up->efr);
775 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
776 serial_out(up, UART_MCR, up->mcr);
778 /* Protocol, Baud Rate, and Interrupt Settings */
780 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_DISABLE);
781 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
783 up->efr = serial_in(up, UART_EFR);
784 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
786 serial_out(up, UART_LCR, 0);
787 serial_out(up, UART_IER, 0);
788 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
790 serial_out(up, UART_DLL, quot & 0xff); /* LS of divisor */
791 serial_out(up, UART_DLM, quot >> 8); /* MS of divisor */
793 serial_out(up, UART_LCR, 0);
794 serial_out(up, UART_IER, up->ier);
795 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
797 serial_out(up, UART_EFR, up->efr);
798 serial_out(up, UART_LCR, cval);
800 if (baud > 230400 && baud != 3000000)
801 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_13X_MODE);
803 serial_out(up, UART_OMAP_MDR1, UART_OMAP_MDR1_16X_MODE);
805 /* Hardware Flow Control Configuration */
807 if (termios->c_cflag & CRTSCTS) {
808 efr |= (UART_EFR_CTS | UART_EFR_RTS);
809 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
811 up->mcr = serial_in(up, UART_MCR);
812 serial_out(up, UART_MCR, up->mcr | UART_MCR_TCRTLR);
814 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
815 up->efr = serial_in(up, UART_EFR);
816 serial_out(up, UART_EFR, up->efr | UART_EFR_ECB);
818 serial_out(up, UART_TI752_TCR, OMAP_UART_TCR_TRIG);
819 serial_out(up, UART_EFR, efr); /* Enable AUTORTS and AUTOCTS */
820 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_A);
821 serial_out(up, UART_MCR, up->mcr | UART_MCR_RTS);
822 serial_out(up, UART_LCR, cval);
825 serial_omap_set_mctrl(&up->port, up->port.mctrl);
826 /* Software Flow Control Configuration */
827 serial_omap_configure_xonxoff(up, termios);
829 spin_unlock_irqrestore(&up->port.lock, flags);
830 dev_dbg(up->port.dev, "serial_omap_set_termios+%d\n", up->pdev->id);
834 serial_omap_pm(struct uart_port *port, unsigned int state,
835 unsigned int oldstate)
837 struct uart_omap_port *up = (struct uart_omap_port *)port;
840 dev_dbg(up->port.dev, "serial_omap_pm+%d\n", up->pdev->id);
841 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
842 efr = serial_in(up, UART_EFR);
843 serial_out(up, UART_EFR, efr | UART_EFR_ECB);
844 serial_out(up, UART_LCR, 0);
846 serial_out(up, UART_IER, (state != 0) ? UART_IERX_SLEEP : 0);
847 serial_out(up, UART_LCR, UART_LCR_CONF_MODE_B);
848 serial_out(up, UART_EFR, efr);
849 serial_out(up, UART_LCR, 0);
852 static void serial_omap_release_port(struct uart_port *port)
854 dev_dbg(port->dev, "serial_omap_release_port+\n");
857 static int serial_omap_request_port(struct uart_port *port)
859 dev_dbg(port->dev, "serial_omap_request_port+\n");
863 static void serial_omap_config_port(struct uart_port *port, int flags)
865 struct uart_omap_port *up = (struct uart_omap_port *)port;
867 dev_dbg(up->port.dev, "serial_omap_config_port+%d\n",
869 up->port.type = PORT_OMAP;
873 serial_omap_verify_port(struct uart_port *port, struct serial_struct *ser)
875 /* we don't want the core code to modify any port params */
876 dev_dbg(port->dev, "serial_omap_verify_port+\n");
881 serial_omap_type(struct uart_port *port)
883 struct uart_omap_port *up = (struct uart_omap_port *)port;
885 dev_dbg(up->port.dev, "serial_omap_type+%d\n", up->pdev->id);
889 #define BOTH_EMPTY (UART_LSR_TEMT | UART_LSR_THRE)
891 static inline void wait_for_xmitr(struct uart_omap_port *up)
893 unsigned int status, tmout = 10000;
895 /* Wait up to 10ms for the character(s) to be sent. */
897 status = serial_in(up, UART_LSR);
899 if (status & UART_LSR_BI)
900 up->lsr_break_flag = UART_LSR_BI;
905 } while ((status & BOTH_EMPTY) != BOTH_EMPTY);
907 /* Wait up to 1s for flow control if necessary */
908 if (up->port.flags & UPF_CONS_FLOW) {
910 for (tmout = 1000000; tmout; tmout--) {
911 unsigned int msr = serial_in(up, UART_MSR);
913 up->msr_saved_flags |= msr & MSR_SAVE_FLAGS;
914 if (msr & UART_MSR_CTS)
922 #ifdef CONFIG_CONSOLE_POLL
924 static void serial_omap_poll_put_char(struct uart_port *port, unsigned char ch)
926 struct uart_omap_port *up = (struct uart_omap_port *)port;
928 serial_out(up, UART_TX, ch);
931 static int serial_omap_poll_get_char(struct uart_port *port)
933 struct uart_omap_port *up = (struct uart_omap_port *)port;
934 unsigned int status = serial_in(up, UART_LSR);
936 if (!(status & UART_LSR_DR))
939 return serial_in(up, UART_RX);
942 #endif /* CONFIG_CONSOLE_POLL */
944 #ifdef CONFIG_SERIAL_OMAP_CONSOLE
946 static struct uart_omap_port *serial_omap_console_ports[4];
948 static struct uart_driver serial_omap_reg;
950 static void serial_omap_console_putchar(struct uart_port *port, int ch)
952 struct uart_omap_port *up = (struct uart_omap_port *)port;
955 serial_out(up, UART_TX, ch);
959 serial_omap_console_write(struct console *co, const char *s,
962 struct uart_omap_port *up = serial_omap_console_ports[co->index];
967 local_irq_save(flags);
970 else if (oops_in_progress)
971 locked = spin_trylock(&up->port.lock);
973 spin_lock(&up->port.lock);
976 * First save the IER then disable the interrupts
978 ier = serial_in(up, UART_IER);
979 serial_out(up, UART_IER, 0);
981 uart_console_write(&up->port, s, count, serial_omap_console_putchar);
984 * Finally, wait for transmitter to become empty
985 * and restore the IER
988 serial_out(up, UART_IER, ier);
990 * The receive handling will happen properly because the
991 * receive ready bit will still be set; it is not cleared
992 * on read. However, modem control will not, we must
993 * call it if we have saved something in the saved flags
994 * while processing with interrupts off.
996 if (up->msr_saved_flags)
997 check_modem_status(up);
1000 spin_unlock(&up->port.lock);
1001 local_irq_restore(flags);
1005 serial_omap_console_setup(struct console *co, char *options)
1007 struct uart_omap_port *up;
1013 if (serial_omap_console_ports[co->index] == NULL)
1015 up = serial_omap_console_ports[co->index];
1018 uart_parse_options(options, &baud, &parity, &bits, &flow);
1020 return uart_set_options(&up->port, co, baud, parity, bits, flow);
1023 static struct console serial_omap_console = {
1024 .name = OMAP_SERIAL_NAME,
1025 .write = serial_omap_console_write,
1026 .device = uart_console_device,
1027 .setup = serial_omap_console_setup,
1028 .flags = CON_PRINTBUFFER,
1030 .data = &serial_omap_reg,
1033 static void serial_omap_add_console_port(struct uart_omap_port *up)
1035 serial_omap_console_ports[up->pdev->id] = up;
1038 #define OMAP_CONSOLE (&serial_omap_console)
1042 #define OMAP_CONSOLE NULL
1044 static inline void serial_omap_add_console_port(struct uart_omap_port *up)
1049 static struct uart_ops serial_omap_pops = {
1050 .tx_empty = serial_omap_tx_empty,
1051 .set_mctrl = serial_omap_set_mctrl,
1052 .get_mctrl = serial_omap_get_mctrl,
1053 .stop_tx = serial_omap_stop_tx,
1054 .start_tx = serial_omap_start_tx,
1055 .stop_rx = serial_omap_stop_rx,
1056 .enable_ms = serial_omap_enable_ms,
1057 .break_ctl = serial_omap_break_ctl,
1058 .startup = serial_omap_startup,
1059 .shutdown = serial_omap_shutdown,
1060 .set_termios = serial_omap_set_termios,
1061 .pm = serial_omap_pm,
1062 .type = serial_omap_type,
1063 .release_port = serial_omap_release_port,
1064 .request_port = serial_omap_request_port,
1065 .config_port = serial_omap_config_port,
1066 .verify_port = serial_omap_verify_port,
1067 #ifdef CONFIG_CONSOLE_POLL
1068 .poll_put_char = serial_omap_poll_put_char,
1069 .poll_get_char = serial_omap_poll_get_char,
1073 static struct uart_driver serial_omap_reg = {
1074 .owner = THIS_MODULE,
1075 .driver_name = "OMAP-SERIAL",
1076 .dev_name = OMAP_SERIAL_NAME,
1077 .nr = OMAP_MAX_HSUART_PORTS,
1078 .cons = OMAP_CONSOLE,
1082 serial_omap_suspend(struct platform_device *pdev, pm_message_t state)
1084 struct uart_omap_port *up = platform_get_drvdata(pdev);
1087 uart_suspend_port(&serial_omap_reg, &up->port);
1091 static int serial_omap_resume(struct platform_device *dev)
1093 struct uart_omap_port *up = platform_get_drvdata(dev);
1096 uart_resume_port(&serial_omap_reg, &up->port);
1100 static void serial_omap_rx_timeout(unsigned long uart_no)
1102 struct uart_omap_port *up = ui[uart_no];
1103 unsigned int curr_dma_pos, curr_transmitted_size;
1106 curr_dma_pos = omap_get_dma_dst_pos(up->uart_dma.rx_dma_channel);
1107 if ((curr_dma_pos == up->uart_dma.prev_rx_dma_pos) ||
1108 (curr_dma_pos == 0)) {
1109 if (jiffies_to_msecs(jiffies - up->port_activity) <
1111 mod_timer(&up->uart_dma.rx_timer, jiffies +
1112 usecs_to_jiffies(up->uart_dma.rx_timeout));
1114 serial_omap_stop_rxdma(up);
1115 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1116 serial_out(up, UART_IER, up->ier);
1121 curr_transmitted_size = curr_dma_pos -
1122 up->uart_dma.prev_rx_dma_pos;
1123 up->port.icount.rx += curr_transmitted_size;
1124 tty_insert_flip_string(up->port.state->port.tty,
1125 up->uart_dma.rx_buf +
1126 (up->uart_dma.prev_rx_dma_pos -
1127 up->uart_dma.rx_buf_dma_phys),
1128 curr_transmitted_size);
1129 tty_flip_buffer_push(up->port.state->port.tty);
1130 up->uart_dma.prev_rx_dma_pos = curr_dma_pos;
1131 if (up->uart_dma.rx_buf_size +
1132 up->uart_dma.rx_buf_dma_phys == curr_dma_pos) {
1133 ret = serial_omap_start_rxdma(up);
1135 serial_omap_stop_rxdma(up);
1136 up->ier |= (UART_IER_RDI | UART_IER_RLSI);
1137 serial_out(up, UART_IER, up->ier);
1140 mod_timer(&up->uart_dma.rx_timer, jiffies +
1141 usecs_to_jiffies(up->uart_dma.rx_timeout));
1143 up->port_activity = jiffies;
1146 static void uart_rx_dma_callback(int lch, u16 ch_status, void *data)
1151 static int serial_omap_start_rxdma(struct uart_omap_port *up)
1155 if (up->uart_dma.rx_dma_channel == -1) {
1156 ret = omap_request_dma(up->uart_dma.uart_dma_rx,
1158 (void *)uart_rx_dma_callback, up,
1159 &(up->uart_dma.rx_dma_channel));
1163 omap_set_dma_src_params(up->uart_dma.rx_dma_channel, 0,
1164 OMAP_DMA_AMODE_CONSTANT,
1165 up->uart_dma.uart_base, 0, 0);
1166 omap_set_dma_dest_params(up->uart_dma.rx_dma_channel, 0,
1167 OMAP_DMA_AMODE_POST_INC,
1168 up->uart_dma.rx_buf_dma_phys, 0, 0);
1169 omap_set_dma_transfer_params(up->uart_dma.rx_dma_channel,
1170 OMAP_DMA_DATA_TYPE_S8,
1171 up->uart_dma.rx_buf_size, 1,
1172 OMAP_DMA_SYNC_ELEMENT,
1173 up->uart_dma.uart_dma_rx, 0);
1175 up->uart_dma.prev_rx_dma_pos = up->uart_dma.rx_buf_dma_phys;
1176 /* FIXME: Cache maintenance needed here? */
1177 omap_start_dma(up->uart_dma.rx_dma_channel);
1178 mod_timer(&up->uart_dma.rx_timer, jiffies +
1179 usecs_to_jiffies(up->uart_dma.rx_timeout));
1180 up->uart_dma.rx_dma_used = true;
1184 static void serial_omap_continue_tx(struct uart_omap_port *up)
1186 struct circ_buf *xmit = &up->port.state->xmit;
1187 unsigned int start = up->uart_dma.tx_buf_dma_phys
1188 + (xmit->tail & (UART_XMIT_SIZE - 1));
1190 if (uart_circ_empty(xmit))
1193 up->uart_dma.tx_buf_size = uart_circ_chars_pending(xmit);
1195 * It is a circular buffer. See if the buffer has wounded back.
1196 * If yes it will have to be transferred in two separate dma
1199 if (start + up->uart_dma.tx_buf_size >=
1200 up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE)
1201 up->uart_dma.tx_buf_size =
1202 (up->uart_dma.tx_buf_dma_phys + UART_XMIT_SIZE) - start;
1203 omap_set_dma_dest_params(up->uart_dma.tx_dma_channel, 0,
1204 OMAP_DMA_AMODE_CONSTANT,
1205 up->uart_dma.uart_base, 0, 0);
1206 omap_set_dma_src_params(up->uart_dma.tx_dma_channel, 0,
1207 OMAP_DMA_AMODE_POST_INC, start, 0, 0);
1208 omap_set_dma_transfer_params(up->uart_dma.tx_dma_channel,
1209 OMAP_DMA_DATA_TYPE_S8,
1210 up->uart_dma.tx_buf_size, 1,
1211 OMAP_DMA_SYNC_ELEMENT,
1212 up->uart_dma.uart_dma_tx, 0);
1213 /* FIXME: Cache maintenance needed here? */
1214 omap_start_dma(up->uart_dma.tx_dma_channel);
1217 static void uart_tx_dma_callback(int lch, u16 ch_status, void *data)
1219 struct uart_omap_port *up = (struct uart_omap_port *)data;
1220 struct circ_buf *xmit = &up->port.state->xmit;
1222 xmit->tail = (xmit->tail + up->uart_dma.tx_buf_size) & \
1223 (UART_XMIT_SIZE - 1);
1224 up->port.icount.tx += up->uart_dma.tx_buf_size;
1226 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
1227 uart_write_wakeup(&up->port);
1229 if (uart_circ_empty(xmit)) {
1230 spin_lock(&(up->uart_dma.tx_lock));
1231 serial_omap_stop_tx(&up->port);
1232 up->uart_dma.tx_dma_used = false;
1233 spin_unlock(&(up->uart_dma.tx_lock));
1235 omap_stop_dma(up->uart_dma.tx_dma_channel);
1236 serial_omap_continue_tx(up);
1238 up->port_activity = jiffies;
1242 static int serial_omap_probe(struct platform_device *pdev)
1244 struct uart_omap_port *up;
1245 struct resource *mem, *irq, *dma_tx, *dma_rx;
1246 struct omap_uart_port_info *omap_up_info = pdev->dev.platform_data;
1249 mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1251 dev_err(&pdev->dev, "no mem resource?\n");
1255 irq = platform_get_resource(pdev, IORESOURCE_IRQ, 0);
1257 dev_err(&pdev->dev, "no irq resource?\n");
1261 if (!request_mem_region(mem->start, resource_size(mem),
1262 pdev->dev.driver->name)) {
1263 dev_err(&pdev->dev, "memory region already claimed\n");
1267 dma_rx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "rx");
1273 dma_tx = platform_get_resource_byname(pdev, IORESOURCE_DMA, "tx");
1279 up = kzalloc(sizeof(*up), GFP_KERNEL);
1282 goto do_release_region;
1284 sprintf(up->name, "OMAP UART%d", pdev->id);
1286 up->port.dev = &pdev->dev;
1287 up->port.type = PORT_OMAP;
1288 up->port.iotype = UPIO_MEM;
1289 up->port.irq = irq->start;
1291 up->port.regshift = 2;
1292 up->port.fifosize = 64;
1293 up->port.ops = &serial_omap_pops;
1294 up->port.line = pdev->id;
1296 up->port.membase = omap_up_info->membase;
1297 up->port.mapbase = omap_up_info->mapbase;
1298 up->port.flags = omap_up_info->flags;
1299 up->port.irqflags = omap_up_info->irqflags;
1300 up->port.uartclk = omap_up_info->uartclk;
1301 up->uart_dma.uart_base = mem->start;
1303 if (omap_up_info->dma_enabled) {
1304 up->uart_dma.uart_dma_tx = dma_tx->start;
1305 up->uart_dma.uart_dma_rx = dma_rx->start;
1307 up->uart_dma.rx_buf_size = 4096;
1308 up->uart_dma.rx_timeout = 2;
1309 spin_lock_init(&(up->uart_dma.tx_lock));
1310 spin_lock_init(&(up->uart_dma.rx_lock));
1311 up->uart_dma.tx_dma_channel = OMAP_UART_DMA_CH_FREE;
1312 up->uart_dma.rx_dma_channel = OMAP_UART_DMA_CH_FREE;
1316 serial_omap_add_console_port(up);
1318 ret = uart_add_one_port(&serial_omap_reg, &up->port);
1320 goto do_release_region;
1322 platform_set_drvdata(pdev, up);
1325 dev_err(&pdev->dev, "[UART%d]: failure [%s]: %d\n",
1326 pdev->id, __func__, ret);
1328 release_mem_region(mem->start, resource_size(mem));
1332 static int serial_omap_remove(struct platform_device *dev)
1334 struct uart_omap_port *up = platform_get_drvdata(dev);
1336 platform_set_drvdata(dev, NULL);
1338 uart_remove_one_port(&serial_omap_reg, &up->port);
1344 static struct platform_driver serial_omap_driver = {
1345 .probe = serial_omap_probe,
1346 .remove = serial_omap_remove,
1348 .suspend = serial_omap_suspend,
1349 .resume = serial_omap_resume,
1351 .name = DRIVER_NAME,
1355 static int __init serial_omap_init(void)
1359 ret = uart_register_driver(&serial_omap_reg);
1362 ret = platform_driver_register(&serial_omap_driver);
1364 uart_unregister_driver(&serial_omap_reg);
1368 static void __exit serial_omap_exit(void)
1370 platform_driver_unregister(&serial_omap_driver);
1371 uart_unregister_driver(&serial_omap_reg);
1374 module_init(serial_omap_init);
1375 module_exit(serial_omap_exit);
1377 MODULE_DESCRIPTION("OMAP High Speed UART driver");
1378 MODULE_LICENSE("GPL");
1379 MODULE_AUTHOR("Texas Instruments Inc");