19ee4b83eSHeikki Krogerus /* 29ee4b83eSHeikki Krogerus * 8250_dma.c - DMA Engine API support for 8250.c 39ee4b83eSHeikki Krogerus * 49ee4b83eSHeikki Krogerus * Copyright (C) 2013 Intel Corporation 59ee4b83eSHeikki Krogerus * 69ee4b83eSHeikki Krogerus * This program is free software; you can redistribute it and/or modify 79ee4b83eSHeikki Krogerus * it under the terms of the GNU General Public License as published by 89ee4b83eSHeikki Krogerus * the Free Software Foundation; either version 2 of the License, or 99ee4b83eSHeikki Krogerus * (at your option) any later version. 109ee4b83eSHeikki Krogerus */ 119ee4b83eSHeikki Krogerus #include <linux/tty.h> 129ee4b83eSHeikki Krogerus #include <linux/tty_flip.h> 139ee4b83eSHeikki Krogerus #include <linux/serial_reg.h> 149ee4b83eSHeikki Krogerus #include <linux/dma-mapping.h> 159ee4b83eSHeikki Krogerus 169ee4b83eSHeikki Krogerus #include "8250.h" 179ee4b83eSHeikki Krogerus 189ee4b83eSHeikki Krogerus static void __dma_tx_complete(void *param) 199ee4b83eSHeikki Krogerus { 209ee4b83eSHeikki Krogerus struct uart_8250_port *p = param; 219ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 229ee4b83eSHeikki Krogerus struct circ_buf *xmit = &p->port.state->xmit; 23f8fd1b03SLoic Poulain unsigned long flags; 242dc98946SAndy Shevchenko int ret; 259ee4b83eSHeikki Krogerus 269ee4b83eSHeikki Krogerus dma_sync_single_for_cpu(dma->txchan->device->dev, dma->tx_addr, 279ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 289ee4b83eSHeikki Krogerus 29f8fd1b03SLoic Poulain spin_lock_irqsave(&p->port.lock, flags); 30f8fd1b03SLoic Poulain 31f8fd1b03SLoic Poulain dma->tx_running = 0; 32f8fd1b03SLoic Poulain 339ee4b83eSHeikki Krogerus xmit->tail += dma->tx_size; 349ee4b83eSHeikki Krogerus xmit->tail &= UART_XMIT_SIZE - 1; 359ee4b83eSHeikki Krogerus p->port.icount.tx += dma->tx_size; 369ee4b83eSHeikki Krogerus 379ee4b83eSHeikki Krogerus if (uart_circ_chars_pending(xmit) < WAKEUP_CHARS) 389ee4b83eSHeikki Krogerus uart_write_wakeup(&p->port); 399ee4b83eSHeikki Krogerus 40b2202821SSebastian Andrzej Siewior ret = serial8250_tx_dma(p); 41b2202821SSebastian Andrzej Siewior if (ret) { 42b2202821SSebastian Andrzej Siewior p->ier |= UART_IER_THRI; 43b2202821SSebastian Andrzej Siewior serial_port_out(&p->port, UART_IER, p->ier); 44b2202821SSebastian Andrzej Siewior } 45f8fd1b03SLoic Poulain 46f8fd1b03SLoic Poulain spin_unlock_irqrestore(&p->port.lock, flags); 479ee4b83eSHeikki Krogerus } 489ee4b83eSHeikki Krogerus 499ee4b83eSHeikki Krogerus static void __dma_rx_complete(void *param) 509ee4b83eSHeikki Krogerus { 519ee4b83eSHeikki Krogerus struct uart_8250_port *p = param; 529ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 536f3fe3b1SHeikki Krogerus struct tty_port *tty_port = &p->port.state->port; 549ee4b83eSHeikki Krogerus struct dma_tx_state state; 556f3fe3b1SHeikki Krogerus int count; 569ee4b83eSHeikki Krogerus 570fcb7901SSebastian Andrzej Siewior dma->rx_running = 0; 589ee4b83eSHeikki Krogerus dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state); 599ee4b83eSHeikki Krogerus 606f3fe3b1SHeikki Krogerus count = dma->rx_size - state.residue; 619ee4b83eSHeikki Krogerus 626f3fe3b1SHeikki Krogerus tty_insert_flip_string(tty_port, dma->rx_buf, count); 636f3fe3b1SHeikki Krogerus p->port.icount.rx += count; 646f3fe3b1SHeikki Krogerus 656f3fe3b1SHeikki Krogerus tty_flip_buffer_push(tty_port); 669ee4b83eSHeikki Krogerus } 679ee4b83eSHeikki Krogerus 689ee4b83eSHeikki Krogerus int serial8250_tx_dma(struct uart_8250_port *p) 699ee4b83eSHeikki Krogerus { 709ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 719ee4b83eSHeikki Krogerus struct circ_buf *xmit = &p->port.state->xmit; 729ee4b83eSHeikki Krogerus struct dma_async_tx_descriptor *desc; 73b2202821SSebastian Andrzej Siewior int ret; 749ee4b83eSHeikki Krogerus 755ea5b24dSHeikki Krogerus if (uart_tx_stopped(&p->port) || dma->tx_running || 765ea5b24dSHeikki Krogerus uart_circ_empty(xmit)) 775ea5b24dSHeikki Krogerus return 0; 789ee4b83eSHeikki Krogerus 799ee4b83eSHeikki Krogerus dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 809ee4b83eSHeikki Krogerus 819ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->txchan, 829ee4b83eSHeikki Krogerus dma->tx_addr + xmit->tail, 839ee4b83eSHeikki Krogerus dma->tx_size, DMA_MEM_TO_DEV, 849ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 85b2202821SSebastian Andrzej Siewior if (!desc) { 86b2202821SSebastian Andrzej Siewior ret = -EBUSY; 87b2202821SSebastian Andrzej Siewior goto err; 88b2202821SSebastian Andrzej Siewior } 899ee4b83eSHeikki Krogerus 909ee4b83eSHeikki Krogerus dma->tx_running = 1; 919ee4b83eSHeikki Krogerus desc->callback = __dma_tx_complete; 929ee4b83eSHeikki Krogerus desc->callback_param = p; 939ee4b83eSHeikki Krogerus 949ee4b83eSHeikki Krogerus dma->tx_cookie = dmaengine_submit(desc); 959ee4b83eSHeikki Krogerus 969ee4b83eSHeikki Krogerus dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr, 979ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 989ee4b83eSHeikki Krogerus 999ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->txchan); 100b2202821SSebastian Andrzej Siewior if (dma->tx_err) { 101b2202821SSebastian Andrzej Siewior dma->tx_err = 0; 102b2202821SSebastian Andrzej Siewior if (p->ier & UART_IER_THRI) { 103b2202821SSebastian Andrzej Siewior p->ier &= ~UART_IER_THRI; 104b2202821SSebastian Andrzej Siewior serial_out(p, UART_IER, p->ier); 105b2202821SSebastian Andrzej Siewior } 106b2202821SSebastian Andrzej Siewior } 1079ee4b83eSHeikki Krogerus return 0; 108b2202821SSebastian Andrzej Siewior err: 109b2202821SSebastian Andrzej Siewior dma->tx_err = 1; 110b2202821SSebastian Andrzej Siewior return ret; 1119ee4b83eSHeikki Krogerus } 1129ee4b83eSHeikki Krogerus 11333d9b8b2SPeter Hurley int serial8250_rx_dma(struct uart_8250_port *p) 1149ee4b83eSHeikki Krogerus { 1159ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 1169ee4b83eSHeikki Krogerus struct dma_async_tx_descriptor *desc; 11775df022bSHeikki Krogerus 1180fcb7901SSebastian Andrzej Siewior if (dma->rx_running) 11975df022bSHeikki Krogerus return 0; 12075df022bSHeikki Krogerus 1219ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr, 1229ee4b83eSHeikki Krogerus dma->rx_size, DMA_DEV_TO_MEM, 1239ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 1249ee4b83eSHeikki Krogerus if (!desc) 1259ee4b83eSHeikki Krogerus return -EBUSY; 1269ee4b83eSHeikki Krogerus 1270fcb7901SSebastian Andrzej Siewior dma->rx_running = 1; 1289ee4b83eSHeikki Krogerus desc->callback = __dma_rx_complete; 1299ee4b83eSHeikki Krogerus desc->callback_param = p; 1309ee4b83eSHeikki Krogerus 1319ee4b83eSHeikki Krogerus dma->rx_cookie = dmaengine_submit(desc); 1329ee4b83eSHeikki Krogerus 1339ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->rxchan); 1349ee4b83eSHeikki Krogerus 1359ee4b83eSHeikki Krogerus return 0; 1369ee4b83eSHeikki Krogerus } 1379ee4b83eSHeikki Krogerus 13833d9b8b2SPeter Hurley void serial8250_rx_dma_flush(struct uart_8250_port *p) 13933d9b8b2SPeter Hurley { 14033d9b8b2SPeter Hurley struct uart_8250_dma *dma = p->dma; 14133d9b8b2SPeter Hurley 14233d9b8b2SPeter Hurley if (dma->rx_running) { 14333d9b8b2SPeter Hurley dmaengine_pause(dma->rxchan); 14433d9b8b2SPeter Hurley __dma_rx_complete(p); 1458d170472SAndy Shevchenko dmaengine_terminate_async(dma->rxchan); 14633d9b8b2SPeter Hurley } 14733d9b8b2SPeter Hurley } 148fd9e516dSChuah, Kim Tatt EXPORT_SYMBOL_GPL(serial8250_rx_dma_flush); 14933d9b8b2SPeter Hurley 1509ee4b83eSHeikki Krogerus int serial8250_request_dma(struct uart_8250_port *p) 1519ee4b83eSHeikki Krogerus { 1529ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 153*d1834babSAndy Shevchenko phys_addr_t rx_dma_addr = dma->rx_dma_addr ? 154*d1834babSAndy Shevchenko dma->rx_dma_addr : p->port.mapbase; 155*d1834babSAndy Shevchenko phys_addr_t tx_dma_addr = dma->tx_dma_addr ? 156*d1834babSAndy Shevchenko dma->tx_dma_addr : p->port.mapbase; 1579ee4b83eSHeikki Krogerus dma_cap_mask_t mask; 158ec5a11a9SPeter Hurley struct dma_slave_caps caps; 159ec5a11a9SPeter Hurley int ret; 1609ee4b83eSHeikki Krogerus 161ab19479aSHeikki Krogerus /* Default slave configuration parameters */ 162ab19479aSHeikki Krogerus dma->rxconf.direction = DMA_DEV_TO_MEM; 163ab19479aSHeikki Krogerus dma->rxconf.src_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 164*d1834babSAndy Shevchenko dma->rxconf.src_addr = rx_dma_addr + UART_RX; 165ab19479aSHeikki Krogerus 166ab19479aSHeikki Krogerus dma->txconf.direction = DMA_MEM_TO_DEV; 167ab19479aSHeikki Krogerus dma->txconf.dst_addr_width = DMA_SLAVE_BUSWIDTH_1_BYTE; 168*d1834babSAndy Shevchenko dma->txconf.dst_addr = tx_dma_addr + UART_TX; 1699ee4b83eSHeikki Krogerus 1709ee4b83eSHeikki Krogerus dma_cap_zero(mask); 1719ee4b83eSHeikki Krogerus dma_cap_set(DMA_SLAVE, mask); 1729ee4b83eSHeikki Krogerus 1739ee4b83eSHeikki Krogerus /* Get a channel for RX */ 174e4fb3b88SHeikki Krogerus dma->rxchan = dma_request_slave_channel_compat(mask, 175e4fb3b88SHeikki Krogerus dma->fn, dma->rx_param, 176e4fb3b88SHeikki Krogerus p->port.dev, "rx"); 1779ee4b83eSHeikki Krogerus if (!dma->rxchan) 1789ee4b83eSHeikki Krogerus return -ENODEV; 1799ee4b83eSHeikki Krogerus 180ec5a11a9SPeter Hurley /* 8250 rx dma requires dmaengine driver to support pause/terminate */ 181ec5a11a9SPeter Hurley ret = dma_get_slave_caps(dma->rxchan, &caps); 182ec5a11a9SPeter Hurley if (ret) 183ec5a11a9SPeter Hurley goto release_rx; 184ec5a11a9SPeter Hurley if (!caps.cmd_pause || !caps.cmd_terminate || 185ec5a11a9SPeter Hurley caps.residue_granularity == DMA_RESIDUE_GRANULARITY_DESCRIPTOR) { 186ec5a11a9SPeter Hurley ret = -EINVAL; 187ec5a11a9SPeter Hurley goto release_rx; 188ec5a11a9SPeter Hurley } 189ec5a11a9SPeter Hurley 1909ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->rxchan, &dma->rxconf); 1919ee4b83eSHeikki Krogerus 1929ee4b83eSHeikki Krogerus /* Get a channel for TX */ 193e4fb3b88SHeikki Krogerus dma->txchan = dma_request_slave_channel_compat(mask, 194e4fb3b88SHeikki Krogerus dma->fn, dma->tx_param, 195e4fb3b88SHeikki Krogerus p->port.dev, "tx"); 1969ee4b83eSHeikki Krogerus if (!dma->txchan) { 197ec5a11a9SPeter Hurley ret = -ENODEV; 198ec5a11a9SPeter Hurley goto release_rx; 1999ee4b83eSHeikki Krogerus } 2009ee4b83eSHeikki Krogerus 201ddedfd82SPeter Hurley /* 8250 tx dma requires dmaengine driver to support terminate */ 202ddedfd82SPeter Hurley ret = dma_get_slave_caps(dma->txchan, &caps); 203ddedfd82SPeter Hurley if (ret) 204ddedfd82SPeter Hurley goto err; 205ddedfd82SPeter Hurley if (!caps.cmd_terminate) { 206ddedfd82SPeter Hurley ret = -EINVAL; 207ddedfd82SPeter Hurley goto err; 208ddedfd82SPeter Hurley } 209ddedfd82SPeter Hurley 2109ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->txchan, &dma->txconf); 2119ee4b83eSHeikki Krogerus 2129ee4b83eSHeikki Krogerus /* RX buffer */ 2139ee4b83eSHeikki Krogerus if (!dma->rx_size) 2149ee4b83eSHeikki Krogerus dma->rx_size = PAGE_SIZE; 2159ee4b83eSHeikki Krogerus 2169ee4b83eSHeikki Krogerus dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size, 2179ee4b83eSHeikki Krogerus &dma->rx_addr, GFP_KERNEL); 218ec5a11a9SPeter Hurley if (!dma->rx_buf) { 219ec5a11a9SPeter Hurley ret = -ENOMEM; 220d4089a33SHeikki Krogerus goto err; 221ec5a11a9SPeter Hurley } 2229ee4b83eSHeikki Krogerus 2239ee4b83eSHeikki Krogerus /* TX buffer */ 2249ee4b83eSHeikki Krogerus dma->tx_addr = dma_map_single(dma->txchan->device->dev, 2259ee4b83eSHeikki Krogerus p->port.state->xmit.buf, 2269ee4b83eSHeikki Krogerus UART_XMIT_SIZE, 2279ee4b83eSHeikki Krogerus DMA_TO_DEVICE); 228d4089a33SHeikki Krogerus if (dma_mapping_error(dma->txchan->device->dev, dma->tx_addr)) { 229d4089a33SHeikki Krogerus dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, 230d4089a33SHeikki Krogerus dma->rx_buf, dma->rx_addr); 231ec5a11a9SPeter Hurley ret = -ENOMEM; 232d4089a33SHeikki Krogerus goto err; 233d4089a33SHeikki Krogerus } 2349ee4b83eSHeikki Krogerus 2359ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "got both dma channels\n"); 2369ee4b83eSHeikki Krogerus 2379ee4b83eSHeikki Krogerus return 0; 238d4089a33SHeikki Krogerus err: 239d4089a33SHeikki Krogerus dma_release_channel(dma->txchan); 240ec5a11a9SPeter Hurley release_rx: 241ec5a11a9SPeter Hurley dma_release_channel(dma->rxchan); 242ec5a11a9SPeter Hurley return ret; 2439ee4b83eSHeikki Krogerus } 2449ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_request_dma); 2459ee4b83eSHeikki Krogerus 2469ee4b83eSHeikki Krogerus void serial8250_release_dma(struct uart_8250_port *p) 2479ee4b83eSHeikki Krogerus { 2489ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 2499ee4b83eSHeikki Krogerus 2509ee4b83eSHeikki Krogerus if (!dma) 2519ee4b83eSHeikki Krogerus return; 2529ee4b83eSHeikki Krogerus 2539ee4b83eSHeikki Krogerus /* Release RX resources */ 2548d170472SAndy Shevchenko dmaengine_terminate_sync(dma->rxchan); 2559ee4b83eSHeikki Krogerus dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf, 2569ee4b83eSHeikki Krogerus dma->rx_addr); 2579ee4b83eSHeikki Krogerus dma_release_channel(dma->rxchan); 2589ee4b83eSHeikki Krogerus dma->rxchan = NULL; 2599ee4b83eSHeikki Krogerus 2609ee4b83eSHeikki Krogerus /* Release TX resources */ 2618d170472SAndy Shevchenko dmaengine_terminate_sync(dma->txchan); 2629ee4b83eSHeikki Krogerus dma_unmap_single(dma->txchan->device->dev, dma->tx_addr, 2639ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 2649ee4b83eSHeikki Krogerus dma_release_channel(dma->txchan); 2659ee4b83eSHeikki Krogerus dma->txchan = NULL; 2669ee4b83eSHeikki Krogerus dma->tx_running = 0; 2679ee4b83eSHeikki Krogerus 2689ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "dma channels released\n"); 2699ee4b83eSHeikki Krogerus } 2709ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_release_dma); 271