blob: c8dab07e34b81a22b18f334fae09161cca52fa46 [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>
Anton Vorontsov8226a212009-09-22 16:45:15 -070024#include <asm/machdep.h>
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030025#include "sdhci.h"
26
27struct sdhci_of_data {
28 unsigned int quirks;
29 struct sdhci_ops ops;
30};
31
32struct sdhci_of_host {
33 unsigned int clock;
34 u16 xfer_mode_shadow;
35};
36
37/*
38 * Ops and quirks for the Freescale eSDHC controller.
39 */
40
41#define ESDHC_DMA_SYSCTL 0x40c
42#define ESDHC_DMA_SNOOP 0x00000040
43
44#define ESDHC_SYSTEM_CONTROL 0x2c
45#define ESDHC_CLOCK_MASK 0x0000fff0
46#define ESDHC_PREDIV_SHIFT 8
47#define ESDHC_DIVIDER_SHIFT 4
48#define ESDHC_CLOCK_PEREN 0x00000004
49#define ESDHC_CLOCK_HCKEN 0x00000002
50#define ESDHC_CLOCK_IPGEN 0x00000001
51
Anton Vorontsovc0859262009-09-22 16:45:11 -070052#define ESDHC_HOST_CONTROL_RES 0x05
53
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030054static u32 esdhc_readl(struct sdhci_host *host, int reg)
55{
56 return in_be32(host->ioaddr + reg);
57}
58
59static u16 esdhc_readw(struct sdhci_host *host, int reg)
60{
Dave Liufbf6a5f2009-05-06 18:40:07 +080061 u16 ret;
62
63 if (unlikely(reg == SDHCI_HOST_VERSION))
64 ret = in_be16(host->ioaddr + reg);
65 else
66 ret = in_be16(host->ioaddr + (reg ^ 0x2));
67 return ret;
Anton Vorontsov3085e9c2009-03-17 00:14:05 +030068}
69
70static u8 esdhc_readb(struct sdhci_host *host, int reg)
71{
72 return in_8(host->ioaddr + (reg ^ 0x3));
73}
74
75static void esdhc_writel(struct sdhci_host *host, u32 val, int reg)
76{
77 out_be32(host->ioaddr + reg, val);
78}
79
80static void esdhc_writew(struct sdhci_host *host, u16 val, int reg)
81{
82 struct sdhci_of_host *of_host = sdhci_priv(host);
83 int base = reg & ~0x3;
84 int shift = (reg & 0x2) * 8;
85
86 switch (reg) {
87 case SDHCI_TRANSFER_MODE:
88 /*
89 * Postpone this write, we must do it together with a
90 * command write that is down below.
91 */
92 of_host->xfer_mode_shadow = val;
93 return;
94 case SDHCI_COMMAND:
95 esdhc_writel(host, val << 16 | of_host->xfer_mode_shadow,
96 SDHCI_TRANSFER_MODE);
97 return;
98 case SDHCI_BLOCK_SIZE:
99 /*
100 * Two last DMA bits are reserved, and first one is used for
101 * non-standard blksz of 4096 bytes that we don't support
102 * yet. So clear the DMA boundary bits.
103 */
104 val &= ~SDHCI_MAKE_BLKSZ(0x7, 0);
105 /* fall through */
106 }
107 clrsetbits_be32(host->ioaddr + base, 0xffff << shift, val << shift);
108}
109
110static void esdhc_writeb(struct sdhci_host *host, u8 val, int reg)
111{
112 int base = reg & ~0x3;
113 int shift = (reg & 0x3) * 8;
114
Anton Vorontsovc0859262009-09-22 16:45:11 -0700115 /* Prevent SDHCI core from writing reserved bits (e.g. HISPD). */
116 if (reg == SDHCI_HOST_CONTROL)
117 val &= ~ESDHC_HOST_CONTROL_RES;
118
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300119 clrsetbits_be32(host->ioaddr + base , 0xff << shift, val << shift);
120}
121
122static void esdhc_set_clock(struct sdhci_host *host, unsigned int clock)
123{
124 int div;
125 int pre_div = 2;
126
127 clrbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN |
128 ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN | ESDHC_CLOCK_MASK);
129
130 if (clock == 0)
131 goto out;
132
133 if (host->max_clk / 16 > clock) {
134 for (; pre_div < 256; pre_div *= 2) {
135 if (host->max_clk / pre_div < clock * 16)
136 break;
137 }
138 }
139
140 for (div = 1; div <= 16; div++) {
141 if (host->max_clk / (div * pre_div) <= clock)
142 break;
143 }
144
145 pre_div >>= 1;
Anton Vorontsov1e5df752009-09-22 16:45:10 -0700146 div--;
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300147
148 setbits32(host->ioaddr + ESDHC_SYSTEM_CONTROL, ESDHC_CLOCK_IPGEN |
149 ESDHC_CLOCK_HCKEN | ESDHC_CLOCK_PEREN |
150 div << ESDHC_DIVIDER_SHIFT | pre_div << ESDHC_PREDIV_SHIFT);
151 mdelay(100);
152out:
153 host->clock = clock;
154}
155
156static int esdhc_enable_dma(struct sdhci_host *host)
157{
158 setbits32(host->ioaddr + ESDHC_DMA_SYSCTL, ESDHC_DMA_SNOOP);
159 return 0;
160}
161
162static unsigned int esdhc_get_max_clock(struct sdhci_host *host)
163{
164 struct sdhci_of_host *of_host = sdhci_priv(host);
165
166 return of_host->clock;
167}
168
Anton Vorontsova9e58f22009-07-29 15:04:16 -0700169static unsigned int esdhc_get_min_clock(struct sdhci_host *host)
170{
171 struct sdhci_of_host *of_host = sdhci_priv(host);
172
173 return of_host->clock / 256 / 16;
174}
175
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300176static struct sdhci_of_data sdhci_esdhc = {
177 .quirks = SDHCI_QUIRK_FORCE_BLK_SZ_2048 |
178 SDHCI_QUIRK_BROKEN_CARD_DETECTION |
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300179 SDHCI_QUIRK_NO_BUSY_IRQ |
180 SDHCI_QUIRK_NONSTANDARD_CLOCK |
Anton Vorontsov81b39802009-09-22 16:45:13 -0700181 SDHCI_QUIRK_DATA_TIMEOUT_USES_SDCLK |
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300182 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 },
197};
198
199#ifdef CONFIG_PM
200
201static int sdhci_of_suspend(struct of_device *ofdev, pm_message_t state)
202{
203 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
204
205 return mmc_suspend_host(host->mmc, state);
206}
207
208static int sdhci_of_resume(struct of_device *ofdev)
209{
210 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
211
212 return mmc_resume_host(host->mmc);
213}
214
215#else
216
217#define sdhci_of_suspend NULL
218#define sdhci_of_resume NULL
219
220#endif
221
Anton Vorontsov8226a212009-09-22 16:45:15 -0700222static bool __devinit sdhci_of_wp_inverted(struct device_node *np)
223{
224 if (of_get_property(np, "sdhci,wp-inverted", NULL))
225 return true;
226
227 /* Old device trees don't have the wp-inverted property. */
228 return machine_is(mpc837x_rdb) || machine_is(mpc837x_mds);
229}
230
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300231static int __devinit sdhci_of_probe(struct of_device *ofdev,
232 const struct of_device_id *match)
233{
234 struct device_node *np = ofdev->node;
235 struct sdhci_of_data *sdhci_of_data = match->data;
236 struct sdhci_host *host;
237 struct sdhci_of_host *of_host;
238 const u32 *clk;
239 int size;
240 int ret;
241
242 if (!of_device_is_available(np))
243 return -ENODEV;
244
245 host = sdhci_alloc_host(&ofdev->dev, sizeof(*of_host));
Julia Lawall2198a642009-08-06 15:07:41 -0700246 if (IS_ERR(host))
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300247 return -ENOMEM;
248
249 of_host = sdhci_priv(host);
250 dev_set_drvdata(&ofdev->dev, host);
251
252 host->ioaddr = of_iomap(np, 0);
253 if (!host->ioaddr) {
254 ret = -ENOMEM;
255 goto err_addr_map;
256 }
257
258 host->irq = irq_of_parse_and_map(np, 0);
259 if (!host->irq) {
260 ret = -EINVAL;
261 goto err_no_irq;
262 }
263
264 host->hw_name = dev_name(&ofdev->dev);
265 if (sdhci_of_data) {
266 host->quirks = sdhci_of_data->quirks;
267 host->ops = &sdhci_of_data->ops;
268 }
269
Anton Vorontsov5fe23c72009-06-18 00:14:08 +0400270 if (of_get_property(np, "sdhci,1-bit-only", NULL))
271 host->quirks |= SDHCI_QUIRK_FORCE_1_BIT_DATA;
272
Anton Vorontsov8226a212009-09-22 16:45:15 -0700273 if (sdhci_of_wp_inverted(np))
274 host->quirks |= SDHCI_QUIRK_INVERTED_WRITE_PROTECT;
275
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300276 clk = of_get_property(np, "clock-frequency", &size);
277 if (clk && size == sizeof(*clk) && *clk)
278 of_host->clock = *clk;
279
280 ret = sdhci_add_host(host);
281 if (ret)
282 goto err_add_host;
283
284 return 0;
285
286err_add_host:
287 irq_dispose_mapping(host->irq);
288err_no_irq:
289 iounmap(host->ioaddr);
290err_addr_map:
291 sdhci_free_host(host);
292 return ret;
293}
294
295static int __devexit sdhci_of_remove(struct of_device *ofdev)
296{
297 struct sdhci_host *host = dev_get_drvdata(&ofdev->dev);
298
299 sdhci_remove_host(host, 0);
300 sdhci_free_host(host);
301 irq_dispose_mapping(host->irq);
302 iounmap(host->ioaddr);
303 return 0;
304}
305
306static const struct of_device_id sdhci_of_match[] = {
307 { .compatible = "fsl,mpc8379-esdhc", .data = &sdhci_esdhc, },
308 { .compatible = "fsl,mpc8536-esdhc", .data = &sdhci_esdhc, },
Kumar Gala992697e2009-05-08 08:52:49 -0500309 { .compatible = "fsl,esdhc", .data = &sdhci_esdhc, },
Anton Vorontsov3085e9c2009-03-17 00:14:05 +0300310 { .compatible = "generic-sdhci", },
311 {},
312};
313MODULE_DEVICE_TABLE(of, sdhci_of_match);
314
315static struct of_platform_driver sdhci_of_driver = {
316 .driver.name = "sdhci-of",
317 .match_table = sdhci_of_match,
318 .probe = sdhci_of_probe,
319 .remove = __devexit_p(sdhci_of_remove),
320 .suspend = sdhci_of_suspend,
321 .resume = sdhci_of_resume,
322};
323
324static int __init sdhci_of_init(void)
325{
326 return of_register_platform_driver(&sdhci_of_driver);
327}
328module_init(sdhci_of_init);
329
330static void __exit sdhci_of_exit(void)
331{
332 of_unregister_platform_driver(&sdhci_of_driver);
333}
334module_exit(sdhci_of_exit);
335
336MODULE_DESCRIPTION("Secure Digital Host Controller Interface OF driver");
337MODULE_AUTHOR("Xiaobo Xie <X.Xie@freescale.com>, "
338 "Anton Vorontsov <avorontsov@ru.mvista.com>");
339MODULE_LICENSE("GPL");