blob: 8b05d3ceb29c8657484dd3082a223f8a56abd226 [file] [log] [blame]
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +01001/*
2 * linux/drivers/mmc/host/tmio_mmc_pio.c
3 *
4 * Copyright (C) 2011 Guennadi Liakhovetski
5 * Copyright (C) 2007 Ian Molton
6 * Copyright (C) 2004 Ian Molton
7 *
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License version 2 as
10 * published by the Free Software Foundation.
11 *
12 * Driver for the MMC / SD / SDIO IP found in:
13 *
14 * TC6393XB, TC6391XB, TC6387XB, T7L66XB, ASIC3, SH-Mobile SoCs
15 *
16 * This driver draws mainly on scattered spec sheets, Reverse engineering
17 * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
18 * support). (Further 4 bit support from a later datasheet).
19 *
20 * TODO:
21 * Investigate using a workqueue for PIO transfers
22 * Eliminate FIXMEs
23 * SDIO support
24 * Better Power management
25 * Handle MMC errors better
26 * double buffer support
27 *
28 */
29
30#include <linux/delay.h>
31#include <linux/device.h>
32#include <linux/highmem.h>
33#include <linux/interrupt.h>
34#include <linux/io.h>
35#include <linux/irq.h>
36#include <linux/mfd/tmio.h>
37#include <linux/mmc/host.h>
Simon Hormancba179a2011-03-24 09:48:36 +010038#include <linux/mmc/tmio.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010039#include <linux/module.h>
40#include <linux/pagemap.h>
41#include <linux/platform_device.h>
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +000042#include <linux/pm_runtime.h>
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010043#include <linux/scatterlist.h>
44#include <linux/workqueue.h>
45#include <linux/spinlock.h>
46
47#include "tmio_mmc.h"
48
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +010049static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
50{
51 return readw(host->ctl + (addr << host->bus_shift));
52}
53
54static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
55 u16 *buf, int count)
56{
57 readsw(host->ctl + (addr << host->bus_shift), buf, count);
58}
59
60static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
61{
62 return readw(host->ctl + (addr << host->bus_shift)) |
63 readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
64}
65
66static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
67{
68 writew(val, host->ctl + (addr << host->bus_shift));
69}
70
71static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
72 u16 *buf, int count)
73{
74 writesw(host->ctl + (addr << host->bus_shift), buf, count);
75}
76
77static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
78{
79 writew(val, host->ctl + (addr << host->bus_shift));
80 writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
81}
82
83void tmio_mmc_enable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
84{
85 u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) & ~(i & TMIO_MASK_IRQ);
86 sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
87}
88
89void tmio_mmc_disable_mmc_irqs(struct tmio_mmc_host *host, u32 i)
90{
91 u32 mask = sd_ctrl_read32(host, CTL_IRQ_MASK) | (i & TMIO_MASK_IRQ);
92 sd_ctrl_write32(host, CTL_IRQ_MASK, mask);
93}
94
95static void tmio_mmc_ack_mmc_irqs(struct tmio_mmc_host *host, u32 i)
96{
97 sd_ctrl_write32(host, CTL_STATUS, ~i);
98}
99
100static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
101{
102 host->sg_len = data->sg_len;
103 host->sg_ptr = data->sg;
104 host->sg_orig = data->sg;
105 host->sg_off = 0;
106}
107
108static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
109{
110 host->sg_ptr = sg_next(host->sg_ptr);
111 host->sg_off = 0;
112 return --host->sg_len;
113}
114
115#ifdef CONFIG_MMC_DEBUG
116
117#define STATUS_TO_TEXT(a, status, i) \
118 do { \
119 if (status & TMIO_STAT_##a) { \
120 if (i++) \
121 printk(" | "); \
122 printk(#a); \
123 } \
124 } while (0)
125
126static void pr_debug_status(u32 status)
127{
128 int i = 0;
129 printk(KERN_DEBUG "status: %08x = ", status);
130 STATUS_TO_TEXT(CARD_REMOVE, status, i);
131 STATUS_TO_TEXT(CARD_INSERT, status, i);
132 STATUS_TO_TEXT(SIGSTATE, status, i);
133 STATUS_TO_TEXT(WRPROTECT, status, i);
134 STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
135 STATUS_TO_TEXT(CARD_INSERT_A, status, i);
136 STATUS_TO_TEXT(SIGSTATE_A, status, i);
137 STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
138 STATUS_TO_TEXT(STOPBIT_ERR, status, i);
139 STATUS_TO_TEXT(ILL_FUNC, status, i);
140 STATUS_TO_TEXT(CMD_BUSY, status, i);
141 STATUS_TO_TEXT(CMDRESPEND, status, i);
142 STATUS_TO_TEXT(DATAEND, status, i);
143 STATUS_TO_TEXT(CRCFAIL, status, i);
144 STATUS_TO_TEXT(DATATIMEOUT, status, i);
145 STATUS_TO_TEXT(CMDTIMEOUT, status, i);
146 STATUS_TO_TEXT(RXOVERFLOW, status, i);
147 STATUS_TO_TEXT(TXUNDERRUN, status, i);
148 STATUS_TO_TEXT(RXRDY, status, i);
149 STATUS_TO_TEXT(TXRQ, status, i);
150 STATUS_TO_TEXT(ILL_ACCESS, status, i);
151 printk("\n");
152}
153
154#else
155#define pr_debug_status(s) do { } while (0)
156#endif
157
158static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
159{
160 struct tmio_mmc_host *host = mmc_priv(mmc);
161
162 if (enable) {
163 host->sdio_irq_enabled = 1;
164 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
165 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK,
166 (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ));
167 } else {
168 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL);
169 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
170 host->sdio_irq_enabled = 0;
171 }
172}
173
174static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
175{
176 u32 clk = 0, clock;
177
178 if (new_clock) {
179 for (clock = host->mmc->f_min, clk = 0x80000080;
180 new_clock >= (clock<<1); clk >>= 1)
181 clock <<= 1;
182 clk |= 0x100;
183 }
184
185 if (host->set_clk_div)
186 host->set_clk_div(host->pdev, (clk>>22) & 1);
187
188 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
189}
190
191static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
192{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100193 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100194
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100195 /* implicit BUG_ON(!res) */
196 if (resource_size(res) > 0x100) {
197 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
198 msleep(10);
199 }
Guennadi Liakhovetskid9b03422011-03-10 18:43:07 +0100200
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100201 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
202 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
203 msleep(10);
204}
205
206static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
207{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100208 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100209
210 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
211 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
212 msleep(10);
Guennadi Liakhovetskid9b03422011-03-10 18:43:07 +0100213
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100214 /* implicit BUG_ON(!res) */
215 if (resource_size(res) > 0x100) {
216 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
217 msleep(10);
218 }
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100219}
220
221static void tmio_mmc_reset(struct tmio_mmc_host *host)
222{
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100223 struct resource *res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
224
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100225 /* FIXME - should we set stop clock reg here */
226 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100227 /* implicit BUG_ON(!res) */
228 if (resource_size(res) > 0x100)
229 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100230 msleep(10);
231 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
Guennadi Liakhovetski69d1fe12011-03-09 17:28:55 +0100232 if (resource_size(res) > 0x100)
233 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100234 msleep(10);
235}
236
237static void tmio_mmc_reset_work(struct work_struct *work)
238{
239 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
240 delayed_reset_work.work);
241 struct mmc_request *mrq;
242 unsigned long flags;
243
244 spin_lock_irqsave(&host->lock, flags);
245 mrq = host->mrq;
246
247 /* request already finished */
248 if (!mrq
249 || time_is_after_jiffies(host->last_req_ts +
250 msecs_to_jiffies(2000))) {
251 spin_unlock_irqrestore(&host->lock, flags);
252 return;
253 }
254
255 dev_warn(&host->pdev->dev,
256 "timeout waiting for hardware interrupt (CMD%u)\n",
257 mrq->cmd->opcode);
258
259 if (host->data)
260 host->data->error = -ETIMEDOUT;
261 else if (host->cmd)
262 host->cmd->error = -ETIMEDOUT;
263 else
264 mrq->cmd->error = -ETIMEDOUT;
265
266 host->cmd = NULL;
267 host->data = NULL;
268 host->mrq = NULL;
269 host->force_pio = false;
270
271 spin_unlock_irqrestore(&host->lock, flags);
272
273 tmio_mmc_reset(host);
274
275 mmc_request_done(host->mmc, mrq);
276}
277
278static void tmio_mmc_finish_request(struct tmio_mmc_host *host)
279{
280 struct mmc_request *mrq = host->mrq;
281
282 if (!mrq)
283 return;
284
285 host->mrq = NULL;
286 host->cmd = NULL;
287 host->data = NULL;
288 host->force_pio = false;
289
290 cancel_delayed_work(&host->delayed_reset_work);
291
292 mmc_request_done(host->mmc, mrq);
293}
294
295/* These are the bitmasks the tmio chip requires to implement the MMC response
296 * types. Note that R1 and R6 are the same in this scheme. */
297#define APP_CMD 0x0040
298#define RESP_NONE 0x0300
299#define RESP_R1 0x0400
300#define RESP_R1B 0x0500
301#define RESP_R2 0x0600
302#define RESP_R3 0x0700
303#define DATA_PRESENT 0x0800
304#define TRANSFER_READ 0x1000
305#define TRANSFER_MULTI 0x2000
306#define SECURITY_CMD 0x4000
307
308static int tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
309{
310 struct mmc_data *data = host->data;
311 int c = cmd->opcode;
312
313 /* Command 12 is handled by hardware */
314 if (cmd->opcode == 12 && !cmd->arg) {
315 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
316 return 0;
317 }
318
319 switch (mmc_resp_type(cmd)) {
320 case MMC_RSP_NONE: c |= RESP_NONE; break;
321 case MMC_RSP_R1: c |= RESP_R1; break;
322 case MMC_RSP_R1B: c |= RESP_R1B; break;
323 case MMC_RSP_R2: c |= RESP_R2; break;
324 case MMC_RSP_R3: c |= RESP_R3; break;
325 default:
326 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
327 return -EINVAL;
328 }
329
330 host->cmd = cmd;
331
332/* FIXME - this seems to be ok commented out but the spec suggest this bit
333 * should be set when issuing app commands.
334 * if(cmd->flags & MMC_FLAG_ACMD)
335 * c |= APP_CMD;
336 */
337 if (data) {
338 c |= DATA_PRESENT;
339 if (data->blocks > 1) {
340 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
341 c |= TRANSFER_MULTI;
342 }
343 if (data->flags & MMC_DATA_READ)
344 c |= TRANSFER_READ;
345 }
346
347 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_CMD);
348
349 /* Fire off the command */
350 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
351 sd_ctrl_write16(host, CTL_SD_CMD, c);
352
353 return 0;
354}
355
356/*
357 * This chip always returns (at least?) as much data as you ask for.
358 * I'm unsure what happens if you ask for less than a block. This should be
Lucas De Marchi25985ed2011-03-30 22:57:33 -0300359 * looked into to ensure that a funny length read doesn't hose the controller.
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100360 */
361static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
362{
363 struct mmc_data *data = host->data;
364 void *sg_virt;
365 unsigned short *buf;
366 unsigned int count;
367 unsigned long flags;
368
369 if ((host->chan_tx || host->chan_rx) && !host->force_pio) {
370 pr_err("PIO IRQ in DMA mode!\n");
371 return;
372 } else if (!data) {
373 pr_debug("Spurious PIO IRQ\n");
374 return;
375 }
376
377 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
378 buf = (unsigned short *)(sg_virt + host->sg_off);
379
380 count = host->sg_ptr->length - host->sg_off;
381 if (count > data->blksz)
382 count = data->blksz;
383
384 pr_debug("count: %08x offset: %08x flags %08x\n",
385 count, host->sg_off, data->flags);
386
387 /* Transfer the data */
388 if (data->flags & MMC_DATA_READ)
389 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
390 else
391 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
392
393 host->sg_off += count;
394
395 tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
396
397 if (host->sg_off == host->sg_ptr->length)
398 tmio_mmc_next_sg(host);
399
400 return;
401}
402
403static void tmio_mmc_check_bounce_buffer(struct tmio_mmc_host *host)
404{
405 if (host->sg_ptr == &host->bounce_sg) {
406 unsigned long flags;
407 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
408 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
409 tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
410 }
411}
412
413/* needs to be called with host->lock held */
414void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
415{
416 struct mmc_data *data = host->data;
417 struct mmc_command *stop;
418
419 host->data = NULL;
420
421 if (!data) {
422 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
423 return;
424 }
425 stop = data->stop;
426
427 /* FIXME - return correct transfer count on errors */
428 if (!data->error)
429 data->bytes_xfered = data->blocks * data->blksz;
430 else
431 data->bytes_xfered = 0;
432
433 pr_debug("Completed data request\n");
434
435 /*
436 * FIXME: other drivers allow an optional stop command of any given type
437 * which we dont do, as the chip can auto generate them.
438 * Perhaps we can be smarter about when to use auto CMD12 and
439 * only issue the auto request when we know this is the desired
440 * stop command, allowing fallback to the stop command the
441 * upper layers expect. For now, we do what works.
442 */
443
444 if (data->flags & MMC_DATA_READ) {
445 if (host->chan_rx && !host->force_pio)
446 tmio_mmc_check_bounce_buffer(host);
447 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
448 host->mrq);
449 } else {
450 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
451 host->mrq);
452 }
453
454 if (stop) {
455 if (stop->opcode == 12 && !stop->arg)
456 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
457 else
458 BUG();
459 }
460
461 tmio_mmc_finish_request(host);
462}
463
464static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
465{
466 struct mmc_data *data;
467 spin_lock(&host->lock);
468 data = host->data;
469
470 if (!data)
471 goto out;
472
473 if (host->chan_tx && (data->flags & MMC_DATA_WRITE) && !host->force_pio) {
474 /*
475 * Has all data been written out yet? Testing on SuperH showed,
476 * that in most cases the first interrupt comes already with the
477 * BUSY status bit clear, but on some operations, like mount or
478 * in the beginning of a write / sync / umount, there is one
479 * DATAEND interrupt with the BUSY bit set, in this cases
480 * waiting for one more interrupt fixes the problem.
481 */
482 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
483 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
484 tasklet_schedule(&host->dma_complete);
485 }
486 } else if (host->chan_rx && (data->flags & MMC_DATA_READ) && !host->force_pio) {
487 tmio_mmc_disable_mmc_irqs(host, TMIO_STAT_DATAEND);
488 tasklet_schedule(&host->dma_complete);
489 } else {
490 tmio_mmc_do_data_irq(host);
491 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_READOP | TMIO_MASK_WRITEOP);
492 }
493out:
494 spin_unlock(&host->lock);
495}
496
497static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
498 unsigned int stat)
499{
500 struct mmc_command *cmd = host->cmd;
501 int i, addr;
502
503 spin_lock(&host->lock);
504
505 if (!host->cmd) {
506 pr_debug("Spurious CMD irq\n");
507 goto out;
508 }
509
510 host->cmd = NULL;
511
512 /* This controller is sicker than the PXA one. Not only do we need to
513 * drop the top 8 bits of the first response word, we also need to
514 * modify the order of the response for short response command types.
515 */
516
517 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
518 cmd->resp[i] = sd_ctrl_read32(host, addr);
519
520 if (cmd->flags & MMC_RSP_136) {
521 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
522 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
523 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
524 cmd->resp[3] <<= 8;
525 } else if (cmd->flags & MMC_RSP_R3) {
526 cmd->resp[0] = cmd->resp[3];
527 }
528
529 if (stat & TMIO_STAT_CMDTIMEOUT)
530 cmd->error = -ETIMEDOUT;
531 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
532 cmd->error = -EILSEQ;
533
534 /* If there is data to handle we enable data IRQs here, and
535 * we will ultimatley finish the request in the data_end handler.
536 * If theres no data or we encountered an error, finish now.
537 */
538 if (host->data && !cmd->error) {
539 if (host->data->flags & MMC_DATA_READ) {
540 if (host->force_pio || !host->chan_rx)
541 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_READOP);
542 else
543 tasklet_schedule(&host->dma_issue);
544 } else {
545 if (host->force_pio || !host->chan_tx)
546 tmio_mmc_enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
547 else
548 tasklet_schedule(&host->dma_issue);
549 }
550 } else {
551 tmio_mmc_finish_request(host);
552 }
553
554out:
555 spin_unlock(&host->lock);
556}
557
558static irqreturn_t tmio_mmc_irq(int irq, void *devid)
559{
560 struct tmio_mmc_host *host = devid;
561 struct tmio_mmc_data *pdata = host->pdata;
562 unsigned int ireg, irq_mask, status;
563 unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
564
565 pr_debug("MMC IRQ begin\n");
566
567 status = sd_ctrl_read32(host, CTL_STATUS);
568 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
569 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
570
571 sdio_ireg = 0;
572 if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
573 sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
574 sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
575 sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
576
577 sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
578
579 if (sdio_ireg && !host->sdio_irq_enabled) {
580 pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
581 sdio_status, sdio_irq_mask, sdio_ireg);
582 tmio_mmc_enable_sdio_irq(host->mmc, 0);
583 goto out;
584 }
585
586 if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
587 sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
588 mmc_signal_sdio_irq(host->mmc);
589
590 if (sdio_ireg)
591 goto out;
592 }
593
594 pr_debug_status(status);
595 pr_debug_status(ireg);
596
597 if (!ireg) {
598 tmio_mmc_disable_mmc_irqs(host, status & ~irq_mask);
599
600 pr_warning("tmio_mmc: Spurious irq, disabling! "
601 "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
602 pr_debug_status(status);
603
604 goto out;
605 }
606
607 while (ireg) {
608 /* Card insert / remove attempts */
609 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
610 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
611 TMIO_STAT_CARD_REMOVE);
612 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
613 }
614
615 /* CRC and other errors */
616/* if (ireg & TMIO_STAT_ERR_IRQ)
617 * handled |= tmio_error_irq(host, irq, stat);
618 */
619
620 /* Command completion */
621 if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
622 tmio_mmc_ack_mmc_irqs(host,
623 TMIO_STAT_CMDRESPEND |
624 TMIO_STAT_CMDTIMEOUT);
625 tmio_mmc_cmd_irq(host, status);
626 }
627
628 /* Data transfer */
629 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
630 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
631 tmio_mmc_pio_irq(host);
632 }
633
634 /* Data transfer completion */
635 if (ireg & TMIO_STAT_DATAEND) {
636 tmio_mmc_ack_mmc_irqs(host, TMIO_STAT_DATAEND);
637 tmio_mmc_data_irq(host);
638 }
639
640 /* Check status - keep going until we've handled it all */
641 status = sd_ctrl_read32(host, CTL_STATUS);
642 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
643 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
644
645 pr_debug("Status at end of loop: %08x\n", status);
646 pr_debug_status(status);
647 }
648 pr_debug("MMC IRQ end\n");
649
650out:
651 return IRQ_HANDLED;
652}
653
654static int tmio_mmc_start_data(struct tmio_mmc_host *host,
655 struct mmc_data *data)
656{
657 struct tmio_mmc_data *pdata = host->pdata;
658
659 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
660 data->blksz, data->blocks);
661
662 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
663 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
664 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
665
666 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
667 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
668 mmc_hostname(host->mmc), data->blksz);
669 return -EINVAL;
670 }
671 }
672
673 tmio_mmc_init_sg(host, data);
674 host->data = data;
675
676 /* Set transfer length / blocksize */
677 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
678 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
679
680 tmio_mmc_start_dma(host, data);
681
682 return 0;
683}
684
685/* Process requests from the MMC layer */
686static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
687{
688 struct tmio_mmc_host *host = mmc_priv(mmc);
689 int ret;
690
691 if (host->mrq)
692 pr_debug("request not null\n");
693
694 host->last_req_ts = jiffies;
695 wmb();
696 host->mrq = mrq;
697
698 if (mrq->data) {
699 ret = tmio_mmc_start_data(host, mrq->data);
700 if (ret)
701 goto fail;
702 }
703
704 ret = tmio_mmc_start_command(host, mrq->cmd);
705 if (!ret) {
706 schedule_delayed_work(&host->delayed_reset_work,
707 msecs_to_jiffies(2000));
708 return;
709 }
710
711fail:
712 host->mrq = NULL;
713 host->force_pio = false;
714 mrq->cmd->error = ret;
715 mmc_request_done(mmc, mrq);
716}
717
718/* Set MMC clock / power.
719 * Note: This controller uses a simple divider scheme therefore it cannot
720 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
721 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
722 * slowest setting.
723 */
724static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
725{
726 struct tmio_mmc_host *host = mmc_priv(mmc);
727
728 if (ios->clock)
729 tmio_mmc_set_clock(host, ios->clock);
730
Guennadi Liakhovetskia7edbe32011-03-09 14:38:58 +0100731 /* Power sequence - OFF -> UP -> ON */
Guennadi Liakhovetskic919c2a2011-04-21 09:09:59 +0200732 if (ios->power_mode == MMC_POWER_UP) {
733 /* power up SD bus */
734 if (host->set_pwr)
735 host->set_pwr(host->pdev, 1);
736 } else if (ios->power_mode == MMC_POWER_OFF || !ios->clock) {
Guennadi Liakhovetski5fd01572011-03-09 14:45:44 +0100737 /* power down SD bus */
738 if (ios->power_mode == MMC_POWER_OFF && host->set_pwr)
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100739 host->set_pwr(host->pdev, 0);
740 tmio_mmc_clk_stop(host);
Guennadi Liakhovetski5fd01572011-03-09 14:45:44 +0100741 } else {
742 /* start bus clock */
743 tmio_mmc_clk_start(host);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100744 }
745
746 switch (ios->bus_width) {
747 case MMC_BUS_WIDTH_1:
748 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
749 break;
750 case MMC_BUS_WIDTH_4:
751 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
752 break;
753 }
754
755 /* Let things settle. delay taken from winCE driver */
756 udelay(140);
757}
758
759static int tmio_mmc_get_ro(struct mmc_host *mmc)
760{
761 struct tmio_mmc_host *host = mmc_priv(mmc);
762 struct tmio_mmc_data *pdata = host->pdata;
763
764 return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
765 !(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT));
766}
767
768static int tmio_mmc_get_cd(struct mmc_host *mmc)
769{
770 struct tmio_mmc_host *host = mmc_priv(mmc);
771 struct tmio_mmc_data *pdata = host->pdata;
772
773 if (!pdata->get_cd)
774 return -ENOSYS;
775 else
776 return pdata->get_cd(host->pdev);
777}
778
779static const struct mmc_host_ops tmio_mmc_ops = {
780 .request = tmio_mmc_request,
781 .set_ios = tmio_mmc_set_ios,
782 .get_ro = tmio_mmc_get_ro,
783 .get_cd = tmio_mmc_get_cd,
784 .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
785};
786
787int __devinit tmio_mmc_host_probe(struct tmio_mmc_host **host,
788 struct platform_device *pdev,
789 struct tmio_mmc_data *pdata)
790{
791 struct tmio_mmc_host *_host;
792 struct mmc_host *mmc;
793 struct resource *res_ctl;
794 int ret;
795 u32 irq_mask = TMIO_MASK_CMD;
796
797 res_ctl = platform_get_resource(pdev, IORESOURCE_MEM, 0);
798 if (!res_ctl)
799 return -EINVAL;
800
801 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &pdev->dev);
802 if (!mmc)
803 return -ENOMEM;
804
805 _host = mmc_priv(mmc);
806 _host->pdata = pdata;
807 _host->mmc = mmc;
808 _host->pdev = pdev;
809 platform_set_drvdata(pdev, mmc);
810
811 _host->set_pwr = pdata->set_pwr;
812 _host->set_clk_div = pdata->set_clk_div;
813
814 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
815 _host->bus_shift = resource_size(res_ctl) >> 10;
816
817 _host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
818 if (!_host->ctl) {
819 ret = -ENOMEM;
820 goto host_free;
821 }
822
823 mmc->ops = &tmio_mmc_ops;
824 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
825 mmc->f_max = pdata->hclk;
826 mmc->f_min = mmc->f_max / 512;
827 mmc->max_segs = 32;
828 mmc->max_blk_size = 512;
829 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
830 mmc->max_segs;
831 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
832 mmc->max_seg_size = mmc->max_req_size;
833 if (pdata->ocr_mask)
834 mmc->ocr_avail = pdata->ocr_mask;
835 else
836 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
837
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000838 pm_runtime_enable(&pdev->dev);
839 ret = pm_runtime_resume(&pdev->dev);
840 if (ret < 0)
841 goto pm_disable;
842
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100843 tmio_mmc_clk_stop(_host);
844 tmio_mmc_reset(_host);
845
846 ret = platform_get_irq(pdev, 0);
847 if (ret < 0)
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000848 goto pm_suspend;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100849
850 _host->irq = ret;
851
852 tmio_mmc_disable_mmc_irqs(_host, TMIO_MASK_ALL);
853 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
854 tmio_mmc_enable_sdio_irq(mmc, 0);
855
856 ret = request_irq(_host->irq, tmio_mmc_irq, IRQF_DISABLED |
857 IRQF_TRIGGER_FALLING, dev_name(&pdev->dev), _host);
858 if (ret)
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000859 goto pm_suspend;
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100860
861 spin_lock_init(&_host->lock);
862
863 /* Init delayed work for request timeouts */
864 INIT_DELAYED_WORK(&_host->delayed_reset_work, tmio_mmc_reset_work);
865
866 /* See if we also get DMA */
867 tmio_mmc_request_dma(_host, pdata);
868
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000869 /* We have to keep the device powered for its card detection to work */
870 pm_runtime_get_noresume(&pdev->dev);
871
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100872 mmc_add_host(mmc);
873
874 /* Unmask the IRQs we want to know about */
875 if (!_host->chan_rx)
876 irq_mask |= TMIO_MASK_READOP;
877 if (!_host->chan_tx)
878 irq_mask |= TMIO_MASK_WRITEOP;
879
880 tmio_mmc_enable_mmc_irqs(_host, irq_mask);
881
882 *host = _host;
883
884 return 0;
885
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000886pm_suspend:
887 pm_runtime_suspend(&pdev->dev);
888pm_disable:
889 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100890 iounmap(_host->ctl);
891host_free:
892 mmc_free_host(mmc);
893
894 return ret;
895}
896EXPORT_SYMBOL(tmio_mmc_host_probe);
897
898void tmio_mmc_host_remove(struct tmio_mmc_host *host)
899{
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000900 struct platform_device *pdev = host->pdev;
901
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100902 mmc_remove_host(host->mmc);
903 cancel_delayed_work_sync(&host->delayed_reset_work);
904 tmio_mmc_release_dma(host);
905 free_irq(host->irq, host);
906 iounmap(host->ctl);
907 mmc_free_host(host->mmc);
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000908
909 /* Compensate for pm_runtime_get_sync() in probe() above */
910 pm_runtime_put_sync(&pdev->dev);
911 pm_runtime_disable(&pdev->dev);
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100912}
913EXPORT_SYMBOL(tmio_mmc_host_remove);
914
Guennadi Liakhovetskie6ee7182011-05-05 16:13:12 +0000915#ifdef CONFIG_PM
916int tmio_mmc_host_suspend(struct device *dev)
917{
918 struct mmc_host *mmc = dev_get_drvdata(dev);
919 struct tmio_mmc_host *host = mmc_priv(mmc);
920 int ret = mmc_suspend_host(mmc);
921
922 if (!ret)
923 tmio_mmc_disable_mmc_irqs(host, TMIO_MASK_ALL);
924
925 host->pm_error = pm_runtime_put_sync(dev);
926
927 return ret;
928}
929EXPORT_SYMBOL(tmio_mmc_host_suspend);
930
931int tmio_mmc_host_resume(struct device *dev)
932{
933 struct mmc_host *mmc = dev_get_drvdata(dev);
934 struct tmio_mmc_host *host = mmc_priv(mmc);
935
936 if (!host->pm_error)
937 pm_runtime_get_sync(dev);
938
939 tmio_mmc_reset(mmc_priv(mmc));
940 tmio_mmc_request_dma(host, host->pdata);
941
942 return mmc_resume_host(mmc);
943}
944EXPORT_SYMBOL(tmio_mmc_host_resume);
945
946#endif /* CONFIG_PM */
947
Guennadi Liakhovetskib6147492011-03-23 12:42:44 +0100948MODULE_LICENSE("GPL v2");