xref: /openbmc/linux/drivers/mmc/host/sdhci-of-esdhc.c (revision 192b5372421766f62fce20d2db2deb19a58e2cfc)
17657c3a7SAlbert Herranz /*
27657c3a7SAlbert Herranz  * Freescale eSDHC controller driver.
37657c3a7SAlbert Herranz  *
4e51cbc9eSXu lei  * Copyright (c) 2007, 2010 Freescale Semiconductor, Inc.
57657c3a7SAlbert Herranz  * Copyright (c) 2009 MontaVista Software, Inc.
67657c3a7SAlbert Herranz  *
77657c3a7SAlbert Herranz  * Authors: Xiaobo Xie <X.Xie@freescale.com>
87657c3a7SAlbert Herranz  *	    Anton Vorontsov <avorontsov@ru.mvista.com>
97657c3a7SAlbert Herranz  *
107657c3a7SAlbert Herranz  * This program is free software; you can redistribute it and/or modify
117657c3a7SAlbert Herranz  * it under the terms of the GNU General Public License as published by
127657c3a7SAlbert Herranz  * the Free Software Foundation; either version 2 of the License, or (at
137657c3a7SAlbert Herranz  * your option) any later version.
147657c3a7SAlbert Herranz  */
157657c3a7SAlbert Herranz 
167657c3a7SAlbert Herranz #include <linux/io.h>
177657c3a7SAlbert Herranz #include <linux/delay.h>
1888b47679SPaul Gortmaker #include <linux/module.h>
197657c3a7SAlbert Herranz #include <linux/mmc/host.h>
2038576af1SShawn Guo #include "sdhci-pltfm.h"
2180872e21SWolfram Sang #include "sdhci-esdhc.h"
227657c3a7SAlbert Herranz 
237657c3a7SAlbert Herranz static u16 esdhc_readw(struct sdhci_host *host, int reg)
247657c3a7SAlbert Herranz {
257657c3a7SAlbert Herranz 	u16 ret;
26e51cbc9eSXu lei 	int base = reg & ~0x3;
27e51cbc9eSXu lei 	int shift = (reg & 0x2) * 8;
287657c3a7SAlbert Herranz 
297657c3a7SAlbert Herranz 	if (unlikely(reg == SDHCI_HOST_VERSION))
30e51cbc9eSXu lei 		ret = in_be32(host->ioaddr + base) & 0xffff;
317657c3a7SAlbert Herranz 	else
32e51cbc9eSXu lei 		ret = (in_be32(host->ioaddr + base) >> shift) & 0xffff;
33e51cbc9eSXu lei 	return ret;
34e51cbc9eSXu lei }
35e51cbc9eSXu lei 
36e51cbc9eSXu lei static u8 esdhc_readb(struct sdhci_host *host, int reg)
37e51cbc9eSXu lei {
38e51cbc9eSXu lei 	int base = reg & ~0x3;
39e51cbc9eSXu lei 	int shift = (reg & 0x3) * 8;
40e51cbc9eSXu lei 	u8 ret = (in_be32(host->ioaddr + base) >> shift) & 0xff;
41ba8c4dc9SRoy Zang 
42ba8c4dc9SRoy Zang 	/*
43ba8c4dc9SRoy Zang 	 * "DMA select" locates at offset 0x28 in SD specification, but on
44ba8c4dc9SRoy Zang 	 * P5020 or P3041, it locates at 0x29.
45ba8c4dc9SRoy Zang 	 */
46ba8c4dc9SRoy Zang 	if (reg == SDHCI_HOST_CONTROL) {
47ba8c4dc9SRoy Zang 		u32 dma_bits;
48ba8c4dc9SRoy Zang 
49ba8c4dc9SRoy Zang 		dma_bits = in_be32(host->ioaddr + reg);
50ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
51ba8c4dc9SRoy Zang 		dma_bits = (dma_bits >> 5) & SDHCI_CTRL_DMA_MASK;
52ba8c4dc9SRoy Zang 
53ba8c4dc9SRoy Zang 		/* fixup the result */
54ba8c4dc9SRoy Zang 		ret &= ~SDHCI_CTRL_DMA_MASK;
55ba8c4dc9SRoy Zang 		ret |= dma_bits;
56ba8c4dc9SRoy Zang 	}
57ba8c4dc9SRoy Zang 
587657c3a7SAlbert Herranz 	return ret;
597657c3a7SAlbert Herranz }
607657c3a7SAlbert Herranz 
617657c3a7SAlbert Herranz static void esdhc_writew(struct sdhci_host *host, u16 val, int reg)
627657c3a7SAlbert Herranz {
637657c3a7SAlbert Herranz 	if (reg == SDHCI_BLOCK_SIZE) {
647657c3a7SAlbert Herranz 		/*
657657c3a7SAlbert Herranz 		 * Two last DMA bits are reserved, and first one is used for
667657c3a7SAlbert Herranz 		 * non-standard blksz of 4096 bytes that we don't support
677657c3a7SAlbert Herranz 		 * yet. So clear the DMA boundary bits.
687657c3a7SAlbert Herranz 		 */
697657c3a7SAlbert Herranz 		val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
707657c3a7SAlbert Herranz 	}
717657c3a7SAlbert Herranz 	sdhci_be32bs_writew(host, val, reg);
727657c3a7SAlbert Herranz }
737657c3a7SAlbert Herranz 
747657c3a7SAlbert Herranz static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg)
757657c3a7SAlbert Herranz {
76ba8c4dc9SRoy Zang 	/*
77ba8c4dc9SRoy Zang 	 * "DMA select" location is offset 0x28 in SD specification, but on
78ba8c4dc9SRoy Zang 	 * P5020 or P3041, it's located at 0x29.
79ba8c4dc9SRoy Zang 	 */
80ba8c4dc9SRoy Zang 	if (reg == SDHCI_HOST_CONTROL) {
81ba8c4dc9SRoy Zang 		u32 dma_bits;
82ba8c4dc9SRoy Zang 
83ba8c4dc9SRoy Zang 		/* DMA select is 22,23 bits in Protocol Control Register */
84ba8c4dc9SRoy Zang 		dma_bits = (val & SDHCI_CTRL_DMA_MASK) << 5;
85ba8c4dc9SRoy Zang 		clrsetbits_be32(host->ioaddr + reg , SDHCI_CTRL_DMA_MASK << 5,
86ba8c4dc9SRoy Zang 			dma_bits);
87ba8c4dc9SRoy Zang 		val &= ~SDHCI_CTRL_DMA_MASK;
88ba8c4dc9SRoy Zang 		val |= in_be32(host->ioaddr + reg) & SDHCI_CTRL_DMA_MASK;
89ba8c4dc9SRoy Zang 	}
90ba8c4dc9SRoy Zang 
917657c3a7SAlbert Herranz 	/* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */
927657c3a7SAlbert Herranz 	if (reg == SDHCI_HOST_CONTROL)
937657c3a7SAlbert Herranz 		val &= ~ESDHC_HOST_CONTROL_RES;
947657c3a7SAlbert Herranz 	sdhci_be32bs_writeb(host, val, reg);
957657c3a7SAlbert Herranz }
967657c3a7SAlbert Herranz 
9780872e21SWolfram Sang static int esdhc_of_enable_dma(struct sdhci_host *host)
987657c3a7SAlbert Herranz {
997657c3a7SAlbert Herranz 	setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP);
1007657c3a7SAlbert Herranz 	return 0;
1017657c3a7SAlbert Herranz }
1027657c3a7SAlbert Herranz 
10380872e21SWolfram Sang static unsigned int esdhc_of_get_max_clock(struct sdhci_host *host)
1047657c3a7SAlbert Herranz {
105e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1067657c3a7SAlbert Herranz 
107e307148fSShawn Guo 	return pltfm_host->clock;
1087657c3a7SAlbert Herranz }
1097657c3a7SAlbert Herranz 
11080872e21SWolfram Sang static unsigned int esdhc_of_get_min_clock(struct sdhci_host *host)
1117657c3a7SAlbert Herranz {
112e307148fSShawn Guo 	struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
1137657c3a7SAlbert Herranz 
114e307148fSShawn Guo 	return pltfm_host->clock / 256 / 16;
1157657c3a7SAlbert Herranz }
1167657c3a7SAlbert Herranz 
117*192b5372SJerry Huang #ifdef CONFIG_PM
118*192b5372SJerry Huang static u32 esdhc_proctl;
119*192b5372SJerry Huang static void esdhc_of_suspend(struct sdhci_host *host)
120*192b5372SJerry Huang {
121*192b5372SJerry Huang 	esdhc_proctl = sdhci_be32bs_readl(host, SDHCI_HOST_CONTROL);
122*192b5372SJerry Huang }
123*192b5372SJerry Huang 
124*192b5372SJerry Huang static void esdhc_of_resume(struct sdhci_host *host)
125*192b5372SJerry Huang {
126*192b5372SJerry Huang 	esdhc_of_enable_dma(host);
127*192b5372SJerry Huang 	sdhci_be32bs_writel(host, esdhc_proctl, SDHCI_HOST_CONTROL);
128*192b5372SJerry Huang }
129*192b5372SJerry Huang #endif
130*192b5372SJerry Huang 
131e307148fSShawn Guo static struct sdhci_ops sdhci_esdhc_ops = {
132dc297c92SMatt Fleming 	.read_l = sdhci_be32bs_readl,
133dc297c92SMatt Fleming 	.read_w = esdhc_readw,
134e51cbc9eSXu lei 	.read_b = esdhc_readb,
135dc297c92SMatt Fleming 	.write_l = sdhci_be32bs_writel,
136dc297c92SMatt Fleming 	.write_w = esdhc_writew,
137dc297c92SMatt Fleming 	.write_b = esdhc_writeb,
1387657c3a7SAlbert Herranz 	.set_clock = esdhc_set_clock,
13980872e21SWolfram Sang 	.enable_dma = esdhc_of_enable_dma,
14080872e21SWolfram Sang 	.get_max_clock = esdhc_of_get_max_clock,
14180872e21SWolfram Sang 	.get_min_clock = esdhc_of_get_min_clock,
142*192b5372SJerry Huang #ifdef CONFIG_PM
143*192b5372SJerry Huang 	.platform_suspend = esdhc_of_suspend,
144*192b5372SJerry Huang 	.platform_resume = esdhc_of_resume,
145*192b5372SJerry Huang #endif
146e307148fSShawn Guo };
147e307148fSShawn Guo 
14838576af1SShawn Guo static struct sdhci_pltfm_data sdhci_esdhc_pdata = {
149e307148fSShawn Guo 	/* card detection could be handled via GPIO */
150e307148fSShawn Guo 	.quirks = ESDHC_DEFAULT_QUIRKS | SDHCI_QUIRK_BROKEN_CARD_DETECTION
151e307148fSShawn Guo 		| SDHCI_QUIRK_NO_CARD_NO_RESET,
152e307148fSShawn Guo 	.ops = &sdhci_esdhc_ops,
1537657c3a7SAlbert Herranz };
15438576af1SShawn Guo 
15538576af1SShawn Guo static int __devinit sdhci_esdhc_probe(struct platform_device *pdev)
15638576af1SShawn Guo {
15738576af1SShawn Guo 	return sdhci_pltfm_register(pdev, &sdhci_esdhc_pdata);
15838576af1SShawn Guo }
15938576af1SShawn Guo 
16038576af1SShawn Guo static int __devexit sdhci_esdhc_remove(struct platform_device *pdev)
16138576af1SShawn Guo {
16238576af1SShawn Guo 	return sdhci_pltfm_unregister(pdev);
16338576af1SShawn Guo }
16438576af1SShawn Guo 
16538576af1SShawn Guo static const struct of_device_id sdhci_esdhc_of_match[] = {
16638576af1SShawn Guo 	{ .compatible = "fsl,mpc8379-esdhc" },
16738576af1SShawn Guo 	{ .compatible = "fsl,mpc8536-esdhc" },
16838576af1SShawn Guo 	{ .compatible = "fsl,esdhc" },
16938576af1SShawn Guo 	{ }
17038576af1SShawn Guo };
17138576af1SShawn Guo MODULE_DEVICE_TABLE(of, sdhci_esdhc_of_match);
17238576af1SShawn Guo 
17338576af1SShawn Guo static struct platform_driver sdhci_esdhc_driver = {
17438576af1SShawn Guo 	.driver = {
17538576af1SShawn Guo 		.name = "sdhci-esdhc",
17638576af1SShawn Guo 		.owner = THIS_MODULE,
17738576af1SShawn Guo 		.of_match_table = sdhci_esdhc_of_match,
17829495aa0SManuel Lauss 		.pm = SDHCI_PLTFM_PMOPS,
17938576af1SShawn Guo 	},
18038576af1SShawn Guo 	.probe = sdhci_esdhc_probe,
18138576af1SShawn Guo 	.remove = __devexit_p(sdhci_esdhc_remove),
18238576af1SShawn Guo };
18338576af1SShawn Guo 
184d1f81a64SAxel Lin module_platform_driver(sdhci_esdhc_driver);
18538576af1SShawn Guo 
18638576af1SShawn Guo MODULE_DESCRIPTION("SDHCI OF driver for Freescale MPC eSDHC");
18738576af1SShawn Guo MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
18838576af1SShawn Guo 	      "Anton Vorontsov <avorontsov@ru.mvista.com>");
18938576af1SShawn Guo MODULE_LICENSE("GPL v2");
190