Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1 | /* |
Heikki Krogerus | b801479 | 2012-10-18 17:34:08 +0300 | [diff] [blame] | 2 | * Core driver for the Synopsys DesignWare DMA Controller |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 2007-2008 Atmel Corporation |
Viresh Kumar | aecb7b6 | 2011-05-24 14:04:09 +0530 | [diff] [blame] | 5 | * Copyright (C) 2010-2011 ST Microelectronics |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 6 | * Copyright (C) 2013 Intel Corporation |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 7 | * |
| 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License version 2 as |
| 10 | * published by the Free Software Foundation. |
| 11 | */ |
Heikki Krogerus | b801479 | 2012-10-18 17:34:08 +0300 | [diff] [blame] | 12 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 13 | #include <linux/bitops.h> |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 14 | #include <linux/delay.h> |
| 15 | #include <linux/dmaengine.h> |
| 16 | #include <linux/dma-mapping.h> |
Andy Shevchenko | f8122a8 | 2013-01-16 15:48:50 +0200 | [diff] [blame] | 17 | #include <linux/dmapool.h> |
Thierry Reding | 7331205 | 2013-01-21 11:09:00 +0100 | [diff] [blame] | 18 | #include <linux/err.h> |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 19 | #include <linux/init.h> |
| 20 | #include <linux/interrupt.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/mm.h> |
| 23 | #include <linux/module.h> |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 24 | #include <linux/slab.h> |
Andy Shevchenko | bb32baf | 2014-11-05 18:34:48 +0200 | [diff] [blame] | 25 | #include <linux/pm_runtime.h> |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 26 | |
Andy Shevchenko | 61a7649 | 2013-06-05 15:26:44 +0300 | [diff] [blame] | 27 | #include "../dmaengine.h" |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 28 | #include "internal.h" |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 29 | |
| 30 | /* |
| 31 | * This supports the Synopsys "DesignWare AHB Central DMA Controller", |
| 32 | * (DW_ahb_dmac) which is used with various AMBA 2.0 systems (not all |
| 33 | * of which use ARM any more). See the "Databook" from Synopsys for |
| 34 | * information beyond what licensees probably provide. |
| 35 | * |
Andy Shevchenko | dd5720b | 2014-02-12 11:16:17 +0200 | [diff] [blame] | 36 | * The driver has been tested with the Atmel AT32AP7000, which does not |
| 37 | * support descriptor writeback. |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 38 | */ |
| 39 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 40 | #define DWC_DEFAULT_CTLLO(_chan) ({ \ |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 41 | struct dw_dma_chan *_dwc = to_dw_dma_chan(_chan); \ |
| 42 | struct dma_slave_config *_sconfig = &_dwc->dma_sconfig; \ |
Andy Shevchenko | 495aea4 | 2013-01-10 11:11:41 +0200 | [diff] [blame] | 43 | bool _is_slave = is_slave_direction(_dwc->direction); \ |
Andy Shevchenko | 495aea4 | 2013-01-10 11:11:41 +0200 | [diff] [blame] | 44 | u8 _smsize = _is_slave ? _sconfig->src_maxburst : \ |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 45 | DW_DMA_MSIZE_16; \ |
Andy Shevchenko | 495aea4 | 2013-01-10 11:11:41 +0200 | [diff] [blame] | 46 | u8 _dmsize = _is_slave ? _sconfig->dst_maxburst : \ |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 47 | DW_DMA_MSIZE_16; \ |
Mans Rullgard | bb3450a | 2016-03-18 16:24:42 +0200 | [diff] [blame] | 48 | u8 _dms = (_dwc->direction == DMA_MEM_TO_DEV) ? \ |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 49 | _dwc->dws.p_master : _dwc->dws.m_master; \ |
Mans Rullgard | bb3450a | 2016-03-18 16:24:42 +0200 | [diff] [blame] | 50 | u8 _sms = (_dwc->direction == DMA_DEV_TO_MEM) ? \ |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 51 | _dwc->dws.p_master : _dwc->dws.m_master; \ |
Jamie Iles | f301c06 | 2011-01-21 14:11:53 +0000 | [diff] [blame] | 52 | \ |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 53 | (DWC_CTLL_DST_MSIZE(_dmsize) \ |
| 54 | | DWC_CTLL_SRC_MSIZE(_smsize) \ |
Jamie Iles | f301c06 | 2011-01-21 14:11:53 +0000 | [diff] [blame] | 55 | | DWC_CTLL_LLP_D_EN \ |
| 56 | | DWC_CTLL_LLP_S_EN \ |
Mans Rullgard | bb3450a | 2016-03-18 16:24:42 +0200 | [diff] [blame] | 57 | | DWC_CTLL_DMS(_dms) \ |
| 58 | | DWC_CTLL_SMS(_sms)); \ |
Jamie Iles | f301c06 | 2011-01-21 14:11:53 +0000 | [diff] [blame] | 59 | }) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 60 | |
Andy Shevchenko | 029a40e | 2015-01-02 16:17:24 +0200 | [diff] [blame] | 61 | /* The set of bus widths supported by the DMA controller */ |
| 62 | #define DW_DMA_BUSWIDTHS \ |
| 63 | BIT(DMA_SLAVE_BUSWIDTH_UNDEFINED) | \ |
| 64 | BIT(DMA_SLAVE_BUSWIDTH_1_BYTE) | \ |
| 65 | BIT(DMA_SLAVE_BUSWIDTH_2_BYTES) | \ |
| 66 | BIT(DMA_SLAVE_BUSWIDTH_4_BYTES) |
| 67 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 68 | /*----------------------------------------------------------------------*/ |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 69 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 70 | static struct device *chan2dev(struct dma_chan *chan) |
| 71 | { |
| 72 | return &chan->dev->device; |
| 73 | } |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 74 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 75 | static struct dw_desc *dwc_first_active(struct dw_dma_chan *dwc) |
| 76 | { |
Andy Shevchenko | e63a47a | 2012-10-18 17:34:12 +0300 | [diff] [blame] | 77 | return to_dw_desc(dwc->active_list.next); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 78 | } |
| 79 | |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 80 | static dma_cookie_t dwc_tx_submit(struct dma_async_tx_descriptor *tx) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 81 | { |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 82 | struct dw_desc *desc = txd_to_dw_desc(tx); |
| 83 | struct dw_dma_chan *dwc = to_dw_dma_chan(tx->chan); |
| 84 | dma_cookie_t cookie; |
| 85 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 86 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 87 | spin_lock_irqsave(&dwc->lock, flags); |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 88 | cookie = dma_cookie_assign(tx); |
| 89 | |
| 90 | /* |
| 91 | * REVISIT: We should attempt to chain as many descriptors as |
| 92 | * possible, perhaps even appending to those already submitted |
| 93 | * for DMA. But this is hard to do in a race-free manner. |
| 94 | */ |
| 95 | |
| 96 | list_add_tail(&desc->desc_node, &dwc->queue); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 97 | spin_unlock_irqrestore(&dwc->lock, flags); |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 98 | dev_vdbg(chan2dev(tx->chan), "%s: queued %u\n", |
| 99 | __func__, desc->txd.cookie); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 100 | |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 101 | return cookie; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 102 | } |
| 103 | |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 104 | static struct dw_desc *dwc_desc_get(struct dw_dma_chan *dwc) |
| 105 | { |
| 106 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
| 107 | struct dw_desc *desc; |
| 108 | dma_addr_t phys; |
| 109 | |
| 110 | desc = dma_pool_zalloc(dw->desc_pool, GFP_ATOMIC, &phys); |
| 111 | if (!desc) |
| 112 | return NULL; |
| 113 | |
| 114 | dwc->descs_allocated++; |
| 115 | INIT_LIST_HEAD(&desc->tx_list); |
| 116 | dma_async_tx_descriptor_init(&desc->txd, &dwc->chan); |
| 117 | desc->txd.tx_submit = dwc_tx_submit; |
| 118 | desc->txd.flags = DMA_CTRL_ACK; |
| 119 | desc->txd.phys = phys; |
| 120 | return desc; |
| 121 | } |
| 122 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 123 | static void dwc_desc_put(struct dw_dma_chan *dwc, struct dw_desc *desc) |
| 124 | { |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 125 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
| 126 | struct dw_desc *child, *_next; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 127 | |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 128 | if (unlikely(!desc)) |
| 129 | return; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 130 | |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 131 | list_for_each_entry_safe(child, _next, &desc->tx_list, desc_node) { |
| 132 | list_del(&child->desc_node); |
| 133 | dma_pool_free(dw->desc_pool, child, child->txd.phys); |
| 134 | dwc->descs_allocated--; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 135 | } |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 136 | |
| 137 | dma_pool_free(dw->desc_pool, desc, desc->txd.phys); |
| 138 | dwc->descs_allocated--; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 139 | } |
| 140 | |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 141 | static void dwc_initialize(struct dw_dma_chan *dwc) |
| 142 | { |
| 143 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 144 | u32 cfghi = DWC_CFGH_FIFO_MODE; |
| 145 | u32 cfglo = DWC_CFGL_CH_PRIOR(dwc->priority); |
| 146 | |
Andy Shevchenko | 423f9cb | 2016-03-18 16:24:52 +0200 | [diff] [blame] | 147 | if (test_bit(DW_DMA_IS_INITIALIZED, &dwc->flags)) |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 148 | return; |
| 149 | |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 150 | cfghi |= DWC_CFGH_DST_PER(dwc->dws.dst_id); |
| 151 | cfghi |= DWC_CFGH_SRC_PER(dwc->dws.src_id); |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 152 | |
| 153 | channel_writel(dwc, CFG_LO, cfglo); |
| 154 | channel_writel(dwc, CFG_HI, cfghi); |
| 155 | |
| 156 | /* Enable interrupts */ |
| 157 | channel_set_bit(dw, MASK.XFER, dwc->mask); |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 158 | channel_set_bit(dw, MASK.ERROR, dwc->mask); |
| 159 | |
Andy Shevchenko | 423f9cb | 2016-03-18 16:24:52 +0200 | [diff] [blame] | 160 | set_bit(DW_DMA_IS_INITIALIZED, &dwc->flags); |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 161 | } |
| 162 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 163 | /*----------------------------------------------------------------------*/ |
| 164 | |
Andy Shevchenko | f52b36d | 2012-09-21 15:05:44 +0300 | [diff] [blame] | 165 | static inline void dwc_dump_chan_regs(struct dw_dma_chan *dwc) |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 166 | { |
| 167 | dev_err(chan2dev(&dwc->chan), |
| 168 | " SAR: 0x%x DAR: 0x%x LLP: 0x%x CTL: 0x%x:%08x\n", |
| 169 | channel_readl(dwc, SAR), |
| 170 | channel_readl(dwc, DAR), |
| 171 | channel_readl(dwc, LLP), |
| 172 | channel_readl(dwc, CTL_HI), |
| 173 | channel_readl(dwc, CTL_LO)); |
| 174 | } |
| 175 | |
Andy Shevchenko | 3f936207 | 2012-06-19 13:46:32 +0300 | [diff] [blame] | 176 | static inline void dwc_chan_disable(struct dw_dma *dw, struct dw_dma_chan *dwc) |
| 177 | { |
| 178 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 179 | while (dma_readl(dw, CH_EN) & dwc->mask) |
| 180 | cpu_relax(); |
| 181 | } |
| 182 | |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 183 | /*----------------------------------------------------------------------*/ |
| 184 | |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 185 | /* Perform single block transfer */ |
| 186 | static inline void dwc_do_single_block(struct dw_dma_chan *dwc, |
| 187 | struct dw_desc *desc) |
| 188 | { |
| 189 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
| 190 | u32 ctllo; |
| 191 | |
Andy Shevchenko | 1d566f1 | 2014-01-13 14:04:48 +0200 | [diff] [blame] | 192 | /* |
| 193 | * Software emulation of LLP mode relies on interrupts to continue |
| 194 | * multi block transfer. |
| 195 | */ |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 196 | ctllo = lli_read(desc, ctllo) | DWC_CTLL_INT_EN; |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 197 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 198 | channel_writel(dwc, SAR, lli_read(desc, sar)); |
| 199 | channel_writel(dwc, DAR, lli_read(desc, dar)); |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 200 | channel_writel(dwc, CTL_LO, ctllo); |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 201 | channel_writel(dwc, CTL_HI, lli_read(desc, ctlhi)); |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 202 | channel_set_bit(dw, CH_EN, dwc->mask); |
Andy Shevchenko | f5c6a7d | 2013-01-09 10:17:13 +0200 | [diff] [blame] | 203 | |
| 204 | /* Move pointer to next descriptor */ |
| 205 | dwc->tx_node_active = dwc->tx_node_active->next; |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 206 | } |
| 207 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 208 | /* Called with dwc->lock held and bh disabled */ |
| 209 | static void dwc_dostart(struct dw_dma_chan *dwc, struct dw_desc *first) |
| 210 | { |
| 211 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 212 | u8 lms = DWC_LLP_LMS(dwc->dws.m_master); |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 213 | unsigned long was_soft_llp; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 214 | |
| 215 | /* ASSERT: channel is idle */ |
| 216 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 217 | dev_err(chan2dev(&dwc->chan), |
Jarkko Nikula | 550da64 | 2015-03-10 11:37:23 +0200 | [diff] [blame] | 218 | "%s: BUG: Attempted to start non-idle channel\n", |
| 219 | __func__); |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 220 | dwc_dump_chan_regs(dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 221 | |
| 222 | /* The tasklet will hopefully advance the queue... */ |
| 223 | return; |
| 224 | } |
| 225 | |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 226 | if (dwc->nollp) { |
| 227 | was_soft_llp = test_and_set_bit(DW_DMA_IS_SOFT_LLP, |
| 228 | &dwc->flags); |
| 229 | if (was_soft_llp) { |
| 230 | dev_err(chan2dev(&dwc->chan), |
Andy Shevchenko | fc61f6b | 2014-01-13 14:04:49 +0200 | [diff] [blame] | 231 | "BUG: Attempted to start new LLP transfer inside ongoing one\n"); |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 232 | return; |
| 233 | } |
| 234 | |
| 235 | dwc_initialize(dwc); |
| 236 | |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 237 | first->residue = first->total_len; |
Andy Shevchenko | f5c6a7d | 2013-01-09 10:17:13 +0200 | [diff] [blame] | 238 | dwc->tx_node_active = &first->tx_list; |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 239 | |
Andy Shevchenko | fdf475f | 2013-01-25 11:48:00 +0200 | [diff] [blame] | 240 | /* Submit first block */ |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 241 | dwc_do_single_block(dwc, first); |
| 242 | |
| 243 | return; |
| 244 | } |
| 245 | |
Viresh Kumar | 61e183f | 2011-11-17 16:01:29 +0530 | [diff] [blame] | 246 | dwc_initialize(dwc); |
| 247 | |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 248 | channel_writel(dwc, LLP, first->txd.phys | lms); |
| 249 | channel_writel(dwc, CTL_LO, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 250 | channel_writel(dwc, CTL_HI, 0); |
| 251 | channel_set_bit(dw, CH_EN, dwc->mask); |
| 252 | } |
| 253 | |
Andy Shevchenko | e7637c6 | 2014-06-18 12:15:36 +0300 | [diff] [blame] | 254 | static void dwc_dostart_first_queued(struct dw_dma_chan *dwc) |
| 255 | { |
Andy Shevchenko | cba1561 | 2014-06-18 12:15:37 +0300 | [diff] [blame] | 256 | struct dw_desc *desc; |
| 257 | |
Andy Shevchenko | e7637c6 | 2014-06-18 12:15:36 +0300 | [diff] [blame] | 258 | if (list_empty(&dwc->queue)) |
| 259 | return; |
| 260 | |
| 261 | list_move(dwc->queue.next, &dwc->active_list); |
Andy Shevchenko | cba1561 | 2014-06-18 12:15:37 +0300 | [diff] [blame] | 262 | desc = dwc_first_active(dwc); |
| 263 | dev_vdbg(chan2dev(&dwc->chan), "%s: started %u\n", __func__, desc->txd.cookie); |
| 264 | dwc_dostart(dwc, desc); |
Andy Shevchenko | e7637c6 | 2014-06-18 12:15:36 +0300 | [diff] [blame] | 265 | } |
| 266 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 267 | /*----------------------------------------------------------------------*/ |
| 268 | |
| 269 | static void |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 270 | dwc_descriptor_complete(struct dw_dma_chan *dwc, struct dw_desc *desc, |
| 271 | bool callback_required) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 272 | { |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 273 | dma_async_tx_callback callback = NULL; |
| 274 | void *param = NULL; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 275 | struct dma_async_tx_descriptor *txd = &desc->txd; |
Viresh Kumar | e518076 | 2011-03-03 15:47:20 +0530 | [diff] [blame] | 276 | struct dw_desc *child; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 277 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 278 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 279 | dev_vdbg(chan2dev(&dwc->chan), "descriptor %u complete\n", txd->cookie); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 280 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 281 | spin_lock_irqsave(&dwc->lock, flags); |
Russell King - ARM Linux | f7fbce0 | 2012-03-06 22:35:07 +0000 | [diff] [blame] | 282 | dma_cookie_complete(txd); |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 283 | if (callback_required) { |
| 284 | callback = txd->callback; |
| 285 | param = txd->callback_param; |
| 286 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 287 | |
Viresh Kumar | e518076 | 2011-03-03 15:47:20 +0530 | [diff] [blame] | 288 | /* async_tx_ack */ |
| 289 | list_for_each_entry(child, &desc->tx_list, desc_node) |
| 290 | async_tx_ack(&child->txd); |
| 291 | async_tx_ack(&desc->txd); |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 292 | dwc_desc_put(dwc, desc); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 293 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 294 | |
Andy Shevchenko | 21e93c1 | 2013-01-09 10:17:12 +0200 | [diff] [blame] | 295 | if (callback) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 296 | callback(param); |
| 297 | } |
| 298 | |
| 299 | static void dwc_complete_all(struct dw_dma *dw, struct dw_dma_chan *dwc) |
| 300 | { |
| 301 | struct dw_desc *desc, *_desc; |
| 302 | LIST_HEAD(list); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 303 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 304 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 305 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 306 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 307 | dev_err(chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 308 | "BUG: XFER bit set, but channel not idle!\n"); |
| 309 | |
| 310 | /* Try to continue after resetting the channel... */ |
Andy Shevchenko | 3f936207 | 2012-06-19 13:46:32 +0300 | [diff] [blame] | 311 | dwc_chan_disable(dw, dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 312 | } |
| 313 | |
| 314 | /* |
| 315 | * Submit queued descriptors ASAP, i.e. before we go through |
| 316 | * the completed ones. |
| 317 | */ |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 318 | list_splice_init(&dwc->active_list, &list); |
Andy Shevchenko | e7637c6 | 2014-06-18 12:15:36 +0300 | [diff] [blame] | 319 | dwc_dostart_first_queued(dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 320 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 321 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 322 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 323 | list_for_each_entry_safe(desc, _desc, &list, desc_node) |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 324 | dwc_descriptor_complete(dwc, desc, true); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 325 | } |
| 326 | |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 327 | /* Returns how many bytes were already received from source */ |
| 328 | static inline u32 dwc_get_sent(struct dw_dma_chan *dwc) |
| 329 | { |
| 330 | u32 ctlhi = channel_readl(dwc, CTL_HI); |
| 331 | u32 ctllo = channel_readl(dwc, CTL_LO); |
| 332 | |
| 333 | return (ctlhi & DWC_CTLH_BLOCK_TS_MASK) * (1 << (ctllo >> 4 & 7)); |
| 334 | } |
| 335 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 336 | static void dwc_scan_descriptors(struct dw_dma *dw, struct dw_dma_chan *dwc) |
| 337 | { |
| 338 | dma_addr_t llp; |
| 339 | struct dw_desc *desc, *_desc; |
| 340 | struct dw_desc *child; |
| 341 | u32 status_xfer; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 342 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 343 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 344 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 345 | llp = channel_readl(dwc, LLP); |
| 346 | status_xfer = dma_readl(dw, RAW.XFER); |
| 347 | |
| 348 | if (status_xfer & dwc->mask) { |
| 349 | /* Everything we've submitted is done */ |
| 350 | dma_writel(dw, CLEAR.XFER, dwc->mask); |
Andy Shevchenko | 77bcc497 | 2013-01-18 14:14:15 +0200 | [diff] [blame] | 351 | |
| 352 | if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) { |
Andy Shevchenko | fdf475f | 2013-01-25 11:48:00 +0200 | [diff] [blame] | 353 | struct list_head *head, *active = dwc->tx_node_active; |
| 354 | |
| 355 | /* |
| 356 | * We are inside first active descriptor. |
| 357 | * Otherwise something is really wrong. |
| 358 | */ |
| 359 | desc = dwc_first_active(dwc); |
| 360 | |
| 361 | head = &desc->tx_list; |
| 362 | if (active != head) { |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 363 | /* Update residue to reflect last sent descriptor */ |
| 364 | if (active == head->next) |
| 365 | desc->residue -= desc->len; |
| 366 | else |
| 367 | desc->residue -= to_dw_desc(active->prev)->len; |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 368 | |
Andy Shevchenko | fdf475f | 2013-01-25 11:48:00 +0200 | [diff] [blame] | 369 | child = to_dw_desc(active); |
Andy Shevchenko | 77bcc497 | 2013-01-18 14:14:15 +0200 | [diff] [blame] | 370 | |
| 371 | /* Submit next block */ |
Andy Shevchenko | fdf475f | 2013-01-25 11:48:00 +0200 | [diff] [blame] | 372 | dwc_do_single_block(dwc, child); |
Andy Shevchenko | 77bcc497 | 2013-01-18 14:14:15 +0200 | [diff] [blame] | 373 | |
Andy Shevchenko | fdf475f | 2013-01-25 11:48:00 +0200 | [diff] [blame] | 374 | spin_unlock_irqrestore(&dwc->lock, flags); |
Andy Shevchenko | 77bcc497 | 2013-01-18 14:14:15 +0200 | [diff] [blame] | 375 | return; |
| 376 | } |
Andy Shevchenko | fdf475f | 2013-01-25 11:48:00 +0200 | [diff] [blame] | 377 | |
Andy Shevchenko | 77bcc497 | 2013-01-18 14:14:15 +0200 | [diff] [blame] | 378 | /* We are done here */ |
| 379 | clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags); |
| 380 | } |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 381 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 382 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 383 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 384 | dwc_complete_all(dw, dwc); |
| 385 | return; |
| 386 | } |
| 387 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 388 | if (list_empty(&dwc->active_list)) { |
| 389 | spin_unlock_irqrestore(&dwc->lock, flags); |
Jamie Iles | 087809f | 2011-01-21 14:11:52 +0000 | [diff] [blame] | 390 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 391 | } |
Jamie Iles | 087809f | 2011-01-21 14:11:52 +0000 | [diff] [blame] | 392 | |
Andy Shevchenko | 77bcc497 | 2013-01-18 14:14:15 +0200 | [diff] [blame] | 393 | if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags)) { |
| 394 | dev_vdbg(chan2dev(&dwc->chan), "%s: soft LLP mode\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 395 | spin_unlock_irqrestore(&dwc->lock, flags); |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 396 | return; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 397 | } |
| 398 | |
Andy Shevchenko | 5a87f0e | 2014-01-13 14:04:50 +0200 | [diff] [blame] | 399 | dev_vdbg(chan2dev(&dwc->chan), "%s: llp=%pad\n", __func__, &llp); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 400 | |
| 401 | list_for_each_entry_safe(desc, _desc, &dwc->active_list, desc_node) { |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 402 | /* Initial residue value */ |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 403 | desc->residue = desc->total_len; |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 404 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 405 | /* Check first descriptors addr */ |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 406 | if (desc->txd.phys == DWC_LLP_LOC(llp)) { |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 407 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 408 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 409 | } |
Viresh Kumar | 84adccf | 2011-03-24 11:32:15 +0530 | [diff] [blame] | 410 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 411 | /* Check first descriptors llp */ |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 412 | if (lli_read(desc, llp) == llp) { |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 413 | /* This one is currently in progress */ |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 414 | desc->residue -= dwc_get_sent(dwc); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 415 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 416 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 417 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 418 | |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 419 | desc->residue -= desc->len; |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 420 | list_for_each_entry(child, &desc->tx_list, desc_node) { |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 421 | if (lli_read(child, llp) == llp) { |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 422 | /* Currently in progress */ |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 423 | desc->residue -= dwc_get_sent(dwc); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 424 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 425 | return; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 426 | } |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 427 | desc->residue -= child->len; |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 428 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 429 | |
| 430 | /* |
| 431 | * No descriptors so far seem to be in progress, i.e. |
| 432 | * this one must be done. |
| 433 | */ |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 434 | spin_unlock_irqrestore(&dwc->lock, flags); |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 435 | dwc_descriptor_complete(dwc, desc, true); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 436 | spin_lock_irqsave(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 437 | } |
| 438 | |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 439 | dev_err(chan2dev(&dwc->chan), |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 440 | "BUG: All descriptors done, but channel not idle!\n"); |
| 441 | |
| 442 | /* Try to continue after resetting the channel... */ |
Andy Shevchenko | 3f936207 | 2012-06-19 13:46:32 +0300 | [diff] [blame] | 443 | dwc_chan_disable(dw, dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 444 | |
Andy Shevchenko | e7637c6 | 2014-06-18 12:15:36 +0300 | [diff] [blame] | 445 | dwc_dostart_first_queued(dwc); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 446 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 447 | } |
| 448 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 449 | static inline void dwc_dump_lli(struct dw_dma_chan *dwc, struct dw_desc *desc) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 450 | { |
Andy Shevchenko | 21d43f4 | 2012-10-18 17:34:09 +0300 | [diff] [blame] | 451 | dev_crit(chan2dev(&dwc->chan), " desc: s0x%x d0x%x l0x%x c0x%x:%x\n", |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 452 | lli_read(desc, sar), |
| 453 | lli_read(desc, dar), |
| 454 | lli_read(desc, llp), |
| 455 | lli_read(desc, ctlhi), |
| 456 | lli_read(desc, ctllo)); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 457 | } |
| 458 | |
| 459 | static void dwc_handle_error(struct dw_dma *dw, struct dw_dma_chan *dwc) |
| 460 | { |
| 461 | struct dw_desc *bad_desc; |
| 462 | struct dw_desc *child; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 463 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 464 | |
| 465 | dwc_scan_descriptors(dw, dwc); |
| 466 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 467 | spin_lock_irqsave(&dwc->lock, flags); |
| 468 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 469 | /* |
| 470 | * The descriptor currently at the head of the active list is |
| 471 | * borked. Since we don't have any way to report errors, we'll |
| 472 | * just have to scream loudly and try to carry on. |
| 473 | */ |
| 474 | bad_desc = dwc_first_active(dwc); |
| 475 | list_del_init(&bad_desc->desc_node); |
Viresh Kumar | f336e42 | 2011-03-03 15:47:16 +0530 | [diff] [blame] | 476 | list_move(dwc->queue.next, dwc->active_list.prev); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 477 | |
| 478 | /* Clear the error flag and try to restart the controller */ |
| 479 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
| 480 | if (!list_empty(&dwc->active_list)) |
| 481 | dwc_dostart(dwc, dwc_first_active(dwc)); |
| 482 | |
| 483 | /* |
Andy Shevchenko | ba84bd7 | 2012-10-18 17:34:11 +0300 | [diff] [blame] | 484 | * WARN may seem harsh, but since this only happens |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 485 | * when someone submits a bad physical address in a |
| 486 | * descriptor, we should consider ourselves lucky that the |
| 487 | * controller flagged an error instead of scribbling over |
| 488 | * random memory locations. |
| 489 | */ |
Andy Shevchenko | ba84bd7 | 2012-10-18 17:34:11 +0300 | [diff] [blame] | 490 | dev_WARN(chan2dev(&dwc->chan), "Bad descriptor submitted for DMA!\n" |
| 491 | " cookie: %d\n", bad_desc->txd.cookie); |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 492 | dwc_dump_lli(dwc, bad_desc); |
Dan Williams | e0bd0f8 | 2009-09-08 17:53:02 -0700 | [diff] [blame] | 493 | list_for_each_entry(child, &bad_desc->tx_list, desc_node) |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 494 | dwc_dump_lli(dwc, child); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 495 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 496 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 497 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 498 | /* Pretend the descriptor completed successfully */ |
Viresh Kumar | 5fedefb | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 499 | dwc_descriptor_complete(dwc, bad_desc, true); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 500 | } |
| 501 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 502 | /* --------------------- Cyclic DMA API extensions -------------------- */ |
| 503 | |
Denis Efremov | 8004cbb | 2013-05-09 13:19:40 +0400 | [diff] [blame] | 504 | dma_addr_t dw_dma_get_src_addr(struct dma_chan *chan) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 505 | { |
| 506 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 507 | return channel_readl(dwc, SAR); |
| 508 | } |
| 509 | EXPORT_SYMBOL(dw_dma_get_src_addr); |
| 510 | |
Denis Efremov | 8004cbb | 2013-05-09 13:19:40 +0400 | [diff] [blame] | 511 | dma_addr_t dw_dma_get_dst_addr(struct dma_chan *chan) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 512 | { |
| 513 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 514 | return channel_readl(dwc, DAR); |
| 515 | } |
| 516 | EXPORT_SYMBOL(dw_dma_get_dst_addr); |
| 517 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 518 | /* Called with dwc->lock held and all DMAC interrupts disabled */ |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 519 | static void dwc_handle_cyclic(struct dw_dma *dw, struct dw_dma_chan *dwc, |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 520 | u32 status_block, u32 status_err, u32 status_xfer) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 521 | { |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 522 | unsigned long flags; |
| 523 | |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 524 | if (status_block & dwc->mask) { |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 525 | void (*callback)(void *param); |
| 526 | void *callback_param; |
| 527 | |
| 528 | dev_vdbg(chan2dev(&dwc->chan), "new cyclic period llp 0x%08x\n", |
| 529 | channel_readl(dwc, LLP)); |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 530 | dma_writel(dw, CLEAR.BLOCK, dwc->mask); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 531 | |
| 532 | callback = dwc->cdesc->period_callback; |
| 533 | callback_param = dwc->cdesc->period_callback_param; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 534 | |
| 535 | if (callback) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 536 | callback(callback_param); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 537 | } |
| 538 | |
| 539 | /* |
| 540 | * Error and transfer complete are highly unlikely, and will most |
| 541 | * likely be due to a configuration error by the user. |
| 542 | */ |
| 543 | if (unlikely(status_err & dwc->mask) || |
| 544 | unlikely(status_xfer & dwc->mask)) { |
Andy Shevchenko | 7794e5b | 2016-03-18 16:24:48 +0200 | [diff] [blame] | 545 | unsigned int i; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 546 | |
Andy Shevchenko | fc61f6b | 2014-01-13 14:04:49 +0200 | [diff] [blame] | 547 | dev_err(chan2dev(&dwc->chan), |
| 548 | "cyclic DMA unexpected %s interrupt, stopping DMA transfer\n", |
| 549 | status_xfer ? "xfer" : "error"); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 550 | |
| 551 | spin_lock_irqsave(&dwc->lock, flags); |
| 552 | |
Andy Shevchenko | 1d45543 | 2012-06-19 13:34:03 +0300 | [diff] [blame] | 553 | dwc_dump_chan_regs(dwc); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 554 | |
Andy Shevchenko | 3f936207 | 2012-06-19 13:46:32 +0300 | [diff] [blame] | 555 | dwc_chan_disable(dw, dwc); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 556 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 557 | /* Make sure DMA does not restart by loading a new list */ |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 558 | channel_writel(dwc, LLP, 0); |
| 559 | channel_writel(dwc, CTL_LO, 0); |
| 560 | channel_writel(dwc, CTL_HI, 0); |
| 561 | |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 562 | dma_writel(dw, CLEAR.BLOCK, dwc->mask); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 563 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
| 564 | dma_writel(dw, CLEAR.XFER, dwc->mask); |
| 565 | |
| 566 | for (i = 0; i < dwc->cdesc->periods; i++) |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 567 | dwc_dump_lli(dwc, dwc->cdesc->desc[i]); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 568 | |
| 569 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 570 | } |
Andy Shevchenko | ee1cdcd | 2016-02-10 15:59:42 +0200 | [diff] [blame] | 571 | |
| 572 | /* Re-enable interrupts */ |
| 573 | channel_set_bit(dw, MASK.BLOCK, dwc->mask); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | /* ------------------------------------------------------------------------- */ |
| 577 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 578 | static void dw_dma_tasklet(unsigned long data) |
| 579 | { |
| 580 | struct dw_dma *dw = (struct dw_dma *)data; |
| 581 | struct dw_dma_chan *dwc; |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 582 | u32 status_block; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 583 | u32 status_xfer; |
| 584 | u32 status_err; |
Andy Shevchenko | 7794e5b | 2016-03-18 16:24:48 +0200 | [diff] [blame] | 585 | unsigned int i; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 586 | |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 587 | status_block = dma_readl(dw, RAW.BLOCK); |
Haavard Skinnemoen | 7fe7b2f | 2008-10-03 15:23:46 -0700 | [diff] [blame] | 588 | status_xfer = dma_readl(dw, RAW.XFER); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 589 | status_err = dma_readl(dw, RAW.ERROR); |
| 590 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame] | 591 | dev_vdbg(dw->dma.dev, "%s: status_err=%x\n", __func__, status_err); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 592 | |
| 593 | for (i = 0; i < dw->dma.chancnt; i++) { |
| 594 | dwc = &dw->chan[i]; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 595 | if (test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 596 | dwc_handle_cyclic(dw, dwc, status_block, status_err, |
| 597 | status_xfer); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 598 | else if (status_err & (1 << i)) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 599 | dwc_handle_error(dw, dwc); |
Andy Shevchenko | 77bcc497 | 2013-01-18 14:14:15 +0200 | [diff] [blame] | 600 | else if (status_xfer & (1 << i)) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 601 | dwc_scan_descriptors(dw, dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 602 | } |
| 603 | |
Andy Shevchenko | ee1cdcd | 2016-02-10 15:59:42 +0200 | [diff] [blame] | 604 | /* Re-enable interrupts */ |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 605 | channel_set_bit(dw, MASK.XFER, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 606 | channel_set_bit(dw, MASK.ERROR, dw->all_chan_mask); |
| 607 | } |
| 608 | |
| 609 | static irqreturn_t dw_dma_interrupt(int irq, void *dev_id) |
| 610 | { |
| 611 | struct dw_dma *dw = dev_id; |
Andy Shevchenko | 02a21b7 | 2015-12-04 23:49:24 +0200 | [diff] [blame] | 612 | u32 status; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 613 | |
Andy Shevchenko | 02a21b7 | 2015-12-04 23:49:24 +0200 | [diff] [blame] | 614 | /* Check if we have any interrupt from the DMAC which is not in use */ |
| 615 | if (!dw->in_use) |
| 616 | return IRQ_NONE; |
| 617 | |
| 618 | status = dma_readl(dw, STATUS_INT); |
Andy Shevchenko | 3783cef | 2013-07-15 15:04:39 +0300 | [diff] [blame] | 619 | dev_vdbg(dw->dma.dev, "%s: status=0x%x\n", __func__, status); |
| 620 | |
| 621 | /* Check if we have any interrupt from the DMAC */ |
Andy Shevchenko | 02a21b7 | 2015-12-04 23:49:24 +0200 | [diff] [blame] | 622 | if (!status) |
Andy Shevchenko | 3783cef | 2013-07-15 15:04:39 +0300 | [diff] [blame] | 623 | return IRQ_NONE; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 624 | |
| 625 | /* |
| 626 | * Just disable the interrupts. We'll turn them back on in the |
| 627 | * softirq handler. |
| 628 | */ |
| 629 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 630 | channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 631 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); |
| 632 | |
| 633 | status = dma_readl(dw, STATUS_INT); |
| 634 | if (status) { |
| 635 | dev_err(dw->dma.dev, |
| 636 | "BUG: Unexpected interrupts pending: 0x%x\n", |
| 637 | status); |
| 638 | |
| 639 | /* Try to recover */ |
| 640 | channel_clear_bit(dw, MASK.XFER, (1 << 8) - 1); |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 641 | channel_clear_bit(dw, MASK.BLOCK, (1 << 8) - 1); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 642 | channel_clear_bit(dw, MASK.SRC_TRAN, (1 << 8) - 1); |
| 643 | channel_clear_bit(dw, MASK.DST_TRAN, (1 << 8) - 1); |
| 644 | channel_clear_bit(dw, MASK.ERROR, (1 << 8) - 1); |
| 645 | } |
| 646 | |
| 647 | tasklet_schedule(&dw->tasklet); |
| 648 | |
| 649 | return IRQ_HANDLED; |
| 650 | } |
| 651 | |
| 652 | /*----------------------------------------------------------------------*/ |
| 653 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 654 | static struct dma_async_tx_descriptor * |
| 655 | dwc_prep_dma_memcpy(struct dma_chan *chan, dma_addr_t dest, dma_addr_t src, |
| 656 | size_t len, unsigned long flags) |
| 657 | { |
| 658 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Arnd Bergmann | f776076 | 2013-03-26 16:53:57 +0200 | [diff] [blame] | 659 | struct dw_dma *dw = to_dw_dma(chan->device); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 660 | struct dw_desc *desc; |
| 661 | struct dw_desc *first; |
| 662 | struct dw_desc *prev; |
| 663 | size_t xfer_count; |
| 664 | size_t offset; |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 665 | u8 m_master = dwc->dws.m_master; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 666 | unsigned int src_width; |
| 667 | unsigned int dst_width; |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 668 | unsigned int data_width = dw->pdata->data_width[m_master]; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 669 | u32 ctllo; |
Andy Shevchenko | 2e65060 | 2016-04-27 14:15:38 +0300 | [diff] [blame] | 670 | u8 lms = DWC_LLP_LMS(m_master); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 671 | |
Andy Shevchenko | 2f45d61 | 2012-06-19 13:34:02 +0300 | [diff] [blame] | 672 | dev_vdbg(chan2dev(chan), |
Andy Shevchenko | 5a87f0e | 2014-01-13 14:04:50 +0200 | [diff] [blame] | 673 | "%s: d%pad s%pad l0x%zx f0x%lx\n", __func__, |
| 674 | &dest, &src, len, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 675 | |
| 676 | if (unlikely(!len)) { |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame] | 677 | dev_dbg(chan2dev(chan), "%s: length is zero!\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 678 | return NULL; |
| 679 | } |
| 680 | |
Andy Shevchenko | 0fdb567 | 2013-01-10 10:53:03 +0200 | [diff] [blame] | 681 | dwc->direction = DMA_MEM_TO_MEM; |
| 682 | |
Andy Shevchenko | 2e65060 | 2016-04-27 14:15:38 +0300 | [diff] [blame] | 683 | src_width = dst_width = __ffs(data_width | src | dest | len); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 684 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 685 | ctllo = DWC_DEFAULT_CTLLO(chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 686 | | DWC_CTLL_DST_WIDTH(dst_width) |
| 687 | | DWC_CTLL_SRC_WIDTH(src_width) |
| 688 | | DWC_CTLL_DST_INC |
| 689 | | DWC_CTLL_SRC_INC |
| 690 | | DWC_CTLL_FC_M2M; |
| 691 | prev = first = NULL; |
| 692 | |
| 693 | for (offset = 0; offset < len; offset += xfer_count << src_width) { |
| 694 | xfer_count = min_t(size_t, (len - offset) >> src_width, |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 695 | dwc->block_size); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 696 | |
| 697 | desc = dwc_desc_get(dwc); |
| 698 | if (!desc) |
| 699 | goto err_desc_get; |
| 700 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 701 | lli_write(desc, sar, src + offset); |
| 702 | lli_write(desc, dar, dest + offset); |
| 703 | lli_write(desc, ctllo, ctllo); |
| 704 | lli_write(desc, ctlhi, xfer_count); |
Andy Shevchenko | 176dcec | 2013-01-25 11:48:02 +0200 | [diff] [blame] | 705 | desc->len = xfer_count << src_width; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 706 | |
| 707 | if (!first) { |
| 708 | first = desc; |
| 709 | } else { |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 710 | lli_write(prev, llp, desc->txd.phys | lms); |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 711 | list_add_tail(&desc->desc_node, &first->tx_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 712 | } |
| 713 | prev = desc; |
| 714 | } |
| 715 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 716 | if (flags & DMA_PREP_INTERRUPT) |
| 717 | /* Trigger interrupt after last block */ |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 718 | lli_set(prev, ctllo, DWC_CTLL_INT_EN); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 719 | |
| 720 | prev->lli.llp = 0; |
Mans Rullgard | a3e5579 | 2016-03-18 16:24:45 +0200 | [diff] [blame] | 721 | lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 722 | first->txd.flags = flags; |
Andy Shevchenko | 30d38a3 | 2013-01-25 11:48:01 +0200 | [diff] [blame] | 723 | first->total_len = len; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 724 | |
| 725 | return &first->txd; |
| 726 | |
| 727 | err_desc_get: |
| 728 | dwc_desc_put(dwc, first); |
| 729 | return NULL; |
| 730 | } |
| 731 | |
| 732 | static struct dma_async_tx_descriptor * |
| 733 | dwc_prep_slave_sg(struct dma_chan *chan, struct scatterlist *sgl, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 734 | unsigned int sg_len, enum dma_transfer_direction direction, |
Alexandre Bounine | 185ecb5 | 2012-03-08 15:35:13 -0500 | [diff] [blame] | 735 | unsigned long flags, void *context) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 736 | { |
| 737 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Arnd Bergmann | f776076 | 2013-03-26 16:53:57 +0200 | [diff] [blame] | 738 | struct dw_dma *dw = to_dw_dma(chan->device); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 739 | struct dma_slave_config *sconfig = &dwc->dma_sconfig; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 740 | struct dw_desc *prev; |
| 741 | struct dw_desc *first; |
| 742 | u32 ctllo; |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 743 | u8 m_master = dwc->dws.m_master; |
Andy Shevchenko | 2e65060 | 2016-04-27 14:15:38 +0300 | [diff] [blame] | 744 | u8 lms = DWC_LLP_LMS(m_master); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 745 | dma_addr_t reg; |
| 746 | unsigned int reg_width; |
| 747 | unsigned int mem_width; |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 748 | unsigned int data_width = dw->pdata->data_width[m_master]; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 749 | unsigned int i; |
| 750 | struct scatterlist *sg; |
| 751 | size_t total_len = 0; |
| 752 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame] | 753 | dev_vdbg(chan2dev(chan), "%s\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 754 | |
Andy Shevchenko | 495aea4 | 2013-01-10 11:11:41 +0200 | [diff] [blame] | 755 | if (unlikely(!is_slave_direction(direction) || !sg_len)) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 756 | return NULL; |
| 757 | |
Andy Shevchenko | 0fdb567 | 2013-01-10 10:53:03 +0200 | [diff] [blame] | 758 | dwc->direction = direction; |
| 759 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 760 | prev = first = NULL; |
| 761 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 762 | switch (direction) { |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 763 | case DMA_MEM_TO_DEV: |
Andy Shevchenko | 3941667 | 2015-09-28 18:57:04 +0300 | [diff] [blame] | 764 | reg_width = __ffs(sconfig->dst_addr_width); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 765 | reg = sconfig->dst_addr; |
| 766 | ctllo = (DWC_DEFAULT_CTLLO(chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 767 | | DWC_CTLL_DST_WIDTH(reg_width) |
| 768 | | DWC_CTLL_DST_FIX |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 769 | | DWC_CTLL_SRC_INC); |
| 770 | |
| 771 | ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_M2P) : |
| 772 | DWC_CTLL_FC(DW_DMA_FC_D_M2P); |
| 773 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 774 | for_each_sg(sgl, sg, sg_len, i) { |
| 775 | struct dw_desc *desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 776 | u32 len, dlen, mem; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 777 | |
Lars-Peter Clausen | cbb796c | 2012-04-25 20:50:51 +0200 | [diff] [blame] | 778 | mem = sg_dma_address(sg); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 779 | len = sg_dma_len(sg); |
Viresh Kumar | 6bc711f | 2012-02-01 16:12:25 +0530 | [diff] [blame] | 780 | |
Andy Shevchenko | 2e65060 | 2016-04-27 14:15:38 +0300 | [diff] [blame] | 781 | mem_width = __ffs(data_width | mem | len); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 782 | |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 783 | slave_sg_todev_fill_desc: |
| 784 | desc = dwc_desc_get(dwc); |
Jarkko Nikula | b260722 | 2015-03-10 11:37:24 +0200 | [diff] [blame] | 785 | if (!desc) |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 786 | goto err_desc_get; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 787 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 788 | lli_write(desc, sar, mem); |
| 789 | lli_write(desc, dar, reg); |
| 790 | lli_write(desc, ctllo, ctllo | DWC_CTLL_SRC_WIDTH(mem_width)); |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 791 | if ((len >> mem_width) > dwc->block_size) { |
| 792 | dlen = dwc->block_size << mem_width; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 793 | mem += dlen; |
| 794 | len -= dlen; |
| 795 | } else { |
| 796 | dlen = len; |
| 797 | len = 0; |
| 798 | } |
| 799 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 800 | lli_write(desc, ctlhi, dlen >> mem_width); |
Andy Shevchenko | 176dcec | 2013-01-25 11:48:02 +0200 | [diff] [blame] | 801 | desc->len = dlen; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 802 | |
| 803 | if (!first) { |
| 804 | first = desc; |
| 805 | } else { |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 806 | lli_write(prev, llp, desc->txd.phys | lms); |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 807 | list_add_tail(&desc->desc_node, &first->tx_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 808 | } |
| 809 | prev = desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 810 | total_len += dlen; |
| 811 | |
| 812 | if (len) |
| 813 | goto slave_sg_todev_fill_desc; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 814 | } |
| 815 | break; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 816 | case DMA_DEV_TO_MEM: |
Andy Shevchenko | 3941667 | 2015-09-28 18:57:04 +0300 | [diff] [blame] | 817 | reg_width = __ffs(sconfig->src_addr_width); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 818 | reg = sconfig->src_addr; |
| 819 | ctllo = (DWC_DEFAULT_CTLLO(chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 820 | | DWC_CTLL_SRC_WIDTH(reg_width) |
| 821 | | DWC_CTLL_DST_INC |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 822 | | DWC_CTLL_SRC_FIX); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 823 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 824 | ctllo |= sconfig->device_fc ? DWC_CTLL_FC(DW_DMA_FC_P_P2M) : |
| 825 | DWC_CTLL_FC(DW_DMA_FC_D_P2M); |
| 826 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 827 | for_each_sg(sgl, sg, sg_len, i) { |
| 828 | struct dw_desc *desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 829 | u32 len, dlen, mem; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 830 | |
Lars-Peter Clausen | cbb796c | 2012-04-25 20:50:51 +0200 | [diff] [blame] | 831 | mem = sg_dma_address(sg); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 832 | len = sg_dma_len(sg); |
Viresh Kumar | 6bc711f | 2012-02-01 16:12:25 +0530 | [diff] [blame] | 833 | |
Andy Shevchenko | 2e65060 | 2016-04-27 14:15:38 +0300 | [diff] [blame] | 834 | mem_width = __ffs(data_width | mem | len); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 835 | |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 836 | slave_sg_fromdev_fill_desc: |
| 837 | desc = dwc_desc_get(dwc); |
Jarkko Nikula | b260722 | 2015-03-10 11:37:24 +0200 | [diff] [blame] | 838 | if (!desc) |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 839 | goto err_desc_get; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 840 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 841 | lli_write(desc, sar, reg); |
| 842 | lli_write(desc, dar, mem); |
| 843 | lli_write(desc, ctllo, ctllo | DWC_CTLL_DST_WIDTH(mem_width)); |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 844 | if ((len >> reg_width) > dwc->block_size) { |
| 845 | dlen = dwc->block_size << reg_width; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 846 | mem += dlen; |
| 847 | len -= dlen; |
| 848 | } else { |
| 849 | dlen = len; |
| 850 | len = 0; |
| 851 | } |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 852 | lli_write(desc, ctlhi, dlen >> reg_width); |
Andy Shevchenko | 176dcec | 2013-01-25 11:48:02 +0200 | [diff] [blame] | 853 | desc->len = dlen; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 854 | |
| 855 | if (!first) { |
| 856 | first = desc; |
| 857 | } else { |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 858 | lli_write(prev, llp, desc->txd.phys | lms); |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 859 | list_add_tail(&desc->desc_node, &first->tx_list); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 860 | } |
| 861 | prev = desc; |
Viresh Kumar | 69dc14b | 2011-04-18 14:54:56 +0530 | [diff] [blame] | 862 | total_len += dlen; |
| 863 | |
| 864 | if (len) |
| 865 | goto slave_sg_fromdev_fill_desc; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 866 | } |
| 867 | break; |
| 868 | default: |
| 869 | return NULL; |
| 870 | } |
| 871 | |
| 872 | if (flags & DMA_PREP_INTERRUPT) |
| 873 | /* Trigger interrupt after last block */ |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 874 | lli_set(prev, ctllo, DWC_CTLL_INT_EN); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 875 | |
| 876 | prev->lli.llp = 0; |
Mans Rullgard | a3e5579 | 2016-03-18 16:24:45 +0200 | [diff] [blame] | 877 | lli_clear(prev, ctllo, DWC_CTLL_LLP_D_EN | DWC_CTLL_LLP_S_EN); |
Andy Shevchenko | 30d38a3 | 2013-01-25 11:48:01 +0200 | [diff] [blame] | 878 | first->total_len = total_len; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 879 | |
| 880 | return &first->txd; |
| 881 | |
| 882 | err_desc_get: |
Jarkko Nikula | b260722 | 2015-03-10 11:37:24 +0200 | [diff] [blame] | 883 | dev_err(chan2dev(chan), |
| 884 | "not enough descriptors available. Direction %d\n", direction); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 885 | dwc_desc_put(dwc, first); |
| 886 | return NULL; |
| 887 | } |
| 888 | |
Andy Shevchenko | 4d130de | 2014-08-19 20:29:16 +0300 | [diff] [blame] | 889 | bool dw_dma_filter(struct dma_chan *chan, void *param) |
| 890 | { |
| 891 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 892 | struct dw_dma_slave *dws = param; |
| 893 | |
Andy Shevchenko | 3fe6409 | 2016-04-08 16:22:17 +0300 | [diff] [blame] | 894 | if (dws->dma_dev != chan->device->dev) |
Andy Shevchenko | 4d130de | 2014-08-19 20:29:16 +0300 | [diff] [blame] | 895 | return false; |
| 896 | |
| 897 | /* We have to copy data since dws can be temporary storage */ |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 898 | memcpy(&dwc->dws, dws, sizeof(struct dw_dma_slave)); |
Andy Shevchenko | 4d130de | 2014-08-19 20:29:16 +0300 | [diff] [blame] | 899 | |
| 900 | return true; |
| 901 | } |
| 902 | EXPORT_SYMBOL_GPL(dw_dma_filter); |
| 903 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 904 | /* |
| 905 | * Fix sconfig's burst size according to dw_dmac. We need to convert them as: |
| 906 | * 1 -> 0, 4 -> 1, 8 -> 2, 16 -> 3. |
| 907 | * |
| 908 | * NOTE: burst size 2 is not supported by controller. |
| 909 | * |
| 910 | * This can be done by finding least significant bit set: n & (n - 1) |
| 911 | */ |
| 912 | static inline void convert_burst(u32 *maxburst) |
| 913 | { |
| 914 | if (*maxburst > 1) |
| 915 | *maxburst = fls(*maxburst) - 2; |
| 916 | else |
| 917 | *maxburst = 0; |
| 918 | } |
| 919 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 920 | static int dwc_config(struct dma_chan *chan, struct dma_slave_config *sconfig) |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 921 | { |
| 922 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 923 | |
Andy Shevchenko | 495aea4 | 2013-01-10 11:11:41 +0200 | [diff] [blame] | 924 | /* Check if chan will be configured for slave transfers */ |
| 925 | if (!is_slave_direction(sconfig->direction)) |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 926 | return -EINVAL; |
| 927 | |
| 928 | memcpy(&dwc->dma_sconfig, sconfig, sizeof(*sconfig)); |
Andy Shevchenko | 0fdb567 | 2013-01-10 10:53:03 +0200 | [diff] [blame] | 929 | dwc->direction = sconfig->direction; |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 930 | |
| 931 | convert_burst(&dwc->dma_sconfig.src_maxburst); |
| 932 | convert_burst(&dwc->dma_sconfig.dst_maxburst); |
| 933 | |
| 934 | return 0; |
| 935 | } |
| 936 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 937 | static int dwc_pause(struct dma_chan *chan) |
Andy Shevchenko | 21fe3c5 | 2013-01-09 10:17:14 +0200 | [diff] [blame] | 938 | { |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 939 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 940 | unsigned long flags; |
| 941 | unsigned int count = 20; /* timeout iterations */ |
| 942 | u32 cfglo; |
Andy Shevchenko | 21fe3c5 | 2013-01-09 10:17:14 +0200 | [diff] [blame] | 943 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 944 | spin_lock_irqsave(&dwc->lock, flags); |
| 945 | |
| 946 | cfglo = channel_readl(dwc, CFG_LO); |
Andy Shevchenko | 21fe3c5 | 2013-01-09 10:17:14 +0200 | [diff] [blame] | 947 | channel_writel(dwc, CFG_LO, cfglo | DWC_CFGL_CH_SUSP); |
Andy Shevchenko | 123b69a | 2013-03-21 11:49:17 +0200 | [diff] [blame] | 948 | while (!(channel_readl(dwc, CFG_LO) & DWC_CFGL_FIFO_EMPTY) && count--) |
| 949 | udelay(2); |
Andy Shevchenko | 21fe3c5 | 2013-01-09 10:17:14 +0200 | [diff] [blame] | 950 | |
Andy Shevchenko | 5e09f98 | 2016-03-18 16:24:51 +0200 | [diff] [blame] | 951 | set_bit(DW_DMA_IS_PAUSED, &dwc->flags); |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 952 | |
| 953 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 954 | |
| 955 | return 0; |
Andy Shevchenko | 21fe3c5 | 2013-01-09 10:17:14 +0200 | [diff] [blame] | 956 | } |
| 957 | |
| 958 | static inline void dwc_chan_resume(struct dw_dma_chan *dwc) |
| 959 | { |
| 960 | u32 cfglo = channel_readl(dwc, CFG_LO); |
| 961 | |
| 962 | channel_writel(dwc, CFG_LO, cfglo & ~DWC_CFGL_CH_SUSP); |
| 963 | |
Andy Shevchenko | 5e09f98 | 2016-03-18 16:24:51 +0200 | [diff] [blame] | 964 | clear_bit(DW_DMA_IS_PAUSED, &dwc->flags); |
Andy Shevchenko | 21fe3c5 | 2013-01-09 10:17:14 +0200 | [diff] [blame] | 965 | } |
| 966 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 967 | static int dwc_resume(struct dma_chan *chan) |
| 968 | { |
| 969 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 970 | unsigned long flags; |
| 971 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 972 | spin_lock_irqsave(&dwc->lock, flags); |
| 973 | |
Andy Shevchenko | 5e09f98 | 2016-03-18 16:24:51 +0200 | [diff] [blame] | 974 | if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags)) |
| 975 | dwc_chan_resume(dwc); |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 976 | |
| 977 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 978 | |
| 979 | return 0; |
| 980 | } |
| 981 | |
| 982 | static int dwc_terminate_all(struct dma_chan *chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 983 | { |
| 984 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 985 | struct dw_dma *dw = to_dw_dma(chan->device); |
| 986 | struct dw_desc *desc, *_desc; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 987 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 988 | LIST_HEAD(list); |
| 989 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 990 | spin_lock_irqsave(&dwc->lock, flags); |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 991 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 992 | clear_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags); |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 993 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 994 | dwc_chan_disable(dw, dwc); |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 995 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 996 | dwc_chan_resume(dwc); |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 997 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 998 | /* active_list entries will end up before queued entries */ |
| 999 | list_splice_init(&dwc->queue, &list); |
| 1000 | list_splice_init(&dwc->active_list, &list); |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 1001 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 1002 | spin_unlock_irqrestore(&dwc->lock, flags); |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 1003 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 1004 | /* Flush all pending and queued descriptors */ |
| 1005 | list_for_each_entry_safe(desc, _desc, &list, desc_node) |
| 1006 | dwc_descriptor_complete(dwc, desc, false); |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1007 | |
Linus Walleij | c3635c7 | 2010-03-26 16:44:01 -0700 | [diff] [blame] | 1008 | return 0; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1009 | } |
| 1010 | |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 1011 | static struct dw_desc *dwc_find_desc(struct dw_dma_chan *dwc, dma_cookie_t c) |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 1012 | { |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 1013 | struct dw_desc *desc; |
| 1014 | |
| 1015 | list_for_each_entry(desc, &dwc->active_list, desc_node) |
| 1016 | if (desc->txd.cookie == c) |
| 1017 | return desc; |
| 1018 | |
| 1019 | return NULL; |
| 1020 | } |
| 1021 | |
| 1022 | static u32 dwc_get_residue(struct dw_dma_chan *dwc, dma_cookie_t cookie) |
| 1023 | { |
| 1024 | struct dw_desc *desc; |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 1025 | unsigned long flags; |
| 1026 | u32 residue; |
| 1027 | |
| 1028 | spin_lock_irqsave(&dwc->lock, flags); |
| 1029 | |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 1030 | desc = dwc_find_desc(dwc, cookie); |
| 1031 | if (desc) { |
| 1032 | if (desc == dwc_first_active(dwc)) { |
| 1033 | residue = desc->residue; |
| 1034 | if (test_bit(DW_DMA_IS_SOFT_LLP, &dwc->flags) && residue) |
| 1035 | residue -= dwc_get_sent(dwc); |
| 1036 | } else { |
| 1037 | residue = desc->total_len; |
| 1038 | } |
| 1039 | } else { |
| 1040 | residue = 0; |
| 1041 | } |
Andy Shevchenko | 4702d52 | 2013-01-25 11:48:03 +0200 | [diff] [blame] | 1042 | |
| 1043 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1044 | return residue; |
| 1045 | } |
| 1046 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1047 | static enum dma_status |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1048 | dwc_tx_status(struct dma_chan *chan, |
| 1049 | dma_cookie_t cookie, |
| 1050 | struct dma_tx_state *txstate) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1051 | { |
| 1052 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 1053 | enum dma_status ret; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1054 | |
Russell King - ARM Linux | 96a2af4 | 2012-03-06 22:35:27 +0000 | [diff] [blame] | 1055 | ret = dma_cookie_status(chan, cookie, txstate); |
Vinod Koul | 2c40410 | 2013-10-16 13:41:15 +0530 | [diff] [blame] | 1056 | if (ret == DMA_COMPLETE) |
Andy Shevchenko | 12381dc | 2013-07-15 15:04:40 +0300 | [diff] [blame] | 1057 | return ret; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1058 | |
Andy Shevchenko | 12381dc | 2013-07-15 15:04:40 +0300 | [diff] [blame] | 1059 | dwc_scan_descriptors(to_dw_dma(chan->device), dwc); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1060 | |
Andy Shevchenko | 12381dc | 2013-07-15 15:04:40 +0300 | [diff] [blame] | 1061 | ret = dma_cookie_status(chan, cookie, txstate); |
Andy Shevchenko | b68fd09 | 2016-03-18 16:24:53 +0200 | [diff] [blame] | 1062 | if (ret == DMA_COMPLETE) |
| 1063 | return ret; |
| 1064 | |
| 1065 | dma_set_residue(txstate, dwc_get_residue(dwc, cookie)); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1066 | |
Andy Shevchenko | 5e09f98 | 2016-03-18 16:24:51 +0200 | [diff] [blame] | 1067 | if (test_bit(DW_DMA_IS_PAUSED, &dwc->flags) && ret == DMA_IN_PROGRESS) |
Linus Walleij | a7c57cf | 2011-04-19 08:31:32 +0800 | [diff] [blame] | 1068 | return DMA_PAUSED; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1069 | |
| 1070 | return ret; |
| 1071 | } |
| 1072 | |
| 1073 | static void dwc_issue_pending(struct dma_chan *chan) |
| 1074 | { |
| 1075 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Andy Shevchenko | dd8ecfca | 2014-06-18 12:15:38 +0300 | [diff] [blame] | 1076 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1077 | |
Andy Shevchenko | dd8ecfca | 2014-06-18 12:15:38 +0300 | [diff] [blame] | 1078 | spin_lock_irqsave(&dwc->lock, flags); |
| 1079 | if (list_empty(&dwc->active_list)) |
| 1080 | dwc_dostart_first_queued(dwc); |
| 1081 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1082 | } |
| 1083 | |
Andy Shevchenko | 99d9bf4 | 2014-09-23 17:18:14 +0300 | [diff] [blame] | 1084 | /*----------------------------------------------------------------------*/ |
| 1085 | |
| 1086 | static void dw_dma_off(struct dw_dma *dw) |
| 1087 | { |
Andy Shevchenko | 7794e5b | 2016-03-18 16:24:48 +0200 | [diff] [blame] | 1088 | unsigned int i; |
Andy Shevchenko | 99d9bf4 | 2014-09-23 17:18:14 +0300 | [diff] [blame] | 1089 | |
| 1090 | dma_writel(dw, CFG, 0); |
| 1091 | |
| 1092 | channel_clear_bit(dw, MASK.XFER, dw->all_chan_mask); |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 1093 | channel_clear_bit(dw, MASK.BLOCK, dw->all_chan_mask); |
Andy Shevchenko | 99d9bf4 | 2014-09-23 17:18:14 +0300 | [diff] [blame] | 1094 | channel_clear_bit(dw, MASK.SRC_TRAN, dw->all_chan_mask); |
| 1095 | channel_clear_bit(dw, MASK.DST_TRAN, dw->all_chan_mask); |
| 1096 | channel_clear_bit(dw, MASK.ERROR, dw->all_chan_mask); |
| 1097 | |
| 1098 | while (dma_readl(dw, CFG) & DW_CFG_DMA_EN) |
| 1099 | cpu_relax(); |
| 1100 | |
| 1101 | for (i = 0; i < dw->dma.chancnt; i++) |
Andy Shevchenko | 423f9cb | 2016-03-18 16:24:52 +0200 | [diff] [blame] | 1102 | clear_bit(DW_DMA_IS_INITIALIZED, &dw->chan[i].flags); |
Andy Shevchenko | 99d9bf4 | 2014-09-23 17:18:14 +0300 | [diff] [blame] | 1103 | } |
| 1104 | |
| 1105 | static void dw_dma_on(struct dw_dma *dw) |
| 1106 | { |
| 1107 | dma_writel(dw, CFG, DW_CFG_DMA_EN); |
| 1108 | } |
| 1109 | |
Dan Williams | aa1e6f1 | 2009-01-06 11:38:17 -0700 | [diff] [blame] | 1110 | static int dwc_alloc_chan_resources(struct dma_chan *chan) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1111 | { |
| 1112 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1113 | struct dw_dma *dw = to_dw_dma(chan->device); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1114 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame] | 1115 | dev_vdbg(chan2dev(chan), "%s\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1116 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1117 | /* ASSERT: channel is idle */ |
| 1118 | if (dma_readl(dw, CH_EN) & dwc->mask) { |
Dan Williams | 41d5e59 | 2009-01-06 11:38:21 -0700 | [diff] [blame] | 1119 | dev_dbg(chan2dev(chan), "DMA channel not idle?\n"); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1120 | return -EIO; |
| 1121 | } |
| 1122 | |
Russell King - ARM Linux | d3ee98cdc | 2012-03-06 22:35:47 +0000 | [diff] [blame] | 1123 | dma_cookie_init(chan); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1124 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1125 | /* |
| 1126 | * NOTE: some controllers may have additional features that we |
| 1127 | * need to initialize here, like "scatter-gather" (which |
| 1128 | * doesn't mean what you think it means), and status writeback. |
| 1129 | */ |
| 1130 | |
Andy Shevchenko | 3fe6409 | 2016-04-08 16:22:17 +0300 | [diff] [blame] | 1131 | /* |
| 1132 | * We need controller-specific data to set up slave transfers. |
| 1133 | */ |
| 1134 | if (chan->private && !dw_dma_filter(chan, chan->private)) { |
| 1135 | dev_warn(chan2dev(chan), "Wrong controller-specific data\n"); |
| 1136 | return -EINVAL; |
| 1137 | } |
| 1138 | |
Andy Shevchenko | 99d9bf4 | 2014-09-23 17:18:14 +0300 | [diff] [blame] | 1139 | /* Enable controller here if needed */ |
| 1140 | if (!dw->in_use) |
| 1141 | dw_dma_on(dw); |
| 1142 | dw->in_use |= dwc->mask; |
| 1143 | |
Christian Lamparter | ab703f8 | 2016-04-14 18:11:01 +0200 | [diff] [blame] | 1144 | return 0; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1145 | } |
| 1146 | |
| 1147 | static void dwc_free_chan_resources(struct dma_chan *chan) |
| 1148 | { |
| 1149 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1150 | struct dw_dma *dw = to_dw_dma(chan->device); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1151 | unsigned long flags; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1152 | LIST_HEAD(list); |
| 1153 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame] | 1154 | dev_dbg(chan2dev(chan), "%s: descs allocated=%u\n", __func__, |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1155 | dwc->descs_allocated); |
| 1156 | |
| 1157 | /* ASSERT: channel is idle */ |
| 1158 | BUG_ON(!list_empty(&dwc->active_list)); |
| 1159 | BUG_ON(!list_empty(&dwc->queue)); |
| 1160 | BUG_ON(dma_readl(to_dw_dma(chan->device), CH_EN) & dwc->mask); |
| 1161 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1162 | spin_lock_irqsave(&dwc->lock, flags); |
Andy Shevchenko | 3fe6409 | 2016-04-08 16:22:17 +0300 | [diff] [blame] | 1163 | |
| 1164 | /* Clear custom channel configuration */ |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 1165 | memset(&dwc->dws, 0, sizeof(struct dw_dma_slave)); |
Andy Shevchenko | 3fe6409 | 2016-04-08 16:22:17 +0300 | [diff] [blame] | 1166 | |
Andy Shevchenko | 423f9cb | 2016-03-18 16:24:52 +0200 | [diff] [blame] | 1167 | clear_bit(DW_DMA_IS_INITIALIZED, &dwc->flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1168 | |
| 1169 | /* Disable interrupts */ |
| 1170 | channel_clear_bit(dw, MASK.XFER, dwc->mask); |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 1171 | channel_clear_bit(dw, MASK.BLOCK, dwc->mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1172 | channel_clear_bit(dw, MASK.ERROR, dwc->mask); |
| 1173 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1174 | spin_unlock_irqrestore(&dwc->lock, flags); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1175 | |
Andy Shevchenko | 99d9bf4 | 2014-09-23 17:18:14 +0300 | [diff] [blame] | 1176 | /* Disable controller in case it was a last user */ |
| 1177 | dw->in_use &= ~dwc->mask; |
| 1178 | if (!dw->in_use) |
| 1179 | dw_dma_off(dw); |
| 1180 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame] | 1181 | dev_vdbg(chan2dev(chan), "%s: done\n", __func__); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1182 | } |
| 1183 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1184 | /* --------------------- Cyclic DMA API extensions -------------------- */ |
| 1185 | |
| 1186 | /** |
| 1187 | * dw_dma_cyclic_start - start the cyclic DMA transfer |
| 1188 | * @chan: the DMA channel to start |
| 1189 | * |
| 1190 | * Must be called with soft interrupts disabled. Returns zero on success or |
| 1191 | * -errno on failure. |
| 1192 | */ |
| 1193 | int dw_dma_cyclic_start(struct dma_chan *chan) |
| 1194 | { |
| 1195 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Andy Shevchenko | ee1cdcd | 2016-02-10 15:59:42 +0200 | [diff] [blame] | 1196 | struct dw_dma *dw = to_dw_dma(chan->device); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1197 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1198 | |
| 1199 | if (!test_bit(DW_DMA_IS_CYCLIC, &dwc->flags)) { |
| 1200 | dev_err(chan2dev(&dwc->chan), "missing prep for cyclic DMA\n"); |
| 1201 | return -ENODEV; |
| 1202 | } |
| 1203 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1204 | spin_lock_irqsave(&dwc->lock, flags); |
Andy Shevchenko | ee1cdcd | 2016-02-10 15:59:42 +0200 | [diff] [blame] | 1205 | |
| 1206 | /* Enable interrupts to perform cyclic transfer */ |
| 1207 | channel_set_bit(dw, MASK.BLOCK, dwc->mask); |
| 1208 | |
Mans Rullgard | df3bb8a | 2016-01-11 13:04:28 +0000 | [diff] [blame] | 1209 | dwc_dostart(dwc, dwc->cdesc->desc[0]); |
Andy Shevchenko | ee1cdcd | 2016-02-10 15:59:42 +0200 | [diff] [blame] | 1210 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1211 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1212 | |
| 1213 | return 0; |
| 1214 | } |
| 1215 | EXPORT_SYMBOL(dw_dma_cyclic_start); |
| 1216 | |
| 1217 | /** |
| 1218 | * dw_dma_cyclic_stop - stop the cyclic DMA transfer |
| 1219 | * @chan: the DMA channel to stop |
| 1220 | * |
| 1221 | * Must be called with soft interrupts disabled. |
| 1222 | */ |
| 1223 | void dw_dma_cyclic_stop(struct dma_chan *chan) |
| 1224 | { |
| 1225 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1226 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1227 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1228 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1229 | spin_lock_irqsave(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1230 | |
Andy Shevchenko | 3f936207 | 2012-06-19 13:46:32 +0300 | [diff] [blame] | 1231 | dwc_chan_disable(dw, dwc); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1232 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1233 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1234 | } |
| 1235 | EXPORT_SYMBOL(dw_dma_cyclic_stop); |
| 1236 | |
| 1237 | /** |
| 1238 | * dw_dma_cyclic_prep - prepare the cyclic DMA transfer |
| 1239 | * @chan: the DMA channel to prepare |
| 1240 | * @buf_addr: physical DMA address where the buffer starts |
| 1241 | * @buf_len: total number of bytes for the entire buffer |
| 1242 | * @period_len: number of bytes for each period |
| 1243 | * @direction: transfer direction, to or from device |
| 1244 | * |
| 1245 | * Must be called before trying to start the transfer. Returns a valid struct |
| 1246 | * dw_cyclic_desc if successful or an ERR_PTR(-errno) if not successful. |
| 1247 | */ |
| 1248 | struct dw_cyclic_desc *dw_dma_cyclic_prep(struct dma_chan *chan, |
| 1249 | dma_addr_t buf_addr, size_t buf_len, size_t period_len, |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1250 | enum dma_transfer_direction direction) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1251 | { |
| 1252 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1253 | struct dma_slave_config *sconfig = &dwc->dma_sconfig; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1254 | struct dw_cyclic_desc *cdesc; |
| 1255 | struct dw_cyclic_desc *retval = NULL; |
| 1256 | struct dw_desc *desc; |
| 1257 | struct dw_desc *last = NULL; |
Andy Shevchenko | 9217a5b | 2016-08-17 19:20:20 +0300 | [diff] [blame^] | 1258 | u8 lms = DWC_LLP_LMS(dwc->dws.m_master); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1259 | unsigned long was_cyclic; |
| 1260 | unsigned int reg_width; |
| 1261 | unsigned int periods; |
| 1262 | unsigned int i; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1263 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1264 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1265 | spin_lock_irqsave(&dwc->lock, flags); |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 1266 | if (dwc->nollp) { |
| 1267 | spin_unlock_irqrestore(&dwc->lock, flags); |
| 1268 | dev_dbg(chan2dev(&dwc->chan), |
| 1269 | "channel doesn't support LLP transfers\n"); |
| 1270 | return ERR_PTR(-EINVAL); |
| 1271 | } |
| 1272 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1273 | if (!list_empty(&dwc->queue) || !list_empty(&dwc->active_list)) { |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1274 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1275 | dev_dbg(chan2dev(&dwc->chan), |
| 1276 | "queue and/or active list are not empty\n"); |
| 1277 | return ERR_PTR(-EBUSY); |
| 1278 | } |
| 1279 | |
| 1280 | was_cyclic = test_and_set_bit(DW_DMA_IS_CYCLIC, &dwc->flags); |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1281 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1282 | if (was_cyclic) { |
| 1283 | dev_dbg(chan2dev(&dwc->chan), |
| 1284 | "channel already prepared for cyclic DMA\n"); |
| 1285 | return ERR_PTR(-EBUSY); |
| 1286 | } |
| 1287 | |
| 1288 | retval = ERR_PTR(-EINVAL); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1289 | |
Andy Shevchenko | f44b92f | 2013-01-10 10:52:58 +0200 | [diff] [blame] | 1290 | if (unlikely(!is_slave_direction(direction))) |
| 1291 | goto out_err; |
| 1292 | |
Andy Shevchenko | 0fdb567 | 2013-01-10 10:53:03 +0200 | [diff] [blame] | 1293 | dwc->direction = direction; |
| 1294 | |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1295 | if (direction == DMA_MEM_TO_DEV) |
| 1296 | reg_width = __ffs(sconfig->dst_addr_width); |
| 1297 | else |
| 1298 | reg_width = __ffs(sconfig->src_addr_width); |
| 1299 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1300 | periods = buf_len / period_len; |
| 1301 | |
| 1302 | /* Check for too big/unaligned periods and unaligned DMA buffer. */ |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 1303 | if (period_len > (dwc->block_size << reg_width)) |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1304 | goto out_err; |
| 1305 | if (unlikely(period_len & ((1 << reg_width) - 1))) |
| 1306 | goto out_err; |
| 1307 | if (unlikely(buf_addr & ((1 << reg_width) - 1))) |
| 1308 | goto out_err; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1309 | |
| 1310 | retval = ERR_PTR(-ENOMEM); |
| 1311 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1312 | cdesc = kzalloc(sizeof(struct dw_cyclic_desc), GFP_KERNEL); |
| 1313 | if (!cdesc) |
| 1314 | goto out_err; |
| 1315 | |
| 1316 | cdesc->desc = kzalloc(sizeof(struct dw_desc *) * periods, GFP_KERNEL); |
| 1317 | if (!cdesc->desc) |
| 1318 | goto out_err_alloc; |
| 1319 | |
| 1320 | for (i = 0; i < periods; i++) { |
| 1321 | desc = dwc_desc_get(dwc); |
| 1322 | if (!desc) |
| 1323 | goto out_err_desc_get; |
| 1324 | |
| 1325 | switch (direction) { |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1326 | case DMA_MEM_TO_DEV: |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 1327 | lli_write(desc, dar, sconfig->dst_addr); |
| 1328 | lli_write(desc, sar, buf_addr + period_len * i); |
| 1329 | lli_write(desc, ctllo, (DWC_DEFAULT_CTLLO(chan) |
| 1330 | | DWC_CTLL_DST_WIDTH(reg_width) |
| 1331 | | DWC_CTLL_SRC_WIDTH(reg_width) |
| 1332 | | DWC_CTLL_DST_FIX |
| 1333 | | DWC_CTLL_SRC_INC |
| 1334 | | DWC_CTLL_INT_EN)); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1335 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 1336 | lli_set(desc, ctllo, sconfig->device_fc ? |
| 1337 | DWC_CTLL_FC(DW_DMA_FC_P_M2P) : |
| 1338 | DWC_CTLL_FC(DW_DMA_FC_D_M2P)); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1339 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1340 | break; |
Vinod Koul | db8196d | 2011-10-13 22:34:23 +0530 | [diff] [blame] | 1341 | case DMA_DEV_TO_MEM: |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 1342 | lli_write(desc, dar, buf_addr + period_len * i); |
| 1343 | lli_write(desc, sar, sconfig->src_addr); |
| 1344 | lli_write(desc, ctllo, (DWC_DEFAULT_CTLLO(chan) |
| 1345 | | DWC_CTLL_SRC_WIDTH(reg_width) |
| 1346 | | DWC_CTLL_DST_WIDTH(reg_width) |
| 1347 | | DWC_CTLL_DST_INC |
| 1348 | | DWC_CTLL_SRC_FIX |
| 1349 | | DWC_CTLL_INT_EN)); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1350 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 1351 | lli_set(desc, ctllo, sconfig->device_fc ? |
| 1352 | DWC_CTLL_FC(DW_DMA_FC_P_P2M) : |
| 1353 | DWC_CTLL_FC(DW_DMA_FC_D_P2M)); |
Viresh Kumar | 327e697 | 2012-02-01 16:12:26 +0530 | [diff] [blame] | 1354 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1355 | break; |
| 1356 | default: |
| 1357 | break; |
| 1358 | } |
| 1359 | |
Mans Rullgard | df1f3a2 | 2016-03-18 16:24:43 +0200 | [diff] [blame] | 1360 | lli_write(desc, ctlhi, period_len >> reg_width); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1361 | cdesc->desc[i] = desc; |
| 1362 | |
Andy Shevchenko | f8122a8 | 2013-01-16 15:48:50 +0200 | [diff] [blame] | 1363 | if (last) |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 1364 | lli_write(last, llp, desc->txd.phys | lms); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1365 | |
| 1366 | last = desc; |
| 1367 | } |
| 1368 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 1369 | /* Let's make a cyclic list */ |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 1370 | lli_write(last, llp, cdesc->desc[0]->txd.phys | lms); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1371 | |
Andy Shevchenko | 5a87f0e | 2014-01-13 14:04:50 +0200 | [diff] [blame] | 1372 | dev_dbg(chan2dev(&dwc->chan), |
| 1373 | "cyclic prepared buf %pad len %zu period %zu periods %d\n", |
| 1374 | &buf_addr, buf_len, period_len, periods); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1375 | |
| 1376 | cdesc->periods = periods; |
| 1377 | dwc->cdesc = cdesc; |
| 1378 | |
| 1379 | return cdesc; |
| 1380 | |
| 1381 | out_err_desc_get: |
| 1382 | while (i--) |
| 1383 | dwc_desc_put(dwc, cdesc->desc[i]); |
| 1384 | out_err_alloc: |
| 1385 | kfree(cdesc); |
| 1386 | out_err: |
| 1387 | clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags); |
| 1388 | return (struct dw_cyclic_desc *)retval; |
| 1389 | } |
| 1390 | EXPORT_SYMBOL(dw_dma_cyclic_prep); |
| 1391 | |
| 1392 | /** |
| 1393 | * dw_dma_cyclic_free - free a prepared cyclic DMA transfer |
| 1394 | * @chan: the DMA channel to free |
| 1395 | */ |
| 1396 | void dw_dma_cyclic_free(struct dma_chan *chan) |
| 1397 | { |
| 1398 | struct dw_dma_chan *dwc = to_dw_dma_chan(chan); |
| 1399 | struct dw_dma *dw = to_dw_dma(dwc->chan.device); |
| 1400 | struct dw_cyclic_desc *cdesc = dwc->cdesc; |
Andy Shevchenko | 7794e5b | 2016-03-18 16:24:48 +0200 | [diff] [blame] | 1401 | unsigned int i; |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1402 | unsigned long flags; |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1403 | |
Andy Shevchenko | 2e4c364 | 2012-06-19 13:34:05 +0300 | [diff] [blame] | 1404 | dev_dbg(chan2dev(&dwc->chan), "%s\n", __func__); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1405 | |
| 1406 | if (!cdesc) |
| 1407 | return; |
| 1408 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1409 | spin_lock_irqsave(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1410 | |
Andy Shevchenko | 3f936207 | 2012-06-19 13:46:32 +0300 | [diff] [blame] | 1411 | dwc_chan_disable(dw, dwc); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1412 | |
Mans Rullgard | 2895b2c | 2016-01-11 13:04:29 +0000 | [diff] [blame] | 1413 | dma_writel(dw, CLEAR.BLOCK, dwc->mask); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1414 | dma_writel(dw, CLEAR.ERROR, dwc->mask); |
| 1415 | dma_writel(dw, CLEAR.XFER, dwc->mask); |
| 1416 | |
Viresh Kumar | 69cea5a | 2011-04-15 16:03:35 +0530 | [diff] [blame] | 1417 | spin_unlock_irqrestore(&dwc->lock, flags); |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1418 | |
| 1419 | for (i = 0; i < cdesc->periods; i++) |
| 1420 | dwc_desc_put(dwc, cdesc->desc[i]); |
| 1421 | |
| 1422 | kfree(cdesc->desc); |
| 1423 | kfree(cdesc); |
| 1424 | |
Andy Shevchenko | 925a7d0 | 2016-03-18 16:24:54 +0200 | [diff] [blame] | 1425 | dwc->cdesc = NULL; |
| 1426 | |
Hans-Christian Egtvedt | d9de451 | 2009-04-01 15:47:02 +0200 | [diff] [blame] | 1427 | clear_bit(DW_DMA_IS_CYCLIC, &dwc->flags); |
| 1428 | } |
| 1429 | EXPORT_SYMBOL(dw_dma_cyclic_free); |
| 1430 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1431 | /*----------------------------------------------------------------------*/ |
| 1432 | |
Andy Shevchenko | 3a14c66 | 2016-04-27 14:15:40 +0300 | [diff] [blame] | 1433 | int dw_dma_probe(struct dw_dma_chip *chip) |
Viresh Kumar | a9ddb57 | 2012-10-16 09:49:17 +0530 | [diff] [blame] | 1434 | { |
Andy Shevchenko | 3a14c66 | 2016-04-27 14:15:40 +0300 | [diff] [blame] | 1435 | struct dw_dma_platform_data *pdata; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1436 | struct dw_dma *dw; |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1437 | bool autocfg = false; |
Andy Shevchenko | 482c67e | 2012-09-21 15:05:46 +0300 | [diff] [blame] | 1438 | unsigned int dw_params; |
Andy Shevchenko | 7794e5b | 2016-03-18 16:24:48 +0200 | [diff] [blame] | 1439 | unsigned int i; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1440 | int err; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1441 | |
Andy Shevchenko | 000871c | 2014-03-05 15:48:12 +0200 | [diff] [blame] | 1442 | dw = devm_kzalloc(chip->dev, sizeof(*dw), GFP_KERNEL); |
| 1443 | if (!dw) |
| 1444 | return -ENOMEM; |
| 1445 | |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 1446 | dw->pdata = devm_kzalloc(chip->dev, sizeof(*dw->pdata), GFP_KERNEL); |
| 1447 | if (!dw->pdata) |
| 1448 | return -ENOMEM; |
| 1449 | |
Andy Shevchenko | 000871c | 2014-03-05 15:48:12 +0200 | [diff] [blame] | 1450 | dw->regs = chip->regs; |
| 1451 | chip->dw = dw; |
| 1452 | |
Andy Shevchenko | bb32baf | 2014-11-05 18:34:48 +0200 | [diff] [blame] | 1453 | pm_runtime_get_sync(chip->dev); |
| 1454 | |
Andy Shevchenko | 3a14c66 | 2016-04-27 14:15:40 +0300 | [diff] [blame] | 1455 | if (!chip->pdata) { |
Andy Shevchenko | 897e40d | 2016-03-18 16:24:46 +0200 | [diff] [blame] | 1456 | dw_params = dma_readl(dw, DW_PARAMS); |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1457 | dev_dbg(chip->dev, "DW_PARAMS: 0x%08x\n", dw_params); |
Andy Shevchenko | 482c67e | 2012-09-21 15:05:46 +0300 | [diff] [blame] | 1458 | |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1459 | autocfg = dw_params >> DW_PARAMS_EN & 1; |
| 1460 | if (!autocfg) { |
| 1461 | err = -EINVAL; |
| 1462 | goto err_pdata; |
| 1463 | } |
Andy Shevchenko | 123de54 | 2013-01-09 10:17:01 +0200 | [diff] [blame] | 1464 | |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 1465 | /* Reassign the platform data pointer */ |
| 1466 | pdata = dw->pdata; |
Andy Shevchenko | 123de54 | 2013-01-09 10:17:01 +0200 | [diff] [blame] | 1467 | |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1468 | /* Get hardware configuration parameters */ |
| 1469 | pdata->nr_channels = (dw_params >> DW_PARAMS_NR_CHAN & 7) + 1; |
| 1470 | pdata->nr_masters = (dw_params >> DW_PARAMS_NR_MASTER & 3) + 1; |
| 1471 | for (i = 0; i < pdata->nr_masters; i++) { |
| 1472 | pdata->data_width[i] = |
Andy Shevchenko | 2e65060 | 2016-04-27 14:15:38 +0300 | [diff] [blame] | 1473 | 4 << (dw_params >> DW_PARAMS_DATA_WIDTH(i) & 3); |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1474 | } |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 1475 | pdata->block_size = dma_readl(dw, MAX_BLK_SIZE); |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1476 | |
Andy Shevchenko | 123de54 | 2013-01-09 10:17:01 +0200 | [diff] [blame] | 1477 | /* Fill platform data with the default values */ |
| 1478 | pdata->is_private = true; |
Andy Shevchenko | df5c738 | 2015-10-13 20:09:19 +0300 | [diff] [blame] | 1479 | pdata->is_memcpy = true; |
Andy Shevchenko | 123de54 | 2013-01-09 10:17:01 +0200 | [diff] [blame] | 1480 | pdata->chan_allocation_order = CHAN_ALLOCATION_ASCENDING; |
| 1481 | pdata->chan_priority = CHAN_PRIORITY_ASCENDING; |
Andy Shevchenko | 3a14c66 | 2016-04-27 14:15:40 +0300 | [diff] [blame] | 1482 | } else if (chip->pdata->nr_channels > DW_DMA_MAX_NR_CHANNELS) { |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1483 | err = -EINVAL; |
| 1484 | goto err_pdata; |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 1485 | } else { |
Andy Shevchenko | 3a14c66 | 2016-04-27 14:15:40 +0300 | [diff] [blame] | 1486 | memcpy(dw->pdata, chip->pdata, sizeof(*dw->pdata)); |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 1487 | |
| 1488 | /* Reassign the platform data pointer */ |
| 1489 | pdata = dw->pdata; |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1490 | } |
Andy Shevchenko | 123de54 | 2013-01-09 10:17:01 +0200 | [diff] [blame] | 1491 | |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1492 | dw->chan = devm_kcalloc(chip->dev, pdata->nr_channels, sizeof(*dw->chan), |
Andy Shevchenko | 000871c | 2014-03-05 15:48:12 +0200 | [diff] [blame] | 1493 | GFP_KERNEL); |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1494 | if (!dw->chan) { |
| 1495 | err = -ENOMEM; |
| 1496 | goto err_pdata; |
| 1497 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1498 | |
Andy Shevchenko | 11f932e | 2012-06-19 13:34:06 +0300 | [diff] [blame] | 1499 | /* Calculate all channel mask before DMA setup */ |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1500 | dw->all_chan_mask = (1 << pdata->nr_channels) - 1; |
Andy Shevchenko | 11f932e | 2012-06-19 13:34:06 +0300 | [diff] [blame] | 1501 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 1502 | /* Force dma off, just in case */ |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1503 | dw_dma_off(dw); |
| 1504 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 1505 | /* Create a pool of consistent memory blocks for hardware descriptors */ |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1506 | dw->desc_pool = dmam_pool_create("dw_dmac_desc_pool", chip->dev, |
Andy Shevchenko | f8122a8 | 2013-01-16 15:48:50 +0200 | [diff] [blame] | 1507 | sizeof(struct dw_desc), 4, 0); |
| 1508 | if (!dw->desc_pool) { |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1509 | dev_err(chip->dev, "No memory for descriptors dma pool\n"); |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1510 | err = -ENOMEM; |
| 1511 | goto err_pdata; |
Andy Shevchenko | f8122a8 | 2013-01-16 15:48:50 +0200 | [diff] [blame] | 1512 | } |
| 1513 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1514 | tasklet_init(&dw->tasklet, dw_dma_tasklet, (unsigned long)dw); |
| 1515 | |
Andy Shevchenko | 97977f7 | 2014-05-07 10:56:24 +0300 | [diff] [blame] | 1516 | err = request_irq(chip->irq, dw_dma_interrupt, IRQF_SHARED, |
| 1517 | "dw_dmac", dw); |
| 1518 | if (err) |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1519 | goto err_pdata; |
Andy Shevchenko | 97977f7 | 2014-05-07 10:56:24 +0300 | [diff] [blame] | 1520 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1521 | INIT_LIST_HEAD(&dw->dma.channels); |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1522 | for (i = 0; i < pdata->nr_channels; i++) { |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1523 | struct dw_dma_chan *dwc = &dw->chan[i]; |
| 1524 | |
| 1525 | dwc->chan.device = &dw->dma; |
Russell King - ARM Linux | d3ee98cdc | 2012-03-06 22:35:47 +0000 | [diff] [blame] | 1526 | dma_cookie_init(&dwc->chan); |
Viresh Kumar | b0c3130 | 2011-03-03 15:47:21 +0530 | [diff] [blame] | 1527 | if (pdata->chan_allocation_order == CHAN_ALLOCATION_ASCENDING) |
| 1528 | list_add_tail(&dwc->chan.device_node, |
| 1529 | &dw->dma.channels); |
| 1530 | else |
| 1531 | list_add(&dwc->chan.device_node, &dw->dma.channels); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1532 | |
Viresh Kumar | 93317e8 | 2011-03-03 15:47:22 +0530 | [diff] [blame] | 1533 | /* 7 is highest priority & 0 is lowest. */ |
| 1534 | if (pdata->chan_priority == CHAN_PRIORITY_ASCENDING) |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1535 | dwc->priority = pdata->nr_channels - i - 1; |
Viresh Kumar | 93317e8 | 2011-03-03 15:47:22 +0530 | [diff] [blame] | 1536 | else |
| 1537 | dwc->priority = i; |
| 1538 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1539 | dwc->ch_regs = &__dw_regs(dw)->CHAN[i]; |
| 1540 | spin_lock_init(&dwc->lock); |
| 1541 | dwc->mask = 1 << i; |
| 1542 | |
| 1543 | INIT_LIST_HEAD(&dwc->active_list); |
| 1544 | INIT_LIST_HEAD(&dwc->queue); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1545 | |
| 1546 | channel_clear_bit(dw, CH_EN, dwc->mask); |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 1547 | |
Andy Shevchenko | 0fdb567 | 2013-01-10 10:53:03 +0200 | [diff] [blame] | 1548 | dwc->direction = DMA_TRANS_NONE; |
Andy Shevchenko | a098200 | 2012-09-21 15:05:48 +0300 | [diff] [blame] | 1549 | |
Andy Shevchenko | 75c6122 | 2013-03-26 16:53:54 +0200 | [diff] [blame] | 1550 | /* Hardware configuration */ |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 1551 | if (autocfg) { |
Andy Shevchenko | 6bea0f6 | 2015-09-28 18:57:03 +0300 | [diff] [blame] | 1552 | unsigned int r = DW_DMA_MAX_NR_CHANNELS - i - 1; |
Andy Shevchenko | 897e40d | 2016-03-18 16:24:46 +0200 | [diff] [blame] | 1553 | void __iomem *addr = &__dw_regs(dw)->DWC_PARAMS[r]; |
| 1554 | unsigned int dwc_params = dma_readl_native(addr); |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 1555 | |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1556 | dev_dbg(chip->dev, "DWC_PARAMS[%d]: 0x%08x\n", i, |
| 1557 | dwc_params); |
Andy Shevchenko | 985a6c7 | 2013-01-18 17:10:59 +0200 | [diff] [blame] | 1558 | |
Andy Shevchenko | 1d566f1 | 2014-01-13 14:04:48 +0200 | [diff] [blame] | 1559 | /* |
| 1560 | * Decode maximum block size for given channel. The |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 1561 | * stored 4 bit value represents blocks from 0x00 for 3 |
Andy Shevchenko | 1d566f1 | 2014-01-13 14:04:48 +0200 | [diff] [blame] | 1562 | * up to 0x0a for 4095. |
| 1563 | */ |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 1564 | dwc->block_size = |
Andy Shevchenko | 161c3d0 | 2016-04-27 14:15:39 +0300 | [diff] [blame] | 1565 | (4 << ((pdata->block_size >> 4 * i) & 0xf)) - 1; |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 1566 | dwc->nollp = |
| 1567 | (dwc_params >> DWC_PARAMS_MBLK_EN & 0x1) == 0; |
| 1568 | } else { |
Andy Shevchenko | 4a63a8b | 2012-09-21 15:05:47 +0300 | [diff] [blame] | 1569 | dwc->block_size = pdata->block_size; |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 1570 | |
| 1571 | /* Check if channel supports multi block transfer */ |
Mans Rullgard | 2a0fae0 | 2016-03-18 16:24:44 +0200 | [diff] [blame] | 1572 | channel_writel(dwc, LLP, DWC_LLP_LOC(0xffffffff)); |
| 1573 | dwc->nollp = DWC_LLP_LOC(channel_readl(dwc, LLP)) == 0; |
Andy Shevchenko | fed2574 | 2012-09-21 15:05:49 +0300 | [diff] [blame] | 1574 | channel_writel(dwc, LLP, 0); |
| 1575 | } |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1576 | } |
| 1577 | |
Andy Shevchenko | 11f932e | 2012-06-19 13:34:06 +0300 | [diff] [blame] | 1578 | /* Clear all interrupts on all channels. */ |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1579 | dma_writel(dw, CLEAR.XFER, dw->all_chan_mask); |
Andy Shevchenko | 236b106 | 2012-06-19 13:34:07 +0300 | [diff] [blame] | 1580 | dma_writel(dw, CLEAR.BLOCK, dw->all_chan_mask); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1581 | dma_writel(dw, CLEAR.SRC_TRAN, dw->all_chan_mask); |
| 1582 | dma_writel(dw, CLEAR.DST_TRAN, dw->all_chan_mask); |
| 1583 | dma_writel(dw, CLEAR.ERROR, dw->all_chan_mask); |
| 1584 | |
Andy Shevchenko | df5c738 | 2015-10-13 20:09:19 +0300 | [diff] [blame] | 1585 | /* Set capabilities */ |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1586 | dma_cap_set(DMA_SLAVE, dw->dma.cap_mask); |
Jamie Iles | 95ea759 | 2011-01-21 14:11:54 +0000 | [diff] [blame] | 1587 | if (pdata->is_private) |
| 1588 | dma_cap_set(DMA_PRIVATE, dw->dma.cap_mask); |
Andy Shevchenko | df5c738 | 2015-10-13 20:09:19 +0300 | [diff] [blame] | 1589 | if (pdata->is_memcpy) |
| 1590 | dma_cap_set(DMA_MEMCPY, dw->dma.cap_mask); |
| 1591 | |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1592 | dw->dma.dev = chip->dev; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1593 | dw->dma.device_alloc_chan_resources = dwc_alloc_chan_resources; |
| 1594 | dw->dma.device_free_chan_resources = dwc_free_chan_resources; |
| 1595 | |
| 1596 | dw->dma.device_prep_dma_memcpy = dwc_prep_dma_memcpy; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1597 | dw->dma.device_prep_slave_sg = dwc_prep_slave_sg; |
Andy Shevchenko | 029a40e | 2015-01-02 16:17:24 +0200 | [diff] [blame] | 1598 | |
Maxime Ripard | a4b0d34 | 2014-11-17 14:42:12 +0100 | [diff] [blame] | 1599 | dw->dma.device_config = dwc_config; |
| 1600 | dw->dma.device_pause = dwc_pause; |
| 1601 | dw->dma.device_resume = dwc_resume; |
| 1602 | dw->dma.device_terminate_all = dwc_terminate_all; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1603 | |
Linus Walleij | 0793448 | 2010-03-26 16:50:49 -0700 | [diff] [blame] | 1604 | dw->dma.device_tx_status = dwc_tx_status; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1605 | dw->dma.device_issue_pending = dwc_issue_pending; |
| 1606 | |
Andy Shevchenko | 029a40e | 2015-01-02 16:17:24 +0200 | [diff] [blame] | 1607 | /* DMA capabilities */ |
| 1608 | dw->dma.src_addr_widths = DW_DMA_BUSWIDTHS; |
| 1609 | dw->dma.dst_addr_widths = DW_DMA_BUSWIDTHS; |
| 1610 | dw->dma.directions = BIT(DMA_DEV_TO_MEM) | BIT(DMA_MEM_TO_DEV) | |
| 1611 | BIT(DMA_MEM_TO_MEM); |
| 1612 | dw->dma.residue_granularity = DMA_RESIDUE_GRANULARITY_BURST; |
| 1613 | |
Andy Shevchenko | 1222934 | 2014-05-08 12:01:50 +0300 | [diff] [blame] | 1614 | err = dma_async_device_register(&dw->dma); |
| 1615 | if (err) |
| 1616 | goto err_dma_register; |
| 1617 | |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1618 | dev_info(chip->dev, "DesignWare DMA Controller, %d channels\n", |
Andy Shevchenko | 30cb263 | 2015-10-13 20:09:17 +0300 | [diff] [blame] | 1619 | pdata->nr_channels); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1620 | |
Andy Shevchenko | bb32baf | 2014-11-05 18:34:48 +0200 | [diff] [blame] | 1621 | pm_runtime_put_sync_suspend(chip->dev); |
| 1622 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1623 | return 0; |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1624 | |
Andy Shevchenko | 1222934 | 2014-05-08 12:01:50 +0300 | [diff] [blame] | 1625 | err_dma_register: |
| 1626 | free_irq(chip->irq, dw); |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1627 | err_pdata: |
Andy Shevchenko | bb32baf | 2014-11-05 18:34:48 +0200 | [diff] [blame] | 1628 | pm_runtime_put_sync_suspend(chip->dev); |
Andy Shevchenko | 8be4f52 | 2014-05-08 12:01:49 +0300 | [diff] [blame] | 1629 | return err; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1630 | } |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1631 | EXPORT_SYMBOL_GPL(dw_dma_probe); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1632 | |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1633 | int dw_dma_remove(struct dw_dma_chip *chip) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1634 | { |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1635 | struct dw_dma *dw = chip->dw; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1636 | struct dw_dma_chan *dwc, *_dwc; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1637 | |
Andy Shevchenko | bb32baf | 2014-11-05 18:34:48 +0200 | [diff] [blame] | 1638 | pm_runtime_get_sync(chip->dev); |
| 1639 | |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1640 | dw_dma_off(dw); |
| 1641 | dma_async_device_unregister(&dw->dma); |
| 1642 | |
Andy Shevchenko | 97977f7 | 2014-05-07 10:56:24 +0300 | [diff] [blame] | 1643 | free_irq(chip->irq, dw); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1644 | tasklet_kill(&dw->tasklet); |
| 1645 | |
| 1646 | list_for_each_entry_safe(dwc, _dwc, &dw->dma.channels, |
| 1647 | chan.device_node) { |
| 1648 | list_del(&dwc->chan.device_node); |
| 1649 | channel_clear_bit(dw, CH_EN, dwc->mask); |
| 1650 | } |
| 1651 | |
Andy Shevchenko | bb32baf | 2014-11-05 18:34:48 +0200 | [diff] [blame] | 1652 | pm_runtime_put_sync_suspend(chip->dev); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1653 | return 0; |
| 1654 | } |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1655 | EXPORT_SYMBOL_GPL(dw_dma_remove); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1656 | |
Andy Shevchenko | 2540f74 | 2014-09-23 17:18:13 +0300 | [diff] [blame] | 1657 | int dw_dma_disable(struct dw_dma_chip *chip) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1658 | { |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1659 | struct dw_dma *dw = chip->dw; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1660 | |
Andy Shevchenko | 6168d56 | 2012-10-18 17:34:10 +0300 | [diff] [blame] | 1661 | dw_dma_off(dw); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1662 | return 0; |
| 1663 | } |
Andy Shevchenko | 2540f74 | 2014-09-23 17:18:13 +0300 | [diff] [blame] | 1664 | EXPORT_SYMBOL_GPL(dw_dma_disable); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1665 | |
Andy Shevchenko | 2540f74 | 2014-09-23 17:18:13 +0300 | [diff] [blame] | 1666 | int dw_dma_enable(struct dw_dma_chip *chip) |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1667 | { |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1668 | struct dw_dma *dw = chip->dw; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1669 | |
Andy Shevchenko | 7a83c04 | 2014-09-23 17:18:12 +0300 | [diff] [blame] | 1670 | dw_dma_on(dw); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1671 | return 0; |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1672 | } |
Andy Shevchenko | 2540f74 | 2014-09-23 17:18:13 +0300 | [diff] [blame] | 1673 | EXPORT_SYMBOL_GPL(dw_dma_enable); |
Haavard Skinnemoen | 3bfb1d2 | 2008-07-08 11:59:42 -0700 | [diff] [blame] | 1674 | |
| 1675 | MODULE_LICENSE("GPL v2"); |
Andy Shevchenko | 9cade1a | 2013-06-05 15:26:45 +0300 | [diff] [blame] | 1676 | MODULE_DESCRIPTION("Synopsys DesignWare DMA Controller core driver"); |
Jean Delvare | e05503e | 2011-05-18 16:49:24 +0200 | [diff] [blame] | 1677 | MODULE_AUTHOR("Haavard Skinnemoen (Atmel)"); |
Viresh Kumar | da89947 | 2015-07-17 16:23:50 -0700 | [diff] [blame] | 1678 | MODULE_AUTHOR("Viresh Kumar <vireshk@kernel.org>"); |