blob: c2319b4134535f9f5f066a9d8054a4c92ee3d525 [file] [log] [blame]
Arun Parameswaran36645f42019-09-12 11:06:08 -07001// SPDX-License-Identifier: GPL-2.0+
2/*
3 * Copyright 2019 Broadcom.
4 *
5 */
6
7#include <common.h>
8#include <dm.h>
9#include <errno.h>
10#include <malloc.h>
11#include <sdhci.h>
12
13DECLARE_GLOBAL_DATA_PTR;
14
15struct sdhci_iproc_host {
16 struct sdhci_host host;
17 u32 shadow_cmd;
18 u32 shadow_blk;
19};
20
21#define REG_OFFSET_IN_BITS(reg) ((reg) << 3 & 0x18)
22
23static inline struct sdhci_iproc_host *to_iproc(struct sdhci_host *host)
24{
25 return (struct sdhci_iproc_host *)host;
26}
27
28#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
29static u32 sdhci_iproc_readl(struct sdhci_host *host, int reg)
30{
31 u32 val = readl(host->ioaddr + reg);
32#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS_TRACE
33 printf("%s %d: readl [0x%02x] 0x%08x\n",
34 host->name, host->index, reg, val);
35#endif
36 return val;
37}
38
39static u16 sdhci_iproc_readw(struct sdhci_host *host, int reg)
40{
41 u32 val = sdhci_iproc_readl(host, (reg & ~3));
42 u16 word = val >> REG_OFFSET_IN_BITS(reg) & 0xffff;
43 return word;
44}
45
46static u8 sdhci_iproc_readb(struct sdhci_host *host, int reg)
47{
48 u32 val = sdhci_iproc_readl(host, (reg & ~3));
49 u8 byte = val >> REG_OFFSET_IN_BITS(reg) & 0xff;
50 return byte;
51}
52
53static void sdhci_iproc_writel(struct sdhci_host *host, u32 val, int reg)
54{
55 u32 clock = 0;
56#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS_TRACE
57 printf("%s %d: writel [0x%02x] 0x%08x\n",
58 host->name, host->index, reg, val);
59#endif
60 writel(val, host->ioaddr + reg);
61
62 if (host->mmc)
63 clock = host->mmc->clock;
64 if (clock <= 400000) {
65 /* Round up to micro-second four SD clock delay */
66 if (clock)
67 udelay((4 * 1000000 + clock - 1) / clock);
68 else
69 udelay(10);
70 }
71}
72
73/*
74 * The Arasan has a bugette whereby it may lose the content of successive
75 * writes to the same register that are within two SD-card clock cycles of
76 * each other (a clock domain crossing problem). The data
77 * register does not have this problem, which is just as well - otherwise we'd
78 * have to nobble the DMA engine too.
79 *
80 * This wouldn't be a problem with the code except that we can only write the
81 * controller with 32-bit writes. So two different 16-bit registers are
82 * written back to back creates the problem.
83 *
84 * In reality, this only happens when SDHCI_BLOCK_SIZE and SDHCI_BLOCK_COUNT
85 * are written followed by SDHCI_TRANSFER_MODE and SDHCI_COMMAND.
86 * The BLOCK_SIZE and BLOCK_COUNT are meaningless until a command issued so
87 * the work around can be further optimized. We can keep shadow values of
88 * BLOCK_SIZE, BLOCK_COUNT, and TRANSFER_MODE until a COMMAND is issued.
89 * Then, write the BLOCK_SIZE+BLOCK_COUNT in a single 32-bit write followed
90 * by the TRANSFER+COMMAND in another 32-bit write.
91 */
92static void sdhci_iproc_writew(struct sdhci_host *host, u16 val, int reg)
93{
94 struct sdhci_iproc_host *iproc_host = to_iproc(host);
95 u32 word_shift = REG_OFFSET_IN_BITS(reg);
96 u32 mask = 0xffff << word_shift;
97 u32 oldval, newval;
98
99 if (reg == SDHCI_COMMAND) {
100 /* Write the block now as we are issuing a command */
101 if (iproc_host->shadow_blk != 0) {
102 sdhci_iproc_writel(host, iproc_host->shadow_blk,
103 SDHCI_BLOCK_SIZE);
104 iproc_host->shadow_blk = 0;
105 }
106 oldval = iproc_host->shadow_cmd;
107 } else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) {
108 /* Block size and count are stored in shadow reg */
109 oldval = iproc_host->shadow_blk;
110 } else {
111 /* Read reg, all other registers are not shadowed */
112 oldval = sdhci_iproc_readl(host, (reg & ~3));
113 }
114 newval = (oldval & ~mask) | (val << word_shift);
115
116 if (reg == SDHCI_TRANSFER_MODE) {
117 /* Save the transfer mode until the command is issued */
118 iproc_host->shadow_cmd = newval;
119 } else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) {
120 /* Save the block info until the command is issued */
121 iproc_host->shadow_blk = newval;
122 } else {
123 /* Command or other regular 32-bit write */
124 sdhci_iproc_writel(host, newval, reg & ~3);
125 }
126}
127
128static void sdhci_iproc_writeb(struct sdhci_host *host, u8 val, int reg)
129{
130 u32 oldval = sdhci_iproc_readl(host, (reg & ~3));
131 u32 byte_shift = REG_OFFSET_IN_BITS(reg);
132 u32 mask = 0xff << byte_shift;
133 u32 newval = (oldval & ~mask) | (val << byte_shift);
134
135 sdhci_iproc_writel(host, newval, reg & ~3);
136}
137#endif
138
Rayagonda Kokatanur7a65b8b2020-03-31 11:04:05 +0530139static int sdhci_iproc_set_ios_post(struct sdhci_host *host)
Arun Parameswaran36645f42019-09-12 11:06:08 -0700140{
141 u32 ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
142
143 /* Reset UHS mode bits */
144 ctrl &= ~SDHCI_CTRL_UHS_MASK;
145
146 if (host->mmc->ddr_mode)
147 ctrl |= UHS_DDR50_BUS_SPEED;
148
149 sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
Rayagonda Kokatanur7a65b8b2020-03-31 11:04:05 +0530150
151 return 0;
Arun Parameswaran36645f42019-09-12 11:06:08 -0700152}
153
154static struct sdhci_ops sdhci_platform_ops = {
155#ifdef CONFIG_MMC_SDHCI_IO_ACCESSORS
156 .read_l = sdhci_iproc_readl,
157 .read_w = sdhci_iproc_readw,
158 .read_b = sdhci_iproc_readb,
159 .write_l = sdhci_iproc_writel,
160 .write_w = sdhci_iproc_writew,
161 .write_b = sdhci_iproc_writeb,
162#endif
163 .set_ios_post = sdhci_iproc_set_ios_post,
164};
165
166struct iproc_sdhci_plat {
167 struct mmc_config cfg;
168 struct mmc mmc;
169};
170
171static int iproc_sdhci_probe(struct udevice *dev)
172{
173 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
174 struct iproc_sdhci_plat *plat = dev_get_platdata(dev);
175 struct sdhci_host *host = dev_get_priv(dev);
176 struct sdhci_iproc_host *iproc_host;
177 int node = dev_of_offset(dev);
178 u32 f_min_max[2];
179 int ret;
180
Bharat Kumar Reddy Gootyd5b85002020-03-31 11:04:03 +0530181 iproc_host = malloc(sizeof(struct sdhci_iproc_host));
Arun Parameswaran36645f42019-09-12 11:06:08 -0700182 if (!iproc_host) {
183 printf("%s: sdhci host malloc fail!\n", __func__);
184 return -ENOMEM;
185 }
186 iproc_host->shadow_cmd = 0;
187 iproc_host->shadow_blk = 0;
188
189 host->name = dev->name;
190 host->ioaddr = (void *)devfdt_get_addr(dev);
191 host->voltages = MMC_VDD_165_195 |
192 MMC_VDD_32_33 | MMC_VDD_33_34;
Bharat Kumar Reddy Gooty2bb02b12020-03-31 11:04:04 +0530193 host->quirks = SDHCI_QUIRK_BROKEN_VOLTAGE | SDHCI_QUIRK_BROKEN_R1B;
Arun Parameswaran36645f42019-09-12 11:06:08 -0700194 host->host_caps = MMC_MODE_DDR_52MHz;
195 host->index = fdtdec_get_uint(gd->fdt_blob, node, "index", 0);
196 host->ops = &sdhci_platform_ops;
197 host->version = sdhci_readw(host, SDHCI_HOST_VERSION);
198 ret = fdtdec_get_int_array(gd->fdt_blob, dev_of_offset(dev),
199 "clock-freq-min-max", f_min_max, 2);
200 if (ret) {
201 printf("sdhci: clock-freq-min-max not found\n");
Bharat Kumar Reddy Gootyd5b85002020-03-31 11:04:03 +0530202 free(iproc_host);
Arun Parameswaran36645f42019-09-12 11:06:08 -0700203 return ret;
204 }
205 host->max_clk = f_min_max[1];
206 host->bus_width = fdtdec_get_int(gd->fdt_blob,
207 dev_of_offset(dev), "bus-width", 4);
208
209 /* Update host_caps for 8 bit bus width */
210 if (host->bus_width == 8)
211 host->host_caps |= MMC_MODE_8BIT;
212
213 memcpy(&iproc_host->host, host, sizeof(struct sdhci_host));
214
Rayagonda Kokatanur29617ca2020-03-31 11:04:06 +0530215 iproc_host->host.mmc = &plat->mmc;
216 iproc_host->host.mmc->dev = dev;
217 iproc_host->host.mmc->priv = &iproc_host->host;
218 upriv->mmc = iproc_host->host.mmc;
219
Arun Parameswaran36645f42019-09-12 11:06:08 -0700220 ret = sdhci_setup_cfg(&plat->cfg, &iproc_host->host,
221 f_min_max[1], f_min_max[0]);
Bharat Kumar Reddy Gootyd5b85002020-03-31 11:04:03 +0530222 if (ret) {
223 free(iproc_host);
Arun Parameswaran36645f42019-09-12 11:06:08 -0700224 return ret;
Bharat Kumar Reddy Gootyd5b85002020-03-31 11:04:03 +0530225 }
Arun Parameswaran36645f42019-09-12 11:06:08 -0700226
Arun Parameswaran36645f42019-09-12 11:06:08 -0700227 return sdhci_probe(dev);
228}
229
230static int iproc_sdhci_bind(struct udevice *dev)
231{
232 struct iproc_sdhci_plat *plat = dev_get_platdata(dev);
233
234 return sdhci_bind(dev, &plat->mmc, &plat->cfg);
235}
236
237static const struct udevice_id iproc_sdhci_ids[] = {
238 { .compatible = "brcm,iproc-sdhci" },
239 { }
240};
241
242U_BOOT_DRIVER(iproc_sdhci_drv) = {
243 .name = "iproc_sdhci",
244 .id = UCLASS_MMC,
245 .of_match = iproc_sdhci_ids,
246 .ops = &sdhci_ops,
247 .bind = iproc_sdhci_bind,
248 .probe = iproc_sdhci_probe,
249 .priv_auto_alloc_size = sizeof(struct sdhci_host),
250 .platdata_auto_alloc_size = sizeof(struct iproc_sdhci_plat),
251};