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