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