]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - drivers/tty/serial/8250/8250_dma.c
tty: serial: 8250: allow to use custom DMA implementation
[mirror_ubuntu-bionic-kernel.git] / drivers / tty / serial / 8250 / 8250_dma.c
1 /*
2 * 8250_dma.c - DMA Engine API support for 8250.c
3 *
4 * Copyright (C) 2013 Intel Corporation
5 *
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.
10 */
11 #include <linux/tty.h>
12 #include <linux/tty_flip.h>
13 #include <linux/serial_reg.h>
14 #include <linux/dma-mapping.h>
15
16 #include "8250.h"
17
18 static void __dma_tx_complete(void *param)
19 {
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;
24
25 dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
26 UART_XMIT_SIZE, DMA_TO_DEVICE);
27
28 spin_lock_irqsave(&p->port.lock, flags);
29
30 dma->tx_running = 0;
31
32 xmit->tail += dma->tx_size;
33 xmit->tail &= UART_XMIT_SIZE - 1;
34 p->port.icount.tx += dma->tx_size;
35
36 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
37 uart_write_wakeup(&p->port);
38
39 if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) {
40 int ret;
41
42 ret = serial8250_tx_dma(p);
43 if (ret) {
44 dma->tx_err = 1;
45 p->ier |= UART_IER_THRI;
46 serial_port_out(&p->port, UART_IER, p->ier);
47 }
48 }
49
50 spin_unlock_irqrestore(&p->port.lock, flags);
51 }
52
53 static void __dma_rx_complete(void *param)
54 {
55 struct uart_8250_port *p = param;
56 struct uart_8250_dma *dma = p->dma;
57 struct tty_port *tty_port = &p->port.state->port;
58 struct dma_tx_state state;
59 int count;
60
61 dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr,
62 dma->rx_size, DMA_FROM_DEVICE);
63
64 dma->rx_running = 0;
65 dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
66 dmaengine_terminate_all(dma->rxchan);
67
68 count = dma->rx_size - state.residue;
69
70 tty_insert_flip_string(tty_port, dma->rx_buf, count);
71 p->port.icount.rx += count;
72
73 tty_flip_buffer_push(tty_port);
74 }
75
76 int serial8250_tx_dma(struct uart_8250_port *p)
77 {
78 struct uart_8250_dma *dma = p->dma;
79 struct circ_buf *xmit = &p->port.state->xmit;
80 struct dma_async_tx_descriptor *desc;
81 int ret;
82
83 if (uart_tx_stopped(&p->port) || dma->tx_running ||
84 uart_circ_empty(xmit))
85 return 0;
86
87 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
88
89 desc = dmaengine_prep_slave_single(dma->txchan,
90 dma->tx_addr + xmit->tail,
91 dma->tx_size, DMA_MEM_TO_DEV,
92 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
93 if (!desc) {
94 ret = -EBUSY;
95 goto err;
96 }
97
98 dma->tx_running = 1;
99
100 desc->callback = __dma_tx_complete;
101 desc->callback_param = p;
102
103 dma->tx_cookie = dmaengine_submit(desc);
104
105 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
106 UART_XMIT_SIZE, DMA_TO_DEVICE);
107
108 dma_async_issue_pending(dma->txchan);
109 if (dma->tx_err) {
110 dma->tx_err = 0;
111 if (p->ier & UART_IER_THRI) {
112 p->ier &= ~UART_IER_THRI;
113 serial_out(p, UART_IER, p->ier);
114 }
115 }
116 return 0;
117 err:
118 dma->tx_err = 1;
119 return ret;
120 }
121
122 int serial8250_rx_dma(struct uart_8250_port *p, unsigned int iir)
123 {
124 struct uart_8250_dma *dma = p->dma;
125 struct dma_async_tx_descriptor *desc;
126
127 switch (iir & 0x3f) {
128 case UART_IIR_RLSI:
129 /* 8250_core handles errors and break interrupts */
130 return -EIO;
131 case UART_IIR_RX_TIMEOUT:
132 /*
133 * If RCVR FIFO trigger level was not reached, complete the
134 * transfer and let 8250_core copy the remaining data.
135 */
136 if (dma->rx_running) {
137 dmaengine_pause(dma->rxchan);
138 __dma_rx_complete(p);
139 }
140 return -ETIMEDOUT;
141 default:
142 break;
143 }
144
145 if (dma->rx_running)
146 return 0;
147
148 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
149 dma->rx_size, DMA_DEV_TO_MEM,
150 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
151 if (!desc)
152 return -EBUSY;
153
154 dma->rx_running = 1;
155 desc->callback = __dma_rx_complete;
156 desc->callback_param = p;
157
158 dma->rx_cookie = dmaengine_submit(desc);
159
160 dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr,
161 dma->rx_size, DMA_FROM_DEVICE);
162
163 dma_async_issue_pending(dma->rxchan);
164
165 return 0;
166 }
167
168 int serial8250_request_dma(struct uart_8250_port *p)
169 {
170 struct uart_8250_dma *dma = p->dma;
171 dma_cap_mask_t mask;
172
173 /* Default slave configuration parameters */
174 dma->rxconf.direction = DMA_DEV_TO_MEM;
175 dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
176 dma->rxconf.src_addr = p->port.mapbase + UART_RX;
177
178 dma->txconf.direction = DMA_MEM_TO_DEV;
179 dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
180 dma->txconf.dst_addr = p->port.mapbase + UART_TX;
181
182 dma_cap_zero(mask);
183 dma_cap_set(DMA_SLAVE, mask);
184
185 /* Get a channel for RX */
186 dma->rxchan = dma_request_slave_channel_compat(mask,
187 dma->fn, dma->rx_param,
188 p->port.dev, "rx");
189 if (!dma->rxchan)
190 return -ENODEV;
191
192 dmaengine_slave_config(dma->rxchan, &dma->rxconf);
193
194 /* Get a channel for TX */
195 dma->txchan = dma_request_slave_channel_compat(mask,
196 dma->fn, dma->tx_param,
197 p->port.dev, "tx");
198 if (!dma->txchan) {
199 dma_release_channel(dma->rxchan);
200 return -ENODEV;
201 }
202
203 dmaengine_slave_config(dma->txchan, &dma->txconf);
204
205 /* RX buffer */
206 if (!dma->rx_size)
207 dma->rx_size = PAGE_SIZE;
208
209 dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
210 &dma->rx_addr, GFP_KERNEL);
211 if (!dma->rx_buf)
212 goto err;
213
214 /* TX buffer */
215 dma->tx_addr = dma_map_single(dma->txchan->device->dev,
216 p->port.state->xmit.buf,
217 UART_XMIT_SIZE,
218 DMA_TO_DEVICE);
219 if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) {
220 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size,
221 dma->rx_buf, dma->rx_addr);
222 goto err;
223 }
224
225 dev_dbg_ratelimited(p->port.dev, "got both dma channels\n");
226
227 return 0;
228 err:
229 dma_release_channel(dma->rxchan);
230 dma_release_channel(dma->txchan);
231
232 return -ENOMEM;
233 }
234 EXPORT_SYMBOL_GPL(serial8250_request_dma);
235
236 void serial8250_release_dma(struct uart_8250_port *p)
237 {
238 struct uart_8250_dma *dma = p->dma;
239
240 if (!dma)
241 return;
242
243 /* Release RX resources */
244 dmaengine_terminate_all(dma->rxchan);
245 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
246 dma->rx_addr);
247 dma_release_channel(dma->rxchan);
248 dma->rxchan = NULL;
249
250 /* Release TX resources */
251 dmaengine_terminate_all(dma->txchan);
252 dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
253 UART_XMIT_SIZE, DMA_TO_DEVICE);
254 dma_release_channel(dma->txchan);
255 dma->txchan = NULL;
256 dma->tx_running = 0;
257
258 dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
259 }
260 EXPORT_SYMBOL_GPL(serial8250_release_dma);