blob: 49846adcf5476e19408986d24b2d0ebf53fc3738 [file] [log] [blame]
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -04001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * (C) Copyright 2018 Cisco Systems, Inc.
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -04004 * (C) Copyright 2019 Synamedia
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -04005 *
6 * Author: Thomas Fitzsimmons <fitzsim@fitzsim.org>
7 */
8
9#include <common.h>
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -040010#include <dm.h>
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -040011#include <mach/sdhci.h>
12#include <malloc.h>
13#include <sdhci.h>
14
15/*
16 * The BCMSTB SDHCI has a quirk in that its actual maximum frequency
17 * capability is 100 MHz. The divisor that is eventually written to
18 * SDHCI_CLOCK_CONTROL is calculated based on what the MMC device
19 * reports, and relative to this maximum frequency.
20 *
21 * This define used to be set to 52000000 (52 MHz), the desired
22 * maximum frequency, but that would result in the communication
23 * actually running at 100 MHz (seemingly without issue), which is
24 * out-of-spec.
25 *
26 * Now, by setting this to 0 (auto-detect), 100 MHz will be read from
27 * the capabilities register, and the resulting divisor will be
28 * doubled, meaning that the clock control register will be set to the
29 * in-spec 52 MHz value.
30 */
31#define BCMSTB_SDHCI_MAXIMUM_CLOCK_FREQUENCY 0
32/*
33 * When the minimum clock frequency is set to 0 (auto-detect), U-Boot
34 * sets it to 100 MHz divided by SDHCI_MAX_DIV_SPEC_300, or 48,875 Hz,
35 * which results in the controller timing out when trying to
36 * communicate with the MMC device. Hard-code this value to 400000
37 * (400 kHz) to prevent this.
38 */
39#define BCMSTB_SDHCI_MINIMUM_CLOCK_FREQUENCY 400000
40
Ivan T. Ivanov93834992024-01-23 10:07:57 +020041#define SDIO_CFG_CTRL 0x0
42#define SDIO_CFG_CTRL_SDCD_N_TEST_EN BIT(31)
43#define SDIO_CFG_CTRL_SDCD_N_TEST_LEV BIT(30)
44
45#define SDIO_CFG_SD_PIN_SEL 0x44
46#define SDIO_CFG_SD_PIN_SEL_MASK 0x3
47#define SDIO_CFG_SD_PIN_SEL_CARD BIT(1)
48
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -040049struct sdhci_bcmstb_plat {
50 struct mmc_config cfg;
51 struct mmc mmc;
52};
53
Ivan T. Ivanov93834992024-01-23 10:07:57 +020054struct sdhci_brcmstb_dev_priv {
55 int (*init)(struct udevice *dev);
56};
57
58static int sdhci_brcmstb_init_2712(struct udevice *dev)
59{
60 struct sdhci_host *host = dev_get_priv(dev);
61 void *cfg_regs;
62 u32 reg;
63
64 /* Map in the non-standard CFG registers */
65 cfg_regs = dev_remap_addr_name(dev, "cfg");
66 if (!cfg_regs)
67 return -ENOENT;
68
69 if ((host->mmc->host_caps & MMC_CAP_NONREMOVABLE) ||
70 (host->mmc->host_caps & MMC_CAP_NEEDS_POLL)) {
71 /* Force presence */
72 reg = readl(cfg_regs + SDIO_CFG_CTRL);
73 reg &= ~SDIO_CFG_CTRL_SDCD_N_TEST_LEV;
74 reg |= SDIO_CFG_CTRL_SDCD_N_TEST_EN;
75 writel(reg, cfg_regs + SDIO_CFG_CTRL);
76 } else {
77 /* Enable card detection line */
78 reg = readl(cfg_regs + SDIO_CFG_SD_PIN_SEL);
79 reg &= ~SDIO_CFG_SD_PIN_SEL_MASK;
80 reg |= SDIO_CFG_SD_PIN_SEL_CARD;
81 writel(reg, cfg_regs + SDIO_CFG_SD_PIN_SEL);
82 }
83
84 return 0;
85}
86
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -040087static int sdhci_bcmstb_bind(struct udevice *dev)
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -040088{
Simon Glassfa20e932020-12-03 16:55:20 -070089 struct sdhci_bcmstb_plat *plat = dev_get_plat(dev);
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -040090
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -040091 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
92}
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -040093
Ivan T. Ivanov93834992024-01-23 10:07:57 +020094/* No specific SDHCI operations are required */
95static const struct sdhci_ops bcmstb_sdhci_ops = { 0 };
96
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -040097static int sdhci_bcmstb_probe(struct udevice *dev)
98{
99 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassfa20e932020-12-03 16:55:20 -0700100 struct sdhci_bcmstb_plat *plat = dev_get_plat(dev);
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400101 struct sdhci_host *host = dev_get_priv(dev);
Ivan T. Ivanov93834992024-01-23 10:07:57 +0200102 struct sdhci_brcmstb_dev_priv *dev_priv;
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400103 fdt_addr_t base;
104 int ret;
105
Ivan T. Ivanov93834992024-01-23 10:07:57 +0200106 dev_priv = (struct sdhci_brcmstb_dev_priv *)dev_get_driver_data(dev);
107
Masahiro Yamadaa89b4de2020-07-17 14:36:48 +0900108 base = dev_read_addr(dev);
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400109 if (base == FDT_ADDR_T_NONE)
110 return -EINVAL;
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -0400111
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400112 host->name = dev->name;
113 host->ioaddr = (void *)base;
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -0400114
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400115 ret = mmc_of_parse(dev, &plat->cfg);
116 if (ret)
117 return ret;
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -0400118
Peng Fana263aca2019-08-06 02:47:50 +0000119 host->mmc = &plat->mmc;
120 host->mmc->dev = dev;
Ivan T. Ivanov93834992024-01-23 10:07:57 +0200121 host->ops = &bcmstb_sdhci_ops;
122
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400123 ret = sdhci_setup_cfg(&plat->cfg, host,
124 BCMSTB_SDHCI_MAXIMUM_CLOCK_FREQUENCY,
125 BCMSTB_SDHCI_MINIMUM_CLOCK_FREQUENCY);
126 if (ret)
127 return ret;
128
129 upriv->mmc = &plat->mmc;
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400130 host->mmc->priv = host;
131
Ivan T. Ivanov93834992024-01-23 10:07:57 +0200132 if (dev_priv && dev_priv->init) {
133 ret = dev_priv->init(dev);
134 if (ret)
135 return ret;
136 }
137
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400138 return sdhci_probe(dev);
Thomas Fitzsimmons919646d2018-06-08 17:59:45 -0400139}
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400140
Ivan T. Ivanov93834992024-01-23 10:07:57 +0200141static const struct sdhci_brcmstb_dev_priv match_priv_2712 = {
142 .init = sdhci_brcmstb_init_2712,
143};
144
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400145static const struct udevice_id sdhci_bcmstb_match[] = {
Ivan T. Ivanov93834992024-01-23 10:07:57 +0200146 { .compatible = "brcm,bcm2712-sdhci", .data = (ulong)&match_priv_2712 },
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400147 { .compatible = "brcm,bcm7425-sdhci" },
148 { .compatible = "brcm,sdhci-brcmstb" },
149 { }
150};
151
152U_BOOT_DRIVER(sdhci_bcmstb) = {
153 .name = "sdhci-bcmstb",
154 .id = UCLASS_MMC,
155 .of_match = sdhci_bcmstb_match,
156 .ops = &sdhci_ops,
157 .bind = sdhci_bcmstb_bind,
158 .probe = sdhci_bcmstb_probe,
Simon Glass8a2b47f2020-12-03 16:55:17 -0700159 .priv_auto = sizeof(struct sdhci_host),
Simon Glass71fa5b42020-12-03 16:55:18 -0700160 .plat_auto = sizeof(struct sdhci_bcmstb_plat),
Thomas Fitzsimmons06edafb2019-05-17 08:17:07 -0400161};