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