blob: a53b38455a2f49763b758174f07602bdc8fb2426 [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
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +020035#define CPDMA_TX_PRI0_RATE 0x30
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040036#define CPDMA_TXINTSTATRAW 0x80
37#define CPDMA_TXINTSTATMASKED 0x84
38#define CPDMA_TXINTMASKSET 0x88
39#define CPDMA_TXINTMASKCLEAR 0x8c
40#define CPDMA_MACINVECTOR 0x90
41#define CPDMA_MACEOIVECTOR 0x94
42#define CPDMA_RXINTSTATRAW 0xa0
43#define CPDMA_RXINTSTATMASKED 0xa4
44#define CPDMA_RXINTMASKSET 0xa8
45#define CPDMA_RXINTMASKCLEAR 0xac
46#define CPDMA_DMAINTSTATRAW 0xb0
47#define CPDMA_DMAINTSTATMASKED 0xb4
48#define CPDMA_DMAINTMASKSET 0xb8
49#define CPDMA_DMAINTMASKCLEAR 0xbc
50#define CPDMA_DMAINT_HOSTERR BIT(1)
51
52/* the following exist only if has_ext_regs is set */
53#define CPDMA_DMACONTROL 0x20
54#define CPDMA_DMASTATUS 0x24
55#define CPDMA_RXBUFFOFS 0x28
56#define CPDMA_EM_CONTROL 0x2c
57
58/* Descriptor mode bits */
59#define CPDMA_DESC_SOP BIT(31)
60#define CPDMA_DESC_EOP BIT(30)
61#define CPDMA_DESC_OWNER BIT(29)
62#define CPDMA_DESC_EOQ BIT(28)
63#define CPDMA_DESC_TD_COMPLETE BIT(27)
64#define CPDMA_DESC_PASS_CRC BIT(26)
Mugunthan V Nf6e135c2013-02-11 09:52:18 +000065#define CPDMA_DESC_TO_PORT_EN BIT(20)
66#define CPDMA_TO_PORT_SHIFT 16
67#define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16))
Mugunthan V N28a19fe2013-05-29 20:22:01 +000068#define CPDMA_DESC_CRC_LEN 4
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040069
70#define CPDMA_TEARDOWN_VALUE 0xfffffffc
71
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +020072#define CPDMA_MAX_RLIM_CNT 16384
73
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040074struct cpdma_desc {
75 /* hardware fields */
76 u32 hw_next;
77 u32 hw_buffer;
78 u32 hw_len;
79 u32 hw_mode;
80 /* software fields */
81 void *sw_token;
82 u32 sw_buffer;
83 u32 sw_len;
84};
85
86struct cpdma_desc_pool {
Olof Johanssonc767db52013-12-11 15:51:20 -080087 phys_addr_t phys;
Arnd Bergmann84092992016-01-29 12:39:10 +010088 dma_addr_t hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040089 void __iomem *iomap; /* ioremap map */
90 void *cpumap; /* dma_alloc map */
91 int desc_size, mem_size;
Grygorii Strashkoaeec3022016-08-04 18:20:51 +030092 int num_desc;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040093 struct device *dev;
Grygorii Strashko742fb202016-06-27 12:05:11 +030094 struct gen_pool *gen_pool;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040095};
96
97enum cpdma_state {
98 CPDMA_STATE_IDLE,
99 CPDMA_STATE_ACTIVE,
100 CPDMA_STATE_TEARDOWN,
101};
102
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400103struct cpdma_ctlr {
104 enum cpdma_state state;
105 struct cpdma_params params;
106 struct device *dev;
107 struct cpdma_desc_pool *pool;
108 spinlock_t lock;
109 struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS];
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300110 int chan_num;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400111};
112
113struct cpdma_chan {
Mugunthan V Nfae50822013-01-17 06:31:34 +0000114 struct cpdma_desc __iomem *head, *tail;
115 void __iomem *hdp, *cp, *rxfree;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400116 enum cpdma_state state;
117 struct cpdma_ctlr *ctlr;
118 int chan_num;
119 spinlock_t lock;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400120 int count;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300121 u32 desc_num;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400122 u32 mask;
123 cpdma_handler_fn handler;
124 enum dma_data_direction dir;
125 struct cpdma_chan_stats stats;
126 /* offsets into dmaregs */
127 int int_set, int_clear, td;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200128 int weight;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200129 u32 rate_factor;
130 u32 rate;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400131};
132
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200133struct cpdma_control_info {
134 u32 reg;
135 u32 shift, mask;
136 int access;
137#define ACCESS_RO BIT(0)
138#define ACCESS_WO BIT(1)
139#define ACCESS_RW (ACCESS_RO | ACCESS_WO)
140};
141
142static struct cpdma_control_info controls[] = {
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200143 [CPDMA_TX_RLIM] = {CPDMA_DMACONTROL, 8, 0xffff, ACCESS_RW},
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200144 [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO},
145 [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW},
146 [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW},
147 [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW},
148 [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW},
149 [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO},
150 [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW},
151 [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW},
152 [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW},
153 [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW},
154 [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW},
155};
156
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300157#define tx_chan_num(chan) (chan)
158#define rx_chan_num(chan) ((chan) + CPDMA_MAX_CHANNELS)
159#define is_rx_chan(chan) ((chan)->chan_num >= CPDMA_MAX_CHANNELS)
160#define is_tx_chan(chan) (!is_rx_chan(chan))
161#define __chan_linear(chan_num) ((chan_num) & (CPDMA_MAX_CHANNELS - 1))
162#define chan_linear(chan) __chan_linear((chan)->chan_num)
163
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400164/* The following make access to common cpdma_ctlr params more readable */
165#define dmaregs params.dmaregs
166#define num_chan params.num_chan
167
168/* various accessors */
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -0600169#define dma_reg_read(ctlr, ofs) readl((ctlr)->dmaregs + (ofs))
170#define chan_read(chan, fld) readl((chan)->fld)
171#define desc_read(desc, fld) readl(&(desc)->fld)
172#define dma_reg_write(ctlr, ofs, v) writel(v, (ctlr)->dmaregs + (ofs))
173#define chan_write(chan, fld, v) writel(v, (chan)->fld)
174#define desc_write(desc, fld, v) writel((u32)(v), &(desc)->fld)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400175
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000176#define cpdma_desc_to_port(chan, mode, directed) \
177 do { \
178 if (!is_rx_chan(chan) && ((directed == 1) || \
179 (directed == 2))) \
180 mode |= (CPDMA_DESC_TO_PORT_EN | \
181 (directed << CPDMA_TO_PORT_SHIFT)); \
182 } while (0)
183
Grygorii Strashko742fb202016-06-27 12:05:11 +0300184static void cpdma_desc_pool_destroy(struct cpdma_desc_pool *pool)
185{
186 if (!pool)
187 return;
188
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300189 WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool),
190 "cpdma_desc_pool size %d != avail %d",
191 gen_pool_size(pool->gen_pool),
192 gen_pool_avail(pool->gen_pool));
Grygorii Strashko742fb202016-06-27 12:05:11 +0300193 if (pool->cpumap)
194 dma_free_coherent(pool->dev, pool->mem_size, pool->cpumap,
195 pool->phys);
196 else
197 iounmap(pool->iomap);
198}
199
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400200/*
201 * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
202 * emac) have dedicated on-chip memory for these descriptors. Some other
203 * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
204 * abstract out these details
205 */
206static struct cpdma_desc_pool *
Arnd Bergmann84092992016-01-29 12:39:10 +0100207cpdma_desc_pool_create(struct device *dev, u32 phys, dma_addr_t hw_addr,
Sriram6a1fef62011-03-22 02:31:03 +0000208 int size, int align)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400209{
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400210 struct cpdma_desc_pool *pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300211 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400212
George Cheriane1943122014-05-12 10:21:21 +0530213 pool = devm_kzalloc(dev, sizeof(*pool), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400214 if (!pool)
Grygorii Strashko742fb202016-06-27 12:05:11 +0300215 goto gen_pool_create_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400216
217 pool->dev = dev;
218 pool->mem_size = size;
219 pool->desc_size = ALIGN(sizeof(struct cpdma_desc), align);
220 pool->num_desc = size / pool->desc_size;
221
Grygorii Strashko742fb202016-06-27 12:05:11 +0300222 pool->gen_pool = devm_gen_pool_create(dev, ilog2(pool->desc_size), -1,
223 "cpdma");
224 if (IS_ERR(pool->gen_pool)) {
225 dev_err(dev, "pool create failed %ld\n",
226 PTR_ERR(pool->gen_pool));
227 goto gen_pool_create_fail;
228 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400229
230 if (phys) {
231 pool->phys = phys;
Arnd Bergmann84092992016-01-29 12:39:10 +0100232 pool->iomap = ioremap(phys, size); /* should be memremap? */
Sriram6a1fef62011-03-22 02:31:03 +0000233 pool->hw_addr = hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400234 } else {
Arnd Bergmann84092992016-01-29 12:39:10 +0100235 pool->cpumap = dma_alloc_coherent(dev, size, &pool->hw_addr,
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400236 GFP_KERNEL);
Arnd Bergmann84092992016-01-29 12:39:10 +0100237 pool->iomap = (void __iomem __force *)pool->cpumap;
238 pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400239 }
240
Grygorii Strashko742fb202016-06-27 12:05:11 +0300241 if (!pool->iomap)
242 goto gen_pool_create_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400243
Grygorii Strashko742fb202016-06-27 12:05:11 +0300244 ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
245 pool->phys, pool->mem_size, -1);
246 if (ret < 0) {
247 dev_err(dev, "pool add failed %d\n", ret);
248 goto gen_pool_add_virt_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400249 }
Grygorii Strashko742fb202016-06-27 12:05:11 +0300250
251 return pool;
252
253gen_pool_add_virt_fail:
254 cpdma_desc_pool_destroy(pool);
255gen_pool_create_fail:
256 return NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400257}
258
259static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
260 struct cpdma_desc __iomem *desc)
261{
262 if (!desc)
263 return 0;
Olof Johanssonc767db52013-12-11 15:51:20 -0800264 return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400265}
266
267static inline struct cpdma_desc __iomem *
268desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
269{
Sriram6a1fef62011-03-22 02:31:03 +0000270 return dma ? pool->iomap + dma - pool->hw_addr : NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400271}
272
273static struct cpdma_desc __iomem *
Grygorii Strashko742fb202016-06-27 12:05:11 +0300274cpdma_desc_alloc(struct cpdma_desc_pool *pool)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400275{
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300276 return (struct cpdma_desc __iomem *)
277 gen_pool_alloc(pool->gen_pool, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400278}
279
280static void cpdma_desc_free(struct cpdma_desc_pool *pool,
281 struct cpdma_desc __iomem *desc, int num_desc)
282{
Grygorii Strashko742fb202016-06-27 12:05:11 +0300283 gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400284}
285
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200286static int _cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
287{
288 struct cpdma_control_info *info = &controls[control];
289 u32 val;
290
291 if (!ctlr->params.has_ext_regs)
292 return -ENOTSUPP;
293
294 if (ctlr->state != CPDMA_STATE_ACTIVE)
295 return -EINVAL;
296
297 if (control < 0 || control >= ARRAY_SIZE(controls))
298 return -ENOENT;
299
300 if ((info->access & ACCESS_WO) != ACCESS_WO)
301 return -EPERM;
302
303 val = dma_reg_read(ctlr, info->reg);
304 val &= ~(info->mask << info->shift);
305 val |= (value & info->mask) << info->shift;
306 dma_reg_write(ctlr, info->reg, val);
307
308 return 0;
309}
310
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200311static int _cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
312{
313 struct cpdma_control_info *info = &controls[control];
314 int ret;
315
316 if (!ctlr->params.has_ext_regs)
317 return -ENOTSUPP;
318
319 if (ctlr->state != CPDMA_STATE_ACTIVE)
320 return -EINVAL;
321
322 if (control < 0 || control >= ARRAY_SIZE(controls))
323 return -ENOENT;
324
325 if ((info->access & ACCESS_RO) != ACCESS_RO)
326 return -EPERM;
327
328 ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
329 return ret;
330}
331
332/* cpdma_chan_set_chan_shaper - set shaper for a channel
333 * Has to be called under ctlr lock
334 */
335static int cpdma_chan_set_chan_shaper(struct cpdma_chan *chan)
336{
337 struct cpdma_ctlr *ctlr = chan->ctlr;
338 u32 rate_reg;
339 u32 rmask;
340 int ret;
341
342 if (!chan->rate)
343 return 0;
344
345 rate_reg = CPDMA_TX_PRI0_RATE + 4 * chan->chan_num;
346 dma_reg_write(ctlr, rate_reg, chan->rate_factor);
347
348 rmask = _cpdma_control_get(ctlr, CPDMA_TX_RLIM);
349 rmask |= chan->mask;
350
351 ret = _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
352 return ret;
353}
354
355static int cpdma_chan_on(struct cpdma_chan *chan)
356{
357 struct cpdma_ctlr *ctlr = chan->ctlr;
358 struct cpdma_desc_pool *pool = ctlr->pool;
359 unsigned long flags;
360
361 spin_lock_irqsave(&chan->lock, flags);
362 if (chan->state != CPDMA_STATE_IDLE) {
363 spin_unlock_irqrestore(&chan->lock, flags);
364 return -EBUSY;
365 }
366 if (ctlr->state != CPDMA_STATE_ACTIVE) {
367 spin_unlock_irqrestore(&chan->lock, flags);
368 return -EINVAL;
369 }
370 dma_reg_write(ctlr, chan->int_set, chan->mask);
371 chan->state = CPDMA_STATE_ACTIVE;
372 if (chan->head) {
373 chan_write(chan, hdp, desc_phys(pool, chan->head));
374 if (chan->rxfree)
375 chan_write(chan, rxfree, chan->count);
376 }
377
378 spin_unlock_irqrestore(&chan->lock, flags);
379 return 0;
380}
381
382/* cpdma_chan_fit_rate - set rate for a channel and check if it's possible.
383 * rmask - mask of rate limited channels
384 * Returns min rate in Kb/s
385 */
386static int cpdma_chan_fit_rate(struct cpdma_chan *ch, u32 rate,
387 u32 *rmask, int *prio_mode)
388{
389 struct cpdma_ctlr *ctlr = ch->ctlr;
390 struct cpdma_chan *chan;
391 u32 old_rate = ch->rate;
392 u32 new_rmask = 0;
393 int rlim = 1;
394 int i;
395
396 *prio_mode = 0;
397 for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) {
398 chan = ctlr->channels[i];
399 if (!chan) {
400 rlim = 0;
401 continue;
402 }
403
404 if (chan == ch)
405 chan->rate = rate;
406
407 if (chan->rate) {
408 if (rlim) {
409 new_rmask |= chan->mask;
410 } else {
411 ch->rate = old_rate;
412 dev_err(ctlr->dev, "Prev channel of %dch is not rate limited\n",
413 chan->chan_num);
414 return -EINVAL;
415 }
416 } else {
417 *prio_mode = 1;
418 rlim = 0;
419 }
420 }
421
422 *rmask = new_rmask;
423 return 0;
424}
425
426static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr,
427 struct cpdma_chan *ch)
428{
429 u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX;
430 u32 best_send_cnt = 0, best_idle_cnt = 0;
431 u32 new_rate, best_rate = 0, rate_reg;
432 u64 send_cnt, idle_cnt;
433 u32 min_send_cnt, freq;
434 u64 divident, divisor;
435
436 if (!ch->rate) {
437 ch->rate_factor = 0;
438 goto set_factor;
439 }
440
441 freq = ctlr->params.bus_freq_mhz * 1000 * 32;
442 if (!freq) {
443 dev_err(ctlr->dev, "The bus frequency is not set\n");
444 return -EINVAL;
445 }
446
447 min_send_cnt = freq - ch->rate;
448 send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate);
449 while (send_cnt <= CPDMA_MAX_RLIM_CNT) {
450 divident = ch->rate * send_cnt;
451 divisor = min_send_cnt;
452 idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor);
453
454 divident = freq * idle_cnt;
455 divisor = idle_cnt + send_cnt;
456 new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor);
457
458 delta = new_rate >= ch->rate ? new_rate - ch->rate : delta;
459 if (delta < best_delta) {
460 best_delta = delta;
461 best_send_cnt = send_cnt;
462 best_idle_cnt = idle_cnt;
463 best_rate = new_rate;
464
465 if (!delta)
466 break;
467 }
468
469 if (prev_delta >= delta) {
470 prev_delta = delta;
471 send_cnt++;
472 continue;
473 }
474
475 idle_cnt++;
476 divident = freq * idle_cnt;
477 send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate);
478 send_cnt -= idle_cnt;
479 prev_delta = UINT_MAX;
480 }
481
482 ch->rate = best_rate;
483 ch->rate_factor = best_send_cnt | (best_idle_cnt << 16);
484
485set_factor:
486 rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num;
487 dma_reg_write(ctlr, rate_reg, ch->rate_factor);
488 return 0;
489}
490
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400491struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
492{
493 struct cpdma_ctlr *ctlr;
494
George Cheriane1943122014-05-12 10:21:21 +0530495 ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400496 if (!ctlr)
497 return NULL;
498
499 ctlr->state = CPDMA_STATE_IDLE;
500 ctlr->params = *params;
501 ctlr->dev = params->dev;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300502 ctlr->chan_num = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400503 spin_lock_init(&ctlr->lock);
504
505 ctlr->pool = cpdma_desc_pool_create(ctlr->dev,
506 ctlr->params.desc_mem_phys,
Sriram6a1fef62011-03-22 02:31:03 +0000507 ctlr->params.desc_hw_addr,
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400508 ctlr->params.desc_mem_size,
509 ctlr->params.desc_align);
Dan Carpenter2f872082014-06-11 11:16:51 +0300510 if (!ctlr->pool)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400511 return NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400512
513 if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
514 ctlr->num_chan = CPDMA_MAX_CHANNELS;
515 return ctlr;
516}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000517EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400518
519int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
520{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200521 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400522 unsigned long flags;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200523 int i, prio_mode;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400524
525 spin_lock_irqsave(&ctlr->lock, flags);
526 if (ctlr->state != CPDMA_STATE_IDLE) {
527 spin_unlock_irqrestore(&ctlr->lock, flags);
528 return -EBUSY;
529 }
530
531 if (ctlr->params.has_soft_reset) {
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000532 unsigned timeout = 10 * 100;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400533
534 dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000535 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400536 if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
537 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000538 udelay(10);
539 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400540 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000541 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400542 }
543
544 for (i = 0; i < ctlr->num_chan; i++) {
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -0600545 writel(0, ctlr->params.txhdp + 4 * i);
546 writel(0, ctlr->params.rxhdp + 4 * i);
547 writel(0, ctlr->params.txcp + 4 * i);
548 writel(0, ctlr->params.rxcp + 4 * i);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400549 }
550
551 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
552 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
553
554 dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
555 dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
556
557 ctlr->state = CPDMA_STATE_ACTIVE;
558
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200559 prio_mode = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400560 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200561 chan = ctlr->channels[i];
562 if (chan) {
563 cpdma_chan_set_chan_shaper(chan);
564 cpdma_chan_on(chan);
565
566 /* off prio mode if all tx channels are rate limited */
567 if (is_tx_chan(chan) && !chan->rate)
568 prio_mode = 1;
569 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400570 }
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200571
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200572 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200573 _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0);
574
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400575 spin_unlock_irqrestore(&ctlr->lock, flags);
576 return 0;
577}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000578EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400579
580int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
581{
582 unsigned long flags;
583 int i;
584
585 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhukb993eec2016-11-11 16:10:47 +0200586 if (ctlr->state != CPDMA_STATE_ACTIVE) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400587 spin_unlock_irqrestore(&ctlr->lock, flags);
588 return -EINVAL;
589 }
590
591 ctlr->state = CPDMA_STATE_TEARDOWN;
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300592 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400593
594 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
595 if (ctlr->channels[i])
596 cpdma_chan_stop(ctlr->channels[i]);
597 }
598
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300599 spin_lock_irqsave(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400600 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
601 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
602
603 dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
604 dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
605
606 ctlr->state = CPDMA_STATE_IDLE;
607
608 spin_unlock_irqrestore(&ctlr->lock, flags);
609 return 0;
610}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000611EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400612
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400613int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
614{
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400615 int ret = 0, i;
616
617 if (!ctlr)
618 return -EINVAL;
619
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400620 if (ctlr->state != CPDMA_STATE_IDLE)
621 cpdma_ctlr_stop(ctlr);
622
Cyril Roelandt79876e02013-02-12 12:52:30 +0000623 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
624 cpdma_chan_destroy(ctlr->channels[i]);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400625
626 cpdma_desc_pool_destroy(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400627 return ret;
628}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000629EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400630
631int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
632{
633 unsigned long flags;
634 int i, reg;
635
636 spin_lock_irqsave(&ctlr->lock, flags);
637 if (ctlr->state != CPDMA_STATE_ACTIVE) {
638 spin_unlock_irqrestore(&ctlr->lock, flags);
639 return -EINVAL;
640 }
641
642 reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR;
643 dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR);
644
645 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
646 if (ctlr->channels[i])
647 cpdma_chan_int_ctrl(ctlr->channels[i], enable);
648 }
649
650 spin_unlock_irqrestore(&ctlr->lock, flags);
651 return 0;
652}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100653EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400654
Mugunthan V N510a1e722013-02-17 22:19:20 +0000655void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400656{
Mugunthan V N510a1e722013-02-17 22:19:20 +0000657 dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400658}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100659EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400660
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300661u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
662{
663 return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
664}
665EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state);
666
667u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
668{
669 return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
670}
671EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state);
672
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200673static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr,
674 int rx, int desc_num,
675 int per_ch_desc)
676{
677 struct cpdma_chan *chan, *most_chan = NULL;
678 int desc_cnt = desc_num;
679 int most_dnum = 0;
680 int min, max, i;
681
682 if (!desc_num)
683 return;
684
685 if (rx) {
686 min = rx_chan_num(0);
687 max = rx_chan_num(CPDMA_MAX_CHANNELS);
688 } else {
689 min = tx_chan_num(0);
690 max = tx_chan_num(CPDMA_MAX_CHANNELS);
691 }
692
693 for (i = min; i < max; i++) {
694 chan = ctlr->channels[i];
695 if (!chan)
696 continue;
697
698 if (chan->weight)
699 chan->desc_num = (chan->weight * desc_num) / 100;
700 else
701 chan->desc_num = per_ch_desc;
702
703 desc_cnt -= chan->desc_num;
704
705 if (most_dnum < chan->desc_num) {
706 most_dnum = chan->desc_num;
707 most_chan = chan;
708 }
709 }
710 /* use remains */
711 most_chan->desc_num += desc_cnt;
712}
713
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300714/**
715 * cpdma_chan_split_pool - Splits ctrl pool between all channels.
716 * Has to be called under ctlr lock
717 */
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200718static int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300719{
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200720 int tx_per_ch_desc = 0, rx_per_ch_desc = 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300721 struct cpdma_desc_pool *pool = ctlr->pool;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200722 int free_rx_num = 0, free_tx_num = 0;
723 int rx_weight = 0, tx_weight = 0;
724 int tx_desc_num, rx_desc_num;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300725 struct cpdma_chan *chan;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200726 int i, tx_num = 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300727
728 if (!ctlr->chan_num)
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200729 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300730
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300731 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
732 chan = ctlr->channels[i];
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200733 if (!chan)
734 continue;
735
736 if (is_rx_chan(chan)) {
737 if (!chan->weight)
738 free_rx_num++;
739 rx_weight += chan->weight;
740 } else {
741 if (!chan->weight)
742 free_tx_num++;
743 tx_weight += chan->weight;
744 tx_num++;
745 }
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300746 }
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200747
748 if (rx_weight > 100 || tx_weight > 100)
749 return -EINVAL;
750
751 tx_desc_num = (tx_num * pool->num_desc) / ctlr->chan_num;
752 rx_desc_num = pool->num_desc - tx_desc_num;
753
754 if (free_tx_num) {
755 tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100;
756 tx_per_ch_desc /= free_tx_num;
757 }
758 if (free_rx_num) {
759 rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100;
760 rx_per_ch_desc /= free_rx_num;
761 }
762
763 cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc);
764 cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc);
765
766 return 0;
767}
768
769/* cpdma_chan_set_weight - set weight of a channel in percentage.
770 * Tx and Rx channels have separate weights. That is 100% for RX
771 * and 100% for Tx. The weight is used to split cpdma resources
772 * in correct proportion required by the channels, including number
773 * of descriptors. The channel rate is not enough to know the
774 * weight of a channel as the maximum rate of an interface is needed.
775 * If weight = 0, then channel uses rest of descriptors leaved by
776 * weighted channels.
777 */
778int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight)
779{
780 struct cpdma_ctlr *ctlr = ch->ctlr;
781 unsigned long flags, ch_flags;
782 int ret;
783
784 spin_lock_irqsave(&ctlr->lock, flags);
785 spin_lock_irqsave(&ch->lock, ch_flags);
786 if (ch->weight == weight) {
787 spin_unlock_irqrestore(&ch->lock, ch_flags);
788 spin_unlock_irqrestore(&ctlr->lock, flags);
789 return 0;
790 }
791 ch->weight = weight;
792 spin_unlock_irqrestore(&ch->lock, ch_flags);
793
794 /* re-split pool using new channel weight */
795 ret = cpdma_chan_split_pool(ctlr);
796 spin_unlock_irqrestore(&ctlr->lock, flags);
797 return ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300798}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500799EXPORT_SYMBOL_GPL(cpdma_chan_set_weight);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300800
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200801/* cpdma_chan_get_min_rate - get minimum allowed rate for channel
802 * Should be called before cpdma_chan_set_rate.
803 * Returns min rate in Kb/s
804 */
805u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
806{
807 unsigned int divident, divisor;
808
809 divident = ctlr->params.bus_freq_mhz * 32 * 1000;
810 divisor = 1 + CPDMA_MAX_RLIM_CNT;
811
812 return DIV_ROUND_UP(divident, divisor);
813}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500814EXPORT_SYMBOL_GPL(cpdma_chan_get_min_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200815
816/* cpdma_chan_set_rate - limits bandwidth for transmit channel.
817 * The bandwidth * limited channels have to be in order beginning from lowest.
818 * ch - transmit channel the bandwidth is configured for
819 * rate - bandwidth in Kb/s, if 0 - then off shaper
820 */
821int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate)
822{
823 struct cpdma_ctlr *ctlr = ch->ctlr;
824 unsigned long flags, ch_flags;
825 int ret, prio_mode;
826 u32 rmask;
827
828 if (!ch || !is_tx_chan(ch))
829 return -EINVAL;
830
831 if (ch->rate == rate)
832 return rate;
833
834 spin_lock_irqsave(&ctlr->lock, flags);
835 spin_lock_irqsave(&ch->lock, ch_flags);
836
837 ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode);
838 if (ret)
839 goto err;
840
841 ret = cpdma_chan_set_factors(ctlr, ch);
842 if (ret)
843 goto err;
844
845 spin_unlock_irqrestore(&ch->lock, ch_flags);
846
847 /* on shapers */
848 _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
849 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
850 spin_unlock_irqrestore(&ctlr->lock, flags);
851 return ret;
852
853err:
854 spin_unlock_irqrestore(&ch->lock, ch_flags);
855 spin_unlock_irqrestore(&ctlr->lock, flags);
856 return ret;
857}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500858EXPORT_SYMBOL_GPL(cpdma_chan_set_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200859
860u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
861{
862 unsigned long flags;
863 u32 rate;
864
865 spin_lock_irqsave(&ch->lock, flags);
866 rate = ch->rate;
867 spin_unlock_irqrestore(&ch->lock, flags);
868
869 return rate;
870}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500871EXPORT_SYMBOL_GPL(cpdma_chan_get_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200872
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400873struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300874 cpdma_handler_fn handler, int rx_type)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400875{
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300876 int offset = chan_num * 4;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400877 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400878 unsigned long flags;
879
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300880 chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
881
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400882 if (__chan_linear(chan_num) >= ctlr->num_chan)
883 return NULL;
884
George Cheriane1943122014-05-12 10:21:21 +0530885 chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400886 if (!chan)
George Cheriane1943122014-05-12 10:21:21 +0530887 return ERR_PTR(-ENOMEM);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400888
889 spin_lock_irqsave(&ctlr->lock, flags);
George Cheriane1943122014-05-12 10:21:21 +0530890 if (ctlr->channels[chan_num]) {
891 spin_unlock_irqrestore(&ctlr->lock, flags);
892 devm_kfree(ctlr->dev, chan);
893 return ERR_PTR(-EBUSY);
894 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400895
896 chan->ctlr = ctlr;
897 chan->state = CPDMA_STATE_IDLE;
898 chan->chan_num = chan_num;
899 chan->handler = handler;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200900 chan->rate = 0;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300901 chan->desc_num = ctlr->pool->num_desc / 2;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200902 chan->weight = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400903
904 if (is_rx_chan(chan)) {
905 chan->hdp = ctlr->params.rxhdp + offset;
906 chan->cp = ctlr->params.rxcp + offset;
907 chan->rxfree = ctlr->params.rxfree + offset;
908 chan->int_set = CPDMA_RXINTMASKSET;
909 chan->int_clear = CPDMA_RXINTMASKCLEAR;
910 chan->td = CPDMA_RXTEARDOWN;
911 chan->dir = DMA_FROM_DEVICE;
912 } else {
913 chan->hdp = ctlr->params.txhdp + offset;
914 chan->cp = ctlr->params.txcp + offset;
915 chan->int_set = CPDMA_TXINTMASKSET;
916 chan->int_clear = CPDMA_TXINTMASKCLEAR;
917 chan->td = CPDMA_TXTEARDOWN;
918 chan->dir = DMA_TO_DEVICE;
919 }
920 chan->mask = BIT(chan_linear(chan));
921
922 spin_lock_init(&chan->lock);
923
924 ctlr->channels[chan_num] = chan;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300925 ctlr->chan_num++;
926
927 cpdma_chan_split_pool(ctlr);
928
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400929 spin_unlock_irqrestore(&ctlr->lock, flags);
930 return chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400931}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000932EXPORT_SYMBOL_GPL(cpdma_chan_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400933
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300934int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300935{
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300936 unsigned long flags;
937 int desc_num;
938
939 spin_lock_irqsave(&chan->lock, flags);
940 desc_num = chan->desc_num;
941 spin_unlock_irqrestore(&chan->lock, flags);
942
943 return desc_num;
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300944}
945EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
946
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400947int cpdma_chan_destroy(struct cpdma_chan *chan)
948{
Julia Lawallf37c54b2012-08-14 05:49:47 +0000949 struct cpdma_ctlr *ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400950 unsigned long flags;
951
952 if (!chan)
953 return -EINVAL;
Julia Lawallf37c54b2012-08-14 05:49:47 +0000954 ctlr = chan->ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400955
956 spin_lock_irqsave(&ctlr->lock, flags);
957 if (chan->state != CPDMA_STATE_IDLE)
958 cpdma_chan_stop(chan);
959 ctlr->channels[chan->chan_num] = NULL;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300960 ctlr->chan_num--;
Ivan Khoronzhukb602e492016-11-08 15:16:05 +0200961 devm_kfree(ctlr->dev, chan);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300962 cpdma_chan_split_pool(ctlr);
963
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400964 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400965 return 0;
966}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000967EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400968
969int cpdma_chan_get_stats(struct cpdma_chan *chan,
970 struct cpdma_chan_stats *stats)
971{
972 unsigned long flags;
973 if (!chan)
974 return -EINVAL;
975 spin_lock_irqsave(&chan->lock, flags);
976 memcpy(stats, &chan->stats, sizeof(*stats));
977 spin_unlock_irqrestore(&chan->lock, flags);
978 return 0;
979}
Daniel Mack0ca04b62013-08-22 13:47:00 +0200980EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400981
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400982static void __cpdma_chan_submit(struct cpdma_chan *chan,
983 struct cpdma_desc __iomem *desc)
984{
985 struct cpdma_ctlr *ctlr = chan->ctlr;
986 struct cpdma_desc __iomem *prev = chan->tail;
987 struct cpdma_desc_pool *pool = ctlr->pool;
988 dma_addr_t desc_dma;
989 u32 mode;
990
991 desc_dma = desc_phys(pool, desc);
992
993 /* simple case - idle channel */
994 if (!chan->head) {
995 chan->stats.head_enqueue++;
996 chan->head = desc;
997 chan->tail = desc;
998 if (chan->state == CPDMA_STATE_ACTIVE)
999 chan_write(chan, hdp, desc_dma);
1000 return;
1001 }
1002
1003 /* first chain the descriptor at the tail of the list */
1004 desc_write(prev, hw_next, desc_dma);
1005 chan->tail = desc;
1006 chan->stats.tail_enqueue++;
1007
1008 /* next check if EOQ has been triggered already */
1009 mode = desc_read(prev, hw_mode);
1010 if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
1011 (chan->state == CPDMA_STATE_ACTIVE)) {
1012 desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
1013 chan_write(chan, hdp, desc_dma);
1014 chan->stats.misqueued++;
1015 }
1016}
1017
1018int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
Sebastian Siewioraef614e2013-04-23 07:31:38 +00001019 int len, int directed)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001020{
1021 struct cpdma_ctlr *ctlr = chan->ctlr;
1022 struct cpdma_desc __iomem *desc;
1023 dma_addr_t buffer;
1024 unsigned long flags;
1025 u32 mode;
1026 int ret = 0;
1027
1028 spin_lock_irqsave(&chan->lock, flags);
1029
1030 if (chan->state == CPDMA_STATE_TEARDOWN) {
1031 ret = -EINVAL;
1032 goto unlock_ret;
1033 }
1034
Grygorii Strashko742fb202016-06-27 12:05:11 +03001035 if (chan->count >= chan->desc_num) {
1036 chan->stats.desc_alloc_fail++;
1037 ret = -ENOMEM;
1038 goto unlock_ret;
1039 }
1040
1041 desc = cpdma_desc_alloc(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001042 if (!desc) {
1043 chan->stats.desc_alloc_fail++;
1044 ret = -ENOMEM;
1045 goto unlock_ret;
1046 }
1047
1048 if (len < ctlr->params.min_packet_size) {
1049 len = ctlr->params.min_packet_size;
1050 chan->stats.runt_transmit_buff++;
1051 }
1052
1053 buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
Sebastian Siewior14bd0762013-06-20 16:58:45 +02001054 ret = dma_mapping_error(ctlr->dev, buffer);
1055 if (ret) {
1056 cpdma_desc_free(ctlr->pool, desc, 1);
1057 ret = -EINVAL;
1058 goto unlock_ret;
1059 }
1060
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001061 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001062 cpdma_desc_to_port(chan, mode, directed);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001063
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001064 /* Relaxed IO accessors can be used here as there is read barrier
1065 * at the end of write sequence.
1066 */
1067 writel_relaxed(0, &desc->hw_next);
1068 writel_relaxed(buffer, &desc->hw_buffer);
1069 writel_relaxed(len, &desc->hw_len);
1070 writel_relaxed(mode | len, &desc->hw_mode);
1071 writel_relaxed(token, &desc->sw_token);
1072 writel_relaxed(buffer, &desc->sw_buffer);
1073 writel_relaxed(len, &desc->sw_len);
1074 desc_read(desc, sw_len);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001075
1076 __cpdma_chan_submit(chan, desc);
1077
1078 if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
1079 chan_write(chan, rxfree, 1);
1080
1081 chan->count++;
1082
1083unlock_ret:
1084 spin_unlock_irqrestore(&chan->lock, flags);
1085 return ret;
1086}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001087EXPORT_SYMBOL_GPL(cpdma_chan_submit);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001088
Mugunthan V Nfae50822013-01-17 06:31:34 +00001089bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
1090{
Mugunthan V Nfae50822013-01-17 06:31:34 +00001091 struct cpdma_ctlr *ctlr = chan->ctlr;
1092 struct cpdma_desc_pool *pool = ctlr->pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +03001093 bool free_tx_desc;
1094 unsigned long flags;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001095
Grygorii Strashko742fb202016-06-27 12:05:11 +03001096 spin_lock_irqsave(&chan->lock, flags);
1097 free_tx_desc = (chan->count < chan->desc_num) &&
1098 gen_pool_avail(pool->gen_pool);
1099 spin_unlock_irqrestore(&chan->lock, flags);
1100 return free_tx_desc;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001101}
1102EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
1103
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001104static void __cpdma_chan_free(struct cpdma_chan *chan,
1105 struct cpdma_desc __iomem *desc,
1106 int outlen, int status)
1107{
1108 struct cpdma_ctlr *ctlr = chan->ctlr;
1109 struct cpdma_desc_pool *pool = ctlr->pool;
1110 dma_addr_t buff_dma;
1111 int origlen;
1112 void *token;
1113
1114 token = (void *)desc_read(desc, sw_token);
1115 buff_dma = desc_read(desc, sw_buffer);
1116 origlen = desc_read(desc, sw_len);
1117
1118 dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
1119 cpdma_desc_free(pool, desc, 1);
1120 (*chan->handler)(token, outlen, status);
1121}
1122
1123static int __cpdma_chan_process(struct cpdma_chan *chan)
1124{
1125 struct cpdma_ctlr *ctlr = chan->ctlr;
1126 struct cpdma_desc __iomem *desc;
1127 int status, outlen;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001128 int cb_status = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001129 struct cpdma_desc_pool *pool = ctlr->pool;
1130 dma_addr_t desc_dma;
1131 unsigned long flags;
1132
1133 spin_lock_irqsave(&chan->lock, flags);
1134
1135 desc = chan->head;
1136 if (!desc) {
1137 chan->stats.empty_dequeue++;
1138 status = -ENOENT;
1139 goto unlock_ret;
1140 }
1141 desc_dma = desc_phys(pool, desc);
1142
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001143 status = desc_read(desc, hw_mode);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001144 outlen = status & 0x7ff;
1145 if (status & CPDMA_DESC_OWNER) {
1146 chan->stats.busy_dequeue++;
1147 status = -EBUSY;
1148 goto unlock_ret;
1149 }
Mugunthan V N28a19fe2013-05-29 20:22:01 +00001150
1151 if (status & CPDMA_DESC_PASS_CRC)
1152 outlen -= CPDMA_DESC_CRC_LEN;
1153
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001154 status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
1155 CPDMA_DESC_PORT_MASK);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001156
1157 chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
1158 chan_write(chan, cp, desc_dma);
1159 chan->count--;
1160 chan->stats.good_dequeue++;
1161
Grygorii Strashko12a303e2017-01-06 14:07:30 -06001162 if ((status & CPDMA_DESC_EOQ) && chan->head) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001163 chan->stats.requeue++;
1164 chan_write(chan, hdp, desc_phys(pool, chan->head));
1165 }
1166
1167 spin_unlock_irqrestore(&chan->lock, flags);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001168 if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
1169 cb_status = -ENOSYS;
1170 else
1171 cb_status = status;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001172
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001173 __cpdma_chan_free(chan, desc, outlen, cb_status);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001174 return status;
1175
1176unlock_ret:
1177 spin_unlock_irqrestore(&chan->lock, flags);
1178 return status;
1179}
1180
1181int cpdma_chan_process(struct cpdma_chan *chan, int quota)
1182{
1183 int used = 0, ret = 0;
1184
1185 if (chan->state != CPDMA_STATE_ACTIVE)
1186 return -EINVAL;
1187
1188 while (used < quota) {
1189 ret = __cpdma_chan_process(chan);
1190 if (ret < 0)
1191 break;
1192 used++;
1193 }
1194 return used;
1195}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001196EXPORT_SYMBOL_GPL(cpdma_chan_process);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001197
1198int cpdma_chan_start(struct cpdma_chan *chan)
1199{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001200 struct cpdma_ctlr *ctlr = chan->ctlr;
1201 unsigned long flags;
1202 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001203
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001204 spin_lock_irqsave(&ctlr->lock, flags);
1205 ret = cpdma_chan_set_chan_shaper(chan);
1206 spin_unlock_irqrestore(&ctlr->lock, flags);
1207 if (ret)
1208 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001209
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001210 ret = cpdma_chan_on(chan);
1211 if (ret)
1212 return ret;
1213
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001214 return 0;
1215}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001216EXPORT_SYMBOL_GPL(cpdma_chan_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001217
1218int cpdma_chan_stop(struct cpdma_chan *chan)
1219{
1220 struct cpdma_ctlr *ctlr = chan->ctlr;
1221 struct cpdma_desc_pool *pool = ctlr->pool;
1222 unsigned long flags;
1223 int ret;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001224 unsigned timeout;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001225
1226 spin_lock_irqsave(&chan->lock, flags);
Christian Rieschcd11cf52014-03-24 13:46:27 +01001227 if (chan->state == CPDMA_STATE_TEARDOWN) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001228 spin_unlock_irqrestore(&chan->lock, flags);
1229 return -EINVAL;
1230 }
1231
1232 chan->state = CPDMA_STATE_TEARDOWN;
1233 dma_reg_write(ctlr, chan->int_clear, chan->mask);
1234
1235 /* trigger teardown */
Christian Rieschb4ad0422012-02-22 21:58:00 +00001236 dma_reg_write(ctlr, chan->td, chan_linear(chan));
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001237
1238 /* wait for teardown complete */
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001239 timeout = 100 * 100; /* 100 ms */
1240 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001241 u32 cp = chan_read(chan, cp);
1242 if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
1243 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001244 udelay(10);
1245 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001246 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001247 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001248 chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
1249
1250 /* handle completed packets */
Ilya Yanok7746ab02011-12-18 10:02:04 +00001251 spin_unlock_irqrestore(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001252 do {
1253 ret = __cpdma_chan_process(chan);
1254 if (ret < 0)
1255 break;
1256 } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
Ilya Yanok7746ab02011-12-18 10:02:04 +00001257 spin_lock_irqsave(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001258
1259 /* remaining packets haven't been tx/rx'ed, clean them up */
1260 while (chan->head) {
1261 struct cpdma_desc __iomem *desc = chan->head;
1262 dma_addr_t next_dma;
1263
1264 next_dma = desc_read(desc, hw_next);
1265 chan->head = desc_from_phys(pool, next_dma);
htbeginffb5ba92012-10-01 16:42:43 +00001266 chan->count--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001267 chan->stats.teardown_dequeue++;
1268
1269 /* issue callback without locks held */
1270 spin_unlock_irqrestore(&chan->lock, flags);
1271 __cpdma_chan_free(chan, desc, 0, -ENOSYS);
1272 spin_lock_irqsave(&chan->lock, flags);
1273 }
1274
1275 chan->state = CPDMA_STATE_IDLE;
1276 spin_unlock_irqrestore(&chan->lock, flags);
1277 return 0;
1278}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001279EXPORT_SYMBOL_GPL(cpdma_chan_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001280
1281int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
1282{
1283 unsigned long flags;
1284
1285 spin_lock_irqsave(&chan->lock, flags);
1286 if (chan->state != CPDMA_STATE_ACTIVE) {
1287 spin_unlock_irqrestore(&chan->lock, flags);
1288 return -EINVAL;
1289 }
1290
1291 dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
1292 chan->mask);
1293 spin_unlock_irqrestore(&chan->lock, flags);
1294
1295 return 0;
1296}
1297
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001298int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
1299{
1300 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001301 int ret;
1302
1303 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001304 ret = _cpdma_control_get(ctlr, control);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001305 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001306
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001307 return ret;
1308}
1309
1310int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
1311{
1312 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001313 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001314
1315 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +02001316 ret = _cpdma_control_set(ctlr, control, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001317 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001318
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001319 return ret;
1320}
Arnd Bergmann6929e242013-02-14 17:53:01 +01001321EXPORT_SYMBOL_GPL(cpdma_control_set);
Sebastian Siewior4bc21d42013-04-24 08:48:22 +00001322
1323MODULE_LICENSE("GPL");