1 // SPDX-License-Identifier: GPL-2.0+
3 * 8250_dma.c - DMA Engine API support for 8250.c
5 * Copyright (C) 2013 Intel Corporation
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 #include <linux/tty.h>
13 #include <linux/tty_flip.h>
14 #include <linux/serial_reg.h>
15 #include <linux/dma-mapping.h>
19 static void __dma_tx_complete(void *param)
21 struct uart_8250_port *p = param;
22 struct uart_8250_dma *dma = p->dma;
23 struct circ_buf *xmit = &p->port.state->xmit;
27 dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
28 UART_XMIT_SIZE, DMA_TO_DEVICE);
30 spin_lock_irqsave(&p->port.lock, flags);
34 xmit->tail += dma->tx_size;
35 xmit->tail &= UART_XMIT_SIZE - 1;
36 p->port.icount.tx += dma->tx_size;
38 if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
39 uart_write_wakeup(&p->port);
41 ret = serial8250_tx_dma(p);
43 p->ier |= UART_IER_THRI;
44 serial_port_out(&p->port, UART_IER, p->ier);
47 spin_unlock_irqrestore(&p->port.lock, flags);
50 static void __dma_rx_complete(void *param)
52 struct uart_8250_port *p = param;
53 struct uart_8250_dma *dma = p->dma;
54 struct tty_port *tty_port = &p->port.state->port;
55 struct dma_tx_state state;
59 dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
61 count = dma->rx_size - state.residue;
63 tty_insert_flip_string(tty_port, dma->rx_buf, count);
64 p->port.icount.rx += count;
66 tty_flip_buffer_push(tty_port);
69 int serial8250_tx_dma(struct uart_8250_port *p)
71 struct uart_8250_dma *dma = p->dma;
72 struct circ_buf *xmit = &p->port.state->xmit;
73 struct dma_async_tx_descriptor *desc;
79 if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
80 /* We have been called from __dma_tx_complete() */
81 serial8250_rpm_put_tx(p);
85 dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
87 desc = dmaengine_prep_slave_single(dma->txchan,
88 dma->tx_addr + xmit->tail,
89 dma->tx_size, DMA_MEM_TO_DEV,
90 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
97 desc->callback = __dma_tx_complete;
98 desc->callback_param = p;
100 dma->tx_cookie = dmaengine_submit(desc);
102 dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
103 UART_XMIT_SIZE, DMA_TO_DEVICE);
105 dma_async_issue_pending(dma->txchan);
108 if (p->ier & UART_IER_THRI) {
109 p->ier &= ~UART_IER_THRI;
110 serial_out(p, UART_IER, p->ier);
119 int serial8250_rx_dma(struct uart_8250_port *p)
121 struct uart_8250_dma *dma = p->dma;
122 struct dma_async_tx_descriptor *desc;
127 desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
128 dma->rx_size, DMA_DEV_TO_MEM,
129 DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
134 desc->callback = __dma_rx_complete;
135 desc->callback_param = p;
137 dma->rx_cookie = dmaengine_submit(desc);
139 dma_async_issue_pending(dma->rxchan);
144 void serial8250_rx_dma_flush(struct uart_8250_port *p)
146 struct uart_8250_dma *dma = p->dma;
148 if (dma->rx_running) {
149 dmaengine_pause(dma->rxchan);
150 __dma_rx_complete(p);
151 dmaengine_terminate_async(dma->rxchan);
154 EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush);
156 int serial8250_request_dma(struct uart_8250_port *p)
158 struct uart_8250_dma *dma = p->dma;
159 phys_addr_t rx_dma_addr = dma->rx_dma_addr ?
160 dma->rx_dma_addr : p->port.mapbase;
161 phys_addr_t tx_dma_addr = dma->tx_dma_addr ?
162 dma->tx_dma_addr : p->port.mapbase;
164 struct dma_slave_caps caps;
167 /* Default slave configuration parameters */
168 dma->rxconf.direction = DMA_DEV_TO_MEM;
169 dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
170 dma->rxconf.src_addr = rx_dma_addr + UART_RX;
172 dma->txconf.direction = DMA_MEM_TO_DEV;
173 dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE;
174 dma->txconf.dst_addr = tx_dma_addr + UART_TX;
177 dma_cap_set(DMA_SLAVE, mask);
179 /* Get a channel for RX */
180 dma->rxchan = dma_request_slave_channel_compat(mask,
181 dma->fn, dma->rx_param,
186 /* 8250 rx dma requires dmaengine driver to support pause/terminate */
187 ret = dma_get_slave_caps(dma->rxchan, &caps);
190 if (!caps.cmd_pause || !caps.cmd_terminate ||
191 caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
196 dmaengine_slave_config(dma->rxchan, &dma->rxconf);
198 /* Get a channel for TX */
199 dma->txchan = dma_request_slave_channel_compat(mask,
200 dma->fn, dma->tx_param,
207 /* 8250 tx dma requires dmaengine driver to support terminate */
208 ret = dma_get_slave_caps(dma->txchan, &caps);
211 if (!caps.cmd_terminate) {
216 dmaengine_slave_config(dma->txchan, &dma->txconf);
220 dma->rx_size = PAGE_SIZE;
222 dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
223 &dma->rx_addr, GFP_KERNEL);
230 dma->tx_addr = dma_map_single(dma->txchan->device->dev,
231 p->port.state->xmit.buf,
234 if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) {
235 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size,
236 dma->rx_buf, dma->rx_addr);
241 dev_dbg_ratelimited(p->port.dev, "got both dma channels\n");
245 dma_release_channel(dma->txchan);
247 dma_release_channel(dma->rxchan);
250 EXPORT_SYMBOL_GPL(serial8250_request_dma);
252 void serial8250_release_dma(struct uart_8250_port *p)
254 struct uart_8250_dma *dma = p->dma;
259 /* Release RX resources */
260 dmaengine_terminate_sync(dma->rxchan);
261 dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
263 dma_release_channel(dma->rxchan);
266 /* Release TX resources */
267 dmaengine_terminate_sync(dma->txchan);
268 dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
269 UART_XMIT_SIZE, DMA_TO_DEVICE);
270 dma_release_channel(dma->txchan);
274 dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
276 EXPORT_SYMBOL_GPL(serial8250_release_dma);