blob: 6ed3f43be41ebbb3f1c2cb14ddb2a7232110e0c8 [file] [log] [blame]
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301/*
2 * DMA driver for Nvidia's Tegra20 APB DMA controller.
3 *
4 * Copyright (c) 2012, NVIDIA CORPORATION. All rights reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify it
7 * under the terms and conditions of the GNU General Public License,
8 * version 2, as published by the Free Software Foundation.
9 *
10 * This program is distributed in the hope it will be useful, but WITHOUT
11 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13 * more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
17 */
18
19#include <linux/bitops.h>
20#include <linux/clk.h>
21#include <linux/delay.h>
22#include <linux/dmaengine.h>
23#include <linux/dma-mapping.h>
24#include <linux/init.h>
25#include <linux/interrupt.h>
26#include <linux/io.h>
27#include <linux/mm.h>
28#include <linux/module.h>
29#include <linux/of.h>
30#include <linux/of_device.h>
31#include <linux/platform_device.h>
32#include <linux/pm_runtime.h>
33#include <linux/slab.h>
34
35#include <mach/clk.h>
36#include "dmaengine.h"
37
38#define TEGRA_APBDMA_GENERAL 0x0
39#define TEGRA_APBDMA_GENERAL_ENABLE BIT(31)
40
41#define TEGRA_APBDMA_CONTROL 0x010
42#define TEGRA_APBDMA_IRQ_MASK 0x01c
43#define TEGRA_APBDMA_IRQ_MASK_SET 0x020
44
45/* CSR register */
46#define TEGRA_APBDMA_CHAN_CSR 0x00
47#define TEGRA_APBDMA_CSR_ENB BIT(31)
48#define TEGRA_APBDMA_CSR_IE_EOC BIT(30)
49#define TEGRA_APBDMA_CSR_HOLD BIT(29)
50#define TEGRA_APBDMA_CSR_DIR BIT(28)
51#define TEGRA_APBDMA_CSR_ONCE BIT(27)
52#define TEGRA_APBDMA_CSR_FLOW BIT(21)
53#define TEGRA_APBDMA_CSR_REQ_SEL_SHIFT 16
54#define TEGRA_APBDMA_CSR_WCOUNT_MASK 0xFFFC
55
56/* STATUS register */
57#define TEGRA_APBDMA_CHAN_STATUS 0x004
58#define TEGRA_APBDMA_STATUS_BUSY BIT(31)
59#define TEGRA_APBDMA_STATUS_ISE_EOC BIT(30)
60#define TEGRA_APBDMA_STATUS_HALT BIT(29)
61#define TEGRA_APBDMA_STATUS_PING_PONG BIT(28)
62#define TEGRA_APBDMA_STATUS_COUNT_SHIFT 2
63#define TEGRA_APBDMA_STATUS_COUNT_MASK 0xFFFC
64
65/* AHB memory address */
66#define TEGRA_APBDMA_CHAN_AHBPTR 0x010
67
68/* AHB sequence register */
69#define TEGRA_APBDMA_CHAN_AHBSEQ 0x14
70#define TEGRA_APBDMA_AHBSEQ_INTR_ENB BIT(31)
71#define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_8 (0 << 28)
72#define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_16 (1 << 28)
73#define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32 (2 << 28)
74#define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_64 (3 << 28)
75#define TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_128 (4 << 28)
76#define TEGRA_APBDMA_AHBSEQ_DATA_SWAP BIT(27)
77#define TEGRA_APBDMA_AHBSEQ_BURST_1 (4 << 24)
78#define TEGRA_APBDMA_AHBSEQ_BURST_4 (5 << 24)
79#define TEGRA_APBDMA_AHBSEQ_BURST_8 (6 << 24)
80#define TEGRA_APBDMA_AHBSEQ_DBL_BUF BIT(19)
81#define TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT 16
82#define TEGRA_APBDMA_AHBSEQ_WRAP_NONE 0
83
84/* APB address */
85#define TEGRA_APBDMA_CHAN_APBPTR 0x018
86
87/* APB sequence register */
88#define TEGRA_APBDMA_CHAN_APBSEQ 0x01c
89#define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8 (0 << 28)
90#define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16 (1 << 28)
91#define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32 (2 << 28)
92#define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64 (3 << 28)
93#define TEGRA_APBDMA_APBSEQ_BUS_WIDTH_128 (4 << 28)
94#define TEGRA_APBDMA_APBSEQ_DATA_SWAP BIT(27)
95#define TEGRA_APBDMA_APBSEQ_WRAP_WORD_1 (1 << 16)
96
97/*
98 * If any burst is in flight and DMA paused then this is the time to complete
99 * on-flight burst and update DMA status register.
100 */
101#define TEGRA_APBDMA_BURST_COMPLETE_TIME 20
102
103/* Channel base address offset from APBDMA base address */
104#define TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET 0x1000
105
106/* DMA channel register space size */
107#define TEGRA_APBDMA_CHANNEL_REGISTER_SIZE 0x20
108
109struct tegra_dma;
110
111/*
112 * tegra_dma_chip_data Tegra chip specific DMA data
113 * @nr_channels: Number of channels available in the controller.
114 * @max_dma_count: Maximum DMA transfer count supported by DMA controller.
115 */
116struct tegra_dma_chip_data {
117 int nr_channels;
118 int max_dma_count;
119};
120
121/* DMA channel registers */
122struct tegra_dma_channel_regs {
123 unsigned long csr;
124 unsigned long ahb_ptr;
125 unsigned long apb_ptr;
126 unsigned long ahb_seq;
127 unsigned long apb_seq;
128};
129
130/*
131 * tegra_dma_sg_req: Dma request details to configure hardware. This
132 * contains the details for one transfer to configure DMA hw.
133 * The client's request for data transfer can be broken into multiple
134 * sub-transfer as per requester details and hw support.
135 * This sub transfer get added in the list of transfer and point to Tegra
136 * DMA descriptor which manages the transfer details.
137 */
138struct tegra_dma_sg_req {
139 struct tegra_dma_channel_regs ch_regs;
140 int req_len;
141 bool configured;
142 bool last_sg;
143 bool half_done;
144 struct list_head node;
145 struct tegra_dma_desc *dma_desc;
146};
147
148/*
149 * tegra_dma_desc: Tegra DMA descriptors which manages the client requests.
150 * This descriptor keep track of transfer status, callbacks and request
151 * counts etc.
152 */
153struct tegra_dma_desc {
154 struct dma_async_tx_descriptor txd;
155 int bytes_requested;
156 int bytes_transferred;
157 enum dma_status dma_status;
158 struct list_head node;
159 struct list_head tx_list;
160 struct list_head cb_node;
161 int cb_count;
162};
163
164struct tegra_dma_channel;
165
166typedef void (*dma_isr_handler)(struct tegra_dma_channel *tdc,
167 bool to_terminate);
168
169/* tegra_dma_channel: Channel specific information */
170struct tegra_dma_channel {
171 struct dma_chan dma_chan;
172 bool config_init;
173 int id;
174 int irq;
175 unsigned long chan_base_offset;
176 spinlock_t lock;
177 bool busy;
178 struct tegra_dma *tdma;
179 bool cyclic;
180
181 /* Different lists for managing the requests */
182 struct list_head free_sg_req;
183 struct list_head pending_sg_req;
184 struct list_head free_dma_desc;
185 struct list_head cb_desc;
186
187 /* ISR handler and tasklet for bottom half of isr handling */
188 dma_isr_handler isr_handler;
189 struct tasklet_struct tasklet;
190 dma_async_tx_callback callback;
191 void *callback_param;
192
193 /* Channel-slave specific configuration */
194 struct dma_slave_config dma_sconfig;
195};
196
197/* tegra_dma: Tegra DMA specific information */
198struct tegra_dma {
199 struct dma_device dma_dev;
200 struct device *dev;
201 struct clk *dma_clk;
202 spinlock_t global_lock;
203 void __iomem *base_addr;
204 struct tegra_dma_chip_data *chip_data;
205
206 /* Some register need to be cache before suspend */
207 u32 reg_gen;
208
209 /* Last member of the structure */
210 struct tegra_dma_channel channels[0];
211};
212
213static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
214{
215 writel(val, tdma->base_addr + reg);
216}
217
218static inline u32 tdma_read(struct tegra_dma *tdma, u32 reg)
219{
220 return readl(tdma->base_addr + reg);
221}
222
223static inline void tdc_write(struct tegra_dma_channel *tdc,
224 u32 reg, u32 val)
225{
226 writel(val, tdc->tdma->base_addr + tdc->chan_base_offset + reg);
227}
228
229static inline u32 tdc_read(struct tegra_dma_channel *tdc, u32 reg)
230{
231 return readl(tdc->tdma->base_addr + tdc->chan_base_offset + reg);
232}
233
234static inline struct tegra_dma_channel *to_tegra_dma_chan(struct dma_chan *dc)
235{
236 return container_of(dc, struct tegra_dma_channel, dma_chan);
237}
238
239static inline struct tegra_dma_desc *txd_to_tegra_dma_desc(
240 struct dma_async_tx_descriptor *td)
241{
242 return container_of(td, struct tegra_dma_desc, txd);
243}
244
245static inline struct device *tdc2dev(struct tegra_dma_channel *tdc)
246{
247 return &tdc->dma_chan.dev->device;
248}
249
250static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);
251static int tegra_dma_runtime_suspend(struct device *dev);
252static int tegra_dma_runtime_resume(struct device *dev);
253
254/* Get DMA desc from free list, if not there then allocate it. */
255static struct tegra_dma_desc *tegra_dma_desc_get(
256 struct tegra_dma_channel *tdc)
257{
258 struct tegra_dma_desc *dma_desc;
259 unsigned long flags;
260
261 spin_lock_irqsave(&tdc->lock, flags);
262
263 /* Do not allocate if desc are waiting for ack */
264 list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
265 if (async_tx_test_ack(&dma_desc->txd)) {
266 list_del(&dma_desc->node);
267 spin_unlock_irqrestore(&tdc->lock, flags);
268 return dma_desc;
269 }
270 }
271
272 spin_unlock_irqrestore(&tdc->lock, flags);
273
274 /* Allocate DMA desc */
275 dma_desc = kzalloc(sizeof(*dma_desc), GFP_ATOMIC);
276 if (!dma_desc) {
277 dev_err(tdc2dev(tdc), "dma_desc alloc failed\n");
278 return NULL;
279 }
280
281 dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan);
282 dma_desc->txd.tx_submit = tegra_dma_tx_submit;
283 dma_desc->txd.flags = 0;
284 return dma_desc;
285}
286
287static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
288 struct tegra_dma_desc *dma_desc)
289{
290 unsigned long flags;
291
292 spin_lock_irqsave(&tdc->lock, flags);
293 if (!list_empty(&dma_desc->tx_list))
294 list_splice_init(&dma_desc->tx_list, &tdc->free_sg_req);
295 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
296 spin_unlock_irqrestore(&tdc->lock, flags);
297}
298
299static struct tegra_dma_sg_req *tegra_dma_sg_req_get(
300 struct tegra_dma_channel *tdc)
301{
302 struct tegra_dma_sg_req *sg_req = NULL;
303 unsigned long flags;
304
305 spin_lock_irqsave(&tdc->lock, flags);
306 if (!list_empty(&tdc->free_sg_req)) {
307 sg_req = list_first_entry(&tdc->free_sg_req,
308 typeof(*sg_req), node);
309 list_del(&sg_req->node);
310 spin_unlock_irqrestore(&tdc->lock, flags);
311 return sg_req;
312 }
313 spin_unlock_irqrestore(&tdc->lock, flags);
314
315 sg_req = kzalloc(sizeof(struct tegra_dma_sg_req), GFP_ATOMIC);
316 if (!sg_req)
317 dev_err(tdc2dev(tdc), "sg_req alloc failed\n");
318 return sg_req;
319}
320
321static int tegra_dma_slave_config(struct dma_chan *dc,
322 struct dma_slave_config *sconfig)
323{
324 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
325
326 if (!list_empty(&tdc->pending_sg_req)) {
327 dev_err(tdc2dev(tdc), "Configuration not allowed\n");
328 return -EBUSY;
329 }
330
331 memcpy(&tdc->dma_sconfig, sconfig, sizeof(*sconfig));
332 tdc->config_init = true;
333 return 0;
334}
335
336static void tegra_dma_global_pause(struct tegra_dma_channel *tdc,
337 bool wait_for_burst_complete)
338{
339 struct tegra_dma *tdma = tdc->tdma;
340
341 spin_lock(&tdma->global_lock);
342 tdma_write(tdma, TEGRA_APBDMA_GENERAL, 0);
343 if (wait_for_burst_complete)
344 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
345}
346
347static void tegra_dma_global_resume(struct tegra_dma_channel *tdc)
348{
349 struct tegra_dma *tdma = tdc->tdma;
350
351 tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
352 spin_unlock(&tdma->global_lock);
353}
354
355static void tegra_dma_stop(struct tegra_dma_channel *tdc)
356{
357 u32 csr;
358 u32 status;
359
360 /* Disable interrupts */
361 csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
362 csr &= ~TEGRA_APBDMA_CSR_IE_EOC;
363 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
364
365 /* Disable DMA */
366 csr &= ~TEGRA_APBDMA_CSR_ENB;
367 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, csr);
368
369 /* Clear interrupt status if it is there */
370 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
371 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
372 dev_dbg(tdc2dev(tdc), "%s():clearing interrupt\n", __func__);
373 tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
374 }
375 tdc->busy = false;
376}
377
378static void tegra_dma_start(struct tegra_dma_channel *tdc,
379 struct tegra_dma_sg_req *sg_req)
380{
381 struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs;
382
383 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR, ch_regs->csr);
384 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_regs->apb_seq);
385 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_regs->apb_ptr);
386 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_regs->ahb_seq);
387 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_regs->ahb_ptr);
388
389 /* Start DMA */
390 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
391 ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
392}
393
394static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
395 struct tegra_dma_sg_req *nsg_req)
396{
397 unsigned long status;
398
399 /*
400 * The DMA controller reloads the new configuration for next transfer
401 * after last burst of current transfer completes.
402 * If there is no IEC status then this makes sure that last burst
403 * has not be completed. There may be case that last burst is on
404 * flight and so it can complete but because DMA is paused, it
405 * will not generates interrupt as well as not reload the new
406 * configuration.
407 * If there is already IEC status then interrupt handler need to
408 * load new configuration.
409 */
410 tegra_dma_global_pause(tdc, false);
411 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
412
413 /*
414 * If interrupt is pending then do nothing as the ISR will handle
415 * the programing for new request.
416 */
417 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
418 dev_err(tdc2dev(tdc),
419 "Skipping new configuration as interrupt is pending\n");
420 tegra_dma_global_resume(tdc);
421 return;
422 }
423
424 /* Safe to program new configuration */
425 tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, nsg_req->ch_regs.apb_ptr);
426 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, nsg_req->ch_regs.ahb_ptr);
427 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
428 nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
429 nsg_req->configured = true;
430
431 tegra_dma_global_resume(tdc);
432}
433
434static void tdc_start_head_req(struct tegra_dma_channel *tdc)
435{
436 struct tegra_dma_sg_req *sg_req;
437
438 if (list_empty(&tdc->pending_sg_req))
439 return;
440
441 sg_req = list_first_entry(&tdc->pending_sg_req,
442 typeof(*sg_req), node);
443 tegra_dma_start(tdc, sg_req);
444 sg_req->configured = true;
445 tdc->busy = true;
446}
447
448static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
449{
450 struct tegra_dma_sg_req *hsgreq;
451 struct tegra_dma_sg_req *hnsgreq;
452
453 if (list_empty(&tdc->pending_sg_req))
454 return;
455
456 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
457 if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
458 hnsgreq = list_first_entry(&hsgreq->node,
459 typeof(*hnsgreq), node);
460 tegra_dma_configure_for_next(tdc, hnsgreq);
461 }
462}
463
464static inline int get_current_xferred_count(struct tegra_dma_channel *tdc,
465 struct tegra_dma_sg_req *sg_req, unsigned long status)
466{
467 return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4;
468}
469
470static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
471{
472 struct tegra_dma_sg_req *sgreq;
473 struct tegra_dma_desc *dma_desc;
474
475 while (!list_empty(&tdc->pending_sg_req)) {
476 sgreq = list_first_entry(&tdc->pending_sg_req,
477 typeof(*sgreq), node);
Wei Yongjun2cc44e62012-09-05 15:08:56 +0800478 list_move_tail(&sgreq->node, &tdc->free_sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530479 if (sgreq->last_sg) {
480 dma_desc = sgreq->dma_desc;
481 dma_desc->dma_status = DMA_ERROR;
482 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
483
484 /* Add in cb list if it is not there. */
485 if (!dma_desc->cb_count)
486 list_add_tail(&dma_desc->cb_node,
487 &tdc->cb_desc);
488 dma_desc->cb_count++;
489 }
490 }
491 tdc->isr_handler = NULL;
492}
493
494static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
495 struct tegra_dma_sg_req *last_sg_req, bool to_terminate)
496{
497 struct tegra_dma_sg_req *hsgreq = NULL;
498
499 if (list_empty(&tdc->pending_sg_req)) {
500 dev_err(tdc2dev(tdc), "Dma is running without req\n");
501 tegra_dma_stop(tdc);
502 return false;
503 }
504
505 /*
506 * Check that head req on list should be in flight.
507 * If it is not in flight then abort transfer as
508 * looping of transfer can not continue.
509 */
510 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
511 if (!hsgreq->configured) {
512 tegra_dma_stop(tdc);
513 dev_err(tdc2dev(tdc), "Error in dma transfer, aborting dma\n");
514 tegra_dma_abort_all(tdc);
515 return false;
516 }
517
518 /* Configure next request */
519 if (!to_terminate)
520 tdc_configure_next_head_desc(tdc);
521 return true;
522}
523
524static void handle_once_dma_done(struct tegra_dma_channel *tdc,
525 bool to_terminate)
526{
527 struct tegra_dma_sg_req *sgreq;
528 struct tegra_dma_desc *dma_desc;
529
530 tdc->busy = false;
531 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
532 dma_desc = sgreq->dma_desc;
533 dma_desc->bytes_transferred += sgreq->req_len;
534
535 list_del(&sgreq->node);
536 if (sgreq->last_sg) {
537 dma_desc->dma_status = DMA_SUCCESS;
538 dma_cookie_complete(&dma_desc->txd);
539 if (!dma_desc->cb_count)
540 list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
541 dma_desc->cb_count++;
542 list_add_tail(&dma_desc->node, &tdc->free_dma_desc);
543 }
544 list_add_tail(&sgreq->node, &tdc->free_sg_req);
545
546 /* Do not start DMA if it is going to be terminate */
547 if (to_terminate || list_empty(&tdc->pending_sg_req))
548 return;
549
550 tdc_start_head_req(tdc);
551 return;
552}
553
554static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
555 bool to_terminate)
556{
557 struct tegra_dma_sg_req *sgreq;
558 struct tegra_dma_desc *dma_desc;
559 bool st;
560
561 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
562 dma_desc = sgreq->dma_desc;
563 dma_desc->bytes_transferred += sgreq->req_len;
564
565 /* Callback need to be call */
566 if (!dma_desc->cb_count)
567 list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
568 dma_desc->cb_count++;
569
570 /* If not last req then put at end of pending list */
571 if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
Wei Yongjun2cc44e62012-09-05 15:08:56 +0800572 list_move_tail(&sgreq->node, &tdc->pending_sg_req);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530573 sgreq->configured = false;
574 st = handle_continuous_head_request(tdc, sgreq, to_terminate);
575 if (!st)
576 dma_desc->dma_status = DMA_ERROR;
577 }
578 return;
579}
580
581static void tegra_dma_tasklet(unsigned long data)
582{
583 struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data;
584 dma_async_tx_callback callback = NULL;
585 void *callback_param = NULL;
586 struct tegra_dma_desc *dma_desc;
587 unsigned long flags;
588 int cb_count;
589
590 spin_lock_irqsave(&tdc->lock, flags);
591 while (!list_empty(&tdc->cb_desc)) {
592 dma_desc = list_first_entry(&tdc->cb_desc,
593 typeof(*dma_desc), cb_node);
594 list_del(&dma_desc->cb_node);
595 callback = dma_desc->txd.callback;
596 callback_param = dma_desc->txd.callback_param;
597 cb_count = dma_desc->cb_count;
598 dma_desc->cb_count = 0;
599 spin_unlock_irqrestore(&tdc->lock, flags);
600 while (cb_count-- && callback)
601 callback(callback_param);
602 spin_lock_irqsave(&tdc->lock, flags);
603 }
604 spin_unlock_irqrestore(&tdc->lock, flags);
605}
606
607static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
608{
609 struct tegra_dma_channel *tdc = dev_id;
610 unsigned long status;
611 unsigned long flags;
612
613 spin_lock_irqsave(&tdc->lock, flags);
614
615 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
616 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
617 tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
618 tdc->isr_handler(tdc, false);
619 tasklet_schedule(&tdc->tasklet);
620 spin_unlock_irqrestore(&tdc->lock, flags);
621 return IRQ_HANDLED;
622 }
623
624 spin_unlock_irqrestore(&tdc->lock, flags);
625 dev_info(tdc2dev(tdc),
626 "Interrupt already served status 0x%08lx\n", status);
627 return IRQ_NONE;
628}
629
630static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *txd)
631{
632 struct tegra_dma_desc *dma_desc = txd_to_tegra_dma_desc(txd);
633 struct tegra_dma_channel *tdc = to_tegra_dma_chan(txd->chan);
634 unsigned long flags;
635 dma_cookie_t cookie;
636
637 spin_lock_irqsave(&tdc->lock, flags);
638 dma_desc->dma_status = DMA_IN_PROGRESS;
639 cookie = dma_cookie_assign(&dma_desc->txd);
640 list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req);
641 spin_unlock_irqrestore(&tdc->lock, flags);
642 return cookie;
643}
644
645static void tegra_dma_issue_pending(struct dma_chan *dc)
646{
647 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
648 unsigned long flags;
649
650 spin_lock_irqsave(&tdc->lock, flags);
651 if (list_empty(&tdc->pending_sg_req)) {
652 dev_err(tdc2dev(tdc), "No DMA request\n");
653 goto end;
654 }
655 if (!tdc->busy) {
656 tdc_start_head_req(tdc);
657
658 /* Continuous single mode: Configure next req */
659 if (tdc->cyclic) {
660 /*
661 * Wait for 1 burst time for configure DMA for
662 * next transfer.
663 */
664 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
665 tdc_configure_next_head_desc(tdc);
666 }
667 }
668end:
669 spin_unlock_irqrestore(&tdc->lock, flags);
670 return;
671}
672
673static void tegra_dma_terminate_all(struct dma_chan *dc)
674{
675 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
676 struct tegra_dma_sg_req *sgreq;
677 struct tegra_dma_desc *dma_desc;
678 unsigned long flags;
679 unsigned long status;
680 bool was_busy;
681
682 spin_lock_irqsave(&tdc->lock, flags);
683 if (list_empty(&tdc->pending_sg_req)) {
684 spin_unlock_irqrestore(&tdc->lock, flags);
685 return;
686 }
687
688 if (!tdc->busy)
689 goto skip_dma_stop;
690
691 /* Pause DMA before checking the queue status */
692 tegra_dma_global_pause(tdc, true);
693
694 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
695 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
696 dev_dbg(tdc2dev(tdc), "%s():handling isr\n", __func__);
697 tdc->isr_handler(tdc, true);
698 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
699 }
700
701 was_busy = tdc->busy;
702 tegra_dma_stop(tdc);
703
704 if (!list_empty(&tdc->pending_sg_req) && was_busy) {
705 sgreq = list_first_entry(&tdc->pending_sg_req,
706 typeof(*sgreq), node);
707 sgreq->dma_desc->bytes_transferred +=
708 get_current_xferred_count(tdc, sgreq, status);
709 }
710 tegra_dma_global_resume(tdc);
711
712skip_dma_stop:
713 tegra_dma_abort_all(tdc);
714
715 while (!list_empty(&tdc->cb_desc)) {
716 dma_desc = list_first_entry(&tdc->cb_desc,
717 typeof(*dma_desc), cb_node);
718 list_del(&dma_desc->cb_node);
719 dma_desc->cb_count = 0;
720 }
721 spin_unlock_irqrestore(&tdc->lock, flags);
722}
723
724static enum dma_status tegra_dma_tx_status(struct dma_chan *dc,
725 dma_cookie_t cookie, struct dma_tx_state *txstate)
726{
727 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
728 struct tegra_dma_desc *dma_desc;
729 struct tegra_dma_sg_req *sg_req;
730 enum dma_status ret;
731 unsigned long flags;
Laxman Dewangan4a46ba32012-07-02 13:52:07 +0530732 unsigned int residual;
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530733
734 spin_lock_irqsave(&tdc->lock, flags);
735
736 ret = dma_cookie_status(dc, cookie, txstate);
737 if (ret == DMA_SUCCESS) {
738 dma_set_residue(txstate, 0);
739 spin_unlock_irqrestore(&tdc->lock, flags);
740 return ret;
741 }
742
743 /* Check on wait_ack desc status */
744 list_for_each_entry(dma_desc, &tdc->free_dma_desc, node) {
745 if (dma_desc->txd.cookie == cookie) {
Laxman Dewangan4a46ba32012-07-02 13:52:07 +0530746 residual = dma_desc->bytes_requested -
747 (dma_desc->bytes_transferred %
748 dma_desc->bytes_requested);
749 dma_set_residue(txstate, residual);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530750 ret = dma_desc->dma_status;
751 spin_unlock_irqrestore(&tdc->lock, flags);
752 return ret;
753 }
754 }
755
756 /* Check in pending list */
757 list_for_each_entry(sg_req, &tdc->pending_sg_req, node) {
758 dma_desc = sg_req->dma_desc;
759 if (dma_desc->txd.cookie == cookie) {
Laxman Dewangan4a46ba32012-07-02 13:52:07 +0530760 residual = dma_desc->bytes_requested -
761 (dma_desc->bytes_transferred %
762 dma_desc->bytes_requested);
763 dma_set_residue(txstate, residual);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530764 ret = dma_desc->dma_status;
765 spin_unlock_irqrestore(&tdc->lock, flags);
766 return ret;
767 }
768 }
769
770 dev_dbg(tdc2dev(tdc), "cookie %d does not found\n", cookie);
771 spin_unlock_irqrestore(&tdc->lock, flags);
772 return ret;
773}
774
775static int tegra_dma_device_control(struct dma_chan *dc, enum dma_ctrl_cmd cmd,
776 unsigned long arg)
777{
778 switch (cmd) {
779 case DMA_SLAVE_CONFIG:
780 return tegra_dma_slave_config(dc,
781 (struct dma_slave_config *)arg);
782
783 case DMA_TERMINATE_ALL:
784 tegra_dma_terminate_all(dc);
785 return 0;
786
787 default:
788 break;
789 }
790
791 return -ENXIO;
792}
793
794static inline int get_bus_width(struct tegra_dma_channel *tdc,
795 enum dma_slave_buswidth slave_bw)
796{
797 switch (slave_bw) {
798 case DMA_SLAVE_BUSWIDTH_1_BYTE:
799 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_8;
800 case DMA_SLAVE_BUSWIDTH_2_BYTES:
801 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_16;
802 case DMA_SLAVE_BUSWIDTH_4_BYTES:
803 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
804 case DMA_SLAVE_BUSWIDTH_8_BYTES:
805 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64;
806 default:
807 dev_warn(tdc2dev(tdc),
808 "slave bw is not supported, using 32bits\n");
809 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
810 }
811}
812
813static inline int get_burst_size(struct tegra_dma_channel *tdc,
814 u32 burst_size, enum dma_slave_buswidth slave_bw, int len)
815{
816 int burst_byte;
817 int burst_ahb_width;
818
819 /*
820 * burst_size from client is in terms of the bus_width.
821 * convert them into AHB memory width which is 4 byte.
822 */
823 burst_byte = burst_size * slave_bw;
824 burst_ahb_width = burst_byte / 4;
825
826 /* If burst size is 0 then calculate the burst size based on length */
827 if (!burst_ahb_width) {
828 if (len & 0xF)
829 return TEGRA_APBDMA_AHBSEQ_BURST_1;
830 else if ((len >> 4) & 0x1)
831 return TEGRA_APBDMA_AHBSEQ_BURST_4;
832 else
833 return TEGRA_APBDMA_AHBSEQ_BURST_8;
834 }
835 if (burst_ahb_width < 4)
836 return TEGRA_APBDMA_AHBSEQ_BURST_1;
837 else if (burst_ahb_width < 8)
838 return TEGRA_APBDMA_AHBSEQ_BURST_4;
839 else
840 return TEGRA_APBDMA_AHBSEQ_BURST_8;
841}
842
843static int get_transfer_param(struct tegra_dma_channel *tdc,
844 enum dma_transfer_direction direction, unsigned long *apb_addr,
845 unsigned long *apb_seq, unsigned long *csr, unsigned int *burst_size,
846 enum dma_slave_buswidth *slave_bw)
847{
848
849 switch (direction) {
850 case DMA_MEM_TO_DEV:
851 *apb_addr = tdc->dma_sconfig.dst_addr;
852 *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.dst_addr_width);
853 *burst_size = tdc->dma_sconfig.dst_maxburst;
854 *slave_bw = tdc->dma_sconfig.dst_addr_width;
855 *csr = TEGRA_APBDMA_CSR_DIR;
856 return 0;
857
858 case DMA_DEV_TO_MEM:
859 *apb_addr = tdc->dma_sconfig.src_addr;
860 *apb_seq = get_bus_width(tdc, tdc->dma_sconfig.src_addr_width);
861 *burst_size = tdc->dma_sconfig.src_maxburst;
862 *slave_bw = tdc->dma_sconfig.src_addr_width;
863 *csr = 0;
864 return 0;
865
866 default:
867 dev_err(tdc2dev(tdc), "Dma direction is not supported\n");
868 return -EINVAL;
869 }
870 return -EINVAL;
871}
872
873static struct dma_async_tx_descriptor *tegra_dma_prep_slave_sg(
874 struct dma_chan *dc, struct scatterlist *sgl, unsigned int sg_len,
875 enum dma_transfer_direction direction, unsigned long flags,
876 void *context)
877{
878 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
879 struct tegra_dma_desc *dma_desc;
880 unsigned int i;
881 struct scatterlist *sg;
882 unsigned long csr, ahb_seq, apb_ptr, apb_seq;
883 struct list_head req_list;
884 struct tegra_dma_sg_req *sg_req = NULL;
885 u32 burst_size;
886 enum dma_slave_buswidth slave_bw;
887 int ret;
888
889 if (!tdc->config_init) {
890 dev_err(tdc2dev(tdc), "dma channel is not configured\n");
891 return NULL;
892 }
893 if (sg_len < 1) {
894 dev_err(tdc2dev(tdc), "Invalid segment length %d\n", sg_len);
895 return NULL;
896 }
897
898 ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
899 &burst_size, &slave_bw);
900 if (ret < 0)
901 return NULL;
902
903 INIT_LIST_HEAD(&req_list);
904
905 ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
906 ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
907 TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
908 ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
909
910 csr |= TEGRA_APBDMA_CSR_ONCE | TEGRA_APBDMA_CSR_FLOW;
911 csr |= tdc->dma_sconfig.slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
912 if (flags & DMA_PREP_INTERRUPT)
913 csr |= TEGRA_APBDMA_CSR_IE_EOC;
914
915 apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
916
917 dma_desc = tegra_dma_desc_get(tdc);
918 if (!dma_desc) {
919 dev_err(tdc2dev(tdc), "Dma descriptors not available\n");
920 return NULL;
921 }
922 INIT_LIST_HEAD(&dma_desc->tx_list);
923 INIT_LIST_HEAD(&dma_desc->cb_node);
924 dma_desc->cb_count = 0;
925 dma_desc->bytes_requested = 0;
926 dma_desc->bytes_transferred = 0;
927 dma_desc->dma_status = DMA_IN_PROGRESS;
928
929 /* Make transfer requests */
930 for_each_sg(sgl, sg, sg_len, i) {
931 u32 len, mem;
932
Laxman Dewangan597c8542012-06-22 20:41:10 +0530933 mem = sg_dma_address(sg);
Laxman Dewanganec8a1582012-06-06 10:55:27 +0530934 len = sg_dma_len(sg);
935
936 if ((len & 3) || (mem & 3) ||
937 (len > tdc->tdma->chip_data->max_dma_count)) {
938 dev_err(tdc2dev(tdc),
939 "Dma length/memory address is not supported\n");
940 tegra_dma_desc_put(tdc, dma_desc);
941 return NULL;
942 }
943
944 sg_req = tegra_dma_sg_req_get(tdc);
945 if (!sg_req) {
946 dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
947 tegra_dma_desc_put(tdc, dma_desc);
948 return NULL;
949 }
950
951 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
952 dma_desc->bytes_requested += len;
953
954 sg_req->ch_regs.apb_ptr = apb_ptr;
955 sg_req->ch_regs.ahb_ptr = mem;
956 sg_req->ch_regs.csr = csr | ((len - 4) & 0xFFFC);
957 sg_req->ch_regs.apb_seq = apb_seq;
958 sg_req->ch_regs.ahb_seq = ahb_seq;
959 sg_req->configured = false;
960 sg_req->last_sg = false;
961 sg_req->dma_desc = dma_desc;
962 sg_req->req_len = len;
963
964 list_add_tail(&sg_req->node, &dma_desc->tx_list);
965 }
966 sg_req->last_sg = true;
967 if (flags & DMA_CTRL_ACK)
968 dma_desc->txd.flags = DMA_CTRL_ACK;
969
970 /*
971 * Make sure that mode should not be conflicting with currently
972 * configured mode.
973 */
974 if (!tdc->isr_handler) {
975 tdc->isr_handler = handle_once_dma_done;
976 tdc->cyclic = false;
977 } else {
978 if (tdc->cyclic) {
979 dev_err(tdc2dev(tdc), "DMA configured in cyclic mode\n");
980 tegra_dma_desc_put(tdc, dma_desc);
981 return NULL;
982 }
983 }
984
985 return &dma_desc->txd;
986}
987
988struct dma_async_tx_descriptor *tegra_dma_prep_dma_cyclic(
989 struct dma_chan *dc, dma_addr_t buf_addr, size_t buf_len,
990 size_t period_len, enum dma_transfer_direction direction,
991 void *context)
992{
993 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
994 struct tegra_dma_desc *dma_desc = NULL;
995 struct tegra_dma_sg_req *sg_req = NULL;
996 unsigned long csr, ahb_seq, apb_ptr, apb_seq;
997 int len;
998 size_t remain_len;
999 dma_addr_t mem = buf_addr;
1000 u32 burst_size;
1001 enum dma_slave_buswidth slave_bw;
1002 int ret;
1003
1004 if (!buf_len || !period_len) {
1005 dev_err(tdc2dev(tdc), "Invalid buffer/period len\n");
1006 return NULL;
1007 }
1008
1009 if (!tdc->config_init) {
1010 dev_err(tdc2dev(tdc), "DMA slave is not configured\n");
1011 return NULL;
1012 }
1013
1014 /*
1015 * We allow to take more number of requests till DMA is
1016 * not started. The driver will loop over all requests.
1017 * Once DMA is started then new requests can be queued only after
1018 * terminating the DMA.
1019 */
1020 if (tdc->busy) {
1021 dev_err(tdc2dev(tdc), "Request not allowed when dma running\n");
1022 return NULL;
1023 }
1024
1025 /*
1026 * We only support cycle transfer when buf_len is multiple of
1027 * period_len.
1028 */
1029 if (buf_len % period_len) {
1030 dev_err(tdc2dev(tdc), "buf_len is not multiple of period_len\n");
1031 return NULL;
1032 }
1033
1034 len = period_len;
1035 if ((len & 3) || (buf_addr & 3) ||
1036 (len > tdc->tdma->chip_data->max_dma_count)) {
1037 dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n");
1038 return NULL;
1039 }
1040
1041 ret = get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
1042 &burst_size, &slave_bw);
1043 if (ret < 0)
1044 return NULL;
1045
1046
1047 ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
1048 ahb_seq |= TEGRA_APBDMA_AHBSEQ_WRAP_NONE <<
1049 TEGRA_APBDMA_AHBSEQ_WRAP_SHIFT;
1050 ahb_seq |= TEGRA_APBDMA_AHBSEQ_BUS_WIDTH_32;
1051
1052 csr |= TEGRA_APBDMA_CSR_FLOW | TEGRA_APBDMA_CSR_IE_EOC;
1053 csr |= tdc->dma_sconfig.slave_id << TEGRA_APBDMA_CSR_REQ_SEL_SHIFT;
1054
1055 apb_seq |= TEGRA_APBDMA_APBSEQ_WRAP_WORD_1;
1056
1057 dma_desc = tegra_dma_desc_get(tdc);
1058 if (!dma_desc) {
1059 dev_err(tdc2dev(tdc), "not enough descriptors available\n");
1060 return NULL;
1061 }
1062
1063 INIT_LIST_HEAD(&dma_desc->tx_list);
1064 INIT_LIST_HEAD(&dma_desc->cb_node);
1065 dma_desc->cb_count = 0;
1066
1067 dma_desc->bytes_transferred = 0;
1068 dma_desc->bytes_requested = buf_len;
1069 remain_len = buf_len;
1070
1071 /* Split transfer equal to period size */
1072 while (remain_len) {
1073 sg_req = tegra_dma_sg_req_get(tdc);
1074 if (!sg_req) {
1075 dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
1076 tegra_dma_desc_put(tdc, dma_desc);
1077 return NULL;
1078 }
1079
1080 ahb_seq |= get_burst_size(tdc, burst_size, slave_bw, len);
1081 sg_req->ch_regs.apb_ptr = apb_ptr;
1082 sg_req->ch_regs.ahb_ptr = mem;
1083 sg_req->ch_regs.csr = csr | ((len - 4) & 0xFFFC);
1084 sg_req->ch_regs.apb_seq = apb_seq;
1085 sg_req->ch_regs.ahb_seq = ahb_seq;
1086 sg_req->configured = false;
1087 sg_req->half_done = false;
1088 sg_req->last_sg = false;
1089 sg_req->dma_desc = dma_desc;
1090 sg_req->req_len = len;
1091
1092 list_add_tail(&sg_req->node, &dma_desc->tx_list);
1093 remain_len -= len;
1094 mem += len;
1095 }
1096 sg_req->last_sg = true;
Laxman Dewangan6660f7a2012-06-22 17:12:44 +05301097 dma_desc->txd.flags = 0;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301098
1099 /*
1100 * Make sure that mode should not be conflicting with currently
1101 * configured mode.
1102 */
1103 if (!tdc->isr_handler) {
1104 tdc->isr_handler = handle_cont_sngl_cycle_dma_done;
1105 tdc->cyclic = true;
1106 } else {
1107 if (!tdc->cyclic) {
1108 dev_err(tdc2dev(tdc), "DMA configuration conflict\n");
1109 tegra_dma_desc_put(tdc, dma_desc);
1110 return NULL;
1111 }
1112 }
1113
1114 return &dma_desc->txd;
1115}
1116
1117static int tegra_dma_alloc_chan_resources(struct dma_chan *dc)
1118{
1119 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Laxman Dewanganfed57cd2012-07-20 13:31:08 +05301120 struct tegra_dma *tdma = tdc->tdma;
1121 int ret;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301122
1123 dma_cookie_init(&tdc->dma_chan);
1124 tdc->config_init = false;
Laxman Dewanganfed57cd2012-07-20 13:31:08 +05301125 ret = clk_prepare_enable(tdma->dma_clk);
1126 if (ret < 0)
1127 dev_err(tdc2dev(tdc), "clk_prepare_enable failed: %d\n", ret);
1128 return ret;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301129}
1130
1131static void tegra_dma_free_chan_resources(struct dma_chan *dc)
1132{
1133 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
Laxman Dewanganfed57cd2012-07-20 13:31:08 +05301134 struct tegra_dma *tdma = tdc->tdma;
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301135
1136 struct tegra_dma_desc *dma_desc;
1137 struct tegra_dma_sg_req *sg_req;
1138 struct list_head dma_desc_list;
1139 struct list_head sg_req_list;
1140 unsigned long flags;
1141
1142 INIT_LIST_HEAD(&dma_desc_list);
1143 INIT_LIST_HEAD(&sg_req_list);
1144
1145 dev_dbg(tdc2dev(tdc), "Freeing channel %d\n", tdc->id);
1146
1147 if (tdc->busy)
1148 tegra_dma_terminate_all(dc);
1149
1150 spin_lock_irqsave(&tdc->lock, flags);
1151 list_splice_init(&tdc->pending_sg_req, &sg_req_list);
1152 list_splice_init(&tdc->free_sg_req, &sg_req_list);
1153 list_splice_init(&tdc->free_dma_desc, &dma_desc_list);
1154 INIT_LIST_HEAD(&tdc->cb_desc);
1155 tdc->config_init = false;
1156 spin_unlock_irqrestore(&tdc->lock, flags);
1157
1158 while (!list_empty(&dma_desc_list)) {
1159 dma_desc = list_first_entry(&dma_desc_list,
1160 typeof(*dma_desc), node);
1161 list_del(&dma_desc->node);
1162 kfree(dma_desc);
1163 }
1164
1165 while (!list_empty(&sg_req_list)) {
1166 sg_req = list_first_entry(&sg_req_list, typeof(*sg_req), node);
1167 list_del(&sg_req->node);
1168 kfree(sg_req);
1169 }
Laxman Dewanganfed57cd2012-07-20 13:31:08 +05301170 clk_disable_unprepare(tdma->dma_clk);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301171}
1172
1173/* Tegra20 specific DMA controller information */
1174static struct tegra_dma_chip_data tegra20_dma_chip_data = {
1175 .nr_channels = 16,
1176 .max_dma_count = 1024UL * 64,
1177};
1178
1179#if defined(CONFIG_OF)
1180/* Tegra30 specific DMA controller information */
1181static struct tegra_dma_chip_data tegra30_dma_chip_data = {
1182 .nr_channels = 32,
1183 .max_dma_count = 1024UL * 64,
1184};
1185
1186static const struct of_device_id tegra_dma_of_match[] __devinitconst = {
1187 {
Laxman Dewangancd9092c2012-07-02 13:52:08 +05301188 .compatible = "nvidia,tegra30-apbdma",
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301189 .data = &tegra30_dma_chip_data,
1190 }, {
Laxman Dewangancd9092c2012-07-02 13:52:08 +05301191 .compatible = "nvidia,tegra20-apbdma",
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301192 .data = &tegra20_dma_chip_data,
1193 }, {
1194 },
1195};
1196MODULE_DEVICE_TABLE(of, tegra_dma_of_match);
1197#endif
1198
1199static int __devinit tegra_dma_probe(struct platform_device *pdev)
1200{
1201 struct resource *res;
1202 struct tegra_dma *tdma;
1203 int ret;
1204 int i;
1205 struct tegra_dma_chip_data *cdata = NULL;
1206
1207 if (pdev->dev.of_node) {
1208 const struct of_device_id *match;
1209 match = of_match_device(of_match_ptr(tegra_dma_of_match),
1210 &pdev->dev);
1211 if (!match) {
1212 dev_err(&pdev->dev, "Error: No device match found\n");
1213 return -ENODEV;
1214 }
1215 cdata = match->data;
1216 } else {
1217 /* If no device tree then fallback to tegra20 */
1218 cdata = &tegra20_dma_chip_data;
1219 }
1220
1221 tdma = devm_kzalloc(&pdev->dev, sizeof(*tdma) + cdata->nr_channels *
1222 sizeof(struct tegra_dma_channel), GFP_KERNEL);
1223 if (!tdma) {
1224 dev_err(&pdev->dev, "Error: memory allocation failed\n");
1225 return -ENOMEM;
1226 }
1227
1228 tdma->dev = &pdev->dev;
1229 tdma->chip_data = cdata;
1230 platform_set_drvdata(pdev, tdma);
1231
1232 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1233 if (!res) {
1234 dev_err(&pdev->dev, "No mem resource for DMA\n");
1235 return -EINVAL;
1236 }
1237
1238 tdma->base_addr = devm_request_and_ioremap(&pdev->dev, res);
1239 if (!tdma->base_addr) {
1240 dev_err(&pdev->dev,
1241 "Cannot request memregion/iomap dma address\n");
1242 return -EADDRNOTAVAIL;
1243 }
1244
1245 tdma->dma_clk = devm_clk_get(&pdev->dev, NULL);
1246 if (IS_ERR(tdma->dma_clk)) {
1247 dev_err(&pdev->dev, "Error: Missing controller clock\n");
1248 return PTR_ERR(tdma->dma_clk);
1249 }
1250
1251 spin_lock_init(&tdma->global_lock);
1252
1253 pm_runtime_enable(&pdev->dev);
1254 if (!pm_runtime_enabled(&pdev->dev)) {
1255 ret = tegra_dma_runtime_resume(&pdev->dev);
1256 if (ret) {
1257 dev_err(&pdev->dev, "dma_runtime_resume failed %d\n",
1258 ret);
1259 goto err_pm_disable;
1260 }
1261 }
1262
Laxman Dewanganfed57cd2012-07-20 13:31:08 +05301263 /* Enable clock before accessing registers */
1264 ret = clk_prepare_enable(tdma->dma_clk);
1265 if (ret < 0) {
1266 dev_err(&pdev->dev, "clk_prepare_enable failed: %d\n", ret);
1267 goto err_pm_disable;
1268 }
1269
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301270 /* Reset DMA controller */
1271 tegra_periph_reset_assert(tdma->dma_clk);
1272 udelay(2);
1273 tegra_periph_reset_deassert(tdma->dma_clk);
1274
1275 /* Enable global DMA registers */
1276 tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
1277 tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
1278 tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
1279
Laxman Dewanganfed57cd2012-07-20 13:31:08 +05301280 clk_disable_unprepare(tdma->dma_clk);
1281
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301282 INIT_LIST_HEAD(&tdma->dma_dev.channels);
1283 for (i = 0; i < cdata->nr_channels; i++) {
1284 struct tegra_dma_channel *tdc = &tdma->channels[i];
1285 char irq_name[30];
1286
1287 tdc->chan_base_offset = TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
1288 i * TEGRA_APBDMA_CHANNEL_REGISTER_SIZE;
1289
1290 res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1291 if (!res) {
1292 ret = -EINVAL;
1293 dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
1294 goto err_irq;
1295 }
1296 tdc->irq = res->start;
1297 snprintf(irq_name, sizeof(irq_name), "apbdma.%d", i);
1298 ret = devm_request_irq(&pdev->dev, tdc->irq,
1299 tegra_dma_isr, 0, irq_name, tdc);
1300 if (ret) {
1301 dev_err(&pdev->dev,
1302 "request_irq failed with err %d channel %d\n",
1303 i, ret);
1304 goto err_irq;
1305 }
1306
1307 tdc->dma_chan.device = &tdma->dma_dev;
1308 dma_cookie_init(&tdc->dma_chan);
1309 list_add_tail(&tdc->dma_chan.device_node,
1310 &tdma->dma_dev.channels);
1311 tdc->tdma = tdma;
1312 tdc->id = i;
1313
1314 tasklet_init(&tdc->tasklet, tegra_dma_tasklet,
1315 (unsigned long)tdc);
1316 spin_lock_init(&tdc->lock);
1317
1318 INIT_LIST_HEAD(&tdc->pending_sg_req);
1319 INIT_LIST_HEAD(&tdc->free_sg_req);
1320 INIT_LIST_HEAD(&tdc->free_dma_desc);
1321 INIT_LIST_HEAD(&tdc->cb_desc);
1322 }
1323
1324 dma_cap_set(DMA_SLAVE, tdma->dma_dev.cap_mask);
1325 dma_cap_set(DMA_PRIVATE, tdma->dma_dev.cap_mask);
Laxman Dewangan46fb3f82012-06-22 17:12:43 +05301326 dma_cap_set(DMA_CYCLIC, tdma->dma_dev.cap_mask);
1327
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301328 tdma->dma_dev.dev = &pdev->dev;
1329 tdma->dma_dev.device_alloc_chan_resources =
1330 tegra_dma_alloc_chan_resources;
1331 tdma->dma_dev.device_free_chan_resources =
1332 tegra_dma_free_chan_resources;
1333 tdma->dma_dev.device_prep_slave_sg = tegra_dma_prep_slave_sg;
1334 tdma->dma_dev.device_prep_dma_cyclic = tegra_dma_prep_dma_cyclic;
1335 tdma->dma_dev.device_control = tegra_dma_device_control;
1336 tdma->dma_dev.device_tx_status = tegra_dma_tx_status;
1337 tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending;
1338
1339 ret = dma_async_device_register(&tdma->dma_dev);
1340 if (ret < 0) {
1341 dev_err(&pdev->dev,
1342 "Tegra20 APB DMA driver registration failed %d\n", ret);
1343 goto err_irq;
1344 }
1345
1346 dev_info(&pdev->dev, "Tegra20 APB DMA driver register %d channels\n",
1347 cdata->nr_channels);
1348 return 0;
1349
1350err_irq:
1351 while (--i >= 0) {
1352 struct tegra_dma_channel *tdc = &tdma->channels[i];
1353 tasklet_kill(&tdc->tasklet);
1354 }
1355
1356err_pm_disable:
1357 pm_runtime_disable(&pdev->dev);
1358 if (!pm_runtime_status_suspended(&pdev->dev))
1359 tegra_dma_runtime_suspend(&pdev->dev);
1360 return ret;
1361}
1362
1363static int __devexit tegra_dma_remove(struct platform_device *pdev)
1364{
1365 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1366 int i;
1367 struct tegra_dma_channel *tdc;
1368
1369 dma_async_device_unregister(&tdma->dma_dev);
1370
1371 for (i = 0; i < tdma->chip_data->nr_channels; ++i) {
1372 tdc = &tdma->channels[i];
1373 tasklet_kill(&tdc->tasklet);
1374 }
1375
1376 pm_runtime_disable(&pdev->dev);
1377 if (!pm_runtime_status_suspended(&pdev->dev))
1378 tegra_dma_runtime_suspend(&pdev->dev);
1379
1380 return 0;
1381}
1382
1383static int tegra_dma_runtime_suspend(struct device *dev)
1384{
1385 struct platform_device *pdev = to_platform_device(dev);
1386 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1387
Prashant Gaikwad56482ec2012-06-25 12:01:31 +05301388 clk_disable_unprepare(tdma->dma_clk);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301389 return 0;
1390}
1391
1392static int tegra_dma_runtime_resume(struct device *dev)
1393{
1394 struct platform_device *pdev = to_platform_device(dev);
1395 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1396 int ret;
1397
Prashant Gaikwad56482ec2012-06-25 12:01:31 +05301398 ret = clk_prepare_enable(tdma->dma_clk);
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301399 if (ret < 0) {
1400 dev_err(dev, "clk_enable failed: %d\n", ret);
1401 return ret;
1402 }
1403 return 0;
1404}
1405
1406static const struct dev_pm_ops tegra_dma_dev_pm_ops __devinitconst = {
1407#ifdef CONFIG_PM_RUNTIME
1408 .runtime_suspend = tegra_dma_runtime_suspend,
1409 .runtime_resume = tegra_dma_runtime_resume,
1410#endif
1411};
1412
1413static struct platform_driver tegra_dmac_driver = {
1414 .driver = {
Laxman Dewangancd9092c2012-07-02 13:52:08 +05301415 .name = "tegra-apbdma",
Laxman Dewanganec8a1582012-06-06 10:55:27 +05301416 .owner = THIS_MODULE,
1417 .pm = &tegra_dma_dev_pm_ops,
1418 .of_match_table = of_match_ptr(tegra_dma_of_match),
1419 },
1420 .probe = tegra_dma_probe,
1421 .remove = __devexit_p(tegra_dma_remove),
1422};
1423
1424module_platform_driver(tegra_dmac_driver);
1425
1426MODULE_ALIAS("platform:tegra20-apbdma");
1427MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
1428MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
1429MODULE_LICENSE("GPL v2");