2 * Copyright (C) 2014 Broadcom Corporation
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License as
6 * published by the Free Software Foundation version 2.
8 * This program is distributed "as is" WITHOUT ANY WARRANTY of any
9 * kind, whether express or implied; without even the implied warranty
10 * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
15 * iProc SDHCI platform driver
18 #include <linux/delay.h>
19 #include <linux/module.h>
20 #include <linux/mmc/host.h>
22 #include <linux/of_device.h>
23 #include "sdhci-pltfm.h"
25 struct sdhci_iproc_data {
26 const struct sdhci_pltfm_data *pdata;
31 struct sdhci_iproc_host {
32 const struct sdhci_iproc_data *data;
37 #define REG_OFFSET_IN_BITS(reg) ((reg) << 3 & 0x18)
39 static inline u32 sdhci_iproc_readl(struct sdhci_host *host, int reg)
41 u32 val = readl(host->ioaddr + reg);
43 pr_debug("%s: readl [0x%02x] 0x%08x\n",
44 mmc_hostname(host->mmc), reg, val);
48 static u16 sdhci_iproc_readw(struct sdhci_host *host, int reg)
50 u32 val = sdhci_iproc_readl(host, (reg & ~3));
51 u16 word = val >> REG_OFFSET_IN_BITS(reg) & 0xffff;
55 static u8 sdhci_iproc_readb(struct sdhci_host *host, int reg)
57 u32 val = sdhci_iproc_readl(host, (reg & ~3));
58 u8 byte = val >> REG_OFFSET_IN_BITS(reg) & 0xff;
62 static inline void sdhci_iproc_writel(struct sdhci_host *host, u32 val, int reg)
64 pr_debug("%s: writel [0x%02x] 0x%08x\n",
65 mmc_hostname(host->mmc), reg, val);
67 writel(val, host->ioaddr + reg);
69 if (host->clock <= 400000) {
70 /* Round up to micro-second four SD clock delay */
72 udelay((4 * 1000000 + host->clock - 1) / host->clock);
79 * The Arasan has a bugette whereby it may lose the content of successive
80 * writes to the same register that are within two SD-card clock cycles of
81 * each other (a clock domain crossing problem). The data
82 * register does not have this problem, which is just as well - otherwise we'd
83 * have to nobble the DMA engine too.
85 * This wouldn't be a problem with the code except that we can only write the
86 * controller with 32-bit writes. So two different 16-bit registers are
87 * written back to back creates the problem.
89 * In reality, this only happens when SDHCI_BLOCK_SIZE and SDHCI_BLOCK_COUNT
90 * are written followed by SDHCI_TRANSFER_MODE and SDHCI_COMMAND.
91 * The BLOCK_SIZE and BLOCK_COUNT are meaningless until a command issued so
92 * the work around can be further optimized. We can keep shadow values of
93 * BLOCK_SIZE, BLOCK_COUNT, and TRANSFER_MODE until a COMMAND is issued.
94 * Then, write the BLOCK_SIZE+BLOCK_COUNT in a single 32-bit write followed
95 * by the TRANSFER+COMMAND in another 32-bit write.
97 static void sdhci_iproc_writew(struct sdhci_host *host, u16 val, int reg)
99 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
100 struct sdhci_iproc_host *iproc_host = sdhci_pltfm_priv(pltfm_host);
101 u32 word_shift = REG_OFFSET_IN_BITS(reg);
102 u32 mask = 0xffff << word_shift;
105 if (reg == SDHCI_COMMAND) {
106 /* Write the block now as we are issuing a command */
107 if (iproc_host->shadow_blk != 0) {
108 sdhci_iproc_writel(host, iproc_host->shadow_blk,
110 iproc_host->shadow_blk = 0;
112 oldval = iproc_host->shadow_cmd;
113 } else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) {
114 /* Block size and count are stored in shadow reg */
115 oldval = iproc_host->shadow_blk;
117 /* Read reg, all other registers are not shadowed */
118 oldval = sdhci_iproc_readl(host, (reg & ~3));
120 newval = (oldval & ~mask) | (val << word_shift);
122 if (reg == SDHCI_TRANSFER_MODE) {
123 /* Save the transfer mode until the command is issued */
124 iproc_host->shadow_cmd = newval;
125 } else if (reg == SDHCI_BLOCK_SIZE || reg == SDHCI_BLOCK_COUNT) {
126 /* Save the block info until the command is issued */
127 iproc_host->shadow_blk = newval;
129 /* Command or other regular 32-bit write */
130 sdhci_iproc_writel(host, newval, reg & ~3);
134 static void sdhci_iproc_writeb(struct sdhci_host *host, u8 val, int reg)
136 u32 oldval = sdhci_iproc_readl(host, (reg & ~3));
137 u32 byte_shift = REG_OFFSET_IN_BITS(reg);
138 u32 mask = 0xff << byte_shift;
139 u32 newval = (oldval & ~mask) | (val << byte_shift);
141 sdhci_iproc_writel(host, newval, reg & ~3);
144 static const struct sdhci_ops sdhci_iproc_ops = {
145 .read_l = sdhci_iproc_readl,
146 .read_w = sdhci_iproc_readw,
147 .read_b = sdhci_iproc_readb,
148 .write_l = sdhci_iproc_writel,
149 .write_w = sdhci_iproc_writew,
150 .write_b = sdhci_iproc_writeb,
151 .set_clock = sdhci_set_clock,
152 .get_max_clock = sdhci_pltfm_clk_get_max_clock,
153 .set_bus_width = sdhci_set_bus_width,
154 .reset = sdhci_reset,
155 .set_uhs_signaling = sdhci_set_uhs_signaling,
158 static const struct sdhci_pltfm_data sdhci_iproc_pltfm_data = {
159 .quirks = SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK,
160 .quirks2 = SDHCI_QUIRK2_ACMD23_BROKEN,
161 .ops = &sdhci_iproc_ops,
164 static const struct sdhci_iproc_data iproc_data = {
165 .pdata = &sdhci_iproc_pltfm_data,
170 static const struct of_device_id sdhci_iproc_of_match[] = {
171 { .compatible = "brcm,sdhci-iproc-cygnus", .data = &iproc_data },
174 MODULE_DEVICE_TABLE(of, sdhci_iproc_of_match);
176 static int sdhci_iproc_probe(struct platform_device *pdev)
178 const struct of_device_id *match;
179 const struct sdhci_iproc_data *iproc_data;
180 struct sdhci_host *host;
181 struct sdhci_iproc_host *iproc_host;
182 struct sdhci_pltfm_host *pltfm_host;
185 match = of_match_device(sdhci_iproc_of_match, &pdev->dev);
188 iproc_data = match->data;
190 host = sdhci_pltfm_init(pdev, iproc_data->pdata, sizeof(*iproc_host));
192 return PTR_ERR(host);
194 pltfm_host = sdhci_priv(host);
195 iproc_host = sdhci_pltfm_priv(pltfm_host);
197 iproc_host->data = iproc_data;
199 mmc_of_parse(host->mmc);
200 sdhci_get_of_property(pdev);
202 /* Enable EMMC 1/8V DDR capable */
203 host->mmc->caps |= MMC_CAP_1_8V_DDR;
205 pltfm_host->clk = devm_clk_get(&pdev->dev, NULL);
206 if (IS_ERR(pltfm_host->clk)) {
207 ret = PTR_ERR(pltfm_host->clk);
211 if (iproc_host->data->pdata->quirks & SDHCI_QUIRK_MISSING_CAPS) {
212 host->caps = iproc_host->data->caps;
213 host->caps1 = iproc_host->data->caps1;
216 return sdhci_add_host(host);
219 sdhci_pltfm_free(pdev);
223 static int sdhci_iproc_remove(struct platform_device *pdev)
225 return sdhci_pltfm_unregister(pdev);
228 static struct platform_driver sdhci_iproc_driver = {
230 .name = "sdhci-iproc",
231 .of_match_table = sdhci_iproc_of_match,
232 .pm = SDHCI_PLTFM_PMOPS,
234 .probe = sdhci_iproc_probe,
235 .remove = sdhci_iproc_remove,
237 module_platform_driver(sdhci_iproc_driver);
239 MODULE_AUTHOR("Broadcom");
240 MODULE_DESCRIPTION("IPROC SDHCI driver");
241 MODULE_LICENSE("GPL v2");