blob: ffb32af94f4cf45f65b4f98e21446d38663e703b [file] [log] [blame]
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001/*
2 * Texas Instruments CPDMA Driver
3 *
4 * Copyright (C) 2010 Texas Instruments
5 *
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation version 2.
9 *
10 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
11 * kind, whether express or implied; without even the implied warranty
12 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 */
15#include <linux/kernel.h>
16#include <linux/spinlock.h>
17#include <linux/device.h>
Daniel Mack76fbc242012-06-28 06:12:32 +000018#include <linux/module.h>
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040019#include <linux/slab.h>
20#include <linux/err.h>
21#include <linux/dma-mapping.h>
22#include <linux/io.h>
Sebastian Siewior817f6d12013-04-23 07:31:35 +000023#include <linux/delay.h>
Grygorii Strashko742fb202016-06-27 12:05:11 +030024#include <linux/genalloc.h>
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040025#include "davinci_cpdma.h"
26
27/* DMA Registers */
28#define CPDMA_TXIDVER 0x00
29#define CPDMA_TXCONTROL 0x04
30#define CPDMA_TXTEARDOWN 0x08
31#define CPDMA_RXIDVER 0x10
32#define CPDMA_RXCONTROL 0x14
33#define CPDMA_SOFTRESET 0x1c
34#define CPDMA_RXTEARDOWN 0x18
35#define CPDMA_TXINTSTATRAW 0x80
36#define CPDMA_TXINTSTATMASKED 0x84
37#define CPDMA_TXINTMASKSET 0x88
38#define CPDMA_TXINTMASKCLEAR 0x8c
39#define CPDMA_MACINVECTOR 0x90
40#define CPDMA_MACEOIVECTOR 0x94
41#define CPDMA_RXINTSTATRAW 0xa0
42#define CPDMA_RXINTSTATMASKED 0xa4
43#define CPDMA_RXINTMASKSET 0xa8
44#define CPDMA_RXINTMASKCLEAR 0xac
45#define CPDMA_DMAINTSTATRAW 0xb0
46#define CPDMA_DMAINTSTATMASKED 0xb4
47#define CPDMA_DMAINTMASKSET 0xb8
48#define CPDMA_DMAINTMASKCLEAR 0xbc
49#define CPDMA_DMAINT_HOSTERR BIT(1)
50
51/* the following exist only if has_ext_regs is set */
52#define CPDMA_DMACONTROL 0x20
53#define CPDMA_DMASTATUS 0x24
54#define CPDMA_RXBUFFOFS 0x28
55#define CPDMA_EM_CONTROL 0x2c
56
57/* Descriptor mode bits */
58#define CPDMA_DESC_SOP BIT(31)
59#define CPDMA_DESC_EOP BIT(30)
60#define CPDMA_DESC_OWNER BIT(29)
61#define CPDMA_DESC_EOQ BIT(28)
62#define CPDMA_DESC_TD_COMPLETE BIT(27)
63#define CPDMA_DESC_PASS_CRC BIT(26)
Mugunthan V Nf6e135c2013-02-11 09:52:18 +000064#define CPDMA_DESC_TO_PORT_EN BIT(20)
65#define CPDMA_TO_PORT_SHIFT 16
66#define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16))
Mugunthan V N28a19fe2013-05-29 20:22:01 +000067#define CPDMA_DESC_CRC_LEN 4
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040068
69#define CPDMA_TEARDOWN_VALUE 0xfffffffc
70
71struct cpdma_desc {
72 /* hardware fields */
73 u32 hw_next;
74 u32 hw_buffer;
75 u32 hw_len;
76 u32 hw_mode;
77 /* software fields */
78 void *sw_token;
79 u32 sw_buffer;
80 u32 sw_len;
81};
82
83struct cpdma_desc_pool {
Olof Johanssonc767db52013-12-11 15:51:20 -080084 phys_addr_t phys;
Arnd Bergmann84092992016-01-29 12:39:10 +010085 dma_addr_t hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040086 void __iomem *iomap; /* ioremap map */
87 void *cpumap; /* dma_alloc map */
88 int desc_size, mem_size;
Grygorii Strashkoaeec3022016-08-04 18:20:51 +030089 int num_desc;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040090 struct device *dev;
Grygorii Strashko742fb202016-06-27 12:05:11 +030091 struct gen_pool *gen_pool;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040092};
93
94enum cpdma_state {
95 CPDMA_STATE_IDLE,
96 CPDMA_STATE_ACTIVE,
97 CPDMA_STATE_TEARDOWN,
98};
99
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400100struct cpdma_ctlr {
101 enum cpdma_state state;
102 struct cpdma_params params;
103 struct device *dev;
104 struct cpdma_desc_pool *pool;
105 spinlock_t lock;
106 struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS];
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300107 int chan_num;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400108};
109
110struct cpdma_chan {
Mugunthan V Nfae50822013-01-17 06:31:34 +0000111 struct cpdma_desc __iomem *head, *tail;
112 void __iomem *hdp, *cp, *rxfree;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400113 enum cpdma_state state;
114 struct cpdma_ctlr *ctlr;
115 int chan_num;
116 spinlock_t lock;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400117 int count;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300118 u32 desc_num;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400119 u32 mask;
120 cpdma_handler_fn handler;
121 enum dma_data_direction dir;
122 struct cpdma_chan_stats stats;
123 /* offsets into dmaregs */
124 int int_set, int_clear, td;
125};
126
127/* The following make access to common cpdma_ctlr params more readable */
128#define dmaregs params.dmaregs
129#define num_chan params.num_chan
130
131/* various accessors */
132#define dma_reg_read(ctlr, ofs) __raw_readl((ctlr)->dmaregs + (ofs))
133#define chan_read(chan, fld) __raw_readl((chan)->fld)
134#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
135#define dma_reg_write(ctlr, ofs, v) __raw_writel(v, (ctlr)->dmaregs + (ofs))
136#define chan_write(chan, fld, v) __raw_writel(v, (chan)->fld)
137#define desc_write(desc, fld, v) __raw_writel((u32)(v), &(desc)->fld)
138
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000139#define cpdma_desc_to_port(chan, mode, directed) \
140 do { \
141 if (!is_rx_chan(chan) && ((directed == 1) || \
142 (directed == 2))) \
143 mode |= (CPDMA_DESC_TO_PORT_EN | \
144 (directed << CPDMA_TO_PORT_SHIFT)); \
145 } while (0)
146
Grygorii Strashko742fb202016-06-27 12:05:11 +0300147static void cpdma_desc_pool_destroy(struct cpdma_desc_pool *pool)
148{
149 if (!pool)
150 return;
151
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300152 WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool),
153 "cpdma_desc_pool size %d != avail %d",
154 gen_pool_size(pool->gen_pool),
155 gen_pool_avail(pool->gen_pool));
Grygorii Strashko742fb202016-06-27 12:05:11 +0300156 if (pool->cpumap)
157 dma_free_coherent(pool->dev, pool->mem_size, pool->cpumap,
158 pool->phys);
159 else
160 iounmap(pool->iomap);
161}
162
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400163/*
164 * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
165 * emac) have dedicated on-chip memory for these descriptors. Some other
166 * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
167 * abstract out these details
168 */
169static struct cpdma_desc_pool *
Arnd Bergmann84092992016-01-29 12:39:10 +0100170cpdma_desc_pool_create(struct device *dev, u32 phys, dma_addr_t hw_addr,
Sriram6a1fef62011-03-22 02:31:03 +0000171 int size, int align)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400172{
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400173 struct cpdma_desc_pool *pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300174 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400175
George Cheriane1943122014-05-12 10:21:21 +0530176 pool = devm_kzalloc(dev, sizeof(*pool), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400177 if (!pool)
Grygorii Strashko742fb202016-06-27 12:05:11 +0300178 goto gen_pool_create_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400179
180 pool->dev = dev;
181 pool->mem_size = size;
182 pool->desc_size = ALIGN(sizeof(struct cpdma_desc), align);
183 pool->num_desc = size / pool->desc_size;
184
Grygorii Strashko742fb202016-06-27 12:05:11 +0300185 pool->gen_pool = devm_gen_pool_create(dev, ilog2(pool->desc_size), -1,
186 "cpdma");
187 if (IS_ERR(pool->gen_pool)) {
188 dev_err(dev, "pool create failed %ld\n",
189 PTR_ERR(pool->gen_pool));
190 goto gen_pool_create_fail;
191 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400192
193 if (phys) {
194 pool->phys = phys;
Arnd Bergmann84092992016-01-29 12:39:10 +0100195 pool->iomap = ioremap(phys, size); /* should be memremap? */
Sriram6a1fef62011-03-22 02:31:03 +0000196 pool->hw_addr = hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400197 } else {
Arnd Bergmann84092992016-01-29 12:39:10 +0100198 pool->cpumap = dma_alloc_coherent(dev, size, &pool->hw_addr,
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400199 GFP_KERNEL);
Arnd Bergmann84092992016-01-29 12:39:10 +0100200 pool->iomap = (void __iomem __force *)pool->cpumap;
201 pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400202 }
203
Grygorii Strashko742fb202016-06-27 12:05:11 +0300204 if (!pool->iomap)
205 goto gen_pool_create_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400206
Grygorii Strashko742fb202016-06-27 12:05:11 +0300207 ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
208 pool->phys, pool->mem_size, -1);
209 if (ret < 0) {
210 dev_err(dev, "pool add failed %d\n", ret);
211 goto gen_pool_add_virt_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400212 }
Grygorii Strashko742fb202016-06-27 12:05:11 +0300213
214 return pool;
215
216gen_pool_add_virt_fail:
217 cpdma_desc_pool_destroy(pool);
218gen_pool_create_fail:
219 return NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400220}
221
222static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
223 struct cpdma_desc __iomem *desc)
224{
225 if (!desc)
226 return 0;
Olof Johanssonc767db52013-12-11 15:51:20 -0800227 return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400228}
229
230static inline struct cpdma_desc __iomem *
231desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
232{
Sriram6a1fef62011-03-22 02:31:03 +0000233 return dma ? pool->iomap + dma - pool->hw_addr : NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400234}
235
236static struct cpdma_desc __iomem *
Grygorii Strashko742fb202016-06-27 12:05:11 +0300237cpdma_desc_alloc(struct cpdma_desc_pool *pool)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400238{
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300239 return (struct cpdma_desc __iomem *)
240 gen_pool_alloc(pool->gen_pool, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400241}
242
243static void cpdma_desc_free(struct cpdma_desc_pool *pool,
244 struct cpdma_desc __iomem *desc, int num_desc)
245{
Grygorii Strashko742fb202016-06-27 12:05:11 +0300246 gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400247}
248
249struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
250{
251 struct cpdma_ctlr *ctlr;
252
George Cheriane1943122014-05-12 10:21:21 +0530253 ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400254 if (!ctlr)
255 return NULL;
256
257 ctlr->state = CPDMA_STATE_IDLE;
258 ctlr->params = *params;
259 ctlr->dev = params->dev;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300260 ctlr->chan_num = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400261 spin_lock_init(&ctlr->lock);
262
263 ctlr->pool = cpdma_desc_pool_create(ctlr->dev,
264 ctlr->params.desc_mem_phys,
Sriram6a1fef62011-03-22 02:31:03 +0000265 ctlr->params.desc_hw_addr,
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400266 ctlr->params.desc_mem_size,
267 ctlr->params.desc_align);
Dan Carpenter2f872082014-06-11 11:16:51 +0300268 if (!ctlr->pool)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400269 return NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400270
271 if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
272 ctlr->num_chan = CPDMA_MAX_CHANNELS;
273 return ctlr;
274}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000275EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400276
277int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
278{
279 unsigned long flags;
280 int i;
281
282 spin_lock_irqsave(&ctlr->lock, flags);
283 if (ctlr->state != CPDMA_STATE_IDLE) {
284 spin_unlock_irqrestore(&ctlr->lock, flags);
285 return -EBUSY;
286 }
287
288 if (ctlr->params.has_soft_reset) {
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000289 unsigned timeout = 10 * 100;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400290
291 dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000292 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400293 if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
294 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000295 udelay(10);
296 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400297 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000298 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400299 }
300
301 for (i = 0; i < ctlr->num_chan; i++) {
302 __raw_writel(0, ctlr->params.txhdp + 4 * i);
303 __raw_writel(0, ctlr->params.rxhdp + 4 * i);
304 __raw_writel(0, ctlr->params.txcp + 4 * i);
305 __raw_writel(0, ctlr->params.rxcp + 4 * i);
306 }
307
308 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
309 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
310
311 dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
312 dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
313
314 ctlr->state = CPDMA_STATE_ACTIVE;
315
316 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
317 if (ctlr->channels[i])
318 cpdma_chan_start(ctlr->channels[i]);
319 }
320 spin_unlock_irqrestore(&ctlr->lock, flags);
321 return 0;
322}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000323EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400324
325int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
326{
327 unsigned long flags;
328 int i;
329
330 spin_lock_irqsave(&ctlr->lock, flags);
Christian Rieschcd11cf52014-03-24 13:46:27 +0100331 if (ctlr->state == CPDMA_STATE_TEARDOWN) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400332 spin_unlock_irqrestore(&ctlr->lock, flags);
333 return -EINVAL;
334 }
335
336 ctlr->state = CPDMA_STATE_TEARDOWN;
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300337 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400338
339 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
340 if (ctlr->channels[i])
341 cpdma_chan_stop(ctlr->channels[i]);
342 }
343
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300344 spin_lock_irqsave(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400345 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
346 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
347
348 dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
349 dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
350
351 ctlr->state = CPDMA_STATE_IDLE;
352
353 spin_unlock_irqrestore(&ctlr->lock, flags);
354 return 0;
355}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000356EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400357
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400358int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
359{
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400360 int ret = 0, i;
361
362 if (!ctlr)
363 return -EINVAL;
364
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400365 if (ctlr->state != CPDMA_STATE_IDLE)
366 cpdma_ctlr_stop(ctlr);
367
Cyril Roelandt79876e02013-02-12 12:52:30 +0000368 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
369 cpdma_chan_destroy(ctlr->channels[i]);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400370
371 cpdma_desc_pool_destroy(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400372 return ret;
373}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000374EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400375
376int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
377{
378 unsigned long flags;
379 int i, reg;
380
381 spin_lock_irqsave(&ctlr->lock, flags);
382 if (ctlr->state != CPDMA_STATE_ACTIVE) {
383 spin_unlock_irqrestore(&ctlr->lock, flags);
384 return -EINVAL;
385 }
386
387 reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR;
388 dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR);
389
390 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
391 if (ctlr->channels[i])
392 cpdma_chan_int_ctrl(ctlr->channels[i], enable);
393 }
394
395 spin_unlock_irqrestore(&ctlr->lock, flags);
396 return 0;
397}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100398EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400399
Mugunthan V N510a1e722013-02-17 22:19:20 +0000400void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400401{
Mugunthan V N510a1e722013-02-17 22:19:20 +0000402 dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400403}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100404EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400405
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300406/**
407 * cpdma_chan_split_pool - Splits ctrl pool between all channels.
408 * Has to be called under ctlr lock
409 */
410static void cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
411{
412 struct cpdma_desc_pool *pool = ctlr->pool;
413 struct cpdma_chan *chan;
414 int ch_desc_num;
415 int i;
416
417 if (!ctlr->chan_num)
418 return;
419
420 /* calculate average size of pool slice */
421 ch_desc_num = pool->num_desc / ctlr->chan_num;
422
423 /* split ctlr pool */
424 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
425 chan = ctlr->channels[i];
426 if (chan)
427 chan->desc_num = ch_desc_num;
428 }
429}
430
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400431struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
432 cpdma_handler_fn handler)
433{
434 struct cpdma_chan *chan;
George Cheriane1943122014-05-12 10:21:21 +0530435 int offset = (chan_num % CPDMA_MAX_CHANNELS) * 4;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400436 unsigned long flags;
437
438 if (__chan_linear(chan_num) >= ctlr->num_chan)
439 return NULL;
440
George Cheriane1943122014-05-12 10:21:21 +0530441 chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400442 if (!chan)
George Cheriane1943122014-05-12 10:21:21 +0530443 return ERR_PTR(-ENOMEM);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400444
445 spin_lock_irqsave(&ctlr->lock, flags);
George Cheriane1943122014-05-12 10:21:21 +0530446 if (ctlr->channels[chan_num]) {
447 spin_unlock_irqrestore(&ctlr->lock, flags);
448 devm_kfree(ctlr->dev, chan);
449 return ERR_PTR(-EBUSY);
450 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400451
452 chan->ctlr = ctlr;
453 chan->state = CPDMA_STATE_IDLE;
454 chan->chan_num = chan_num;
455 chan->handler = handler;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300456 chan->desc_num = ctlr->pool->num_desc / 2;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400457
458 if (is_rx_chan(chan)) {
459 chan->hdp = ctlr->params.rxhdp + offset;
460 chan->cp = ctlr->params.rxcp + offset;
461 chan->rxfree = ctlr->params.rxfree + offset;
462 chan->int_set = CPDMA_RXINTMASKSET;
463 chan->int_clear = CPDMA_RXINTMASKCLEAR;
464 chan->td = CPDMA_RXTEARDOWN;
465 chan->dir = DMA_FROM_DEVICE;
466 } else {
467 chan->hdp = ctlr->params.txhdp + offset;
468 chan->cp = ctlr->params.txcp + offset;
469 chan->int_set = CPDMA_TXINTMASKSET;
470 chan->int_clear = CPDMA_TXINTMASKCLEAR;
471 chan->td = CPDMA_TXTEARDOWN;
472 chan->dir = DMA_TO_DEVICE;
473 }
474 chan->mask = BIT(chan_linear(chan));
475
476 spin_lock_init(&chan->lock);
477
478 ctlr->channels[chan_num] = chan;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300479 ctlr->chan_num++;
480
481 cpdma_chan_split_pool(ctlr);
482
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400483 spin_unlock_irqrestore(&ctlr->lock, flags);
484 return chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400485}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000486EXPORT_SYMBOL_GPL(cpdma_chan_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400487
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300488int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300489{
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300490 unsigned long flags;
491 int desc_num;
492
493 spin_lock_irqsave(&chan->lock, flags);
494 desc_num = chan->desc_num;
495 spin_unlock_irqrestore(&chan->lock, flags);
496
497 return desc_num;
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300498}
499EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
500
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400501int cpdma_chan_destroy(struct cpdma_chan *chan)
502{
Julia Lawallf37c54b2012-08-14 05:49:47 +0000503 struct cpdma_ctlr *ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400504 unsigned long flags;
505
506 if (!chan)
507 return -EINVAL;
Julia Lawallf37c54b2012-08-14 05:49:47 +0000508 ctlr = chan->ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400509
510 spin_lock_irqsave(&ctlr->lock, flags);
511 if (chan->state != CPDMA_STATE_IDLE)
512 cpdma_chan_stop(chan);
513 ctlr->channels[chan->chan_num] = NULL;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300514 ctlr->chan_num--;
515
516 cpdma_chan_split_pool(ctlr);
517
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400518 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400519 return 0;
520}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000521EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400522
523int cpdma_chan_get_stats(struct cpdma_chan *chan,
524 struct cpdma_chan_stats *stats)
525{
526 unsigned long flags;
527 if (!chan)
528 return -EINVAL;
529 spin_lock_irqsave(&chan->lock, flags);
530 memcpy(stats, &chan->stats, sizeof(*stats));
531 spin_unlock_irqrestore(&chan->lock, flags);
532 return 0;
533}
Daniel Mack0ca04b62013-08-22 13:47:00 +0200534EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400535
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400536static void __cpdma_chan_submit(struct cpdma_chan *chan,
537 struct cpdma_desc __iomem *desc)
538{
539 struct cpdma_ctlr *ctlr = chan->ctlr;
540 struct cpdma_desc __iomem *prev = chan->tail;
541 struct cpdma_desc_pool *pool = ctlr->pool;
542 dma_addr_t desc_dma;
543 u32 mode;
544
545 desc_dma = desc_phys(pool, desc);
546
547 /* simple case - idle channel */
548 if (!chan->head) {
549 chan->stats.head_enqueue++;
550 chan->head = desc;
551 chan->tail = desc;
552 if (chan->state == CPDMA_STATE_ACTIVE)
553 chan_write(chan, hdp, desc_dma);
554 return;
555 }
556
557 /* first chain the descriptor at the tail of the list */
558 desc_write(prev, hw_next, desc_dma);
559 chan->tail = desc;
560 chan->stats.tail_enqueue++;
561
562 /* next check if EOQ has been triggered already */
563 mode = desc_read(prev, hw_mode);
564 if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
565 (chan->state == CPDMA_STATE_ACTIVE)) {
566 desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
567 chan_write(chan, hdp, desc_dma);
568 chan->stats.misqueued++;
569 }
570}
571
572int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
Sebastian Siewioraef614e2013-04-23 07:31:38 +0000573 int len, int directed)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400574{
575 struct cpdma_ctlr *ctlr = chan->ctlr;
576 struct cpdma_desc __iomem *desc;
577 dma_addr_t buffer;
578 unsigned long flags;
579 u32 mode;
580 int ret = 0;
581
582 spin_lock_irqsave(&chan->lock, flags);
583
584 if (chan->state == CPDMA_STATE_TEARDOWN) {
585 ret = -EINVAL;
586 goto unlock_ret;
587 }
588
Grygorii Strashko742fb202016-06-27 12:05:11 +0300589 if (chan->count >= chan->desc_num) {
590 chan->stats.desc_alloc_fail++;
591 ret = -ENOMEM;
592 goto unlock_ret;
593 }
594
595 desc = cpdma_desc_alloc(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400596 if (!desc) {
597 chan->stats.desc_alloc_fail++;
598 ret = -ENOMEM;
599 goto unlock_ret;
600 }
601
602 if (len < ctlr->params.min_packet_size) {
603 len = ctlr->params.min_packet_size;
604 chan->stats.runt_transmit_buff++;
605 }
606
607 buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
Sebastian Siewior14bd0762013-06-20 16:58:45 +0200608 ret = dma_mapping_error(ctlr->dev, buffer);
609 if (ret) {
610 cpdma_desc_free(ctlr->pool, desc, 1);
611 ret = -EINVAL;
612 goto unlock_ret;
613 }
614
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400615 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000616 cpdma_desc_to_port(chan, mode, directed);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400617
618 desc_write(desc, hw_next, 0);
619 desc_write(desc, hw_buffer, buffer);
620 desc_write(desc, hw_len, len);
621 desc_write(desc, hw_mode, mode | len);
622 desc_write(desc, sw_token, token);
623 desc_write(desc, sw_buffer, buffer);
624 desc_write(desc, sw_len, len);
625
626 __cpdma_chan_submit(chan, desc);
627
628 if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
629 chan_write(chan, rxfree, 1);
630
631 chan->count++;
632
633unlock_ret:
634 spin_unlock_irqrestore(&chan->lock, flags);
635 return ret;
636}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000637EXPORT_SYMBOL_GPL(cpdma_chan_submit);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400638
Mugunthan V Nfae50822013-01-17 06:31:34 +0000639bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
640{
Mugunthan V Nfae50822013-01-17 06:31:34 +0000641 struct cpdma_ctlr *ctlr = chan->ctlr;
642 struct cpdma_desc_pool *pool = ctlr->pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300643 bool free_tx_desc;
644 unsigned long flags;
Mugunthan V Nfae50822013-01-17 06:31:34 +0000645
Grygorii Strashko742fb202016-06-27 12:05:11 +0300646 spin_lock_irqsave(&chan->lock, flags);
647 free_tx_desc = (chan->count < chan->desc_num) &&
648 gen_pool_avail(pool->gen_pool);
649 spin_unlock_irqrestore(&chan->lock, flags);
650 return free_tx_desc;
Mugunthan V Nfae50822013-01-17 06:31:34 +0000651}
652EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
653
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400654static void __cpdma_chan_free(struct cpdma_chan *chan,
655 struct cpdma_desc __iomem *desc,
656 int outlen, int status)
657{
658 struct cpdma_ctlr *ctlr = chan->ctlr;
659 struct cpdma_desc_pool *pool = ctlr->pool;
660 dma_addr_t buff_dma;
661 int origlen;
662 void *token;
663
664 token = (void *)desc_read(desc, sw_token);
665 buff_dma = desc_read(desc, sw_buffer);
666 origlen = desc_read(desc, sw_len);
667
668 dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
669 cpdma_desc_free(pool, desc, 1);
670 (*chan->handler)(token, outlen, status);
671}
672
673static int __cpdma_chan_process(struct cpdma_chan *chan)
674{
675 struct cpdma_ctlr *ctlr = chan->ctlr;
676 struct cpdma_desc __iomem *desc;
677 int status, outlen;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000678 int cb_status = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400679 struct cpdma_desc_pool *pool = ctlr->pool;
680 dma_addr_t desc_dma;
681 unsigned long flags;
682
683 spin_lock_irqsave(&chan->lock, flags);
684
685 desc = chan->head;
686 if (!desc) {
687 chan->stats.empty_dequeue++;
688 status = -ENOENT;
689 goto unlock_ret;
690 }
691 desc_dma = desc_phys(pool, desc);
692
693 status = __raw_readl(&desc->hw_mode);
694 outlen = status & 0x7ff;
695 if (status & CPDMA_DESC_OWNER) {
696 chan->stats.busy_dequeue++;
697 status = -EBUSY;
698 goto unlock_ret;
699 }
Mugunthan V N28a19fe2013-05-29 20:22:01 +0000700
701 if (status & CPDMA_DESC_PASS_CRC)
702 outlen -= CPDMA_DESC_CRC_LEN;
703
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000704 status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
705 CPDMA_DESC_PORT_MASK);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400706
707 chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
708 chan_write(chan, cp, desc_dma);
709 chan->count--;
710 chan->stats.good_dequeue++;
711
712 if (status & CPDMA_DESC_EOQ) {
713 chan->stats.requeue++;
714 chan_write(chan, hdp, desc_phys(pool, chan->head));
715 }
716
717 spin_unlock_irqrestore(&chan->lock, flags);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000718 if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
719 cb_status = -ENOSYS;
720 else
721 cb_status = status;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400722
Sebastian Siewiorb4727e62013-04-23 07:31:39 +0000723 __cpdma_chan_free(chan, desc, outlen, cb_status);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400724 return status;
725
726unlock_ret:
727 spin_unlock_irqrestore(&chan->lock, flags);
728 return status;
729}
730
731int cpdma_chan_process(struct cpdma_chan *chan, int quota)
732{
733 int used = 0, ret = 0;
734
735 if (chan->state != CPDMA_STATE_ACTIVE)
736 return -EINVAL;
737
738 while (used < quota) {
739 ret = __cpdma_chan_process(chan);
740 if (ret < 0)
741 break;
742 used++;
743 }
744 return used;
745}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000746EXPORT_SYMBOL_GPL(cpdma_chan_process);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400747
748int cpdma_chan_start(struct cpdma_chan *chan)
749{
750 struct cpdma_ctlr *ctlr = chan->ctlr;
751 struct cpdma_desc_pool *pool = ctlr->pool;
752 unsigned long flags;
753
754 spin_lock_irqsave(&chan->lock, flags);
755 if (chan->state != CPDMA_STATE_IDLE) {
756 spin_unlock_irqrestore(&chan->lock, flags);
757 return -EBUSY;
758 }
759 if (ctlr->state != CPDMA_STATE_ACTIVE) {
760 spin_unlock_irqrestore(&chan->lock, flags);
761 return -EINVAL;
762 }
763 dma_reg_write(ctlr, chan->int_set, chan->mask);
764 chan->state = CPDMA_STATE_ACTIVE;
765 if (chan->head) {
766 chan_write(chan, hdp, desc_phys(pool, chan->head));
767 if (chan->rxfree)
768 chan_write(chan, rxfree, chan->count);
769 }
770
771 spin_unlock_irqrestore(&chan->lock, flags);
772 return 0;
773}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000774EXPORT_SYMBOL_GPL(cpdma_chan_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400775
776int cpdma_chan_stop(struct cpdma_chan *chan)
777{
778 struct cpdma_ctlr *ctlr = chan->ctlr;
779 struct cpdma_desc_pool *pool = ctlr->pool;
780 unsigned long flags;
781 int ret;
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000782 unsigned timeout;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400783
784 spin_lock_irqsave(&chan->lock, flags);
Christian Rieschcd11cf52014-03-24 13:46:27 +0100785 if (chan->state == CPDMA_STATE_TEARDOWN) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400786 spin_unlock_irqrestore(&chan->lock, flags);
787 return -EINVAL;
788 }
789
790 chan->state = CPDMA_STATE_TEARDOWN;
791 dma_reg_write(ctlr, chan->int_clear, chan->mask);
792
793 /* trigger teardown */
Christian Rieschb4ad0422012-02-22 21:58:00 +0000794 dma_reg_write(ctlr, chan->td, chan_linear(chan));
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400795
796 /* wait for teardown complete */
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000797 timeout = 100 * 100; /* 100 ms */
798 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400799 u32 cp = chan_read(chan, cp);
800 if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
801 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000802 udelay(10);
803 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400804 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000805 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400806 chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
807
808 /* handle completed packets */
Ilya Yanok7746ab02011-12-18 10:02:04 +0000809 spin_unlock_irqrestore(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400810 do {
811 ret = __cpdma_chan_process(chan);
812 if (ret < 0)
813 break;
814 } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
Ilya Yanok7746ab02011-12-18 10:02:04 +0000815 spin_lock_irqsave(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400816
817 /* remaining packets haven't been tx/rx'ed, clean them up */
818 while (chan->head) {
819 struct cpdma_desc __iomem *desc = chan->head;
820 dma_addr_t next_dma;
821
822 next_dma = desc_read(desc, hw_next);
823 chan->head = desc_from_phys(pool, next_dma);
htbeginffb5ba92012-10-01 16:42:43 +0000824 chan->count--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400825 chan->stats.teardown_dequeue++;
826
827 /* issue callback without locks held */
828 spin_unlock_irqrestore(&chan->lock, flags);
829 __cpdma_chan_free(chan, desc, 0, -ENOSYS);
830 spin_lock_irqsave(&chan->lock, flags);
831 }
832
833 chan->state = CPDMA_STATE_IDLE;
834 spin_unlock_irqrestore(&chan->lock, flags);
835 return 0;
836}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000837EXPORT_SYMBOL_GPL(cpdma_chan_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400838
839int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
840{
841 unsigned long flags;
842
843 spin_lock_irqsave(&chan->lock, flags);
844 if (chan->state != CPDMA_STATE_ACTIVE) {
845 spin_unlock_irqrestore(&chan->lock, flags);
846 return -EINVAL;
847 }
848
849 dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
850 chan->mask);
851 spin_unlock_irqrestore(&chan->lock, flags);
852
853 return 0;
854}
855
856struct cpdma_control_info {
857 u32 reg;
858 u32 shift, mask;
859 int access;
860#define ACCESS_RO BIT(0)
861#define ACCESS_WO BIT(1)
862#define ACCESS_RW (ACCESS_RO | ACCESS_WO)
863};
864
Olof Johanssondf784162013-12-11 15:51:21 -0800865static struct cpdma_control_info controls[] = {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400866 [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO},
867 [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW},
868 [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW},
869 [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW},
870 [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW},
871 [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO},
872 [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW},
873 [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW},
874 [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW},
875 [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW},
876 [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW},
877};
878
879int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
880{
881 unsigned long flags;
882 struct cpdma_control_info *info = &controls[control];
883 int ret;
884
885 spin_lock_irqsave(&ctlr->lock, flags);
886
887 ret = -ENOTSUPP;
888 if (!ctlr->params.has_ext_regs)
889 goto unlock_ret;
890
891 ret = -EINVAL;
892 if (ctlr->state != CPDMA_STATE_ACTIVE)
893 goto unlock_ret;
894
895 ret = -ENOENT;
896 if (control < 0 || control >= ARRAY_SIZE(controls))
897 goto unlock_ret;
898
899 ret = -EPERM;
900 if ((info->access & ACCESS_RO) != ACCESS_RO)
901 goto unlock_ret;
902
903 ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
904
905unlock_ret:
906 spin_unlock_irqrestore(&ctlr->lock, flags);
907 return ret;
908}
909
910int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
911{
912 unsigned long flags;
913 struct cpdma_control_info *info = &controls[control];
914 int ret;
915 u32 val;
916
917 spin_lock_irqsave(&ctlr->lock, flags);
918
919 ret = -ENOTSUPP;
920 if (!ctlr->params.has_ext_regs)
921 goto unlock_ret;
922
923 ret = -EINVAL;
924 if (ctlr->state != CPDMA_STATE_ACTIVE)
925 goto unlock_ret;
926
927 ret = -ENOENT;
928 if (control < 0 || control >= ARRAY_SIZE(controls))
929 goto unlock_ret;
930
931 ret = -EPERM;
932 if ((info->access & ACCESS_WO) != ACCESS_WO)
933 goto unlock_ret;
934
935 val = dma_reg_read(ctlr, info->reg);
936 val &= ~(info->mask << info->shift);
937 val |= (value & info->mask) << info->shift;
938 dma_reg_write(ctlr, info->reg, val);
939 ret = 0;
940
941unlock_ret:
942 spin_unlock_irqrestore(&ctlr->lock, flags);
943 return ret;
944}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100945EXPORT_SYMBOL_GPL(cpdma_control_set);
Sebastian Siewior4bc21d42013-04-24 08:48:22 +0000946
947MODULE_LICENSE("GPL");