blob: b6ee0d71969898257cf625b6b21cc37b3426a9f6 [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>
27#include <linux/platform_device.h>
28
29#include <linux/mmc/host.h>
30
31#include <linux/io.h>
Anton Vorontsova7626b72010-05-26 14:41:55 -070032#include <linux/sdhci-pltfm.h>
Richard Röjforsa3456a22009-06-04 13:57:29 +020033
34#include "sdhci.h"
35
36/*****************************************************************************\
37 * *
38 * SDHCI core callbacks *
39 * *
40\*****************************************************************************/
41
42static struct sdhci_ops sdhci_pltfm_ops = {
43};
44
45/*****************************************************************************\
46 * *
47 * Device probing/removal *
48 * *
49\*****************************************************************************/
50
51static int __devinit sdhci_pltfm_probe(struct platform_device *pdev)
52{
Anton Vorontsova7626b72010-05-26 14:41:55 -070053 struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
Richard Röjforsa3456a22009-06-04 13:57:29 +020054 struct sdhci_host *host;
55 struct resource *iomem;
56 int ret;
57
Richard Röjforsa3456a22009-06-04 13:57:29 +020058 iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
59 if (!iomem) {
60 ret = -ENOMEM;
61 goto err;
62 }
63
Anton Vorontsove632c452010-05-26 14:41:56 -070064 if (resource_size(iomem) < 0x100)
Richard Röjforsa3456a22009-06-04 13:57:29 +020065 dev_err(&pdev->dev, "Invalid iomem size. You may "
66 "experience problems.\n");
67
68 if (pdev->dev.parent)
69 host = sdhci_alloc_host(pdev->dev.parent, 0);
70 else
71 host = sdhci_alloc_host(&pdev->dev, 0);
72
73 if (IS_ERR(host)) {
74 ret = PTR_ERR(host);
75 goto err;
76 }
77
78 host->hw_name = "platform";
Anton Vorontsova7626b72010-05-26 14:41:55 -070079 if (pdata && pdata->ops)
80 host->ops = pdata->ops;
81 else
82 host->ops = &sdhci_pltfm_ops;
83 if (pdata)
84 host->quirks = pdata->quirks;
Richard Röjforsa3456a22009-06-04 13:57:29 +020085 host->irq = platform_get_irq(pdev, 0);
86
87 if (!request_mem_region(iomem->start, resource_size(iomem),
88 mmc_hostname(host->mmc))) {
89 dev_err(&pdev->dev, "cannot request region\n");
90 ret = -EBUSY;
91 goto err_request;
92 }
93
94 host->ioaddr = ioremap(iomem->start, resource_size(iomem));
95 if (!host->ioaddr) {
96 dev_err(&pdev->dev, "failed to remap registers\n");
97 ret = -ENOMEM;
98 goto err_remap;
99 }
100
Anton Vorontsova7626b72010-05-26 14:41:55 -0700101 if (pdata && pdata->init) {
102 ret = pdata->init(host);
103 if (ret)
104 goto err_plat_init;
105 }
106
Richard Röjforsa3456a22009-06-04 13:57:29 +0200107 ret = sdhci_add_host(host);
108 if (ret)
109 goto err_add_host;
110
111 platform_set_drvdata(pdev, host);
112
113 return 0;
114
115err_add_host:
Anton Vorontsova7626b72010-05-26 14:41:55 -0700116 if (pdata && pdata->exit)
117 pdata->exit(host);
118err_plat_init:
Richard Röjforsa3456a22009-06-04 13:57:29 +0200119 iounmap(host->ioaddr);
120err_remap:
121 release_mem_region(iomem->start, resource_size(iomem));
122err_request:
123 sdhci_free_host(host);
124err:
125 printk(KERN_ERR"Probing of sdhci-pltfm failed: %d\n", ret);
126 return ret;
127}
128
129static int __devexit sdhci_pltfm_remove(struct platform_device *pdev)
130{
Anton Vorontsova7626b72010-05-26 14:41:55 -0700131 struct sdhci_pltfm_data *pdata = pdev->dev.platform_data;
Richard Röjforsa3456a22009-06-04 13:57:29 +0200132 struct sdhci_host *host = platform_get_drvdata(pdev);
133 struct resource *iomem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
134 int dead;
135 u32 scratch;
136
137 dead = 0;
138 scratch = readl(host->ioaddr + SDHCI_INT_STATUS);
139 if (scratch == (u32)-1)
140 dead = 1;
141
142 sdhci_remove_host(host, dead);
Anton Vorontsova7626b72010-05-26 14:41:55 -0700143 if (pdata && pdata->exit)
144 pdata->exit(host);
Richard Röjforsa3456a22009-06-04 13:57:29 +0200145 iounmap(host->ioaddr);
146 release_mem_region(iomem->start, resource_size(iomem));
147 sdhci_free_host(host);
148 platform_set_drvdata(pdev, NULL);
149
150 return 0;
151}
152
153static struct platform_driver sdhci_pltfm_driver = {
154 .driver = {
155 .name = "sdhci",
156 .owner = THIS_MODULE,
157 },
158 .probe = sdhci_pltfm_probe,
159 .remove = __devexit_p(sdhci_pltfm_remove),
160};
161
162/*****************************************************************************\
163 * *
164 * Driver init/exit *
165 * *
166\*****************************************************************************/
167
168static int __init sdhci_drv_init(void)
169{
170 return platform_driver_register(&sdhci_pltfm_driver);
171}
172
173static void __exit sdhci_drv_exit(void)
174{
175 platform_driver_unregister(&sdhci_pltfm_driver);
176}
177
178module_init(sdhci_drv_init);
179module_exit(sdhci_drv_exit);
180
181MODULE_DESCRIPTION("Secure Digital Host Controller Interface platform driver");
182MODULE_AUTHOR("Mocean Laboratories <info@mocean-labs.com>");
183MODULE_LICENSE("GPL v2");
184MODULE_ALIAS("platform:sdhci");