Linux 3.12.39
[linux/fpc-iii.git] / drivers / tty / serial / 8250 / 8250_dma.c
blob148ffe4c232f6bd8322d3387d41f227d5d811263
1 /*
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>
16 #include "8250.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;
23 unsigned long flags;
25 dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
26 UART_XMIT_SIZE, DMA_TO_DEVICE);
28 spin_lock_irqsave(&p->port.lock, flags);
30 dma->tx_running = 0;
32 xmit->tail += dma->tx_size;
33 xmit->tail &= UART_XMIT_SIZE - 1;
34 p->port.icount.tx += dma->tx_size;
36 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
37 uart_write_wakeup(&p->port);
39 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port))
40 serial8250_tx_dma(p);
42 spin_unlock_irqrestore(&p->port.lock, flags);
45 static void __dma_rx_complete(void *param)
47 struct uart_8250_port *p = param;
48 struct uart_8250_dma *dma = p->dma;
49 struct tty_port *tty_port = &p->port.state->port;
50 struct dma_tx_state state;
51 int count;
53 dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr,
54 dma->rx_size, DMA_FROM_DEVICE);
56 dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
57 dmaengine_terminate_all(dma->rxchan);
59 count = dma->rx_size - state.residue;
61 tty_insert_flip_string(tty_port, dma->rx_buf, count);
62 p->port.icount.rx += count;
64 tty_flip_buffer_push(tty_port);
67 int serial8250_tx_dma(struct uart_8250_port *p)
69 struct uart_8250_dma *dma = p->dma;
70 struct circ_buf *xmit = &p->port.state->xmit;
71 struct dma_async_tx_descriptor *desc;
73 if (uart_tx_stopped(&p->port) || dma->tx_running ||
74 uart_circ_empty(xmit))
75 return 0;
77 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
79 desc = dmaengine_prep_slave_single(dma->txchan,
80 dma->tx_addr + xmit->tail,
81 dma->tx_size, DMA_MEM_TO_DEV,
82 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
83 if (!desc)
84 return -EBUSY;
86 dma->tx_running = 1;
88 desc->callback = __dma_tx_complete;
89 desc->callback_param = p;
91 dma->tx_cookie = dmaengine_submit(desc);
93 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
94 UART_XMIT_SIZE, DMA_TO_DEVICE);
96 dma_async_issue_pending(dma->txchan);
98 return 0;
100 EXPORT_SYMBOL_GPL(serial8250_tx_dma);
102 int serial8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
104 struct uart_8250_dma *dma = p->dma;
105 struct dma_async_tx_descriptor *desc;
106 struct dma_tx_state state;
107 int dma_status;
109 dma_status = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
111 switch (iir & 0x3f) {
112 case UART_IIR_RLSI:
113 /* 8250_core handles errors and break interrupts */
114 return -EIO;
115 case UART_IIR_RX_TIMEOUT:
117 * If RCVR FIFO trigger level was not reached, complete the
118 * transfer and let 8250_core copy the remaining data.
120 if (dma_status == DMA_IN_PROGRESS) {
121 dmaengine_pause(dma->rxchan);
122 __dma_rx_complete(p);
124 return -ETIMEDOUT;
125 default:
126 break;
129 if (dma_status)
130 return 0;
132 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
133 dma->rx_size, DMA_DEV_TO_MEM,
134 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
135 if (!desc)
136 return -EBUSY;
138 desc->callback = __dma_rx_complete;
139 desc->callback_param = p;
141 dma->rx_cookie = dmaengine_submit(desc);
143 dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr,
144 dma->rx_size, DMA_FROM_DEVICE);
146 dma_async_issue_pending(dma->rxchan);
148 return 0;
150 EXPORT_SYMBOL_GPL(serial8250_rx_dma);
152 int serial8250_request_dma(struct uart_8250_port *p)
154 struct uart_8250_dma *dma = p->dma;
155 dma_cap_mask_t mask;
157 /* Default slave configuration parameters */
158 dma->rxconf.direction = DMA_DEV_TO_MEM;
159 dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
160 dma->rxconf.src_addr = p->port.mapbase + UART_RX;
162 dma->txconf.direction = DMA_MEM_TO_DEV;
163 dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
164 dma->txconf.dst_addr = p->port.mapbase + UART_TX;
166 dma_cap_zero(mask);
167 dma_cap_set(DMA_SLAVE, mask);
169 /* Get a channel for RX */
170 dma->rxchan = dma_request_slave_channel_compat(mask,
171 dma->fn, dma->rx_param,
172 p->port.dev, "rx");
173 if (!dma->rxchan)
174 return -ENODEV;
176 dmaengine_slave_config(dma->rxchan, &dma->rxconf);
178 /* Get a channel for TX */
179 dma->txchan = dma_request_slave_channel_compat(mask,
180 dma->fn, dma->tx_param,
181 p->port.dev, "tx");
182 if (!dma->txchan) {
183 dma_release_channel(dma->rxchan);
184 return -ENODEV;
187 dmaengine_slave_config(dma->txchan, &dma->txconf);
189 /* RX buffer */
190 if (!dma->rx_size)
191 dma->rx_size = PAGE_SIZE;
193 dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
194 &dma->rx_addr, GFP_KERNEL);
195 if (!dma->rx_buf)
196 goto err;
198 /* TX buffer */
199 dma->tx_addr = dma_map_single(dma->txchan->device->dev,
200 p->port.state->xmit.buf,
201 UART_XMIT_SIZE,
202 DMA_TO_DEVICE);
203 if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) {
204 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size,
205 dma->rx_buf, dma->rx_addr);
206 goto err;
209 dev_dbg_ratelimited(p->port.dev, "got both dma channels\n");
211 return 0;
212 err:
213 dma_release_channel(dma->rxchan);
214 dma_release_channel(dma->txchan);
216 return -ENOMEM;
218 EXPORT_SYMBOL_GPL(serial8250_request_dma);
220 void serial8250_release_dma(struct uart_8250_port *p)
222 struct uart_8250_dma *dma = p->dma;
224 if (!dma)
225 return;
227 /* Release RX resources */
228 dmaengine_terminate_all(dma->rxchan);
229 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
230 dma->rx_addr);
231 dma_release_channel(dma->rxchan);
232 dma->rxchan = NULL;
234 /* Release TX resources */
235 dmaengine_terminate_all(dma->txchan);
236 dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
237 UART_XMIT_SIZE, DMA_TO_DEVICE);
238 dma_release_channel(dma->txchan);
239 dma->txchan = NULL;
240 dma->tx_running = 0;
242 dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
244 EXPORT_SYMBOL_GPL(serial8250_release_dma);