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
;
58 dmaengine_tx_status(dma
->rxchan
, dma
->rx_cookie
, &state
);
60 count
= dma
->rx_size
- state
.residue
;
62 tty_insert_flip_string(tty_port
, dma
->rx_buf
, count
);
63 p
->port
.icount
.rx
+= count
;
65 tty_flip_buffer_push(tty_port
);
68 int serial8250_tx_dma(struct uart_8250_port
*p
)
70 struct uart_8250_dma
*dma
= p
->dma
;
71 struct circ_buf
*xmit
= &p
->port
.state
->xmit
;
72 struct dma_async_tx_descriptor
*desc
;
78 if (uart_tx_stopped(&p
->port
) || uart_circ_empty(xmit
)) {
79 /* We have been called from __dma_tx_complete() */
80 serial8250_rpm_put_tx(p
);
84 dma
->tx_size
= CIRC_CNT_TO_END(xmit
->head
, xmit
->tail
, UART_XMIT_SIZE
);
86 desc
= dmaengine_prep_slave_single(dma
->txchan
,
87 dma
->tx_addr
+ xmit
->tail
,
88 dma
->tx_size
, DMA_MEM_TO_DEV
,
89 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
96 desc
->callback
= __dma_tx_complete
;
97 desc
->callback_param
= p
;
99 dma
->tx_cookie
= dmaengine_submit(desc
);
101 dma_sync_single_for_device(dma
->txchan
->device
->dev
, dma
->tx_addr
,
102 UART_XMIT_SIZE
, DMA_TO_DEVICE
);
104 dma_async_issue_pending(dma
->txchan
);
107 if (p
->ier
& UART_IER_THRI
) {
108 p
->ier
&= ~UART_IER_THRI
;
109 serial_out(p
, UART_IER
, p
->ier
);
118 int serial8250_rx_dma(struct uart_8250_port
*p
)
120 struct uart_8250_dma
*dma
= p
->dma
;
121 struct dma_async_tx_descriptor
*desc
;
126 desc
= dmaengine_prep_slave_single(dma
->rxchan
, dma
->rx_addr
,
127 dma
->rx_size
, DMA_DEV_TO_MEM
,
128 DMA_PREP_INTERRUPT
| DMA_CTRL_ACK
);
133 desc
->callback
= __dma_rx_complete
;
134 desc
->callback_param
= p
;
136 dma
->rx_cookie
= dmaengine_submit(desc
);
138 dma_async_issue_pending(dma
->rxchan
);
143 void serial8250_rx_dma_flush(struct uart_8250_port
*p
)
145 struct uart_8250_dma
*dma
= p
->dma
;
147 if (dma
->rx_running
) {
148 dmaengine_pause(dma
->rxchan
);
149 __dma_rx_complete(p
);
150 dmaengine_terminate_async(dma
->rxchan
);
153 EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush
);
155 int serial8250_request_dma(struct uart_8250_port
*p
)
157 struct uart_8250_dma
*dma
= p
->dma
;
158 phys_addr_t rx_dma_addr
= dma
->rx_dma_addr
?
159 dma
->rx_dma_addr
: p
->port
.mapbase
;
160 phys_addr_t tx_dma_addr
= dma
->tx_dma_addr
?
161 dma
->tx_dma_addr
: p
->port
.mapbase
;
163 struct dma_slave_caps caps
;
166 /* Default slave configuration parameters */
167 dma
->rxconf
.direction
= DMA_DEV_TO_MEM
;
168 dma
->rxconf
.src_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
169 dma
->rxconf
.src_addr
= rx_dma_addr
+ UART_RX
;
171 dma
->txconf
.direction
= DMA_MEM_TO_DEV
;
172 dma
->txconf
.dst_addr_width
= DMA_SLAVE_BUSWIDTH_1_BYTE
;
173 dma
->txconf
.dst_addr
= tx_dma_addr
+ UART_TX
;
176 dma_cap_set(DMA_SLAVE
, mask
);
178 /* Get a channel for RX */
179 dma
->rxchan
= dma_request_slave_channel_compat(mask
,
180 dma
->fn
, dma
->rx_param
,
185 /* 8250 rx dma requires dmaengine driver to support pause/terminate */
186 ret
= dma_get_slave_caps(dma
->rxchan
, &caps
);
189 if (!caps
.cmd_pause
|| !caps
.cmd_terminate
||
190 caps
.residue_granularity
== DMA_RESIDUE_GRANULARITY_DESCRIPTOR
) {
195 dmaengine_slave_config(dma
->rxchan
, &dma
->rxconf
);
197 /* Get a channel for TX */
198 dma
->txchan
= dma_request_slave_channel_compat(mask
,
199 dma
->fn
, dma
->tx_param
,
206 /* 8250 tx dma requires dmaengine driver to support terminate */
207 ret
= dma_get_slave_caps(dma
->txchan
, &caps
);
210 if (!caps
.cmd_terminate
) {
215 dmaengine_slave_config(dma
->txchan
, &dma
->txconf
);
219 dma
->rx_size
= PAGE_SIZE
;
221 dma
->rx_buf
= dma_alloc_coherent(dma
->rxchan
->device
->dev
, dma
->rx_size
,
222 &dma
->rx_addr
, GFP_KERNEL
);
229 dma
->tx_addr
= dma_map_single(dma
->txchan
->device
->dev
,
230 p
->port
.state
->xmit
.buf
,
233 if (dma_mapping_error(dma
->txchan
->device
->dev
, dma
->tx_addr
)) {
234 dma_free_coherent(dma
->rxchan
->device
->dev
, dma
->rx_size
,
235 dma
->rx_buf
, dma
->rx_addr
);
240 dev_dbg_ratelimited(p
->port
.dev
, "got both dma channels\n");
244 dma_release_channel(dma
->txchan
);
246 dma_release_channel(dma
->rxchan
);
249 EXPORT_SYMBOL_GPL(serial8250_request_dma
);
251 void serial8250_release_dma(struct uart_8250_port
*p
)
253 struct uart_8250_dma
*dma
= p
->dma
;
258 /* Release RX resources */
259 dmaengine_terminate_sync(dma
->rxchan
);
260 dma_free_coherent(dma
->rxchan
->device
->dev
, dma
->rx_size
, dma
->rx_buf
,
262 dma_release_channel(dma
->rxchan
);
265 /* Release TX resources */
266 dmaengine_terminate_sync(dma
->txchan
);
267 dma_unmap_single(dma
->txchan
->device
->dev
, dma
->tx_addr
,
268 UART_XMIT_SIZE
, DMA_TO_DEVICE
);
269 dma_release_channel(dma
->txchan
);
273 dev_dbg_ratelimited(p
->port
.dev
, "dma channels released\n");
275 EXPORT_SYMBOL_GPL(serial8250_release_dma
);