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 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 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; 466f3fe3b1SHeikki Krogerus int count; 479ee4b83eSHeikki Krogerus 480fcb7901SSebastian Andrzej Siewior dma->rx_running = 0; 499ee4b83eSHeikki Krogerus dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state); 509ee4b83eSHeikki Krogerus 516f3fe3b1SHeikki Krogerus count = dma->rx_size - state.residue; 529ee4b83eSHeikki Krogerus 536f3fe3b1SHeikki Krogerus tty_insert_flip_string(tty_port, dma->rx_buf, count); 546f3fe3b1SHeikki Krogerus p->port.icount.rx += count; 556f3fe3b1SHeikki Krogerus 566f3fe3b1SHeikki Krogerus tty_flip_buffer_push(tty_port); 579ee4b83eSHeikki Krogerus } 589ee4b83eSHeikki Krogerus 5956dc5074SIlpo Järvinen static void dma_rx_complete(void *param) 6056dc5074SIlpo Järvinen { 6156dc5074SIlpo Järvinen struct uart_8250_port *p = param; 6256dc5074SIlpo Järvinen struct uart_8250_dma *dma = p->dma; 6356dc5074SIlpo Järvinen unsigned long flags; 6456dc5074SIlpo Järvinen 65*31352811SIlpo Järvinen spin_lock_irqsave(&p->port.lock, flags); 66*31352811SIlpo Järvinen if (dma->rx_running) 6756dc5074SIlpo Järvinen __dma_rx_complete(p); 6856dc5074SIlpo Järvinen 69*31352811SIlpo Järvinen /* 70*31352811SIlpo Järvinen * Cannot be combined with the previous check because __dma_rx_complete() 71*31352811SIlpo Järvinen * changes dma->rx_running. 72*31352811SIlpo Järvinen */ 7356dc5074SIlpo Järvinen if (!dma->rx_running && (serial_lsr_in(p) & UART_LSR_DR)) 7456dc5074SIlpo Järvinen p->dma->rx_dma(p); 7556dc5074SIlpo Järvinen spin_unlock_irqrestore(&p->port.lock, flags); 7656dc5074SIlpo Järvinen } 7756dc5074SIlpo Järvinen 789ee4b83eSHeikki Krogerus int serial8250_tx_dma(struct uart_8250_port *p) 799ee4b83eSHeikki Krogerus { 809ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 819ee4b83eSHeikki Krogerus struct circ_buf *xmit = &p->port.state->xmit; 829ee4b83eSHeikki Krogerus struct dma_async_tx_descriptor *desc; 83f58c252eSIlpo Järvinen struct uart_port *up = &p->port; 84b2202821SSebastian Andrzej Siewior int ret; 859ee4b83eSHeikki Krogerus 86f58c252eSIlpo Järvinen if (dma->tx_running) { 87f58c252eSIlpo Järvinen if (up->x_char) { 88f58c252eSIlpo Järvinen dmaengine_pause(dma->txchan); 89f58c252eSIlpo Järvinen uart_xchar_out(up, UART_TX); 90f58c252eSIlpo Järvinen dmaengine_resume(dma->txchan); 91f58c252eSIlpo Järvinen } 925ea5b24dSHeikki Krogerus return 0; 93f58c252eSIlpo Järvinen } else if (up->x_char) { 94f58c252eSIlpo Järvinen uart_xchar_out(up, UART_TX); 95f58c252eSIlpo Järvinen } 969ee4b83eSHeikki Krogerus 975fe86674SAndy Shevchenko if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) { 985fe86674SAndy Shevchenko /* We have been called from __dma_tx_complete() */ 995fe86674SAndy Shevchenko return 0; 1005fe86674SAndy Shevchenko } 1015fe86674SAndy Shevchenko 1029ee4b83eSHeikki Krogerus dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 1039ee4b83eSHeikki Krogerus 104e4fb03feSMiquel Raynal serial8250_do_prepare_tx_dma(p); 105e4fb03feSMiquel Raynal 1069ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->txchan, 1079ee4b83eSHeikki Krogerus dma->tx_addr + xmit->tail, 1089ee4b83eSHeikki Krogerus dma->tx_size, DMA_MEM_TO_DEV, 1099ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 110b2202821SSebastian Andrzej Siewior if (!desc) { 111b2202821SSebastian Andrzej Siewior ret = -EBUSY; 112b2202821SSebastian Andrzej Siewior goto err; 113b2202821SSebastian Andrzej Siewior } 1149ee4b83eSHeikki Krogerus 1159ee4b83eSHeikki Krogerus dma->tx_running = 1; 1169ee4b83eSHeikki Krogerus desc->callback = __dma_tx_complete; 1179ee4b83eSHeikki Krogerus desc->callback_param = p; 1189ee4b83eSHeikki Krogerus 1199ee4b83eSHeikki Krogerus dma->tx_cookie = dmaengine_submit(desc); 1209ee4b83eSHeikki Krogerus 1219ee4b83eSHeikki Krogerus dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr, 1229ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 1239ee4b83eSHeikki Krogerus 1249ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->txchan); 1257e267b29SAndy Shevchenko serial8250_clear_THRI(p); 126f8d6e9d3SIlpo Järvinen dma->tx_err = 0; 127f8d6e9d3SIlpo Järvinen 1289ee4b83eSHeikki Krogerus return 0; 129b2202821SSebastian Andrzej Siewior err: 130b2202821SSebastian Andrzej Siewior dma->tx_err = 1; 131b2202821SSebastian Andrzej Siewior return ret; 1329ee4b83eSHeikki Krogerus } 1339ee4b83eSHeikki Krogerus 13433d9b8b2SPeter Hurley int serial8250_rx_dma(struct uart_8250_port *p) 1359ee4b83eSHeikki Krogerus { 1369ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 1379ee4b83eSHeikki Krogerus struct dma_async_tx_descriptor *desc; 13875df022bSHeikki Krogerus 1390fcb7901SSebastian Andrzej Siewior if (dma->rx_running) 14075df022bSHeikki Krogerus return 0; 14175df022bSHeikki Krogerus 142e4fb03feSMiquel Raynal serial8250_do_prepare_rx_dma(p); 143e4fb03feSMiquel Raynal 1449ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr, 1459ee4b83eSHeikki Krogerus dma->rx_size, DMA_DEV_TO_MEM, 1469ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 1479ee4b83eSHeikki Krogerus if (!desc) 1489ee4b83eSHeikki Krogerus return -EBUSY; 1499ee4b83eSHeikki Krogerus 1500fcb7901SSebastian Andrzej Siewior dma->rx_running = 1; 15156dc5074SIlpo Järvinen desc->callback = dma_rx_complete; 1529ee4b83eSHeikki Krogerus desc->callback_param = p; 1539ee4b83eSHeikki Krogerus 1549ee4b83eSHeikki Krogerus dma->rx_cookie = dmaengine_submit(desc); 1559ee4b83eSHeikki Krogerus 1569ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->rxchan); 1579ee4b83eSHeikki Krogerus 1589ee4b83eSHeikki Krogerus return 0; 1599ee4b83eSHeikki Krogerus } 1609ee4b83eSHeikki Krogerus 16133d9b8b2SPeter Hurley void serial8250_rx_dma_flush(struct uart_8250_port *p) 16233d9b8b2SPeter Hurley { 16333d9b8b2SPeter Hurley struct uart_8250_dma *dma = p->dma; 16433d9b8b2SPeter Hurley 16533d9b8b2SPeter Hurley if (dma->rx_running) { 16633d9b8b2SPeter Hurley dmaengine_pause(dma->rxchan); 16733d9b8b2SPeter Hurley __dma_rx_complete(p); 1688d170472SAndy Shevchenko dmaengine_terminate_async(dma->rxchan); 16933d9b8b2SPeter Hurley } 17033d9b8b2SPeter Hurley } 171fd9e516dSChuah, Kim Tatt EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush); 17233d9b8b2SPeter Hurley 1739ee4b83eSHeikki Krogerus int serial8250_request_dma(struct uart_8250_port *p) 1749ee4b83eSHeikki Krogerus { 1759ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 176d1834babSAndy Shevchenko phys_addr_t rx_dma_addr = dma->rx_dma_addr ? 177d1834babSAndy Shevchenko dma->rx_dma_addr : p->port.mapbase; 178d1834babSAndy Shevchenko phys_addr_t tx_dma_addr = dma->tx_dma_addr ? 179d1834babSAndy Shevchenko dma->tx_dma_addr : p->port.mapbase; 1809ee4b83eSHeikki Krogerus dma_cap_mask_t mask; 181ec5a11a9SPeter Hurley struct dma_slave_caps caps; 182ec5a11a9SPeter Hurley int ret; 1839ee4b83eSHeikki Krogerus 184ab19479aSHeikki Krogerus /* Default slave configuration parameters */ 185ab19479aSHeikki Krogerus dma->rxconf.direction = DMA_DEV_TO_MEM; 186ab19479aSHeikki Krogerus dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 187d1834babSAndy Shevchenko dma->rxconf.src_addr = rx_dma_addr + UART_RX; 188ab19479aSHeikki Krogerus 189ab19479aSHeikki Krogerus dma->txconf.direction = DMA_MEM_TO_DEV; 190ab19479aSHeikki Krogerus dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 191d1834babSAndy Shevchenko dma->txconf.dst_addr = tx_dma_addr + UART_TX; 1929ee4b83eSHeikki Krogerus 1939ee4b83eSHeikki Krogerus dma_cap_zero(mask); 1949ee4b83eSHeikki Krogerus dma_cap_set(DMA_SLAVE, mask); 1959ee4b83eSHeikki Krogerus 1969ee4b83eSHeikki Krogerus /* Get a channel for RX */ 197e4fb3b88SHeikki Krogerus dma->rxchan = dma_request_slave_channel_compat(mask, 198e4fb3b88SHeikki Krogerus dma->fn, dma->rx_param, 199e4fb3b88SHeikki Krogerus p->port.dev, "rx"); 2009ee4b83eSHeikki Krogerus if (!dma->rxchan) 2019ee4b83eSHeikki Krogerus return -ENODEV; 2029ee4b83eSHeikki Krogerus 203ec5a11a9SPeter Hurley /* 8250 rx dma requires dmaengine driver to support pause/terminate */ 204ec5a11a9SPeter Hurley ret = dma_get_slave_caps(dma->rxchan, &caps); 205ec5a11a9SPeter Hurley if (ret) 206ec5a11a9SPeter Hurley goto release_rx; 207ec5a11a9SPeter Hurley if (!caps.cmd_pause || !caps.cmd_terminate || 208ec5a11a9SPeter Hurley caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) { 209ec5a11a9SPeter Hurley ret = -EINVAL; 210ec5a11a9SPeter Hurley goto release_rx; 211ec5a11a9SPeter Hurley } 212ec5a11a9SPeter Hurley 2139ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->rxchan, &dma->rxconf); 2149ee4b83eSHeikki Krogerus 2159ee4b83eSHeikki Krogerus /* Get a channel for TX */ 216e4fb3b88SHeikki Krogerus dma->txchan = dma_request_slave_channel_compat(mask, 217e4fb3b88SHeikki Krogerus dma->fn, dma->tx_param, 218e4fb3b88SHeikki Krogerus p->port.dev, "tx"); 2199ee4b83eSHeikki Krogerus if (!dma->txchan) { 220ec5a11a9SPeter Hurley ret = -ENODEV; 221ec5a11a9SPeter Hurley goto release_rx; 2229ee4b83eSHeikki Krogerus } 2239ee4b83eSHeikki Krogerus 224ddedfd82SPeter Hurley /* 8250 tx dma requires dmaengine driver to support terminate */ 225ddedfd82SPeter Hurley ret = dma_get_slave_caps(dma->txchan, &caps); 226ddedfd82SPeter Hurley if (ret) 227ddedfd82SPeter Hurley goto err; 228ddedfd82SPeter Hurley if (!caps.cmd_terminate) { 229ddedfd82SPeter Hurley ret = -EINVAL; 230ddedfd82SPeter Hurley goto err; 231ddedfd82SPeter Hurley } 232ddedfd82SPeter Hurley 2339ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->txchan, &dma->txconf); 2349ee4b83eSHeikki Krogerus 2359ee4b83eSHeikki Krogerus /* RX buffer */ 2369ee4b83eSHeikki Krogerus if (!dma->rx_size) 2379ee4b83eSHeikki Krogerus dma->rx_size = PAGE_SIZE; 2389ee4b83eSHeikki Krogerus 2399ee4b83eSHeikki Krogerus dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size, 2409ee4b83eSHeikki Krogerus &dma->rx_addr, GFP_KERNEL); 241ec5a11a9SPeter Hurley if (!dma->rx_buf) { 242ec5a11a9SPeter Hurley ret = -ENOMEM; 243d4089a33SHeikki Krogerus goto err; 244ec5a11a9SPeter Hurley } 2459ee4b83eSHeikki Krogerus 2469ee4b83eSHeikki Krogerus /* TX buffer */ 2479ee4b83eSHeikki Krogerus dma->tx_addr = dma_map_single(dma->txchan->device->dev, 2489ee4b83eSHeikki Krogerus p->port.state->xmit.buf, 2499ee4b83eSHeikki Krogerus UART_XMIT_SIZE, 2509ee4b83eSHeikki Krogerus DMA_TO_DEVICE); 251d4089a33SHeikki Krogerus if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) { 252d4089a33SHeikki Krogerus dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, 253d4089a33SHeikki Krogerus dma->rx_buf, dma->rx_addr); 254ec5a11a9SPeter Hurley ret = -ENOMEM; 255d4089a33SHeikki Krogerus goto err; 256d4089a33SHeikki Krogerus } 2579ee4b83eSHeikki Krogerus 2589ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "got both dma channels\n"); 2599ee4b83eSHeikki Krogerus 2609ee4b83eSHeikki Krogerus return 0; 261d4089a33SHeikki Krogerus err: 262d4089a33SHeikki Krogerus dma_release_channel(dma->txchan); 263ec5a11a9SPeter Hurley release_rx: 264ec5a11a9SPeter Hurley dma_release_channel(dma->rxchan); 265ec5a11a9SPeter Hurley return ret; 2669ee4b83eSHeikki Krogerus } 2679ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_request_dma); 2689ee4b83eSHeikki Krogerus 2699ee4b83eSHeikki Krogerus void serial8250_release_dma(struct uart_8250_port *p) 2709ee4b83eSHeikki Krogerus { 2719ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 2729ee4b83eSHeikki Krogerus 2739ee4b83eSHeikki Krogerus if (!dma) 2749ee4b83eSHeikki Krogerus return; 2759ee4b83eSHeikki Krogerus 2769ee4b83eSHeikki Krogerus /* Release RX resources */ 2778d170472SAndy Shevchenko dmaengine_terminate_sync(dma->rxchan); 2789ee4b83eSHeikki Krogerus dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf, 2799ee4b83eSHeikki Krogerus dma->rx_addr); 2809ee4b83eSHeikki Krogerus dma_release_channel(dma->rxchan); 2819ee4b83eSHeikki Krogerus dma->rxchan = NULL; 2829ee4b83eSHeikki Krogerus 2839ee4b83eSHeikki Krogerus /* Release TX resources */ 2848d170472SAndy Shevchenko dmaengine_terminate_sync(dma->txchan); 2859ee4b83eSHeikki Krogerus dma_unmap_single(dma->txchan->device->dev, dma->tx_addr, 2869ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 2879ee4b83eSHeikki Krogerus dma_release_channel(dma->txchan); 2889ee4b83eSHeikki Krogerus dma->txchan = NULL; 2899ee4b83eSHeikki Krogerus dma->tx_running = 0; 2909ee4b83eSHeikki Krogerus 2919ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "dma channels released\n"); 2929ee4b83eSHeikki Krogerus } 2939ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_release_dma); 294