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