Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
| 2 | /* |
| 3 | * (C) Copyright 2018 Cisco Systems, Inc. |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 4 | * (C) Copyright 2019 Synamedia |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 5 | * |
| 6 | * Author: Thomas Fitzsimmons <fitzsim@fitzsim.org> |
| 7 | */ |
| 8 | |
| 9 | #include <common.h> |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 10 | #include <dm.h> |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 11 | #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. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 41 | #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 Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 49 | struct sdhci_bcmstb_plat { |
| 50 | struct mmc_config cfg; |
| 51 | struct mmc mmc; |
| 52 | }; |
| 53 | |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 54 | struct sdhci_brcmstb_dev_priv { |
| 55 | int (*init)(struct udevice *dev); |
| 56 | }; |
| 57 | |
| 58 | static 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 Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 87 | static int sdhci_bcmstb_bind(struct udevice *dev) |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 88 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 89 | struct sdhci_bcmstb_plat *plat = dev_get_plat(dev); |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 90 | |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 91 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
| 92 | } |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 93 | |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 94 | /* No specific SDHCI operations are required */ |
| 95 | static const struct sdhci_ops bcmstb_sdhci_ops = { 0 }; |
| 96 | |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 97 | static int sdhci_bcmstb_probe(struct udevice *dev) |
| 98 | { |
| 99 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 100 | struct sdhci_bcmstb_plat *plat = dev_get_plat(dev); |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 101 | struct sdhci_host *host = dev_get_priv(dev); |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 102 | struct sdhci_brcmstb_dev_priv *dev_priv; |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 103 | fdt_addr_t base; |
| 104 | int ret; |
| 105 | |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 106 | dev_priv = (struct sdhci_brcmstb_dev_priv *)dev_get_driver_data(dev); |
| 107 | |
Masahiro Yamada | a89b4de | 2020-07-17 14:36:48 +0900 | [diff] [blame] | 108 | base = dev_read_addr(dev); |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 109 | if (base == FDT_ADDR_T_NONE) |
| 110 | return -EINVAL; |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 111 | |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 112 | host->name = dev->name; |
| 113 | host->ioaddr = (void *)base; |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 114 | |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 115 | ret = mmc_of_parse(dev, &plat->cfg); |
| 116 | if (ret) |
| 117 | return ret; |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 118 | |
Peng Fan | a263aca | 2019-08-06 02:47:50 +0000 | [diff] [blame] | 119 | host->mmc = &plat->mmc; |
| 120 | host->mmc->dev = dev; |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 121 | host->ops = &bcmstb_sdhci_ops; |
| 122 | |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 123 | 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 Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 130 | host->mmc->priv = host; |
| 131 | |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 132 | if (dev_priv && dev_priv->init) { |
| 133 | ret = dev_priv->init(dev); |
| 134 | if (ret) |
| 135 | return ret; |
| 136 | } |
| 137 | |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 138 | return sdhci_probe(dev); |
Thomas Fitzsimmons | 919646d | 2018-06-08 17:59:45 -0400 | [diff] [blame] | 139 | } |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 140 | |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 141 | static const struct sdhci_brcmstb_dev_priv match_priv_2712 = { |
| 142 | .init = sdhci_brcmstb_init_2712, |
| 143 | }; |
| 144 | |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 145 | static const struct udevice_id sdhci_bcmstb_match[] = { |
Ivan T. Ivanov | 9383499 | 2024-01-23 10:07:57 +0200 | [diff] [blame] | 146 | { .compatible = "brcm,bcm2712-sdhci", .data = (ulong)&match_priv_2712 }, |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 147 | { .compatible = "brcm,bcm7425-sdhci" }, |
| 148 | { .compatible = "brcm,sdhci-brcmstb" }, |
| 149 | { } |
| 150 | }; |
| 151 | |
| 152 | U_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 Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 159 | .priv_auto = sizeof(struct sdhci_host), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 160 | .plat_auto = sizeof(struct sdhci_bcmstb_plat), |
Thomas Fitzsimmons | 06edafb | 2019-05-17 08:17:07 -0400 | [diff] [blame] | 161 | }; |