Merge git://git.kernel.org/pub/scm/linux/kernel/git/lethal/sh-2.6
[pandora-kernel.git] / drivers / mmc / host / mmci.c
1 /*
2  *  linux/drivers/mmc/host/mmci.c - ARM PrimeCell MMCI PL180/1 driver
3  *
4  *  Copyright (C) 2003 Deep Blue Solutions, Ltd, All Rights Reserved.
5  *  Copyright (C) 2010 ST-Ericsson AB.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License version 2 as
9  * published by the Free Software Foundation.
10  */
11 #include <linux/module.h>
12 #include <linux/moduleparam.h>
13 #include <linux/init.h>
14 #include <linux/ioport.h>
15 #include <linux/device.h>
16 #include <linux/interrupt.h>
17 #include <linux/delay.h>
18 #include <linux/err.h>
19 #include <linux/highmem.h>
20 #include <linux/log2.h>
21 #include <linux/mmc/host.h>
22 #include <linux/amba/bus.h>
23 #include <linux/clk.h>
24 #include <linux/scatterlist.h>
25 #include <linux/gpio.h>
26 #include <linux/amba/mmci.h>
27 #include <linux/regulator/consumer.h>
28
29 #include <asm/div64.h>
30 #include <asm/io.h>
31 #include <asm/sizes.h>
32
33 #include "mmci.h"
34
35 #define DRIVER_NAME "mmci-pl18x"
36
37 static unsigned int fmax = 515633;
38
39 /**
40  * struct variant_data - MMCI variant-specific quirks
41  * @clkreg: default value for MCICLOCK register
42  * @clkreg_enable: enable value for MMCICLOCK register
43  * @datalength_bits: number of bits in the MMCIDATALENGTH register
44  * @fifosize: number of bytes that can be written when MMCI_TXFIFOEMPTY
45  *            is asserted (likewise for RX)
46  * @fifohalfsize: number of bytes that can be written when MCI_TXFIFOHALFEMPTY
47  *                is asserted (likewise for RX)
48  */
49 struct variant_data {
50         unsigned int            clkreg;
51         unsigned int            clkreg_enable;
52         unsigned int            datalength_bits;
53         unsigned int            fifosize;
54         unsigned int            fifohalfsize;
55 };
56
57 static struct variant_data variant_arm = {
58         .fifosize               = 16 * 4,
59         .fifohalfsize           = 8 * 4,
60         .datalength_bits        = 16,
61 };
62
63 static struct variant_data variant_u300 = {
64         .fifosize               = 16 * 4,
65         .fifohalfsize           = 8 * 4,
66         .clkreg_enable          = 1 << 13, /* HWFCEN */
67         .datalength_bits        = 16,
68 };
69
70 static struct variant_data variant_ux500 = {
71         .fifosize               = 30 * 4,
72         .fifohalfsize           = 8 * 4,
73         .clkreg                 = MCI_CLK_ENABLE,
74         .clkreg_enable          = 1 << 14, /* HWFCEN */
75         .datalength_bits        = 24,
76 };
77 /*
78  * This must be called with host->lock held
79  */
80 static void mmci_set_clkreg(struct mmci_host *host, unsigned int desired)
81 {
82         struct variant_data *variant = host->variant;
83         u32 clk = variant->clkreg;
84
85         if (desired) {
86                 if (desired >= host->mclk) {
87                         clk = MCI_CLK_BYPASS;
88                         host->cclk = host->mclk;
89                 } else {
90                         clk = host->mclk / (2 * desired) - 1;
91                         if (clk >= 256)
92                                 clk = 255;
93                         host->cclk = host->mclk / (2 * (clk + 1));
94                 }
95
96                 clk |= variant->clkreg_enable;
97                 clk |= MCI_CLK_ENABLE;
98                 /* This hasn't proven to be worthwhile */
99                 /* clk |= MCI_CLK_PWRSAVE; */
100         }
101
102         if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4)
103                 clk |= MCI_4BIT_BUS;
104         if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_8)
105                 clk |= MCI_ST_8BIT_BUS;
106
107         writel(clk, host->base + MMCICLOCK);
108 }
109
110 static void
111 mmci_request_end(struct mmci_host *host, struct mmc_request *mrq)
112 {
113         writel(0, host->base + MMCICOMMAND);
114
115         BUG_ON(host->data);
116
117         host->mrq = NULL;
118         host->cmd = NULL;
119
120         if (mrq->data)
121                 mrq->data->bytes_xfered = host->data_xfered;
122
123         /*
124          * Need to drop the host lock here; mmc_request_done may call
125          * back into the driver...
126          */
127         spin_unlock(&host->lock);
128         mmc_request_done(host->mmc, mrq);
129         spin_lock(&host->lock);
130 }
131
132 static void mmci_stop_data(struct mmci_host *host)
133 {
134         writel(0, host->base + MMCIDATACTRL);
135         writel(0, host->base + MMCIMASK1);
136         host->data = NULL;
137 }
138
139 static void mmci_init_sg(struct mmci_host *host, struct mmc_data *data)
140 {
141         unsigned int flags = SG_MITER_ATOMIC;
142
143         if (data->flags & MMC_DATA_READ)
144                 flags |= SG_MITER_TO_SG;
145         else
146                 flags |= SG_MITER_FROM_SG;
147
148         sg_miter_start(&host->sg_miter, data->sg, data->sg_len, flags);
149 }
150
151 static void mmci_start_data(struct mmci_host *host, struct mmc_data *data)
152 {
153         struct variant_data *variant = host->variant;
154         unsigned int datactrl, timeout, irqmask;
155         unsigned long long clks;
156         void __iomem *base;
157         int blksz_bits;
158
159         dev_dbg(mmc_dev(host->mmc), "blksz %04x blks %04x flags %08x\n",
160                 data->blksz, data->blocks, data->flags);
161
162         host->data = data;
163         host->size = data->blksz * data->blocks;
164         host->data_xfered = 0;
165
166         mmci_init_sg(host, data);
167
168         clks = (unsigned long long)data->timeout_ns * host->cclk;
169         do_div(clks, 1000000000UL);
170
171         timeout = data->timeout_clks + (unsigned int)clks;
172
173         base = host->base;
174         writel(timeout, base + MMCIDATATIMER);
175         writel(host->size, base + MMCIDATALENGTH);
176
177         blksz_bits = ffs(data->blksz) - 1;
178         BUG_ON(1 << blksz_bits != data->blksz);
179
180         datactrl = MCI_DPSM_ENABLE | blksz_bits << 4;
181         if (data->flags & MMC_DATA_READ) {
182                 datactrl |= MCI_DPSM_DIRECTION;
183                 irqmask = MCI_RXFIFOHALFFULLMASK;
184
185                 /*
186                  * If we have less than a FIFOSIZE of bytes to transfer,
187                  * trigger a PIO interrupt as soon as any data is available.
188                  */
189                 if (host->size < variant->fifosize)
190                         irqmask |= MCI_RXDATAAVLBLMASK;
191         } else {
192                 /*
193                  * We don't actually need to include "FIFO empty" here
194                  * since its implicit in "FIFO half empty".
195                  */
196                 irqmask = MCI_TXFIFOHALFEMPTYMASK;
197         }
198
199         writel(datactrl, base + MMCIDATACTRL);
200         writel(readl(base + MMCIMASK0) & ~MCI_DATAENDMASK, base + MMCIMASK0);
201         writel(irqmask, base + MMCIMASK1);
202 }
203
204 static void
205 mmci_start_command(struct mmci_host *host, struct mmc_command *cmd, u32 c)
206 {
207         void __iomem *base = host->base;
208
209         dev_dbg(mmc_dev(host->mmc), "op %02x arg %08x flags %08x\n",
210             cmd->opcode, cmd->arg, cmd->flags);
211
212         if (readl(base + MMCICOMMAND) & MCI_CPSM_ENABLE) {
213                 writel(0, base + MMCICOMMAND);
214                 udelay(1);
215         }
216
217         c |= cmd->opcode | MCI_CPSM_ENABLE;
218         if (cmd->flags & MMC_RSP_PRESENT) {
219                 if (cmd->flags & MMC_RSP_136)
220                         c |= MCI_CPSM_LONGRSP;
221                 c |= MCI_CPSM_RESPONSE;
222         }
223         if (/*interrupt*/0)
224                 c |= MCI_CPSM_INTERRUPT;
225
226         host->cmd = cmd;
227
228         writel(cmd->arg, base + MMCIARGUMENT);
229         writel(c, base + MMCICOMMAND);
230 }
231
232 static void
233 mmci_data_irq(struct mmci_host *host, struct mmc_data *data,
234               unsigned int status)
235 {
236         if (status & MCI_DATABLOCKEND) {
237                 host->data_xfered += data->blksz;
238 #ifdef CONFIG_ARCH_U300
239                 /*
240                  * On the U300 some signal or other is
241                  * badly routed so that a data write does
242                  * not properly terminate with a MCI_DATAEND
243                  * status flag. This quirk will make writes
244                  * work again.
245                  */
246                 if (data->flags & MMC_DATA_WRITE)
247                         status |= MCI_DATAEND;
248 #endif
249         }
250         if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|MCI_RXOVERRUN)) {
251                 dev_dbg(mmc_dev(host->mmc), "MCI ERROR IRQ (status %08x)\n", status);
252                 if (status & MCI_DATACRCFAIL)
253                         data->error = -EILSEQ;
254                 else if (status & MCI_DATATIMEOUT)
255                         data->error = -ETIMEDOUT;
256                 else if (status & (MCI_TXUNDERRUN|MCI_RXOVERRUN))
257                         data->error = -EIO;
258                 status |= MCI_DATAEND;
259
260                 /*
261                  * We hit an error condition.  Ensure that any data
262                  * partially written to a page is properly coherent.
263                  */
264                 if (data->flags & MMC_DATA_READ) {
265                         struct sg_mapping_iter *sg_miter = &host->sg_miter;
266                         unsigned long flags;
267
268                         local_irq_save(flags);
269                         if (sg_miter_next(sg_miter)) {
270                                 flush_dcache_page(sg_miter->page);
271                                 sg_miter_stop(sg_miter);
272                         }
273                         local_irq_restore(flags);
274                 }
275         }
276         if (status & MCI_DATAEND) {
277                 mmci_stop_data(host);
278
279                 if (!data->stop) {
280                         mmci_request_end(host, data->mrq);
281                 } else {
282                         mmci_start_command(host, data->stop, 0);
283                 }
284         }
285 }
286
287 static void
288 mmci_cmd_irq(struct mmci_host *host, struct mmc_command *cmd,
289              unsigned int status)
290 {
291         void __iomem *base = host->base;
292
293         host->cmd = NULL;
294
295         cmd->resp[0] = readl(base + MMCIRESPONSE0);
296         cmd->resp[1] = readl(base + MMCIRESPONSE1);
297         cmd->resp[2] = readl(base + MMCIRESPONSE2);
298         cmd->resp[3] = readl(base + MMCIRESPONSE3);
299
300         if (status & MCI_CMDTIMEOUT) {
301                 cmd->error = -ETIMEDOUT;
302         } else if (status & MCI_CMDCRCFAIL && cmd->flags & MMC_RSP_CRC) {
303                 cmd->error = -EILSEQ;
304         }
305
306         if (!cmd->data || cmd->error) {
307                 if (host->data)
308                         mmci_stop_data(host);
309                 mmci_request_end(host, cmd->mrq);
310         } else if (!(cmd->data->flags & MMC_DATA_READ)) {
311                 mmci_start_data(host, cmd->data);
312         }
313 }
314
315 static int mmci_pio_read(struct mmci_host *host, char *buffer, unsigned int remain)
316 {
317         void __iomem *base = host->base;
318         char *ptr = buffer;
319         u32 status;
320         int host_remain = host->size;
321
322         do {
323                 int count = host_remain - (readl(base + MMCIFIFOCNT) << 2);
324
325                 if (count > remain)
326                         count = remain;
327
328                 if (count <= 0)
329                         break;
330
331                 readsl(base + MMCIFIFO, ptr, count >> 2);
332
333                 ptr += count;
334                 remain -= count;
335                 host_remain -= count;
336
337                 if (remain == 0)
338                         break;
339
340                 status = readl(base + MMCISTATUS);
341         } while (status & MCI_RXDATAAVLBL);
342
343         return ptr - buffer;
344 }
345
346 static int mmci_pio_write(struct mmci_host *host, char *buffer, unsigned int remain, u32 status)
347 {
348         struct variant_data *variant = host->variant;
349         void __iomem *base = host->base;
350         char *ptr = buffer;
351
352         do {
353                 unsigned int count, maxcnt;
354
355                 maxcnt = status & MCI_TXFIFOEMPTY ?
356                          variant->fifosize : variant->fifohalfsize;
357                 count = min(remain, maxcnt);
358
359                 writesl(base + MMCIFIFO, ptr, count >> 2);
360
361                 ptr += count;
362                 remain -= count;
363
364                 if (remain == 0)
365                         break;
366
367                 status = readl(base + MMCISTATUS);
368         } while (status & MCI_TXFIFOHALFEMPTY);
369
370         return ptr - buffer;
371 }
372
373 /*
374  * PIO data transfer IRQ handler.
375  */
376 static irqreturn_t mmci_pio_irq(int irq, void *dev_id)
377 {
378         struct mmci_host *host = dev_id;
379         struct sg_mapping_iter *sg_miter = &host->sg_miter;
380         struct variant_data *variant = host->variant;
381         void __iomem *base = host->base;
382         unsigned long flags;
383         u32 status;
384
385         status = readl(base + MMCISTATUS);
386
387         dev_dbg(mmc_dev(host->mmc), "irq1 (pio) %08x\n", status);
388
389         local_irq_save(flags);
390
391         do {
392                 unsigned int remain, len;
393                 char *buffer;
394
395                 /*
396                  * For write, we only need to test the half-empty flag
397                  * here - if the FIFO is completely empty, then by
398                  * definition it is more than half empty.
399                  *
400                  * For read, check for data available.
401                  */
402                 if (!(status & (MCI_TXFIFOHALFEMPTY|MCI_RXDATAAVLBL)))
403                         break;
404
405                 if (!sg_miter_next(sg_miter))
406                         break;
407
408                 buffer = sg_miter->addr;
409                 remain = sg_miter->length;
410
411                 len = 0;
412                 if (status & MCI_RXACTIVE)
413                         len = mmci_pio_read(host, buffer, remain);
414                 if (status & MCI_TXACTIVE)
415                         len = mmci_pio_write(host, buffer, remain, status);
416
417                 sg_miter->consumed = len;
418
419                 host->size -= len;
420                 remain -= len;
421
422                 if (remain)
423                         break;
424
425                 if (status & MCI_RXACTIVE)
426                         flush_dcache_page(sg_miter->page);
427
428                 status = readl(base + MMCISTATUS);
429         } while (1);
430
431         sg_miter_stop(sg_miter);
432
433         local_irq_restore(flags);
434
435         /*
436          * If we're nearing the end of the read, switch to
437          * "any data available" mode.
438          */
439         if (status & MCI_RXACTIVE && host->size < variant->fifosize)
440                 writel(MCI_RXDATAAVLBLMASK, base + MMCIMASK1);
441
442         /*
443          * If we run out of data, disable the data IRQs; this
444          * prevents a race where the FIFO becomes empty before
445          * the chip itself has disabled the data path, and
446          * stops us racing with our data end IRQ.
447          */
448         if (host->size == 0) {
449                 writel(0, base + MMCIMASK1);
450                 writel(readl(base + MMCIMASK0) | MCI_DATAENDMASK, base + MMCIMASK0);
451         }
452
453         return IRQ_HANDLED;
454 }
455
456 /*
457  * Handle completion of command and data transfers.
458  */
459 static irqreturn_t mmci_irq(int irq, void *dev_id)
460 {
461         struct mmci_host *host = dev_id;
462         u32 status;
463         int ret = 0;
464
465         spin_lock(&host->lock);
466
467         do {
468                 struct mmc_command *cmd;
469                 struct mmc_data *data;
470
471                 status = readl(host->base + MMCISTATUS);
472                 status &= readl(host->base + MMCIMASK0);
473                 writel(status, host->base + MMCICLEAR);
474
475                 dev_dbg(mmc_dev(host->mmc), "irq0 (data+cmd) %08x\n", status);
476
477                 data = host->data;
478                 if (status & (MCI_DATACRCFAIL|MCI_DATATIMEOUT|MCI_TXUNDERRUN|
479                               MCI_RXOVERRUN|MCI_DATAEND|MCI_DATABLOCKEND) && data)
480                         mmci_data_irq(host, data, status);
481
482                 cmd = host->cmd;
483                 if (status & (MCI_CMDCRCFAIL|MCI_CMDTIMEOUT|MCI_CMDSENT|MCI_CMDRESPEND) && cmd)
484                         mmci_cmd_irq(host, cmd, status);
485
486                 ret = 1;
487         } while (status);
488
489         spin_unlock(&host->lock);
490
491         return IRQ_RETVAL(ret);
492 }
493
494 static void mmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
495 {
496         struct mmci_host *host = mmc_priv(mmc);
497         unsigned long flags;
498
499         WARN_ON(host->mrq != NULL);
500
501         if (mrq->data && !is_power_of_2(mrq->data->blksz)) {
502                 dev_err(mmc_dev(mmc), "unsupported block size (%d bytes)\n",
503                         mrq->data->blksz);
504                 mrq->cmd->error = -EINVAL;
505                 mmc_request_done(mmc, mrq);
506                 return;
507         }
508
509         spin_lock_irqsave(&host->lock, flags);
510
511         host->mrq = mrq;
512
513         if (mrq->data && mrq->data->flags & MMC_DATA_READ)
514                 mmci_start_data(host, mrq->data);
515
516         mmci_start_command(host, mrq->cmd, 0);
517
518         spin_unlock_irqrestore(&host->lock, flags);
519 }
520
521 static void mmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
522 {
523         struct mmci_host *host = mmc_priv(mmc);
524         u32 pwr = 0;
525         unsigned long flags;
526         int ret;
527
528         switch (ios->power_mode) {
529         case MMC_POWER_OFF:
530                 if (host->vcc)
531                         ret = mmc_regulator_set_ocr(mmc, host->vcc, 0);
532                 break;
533         case MMC_POWER_UP:
534                 if (host->vcc) {
535                         ret = mmc_regulator_set_ocr(mmc, host->vcc, ios->vdd);
536                         if (ret) {
537                                 dev_err(mmc_dev(mmc), "unable to set OCR\n");
538                                 /*
539                                  * The .set_ios() function in the mmc_host_ops
540                                  * struct return void, and failing to set the
541                                  * power should be rare so we print an error
542                                  * and return here.
543                                  */
544                                 return;
545                         }
546                 }
547                 if (host->plat->vdd_handler)
548                         pwr |= host->plat->vdd_handler(mmc_dev(mmc), ios->vdd,
549                                                        ios->power_mode);
550                 /* The ST version does not have this, fall through to POWER_ON */
551                 if (host->hw_designer != AMBA_VENDOR_ST) {
552                         pwr |= MCI_PWR_UP;
553                         break;
554                 }
555         case MMC_POWER_ON:
556                 pwr |= MCI_PWR_ON;
557                 break;
558         }
559
560         if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN) {
561                 if (host->hw_designer != AMBA_VENDOR_ST)
562                         pwr |= MCI_ROD;
563                 else {
564                         /*
565                          * The ST Micro variant use the ROD bit for something
566                          * else and only has OD (Open Drain).
567                          */
568                         pwr |= MCI_OD;
569                 }
570         }
571
572         spin_lock_irqsave(&host->lock, flags);
573
574         mmci_set_clkreg(host, ios->clock);
575
576         if (host->pwr != pwr) {
577                 host->pwr = pwr;
578                 writel(pwr, host->base + MMCIPOWER);
579         }
580
581         spin_unlock_irqrestore(&host->lock, flags);
582 }
583
584 static int mmci_get_ro(struct mmc_host *mmc)
585 {
586         struct mmci_host *host = mmc_priv(mmc);
587
588         if (host->gpio_wp == -ENOSYS)
589                 return -ENOSYS;
590
591         return gpio_get_value_cansleep(host->gpio_wp);
592 }
593
594 static int mmci_get_cd(struct mmc_host *mmc)
595 {
596         struct mmci_host *host = mmc_priv(mmc);
597         struct mmci_platform_data *plat = host->plat;
598         unsigned int status;
599
600         if (host->gpio_cd == -ENOSYS) {
601                 if (!plat->status)
602                         return 1; /* Assume always present */
603
604                 status = plat->status(mmc_dev(host->mmc));
605         } else
606                 status = !!gpio_get_value_cansleep(host->gpio_cd)
607                         ^ plat->cd_invert;
608
609         /*
610          * Use positive logic throughout - status is zero for no card,
611          * non-zero for card inserted.
612          */
613         return status;
614 }
615
616 static irqreturn_t mmci_cd_irq(int irq, void *dev_id)
617 {
618         struct mmci_host *host = dev_id;
619
620         mmc_detect_change(host->mmc, msecs_to_jiffies(500));
621
622         return IRQ_HANDLED;
623 }
624
625 static const struct mmc_host_ops mmci_ops = {
626         .request        = mmci_request,
627         .set_ios        = mmci_set_ios,
628         .get_ro         = mmci_get_ro,
629         .get_cd         = mmci_get_cd,
630 };
631
632 static int __devinit mmci_probe(struct amba_device *dev, struct amba_id *id)
633 {
634         struct mmci_platform_data *plat = dev->dev.platform_data;
635         struct variant_data *variant = id->data;
636         struct mmci_host *host;
637         struct mmc_host *mmc;
638         int ret;
639
640         /* must have platform data */
641         if (!plat) {
642                 ret = -EINVAL;
643                 goto out;
644         }
645
646         ret = amba_request_regions(dev, DRIVER_NAME);
647         if (ret)
648                 goto out;
649
650         mmc = mmc_alloc_host(sizeof(struct mmci_host), &dev->dev);
651         if (!mmc) {
652                 ret = -ENOMEM;
653                 goto rel_regions;
654         }
655
656         host = mmc_priv(mmc);
657         host->mmc = mmc;
658
659         host->gpio_wp = -ENOSYS;
660         host->gpio_cd = -ENOSYS;
661         host->gpio_cd_irq = -1;
662
663         host->hw_designer = amba_manf(dev);
664         host->hw_revision = amba_rev(dev);
665         dev_dbg(mmc_dev(mmc), "designer ID = 0x%02x\n", host->hw_designer);
666         dev_dbg(mmc_dev(mmc), "revision = 0x%01x\n", host->hw_revision);
667
668         host->clk = clk_get(&dev->dev, NULL);
669         if (IS_ERR(host->clk)) {
670                 ret = PTR_ERR(host->clk);
671                 host->clk = NULL;
672                 goto host_free;
673         }
674
675         ret = clk_enable(host->clk);
676         if (ret)
677                 goto clk_free;
678
679         host->plat = plat;
680         host->variant = variant;
681         host->mclk = clk_get_rate(host->clk);
682         /*
683          * According to the spec, mclk is max 100 MHz,
684          * so we try to adjust the clock down to this,
685          * (if possible).
686          */
687         if (host->mclk > 100000000) {
688                 ret = clk_set_rate(host->clk, 100000000);
689                 if (ret < 0)
690                         goto clk_disable;
691                 host->mclk = clk_get_rate(host->clk);
692                 dev_dbg(mmc_dev(mmc), "eventual mclk rate: %u Hz\n",
693                         host->mclk);
694         }
695         host->base = ioremap(dev->res.start, resource_size(&dev->res));
696         if (!host->base) {
697                 ret = -ENOMEM;
698                 goto clk_disable;
699         }
700
701         mmc->ops = &mmci_ops;
702         mmc->f_min = (host->mclk + 511) / 512;
703         /*
704          * If the platform data supplies a maximum operating
705          * frequency, this takes precedence. Else, we fall back
706          * to using the module parameter, which has a (low)
707          * default value in case it is not specified. Either
708          * value must not exceed the clock rate into the block,
709          * of course.
710          */
711         if (plat->f_max)
712                 mmc->f_max = min(host->mclk, plat->f_max);
713         else
714                 mmc->f_max = min(host->mclk, fmax);
715         dev_dbg(mmc_dev(mmc), "clocking block at %u Hz\n", mmc->f_max);
716
717 #ifdef CONFIG_REGULATOR
718         /* If we're using the regulator framework, try to fetch a regulator */
719         host->vcc = regulator_get(&dev->dev, "vmmc");
720         if (IS_ERR(host->vcc))
721                 host->vcc = NULL;
722         else {
723                 int mask = mmc_regulator_get_ocrmask(host->vcc);
724
725                 if (mask < 0)
726                         dev_err(&dev->dev, "error getting OCR mask (%d)\n",
727                                 mask);
728                 else {
729                         host->mmc->ocr_avail = (u32) mask;
730                         if (plat->ocr_mask)
731                                 dev_warn(&dev->dev,
732                                  "Provided ocr_mask/setpower will not be used "
733                                  "(using regulator instead)\n");
734                 }
735         }
736 #endif
737         /* Fall back to platform data if no regulator is found */
738         if (host->vcc == NULL)
739                 mmc->ocr_avail = plat->ocr_mask;
740         mmc->caps = plat->capabilities;
741
742         /*
743          * We can do SGIO
744          */
745         mmc->max_segs = NR_SG;
746
747         /*
748          * Since only a certain number of bits are valid in the data length
749          * register, we must ensure that we don't exceed 2^num-1 bytes in a
750          * single request.
751          */
752         mmc->max_req_size = (1 << variant->datalength_bits) - 1;
753
754         /*
755          * Set the maximum segment size.  Since we aren't doing DMA
756          * (yet) we are only limited by the data length register.
757          */
758         mmc->max_seg_size = mmc->max_req_size;
759
760         /*
761          * Block size can be up to 2048 bytes, but must be a power of two.
762          */
763         mmc->max_blk_size = 2048;
764
765         /*
766          * No limit on the number of blocks transferred.
767          */
768         mmc->max_blk_count = mmc->max_req_size;
769
770         spin_lock_init(&host->lock);
771
772         writel(0, host->base + MMCIMASK0);
773         writel(0, host->base + MMCIMASK1);
774         writel(0xfff, host->base + MMCICLEAR);
775
776         if (gpio_is_valid(plat->gpio_cd)) {
777                 ret = gpio_request(plat->gpio_cd, DRIVER_NAME " (cd)");
778                 if (ret == 0)
779                         ret = gpio_direction_input(plat->gpio_cd);
780                 if (ret == 0)
781                         host->gpio_cd = plat->gpio_cd;
782                 else if (ret != -ENOSYS)
783                         goto err_gpio_cd;
784
785                 ret = request_any_context_irq(gpio_to_irq(plat->gpio_cd),
786                                               mmci_cd_irq, 0,
787                                               DRIVER_NAME " (cd)", host);
788                 if (ret >= 0)
789                         host->gpio_cd_irq = gpio_to_irq(plat->gpio_cd);
790         }
791         if (gpio_is_valid(plat->gpio_wp)) {
792                 ret = gpio_request(plat->gpio_wp, DRIVER_NAME " (wp)");
793                 if (ret == 0)
794                         ret = gpio_direction_input(plat->gpio_wp);
795                 if (ret == 0)
796                         host->gpio_wp = plat->gpio_wp;
797                 else if (ret != -ENOSYS)
798                         goto err_gpio_wp;
799         }
800
801         if ((host->plat->status || host->gpio_cd != -ENOSYS)
802             && host->gpio_cd_irq < 0)
803                 mmc->caps |= MMC_CAP_NEEDS_POLL;
804
805         ret = request_irq(dev->irq[0], mmci_irq, IRQF_SHARED, DRIVER_NAME " (cmd)", host);
806         if (ret)
807                 goto unmap;
808
809         ret = request_irq(dev->irq[1], mmci_pio_irq, IRQF_SHARED, DRIVER_NAME " (pio)", host);
810         if (ret)
811                 goto irq0_free;
812
813         writel(MCI_IRQENABLE, host->base + MMCIMASK0);
814
815         amba_set_drvdata(dev, mmc);
816
817         mmc_add_host(mmc);
818
819         dev_info(&dev->dev, "%s: MMCI rev %x cfg %02x at 0x%016llx irq %d,%d\n",
820                 mmc_hostname(mmc), amba_rev(dev), amba_config(dev),
821                 (unsigned long long)dev->res.start, dev->irq[0], dev->irq[1]);
822
823         return 0;
824
825  irq0_free:
826         free_irq(dev->irq[0], host);
827  unmap:
828         if (host->gpio_wp != -ENOSYS)
829                 gpio_free(host->gpio_wp);
830  err_gpio_wp:
831         if (host->gpio_cd_irq >= 0)
832                 free_irq(host->gpio_cd_irq, host);
833         if (host->gpio_cd != -ENOSYS)
834                 gpio_free(host->gpio_cd);
835  err_gpio_cd:
836         iounmap(host->base);
837  clk_disable:
838         clk_disable(host->clk);
839  clk_free:
840         clk_put(host->clk);
841  host_free:
842         mmc_free_host(mmc);
843  rel_regions:
844         amba_release_regions(dev);
845  out:
846         return ret;
847 }
848
849 static int __devexit mmci_remove(struct amba_device *dev)
850 {
851         struct mmc_host *mmc = amba_get_drvdata(dev);
852
853         amba_set_drvdata(dev, NULL);
854
855         if (mmc) {
856                 struct mmci_host *host = mmc_priv(mmc);
857
858                 mmc_remove_host(mmc);
859
860                 writel(0, host->base + MMCIMASK0);
861                 writel(0, host->base + MMCIMASK1);
862
863                 writel(0, host->base + MMCICOMMAND);
864                 writel(0, host->base + MMCIDATACTRL);
865
866                 free_irq(dev->irq[0], host);
867                 free_irq(dev->irq[1], host);
868
869                 if (host->gpio_wp != -ENOSYS)
870                         gpio_free(host->gpio_wp);
871                 if (host->gpio_cd_irq >= 0)
872                         free_irq(host->gpio_cd_irq, host);
873                 if (host->gpio_cd != -ENOSYS)
874                         gpio_free(host->gpio_cd);
875
876                 iounmap(host->base);
877                 clk_disable(host->clk);
878                 clk_put(host->clk);
879
880                 if (host->vcc)
881                         mmc_regulator_set_ocr(mmc, host->vcc, 0);
882                 regulator_put(host->vcc);
883
884                 mmc_free_host(mmc);
885
886                 amba_release_regions(dev);
887         }
888
889         return 0;
890 }
891
892 #ifdef CONFIG_PM
893 static int mmci_suspend(struct amba_device *dev, pm_message_t state)
894 {
895         struct mmc_host *mmc = amba_get_drvdata(dev);
896         int ret = 0;
897
898         if (mmc) {
899                 struct mmci_host *host = mmc_priv(mmc);
900
901                 ret = mmc_suspend_host(mmc);
902                 if (ret == 0)
903                         writel(0, host->base + MMCIMASK0);
904         }
905
906         return ret;
907 }
908
909 static int mmci_resume(struct amba_device *dev)
910 {
911         struct mmc_host *mmc = amba_get_drvdata(dev);
912         int ret = 0;
913
914         if (mmc) {
915                 struct mmci_host *host = mmc_priv(mmc);
916
917                 writel(MCI_IRQENABLE, host->base + MMCIMASK0);
918
919                 ret = mmc_resume_host(mmc);
920         }
921
922         return ret;
923 }
924 #else
925 #define mmci_suspend    NULL
926 #define mmci_resume     NULL
927 #endif
928
929 static struct amba_id mmci_ids[] = {
930         {
931                 .id     = 0x00041180,
932                 .mask   = 0x000fffff,
933                 .data   = &variant_arm,
934         },
935         {
936                 .id     = 0x00041181,
937                 .mask   = 0x000fffff,
938                 .data   = &variant_arm,
939         },
940         /* ST Micro variants */
941         {
942                 .id     = 0x00180180,
943                 .mask   = 0x00ffffff,
944                 .data   = &variant_u300,
945         },
946         {
947                 .id     = 0x00280180,
948                 .mask   = 0x00ffffff,
949                 .data   = &variant_u300,
950         },
951         {
952                 .id     = 0x00480180,
953                 .mask   = 0x00ffffff,
954                 .data   = &variant_ux500,
955         },
956         { 0, 0 },
957 };
958
959 static struct amba_driver mmci_driver = {
960         .drv            = {
961                 .name   = DRIVER_NAME,
962         },
963         .probe          = mmci_probe,
964         .remove         = __devexit_p(mmci_remove),
965         .suspend        = mmci_suspend,
966         .resume         = mmci_resume,
967         .id_table       = mmci_ids,
968 };
969
970 static int __init mmci_init(void)
971 {
972         return amba_driver_register(&mmci_driver);
973 }
974
975 static void __exit mmci_exit(void)
976 {
977         amba_driver_unregister(&mmci_driver);
978 }
979
980 module_init(mmci_init);
981 module_exit(mmci_exit);
982 module_param(fmax, uint, 0444);
983
984 MODULE_DESCRIPTION("ARM PrimeCell PL180/181 Multimedia Card Interface driver");
985 MODULE_LICENSE("GPL");