blob: d63d7b3a2c1065b8a50ed8e47b86dee7210f7267 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Mateusz Kulikowski9d11d122016-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 Kulikowski9d11d122016-03-31 23:12:16 +02008 */
9
10#include <common.h>
11#include <clk.h>
12#include <dm.h>
Simon Glass336d4612020-02-03 07:36:16 -070013#include <malloc.h>
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020014#include <sdhci.h>
15#include <wait_bit.h>
Simon Glass401d1c42020-10-30 21:38:53 -060016#include <asm/global_data.h>
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020017#include <asm/io.h>
18#include <linux/bitops.h>
19
20/* Non-standard registers needed for SDHCI startup */
21#define SDCC_MCI_POWER 0x0
22#define SDCC_MCI_POWER_SW_RST BIT(7)
23
24/* This is undocumented register */
25#define SDCC_MCI_VERSION 0x50
26#define SDCC_MCI_VERSION_MAJOR_SHIFT 28
27#define SDCC_MCI_VERSION_MAJOR_MASK (0xf << SDCC_MCI_VERSION_MAJOR_SHIFT)
28#define SDCC_MCI_VERSION_MINOR_MASK 0xff
29
30#define SDCC_MCI_STATUS2 0x6C
31#define SDCC_MCI_STATUS2_MCI_ACT 0x1
32#define SDCC_MCI_HC_MODE 0x78
33
34/* Offset to SDHCI registers */
35#define SDCC_SDHCI_OFFSET 0x900
36
37/* Non standard (?) SDHCI register */
38#define SDHCI_VENDOR_SPEC_CAPABILITIES0 0x11c
39
Simon Glass12293f62016-06-12 23:30:29 -060040struct msm_sdhc_plat {
41 struct mmc_config cfg;
42 struct mmc mmc;
43};
44
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020045struct msm_sdhc {
46 struct sdhci_host host;
47 void *base;
48};
49
50DECLARE_GLOBAL_DATA_PTR;
51
52static int msm_sdc_clk_init(struct udevice *dev)
53{
Simon Glasse160f7d2017-01-17 16:52:55 -070054 int node = dev_of_offset(dev);
55 uint clk_rate = fdtdec_get_uint(gd->fdt_blob, node, "clock-frequency",
56 400000);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020057 uint clkd[2]; /* clk_id and clk_no */
58 int clk_offset;
Stephen Warren135aa952016-06-17 09:44:00 -060059 struct udevice *clk_dev;
60 struct clk clk;
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020061 int ret;
62
Simon Glasse160f7d2017-01-17 16:52:55 -070063 ret = fdtdec_get_int_array(gd->fdt_blob, node, "clock", clkd, 2);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020064 if (ret)
65 return ret;
66
67 clk_offset = fdt_node_offset_by_phandle(gd->fdt_blob, clkd[0]);
68 if (clk_offset < 0)
69 return clk_offset;
70
Stephen Warren135aa952016-06-17 09:44:00 -060071 ret = uclass_get_device_by_of_offset(UCLASS_CLK, clk_offset, &clk_dev);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020072 if (ret)
73 return ret;
74
Stephen Warren135aa952016-06-17 09:44:00 -060075 clk.id = clkd[1];
76 ret = clk_request(clk_dev, &clk);
77 if (ret < 0)
78 return ret;
79
80 ret = clk_set_rate(&clk, clk_rate);
81 clk_free(&clk);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020082 if (ret < 0)
83 return ret;
84
85 return 0;
86}
87
88static int msm_sdc_probe(struct udevice *dev)
89{
Simon Glass12293f62016-06-12 23:30:29 -060090 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Simon Glassc69cda22020-12-03 16:55:20 -070091 struct msm_sdhc_plat *plat = dev_get_plat(dev);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020092 struct msm_sdhc *prv = dev_get_priv(dev);
93 struct sdhci_host *host = &prv->host;
94 u32 core_version, core_minor, core_major;
Simon Glass12293f62016-06-12 23:30:29 -060095 u32 caps;
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +020096 int ret;
97
98 host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_BROKEN_R1B;
99
Stefan Herbrechtsmeier6d0e34b2017-01-17 15:58:48 +0100100 host->max_clk = 0;
101
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200102 /* Init clocks */
103 ret = msm_sdc_clk_init(dev);
104 if (ret)
105 return ret;
106
107 /* Reset the core and Enable SDHC mode */
108 writel(readl(prv->base + SDCC_MCI_POWER) | SDCC_MCI_POWER_SW_RST,
109 prv->base + SDCC_MCI_POWER);
110
111
112 /* Wait for reset to be written to register */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100113 if (wait_for_bit_le32(prv->base + SDCC_MCI_STATUS2,
114 SDCC_MCI_STATUS2_MCI_ACT, false, 10, false)) {
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200115 printf("msm_sdhci: reset request failed\n");
116 return -EIO;
117 }
118
119 /* SW reset can take upto 10HCLK + 15MCLK cycles. (min 40us) */
Álvaro Fernández Rojas48263502018-01-23 17:14:55 +0100120 if (wait_for_bit_le32(prv->base + SDCC_MCI_POWER,
121 SDCC_MCI_POWER_SW_RST, false, 2, false)) {
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200122 printf("msm_sdhci: stuck in reset\n");
123 return -ETIMEDOUT;
124 }
125
126 /* Enable host-controller mode */
127 writel(1, prv->base + SDCC_MCI_HC_MODE);
128
129 core_version = readl(prv->base + SDCC_MCI_VERSION);
130
131 core_major = (core_version & SDCC_MCI_VERSION_MAJOR_MASK);
132 core_major >>= SDCC_MCI_VERSION_MAJOR_SHIFT;
133
134 core_minor = core_version & SDCC_MCI_VERSION_MINOR_MASK;
135
136 /*
137 * Support for some capabilities is not advertised by newer
138 * controller versions and must be explicitly enabled.
139 */
140 if (core_major >= 1 && core_minor != 0x11 && core_minor != 0x12) {
Simon Glass12293f62016-06-12 23:30:29 -0600141 caps = readl(host->ioaddr + SDHCI_CAPABILITIES);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200142 caps |= SDHCI_CAN_VDD_300 | SDHCI_CAN_DO_8BIT;
143 writel(caps, host->ioaddr + SDHCI_VENDOR_SPEC_CAPABILITIES0);
144 }
145
Manivannan Sadhasivam85051472020-07-16 14:37:26 +0530146 ret = mmc_of_parse(dev, &plat->cfg);
147 if (ret)
148 return ret;
149
Simon Glass12293f62016-06-12 23:30:29 -0600150 host->mmc = &plat->mmc;
Peng Fan6904e1b2019-08-06 02:47:53 +0000151 host->mmc->dev = dev;
152 ret = sdhci_setup_cfg(&plat->cfg, host, 0, 0);
Mateusz Kulikowskieb9d3ca2016-06-26 22:43:55 +0200153 if (ret)
154 return ret;
Simon Glass12293f62016-06-12 23:30:29 -0600155 host->mmc->priv = &prv->host;
Simon Glass12293f62016-06-12 23:30:29 -0600156 upriv->mmc = host->mmc;
Mateusz Kulikowskieb9d3ca2016-06-26 22:43:55 +0200157
Simon Glass12293f62016-06-12 23:30:29 -0600158 return sdhci_probe(dev);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200159}
160
161static int msm_sdc_remove(struct udevice *dev)
162{
163 struct msm_sdhc *priv = dev_get_priv(dev);
164
165 /* Disable host-controller mode */
166 writel(0, priv->base + SDCC_MCI_HC_MODE);
167
168 return 0;
169}
170
Simon Glassd1998a92020-12-03 16:55:21 -0700171static int msm_of_to_plat(struct udevice *dev)
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200172{
173 struct udevice *parent = dev->parent;
174 struct msm_sdhc *priv = dev_get_priv(dev);
175 struct sdhci_host *host = &priv->host;
Simon Glasse160f7d2017-01-17 16:52:55 -0700176 int node = dev_of_offset(dev);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200177
178 host->name = strdup(dev->name);
Masahiro Yamada8613c8d2020-07-17 14:36:46 +0900179 host->ioaddr = dev_read_addr_ptr(dev);
Simon Glasse160f7d2017-01-17 16:52:55 -0700180 host->bus_width = fdtdec_get_int(gd->fdt_blob, node, "bus-width", 4);
181 host->index = fdtdec_get_uint(gd->fdt_blob, node, "index", 0);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200182 priv->base = (void *)fdtdec_get_addr_size_auto_parent(gd->fdt_blob,
Simon Glasse160f7d2017-01-17 16:52:55 -0700183 dev_of_offset(parent), node, "reg", 1, NULL, false);
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200184 if (priv->base == (void *)FDT_ADDR_T_NONE ||
185 host->ioaddr == (void *)FDT_ADDR_T_NONE)
186 return -EINVAL;
187
188 return 0;
189}
190
Simon Glass12293f62016-06-12 23:30:29 -0600191static int msm_sdc_bind(struct udevice *dev)
192{
Simon Glassc69cda22020-12-03 16:55:20 -0700193 struct msm_sdhc_plat *plat = dev_get_plat(dev);
Simon Glass12293f62016-06-12 23:30:29 -0600194
Masahiro Yamada24f5aec2016-09-06 22:17:32 +0900195 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
Simon Glass12293f62016-06-12 23:30:29 -0600196}
197
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200198static const struct udevice_id msm_mmc_ids[] = {
199 { .compatible = "qcom,sdhci-msm-v4" },
200 { }
201};
202
203U_BOOT_DRIVER(msm_sdc_drv) = {
204 .name = "msm_sdc",
205 .id = UCLASS_MMC,
206 .of_match = msm_mmc_ids,
Simon Glassd1998a92020-12-03 16:55:21 -0700207 .of_to_plat = msm_of_to_plat,
Simon Glass12293f62016-06-12 23:30:29 -0600208 .ops = &sdhci_ops,
Simon Glass12293f62016-06-12 23:30:29 -0600209 .bind = msm_sdc_bind,
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200210 .probe = msm_sdc_probe,
211 .remove = msm_sdc_remove,
Simon Glass41575d82020-12-03 16:55:17 -0700212 .priv_auto = sizeof(struct msm_sdhc),
Simon Glasscaa4daa2020-12-03 16:55:18 -0700213 .plat_auto = sizeof(struct msm_sdhc_plat),
Mateusz Kulikowski9d11d122016-03-31 23:12:16 +0200214};