blob: 0bfea9c2a5970baa0799af79e78b2fdd023558b8 [file] [log] [blame]
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001/*
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>
Denis Karpovd900f712009-09-22 16:44:38 -070020#include <linux/debugfs.h>
21#include <linux/seq_file.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010022#include <linux/interrupt.h>
23#include <linux/delay.h>
24#include <linux/dma-mapping.h>
25#include <linux/platform_device.h>
26#include <linux/workqueue.h>
27#include <linux/timer.h>
28#include <linux/clk.h>
29#include <linux/mmc/host.h>
Jarkko Lavinen13189e72009-09-22 16:44:53 -070030#include <linux/mmc/core.h>
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010031#include <linux/io.h>
32#include <linux/semaphore.h>
33#include <mach/dma.h>
34#include <mach/hardware.h>
35#include <mach/board.h>
36#include <mach/mmc.h>
37#include <mach/cpu.h>
38
39/* OMAP HSMMC Host Controller Registers */
40#define OMAP_HSMMC_SYSCONFIG 0x0010
Denis Karpov11dd62a2009-09-22 16:44:43 -070041#define OMAP_HSMMC_SYSSTATUS 0x0014
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010042#define OMAP_HSMMC_CON 0x002C
43#define OMAP_HSMMC_BLK 0x0104
44#define OMAP_HSMMC_ARG 0x0108
45#define OMAP_HSMMC_CMD 0x010C
46#define OMAP_HSMMC_RSP10 0x0110
47#define OMAP_HSMMC_RSP32 0x0114
48#define OMAP_HSMMC_RSP54 0x0118
49#define OMAP_HSMMC_RSP76 0x011C
50#define OMAP_HSMMC_DATA 0x0120
51#define OMAP_HSMMC_HCTL 0x0128
52#define OMAP_HSMMC_SYSCTL 0x012C
53#define OMAP_HSMMC_STAT 0x0130
54#define OMAP_HSMMC_IE 0x0134
55#define OMAP_HSMMC_ISE 0x0138
56#define OMAP_HSMMC_CAPA 0x0140
57
58#define VS18 (1 << 26)
59#define VS30 (1 << 25)
60#define SDVS18 (0x5 << 9)
61#define SDVS30 (0x6 << 9)
David Brownelleb250822009-02-17 14:49:01 -080062#define SDVS33 (0x7 << 9)
Kim Kyuwon1b331e62009-02-20 13:10:08 +010063#define SDVS_MASK 0x00000E00
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010064#define SDVSCLR 0xFFFFF1FF
65#define SDVSDET 0x00000400
66#define AUTOIDLE 0x1
67#define SDBP (1 << 8)
68#define DTO 0xe
69#define ICE 0x1
70#define ICS 0x2
71#define CEN (1 << 2)
72#define CLKD_MASK 0x0000FFC0
73#define CLKD_SHIFT 6
74#define DTO_MASK 0x000F0000
75#define DTO_SHIFT 16
76#define INT_EN_MASK 0x307F0033
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -070077#define BWR_ENABLE (1 << 4)
78#define BRR_ENABLE (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010079#define INIT_STREAM (1 << 1)
80#define DP_SELECT (1 << 21)
81#define DDIR (1 << 4)
82#define DMA_EN 0x1
83#define MSBS (1 << 5)
84#define BCE (1 << 1)
85#define FOUR_BIT (1 << 1)
Jarkko Lavinen73153012008-11-21 16:49:54 +020086#define DW8 (1 << 5)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +010087#define CC 0x1
88#define TC 0x02
89#define OD 0x1
90#define ERR (1 << 15)
91#define CMD_TIMEOUT (1 << 16)
92#define DATA_TIMEOUT (1 << 20)
93#define CMD_CRC (1 << 17)
94#define DATA_CRC (1 << 21)
95#define CARD_ERR (1 << 28)
96#define STAT_CLEAR 0xFFFFFFFF
97#define INIT_STREAM_CMD 0x00000000
98#define DUAL_VOLT_OCR_BIT 7
99#define SRC (1 << 25)
100#define SRD (1 << 26)
Denis Karpov11dd62a2009-09-22 16:44:43 -0700101#define SOFTRESET (1 << 1)
102#define RESETDONE (1 << 0)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100103
104/*
105 * FIXME: Most likely all the data using these _DEVID defines should come
106 * from the platform_data, or implemented in controller and slot specific
107 * functions.
108 */
109#define OMAP_MMC1_DEVID 0
110#define OMAP_MMC2_DEVID 1
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000111#define OMAP_MMC3_DEVID 2
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100112
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100113#define MMC_TIMEOUT_MS 20
114#define OMAP_MMC_MASTER_CLOCK 96000000
115#define DRIVER_NAME "mmci-omap-hs"
116
Denis Karpovdd498ef2009-09-22 16:44:49 -0700117/* Timeouts for entering power saving states on inactivity, msec */
118#define OMAP_MMC_DISABLED_TIMEOUT 100
Jarkko Lavinen13189e72009-09-22 16:44:53 -0700119#define OMAP_MMC_SLEEP_TIMEOUT 1000
120#define OMAP_MMC_OFF_TIMEOUT 8000
Denis Karpovdd498ef2009-09-22 16:44:49 -0700121
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100122/*
123 * One controller can have multiple slots, like on some omap boards using
124 * omap.c controller driver. Luckily this is not currently done on any known
125 * omap_hsmmc.c device.
126 */
127#define mmc_slot(host) (host->pdata->slots[host->slot_id])
128
129/*
130 * MMC Host controller read/write API's
131 */
132#define OMAP_HSMMC_READ(base, reg) \
133 __raw_readl((base) + OMAP_HSMMC_##reg)
134
135#define OMAP_HSMMC_WRITE(base, reg, val) \
136 __raw_writel((val), (base) + OMAP_HSMMC_##reg)
137
138struct mmc_omap_host {
139 struct device *dev;
140 struct mmc_host *mmc;
141 struct mmc_request *mrq;
142 struct mmc_command *cmd;
143 struct mmc_data *data;
144 struct clk *fclk;
145 struct clk *iclk;
146 struct clk *dbclk;
147 struct semaphore sem;
148 struct work_struct mmc_carddetect_work;
149 void __iomem *base;
150 resource_size_t mapbase;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700151 spinlock_t irq_lock; /* Prevent races with irq handler */
152 unsigned long flags;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100153 unsigned int id;
154 unsigned int dma_len;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200155 unsigned int dma_sg_idx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100156 unsigned char bus_mode;
Adrian Huntera3621462009-09-22 16:44:42 -0700157 unsigned char power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100158 u32 *buffer;
159 u32 bytesleft;
160 int suspended;
161 int irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100162 int use_dma, dma_ch;
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000163 int dma_line_tx, dma_line_rx;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100164 int slot_id;
165 int dbclk_enabled;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200166 int response_busy;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700167 int context_loss;
Denis Karpovdd498ef2009-09-22 16:44:49 -0700168 int dpm_state;
Adrian Hunter623821f2009-09-22 16:44:51 -0700169 int vdd;
Denis Karpov11dd62a2009-09-22 16:44:43 -0700170
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100171 struct omap_mmc_platform_data *pdata;
172};
173
174/*
175 * Stop clock to the card
176 */
177static void omap_mmc_stop_clock(struct mmc_omap_host *host)
178{
179 OMAP_HSMMC_WRITE(host->base, SYSCTL,
180 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
181 if ((OMAP_HSMMC_READ(host->base, SYSCTL) & CEN) != 0x0)
182 dev_dbg(mmc_dev(host->mmc), "MMC Clock is not stoped\n");
183}
184
Denis Karpov11dd62a2009-09-22 16:44:43 -0700185#ifdef CONFIG_PM
186
187/*
188 * Restore the MMC host context, if it was lost as result of a
189 * power state change.
190 */
191static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
192{
193 struct mmc_ios *ios = &host->mmc->ios;
194 struct omap_mmc_platform_data *pdata = host->pdata;
195 int context_loss = 0;
196 u32 hctl, capa, con;
197 u16 dsor = 0;
198 unsigned long timeout;
199
200 if (pdata->get_context_loss_count) {
201 context_loss = pdata->get_context_loss_count(host->dev);
202 if (context_loss < 0)
203 return 1;
204 }
205
206 dev_dbg(mmc_dev(host->mmc), "context was %slost\n",
207 context_loss == host->context_loss ? "not " : "");
208 if (host->context_loss == context_loss)
209 return 1;
210
211 /* Wait for hardware reset */
212 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
213 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
214 && time_before(jiffies, timeout))
215 ;
216
217 /* Do software reset */
218 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, SOFTRESET);
219 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
220 while ((OMAP_HSMMC_READ(host->base, SYSSTATUS) & RESETDONE) != RESETDONE
221 && time_before(jiffies, timeout))
222 ;
223
224 OMAP_HSMMC_WRITE(host->base, SYSCONFIG,
225 OMAP_HSMMC_READ(host->base, SYSCONFIG) | AUTOIDLE);
226
227 if (host->id == OMAP_MMC1_DEVID) {
228 if (host->power_mode != MMC_POWER_OFF &&
229 (1 << ios->vdd) <= MMC_VDD_23_24)
230 hctl = SDVS18;
231 else
232 hctl = SDVS30;
233 capa = VS30 | VS18;
234 } else {
235 hctl = SDVS18;
236 capa = VS18;
237 }
238
239 OMAP_HSMMC_WRITE(host->base, HCTL,
240 OMAP_HSMMC_READ(host->base, HCTL) | hctl);
241
242 OMAP_HSMMC_WRITE(host->base, CAPA,
243 OMAP_HSMMC_READ(host->base, CAPA) | capa);
244
245 OMAP_HSMMC_WRITE(host->base, HCTL,
246 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
247
248 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
249 while ((OMAP_HSMMC_READ(host->base, HCTL) & SDBP) != SDBP
250 && time_before(jiffies, timeout))
251 ;
252
253 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
254 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
255 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
256
257 /* Do not initialize card-specific things if the power is off */
258 if (host->power_mode == MMC_POWER_OFF)
259 goto out;
260
261 con = OMAP_HSMMC_READ(host->base, CON);
262 switch (ios->bus_width) {
263 case MMC_BUS_WIDTH_8:
264 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
265 break;
266 case MMC_BUS_WIDTH_4:
267 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
268 OMAP_HSMMC_WRITE(host->base, HCTL,
269 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
270 break;
271 case MMC_BUS_WIDTH_1:
272 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
273 OMAP_HSMMC_WRITE(host->base, HCTL,
274 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
275 break;
276 }
277
278 if (ios->clock) {
279 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
280 if (dsor < 1)
281 dsor = 1;
282
283 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
284 dsor++;
285
286 if (dsor > 250)
287 dsor = 250;
288 }
289
290 OMAP_HSMMC_WRITE(host->base, SYSCTL,
291 OMAP_HSMMC_READ(host->base, SYSCTL) & ~CEN);
292 OMAP_HSMMC_WRITE(host->base, SYSCTL, (dsor << 6) | (DTO << 16));
293 OMAP_HSMMC_WRITE(host->base, SYSCTL,
294 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
295
296 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
297 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
298 && time_before(jiffies, timeout))
299 ;
300
301 OMAP_HSMMC_WRITE(host->base, SYSCTL,
302 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
303
304 con = OMAP_HSMMC_READ(host->base, CON);
305 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
306 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
307 else
308 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
309out:
310 host->context_loss = context_loss;
311
312 dev_dbg(mmc_dev(host->mmc), "context is restored\n");
313 return 0;
314}
315
316/*
317 * Save the MMC host context (store the number of power state changes so far).
318 */
319static void omap_mmc_save_ctx(struct mmc_omap_host *host)
320{
321 struct omap_mmc_platform_data *pdata = host->pdata;
322 int context_loss;
323
324 if (pdata->get_context_loss_count) {
325 context_loss = pdata->get_context_loss_count(host->dev);
326 if (context_loss < 0)
327 return;
328 host->context_loss = context_loss;
329 }
330}
331
332#else
333
334static int omap_mmc_restore_ctx(struct mmc_omap_host *host)
335{
336 return 0;
337}
338
339static void omap_mmc_save_ctx(struct mmc_omap_host *host)
340{
341}
342
343#endif
344
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100345/*
346 * Send init stream sequence to card
347 * before sending IDLE command
348 */
349static void send_init_stream(struct mmc_omap_host *host)
350{
351 int reg = 0;
352 unsigned long timeout;
353
354 disable_irq(host->irq);
355 OMAP_HSMMC_WRITE(host->base, CON,
356 OMAP_HSMMC_READ(host->base, CON) | INIT_STREAM);
357 OMAP_HSMMC_WRITE(host->base, CMD, INIT_STREAM_CMD);
358
359 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
360 while ((reg != CC) && time_before(jiffies, timeout))
361 reg = OMAP_HSMMC_READ(host->base, STAT) & CC;
362
363 OMAP_HSMMC_WRITE(host->base, CON,
364 OMAP_HSMMC_READ(host->base, CON) & ~INIT_STREAM);
Adrian Hunterc653a6d2009-09-22 16:44:56 -0700365
366 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
367 OMAP_HSMMC_READ(host->base, STAT);
368
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100369 enable_irq(host->irq);
370}
371
372static inline
373int mmc_omap_cover_is_closed(struct mmc_omap_host *host)
374{
375 int r = 1;
376
Denis Karpov191d1f12009-09-22 16:44:55 -0700377 if (mmc_slot(host).get_cover_state)
378 r = mmc_slot(host).get_cover_state(host->dev, host->slot_id);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100379 return r;
380}
381
382static ssize_t
383mmc_omap_show_cover_switch(struct device *dev, struct device_attribute *attr,
384 char *buf)
385{
386 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
387 struct mmc_omap_host *host = mmc_priv(mmc);
388
389 return sprintf(buf, "%s\n", mmc_omap_cover_is_closed(host) ? "closed" :
390 "open");
391}
392
393static DEVICE_ATTR(cover_switch, S_IRUGO, mmc_omap_show_cover_switch, NULL);
394
395static ssize_t
396mmc_omap_show_slot_name(struct device *dev, struct device_attribute *attr,
397 char *buf)
398{
399 struct mmc_host *mmc = container_of(dev, struct mmc_host, class_dev);
400 struct mmc_omap_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100401
Denis Karpov191d1f12009-09-22 16:44:55 -0700402 return sprintf(buf, "%s\n", mmc_slot(host).name);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100403}
404
405static DEVICE_ATTR(slot_name, S_IRUGO, mmc_omap_show_slot_name, NULL);
406
407/*
408 * Configure the response type and send the cmd.
409 */
410static void
411mmc_omap_start_command(struct mmc_omap_host *host, struct mmc_command *cmd,
412 struct mmc_data *data)
413{
414 int cmdreg = 0, resptype = 0, cmdtype = 0;
415
416 dev_dbg(mmc_dev(host->mmc), "%s: CMD%d, argument 0x%08x\n",
417 mmc_hostname(host->mmc), cmd->opcode, cmd->arg);
418 host->cmd = cmd;
419
420 /*
421 * Clear status bits and enable interrupts
422 */
423 OMAP_HSMMC_WRITE(host->base, STAT, STAT_CLEAR);
424 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
Anand Gadiyarccdfe3a2009-09-22 16:44:21 -0700425
426 if (host->use_dma)
427 OMAP_HSMMC_WRITE(host->base, IE,
428 INT_EN_MASK & ~(BRR_ENABLE | BWR_ENABLE));
429 else
430 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100431
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200432 host->response_busy = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100433 if (cmd->flags & MMC_RSP_PRESENT) {
434 if (cmd->flags & MMC_RSP_136)
435 resptype = 1;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200436 else if (cmd->flags & MMC_RSP_BUSY) {
437 resptype = 3;
438 host->response_busy = 1;
439 } else
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100440 resptype = 2;
441 }
442
443 /*
444 * Unlike OMAP1 controller, the cmdtype does not seem to be based on
445 * ac, bc, adtc, bcr. Only commands ending an open ended transfer need
446 * a val of 0x3, rest 0x0.
447 */
448 if (cmd == host->mrq->stop)
449 cmdtype = 0x3;
450
451 cmdreg = (cmd->opcode << 24) | (resptype << 16) | (cmdtype << 22);
452
453 if (data) {
454 cmdreg |= DP_SELECT | MSBS | BCE;
455 if (data->flags & MMC_DATA_READ)
456 cmdreg |= DDIR;
457 else
458 cmdreg &= ~(DDIR);
459 }
460
461 if (host->use_dma)
462 cmdreg |= DMA_EN;
463
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700464 /*
465 * In an interrupt context (i.e. STOP command), the spinlock is unlocked
466 * by the interrupt handler, otherwise (i.e. for a new request) it is
467 * unlocked here.
468 */
469 if (!in_interrupt())
470 spin_unlock_irqrestore(&host->irq_lock, host->flags);
471
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100472 OMAP_HSMMC_WRITE(host->base, ARG, cmd->arg);
473 OMAP_HSMMC_WRITE(host->base, CMD, cmdreg);
474}
475
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200476static int
477mmc_omap_get_dma_dir(struct mmc_omap_host *host, struct mmc_data *data)
478{
479 if (data->flags & MMC_DATA_WRITE)
480 return DMA_TO_DEVICE;
481 else
482 return DMA_FROM_DEVICE;
483}
484
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100485/*
486 * Notify the transfer complete to MMC core
487 */
488static void
489mmc_omap_xfer_done(struct mmc_omap_host *host, struct mmc_data *data)
490{
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200491 if (!data) {
492 struct mmc_request *mrq = host->mrq;
493
Adrian Hunter23050102009-09-22 16:44:57 -0700494 /* TC before CC from CMD6 - don't know why, but it happens */
495 if (host->cmd && host->cmd->opcode == 6 &&
496 host->response_busy) {
497 host->response_busy = 0;
498 return;
499 }
500
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200501 host->mrq = NULL;
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200502 mmc_request_done(host->mmc, mrq);
503 return;
504 }
505
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100506 host->data = NULL;
507
508 if (host->use_dma && host->dma_ch != -1)
509 dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->dma_len,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200510 mmc_omap_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100511
512 if (!data->error)
513 data->bytes_xfered += data->blocks * (data->blksz);
514 else
515 data->bytes_xfered = 0;
516
517 if (!data->stop) {
518 host->mrq = NULL;
519 mmc_request_done(host->mmc, data->mrq);
520 return;
521 }
522 mmc_omap_start_command(host, data->stop, NULL);
523}
524
525/*
526 * Notify the core about command completion
527 */
528static void
529mmc_omap_cmd_done(struct mmc_omap_host *host, struct mmc_command *cmd)
530{
531 host->cmd = NULL;
532
533 if (cmd->flags & MMC_RSP_PRESENT) {
534 if (cmd->flags & MMC_RSP_136) {
535 /* response type 2 */
536 cmd->resp[3] = OMAP_HSMMC_READ(host->base, RSP10);
537 cmd->resp[2] = OMAP_HSMMC_READ(host->base, RSP32);
538 cmd->resp[1] = OMAP_HSMMC_READ(host->base, RSP54);
539 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP76);
540 } else {
541 /* response types 1, 1b, 3, 4, 5, 6 */
542 cmd->resp[0] = OMAP_HSMMC_READ(host->base, RSP10);
543 }
544 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200545 if ((host->data == NULL && !host->response_busy) || cmd->error) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100546 host->mrq = NULL;
547 mmc_request_done(host->mmc, cmd->mrq);
548 }
549}
550
551/*
552 * DMA clean up for command errors
553 */
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200554static void mmc_dma_cleanup(struct mmc_omap_host *host, int errno)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100555{
Jarkko Lavinen82788ff2008-12-05 12:31:46 +0200556 host->data->error = errno;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100557
558 if (host->use_dma && host->dma_ch != -1) {
559 dma_unmap_sg(mmc_dev(host->mmc), host->data->sg, host->dma_len,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200560 mmc_omap_get_dma_dir(host, host->data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100561 omap_free_dma(host->dma_ch);
562 host->dma_ch = -1;
563 up(&host->sem);
564 }
565 host->data = NULL;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100566}
567
568/*
569 * Readable error output
570 */
571#ifdef CONFIG_MMC_DEBUG
572static void mmc_omap_report_irq(struct mmc_omap_host *host, u32 status)
573{
574 /* --- means reserved bit without definition at documentation */
575 static const char *mmc_omap_status_bits[] = {
576 "CC", "TC", "BGE", "---", "BWR", "BRR", "---", "---", "CIRQ",
577 "OBI", "---", "---", "---", "---", "---", "ERRI", "CTO", "CCRC",
578 "CEB", "CIE", "DTO", "DCRC", "DEB", "---", "ACE", "---",
579 "---", "---", "---", "CERR", "CERR", "BADA", "---", "---", "---"
580 };
581 char res[256];
582 char *buf = res;
583 int len, i;
584
585 len = sprintf(buf, "MMC IRQ 0x%x :", status);
586 buf += len;
587
588 for (i = 0; i < ARRAY_SIZE(mmc_omap_status_bits); i++)
589 if (status & (1 << i)) {
590 len = sprintf(buf, " %s", mmc_omap_status_bits[i]);
591 buf += len;
592 }
593
594 dev_dbg(mmc_dev(host->mmc), "%s\n", res);
595}
596#endif /* CONFIG_MMC_DEBUG */
597
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100598/*
599 * MMC controller internal state machines reset
600 *
601 * Used to reset command or data internal state machines, using respectively
602 * SRC or SRD bit of SYSCTL register
603 * Can be called from interrupt context
604 */
605static inline void mmc_omap_reset_controller_fsm(struct mmc_omap_host *host,
606 unsigned long bit)
607{
608 unsigned long i = 0;
609 unsigned long limit = (loops_per_jiffy *
610 msecs_to_jiffies(MMC_TIMEOUT_MS));
611
612 OMAP_HSMMC_WRITE(host->base, SYSCTL,
613 OMAP_HSMMC_READ(host->base, SYSCTL) | bit);
614
615 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & bit) &&
616 (i++ < limit))
617 cpu_relax();
618
619 if (OMAP_HSMMC_READ(host->base, SYSCTL) & bit)
620 dev_err(mmc_dev(host->mmc),
621 "Timeout waiting on controller reset in %s\n",
622 __func__);
623}
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100624
625/*
626 * MMC controller IRQ handler
627 */
628static irqreturn_t mmc_omap_irq(int irq, void *dev_id)
629{
630 struct mmc_omap_host *host = dev_id;
631 struct mmc_data *data;
632 int end_cmd = 0, end_trans = 0, status;
633
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700634 spin_lock(&host->irq_lock);
635
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200636 if (host->mrq == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100637 OMAP_HSMMC_WRITE(host->base, STAT,
638 OMAP_HSMMC_READ(host->base, STAT));
Kevin Hilman00adadc2009-04-06 15:01:19 +0300639 /* Flush posted write */
640 OMAP_HSMMC_READ(host->base, STAT);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700641 spin_unlock(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100642 return IRQ_HANDLED;
643 }
644
645 data = host->data;
646 status = OMAP_HSMMC_READ(host->base, STAT);
647 dev_dbg(mmc_dev(host->mmc), "IRQ Status is %x\n", status);
648
649 if (status & ERR) {
650#ifdef CONFIG_MMC_DEBUG
651 mmc_omap_report_irq(host, status);
652#endif
653 if ((status & CMD_TIMEOUT) ||
654 (status & CMD_CRC)) {
655 if (host->cmd) {
656 if (status & CMD_TIMEOUT) {
Denis Karpov191d1f12009-09-22 16:44:55 -0700657 mmc_omap_reset_controller_fsm(host,
658 SRC);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100659 host->cmd->error = -ETIMEDOUT;
660 } else {
661 host->cmd->error = -EILSEQ;
662 }
663 end_cmd = 1;
664 }
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200665 if (host->data || host->response_busy) {
666 if (host->data)
667 mmc_dma_cleanup(host, -ETIMEDOUT);
668 host->response_busy = 0;
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100669 mmc_omap_reset_controller_fsm(host, SRD);
Jean Pihetc232f452009-02-11 13:11:39 -0800670 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100671 }
672 if ((status & DATA_TIMEOUT) ||
673 (status & DATA_CRC)) {
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200674 if (host->data || host->response_busy) {
675 int err = (status & DATA_TIMEOUT) ?
676 -ETIMEDOUT : -EILSEQ;
677
678 if (host->data)
679 mmc_dma_cleanup(host, err);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100680 else
Adrian Hunter4a694dc2009-01-12 16:13:08 +0200681 host->mrq->cmd->error = err;
682 host->response_busy = 0;
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100683 mmc_omap_reset_controller_fsm(host, SRD);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100684 end_trans = 1;
685 }
686 }
687 if (status & CARD_ERR) {
688 dev_dbg(mmc_dev(host->mmc),
689 "Ignoring card err CMD%d\n", host->cmd->opcode);
690 if (host->cmd)
691 end_cmd = 1;
692 if (host->data)
693 end_trans = 1;
694 }
695 }
696
697 OMAP_HSMMC_WRITE(host->base, STAT, status);
Kevin Hilman00adadc2009-04-06 15:01:19 +0300698 /* Flush posted write */
699 OMAP_HSMMC_READ(host->base, STAT);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100700
Jarkko Lavinena8fe29d2009-04-08 11:18:32 +0300701 if (end_cmd || ((status & CC) && host->cmd))
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100702 mmc_omap_cmd_done(host, host->cmd);
Jarkko Lavinen0a40e642009-09-22 16:44:54 -0700703 if ((end_trans || (status & TC)) && host->mrq)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100704 mmc_omap_xfer_done(host, data);
705
Adrian Hunter4dffd7a2009-09-22 16:44:58 -0700706 spin_unlock(&host->irq_lock);
707
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100708 return IRQ_HANDLED;
709}
710
Adrian Huntere13bb302009-03-12 17:08:26 +0200711static void set_sd_bus_power(struct mmc_omap_host *host)
712{
713 unsigned long i;
714
715 OMAP_HSMMC_WRITE(host->base, HCTL,
716 OMAP_HSMMC_READ(host->base, HCTL) | SDBP);
717 for (i = 0; i < loops_per_jiffy; i++) {
718 if (OMAP_HSMMC_READ(host->base, HCTL) & SDBP)
719 break;
720 cpu_relax();
721 }
722}
723
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100724/*
David Brownelleb250822009-02-17 14:49:01 -0800725 * Switch MMC interface voltage ... only relevant for MMC1.
726 *
727 * MMC2 and MMC3 use fixed 1.8V levels, and maybe a transceiver.
728 * The MMC2 transceiver controls are used instead of DAT4..DAT7.
729 * Some chips, like eMMC ones, use internal transceivers.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100730 */
731static int omap_mmc_switch_opcond(struct mmc_omap_host *host, int vdd)
732{
733 u32 reg_val = 0;
734 int ret;
735
736 /* Disable the clocks */
737 clk_disable(host->fclk);
738 clk_disable(host->iclk);
739 clk_disable(host->dbclk);
740
741 /* Turn the power off */
742 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
743 if (ret != 0)
744 goto err;
745
746 /* Turn the power ON with given VDD 1.8 or 3.0v */
747 ret = mmc_slot(host).set_power(host->dev, host->slot_id, 1, vdd);
748 if (ret != 0)
749 goto err;
750
751 clk_enable(host->fclk);
752 clk_enable(host->iclk);
753 clk_enable(host->dbclk);
754
755 OMAP_HSMMC_WRITE(host->base, HCTL,
756 OMAP_HSMMC_READ(host->base, HCTL) & SDVSCLR);
757 reg_val = OMAP_HSMMC_READ(host->base, HCTL);
David Brownelleb250822009-02-17 14:49:01 -0800758
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100759 /*
760 * If a MMC dual voltage card is detected, the set_ios fn calls
761 * this fn with VDD bit set for 1.8V. Upon card removal from the
762 * slot, omap_mmc_set_ios sets the VDD back to 3V on MMC_POWER_OFF.
763 *
David Brownelleb250822009-02-17 14:49:01 -0800764 * Cope with a bit of slop in the range ... per data sheets:
765 * - "1.8V" for vdds_mmc1/vdds_mmc1a can be up to 2.45V max,
766 * but recommended values are 1.71V to 1.89V
767 * - "3.0V" for vdds_mmc1/vdds_mmc1a can be up to 3.5V max,
768 * but recommended values are 2.7V to 3.3V
769 *
770 * Board setup code shouldn't permit anything very out-of-range.
771 * TWL4030-family VMMC1 and VSIM regulators are fine (avoiding the
772 * middle range) but VSIM can't power DAT4..DAT7 at more than 3V.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100773 */
David Brownelleb250822009-02-17 14:49:01 -0800774 if ((1 << vdd) <= MMC_VDD_23_24)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100775 reg_val |= SDVS18;
David Brownelleb250822009-02-17 14:49:01 -0800776 else
777 reg_val |= SDVS30;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100778
779 OMAP_HSMMC_WRITE(host->base, HCTL, reg_val);
Adrian Huntere13bb302009-03-12 17:08:26 +0200780 set_sd_bus_power(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100781
782 return 0;
783err:
784 dev_dbg(mmc_dev(host->mmc), "Unable to switch operating voltage\n");
785 return ret;
786}
787
788/*
789 * Work Item to notify the core about card insertion/removal
790 */
791static void mmc_omap_detect(struct work_struct *work)
792{
793 struct mmc_omap_host *host = container_of(work, struct mmc_omap_host,
794 mmc_carddetect_work);
David Brownell249d0fa2009-02-04 14:42:03 -0800795 struct omap_mmc_slot_data *slot = &mmc_slot(host);
Adrian Huntera6b22402009-09-22 16:44:45 -0700796 int carddetect;
David Brownell249d0fa2009-02-04 14:42:03 -0800797
Adrian Huntera6b22402009-09-22 16:44:45 -0700798 if (host->suspended)
799 return;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100800
801 sysfs_notify(&host->mmc->class_dev.kobj, NULL, "cover_switch");
Adrian Huntera6b22402009-09-22 16:44:45 -0700802
Denis Karpov191d1f12009-09-22 16:44:55 -0700803 if (slot->card_detect)
Adrian Huntera6b22402009-09-22 16:44:45 -0700804 carddetect = slot->card_detect(slot->card_detect_irq);
805 else
806 carddetect = -ENOSYS;
807
808 if (carddetect) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100809 mmc_detect_change(host->mmc, (HZ * 200) / 1000);
810 } else {
Adrian Hunter5e2ea612009-09-22 16:44:39 -0700811 mmc_host_enable(host->mmc);
Jean Pihet3ebf74b2009-02-06 16:42:51 +0100812 mmc_omap_reset_controller_fsm(host, SRD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -0700813 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100814 mmc_detect_change(host->mmc, (HZ * 50) / 1000);
815 }
816}
817
818/*
819 * ISR for handling card insertion and removal
820 */
821static irqreturn_t omap_mmc_cd_handler(int irq, void *dev_id)
822{
823 struct mmc_omap_host *host = (struct mmc_omap_host *)dev_id;
824
Adrian Huntera6b22402009-09-22 16:44:45 -0700825 if (host->suspended)
826 return IRQ_HANDLED;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100827 schedule_work(&host->mmc_carddetect_work);
828
829 return IRQ_HANDLED;
830}
831
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200832static int mmc_omap_get_dma_sync_dev(struct mmc_omap_host *host,
833 struct mmc_data *data)
834{
835 int sync_dev;
836
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +0000837 if (data->flags & MMC_DATA_WRITE)
838 sync_dev = host->dma_line_tx;
839 else
840 sync_dev = host->dma_line_rx;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200841 return sync_dev;
842}
843
844static void mmc_omap_config_dma_params(struct mmc_omap_host *host,
845 struct mmc_data *data,
846 struct scatterlist *sgl)
847{
848 int blksz, nblk, dma_ch;
849
850 dma_ch = host->dma_ch;
851 if (data->flags & MMC_DATA_WRITE) {
852 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
853 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
854 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
855 sg_dma_address(sgl), 0, 0);
856 } else {
857 omap_set_dma_src_params(dma_ch, 0, OMAP_DMA_AMODE_CONSTANT,
Denis Karpov191d1f12009-09-22 16:44:55 -0700858 (host->mapbase + OMAP_HSMMC_DATA), 0, 0);
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200859 omap_set_dma_dest_params(dma_ch, 0, OMAP_DMA_AMODE_POST_INC,
860 sg_dma_address(sgl), 0, 0);
861 }
862
863 blksz = host->data->blksz;
864 nblk = sg_dma_len(sgl) / blksz;
865
866 omap_set_dma_transfer_params(dma_ch, OMAP_DMA_DATA_TYPE_S32,
867 blksz / 4, nblk, OMAP_DMA_SYNC_FRAME,
868 mmc_omap_get_dma_sync_dev(host, data),
869 !(data->flags & MMC_DATA_WRITE));
870
871 omap_start_dma(dma_ch);
872}
873
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100874/*
875 * DMA call back function
876 */
877static void mmc_omap_dma_cb(int lch, u16 ch_status, void *data)
878{
879 struct mmc_omap_host *host = data;
880
881 if (ch_status & OMAP2_DMA_MISALIGNED_ERR_IRQ)
882 dev_dbg(mmc_dev(host->mmc), "MISALIGNED_ADRS_ERR\n");
883
884 if (host->dma_ch < 0)
885 return;
886
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200887 host->dma_sg_idx++;
888 if (host->dma_sg_idx < host->dma_len) {
889 /* Fire up the next transfer. */
890 mmc_omap_config_dma_params(host, host->data,
891 host->data->sg + host->dma_sg_idx);
892 return;
893 }
894
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100895 omap_free_dma(host->dma_ch);
896 host->dma_ch = -1;
897 /*
898 * DMA Callback: run in interrupt context.
Anand Gadiyar85b84322009-04-15 17:44:58 +0530899 * mutex_unlock will throw a kernel warning if used.
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100900 */
901 up(&host->sem);
902}
903
904/*
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100905 * Routine to configure and start DMA for the MMC card
906 */
907static int
908mmc_omap_start_dma_transfer(struct mmc_omap_host *host, struct mmc_request *req)
909{
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200910 int dma_ch = 0, ret = 0, err = 1, i;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100911 struct mmc_data *data = req->data;
912
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200913 /* Sanity check: all the SG entries must be aligned by block size. */
Jarkko Lavinena3f406f2009-09-22 16:44:46 -0700914 for (i = 0; i < data->sg_len; i++) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200915 struct scatterlist *sgl;
916
917 sgl = data->sg + i;
918 if (sgl->length % data->blksz)
919 return -EINVAL;
920 }
921 if ((data->blksz % 4) != 0)
922 /* REVISIT: The MMC buffer increments only when MSB is written.
923 * Return error for blksz which is non multiple of four.
924 */
925 return -EINVAL;
926
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100927 /*
928 * If for some reason the DMA transfer is still active,
929 * we wait for timeout period and free the dma
930 */
931 if (host->dma_ch != -1) {
932 set_current_state(TASK_UNINTERRUPTIBLE);
933 schedule_timeout(100);
934 if (down_trylock(&host->sem)) {
935 omap_free_dma(host->dma_ch);
936 host->dma_ch = -1;
937 up(&host->sem);
938 return err;
939 }
940 } else {
941 if (down_trylock(&host->sem))
942 return err;
943 }
944
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200945 ret = omap_request_dma(mmc_omap_get_dma_sync_dev(host, data), "MMC/SD",
Denis Karpov191d1f12009-09-22 16:44:55 -0700946 mmc_omap_dma_cb, host, &dma_ch);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100947 if (ret != 0) {
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200948 dev_err(mmc_dev(host->mmc),
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100949 "%s: omap_request_dma() failed with %d\n",
950 mmc_hostname(host->mmc), ret);
951 return ret;
952 }
953
954 host->dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200955 data->sg_len, mmc_omap_get_dma_dir(host, data));
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100956 host->dma_ch = dma_ch;
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200957 host->dma_sg_idx = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100958
Juha Yrjola0ccd76d2008-11-14 15:22:00 +0200959 mmc_omap_config_dma_params(host, data, data->sg);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100960
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +0100961 return 0;
962}
963
964static void set_data_timeout(struct mmc_omap_host *host,
965 struct mmc_request *req)
966{
967 unsigned int timeout, cycle_ns;
968 uint32_t reg, clkd, dto = 0;
969
970 reg = OMAP_HSMMC_READ(host->base, SYSCTL);
971 clkd = (reg & CLKD_MASK) >> CLKD_SHIFT;
972 if (clkd == 0)
973 clkd = 1;
974
975 cycle_ns = 1000000000 / (clk_get_rate(host->fclk) / clkd);
976 timeout = req->data->timeout_ns / cycle_ns;
977 timeout += req->data->timeout_clks;
978 if (timeout) {
979 while ((timeout & 0x80000000) == 0) {
980 dto += 1;
981 timeout <<= 1;
982 }
983 dto = 31 - dto;
984 timeout <<= 1;
985 if (timeout && dto)
986 dto += 1;
987 if (dto >= 13)
988 dto -= 13;
989 else
990 dto = 0;
991 if (dto > 14)
992 dto = 14;
993 }
994
995 reg &= ~DTO_MASK;
996 reg |= dto << DTO_SHIFT;
997 OMAP_HSMMC_WRITE(host->base, SYSCTL, reg);
998}
999
1000/*
1001 * Configure block length for MMC/SD cards and initiate the transfer.
1002 */
1003static int
1004mmc_omap_prepare_data(struct mmc_omap_host *host, struct mmc_request *req)
1005{
1006 int ret;
1007 host->data = req->data;
1008
1009 if (req->data == NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001010 OMAP_HSMMC_WRITE(host->base, BLK, 0);
1011 return 0;
1012 }
1013
1014 OMAP_HSMMC_WRITE(host->base, BLK, (req->data->blksz)
1015 | (req->data->blocks << 16));
1016 set_data_timeout(host, req);
1017
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001018 if (host->use_dma) {
1019 ret = mmc_omap_start_dma_transfer(host, req);
1020 if (ret != 0) {
1021 dev_dbg(mmc_dev(host->mmc), "MMC start dma failure\n");
1022 return ret;
1023 }
1024 }
1025 return 0;
1026}
1027
1028/*
1029 * Request function. for read/write operation
1030 */
1031static void omap_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
1032{
1033 struct mmc_omap_host *host = mmc_priv(mmc);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001034 int err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001035
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001036 /*
1037 * Prevent races with the interrupt handler because of unexpected
1038 * interrupts, but not if we are already in interrupt context i.e.
1039 * retries.
1040 */
1041 if (!in_interrupt())
1042 spin_lock_irqsave(&host->irq_lock, host->flags);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001043 WARN_ON(host->mrq != NULL);
1044 host->mrq = req;
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001045 err = mmc_omap_prepare_data(host, req);
1046 if (err) {
1047 req->cmd->error = err;
1048 if (req->data)
1049 req->data->error = err;
1050 host->mrq = NULL;
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001051 if (!in_interrupt())
1052 spin_unlock_irqrestore(&host->irq_lock, host->flags);
Jarkko Lavinena3f406f2009-09-22 16:44:46 -07001053 mmc_request_done(mmc, req);
1054 return;
1055 }
1056
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001057 mmc_omap_start_command(host, req->cmd, req->data);
1058}
1059
1060
1061/* Routine to configure clock values. Exposed API to core */
1062static void omap_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1063{
1064 struct mmc_omap_host *host = mmc_priv(mmc);
1065 u16 dsor = 0;
1066 unsigned long regval;
1067 unsigned long timeout;
Jarkko Lavinen73153012008-11-21 16:49:54 +02001068 u32 con;
Adrian Huntera3621462009-09-22 16:44:42 -07001069 int do_send_init_stream = 0;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001070
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001071 mmc_host_enable(host->mmc);
1072
Adrian Huntera3621462009-09-22 16:44:42 -07001073 if (ios->power_mode != host->power_mode) {
1074 switch (ios->power_mode) {
1075 case MMC_POWER_OFF:
1076 mmc_slot(host).set_power(host->dev, host->slot_id,
1077 0, 0);
Adrian Hunter623821f2009-09-22 16:44:51 -07001078 host->vdd = 0;
Adrian Huntera3621462009-09-22 16:44:42 -07001079 break;
1080 case MMC_POWER_UP:
1081 mmc_slot(host).set_power(host->dev, host->slot_id,
1082 1, ios->vdd);
Adrian Hunter623821f2009-09-22 16:44:51 -07001083 host->vdd = ios->vdd;
Adrian Huntera3621462009-09-22 16:44:42 -07001084 break;
1085 case MMC_POWER_ON:
1086 do_send_init_stream = 1;
1087 break;
1088 }
1089 host->power_mode = ios->power_mode;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001090 }
1091
Denis Karpovdd498ef2009-09-22 16:44:49 -07001092 /* FIXME: set registers based only on changes to ios */
1093
Jarkko Lavinen73153012008-11-21 16:49:54 +02001094 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001095 switch (mmc->ios.bus_width) {
Jarkko Lavinen73153012008-11-21 16:49:54 +02001096 case MMC_BUS_WIDTH_8:
1097 OMAP_HSMMC_WRITE(host->base, CON, con | DW8);
1098 break;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001099 case MMC_BUS_WIDTH_4:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001100 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001101 OMAP_HSMMC_WRITE(host->base, HCTL,
1102 OMAP_HSMMC_READ(host->base, HCTL) | FOUR_BIT);
1103 break;
1104 case MMC_BUS_WIDTH_1:
Jarkko Lavinen73153012008-11-21 16:49:54 +02001105 OMAP_HSMMC_WRITE(host->base, CON, con & ~DW8);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001106 OMAP_HSMMC_WRITE(host->base, HCTL,
1107 OMAP_HSMMC_READ(host->base, HCTL) & ~FOUR_BIT);
1108 break;
1109 }
1110
1111 if (host->id == OMAP_MMC1_DEVID) {
David Brownelleb250822009-02-17 14:49:01 -08001112 /* Only MMC1 can interface at 3V without some flavor
1113 * of external transceiver; but they all handle 1.8V.
1114 */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001115 if ((OMAP_HSMMC_READ(host->base, HCTL) & SDVSDET) &&
1116 (ios->vdd == DUAL_VOLT_OCR_BIT)) {
1117 /*
1118 * The mmc_select_voltage fn of the core does
1119 * not seem to set the power_mode to
1120 * MMC_POWER_UP upon recalculating the voltage.
1121 * vdd 1.8v.
1122 */
1123 if (omap_mmc_switch_opcond(host, ios->vdd) != 0)
1124 dev_dbg(mmc_dev(host->mmc),
1125 "Switch operation failed\n");
1126 }
1127 }
1128
1129 if (ios->clock) {
1130 dsor = OMAP_MMC_MASTER_CLOCK / ios->clock;
1131 if (dsor < 1)
1132 dsor = 1;
1133
1134 if (OMAP_MMC_MASTER_CLOCK / dsor > ios->clock)
1135 dsor++;
1136
1137 if (dsor > 250)
1138 dsor = 250;
1139 }
1140 omap_mmc_stop_clock(host);
1141 regval = OMAP_HSMMC_READ(host->base, SYSCTL);
1142 regval = regval & ~(CLKD_MASK);
1143 regval = regval | (dsor << 6) | (DTO << 16);
1144 OMAP_HSMMC_WRITE(host->base, SYSCTL, regval);
1145 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1146 OMAP_HSMMC_READ(host->base, SYSCTL) | ICE);
1147
1148 /* Wait till the ICS bit is set */
1149 timeout = jiffies + msecs_to_jiffies(MMC_TIMEOUT_MS);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001150 while ((OMAP_HSMMC_READ(host->base, SYSCTL) & ICS) != ICS
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001151 && time_before(jiffies, timeout))
1152 msleep(1);
1153
1154 OMAP_HSMMC_WRITE(host->base, SYSCTL,
1155 OMAP_HSMMC_READ(host->base, SYSCTL) | CEN);
1156
Adrian Huntera3621462009-09-22 16:44:42 -07001157 if (do_send_init_stream)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001158 send_init_stream(host);
1159
Denis Karpovabb28e72009-09-22 16:44:44 -07001160 con = OMAP_HSMMC_READ(host->base, CON);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001161 if (ios->bus_mode == MMC_BUSMODE_OPENDRAIN)
Denis Karpovabb28e72009-09-22 16:44:44 -07001162 OMAP_HSMMC_WRITE(host->base, CON, con | OD);
1163 else
1164 OMAP_HSMMC_WRITE(host->base, CON, con & ~OD);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001165
Denis Karpovdd498ef2009-09-22 16:44:49 -07001166 if (host->power_mode == MMC_POWER_OFF)
1167 mmc_host_disable(host->mmc);
1168 else
1169 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001170}
1171
1172static int omap_hsmmc_get_cd(struct mmc_host *mmc)
1173{
1174 struct mmc_omap_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001175
Denis Karpov191d1f12009-09-22 16:44:55 -07001176 if (!mmc_slot(host).card_detect)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001177 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001178 return mmc_slot(host).card_detect(mmc_slot(host).card_detect_irq);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001179}
1180
1181static int omap_hsmmc_get_ro(struct mmc_host *mmc)
1182{
1183 struct mmc_omap_host *host = mmc_priv(mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001184
Denis Karpov191d1f12009-09-22 16:44:55 -07001185 if (!mmc_slot(host).get_ro)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001186 return -ENOSYS;
Denis Karpov191d1f12009-09-22 16:44:55 -07001187 return mmc_slot(host).get_ro(host->dev, 0);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001188}
1189
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001190static void omap_hsmmc_init(struct mmc_omap_host *host)
1191{
1192 u32 hctl, capa, value;
1193
1194 /* Only MMC1 supports 3.0V */
1195 if (host->id == OMAP_MMC1_DEVID) {
1196 hctl = SDVS30;
1197 capa = VS30 | VS18;
1198 } else {
1199 hctl = SDVS18;
1200 capa = VS18;
1201 }
1202
1203 value = OMAP_HSMMC_READ(host->base, HCTL) & ~SDVS_MASK;
1204 OMAP_HSMMC_WRITE(host->base, HCTL, value | hctl);
1205
1206 value = OMAP_HSMMC_READ(host->base, CAPA);
1207 OMAP_HSMMC_WRITE(host->base, CAPA, value | capa);
1208
1209 /* Set the controller to AUTO IDLE mode */
1210 value = OMAP_HSMMC_READ(host->base, SYSCONFIG);
1211 OMAP_HSMMC_WRITE(host->base, SYSCONFIG, value | AUTOIDLE);
1212
1213 /* Set SD bus power bit */
Adrian Huntere13bb302009-03-12 17:08:26 +02001214 set_sd_bus_power(host);
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001215}
1216
Denis Karpovdd498ef2009-09-22 16:44:49 -07001217/*
1218 * Dynamic power saving handling, FSM:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001219 * ENABLED -> DISABLED -> CARDSLEEP / REGSLEEP -> OFF
1220 * ^___________| | |
1221 * |______________________|______________________|
Denis Karpovdd498ef2009-09-22 16:44:49 -07001222 *
1223 * ENABLED: mmc host is fully functional
1224 * DISABLED: fclk is off
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001225 * CARDSLEEP: fclk is off, card is asleep, voltage regulator is asleep
1226 * REGSLEEP: fclk is off, voltage regulator is asleep
1227 * OFF: fclk is off, voltage regulator is off
Denis Karpovdd498ef2009-09-22 16:44:49 -07001228 *
1229 * Transition handlers return the timeout for the next state transition
1230 * or negative error.
1231 */
1232
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001233enum {ENABLED = 0, DISABLED, CARDSLEEP, REGSLEEP, OFF};
Denis Karpovdd498ef2009-09-22 16:44:49 -07001234
1235/* Handler for [ENABLED -> DISABLED] transition */
1236static int omap_mmc_enabled_to_disabled(struct mmc_omap_host *host)
1237{
1238 omap_mmc_save_ctx(host);
1239 clk_disable(host->fclk);
1240 host->dpm_state = DISABLED;
1241
1242 dev_dbg(mmc_dev(host->mmc), "ENABLED -> DISABLED\n");
1243
1244 if (host->power_mode == MMC_POWER_OFF)
1245 return 0;
1246
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001247 return msecs_to_jiffies(OMAP_MMC_SLEEP_TIMEOUT);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001248}
1249
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001250/* Handler for [DISABLED -> REGSLEEP / CARDSLEEP] transition */
1251static int omap_mmc_disabled_to_sleep(struct mmc_omap_host *host)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001252{
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001253 int err, new_state;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001254
1255 if (!mmc_try_claim_host(host->mmc))
1256 return 0;
1257
1258 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001259 omap_mmc_restore_ctx(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001260 if (mmc_card_can_sleep(host->mmc)) {
1261 err = mmc_card_sleep(host->mmc);
1262 if (err < 0) {
1263 clk_disable(host->fclk);
1264 mmc_release_host(host->mmc);
1265 return err;
1266 }
1267 new_state = CARDSLEEP;
1268 } else
1269 new_state = REGSLEEP;
1270 if (mmc_slot(host).set_sleep)
1271 mmc_slot(host).set_sleep(host->dev, host->slot_id, 1, 0,
1272 new_state == CARDSLEEP);
1273 /* FIXME: turn off bus power and perhaps interrupts too */
1274 clk_disable(host->fclk);
1275 host->dpm_state = new_state;
1276
1277 mmc_release_host(host->mmc);
1278
1279 dev_dbg(mmc_dev(host->mmc), "DISABLED -> %s\n",
1280 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001281
1282 if ((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1283 mmc_slot(host).card_detect ||
1284 (mmc_slot(host).get_cover_state &&
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001285 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))
1286 return msecs_to_jiffies(OMAP_MMC_OFF_TIMEOUT);
1287
1288 return 0;
1289}
1290
1291/* Handler for [REGSLEEP / CARDSLEEP -> OFF] transition */
1292static int omap_mmc_sleep_to_off(struct mmc_omap_host *host)
1293{
1294 if (!mmc_try_claim_host(host->mmc))
1295 return 0;
1296
1297 if (!((host->mmc->caps & MMC_CAP_NONREMOVABLE) ||
1298 mmc_slot(host).card_detect ||
1299 (mmc_slot(host).get_cover_state &&
1300 mmc_slot(host).get_cover_state(host->dev, host->slot_id)))) {
1301 mmc_release_host(host->mmc);
1302 return 0;
Adrian Hunter623821f2009-09-22 16:44:51 -07001303 }
Denis Karpovdd498ef2009-09-22 16:44:49 -07001304
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001305 mmc_slot(host).set_power(host->dev, host->slot_id, 0, 0);
1306 host->vdd = 0;
1307 host->power_mode = MMC_POWER_OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001308
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001309 dev_dbg(mmc_dev(host->mmc), "%s -> OFF\n",
1310 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001311
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001312 host->dpm_state = OFF;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001313
1314 mmc_release_host(host->mmc);
1315
1316 return 0;
1317}
1318
1319/* Handler for [DISABLED -> ENABLED] transition */
1320static int omap_mmc_disabled_to_enabled(struct mmc_omap_host *host)
1321{
1322 int err;
1323
1324 err = clk_enable(host->fclk);
1325 if (err < 0)
1326 return err;
1327
1328 omap_mmc_restore_ctx(host);
1329
1330 host->dpm_state = ENABLED;
1331
1332 dev_dbg(mmc_dev(host->mmc), "DISABLED -> ENABLED\n");
1333
1334 return 0;
1335}
1336
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001337/* Handler for [SLEEP -> ENABLED] transition */
1338static int omap_mmc_sleep_to_enabled(struct mmc_omap_host *host)
1339{
1340 if (!mmc_try_claim_host(host->mmc))
1341 return 0;
1342
1343 clk_enable(host->fclk);
1344 omap_mmc_restore_ctx(host);
1345 if (mmc_slot(host).set_sleep)
1346 mmc_slot(host).set_sleep(host->dev, host->slot_id, 0,
1347 host->vdd, host->dpm_state == CARDSLEEP);
1348 if (mmc_card_can_sleep(host->mmc))
1349 mmc_card_awake(host->mmc);
1350
1351 dev_dbg(mmc_dev(host->mmc), "%s -> ENABLED\n",
1352 host->dpm_state == CARDSLEEP ? "CARDSLEEP" : "REGSLEEP");
1353
1354 host->dpm_state = ENABLED;
1355
1356 mmc_release_host(host->mmc);
1357
1358 return 0;
1359}
1360
Denis Karpovdd498ef2009-09-22 16:44:49 -07001361/* Handler for [OFF -> ENABLED] transition */
1362static int omap_mmc_off_to_enabled(struct mmc_omap_host *host)
1363{
1364 clk_enable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001365
1366 omap_mmc_restore_ctx(host);
1367 omap_hsmmc_init(host);
1368 mmc_power_restore_host(host->mmc);
1369
1370 host->dpm_state = ENABLED;
1371
1372 dev_dbg(mmc_dev(host->mmc), "OFF -> ENABLED\n");
1373
1374 return 0;
1375}
1376
1377/*
1378 * Bring MMC host to ENABLED from any other PM state.
1379 */
1380static int omap_mmc_enable(struct mmc_host *mmc)
1381{
1382 struct mmc_omap_host *host = mmc_priv(mmc);
1383
1384 switch (host->dpm_state) {
1385 case DISABLED:
1386 return omap_mmc_disabled_to_enabled(host);
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001387 case CARDSLEEP:
Adrian Hunter623821f2009-09-22 16:44:51 -07001388 case REGSLEEP:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001389 return omap_mmc_sleep_to_enabled(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001390 case OFF:
1391 return omap_mmc_off_to_enabled(host);
1392 default:
1393 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1394 return -EINVAL;
1395 }
1396}
1397
1398/*
1399 * Bring MMC host in PM state (one level deeper).
1400 */
1401static int omap_mmc_disable(struct mmc_host *mmc, int lazy)
1402{
1403 struct mmc_omap_host *host = mmc_priv(mmc);
1404
1405 switch (host->dpm_state) {
1406 case ENABLED: {
1407 int delay;
1408
1409 delay = omap_mmc_enabled_to_disabled(host);
1410 if (lazy || delay < 0)
1411 return delay;
1412 return 0;
1413 }
1414 case DISABLED:
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001415 return omap_mmc_disabled_to_sleep(host);
1416 case CARDSLEEP:
1417 case REGSLEEP:
1418 return omap_mmc_sleep_to_off(host);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001419 default:
1420 dev_dbg(mmc_dev(host->mmc), "UNKNOWN state\n");
1421 return -EINVAL;
1422 }
1423}
1424
1425static int omap_mmc_enable_fclk(struct mmc_host *mmc)
1426{
1427 struct mmc_omap_host *host = mmc_priv(mmc);
1428 int err;
1429
1430 err = clk_enable(host->fclk);
1431 if (err)
1432 return err;
1433 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: enabled\n");
1434 omap_mmc_restore_ctx(host);
1435 return 0;
1436}
1437
1438static int omap_mmc_disable_fclk(struct mmc_host *mmc, int lazy)
1439{
1440 struct mmc_omap_host *host = mmc_priv(mmc);
1441
1442 omap_mmc_save_ctx(host);
1443 clk_disable(host->fclk);
1444 dev_dbg(mmc_dev(host->mmc), "mmc_fclk: disabled\n");
1445 return 0;
1446}
1447
1448static const struct mmc_host_ops mmc_omap_ops = {
1449 .enable = omap_mmc_enable_fclk,
1450 .disable = omap_mmc_disable_fclk,
1451 .request = omap_mmc_request,
1452 .set_ios = omap_mmc_set_ios,
1453 .get_cd = omap_hsmmc_get_cd,
1454 .get_ro = omap_hsmmc_get_ro,
1455 /* NYET -- enable_sdio_irq */
1456};
1457
1458static const struct mmc_host_ops mmc_omap_ps_ops = {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001459 .enable = omap_mmc_enable,
1460 .disable = omap_mmc_disable,
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001461 .request = omap_mmc_request,
1462 .set_ios = omap_mmc_set_ios,
1463 .get_cd = omap_hsmmc_get_cd,
1464 .get_ro = omap_hsmmc_get_ro,
1465 /* NYET -- enable_sdio_irq */
1466};
1467
Denis Karpovd900f712009-09-22 16:44:38 -07001468#ifdef CONFIG_DEBUG_FS
1469
1470static int mmc_regs_show(struct seq_file *s, void *data)
1471{
1472 struct mmc_host *mmc = s->private;
1473 struct mmc_omap_host *host = mmc_priv(mmc);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001474 struct omap_mmc_platform_data *pdata = host->pdata;
1475 int context_loss = 0;
1476
1477 if (pdata->get_context_loss_count)
1478 context_loss = pdata->get_context_loss_count(host->dev);
Denis Karpovd900f712009-09-22 16:44:38 -07001479
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001480 seq_printf(s, "mmc%d:\n"
1481 " enabled:\t%d\n"
Denis Karpovdd498ef2009-09-22 16:44:49 -07001482 " dpm_state:\t%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001483 " nesting_cnt:\t%d\n"
Denis Karpov11dd62a2009-09-22 16:44:43 -07001484 " ctx_loss:\t%d:%d\n"
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001485 "\nregs:\n",
Denis Karpovdd498ef2009-09-22 16:44:49 -07001486 mmc->index, mmc->enabled ? 1 : 0,
1487 host->dpm_state, mmc->nesting_cnt,
Denis Karpov11dd62a2009-09-22 16:44:43 -07001488 host->context_loss, context_loss);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001489
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001490 if (host->suspended || host->dpm_state == OFF) {
Denis Karpovdd498ef2009-09-22 16:44:49 -07001491 seq_printf(s, "host suspended, can't read registers\n");
1492 return 0;
1493 }
1494
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001495 if (clk_enable(host->fclk) != 0) {
1496 seq_printf(s, "can't read the regs\n");
Denis Karpovdd498ef2009-09-22 16:44:49 -07001497 return 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001498 }
Denis Karpovd900f712009-09-22 16:44:38 -07001499
1500 seq_printf(s, "SYSCONFIG:\t0x%08x\n",
1501 OMAP_HSMMC_READ(host->base, SYSCONFIG));
1502 seq_printf(s, "CON:\t\t0x%08x\n",
1503 OMAP_HSMMC_READ(host->base, CON));
1504 seq_printf(s, "HCTL:\t\t0x%08x\n",
1505 OMAP_HSMMC_READ(host->base, HCTL));
1506 seq_printf(s, "SYSCTL:\t\t0x%08x\n",
1507 OMAP_HSMMC_READ(host->base, SYSCTL));
1508 seq_printf(s, "IE:\t\t0x%08x\n",
1509 OMAP_HSMMC_READ(host->base, IE));
1510 seq_printf(s, "ISE:\t\t0x%08x\n",
1511 OMAP_HSMMC_READ(host->base, ISE));
1512 seq_printf(s, "CAPA:\t\t0x%08x\n",
1513 OMAP_HSMMC_READ(host->base, CAPA));
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001514
1515 clk_disable(host->fclk);
Denis Karpovdd498ef2009-09-22 16:44:49 -07001516
Denis Karpovd900f712009-09-22 16:44:38 -07001517 return 0;
1518}
1519
1520static int mmc_regs_open(struct inode *inode, struct file *file)
1521{
1522 return single_open(file, mmc_regs_show, inode->i_private);
1523}
1524
1525static const struct file_operations mmc_regs_fops = {
1526 .open = mmc_regs_open,
1527 .read = seq_read,
1528 .llseek = seq_lseek,
1529 .release = single_release,
1530};
1531
1532static void omap_mmc_debugfs(struct mmc_host *mmc)
1533{
1534 if (mmc->debugfs_root)
1535 debugfs_create_file("regs", S_IRUSR, mmc->debugfs_root,
1536 mmc, &mmc_regs_fops);
1537}
1538
1539#else
1540
1541static void omap_mmc_debugfs(struct mmc_host *mmc)
1542{
1543}
1544
1545#endif
1546
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001547static int __init omap_mmc_probe(struct platform_device *pdev)
1548{
1549 struct omap_mmc_platform_data *pdata = pdev->dev.platform_data;
1550 struct mmc_host *mmc;
1551 struct mmc_omap_host *host = NULL;
1552 struct resource *res;
1553 int ret = 0, irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001554
1555 if (pdata == NULL) {
1556 dev_err(&pdev->dev, "Platform Data is missing\n");
1557 return -ENXIO;
1558 }
1559
1560 if (pdata->nr_slots == 0) {
1561 dev_err(&pdev->dev, "No Slots\n");
1562 return -ENXIO;
1563 }
1564
1565 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1566 irq = platform_get_irq(pdev, 0);
1567 if (res == NULL || irq < 0)
1568 return -ENXIO;
1569
1570 res = request_mem_region(res->start, res->end - res->start + 1,
1571 pdev->name);
1572 if (res == NULL)
1573 return -EBUSY;
1574
1575 mmc = mmc_alloc_host(sizeof(struct mmc_omap_host), &pdev->dev);
1576 if (!mmc) {
1577 ret = -ENOMEM;
1578 goto err;
1579 }
1580
1581 host = mmc_priv(mmc);
1582 host->mmc = mmc;
1583 host->pdata = pdata;
1584 host->dev = &pdev->dev;
1585 host->use_dma = 1;
1586 host->dev->dma_mask = &pdata->dma_mask;
1587 host->dma_ch = -1;
1588 host->irq = irq;
1589 host->id = pdev->id;
1590 host->slot_id = 0;
1591 host->mapbase = res->start;
1592 host->base = ioremap(host->mapbase, SZ_4K);
Adrian Huntera3621462009-09-22 16:44:42 -07001593 host->power_mode = -1;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001594
1595 platform_set_drvdata(pdev, host);
1596 INIT_WORK(&host->mmc_carddetect_work, mmc_omap_detect);
1597
Denis Karpov191d1f12009-09-22 16:44:55 -07001598 if (mmc_slot(host).power_saving)
Denis Karpovdd498ef2009-09-22 16:44:49 -07001599 mmc->ops = &mmc_omap_ps_ops;
1600 else
1601 mmc->ops = &mmc_omap_ops;
1602
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001603 mmc->f_min = 400000;
1604 mmc->f_max = 52000000;
1605
1606 sema_init(&host->sem, 1);
Adrian Hunter4dffd7a2009-09-22 16:44:58 -07001607 spin_lock_init(&host->irq_lock);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001608
Russell King6f7607c2009-01-28 10:22:50 +00001609 host->iclk = clk_get(&pdev->dev, "ick");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001610 if (IS_ERR(host->iclk)) {
1611 ret = PTR_ERR(host->iclk);
1612 host->iclk = NULL;
1613 goto err1;
1614 }
Russell King6f7607c2009-01-28 10:22:50 +00001615 host->fclk = clk_get(&pdev->dev, "fck");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001616 if (IS_ERR(host->fclk)) {
1617 ret = PTR_ERR(host->fclk);
1618 host->fclk = NULL;
1619 clk_put(host->iclk);
1620 goto err1;
1621 }
1622
Denis Karpov11dd62a2009-09-22 16:44:43 -07001623 omap_mmc_save_ctx(host);
1624
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001625 mmc->caps |= MMC_CAP_DISABLE;
Denis Karpovdd498ef2009-09-22 16:44:49 -07001626 mmc_set_disable_delay(mmc, OMAP_MMC_DISABLED_TIMEOUT);
1627 /* we start off in DISABLED state */
1628 host->dpm_state = DISABLED;
1629
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001630 if (mmc_host_enable(host->mmc) != 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001631 clk_put(host->iclk);
1632 clk_put(host->fclk);
1633 goto err1;
1634 }
1635
1636 if (clk_enable(host->iclk) != 0) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001637 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001638 clk_put(host->iclk);
1639 clk_put(host->fclk);
1640 goto err1;
1641 }
1642
1643 host->dbclk = clk_get(&pdev->dev, "mmchsdb_fck");
1644 /*
1645 * MMC can still work without debounce clock.
1646 */
1647 if (IS_ERR(host->dbclk))
1648 dev_warn(mmc_dev(host->mmc), "Failed to get debounce clock\n");
1649 else
1650 if (clk_enable(host->dbclk) != 0)
1651 dev_dbg(mmc_dev(host->mmc), "Enabling debounce"
1652 " clk failed\n");
1653 else
1654 host->dbclk_enabled = 1;
1655
Juha Yrjola0ccd76d2008-11-14 15:22:00 +02001656 /* Since we do only SG emulation, we can have as many segs
1657 * as we want. */
1658 mmc->max_phys_segs = 1024;
1659 mmc->max_hw_segs = 1024;
1660
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001661 mmc->max_blk_size = 512; /* Block Length at max can be 1024 */
1662 mmc->max_blk_count = 0xFFFF; /* No. of Blocks is 16 bits */
1663 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1664 mmc->max_seg_size = mmc->max_req_size;
1665
Jarkko Lavinen13189e72009-09-22 16:44:53 -07001666 mmc->caps |= MMC_CAP_MMC_HIGHSPEED | MMC_CAP_SD_HIGHSPEED |
1667 MMC_CAP_WAIT_WHILE_BUSY;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001668
Denis Karpov191d1f12009-09-22 16:44:55 -07001669 if (mmc_slot(host).wires >= 8)
Jarkko Lavinen73153012008-11-21 16:49:54 +02001670 mmc->caps |= MMC_CAP_8_BIT_DATA;
Denis Karpov191d1f12009-09-22 16:44:55 -07001671 else if (mmc_slot(host).wires >= 4)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001672 mmc->caps |= MMC_CAP_4_BIT_DATA;
1673
Denis Karpov191d1f12009-09-22 16:44:55 -07001674 if (mmc_slot(host).nonremovable)
Adrian Hunter23d99bb2009-09-22 16:44:48 -07001675 mmc->caps |= MMC_CAP_NONREMOVABLE;
1676
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001677 omap_hsmmc_init(host);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001678
Grazvydas Ignotasf3e2f1d2009-01-03 10:36:13 +00001679 /* Select DMA lines */
1680 switch (host->id) {
1681 case OMAP_MMC1_DEVID:
1682 host->dma_line_tx = OMAP24XX_DMA_MMC1_TX;
1683 host->dma_line_rx = OMAP24XX_DMA_MMC1_RX;
1684 break;
1685 case OMAP_MMC2_DEVID:
1686 host->dma_line_tx = OMAP24XX_DMA_MMC2_TX;
1687 host->dma_line_rx = OMAP24XX_DMA_MMC2_RX;
1688 break;
1689 case OMAP_MMC3_DEVID:
1690 host->dma_line_tx = OMAP34XX_DMA_MMC3_TX;
1691 host->dma_line_rx = OMAP34XX_DMA_MMC3_RX;
1692 break;
1693 default:
1694 dev_err(mmc_dev(host->mmc), "Invalid MMC id\n");
1695 goto err_irq;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001696 }
1697
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001698 /* Request IRQ for MMC operations */
1699 ret = request_irq(host->irq, mmc_omap_irq, IRQF_DISABLED,
1700 mmc_hostname(mmc), host);
1701 if (ret) {
1702 dev_dbg(mmc_dev(host->mmc), "Unable to grab HSMMC IRQ\n");
1703 goto err_irq;
1704 }
1705
David Brownellb583f262009-05-28 14:04:03 -07001706 /* initialize power supplies, gpios, etc */
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001707 if (pdata->init != NULL) {
1708 if (pdata->init(&pdev->dev) != 0) {
David Brownellb583f262009-05-28 14:04:03 -07001709 dev_dbg(mmc_dev(host->mmc), "late init error\n");
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001710 goto err_irq_cd_init;
1711 }
1712 }
David Brownellb583f262009-05-28 14:04:03 -07001713 mmc->ocr_avail = mmc_slot(host).ocr_mask;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001714
1715 /* Request IRQ for card detect */
Adrian Huntere1a55f52009-01-26 13:17:25 +02001716 if ((mmc_slot(host).card_detect_irq)) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001717 ret = request_irq(mmc_slot(host).card_detect_irq,
1718 omap_mmc_cd_handler,
1719 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING
1720 | IRQF_DISABLED,
1721 mmc_hostname(mmc), host);
1722 if (ret) {
1723 dev_dbg(mmc_dev(host->mmc),
1724 "Unable to grab MMC CD IRQ\n");
1725 goto err_irq_cd;
1726 }
1727 }
1728
1729 OMAP_HSMMC_WRITE(host->base, ISE, INT_EN_MASK);
1730 OMAP_HSMMC_WRITE(host->base, IE, INT_EN_MASK);
1731
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001732 mmc_host_lazy_disable(host->mmc);
1733
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001734 mmc_add_host(mmc);
1735
Denis Karpov191d1f12009-09-22 16:44:55 -07001736 if (mmc_slot(host).name != NULL) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001737 ret = device_create_file(&mmc->class_dev, &dev_attr_slot_name);
1738 if (ret < 0)
1739 goto err_slot_name;
1740 }
Denis Karpov191d1f12009-09-22 16:44:55 -07001741 if (mmc_slot(host).card_detect_irq && mmc_slot(host).get_cover_state) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001742 ret = device_create_file(&mmc->class_dev,
1743 &dev_attr_cover_switch);
1744 if (ret < 0)
1745 goto err_cover_switch;
1746 }
1747
Denis Karpovd900f712009-09-22 16:44:38 -07001748 omap_mmc_debugfs(mmc);
1749
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001750 return 0;
1751
1752err_cover_switch:
1753 device_remove_file(&mmc->class_dev, &dev_attr_cover_switch);
1754err_slot_name:
1755 mmc_remove_host(mmc);
1756err_irq_cd:
1757 free_irq(mmc_slot(host).card_detect_irq, host);
1758err_irq_cd_init:
1759 free_irq(host->irq, host);
1760err_irq:
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001761 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001762 clk_disable(host->iclk);
1763 clk_put(host->fclk);
1764 clk_put(host->iclk);
1765 if (host->dbclk_enabled) {
1766 clk_disable(host->dbclk);
1767 clk_put(host->dbclk);
1768 }
1769
1770err1:
1771 iounmap(host->base);
1772err:
1773 dev_dbg(mmc_dev(host->mmc), "Probe Failed\n");
1774 release_mem_region(res->start, res->end - res->start + 1);
1775 if (host)
1776 mmc_free_host(mmc);
1777 return ret;
1778}
1779
1780static int omap_mmc_remove(struct platform_device *pdev)
1781{
1782 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1783 struct resource *res;
1784
1785 if (host) {
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001786 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001787 mmc_remove_host(host->mmc);
1788 if (host->pdata->cleanup)
1789 host->pdata->cleanup(&pdev->dev);
1790 free_irq(host->irq, host);
1791 if (mmc_slot(host).card_detect_irq)
1792 free_irq(mmc_slot(host).card_detect_irq, host);
1793 flush_scheduled_work();
1794
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001795 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001796 clk_disable(host->iclk);
1797 clk_put(host->fclk);
1798 clk_put(host->iclk);
1799 if (host->dbclk_enabled) {
1800 clk_disable(host->dbclk);
1801 clk_put(host->dbclk);
1802 }
1803
1804 mmc_free_host(host->mmc);
1805 iounmap(host->base);
1806 }
1807
1808 res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1809 if (res)
1810 release_mem_region(res->start, res->end - res->start + 1);
1811 platform_set_drvdata(pdev, NULL);
1812
1813 return 0;
1814}
1815
1816#ifdef CONFIG_PM
1817static int omap_mmc_suspend(struct platform_device *pdev, pm_message_t state)
1818{
1819 int ret = 0;
1820 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1821
1822 if (host && host->suspended)
1823 return 0;
1824
1825 if (host) {
Adrian Huntera6b22402009-09-22 16:44:45 -07001826 host->suspended = 1;
1827 if (host->pdata->suspend) {
1828 ret = host->pdata->suspend(&pdev->dev,
1829 host->slot_id);
1830 if (ret) {
1831 dev_dbg(mmc_dev(host->mmc),
1832 "Unable to handle MMC board"
1833 " level suspend\n");
1834 host->suspended = 0;
1835 return ret;
1836 }
1837 }
1838 cancel_work_sync(&host->mmc_carddetect_work);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001839 mmc_host_enable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001840 ret = mmc_suspend_host(host->mmc, state);
1841 if (ret == 0) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001842 OMAP_HSMMC_WRITE(host->base, ISE, 0);
1843 OMAP_HSMMC_WRITE(host->base, IE, 0);
1844
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001845
Jarkko Lavinen0683af42009-03-12 15:30:58 +02001846 OMAP_HSMMC_WRITE(host->base, HCTL,
Denis Karpov191d1f12009-09-22 16:44:55 -07001847 OMAP_HSMMC_READ(host->base, HCTL) & ~SDBP);
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001848 mmc_host_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001849 clk_disable(host->iclk);
1850 clk_disable(host->dbclk);
Adrian Huntera6b22402009-09-22 16:44:45 -07001851 } else {
1852 host->suspended = 0;
1853 if (host->pdata->resume) {
1854 ret = host->pdata->resume(&pdev->dev,
1855 host->slot_id);
1856 if (ret)
1857 dev_dbg(mmc_dev(host->mmc),
1858 "Unmask interrupt failed\n");
1859 }
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001860 mmc_host_disable(host->mmc);
Adrian Huntera6b22402009-09-22 16:44:45 -07001861 }
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001862
1863 }
1864 return ret;
1865}
1866
1867/* Routine to resume the MMC device */
1868static int omap_mmc_resume(struct platform_device *pdev)
1869{
1870 int ret = 0;
1871 struct mmc_omap_host *host = platform_get_drvdata(pdev);
1872
1873 if (host && !host->suspended)
1874 return 0;
1875
1876 if (host) {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001877 ret = clk_enable(host->iclk);
Denis Karpov11dd62a2009-09-22 16:44:43 -07001878 if (ret)
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001879 goto clk_en_err;
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001880
1881 if (clk_enable(host->dbclk) != 0)
1882 dev_dbg(mmc_dev(host->mmc),
1883 "Enabling debounce clk failed\n");
1884
Denis Karpov11dd62a2009-09-22 16:44:43 -07001885 if (mmc_host_enable(host->mmc) != 0) {
1886 clk_disable(host->iclk);
1887 goto clk_en_err;
1888 }
1889
Kim Kyuwon1b331e62009-02-20 13:10:08 +01001890 omap_hsmmc_init(host);
1891
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001892 if (host->pdata->resume) {
1893 ret = host->pdata->resume(&pdev->dev, host->slot_id);
1894 if (ret)
1895 dev_dbg(mmc_dev(host->mmc),
1896 "Unmask interrupt failed\n");
1897 }
1898
1899 /* Notify the core to resume the host */
1900 ret = mmc_resume_host(host->mmc);
1901 if (ret == 0)
1902 host->suspended = 0;
Adrian Hunter5e2ea612009-09-22 16:44:39 -07001903 mmc_host_lazy_disable(host->mmc);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001904 }
1905
1906 return ret;
1907
1908clk_en_err:
1909 dev_dbg(mmc_dev(host->mmc),
1910 "Failed to enable MMC clocks during resume\n");
1911 return ret;
1912}
1913
1914#else
1915#define omap_mmc_suspend NULL
1916#define omap_mmc_resume NULL
1917#endif
1918
1919static struct platform_driver omap_mmc_driver = {
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001920 .remove = omap_mmc_remove,
1921 .suspend = omap_mmc_suspend,
1922 .resume = omap_mmc_resume,
1923 .driver = {
1924 .name = DRIVER_NAME,
1925 .owner = THIS_MODULE,
1926 },
1927};
1928
1929static int __init omap_mmc_init(void)
1930{
1931 /* Register the MMC driver */
Uwe Kleine-Königf400cd82009-09-22 16:44:26 -07001932 return platform_driver_probe(&omap_mmc_driver, omap_mmc_probe);
Madhusudhan Chikkaturea45c6cb2009-01-23 01:05:23 +01001933}
1934
1935static void __exit omap_mmc_cleanup(void)
1936{
1937 /* Unregister MMC driver */
1938 platform_driver_unregister(&omap_mmc_driver);
1939}
1940
1941module_init(omap_mmc_init);
1942module_exit(omap_mmc_cleanup);
1943
1944MODULE_DESCRIPTION("OMAP High Speed Multimedia Card driver");
1945MODULE_LICENSE("GPL");
1946MODULE_ALIAS("platform:" DRIVER_NAME);
1947MODULE_AUTHOR("Texas Instruments Inc");