Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 2 | /* |
| 3 | * Qualcomm SDHCI driver - SD/eMMC controller |
| 4 | * |
| 5 | * (C) Copyright 2015 Mateusz Kulikowski <mateusz.kulikowski@gmail.com> |
| 6 | * |
| 7 | * Based on Linux driver |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 8 | */ |
| 9 | |
| 10 | #include <common.h> |
| 11 | #include <clk.h> |
| 12 | #include <dm.h> |
| 13 | #include <sdhci.h> |
| 14 | #include <wait_bit.h> |
| 15 | #include <asm/io.h> |
| 16 | #include <linux/bitops.h> |
| 17 | |
| 18 | /* Non-standard registers needed for SDHCI startup */ |
| 19 | #define SDCC_MCI_POWER 0x0 |
| 20 | #define SDCC_MCI_POWER_SW_RST BIT(7) |
| 21 | |
| 22 | /* This is undocumented register */ |
| 23 | #define SDCC_MCI_VERSION 0x50 |
| 24 | #define SDCC_MCI_VERSION_MAJOR_SHIFT 28 |
| 25 | #define SDCC_MCI_VERSION_MAJOR_MASK (0xf << SDCC_MCI_VERSION_MAJOR_SHIFT) |
| 26 | #define SDCC_MCI_VERSION_MINOR_MASK 0xff |
| 27 | |
| 28 | #define SDCC_MCI_STATUS2 0x6C |
| 29 | #define SDCC_MCI_STATUS2_MCI_ACT 0x1 |
| 30 | #define SDCC_MCI_HC_MODE 0x78 |
| 31 | |
| 32 | /* Offset to SDHCI registers */ |
| 33 | #define SDCC_SDHCI_OFFSET 0x900 |
| 34 | |
| 35 | /* Non standard (?) SDHCI register */ |
| 36 | #define SDHCI_VENDOR_SPEC_CAPABILITIES0 0x11c |
| 37 | |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 38 | struct msm_sdhc_plat { |
| 39 | struct mmc_config cfg; |
| 40 | struct mmc mmc; |
| 41 | }; |
| 42 | |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 43 | struct msm_sdhc { |
| 44 | struct sdhci_host host; |
| 45 | void *base; |
| 46 | }; |
| 47 | |
| 48 | DECLARE_GLOBAL_DATA_PTR; |
| 49 | |
| 50 | static int msm_sdc_clk_init(struct udevice *dev) |
| 51 | { |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 52 | int node = dev_of_offset(dev); |
| 53 | uint clk_rate = fdtdec_get_uint(gd->fdt_blob, node, "clock-frequency", |
| 54 | 400000); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 55 | uint clkd[2]; /* clk_id and clk_no */ |
| 56 | int clk_offset; |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 57 | struct udevice *clk_dev; |
| 58 | struct clk clk; |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 59 | int ret; |
| 60 | |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 61 | ret = fdtdec_get_int_array(gd->fdt_blob, node, "clock", clkd, 2); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 62 | if (ret) |
| 63 | return ret; |
| 64 | |
| 65 | clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]); |
| 66 | if (clk_offset < 0) |
| 67 | return clk_offset; |
| 68 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 69 | ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 70 | if (ret) |
| 71 | return ret; |
| 72 | |
Stephen Warren | a962243 | 2016-06-17 09:44:00 -0600 | [diff] [blame] | 73 | clk.id = clkd[1]; |
| 74 | ret = clk_request(clk_dev, &clk); |
| 75 | if (ret < 0) |
| 76 | return ret; |
| 77 | |
| 78 | ret = clk_set_rate(&clk, clk_rate); |
| 79 | clk_free(&clk); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 80 | if (ret < 0) |
| 81 | return ret; |
| 82 | |
| 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | static int msm_sdc_probe(struct udevice *dev) |
| 87 | { |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 88 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 89 | struct msm_sdhc_plat *plat = dev_get_platdata(dev); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 90 | struct msm_sdhc *prv = dev_get_priv(dev); |
| 91 | struct sdhci_host *host = &prv->host; |
| 92 | u32 core_version, core_minor, core_major; |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 93 | u32 caps; |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 94 | int ret; |
| 95 | |
| 96 | host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B; |
| 97 | |
Stefan Herbrechtsmeier | bc47e0e | 2017-01-17 15:58:48 +0100 | [diff] [blame] | 98 | host->max_clk = 0; |
| 99 | |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 100 | /* Init clocks */ |
| 101 | ret = msm_sdc_clk_init(dev); |
| 102 | if (ret) |
| 103 | return ret; |
| 104 | |
| 105 | /* Reset the core and Enable SDHC mode */ |
| 106 | writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST, |
| 107 | prv->base + SDCC_MCI_POWER); |
| 108 | |
| 109 | |
| 110 | /* Wait for reset to be written to register */ |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 111 | if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2, |
| 112 | SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) { |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 113 | printf("msm_sdhci: reset request failed\n"); |
| 114 | return -EIO; |
| 115 | } |
| 116 | |
| 117 | /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */ |
Álvaro Fernández Rojas | 918de03 | 2018-01-23 17:14:55 +0100 | [diff] [blame] | 118 | if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER, |
| 119 | SDCC_MCI_POWER_SW_RST, false, 2, false)) { |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 120 | printf("msm_sdhci: stuck in reset\n"); |
| 121 | return -ETIMEDOUT; |
| 122 | } |
| 123 | |
| 124 | /* Enable host-controller mode */ |
| 125 | writel(1, prv->base + SDCC_MCI_HC_MODE); |
| 126 | |
| 127 | core_version = readl(prv->base + SDCC_MCI_VERSION); |
| 128 | |
| 129 | core_major = (core_version & SDCC_MCI_VERSION_MAJOR_MASK); |
| 130 | core_major >>= SDCC_MCI_VERSION_MAJOR_SHIFT; |
| 131 | |
| 132 | core_minor = core_version & SDCC_MCI_VERSION_MINOR_MASK; |
| 133 | |
| 134 | /* |
| 135 | * Support for some capabilities is not advertised by newer |
| 136 | * controller versions and must be explicitly enabled. |
| 137 | */ |
| 138 | if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) { |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 139 | caps = readl(host->ioaddr + SDHCI_CAPABILITIES); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 140 | caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT; |
| 141 | writel(caps, host->ioaddr + SDHCI_VENDOR_SPEC_CAPABILITIES0); |
| 142 | } |
| 143 | |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 144 | host->mmc = &plat->mmc; |
Peng Fan | f92f7b6 | 2019-08-06 02:47:53 +0000 | [diff] [blame] | 145 | host->mmc->dev = dev; |
| 146 | ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0); |
Mateusz Kulikowski | c012e57 | 2016-06-26 22:43:55 +0200 | [diff] [blame] | 147 | if (ret) |
| 148 | return ret; |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 149 | host->mmc->priv = &prv->host; |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 150 | upriv->mmc = host->mmc; |
Mateusz Kulikowski | c012e57 | 2016-06-26 22:43:55 +0200 | [diff] [blame] | 151 | |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 152 | return sdhci_probe(dev); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 153 | } |
| 154 | |
| 155 | static int msm_sdc_remove(struct udevice *dev) |
| 156 | { |
| 157 | struct msm_sdhc *priv = dev_get_priv(dev); |
| 158 | |
| 159 | /* Disable host-controller mode */ |
| 160 | writel(0, priv->base + SDCC_MCI_HC_MODE); |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int msm_ofdata_to_platdata(struct udevice *dev) |
| 166 | { |
| 167 | struct udevice *parent = dev->parent; |
| 168 | struct msm_sdhc *priv = dev_get_priv(dev); |
| 169 | struct sdhci_host *host = &priv->host; |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 170 | int node = dev_of_offset(dev); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 171 | |
| 172 | host->name = strdup(dev->name); |
Simon Glass | ba1dea4 | 2017-05-17 17:18:05 -0600 | [diff] [blame] | 173 | host->ioaddr = (void *)devfdt_get_addr(dev); |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 174 | host->bus_width = fdtdec_get_int(gd->fdt_blob, node, "bus-width", 4); |
| 175 | host->index = fdtdec_get_uint(gd->fdt_blob, node, "index", 0); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 176 | priv->base = (void *)fdtdec_get_addr_size_auto_parent(gd->fdt_blob, |
Simon Glass | dd79d6e | 2017-01-17 16:52:55 -0700 | [diff] [blame] | 177 | dev_of_offset(parent), node, "reg", 1, NULL, false); |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 178 | if (priv->base == (void *)FDT_ADDR_T_NONE || |
| 179 | host->ioaddr == (void *)FDT_ADDR_T_NONE) |
| 180 | return -EINVAL; |
| 181 | |
| 182 | return 0; |
| 183 | } |
| 184 | |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 185 | static int msm_sdc_bind(struct udevice *dev) |
| 186 | { |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 187 | struct msm_sdhc_plat *plat = dev_get_platdata(dev); |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 188 | |
Masahiro Yamada | cdb67f3 | 2016-09-06 22:17:32 +0900 | [diff] [blame] | 189 | return sdhci_bind(dev, &plat->mmc, &plat->cfg); |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 190 | } |
| 191 | |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 192 | static const struct udevice_id msm_mmc_ids[] = { |
| 193 | { .compatible = "qcom,sdhci-msm-v4" }, |
| 194 | { } |
| 195 | }; |
| 196 | |
| 197 | U_BOOT_DRIVER(msm_sdc_drv) = { |
| 198 | .name = "msm_sdc", |
| 199 | .id = UCLASS_MMC, |
| 200 | .of_match = msm_mmc_ids, |
| 201 | .ofdata_to_platdata = msm_ofdata_to_platdata, |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 202 | .ops = &sdhci_ops, |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 203 | .bind = msm_sdc_bind, |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 204 | .probe = msm_sdc_probe, |
| 205 | .remove = msm_sdc_remove, |
| 206 | .priv_auto_alloc_size = sizeof(struct msm_sdhc), |
Simon Glass | 8ef0765 | 2016-06-12 23:30:29 -0600 | [diff] [blame] | 207 | .platdata_auto_alloc_size = sizeof(struct msm_sdhc_plat), |
Mateusz Kulikowski | a00b0c0 | 2016-03-31 23:12:16 +0200 | [diff] [blame] | 208 | }; |