Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Driver For Marvell Two-channel DMA Engine |
| 3 | * |
| 4 | * Copyright: Marvell International Ltd. |
| 5 | * |
| 6 | * The code contained herein is licensed under the GNU General Public |
| 7 | * License. You may obtain a copy of the GNU General Public License |
| 8 | * Version 2 or later at the following locations: |
| 9 | * |
| 10 | */ |
| 11 | |
Thierry Reding | 7331205 | 2013-01-21 11:09:00 +0100 | [diff] [blame] | 12 | #include <linux/err.h> |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 13 | #include <linux/module.h> |
| 14 | #include <linux/init.h> |
| 15 | #include <linux/types.h> |
| 16 | #include <linux/interrupt.h> |
| 17 | #include <linux/dma-mapping.h> |
| 18 | #include <linux/slab.h> |
| 19 | #include <linux/dmaengine.h> |
| 20 | #include <linux/platform_device.h> |
| 21 | #include <linux/device.h> |
| 22 | #include <mach/regs-icu.h> |
Arnd Bergmann | 293b2da | 2012-08-24 15:16:48 +0200 | [diff] [blame] | 23 | #include <linux/platform_data/dma-mmp_tdma.h> |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 24 | #include <linux/of_device.h> |
Nenghua Cao | 7dedc00 | 2014-01-20 20:39:01 +0800 | [diff] [blame] | 25 | #include <linux/of_dma.h> |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 26 | |
| 27 | #include "dmaengine.h" |
| 28 | |
| 29 | /* |
| 30 | * Two-Channel DMA registers |
| 31 | */ |
| 32 | #define TDBCR 0x00 /* Byte Count */ |
| 33 | #define TDSAR 0x10 /* Src Addr */ |
| 34 | #define TDDAR 0x20 /* Dst Addr */ |
| 35 | #define TDNDPR 0x30 /* Next Desc */ |
| 36 | #define TDCR 0x40 /* Control */ |
| 37 | #define TDCP 0x60 /* Priority*/ |
| 38 | #define TDCDPR 0x70 /* Current Desc */ |
| 39 | #define TDIMR 0x80 /* Int Mask */ |
| 40 | #define TDISR 0xa0 /* Int Status */ |
| 41 | |
| 42 | /* Two-Channel DMA Control Register */ |
| 43 | #define TDCR_SSZ_8_BITS (0x0 << 22) /* Sample Size */ |
| 44 | #define TDCR_SSZ_12_BITS (0x1 << 22) |
| 45 | #define TDCR_SSZ_16_BITS (0x2 << 22) |
| 46 | #define TDCR_SSZ_20_BITS (0x3 << 22) |
| 47 | #define TDCR_SSZ_24_BITS (0x4 << 22) |
| 48 | #define TDCR_SSZ_32_BITS (0x5 << 22) |
| 49 | #define TDCR_SSZ_SHIFT (0x1 << 22) |
| 50 | #define TDCR_SSZ_MASK (0x7 << 22) |
| 51 | #define TDCR_SSPMOD (0x1 << 21) /* SSP MOD */ |
| 52 | #define TDCR_ABR (0x1 << 20) /* Channel Abort */ |
| 53 | #define TDCR_CDE (0x1 << 17) /* Close Desc Enable */ |
| 54 | #define TDCR_PACKMOD (0x1 << 16) /* Pack Mode (ADMA Only) */ |
| 55 | #define TDCR_CHANACT (0x1 << 14) /* Channel Active */ |
| 56 | #define TDCR_FETCHND (0x1 << 13) /* Fetch Next Desc */ |
| 57 | #define TDCR_CHANEN (0x1 << 12) /* Channel Enable */ |
| 58 | #define TDCR_INTMODE (0x1 << 10) /* Interrupt Mode */ |
| 59 | #define TDCR_CHAINMOD (0x1 << 9) /* Chain Mode */ |
| 60 | #define TDCR_BURSTSZ_MSK (0x7 << 6) /* Burst Size */ |
| 61 | #define TDCR_BURSTSZ_4B (0x0 << 6) |
| 62 | #define TDCR_BURSTSZ_8B (0x1 << 6) |
| 63 | #define TDCR_BURSTSZ_16B (0x3 << 6) |
| 64 | #define TDCR_BURSTSZ_32B (0x6 << 6) |
| 65 | #define TDCR_BURSTSZ_64B (0x7 << 6) |
Qiao Zhou | 20a90b0 | 2013-10-11 09:07:01 +0800 | [diff] [blame] | 66 | #define TDCR_BURSTSZ_SQU_1B (0x5 << 6) |
| 67 | #define TDCR_BURSTSZ_SQU_2B (0x6 << 6) |
| 68 | #define TDCR_BURSTSZ_SQU_4B (0x0 << 6) |
| 69 | #define TDCR_BURSTSZ_SQU_8B (0x1 << 6) |
| 70 | #define TDCR_BURSTSZ_SQU_16B (0x3 << 6) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 71 | #define TDCR_BURSTSZ_SQU_32B (0x7 << 6) |
| 72 | #define TDCR_BURSTSZ_128B (0x5 << 6) |
| 73 | #define TDCR_DSTDIR_MSK (0x3 << 4) /* Dst Direction */ |
| 74 | #define TDCR_DSTDIR_ADDR_HOLD (0x2 << 4) /* Dst Addr Hold */ |
| 75 | #define TDCR_DSTDIR_ADDR_INC (0x0 << 4) /* Dst Addr Increment */ |
| 76 | #define TDCR_SRCDIR_MSK (0x3 << 2) /* Src Direction */ |
| 77 | #define TDCR_SRCDIR_ADDR_HOLD (0x2 << 2) /* Src Addr Hold */ |
| 78 | #define TDCR_SRCDIR_ADDR_INC (0x0 << 2) /* Src Addr Increment */ |
| 79 | #define TDCR_DSTDESCCONT (0x1 << 1) |
| 80 | #define TDCR_SRCDESTCONT (0x1 << 0) |
| 81 | |
| 82 | /* Two-Channel DMA Int Mask Register */ |
| 83 | #define TDIMR_COMP (0x1 << 0) |
| 84 | |
| 85 | /* Two-Channel DMA Int Status Register */ |
| 86 | #define TDISR_COMP (0x1 << 0) |
| 87 | |
| 88 | /* |
| 89 | * Two-Channel DMA Descriptor Struct |
| 90 | * NOTE: desc's buf must be aligned to 16 bytes. |
| 91 | */ |
| 92 | struct mmp_tdma_desc { |
| 93 | u32 byte_cnt; |
| 94 | u32 src_addr; |
| 95 | u32 dst_addr; |
| 96 | u32 nxt_desc; |
| 97 | }; |
| 98 | |
| 99 | enum mmp_tdma_type { |
| 100 | MMP_AUD_TDMA = 0, |
| 101 | PXA910_SQU, |
| 102 | }; |
| 103 | |
| 104 | #define TDMA_ALIGNMENT 3 |
| 105 | #define TDMA_MAX_XFER_BYTES SZ_64K |
| 106 | |
| 107 | struct mmp_tdma_chan { |
| 108 | struct device *dev; |
| 109 | struct dma_chan chan; |
| 110 | struct dma_async_tx_descriptor desc; |
| 111 | struct tasklet_struct tasklet; |
| 112 | |
| 113 | struct mmp_tdma_desc *desc_arr; |
| 114 | phys_addr_t desc_arr_phys; |
| 115 | int desc_num; |
| 116 | enum dma_transfer_direction dir; |
| 117 | dma_addr_t dev_addr; |
| 118 | u32 burst_sz; |
| 119 | enum dma_slave_buswidth buswidth; |
| 120 | enum dma_status status; |
| 121 | |
| 122 | int idx; |
| 123 | enum mmp_tdma_type type; |
| 124 | int irq; |
Vinod Koul | 9d0f1fa | 2013-11-28 14:59:39 +0530 | [diff] [blame] | 125 | void __iomem *reg_base; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 126 | |
| 127 | size_t buf_len; |
| 128 | size_t period_len; |
| 129 | size_t pos; |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 130 | |
| 131 | struct gen_pool *pool; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | #define TDMA_CHANNEL_NUM 2 |
| 135 | struct mmp_tdma_device { |
| 136 | struct device *dev; |
| 137 | void __iomem *base; |
| 138 | struct dma_device device; |
| 139 | struct mmp_tdma_chan *tdmac[TDMA_CHANNEL_NUM]; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 140 | }; |
| 141 | |
| 142 | #define to_mmp_tdma_chan(dchan) container_of(dchan, struct mmp_tdma_chan, chan) |
| 143 | |
| 144 | static void mmp_tdma_chan_set_desc(struct mmp_tdma_chan *tdmac, dma_addr_t phys) |
| 145 | { |
| 146 | writel(phys, tdmac->reg_base + TDNDPR); |
| 147 | writel(readl(tdmac->reg_base + TDCR) | TDCR_FETCHND, |
| 148 | tdmac->reg_base + TDCR); |
| 149 | } |
| 150 | |
Qiao Zhou | e622226 | 2014-09-10 16:40:48 +0800 | [diff] [blame] | 151 | static void mmp_tdma_enable_irq(struct mmp_tdma_chan *tdmac, bool enable) |
| 152 | { |
| 153 | if (enable) |
| 154 | writel(TDIMR_COMP, tdmac->reg_base + TDIMR); |
| 155 | else |
| 156 | writel(0, tdmac->reg_base + TDIMR); |
| 157 | } |
| 158 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 159 | static void mmp_tdma_enable_chan(struct mmp_tdma_chan *tdmac) |
| 160 | { |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 161 | /* enable dma chan */ |
| 162 | writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN, |
| 163 | tdmac->reg_base + TDCR); |
| 164 | tdmac->status = DMA_IN_PROGRESS; |
| 165 | } |
| 166 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 167 | static int mmp_tdma_disable_chan(struct dma_chan *chan) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 168 | { |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 169 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 170 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 171 | writel(readl(tdmac->reg_base + TDCR) & ~TDCR_CHANEN, |
| 172 | tdmac->reg_base + TDCR); |
Qiao Zhou | 8e3c518 | 2013-06-15 12:51:48 +0800 | [diff] [blame] | 173 | |
Vinod Koul | f64eabd | 2013-10-16 20:50:36 +0530 | [diff] [blame] | 174 | tdmac->status = DMA_COMPLETE; |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 175 | |
| 176 | return 0; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 177 | } |
| 178 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 179 | static int mmp_tdma_resume_chan(struct dma_chan *chan) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 180 | { |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 181 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 182 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 183 | writel(readl(tdmac->reg_base + TDCR) | TDCR_CHANEN, |
| 184 | tdmac->reg_base + TDCR); |
| 185 | tdmac->status = DMA_IN_PROGRESS; |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 186 | |
| 187 | return 0; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 188 | } |
| 189 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 190 | static int mmp_tdma_pause_chan(struct dma_chan *chan) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 191 | { |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 192 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 193 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 194 | writel(readl(tdmac->reg_base + TDCR) & ~TDCR_CHANEN, |
| 195 | tdmac->reg_base + TDCR); |
| 196 | tdmac->status = DMA_PAUSED; |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 197 | |
| 198 | return 0; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 199 | } |
| 200 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 201 | static int mmp_tdma_config_chan(struct dma_chan *chan) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 202 | { |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 203 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
Vinod Koul | a9ebbcd | 2013-11-29 10:52:52 +0530 | [diff] [blame] | 204 | unsigned int tdcr = 0; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 205 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 206 | mmp_tdma_disable_chan(chan); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 207 | |
| 208 | if (tdmac->dir == DMA_MEM_TO_DEV) |
| 209 | tdcr = TDCR_DSTDIR_ADDR_HOLD | TDCR_SRCDIR_ADDR_INC; |
| 210 | else if (tdmac->dir == DMA_DEV_TO_MEM) |
| 211 | tdcr = TDCR_SRCDIR_ADDR_HOLD | TDCR_DSTDIR_ADDR_INC; |
| 212 | |
| 213 | if (tdmac->type == MMP_AUD_TDMA) { |
| 214 | tdcr |= TDCR_PACKMOD; |
| 215 | |
| 216 | switch (tdmac->burst_sz) { |
| 217 | case 4: |
| 218 | tdcr |= TDCR_BURSTSZ_4B; |
| 219 | break; |
| 220 | case 8: |
| 221 | tdcr |= TDCR_BURSTSZ_8B; |
| 222 | break; |
| 223 | case 16: |
| 224 | tdcr |= TDCR_BURSTSZ_16B; |
| 225 | break; |
| 226 | case 32: |
| 227 | tdcr |= TDCR_BURSTSZ_32B; |
| 228 | break; |
| 229 | case 64: |
| 230 | tdcr |= TDCR_BURSTSZ_64B; |
| 231 | break; |
| 232 | case 128: |
| 233 | tdcr |= TDCR_BURSTSZ_128B; |
| 234 | break; |
| 235 | default: |
| 236 | dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n"); |
| 237 | return -EINVAL; |
| 238 | } |
| 239 | |
| 240 | switch (tdmac->buswidth) { |
| 241 | case DMA_SLAVE_BUSWIDTH_1_BYTE: |
| 242 | tdcr |= TDCR_SSZ_8_BITS; |
| 243 | break; |
| 244 | case DMA_SLAVE_BUSWIDTH_2_BYTES: |
| 245 | tdcr |= TDCR_SSZ_16_BITS; |
| 246 | break; |
| 247 | case DMA_SLAVE_BUSWIDTH_4_BYTES: |
| 248 | tdcr |= TDCR_SSZ_32_BITS; |
| 249 | break; |
| 250 | default: |
| 251 | dev_err(tdmac->dev, "mmp_tdma: unknown bus size.\n"); |
| 252 | return -EINVAL; |
| 253 | } |
| 254 | } else if (tdmac->type == PXA910_SQU) { |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 255 | tdcr |= TDCR_SSPMOD; |
Qiao Zhou | 20a90b0 | 2013-10-11 09:07:01 +0800 | [diff] [blame] | 256 | |
| 257 | switch (tdmac->burst_sz) { |
| 258 | case 1: |
| 259 | tdcr |= TDCR_BURSTSZ_SQU_1B; |
| 260 | break; |
| 261 | case 2: |
| 262 | tdcr |= TDCR_BURSTSZ_SQU_2B; |
| 263 | break; |
| 264 | case 4: |
| 265 | tdcr |= TDCR_BURSTSZ_SQU_4B; |
| 266 | break; |
| 267 | case 8: |
| 268 | tdcr |= TDCR_BURSTSZ_SQU_8B; |
| 269 | break; |
| 270 | case 16: |
| 271 | tdcr |= TDCR_BURSTSZ_SQU_16B; |
| 272 | break; |
| 273 | case 32: |
| 274 | tdcr |= TDCR_BURSTSZ_SQU_32B; |
| 275 | break; |
| 276 | default: |
| 277 | dev_err(tdmac->dev, "mmp_tdma: unknown burst size.\n"); |
| 278 | return -EINVAL; |
| 279 | } |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 280 | } |
| 281 | |
| 282 | writel(tdcr, tdmac->reg_base + TDCR); |
| 283 | return 0; |
| 284 | } |
| 285 | |
| 286 | static int mmp_tdma_clear_chan_irq(struct mmp_tdma_chan *tdmac) |
| 287 | { |
| 288 | u32 reg = readl(tdmac->reg_base + TDISR); |
| 289 | |
| 290 | if (reg & TDISR_COMP) { |
| 291 | /* clear irq */ |
| 292 | reg &= ~TDISR_COMP; |
| 293 | writel(reg, tdmac->reg_base + TDISR); |
| 294 | |
| 295 | return 0; |
| 296 | } |
| 297 | return -EAGAIN; |
| 298 | } |
| 299 | |
| 300 | static irqreturn_t mmp_tdma_chan_handler(int irq, void *dev_id) |
| 301 | { |
| 302 | struct mmp_tdma_chan *tdmac = dev_id; |
| 303 | |
| 304 | if (mmp_tdma_clear_chan_irq(tdmac) == 0) { |
| 305 | tdmac->pos = (tdmac->pos + tdmac->period_len) % tdmac->buf_len; |
| 306 | tasklet_schedule(&tdmac->tasklet); |
| 307 | return IRQ_HANDLED; |
| 308 | } else |
| 309 | return IRQ_NONE; |
| 310 | } |
| 311 | |
| 312 | static irqreturn_t mmp_tdma_int_handler(int irq, void *dev_id) |
| 313 | { |
| 314 | struct mmp_tdma_device *tdev = dev_id; |
| 315 | int i, ret; |
| 316 | int irq_num = 0; |
| 317 | |
| 318 | for (i = 0; i < TDMA_CHANNEL_NUM; i++) { |
| 319 | struct mmp_tdma_chan *tdmac = tdev->tdmac[i]; |
| 320 | |
| 321 | ret = mmp_tdma_chan_handler(irq, tdmac); |
| 322 | if (ret == IRQ_HANDLED) |
| 323 | irq_num++; |
| 324 | } |
| 325 | |
| 326 | if (irq_num) |
| 327 | return IRQ_HANDLED; |
| 328 | else |
| 329 | return IRQ_NONE; |
| 330 | } |
| 331 | |
| 332 | static void dma_do_tasklet(unsigned long data) |
| 333 | { |
| 334 | struct mmp_tdma_chan *tdmac = (struct mmp_tdma_chan *)data; |
| 335 | |
| 336 | if (tdmac->desc.callback) |
| 337 | tdmac->desc.callback(tdmac->desc.callback_param); |
| 338 | |
| 339 | } |
| 340 | |
| 341 | static void mmp_tdma_free_descriptor(struct mmp_tdma_chan *tdmac) |
| 342 | { |
| 343 | struct gen_pool *gpool; |
| 344 | int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc); |
| 345 | |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 346 | gpool = tdmac->pool; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 347 | if (tdmac->desc_arr) |
| 348 | gen_pool_free(gpool, (unsigned long)tdmac->desc_arr, |
| 349 | size); |
| 350 | tdmac->desc_arr = NULL; |
| 351 | |
| 352 | return; |
| 353 | } |
| 354 | |
| 355 | static dma_cookie_t mmp_tdma_tx_submit(struct dma_async_tx_descriptor *tx) |
| 356 | { |
| 357 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(tx->chan); |
| 358 | |
| 359 | mmp_tdma_chan_set_desc(tdmac, tdmac->desc_arr_phys); |
| 360 | |
| 361 | return 0; |
| 362 | } |
| 363 | |
| 364 | static int mmp_tdma_alloc_chan_resources(struct dma_chan *chan) |
| 365 | { |
| 366 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 367 | int ret; |
| 368 | |
| 369 | dma_async_tx_descriptor_init(&tdmac->desc, chan); |
| 370 | tdmac->desc.tx_submit = mmp_tdma_tx_submit; |
| 371 | |
| 372 | if (tdmac->irq) { |
| 373 | ret = devm_request_irq(tdmac->dev, tdmac->irq, |
Michael Opdenacker | 174b537 | 2013-10-13 07:10:51 +0200 | [diff] [blame] | 374 | mmp_tdma_chan_handler, 0, "tdma", tdmac); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 375 | if (ret) |
| 376 | return ret; |
| 377 | } |
| 378 | return 1; |
| 379 | } |
| 380 | |
| 381 | static void mmp_tdma_free_chan_resources(struct dma_chan *chan) |
| 382 | { |
| 383 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 384 | |
| 385 | if (tdmac->irq) |
| 386 | devm_free_irq(tdmac->dev, tdmac->irq, tdmac); |
| 387 | mmp_tdma_free_descriptor(tdmac); |
| 388 | return; |
| 389 | } |
| 390 | |
| 391 | struct mmp_tdma_desc *mmp_tdma_alloc_descriptor(struct mmp_tdma_chan *tdmac) |
| 392 | { |
| 393 | struct gen_pool *gpool; |
| 394 | int size = tdmac->desc_num * sizeof(struct mmp_tdma_desc); |
| 395 | |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 396 | gpool = tdmac->pool; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 397 | if (!gpool) |
| 398 | return NULL; |
| 399 | |
Nicolin Chen | a6dd30e | 2013-11-12 15:09:55 -0800 | [diff] [blame] | 400 | tdmac->desc_arr = gen_pool_dma_alloc(gpool, size, &tdmac->desc_arr_phys); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 401 | |
| 402 | return tdmac->desc_arr; |
| 403 | } |
| 404 | |
| 405 | static struct dma_async_tx_descriptor *mmp_tdma_prep_dma_cyclic( |
| 406 | struct dma_chan *chan, dma_addr_t dma_addr, size_t buf_len, |
| 407 | size_t period_len, enum dma_transfer_direction direction, |
Laurent Pinchart | 31c1e5a | 2014-08-01 12:20:10 +0200 | [diff] [blame] | 408 | unsigned long flags) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 409 | { |
| 410 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 411 | struct mmp_tdma_desc *desc; |
| 412 | int num_periods = buf_len / period_len; |
| 413 | int i = 0, buf = 0; |
| 414 | |
Vinod Koul | f64eabd | 2013-10-16 20:50:36 +0530 | [diff] [blame] | 415 | if (tdmac->status != DMA_COMPLETE) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 416 | return NULL; |
| 417 | |
| 418 | if (period_len > TDMA_MAX_XFER_BYTES) { |
| 419 | dev_err(tdmac->dev, |
| 420 | "maximum period size exceeded: %d > %d\n", |
| 421 | period_len, TDMA_MAX_XFER_BYTES); |
| 422 | goto err_out; |
| 423 | } |
| 424 | |
| 425 | tdmac->status = DMA_IN_PROGRESS; |
| 426 | tdmac->desc_num = num_periods; |
| 427 | desc = mmp_tdma_alloc_descriptor(tdmac); |
| 428 | if (!desc) |
| 429 | goto err_out; |
| 430 | |
| 431 | while (buf < buf_len) { |
| 432 | desc = &tdmac->desc_arr[i]; |
| 433 | |
| 434 | if (i + 1 == num_periods) |
| 435 | desc->nxt_desc = tdmac->desc_arr_phys; |
| 436 | else |
| 437 | desc->nxt_desc = tdmac->desc_arr_phys + |
| 438 | sizeof(*desc) * (i + 1); |
| 439 | |
| 440 | if (direction == DMA_MEM_TO_DEV) { |
| 441 | desc->src_addr = dma_addr; |
| 442 | desc->dst_addr = tdmac->dev_addr; |
| 443 | } else { |
| 444 | desc->src_addr = tdmac->dev_addr; |
| 445 | desc->dst_addr = dma_addr; |
| 446 | } |
| 447 | desc->byte_cnt = period_len; |
| 448 | dma_addr += period_len; |
| 449 | buf += period_len; |
| 450 | i++; |
| 451 | } |
| 452 | |
Qiao Zhou | e622226 | 2014-09-10 16:40:48 +0800 | [diff] [blame] | 453 | /* enable interrupt */ |
| 454 | if (flags & DMA_PREP_INTERRUPT) |
| 455 | mmp_tdma_enable_irq(tdmac, true); |
| 456 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 457 | tdmac->buf_len = buf_len; |
| 458 | tdmac->period_len = period_len; |
| 459 | tdmac->pos = 0; |
| 460 | |
| 461 | return &tdmac->desc; |
| 462 | |
| 463 | err_out: |
| 464 | tdmac->status = DMA_ERROR; |
| 465 | return NULL; |
| 466 | } |
| 467 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 468 | static int mmp_tdma_terminate_all(struct dma_chan *chan) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 469 | { |
| 470 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 471 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 472 | mmp_tdma_disable_chan(chan); |
| 473 | /* disable interrupt */ |
| 474 | mmp_tdma_enable_irq(tdmac, false); |
| 475 | } |
| 476 | |
| 477 | static int mmp_tdma_config(struct dma_chan *chan, |
| 478 | struct dma_slave_config *dmaengine_cfg) |
| 479 | { |
| 480 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 481 | |
| 482 | if (dmaengine_cfg->direction == DMA_DEV_TO_MEM) { |
| 483 | tdmac->dev_addr = dmaengine_cfg->src_addr; |
| 484 | tdmac->burst_sz = dmaengine_cfg->src_maxburst; |
| 485 | tdmac->buswidth = dmaengine_cfg->src_addr_width; |
| 486 | } else { |
| 487 | tdmac->dev_addr = dmaengine_cfg->dst_addr; |
| 488 | tdmac->burst_sz = dmaengine_cfg->dst_maxburst; |
| 489 | tdmac->buswidth = dmaengine_cfg->dst_addr_width; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 490 | } |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 491 | tdmac->dir = dmaengine_cfg->direction; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 492 | |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 493 | return mmp_tdma_config_chan(chan); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 494 | } |
| 495 | |
| 496 | static enum dma_status mmp_tdma_tx_status(struct dma_chan *chan, |
| 497 | dma_cookie_t cookie, struct dma_tx_state *txstate) |
| 498 | { |
| 499 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 500 | |
Andy Shevchenko | c14d2bc | 2013-05-27 15:14:41 +0300 | [diff] [blame] | 501 | dma_set_tx_state(txstate, chan->completed_cookie, chan->cookie, |
| 502 | tdmac->buf_len - tdmac->pos); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 503 | |
| 504 | return tdmac->status; |
| 505 | } |
| 506 | |
| 507 | static void mmp_tdma_issue_pending(struct dma_chan *chan) |
| 508 | { |
| 509 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 510 | |
| 511 | mmp_tdma_enable_chan(tdmac); |
| 512 | } |
| 513 | |
Greg Kroah-Hartman | 4bf27b8 | 2012-12-21 15:09:59 -0800 | [diff] [blame] | 514 | static int mmp_tdma_remove(struct platform_device *pdev) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 515 | { |
| 516 | struct mmp_tdma_device *tdev = platform_get_drvdata(pdev); |
| 517 | |
| 518 | dma_async_device_unregister(&tdev->device); |
| 519 | return 0; |
| 520 | } |
| 521 | |
Bill Pemberton | 463a1f8 | 2012-11-19 13:22:55 -0500 | [diff] [blame] | 522 | static int mmp_tdma_chan_init(struct mmp_tdma_device *tdev, |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 523 | int idx, int irq, |
| 524 | int type, struct gen_pool *pool) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 525 | { |
| 526 | struct mmp_tdma_chan *tdmac; |
| 527 | |
| 528 | if (idx >= TDMA_CHANNEL_NUM) { |
| 529 | dev_err(tdev->dev, "too many channels for device!\n"); |
| 530 | return -EINVAL; |
| 531 | } |
| 532 | |
| 533 | /* alloc channel */ |
| 534 | tdmac = devm_kzalloc(tdev->dev, sizeof(*tdmac), GFP_KERNEL); |
| 535 | if (!tdmac) { |
| 536 | dev_err(tdev->dev, "no free memory for DMA channels!\n"); |
| 537 | return -ENOMEM; |
| 538 | } |
| 539 | if (irq) |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 540 | tdmac->irq = irq; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 541 | tdmac->dev = tdev->dev; |
| 542 | tdmac->chan.device = &tdev->device; |
| 543 | tdmac->idx = idx; |
| 544 | tdmac->type = type; |
Vinod Koul | 9d0f1fa | 2013-11-28 14:59:39 +0530 | [diff] [blame] | 545 | tdmac->reg_base = tdev->base + idx * 4; |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 546 | tdmac->pool = pool; |
Vinod Koul | f64eabd | 2013-10-16 20:50:36 +0530 | [diff] [blame] | 547 | tdmac->status = DMA_COMPLETE; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 548 | tdev->tdmac[tdmac->idx] = tdmac; |
| 549 | tasklet_init(&tdmac->tasklet, dma_do_tasklet, (unsigned long)tdmac); |
| 550 | |
| 551 | /* add the channel to tdma_chan list */ |
| 552 | list_add_tail(&tdmac->chan.device_node, |
| 553 | &tdev->device.channels); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 554 | return 0; |
| 555 | } |
| 556 | |
Nenghua Cao | 7dedc00 | 2014-01-20 20:39:01 +0800 | [diff] [blame] | 557 | struct mmp_tdma_filter_param { |
| 558 | struct device_node *of_node; |
| 559 | unsigned int chan_id; |
| 560 | }; |
| 561 | |
| 562 | static bool mmp_tdma_filter_fn(struct dma_chan *chan, void *fn_param) |
| 563 | { |
| 564 | struct mmp_tdma_filter_param *param = fn_param; |
| 565 | struct mmp_tdma_chan *tdmac = to_mmp_tdma_chan(chan); |
| 566 | struct dma_device *pdma_device = tdmac->chan.device; |
| 567 | |
| 568 | if (pdma_device->dev->of_node != param->of_node) |
| 569 | return false; |
| 570 | |
| 571 | if (chan->chan_id != param->chan_id) |
| 572 | return false; |
| 573 | |
| 574 | return true; |
| 575 | } |
| 576 | |
| 577 | struct dma_chan *mmp_tdma_xlate(struct of_phandle_args *dma_spec, |
| 578 | struct of_dma *ofdma) |
| 579 | { |
| 580 | struct mmp_tdma_device *tdev = ofdma->of_dma_data; |
| 581 | dma_cap_mask_t mask = tdev->device.cap_mask; |
| 582 | struct mmp_tdma_filter_param param; |
| 583 | |
| 584 | if (dma_spec->args_count != 1) |
| 585 | return NULL; |
| 586 | |
| 587 | param.of_node = ofdma->of_node; |
| 588 | param.chan_id = dma_spec->args[0]; |
| 589 | |
| 590 | if (param.chan_id >= TDMA_CHANNEL_NUM) |
| 591 | return NULL; |
| 592 | |
| 593 | return dma_request_channel(mask, mmp_tdma_filter_fn, ¶m); |
| 594 | } |
| 595 | |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 596 | static struct of_device_id mmp_tdma_dt_ids[] = { |
| 597 | { .compatible = "marvell,adma-1.0", .data = (void *)MMP_AUD_TDMA}, |
| 598 | { .compatible = "marvell,pxa910-squ", .data = (void *)PXA910_SQU}, |
| 599 | {} |
| 600 | }; |
| 601 | MODULE_DEVICE_TABLE(of, mmp_tdma_dt_ids); |
| 602 | |
Bill Pemberton | 463a1f8 | 2012-11-19 13:22:55 -0500 | [diff] [blame] | 603 | static int mmp_tdma_probe(struct platform_device *pdev) |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 604 | { |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 605 | enum mmp_tdma_type type; |
| 606 | const struct of_device_id *of_id; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 607 | struct mmp_tdma_device *tdev; |
| 608 | struct resource *iores; |
| 609 | int i, ret; |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 610 | int irq = 0, irq_num = 0; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 611 | int chan_num = TDMA_CHANNEL_NUM; |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 612 | struct gen_pool *pool; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 613 | |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 614 | of_id = of_match_device(mmp_tdma_dt_ids, &pdev->dev); |
| 615 | if (of_id) |
| 616 | type = (enum mmp_tdma_type) of_id->data; |
| 617 | else |
| 618 | type = platform_get_device_id(pdev)->driver_data; |
| 619 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 620 | /* always have couple channels */ |
| 621 | tdev = devm_kzalloc(&pdev->dev, sizeof(*tdev), GFP_KERNEL); |
| 622 | if (!tdev) |
| 623 | return -ENOMEM; |
| 624 | |
| 625 | tdev->dev = &pdev->dev; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 626 | |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 627 | for (i = 0; i < chan_num; i++) { |
| 628 | if (platform_get_irq(pdev, i) > 0) |
| 629 | irq_num++; |
| 630 | } |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 631 | |
| 632 | iores = platform_get_resource(pdev, IORESOURCE_MEM, 0); |
Thierry Reding | 7331205 | 2013-01-21 11:09:00 +0100 | [diff] [blame] | 633 | tdev->base = devm_ioremap_resource(&pdev->dev, iores); |
| 634 | if (IS_ERR(tdev->base)) |
| 635 | return PTR_ERR(tdev->base); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 636 | |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 637 | INIT_LIST_HEAD(&tdev->device.channels); |
| 638 | |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 639 | if (pdev->dev.of_node) |
| 640 | pool = of_get_named_gen_pool(pdev->dev.of_node, "asram", 0); |
| 641 | else |
| 642 | pool = sram_get_gpool("asram"); |
| 643 | if (!pool) { |
| 644 | dev_err(&pdev->dev, "asram pool not available\n"); |
| 645 | return -ENOMEM; |
| 646 | } |
| 647 | |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 648 | if (irq_num != chan_num) { |
| 649 | irq = platform_get_irq(pdev, 0); |
| 650 | ret = devm_request_irq(&pdev->dev, irq, |
Michael Opdenacker | 174b537 | 2013-10-13 07:10:51 +0200 | [diff] [blame] | 651 | mmp_tdma_int_handler, 0, "tdma", tdev); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 652 | if (ret) |
| 653 | return ret; |
| 654 | } |
| 655 | |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 656 | /* initialize channel parameters */ |
| 657 | for (i = 0; i < chan_num; i++) { |
| 658 | irq = (irq_num != chan_num) ? 0 : platform_get_irq(pdev, i); |
Nenghua Cao | 3b0f4a5 | 2013-12-13 16:14:31 +0800 | [diff] [blame] | 659 | ret = mmp_tdma_chan_init(tdev, i, irq, type, pool); |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 660 | if (ret) |
| 661 | return ret; |
| 662 | } |
| 663 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 664 | dma_cap_set(DMA_SLAVE, tdev->device.cap_mask); |
| 665 | dma_cap_set(DMA_CYCLIC, tdev->device.cap_mask); |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 666 | tdev->device.dev = &pdev->dev; |
| 667 | tdev->device.device_alloc_chan_resources = |
| 668 | mmp_tdma_alloc_chan_resources; |
| 669 | tdev->device.device_free_chan_resources = |
| 670 | mmp_tdma_free_chan_resources; |
| 671 | tdev->device.device_prep_dma_cyclic = mmp_tdma_prep_dma_cyclic; |
| 672 | tdev->device.device_tx_status = mmp_tdma_tx_status; |
| 673 | tdev->device.device_issue_pending = mmp_tdma_issue_pending; |
Maxime Ripard | f43a6fd | 2014-11-17 14:42:22 +0100 | [diff] [blame^] | 674 | tdev->device.device_config = mmp_tdma_config; |
| 675 | tdev->device.device_pause = mmp_tdma_pause_chan; |
| 676 | tdev->device.device_resume = mmp_tdma_resume_chan; |
| 677 | tdev->device.device_terminate_all = mmp_tdma_terminate_all; |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 678 | tdev->device.copy_align = TDMA_ALIGNMENT; |
| 679 | |
| 680 | dma_set_mask(&pdev->dev, DMA_BIT_MASK(64)); |
| 681 | platform_set_drvdata(pdev, tdev); |
| 682 | |
| 683 | ret = dma_async_device_register(&tdev->device); |
| 684 | if (ret) { |
| 685 | dev_err(tdev->device.dev, "unable to register\n"); |
| 686 | return ret; |
| 687 | } |
| 688 | |
Nenghua Cao | 7dedc00 | 2014-01-20 20:39:01 +0800 | [diff] [blame] | 689 | if (pdev->dev.of_node) { |
| 690 | ret = of_dma_controller_register(pdev->dev.of_node, |
| 691 | mmp_tdma_xlate, tdev); |
| 692 | if (ret) { |
| 693 | dev_err(tdev->device.dev, |
| 694 | "failed to register controller\n"); |
| 695 | dma_async_device_unregister(&tdev->device); |
| 696 | } |
| 697 | } |
| 698 | |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 699 | dev_info(tdev->device.dev, "initialized\n"); |
| 700 | return 0; |
| 701 | } |
| 702 | |
| 703 | static const struct platform_device_id mmp_tdma_id_table[] = { |
| 704 | { "mmp-adma", MMP_AUD_TDMA }, |
| 705 | { "pxa910-squ", PXA910_SQU }, |
| 706 | { }, |
| 707 | }; |
| 708 | |
| 709 | static struct platform_driver mmp_tdma_driver = { |
| 710 | .driver = { |
| 711 | .name = "mmp-tdma", |
Zhangfei Gao | f1a7757 | 2012-09-03 11:03:46 +0800 | [diff] [blame] | 712 | .of_match_table = mmp_tdma_dt_ids, |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 713 | }, |
| 714 | .id_table = mmp_tdma_id_table, |
| 715 | .probe = mmp_tdma_probe, |
Bill Pemberton | a7d6e3e | 2012-11-19 13:20:04 -0500 | [diff] [blame] | 716 | .remove = mmp_tdma_remove, |
Zhangfei Gao | c6da0ba | 2012-06-15 11:04:08 +0800 | [diff] [blame] | 717 | }; |
| 718 | |
| 719 | module_platform_driver(mmp_tdma_driver); |
| 720 | |
| 721 | MODULE_LICENSE("GPL"); |
| 722 | MODULE_DESCRIPTION("MMP Two-Channel DMA Driver"); |
| 723 | MODULE_ALIAS("platform:mmp-tdma"); |
| 724 | MODULE_AUTHOR("Leo Yan <leoy@marvell.com>"); |
| 725 | MODULE_AUTHOR("Zhangfei Gao <zhangfei.gao@marvell.com>"); |