blob: 730fdf54ecb0977f018e933cdaa282ab96dfee0b [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 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License version 2 as
7 * published by the Free Software Foundation.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
17 */
18
19/* Supports:
20 * SDHCI platform devices
21 *
22 * Inspired by sdhci-pci.c, by Pierre Ossman
23 */
24
25#include <linux/delay.h>
26#include <linux/highmem.h>
Anton Vorontsov515033f2010-08-10 18:01:47 -070027#include <linux/mod_devicetable.h>
Richard Röjforsa3456a22009-06-04 13:57:29 +020028#include <linux/platform_device.h>
29
30#include <linux/mmc/host.h>
31
32#include <linux/io.h>
Anton Vorontsova7626b72010-05-26 14:41:55 -070033#include <linux/sdhci-pltfm.h>
Richard Röjforsa3456a22009-06-04 13:57:29 +020034
35#include "sdhci.h"
Anton Vorontsov515033f2010-08-10 18:01:47 -070036#include "sdhci-pltfm.h"
Richard Röjforsa3456a22009-06-04 13:57:29 +020037
38/*****************************************************************************\
39 * *
40 * SDHCI core callbacks *
41 * *
42\*****************************************************************************/
43
44static struct sdhci_ops sdhci_pltfm_ops = {
45};
46
47/*****************************************************************************\
48 * *
49 * Device probing/removal *
50 * *
51\*****************************************************************************/
52
53static int __devinit sdhci_pltfm_probe(struct platform_device *pdev)
54{
Anton Vorontsova7626b72010-05-26 14:41:55 -070055 struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
Anton Vorontsov515033f2010-08-10 18:01:47 -070056 const struct platform_device_id *platid = platform_get_device_id(pdev);
Richard Röjforsa3456a22009-06-04 13:57:29 +020057 struct sdhci_host *host;
Wolfram Sang4b711cb2010-10-15 12:20:59 +020058 struct sdhci_pltfm_host *pltfm_host;
Richard Röjforsa3456a22009-06-04 13:57:29 +020059 struct resource *iomem;
60 int ret;
61
Anton Vorontsov515033f2010-08-10 18:01:47 -070062 if (!pdata && platid && platid->driver_data)
63 pdata = (void *)platid->driver_data;
64
Richard Röjforsa3456a22009-06-04 13:57:29 +020065 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
66 if (!iomem) {
67 ret = -ENOMEM;
68 goto err;
69 }
70
Anton Vorontsove632c452010-05-26 14:41:56 -070071 if (resource_size(iomem) < 0x100)
Richard Röjforsa3456a22009-06-04 13:57:29 +020072 dev_err(&pdev->dev, "Invalid iomem size. You may "
73 "experience problems.\n");
74
Wolfram Sang4b711cb2010-10-15 12:20:59 +020075 /* Some PCI-based MFD need the parent here */
76 if (pdev->dev.parent != &platform_bus)
77 host = sdhci_alloc_host(pdev->dev.parent, sizeof(*pltfm_host));
Richard Röjforsa3456a22009-06-04 13:57:29 +020078 else
Wolfram Sang4b711cb2010-10-15 12:20:59 +020079 host = sdhci_alloc_host(&pdev->dev, sizeof(*pltfm_host));
Richard Röjforsa3456a22009-06-04 13:57:29 +020080
81 if (IS_ERR(host)) {
82 ret = PTR_ERR(host);
83 goto err;
84 }
85
Wolfram Sang4b711cb2010-10-15 12:20:59 +020086 pltfm_host = sdhci_priv(host);
87
Richard Röjforsa3456a22009-06-04 13:57:29 +020088 host->hw_name = "platform";
Anton Vorontsova7626b72010-05-26 14:41:55 -070089 if (pdata && pdata->ops)
90 host->ops = pdata->ops;
91 else
92 host->ops = &sdhci_pltfm_ops;
93 if (pdata)
94 host->quirks = pdata->quirks;
Richard Röjforsa3456a22009-06-04 13:57:29 +020095 host->irq = platform_get_irq(pdev, 0);
96
97 if (!request_mem_region(iomem->start, resource_size(iomem),
98 mmc_hostname(host->mmc))) {
99 dev_err(&pdev->dev, "cannot request region\n");
100 ret = -EBUSY;
101 goto err_request;
102 }
103
104 host->ioaddr = ioremap(iomem->start, resource_size(iomem));
105 if (!host->ioaddr) {
106 dev_err(&pdev->dev, "failed to remap registers\n");
107 ret = -ENOMEM;
108 goto err_remap;
109 }
110
Anton Vorontsova7626b72010-05-26 14:41:55 -0700111 if (pdata && pdata->init) {
112 ret = pdata->init(host);
113 if (ret)
114 goto err_plat_init;
115 }
116
Richard Röjforsa3456a22009-06-04 13:57:29 +0200117 ret = sdhci_add_host(host);
118 if (ret)
119 goto err_add_host;
120
121 platform_set_drvdata(pdev, host);
122
123 return 0;
124
125err_add_host:
Anton Vorontsova7626b72010-05-26 14:41:55 -0700126 if (pdata && pdata->exit)
127 pdata->exit(host);
128err_plat_init:
Richard Röjforsa3456a22009-06-04 13:57:29 +0200129 iounmap(host->ioaddr);
130err_remap:
131 release_mem_region(iomem->start, resource_size(iomem));
132err_request:
133 sdhci_free_host(host);
134err:
135 printk(KERN_ERR"Probing of sdhci-pltfm failed: %d\n", ret);
136 return ret;
137}
138
139static int __devexit sdhci_pltfm_remove(struct platform_device *pdev)
140{
Anton Vorontsova7626b72010-05-26 14:41:55 -0700141 struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
Richard Röjforsa3456a22009-06-04 13:57:29 +0200142 struct sdhci_host *host = platform_get_drvdata(pdev);
143 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
144 int dead;
145 u32 scratch;
146
147 dead = 0;
148 scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
149 if (scratch == (u32)-1)
150 dead = 1;
151
152 sdhci_remove_host(host, dead);
Anton Vorontsova7626b72010-05-26 14:41:55 -0700153 if (pdata && pdata->exit)
154 pdata->exit(host);
Richard Röjforsa3456a22009-06-04 13:57:29 +0200155 iounmap(host->ioaddr);
156 release_mem_region(iomem->start, resource_size(iomem));
157 sdhci_free_host(host);
158 platform_set_drvdata(pdev, NULL);
159
160 return 0;
161}
162
Anton Vorontsov515033f2010-08-10 18:01:47 -0700163static const struct platform_device_id sdhci_pltfm_ids[] = {
164 { "sdhci", },
Anton Vorontsov20b1597b2010-08-10 18:01:49 -0700165#ifdef CONFIG_MMC_SDHCI_CNS3XXX
166 { "sdhci-cns3xxx", (kernel_ulong_t)&sdhci_cns3xxx_pdata },
167#endif
Anton Vorontsov515033f2010-08-10 18:01:47 -0700168 { },
169};
170MODULE_DEVICE_TABLE(platform, sdhci_pltfm_ids);
171
Giuseppe Cavallarobe8ae092010-09-28 10:41:27 +0200172#ifdef CONFIG_PM
173static int sdhci_pltfm_suspend(struct platform_device *dev, pm_message_t state)
174{
175 struct sdhci_host *host = platform_get_drvdata(dev);
176
177 return sdhci_suspend_host(host, state);
178}
179
180static int sdhci_pltfm_resume(struct platform_device *dev)
181{
182 struct sdhci_host *host = platform_get_drvdata(dev);
183
184 return sdhci_resume_host(host);
185}
186#else
187#define sdhci_pltfm_suspend NULL
188#define sdhci_pltfm_resume NULL
189#endif /* CONFIG_PM */
190
Richard Röjforsa3456a22009-06-04 13:57:29 +0200191static struct platform_driver sdhci_pltfm_driver = {
192 .driver = {
193 .name = "sdhci",
194 .owner = THIS_MODULE,
195 },
196 .probe = sdhci_pltfm_probe,
197 .remove = __devexit_p(sdhci_pltfm_remove),
Anton Vorontsov515033f2010-08-10 18:01:47 -0700198 .id_table = sdhci_pltfm_ids,
Giuseppe Cavallarobe8ae092010-09-28 10:41:27 +0200199 .suspend = sdhci_pltfm_suspend,
200 .resume = sdhci_pltfm_resume,
Richard Röjforsa3456a22009-06-04 13:57:29 +0200201};
202
203/*****************************************************************************\
204 * *
205 * Driver init/exit *
206 * *
207\*****************************************************************************/
208
209static int __init sdhci_drv_init(void)
210{
211 return platform_driver_register(&sdhci_pltfm_driver);
212}
213
214static void __exit sdhci_drv_exit(void)
215{
216 platform_driver_unregister(&sdhci_pltfm_driver);
217}
218
219module_init(sdhci_drv_init);
220module_exit(sdhci_drv_exit);
221
222MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
223MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
224MODULE_LICENSE("GPL v2");