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