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> |
Simon Glass | 3ba929a | 2020-10-30 21:38:53 -0600 | [diff] [blame] | 10 | #include <asm/global_data.h> |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 11 | #include <linux/mbus.h> |
| 12 | |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 13 | #define MVSDH_NAME "mv_sdh" |
| 14 | |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 15 | #define SDHCI_WINDOW_CTRL(win) (0x4080 + ((win) << 4)) |
| 16 | #define SDHCI_WINDOW_BASE(win) (0x4084 + ((win) << 4)) |
| 17 | |
| 18 | static void sdhci_mvebu_mbus_config(void __iomem *base) |
| 19 | { |
| 20 | const struct mbus_dram_target_info *dram; |
| 21 | int i; |
| 22 | |
| 23 | dram = mvebu_mbus_dram_info(); |
| 24 | |
| 25 | for (i = 0; i < 4; i++) { |
| 26 | writel(0, base + SDHCI_WINDOW_CTRL(i)); |
| 27 | writel(0, base + SDHCI_WINDOW_BASE(i)); |
| 28 | } |
| 29 | |
| 30 | for (i = 0; i < dram->num_cs; i++) { |
| 31 | const struct mbus_dram_window *cs = dram->cs + i; |
| 32 | |
| 33 | /* Write size, attributes and target id to control register */ |
| 34 | writel(((cs->size - 1) & 0xffff0000) | (cs->mbus_attr << 8) | |
| 35 | (dram->mbus_dram_target_id << 4) | 1, |
| 36 | base + SDHCI_WINDOW_CTRL(i)); |
| 37 | |
| 38 | /* Write base address to base register */ |
| 39 | writel(cs->base, base + SDHCI_WINDOW_BASE(i)); |
| 40 | } |
| 41 | } |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 42 | |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 43 | #ifndef CONFIG_DM_MMC |
| 44 | |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 45 | #ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS |
| 46 | static struct sdhci_ops mv_ops; |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 47 | #endif /* CONFIG_MMC_SDHCI_IO_ACCESSORS */ |
| 48 | |
Rob Herring | 8d3a2a7 | 2015-03-17 15:46:39 -0500 | [diff] [blame] | 49 | 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] | 50 | { |
| 51 | struct sdhci_host *host = NULL; |
Matt Pelland | 3bf0e42 | 2018-04-16 10:08:18 -0400 | [diff] [blame] | 52 | host = calloc(1, sizeof(*host)); |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 53 | if (!host) { |
| 54 | printf("sdh_host malloc fail!\n"); |
Jaehoon Chung | fc6c1c6 | 2016-09-26 08:10:02 +0900 | [diff] [blame] | 55 | return -ENOMEM; |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | host->name = MVSDH_NAME; |
| 59 | host->ioaddr = (void *)regbase; |
| 60 | host->quirks = quirks; |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 61 | host->max_clk = max_clk; |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 62 | #ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS |
| 63 | memset(&mv_ops, 0, sizeof(struct sdhci_ops)); |
Lei Wen | 4606074 | 2011-10-03 20:33:44 +0000 | [diff] [blame] | 64 | host->ops = &mv_ops; |
| 65 | #endif |
Stefan Roese | 207f86e | 2015-10-01 17:34:41 +0200 | [diff] [blame] | 66 | |
| 67 | if (CONFIG_IS_ENABLED(ARCH_MVEBU)) { |
| 68 | /* Configure SDHCI MBUS mbus bridge windows */ |
| 69 | sdhci_mvebu_mbus_config((void __iomem *)regbase); |
| 70 | } |
| 71 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 72 | return add_sdhci(host, 0, min_clk); |
Lei Wen | 11ebc70 | 2011-06-28 21:50:07 +0000 | [diff] [blame] | 73 | } |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 74 | |
| 75 | #else |
| 76 | |
| 77 | DECLARE_GLOBAL_DATA_PTR; |
| 78 | |
| 79 | struct mv_sdhci_plat { |
| 80 | struct mmc_config cfg; |
| 81 | struct mmc mmc; |
| 82 | }; |
| 83 | |
| 84 | static int mv_sdhci_probe(struct udevice *dev) |
| 85 | { |
| 86 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 87 | struct mv_sdhci_plat *plat = dev_get_plat(dev); |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 88 | struct sdhci_host *host = dev_get_priv(dev); |
| 89 | int ret; |
| 90 | |
| 91 | host->name = MVSDH_NAME; |
Masahiro Yamada | 1096ae1 | 2020-07-17 14:36:46 +0900 | [diff] [blame] | 92 | host->ioaddr = dev_read_addr_ptr(dev); |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 93 | host->quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_WAIT_SEND_CMD; |
Baruch Siach | cddbfd4 | 2019-07-22 18:55:35 +0300 | [diff] [blame] | 94 | host->mmc = &plat->mmc; |
| 95 | host->mmc->dev = dev; |
| 96 | host->mmc->priv = host; |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 97 | |
Baruch Siach | 8a9cddd | 2021-02-02 08:43:04 +0200 | [diff] [blame] | 98 | ret = mmc_of_parse(dev, &plat->cfg); |
| 99 | if (ret) |
| 100 | return ret; |
| 101 | |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 102 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
| 103 | if (ret) |
| 104 | return ret; |
| 105 | |
| 106 | if (CONFIG_IS_ENABLED(ARCH_MVEBU)) { |
| 107 | /* Configure SDHCI MBUS mbus bridge windows */ |
| 108 | sdhci_mvebu_mbus_config(host->ioaddr); |
| 109 | } |
| 110 | |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 111 | upriv->mmc = host->mmc; |
| 112 | |
| 113 | return sdhci_probe(dev); |
| 114 | } |
| 115 | |
| 116 | static int mv_sdhci_bind(struct udevice *dev) |
| 117 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 118 | struct mv_sdhci_plat *plat = dev_get_plat(dev); |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 119 | |
| 120 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 121 | } |
| 122 | |
| 123 | static const struct udevice_id mv_sdhci_ids[] = { |
| 124 | { .compatible = "marvell,armada-380-sdhci" }, |
| 125 | { } |
| 126 | }; |
| 127 | |
| 128 | U_BOOT_DRIVER(mv_sdhci_drv) = { |
| 129 | .name = MVSDH_NAME, |
| 130 | .id = UCLASS_MMC, |
| 131 | .of_match = mv_sdhci_ids, |
| 132 | .bind = mv_sdhci_bind, |
| 133 | .probe = mv_sdhci_probe, |
| 134 | .ops = &sdhci_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 135 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 136 | .plat_auto = sizeof(struct mv_sdhci_plat), |
Pierre Bourdon | b9af62d | 2019-04-11 04:56:58 +0200 | [diff] [blame] | 137 | }; |
| 138 | #endif /* CONFIG_DM_MMC */ |