19a4fbe4fSStephen Warren /*
29a4fbe4fSStephen Warren * This code was extracted from:
39a4fbe4fSStephen Warren * git://github.com/gonzoua/u-boot-pi.git master
49a4fbe4fSStephen Warren * and hence presumably (C) 2012 Oleksandr Tymoshenko
59a4fbe4fSStephen Warren *
69a4fbe4fSStephen Warren * Tweaks for U-Boot upstreaming
79a4fbe4fSStephen Warren * (C) 2012 Stephen Warren
89a4fbe4fSStephen Warren *
99a4fbe4fSStephen Warren * Portions (e.g. read/write macros, concepts for back-to-back register write
109a4fbe4fSStephen Warren * timing workarounds) obviously extracted from the Linux kernel at:
119a4fbe4fSStephen Warren * https://github.com/raspberrypi/linux.git rpi-3.6.y
129a4fbe4fSStephen Warren *
139a4fbe4fSStephen Warren * The Linux kernel code has the following (c) and license, which is hence
149a4fbe4fSStephen Warren * propagated to Oleksandr's tree and here:
159a4fbe4fSStephen Warren *
169a4fbe4fSStephen Warren * Support for SDHCI device on 2835
179a4fbe4fSStephen Warren * Based on sdhci-bcm2708.c (c) 2010 Broadcom
189a4fbe4fSStephen Warren *
199a4fbe4fSStephen Warren * This program is free software; you can redistribute it and/or modify
209a4fbe4fSStephen Warren * it under the terms of the GNU General Public License version 2 as
219a4fbe4fSStephen Warren * published by the Free Software Foundation.
229a4fbe4fSStephen Warren *
239a4fbe4fSStephen Warren * This program is distributed in the hope that it will be useful,
249a4fbe4fSStephen Warren * but WITHOUT ANY WARRANTY; without even the implied warranty of
259a4fbe4fSStephen Warren * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
269a4fbe4fSStephen Warren * GNU General Public License for more details.
279a4fbe4fSStephen Warren *
289a4fbe4fSStephen Warren * You should have received a copy of the GNU General Public License
299a4fbe4fSStephen Warren * along with this program; if not, write to the Free Software
309a4fbe4fSStephen Warren * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
319a4fbe4fSStephen Warren */
329a4fbe4fSStephen Warren
339a4fbe4fSStephen Warren /* Supports:
349a4fbe4fSStephen Warren * SDHCI platform device - Arasan SD controller in BCM2708
359a4fbe4fSStephen Warren *
369a4fbe4fSStephen Warren * Inspired by sdhci-pci.c, by Pierre Ossman
379a4fbe4fSStephen Warren */
389a4fbe4fSStephen Warren
399a4fbe4fSStephen Warren #include <common.h>
40e6c6d07eSSimon Glass #include <dm.h>
419a4fbe4fSStephen Warren #include <malloc.h>
42e6c6d07eSSimon Glass #include <memalign.h>
439a4fbe4fSStephen Warren #include <sdhci.h>
44e6c6d07eSSimon Glass #include <asm/arch/msg.h>
45e6c6d07eSSimon Glass #include <asm/arch/mbox.h>
46d6c418e4SMasahiro Yamada #include <mach/sdhci.h>
47e6c6d07eSSimon Glass #include <mach/timer.h>
489a4fbe4fSStephen Warren
499a4fbe4fSStephen Warren /* 400KHz is max freq for card ID etc. Use that as min */
509a4fbe4fSStephen Warren #define MIN_FREQ 400000
514db2b61fSJocelyn Bohr #define SDHCI_BUFFER 0x20
529a4fbe4fSStephen Warren
53e6c6d07eSSimon Glass struct bcm2835_sdhci_plat {
54e6c6d07eSSimon Glass struct mmc_config cfg;
55e6c6d07eSSimon Glass struct mmc mmc;
56e6c6d07eSSimon Glass };
57e6c6d07eSSimon Glass
589a4fbe4fSStephen Warren struct bcm2835_sdhci_host {
599a4fbe4fSStephen Warren struct sdhci_host host;
609a4fbe4fSStephen Warren uint twoticks_delay;
619a4fbe4fSStephen Warren ulong last_write;
629a4fbe4fSStephen Warren };
639a4fbe4fSStephen Warren
to_bcm(struct sdhci_host * host)649a4fbe4fSStephen Warren static inline struct bcm2835_sdhci_host *to_bcm(struct sdhci_host *host)
659a4fbe4fSStephen Warren {
669a4fbe4fSStephen Warren return (struct bcm2835_sdhci_host *)host;
679a4fbe4fSStephen Warren }
689a4fbe4fSStephen Warren
bcm2835_sdhci_raw_writel(struct sdhci_host * host,u32 val,int reg)699a4fbe4fSStephen Warren static inline void bcm2835_sdhci_raw_writel(struct sdhci_host *host, u32 val,
709a4fbe4fSStephen Warren int reg)
719a4fbe4fSStephen Warren {
729a4fbe4fSStephen Warren struct bcm2835_sdhci_host *bcm_host = to_bcm(host);
739a4fbe4fSStephen Warren
749a4fbe4fSStephen Warren /*
759a4fbe4fSStephen Warren * The Arasan has a bugette whereby it may lose the content of
769a4fbe4fSStephen Warren * successive writes to registers that are within two SD-card clock
779a4fbe4fSStephen Warren * cycles of each other (a clock domain crossing problem).
789a4fbe4fSStephen Warren * It seems, however, that the data register does not have this problem.
799a4fbe4fSStephen Warren * (Which is just as well - otherwise we'd have to nobble the DMA engine
809a4fbe4fSStephen Warren * too)
819a4fbe4fSStephen Warren */
824db2b61fSJocelyn Bohr if (reg != SDHCI_BUFFER) {
834db2b61fSJocelyn Bohr while (timer_get_us() - bcm_host->last_write <
844db2b61fSJocelyn Bohr bcm_host->twoticks_delay)
859a4fbe4fSStephen Warren ;
864db2b61fSJocelyn Bohr }
879a4fbe4fSStephen Warren
889a4fbe4fSStephen Warren writel(val, host->ioaddr + reg);
899f1b4456SMarek Vasut bcm_host->last_write = timer_get_us();
909a4fbe4fSStephen Warren }
919a4fbe4fSStephen Warren
bcm2835_sdhci_raw_readl(struct sdhci_host * host,int reg)929a4fbe4fSStephen Warren static inline u32 bcm2835_sdhci_raw_readl(struct sdhci_host *host, int reg)
939a4fbe4fSStephen Warren {
949a4fbe4fSStephen Warren return readl(host->ioaddr + reg);
959a4fbe4fSStephen Warren }
969a4fbe4fSStephen Warren
bcm2835_sdhci_writel(struct sdhci_host * host,u32 val,int reg)979a4fbe4fSStephen Warren static void bcm2835_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
989a4fbe4fSStephen Warren {
999a4fbe4fSStephen Warren bcm2835_sdhci_raw_writel(host, val, reg);
1009a4fbe4fSStephen Warren }
1019a4fbe4fSStephen Warren
bcm2835_sdhci_writew(struct sdhci_host * host,u16 val,int reg)1029a4fbe4fSStephen Warren static void bcm2835_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
1039a4fbe4fSStephen Warren {
1049a4fbe4fSStephen Warren static u32 shadow;
1059a4fbe4fSStephen Warren u32 oldval = (reg == SDHCI_COMMAND) ? shadow :
1069a4fbe4fSStephen Warren bcm2835_sdhci_raw_readl(host, reg & ~3);
1079a4fbe4fSStephen Warren u32 word_num = (reg >> 1) & 1;
1089a4fbe4fSStephen Warren u32 word_shift = word_num * 16;
1099a4fbe4fSStephen Warren u32 mask = 0xffff << word_shift;
1109a4fbe4fSStephen Warren u32 newval = (oldval & ~mask) | (val << word_shift);
1119a4fbe4fSStephen Warren
1129a4fbe4fSStephen Warren if (reg == SDHCI_TRANSFER_MODE)
1139a4fbe4fSStephen Warren shadow = newval;
1149a4fbe4fSStephen Warren else
1159a4fbe4fSStephen Warren bcm2835_sdhci_raw_writel(host, newval, reg & ~3);
1169a4fbe4fSStephen Warren }
1179a4fbe4fSStephen Warren
bcm2835_sdhci_writeb(struct sdhci_host * host,u8 val,int reg)1189a4fbe4fSStephen Warren static void bcm2835_sdhci_writeb(struct sdhci_host *host, u8 val, int reg)
1199a4fbe4fSStephen Warren {
1209a4fbe4fSStephen Warren u32 oldval = bcm2835_sdhci_raw_readl(host, reg & ~3);
1219a4fbe4fSStephen Warren u32 byte_num = reg & 3;
1229a4fbe4fSStephen Warren u32 byte_shift = byte_num * 8;
1239a4fbe4fSStephen Warren u32 mask = 0xff << byte_shift;
1249a4fbe4fSStephen Warren u32 newval = (oldval & ~mask) | (val << byte_shift);
1259a4fbe4fSStephen Warren
1269a4fbe4fSStephen Warren bcm2835_sdhci_raw_writel(host, newval, reg & ~3);
1279a4fbe4fSStephen Warren }
1289a4fbe4fSStephen Warren
bcm2835_sdhci_readl(struct sdhci_host * host,int reg)1299a4fbe4fSStephen Warren static u32 bcm2835_sdhci_readl(struct sdhci_host *host, int reg)
1309a4fbe4fSStephen Warren {
1319a4fbe4fSStephen Warren u32 val = bcm2835_sdhci_raw_readl(host, reg);
1329a4fbe4fSStephen Warren
1339a4fbe4fSStephen Warren return val;
1349a4fbe4fSStephen Warren }
1359a4fbe4fSStephen Warren
bcm2835_sdhci_readw(struct sdhci_host * host,int reg)1369a4fbe4fSStephen Warren static u16 bcm2835_sdhci_readw(struct sdhci_host *host, int reg)
1379a4fbe4fSStephen Warren {
1389a4fbe4fSStephen Warren u32 val = bcm2835_sdhci_raw_readl(host, (reg & ~3));
1399a4fbe4fSStephen Warren u32 word_num = (reg >> 1) & 1;
1409a4fbe4fSStephen Warren u32 word_shift = word_num * 16;
1419a4fbe4fSStephen Warren u32 word = (val >> word_shift) & 0xffff;
1429a4fbe4fSStephen Warren
1439a4fbe4fSStephen Warren return word;
1449a4fbe4fSStephen Warren }
1459a4fbe4fSStephen Warren
bcm2835_sdhci_readb(struct sdhci_host * host,int reg)1469a4fbe4fSStephen Warren static u8 bcm2835_sdhci_readb(struct sdhci_host *host, int reg)
1479a4fbe4fSStephen Warren {
1489a4fbe4fSStephen Warren u32 val = bcm2835_sdhci_raw_readl(host, (reg & ~3));
1499a4fbe4fSStephen Warren u32 byte_num = reg & 3;
1509a4fbe4fSStephen Warren u32 byte_shift = byte_num * 8;
1519a4fbe4fSStephen Warren u32 byte = (val >> byte_shift) & 0xff;
1529a4fbe4fSStephen Warren
1539a4fbe4fSStephen Warren return byte;
1549a4fbe4fSStephen Warren }
1559a4fbe4fSStephen Warren
1569a4fbe4fSStephen Warren static const struct sdhci_ops bcm2835_ops = {
1579a4fbe4fSStephen Warren .write_l = bcm2835_sdhci_writel,
1589a4fbe4fSStephen Warren .write_w = bcm2835_sdhci_writew,
1599a4fbe4fSStephen Warren .write_b = bcm2835_sdhci_writeb,
1609a4fbe4fSStephen Warren .read_l = bcm2835_sdhci_readl,
1619a4fbe4fSStephen Warren .read_w = bcm2835_sdhci_readw,
1629a4fbe4fSStephen Warren .read_b = bcm2835_sdhci_readb,
1639a4fbe4fSStephen Warren };
1649a4fbe4fSStephen Warren
bcm2835_sdhci_bind(struct udevice * dev)165e6c6d07eSSimon Glass static int bcm2835_sdhci_bind(struct udevice *dev)
1669a4fbe4fSStephen Warren {
167e6c6d07eSSimon Glass struct bcm2835_sdhci_plat *plat = dev_get_platdata(dev);
1689a4fbe4fSStephen Warren
169e6c6d07eSSimon Glass return sdhci_bind(dev, &plat->mmc, &plat->cfg);
1709a4fbe4fSStephen Warren }
1719a4fbe4fSStephen Warren
bcm2835_sdhci_probe(struct udevice * dev)172e6c6d07eSSimon Glass static int bcm2835_sdhci_probe(struct udevice *dev)
173e6c6d07eSSimon Glass {
174e6c6d07eSSimon Glass struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
175e6c6d07eSSimon Glass struct bcm2835_sdhci_plat *plat = dev_get_platdata(dev);
176e6c6d07eSSimon Glass struct bcm2835_sdhci_host *priv = dev_get_priv(dev);
177e6c6d07eSSimon Glass struct sdhci_host *host = &priv->host;
178e6c6d07eSSimon Glass fdt_addr_t base;
179e6c6d07eSSimon Glass int emmc_freq;
180e6c6d07eSSimon Glass int ret;
181e6c6d07eSSimon Glass
182a821c4afSSimon Glass base = devfdt_get_addr(dev);
183e6c6d07eSSimon Glass if (base == FDT_ADDR_T_NONE)
184e6c6d07eSSimon Glass return -EINVAL;
185e6c6d07eSSimon Glass
186*8ae1f829SJonathan Gray ret = bcm2835_get_mmc_clock(BCM2835_MBOX_CLOCK_ID_EMMC);
187e6c6d07eSSimon Glass if (ret < 0) {
188e6c6d07eSSimon Glass debug("%s: Failed to set MMC clock (err=%d)\n", __func__, ret);
189e6c6d07eSSimon Glass return ret;
190e6c6d07eSSimon Glass }
191e6c6d07eSSimon Glass emmc_freq = ret;
192e6c6d07eSSimon Glass
1939a4fbe4fSStephen Warren /*
1949a4fbe4fSStephen Warren * See the comments in bcm2835_sdhci_raw_writel().
1959a4fbe4fSStephen Warren *
1969a4fbe4fSStephen Warren * This should probably be dynamically calculated based on the actual
1979a4fbe4fSStephen Warren * frequency. However, this is the longest we'll have to wait, and
1989a4fbe4fSStephen Warren * doesn't seem to slow access down too much, so the added complexity
1999a4fbe4fSStephen Warren * doesn't seem worth it for now.
2009a4fbe4fSStephen Warren *
2019a4fbe4fSStephen Warren * 1/MIN_FREQ is (max) time per tick of eMMC clock.
2029a4fbe4fSStephen Warren * 2/MIN_FREQ is time for two ticks.
2039a4fbe4fSStephen Warren * Multiply by 1000000 to get uS per two ticks.
2049a4fbe4fSStephen Warren * +1 for hack rounding.
2059a4fbe4fSStephen Warren */
206e6c6d07eSSimon Glass priv->twoticks_delay = ((2 * 1000000) / MIN_FREQ) + 1;
207e6c6d07eSSimon Glass priv->last_write = 0;
2089a4fbe4fSStephen Warren
209e6c6d07eSSimon Glass host->name = dev->name;
210e6c6d07eSSimon Glass host->ioaddr = (void *)base;
2119a4fbe4fSStephen Warren host->quirks = SDHCI_QUIRK_BROKEN_VOLTAGE | SDHCI_QUIRK_BROKEN_R1B |
21264973023SLubomir Rintel SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_NO_HISPD_BIT;
2136d0e34bfSStefan Herbrechtsmeier host->max_clk = emmc_freq;
2149a4fbe4fSStephen Warren host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
2159a4fbe4fSStephen Warren host->ops = &bcm2835_ops;
2169a4fbe4fSStephen Warren
217e6c6d07eSSimon Glass ret = sdhci_setup_cfg(&plat->cfg, host, emmc_freq, MIN_FREQ);
218e6c6d07eSSimon Glass if (ret) {
219e6c6d07eSSimon Glass debug("%s: Failed to setup SDHCI (err=%d)\n", __func__, ret);
220e6c6d07eSSimon Glass return ret;
2219a4fbe4fSStephen Warren }
222e6c6d07eSSimon Glass
223e6c6d07eSSimon Glass upriv->mmc = &plat->mmc;
224e6c6d07eSSimon Glass host->mmc = &plat->mmc;
225e6c6d07eSSimon Glass host->mmc->priv = host;
226e6c6d07eSSimon Glass
227e6c6d07eSSimon Glass return sdhci_probe(dev);
228e6c6d07eSSimon Glass }
229e6c6d07eSSimon Glass
230e6c6d07eSSimon Glass static const struct udevice_id bcm2835_sdhci_match[] = {
231e6c6d07eSSimon Glass { .compatible = "brcm,bcm2835-sdhci" },
232e6c6d07eSSimon Glass { /* sentinel */ }
233e6c6d07eSSimon Glass };
234e6c6d07eSSimon Glass
235e6c6d07eSSimon Glass U_BOOT_DRIVER(sdhci_cdns) = {
236e6c6d07eSSimon Glass .name = "sdhci-bcm2835",
237e6c6d07eSSimon Glass .id = UCLASS_MMC,
238e6c6d07eSSimon Glass .of_match = bcm2835_sdhci_match,
239e6c6d07eSSimon Glass .bind = bcm2835_sdhci_bind,
240e6c6d07eSSimon Glass .probe = bcm2835_sdhci_probe,
241e6c6d07eSSimon Glass .priv_auto_alloc_size = sizeof(struct bcm2835_sdhci_host),
242e6c6d07eSSimon Glass .platdata_auto_alloc_size = sizeof(struct bcm2835_sdhci_plat),
243e6c6d07eSSimon Glass .ops = &sdhci_ops,
244e6c6d07eSSimon Glass };
245