xref: /openbmc/linux/drivers/tty/serial/8250/8250_dma.c (revision 9a87ffc99ec8eb8d35eed7c4f816d75f5cc9662e)
1e3b3d0f5SGreg Kroah-Hartman // SPDX-License-Identifier: GPL-2.0+
29ee4b83eSHeikki Krogerus /*
39ee4b83eSHeikki Krogerus  * 8250_dma.c - DMA Engine API support for 8250.c
49ee4b83eSHeikki Krogerus  *
59ee4b83eSHeikki Krogerus  * Copyright (C) 2013 Intel Corporation
69ee4b83eSHeikki Krogerus  */
79ee4b83eSHeikki Krogerus #include <linux/tty.h>
89ee4b83eSHeikki Krogerus #include <linux/tty_flip.h>
99ee4b83eSHeikki Krogerus #include <linux/serial_reg.h>
109ee4b83eSHeikki Krogerus #include <linux/dma-mapping.h>
119ee4b83eSHeikki Krogerus 
129ee4b83eSHeikki Krogerus #include "8250.h"
139ee4b83eSHeikki Krogerus 
__dma_tx_complete(void * param)149ee4b83eSHeikki Krogerus static void __dma_tx_complete(void *param)
159ee4b83eSHeikki Krogerus {
169ee4b83eSHeikki Krogerus 	struct uart_8250_port	*p = param;
179ee4b83eSHeikki Krogerus 	struct uart_8250_dma	*dma = p->dma;
189ee4b83eSHeikki Krogerus 	struct circ_buf		*xmit = &p->port.state->xmit;
19f8fd1b03SLoic Poulain 	unsigned long	flags;
202dc98946SAndy Shevchenko 	int		ret;
219ee4b83eSHeikki Krogerus 
229ee4b83eSHeikki Krogerus 	dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr,
239ee4b83eSHeikki Krogerus 				UART_XMIT_SIZE, DMA_TO_DEVICE);
249ee4b83eSHeikki Krogerus 
25f8fd1b03SLoic Poulain 	spin_lock_irqsave(&p->port.lock, flags);
26f8fd1b03SLoic Poulain 
27f8fd1b03SLoic Poulain 	dma->tx_running = 0;
28f8fd1b03SLoic Poulain 
29e02fbb0bSAndy Shevchenko 	uart_xmit_advance(&p->port, dma->tx_size);
309ee4b83eSHeikki Krogerus 
319ee4b83eSHeikki Krogerus 	if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS)
329ee4b83eSHeikki Krogerus 		uart_write_wakeup(&p->port);
339ee4b83eSHeikki Krogerus 
34b2202821SSebastian Andrzej Siewior 	ret = serial8250_tx_dma(p);
35e8ffbb71SIlpo Järvinen 	if (ret || !dma->tx_running)
367e267b29SAndy Shevchenko 		serial8250_set_THRI(p);
37f8fd1b03SLoic Poulain 
38f8fd1b03SLoic Poulain 	spin_unlock_irqrestore(&p->port.lock, flags);
399ee4b83eSHeikki Krogerus }
409ee4b83eSHeikki Krogerus 
__dma_rx_complete(struct uart_8250_port * p)4156dc5074SIlpo Järvinen static void __dma_rx_complete(struct uart_8250_port *p)
429ee4b83eSHeikki Krogerus {
439ee4b83eSHeikki Krogerus 	struct uart_8250_dma	*dma = p->dma;
446f3fe3b1SHeikki Krogerus 	struct tty_port		*tty_port = &p->port.state->port;
459ee4b83eSHeikki Krogerus 	struct dma_tx_state	state;
46*57e9af78SIlpo Järvinen 	enum dma_status		dma_status;
476f3fe3b1SHeikki Krogerus 	int			count;
489ee4b83eSHeikki Krogerus 
49*57e9af78SIlpo Järvinen 	/*
50*57e9af78SIlpo Järvinen 	 * New DMA Rx can be started during the completion handler before it
51*57e9af78SIlpo Järvinen 	 * could acquire port's lock and it might still be ongoing. Don't to
52*57e9af78SIlpo Järvinen 	 * anything in such case.
53*57e9af78SIlpo Järvinen 	 */
54*57e9af78SIlpo Järvinen 	dma_status = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state);
55*57e9af78SIlpo Järvinen 	if (dma_status == DMA_IN_PROGRESS)
56*57e9af78SIlpo Järvinen 		return;
579ee4b83eSHeikki Krogerus 
586f3fe3b1SHeikki Krogerus 	count = dma->rx_size - state.residue;
599ee4b83eSHeikki Krogerus 
606f3fe3b1SHeikki Krogerus 	tty_insert_flip_string(tty_port, dma->rx_buf, count);
616f3fe3b1SHeikki Krogerus 	p->port.icount.rx += count;
62*57e9af78SIlpo Järvinen 	dma->rx_running = 0;
636f3fe3b1SHeikki Krogerus 
646f3fe3b1SHeikki Krogerus 	tty_flip_buffer_push(tty_port);
659ee4b83eSHeikki Krogerus }
669ee4b83eSHeikki Krogerus 
dma_rx_complete(void * param)6756dc5074SIlpo Järvinen static void dma_rx_complete(void *param)
6856dc5074SIlpo Järvinen {
6956dc5074SIlpo Järvinen 	struct uart_8250_port *p = param;
7056dc5074SIlpo Järvinen 	struct uart_8250_dma *dma = p->dma;
7156dc5074SIlpo Järvinen 	unsigned long flags;
7256dc5074SIlpo Järvinen 
7331352811SIlpo Järvinen 	spin_lock_irqsave(&p->port.lock, flags);
7431352811SIlpo Järvinen 	if (dma->rx_running)
7556dc5074SIlpo Järvinen 		__dma_rx_complete(p);
7656dc5074SIlpo Järvinen 
7731352811SIlpo Järvinen 	/*
7831352811SIlpo Järvinen 	 * Cannot be combined with the previous check because __dma_rx_complete()
7931352811SIlpo Järvinen 	 * changes dma->rx_running.
8031352811SIlpo Järvinen 	 */
8156dc5074SIlpo Järvinen 	if (!dma->rx_running && (serial_lsr_in(p) & UART_LSR_DR))
8256dc5074SIlpo Järvinen 		p->dma->rx_dma(p);
8356dc5074SIlpo Järvinen 	spin_unlock_irqrestore(&p->port.lock, flags);
8456dc5074SIlpo Järvinen }
8556dc5074SIlpo Järvinen 
serial8250_tx_dma(struct uart_8250_port * p)869ee4b83eSHeikki Krogerus int serial8250_tx_dma(struct uart_8250_port *p)
879ee4b83eSHeikki Krogerus {
889ee4b83eSHeikki Krogerus 	struct uart_8250_dma		*dma = p->dma;
899ee4b83eSHeikki Krogerus 	struct circ_buf			*xmit = &p->port.state->xmit;
909ee4b83eSHeikki Krogerus 	struct dma_async_tx_descriptor	*desc;
91f58c252eSIlpo Järvinen 	struct uart_port		*up = &p->port;
92b2202821SSebastian Andrzej Siewior 	int ret;
939ee4b83eSHeikki Krogerus 
94f58c252eSIlpo Järvinen 	if (dma->tx_running) {
95f58c252eSIlpo Järvinen 		if (up->x_char) {
96f58c252eSIlpo Järvinen 			dmaengine_pause(dma->txchan);
97f58c252eSIlpo Järvinen 			uart_xchar_out(up, UART_TX);
98f58c252eSIlpo Järvinen 			dmaengine_resume(dma->txchan);
99f58c252eSIlpo Järvinen 		}
1005ea5b24dSHeikki Krogerus 		return 0;
101f58c252eSIlpo Järvinen 	} else if (up->x_char) {
102f58c252eSIlpo Järvinen 		uart_xchar_out(up, UART_TX);
103f58c252eSIlpo Järvinen 	}
1049ee4b83eSHeikki Krogerus 
1055fe86674SAndy Shevchenko 	if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) {
1065fe86674SAndy Shevchenko 		/* We have been called from __dma_tx_complete() */
1075fe86674SAndy Shevchenko 		return 0;
1085fe86674SAndy Shevchenko 	}
1095fe86674SAndy Shevchenko 
1109ee4b83eSHeikki Krogerus 	dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE);
1119ee4b83eSHeikki Krogerus 
112e4fb03feSMiquel Raynal 	serial8250_do_prepare_tx_dma(p);
113e4fb03feSMiquel Raynal 
1149ee4b83eSHeikki Krogerus 	desc = dmaengine_prep_slave_single(dma->txchan,
1159ee4b83eSHeikki Krogerus 					   dma->tx_addr + xmit->tail,
1169ee4b83eSHeikki Krogerus 					   dma->tx_size, DMA_MEM_TO_DEV,
1179ee4b83eSHeikki Krogerus 					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
118b2202821SSebastian Andrzej Siewior 	if (!desc) {
119b2202821SSebastian Andrzej Siewior 		ret = -EBUSY;
120b2202821SSebastian Andrzej Siewior 		goto err;
121b2202821SSebastian Andrzej Siewior 	}
1229ee4b83eSHeikki Krogerus 
1239ee4b83eSHeikki Krogerus 	dma->tx_running = 1;
1249ee4b83eSHeikki Krogerus 	desc->callback = __dma_tx_complete;
1259ee4b83eSHeikki Krogerus 	desc->callback_param = p;
1269ee4b83eSHeikki Krogerus 
1279ee4b83eSHeikki Krogerus 	dma->tx_cookie = dmaengine_submit(desc);
1289ee4b83eSHeikki Krogerus 
1299ee4b83eSHeikki Krogerus 	dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr,
1309ee4b83eSHeikki Krogerus 				   UART_XMIT_SIZE, DMA_TO_DEVICE);
1319ee4b83eSHeikki Krogerus 
1329ee4b83eSHeikki Krogerus 	dma_async_issue_pending(dma->txchan);
1337e267b29SAndy Shevchenko 	serial8250_clear_THRI(p);
134f8d6e9d3SIlpo Järvinen 	dma->tx_err = 0;
135f8d6e9d3SIlpo Järvinen 
1369ee4b83eSHeikki Krogerus 	return 0;
137b2202821SSebastian Andrzej Siewior err:
138b2202821SSebastian Andrzej Siewior 	dma->tx_err = 1;
139b2202821SSebastian Andrzej Siewior 	return ret;
1409ee4b83eSHeikki Krogerus }
1419ee4b83eSHeikki Krogerus 
serial8250_rx_dma(struct uart_8250_port * p)14233d9b8b2SPeter Hurley int serial8250_rx_dma(struct uart_8250_port *p)
1439ee4b83eSHeikki Krogerus {
1449ee4b83eSHeikki Krogerus 	struct uart_8250_dma		*dma = p->dma;
1459ee4b83eSHeikki Krogerus 	struct dma_async_tx_descriptor	*desc;
14675df022bSHeikki Krogerus 
1470fcb7901SSebastian Andrzej Siewior 	if (dma->rx_running)
14875df022bSHeikki Krogerus 		return 0;
14975df022bSHeikki Krogerus 
150e4fb03feSMiquel Raynal 	serial8250_do_prepare_rx_dma(p);
151e4fb03feSMiquel Raynal 
1529ee4b83eSHeikki Krogerus 	desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr,
1539ee4b83eSHeikki Krogerus 					   dma->rx_size, DMA_DEV_TO_MEM,
1549ee4b83eSHeikki Krogerus 					   DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
1559ee4b83eSHeikki Krogerus 	if (!desc)
1569ee4b83eSHeikki Krogerus 		return -EBUSY;
1579ee4b83eSHeikki Krogerus 
1580fcb7901SSebastian Andrzej Siewior 	dma->rx_running = 1;
15956dc5074SIlpo Järvinen 	desc->callback = dma_rx_complete;
1609ee4b83eSHeikki Krogerus 	desc->callback_param = p;
1619ee4b83eSHeikki Krogerus 
1629ee4b83eSHeikki Krogerus 	dma->rx_cookie = dmaengine_submit(desc);
1639ee4b83eSHeikki Krogerus 
1649ee4b83eSHeikki Krogerus 	dma_async_issue_pending(dma->rxchan);
1659ee4b83eSHeikki Krogerus 
1669ee4b83eSHeikki Krogerus 	return 0;
1679ee4b83eSHeikki Krogerus }
1689ee4b83eSHeikki Krogerus 
serial8250_rx_dma_flush(struct uart_8250_port * p)16933d9b8b2SPeter Hurley void serial8250_rx_dma_flush(struct uart_8250_port *p)
17033d9b8b2SPeter Hurley {
17133d9b8b2SPeter Hurley 	struct uart_8250_dma *dma = p->dma;
17233d9b8b2SPeter Hurley 
17333d9b8b2SPeter Hurley 	if (dma->rx_running) {
17433d9b8b2SPeter Hurley 		dmaengine_pause(dma->rxchan);
17533d9b8b2SPeter Hurley 		__dma_rx_complete(p);
1768d170472SAndy Shevchenko 		dmaengine_terminate_async(dma->rxchan);
17733d9b8b2SPeter Hurley 	}
17833d9b8b2SPeter Hurley }
179fd9e516dSChuah, Kim Tatt EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush);
18033d9b8b2SPeter Hurley 
serial8250_request_dma(struct uart_8250_port * p)1819ee4b83eSHeikki Krogerus int serial8250_request_dma(struct uart_8250_port *p)
1829ee4b83eSHeikki Krogerus {
1839ee4b83eSHeikki Krogerus 	struct uart_8250_dma	*dma = p->dma;
184d1834babSAndy Shevchenko 	phys_addr_t rx_dma_addr = dma->rx_dma_addr ?
185d1834babSAndy Shevchenko 				  dma->rx_dma_addr : p->port.mapbase;
186d1834babSAndy Shevchenko 	phys_addr_t tx_dma_addr = dma->tx_dma_addr ?
187d1834babSAndy Shevchenko 				  dma->tx_dma_addr : p->port.mapbase;
1889ee4b83eSHeikki Krogerus 	dma_cap_mask_t		mask;
189ec5a11a9SPeter Hurley 	struct dma_slave_caps	caps;
190ec5a11a9SPeter Hurley 	int			ret;
1919ee4b83eSHeikki Krogerus 
192ab19479aSHeikki Krogerus 	/* Default slave configuration parameters */
193ab19479aSHeikki Krogerus 	dma->rxconf.direction		= DMA_DEV_TO_MEM;
194ab19479aSHeikki Krogerus 	dma->rxconf.src_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE;
195d1834babSAndy Shevchenko 	dma->rxconf.src_addr		= rx_dma_addr + UART_RX;
196ab19479aSHeikki Krogerus 
197ab19479aSHeikki Krogerus 	dma->txconf.direction		= DMA_MEM_TO_DEV;
198ab19479aSHeikki Krogerus 	dma->txconf.dst_addr_width	= DMA_SLAVE_BUSWIDTH_1_BYTE;
199d1834babSAndy Shevchenko 	dma->txconf.dst_addr		= tx_dma_addr + UART_TX;
2009ee4b83eSHeikki Krogerus 
2019ee4b83eSHeikki Krogerus 	dma_cap_zero(mask);
2029ee4b83eSHeikki Krogerus 	dma_cap_set(DMA_SLAVE, mask);
2039ee4b83eSHeikki Krogerus 
2049ee4b83eSHeikki Krogerus 	/* Get a channel for RX */
205e4fb3b88SHeikki Krogerus 	dma->rxchan = dma_request_slave_channel_compat(mask,
206e4fb3b88SHeikki Krogerus 						       dma->fn, dma->rx_param,
207e4fb3b88SHeikki Krogerus 						       p->port.dev, "rx");
2089ee4b83eSHeikki Krogerus 	if (!dma->rxchan)
2099ee4b83eSHeikki Krogerus 		return -ENODEV;
2109ee4b83eSHeikki Krogerus 
211ec5a11a9SPeter Hurley 	/* 8250 rx dma requires dmaengine driver to support pause/terminate */
212ec5a11a9SPeter Hurley 	ret = dma_get_slave_caps(dma->rxchan, &caps);
213ec5a11a9SPeter Hurley 	if (ret)
214ec5a11a9SPeter Hurley 		goto release_rx;
215ec5a11a9SPeter Hurley 	if (!caps.cmd_pause || !caps.cmd_terminate ||
216ec5a11a9SPeter Hurley 	    caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) {
217ec5a11a9SPeter Hurley 		ret = -EINVAL;
218ec5a11a9SPeter Hurley 		goto release_rx;
219ec5a11a9SPeter Hurley 	}
220ec5a11a9SPeter Hurley 
2219ee4b83eSHeikki Krogerus 	dmaengine_slave_config(dma->rxchan, &dma->rxconf);
2229ee4b83eSHeikki Krogerus 
2239ee4b83eSHeikki Krogerus 	/* Get a channel for TX */
224e4fb3b88SHeikki Krogerus 	dma->txchan = dma_request_slave_channel_compat(mask,
225e4fb3b88SHeikki Krogerus 						       dma->fn, dma->tx_param,
226e4fb3b88SHeikki Krogerus 						       p->port.dev, "tx");
2279ee4b83eSHeikki Krogerus 	if (!dma->txchan) {
228ec5a11a9SPeter Hurley 		ret = -ENODEV;
229ec5a11a9SPeter Hurley 		goto release_rx;
2309ee4b83eSHeikki Krogerus 	}
2319ee4b83eSHeikki Krogerus 
232ddedfd82SPeter Hurley 	/* 8250 tx dma requires dmaengine driver to support terminate */
233ddedfd82SPeter Hurley 	ret = dma_get_slave_caps(dma->txchan, &caps);
234ddedfd82SPeter Hurley 	if (ret)
235ddedfd82SPeter Hurley 		goto err;
236ddedfd82SPeter Hurley 	if (!caps.cmd_terminate) {
237ddedfd82SPeter Hurley 		ret = -EINVAL;
238ddedfd82SPeter Hurley 		goto err;
239ddedfd82SPeter Hurley 	}
240ddedfd82SPeter Hurley 
2419ee4b83eSHeikki Krogerus 	dmaengine_slave_config(dma->txchan, &dma->txconf);
2429ee4b83eSHeikki Krogerus 
2439ee4b83eSHeikki Krogerus 	/* RX buffer */
2449ee4b83eSHeikki Krogerus 	if (!dma->rx_size)
2459ee4b83eSHeikki Krogerus 		dma->rx_size = PAGE_SIZE;
2469ee4b83eSHeikki Krogerus 
2479ee4b83eSHeikki Krogerus 	dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size,
2489ee4b83eSHeikki Krogerus 					&dma->rx_addr, GFP_KERNEL);
249ec5a11a9SPeter Hurley 	if (!dma->rx_buf) {
250ec5a11a9SPeter Hurley 		ret = -ENOMEM;
251d4089a33SHeikki Krogerus 		goto err;
252ec5a11a9SPeter Hurley 	}
2539ee4b83eSHeikki Krogerus 
2549ee4b83eSHeikki Krogerus 	/* TX buffer */
2559ee4b83eSHeikki Krogerus 	dma->tx_addr = dma_map_single(dma->txchan->device->dev,
2569ee4b83eSHeikki Krogerus 					p->port.state->xmit.buf,
2579ee4b83eSHeikki Krogerus 					UART_XMIT_SIZE,
2589ee4b83eSHeikki Krogerus 					DMA_TO_DEVICE);
259d4089a33SHeikki Krogerus 	if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) {
260d4089a33SHeikki Krogerus 		dma_free_coherent(dma->rxchan->device->dev, dma->rx_size,
261d4089a33SHeikki Krogerus 				  dma->rx_buf, dma->rx_addr);
262ec5a11a9SPeter Hurley 		ret = -ENOMEM;
263d4089a33SHeikki Krogerus 		goto err;
264d4089a33SHeikki Krogerus 	}
2659ee4b83eSHeikki Krogerus 
2669ee4b83eSHeikki Krogerus 	dev_dbg_ratelimited(p->port.dev, "got both dma channels\n");
2679ee4b83eSHeikki Krogerus 
2689ee4b83eSHeikki Krogerus 	return 0;
269d4089a33SHeikki Krogerus err:
270d4089a33SHeikki Krogerus 	dma_release_channel(dma->txchan);
271ec5a11a9SPeter Hurley release_rx:
272ec5a11a9SPeter Hurley 	dma_release_channel(dma->rxchan);
273ec5a11a9SPeter Hurley 	return ret;
2749ee4b83eSHeikki Krogerus }
2759ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_request_dma);
2769ee4b83eSHeikki Krogerus 
serial8250_release_dma(struct uart_8250_port * p)2779ee4b83eSHeikki Krogerus void serial8250_release_dma(struct uart_8250_port *p)
2789ee4b83eSHeikki Krogerus {
2799ee4b83eSHeikki Krogerus 	struct uart_8250_dma *dma = p->dma;
2809ee4b83eSHeikki Krogerus 
2819ee4b83eSHeikki Krogerus 	if (!dma)
2829ee4b83eSHeikki Krogerus 		return;
2839ee4b83eSHeikki Krogerus 
2849ee4b83eSHeikki Krogerus 	/* Release RX resources */
2858d170472SAndy Shevchenko 	dmaengine_terminate_sync(dma->rxchan);
2869ee4b83eSHeikki Krogerus 	dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf,
2879ee4b83eSHeikki Krogerus 			  dma->rx_addr);
2889ee4b83eSHeikki Krogerus 	dma_release_channel(dma->rxchan);
2899ee4b83eSHeikki Krogerus 	dma->rxchan = NULL;
2909ee4b83eSHeikki Krogerus 
2919ee4b83eSHeikki Krogerus 	/* Release TX resources */
2928d170472SAndy Shevchenko 	dmaengine_terminate_sync(dma->txchan);
2939ee4b83eSHeikki Krogerus 	dma_unmap_single(dma->txchan->device->dev, dma->tx_addr,
2949ee4b83eSHeikki Krogerus 			 UART_XMIT_SIZE, DMA_TO_DEVICE);
2959ee4b83eSHeikki Krogerus 	dma_release_channel(dma->txchan);
2969ee4b83eSHeikki Krogerus 	dma->txchan = NULL;
2979ee4b83eSHeikki Krogerus 	dma->tx_running = 0;
2989ee4b83eSHeikki Krogerus 
2999ee4b83eSHeikki Krogerus 	dev_dbg_ratelimited(p->port.dev, "dma channels released\n");
3009ee4b83eSHeikki Krogerus }
3019ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_release_dma);
302