2 * 8250_dma.c - DMA Engine API support for 8250.c
4 * Copyright (C) 2013 Intel Corporation
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 #include <linux/tty.h>
12 #include <linux/tty_flip.h>
13 #include <linux/serial_reg.h>
14 #include <linux/dma-mapping.h>
18 static void __dma_tx_complete(void *param)
20 struct uart_8250_port *p = param;
21 struct uart_8250_dma *dma = p->dma;
22 struct circ_buf *xmit = &p->port.state->xmit;
26 dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
27 UART_XMIT_SIZE, DMA_TO_DEVICE);
29 xmit->tail += dma->tx_size;
30 xmit->tail &= UART_XMIT_SIZE - 1;
31 p->port.icount.tx += dma->tx_size;
33 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
34 uart_write_wakeup(&p->port);
36 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
38 uart_write_wakeup(&p->port);
42 static void __dma_rx_complete(void *param)
44 struct uart_8250_port *p = param;
45 struct uart_8250_dma *dma = p->dma;
46 struct tty_port *tty_port = &p->port.state->port;
47 struct dma_tx_state state;
50 dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr,
51 dma->rx_size, DMA_FROM_DEVICE);
53 dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
54 dmaengine_terminate_all(dma->rxchan);
56 count = dma->rx_size - state.residue;
58 tty_insert_flip_string(tty_port, dma->rx_buf, count);
59 p->port.icount.rx += count;
61 tty_flip_buffer_push(tty_port);
64 int serial8250_tx_dma(struct uart_8250_port *p)
66 struct uart_8250_dma *dma = p->dma;
67 struct circ_buf *xmit = &p->port.state->xmit;
68 struct dma_async_tx_descriptor *desc;
73 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
77 desc = dmaengine_prep_slave_single(dma->txchan,
78 dma->tx_addr + xmit->tail,
79 dma->tx_size, DMA_MEM_TO_DEV,
80 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
86 desc->callback = __dma_tx_complete;
87 desc->callback_param = p;
89 dma->tx_cookie = dmaengine_submit(desc);
91 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
92 UART_XMIT_SIZE, DMA_TO_DEVICE);
94 dma_async_issue_pending(dma->txchan);
98 EXPORT_SYMBOL_GPL(serial8250_tx_dma);
100 int serial8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
102 struct uart_8250_dma *dma = p->dma;
103 struct dma_async_tx_descriptor *desc;
104 struct dma_tx_state state;
108 * If RCVR FIFO trigger level was not reached, complete the transfer and
109 * let 8250.c copy the remaining data.
111 if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) {
112 dma_status = dmaengine_tx_status(dma->rxchan, dma->rx_cookie,
114 if (dma_status == DMA_IN_PROGRESS) {
115 dmaengine_pause(dma->rxchan);
116 __dma_rx_complete(p);
121 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
122 dma->rx_size, DMA_DEV_TO_MEM,
123 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
127 desc->callback = __dma_rx_complete;
128 desc->callback_param = p;
130 dma->rx_cookie = dmaengine_submit(desc);
132 dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr,
133 dma->rx_size, DMA_FROM_DEVICE);
135 dma_async_issue_pending(dma->rxchan);
139 EXPORT_SYMBOL_GPL(serial8250_rx_dma);
141 int serial8250_request_dma(struct uart_8250_port *p)
143 struct uart_8250_dma *dma = p->dma;
146 dma->rxconf.src_addr = p->port.mapbase + UART_RX;
147 dma->txconf.dst_addr = p->port.mapbase + UART_TX;
150 dma_cap_set(DMA_SLAVE, mask);
152 /* Get a channel for RX */
153 dma->rxchan = dma_request_channel(mask, dma->fn, dma->rx_param);
157 dmaengine_slave_config(dma->rxchan, &dma->rxconf);
159 /* Get a channel for TX */
160 dma->txchan = dma_request_channel(mask, dma->fn, dma->tx_param);
162 dma_release_channel(dma->rxchan);
166 dmaengine_slave_config(dma->txchan, &dma->txconf);
170 dma->rx_size = PAGE_SIZE;
172 dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
173 &dma->rx_addr, GFP_KERNEL);
175 dma_release_channel(dma->rxchan);
176 dma_release_channel(dma->txchan);
181 dma->tx_addr = dma_map_single(dma->txchan->device->dev,
182 p->port.state->xmit.buf,
186 dev_dbg_ratelimited(p->port.dev, "got both dma channels\n");
190 EXPORT_SYMBOL_GPL(serial8250_request_dma);
192 void serial8250_release_dma(struct uart_8250_port *p)
194 struct uart_8250_dma *dma = p->dma;
199 /* Release RX resources */
200 dmaengine_terminate_all(dma->rxchan);
201 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
203 dma_release_channel(dma->rxchan);
206 /* Release TX resources */
207 dmaengine_terminate_all(dma->txchan);
208 dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
209 UART_XMIT_SIZE, DMA_TO_DEVICE);
210 dma_release_channel(dma->txchan);
214 dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
216 EXPORT_SYMBOL_GPL(serial8250_release_dma);