blob: c776e4575d2da2d6d0b0cf584798c08ef705dd34 [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 */
169#define dma_reg_read(ctlr, ofs) __raw_readl((ctlr)->dmaregs + (ofs))
170#define chan_read(chan, fld) __raw_readl((chan)->fld)
171#define desc_read(desc, fld) __raw_readl(&(desc)->fld)
172#define dma_reg_write(ctlr, ofs, v) __raw_writel(v, (ctlr)->dmaregs + (ofs))
173#define chan_write(chan, fld, v) __raw_writel(v, (chan)->fld)
174#define desc_write(desc, fld, v) __raw_writel((u32)(v), &(desc)->fld)
175
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++) {
545 __raw_writel(0, ctlr->params.txhdp + 4 * i);
546 __raw_writel(0, ctlr->params.rxhdp + 4 * i);
547 __raw_writel(0, ctlr->params.txcp + 4 * i);
548 __raw_writel(0, ctlr->params.rxcp + 4 * i);
549 }
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}
799
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200800/* cpdma_chan_get_min_rate - get minimum allowed rate for channel
801 * Should be called before cpdma_chan_set_rate.
802 * Returns min rate in Kb/s
803 */
804u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
805{
806 unsigned int divident, divisor;
807
808 divident = ctlr->params.bus_freq_mhz * 32 * 1000;
809 divisor = 1 + CPDMA_MAX_RLIM_CNT;
810
811 return DIV_ROUND_UP(divident, divisor);
812}
813
814/* cpdma_chan_set_rate - limits bandwidth for transmit channel.
815 * The bandwidth * limited channels have to be in order beginning from lowest.
816 * ch - transmit channel the bandwidth is configured for
817 * rate - bandwidth in Kb/s, if 0 - then off shaper
818 */
819int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate)
820{
821 struct cpdma_ctlr *ctlr = ch->ctlr;
822 unsigned long flags, ch_flags;
823 int ret, prio_mode;
824 u32 rmask;
825
826 if (!ch || !is_tx_chan(ch))
827 return -EINVAL;
828
829 if (ch->rate == rate)
830 return rate;
831
832 spin_lock_irqsave(&ctlr->lock, flags);
833 spin_lock_irqsave(&ch->lock, ch_flags);
834
835 ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode);
836 if (ret)
837 goto err;
838
839 ret = cpdma_chan_set_factors(ctlr, ch);
840 if (ret)
841 goto err;
842
843 spin_unlock_irqrestore(&ch->lock, ch_flags);
844
845 /* on shapers */
846 _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
847 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
848 spin_unlock_irqrestore(&ctlr->lock, flags);
849 return ret;
850
851err:
852 spin_unlock_irqrestore(&ch->lock, ch_flags);
853 spin_unlock_irqrestore(&ctlr->lock, flags);
854 return ret;
855}
856
857u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
858{
859 unsigned long flags;
860 u32 rate;
861
862 spin_lock_irqsave(&ch->lock, flags);
863 rate = ch->rate;
864 spin_unlock_irqrestore(&ch->lock, flags);
865
866 return rate;
867}
868
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400869struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300870 cpdma_handler_fn handler, int rx_type)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400871{
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300872 int offset = chan_num * 4;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400873 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400874 unsigned long flags;
875
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300876 chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
877
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400878 if (__chan_linear(chan_num) >= ctlr->num_chan)
879 return NULL;
880
George Cheriane1943122014-05-12 10:21:21 +0530881 chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400882 if (!chan)
George Cheriane1943122014-05-12 10:21:21 +0530883 return ERR_PTR(-ENOMEM);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400884
885 spin_lock_irqsave(&ctlr->lock, flags);
George Cheriane1943122014-05-12 10:21:21 +0530886 if (ctlr->channels[chan_num]) {
887 spin_unlock_irqrestore(&ctlr->lock, flags);
888 devm_kfree(ctlr->dev, chan);
889 return ERR_PTR(-EBUSY);
890 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400891
892 chan->ctlr = ctlr;
893 chan->state = CPDMA_STATE_IDLE;
894 chan->chan_num = chan_num;
895 chan->handler = handler;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200896 chan->rate = 0;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300897 chan->desc_num = ctlr->pool->num_desc / 2;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200898 chan->weight = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400899
900 if (is_rx_chan(chan)) {
901 chan->hdp = ctlr->params.rxhdp + offset;
902 chan->cp = ctlr->params.rxcp + offset;
903 chan->rxfree = ctlr->params.rxfree + offset;
904 chan->int_set = CPDMA_RXINTMASKSET;
905 chan->int_clear = CPDMA_RXINTMASKCLEAR;
906 chan->td = CPDMA_RXTEARDOWN;
907 chan->dir = DMA_FROM_DEVICE;
908 } else {
909 chan->hdp = ctlr->params.txhdp + offset;
910 chan->cp = ctlr->params.txcp + offset;
911 chan->int_set = CPDMA_TXINTMASKSET;
912 chan->int_clear = CPDMA_TXINTMASKCLEAR;
913 chan->td = CPDMA_TXTEARDOWN;
914 chan->dir = DMA_TO_DEVICE;
915 }
916 chan->mask = BIT(chan_linear(chan));
917
918 spin_lock_init(&chan->lock);
919
920 ctlr->channels[chan_num] = chan;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300921 ctlr->chan_num++;
922
923 cpdma_chan_split_pool(ctlr);
924
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400925 spin_unlock_irqrestore(&ctlr->lock, flags);
926 return chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400927}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000928EXPORT_SYMBOL_GPL(cpdma_chan_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400929
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300930int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300931{
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300932 unsigned long flags;
933 int desc_num;
934
935 spin_lock_irqsave(&chan->lock, flags);
936 desc_num = chan->desc_num;
937 spin_unlock_irqrestore(&chan->lock, flags);
938
939 return desc_num;
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300940}
941EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
942
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400943int cpdma_chan_destroy(struct cpdma_chan *chan)
944{
Julia Lawallf37c54b2012-08-14 05:49:47 +0000945 struct cpdma_ctlr *ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400946 unsigned long flags;
947
948 if (!chan)
949 return -EINVAL;
Julia Lawallf37c54b2012-08-14 05:49:47 +0000950 ctlr = chan->ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400951
952 spin_lock_irqsave(&ctlr->lock, flags);
953 if (chan->state != CPDMA_STATE_IDLE)
954 cpdma_chan_stop(chan);
955 ctlr->channels[chan->chan_num] = NULL;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300956 ctlr->chan_num--;
Ivan Khoronzhukb602e492016-11-08 15:16:05 +0200957 devm_kfree(ctlr->dev, chan);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300958 cpdma_chan_split_pool(ctlr);
959
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400960 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400961 return 0;
962}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000963EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400964
965int cpdma_chan_get_stats(struct cpdma_chan *chan,
966 struct cpdma_chan_stats *stats)
967{
968 unsigned long flags;
969 if (!chan)
970 return -EINVAL;
971 spin_lock_irqsave(&chan->lock, flags);
972 memcpy(stats, &chan->stats, sizeof(*stats));
973 spin_unlock_irqrestore(&chan->lock, flags);
974 return 0;
975}
Daniel Mack0ca04b62013-08-22 13:47:00 +0200976EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400977
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400978static void __cpdma_chan_submit(struct cpdma_chan *chan,
979 struct cpdma_desc __iomem *desc)
980{
981 struct cpdma_ctlr *ctlr = chan->ctlr;
982 struct cpdma_desc __iomem *prev = chan->tail;
983 struct cpdma_desc_pool *pool = ctlr->pool;
984 dma_addr_t desc_dma;
985 u32 mode;
986
987 desc_dma = desc_phys(pool, desc);
988
989 /* simple case - idle channel */
990 if (!chan->head) {
991 chan->stats.head_enqueue++;
992 chan->head = desc;
993 chan->tail = desc;
994 if (chan->state == CPDMA_STATE_ACTIVE)
995 chan_write(chan, hdp, desc_dma);
996 return;
997 }
998
999 /* first chain the descriptor at the tail of the list */
1000 desc_write(prev, hw_next, desc_dma);
1001 chan->tail = desc;
1002 chan->stats.tail_enqueue++;
1003
1004 /* next check if EOQ has been triggered already */
1005 mode = desc_read(prev, hw_mode);
1006 if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
1007 (chan->state == CPDMA_STATE_ACTIVE)) {
1008 desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
1009 chan_write(chan, hdp, desc_dma);
1010 chan->stats.misqueued++;
1011 }
1012}
1013
1014int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
Sebastian Siewioraef614e2013-04-23 07:31:38 +00001015 int len, int directed)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001016{
1017 struct cpdma_ctlr *ctlr = chan->ctlr;
1018 struct cpdma_desc __iomem *desc;
1019 dma_addr_t buffer;
1020 unsigned long flags;
1021 u32 mode;
1022 int ret = 0;
1023
1024 spin_lock_irqsave(&chan->lock, flags);
1025
1026 if (chan->state == CPDMA_STATE_TEARDOWN) {
1027 ret = -EINVAL;
1028 goto unlock_ret;
1029 }
1030
Grygorii Strashko742fb202016-06-27 12:05:11 +03001031 if (chan->count >= chan->desc_num) {
1032 chan->stats.desc_alloc_fail++;
1033 ret = -ENOMEM;
1034 goto unlock_ret;
1035 }
1036
1037 desc = cpdma_desc_alloc(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001038 if (!desc) {
1039 chan->stats.desc_alloc_fail++;
1040 ret = -ENOMEM;
1041 goto unlock_ret;
1042 }
1043
1044 if (len < ctlr->params.min_packet_size) {
1045 len = ctlr->params.min_packet_size;
1046 chan->stats.runt_transmit_buff++;
1047 }
1048
1049 buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
Sebastian Siewior14bd0762013-06-20 16:58:45 +02001050 ret = dma_mapping_error(ctlr->dev, buffer);
1051 if (ret) {
1052 cpdma_desc_free(ctlr->pool, desc, 1);
1053 ret = -EINVAL;
1054 goto unlock_ret;
1055 }
1056
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001057 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001058 cpdma_desc_to_port(chan, mode, directed);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001059
1060 desc_write(desc, hw_next, 0);
1061 desc_write(desc, hw_buffer, buffer);
1062 desc_write(desc, hw_len, len);
1063 desc_write(desc, hw_mode, mode | len);
1064 desc_write(desc, sw_token, token);
1065 desc_write(desc, sw_buffer, buffer);
1066 desc_write(desc, sw_len, len);
1067
1068 __cpdma_chan_submit(chan, desc);
1069
1070 if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
1071 chan_write(chan, rxfree, 1);
1072
1073 chan->count++;
1074
1075unlock_ret:
1076 spin_unlock_irqrestore(&chan->lock, flags);
1077 return ret;
1078}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001079EXPORT_SYMBOL_GPL(cpdma_chan_submit);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001080
Mugunthan V Nfae50822013-01-17 06:31:34 +00001081bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
1082{
Mugunthan V Nfae50822013-01-17 06:31:34 +00001083 struct cpdma_ctlr *ctlr = chan->ctlr;
1084 struct cpdma_desc_pool *pool = ctlr->pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +03001085 bool free_tx_desc;
1086 unsigned long flags;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001087
Grygorii Strashko742fb202016-06-27 12:05:11 +03001088 spin_lock_irqsave(&chan->lock, flags);
1089 free_tx_desc = (chan->count < chan->desc_num) &&
1090 gen_pool_avail(pool->gen_pool);
1091 spin_unlock_irqrestore(&chan->lock, flags);
1092 return free_tx_desc;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001093}
1094EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
1095
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001096static void __cpdma_chan_free(struct cpdma_chan *chan,
1097 struct cpdma_desc __iomem *desc,
1098 int outlen, int status)
1099{
1100 struct cpdma_ctlr *ctlr = chan->ctlr;
1101 struct cpdma_desc_pool *pool = ctlr->pool;
1102 dma_addr_t buff_dma;
1103 int origlen;
1104 void *token;
1105
1106 token = (void *)desc_read(desc, sw_token);
1107 buff_dma = desc_read(desc, sw_buffer);
1108 origlen = desc_read(desc, sw_len);
1109
1110 dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
1111 cpdma_desc_free(pool, desc, 1);
1112 (*chan->handler)(token, outlen, status);
1113}
1114
1115static int __cpdma_chan_process(struct cpdma_chan *chan)
1116{
1117 struct cpdma_ctlr *ctlr = chan->ctlr;
1118 struct cpdma_desc __iomem *desc;
1119 int status, outlen;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001120 int cb_status = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001121 struct cpdma_desc_pool *pool = ctlr->pool;
1122 dma_addr_t desc_dma;
1123 unsigned long flags;
1124
1125 spin_lock_irqsave(&chan->lock, flags);
1126
1127 desc = chan->head;
1128 if (!desc) {
1129 chan->stats.empty_dequeue++;
1130 status = -ENOENT;
1131 goto unlock_ret;
1132 }
1133 desc_dma = desc_phys(pool, desc);
1134
1135 status = __raw_readl(&desc->hw_mode);
1136 outlen = status & 0x7ff;
1137 if (status & CPDMA_DESC_OWNER) {
1138 chan->stats.busy_dequeue++;
1139 status = -EBUSY;
1140 goto unlock_ret;
1141 }
Mugunthan V N28a19fe2013-05-29 20:22:01 +00001142
1143 if (status & CPDMA_DESC_PASS_CRC)
1144 outlen -= CPDMA_DESC_CRC_LEN;
1145
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001146 status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
1147 CPDMA_DESC_PORT_MASK);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001148
1149 chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
1150 chan_write(chan, cp, desc_dma);
1151 chan->count--;
1152 chan->stats.good_dequeue++;
1153
1154 if (status & CPDMA_DESC_EOQ) {
1155 chan->stats.requeue++;
1156 chan_write(chan, hdp, desc_phys(pool, chan->head));
1157 }
1158
1159 spin_unlock_irqrestore(&chan->lock, flags);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001160 if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
1161 cb_status = -ENOSYS;
1162 else
1163 cb_status = status;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001164
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001165 __cpdma_chan_free(chan, desc, outlen, cb_status);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001166 return status;
1167
1168unlock_ret:
1169 spin_unlock_irqrestore(&chan->lock, flags);
1170 return status;
1171}
1172
1173int cpdma_chan_process(struct cpdma_chan *chan, int quota)
1174{
1175 int used = 0, ret = 0;
1176
1177 if (chan->state != CPDMA_STATE_ACTIVE)
1178 return -EINVAL;
1179
1180 while (used < quota) {
1181 ret = __cpdma_chan_process(chan);
1182 if (ret < 0)
1183 break;
1184 used++;
1185 }
1186 return used;
1187}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001188EXPORT_SYMBOL_GPL(cpdma_chan_process);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001189
1190int cpdma_chan_start(struct cpdma_chan *chan)
1191{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001192 struct cpdma_ctlr *ctlr = chan->ctlr;
1193 unsigned long flags;
1194 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001195
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001196 spin_lock_irqsave(&ctlr->lock, flags);
1197 ret = cpdma_chan_set_chan_shaper(chan);
1198 spin_unlock_irqrestore(&ctlr->lock, flags);
1199 if (ret)
1200 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001201
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001202 ret = cpdma_chan_on(chan);
1203 if (ret)
1204 return ret;
1205
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001206 return 0;
1207}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001208EXPORT_SYMBOL_GPL(cpdma_chan_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001209
1210int cpdma_chan_stop(struct cpdma_chan *chan)
1211{
1212 struct cpdma_ctlr *ctlr = chan->ctlr;
1213 struct cpdma_desc_pool *pool = ctlr->pool;
1214 unsigned long flags;
1215 int ret;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001216 unsigned timeout;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001217
1218 spin_lock_irqsave(&chan->lock, flags);
Christian Rieschcd11cf52014-03-24 13:46:27 +01001219 if (chan->state == CPDMA_STATE_TEARDOWN) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001220 spin_unlock_irqrestore(&chan->lock, flags);
1221 return -EINVAL;
1222 }
1223
1224 chan->state = CPDMA_STATE_TEARDOWN;
1225 dma_reg_write(ctlr, chan->int_clear, chan->mask);
1226
1227 /* trigger teardown */
Christian Rieschb4ad0422012-02-22 21:58:00 +00001228 dma_reg_write(ctlr, chan->td, chan_linear(chan));
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001229
1230 /* wait for teardown complete */
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001231 timeout = 100 * 100; /* 100 ms */
1232 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001233 u32 cp = chan_read(chan, cp);
1234 if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
1235 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001236 udelay(10);
1237 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001238 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001239 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001240 chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
1241
1242 /* handle completed packets */
Ilya Yanok7746ab02011-12-18 10:02:04 +00001243 spin_unlock_irqrestore(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001244 do {
1245 ret = __cpdma_chan_process(chan);
1246 if (ret < 0)
1247 break;
1248 } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
Ilya Yanok7746ab02011-12-18 10:02:04 +00001249 spin_lock_irqsave(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001250
1251 /* remaining packets haven't been tx/rx'ed, clean them up */
1252 while (chan->head) {
1253 struct cpdma_desc __iomem *desc = chan->head;
1254 dma_addr_t next_dma;
1255
1256 next_dma = desc_read(desc, hw_next);
1257 chan->head = desc_from_phys(pool, next_dma);
htbeginffb5ba92012-10-01 16:42:43 +00001258 chan->count--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001259 chan->stats.teardown_dequeue++;
1260
1261 /* issue callback without locks held */
1262 spin_unlock_irqrestore(&chan->lock, flags);
1263 __cpdma_chan_free(chan, desc, 0, -ENOSYS);
1264 spin_lock_irqsave(&chan->lock, flags);
1265 }
1266
1267 chan->state = CPDMA_STATE_IDLE;
1268 spin_unlock_irqrestore(&chan->lock, flags);
1269 return 0;
1270}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001271EXPORT_SYMBOL_GPL(cpdma_chan_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001272
1273int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
1274{
1275 unsigned long flags;
1276
1277 spin_lock_irqsave(&chan->lock, flags);
1278 if (chan->state != CPDMA_STATE_ACTIVE) {
1279 spin_unlock_irqrestore(&chan->lock, flags);
1280 return -EINVAL;
1281 }
1282
1283 dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
1284 chan->mask);
1285 spin_unlock_irqrestore(&chan->lock, flags);
1286
1287 return 0;
1288}
1289
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001290int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
1291{
1292 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001293 int ret;
1294
1295 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001296 ret = _cpdma_control_get(ctlr, control);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001297 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001298
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001299 return ret;
1300}
1301
1302int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
1303{
1304 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001305 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001306
1307 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +02001308 ret = _cpdma_control_set(ctlr, control, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001309 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001310
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001311 return ret;
1312}
Arnd Bergmann6929e242013-02-14 17:53:01 +01001313EXPORT_SYMBOL_GPL(cpdma_control_set);
Sebastian Siewior4bc21d42013-04-24 08:48:22 +00001314
1315MODULE_LICENSE("GPL");