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