blob: f1ff836abeaff7599648720ecad02e408e5be731 [file] [log] [blame]
Thomas Gleixner9952f692019-05-28 10:10:04 -07001// SPDX-License-Identifier: GPL-2.0-only
Laxman Dewanganec8a1582012-06-06 10:55:27 +05302/*
3 * DMA driver for Nvidia's Tegra20 APB DMA controller.
4 *
Stephen Warren996556c2013-11-11 13:09:35 -07005 * Copyright (c) 2012-2013, NVIDIA CORPORATION. All rights reserved.
Laxman Dewanganec8a1582012-06-06 10:55:27 +05306 */
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 Reding73312052013-01-21 11:09:00 +010013#include <linux/err.h>
Laxman Dewanganec8a1582012-06-06 10:55:27 +053014#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 Warren996556c2013-11-11 13:09:35 -070021#include <linux/of_dma.h>
Laxman Dewanganec8a1582012-06-06 10:55:27 +053022#include <linux/platform_device.h>
Laxman Dewangan3065c192013-04-24 15:24:27 +053023#include <linux/pm.h>
Laxman Dewanganec8a1582012-06-06 10:55:27 +053024#include <linux/pm_runtime.h>
Stephen Warren9aa433d2013-11-06 16:35:34 -070025#include <linux/reset.h>
Laxman Dewanganec8a1582012-06-06 10:55:27 +053026#include <linux/slab.h>
27
Laxman Dewanganec8a1582012-06-06 10:55:27 +053028#include "dmaengine.h"
29
Ben Dooks95f295f2018-11-21 16:13:23 +000030#define CREATE_TRACE_POINTS
31#include <trace/events/tegra_apb_dma.h>
32
Laxman Dewanganec8a1582012-06-06 10:55:27 +053033#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 Md00ef4492016-04-23 15:06:00 +053049#define TEGRA_APBDMA_CSR_REQ_SEL_MASK 0x1F
Laxman Dewanganec8a1582012-06-06 10:55:27 +053050#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 Dewangan1b140902013-01-06 21:52:02 +053061#define TEGRA_APBDMA_CHAN_CSRE 0x00C
Dmitry Osipenko39642932020-02-09 19:33:45 +030062#define TEGRA_APBDMA_CHAN_CSRE_PAUSE BIT(31)
Laxman Dewangan1b140902013-01-06 21:52:02 +053063
Laxman Dewanganec8a1582012-06-06 10:55:27 +053064/* 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 Dewangan911dacc2014-01-06 11:16:45 -070096/* Tegra148 specific registers */
97#define TEGRA_APBDMA_CHAN_WCOUNT 0x20
98
99#define TEGRA_APBDMA_CHAN_WORD_TRANSFER 0x24
100
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530101/*
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 Md00ef4492016-04-23 15:06:00 +0530110#define TEGRA_APBDMA_SLAVE_ID_INVALID (TEGRA_APBDMA_CSR_REQ_SEL_MASK + 1)
111
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530112struct 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 Dewangan911dacc2014-01-06 11:16:45 -0700117 * @channel_reg_size: Channel register size/stride.
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530118 * @max_dma_count: Maximum DMA transfer count supported by DMA controller.
Laxman Dewangan1b140902013-01-06 21:52:02 +0530119 * @support_channel_pause: Support channel wise pause of dma.
Laxman Dewangan911dacc2014-01-06 11:16:45 -0700120 * @support_separate_wcount_reg: Support separate word count register.
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530121 */
122struct tegra_dma_chip_data {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300123 unsigned int nr_channels;
124 unsigned int channel_reg_size;
125 unsigned int max_dma_count;
Laxman Dewangan1b140902013-01-06 21:52:02 +0530126 bool support_channel_pause;
Laxman Dewangan911dacc2014-01-06 11:16:45 -0700127 bool support_separate_wcount_reg;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530128};
129
130/* DMA channel registers */
131struct tegra_dma_channel_regs {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300132 u32 csr;
133 u32 ahb_ptr;
134 u32 apb_ptr;
135 u32 ahb_seq;
136 u32 apb_seq;
137 u32 wcount;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530138};
139
140/*
Ben Dooks547b3112018-11-21 16:13:21 +0000141 * tegra_dma_sg_req: DMA request details to configure hardware. This
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530142 * 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 */
148struct tegra_dma_sg_req {
149 struct tegra_dma_channel_regs ch_regs;
Ben Dooks216a1d72018-11-21 16:13:20 +0000150 unsigned int req_len;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530151 bool configured;
152 bool last_sg;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530153 struct list_head node;
154 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300155 unsigned int words_xferred;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530156};
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 */
163struct tegra_dma_desc {
164 struct dma_async_tx_descriptor txd;
Ben Dooks216a1d72018-11-21 16:13:20 +0000165 unsigned int bytes_requested;
166 unsigned int bytes_transferred;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530167 enum dma_status dma_status;
168 struct list_head node;
169 struct list_head tx_list;
170 struct list_head cb_node;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300171 unsigned int cb_count;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530172};
173
174struct tegra_dma_channel;
175
176typedef void (*dma_isr_handler)(struct tegra_dma_channel *tdc,
177 bool to_terminate);
178
179/* tegra_dma_channel: Channel specific information */
180struct tegra_dma_channel {
181 struct dma_chan dma_chan;
Ben Dooks65c383c2018-11-21 16:13:22 +0000182 char name[12];
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530183 bool config_init;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300184 unsigned int id;
Jon Hunter13a33282015-08-06 14:32:31 +0100185 void __iomem *chan_addr;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530186 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 Dewanganec8a1582012-06-06 10:55:27 +0530200
201 /* Channel-slave specific configuration */
Stephen Warren996556c2013-11-11 13:09:35 -0700202 unsigned int slave_id;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530203 struct dma_slave_config dma_sconfig;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300204 struct tegra_dma_channel_regs channel_reg;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530205};
206
207/* tegra_dma: Tegra DMA specific information */
208struct tegra_dma {
209 struct dma_device dma_dev;
210 struct device *dev;
211 struct clk *dma_clk;
Stephen Warren9aa433d2013-11-06 16:35:34 -0700212 struct reset_control *rst;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530213 spinlock_t global_lock;
214 void __iomem *base_addr;
Laxman Dewangan83a1ef22012-08-29 10:23:07 +0200215 const struct tegra_dma_chip_data *chip_data;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530216
Jon Hunter23a1ec32015-08-06 14:32:33 +0100217 /*
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 Dewanganec8a1582012-06-06 10:55:27 +0530224 /* Last member of the structure */
225 struct tegra_dma_channel channels[0];
226};
227
228static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
229{
230 writel(val, tdma->base_addr + reg);
231}
232
233static inline u32 tdma_read(struct tegra_dma *tdma, u32 reg)
234{
235 return readl(tdma->base_addr + reg);
236}
237
238static inline void tdc_write(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300239 u32 reg, u32 val)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530240{
Jon Hunter13a33282015-08-06 14:32:31 +0100241 writel(val, tdc->chan_addr + reg);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530242}
243
244static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg)
245{
Jon Hunter13a33282015-08-06 14:32:31 +0100246 return readl(tdc->chan_addr + reg);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530247}
248
249static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc)
250{
251 return container_of(dc, struct tegra_dma_channel, dma_chan);
252}
253
Dmitry Osipenko39642932020-02-09 19:33:45 +0300254static inline struct tegra_dma_desc *
255txd_to_tegra_dma_desc(struct dma_async_tx_descriptor *td)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530256{
257 return container_of(td, struct tegra_dma_desc, txd);
258}
259
260static inline struct device *tdc2dev(struct tegra_dma_channel *tdc)
261{
262 return &tdc->dma_chan.dev->device;
263}
264
265static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);
266static int tegra_dma_runtime_suspend(struct device *dev);
267static int tegra_dma_runtime_resume(struct device *dev);
268
269/* Get DMA desc from free list, if not there then allocate it. */
Dmitry Osipenko39642932020-02-09 19:33:45 +0300270static struct tegra_dma_desc *tegra_dma_desc_get(struct tegra_dma_channel *tdc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530271{
272 struct tegra_dma_desc *dma_desc;
273 unsigned long flags;
274
275 spin_lock_irqsave(&tdc->lock, flags);
276
277 /* Do not allocate if desc are waiting for ack */
278 list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
279 if (async_tx_test_ack(&dma_desc->txd)) {
280 list_del(&dma_desc->node);
281 spin_unlock_irqrestore(&tdc->lock, flags);
Laxman Dewanganb9bb37f2013-01-09 15:26:22 +0530282 dma_desc->txd.flags = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530283 return dma_desc;
284 }
285 }
286
287 spin_unlock_irqrestore(&tdc->lock, flags);
288
289 /* Allocate DMA desc */
Jon Hunter8fe97392015-11-13 16:39:42 +0000290 dma_desc = kzalloc(sizeof(*dma_desc), GFP_NOWAIT);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100291 if (!dma_desc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530292 return NULL;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530293
294 dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan);
295 dma_desc->txd.tx_submit = tegra_dma_tx_submit;
296 dma_desc->txd.flags = 0;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300297
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530298 return dma_desc;
299}
300
301static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300302 struct tegra_dma_desc *dma_desc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530303{
304 unsigned long flags;
305
306 spin_lock_irqsave(&tdc->lock, flags);
307 if (!list_empty(&dma_desc->tx_list))
308 list_splice_init(&dma_desc->tx_list, &tdc->free_sg_req);
309 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
310 spin_unlock_irqrestore(&tdc->lock, flags);
311}
312
Dmitry Osipenko39642932020-02-09 19:33:45 +0300313static struct tegra_dma_sg_req *
314tegra_dma_sg_req_get(struct tegra_dma_channel *tdc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530315{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300316 struct tegra_dma_sg_req *sg_req;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530317 unsigned long flags;
318
319 spin_lock_irqsave(&tdc->lock, flags);
320 if (!list_empty(&tdc->free_sg_req)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300321 sg_req = list_first_entry(&tdc->free_sg_req, typeof(*sg_req),
322 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530323 list_del(&sg_req->node);
324 spin_unlock_irqrestore(&tdc->lock, flags);
325 return sg_req;
326 }
327 spin_unlock_irqrestore(&tdc->lock, flags);
328
Dmitry Osipenko39642932020-02-09 19:33:45 +0300329 sg_req = kzalloc(sizeof(*sg_req), GFP_NOWAIT);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100330
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530331 return sg_req;
332}
333
334static int tegra_dma_slave_config(struct dma_chan *dc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300335 struct dma_slave_config *sconfig)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530336{
337 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
338
339 if (!list_empty(&tdc->pending_sg_req)) {
340 dev_err(tdc2dev(tdc), "Configuration not allowed\n");
341 return -EBUSY;
342 }
343
344 memcpy(&tdc->dma_sconfig, sconfig, sizeof(*sconfig));
Dmitry Osipenkof6160f32017-11-16 20:11:06 +0300345 if (tdc->slave_id == TEGRA_APBDMA_SLAVE_ID_INVALID &&
346 sconfig->device_fc) {
Shardar Shariff Md00ef4492016-04-23 15:06:00 +0530347 if (sconfig->slave_id > TEGRA_APBDMA_CSR_REQ_SEL_MASK)
348 return -EINVAL;
Stephen Warren996556c2013-11-11 13:09:35 -0700349 tdc->slave_id = sconfig->slave_id;
Shardar Shariff Md00ef4492016-04-23 15:06:00 +0530350 }
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530351 tdc->config_init = true;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300352
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530353 return 0;
354}
355
356static void tegra_dma_global_pause(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300357 bool wait_for_burst_complete)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530358{
359 struct tegra_dma *tdma = tdc->tdma;
360
361 spin_lock(&tdma->global_lock);
Jon Hunter23a1ec32015-08-06 14:32:33 +0100362
363 if (tdc->tdma->global_pause_count == 0) {
364 tdma_write(tdma, TEGRA_APBDMA_GENERAL, 0);
365 if (wait_for_burst_complete)
366 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
367 }
368
369 tdc->tdma->global_pause_count++;
370
371 spin_unlock(&tdma->global_lock);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530372}
373
374static void tegra_dma_global_resume(struct tegra_dma_channel *tdc)
375{
376 struct tegra_dma *tdma = tdc->tdma;
377
Jon Hunter23a1ec32015-08-06 14:32:33 +0100378 spin_lock(&tdma->global_lock);
379
380 if (WARN_ON(tdc->tdma->global_pause_count == 0))
381 goto out;
382
383 if (--tdc->tdma->global_pause_count == 0)
384 tdma_write(tdma, TEGRA_APBDMA_GENERAL,
385 TEGRA_APBDMA_GENERAL_ENABLE);
386
387out:
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530388 spin_unlock(&tdma->global_lock);
389}
390
Laxman Dewangan1b140902013-01-06 21:52:02 +0530391static void tegra_dma_pause(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300392 bool wait_for_burst_complete)
Laxman Dewangan1b140902013-01-06 21:52:02 +0530393{
394 struct tegra_dma *tdma = tdc->tdma;
395
396 if (tdma->chip_data->support_channel_pause) {
397 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300398 TEGRA_APBDMA_CHAN_CSRE_PAUSE);
Laxman Dewangan1b140902013-01-06 21:52:02 +0530399 if (wait_for_burst_complete)
400 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
401 } else {
402 tegra_dma_global_pause(tdc, wait_for_burst_complete);
403 }
404}
405
406static void tegra_dma_resume(struct tegra_dma_channel *tdc)
407{
408 struct tegra_dma *tdma = tdc->tdma;
409
Dmitry Osipenko39642932020-02-09 19:33:45 +0300410 if (tdma->chip_data->support_channel_pause)
Laxman Dewangan1b140902013-01-06 21:52:02 +0530411 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, 0);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300412 else
Laxman Dewangan1b140902013-01-06 21:52:02 +0530413 tegra_dma_global_resume(tdc);
Laxman Dewangan1b140902013-01-06 21:52:02 +0530414}
415
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530416static void tegra_dma_stop(struct tegra_dma_channel *tdc)
417{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300418 u32 csr, status;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530419
420 /* Disable interrupts */
421 csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
422 csr &= ~TEGRA_APBDMA_CSR_IE_EOC;
423 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
424
425 /* Disable DMA */
426 csr &= ~TEGRA_APBDMA_CSR_ENB;
427 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
428
429 /* Clear interrupt status if it is there */
430 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
431 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
432 dev_dbg(tdc2dev(tdc), "%s():clearing interrupt\n", __func__);
433 tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
434 }
435 tdc->busy = false;
436}
437
438static void tegra_dma_start(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300439 struct tegra_dma_sg_req *sg_req)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530440{
441 struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs;
442
443 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, ch_regs->csr);
444 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_regs->apb_seq);
445 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_regs->apb_ptr);
446 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_regs->ahb_seq);
447 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_regs->ahb_ptr);
Laxman Dewangan911dacc2014-01-06 11:16:45 -0700448 if (tdc->tdma->chip_data->support_separate_wcount_reg)
449 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT, ch_regs->wcount);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530450
451 /* Start DMA */
452 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300453 ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530454}
455
456static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300457 struct tegra_dma_sg_req *nsg_req)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530458{
459 unsigned long status;
460
461 /*
462 * The DMA controller reloads the new configuration for next transfer
463 * after last burst of current transfer completes.
464 * If there is no IEC status then this makes sure that last burst
465 * has not be completed. There may be case that last burst is on
466 * flight and so it can complete but because DMA is paused, it
467 * will not generates interrupt as well as not reload the new
468 * configuration.
469 * If there is already IEC status then interrupt handler need to
470 * load new configuration.
471 */
Laxman Dewangan1b140902013-01-06 21:52:02 +0530472 tegra_dma_pause(tdc, false);
Thierry Reding7b0e00d2016-06-14 16:18:46 +0200473 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530474
475 /*
476 * If interrupt is pending then do nothing as the ISR will handle
477 * the programing for new request.
478 */
479 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
480 dev_err(tdc2dev(tdc),
481 "Skipping new configuration as interrupt is pending\n");
Laxman Dewangan1b140902013-01-06 21:52:02 +0530482 tegra_dma_resume(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530483 return;
484 }
485
486 /* Safe to program new configuration */
487 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, nsg_req->ch_regs.apb_ptr);
488 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, nsg_req->ch_regs.ahb_ptr);
Laxman Dewangan911dacc2014-01-06 11:16:45 -0700489 if (tdc->tdma->chip_data->support_separate_wcount_reg)
490 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300491 nsg_req->ch_regs.wcount);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530492 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300493 nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530494 nsg_req->configured = true;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300495 nsg_req->words_xferred = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530496
Laxman Dewangan1b140902013-01-06 21:52:02 +0530497 tegra_dma_resume(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530498}
499
500static void tdc_start_head_req(struct tegra_dma_channel *tdc)
501{
502 struct tegra_dma_sg_req *sg_req;
503
Dmitry Osipenko39642932020-02-09 19:33:45 +0300504 sg_req = list_first_entry(&tdc->pending_sg_req, typeof(*sg_req), node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530505 tegra_dma_start(tdc, sg_req);
506 sg_req->configured = true;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300507 sg_req->words_xferred = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530508 tdc->busy = true;
509}
510
511static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
512{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300513 struct tegra_dma_sg_req *hsgreq, *hnsgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530514
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530515 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
516 if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300517 hnsgreq = list_first_entry(&hsgreq->node, typeof(*hnsgreq),
518 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530519 tegra_dma_configure_for_next(tdc, hnsgreq);
520 }
521}
522
Dmitry Osipenko39642932020-02-09 19:33:45 +0300523static inline unsigned int
524get_current_xferred_count(struct tegra_dma_channel *tdc,
525 struct tegra_dma_sg_req *sg_req,
526 unsigned long status)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530527{
528 return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4;
529}
530
531static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
532{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530533 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300534 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530535
536 while (!list_empty(&tdc->pending_sg_req)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300537 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
538 node);
Wei Yongjun2cc44e62012-09-05 15:08:56 +0800539 list_move_tail(&sgreq->node, &tdc->free_sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530540 if (sgreq->last_sg) {
541 dma_desc = sgreq->dma_desc;
542 dma_desc->dma_status = DMA_ERROR;
543 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
544
545 /* Add in cb list if it is not there. */
546 if (!dma_desc->cb_count)
547 list_add_tail(&dma_desc->cb_node,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300548 &tdc->cb_desc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530549 dma_desc->cb_count++;
550 }
551 }
552 tdc->isr_handler = NULL;
553}
554
555static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300556 bool to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530557{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300558 struct tegra_dma_sg_req *hsgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530559
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530560 /*
561 * Check that head req on list should be in flight.
562 * If it is not in flight then abort transfer as
563 * looping of transfer can not continue.
564 */
565 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
566 if (!hsgreq->configured) {
567 tegra_dma_stop(tdc);
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300568 pm_runtime_put(tdc->tdma->dev);
Dmitry Osipenko01b66a72020-02-09 19:33:56 +0300569 dev_err(tdc2dev(tdc), "DMA transfer underflow, aborting DMA\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530570 tegra_dma_abort_all(tdc);
571 return false;
572 }
573
574 /* Configure next request */
575 if (!to_terminate)
576 tdc_configure_next_head_desc(tdc);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300577
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530578 return true;
579}
580
581static void handle_once_dma_done(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300582 bool to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530583{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530584 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300585 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530586
587 tdc->busy = false;
588 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
589 dma_desc = sgreq->dma_desc;
590 dma_desc->bytes_transferred += sgreq->req_len;
591
592 list_del(&sgreq->node);
593 if (sgreq->last_sg) {
Vinod Koul00d696f2013-10-16 21:04:50 +0530594 dma_desc->dma_status = DMA_COMPLETE;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530595 dma_cookie_complete(&dma_desc->txd);
596 if (!dma_desc->cb_count)
597 list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
598 dma_desc->cb_count++;
599 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
600 }
601 list_add_tail(&sgreq->node, &tdc->free_sg_req);
602
603 /* Do not start DMA if it is going to be terminate */
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300604 if (to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530605 return;
606
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300607 if (list_empty(&tdc->pending_sg_req)) {
608 pm_runtime_put(tdc->tdma->dev);
609 return;
610 }
611
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530612 tdc_start_head_req(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530613}
614
615static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300616 bool to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530617{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530618 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300619 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530620 bool st;
621
622 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
623 dma_desc = sgreq->dma_desc;
Ben Dookse486df32018-11-21 16:13:19 +0000624 /* if we dma for long enough the transfer count will wrap */
625 dma_desc->bytes_transferred =
626 (dma_desc->bytes_transferred + sgreq->req_len) %
627 dma_desc->bytes_requested;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530628
629 /* Callback need to be call */
630 if (!dma_desc->cb_count)
631 list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
632 dma_desc->cb_count++;
633
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300634 sgreq->words_xferred = 0;
635
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530636 /* If not last req then put at end of pending list */
637 if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
Wei Yongjun2cc44e62012-09-05 15:08:56 +0800638 list_move_tail(&sgreq->node, &tdc->pending_sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530639 sgreq->configured = false;
Dmitry Osipenkof261f1c2020-02-09 19:33:55 +0300640 st = handle_continuous_head_request(tdc, to_terminate);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530641 if (!st)
642 dma_desc->dma_status = DMA_ERROR;
643 }
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530644}
645
646static void tegra_dma_tasklet(unsigned long data)
647{
648 struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data;
Dave Jiang370c0442016-07-20 13:13:16 -0700649 struct dmaengine_desc_callback cb;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530650 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300651 unsigned int cb_count;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530652 unsigned long flags;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530653
654 spin_lock_irqsave(&tdc->lock, flags);
655 while (!list_empty(&tdc->cb_desc)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300656 dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
657 cb_node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530658 list_del(&dma_desc->cb_node);
Dave Jiang370c0442016-07-20 13:13:16 -0700659 dmaengine_desc_get_callback(&dma_desc->txd, &cb);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530660 cb_count = dma_desc->cb_count;
661 dma_desc->cb_count = 0;
Ben Dooks95f295f2018-11-21 16:13:23 +0000662 trace_tegra_dma_complete_cb(&tdc->dma_chan, cb_count,
663 cb.callback);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530664 spin_unlock_irqrestore(&tdc->lock, flags);
Dave Jiang370c0442016-07-20 13:13:16 -0700665 while (cb_count--)
666 dmaengine_desc_callback_invoke(&cb, NULL);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530667 spin_lock_irqsave(&tdc->lock, flags);
668 }
669 spin_unlock_irqrestore(&tdc->lock, flags);
670}
671
672static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
673{
674 struct tegra_dma_channel *tdc = dev_id;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530675 unsigned long flags;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300676 u32 status;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530677
678 spin_lock_irqsave(&tdc->lock, flags);
679
Ben Dooks95f295f2018-11-21 16:13:23 +0000680 trace_tegra_dma_isr(&tdc->dma_chan, irq);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530681 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
682 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
683 tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
684 tdc->isr_handler(tdc, false);
685 tasklet_schedule(&tdc->tasklet);
686 spin_unlock_irqrestore(&tdc->lock, flags);
687 return IRQ_HANDLED;
688 }
689
690 spin_unlock_irqrestore(&tdc->lock, flags);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300691 dev_info(tdc2dev(tdc), "Interrupt already served status 0x%08x\n",
692 status);
693
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530694 return IRQ_NONE;
695}
696
697static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *txd)
698{
699 struct tegra_dma_desc *dma_desc = txd_to_tegra_dma_desc(txd);
700 struct tegra_dma_channel *tdc = to_tegra_dma_chan(txd->chan);
701 unsigned long flags;
702 dma_cookie_t cookie;
703
704 spin_lock_irqsave(&tdc->lock, flags);
705 dma_desc->dma_status = DMA_IN_PROGRESS;
706 cookie = dma_cookie_assign(&dma_desc->txd);
707 list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req);
708 spin_unlock_irqrestore(&tdc->lock, flags);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300709
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530710 return cookie;
711}
712
713static void tegra_dma_issue_pending(struct dma_chan *dc)
714{
715 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
716 unsigned long flags;
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300717 int err;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530718
719 spin_lock_irqsave(&tdc->lock, flags);
720 if (list_empty(&tdc->pending_sg_req)) {
721 dev_err(tdc2dev(tdc), "No DMA request\n");
722 goto end;
723 }
724 if (!tdc->busy) {
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300725 err = pm_runtime_get_sync(tdc->tdma->dev);
726 if (err < 0) {
727 dev_err(tdc2dev(tdc), "Failed to enable DMA\n");
728 goto end;
729 }
730
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530731 tdc_start_head_req(tdc);
732
733 /* Continuous single mode: Configure next req */
734 if (tdc->cyclic) {
735 /*
736 * Wait for 1 burst time for configure DMA for
737 * next transfer.
738 */
739 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
740 tdc_configure_next_head_desc(tdc);
741 }
742 }
743end:
744 spin_unlock_irqrestore(&tdc->lock, flags);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530745}
746
Vinod Koula7c439a2014-12-08 11:30:17 +0530747static int tegra_dma_terminate_all(struct dma_chan *dc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530748{
749 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530750 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300751 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530752 unsigned long flags;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300753 u32 status, wcount;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530754 bool was_busy;
755
756 spin_lock_irqsave(&tdc->lock, flags);
757 if (list_empty(&tdc->pending_sg_req)) {
758 spin_unlock_irqrestore(&tdc->lock, flags);
Vinod Koula7c439a2014-12-08 11:30:17 +0530759 return 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530760 }
761
762 if (!tdc->busy)
763 goto skip_dma_stop;
764
765 /* Pause DMA before checking the queue status */
Laxman Dewangan1b140902013-01-06 21:52:02 +0530766 tegra_dma_pause(tdc, true);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530767
768 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
769 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
770 dev_dbg(tdc2dev(tdc), "%s():handling isr\n", __func__);
771 tdc->isr_handler(tdc, true);
772 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
773 }
Laxman Dewangan911dacc2014-01-06 11:16:45 -0700774 if (tdc->tdma->chip_data->support_separate_wcount_reg)
775 wcount = tdc_read(tdc, TEGRA_APBDMA_CHAN_WORD_TRANSFER);
776 else
777 wcount = status;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530778
779 was_busy = tdc->busy;
780 tegra_dma_stop(tdc);
781
782 if (!list_empty(&tdc->pending_sg_req) && was_busy) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300783 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
784 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530785 sgreq->dma_desc->bytes_transferred +=
Laxman Dewangan911dacc2014-01-06 11:16:45 -0700786 get_current_xferred_count(tdc, sgreq, wcount);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530787 }
Laxman Dewangan1b140902013-01-06 21:52:02 +0530788 tegra_dma_resume(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530789
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300790 pm_runtime_put(tdc->tdma->dev);
791
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530792skip_dma_stop:
793 tegra_dma_abort_all(tdc);
794
795 while (!list_empty(&tdc->cb_desc)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300796 dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
797 cb_node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530798 list_del(&dma_desc->cb_node);
799 dma_desc->cb_count = 0;
800 }
801 spin_unlock_irqrestore(&tdc->lock, flags);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300802
Vinod Koula7c439a2014-12-08 11:30:17 +0530803 return 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530804}
805
Dmitry Osipenkodda5e352020-02-09 19:33:40 +0300806static void tegra_dma_synchronize(struct dma_chan *dc)
807{
808 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
809
810 tasklet_kill(&tdc->tasklet);
811}
812
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300813static unsigned int tegra_dma_sg_bytes_xferred(struct tegra_dma_channel *tdc,
814 struct tegra_dma_sg_req *sg_req)
815{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300816 u32 status, wcount = 0;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300817
818 if (!list_is_first(&sg_req->node, &tdc->pending_sg_req))
819 return 0;
820
821 if (tdc->tdma->chip_data->support_separate_wcount_reg)
822 wcount = tdc_read(tdc, TEGRA_APBDMA_CHAN_WORD_TRANSFER);
823
824 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
825
826 if (!tdc->tdma->chip_data->support_separate_wcount_reg)
827 wcount = status;
828
829 if (status & TEGRA_APBDMA_STATUS_ISE_EOC)
830 return sg_req->req_len;
831
832 wcount = get_current_xferred_count(tdc, sg_req, wcount);
833
834 if (!wcount) {
835 /*
836 * If wcount wasn't ever polled for this SG before, then
837 * simply assume that transfer hasn't started yet.
838 *
839 * Otherwise it's the end of the transfer.
840 *
841 * The alternative would be to poll the status register
842 * until EOC bit is set or wcount goes UP. That's so
843 * because EOC bit is getting set only after the last
844 * burst's completion and counter is less than the actual
845 * transfer size by 4 bytes. The counter value wraps around
846 * in a cyclic mode before EOC is set(!), so we can't easily
847 * distinguish start of transfer from its end.
848 */
849 if (sg_req->words_xferred)
850 wcount = sg_req->req_len - 4;
851
852 } else if (wcount < sg_req->words_xferred) {
853 /*
854 * This case will never happen for a non-cyclic transfer.
855 *
856 * For a cyclic transfer, although it is possible for the
857 * next transfer to have already started (resetting the word
858 * count), this case should still not happen because we should
859 * have detected that the EOC bit is set and hence the transfer
860 * was completed.
861 */
862 WARN_ON_ONCE(1);
863
864 wcount = sg_req->req_len - 4;
865 } else {
866 sg_req->words_xferred = wcount;
867 }
868
869 return wcount;
870}
871
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530872static enum dma_status tegra_dma_tx_status(struct dma_chan *dc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300873 dma_cookie_t cookie,
874 struct dma_tx_state *txstate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530875{
876 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
877 struct tegra_dma_desc *dma_desc;
878 struct tegra_dma_sg_req *sg_req;
879 enum dma_status ret;
880 unsigned long flags;
Laxman Dewangan4a46ba32012-07-02 13:52:07 +0530881 unsigned int residual;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300882 unsigned int bytes = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530883
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530884 ret = dma_cookie_status(dc, cookie, txstate);
Jon Hunterd3183442016-06-29 17:08:39 +0100885 if (ret == DMA_COMPLETE)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530886 return ret;
Andy Shevchenko0a0aee22013-05-27 15:14:39 +0300887
888 spin_lock_irqsave(&tdc->lock, flags);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530889
890 /* Check on wait_ack desc status */
891 list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
892 if (dma_desc->txd.cookie == cookie) {
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530893 ret = dma_desc->dma_status;
Jon Hunter004f6142016-06-29 17:08:38 +0100894 goto found;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530895 }
896 }
897
898 /* Check in pending list */
899 list_for_each_entry(sg_req, &tdc->pending_sg_req, node) {
900 dma_desc = sg_req->dma_desc;
901 if (dma_desc->txd.cookie == cookie) {
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300902 bytes = tegra_dma_sg_bytes_xferred(tdc, sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530903 ret = dma_desc->dma_status;
Jon Hunter004f6142016-06-29 17:08:38 +0100904 goto found;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530905 }
906 }
907
Jon Hunter019bfcc2016-06-29 17:08:37 +0100908 dev_dbg(tdc2dev(tdc), "cookie %d not found\n", cookie);
Jon Hunter004f6142016-06-29 17:08:38 +0100909 dma_desc = NULL;
910
911found:
Jon Hunterd3183442016-06-29 17:08:39 +0100912 if (dma_desc && txstate) {
Jon Hunter004f6142016-06-29 17:08:38 +0100913 residual = dma_desc->bytes_requested -
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300914 ((dma_desc->bytes_transferred + bytes) %
Jon Hunter004f6142016-06-29 17:08:38 +0100915 dma_desc->bytes_requested);
916 dma_set_residue(txstate, residual);
917 }
918
Ben Dooks95f295f2018-11-21 16:13:23 +0000919 trace_tegra_dma_tx_status(&tdc->dma_chan, cookie, txstate);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530920 spin_unlock_irqrestore(&tdc->lock, flags);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300921
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530922 return ret;
923}
924
Dmitry Osipenko39642932020-02-09 19:33:45 +0300925static inline unsigned int get_bus_width(struct tegra_dma_channel *tdc,
926 enum dma_slave_buswidth slave_bw)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530927{
928 switch (slave_bw) {
929 case DMA_SLAVE_BUSWIDTH_1_BYTE:
930 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8;
931 case DMA_SLAVE_BUSWIDTH_2_BYTES:
932 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16;
933 case DMA_SLAVE_BUSWIDTH_4_BYTES:
934 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
935 case DMA_SLAVE_BUSWIDTH_8_BYTES:
936 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64;
937 default:
938 dev_warn(tdc2dev(tdc),
Dmitry Osipenko39642932020-02-09 19:33:45 +0300939 "slave bw is not supported, using 32bits\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530940 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
941 }
942}
943
Dmitry Osipenko39642932020-02-09 19:33:45 +0300944static inline unsigned int get_burst_size(struct tegra_dma_channel *tdc,
945 u32 burst_size,
946 enum dma_slave_buswidth slave_bw,
947 u32 len)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530948{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300949 unsigned int burst_byte, burst_ahb_width;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530950
951 /*
952 * burst_size from client is in terms of the bus_width.
953 * convert them into AHB memory width which is 4 byte.
954 */
955 burst_byte = burst_size * slave_bw;
956 burst_ahb_width = burst_byte / 4;
957
958 /* If burst size is 0 then calculate the burst size based on length */
959 if (!burst_ahb_width) {
960 if (len & 0xF)
961 return TEGRA_APBDMA_AHBSEQ_BURST_1;
962 else if ((len >> 4) & 0x1)
963 return TEGRA_APBDMA_AHBSEQ_BURST_4;
964 else
965 return TEGRA_APBDMA_AHBSEQ_BURST_8;
966 }
967 if (burst_ahb_width < 4)
968 return TEGRA_APBDMA_AHBSEQ_BURST_1;
969 else if (burst_ahb_width < 8)
970 return TEGRA_APBDMA_AHBSEQ_BURST_4;
971 else
972 return TEGRA_APBDMA_AHBSEQ_BURST_8;
973}
974
975static int get_transfer_param(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300976 enum dma_transfer_direction direction,
977 u32 *apb_addr,
978 u32 *apb_seq,
979 u32 *csr,
980 unsigned int *burst_size,
981 enum dma_slave_buswidth *slave_bw)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530982{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530983 switch (direction) {
984 case DMA_MEM_TO_DEV:
985 *apb_addr = tdc->dma_sconfig.dst_addr;
986 *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.dst_addr_width);
987 *burst_size = tdc->dma_sconfig.dst_maxburst;
988 *slave_bw = tdc->dma_sconfig.dst_addr_width;
989 *csr = TEGRA_APBDMA_CSR_DIR;
990 return 0;
991
992 case DMA_DEV_TO_MEM:
993 *apb_addr = tdc->dma_sconfig.src_addr;
994 *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.src_addr_width);
995 *burst_size = tdc->dma_sconfig.src_maxburst;
996 *slave_bw = tdc->dma_sconfig.src_addr_width;
997 *csr = 0;
998 return 0;
999
1000 default:
Ben Dooks547b3112018-11-21 16:13:21 +00001001 dev_err(tdc2dev(tdc), "DMA direction is not supported\n");
Dmitry Osipenko39642932020-02-09 19:33:45 +03001002 break;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301003 }
Dmitry Osipenko39642932020-02-09 19:33:45 +03001004
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301005 return -EINVAL;
1006}
1007
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001008static void tegra_dma_prep_wcount(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001009 struct tegra_dma_channel_regs *ch_regs,
1010 u32 len)
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001011{
1012 u32 len_field = (len - 4) & 0xFFFC;
1013
1014 if (tdc->tdma->chip_data->support_separate_wcount_reg)
1015 ch_regs->wcount = len_field;
1016 else
1017 ch_regs->csr |= len_field;
1018}
1019
Dmitry Osipenko39642932020-02-09 19:33:45 +03001020static struct dma_async_tx_descriptor *
1021tegra_dma_prep_slave_sg(struct dma_chan *dc,
1022 struct scatterlist *sgl,
1023 unsigned int sg_len,
1024 enum dma_transfer_direction direction,
1025 unsigned long flags,
1026 void *context)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301027{
1028 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001029 struct tegra_dma_sg_req *sg_req = NULL;
1030 u32 csr, ahb_seq, apb_ptr, apb_seq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301031 enum dma_slave_buswidth slave_bw;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001032 struct tegra_dma_desc *dma_desc;
1033 struct list_head req_list;
1034 struct scatterlist *sg;
1035 unsigned int burst_size;
1036 unsigned int i;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301037
1038 if (!tdc->config_init) {
Ben Dooks547b3112018-11-21 16:13:21 +00001039 dev_err(tdc2dev(tdc), "DMA channel is not configured\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301040 return NULL;
1041 }
1042 if (sg_len < 1) {
1043 dev_err(tdc2dev(tdc), "Invalid segment length %d\n", sg_len);
1044 return NULL;
1045 }
1046
Jon Hunterdc1ff4b2015-08-06 14:32:32 +01001047 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001048 &burst_size, &slave_bw) < 0)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301049 return NULL;
1050
1051 INIT_LIST_HEAD(&req_list);
1052
1053 ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
1054 ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
1055 TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
1056 ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
1057
Dmitry Osipenkof6160f32017-11-16 20:11:06 +03001058 csr |= TEGRA_APBDMA_CSR_ONCE;
1059
1060 if (tdc->slave_id != TEGRA_APBDMA_SLAVE_ID_INVALID) {
1061 csr |= TEGRA_APBDMA_CSR_FLOW;
1062 csr |= tdc->slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
1063 }
1064
Dmitry Osipenkodc161062019-05-30 00:43:55 +03001065 if (flags & DMA_PREP_INTERRUPT) {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301066 csr |= TEGRA_APBDMA_CSR_IE_EOC;
Dmitry Osipenkodc161062019-05-30 00:43:55 +03001067 } else {
1068 WARN_ON_ONCE(1);
1069 return NULL;
1070 }
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301071
1072 apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
1073
1074 dma_desc = tegra_dma_desc_get(tdc);
1075 if (!dma_desc) {
Ben Dooks547b3112018-11-21 16:13:21 +00001076 dev_err(tdc2dev(tdc), "DMA descriptors not available\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301077 return NULL;
1078 }
1079 INIT_LIST_HEAD(&dma_desc->tx_list);
1080 INIT_LIST_HEAD(&dma_desc->cb_node);
1081 dma_desc->cb_count = 0;
1082 dma_desc->bytes_requested = 0;
1083 dma_desc->bytes_transferred = 0;
1084 dma_desc->dma_status = DMA_IN_PROGRESS;
1085
1086 /* Make transfer requests */
1087 for_each_sg(sgl, sg, sg_len, i) {
1088 u32 len, mem;
1089
Laxman Dewangan597c8542012-06-22 20:41:10 +05301090 mem = sg_dma_address(sg);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301091 len = sg_dma_len(sg);
1092
1093 if ((len & 3) || (mem & 3) ||
Dmitry Osipenko39642932020-02-09 19:33:45 +03001094 len > tdc->tdma->chip_data->max_dma_count) {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301095 dev_err(tdc2dev(tdc),
Ben Dooks547b3112018-11-21 16:13:21 +00001096 "DMA length/memory address is not supported\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301097 tegra_dma_desc_put(tdc, dma_desc);
1098 return NULL;
1099 }
1100
1101 sg_req = tegra_dma_sg_req_get(tdc);
1102 if (!sg_req) {
Ben Dooks547b3112018-11-21 16:13:21 +00001103 dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301104 tegra_dma_desc_put(tdc, dma_desc);
1105 return NULL;
1106 }
1107
1108 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
1109 dma_desc->bytes_requested += len;
1110
1111 sg_req->ch_regs.apb_ptr = apb_ptr;
1112 sg_req->ch_regs.ahb_ptr = mem;
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001113 sg_req->ch_regs.csr = csr;
1114 tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301115 sg_req->ch_regs.apb_seq = apb_seq;
1116 sg_req->ch_regs.ahb_seq = ahb_seq;
1117 sg_req->configured = false;
1118 sg_req->last_sg = false;
1119 sg_req->dma_desc = dma_desc;
1120 sg_req->req_len = len;
1121
1122 list_add_tail(&sg_req->node, &dma_desc->tx_list);
1123 }
1124 sg_req->last_sg = true;
1125 if (flags & DMA_CTRL_ACK)
1126 dma_desc->txd.flags = DMA_CTRL_ACK;
1127
1128 /*
1129 * Make sure that mode should not be conflicting with currently
1130 * configured mode.
1131 */
1132 if (!tdc->isr_handler) {
1133 tdc->isr_handler = handle_once_dma_done;
1134 tdc->cyclic = false;
1135 } else {
1136 if (tdc->cyclic) {
1137 dev_err(tdc2dev(tdc), "DMA configured in cyclic mode\n");
1138 tegra_dma_desc_put(tdc, dma_desc);
1139 return NULL;
1140 }
1141 }
1142
1143 return &dma_desc->txd;
1144}
1145
Dmitry Osipenko39642932020-02-09 19:33:45 +03001146static struct dma_async_tx_descriptor *
1147tegra_dma_prep_dma_cyclic(struct dma_chan *dc, dma_addr_t buf_addr,
1148 size_t buf_len,
1149 size_t period_len,
1150 enum dma_transfer_direction direction,
1151 unsigned long flags)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301152{
1153 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Thierry Reding7b0e00d2016-06-14 16:18:46 +02001154 struct tegra_dma_sg_req *sg_req = NULL;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001155 u32 csr, ahb_seq, apb_ptr, apb_seq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301156 enum dma_slave_buswidth slave_bw;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001157 struct tegra_dma_desc *dma_desc;
1158 dma_addr_t mem = buf_addr;
1159 unsigned int burst_size;
1160 size_t len, remain_len;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301161
1162 if (!buf_len || !period_len) {
1163 dev_err(tdc2dev(tdc), "Invalid buffer/period len\n");
1164 return NULL;
1165 }
1166
1167 if (!tdc->config_init) {
1168 dev_err(tdc2dev(tdc), "DMA slave is not configured\n");
1169 return NULL;
1170 }
1171
1172 /*
1173 * We allow to take more number of requests till DMA is
1174 * not started. The driver will loop over all requests.
1175 * Once DMA is started then new requests can be queued only after
1176 * terminating the DMA.
1177 */
1178 if (tdc->busy) {
Ben Dooks547b3112018-11-21 16:13:21 +00001179 dev_err(tdc2dev(tdc), "Request not allowed when DMA running\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301180 return NULL;
1181 }
1182
1183 /*
1184 * We only support cycle transfer when buf_len is multiple of
1185 * period_len.
1186 */
1187 if (buf_len % period_len) {
1188 dev_err(tdc2dev(tdc), "buf_len is not multiple of period_len\n");
1189 return NULL;
1190 }
1191
1192 len = period_len;
1193 if ((len & 3) || (buf_addr & 3) ||
Dmitry Osipenko39642932020-02-09 19:33:45 +03001194 len > tdc->tdma->chip_data->max_dma_count) {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301195 dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n");
1196 return NULL;
1197 }
1198
Jon Hunterdc1ff4b2015-08-06 14:32:32 +01001199 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001200 &burst_size, &slave_bw) < 0)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301201 return NULL;
1202
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301203 ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
1204 ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
1205 TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
1206 ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
1207
Dmitry Osipenkof6160f32017-11-16 20:11:06 +03001208 if (tdc->slave_id != TEGRA_APBDMA_SLAVE_ID_INVALID) {
1209 csr |= TEGRA_APBDMA_CSR_FLOW;
1210 csr |= tdc->slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
1211 }
1212
Dmitry Osipenkodc161062019-05-30 00:43:55 +03001213 if (flags & DMA_PREP_INTERRUPT) {
Laxman Dewanganb9bb37f2013-01-09 15:26:22 +05301214 csr |= TEGRA_APBDMA_CSR_IE_EOC;
Dmitry Osipenkodc161062019-05-30 00:43:55 +03001215 } else {
1216 WARN_ON_ONCE(1);
1217 return NULL;
1218 }
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301219
1220 apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
1221
1222 dma_desc = tegra_dma_desc_get(tdc);
1223 if (!dma_desc) {
1224 dev_err(tdc2dev(tdc), "not enough descriptors available\n");
1225 return NULL;
1226 }
1227
1228 INIT_LIST_HEAD(&dma_desc->tx_list);
1229 INIT_LIST_HEAD(&dma_desc->cb_node);
1230 dma_desc->cb_count = 0;
1231
1232 dma_desc->bytes_transferred = 0;
1233 dma_desc->bytes_requested = buf_len;
1234 remain_len = buf_len;
1235
1236 /* Split transfer equal to period size */
1237 while (remain_len) {
1238 sg_req = tegra_dma_sg_req_get(tdc);
1239 if (!sg_req) {
Ben Dooks547b3112018-11-21 16:13:21 +00001240 dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301241 tegra_dma_desc_put(tdc, dma_desc);
1242 return NULL;
1243 }
1244
1245 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
1246 sg_req->ch_regs.apb_ptr = apb_ptr;
1247 sg_req->ch_regs.ahb_ptr = mem;
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001248 sg_req->ch_regs.csr = csr;
1249 tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301250 sg_req->ch_regs.apb_seq = apb_seq;
1251 sg_req->ch_regs.ahb_seq = ahb_seq;
1252 sg_req->configured = false;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301253 sg_req->last_sg = false;
1254 sg_req->dma_desc = dma_desc;
1255 sg_req->req_len = len;
1256
1257 list_add_tail(&sg_req->node, &dma_desc->tx_list);
1258 remain_len -= len;
1259 mem += len;
1260 }
1261 sg_req->last_sg = true;
Laxman Dewanganb9bb37f2013-01-09 15:26:22 +05301262 if (flags & DMA_CTRL_ACK)
1263 dma_desc->txd.flags = DMA_CTRL_ACK;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301264
1265 /*
1266 * Make sure that mode should not be conflicting with currently
1267 * configured mode.
1268 */
1269 if (!tdc->isr_handler) {
1270 tdc->isr_handler = handle_cont_sngl_cycle_dma_done;
1271 tdc->cyclic = true;
1272 } else {
1273 if (!tdc->cyclic) {
1274 dev_err(tdc2dev(tdc), "DMA configuration conflict\n");
1275 tegra_dma_desc_put(tdc, dma_desc);
1276 return NULL;
1277 }
1278 }
1279
1280 return &dma_desc->txd;
1281}
1282
1283static int tegra_dma_alloc_chan_resources(struct dma_chan *dc)
1284{
1285 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1286
1287 dma_cookie_init(&tdc->dma_chan);
Jon Hunteredd3bdb2015-11-13 16:39:38 +00001288
Jon Hunteredd3bdb2015-11-13 16:39:38 +00001289 return 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301290}
1291
1292static void tegra_dma_free_chan_resources(struct dma_chan *dc)
1293{
1294 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301295 struct tegra_dma_desc *dma_desc;
1296 struct tegra_dma_sg_req *sg_req;
1297 struct list_head dma_desc_list;
1298 struct list_head sg_req_list;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301299
1300 INIT_LIST_HEAD(&dma_desc_list);
1301 INIT_LIST_HEAD(&sg_req_list);
1302
1303 dev_dbg(tdc2dev(tdc), "Freeing channel %d\n", tdc->id);
1304
Dmitry Osipenko8e841722020-02-09 19:33:41 +03001305 tegra_dma_terminate_all(dc);
Dmitry Osipenko41ffc422020-02-09 19:33:42 +03001306 tasklet_kill(&tdc->tasklet);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301307
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301308 list_splice_init(&tdc->pending_sg_req, &sg_req_list);
1309 list_splice_init(&tdc->free_sg_req, &sg_req_list);
1310 list_splice_init(&tdc->free_dma_desc, &dma_desc_list);
1311 INIT_LIST_HEAD(&tdc->cb_desc);
1312 tdc->config_init = false;
Dmitry Osipenko7bdc1e22013-05-11 20:30:53 +04001313 tdc->isr_handler = NULL;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301314
1315 while (!list_empty(&dma_desc_list)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +03001316 dma_desc = list_first_entry(&dma_desc_list, typeof(*dma_desc),
1317 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301318 list_del(&dma_desc->node);
1319 kfree(dma_desc);
1320 }
1321
1322 while (!list_empty(&sg_req_list)) {
1323 sg_req = list_first_entry(&sg_req_list, typeof(*sg_req), node);
1324 list_del(&sg_req->node);
1325 kfree(sg_req);
1326 }
Stephen Warren996556c2013-11-11 13:09:35 -07001327
Shardar Shariff Md00ef4492016-04-23 15:06:00 +05301328 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
Stephen Warren996556c2013-11-11 13:09:35 -07001329}
1330
1331static struct dma_chan *tegra_dma_of_xlate(struct of_phandle_args *dma_spec,
1332 struct of_dma *ofdma)
1333{
1334 struct tegra_dma *tdma = ofdma->of_dma_data;
Stephen Warren996556c2013-11-11 13:09:35 -07001335 struct tegra_dma_channel *tdc;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001336 struct dma_chan *chan;
Stephen Warren996556c2013-11-11 13:09:35 -07001337
Shardar Shariff Md00ef4492016-04-23 15:06:00 +05301338 if (dma_spec->args[0] > TEGRA_APBDMA_CSR_REQ_SEL_MASK) {
1339 dev_err(tdma->dev, "Invalid slave id: %d\n", dma_spec->args[0]);
1340 return NULL;
1341 }
1342
Stephen Warren996556c2013-11-11 13:09:35 -07001343 chan = dma_get_any_slave_channel(&tdma->dma_dev);
1344 if (!chan)
1345 return NULL;
1346
1347 tdc = to_tegra_dma_chan(chan);
1348 tdc->slave_id = dma_spec->args[0];
1349
1350 return chan;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301351}
1352
1353/* Tegra20 specific DMA controller information */
Laxman Dewangan75f21632012-08-29 10:31:18 +02001354static const struct tegra_dma_chip_data tegra20_dma_chip_data = {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301355 .nr_channels = 16,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001356 .channel_reg_size = 0x20,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301357 .max_dma_count = 1024UL * 64,
Laxman Dewangan1b140902013-01-06 21:52:02 +05301358 .support_channel_pause = false,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001359 .support_separate_wcount_reg = false,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301360};
1361
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301362/* Tegra30 specific DMA controller information */
Laxman Dewangan75f21632012-08-29 10:31:18 +02001363static const struct tegra_dma_chip_data tegra30_dma_chip_data = {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301364 .nr_channels = 32,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001365 .channel_reg_size = 0x20,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301366 .max_dma_count = 1024UL * 64,
Laxman Dewangan1b140902013-01-06 21:52:02 +05301367 .support_channel_pause = false,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001368 .support_separate_wcount_reg = false,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301369};
1370
Laxman Dewangan5ea7caf2013-01-06 21:52:03 +05301371/* Tegra114 specific DMA controller information */
1372static const struct tegra_dma_chip_data tegra114_dma_chip_data = {
1373 .nr_channels = 32,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001374 .channel_reg_size = 0x20,
Laxman Dewangan5ea7caf2013-01-06 21:52:03 +05301375 .max_dma_count = 1024UL * 64,
1376 .support_channel_pause = true,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001377 .support_separate_wcount_reg = false,
1378};
1379
1380/* Tegra148 specific DMA controller information */
1381static const struct tegra_dma_chip_data tegra148_dma_chip_data = {
1382 .nr_channels = 32,
1383 .channel_reg_size = 0x40,
1384 .max_dma_count = 1024UL * 64,
1385 .support_channel_pause = true,
1386 .support_separate_wcount_reg = true,
Laxman Dewangan5ea7caf2013-01-06 21:52:03 +05301387};
1388
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001389static int tegra_dma_init_hw(struct tegra_dma *tdma)
1390{
1391 int err;
1392
1393 err = reset_control_assert(tdma->rst);
1394 if (err) {
1395 dev_err(tdma->dev, "failed to assert reset: %d\n", err);
1396 return err;
1397 }
1398
1399 err = clk_enable(tdma->dma_clk);
1400 if (err) {
1401 dev_err(tdma->dev, "failed to enable clk: %d\n", err);
1402 return err;
1403 }
1404
1405 /* reset DMA controller */
1406 udelay(2);
1407 reset_control_deassert(tdma->rst);
1408
1409 /* enable global DMA registers */
1410 tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
1411 tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
1412 tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFF);
1413
1414 clk_disable(tdma->dma_clk);
1415
1416 return 0;
1417}
1418
Bill Pemberton463a1f82012-11-19 13:22:55 -05001419static int tegra_dma_probe(struct platform_device *pdev)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301420{
Laxman Dewangan333f16e2016-03-01 18:54:40 +05301421 const struct tegra_dma_chip_data *cdata;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001422 struct tegra_dma *tdma;
1423 unsigned int i;
1424 size_t size;
1425 int ret;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301426
Laxman Dewangan333f16e2016-03-01 18:54:40 +05301427 cdata = of_device_get_match_data(&pdev->dev);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001428 size = struct_size(tdma, channels, cdata->nr_channels);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301429
Dmitry Osipenko39642932020-02-09 19:33:45 +03001430 tdma = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Peter Griffinaef94fe2016-06-07 18:38:41 +01001431 if (!tdma)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301432 return -ENOMEM;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301433
1434 tdma->dev = &pdev->dev;
1435 tdma->chip_data = cdata;
1436 platform_set_drvdata(pdev, tdma);
1437
Dmitry Osipenkoc55c745e2020-02-09 19:33:43 +03001438 tdma->base_addr = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding73312052013-01-21 11:09:00 +01001439 if (IS_ERR(tdma->base_addr))
1440 return PTR_ERR(tdma->base_addr);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301441
1442 tdma->dma_clk = devm_clk_get(&pdev->dev, NULL);
1443 if (IS_ERR(tdma->dma_clk)) {
1444 dev_err(&pdev->dev, "Error: Missing controller clock\n");
1445 return PTR_ERR(tdma->dma_clk);
1446 }
1447
Stephen Warren9aa433d2013-11-06 16:35:34 -07001448 tdma->rst = devm_reset_control_get(&pdev->dev, "dma");
1449 if (IS_ERR(tdma->rst)) {
1450 dev_err(&pdev->dev, "Error: Missing reset\n");
1451 return PTR_ERR(tdma->rst);
1452 }
1453
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301454 spin_lock_init(&tdma->global_lock);
1455
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001456 ret = clk_prepare(tdma->dma_clk);
1457 if (ret)
1458 return ret;
1459
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001460 ret = tegra_dma_init_hw(tdma);
1461 if (ret)
1462 goto err_clk_unprepare;
1463
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001464 pm_runtime_irq_safe(&pdev->dev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301465 pm_runtime_enable(&pdev->dev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301466
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301467 INIT_LIST_HEAD(&tdma->dma_dev.channels);
1468 for (i = 0; i < cdata->nr_channels; i++) {
1469 struct tegra_dma_channel *tdc = &tdma->channels[i];
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001470 int irq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301471
Jon Hunter13a33282015-08-06 14:32:31 +01001472 tdc->chan_addr = tdma->base_addr +
1473 TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
1474 (i * cdata->channel_reg_size);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301475
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001476 irq = platform_get_irq(pdev, i);
1477 if (irq < 0) {
1478 ret = irq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301479 dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001480 goto err_pm_disable;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301481 }
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001482
Laxman Dewangand0fc9052012-10-03 22:48:07 +05301483 snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i);
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001484 ret = devm_request_irq(&pdev->dev, irq, tegra_dma_isr, 0,
1485 tdc->name, tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301486 if (ret) {
1487 dev_err(&pdev->dev,
1488 "request_irq failed with err %d channel %d\n",
Dmitry Osipenkoac7ae752013-05-11 20:30:52 +04001489 ret, i);
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001490 goto err_pm_disable;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301491 }
1492
1493 tdc->dma_chan.device = &tdma->dma_dev;
1494 dma_cookie_init(&tdc->dma_chan);
1495 list_add_tail(&tdc->dma_chan.device_node,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001496 &tdma->dma_dev.channels);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301497 tdc->tdma = tdma;
1498 tdc->id = i;
Shardar Shariff Md00ef4492016-04-23 15:06:00 +05301499 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301500
1501 tasklet_init(&tdc->tasklet, tegra_dma_tasklet,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001502 (unsigned long)tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301503 spin_lock_init(&tdc->lock);
1504
1505 INIT_LIST_HEAD(&tdc->pending_sg_req);
1506 INIT_LIST_HEAD(&tdc->free_sg_req);
1507 INIT_LIST_HEAD(&tdc->free_dma_desc);
1508 INIT_LIST_HEAD(&tdc->cb_desc);
1509 }
1510
1511 dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask);
1512 dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask);
Laxman Dewangan46fb3f82012-06-22 17:12:43 +05301513 dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
1514
Jon Hunter23a1ec32015-08-06 14:32:33 +01001515 tdma->global_pause_count = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301516 tdma->dma_dev.dev = &pdev->dev;
1517 tdma->dma_dev.device_alloc_chan_resources =
1518 tegra_dma_alloc_chan_resources;
1519 tdma->dma_dev.device_free_chan_resources =
1520 tegra_dma_free_chan_resources;
1521 tdma->dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg;
1522 tdma->dma_dev.device_prep_dma_cyclic = tegra_dma_prep_dma_cyclic;
Paul Walmsley891653a2015-01-06 06:44:56 +00001523 tdma->dma_dev.src_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1524 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1525 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1526 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1527 tdma->dma_dev.dst_addr_widths = BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) |
1528 BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) |
1529 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
1530 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
1531 tdma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
Dmitry Osipenko156a5992019-07-05 18:05:19 +03001532 tdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Maxime Ripard662f1ac2014-11-17 14:42:37 +01001533 tdma->dma_dev.device_config = tegra_dma_slave_config;
1534 tdma->dma_dev.device_terminate_all = tegra_dma_terminate_all;
Dmitry Osipenkodda5e352020-02-09 19:33:40 +03001535 tdma->dma_dev.device_synchronize = tegra_dma_synchronize;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301536 tdma->dma_dev.device_tx_status = tegra_dma_tx_status;
1537 tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending;
1538
1539 ret = dma_async_device_register(&tdma->dma_dev);
1540 if (ret < 0) {
1541 dev_err(&pdev->dev,
1542 "Tegra20 APB DMA driver registration failed %d\n", ret);
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001543 goto err_pm_disable;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301544 }
1545
Stephen Warren996556c2013-11-11 13:09:35 -07001546 ret = of_dma_controller_register(pdev->dev.of_node,
1547 tegra_dma_of_xlate, tdma);
1548 if (ret < 0) {
1549 dev_err(&pdev->dev,
1550 "Tegra20 APB DMA OF registration failed %d\n", ret);
1551 goto err_unregister_dma_dev;
1552 }
1553
Dmitry Osipenko39642932020-02-09 19:33:45 +03001554 dev_info(&pdev->dev, "Tegra20 APB DMA driver registered %u channels\n",
1555 cdata->nr_channels);
1556
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301557 return 0;
1558
Stephen Warren996556c2013-11-11 13:09:35 -07001559err_unregister_dma_dev:
1560 dma_async_device_unregister(&tdma->dma_dev);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001561
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001562err_pm_disable:
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301563 pm_runtime_disable(&pdev->dev);
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001564
1565err_clk_unprepare:
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001566 clk_unprepare(tdma->dma_clk);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001567
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301568 return ret;
1569}
1570
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001571static int tegra_dma_remove(struct platform_device *pdev)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301572{
1573 struct tegra_dma *tdma = platform_get_drvdata(pdev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301574
Dmitry Osipenko16e2b3e2020-02-09 19:33:51 +03001575 of_dma_controller_free(pdev->dev.of_node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301576 dma_async_device_unregister(&tdma->dma_dev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301577 pm_runtime_disable(&pdev->dev);
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001578 clk_unprepare(tdma->dma_clk);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301579
1580 return 0;
1581}
1582
1583static int tegra_dma_runtime_suspend(struct device *dev)
1584{
Jon Hunter286a6442015-11-13 16:39:39 +00001585 struct tegra_dma *tdma = dev_get_drvdata(dev);
Laxman Dewangan3065c192013-04-24 15:24:27 +05301586
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001587 clk_disable(tdma->dma_clk);
Jon Hunter65a5c3d2017-06-06 13:49:29 +01001588
Laxman Dewangan3065c192013-04-24 15:24:27 +05301589 return 0;
1590}
1591
Jon Hunter65a5c3d2017-06-06 13:49:29 +01001592static int tegra_dma_runtime_resume(struct device *dev)
Laxman Dewangan3065c192013-04-24 15:24:27 +05301593{
1594 struct tegra_dma *tdma = dev_get_drvdata(dev);
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001595
1596 return clk_enable(tdma->dma_clk);
1597}
1598
1599static int __maybe_unused tegra_dma_dev_suspend(struct device *dev)
1600{
1601 struct tegra_dma *tdma = dev_get_drvdata(dev);
1602 unsigned long flags;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001603 unsigned int i;
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001604 bool busy;
Laxman Dewangan3065c192013-04-24 15:24:27 +05301605
1606 for (i = 0; i < tdma->chip_data->nr_channels; i++) {
1607 struct tegra_dma_channel *tdc = &tdma->channels[i];
Laxman Dewangan3065c192013-04-24 15:24:27 +05301608
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001609 tasklet_kill(&tdc->tasklet);
Jon Hunter4aad5be2015-11-13 16:39:41 +00001610
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001611 spin_lock_irqsave(&tdc->lock, flags);
1612 busy = tdc->busy;
1613 spin_unlock_irqrestore(&tdc->lock, flags);
1614
1615 if (busy) {
1616 dev_err(tdma->dev, "channel %u busy\n", i);
1617 return -EBUSY;
1618 }
Laxman Dewangan3065c192013-04-24 15:24:27 +05301619 }
1620
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001621 return pm_runtime_force_suspend(dev);
1622}
1623
1624static int __maybe_unused tegra_dma_dev_resume(struct device *dev)
1625{
1626 struct tegra_dma *tdma = dev_get_drvdata(dev);
1627 int err;
1628
1629 err = tegra_dma_init_hw(tdma);
1630 if (err)
1631 return err;
1632
1633 return pm_runtime_force_resume(dev);
Laxman Dewangan3065c192013-04-24 15:24:27 +05301634}
Laxman Dewangan3065c192013-04-24 15:24:27 +05301635
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001636static const struct dev_pm_ops tegra_dma_dev_pm_ops = {
Jon Hunteredd3bdb2015-11-13 16:39:38 +00001637 SET_RUNTIME_PM_OPS(tegra_dma_runtime_suspend, tegra_dma_runtime_resume,
1638 NULL)
Dmitry Osipenkodcb394b2020-02-09 19:33:50 +03001639 SET_SYSTEM_SLEEP_PM_OPS(tegra_dma_dev_suspend, tegra_dma_dev_resume)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301640};
1641
Laxman Dewangan242637b2016-03-04 15:55:11 +05301642static const struct of_device_id tegra_dma_of_match[] = {
1643 {
1644 .compatible = "nvidia,tegra148-apbdma",
1645 .data = &tegra148_dma_chip_data,
1646 }, {
1647 .compatible = "nvidia,tegra114-apbdma",
1648 .data = &tegra114_dma_chip_data,
1649 }, {
1650 .compatible = "nvidia,tegra30-apbdma",
1651 .data = &tegra30_dma_chip_data,
1652 }, {
1653 .compatible = "nvidia,tegra20-apbdma",
1654 .data = &tegra20_dma_chip_data,
1655 }, {
1656 },
1657};
1658MODULE_DEVICE_TABLE(of, tegra_dma_of_match);
1659
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301660static struct platform_driver tegra_dmac_driver = {
1661 .driver = {
Laxman Dewangancd9092c2012-07-02 13:52:08 +05301662 .name = "tegra-apbdma",
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301663 .pm = &tegra_dma_dev_pm_ops,
Stephen Warrendc7badb2013-03-11 16:30:26 -06001664 .of_match_table = tegra_dma_of_match,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301665 },
1666 .probe = tegra_dma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05001667 .remove = tegra_dma_remove,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301668};
1669
1670module_platform_driver(tegra_dmac_driver);
1671
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301672MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
1673MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1674MODULE_LICENSE("GPL v2");