blob: 3d1e5329da12b9f84a141f544c0aaac8e0f8d92c [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 }
59 if (host->hw_designer == 0x80)
60 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 Torvalds1da177e2005-04-16 15:20:36 -0700187 }
188 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
189 if (status & MCI_DATACRCFAIL)
Pierre Ossman17b04292007-07-22 22:18:46 +0200190 data->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700191 else if (status & MCI_DATATIMEOUT)
Pierre Ossman17b04292007-07-22 22:18:46 +0200192 data->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700193 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
Pierre Ossman17b04292007-07-22 22:18:46 +0200194 data->error = -EIO;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700195 status |= MCI_DATAEND;
Russell Kinge9c091b2006-01-04 16:24:05 +0000196
197 /*
198 * We hit an error condition. Ensure that any data
199 * partially written to a page is properly coherent.
200 */
201 if (host->sg_len && data->flags & MMC_DATA_READ)
Jens Axboebd6dee62007-10-24 09:01:09 +0200202 flush_dcache_page(sg_page(host->sg_ptr));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203 }
204 if (status & MCI_DATAEND) {
205 mmci_stop_data(host);
206
207 if (!data->stop) {
208 mmci_request_end(host, data->mrq);
209 } else {
210 mmci_start_command(host, data->stop, 0);
211 }
212 }
213}
214
215static void
216mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
217 unsigned int status)
218{
219 void __iomem *base = host->base;
220
221 host->cmd = NULL;
222
223 cmd->resp[0] = readl(base + MMCIRESPONSE0);
224 cmd->resp[1] = readl(base + MMCIRESPONSE1);
225 cmd->resp[2] = readl(base + MMCIRESPONSE2);
226 cmd->resp[3] = readl(base + MMCIRESPONSE3);
227
228 if (status & MCI_CMDTIMEOUT) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200229 cmd->error = -ETIMEDOUT;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700230 } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
Pierre Ossman17b04292007-07-22 22:18:46 +0200231 cmd->error = -EILSEQ;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700232 }
233
Pierre Ossman17b04292007-07-22 22:18:46 +0200234 if (!cmd->data || cmd->error) {
Russell Kinge47c2222007-01-08 16:42:51 +0000235 if (host->data)
236 mmci_stop_data(host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237 mmci_request_end(host, cmd->mrq);
238 } else if (!(cmd->data->flags & MMC_DATA_READ)) {
239 mmci_start_data(host, cmd->data);
240 }
241}
242
243static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
244{
245 void __iomem *base = host->base;
246 char *ptr = buffer;
247 u32 status;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100248 int host_remain = host->size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700249
250 do {
Linus Walleij26eed9a2008-04-26 23:39:44 +0100251 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700252
253 if (count > remain)
254 count = remain;
255
256 if (count <= 0)
257 break;
258
259 readsl(base + MMCIFIFO, ptr, count >> 2);
260
261 ptr += count;
262 remain -= count;
Linus Walleij26eed9a2008-04-26 23:39:44 +0100263 host_remain -= count;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700264
265 if (remain == 0)
266 break;
267
268 status = readl(base + MMCISTATUS);
269 } while (status & MCI_RXDATAAVLBL);
270
271 return ptr - buffer;
272}
273
274static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
275{
276 void __iomem *base = host->base;
277 char *ptr = buffer;
278
279 do {
280 unsigned int count, maxcnt;
281
282 maxcnt = status & MCI_TXFIFOEMPTY ? MCI_FIFOSIZE : MCI_FIFOHALFSIZE;
283 count = min(remain, maxcnt);
284
285 writesl(base + MMCIFIFO, ptr, count >> 2);
286
287 ptr += count;
288 remain -= count;
289
290 if (remain == 0)
291 break;
292
293 status = readl(base + MMCISTATUS);
294 } while (status & MCI_TXFIFOHALFEMPTY);
295
296 return ptr - buffer;
297}
298
299/*
300 * PIO data transfer IRQ handler.
301 */
David Howells7d12e782006-10-05 14:55:46 +0100302static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700303{
304 struct mmci_host *host = dev_id;
305 void __iomem *base = host->base;
306 u32 status;
307
308 status = readl(base + MMCISTATUS);
309
310 DBG(host, "irq1 %08x\n", status);
311
312 do {
313 unsigned long flags;
314 unsigned int remain, len;
315 char *buffer;
316
317 /*
318 * For write, we only need to test the half-empty flag
319 * here - if the FIFO is completely empty, then by
320 * definition it is more than half empty.
321 *
322 * For read, check for data available.
323 */
324 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
325 break;
326
327 /*
328 * Map the current scatter buffer.
329 */
330 buffer = mmci_kmap_atomic(host, &flags) + host->sg_off;
331 remain = host->sg_ptr->length - host->sg_off;
332
333 len = 0;
334 if (status & MCI_RXACTIVE)
335 len = mmci_pio_read(host, buffer, remain);
336 if (status & MCI_TXACTIVE)
337 len = mmci_pio_write(host, buffer, remain, status);
338
339 /*
340 * Unmap the buffer.
341 */
Evgeniy Polyakovf3e26282006-01-05 10:31:23 +0000342 mmci_kunmap_atomic(host, buffer, &flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700343
344 host->sg_off += len;
345 host->size -= len;
346 remain -= len;
347
348 if (remain)
349 break;
350
Russell Kinge9c091b2006-01-04 16:24:05 +0000351 /*
352 * If we were reading, and we have completed this
353 * page, ensure that the data cache is coherent.
354 */
355 if (status & MCI_RXACTIVE)
Jens Axboebd6dee62007-10-24 09:01:09 +0200356 flush_dcache_page(sg_page(host->sg_ptr));
Russell Kinge9c091b2006-01-04 16:24:05 +0000357
Linus Torvalds1da177e2005-04-16 15:20:36 -0700358 if (!mmci_next_sg(host))
359 break;
360
361 status = readl(base + MMCISTATUS);
362 } while (1);
363
364 /*
365 * If we're nearing the end of the read, switch to
366 * "any data available" mode.
367 */
368 if (status & MCI_RXACTIVE && host->size < MCI_FIFOSIZE)
369 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
370
371 /*
372 * If we run out of data, disable the data IRQs; this
373 * prevents a race where the FIFO becomes empty before
374 * the chip itself has disabled the data path, and
375 * stops us racing with our data end IRQ.
376 */
377 if (host->size == 0) {
378 writel(0, base + MMCIMASK1);
379 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
380 }
381
382 return IRQ_HANDLED;
383}
384
385/*
386 * Handle completion of command and data transfers.
387 */
David Howells7d12e782006-10-05 14:55:46 +0100388static irqreturn_t mmci_irq(int irq, void *dev_id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389{
390 struct mmci_host *host = dev_id;
391 u32 status;
392 int ret = 0;
393
394 spin_lock(&host->lock);
395
396 do {
397 struct mmc_command *cmd;
398 struct mmc_data *data;
399
400 status = readl(host->base + MMCISTATUS);
401 status &= readl(host->base + MMCIMASK0);
402 writel(status, host->base + MMCICLEAR);
403
404 DBG(host, "irq0 %08x\n", status);
405
406 data = host->data;
407 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
408 MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
409 mmci_data_irq(host, data, status);
410
411 cmd = host->cmd;
412 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
413 mmci_cmd_irq(host, cmd, status);
414
415 ret = 1;
416 } while (status);
417
418 spin_unlock(&host->lock);
419
420 return IRQ_RETVAL(ret);
421}
422
423static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
424{
425 struct mmci_host *host = mmc_priv(mmc);
Linus Walleij9e943022008-10-24 21:17:50 +0100426 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427
428 WARN_ON(host->mrq != NULL);
429
Nicolas Pitre019a5f52007-10-11 01:06:03 -0400430 if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
Pierre Ossman255d01a2007-07-24 20:38:53 +0200431 printk(KERN_ERR "%s: Unsupported block size (%d bytes)\n",
432 mmc_hostname(mmc), mrq->data->blksz);
433 mrq->cmd->error = -EINVAL;
434 mmc_request_done(mmc, mrq);
435 return;
436 }
437
Linus Walleij9e943022008-10-24 21:17:50 +0100438 spin_lock_irqsave(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700439
440 host->mrq = mrq;
441
442 if (mrq->data && mrq->data->flags & MMC_DATA_READ)
443 mmci_start_data(host, mrq->data);
444
445 mmci_start_command(host, mrq->cmd, 0);
446
Linus Walleij9e943022008-10-24 21:17:50 +0100447 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700448}
449
450static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
451{
452 struct mmci_host *host = mmc_priv(mmc);
Linus Walleija6a64642009-09-14 12:56:14 +0100453 u32 pwr = 0;
454 unsigned long flags;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455
Linus Torvalds1da177e2005-04-16 15:20:36 -0700456 switch (ios->power_mode) {
457 case MMC_POWER_OFF:
Linus Walleij34e84f32009-09-22 14:41:40 +0100458 if(host->vcc &&
459 regulator_is_enabled(host->vcc))
460 regulator_disable(host->vcc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700461 break;
462 case MMC_POWER_UP:
Linus Walleij34e84f32009-09-22 14:41:40 +0100463#ifdef CONFIG_REGULATOR
464 if (host->vcc)
465 /* This implicitly enables the regulator */
466 mmc_regulator_set_ocr(host->vcc, ios->vdd);
467#endif
468 /*
469 * The translate_vdd function is not used if you have
470 * an external regulator, or your design is really weird.
471 * Using it would mean sending in power control BOTH using
472 * a regulator AND the 4 MMCIPWR bits. If we don't have
473 * a regulator, we might have some other platform specific
474 * power control behind this translate function.
475 */
476 if (!host->vcc && host->plat->translate_vdd)
477 pwr |= host->plat->translate_vdd(mmc_dev(mmc), ios->vdd);
Linus Walleijcc30d602009-01-04 15:18:54 +0100478 /* The ST version does not have this, fall through to POWER_ON */
Linus Walleijf17a1f02009-08-04 01:01:02 +0100479 if (host->hw_designer != AMBA_VENDOR_ST) {
Linus Walleijcc30d602009-01-04 15:18:54 +0100480 pwr |= MCI_PWR_UP;
481 break;
482 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700483 case MMC_POWER_ON:
484 pwr |= MCI_PWR_ON;
485 break;
486 }
487
Linus Walleijcc30d602009-01-04 15:18:54 +0100488 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
Linus Walleijf17a1f02009-08-04 01:01:02 +0100489 if (host->hw_designer != AMBA_VENDOR_ST)
Linus Walleijcc30d602009-01-04 15:18:54 +0100490 pwr |= MCI_ROD;
491 else {
492 /*
493 * The ST Micro variant use the ROD bit for something
494 * else and only has OD (Open Drain).
495 */
496 pwr |= MCI_OD;
497 }
498 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700499
Linus Walleija6a64642009-09-14 12:56:14 +0100500 spin_lock_irqsave(&host->lock, flags);
501
502 mmci_set_clkreg(host, ios->clock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503
504 if (host->pwr != pwr) {
505 host->pwr = pwr;
506 writel(pwr, host->base + MMCIPOWER);
507 }
Linus Walleija6a64642009-09-14 12:56:14 +0100508
509 spin_unlock_irqrestore(&host->lock, flags);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700510}
511
Russell King89001442009-07-09 15:16:07 +0100512static int mmci_get_ro(struct mmc_host *mmc)
513{
514 struct mmci_host *host = mmc_priv(mmc);
515
516 if (host->gpio_wp == -ENOSYS)
517 return -ENOSYS;
518
519 return gpio_get_value(host->gpio_wp);
520}
521
522static int mmci_get_cd(struct mmc_host *mmc)
523{
524 struct mmci_host *host = mmc_priv(mmc);
525 unsigned int status;
526
527 if (host->gpio_cd == -ENOSYS)
528 status = host->plat->status(mmc_dev(host->mmc));
529 else
530 status = gpio_get_value(host->gpio_cd);
531
532 return !status;
533}
534
David Brownellab7aefd2006-11-12 17:55:30 -0800535static const struct mmc_host_ops mmci_ops = {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536 .request = mmci_request,
537 .set_ios = mmci_set_ios,
Russell King89001442009-07-09 15:16:07 +0100538 .get_ro = mmci_get_ro,
539 .get_cd = mmci_get_cd,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540};
541
542static void mmci_check_status(unsigned long data)
543{
544 struct mmci_host *host = (struct mmci_host *)data;
Russell King89001442009-07-09 15:16:07 +0100545 unsigned int status = mmci_get_cd(host->mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 if (status ^ host->oldstat)
Richard Purdie8dc00332005-09-08 17:53:01 +0100548 mmc_detect_change(host->mmc, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549
550 host->oldstat = status;
551 mod_timer(&host->timer, jiffies + HZ);
552}
553
Alessandro Rubini03fbdb12009-05-20 22:39:08 +0100554static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555{
Linus Walleij6ef297f2009-09-22 14:29:36 +0100556 struct mmci_platform_data *plat = dev->dev.platform_data;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700557 struct mmci_host *host;
558 struct mmc_host *mmc;
559 int ret;
560
561 /* must have platform data */
562 if (!plat) {
563 ret = -EINVAL;
564 goto out;
565 }
566
567 ret = amba_request_regions(dev, DRIVER_NAME);
568 if (ret)
569 goto out;
570
571 mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
572 if (!mmc) {
573 ret = -ENOMEM;
574 goto rel_regions;
575 }
576
577 host = mmc_priv(mmc);
Rabin Vincent4ea580f2009-04-17 08:44:19 +0530578 host->mmc = mmc;
Russell King012b7d32009-07-09 15:13:56 +0100579
Russell King89001442009-07-09 15:16:07 +0100580 host->gpio_wp = -ENOSYS;
581 host->gpio_cd = -ENOSYS;
582
Russell King012b7d32009-07-09 15:13:56 +0100583 host->hw_designer = amba_manf(dev);
584 host->hw_revision = amba_rev(dev);
Linus Walleijcc30d602009-01-04 15:18:54 +0100585 DBG(host, "designer ID = 0x%02x\n", host->hw_designer);
586 DBG(host, "revision = 0x%01x\n", host->hw_revision);
Russell King012b7d32009-07-09 15:13:56 +0100587
Russell Kingee569c42008-11-30 17:38:14 +0000588 host->clk = clk_get(&dev->dev, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700589 if (IS_ERR(host->clk)) {
590 ret = PTR_ERR(host->clk);
591 host->clk = NULL;
592 goto host_free;
593 }
594
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595 ret = clk_enable(host->clk);
596 if (ret)
Russell Kinga8d35842006-01-03 18:41:37 +0000597 goto clk_free;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700598
599 host->plat = plat;
600 host->mclk = clk_get_rate(host->clk);
Linus Walleijc8df9a52008-04-29 09:34:07 +0100601 /*
602 * According to the spec, mclk is max 100 MHz,
603 * so we try to adjust the clock down to this,
604 * (if possible).
605 */
606 if (host->mclk > 100000000) {
607 ret = clk_set_rate(host->clk, 100000000);
608 if (ret < 0)
609 goto clk_disable;
610 host->mclk = clk_get_rate(host->clk);
611 DBG(host, "eventual mclk rate: %u Hz\n", host->mclk);
612 }
Linus Walleijdc890c22009-06-07 23:27:31 +0100613 host->base = ioremap(dev->res.start, resource_size(&dev->res));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 if (!host->base) {
615 ret = -ENOMEM;
616 goto clk_disable;
617 }
618
619 mmc->ops = &mmci_ops;
620 mmc->f_min = (host->mclk + 511) / 512;
621 mmc->f_max = min(host->mclk, fmax);
Linus Walleij34e84f32009-09-22 14:41:40 +0100622#ifdef CONFIG_REGULATOR
623 /* If we're using the regulator framework, try to fetch a regulator */
624 host->vcc = regulator_get(&dev->dev, "vmmc");
625 if (IS_ERR(host->vcc))
626 host->vcc = NULL;
627 else {
628 int mask = mmc_regulator_get_ocrmask(host->vcc);
629
630 if (mask < 0)
631 dev_err(&dev->dev, "error getting OCR mask (%d)\n",
632 mask);
633 else {
634 host->mmc->ocr_avail = (u32) mask;
635 if (plat->ocr_mask)
636 dev_warn(&dev->dev,
637 "Provided ocr_mask/setpower will not be used "
638 "(using regulator instead)\n");
639 }
640 }
641#endif
642 /* Fall back to platform data if no regulator is found */
643 if (host->vcc == NULL)
644 mmc->ocr_avail = plat->ocr_mask;
Linus Walleij9e6c82c2009-09-14 12:57:11 +0100645 mmc->caps = plat->capabilities;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700646
647 /*
648 * We can do SGIO
649 */
650 mmc->max_hw_segs = 16;
651 mmc->max_phys_segs = NR_SG;
652
653 /*
654 * Since we only have a 16-bit data length register, we must
655 * ensure that we don't exceed 2^16-1 bytes in a single request.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700656 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100657 mmc->max_req_size = 65535;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658
659 /*
660 * Set the maximum segment size. Since we aren't doing DMA
661 * (yet) we are only limited by the data length register.
662 */
Pierre Ossman55db8902006-11-21 17:55:45 +0100663 mmc->max_seg_size = mmc->max_req_size;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664
Pierre Ossmanfe4a3c72006-11-21 17:54:23 +0100665 /*
666 * Block size can be up to 2048 bytes, but must be a power of two.
667 */
668 mmc->max_blk_size = 2048;
669
Pierre Ossman55db8902006-11-21 17:55:45 +0100670 /*
671 * No limit on the number of blocks transferred.
672 */
673 mmc->max_blk_count = mmc->max_req_size;
674
Linus Torvalds1da177e2005-04-16 15:20:36 -0700675 spin_lock_init(&host->lock);
676
677 writel(0, host->base + MMCIMASK0);
678 writel(0, host->base + MMCIMASK1);
679 writel(0xfff, host->base + MMCICLEAR);
680
Linus Walleij7064d202009-08-26 09:58:24 +0100681#ifdef CONFIG_GPIOLIB
Russell King89001442009-07-09 15:16:07 +0100682 if (gpio_is_valid(plat->gpio_cd)) {
683 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
684 if (ret == 0)
685 ret = gpio_direction_input(plat->gpio_cd);
686 if (ret == 0)
687 host->gpio_cd = plat->gpio_cd;
688 else if (ret != -ENOSYS)
689 goto err_gpio_cd;
690 }
691 if (gpio_is_valid(plat->gpio_wp)) {
692 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
693 if (ret == 0)
694 ret = gpio_direction_input(plat->gpio_wp);
695 if (ret == 0)
696 host->gpio_wp = plat->gpio_wp;
697 else if (ret != -ENOSYS)
698 goto err_gpio_wp;
699 }
Linus Walleij7064d202009-08-26 09:58:24 +0100700#endif
Russell King89001442009-07-09 15:16:07 +0100701
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700702 ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 if (ret)
704 goto unmap;
705
Thomas Gleixnerdace1452006-07-01 19:29:38 -0700706 ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 if (ret)
708 goto irq0_free;
709
710 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
711
712 amba_set_drvdata(dev, mmc);
Russell King89001442009-07-09 15:16:07 +0100713 host->oldstat = mmci_get_cd(host->mmc);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700714
715 mmc_add_host(mmc);
716
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700717 printk(KERN_INFO "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
Russell Kingd366b642005-08-19 09:40:08 +0100718 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
Greg Kroah-Hartmane29419f2006-06-12 15:20:16 -0700719 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700720
721 init_timer(&host->timer);
722 host->timer.data = (unsigned long)host;
723 host->timer.function = mmci_check_status;
724 host->timer.expires = jiffies + HZ;
725 add_timer(&host->timer);
726
727 return 0;
728
729 irq0_free:
730 free_irq(dev->irq[0], host);
731 unmap:
Russell King89001442009-07-09 15:16:07 +0100732 if (host->gpio_wp != -ENOSYS)
733 gpio_free(host->gpio_wp);
734 err_gpio_wp:
735 if (host->gpio_cd != -ENOSYS)
736 gpio_free(host->gpio_cd);
737 err_gpio_cd:
Linus Torvalds1da177e2005-04-16 15:20:36 -0700738 iounmap(host->base);
739 clk_disable:
740 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700741 clk_free:
742 clk_put(host->clk);
743 host_free:
744 mmc_free_host(mmc);
745 rel_regions:
746 amba_release_regions(dev);
747 out:
748 return ret;
749}
750
Linus Walleij6dc4a472009-03-07 00:23:52 +0100751static int __devexit mmci_remove(struct amba_device *dev)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752{
753 struct mmc_host *mmc = amba_get_drvdata(dev);
754
755 amba_set_drvdata(dev, NULL);
756
757 if (mmc) {
758 struct mmci_host *host = mmc_priv(mmc);
759
760 del_timer_sync(&host->timer);
761
762 mmc_remove_host(mmc);
763
764 writel(0, host->base + MMCIMASK0);
765 writel(0, host->base + MMCIMASK1);
766
767 writel(0, host->base + MMCICOMMAND);
768 writel(0, host->base + MMCIDATACTRL);
769
770 free_irq(dev->irq[0], host);
771 free_irq(dev->irq[1], host);
772
Russell King89001442009-07-09 15:16:07 +0100773 if (host->gpio_wp != -ENOSYS)
774 gpio_free(host->gpio_wp);
775 if (host->gpio_cd != -ENOSYS)
776 gpio_free(host->gpio_cd);
777
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 iounmap(host->base);
779 clk_disable(host->clk);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 clk_put(host->clk);
781
Linus Walleij34e84f32009-09-22 14:41:40 +0100782 if (regulator_is_enabled(host->vcc))
783 regulator_disable(host->vcc);
784 regulator_put(host->vcc);
785
Linus Torvalds1da177e2005-04-16 15:20:36 -0700786 mmc_free_host(mmc);
787
788 amba_release_regions(dev);
789 }
790
791 return 0;
792}
793
794#ifdef CONFIG_PM
Pavel Macheke5378ca2005-04-16 15:25:29 -0700795static int mmci_suspend(struct amba_device *dev, pm_message_t state)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796{
797 struct mmc_host *mmc = amba_get_drvdata(dev);
798 int ret = 0;
799
800 if (mmc) {
801 struct mmci_host *host = mmc_priv(mmc);
802
803 ret = mmc_suspend_host(mmc, state);
804 if (ret == 0)
805 writel(0, host->base + MMCIMASK0);
806 }
807
808 return ret;
809}
810
811static int mmci_resume(struct amba_device *dev)
812{
813 struct mmc_host *mmc = amba_get_drvdata(dev);
814 int ret = 0;
815
816 if (mmc) {
817 struct mmci_host *host = mmc_priv(mmc);
818
819 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
820
821 ret = mmc_resume_host(mmc);
822 }
823
824 return ret;
825}
826#else
827#define mmci_suspend NULL
828#define mmci_resume NULL
829#endif
830
831static struct amba_id mmci_ids[] = {
832 {
833 .id = 0x00041180,
834 .mask = 0x000fffff,
835 },
836 {
837 .id = 0x00041181,
838 .mask = 0x000fffff,
839 },
Linus Walleijcc30d602009-01-04 15:18:54 +0100840 /* ST Micro variants */
841 {
842 .id = 0x00180180,
843 .mask = 0x00ffffff,
844 },
845 {
846 .id = 0x00280180,
847 .mask = 0x00ffffff,
848 },
Linus Torvalds1da177e2005-04-16 15:20:36 -0700849 { 0, 0 },
850};
851
852static struct amba_driver mmci_driver = {
853 .drv = {
854 .name = DRIVER_NAME,
855 },
856 .probe = mmci_probe,
Linus Walleij6dc4a472009-03-07 00:23:52 +0100857 .remove = __devexit_p(mmci_remove),
Linus Torvalds1da177e2005-04-16 15:20:36 -0700858 .suspend = mmci_suspend,
859 .resume = mmci_resume,
860 .id_table = mmci_ids,
861};
862
863static int __init mmci_init(void)
864{
865 return amba_driver_register(&mmci_driver);
866}
867
868static void __exit mmci_exit(void)
869{
870 amba_driver_unregister(&mmci_driver);
871}
872
873module_init(mmci_init);
874module_exit(mmci_exit);
875module_param(fmax, uint, 0444);
876
877MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
878MODULE_LICENSE("GPL");