blob: e04c032abb1c42d377d59854f5581829b2e8308c [file] [log] [blame]
Ian Molton4a489982008-07-15 16:02:21 +01001/*
2 * linux/drivers/mmc/tmio_mmc.c
3 *
4 * Copyright (C) 2004 Ian Molton
5 * Copyright (C) 2007 Ian Molton
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Driver for the MMC / SD / SDIO cell found in:
12 *
Philipp Zabele6f2c7a2009-06-04 20:12:37 +020013 * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
Ian Molton4a489982008-07-15 16:02:21 +010014 *
15 * This driver draws mainly on scattered spec sheets, Reverse engineering
16 * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
17 * support). (Further 4 bit support from a later datasheet).
18 *
19 * TODO:
20 * Investigate using a workqueue for PIO transfers
21 * Eliminate FIXMEs
22 * SDIO support
23 * Better Power management
24 * Handle MMC errors better
25 * double buffer support
26 *
27 */
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010028
Ian Molton4a489982008-07-15 16:02:21 +010029#include <linux/delay.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010030#include <linux/device.h>
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +000031#include <linux/dmaengine.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010032#include <linux/highmem.h>
33#include <linux/interrupt.h>
34#include <linux/io.h>
35#include <linux/irq.h>
Ian Molton4a489982008-07-15 16:02:21 +010036#include <linux/mfd/core.h>
37#include <linux/mfd/tmio.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010038#include <linux/mmc/host.h>
39#include <linux/module.h>
40#include <linux/pagemap.h>
41#include <linux/scatterlist.h>
Ian Molton4a489982008-07-15 16:02:21 +010042
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010043#define CTL_SD_CMD 0x00
44#define CTL_ARG_REG 0x04
45#define CTL_STOP_INTERNAL_ACTION 0x08
46#define CTL_XFER_BLK_COUNT 0xa
47#define CTL_RESPONSE 0x0c
48#define CTL_STATUS 0x1c
49#define CTL_IRQ_MASK 0x20
50#define CTL_SD_CARD_CLK_CTL 0x24
51#define CTL_SD_XFER_LEN 0x26
52#define CTL_SD_MEM_CARD_OPT 0x28
53#define CTL_SD_ERROR_DETAIL_STATUS 0x2c
54#define CTL_SD_DATA_PORT 0x30
55#define CTL_TRANSACTION_CTL 0x34
56#define CTL_RESET_SD 0xe0
57#define CTL_SDIO_REGS 0x100
58#define CTL_CLK_AND_WAIT_CTL 0x138
59#define CTL_RESET_SDIO 0x1e0
60
61/* Definitions for values the CTRL_STATUS register can take. */
62#define TMIO_STAT_CMDRESPEND 0x00000001
63#define TMIO_STAT_DATAEND 0x00000004
64#define TMIO_STAT_CARD_REMOVE 0x00000008
65#define TMIO_STAT_CARD_INSERT 0x00000010
66#define TMIO_STAT_SIGSTATE 0x00000020
67#define TMIO_STAT_WRPROTECT 0x00000080
68#define TMIO_STAT_CARD_REMOVE_A 0x00000100
69#define TMIO_STAT_CARD_INSERT_A 0x00000200
70#define TMIO_STAT_SIGSTATE_A 0x00000400
71#define TMIO_STAT_CMD_IDX_ERR 0x00010000
72#define TMIO_STAT_CRCFAIL 0x00020000
73#define TMIO_STAT_STOPBIT_ERR 0x00040000
74#define TMIO_STAT_DATATIMEOUT 0x00080000
75#define TMIO_STAT_RXOVERFLOW 0x00100000
76#define TMIO_STAT_TXUNDERRUN 0x00200000
77#define TMIO_STAT_CMDTIMEOUT 0x00400000
78#define TMIO_STAT_RXRDY 0x01000000
79#define TMIO_STAT_TXRQ 0x02000000
80#define TMIO_STAT_ILL_FUNC 0x20000000
81#define TMIO_STAT_CMD_BUSY 0x40000000
82#define TMIO_STAT_ILL_ACCESS 0x80000000
83
84/* Define some IRQ masks */
85/* This is the mask used at reset by the chip */
86#define TMIO_MASK_ALL 0x837f031d
87#define TMIO_MASK_READOP (TMIO_STAT_RXRDY | TMIO_STAT_DATAEND)
88#define TMIO_MASK_WRITEOP (TMIO_STAT_TXRQ | TMIO_STAT_DATAEND)
89#define TMIO_MASK_CMD (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT | \
90 TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT)
91#define TMIO_MASK_IRQ (TMIO_MASK_READOP | TMIO_MASK_WRITEOP | TMIO_MASK_CMD)
92
93#define enable_mmc_irqs(host, i) \
94 do { \
95 u32 mask;\
96 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
97 mask &= ~((i) & TMIO_MASK_IRQ); \
98 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
99 } while (0)
100
101#define disable_mmc_irqs(host, i) \
102 do { \
103 u32 mask;\
104 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
105 mask |= ((i) & TMIO_MASK_IRQ); \
106 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
107 } while (0)
108
109#define ack_mmc_irqs(host, i) \
110 do { \
111 sd_ctrl_write32((host), CTL_STATUS, ~(i)); \
112 } while (0)
113
114
115struct tmio_mmc_host {
116 void __iomem *ctl;
117 unsigned long bus_shift;
118 struct mmc_command *cmd;
119 struct mmc_request *mrq;
120 struct mmc_data *data;
121 struct mmc_host *mmc;
122 int irq;
123
124 /* Callbacks for clock / power control */
125 void (*set_pwr)(struct platform_device *host, int state);
126 void (*set_clk_div)(struct platform_device *host, int state);
127
128 /* pio related stuff */
129 struct scatterlist *sg_ptr;
130 unsigned int sg_len;
131 unsigned int sg_off;
132
133 struct platform_device *pdev;
134
135 /* DMA support */
136 struct dma_chan *chan_rx;
137 struct dma_chan *chan_tx;
138 struct tasklet_struct dma_complete;
139 struct tasklet_struct dma_issue;
140#ifdef CONFIG_TMIO_MMC_DMA
141 unsigned int dma_sglen;
142#endif
143};
144
145static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
146{
147 return readw(host->ctl + (addr << host->bus_shift));
148}
149
150static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
151 u16 *buf, int count)
152{
153 readsw(host->ctl + (addr << host->bus_shift), buf, count);
154}
155
156static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
157{
158 return readw(host->ctl + (addr << host->bus_shift)) |
159 readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
160}
161
162static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
163{
164 writew(val, host->ctl + (addr << host->bus_shift));
165}
166
167static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
168 u16 *buf, int count)
169{
170 writesw(host->ctl + (addr << host->bus_shift), buf, count);
171}
172
173static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
174{
175 writew(val, host->ctl + (addr << host->bus_shift));
176 writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
177}
178
179static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
180{
181 host->sg_len = data->sg_len;
182 host->sg_ptr = data->sg;
183 host->sg_off = 0;
184}
185
186static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
187{
188 host->sg_ptr = sg_next(host->sg_ptr);
189 host->sg_off = 0;
190 return --host->sg_len;
191}
192
193static char *tmio_mmc_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
194{
195 local_irq_save(*flags);
196 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
197}
198
199static void tmio_mmc_kunmap_atomic(void *virt, unsigned long *flags)
200{
201 kunmap_atomic(virt, KM_BIO_SRC_IRQ);
202 local_irq_restore(*flags);
203}
204
205#ifdef CONFIG_MMC_DEBUG
206
207#define STATUS_TO_TEXT(a) \
208 do { \
209 if (status & TMIO_STAT_##a) \
210 printk(#a); \
211 } while (0)
212
213void pr_debug_status(u32 status)
214{
215 printk(KERN_DEBUG "status: %08x = ", status);
216 STATUS_TO_TEXT(CARD_REMOVE);
217 STATUS_TO_TEXT(CARD_INSERT);
218 STATUS_TO_TEXT(SIGSTATE);
219 STATUS_TO_TEXT(WRPROTECT);
220 STATUS_TO_TEXT(CARD_REMOVE_A);
221 STATUS_TO_TEXT(CARD_INSERT_A);
222 STATUS_TO_TEXT(SIGSTATE_A);
223 STATUS_TO_TEXT(CMD_IDX_ERR);
224 STATUS_TO_TEXT(STOPBIT_ERR);
225 STATUS_TO_TEXT(ILL_FUNC);
226 STATUS_TO_TEXT(CMD_BUSY);
227 STATUS_TO_TEXT(CMDRESPEND);
228 STATUS_TO_TEXT(DATAEND);
229 STATUS_TO_TEXT(CRCFAIL);
230 STATUS_TO_TEXT(DATATIMEOUT);
231 STATUS_TO_TEXT(CMDTIMEOUT);
232 STATUS_TO_TEXT(RXOVERFLOW);
233 STATUS_TO_TEXT(TXUNDERRUN);
234 STATUS_TO_TEXT(RXRDY);
235 STATUS_TO_TEXT(TXRQ);
236 STATUS_TO_TEXT(ILL_ACCESS);
237 printk("\n");
238}
239
240#else
241#define pr_debug_status(s) do { } while (0)
242#endif
Ian Molton4a489982008-07-15 16:02:21 +0100243
Ian Molton4a489982008-07-15 16:02:21 +0100244static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
245{
Ian Moltonda46a0b2009-06-12 21:53:05 +0100246 u32 clk = 0, clock;
Ian Molton4a489982008-07-15 16:02:21 +0100247
248 if (new_clock) {
Ian Moltonda46a0b2009-06-12 21:53:05 +0100249 for (clock = host->mmc->f_min, clk = 0x80000080;
250 new_clock >= (clock<<1); clk >>= 1)
Ian Molton4a489982008-07-15 16:02:21 +0100251 clock <<= 1;
Ian Molton4a489982008-07-15 16:02:21 +0100252 clk |= 0x100;
253 }
254
Ian Molton64e88672010-01-06 13:51:48 +0100255 if (host->set_clk_div)
256 host->set_clk_div(host->pdev, (clk>>22) & 1);
257
Ian Moltonda46a0b2009-06-12 21:53:05 +0100258 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
Ian Molton4a489982008-07-15 16:02:21 +0100259}
260
261static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
262{
Philipp Zabel5e746722009-06-04 20:12:32 +0200263 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100264 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200265 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
266 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100267 msleep(10);
268}
269
270static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
271{
Philipp Zabel5e746722009-06-04 20:12:32 +0200272 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
273 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100274 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200275 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
Ian Molton4a489982008-07-15 16:02:21 +0100276 msleep(10);
277}
278
279static void reset(struct tmio_mmc_host *host)
280{
Ian Molton4a489982008-07-15 16:02:21 +0100281 /* FIXME - should we set stop clock reg here */
Philipp Zabel5e746722009-06-04 20:12:32 +0200282 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
283 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100284 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200285 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
286 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Ian Molton4a489982008-07-15 16:02:21 +0100287 msleep(10);
288}
289
290static void
291tmio_mmc_finish_request(struct tmio_mmc_host *host)
292{
293 struct mmc_request *mrq = host->mrq;
294
295 host->mrq = NULL;
296 host->cmd = NULL;
297 host->data = NULL;
298
299 mmc_request_done(host->mmc, mrq);
300}
301
302/* These are the bitmasks the tmio chip requires to implement the MMC response
303 * types. Note that R1 and R6 are the same in this scheme. */
304#define APP_CMD 0x0040
305#define RESP_NONE 0x0300
306#define RESP_R1 0x0400
307#define RESP_R1B 0x0500
308#define RESP_R2 0x0600
309#define RESP_R3 0x0700
310#define DATA_PRESENT 0x0800
311#define TRANSFER_READ 0x1000
312#define TRANSFER_MULTI 0x2000
313#define SECURITY_CMD 0x4000
314
315static int
316tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
317{
Ian Molton4a489982008-07-15 16:02:21 +0100318 struct mmc_data *data = host->data;
319 int c = cmd->opcode;
320
321 /* Command 12 is handled by hardware */
322 if (cmd->opcode == 12 && !cmd->arg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200323 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
Ian Molton4a489982008-07-15 16:02:21 +0100324 return 0;
325 }
326
327 switch (mmc_resp_type(cmd)) {
328 case MMC_RSP_NONE: c |= RESP_NONE; break;
329 case MMC_RSP_R1: c |= RESP_R1; break;
330 case MMC_RSP_R1B: c |= RESP_R1B; break;
331 case MMC_RSP_R2: c |= RESP_R2; break;
332 case MMC_RSP_R3: c |= RESP_R3; break;
333 default:
334 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
335 return -EINVAL;
336 }
337
338 host->cmd = cmd;
339
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000340/* FIXME - this seems to be ok commented out but the spec suggest this bit
341 * should be set when issuing app commands.
Ian Molton4a489982008-07-15 16:02:21 +0100342 * if(cmd->flags & MMC_FLAG_ACMD)
343 * c |= APP_CMD;
344 */
345 if (data) {
346 c |= DATA_PRESENT;
347 if (data->blocks > 1) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200348 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
Ian Molton4a489982008-07-15 16:02:21 +0100349 c |= TRANSFER_MULTI;
350 }
351 if (data->flags & MMC_DATA_READ)
352 c |= TRANSFER_READ;
353 }
354
Philipp Zabel5e746722009-06-04 20:12:32 +0200355 enable_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100356
357 /* Fire off the command */
Philipp Zabel5e746722009-06-04 20:12:32 +0200358 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
359 sd_ctrl_write16(host, CTL_SD_CMD, c);
Ian Molton4a489982008-07-15 16:02:21 +0100360
361 return 0;
362}
363
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000364/*
365 * This chip always returns (at least?) as much data as you ask for.
Ian Molton4a489982008-07-15 16:02:21 +0100366 * I'm unsure what happens if you ask for less than a block. This should be
367 * looked into to ensure that a funny length read doesnt hose the controller.
Ian Molton4a489982008-07-15 16:02:21 +0100368 */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000369static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100370{
Ian Molton4a489982008-07-15 16:02:21 +0100371 struct mmc_data *data = host->data;
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700372 void *sg_virt;
Ian Molton4a489982008-07-15 16:02:21 +0100373 unsigned short *buf;
374 unsigned int count;
375 unsigned long flags;
376
377 if (!data) {
378 pr_debug("Spurious PIO IRQ\n");
379 return;
380 }
381
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700382 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
383 buf = (unsigned short *)(sg_virt + host->sg_off);
Ian Molton4a489982008-07-15 16:02:21 +0100384
385 count = host->sg_ptr->length - host->sg_off;
386 if (count > data->blksz)
387 count = data->blksz;
388
389 pr_debug("count: %08x offset: %08x flags %08x\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000390 count, host->sg_off, data->flags);
Ian Molton4a489982008-07-15 16:02:21 +0100391
392 /* Transfer the data */
393 if (data->flags & MMC_DATA_READ)
Philipp Zabel5e746722009-06-04 20:12:32 +0200394 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100395 else
Philipp Zabel5e746722009-06-04 20:12:32 +0200396 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100397
398 host->sg_off += count;
399
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700400 tmio_mmc_kunmap_atomic(sg_virt, &flags);
Ian Molton4a489982008-07-15 16:02:21 +0100401
402 if (host->sg_off == host->sg_ptr->length)
403 tmio_mmc_next_sg(host);
404
405 return;
406}
407
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000408static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100409{
Ian Molton4a489982008-07-15 16:02:21 +0100410 struct mmc_data *data = host->data;
Julia Lawalla0d045c2008-12-16 16:13:09 +0100411 struct mmc_command *stop;
Ian Molton4a489982008-07-15 16:02:21 +0100412
413 host->data = NULL;
414
415 if (!data) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000416 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
Ian Molton4a489982008-07-15 16:02:21 +0100417 return;
418 }
Julia Lawalla0d045c2008-12-16 16:13:09 +0100419 stop = data->stop;
Ian Molton4a489982008-07-15 16:02:21 +0100420
421 /* FIXME - return correct transfer count on errors */
422 if (!data->error)
423 data->bytes_xfered = data->blocks * data->blksz;
424 else
425 data->bytes_xfered = 0;
426
427 pr_debug("Completed data request\n");
428
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000429 /*
430 * FIXME: other drivers allow an optional stop command of any given type
Ian Molton4a489982008-07-15 16:02:21 +0100431 * which we dont do, as the chip can auto generate them.
432 * Perhaps we can be smarter about when to use auto CMD12 and
433 * only issue the auto request when we know this is the desired
434 * stop command, allowing fallback to the stop command the
435 * upper layers expect. For now, we do what works.
436 */
437
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000438 if (data->flags & MMC_DATA_READ) {
439 if (!host->chan_rx)
440 disable_mmc_irqs(host, TMIO_MASK_READOP);
441 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
442 host->mrq);
443 } else {
444 if (!host->chan_tx)
445 disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
446 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
447 host->mrq);
448 }
Ian Molton4a489982008-07-15 16:02:21 +0100449
450 if (stop) {
451 if (stop->opcode == 12 && !stop->arg)
Philipp Zabel5e746722009-06-04 20:12:32 +0200452 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
Ian Molton4a489982008-07-15 16:02:21 +0100453 else
454 BUG();
455 }
456
457 tmio_mmc_finish_request(host);
458}
459
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000460static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
461{
462 struct mmc_data *data = host->data;
463
464 if (!data)
465 return;
466
467 if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) {
468 /*
469 * Has all data been written out yet? Testing on SuperH showed,
470 * that in most cases the first interrupt comes already with the
471 * BUSY status bit clear, but on some operations, like mount or
472 * in the beginning of a write / sync / umount, there is one
473 * DATAEND interrupt with the BUSY bit set, in this cases
474 * waiting for one more interrupt fixes the problem.
475 */
476 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
477 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
478 tasklet_schedule(&host->dma_complete);
479 }
480 } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) {
481 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
482 tasklet_schedule(&host->dma_complete);
483 } else {
484 tmio_mmc_do_data_irq(host);
485 }
486}
487
488static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
Ian Molton4a489982008-07-15 16:02:21 +0100489 unsigned int stat)
490{
Ian Molton4a489982008-07-15 16:02:21 +0100491 struct mmc_command *cmd = host->cmd;
Philipp Zabel5e746722009-06-04 20:12:32 +0200492 int i, addr;
Ian Molton4a489982008-07-15 16:02:21 +0100493
494 if (!host->cmd) {
495 pr_debug("Spurious CMD irq\n");
496 return;
497 }
498
499 host->cmd = NULL;
500
501 /* This controller is sicker than the PXA one. Not only do we need to
502 * drop the top 8 bits of the first response word, we also need to
503 * modify the order of the response for short response command types.
504 */
505
Philipp Zabel5e746722009-06-04 20:12:32 +0200506 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
507 cmd->resp[i] = sd_ctrl_read32(host, addr);
Ian Molton4a489982008-07-15 16:02:21 +0100508
509 if (cmd->flags & MMC_RSP_136) {
510 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
511 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
512 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
513 cmd->resp[3] <<= 8;
514 } else if (cmd->flags & MMC_RSP_R3) {
515 cmd->resp[0] = cmd->resp[3];
516 }
517
518 if (stat & TMIO_STAT_CMDTIMEOUT)
519 cmd->error = -ETIMEDOUT;
520 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
521 cmd->error = -EILSEQ;
522
523 /* If there is data to handle we enable data IRQs here, and
524 * we will ultimatley finish the request in the data_end handler.
525 * If theres no data or we encountered an error, finish now.
526 */
527 if (host->data && !cmd->error) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000528 if (host->data->flags & MMC_DATA_READ) {
529 if (!host->chan_rx)
530 enable_mmc_irqs(host, TMIO_MASK_READOP);
531 } else {
532 struct dma_chan *chan = host->chan_tx;
533 if (!chan)
534 enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
535 else
536 tasklet_schedule(&host->dma_issue);
537 }
Ian Molton4a489982008-07-15 16:02:21 +0100538 } else {
539 tmio_mmc_finish_request(host);
540 }
541
542 return;
543}
544
Ian Molton4a489982008-07-15 16:02:21 +0100545static irqreturn_t tmio_mmc_irq(int irq, void *devid)
546{
547 struct tmio_mmc_host *host = devid;
Ian Molton4a489982008-07-15 16:02:21 +0100548 unsigned int ireg, irq_mask, status;
549
550 pr_debug("MMC IRQ begin\n");
551
Philipp Zabel5e746722009-06-04 20:12:32 +0200552 status = sd_ctrl_read32(host, CTL_STATUS);
553 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100554 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
555
556 pr_debug_status(status);
557 pr_debug_status(ireg);
558
559 if (!ireg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200560 disable_mmc_irqs(host, status & ~irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +0100561
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000562 pr_warning("tmio_mmc: Spurious irq, disabling! "
Ian Molton4a489982008-07-15 16:02:21 +0100563 "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
564 pr_debug_status(status);
565
566 goto out;
567 }
568
569 while (ireg) {
570 /* Card insert / remove attempts */
571 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200572 ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
Ian Molton4a489982008-07-15 16:02:21 +0100573 TMIO_STAT_CARD_REMOVE);
Magnus Damm6d9af5a2010-02-17 16:38:04 +0900574 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
Ian Molton4a489982008-07-15 16:02:21 +0100575 }
576
577 /* CRC and other errors */
578/* if (ireg & TMIO_STAT_ERR_IRQ)
579 * handled |= tmio_error_irq(host, irq, stat);
580 */
581
582 /* Command completion */
583 if (ireg & TMIO_MASK_CMD) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200584 ack_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100585 tmio_mmc_cmd_irq(host, status);
586 }
587
588 /* Data transfer */
589 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200590 ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
Ian Molton4a489982008-07-15 16:02:21 +0100591 tmio_mmc_pio_irq(host);
592 }
593
594 /* Data transfer completion */
595 if (ireg & TMIO_STAT_DATAEND) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200596 ack_mmc_irqs(host, TMIO_STAT_DATAEND);
Ian Molton4a489982008-07-15 16:02:21 +0100597 tmio_mmc_data_irq(host);
598 }
599
600 /* Check status - keep going until we've handled it all */
Philipp Zabel5e746722009-06-04 20:12:32 +0200601 status = sd_ctrl_read32(host, CTL_STATUS);
602 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100603 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
604
605 pr_debug("Status at end of loop: %08x\n", status);
606 pr_debug_status(status);
607 }
608 pr_debug("MMC IRQ end\n");
609
610out:
611 return IRQ_HANDLED;
612}
613
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000614#ifdef CONFIG_TMIO_MMC_DMA
615static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
616{
617#if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
618 /* Switch DMA mode on or off - SuperH specific? */
619 sd_ctrl_write16(host, 0xd8, enable ? 2 : 0);
620#endif
621}
622
623static void tmio_dma_complete(void *arg)
624{
625 struct tmio_mmc_host *host = arg;
626
627 dev_dbg(&host->pdev->dev, "Command completed\n");
628
629 if (!host->data)
630 dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n");
631 else
632 enable_mmc_irqs(host, TMIO_STAT_DATAEND);
633}
634
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100635static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000636{
637 struct scatterlist *sg = host->sg_ptr;
638 struct dma_async_tx_descriptor *desc = NULL;
639 struct dma_chan *chan = host->chan_rx;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100640 dma_cookie_t cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000641 int ret;
642
643 ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_FROM_DEVICE);
644 if (ret > 0) {
645 host->dma_sglen = ret;
646 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
647 DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
648 }
649
650 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000651 desc->callback = tmio_dma_complete;
652 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100653 cookie = desc->tx_submit(desc);
654 if (cookie < 0) {
655 desc = NULL;
656 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000657 } else {
658 chan->device->device_issue_pending(chan);
659 }
660 }
661 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100662 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000663
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100664 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000665 /* DMA failed, fall back to PIO */
666 if (ret >= 0)
667 ret = -EIO;
668 host->chan_rx = NULL;
669 dma_release_channel(chan);
670 /* Free the Tx channel too */
671 chan = host->chan_tx;
672 if (chan) {
673 host->chan_tx = NULL;
674 dma_release_channel(chan);
675 }
676 dev_warn(&host->pdev->dev,
677 "DMA failed: %d, falling back to PIO\n", ret);
678 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000679 }
680
681 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100682 desc, cookie, host->sg_len);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000683}
684
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100685static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000686{
687 struct scatterlist *sg = host->sg_ptr;
688 struct dma_async_tx_descriptor *desc = NULL;
689 struct dma_chan *chan = host->chan_tx;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100690 dma_cookie_t cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000691 int ret;
692
693 ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_TO_DEVICE);
694 if (ret > 0) {
695 host->dma_sglen = ret;
696 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
697 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
698 }
699
700 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000701 desc->callback = tmio_dma_complete;
702 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100703 cookie = desc->tx_submit(desc);
704 if (cookie < 0) {
705 desc = NULL;
706 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000707 }
708 }
709 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100710 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000711
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100712 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000713 /* DMA failed, fall back to PIO */
714 if (ret >= 0)
715 ret = -EIO;
716 host->chan_tx = NULL;
717 dma_release_channel(chan);
718 /* Free the Rx channel too */
719 chan = host->chan_rx;
720 if (chan) {
721 host->chan_rx = NULL;
722 dma_release_channel(chan);
723 }
724 dev_warn(&host->pdev->dev,
725 "DMA failed: %d, falling back to PIO\n", ret);
726 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000727 }
728
729 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100730 desc, cookie);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000731}
732
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100733static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000734 struct mmc_data *data)
735{
736 if (data->flags & MMC_DATA_READ) {
737 if (host->chan_rx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100738 tmio_mmc_start_dma_rx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000739 } else {
740 if (host->chan_tx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100741 tmio_mmc_start_dma_tx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000742 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000743}
744
745static void tmio_issue_tasklet_fn(unsigned long priv)
746{
747 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
748 struct dma_chan *chan = host->chan_tx;
749
750 chan->device->device_issue_pending(chan);
751}
752
753static void tmio_tasklet_fn(unsigned long arg)
754{
755 struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
756
757 if (host->data->flags & MMC_DATA_READ)
758 dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
759 DMA_FROM_DEVICE);
760 else
761 dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
762 DMA_TO_DEVICE);
763
764 tmio_mmc_do_data_irq(host);
765}
766
767/* It might be necessary to make filter MFD specific */
768static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
769{
770 dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
771 chan->private = arg;
772 return true;
773}
774
775static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
776 struct tmio_mmc_data *pdata)
777{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000778 /* We can only either use DMA for both Tx and Rx or not use it at all */
779 if (pdata->dma) {
780 dma_cap_mask_t mask;
781
782 dma_cap_zero(mask);
783 dma_cap_set(DMA_SLAVE, mask);
784
785 host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
786 pdata->dma->chan_priv_tx);
787 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
788 host->chan_tx);
789
790 if (!host->chan_tx)
791 return;
792
793 host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
794 pdata->dma->chan_priv_rx);
795 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
796 host->chan_rx);
797
798 if (!host->chan_rx) {
799 dma_release_channel(host->chan_tx);
800 host->chan_tx = NULL;
801 return;
802 }
803
804 tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host);
805 tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host);
806
807 tmio_mmc_enable_dma(host, true);
808 }
809}
810
811static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
812{
813 if (host->chan_tx) {
814 struct dma_chan *chan = host->chan_tx;
815 host->chan_tx = NULL;
816 dma_release_channel(chan);
817 }
818 if (host->chan_rx) {
819 struct dma_chan *chan = host->chan_rx;
820 host->chan_rx = NULL;
821 dma_release_channel(chan);
822 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000823}
824#else
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100825static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000826 struct mmc_data *data)
827{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000828}
829
830static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
831 struct tmio_mmc_data *pdata)
832{
833 host->chan_tx = NULL;
834 host->chan_rx = NULL;
835}
836
837static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
838{
839}
840#endif
841
Ian Molton4a489982008-07-15 16:02:21 +0100842static int tmio_mmc_start_data(struct tmio_mmc_host *host,
843 struct mmc_data *data)
844{
Yusuke Godaf1334fb2010-08-30 11:50:19 +0100845 struct mfd_cell *cell = host->pdev->dev.platform_data;
846 struct tmio_mmc_data *pdata = cell->driver_data;
847
Ian Molton4a489982008-07-15 16:02:21 +0100848 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000849 data->blksz, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +0100850
Yusuke Godaf1334fb2010-08-30 11:50:19 +0100851 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
852 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
853 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
854
855 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
856 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
857 mmc_hostname(host->mmc), data->blksz);
858 return -EINVAL;
859 }
Ian Molton4a489982008-07-15 16:02:21 +0100860 }
861
862 tmio_mmc_init_sg(host, data);
863 host->data = data;
864
865 /* Set transfer length / blocksize */
Philipp Zabel5e746722009-06-04 20:12:32 +0200866 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
867 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +0100868
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100869 tmio_mmc_start_dma(host, data);
870
871 return 0;
Ian Molton4a489982008-07-15 16:02:21 +0100872}
873
874/* Process requests from the MMC layer */
875static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
876{
877 struct tmio_mmc_host *host = mmc_priv(mmc);
878 int ret;
879
880 if (host->mrq)
881 pr_debug("request not null\n");
882
883 host->mrq = mrq;
884
885 if (mrq->data) {
886 ret = tmio_mmc_start_data(host, mrq->data);
887 if (ret)
888 goto fail;
889 }
890
891 ret = tmio_mmc_start_command(host, mrq->cmd);
Ian Molton4a489982008-07-15 16:02:21 +0100892 if (!ret)
893 return;
894
895fail:
896 mrq->cmd->error = ret;
897 mmc_request_done(mmc, mrq);
898}
899
900/* Set MMC clock / power.
901 * Note: This controller uses a simple divider scheme therefore it cannot
902 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
903 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
904 * slowest setting.
905 */
906static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
907{
908 struct tmio_mmc_host *host = mmc_priv(mmc);
Ian Molton4a489982008-07-15 16:02:21 +0100909
910 if (ios->clock)
911 tmio_mmc_set_clock(host, ios->clock);
912
913 /* Power sequence - OFF -> ON -> UP */
914 switch (ios->power_mode) {
915 case MMC_POWER_OFF: /* power down SD bus */
Ian Molton64e88672010-01-06 13:51:48 +0100916 if (host->set_pwr)
917 host->set_pwr(host->pdev, 0);
Ian Molton4a489982008-07-15 16:02:21 +0100918 tmio_mmc_clk_stop(host);
919 break;
920 case MMC_POWER_ON: /* power up SD bus */
Ian Molton64e88672010-01-06 13:51:48 +0100921 if (host->set_pwr)
922 host->set_pwr(host->pdev, 1);
Ian Molton4a489982008-07-15 16:02:21 +0100923 break;
924 case MMC_POWER_UP: /* start bus clock */
925 tmio_mmc_clk_start(host);
926 break;
927 }
928
929 switch (ios->bus_width) {
930 case MMC_BUS_WIDTH_1:
Philipp Zabel5e746722009-06-04 20:12:32 +0200931 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
Ian Molton4a489982008-07-15 16:02:21 +0100932 break;
933 case MMC_BUS_WIDTH_4:
Philipp Zabel5e746722009-06-04 20:12:32 +0200934 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
Ian Molton4a489982008-07-15 16:02:21 +0100935 break;
936 }
937
938 /* Let things settle. delay taken from winCE driver */
939 udelay(140);
940}
941
942static int tmio_mmc_get_ro(struct mmc_host *mmc)
943{
944 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +0000945 struct mfd_cell *cell = host->pdev->dev.platform_data;
946 struct tmio_mmc_data *pdata = cell->driver_data;
Ian Molton4a489982008-07-15 16:02:21 +0100947
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +0000948 return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
949 (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1;
Ian Molton4a489982008-07-15 16:02:21 +0100950}
951
Arnd Hannemann19ca7502010-08-24 17:26:59 +0200952static int tmio_mmc_get_cd(struct mmc_host *mmc)
953{
954 struct tmio_mmc_host *host = mmc_priv(mmc);
955 struct mfd_cell *cell = host->pdev->dev.platform_data;
956 struct tmio_mmc_data *pdata = cell->driver_data;
957
958 if (!pdata->get_cd)
959 return -ENOSYS;
960 else
961 return pdata->get_cd(host->pdev);
962}
963
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000964static const struct mmc_host_ops tmio_mmc_ops = {
Ian Molton4a489982008-07-15 16:02:21 +0100965 .request = tmio_mmc_request,
966 .set_ios = tmio_mmc_set_ios,
967 .get_ro = tmio_mmc_get_ro,
Arnd Hannemann19ca7502010-08-24 17:26:59 +0200968 .get_cd = tmio_mmc_get_cd,
Ian Molton4a489982008-07-15 16:02:21 +0100969};
970
971#ifdef CONFIG_PM
972static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
973{
974 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
975 struct mmc_host *mmc = platform_get_drvdata(dev);
976 int ret;
977
Matt Fleming1a13f8f2010-05-26 14:42:08 -0700978 ret = mmc_suspend_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +0100979
980 /* Tell MFD core it can disable us now.*/
981 if (!ret && cell->disable)
982 cell->disable(dev);
983
984 return ret;
985}
986
987static int tmio_mmc_resume(struct platform_device *dev)
988{
989 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
990 struct mmc_host *mmc = platform_get_drvdata(dev);
Ian Molton4a489982008-07-15 16:02:21 +0100991 int ret = 0;
992
Ian Molton4a489982008-07-15 16:02:21 +0100993 /* Tell the MFD core we are ready to be enabled */
Ian Molton64e88672010-01-06 13:51:48 +0100994 if (cell->resume) {
995 ret = cell->resume(dev);
Ian Molton4a489982008-07-15 16:02:21 +0100996 if (ret)
997 goto out;
998 }
999
1000 mmc_resume_host(mmc);
1001
1002out:
1003 return ret;
1004}
1005#else
1006#define tmio_mmc_suspend NULL
1007#define tmio_mmc_resume NULL
1008#endif
1009
1010static int __devinit tmio_mmc_probe(struct platform_device *dev)
1011{
1012 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001013 struct tmio_mmc_data *pdata;
Ian Molton64e88672010-01-06 13:51:48 +01001014 struct resource *res_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001015 struct tmio_mmc_host *host;
1016 struct mmc_host *mmc;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001017 int ret = -EINVAL;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001018 u32 irq_mask = TMIO_MASK_CMD;
Ian Molton4a489982008-07-15 16:02:21 +01001019
Ian Molton64e88672010-01-06 13:51:48 +01001020 if (dev->num_resources != 2)
Ian Molton4a489982008-07-15 16:02:21 +01001021 goto out;
1022
1023 res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
Ian Molton64e88672010-01-06 13:51:48 +01001024 if (!res_ctl)
Ian Molton4a489982008-07-15 16:02:21 +01001025 goto out;
Ian Molton4a489982008-07-15 16:02:21 +01001026
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001027 pdata = cell->driver_data;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001028 if (!pdata || !pdata->hclk)
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001029 goto out;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001030
1031 ret = -ENOMEM;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001032
Ian Molton4a489982008-07-15 16:02:21 +01001033 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
1034 if (!mmc)
1035 goto out;
1036
1037 host = mmc_priv(mmc);
1038 host->mmc = mmc;
Ian Molton64e88672010-01-06 13:51:48 +01001039 host->pdev = dev;
Ian Molton4a489982008-07-15 16:02:21 +01001040 platform_set_drvdata(dev, mmc);
1041
Ian Molton64e88672010-01-06 13:51:48 +01001042 host->set_pwr = pdata->set_pwr;
1043 host->set_clk_div = pdata->set_clk_div;
1044
Philipp Zabel5e746722009-06-04 20:12:32 +02001045 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
1046 host->bus_shift = resource_size(res_ctl) >> 10;
1047
Magnus Dammbc6772a2009-03-11 21:58:54 +09001048 host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
Ian Molton4a489982008-07-15 16:02:21 +01001049 if (!host->ctl)
1050 goto host_free;
1051
Ian Molton4a489982008-07-15 16:02:21 +01001052 mmc->ops = &tmio_mmc_ops;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001053 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001054 mmc->f_max = pdata->hclk;
1055 mmc->f_min = mmc->f_max / 512;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001056 mmc->max_segs = 32;
1057 mmc->max_blk_size = 512;
1058 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
1059 mmc->max_segs;
1060 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1061 mmc->max_seg_size = mmc->max_req_size;
Guennadi Liakhovetskia2b14dc2010-05-19 18:37:25 +00001062 if (pdata->ocr_mask)
1063 mmc->ocr_avail = pdata->ocr_mask;
1064 else
1065 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Ian Molton4a489982008-07-15 16:02:21 +01001066
Ian Molton4a489982008-07-15 16:02:21 +01001067 /* Tell the MFD core we are ready to be enabled */
1068 if (cell->enable) {
1069 ret = cell->enable(dev);
1070 if (ret)
Ian Molton64e88672010-01-06 13:51:48 +01001071 goto unmap_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001072 }
1073
Ian Molton4a489982008-07-15 16:02:21 +01001074 tmio_mmc_clk_stop(host);
1075 reset(host);
1076
1077 ret = platform_get_irq(dev, 0);
1078 if (ret >= 0)
1079 host->irq = ret;
1080 else
Magnus Damm7ee422d2010-02-17 16:38:23 +09001081 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001082
Philipp Zabel5e746722009-06-04 20:12:32 +02001083 disable_mmc_irqs(host, TMIO_MASK_ALL);
Ian Molton4a489982008-07-15 16:02:21 +01001084
Philipp Zabel6c413cc2009-06-04 20:12:33 +02001085 ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
Magnus Damm14f1b752009-12-14 18:01:33 -08001086 IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
Ian Molton4a489982008-07-15 16:02:21 +01001087 if (ret)
Magnus Damm7ee422d2010-02-17 16:38:23 +09001088 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001089
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001090 /* See if we also get DMA */
1091 tmio_mmc_request_dma(host, pdata);
1092
Ian Molton4a489982008-07-15 16:02:21 +01001093 mmc_add_host(mmc);
1094
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001095 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
1096 (unsigned long)host->ctl, host->irq);
Ian Molton4a489982008-07-15 16:02:21 +01001097
1098 /* Unmask the IRQs we want to know about */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001099 if (!host->chan_rx)
1100 irq_mask |= TMIO_MASK_READOP;
1101 if (!host->chan_tx)
1102 irq_mask |= TMIO_MASK_WRITEOP;
1103 enable_mmc_irqs(host, irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +01001104
1105 return 0;
1106
Magnus Damm7ee422d2010-02-17 16:38:23 +09001107cell_disable:
1108 if (cell->disable)
1109 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001110unmap_ctl:
1111 iounmap(host->ctl);
1112host_free:
1113 mmc_free_host(mmc);
1114out:
1115 return ret;
1116}
1117
1118static int __devexit tmio_mmc_remove(struct platform_device *dev)
1119{
Magnus Damm7ee422d2010-02-17 16:38:23 +09001120 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Ian Molton4a489982008-07-15 16:02:21 +01001121 struct mmc_host *mmc = platform_get_drvdata(dev);
1122
1123 platform_set_drvdata(dev, NULL);
1124
1125 if (mmc) {
1126 struct tmio_mmc_host *host = mmc_priv(mmc);
1127 mmc_remove_host(mmc);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001128 tmio_mmc_release_dma(host);
Ian Molton4a489982008-07-15 16:02:21 +01001129 free_irq(host->irq, host);
Magnus Damm7ee422d2010-02-17 16:38:23 +09001130 if (cell->disable)
1131 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001132 iounmap(host->ctl);
Magnus Dammbedcc452009-03-11 21:59:03 +09001133 mmc_free_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001134 }
1135
1136 return 0;
1137}
1138
1139/* ------------------- device registration ----------------------- */
1140
1141static struct platform_driver tmio_mmc_driver = {
1142 .driver = {
1143 .name = "tmio-mmc",
1144 .owner = THIS_MODULE,
1145 },
1146 .probe = tmio_mmc_probe,
1147 .remove = __devexit_p(tmio_mmc_remove),
1148 .suspend = tmio_mmc_suspend,
1149 .resume = tmio_mmc_resume,
1150};
1151
1152
1153static int __init tmio_mmc_init(void)
1154{
1155 return platform_driver_register(&tmio_mmc_driver);
1156}
1157
1158static void __exit tmio_mmc_exit(void)
1159{
1160 platform_driver_unregister(&tmio_mmc_driver);
1161}
1162
1163module_init(tmio_mmc_init);
1164module_exit(tmio_mmc_exit);
1165
1166MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
1167MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
1168MODULE_LICENSE("GPL v2");
1169MODULE_ALIAS("platform:tmio-mmc");