hc
2024-02-20 102a0743326a03cd1a1202ceda21e175b7d3575c
kernel/drivers/dma/tegra20-apb-dma.c
....@@ -1,19 +1,8 @@
1
+// SPDX-License-Identifier: GPL-2.0-only
12 /*
23 * DMA driver for Nvidia's Tegra20 APB DMA controller.
34 *
45 * Copyright (c) 2012-2013, 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/>.
176 */
187
198 #include <linux/bitops.h>
....@@ -35,8 +24,12 @@
3524 #include <linux/pm_runtime.h>
3625 #include <linux/reset.h>
3726 #include <linux/slab.h>
27
+#include <linux/wait.h>
3828
3929 #include "dmaengine.h"
30
+
31
+#define CREATE_TRACE_POINTS
32
+#include <trace/events/tegra_apb_dma.h>
4033
4134 #define TEGRA_APBDMA_GENERAL 0x0
4235 #define TEGRA_APBDMA_GENERAL_ENABLE BIT(31)
....@@ -67,7 +60,7 @@
6760 #define TEGRA_APBDMA_STATUS_COUNT_MASK 0xFFFC
6861
6962 #define TEGRA_APBDMA_CHAN_CSRE 0x00C
70
-#define TEGRA_APBDMA_CHAN_CSRE_PAUSE (1 << 31)
63
+#define TEGRA_APBDMA_CHAN_CSRE_PAUSE BIT(31)
7164
7265 /* AHB memory address */
7366 #define TEGRA_APBDMA_CHAN_AHBPTR 0x010
....@@ -128,25 +121,25 @@
128121 * @support_separate_wcount_reg: Support separate word count register.
129122 */
130123 struct tegra_dma_chip_data {
131
- int nr_channels;
132
- int channel_reg_size;
133
- int max_dma_count;
124
+ unsigned int nr_channels;
125
+ unsigned int channel_reg_size;
126
+ unsigned int max_dma_count;
134127 bool support_channel_pause;
135128 bool support_separate_wcount_reg;
136129 };
137130
138131 /* DMA channel registers */
139132 struct tegra_dma_channel_regs {
140
- unsigned long csr;
141
- unsigned long ahb_ptr;
142
- unsigned long apb_ptr;
143
- unsigned long ahb_seq;
144
- unsigned long apb_seq;
145
- unsigned long wcount;
133
+ u32 csr;
134
+ u32 ahb_ptr;
135
+ u32 apb_ptr;
136
+ u32 ahb_seq;
137
+ u32 apb_seq;
138
+ u32 wcount;
146139 };
147140
148141 /*
149
- * tegra_dma_sg_req: Dma request details to configure hardware. This
142
+ * tegra_dma_sg_req: DMA request details to configure hardware. This
150143 * contains the details for one transfer to configure DMA hw.
151144 * The client's request for data transfer can be broken into multiple
152145 * sub-transfer as per requester details and hw support.
....@@ -155,11 +148,12 @@
155148 */
156149 struct tegra_dma_sg_req {
157150 struct tegra_dma_channel_regs ch_regs;
158
- int req_len;
151
+ unsigned int req_len;
159152 bool configured;
160153 bool last_sg;
161154 struct list_head node;
162155 struct tegra_dma_desc *dma_desc;
156
+ unsigned int words_xferred;
163157 };
164158
165159 /*
....@@ -169,13 +163,13 @@
169163 */
170164 struct tegra_dma_desc {
171165 struct dma_async_tx_descriptor txd;
172
- int bytes_requested;
173
- int bytes_transferred;
166
+ unsigned int bytes_requested;
167
+ unsigned int bytes_transferred;
174168 enum dma_status dma_status;
175169 struct list_head node;
176170 struct list_head tx_list;
177171 struct list_head cb_node;
178
- int cb_count;
172
+ unsigned int cb_count;
179173 };
180174
181175 struct tegra_dma_channel;
....@@ -186,10 +180,9 @@
186180 /* tegra_dma_channel: Channel specific information */
187181 struct tegra_dma_channel {
188182 struct dma_chan dma_chan;
189
- char name[30];
183
+ char name[12];
190184 bool config_init;
191
- int id;
192
- int irq;
185
+ unsigned int id;
193186 void __iomem *chan_addr;
194187 spinlock_t lock;
195188 bool busy;
....@@ -209,7 +202,9 @@
209202 /* Channel-slave specific configuration */
210203 unsigned int slave_id;
211204 struct dma_slave_config dma_sconfig;
212
- struct tegra_dma_channel_regs channel_reg;
205
+ struct tegra_dma_channel_regs channel_reg;
206
+
207
+ struct wait_queue_head wq;
213208 };
214209
215210 /* tegra_dma: Tegra DMA specific information */
....@@ -229,11 +224,8 @@
229224 */
230225 u32 global_pause_count;
231226
232
- /* Some register need to be cache before suspend */
233
- u32 reg_gen;
234
-
235227 /* Last member of the structure */
236
- struct tegra_dma_channel channels[0];
228
+ struct tegra_dma_channel channels[];
237229 };
238230
239231 static inline void tdma_write(struct tegra_dma *tdma, u32 reg, u32 val)
....@@ -247,7 +239,7 @@
247239 }
248240
249241 static inline void tdc_write(struct tegra_dma_channel *tdc,
250
- u32 reg, u32 val)
242
+ u32 reg, u32 val)
251243 {
252244 writel(val, tdc->chan_addr + reg);
253245 }
....@@ -262,8 +254,8 @@
262254 return container_of(dc, struct tegra_dma_channel, dma_chan);
263255 }
264256
265
-static inline struct tegra_dma_desc *txd_to_tegra_dma_desc(
266
- struct dma_async_tx_descriptor *td)
257
+static inline struct tegra_dma_desc *
258
+txd_to_tegra_dma_desc(struct dma_async_tx_descriptor *td)
267259 {
268260 return container_of(td, struct tegra_dma_desc, txd);
269261 }
....@@ -274,12 +266,9 @@
274266 }
275267
276268 static dma_cookie_t tegra_dma_tx_submit(struct dma_async_tx_descriptor *tx);
277
-static int tegra_dma_runtime_suspend(struct device *dev);
278
-static int tegra_dma_runtime_resume(struct device *dev);
279269
280270 /* Get DMA desc from free list, if not there then allocate it. */
281
-static struct tegra_dma_desc *tegra_dma_desc_get(
282
- struct tegra_dma_channel *tdc)
271
+static struct tegra_dma_desc *tegra_dma_desc_get(struct tegra_dma_channel *tdc)
283272 {
284273 struct tegra_dma_desc *dma_desc;
285274 unsigned long flags;
....@@ -306,11 +295,12 @@
306295 dma_async_tx_descriptor_init(&dma_desc->txd, &tdc->dma_chan);
307296 dma_desc->txd.tx_submit = tegra_dma_tx_submit;
308297 dma_desc->txd.flags = 0;
298
+
309299 return dma_desc;
310300 }
311301
312302 static void tegra_dma_desc_put(struct tegra_dma_channel *tdc,
313
- struct tegra_dma_desc *dma_desc)
303
+ struct tegra_dma_desc *dma_desc)
314304 {
315305 unsigned long flags;
316306
....@@ -321,29 +311,29 @@
321311 spin_unlock_irqrestore(&tdc->lock, flags);
322312 }
323313
324
-static struct tegra_dma_sg_req *tegra_dma_sg_req_get(
325
- struct tegra_dma_channel *tdc)
314
+static struct tegra_dma_sg_req *
315
+tegra_dma_sg_req_get(struct tegra_dma_channel *tdc)
326316 {
327
- struct tegra_dma_sg_req *sg_req = NULL;
317
+ struct tegra_dma_sg_req *sg_req;
328318 unsigned long flags;
329319
330320 spin_lock_irqsave(&tdc->lock, flags);
331321 if (!list_empty(&tdc->free_sg_req)) {
332
- sg_req = list_first_entry(&tdc->free_sg_req,
333
- typeof(*sg_req), node);
322
+ sg_req = list_first_entry(&tdc->free_sg_req, typeof(*sg_req),
323
+ node);
334324 list_del(&sg_req->node);
335325 spin_unlock_irqrestore(&tdc->lock, flags);
336326 return sg_req;
337327 }
338328 spin_unlock_irqrestore(&tdc->lock, flags);
339329
340
- sg_req = kzalloc(sizeof(struct tegra_dma_sg_req), GFP_NOWAIT);
330
+ sg_req = kzalloc(sizeof(*sg_req), GFP_NOWAIT);
341331
342332 return sg_req;
343333 }
344334
345335 static int tegra_dma_slave_config(struct dma_chan *dc,
346
- struct dma_slave_config *sconfig)
336
+ struct dma_slave_config *sconfig)
347337 {
348338 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
349339
....@@ -360,11 +350,12 @@
360350 tdc->slave_id = sconfig->slave_id;
361351 }
362352 tdc->config_init = true;
353
+
363354 return 0;
364355 }
365356
366357 static void tegra_dma_global_pause(struct tegra_dma_channel *tdc,
367
- bool wait_for_burst_complete)
358
+ bool wait_for_burst_complete)
368359 {
369360 struct tegra_dma *tdma = tdc->tdma;
370361
....@@ -399,13 +390,13 @@
399390 }
400391
401392 static void tegra_dma_pause(struct tegra_dma_channel *tdc,
402
- bool wait_for_burst_complete)
393
+ bool wait_for_burst_complete)
403394 {
404395 struct tegra_dma *tdma = tdc->tdma;
405396
406397 if (tdma->chip_data->support_channel_pause) {
407398 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE,
408
- TEGRA_APBDMA_CHAN_CSRE_PAUSE);
399
+ TEGRA_APBDMA_CHAN_CSRE_PAUSE);
409400 if (wait_for_burst_complete)
410401 udelay(TEGRA_APBDMA_BURST_COMPLETE_TIME);
411402 } else {
....@@ -417,17 +408,15 @@
417408 {
418409 struct tegra_dma *tdma = tdc->tdma;
419410
420
- if (tdma->chip_data->support_channel_pause) {
411
+ if (tdma->chip_data->support_channel_pause)
421412 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSRE, 0);
422
- } else {
413
+ else
423414 tegra_dma_global_resume(tdc);
424
- }
425415 }
426416
427417 static void tegra_dma_stop(struct tegra_dma_channel *tdc)
428418 {
429
- u32 csr;
430
- u32 status;
419
+ u32 csr, status;
431420
432421 /* Disable interrupts */
433422 csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
....@@ -448,7 +437,7 @@
448437 }
449438
450439 static void tegra_dma_start(struct tegra_dma_channel *tdc,
451
- struct tegra_dma_sg_req *sg_req)
440
+ struct tegra_dma_sg_req *sg_req)
452441 {
453442 struct tegra_dma_channel_regs *ch_regs = &sg_req->ch_regs;
454443
....@@ -462,11 +451,11 @@
462451
463452 /* Start DMA */
464453 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
465
- ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
454
+ ch_regs->csr | TEGRA_APBDMA_CSR_ENB);
466455 }
467456
468457 static void tegra_dma_configure_for_next(struct tegra_dma_channel *tdc,
469
- struct tegra_dma_sg_req *nsg_req)
458
+ struct tegra_dma_sg_req *nsg_req)
470459 {
471460 unsigned long status;
472461
....@@ -500,10 +489,11 @@
500489 tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, nsg_req->ch_regs.ahb_ptr);
501490 if (tdc->tdma->chip_data->support_separate_wcount_reg)
502491 tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT,
503
- nsg_req->ch_regs.wcount);
492
+ nsg_req->ch_regs.wcount);
504493 tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
505
- nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
494
+ nsg_req->ch_regs.csr | TEGRA_APBDMA_CSR_ENB);
506495 nsg_req->configured = true;
496
+ nsg_req->words_xferred = 0;
507497
508498 tegra_dma_resume(tdc);
509499 }
....@@ -512,46 +502,41 @@
512502 {
513503 struct tegra_dma_sg_req *sg_req;
514504
515
- if (list_empty(&tdc->pending_sg_req))
516
- return;
517
-
518
- sg_req = list_first_entry(&tdc->pending_sg_req,
519
- typeof(*sg_req), node);
505
+ sg_req = list_first_entry(&tdc->pending_sg_req, typeof(*sg_req), node);
520506 tegra_dma_start(tdc, sg_req);
521507 sg_req->configured = true;
508
+ sg_req->words_xferred = 0;
522509 tdc->busy = true;
523510 }
524511
525512 static void tdc_configure_next_head_desc(struct tegra_dma_channel *tdc)
526513 {
527
- struct tegra_dma_sg_req *hsgreq;
528
- struct tegra_dma_sg_req *hnsgreq;
529
-
530
- if (list_empty(&tdc->pending_sg_req))
531
- return;
514
+ struct tegra_dma_sg_req *hsgreq, *hnsgreq;
532515
533516 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
534517 if (!list_is_last(&hsgreq->node, &tdc->pending_sg_req)) {
535
- hnsgreq = list_first_entry(&hsgreq->node,
536
- typeof(*hnsgreq), node);
518
+ hnsgreq = list_first_entry(&hsgreq->node, typeof(*hnsgreq),
519
+ node);
537520 tegra_dma_configure_for_next(tdc, hnsgreq);
538521 }
539522 }
540523
541
-static inline int get_current_xferred_count(struct tegra_dma_channel *tdc,
542
- struct tegra_dma_sg_req *sg_req, unsigned long status)
524
+static inline unsigned int
525
+get_current_xferred_count(struct tegra_dma_channel *tdc,
526
+ struct tegra_dma_sg_req *sg_req,
527
+ unsigned long status)
543528 {
544529 return sg_req->req_len - (status & TEGRA_APBDMA_STATUS_COUNT_MASK) - 4;
545530 }
546531
547532 static void tegra_dma_abort_all(struct tegra_dma_channel *tdc)
548533 {
549
- struct tegra_dma_sg_req *sgreq;
550534 struct tegra_dma_desc *dma_desc;
535
+ struct tegra_dma_sg_req *sgreq;
551536
552537 while (!list_empty(&tdc->pending_sg_req)) {
553
- sgreq = list_first_entry(&tdc->pending_sg_req,
554
- typeof(*sgreq), node);
538
+ sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
539
+ node);
555540 list_move_tail(&sgreq->node, &tdc->free_sg_req);
556541 if (sgreq->last_sg) {
557542 dma_desc = sgreq->dma_desc;
....@@ -561,7 +546,7 @@
561546 /* Add in cb list if it is not there. */
562547 if (!dma_desc->cb_count)
563548 list_add_tail(&dma_desc->cb_node,
564
- &tdc->cb_desc);
549
+ &tdc->cb_desc);
565550 dma_desc->cb_count++;
566551 }
567552 }
....@@ -569,15 +554,9 @@
569554 }
570555
571556 static bool handle_continuous_head_request(struct tegra_dma_channel *tdc,
572
- struct tegra_dma_sg_req *last_sg_req, bool to_terminate)
557
+ bool to_terminate)
573558 {
574
- struct tegra_dma_sg_req *hsgreq = NULL;
575
-
576
- if (list_empty(&tdc->pending_sg_req)) {
577
- dev_err(tdc2dev(tdc), "Dma is running without req\n");
578
- tegra_dma_stop(tdc);
579
- return false;
580
- }
559
+ struct tegra_dma_sg_req *hsgreq;
581560
582561 /*
583562 * Check that head req on list should be in flight.
....@@ -587,7 +566,8 @@
587566 hsgreq = list_first_entry(&tdc->pending_sg_req, typeof(*hsgreq), node);
588567 if (!hsgreq->configured) {
589568 tegra_dma_stop(tdc);
590
- dev_err(tdc2dev(tdc), "Error in dma transfer, aborting dma\n");
569
+ pm_runtime_put(tdc->tdma->dev);
570
+ dev_err(tdc2dev(tdc), "DMA transfer underflow, aborting DMA\n");
591571 tegra_dma_abort_all(tdc);
592572 return false;
593573 }
....@@ -595,14 +575,15 @@
595575 /* Configure next request */
596576 if (!to_terminate)
597577 tdc_configure_next_head_desc(tdc);
578
+
598579 return true;
599580 }
600581
601582 static void handle_once_dma_done(struct tegra_dma_channel *tdc,
602
- bool to_terminate)
583
+ bool to_terminate)
603584 {
604
- struct tegra_dma_sg_req *sgreq;
605585 struct tegra_dma_desc *dma_desc;
586
+ struct tegra_dma_sg_req *sgreq;
606587
607588 tdc->busy = false;
608589 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
....@@ -621,17 +602,22 @@
621602 list_add_tail(&sgreq->node, &tdc->free_sg_req);
622603
623604 /* Do not start DMA if it is going to be terminate */
624
- if (to_terminate || list_empty(&tdc->pending_sg_req))
605
+ if (to_terminate)
625606 return;
607
+
608
+ if (list_empty(&tdc->pending_sg_req)) {
609
+ pm_runtime_put(tdc->tdma->dev);
610
+ return;
611
+ }
626612
627613 tdc_start_head_req(tdc);
628614 }
629615
630616 static void handle_cont_sngl_cycle_dma_done(struct tegra_dma_channel *tdc,
631
- bool to_terminate)
617
+ bool to_terminate)
632618 {
633
- struct tegra_dma_sg_req *sgreq;
634619 struct tegra_dma_desc *dma_desc;
620
+ struct tegra_dma_sg_req *sgreq;
635621 bool st;
636622
637623 sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq), node);
....@@ -646,32 +632,36 @@
646632 list_add_tail(&dma_desc->cb_node, &tdc->cb_desc);
647633 dma_desc->cb_count++;
648634
635
+ sgreq->words_xferred = 0;
636
+
649637 /* If not last req then put at end of pending list */
650638 if (!list_is_last(&sgreq->node, &tdc->pending_sg_req)) {
651639 list_move_tail(&sgreq->node, &tdc->pending_sg_req);
652640 sgreq->configured = false;
653
- st = handle_continuous_head_request(tdc, sgreq, to_terminate);
641
+ st = handle_continuous_head_request(tdc, to_terminate);
654642 if (!st)
655643 dma_desc->dma_status = DMA_ERROR;
656644 }
657645 }
658646
659
-static void tegra_dma_tasklet(unsigned long data)
647
+static void tegra_dma_tasklet(struct tasklet_struct *t)
660648 {
661
- struct tegra_dma_channel *tdc = (struct tegra_dma_channel *)data;
649
+ struct tegra_dma_channel *tdc = from_tasklet(tdc, t, tasklet);
662650 struct dmaengine_desc_callback cb;
663651 struct tegra_dma_desc *dma_desc;
652
+ unsigned int cb_count;
664653 unsigned long flags;
665
- int cb_count;
666654
667655 spin_lock_irqsave(&tdc->lock, flags);
668656 while (!list_empty(&tdc->cb_desc)) {
669
- dma_desc = list_first_entry(&tdc->cb_desc,
670
- typeof(*dma_desc), cb_node);
657
+ dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
658
+ cb_node);
671659 list_del(&dma_desc->cb_node);
672660 dmaengine_desc_get_callback(&dma_desc->txd, &cb);
673661 cb_count = dma_desc->cb_count;
674662 dma_desc->cb_count = 0;
663
+ trace_tegra_dma_complete_cb(&tdc->dma_chan, cb_count,
664
+ cb.callback);
675665 spin_unlock_irqrestore(&tdc->lock, flags);
676666 while (cb_count--)
677667 dmaengine_desc_callback_invoke(&cb, NULL);
....@@ -683,23 +673,25 @@
683673 static irqreturn_t tegra_dma_isr(int irq, void *dev_id)
684674 {
685675 struct tegra_dma_channel *tdc = dev_id;
686
- unsigned long status;
687
- unsigned long flags;
676
+ u32 status;
688677
689
- spin_lock_irqsave(&tdc->lock, flags);
678
+ spin_lock(&tdc->lock);
690679
680
+ trace_tegra_dma_isr(&tdc->dma_chan, irq);
691681 status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
692682 if (status & TEGRA_APBDMA_STATUS_ISE_EOC) {
693683 tdc_write(tdc, TEGRA_APBDMA_CHAN_STATUS, status);
694684 tdc->isr_handler(tdc, false);
695685 tasklet_schedule(&tdc->tasklet);
696
- spin_unlock_irqrestore(&tdc->lock, flags);
686
+ wake_up_all(&tdc->wq);
687
+ spin_unlock(&tdc->lock);
697688 return IRQ_HANDLED;
698689 }
699690
700
- spin_unlock_irqrestore(&tdc->lock, flags);
701
- dev_info(tdc2dev(tdc),
702
- "Interrupt already served status 0x%08lx\n", status);
691
+ spin_unlock(&tdc->lock);
692
+ dev_info(tdc2dev(tdc), "Interrupt already served status 0x%08x\n",
693
+ status);
694
+
703695 return IRQ_NONE;
704696 }
705697
....@@ -715,6 +707,7 @@
715707 cookie = dma_cookie_assign(&dma_desc->txd);
716708 list_splice_tail_init(&dma_desc->tx_list, &tdc->pending_sg_req);
717709 spin_unlock_irqrestore(&tdc->lock, flags);
710
+
718711 return cookie;
719712 }
720713
....@@ -722,6 +715,7 @@
722715 {
723716 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
724717 unsigned long flags;
718
+ int err;
725719
726720 spin_lock_irqsave(&tdc->lock, flags);
727721 if (list_empty(&tdc->pending_sg_req)) {
....@@ -729,6 +723,12 @@
729723 goto end;
730724 }
731725 if (!tdc->busy) {
726
+ err = pm_runtime_resume_and_get(tdc->tdma->dev);
727
+ if (err < 0) {
728
+ dev_err(tdc2dev(tdc), "Failed to enable DMA\n");
729
+ goto end;
730
+ }
731
+
732732 tdc_start_head_req(tdc);
733733
734734 /* Continuous single mode: Configure next req */
....@@ -748,11 +748,10 @@
748748 static int tegra_dma_terminate_all(struct dma_chan *dc)
749749 {
750750 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
751
- struct tegra_dma_sg_req *sgreq;
752751 struct tegra_dma_desc *dma_desc;
752
+ struct tegra_dma_sg_req *sgreq;
753753 unsigned long flags;
754
- unsigned long status;
755
- unsigned long wcount;
754
+ u32 status, wcount;
756755 bool was_busy;
757756
758757 spin_lock_irqsave(&tdc->lock, flags);
....@@ -778,28 +777,127 @@
778777 tegra_dma_stop(tdc);
779778
780779 if (!list_empty(&tdc->pending_sg_req) && was_busy) {
781
- sgreq = list_first_entry(&tdc->pending_sg_req,
782
- typeof(*sgreq), node);
780
+ sgreq = list_first_entry(&tdc->pending_sg_req, typeof(*sgreq),
781
+ node);
783782 sgreq->dma_desc->bytes_transferred +=
784783 get_current_xferred_count(tdc, sgreq, wcount);
785784 }
786785 tegra_dma_resume(tdc);
787786
787
+ pm_runtime_put(tdc->tdma->dev);
788
+ wake_up_all(&tdc->wq);
789
+
788790 skip_dma_stop:
789791 tegra_dma_abort_all(tdc);
790792
791793 while (!list_empty(&tdc->cb_desc)) {
792
- dma_desc = list_first_entry(&tdc->cb_desc,
793
- typeof(*dma_desc), cb_node);
794
+ dma_desc = list_first_entry(&tdc->cb_desc, typeof(*dma_desc),
795
+ cb_node);
794796 list_del(&dma_desc->cb_node);
795797 dma_desc->cb_count = 0;
796798 }
797799 spin_unlock_irqrestore(&tdc->lock, flags);
800
+
798801 return 0;
799802 }
800803
804
+static bool tegra_dma_eoc_interrupt_deasserted(struct tegra_dma_channel *tdc)
805
+{
806
+ unsigned long flags;
807
+ u32 status;
808
+
809
+ spin_lock_irqsave(&tdc->lock, flags);
810
+ status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
811
+ spin_unlock_irqrestore(&tdc->lock, flags);
812
+
813
+ return !(status & TEGRA_APBDMA_STATUS_ISE_EOC);
814
+}
815
+
816
+static void tegra_dma_synchronize(struct dma_chan *dc)
817
+{
818
+ struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
819
+ int err;
820
+
821
+ err = pm_runtime_resume_and_get(tdc->tdma->dev);
822
+ if (err < 0) {
823
+ dev_err(tdc2dev(tdc), "Failed to synchronize DMA: %d\n", err);
824
+ return;
825
+ }
826
+
827
+ /*
828
+ * CPU, which handles interrupt, could be busy in
829
+ * uninterruptible state, in this case sibling CPU
830
+ * should wait until interrupt is handled.
831
+ */
832
+ wait_event(tdc->wq, tegra_dma_eoc_interrupt_deasserted(tdc));
833
+
834
+ tasklet_kill(&tdc->tasklet);
835
+
836
+ pm_runtime_put(tdc->tdma->dev);
837
+}
838
+
839
+static unsigned int tegra_dma_sg_bytes_xferred(struct tegra_dma_channel *tdc,
840
+ struct tegra_dma_sg_req *sg_req)
841
+{
842
+ u32 status, wcount = 0;
843
+
844
+ if (!list_is_first(&sg_req->node, &tdc->pending_sg_req))
845
+ return 0;
846
+
847
+ if (tdc->tdma->chip_data->support_separate_wcount_reg)
848
+ wcount = tdc_read(tdc, TEGRA_APBDMA_CHAN_WORD_TRANSFER);
849
+
850
+ status = tdc_read(tdc, TEGRA_APBDMA_CHAN_STATUS);
851
+
852
+ if (!tdc->tdma->chip_data->support_separate_wcount_reg)
853
+ wcount = status;
854
+
855
+ if (status & TEGRA_APBDMA_STATUS_ISE_EOC)
856
+ return sg_req->req_len;
857
+
858
+ wcount = get_current_xferred_count(tdc, sg_req, wcount);
859
+
860
+ if (!wcount) {
861
+ /*
862
+ * If wcount wasn't ever polled for this SG before, then
863
+ * simply assume that transfer hasn't started yet.
864
+ *
865
+ * Otherwise it's the end of the transfer.
866
+ *
867
+ * The alternative would be to poll the status register
868
+ * until EOC bit is set or wcount goes UP. That's so
869
+ * because EOC bit is getting set only after the last
870
+ * burst's completion and counter is less than the actual
871
+ * transfer size by 4 bytes. The counter value wraps around
872
+ * in a cyclic mode before EOC is set(!), so we can't easily
873
+ * distinguish start of transfer from its end.
874
+ */
875
+ if (sg_req->words_xferred)
876
+ wcount = sg_req->req_len - 4;
877
+
878
+ } else if (wcount < sg_req->words_xferred) {
879
+ /*
880
+ * This case will never happen for a non-cyclic transfer.
881
+ *
882
+ * For a cyclic transfer, although it is possible for the
883
+ * next transfer to have already started (resetting the word
884
+ * count), this case should still not happen because we should
885
+ * have detected that the EOC bit is set and hence the transfer
886
+ * was completed.
887
+ */
888
+ WARN_ON_ONCE(1);
889
+
890
+ wcount = sg_req->req_len - 4;
891
+ } else {
892
+ sg_req->words_xferred = wcount;
893
+ }
894
+
895
+ return wcount;
896
+}
897
+
801898 static enum dma_status tegra_dma_tx_status(struct dma_chan *dc,
802
- dma_cookie_t cookie, struct dma_tx_state *txstate)
899
+ dma_cookie_t cookie,
900
+ struct dma_tx_state *txstate)
803901 {
804902 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
805903 struct tegra_dma_desc *dma_desc;
....@@ -807,6 +905,7 @@
807905 enum dma_status ret;
808906 unsigned long flags;
809907 unsigned int residual;
908
+ unsigned int bytes = 0;
810909
811910 ret = dma_cookie_status(dc, cookie, txstate);
812911 if (ret == DMA_COMPLETE)
....@@ -826,6 +925,7 @@
826925 list_for_each_entry(sg_req, &tdc->pending_sg_req, node) {
827926 dma_desc = sg_req->dma_desc;
828927 if (dma_desc->txd.cookie == cookie) {
928
+ bytes = tegra_dma_sg_bytes_xferred(tdc, sg_req);
829929 ret = dma_desc->dma_status;
830930 goto found;
831931 }
....@@ -837,17 +937,19 @@
837937 found:
838938 if (dma_desc && txstate) {
839939 residual = dma_desc->bytes_requested -
840
- (dma_desc->bytes_transferred %
940
+ ((dma_desc->bytes_transferred + bytes) %
841941 dma_desc->bytes_requested);
842942 dma_set_residue(txstate, residual);
843943 }
844944
945
+ trace_tegra_dma_tx_status(&tdc->dma_chan, cookie, txstate);
845946 spin_unlock_irqrestore(&tdc->lock, flags);
947
+
846948 return ret;
847949 }
848950
849
-static inline int get_bus_width(struct tegra_dma_channel *tdc,
850
- enum dma_slave_buswidth slave_bw)
951
+static inline unsigned int get_bus_width(struct tegra_dma_channel *tdc,
952
+ enum dma_slave_buswidth slave_bw)
851953 {
852954 switch (slave_bw) {
853955 case DMA_SLAVE_BUSWIDTH_1_BYTE:
....@@ -860,16 +962,17 @@
860962 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_64;
861963 default:
862964 dev_warn(tdc2dev(tdc),
863
- "slave bw is not supported, using 32bits\n");
965
+ "slave bw is not supported, using 32bits\n");
864966 return TEGRA_APBDMA_APBSEQ_BUS_WIDTH_32;
865967 }
866968 }
867969
868
-static inline int get_burst_size(struct tegra_dma_channel *tdc,
869
- u32 burst_size, enum dma_slave_buswidth slave_bw, int len)
970
+static inline unsigned int get_burst_size(struct tegra_dma_channel *tdc,
971
+ u32 burst_size,
972
+ enum dma_slave_buswidth slave_bw,
973
+ u32 len)
870974 {
871
- int burst_byte;
872
- int burst_ahb_width;
975
+ unsigned int burst_byte, burst_ahb_width;
873976
874977 /*
875978 * burst_size from client is in terms of the bus_width.
....@@ -896,9 +999,12 @@
896999 }
8971000
8981001 static int get_transfer_param(struct tegra_dma_channel *tdc,
899
- enum dma_transfer_direction direction, unsigned long *apb_addr,
900
- unsigned long *apb_seq, unsigned long *csr, unsigned int *burst_size,
901
- enum dma_slave_buswidth *slave_bw)
1002
+ enum dma_transfer_direction direction,
1003
+ u32 *apb_addr,
1004
+ u32 *apb_seq,
1005
+ u32 *csr,
1006
+ unsigned int *burst_size,
1007
+ enum dma_slave_buswidth *slave_bw)
9021008 {
9031009 switch (direction) {
9041010 case DMA_MEM_TO_DEV:
....@@ -918,14 +1024,16 @@
9181024 return 0;
9191025
9201026 default:
921
- dev_err(tdc2dev(tdc), "Dma direction is not supported\n");
922
- return -EINVAL;
1027
+ dev_err(tdc2dev(tdc), "DMA direction is not supported\n");
1028
+ break;
9231029 }
1030
+
9241031 return -EINVAL;
9251032 }
9261033
9271034 static void tegra_dma_prep_wcount(struct tegra_dma_channel *tdc,
928
- struct tegra_dma_channel_regs *ch_regs, u32 len)
1035
+ struct tegra_dma_channel_regs *ch_regs,
1036
+ u32 len)
9291037 {
9301038 u32 len_field = (len - 4) & 0xFFFC;
9311039
....@@ -935,23 +1043,26 @@
9351043 ch_regs->csr |= len_field;
9361044 }
9371045
938
-static struct dma_async_tx_descriptor *tegra_dma_prep_slave_sg(
939
- struct dma_chan *dc, struct scatterlist *sgl, unsigned int sg_len,
940
- enum dma_transfer_direction direction, unsigned long flags,
941
- void *context)
1046
+static struct dma_async_tx_descriptor *
1047
+tegra_dma_prep_slave_sg(struct dma_chan *dc,
1048
+ struct scatterlist *sgl,
1049
+ unsigned int sg_len,
1050
+ enum dma_transfer_direction direction,
1051
+ unsigned long flags,
1052
+ void *context)
9421053 {
9431054 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
944
- struct tegra_dma_desc *dma_desc;
945
- unsigned int i;
946
- struct scatterlist *sg;
947
- unsigned long csr, ahb_seq, apb_ptr, apb_seq;
948
- struct list_head req_list;
949
- struct tegra_dma_sg_req *sg_req = NULL;
950
- u32 burst_size;
1055
+ struct tegra_dma_sg_req *sg_req = NULL;
1056
+ u32 csr, ahb_seq, apb_ptr, apb_seq;
9511057 enum dma_slave_buswidth slave_bw;
1058
+ struct tegra_dma_desc *dma_desc;
1059
+ struct list_head req_list;
1060
+ struct scatterlist *sg;
1061
+ unsigned int burst_size;
1062
+ unsigned int i;
9521063
9531064 if (!tdc->config_init) {
954
- dev_err(tdc2dev(tdc), "dma channel is not configured\n");
1065
+ dev_err(tdc2dev(tdc), "DMA channel is not configured\n");
9551066 return NULL;
9561067 }
9571068 if (sg_len < 1) {
....@@ -960,7 +1071,7 @@
9601071 }
9611072
9621073 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
963
- &burst_size, &slave_bw) < 0)
1074
+ &burst_size, &slave_bw) < 0)
9641075 return NULL;
9651076
9661077 INIT_LIST_HEAD(&req_list);
....@@ -988,7 +1099,7 @@
9881099
9891100 dma_desc = tegra_dma_desc_get(tdc);
9901101 if (!dma_desc) {
991
- dev_err(tdc2dev(tdc), "Dma descriptors not available\n");
1102
+ dev_err(tdc2dev(tdc), "DMA descriptors not available\n");
9921103 return NULL;
9931104 }
9941105 INIT_LIST_HEAD(&dma_desc->tx_list);
....@@ -1006,16 +1117,16 @@
10061117 len = sg_dma_len(sg);
10071118
10081119 if ((len & 3) || (mem & 3) ||
1009
- (len > tdc->tdma->chip_data->max_dma_count)) {
1120
+ len > tdc->tdma->chip_data->max_dma_count) {
10101121 dev_err(tdc2dev(tdc),
1011
- "Dma length/memory address is not supported\n");
1122
+ "DMA length/memory address is not supported\n");
10121123 tegra_dma_desc_put(tdc, dma_desc);
10131124 return NULL;
10141125 }
10151126
10161127 sg_req = tegra_dma_sg_req_get(tdc);
10171128 if (!sg_req) {
1018
- dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
1129
+ dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
10191130 tegra_dma_desc_put(tdc, dma_desc);
10201131 return NULL;
10211132 }
....@@ -1058,20 +1169,21 @@
10581169 return &dma_desc->txd;
10591170 }
10601171
1061
-static struct dma_async_tx_descriptor *tegra_dma_prep_dma_cyclic(
1062
- struct dma_chan *dc, dma_addr_t buf_addr, size_t buf_len,
1063
- size_t period_len, enum dma_transfer_direction direction,
1064
- unsigned long flags)
1172
+static struct dma_async_tx_descriptor *
1173
+tegra_dma_prep_dma_cyclic(struct dma_chan *dc, dma_addr_t buf_addr,
1174
+ size_t buf_len,
1175
+ size_t period_len,
1176
+ enum dma_transfer_direction direction,
1177
+ unsigned long flags)
10651178 {
10661179 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1067
- struct tegra_dma_desc *dma_desc = NULL;
10681180 struct tegra_dma_sg_req *sg_req = NULL;
1069
- unsigned long csr, ahb_seq, apb_ptr, apb_seq;
1070
- int len;
1071
- size_t remain_len;
1072
- dma_addr_t mem = buf_addr;
1073
- u32 burst_size;
1181
+ u32 csr, ahb_seq, apb_ptr, apb_seq;
10741182 enum dma_slave_buswidth slave_bw;
1183
+ struct tegra_dma_desc *dma_desc;
1184
+ dma_addr_t mem = buf_addr;
1185
+ unsigned int burst_size;
1186
+ size_t len, remain_len;
10751187
10761188 if (!buf_len || !period_len) {
10771189 dev_err(tdc2dev(tdc), "Invalid buffer/period len\n");
....@@ -1090,7 +1202,7 @@
10901202 * terminating the DMA.
10911203 */
10921204 if (tdc->busy) {
1093
- dev_err(tdc2dev(tdc), "Request not allowed when dma running\n");
1205
+ dev_err(tdc2dev(tdc), "Request not allowed when DMA running\n");
10941206 return NULL;
10951207 }
10961208
....@@ -1105,13 +1217,13 @@
11051217
11061218 len = period_len;
11071219 if ((len & 3) || (buf_addr & 3) ||
1108
- (len > tdc->tdma->chip_data->max_dma_count)) {
1220
+ len > tdc->tdma->chip_data->max_dma_count) {
11091221 dev_err(tdc2dev(tdc), "Req len/mem address is not correct\n");
11101222 return NULL;
11111223 }
11121224
11131225 if (get_transfer_param(tdc, direction, &apb_ptr, &apb_seq, &csr,
1114
- &burst_size, &slave_bw) < 0)
1226
+ &burst_size, &slave_bw) < 0)
11151227 return NULL;
11161228
11171229 ahb_seq = TEGRA_APBDMA_AHBSEQ_INTR_ENB;
....@@ -1151,7 +1263,7 @@
11511263 while (remain_len) {
11521264 sg_req = tegra_dma_sg_req_get(tdc);
11531265 if (!sg_req) {
1154
- dev_err(tdc2dev(tdc), "Dma sg-req not available\n");
1266
+ dev_err(tdc2dev(tdc), "DMA sg-req not available\n");
11551267 tegra_dma_desc_put(tdc, dma_desc);
11561268 return NULL;
11571269 }
....@@ -1197,15 +1309,8 @@
11971309 static int tegra_dma_alloc_chan_resources(struct dma_chan *dc)
11981310 {
11991311 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1200
- struct tegra_dma *tdma = tdc->tdma;
1201
- int ret;
12021312
12031313 dma_cookie_init(&tdc->dma_chan);
1204
- tdc->config_init = false;
1205
-
1206
- ret = pm_runtime_get_sync(tdma->dev);
1207
- if (ret < 0)
1208
- return ret;
12091314
12101315 return 0;
12111316 }
....@@ -1213,12 +1318,10 @@
12131318 static void tegra_dma_free_chan_resources(struct dma_chan *dc)
12141319 {
12151320 struct tegra_dma_channel *tdc = to_tegra_dma_chan(dc);
1216
- struct tegra_dma *tdma = tdc->tdma;
12171321 struct tegra_dma_desc *dma_desc;
12181322 struct tegra_dma_sg_req *sg_req;
12191323 struct list_head dma_desc_list;
12201324 struct list_head sg_req_list;
1221
- unsigned long flags;
12221325
12231326 INIT_LIST_HEAD(&dma_desc_list);
12241327 INIT_LIST_HEAD(&sg_req_list);
....@@ -1226,19 +1329,18 @@
12261329 dev_dbg(tdc2dev(tdc), "Freeing channel %d\n", tdc->id);
12271330
12281331 tegra_dma_terminate_all(dc);
1332
+ tasklet_kill(&tdc->tasklet);
12291333
1230
- spin_lock_irqsave(&tdc->lock, flags);
12311334 list_splice_init(&tdc->pending_sg_req, &sg_req_list);
12321335 list_splice_init(&tdc->free_sg_req, &sg_req_list);
12331336 list_splice_init(&tdc->free_dma_desc, &dma_desc_list);
12341337 INIT_LIST_HEAD(&tdc->cb_desc);
12351338 tdc->config_init = false;
12361339 tdc->isr_handler = NULL;
1237
- spin_unlock_irqrestore(&tdc->lock, flags);
12381340
12391341 while (!list_empty(&dma_desc_list)) {
1240
- dma_desc = list_first_entry(&dma_desc_list,
1241
- typeof(*dma_desc), node);
1342
+ dma_desc = list_first_entry(&dma_desc_list, typeof(*dma_desc),
1343
+ node);
12421344 list_del(&dma_desc->node);
12431345 kfree(dma_desc);
12441346 }
....@@ -1248,7 +1350,6 @@
12481350 list_del(&sg_req->node);
12491351 kfree(sg_req);
12501352 }
1251
- pm_runtime_put(tdma->dev);
12521353
12531354 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
12541355 }
....@@ -1257,8 +1358,8 @@
12571358 struct of_dma *ofdma)
12581359 {
12591360 struct tegra_dma *tdma = ofdma->of_dma_data;
1260
- struct dma_chan *chan;
12611361 struct tegra_dma_channel *tdc;
1362
+ struct dma_chan *chan;
12621363
12631364 if (dma_spec->args[0] > TEGRA_APBDMA_CSR_REQ_SEL_MASK) {
12641365 dev_err(tdma->dev, "Invalid slave id: %d\n", dma_spec->args[0]);
....@@ -1311,22 +1412,48 @@
13111412 .support_separate_wcount_reg = true,
13121413 };
13131414
1314
-static int tegra_dma_probe(struct platform_device *pdev)
1415
+static int tegra_dma_init_hw(struct tegra_dma *tdma)
13151416 {
1316
- struct resource *res;
1317
- struct tegra_dma *tdma;
1318
- int ret;
1319
- int i;
1320
- const struct tegra_dma_chip_data *cdata;
1417
+ int err;
13211418
1322
- cdata = of_device_get_match_data(&pdev->dev);
1323
- if (!cdata) {
1324
- dev_err(&pdev->dev, "Error: No device match data found\n");
1325
- return -ENODEV;
1419
+ err = reset_control_assert(tdma->rst);
1420
+ if (err) {
1421
+ dev_err(tdma->dev, "failed to assert reset: %d\n", err);
1422
+ return err;
13261423 }
13271424
1328
- tdma = devm_kzalloc(&pdev->dev, sizeof(*tdma) + cdata->nr_channels *
1329
- sizeof(struct tegra_dma_channel), GFP_KERNEL);
1425
+ err = clk_enable(tdma->dma_clk);
1426
+ if (err) {
1427
+ dev_err(tdma->dev, "failed to enable clk: %d\n", err);
1428
+ return err;
1429
+ }
1430
+
1431
+ /* reset DMA controller */
1432
+ udelay(2);
1433
+ reset_control_deassert(tdma->rst);
1434
+
1435
+ /* enable global DMA registers */
1436
+ tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
1437
+ tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
1438
+ tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFF);
1439
+
1440
+ clk_disable(tdma->dma_clk);
1441
+
1442
+ return 0;
1443
+}
1444
+
1445
+static int tegra_dma_probe(struct platform_device *pdev)
1446
+{
1447
+ const struct tegra_dma_chip_data *cdata;
1448
+ struct tegra_dma *tdma;
1449
+ unsigned int i;
1450
+ size_t size;
1451
+ int ret;
1452
+
1453
+ cdata = of_device_get_match_data(&pdev->dev);
1454
+ size = struct_size(tdma, channels, cdata->nr_channels);
1455
+
1456
+ tdma = devm_kzalloc(&pdev->dev, size, GFP_KERNEL);
13301457 if (!tdma)
13311458 return -ENOMEM;
13321459
....@@ -1334,8 +1461,7 @@
13341461 tdma->chip_data = cdata;
13351462 platform_set_drvdata(pdev, tdma);
13361463
1337
- res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1338
- tdma->base_addr = devm_ioremap_resource(&pdev->dev, res);
1464
+ tdma->base_addr = devm_platform_ioremap_resource(pdev, 0);
13391465 if (IS_ERR(tdma->base_addr))
13401466 return PTR_ERR(tdma->base_addr);
13411467
....@@ -1353,64 +1479,53 @@
13531479
13541480 spin_lock_init(&tdma->global_lock);
13551481
1356
- pm_runtime_enable(&pdev->dev);
1357
- if (!pm_runtime_enabled(&pdev->dev))
1358
- ret = tegra_dma_runtime_resume(&pdev->dev);
1359
- else
1360
- ret = pm_runtime_get_sync(&pdev->dev);
1361
-
1362
- if (ret < 0) {
1363
- pm_runtime_disable(&pdev->dev);
1482
+ ret = clk_prepare(tdma->dma_clk);
1483
+ if (ret)
13641484 return ret;
1365
- }
13661485
1367
- /* Reset DMA controller */
1368
- reset_control_assert(tdma->rst);
1369
- udelay(2);
1370
- reset_control_deassert(tdma->rst);
1486
+ ret = tegra_dma_init_hw(tdma);
1487
+ if (ret)
1488
+ goto err_clk_unprepare;
13711489
1372
- /* Enable global DMA registers */
1373
- tdma_write(tdma, TEGRA_APBDMA_GENERAL, TEGRA_APBDMA_GENERAL_ENABLE);
1374
- tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
1375
- tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
1376
-
1377
- pm_runtime_put(&pdev->dev);
1490
+ pm_runtime_irq_safe(&pdev->dev);
1491
+ pm_runtime_enable(&pdev->dev);
13781492
13791493 INIT_LIST_HEAD(&tdma->dma_dev.channels);
13801494 for (i = 0; i < cdata->nr_channels; i++) {
13811495 struct tegra_dma_channel *tdc = &tdma->channels[i];
1496
+ int irq;
13821497
13831498 tdc->chan_addr = tdma->base_addr +
13841499 TEGRA_APBDMA_CHANNEL_BASE_ADD_OFFSET +
13851500 (i * cdata->channel_reg_size);
13861501
1387
- res = platform_get_resource(pdev, IORESOURCE_IRQ, i);
1388
- if (!res) {
1389
- ret = -EINVAL;
1390
- dev_err(&pdev->dev, "No irq resource for chan %d\n", i);
1391
- goto err_irq;
1502
+ irq = platform_get_irq(pdev, i);
1503
+ if (irq < 0) {
1504
+ ret = irq;
1505
+ goto err_pm_disable;
13921506 }
1393
- tdc->irq = res->start;
1507
+
13941508 snprintf(tdc->name, sizeof(tdc->name), "apbdma.%d", i);
1395
- ret = request_irq(tdc->irq, tegra_dma_isr, 0, tdc->name, tdc);
1509
+ ret = devm_request_irq(&pdev->dev, irq, tegra_dma_isr, 0,
1510
+ tdc->name, tdc);
13961511 if (ret) {
13971512 dev_err(&pdev->dev,
13981513 "request_irq failed with err %d channel %d\n",
13991514 ret, i);
1400
- goto err_irq;
1515
+ goto err_pm_disable;
14011516 }
14021517
14031518 tdc->dma_chan.device = &tdma->dma_dev;
14041519 dma_cookie_init(&tdc->dma_chan);
14051520 list_add_tail(&tdc->dma_chan.device_node,
1406
- &tdma->dma_dev.channels);
1521
+ &tdma->dma_dev.channels);
14071522 tdc->tdma = tdma;
14081523 tdc->id = i;
14091524 tdc->slave_id = TEGRA_APBDMA_SLAVE_ID_INVALID;
14101525
1411
- tasklet_init(&tdc->tasklet, tegra_dma_tasklet,
1412
- (unsigned long)tdc);
1526
+ tasklet_setup(&tdc->tasklet, tegra_dma_tasklet);
14131527 spin_lock_init(&tdc->lock);
1528
+ init_waitqueue_head(&tdc->wq);
14141529
14151530 INIT_LIST_HEAD(&tdc->pending_sg_req);
14161531 INIT_LIST_HEAD(&tdc->free_sg_req);
....@@ -1439,14 +1554,10 @@
14391554 BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
14401555 BIT(DMA_SLAVE_BUSWIDTH_8_BYTES);
14411556 tdma->dma_dev.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV);
1442
- /*
1443
- * XXX The hardware appears to support
1444
- * DMA_RESIDUE_GRANULARITY_BURST-level reporting, but it's
1445
- * only used by this driver during tegra_dma_terminate_all()
1446
- */
1447
- tdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_SEGMENT;
1557
+ tdma->dma_dev.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST;
14481558 tdma->dma_dev.device_config = tegra_dma_slave_config;
14491559 tdma->dma_dev.device_terminate_all = tegra_dma_terminate_all;
1560
+ tdma->dma_dev.device_synchronize = tegra_dma_synchronize;
14501561 tdma->dma_dev.device_tx_status = tegra_dma_tx_status;
14511562 tdma->dma_dev.device_issue_pending = tegra_dma_issue_pending;
14521563
....@@ -1454,7 +1565,7 @@
14541565 if (ret < 0) {
14551566 dev_err(&pdev->dev,
14561567 "Tegra20 APB DMA driver registration failed %d\n", ret);
1457
- goto err_irq;
1568
+ goto err_pm_disable;
14581569 }
14591570
14601571 ret = of_dma_controller_register(pdev->dev.of_node,
....@@ -1465,118 +1576,92 @@
14651576 goto err_unregister_dma_dev;
14661577 }
14671578
1468
- dev_info(&pdev->dev, "Tegra20 APB DMA driver register %d channels\n",
1469
- cdata->nr_channels);
1579
+ dev_info(&pdev->dev, "Tegra20 APB DMA driver registered %u channels\n",
1580
+ cdata->nr_channels);
1581
+
14701582 return 0;
14711583
14721584 err_unregister_dma_dev:
14731585 dma_async_device_unregister(&tdma->dma_dev);
1474
-err_irq:
1475
- while (--i >= 0) {
1476
- struct tegra_dma_channel *tdc = &tdma->channels[i];
14771586
1478
- free_irq(tdc->irq, tdc);
1479
- tasklet_kill(&tdc->tasklet);
1480
- }
1481
-
1587
+err_pm_disable:
14821588 pm_runtime_disable(&pdev->dev);
1483
- if (!pm_runtime_status_suspended(&pdev->dev))
1484
- tegra_dma_runtime_suspend(&pdev->dev);
1589
+
1590
+err_clk_unprepare:
1591
+ clk_unprepare(tdma->dma_clk);
1592
+
14851593 return ret;
14861594 }
14871595
14881596 static int tegra_dma_remove(struct platform_device *pdev)
14891597 {
14901598 struct tegra_dma *tdma = platform_get_drvdata(pdev);
1491
- int i;
1492
- struct tegra_dma_channel *tdc;
14931599
1600
+ of_dma_controller_free(pdev->dev.of_node);
14941601 dma_async_device_unregister(&tdma->dma_dev);
1495
-
1496
- for (i = 0; i < tdma->chip_data->nr_channels; ++i) {
1497
- tdc = &tdma->channels[i];
1498
- free_irq(tdc->irq, tdc);
1499
- tasklet_kill(&tdc->tasklet);
1500
- }
1501
-
15021602 pm_runtime_disable(&pdev->dev);
1503
- if (!pm_runtime_status_suspended(&pdev->dev))
1504
- tegra_dma_runtime_suspend(&pdev->dev);
1603
+ clk_unprepare(tdma->dma_clk);
15051604
15061605 return 0;
15071606 }
15081607
1509
-static int tegra_dma_runtime_suspend(struct device *dev)
1608
+static int __maybe_unused tegra_dma_runtime_suspend(struct device *dev)
15101609 {
15111610 struct tegra_dma *tdma = dev_get_drvdata(dev);
1512
- int i;
15131611
1514
- tdma->reg_gen = tdma_read(tdma, TEGRA_APBDMA_GENERAL);
1515
- for (i = 0; i < tdma->chip_data->nr_channels; i++) {
1516
- struct tegra_dma_channel *tdc = &tdma->channels[i];
1517
- struct tegra_dma_channel_regs *ch_reg = &tdc->channel_reg;
1518
-
1519
- /* Only save the state of DMA channels that are in use */
1520
- if (!tdc->config_init)
1521
- continue;
1522
-
1523
- ch_reg->csr = tdc_read(tdc, TEGRA_APBDMA_CHAN_CSR);
1524
- ch_reg->ahb_ptr = tdc_read(tdc, TEGRA_APBDMA_CHAN_AHBPTR);
1525
- ch_reg->apb_ptr = tdc_read(tdc, TEGRA_APBDMA_CHAN_APBPTR);
1526
- ch_reg->ahb_seq = tdc_read(tdc, TEGRA_APBDMA_CHAN_AHBSEQ);
1527
- ch_reg->apb_seq = tdc_read(tdc, TEGRA_APBDMA_CHAN_APBSEQ);
1528
- if (tdma->chip_data->support_separate_wcount_reg)
1529
- ch_reg->wcount = tdc_read(tdc,
1530
- TEGRA_APBDMA_CHAN_WCOUNT);
1531
- }
1532
-
1533
- clk_disable_unprepare(tdma->dma_clk);
1612
+ clk_disable(tdma->dma_clk);
15341613
15351614 return 0;
15361615 }
15371616
1538
-static int tegra_dma_runtime_resume(struct device *dev)
1617
+static int __maybe_unused tegra_dma_runtime_resume(struct device *dev)
15391618 {
15401619 struct tegra_dma *tdma = dev_get_drvdata(dev);
1541
- int i, ret;
15421620
1543
- ret = clk_prepare_enable(tdma->dma_clk);
1544
- if (ret < 0) {
1545
- dev_err(dev, "clk_enable failed: %d\n", ret);
1546
- return ret;
1547
- }
1621
+ return clk_enable(tdma->dma_clk);
1622
+}
15481623
1549
- tdma_write(tdma, TEGRA_APBDMA_GENERAL, tdma->reg_gen);
1550
- tdma_write(tdma, TEGRA_APBDMA_CONTROL, 0);
1551
- tdma_write(tdma, TEGRA_APBDMA_IRQ_MASK_SET, 0xFFFFFFFFul);
1624
+static int __maybe_unused tegra_dma_dev_suspend(struct device *dev)
1625
+{
1626
+ struct tegra_dma *tdma = dev_get_drvdata(dev);
1627
+ unsigned long flags;
1628
+ unsigned int i;
1629
+ bool busy;
15521630
15531631 for (i = 0; i < tdma->chip_data->nr_channels; i++) {
15541632 struct tegra_dma_channel *tdc = &tdma->channels[i];
1555
- struct tegra_dma_channel_regs *ch_reg = &tdc->channel_reg;
15561633
1557
- /* Only restore the state of DMA channels that are in use */
1558
- if (!tdc->config_init)
1559
- continue;
1634
+ tasklet_kill(&tdc->tasklet);
15601635
1561
- if (tdma->chip_data->support_separate_wcount_reg)
1562
- tdc_write(tdc, TEGRA_APBDMA_CHAN_WCOUNT,
1563
- ch_reg->wcount);
1564
- tdc_write(tdc, TEGRA_APBDMA_CHAN_APBSEQ, ch_reg->apb_seq);
1565
- tdc_write(tdc, TEGRA_APBDMA_CHAN_APBPTR, ch_reg->apb_ptr);
1566
- tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBSEQ, ch_reg->ahb_seq);
1567
- tdc_write(tdc, TEGRA_APBDMA_CHAN_AHBPTR, ch_reg->ahb_ptr);
1568
- tdc_write(tdc, TEGRA_APBDMA_CHAN_CSR,
1569
- (ch_reg->csr & ~TEGRA_APBDMA_CSR_ENB));
1636
+ spin_lock_irqsave(&tdc->lock, flags);
1637
+ busy = tdc->busy;
1638
+ spin_unlock_irqrestore(&tdc->lock, flags);
1639
+
1640
+ if (busy) {
1641
+ dev_err(tdma->dev, "channel %u busy\n", i);
1642
+ return -EBUSY;
1643
+ }
15701644 }
15711645
1572
- return 0;
1646
+ return pm_runtime_force_suspend(dev);
1647
+}
1648
+
1649
+static int __maybe_unused tegra_dma_dev_resume(struct device *dev)
1650
+{
1651
+ struct tegra_dma *tdma = dev_get_drvdata(dev);
1652
+ int err;
1653
+
1654
+ err = tegra_dma_init_hw(tdma);
1655
+ if (err)
1656
+ return err;
1657
+
1658
+ return pm_runtime_force_resume(dev);
15731659 }
15741660
15751661 static const struct dev_pm_ops tegra_dma_dev_pm_ops = {
15761662 SET_RUNTIME_PM_OPS(tegra_dma_runtime_suspend, tegra_dma_runtime_resume,
15771663 NULL)
1578
- SET_SYSTEM_SLEEP_PM_OPS(pm_runtime_force_suspend,
1579
- pm_runtime_force_resume)
1664
+ SET_SYSTEM_SLEEP_PM_OPS(tegra_dma_dev_suspend, tegra_dma_dev_resume)
15801665 };
15811666
15821667 static const struct of_device_id tegra_dma_of_match[] = {
....@@ -1609,7 +1694,6 @@
16091694
16101695 module_platform_driver(tegra_dmac_driver);
16111696
1612
-MODULE_ALIAS("platform:tegra20-apbdma");
16131697 MODULE_DESCRIPTION("NVIDIA Tegra APB DMA Controller driver");
16141698 MODULE_AUTHOR("Laxman Dewangan <ldewangan@nvidia.com>");
16151699 MODULE_LICENSE("GPL v2");