blob: 2a1f412278e127cbc5712d43b85a01180006be2a [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +02002/*
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 Kulikowskia00b0c02016-03-31 23:12:16 +02008 */
9
10#include <common.h>
11#include <clk.h>
12#include <dm.h>
Simon Glass9bc15642020-02-03 07:36:16 -070013#include <malloc.h>
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020014#include <sdhci.h>
15#include <wait_bit.h>
16#include <asm/io.h>
17#include <linux/bitops.h>
18
19/* Non-standard registers needed for SDHCI startup */
20#define SDCC_MCI_POWER 0x0
21#define SDCC_MCI_POWER_SW_RST BIT(7)
22
23/* This is undocumented register */
24#define SDCC_MCI_VERSION 0x50
25#define SDCC_MCI_VERSION_MAJOR_SHIFT 28
26#define SDCC_MCI_VERSION_MAJOR_MASK (0xf << SDCC_MCI_VERSION_MAJOR_SHIFT)
27#define SDCC_MCI_VERSION_MINOR_MASK 0xff
28
29#define SDCC_MCI_STATUS2 0x6C
30#define SDCC_MCI_STATUS2_MCI_ACT 0x1
31#define SDCC_MCI_HC_MODE 0x78
32
33/* Offset to SDHCI registers */
34#define SDCC_SDHCI_OFFSET 0x900
35
36/* Non standard (?) SDHCI register */
37#define SDHCI_VENDOR_SPEC_CAPABILITIES0 0x11c
38
Simon Glass8ef07652016-06-12 23:30:29 -060039struct msm_sdhc_plat {
40 struct mmc_config cfg;
41 struct mmc mmc;
42};
43
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020044struct msm_sdhc {
45 struct sdhci_host host;
46 void *base;
47};
48
49DECLARE_GLOBAL_DATA_PTR;
50
51static int msm_sdc_clk_init(struct udevice *dev)
52{
Simon Glassdd79d6e2017-01-17 16:52:55 -070053 int node = dev_of_offset(dev);
54 uint clk_rate = fdtdec_get_uint(gd->fdt_blob, node, "clock-frequency",
55 400000);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020056 uint clkd[2]; /* clk_id and clk_no */
57 int clk_offset;
Stephen Warrena9622432016-06-17 09:44:00 -060058 struct udevice *clk_dev;
59 struct clk clk;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020060 int ret;
61
Simon Glassdd79d6e2017-01-17 16:52:55 -070062 ret = fdtdec_get_int_array(gd->fdt_blob, node, "clock", clkd, 2);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020063 if (ret)
64 return ret;
65
66 clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]);
67 if (clk_offset < 0)
68 return clk_offset;
69
Stephen Warrena9622432016-06-17 09:44:00 -060070 ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020071 if (ret)
72 return ret;
73
Stephen Warrena9622432016-06-17 09:44:00 -060074 clk.id = clkd[1];
75 ret = clk_request(clk_dev, &clk);
76 if (ret < 0)
77 return ret;
78
79 ret = clk_set_rate(&clk, clk_rate);
80 clk_free(&clk);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020081 if (ret < 0)
82 return ret;
83
84 return 0;
85}
86
87static int msm_sdc_probe(struct udevice *dev)
88{
Simon Glass8ef07652016-06-12 23:30:29 -060089 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glass8ef07652016-06-12 23:30:29 -060090 struct msm_sdhc_plat *plat = dev_get_platdata(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020091 struct msm_sdhc *prv = dev_get_priv(dev);
92 struct sdhci_host *host = &prv->host;
93 u32 core_version, core_minor, core_major;
Simon Glass8ef07652016-06-12 23:30:29 -060094 u32 caps;
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +020095 int ret;
96
97 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
98
Stefan Herbrechtsmeierbc47e0e2017-01-17 15:58:48 +010099 host->max_clk = 0;
100
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200101 /* Init clocks */
102 ret = msm_sdc_clk_init(dev);
103 if (ret)
104 return ret;
105
106 /* Reset the core and Enable SDHC mode */
107 writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST,
108 prv->base + SDCC_MCI_POWER);
109
110
111 /* Wait for reset to be written to register */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100112 if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2,
113 SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200114 printf("msm_sdhci: reset request failed\n");
115 return -EIO;
116 }
117
118 /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
Álvaro Fernández Rojas918de032018-01-23 17:14:55 +0100119 if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER,
120 SDCC_MCI_POWER_SW_RST, false, 2, false)) {
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200121 printf("msm_sdhci: stuck in reset\n");
122 return -ETIMEDOUT;
123 }
124
125 /* Enable host-controller mode */
126 writel(1, prv->base + SDCC_MCI_HC_MODE);
127
128 core_version = readl(prv->base + SDCC_MCI_VERSION);
129
130 core_major = (core_version & SDCC_MCI_VERSION_MAJOR_MASK);
131 core_major >>= SDCC_MCI_VERSION_MAJOR_SHIFT;
132
133 core_minor = core_version & SDCC_MCI_VERSION_MINOR_MASK;
134
135 /*
136 * Support for some capabilities is not advertised by newer
137 * controller versions and must be explicitly enabled.
138 */
139 if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
Simon Glass8ef07652016-06-12 23:30:29 -0600140 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200141 caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
142 writel(caps, host->ioaddr + SDHCI_VENDOR_SPEC_CAPABILITIES0);
143 }
144
Manivannan Sadhasivam6b36ab52020-07-16 14:37:26 +0530145 ret = mmc_of_parse(dev, &plat->cfg);
146 if (ret)
147 return ret;
148
Simon Glass8ef07652016-06-12 23:30:29 -0600149 host->mmc = &plat->mmc;
Peng Fanf92f7b62019-08-06 02:47:53 +0000150 host->mmc->dev = dev;
151 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200152 if (ret)
153 return ret;
Simon Glass8ef07652016-06-12 23:30:29 -0600154 host->mmc->priv = &prv->host;
Simon Glass8ef07652016-06-12 23:30:29 -0600155 upriv->mmc = host->mmc;
Mateusz Kulikowskic012e572016-06-26 22:43:55 +0200156
Simon Glass8ef07652016-06-12 23:30:29 -0600157 return sdhci_probe(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200158}
159
160static int msm_sdc_remove(struct udevice *dev)
161{
162 struct msm_sdhc *priv = dev_get_priv(dev);
163
164 /* Disable host-controller mode */
165 writel(0, priv->base + SDCC_MCI_HC_MODE);
166
167 return 0;
168}
169
170static int msm_ofdata_to_platdata(struct udevice *dev)
171{
172 struct udevice *parent = dev->parent;
173 struct msm_sdhc *priv = dev_get_priv(dev);
174 struct sdhci_host *host = &priv->host;
Simon Glassdd79d6e2017-01-17 16:52:55 -0700175 int node = dev_of_offset(dev);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200176
177 host->name = strdup(dev->name);
Masahiro Yamada1096ae12020-07-17 14:36:46 +0900178 host->ioaddr = dev_read_addr_ptr(dev);
Simon Glassdd79d6e2017-01-17 16:52:55 -0700179 host->bus_width = fdtdec_get_int(gd->fdt_blob, node, "bus-width", 4);
180 host->index = fdtdec_get_uint(gd->fdt_blob, node, "index", 0);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200181 priv->base = (void *)fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
Simon Glassdd79d6e2017-01-17 16:52:55 -0700182 dev_of_offset(parent), node, "reg", 1, NULL, false);
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200183 if (priv->base == (void *)FDT_ADDR_T_NONE ||
184 host->ioaddr == (void *)FDT_ADDR_T_NONE)
185 return -EINVAL;
186
187 return 0;
188}
189
Simon Glass8ef07652016-06-12 23:30:29 -0600190static int msm_sdc_bind(struct udevice *dev)
191{
Simon Glass8ef07652016-06-12 23:30:29 -0600192 struct msm_sdhc_plat *plat = dev_get_platdata(dev);
Simon Glass8ef07652016-06-12 23:30:29 -0600193
Masahiro Yamadacdb67f32016-09-06 22:17:32 +0900194 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass8ef07652016-06-12 23:30:29 -0600195}
196
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200197static const struct udevice_id msm_mmc_ids[] = {
198 { .compatible = "qcom,sdhci-msm-v4" },
199 { }
200};
201
202U_BOOT_DRIVER(msm_sdc_drv) = {
203 .name = "msm_sdc",
204 .id = UCLASS_MMC,
205 .of_match = msm_mmc_ids,
206 .ofdata_to_platdata = msm_ofdata_to_platdata,
Simon Glass8ef07652016-06-12 23:30:29 -0600207 .ops = &sdhci_ops,
Simon Glass8ef07652016-06-12 23:30:29 -0600208 .bind = msm_sdc_bind,
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200209 .probe = msm_sdc_probe,
210 .remove = msm_sdc_remove,
211 .priv_auto_alloc_size = sizeof(struct msm_sdhc),
Simon Glass8ef07652016-06-12 23:30:29 -0600212 .platdata_auto_alloc_size = sizeof(struct msm_sdhc_plat),
Mateusz Kulikowskia00b0c02016-03-31 23:12:16 +0200213};