blob: 7080eb9eceff8c887ee73543a7097696bbc6732d [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 Strashko5fcc40a2017-01-06 14:07:31 -0600184static void cpdma_desc_pool_destroy(struct cpdma_ctlr *ctlr)
Grygorii Strashko742fb202016-06-27 12:05:11 +0300185{
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600186 struct cpdma_desc_pool *pool = ctlr->pool;
187
Grygorii Strashko742fb202016-06-27 12:05:11 +0300188 if (!pool)
189 return;
190
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300191 WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool),
192 "cpdma_desc_pool size %d != avail %d",
193 gen_pool_size(pool->gen_pool),
194 gen_pool_avail(pool->gen_pool));
Grygorii Strashko742fb202016-06-27 12:05:11 +0300195 if (pool->cpumap)
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600196 dma_free_coherent(ctlr->dev, pool->mem_size, pool->cpumap,
Grygorii Strashko742fb202016-06-27 12:05:11 +0300197 pool->phys);
198 else
199 iounmap(pool->iomap);
200}
201
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400202/*
203 * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
204 * emac) have dedicated on-chip memory for these descriptors. Some other
205 * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
206 * abstract out these details
207 */
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600208int cpdma_desc_pool_create(struct cpdma_ctlr *ctlr)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400209{
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600210 struct cpdma_params *cpdma_params = &ctlr->params;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400211 struct cpdma_desc_pool *pool;
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600212 int ret = -ENOMEM;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400213
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600214 pool = devm_kzalloc(ctlr->dev, sizeof(*pool), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400215 if (!pool)
Grygorii Strashko742fb202016-06-27 12:05:11 +0300216 goto gen_pool_create_fail;
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600217 ctlr->pool = pool;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400218
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600219 pool->mem_size = cpdma_params->desc_mem_size;
220 pool->desc_size = ALIGN(sizeof(struct cpdma_desc),
221 cpdma_params->desc_align);
222 pool->num_desc = pool->mem_size / pool->desc_size;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400223
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600224 pool->gen_pool = devm_gen_pool_create(ctlr->dev, ilog2(pool->desc_size),
225 -1, "cpdma");
Grygorii Strashko742fb202016-06-27 12:05:11 +0300226 if (IS_ERR(pool->gen_pool)) {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600227 ret = PTR_ERR(pool->gen_pool);
228 dev_err(ctlr->dev, "pool create failed %d\n", ret);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300229 goto gen_pool_create_fail;
230 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400231
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600232 if (cpdma_params->desc_mem_phys) {
233 pool->phys = cpdma_params->desc_mem_phys;
234 pool->iomap = ioremap(pool->phys, pool->mem_size);
235 pool->hw_addr = cpdma_params->desc_hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400236 } else {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600237 pool->cpumap = dma_alloc_coherent(ctlr->dev, pool->mem_size,
238 &pool->hw_addr, GFP_KERNEL);
Arnd Bergmann84092992016-01-29 12:39:10 +0100239 pool->iomap = (void __iomem __force *)pool->cpumap;
240 pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400241 }
242
Grygorii Strashko742fb202016-06-27 12:05:11 +0300243 if (!pool->iomap)
244 goto gen_pool_create_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400245
Grygorii Strashko742fb202016-06-27 12:05:11 +0300246 ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
247 pool->phys, pool->mem_size, -1);
248 if (ret < 0) {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600249 dev_err(ctlr->dev, "pool add failed %d\n", ret);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300250 goto gen_pool_add_virt_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400251 }
Grygorii Strashko742fb202016-06-27 12:05:11 +0300252
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600253 return 0;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300254
255gen_pool_add_virt_fail:
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600256 cpdma_desc_pool_destroy(ctlr);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300257gen_pool_create_fail:
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600258 ctlr->pool = NULL;
259 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400260}
261
262static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
263 struct cpdma_desc __iomem *desc)
264{
265 if (!desc)
266 return 0;
Olof Johanssonc767db52013-12-11 15:51:20 -0800267 return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400268}
269
270static inline struct cpdma_desc __iomem *
271desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
272{
Sriram6a1fef62011-03-22 02:31:03 +0000273 return dma ? pool->iomap + dma - pool->hw_addr : NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400274}
275
276static struct cpdma_desc __iomem *
Grygorii Strashko742fb202016-06-27 12:05:11 +0300277cpdma_desc_alloc(struct cpdma_desc_pool *pool)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400278{
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300279 return (struct cpdma_desc __iomem *)
280 gen_pool_alloc(pool->gen_pool, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400281}
282
283static void cpdma_desc_free(struct cpdma_desc_pool *pool,
284 struct cpdma_desc __iomem *desc, int num_desc)
285{
Grygorii Strashko742fb202016-06-27 12:05:11 +0300286 gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400287}
288
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200289static int _cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
290{
291 struct cpdma_control_info *info = &controls[control];
292 u32 val;
293
294 if (!ctlr->params.has_ext_regs)
295 return -ENOTSUPP;
296
297 if (ctlr->state != CPDMA_STATE_ACTIVE)
298 return -EINVAL;
299
300 if (control < 0 || control >= ARRAY_SIZE(controls))
301 return -ENOENT;
302
303 if ((info->access & ACCESS_WO) != ACCESS_WO)
304 return -EPERM;
305
306 val = dma_reg_read(ctlr, info->reg);
307 val &= ~(info->mask << info->shift);
308 val |= (value & info->mask) << info->shift;
309 dma_reg_write(ctlr, info->reg, val);
310
311 return 0;
312}
313
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200314static int _cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
315{
316 struct cpdma_control_info *info = &controls[control];
317 int ret;
318
319 if (!ctlr->params.has_ext_regs)
320 return -ENOTSUPP;
321
322 if (ctlr->state != CPDMA_STATE_ACTIVE)
323 return -EINVAL;
324
325 if (control < 0 || control >= ARRAY_SIZE(controls))
326 return -ENOENT;
327
328 if ((info->access & ACCESS_RO) != ACCESS_RO)
329 return -EPERM;
330
331 ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
332 return ret;
333}
334
335/* cpdma_chan_set_chan_shaper - set shaper for a channel
336 * Has to be called under ctlr lock
337 */
338static int cpdma_chan_set_chan_shaper(struct cpdma_chan *chan)
339{
340 struct cpdma_ctlr *ctlr = chan->ctlr;
341 u32 rate_reg;
342 u32 rmask;
343 int ret;
344
345 if (!chan->rate)
346 return 0;
347
348 rate_reg = CPDMA_TX_PRI0_RATE + 4 * chan->chan_num;
349 dma_reg_write(ctlr, rate_reg, chan->rate_factor);
350
351 rmask = _cpdma_control_get(ctlr, CPDMA_TX_RLIM);
352 rmask |= chan->mask;
353
354 ret = _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
355 return ret;
356}
357
358static int cpdma_chan_on(struct cpdma_chan *chan)
359{
360 struct cpdma_ctlr *ctlr = chan->ctlr;
361 struct cpdma_desc_pool *pool = ctlr->pool;
362 unsigned long flags;
363
364 spin_lock_irqsave(&chan->lock, flags);
365 if (chan->state != CPDMA_STATE_IDLE) {
366 spin_unlock_irqrestore(&chan->lock, flags);
367 return -EBUSY;
368 }
369 if (ctlr->state != CPDMA_STATE_ACTIVE) {
370 spin_unlock_irqrestore(&chan->lock, flags);
371 return -EINVAL;
372 }
373 dma_reg_write(ctlr, chan->int_set, chan->mask);
374 chan->state = CPDMA_STATE_ACTIVE;
375 if (chan->head) {
376 chan_write(chan, hdp, desc_phys(pool, chan->head));
377 if (chan->rxfree)
378 chan_write(chan, rxfree, chan->count);
379 }
380
381 spin_unlock_irqrestore(&chan->lock, flags);
382 return 0;
383}
384
385/* cpdma_chan_fit_rate - set rate for a channel and check if it's possible.
386 * rmask - mask of rate limited channels
387 * Returns min rate in Kb/s
388 */
389static int cpdma_chan_fit_rate(struct cpdma_chan *ch, u32 rate,
390 u32 *rmask, int *prio_mode)
391{
392 struct cpdma_ctlr *ctlr = ch->ctlr;
393 struct cpdma_chan *chan;
394 u32 old_rate = ch->rate;
395 u32 new_rmask = 0;
396 int rlim = 1;
397 int i;
398
399 *prio_mode = 0;
400 for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) {
401 chan = ctlr->channels[i];
402 if (!chan) {
403 rlim = 0;
404 continue;
405 }
406
407 if (chan == ch)
408 chan->rate = rate;
409
410 if (chan->rate) {
411 if (rlim) {
412 new_rmask |= chan->mask;
413 } else {
414 ch->rate = old_rate;
415 dev_err(ctlr->dev, "Prev channel of %dch is not rate limited\n",
416 chan->chan_num);
417 return -EINVAL;
418 }
419 } else {
420 *prio_mode = 1;
421 rlim = 0;
422 }
423 }
424
425 *rmask = new_rmask;
426 return 0;
427}
428
429static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr,
430 struct cpdma_chan *ch)
431{
432 u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX;
433 u32 best_send_cnt = 0, best_idle_cnt = 0;
434 u32 new_rate, best_rate = 0, rate_reg;
435 u64 send_cnt, idle_cnt;
436 u32 min_send_cnt, freq;
437 u64 divident, divisor;
438
439 if (!ch->rate) {
440 ch->rate_factor = 0;
441 goto set_factor;
442 }
443
444 freq = ctlr->params.bus_freq_mhz * 1000 * 32;
445 if (!freq) {
446 dev_err(ctlr->dev, "The bus frequency is not set\n");
447 return -EINVAL;
448 }
449
450 min_send_cnt = freq - ch->rate;
451 send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate);
452 while (send_cnt <= CPDMA_MAX_RLIM_CNT) {
453 divident = ch->rate * send_cnt;
454 divisor = min_send_cnt;
455 idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor);
456
457 divident = freq * idle_cnt;
458 divisor = idle_cnt + send_cnt;
459 new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor);
460
461 delta = new_rate >= ch->rate ? new_rate - ch->rate : delta;
462 if (delta < best_delta) {
463 best_delta = delta;
464 best_send_cnt = send_cnt;
465 best_idle_cnt = idle_cnt;
466 best_rate = new_rate;
467
468 if (!delta)
469 break;
470 }
471
472 if (prev_delta >= delta) {
473 prev_delta = delta;
474 send_cnt++;
475 continue;
476 }
477
478 idle_cnt++;
479 divident = freq * idle_cnt;
480 send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate);
481 send_cnt -= idle_cnt;
482 prev_delta = UINT_MAX;
483 }
484
485 ch->rate = best_rate;
486 ch->rate_factor = best_send_cnt | (best_idle_cnt << 16);
487
488set_factor:
489 rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num;
490 dma_reg_write(ctlr, rate_reg, ch->rate_factor);
491 return 0;
492}
493
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400494struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
495{
496 struct cpdma_ctlr *ctlr;
497
George Cheriane1943122014-05-12 10:21:21 +0530498 ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400499 if (!ctlr)
500 return NULL;
501
502 ctlr->state = CPDMA_STATE_IDLE;
503 ctlr->params = *params;
504 ctlr->dev = params->dev;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300505 ctlr->chan_num = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400506 spin_lock_init(&ctlr->lock);
507
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600508 if (cpdma_desc_pool_create(ctlr))
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400509 return NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400510
511 if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
512 ctlr->num_chan = CPDMA_MAX_CHANNELS;
513 return ctlr;
514}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000515EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400516
517int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
518{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200519 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400520 unsigned long flags;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200521 int i, prio_mode;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400522
523 spin_lock_irqsave(&ctlr->lock, flags);
524 if (ctlr->state != CPDMA_STATE_IDLE) {
525 spin_unlock_irqrestore(&ctlr->lock, flags);
526 return -EBUSY;
527 }
528
529 if (ctlr->params.has_soft_reset) {
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000530 unsigned timeout = 10 * 100;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400531
532 dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000533 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400534 if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
535 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000536 udelay(10);
537 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400538 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000539 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400540 }
541
542 for (i = 0; i < ctlr->num_chan; i++) {
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -0600543 writel(0, ctlr->params.txhdp + 4 * i);
544 writel(0, ctlr->params.rxhdp + 4 * i);
545 writel(0, ctlr->params.txcp + 4 * i);
546 writel(0, ctlr->params.rxcp + 4 * i);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400547 }
548
549 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
550 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
551
552 dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
553 dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
554
555 ctlr->state = CPDMA_STATE_ACTIVE;
556
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200557 prio_mode = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400558 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200559 chan = ctlr->channels[i];
560 if (chan) {
561 cpdma_chan_set_chan_shaper(chan);
562 cpdma_chan_on(chan);
563
564 /* off prio mode if all tx channels are rate limited */
565 if (is_tx_chan(chan) && !chan->rate)
566 prio_mode = 1;
567 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400568 }
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200569
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200570 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200571 _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0);
572
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400573 spin_unlock_irqrestore(&ctlr->lock, flags);
574 return 0;
575}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000576EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400577
578int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
579{
580 unsigned long flags;
581 int i;
582
583 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhukb993eec2016-11-11 16:10:47 +0200584 if (ctlr->state != CPDMA_STATE_ACTIVE) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400585 spin_unlock_irqrestore(&ctlr->lock, flags);
586 return -EINVAL;
587 }
588
589 ctlr->state = CPDMA_STATE_TEARDOWN;
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300590 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400591
592 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
593 if (ctlr->channels[i])
594 cpdma_chan_stop(ctlr->channels[i]);
595 }
596
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300597 spin_lock_irqsave(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400598 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
599 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
600
601 dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
602 dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
603
604 ctlr->state = CPDMA_STATE_IDLE;
605
606 spin_unlock_irqrestore(&ctlr->lock, flags);
607 return 0;
608}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000609EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400610
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400611int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
612{
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400613 int ret = 0, i;
614
615 if (!ctlr)
616 return -EINVAL;
617
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400618 if (ctlr->state != CPDMA_STATE_IDLE)
619 cpdma_ctlr_stop(ctlr);
620
Cyril Roelandt79876e02013-02-12 12:52:30 +0000621 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
622 cpdma_chan_destroy(ctlr->channels[i]);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400623
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600624 cpdma_desc_pool_destroy(ctlr);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400625 return ret;
626}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000627EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400628
629int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
630{
631 unsigned long flags;
632 int i, reg;
633
634 spin_lock_irqsave(&ctlr->lock, flags);
635 if (ctlr->state != CPDMA_STATE_ACTIVE) {
636 spin_unlock_irqrestore(&ctlr->lock, flags);
637 return -EINVAL;
638 }
639
640 reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR;
641 dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR);
642
643 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
644 if (ctlr->channels[i])
645 cpdma_chan_int_ctrl(ctlr->channels[i], enable);
646 }
647
648 spin_unlock_irqrestore(&ctlr->lock, flags);
649 return 0;
650}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100651EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400652
Mugunthan V N510a1e722013-02-17 22:19:20 +0000653void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400654{
Mugunthan V N510a1e722013-02-17 22:19:20 +0000655 dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400656}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100657EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400658
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300659u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
660{
661 return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
662}
663EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state);
664
665u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
666{
667 return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
668}
669EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state);
670
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200671static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr,
672 int rx, int desc_num,
673 int per_ch_desc)
674{
675 struct cpdma_chan *chan, *most_chan = NULL;
676 int desc_cnt = desc_num;
677 int most_dnum = 0;
678 int min, max, i;
679
680 if (!desc_num)
681 return;
682
683 if (rx) {
684 min = rx_chan_num(0);
685 max = rx_chan_num(CPDMA_MAX_CHANNELS);
686 } else {
687 min = tx_chan_num(0);
688 max = tx_chan_num(CPDMA_MAX_CHANNELS);
689 }
690
691 for (i = min; i < max; i++) {
692 chan = ctlr->channels[i];
693 if (!chan)
694 continue;
695
696 if (chan->weight)
697 chan->desc_num = (chan->weight * desc_num) / 100;
698 else
699 chan->desc_num = per_ch_desc;
700
701 desc_cnt -= chan->desc_num;
702
703 if (most_dnum < chan->desc_num) {
704 most_dnum = chan->desc_num;
705 most_chan = chan;
706 }
707 }
708 /* use remains */
709 most_chan->desc_num += desc_cnt;
710}
711
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300712/**
713 * cpdma_chan_split_pool - Splits ctrl pool between all channels.
714 * Has to be called under ctlr lock
715 */
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200716static int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300717{
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200718 int tx_per_ch_desc = 0, rx_per_ch_desc = 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300719 struct cpdma_desc_pool *pool = ctlr->pool;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200720 int free_rx_num = 0, free_tx_num = 0;
721 int rx_weight = 0, tx_weight = 0;
722 int tx_desc_num, rx_desc_num;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300723 struct cpdma_chan *chan;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200724 int i, tx_num = 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300725
726 if (!ctlr->chan_num)
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200727 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300728
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300729 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
730 chan = ctlr->channels[i];
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200731 if (!chan)
732 continue;
733
734 if (is_rx_chan(chan)) {
735 if (!chan->weight)
736 free_rx_num++;
737 rx_weight += chan->weight;
738 } else {
739 if (!chan->weight)
740 free_tx_num++;
741 tx_weight += chan->weight;
742 tx_num++;
743 }
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300744 }
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200745
746 if (rx_weight > 100 || tx_weight > 100)
747 return -EINVAL;
748
749 tx_desc_num = (tx_num * pool->num_desc) / ctlr->chan_num;
750 rx_desc_num = pool->num_desc - tx_desc_num;
751
752 if (free_tx_num) {
753 tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100;
754 tx_per_ch_desc /= free_tx_num;
755 }
756 if (free_rx_num) {
757 rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100;
758 rx_per_ch_desc /= free_rx_num;
759 }
760
761 cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc);
762 cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc);
763
764 return 0;
765}
766
767/* cpdma_chan_set_weight - set weight of a channel in percentage.
768 * Tx and Rx channels have separate weights. That is 100% for RX
769 * and 100% for Tx. The weight is used to split cpdma resources
770 * in correct proportion required by the channels, including number
771 * of descriptors. The channel rate is not enough to know the
772 * weight of a channel as the maximum rate of an interface is needed.
773 * If weight = 0, then channel uses rest of descriptors leaved by
774 * weighted channels.
775 */
776int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight)
777{
778 struct cpdma_ctlr *ctlr = ch->ctlr;
779 unsigned long flags, ch_flags;
780 int ret;
781
782 spin_lock_irqsave(&ctlr->lock, flags);
783 spin_lock_irqsave(&ch->lock, ch_flags);
784 if (ch->weight == weight) {
785 spin_unlock_irqrestore(&ch->lock, ch_flags);
786 spin_unlock_irqrestore(&ctlr->lock, flags);
787 return 0;
788 }
789 ch->weight = weight;
790 spin_unlock_irqrestore(&ch->lock, ch_flags);
791
792 /* re-split pool using new channel weight */
793 ret = cpdma_chan_split_pool(ctlr);
794 spin_unlock_irqrestore(&ctlr->lock, flags);
795 return ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300796}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500797EXPORT_SYMBOL_GPL(cpdma_chan_set_weight);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300798
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200799/* cpdma_chan_get_min_rate - get minimum allowed rate for channel
800 * Should be called before cpdma_chan_set_rate.
801 * Returns min rate in Kb/s
802 */
803u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
804{
805 unsigned int divident, divisor;
806
807 divident = ctlr->params.bus_freq_mhz * 32 * 1000;
808 divisor = 1 + CPDMA_MAX_RLIM_CNT;
809
810 return DIV_ROUND_UP(divident, divisor);
811}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500812EXPORT_SYMBOL_GPL(cpdma_chan_get_min_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200813
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}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500856EXPORT_SYMBOL_GPL(cpdma_chan_set_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200857
858u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
859{
860 unsigned long flags;
861 u32 rate;
862
863 spin_lock_irqsave(&ch->lock, flags);
864 rate = ch->rate;
865 spin_unlock_irqrestore(&ch->lock, flags);
866
867 return rate;
868}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500869EXPORT_SYMBOL_GPL(cpdma_chan_get_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200870
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400871struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300872 cpdma_handler_fn handler, int rx_type)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400873{
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300874 int offset = chan_num * 4;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400875 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400876 unsigned long flags;
877
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300878 chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
879
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400880 if (__chan_linear(chan_num) >= ctlr->num_chan)
881 return NULL;
882
George Cheriane1943122014-05-12 10:21:21 +0530883 chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400884 if (!chan)
George Cheriane1943122014-05-12 10:21:21 +0530885 return ERR_PTR(-ENOMEM);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400886
887 spin_lock_irqsave(&ctlr->lock, flags);
George Cheriane1943122014-05-12 10:21:21 +0530888 if (ctlr->channels[chan_num]) {
889 spin_unlock_irqrestore(&ctlr->lock, flags);
890 devm_kfree(ctlr->dev, chan);
891 return ERR_PTR(-EBUSY);
892 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400893
894 chan->ctlr = ctlr;
895 chan->state = CPDMA_STATE_IDLE;
896 chan->chan_num = chan_num;
897 chan->handler = handler;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200898 chan->rate = 0;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300899 chan->desc_num = ctlr->pool->num_desc / 2;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200900 chan->weight = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400901
902 if (is_rx_chan(chan)) {
903 chan->hdp = ctlr->params.rxhdp + offset;
904 chan->cp = ctlr->params.rxcp + offset;
905 chan->rxfree = ctlr->params.rxfree + offset;
906 chan->int_set = CPDMA_RXINTMASKSET;
907 chan->int_clear = CPDMA_RXINTMASKCLEAR;
908 chan->td = CPDMA_RXTEARDOWN;
909 chan->dir = DMA_FROM_DEVICE;
910 } else {
911 chan->hdp = ctlr->params.txhdp + offset;
912 chan->cp = ctlr->params.txcp + offset;
913 chan->int_set = CPDMA_TXINTMASKSET;
914 chan->int_clear = CPDMA_TXINTMASKCLEAR;
915 chan->td = CPDMA_TXTEARDOWN;
916 chan->dir = DMA_TO_DEVICE;
917 }
918 chan->mask = BIT(chan_linear(chan));
919
920 spin_lock_init(&chan->lock);
921
922 ctlr->channels[chan_num] = chan;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300923 ctlr->chan_num++;
924
925 cpdma_chan_split_pool(ctlr);
926
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400927 spin_unlock_irqrestore(&ctlr->lock, flags);
928 return chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400929}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000930EXPORT_SYMBOL_GPL(cpdma_chan_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400931
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300932int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300933{
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300934 unsigned long flags;
935 int desc_num;
936
937 spin_lock_irqsave(&chan->lock, flags);
938 desc_num = chan->desc_num;
939 spin_unlock_irqrestore(&chan->lock, flags);
940
941 return desc_num;
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300942}
943EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
944
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400945int cpdma_chan_destroy(struct cpdma_chan *chan)
946{
Julia Lawallf37c54b2012-08-14 05:49:47 +0000947 struct cpdma_ctlr *ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400948 unsigned long flags;
949
950 if (!chan)
951 return -EINVAL;
Julia Lawallf37c54b2012-08-14 05:49:47 +0000952 ctlr = chan->ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400953
954 spin_lock_irqsave(&ctlr->lock, flags);
955 if (chan->state != CPDMA_STATE_IDLE)
956 cpdma_chan_stop(chan);
957 ctlr->channels[chan->chan_num] = NULL;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300958 ctlr->chan_num--;
Ivan Khoronzhukb602e492016-11-08 15:16:05 +0200959 devm_kfree(ctlr->dev, chan);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300960 cpdma_chan_split_pool(ctlr);
961
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400962 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400963 return 0;
964}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000965EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400966
967int cpdma_chan_get_stats(struct cpdma_chan *chan,
968 struct cpdma_chan_stats *stats)
969{
970 unsigned long flags;
971 if (!chan)
972 return -EINVAL;
973 spin_lock_irqsave(&chan->lock, flags);
974 memcpy(stats, &chan->stats, sizeof(*stats));
975 spin_unlock_irqrestore(&chan->lock, flags);
976 return 0;
977}
Daniel Mack0ca04b62013-08-22 13:47:00 +0200978EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400979
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400980static void __cpdma_chan_submit(struct cpdma_chan *chan,
981 struct cpdma_desc __iomem *desc)
982{
983 struct cpdma_ctlr *ctlr = chan->ctlr;
984 struct cpdma_desc __iomem *prev = chan->tail;
985 struct cpdma_desc_pool *pool = ctlr->pool;
986 dma_addr_t desc_dma;
987 u32 mode;
988
989 desc_dma = desc_phys(pool, desc);
990
991 /* simple case - idle channel */
992 if (!chan->head) {
993 chan->stats.head_enqueue++;
994 chan->head = desc;
995 chan->tail = desc;
996 if (chan->state == CPDMA_STATE_ACTIVE)
997 chan_write(chan, hdp, desc_dma);
998 return;
999 }
1000
1001 /* first chain the descriptor at the tail of the list */
1002 desc_write(prev, hw_next, desc_dma);
1003 chan->tail = desc;
1004 chan->stats.tail_enqueue++;
1005
1006 /* next check if EOQ has been triggered already */
1007 mode = desc_read(prev, hw_mode);
1008 if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
1009 (chan->state == CPDMA_STATE_ACTIVE)) {
1010 desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
1011 chan_write(chan, hdp, desc_dma);
1012 chan->stats.misqueued++;
1013 }
1014}
1015
1016int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
Sebastian Siewioraef614e2013-04-23 07:31:38 +00001017 int len, int directed)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001018{
1019 struct cpdma_ctlr *ctlr = chan->ctlr;
1020 struct cpdma_desc __iomem *desc;
1021 dma_addr_t buffer;
1022 unsigned long flags;
1023 u32 mode;
1024 int ret = 0;
1025
1026 spin_lock_irqsave(&chan->lock, flags);
1027
1028 if (chan->state == CPDMA_STATE_TEARDOWN) {
1029 ret = -EINVAL;
1030 goto unlock_ret;
1031 }
1032
Grygorii Strashko742fb202016-06-27 12:05:11 +03001033 if (chan->count >= chan->desc_num) {
1034 chan->stats.desc_alloc_fail++;
1035 ret = -ENOMEM;
1036 goto unlock_ret;
1037 }
1038
1039 desc = cpdma_desc_alloc(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001040 if (!desc) {
1041 chan->stats.desc_alloc_fail++;
1042 ret = -ENOMEM;
1043 goto unlock_ret;
1044 }
1045
1046 if (len < ctlr->params.min_packet_size) {
1047 len = ctlr->params.min_packet_size;
1048 chan->stats.runt_transmit_buff++;
1049 }
1050
1051 buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
Sebastian Siewior14bd0762013-06-20 16:58:45 +02001052 ret = dma_mapping_error(ctlr->dev, buffer);
1053 if (ret) {
1054 cpdma_desc_free(ctlr->pool, desc, 1);
1055 ret = -EINVAL;
1056 goto unlock_ret;
1057 }
1058
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001059 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001060 cpdma_desc_to_port(chan, mode, directed);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001061
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001062 /* Relaxed IO accessors can be used here as there is read barrier
1063 * at the end of write sequence.
1064 */
1065 writel_relaxed(0, &desc->hw_next);
1066 writel_relaxed(buffer, &desc->hw_buffer);
1067 writel_relaxed(len, &desc->hw_len);
1068 writel_relaxed(mode | len, &desc->hw_mode);
1069 writel_relaxed(token, &desc->sw_token);
1070 writel_relaxed(buffer, &desc->sw_buffer);
1071 writel_relaxed(len, &desc->sw_len);
1072 desc_read(desc, sw_len);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001073
1074 __cpdma_chan_submit(chan, desc);
1075
1076 if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
1077 chan_write(chan, rxfree, 1);
1078
1079 chan->count++;
1080
1081unlock_ret:
1082 spin_unlock_irqrestore(&chan->lock, flags);
1083 return ret;
1084}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001085EXPORT_SYMBOL_GPL(cpdma_chan_submit);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001086
Mugunthan V Nfae50822013-01-17 06:31:34 +00001087bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
1088{
Mugunthan V Nfae50822013-01-17 06:31:34 +00001089 struct cpdma_ctlr *ctlr = chan->ctlr;
1090 struct cpdma_desc_pool *pool = ctlr->pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +03001091 bool free_tx_desc;
1092 unsigned long flags;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001093
Grygorii Strashko742fb202016-06-27 12:05:11 +03001094 spin_lock_irqsave(&chan->lock, flags);
1095 free_tx_desc = (chan->count < chan->desc_num) &&
1096 gen_pool_avail(pool->gen_pool);
1097 spin_unlock_irqrestore(&chan->lock, flags);
1098 return free_tx_desc;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001099}
1100EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
1101
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001102static void __cpdma_chan_free(struct cpdma_chan *chan,
1103 struct cpdma_desc __iomem *desc,
1104 int outlen, int status)
1105{
1106 struct cpdma_ctlr *ctlr = chan->ctlr;
1107 struct cpdma_desc_pool *pool = ctlr->pool;
1108 dma_addr_t buff_dma;
1109 int origlen;
1110 void *token;
1111
1112 token = (void *)desc_read(desc, sw_token);
1113 buff_dma = desc_read(desc, sw_buffer);
1114 origlen = desc_read(desc, sw_len);
1115
1116 dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
1117 cpdma_desc_free(pool, desc, 1);
1118 (*chan->handler)(token, outlen, status);
1119}
1120
1121static int __cpdma_chan_process(struct cpdma_chan *chan)
1122{
1123 struct cpdma_ctlr *ctlr = chan->ctlr;
1124 struct cpdma_desc __iomem *desc;
1125 int status, outlen;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001126 int cb_status = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001127 struct cpdma_desc_pool *pool = ctlr->pool;
1128 dma_addr_t desc_dma;
1129 unsigned long flags;
1130
1131 spin_lock_irqsave(&chan->lock, flags);
1132
1133 desc = chan->head;
1134 if (!desc) {
1135 chan->stats.empty_dequeue++;
1136 status = -ENOENT;
1137 goto unlock_ret;
1138 }
1139 desc_dma = desc_phys(pool, desc);
1140
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001141 status = desc_read(desc, hw_mode);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001142 outlen = status & 0x7ff;
1143 if (status & CPDMA_DESC_OWNER) {
1144 chan->stats.busy_dequeue++;
1145 status = -EBUSY;
1146 goto unlock_ret;
1147 }
Mugunthan V N28a19fe2013-05-29 20:22:01 +00001148
1149 if (status & CPDMA_DESC_PASS_CRC)
1150 outlen -= CPDMA_DESC_CRC_LEN;
1151
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001152 status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
1153 CPDMA_DESC_PORT_MASK);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001154
1155 chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
1156 chan_write(chan, cp, desc_dma);
1157 chan->count--;
1158 chan->stats.good_dequeue++;
1159
Grygorii Strashko12a303e2017-01-06 14:07:30 -06001160 if ((status & CPDMA_DESC_EOQ) && chan->head) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001161 chan->stats.requeue++;
1162 chan_write(chan, hdp, desc_phys(pool, chan->head));
1163 }
1164
1165 spin_unlock_irqrestore(&chan->lock, flags);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001166 if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
1167 cb_status = -ENOSYS;
1168 else
1169 cb_status = status;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001170
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001171 __cpdma_chan_free(chan, desc, outlen, cb_status);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001172 return status;
1173
1174unlock_ret:
1175 spin_unlock_irqrestore(&chan->lock, flags);
1176 return status;
1177}
1178
1179int cpdma_chan_process(struct cpdma_chan *chan, int quota)
1180{
1181 int used = 0, ret = 0;
1182
1183 if (chan->state != CPDMA_STATE_ACTIVE)
1184 return -EINVAL;
1185
1186 while (used < quota) {
1187 ret = __cpdma_chan_process(chan);
1188 if (ret < 0)
1189 break;
1190 used++;
1191 }
1192 return used;
1193}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001194EXPORT_SYMBOL_GPL(cpdma_chan_process);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001195
1196int cpdma_chan_start(struct cpdma_chan *chan)
1197{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001198 struct cpdma_ctlr *ctlr = chan->ctlr;
1199 unsigned long flags;
1200 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001201
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001202 spin_lock_irqsave(&ctlr->lock, flags);
1203 ret = cpdma_chan_set_chan_shaper(chan);
1204 spin_unlock_irqrestore(&ctlr->lock, flags);
1205 if (ret)
1206 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001207
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001208 ret = cpdma_chan_on(chan);
1209 if (ret)
1210 return ret;
1211
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001212 return 0;
1213}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001214EXPORT_SYMBOL_GPL(cpdma_chan_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001215
1216int cpdma_chan_stop(struct cpdma_chan *chan)
1217{
1218 struct cpdma_ctlr *ctlr = chan->ctlr;
1219 struct cpdma_desc_pool *pool = ctlr->pool;
1220 unsigned long flags;
1221 int ret;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001222 unsigned timeout;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001223
1224 spin_lock_irqsave(&chan->lock, flags);
Christian Rieschcd11cf52014-03-24 13:46:27 +01001225 if (chan->state == CPDMA_STATE_TEARDOWN) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001226 spin_unlock_irqrestore(&chan->lock, flags);
1227 return -EINVAL;
1228 }
1229
1230 chan->state = CPDMA_STATE_TEARDOWN;
1231 dma_reg_write(ctlr, chan->int_clear, chan->mask);
1232
1233 /* trigger teardown */
Christian Rieschb4ad0422012-02-22 21:58:00 +00001234 dma_reg_write(ctlr, chan->td, chan_linear(chan));
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001235
1236 /* wait for teardown complete */
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001237 timeout = 100 * 100; /* 100 ms */
1238 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001239 u32 cp = chan_read(chan, cp);
1240 if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
1241 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001242 udelay(10);
1243 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001244 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001245 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001246 chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
1247
1248 /* handle completed packets */
Ilya Yanok7746ab02011-12-18 10:02:04 +00001249 spin_unlock_irqrestore(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001250 do {
1251 ret = __cpdma_chan_process(chan);
1252 if (ret < 0)
1253 break;
1254 } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
Ilya Yanok7746ab02011-12-18 10:02:04 +00001255 spin_lock_irqsave(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001256
1257 /* remaining packets haven't been tx/rx'ed, clean them up */
1258 while (chan->head) {
1259 struct cpdma_desc __iomem *desc = chan->head;
1260 dma_addr_t next_dma;
1261
1262 next_dma = desc_read(desc, hw_next);
1263 chan->head = desc_from_phys(pool, next_dma);
htbeginffb5ba92012-10-01 16:42:43 +00001264 chan->count--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001265 chan->stats.teardown_dequeue++;
1266
1267 /* issue callback without locks held */
1268 spin_unlock_irqrestore(&chan->lock, flags);
1269 __cpdma_chan_free(chan, desc, 0, -ENOSYS);
1270 spin_lock_irqsave(&chan->lock, flags);
1271 }
1272
1273 chan->state = CPDMA_STATE_IDLE;
1274 spin_unlock_irqrestore(&chan->lock, flags);
1275 return 0;
1276}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001277EXPORT_SYMBOL_GPL(cpdma_chan_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001278
1279int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
1280{
1281 unsigned long flags;
1282
1283 spin_lock_irqsave(&chan->lock, flags);
1284 if (chan->state != CPDMA_STATE_ACTIVE) {
1285 spin_unlock_irqrestore(&chan->lock, flags);
1286 return -EINVAL;
1287 }
1288
1289 dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
1290 chan->mask);
1291 spin_unlock_irqrestore(&chan->lock, flags);
1292
1293 return 0;
1294}
1295
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001296int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
1297{
1298 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001299 int ret;
1300
1301 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001302 ret = _cpdma_control_get(ctlr, control);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001303 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001304
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001305 return ret;
1306}
1307
1308int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
1309{
1310 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001311 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001312
1313 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +02001314 ret = _cpdma_control_set(ctlr, control, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001315 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001316
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001317 return ret;
1318}
Arnd Bergmann6929e242013-02-14 17:53:01 +01001319EXPORT_SYMBOL_GPL(cpdma_control_set);
Sebastian Siewior4bc21d42013-04-24 08:48:22 +00001320
1321MODULE_LICENSE("GPL");