blob: 689a3692242eddad16df2cf90e712f66e6a35471 [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>
Arnd Hannemann6ff56e02011-01-05 17:36:14 -050042#include <linux/workqueue.h>
43#include <linux/spinlock.h>
Ian Molton4a489982008-07-15 16:02:21 +010044
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010045#define CTL_SD_CMD 0x00
46#define CTL_ARG_REG 0x04
47#define CTL_STOP_INTERNAL_ACTION 0x08
48#define CTL_XFER_BLK_COUNT 0xa
49#define CTL_RESPONSE 0x0c
50#define CTL_STATUS 0x1c
51#define CTL_IRQ_MASK 0x20
52#define CTL_SD_CARD_CLK_CTL 0x24
53#define CTL_SD_XFER_LEN 0x26
54#define CTL_SD_MEM_CARD_OPT 0x28
55#define CTL_SD_ERROR_DETAIL_STATUS 0x2c
56#define CTL_SD_DATA_PORT 0x30
57#define CTL_TRANSACTION_CTL 0x34
Arnd Hannemann845ecd22010-12-28 23:22:31 +010058#define CTL_SDIO_STATUS 0x36
59#define CTL_SDIO_IRQ_MASK 0x38
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010060#define CTL_RESET_SD 0xe0
61#define CTL_SDIO_REGS 0x100
62#define CTL_CLK_AND_WAIT_CTL 0x138
63#define CTL_RESET_SDIO 0x1e0
64
65/* Definitions for values the CTRL_STATUS register can take. */
66#define TMIO_STAT_CMDRESPEND 0x00000001
67#define TMIO_STAT_DATAEND 0x00000004
68#define TMIO_STAT_CARD_REMOVE 0x00000008
69#define TMIO_STAT_CARD_INSERT 0x00000010
70#define TMIO_STAT_SIGSTATE 0x00000020
71#define TMIO_STAT_WRPROTECT 0x00000080
72#define TMIO_STAT_CARD_REMOVE_A 0x00000100
73#define TMIO_STAT_CARD_INSERT_A 0x00000200
74#define TMIO_STAT_SIGSTATE_A 0x00000400
75#define TMIO_STAT_CMD_IDX_ERR 0x00010000
76#define TMIO_STAT_CRCFAIL 0x00020000
77#define TMIO_STAT_STOPBIT_ERR 0x00040000
78#define TMIO_STAT_DATATIMEOUT 0x00080000
79#define TMIO_STAT_RXOVERFLOW 0x00100000
80#define TMIO_STAT_TXUNDERRUN 0x00200000
81#define TMIO_STAT_CMDTIMEOUT 0x00400000
82#define TMIO_STAT_RXRDY 0x01000000
83#define TMIO_STAT_TXRQ 0x02000000
84#define TMIO_STAT_ILL_FUNC 0x20000000
85#define TMIO_STAT_CMD_BUSY 0x40000000
86#define TMIO_STAT_ILL_ACCESS 0x80000000
87
Arnd Hannemann845ecd22010-12-28 23:22:31 +010088/* Definitions for values the CTRL_SDIO_STATUS register can take. */
89#define TMIO_SDIO_STAT_IOIRQ 0x0001
90#define TMIO_SDIO_STAT_EXPUB52 0x4000
91#define TMIO_SDIO_STAT_EXWT 0x8000
92#define TMIO_SDIO_MASK_ALL 0xc007
93
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010094/* Define some IRQ masks */
95/* This is the mask used at reset by the chip */
96#define TMIO_MASK_ALL 0x837f031d
97#define TMIO_MASK_READOP (TMIO_STAT_RXRDY | TMIO_STAT_DATAEND)
98#define TMIO_MASK_WRITEOP (TMIO_STAT_TXRQ | TMIO_STAT_DATAEND)
99#define TMIO_MASK_CMD (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT | \
100 TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT)
101#define TMIO_MASK_IRQ (TMIO_MASK_READOP | TMIO_MASK_WRITEOP | TMIO_MASK_CMD)
102
103#define enable_mmc_irqs(host, i) \
104 do { \
105 u32 mask;\
106 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
107 mask &= ~((i) & TMIO_MASK_IRQ); \
108 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
109 } while (0)
110
111#define disable_mmc_irqs(host, i) \
112 do { \
113 u32 mask;\
114 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
115 mask |= ((i) & TMIO_MASK_IRQ); \
116 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
117 } while (0)
118
119#define ack_mmc_irqs(host, i) \
120 do { \
121 sd_ctrl_write32((host), CTL_STATUS, ~(i)); \
122 } while (0)
123
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100124/* This is arbitrary, just noone needed any higher alignment yet */
125#define MAX_ALIGN 4
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100126
127struct tmio_mmc_host {
128 void __iomem *ctl;
129 unsigned long bus_shift;
130 struct mmc_command *cmd;
131 struct mmc_request *mrq;
132 struct mmc_data *data;
133 struct mmc_host *mmc;
134 int irq;
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100135 unsigned int sdio_irq_enabled;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100136
137 /* Callbacks for clock / power control */
138 void (*set_pwr)(struct platform_device *host, int state);
139 void (*set_clk_div)(struct platform_device *host, int state);
140
141 /* pio related stuff */
142 struct scatterlist *sg_ptr;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100143 struct scatterlist *sg_orig;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100144 unsigned int sg_len;
145 unsigned int sg_off;
146
147 struct platform_device *pdev;
148
149 /* DMA support */
150 struct dma_chan *chan_rx;
151 struct dma_chan *chan_tx;
152 struct tasklet_struct dma_complete;
153 struct tasklet_struct dma_issue;
154#ifdef CONFIG_TMIO_MMC_DMA
155 unsigned int dma_sglen;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100156 u8 bounce_buf[PAGE_CACHE_SIZE] __attribute__((aligned(MAX_ALIGN)));
157 struct scatterlist bounce_sg;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100158#endif
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500159
160 /* Track lost interrupts */
161 struct delayed_work delayed_reset_work;
162 spinlock_t lock;
163 unsigned long last_req_ts;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100164};
165
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100166static void tmio_check_bounce_buffer(struct tmio_mmc_host *host);
167
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100168static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
169{
170 return readw(host->ctl + (addr << host->bus_shift));
171}
172
173static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
174 u16 *buf, int count)
175{
176 readsw(host->ctl + (addr << host->bus_shift), buf, count);
177}
178
179static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
180{
181 return readw(host->ctl + (addr << host->bus_shift)) |
182 readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
183}
184
185static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
186{
187 writew(val, host->ctl + (addr << host->bus_shift));
188}
189
190static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
191 u16 *buf, int count)
192{
193 writesw(host->ctl + (addr << host->bus_shift), buf, count);
194}
195
196static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
197{
198 writew(val, host->ctl + (addr << host->bus_shift));
199 writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
200}
201
202static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
203{
204 host->sg_len = data->sg_len;
205 host->sg_ptr = data->sg;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100206 host->sg_orig = data->sg;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100207 host->sg_off = 0;
208}
209
210static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
211{
212 host->sg_ptr = sg_next(host->sg_ptr);
213 host->sg_off = 0;
214 return --host->sg_len;
215}
216
217static char *tmio_mmc_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
218{
219 local_irq_save(*flags);
220 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
221}
222
223static void tmio_mmc_kunmap_atomic(void *virt, unsigned long *flags)
224{
225 kunmap_atomic(virt, KM_BIO_SRC_IRQ);
226 local_irq_restore(*flags);
227}
228
229#ifdef CONFIG_MMC_DEBUG
230
231#define STATUS_TO_TEXT(a) \
232 do { \
233 if (status & TMIO_STAT_##a) \
234 printk(#a); \
235 } while (0)
236
237void pr_debug_status(u32 status)
238{
239 printk(KERN_DEBUG "status: %08x = ", status);
240 STATUS_TO_TEXT(CARD_REMOVE);
241 STATUS_TO_TEXT(CARD_INSERT);
242 STATUS_TO_TEXT(SIGSTATE);
243 STATUS_TO_TEXT(WRPROTECT);
244 STATUS_TO_TEXT(CARD_REMOVE_A);
245 STATUS_TO_TEXT(CARD_INSERT_A);
246 STATUS_TO_TEXT(SIGSTATE_A);
247 STATUS_TO_TEXT(CMD_IDX_ERR);
248 STATUS_TO_TEXT(STOPBIT_ERR);
249 STATUS_TO_TEXT(ILL_FUNC);
250 STATUS_TO_TEXT(CMD_BUSY);
251 STATUS_TO_TEXT(CMDRESPEND);
252 STATUS_TO_TEXT(DATAEND);
253 STATUS_TO_TEXT(CRCFAIL);
254 STATUS_TO_TEXT(DATATIMEOUT);
255 STATUS_TO_TEXT(CMDTIMEOUT);
256 STATUS_TO_TEXT(RXOVERFLOW);
257 STATUS_TO_TEXT(TXUNDERRUN);
258 STATUS_TO_TEXT(RXRDY);
259 STATUS_TO_TEXT(TXRQ);
260 STATUS_TO_TEXT(ILL_ACCESS);
261 printk("\n");
262}
263
264#else
265#define pr_debug_status(s) do { } while (0)
266#endif
Ian Molton4a489982008-07-15 16:02:21 +0100267
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100268static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
269{
270 struct tmio_mmc_host *host = mmc_priv(mmc);
271
272 if (enable) {
273 host->sdio_irq_enabled = 1;
274 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
275 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK,
276 (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ));
277 } else {
278 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL);
279 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
280 host->sdio_irq_enabled = 0;
281 }
282}
283
Ian Molton4a489982008-07-15 16:02:21 +0100284static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
285{
Ian Moltonda46a0b2009-06-12 21:53:05 +0100286 u32 clk = 0, clock;
Ian Molton4a489982008-07-15 16:02:21 +0100287
288 if (new_clock) {
Ian Moltonda46a0b2009-06-12 21:53:05 +0100289 for (clock = host->mmc->f_min, clk = 0x80000080;
290 new_clock >= (clock<<1); clk >>= 1)
Ian Molton4a489982008-07-15 16:02:21 +0100291 clock <<= 1;
Ian Molton4a489982008-07-15 16:02:21 +0100292 clk |= 0x100;
293 }
294
Ian Molton64e88672010-01-06 13:51:48 +0100295 if (host->set_clk_div)
296 host->set_clk_div(host->pdev, (clk>>22) & 1);
297
Ian Moltonda46a0b2009-06-12 21:53:05 +0100298 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
Ian Molton4a489982008-07-15 16:02:21 +0100299}
300
301static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
302{
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100303 struct mfd_cell *cell = host->pdev->dev.platform_data;
304 struct tmio_mmc_data *pdata = cell->driver_data;
305
306 /*
307 * Testing on sh-mobile showed that SDIO IRQs are unmasked when
308 * CTL_CLK_AND_WAIT_CTL gets written, so we have to disable the
309 * device IRQ here and restore the SDIO IRQ mask before
310 * re-enabling the device IRQ.
311 */
312 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
313 disable_irq(host->irq);
Philipp Zabel5e746722009-06-04 20:12:32 +0200314 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100315 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100316 if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
317 tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
318 enable_irq(host->irq);
319 }
Philipp Zabel5e746722009-06-04 20:12:32 +0200320 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
321 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100322 msleep(10);
323}
324
325static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
326{
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100327 struct mfd_cell *cell = host->pdev->dev.platform_data;
328 struct tmio_mmc_data *pdata = cell->driver_data;
329
Philipp Zabel5e746722009-06-04 20:12:32 +0200330 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
331 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100332 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100333 /* see comment in tmio_mmc_clk_stop above */
334 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
335 disable_irq(host->irq);
Philipp Zabel5e746722009-06-04 20:12:32 +0200336 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
Ian Molton4a489982008-07-15 16:02:21 +0100337 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100338 if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
339 tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
340 enable_irq(host->irq);
341 }
Ian Molton4a489982008-07-15 16:02:21 +0100342}
343
344static void reset(struct tmio_mmc_host *host)
345{
Ian Molton4a489982008-07-15 16:02:21 +0100346 /* FIXME - should we set stop clock reg here */
Philipp Zabel5e746722009-06-04 20:12:32 +0200347 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
348 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100349 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200350 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
351 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Ian Molton4a489982008-07-15 16:02:21 +0100352 msleep(10);
353}
354
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500355static void tmio_mmc_reset_work(struct work_struct *work)
356{
357 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
358 delayed_reset_work.work);
359 struct mmc_request *mrq;
360 unsigned long flags;
361
362 spin_lock_irqsave(&host->lock, flags);
363 mrq = host->mrq;
364
365 /* request already finished */
366 if (!mrq
367 || time_is_after_jiffies(host->last_req_ts +
368 msecs_to_jiffies(2000))) {
369 spin_unlock_irqrestore(&host->lock, flags);
370 return;
371 }
372
373 dev_warn(&host->pdev->dev,
374 "timeout waiting for hardware interrupt (CMD%u)\n",
375 mrq->cmd->opcode);
376
377 if (host->data)
378 host->data->error = -ETIMEDOUT;
379 else if (host->cmd)
380 host->cmd->error = -ETIMEDOUT;
381 else
382 mrq->cmd->error = -ETIMEDOUT;
383
384 host->cmd = NULL;
385 host->data = NULL;
386 host->mrq = NULL;
387
388 spin_unlock_irqrestore(&host->lock, flags);
389
390 reset(host);
391
392 mmc_request_done(host->mmc, mrq);
393}
394
Ian Molton4a489982008-07-15 16:02:21 +0100395static void
396tmio_mmc_finish_request(struct tmio_mmc_host *host)
397{
398 struct mmc_request *mrq = host->mrq;
399
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500400 if (!mrq)
401 return;
402
Ian Molton4a489982008-07-15 16:02:21 +0100403 host->mrq = NULL;
404 host->cmd = NULL;
405 host->data = NULL;
406
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500407 cancel_delayed_work(&host->delayed_reset_work);
408
Ian Molton4a489982008-07-15 16:02:21 +0100409 mmc_request_done(host->mmc, mrq);
410}
411
412/* These are the bitmasks the tmio chip requires to implement the MMC response
413 * types. Note that R1 and R6 are the same in this scheme. */
414#define APP_CMD 0x0040
415#define RESP_NONE 0x0300
416#define RESP_R1 0x0400
417#define RESP_R1B 0x0500
418#define RESP_R2 0x0600
419#define RESP_R3 0x0700
420#define DATA_PRESENT 0x0800
421#define TRANSFER_READ 0x1000
422#define TRANSFER_MULTI 0x2000
423#define SECURITY_CMD 0x4000
424
425static int
426tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
427{
Ian Molton4a489982008-07-15 16:02:21 +0100428 struct mmc_data *data = host->data;
429 int c = cmd->opcode;
430
431 /* Command 12 is handled by hardware */
432 if (cmd->opcode == 12 && !cmd->arg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200433 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
Ian Molton4a489982008-07-15 16:02:21 +0100434 return 0;
435 }
436
437 switch (mmc_resp_type(cmd)) {
438 case MMC_RSP_NONE: c |= RESP_NONE; break;
439 case MMC_RSP_R1: c |= RESP_R1; break;
440 case MMC_RSP_R1B: c |= RESP_R1B; break;
441 case MMC_RSP_R2: c |= RESP_R2; break;
442 case MMC_RSP_R3: c |= RESP_R3; break;
443 default:
444 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
445 return -EINVAL;
446 }
447
448 host->cmd = cmd;
449
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000450/* FIXME - this seems to be ok commented out but the spec suggest this bit
451 * should be set when issuing app commands.
Ian Molton4a489982008-07-15 16:02:21 +0100452 * if(cmd->flags & MMC_FLAG_ACMD)
453 * c |= APP_CMD;
454 */
455 if (data) {
456 c |= DATA_PRESENT;
457 if (data->blocks > 1) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200458 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
Ian Molton4a489982008-07-15 16:02:21 +0100459 c |= TRANSFER_MULTI;
460 }
461 if (data->flags & MMC_DATA_READ)
462 c |= TRANSFER_READ;
463 }
464
Philipp Zabel5e746722009-06-04 20:12:32 +0200465 enable_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100466
467 /* Fire off the command */
Philipp Zabel5e746722009-06-04 20:12:32 +0200468 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
469 sd_ctrl_write16(host, CTL_SD_CMD, c);
Ian Molton4a489982008-07-15 16:02:21 +0100470
471 return 0;
472}
473
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000474/*
475 * This chip always returns (at least?) as much data as you ask for.
Ian Molton4a489982008-07-15 16:02:21 +0100476 * I'm unsure what happens if you ask for less than a block. This should be
477 * looked into to ensure that a funny length read doesnt hose the controller.
Ian Molton4a489982008-07-15 16:02:21 +0100478 */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000479static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100480{
Ian Molton4a489982008-07-15 16:02:21 +0100481 struct mmc_data *data = host->data;
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700482 void *sg_virt;
Ian Molton4a489982008-07-15 16:02:21 +0100483 unsigned short *buf;
484 unsigned int count;
485 unsigned long flags;
486
487 if (!data) {
488 pr_debug("Spurious PIO IRQ\n");
489 return;
490 }
491
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700492 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
493 buf = (unsigned short *)(sg_virt + host->sg_off);
Ian Molton4a489982008-07-15 16:02:21 +0100494
495 count = host->sg_ptr->length - host->sg_off;
496 if (count > data->blksz)
497 count = data->blksz;
498
499 pr_debug("count: %08x offset: %08x flags %08x\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000500 count, host->sg_off, data->flags);
Ian Molton4a489982008-07-15 16:02:21 +0100501
502 /* Transfer the data */
503 if (data->flags & MMC_DATA_READ)
Philipp Zabel5e746722009-06-04 20:12:32 +0200504 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100505 else
Philipp Zabel5e746722009-06-04 20:12:32 +0200506 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100507
508 host->sg_off += count;
509
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700510 tmio_mmc_kunmap_atomic(sg_virt, &flags);
Ian Molton4a489982008-07-15 16:02:21 +0100511
512 if (host->sg_off == host->sg_ptr->length)
513 tmio_mmc_next_sg(host);
514
515 return;
516}
517
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500518/* needs to be called with host->lock held */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000519static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100520{
Ian Molton4a489982008-07-15 16:02:21 +0100521 struct mmc_data *data = host->data;
Julia Lawalla0d045c2008-12-16 16:13:09 +0100522 struct mmc_command *stop;
Ian Molton4a489982008-07-15 16:02:21 +0100523
524 host->data = NULL;
525
526 if (!data) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000527 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
Ian Molton4a489982008-07-15 16:02:21 +0100528 return;
529 }
Julia Lawalla0d045c2008-12-16 16:13:09 +0100530 stop = data->stop;
Ian Molton4a489982008-07-15 16:02:21 +0100531
532 /* FIXME - return correct transfer count on errors */
533 if (!data->error)
534 data->bytes_xfered = data->blocks * data->blksz;
535 else
536 data->bytes_xfered = 0;
537
538 pr_debug("Completed data request\n");
539
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000540 /*
541 * FIXME: other drivers allow an optional stop command of any given type
Ian Molton4a489982008-07-15 16:02:21 +0100542 * which we dont do, as the chip can auto generate them.
543 * Perhaps we can be smarter about when to use auto CMD12 and
544 * only issue the auto request when we know this is the desired
545 * stop command, allowing fallback to the stop command the
546 * upper layers expect. For now, we do what works.
547 */
548
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000549 if (data->flags & MMC_DATA_READ) {
550 if (!host->chan_rx)
551 disable_mmc_irqs(host, TMIO_MASK_READOP);
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100552 else
553 tmio_check_bounce_buffer(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000554 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
555 host->mrq);
556 } else {
557 if (!host->chan_tx)
558 disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
559 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
560 host->mrq);
561 }
Ian Molton4a489982008-07-15 16:02:21 +0100562
563 if (stop) {
564 if (stop->opcode == 12 && !stop->arg)
Philipp Zabel5e746722009-06-04 20:12:32 +0200565 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
Ian Molton4a489982008-07-15 16:02:21 +0100566 else
567 BUG();
568 }
569
570 tmio_mmc_finish_request(host);
571}
572
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000573static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
574{
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500575 struct mmc_data *data;
576 spin_lock(&host->lock);
577 data = host->data;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000578
579 if (!data)
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500580 goto out;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000581
582 if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) {
583 /*
584 * Has all data been written out yet? Testing on SuperH showed,
585 * that in most cases the first interrupt comes already with the
586 * BUSY status bit clear, but on some operations, like mount or
587 * in the beginning of a write / sync / umount, there is one
588 * DATAEND interrupt with the BUSY bit set, in this cases
589 * waiting for one more interrupt fixes the problem.
590 */
591 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
592 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
593 tasklet_schedule(&host->dma_complete);
594 }
595 } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) {
596 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
597 tasklet_schedule(&host->dma_complete);
598 } else {
599 tmio_mmc_do_data_irq(host);
600 }
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500601out:
602 spin_unlock(&host->lock);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000603}
604
605static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
Ian Molton4a489982008-07-15 16:02:21 +0100606 unsigned int stat)
607{
Ian Molton4a489982008-07-15 16:02:21 +0100608 struct mmc_command *cmd = host->cmd;
Philipp Zabel5e746722009-06-04 20:12:32 +0200609 int i, addr;
Ian Molton4a489982008-07-15 16:02:21 +0100610
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500611 spin_lock(&host->lock);
612
Ian Molton4a489982008-07-15 16:02:21 +0100613 if (!host->cmd) {
614 pr_debug("Spurious CMD irq\n");
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500615 goto out;
Ian Molton4a489982008-07-15 16:02:21 +0100616 }
617
618 host->cmd = NULL;
619
620 /* This controller is sicker than the PXA one. Not only do we need to
621 * drop the top 8 bits of the first response word, we also need to
622 * modify the order of the response for short response command types.
623 */
624
Philipp Zabel5e746722009-06-04 20:12:32 +0200625 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
626 cmd->resp[i] = sd_ctrl_read32(host, addr);
Ian Molton4a489982008-07-15 16:02:21 +0100627
628 if (cmd->flags & MMC_RSP_136) {
629 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
630 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
631 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
632 cmd->resp[3] <<= 8;
633 } else if (cmd->flags & MMC_RSP_R3) {
634 cmd->resp[0] = cmd->resp[3];
635 }
636
637 if (stat & TMIO_STAT_CMDTIMEOUT)
638 cmd->error = -ETIMEDOUT;
639 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
640 cmd->error = -EILSEQ;
641
642 /* If there is data to handle we enable data IRQs here, and
643 * we will ultimatley finish the request in the data_end handler.
644 * If theres no data or we encountered an error, finish now.
645 */
646 if (host->data && !cmd->error) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000647 if (host->data->flags & MMC_DATA_READ) {
648 if (!host->chan_rx)
649 enable_mmc_irqs(host, TMIO_MASK_READOP);
650 } else {
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100651 if (!host->chan_tx)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000652 enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
653 else
654 tasklet_schedule(&host->dma_issue);
655 }
Ian Molton4a489982008-07-15 16:02:21 +0100656 } else {
657 tmio_mmc_finish_request(host);
658 }
659
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500660out:
661 spin_unlock(&host->lock);
662
Ian Molton4a489982008-07-15 16:02:21 +0100663 return;
664}
665
Ian Molton4a489982008-07-15 16:02:21 +0100666static irqreturn_t tmio_mmc_irq(int irq, void *devid)
667{
668 struct tmio_mmc_host *host = devid;
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100669 struct mfd_cell *cell = host->pdev->dev.platform_data;
670 struct tmio_mmc_data *pdata = cell->driver_data;
Ian Molton4a489982008-07-15 16:02:21 +0100671 unsigned int ireg, irq_mask, status;
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100672 unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
Ian Molton4a489982008-07-15 16:02:21 +0100673
674 pr_debug("MMC IRQ begin\n");
675
Philipp Zabel5e746722009-06-04 20:12:32 +0200676 status = sd_ctrl_read32(host, CTL_STATUS);
677 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100678 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
679
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100680 sdio_ireg = 0;
681 if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
682 sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
683 sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
684 sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
685
686 sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
687
688 if (sdio_ireg && !host->sdio_irq_enabled) {
689 pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
690 sdio_status, sdio_irq_mask, sdio_ireg);
691 tmio_mmc_enable_sdio_irq(host->mmc, 0);
692 goto out;
693 }
694
695 if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
696 sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
697 mmc_signal_sdio_irq(host->mmc);
698
699 if (sdio_ireg)
700 goto out;
701 }
702
Ian Molton4a489982008-07-15 16:02:21 +0100703 pr_debug_status(status);
704 pr_debug_status(ireg);
705
706 if (!ireg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200707 disable_mmc_irqs(host, status & ~irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +0100708
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000709 pr_warning("tmio_mmc: Spurious irq, disabling! "
Ian Molton4a489982008-07-15 16:02:21 +0100710 "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
711 pr_debug_status(status);
712
713 goto out;
714 }
715
716 while (ireg) {
717 /* Card insert / remove attempts */
718 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200719 ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
Ian Molton4a489982008-07-15 16:02:21 +0100720 TMIO_STAT_CARD_REMOVE);
Magnus Damm6d9af5a2010-02-17 16:38:04 +0900721 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
Ian Molton4a489982008-07-15 16:02:21 +0100722 }
723
724 /* CRC and other errors */
725/* if (ireg & TMIO_STAT_ERR_IRQ)
726 * handled |= tmio_error_irq(host, irq, stat);
727 */
728
729 /* Command completion */
730 if (ireg & TMIO_MASK_CMD) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200731 ack_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100732 tmio_mmc_cmd_irq(host, status);
733 }
734
735 /* Data transfer */
736 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200737 ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
Ian Molton4a489982008-07-15 16:02:21 +0100738 tmio_mmc_pio_irq(host);
739 }
740
741 /* Data transfer completion */
742 if (ireg & TMIO_STAT_DATAEND) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200743 ack_mmc_irqs(host, TMIO_STAT_DATAEND);
Ian Molton4a489982008-07-15 16:02:21 +0100744 tmio_mmc_data_irq(host);
745 }
746
747 /* Check status - keep going until we've handled it all */
Philipp Zabel5e746722009-06-04 20:12:32 +0200748 status = sd_ctrl_read32(host, CTL_STATUS);
749 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100750 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
751
752 pr_debug("Status at end of loop: %08x\n", status);
753 pr_debug_status(status);
754 }
755 pr_debug("MMC IRQ end\n");
756
757out:
758 return IRQ_HANDLED;
759}
760
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000761#ifdef CONFIG_TMIO_MMC_DMA
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100762static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
763{
764 if (host->sg_ptr == &host->bounce_sg) {
765 unsigned long flags;
766 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
767 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
768 tmio_mmc_kunmap_atomic(sg_vaddr, &flags);
769 }
770}
771
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000772static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
773{
774#if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
775 /* Switch DMA mode on or off - SuperH specific? */
776 sd_ctrl_write16(host, 0xd8, enable ? 2 : 0);
777#endif
778}
779
780static void tmio_dma_complete(void *arg)
781{
782 struct tmio_mmc_host *host = arg;
783
784 dev_dbg(&host->pdev->dev, "Command completed\n");
785
786 if (!host->data)
787 dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n");
788 else
789 enable_mmc_irqs(host, TMIO_STAT_DATAEND);
790}
791
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100792static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000793{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100794 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000795 struct dma_async_tx_descriptor *desc = NULL;
796 struct dma_chan *chan = host->chan_rx;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100797 struct mfd_cell *cell = host->pdev->dev.platform_data;
798 struct tmio_mmc_data *pdata = cell->driver_data;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100799 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100800 int ret, i;
801 bool aligned = true, multiple = true;
802 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
803
804 for_each_sg(sg, sg_tmp, host->sg_len, i) {
805 if (sg_tmp->offset & align)
806 aligned = false;
807 if (sg_tmp->length & align) {
808 multiple = false;
809 break;
810 }
811 }
812
813 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000814 align >= MAX_ALIGN)) || !multiple) {
815 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100816 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000817 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100818
819 /* The only sg element can be unaligned, use our bounce buffer then */
820 if (!aligned) {
821 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
822 host->sg_ptr = &host->bounce_sg;
823 sg = host->sg_ptr;
824 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000825
826 ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_FROM_DEVICE);
827 if (ret > 0) {
828 host->dma_sglen = ret;
829 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
830 DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
831 }
832
833 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000834 desc->callback = tmio_dma_complete;
835 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100836 cookie = desc->tx_submit(desc);
837 if (cookie < 0) {
838 desc = NULL;
839 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000840 } else {
841 chan->device->device_issue_pending(chan);
842 }
843 }
844 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100845 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000846
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100847pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100848 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000849 /* DMA failed, fall back to PIO */
850 if (ret >= 0)
851 ret = -EIO;
852 host->chan_rx = NULL;
853 dma_release_channel(chan);
854 /* Free the Tx channel too */
855 chan = host->chan_tx;
856 if (chan) {
857 host->chan_tx = NULL;
858 dma_release_channel(chan);
859 }
860 dev_warn(&host->pdev->dev,
861 "DMA failed: %d, falling back to PIO\n", ret);
862 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000863 }
864
865 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100866 desc, cookie, host->sg_len);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000867}
868
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100869static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000870{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100871 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000872 struct dma_async_tx_descriptor *desc = NULL;
873 struct dma_chan *chan = host->chan_tx;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100874 struct mfd_cell *cell = host->pdev->dev.platform_data;
875 struct tmio_mmc_data *pdata = cell->driver_data;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100876 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100877 int ret, i;
878 bool aligned = true, multiple = true;
879 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
880
881 for_each_sg(sg, sg_tmp, host->sg_len, i) {
882 if (sg_tmp->offset & align)
883 aligned = false;
884 if (sg_tmp->length & align) {
885 multiple = false;
886 break;
887 }
888 }
889
890 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000891 align >= MAX_ALIGN)) || !multiple) {
892 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100893 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000894 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100895
896 /* The only sg element can be unaligned, use our bounce buffer then */
897 if (!aligned) {
898 unsigned long flags;
899 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
900 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
901 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
902 tmio_mmc_kunmap_atomic(sg_vaddr, &flags);
903 host->sg_ptr = &host->bounce_sg;
904 sg = host->sg_ptr;
905 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000906
907 ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_TO_DEVICE);
908 if (ret > 0) {
909 host->dma_sglen = ret;
910 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
911 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
912 }
913
914 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000915 desc->callback = tmio_dma_complete;
916 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100917 cookie = desc->tx_submit(desc);
918 if (cookie < 0) {
919 desc = NULL;
920 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000921 }
922 }
923 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100924 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000925
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100926pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100927 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000928 /* DMA failed, fall back to PIO */
929 if (ret >= 0)
930 ret = -EIO;
931 host->chan_tx = NULL;
932 dma_release_channel(chan);
933 /* Free the Rx channel too */
934 chan = host->chan_rx;
935 if (chan) {
936 host->chan_rx = NULL;
937 dma_release_channel(chan);
938 }
939 dev_warn(&host->pdev->dev,
940 "DMA failed: %d, falling back to PIO\n", ret);
941 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000942 }
943
944 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100945 desc, cookie);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000946}
947
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100948static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000949 struct mmc_data *data)
950{
951 if (data->flags & MMC_DATA_READ) {
952 if (host->chan_rx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100953 tmio_mmc_start_dma_rx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000954 } else {
955 if (host->chan_tx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100956 tmio_mmc_start_dma_tx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000957 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000958}
959
960static void tmio_issue_tasklet_fn(unsigned long priv)
961{
962 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
963 struct dma_chan *chan = host->chan_tx;
964
965 chan->device->device_issue_pending(chan);
966}
967
968static void tmio_tasklet_fn(unsigned long arg)
969{
970 struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500971 unsigned long flags;
972
973 spin_lock_irqsave(&host->lock, flags);
974
975 if (!host->data)
976 goto out;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000977
978 if (host->data->flags & MMC_DATA_READ)
979 dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
980 DMA_FROM_DEVICE);
981 else
982 dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
983 DMA_TO_DEVICE);
984
985 tmio_mmc_do_data_irq(host);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500986out:
987 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000988}
989
990/* It might be necessary to make filter MFD specific */
991static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
992{
993 dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
994 chan->private = arg;
995 return true;
996}
997
998static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
999 struct tmio_mmc_data *pdata)
1000{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001001 /* We can only either use DMA for both Tx and Rx or not use it at all */
1002 if (pdata->dma) {
1003 dma_cap_mask_t mask;
1004
1005 dma_cap_zero(mask);
1006 dma_cap_set(DMA_SLAVE, mask);
1007
1008 host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
1009 pdata->dma->chan_priv_tx);
1010 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
1011 host->chan_tx);
1012
1013 if (!host->chan_tx)
1014 return;
1015
1016 host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
1017 pdata->dma->chan_priv_rx);
1018 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
1019 host->chan_rx);
1020
1021 if (!host->chan_rx) {
1022 dma_release_channel(host->chan_tx);
1023 host->chan_tx = NULL;
1024 return;
1025 }
1026
1027 tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host);
1028 tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host);
1029
1030 tmio_mmc_enable_dma(host, true);
1031 }
1032}
1033
1034static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
1035{
1036 if (host->chan_tx) {
1037 struct dma_chan *chan = host->chan_tx;
1038 host->chan_tx = NULL;
1039 dma_release_channel(chan);
1040 }
1041 if (host->chan_rx) {
1042 struct dma_chan *chan = host->chan_rx;
1043 host->chan_rx = NULL;
1044 dma_release_channel(chan);
1045 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001046}
1047#else
Guennadi Liakhovetski93173052010-12-22 12:02:15 +01001048static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
1049{
1050}
1051
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +01001052static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001053 struct mmc_data *data)
1054{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001055}
1056
1057static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
1058 struct tmio_mmc_data *pdata)
1059{
1060 host->chan_tx = NULL;
1061 host->chan_rx = NULL;
1062}
1063
1064static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
1065{
1066}
1067#endif
1068
Ian Molton4a489982008-07-15 16:02:21 +01001069static int tmio_mmc_start_data(struct tmio_mmc_host *host,
1070 struct mmc_data *data)
1071{
Yusuke Godaf1334fb2010-08-30 11:50:19 +01001072 struct mfd_cell *cell = host->pdev->dev.platform_data;
1073 struct tmio_mmc_data *pdata = cell->driver_data;
1074
Ian Molton4a489982008-07-15 16:02:21 +01001075 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001076 data->blksz, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +01001077
Yusuke Godaf1334fb2010-08-30 11:50:19 +01001078 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
1079 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1080 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
1081
1082 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
1083 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
1084 mmc_hostname(host->mmc), data->blksz);
1085 return -EINVAL;
1086 }
Ian Molton4a489982008-07-15 16:02:21 +01001087 }
1088
1089 tmio_mmc_init_sg(host, data);
1090 host->data = data;
1091
1092 /* Set transfer length / blocksize */
Philipp Zabel5e746722009-06-04 20:12:32 +02001093 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
1094 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +01001095
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +01001096 tmio_mmc_start_dma(host, data);
1097
1098 return 0;
Ian Molton4a489982008-07-15 16:02:21 +01001099}
1100
1101/* Process requests from the MMC layer */
1102static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
1103{
1104 struct tmio_mmc_host *host = mmc_priv(mmc);
1105 int ret;
1106
1107 if (host->mrq)
1108 pr_debug("request not null\n");
1109
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001110 host->last_req_ts = jiffies;
1111 wmb();
Ian Molton4a489982008-07-15 16:02:21 +01001112 host->mrq = mrq;
1113
1114 if (mrq->data) {
1115 ret = tmio_mmc_start_data(host, mrq->data);
1116 if (ret)
1117 goto fail;
1118 }
1119
1120 ret = tmio_mmc_start_command(host, mrq->cmd);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001121 if (!ret) {
1122 schedule_delayed_work(&host->delayed_reset_work,
1123 msecs_to_jiffies(2000));
Ian Molton4a489982008-07-15 16:02:21 +01001124 return;
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001125 }
Ian Molton4a489982008-07-15 16:02:21 +01001126
1127fail:
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001128 host->mrq = NULL;
Ian Molton4a489982008-07-15 16:02:21 +01001129 mrq->cmd->error = ret;
1130 mmc_request_done(mmc, mrq);
1131}
1132
1133/* Set MMC clock / power.
1134 * Note: This controller uses a simple divider scheme therefore it cannot
1135 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
1136 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
1137 * slowest setting.
1138 */
1139static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1140{
1141 struct tmio_mmc_host *host = mmc_priv(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001142
1143 if (ios->clock)
1144 tmio_mmc_set_clock(host, ios->clock);
1145
1146 /* Power sequence - OFF -> ON -> UP */
1147 switch (ios->power_mode) {
1148 case MMC_POWER_OFF: /* power down SD bus */
Ian Molton64e88672010-01-06 13:51:48 +01001149 if (host->set_pwr)
1150 host->set_pwr(host->pdev, 0);
Ian Molton4a489982008-07-15 16:02:21 +01001151 tmio_mmc_clk_stop(host);
1152 break;
1153 case MMC_POWER_ON: /* power up SD bus */
Ian Molton64e88672010-01-06 13:51:48 +01001154 if (host->set_pwr)
1155 host->set_pwr(host->pdev, 1);
Ian Molton4a489982008-07-15 16:02:21 +01001156 break;
1157 case MMC_POWER_UP: /* start bus clock */
1158 tmio_mmc_clk_start(host);
1159 break;
1160 }
1161
1162 switch (ios->bus_width) {
1163 case MMC_BUS_WIDTH_1:
Philipp Zabel5e746722009-06-04 20:12:32 +02001164 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
Ian Molton4a489982008-07-15 16:02:21 +01001165 break;
1166 case MMC_BUS_WIDTH_4:
Philipp Zabel5e746722009-06-04 20:12:32 +02001167 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
Ian Molton4a489982008-07-15 16:02:21 +01001168 break;
1169 }
1170
1171 /* Let things settle. delay taken from winCE driver */
1172 udelay(140);
1173}
1174
1175static int tmio_mmc_get_ro(struct mmc_host *mmc)
1176{
1177 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +00001178 struct mfd_cell *cell = host->pdev->dev.platform_data;
1179 struct tmio_mmc_data *pdata = cell->driver_data;
Ian Molton4a489982008-07-15 16:02:21 +01001180
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +00001181 return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
1182 (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1;
Ian Molton4a489982008-07-15 16:02:21 +01001183}
1184
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001185static int tmio_mmc_get_cd(struct mmc_host *mmc)
1186{
1187 struct tmio_mmc_host *host = mmc_priv(mmc);
1188 struct mfd_cell *cell = host->pdev->dev.platform_data;
1189 struct tmio_mmc_data *pdata = cell->driver_data;
1190
1191 if (!pdata->get_cd)
1192 return -ENOSYS;
1193 else
1194 return pdata->get_cd(host->pdev);
1195}
1196
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001197static const struct mmc_host_ops tmio_mmc_ops = {
Ian Molton4a489982008-07-15 16:02:21 +01001198 .request = tmio_mmc_request,
1199 .set_ios = tmio_mmc_set_ios,
1200 .get_ro = tmio_mmc_get_ro,
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001201 .get_cd = tmio_mmc_get_cd,
Arnd Hannemann845ecd22010-12-28 23:22:31 +01001202 .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
Ian Molton4a489982008-07-15 16:02:21 +01001203};
1204
1205#ifdef CONFIG_PM
1206static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
1207{
1208 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
1209 struct mmc_host *mmc = platform_get_drvdata(dev);
1210 int ret;
1211
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001212 ret = mmc_suspend_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001213
1214 /* Tell MFD core it can disable us now.*/
1215 if (!ret && cell->disable)
1216 cell->disable(dev);
1217
1218 return ret;
1219}
1220
1221static int tmio_mmc_resume(struct platform_device *dev)
1222{
1223 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
1224 struct mmc_host *mmc = platform_get_drvdata(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001225 int ret = 0;
1226
Ian Molton4a489982008-07-15 16:02:21 +01001227 /* Tell the MFD core we are ready to be enabled */
Ian Molton64e88672010-01-06 13:51:48 +01001228 if (cell->resume) {
1229 ret = cell->resume(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001230 if (ret)
1231 goto out;
1232 }
1233
1234 mmc_resume_host(mmc);
1235
1236out:
1237 return ret;
1238}
1239#else
1240#define tmio_mmc_suspend NULL
1241#define tmio_mmc_resume NULL
1242#endif
1243
1244static int __devinit tmio_mmc_probe(struct platform_device *dev)
1245{
1246 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001247 struct tmio_mmc_data *pdata;
Ian Molton64e88672010-01-06 13:51:48 +01001248 struct resource *res_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001249 struct tmio_mmc_host *host;
1250 struct mmc_host *mmc;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001251 int ret = -EINVAL;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001252 u32 irq_mask = TMIO_MASK_CMD;
Ian Molton4a489982008-07-15 16:02:21 +01001253
Ian Molton64e88672010-01-06 13:51:48 +01001254 if (dev->num_resources != 2)
Ian Molton4a489982008-07-15 16:02:21 +01001255 goto out;
1256
1257 res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
Ian Molton64e88672010-01-06 13:51:48 +01001258 if (!res_ctl)
Ian Molton4a489982008-07-15 16:02:21 +01001259 goto out;
Ian Molton4a489982008-07-15 16:02:21 +01001260
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001261 pdata = cell->driver_data;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001262 if (!pdata || !pdata->hclk)
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001263 goto out;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001264
1265 ret = -ENOMEM;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001266
Ian Molton4a489982008-07-15 16:02:21 +01001267 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
1268 if (!mmc)
1269 goto out;
1270
1271 host = mmc_priv(mmc);
1272 host->mmc = mmc;
Ian Molton64e88672010-01-06 13:51:48 +01001273 host->pdev = dev;
Ian Molton4a489982008-07-15 16:02:21 +01001274 platform_set_drvdata(dev, mmc);
1275
Ian Molton64e88672010-01-06 13:51:48 +01001276 host->set_pwr = pdata->set_pwr;
1277 host->set_clk_div = pdata->set_clk_div;
1278
Philipp Zabel5e746722009-06-04 20:12:32 +02001279 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
1280 host->bus_shift = resource_size(res_ctl) >> 10;
1281
Magnus Dammbc6772a2009-03-11 21:58:54 +09001282 host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
Ian Molton4a489982008-07-15 16:02:21 +01001283 if (!host->ctl)
1284 goto host_free;
1285
Ian Molton4a489982008-07-15 16:02:21 +01001286 mmc->ops = &tmio_mmc_ops;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001287 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001288 mmc->f_max = pdata->hclk;
1289 mmc->f_min = mmc->f_max / 512;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001290 mmc->max_segs = 32;
1291 mmc->max_blk_size = 512;
1292 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
1293 mmc->max_segs;
1294 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1295 mmc->max_seg_size = mmc->max_req_size;
Guennadi Liakhovetskia2b14dc2010-05-19 18:37:25 +00001296 if (pdata->ocr_mask)
1297 mmc->ocr_avail = pdata->ocr_mask;
1298 else
1299 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Ian Molton4a489982008-07-15 16:02:21 +01001300
Ian Molton4a489982008-07-15 16:02:21 +01001301 /* Tell the MFD core we are ready to be enabled */
1302 if (cell->enable) {
1303 ret = cell->enable(dev);
1304 if (ret)
Ian Molton64e88672010-01-06 13:51:48 +01001305 goto unmap_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001306 }
1307
Ian Molton4a489982008-07-15 16:02:21 +01001308 tmio_mmc_clk_stop(host);
1309 reset(host);
1310
1311 ret = platform_get_irq(dev, 0);
1312 if (ret >= 0)
1313 host->irq = ret;
1314 else
Magnus Damm7ee422d2010-02-17 16:38:23 +09001315 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001316
Philipp Zabel5e746722009-06-04 20:12:32 +02001317 disable_mmc_irqs(host, TMIO_MASK_ALL);
Arnd Hannemann845ecd22010-12-28 23:22:31 +01001318 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
1319 tmio_mmc_enable_sdio_irq(mmc, 0);
Ian Molton4a489982008-07-15 16:02:21 +01001320
Philipp Zabel6c413cc2009-06-04 20:12:33 +02001321 ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
Magnus Damm14f1b752009-12-14 18:01:33 -08001322 IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
Ian Molton4a489982008-07-15 16:02:21 +01001323 if (ret)
Magnus Damm7ee422d2010-02-17 16:38:23 +09001324 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001325
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001326 spin_lock_init(&host->lock);
1327
1328 /* Init delayed work for request timeouts */
1329 INIT_DELAYED_WORK(&host->delayed_reset_work, tmio_mmc_reset_work);
1330
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001331 /* See if we also get DMA */
1332 tmio_mmc_request_dma(host, pdata);
1333
Ian Molton4a489982008-07-15 16:02:21 +01001334 mmc_add_host(mmc);
1335
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001336 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
1337 (unsigned long)host->ctl, host->irq);
Ian Molton4a489982008-07-15 16:02:21 +01001338
1339 /* Unmask the IRQs we want to know about */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001340 if (!host->chan_rx)
1341 irq_mask |= TMIO_MASK_READOP;
1342 if (!host->chan_tx)
1343 irq_mask |= TMIO_MASK_WRITEOP;
1344 enable_mmc_irqs(host, irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +01001345
1346 return 0;
1347
Magnus Damm7ee422d2010-02-17 16:38:23 +09001348cell_disable:
1349 if (cell->disable)
1350 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001351unmap_ctl:
1352 iounmap(host->ctl);
1353host_free:
1354 mmc_free_host(mmc);
1355out:
1356 return ret;
1357}
1358
1359static int __devexit tmio_mmc_remove(struct platform_device *dev)
1360{
Magnus Damm7ee422d2010-02-17 16:38:23 +09001361 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Ian Molton4a489982008-07-15 16:02:21 +01001362 struct mmc_host *mmc = platform_get_drvdata(dev);
1363
1364 platform_set_drvdata(dev, NULL);
1365
1366 if (mmc) {
1367 struct tmio_mmc_host *host = mmc_priv(mmc);
1368 mmc_remove_host(mmc);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001369 cancel_delayed_work_sync(&host->delayed_reset_work);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001370 tmio_mmc_release_dma(host);
Ian Molton4a489982008-07-15 16:02:21 +01001371 free_irq(host->irq, host);
Magnus Damm7ee422d2010-02-17 16:38:23 +09001372 if (cell->disable)
1373 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001374 iounmap(host->ctl);
Magnus Dammbedcc452009-03-11 21:59:03 +09001375 mmc_free_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001376 }
1377
1378 return 0;
1379}
1380
1381/* ------------------- device registration ----------------------- */
1382
1383static struct platform_driver tmio_mmc_driver = {
1384 .driver = {
1385 .name = "tmio-mmc",
1386 .owner = THIS_MODULE,
1387 },
1388 .probe = tmio_mmc_probe,
1389 .remove = __devexit_p(tmio_mmc_remove),
1390 .suspend = tmio_mmc_suspend,
1391 .resume = tmio_mmc_resume,
1392};
1393
1394
1395static int __init tmio_mmc_init(void)
1396{
1397 return platform_driver_register(&tmio_mmc_driver);
1398}
1399
1400static void __exit tmio_mmc_exit(void)
1401{
1402 platform_driver_unregister(&tmio_mmc_driver);
1403}
1404
1405module_init(tmio_mmc_init);
1406module_exit(tmio_mmc_exit);
1407
1408MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
1409MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
1410MODULE_LICENSE("GPL v2");
1411MODULE_ALIAS("platform:tmio-mmc");