Pull fujitsu into release branch
[pandora-kernel.git] / drivers / mmc / host / pxamci.c
1 /*
2  *  linux/drivers/mmc/host/pxa.c - PXA MMCI driver
3  *
4  *  Copyright (C) 2003 Russell King, 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  *
10  *  This hardware is really sick:
11  *   - No way to clear interrupts.
12  *   - Have to turn off the clock whenever we touch the device.
13  *   - Doesn't tell you how many data blocks were transferred.
14  *  Yuck!
15  *
16  *      1 and 3 byte data transfers not supported
17  *      max block length up to 1023
18  */
19 #include <linux/module.h>
20 #include <linux/init.h>
21 #include <linux/ioport.h>
22 #include <linux/platform_device.h>
23 #include <linux/delay.h>
24 #include <linux/interrupt.h>
25 #include <linux/dma-mapping.h>
26 #include <linux/clk.h>
27 #include <linux/err.h>
28 #include <linux/mmc/host.h>
29
30 #include <asm/dma.h>
31 #include <asm/io.h>
32 #include <asm/sizes.h>
33
34 #include <asm/arch/pxa-regs.h>
35 #include <asm/arch/mmc.h>
36
37 #include "pxamci.h"
38
39 #define DRIVER_NAME     "pxa2xx-mci"
40
41 #define NR_SG   1
42
43 struct pxamci_host {
44         struct mmc_host         *mmc;
45         spinlock_t              lock;
46         struct resource         *res;
47         void __iomem            *base;
48         struct clk              *clk;
49         unsigned long           clkrate;
50         int                     irq;
51         int                     dma;
52         unsigned int            clkrt;
53         unsigned int            cmdat;
54         unsigned int            imask;
55         unsigned int            power_mode;
56         struct pxamci_platform_data *pdata;
57
58         struct mmc_request      *mrq;
59         struct mmc_command      *cmd;
60         struct mmc_data         *data;
61
62         dma_addr_t              sg_dma;
63         struct pxa_dma_desc     *sg_cpu;
64         unsigned int            dma_len;
65
66         unsigned int            dma_dir;
67 };
68
69 static void pxamci_stop_clock(struct pxamci_host *host)
70 {
71         if (readl(host->base + MMC_STAT) & STAT_CLK_EN) {
72                 unsigned long timeout = 10000;
73                 unsigned int v;
74
75                 writel(STOP_CLOCK, host->base + MMC_STRPCL);
76
77                 do {
78                         v = readl(host->base + MMC_STAT);
79                         if (!(v & STAT_CLK_EN))
80                                 break;
81                         udelay(1);
82                 } while (timeout--);
83
84                 if (v & STAT_CLK_EN)
85                         dev_err(mmc_dev(host->mmc), "unable to stop clock\n");
86         }
87 }
88
89 static void pxamci_enable_irq(struct pxamci_host *host, unsigned int mask)
90 {
91         unsigned long flags;
92
93         spin_lock_irqsave(&host->lock, flags);
94         host->imask &= ~mask;
95         writel(host->imask, host->base + MMC_I_MASK);
96         spin_unlock_irqrestore(&host->lock, flags);
97 }
98
99 static void pxamci_disable_irq(struct pxamci_host *host, unsigned int mask)
100 {
101         unsigned long flags;
102
103         spin_lock_irqsave(&host->lock, flags);
104         host->imask |= mask;
105         writel(host->imask, host->base + MMC_I_MASK);
106         spin_unlock_irqrestore(&host->lock, flags);
107 }
108
109 static void pxamci_setup_data(struct pxamci_host *host, struct mmc_data *data)
110 {
111         unsigned int nob = data->blocks;
112         unsigned long long clks;
113         unsigned int timeout;
114         u32 dcmd;
115         int i;
116
117         host->data = data;
118
119         if (data->flags & MMC_DATA_STREAM)
120                 nob = 0xffff;
121
122         writel(nob, host->base + MMC_NOB);
123         writel(data->blksz, host->base + MMC_BLKLEN);
124
125         clks = (unsigned long long)data->timeout_ns * host->clkrate;
126         do_div(clks, 1000000000UL);
127         timeout = (unsigned int)clks + (data->timeout_clks << host->clkrt);
128         writel((timeout + 255) / 256, host->base + MMC_RDTO);
129
130         if (data->flags & MMC_DATA_READ) {
131                 host->dma_dir = DMA_FROM_DEVICE;
132                 dcmd = DCMD_INCTRGADDR | DCMD_FLOWTRG;
133                 DRCMRTXMMC = 0;
134                 DRCMRRXMMC = host->dma | DRCMR_MAPVLD;
135         } else {
136                 host->dma_dir = DMA_TO_DEVICE;
137                 dcmd = DCMD_INCSRCADDR | DCMD_FLOWSRC;
138                 DRCMRRXMMC = 0;
139                 DRCMRTXMMC = host->dma | DRCMR_MAPVLD;
140         }
141
142         dcmd |= DCMD_BURST32 | DCMD_WIDTH1;
143
144         host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
145                                    host->dma_dir);
146
147         for (i = 0; i < host->dma_len; i++) {
148                 unsigned int length = sg_dma_len(&data->sg[i]);
149                 host->sg_cpu[i].dcmd = dcmd | length;
150                 if (length & 31 && !(data->flags & MMC_DATA_READ))
151                         host->sg_cpu[i].dcmd |= DCMD_ENDIRQEN;
152                 if (data->flags & MMC_DATA_READ) {
153                         host->sg_cpu[i].dsadr = host->res->start + MMC_RXFIFO;
154                         host->sg_cpu[i].dtadr = sg_dma_address(&data->sg[i]);
155                 } else {
156                         host->sg_cpu[i].dsadr = sg_dma_address(&data->sg[i]);
157                         host->sg_cpu[i].dtadr = host->res->start + MMC_TXFIFO;
158                 }
159                 host->sg_cpu[i].ddadr = host->sg_dma + (i + 1) *
160                                         sizeof(struct pxa_dma_desc);
161         }
162         host->sg_cpu[host->dma_len - 1].ddadr = DDADR_STOP;
163         wmb();
164
165         DDADR(host->dma) = host->sg_dma;
166         DCSR(host->dma) = DCSR_RUN;
167 }
168
169 static void pxamci_start_cmd(struct pxamci_host *host, struct mmc_command *cmd, unsigned int cmdat)
170 {
171         WARN_ON(host->cmd != NULL);
172         host->cmd = cmd;
173
174         if (cmd->flags & MMC_RSP_BUSY)
175                 cmdat |= CMDAT_BUSY;
176
177 #define RSP_TYPE(x)     ((x) & ~(MMC_RSP_BUSY|MMC_RSP_OPCODE))
178         switch (RSP_TYPE(mmc_resp_type(cmd))) {
179         case RSP_TYPE(MMC_RSP_R1): /* r1, r1b, r6, r7 */
180                 cmdat |= CMDAT_RESP_SHORT;
181                 break;
182         case RSP_TYPE(MMC_RSP_R3):
183                 cmdat |= CMDAT_RESP_R3;
184                 break;
185         case RSP_TYPE(MMC_RSP_R2):
186                 cmdat |= CMDAT_RESP_R2;
187                 break;
188         default:
189                 break;
190         }
191
192         writel(cmd->opcode, host->base + MMC_CMD);
193         writel(cmd->arg >> 16, host->base + MMC_ARGH);
194         writel(cmd->arg & 0xffff, host->base + MMC_ARGL);
195         writel(cmdat, host->base + MMC_CMDAT);
196         writel(host->clkrt, host->base + MMC_CLKRT);
197
198         writel(START_CLOCK, host->base + MMC_STRPCL);
199
200         pxamci_enable_irq(host, END_CMD_RES);
201 }
202
203 static void pxamci_finish_request(struct pxamci_host *host, struct mmc_request *mrq)
204 {
205         host->mrq = NULL;
206         host->cmd = NULL;
207         host->data = NULL;
208         mmc_request_done(host->mmc, mrq);
209 }
210
211 static int pxamci_cmd_done(struct pxamci_host *host, unsigned int stat)
212 {
213         struct mmc_command *cmd = host->cmd;
214         int i;
215         u32 v;
216
217         if (!cmd)
218                 return 0;
219
220         host->cmd = NULL;
221
222         /*
223          * Did I mention this is Sick.  We always need to
224          * discard the upper 8 bits of the first 16-bit word.
225          */
226         v = readl(host->base + MMC_RES) & 0xffff;
227         for (i = 0; i < 4; i++) {
228                 u32 w1 = readl(host->base + MMC_RES) & 0xffff;
229                 u32 w2 = readl(host->base + MMC_RES) & 0xffff;
230                 cmd->resp[i] = v << 24 | w1 << 8 | w2 >> 8;
231                 v = w2;
232         }
233
234         if (stat & STAT_TIME_OUT_RESPONSE) {
235                 cmd->error = -ETIMEDOUT;
236         } else if (stat & STAT_RES_CRC_ERR && cmd->flags & MMC_RSP_CRC) {
237 #ifdef CONFIG_PXA27x
238                 /*
239                  * workaround for erratum #42:
240                  * Intel PXA27x Family Processor Specification Update Rev 001
241                  * A bogus CRC error can appear if the msb of a 136 bit
242                  * response is a one.
243                  */
244                 if (cmd->flags & MMC_RSP_136 && cmd->resp[0] & 0x80000000) {
245                         pr_debug("ignoring CRC from command %d - *risky*\n", cmd->opcode);
246                 } else
247 #endif
248                 cmd->error = -EILSEQ;
249         }
250
251         pxamci_disable_irq(host, END_CMD_RES);
252         if (host->data && !cmd->error) {
253                 pxamci_enable_irq(host, DATA_TRAN_DONE);
254         } else {
255                 pxamci_finish_request(host, host->mrq);
256         }
257
258         return 1;
259 }
260
261 static int pxamci_data_done(struct pxamci_host *host, unsigned int stat)
262 {
263         struct mmc_data *data = host->data;
264
265         if (!data)
266                 return 0;
267
268         DCSR(host->dma) = 0;
269         dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
270                      host->dma_dir);
271
272         if (stat & STAT_READ_TIME_OUT)
273                 data->error = -ETIMEDOUT;
274         else if (stat & (STAT_CRC_READ_ERROR|STAT_CRC_WRITE_ERROR))
275                 data->error = -EILSEQ;
276
277         /*
278          * There appears to be a hardware design bug here.  There seems to
279          * be no way to find out how much data was transferred to the card.
280          * This means that if there was an error on any block, we mark all
281          * data blocks as being in error.
282          */
283         if (!data->error)
284                 data->bytes_xfered = data->blocks * data->blksz;
285         else
286                 data->bytes_xfered = 0;
287
288         pxamci_disable_irq(host, DATA_TRAN_DONE);
289
290         host->data = NULL;
291         if (host->mrq->stop) {
292                 pxamci_stop_clock(host);
293                 pxamci_start_cmd(host, host->mrq->stop, host->cmdat);
294         } else {
295                 pxamci_finish_request(host, host->mrq);
296         }
297
298         return 1;
299 }
300
301 static irqreturn_t pxamci_irq(int irq, void *devid)
302 {
303         struct pxamci_host *host = devid;
304         unsigned int ireg;
305         int handled = 0;
306
307         ireg = readl(host->base + MMC_I_REG) & ~readl(host->base + MMC_I_MASK);
308
309         if (ireg) {
310                 unsigned stat = readl(host->base + MMC_STAT);
311
312                 pr_debug("PXAMCI: irq %08x stat %08x\n", ireg, stat);
313
314                 if (ireg & END_CMD_RES)
315                         handled |= pxamci_cmd_done(host, stat);
316                 if (ireg & DATA_TRAN_DONE)
317                         handled |= pxamci_data_done(host, stat);
318                 if (ireg & SDIO_INT) {
319                         mmc_signal_sdio_irq(host->mmc);
320                         handled = 1;
321                 }
322         }
323
324         return IRQ_RETVAL(handled);
325 }
326
327 static void pxamci_request(struct mmc_host *mmc, struct mmc_request *mrq)
328 {
329         struct pxamci_host *host = mmc_priv(mmc);
330         unsigned int cmdat;
331
332         WARN_ON(host->mrq != NULL);
333
334         host->mrq = mrq;
335
336         pxamci_stop_clock(host);
337
338         cmdat = host->cmdat;
339         host->cmdat &= ~CMDAT_INIT;
340
341         if (mrq->data) {
342                 pxamci_setup_data(host, mrq->data);
343
344                 cmdat &= ~CMDAT_BUSY;
345                 cmdat |= CMDAT_DATAEN | CMDAT_DMAEN;
346                 if (mrq->data->flags & MMC_DATA_WRITE)
347                         cmdat |= CMDAT_WRITE;
348
349                 if (mrq->data->flags & MMC_DATA_STREAM)
350                         cmdat |= CMDAT_STREAM;
351         }
352
353         pxamci_start_cmd(host, mrq->cmd, cmdat);
354 }
355
356 static int pxamci_get_ro(struct mmc_host *mmc)
357 {
358         struct pxamci_host *host = mmc_priv(mmc);
359
360         if (host->pdata && host->pdata->get_ro)
361                 return host->pdata->get_ro(mmc_dev(mmc));
362         /* Host doesn't support read only detection so assume writeable */
363         return 0;
364 }
365
366 static void pxamci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
367 {
368         struct pxamci_host *host = mmc_priv(mmc);
369
370         if (ios->clock) {
371                 unsigned long rate = host->clkrate;
372                 unsigned int clk = rate / ios->clock;
373
374                 /*
375                  * clk might result in a lower divisor than we
376                  * desire.  check for that condition and adjust
377                  * as appropriate.
378                  */
379                 if (rate / clk > ios->clock)
380                         clk <<= 1;
381                 host->clkrt = fls(clk) - 1;
382                 clk_enable(host->clk);
383
384                 /*
385                  * we write clkrt on the next command
386                  */
387         } else {
388                 pxamci_stop_clock(host);
389                 clk_disable(host->clk);
390         }
391
392         if (host->power_mode != ios->power_mode) {
393                 host->power_mode = ios->power_mode;
394
395                 if (host->pdata && host->pdata->setpower)
396                         host->pdata->setpower(mmc_dev(mmc), ios->vdd);
397
398                 if (ios->power_mode == MMC_POWER_ON)
399                         host->cmdat |= CMDAT_INIT;
400         }
401
402         if (ios->bus_width == MMC_BUS_WIDTH_4)
403                 host->cmdat |= CMDAT_SD_4DAT;
404         else
405                 host->cmdat &= ~CMDAT_SD_4DAT;
406
407         pr_debug("PXAMCI: clkrt = %x cmdat = %x\n",
408                  host->clkrt, host->cmdat);
409 }
410
411 static void pxamci_enable_sdio_irq(struct mmc_host *host, int enable)
412 {
413         struct pxamci_host *pxa_host = mmc_priv(host);
414
415         if (enable)
416                 pxamci_enable_irq(pxa_host, SDIO_INT);
417         else
418                 pxamci_disable_irq(pxa_host, SDIO_INT);
419 }
420
421 static const struct mmc_host_ops pxamci_ops = {
422         .request                = pxamci_request,
423         .get_ro                 = pxamci_get_ro,
424         .set_ios                = pxamci_set_ios,
425         .enable_sdio_irq        = pxamci_enable_sdio_irq,
426 };
427
428 static void pxamci_dma_irq(int dma, void *devid)
429 {
430         struct pxamci_host *host = devid;
431         int dcsr = DCSR(dma);
432         DCSR(dma) = dcsr & ~DCSR_STOPIRQEN;
433
434         if (dcsr & DCSR_ENDINTR) {
435                 writel(BUF_PART_FULL, host->base + MMC_PRTBUF);
436         } else {
437                 printk(KERN_ERR "%s: DMA error on channel %d (DCSR=%#x)\n",
438                        mmc_hostname(host->mmc), dma, dcsr);
439                 host->data->error = -EIO;
440                 pxamci_data_done(host, 0);
441         }
442 }
443
444 static irqreturn_t pxamci_detect_irq(int irq, void *devid)
445 {
446         struct pxamci_host *host = mmc_priv(devid);
447
448         mmc_detect_change(devid, host->pdata->detect_delay);
449         return IRQ_HANDLED;
450 }
451
452 static int pxamci_probe(struct platform_device *pdev)
453 {
454         struct mmc_host *mmc;
455         struct pxamci_host *host = NULL;
456         struct resource *r;
457         int ret, irq;
458
459         r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
460         irq = platform_get_irq(pdev, 0);
461         if (!r || irq < 0)
462                 return -ENXIO;
463
464         r = request_mem_region(r->start, SZ_4K, DRIVER_NAME);
465         if (!r)
466                 return -EBUSY;
467
468         mmc = mmc_alloc_host(sizeof(struct pxamci_host), &pdev->dev);
469         if (!mmc) {
470                 ret = -ENOMEM;
471                 goto out;
472         }
473
474         mmc->ops = &pxamci_ops;
475
476         /*
477          * We can do SG-DMA, but we don't because we never know how much
478          * data we successfully wrote to the card.
479          */
480         mmc->max_phys_segs = NR_SG;
481
482         /*
483          * Our hardware DMA can handle a maximum of one page per SG entry.
484          */
485         mmc->max_seg_size = PAGE_SIZE;
486
487         /*
488          * Block length register is only 10 bits before PXA27x.
489          */
490         mmc->max_blk_size = (cpu_is_pxa21x() || cpu_is_pxa25x()) ? 1023 : 2048;
491
492         /*
493          * Block count register is 16 bits.
494          */
495         mmc->max_blk_count = 65535;
496
497         host = mmc_priv(mmc);
498         host->mmc = mmc;
499         host->dma = -1;
500         host->pdata = pdev->dev.platform_data;
501
502         host->clk = clk_get(&pdev->dev, "MMCCLK");
503         if (IS_ERR(host->clk)) {
504                 ret = PTR_ERR(host->clk);
505                 host->clk = NULL;
506                 goto out;
507         }
508
509         host->clkrate = clk_get_rate(host->clk);
510
511         /*
512          * Calculate minimum clock rate, rounding up.
513          */
514         mmc->f_min = (host->clkrate + 63) / 64;
515         mmc->f_max = host->clkrate;
516
517         mmc->ocr_avail = host->pdata ?
518                          host->pdata->ocr_mask :
519                          MMC_VDD_32_33|MMC_VDD_33_34;
520         mmc->caps = 0;
521         host->cmdat = 0;
522         if (!cpu_is_pxa21x() && !cpu_is_pxa25x()) {
523                 mmc->caps |= MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
524                 host->cmdat |= CMDAT_SDIO_INT_EN;
525         }
526
527         host->sg_cpu = dma_alloc_coherent(&pdev->dev, PAGE_SIZE, &host->sg_dma, GFP_KERNEL);
528         if (!host->sg_cpu) {
529                 ret = -ENOMEM;
530                 goto out;
531         }
532
533         spin_lock_init(&host->lock);
534         host->res = r;
535         host->irq = irq;
536         host->imask = MMC_I_MASK_ALL;
537
538         host->base = ioremap(r->start, SZ_4K);
539         if (!host->base) {
540                 ret = -ENOMEM;
541                 goto out;
542         }
543
544         /*
545          * Ensure that the host controller is shut down, and setup
546          * with our defaults.
547          */
548         pxamci_stop_clock(host);
549         writel(0, host->base + MMC_SPI);
550         writel(64, host->base + MMC_RESTO);
551         writel(host->imask, host->base + MMC_I_MASK);
552
553         host->dma = pxa_request_dma(DRIVER_NAME, DMA_PRIO_LOW,
554                                     pxamci_dma_irq, host);
555         if (host->dma < 0) {
556                 ret = -EBUSY;
557                 goto out;
558         }
559
560         ret = request_irq(host->irq, pxamci_irq, 0, DRIVER_NAME, host);
561         if (ret)
562                 goto out;
563
564         platform_set_drvdata(pdev, mmc);
565
566         if (host->pdata && host->pdata->init)
567                 host->pdata->init(&pdev->dev, pxamci_detect_irq, mmc);
568
569         mmc_add_host(mmc);
570
571         return 0;
572
573  out:
574         if (host) {
575                 if (host->dma >= 0)
576                         pxa_free_dma(host->dma);
577                 if (host->base)
578                         iounmap(host->base);
579                 if (host->sg_cpu)
580                         dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
581                 if (host->clk)
582                         clk_put(host->clk);
583         }
584         if (mmc)
585                 mmc_free_host(mmc);
586         release_resource(r);
587         return ret;
588 }
589
590 static int pxamci_remove(struct platform_device *pdev)
591 {
592         struct mmc_host *mmc = platform_get_drvdata(pdev);
593
594         platform_set_drvdata(pdev, NULL);
595
596         if (mmc) {
597                 struct pxamci_host *host = mmc_priv(mmc);
598
599                 if (host->pdata && host->pdata->exit)
600                         host->pdata->exit(&pdev->dev, mmc);
601
602                 mmc_remove_host(mmc);
603
604                 pxamci_stop_clock(host);
605                 writel(TXFIFO_WR_REQ|RXFIFO_RD_REQ|CLK_IS_OFF|STOP_CMD|
606                        END_CMD_RES|PRG_DONE|DATA_TRAN_DONE,
607                        host->base + MMC_I_MASK);
608
609                 DRCMRRXMMC = 0;
610                 DRCMRTXMMC = 0;
611
612                 free_irq(host->irq, host);
613                 pxa_free_dma(host->dma);
614                 iounmap(host->base);
615                 dma_free_coherent(&pdev->dev, PAGE_SIZE, host->sg_cpu, host->sg_dma);
616
617                 clk_put(host->clk);
618
619                 release_resource(host->res);
620
621                 mmc_free_host(mmc);
622         }
623         return 0;
624 }
625
626 #ifdef CONFIG_PM
627 static int pxamci_suspend(struct platform_device *dev, pm_message_t state)
628 {
629         struct mmc_host *mmc = platform_get_drvdata(dev);
630         int ret = 0;
631
632         if (mmc)
633                 ret = mmc_suspend_host(mmc, state);
634
635         return ret;
636 }
637
638 static int pxamci_resume(struct platform_device *dev)
639 {
640         struct mmc_host *mmc = platform_get_drvdata(dev);
641         int ret = 0;
642
643         if (mmc)
644                 ret = mmc_resume_host(mmc);
645
646         return ret;
647 }
648 #else
649 #define pxamci_suspend  NULL
650 #define pxamci_resume   NULL
651 #endif
652
653 static struct platform_driver pxamci_driver = {
654         .probe          = pxamci_probe,
655         .remove         = pxamci_remove,
656         .suspend        = pxamci_suspend,
657         .resume         = pxamci_resume,
658         .driver         = {
659                 .name   = DRIVER_NAME,
660         },
661 };
662
663 static int __init pxamci_init(void)
664 {
665         return platform_driver_register(&pxamci_driver);
666 }
667
668 static void __exit pxamci_exit(void)
669 {
670         platform_driver_unregister(&pxamci_driver);
671 }
672
673 module_init(pxamci_init);
674 module_exit(pxamci_exit);
675
676 MODULE_DESCRIPTION("PXA Multimedia Card Interface Driver");
677 MODULE_LICENSE("GPL");