sdhci-adma.c 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * SDHCI ADMA2 helper functions.
  4. */
  5. #include <common.h>
  6. #include <cpu_func.h>
  7. #include <sdhci.h>
  8. #include <malloc.h>
  9. #include <asm/cache.h>
  10. static void sdhci_adma_desc(struct sdhci_adma_desc *desc,
  11. dma_addr_t addr, u16 len, bool end)
  12. {
  13. u8 attr;
  14. attr = ADMA_DESC_ATTR_VALID | ADMA_DESC_TRANSFER_DATA;
  15. if (end)
  16. attr |= ADMA_DESC_ATTR_END;
  17. desc->attr = attr;
  18. desc->len = len;
  19. desc->reserved = 0;
  20. desc->addr_lo = lower_32_bits(addr);
  21. #ifdef CONFIG_DMA_ADDR_T_64BIT
  22. desc->addr_hi = upper_32_bits(addr);
  23. #endif
  24. }
  25. /**
  26. * sdhci_prepare_adma_table() - Populate the ADMA table
  27. *
  28. * @table: Pointer to the ADMA table
  29. * @data: Pointer to MMC data
  30. * @addr: DMA address to write to or read from
  31. *
  32. * Fill the ADMA table according to the MMC data to read from or write to the
  33. * given DMA address.
  34. * Please note, that the table size depends on CONFIG_SYS_MMC_MAX_BLK_COUNT and
  35. * we don't have to check for overflow.
  36. */
  37. void sdhci_prepare_adma_table(struct sdhci_adma_desc *table,
  38. struct mmc_data *data, dma_addr_t addr)
  39. {
  40. uint trans_bytes = data->blocksize * data->blocks;
  41. uint desc_count = DIV_ROUND_UP(trans_bytes, ADMA_MAX_LEN);
  42. struct sdhci_adma_desc *desc = table;
  43. int i = desc_count;
  44. while (--i) {
  45. sdhci_adma_desc(desc, addr, ADMA_MAX_LEN, false);
  46. addr += ADMA_MAX_LEN;
  47. trans_bytes -= ADMA_MAX_LEN;
  48. desc++;
  49. }
  50. sdhci_adma_desc(desc, addr, trans_bytes, true);
  51. flush_cache((dma_addr_t)table,
  52. ROUND(desc_count * sizeof(struct sdhci_adma_desc),
  53. ARCH_DMA_MINALIGN));
  54. }
  55. /**
  56. * sdhci_adma_init() - initialize the ADMA descriptor table
  57. *
  58. * @return pointer to the allocated descriptor table or NULL in case of an
  59. * error.
  60. */
  61. struct sdhci_adma_desc *sdhci_adma_init(void)
  62. {
  63. return memalign(ARCH_DMA_MINALIGN, ADMA_TABLE_SZ);
  64. }