blob: 699b6ab209b24c0f3b1d6e6626ca78ee67487477 [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
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100155 u8 bounce_buf[PAGE_CACHE_SIZE] __attribute__((aligned(MAX_ALIGN)));
156 struct scatterlist bounce_sg;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100157#endif
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500158
159 /* Track lost interrupts */
160 struct delayed_work delayed_reset_work;
161 spinlock_t lock;
162 unsigned long last_req_ts;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100163};
164
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100165static void tmio_check_bounce_buffer(struct tmio_mmc_host *host);
166
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100167static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
168{
169 return readw(host->ctl + (addr << host->bus_shift));
170}
171
172static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
173 u16 *buf, int count)
174{
175 readsw(host->ctl + (addr << host->bus_shift), buf, count);
176}
177
178static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
179{
180 return readw(host->ctl + (addr << host->bus_shift)) |
181 readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
182}
183
184static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
185{
186 writew(val, host->ctl + (addr << host->bus_shift));
187}
188
189static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
190 u16 *buf, int count)
191{
192 writesw(host->ctl + (addr << host->bus_shift), buf, count);
193}
194
195static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
196{
197 writew(val, host->ctl + (addr << host->bus_shift));
198 writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
199}
200
201static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
202{
203 host->sg_len = data->sg_len;
204 host->sg_ptr = data->sg;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100205 host->sg_orig = data->sg;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100206 host->sg_off = 0;
207}
208
209static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
210{
211 host->sg_ptr = sg_next(host->sg_ptr);
212 host->sg_off = 0;
213 return --host->sg_len;
214}
215
216static char *tmio_mmc_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
217{
218 local_irq_save(*flags);
219 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
220}
221
222static void tmio_mmc_kunmap_atomic(void *virt, unsigned long *flags)
223{
224 kunmap_atomic(virt, KM_BIO_SRC_IRQ);
225 local_irq_restore(*flags);
226}
227
228#ifdef CONFIG_MMC_DEBUG
229
Simon Hormana803d7f2011-02-09 07:25:22 +0900230#define STATUS_TO_TEXT(a, status, i) \
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100231 do { \
Simon Hormana803d7f2011-02-09 07:25:22 +0900232 if (status & TMIO_STAT_##a) { \
233 if (i++) \
234 printk(" | "); \
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100235 printk(#a); \
Simon Hormana803d7f2011-02-09 07:25:22 +0900236 } \
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100237 } while (0)
238
239void pr_debug_status(u32 status)
240{
Simon Hormana803d7f2011-02-09 07:25:22 +0900241 int i = 0;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100242 printk(KERN_DEBUG "status: %08x = ", status);
Simon Hormana803d7f2011-02-09 07:25:22 +0900243 STATUS_TO_TEXT(CARD_REMOVE, status, i);
244 STATUS_TO_TEXT(CARD_INSERT, status, i);
245 STATUS_TO_TEXT(SIGSTATE, status, i);
246 STATUS_TO_TEXT(WRPROTECT, status, i);
247 STATUS_TO_TEXT(CARD_REMOVE_A, status, i);
248 STATUS_TO_TEXT(CARD_INSERT_A, status, i);
249 STATUS_TO_TEXT(SIGSTATE_A, status, i);
250 STATUS_TO_TEXT(CMD_IDX_ERR, status, i);
251 STATUS_TO_TEXT(STOPBIT_ERR, status, i);
252 STATUS_TO_TEXT(ILL_FUNC, status, i);
253 STATUS_TO_TEXT(CMD_BUSY, status, i);
254 STATUS_TO_TEXT(CMDRESPEND, status, i);
255 STATUS_TO_TEXT(DATAEND, status, i);
256 STATUS_TO_TEXT(CRCFAIL, status, i);
257 STATUS_TO_TEXT(DATATIMEOUT, status, i);
258 STATUS_TO_TEXT(CMDTIMEOUT, status, i);
259 STATUS_TO_TEXT(RXOVERFLOW, status, i);
260 STATUS_TO_TEXT(TXUNDERRUN, status, i);
261 STATUS_TO_TEXT(RXRDY, status, i);
262 STATUS_TO_TEXT(TXRQ, status, i);
263 STATUS_TO_TEXT(ILL_ACCESS, status, i);
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100264 printk("\n");
265}
266
267#else
268#define pr_debug_status(s) do { } while (0)
269#endif
Ian Molton4a489982008-07-15 16:02:21 +0100270
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100271static void tmio_mmc_enable_sdio_irq(struct mmc_host *mmc, int enable)
272{
273 struct tmio_mmc_host *host = mmc_priv(mmc);
274
275 if (enable) {
276 host->sdio_irq_enabled = 1;
277 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0001);
278 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK,
279 (TMIO_SDIO_MASK_ALL & ~TMIO_SDIO_STAT_IOIRQ));
280 } else {
281 sd_ctrl_write16(host, CTL_SDIO_IRQ_MASK, TMIO_SDIO_MASK_ALL);
282 sd_ctrl_write16(host, CTL_TRANSACTION_CTL, 0x0000);
283 host->sdio_irq_enabled = 0;
284 }
285}
286
Ian Molton4a489982008-07-15 16:02:21 +0100287static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
288{
Ian Moltonda46a0b2009-06-12 21:53:05 +0100289 u32 clk = 0, clock;
Ian Molton4a489982008-07-15 16:02:21 +0100290
291 if (new_clock) {
Ian Moltonda46a0b2009-06-12 21:53:05 +0100292 for (clock = host->mmc->f_min, clk = 0x80000080;
293 new_clock >= (clock<<1); clk >>= 1)
Ian Molton4a489982008-07-15 16:02:21 +0100294 clock <<= 1;
Ian Molton4a489982008-07-15 16:02:21 +0100295 clk |= 0x100;
296 }
297
Ian Molton64e88672010-01-06 13:51:48 +0100298 if (host->set_clk_div)
299 host->set_clk_div(host->pdev, (clk>>22) & 1);
300
Ian Moltonda46a0b2009-06-12 21:53:05 +0100301 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
Ian Molton4a489982008-07-15 16:02:21 +0100302}
303
304static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
305{
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100306 struct mfd_cell *cell = host->pdev->dev.platform_data;
307 struct tmio_mmc_data *pdata = cell->driver_data;
308
309 /*
310 * Testing on sh-mobile showed that SDIO IRQs are unmasked when
311 * CTL_CLK_AND_WAIT_CTL gets written, so we have to disable the
312 * device IRQ here and restore the SDIO IRQ mask before
313 * re-enabling the device IRQ.
314 */
315 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
316 disable_irq(host->irq);
Philipp Zabel5e746722009-06-04 20:12:32 +0200317 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100318 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100319 if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
320 tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
321 enable_irq(host->irq);
322 }
Philipp Zabel5e746722009-06-04 20:12:32 +0200323 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
324 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100325 msleep(10);
326}
327
328static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
329{
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100330 struct mfd_cell *cell = host->pdev->dev.platform_data;
331 struct tmio_mmc_data *pdata = cell->driver_data;
332
Philipp Zabel5e746722009-06-04 20:12:32 +0200333 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
334 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100335 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100336 /* see comment in tmio_mmc_clk_stop above */
337 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
338 disable_irq(host->irq);
Philipp Zabel5e746722009-06-04 20:12:32 +0200339 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
Ian Molton4a489982008-07-15 16:02:21 +0100340 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100341 if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
342 tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
343 enable_irq(host->irq);
344 }
Ian Molton4a489982008-07-15 16:02:21 +0100345}
346
347static void reset(struct tmio_mmc_host *host)
348{
Ian Molton4a489982008-07-15 16:02:21 +0100349 /* FIXME - should we set stop clock reg here */
Philipp Zabel5e746722009-06-04 20:12:32 +0200350 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
351 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100352 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200353 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
354 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Ian Molton4a489982008-07-15 16:02:21 +0100355 msleep(10);
356}
357
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500358static void tmio_mmc_reset_work(struct work_struct *work)
359{
360 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
361 delayed_reset_work.work);
362 struct mmc_request *mrq;
363 unsigned long flags;
364
365 spin_lock_irqsave(&host->lock, flags);
366 mrq = host->mrq;
367
368 /* request already finished */
369 if (!mrq
370 || time_is_after_jiffies(host->last_req_ts +
371 msecs_to_jiffies(2000))) {
372 spin_unlock_irqrestore(&host->lock, flags);
373 return;
374 }
375
376 dev_warn(&host->pdev->dev,
377 "timeout waiting for hardware interrupt (CMD%u)\n",
378 mrq->cmd->opcode);
379
380 if (host->data)
381 host->data->error = -ETIMEDOUT;
382 else if (host->cmd)
383 host->cmd->error = -ETIMEDOUT;
384 else
385 mrq->cmd->error = -ETIMEDOUT;
386
387 host->cmd = NULL;
388 host->data = NULL;
389 host->mrq = NULL;
390
391 spin_unlock_irqrestore(&host->lock, flags);
392
393 reset(host);
394
395 mmc_request_done(host->mmc, mrq);
396}
397
Ian Molton4a489982008-07-15 16:02:21 +0100398static void
399tmio_mmc_finish_request(struct tmio_mmc_host *host)
400{
401 struct mmc_request *mrq = host->mrq;
402
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500403 if (!mrq)
404 return;
405
Ian Molton4a489982008-07-15 16:02:21 +0100406 host->mrq = NULL;
407 host->cmd = NULL;
408 host->data = NULL;
409
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500410 cancel_delayed_work(&host->delayed_reset_work);
411
Ian Molton4a489982008-07-15 16:02:21 +0100412 mmc_request_done(host->mmc, mrq);
413}
414
415/* These are the bitmasks the tmio chip requires to implement the MMC response
416 * types. Note that R1 and R6 are the same in this scheme. */
417#define APP_CMD 0x0040
418#define RESP_NONE 0x0300
419#define RESP_R1 0x0400
420#define RESP_R1B 0x0500
421#define RESP_R2 0x0600
422#define RESP_R3 0x0700
423#define DATA_PRESENT 0x0800
424#define TRANSFER_READ 0x1000
425#define TRANSFER_MULTI 0x2000
426#define SECURITY_CMD 0x4000
427
428static int
429tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
430{
Ian Molton4a489982008-07-15 16:02:21 +0100431 struct mmc_data *data = host->data;
432 int c = cmd->opcode;
433
434 /* Command 12 is handled by hardware */
435 if (cmd->opcode == 12 && !cmd->arg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200436 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
Ian Molton4a489982008-07-15 16:02:21 +0100437 return 0;
438 }
439
440 switch (mmc_resp_type(cmd)) {
441 case MMC_RSP_NONE: c |= RESP_NONE; break;
442 case MMC_RSP_R1: c |= RESP_R1; break;
443 case MMC_RSP_R1B: c |= RESP_R1B; break;
444 case MMC_RSP_R2: c |= RESP_R2; break;
445 case MMC_RSP_R3: c |= RESP_R3; break;
446 default:
447 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
448 return -EINVAL;
449 }
450
451 host->cmd = cmd;
452
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000453/* FIXME - this seems to be ok commented out but the spec suggest this bit
454 * should be set when issuing app commands.
Ian Molton4a489982008-07-15 16:02:21 +0100455 * if(cmd->flags & MMC_FLAG_ACMD)
456 * c |= APP_CMD;
457 */
458 if (data) {
459 c |= DATA_PRESENT;
460 if (data->blocks > 1) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200461 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
Ian Molton4a489982008-07-15 16:02:21 +0100462 c |= TRANSFER_MULTI;
463 }
464 if (data->flags & MMC_DATA_READ)
465 c |= TRANSFER_READ;
466 }
467
Philipp Zabel5e746722009-06-04 20:12:32 +0200468 enable_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100469
470 /* Fire off the command */
Philipp Zabel5e746722009-06-04 20:12:32 +0200471 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
472 sd_ctrl_write16(host, CTL_SD_CMD, c);
Ian Molton4a489982008-07-15 16:02:21 +0100473
474 return 0;
475}
476
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000477/*
478 * This chip always returns (at least?) as much data as you ask for.
Ian Molton4a489982008-07-15 16:02:21 +0100479 * I'm unsure what happens if you ask for less than a block. This should be
480 * looked into to ensure that a funny length read doesnt hose the controller.
Ian Molton4a489982008-07-15 16:02:21 +0100481 */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000482static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100483{
Ian Molton4a489982008-07-15 16:02:21 +0100484 struct mmc_data *data = host->data;
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700485 void *sg_virt;
Ian Molton4a489982008-07-15 16:02:21 +0100486 unsigned short *buf;
487 unsigned int count;
488 unsigned long flags;
489
490 if (!data) {
491 pr_debug("Spurious PIO IRQ\n");
492 return;
493 }
494
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700495 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
496 buf = (unsigned short *)(sg_virt + host->sg_off);
Ian Molton4a489982008-07-15 16:02:21 +0100497
498 count = host->sg_ptr->length - host->sg_off;
499 if (count > data->blksz)
500 count = data->blksz;
501
502 pr_debug("count: %08x offset: %08x flags %08x\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000503 count, host->sg_off, data->flags);
Ian Molton4a489982008-07-15 16:02:21 +0100504
505 /* Transfer the data */
506 if (data->flags & MMC_DATA_READ)
Philipp Zabel5e746722009-06-04 20:12:32 +0200507 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100508 else
Philipp Zabel5e746722009-06-04 20:12:32 +0200509 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100510
511 host->sg_off += count;
512
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700513 tmio_mmc_kunmap_atomic(sg_virt, &flags);
Ian Molton4a489982008-07-15 16:02:21 +0100514
515 if (host->sg_off == host->sg_ptr->length)
516 tmio_mmc_next_sg(host);
517
518 return;
519}
520
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500521/* needs to be called with host->lock held */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000522static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100523{
Ian Molton4a489982008-07-15 16:02:21 +0100524 struct mmc_data *data = host->data;
Julia Lawalla0d045c2008-12-16 16:13:09 +0100525 struct mmc_command *stop;
Ian Molton4a489982008-07-15 16:02:21 +0100526
527 host->data = NULL;
528
529 if (!data) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000530 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
Ian Molton4a489982008-07-15 16:02:21 +0100531 return;
532 }
Julia Lawalla0d045c2008-12-16 16:13:09 +0100533 stop = data->stop;
Ian Molton4a489982008-07-15 16:02:21 +0100534
535 /* FIXME - return correct transfer count on errors */
536 if (!data->error)
537 data->bytes_xfered = data->blocks * data->blksz;
538 else
539 data->bytes_xfered = 0;
540
541 pr_debug("Completed data request\n");
542
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000543 /*
544 * FIXME: other drivers allow an optional stop command of any given type
Ian Molton4a489982008-07-15 16:02:21 +0100545 * which we dont do, as the chip can auto generate them.
546 * Perhaps we can be smarter about when to use auto CMD12 and
547 * only issue the auto request when we know this is the desired
548 * stop command, allowing fallback to the stop command the
549 * upper layers expect. For now, we do what works.
550 */
551
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000552 if (data->flags & MMC_DATA_READ) {
553 if (!host->chan_rx)
554 disable_mmc_irqs(host, TMIO_MASK_READOP);
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100555 else
556 tmio_check_bounce_buffer(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000557 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
558 host->mrq);
559 } else {
560 if (!host->chan_tx)
561 disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
562 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
563 host->mrq);
564 }
Ian Molton4a489982008-07-15 16:02:21 +0100565
566 if (stop) {
567 if (stop->opcode == 12 && !stop->arg)
Philipp Zabel5e746722009-06-04 20:12:32 +0200568 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
Ian Molton4a489982008-07-15 16:02:21 +0100569 else
570 BUG();
571 }
572
573 tmio_mmc_finish_request(host);
574}
575
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000576static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
577{
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500578 struct mmc_data *data;
579 spin_lock(&host->lock);
580 data = host->data;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000581
582 if (!data)
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500583 goto out;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000584
585 if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) {
586 /*
587 * Has all data been written out yet? Testing on SuperH showed,
588 * that in most cases the first interrupt comes already with the
589 * BUSY status bit clear, but on some operations, like mount or
590 * in the beginning of a write / sync / umount, there is one
591 * DATAEND interrupt with the BUSY bit set, in this cases
592 * waiting for one more interrupt fixes the problem.
593 */
594 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
595 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
596 tasklet_schedule(&host->dma_complete);
597 }
598 } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) {
599 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
600 tasklet_schedule(&host->dma_complete);
601 } else {
602 tmio_mmc_do_data_irq(host);
603 }
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500604out:
605 spin_unlock(&host->lock);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000606}
607
608static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
Ian Molton4a489982008-07-15 16:02:21 +0100609 unsigned int stat)
610{
Ian Molton4a489982008-07-15 16:02:21 +0100611 struct mmc_command *cmd = host->cmd;
Philipp Zabel5e746722009-06-04 20:12:32 +0200612 int i, addr;
Ian Molton4a489982008-07-15 16:02:21 +0100613
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500614 spin_lock(&host->lock);
615
Ian Molton4a489982008-07-15 16:02:21 +0100616 if (!host->cmd) {
617 pr_debug("Spurious CMD irq\n");
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500618 goto out;
Ian Molton4a489982008-07-15 16:02:21 +0100619 }
620
621 host->cmd = NULL;
622
623 /* This controller is sicker than the PXA one. Not only do we need to
624 * drop the top 8 bits of the first response word, we also need to
625 * modify the order of the response for short response command types.
626 */
627
Philipp Zabel5e746722009-06-04 20:12:32 +0200628 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
629 cmd->resp[i] = sd_ctrl_read32(host, addr);
Ian Molton4a489982008-07-15 16:02:21 +0100630
631 if (cmd->flags & MMC_RSP_136) {
632 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
633 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
634 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
635 cmd->resp[3] <<= 8;
636 } else if (cmd->flags & MMC_RSP_R3) {
637 cmd->resp[0] = cmd->resp[3];
638 }
639
640 if (stat & TMIO_STAT_CMDTIMEOUT)
641 cmd->error = -ETIMEDOUT;
642 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
643 cmd->error = -EILSEQ;
644
645 /* If there is data to handle we enable data IRQs here, and
646 * we will ultimatley finish the request in the data_end handler.
647 * If theres no data or we encountered an error, finish now.
648 */
649 if (host->data && !cmd->error) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000650 if (host->data->flags & MMC_DATA_READ) {
651 if (!host->chan_rx)
652 enable_mmc_irqs(host, TMIO_MASK_READOP);
653 } else {
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100654 if (!host->chan_tx)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000655 enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
656 else
657 tasklet_schedule(&host->dma_issue);
658 }
Ian Molton4a489982008-07-15 16:02:21 +0100659 } else {
660 tmio_mmc_finish_request(host);
661 }
662
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500663out:
664 spin_unlock(&host->lock);
665
Ian Molton4a489982008-07-15 16:02:21 +0100666 return;
667}
668
Ian Molton4a489982008-07-15 16:02:21 +0100669static irqreturn_t tmio_mmc_irq(int irq, void *devid)
670{
671 struct tmio_mmc_host *host = devid;
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100672 struct mfd_cell *cell = host->pdev->dev.platform_data;
673 struct tmio_mmc_data *pdata = cell->driver_data;
Ian Molton4a489982008-07-15 16:02:21 +0100674 unsigned int ireg, irq_mask, status;
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100675 unsigned int sdio_ireg, sdio_irq_mask, sdio_status;
Ian Molton4a489982008-07-15 16:02:21 +0100676
677 pr_debug("MMC IRQ begin\n");
678
Philipp Zabel5e746722009-06-04 20:12:32 +0200679 status = sd_ctrl_read32(host, CTL_STATUS);
680 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100681 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
682
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100683 sdio_ireg = 0;
684 if (!ireg && pdata->flags & TMIO_MMC_SDIO_IRQ) {
685 sdio_status = sd_ctrl_read16(host, CTL_SDIO_STATUS);
686 sdio_irq_mask = sd_ctrl_read16(host, CTL_SDIO_IRQ_MASK);
687 sdio_ireg = sdio_status & TMIO_SDIO_MASK_ALL & ~sdio_irq_mask;
688
689 sd_ctrl_write16(host, CTL_SDIO_STATUS, sdio_status & ~TMIO_SDIO_MASK_ALL);
690
691 if (sdio_ireg && !host->sdio_irq_enabled) {
692 pr_warning("tmio_mmc: Spurious SDIO IRQ, disabling! 0x%04x 0x%04x 0x%04x\n",
693 sdio_status, sdio_irq_mask, sdio_ireg);
694 tmio_mmc_enable_sdio_irq(host->mmc, 0);
695 goto out;
696 }
697
698 if (host->mmc->caps & MMC_CAP_SDIO_IRQ &&
699 sdio_ireg & TMIO_SDIO_STAT_IOIRQ)
700 mmc_signal_sdio_irq(host->mmc);
701
702 if (sdio_ireg)
703 goto out;
704 }
705
Ian Molton4a489982008-07-15 16:02:21 +0100706 pr_debug_status(status);
707 pr_debug_status(ireg);
708
709 if (!ireg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200710 disable_mmc_irqs(host, status & ~irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +0100711
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000712 pr_warning("tmio_mmc: Spurious irq, disabling! "
Ian Molton4a489982008-07-15 16:02:21 +0100713 "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
714 pr_debug_status(status);
715
716 goto out;
717 }
718
719 while (ireg) {
720 /* Card insert / remove attempts */
721 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200722 ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
Ian Molton4a489982008-07-15 16:02:21 +0100723 TMIO_STAT_CARD_REMOVE);
Magnus Damm6d9af5a2010-02-17 16:38:04 +0900724 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
Ian Molton4a489982008-07-15 16:02:21 +0100725 }
726
727 /* CRC and other errors */
728/* if (ireg & TMIO_STAT_ERR_IRQ)
729 * handled |= tmio_error_irq(host, irq, stat);
730 */
731
732 /* Command completion */
Arnd Hannemann2bd6a932010-12-29 14:21:14 +0100733 if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
734 ack_mmc_irqs(host,
735 TMIO_STAT_CMDRESPEND |
736 TMIO_STAT_CMDTIMEOUT);
Ian Molton4a489982008-07-15 16:02:21 +0100737 tmio_mmc_cmd_irq(host, status);
738 }
739
740 /* Data transfer */
741 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200742 ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
Ian Molton4a489982008-07-15 16:02:21 +0100743 tmio_mmc_pio_irq(host);
744 }
745
746 /* Data transfer completion */
747 if (ireg & TMIO_STAT_DATAEND) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200748 ack_mmc_irqs(host, TMIO_STAT_DATAEND);
Ian Molton4a489982008-07-15 16:02:21 +0100749 tmio_mmc_data_irq(host);
750 }
751
752 /* Check status - keep going until we've handled it all */
Philipp Zabel5e746722009-06-04 20:12:32 +0200753 status = sd_ctrl_read32(host, CTL_STATUS);
754 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100755 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
756
757 pr_debug("Status at end of loop: %08x\n", status);
758 pr_debug_status(status);
759 }
760 pr_debug("MMC IRQ end\n");
761
762out:
763 return IRQ_HANDLED;
764}
765
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000766#ifdef CONFIG_TMIO_MMC_DMA
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100767static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
768{
769 if (host->sg_ptr == &host->bounce_sg) {
770 unsigned long flags;
771 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
772 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
773 tmio_mmc_kunmap_atomic(sg_vaddr, &flags);
774 }
775}
776
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000777static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
778{
779#if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
780 /* Switch DMA mode on or off - SuperH specific? */
781 sd_ctrl_write16(host, 0xd8, enable ? 2 : 0);
782#endif
783}
784
785static void tmio_dma_complete(void *arg)
786{
787 struct tmio_mmc_host *host = arg;
788
789 dev_dbg(&host->pdev->dev, "Command completed\n");
790
791 if (!host->data)
792 dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n");
793 else
794 enable_mmc_irqs(host, TMIO_STAT_DATAEND);
795}
796
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100797static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000798{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100799 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000800 struct dma_async_tx_descriptor *desc = NULL;
801 struct dma_chan *chan = host->chan_rx;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100802 struct mfd_cell *cell = host->pdev->dev.platform_data;
803 struct tmio_mmc_data *pdata = cell->driver_data;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100804 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100805 int ret, i;
806 bool aligned = true, multiple = true;
807 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
808
809 for_each_sg(sg, sg_tmp, host->sg_len, i) {
810 if (sg_tmp->offset & align)
811 aligned = false;
812 if (sg_tmp->length & align) {
813 multiple = false;
814 break;
815 }
816 }
817
818 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000819 align >= MAX_ALIGN)) || !multiple) {
820 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100821 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000822 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100823
824 /* The only sg element can be unaligned, use our bounce buffer then */
825 if (!aligned) {
826 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
827 host->sg_ptr = &host->bounce_sg;
828 sg = host->sg_ptr;
829 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000830
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100831 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
Linus Walleij33834332011-02-10 16:10:56 +0100832 if (ret > 0)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000833 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
834 DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000835
836 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000837 desc->callback = tmio_dma_complete;
838 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100839 cookie = desc->tx_submit(desc);
840 if (cookie < 0) {
841 desc = NULL;
842 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000843 } else {
844 chan->device->device_issue_pending(chan);
845 }
846 }
847 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100848 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000849
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100850pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100851 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000852 /* DMA failed, fall back to PIO */
853 if (ret >= 0)
854 ret = -EIO;
855 host->chan_rx = NULL;
856 dma_release_channel(chan);
857 /* Free the Tx channel too */
858 chan = host->chan_tx;
859 if (chan) {
860 host->chan_tx = NULL;
861 dma_release_channel(chan);
862 }
863 dev_warn(&host->pdev->dev,
864 "DMA failed: %d, falling back to PIO\n", ret);
865 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000866 }
867
868 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100869 desc, cookie, host->sg_len);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000870}
871
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100872static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000873{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100874 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000875 struct dma_async_tx_descriptor *desc = NULL;
876 struct dma_chan *chan = host->chan_tx;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100877 struct mfd_cell *cell = host->pdev->dev.platform_data;
878 struct tmio_mmc_data *pdata = cell->driver_data;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100879 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100880 int ret, i;
881 bool aligned = true, multiple = true;
882 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
883
884 for_each_sg(sg, sg_tmp, host->sg_len, i) {
885 if (sg_tmp->offset & align)
886 aligned = false;
887 if (sg_tmp->length & align) {
888 multiple = false;
889 break;
890 }
891 }
892
893 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000894 align >= MAX_ALIGN)) || !multiple) {
895 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100896 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000897 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100898
899 /* The only sg element can be unaligned, use our bounce buffer then */
900 if (!aligned) {
901 unsigned long flags;
902 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
903 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
904 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
905 tmio_mmc_kunmap_atomic(sg_vaddr, &flags);
906 host->sg_ptr = &host->bounce_sg;
907 sg = host->sg_ptr;
908 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000909
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100910 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
Linus Walleij33834332011-02-10 16:10:56 +0100911 if (ret > 0)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000912 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
913 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000914
915 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000916 desc->callback = tmio_dma_complete;
917 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100918 cookie = desc->tx_submit(desc);
919 if (cookie < 0) {
920 desc = NULL;
921 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000922 }
923 }
924 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100925 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000926
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100927pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100928 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000929 /* DMA failed, fall back to PIO */
930 if (ret >= 0)
931 ret = -EIO;
932 host->chan_tx = NULL;
933 dma_release_channel(chan);
934 /* Free the Rx channel too */
935 chan = host->chan_rx;
936 if (chan) {
937 host->chan_rx = NULL;
938 dma_release_channel(chan);
939 }
940 dev_warn(&host->pdev->dev,
941 "DMA failed: %d, falling back to PIO\n", ret);
942 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000943 }
944
945 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100946 desc, cookie);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000947}
948
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100949static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000950 struct mmc_data *data)
951{
952 if (data->flags & MMC_DATA_READ) {
953 if (host->chan_rx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100954 tmio_mmc_start_dma_rx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000955 } else {
956 if (host->chan_tx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100957 tmio_mmc_start_dma_tx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000958 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000959}
960
961static void tmio_issue_tasklet_fn(unsigned long priv)
962{
963 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
964 struct dma_chan *chan = host->chan_tx;
965
966 chan->device->device_issue_pending(chan);
967}
968
969static void tmio_tasklet_fn(unsigned long arg)
970{
971 struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500972 unsigned long flags;
973
974 spin_lock_irqsave(&host->lock, flags);
975
976 if (!host->data)
977 goto out;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000978
979 if (host->data->flags & MMC_DATA_READ)
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100980 dma_unmap_sg(host->chan_rx->device->dev,
Linus Walleijd7554ca2011-02-10 16:10:47 +0100981 host->sg_ptr, host->sg_len,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000982 DMA_FROM_DEVICE);
983 else
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100984 dma_unmap_sg(host->chan_tx->device->dev,
Linus Walleijd7554ca2011-02-10 16:10:47 +0100985 host->sg_ptr, host->sg_len,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000986 DMA_TO_DEVICE);
987
988 tmio_mmc_do_data_irq(host);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500989out:
990 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000991}
992
993/* It might be necessary to make filter MFD specific */
994static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
995{
996 dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
997 chan->private = arg;
998 return true;
999}
1000
1001static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
1002 struct tmio_mmc_data *pdata)
1003{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001004 /* We can only either use DMA for both Tx and Rx or not use it at all */
1005 if (pdata->dma) {
1006 dma_cap_mask_t mask;
1007
1008 dma_cap_zero(mask);
1009 dma_cap_set(DMA_SLAVE, mask);
1010
1011 host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
1012 pdata->dma->chan_priv_tx);
1013 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
1014 host->chan_tx);
1015
1016 if (!host->chan_tx)
1017 return;
1018
1019 host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
1020 pdata->dma->chan_priv_rx);
1021 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
1022 host->chan_rx);
1023
1024 if (!host->chan_rx) {
1025 dma_release_channel(host->chan_tx);
1026 host->chan_tx = NULL;
1027 return;
1028 }
1029
1030 tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host);
1031 tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host);
1032
1033 tmio_mmc_enable_dma(host, true);
1034 }
1035}
1036
1037static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
1038{
1039 if (host->chan_tx) {
1040 struct dma_chan *chan = host->chan_tx;
1041 host->chan_tx = NULL;
1042 dma_release_channel(chan);
1043 }
1044 if (host->chan_rx) {
1045 struct dma_chan *chan = host->chan_rx;
1046 host->chan_rx = NULL;
1047 dma_release_channel(chan);
1048 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001049}
1050#else
Guennadi Liakhovetski93173052010-12-22 12:02:15 +01001051static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
1052{
1053}
1054
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +01001055static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001056 struct mmc_data *data)
1057{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001058}
1059
1060static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
1061 struct tmio_mmc_data *pdata)
1062{
1063 host->chan_tx = NULL;
1064 host->chan_rx = NULL;
1065}
1066
1067static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
1068{
1069}
1070#endif
1071
Ian Molton4a489982008-07-15 16:02:21 +01001072static int tmio_mmc_start_data(struct tmio_mmc_host *host,
1073 struct mmc_data *data)
1074{
Yusuke Godaf1334fb2010-08-30 11:50:19 +01001075 struct mfd_cell *cell = host->pdev->dev.platform_data;
1076 struct tmio_mmc_data *pdata = cell->driver_data;
1077
Ian Molton4a489982008-07-15 16:02:21 +01001078 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001079 data->blksz, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +01001080
Yusuke Godaf1334fb2010-08-30 11:50:19 +01001081 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
1082 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1083 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
1084
1085 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
1086 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
1087 mmc_hostname(host->mmc), data->blksz);
1088 return -EINVAL;
1089 }
Ian Molton4a489982008-07-15 16:02:21 +01001090 }
1091
1092 tmio_mmc_init_sg(host, data);
1093 host->data = data;
1094
1095 /* Set transfer length / blocksize */
Philipp Zabel5e746722009-06-04 20:12:32 +02001096 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
1097 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +01001098
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +01001099 tmio_mmc_start_dma(host, data);
1100
1101 return 0;
Ian Molton4a489982008-07-15 16:02:21 +01001102}
1103
1104/* Process requests from the MMC layer */
1105static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
1106{
1107 struct tmio_mmc_host *host = mmc_priv(mmc);
1108 int ret;
1109
1110 if (host->mrq)
1111 pr_debug("request not null\n");
1112
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001113 host->last_req_ts = jiffies;
1114 wmb();
Ian Molton4a489982008-07-15 16:02:21 +01001115 host->mrq = mrq;
1116
1117 if (mrq->data) {
1118 ret = tmio_mmc_start_data(host, mrq->data);
1119 if (ret)
1120 goto fail;
1121 }
1122
1123 ret = tmio_mmc_start_command(host, mrq->cmd);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001124 if (!ret) {
1125 schedule_delayed_work(&host->delayed_reset_work,
1126 msecs_to_jiffies(2000));
Ian Molton4a489982008-07-15 16:02:21 +01001127 return;
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001128 }
Ian Molton4a489982008-07-15 16:02:21 +01001129
1130fail:
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001131 host->mrq = NULL;
Ian Molton4a489982008-07-15 16:02:21 +01001132 mrq->cmd->error = ret;
1133 mmc_request_done(mmc, mrq);
1134}
1135
1136/* Set MMC clock / power.
1137 * Note: This controller uses a simple divider scheme therefore it cannot
1138 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
1139 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
1140 * slowest setting.
1141 */
1142static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1143{
1144 struct tmio_mmc_host *host = mmc_priv(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001145
1146 if (ios->clock)
1147 tmio_mmc_set_clock(host, ios->clock);
1148
1149 /* Power sequence - OFF -> ON -> UP */
1150 switch (ios->power_mode) {
1151 case MMC_POWER_OFF: /* power down SD bus */
Ian Molton64e88672010-01-06 13:51:48 +01001152 if (host->set_pwr)
1153 host->set_pwr(host->pdev, 0);
Ian Molton4a489982008-07-15 16:02:21 +01001154 tmio_mmc_clk_stop(host);
1155 break;
1156 case MMC_POWER_ON: /* power up SD bus */
Ian Molton64e88672010-01-06 13:51:48 +01001157 if (host->set_pwr)
1158 host->set_pwr(host->pdev, 1);
Ian Molton4a489982008-07-15 16:02:21 +01001159 break;
1160 case MMC_POWER_UP: /* start bus clock */
1161 tmio_mmc_clk_start(host);
1162 break;
1163 }
1164
1165 switch (ios->bus_width) {
1166 case MMC_BUS_WIDTH_1:
Philipp Zabel5e746722009-06-04 20:12:32 +02001167 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
Ian Molton4a489982008-07-15 16:02:21 +01001168 break;
1169 case MMC_BUS_WIDTH_4:
Philipp Zabel5e746722009-06-04 20:12:32 +02001170 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
Ian Molton4a489982008-07-15 16:02:21 +01001171 break;
1172 }
1173
1174 /* Let things settle. delay taken from winCE driver */
1175 udelay(140);
1176}
1177
1178static int tmio_mmc_get_ro(struct mmc_host *mmc)
1179{
1180 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +00001181 struct mfd_cell *cell = host->pdev->dev.platform_data;
1182 struct tmio_mmc_data *pdata = cell->driver_data;
Ian Molton4a489982008-07-15 16:02:21 +01001183
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +00001184 return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
1185 (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1;
Ian Molton4a489982008-07-15 16:02:21 +01001186}
1187
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001188static int tmio_mmc_get_cd(struct mmc_host *mmc)
1189{
1190 struct tmio_mmc_host *host = mmc_priv(mmc);
1191 struct mfd_cell *cell = host->pdev->dev.platform_data;
1192 struct tmio_mmc_data *pdata = cell->driver_data;
1193
1194 if (!pdata->get_cd)
1195 return -ENOSYS;
1196 else
1197 return pdata->get_cd(host->pdev);
1198}
1199
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001200static const struct mmc_host_ops tmio_mmc_ops = {
Ian Molton4a489982008-07-15 16:02:21 +01001201 .request = tmio_mmc_request,
1202 .set_ios = tmio_mmc_set_ios,
1203 .get_ro = tmio_mmc_get_ro,
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001204 .get_cd = tmio_mmc_get_cd,
Arnd Hannemann845ecd22010-12-28 23:22:31 +01001205 .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
Ian Molton4a489982008-07-15 16:02:21 +01001206};
1207
1208#ifdef CONFIG_PM
1209static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
1210{
1211 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
1212 struct mmc_host *mmc = platform_get_drvdata(dev);
1213 int ret;
1214
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001215 ret = mmc_suspend_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001216
1217 /* Tell MFD core it can disable us now.*/
1218 if (!ret && cell->disable)
1219 cell->disable(dev);
1220
1221 return ret;
1222}
1223
1224static int tmio_mmc_resume(struct platform_device *dev)
1225{
1226 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
1227 struct mmc_host *mmc = platform_get_drvdata(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001228 int ret = 0;
1229
Ian Molton4a489982008-07-15 16:02:21 +01001230 /* Tell the MFD core we are ready to be enabled */
Ian Molton64e88672010-01-06 13:51:48 +01001231 if (cell->resume) {
1232 ret = cell->resume(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001233 if (ret)
1234 goto out;
1235 }
1236
1237 mmc_resume_host(mmc);
1238
1239out:
1240 return ret;
1241}
1242#else
1243#define tmio_mmc_suspend NULL
1244#define tmio_mmc_resume NULL
1245#endif
1246
1247static int __devinit tmio_mmc_probe(struct platform_device *dev)
1248{
1249 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001250 struct tmio_mmc_data *pdata;
Ian Molton64e88672010-01-06 13:51:48 +01001251 struct resource *res_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001252 struct tmio_mmc_host *host;
1253 struct mmc_host *mmc;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001254 int ret = -EINVAL;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001255 u32 irq_mask = TMIO_MASK_CMD;
Ian Molton4a489982008-07-15 16:02:21 +01001256
Ian Molton64e88672010-01-06 13:51:48 +01001257 if (dev->num_resources != 2)
Ian Molton4a489982008-07-15 16:02:21 +01001258 goto out;
1259
1260 res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
Ian Molton64e88672010-01-06 13:51:48 +01001261 if (!res_ctl)
Ian Molton4a489982008-07-15 16:02:21 +01001262 goto out;
Ian Molton4a489982008-07-15 16:02:21 +01001263
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001264 pdata = cell->driver_data;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001265 if (!pdata || !pdata->hclk)
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001266 goto out;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001267
1268 ret = -ENOMEM;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001269
Ian Molton4a489982008-07-15 16:02:21 +01001270 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
1271 if (!mmc)
1272 goto out;
1273
1274 host = mmc_priv(mmc);
1275 host->mmc = mmc;
Ian Molton64e88672010-01-06 13:51:48 +01001276 host->pdev = dev;
Ian Molton4a489982008-07-15 16:02:21 +01001277 platform_set_drvdata(dev, mmc);
1278
Ian Molton64e88672010-01-06 13:51:48 +01001279 host->set_pwr = pdata->set_pwr;
1280 host->set_clk_div = pdata->set_clk_div;
1281
Philipp Zabel5e746722009-06-04 20:12:32 +02001282 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
1283 host->bus_shift = resource_size(res_ctl) >> 10;
1284
Magnus Dammbc6772a2009-03-11 21:58:54 +09001285 host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
Ian Molton4a489982008-07-15 16:02:21 +01001286 if (!host->ctl)
1287 goto host_free;
1288
Ian Molton4a489982008-07-15 16:02:21 +01001289 mmc->ops = &tmio_mmc_ops;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001290 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001291 mmc->f_max = pdata->hclk;
1292 mmc->f_min = mmc->f_max / 512;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001293 mmc->max_segs = 32;
1294 mmc->max_blk_size = 512;
1295 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
1296 mmc->max_segs;
1297 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1298 mmc->max_seg_size = mmc->max_req_size;
Guennadi Liakhovetskia2b14dc2010-05-19 18:37:25 +00001299 if (pdata->ocr_mask)
1300 mmc->ocr_avail = pdata->ocr_mask;
1301 else
1302 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Ian Molton4a489982008-07-15 16:02:21 +01001303
Ian Molton4a489982008-07-15 16:02:21 +01001304 /* Tell the MFD core we are ready to be enabled */
1305 if (cell->enable) {
1306 ret = cell->enable(dev);
1307 if (ret)
Ian Molton64e88672010-01-06 13:51:48 +01001308 goto unmap_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001309 }
1310
Ian Molton4a489982008-07-15 16:02:21 +01001311 tmio_mmc_clk_stop(host);
1312 reset(host);
1313
1314 ret = platform_get_irq(dev, 0);
1315 if (ret >= 0)
1316 host->irq = ret;
1317 else
Magnus Damm7ee422d2010-02-17 16:38:23 +09001318 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001319
Philipp Zabel5e746722009-06-04 20:12:32 +02001320 disable_mmc_irqs(host, TMIO_MASK_ALL);
Arnd Hannemann845ecd22010-12-28 23:22:31 +01001321 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
1322 tmio_mmc_enable_sdio_irq(mmc, 0);
Ian Molton4a489982008-07-15 16:02:21 +01001323
Philipp Zabel6c413cc2009-06-04 20:12:33 +02001324 ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
Magnus Damm14f1b752009-12-14 18:01:33 -08001325 IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
Ian Molton4a489982008-07-15 16:02:21 +01001326 if (ret)
Magnus Damm7ee422d2010-02-17 16:38:23 +09001327 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001328
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001329 spin_lock_init(&host->lock);
1330
1331 /* Init delayed work for request timeouts */
1332 INIT_DELAYED_WORK(&host->delayed_reset_work, tmio_mmc_reset_work);
1333
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001334 /* See if we also get DMA */
1335 tmio_mmc_request_dma(host, pdata);
1336
Ian Molton4a489982008-07-15 16:02:21 +01001337 mmc_add_host(mmc);
1338
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001339 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
1340 (unsigned long)host->ctl, host->irq);
Ian Molton4a489982008-07-15 16:02:21 +01001341
1342 /* Unmask the IRQs we want to know about */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001343 if (!host->chan_rx)
1344 irq_mask |= TMIO_MASK_READOP;
1345 if (!host->chan_tx)
1346 irq_mask |= TMIO_MASK_WRITEOP;
1347 enable_mmc_irqs(host, irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +01001348
1349 return 0;
1350
Magnus Damm7ee422d2010-02-17 16:38:23 +09001351cell_disable:
1352 if (cell->disable)
1353 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001354unmap_ctl:
1355 iounmap(host->ctl);
1356host_free:
1357 mmc_free_host(mmc);
1358out:
1359 return ret;
1360}
1361
1362static int __devexit tmio_mmc_remove(struct platform_device *dev)
1363{
Magnus Damm7ee422d2010-02-17 16:38:23 +09001364 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Ian Molton4a489982008-07-15 16:02:21 +01001365 struct mmc_host *mmc = platform_get_drvdata(dev);
1366
1367 platform_set_drvdata(dev, NULL);
1368
1369 if (mmc) {
1370 struct tmio_mmc_host *host = mmc_priv(mmc);
1371 mmc_remove_host(mmc);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001372 cancel_delayed_work_sync(&host->delayed_reset_work);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001373 tmio_mmc_release_dma(host);
Ian Molton4a489982008-07-15 16:02:21 +01001374 free_irq(host->irq, host);
Magnus Damm7ee422d2010-02-17 16:38:23 +09001375 if (cell->disable)
1376 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001377 iounmap(host->ctl);
Magnus Dammbedcc452009-03-11 21:59:03 +09001378 mmc_free_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001379 }
1380
1381 return 0;
1382}
1383
1384/* ------------------- device registration ----------------------- */
1385
1386static struct platform_driver tmio_mmc_driver = {
1387 .driver = {
1388 .name = "tmio-mmc",
1389 .owner = THIS_MODULE,
1390 },
1391 .probe = tmio_mmc_probe,
1392 .remove = __devexit_p(tmio_mmc_remove),
1393 .suspend = tmio_mmc_suspend,
1394 .resume = tmio_mmc_resume,
1395};
1396
1397
1398static int __init tmio_mmc_init(void)
1399{
1400 return platform_driver_register(&tmio_mmc_driver);
1401}
1402
1403static void __exit tmio_mmc_exit(void)
1404{
1405 platform_driver_unregister(&tmio_mmc_driver);
1406}
1407
1408module_init(tmio_mmc_init);
1409module_exit(tmio_mmc_exit);
1410
1411MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
1412MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
1413MODULE_LICENSE("GPL v2");
1414MODULE_ALIAS("platform:tmio-mmc");