Thomas Gleixner | 9952f69 | 2019-05-28 10:10:04 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0-only |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 2 | /* |
| 3 | * DMA driver for Nvidia's Tegra20 APB DMA controller. |
| 4 | * |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 5 | * Copyright (c) 2012-2013, NVIDIA CORPORATION. All rights reserved. |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | #include <linux/bitops.h> |
| 9 | #include <linux/clk.h> |
| 10 | #include <linux/delay.h> |
| 11 | #include <linux/dmaengine.h> |
| 12 | #include <linux/dma-mapping.h> |
Thierry Reding | 7331205 | 2013-01-21 11:09:00 +0100 | [diff] [blame] | 13 | #include <linux/err.h> |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 14 | #include <linux/init.h> |
| 15 | #include <linux/interrupt.h> |
| 16 | #include <linux/io.h> |
| 17 | #include <linux/mm.h> |
| 18 | #include <linux/module.h> |
| 19 | #include <linux/of.h> |
| 20 | #include <linux/of_device.h> |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 21 | #include <linux/of_dma.h> |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 22 | #include <linux/platform_device.h> |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 23 | #include <linux/pm.h> |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 24 | #include <linux/pm_runtime.h> |
Stephen Warren | 9aa433d | 2013-11-06 16:35:34 -0700 | [diff] [blame] | 25 | #include <linux/reset.h> |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 26 | #include <linux/slab.h> |
| 27 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 28 | #include "dmaengine.h" |
| 29 | |
Ben Dooks | 95f295f | 2018-11-21 16:13:23 +0000 | [diff] [blame] | 30 | #define CREATE_TRACE_POINTS |
| 31 | #include <trace/events/tegra_apb_dma.h> |
| 32 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 33 | #define TEGRA_APBDMA_GENERAL 0x0 |
| 34 | #define TEGRA_APBDMA_GENERAL_ENABLE BIT(31) |
| 35 | |
| 36 | #define TEGRA_APBDMA_CONTROL 0x010 |
| 37 | #define TEGRA_APBDMA_IRQ_MASK 0x01c |
| 38 | #define TEGRA_APBDMA_IRQ_MASK_SET 0x020 |
| 39 | |
| 40 | /* CSR register */ |
| 41 | #define TEGRA_APBDMA_CHAN_CSR 0x00 |
| 42 | #define TEGRA_APBDMA_CSR_ENB BIT(31) |
| 43 | #define TEGRA_APBDMA_CSR_IE_EOC BIT(30) |
| 44 | #define TEGRA_APBDMA_CSR_HOLD BIT(29) |
| 45 | #define TEGRA_APBDMA_CSR_DIR BIT(28) |
| 46 | #define TEGRA_APBDMA_CSR_ONCE BIT(27) |
| 47 | #define TEGRA_APBDMA_CSR_FLOW BIT(21) |
| 48 | #define TEGRA_APBDMA_CSR_REQ_SEL_SHIFT 16 |
Shardar Shariff Md | 00ef449 | 2016-04-23 15:06:00 +0530 | [diff] [blame] | 49 | #define TEGRA_APBDMA_CSR_REQ_SEL_MASK 0x1F |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 50 | #define TEGRA_APBDMA_CSR_WCOUNT_MASK 0xFFFC |
| 51 | |
| 52 | /* STATUS register */ |
| 53 | #define TEGRA_APBDMA_CHAN_STATUS 0x004 |
| 54 | #define TEGRA_APBDMA_STATUS_BUSY BIT(31) |
| 55 | #define TEGRA_APBDMA_STATUS_ISE_EOC BIT(30) |
| 56 | #define TEGRA_APBDMA_STATUS_HALT BIT(29) |
| 57 | #define TEGRA_APBDMA_STATUS_PING_PONG BIT(28) |
| 58 | #define TEGRA_APBDMA_STATUS_COUNT_SHIFT 2 |
| 59 | #define TEGRA_APBDMA_STATUS_COUNT_MASK 0xFFFC |
| 60 | |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 61 | #define TEGRA_APBDMA_CHAN_CSRE 0x00C |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 62 | #define TEGRA_APBDMA_CHAN_CSRE_PAUSE BIT(31) |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 63 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 64 | /* AHB memory address */ |
| 65 | #define TEGRA_APBDMA_CHAN_AHBPTR 0x010 |
| 66 | |
| 67 | /* AHB sequence register */ |
| 68 | #define TEGRA_APBDMA_CHAN_AHBSEQ 0x14 |
| 69 | #define TEGRA_APBDMA_AHBSEQ_INTR_ENB BIT(31) |
| 70 | #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_8 (0 << 28) |
| 71 | #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_16 (1 << 28) |
| 72 | #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32 (2 << 28) |
| 73 | #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_64 (3 << 28) |
| 74 | #define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_128 (4 << 28) |
| 75 | #define TEGRA_APBDMA_AHBSEQ_DATA_SWAP BIT(27) |
| 76 | #define TEGRA_APBDMA_AHBSEQ_BURST_1 (4 << 24) |
| 77 | #define TEGRA_APBDMA_AHBSEQ_BURST_4 (5 << 24) |
| 78 | #define TEGRA_APBDMA_AHBSEQ_BURST_8 (6 << 24) |
| 79 | #define TEGRA_APBDMA_AHBSEQ_DBL_BUF BIT(19) |
| 80 | #define TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT 16 |
| 81 | #define TEGRA_APBDMA_AHBSEQ_WRAP_NONE 0 |
| 82 | |
| 83 | /* APB address */ |
| 84 | #define TEGRA_APBDMA_CHAN_APBPTR 0x018 |
| 85 | |
| 86 | /* APB sequence register */ |
| 87 | #define TEGRA_APBDMA_CHAN_APBSEQ 0x01c |
| 88 | #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8 (0 << 28) |
| 89 | #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16 (1 << 28) |
| 90 | #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32 (2 << 28) |
| 91 | #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64 (3 << 28) |
| 92 | #define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_128 (4 << 28) |
| 93 | #define TEGRA_APBDMA_APBSEQ_DATA_SWAP BIT(27) |
| 94 | #define TEGRA_APBDMA_APBSEQ_WRAP_WORD_1 (1 << 16) |
| 95 | |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 96 | /* Tegra148 specific registers */ |
| 97 | #define TEGRA_APBDMA_CHAN_WCOUNT 0x20 |
| 98 | |
| 99 | #define TEGRA_APBDMA_CHAN_WORD_TRANSFER 0x24 |
| 100 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 101 | /* |
| 102 | * If any burst is in flight and DMA paused then this is the time to complete |
| 103 | * on-flight burst and update DMA status register. |
| 104 | */ |
| 105 | #define TEGRA_APBDMA_BURST_COMPLETE_TIME 20 |
| 106 | |
| 107 | /* Channel base address offset from APBDMA base address */ |
| 108 | #define TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET 0x1000 |
| 109 | |
Shardar Shariff Md | 00ef449 | 2016-04-23 15:06:00 +0530 | [diff] [blame] | 110 | #define TEGRA_APBDMA_SLAVE_ID_INVALID (TEGRA_APBDMA_CSR_REQ_SEL_MASK + 1) |
| 111 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 112 | struct tegra_dma; |
| 113 | |
| 114 | /* |
| 115 | * tegra_dma_chip_data Tegra chip specific DMA data |
| 116 | * @nr_channels: Number of channels available in the controller. |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 117 | * @channel_reg_size: Channel register size/stride. |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 118 | * @max_dma_count: Maximum DMA transfer count supported by DMA controller. |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 119 | * @support_channel_pause: Support channel wise pause of dma. |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 120 | * @support_separate_wcount_reg: Support separate word count register. |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 121 | */ |
| 122 | struct tegra_dma_chip_data { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 123 | unsigned int nr_channels; |
| 124 | unsigned int channel_reg_size; |
| 125 | unsigned int max_dma_count; |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 126 | bool support_channel_pause; |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 127 | bool support_separate_wcount_reg; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | /* DMA channel registers */ |
| 131 | struct tegra_dma_channel_regs { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 132 | u32 csr; |
| 133 | u32 ahb_ptr; |
| 134 | u32 apb_ptr; |
| 135 | u32 ahb_seq; |
| 136 | u32 apb_seq; |
| 137 | u32 wcount; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | /* |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 141 | * tegra_dma_sg_req: DMA request details to configure hardware. This |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 142 | * contains the details for one transfer to configure DMA hw. |
| 143 | * The client's request for data transfer can be broken into multiple |
| 144 | * sub-transfer as per requester details and hw support. |
| 145 | * This sub transfer get added in the list of transfer and point to Tegra |
| 146 | * DMA descriptor which manages the transfer details. |
| 147 | */ |
| 148 | struct tegra_dma_sg_req { |
| 149 | struct tegra_dma_channel_regs ch_regs; |
Ben Dooks | 216a1d7 | 2018-11-21 16:13:20 +0000 | [diff] [blame] | 150 | unsigned int req_len; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 151 | bool configured; |
| 152 | bool last_sg; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 153 | struct list_head node; |
| 154 | struct tegra_dma_desc *dma_desc; |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 155 | unsigned int words_xferred; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 156 | }; |
| 157 | |
| 158 | /* |
| 159 | * tegra_dma_desc: Tegra DMA descriptors which manages the client requests. |
| 160 | * This descriptor keep track of transfer status, callbacks and request |
| 161 | * counts etc. |
| 162 | */ |
| 163 | struct tegra_dma_desc { |
| 164 | struct dma_async_tx_descriptor txd; |
Ben Dooks | 216a1d7 | 2018-11-21 16:13:20 +0000 | [diff] [blame] | 165 | unsigned int bytes_requested; |
| 166 | unsigned int bytes_transferred; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 167 | enum dma_status dma_status; |
| 168 | struct list_head node; |
| 169 | struct list_head tx_list; |
| 170 | struct list_head cb_node; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 171 | unsigned int cb_count; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 172 | }; |
| 173 | |
| 174 | struct tegra_dma_channel; |
| 175 | |
| 176 | typedef void (*dma_isr_handler)(struct tegra_dma_channel *tdc, |
| 177 | bool to_terminate); |
| 178 | |
| 179 | /* tegra_dma_channel: Channel specific information */ |
| 180 | struct tegra_dma_channel { |
| 181 | struct dma_chan dma_chan; |
Ben Dooks | 65c383c | 2018-11-21 16:13:22 +0000 | [diff] [blame] | 182 | char name[12]; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 183 | bool config_init; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 184 | unsigned int id; |
Jon Hunter | 13a3328 | 2015-08-06 14:32:31 +0100 | [diff] [blame] | 185 | void __iomem *chan_addr; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 186 | spinlock_t lock; |
| 187 | bool busy; |
| 188 | struct tegra_dma *tdma; |
| 189 | bool cyclic; |
| 190 | |
| 191 | /* Different lists for managing the requests */ |
| 192 | struct list_head free_sg_req; |
| 193 | struct list_head pending_sg_req; |
| 194 | struct list_head free_dma_desc; |
| 195 | struct list_head cb_desc; |
| 196 | |
| 197 | /* ISR handler and tasklet for bottom half of isr handling */ |
| 198 | dma_isr_handler isr_handler; |
| 199 | struct tasklet_struct tasklet; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 200 | |
| 201 | /* Channel-slave specific configuration */ |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 202 | unsigned int slave_id; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 203 | struct dma_slave_config dma_sconfig; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 204 | struct tegra_dma_channel_regs channel_reg; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 205 | }; |
| 206 | |
| 207 | /* tegra_dma: Tegra DMA specific information */ |
| 208 | struct tegra_dma { |
| 209 | struct dma_device dma_dev; |
| 210 | struct device *dev; |
| 211 | struct clk *dma_clk; |
Stephen Warren | 9aa433d | 2013-11-06 16:35:34 -0700 | [diff] [blame] | 212 | struct reset_control *rst; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 213 | spinlock_t global_lock; |
| 214 | void __iomem *base_addr; |
Laxman Dewangan | 83a1ef2 | 2012-08-29 10:23:07 +0200 | [diff] [blame] | 215 | const struct tegra_dma_chip_data *chip_data; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 216 | |
Jon Hunter | 23a1ec3 | 2015-08-06 14:32:33 +0100 | [diff] [blame] | 217 | /* |
| 218 | * Counter for managing global pausing of the DMA controller. |
| 219 | * Only applicable for devices that don't support individual |
| 220 | * channel pausing. |
| 221 | */ |
| 222 | u32 global_pause_count; |
| 223 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 224 | /* Last member of the structure */ |
| 225 | struct tegra_dma_channel channels[0]; |
| 226 | }; |
| 227 | |
| 228 | static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val) |
| 229 | { |
| 230 | writel(val, tdma->base_addr + reg); |
| 231 | } |
| 232 | |
| 233 | static inline u32 tdma_read(struct tegra_dma *tdma, u32 reg) |
| 234 | { |
| 235 | return readl(tdma->base_addr + reg); |
| 236 | } |
| 237 | |
| 238 | static inline void tdc_write(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 239 | u32 reg, u32 val) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 240 | { |
Jon Hunter | 13a3328 | 2015-08-06 14:32:31 +0100 | [diff] [blame] | 241 | writel(val, tdc->chan_addr + reg); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg) |
| 245 | { |
Jon Hunter | 13a3328 | 2015-08-06 14:32:31 +0100 | [diff] [blame] | 246 | return readl(tdc->chan_addr + reg); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc) |
| 250 | { |
| 251 | return container_of(dc, struct tegra_dma_channel, dma_chan); |
| 252 | } |
| 253 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 254 | static inline struct tegra_dma_desc * |
| 255 | txd_to_tegra_dma_desc(struct dma_async_tx_descriptor *td) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 256 | { |
| 257 | return container_of(td, struct tegra_dma_desc, txd); |
| 258 | } |
| 259 | |
| 260 | static inline struct device *tdc2dev(struct tegra_dma_channel *tdc) |
| 261 | { |
| 262 | return &tdc->dma_chan.dev->device; |
| 263 | } |
| 264 | |
| 265 | static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx); |
| 266 | static int tegra_dma_runtime_suspend(struct device *dev); |
| 267 | static int tegra_dma_runtime_resume(struct device *dev); |
| 268 | |
| 269 | /* Get DMA desc from free list, if not there then allocate it. */ |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 270 | static struct tegra_dma_desc *tegra_dma_desc_get(struct tegra_dma_channel *tdc) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 271 | { |
| 272 | struct tegra_dma_desc *dma_desc; |
| 273 | unsigned long flags; |
| 274 | |
| 275 | spin_lock_irqsave(&tdc->lock, flags); |
| 276 | |
| 277 | /* Do not allocate if desc are waiting for ack */ |
| 278 | list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) { |
| 279 | if (async_tx_test_ack(&dma_desc->txd)) { |
| 280 | list_del(&dma_desc->node); |
| 281 | spin_unlock_irqrestore(&tdc->lock, flags); |
Laxman Dewangan | b9bb37f | 2013-01-09 15:26:22 +0530 | [diff] [blame] | 282 | dma_desc->txd.flags = 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 283 | return dma_desc; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | spin_unlock_irqrestore(&tdc->lock, flags); |
| 288 | |
| 289 | /* Allocate DMA desc */ |
Jon Hunter | 8fe9739 | 2015-11-13 16:39:42 +0000 | [diff] [blame] | 290 | dma_desc = kzalloc(sizeof(*dma_desc), GFP_NOWAIT); |
Peter Griffin | aef94fe | 2016-06-07 18:38:41 +0100 | [diff] [blame] | 291 | if (!dma_desc) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 292 | return NULL; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 293 | |
| 294 | dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan); |
| 295 | dma_desc->txd.tx_submit = tegra_dma_tx_submit; |
| 296 | dma_desc->txd.flags = 0; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 297 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 298 | return dma_desc; |
| 299 | } |
| 300 | |
| 301 | static void tegra_dma_desc_put(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 302 | struct tegra_dma_desc *dma_desc) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 303 | { |
| 304 | unsigned long flags; |
| 305 | |
| 306 | spin_lock_irqsave(&tdc->lock, flags); |
| 307 | if (!list_empty(&dma_desc->tx_list)) |
| 308 | list_splice_init(&dma_desc->tx_list, &tdc->free_sg_req); |
| 309 | list_add_tail(&dma_desc->node, &tdc->free_dma_desc); |
| 310 | spin_unlock_irqrestore(&tdc->lock, flags); |
| 311 | } |
| 312 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 313 | static struct tegra_dma_sg_req * |
| 314 | tegra_dma_sg_req_get(struct tegra_dma_channel *tdc) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 315 | { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 316 | struct tegra_dma_sg_req *sg_req; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 317 | unsigned long flags; |
| 318 | |
| 319 | spin_lock_irqsave(&tdc->lock, flags); |
| 320 | if (!list_empty(&tdc->free_sg_req)) { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 321 | sg_req = list_first_entry(&tdc->free_sg_req, typeof(*sg_req), |
| 322 | node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 323 | list_del(&sg_req->node); |
| 324 | spin_unlock_irqrestore(&tdc->lock, flags); |
| 325 | return sg_req; |
| 326 | } |
| 327 | spin_unlock_irqrestore(&tdc->lock, flags); |
| 328 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 329 | sg_req = kzalloc(sizeof(*sg_req), GFP_NOWAIT); |
Peter Griffin | aef94fe | 2016-06-07 18:38:41 +0100 | [diff] [blame] | 330 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 331 | return sg_req; |
| 332 | } |
| 333 | |
| 334 | static int tegra_dma_slave_config(struct dma_chan *dc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 335 | struct dma_slave_config *sconfig) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 336 | { |
| 337 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
| 338 | |
| 339 | if (!list_empty(&tdc->pending_sg_req)) { |
| 340 | dev_err(tdc2dev(tdc), "Configuration not allowed\n"); |
| 341 | return -EBUSY; |
| 342 | } |
| 343 | |
| 344 | memcpy(&tdc->dma_sconfig, sconfig, sizeof(*sconfig)); |
Dmitry Osipenko | f6160f3 | 2017-11-16 20:11:06 +0300 | [diff] [blame] | 345 | if (tdc->slave_id == TEGRA_APBDMA_SLAVE_ID_INVALID && |
| 346 | sconfig->device_fc) { |
Shardar Shariff Md | 00ef449 | 2016-04-23 15:06:00 +0530 | [diff] [blame] | 347 | if (sconfig->slave_id > TEGRA_APBDMA_CSR_REQ_SEL_MASK) |
| 348 | return -EINVAL; |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 349 | tdc->slave_id = sconfig->slave_id; |
Shardar Shariff Md | 00ef449 | 2016-04-23 15:06:00 +0530 | [diff] [blame] | 350 | } |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 351 | tdc->config_init = true; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 352 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 353 | return 0; |
| 354 | } |
| 355 | |
| 356 | static void tegra_dma_global_pause(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 357 | bool wait_for_burst_complete) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 358 | { |
| 359 | struct tegra_dma *tdma = tdc->tdma; |
| 360 | |
| 361 | spin_lock(&tdma->global_lock); |
Jon Hunter | 23a1ec3 | 2015-08-06 14:32:33 +0100 | [diff] [blame] | 362 | |
| 363 | if (tdc->tdma->global_pause_count == 0) { |
| 364 | tdma_write(tdma, TEGRA_APBDMA_GENERAL, 0); |
| 365 | if (wait_for_burst_complete) |
| 366 | udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME); |
| 367 | } |
| 368 | |
| 369 | tdc->tdma->global_pause_count++; |
| 370 | |
| 371 | spin_unlock(&tdma->global_lock); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | static void tegra_dma_global_resume(struct tegra_dma_channel *tdc) |
| 375 | { |
| 376 | struct tegra_dma *tdma = tdc->tdma; |
| 377 | |
Jon Hunter | 23a1ec3 | 2015-08-06 14:32:33 +0100 | [diff] [blame] | 378 | spin_lock(&tdma->global_lock); |
| 379 | |
| 380 | if (WARN_ON(tdc->tdma->global_pause_count == 0)) |
| 381 | goto out; |
| 382 | |
| 383 | if (--tdc->tdma->global_pause_count == 0) |
| 384 | tdma_write(tdma, TEGRA_APBDMA_GENERAL, |
| 385 | TEGRA_APBDMA_GENERAL_ENABLE); |
| 386 | |
| 387 | out: |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 388 | spin_unlock(&tdma->global_lock); |
| 389 | } |
| 390 | |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 391 | static void tegra_dma_pause(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 392 | bool wait_for_burst_complete) |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 393 | { |
| 394 | struct tegra_dma *tdma = tdc->tdma; |
| 395 | |
| 396 | if (tdma->chip_data->support_channel_pause) { |
| 397 | tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 398 | TEGRA_APBDMA_CHAN_CSRE_PAUSE); |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 399 | if (wait_for_burst_complete) |
| 400 | udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME); |
| 401 | } else { |
| 402 | tegra_dma_global_pause(tdc, wait_for_burst_complete); |
| 403 | } |
| 404 | } |
| 405 | |
| 406 | static void tegra_dma_resume(struct tegra_dma_channel *tdc) |
| 407 | { |
| 408 | struct tegra_dma *tdma = tdc->tdma; |
| 409 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 410 | if (tdma->chip_data->support_channel_pause) |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 411 | tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, 0); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 412 | else |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 413 | tegra_dma_global_resume(tdc); |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 414 | } |
| 415 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 416 | static void tegra_dma_stop(struct tegra_dma_channel *tdc) |
| 417 | { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 418 | u32 csr, status; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 419 | |
| 420 | /* Disable interrupts */ |
| 421 | csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR); |
| 422 | csr &= ~TEGRA_APBDMA_CSR_IE_EOC; |
| 423 | tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr); |
| 424 | |
| 425 | /* Disable DMA */ |
| 426 | csr &= ~TEGRA_APBDMA_CSR_ENB; |
| 427 | tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr); |
| 428 | |
| 429 | /* Clear interrupt status if it is there */ |
| 430 | status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS); |
| 431 | if (status & TEGRA_APBDMA_STATUS_ISE_EOC) { |
| 432 | dev_dbg(tdc2dev(tdc), "%s():clearing interrupt\n", __func__); |
| 433 | tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status); |
| 434 | } |
| 435 | tdc->busy = false; |
| 436 | } |
| 437 | |
| 438 | static void tegra_dma_start(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 439 | struct tegra_dma_sg_req *sg_req) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 440 | { |
| 441 | struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs; |
| 442 | |
| 443 | tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, ch_regs->csr); |
| 444 | tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_regs->apb_seq); |
| 445 | tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_regs->apb_ptr); |
| 446 | tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_regs->ahb_seq); |
| 447 | tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_regs->ahb_ptr); |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 448 | if (tdc->tdma->chip_data->support_separate_wcount_reg) |
| 449 | tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT, ch_regs->wcount); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 450 | |
| 451 | /* Start DMA */ |
| 452 | tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 453 | ch_regs->csr | TEGRA_APBDMA_CSR_ENB); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 454 | } |
| 455 | |
| 456 | static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 457 | struct tegra_dma_sg_req *nsg_req) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 458 | { |
| 459 | unsigned long status; |
| 460 | |
| 461 | /* |
| 462 | * The DMA controller reloads the new configuration for next transfer |
| 463 | * after last burst of current transfer completes. |
| 464 | * If there is no IEC status then this makes sure that last burst |
| 465 | * has not be completed. There may be case that last burst is on |
| 466 | * flight and so it can complete but because DMA is paused, it |
| 467 | * will not generates interrupt as well as not reload the new |
| 468 | * configuration. |
| 469 | * If there is already IEC status then interrupt handler need to |
| 470 | * load new configuration. |
| 471 | */ |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 472 | tegra_dma_pause(tdc, false); |
Thierry Reding | 7b0e00d | 2016-06-14 16:18:46 +0200 | [diff] [blame] | 473 | status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 474 | |
| 475 | /* |
| 476 | * If interrupt is pending then do nothing as the ISR will handle |
| 477 | * the programing for new request. |
| 478 | */ |
| 479 | if (status & TEGRA_APBDMA_STATUS_ISE_EOC) { |
| 480 | dev_err(tdc2dev(tdc), |
| 481 | "Skipping new configuration as interrupt is pending\n"); |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 482 | tegra_dma_resume(tdc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 483 | return; |
| 484 | } |
| 485 | |
| 486 | /* Safe to program new configuration */ |
| 487 | tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, nsg_req->ch_regs.apb_ptr); |
| 488 | tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, nsg_req->ch_regs.ahb_ptr); |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 489 | if (tdc->tdma->chip_data->support_separate_wcount_reg) |
| 490 | tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 491 | nsg_req->ch_regs.wcount); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 492 | tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 493 | nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 494 | nsg_req->configured = true; |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 495 | nsg_req->words_xferred = 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 496 | |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 497 | tegra_dma_resume(tdc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | static void tdc_start_head_req(struct tegra_dma_channel *tdc) |
| 501 | { |
| 502 | struct tegra_dma_sg_req *sg_req; |
| 503 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 504 | sg_req = list_first_entry(&tdc->pending_sg_req, typeof(*sg_req), node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 505 | tegra_dma_start(tdc, sg_req); |
| 506 | sg_req->configured = true; |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 507 | sg_req->words_xferred = 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 508 | tdc->busy = true; |
| 509 | } |
| 510 | |
| 511 | static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc) |
| 512 | { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 513 | struct tegra_dma_sg_req *hsgreq, *hnsgreq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 514 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 515 | hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node); |
| 516 | if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 517 | hnsgreq = list_first_entry(&hsgreq->node, typeof(*hnsgreq), |
| 518 | node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 519 | tegra_dma_configure_for_next(tdc, hnsgreq); |
| 520 | } |
| 521 | } |
| 522 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 523 | static inline unsigned int |
| 524 | get_current_xferred_count(struct tegra_dma_channel *tdc, |
| 525 | struct tegra_dma_sg_req *sg_req, |
| 526 | unsigned long status) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 527 | { |
| 528 | return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4; |
| 529 | } |
| 530 | |
| 531 | static void tegra_dma_abort_all(struct tegra_dma_channel *tdc) |
| 532 | { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 533 | struct tegra_dma_desc *dma_desc; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 534 | struct tegra_dma_sg_req *sgreq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 535 | |
| 536 | while (!list_empty(&tdc->pending_sg_req)) { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 537 | sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), |
| 538 | node); |
Wei Yongjun | 2cc44e6 | 2012-09-05 15:08:56 +0800 | [diff] [blame] | 539 | list_move_tail(&sgreq->node, &tdc->free_sg_req); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 540 | if (sgreq->last_sg) { |
| 541 | dma_desc = sgreq->dma_desc; |
| 542 | dma_desc->dma_status = DMA_ERROR; |
| 543 | list_add_tail(&dma_desc->node, &tdc->free_dma_desc); |
| 544 | |
| 545 | /* Add in cb list if it is not there. */ |
| 546 | if (!dma_desc->cb_count) |
| 547 | list_add_tail(&dma_desc->cb_node, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 548 | &tdc->cb_desc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 549 | dma_desc->cb_count++; |
| 550 | } |
| 551 | } |
| 552 | tdc->isr_handler = NULL; |
| 553 | } |
| 554 | |
| 555 | static bool handle_continuous_head_request(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 556 | bool to_terminate) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 557 | { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 558 | struct tegra_dma_sg_req *hsgreq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 559 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 560 | /* |
| 561 | * Check that head req on list should be in flight. |
| 562 | * If it is not in flight then abort transfer as |
| 563 | * looping of transfer can not continue. |
| 564 | */ |
| 565 | hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node); |
| 566 | if (!hsgreq->configured) { |
| 567 | tegra_dma_stop(tdc); |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 568 | pm_runtime_put(tdc->tdma->dev); |
Dmitry Osipenko | 01b66a7 | 2020-02-09 19:33:56 +0300 | [diff] [blame^] | 569 | dev_err(tdc2dev(tdc), "DMA transfer underflow, aborting DMA\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 570 | tegra_dma_abort_all(tdc); |
| 571 | return false; |
| 572 | } |
| 573 | |
| 574 | /* Configure next request */ |
| 575 | if (!to_terminate) |
| 576 | tdc_configure_next_head_desc(tdc); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 577 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 578 | return true; |
| 579 | } |
| 580 | |
| 581 | static void handle_once_dma_done(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 582 | bool to_terminate) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 583 | { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 584 | struct tegra_dma_desc *dma_desc; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 585 | struct tegra_dma_sg_req *sgreq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 586 | |
| 587 | tdc->busy = false; |
| 588 | sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node); |
| 589 | dma_desc = sgreq->dma_desc; |
| 590 | dma_desc->bytes_transferred += sgreq->req_len; |
| 591 | |
| 592 | list_del(&sgreq->node); |
| 593 | if (sgreq->last_sg) { |
Vinod Koul | 00d696f | 2013-10-16 21:04:50 +0530 | [diff] [blame] | 594 | dma_desc->dma_status = DMA_COMPLETE; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 595 | dma_cookie_complete(&dma_desc->txd); |
| 596 | if (!dma_desc->cb_count) |
| 597 | list_add_tail(&dma_desc->cb_node, &tdc->cb_desc); |
| 598 | dma_desc->cb_count++; |
| 599 | list_add_tail(&dma_desc->node, &tdc->free_dma_desc); |
| 600 | } |
| 601 | list_add_tail(&sgreq->node, &tdc->free_sg_req); |
| 602 | |
| 603 | /* Do not start DMA if it is going to be terminate */ |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 604 | if (to_terminate) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 605 | return; |
| 606 | |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 607 | if (list_empty(&tdc->pending_sg_req)) { |
| 608 | pm_runtime_put(tdc->tdma->dev); |
| 609 | return; |
| 610 | } |
| 611 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 612 | tdc_start_head_req(tdc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 613 | } |
| 614 | |
| 615 | static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 616 | bool to_terminate) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 617 | { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 618 | struct tegra_dma_desc *dma_desc; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 619 | struct tegra_dma_sg_req *sgreq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 620 | bool st; |
| 621 | |
| 622 | sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node); |
| 623 | dma_desc = sgreq->dma_desc; |
Ben Dooks | e486df3 | 2018-11-21 16:13:19 +0000 | [diff] [blame] | 624 | /* if we dma for long enough the transfer count will wrap */ |
| 625 | dma_desc->bytes_transferred = |
| 626 | (dma_desc->bytes_transferred + sgreq->req_len) % |
| 627 | dma_desc->bytes_requested; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 628 | |
| 629 | /* Callback need to be call */ |
| 630 | if (!dma_desc->cb_count) |
| 631 | list_add_tail(&dma_desc->cb_node, &tdc->cb_desc); |
| 632 | dma_desc->cb_count++; |
| 633 | |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 634 | sgreq->words_xferred = 0; |
| 635 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 636 | /* If not last req then put at end of pending list */ |
| 637 | if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) { |
Wei Yongjun | 2cc44e6 | 2012-09-05 15:08:56 +0800 | [diff] [blame] | 638 | list_move_tail(&sgreq->node, &tdc->pending_sg_req); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 639 | sgreq->configured = false; |
Dmitry Osipenko | f261f1c | 2020-02-09 19:33:55 +0300 | [diff] [blame] | 640 | st = handle_continuous_head_request(tdc, to_terminate); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 641 | if (!st) |
| 642 | dma_desc->dma_status = DMA_ERROR; |
| 643 | } |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 644 | } |
| 645 | |
| 646 | static void tegra_dma_tasklet(unsigned long data) |
| 647 | { |
| 648 | struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data; |
Dave Jiang | 370c044 | 2016-07-20 13:13:16 -0700 | [diff] [blame] | 649 | struct dmaengine_desc_callback cb; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 650 | struct tegra_dma_desc *dma_desc; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 651 | unsigned int cb_count; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 652 | unsigned long flags; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 653 | |
| 654 | spin_lock_irqsave(&tdc->lock, flags); |
| 655 | while (!list_empty(&tdc->cb_desc)) { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 656 | dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc), |
| 657 | cb_node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 658 | list_del(&dma_desc->cb_node); |
Dave Jiang | 370c044 | 2016-07-20 13:13:16 -0700 | [diff] [blame] | 659 | dmaengine_desc_get_callback(&dma_desc->txd, &cb); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 660 | cb_count = dma_desc->cb_count; |
| 661 | dma_desc->cb_count = 0; |
Ben Dooks | 95f295f | 2018-11-21 16:13:23 +0000 | [diff] [blame] | 662 | trace_tegra_dma_complete_cb(&tdc->dma_chan, cb_count, |
| 663 | cb.callback); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 664 | spin_unlock_irqrestore(&tdc->lock, flags); |
Dave Jiang | 370c044 | 2016-07-20 13:13:16 -0700 | [diff] [blame] | 665 | while (cb_count--) |
| 666 | dmaengine_desc_callback_invoke(&cb, NULL); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 667 | spin_lock_irqsave(&tdc->lock, flags); |
| 668 | } |
| 669 | spin_unlock_irqrestore(&tdc->lock, flags); |
| 670 | } |
| 671 | |
| 672 | static irqreturn_t tegra_dma_isr(int irq, void *dev_id) |
| 673 | { |
| 674 | struct tegra_dma_channel *tdc = dev_id; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 675 | unsigned long flags; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 676 | u32 status; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 677 | |
| 678 | spin_lock_irqsave(&tdc->lock, flags); |
| 679 | |
Ben Dooks | 95f295f | 2018-11-21 16:13:23 +0000 | [diff] [blame] | 680 | trace_tegra_dma_isr(&tdc->dma_chan, irq); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 681 | status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS); |
| 682 | if (status & TEGRA_APBDMA_STATUS_ISE_EOC) { |
| 683 | tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status); |
| 684 | tdc->isr_handler(tdc, false); |
| 685 | tasklet_schedule(&tdc->tasklet); |
| 686 | spin_unlock_irqrestore(&tdc->lock, flags); |
| 687 | return IRQ_HANDLED; |
| 688 | } |
| 689 | |
| 690 | spin_unlock_irqrestore(&tdc->lock, flags); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 691 | dev_info(tdc2dev(tdc), "Interrupt already served status 0x%08x\n", |
| 692 | status); |
| 693 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 694 | return IRQ_NONE; |
| 695 | } |
| 696 | |
| 697 | static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *txd) |
| 698 | { |
| 699 | struct tegra_dma_desc *dma_desc = txd_to_tegra_dma_desc(txd); |
| 700 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(txd->chan); |
| 701 | unsigned long flags; |
| 702 | dma_cookie_t cookie; |
| 703 | |
| 704 | spin_lock_irqsave(&tdc->lock, flags); |
| 705 | dma_desc->dma_status = DMA_IN_PROGRESS; |
| 706 | cookie = dma_cookie_assign(&dma_desc->txd); |
| 707 | list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req); |
| 708 | spin_unlock_irqrestore(&tdc->lock, flags); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 709 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 710 | return cookie; |
| 711 | } |
| 712 | |
| 713 | static void tegra_dma_issue_pending(struct dma_chan *dc) |
| 714 | { |
| 715 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
| 716 | unsigned long flags; |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 717 | int err; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 718 | |
| 719 | spin_lock_irqsave(&tdc->lock, flags); |
| 720 | if (list_empty(&tdc->pending_sg_req)) { |
| 721 | dev_err(tdc2dev(tdc), "No DMA request\n"); |
| 722 | goto end; |
| 723 | } |
| 724 | if (!tdc->busy) { |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 725 | err = pm_runtime_get_sync(tdc->tdma->dev); |
| 726 | if (err < 0) { |
| 727 | dev_err(tdc2dev(tdc), "Failed to enable DMA\n"); |
| 728 | goto end; |
| 729 | } |
| 730 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 731 | tdc_start_head_req(tdc); |
| 732 | |
| 733 | /* Continuous single mode: Configure next req */ |
| 734 | if (tdc->cyclic) { |
| 735 | /* |
| 736 | * Wait for 1 burst time for configure DMA for |
| 737 | * next transfer. |
| 738 | */ |
| 739 | udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME); |
| 740 | tdc_configure_next_head_desc(tdc); |
| 741 | } |
| 742 | } |
| 743 | end: |
| 744 | spin_unlock_irqrestore(&tdc->lock, flags); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 745 | } |
| 746 | |
Vinod Koul | a7c439a | 2014-12-08 11:30:17 +0530 | [diff] [blame] | 747 | static int tegra_dma_terminate_all(struct dma_chan *dc) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 748 | { |
| 749 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 750 | struct tegra_dma_desc *dma_desc; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 751 | struct tegra_dma_sg_req *sgreq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 752 | unsigned long flags; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 753 | u32 status, wcount; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 754 | bool was_busy; |
| 755 | |
| 756 | spin_lock_irqsave(&tdc->lock, flags); |
| 757 | if (list_empty(&tdc->pending_sg_req)) { |
| 758 | spin_unlock_irqrestore(&tdc->lock, flags); |
Vinod Koul | a7c439a | 2014-12-08 11:30:17 +0530 | [diff] [blame] | 759 | return 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 760 | } |
| 761 | |
| 762 | if (!tdc->busy) |
| 763 | goto skip_dma_stop; |
| 764 | |
| 765 | /* Pause DMA before checking the queue status */ |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 766 | tegra_dma_pause(tdc, true); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 767 | |
| 768 | status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS); |
| 769 | if (status & TEGRA_APBDMA_STATUS_ISE_EOC) { |
| 770 | dev_dbg(tdc2dev(tdc), "%s():handling isr\n", __func__); |
| 771 | tdc->isr_handler(tdc, true); |
| 772 | status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS); |
| 773 | } |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 774 | if (tdc->tdma->chip_data->support_separate_wcount_reg) |
| 775 | wcount = tdc_read(tdc, TEGRA_APBDMA_CHAN_WORD_TRANSFER); |
| 776 | else |
| 777 | wcount = status; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 778 | |
| 779 | was_busy = tdc->busy; |
| 780 | tegra_dma_stop(tdc); |
| 781 | |
| 782 | if (!list_empty(&tdc->pending_sg_req) && was_busy) { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 783 | sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), |
| 784 | node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 785 | sgreq->dma_desc->bytes_transferred += |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 786 | get_current_xferred_count(tdc, sgreq, wcount); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 787 | } |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 788 | tegra_dma_resume(tdc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 789 | |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 790 | pm_runtime_put(tdc->tdma->dev); |
| 791 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 792 | skip_dma_stop: |
| 793 | tegra_dma_abort_all(tdc); |
| 794 | |
| 795 | while (!list_empty(&tdc->cb_desc)) { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 796 | dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc), |
| 797 | cb_node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 798 | list_del(&dma_desc->cb_node); |
| 799 | dma_desc->cb_count = 0; |
| 800 | } |
| 801 | spin_unlock_irqrestore(&tdc->lock, flags); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 802 | |
Vinod Koul | a7c439a | 2014-12-08 11:30:17 +0530 | [diff] [blame] | 803 | return 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 804 | } |
| 805 | |
Dmitry Osipenko | dda5e35 | 2020-02-09 19:33:40 +0300 | [diff] [blame] | 806 | static void tegra_dma_synchronize(struct dma_chan *dc) |
| 807 | { |
| 808 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
| 809 | |
| 810 | tasklet_kill(&tdc->tasklet); |
| 811 | } |
| 812 | |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 813 | static unsigned int tegra_dma_sg_bytes_xferred(struct tegra_dma_channel *tdc, |
| 814 | struct tegra_dma_sg_req *sg_req) |
| 815 | { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 816 | u32 status, wcount = 0; |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 817 | |
| 818 | if (!list_is_first(&sg_req->node, &tdc->pending_sg_req)) |
| 819 | return 0; |
| 820 | |
| 821 | if (tdc->tdma->chip_data->support_separate_wcount_reg) |
| 822 | wcount = tdc_read(tdc, TEGRA_APBDMA_CHAN_WORD_TRANSFER); |
| 823 | |
| 824 | status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS); |
| 825 | |
| 826 | if (!tdc->tdma->chip_data->support_separate_wcount_reg) |
| 827 | wcount = status; |
| 828 | |
| 829 | if (status & TEGRA_APBDMA_STATUS_ISE_EOC) |
| 830 | return sg_req->req_len; |
| 831 | |
| 832 | wcount = get_current_xferred_count(tdc, sg_req, wcount); |
| 833 | |
| 834 | if (!wcount) { |
| 835 | /* |
| 836 | * If wcount wasn't ever polled for this SG before, then |
| 837 | * simply assume that transfer hasn't started yet. |
| 838 | * |
| 839 | * Otherwise it's the end of the transfer. |
| 840 | * |
| 841 | * The alternative would be to poll the status register |
| 842 | * until EOC bit is set or wcount goes UP. That's so |
| 843 | * because EOC bit is getting set only after the last |
| 844 | * burst's completion and counter is less than the actual |
| 845 | * transfer size by 4 bytes. The counter value wraps around |
| 846 | * in a cyclic mode before EOC is set(!), so we can't easily |
| 847 | * distinguish start of transfer from its end. |
| 848 | */ |
| 849 | if (sg_req->words_xferred) |
| 850 | wcount = sg_req->req_len - 4; |
| 851 | |
| 852 | } else if (wcount < sg_req->words_xferred) { |
| 853 | /* |
| 854 | * This case will never happen for a non-cyclic transfer. |
| 855 | * |
| 856 | * For a cyclic transfer, although it is possible for the |
| 857 | * next transfer to have already started (resetting the word |
| 858 | * count), this case should still not happen because we should |
| 859 | * have detected that the EOC bit is set and hence the transfer |
| 860 | * was completed. |
| 861 | */ |
| 862 | WARN_ON_ONCE(1); |
| 863 | |
| 864 | wcount = sg_req->req_len - 4; |
| 865 | } else { |
| 866 | sg_req->words_xferred = wcount; |
| 867 | } |
| 868 | |
| 869 | return wcount; |
| 870 | } |
| 871 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 872 | static enum dma_status tegra_dma_tx_status(struct dma_chan *dc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 873 | dma_cookie_t cookie, |
| 874 | struct dma_tx_state *txstate) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 875 | { |
| 876 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
| 877 | struct tegra_dma_desc *dma_desc; |
| 878 | struct tegra_dma_sg_req *sg_req; |
| 879 | enum dma_status ret; |
| 880 | unsigned long flags; |
Laxman Dewangan | 4a46ba3 | 2012-07-02 13:52:07 +0530 | [diff] [blame] | 881 | unsigned int residual; |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 882 | unsigned int bytes = 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 883 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 884 | ret = dma_cookie_status(dc, cookie, txstate); |
Jon Hunter | d318344 | 2016-06-29 17:08:39 +0100 | [diff] [blame] | 885 | if (ret == DMA_COMPLETE) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 886 | return ret; |
Andy Shevchenko | 0a0aee2 | 2013-05-27 15:14:39 +0300 | [diff] [blame] | 887 | |
| 888 | spin_lock_irqsave(&tdc->lock, flags); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 889 | |
| 890 | /* Check on wait_ack desc status */ |
| 891 | list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) { |
| 892 | if (dma_desc->txd.cookie == cookie) { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 893 | ret = dma_desc->dma_status; |
Jon Hunter | 004f614 | 2016-06-29 17:08:38 +0100 | [diff] [blame] | 894 | goto found; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 895 | } |
| 896 | } |
| 897 | |
| 898 | /* Check in pending list */ |
| 899 | list_for_each_entry(sg_req, &tdc->pending_sg_req, node) { |
| 900 | dma_desc = sg_req->dma_desc; |
| 901 | if (dma_desc->txd.cookie == cookie) { |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 902 | bytes = tegra_dma_sg_bytes_xferred(tdc, sg_req); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 903 | ret = dma_desc->dma_status; |
Jon Hunter | 004f614 | 2016-06-29 17:08:38 +0100 | [diff] [blame] | 904 | goto found; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 905 | } |
| 906 | } |
| 907 | |
Jon Hunter | 019bfcc | 2016-06-29 17:08:37 +0100 | [diff] [blame] | 908 | dev_dbg(tdc2dev(tdc), "cookie %d not found\n", cookie); |
Jon Hunter | 004f614 | 2016-06-29 17:08:38 +0100 | [diff] [blame] | 909 | dma_desc = NULL; |
| 910 | |
| 911 | found: |
Jon Hunter | d318344 | 2016-06-29 17:08:39 +0100 | [diff] [blame] | 912 | if (dma_desc && txstate) { |
Jon Hunter | 004f614 | 2016-06-29 17:08:38 +0100 | [diff] [blame] | 913 | residual = dma_desc->bytes_requested - |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 914 | ((dma_desc->bytes_transferred + bytes) % |
Jon Hunter | 004f614 | 2016-06-29 17:08:38 +0100 | [diff] [blame] | 915 | dma_desc->bytes_requested); |
| 916 | dma_set_residue(txstate, residual); |
| 917 | } |
| 918 | |
Ben Dooks | 95f295f | 2018-11-21 16:13:23 +0000 | [diff] [blame] | 919 | trace_tegra_dma_tx_status(&tdc->dma_chan, cookie, txstate); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 920 | spin_unlock_irqrestore(&tdc->lock, flags); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 921 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 922 | return ret; |
| 923 | } |
| 924 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 925 | static inline unsigned int get_bus_width(struct tegra_dma_channel *tdc, |
| 926 | enum dma_slave_buswidth slave_bw) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 927 | { |
| 928 | switch (slave_bw) { |
| 929 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
| 930 | return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8; |
| 931 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
| 932 | return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16; |
| 933 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
| 934 | return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32; |
| 935 | case DMA_SLAVE_BUSWIDTH_8_BYTES: |
| 936 | return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64; |
| 937 | default: |
| 938 | dev_warn(tdc2dev(tdc), |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 939 | "slave bw is not supported, using 32bits\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 940 | return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32; |
| 941 | } |
| 942 | } |
| 943 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 944 | static inline unsigned int get_burst_size(struct tegra_dma_channel *tdc, |
| 945 | u32 burst_size, |
| 946 | enum dma_slave_buswidth slave_bw, |
| 947 | u32 len) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 948 | { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 949 | unsigned int burst_byte, burst_ahb_width; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 950 | |
| 951 | /* |
| 952 | * burst_size from client is in terms of the bus_width. |
| 953 | * convert them into AHB memory width which is 4 byte. |
| 954 | */ |
| 955 | burst_byte = burst_size * slave_bw; |
| 956 | burst_ahb_width = burst_byte / 4; |
| 957 | |
| 958 | /* If burst size is 0 then calculate the burst size based on length */ |
| 959 | if (!burst_ahb_width) { |
| 960 | if (len & 0xF) |
| 961 | return TEGRA_APBDMA_AHBSEQ_BURST_1; |
| 962 | else if ((len >> 4) & 0x1) |
| 963 | return TEGRA_APBDMA_AHBSEQ_BURST_4; |
| 964 | else |
| 965 | return TEGRA_APBDMA_AHBSEQ_BURST_8; |
| 966 | } |
| 967 | if (burst_ahb_width < 4) |
| 968 | return TEGRA_APBDMA_AHBSEQ_BURST_1; |
| 969 | else if (burst_ahb_width < 8) |
| 970 | return TEGRA_APBDMA_AHBSEQ_BURST_4; |
| 971 | else |
| 972 | return TEGRA_APBDMA_AHBSEQ_BURST_8; |
| 973 | } |
| 974 | |
| 975 | static int get_transfer_param(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 976 | enum dma_transfer_direction direction, |
| 977 | u32 *apb_addr, |
| 978 | u32 *apb_seq, |
| 979 | u32 *csr, |
| 980 | unsigned int *burst_size, |
| 981 | enum dma_slave_buswidth *slave_bw) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 982 | { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 983 | switch (direction) { |
| 984 | case DMA_MEM_TO_DEV: |
| 985 | *apb_addr = tdc->dma_sconfig.dst_addr; |
| 986 | *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.dst_addr_width); |
| 987 | *burst_size = tdc->dma_sconfig.dst_maxburst; |
| 988 | *slave_bw = tdc->dma_sconfig.dst_addr_width; |
| 989 | *csr = TEGRA_APBDMA_CSR_DIR; |
| 990 | return 0; |
| 991 | |
| 992 | case DMA_DEV_TO_MEM: |
| 993 | *apb_addr = tdc->dma_sconfig.src_addr; |
| 994 | *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.src_addr_width); |
| 995 | *burst_size = tdc->dma_sconfig.src_maxburst; |
| 996 | *slave_bw = tdc->dma_sconfig.src_addr_width; |
| 997 | *csr = 0; |
| 998 | return 0; |
| 999 | |
| 1000 | default: |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 1001 | dev_err(tdc2dev(tdc), "DMA direction is not supported\n"); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1002 | break; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1003 | } |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1004 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1005 | return -EINVAL; |
| 1006 | } |
| 1007 | |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1008 | static void tegra_dma_prep_wcount(struct tegra_dma_channel *tdc, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1009 | struct tegra_dma_channel_regs *ch_regs, |
| 1010 | u32 len) |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1011 | { |
| 1012 | u32 len_field = (len - 4) & 0xFFFC; |
| 1013 | |
| 1014 | if (tdc->tdma->chip_data->support_separate_wcount_reg) |
| 1015 | ch_regs->wcount = len_field; |
| 1016 | else |
| 1017 | ch_regs->csr |= len_field; |
| 1018 | } |
| 1019 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1020 | static struct dma_async_tx_descriptor * |
| 1021 | tegra_dma_prep_slave_sg(struct dma_chan *dc, |
| 1022 | struct scatterlist *sgl, |
| 1023 | unsigned int sg_len, |
| 1024 | enum dma_transfer_direction direction, |
| 1025 | unsigned long flags, |
| 1026 | void *context) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1027 | { |
| 1028 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1029 | struct tegra_dma_sg_req *sg_req = NULL; |
| 1030 | u32 csr, ahb_seq, apb_ptr, apb_seq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1031 | enum dma_slave_buswidth slave_bw; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1032 | struct tegra_dma_desc *dma_desc; |
| 1033 | struct list_head req_list; |
| 1034 | struct scatterlist *sg; |
| 1035 | unsigned int burst_size; |
| 1036 | unsigned int i; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1037 | |
| 1038 | if (!tdc->config_init) { |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 1039 | dev_err(tdc2dev(tdc), "DMA channel is not configured\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1040 | return NULL; |
| 1041 | } |
| 1042 | if (sg_len < 1) { |
| 1043 | dev_err(tdc2dev(tdc), "Invalid segment length %d\n", sg_len); |
| 1044 | return NULL; |
| 1045 | } |
| 1046 | |
Jon Hunter | dc1ff4b | 2015-08-06 14:32:32 +0100 | [diff] [blame] | 1047 | if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1048 | &burst_size, &slave_bw) < 0) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1049 | return NULL; |
| 1050 | |
| 1051 | INIT_LIST_HEAD(&req_list); |
| 1052 | |
| 1053 | ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB; |
| 1054 | ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE << |
| 1055 | TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT; |
| 1056 | ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32; |
| 1057 | |
Dmitry Osipenko | f6160f3 | 2017-11-16 20:11:06 +0300 | [diff] [blame] | 1058 | csr |= TEGRA_APBDMA_CSR_ONCE; |
| 1059 | |
| 1060 | if (tdc->slave_id != TEGRA_APBDMA_SLAVE_ID_INVALID) { |
| 1061 | csr |= TEGRA_APBDMA_CSR_FLOW; |
| 1062 | csr |= tdc->slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT; |
| 1063 | } |
| 1064 | |
Dmitry Osipenko | dc16106 | 2019-05-30 00:43:55 +0300 | [diff] [blame] | 1065 | if (flags & DMA_PREP_INTERRUPT) { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1066 | csr |= TEGRA_APBDMA_CSR_IE_EOC; |
Dmitry Osipenko | dc16106 | 2019-05-30 00:43:55 +0300 | [diff] [blame] | 1067 | } else { |
| 1068 | WARN_ON_ONCE(1); |
| 1069 | return NULL; |
| 1070 | } |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1071 | |
| 1072 | apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1; |
| 1073 | |
| 1074 | dma_desc = tegra_dma_desc_get(tdc); |
| 1075 | if (!dma_desc) { |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 1076 | dev_err(tdc2dev(tdc), "DMA descriptors not available\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1077 | return NULL; |
| 1078 | } |
| 1079 | INIT_LIST_HEAD(&dma_desc->tx_list); |
| 1080 | INIT_LIST_HEAD(&dma_desc->cb_node); |
| 1081 | dma_desc->cb_count = 0; |
| 1082 | dma_desc->bytes_requested = 0; |
| 1083 | dma_desc->bytes_transferred = 0; |
| 1084 | dma_desc->dma_status = DMA_IN_PROGRESS; |
| 1085 | |
| 1086 | /* Make transfer requests */ |
| 1087 | for_each_sg(sgl, sg, sg_len, i) { |
| 1088 | u32 len, mem; |
| 1089 | |
Laxman Dewangan | 597c854 | 2012-06-22 20:41:10 +0530 | [diff] [blame] | 1090 | mem = sg_dma_address(sg); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1091 | len = sg_dma_len(sg); |
| 1092 | |
| 1093 | if ((len & 3) || (mem & 3) || |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1094 | len > tdc->tdma->chip_data->max_dma_count) { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1095 | dev_err(tdc2dev(tdc), |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 1096 | "DMA length/memory address is not supported\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1097 | tegra_dma_desc_put(tdc, dma_desc); |
| 1098 | return NULL; |
| 1099 | } |
| 1100 | |
| 1101 | sg_req = tegra_dma_sg_req_get(tdc); |
| 1102 | if (!sg_req) { |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 1103 | dev_err(tdc2dev(tdc), "DMA sg-req not available\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1104 | tegra_dma_desc_put(tdc, dma_desc); |
| 1105 | return NULL; |
| 1106 | } |
| 1107 | |
| 1108 | ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len); |
| 1109 | dma_desc->bytes_requested += len; |
| 1110 | |
| 1111 | sg_req->ch_regs.apb_ptr = apb_ptr; |
| 1112 | sg_req->ch_regs.ahb_ptr = mem; |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1113 | sg_req->ch_regs.csr = csr; |
| 1114 | tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1115 | sg_req->ch_regs.apb_seq = apb_seq; |
| 1116 | sg_req->ch_regs.ahb_seq = ahb_seq; |
| 1117 | sg_req->configured = false; |
| 1118 | sg_req->last_sg = false; |
| 1119 | sg_req->dma_desc = dma_desc; |
| 1120 | sg_req->req_len = len; |
| 1121 | |
| 1122 | list_add_tail(&sg_req->node, &dma_desc->tx_list); |
| 1123 | } |
| 1124 | sg_req->last_sg = true; |
| 1125 | if (flags & DMA_CTRL_ACK) |
| 1126 | dma_desc->txd.flags = DMA_CTRL_ACK; |
| 1127 | |
| 1128 | /* |
| 1129 | * Make sure that mode should not be conflicting with currently |
| 1130 | * configured mode. |
| 1131 | */ |
| 1132 | if (!tdc->isr_handler) { |
| 1133 | tdc->isr_handler = handle_once_dma_done; |
| 1134 | tdc->cyclic = false; |
| 1135 | } else { |
| 1136 | if (tdc->cyclic) { |
| 1137 | dev_err(tdc2dev(tdc), "DMA configured in cyclic mode\n"); |
| 1138 | tegra_dma_desc_put(tdc, dma_desc); |
| 1139 | return NULL; |
| 1140 | } |
| 1141 | } |
| 1142 | |
| 1143 | return &dma_desc->txd; |
| 1144 | } |
| 1145 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1146 | static struct dma_async_tx_descriptor * |
| 1147 | tegra_dma_prep_dma_cyclic(struct dma_chan *dc, dma_addr_t buf_addr, |
| 1148 | size_t buf_len, |
| 1149 | size_t period_len, |
| 1150 | enum dma_transfer_direction direction, |
| 1151 | unsigned long flags) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1152 | { |
| 1153 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
Thierry Reding | 7b0e00d | 2016-06-14 16:18:46 +0200 | [diff] [blame] | 1154 | struct tegra_dma_sg_req *sg_req = NULL; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1155 | u32 csr, ahb_seq, apb_ptr, apb_seq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1156 | enum dma_slave_buswidth slave_bw; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1157 | struct tegra_dma_desc *dma_desc; |
| 1158 | dma_addr_t mem = buf_addr; |
| 1159 | unsigned int burst_size; |
| 1160 | size_t len, remain_len; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1161 | |
| 1162 | if (!buf_len || !period_len) { |
| 1163 | dev_err(tdc2dev(tdc), "Invalid buffer/period len\n"); |
| 1164 | return NULL; |
| 1165 | } |
| 1166 | |
| 1167 | if (!tdc->config_init) { |
| 1168 | dev_err(tdc2dev(tdc), "DMA slave is not configured\n"); |
| 1169 | return NULL; |
| 1170 | } |
| 1171 | |
| 1172 | /* |
| 1173 | * We allow to take more number of requests till DMA is |
| 1174 | * not started. The driver will loop over all requests. |
| 1175 | * Once DMA is started then new requests can be queued only after |
| 1176 | * terminating the DMA. |
| 1177 | */ |
| 1178 | if (tdc->busy) { |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 1179 | dev_err(tdc2dev(tdc), "Request not allowed when DMA running\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1180 | return NULL; |
| 1181 | } |
| 1182 | |
| 1183 | /* |
| 1184 | * We only support cycle transfer when buf_len is multiple of |
| 1185 | * period_len. |
| 1186 | */ |
| 1187 | if (buf_len % period_len) { |
| 1188 | dev_err(tdc2dev(tdc), "buf_len is not multiple of period_len\n"); |
| 1189 | return NULL; |
| 1190 | } |
| 1191 | |
| 1192 | len = period_len; |
| 1193 | if ((len & 3) || (buf_addr & 3) || |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1194 | len > tdc->tdma->chip_data->max_dma_count) { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1195 | dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n"); |
| 1196 | return NULL; |
| 1197 | } |
| 1198 | |
Jon Hunter | dc1ff4b | 2015-08-06 14:32:32 +0100 | [diff] [blame] | 1199 | if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1200 | &burst_size, &slave_bw) < 0) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1201 | return NULL; |
| 1202 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1203 | ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB; |
| 1204 | ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE << |
| 1205 | TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT; |
| 1206 | ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32; |
| 1207 | |
Dmitry Osipenko | f6160f3 | 2017-11-16 20:11:06 +0300 | [diff] [blame] | 1208 | if (tdc->slave_id != TEGRA_APBDMA_SLAVE_ID_INVALID) { |
| 1209 | csr |= TEGRA_APBDMA_CSR_FLOW; |
| 1210 | csr |= tdc->slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT; |
| 1211 | } |
| 1212 | |
Dmitry Osipenko | dc16106 | 2019-05-30 00:43:55 +0300 | [diff] [blame] | 1213 | if (flags & DMA_PREP_INTERRUPT) { |
Laxman Dewangan | b9bb37f | 2013-01-09 15:26:22 +0530 | [diff] [blame] | 1214 | csr |= TEGRA_APBDMA_CSR_IE_EOC; |
Dmitry Osipenko | dc16106 | 2019-05-30 00:43:55 +0300 | [diff] [blame] | 1215 | } else { |
| 1216 | WARN_ON_ONCE(1); |
| 1217 | return NULL; |
| 1218 | } |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1219 | |
| 1220 | apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1; |
| 1221 | |
| 1222 | dma_desc = tegra_dma_desc_get(tdc); |
| 1223 | if (!dma_desc) { |
| 1224 | dev_err(tdc2dev(tdc), "not enough descriptors available\n"); |
| 1225 | return NULL; |
| 1226 | } |
| 1227 | |
| 1228 | INIT_LIST_HEAD(&dma_desc->tx_list); |
| 1229 | INIT_LIST_HEAD(&dma_desc->cb_node); |
| 1230 | dma_desc->cb_count = 0; |
| 1231 | |
| 1232 | dma_desc->bytes_transferred = 0; |
| 1233 | dma_desc->bytes_requested = buf_len; |
| 1234 | remain_len = buf_len; |
| 1235 | |
| 1236 | /* Split transfer equal to period size */ |
| 1237 | while (remain_len) { |
| 1238 | sg_req = tegra_dma_sg_req_get(tdc); |
| 1239 | if (!sg_req) { |
Ben Dooks | 547b311 | 2018-11-21 16:13:21 +0000 | [diff] [blame] | 1240 | dev_err(tdc2dev(tdc), "DMA sg-req not available\n"); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1241 | tegra_dma_desc_put(tdc, dma_desc); |
| 1242 | return NULL; |
| 1243 | } |
| 1244 | |
| 1245 | ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len); |
| 1246 | sg_req->ch_regs.apb_ptr = apb_ptr; |
| 1247 | sg_req->ch_regs.ahb_ptr = mem; |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1248 | sg_req->ch_regs.csr = csr; |
| 1249 | tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1250 | sg_req->ch_regs.apb_seq = apb_seq; |
| 1251 | sg_req->ch_regs.ahb_seq = ahb_seq; |
| 1252 | sg_req->configured = false; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1253 | sg_req->last_sg = false; |
| 1254 | sg_req->dma_desc = dma_desc; |
| 1255 | sg_req->req_len = len; |
| 1256 | |
| 1257 | list_add_tail(&sg_req->node, &dma_desc->tx_list); |
| 1258 | remain_len -= len; |
| 1259 | mem += len; |
| 1260 | } |
| 1261 | sg_req->last_sg = true; |
Laxman Dewangan | b9bb37f | 2013-01-09 15:26:22 +0530 | [diff] [blame] | 1262 | if (flags & DMA_CTRL_ACK) |
| 1263 | dma_desc->txd.flags = DMA_CTRL_ACK; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1264 | |
| 1265 | /* |
| 1266 | * Make sure that mode should not be conflicting with currently |
| 1267 | * configured mode. |
| 1268 | */ |
| 1269 | if (!tdc->isr_handler) { |
| 1270 | tdc->isr_handler = handle_cont_sngl_cycle_dma_done; |
| 1271 | tdc->cyclic = true; |
| 1272 | } else { |
| 1273 | if (!tdc->cyclic) { |
| 1274 | dev_err(tdc2dev(tdc), "DMA configuration conflict\n"); |
| 1275 | tegra_dma_desc_put(tdc, dma_desc); |
| 1276 | return NULL; |
| 1277 | } |
| 1278 | } |
| 1279 | |
| 1280 | return &dma_desc->txd; |
| 1281 | } |
| 1282 | |
| 1283 | static int tegra_dma_alloc_chan_resources(struct dma_chan *dc) |
| 1284 | { |
| 1285 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
| 1286 | |
| 1287 | dma_cookie_init(&tdc->dma_chan); |
Jon Hunter | edd3bdb | 2015-11-13 16:39:38 +0000 | [diff] [blame] | 1288 | |
Jon Hunter | edd3bdb | 2015-11-13 16:39:38 +0000 | [diff] [blame] | 1289 | return 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1290 | } |
| 1291 | |
| 1292 | static void tegra_dma_free_chan_resources(struct dma_chan *dc) |
| 1293 | { |
| 1294 | struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1295 | struct tegra_dma_desc *dma_desc; |
| 1296 | struct tegra_dma_sg_req *sg_req; |
| 1297 | struct list_head dma_desc_list; |
| 1298 | struct list_head sg_req_list; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1299 | |
| 1300 | INIT_LIST_HEAD(&dma_desc_list); |
| 1301 | INIT_LIST_HEAD(&sg_req_list); |
| 1302 | |
| 1303 | dev_dbg(tdc2dev(tdc), "Freeing channel %d\n", tdc->id); |
| 1304 | |
Dmitry Osipenko | 8e84172 | 2020-02-09 19:33:41 +0300 | [diff] [blame] | 1305 | tegra_dma_terminate_all(dc); |
Dmitry Osipenko | 41ffc42 | 2020-02-09 19:33:42 +0300 | [diff] [blame] | 1306 | tasklet_kill(&tdc->tasklet); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1307 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1308 | list_splice_init(&tdc->pending_sg_req, &sg_req_list); |
| 1309 | list_splice_init(&tdc->free_sg_req, &sg_req_list); |
| 1310 | list_splice_init(&tdc->free_dma_desc, &dma_desc_list); |
| 1311 | INIT_LIST_HEAD(&tdc->cb_desc); |
| 1312 | tdc->config_init = false; |
Dmitry Osipenko | 7bdc1e2 | 2013-05-11 20:30:53 +0400 | [diff] [blame] | 1313 | tdc->isr_handler = NULL; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1314 | |
| 1315 | while (!list_empty(&dma_desc_list)) { |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1316 | dma_desc = list_first_entry(&dma_desc_list, typeof(*dma_desc), |
| 1317 | node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1318 | list_del(&dma_desc->node); |
| 1319 | kfree(dma_desc); |
| 1320 | } |
| 1321 | |
| 1322 | while (!list_empty(&sg_req_list)) { |
| 1323 | sg_req = list_first_entry(&sg_req_list, typeof(*sg_req), node); |
| 1324 | list_del(&sg_req->node); |
| 1325 | kfree(sg_req); |
| 1326 | } |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 1327 | |
Shardar Shariff Md | 00ef449 | 2016-04-23 15:06:00 +0530 | [diff] [blame] | 1328 | tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID; |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 1329 | } |
| 1330 | |
| 1331 | static struct dma_chan *tegra_dma_of_xlate(struct of_phandle_args *dma_spec, |
| 1332 | struct of_dma *ofdma) |
| 1333 | { |
| 1334 | struct tegra_dma *tdma = ofdma->of_dma_data; |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 1335 | struct tegra_dma_channel *tdc; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1336 | struct dma_chan *chan; |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 1337 | |
Shardar Shariff Md | 00ef449 | 2016-04-23 15:06:00 +0530 | [diff] [blame] | 1338 | if (dma_spec->args[0] > TEGRA_APBDMA_CSR_REQ_SEL_MASK) { |
| 1339 | dev_err(tdma->dev, "Invalid slave id: %d\n", dma_spec->args[0]); |
| 1340 | return NULL; |
| 1341 | } |
| 1342 | |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 1343 | chan = dma_get_any_slave_channel(&tdma->dma_dev); |
| 1344 | if (!chan) |
| 1345 | return NULL; |
| 1346 | |
| 1347 | tdc = to_tegra_dma_chan(chan); |
| 1348 | tdc->slave_id = dma_spec->args[0]; |
| 1349 | |
| 1350 | return chan; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1351 | } |
| 1352 | |
| 1353 | /* Tegra20 specific DMA controller information */ |
Laxman Dewangan | 75f2163 | 2012-08-29 10:31:18 +0200 | [diff] [blame] | 1354 | static const struct tegra_dma_chip_data tegra20_dma_chip_data = { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1355 | .nr_channels = 16, |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1356 | .channel_reg_size = 0x20, |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1357 | .max_dma_count = 1024UL * 64, |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 1358 | .support_channel_pause = false, |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1359 | .support_separate_wcount_reg = false, |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1360 | }; |
| 1361 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1362 | /* Tegra30 specific DMA controller information */ |
Laxman Dewangan | 75f2163 | 2012-08-29 10:31:18 +0200 | [diff] [blame] | 1363 | static const struct tegra_dma_chip_data tegra30_dma_chip_data = { |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1364 | .nr_channels = 32, |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1365 | .channel_reg_size = 0x20, |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1366 | .max_dma_count = 1024UL * 64, |
Laxman Dewangan | 1b14090 | 2013-01-06 21:52:02 +0530 | [diff] [blame] | 1367 | .support_channel_pause = false, |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1368 | .support_separate_wcount_reg = false, |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1369 | }; |
| 1370 | |
Laxman Dewangan | 5ea7caf | 2013-01-06 21:52:03 +0530 | [diff] [blame] | 1371 | /* Tegra114 specific DMA controller information */ |
| 1372 | static const struct tegra_dma_chip_data tegra114_dma_chip_data = { |
| 1373 | .nr_channels = 32, |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1374 | .channel_reg_size = 0x20, |
Laxman Dewangan | 5ea7caf | 2013-01-06 21:52:03 +0530 | [diff] [blame] | 1375 | .max_dma_count = 1024UL * 64, |
| 1376 | .support_channel_pause = true, |
Laxman Dewangan | 911dacc | 2014-01-06 11:16:45 -0700 | [diff] [blame] | 1377 | .support_separate_wcount_reg = false, |
| 1378 | }; |
| 1379 | |
| 1380 | /* Tegra148 specific DMA controller information */ |
| 1381 | static const struct tegra_dma_chip_data tegra148_dma_chip_data = { |
| 1382 | .nr_channels = 32, |
| 1383 | .channel_reg_size = 0x40, |
| 1384 | .max_dma_count = 1024UL * 64, |
| 1385 | .support_channel_pause = true, |
| 1386 | .support_separate_wcount_reg = true, |
Laxman Dewangan | 5ea7caf | 2013-01-06 21:52:03 +0530 | [diff] [blame] | 1387 | }; |
| 1388 | |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1389 | static int tegra_dma_init_hw(struct tegra_dma *tdma) |
| 1390 | { |
| 1391 | int err; |
| 1392 | |
| 1393 | err = reset_control_assert(tdma->rst); |
| 1394 | if (err) { |
| 1395 | dev_err(tdma->dev, "failed to assert reset: %d\n", err); |
| 1396 | return err; |
| 1397 | } |
| 1398 | |
| 1399 | err = clk_enable(tdma->dma_clk); |
| 1400 | if (err) { |
| 1401 | dev_err(tdma->dev, "failed to enable clk: %d\n", err); |
| 1402 | return err; |
| 1403 | } |
| 1404 | |
| 1405 | /* reset DMA controller */ |
| 1406 | udelay(2); |
| 1407 | reset_control_deassert(tdma->rst); |
| 1408 | |
| 1409 | /* enable global DMA registers */ |
| 1410 | tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE); |
| 1411 | tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0); |
| 1412 | tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFF); |
| 1413 | |
| 1414 | clk_disable(tdma->dma_clk); |
| 1415 | |
| 1416 | return 0; |
| 1417 | } |
| 1418 | |
Bill Pemberton | 463a1f8 | 2012-11-19 13:22:55 -0500 | [diff] [blame] | 1419 | static int tegra_dma_probe(struct platform_device *pdev) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1420 | { |
Laxman Dewangan | 333f16e | 2016-03-01 18:54:40 +0530 | [diff] [blame] | 1421 | const struct tegra_dma_chip_data *cdata; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1422 | struct tegra_dma *tdma; |
| 1423 | unsigned int i; |
| 1424 | size_t size; |
| 1425 | int ret; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1426 | |
Laxman Dewangan | 333f16e | 2016-03-01 18:54:40 +0530 | [diff] [blame] | 1427 | cdata = of_device_get_match_data(&pdev->dev); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1428 | size = struct_size(tdma, channels, cdata->nr_channels); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1429 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1430 | tdma = devm_kzalloc(&pdev->dev, size, GFP_KERNEL); |
Peter Griffin | aef94fe | 2016-06-07 18:38:41 +0100 | [diff] [blame] | 1431 | if (!tdma) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1432 | return -ENOMEM; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1433 | |
| 1434 | tdma->dev = &pdev->dev; |
| 1435 | tdma->chip_data = cdata; |
| 1436 | platform_set_drvdata(pdev, tdma); |
| 1437 | |
Dmitry Osipenko | c55c745e | 2020-02-09 19:33:43 +0300 | [diff] [blame] | 1438 | tdma->base_addr = devm_platform_ioremap_resource(pdev, 0); |
Thierry Reding | 7331205 | 2013-01-21 11:09:00 +0100 | [diff] [blame] | 1439 | if (IS_ERR(tdma->base_addr)) |
| 1440 | return PTR_ERR(tdma->base_addr); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1441 | |
| 1442 | tdma->dma_clk = devm_clk_get(&pdev->dev, NULL); |
| 1443 | if (IS_ERR(tdma->dma_clk)) { |
| 1444 | dev_err(&pdev->dev, "Error: Missing controller clock\n"); |
| 1445 | return PTR_ERR(tdma->dma_clk); |
| 1446 | } |
| 1447 | |
Stephen Warren | 9aa433d | 2013-11-06 16:35:34 -0700 | [diff] [blame] | 1448 | tdma->rst = devm_reset_control_get(&pdev->dev, "dma"); |
| 1449 | if (IS_ERR(tdma->rst)) { |
| 1450 | dev_err(&pdev->dev, "Error: Missing reset\n"); |
| 1451 | return PTR_ERR(tdma->rst); |
| 1452 | } |
| 1453 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1454 | spin_lock_init(&tdma->global_lock); |
| 1455 | |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 1456 | ret = clk_prepare(tdma->dma_clk); |
| 1457 | if (ret) |
| 1458 | return ret; |
| 1459 | |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1460 | ret = tegra_dma_init_hw(tdma); |
| 1461 | if (ret) |
| 1462 | goto err_clk_unprepare; |
| 1463 | |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 1464 | pm_runtime_irq_safe(&pdev->dev); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1465 | pm_runtime_enable(&pdev->dev); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1466 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1467 | INIT_LIST_HEAD(&tdma->dma_dev.channels); |
| 1468 | for (i = 0; i < cdata->nr_channels; i++) { |
| 1469 | struct tegra_dma_channel *tdc = &tdma->channels[i]; |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1470 | int irq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1471 | |
Jon Hunter | 13a3328 | 2015-08-06 14:32:31 +0100 | [diff] [blame] | 1472 | tdc->chan_addr = tdma->base_addr + |
| 1473 | TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET + |
| 1474 | (i * cdata->channel_reg_size); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1475 | |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1476 | irq = platform_get_irq(pdev, i); |
| 1477 | if (irq < 0) { |
| 1478 | ret = irq; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1479 | dev_err(&pdev->dev, "No irq resource for chan %d\n", i); |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1480 | goto err_pm_disable; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1481 | } |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1482 | |
Laxman Dewangan | d0fc905 | 2012-10-03 22:48:07 +0530 | [diff] [blame] | 1483 | snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i); |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1484 | ret = devm_request_irq(&pdev->dev, irq, tegra_dma_isr, 0, |
| 1485 | tdc->name, tdc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1486 | if (ret) { |
| 1487 | dev_err(&pdev->dev, |
| 1488 | "request_irq failed with err %d channel %d\n", |
Dmitry Osipenko | ac7ae75 | 2013-05-11 20:30:52 +0400 | [diff] [blame] | 1489 | ret, i); |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1490 | goto err_pm_disable; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1491 | } |
| 1492 | |
| 1493 | tdc->dma_chan.device = &tdma->dma_dev; |
| 1494 | dma_cookie_init(&tdc->dma_chan); |
| 1495 | list_add_tail(&tdc->dma_chan.device_node, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1496 | &tdma->dma_dev.channels); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1497 | tdc->tdma = tdma; |
| 1498 | tdc->id = i; |
Shardar Shariff Md | 00ef449 | 2016-04-23 15:06:00 +0530 | [diff] [blame] | 1499 | tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1500 | |
| 1501 | tasklet_init(&tdc->tasklet, tegra_dma_tasklet, |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1502 | (unsigned long)tdc); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1503 | spin_lock_init(&tdc->lock); |
| 1504 | |
| 1505 | INIT_LIST_HEAD(&tdc->pending_sg_req); |
| 1506 | INIT_LIST_HEAD(&tdc->free_sg_req); |
| 1507 | INIT_LIST_HEAD(&tdc->free_dma_desc); |
| 1508 | INIT_LIST_HEAD(&tdc->cb_desc); |
| 1509 | } |
| 1510 | |
| 1511 | dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask); |
| 1512 | dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask); |
Laxman Dewangan | 46fb3f8 | 2012-06-22 17:12:43 +0530 | [diff] [blame] | 1513 | dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask); |
| 1514 | |
Jon Hunter | 23a1ec3 | 2015-08-06 14:32:33 +0100 | [diff] [blame] | 1515 | tdma->global_pause_count = 0; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1516 | tdma->dma_dev.dev = &pdev->dev; |
| 1517 | tdma->dma_dev.device_alloc_chan_resources = |
| 1518 | tegra_dma_alloc_chan_resources; |
| 1519 | tdma->dma_dev.device_free_chan_resources = |
| 1520 | tegra_dma_free_chan_resources; |
| 1521 | tdma->dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg; |
| 1522 | tdma->dma_dev.device_prep_dma_cyclic = tegra_dma_prep_dma_cyclic; |
Paul Walmsley | 891653a | 2015-01-06 06:44:56 +0000 | [diff] [blame] | 1523 | tdma->dma_dev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | |
| 1524 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | |
| 1525 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | |
| 1526 | BIT(DMA_SLAVE_BUSWIDTH_8_BYTES); |
| 1527 | tdma->dma_dev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | |
| 1528 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | |
| 1529 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) | |
| 1530 | BIT(DMA_SLAVE_BUSWIDTH_8_BYTES); |
| 1531 | tdma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV); |
Dmitry Osipenko | 156a599 | 2019-07-05 18:05:19 +0300 | [diff] [blame] | 1532 | tdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; |
Maxime Ripard | 662f1ac | 2014-11-17 14:42:37 +0100 | [diff] [blame] | 1533 | tdma->dma_dev.device_config = tegra_dma_slave_config; |
| 1534 | tdma->dma_dev.device_terminate_all = tegra_dma_terminate_all; |
Dmitry Osipenko | dda5e35 | 2020-02-09 19:33:40 +0300 | [diff] [blame] | 1535 | tdma->dma_dev.device_synchronize = tegra_dma_synchronize; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1536 | tdma->dma_dev.device_tx_status = tegra_dma_tx_status; |
| 1537 | tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending; |
| 1538 | |
| 1539 | ret = dma_async_device_register(&tdma->dma_dev); |
| 1540 | if (ret < 0) { |
| 1541 | dev_err(&pdev->dev, |
| 1542 | "Tegra20 APB DMA driver registration failed %d\n", ret); |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1543 | goto err_pm_disable; |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1544 | } |
| 1545 | |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 1546 | ret = of_dma_controller_register(pdev->dev.of_node, |
| 1547 | tegra_dma_of_xlate, tdma); |
| 1548 | if (ret < 0) { |
| 1549 | dev_err(&pdev->dev, |
| 1550 | "Tegra20 APB DMA OF registration failed %d\n", ret); |
| 1551 | goto err_unregister_dma_dev; |
| 1552 | } |
| 1553 | |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1554 | dev_info(&pdev->dev, "Tegra20 APB DMA driver registered %u channels\n", |
| 1555 | cdata->nr_channels); |
| 1556 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1557 | return 0; |
| 1558 | |
Stephen Warren | 996556c | 2013-11-11 13:09:35 -0700 | [diff] [blame] | 1559 | err_unregister_dma_dev: |
| 1560 | dma_async_device_unregister(&tdma->dma_dev); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1561 | |
Dmitry Osipenko | 2cd3d13 | 2020-02-09 19:33:44 +0300 | [diff] [blame] | 1562 | err_pm_disable: |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1563 | pm_runtime_disable(&pdev->dev); |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1564 | |
| 1565 | err_clk_unprepare: |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 1566 | clk_unprepare(tdma->dma_clk); |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1567 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1568 | return ret; |
| 1569 | } |
| 1570 | |
Greg Kroah-Hartman | 4bf27b8 | 2012-12-21 15:09:59 -0800 | [diff] [blame] | 1571 | static int tegra_dma_remove(struct platform_device *pdev) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1572 | { |
| 1573 | struct tegra_dma *tdma = platform_get_drvdata(pdev); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1574 | |
Dmitry Osipenko | 16e2b3e | 2020-02-09 19:33:51 +0300 | [diff] [blame] | 1575 | of_dma_controller_free(pdev->dev.of_node); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1576 | dma_async_device_unregister(&tdma->dma_dev); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1577 | pm_runtime_disable(&pdev->dev); |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 1578 | clk_unprepare(tdma->dma_clk); |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1579 | |
| 1580 | return 0; |
| 1581 | } |
| 1582 | |
| 1583 | static int tegra_dma_runtime_suspend(struct device *dev) |
| 1584 | { |
Jon Hunter | 286a644 | 2015-11-13 16:39:39 +0000 | [diff] [blame] | 1585 | struct tegra_dma *tdma = dev_get_drvdata(dev); |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1586 | |
Dmitry Osipenko | 84a3f37 | 2020-02-09 19:33:49 +0300 | [diff] [blame] | 1587 | clk_disable(tdma->dma_clk); |
Jon Hunter | 65a5c3d | 2017-06-06 13:49:29 +0100 | [diff] [blame] | 1588 | |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1589 | return 0; |
| 1590 | } |
| 1591 | |
Jon Hunter | 65a5c3d | 2017-06-06 13:49:29 +0100 | [diff] [blame] | 1592 | static int tegra_dma_runtime_resume(struct device *dev) |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1593 | { |
| 1594 | struct tegra_dma *tdma = dev_get_drvdata(dev); |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1595 | |
| 1596 | return clk_enable(tdma->dma_clk); |
| 1597 | } |
| 1598 | |
| 1599 | static int __maybe_unused tegra_dma_dev_suspend(struct device *dev) |
| 1600 | { |
| 1601 | struct tegra_dma *tdma = dev_get_drvdata(dev); |
| 1602 | unsigned long flags; |
Dmitry Osipenko | 3964293 | 2020-02-09 19:33:45 +0300 | [diff] [blame] | 1603 | unsigned int i; |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1604 | bool busy; |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1605 | |
| 1606 | for (i = 0; i < tdma->chip_data->nr_channels; i++) { |
| 1607 | struct tegra_dma_channel *tdc = &tdma->channels[i]; |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1608 | |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1609 | tasklet_kill(&tdc->tasklet); |
Jon Hunter | 4aad5be | 2015-11-13 16:39:41 +0000 | [diff] [blame] | 1610 | |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1611 | spin_lock_irqsave(&tdc->lock, flags); |
| 1612 | busy = tdc->busy; |
| 1613 | spin_unlock_irqrestore(&tdc->lock, flags); |
| 1614 | |
| 1615 | if (busy) { |
| 1616 | dev_err(tdma->dev, "channel %u busy\n", i); |
| 1617 | return -EBUSY; |
| 1618 | } |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1619 | } |
| 1620 | |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1621 | return pm_runtime_force_suspend(dev); |
| 1622 | } |
| 1623 | |
| 1624 | static int __maybe_unused tegra_dma_dev_resume(struct device *dev) |
| 1625 | { |
| 1626 | struct tegra_dma *tdma = dev_get_drvdata(dev); |
| 1627 | int err; |
| 1628 | |
| 1629 | err = tegra_dma_init_hw(tdma); |
| 1630 | if (err) |
| 1631 | return err; |
| 1632 | |
| 1633 | return pm_runtime_force_resume(dev); |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1634 | } |
Laxman Dewangan | 3065c19 | 2013-04-24 15:24:27 +0530 | [diff] [blame] | 1635 | |
Greg Kroah-Hartman | 4bf27b8 | 2012-12-21 15:09:59 -0800 | [diff] [blame] | 1636 | static const struct dev_pm_ops tegra_dma_dev_pm_ops = { |
Jon Hunter | edd3bdb | 2015-11-13 16:39:38 +0000 | [diff] [blame] | 1637 | SET_RUNTIME_PM_OPS(tegra_dma_runtime_suspend, tegra_dma_runtime_resume, |
| 1638 | NULL) |
Dmitry Osipenko | dcb394b | 2020-02-09 19:33:50 +0300 | [diff] [blame] | 1639 | SET_SYSTEM_SLEEP_PM_OPS(tegra_dma_dev_suspend, tegra_dma_dev_resume) |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1640 | }; |
| 1641 | |
Laxman Dewangan | 242637b | 2016-03-04 15:55:11 +0530 | [diff] [blame] | 1642 | static const struct of_device_id tegra_dma_of_match[] = { |
| 1643 | { |
| 1644 | .compatible = "nvidia,tegra148-apbdma", |
| 1645 | .data = &tegra148_dma_chip_data, |
| 1646 | }, { |
| 1647 | .compatible = "nvidia,tegra114-apbdma", |
| 1648 | .data = &tegra114_dma_chip_data, |
| 1649 | }, { |
| 1650 | .compatible = "nvidia,tegra30-apbdma", |
| 1651 | .data = &tegra30_dma_chip_data, |
| 1652 | }, { |
| 1653 | .compatible = "nvidia,tegra20-apbdma", |
| 1654 | .data = &tegra20_dma_chip_data, |
| 1655 | }, { |
| 1656 | }, |
| 1657 | }; |
| 1658 | MODULE_DEVICE_TABLE(of, tegra_dma_of_match); |
| 1659 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1660 | static struct platform_driver tegra_dmac_driver = { |
| 1661 | .driver = { |
Laxman Dewangan | cd9092c | 2012-07-02 13:52:08 +0530 | [diff] [blame] | 1662 | .name = "tegra-apbdma", |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1663 | .pm = &tegra_dma_dev_pm_ops, |
Stephen Warren | dc7badb | 2013-03-11 16:30:26 -0600 | [diff] [blame] | 1664 | .of_match_table = tegra_dma_of_match, |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1665 | }, |
| 1666 | .probe = tegra_dma_probe, |
Bill Pemberton | a7d6e3e | 2012-11-19 13:20:04 -0500 | [diff] [blame] | 1667 | .remove = tegra_dma_remove, |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1668 | }; |
| 1669 | |
| 1670 | module_platform_driver(tegra_dmac_driver); |
| 1671 | |
Laxman Dewangan | ec8a158 | 2012-06-06 10:55:27 +0530 | [diff] [blame] | 1672 | MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver"); |
| 1673 | MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>"); |
| 1674 | MODULE_LICENSE("GPL v2"); |