mmc: sdhci: make sdhci-of device drivers self registered
[cascardo/linux.git] / drivers / mmc / host / sdhci-pltfm.c
1 /*
2  * sdhci-pltfm.c Support for SDHCI platform devices
3  * Copyright (c) 2009 Intel Corporation
4  *
5  * 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  *
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.
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
31 #include <linux/err.h>
32 #include <linux/of.h>
33 #ifdef CONFIG_PPC
34 #include <asm/machdep.h>
35 #endif
36 #include "sdhci.h"
37 #include "sdhci-pltfm.h"
38
39 static struct sdhci_ops sdhci_pltfm_ops = {
40 };
41
42 #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  */
47 u32 sdhci_be32bs_readl(struct sdhci_host *host, int reg)
48 {
49         return in_be32(host->ioaddr + reg);
50 }
51
52 u16 sdhci_be32bs_readw(struct sdhci_host *host, int reg)
53 {
54         return in_be16(host->ioaddr + (reg ^ 0x2));
55 }
56
57 u8 sdhci_be32bs_readb(struct sdhci_host *host, int reg)
58 {
59         return in_8(host->ioaddr + (reg ^ 0x3));
60 }
61
62 void sdhci_be32bs_writel(struct sdhci_host *host, u32 val, int reg)
63 {
64         out_be32(host->ioaddr + reg, val);
65 }
66
67 void 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
90 void 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
100 static 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
113 void 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
137 void sdhci_get_of_property(struct platform_device *pdev) {}
138 #endif /* CONFIG_OF */
139
140 struct sdhci_host *sdhci_pltfm_init(struct platform_device *pdev,
141                                     struct sdhci_pltfm_data *pdata)
142 {
143         struct sdhci_host *host;
144         struct sdhci_pltfm_host *pltfm_host;
145         struct resource *iomem;
146         int ret;
147
148         iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
149         if (!iomem) {
150                 ret = -ENOMEM;
151                 goto err;
152         }
153
154         if (resource_size(iomem) < 0x100)
155                 dev_err(&pdev->dev, "Invalid iomem size!\n");
156
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));
160         else
161                 host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
162
163         if (IS_ERR(host)) {
164                 ret = PTR_ERR(host);
165                 goto err;
166         }
167
168         pltfm_host = sdhci_priv(host);
169
170         host->hw_name = dev_name(&pdev->dev);
171         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;
177         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
193         platform_set_drvdata(pdev, host);
194
195         return host;
196
197 err_remap:
198         release_mem_region(iomem->start, resource_size(iomem));
199 err_request:
200         sdhci_free_host(host);
201 err:
202         dev_err(&pdev->dev, "%s failed %d\n", __func__, ret);
203         return ERR_PTR(ret);
204 }
205
206 void sdhci_pltfm_free(struct platform_device *pdev)
207 {
208         struct sdhci_host *host = platform_get_drvdata(pdev);
209         struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
210
211         iounmap(host->ioaddr);
212         release_mem_region(iomem->start, resource_size(iomem));
213         sdhci_free_host(host);
214         platform_set_drvdata(pdev, NULL);
215 }
216
217 int 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
227         sdhci_get_of_property(pdev);
228
229         ret = sdhci_add_host(host);
230         if (ret)
231                 sdhci_pltfm_free(pdev);
232
233         return ret;
234 }
235
236 int 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);
243
244         return 0;
245 }
246
247 #ifdef CONFIG_PM
248 int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
249 {
250         struct sdhci_host *host = platform_get_drvdata(dev);
251
252         return sdhci_suspend_host(host, state);
253 }
254
255 int sdhci_pltfm_resume(struct platform_device *dev)
256 {
257         struct sdhci_host *host = platform_get_drvdata(dev);
258
259         return sdhci_resume_host(host);
260 }
261 #endif  /* CONFIG_PM */