Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Freescale i.MX28 APBH DMA driver |
| 4 | * |
| 5 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 6 | * on behalf of DENX Software Engineering GmbH |
| 7 | * |
| 8 | * Based on code from LTIB: |
| 9 | * Copyright (C) 2010 Freescale Semiconductor, Inc. All Rights Reserved. |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 10 | * Copyright 2017 NXP |
| 11 | * |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 12 | */ |
| 13 | |
Simon Glass | 6333448 | 2019-11-14 12:57:39 -0700 | [diff] [blame] | 14 | #include <cpu_func.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 15 | #include <asm/cache.h> |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 16 | #include <linux/list.h> |
| 17 | |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 18 | #include <malloc.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 19 | #include <linux/errno.h> |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 20 | #include <asm/io.h> |
| 21 | #include <asm/arch/clock.h> |
| 22 | #include <asm/arch/imx-regs.h> |
| 23 | #include <asm/arch/sys_proto.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 24 | #include <asm/mach-imx/dma.h> |
| 25 | #include <asm/mach-imx/regs-apbh.h> |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 26 | |
| 27 | static struct mxs_dma_chan mxs_dma_channels[MXS_MAX_DMA_CHANNELS]; |
| 28 | |
| 29 | /* |
| 30 | * Test is the DMA channel is valid channel |
| 31 | */ |
| 32 | int mxs_dma_validate_chan(int channel) |
| 33 | { |
| 34 | struct mxs_dma_chan *pchan; |
| 35 | |
| 36 | if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS)) |
| 37 | return -EINVAL; |
| 38 | |
| 39 | pchan = mxs_dma_channels + channel; |
| 40 | if (!(pchan->flags & MXS_DMA_FLAGS_ALLOCATED)) |
| 41 | return -EINVAL; |
| 42 | |
| 43 | return 0; |
| 44 | } |
| 45 | |
| 46 | /* |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 47 | * Return the address of the command within a descriptor. |
| 48 | */ |
| 49 | static unsigned int mxs_dma_cmd_address(struct mxs_dma_desc *desc) |
| 50 | { |
| 51 | return desc->address + offsetof(struct mxs_dma_desc, cmd); |
| 52 | } |
| 53 | |
| 54 | /* |
| 55 | * Read a DMA channel's hardware semaphore. |
| 56 | * |
| 57 | * As used by the MXS platform's DMA software, the DMA channel's hardware |
| 58 | * semaphore reflects the number of DMA commands the hardware will process, but |
| 59 | * has not yet finished. This is a volatile value read directly from hardware, |
| 60 | * so it must be be viewed as immediately stale. |
| 61 | * |
| 62 | * If the channel is not marked busy, or has finished processing all its |
| 63 | * commands, this value should be zero. |
| 64 | * |
| 65 | * See mxs_dma_append() for details on how DMA command blocks must be configured |
| 66 | * to maintain the expected behavior of the semaphore's value. |
| 67 | */ |
| 68 | static int mxs_dma_read_semaphore(int channel) |
| 69 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 70 | struct mxs_apbh_regs *apbh_regs = |
| 71 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 72 | uint32_t tmp; |
| 73 | int ret; |
| 74 | |
| 75 | ret = mxs_dma_validate_chan(channel); |
| 76 | if (ret) |
| 77 | return ret; |
| 78 | |
| 79 | tmp = readl(&apbh_regs->ch[channel].hw_apbh_ch_sema); |
| 80 | |
| 81 | tmp &= APBH_CHn_SEMA_PHORE_MASK; |
| 82 | tmp >>= APBH_CHn_SEMA_PHORE_OFFSET; |
| 83 | |
| 84 | return tmp; |
| 85 | } |
| 86 | |
Trevor Woerner | 43ec7e0 | 2019-05-03 09:41:00 -0400 | [diff] [blame] | 87 | #if !CONFIG_IS_ENABLED(SYS_DCACHE_OFF) |
Marek Vasut | 2642766 | 2012-03-15 18:33:18 +0000 | [diff] [blame] | 88 | void mxs_dma_flush_desc(struct mxs_dma_desc *desc) |
| 89 | { |
| 90 | uint32_t addr; |
| 91 | uint32_t size; |
| 92 | |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 93 | addr = (uintptr_t)desc; |
Marek Vasut | 2642766 | 2012-03-15 18:33:18 +0000 | [diff] [blame] | 94 | size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT); |
| 95 | |
| 96 | flush_dcache_range(addr, addr + size); |
| 97 | } |
| 98 | #else |
| 99 | inline void mxs_dma_flush_desc(struct mxs_dma_desc *desc) {} |
| 100 | #endif |
| 101 | |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 102 | /* |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 103 | * Enable a DMA channel. |
| 104 | * |
| 105 | * If the given channel has any DMA descriptors on its active list, this |
| 106 | * function causes the DMA hardware to begin processing them. |
| 107 | * |
| 108 | * This function marks the DMA channel as "busy," whether or not there are any |
| 109 | * descriptors to process. |
| 110 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 111 | static int mxs_dma_enable(int channel) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 112 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 113 | struct mxs_apbh_regs *apbh_regs = |
| 114 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 115 | unsigned int sem; |
| 116 | struct mxs_dma_chan *pchan; |
| 117 | struct mxs_dma_desc *pdesc; |
| 118 | int ret; |
| 119 | |
| 120 | ret = mxs_dma_validate_chan(channel); |
| 121 | if (ret) |
| 122 | return ret; |
| 123 | |
| 124 | pchan = mxs_dma_channels + channel; |
| 125 | |
| 126 | if (pchan->pending_num == 0) { |
| 127 | pchan->flags |= MXS_DMA_FLAGS_BUSY; |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | pdesc = list_first_entry(&pchan->active, struct mxs_dma_desc, node); |
| 132 | if (pdesc == NULL) |
| 133 | return -EFAULT; |
| 134 | |
| 135 | if (pchan->flags & MXS_DMA_FLAGS_BUSY) { |
| 136 | if (!(pdesc->cmd.data & MXS_DMA_DESC_CHAIN)) |
| 137 | return 0; |
| 138 | |
| 139 | sem = mxs_dma_read_semaphore(channel); |
| 140 | if (sem == 0) |
| 141 | return 0; |
| 142 | |
| 143 | if (sem == 1) { |
| 144 | pdesc = list_entry(pdesc->node.next, |
| 145 | struct mxs_dma_desc, node); |
| 146 | writel(mxs_dma_cmd_address(pdesc), |
| 147 | &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar); |
| 148 | } |
| 149 | writel(pchan->pending_num, |
| 150 | &apbh_regs->ch[channel].hw_apbh_ch_sema); |
| 151 | pchan->active_num += pchan->pending_num; |
| 152 | pchan->pending_num = 0; |
| 153 | } else { |
| 154 | pchan->active_num += pchan->pending_num; |
| 155 | pchan->pending_num = 0; |
| 156 | writel(mxs_dma_cmd_address(pdesc), |
| 157 | &apbh_regs->ch[channel].hw_apbh_ch_nxtcmdar); |
| 158 | writel(pchan->active_num, |
| 159 | &apbh_regs->ch[channel].hw_apbh_ch_sema); |
| 160 | writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET), |
| 161 | &apbh_regs->hw_apbh_ctrl0_clr); |
| 162 | } |
| 163 | |
| 164 | pchan->flags |= MXS_DMA_FLAGS_BUSY; |
| 165 | return 0; |
| 166 | } |
| 167 | |
| 168 | /* |
| 169 | * Disable a DMA channel. |
| 170 | * |
| 171 | * This function shuts down a DMA channel and marks it as "not busy." Any |
| 172 | * descriptors on the active list are immediately moved to the head of the |
| 173 | * "done" list, whether or not they have actually been processed by the |
| 174 | * hardware. The "ready" flags of these descriptors are NOT cleared, so they |
| 175 | * still appear to be active. |
| 176 | * |
| 177 | * This function immediately shuts down a DMA channel's hardware, aborting any |
| 178 | * I/O that may be in progress, potentially leaving I/O hardware in an undefined |
| 179 | * state. It is unwise to call this function if there is ANY chance the hardware |
| 180 | * is still processing a command. |
| 181 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 182 | static int mxs_dma_disable(int channel) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 183 | { |
| 184 | struct mxs_dma_chan *pchan; |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 185 | struct mxs_apbh_regs *apbh_regs = |
| 186 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 187 | int ret; |
| 188 | |
| 189 | ret = mxs_dma_validate_chan(channel); |
| 190 | if (ret) |
| 191 | return ret; |
| 192 | |
| 193 | pchan = mxs_dma_channels + channel; |
| 194 | |
| 195 | if (!(pchan->flags & MXS_DMA_FLAGS_BUSY)) |
| 196 | return -EINVAL; |
| 197 | |
| 198 | writel(1 << (channel + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET), |
| 199 | &apbh_regs->hw_apbh_ctrl0_set); |
| 200 | |
| 201 | pchan->flags &= ~MXS_DMA_FLAGS_BUSY; |
| 202 | pchan->active_num = 0; |
| 203 | pchan->pending_num = 0; |
| 204 | list_splice_init(&pchan->active, &pchan->done); |
| 205 | |
| 206 | return 0; |
| 207 | } |
| 208 | |
| 209 | /* |
| 210 | * Resets the DMA channel hardware. |
| 211 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 212 | static int mxs_dma_reset(int channel) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 213 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 214 | struct mxs_apbh_regs *apbh_regs = |
| 215 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 216 | int ret; |
Marek Vasut | 154af3a | 2013-01-22 15:01:00 +0000 | [diff] [blame] | 217 | #if defined(CONFIG_MX23) |
| 218 | uint32_t setreg = (uint32_t)(&apbh_regs->hw_apbh_ctrl0_set); |
| 219 | uint32_t offset = APBH_CTRL0_RESET_CHANNEL_OFFSET; |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 220 | #elif defined(CONFIG_MX28) || defined(CONFIG_MX6) || defined(CONFIG_MX7) || \ |
| 221 | defined(CONFIG_IMX8) || defined(CONFIG_IMX8M) |
| 222 | u32 setreg = (uintptr_t)(&apbh_regs->hw_apbh_channel_ctrl_set); |
| 223 | u32 offset = APBH_CHANNEL_CTRL_RESET_CHANNEL_OFFSET; |
Marek Vasut | 154af3a | 2013-01-22 15:01:00 +0000 | [diff] [blame] | 224 | #endif |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 225 | |
| 226 | ret = mxs_dma_validate_chan(channel); |
| 227 | if (ret) |
| 228 | return ret; |
| 229 | |
Peng Fan | 128abf4 | 2020-05-04 22:09:00 +0800 | [diff] [blame] | 230 | writel(1 << (channel + offset), (uintptr_t)setreg); |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | /* |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 236 | * Enable or disable DMA interrupt. |
| 237 | * |
| 238 | * This function enables the given DMA channel to interrupt the CPU. |
| 239 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 240 | static int mxs_dma_enable_irq(int channel, int enable) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 241 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 242 | struct mxs_apbh_regs *apbh_regs = |
| 243 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 244 | int ret; |
| 245 | |
| 246 | ret = mxs_dma_validate_chan(channel); |
| 247 | if (ret) |
| 248 | return ret; |
| 249 | |
| 250 | if (enable) |
| 251 | writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET), |
| 252 | &apbh_regs->hw_apbh_ctrl1_set); |
| 253 | else |
| 254 | writel(1 << (channel + APBH_CTRL1_CH_CMDCMPLT_IRQ_EN_OFFSET), |
| 255 | &apbh_regs->hw_apbh_ctrl1_clr); |
| 256 | |
| 257 | return 0; |
| 258 | } |
| 259 | |
| 260 | /* |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 261 | * Clear DMA interrupt. |
| 262 | * |
| 263 | * The software that is using the DMA channel must register to receive its |
| 264 | * interrupts and, when they arrive, must call this function to clear them. |
| 265 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 266 | static int mxs_dma_ack_irq(int channel) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 267 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 268 | struct mxs_apbh_regs *apbh_regs = |
| 269 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 270 | int ret; |
| 271 | |
| 272 | ret = mxs_dma_validate_chan(channel); |
| 273 | if (ret) |
| 274 | return ret; |
| 275 | |
| 276 | writel(1 << channel, &apbh_regs->hw_apbh_ctrl1_clr); |
| 277 | writel(1 << channel, &apbh_regs->hw_apbh_ctrl2_clr); |
| 278 | |
| 279 | return 0; |
| 280 | } |
| 281 | |
| 282 | /* |
| 283 | * Request to reserve a DMA channel |
| 284 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 285 | static int mxs_dma_request(int channel) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 286 | { |
| 287 | struct mxs_dma_chan *pchan; |
| 288 | |
| 289 | if ((channel < 0) || (channel >= MXS_MAX_DMA_CHANNELS)) |
| 290 | return -EINVAL; |
| 291 | |
| 292 | pchan = mxs_dma_channels + channel; |
| 293 | if ((pchan->flags & MXS_DMA_FLAGS_VALID) != MXS_DMA_FLAGS_VALID) |
| 294 | return -ENODEV; |
| 295 | |
| 296 | if (pchan->flags & MXS_DMA_FLAGS_ALLOCATED) |
| 297 | return -EBUSY; |
| 298 | |
| 299 | pchan->flags |= MXS_DMA_FLAGS_ALLOCATED; |
| 300 | pchan->active_num = 0; |
| 301 | pchan->pending_num = 0; |
| 302 | |
| 303 | INIT_LIST_HEAD(&pchan->active); |
| 304 | INIT_LIST_HEAD(&pchan->done); |
| 305 | |
| 306 | return 0; |
| 307 | } |
| 308 | |
| 309 | /* |
| 310 | * Release a DMA channel. |
| 311 | * |
| 312 | * This function releases a DMA channel from its current owner. |
| 313 | * |
| 314 | * The channel will NOT be released if it's marked "busy" (see |
| 315 | * mxs_dma_enable()). |
| 316 | */ |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 317 | int mxs_dma_release(int channel) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 318 | { |
| 319 | struct mxs_dma_chan *pchan; |
| 320 | int ret; |
| 321 | |
| 322 | ret = mxs_dma_validate_chan(channel); |
| 323 | if (ret) |
| 324 | return ret; |
| 325 | |
| 326 | pchan = mxs_dma_channels + channel; |
| 327 | |
| 328 | if (pchan->flags & MXS_DMA_FLAGS_BUSY) |
| 329 | return -EBUSY; |
| 330 | |
| 331 | pchan->dev = 0; |
| 332 | pchan->active_num = 0; |
| 333 | pchan->pending_num = 0; |
| 334 | pchan->flags &= ~MXS_DMA_FLAGS_ALLOCATED; |
| 335 | |
| 336 | return 0; |
| 337 | } |
| 338 | |
| 339 | /* |
| 340 | * Allocate DMA descriptor |
| 341 | */ |
| 342 | struct mxs_dma_desc *mxs_dma_desc_alloc(void) |
| 343 | { |
| 344 | struct mxs_dma_desc *pdesc; |
Marek Vasut | 2642766 | 2012-03-15 18:33:18 +0000 | [diff] [blame] | 345 | uint32_t size; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 346 | |
Marek Vasut | 2642766 | 2012-03-15 18:33:18 +0000 | [diff] [blame] | 347 | size = roundup(sizeof(struct mxs_dma_desc), MXS_DMA_ALIGNMENT); |
| 348 | pdesc = memalign(MXS_DMA_ALIGNMENT, size); |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 349 | |
| 350 | if (pdesc == NULL) |
| 351 | return NULL; |
| 352 | |
| 353 | memset(pdesc, 0, sizeof(*pdesc)); |
| 354 | pdesc->address = (dma_addr_t)pdesc; |
| 355 | |
| 356 | return pdesc; |
| 357 | }; |
| 358 | |
| 359 | /* |
| 360 | * Free DMA descriptor |
| 361 | */ |
| 362 | void mxs_dma_desc_free(struct mxs_dma_desc *pdesc) |
| 363 | { |
| 364 | if (pdesc == NULL) |
| 365 | return; |
| 366 | |
| 367 | free(pdesc); |
| 368 | } |
| 369 | |
| 370 | /* |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 371 | * Add a DMA descriptor to a channel. |
| 372 | * |
| 373 | * If the descriptor list for this channel is not empty, this function sets the |
| 374 | * CHAIN bit and the NEXTCMD_ADDR fields in the last descriptor's DMA command so |
| 375 | * it will chain to the new descriptor's command. |
| 376 | * |
| 377 | * Then, this function marks the new descriptor as "ready," adds it to the end |
| 378 | * of the active descriptor list, and increments the count of pending |
| 379 | * descriptors. |
| 380 | * |
| 381 | * The MXS platform DMA software imposes some rules on DMA commands to maintain |
| 382 | * important invariants. These rules are NOT checked, but they must be carefully |
| 383 | * applied by software that uses MXS DMA channels. |
| 384 | * |
| 385 | * Invariant: |
| 386 | * The DMA channel's hardware semaphore must reflect the number of DMA |
| 387 | * commands the hardware will process, but has not yet finished. |
| 388 | * |
| 389 | * Explanation: |
| 390 | * A DMA channel begins processing commands when its hardware semaphore is |
| 391 | * written with a value greater than zero, and it stops processing commands |
| 392 | * when the semaphore returns to zero. |
| 393 | * |
| 394 | * When a channel finishes a DMA command, it will decrement its semaphore if |
| 395 | * the DECREMENT_SEMAPHORE bit is set in that command's flags bits. |
| 396 | * |
| 397 | * In principle, it's not necessary for the DECREMENT_SEMAPHORE to be set, |
| 398 | * unless it suits the purposes of the software. For example, one could |
| 399 | * construct a series of five DMA commands, with the DECREMENT_SEMAPHORE |
| 400 | * bit set only in the last one. Then, setting the DMA channel's hardware |
| 401 | * semaphore to one would cause the entire series of five commands to be |
| 402 | * processed. However, this example would violate the invariant given above. |
| 403 | * |
| 404 | * Rule: |
| 405 | * ALL DMA commands MUST have the DECREMENT_SEMAPHORE bit set so that the DMA |
| 406 | * channel's hardware semaphore will be decremented EVERY time a command is |
| 407 | * processed. |
| 408 | */ |
| 409 | int mxs_dma_desc_append(int channel, struct mxs_dma_desc *pdesc) |
| 410 | { |
| 411 | struct mxs_dma_chan *pchan; |
| 412 | struct mxs_dma_desc *last; |
| 413 | int ret; |
| 414 | |
| 415 | ret = mxs_dma_validate_chan(channel); |
| 416 | if (ret) |
| 417 | return ret; |
| 418 | |
| 419 | pchan = mxs_dma_channels + channel; |
| 420 | |
| 421 | pdesc->cmd.next = mxs_dma_cmd_address(pdesc); |
| 422 | pdesc->flags |= MXS_DMA_DESC_FIRST | MXS_DMA_DESC_LAST; |
| 423 | |
| 424 | if (!list_empty(&pchan->active)) { |
| 425 | last = list_entry(pchan->active.prev, struct mxs_dma_desc, |
| 426 | node); |
| 427 | |
| 428 | pdesc->flags &= ~MXS_DMA_DESC_FIRST; |
| 429 | last->flags &= ~MXS_DMA_DESC_LAST; |
| 430 | |
| 431 | last->cmd.next = mxs_dma_cmd_address(pdesc); |
| 432 | last->cmd.data |= MXS_DMA_DESC_CHAIN; |
Marek Vasut | 2642766 | 2012-03-15 18:33:18 +0000 | [diff] [blame] | 433 | |
| 434 | mxs_dma_flush_desc(last); |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 435 | } |
| 436 | pdesc->flags |= MXS_DMA_DESC_READY; |
| 437 | if (pdesc->flags & MXS_DMA_DESC_FIRST) |
| 438 | pchan->pending_num++; |
| 439 | list_add_tail(&pdesc->node, &pchan->active); |
| 440 | |
Marek Vasut | 2642766 | 2012-03-15 18:33:18 +0000 | [diff] [blame] | 441 | mxs_dma_flush_desc(pdesc); |
| 442 | |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 443 | return ret; |
| 444 | } |
| 445 | |
| 446 | /* |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 447 | * Clean up processed DMA descriptors. |
| 448 | * |
| 449 | * This function removes processed DMA descriptors from the "active" list. Pass |
| 450 | * in a non-NULL list head to get the descriptors moved to your list. Pass NULL |
| 451 | * to get the descriptors moved to the channel's "done" list. Descriptors on |
| 452 | * the "done" list can be retrieved with mxs_dma_get_finished(). |
| 453 | * |
| 454 | * This function marks the DMA channel as "not busy" if no unprocessed |
| 455 | * descriptors remain on the "active" list. |
| 456 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 457 | static int mxs_dma_finish(int channel, struct list_head *head) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 458 | { |
| 459 | int sem; |
| 460 | struct mxs_dma_chan *pchan; |
| 461 | struct list_head *p, *q; |
| 462 | struct mxs_dma_desc *pdesc; |
| 463 | int ret; |
| 464 | |
| 465 | ret = mxs_dma_validate_chan(channel); |
| 466 | if (ret) |
| 467 | return ret; |
| 468 | |
| 469 | pchan = mxs_dma_channels + channel; |
| 470 | |
| 471 | sem = mxs_dma_read_semaphore(channel); |
| 472 | if (sem < 0) |
| 473 | return sem; |
| 474 | |
| 475 | if (sem == pchan->active_num) |
| 476 | return 0; |
| 477 | |
| 478 | list_for_each_safe(p, q, &pchan->active) { |
| 479 | if ((pchan->active_num) <= sem) |
| 480 | break; |
| 481 | |
| 482 | pdesc = list_entry(p, struct mxs_dma_desc, node); |
| 483 | pdesc->flags &= ~MXS_DMA_DESC_READY; |
| 484 | |
| 485 | if (head) |
| 486 | list_move_tail(p, head); |
| 487 | else |
| 488 | list_move_tail(p, &pchan->done); |
| 489 | |
| 490 | if (pdesc->flags & MXS_DMA_DESC_LAST) |
| 491 | pchan->active_num--; |
| 492 | } |
| 493 | |
| 494 | if (sem == 0) |
| 495 | pchan->flags &= ~MXS_DMA_FLAGS_BUSY; |
| 496 | |
| 497 | return 0; |
| 498 | } |
| 499 | |
| 500 | /* |
| 501 | * Wait for DMA channel to complete |
| 502 | */ |
Marek Vasut | 4742c26 | 2011-11-23 10:59:13 +0000 | [diff] [blame] | 503 | static int mxs_dma_wait_complete(uint32_t timeout, unsigned int chan) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 504 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 505 | struct mxs_apbh_regs *apbh_regs = |
| 506 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 507 | int ret; |
| 508 | |
| 509 | ret = mxs_dma_validate_chan(chan); |
| 510 | if (ret) |
| 511 | return ret; |
| 512 | |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 513 | if (mxs_wait_mask_set(&apbh_regs->hw_apbh_ctrl1_reg, |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 514 | 1 << chan, timeout)) { |
| 515 | ret = -ETIMEDOUT; |
| 516 | mxs_dma_reset(chan); |
| 517 | } |
| 518 | |
Wolfram Sang | 98ee923 | 2011-11-18 01:17:44 +0000 | [diff] [blame] | 519 | return ret; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 520 | } |
| 521 | |
| 522 | /* |
| 523 | * Execute the DMA channel |
| 524 | */ |
| 525 | int mxs_dma_go(int chan) |
| 526 | { |
Marek Vasut | 0f6f68c | 2012-08-21 16:17:26 +0000 | [diff] [blame] | 527 | uint32_t timeout = 10000000; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 528 | int ret; |
| 529 | |
| 530 | LIST_HEAD(tmp_desc_list); |
| 531 | |
| 532 | mxs_dma_enable_irq(chan, 1); |
| 533 | mxs_dma_enable(chan); |
| 534 | |
| 535 | /* Wait for DMA to finish. */ |
| 536 | ret = mxs_dma_wait_complete(timeout, chan); |
| 537 | |
| 538 | /* Clear out the descriptors we just ran. */ |
| 539 | mxs_dma_finish(chan, &tmp_desc_list); |
| 540 | |
| 541 | /* Shut the DMA channel down. */ |
| 542 | mxs_dma_ack_irq(chan); |
| 543 | mxs_dma_reset(chan); |
| 544 | mxs_dma_enable_irq(chan, 0); |
| 545 | mxs_dma_disable(chan); |
| 546 | |
| 547 | return ret; |
| 548 | } |
| 549 | |
| 550 | /* |
Marek Vasut | 4291780 | 2013-07-30 23:37:51 +0200 | [diff] [blame] | 551 | * Execute a continuously running circular DMA descriptor. |
| 552 | * NOTE: This is not intended for general use, but rather |
| 553 | * for the LCD driver in Smart-LCD mode. It allows |
| 554 | * continuous triggering of the RUN bit there. |
| 555 | */ |
| 556 | void mxs_dma_circ_start(int chan, struct mxs_dma_desc *pdesc) |
| 557 | { |
| 558 | struct mxs_apbh_regs *apbh_regs = |
| 559 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
| 560 | |
| 561 | mxs_dma_flush_desc(pdesc); |
| 562 | |
| 563 | mxs_dma_enable_irq(chan, 1); |
| 564 | |
| 565 | writel(mxs_dma_cmd_address(pdesc), |
| 566 | &apbh_regs->ch[chan].hw_apbh_ch_nxtcmdar); |
| 567 | writel(1, &apbh_regs->ch[chan].hw_apbh_ch_sema); |
| 568 | writel(1 << (chan + APBH_CTRL0_CLKGATE_CHANNEL_OFFSET), |
| 569 | &apbh_regs->hw_apbh_ctrl0_clr); |
| 570 | } |
| 571 | |
| 572 | /* |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 573 | * Initialize the DMA hardware |
| 574 | */ |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 575 | void mxs_dma_init(void) |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 576 | { |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 577 | struct mxs_apbh_regs *apbh_regs = |
| 578 | (struct mxs_apbh_regs *)MXS_APBH_BASE; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 579 | |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 580 | mxs_reset_block(&apbh_regs->hw_apbh_ctrl0_reg); |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 581 | |
| 582 | #ifdef CONFIG_APBH_DMA_BURST8 |
| 583 | writel(APBH_CTRL0_AHB_BURST8_EN, |
| 584 | &apbh_regs->hw_apbh_ctrl0_set); |
| 585 | #else |
| 586 | writel(APBH_CTRL0_AHB_BURST8_EN, |
| 587 | &apbh_regs->hw_apbh_ctrl0_clr); |
| 588 | #endif |
| 589 | |
| 590 | #ifdef CONFIG_APBH_DMA_BURST |
| 591 | writel(APBH_CTRL0_APB_BURST_EN, |
| 592 | &apbh_regs->hw_apbh_ctrl0_set); |
| 593 | #else |
| 594 | writel(APBH_CTRL0_APB_BURST_EN, |
| 595 | &apbh_regs->hw_apbh_ctrl0_clr); |
| 596 | #endif |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 597 | } |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 598 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 599 | int mxs_dma_init_channel(int channel) |
| 600 | { |
| 601 | struct mxs_dma_chan *pchan; |
| 602 | int ret; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 603 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 604 | pchan = mxs_dma_channels + channel; |
| 605 | pchan->flags = MXS_DMA_FLAGS_VALID; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 606 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 607 | ret = mxs_dma_request(channel); |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 608 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 609 | if (ret) { |
| 610 | printf("MXS DMA: Can't acquire DMA channel %i\n", |
| 611 | channel); |
| 612 | return ret; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 613 | } |
| 614 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 615 | mxs_dma_reset(channel); |
| 616 | mxs_dma_ack_irq(channel); |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 617 | |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 618 | return 0; |
Marek Vasut | f7c752c | 2011-11-08 23:18:15 +0000 | [diff] [blame] | 619 | } |