mfd: tmio_mmc hardware abstraction for CNF area
[safe/jmp/linux-2.6] / drivers / mmc / host / tmio_mmc.c
1 /*
2  *  linux/drivers/mmc/tmio_mmc.c
3  *
4  *  Copyright (C) 2004 Ian Molton
5  *  Copyright (C) 2007 Ian Molton
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  * Driver for the MMC / SD / SDIO cell found in:
12  *
13  * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
14  *
15  * This driver draws mainly on scattered spec sheets, Reverse engineering
16  * of the toshiba e800  SD driver and some parts of the 2.4 ASIC3 driver (4 bit
17  * support). (Further 4 bit support from a later datasheet).
18  *
19  * TODO:
20  *   Investigate using a workqueue for PIO transfers
21  *   Eliminate FIXMEs
22  *   SDIO support
23  *   Better Power management
24  *   Handle MMC errors better
25  *   double buffer support
26  *
27  */
28 #include <linux/module.h>
29 #include <linux/irq.h>
30 #include <linux/device.h>
31 #include <linux/delay.h>
32 #include <linux/mmc/host.h>
33 #include <linux/mfd/core.h>
34 #include <linux/mfd/tmio.h>
35
36 #include "tmio_mmc.h"
37
38 static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
39 {
40         u32 clk = 0, clock;
41
42         if (new_clock) {
43                 for (clock = host->mmc->f_min, clk = 0x80000080;
44                         new_clock >= (clock<<1); clk >>= 1)
45                         clock <<= 1;
46                 clk |= 0x100;
47         }
48
49         if (host->set_clk_div)
50                 host->set_clk_div(host->pdev, (clk>>22) & 1);
51
52         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
53 }
54
55 static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
56 {
57         sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
58         msleep(10);
59         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
60                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
61         msleep(10);
62 }
63
64 static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
65 {
66         sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
67                 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
68         msleep(10);
69         sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
70         msleep(10);
71 }
72
73 static void reset(struct tmio_mmc_host *host)
74 {
75         /* FIXME - should we set stop clock reg here */
76         sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
77         sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
78         msleep(10);
79         sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
80         sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
81         msleep(10);
82 }
83
84 static void
85 tmio_mmc_finish_request(struct tmio_mmc_host *host)
86 {
87         struct mmc_request *mrq = host->mrq;
88
89         host->mrq = NULL;
90         host->cmd = NULL;
91         host->data = NULL;
92
93         mmc_request_done(host->mmc, mrq);
94 }
95
96 /* These are the bitmasks the tmio chip requires to implement the MMC response
97  * types. Note that R1 and R6 are the same in this scheme. */
98 #define APP_CMD        0x0040
99 #define RESP_NONE      0x0300
100 #define RESP_R1        0x0400
101 #define RESP_R1B       0x0500
102 #define RESP_R2        0x0600
103 #define RESP_R3        0x0700
104 #define DATA_PRESENT   0x0800
105 #define TRANSFER_READ  0x1000
106 #define TRANSFER_MULTI 0x2000
107 #define SECURITY_CMD   0x4000
108
109 static int
110 tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
111 {
112         struct mmc_data *data = host->data;
113         int c = cmd->opcode;
114
115         /* Command 12 is handled by hardware */
116         if (cmd->opcode == 12 && !cmd->arg) {
117                 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
118                 return 0;
119         }
120
121         switch (mmc_resp_type(cmd)) {
122         case MMC_RSP_NONE: c |= RESP_NONE; break;
123         case MMC_RSP_R1:   c |= RESP_R1;   break;
124         case MMC_RSP_R1B:  c |= RESP_R1B;  break;
125         case MMC_RSP_R2:   c |= RESP_R2;   break;
126         case MMC_RSP_R3:   c |= RESP_R3;   break;
127         default:
128                 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
129                 return -EINVAL;
130         }
131
132         host->cmd = cmd;
133
134 /* FIXME - this seems to be ok comented out but the spec suggest this bit should
135  *         be set when issuing app commands.
136  *      if(cmd->flags & MMC_FLAG_ACMD)
137  *              c |= APP_CMD;
138  */
139         if (data) {
140                 c |= DATA_PRESENT;
141                 if (data->blocks > 1) {
142                         sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
143                         c |= TRANSFER_MULTI;
144                 }
145                 if (data->flags & MMC_DATA_READ)
146                         c |= TRANSFER_READ;
147         }
148
149         enable_mmc_irqs(host, TMIO_MASK_CMD);
150
151         /* Fire off the command */
152         sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
153         sd_ctrl_write16(host, CTL_SD_CMD, c);
154
155         return 0;
156 }
157
158 /* This chip always returns (at least?) as much data as you ask for.
159  * I'm unsure what happens if you ask for less than a block. This should be
160  * looked into to ensure that a funny length read doesnt hose the controller.
161  *
162  */
163 static inline void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
164 {
165         struct mmc_data *data = host->data;
166         unsigned short *buf;
167         unsigned int count;
168         unsigned long flags;
169
170         if (!data) {
171                 pr_debug("Spurious PIO IRQ\n");
172                 return;
173         }
174
175         buf = (unsigned short *)(tmio_mmc_kmap_atomic(host, &flags) +
176               host->sg_off);
177
178         count = host->sg_ptr->length - host->sg_off;
179         if (count > data->blksz)
180                 count = data->blksz;
181
182         pr_debug("count: %08x offset: %08x flags %08x\n",
183             count, host->sg_off, data->flags);
184
185         /* Transfer the data */
186         if (data->flags & MMC_DATA_READ)
187                 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
188         else
189                 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
190
191         host->sg_off += count;
192
193         tmio_mmc_kunmap_atomic(host, &flags);
194
195         if (host->sg_off == host->sg_ptr->length)
196                 tmio_mmc_next_sg(host);
197
198         return;
199 }
200
201 static inline void tmio_mmc_data_irq(struct tmio_mmc_host *host)
202 {
203         struct mmc_data *data = host->data;
204         struct mmc_command *stop;
205
206         host->data = NULL;
207
208         if (!data) {
209                 pr_debug("Spurious data end IRQ\n");
210                 return;
211         }
212         stop = data->stop;
213
214         /* FIXME - return correct transfer count on errors */
215         if (!data->error)
216                 data->bytes_xfered = data->blocks * data->blksz;
217         else
218                 data->bytes_xfered = 0;
219
220         pr_debug("Completed data request\n");
221
222         /*FIXME - other drivers allow an optional stop command of any given type
223          *        which we dont do, as the chip can auto generate them.
224          *        Perhaps we can be smarter about when to use auto CMD12 and
225          *        only issue the auto request when we know this is the desired
226          *        stop command, allowing fallback to the stop command the
227          *        upper layers expect. For now, we do what works.
228          */
229
230         if (data->flags & MMC_DATA_READ)
231                 disable_mmc_irqs(host, TMIO_MASK_READOP);
232         else
233                 disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
234
235         if (stop) {
236                 if (stop->opcode == 12 && !stop->arg)
237                         sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
238                 else
239                         BUG();
240         }
241
242         tmio_mmc_finish_request(host);
243 }
244
245 static inline void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
246         unsigned int stat)
247 {
248         struct mmc_command *cmd = host->cmd;
249         int i, addr;
250
251         if (!host->cmd) {
252                 pr_debug("Spurious CMD irq\n");
253                 return;
254         }
255
256         host->cmd = NULL;
257
258         /* This controller is sicker than the PXA one. Not only do we need to
259          * drop the top 8 bits of the first response word, we also need to
260          * modify the order of the response for short response command types.
261          */
262
263         for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
264                 cmd->resp[i] = sd_ctrl_read32(host, addr);
265
266         if (cmd->flags &  MMC_RSP_136) {
267                 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
268                 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
269                 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
270                 cmd->resp[3] <<= 8;
271         } else if (cmd->flags & MMC_RSP_R3) {
272                 cmd->resp[0] = cmd->resp[3];
273         }
274
275         if (stat & TMIO_STAT_CMDTIMEOUT)
276                 cmd->error = -ETIMEDOUT;
277         else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
278                 cmd->error = -EILSEQ;
279
280         /* If there is data to handle we enable data IRQs here, and
281          * we will ultimatley finish the request in the data_end handler.
282          * If theres no data or we encountered an error, finish now.
283          */
284         if (host->data && !cmd->error) {
285                 if (host->data->flags & MMC_DATA_READ)
286                         enable_mmc_irqs(host, TMIO_MASK_READOP);
287                 else
288                         enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
289         } else {
290                 tmio_mmc_finish_request(host);
291         }
292
293         return;
294 }
295
296
297 static irqreturn_t tmio_mmc_irq(int irq, void *devid)
298 {
299         struct tmio_mmc_host *host = devid;
300         unsigned int ireg, irq_mask, status;
301
302         pr_debug("MMC IRQ begin\n");
303
304         status = sd_ctrl_read32(host, CTL_STATUS);
305         irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
306         ireg = status & TMIO_MASK_IRQ & ~irq_mask;
307
308         pr_debug_status(status);
309         pr_debug_status(ireg);
310
311         if (!ireg) {
312                 disable_mmc_irqs(host, status & ~irq_mask);
313
314                 pr_debug("tmio_mmc: Spurious irq, disabling! "
315                         "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
316                 pr_debug_status(status);
317
318                 goto out;
319         }
320
321         while (ireg) {
322                 /* Card insert / remove attempts */
323                 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
324                         ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
325                                 TMIO_STAT_CARD_REMOVE);
326                         mmc_detect_change(host->mmc, 0);
327                 }
328
329                 /* CRC and other errors */
330 /*              if (ireg & TMIO_STAT_ERR_IRQ)
331  *                      handled |= tmio_error_irq(host, irq, stat);
332  */
333
334                 /* Command completion */
335                 if (ireg & TMIO_MASK_CMD) {
336                         ack_mmc_irqs(host, TMIO_MASK_CMD);
337                         tmio_mmc_cmd_irq(host, status);
338                 }
339
340                 /* Data transfer */
341                 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
342                         ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
343                         tmio_mmc_pio_irq(host);
344                 }
345
346                 /* Data transfer completion */
347                 if (ireg & TMIO_STAT_DATAEND) {
348                         ack_mmc_irqs(host, TMIO_STAT_DATAEND);
349                         tmio_mmc_data_irq(host);
350                 }
351
352                 /* Check status - keep going until we've handled it all */
353                 status = sd_ctrl_read32(host, CTL_STATUS);
354                 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
355                 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
356
357                 pr_debug("Status at end of loop: %08x\n", status);
358                 pr_debug_status(status);
359         }
360         pr_debug("MMC IRQ end\n");
361
362 out:
363         return IRQ_HANDLED;
364 }
365
366 static int tmio_mmc_start_data(struct tmio_mmc_host *host,
367         struct mmc_data *data)
368 {
369         pr_debug("setup data transfer: blocksize %08x  nr_blocks %d\n",
370             data->blksz, data->blocks);
371
372         /* Hardware cannot perform 1 and 2 byte requests in 4 bit mode */
373         if (data->blksz < 4 && host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
374                 printk(KERN_ERR "%s: %d byte block unsupported in 4 bit mode\n",
375                         mmc_hostname(host->mmc), data->blksz);
376                 return -EINVAL;
377         }
378
379         tmio_mmc_init_sg(host, data);
380         host->data = data;
381
382         /* Set transfer length / blocksize */
383         sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
384         sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
385
386         return 0;
387 }
388
389 /* Process requests from the MMC layer */
390 static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
391 {
392         struct tmio_mmc_host *host = mmc_priv(mmc);
393         int ret;
394
395         if (host->mrq)
396                 pr_debug("request not null\n");
397
398         host->mrq = mrq;
399
400         if (mrq->data) {
401                 ret = tmio_mmc_start_data(host, mrq->data);
402                 if (ret)
403                         goto fail;
404         }
405
406         ret = tmio_mmc_start_command(host, mrq->cmd);
407
408         if (!ret)
409                 return;
410
411 fail:
412         mrq->cmd->error = ret;
413         mmc_request_done(mmc, mrq);
414 }
415
416 /* Set MMC clock / power.
417  * Note: This controller uses a simple divider scheme therefore it cannot
418  * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
419  * MMC wont run that fast, it has to be clocked at 12MHz which is the next
420  * slowest setting.
421  */
422 static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
423 {
424         struct tmio_mmc_host *host = mmc_priv(mmc);
425
426         if (ios->clock)
427                 tmio_mmc_set_clock(host, ios->clock);
428
429         /* Power sequence - OFF -> ON -> UP */
430         switch (ios->power_mode) {
431         case MMC_POWER_OFF: /* power down SD bus */
432                 if (host->set_pwr)
433                         host->set_pwr(host->pdev, 0);
434                 tmio_mmc_clk_stop(host);
435                 break;
436         case MMC_POWER_ON: /* power up SD bus */
437                 if (host->set_pwr)
438                         host->set_pwr(host->pdev, 1);
439                 break;
440         case MMC_POWER_UP: /* start bus clock */
441                 tmio_mmc_clk_start(host);
442                 break;
443         }
444
445         switch (ios->bus_width) {
446         case MMC_BUS_WIDTH_1:
447                 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
448         break;
449         case MMC_BUS_WIDTH_4:
450                 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
451         break;
452         }
453
454         /* Let things settle. delay taken from winCE driver */
455         udelay(140);
456 }
457
458 static int tmio_mmc_get_ro(struct mmc_host *mmc)
459 {
460         struct tmio_mmc_host *host = mmc_priv(mmc);
461
462         return (sd_ctrl_read16(host, CTL_STATUS) & TMIO_STAT_WRPROTECT) ? 0 : 1;
463 }
464
465 static struct mmc_host_ops tmio_mmc_ops = {
466         .request        = tmio_mmc_request,
467         .set_ios        = tmio_mmc_set_ios,
468         .get_ro         = tmio_mmc_get_ro,
469 };
470
471 #ifdef CONFIG_PM
472 static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
473 {
474         struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
475         struct mmc_host *mmc = platform_get_drvdata(dev);
476         int ret;
477
478         ret = mmc_suspend_host(mmc, state);
479
480         /* Tell MFD core it can disable us now.*/
481         if (!ret && cell->disable)
482                 cell->disable(dev);
483
484         return ret;
485 }
486
487 static int tmio_mmc_resume(struct platform_device *dev)
488 {
489         struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
490         struct mmc_host *mmc = platform_get_drvdata(dev);
491         int ret = 0;
492
493         /* Tell the MFD core we are ready to be enabled */
494         if (cell->resume) {
495                 ret = cell->resume(dev);
496                 if (ret)
497                         goto out;
498         }
499
500         mmc_resume_host(mmc);
501
502 out:
503         return ret;
504 }
505 #else
506 #define tmio_mmc_suspend NULL
507 #define tmio_mmc_resume NULL
508 #endif
509
510 static int __devinit tmio_mmc_probe(struct platform_device *dev)
511 {
512         struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
513         struct tmio_mmc_data *pdata;
514         struct resource *res_ctl;
515         struct tmio_mmc_host *host;
516         struct mmc_host *mmc;
517         int ret = -EINVAL;
518
519         if (dev->num_resources != 2)
520                 goto out;
521
522         res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
523         if (!res_ctl)
524                 goto out;
525
526         pdata = cell->driver_data;
527         if (!pdata || !pdata->hclk)
528                 goto out;
529
530         ret = -ENOMEM;
531
532         mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
533         if (!mmc)
534                 goto out;
535
536         host = mmc_priv(mmc);
537         host->mmc = mmc;
538         host->pdev = dev;
539         platform_set_drvdata(dev, mmc);
540
541         host->set_pwr = pdata->set_pwr;
542         host->set_clk_div = pdata->set_clk_div;
543
544         /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
545         host->bus_shift = resource_size(res_ctl) >> 10;
546
547         host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
548         if (!host->ctl)
549                 goto host_free;
550
551         mmc->ops = &tmio_mmc_ops;
552         mmc->caps = MMC_CAP_4_BIT_DATA;
553         mmc->f_max = pdata->hclk;
554         mmc->f_min = mmc->f_max / 512;
555         mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
556
557         /* Tell the MFD core we are ready to be enabled */
558         if (cell->enable) {
559                 ret = cell->enable(dev);
560                 if (ret)
561                         goto unmap_ctl;
562         }
563
564         tmio_mmc_clk_stop(host);
565         reset(host);
566
567         ret = platform_get_irq(dev, 0);
568         if (ret >= 0)
569                 host->irq = ret;
570         else
571                 goto unmap_ctl;
572
573         disable_mmc_irqs(host, TMIO_MASK_ALL);
574
575         ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
576                 IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
577         if (ret)
578                 goto unmap_ctl;
579
580         mmc_add_host(mmc);
581
582         printk(KERN_INFO "%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
583                (unsigned long)host->ctl, host->irq);
584
585         /* Unmask the IRQs we want to know about */
586         enable_mmc_irqs(host, TMIO_MASK_IRQ);
587
588         return 0;
589
590 unmap_ctl:
591         iounmap(host->ctl);
592 host_free:
593         mmc_free_host(mmc);
594 out:
595         return ret;
596 }
597
598 static int __devexit tmio_mmc_remove(struct platform_device *dev)
599 {
600         struct mmc_host *mmc = platform_get_drvdata(dev);
601
602         platform_set_drvdata(dev, NULL);
603
604         if (mmc) {
605                 struct tmio_mmc_host *host = mmc_priv(mmc);
606                 mmc_remove_host(mmc);
607                 free_irq(host->irq, host);
608                 iounmap(host->ctl);
609                 mmc_free_host(mmc);
610         }
611
612         return 0;
613 }
614
615 /* ------------------- device registration ----------------------- */
616
617 static struct platform_driver tmio_mmc_driver = {
618         .driver = {
619                 .name = "tmio-mmc",
620                 .owner = THIS_MODULE,
621         },
622         .probe = tmio_mmc_probe,
623         .remove = __devexit_p(tmio_mmc_remove),
624         .suspend = tmio_mmc_suspend,
625         .resume = tmio_mmc_resume,
626 };
627
628
629 static int __init tmio_mmc_init(void)
630 {
631         return platform_driver_register(&tmio_mmc_driver);
632 }
633
634 static void __exit tmio_mmc_exit(void)
635 {
636         platform_driver_unregister(&tmio_mmc_driver);
637 }
638
639 module_init(tmio_mmc_init);
640 module_exit(tmio_mmc_exit);
641
642 MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
643 MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
644 MODULE_LICENSE("GPL v2");
645 MODULE_ALIAS("platform:tmio-mmc");