blob: 35bf14d8e7af322ee2fc3bc02cff3065b5b1c067 [file] [log] [blame]
Grygorii Strashko68cf0272019-04-26 20:12:23 +03001// SPDX-License-Identifier: GPL-2.0
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04002/*
3 * Texas Instruments CPDMA Driver
4 *
5 * Copyright (C) 2010 Texas Instruments
6 *
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04007 */
8#include <linux/kernel.h>
9#include <linux/spinlock.h>
10#include <linux/device.h>
Daniel Mack76fbc242012-06-28 06:12:32 +000011#include <linux/module.h>
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040012#include <linux/slab.h>
13#include <linux/err.h>
14#include <linux/dma-mapping.h>
15#include <linux/io.h>
Sebastian Siewior817f6d12013-04-23 07:31:35 +000016#include <linux/delay.h>
Grygorii Strashko742fb202016-06-27 12:05:11 +030017#include <linux/genalloc.h>
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040018#include "davinci_cpdma.h"
19
20/* DMA Registers */
21#define CPDMA_TXIDVER 0x00
22#define CPDMA_TXCONTROL 0x04
23#define CPDMA_TXTEARDOWN 0x08
24#define CPDMA_RXIDVER 0x10
25#define CPDMA_RXCONTROL 0x14
26#define CPDMA_SOFTRESET 0x1c
27#define CPDMA_RXTEARDOWN 0x18
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +020028#define CPDMA_TX_PRI0_RATE 0x30
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040029#define CPDMA_TXINTSTATRAW 0x80
30#define CPDMA_TXINTSTATMASKED 0x84
31#define CPDMA_TXINTMASKSET 0x88
32#define CPDMA_TXINTMASKCLEAR 0x8c
33#define CPDMA_MACINVECTOR 0x90
34#define CPDMA_MACEOIVECTOR 0x94
35#define CPDMA_RXINTSTATRAW 0xa0
36#define CPDMA_RXINTSTATMASKED 0xa4
37#define CPDMA_RXINTMASKSET 0xa8
38#define CPDMA_RXINTMASKCLEAR 0xac
39#define CPDMA_DMAINTSTATRAW 0xb0
40#define CPDMA_DMAINTSTATMASKED 0xb4
41#define CPDMA_DMAINTMASKSET 0xb8
42#define CPDMA_DMAINTMASKCLEAR 0xbc
43#define CPDMA_DMAINT_HOSTERR BIT(1)
44
45/* the following exist only if has_ext_regs is set */
46#define CPDMA_DMACONTROL 0x20
47#define CPDMA_DMASTATUS 0x24
48#define CPDMA_RXBUFFOFS 0x28
49#define CPDMA_EM_CONTROL 0x2c
50
51/* Descriptor mode bits */
52#define CPDMA_DESC_SOP BIT(31)
53#define CPDMA_DESC_EOP BIT(30)
54#define CPDMA_DESC_OWNER BIT(29)
55#define CPDMA_DESC_EOQ BIT(28)
56#define CPDMA_DESC_TD_COMPLETE BIT(27)
57#define CPDMA_DESC_PASS_CRC BIT(26)
Mugunthan V Nf6e135c2013-02-11 09:52:18 +000058#define CPDMA_DESC_TO_PORT_EN BIT(20)
59#define CPDMA_TO_PORT_SHIFT 16
60#define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16))
Mugunthan V N28a19fe2013-05-29 20:22:01 +000061#define CPDMA_DESC_CRC_LEN 4
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040062
63#define CPDMA_TEARDOWN_VALUE 0xfffffffc
64
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +020065#define CPDMA_MAX_RLIM_CNT 16384
66
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040067struct cpdma_desc {
68 /* hardware fields */
69 u32 hw_next;
70 u32 hw_buffer;
71 u32 hw_len;
72 u32 hw_mode;
73 /* software fields */
74 void *sw_token;
75 u32 sw_buffer;
76 u32 sw_len;
77};
78
79struct cpdma_desc_pool {
Olof Johanssonc767db52013-12-11 15:51:20 -080080 phys_addr_t phys;
Arnd Bergmann84092992016-01-29 12:39:10 +010081 dma_addr_t hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040082 void __iomem *iomap; /* ioremap map */
83 void *cpumap; /* dma_alloc map */
84 int desc_size, mem_size;
Grygorii Strashkoaeec3022016-08-04 18:20:51 +030085 int num_desc;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040086 struct device *dev;
Grygorii Strashko742fb202016-06-27 12:05:11 +030087 struct gen_pool *gen_pool;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040088};
89
90enum cpdma_state {
91 CPDMA_STATE_IDLE,
92 CPDMA_STATE_ACTIVE,
93 CPDMA_STATE_TEARDOWN,
94};
95
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -040096struct cpdma_ctlr {
97 enum cpdma_state state;
98 struct cpdma_params params;
99 struct device *dev;
100 struct cpdma_desc_pool *pool;
101 spinlock_t lock;
102 struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS];
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300103 int chan_num;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -0600104 int num_rx_desc; /* RX descriptors number */
105 int num_tx_desc; /* TX descriptors number */
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400106};
107
108struct cpdma_chan {
Mugunthan V Nfae50822013-01-17 06:31:34 +0000109 struct cpdma_desc __iomem *head, *tail;
110 void __iomem *hdp, *cp, *rxfree;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400111 enum cpdma_state state;
112 struct cpdma_ctlr *ctlr;
113 int chan_num;
114 spinlock_t lock;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400115 int count;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300116 u32 desc_num;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400117 u32 mask;
118 cpdma_handler_fn handler;
119 enum dma_data_direction dir;
120 struct cpdma_chan_stats stats;
121 /* offsets into dmaregs */
122 int int_set, int_clear, td;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200123 int weight;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200124 u32 rate_factor;
125 u32 rate;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400126};
127
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200128struct cpdma_control_info {
129 u32 reg;
130 u32 shift, mask;
131 int access;
132#define ACCESS_RO BIT(0)
133#define ACCESS_WO BIT(1)
134#define ACCESS_RW (ACCESS_RO | ACCESS_WO)
135};
136
137static struct cpdma_control_info controls[] = {
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200138 [CPDMA_TX_RLIM] = {CPDMA_DMACONTROL, 8, 0xffff, ACCESS_RW},
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200139 [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO},
140 [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW},
141 [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW},
142 [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW},
143 [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW},
144 [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO},
145 [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW},
146 [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW},
147 [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW},
148 [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW},
149 [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW},
150};
151
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300152#define tx_chan_num(chan) (chan)
153#define rx_chan_num(chan) ((chan) + CPDMA_MAX_CHANNELS)
154#define is_rx_chan(chan) ((chan)->chan_num >= CPDMA_MAX_CHANNELS)
155#define is_tx_chan(chan) (!is_rx_chan(chan))
156#define __chan_linear(chan_num) ((chan_num) & (CPDMA_MAX_CHANNELS - 1))
157#define chan_linear(chan) __chan_linear((chan)->chan_num)
158
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400159/* The following make access to common cpdma_ctlr params more readable */
160#define dmaregs params.dmaregs
161#define num_chan params.num_chan
162
163/* various accessors */
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -0600164#define dma_reg_read(ctlr, ofs) readl((ctlr)->dmaregs + (ofs))
165#define chan_read(chan, fld) readl((chan)->fld)
166#define desc_read(desc, fld) readl(&(desc)->fld)
167#define dma_reg_write(ctlr, ofs, v) writel(v, (ctlr)->dmaregs + (ofs))
168#define chan_write(chan, fld, v) writel(v, (chan)->fld)
169#define desc_write(desc, fld, v) writel((u32)(v), &(desc)->fld)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400170
Mugunthan V Nf6e135c2013-02-11 09:52:18 +0000171#define cpdma_desc_to_port(chan, mode, directed) \
172 do { \
173 if (!is_rx_chan(chan) && ((directed == 1) || \
174 (directed == 2))) \
175 mode |= (CPDMA_DESC_TO_PORT_EN | \
176 (directed << CPDMA_TO_PORT_SHIFT)); \
177 } while (0)
178
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600179static void cpdma_desc_pool_destroy(struct cpdma_ctlr *ctlr)
Grygorii Strashko742fb202016-06-27 12:05:11 +0300180{
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600181 struct cpdma_desc_pool *pool = ctlr->pool;
182
Grygorii Strashko742fb202016-06-27 12:05:11 +0300183 if (!pool)
184 return;
185
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300186 WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool),
Florian Fainellib4eb7392018-05-21 11:45:51 -0700187 "cpdma_desc_pool size %zd != avail %zd",
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300188 gen_pool_size(pool->gen_pool),
189 gen_pool_avail(pool->gen_pool));
Grygorii Strashko742fb202016-06-27 12:05:11 +0300190 if (pool->cpumap)
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600191 dma_free_coherent(ctlr->dev, pool->mem_size, pool->cpumap,
Grygorii Strashko742fb202016-06-27 12:05:11 +0300192 pool->phys);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300193}
194
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400195/*
196 * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
197 * emac) have dedicated on-chip memory for these descriptors. Some other
198 * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
199 * abstract out these details
200 */
Colin Ian King40141bb42018-06-21 18:16:45 +0100201static int cpdma_desc_pool_create(struct cpdma_ctlr *ctlr)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400202{
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600203 struct cpdma_params *cpdma_params = &ctlr->params;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400204 struct cpdma_desc_pool *pool;
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600205 int ret = -ENOMEM;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400206
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600207 pool = devm_kzalloc(ctlr->dev, sizeof(*pool), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400208 if (!pool)
Grygorii Strashko742fb202016-06-27 12:05:11 +0300209 goto gen_pool_create_fail;
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600210 ctlr->pool = pool;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400211
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600212 pool->mem_size = cpdma_params->desc_mem_size;
213 pool->desc_size = ALIGN(sizeof(struct cpdma_desc),
214 cpdma_params->desc_align);
215 pool->num_desc = pool->mem_size / pool->desc_size;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400216
Grygorii Strashko90225bf2017-01-06 14:07:33 -0600217 if (cpdma_params->descs_pool_size) {
218 /* recalculate memory size required cpdma descriptor pool
219 * basing on number of descriptors specified by user and
220 * if memory size > CPPI internal RAM size (desc_mem_size)
221 * then switch to use DDR
222 */
223 pool->num_desc = cpdma_params->descs_pool_size;
224 pool->mem_size = pool->desc_size * pool->num_desc;
225 if (pool->mem_size > cpdma_params->desc_mem_size)
226 cpdma_params->desc_mem_phys = 0;
227 }
228
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600229 pool->gen_pool = devm_gen_pool_create(ctlr->dev, ilog2(pool->desc_size),
230 -1, "cpdma");
Grygorii Strashko742fb202016-06-27 12:05:11 +0300231 if (IS_ERR(pool->gen_pool)) {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600232 ret = PTR_ERR(pool->gen_pool);
233 dev_err(ctlr->dev, "pool create failed %d\n", ret);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300234 goto gen_pool_create_fail;
235 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400236
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600237 if (cpdma_params->desc_mem_phys) {
238 pool->phys = cpdma_params->desc_mem_phys;
Grygorii Strashko7f3b4902017-01-06 14:07:32 -0600239 pool->iomap = devm_ioremap(ctlr->dev, pool->phys,
240 pool->mem_size);
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600241 pool->hw_addr = cpdma_params->desc_hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400242 } else {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600243 pool->cpumap = dma_alloc_coherent(ctlr->dev, pool->mem_size,
244 &pool->hw_addr, GFP_KERNEL);
Arnd Bergmann84092992016-01-29 12:39:10 +0100245 pool->iomap = (void __iomem __force *)pool->cpumap;
246 pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400247 }
248
Grygorii Strashko742fb202016-06-27 12:05:11 +0300249 if (!pool->iomap)
250 goto gen_pool_create_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400251
Grygorii Strashko742fb202016-06-27 12:05:11 +0300252 ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
253 pool->phys, pool->mem_size, -1);
254 if (ret < 0) {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600255 dev_err(ctlr->dev, "pool add failed %d\n", ret);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300256 goto gen_pool_add_virt_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400257 }
Grygorii Strashko742fb202016-06-27 12:05:11 +0300258
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600259 return 0;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300260
261gen_pool_add_virt_fail:
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600262 cpdma_desc_pool_destroy(ctlr);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300263gen_pool_create_fail:
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600264 ctlr->pool = NULL;
265 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400266}
267
268static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
269 struct cpdma_desc __iomem *desc)
270{
271 if (!desc)
272 return 0;
Olof Johanssonc767db52013-12-11 15:51:20 -0800273 return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400274}
275
276static inline struct cpdma_desc __iomem *
277desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
278{
Sriram6a1fef62011-03-22 02:31:03 +0000279 return dma ? pool->iomap + dma - pool->hw_addr : NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400280}
281
282static struct cpdma_desc __iomem *
Grygorii Strashko742fb202016-06-27 12:05:11 +0300283cpdma_desc_alloc(struct cpdma_desc_pool *pool)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400284{
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300285 return (struct cpdma_desc __iomem *)
286 gen_pool_alloc(pool->gen_pool, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400287}
288
289static void cpdma_desc_free(struct cpdma_desc_pool *pool,
290 struct cpdma_desc __iomem *desc, int num_desc)
291{
Grygorii Strashko742fb202016-06-27 12:05:11 +0300292 gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400293}
294
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200295static int _cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
296{
297 struct cpdma_control_info *info = &controls[control];
298 u32 val;
299
300 if (!ctlr->params.has_ext_regs)
301 return -ENOTSUPP;
302
303 if (ctlr->state != CPDMA_STATE_ACTIVE)
304 return -EINVAL;
305
306 if (control < 0 || control >= ARRAY_SIZE(controls))
307 return -ENOENT;
308
309 if ((info->access & ACCESS_WO) != ACCESS_WO)
310 return -EPERM;
311
312 val = dma_reg_read(ctlr, info->reg);
313 val &= ~(info->mask << info->shift);
314 val |= (value & info->mask) << info->shift;
315 dma_reg_write(ctlr, info->reg, val);
316
317 return 0;
318}
319
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200320static int _cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
321{
322 struct cpdma_control_info *info = &controls[control];
323 int ret;
324
325 if (!ctlr->params.has_ext_regs)
326 return -ENOTSUPP;
327
328 if (ctlr->state != CPDMA_STATE_ACTIVE)
329 return -EINVAL;
330
331 if (control < 0 || control >= ARRAY_SIZE(controls))
332 return -ENOENT;
333
334 if ((info->access & ACCESS_RO) != ACCESS_RO)
335 return -EPERM;
336
337 ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
338 return ret;
339}
340
341/* cpdma_chan_set_chan_shaper - set shaper for a channel
342 * Has to be called under ctlr lock
343 */
344static int cpdma_chan_set_chan_shaper(struct cpdma_chan *chan)
345{
346 struct cpdma_ctlr *ctlr = chan->ctlr;
347 u32 rate_reg;
348 u32 rmask;
349 int ret;
350
351 if (!chan->rate)
352 return 0;
353
354 rate_reg = CPDMA_TX_PRI0_RATE + 4 * chan->chan_num;
355 dma_reg_write(ctlr, rate_reg, chan->rate_factor);
356
357 rmask = _cpdma_control_get(ctlr, CPDMA_TX_RLIM);
358 rmask |= chan->mask;
359
360 ret = _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
361 return ret;
362}
363
364static int cpdma_chan_on(struct cpdma_chan *chan)
365{
366 struct cpdma_ctlr *ctlr = chan->ctlr;
367 struct cpdma_desc_pool *pool = ctlr->pool;
368 unsigned long flags;
369
370 spin_lock_irqsave(&chan->lock, flags);
371 if (chan->state != CPDMA_STATE_IDLE) {
372 spin_unlock_irqrestore(&chan->lock, flags);
373 return -EBUSY;
374 }
375 if (ctlr->state != CPDMA_STATE_ACTIVE) {
376 spin_unlock_irqrestore(&chan->lock, flags);
377 return -EINVAL;
378 }
379 dma_reg_write(ctlr, chan->int_set, chan->mask);
380 chan->state = CPDMA_STATE_ACTIVE;
381 if (chan->head) {
382 chan_write(chan, hdp, desc_phys(pool, chan->head));
383 if (chan->rxfree)
384 chan_write(chan, rxfree, chan->count);
385 }
386
387 spin_unlock_irqrestore(&chan->lock, flags);
388 return 0;
389}
390
391/* cpdma_chan_fit_rate - set rate for a channel and check if it's possible.
392 * rmask - mask of rate limited channels
393 * Returns min rate in Kb/s
394 */
395static int cpdma_chan_fit_rate(struct cpdma_chan *ch, u32 rate,
396 u32 *rmask, int *prio_mode)
397{
398 struct cpdma_ctlr *ctlr = ch->ctlr;
399 struct cpdma_chan *chan;
400 u32 old_rate = ch->rate;
401 u32 new_rmask = 0;
Ivan Khoronzhuk4bb6c352018-07-24 00:26:30 +0300402 int rlim = 0;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200403 int i;
404
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200405 for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) {
406 chan = ctlr->channels[i];
Ivan Khoronzhuk4bb6c352018-07-24 00:26:30 +0300407 if (!chan)
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200408 continue;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200409
410 if (chan == ch)
411 chan->rate = rate;
412
413 if (chan->rate) {
Ivan Khoronzhuk4bb6c352018-07-24 00:26:30 +0300414 rlim = 1;
415 new_rmask |= chan->mask;
416 continue;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200417 }
Ivan Khoronzhuk4bb6c352018-07-24 00:26:30 +0300418
419 if (rlim)
420 goto err;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200421 }
422
423 *rmask = new_rmask;
Ivan Khoronzhuk4bb6c352018-07-24 00:26:30 +0300424 *prio_mode = rlim;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200425 return 0;
Ivan Khoronzhuk4bb6c352018-07-24 00:26:30 +0300426
427err:
428 ch->rate = old_rate;
429 dev_err(ctlr->dev, "Upper cpdma ch%d is not rate limited\n",
430 chan->chan_num);
431 return -EINVAL;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200432}
433
434static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr,
435 struct cpdma_chan *ch)
436{
437 u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX;
438 u32 best_send_cnt = 0, best_idle_cnt = 0;
439 u32 new_rate, best_rate = 0, rate_reg;
440 u64 send_cnt, idle_cnt;
441 u32 min_send_cnt, freq;
442 u64 divident, divisor;
443
444 if (!ch->rate) {
445 ch->rate_factor = 0;
446 goto set_factor;
447 }
448
449 freq = ctlr->params.bus_freq_mhz * 1000 * 32;
450 if (!freq) {
451 dev_err(ctlr->dev, "The bus frequency is not set\n");
452 return -EINVAL;
453 }
454
455 min_send_cnt = freq - ch->rate;
456 send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate);
457 while (send_cnt <= CPDMA_MAX_RLIM_CNT) {
458 divident = ch->rate * send_cnt;
459 divisor = min_send_cnt;
460 idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor);
461
462 divident = freq * idle_cnt;
463 divisor = idle_cnt + send_cnt;
464 new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor);
465
466 delta = new_rate >= ch->rate ? new_rate - ch->rate : delta;
467 if (delta < best_delta) {
468 best_delta = delta;
469 best_send_cnt = send_cnt;
470 best_idle_cnt = idle_cnt;
471 best_rate = new_rate;
472
473 if (!delta)
474 break;
475 }
476
477 if (prev_delta >= delta) {
478 prev_delta = delta;
479 send_cnt++;
480 continue;
481 }
482
483 idle_cnt++;
484 divident = freq * idle_cnt;
485 send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate);
486 send_cnt -= idle_cnt;
487 prev_delta = UINT_MAX;
488 }
489
490 ch->rate = best_rate;
491 ch->rate_factor = best_send_cnt | (best_idle_cnt << 16);
492
493set_factor:
494 rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num;
495 dma_reg_write(ctlr, rate_reg, ch->rate_factor);
496 return 0;
497}
498
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400499struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
500{
501 struct cpdma_ctlr *ctlr;
502
George Cheriane1943122014-05-12 10:21:21 +0530503 ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400504 if (!ctlr)
505 return NULL;
506
507 ctlr->state = CPDMA_STATE_IDLE;
508 ctlr->params = *params;
509 ctlr->dev = params->dev;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300510 ctlr->chan_num = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400511 spin_lock_init(&ctlr->lock);
512
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600513 if (cpdma_desc_pool_create(ctlr))
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400514 return NULL;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -0600515 /* split pool equally between RX/TX by default */
516 ctlr->num_tx_desc = ctlr->pool->num_desc / 2;
517 ctlr->num_rx_desc = ctlr->pool->num_desc - ctlr->num_tx_desc;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400518
519 if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
520 ctlr->num_chan = CPDMA_MAX_CHANNELS;
521 return ctlr;
522}
523
524int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
525{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200526 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400527 unsigned long flags;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200528 int i, prio_mode;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400529
530 spin_lock_irqsave(&ctlr->lock, flags);
531 if (ctlr->state != CPDMA_STATE_IDLE) {
532 spin_unlock_irqrestore(&ctlr->lock, flags);
533 return -EBUSY;
534 }
535
536 if (ctlr->params.has_soft_reset) {
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000537 unsigned timeout = 10 * 100;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400538
539 dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000540 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400541 if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
542 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000543 udelay(10);
544 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400545 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000546 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400547 }
548
549 for (i = 0; i < ctlr->num_chan; i++) {
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -0600550 writel(0, ctlr->params.txhdp + 4 * i);
551 writel(0, ctlr->params.rxhdp + 4 * i);
552 writel(0, ctlr->params.txcp + 4 * i);
553 writel(0, ctlr->params.rxcp + 4 * i);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400554 }
555
556 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
557 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
558
559 dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
560 dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
561
562 ctlr->state = CPDMA_STATE_ACTIVE;
563
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200564 prio_mode = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400565 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200566 chan = ctlr->channels[i];
567 if (chan) {
568 cpdma_chan_set_chan_shaper(chan);
569 cpdma_chan_on(chan);
570
571 /* off prio mode if all tx channels are rate limited */
572 if (is_tx_chan(chan) && !chan->rate)
573 prio_mode = 1;
574 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400575 }
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200576
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200577 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200578 _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0);
579
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400580 spin_unlock_irqrestore(&ctlr->lock, flags);
581 return 0;
582}
583
584int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
585{
586 unsigned long flags;
587 int i;
588
589 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhukb993eec2016-11-11 16:10:47 +0200590 if (ctlr->state != CPDMA_STATE_ACTIVE) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400591 spin_unlock_irqrestore(&ctlr->lock, flags);
592 return -EINVAL;
593 }
594
595 ctlr->state = CPDMA_STATE_TEARDOWN;
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300596 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400597
598 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
599 if (ctlr->channels[i])
600 cpdma_chan_stop(ctlr->channels[i]);
601 }
602
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300603 spin_lock_irqsave(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400604 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
605 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
606
607 dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
608 dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
609
610 ctlr->state = CPDMA_STATE_IDLE;
611
612 spin_unlock_irqrestore(&ctlr->lock, flags);
613 return 0;
614}
615
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400616int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
617{
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400618 int ret = 0, i;
619
620 if (!ctlr)
621 return -EINVAL;
622
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400623 if (ctlr->state != CPDMA_STATE_IDLE)
624 cpdma_ctlr_stop(ctlr);
625
Cyril Roelandt79876e02013-02-12 12:52:30 +0000626 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
627 cpdma_chan_destroy(ctlr->channels[i]);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400628
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600629 cpdma_desc_pool_destroy(ctlr);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400630 return ret;
631}
632
633int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
634{
635 unsigned long flags;
Grygorii Strashko6d307f62017-06-08 13:51:52 -0500636 int i;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400637
638 spin_lock_irqsave(&ctlr->lock, flags);
639 if (ctlr->state != CPDMA_STATE_ACTIVE) {
640 spin_unlock_irqrestore(&ctlr->lock, flags);
641 return -EINVAL;
642 }
643
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400644 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
645 if (ctlr->channels[i])
646 cpdma_chan_int_ctrl(ctlr->channels[i], enable);
647 }
648
649 spin_unlock_irqrestore(&ctlr->lock, flags);
650 return 0;
651}
652
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}
657
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300658u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
659{
660 return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
661}
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300662
663u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
664{
665 return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
666}
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300667
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200668static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr,
669 int rx, int desc_num,
670 int per_ch_desc)
671{
672 struct cpdma_chan *chan, *most_chan = NULL;
673 int desc_cnt = desc_num;
674 int most_dnum = 0;
675 int min, max, i;
676
677 if (!desc_num)
678 return;
679
680 if (rx) {
681 min = rx_chan_num(0);
682 max = rx_chan_num(CPDMA_MAX_CHANNELS);
683 } else {
684 min = tx_chan_num(0);
685 max = tx_chan_num(CPDMA_MAX_CHANNELS);
686 }
687
688 for (i = min; i < max; i++) {
689 chan = ctlr->channels[i];
690 if (!chan)
691 continue;
692
693 if (chan->weight)
694 chan->desc_num = (chan->weight * desc_num) / 100;
695 else
696 chan->desc_num = per_ch_desc;
697
698 desc_cnt -= chan->desc_num;
699
700 if (most_dnum < chan->desc_num) {
701 most_dnum = chan->desc_num;
702 most_chan = chan;
703 }
704 }
705 /* use remains */
Grygorii Strashkobe034fc2017-01-06 14:07:34 -0600706 if (most_chan)
707 most_chan->desc_num += desc_cnt;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200708}
709
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300710/**
711 * cpdma_chan_split_pool - Splits ctrl pool between all channels.
712 * Has to be called under ctlr lock
713 */
Grygorii Strashkobe034fc2017-01-06 14:07:34 -0600714int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300715{
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200716 int tx_per_ch_desc = 0, rx_per_ch_desc = 0;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200717 int free_rx_num = 0, free_tx_num = 0;
718 int rx_weight = 0, tx_weight = 0;
719 int tx_desc_num, rx_desc_num;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300720 struct cpdma_chan *chan;
Grygorii Strashkobe034fc2017-01-06 14:07:34 -0600721 int i;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300722
723 if (!ctlr->chan_num)
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200724 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300725
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300726 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
727 chan = ctlr->channels[i];
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200728 if (!chan)
729 continue;
730
731 if (is_rx_chan(chan)) {
732 if (!chan->weight)
733 free_rx_num++;
734 rx_weight += chan->weight;
735 } else {
736 if (!chan->weight)
737 free_tx_num++;
738 tx_weight += chan->weight;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200739 }
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300740 }
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200741
742 if (rx_weight > 100 || tx_weight > 100)
743 return -EINVAL;
744
Grygorii Strashkobe034fc2017-01-06 14:07:34 -0600745 tx_desc_num = ctlr->num_tx_desc;
746 rx_desc_num = ctlr->num_rx_desc;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200747
748 if (free_tx_num) {
749 tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100;
750 tx_per_ch_desc /= free_tx_num;
751 }
752 if (free_rx_num) {
753 rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100;
754 rx_per_ch_desc /= free_rx_num;
755 }
756
757 cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc);
758 cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc);
759
760 return 0;
761}
Grygorii Strashkobe034fc2017-01-06 14:07:34 -0600762
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200763
764/* cpdma_chan_set_weight - set weight of a channel in percentage.
765 * Tx and Rx channels have separate weights. That is 100% for RX
766 * and 100% for Tx. The weight is used to split cpdma resources
767 * in correct proportion required by the channels, including number
768 * of descriptors. The channel rate is not enough to know the
769 * weight of a channel as the maximum rate of an interface is needed.
770 * If weight = 0, then channel uses rest of descriptors leaved by
771 * weighted channels.
772 */
773int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight)
774{
775 struct cpdma_ctlr *ctlr = ch->ctlr;
776 unsigned long flags, ch_flags;
777 int ret;
778
779 spin_lock_irqsave(&ctlr->lock, flags);
780 spin_lock_irqsave(&ch->lock, ch_flags);
781 if (ch->weight == weight) {
782 spin_unlock_irqrestore(&ch->lock, ch_flags);
783 spin_unlock_irqrestore(&ctlr->lock, flags);
784 return 0;
785 }
786 ch->weight = weight;
787 spin_unlock_irqrestore(&ch->lock, ch_flags);
788
789 /* re-split pool using new channel weight */
790 ret = cpdma_chan_split_pool(ctlr);
791 spin_unlock_irqrestore(&ctlr->lock, flags);
792 return ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300793}
794
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200795/* cpdma_chan_get_min_rate - get minimum allowed rate for channel
796 * Should be called before cpdma_chan_set_rate.
797 * Returns min rate in Kb/s
798 */
799u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
800{
801 unsigned int divident, divisor;
802
803 divident = ctlr->params.bus_freq_mhz * 32 * 1000;
804 divisor = 1 + CPDMA_MAX_RLIM_CNT;
805
806 return DIV_ROUND_UP(divident, divisor);
807}
808
809/* cpdma_chan_set_rate - limits bandwidth for transmit channel.
810 * The bandwidth * limited channels have to be in order beginning from lowest.
811 * ch - transmit channel the bandwidth is configured for
812 * rate - bandwidth in Kb/s, if 0 - then off shaper
813 */
814int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate)
815{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200816 unsigned long flags, ch_flags;
Ivan Khoronzhuke33c2ef2017-01-18 02:28:06 +0200817 struct cpdma_ctlr *ctlr;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200818 int ret, prio_mode;
819 u32 rmask;
820
821 if (!ch || !is_tx_chan(ch))
822 return -EINVAL;
823
824 if (ch->rate == rate)
825 return rate;
826
Ivan Khoronzhuke33c2ef2017-01-18 02:28:06 +0200827 ctlr = ch->ctlr;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200828 spin_lock_irqsave(&ctlr->lock, flags);
829 spin_lock_irqsave(&ch->lock, ch_flags);
830
831 ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode);
832 if (ret)
833 goto err;
834
835 ret = cpdma_chan_set_factors(ctlr, ch);
836 if (ret)
837 goto err;
838
839 spin_unlock_irqrestore(&ch->lock, ch_flags);
840
841 /* on shapers */
842 _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
843 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
844 spin_unlock_irqrestore(&ctlr->lock, flags);
845 return ret;
846
847err:
848 spin_unlock_irqrestore(&ch->lock, ch_flags);
849 spin_unlock_irqrestore(&ctlr->lock, flags);
850 return ret;
851}
852
853u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
854{
855 unsigned long flags;
856 u32 rate;
857
858 spin_lock_irqsave(&ch->lock, flags);
859 rate = ch->rate;
860 spin_unlock_irqrestore(&ch->lock, flags);
861
862 return rate;
863}
864
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400865struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300866 cpdma_handler_fn handler, int rx_type)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400867{
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300868 int offset = chan_num * 4;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400869 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400870 unsigned long flags;
871
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300872 chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
873
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400874 if (__chan_linear(chan_num) >= ctlr->num_chan)
Ivan Khoronzhuk8a83c5d2017-12-12 23:06:35 +0200875 return ERR_PTR(-EINVAL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400876
George Cheriane1943122014-05-12 10:21:21 +0530877 chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400878 if (!chan)
George Cheriane1943122014-05-12 10:21:21 +0530879 return ERR_PTR(-ENOMEM);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400880
881 spin_lock_irqsave(&ctlr->lock, flags);
George Cheriane1943122014-05-12 10:21:21 +0530882 if (ctlr->channels[chan_num]) {
883 spin_unlock_irqrestore(&ctlr->lock, flags);
884 devm_kfree(ctlr->dev, chan);
885 return ERR_PTR(-EBUSY);
886 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400887
888 chan->ctlr = ctlr;
889 chan->state = CPDMA_STATE_IDLE;
890 chan->chan_num = chan_num;
891 chan->handler = handler;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200892 chan->rate = 0;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200893 chan->weight = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400894
895 if (is_rx_chan(chan)) {
896 chan->hdp = ctlr->params.rxhdp + offset;
897 chan->cp = ctlr->params.rxcp + offset;
898 chan->rxfree = ctlr->params.rxfree + offset;
899 chan->int_set = CPDMA_RXINTMASKSET;
900 chan->int_clear = CPDMA_RXINTMASKCLEAR;
901 chan->td = CPDMA_RXTEARDOWN;
902 chan->dir = DMA_FROM_DEVICE;
903 } else {
904 chan->hdp = ctlr->params.txhdp + offset;
905 chan->cp = ctlr->params.txcp + offset;
906 chan->int_set = CPDMA_TXINTMASKSET;
907 chan->int_clear = CPDMA_TXINTMASKCLEAR;
908 chan->td = CPDMA_TXTEARDOWN;
909 chan->dir = DMA_TO_DEVICE;
910 }
911 chan->mask = BIT(chan_linear(chan));
912
913 spin_lock_init(&chan->lock);
914
915 ctlr->channels[chan_num] = chan;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300916 ctlr->chan_num++;
917
918 cpdma_chan_split_pool(ctlr);
919
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400920 spin_unlock_irqrestore(&ctlr->lock, flags);
921 return chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400922}
923
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300924int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300925{
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300926 unsigned long flags;
927 int desc_num;
928
929 spin_lock_irqsave(&chan->lock, flags);
930 desc_num = chan->desc_num;
931 spin_unlock_irqrestore(&chan->lock, flags);
932
933 return desc_num;
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300934}
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300935
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400936int cpdma_chan_destroy(struct cpdma_chan *chan)
937{
Julia Lawallf37c54b2012-08-14 05:49:47 +0000938 struct cpdma_ctlr *ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400939 unsigned long flags;
940
941 if (!chan)
942 return -EINVAL;
Julia Lawallf37c54b2012-08-14 05:49:47 +0000943 ctlr = chan->ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400944
945 spin_lock_irqsave(&ctlr->lock, flags);
946 if (chan->state != CPDMA_STATE_IDLE)
947 cpdma_chan_stop(chan);
948 ctlr->channels[chan->chan_num] = NULL;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300949 ctlr->chan_num--;
Ivan Khoronzhukb602e492016-11-08 15:16:05 +0200950 devm_kfree(ctlr->dev, chan);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300951 cpdma_chan_split_pool(ctlr);
952
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400953 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400954 return 0;
955}
956
957int cpdma_chan_get_stats(struct cpdma_chan *chan,
958 struct cpdma_chan_stats *stats)
959{
960 unsigned long flags;
961 if (!chan)
962 return -EINVAL;
963 spin_lock_irqsave(&chan->lock, flags);
964 memcpy(stats, &chan->stats, sizeof(*stats));
965 spin_unlock_irqrestore(&chan->lock, flags);
966 return 0;
967}
968
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400969static void __cpdma_chan_submit(struct cpdma_chan *chan,
970 struct cpdma_desc __iomem *desc)
971{
972 struct cpdma_ctlr *ctlr = chan->ctlr;
973 struct cpdma_desc __iomem *prev = chan->tail;
974 struct cpdma_desc_pool *pool = ctlr->pool;
975 dma_addr_t desc_dma;
976 u32 mode;
977
978 desc_dma = desc_phys(pool, desc);
979
980 /* simple case - idle channel */
981 if (!chan->head) {
982 chan->stats.head_enqueue++;
983 chan->head = desc;
984 chan->tail = desc;
985 if (chan->state == CPDMA_STATE_ACTIVE)
986 chan_write(chan, hdp, desc_dma);
987 return;
988 }
989
990 /* first chain the descriptor at the tail of the list */
991 desc_write(prev, hw_next, desc_dma);
992 chan->tail = desc;
993 chan->stats.tail_enqueue++;
994
995 /* next check if EOQ has been triggered already */
996 mode = desc_read(prev, hw_mode);
997 if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
998 (chan->state == CPDMA_STATE_ACTIVE)) {
999 desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
1000 chan_write(chan, hdp, desc_dma);
1001 chan->stats.misqueued++;
1002 }
1003}
1004
1005int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
Sebastian Siewioraef614e2013-04-23 07:31:38 +00001006 int len, int directed)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001007{
1008 struct cpdma_ctlr *ctlr = chan->ctlr;
1009 struct cpdma_desc __iomem *desc;
1010 dma_addr_t buffer;
1011 unsigned long flags;
1012 u32 mode;
1013 int ret = 0;
1014
1015 spin_lock_irqsave(&chan->lock, flags);
1016
1017 if (chan->state == CPDMA_STATE_TEARDOWN) {
1018 ret = -EINVAL;
1019 goto unlock_ret;
1020 }
1021
Grygorii Strashko742fb202016-06-27 12:05:11 +03001022 if (chan->count >= chan->desc_num) {
1023 chan->stats.desc_alloc_fail++;
1024 ret = -ENOMEM;
1025 goto unlock_ret;
1026 }
1027
1028 desc = cpdma_desc_alloc(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001029 if (!desc) {
1030 chan->stats.desc_alloc_fail++;
1031 ret = -ENOMEM;
1032 goto unlock_ret;
1033 }
1034
1035 if (len < ctlr->params.min_packet_size) {
1036 len = ctlr->params.min_packet_size;
1037 chan->stats.runt_transmit_buff++;
1038 }
1039
1040 buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
Sebastian Siewior14bd0762013-06-20 16:58:45 +02001041 ret = dma_mapping_error(ctlr->dev, buffer);
1042 if (ret) {
1043 cpdma_desc_free(ctlr->pool, desc, 1);
1044 ret = -EINVAL;
1045 goto unlock_ret;
1046 }
1047
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001048 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001049 cpdma_desc_to_port(chan, mode, directed);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001050
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001051 /* Relaxed IO accessors can be used here as there is read barrier
1052 * at the end of write sequence.
1053 */
1054 writel_relaxed(0, &desc->hw_next);
1055 writel_relaxed(buffer, &desc->hw_buffer);
1056 writel_relaxed(len, &desc->hw_len);
1057 writel_relaxed(mode | len, &desc->hw_mode);
Florian Fainellic79c3852018-05-21 11:45:55 -07001058 writel_relaxed((uintptr_t)token, &desc->sw_token);
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001059 writel_relaxed(buffer, &desc->sw_buffer);
1060 writel_relaxed(len, &desc->sw_len);
1061 desc_read(desc, sw_len);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001062
1063 __cpdma_chan_submit(chan, desc);
1064
1065 if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
1066 chan_write(chan, rxfree, 1);
1067
1068 chan->count++;
1069
1070unlock_ret:
1071 spin_unlock_irqrestore(&chan->lock, flags);
1072 return ret;
1073}
1074
Mugunthan V Nfae50822013-01-17 06:31:34 +00001075bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
1076{
Mugunthan V Nfae50822013-01-17 06:31:34 +00001077 struct cpdma_ctlr *ctlr = chan->ctlr;
1078 struct cpdma_desc_pool *pool = ctlr->pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +03001079 bool free_tx_desc;
1080 unsigned long flags;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001081
Grygorii Strashko742fb202016-06-27 12:05:11 +03001082 spin_lock_irqsave(&chan->lock, flags);
1083 free_tx_desc = (chan->count < chan->desc_num) &&
1084 gen_pool_avail(pool->gen_pool);
1085 spin_unlock_irqrestore(&chan->lock, flags);
1086 return free_tx_desc;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001087}
Mugunthan V Nfae50822013-01-17 06:31:34 +00001088
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001089static void __cpdma_chan_free(struct cpdma_chan *chan,
1090 struct cpdma_desc __iomem *desc,
1091 int outlen, int status)
1092{
1093 struct cpdma_ctlr *ctlr = chan->ctlr;
1094 struct cpdma_desc_pool *pool = ctlr->pool;
1095 dma_addr_t buff_dma;
1096 int origlen;
Florian Fainellic79c3852018-05-21 11:45:55 -07001097 uintptr_t token;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001098
Florian Fainellic79c3852018-05-21 11:45:55 -07001099 token = desc_read(desc, sw_token);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001100 buff_dma = desc_read(desc, sw_buffer);
1101 origlen = desc_read(desc, sw_len);
1102
1103 dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
1104 cpdma_desc_free(pool, desc, 1);
Florian Fainellic79c3852018-05-21 11:45:55 -07001105 (*chan->handler)((void *)token, outlen, status);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001106}
1107
1108static int __cpdma_chan_process(struct cpdma_chan *chan)
1109{
1110 struct cpdma_ctlr *ctlr = chan->ctlr;
1111 struct cpdma_desc __iomem *desc;
1112 int status, outlen;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001113 int cb_status = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001114 struct cpdma_desc_pool *pool = ctlr->pool;
1115 dma_addr_t desc_dma;
1116 unsigned long flags;
1117
1118 spin_lock_irqsave(&chan->lock, flags);
1119
1120 desc = chan->head;
1121 if (!desc) {
1122 chan->stats.empty_dequeue++;
1123 status = -ENOENT;
1124 goto unlock_ret;
1125 }
1126 desc_dma = desc_phys(pool, desc);
1127
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001128 status = desc_read(desc, hw_mode);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001129 outlen = status & 0x7ff;
1130 if (status & CPDMA_DESC_OWNER) {
1131 chan->stats.busy_dequeue++;
1132 status = -EBUSY;
1133 goto unlock_ret;
1134 }
Mugunthan V N28a19fe2013-05-29 20:22:01 +00001135
1136 if (status & CPDMA_DESC_PASS_CRC)
1137 outlen -= CPDMA_DESC_CRC_LEN;
1138
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001139 status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
Grygorii Strashkoa3a41d22018-03-15 15:15:50 -05001140 CPDMA_DESC_PORT_MASK | CPDMA_RX_VLAN_ENCAP);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001141
1142 chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
1143 chan_write(chan, cp, desc_dma);
1144 chan->count--;
1145 chan->stats.good_dequeue++;
1146
Grygorii Strashko12a303e2017-01-06 14:07:30 -06001147 if ((status & CPDMA_DESC_EOQ) && chan->head) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001148 chan->stats.requeue++;
1149 chan_write(chan, hdp, desc_phys(pool, chan->head));
1150 }
1151
1152 spin_unlock_irqrestore(&chan->lock, flags);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001153 if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
1154 cb_status = -ENOSYS;
1155 else
1156 cb_status = status;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001157
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001158 __cpdma_chan_free(chan, desc, outlen, cb_status);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001159 return status;
1160
1161unlock_ret:
1162 spin_unlock_irqrestore(&chan->lock, flags);
1163 return status;
1164}
1165
1166int cpdma_chan_process(struct cpdma_chan *chan, int quota)
1167{
1168 int used = 0, ret = 0;
1169
1170 if (chan->state != CPDMA_STATE_ACTIVE)
1171 return -EINVAL;
1172
1173 while (used < quota) {
1174 ret = __cpdma_chan_process(chan);
1175 if (ret < 0)
1176 break;
1177 used++;
1178 }
1179 return used;
1180}
1181
1182int cpdma_chan_start(struct cpdma_chan *chan)
1183{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001184 struct cpdma_ctlr *ctlr = chan->ctlr;
1185 unsigned long flags;
1186 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001187
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001188 spin_lock_irqsave(&ctlr->lock, flags);
1189 ret = cpdma_chan_set_chan_shaper(chan);
1190 spin_unlock_irqrestore(&ctlr->lock, flags);
1191 if (ret)
1192 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001193
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001194 ret = cpdma_chan_on(chan);
1195 if (ret)
1196 return ret;
1197
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001198 return 0;
1199}
1200
1201int cpdma_chan_stop(struct cpdma_chan *chan)
1202{
1203 struct cpdma_ctlr *ctlr = chan->ctlr;
1204 struct cpdma_desc_pool *pool = ctlr->pool;
1205 unsigned long flags;
1206 int ret;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001207 unsigned timeout;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001208
1209 spin_lock_irqsave(&chan->lock, flags);
Christian Rieschcd11cf52014-03-24 13:46:27 +01001210 if (chan->state == CPDMA_STATE_TEARDOWN) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001211 spin_unlock_irqrestore(&chan->lock, flags);
1212 return -EINVAL;
1213 }
1214
1215 chan->state = CPDMA_STATE_TEARDOWN;
1216 dma_reg_write(ctlr, chan->int_clear, chan->mask);
1217
1218 /* trigger teardown */
Christian Rieschb4ad0422012-02-22 21:58:00 +00001219 dma_reg_write(ctlr, chan->td, chan_linear(chan));
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001220
1221 /* wait for teardown complete */
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001222 timeout = 100 * 100; /* 100 ms */
1223 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001224 u32 cp = chan_read(chan, cp);
1225 if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
1226 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001227 udelay(10);
1228 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001229 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001230 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001231 chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
1232
1233 /* handle completed packets */
Ilya Yanok7746ab02011-12-18 10:02:04 +00001234 spin_unlock_irqrestore(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001235 do {
1236 ret = __cpdma_chan_process(chan);
1237 if (ret < 0)
1238 break;
1239 } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
Ilya Yanok7746ab02011-12-18 10:02:04 +00001240 spin_lock_irqsave(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001241
1242 /* remaining packets haven't been tx/rx'ed, clean them up */
1243 while (chan->head) {
1244 struct cpdma_desc __iomem *desc = chan->head;
1245 dma_addr_t next_dma;
1246
1247 next_dma = desc_read(desc, hw_next);
1248 chan->head = desc_from_phys(pool, next_dma);
htbeginffb5ba92012-10-01 16:42:43 +00001249 chan->count--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001250 chan->stats.teardown_dequeue++;
1251
1252 /* issue callback without locks held */
1253 spin_unlock_irqrestore(&chan->lock, flags);
1254 __cpdma_chan_free(chan, desc, 0, -ENOSYS);
1255 spin_lock_irqsave(&chan->lock, flags);
1256 }
1257
1258 chan->state = CPDMA_STATE_IDLE;
1259 spin_unlock_irqrestore(&chan->lock, flags);
1260 return 0;
1261}
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001262
1263int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
1264{
1265 unsigned long flags;
1266
1267 spin_lock_irqsave(&chan->lock, flags);
1268 if (chan->state != CPDMA_STATE_ACTIVE) {
1269 spin_unlock_irqrestore(&chan->lock, flags);
1270 return -EINVAL;
1271 }
1272
1273 dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
1274 chan->mask);
1275 spin_unlock_irqrestore(&chan->lock, flags);
1276
1277 return 0;
1278}
1279
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001280int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
1281{
1282 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001283 int ret;
1284
1285 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001286 ret = _cpdma_control_get(ctlr, control);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001287 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001288
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001289 return ret;
1290}
1291
1292int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
1293{
1294 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001295 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001296
1297 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +02001298 ret = _cpdma_control_set(ctlr, control, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001299 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001300
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001301 return ret;
1302}
Sebastian Siewior4bc21d42013-04-24 08:48:22 +00001303
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06001304int cpdma_get_num_rx_descs(struct cpdma_ctlr *ctlr)
1305{
1306 return ctlr->num_rx_desc;
1307}
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06001308
1309int cpdma_get_num_tx_descs(struct cpdma_ctlr *ctlr)
1310{
1311 return ctlr->num_tx_desc;
1312}
Grygorii Strashkobe034fc2017-01-06 14:07:34 -06001313
1314void cpdma_set_num_rx_descs(struct cpdma_ctlr *ctlr, int num_rx_desc)
1315{
1316 ctlr->num_rx_desc = num_rx_desc;
1317 ctlr->num_tx_desc = ctlr->pool->num_desc - ctlr->num_rx_desc;
1318}