omap3_dma.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. /* Copyright (C) 2011
  2. * Corscience GmbH & Co. KG - Simon Schwarz <schwarz@corscience.de>
  3. *
  4. * SPDX-License-Identifier: GPL-2.0+
  5. */
  6. /* This is a basic implementation of the SDMA/DMA4 controller of OMAP3
  7. * Tested on Silicon Revision major:0x4 minor:0x0
  8. */
  9. #include <common.h>
  10. #include <asm/arch/cpu.h>
  11. #include <asm/arch/omap3.h>
  12. #include <asm/arch/dma.h>
  13. #include <asm/io.h>
  14. #include <asm/errno.h>
  15. static struct dma4 *dma4_cfg = (struct dma4 *)OMAP34XX_DMA4_BASE;
  16. uint32_t dma_active; /* if a transfer is started the respective
  17. bit is set for the logical channel */
  18. /* Check if we have the given channel
  19. * PARAMETERS:
  20. * chan: Channel number
  21. *
  22. * RETURN of non-zero means error */
  23. static inline int check_channel(uint32_t chan)
  24. {
  25. if (chan < CHAN_NR_MIN || chan > CHAN_NR_MAX)
  26. return -EINVAL;
  27. return 0;
  28. }
  29. static inline void reset_irq(uint32_t chan)
  30. {
  31. /* reset IRQ reason */
  32. writel(0x1DFE, &dma4_cfg->chan[chan].csr);
  33. /* reset IRQ */
  34. writel((1 << chan), &dma4_cfg->irqstatus_l[0]);
  35. dma_active &= ~(1 << chan);
  36. }
  37. /* Set Source, Destination and Size of DMA transfer for the
  38. * specified channel.
  39. * PARAMETERS:
  40. * chan: channel to use
  41. * src: source of the transfer
  42. * dst: destination of the transfer
  43. * sze: Size of the transfer
  44. *
  45. * RETURN of non-zero means error */
  46. int omap3_dma_conf_transfer(uint32_t chan, uint32_t *src, uint32_t *dst,
  47. uint32_t sze)
  48. {
  49. if (check_channel(chan))
  50. return -EINVAL;
  51. /* CDSA0 */
  52. writel((uint32_t)src, &dma4_cfg->chan[chan].cssa);
  53. writel((uint32_t)dst, &dma4_cfg->chan[chan].cdsa);
  54. writel(sze, &dma4_cfg->chan[chan].cen);
  55. return 0;
  56. }
  57. /* Start the DMA transfer */
  58. int omap3_dma_start_transfer(uint32_t chan)
  59. {
  60. uint32_t val;
  61. if (check_channel(chan))
  62. return -EINVAL;
  63. val = readl(&dma4_cfg->chan[chan].ccr);
  64. /* Test for channel already in use */
  65. if (val & CCR_ENABLE_ENABLE)
  66. return -EBUSY;
  67. writel((val | CCR_ENABLE_ENABLE), &dma4_cfg->chan[chan].ccr);
  68. dma_active |= (1 << chan);
  69. debug("started transfer...\n");
  70. return 0;
  71. }
  72. /* Busy-waiting for a DMA transfer
  73. * This has to be called before another transfer is started
  74. * PARAMETER
  75. * chan: Channel to wait for
  76. *
  77. * RETURN of non-zero means error*/
  78. int omap3_dma_wait_for_transfer(uint32_t chan)
  79. {
  80. uint32_t val;
  81. if (!(dma_active & (1 << chan))) {
  82. val = readl(&dma4_cfg->irqstatus_l[0]);
  83. if (!(val & chan)) {
  84. debug("dma: The channel you are trying to wait for "
  85. "was never activated - ERROR\n");
  86. return -1; /* channel was never active */
  87. }
  88. }
  89. /* all irqs on line 0 */
  90. while (!(readl(&dma4_cfg->irqstatus_l[0]) & (1 << chan)))
  91. asm("nop");
  92. val = readl(&dma4_cfg->chan[chan].csr);
  93. if ((val & CSR_TRANS_ERR) | (val & CSR_SUPERVISOR_ERR) |
  94. (val & CSR_MISALIGNED_ADRS_ERR)) {
  95. debug("err code: %X\n", val);
  96. debug("dma: transfer error detected\n");
  97. reset_irq(chan);
  98. return -1;
  99. }
  100. reset_irq(chan);
  101. return 0;
  102. }
  103. /* Get the revision of the DMA module
  104. * PARAMETER
  105. * minor: Address of minor revision to write
  106. * major: Address of major revision to write
  107. *
  108. * RETURN of non-zero means error
  109. */
  110. int omap3_dma_get_revision(uint32_t *minor, uint32_t *major)
  111. {
  112. uint32_t val;
  113. /* debug information */
  114. val = readl(&dma4_cfg->revision);
  115. *major = (val & 0x000000F0) >> 4;
  116. *minor = (val & 0x0000000F);
  117. debug("DMA Silicon revision (maj/min): 0x%X/0x%X\n", *major, *minor);
  118. return 0;
  119. }
  120. /* Initial config of omap dma
  121. */
  122. void omap3_dma_init(void)
  123. {
  124. dma_active = 0;
  125. /* All interrupts on channel 0 */
  126. writel(0xFFFFFFFF, &dma4_cfg->irqenable_l[0]);
  127. }
  128. /* set channel config to config
  129. *
  130. * RETURN of non-zero means error */
  131. int omap3_dma_conf_chan(uint32_t chan, struct dma4_chan *config)
  132. {
  133. if (check_channel(chan))
  134. return -EINVAL;
  135. dma4_cfg->chan[chan] = *config;
  136. return 0;
  137. }
  138. /* get channel config to config
  139. *
  140. * RETURN of non-zero means error */
  141. int omap3_dma_get_conf_chan(uint32_t chan, struct dma4_chan *config)
  142. {
  143. if (check_channel(chan))
  144. return -EINVAL;
  145. *config = dma4_cfg->chan[chan];
  146. return 0;
  147. }