2 * sdhci-pltfm.c Support for SDHCI platform devices
3 * Copyright (c) 2009 Intel Corporation
5 * Copyright (c) 2007 Freescale Semiconductor, Inc.
6 * Copyright (c) 2009 MontaVista Software, Inc.
8 * Authors: Xiaobo Xie <X.Xie@freescale.com>
9 * Anton Vorontsov <avorontsov@ru.mvista.com>
11 * This program is free software; you can redistribute it and/or modify
12 * it under the terms of the GNU General Public License version 2 as
13 * published by the Free Software Foundation.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
26 * SDHCI platform devices
28 * Inspired by sdhci-pci.c, by Pierre Ossman
31 #include <linux/err.h>
34 #include <asm/machdep.h>
37 #include "sdhci-pltfm.h"
39 static struct sdhci_ops sdhci_pltfm_ops = {
42 #ifdef CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER
44 * These accessors are designed for big endian hosts doing I/O to
45 * little endian controllers incorporating a 32-bit hardware byte swapper.
47 u32 sdhci_be32bs_readl(struct sdhci_host *host, int reg)
49 return in_be32(host->ioaddr + reg);
52 u16 sdhci_be32bs_readw(struct sdhci_host *host, int reg)
54 return in_be16(host->ioaddr + (reg ^ 0x2));
57 u8 sdhci_be32bs_readb(struct sdhci_host *host, int reg)
59 return in_8(host->ioaddr + (reg ^ 0x3));
62 void sdhci_be32bs_writel(struct sdhci_host *host, u32 val, int reg)
64 out_be32(host->ioaddr + reg, val);
67 void sdhci_be32bs_writew(struct sdhci_host *host, u16 val, int reg)
69 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
70 int base = reg & ~0x3;
71 int shift = (reg & 0x2) * 8;
74 case SDHCI_TRANSFER_MODE:
76 * Postpone this write, we must do it together with a
77 * command write that is down below.
79 pltfm_host->xfer_mode_shadow = val;
82 sdhci_be32bs_writel(host,
83 val << 16 | pltfm_host->xfer_mode_shadow,
87 clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
90 void sdhci_be32bs_writeb(struct sdhci_host *host, u8 val, int reg)
92 int base = reg & ~0x3;
93 int shift = (reg & 0x3) * 8;
95 clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
97 #endif /* CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER */
100 static bool sdhci_of_wp_inverted(struct device_node *np)
102 if (of_get_property(np, "sdhci,wp-inverted", NULL))
105 /* Old device trees don't have the wp-inverted property. */
107 return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
110 #endif /* CONFIG_PPC */
113 void sdhci_get_of_property(struct platform_device *pdev)
115 struct device_node *np = pdev->dev.of_node;
116 struct sdhci_host *host = platform_get_drvdata(pdev);
117 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
121 if (of_device_is_available(np)) {
122 if (of_get_property(np, "sdhci,auto-cmd12", NULL))
123 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
125 if (of_get_property(np, "sdhci,1-bit-only", NULL))
126 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
128 if (sdhci_of_wp_inverted(np))
129 host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
131 clk = of_get_property(np, "clock-frequency", &size);
132 if (clk && size == sizeof(*clk) && *clk)
133 pltfm_host->clock = be32_to_cpup(clk);
137 void sdhci_get_of_property(struct platform_device *pdev) {}
138 #endif /* CONFIG_OF */
140 struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
141 struct sdhci_pltfm_data *pdata)
143 struct sdhci_host *host;
144 struct sdhci_pltfm_host *pltfm_host;
145 struct resource *iomem;
148 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
154 if (resource_size(iomem) < 0x100)
155 dev_err(&pdev->dev, "Invalid iomem size!\n");
157 /* Some PCI-based MFD need the parent here */
158 if (pdev->dev.parent != &platform_bus)
159 host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
161 host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
168 pltfm_host = sdhci_priv(host);
170 host->hw_name = dev_name(&pdev->dev);
171 if (pdata && pdata->ops)
172 host->ops = pdata->ops;
174 host->ops = &sdhci_pltfm_ops;
176 host->quirks = pdata->quirks;
177 host->irq = platform_get_irq(pdev, 0);
179 if (!request_mem_region(iomem->start, resource_size(iomem),
180 mmc_hostname(host->mmc))) {
181 dev_err(&pdev->dev, "cannot request region\n");
186 host->ioaddr = ioremap(iomem->start, resource_size(iomem));
188 dev_err(&pdev->dev, "failed to remap registers\n");
193 platform_set_drvdata(pdev, host);
198 release_mem_region(iomem->start, resource_size(iomem));
200 sdhci_free_host(host);
202 dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
206 void sdhci_pltfm_free(struct platform_device *pdev)
208 struct sdhci_host *host = platform_get_drvdata(pdev);
209 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
211 iounmap(host->ioaddr);
212 release_mem_region(iomem->start, resource_size(iomem));
213 sdhci_free_host(host);
214 platform_set_drvdata(pdev, NULL);
217 int sdhci_pltfm_register(struct platform_device *pdev,
218 struct sdhci_pltfm_data *pdata)
220 struct sdhci_host *host;
223 host = sdhci_pltfm_init(pdev, pdata);
225 return PTR_ERR(host);
227 sdhci_get_of_property(pdev);
229 ret = sdhci_add_host(host);
231 sdhci_pltfm_free(pdev);
236 int sdhci_pltfm_unregister(struct platform_device *pdev)
238 struct sdhci_host *host = platform_get_drvdata(pdev);
239 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
241 sdhci_remove_host(host, dead);
242 sdhci_pltfm_free(pdev);
248 int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
250 struct sdhci_host *host = platform_get_drvdata(dev);
252 return sdhci_suspend_host(host, state);
255 int sdhci_pltfm_resume(struct platform_device *dev)
257 struct sdhci_host *host = platform_get_drvdata(dev);
259 return sdhci_resume_host(host);
261 #endif /* CONFIG_PM */