blob: 3439fc1189042d1f4a4ee23d8cf2f72fb551fd67 [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
16#include <linux/module.h>
17#include <linux/init.h>
18#include <linux/io.h>
19#include <linux/interrupt.h>
20#include <linux/delay.h>
21#include <linux/of.h>
22#include <linux/of_platform.h>
23#include <linux/mmc/host.h>
24#include "sdhci.h"
25
26struct sdhci_of_data {
27 unsigned int quirks;
28 struct sdhci_ops ops;
29};
30
31struct sdhci_of_host {
32 unsigned int clock;
33 u16 xfer_mode_shadow;
34};
35
36/*
37 * Ops and quirks for the Freescale eSDHC controller.
38 */
39
40#define ESDHC_DMA_SYSCTL 0x40c
41#define ESDHC_DMA_SNOOP 0x00000040
42
43#define ESDHC_SYSTEM_CONTROL 0x2c
44#define ESDHC_CLOCK_MASK 0x0000fff0
45#define ESDHC_PREDIV_SHIFT 8
46#define ESDHC_DIVIDER_SHIFT 4
47#define ESDHC_CLOCK_PEREN 0x00000004
48#define ESDHC_CLOCK_HCKEN 0x00000002
49#define ESDHC_CLOCK_IPGEN 0x00000001
50
Anton Vorontsovc0859262009-09-22 16:45:11 -070051#define ESDHC_HOST_CONTROL_RES 0x05
52
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030053static u32 esdhc_readl(struct sdhci_host *host, int reg)
54{
55 return in_be32(host->ioaddr + reg);
56}
57
58static u16 esdhc_readw(struct sdhci_host *host, int reg)
59{
Dave Liufbf6a5f2009-05-06 18:40:07 +080060 u16 ret;
61
62 if (unlikely(reg == SDHCI_HOST_VERSION))
63 ret = in_be16(host->ioaddr + reg);
64 else
65 ret = in_be16(host->ioaddr + (reg ^ 0x2));
66 return ret;
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030067}
68
69static u8 esdhc_readb(struct sdhci_host *host, int reg)
70{
71 return in_8(host->ioaddr + (reg ^ 0x3));
72}
73
74static void esdhc_writel(struct sdhci_host *host, u32 val, int reg)
75{
76 out_be32(host->ioaddr + reg, val);
77}
78
79static void esdhc_writew(struct sdhci_host *host, u16 val, int reg)
80{
81 struct sdhci_of_host *of_host = sdhci_priv(host);
82 int base = reg & ~0x3;
83 int shift = (reg & 0x2) * 8;
84
85 switch (reg) {
86 case SDHCI_TRANSFER_MODE:
87 /*
88 * Postpone this write, we must do it together with a
89 * command write that is down below.
90 */
91 of_host->xfer_mode_shadow = val;
92 return;
93 case SDHCI_COMMAND:
94 esdhc_writel(host, val << 16 | of_host->xfer_mode_shadow,
95 SDHCI_TRANSFER_MODE);
96 return;
97 case SDHCI_BLOCK_SIZE:
98 /*
99 * Two last DMA bits are reserved, and first one is used for
100 * non-standard blksz of 4096 bytes that we don't support
101 * yet. So clear the DMA boundary bits.
102 */
103 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
104 /* fall through */
105 }
106 clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
107}
108
109static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg)
110{
111 int base = reg & ~0x3;
112 int shift = (reg & 0x3) * 8;
113
Anton Vorontsovc0859262009-09-22 16:45:11 -0700114 /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */
115 if (reg == SDHCI_HOST_CONTROL)
116 val &= ~ESDHC_HOST_CONTROL_RES;
117
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300118 clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
119}
120
121static void esdhc_set_clock(struct sdhci_host *host, unsigned int clock)
122{
123 int div;
124 int pre_div = 2;
125
126 clrbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN |
127 ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
128
129 if (clock == 0)
130 goto out;
131
132 if (host->max_clk / 16 > clock) {
133 for (; pre_div < 256; pre_div *= 2) {
134 if (host->max_clk / pre_div < clock * 16)
135 break;
136 }
137 }
138
139 for (div = 1; div <= 16; div++) {
140 if (host->max_clk / (div * pre_div) <= clock)
141 break;
142 }
143
144 pre_div >>= 1;
Anton Vorontsov1e5df752009-09-22 16:45:10 -0700145 div--;
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300146
147 setbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN |
148 ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN |
149 div << ESDHC_DIVIDER_SHIFT | pre_div << ESDHC_PREDIV_SHIFT);
150 mdelay(100);
151out:
152 host->clock = clock;
153}
154
155static int esdhc_enable_dma(struct sdhci_host *host)
156{
157 setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP);
158 return 0;
159}
160
161static unsigned int esdhc_get_max_clock(struct sdhci_host *host)
162{
163 struct sdhci_of_host *of_host = sdhci_priv(host);
164
165 return of_host->clock;
166}
167
Anton Vorontsova9e58f22009-07-29 15:04:16 -0700168static unsigned int esdhc_get_min_clock(struct sdhci_host *host)
169{
170 struct sdhci_of_host *of_host = sdhci_priv(host);
171
172 return of_host->clock / 256 / 16;
173}
174
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300175static unsigned int esdhc_get_timeout_clock(struct sdhci_host *host)
176{
177 struct sdhci_of_host *of_host = sdhci_priv(host);
178
179 return of_host->clock / 1000;
180}
181
182static struct sdhci_of_data sdhci_esdhc = {
183 .quirks = SDHCI_QUIRK_FORCE_BLK_SZ_2048 |
184 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
185 SDHCI_QUIRK_INVERTED_WRITE_PROTECT |
186 SDHCI_QUIRK_NO_BUSY_IRQ |
187 SDHCI_QUIRK_NONSTANDARD_CLOCK |
188 SDHCI_QUIRK_PIO_NEEDS_DELAY |
189 SDHCI_QUIRK_RESTORE_IRQS_AFTER_RESET |
190 SDHCI_QUIRK_NO_CARD_NO_RESET,
191 .ops = {
192 .readl = esdhc_readl,
193 .readw = esdhc_readw,
194 .readb = esdhc_readb,
195 .writel = esdhc_writel,
196 .writew = esdhc_writew,
197 .writeb = esdhc_writeb,
198 .set_clock = esdhc_set_clock,
199 .enable_dma = esdhc_enable_dma,
200 .get_max_clock = esdhc_get_max_clock,
Anton Vorontsova9e58f22009-07-29 15:04:16 -0700201 .get_min_clock = esdhc_get_min_clock,
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300202 .get_timeout_clock = esdhc_get_timeout_clock,
203 },
204};
205
206#ifdef CONFIG_PM
207
208static int sdhci_of_suspend(struct of_device *ofdev, pm_message_t state)
209{
210 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
211
212 return mmc_suspend_host(host->mmc, state);
213}
214
215static int sdhci_of_resume(struct of_device *ofdev)
216{
217 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
218
219 return mmc_resume_host(host->mmc);
220}
221
222#else
223
224#define sdhci_of_suspend NULL
225#define sdhci_of_resume NULL
226
227#endif
228
229static int __devinit sdhci_of_probe(struct of_device *ofdev,
230 const struct of_device_id *match)
231{
232 struct device_node *np = ofdev->node;
233 struct sdhci_of_data *sdhci_of_data = match->data;
234 struct sdhci_host *host;
235 struct sdhci_of_host *of_host;
236 const u32 *clk;
237 int size;
238 int ret;
239
240 if (!of_device_is_available(np))
241 return -ENODEV;
242
243 host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host));
Julia Lawall2198a642009-08-06 15:07:41 -0700244 if (IS_ERR(host))
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300245 return -ENOMEM;
246
247 of_host = sdhci_priv(host);
248 dev_set_drvdata(&ofdev->dev, host);
249
250 host->ioaddr = of_iomap(np, 0);
251 if (!host->ioaddr) {
252 ret = -ENOMEM;
253 goto err_addr_map;
254 }
255
256 host->irq = irq_of_parse_and_map(np, 0);
257 if (!host->irq) {
258 ret = -EINVAL;
259 goto err_no_irq;
260 }
261
262 host->hw_name = dev_name(&ofdev->dev);
263 if (sdhci_of_data) {
264 host->quirks = sdhci_of_data->quirks;
265 host->ops = &sdhci_of_data->ops;
266 }
267
Anton Vorontsov5fe23c72009-06-18 00:14:08 +0400268 if (of_get_property(np, "sdhci,1-bit-only", NULL))
269 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
270
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300271 clk = of_get_property(np, "clock-frequency", &size);
272 if (clk && size == sizeof(*clk) && *clk)
273 of_host->clock = *clk;
274
275 ret = sdhci_add_host(host);
276 if (ret)
277 goto err_add_host;
278
279 return 0;
280
281err_add_host:
282 irq_dispose_mapping(host->irq);
283err_no_irq:
284 iounmap(host->ioaddr);
285err_addr_map:
286 sdhci_free_host(host);
287 return ret;
288}
289
290static int __devexit sdhci_of_remove(struct of_device *ofdev)
291{
292 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
293
294 sdhci_remove_host(host, 0);
295 sdhci_free_host(host);
296 irq_dispose_mapping(host->irq);
297 iounmap(host->ioaddr);
298 return 0;
299}
300
301static const struct of_device_id sdhci_of_match[] = {
302 { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, },
303 { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, },
Kumar Gala992697e2009-05-08 08:52:49 -0500304 { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, },
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300305 { .compatible = "generic-sdhci", },
306 {},
307};
308MODULE_DEVICE_TABLE(of, sdhci_of_match);
309
310static struct of_platform_driver sdhci_of_driver = {
311 .driver.name = "sdhci-of",
312 .match_table = sdhci_of_match,
313 .probe = sdhci_of_probe,
314 .remove = __devexit_p(sdhci_of_remove),
315 .suspend = sdhci_of_suspend,
316 .resume = sdhci_of_resume,
317};
318
319static int __init sdhci_of_init(void)
320{
321 return of_register_platform_driver(&sdhci_of_driver);
322}
323module_init(sdhci_of_init);
324
325static void __exit sdhci_of_exit(void)
326{
327 of_unregister_platform_driver(&sdhci_of_driver);
328}
329module_exit(sdhci_of_exit);
330
331MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver");
332MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
333 "Anton Vorontsov <avorontsov@ru.mvista.com>");
334MODULE_LICENSE("GPL");