blob: 652b63c85df31c89093bfa37aaf789122059b4d9 [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 /* 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
231static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
232{
233 writel(val, tdma->base_addr + reg);
234}
235
236static inline u32 tdma_read(struct tegra_dma *tdma, u32 reg)
237{
238 return readl(tdma->base_addr + reg);
239}
240
241static inline void tdc_write(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300242 u32 reg, u32 val)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530243{
Jon Hunter13a33282015-08-06 14:32:31 +0100244 writel(val, tdc->chan_addr + reg);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530245}
246
247static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg)
248{
Jon Hunter13a33282015-08-06 14:32:31 +0100249 return readl(tdc->chan_addr + reg);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530250}
251
252static 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 Osipenko39642932020-02-09 19:33:45 +0300257static inline struct tegra_dma_desc *
258txd_to_tegra_dma_desc(struct dma_async_tx_descriptor *td)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530259{
260 return container_of(td, struct tegra_dma_desc, txd);
261}
262
263static inline struct device *tdc2dev(struct tegra_dma_channel *tdc)
264{
265 return &tdc->dma_chan.dev->device;
266}
267
268static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);
269static int tegra_dma_runtime_suspend(struct device *dev);
270static int tegra_dma_runtime_resume(struct device *dev);
271
272/* Get DMA desc from free list, if not there then allocate it. */
Dmitry Osipenko39642932020-02-09 19:33:45 +0300273static struct tegra_dma_desc *tegra_dma_desc_get(struct tegra_dma_channel *tdc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530274{
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 Dewanganb9bb37f2013-01-09 15:26:22 +0530285 dma_desc->txd.flags = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530286 return dma_desc;
287 }
288 }
289
290 spin_unlock_irqrestore(&tdc->lock, flags);
291
292 /* Allocate DMA desc */
Jon Hunter8fe97392015-11-13 16:39:42 +0000293 dma_desc = kzalloc(sizeof(*dma_desc), GFP_NOWAIT);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100294 if (!dma_desc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530295 return NULL;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530296
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 Osipenko39642932020-02-09 19:33:45 +0300300
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530301 return dma_desc;
302}
303
304static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300305 struct tegra_dma_desc *dma_desc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530306{
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 Osipenko39642932020-02-09 19:33:45 +0300316static struct tegra_dma_sg_req *
317tegra_dma_sg_req_get(struct tegra_dma_channel *tdc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530318{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300319 struct tegra_dma_sg_req *sg_req;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530320 unsigned long flags;
321
322 spin_lock_irqsave(&tdc->lock, flags);
323 if (!list_empty(&tdc->free_sg_req)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300324 sg_req = list_first_entry(&tdc->free_sg_req, typeof(*sg_req),
325 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530326 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 Osipenko39642932020-02-09 19:33:45 +0300332 sg_req = kzalloc(sizeof(*sg_req), GFP_NOWAIT);
Peter Griffinaef94fe2016-06-07 18:38:41 +0100333
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530334 return sg_req;
335}
336
337static int tegra_dma_slave_config(struct dma_chan *dc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300338 struct dma_slave_config *sconfig)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530339{
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 Osipenkof6160f32017-11-16 20:11:06 +0300348 if (tdc->slave_id == TEGRA_APBDMA_SLAVE_ID_INVALID &&
349 sconfig->device_fc) {
Shardar Shariff Md00ef4492016-04-23 15:06:00 +0530350 if (sconfig->slave_id > TEGRA_APBDMA_CSR_REQ_SEL_MASK)
351 return -EINVAL;
Stephen Warren996556c2013-11-11 13:09:35 -0700352 tdc->slave_id = sconfig->slave_id;
Shardar Shariff Md00ef4492016-04-23 15:06:00 +0530353 }
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530354 tdc->config_init = true;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300355
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530356 return 0;
357}
358
359static void tegra_dma_global_pause(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300360 bool wait_for_burst_complete)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530361{
362 struct tegra_dma *tdma = tdc->tdma;
363
364 spin_lock(&tdma->global_lock);
Jon Hunter23a1ec32015-08-06 14:32:33 +0100365
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 Dewanganec8a1582012-06-06 10:55:27 +0530375}
376
377static void tegra_dma_global_resume(struct tegra_dma_channel *tdc)
378{
379 struct tegra_dma *tdma = tdc->tdma;
380
Jon Hunter23a1ec32015-08-06 14:32:33 +0100381 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
390out:
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530391 spin_unlock(&tdma->global_lock);
392}
393
Laxman Dewangan1b140902013-01-06 21:52:02 +0530394static void tegra_dma_pause(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300395 bool wait_for_burst_complete)
Laxman Dewangan1b140902013-01-06 21:52:02 +0530396{
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 Osipenko39642932020-02-09 19:33:45 +0300401 TEGRA_APBDMA_CHAN_CSRE_PAUSE);
Laxman Dewangan1b140902013-01-06 21:52:02 +0530402 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
409static void tegra_dma_resume(struct tegra_dma_channel *tdc)
410{
411 struct tegra_dma *tdma = tdc->tdma;
412
Dmitry Osipenko39642932020-02-09 19:33:45 +0300413 if (tdma->chip_data->support_channel_pause)
Laxman Dewangan1b140902013-01-06 21:52:02 +0530414 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, 0);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300415 else
Laxman Dewangan1b140902013-01-06 21:52:02 +0530416 tegra_dma_global_resume(tdc);
Laxman Dewangan1b140902013-01-06 21:52:02 +0530417}
418
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530419static void tegra_dma_stop(struct tegra_dma_channel *tdc)
420{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300421 u32 csr, status;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530422
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
441static void tegra_dma_start(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300442 struct tegra_dma_sg_req *sg_req)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530443{
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 Dewangan911dacc2014-01-06 11:16:45 -0700451 if (tdc->tdma->chip_data->support_separate_wcount_reg)
452 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT, ch_regs->wcount);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530453
454 /* Start DMA */
455 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300456 ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530457}
458
459static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300460 struct tegra_dma_sg_req *nsg_req)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530461{
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 Dewangan1b140902013-01-06 21:52:02 +0530475 tegra_dma_pause(tdc, false);
Thierry Reding7b0e00d2016-06-14 16:18:46 +0200476 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530477
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 Dewangan1b140902013-01-06 21:52:02 +0530485 tegra_dma_resume(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530486 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 Dewangan911dacc2014-01-06 11:16:45 -0700492 if (tdc->tdma->chip_data->support_separate_wcount_reg)
493 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300494 nsg_req->ch_regs.wcount);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530495 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300496 nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530497 nsg_req->configured = true;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300498 nsg_req->words_xferred = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530499
Laxman Dewangan1b140902013-01-06 21:52:02 +0530500 tegra_dma_resume(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530501}
502
503static void tdc_start_head_req(struct tegra_dma_channel *tdc)
504{
505 struct tegra_dma_sg_req *sg_req;
506
Dmitry Osipenko39642932020-02-09 19:33:45 +0300507 sg_req = list_first_entry(&tdc->pending_sg_req, typeof(*sg_req), node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530508 tegra_dma_start(tdc, sg_req);
509 sg_req->configured = true;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300510 sg_req->words_xferred = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530511 tdc->busy = true;
512}
513
514static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
515{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300516 struct tegra_dma_sg_req *hsgreq, *hnsgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530517
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530518 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
519 if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300520 hnsgreq = list_first_entry(&hsgreq->node, typeof(*hnsgreq),
521 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530522 tegra_dma_configure_for_next(tdc, hnsgreq);
523 }
524}
525
Dmitry Osipenko39642932020-02-09 19:33:45 +0300526static inline unsigned int
527get_current_xferred_count(struct tegra_dma_channel *tdc,
528 struct tegra_dma_sg_req *sg_req,
529 unsigned long status)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530530{
531 return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4;
532}
533
534static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
535{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530536 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300537 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530538
539 while (!list_empty(&tdc->pending_sg_req)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300540 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
541 node);
Wei Yongjun2cc44e62012-09-05 15:08:56 +0800542 list_move_tail(&sgreq->node, &tdc->free_sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530543 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 Osipenko39642932020-02-09 19:33:45 +0300551 &tdc->cb_desc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530552 dma_desc->cb_count++;
553 }
554 }
555 tdc->isr_handler = NULL;
556}
557
558static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300559 struct tegra_dma_sg_req *last_sg_req,
560 bool to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530561{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300562 struct tegra_dma_sg_req *hsgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530563
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530564 /*
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 Osipenko84a3f372020-02-09 19:33:49 +0300572 pm_runtime_put(tdc->tdma->dev);
Ben Dooks547b3112018-11-21 16:13:21 +0000573 dev_err(tdc2dev(tdc), "Error in DMA transfer, aborting DMA\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530574 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 Osipenko39642932020-02-09 19:33:45 +0300581
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530582 return true;
583}
584
585static void handle_once_dma_done(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300586 bool to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530587{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530588 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300589 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530590
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 Koul00d696f2013-10-16 21:04:50 +0530598 dma_desc->dma_status = DMA_COMPLETE;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530599 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 Osipenko84a3f372020-02-09 19:33:49 +0300608 if (to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530609 return;
610
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300611 if (list_empty(&tdc->pending_sg_req)) {
612 pm_runtime_put(tdc->tdma->dev);
613 return;
614 }
615
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530616 tdc_start_head_req(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530617}
618
619static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300620 bool to_terminate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530621{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530622 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300623 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530624 bool st;
625
626 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
627 dma_desc = sgreq->dma_desc;
Ben Dookse486df32018-11-21 16:13:19 +0000628 /* 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 Dewanganec8a1582012-06-06 10:55:27 +0530632
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 Osipenko156a5992019-07-05 18:05:19 +0300638 sgreq->words_xferred = 0;
639
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530640 /* If not last req then put at end of pending list */
641 if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
Wei Yongjun2cc44e62012-09-05 15:08:56 +0800642 list_move_tail(&sgreq->node, &tdc->pending_sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530643 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 Dewanganec8a1582012-06-06 10:55:27 +0530648}
649
650static void tegra_dma_tasklet(unsigned long data)
651{
652 struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data;
Dave Jiang370c0442016-07-20 13:13:16 -0700653 struct dmaengine_desc_callback cb;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530654 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300655 unsigned int cb_count;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530656 unsigned long flags;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530657
658 spin_lock_irqsave(&tdc->lock, flags);
659 while (!list_empty(&tdc->cb_desc)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300660 dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
661 cb_node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530662 list_del(&dma_desc->cb_node);
Dave Jiang370c0442016-07-20 13:13:16 -0700663 dmaengine_desc_get_callback(&dma_desc->txd, &cb);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530664 cb_count = dma_desc->cb_count;
665 dma_desc->cb_count = 0;
Ben Dooks95f295f2018-11-21 16:13:23 +0000666 trace_tegra_dma_complete_cb(&tdc->dma_chan, cb_count,
667 cb.callback);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530668 spin_unlock_irqrestore(&tdc->lock, flags);
Dave Jiang370c0442016-07-20 13:13:16 -0700669 while (cb_count--)
670 dmaengine_desc_callback_invoke(&cb, NULL);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530671 spin_lock_irqsave(&tdc->lock, flags);
672 }
673 spin_unlock_irqrestore(&tdc->lock, flags);
674}
675
676static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
677{
678 struct tegra_dma_channel *tdc = dev_id;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530679 unsigned long flags;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300680 u32 status;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530681
682 spin_lock_irqsave(&tdc->lock, flags);
683
Ben Dooks95f295f2018-11-21 16:13:23 +0000684 trace_tegra_dma_isr(&tdc->dma_chan, irq);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530685 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 Osipenko39642932020-02-09 19:33:45 +0300695 dev_info(tdc2dev(tdc), "Interrupt already served status 0x%08x\n",
696 status);
697
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530698 return IRQ_NONE;
699}
700
701static 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 Osipenko39642932020-02-09 19:33:45 +0300713
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530714 return cookie;
715}
716
717static 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 Osipenko84a3f372020-02-09 19:33:49 +0300721 int err;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530722
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 Osipenko84a3f372020-02-09 19:33:49 +0300729 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 Dewanganec8a1582012-06-06 10:55:27 +0530735 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 }
747end:
748 spin_unlock_irqrestore(&tdc->lock, flags);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530749}
750
Vinod Koula7c439a2014-12-08 11:30:17 +0530751static int tegra_dma_terminate_all(struct dma_chan *dc)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530752{
753 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530754 struct tegra_dma_desc *dma_desc;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300755 struct tegra_dma_sg_req *sgreq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530756 unsigned long flags;
Dmitry Osipenko39642932020-02-09 19:33:45 +0300757 u32 status, wcount;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530758 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 Koula7c439a2014-12-08 11:30:17 +0530763 return 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530764 }
765
766 if (!tdc->busy)
767 goto skip_dma_stop;
768
769 /* Pause DMA before checking the queue status */
Laxman Dewangan1b140902013-01-06 21:52:02 +0530770 tegra_dma_pause(tdc, true);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530771
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 Dewangan911dacc2014-01-06 11:16:45 -0700778 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 Dewanganec8a1582012-06-06 10:55:27 +0530782
783 was_busy = tdc->busy;
784 tegra_dma_stop(tdc);
785
786 if (!list_empty(&tdc->pending_sg_req) && was_busy) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300787 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
788 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530789 sgreq->dma_desc->bytes_transferred +=
Laxman Dewangan911dacc2014-01-06 11:16:45 -0700790 get_current_xferred_count(tdc, sgreq, wcount);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530791 }
Laxman Dewangan1b140902013-01-06 21:52:02 +0530792 tegra_dma_resume(tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530793
Dmitry Osipenko84a3f372020-02-09 19:33:49 +0300794 pm_runtime_put(tdc->tdma->dev);
795
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530796skip_dma_stop:
797 tegra_dma_abort_all(tdc);
798
799 while (!list_empty(&tdc->cb_desc)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +0300800 dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
801 cb_node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530802 list_del(&dma_desc->cb_node);
803 dma_desc->cb_count = 0;
804 }
805 spin_unlock_irqrestore(&tdc->lock, flags);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300806
Vinod Koula7c439a2014-12-08 11:30:17 +0530807 return 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530808}
809
Dmitry Osipenkodda5e352020-02-09 19:33:40 +0300810static 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 Osipenko156a5992019-07-05 18:05:19 +0300817static unsigned int tegra_dma_sg_bytes_xferred(struct tegra_dma_channel *tdc,
818 struct tegra_dma_sg_req *sg_req)
819{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300820 u32 status, wcount = 0;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300821
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 Dewanganec8a1582012-06-06 10:55:27 +0530876static enum dma_status tegra_dma_tx_status(struct dma_chan *dc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300877 dma_cookie_t cookie,
878 struct dma_tx_state *txstate)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530879{
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 Dewangan4a46ba32012-07-02 13:52:07 +0530885 unsigned int residual;
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300886 unsigned int bytes = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530887
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530888 ret = dma_cookie_status(dc, cookie, txstate);
Jon Hunterd3183442016-06-29 17:08:39 +0100889 if (ret == DMA_COMPLETE)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530890 return ret;
Andy Shevchenko0a0aee22013-05-27 15:14:39 +0300891
892 spin_lock_irqsave(&tdc->lock, flags);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530893
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 Dewanganec8a1582012-06-06 10:55:27 +0530897 ret = dma_desc->dma_status;
Jon Hunter004f6142016-06-29 17:08:38 +0100898 goto found;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530899 }
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 Osipenko156a5992019-07-05 18:05:19 +0300906 bytes = tegra_dma_sg_bytes_xferred(tdc, sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530907 ret = dma_desc->dma_status;
Jon Hunter004f6142016-06-29 17:08:38 +0100908 goto found;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530909 }
910 }
911
Jon Hunter019bfcc2016-06-29 17:08:37 +0100912 dev_dbg(tdc2dev(tdc), "cookie %d not found\n", cookie);
Jon Hunter004f6142016-06-29 17:08:38 +0100913 dma_desc = NULL;
914
915found:
Jon Hunterd3183442016-06-29 17:08:39 +0100916 if (dma_desc && txstate) {
Jon Hunter004f6142016-06-29 17:08:38 +0100917 residual = dma_desc->bytes_requested -
Dmitry Osipenko156a5992019-07-05 18:05:19 +0300918 ((dma_desc->bytes_transferred + bytes) %
Jon Hunter004f6142016-06-29 17:08:38 +0100919 dma_desc->bytes_requested);
920 dma_set_residue(txstate, residual);
921 }
922
Ben Dooks95f295f2018-11-21 16:13:23 +0000923 trace_tegra_dma_tx_status(&tdc->dma_chan, cookie, txstate);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530924 spin_unlock_irqrestore(&tdc->lock, flags);
Dmitry Osipenko39642932020-02-09 19:33:45 +0300925
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530926 return ret;
927}
928
Dmitry Osipenko39642932020-02-09 19:33:45 +0300929static inline unsigned int get_bus_width(struct tegra_dma_channel *tdc,
930 enum dma_slave_buswidth slave_bw)
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530931{
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 Osipenko39642932020-02-09 19:33:45 +0300943 "slave bw is not supported, using 32bits\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530944 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
945 }
946}
947
Dmitry Osipenko39642932020-02-09 19:33:45 +0300948static 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 Dewanganec8a1582012-06-06 10:55:27 +0530952{
Dmitry Osipenko39642932020-02-09 19:33:45 +0300953 unsigned int burst_byte, burst_ahb_width;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530954
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
979static int get_transfer_param(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +0300980 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 Dewanganec8a1582012-06-06 10:55:27 +0530986{
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530987 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 Dooks547b3112018-11-21 16:13:21 +00001005 dev_err(tdc2dev(tdc), "DMA direction is not supported\n");
Dmitry Osipenko39642932020-02-09 19:33:45 +03001006 break;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301007 }
Dmitry Osipenko39642932020-02-09 19:33:45 +03001008
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301009 return -EINVAL;
1010}
1011
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001012static void tegra_dma_prep_wcount(struct tegra_dma_channel *tdc,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001013 struct tegra_dma_channel_regs *ch_regs,
1014 u32 len)
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001015{
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 Osipenko39642932020-02-09 19:33:45 +03001024static struct dma_async_tx_descriptor *
1025tegra_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 Dewanganec8a1582012-06-06 10:55:27 +05301031{
1032 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001033 struct tegra_dma_sg_req *sg_req = NULL;
1034 u32 csr, ahb_seq, apb_ptr, apb_seq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301035 enum dma_slave_buswidth slave_bw;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001036 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 Dewanganec8a1582012-06-06 10:55:27 +05301041
1042 if (!tdc->config_init) {
Ben Dooks547b3112018-11-21 16:13:21 +00001043 dev_err(tdc2dev(tdc), "DMA channel is not configured\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301044 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 Hunterdc1ff4b2015-08-06 14:32:32 +01001051 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001052 &burst_size, &slave_bw) < 0)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301053 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 Osipenkof6160f32017-11-16 20:11:06 +03001062 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 Osipenkodc161062019-05-30 00:43:55 +03001069 if (flags & DMA_PREP_INTERRUPT) {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301070 csr |= TEGRA_APBDMA_CSR_IE_EOC;
Dmitry Osipenkodc161062019-05-30 00:43:55 +03001071 } else {
1072 WARN_ON_ONCE(1);
1073 return NULL;
1074 }
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301075
1076 apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
1077
1078 dma_desc = tegra_dma_desc_get(tdc);
1079 if (!dma_desc) {
Ben Dooks547b3112018-11-21 16:13:21 +00001080 dev_err(tdc2dev(tdc), "DMA descriptors not available\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301081 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 Dewangan597c8542012-06-22 20:41:10 +05301094 mem = sg_dma_address(sg);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301095 len = sg_dma_len(sg);
1096
1097 if ((len & 3) || (mem & 3) ||
Dmitry Osipenko39642932020-02-09 19:33:45 +03001098 len > tdc->tdma->chip_data->max_dma_count) {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301099 dev_err(tdc2dev(tdc),
Ben Dooks547b3112018-11-21 16:13:21 +00001100 "DMA length/memory address is not supported\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301101 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 Dooks547b3112018-11-21 16:13:21 +00001107 dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301108 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 Dewangan911dacc2014-01-06 11:16:45 -07001117 sg_req->ch_regs.csr = csr;
1118 tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301119 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 Osipenko39642932020-02-09 19:33:45 +03001150static struct dma_async_tx_descriptor *
1151tegra_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 Dewanganec8a1582012-06-06 10:55:27 +05301156{
1157 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Thierry Reding7b0e00d2016-06-14 16:18:46 +02001158 struct tegra_dma_sg_req *sg_req = NULL;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001159 u32 csr, ahb_seq, apb_ptr, apb_seq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301160 enum dma_slave_buswidth slave_bw;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001161 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 Dewanganec8a1582012-06-06 10:55:27 +05301165
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 Dooks547b3112018-11-21 16:13:21 +00001183 dev_err(tdc2dev(tdc), "Request not allowed when DMA running\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301184 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 Osipenko39642932020-02-09 19:33:45 +03001198 len > tdc->tdma->chip_data->max_dma_count) {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301199 dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n");
1200 return NULL;
1201 }
1202
Jon Hunterdc1ff4b2015-08-06 14:32:32 +01001203 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001204 &burst_size, &slave_bw) < 0)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301205 return NULL;
1206
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301207 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 Osipenkof6160f32017-11-16 20:11:06 +03001212 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 Osipenkodc161062019-05-30 00:43:55 +03001217 if (flags & DMA_PREP_INTERRUPT) {
Laxman Dewanganb9bb37f2013-01-09 15:26:22 +05301218 csr |= TEGRA_APBDMA_CSR_IE_EOC;
Dmitry Osipenkodc161062019-05-30 00:43:55 +03001219 } else {
1220 WARN_ON_ONCE(1);
1221 return NULL;
1222 }
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301223
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 Dooks547b3112018-11-21 16:13:21 +00001244 dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301245 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 Dewangan911dacc2014-01-06 11:16:45 -07001252 sg_req->ch_regs.csr = csr;
1253 tegra_dma_prep_wcount(tdc, &sg_req->ch_regs, len);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301254 sg_req->ch_regs.apb_seq = apb_seq;
1255 sg_req->ch_regs.ahb_seq = ahb_seq;
1256 sg_req->configured = false;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301257 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 Dewanganb9bb37f2013-01-09 15:26:22 +05301266 if (flags & DMA_CTRL_ACK)
1267 dma_desc->txd.flags = DMA_CTRL_ACK;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301268
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
1287static 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 Hunteredd3bdb2015-11-13 16:39:38 +00001292
Jon Hunteredd3bdb2015-11-13 16:39:38 +00001293 return 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301294}
1295
1296static void tegra_dma_free_chan_resources(struct dma_chan *dc)
1297{
1298 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301299 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 Dewanganec8a1582012-06-06 10:55:27 +05301303
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 Osipenko8e841722020-02-09 19:33:41 +03001309 tegra_dma_terminate_all(dc);
Dmitry Osipenko41ffc422020-02-09 19:33:42 +03001310 tasklet_kill(&tdc->tasklet);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301311
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301312 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 Osipenko7bdc1e22013-05-11 20:30:53 +04001317 tdc->isr_handler = NULL;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301318
1319 while (!list_empty(&dma_desc_list)) {
Dmitry Osipenko39642932020-02-09 19:33:45 +03001320 dma_desc = list_first_entry(&dma_desc_list, typeof(*dma_desc),
1321 node);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301322 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 Warren996556c2013-11-11 13:09:35 -07001331
Shardar Shariff Md00ef4492016-04-23 15:06:00 +05301332 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
Stephen Warren996556c2013-11-11 13:09:35 -07001333}
1334
1335static 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 Warren996556c2013-11-11 13:09:35 -07001339 struct tegra_dma_channel *tdc;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001340 struct dma_chan *chan;
Stephen Warren996556c2013-11-11 13:09:35 -07001341
Shardar Shariff Md00ef4492016-04-23 15:06:00 +05301342 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 Warren996556c2013-11-11 13:09:35 -07001347 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 Dewanganec8a1582012-06-06 10:55:27 +05301355}
1356
1357/* Tegra20 specific DMA controller information */
Laxman Dewangan75f21632012-08-29 10:31:18 +02001358static const struct tegra_dma_chip_data tegra20_dma_chip_data = {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301359 .nr_channels = 16,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001360 .channel_reg_size = 0x20,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301361 .max_dma_count = 1024UL * 64,
Laxman Dewangan1b140902013-01-06 21:52:02 +05301362 .support_channel_pause = false,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001363 .support_separate_wcount_reg = false,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301364};
1365
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301366/* Tegra30 specific DMA controller information */
Laxman Dewangan75f21632012-08-29 10:31:18 +02001367static const struct tegra_dma_chip_data tegra30_dma_chip_data = {
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301368 .nr_channels = 32,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001369 .channel_reg_size = 0x20,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301370 .max_dma_count = 1024UL * 64,
Laxman Dewangan1b140902013-01-06 21:52:02 +05301371 .support_channel_pause = false,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001372 .support_separate_wcount_reg = false,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301373};
1374
Laxman Dewangan5ea7caf2013-01-06 21:52:03 +05301375/* Tegra114 specific DMA controller information */
1376static const struct tegra_dma_chip_data tegra114_dma_chip_data = {
1377 .nr_channels = 32,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001378 .channel_reg_size = 0x20,
Laxman Dewangan5ea7caf2013-01-06 21:52:03 +05301379 .max_dma_count = 1024UL * 64,
1380 .support_channel_pause = true,
Laxman Dewangan911dacc2014-01-06 11:16:45 -07001381 .support_separate_wcount_reg = false,
1382};
1383
1384/* Tegra148 specific DMA controller information */
1385static 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 Dewangan5ea7caf2013-01-06 21:52:03 +05301391};
1392
Bill Pemberton463a1f82012-11-19 13:22:55 -05001393static int tegra_dma_probe(struct platform_device *pdev)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301394{
Laxman Dewangan333f16e2016-03-01 18:54:40 +05301395 const struct tegra_dma_chip_data *cdata;
Dmitry Osipenko39642932020-02-09 19:33:45 +03001396 struct tegra_dma *tdma;
1397 unsigned int i;
1398 size_t size;
1399 int ret;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301400
Laxman Dewangan333f16e2016-03-01 18:54:40 +05301401 cdata = of_device_get_match_data(&pdev->dev);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001402 size = struct_size(tdma, channels, cdata->nr_channels);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301403
Dmitry Osipenko39642932020-02-09 19:33:45 +03001404 tdma = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
Peter Griffinaef94fe2016-06-07 18:38:41 +01001405 if (!tdma)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301406 return -ENOMEM;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301407
1408 tdma->dev = &pdev->dev;
1409 tdma->chip_data = cdata;
1410 platform_set_drvdata(pdev, tdma);
1411
Dmitry Osipenkoc55c745e2020-02-09 19:33:43 +03001412 tdma->base_addr = devm_platform_ioremap_resource(pdev, 0);
Thierry Reding73312052013-01-21 11:09:00 +01001413 if (IS_ERR(tdma->base_addr))
1414 return PTR_ERR(tdma->base_addr);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301415
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 Warren9aa433d2013-11-06 16:35:34 -07001422 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 Dewanganec8a1582012-06-06 10:55:27 +05301428 spin_lock_init(&tdma->global_lock);
1429
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001430 ret = clk_prepare(tdma->dma_clk);
1431 if (ret)
1432 return ret;
1433
1434 pm_runtime_irq_safe(&pdev->dev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301435 pm_runtime_enable(&pdev->dev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301436
Dmitry Osipenkoa75013a52020-02-09 19:33:47 +03001437 ret = pm_runtime_get_sync(&pdev->dev);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001438 if (ret < 0)
1439 goto err_pm_disable;
Laxman Dewanganffc49302012-07-20 13:31:08 +05301440
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301441 /* Reset DMA controller */
Stephen Warren9aa433d2013-11-06 16:35:34 -07001442 reset_control_assert(tdma->rst);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301443 udelay(2);
Stephen Warren9aa433d2013-11-06 16:35:34 -07001444 reset_control_deassert(tdma->rst);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301445
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 Hunteredd3bdb2015-11-13 16:39:38 +00001451 pm_runtime_put(&pdev->dev);
Laxman Dewanganffc49302012-07-20 13:31:08 +05301452
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301453 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 Osipenko2cd3d132020-02-09 19:33:44 +03001456 int irq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301457
Jon Hunter13a33282015-08-06 14:32:31 +01001458 tdc->chan_addr = tdma->base_addr +
1459 TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
1460 (i * cdata->channel_reg_size);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301461
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001462 irq = platform_get_irq(pdev, i);
1463 if (irq < 0) {
1464 ret = irq;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301465 dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001466 goto err_pm_disable;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301467 }
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001468
Laxman Dewangand0fc9052012-10-03 22:48:07 +05301469 snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i);
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001470 ret = devm_request_irq(&pdev->dev, irq, tegra_dma_isr, 0,
1471 tdc->name, tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301472 if (ret) {
1473 dev_err(&pdev->dev,
1474 "request_irq failed with err %d channel %d\n",
Dmitry Osipenkoac7ae752013-05-11 20:30:52 +04001475 ret, i);
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001476 goto err_pm_disable;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301477 }
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 Osipenko39642932020-02-09 19:33:45 +03001482 &tdma->dma_dev.channels);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301483 tdc->tdma = tdma;
1484 tdc->id = i;
Shardar Shariff Md00ef4492016-04-23 15:06:00 +05301485 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301486
1487 tasklet_init(&tdc->tasklet, tegra_dma_tasklet,
Dmitry Osipenko39642932020-02-09 19:33:45 +03001488 (unsigned long)tdc);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301489 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 Dewangan46fb3f82012-06-22 17:12:43 +05301499 dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
1500
Jon Hunter23a1ec32015-08-06 14:32:33 +01001501 tdma->global_pause_count = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301502 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 Walmsley891653a2015-01-06 06:44:56 +00001509 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 Osipenko156a5992019-07-05 18:05:19 +03001518 tdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
Maxime Ripard662f1ac2014-11-17 14:42:37 +01001519 tdma->dma_dev.device_config = tegra_dma_slave_config;
1520 tdma->dma_dev.device_terminate_all = tegra_dma_terminate_all;
Dmitry Osipenkodda5e352020-02-09 19:33:40 +03001521 tdma->dma_dev.device_synchronize = tegra_dma_synchronize;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301522 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 Osipenko2cd3d132020-02-09 19:33:44 +03001529 goto err_pm_disable;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301530 }
1531
Stephen Warren996556c2013-11-11 13:09:35 -07001532 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 Osipenko39642932020-02-09 19:33:45 +03001540 dev_info(&pdev->dev, "Tegra20 APB DMA driver registered %u channels\n",
1541 cdata->nr_channels);
1542
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301543 return 0;
1544
Stephen Warren996556c2013-11-11 13:09:35 -07001545err_unregister_dma_dev:
1546 dma_async_device_unregister(&tdma->dma_dev);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001547
Dmitry Osipenko2cd3d132020-02-09 19:33:44 +03001548err_pm_disable:
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301549 pm_runtime_disable(&pdev->dev);
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001550 clk_unprepare(tdma->dma_clk);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001551
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301552 return ret;
1553}
1554
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001555static int tegra_dma_remove(struct platform_device *pdev)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301556{
1557 struct tegra_dma *tdma = platform_get_drvdata(pdev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301558
1559 dma_async_device_unregister(&tdma->dma_dev);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301560 pm_runtime_disable(&pdev->dev);
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001561 clk_unprepare(tdma->dma_clk);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301562
1563 return 0;
1564}
1565
1566static int tegra_dma_runtime_suspend(struct device *dev)
1567{
Jon Hunter286a6442015-11-13 16:39:39 +00001568 struct tegra_dma *tdma = dev_get_drvdata(dev);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001569 unsigned int i;
Laxman Dewangan3065c192013-04-24 15:24:27 +05301570
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 Hunter4aad5be2015-11-13 16:39:41 +00001576 /* Only save the state of DMA channels that are in use */
1577 if (!tdc->config_init)
1578 continue;
1579
Laxman Dewangan3065c192013-04-24 15:24:27 +05301580 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 Hunter68ae7a92015-11-13 16:39:40 +00001585 if (tdma->chip_data->support_separate_wcount_reg)
1586 ch_reg->wcount = tdc_read(tdc,
1587 TEGRA_APBDMA_CHAN_WCOUNT);
Laxman Dewangan3065c192013-04-24 15:24:27 +05301588 }
1589
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001590 clk_disable(tdma->dma_clk);
Jon Hunter65a5c3d2017-06-06 13:49:29 +01001591
Laxman Dewangan3065c192013-04-24 15:24:27 +05301592 return 0;
1593}
1594
Jon Hunter65a5c3d2017-06-06 13:49:29 +01001595static int tegra_dma_runtime_resume(struct device *dev)
Laxman Dewangan3065c192013-04-24 15:24:27 +05301596{
1597 struct tegra_dma *tdma = dev_get_drvdata(dev);
Dmitry Osipenko39642932020-02-09 19:33:45 +03001598 unsigned int i;
1599 int ret;
Laxman Dewangan3065c192013-04-24 15:24:27 +05301600
Dmitry Osipenko84a3f372020-02-09 19:33:49 +03001601 ret = clk_enable(tdma->dma_clk);
Jon Hunter65a5c3d2017-06-06 13:49:29 +01001602 if (ret < 0) {
1603 dev_err(dev, "clk_enable failed: %d\n", ret);
Laxman Dewangan3065c192013-04-24 15:24:27 +05301604 return ret;
Jon Hunter65a5c3d2017-06-06 13:49:29 +01001605 }
Laxman Dewangan3065c192013-04-24 15:24:27 +05301606
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 Hunter4aad5be2015-11-13 16:39:41 +00001615 /* Only restore the state of DMA channels that are in use */
1616 if (!tdc->config_init)
1617 continue;
1618
Jon Hunter68ae7a92015-11-13 16:39:40 +00001619 if (tdma->chip_data->support_separate_wcount_reg)
1620 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT,
1621 ch_reg->wcount);
Laxman Dewangan3065c192013-04-24 15:24:27 +05301622 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 Osipenko39642932020-02-09 19:33:45 +03001627 ch_reg->csr & ~TEGRA_APBDMA_CSR_ENB);
Laxman Dewangan3065c192013-04-24 15:24:27 +05301628 }
1629
Laxman Dewangan3065c192013-04-24 15:24:27 +05301630 return 0;
1631}
Laxman Dewangan3065c192013-04-24 15:24:27 +05301632
Greg Kroah-Hartman4bf27b82012-12-21 15:09:59 -08001633static const struct dev_pm_ops tegra_dma_dev_pm_ops = {
Jon Hunteredd3bdb2015-11-13 16:39:38 +00001634 SET_RUNTIME_PM_OPS(tegra_dma_runtime_suspend, tegra_dma_runtime_resume,
1635 NULL)
Jon Hunter65a5c3d2017-06-06 13:49:29 +01001636 SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1637 pm_runtime_force_resume)
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301638};
1639
Laxman Dewangan242637b2016-03-04 15:55:11 +05301640static 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};
1656MODULE_DEVICE_TABLE(of, tegra_dma_of_match);
1657
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301658static struct platform_driver tegra_dmac_driver = {
1659 .driver = {
Laxman Dewangancd9092c2012-07-02 13:52:08 +05301660 .name = "tegra-apbdma",
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301661 .pm = &tegra_dma_dev_pm_ops,
Stephen Warrendc7badb2013-03-11 16:30:26 -06001662 .of_match_table = tegra_dma_of_match,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301663 },
1664 .probe = tegra_dma_probe,
Bill Pembertona7d6e3e2012-11-19 13:20:04 -05001665 .remove = tegra_dma_remove,
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301666};
1667
1668module_platform_driver(tegra_dmac_driver);
1669
1670MODULE_ALIAS("platform:tegra20-apbdma");
1671MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
1672MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1673MODULE_LICENSE("GPL v2");