blob: 65e2f124d62a4cbba2234a77b557e918e7bb1721 [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);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300198}
199
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400200/*
201 * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci
202 * emac) have dedicated on-chip memory for these descriptors. Some other
203 * devices (e.g. cpsw switches) use plain old memory. Descriptor pools
204 * abstract out these details
205 */
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600206int cpdma_desc_pool_create(struct cpdma_ctlr *ctlr)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400207{
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600208 struct cpdma_params *cpdma_params = &ctlr->params;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400209 struct cpdma_desc_pool *pool;
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600210 int ret = -ENOMEM;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400211
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600212 pool = devm_kzalloc(ctlr->dev, sizeof(*pool), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400213 if (!pool)
Grygorii Strashko742fb202016-06-27 12:05:11 +0300214 goto gen_pool_create_fail;
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600215 ctlr->pool = pool;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400216
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600217 pool->mem_size = cpdma_params->desc_mem_size;
218 pool->desc_size = ALIGN(sizeof(struct cpdma_desc),
219 cpdma_params->desc_align);
220 pool->num_desc = pool->mem_size / pool->desc_size;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400221
Grygorii Strashko90225bf2017-01-06 14:07:33 -0600222 if (cpdma_params->descs_pool_size) {
223 /* recalculate memory size required cpdma descriptor pool
224 * basing on number of descriptors specified by user and
225 * if memory size > CPPI internal RAM size (desc_mem_size)
226 * then switch to use DDR
227 */
228 pool->num_desc = cpdma_params->descs_pool_size;
229 pool->mem_size = pool->desc_size * pool->num_desc;
230 if (pool->mem_size > cpdma_params->desc_mem_size)
231 cpdma_params->desc_mem_phys = 0;
232 }
233
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600234 pool->gen_pool = devm_gen_pool_create(ctlr->dev, ilog2(pool->desc_size),
235 -1, "cpdma");
Grygorii Strashko742fb202016-06-27 12:05:11 +0300236 if (IS_ERR(pool->gen_pool)) {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600237 ret = PTR_ERR(pool->gen_pool);
238 dev_err(ctlr->dev, "pool create failed %d\n", ret);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300239 goto gen_pool_create_fail;
240 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400241
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600242 if (cpdma_params->desc_mem_phys) {
243 pool->phys = cpdma_params->desc_mem_phys;
Grygorii Strashko7f3b4902017-01-06 14:07:32 -0600244 pool->iomap = devm_ioremap(ctlr->dev, pool->phys,
245 pool->mem_size);
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600246 pool->hw_addr = cpdma_params->desc_hw_addr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400247 } else {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600248 pool->cpumap = dma_alloc_coherent(ctlr->dev, pool->mem_size,
249 &pool->hw_addr, GFP_KERNEL);
Arnd Bergmann84092992016-01-29 12:39:10 +0100250 pool->iomap = (void __iomem __force *)pool->cpumap;
251 pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400252 }
253
Grygorii Strashko742fb202016-06-27 12:05:11 +0300254 if (!pool->iomap)
255 goto gen_pool_create_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400256
Grygorii Strashko742fb202016-06-27 12:05:11 +0300257 ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap,
258 pool->phys, pool->mem_size, -1);
259 if (ret < 0) {
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600260 dev_err(ctlr->dev, "pool add failed %d\n", ret);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300261 goto gen_pool_add_virt_fail;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400262 }
Grygorii Strashko742fb202016-06-27 12:05:11 +0300263
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600264 return 0;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300265
266gen_pool_add_virt_fail:
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600267 cpdma_desc_pool_destroy(ctlr);
Grygorii Strashko742fb202016-06-27 12:05:11 +0300268gen_pool_create_fail:
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600269 ctlr->pool = NULL;
270 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400271}
272
273static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool,
274 struct cpdma_desc __iomem *desc)
275{
276 if (!desc)
277 return 0;
Olof Johanssonc767db52013-12-11 15:51:20 -0800278 return pool->hw_addr + (__force long)desc - (__force long)pool->iomap;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400279}
280
281static inline struct cpdma_desc __iomem *
282desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma)
283{
Sriram6a1fef62011-03-22 02:31:03 +0000284 return dma ? pool->iomap + dma - pool->hw_addr : NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400285}
286
287static struct cpdma_desc __iomem *
Grygorii Strashko742fb202016-06-27 12:05:11 +0300288cpdma_desc_alloc(struct cpdma_desc_pool *pool)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400289{
Grygorii Strashkoaeec3022016-08-04 18:20:51 +0300290 return (struct cpdma_desc __iomem *)
291 gen_pool_alloc(pool->gen_pool, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400292}
293
294static void cpdma_desc_free(struct cpdma_desc_pool *pool,
295 struct cpdma_desc __iomem *desc, int num_desc)
296{
Grygorii Strashko742fb202016-06-27 12:05:11 +0300297 gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400298}
299
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200300static int _cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
301{
302 struct cpdma_control_info *info = &controls[control];
303 u32 val;
304
305 if (!ctlr->params.has_ext_regs)
306 return -ENOTSUPP;
307
308 if (ctlr->state != CPDMA_STATE_ACTIVE)
309 return -EINVAL;
310
311 if (control < 0 || control >= ARRAY_SIZE(controls))
312 return -ENOENT;
313
314 if ((info->access & ACCESS_WO) != ACCESS_WO)
315 return -EPERM;
316
317 val = dma_reg_read(ctlr, info->reg);
318 val &= ~(info->mask << info->shift);
319 val |= (value & info->mask) << info->shift;
320 dma_reg_write(ctlr, info->reg, val);
321
322 return 0;
323}
324
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200325static int _cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
326{
327 struct cpdma_control_info *info = &controls[control];
328 int ret;
329
330 if (!ctlr->params.has_ext_regs)
331 return -ENOTSUPP;
332
333 if (ctlr->state != CPDMA_STATE_ACTIVE)
334 return -EINVAL;
335
336 if (control < 0 || control >= ARRAY_SIZE(controls))
337 return -ENOENT;
338
339 if ((info->access & ACCESS_RO) != ACCESS_RO)
340 return -EPERM;
341
342 ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask;
343 return ret;
344}
345
346/* cpdma_chan_set_chan_shaper - set shaper for a channel
347 * Has to be called under ctlr lock
348 */
349static int cpdma_chan_set_chan_shaper(struct cpdma_chan *chan)
350{
351 struct cpdma_ctlr *ctlr = chan->ctlr;
352 u32 rate_reg;
353 u32 rmask;
354 int ret;
355
356 if (!chan->rate)
357 return 0;
358
359 rate_reg = CPDMA_TX_PRI0_RATE + 4 * chan->chan_num;
360 dma_reg_write(ctlr, rate_reg, chan->rate_factor);
361
362 rmask = _cpdma_control_get(ctlr, CPDMA_TX_RLIM);
363 rmask |= chan->mask;
364
365 ret = _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
366 return ret;
367}
368
369static int cpdma_chan_on(struct cpdma_chan *chan)
370{
371 struct cpdma_ctlr *ctlr = chan->ctlr;
372 struct cpdma_desc_pool *pool = ctlr->pool;
373 unsigned long flags;
374
375 spin_lock_irqsave(&chan->lock, flags);
376 if (chan->state != CPDMA_STATE_IDLE) {
377 spin_unlock_irqrestore(&chan->lock, flags);
378 return -EBUSY;
379 }
380 if (ctlr->state != CPDMA_STATE_ACTIVE) {
381 spin_unlock_irqrestore(&chan->lock, flags);
382 return -EINVAL;
383 }
384 dma_reg_write(ctlr, chan->int_set, chan->mask);
385 chan->state = CPDMA_STATE_ACTIVE;
386 if (chan->head) {
387 chan_write(chan, hdp, desc_phys(pool, chan->head));
388 if (chan->rxfree)
389 chan_write(chan, rxfree, chan->count);
390 }
391
392 spin_unlock_irqrestore(&chan->lock, flags);
393 return 0;
394}
395
396/* cpdma_chan_fit_rate - set rate for a channel and check if it's possible.
397 * rmask - mask of rate limited channels
398 * Returns min rate in Kb/s
399 */
400static int cpdma_chan_fit_rate(struct cpdma_chan *ch, u32 rate,
401 u32 *rmask, int *prio_mode)
402{
403 struct cpdma_ctlr *ctlr = ch->ctlr;
404 struct cpdma_chan *chan;
405 u32 old_rate = ch->rate;
406 u32 new_rmask = 0;
407 int rlim = 1;
408 int i;
409
410 *prio_mode = 0;
411 for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) {
412 chan = ctlr->channels[i];
413 if (!chan) {
414 rlim = 0;
415 continue;
416 }
417
418 if (chan == ch)
419 chan->rate = rate;
420
421 if (chan->rate) {
422 if (rlim) {
423 new_rmask |= chan->mask;
424 } else {
425 ch->rate = old_rate;
426 dev_err(ctlr->dev, "Prev channel of %dch is not rate limited\n",
427 chan->chan_num);
428 return -EINVAL;
429 }
430 } else {
431 *prio_mode = 1;
432 rlim = 0;
433 }
434 }
435
436 *rmask = new_rmask;
437 return 0;
438}
439
440static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr,
441 struct cpdma_chan *ch)
442{
443 u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX;
444 u32 best_send_cnt = 0, best_idle_cnt = 0;
445 u32 new_rate, best_rate = 0, rate_reg;
446 u64 send_cnt, idle_cnt;
447 u32 min_send_cnt, freq;
448 u64 divident, divisor;
449
450 if (!ch->rate) {
451 ch->rate_factor = 0;
452 goto set_factor;
453 }
454
455 freq = ctlr->params.bus_freq_mhz * 1000 * 32;
456 if (!freq) {
457 dev_err(ctlr->dev, "The bus frequency is not set\n");
458 return -EINVAL;
459 }
460
461 min_send_cnt = freq - ch->rate;
462 send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate);
463 while (send_cnt <= CPDMA_MAX_RLIM_CNT) {
464 divident = ch->rate * send_cnt;
465 divisor = min_send_cnt;
466 idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor);
467
468 divident = freq * idle_cnt;
469 divisor = idle_cnt + send_cnt;
470 new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor);
471
472 delta = new_rate >= ch->rate ? new_rate - ch->rate : delta;
473 if (delta < best_delta) {
474 best_delta = delta;
475 best_send_cnt = send_cnt;
476 best_idle_cnt = idle_cnt;
477 best_rate = new_rate;
478
479 if (!delta)
480 break;
481 }
482
483 if (prev_delta >= delta) {
484 prev_delta = delta;
485 send_cnt++;
486 continue;
487 }
488
489 idle_cnt++;
490 divident = freq * idle_cnt;
491 send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate);
492 send_cnt -= idle_cnt;
493 prev_delta = UINT_MAX;
494 }
495
496 ch->rate = best_rate;
497 ch->rate_factor = best_send_cnt | (best_idle_cnt << 16);
498
499set_factor:
500 rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num;
501 dma_reg_write(ctlr, rate_reg, ch->rate_factor);
502 return 0;
503}
504
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400505struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params)
506{
507 struct cpdma_ctlr *ctlr;
508
George Cheriane1943122014-05-12 10:21:21 +0530509 ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400510 if (!ctlr)
511 return NULL;
512
513 ctlr->state = CPDMA_STATE_IDLE;
514 ctlr->params = *params;
515 ctlr->dev = params->dev;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300516 ctlr->chan_num = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400517 spin_lock_init(&ctlr->lock);
518
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600519 if (cpdma_desc_pool_create(ctlr))
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400520 return NULL;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400521
522 if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS))
523 ctlr->num_chan = CPDMA_MAX_CHANNELS;
524 return ctlr;
525}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000526EXPORT_SYMBOL_GPL(cpdma_ctlr_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400527
528int cpdma_ctlr_start(struct cpdma_ctlr *ctlr)
529{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200530 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400531 unsigned long flags;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200532 int i, prio_mode;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400533
534 spin_lock_irqsave(&ctlr->lock, flags);
535 if (ctlr->state != CPDMA_STATE_IDLE) {
536 spin_unlock_irqrestore(&ctlr->lock, flags);
537 return -EBUSY;
538 }
539
540 if (ctlr->params.has_soft_reset) {
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000541 unsigned timeout = 10 * 100;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400542
543 dma_reg_write(ctlr, CPDMA_SOFTRESET, 1);
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000544 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400545 if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0)
546 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000547 udelay(10);
548 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400549 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +0000550 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400551 }
552
553 for (i = 0; i < ctlr->num_chan; i++) {
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -0600554 writel(0, ctlr->params.txhdp + 4 * i);
555 writel(0, ctlr->params.rxhdp + 4 * i);
556 writel(0, ctlr->params.txcp + 4 * i);
557 writel(0, ctlr->params.rxcp + 4 * i);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400558 }
559
560 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
561 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
562
563 dma_reg_write(ctlr, CPDMA_TXCONTROL, 1);
564 dma_reg_write(ctlr, CPDMA_RXCONTROL, 1);
565
566 ctlr->state = CPDMA_STATE_ACTIVE;
567
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200568 prio_mode = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400569 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200570 chan = ctlr->channels[i];
571 if (chan) {
572 cpdma_chan_set_chan_shaper(chan);
573 cpdma_chan_on(chan);
574
575 /* off prio mode if all tx channels are rate limited */
576 if (is_tx_chan(chan) && !chan->rate)
577 prio_mode = 1;
578 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400579 }
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200580
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200581 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +0200582 _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0);
583
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400584 spin_unlock_irqrestore(&ctlr->lock, flags);
585 return 0;
586}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000587EXPORT_SYMBOL_GPL(cpdma_ctlr_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400588
589int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr)
590{
591 unsigned long flags;
592 int i;
593
594 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhukb993eec2016-11-11 16:10:47 +0200595 if (ctlr->state != CPDMA_STATE_ACTIVE) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400596 spin_unlock_irqrestore(&ctlr->lock, flags);
597 return -EINVAL;
598 }
599
600 ctlr->state = CPDMA_STATE_TEARDOWN;
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300601 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400602
603 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
604 if (ctlr->channels[i])
605 cpdma_chan_stop(ctlr->channels[i]);
606 }
607
Ivan Khoronzhuk080d5c5a2016-08-22 21:18:25 +0300608 spin_lock_irqsave(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400609 dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff);
610 dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff);
611
612 dma_reg_write(ctlr, CPDMA_TXCONTROL, 0);
613 dma_reg_write(ctlr, CPDMA_RXCONTROL, 0);
614
615 ctlr->state = CPDMA_STATE_IDLE;
616
617 spin_unlock_irqrestore(&ctlr->lock, flags);
618 return 0;
619}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000620EXPORT_SYMBOL_GPL(cpdma_ctlr_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400621
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400622int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr)
623{
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400624 int ret = 0, i;
625
626 if (!ctlr)
627 return -EINVAL;
628
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400629 if (ctlr->state != CPDMA_STATE_IDLE)
630 cpdma_ctlr_stop(ctlr);
631
Cyril Roelandt79876e02013-02-12 12:52:30 +0000632 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++)
633 cpdma_chan_destroy(ctlr->channels[i]);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400634
Grygorii Strashko5fcc40a2017-01-06 14:07:31 -0600635 cpdma_desc_pool_destroy(ctlr);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400636 return ret;
637}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000638EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400639
640int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable)
641{
642 unsigned long flags;
643 int i, reg;
644
645 spin_lock_irqsave(&ctlr->lock, flags);
646 if (ctlr->state != CPDMA_STATE_ACTIVE) {
647 spin_unlock_irqrestore(&ctlr->lock, flags);
648 return -EINVAL;
649 }
650
651 reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR;
652 dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR);
653
654 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
655 if (ctlr->channels[i])
656 cpdma_chan_int_ctrl(ctlr->channels[i], enable);
657 }
658
659 spin_unlock_irqrestore(&ctlr->lock, flags);
660 return 0;
661}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100662EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400663
Mugunthan V N510a1e722013-02-17 22:19:20 +0000664void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400665{
Mugunthan V N510a1e722013-02-17 22:19:20 +0000666 dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400667}
Arnd Bergmann6929e242013-02-14 17:53:01 +0100668EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400669
Ivan Khoronzhuke05107e2016-08-22 21:18:26 +0300670u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr)
671{
672 return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED);
673}
674EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state);
675
676u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr)
677{
678 return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED);
679}
680EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state);
681
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200682static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr,
683 int rx, int desc_num,
684 int per_ch_desc)
685{
686 struct cpdma_chan *chan, *most_chan = NULL;
687 int desc_cnt = desc_num;
688 int most_dnum = 0;
689 int min, max, i;
690
691 if (!desc_num)
692 return;
693
694 if (rx) {
695 min = rx_chan_num(0);
696 max = rx_chan_num(CPDMA_MAX_CHANNELS);
697 } else {
698 min = tx_chan_num(0);
699 max = tx_chan_num(CPDMA_MAX_CHANNELS);
700 }
701
702 for (i = min; i < max; i++) {
703 chan = ctlr->channels[i];
704 if (!chan)
705 continue;
706
707 if (chan->weight)
708 chan->desc_num = (chan->weight * desc_num) / 100;
709 else
710 chan->desc_num = per_ch_desc;
711
712 desc_cnt -= chan->desc_num;
713
714 if (most_dnum < chan->desc_num) {
715 most_dnum = chan->desc_num;
716 most_chan = chan;
717 }
718 }
719 /* use remains */
720 most_chan->desc_num += desc_cnt;
721}
722
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300723/**
724 * cpdma_chan_split_pool - Splits ctrl pool between all channels.
725 * Has to be called under ctlr lock
726 */
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200727static int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr)
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300728{
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200729 int tx_per_ch_desc = 0, rx_per_ch_desc = 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300730 struct cpdma_desc_pool *pool = ctlr->pool;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200731 int free_rx_num = 0, free_tx_num = 0;
732 int rx_weight = 0, tx_weight = 0;
733 int tx_desc_num, rx_desc_num;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300734 struct cpdma_chan *chan;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200735 int i, tx_num = 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300736
737 if (!ctlr->chan_num)
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200738 return 0;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300739
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300740 for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) {
741 chan = ctlr->channels[i];
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200742 if (!chan)
743 continue;
744
745 if (is_rx_chan(chan)) {
746 if (!chan->weight)
747 free_rx_num++;
748 rx_weight += chan->weight;
749 } else {
750 if (!chan->weight)
751 free_tx_num++;
752 tx_weight += chan->weight;
753 tx_num++;
754 }
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300755 }
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200756
757 if (rx_weight > 100 || tx_weight > 100)
758 return -EINVAL;
759
760 tx_desc_num = (tx_num * pool->num_desc) / ctlr->chan_num;
761 rx_desc_num = pool->num_desc - tx_desc_num;
762
763 if (free_tx_num) {
764 tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100;
765 tx_per_ch_desc /= free_tx_num;
766 }
767 if (free_rx_num) {
768 rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100;
769 rx_per_ch_desc /= free_rx_num;
770 }
771
772 cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc);
773 cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc);
774
775 return 0;
776}
777
778/* cpdma_chan_set_weight - set weight of a channel in percentage.
779 * Tx and Rx channels have separate weights. That is 100% for RX
780 * and 100% for Tx. The weight is used to split cpdma resources
781 * in correct proportion required by the channels, including number
782 * of descriptors. The channel rate is not enough to know the
783 * weight of a channel as the maximum rate of an interface is needed.
784 * If weight = 0, then channel uses rest of descriptors leaved by
785 * weighted channels.
786 */
787int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight)
788{
789 struct cpdma_ctlr *ctlr = ch->ctlr;
790 unsigned long flags, ch_flags;
791 int ret;
792
793 spin_lock_irqsave(&ctlr->lock, flags);
794 spin_lock_irqsave(&ch->lock, ch_flags);
795 if (ch->weight == weight) {
796 spin_unlock_irqrestore(&ch->lock, ch_flags);
797 spin_unlock_irqrestore(&ctlr->lock, flags);
798 return 0;
799 }
800 ch->weight = weight;
801 spin_unlock_irqrestore(&ch->lock, ch_flags);
802
803 /* re-split pool using new channel weight */
804 ret = cpdma_chan_split_pool(ctlr);
805 spin_unlock_irqrestore(&ctlr->lock, flags);
806 return ret;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300807}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500808EXPORT_SYMBOL_GPL(cpdma_chan_set_weight);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300809
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200810/* cpdma_chan_get_min_rate - get minimum allowed rate for channel
811 * Should be called before cpdma_chan_set_rate.
812 * Returns min rate in Kb/s
813 */
814u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr)
815{
816 unsigned int divident, divisor;
817
818 divident = ctlr->params.bus_freq_mhz * 32 * 1000;
819 divisor = 1 + CPDMA_MAX_RLIM_CNT;
820
821 return DIV_ROUND_UP(divident, divisor);
822}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500823EXPORT_SYMBOL_GPL(cpdma_chan_get_min_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200824
825/* cpdma_chan_set_rate - limits bandwidth for transmit channel.
826 * The bandwidth * limited channels have to be in order beginning from lowest.
827 * ch - transmit channel the bandwidth is configured for
828 * rate - bandwidth in Kb/s, if 0 - then off shaper
829 */
830int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate)
831{
832 struct cpdma_ctlr *ctlr = ch->ctlr;
833 unsigned long flags, ch_flags;
834 int ret, prio_mode;
835 u32 rmask;
836
837 if (!ch || !is_tx_chan(ch))
838 return -EINVAL;
839
840 if (ch->rate == rate)
841 return rate;
842
843 spin_lock_irqsave(&ctlr->lock, flags);
844 spin_lock_irqsave(&ch->lock, ch_flags);
845
846 ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode);
847 if (ret)
848 goto err;
849
850 ret = cpdma_chan_set_factors(ctlr, ch);
851 if (ret)
852 goto err;
853
854 spin_unlock_irqrestore(&ch->lock, ch_flags);
855
856 /* on shapers */
857 _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask);
858 _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode);
859 spin_unlock_irqrestore(&ctlr->lock, flags);
860 return ret;
861
862err:
863 spin_unlock_irqrestore(&ch->lock, ch_flags);
864 spin_unlock_irqrestore(&ctlr->lock, flags);
865 return ret;
866}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500867EXPORT_SYMBOL_GPL(cpdma_chan_set_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200868
869u32 cpdma_chan_get_rate(struct cpdma_chan *ch)
870{
871 unsigned long flags;
872 u32 rate;
873
874 spin_lock_irqsave(&ch->lock, flags);
875 rate = ch->rate;
876 spin_unlock_irqrestore(&ch->lock, flags);
877
878 return rate;
879}
Paul Gortmaker397c5ad2016-12-01 15:25:28 -0500880EXPORT_SYMBOL_GPL(cpdma_chan_get_rate);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200881
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400882struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num,
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300883 cpdma_handler_fn handler, int rx_type)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400884{
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300885 int offset = chan_num * 4;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400886 struct cpdma_chan *chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400887 unsigned long flags;
888
Ivan Khoronzhuk925d65e2016-08-22 21:18:27 +0300889 chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num);
890
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400891 if (__chan_linear(chan_num) >= ctlr->num_chan)
892 return NULL;
893
George Cheriane1943122014-05-12 10:21:21 +0530894 chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400895 if (!chan)
George Cheriane1943122014-05-12 10:21:21 +0530896 return ERR_PTR(-ENOMEM);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400897
898 spin_lock_irqsave(&ctlr->lock, flags);
George Cheriane1943122014-05-12 10:21:21 +0530899 if (ctlr->channels[chan_num]) {
900 spin_unlock_irqrestore(&ctlr->lock, flags);
901 devm_kfree(ctlr->dev, chan);
902 return ERR_PTR(-EBUSY);
903 }
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400904
905 chan->ctlr = ctlr;
906 chan->state = CPDMA_STATE_IDLE;
907 chan->chan_num = chan_num;
908 chan->handler = handler;
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +0200909 chan->rate = 0;
Grygorii Strashko742fb202016-06-27 12:05:11 +0300910 chan->desc_num = ctlr->pool->num_desc / 2;
Ivan Khoronzhuk0fc64322016-11-29 17:00:47 +0200911 chan->weight = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400912
913 if (is_rx_chan(chan)) {
914 chan->hdp = ctlr->params.rxhdp + offset;
915 chan->cp = ctlr->params.rxcp + offset;
916 chan->rxfree = ctlr->params.rxfree + offset;
917 chan->int_set = CPDMA_RXINTMASKSET;
918 chan->int_clear = CPDMA_RXINTMASKCLEAR;
919 chan->td = CPDMA_RXTEARDOWN;
920 chan->dir = DMA_FROM_DEVICE;
921 } else {
922 chan->hdp = ctlr->params.txhdp + offset;
923 chan->cp = ctlr->params.txcp + offset;
924 chan->int_set = CPDMA_TXINTMASKSET;
925 chan->int_clear = CPDMA_TXINTMASKCLEAR;
926 chan->td = CPDMA_TXTEARDOWN;
927 chan->dir = DMA_TO_DEVICE;
928 }
929 chan->mask = BIT(chan_linear(chan));
930
931 spin_lock_init(&chan->lock);
932
933 ctlr->channels[chan_num] = chan;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300934 ctlr->chan_num++;
935
936 cpdma_chan_split_pool(ctlr);
937
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400938 spin_unlock_irqrestore(&ctlr->lock, flags);
939 return chan;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400940}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000941EXPORT_SYMBOL_GPL(cpdma_chan_create);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400942
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300943int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan)
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300944{
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300945 unsigned long flags;
946 int desc_num;
947
948 spin_lock_irqsave(&chan->lock, flags);
949 desc_num = chan->desc_num;
950 spin_unlock_irqrestore(&chan->lock, flags);
951
952 return desc_num;
Ivan Khoronzhuk17933312016-06-17 13:25:39 +0300953}
954EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num);
955
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400956int cpdma_chan_destroy(struct cpdma_chan *chan)
957{
Julia Lawallf37c54b2012-08-14 05:49:47 +0000958 struct cpdma_ctlr *ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400959 unsigned long flags;
960
961 if (!chan)
962 return -EINVAL;
Julia Lawallf37c54b2012-08-14 05:49:47 +0000963 ctlr = chan->ctlr;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400964
965 spin_lock_irqsave(&ctlr->lock, flags);
966 if (chan->state != CPDMA_STATE_IDLE)
967 cpdma_chan_stop(chan);
968 ctlr->channels[chan->chan_num] = NULL;
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300969 ctlr->chan_num--;
Ivan Khoronzhukb602e492016-11-08 15:16:05 +0200970 devm_kfree(ctlr->dev, chan);
Ivan Khoronzhuk3802dce12016-08-22 21:18:24 +0300971 cpdma_chan_split_pool(ctlr);
972
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400973 spin_unlock_irqrestore(&ctlr->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400974 return 0;
975}
Arnd Bergmann32a6d902012-04-20 10:56:09 +0000976EXPORT_SYMBOL_GPL(cpdma_chan_destroy);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400977
978int cpdma_chan_get_stats(struct cpdma_chan *chan,
979 struct cpdma_chan_stats *stats)
980{
981 unsigned long flags;
982 if (!chan)
983 return -EINVAL;
984 spin_lock_irqsave(&chan->lock, flags);
985 memcpy(stats, &chan->stats, sizeof(*stats));
986 spin_unlock_irqrestore(&chan->lock, flags);
987 return 0;
988}
Daniel Mack0ca04b62013-08-22 13:47:00 +0200989EXPORT_SYMBOL_GPL(cpdma_chan_get_stats);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400990
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -0400991static void __cpdma_chan_submit(struct cpdma_chan *chan,
992 struct cpdma_desc __iomem *desc)
993{
994 struct cpdma_ctlr *ctlr = chan->ctlr;
995 struct cpdma_desc __iomem *prev = chan->tail;
996 struct cpdma_desc_pool *pool = ctlr->pool;
997 dma_addr_t desc_dma;
998 u32 mode;
999
1000 desc_dma = desc_phys(pool, desc);
1001
1002 /* simple case - idle channel */
1003 if (!chan->head) {
1004 chan->stats.head_enqueue++;
1005 chan->head = desc;
1006 chan->tail = desc;
1007 if (chan->state == CPDMA_STATE_ACTIVE)
1008 chan_write(chan, hdp, desc_dma);
1009 return;
1010 }
1011
1012 /* first chain the descriptor at the tail of the list */
1013 desc_write(prev, hw_next, desc_dma);
1014 chan->tail = desc;
1015 chan->stats.tail_enqueue++;
1016
1017 /* next check if EOQ has been triggered already */
1018 mode = desc_read(prev, hw_mode);
1019 if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) &&
1020 (chan->state == CPDMA_STATE_ACTIVE)) {
1021 desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ);
1022 chan_write(chan, hdp, desc_dma);
1023 chan->stats.misqueued++;
1024 }
1025}
1026
1027int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data,
Sebastian Siewioraef614e2013-04-23 07:31:38 +00001028 int len, int directed)
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001029{
1030 struct cpdma_ctlr *ctlr = chan->ctlr;
1031 struct cpdma_desc __iomem *desc;
1032 dma_addr_t buffer;
1033 unsigned long flags;
1034 u32 mode;
1035 int ret = 0;
1036
1037 spin_lock_irqsave(&chan->lock, flags);
1038
1039 if (chan->state == CPDMA_STATE_TEARDOWN) {
1040 ret = -EINVAL;
1041 goto unlock_ret;
1042 }
1043
Grygorii Strashko742fb202016-06-27 12:05:11 +03001044 if (chan->count >= chan->desc_num) {
1045 chan->stats.desc_alloc_fail++;
1046 ret = -ENOMEM;
1047 goto unlock_ret;
1048 }
1049
1050 desc = cpdma_desc_alloc(ctlr->pool);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001051 if (!desc) {
1052 chan->stats.desc_alloc_fail++;
1053 ret = -ENOMEM;
1054 goto unlock_ret;
1055 }
1056
1057 if (len < ctlr->params.min_packet_size) {
1058 len = ctlr->params.min_packet_size;
1059 chan->stats.runt_transmit_buff++;
1060 }
1061
1062 buffer = dma_map_single(ctlr->dev, data, len, chan->dir);
Sebastian Siewior14bd0762013-06-20 16:58:45 +02001063 ret = dma_mapping_error(ctlr->dev, buffer);
1064 if (ret) {
1065 cpdma_desc_free(ctlr->pool, desc, 1);
1066 ret = -EINVAL;
1067 goto unlock_ret;
1068 }
1069
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001070 mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP;
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001071 cpdma_desc_to_port(chan, mode, directed);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001072
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001073 /* Relaxed IO accessors can be used here as there is read barrier
1074 * at the end of write sequence.
1075 */
1076 writel_relaxed(0, &desc->hw_next);
1077 writel_relaxed(buffer, &desc->hw_buffer);
1078 writel_relaxed(len, &desc->hw_len);
1079 writel_relaxed(mode | len, &desc->hw_mode);
1080 writel_relaxed(token, &desc->sw_token);
1081 writel_relaxed(buffer, &desc->sw_buffer);
1082 writel_relaxed(len, &desc->sw_len);
1083 desc_read(desc, sw_len);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001084
1085 __cpdma_chan_submit(chan, desc);
1086
1087 if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree)
1088 chan_write(chan, rxfree, 1);
1089
1090 chan->count++;
1091
1092unlock_ret:
1093 spin_unlock_irqrestore(&chan->lock, flags);
1094 return ret;
1095}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001096EXPORT_SYMBOL_GPL(cpdma_chan_submit);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001097
Mugunthan V Nfae50822013-01-17 06:31:34 +00001098bool cpdma_check_free_tx_desc(struct cpdma_chan *chan)
1099{
Mugunthan V Nfae50822013-01-17 06:31:34 +00001100 struct cpdma_ctlr *ctlr = chan->ctlr;
1101 struct cpdma_desc_pool *pool = ctlr->pool;
Grygorii Strashko742fb202016-06-27 12:05:11 +03001102 bool free_tx_desc;
1103 unsigned long flags;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001104
Grygorii Strashko742fb202016-06-27 12:05:11 +03001105 spin_lock_irqsave(&chan->lock, flags);
1106 free_tx_desc = (chan->count < chan->desc_num) &&
1107 gen_pool_avail(pool->gen_pool);
1108 spin_unlock_irqrestore(&chan->lock, flags);
1109 return free_tx_desc;
Mugunthan V Nfae50822013-01-17 06:31:34 +00001110}
1111EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc);
1112
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001113static void __cpdma_chan_free(struct cpdma_chan *chan,
1114 struct cpdma_desc __iomem *desc,
1115 int outlen, int status)
1116{
1117 struct cpdma_ctlr *ctlr = chan->ctlr;
1118 struct cpdma_desc_pool *pool = ctlr->pool;
1119 dma_addr_t buff_dma;
1120 int origlen;
1121 void *token;
1122
1123 token = (void *)desc_read(desc, sw_token);
1124 buff_dma = desc_read(desc, sw_buffer);
1125 origlen = desc_read(desc, sw_len);
1126
1127 dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir);
1128 cpdma_desc_free(pool, desc, 1);
1129 (*chan->handler)(token, outlen, status);
1130}
1131
1132static int __cpdma_chan_process(struct cpdma_chan *chan)
1133{
1134 struct cpdma_ctlr *ctlr = chan->ctlr;
1135 struct cpdma_desc __iomem *desc;
1136 int status, outlen;
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001137 int cb_status = 0;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001138 struct cpdma_desc_pool *pool = ctlr->pool;
1139 dma_addr_t desc_dma;
1140 unsigned long flags;
1141
1142 spin_lock_irqsave(&chan->lock, flags);
1143
1144 desc = chan->head;
1145 if (!desc) {
1146 chan->stats.empty_dequeue++;
1147 status = -ENOENT;
1148 goto unlock_ret;
1149 }
1150 desc_dma = desc_phys(pool, desc);
1151
Grygorii Strashkoa6c83cc2017-01-06 14:07:29 -06001152 status = desc_read(desc, hw_mode);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001153 outlen = status & 0x7ff;
1154 if (status & CPDMA_DESC_OWNER) {
1155 chan->stats.busy_dequeue++;
1156 status = -EBUSY;
1157 goto unlock_ret;
1158 }
Mugunthan V N28a19fe2013-05-29 20:22:01 +00001159
1160 if (status & CPDMA_DESC_PASS_CRC)
1161 outlen -= CPDMA_DESC_CRC_LEN;
1162
Mugunthan V Nf6e135c2013-02-11 09:52:18 +00001163 status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE |
1164 CPDMA_DESC_PORT_MASK);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001165
1166 chan->head = desc_from_phys(pool, desc_read(desc, hw_next));
1167 chan_write(chan, cp, desc_dma);
1168 chan->count--;
1169 chan->stats.good_dequeue++;
1170
Grygorii Strashko12a303e2017-01-06 14:07:30 -06001171 if ((status & CPDMA_DESC_EOQ) && chan->head) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001172 chan->stats.requeue++;
1173 chan_write(chan, hdp, desc_phys(pool, chan->head));
1174 }
1175
1176 spin_unlock_irqrestore(&chan->lock, flags);
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001177 if (unlikely(status & CPDMA_DESC_TD_COMPLETE))
1178 cb_status = -ENOSYS;
1179 else
1180 cb_status = status;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001181
Sebastian Siewiorb4727e62013-04-23 07:31:39 +00001182 __cpdma_chan_free(chan, desc, outlen, cb_status);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001183 return status;
1184
1185unlock_ret:
1186 spin_unlock_irqrestore(&chan->lock, flags);
1187 return status;
1188}
1189
1190int cpdma_chan_process(struct cpdma_chan *chan, int quota)
1191{
1192 int used = 0, ret = 0;
1193
1194 if (chan->state != CPDMA_STATE_ACTIVE)
1195 return -EINVAL;
1196
1197 while (used < quota) {
1198 ret = __cpdma_chan_process(chan);
1199 if (ret < 0)
1200 break;
1201 used++;
1202 }
1203 return used;
1204}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001205EXPORT_SYMBOL_GPL(cpdma_chan_process);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001206
1207int cpdma_chan_start(struct cpdma_chan *chan)
1208{
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001209 struct cpdma_ctlr *ctlr = chan->ctlr;
1210 unsigned long flags;
1211 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001212
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001213 spin_lock_irqsave(&ctlr->lock, flags);
1214 ret = cpdma_chan_set_chan_shaper(chan);
1215 spin_unlock_irqrestore(&ctlr->lock, flags);
1216 if (ret)
1217 return ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001218
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001219 ret = cpdma_chan_on(chan);
1220 if (ret)
1221 return ret;
1222
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001223 return 0;
1224}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001225EXPORT_SYMBOL_GPL(cpdma_chan_start);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001226
1227int cpdma_chan_stop(struct cpdma_chan *chan)
1228{
1229 struct cpdma_ctlr *ctlr = chan->ctlr;
1230 struct cpdma_desc_pool *pool = ctlr->pool;
1231 unsigned long flags;
1232 int ret;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001233 unsigned timeout;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001234
1235 spin_lock_irqsave(&chan->lock, flags);
Christian Rieschcd11cf52014-03-24 13:46:27 +01001236 if (chan->state == CPDMA_STATE_TEARDOWN) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001237 spin_unlock_irqrestore(&chan->lock, flags);
1238 return -EINVAL;
1239 }
1240
1241 chan->state = CPDMA_STATE_TEARDOWN;
1242 dma_reg_write(ctlr, chan->int_clear, chan->mask);
1243
1244 /* trigger teardown */
Christian Rieschb4ad0422012-02-22 21:58:00 +00001245 dma_reg_write(ctlr, chan->td, chan_linear(chan));
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001246
1247 /* wait for teardown complete */
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001248 timeout = 100 * 100; /* 100 ms */
1249 while (timeout) {
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001250 u32 cp = chan_read(chan, cp);
1251 if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE)
1252 break;
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001253 udelay(10);
1254 timeout--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001255 }
Sebastian Siewior817f6d12013-04-23 07:31:35 +00001256 WARN_ON(!timeout);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001257 chan_write(chan, cp, CPDMA_TEARDOWN_VALUE);
1258
1259 /* handle completed packets */
Ilya Yanok7746ab02011-12-18 10:02:04 +00001260 spin_unlock_irqrestore(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001261 do {
1262 ret = __cpdma_chan_process(chan);
1263 if (ret < 0)
1264 break;
1265 } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0);
Ilya Yanok7746ab02011-12-18 10:02:04 +00001266 spin_lock_irqsave(&chan->lock, flags);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001267
1268 /* remaining packets haven't been tx/rx'ed, clean them up */
1269 while (chan->head) {
1270 struct cpdma_desc __iomem *desc = chan->head;
1271 dma_addr_t next_dma;
1272
1273 next_dma = desc_read(desc, hw_next);
1274 chan->head = desc_from_phys(pool, next_dma);
htbeginffb5ba92012-10-01 16:42:43 +00001275 chan->count--;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001276 chan->stats.teardown_dequeue++;
1277
1278 /* issue callback without locks held */
1279 spin_unlock_irqrestore(&chan->lock, flags);
1280 __cpdma_chan_free(chan, desc, 0, -ENOSYS);
1281 spin_lock_irqsave(&chan->lock, flags);
1282 }
1283
1284 chan->state = CPDMA_STATE_IDLE;
1285 spin_unlock_irqrestore(&chan->lock, flags);
1286 return 0;
1287}
Arnd Bergmann32a6d902012-04-20 10:56:09 +00001288EXPORT_SYMBOL_GPL(cpdma_chan_stop);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001289
1290int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable)
1291{
1292 unsigned long flags;
1293
1294 spin_lock_irqsave(&chan->lock, flags);
1295 if (chan->state != CPDMA_STATE_ACTIVE) {
1296 spin_unlock_irqrestore(&chan->lock, flags);
1297 return -EINVAL;
1298 }
1299
1300 dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear,
1301 chan->mask);
1302 spin_unlock_irqrestore(&chan->lock, flags);
1303
1304 return 0;
1305}
1306
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001307int cpdma_control_get(struct cpdma_ctlr *ctlr, int control)
1308{
1309 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001310 int ret;
1311
1312 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001313 ret = _cpdma_control_get(ctlr, control);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001314 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001315
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001316 return ret;
1317}
1318
1319int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value)
1320{
1321 unsigned long flags;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001322 int ret;
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001323
1324 spin_lock_irqsave(&ctlr->lock, flags);
Ivan Khoronzhuk991ddb12016-11-11 15:45:24 +02001325 ret = _cpdma_control_set(ctlr, control, value);
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001326 spin_unlock_irqrestore(&ctlr->lock, flags);
Ivan Khoronzhuk8f32b902016-11-29 17:00:48 +02001327
Cyril Chemparathyef8c2da2010-09-15 10:11:28 -04001328 return ret;
1329}
Arnd Bergmann6929e242013-02-14 17:53:01 +01001330EXPORT_SYMBOL_GPL(cpdma_control_set);
Sebastian Siewior4bc21d42013-04-24 08:48:22 +00001331
1332MODULE_LICENSE("GPL");