Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Marvell SD Host Controller Interface |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 4 | */ |
| 5 | |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 6 | #include <common.h> |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 7 | #include <dm.h> |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 8 | #include <malloc.h> |
| 9 | #include <sdhci.h> |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 10 | #include <linux/mbus.h> |
| 11 | |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 12 | #define MVSDH_NAME "mv_sdh" |
| 13 | |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 14 | #define SDHCI_WINDOW_CTRL(win) (0x4080 + ((win) << 4)) |
| 15 | #define SDHCI_WINDOW_BASE(win) (0x4084 + ((win) << 4)) |
| 16 | |
| 17 | static void sdhci_mvebu_mbus_config(void __iomem *base) |
| 18 | { |
| 19 | const struct mbus_dram_target_info *dram; |
| 20 | int i; |
| 21 | |
| 22 | dram = mvebu_mbus_dram_info(); |
| 23 | |
| 24 | for (i = 0; i < 4; i++) { |
| 25 | writel(0, base + SDHCI_WINDOW_CTRL(i)); |
| 26 | writel(0, base + SDHCI_WINDOW_BASE(i)); |
| 27 | } |
| 28 | |
| 29 | for (i = 0; i < dram->num_cs; i++) { |
| 30 | const struct mbus_dram_window *cs = dram->cs + i; |
| 31 | |
| 32 | /* Write size, attributes and target id to control register */ |
| 33 | writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) | |
| 34 | (dram->mbus_dram_target_id << 4) | 1, |
| 35 | base + SDHCI_WINDOW_CTRL(i)); |
| 36 | |
| 37 | /* Write base address to base register */ |
| 38 | writel(cs->base, base + SDHCI_WINDOW_BASE(i)); |
| 39 | } |
| 40 | } |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 41 | |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 42 | #ifndef CONFIG_DM_MMC |
| 43 | |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 44 | #ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS |
| 45 | static struct sdhci_ops mv_ops; |
| 46 | |
| 47 | #if defined(CONFIG_SHEEVA_88SV331xV5) |
| 48 | #define SD_CE_ATA_2 0xEA |
| 49 | #define MMC_CARD 0x1000 |
| 50 | #define MMC_WIDTH 0x0100 |
| 51 | static inline void mv_sdhci_writeb(struct sdhci_host *host, u8 val, int reg) |
| 52 | { |
| 53 | struct mmc *mmc = host->mmc; |
Rob Herring | 8d3a2a7 | 2015-03-17 15:46:39 -0500 | [diff] [blame] | 54 | u32 ata = (unsigned long)host->ioaddr + SD_CE_ATA_2; |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 55 | |
| 56 | if (!IS_SD(mmc) && reg == SDHCI_HOST_CONTROL) { |
| 57 | if (mmc->bus_width == 8) |
| 58 | writew(readw(ata) | (MMC_CARD | MMC_WIDTH), ata); |
| 59 | else |
| 60 | writew(readw(ata) & ~(MMC_CARD | MMC_WIDTH), ata); |
| 61 | } |
| 62 | |
| 63 | writeb(val, host->ioaddr + reg); |
| 64 | } |
| 65 | |
| 66 | #else |
| 67 | #define mv_sdhci_writeb NULL |
| 68 | #endif /* CONFIG_SHEEVA_88SV331xV5 */ |
| 69 | #endif /* CONFIG_MMC_SDHCI_IO_ACCESSORS */ |
| 70 | |
Rob Herring | 8d3a2a7 | 2015-03-17 15:46:39 -0500 | [diff] [blame] | 71 | int mv_sdh_init(unsigned long regbase, u32 max_clk, u32 min_clk, u32 quirks) |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 72 | { |
| 73 | struct sdhci_host *host = NULL; |
Matt Pelland | 3bf0e42 | 2018-04-16 10:08:18 -0400 | [diff] [blame] | 74 | host = calloc(1, sizeof(*host)); |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 75 | if (!host) { |
| 76 | printf("sdh_host malloc fail!\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 77 | return -ENOMEM; |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 78 | } |
| 79 | |
| 80 | host->name = MVSDH_NAME; |
| 81 | host->ioaddr = (void *)regbase; |
| 82 | host->quirks = quirks; |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 83 | host->max_clk = max_clk; |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 84 | #ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS |
| 85 | memset(&mv_ops, 0, sizeof(struct sdhci_ops)); |
Anatolij Gustschin | e001a69 | 2011-12-07 11:47:48 +0000 | [diff] [blame] | 86 | mv_ops.write_b = mv_sdhci_writeb; |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 87 | host->ops = &mv_ops; |
| 88 | #endif |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 89 | |
| 90 | if (CONFIG_IS_ENABLED(ARCH_MVEBU)) { |
| 91 | /* Configure SDHCI MBUS mbus bridge windows */ |
| 92 | sdhci_mvebu_mbus_config((void __iomem *)regbase); |
| 93 | } |
| 94 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 95 | return add_sdhci(host, 0, min_clk); |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 96 | } |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 97 | |
| 98 | #else |
| 99 | |
| 100 | DECLARE_GLOBAL_DATA_PTR; |
| 101 | |
| 102 | struct mv_sdhci_plat { |
| 103 | struct mmc_config cfg; |
| 104 | struct mmc mmc; |
| 105 | }; |
| 106 | |
| 107 | static int mv_sdhci_probe(struct udevice *dev) |
| 108 | { |
| 109 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 110 | struct mv_sdhci_plat *plat = dev_get_platdata(dev); |
| 111 | struct sdhci_host *host = dev_get_priv(dev); |
| 112 | int ret; |
| 113 | |
| 114 | host->name = MVSDH_NAME; |
| 115 | host->ioaddr = (void *)devfdt_get_addr(dev); |
| 116 | host->quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_WAIT_SEND_CMD; |
Baruch Siach | cddbfd4 | 2019-07-22 18:55:35 +0300 | [diff] [blame] | 117 | host->mmc = &plat->mmc; |
| 118 | host->mmc->dev = dev; |
| 119 | host->mmc->priv = host; |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 120 | |
| 121 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 122 | if (ret) |
| 123 | return ret; |
| 124 | |
| 125 | if (CONFIG_IS_ENABLED(ARCH_MVEBU)) { |
| 126 | /* Configure SDHCI MBUS mbus bridge windows */ |
| 127 | sdhci_mvebu_mbus_config(host->ioaddr); |
| 128 | } |
| 129 | |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 130 | upriv->mmc = host->mmc; |
| 131 | |
| 132 | return sdhci_probe(dev); |
| 133 | } |
| 134 | |
| 135 | static int mv_sdhci_bind(struct udevice *dev) |
| 136 | { |
| 137 | struct mv_sdhci_plat *plat = dev_get_platdata(dev); |
| 138 | |
| 139 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 140 | } |
| 141 | |
| 142 | static const struct udevice_id mv_sdhci_ids[] = { |
| 143 | { .compatible = "marvell,armada-380-sdhci" }, |
| 144 | { } |
| 145 | }; |
| 146 | |
| 147 | U_BOOT_DRIVER(mv_sdhci_drv) = { |
| 148 | .name = MVSDH_NAME, |
| 149 | .id = UCLASS_MMC, |
| 150 | .of_match = mv_sdhci_ids, |
| 151 | .bind = mv_sdhci_bind, |
| 152 | .probe = mv_sdhci_probe, |
| 153 | .ops = &sdhci_ops, |
| 154 | .priv_auto_alloc_size = sizeof(struct sdhci_host), |
| 155 | .platdata_auto_alloc_size = sizeof(struct mv_sdhci_plat), |
| 156 | }; |
| 157 | #endif /* CONFIG_DM_MMC */ |