blob: 60e4186a434592bc2a42b0a365578eed4ed525f3 [file] [log] [blame]
Anton Vorontsov3085e9c2009-03-17 00:14:05 +03001/*
2 * OpenFirmware bindings for Secure Digital Host Controller Interface.
3 *
4 * Copyright (c) 2007 Freescale Semiconductor, Inc.
5 * Copyright (c) 2009 MontaVista Software, Inc.
6 *
7 * Authors: Xiaobo Xie <X.Xie@freescale.com>
8 * Anton Vorontsov <avorontsov@ru.mvista.com>
9 *
10 * This program is free software; you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation; either version 2 of the License, or (at
13 * your option) any later version.
14 */
15
Rob Herringc289ef42010-11-16 14:33:52 -060016#include <linux/err.h>
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030017#include <linux/module.h>
18#include <linux/init.h>
19#include <linux/io.h>
20#include <linux/interrupt.h>
21#include <linux/delay.h>
22#include <linux/of.h>
23#include <linux/of_platform.h>
Rob Herringc289ef42010-11-16 14:33:52 -060024#include <linux/of_address.h>
25#include <linux/of_irq.h>
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030026#include <linux/mmc/host.h>
Rob Herringc289ef42010-11-16 14:33:52 -060027#ifdef CONFIG_PPC
Anton Vorontsov8226a212009-09-22 16:45:15 -070028#include <asm/machdep.h>
Rob Herringc289ef42010-11-16 14:33:52 -060029#endif
Albert Herranz7657c3a2009-12-17 15:27:20 -080030#include "sdhci-of.h"
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030031#include "sdhci.h"
32
Albert Herranz7657c3a2009-12-17 15:27:20 -080033#ifdef CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030034
35/*
Albert Herranz7657c3a2009-12-17 15:27:20 -080036 * These accessors are designed for big endian hosts doing I/O to
37 * little endian controllers incorporating a 32-bit hardware byte swapper.
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030038 */
39
Albert Herranz7657c3a2009-12-17 15:27:20 -080040u32 sdhci_be32bs_readl(struct sdhci_host *host, int reg)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030041{
42 return in_be32(host->ioaddr + reg);
43}
44
Albert Herranz7657c3a2009-12-17 15:27:20 -080045u16 sdhci_be32bs_readw(struct sdhci_host *host, int reg)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030046{
Albert Herranz7657c3a2009-12-17 15:27:20 -080047 return in_be16(host->ioaddr + (reg ^ 0x2));
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030048}
49
Albert Herranz7657c3a2009-12-17 15:27:20 -080050u8 sdhci_be32bs_readb(struct sdhci_host *host, int reg)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030051{
52 return in_8(host->ioaddr + (reg ^ 0x3));
53}
54
Albert Herranz7657c3a2009-12-17 15:27:20 -080055void sdhci_be32bs_writel(struct sdhci_host *host, u32 val, int reg)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030056{
57 out_be32(host->ioaddr + reg, val);
58}
59
Albert Herranz7657c3a2009-12-17 15:27:20 -080060void sdhci_be32bs_writew(struct sdhci_host *host, u16 val, int reg)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030061{
62 struct sdhci_of_host *of_host = sdhci_priv(host);
63 int base = reg & ~0x3;
64 int shift = (reg & 0x2) * 8;
65
66 switch (reg) {
67 case SDHCI_TRANSFER_MODE:
68 /*
69 * Postpone this write, we must do it together with a
70 * command write that is down below.
71 */
72 of_host->xfer_mode_shadow = val;
73 return;
74 case SDHCI_COMMAND:
Albert Herranz7657c3a2009-12-17 15:27:20 -080075 sdhci_be32bs_writel(host, val << 16 | of_host->xfer_mode_shadow,
76 SDHCI_TRANSFER_MODE);
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030077 return;
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030078 }
79 clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
80}
81
Albert Herranz7657c3a2009-12-17 15:27:20 -080082void sdhci_be32bs_writeb(struct sdhci_host *host, u8 val, int reg)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030083{
84 int base = reg & ~0x3;
85 int shift = (reg & 0x3) * 8;
86
87 clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
88}
Albert Herranz7657c3a2009-12-17 15:27:20 -080089#endif /* CONFIG_MMC_SDHCI_BIG_ENDIAN_32BIT_BYTE_SWAPPER */
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030090
91#ifdef CONFIG_PM
92
Grant Likely2dc11582010-08-06 09:25:50 -060093static int sdhci_of_suspend(struct platform_device *ofdev, pm_message_t state)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030094{
95 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
96
Matt Fleming1a13f8f2010-05-26 14:42:08 -070097 return mmc_suspend_host(host->mmc);
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030098}
99
Grant Likely2dc11582010-08-06 09:25:50 -0600100static int sdhci_of_resume(struct platform_device *ofdev)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300101{
102 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
103
104 return mmc_resume_host(host->mmc);
105}
106
107#else
108
109#define sdhci_of_suspend NULL
110#define sdhci_of_resume NULL
111
112#endif
113
Anton Vorontsov8226a212009-09-22 16:45:15 -0700114static bool __devinit sdhci_of_wp_inverted(struct device_node *np)
115{
116 if (of_get_property(np, "sdhci,wp-inverted", NULL))
117 return true;
118
119 /* Old device trees don't have the wp-inverted property. */
Rob Herringc289ef42010-11-16 14:33:52 -0600120#ifdef CONFIG_PPC
Anton Vorontsov8226a212009-09-22 16:45:15 -0700121 return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
Rob Herringc289ef42010-11-16 14:33:52 -0600122#else
123 return false;
124#endif
Anton Vorontsov8226a212009-09-22 16:45:15 -0700125}
126
Grant Likelyb1608d62011-05-18 11:19:24 -0600127static const struct of_device_id sdhci_of_match[];
Grant Likely1c48a5c2011-02-17 02:43:24 -0700128static int __devinit sdhci_of_probe(struct platform_device *ofdev)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300129{
Grant Likelyb1608d62011-05-18 11:19:24 -0600130 const struct of_device_id *match;
Grant Likely61c7a082010-04-13 16:12:29 -0700131 struct device_node *np = ofdev->dev.of_node;
Grant Likely1c48a5c2011-02-17 02:43:24 -0700132 struct sdhci_of_data *sdhci_of_data;
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300133 struct sdhci_host *host;
134 struct sdhci_of_host *of_host;
Rob Herringda81c3b2010-11-16 14:33:50 -0600135 const __be32 *clk;
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300136 int size;
137 int ret;
138
Grant Likelyb1608d62011-05-18 11:19:24 -0600139 match = of_match_device(sdhci_of_match, &ofdev->dev);
140 if (!match)
Grant Likely1c48a5c2011-02-17 02:43:24 -0700141 return -EINVAL;
Grant Likelyb1608d62011-05-18 11:19:24 -0600142 sdhci_of_data = match->data;
Grant Likely1c48a5c2011-02-17 02:43:24 -0700143
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300144 if (!of_device_is_available(np))
145 return -ENODEV;
146
147 host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host));
Julia Lawall2198a642009-08-06 15:07:41 -0700148 if (IS_ERR(host))
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300149 return -ENOMEM;
150
151 of_host = sdhci_priv(host);
152 dev_set_drvdata(&ofdev->dev, host);
153
154 host->ioaddr = of_iomap(np, 0);
155 if (!host->ioaddr) {
156 ret = -ENOMEM;
157 goto err_addr_map;
158 }
159
160 host->irq = irq_of_parse_and_map(np, 0);
161 if (!host->irq) {
162 ret = -EINVAL;
163 goto err_no_irq;
164 }
165
166 host->hw_name = dev_name(&ofdev->dev);
167 if (sdhci_of_data) {
168 host->quirks = sdhci_of_data->quirks;
169 host->ops = &sdhci_of_data->ops;
170 }
171
Jerry Huangc4512f72010-08-10 18:01:59 -0700172 if (of_get_property(np, "sdhci,auto-cmd12", NULL))
173 host->quirks |= SDHCI_QUIRK_MULTIBLOCK_READ_ACMD12;
174
175
Anton Vorontsov5fe23c72009-06-18 00:14:08 +0400176 if (of_get_property(np, "sdhci,1-bit-only", NULL))
177 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
178
Anton Vorontsov8226a212009-09-22 16:45:15 -0700179 if (sdhci_of_wp_inverted(np))
180 host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
181
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300182 clk = of_get_property(np, "clock-frequency", &size);
183 if (clk && size == sizeof(*clk) && *clk)
Rob Herringda81c3b2010-11-16 14:33:50 -0600184 of_host->clock = be32_to_cpup(clk);
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300185
186 ret = sdhci_add_host(host);
187 if (ret)
188 goto err_add_host;
189
190 return 0;
191
192err_add_host:
193 irq_dispose_mapping(host->irq);
194err_no_irq:
195 iounmap(host->ioaddr);
196err_addr_map:
197 sdhci_free_host(host);
198 return ret;
199}
200
Grant Likely2dc11582010-08-06 09:25:50 -0600201static int __devexit sdhci_of_remove(struct platform_device *ofdev)
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300202{
203 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
204
205 sdhci_remove_host(host, 0);
206 sdhci_free_host(host);
207 irq_dispose_mapping(host->irq);
208 iounmap(host->ioaddr);
209 return 0;
210}
211
212static const struct of_device_id sdhci_of_match[] = {
Albert Herranz7657c3a2009-12-17 15:27:20 -0800213#ifdef CONFIG_MMC_SDHCI_OF_ESDHC
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300214 { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, },
215 { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, },
Kumar Gala992697e2009-05-08 08:52:49 -0500216 { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, },
Albert Herranz7657c3a2009-12-17 15:27:20 -0800217#endif
Albert Herranz1144ab52009-12-17 15:27:20 -0800218#ifdef CONFIG_MMC_SDHCI_OF_HLWD
219 { .compatible = "nintendo,hollywood-sdhci", .data = &sdhci_hlwd, },
220#endif
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300221 { .compatible = "generic-sdhci", },
222 {},
223};
224MODULE_DEVICE_TABLE(of, sdhci_of_match);
225
Grant Likely1c48a5c2011-02-17 02:43:24 -0700226static struct platform_driver sdhci_of_driver = {
Grant Likely40182942010-04-13 16:13:02 -0700227 .driver = {
228 .name = "sdhci-of",
229 .owner = THIS_MODULE,
230 .of_match_table = sdhci_of_match,
231 },
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300232 .probe = sdhci_of_probe,
233 .remove = __devexit_p(sdhci_of_remove),
234 .suspend = sdhci_of_suspend,
235 .resume = sdhci_of_resume,
236};
237
238static int __init sdhci_of_init(void)
239{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700240 return platform_driver_register(&sdhci_of_driver);
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300241}
242module_init(sdhci_of_init);
243
244static void __exit sdhci_of_exit(void)
245{
Grant Likely1c48a5c2011-02-17 02:43:24 -0700246 platform_driver_unregister(&sdhci_of_driver);
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300247}
248module_exit(sdhci_of_exit);
249
250MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver");
251MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
252 "Anton Vorontsov <avorontsov@ru.mvista.com>");
253MODULE_LICENSE("GPL");