xref: /rk3399_rockchip-uboot/drivers/mmc/bcm2835_sdhci.c (revision aba554cc34bdfc92eb13ef852a73d6bd82329f37)
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 
539a4fbe4fSStephen Warren struct bcm2835_sdhci_host {
549a4fbe4fSStephen Warren 	struct sdhci_host host;
559a4fbe4fSStephen Warren 	uint twoticks_delay;
569a4fbe4fSStephen Warren 	ulong last_write;
579a4fbe4fSStephen Warren };
589a4fbe4fSStephen Warren 
to_bcm(struct sdhci_host * host)599a4fbe4fSStephen Warren static inline struct bcm2835_sdhci_host *to_bcm(struct sdhci_host *host)
609a4fbe4fSStephen Warren {
619a4fbe4fSStephen Warren 	return (struct bcm2835_sdhci_host *)host;
629a4fbe4fSStephen Warren }
639a4fbe4fSStephen Warren 
bcm2835_sdhci_raw_writel(struct sdhci_host * host,u32 val,int reg)649a4fbe4fSStephen Warren static inline void bcm2835_sdhci_raw_writel(struct sdhci_host *host, u32 val,
659a4fbe4fSStephen Warren 					    int reg)
669a4fbe4fSStephen Warren {
679a4fbe4fSStephen Warren 	struct bcm2835_sdhci_host *bcm_host = to_bcm(host);
689a4fbe4fSStephen Warren 
699a4fbe4fSStephen Warren 	/*
709a4fbe4fSStephen Warren 	 * The Arasan has a bugette whereby it may lose the content of
719a4fbe4fSStephen Warren 	 * successive writes to registers that are within two SD-card clock
729a4fbe4fSStephen Warren 	 * cycles of each other (a clock domain crossing problem).
739a4fbe4fSStephen Warren 	 * It seems, however, that the data register does not have this problem.
749a4fbe4fSStephen Warren 	 * (Which is just as well - otherwise we'd have to nobble the DMA engine
759a4fbe4fSStephen Warren 	 * too)
769a4fbe4fSStephen Warren 	 */
774db2b61fSJocelyn Bohr 	if (reg != SDHCI_BUFFER) {
78*aba554ccSJocelyn Bohr 		while (timer_get_us() - bcm_host->last_write < bcm_host->twoticks_delay)
799a4fbe4fSStephen Warren 			;
804db2b61fSJocelyn Bohr 	}
819a4fbe4fSStephen Warren 
829a4fbe4fSStephen Warren 	writel(val, host->ioaddr + reg);
839f1b4456SMarek Vasut 	bcm_host->last_write = timer_get_us();
849a4fbe4fSStephen Warren }
859a4fbe4fSStephen Warren 
bcm2835_sdhci_raw_readl(struct sdhci_host * host,int reg)869a4fbe4fSStephen Warren static inline u32 bcm2835_sdhci_raw_readl(struct sdhci_host *host, int reg)
879a4fbe4fSStephen Warren {
889a4fbe4fSStephen Warren 	return readl(host->ioaddr + reg);
899a4fbe4fSStephen Warren }
909a4fbe4fSStephen Warren 
bcm2835_sdhci_writel(struct sdhci_host * host,u32 val,int reg)919a4fbe4fSStephen Warren static void bcm2835_sdhci_writel(struct sdhci_host *host, u32 val, int reg)
929a4fbe4fSStephen Warren {
939a4fbe4fSStephen Warren 	bcm2835_sdhci_raw_writel(host, val, reg);
949a4fbe4fSStephen Warren }
959a4fbe4fSStephen Warren 
bcm2835_sdhci_writew(struct sdhci_host * host,u16 val,int reg)969a4fbe4fSStephen Warren static void bcm2835_sdhci_writew(struct sdhci_host *host, u16 val, int reg)
979a4fbe4fSStephen Warren {
989a4fbe4fSStephen Warren 	static u32 shadow;
999a4fbe4fSStephen Warren 	u32 oldval = (reg == SDHCI_COMMAND) ? shadow :
1009a4fbe4fSStephen Warren 		bcm2835_sdhci_raw_readl(host, reg & ~3);
1019a4fbe4fSStephen Warren 	u32 word_num = (reg >> 1) & 1;
1029a4fbe4fSStephen Warren 	u32 word_shift = word_num * 16;
1039a4fbe4fSStephen Warren 	u32 mask = 0xffff << word_shift;
1049a4fbe4fSStephen Warren 	u32 newval = (oldval & ~mask) | (val << word_shift);
1059a4fbe4fSStephen Warren 
1069a4fbe4fSStephen Warren 	if (reg == SDHCI_TRANSFER_MODE)
1079a4fbe4fSStephen Warren 		shadow = newval;
1089a4fbe4fSStephen Warren 	else
1099a4fbe4fSStephen Warren 		bcm2835_sdhci_raw_writel(host, newval, reg & ~3);
1109a4fbe4fSStephen Warren }
1119a4fbe4fSStephen Warren 
bcm2835_sdhci_writeb(struct sdhci_host * host,u8 val,int reg)1129a4fbe4fSStephen Warren static void bcm2835_sdhci_writeb(struct sdhci_host *host, u8 val, int reg)
1139a4fbe4fSStephen Warren {
1149a4fbe4fSStephen Warren 	u32 oldval = bcm2835_sdhci_raw_readl(host, reg & ~3);
1159a4fbe4fSStephen Warren 	u32 byte_num = reg & 3;
1169a4fbe4fSStephen Warren 	u32 byte_shift = byte_num * 8;
1179a4fbe4fSStephen Warren 	u32 mask = 0xff << byte_shift;
1189a4fbe4fSStephen Warren 	u32 newval = (oldval & ~mask) | (val << byte_shift);
1199a4fbe4fSStephen Warren 
1209a4fbe4fSStephen Warren 	bcm2835_sdhci_raw_writel(host, newval, reg & ~3);
1219a4fbe4fSStephen Warren }
1229a4fbe4fSStephen Warren 
bcm2835_sdhci_readl(struct sdhci_host * host,int reg)1239a4fbe4fSStephen Warren static u32 bcm2835_sdhci_readl(struct sdhci_host *host, int reg)
1249a4fbe4fSStephen Warren {
1259a4fbe4fSStephen Warren 	u32 val = bcm2835_sdhci_raw_readl(host, reg);
1269a4fbe4fSStephen Warren 
1279a4fbe4fSStephen Warren 	return val;
1289a4fbe4fSStephen Warren }
1299a4fbe4fSStephen Warren 
bcm2835_sdhci_readw(struct sdhci_host * host,int reg)1309a4fbe4fSStephen Warren static u16 bcm2835_sdhci_readw(struct sdhci_host *host, int reg)
1319a4fbe4fSStephen Warren {
1329a4fbe4fSStephen Warren 	u32 val = bcm2835_sdhci_raw_readl(host, (reg & ~3));
1339a4fbe4fSStephen Warren 	u32 word_num = (reg >> 1) & 1;
1349a4fbe4fSStephen Warren 	u32 word_shift = word_num * 16;
1359a4fbe4fSStephen Warren 	u32 word = (val >> word_shift) & 0xffff;
1369a4fbe4fSStephen Warren 
1379a4fbe4fSStephen Warren 	return word;
1389a4fbe4fSStephen Warren }
1399a4fbe4fSStephen Warren 
bcm2835_sdhci_readb(struct sdhci_host * host,int reg)1409a4fbe4fSStephen Warren static u8 bcm2835_sdhci_readb(struct sdhci_host *host, int reg)
1419a4fbe4fSStephen Warren {
1429a4fbe4fSStephen Warren 	u32 val = bcm2835_sdhci_raw_readl(host, (reg & ~3));
1439a4fbe4fSStephen Warren 	u32 byte_num = reg & 3;
1449a4fbe4fSStephen Warren 	u32 byte_shift = byte_num * 8;
1459a4fbe4fSStephen Warren 	u32 byte = (val >> byte_shift) & 0xff;
1469a4fbe4fSStephen Warren 
1479a4fbe4fSStephen Warren 	return byte;
1489a4fbe4fSStephen Warren }
1499a4fbe4fSStephen Warren 
1509a4fbe4fSStephen Warren static const struct sdhci_ops bcm2835_ops = {
1519a4fbe4fSStephen Warren 	.write_l = bcm2835_sdhci_writel,
1529a4fbe4fSStephen Warren 	.write_w = bcm2835_sdhci_writew,
1539a4fbe4fSStephen Warren 	.write_b = bcm2835_sdhci_writeb,
1549a4fbe4fSStephen Warren 	.read_l = bcm2835_sdhci_readl,
1559a4fbe4fSStephen Warren 	.read_w = bcm2835_sdhci_readw,
1569a4fbe4fSStephen Warren 	.read_b = bcm2835_sdhci_readb,
1579a4fbe4fSStephen Warren };
1589a4fbe4fSStephen Warren 
bcm2835_sdhci_bind(struct udevice * dev)159e6c6d07eSSimon Glass static int bcm2835_sdhci_bind(struct udevice *dev)
1609a4fbe4fSStephen Warren {
161e6c6d07eSSimon Glass 	struct bcm2835_sdhci_plat *plat = dev_get_platdata(dev);
1629a4fbe4fSStephen Warren 
163e6c6d07eSSimon Glass 	return sdhci_bind(dev, &plat->mmc, &plat->cfg);
1649a4fbe4fSStephen Warren }
1659a4fbe4fSStephen Warren 
bcm2835_sdhci_probe(struct udevice * dev)166e6c6d07eSSimon Glass static int bcm2835_sdhci_probe(struct udevice *dev)
167e6c6d07eSSimon Glass {
168e6c6d07eSSimon Glass 	struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
169e6c6d07eSSimon Glass 	struct bcm2835_sdhci_plat *plat = dev_get_platdata(dev);
170e6c6d07eSSimon Glass 	struct bcm2835_sdhci_host *priv = dev_get_priv(dev);
171e6c6d07eSSimon Glass 	struct sdhci_host *host = &priv->host;
172e6c6d07eSSimon Glass 	fdt_addr_t base;
173e6c6d07eSSimon Glass 	int emmc_freq;
174e6c6d07eSSimon Glass 	int ret;
175e6c6d07eSSimon Glass 
176a821c4afSSimon Glass 	base = devfdt_get_addr(dev);
177e6c6d07eSSimon Glass 	if (base == FDT_ADDR_T_NONE)
178e6c6d07eSSimon Glass 		return -EINVAL;
179e6c6d07eSSimon Glass 
180e6c6d07eSSimon Glass 	ret = bcm2835_get_mmc_clock();
181e6c6d07eSSimon Glass 	if (ret < 0) {
182e6c6d07eSSimon Glass 		debug("%s: Failed to set MMC clock (err=%d)\n", __func__, ret);
183e6c6d07eSSimon Glass 		return ret;
184e6c6d07eSSimon Glass 	}
185e6c6d07eSSimon Glass 	emmc_freq = ret;
186e6c6d07eSSimon Glass 
1879a4fbe4fSStephen Warren 	/*
1889a4fbe4fSStephen Warren 	 * See the comments in bcm2835_sdhci_raw_writel().
1899a4fbe4fSStephen Warren 	 *
1909a4fbe4fSStephen Warren 	 * This should probably be dynamically calculated based on the actual
1919a4fbe4fSStephen Warren 	 * frequency. However, this is the longest we'll have to wait, and
1929a4fbe4fSStephen Warren 	 * doesn't seem to slow access down too much, so the added complexity
1939a4fbe4fSStephen Warren 	 * doesn't seem worth it for now.
1949a4fbe4fSStephen Warren 	 *
1959a4fbe4fSStephen Warren 	 * 1/MIN_FREQ is (max) time per tick of eMMC clock.
1969a4fbe4fSStephen Warren 	 * 2/MIN_FREQ is time for two ticks.
1979a4fbe4fSStephen Warren 	 * Multiply by 1000000 to get uS per two ticks.
1989a4fbe4fSStephen Warren 	 * +1 for hack rounding.
1999a4fbe4fSStephen Warren 	 */
200e6c6d07eSSimon Glass 	priv->twoticks_delay = ((2 * 1000000) / MIN_FREQ) + 1;
201e6c6d07eSSimon Glass 	priv->last_write = 0;
2029a4fbe4fSStephen Warren 
203e6c6d07eSSimon Glass 	host->name = dev->name;
204e6c6d07eSSimon Glass 	host->ioaddr = (void *)base;
2059a4fbe4fSStephen Warren 	host->quirks = SDHCI_QUIRK_BROKEN_VOLTAGE | SDHCI_QUIRK_BROKEN_R1B |
20664973023SLubomir Rintel 		SDHCI_QUIRK_WAIT_SEND_CMD | SDHCI_QUIRK_NO_HISPD_BIT;
2076d0e34bfSStefan Herbrechtsmeier 	host->max_clk = emmc_freq;
2089a4fbe4fSStephen Warren 	host->voltages = MMC_VDD_32_33 | MMC_VDD_33_34 | MMC_VDD_165_195;
2099a4fbe4fSStephen Warren 	host->ops = &bcm2835_ops;
2109a4fbe4fSStephen Warren 
211e6c6d07eSSimon Glass 	ret = sdhci_setup_cfg(&plat->cfg, host, emmc_freq, MIN_FREQ);
212e6c6d07eSSimon Glass 	if (ret) {
213e6c6d07eSSimon Glass 		debug("%s: Failed to setup SDHCI (err=%d)\n", __func__, ret);
214e6c6d07eSSimon Glass 		return ret;
2159a4fbe4fSStephen Warren 	}
216e6c6d07eSSimon Glass 
217e6c6d07eSSimon Glass 	upriv->mmc = &plat->mmc;
218e6c6d07eSSimon Glass 	host->mmc = &plat->mmc;
219e6c6d07eSSimon Glass 	host->mmc->priv = host;
220e6c6d07eSSimon Glass 
221e6c6d07eSSimon Glass 	return sdhci_probe(dev);
222e6c6d07eSSimon Glass }
223e6c6d07eSSimon Glass 
224e6c6d07eSSimon Glass static const struct udevice_id bcm2835_sdhci_match[] = {
225e6c6d07eSSimon Glass 	{ .compatible = "brcm,bcm2835-sdhci" },
226e6c6d07eSSimon Glass 	{ /* sentinel */ }
227e6c6d07eSSimon Glass };
228e6c6d07eSSimon Glass 
229e6c6d07eSSimon Glass U_BOOT_DRIVER(sdhci_cdns) = {
230e6c6d07eSSimon Glass 	.name = "sdhci-bcm2835",
231e6c6d07eSSimon Glass 	.id = UCLASS_MMC,
232e6c6d07eSSimon Glass 	.of_match = bcm2835_sdhci_match,
233e6c6d07eSSimon Glass 	.bind = bcm2835_sdhci_bind,
234e6c6d07eSSimon Glass 	.probe = bcm2835_sdhci_probe,
235e6c6d07eSSimon Glass 	.priv_auto_alloc_size = sizeof(struct bcm2835_sdhci_host),
236e6c6d07eSSimon Glass 	.platdata_auto_alloc_size = sizeof(struct bcm2835_sdhci_plat),
237e6c6d07eSSimon Glass 	.ops = &sdhci_ops,
238e6c6d07eSSimon Glass };
239