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