Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1 | /* |
| 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 Mack | 76fbc24 | 2012-06-28 06:12:32 +0000 | [diff] [blame] | 18 | #include <linux/module.h> |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 19 | #include <linux/slab.h> |
| 20 | #include <linux/err.h> |
| 21 | #include <linux/dma-mapping.h> |
| 22 | #include <linux/io.h> |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 23 | #include <linux/delay.h> |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 24 | #include <linux/genalloc.h> |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 25 | #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 |
| 35 | #define CPDMA_TXINTSTATRAW 0x80 |
| 36 | #define CPDMA_TXINTSTATMASKED 0x84 |
| 37 | #define CPDMA_TXINTMASKSET 0x88 |
| 38 | #define CPDMA_TXINTMASKCLEAR 0x8c |
| 39 | #define CPDMA_MACINVECTOR 0x90 |
| 40 | #define CPDMA_MACEOIVECTOR 0x94 |
| 41 | #define CPDMA_RXINTSTATRAW 0xa0 |
| 42 | #define CPDMA_RXINTSTATMASKED 0xa4 |
| 43 | #define CPDMA_RXINTMASKSET 0xa8 |
| 44 | #define CPDMA_RXINTMASKCLEAR 0xac |
| 45 | #define CPDMA_DMAINTSTATRAW 0xb0 |
| 46 | #define CPDMA_DMAINTSTATMASKED 0xb4 |
| 47 | #define CPDMA_DMAINTMASKSET 0xb8 |
| 48 | #define CPDMA_DMAINTMASKCLEAR 0xbc |
| 49 | #define CPDMA_DMAINT_HOSTERR BIT(1) |
| 50 | |
| 51 | /* the following exist only if has_ext_regs is set */ |
| 52 | #define CPDMA_DMACONTROL 0x20 |
| 53 | #define CPDMA_DMASTATUS 0x24 |
| 54 | #define CPDMA_RXBUFFOFS 0x28 |
| 55 | #define CPDMA_EM_CONTROL 0x2c |
| 56 | |
| 57 | /* Descriptor mode bits */ |
| 58 | #define CPDMA_DESC_SOP BIT(31) |
| 59 | #define CPDMA_DESC_EOP BIT(30) |
| 60 | #define CPDMA_DESC_OWNER BIT(29) |
| 61 | #define CPDMA_DESC_EOQ BIT(28) |
| 62 | #define CPDMA_DESC_TD_COMPLETE BIT(27) |
| 63 | #define CPDMA_DESC_PASS_CRC BIT(26) |
Mugunthan V N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 64 | #define CPDMA_DESC_TO_PORT_EN BIT(20) |
| 65 | #define CPDMA_TO_PORT_SHIFT 16 |
| 66 | #define CPDMA_DESC_PORT_MASK (BIT(18) | BIT(17) | BIT(16)) |
Mugunthan V N | 28a19fe | 2013-05-29 20:22:01 +0000 | [diff] [blame] | 67 | #define CPDMA_DESC_CRC_LEN 4 |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 68 | |
| 69 | #define CPDMA_TEARDOWN_VALUE 0xfffffffc |
| 70 | |
| 71 | struct cpdma_desc { |
| 72 | /* hardware fields */ |
| 73 | u32 hw_next; |
| 74 | u32 hw_buffer; |
| 75 | u32 hw_len; |
| 76 | u32 hw_mode; |
| 77 | /* software fields */ |
| 78 | void *sw_token; |
| 79 | u32 sw_buffer; |
| 80 | u32 sw_len; |
| 81 | }; |
| 82 | |
| 83 | struct cpdma_desc_pool { |
Olof Johansson | c767db5 | 2013-12-11 15:51:20 -0800 | [diff] [blame] | 84 | phys_addr_t phys; |
Arnd Bergmann | 8409299 | 2016-01-29 12:39:10 +0100 | [diff] [blame] | 85 | dma_addr_t hw_addr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 86 | void __iomem *iomap; /* ioremap map */ |
| 87 | void *cpumap; /* dma_alloc map */ |
| 88 | int desc_size, mem_size; |
Grygorii Strashko | aeec302 | 2016-08-04 18:20:51 +0300 | [diff] [blame] | 89 | int num_desc; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 90 | struct device *dev; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 91 | struct gen_pool *gen_pool; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | enum cpdma_state { |
| 95 | CPDMA_STATE_IDLE, |
| 96 | CPDMA_STATE_ACTIVE, |
| 97 | CPDMA_STATE_TEARDOWN, |
| 98 | }; |
| 99 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 100 | struct cpdma_ctlr { |
| 101 | enum cpdma_state state; |
| 102 | struct cpdma_params params; |
| 103 | struct device *dev; |
| 104 | struct cpdma_desc_pool *pool; |
| 105 | spinlock_t lock; |
| 106 | struct cpdma_chan *channels[2 * CPDMA_MAX_CHANNELS]; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame^] | 107 | int chan_num; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 108 | }; |
| 109 | |
| 110 | struct cpdma_chan { |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 111 | struct cpdma_desc __iomem *head, *tail; |
| 112 | void __iomem *hdp, *cp, *rxfree; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 113 | enum cpdma_state state; |
| 114 | struct cpdma_ctlr *ctlr; |
| 115 | int chan_num; |
| 116 | spinlock_t lock; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 117 | int count; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 118 | u32 desc_num; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 119 | u32 mask; |
| 120 | cpdma_handler_fn handler; |
| 121 | enum dma_data_direction dir; |
| 122 | struct cpdma_chan_stats stats; |
| 123 | /* offsets into dmaregs */ |
| 124 | int int_set, int_clear, td; |
| 125 | }; |
| 126 | |
| 127 | /* The following make access to common cpdma_ctlr params more readable */ |
| 128 | #define dmaregs params.dmaregs |
| 129 | #define num_chan params.num_chan |
| 130 | |
| 131 | /* various accessors */ |
| 132 | #define dma_reg_read(ctlr, ofs) __raw_readl((ctlr)->dmaregs + (ofs)) |
| 133 | #define chan_read(chan, fld) __raw_readl((chan)->fld) |
| 134 | #define desc_read(desc, fld) __raw_readl(&(desc)->fld) |
| 135 | #define dma_reg_write(ctlr, ofs, v) __raw_writel(v, (ctlr)->dmaregs + (ofs)) |
| 136 | #define chan_write(chan, fld, v) __raw_writel(v, (chan)->fld) |
| 137 | #define desc_write(desc, fld, v) __raw_writel((u32)(v), &(desc)->fld) |
| 138 | |
Mugunthan V N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 139 | #define cpdma_desc_to_port(chan, mode, directed) \ |
| 140 | do { \ |
| 141 | if (!is_rx_chan(chan) && ((directed == 1) || \ |
| 142 | (directed == 2))) \ |
| 143 | mode |= (CPDMA_DESC_TO_PORT_EN | \ |
| 144 | (directed << CPDMA_TO_PORT_SHIFT)); \ |
| 145 | } while (0) |
| 146 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 147 | static void cpdma_desc_pool_destroy(struct cpdma_desc_pool *pool) |
| 148 | { |
| 149 | if (!pool) |
| 150 | return; |
| 151 | |
Grygorii Strashko | aeec302 | 2016-08-04 18:20:51 +0300 | [diff] [blame] | 152 | WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool), |
| 153 | "cpdma_desc_pool size %d != avail %d", |
| 154 | gen_pool_size(pool->gen_pool), |
| 155 | gen_pool_avail(pool->gen_pool)); |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 156 | if (pool->cpumap) |
| 157 | dma_free_coherent(pool->dev, pool->mem_size, pool->cpumap, |
| 158 | pool->phys); |
| 159 | else |
| 160 | iounmap(pool->iomap); |
| 161 | } |
| 162 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 163 | /* |
| 164 | * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci |
| 165 | * emac) have dedicated on-chip memory for these descriptors. Some other |
| 166 | * devices (e.g. cpsw switches) use plain old memory. Descriptor pools |
| 167 | * abstract out these details |
| 168 | */ |
| 169 | static struct cpdma_desc_pool * |
Arnd Bergmann | 8409299 | 2016-01-29 12:39:10 +0100 | [diff] [blame] | 170 | cpdma_desc_pool_create(struct device *dev, u32 phys, dma_addr_t hw_addr, |
Sriram | 6a1fef6 | 2011-03-22 02:31:03 +0000 | [diff] [blame] | 171 | int size, int align) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 172 | { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 173 | struct cpdma_desc_pool *pool; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 174 | int ret; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 175 | |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 176 | pool = devm_kzalloc(dev, sizeof(*pool), GFP_KERNEL); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 177 | if (!pool) |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 178 | goto gen_pool_create_fail; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 179 | |
| 180 | pool->dev = dev; |
| 181 | pool->mem_size = size; |
| 182 | pool->desc_size = ALIGN(sizeof(struct cpdma_desc), align); |
| 183 | pool->num_desc = size / pool->desc_size; |
| 184 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 185 | pool->gen_pool = devm_gen_pool_create(dev, ilog2(pool->desc_size), -1, |
| 186 | "cpdma"); |
| 187 | if (IS_ERR(pool->gen_pool)) { |
| 188 | dev_err(dev, "pool create failed %ld\n", |
| 189 | PTR_ERR(pool->gen_pool)); |
| 190 | goto gen_pool_create_fail; |
| 191 | } |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 192 | |
| 193 | if (phys) { |
| 194 | pool->phys = phys; |
Arnd Bergmann | 8409299 | 2016-01-29 12:39:10 +0100 | [diff] [blame] | 195 | pool->iomap = ioremap(phys, size); /* should be memremap? */ |
Sriram | 6a1fef6 | 2011-03-22 02:31:03 +0000 | [diff] [blame] | 196 | pool->hw_addr = hw_addr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 197 | } else { |
Arnd Bergmann | 8409299 | 2016-01-29 12:39:10 +0100 | [diff] [blame] | 198 | pool->cpumap = dma_alloc_coherent(dev, size, &pool->hw_addr, |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 199 | GFP_KERNEL); |
Arnd Bergmann | 8409299 | 2016-01-29 12:39:10 +0100 | [diff] [blame] | 200 | pool->iomap = (void __iomem __force *)pool->cpumap; |
| 201 | pool->phys = pool->hw_addr; /* assumes no IOMMU, don't use this value */ |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 202 | } |
| 203 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 204 | if (!pool->iomap) |
| 205 | goto gen_pool_create_fail; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 206 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 207 | ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap, |
| 208 | pool->phys, pool->mem_size, -1); |
| 209 | if (ret < 0) { |
| 210 | dev_err(dev, "pool add failed %d\n", ret); |
| 211 | goto gen_pool_add_virt_fail; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 212 | } |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 213 | |
| 214 | return pool; |
| 215 | |
| 216 | gen_pool_add_virt_fail: |
| 217 | cpdma_desc_pool_destroy(pool); |
| 218 | gen_pool_create_fail: |
| 219 | return NULL; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 220 | } |
| 221 | |
| 222 | static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool, |
| 223 | struct cpdma_desc __iomem *desc) |
| 224 | { |
| 225 | if (!desc) |
| 226 | return 0; |
Olof Johansson | c767db5 | 2013-12-11 15:51:20 -0800 | [diff] [blame] | 227 | return pool->hw_addr + (__force long)desc - (__force long)pool->iomap; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 228 | } |
| 229 | |
| 230 | static inline struct cpdma_desc __iomem * |
| 231 | desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma) |
| 232 | { |
Sriram | 6a1fef6 | 2011-03-22 02:31:03 +0000 | [diff] [blame] | 233 | return dma ? pool->iomap + dma - pool->hw_addr : NULL; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | static struct cpdma_desc __iomem * |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 237 | cpdma_desc_alloc(struct cpdma_desc_pool *pool) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 238 | { |
Grygorii Strashko | aeec302 | 2016-08-04 18:20:51 +0300 | [diff] [blame] | 239 | return (struct cpdma_desc __iomem *) |
| 240 | gen_pool_alloc(pool->gen_pool, pool->desc_size); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | static void cpdma_desc_free(struct cpdma_desc_pool *pool, |
| 244 | struct cpdma_desc __iomem *desc, int num_desc) |
| 245 | { |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 246 | gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 247 | } |
| 248 | |
| 249 | struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params) |
| 250 | { |
| 251 | struct cpdma_ctlr *ctlr; |
| 252 | |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 253 | ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 254 | if (!ctlr) |
| 255 | return NULL; |
| 256 | |
| 257 | ctlr->state = CPDMA_STATE_IDLE; |
| 258 | ctlr->params = *params; |
| 259 | ctlr->dev = params->dev; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame^] | 260 | ctlr->chan_num = 0; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 261 | spin_lock_init(&ctlr->lock); |
| 262 | |
| 263 | ctlr->pool = cpdma_desc_pool_create(ctlr->dev, |
| 264 | ctlr->params.desc_mem_phys, |
Sriram | 6a1fef6 | 2011-03-22 02:31:03 +0000 | [diff] [blame] | 265 | ctlr->params.desc_hw_addr, |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 266 | ctlr->params.desc_mem_size, |
| 267 | ctlr->params.desc_align); |
Dan Carpenter | 2f87208 | 2014-06-11 11:16:51 +0300 | [diff] [blame] | 268 | if (!ctlr->pool) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 269 | return NULL; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 270 | |
| 271 | if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS)) |
| 272 | ctlr->num_chan = CPDMA_MAX_CHANNELS; |
| 273 | return ctlr; |
| 274 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 275 | EXPORT_SYMBOL_GPL(cpdma_ctlr_create); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 276 | |
| 277 | int cpdma_ctlr_start(struct cpdma_ctlr *ctlr) |
| 278 | { |
| 279 | unsigned long flags; |
| 280 | int i; |
| 281 | |
| 282 | spin_lock_irqsave(&ctlr->lock, flags); |
| 283 | if (ctlr->state != CPDMA_STATE_IDLE) { |
| 284 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 285 | return -EBUSY; |
| 286 | } |
| 287 | |
| 288 | if (ctlr->params.has_soft_reset) { |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 289 | unsigned timeout = 10 * 100; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 290 | |
| 291 | dma_reg_write(ctlr, CPDMA_SOFTRESET, 1); |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 292 | while (timeout) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 293 | if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0) |
| 294 | break; |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 295 | udelay(10); |
| 296 | timeout--; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 297 | } |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 298 | WARN_ON(!timeout); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 299 | } |
| 300 | |
| 301 | for (i = 0; i < ctlr->num_chan; i++) { |
| 302 | __raw_writel(0, ctlr->params.txhdp + 4 * i); |
| 303 | __raw_writel(0, ctlr->params.rxhdp + 4 * i); |
| 304 | __raw_writel(0, ctlr->params.txcp + 4 * i); |
| 305 | __raw_writel(0, ctlr->params.rxcp + 4 * i); |
| 306 | } |
| 307 | |
| 308 | dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff); |
| 309 | dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff); |
| 310 | |
| 311 | dma_reg_write(ctlr, CPDMA_TXCONTROL, 1); |
| 312 | dma_reg_write(ctlr, CPDMA_RXCONTROL, 1); |
| 313 | |
| 314 | ctlr->state = CPDMA_STATE_ACTIVE; |
| 315 | |
| 316 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
| 317 | if (ctlr->channels[i]) |
| 318 | cpdma_chan_start(ctlr->channels[i]); |
| 319 | } |
| 320 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 321 | return 0; |
| 322 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 323 | EXPORT_SYMBOL_GPL(cpdma_ctlr_start); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 324 | |
| 325 | int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr) |
| 326 | { |
| 327 | unsigned long flags; |
| 328 | int i; |
| 329 | |
| 330 | spin_lock_irqsave(&ctlr->lock, flags); |
Christian Riesch | cd11cf5 | 2014-03-24 13:46:27 +0100 | [diff] [blame] | 331 | if (ctlr->state == CPDMA_STATE_TEARDOWN) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 332 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 333 | return -EINVAL; |
| 334 | } |
| 335 | |
| 336 | ctlr->state = CPDMA_STATE_TEARDOWN; |
| 337 | |
| 338 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
| 339 | if (ctlr->channels[i]) |
| 340 | cpdma_chan_stop(ctlr->channels[i]); |
| 341 | } |
| 342 | |
| 343 | dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff); |
| 344 | dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff); |
| 345 | |
| 346 | dma_reg_write(ctlr, CPDMA_TXCONTROL, 0); |
| 347 | dma_reg_write(ctlr, CPDMA_RXCONTROL, 0); |
| 348 | |
| 349 | ctlr->state = CPDMA_STATE_IDLE; |
| 350 | |
| 351 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 352 | return 0; |
| 353 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 354 | EXPORT_SYMBOL_GPL(cpdma_ctlr_stop); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 355 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 356 | int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr) |
| 357 | { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 358 | int ret = 0, i; |
| 359 | |
| 360 | if (!ctlr) |
| 361 | return -EINVAL; |
| 362 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 363 | if (ctlr->state != CPDMA_STATE_IDLE) |
| 364 | cpdma_ctlr_stop(ctlr); |
| 365 | |
Cyril Roelandt | 79876e0 | 2013-02-12 12:52:30 +0000 | [diff] [blame] | 366 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) |
| 367 | cpdma_chan_destroy(ctlr->channels[i]); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 368 | |
| 369 | cpdma_desc_pool_destroy(ctlr->pool); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 370 | return ret; |
| 371 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 372 | EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 373 | |
| 374 | int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable) |
| 375 | { |
| 376 | unsigned long flags; |
| 377 | int i, reg; |
| 378 | |
| 379 | spin_lock_irqsave(&ctlr->lock, flags); |
| 380 | if (ctlr->state != CPDMA_STATE_ACTIVE) { |
| 381 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 382 | return -EINVAL; |
| 383 | } |
| 384 | |
| 385 | reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR; |
| 386 | dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR); |
| 387 | |
| 388 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
| 389 | if (ctlr->channels[i]) |
| 390 | cpdma_chan_int_ctrl(ctlr->channels[i], enable); |
| 391 | } |
| 392 | |
| 393 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 394 | return 0; |
| 395 | } |
Arnd Bergmann | 6929e24 | 2013-02-14 17:53:01 +0100 | [diff] [blame] | 396 | EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 397 | |
Mugunthan V N | 510a1e72 | 2013-02-17 22:19:20 +0000 | [diff] [blame] | 398 | void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 399 | { |
Mugunthan V N | 510a1e72 | 2013-02-17 22:19:20 +0000 | [diff] [blame] | 400 | dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 401 | } |
Arnd Bergmann | 6929e24 | 2013-02-14 17:53:01 +0100 | [diff] [blame] | 402 | EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 403 | |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame^] | 404 | /** |
| 405 | * cpdma_chan_split_pool - Splits ctrl pool between all channels. |
| 406 | * Has to be called under ctlr lock |
| 407 | */ |
| 408 | static void cpdma_chan_split_pool(struct cpdma_ctlr *ctlr) |
| 409 | { |
| 410 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 411 | struct cpdma_chan *chan; |
| 412 | int ch_desc_num; |
| 413 | int i; |
| 414 | |
| 415 | if (!ctlr->chan_num) |
| 416 | return; |
| 417 | |
| 418 | /* calculate average size of pool slice */ |
| 419 | ch_desc_num = pool->num_desc / ctlr->chan_num; |
| 420 | |
| 421 | /* split ctlr pool */ |
| 422 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
| 423 | chan = ctlr->channels[i]; |
| 424 | if (chan) |
| 425 | chan->desc_num = ch_desc_num; |
| 426 | } |
| 427 | } |
| 428 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 429 | struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num, |
| 430 | cpdma_handler_fn handler) |
| 431 | { |
| 432 | struct cpdma_chan *chan; |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 433 | int offset = (chan_num % CPDMA_MAX_CHANNELS) * 4; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 434 | unsigned long flags; |
| 435 | |
| 436 | if (__chan_linear(chan_num) >= ctlr->num_chan) |
| 437 | return NULL; |
| 438 | |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 439 | chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 440 | if (!chan) |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 441 | return ERR_PTR(-ENOMEM); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 442 | |
| 443 | spin_lock_irqsave(&ctlr->lock, flags); |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 444 | if (ctlr->channels[chan_num]) { |
| 445 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 446 | devm_kfree(ctlr->dev, chan); |
| 447 | return ERR_PTR(-EBUSY); |
| 448 | } |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 449 | |
| 450 | chan->ctlr = ctlr; |
| 451 | chan->state = CPDMA_STATE_IDLE; |
| 452 | chan->chan_num = chan_num; |
| 453 | chan->handler = handler; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 454 | chan->desc_num = ctlr->pool->num_desc / 2; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 455 | |
| 456 | if (is_rx_chan(chan)) { |
| 457 | chan->hdp = ctlr->params.rxhdp + offset; |
| 458 | chan->cp = ctlr->params.rxcp + offset; |
| 459 | chan->rxfree = ctlr->params.rxfree + offset; |
| 460 | chan->int_set = CPDMA_RXINTMASKSET; |
| 461 | chan->int_clear = CPDMA_RXINTMASKCLEAR; |
| 462 | chan->td = CPDMA_RXTEARDOWN; |
| 463 | chan->dir = DMA_FROM_DEVICE; |
| 464 | } else { |
| 465 | chan->hdp = ctlr->params.txhdp + offset; |
| 466 | chan->cp = ctlr->params.txcp + offset; |
| 467 | chan->int_set = CPDMA_TXINTMASKSET; |
| 468 | chan->int_clear = CPDMA_TXINTMASKCLEAR; |
| 469 | chan->td = CPDMA_TXTEARDOWN; |
| 470 | chan->dir = DMA_TO_DEVICE; |
| 471 | } |
| 472 | chan->mask = BIT(chan_linear(chan)); |
| 473 | |
| 474 | spin_lock_init(&chan->lock); |
| 475 | |
| 476 | ctlr->channels[chan_num] = chan; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame^] | 477 | ctlr->chan_num++; |
| 478 | |
| 479 | cpdma_chan_split_pool(ctlr); |
| 480 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 481 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 482 | return chan; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 483 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 484 | EXPORT_SYMBOL_GPL(cpdma_chan_create); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 485 | |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame^] | 486 | int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan) |
Ivan Khoronzhuk | 1793331 | 2016-06-17 13:25:39 +0300 | [diff] [blame] | 487 | { |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame^] | 488 | unsigned long flags; |
| 489 | int desc_num; |
| 490 | |
| 491 | spin_lock_irqsave(&chan->lock, flags); |
| 492 | desc_num = chan->desc_num; |
| 493 | spin_unlock_irqrestore(&chan->lock, flags); |
| 494 | |
| 495 | return desc_num; |
Ivan Khoronzhuk | 1793331 | 2016-06-17 13:25:39 +0300 | [diff] [blame] | 496 | } |
| 497 | EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num); |
| 498 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 499 | int cpdma_chan_destroy(struct cpdma_chan *chan) |
| 500 | { |
Julia Lawall | f37c54b | 2012-08-14 05:49:47 +0000 | [diff] [blame] | 501 | struct cpdma_ctlr *ctlr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 502 | unsigned long flags; |
| 503 | |
| 504 | if (!chan) |
| 505 | return -EINVAL; |
Julia Lawall | f37c54b | 2012-08-14 05:49:47 +0000 | [diff] [blame] | 506 | ctlr = chan->ctlr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 507 | |
| 508 | spin_lock_irqsave(&ctlr->lock, flags); |
| 509 | if (chan->state != CPDMA_STATE_IDLE) |
| 510 | cpdma_chan_stop(chan); |
| 511 | ctlr->channels[chan->chan_num] = NULL; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame^] | 512 | ctlr->chan_num--; |
| 513 | |
| 514 | cpdma_chan_split_pool(ctlr); |
| 515 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 516 | spin_unlock_irqrestore(&ctlr->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 517 | return 0; |
| 518 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 519 | EXPORT_SYMBOL_GPL(cpdma_chan_destroy); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 520 | |
| 521 | int cpdma_chan_get_stats(struct cpdma_chan *chan, |
| 522 | struct cpdma_chan_stats *stats) |
| 523 | { |
| 524 | unsigned long flags; |
| 525 | if (!chan) |
| 526 | return -EINVAL; |
| 527 | spin_lock_irqsave(&chan->lock, flags); |
| 528 | memcpy(stats, &chan->stats, sizeof(*stats)); |
| 529 | spin_unlock_irqrestore(&chan->lock, flags); |
| 530 | return 0; |
| 531 | } |
Daniel Mack | 0ca04b6 | 2013-08-22 13:47:00 +0200 | [diff] [blame] | 532 | EXPORT_SYMBOL_GPL(cpdma_chan_get_stats); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 533 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 534 | static void __cpdma_chan_submit(struct cpdma_chan *chan, |
| 535 | struct cpdma_desc __iomem *desc) |
| 536 | { |
| 537 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 538 | struct cpdma_desc __iomem *prev = chan->tail; |
| 539 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 540 | dma_addr_t desc_dma; |
| 541 | u32 mode; |
| 542 | |
| 543 | desc_dma = desc_phys(pool, desc); |
| 544 | |
| 545 | /* simple case - idle channel */ |
| 546 | if (!chan->head) { |
| 547 | chan->stats.head_enqueue++; |
| 548 | chan->head = desc; |
| 549 | chan->tail = desc; |
| 550 | if (chan->state == CPDMA_STATE_ACTIVE) |
| 551 | chan_write(chan, hdp, desc_dma); |
| 552 | return; |
| 553 | } |
| 554 | |
| 555 | /* first chain the descriptor at the tail of the list */ |
| 556 | desc_write(prev, hw_next, desc_dma); |
| 557 | chan->tail = desc; |
| 558 | chan->stats.tail_enqueue++; |
| 559 | |
| 560 | /* next check if EOQ has been triggered already */ |
| 561 | mode = desc_read(prev, hw_mode); |
| 562 | if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) && |
| 563 | (chan->state == CPDMA_STATE_ACTIVE)) { |
| 564 | desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ); |
| 565 | chan_write(chan, hdp, desc_dma); |
| 566 | chan->stats.misqueued++; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data, |
Sebastian Siewior | aef614e | 2013-04-23 07:31:38 +0000 | [diff] [blame] | 571 | int len, int directed) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 572 | { |
| 573 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 574 | struct cpdma_desc __iomem *desc; |
| 575 | dma_addr_t buffer; |
| 576 | unsigned long flags; |
| 577 | u32 mode; |
| 578 | int ret = 0; |
| 579 | |
| 580 | spin_lock_irqsave(&chan->lock, flags); |
| 581 | |
| 582 | if (chan->state == CPDMA_STATE_TEARDOWN) { |
| 583 | ret = -EINVAL; |
| 584 | goto unlock_ret; |
| 585 | } |
| 586 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 587 | if (chan->count >= chan->desc_num) { |
| 588 | chan->stats.desc_alloc_fail++; |
| 589 | ret = -ENOMEM; |
| 590 | goto unlock_ret; |
| 591 | } |
| 592 | |
| 593 | desc = cpdma_desc_alloc(ctlr->pool); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 594 | if (!desc) { |
| 595 | chan->stats.desc_alloc_fail++; |
| 596 | ret = -ENOMEM; |
| 597 | goto unlock_ret; |
| 598 | } |
| 599 | |
| 600 | if (len < ctlr->params.min_packet_size) { |
| 601 | len = ctlr->params.min_packet_size; |
| 602 | chan->stats.runt_transmit_buff++; |
| 603 | } |
| 604 | |
| 605 | buffer = dma_map_single(ctlr->dev, data, len, chan->dir); |
Sebastian Siewior | 14bd076 | 2013-06-20 16:58:45 +0200 | [diff] [blame] | 606 | ret = dma_mapping_error(ctlr->dev, buffer); |
| 607 | if (ret) { |
| 608 | cpdma_desc_free(ctlr->pool, desc, 1); |
| 609 | ret = -EINVAL; |
| 610 | goto unlock_ret; |
| 611 | } |
| 612 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 613 | mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP; |
Mugunthan V N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 614 | cpdma_desc_to_port(chan, mode, directed); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 615 | |
| 616 | desc_write(desc, hw_next, 0); |
| 617 | desc_write(desc, hw_buffer, buffer); |
| 618 | desc_write(desc, hw_len, len); |
| 619 | desc_write(desc, hw_mode, mode | len); |
| 620 | desc_write(desc, sw_token, token); |
| 621 | desc_write(desc, sw_buffer, buffer); |
| 622 | desc_write(desc, sw_len, len); |
| 623 | |
| 624 | __cpdma_chan_submit(chan, desc); |
| 625 | |
| 626 | if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree) |
| 627 | chan_write(chan, rxfree, 1); |
| 628 | |
| 629 | chan->count++; |
| 630 | |
| 631 | unlock_ret: |
| 632 | spin_unlock_irqrestore(&chan->lock, flags); |
| 633 | return ret; |
| 634 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 635 | EXPORT_SYMBOL_GPL(cpdma_chan_submit); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 636 | |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 637 | bool cpdma_check_free_tx_desc(struct cpdma_chan *chan) |
| 638 | { |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 639 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 640 | struct cpdma_desc_pool *pool = ctlr->pool; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 641 | bool free_tx_desc; |
| 642 | unsigned long flags; |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 643 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 644 | spin_lock_irqsave(&chan->lock, flags); |
| 645 | free_tx_desc = (chan->count < chan->desc_num) && |
| 646 | gen_pool_avail(pool->gen_pool); |
| 647 | spin_unlock_irqrestore(&chan->lock, flags); |
| 648 | return free_tx_desc; |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 649 | } |
| 650 | EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc); |
| 651 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 652 | static void __cpdma_chan_free(struct cpdma_chan *chan, |
| 653 | struct cpdma_desc __iomem *desc, |
| 654 | int outlen, int status) |
| 655 | { |
| 656 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 657 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 658 | dma_addr_t buff_dma; |
| 659 | int origlen; |
| 660 | void *token; |
| 661 | |
| 662 | token = (void *)desc_read(desc, sw_token); |
| 663 | buff_dma = desc_read(desc, sw_buffer); |
| 664 | origlen = desc_read(desc, sw_len); |
| 665 | |
| 666 | dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir); |
| 667 | cpdma_desc_free(pool, desc, 1); |
| 668 | (*chan->handler)(token, outlen, status); |
| 669 | } |
| 670 | |
| 671 | static int __cpdma_chan_process(struct cpdma_chan *chan) |
| 672 | { |
| 673 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 674 | struct cpdma_desc __iomem *desc; |
| 675 | int status, outlen; |
Sebastian Siewior | b4727e6 | 2013-04-23 07:31:39 +0000 | [diff] [blame] | 676 | int cb_status = 0; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 677 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 678 | dma_addr_t desc_dma; |
| 679 | unsigned long flags; |
| 680 | |
| 681 | spin_lock_irqsave(&chan->lock, flags); |
| 682 | |
| 683 | desc = chan->head; |
| 684 | if (!desc) { |
| 685 | chan->stats.empty_dequeue++; |
| 686 | status = -ENOENT; |
| 687 | goto unlock_ret; |
| 688 | } |
| 689 | desc_dma = desc_phys(pool, desc); |
| 690 | |
| 691 | status = __raw_readl(&desc->hw_mode); |
| 692 | outlen = status & 0x7ff; |
| 693 | if (status & CPDMA_DESC_OWNER) { |
| 694 | chan->stats.busy_dequeue++; |
| 695 | status = -EBUSY; |
| 696 | goto unlock_ret; |
| 697 | } |
Mugunthan V N | 28a19fe | 2013-05-29 20:22:01 +0000 | [diff] [blame] | 698 | |
| 699 | if (status & CPDMA_DESC_PASS_CRC) |
| 700 | outlen -= CPDMA_DESC_CRC_LEN; |
| 701 | |
Mugunthan V N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 702 | status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE | |
| 703 | CPDMA_DESC_PORT_MASK); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 704 | |
| 705 | chan->head = desc_from_phys(pool, desc_read(desc, hw_next)); |
| 706 | chan_write(chan, cp, desc_dma); |
| 707 | chan->count--; |
| 708 | chan->stats.good_dequeue++; |
| 709 | |
| 710 | if (status & CPDMA_DESC_EOQ) { |
| 711 | chan->stats.requeue++; |
| 712 | chan_write(chan, hdp, desc_phys(pool, chan->head)); |
| 713 | } |
| 714 | |
| 715 | spin_unlock_irqrestore(&chan->lock, flags); |
Sebastian Siewior | b4727e6 | 2013-04-23 07:31:39 +0000 | [diff] [blame] | 716 | if (unlikely(status & CPDMA_DESC_TD_COMPLETE)) |
| 717 | cb_status = -ENOSYS; |
| 718 | else |
| 719 | cb_status = status; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 720 | |
Sebastian Siewior | b4727e6 | 2013-04-23 07:31:39 +0000 | [diff] [blame] | 721 | __cpdma_chan_free(chan, desc, outlen, cb_status); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 722 | return status; |
| 723 | |
| 724 | unlock_ret: |
| 725 | spin_unlock_irqrestore(&chan->lock, flags); |
| 726 | return status; |
| 727 | } |
| 728 | |
| 729 | int cpdma_chan_process(struct cpdma_chan *chan, int quota) |
| 730 | { |
| 731 | int used = 0, ret = 0; |
| 732 | |
| 733 | if (chan->state != CPDMA_STATE_ACTIVE) |
| 734 | return -EINVAL; |
| 735 | |
| 736 | while (used < quota) { |
| 737 | ret = __cpdma_chan_process(chan); |
| 738 | if (ret < 0) |
| 739 | break; |
| 740 | used++; |
| 741 | } |
| 742 | return used; |
| 743 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 744 | EXPORT_SYMBOL_GPL(cpdma_chan_process); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 745 | |
| 746 | int cpdma_chan_start(struct cpdma_chan *chan) |
| 747 | { |
| 748 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 749 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 750 | unsigned long flags; |
| 751 | |
| 752 | spin_lock_irqsave(&chan->lock, flags); |
| 753 | if (chan->state != CPDMA_STATE_IDLE) { |
| 754 | spin_unlock_irqrestore(&chan->lock, flags); |
| 755 | return -EBUSY; |
| 756 | } |
| 757 | if (ctlr->state != CPDMA_STATE_ACTIVE) { |
| 758 | spin_unlock_irqrestore(&chan->lock, flags); |
| 759 | return -EINVAL; |
| 760 | } |
| 761 | dma_reg_write(ctlr, chan->int_set, chan->mask); |
| 762 | chan->state = CPDMA_STATE_ACTIVE; |
| 763 | if (chan->head) { |
| 764 | chan_write(chan, hdp, desc_phys(pool, chan->head)); |
| 765 | if (chan->rxfree) |
| 766 | chan_write(chan, rxfree, chan->count); |
| 767 | } |
| 768 | |
| 769 | spin_unlock_irqrestore(&chan->lock, flags); |
| 770 | return 0; |
| 771 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 772 | EXPORT_SYMBOL_GPL(cpdma_chan_start); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 773 | |
| 774 | int cpdma_chan_stop(struct cpdma_chan *chan) |
| 775 | { |
| 776 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 777 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 778 | unsigned long flags; |
| 779 | int ret; |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 780 | unsigned timeout; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 781 | |
| 782 | spin_lock_irqsave(&chan->lock, flags); |
Christian Riesch | cd11cf5 | 2014-03-24 13:46:27 +0100 | [diff] [blame] | 783 | if (chan->state == CPDMA_STATE_TEARDOWN) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 784 | spin_unlock_irqrestore(&chan->lock, flags); |
| 785 | return -EINVAL; |
| 786 | } |
| 787 | |
| 788 | chan->state = CPDMA_STATE_TEARDOWN; |
| 789 | dma_reg_write(ctlr, chan->int_clear, chan->mask); |
| 790 | |
| 791 | /* trigger teardown */ |
Christian Riesch | b4ad042 | 2012-02-22 21:58:00 +0000 | [diff] [blame] | 792 | dma_reg_write(ctlr, chan->td, chan_linear(chan)); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 793 | |
| 794 | /* wait for teardown complete */ |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 795 | timeout = 100 * 100; /* 100 ms */ |
| 796 | while (timeout) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 797 | u32 cp = chan_read(chan, cp); |
| 798 | if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE) |
| 799 | break; |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 800 | udelay(10); |
| 801 | timeout--; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 802 | } |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 803 | WARN_ON(!timeout); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 804 | chan_write(chan, cp, CPDMA_TEARDOWN_VALUE); |
| 805 | |
| 806 | /* handle completed packets */ |
Ilya Yanok | 7746ab0 | 2011-12-18 10:02:04 +0000 | [diff] [blame] | 807 | spin_unlock_irqrestore(&chan->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 808 | do { |
| 809 | ret = __cpdma_chan_process(chan); |
| 810 | if (ret < 0) |
| 811 | break; |
| 812 | } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0); |
Ilya Yanok | 7746ab0 | 2011-12-18 10:02:04 +0000 | [diff] [blame] | 813 | spin_lock_irqsave(&chan->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 814 | |
| 815 | /* remaining packets haven't been tx/rx'ed, clean them up */ |
| 816 | while (chan->head) { |
| 817 | struct cpdma_desc __iomem *desc = chan->head; |
| 818 | dma_addr_t next_dma; |
| 819 | |
| 820 | next_dma = desc_read(desc, hw_next); |
| 821 | chan->head = desc_from_phys(pool, next_dma); |
htbegin | ffb5ba9 | 2012-10-01 16:42:43 +0000 | [diff] [blame] | 822 | chan->count--; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 823 | chan->stats.teardown_dequeue++; |
| 824 | |
| 825 | /* issue callback without locks held */ |
| 826 | spin_unlock_irqrestore(&chan->lock, flags); |
| 827 | __cpdma_chan_free(chan, desc, 0, -ENOSYS); |
| 828 | spin_lock_irqsave(&chan->lock, flags); |
| 829 | } |
| 830 | |
| 831 | chan->state = CPDMA_STATE_IDLE; |
| 832 | spin_unlock_irqrestore(&chan->lock, flags); |
| 833 | return 0; |
| 834 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 835 | EXPORT_SYMBOL_GPL(cpdma_chan_stop); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 836 | |
| 837 | int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable) |
| 838 | { |
| 839 | unsigned long flags; |
| 840 | |
| 841 | spin_lock_irqsave(&chan->lock, flags); |
| 842 | if (chan->state != CPDMA_STATE_ACTIVE) { |
| 843 | spin_unlock_irqrestore(&chan->lock, flags); |
| 844 | return -EINVAL; |
| 845 | } |
| 846 | |
| 847 | dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear, |
| 848 | chan->mask); |
| 849 | spin_unlock_irqrestore(&chan->lock, flags); |
| 850 | |
| 851 | return 0; |
| 852 | } |
| 853 | |
| 854 | struct cpdma_control_info { |
| 855 | u32 reg; |
| 856 | u32 shift, mask; |
| 857 | int access; |
| 858 | #define ACCESS_RO BIT(0) |
| 859 | #define ACCESS_WO BIT(1) |
| 860 | #define ACCESS_RW (ACCESS_RO | ACCESS_WO) |
| 861 | }; |
| 862 | |
Olof Johansson | df78416 | 2013-12-11 15:51:21 -0800 | [diff] [blame] | 863 | static struct cpdma_control_info controls[] = { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 864 | [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO}, |
| 865 | [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW}, |
| 866 | [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW}, |
| 867 | [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW}, |
| 868 | [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW}, |
| 869 | [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO}, |
| 870 | [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW}, |
| 871 | [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW}, |
| 872 | [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW}, |
| 873 | [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW}, |
| 874 | [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW}, |
| 875 | }; |
| 876 | |
| 877 | int cpdma_control_get(struct cpdma_ctlr *ctlr, int control) |
| 878 | { |
| 879 | unsigned long flags; |
| 880 | struct cpdma_control_info *info = &controls[control]; |
| 881 | int ret; |
| 882 | |
| 883 | spin_lock_irqsave(&ctlr->lock, flags); |
| 884 | |
| 885 | ret = -ENOTSUPP; |
| 886 | if (!ctlr->params.has_ext_regs) |
| 887 | goto unlock_ret; |
| 888 | |
| 889 | ret = -EINVAL; |
| 890 | if (ctlr->state != CPDMA_STATE_ACTIVE) |
| 891 | goto unlock_ret; |
| 892 | |
| 893 | ret = -ENOENT; |
| 894 | if (control < 0 || control >= ARRAY_SIZE(controls)) |
| 895 | goto unlock_ret; |
| 896 | |
| 897 | ret = -EPERM; |
| 898 | if ((info->access & ACCESS_RO) != ACCESS_RO) |
| 899 | goto unlock_ret; |
| 900 | |
| 901 | ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask; |
| 902 | |
| 903 | unlock_ret: |
| 904 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 905 | return ret; |
| 906 | } |
| 907 | |
| 908 | int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value) |
| 909 | { |
| 910 | unsigned long flags; |
| 911 | struct cpdma_control_info *info = &controls[control]; |
| 912 | int ret; |
| 913 | u32 val; |
| 914 | |
| 915 | spin_lock_irqsave(&ctlr->lock, flags); |
| 916 | |
| 917 | ret = -ENOTSUPP; |
| 918 | if (!ctlr->params.has_ext_regs) |
| 919 | goto unlock_ret; |
| 920 | |
| 921 | ret = -EINVAL; |
| 922 | if (ctlr->state != CPDMA_STATE_ACTIVE) |
| 923 | goto unlock_ret; |
| 924 | |
| 925 | ret = -ENOENT; |
| 926 | if (control < 0 || control >= ARRAY_SIZE(controls)) |
| 927 | goto unlock_ret; |
| 928 | |
| 929 | ret = -EPERM; |
| 930 | if ((info->access & ACCESS_WO) != ACCESS_WO) |
| 931 | goto unlock_ret; |
| 932 | |
| 933 | val = dma_reg_read(ctlr, info->reg); |
| 934 | val &= ~(info->mask << info->shift); |
| 935 | val |= (value & info->mask) << info->shift; |
| 936 | dma_reg_write(ctlr, info->reg, val); |
| 937 | ret = 0; |
| 938 | |
| 939 | unlock_ret: |
| 940 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 941 | return ret; |
| 942 | } |
Arnd Bergmann | 6929e24 | 2013-02-14 17:53:01 +0100 | [diff] [blame] | 943 | EXPORT_SYMBOL_GPL(cpdma_control_set); |
Sebastian Siewior | 4bc21d4 | 2013-04-24 08:48:22 +0000 | [diff] [blame] | 944 | |
| 945 | MODULE_LICENSE("GPL"); |