blob: 591137f50e39b0274237629874384bf2d0429449 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Stefan Roese5b372122015-10-01 17:34:41 +02002/*
3 * Marvell SD Host Controller Interface
Stefan Roese5b372122015-10-01 17:34:41 +02004 */
5
Lei Wene75787d2011-06-28 21:50:07 +00006#include <common.h>
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +02007#include <dm.h>
Lei Wene75787d2011-06-28 21:50:07 +00008#include <malloc.h>
9#include <sdhci.h>
Simon Glass401d1c42020-10-30 21:38:53 -060010#include <asm/global_data.h>
Stefan Roese5b372122015-10-01 17:34:41 +020011#include <linux/mbus.h>
12
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +020013#define MVSDH_NAME "mv_sdh"
14
Stefan Roese5b372122015-10-01 17:34:41 +020015#define SDHCI_WINDOW_CTRL(win) (0x4080 + ((win) << 4))
16#define SDHCI_WINDOW_BASE(win) (0x4084 + ((win) << 4))
17
18static 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 Wene75787d2011-06-28 21:50:07 +000042
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +020043#ifndef CONFIG_DM_MMC
44
Lei Wen02d3ad32011-10-03 20:33:44 +000045#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
46static struct sdhci_ops mv_ops;
47
48#if defined(CONFIG_SHEEVA_88SV331xV5)
49#define SD_CE_ATA_2 0xEA
50#define MMC_CARD 0x1000
51#define MMC_WIDTH 0x0100
52static inline void mv_sdhci_writeb(struct sdhci_host *host, u8 val, int reg)
53{
54 struct mmc *mmc = host->mmc;
Rob Herring3a489442015-03-17 15:46:39 -050055 u32 ata = (unsigned long)host->ioaddr + SD_CE_ATA_2;
Lei Wen02d3ad32011-10-03 20:33:44 +000056
57 if (!IS_SD(mmc) && reg == SDHCI_HOST_CONTROL) {
58 if (mmc->bus_width == 8)
59 writew(readw(ata) | (MMC_CARD | MMC_WIDTH), ata);
60 else
61 writew(readw(ata) & ~(MMC_CARD | MMC_WIDTH), ata);
62 }
63
64 writeb(val, host->ioaddr + reg);
65}
66
67#else
68#define mv_sdhci_writeb NULL
69#endif /* CONFIG_SHEEVA_88SV331xV5 */
70#endif /* CONFIG_MMC_SDHCI_IO_ACCESSORS */
71
Rob Herring3a489442015-03-17 15:46:39 -050072int mv_sdh_init(unsigned long regbase, u32 max_clk, u32 min_clk, u32 quirks)
Lei Wene75787d2011-06-28 21:50:07 +000073{
74 struct sdhci_host *host = NULL;
Matt Pellandca4e7d62018-04-16 10:08:18 -040075 host = calloc(1, sizeof(*host));
Lei Wene75787d2011-06-28 21:50:07 +000076 if (!host) {
77 printf("sdh_host malloc fail!\n");
Jaehoon Chung2cb5d672016-09-26 08:10:02 +090078 return -ENOMEM;
Lei Wene75787d2011-06-28 21:50:07 +000079 }
80
81 host->name = MVSDH_NAME;
82 host->ioaddr = (void *)regbase;
83 host->quirks = quirks;
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010084 host->max_clk = max_clk;
Lei Wen02d3ad32011-10-03 20:33:44 +000085#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
86 memset(&mv_ops, 0, sizeof(struct sdhci_ops));
Anatolij Gustschinbfe6f622011-12-07 11:47:48 +000087 mv_ops.write_b = mv_sdhci_writeb;
Lei Wen02d3ad32011-10-03 20:33:44 +000088 host->ops = &mv_ops;
89#endif
Stefan Roese5b372122015-10-01 17:34:41 +020090
91 if (CONFIG_IS_ENABLED(ARCH_MVEBU)) {
92 /* Configure SDHCI MBUS mbus bridge windows */
93 sdhci_mvebu_mbus_config((void __iomem *)regbase);
94 }
95
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +010096 return add_sdhci(host, 0, min_clk);
Lei Wene75787d2011-06-28 21:50:07 +000097}
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +020098
99#else
100
101DECLARE_GLOBAL_DATA_PTR;
102
103struct mv_sdhci_plat {
104 struct mmc_config cfg;
105 struct mmc mmc;
106};
107
108static int mv_sdhci_probe(struct udevice *dev)
109{
110 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -0700111 struct mv_sdhci_plat *plat = dev_get_plat(dev);
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +0200112 struct sdhci_host *host = dev_get_priv(dev);
113 int ret;
114
115 host->name = MVSDH_NAME;
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900116 host->ioaddr = dev_read_addr_ptr(dev);
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +0200117 host->quirks = SDHCI_QUIRK_32BIT_DMA_ADDR | SDHCI_QUIRK_WAIT_SEND_CMD;
Baruch Siach41a9fab2019-07-22 18:55:35 +0300118 host->mmc = &plat->mmc;
119 host->mmc->dev = dev;
120 host->mmc->priv = host;
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +0200121
Baruch Siach24a0f8c2021-02-02 08:43:04 +0200122 ret = mmc_of_parse(dev, &plat->cfg);
123 if (ret)
124 return ret;
125
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +0200126 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
127 if (ret)
128 return ret;
129
130 if (CONFIG_IS_ENABLED(ARCH_MVEBU)) {
131 /* Configure SDHCI MBUS mbus bridge windows */
132 sdhci_mvebu_mbus_config(host->ioaddr);
133 }
134
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +0200135 upriv->mmc = host->mmc;
136
137 return sdhci_probe(dev);
138}
139
140static int mv_sdhci_bind(struct udevice *dev)
141{
Simon Glassc69cda22020-12-03 16:55:20 -0700142 struct mv_sdhci_plat *plat = dev_get_plat(dev);
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +0200143
144 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
145}
146
147static const struct udevice_id mv_sdhci_ids[] = {
148 { .compatible = "marvell,armada-380-sdhci" },
149 { }
150};
151
152U_BOOT_DRIVER(mv_sdhci_drv) = {
153 .name = MVSDH_NAME,
154 .id = UCLASS_MMC,
155 .of_match = mv_sdhci_ids,
156 .bind = mv_sdhci_bind,
157 .probe = mv_sdhci_probe,
158 .ops = &sdhci_ops,
Simon Glass41575d82020-12-03 16:55:17 -0700159 .priv_auto = sizeof(struct sdhci_host),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700160 .plat_auto = sizeof(struct mv_sdhci_plat),
Pierre Bourdon4ec9dd42019-04-11 04:56:58 +0200161};
162#endif /* CONFIG_DM_MMC */