]> www.pilppa.org Git - linux-2.6-omap-h63xx.git/blob - drivers/mmc/host/omap_hsmmc.c
Merge current mainline tree into linux-omap tree
[linux-2.6-omap-h63xx.git] / drivers / mmc / host / omap_hsmmc.c
1 /*
2  * drivers/mmc/host/omap_hsmmc.c
3  *
4  * Driver for OMAP2430/3430 MMC controller.
5  *
6  * Copyright (C) 2007 Texas Instruments.
7  *
8  * Authors:
9  *      Syed Mohammed Khasim    <x0khasim@ti.com>
10  *      Madhusudhan             <madhu.cr@ti.com>
11  *      Mohit Jalori            <mjalori@ti.com>
12  *
13  * This file is licensed under the terms of the GNU General Public License
14  * version 2. This program is licensed "as is" without any warranty of any
15  * kind, whether express or implied.
16  */
17
18 #include <linux/module.h>
19 #include <linux/init.h>
20 #include <linux/interrupt.h>
21 #include <linux/delay.h>
22 #include <linux/dma-mapping.h>
23 #include <linux/platform_device.h>
24 #include <linux/workqueue.h>
25 #include <linux/timer.h>
26 #include <linux/clk.h>
27 #include <linux/mmc/host.h>
28 #include <linux/io.h>
29 #include <linux/semaphore.h>
30 #include <asm/dma.h>
31 #include <mach/hardware.h>
32 #include <mach/board.h>
33 #include <mach/mmc.h>
34 #include <mach/cpu.h>
35
36 /* OMAP HSMMC Host Controller Registers */
37 #define OMAP_HSMMC_SYSCONFIG    0x0010
38 #define OMAP_HSMMC_CON          0x002C
39 #define OMAP_HSMMC_BLK          0x0104
40 #define OMAP_HSMMC_ARG          0x0108
41 #define OMAP_HSMMC_CMD          0x010C
42 #define OMAP_HSMMC_RSP10        0x0110
43 #define OMAP_HSMMC_RSP32        0x0114
44 #define OMAP_HSMMC_RSP54        0x0118
45 #define OMAP_HSMMC_RSP76        0x011C
46 #define OMAP_HSMMC_DATA         0x0120
47 #define OMAP_HSMMC_HCTL         0x0128
48 #define OMAP_HSMMC_SYSCTL       0x012C
49 #define OMAP_HSMMC_STAT         0x0130
50 #define OMAP_HSMMC_IE           0x0134
51 #define OMAP_HSMMC_ISE          0x0138
52 #define OMAP_HSMMC_CAPA         0x0140
53
54 #define VS18                    (1<<26)
55 #define VS30                    (1<<25)
56 #define SDVS18                  (0x5<<9)
57 #define SDVS30                  (0x6<<9)
58 #define SDVSCLR                 0xFFFFF1FF
59 #define SDVSDET                 0x00000400
60 #define AUTOIDLE                0x1
61 #define SDBP                    (1<<8)
62 #define DTO                     0xe
63 #define ICE                     0x1
64 #define ICS                     0x2
65 #define CEN                     (1<<2)
66 #define CLKD_MASK               0x0000FFC0
67 #define INT_EN_MASK             0x307F0033
68 #define INIT_STREAM             (1<<1)
69 #define DP_SELECT               (1<<21)
70 #define DDIR                    (1<<4)
71 #define DMA_EN                  0x1
72 #define MSBS                    1<<5
73 #define BCE                     1<<1
74 #define FOUR_BIT                1 << 1
75 #define CC                      0x1
76 #define TC                      0x02
77 #define OD                      0x1
78 #define ERR                     (1 << 15)
79 #define CMD_TIMEOUT             (1 << 16)
80 #define DATA_TIMEOUT            (1 << 20)
81 #define CMD_CRC                 (1 << 17)
82 #define DATA_CRC                (1 << 21)
83 #define CARD_ERR                (1 << 28)
84 #define STAT_CLEAR              0xFFFFFFFF
85 #define INIT_STREAM_CMD         0x00000000
86 #define DUAL_VOLT_OCR_BIT       7
87 #define SRC                     (1 << 25)
88 #define SRD                     (1 << 26)
89
90 /*
91  * FIXME: Most likely all the data using these _DEVID defines should come
92  * from the platform_data, or implemented in controller and slot specific
93  * functions.
94  */
95 #define OMAP_MMC1_DEVID         0
96 #define OMAP_MMC2_DEVID         1
97
98 #define OMAP_MMC_DATADIR_NONE   0
99 #define OMAP_MMC_DATADIR_READ   1
100 #define OMAP_MMC_DATADIR_WRITE  2
101 #define MMC_TIMEOUT_MS          20
102 #define OMAP_MMC_MASTER_CLOCK   96000000
103 #define DRIVER_NAME             "mmci-omap"
104
105 /*
106  * One controller can have multiple slots, like on some omap boards using
107  * omap.c controller driver. Luckily this is not currently done on any known
108  * omap_hsmmc.c device.
109  */
110 #define mmc_slot(host)          (host->pdata->slots[host->slot_id])
111
112 /*
113  * MMC Host controller read/write API's
114  */
115 #define OMAP_HSMMC_READ(base, reg)      \
116         __raw_readl((base) + OMAP_HSMMC_##reg)
117
118 #define OMAP_HSMMC_WRITE(base, reg, val) \
119         __raw_writel((val), (base) + OMAP_HSMMC_##reg)
120
121 struct mmc_omap_host {
122         struct  device          *dev;
123         struct  mmc_host        *mmc;
124         struct  mmc_request     *mrq;
125         struct  mmc_command     *cmd;
126         struct  mmc_data        *data;
127         struct  clk             *fclk;
128         struct  clk             *iclk;
129         struct  clk             *dbclk;
130         struct  semaphore       sem;
131         struct  work_struct     mmc_carddetect_work;
132         void    __iomem         *base;
133         resource_size_t         mapbase;
134         unsigned int            id;
135         unsigned int            dma_len;
136         unsigned int            dma_dir;
137         unsigned char           bus_mode;
138         unsigned char           datadir;
139         u32                     *buffer;
140         u32                     bytesleft;
141         int                     suspended;
142         int                     irq;
143         int                     carddetect;
144         int                     use_dma, dma_ch;
145         int                     initstr;
146         int                     slot_id;
147         int                     dbclk_enabled;
148         struct  omap_mmc_platform_data  *pdata;
149 };
150
151 /*
152  * Stop clock to the card
153  */
154 static void omap_mmc_stop_clock(struct mmc_omap_host *host)
155 {
156         OMAP_HSMMC_WRITE(host->base, SYSCTL,
157                 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
158         if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
159                 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
160 }
161
162 /*
163  * Send init stream sequence to card
164  * before sending IDLE command
165  */
166 static void send_init_stream(struct mmc_omap_host *host)
167 {
168         int reg = 0;
169         unsigned long timeout;
170
171         disable_irq(host->irq);
172         OMAP_HSMMC_WRITE(host->base, CON,
173                 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
174         OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
175
176         timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
177         while ((reg != CC) && time_before(jiffies, timeout))
178                 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
179
180         OMAP_HSMMC_WRITE(host->base, CON,
181                 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
182         enable_irq(host->irq);
183 }
184
185 static inline
186 int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
187 {
188         if (host->pdata->slots[host->slot_id].get_cover_state)
189                 return host->pdata->slots[host->slot_id].get_cover_state(host->dev, host->slot_id);
190         return 1;
191 }
192
193 static ssize_t
194 mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
195                            char *buf)
196 {
197         struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
198         struct mmc_omap_host *host = mmc_priv(mmc);
199
200         return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
201                        "open");
202 }
203
204 static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
205
206 static ssize_t
207 mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
208                         char *buf)
209 {
210         struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
211         struct mmc_omap_host *host = mmc_priv(mmc);
212         struct omap_mmc_slot_data slot = host->pdata->slots[host->slot_id];
213
214         return sprintf(buf, "slot:%s\n", slot.name);
215 }
216
217 static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
218
219 /*
220  * Configure the response type and send the cmd.
221  */
222 static void
223 mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
224         struct mmc_data *data)
225 {
226         int cmdreg = 0, resptype = 0, cmdtype = 0;
227
228         dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
229                 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
230         host->cmd = cmd;
231
232         /*
233          * Clear status bits and enable interrupts
234          */
235         OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
236         OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
237         OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
238
239         if (cmd->flags & MMC_RSP_PRESENT) {
240                 if (cmd->flags & MMC_RSP_136)
241                         resptype = 1;
242                 else
243                         resptype = 2;
244         }
245
246         /*
247          * Unlike OMAP1 controller, the cmdtype does not seem to be based on
248          * ac, bc, adtc, bcr. Only CMD12 needs a val of 0x3, rest 0x0.
249          */
250         if (cmd->opcode == 12)
251                 cmdtype = 0x3;
252
253         cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
254
255         if (data) {
256                 cmdreg |= DP_SELECT | MSBS | BCE;
257                 if (data->flags & MMC_DATA_READ)
258                         cmdreg |= DDIR;
259                 else
260                         cmdreg &= ~(DDIR);
261         }
262
263         if (host->use_dma)
264                 cmdreg |= DMA_EN;
265
266         OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
267         OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
268 }
269
270 /*
271  * Notify the transfer complete to MMC core
272  */
273 static void
274 mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
275 {
276         host->data = NULL;
277
278         if (host->use_dma && host->dma_ch != -1)
279                 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
280                         host->dma_dir);
281
282         host->datadir = OMAP_MMC_DATADIR_NONE;
283
284         if (!data->error)
285                 data->bytes_xfered += data->blocks * (data->blksz);
286         else
287                 data->bytes_xfered = 0;
288
289         if (!data->stop) {
290                 host->mrq = NULL;
291                 mmc_request_done(host->mmc, data->mrq);
292                 return;
293         }
294         mmc_omap_start_command(host, data->stop, NULL);
295 }
296
297 /*
298  * Notify the core about command completion
299  */
300 static void
301 mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
302 {
303         host->cmd = NULL;
304
305         if (cmd->flags & MMC_RSP_PRESENT) {
306                 if (cmd->flags & MMC_RSP_136) {
307                         /* response type 2 */
308                         cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
309                         cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
310                         cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
311                         cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
312                 } else {
313                         /* response types 1, 1b, 3, 4, 5, 6 */
314                         cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
315                 }
316         }
317         if (host->data == NULL || cmd->error) {
318                 host->mrq = NULL;
319                 mmc_request_done(host->mmc, cmd->mrq);
320         }
321 }
322
323 /*
324  * DMA clean up for command errors
325  */
326 static void mmc_dma_cleanup(struct mmc_omap_host *host)
327 {
328         host->data->error = -ETIMEDOUT;
329
330         if (host->use_dma && host->dma_ch != -1) {
331                 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
332                         host->dma_dir);
333                 omap_free_dma(host->dma_ch);
334                 host->dma_ch = -1;
335                 up(&host->sem);
336         }
337         host->data = NULL;
338         host->datadir = OMAP_MMC_DATADIR_NONE;
339 }
340
341 /*
342  * Readable error output
343  */
344 #ifdef CONFIG_MMC_DEBUG
345 static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
346 {
347         /* --- means reserved bit without definition at documentation */
348         static const char *mmc_omap_status_bits[] = {
349                 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
350                 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
351                 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
352                 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
353         };
354         int i;
355
356         dev_dbg(mmc_dev(host->mmc), "MMC IRQ 0x%x :", status);
357
358         for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
359                 if (status & (1 << i))
360                         /*
361                          * KERN_* facility is not used here because this should
362                          * print a single line.
363                          */
364                         printk(" %s", mmc_omap_status_bits[i]);
365
366         printk("\n");
367
368 }
369 #endif  /* CONFIG_MMC_DEBUG */
370
371
372 /*
373  * MMC controller IRQ handler
374  */
375 static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
376 {
377         struct mmc_omap_host *host = dev_id;
378         struct mmc_data *data;
379         int end_cmd = 0, end_trans = 0, status;
380
381         if (host->cmd == NULL && host->data == NULL) {
382                 OMAP_HSMMC_WRITE(host->base, STAT,
383                         OMAP_HSMMC_READ(host->base, STAT));
384                 return IRQ_HANDLED;
385         }
386
387         data = host->data;
388         status = OMAP_HSMMC_READ(host->base, STAT);
389         dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
390
391         if (status & ERR) {
392 #ifdef CONFIG_MMC_DEBUG
393                 mmc_omap_report_irq(host, status);
394 #endif
395                 if ((status & CMD_TIMEOUT) ||
396                         (status & CMD_CRC)) {
397                         if (host->cmd) {
398                                 if (status & CMD_TIMEOUT) {
399                                         OMAP_HSMMC_WRITE(host->base, SYSCTL,
400                                                 OMAP_HSMMC_READ(host->base,
401                                                                 SYSCTL) | SRC);
402                                         while (OMAP_HSMMC_READ(host->base,
403                                                                 SYSCTL) & SRC) ;
404                                         host->cmd->error = -ETIMEDOUT;
405                                 } else {
406                                         host->cmd->error = -EILSEQ;
407                                 }
408                                 end_cmd = 1;
409                         }
410                         if (host->data)
411                                 mmc_dma_cleanup(host);
412                 }
413                 if ((status & DATA_TIMEOUT) ||
414                         (status & DATA_CRC)) {
415                         if (host->data) {
416                                 if (status & DATA_TIMEOUT)
417                                         mmc_dma_cleanup(host);
418                                 else
419                                         host->data->error = -EILSEQ;
420                                 end_trans = 1;
421                         }
422                 }
423                 if (status & CARD_ERR) {
424                         dev_dbg(mmc_dev(host->mmc),
425                                 "Ignoring card err CMD%d\n", host->cmd->opcode);
426                         if (host->cmd)
427                                 end_cmd = 1;
428                         if (host->data)
429                                 end_trans = 1;
430                 }
431         }
432
433         OMAP_HSMMC_WRITE(host->base, STAT, status);
434
435         if (end_cmd || (status & CC))
436                 mmc_omap_cmd_done(host, host->cmd);
437         if (end_trans || (status & TC))
438                 mmc_omap_xfer_done(host, data);
439
440         return IRQ_HANDLED;
441 }
442
443 /*
444  * Switch MMC operating voltage
445  */
446 static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
447 {
448         u32 reg_val = 0;
449         int ret;
450
451         /* Disable the clocks */
452         clk_disable(host->fclk);
453         clk_disable(host->iclk);
454         clk_disable(host->dbclk);
455
456         /* Turn the power off */
457         ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
458         if (ret != 0)
459                 goto err;
460
461         /* Turn the power ON with given VDD 1.8 or 3.0v */
462         ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
463         if (ret != 0)
464                 goto err;
465
466         clk_enable(host->fclk);
467         clk_enable(host->iclk);
468         clk_enable(host->dbclk);
469
470         OMAP_HSMMC_WRITE(host->base, HCTL,
471                 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
472         reg_val = OMAP_HSMMC_READ(host->base, HCTL);
473         /*
474          * If a MMC dual voltage card is detected, the set_ios fn calls
475          * this fn with VDD bit set for 1.8V. Upon card removal from the
476          * slot, mmc_omap_detect fn sets the VDD back to 3V.
477          *
478          * Only MMC1 supports 3.0V.  MMC2 will not function if SDVS30 is
479          * set in HCTL.
480          */
481         if (host->id == OMAP_MMC1_DEVID && (((1 << vdd) == MMC_VDD_32_33) ||
482                                 ((1 << vdd) == MMC_VDD_33_34)))
483                 reg_val |= SDVS30;
484         if ((1 << vdd) == MMC_VDD_165_195)
485                 reg_val |= SDVS18;
486
487         OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
488
489         OMAP_HSMMC_WRITE(host->base, HCTL,
490                 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
491
492         return 0;
493 err:
494         dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
495         return ret;
496 }
497
498 /*
499  * Work Item to notify the core about card insertion/removal
500  */
501 static void mmc_omap_detect(struct work_struct *work)
502 {
503         u16 vdd = 0;
504         struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
505                                                 mmc_carddetect_work);
506
507         sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
508         if (host->carddetect) {
509                 if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
510                         /*
511                          * Set the VDD back to 3V when the card is removed
512                          * before the set_ios fn turns off the power.
513                          */
514                         vdd = fls(host->mmc->ocr_avail) - 1;
515                         if (omap_mmc_switch_opcond(host, vdd) != 0)
516                                 host->mmc->ios.vdd = vdd;
517                 }
518                 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
519         } else {
520                 OMAP_HSMMC_WRITE(host->base, SYSCTL,
521                         OMAP_HSMMC_READ(host->base, SYSCTL) | SRD);
522                 while (OMAP_HSMMC_READ(host->base, SYSCTL) & SRD) ;
523                 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
524         }
525 }
526
527 /*
528  * ISR for handling card insertion and removal
529  */
530 static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
531 {
532         struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
533
534         host->carddetect = mmc_slot(host).card_detect(irq);
535         schedule_work(&host->mmc_carddetect_work);
536
537         return IRQ_HANDLED;
538 }
539
540 /*
541  * DMA call back function
542  */
543 static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
544 {
545         struct mmc_omap_host *host = data;
546
547         if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
548                 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
549
550         if (host->dma_ch < 0)
551                 return;
552
553         omap_free_dma(host->dma_ch);
554         host->dma_ch = -1;
555         /*
556          * DMA Callback: run in interrupt context.
557          * mutex_unlock will through a kernel warning if used.
558          */
559         up(&host->sem);
560 }
561
562 /*
563  * Configure dma src and destination parameters
564  */
565 static int mmc_omap_config_dma_param(int sync_dir, struct mmc_omap_host *host,
566                                 struct mmc_data *data)
567 {
568         if (sync_dir == 0) {
569                 omap_set_dma_dest_params(host->dma_ch, 0,
570                         OMAP_DMA_AMODE_CONSTANT,
571                         (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
572                 omap_set_dma_src_params(host->dma_ch, 0,
573                         OMAP_DMA_AMODE_POST_INC,
574                         sg_dma_address(&data->sg[0]), 0, 0);
575         } else {
576                 omap_set_dma_src_params(host->dma_ch, 0,
577                         OMAP_DMA_AMODE_CONSTANT,
578                         (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
579                 omap_set_dma_dest_params(host->dma_ch, 0,
580                         OMAP_DMA_AMODE_POST_INC,
581                         sg_dma_address(&data->sg[0]), 0, 0);
582         }
583         return 0;
584 }
585 /*
586  * Routine to configure and start DMA for the MMC card
587  */
588 static int
589 mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
590 {
591         int sync_dev, sync_dir = 0;
592         int dma_ch = 0, ret = 0, err = 1;
593         struct mmc_data *data = req->data;
594
595         /*
596          * If for some reason the DMA transfer is still active,
597          * we wait for timeout period and free the dma
598          */
599         if (host->dma_ch != -1) {
600                 set_current_state(TASK_UNINTERRUPTIBLE);
601                 schedule_timeout(100);
602                 if (down_trylock(&host->sem)) {
603                         omap_free_dma(host->dma_ch);
604                         host->dma_ch = -1;
605                         up(&host->sem);
606                         return err;
607                 }
608         } else {
609                 if (down_trylock(&host->sem))
610                         return err;
611         }
612
613         if (!(data->flags & MMC_DATA_WRITE)) {
614                 host->dma_dir = DMA_FROM_DEVICE;
615                 if (host->id == OMAP_MMC1_DEVID)
616                         sync_dev = OMAP24XX_DMA_MMC1_RX;
617                 else
618                         sync_dev = OMAP24XX_DMA_MMC2_RX;
619         } else {
620                 host->dma_dir = DMA_TO_DEVICE;
621                 if (host->id == OMAP_MMC1_DEVID)
622                         sync_dev = OMAP24XX_DMA_MMC1_TX;
623                 else
624                         sync_dev = OMAP24XX_DMA_MMC2_TX;
625         }
626
627         ret = omap_request_dma(sync_dev, "MMC/SD", mmc_omap_dma_cb,
628                         host, &dma_ch);
629         if (ret != 0) {
630                 dev_dbg(mmc_dev(host->mmc),
631                         "%s: omap_request_dma() failed with %d\n",
632                         mmc_hostname(host->mmc), ret);
633                 return ret;
634         }
635
636         host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
637                         data->sg_len, host->dma_dir);
638         host->dma_ch = dma_ch;
639
640         if (!(data->flags & MMC_DATA_WRITE))
641                 mmc_omap_config_dma_param(1, host, data);
642         else
643                 mmc_omap_config_dma_param(0, host, data);
644
645         if ((data->blksz % 4) == 0)
646                 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
647                         (data->blksz / 4), data->blocks, OMAP_DMA_SYNC_FRAME,
648                         sync_dev, sync_dir);
649         else
650                 /* REVISIT: The MMC buffer increments only when MSB is written.
651                  * Return error for blksz which is non multiple of four.
652                  */
653                 return -EINVAL;
654
655         omap_start_dma(dma_ch);
656         return 0;
657 }
658
659 /*
660  * Configure block length for MMC/SD cards and initiate the transfer.
661  */
662 static int
663 mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
664 {
665         int ret;
666         host->data = req->data;
667
668         if (req->data == NULL) {
669                 host->datadir = OMAP_MMC_DATADIR_NONE;
670                 OMAP_HSMMC_WRITE(host->base, BLK, 0);
671                 return 0;
672         }
673
674         OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
675                                         | (req->data->blocks << 16));
676
677         host->datadir = (req->data->flags & MMC_DATA_WRITE) ?
678                         OMAP_MMC_DATADIR_WRITE : OMAP_MMC_DATADIR_READ;
679
680         if (host->use_dma) {
681                 ret = mmc_omap_start_dma_transfer(host, req);
682                 if (ret != 0) {
683                         dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
684                         return ret;
685                 }
686         }
687         return 0;
688 }
689
690 /*
691  * Request function. for read/write operation
692  */
693 static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
694 {
695         struct mmc_omap_host *host = mmc_priv(mmc);
696
697         WARN_ON(host->mrq != NULL);
698         host->mrq = req;
699         mmc_omap_prepare_data(host, req);
700         mmc_omap_start_command(host, req->cmd, req->data);
701 }
702
703
704 /* Routine to configure clock values. Exposed API to core */
705 static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
706 {
707         struct mmc_omap_host *host = mmc_priv(mmc);
708         u16 dsor = 0;
709         unsigned long regval;
710         unsigned long timeout;
711
712         switch (ios->power_mode) {
713         case MMC_POWER_OFF:
714                 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
715                 break;
716         case MMC_POWER_UP:
717                 mmc_slot(host).set_power(host->dev, host->slot_id, 1, ios->vdd);
718                 break;
719         }
720
721         switch (mmc->ios.bus_width) {
722         case MMC_BUS_WIDTH_4:
723                 OMAP_HSMMC_WRITE(host->base, HCTL,
724                         OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
725                 break;
726         case MMC_BUS_WIDTH_1:
727                 OMAP_HSMMC_WRITE(host->base, HCTL,
728                         OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
729                 break;
730         }
731
732         if (host->id == OMAP_MMC1_DEVID) {
733                 /* Only MMC1 can operate at 3V/1.8V */
734                 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
735                         (ios->vdd == DUAL_VOLT_OCR_BIT)) {
736                                 /*
737                                  * The mmc_select_voltage fn of the core does
738                                  * not seem to set the power_mode to
739                                  * MMC_POWER_UP upon recalculating the voltage.
740                                  * vdd 1.8v.
741                                  */
742                                 if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
743                                         dev_dbg(mmc_dev(host->mmc),
744                                                 "Switch operation failed\n");
745                 }
746         }
747
748         if (ios->clock) {
749                 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
750                 if (dsor < 1)
751                         dsor = 1;
752
753                 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
754                         dsor++;
755
756                 if (dsor > 250)
757                         dsor = 250;
758         }
759         omap_mmc_stop_clock(host);
760         regval = OMAP_HSMMC_READ(host->base, SYSCTL);
761         regval = regval & ~(CLKD_MASK);
762         regval = regval | (dsor << 6) | (DTO << 16);
763         OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
764         OMAP_HSMMC_WRITE(host->base, SYSCTL,
765                 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
766
767         /* Wait till the ICS bit is set */
768         timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
769         while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != 0x2
770                 && time_before(jiffies, timeout))
771                 msleep(1);
772
773         OMAP_HSMMC_WRITE(host->base, SYSCTL,
774                 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
775
776         if (ios->power_mode == MMC_POWER_ON)
777                 send_init_stream(host);
778
779         if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
780                 OMAP_HSMMC_WRITE(host->base, CON,
781                                 OMAP_HSMMC_READ(host->base, CON) | OD);
782 }
783 /* NOTE: Read only switch not supported yet */
784 static struct mmc_host_ops mmc_omap_ops = {
785         .request = omap_mmc_request,
786         .set_ios = omap_mmc_set_ios,
787 };
788
789 static int __init omap_mmc_probe(struct platform_device *pdev)
790 {
791         struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
792         struct mmc_host *mmc;
793         struct mmc_omap_host *host = NULL;
794         struct resource *res;
795         int ret = 0, irq;
796         u32 hctl, capa;
797
798         if (pdata == NULL) {
799                 dev_err(&pdev->dev, "Platform Data is missing\n");
800                 return -ENXIO;
801         }
802
803         if (pdata->nr_slots == 0) {
804                 dev_err(&pdev->dev, "No Slots\n");
805                 return -ENXIO;
806         }
807
808         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
809         irq = platform_get_irq(pdev, 0);
810         if (res == NULL || irq < 0)
811                 return -ENXIO;
812
813         res = request_mem_region(res->start, res->end - res->start + 1,
814                                                         pdev->name);
815         if (res == NULL)
816                 return -EBUSY;
817
818         mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
819         if (!mmc) {
820                 ret = -ENOMEM;
821                 goto err;
822         }
823
824         host            = mmc_priv(mmc);
825         host->mmc       = mmc;
826         host->pdata     = pdata;
827         host->dev       = &pdev->dev;
828         host->use_dma   = 1;
829         host->dev->dma_mask = &pdata->dma_mask;
830         host->dma_ch    = -1;
831         host->irq       = irq;
832         host->id        = pdev->id;
833         host->slot_id   = 0;
834         host->mapbase   = res->start;
835         host->base      = ioremap(host->mapbase, SZ_4K);
836         mmc->ops        = &mmc_omap_ops;
837         mmc->f_min      = 400000;
838         mmc->f_max      = 52000000;
839
840         sema_init(&host->sem, 1);
841
842         host->iclk = clk_get(&pdev->dev, "mmchs_ick");
843         if (IS_ERR(host->iclk)) {
844                 ret = PTR_ERR(host->iclk);
845                 host->iclk = NULL;
846                 goto err1;
847         }
848         host->fclk = clk_get(&pdev->dev, "mmchs_fck");
849         if (IS_ERR(host->fclk)) {
850                 ret = PTR_ERR(host->fclk);
851                 host->fclk = NULL;
852                 clk_put(host->iclk);
853                 goto err1;
854         }
855
856         if (clk_enable(host->fclk) != 0) {
857                 clk_put(host->iclk);
858                 clk_put(host->fclk);
859                 goto err1;
860         }
861
862         if (clk_enable(host->iclk) != 0) {
863                 clk_disable(host->fclk);
864                 clk_put(host->iclk);
865                 clk_put(host->fclk);
866                 goto err1;
867         }
868
869         host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
870         /*
871          * MMC can still work without debounce clock.
872          */
873         if (IS_ERR(host->dbclk))
874                 dev_dbg(mmc_dev(host->mmc), "Failed to get debounce clock\n");
875         else
876                 if (clk_enable(host->dbclk) != 0)
877                         dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
878                                                         " clk failed\n");
879                 else
880                         host->dbclk_enabled = 1;
881
882 #ifdef CONFIG_MMC_BLOCK_BOUNCE
883         mmc->max_phys_segs = 1;
884         mmc->max_hw_segs = 1;
885 #endif
886         mmc->max_blk_size = 512;       /* Block Length at max can be 1024 */
887         mmc->max_blk_count = 0xFFFF;    /* No. of Blocks is 16 bits */
888         mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
889         mmc->max_seg_size = mmc->max_req_size;
890
891         mmc->ocr_avail = mmc_slot(host).ocr_mask;
892         mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED;
893
894         if (pdata->slots[host->slot_id].wire4)
895                 mmc->caps |= MMC_CAP_4_BIT_DATA;
896
897         /* Only MMC1 supports 3.0V */
898         if (host->id == OMAP_MMC1_DEVID) {
899                 hctl = SDVS30;
900                 capa = VS30 | VS18;
901         } else {
902                 hctl = SDVS18;
903                 capa = VS18;
904         }
905
906         OMAP_HSMMC_WRITE(host->base, HCTL,
907                         OMAP_HSMMC_READ(host->base, HCTL) | hctl);
908
909         OMAP_HSMMC_WRITE(host->base, CAPA,
910                         OMAP_HSMMC_READ(host->base, CAPA) | capa);
911
912         /* Set the controller to AUTO IDLE mode */
913         OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
914                         OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
915
916         /* Set SD bus power bit */
917         OMAP_HSMMC_WRITE(host->base, HCTL,
918                         OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
919
920         /* Request IRQ for MMC operations */
921         ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
922                         mmc_hostname(mmc), host);
923         if (ret) {
924                 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
925                 goto err_irq;
926         }
927
928         /* Request IRQ for card detect */
929         if ((mmc_slot(host).card_detect_irq) && (mmc_slot(host).card_detect)) {
930                 ret = request_irq(mmc_slot(host).card_detect_irq,
931                                   omap_mmc_cd_handler,
932                                   IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
933                                           | IRQF_DISABLED,
934                                   mmc_hostname(mmc), host);
935                 if (ret) {
936                         dev_dbg(mmc_dev(host->mmc),
937                                 "Unable to grab MMC CD IRQ\n");
938                         goto err_irq_cd;
939                 }
940         }
941
942         INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
943         if (pdata->init != NULL) {
944                 if (pdata->init(&pdev->dev) != 0) {
945                         dev_dbg(mmc_dev(host->mmc),
946                                 "Unable to configure MMC IRQs\n");
947                         goto err_irq_cd_init;
948                 }
949         }
950
951         OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
952         OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
953
954         platform_set_drvdata(pdev, host);
955         mmc_add_host(mmc);
956
957         if (host->pdata->slots[host->slot_id].name != NULL) {
958                 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
959                 if (ret < 0)
960                         goto err_slot_name;
961         }
962         if (mmc_slot(host).card_detect_irq && mmc_slot(host).card_detect &&
963                         host->pdata->slots[host->slot_id].get_cover_state) {
964                 ret = device_create_file(&mmc->class_dev, &dev_attr_cover_switch);
965                 if (ret < 0)
966                         goto err_cover_switch;
967         }
968
969         return 0;
970
971 err_cover_switch:
972         device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
973 err_slot_name:
974         mmc_remove_host(mmc);
975 err_irq_cd_init:
976         free_irq(mmc_slot(host).card_detect_irq, host);
977 err_irq_cd:
978         free_irq(host->irq, host);
979 err_irq:
980         clk_disable(host->fclk);
981         clk_disable(host->iclk);
982         clk_put(host->fclk);
983         clk_put(host->iclk);
984         if (host->dbclk_enabled) {
985                 clk_disable(host->dbclk);
986                 clk_put(host->dbclk);
987         }
988
989 err1:
990         iounmap(host->base);
991 err:
992         dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
993         release_mem_region(res->start, res->end - res->start + 1);
994         if (host)
995                 mmc_free_host(mmc);
996         return ret;
997 }
998
999 static int omap_mmc_remove(struct platform_device *pdev)
1000 {
1001         struct mmc_omap_host *host = platform_get_drvdata(pdev);
1002         struct resource *res;
1003         u16 vdd = 0;
1004
1005         if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
1006         /*
1007          * Set the vdd back to 3V,
1008          * applicable for dual volt support.
1009          */
1010                 vdd = fls(host->mmc->ocr_avail) - 1;
1011                 if (omap_mmc_switch_opcond(host, vdd) != 0)
1012                         host->mmc->ios.vdd = vdd;
1013         }
1014
1015         res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1016         if (res)
1017                 release_mem_region(res->start, res->end - res->start + 1);
1018
1019         platform_set_drvdata(pdev, NULL);
1020         if (host) {
1021                 mmc_remove_host(host->mmc);
1022                 if (host->pdata->cleanup)
1023                         host->pdata->cleanup(&pdev->dev);
1024                 free_irq(host->irq, host);
1025                 if (mmc_slot(host).card_detect_irq)
1026                         free_irq(mmc_slot(host).card_detect_irq, host);
1027                 flush_scheduled_work();
1028
1029                 clk_disable(host->fclk);
1030                 clk_disable(host->iclk);
1031                 clk_put(host->fclk);
1032                 clk_put(host->iclk);
1033                 if (host->dbclk_enabled) {
1034                         clk_disable(host->dbclk);
1035                         clk_put(host->dbclk);
1036                 }
1037
1038                 mmc_free_host(host->mmc);
1039                 iounmap(host->base);
1040         }
1041
1042         return 0;
1043 }
1044
1045 #ifdef CONFIG_PM
1046 static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
1047 {
1048         int ret = 0;
1049         struct mmc_omap_host *host = platform_get_drvdata(pdev);
1050
1051         if (host && host->suspended)
1052                 return 0;
1053
1054         if (host) {
1055                 ret = mmc_suspend_host(host->mmc, state);
1056                 if (ret == 0) {
1057                         host->suspended = 1;
1058
1059                         OMAP_HSMMC_WRITE(host->base, ISE, 0);
1060                         OMAP_HSMMC_WRITE(host->base, IE, 0);
1061
1062                         if (host->pdata->suspend) {
1063                                 ret = host->pdata->suspend(&pdev->dev, host->slot_id);
1064                                 if (ret)
1065                                         dev_dbg(mmc_dev(host->mmc),
1066                                                 "Unable to handle MMC board"
1067                                                 " level suspend\n");
1068                         }
1069
1070                         if (!(OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET)) {
1071                                 OMAP_HSMMC_WRITE(host->base, HCTL,
1072                                         OMAP_HSMMC_READ(host->base, HCTL)
1073                                         & SDVSCLR);
1074                                 OMAP_HSMMC_WRITE(host->base, HCTL,
1075                                         OMAP_HSMMC_READ(host->base, HCTL)
1076                                         | SDVS30);
1077                                 OMAP_HSMMC_WRITE(host->base, HCTL,
1078                                         OMAP_HSMMC_READ(host->base, HCTL)
1079                                         | SDBP);
1080                         }
1081
1082                         clk_disable(host->fclk);
1083                         clk_disable(host->iclk);
1084                         clk_disable(host->dbclk);
1085                 }
1086
1087         }
1088         return ret;
1089 }
1090
1091 /* Routine to resume the MMC device */
1092 static int omap_mmc_resume(struct platform_device *pdev)
1093 {
1094         int ret = 0;
1095         struct mmc_omap_host *host = platform_get_drvdata(pdev);
1096
1097         if (host && !host->suspended)
1098                 return 0;
1099
1100         if (host) {
1101
1102                 ret = clk_enable(host->fclk);
1103                 if (ret)
1104                         goto clk_en_err;
1105
1106                 ret = clk_enable(host->iclk);
1107                 if (ret) {
1108                         clk_disable(host->fclk);
1109                         clk_put(host->fclk);
1110                         goto clk_en_err;
1111                 }
1112
1113                 if (clk_enable(host->dbclk) != 0)
1114                         dev_dbg(mmc_dev(host->mmc),
1115                                         "Enabling debounce clk failed\n");
1116
1117                 if (host->pdata->resume) {
1118                         ret = host->pdata->resume(&pdev->dev, host->slot_id);
1119                         if (ret)
1120                                 dev_dbg(mmc_dev(host->mmc),
1121                                         "Unmask interrupt failed\n");
1122                 }
1123
1124                 /* Notify the core to resume the host */
1125                 ret = mmc_resume_host(host->mmc);
1126                 if (ret == 0)
1127                         host->suspended = 0;
1128         }
1129
1130         return ret;
1131
1132 clk_en_err:
1133         dev_dbg(mmc_dev(host->mmc),
1134                 "Failed to enable MMC clocks during resume\n");
1135         return ret;
1136 }
1137
1138 #else
1139 #define omap_mmc_suspend        NULL
1140 #define omap_mmc_resume         NULL
1141 #endif
1142
1143 static struct platform_driver omap_mmc_driver = {
1144         .probe          = omap_mmc_probe,
1145         .remove         = omap_mmc_remove,
1146         .suspend        = omap_mmc_suspend,
1147         .resume         = omap_mmc_resume,
1148         .driver         = {
1149                 .name = DRIVER_NAME,
1150                 .owner = THIS_MODULE,
1151         },
1152 };
1153
1154 static int __init omap_mmc_init(void)
1155 {
1156         /* Register the MMC driver */
1157         return platform_driver_register(&omap_mmc_driver);
1158 }
1159
1160 static void __exit omap_mmc_cleanup(void)
1161 {
1162         /* Unregister MMC driver */
1163         platform_driver_unregister(&omap_mmc_driver);
1164 }
1165
1166 module_init(omap_mmc_init);
1167 module_exit(omap_mmc_cleanup);
1168
1169 MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1170 MODULE_LICENSE("GPL");
1171 MODULE_ALIAS("platform:" DRIVER_NAME);
1172 MODULE_AUTHOR("Texas Instruments Inc");