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; 239ee4b83eSHeikki Krogerus 249ee4b83eSHeikki Krogerus dma->tx_running = 0; 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 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 36*5ea5b24dSHeikki Krogerus if (!uart_circ_empty(xmit) && !uart_tx_stopped(&p->port)) 379ee4b83eSHeikki Krogerus serial8250_tx_dma(p); 389ee4b83eSHeikki Krogerus } 399ee4b83eSHeikki Krogerus 409ee4b83eSHeikki Krogerus static void __dma_rx_complete(void *param) 419ee4b83eSHeikki Krogerus { 429ee4b83eSHeikki Krogerus struct uart_8250_port *p = param; 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 489ee4b83eSHeikki Krogerus dma_sync_single_for_cpu(dma->rxchan->device->dev, dma->rx_addr, 499ee4b83eSHeikki Krogerus dma->rx_size, DMA_FROM_DEVICE); 509ee4b83eSHeikki Krogerus 519ee4b83eSHeikki Krogerus dmaengine_tx_status(dma->rxchan, dma->rx_cookie, &state); 529ee4b83eSHeikki Krogerus dmaengine_terminate_all(dma->rxchan); 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; 679ee4b83eSHeikki Krogerus 68*5ea5b24dSHeikki Krogerus if (uart_tx_stopped(&p->port) || dma->tx_running || 69*5ea5b24dSHeikki Krogerus uart_circ_empty(xmit)) 70*5ea5b24dSHeikki Krogerus return 0; 719ee4b83eSHeikki Krogerus 729ee4b83eSHeikki Krogerus dma->tx_size = CIRC_CNT_TO_END(xmit->head, xmit->tail, UART_XMIT_SIZE); 739ee4b83eSHeikki Krogerus 749ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->txchan, 759ee4b83eSHeikki Krogerus dma->tx_addr + xmit->tail, 769ee4b83eSHeikki Krogerus dma->tx_size, DMA_MEM_TO_DEV, 779ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 789ee4b83eSHeikki Krogerus if (!desc) 799ee4b83eSHeikki Krogerus return -EBUSY; 809ee4b83eSHeikki Krogerus 819ee4b83eSHeikki Krogerus dma->tx_running = 1; 829ee4b83eSHeikki Krogerus 839ee4b83eSHeikki Krogerus desc->callback = __dma_tx_complete; 849ee4b83eSHeikki Krogerus desc->callback_param = p; 859ee4b83eSHeikki Krogerus 869ee4b83eSHeikki Krogerus dma->tx_cookie = dmaengine_submit(desc); 879ee4b83eSHeikki Krogerus 889ee4b83eSHeikki Krogerus dma_sync_single_for_device(dma->txchan->device->dev, dma->tx_addr, 899ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 909ee4b83eSHeikki Krogerus 919ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->txchan); 929ee4b83eSHeikki Krogerus 939ee4b83eSHeikki Krogerus return 0; 949ee4b83eSHeikki Krogerus } 959ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_tx_dma); 969ee4b83eSHeikki Krogerus 979ee4b83eSHeikki Krogerus int serial8250_rx_dma(struct uart_8250_port *p, unsigned int iir) 989ee4b83eSHeikki Krogerus { 999ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 1009ee4b83eSHeikki Krogerus struct dma_async_tx_descriptor *desc; 1019ee4b83eSHeikki Krogerus struct dma_tx_state state; 1029ee4b83eSHeikki Krogerus int dma_status; 1039ee4b83eSHeikki Krogerus 1049ee4b83eSHeikki Krogerus /* 1059ee4b83eSHeikki Krogerus * If RCVR FIFO trigger level was not reached, complete the transfer and 1069ee4b83eSHeikki Krogerus * let 8250.c copy the remaining data. 1079ee4b83eSHeikki Krogerus */ 1089ee4b83eSHeikki Krogerus if ((iir & 0x3f) == UART_IIR_RX_TIMEOUT) { 1099ee4b83eSHeikki Krogerus dma_status = dmaengine_tx_status(dma->rxchan, dma->rx_cookie, 1109ee4b83eSHeikki Krogerus &state); 1119ee4b83eSHeikki Krogerus if (dma_status == DMA_IN_PROGRESS) { 1129ee4b83eSHeikki Krogerus dmaengine_pause(dma->rxchan); 1139ee4b83eSHeikki Krogerus __dma_rx_complete(p); 1149ee4b83eSHeikki Krogerus } 1159ee4b83eSHeikki Krogerus return -ETIMEDOUT; 1169ee4b83eSHeikki Krogerus } 1179ee4b83eSHeikki Krogerus 1189ee4b83eSHeikki Krogerus desc = dmaengine_prep_slave_single(dma->rxchan, dma->rx_addr, 1199ee4b83eSHeikki Krogerus dma->rx_size, DMA_DEV_TO_MEM, 1209ee4b83eSHeikki Krogerus DMA_PREP_INTERRUPT | DMA_CTRL_ACK); 1219ee4b83eSHeikki Krogerus if (!desc) 1229ee4b83eSHeikki Krogerus return -EBUSY; 1239ee4b83eSHeikki Krogerus 1249ee4b83eSHeikki Krogerus desc->callback = __dma_rx_complete; 1259ee4b83eSHeikki Krogerus desc->callback_param = p; 1269ee4b83eSHeikki Krogerus 1279ee4b83eSHeikki Krogerus dma->rx_cookie = dmaengine_submit(desc); 1289ee4b83eSHeikki Krogerus 1299ee4b83eSHeikki Krogerus dma_sync_single_for_device(dma->rxchan->device->dev, dma->rx_addr, 1309ee4b83eSHeikki Krogerus dma->rx_size, DMA_FROM_DEVICE); 1319ee4b83eSHeikki Krogerus 1329ee4b83eSHeikki Krogerus dma_async_issue_pending(dma->rxchan); 1339ee4b83eSHeikki Krogerus 1349ee4b83eSHeikki Krogerus return 0; 1359ee4b83eSHeikki Krogerus } 1369ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_rx_dma); 1379ee4b83eSHeikki Krogerus 1389ee4b83eSHeikki Krogerus int serial8250_request_dma(struct uart_8250_port *p) 1399ee4b83eSHeikki Krogerus { 1409ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 1419ee4b83eSHeikki Krogerus dma_cap_mask_t mask; 1429ee4b83eSHeikki Krogerus 1439ee4b83eSHeikki Krogerus dma->rxconf.src_addr = p->port.mapbase + UART_RX; 1449ee4b83eSHeikki Krogerus dma->txconf.dst_addr = p->port.mapbase + UART_TX; 1459ee4b83eSHeikki Krogerus 1469ee4b83eSHeikki Krogerus dma_cap_zero(mask); 1479ee4b83eSHeikki Krogerus dma_cap_set(DMA_SLAVE, mask); 1489ee4b83eSHeikki Krogerus 1499ee4b83eSHeikki Krogerus /* Get a channel for RX */ 1509ee4b83eSHeikki Krogerus dma->rxchan = dma_request_channel(mask, dma->fn, dma->rx_param); 1519ee4b83eSHeikki Krogerus if (!dma->rxchan) 1529ee4b83eSHeikki Krogerus return -ENODEV; 1539ee4b83eSHeikki Krogerus 1549ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->rxchan, &dma->rxconf); 1559ee4b83eSHeikki Krogerus 1569ee4b83eSHeikki Krogerus /* Get a channel for TX */ 1579ee4b83eSHeikki Krogerus dma->txchan = dma_request_channel(mask, dma->fn, dma->tx_param); 1589ee4b83eSHeikki Krogerus if (!dma->txchan) { 1599ee4b83eSHeikki Krogerus dma_release_channel(dma->rxchan); 1609ee4b83eSHeikki Krogerus return -ENODEV; 1619ee4b83eSHeikki Krogerus } 1629ee4b83eSHeikki Krogerus 1639ee4b83eSHeikki Krogerus dmaengine_slave_config(dma->txchan, &dma->txconf); 1649ee4b83eSHeikki Krogerus 1659ee4b83eSHeikki Krogerus /* RX buffer */ 1669ee4b83eSHeikki Krogerus if (!dma->rx_size) 1679ee4b83eSHeikki Krogerus dma->rx_size = PAGE_SIZE; 1689ee4b83eSHeikki Krogerus 1699ee4b83eSHeikki Krogerus dma->rx_buf = dma_alloc_coherent(dma->rxchan->device->dev, dma->rx_size, 1709ee4b83eSHeikki Krogerus &dma->rx_addr, GFP_KERNEL); 1719ee4b83eSHeikki Krogerus if (!dma->rx_buf) { 1729ee4b83eSHeikki Krogerus dma_release_channel(dma->rxchan); 1739ee4b83eSHeikki Krogerus dma_release_channel(dma->txchan); 1749ee4b83eSHeikki Krogerus return -ENOMEM; 1759ee4b83eSHeikki Krogerus } 1769ee4b83eSHeikki Krogerus 1779ee4b83eSHeikki Krogerus /* TX buffer */ 1789ee4b83eSHeikki Krogerus dma->tx_addr = dma_map_single(dma->txchan->device->dev, 1799ee4b83eSHeikki Krogerus p->port.state->xmit.buf, 1809ee4b83eSHeikki Krogerus UART_XMIT_SIZE, 1819ee4b83eSHeikki Krogerus DMA_TO_DEVICE); 1829ee4b83eSHeikki Krogerus 1839ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "got both dma channels\n"); 1849ee4b83eSHeikki Krogerus 1859ee4b83eSHeikki Krogerus return 0; 1869ee4b83eSHeikki Krogerus } 1879ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_request_dma); 1889ee4b83eSHeikki Krogerus 1899ee4b83eSHeikki Krogerus void serial8250_release_dma(struct uart_8250_port *p) 1909ee4b83eSHeikki Krogerus { 1919ee4b83eSHeikki Krogerus struct uart_8250_dma *dma = p->dma; 1929ee4b83eSHeikki Krogerus 1939ee4b83eSHeikki Krogerus if (!dma) 1949ee4b83eSHeikki Krogerus return; 1959ee4b83eSHeikki Krogerus 1969ee4b83eSHeikki Krogerus /* Release RX resources */ 1979ee4b83eSHeikki Krogerus dmaengine_terminate_all(dma->rxchan); 1989ee4b83eSHeikki Krogerus dma_free_coherent(dma->rxchan->device->dev, dma->rx_size, dma->rx_buf, 1999ee4b83eSHeikki Krogerus dma->rx_addr); 2009ee4b83eSHeikki Krogerus dma_release_channel(dma->rxchan); 2019ee4b83eSHeikki Krogerus dma->rxchan = NULL; 2029ee4b83eSHeikki Krogerus 2039ee4b83eSHeikki Krogerus /* Release TX resources */ 2049ee4b83eSHeikki Krogerus dmaengine_terminate_all(dma->txchan); 2059ee4b83eSHeikki Krogerus dma_unmap_single(dma->txchan->device->dev, dma->tx_addr, 2069ee4b83eSHeikki Krogerus UART_XMIT_SIZE, DMA_TO_DEVICE); 2079ee4b83eSHeikki Krogerus dma_release_channel(dma->txchan); 2089ee4b83eSHeikki Krogerus dma->txchan = NULL; 2099ee4b83eSHeikki Krogerus dma->tx_running = 0; 2109ee4b83eSHeikki Krogerus 2119ee4b83eSHeikki Krogerus dev_dbg_ratelimited(p->port.dev, "dma channels released\n"); 2129ee4b83eSHeikki Krogerus } 2139ee4b83eSHeikki Krogerus EXPORT_SYMBOL_GPL(serial8250_release_dma); 214