blob: 041b0e2a898e6450306c764f7795a732a3000f2a [file] [log] [blame]
Richard Röjforsa3456a22009-06-04 13:57:29 +02001/*
2 * sdhci-pltfm.c Support for SDHCI platform devices
3 * Copyright (c) 2009 Intel Corporation
4 *
Shawn Guo38576af2011-05-27 23:48:14 +08005 * Copyright (c) 2007 Freescale Semiconductor, Inc.
6 * Copyright (c) 2009 MontaVista Software, Inc.
7 *
8 * Authors: Xiaobo Xie <X.Xie@freescale.com>
9 * Anton Vorontsov <avorontsov@ru.mvista.com>
10 *
Richard Röjforsa3456a22009-06-04 13:57:29 +020011 * 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.
14 *
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.
19 *
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.
23 */
24
25/* Supports:
26 * SDHCI platform devices
27 *
28 * Inspired by sdhci-pci.c, by Pierre Ossman
29 */
30
Shawn Guo85d65092011-05-27 23:48:12 +080031#include <linux/err.h>
Shawn Guo38576af2011-05-27 23:48:14 +080032#include <linux/of.h>
33#ifdef CONFIG_PPC
34#include <asm/machdep.h>
35#endif
Richard Röjforsa3456a22009-06-04 13:57:29 +020036#include "sdhci.h"
Anton Vorontsov515033f2010-08-10 18:01:47 -070037#include "sdhci-pltfm.h"
Richard Röjforsa3456a22009-06-04 13:57:29 +020038
Richard Röjforsa3456a22009-06-04 13:57:29 +020039static struct sdhci_ops sdhci_pltfm_ops = {
40};
41
Shawn Guo38576af2011-05-27 23:48:14 +080042#ifdef CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER
43/*
44 * These accessors are designed for big endian hosts doing I/O to
45 * little endian controllers incorporating a 32-bit hardware byte swapper.
46 */
47u32 sdhci_be32bs_readl(struct sdhci_host *host, int reg)
48{
49 return in_be32(host->ioaddr + reg);
50}
51
52u16 sdhci_be32bs_readw(struct sdhci_host *host, int reg)
53{
54 return in_be16(host->ioaddr + (reg ^ 0x2));
55}
56
57u8 sdhci_be32bs_readb(struct sdhci_host *host, int reg)
58{
59 return in_8(host->ioaddr + (reg ^ 0x3));
60}
61
62void sdhci_be32bs_writel(struct sdhci_host *host, u32 val, int reg)
63{
64 out_be32(host->ioaddr + reg, val);
65}
66
67void sdhci_be32bs_writew(struct sdhci_host *host, u16 val, int reg)
68{
69 struct sdhci_pltfm_host *pltfm_host = sdhci_priv(host);
70 int base = reg & ~0x3;
71 int shift = (reg & 0x2) * 8;
72
73 switch (reg) {
74 case SDHCI_TRANSFER_MODE:
75 /*
76 * Postpone this write, we must do it together with a
77 * command write that is down below.
78 */
79 pltfm_host->xfer_mode_shadow = val;
80 return;
81 case SDHCI_COMMAND:
82 sdhci_be32bs_writel(host,
83 val << 16 | pltfm_host->xfer_mode_shadow,
84 SDHCI_TRANSFER_MODE);
85 return;
86 }
87 clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
88}
89
90void sdhci_be32bs_writeb(struct sdhci_host *host, u8 val, int reg)
91{
92 int base = reg & ~0x3;
93 int shift = (reg & 0x3) * 8;
94
95 clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
96}
97#endif /* CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER */
98
99#ifdef CONFIG_OF
100static bool sdhci_of_wp_inverted(struct device_node *np)
101{
102 if (of_get_property(np, "sdhci,wp-inverted", NULL))
103 return true;
104
105 /* Old device trees don't have the wp-inverted property. */
106#ifdef CONFIG_PPC
107 return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
108#else
109 return false;
110#endif /* CONFIG_PPC */
111}
112
113void sdhci_get_of_property(struct platform_device *pdev)
114{
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);
118 const __be32 *clk;
119 int size;
120
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;
124
125 if (of_get_property(np, "sdhci,1-bit-only", NULL))
126 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
127
128 if (sdhci_of_wp_inverted(np))
129 host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
130
131 clk = of_get_property(np, "clock-frequency", &size);
132 if (clk && size == sizeof(*clk) && *clk)
133 pltfm_host->clock = be32_to_cpup(clk);
134 }
135}
136#else
137void sdhci_get_of_property(struct platform_device *pdev) {}
138#endif /* CONFIG_OF */
139
Shawn Guo85d65092011-05-27 23:48:12 +0800140struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
141 struct sdhci_pltfm_data *pdata)
Richard Röjforsa3456a22009-06-04 13:57:29 +0200142{
143 struct sdhci_host *host;
Wolfram Sang4b711cb2010-10-15 12:20:59 +0200144 struct sdhci_pltfm_host *pltfm_host;
Richard Röjforsa3456a22009-06-04 13:57:29 +0200145 struct resource *iomem;
146 int ret;
147
Richard Röjforsa3456a22009-06-04 13:57:29 +0200148 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149 if (!iomem) {
150 ret = -ENOMEM;
151 goto err;
152 }
153
Anton Vorontsove632c452010-05-26 14:41:56 -0700154 if (resource_size(iomem) < 0x100)
Shawn Guo85d65092011-05-27 23:48:12 +0800155 dev_err(&pdev->dev, "Invalid iomem size!\n");
Richard Röjforsa3456a22009-06-04 13:57:29 +0200156
Wolfram Sang4b711cb2010-10-15 12:20:59 +0200157 /* 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));
Richard Röjforsa3456a22009-06-04 13:57:29 +0200160 else
Wolfram Sang4b711cb2010-10-15 12:20:59 +0200161 host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
Richard Röjforsa3456a22009-06-04 13:57:29 +0200162
163 if (IS_ERR(host)) {
164 ret = PTR_ERR(host);
165 goto err;
166 }
167
Wolfram Sang4b711cb2010-10-15 12:20:59 +0200168 pltfm_host = sdhci_priv(host);
169
Shawn Guo85d65092011-05-27 23:48:12 +0800170 host->hw_name = dev_name(&pdev->dev);
Anton Vorontsova7626b72010-05-26 14:41:55 -0700171 if (pdata && pdata->ops)
172 host->ops = pdata->ops;
173 else
174 host->ops = &sdhci_pltfm_ops;
175 if (pdata)
176 host->quirks = pdata->quirks;
Richard Röjforsa3456a22009-06-04 13:57:29 +0200177 host->irq = platform_get_irq(pdev, 0);
178
179 if (!request_mem_region(iomem->start, resource_size(iomem),
180 mmc_hostname(host->mmc))) {
181 dev_err(&pdev->dev, "cannot request region\n");
182 ret = -EBUSY;
183 goto err_request;
184 }
185
186 host->ioaddr = ioremap(iomem->start, resource_size(iomem));
187 if (!host->ioaddr) {
188 dev_err(&pdev->dev, "failed to remap registers\n");
189 ret = -ENOMEM;
190 goto err_remap;
191 }
192
Richard Röjforsa3456a22009-06-04 13:57:29 +0200193 platform_set_drvdata(pdev, host);
194
Shawn Guo85d65092011-05-27 23:48:12 +0800195 return host;
Richard Röjforsa3456a22009-06-04 13:57:29 +0200196
Richard Röjforsa3456a22009-06-04 13:57:29 +0200197err_remap:
198 release_mem_region(iomem->start, resource_size(iomem));
199err_request:
200 sdhci_free_host(host);
201err:
Shawn Guo85d65092011-05-27 23:48:12 +0800202 dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
203 return ERR_PTR(ret);
Richard Röjforsa3456a22009-06-04 13:57:29 +0200204}
205
Shawn Guo85d65092011-05-27 23:48:12 +0800206void sdhci_pltfm_free(struct platform_device *pdev)
Richard Röjforsa3456a22009-06-04 13:57:29 +0200207{
208 struct sdhci_host *host = platform_get_drvdata(pdev);
209 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
Richard Röjforsa3456a22009-06-04 13:57:29 +0200210
Richard Röjforsa3456a22009-06-04 13:57:29 +0200211 iounmap(host->ioaddr);
212 release_mem_region(iomem->start, resource_size(iomem));
213 sdhci_free_host(host);
214 platform_set_drvdata(pdev, NULL);
Shawn Guo85d65092011-05-27 23:48:12 +0800215}
216
217int sdhci_pltfm_register(struct platform_device *pdev,
218 struct sdhci_pltfm_data *pdata)
219{
220 struct sdhci_host *host;
221 int ret = 0;
222
223 host = sdhci_pltfm_init(pdev, pdata);
224 if (IS_ERR(host))
225 return PTR_ERR(host);
226
Shawn Guo38576af2011-05-27 23:48:14 +0800227 sdhci_get_of_property(pdev);
228
Shawn Guo85d65092011-05-27 23:48:12 +0800229 ret = sdhci_add_host(host);
230 if (ret)
231 sdhci_pltfm_free(pdev);
232
233 return ret;
234}
235
236int sdhci_pltfm_unregister(struct platform_device *pdev)
237{
238 struct sdhci_host *host = platform_get_drvdata(pdev);
239 int dead = (readl(host->ioaddr + SDHCI_INT_STATUS) == 0xffffffff);
240
241 sdhci_remove_host(host, dead);
242 sdhci_pltfm_free(pdev);
Richard Röjforsa3456a22009-06-04 13:57:29 +0200243
244 return 0;
245}
246
Giuseppe Cavallarobe8ae092010-09-28 10:41:27 +0200247#ifdef CONFIG_PM
Shawn Guo85d65092011-05-27 23:48:12 +0800248int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
Giuseppe Cavallarobe8ae092010-09-28 10:41:27 +0200249{
250 struct sdhci_host *host = platform_get_drvdata(dev);
251
252 return sdhci_suspend_host(host, state);
253}
254
Shawn Guo85d65092011-05-27 23:48:12 +0800255int sdhci_pltfm_resume(struct platform_device *dev)
Giuseppe Cavallarobe8ae092010-09-28 10:41:27 +0200256{
257 struct sdhci_host *host = platform_get_drvdata(dev);
258
259 return sdhci_resume_host(host);
260}
Giuseppe Cavallarobe8ae092010-09-28 10:41:27 +0200261#endif /* CONFIG_PM */