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