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 |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 35 | #define CPDMA_TX_PRI0_RATE 0x30 |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 36 | #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 N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 65 | #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 N | 28a19fe | 2013-05-29 20:22:01 +0000 | [diff] [blame] | 68 | #define CPDMA_DESC_CRC_LEN 4 |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 69 | |
| 70 | #define CPDMA_TEARDOWN_VALUE 0xfffffffc |
| 71 | |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 72 | #define CPDMA_MAX_RLIM_CNT 16384 |
| 73 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 74 | struct 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 | |
| 86 | struct cpdma_desc_pool { |
Olof Johansson | c767db5 | 2013-12-11 15:51:20 -0800 | [diff] [blame] | 87 | phys_addr_t phys; |
Arnd Bergmann | 8409299 | 2016-01-29 12:39:10 +0100 | [diff] [blame] | 88 | dma_addr_t hw_addr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 89 | void __iomem *iomap; /* ioremap map */ |
| 90 | void *cpumap; /* dma_alloc map */ |
| 91 | int desc_size, mem_size; |
Grygorii Strashko | aeec302 | 2016-08-04 18:20:51 +0300 | [diff] [blame] | 92 | int num_desc; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 93 | struct device *dev; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 94 | struct gen_pool *gen_pool; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 95 | }; |
| 96 | |
| 97 | enum cpdma_state { |
| 98 | CPDMA_STATE_IDLE, |
| 99 | CPDMA_STATE_ACTIVE, |
| 100 | CPDMA_STATE_TEARDOWN, |
| 101 | }; |
| 102 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 103 | struct 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 Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 110 | int chan_num; |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 111 | int num_rx_desc; /* RX descriptors number */ |
| 112 | int num_tx_desc; /* TX descriptors number */ |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
| 115 | struct cpdma_chan { |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 116 | struct cpdma_desc __iomem *head, *tail; |
| 117 | void __iomem *hdp, *cp, *rxfree; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 118 | enum cpdma_state state; |
| 119 | struct cpdma_ctlr *ctlr; |
| 120 | int chan_num; |
| 121 | spinlock_t lock; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 122 | int count; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 123 | u32 desc_num; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 124 | u32 mask; |
| 125 | cpdma_handler_fn handler; |
| 126 | enum dma_data_direction dir; |
| 127 | struct cpdma_chan_stats stats; |
| 128 | /* offsets into dmaregs */ |
| 129 | int int_set, int_clear, td; |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 130 | int weight; |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 131 | u32 rate_factor; |
| 132 | u32 rate; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 133 | }; |
| 134 | |
Ivan Khoronzhuk | 991ddb1 | 2016-11-11 15:45:24 +0200 | [diff] [blame] | 135 | struct cpdma_control_info { |
| 136 | u32 reg; |
| 137 | u32 shift, mask; |
| 138 | int access; |
| 139 | #define ACCESS_RO BIT(0) |
| 140 | #define ACCESS_WO BIT(1) |
| 141 | #define ACCESS_RW (ACCESS_RO | ACCESS_WO) |
| 142 | }; |
| 143 | |
| 144 | static struct cpdma_control_info controls[] = { |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 145 | [CPDMA_TX_RLIM] = {CPDMA_DMACONTROL, 8, 0xffff, ACCESS_RW}, |
Ivan Khoronzhuk | 991ddb1 | 2016-11-11 15:45:24 +0200 | [diff] [blame] | 146 | [CPDMA_CMD_IDLE] = {CPDMA_DMACONTROL, 3, 1, ACCESS_WO}, |
| 147 | [CPDMA_COPY_ERROR_FRAMES] = {CPDMA_DMACONTROL, 4, 1, ACCESS_RW}, |
| 148 | [CPDMA_RX_OFF_LEN_UPDATE] = {CPDMA_DMACONTROL, 2, 1, ACCESS_RW}, |
| 149 | [CPDMA_RX_OWNERSHIP_FLIP] = {CPDMA_DMACONTROL, 1, 1, ACCESS_RW}, |
| 150 | [CPDMA_TX_PRIO_FIXED] = {CPDMA_DMACONTROL, 0, 1, ACCESS_RW}, |
| 151 | [CPDMA_STAT_IDLE] = {CPDMA_DMASTATUS, 31, 1, ACCESS_RO}, |
| 152 | [CPDMA_STAT_TX_ERR_CODE] = {CPDMA_DMASTATUS, 20, 0xf, ACCESS_RW}, |
| 153 | [CPDMA_STAT_TX_ERR_CHAN] = {CPDMA_DMASTATUS, 16, 0x7, ACCESS_RW}, |
| 154 | [CPDMA_STAT_RX_ERR_CODE] = {CPDMA_DMASTATUS, 12, 0xf, ACCESS_RW}, |
| 155 | [CPDMA_STAT_RX_ERR_CHAN] = {CPDMA_DMASTATUS, 8, 0x7, ACCESS_RW}, |
| 156 | [CPDMA_RX_BUFFER_OFFSET] = {CPDMA_RXBUFFOFS, 0, 0xffff, ACCESS_RW}, |
| 157 | }; |
| 158 | |
Ivan Khoronzhuk | 925d65e | 2016-08-22 21:18:27 +0300 | [diff] [blame] | 159 | #define tx_chan_num(chan) (chan) |
| 160 | #define rx_chan_num(chan) ((chan) + CPDMA_MAX_CHANNELS) |
| 161 | #define is_rx_chan(chan) ((chan)->chan_num >= CPDMA_MAX_CHANNELS) |
| 162 | #define is_tx_chan(chan) (!is_rx_chan(chan)) |
| 163 | #define __chan_linear(chan_num) ((chan_num) & (CPDMA_MAX_CHANNELS - 1)) |
| 164 | #define chan_linear(chan) __chan_linear((chan)->chan_num) |
| 165 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 166 | /* The following make access to common cpdma_ctlr params more readable */ |
| 167 | #define dmaregs params.dmaregs |
| 168 | #define num_chan params.num_chan |
| 169 | |
| 170 | /* various accessors */ |
Grygorii Strashko | a6c83cc | 2017-01-06 14:07:29 -0600 | [diff] [blame] | 171 | #define dma_reg_read(ctlr, ofs) readl((ctlr)->dmaregs + (ofs)) |
| 172 | #define chan_read(chan, fld) readl((chan)->fld) |
| 173 | #define desc_read(desc, fld) readl(&(desc)->fld) |
| 174 | #define dma_reg_write(ctlr, ofs, v) writel(v, (ctlr)->dmaregs + (ofs)) |
| 175 | #define chan_write(chan, fld, v) writel(v, (chan)->fld) |
| 176 | #define desc_write(desc, fld, v) writel((u32)(v), &(desc)->fld) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 177 | |
Mugunthan V N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 178 | #define cpdma_desc_to_port(chan, mode, directed) \ |
| 179 | do { \ |
| 180 | if (!is_rx_chan(chan) && ((directed == 1) || \ |
| 181 | (directed == 2))) \ |
| 182 | mode |= (CPDMA_DESC_TO_PORT_EN | \ |
| 183 | (directed << CPDMA_TO_PORT_SHIFT)); \ |
| 184 | } while (0) |
| 185 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 186 | static void cpdma_desc_pool_destroy(struct cpdma_ctlr *ctlr) |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 187 | { |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 188 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 189 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 190 | if (!pool) |
| 191 | return; |
| 192 | |
Grygorii Strashko | aeec302 | 2016-08-04 18:20:51 +0300 | [diff] [blame] | 193 | WARN(gen_pool_size(pool->gen_pool) != gen_pool_avail(pool->gen_pool), |
| 194 | "cpdma_desc_pool size %d != avail %d", |
| 195 | gen_pool_size(pool->gen_pool), |
| 196 | gen_pool_avail(pool->gen_pool)); |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 197 | if (pool->cpumap) |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 198 | dma_free_coherent(ctlr->dev, pool->mem_size, pool->cpumap, |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 199 | pool->phys); |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 200 | } |
| 201 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 202 | /* |
| 203 | * Utility constructs for a cpdma descriptor pool. Some devices (e.g. davinci |
| 204 | * emac) have dedicated on-chip memory for these descriptors. Some other |
| 205 | * devices (e.g. cpsw switches) use plain old memory. Descriptor pools |
| 206 | * abstract out these details |
| 207 | */ |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 208 | int cpdma_desc_pool_create(struct cpdma_ctlr *ctlr) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 209 | { |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 210 | struct cpdma_params *cpdma_params = &ctlr->params; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 211 | struct cpdma_desc_pool *pool; |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 212 | int ret = -ENOMEM; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 213 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 214 | pool = devm_kzalloc(ctlr->dev, sizeof(*pool), GFP_KERNEL); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 215 | if (!pool) |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 216 | goto gen_pool_create_fail; |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 217 | ctlr->pool = pool; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 218 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 219 | pool->mem_size = cpdma_params->desc_mem_size; |
| 220 | pool->desc_size = ALIGN(sizeof(struct cpdma_desc), |
| 221 | cpdma_params->desc_align); |
| 222 | pool->num_desc = pool->mem_size / pool->desc_size; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 223 | |
Grygorii Strashko | 90225bf | 2017-01-06 14:07:33 -0600 | [diff] [blame] | 224 | if (cpdma_params->descs_pool_size) { |
| 225 | /* recalculate memory size required cpdma descriptor pool |
| 226 | * basing on number of descriptors specified by user and |
| 227 | * if memory size > CPPI internal RAM size (desc_mem_size) |
| 228 | * then switch to use DDR |
| 229 | */ |
| 230 | pool->num_desc = cpdma_params->descs_pool_size; |
| 231 | pool->mem_size = pool->desc_size * pool->num_desc; |
| 232 | if (pool->mem_size > cpdma_params->desc_mem_size) |
| 233 | cpdma_params->desc_mem_phys = 0; |
| 234 | } |
| 235 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 236 | pool->gen_pool = devm_gen_pool_create(ctlr->dev, ilog2(pool->desc_size), |
| 237 | -1, "cpdma"); |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 238 | if (IS_ERR(pool->gen_pool)) { |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 239 | ret = PTR_ERR(pool->gen_pool); |
| 240 | dev_err(ctlr->dev, "pool create failed %d\n", ret); |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 241 | goto gen_pool_create_fail; |
| 242 | } |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 243 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 244 | if (cpdma_params->desc_mem_phys) { |
| 245 | pool->phys = cpdma_params->desc_mem_phys; |
Grygorii Strashko | 7f3b490 | 2017-01-06 14:07:32 -0600 | [diff] [blame] | 246 | pool->iomap = devm_ioremap(ctlr->dev, pool->phys, |
| 247 | pool->mem_size); |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 248 | pool->hw_addr = cpdma_params->desc_hw_addr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 249 | } else { |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 250 | pool->cpumap = dma_alloc_coherent(ctlr->dev, pool->mem_size, |
| 251 | &pool->hw_addr, GFP_KERNEL); |
Arnd Bergmann | 8409299 | 2016-01-29 12:39:10 +0100 | [diff] [blame] | 252 | pool->iomap = (void __iomem __force *)pool->cpumap; |
| 253 | 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] | 254 | } |
| 255 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 256 | if (!pool->iomap) |
| 257 | goto gen_pool_create_fail; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 258 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 259 | ret = gen_pool_add_virt(pool->gen_pool, (unsigned long)pool->iomap, |
| 260 | pool->phys, pool->mem_size, -1); |
| 261 | if (ret < 0) { |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 262 | dev_err(ctlr->dev, "pool add failed %d\n", ret); |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 263 | goto gen_pool_add_virt_fail; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 264 | } |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 265 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 266 | return 0; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 267 | |
| 268 | gen_pool_add_virt_fail: |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 269 | cpdma_desc_pool_destroy(ctlr); |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 270 | gen_pool_create_fail: |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 271 | ctlr->pool = NULL; |
| 272 | return ret; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 273 | } |
| 274 | |
| 275 | static inline dma_addr_t desc_phys(struct cpdma_desc_pool *pool, |
| 276 | struct cpdma_desc __iomem *desc) |
| 277 | { |
| 278 | if (!desc) |
| 279 | return 0; |
Olof Johansson | c767db5 | 2013-12-11 15:51:20 -0800 | [diff] [blame] | 280 | return pool->hw_addr + (__force long)desc - (__force long)pool->iomap; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | static inline struct cpdma_desc __iomem * |
| 284 | desc_from_phys(struct cpdma_desc_pool *pool, dma_addr_t dma) |
| 285 | { |
Sriram | 6a1fef6 | 2011-03-22 02:31:03 +0000 | [diff] [blame] | 286 | return dma ? pool->iomap + dma - pool->hw_addr : NULL; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 287 | } |
| 288 | |
| 289 | static struct cpdma_desc __iomem * |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 290 | cpdma_desc_alloc(struct cpdma_desc_pool *pool) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 291 | { |
Grygorii Strashko | aeec302 | 2016-08-04 18:20:51 +0300 | [diff] [blame] | 292 | return (struct cpdma_desc __iomem *) |
| 293 | gen_pool_alloc(pool->gen_pool, pool->desc_size); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 294 | } |
| 295 | |
| 296 | static void cpdma_desc_free(struct cpdma_desc_pool *pool, |
| 297 | struct cpdma_desc __iomem *desc, int num_desc) |
| 298 | { |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 299 | gen_pool_free(pool->gen_pool, (unsigned long)desc, pool->desc_size); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 300 | } |
| 301 | |
Ivan Khoronzhuk | 991ddb1 | 2016-11-11 15:45:24 +0200 | [diff] [blame] | 302 | static int _cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value) |
| 303 | { |
| 304 | struct cpdma_control_info *info = &controls[control]; |
| 305 | u32 val; |
| 306 | |
| 307 | if (!ctlr->params.has_ext_regs) |
| 308 | return -ENOTSUPP; |
| 309 | |
| 310 | if (ctlr->state != CPDMA_STATE_ACTIVE) |
| 311 | return -EINVAL; |
| 312 | |
| 313 | if (control < 0 || control >= ARRAY_SIZE(controls)) |
| 314 | return -ENOENT; |
| 315 | |
| 316 | if ((info->access & ACCESS_WO) != ACCESS_WO) |
| 317 | return -EPERM; |
| 318 | |
| 319 | val = dma_reg_read(ctlr, info->reg); |
| 320 | val &= ~(info->mask << info->shift); |
| 321 | val |= (value & info->mask) << info->shift; |
| 322 | dma_reg_write(ctlr, info->reg, val); |
| 323 | |
| 324 | return 0; |
| 325 | } |
| 326 | |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 327 | static int _cpdma_control_get(struct cpdma_ctlr *ctlr, int control) |
| 328 | { |
| 329 | struct cpdma_control_info *info = &controls[control]; |
| 330 | int ret; |
| 331 | |
| 332 | if (!ctlr->params.has_ext_regs) |
| 333 | return -ENOTSUPP; |
| 334 | |
| 335 | if (ctlr->state != CPDMA_STATE_ACTIVE) |
| 336 | return -EINVAL; |
| 337 | |
| 338 | if (control < 0 || control >= ARRAY_SIZE(controls)) |
| 339 | return -ENOENT; |
| 340 | |
| 341 | if ((info->access & ACCESS_RO) != ACCESS_RO) |
| 342 | return -EPERM; |
| 343 | |
| 344 | ret = (dma_reg_read(ctlr, info->reg) >> info->shift) & info->mask; |
| 345 | return ret; |
| 346 | } |
| 347 | |
| 348 | /* cpdma_chan_set_chan_shaper - set shaper for a channel |
| 349 | * Has to be called under ctlr lock |
| 350 | */ |
| 351 | static int cpdma_chan_set_chan_shaper(struct cpdma_chan *chan) |
| 352 | { |
| 353 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 354 | u32 rate_reg; |
| 355 | u32 rmask; |
| 356 | int ret; |
| 357 | |
| 358 | if (!chan->rate) |
| 359 | return 0; |
| 360 | |
| 361 | rate_reg = CPDMA_TX_PRI0_RATE + 4 * chan->chan_num; |
| 362 | dma_reg_write(ctlr, rate_reg, chan->rate_factor); |
| 363 | |
| 364 | rmask = _cpdma_control_get(ctlr, CPDMA_TX_RLIM); |
| 365 | rmask |= chan->mask; |
| 366 | |
| 367 | ret = _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask); |
| 368 | return ret; |
| 369 | } |
| 370 | |
| 371 | static int cpdma_chan_on(struct cpdma_chan *chan) |
| 372 | { |
| 373 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 374 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 375 | unsigned long flags; |
| 376 | |
| 377 | spin_lock_irqsave(&chan->lock, flags); |
| 378 | if (chan->state != CPDMA_STATE_IDLE) { |
| 379 | spin_unlock_irqrestore(&chan->lock, flags); |
| 380 | return -EBUSY; |
| 381 | } |
| 382 | if (ctlr->state != CPDMA_STATE_ACTIVE) { |
| 383 | spin_unlock_irqrestore(&chan->lock, flags); |
| 384 | return -EINVAL; |
| 385 | } |
| 386 | dma_reg_write(ctlr, chan->int_set, chan->mask); |
| 387 | chan->state = CPDMA_STATE_ACTIVE; |
| 388 | if (chan->head) { |
| 389 | chan_write(chan, hdp, desc_phys(pool, chan->head)); |
| 390 | if (chan->rxfree) |
| 391 | chan_write(chan, rxfree, chan->count); |
| 392 | } |
| 393 | |
| 394 | spin_unlock_irqrestore(&chan->lock, flags); |
| 395 | return 0; |
| 396 | } |
| 397 | |
| 398 | /* cpdma_chan_fit_rate - set rate for a channel and check if it's possible. |
| 399 | * rmask - mask of rate limited channels |
| 400 | * Returns min rate in Kb/s |
| 401 | */ |
| 402 | static int cpdma_chan_fit_rate(struct cpdma_chan *ch, u32 rate, |
| 403 | u32 *rmask, int *prio_mode) |
| 404 | { |
| 405 | struct cpdma_ctlr *ctlr = ch->ctlr; |
| 406 | struct cpdma_chan *chan; |
| 407 | u32 old_rate = ch->rate; |
| 408 | u32 new_rmask = 0; |
| 409 | int rlim = 1; |
| 410 | int i; |
| 411 | |
| 412 | *prio_mode = 0; |
| 413 | for (i = tx_chan_num(0); i < tx_chan_num(CPDMA_MAX_CHANNELS); i++) { |
| 414 | chan = ctlr->channels[i]; |
| 415 | if (!chan) { |
| 416 | rlim = 0; |
| 417 | continue; |
| 418 | } |
| 419 | |
| 420 | if (chan == ch) |
| 421 | chan->rate = rate; |
| 422 | |
| 423 | if (chan->rate) { |
| 424 | if (rlim) { |
| 425 | new_rmask |= chan->mask; |
| 426 | } else { |
| 427 | ch->rate = old_rate; |
| 428 | dev_err(ctlr->dev, "Prev channel of %dch is not rate limited\n", |
| 429 | chan->chan_num); |
| 430 | return -EINVAL; |
| 431 | } |
| 432 | } else { |
| 433 | *prio_mode = 1; |
| 434 | rlim = 0; |
| 435 | } |
| 436 | } |
| 437 | |
| 438 | *rmask = new_rmask; |
| 439 | return 0; |
| 440 | } |
| 441 | |
| 442 | static u32 cpdma_chan_set_factors(struct cpdma_ctlr *ctlr, |
| 443 | struct cpdma_chan *ch) |
| 444 | { |
| 445 | u32 delta = UINT_MAX, prev_delta = UINT_MAX, best_delta = UINT_MAX; |
| 446 | u32 best_send_cnt = 0, best_idle_cnt = 0; |
| 447 | u32 new_rate, best_rate = 0, rate_reg; |
| 448 | u64 send_cnt, idle_cnt; |
| 449 | u32 min_send_cnt, freq; |
| 450 | u64 divident, divisor; |
| 451 | |
| 452 | if (!ch->rate) { |
| 453 | ch->rate_factor = 0; |
| 454 | goto set_factor; |
| 455 | } |
| 456 | |
| 457 | freq = ctlr->params.bus_freq_mhz * 1000 * 32; |
| 458 | if (!freq) { |
| 459 | dev_err(ctlr->dev, "The bus frequency is not set\n"); |
| 460 | return -EINVAL; |
| 461 | } |
| 462 | |
| 463 | min_send_cnt = freq - ch->rate; |
| 464 | send_cnt = DIV_ROUND_UP(min_send_cnt, ch->rate); |
| 465 | while (send_cnt <= CPDMA_MAX_RLIM_CNT) { |
| 466 | divident = ch->rate * send_cnt; |
| 467 | divisor = min_send_cnt; |
| 468 | idle_cnt = DIV_ROUND_CLOSEST_ULL(divident, divisor); |
| 469 | |
| 470 | divident = freq * idle_cnt; |
| 471 | divisor = idle_cnt + send_cnt; |
| 472 | new_rate = DIV_ROUND_CLOSEST_ULL(divident, divisor); |
| 473 | |
| 474 | delta = new_rate >= ch->rate ? new_rate - ch->rate : delta; |
| 475 | if (delta < best_delta) { |
| 476 | best_delta = delta; |
| 477 | best_send_cnt = send_cnt; |
| 478 | best_idle_cnt = idle_cnt; |
| 479 | best_rate = new_rate; |
| 480 | |
| 481 | if (!delta) |
| 482 | break; |
| 483 | } |
| 484 | |
| 485 | if (prev_delta >= delta) { |
| 486 | prev_delta = delta; |
| 487 | send_cnt++; |
| 488 | continue; |
| 489 | } |
| 490 | |
| 491 | idle_cnt++; |
| 492 | divident = freq * idle_cnt; |
| 493 | send_cnt = DIV_ROUND_CLOSEST_ULL(divident, ch->rate); |
| 494 | send_cnt -= idle_cnt; |
| 495 | prev_delta = UINT_MAX; |
| 496 | } |
| 497 | |
| 498 | ch->rate = best_rate; |
| 499 | ch->rate_factor = best_send_cnt | (best_idle_cnt << 16); |
| 500 | |
| 501 | set_factor: |
| 502 | rate_reg = CPDMA_TX_PRI0_RATE + 4 * ch->chan_num; |
| 503 | dma_reg_write(ctlr, rate_reg, ch->rate_factor); |
| 504 | return 0; |
| 505 | } |
| 506 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 507 | struct cpdma_ctlr *cpdma_ctlr_create(struct cpdma_params *params) |
| 508 | { |
| 509 | struct cpdma_ctlr *ctlr; |
| 510 | |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 511 | ctlr = devm_kzalloc(params->dev, sizeof(*ctlr), GFP_KERNEL); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 512 | if (!ctlr) |
| 513 | return NULL; |
| 514 | |
| 515 | ctlr->state = CPDMA_STATE_IDLE; |
| 516 | ctlr->params = *params; |
| 517 | ctlr->dev = params->dev; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 518 | ctlr->chan_num = 0; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 519 | spin_lock_init(&ctlr->lock); |
| 520 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 521 | if (cpdma_desc_pool_create(ctlr)) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 522 | return NULL; |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 523 | /* split pool equally between RX/TX by default */ |
| 524 | ctlr->num_tx_desc = ctlr->pool->num_desc / 2; |
| 525 | ctlr->num_rx_desc = ctlr->pool->num_desc - ctlr->num_tx_desc; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 526 | |
| 527 | if (WARN_ON(ctlr->num_chan > CPDMA_MAX_CHANNELS)) |
| 528 | ctlr->num_chan = CPDMA_MAX_CHANNELS; |
| 529 | return ctlr; |
| 530 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 531 | EXPORT_SYMBOL_GPL(cpdma_ctlr_create); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 532 | |
| 533 | int cpdma_ctlr_start(struct cpdma_ctlr *ctlr) |
| 534 | { |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 535 | struct cpdma_chan *chan; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 536 | unsigned long flags; |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 537 | int i, prio_mode; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 538 | |
| 539 | spin_lock_irqsave(&ctlr->lock, flags); |
| 540 | if (ctlr->state != CPDMA_STATE_IDLE) { |
| 541 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 542 | return -EBUSY; |
| 543 | } |
| 544 | |
| 545 | if (ctlr->params.has_soft_reset) { |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 546 | unsigned timeout = 10 * 100; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 547 | |
| 548 | dma_reg_write(ctlr, CPDMA_SOFTRESET, 1); |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 549 | while (timeout) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 550 | if (dma_reg_read(ctlr, CPDMA_SOFTRESET) == 0) |
| 551 | break; |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 552 | udelay(10); |
| 553 | timeout--; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 554 | } |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 555 | WARN_ON(!timeout); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | for (i = 0; i < ctlr->num_chan; i++) { |
Grygorii Strashko | a6c83cc | 2017-01-06 14:07:29 -0600 | [diff] [blame] | 559 | writel(0, ctlr->params.txhdp + 4 * i); |
| 560 | writel(0, ctlr->params.rxhdp + 4 * i); |
| 561 | writel(0, ctlr->params.txcp + 4 * i); |
| 562 | writel(0, ctlr->params.rxcp + 4 * i); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 563 | } |
| 564 | |
| 565 | dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff); |
| 566 | dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff); |
| 567 | |
| 568 | dma_reg_write(ctlr, CPDMA_TXCONTROL, 1); |
| 569 | dma_reg_write(ctlr, CPDMA_RXCONTROL, 1); |
| 570 | |
| 571 | ctlr->state = CPDMA_STATE_ACTIVE; |
| 572 | |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 573 | prio_mode = 0; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 574 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 575 | chan = ctlr->channels[i]; |
| 576 | if (chan) { |
| 577 | cpdma_chan_set_chan_shaper(chan); |
| 578 | cpdma_chan_on(chan); |
| 579 | |
| 580 | /* off prio mode if all tx channels are rate limited */ |
| 581 | if (is_tx_chan(chan) && !chan->rate) |
| 582 | prio_mode = 1; |
| 583 | } |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 584 | } |
Ivan Khoronzhuk | 991ddb1 | 2016-11-11 15:45:24 +0200 | [diff] [blame] | 585 | |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 586 | _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode); |
Ivan Khoronzhuk | 991ddb1 | 2016-11-11 15:45:24 +0200 | [diff] [blame] | 587 | _cpdma_control_set(ctlr, CPDMA_RX_BUFFER_OFFSET, 0); |
| 588 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 589 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 590 | return 0; |
| 591 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 592 | EXPORT_SYMBOL_GPL(cpdma_ctlr_start); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 593 | |
| 594 | int cpdma_ctlr_stop(struct cpdma_ctlr *ctlr) |
| 595 | { |
| 596 | unsigned long flags; |
| 597 | int i; |
| 598 | |
| 599 | spin_lock_irqsave(&ctlr->lock, flags); |
Ivan Khoronzhuk | b993eec | 2016-11-11 16:10:47 +0200 | [diff] [blame] | 600 | if (ctlr->state != CPDMA_STATE_ACTIVE) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 601 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 602 | return -EINVAL; |
| 603 | } |
| 604 | |
| 605 | ctlr->state = CPDMA_STATE_TEARDOWN; |
Ivan Khoronzhuk | 080d5c5a | 2016-08-22 21:18:25 +0300 | [diff] [blame] | 606 | spin_unlock_irqrestore(&ctlr->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 607 | |
| 608 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
| 609 | if (ctlr->channels[i]) |
| 610 | cpdma_chan_stop(ctlr->channels[i]); |
| 611 | } |
| 612 | |
Ivan Khoronzhuk | 080d5c5a | 2016-08-22 21:18:25 +0300 | [diff] [blame] | 613 | spin_lock_irqsave(&ctlr->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 614 | dma_reg_write(ctlr, CPDMA_RXINTMASKCLEAR, 0xffffffff); |
| 615 | dma_reg_write(ctlr, CPDMA_TXINTMASKCLEAR, 0xffffffff); |
| 616 | |
| 617 | dma_reg_write(ctlr, CPDMA_TXCONTROL, 0); |
| 618 | dma_reg_write(ctlr, CPDMA_RXCONTROL, 0); |
| 619 | |
| 620 | ctlr->state = CPDMA_STATE_IDLE; |
| 621 | |
| 622 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 623 | return 0; |
| 624 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 625 | EXPORT_SYMBOL_GPL(cpdma_ctlr_stop); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 626 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 627 | int cpdma_ctlr_destroy(struct cpdma_ctlr *ctlr) |
| 628 | { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 629 | int ret = 0, i; |
| 630 | |
| 631 | if (!ctlr) |
| 632 | return -EINVAL; |
| 633 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 634 | if (ctlr->state != CPDMA_STATE_IDLE) |
| 635 | cpdma_ctlr_stop(ctlr); |
| 636 | |
Cyril Roelandt | 79876e0 | 2013-02-12 12:52:30 +0000 | [diff] [blame] | 637 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) |
| 638 | cpdma_chan_destroy(ctlr->channels[i]); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 639 | |
Grygorii Strashko | 5fcc40a | 2017-01-06 14:07:31 -0600 | [diff] [blame] | 640 | cpdma_desc_pool_destroy(ctlr); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 641 | return ret; |
| 642 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 643 | EXPORT_SYMBOL_GPL(cpdma_ctlr_destroy); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 644 | |
| 645 | int cpdma_ctlr_int_ctrl(struct cpdma_ctlr *ctlr, bool enable) |
| 646 | { |
| 647 | unsigned long flags; |
| 648 | int i, reg; |
| 649 | |
| 650 | spin_lock_irqsave(&ctlr->lock, flags); |
| 651 | if (ctlr->state != CPDMA_STATE_ACTIVE) { |
| 652 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 653 | return -EINVAL; |
| 654 | } |
| 655 | |
| 656 | reg = enable ? CPDMA_DMAINTMASKSET : CPDMA_DMAINTMASKCLEAR; |
| 657 | dma_reg_write(ctlr, reg, CPDMA_DMAINT_HOSTERR); |
| 658 | |
| 659 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
| 660 | if (ctlr->channels[i]) |
| 661 | cpdma_chan_int_ctrl(ctlr->channels[i], enable); |
| 662 | } |
| 663 | |
| 664 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 665 | return 0; |
| 666 | } |
Arnd Bergmann | 6929e24 | 2013-02-14 17:53:01 +0100 | [diff] [blame] | 667 | EXPORT_SYMBOL_GPL(cpdma_ctlr_int_ctrl); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 668 | |
Mugunthan V N | 510a1e72 | 2013-02-17 22:19:20 +0000 | [diff] [blame] | 669 | void cpdma_ctlr_eoi(struct cpdma_ctlr *ctlr, u32 value) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 670 | { |
Mugunthan V N | 510a1e72 | 2013-02-17 22:19:20 +0000 | [diff] [blame] | 671 | dma_reg_write(ctlr, CPDMA_MACEOIVECTOR, value); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 672 | } |
Arnd Bergmann | 6929e24 | 2013-02-14 17:53:01 +0100 | [diff] [blame] | 673 | EXPORT_SYMBOL_GPL(cpdma_ctlr_eoi); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 674 | |
Ivan Khoronzhuk | e05107e | 2016-08-22 21:18:26 +0300 | [diff] [blame] | 675 | u32 cpdma_ctrl_rxchs_state(struct cpdma_ctlr *ctlr) |
| 676 | { |
| 677 | return dma_reg_read(ctlr, CPDMA_RXINTSTATMASKED); |
| 678 | } |
| 679 | EXPORT_SYMBOL_GPL(cpdma_ctrl_rxchs_state); |
| 680 | |
| 681 | u32 cpdma_ctrl_txchs_state(struct cpdma_ctlr *ctlr) |
| 682 | { |
| 683 | return dma_reg_read(ctlr, CPDMA_TXINTSTATMASKED); |
| 684 | } |
| 685 | EXPORT_SYMBOL_GPL(cpdma_ctrl_txchs_state); |
| 686 | |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 687 | static void cpdma_chan_set_descs(struct cpdma_ctlr *ctlr, |
| 688 | int rx, int desc_num, |
| 689 | int per_ch_desc) |
| 690 | { |
| 691 | struct cpdma_chan *chan, *most_chan = NULL; |
| 692 | int desc_cnt = desc_num; |
| 693 | int most_dnum = 0; |
| 694 | int min, max, i; |
| 695 | |
| 696 | if (!desc_num) |
| 697 | return; |
| 698 | |
| 699 | if (rx) { |
| 700 | min = rx_chan_num(0); |
| 701 | max = rx_chan_num(CPDMA_MAX_CHANNELS); |
| 702 | } else { |
| 703 | min = tx_chan_num(0); |
| 704 | max = tx_chan_num(CPDMA_MAX_CHANNELS); |
| 705 | } |
| 706 | |
| 707 | for (i = min; i < max; i++) { |
| 708 | chan = ctlr->channels[i]; |
| 709 | if (!chan) |
| 710 | continue; |
| 711 | |
| 712 | if (chan->weight) |
| 713 | chan->desc_num = (chan->weight * desc_num) / 100; |
| 714 | else |
| 715 | chan->desc_num = per_ch_desc; |
| 716 | |
| 717 | desc_cnt -= chan->desc_num; |
| 718 | |
| 719 | if (most_dnum < chan->desc_num) { |
| 720 | most_dnum = chan->desc_num; |
| 721 | most_chan = chan; |
| 722 | } |
| 723 | } |
| 724 | /* use remains */ |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 725 | if (most_chan) |
| 726 | most_chan->desc_num += desc_cnt; |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 727 | } |
| 728 | |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 729 | /** |
| 730 | * cpdma_chan_split_pool - Splits ctrl pool between all channels. |
| 731 | * Has to be called under ctlr lock |
| 732 | */ |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 733 | int cpdma_chan_split_pool(struct cpdma_ctlr *ctlr) |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 734 | { |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 735 | int tx_per_ch_desc = 0, rx_per_ch_desc = 0; |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 736 | int free_rx_num = 0, free_tx_num = 0; |
| 737 | int rx_weight = 0, tx_weight = 0; |
| 738 | int tx_desc_num, rx_desc_num; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 739 | struct cpdma_chan *chan; |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 740 | int i; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 741 | |
| 742 | if (!ctlr->chan_num) |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 743 | return 0; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 744 | |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 745 | for (i = 0; i < ARRAY_SIZE(ctlr->channels); i++) { |
| 746 | chan = ctlr->channels[i]; |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 747 | if (!chan) |
| 748 | continue; |
| 749 | |
| 750 | if (is_rx_chan(chan)) { |
| 751 | if (!chan->weight) |
| 752 | free_rx_num++; |
| 753 | rx_weight += chan->weight; |
| 754 | } else { |
| 755 | if (!chan->weight) |
| 756 | free_tx_num++; |
| 757 | tx_weight += chan->weight; |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 758 | } |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 759 | } |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 760 | |
| 761 | if (rx_weight > 100 || tx_weight > 100) |
| 762 | return -EINVAL; |
| 763 | |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 764 | tx_desc_num = ctlr->num_tx_desc; |
| 765 | rx_desc_num = ctlr->num_rx_desc; |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 766 | |
| 767 | if (free_tx_num) { |
| 768 | tx_per_ch_desc = tx_desc_num - (tx_weight * tx_desc_num) / 100; |
| 769 | tx_per_ch_desc /= free_tx_num; |
| 770 | } |
| 771 | if (free_rx_num) { |
| 772 | rx_per_ch_desc = rx_desc_num - (rx_weight * rx_desc_num) / 100; |
| 773 | rx_per_ch_desc /= free_rx_num; |
| 774 | } |
| 775 | |
| 776 | cpdma_chan_set_descs(ctlr, 0, tx_desc_num, tx_per_ch_desc); |
| 777 | cpdma_chan_set_descs(ctlr, 1, rx_desc_num, rx_per_ch_desc); |
| 778 | |
| 779 | return 0; |
| 780 | } |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 781 | EXPORT_SYMBOL_GPL(cpdma_chan_split_pool); |
| 782 | |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 783 | |
| 784 | /* cpdma_chan_set_weight - set weight of a channel in percentage. |
| 785 | * Tx and Rx channels have separate weights. That is 100% for RX |
| 786 | * and 100% for Tx. The weight is used to split cpdma resources |
| 787 | * in correct proportion required by the channels, including number |
| 788 | * of descriptors. The channel rate is not enough to know the |
| 789 | * weight of a channel as the maximum rate of an interface is needed. |
| 790 | * If weight = 0, then channel uses rest of descriptors leaved by |
| 791 | * weighted channels. |
| 792 | */ |
| 793 | int cpdma_chan_set_weight(struct cpdma_chan *ch, int weight) |
| 794 | { |
| 795 | struct cpdma_ctlr *ctlr = ch->ctlr; |
| 796 | unsigned long flags, ch_flags; |
| 797 | int ret; |
| 798 | |
| 799 | spin_lock_irqsave(&ctlr->lock, flags); |
| 800 | spin_lock_irqsave(&ch->lock, ch_flags); |
| 801 | if (ch->weight == weight) { |
| 802 | spin_unlock_irqrestore(&ch->lock, ch_flags); |
| 803 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 804 | return 0; |
| 805 | } |
| 806 | ch->weight = weight; |
| 807 | spin_unlock_irqrestore(&ch->lock, ch_flags); |
| 808 | |
| 809 | /* re-split pool using new channel weight */ |
| 810 | ret = cpdma_chan_split_pool(ctlr); |
| 811 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 812 | return ret; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 813 | } |
Paul Gortmaker | 397c5ad | 2016-12-01 15:25:28 -0500 | [diff] [blame] | 814 | EXPORT_SYMBOL_GPL(cpdma_chan_set_weight); |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 815 | |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 816 | /* cpdma_chan_get_min_rate - get minimum allowed rate for channel |
| 817 | * Should be called before cpdma_chan_set_rate. |
| 818 | * Returns min rate in Kb/s |
| 819 | */ |
| 820 | u32 cpdma_chan_get_min_rate(struct cpdma_ctlr *ctlr) |
| 821 | { |
| 822 | unsigned int divident, divisor; |
| 823 | |
| 824 | divident = ctlr->params.bus_freq_mhz * 32 * 1000; |
| 825 | divisor = 1 + CPDMA_MAX_RLIM_CNT; |
| 826 | |
| 827 | return DIV_ROUND_UP(divident, divisor); |
| 828 | } |
Paul Gortmaker | 397c5ad | 2016-12-01 15:25:28 -0500 | [diff] [blame] | 829 | EXPORT_SYMBOL_GPL(cpdma_chan_get_min_rate); |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 830 | |
| 831 | /* cpdma_chan_set_rate - limits bandwidth for transmit channel. |
| 832 | * The bandwidth * limited channels have to be in order beginning from lowest. |
| 833 | * ch - transmit channel the bandwidth is configured for |
| 834 | * rate - bandwidth in Kb/s, if 0 - then off shaper |
| 835 | */ |
| 836 | int cpdma_chan_set_rate(struct cpdma_chan *ch, u32 rate) |
| 837 | { |
| 838 | struct cpdma_ctlr *ctlr = ch->ctlr; |
| 839 | unsigned long flags, ch_flags; |
| 840 | int ret, prio_mode; |
| 841 | u32 rmask; |
| 842 | |
| 843 | if (!ch || !is_tx_chan(ch)) |
| 844 | return -EINVAL; |
| 845 | |
| 846 | if (ch->rate == rate) |
| 847 | return rate; |
| 848 | |
| 849 | spin_lock_irqsave(&ctlr->lock, flags); |
| 850 | spin_lock_irqsave(&ch->lock, ch_flags); |
| 851 | |
| 852 | ret = cpdma_chan_fit_rate(ch, rate, &rmask, &prio_mode); |
| 853 | if (ret) |
| 854 | goto err; |
| 855 | |
| 856 | ret = cpdma_chan_set_factors(ctlr, ch); |
| 857 | if (ret) |
| 858 | goto err; |
| 859 | |
| 860 | spin_unlock_irqrestore(&ch->lock, ch_flags); |
| 861 | |
| 862 | /* on shapers */ |
| 863 | _cpdma_control_set(ctlr, CPDMA_TX_RLIM, rmask); |
| 864 | _cpdma_control_set(ctlr, CPDMA_TX_PRIO_FIXED, prio_mode); |
| 865 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 866 | return ret; |
| 867 | |
| 868 | err: |
| 869 | spin_unlock_irqrestore(&ch->lock, ch_flags); |
| 870 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 871 | return ret; |
| 872 | } |
Paul Gortmaker | 397c5ad | 2016-12-01 15:25:28 -0500 | [diff] [blame] | 873 | EXPORT_SYMBOL_GPL(cpdma_chan_set_rate); |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 874 | |
| 875 | u32 cpdma_chan_get_rate(struct cpdma_chan *ch) |
| 876 | { |
| 877 | unsigned long flags; |
| 878 | u32 rate; |
| 879 | |
| 880 | spin_lock_irqsave(&ch->lock, flags); |
| 881 | rate = ch->rate; |
| 882 | spin_unlock_irqrestore(&ch->lock, flags); |
| 883 | |
| 884 | return rate; |
| 885 | } |
Paul Gortmaker | 397c5ad | 2016-12-01 15:25:28 -0500 | [diff] [blame] | 886 | EXPORT_SYMBOL_GPL(cpdma_chan_get_rate); |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 887 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 888 | struct cpdma_chan *cpdma_chan_create(struct cpdma_ctlr *ctlr, int chan_num, |
Ivan Khoronzhuk | 925d65e | 2016-08-22 21:18:27 +0300 | [diff] [blame] | 889 | cpdma_handler_fn handler, int rx_type) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 890 | { |
Ivan Khoronzhuk | 925d65e | 2016-08-22 21:18:27 +0300 | [diff] [blame] | 891 | int offset = chan_num * 4; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 892 | struct cpdma_chan *chan; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 893 | unsigned long flags; |
| 894 | |
Ivan Khoronzhuk | 925d65e | 2016-08-22 21:18:27 +0300 | [diff] [blame] | 895 | chan_num = rx_type ? rx_chan_num(chan_num) : tx_chan_num(chan_num); |
| 896 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 897 | if (__chan_linear(chan_num) >= ctlr->num_chan) |
| 898 | return NULL; |
| 899 | |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 900 | chan = devm_kzalloc(ctlr->dev, sizeof(*chan), GFP_KERNEL); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 901 | if (!chan) |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 902 | return ERR_PTR(-ENOMEM); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 903 | |
| 904 | spin_lock_irqsave(&ctlr->lock, flags); |
George Cherian | e194312 | 2014-05-12 10:21:21 +0530 | [diff] [blame] | 905 | if (ctlr->channels[chan_num]) { |
| 906 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 907 | devm_kfree(ctlr->dev, chan); |
| 908 | return ERR_PTR(-EBUSY); |
| 909 | } |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 910 | |
| 911 | chan->ctlr = ctlr; |
| 912 | chan->state = CPDMA_STATE_IDLE; |
| 913 | chan->chan_num = chan_num; |
| 914 | chan->handler = handler; |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 915 | chan->rate = 0; |
Ivan Khoronzhuk | 0fc6432 | 2016-11-29 17:00:47 +0200 | [diff] [blame] | 916 | chan->weight = 0; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 917 | |
| 918 | if (is_rx_chan(chan)) { |
| 919 | chan->hdp = ctlr->params.rxhdp + offset; |
| 920 | chan->cp = ctlr->params.rxcp + offset; |
| 921 | chan->rxfree = ctlr->params.rxfree + offset; |
| 922 | chan->int_set = CPDMA_RXINTMASKSET; |
| 923 | chan->int_clear = CPDMA_RXINTMASKCLEAR; |
| 924 | chan->td = CPDMA_RXTEARDOWN; |
| 925 | chan->dir = DMA_FROM_DEVICE; |
| 926 | } else { |
| 927 | chan->hdp = ctlr->params.txhdp + offset; |
| 928 | chan->cp = ctlr->params.txcp + offset; |
| 929 | chan->int_set = CPDMA_TXINTMASKSET; |
| 930 | chan->int_clear = CPDMA_TXINTMASKCLEAR; |
| 931 | chan->td = CPDMA_TXTEARDOWN; |
| 932 | chan->dir = DMA_TO_DEVICE; |
| 933 | } |
| 934 | chan->mask = BIT(chan_linear(chan)); |
| 935 | |
| 936 | spin_lock_init(&chan->lock); |
| 937 | |
| 938 | ctlr->channels[chan_num] = chan; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 939 | ctlr->chan_num++; |
| 940 | |
| 941 | cpdma_chan_split_pool(ctlr); |
| 942 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 943 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 944 | return chan; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 945 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 946 | EXPORT_SYMBOL_GPL(cpdma_chan_create); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 947 | |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 948 | int cpdma_chan_get_rx_buf_num(struct cpdma_chan *chan) |
Ivan Khoronzhuk | 1793331 | 2016-06-17 13:25:39 +0300 | [diff] [blame] | 949 | { |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 950 | unsigned long flags; |
| 951 | int desc_num; |
| 952 | |
| 953 | spin_lock_irqsave(&chan->lock, flags); |
| 954 | desc_num = chan->desc_num; |
| 955 | spin_unlock_irqrestore(&chan->lock, flags); |
| 956 | |
| 957 | return desc_num; |
Ivan Khoronzhuk | 1793331 | 2016-06-17 13:25:39 +0300 | [diff] [blame] | 958 | } |
| 959 | EXPORT_SYMBOL_GPL(cpdma_chan_get_rx_buf_num); |
| 960 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 961 | int cpdma_chan_destroy(struct cpdma_chan *chan) |
| 962 | { |
Julia Lawall | f37c54b | 2012-08-14 05:49:47 +0000 | [diff] [blame] | 963 | struct cpdma_ctlr *ctlr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 964 | unsigned long flags; |
| 965 | |
| 966 | if (!chan) |
| 967 | return -EINVAL; |
Julia Lawall | f37c54b | 2012-08-14 05:49:47 +0000 | [diff] [blame] | 968 | ctlr = chan->ctlr; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 969 | |
| 970 | spin_lock_irqsave(&ctlr->lock, flags); |
| 971 | if (chan->state != CPDMA_STATE_IDLE) |
| 972 | cpdma_chan_stop(chan); |
| 973 | ctlr->channels[chan->chan_num] = NULL; |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 974 | ctlr->chan_num--; |
Ivan Khoronzhuk | b602e49 | 2016-11-08 15:16:05 +0200 | [diff] [blame] | 975 | devm_kfree(ctlr->dev, chan); |
Ivan Khoronzhuk | 3802dce1 | 2016-08-22 21:18:24 +0300 | [diff] [blame] | 976 | cpdma_chan_split_pool(ctlr); |
| 977 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 978 | spin_unlock_irqrestore(&ctlr->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 979 | return 0; |
| 980 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 981 | EXPORT_SYMBOL_GPL(cpdma_chan_destroy); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 982 | |
| 983 | int cpdma_chan_get_stats(struct cpdma_chan *chan, |
| 984 | struct cpdma_chan_stats *stats) |
| 985 | { |
| 986 | unsigned long flags; |
| 987 | if (!chan) |
| 988 | return -EINVAL; |
| 989 | spin_lock_irqsave(&chan->lock, flags); |
| 990 | memcpy(stats, &chan->stats, sizeof(*stats)); |
| 991 | spin_unlock_irqrestore(&chan->lock, flags); |
| 992 | return 0; |
| 993 | } |
Daniel Mack | 0ca04b6 | 2013-08-22 13:47:00 +0200 | [diff] [blame] | 994 | EXPORT_SYMBOL_GPL(cpdma_chan_get_stats); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 995 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 996 | static void __cpdma_chan_submit(struct cpdma_chan *chan, |
| 997 | struct cpdma_desc __iomem *desc) |
| 998 | { |
| 999 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 1000 | struct cpdma_desc __iomem *prev = chan->tail; |
| 1001 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 1002 | dma_addr_t desc_dma; |
| 1003 | u32 mode; |
| 1004 | |
| 1005 | desc_dma = desc_phys(pool, desc); |
| 1006 | |
| 1007 | /* simple case - idle channel */ |
| 1008 | if (!chan->head) { |
| 1009 | chan->stats.head_enqueue++; |
| 1010 | chan->head = desc; |
| 1011 | chan->tail = desc; |
| 1012 | if (chan->state == CPDMA_STATE_ACTIVE) |
| 1013 | chan_write(chan, hdp, desc_dma); |
| 1014 | return; |
| 1015 | } |
| 1016 | |
| 1017 | /* first chain the descriptor at the tail of the list */ |
| 1018 | desc_write(prev, hw_next, desc_dma); |
| 1019 | chan->tail = desc; |
| 1020 | chan->stats.tail_enqueue++; |
| 1021 | |
| 1022 | /* next check if EOQ has been triggered already */ |
| 1023 | mode = desc_read(prev, hw_mode); |
| 1024 | if (((mode & (CPDMA_DESC_EOQ | CPDMA_DESC_OWNER)) == CPDMA_DESC_EOQ) && |
| 1025 | (chan->state == CPDMA_STATE_ACTIVE)) { |
| 1026 | desc_write(prev, hw_mode, mode & ~CPDMA_DESC_EOQ); |
| 1027 | chan_write(chan, hdp, desc_dma); |
| 1028 | chan->stats.misqueued++; |
| 1029 | } |
| 1030 | } |
| 1031 | |
| 1032 | int cpdma_chan_submit(struct cpdma_chan *chan, void *token, void *data, |
Sebastian Siewior | aef614e | 2013-04-23 07:31:38 +0000 | [diff] [blame] | 1033 | int len, int directed) |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1034 | { |
| 1035 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 1036 | struct cpdma_desc __iomem *desc; |
| 1037 | dma_addr_t buffer; |
| 1038 | unsigned long flags; |
| 1039 | u32 mode; |
| 1040 | int ret = 0; |
| 1041 | |
| 1042 | spin_lock_irqsave(&chan->lock, flags); |
| 1043 | |
| 1044 | if (chan->state == CPDMA_STATE_TEARDOWN) { |
| 1045 | ret = -EINVAL; |
| 1046 | goto unlock_ret; |
| 1047 | } |
| 1048 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 1049 | if (chan->count >= chan->desc_num) { |
| 1050 | chan->stats.desc_alloc_fail++; |
| 1051 | ret = -ENOMEM; |
| 1052 | goto unlock_ret; |
| 1053 | } |
| 1054 | |
| 1055 | desc = cpdma_desc_alloc(ctlr->pool); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1056 | if (!desc) { |
| 1057 | chan->stats.desc_alloc_fail++; |
| 1058 | ret = -ENOMEM; |
| 1059 | goto unlock_ret; |
| 1060 | } |
| 1061 | |
| 1062 | if (len < ctlr->params.min_packet_size) { |
| 1063 | len = ctlr->params.min_packet_size; |
| 1064 | chan->stats.runt_transmit_buff++; |
| 1065 | } |
| 1066 | |
| 1067 | buffer = dma_map_single(ctlr->dev, data, len, chan->dir); |
Sebastian Siewior | 14bd076 | 2013-06-20 16:58:45 +0200 | [diff] [blame] | 1068 | ret = dma_mapping_error(ctlr->dev, buffer); |
| 1069 | if (ret) { |
| 1070 | cpdma_desc_free(ctlr->pool, desc, 1); |
| 1071 | ret = -EINVAL; |
| 1072 | goto unlock_ret; |
| 1073 | } |
| 1074 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1075 | mode = CPDMA_DESC_OWNER | CPDMA_DESC_SOP | CPDMA_DESC_EOP; |
Mugunthan V N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 1076 | cpdma_desc_to_port(chan, mode, directed); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1077 | |
Grygorii Strashko | a6c83cc | 2017-01-06 14:07:29 -0600 | [diff] [blame] | 1078 | /* Relaxed IO accessors can be used here as there is read barrier |
| 1079 | * at the end of write sequence. |
| 1080 | */ |
| 1081 | writel_relaxed(0, &desc->hw_next); |
| 1082 | writel_relaxed(buffer, &desc->hw_buffer); |
| 1083 | writel_relaxed(len, &desc->hw_len); |
| 1084 | writel_relaxed(mode | len, &desc->hw_mode); |
| 1085 | writel_relaxed(token, &desc->sw_token); |
| 1086 | writel_relaxed(buffer, &desc->sw_buffer); |
| 1087 | writel_relaxed(len, &desc->sw_len); |
| 1088 | desc_read(desc, sw_len); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1089 | |
| 1090 | __cpdma_chan_submit(chan, desc); |
| 1091 | |
| 1092 | if (chan->state == CPDMA_STATE_ACTIVE && chan->rxfree) |
| 1093 | chan_write(chan, rxfree, 1); |
| 1094 | |
| 1095 | chan->count++; |
| 1096 | |
| 1097 | unlock_ret: |
| 1098 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1099 | return ret; |
| 1100 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 1101 | EXPORT_SYMBOL_GPL(cpdma_chan_submit); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1102 | |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 1103 | bool cpdma_check_free_tx_desc(struct cpdma_chan *chan) |
| 1104 | { |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 1105 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 1106 | struct cpdma_desc_pool *pool = ctlr->pool; |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 1107 | bool free_tx_desc; |
| 1108 | unsigned long flags; |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 1109 | |
Grygorii Strashko | 742fb20 | 2016-06-27 12:05:11 +0300 | [diff] [blame] | 1110 | spin_lock_irqsave(&chan->lock, flags); |
| 1111 | free_tx_desc = (chan->count < chan->desc_num) && |
| 1112 | gen_pool_avail(pool->gen_pool); |
| 1113 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1114 | return free_tx_desc; |
Mugunthan V N | fae5082 | 2013-01-17 06:31:34 +0000 | [diff] [blame] | 1115 | } |
| 1116 | EXPORT_SYMBOL_GPL(cpdma_check_free_tx_desc); |
| 1117 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1118 | static void __cpdma_chan_free(struct cpdma_chan *chan, |
| 1119 | struct cpdma_desc __iomem *desc, |
| 1120 | int outlen, int status) |
| 1121 | { |
| 1122 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 1123 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 1124 | dma_addr_t buff_dma; |
| 1125 | int origlen; |
| 1126 | void *token; |
| 1127 | |
| 1128 | token = (void *)desc_read(desc, sw_token); |
| 1129 | buff_dma = desc_read(desc, sw_buffer); |
| 1130 | origlen = desc_read(desc, sw_len); |
| 1131 | |
| 1132 | dma_unmap_single(ctlr->dev, buff_dma, origlen, chan->dir); |
| 1133 | cpdma_desc_free(pool, desc, 1); |
| 1134 | (*chan->handler)(token, outlen, status); |
| 1135 | } |
| 1136 | |
| 1137 | static int __cpdma_chan_process(struct cpdma_chan *chan) |
| 1138 | { |
| 1139 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 1140 | struct cpdma_desc __iomem *desc; |
| 1141 | int status, outlen; |
Sebastian Siewior | b4727e6 | 2013-04-23 07:31:39 +0000 | [diff] [blame] | 1142 | int cb_status = 0; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1143 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 1144 | dma_addr_t desc_dma; |
| 1145 | unsigned long flags; |
| 1146 | |
| 1147 | spin_lock_irqsave(&chan->lock, flags); |
| 1148 | |
| 1149 | desc = chan->head; |
| 1150 | if (!desc) { |
| 1151 | chan->stats.empty_dequeue++; |
| 1152 | status = -ENOENT; |
| 1153 | goto unlock_ret; |
| 1154 | } |
| 1155 | desc_dma = desc_phys(pool, desc); |
| 1156 | |
Grygorii Strashko | a6c83cc | 2017-01-06 14:07:29 -0600 | [diff] [blame] | 1157 | status = desc_read(desc, hw_mode); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1158 | outlen = status & 0x7ff; |
| 1159 | if (status & CPDMA_DESC_OWNER) { |
| 1160 | chan->stats.busy_dequeue++; |
| 1161 | status = -EBUSY; |
| 1162 | goto unlock_ret; |
| 1163 | } |
Mugunthan V N | 28a19fe | 2013-05-29 20:22:01 +0000 | [diff] [blame] | 1164 | |
| 1165 | if (status & CPDMA_DESC_PASS_CRC) |
| 1166 | outlen -= CPDMA_DESC_CRC_LEN; |
| 1167 | |
Mugunthan V N | f6e135c | 2013-02-11 09:52:18 +0000 | [diff] [blame] | 1168 | status = status & (CPDMA_DESC_EOQ | CPDMA_DESC_TD_COMPLETE | |
| 1169 | CPDMA_DESC_PORT_MASK); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1170 | |
| 1171 | chan->head = desc_from_phys(pool, desc_read(desc, hw_next)); |
| 1172 | chan_write(chan, cp, desc_dma); |
| 1173 | chan->count--; |
| 1174 | chan->stats.good_dequeue++; |
| 1175 | |
Grygorii Strashko | 12a303e | 2017-01-06 14:07:30 -0600 | [diff] [blame] | 1176 | if ((status & CPDMA_DESC_EOQ) && chan->head) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1177 | chan->stats.requeue++; |
| 1178 | chan_write(chan, hdp, desc_phys(pool, chan->head)); |
| 1179 | } |
| 1180 | |
| 1181 | spin_unlock_irqrestore(&chan->lock, flags); |
Sebastian Siewior | b4727e6 | 2013-04-23 07:31:39 +0000 | [diff] [blame] | 1182 | if (unlikely(status & CPDMA_DESC_TD_COMPLETE)) |
| 1183 | cb_status = -ENOSYS; |
| 1184 | else |
| 1185 | cb_status = status; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1186 | |
Sebastian Siewior | b4727e6 | 2013-04-23 07:31:39 +0000 | [diff] [blame] | 1187 | __cpdma_chan_free(chan, desc, outlen, cb_status); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1188 | return status; |
| 1189 | |
| 1190 | unlock_ret: |
| 1191 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1192 | return status; |
| 1193 | } |
| 1194 | |
| 1195 | int cpdma_chan_process(struct cpdma_chan *chan, int quota) |
| 1196 | { |
| 1197 | int used = 0, ret = 0; |
| 1198 | |
| 1199 | if (chan->state != CPDMA_STATE_ACTIVE) |
| 1200 | return -EINVAL; |
| 1201 | |
| 1202 | while (used < quota) { |
| 1203 | ret = __cpdma_chan_process(chan); |
| 1204 | if (ret < 0) |
| 1205 | break; |
| 1206 | used++; |
| 1207 | } |
| 1208 | return used; |
| 1209 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 1210 | EXPORT_SYMBOL_GPL(cpdma_chan_process); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1211 | |
| 1212 | int cpdma_chan_start(struct cpdma_chan *chan) |
| 1213 | { |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 1214 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 1215 | unsigned long flags; |
| 1216 | int ret; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1217 | |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 1218 | spin_lock_irqsave(&ctlr->lock, flags); |
| 1219 | ret = cpdma_chan_set_chan_shaper(chan); |
| 1220 | spin_unlock_irqrestore(&ctlr->lock, flags); |
| 1221 | if (ret) |
| 1222 | return ret; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1223 | |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 1224 | ret = cpdma_chan_on(chan); |
| 1225 | if (ret) |
| 1226 | return ret; |
| 1227 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1228 | return 0; |
| 1229 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 1230 | EXPORT_SYMBOL_GPL(cpdma_chan_start); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1231 | |
| 1232 | int cpdma_chan_stop(struct cpdma_chan *chan) |
| 1233 | { |
| 1234 | struct cpdma_ctlr *ctlr = chan->ctlr; |
| 1235 | struct cpdma_desc_pool *pool = ctlr->pool; |
| 1236 | unsigned long flags; |
| 1237 | int ret; |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 1238 | unsigned timeout; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1239 | |
| 1240 | spin_lock_irqsave(&chan->lock, flags); |
Christian Riesch | cd11cf5 | 2014-03-24 13:46:27 +0100 | [diff] [blame] | 1241 | if (chan->state == CPDMA_STATE_TEARDOWN) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1242 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1243 | return -EINVAL; |
| 1244 | } |
| 1245 | |
| 1246 | chan->state = CPDMA_STATE_TEARDOWN; |
| 1247 | dma_reg_write(ctlr, chan->int_clear, chan->mask); |
| 1248 | |
| 1249 | /* trigger teardown */ |
Christian Riesch | b4ad042 | 2012-02-22 21:58:00 +0000 | [diff] [blame] | 1250 | dma_reg_write(ctlr, chan->td, chan_linear(chan)); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1251 | |
| 1252 | /* wait for teardown complete */ |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 1253 | timeout = 100 * 100; /* 100 ms */ |
| 1254 | while (timeout) { |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1255 | u32 cp = chan_read(chan, cp); |
| 1256 | if ((cp & CPDMA_TEARDOWN_VALUE) == CPDMA_TEARDOWN_VALUE) |
| 1257 | break; |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 1258 | udelay(10); |
| 1259 | timeout--; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1260 | } |
Sebastian Siewior | 817f6d1 | 2013-04-23 07:31:35 +0000 | [diff] [blame] | 1261 | WARN_ON(!timeout); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1262 | chan_write(chan, cp, CPDMA_TEARDOWN_VALUE); |
| 1263 | |
| 1264 | /* handle completed packets */ |
Ilya Yanok | 7746ab0 | 2011-12-18 10:02:04 +0000 | [diff] [blame] | 1265 | spin_unlock_irqrestore(&chan->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1266 | do { |
| 1267 | ret = __cpdma_chan_process(chan); |
| 1268 | if (ret < 0) |
| 1269 | break; |
| 1270 | } while ((ret & CPDMA_DESC_TD_COMPLETE) == 0); |
Ilya Yanok | 7746ab0 | 2011-12-18 10:02:04 +0000 | [diff] [blame] | 1271 | spin_lock_irqsave(&chan->lock, flags); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1272 | |
| 1273 | /* remaining packets haven't been tx/rx'ed, clean them up */ |
| 1274 | while (chan->head) { |
| 1275 | struct cpdma_desc __iomem *desc = chan->head; |
| 1276 | dma_addr_t next_dma; |
| 1277 | |
| 1278 | next_dma = desc_read(desc, hw_next); |
| 1279 | chan->head = desc_from_phys(pool, next_dma); |
htbegin | ffb5ba9 | 2012-10-01 16:42:43 +0000 | [diff] [blame] | 1280 | chan->count--; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1281 | chan->stats.teardown_dequeue++; |
| 1282 | |
| 1283 | /* issue callback without locks held */ |
| 1284 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1285 | __cpdma_chan_free(chan, desc, 0, -ENOSYS); |
| 1286 | spin_lock_irqsave(&chan->lock, flags); |
| 1287 | } |
| 1288 | |
| 1289 | chan->state = CPDMA_STATE_IDLE; |
| 1290 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1291 | return 0; |
| 1292 | } |
Arnd Bergmann | 32a6d90 | 2012-04-20 10:56:09 +0000 | [diff] [blame] | 1293 | EXPORT_SYMBOL_GPL(cpdma_chan_stop); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1294 | |
| 1295 | int cpdma_chan_int_ctrl(struct cpdma_chan *chan, bool enable) |
| 1296 | { |
| 1297 | unsigned long flags; |
| 1298 | |
| 1299 | spin_lock_irqsave(&chan->lock, flags); |
| 1300 | if (chan->state != CPDMA_STATE_ACTIVE) { |
| 1301 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1302 | return -EINVAL; |
| 1303 | } |
| 1304 | |
| 1305 | dma_reg_write(chan->ctlr, enable ? chan->int_set : chan->int_clear, |
| 1306 | chan->mask); |
| 1307 | spin_unlock_irqrestore(&chan->lock, flags); |
| 1308 | |
| 1309 | return 0; |
| 1310 | } |
| 1311 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1312 | int cpdma_control_get(struct cpdma_ctlr *ctlr, int control) |
| 1313 | { |
| 1314 | unsigned long flags; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1315 | int ret; |
| 1316 | |
| 1317 | spin_lock_irqsave(&ctlr->lock, flags); |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 1318 | ret = _cpdma_control_get(ctlr, control); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1319 | spin_unlock_irqrestore(&ctlr->lock, flags); |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 1320 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1321 | return ret; |
| 1322 | } |
| 1323 | |
| 1324 | int cpdma_control_set(struct cpdma_ctlr *ctlr, int control, int value) |
| 1325 | { |
| 1326 | unsigned long flags; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1327 | int ret; |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1328 | |
| 1329 | spin_lock_irqsave(&ctlr->lock, flags); |
Ivan Khoronzhuk | 991ddb1 | 2016-11-11 15:45:24 +0200 | [diff] [blame] | 1330 | ret = _cpdma_control_set(ctlr, control, value); |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1331 | spin_unlock_irqrestore(&ctlr->lock, flags); |
Ivan Khoronzhuk | 8f32b90 | 2016-11-29 17:00:48 +0200 | [diff] [blame] | 1332 | |
Cyril Chemparathy | ef8c2da | 2010-09-15 10:11:28 -0400 | [diff] [blame] | 1333 | return ret; |
| 1334 | } |
Arnd Bergmann | 6929e24 | 2013-02-14 17:53:01 +0100 | [diff] [blame] | 1335 | EXPORT_SYMBOL_GPL(cpdma_control_set); |
Sebastian Siewior | 4bc21d4 | 2013-04-24 08:48:22 +0000 | [diff] [blame] | 1336 | |
Grygorii Strashko | be034fc | 2017-01-06 14:07:34 -0600 | [diff] [blame] | 1337 | int cpdma_get_num_rx_descs(struct cpdma_ctlr *ctlr) |
| 1338 | { |
| 1339 | return ctlr->num_rx_desc; |
| 1340 | } |
| 1341 | EXPORT_SYMBOL_GPL(cpdma_get_num_rx_descs); |
| 1342 | |
| 1343 | int cpdma_get_num_tx_descs(struct cpdma_ctlr *ctlr) |
| 1344 | { |
| 1345 | return ctlr->num_tx_desc; |
| 1346 | } |
| 1347 | EXPORT_SYMBOL_GPL(cpdma_get_num_tx_descs); |
| 1348 | |
| 1349 | void cpdma_set_num_rx_descs(struct cpdma_ctlr *ctlr, int num_rx_desc) |
| 1350 | { |
| 1351 | ctlr->num_rx_desc = num_rx_desc; |
| 1352 | ctlr->num_tx_desc = ctlr->pool->num_desc - ctlr->num_rx_desc; |
| 1353 | } |
| 1354 | EXPORT_SYMBOL_GPL(cpdma_set_num_rx_descs); |
| 1355 | |
Sebastian Siewior | 4bc21d4 | 2013-04-24 08:48:22 +0000 | [diff] [blame] | 1356 | MODULE_LICENSE("GPL"); |