lpc32xx_dma.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. /*
  2. * Copyright (C) 2008 by NXP Semiconductors
  3. * @Author: Kevin Wells
  4. * @Descr: LPC3250 DMA controller interface support functions
  5. *
  6. * Copyright (c) 2015 Tyco Fire Protection Products.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <asm/arch/dma.h>
  13. #include <asm/arch/cpu.h>
  14. #include <asm/arch/clk.h>
  15. #include <asm/arch/sys_proto.h>
  16. #include <asm/io.h>
  17. /* DMA controller channel register structure */
  18. struct dmac_chan_reg {
  19. u32 src_addr;
  20. u32 dest_addr;
  21. u32 lli;
  22. u32 control;
  23. u32 config_ch;
  24. u32 reserved[3];
  25. };
  26. /* DMA controller register structures */
  27. struct dma_reg {
  28. u32 int_stat;
  29. u32 int_tc_stat;
  30. u32 int_tc_clear;
  31. u32 int_err_stat;
  32. u32 int_err_clear;
  33. u32 raw_tc_stat;
  34. u32 raw_err_stat;
  35. u32 chan_enable;
  36. u32 sw_burst_req;
  37. u32 sw_single_req;
  38. u32 sw_last_burst_req;
  39. u32 sw_last_single_req;
  40. u32 config;
  41. u32 sync;
  42. u32 reserved[50];
  43. struct dmac_chan_reg dma_chan[8];
  44. };
  45. #define DMA_NO_OF_CHANNELS 8
  46. /* config register definitions */
  47. #define DMAC_CTRL_ENABLE (1 << 0) /* For enabling the DMA controller */
  48. static u32 alloc_ch;
  49. static struct dma_reg *dma = (struct dma_reg *)DMA_BASE;
  50. int lpc32xx_dma_get_channel(void)
  51. {
  52. int i;
  53. if (!alloc_ch) { /* First time caller */
  54. /*
  55. * DMA clock are enable by "lpc32xx_dma_init()" and should
  56. * be call by board "board_early_init_f()" function.
  57. */
  58. /*
  59. * Make sure DMA controller and all channels are disabled.
  60. * Controller is in little-endian mode. Disable sync signals.
  61. */
  62. writel(0, &dma->config);
  63. writel(0, &dma->sync);
  64. /* Clear interrupt and error statuses */
  65. writel(0xFF, &dma->int_tc_clear);
  66. writel(0xFF, &dma->raw_tc_stat);
  67. writel(0xFF, &dma->int_err_clear);
  68. writel(0xFF, &dma->raw_err_stat);
  69. /* Enable DMA controller */
  70. writel(DMAC_CTRL_ENABLE, &dma->config);
  71. }
  72. i = ffz(alloc_ch);
  73. /* Check if all the available channels are busy */
  74. if (unlikely(i == DMA_NO_OF_CHANNELS))
  75. return -1;
  76. alloc_ch |= BIT_MASK(i);
  77. return i;
  78. }
  79. int lpc32xx_dma_start_xfer(unsigned int channel,
  80. const struct lpc32xx_dmac_ll *desc, u32 config)
  81. {
  82. if (unlikely(((BIT_MASK(channel) & alloc_ch) == 0) ||
  83. (channel >= DMA_NO_OF_CHANNELS))) {
  84. error("Request for xfer on unallocated channel %d", channel);
  85. return -1;
  86. }
  87. writel(BIT_MASK(channel), &dma->int_tc_clear);
  88. writel(BIT_MASK(channel), &dma->int_err_clear);
  89. writel(desc->dma_src, &dma->dma_chan[channel].src_addr);
  90. writel(desc->dma_dest, &dma->dma_chan[channel].dest_addr);
  91. writel(desc->next_lli, &dma->dma_chan[channel].lli);
  92. writel(desc->next_ctrl, &dma->dma_chan[channel].control);
  93. writel(config, &dma->dma_chan[channel].config_ch);
  94. return 0;
  95. }
  96. int lpc32xx_dma_wait_status(unsigned int channel)
  97. {
  98. unsigned long start;
  99. u32 reg;
  100. /* Check if given channel is valid */
  101. if (unlikely(channel >= DMA_NO_OF_CHANNELS)) {
  102. error("Request for status on unallocated channel %d", channel);
  103. return -1;
  104. }
  105. start = get_timer(0);
  106. while (1) {
  107. reg = readl(&dma->raw_tc_stat);
  108. reg |= readl(dma->raw_err_stat);
  109. if (reg & BIT_MASK(channel))
  110. break;
  111. if (get_timer(start) > CONFIG_SYS_HZ) {
  112. error("DMA status timeout channel %d\n", channel);
  113. return -ETIMEDOUT;
  114. }
  115. udelay(1);
  116. }
  117. if (unlikely(readl(&dma->raw_err_stat) & BIT_MASK(channel))) {
  118. setbits_le32(&dma->int_err_clear, BIT_MASK(channel));
  119. setbits_le32(&dma->raw_err_stat, BIT_MASK(channel));
  120. error("DMA error on channel %d\n", channel);
  121. return -1;
  122. }
  123. setbits_le32(&dma->int_tc_clear, BIT_MASK(channel));
  124. setbits_le32(&dma->raw_tc_stat, BIT_MASK(channel));
  125. return 0;
  126. }