Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

mtd: hyperbus: hbmc-am654: Add DMA support for reads

AM654 HyperBus controller provides MMIO interface to read data from
flash. So add DMA memcpy support for reading data over MMIO interface.
This provides 5x improvement in throughput and reduces CPU usage as
well.

Signed-off-by: Vignesh Raghavendra <vigneshr@ti.com>
Reviewed-by: Peter Ujfalusi <peter.ujfalusi@ti.com>
Link: https://lore.kernel.org/r/20200924081214.16934-5-vigneshr@ti.com

+127 -3
+127 -3
drivers/mtd/hyperbus/hbmc-am654.c
··· 3 3 // Copyright (C) 2019 Texas Instruments Incorporated - https://www.ti.com/ 4 4 // Author: Vignesh Raghavendra <vigneshr@ti.com> 5 5 6 + #include <linux/completion.h> 7 + #include <linux/dma-direction.h> 8 + #include <linux/dma-mapping.h> 9 + #include <linux/dmaengine.h> 6 10 #include <linux/err.h> 7 11 #include <linux/kernel.h> 8 12 #include <linux/module.h> ··· 17 13 #include <linux/of.h> 18 14 #include <linux/of_address.h> 19 15 #include <linux/platform_device.h> 16 + #include <linux/sched/task_stack.h> 20 17 #include <linux/types.h> 21 18 22 19 #define AM654_HBMC_CALIB_COUNT 25 20 + 21 + struct am654_hbmc_device_priv { 22 + struct completion rx_dma_complete; 23 + phys_addr_t device_base; 24 + struct hyperbus_ctlr *ctlr; 25 + struct dma_chan *rx_chan; 26 + }; 23 27 24 28 struct am654_hbmc_priv { 25 29 struct hyperbus_ctlr ctlr; ··· 63 51 return ret; 64 52 } 65 53 54 + static void am654_hbmc_dma_callback(void *param) 55 + { 56 + struct am654_hbmc_device_priv *priv = param; 57 + 58 + complete(&priv->rx_dma_complete); 59 + } 60 + 61 + static int am654_hbmc_dma_read(struct am654_hbmc_device_priv *priv, void *to, 62 + unsigned long from, ssize_t len) 63 + 64 + { 65 + enum dma_ctrl_flags flags = DMA_CTRL_ACK | DMA_PREP_INTERRUPT; 66 + struct dma_chan *rx_chan = priv->rx_chan; 67 + struct dma_async_tx_descriptor *tx; 68 + dma_addr_t dma_dst, dma_src; 69 + dma_cookie_t cookie; 70 + int ret; 71 + 72 + if (!priv->rx_chan || !virt_addr_valid(to) || object_is_on_stack(to)) 73 + return -EINVAL; 74 + 75 + dma_dst = dma_map_single(rx_chan->device->dev, to, len, DMA_FROM_DEVICE); 76 + if (dma_mapping_error(rx_chan->device->dev, dma_dst)) { 77 + dev_dbg(priv->ctlr->dev, "DMA mapping failed\n"); 78 + return -EIO; 79 + } 80 + 81 + dma_src = priv->device_base + from; 82 + tx = dmaengine_prep_dma_memcpy(rx_chan, dma_dst, dma_src, len, flags); 83 + if (!tx) { 84 + dev_err(priv->ctlr->dev, "device_prep_dma_memcpy error\n"); 85 + ret = -EIO; 86 + goto unmap_dma; 87 + } 88 + 89 + reinit_completion(&priv->rx_dma_complete); 90 + tx->callback = am654_hbmc_dma_callback; 91 + tx->callback_param = priv; 92 + cookie = dmaengine_submit(tx); 93 + 94 + ret = dma_submit_error(cookie); 95 + if (ret) { 96 + dev_err(priv->ctlr->dev, "dma_submit_error %d\n", cookie); 97 + goto unmap_dma; 98 + } 99 + 100 + dma_async_issue_pending(rx_chan); 101 + if (!wait_for_completion_timeout(&priv->rx_dma_complete, msecs_to_jiffies(len + 1000))) { 102 + dmaengine_terminate_sync(rx_chan); 103 + dev_err(priv->ctlr->dev, "DMA wait_for_completion_timeout\n"); 104 + ret = -ETIMEDOUT; 105 + } 106 + 107 + unmap_dma: 108 + dma_unmap_single(rx_chan->device->dev, dma_dst, len, DMA_FROM_DEVICE); 109 + return ret; 110 + } 111 + 112 + static void am654_hbmc_read(struct hyperbus_device *hbdev, void *to, 113 + unsigned long from, ssize_t len) 114 + { 115 + struct am654_hbmc_device_priv *priv = hbdev->priv; 116 + 117 + if (len < SZ_1K || am654_hbmc_dma_read(priv, to, from, len)) 118 + memcpy_fromio(to, hbdev->map.virt + from, len); 119 + } 120 + 66 121 static const struct hyperbus_ops am654_hbmc_ops = { 67 122 .calibrate = am654_hbmc_calibrate, 123 + .copy_from = am654_hbmc_read, 68 124 }; 125 + 126 + static int am654_hbmc_request_mmap_dma(struct am654_hbmc_device_priv *priv) 127 + { 128 + struct dma_chan *rx_chan; 129 + dma_cap_mask_t mask; 130 + 131 + dma_cap_zero(mask); 132 + dma_cap_set(DMA_MEMCPY, mask); 133 + 134 + rx_chan = dma_request_chan_by_mask(&mask); 135 + if (IS_ERR(rx_chan)) { 136 + if (PTR_ERR(rx_chan) == -EPROBE_DEFER) 137 + return -EPROBE_DEFER; 138 + dev_dbg(priv->ctlr->dev, "No DMA channel available\n"); 139 + return 0; 140 + } 141 + priv->rx_chan = rx_chan; 142 + init_completion(&priv->rx_dma_complete); 143 + 144 + return 0; 145 + } 69 146 70 147 static int am654_hbmc_probe(struct platform_device *pdev) 71 148 { 72 149 struct device_node *np = pdev->dev.of_node; 150 + struct am654_hbmc_device_priv *dev_priv; 73 151 struct device *dev = &pdev->dev; 74 152 struct am654_hbmc_priv *priv; 75 153 struct resource res; ··· 198 96 priv->ctlr.dev = dev; 199 97 priv->ctlr.ops = &am654_hbmc_ops; 200 98 priv->hbdev.ctlr = &priv->ctlr; 201 - ret = hyperbus_register_device(&priv->hbdev); 202 - if (ret) { 203 - dev_err(dev, "failed to register controller\n"); 99 + 100 + dev_priv = devm_kzalloc(dev, sizeof(*dev_priv), GFP_KERNEL); 101 + if (!dev_priv) { 102 + ret = -ENOMEM; 204 103 goto disable_mux; 205 104 } 206 105 106 + priv->hbdev.priv = dev_priv; 107 + dev_priv->device_base = res.start; 108 + dev_priv->ctlr = &priv->ctlr; 109 + 110 + ret = am654_hbmc_request_mmap_dma(dev_priv); 111 + if (ret) 112 + goto disable_mux; 113 + 114 + ret = hyperbus_register_device(&priv->hbdev); 115 + if (ret) { 116 + dev_err(dev, "failed to register controller\n"); 117 + goto release_dma; 118 + } 119 + 207 120 return 0; 121 + release_dma: 122 + if (dev_priv->rx_chan) 123 + dma_release_channel(dev_priv->rx_chan); 208 124 disable_mux: 209 125 if (priv->mux_ctrl) 210 126 mux_control_deselect(priv->mux_ctrl); ··· 232 112 static int am654_hbmc_remove(struct platform_device *pdev) 233 113 { 234 114 struct am654_hbmc_priv *priv = platform_get_drvdata(pdev); 115 + struct am654_hbmc_device_priv *dev_priv = priv->hbdev.priv; 235 116 int ret; 236 117 237 118 ret = hyperbus_unregister_device(&priv->hbdev); 238 119 if (priv->mux_ctrl) 239 120 mux_control_deselect(priv->mux_ctrl); 121 + 122 + if (dev_priv->rx_chan) 123 + dma_release_channel(dev_priv->rx_chan); 240 124 241 125 return ret; 242 126 }