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 spin_lock_irqsave(&p->port.lock, flags);
33 xmit->tail += dma->tx_size;
34 xmit->tail &= UART_XMIT_SIZE - 1;
35 p->port.icount.tx += dma->tx_size;
37 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
38 uart_write_wakeup(&p->port);
40 ret = serial8250_tx_dma(p);
42 p->ier |= UART_IER_THRI;
43 serial_port_out(&p->port, UART_IER, p->ier);
46 spin_unlock_irqrestore(&p->port.lock, flags);
49 static void __dma_rx_complete(void *param)
51 struct uart_8250_port *p = param;
52 struct uart_8250_dma *dma = p->dma;
53 struct tty_port *tty_port = &p->port.state->port;
54 struct dma_tx_state state;
57 dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr,
58 dma->rx_size, DMA_FROM_DEVICE);
61 dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
63 count = dma->rx_size - state.residue;
65 tty_insert_flip_string(tty_port, dma->rx_buf, count);
66 p->port.icount.rx += count;
68 tty_flip_buffer_push(tty_port);
71 int serial8250_tx_dma(struct uart_8250_port *p)
73 struct uart_8250_dma *dma = p->dma;
74 struct circ_buf *xmit = &p->port.state->xmit;
75 struct dma_async_tx_descriptor *desc;
78 if (uart_tx_stopped(&p->port) || dma->tx_running ||
79 uart_circ_empty(xmit))
82 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
83 if (dma->tx_size < p->port.fifosize) {
88 desc = dmaengine_prep_slave_single(dma->txchan,
89 dma->tx_addr + xmit->tail,
90 dma->tx_size, DMA_MEM_TO_DEV,
91 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
98 desc->callback = __dma_tx_complete;
99 desc->callback_param = p;
101 dma->tx_cookie = dmaengine_submit(desc);
103 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
104 UART_XMIT_SIZE, DMA_TO_DEVICE);
106 dma_async_issue_pending(dma->txchan);
109 if (p->ier & UART_IER_THRI) {
110 p->ier &= ~UART_IER_THRI;
111 serial_out(p, UART_IER, p->ier);
120 int serial8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
122 struct uart_8250_dma *dma = p->dma;
123 struct dma_async_tx_descriptor *desc;
125 switch (iir & 0x3f) {
127 /* 8250_core handles errors and break interrupts */
129 case UART_IIR_RX_TIMEOUT:
131 * If RCVR FIFO trigger level was not reached, complete the
132 * transfer and let 8250_core copy the remaining data.
134 if (dma->rx_running) {
135 dmaengine_pause(dma->rxchan);
136 __dma_rx_complete(p);
137 dmaengine_terminate_all(dma->rxchan);
147 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
148 dma->rx_size, DMA_DEV_TO_MEM,
149 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
154 desc->callback = __dma_rx_complete;
155 desc->callback_param = p;
157 dma->rx_cookie = dmaengine_submit(desc);
159 dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr,
160 dma->rx_size, DMA_FROM_DEVICE);
162 dma_async_issue_pending(dma->rxchan);
167 int serial8250_request_dma(struct uart_8250_port *p)
169 struct uart_8250_dma *dma = p->dma;
172 /* Default slave configuration parameters */
173 dma->rxconf.direction = DMA_DEV_TO_MEM;
174 dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
175 dma->rxconf.src_addr = p->port.mapbase + UART_RX;
177 dma->txconf.direction = DMA_MEM_TO_DEV;
178 dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
179 dma->txconf.dst_addr = p->port.mapbase + UART_TX;
182 dma_cap_set(DMA_SLAVE, mask);
184 /* Get a channel for RX */
185 dma->rxchan = dma_request_slave_channel_compat(mask,
186 dma->fn, dma->rx_param,
191 dmaengine_slave_config(dma->rxchan, &dma->rxconf);
193 /* Get a channel for TX */
194 dma->txchan = dma_request_slave_channel_compat(mask,
195 dma->fn, dma->tx_param,
198 dma_release_channel(dma->rxchan);
202 dmaengine_slave_config(dma->txchan, &dma->txconf);
206 dma->rx_size = PAGE_SIZE;
208 dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
209 &dma->rx_addr, GFP_KERNEL);
214 dma->tx_addr = dma_map_single(dma->txchan->device->dev,
215 p->port.state->xmit.buf,
218 if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) {
219 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size,
220 dma->rx_buf, dma->rx_addr);
224 dev_dbg_ratelimited(p->port.dev, "got both dma channels\n");
228 dma_release_channel(dma->rxchan);
229 dma_release_channel(dma->txchan);
233 EXPORT_SYMBOL_GPL(serial8250_request_dma);
235 void serial8250_release_dma(struct uart_8250_port *p)
237 struct uart_8250_dma *dma = p->dma;
242 /* Release RX resources */
243 dmaengine_terminate_all(dma->rxchan);
244 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
246 dma_release_channel(dma->rxchan);
249 /* Release TX resources */
250 dmaengine_terminate_all(dma->txchan);
251 dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
252 UART_XMIT_SIZE, DMA_TO_DEVICE);
253 dma_release_channel(dma->txchan);
257 dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
259 EXPORT_SYMBOL_GPL(serial8250_release_dma);