mmc: sdhci: reduce code duplication for aligned buffer
[oweals/u-boot.git] / drivers / mmc / sdhci.c
index 32e83db8e09be1d85b617d63cc811ea38f3295b6..b4713e7b9bba315f66fa5208fd43061f0da3b3dd 100644 (file)
@@ -8,6 +8,7 @@
  */
 
 #include <common.h>
+#include <cpu_func.h>
 #include <dm.h>
 #include <errno.h>
 #include <malloc.h>
 #include <sdhci.h>
 #include <dm.h>
 
-#if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
-void *aligned_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
-#else
-void *aligned_buffer;
-#endif
-
 static void sdhci_reset(struct sdhci_host *host, u8 mask)
 {
        unsigned long timeout;
@@ -145,26 +140,16 @@ static void sdhci_prepare_dma(struct sdhci_host *host, struct mmc_data *data,
        sdhci_writeb(host, ctrl, SDHCI_HOST_CONTROL);
 
        if (host->flags & USE_SDMA) {
-               if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
-                   (host->start_addr & 0x7) != 0x0) {
+               if (host->force_align_buffer ||
+                   (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR &&
+                    (host->start_addr & 0x7) != 0x0)) {
                        *is_aligned = 0;
-                       host->start_addr = (unsigned long)aligned_buffer;
+                       host->start_addr = (unsigned long)host->align_buffer;
                        if (data->flags != MMC_DATA_READ)
-                               memcpy(aligned_buffer, data->src, trans_bytes);
+                               memcpy(host->align_buffer, data->src,
+                                      trans_bytes);
                }
-
-#if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
-               /*
-                * Always use this bounce-buffer when
-                * CONFIG_FIXED_SDHCI_ALIGNED_BUFFER is defined
-                */
-               *is_aligned = 0;
-               host->start_addr = (unsigned long)aligned_buffer;
-               if (data->flags != MMC_DATA_READ)
-                       memcpy(aligned_buffer, data->src, trans_bytes);
-#endif
                sdhci_writel(host, host->start_addr, SDHCI_DMA_ADDRESS);
-
        } else if (host->flags & (USE_ADMA | USE_ADMA64)) {
                sdhci_prepare_adma_table(host, data);
 
@@ -380,7 +365,7 @@ static int sdhci_send_command(struct mmc *mmc, struct mmc_cmd *cmd,
        if (!ret) {
                if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) &&
                                !is_aligned && (data->flags == MMC_DATA_READ))
-                       memcpy(data->dest, aligned_buffer, trans_bytes);
+                       memcpy(data->dest, host->align_buffer, trans_bytes);
                return 0;
        }
 
@@ -629,14 +614,23 @@ static int sdhci_init(struct mmc *mmc)
 
        sdhci_reset(host, SDHCI_RESET_ALL);
 
-       if ((host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) && !aligned_buffer) {
-               aligned_buffer = memalign(8, 512*1024);
-               if (!aligned_buffer) {
+#if defined(CONFIG_FIXED_SDHCI_ALIGNED_BUFFER)
+       host->align_buffer = (void *)CONFIG_FIXED_SDHCI_ALIGNED_BUFFER;
+       /*
+        * Always use this bounce-buffer when CONFIG_FIXED_SDHCI_ALIGNED_BUFFER
+        * is defined.
+        */
+       host->force_align_buffer = true;
+#else
+       if (host->quirks & SDHCI_QUIRK_32BIT_DMA_ADDR) {
+               host->align_buffer = memalign(8, 512 * 1024);
+               if (!host->align_buffer) {
                        printf("%s: Aligned buffer alloc failed!!!\n",
                               __func__);
                        return -ENOMEM;
                }
        }
+#endif
 
        sdhci_set_power(host, fls(mmc->cfg->voltages) - 1);