blob: 643818a5ac459fd7ad473cddd6821666fc5bec20 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Pierre Ossman70f10482007-07-11 20:04:50 +02002 * linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
Linus Torvalds1da177e2005-04-16 15:20:36 -07003 *
4 * Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include <linux/module.h>
11#include <linux/moduleparam.h>
12#include <linux/init.h>
13#include <linux/ioport.h>
14#include <linux/device.h>
15#include <linux/interrupt.h>
16#include <linux/delay.h>
17#include <linux/err.h>
18#include <linux/highmem.h>
Nicolas Pitre019a5f52007-10-11 01:06:03 -040019#include <linux/log2.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070020#include <linux/mmc/host.h>
Russell Kinga62c80e2006-01-07 13:52:45 +000021#include <linux/amba/bus.h>
Russell Kingf8ce2542006-01-07 16:15:52 +000022#include <linux/clk.h>
Jens Axboebd6dee62007-10-24 09:01:09 +020023#include <linux/scatterlist.h>
Russell King89001442009-07-09 15:16:07 +010024#include <linux/gpio.h>
Linus Walleij6ef297f2009-09-22 14:29:36 +010025#include <linux/amba/mmci.h>
Linus Walleij34e84f32009-09-22 14:41:40 +010026#include <linux/regulator/consumer.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070027
Russell Kinge9c091b2006-01-04 16:24:05 +000028#include <asm/cacheflush.h>
Russell King7b09cda2005-07-01 12:02:59 +010029#include <asm/div64.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070030#include <asm/io.h>
Russell Kingc6b8fda2005-10-28 14:05:16 +010031#include <asm/sizes.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
33#include "mmci.h"
34
35#define DRIVER_NAME "mmci-pl18x"
36
Linus Torvalds1da177e2005-04-16 15:20:36 -070037#define DBG(host,fmt,args...) \
Russell Kingd366b642005-08-19 09:40:08 +010038 pr_debug("%s: %s: " fmt, mmc_hostname(host->mmc), __func__ , args)
Linus Torvalds1da177e2005-04-16 15:20:36 -070039
40static unsigned int fmax = 515633;
41
Linus Walleija6a64642009-09-14 12:56:14 +010042/*
43 * This must be called with host->lock held
44 */
45static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
46{
47 u32 clk = 0;
48
49 if (desired) {
50 if (desired >= host->mclk) {
51 clk = MCI_CLK_BYPASS;
52 host->cclk = host->mclk;
53 } else {
54 clk = host->mclk / (2 * desired) - 1;
55 if (clk >= 256)
56 clk = 255;
57 host->cclk = host->mclk / (2 * (clk + 1));
58 }
Linus Walleijb43149c2009-11-10 08:33:01 +010059 if (host->hw_designer == AMBA_VENDOR_ST)
Linus Walleija6a64642009-09-14 12:56:14 +010060 clk |= MCI_FCEN; /* Bug fix in ST IP block */
61 clk |= MCI_CLK_ENABLE;
62 /* This hasn't proven to be worthwhile */
63 /* clk |= MCI_CLK_PWRSAVE; */
64 }
65
Linus Walleij9e6c82c2009-09-14 12:57:11 +010066 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
67 clk |= MCI_WIDE_BUS;
68
Linus Walleija6a64642009-09-14 12:56:14 +010069 writel(clk, host->base + MMCICLOCK);
70}
71
Linus Torvalds1da177e2005-04-16 15:20:36 -070072static void
73mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
74{
75 writel(0, host->base + MMCICOMMAND);
76
Russell Kinge47c2222007-01-08 16:42:51 +000077 BUG_ON(host->data);
78
Linus Torvalds1da177e2005-04-16 15:20:36 -070079 host->mrq = NULL;
80 host->cmd = NULL;
81
82 if (mrq->data)
83 mrq->data->bytes_xfered = host->data_xfered;
84
85 /*
86 * Need to drop the host lock here; mmc_request_done may call
87 * back into the driver...
88 */
89 spin_unlock(&host->lock);
90 mmc_request_done(host->mmc, mrq);
91 spin_lock(&host->lock);
92}
93
94static void mmci_stop_data(struct mmci_host *host)
95{
96 writel(0, host->base + MMCIDATACTRL);
97 writel(0, host->base + MMCIMASK1);
98 host->data = NULL;
99}
100
101static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
102{
103 unsigned int datactrl, timeout, irqmask;
Russell King7b09cda2005-07-01 12:02:59 +0100104 unsigned long long clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105 void __iomem *base;
Russell King3bc87f22006-08-27 13:51:28 +0100106 int blksz_bits;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107
108 DBG(host, "blksz %04x blks %04x flags %08x\n",
Russell King3bc87f22006-08-27 13:51:28 +0100109 data->blksz, data->blocks, data->flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700110
111 host->data = data;
Russell King3bc87f22006-08-27 13:51:28 +0100112 host->size = data->blksz;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 host->data_xfered = 0;
114
115 mmci_init_sg(host, data);
116
Russell King7b09cda2005-07-01 12:02:59 +0100117 clks = (unsigned long long)data->timeout_ns * host->cclk;
118 do_div(clks, 1000000000UL);
119
120 timeout = data->timeout_clks + (unsigned int)clks;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700121
122 base = host->base;
123 writel(timeout, base + MMCIDATATIMER);
124 writel(host->size, base + MMCIDATALENGTH);
125
Russell King3bc87f22006-08-27 13:51:28 +0100126 blksz_bits = ffs(data->blksz) - 1;
127 BUG_ON(1 << blksz_bits != data->blksz);
128
129 datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700130 if (data->flags & MMC_DATA_READ) {
131 datactrl |= MCI_DPSM_DIRECTION;
132 irqmask = MCI_RXFIFOHALFFULLMASK;
Russell King0425a142006-02-16 16:48:31 +0000133
134 /*
135 * If we have less than a FIFOSIZE of bytes to transfer,
136 * trigger a PIO interrupt as soon as any data is available.
137 */
138 if (host->size < MCI_FIFOSIZE)
139 irqmask |= MCI_RXDATAAVLBLMASK;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 } else {
141 /*
142 * We don't actually need to include "FIFO empty" here
143 * since its implicit in "FIFO half empty".
144 */
145 irqmask = MCI_TXFIFOHALFEMPTYMASK;
146 }
147
148 writel(datactrl, base + MMCIDATACTRL);
149 writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
150 writel(irqmask, base + MMCIMASK1);
151}
152
153static void
154mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
155{
156 void __iomem *base = host->base;
157
158 DBG(host, "op %02x arg %08x flags %08x\n",
159 cmd->opcode, cmd->arg, cmd->flags);
160
161 if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
162 writel(0, base + MMCICOMMAND);
163 udelay(1);
164 }
165
166 c |= cmd->opcode | MCI_CPSM_ENABLE;
Russell Kinge9225172006-02-02 12:23:12 +0000167 if (cmd->flags & MMC_RSP_PRESENT) {
168 if (cmd->flags & MMC_RSP_136)
169 c |= MCI_CPSM_LONGRSP;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 c |= MCI_CPSM_RESPONSE;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 }
172 if (/*interrupt*/0)
173 c |= MCI_CPSM_INTERRUPT;
174
175 host->cmd = cmd;
176
177 writel(cmd->arg, base + MMCIARGUMENT);
178 writel(c, base + MMCICOMMAND);
179}
180
181static void
182mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
183 unsigned int status)
184{
185 if (status & MCI_DATABLOCKEND) {
Russell King3bc87f22006-08-27 13:51:28 +0100186 host->data_xfered += data->blksz;
Linus Walleijf28e8a42010-01-25 07:14:46 +0100187#ifdef CONFIG_ARCH_U300
188 /*
189 * On the U300 some signal or other is
190 * badly routed so that a data write does
191 * not properly terminate with a MCI_DATAEND
192 * status flag. This quirk will make writes
193 * work again.
194 */
195 if (data->flags & MMC_DATA_WRITE)
196 status |= MCI_DATAEND;
197#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700198 }
199 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
200 if (status & MCI_DATACRCFAIL)
Pierre Ossman17b04292007-07-22 22:18:46 +0200201 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700202 else if (status & MCI_DATATIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200203 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700204 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
Pierre Ossman17b04292007-07-22 22:18:46 +0200205 data->error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206 status |= MCI_DATAEND;
Russell Kinge9c091b2006-01-04 16:24:05 +0000207
208 /*
209 * We hit an error condition. Ensure that any data
210 * partially written to a page is properly coherent.
211 */
212 if (host->sg_len && data->flags & MMC_DATA_READ)
Jens Axboebd6dee62007-10-24 09:01:09 +0200213 flush_dcache_page(sg_page(host->sg_ptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214 }
215 if (status & MCI_DATAEND) {
216 mmci_stop_data(host);
217
218 if (!data->stop) {
219 mmci_request_end(host, data->mrq);
220 } else {
221 mmci_start_command(host, data->stop, 0);
222 }
223 }
224}
225
226static void
227mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
228 unsigned int status)
229{
230 void __iomem *base = host->base;
231
232 host->cmd = NULL;
233
234 cmd->resp[0] = readl(base + MMCIRESPONSE0);
235 cmd->resp[1] = readl(base + MMCIRESPONSE1);
236 cmd->resp[2] = readl(base + MMCIRESPONSE2);
237 cmd->resp[3] = readl(base + MMCIRESPONSE3);
238
239 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200240 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700241 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200242 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700243 }
244
Pierre Ossman17b04292007-07-22 22:18:46 +0200245 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000246 if (host->data)
247 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700248 mmci_request_end(host, cmd->mrq);
249 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
250 mmci_start_data(host, cmd->data);
251 }
252}
253
254static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
255{
256 void __iomem *base = host->base;
257 char *ptr = buffer;
258 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100259 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700260
261 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100262 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700263
264 if (count > remain)
265 count = remain;
266
267 if (count <= 0)
268 break;
269
270 readsl(base + MMCIFIFO, ptr, count >> 2);
271
272 ptr += count;
273 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100274 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700275
276 if (remain == 0)
277 break;
278
279 status = readl(base + MMCISTATUS);
280 } while (status & MCI_RXDATAAVLBL);
281
282 return ptr - buffer;
283}
284
285static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
286{
287 void __iomem *base = host->base;
288 char *ptr = buffer;
289
290 do {
291 unsigned int count, maxcnt;
292
293 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
294 count = min(remain, maxcnt);
295
296 writesl(base + MMCIFIFO, ptr, count >> 2);
297
298 ptr += count;
299 remain -= count;
300
301 if (remain == 0)
302 break;
303
304 status = readl(base + MMCISTATUS);
305 } while (status & MCI_TXFIFOHALFEMPTY);
306
307 return ptr - buffer;
308}
309
310/*
311 * PIO data transfer IRQ handler.
312 */
David Howells7d12e782006-10-05 14:55:46 +0100313static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314{
315 struct mmci_host *host = dev_id;
316 void __iomem *base = host->base;
317 u32 status;
318
319 status = readl(base + MMCISTATUS);
320
321 DBG(host, "irq1 %08x\n", status);
322
323 do {
324 unsigned long flags;
325 unsigned int remain, len;
326 char *buffer;
327
328 /*
329 * For write, we only need to test the half-empty flag
330 * here - if the FIFO is completely empty, then by
331 * definition it is more than half empty.
332 *
333 * For read, check for data available.
334 */
335 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
336 break;
337
338 /*
339 * Map the current scatter buffer.
340 */
341 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
342 remain = host->sg_ptr->length - host->sg_off;
343
344 len = 0;
345 if (status & MCI_RXACTIVE)
346 len = mmci_pio_read(host, buffer, remain);
347 if (status & MCI_TXACTIVE)
348 len = mmci_pio_write(host, buffer, remain, status);
349
350 /*
351 * Unmap the buffer.
352 */
Evgeniy Polyakovf3e26282006-01-05 10:31:23 +0000353 mmci_kunmap_atomic(host, buffer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354
355 host->sg_off += len;
356 host->size -= len;
357 remain -= len;
358
359 if (remain)
360 break;
361
Russell Kinge9c091b2006-01-04 16:24:05 +0000362 /*
363 * If we were reading, and we have completed this
364 * page, ensure that the data cache is coherent.
365 */
366 if (status & MCI_RXACTIVE)
Jens Axboebd6dee62007-10-24 09:01:09 +0200367 flush_dcache_page(sg_page(host->sg_ptr));
Russell Kinge9c091b2006-01-04 16:24:05 +0000368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 if (!mmci_next_sg(host))
370 break;
371
372 status = readl(base + MMCISTATUS);
373 } while (1);
374
375 /*
376 * If we're nearing the end of the read, switch to
377 * "any data available" mode.
378 */
379 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
380 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
381
382 /*
383 * If we run out of data, disable the data IRQs; this
384 * prevents a race where the FIFO becomes empty before
385 * the chip itself has disabled the data path, and
386 * stops us racing with our data end IRQ.
387 */
388 if (host->size == 0) {
389 writel(0, base + MMCIMASK1);
390 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
391 }
392
393 return IRQ_HANDLED;
394}
395
396/*
397 * Handle completion of command and data transfers.
398 */
David Howells7d12e782006-10-05 14:55:46 +0100399static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 struct mmci_host *host = dev_id;
402 u32 status;
403 int ret = 0;
404
405 spin_lock(&host->lock);
406
407 do {
408 struct mmc_command *cmd;
409 struct mmc_data *data;
410
411 status = readl(host->base + MMCISTATUS);
412 status &= readl(host->base + MMCIMASK0);
413 writel(status, host->base + MMCICLEAR);
414
415 DBG(host, "irq0 %08x\n", status);
416
417 data = host->data;
418 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
419 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
420 mmci_data_irq(host, data, status);
421
422 cmd = host->cmd;
423 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
424 mmci_cmd_irq(host, cmd, status);
425
426 ret = 1;
427 } while (status);
428
429 spin_unlock(&host->lock);
430
431 return IRQ_RETVAL(ret);
432}
433
434static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
435{
436 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100437 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438
439 WARN_ON(host->mrq != NULL);
440
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400441 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Pierre Ossman255d01a2007-07-24 20:38:53 +0200442 printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n",
443 mmc_hostname(mmc), mrq->data->blksz);
444 mrq->cmd->error = -EINVAL;
445 mmc_request_done(mmc, mrq);
446 return;
447 }
448
Linus Walleij9e943022008-10-24 21:17:50 +0100449 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700450
451 host->mrq = mrq;
452
453 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
454 mmci_start_data(host, mrq->data);
455
456 mmci_start_command(host, mrq->cmd, 0);
457
Linus Walleij9e943022008-10-24 21:17:50 +0100458 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459}
460
461static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
462{
463 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100464 u32 pwr = 0;
465 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700466
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467 switch (ios->power_mode) {
468 case MMC_POWER_OFF:
Linus Walleij34e84f32009-09-22 14:41:40 +0100469 if(host->vcc &&
470 regulator_is_enabled(host->vcc))
471 regulator_disable(host->vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700472 break;
473 case MMC_POWER_UP:
Linus Walleij34e84f32009-09-22 14:41:40 +0100474#ifdef CONFIG_REGULATOR
475 if (host->vcc)
476 /* This implicitly enables the regulator */
477 mmc_regulator_set_ocr(host->vcc, ios->vdd);
478#endif
479 /*
480 * The translate_vdd function is not used if you have
481 * an external regulator, or your design is really weird.
482 * Using it would mean sending in power control BOTH using
483 * a regulator AND the 4 MMCIPWR bits. If we don't have
484 * a regulator, we might have some other platform specific
485 * power control behind this translate function.
486 */
487 if (!host->vcc && host->plat->translate_vdd)
488 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
Linus Walleijcc30d602009-01-04 15:18:54 +0100489 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100490 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100491 pwr |= MCI_PWR_UP;
492 break;
493 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700494 case MMC_POWER_ON:
495 pwr |= MCI_PWR_ON;
496 break;
497 }
498
Linus Walleijcc30d602009-01-04 15:18:54 +0100499 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100500 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100501 pwr |= MCI_ROD;
502 else {
503 /*
504 * The ST Micro variant use the ROD bit for something
505 * else and only has OD (Open Drain).
506 */
507 pwr |= MCI_OD;
508 }
509 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510
Linus Walleija6a64642009-09-14 12:56:14 +0100511 spin_lock_irqsave(&host->lock, flags);
512
513 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700514
515 if (host->pwr != pwr) {
516 host->pwr = pwr;
517 writel(pwr, host->base + MMCIPOWER);
518 }
Linus Walleija6a64642009-09-14 12:56:14 +0100519
520 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521}
522
Russell King89001442009-07-09 15:16:07 +0100523static int mmci_get_ro(struct mmc_host *mmc)
524{
525 struct mmci_host *host = mmc_priv(mmc);
526
527 if (host->gpio_wp == -ENOSYS)
528 return -ENOSYS;
529
530 return gpio_get_value(host->gpio_wp);
531}
532
533static int mmci_get_cd(struct mmc_host *mmc)
534{
535 struct mmci_host *host = mmc_priv(mmc);
536 unsigned int status;
537
538 if (host->gpio_cd == -ENOSYS)
539 status = host->plat->status(mmc_dev(host->mmc));
540 else
541 status = gpio_get_value(host->gpio_cd);
542
543 return !status;
544}
545
David Brownellab7aefd2006-11-12 17:55:30 -0800546static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 .request = mmci_request,
548 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100549 .get_ro = mmci_get_ro,
550 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551};
552
553static void mmci_check_status(unsigned long data)
554{
555 struct mmci_host *host = (struct mmci_host *)data;
Russell King89001442009-07-09 15:16:07 +0100556 unsigned int status = mmci_get_cd(host->mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558 if (status ^ host->oldstat)
Richard Purdie8dc00332005-09-08 17:53:01 +0100559 mmc_detect_change(host->mmc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700560
561 host->oldstat = status;
562 mod_timer(&host->timer, jiffies + HZ);
563}
564
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100565static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100567 struct mmci_platform_data *plat = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700568 struct mmci_host *host;
569 struct mmc_host *mmc;
570 int ret;
571
572 /* must have platform data */
573 if (!plat) {
574 ret = -EINVAL;
575 goto out;
576 }
577
578 ret = amba_request_regions(dev, DRIVER_NAME);
579 if (ret)
580 goto out;
581
582 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
583 if (!mmc) {
584 ret = -ENOMEM;
585 goto rel_regions;
586 }
587
588 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530589 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100590
Russell King89001442009-07-09 15:16:07 +0100591 host->gpio_wp = -ENOSYS;
592 host->gpio_cd = -ENOSYS;
593
Russell King012b7d32009-07-09 15:13:56 +0100594 host->hw_designer = amba_manf(dev);
595 host->hw_revision = amba_rev(dev);
Linus Walleijcc30d602009-01-04 15:18:54 +0100596 DBG(host, "designer ID = 0x%02x\n", host->hw_designer);
597 DBG(host, "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100598
Russell Kingee569c42008-11-30 17:38:14 +0000599 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 if (IS_ERR(host->clk)) {
601 ret = PTR_ERR(host->clk);
602 host->clk = NULL;
603 goto host_free;
604 }
605
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 ret = clk_enable(host->clk);
607 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000608 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700609
610 host->plat = plat;
611 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100612 /*
613 * According to the spec, mclk is max 100 MHz,
614 * so we try to adjust the clock down to this,
615 * (if possible).
616 */
617 if (host->mclk > 100000000) {
618 ret = clk_set_rate(host->clk, 100000000);
619 if (ret < 0)
620 goto clk_disable;
621 host->mclk = clk_get_rate(host->clk);
622 DBG(host, "eventual mclk rate: %u Hz\n", host->mclk);
623 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100624 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700625 if (!host->base) {
626 ret = -ENOMEM;
627 goto clk_disable;
628 }
629
630 mmc->ops = &mmci_ops;
631 mmc->f_min = (host->mclk + 511) / 512;
632 mmc->f_max = min(host->mclk, fmax);
Linus Walleij34e84f32009-09-22 14:41:40 +0100633#ifdef CONFIG_REGULATOR
634 /* If we're using the regulator framework, try to fetch a regulator */
635 host->vcc = regulator_get(&dev->dev, "vmmc");
636 if (IS_ERR(host->vcc))
637 host->vcc = NULL;
638 else {
639 int mask = mmc_regulator_get_ocrmask(host->vcc);
640
641 if (mask < 0)
642 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
643 mask);
644 else {
645 host->mmc->ocr_avail = (u32) mask;
646 if (plat->ocr_mask)
647 dev_warn(&dev->dev,
648 "Provided ocr_mask/setpower will not be used "
649 "(using regulator instead)\n");
650 }
651 }
652#endif
653 /* Fall back to platform data if no regulator is found */
654 if (host->vcc == NULL)
655 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100656 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657
658 /*
659 * We can do SGIO
660 */
661 mmc->max_hw_segs = 16;
662 mmc->max_phys_segs = NR_SG;
663
664 /*
665 * Since we only have a 16-bit data length register, we must
666 * ensure that we don't exceed 2^16-1 bytes in a single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100668 mmc->max_req_size = 65535;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700669
670 /*
671 * Set the maximum segment size. Since we aren't doing DMA
672 * (yet) we are only limited by the data length register.
673 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100674 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100676 /*
677 * Block size can be up to 2048 bytes, but must be a power of two.
678 */
679 mmc->max_blk_size = 2048;
680
Pierre Ossman55db8902006-11-21 17:55:45 +0100681 /*
682 * No limit on the number of blocks transferred.
683 */
684 mmc->max_blk_count = mmc->max_req_size;
685
Linus Torvalds1da177e2005-04-16 15:20:36 -0700686 spin_lock_init(&host->lock);
687
688 writel(0, host->base + MMCIMASK0);
689 writel(0, host->base + MMCIMASK1);
690 writel(0xfff, host->base + MMCICLEAR);
691
Russell King89001442009-07-09 15:16:07 +0100692 if (gpio_is_valid(plat->gpio_cd)) {
693 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
694 if (ret == 0)
695 ret = gpio_direction_input(plat->gpio_cd);
696 if (ret == 0)
697 host->gpio_cd = plat->gpio_cd;
698 else if (ret != -ENOSYS)
699 goto err_gpio_cd;
700 }
701 if (gpio_is_valid(plat->gpio_wp)) {
702 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
703 if (ret == 0)
704 ret = gpio_direction_input(plat->gpio_wp);
705 if (ret == 0)
706 host->gpio_wp = plat->gpio_wp;
707 else if (ret != -ENOSYS)
708 goto err_gpio_wp;
709 }
710
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700711 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700712 if (ret)
713 goto unmap;
714
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700715 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 if (ret)
717 goto irq0_free;
718
719 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
720
721 amba_set_drvdata(dev, mmc);
Russell King89001442009-07-09 15:16:07 +0100722 host->oldstat = mmci_get_cd(host->mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723
724 mmc_add_host(mmc);
725
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700726 printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
Russell Kingd366b642005-08-19 09:40:08 +0100727 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700728 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700729
730 init_timer(&host->timer);
731 host->timer.data = (unsigned long)host;
732 host->timer.function = mmci_check_status;
733 host->timer.expires = jiffies + HZ;
734 add_timer(&host->timer);
735
736 return 0;
737
738 irq0_free:
739 free_irq(dev->irq[0], host);
740 unmap:
Russell King89001442009-07-09 15:16:07 +0100741 if (host->gpio_wp != -ENOSYS)
742 gpio_free(host->gpio_wp);
743 err_gpio_wp:
744 if (host->gpio_cd != -ENOSYS)
745 gpio_free(host->gpio_cd);
746 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 iounmap(host->base);
748 clk_disable:
749 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700750 clk_free:
751 clk_put(host->clk);
752 host_free:
753 mmc_free_host(mmc);
754 rel_regions:
755 amba_release_regions(dev);
756 out:
757 return ret;
758}
759
Linus Walleij6dc4a472009-03-07 00:23:52 +0100760static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700761{
762 struct mmc_host *mmc = amba_get_drvdata(dev);
763
764 amba_set_drvdata(dev, NULL);
765
766 if (mmc) {
767 struct mmci_host *host = mmc_priv(mmc);
768
769 del_timer_sync(&host->timer);
770
771 mmc_remove_host(mmc);
772
773 writel(0, host->base + MMCIMASK0);
774 writel(0, host->base + MMCIMASK1);
775
776 writel(0, host->base + MMCICOMMAND);
777 writel(0, host->base + MMCIDATACTRL);
778
779 free_irq(dev->irq[0], host);
780 free_irq(dev->irq[1], host);
781
Russell King89001442009-07-09 15:16:07 +0100782 if (host->gpio_wp != -ENOSYS)
783 gpio_free(host->gpio_wp);
784 if (host->gpio_cd != -ENOSYS)
785 gpio_free(host->gpio_cd);
786
Linus Torvalds1da177e2005-04-16 15:20:36 -0700787 iounmap(host->base);
788 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 clk_put(host->clk);
790
Linus Walleij34e84f32009-09-22 14:41:40 +0100791 if (regulator_is_enabled(host->vcc))
792 regulator_disable(host->vcc);
793 regulator_put(host->vcc);
794
Linus Torvalds1da177e2005-04-16 15:20:36 -0700795 mmc_free_host(mmc);
796
797 amba_release_regions(dev);
798 }
799
800 return 0;
801}
802
803#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700804static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805{
806 struct mmc_host *mmc = amba_get_drvdata(dev);
807 int ret = 0;
808
809 if (mmc) {
810 struct mmci_host *host = mmc_priv(mmc);
811
812 ret = mmc_suspend_host(mmc, state);
813 if (ret == 0)
814 writel(0, host->base + MMCIMASK0);
815 }
816
817 return ret;
818}
819
820static int mmci_resume(struct amba_device *dev)
821{
822 struct mmc_host *mmc = amba_get_drvdata(dev);
823 int ret = 0;
824
825 if (mmc) {
826 struct mmci_host *host = mmc_priv(mmc);
827
828 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
829
830 ret = mmc_resume_host(mmc);
831 }
832
833 return ret;
834}
835#else
836#define mmci_suspend NULL
837#define mmci_resume NULL
838#endif
839
840static struct amba_id mmci_ids[] = {
841 {
842 .id = 0x00041180,
843 .mask = 0x000fffff,
844 },
845 {
846 .id = 0x00041181,
847 .mask = 0x000fffff,
848 },
Linus Walleijcc30d602009-01-04 15:18:54 +0100849 /* ST Micro variants */
850 {
851 .id = 0x00180180,
852 .mask = 0x00ffffff,
853 },
854 {
855 .id = 0x00280180,
856 .mask = 0x00ffffff,
857 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 { 0, 0 },
859};
860
861static struct amba_driver mmci_driver = {
862 .drv = {
863 .name = DRIVER_NAME,
864 },
865 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +0100866 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700867 .suspend = mmci_suspend,
868 .resume = mmci_resume,
869 .id_table = mmci_ids,
870};
871
872static int __init mmci_init(void)
873{
874 return amba_driver_register(&mmci_driver);
875}
876
877static void __exit mmci_exit(void)
878{
879 amba_driver_unregister(&mmci_driver);
880}
881
882module_init(mmci_init);
883module_exit(mmci_exit);
884module_param(fmax, uint, 0444);
885
886MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
887MODULE_LICENSE("GPL");