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 299ee4b83eSHeikki Krogerus xmit->tail += dma->tx_size; 309ee4b83eSHeikki Krogerus xmit->tail &= UART_XMIT_SIZE - 1; 319ee4b83eSHeikki Krogerus p->port.icount.tx += dma->tx_size; 329ee4b83eSHeikki Krogerus 339ee4b83eSHeikki Krogerus if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 349ee4b83eSHeikki Krogerus uart_write_wakeup(&p->port); 359ee4b83eSHeikki Krogerus 36b2202821SSebastian Andrzej Siewior ret = serial8250_tx_dma(p); 37e8ffbb71SIlpo Järvinen if (ret || !dma->tx_running) 387e267b29SAndy Shevchenko serial8250_set_THRI(p); 39f8fd1b03SLoic Poulain 40f8fd1b03SLoic Poulain spin_unlock_irqrestore(&p->port.lock, flags); 419ee4b83eSHeikki Krogerus } 429ee4b83eSHeikki Krogerus 439ee4b83eSHeikki Krogerus static void __dma_rx_complete(void *param) 449ee4b83eSHeikki Krogerus { 459ee4b83eSHeikki Krogerus struct uart_8250_port *p = param; 469ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 476f3fe3b1SHeikki Krogerus struct tty_port *tty_port = &p->port.state->port; 489ee4b83eSHeikki Krogerus struct dma_tx_state state; 496f3fe3b1SHeikki Krogerus int count; 509ee4b83eSHeikki Krogerus 510fcb7901SSebastian Andrzej Siewior dma->rx_running = 0; 529ee4b83eSHeikki Krogerus dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state); 539ee4b83eSHeikki Krogerus 546f3fe3b1SHeikki Krogerus count = dma->rx_size - state.residue; 559ee4b83eSHeikki Krogerus 566f3fe3b1SHeikki Krogerus tty_insert_flip_string(tty_port, dma->rx_buf, count); 576f3fe3b1SHeikki Krogerus p->port.icount.rx += count; 586f3fe3b1SHeikki Krogerus 596f3fe3b1SHeikki Krogerus tty_flip_buffer_push(tty_port); 609ee4b83eSHeikki Krogerus } 619ee4b83eSHeikki Krogerus 629ee4b83eSHeikki Krogerus int serial8250_tx_dma(struct uart_8250_port *p) 639ee4b83eSHeikki Krogerus { 649ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 659ee4b83eSHeikki Krogerus struct circ_buf *xmit = &p->port.state->xmit; 669ee4b83eSHeikki Krogerus struct dma_async_tx_descriptor *desc; 67f58c252eSIlpo Järvinen struct uart_port *up = &p->port; 68b2202821SSebastian Andrzej Siewior int ret; 699ee4b83eSHeikki Krogerus 70f58c252eSIlpo Järvinen if (dma->tx_running) { 71f58c252eSIlpo Järvinen if (up->x_char) { 72f58c252eSIlpo Järvinen dmaengine_pause(dma->txchan); 73f58c252eSIlpo Järvinen uart_xchar_out(up, UART_TX); 74f58c252eSIlpo Järvinen dmaengine_resume(dma->txchan); 75f58c252eSIlpo Järvinen } 765ea5b24dSHeikki Krogerus return 0; 77f58c252eSIlpo Järvinen } else if (up->x_char) { 78f58c252eSIlpo Järvinen uart_xchar_out(up, UART_TX); 79f58c252eSIlpo Järvinen } 809ee4b83eSHeikki Krogerus 815fe86674SAndy Shevchenko if (uart_tx_stopped(&p->port) || uart_circ_empty(xmit)) { 825fe86674SAndy Shevchenko /* We have been called from __dma_tx_complete() */ 835fe86674SAndy Shevchenko return 0; 845fe86674SAndy Shevchenko } 855fe86674SAndy Shevchenko 869ee4b83eSHeikki Krogerus dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 879ee4b83eSHeikki Krogerus 88e4fb03feSMiquel Raynal serial8250_do_prepare_tx_dma(p); 89e4fb03feSMiquel Raynal 909ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->txchan, 919ee4b83eSHeikki Krogerus dma->tx_addr + xmit->tail, 929ee4b83eSHeikki Krogerus dma->tx_size, DMA_MEM_TO_DEV, 939ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 94b2202821SSebastian Andrzej Siewior if (!desc) { 95b2202821SSebastian Andrzej Siewior ret = -EBUSY; 96b2202821SSebastian Andrzej Siewior goto err; 97b2202821SSebastian Andrzej Siewior } 989ee4b83eSHeikki Krogerus 999ee4b83eSHeikki Krogerus dma->tx_running = 1; 1009ee4b83eSHeikki Krogerus desc->callback = __dma_tx_complete; 1019ee4b83eSHeikki Krogerus desc->callback_param = p; 1029ee4b83eSHeikki Krogerus 1039ee4b83eSHeikki Krogerus dma->tx_cookie = dmaengine_submit(desc); 1049ee4b83eSHeikki Krogerus 1059ee4b83eSHeikki Krogerus dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr, 1069ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 1079ee4b83eSHeikki Krogerus 1089ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->txchan); 1097e267b29SAndy Shevchenko serial8250_clear_THRI(p); 110*f8d6e9d3SIlpo Järvinen if (dma->tx_err) 111*f8d6e9d3SIlpo Järvinen dma->tx_err = 0; 112*f8d6e9d3SIlpo Järvinen 1139ee4b83eSHeikki Krogerus return 0; 114b2202821SSebastian Andrzej Siewior err: 115b2202821SSebastian Andrzej Siewior dma->tx_err = 1; 116b2202821SSebastian Andrzej Siewior return ret; 1179ee4b83eSHeikki Krogerus } 1189ee4b83eSHeikki Krogerus 11933d9b8b2SPeter Hurley int serial8250_rx_dma(struct uart_8250_port *p) 1209ee4b83eSHeikki Krogerus { 1219ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 1229ee4b83eSHeikki Krogerus struct dma_async_tx_descriptor *desc; 12375df022bSHeikki Krogerus 1240fcb7901SSebastian Andrzej Siewior if (dma->rx_running) 12575df022bSHeikki Krogerus return 0; 12675df022bSHeikki Krogerus 127e4fb03feSMiquel Raynal serial8250_do_prepare_rx_dma(p); 128e4fb03feSMiquel Raynal 1299ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr, 1309ee4b83eSHeikki Krogerus dma->rx_size, DMA_DEV_TO_MEM, 1319ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 1329ee4b83eSHeikki Krogerus if (!desc) 1339ee4b83eSHeikki Krogerus return -EBUSY; 1349ee4b83eSHeikki Krogerus 1350fcb7901SSebastian Andrzej Siewior dma->rx_running = 1; 1369ee4b83eSHeikki Krogerus desc->callback = __dma_rx_complete; 1379ee4b83eSHeikki Krogerus desc->callback_param = p; 1389ee4b83eSHeikki Krogerus 1399ee4b83eSHeikki Krogerus dma->rx_cookie = dmaengine_submit(desc); 1409ee4b83eSHeikki Krogerus 1419ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->rxchan); 1429ee4b83eSHeikki Krogerus 1439ee4b83eSHeikki Krogerus return 0; 1449ee4b83eSHeikki Krogerus } 1459ee4b83eSHeikki Krogerus 14633d9b8b2SPeter Hurley void serial8250_rx_dma_flush(struct uart_8250_port *p) 14733d9b8b2SPeter Hurley { 14833d9b8b2SPeter Hurley struct uart_8250_dma *dma = p->dma; 14933d9b8b2SPeter Hurley 15033d9b8b2SPeter Hurley if (dma->rx_running) { 15133d9b8b2SPeter Hurley dmaengine_pause(dma->rxchan); 15233d9b8b2SPeter Hurley __dma_rx_complete(p); 1538d170472SAndy Shevchenko dmaengine_terminate_async(dma->rxchan); 15433d9b8b2SPeter Hurley } 15533d9b8b2SPeter Hurley } 156fd9e516dSChuah, Kim Tatt EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush); 15733d9b8b2SPeter Hurley 1589ee4b83eSHeikki Krogerus int serial8250_request_dma(struct uart_8250_port *p) 1599ee4b83eSHeikki Krogerus { 1609ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 161d1834babSAndy Shevchenko phys_addr_t rx_dma_addr = dma->rx_dma_addr ? 162d1834babSAndy Shevchenko dma->rx_dma_addr : p->port.mapbase; 163d1834babSAndy Shevchenko phys_addr_t tx_dma_addr = dma->tx_dma_addr ? 164d1834babSAndy Shevchenko dma->tx_dma_addr : p->port.mapbase; 1659ee4b83eSHeikki Krogerus dma_cap_mask_t mask; 166ec5a11a9SPeter Hurley struct dma_slave_caps caps; 167ec5a11a9SPeter Hurley int ret; 1689ee4b83eSHeikki Krogerus 169ab19479aSHeikki Krogerus /* Default slave configuration parameters */ 170ab19479aSHeikki Krogerus dma->rxconf.direction = DMA_DEV_TO_MEM; 171ab19479aSHeikki Krogerus dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 172d1834babSAndy Shevchenko dma->rxconf.src_addr = rx_dma_addr + UART_RX; 173ab19479aSHeikki Krogerus 174ab19479aSHeikki Krogerus dma->txconf.direction = DMA_MEM_TO_DEV; 175ab19479aSHeikki Krogerus dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 176d1834babSAndy Shevchenko dma->txconf.dst_addr = tx_dma_addr + UART_TX; 1779ee4b83eSHeikki Krogerus 1789ee4b83eSHeikki Krogerus dma_cap_zero(mask); 1799ee4b83eSHeikki Krogerus dma_cap_set(DMA_SLAVE, mask); 1809ee4b83eSHeikki Krogerus 1819ee4b83eSHeikki Krogerus /* Get a channel for RX */ 182e4fb3b88SHeikki Krogerus dma->rxchan = dma_request_slave_channel_compat(mask, 183e4fb3b88SHeikki Krogerus dma->fn, dma->rx_param, 184e4fb3b88SHeikki Krogerus p->port.dev, "rx"); 1859ee4b83eSHeikki Krogerus if (!dma->rxchan) 1869ee4b83eSHeikki Krogerus return -ENODEV; 1879ee4b83eSHeikki Krogerus 188ec5a11a9SPeter Hurley /* 8250 rx dma requires dmaengine driver to support pause/terminate */ 189ec5a11a9SPeter Hurley ret = dma_get_slave_caps(dma->rxchan, &caps); 190ec5a11a9SPeter Hurley if (ret) 191ec5a11a9SPeter Hurley goto release_rx; 192ec5a11a9SPeter Hurley if (!caps.cmd_pause || !caps.cmd_terminate || 193ec5a11a9SPeter Hurley caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) { 194ec5a11a9SPeter Hurley ret = -EINVAL; 195ec5a11a9SPeter Hurley goto release_rx; 196ec5a11a9SPeter Hurley } 197ec5a11a9SPeter Hurley 1989ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->rxchan, &dma->rxconf); 1999ee4b83eSHeikki Krogerus 2009ee4b83eSHeikki Krogerus /* Get a channel for TX */ 201e4fb3b88SHeikki Krogerus dma->txchan = dma_request_slave_channel_compat(mask, 202e4fb3b88SHeikki Krogerus dma->fn, dma->tx_param, 203e4fb3b88SHeikki Krogerus p->port.dev, "tx"); 2049ee4b83eSHeikki Krogerus if (!dma->txchan) { 205ec5a11a9SPeter Hurley ret = -ENODEV; 206ec5a11a9SPeter Hurley goto release_rx; 2079ee4b83eSHeikki Krogerus } 2089ee4b83eSHeikki Krogerus 209ddedfd82SPeter Hurley /* 8250 tx dma requires dmaengine driver to support terminate */ 210ddedfd82SPeter Hurley ret = dma_get_slave_caps(dma->txchan, &caps); 211ddedfd82SPeter Hurley if (ret) 212ddedfd82SPeter Hurley goto err; 213ddedfd82SPeter Hurley if (!caps.cmd_terminate) { 214ddedfd82SPeter Hurley ret = -EINVAL; 215ddedfd82SPeter Hurley goto err; 216ddedfd82SPeter Hurley } 217ddedfd82SPeter Hurley 2189ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->txchan, &dma->txconf); 2199ee4b83eSHeikki Krogerus 2209ee4b83eSHeikki Krogerus /* RX buffer */ 2219ee4b83eSHeikki Krogerus if (!dma->rx_size) 2229ee4b83eSHeikki Krogerus dma->rx_size = PAGE_SIZE; 2239ee4b83eSHeikki Krogerus 2249ee4b83eSHeikki Krogerus dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size, 2259ee4b83eSHeikki Krogerus &dma->rx_addr, GFP_KERNEL); 226ec5a11a9SPeter Hurley if (!dma->rx_buf) { 227ec5a11a9SPeter Hurley ret = -ENOMEM; 228d4089a33SHeikki Krogerus goto err; 229ec5a11a9SPeter Hurley } 2309ee4b83eSHeikki Krogerus 2319ee4b83eSHeikki Krogerus /* TX buffer */ 2329ee4b83eSHeikki Krogerus dma->tx_addr = dma_map_single(dma->txchan->device->dev, 2339ee4b83eSHeikki Krogerus p->port.state->xmit.buf, 2349ee4b83eSHeikki Krogerus UART_XMIT_SIZE, 2359ee4b83eSHeikki Krogerus DMA_TO_DEVICE); 236d4089a33SHeikki Krogerus if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) { 237d4089a33SHeikki Krogerus dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, 238d4089a33SHeikki Krogerus dma->rx_buf, dma->rx_addr); 239ec5a11a9SPeter Hurley ret = -ENOMEM; 240d4089a33SHeikki Krogerus goto err; 241d4089a33SHeikki Krogerus } 2429ee4b83eSHeikki Krogerus 2439ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "got both dma channels\n"); 2449ee4b83eSHeikki Krogerus 2459ee4b83eSHeikki Krogerus return 0; 246d4089a33SHeikki Krogerus err: 247d4089a33SHeikki Krogerus dma_release_channel(dma->txchan); 248ec5a11a9SPeter Hurley release_rx: 249ec5a11a9SPeter Hurley dma_release_channel(dma->rxchan); 250ec5a11a9SPeter Hurley return ret; 2519ee4b83eSHeikki Krogerus } 2529ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_request_dma); 2539ee4b83eSHeikki Krogerus 2549ee4b83eSHeikki Krogerus void serial8250_release_dma(struct uart_8250_port *p) 2559ee4b83eSHeikki Krogerus { 2569ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 2579ee4b83eSHeikki Krogerus 2589ee4b83eSHeikki Krogerus if (!dma) 2599ee4b83eSHeikki Krogerus return; 2609ee4b83eSHeikki Krogerus 2619ee4b83eSHeikki Krogerus /* Release RX resources */ 2628d170472SAndy Shevchenko dmaengine_terminate_sync(dma->rxchan); 2639ee4b83eSHeikki Krogerus dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf, 2649ee4b83eSHeikki Krogerus dma->rx_addr); 2659ee4b83eSHeikki Krogerus dma_release_channel(dma->rxchan); 2669ee4b83eSHeikki Krogerus dma->rxchan = NULL; 2679ee4b83eSHeikki Krogerus 2689ee4b83eSHeikki Krogerus /* Release TX resources */ 2698d170472SAndy Shevchenko dmaengine_terminate_sync(dma->txchan); 2709ee4b83eSHeikki Krogerus dma_unmap_single(dma->txchan->device->dev, dma->tx_addr, 2719ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 2729ee4b83eSHeikki Krogerus dma_release_channel(dma->txchan); 2739ee4b83eSHeikki Krogerus dma->txchan = NULL; 2749ee4b83eSHeikki Krogerus dma->tx_running = 0; 2759ee4b83eSHeikki Krogerus 2769ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "dma channels released\n"); 2779ee4b83eSHeikki Krogerus } 2789ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_release_dma); 279