blob: ab1adeabdd224f43e78fec6cbe01dfb1c13f4f45 [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
Guennadi Liakhovetski860cfe72011-03-11 08:30:14 +0100222static void tmio_mmc_kunmap_atomic(struct scatterlist *sg, unsigned long *flags, void *virt)
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100223{
Guennadi Liakhovetski860cfe72011-03-11 08:30:14 +0100224 kunmap_atomic(virt - sg->offset, KM_BIO_SRC_IRQ);
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100225 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{
Andres Salomon4f95bf42011-02-17 19:07:29 -0800306 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100307
308 /*
309 * Testing on sh-mobile showed that SDIO IRQs are unmasked when
310 * CTL_CLK_AND_WAIT_CTL gets written, so we have to disable the
311 * device IRQ here and restore the SDIO IRQ mask before
312 * re-enabling the device IRQ.
313 */
314 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
315 disable_irq(host->irq);
Philipp Zabel5e746722009-06-04 20:12:32 +0200316 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100317 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100318 if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
319 tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
320 enable_irq(host->irq);
321 }
Philipp Zabel5e746722009-06-04 20:12:32 +0200322 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
323 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100324 msleep(10);
325}
326
327static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
328{
Andres Salomon4f95bf42011-02-17 19:07:29 -0800329 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100330
Philipp Zabel5e746722009-06-04 20:12:32 +0200331 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
332 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100333 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100334 /* see comment in tmio_mmc_clk_stop above */
335 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
336 disable_irq(host->irq);
Philipp Zabel5e746722009-06-04 20:12:32 +0200337 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
Ian Molton4a489982008-07-15 16:02:21 +0100338 msleep(10);
Arnd Hannemann845ecd22010-12-28 23:22:31 +0100339 if (pdata->flags & TMIO_MMC_SDIO_IRQ) {
340 tmio_mmc_enable_sdio_irq(host->mmc, host->sdio_irq_enabled);
341 enable_irq(host->irq);
342 }
Ian Molton4a489982008-07-15 16:02:21 +0100343}
344
345static void reset(struct tmio_mmc_host *host)
346{
Ian Molton4a489982008-07-15 16:02:21 +0100347 /* FIXME - should we set stop clock reg here */
Philipp Zabel5e746722009-06-04 20:12:32 +0200348 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
349 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100350 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200351 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
352 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Ian Molton4a489982008-07-15 16:02:21 +0100353 msleep(10);
354}
355
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500356static void tmio_mmc_reset_work(struct work_struct *work)
357{
358 struct tmio_mmc_host *host = container_of(work, struct tmio_mmc_host,
359 delayed_reset_work.work);
360 struct mmc_request *mrq;
361 unsigned long flags;
362
363 spin_lock_irqsave(&host->lock, flags);
364 mrq = host->mrq;
365
366 /* request already finished */
367 if (!mrq
368 || time_is_after_jiffies(host->last_req_ts +
369 msecs_to_jiffies(2000))) {
370 spin_unlock_irqrestore(&host->lock, flags);
371 return;
372 }
373
374 dev_warn(&host->pdev->dev,
375 "timeout waiting for hardware interrupt (CMD%u)\n",
376 mrq->cmd->opcode);
377
378 if (host->data)
379 host->data->error = -ETIMEDOUT;
380 else if (host->cmd)
381 host->cmd->error = -ETIMEDOUT;
382 else
383 mrq->cmd->error = -ETIMEDOUT;
384
385 host->cmd = NULL;
386 host->data = NULL;
387 host->mrq = NULL;
388
389 spin_unlock_irqrestore(&host->lock, flags);
390
391 reset(host);
392
393 mmc_request_done(host->mmc, mrq);
394}
395
Ian Molton4a489982008-07-15 16:02:21 +0100396static void
397tmio_mmc_finish_request(struct tmio_mmc_host *host)
398{
399 struct mmc_request *mrq = host->mrq;
400
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500401 if (!mrq)
402 return;
403
Ian Molton4a489982008-07-15 16:02:21 +0100404 host->mrq = NULL;
405 host->cmd = NULL;
406 host->data = NULL;
407
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500408 cancel_delayed_work(&host->delayed_reset_work);
409
Ian Molton4a489982008-07-15 16:02:21 +0100410 mmc_request_done(host->mmc, mrq);
411}
412
413/* These are the bitmasks the tmio chip requires to implement the MMC response
414 * types. Note that R1 and R6 are the same in this scheme. */
415#define APP_CMD 0x0040
416#define RESP_NONE 0x0300
417#define RESP_R1 0x0400
418#define RESP_R1B 0x0500
419#define RESP_R2 0x0600
420#define RESP_R3 0x0700
421#define DATA_PRESENT 0x0800
422#define TRANSFER_READ 0x1000
423#define TRANSFER_MULTI 0x2000
424#define SECURITY_CMD 0x4000
425
426static int
427tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
428{
Ian Molton4a489982008-07-15 16:02:21 +0100429 struct mmc_data *data = host->data;
430 int c = cmd->opcode;
431
432 /* Command 12 is handled by hardware */
433 if (cmd->opcode == 12 && !cmd->arg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200434 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
Ian Molton4a489982008-07-15 16:02:21 +0100435 return 0;
436 }
437
438 switch (mmc_resp_type(cmd)) {
439 case MMC_RSP_NONE: c |= RESP_NONE; break;
440 case MMC_RSP_R1: c |= RESP_R1; break;
441 case MMC_RSP_R1B: c |= RESP_R1B; break;
442 case MMC_RSP_R2: c |= RESP_R2; break;
443 case MMC_RSP_R3: c |= RESP_R3; break;
444 default:
445 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
446 return -EINVAL;
447 }
448
449 host->cmd = cmd;
450
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000451/* FIXME - this seems to be ok commented out but the spec suggest this bit
452 * should be set when issuing app commands.
Ian Molton4a489982008-07-15 16:02:21 +0100453 * if(cmd->flags & MMC_FLAG_ACMD)
454 * c |= APP_CMD;
455 */
456 if (data) {
457 c |= DATA_PRESENT;
458 if (data->blocks > 1) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200459 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
Ian Molton4a489982008-07-15 16:02:21 +0100460 c |= TRANSFER_MULTI;
461 }
462 if (data->flags & MMC_DATA_READ)
463 c |= TRANSFER_READ;
464 }
465
Philipp Zabel5e746722009-06-04 20:12:32 +0200466 enable_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100467
468 /* Fire off the command */
Philipp Zabel5e746722009-06-04 20:12:32 +0200469 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
470 sd_ctrl_write16(host, CTL_SD_CMD, c);
Ian Molton4a489982008-07-15 16:02:21 +0100471
472 return 0;
473}
474
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000475/*
476 * This chip always returns (at least?) as much data as you ask for.
Ian Molton4a489982008-07-15 16:02:21 +0100477 * I'm unsure what happens if you ask for less than a block. This should be
478 * looked into to ensure that a funny length read doesnt hose the controller.
Ian Molton4a489982008-07-15 16:02:21 +0100479 */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000480static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100481{
Ian Molton4a489982008-07-15 16:02:21 +0100482 struct mmc_data *data = host->data;
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700483 void *sg_virt;
Ian Molton4a489982008-07-15 16:02:21 +0100484 unsigned short *buf;
485 unsigned int count;
486 unsigned long flags;
487
488 if (!data) {
489 pr_debug("Spurious PIO IRQ\n");
490 return;
491 }
492
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700493 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
494 buf = (unsigned short *)(sg_virt + host->sg_off);
Ian Molton4a489982008-07-15 16:02:21 +0100495
496 count = host->sg_ptr->length - host->sg_off;
497 if (count > data->blksz)
498 count = data->blksz;
499
500 pr_debug("count: %08x offset: %08x flags %08x\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000501 count, host->sg_off, data->flags);
Ian Molton4a489982008-07-15 16:02:21 +0100502
503 /* Transfer the data */
504 if (data->flags & MMC_DATA_READ)
Philipp Zabel5e746722009-06-04 20:12:32 +0200505 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100506 else
Philipp Zabel5e746722009-06-04 20:12:32 +0200507 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100508
509 host->sg_off += count;
510
Guennadi Liakhovetski860cfe72011-03-11 08:30:14 +0100511 tmio_mmc_kunmap_atomic(host->sg_ptr, &flags, sg_virt);
Ian Molton4a489982008-07-15 16:02:21 +0100512
513 if (host->sg_off == host->sg_ptr->length)
514 tmio_mmc_next_sg(host);
515
516 return;
517}
518
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500519/* needs to be called with host->lock held */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000520static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100521{
Ian Molton4a489982008-07-15 16:02:21 +0100522 struct mmc_data *data = host->data;
Julia Lawalla0d045c2008-12-16 16:13:09 +0100523 struct mmc_command *stop;
Ian Molton4a489982008-07-15 16:02:21 +0100524
525 host->data = NULL;
526
527 if (!data) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000528 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
Ian Molton4a489982008-07-15 16:02:21 +0100529 return;
530 }
Julia Lawalla0d045c2008-12-16 16:13:09 +0100531 stop = data->stop;
Ian Molton4a489982008-07-15 16:02:21 +0100532
533 /* FIXME - return correct transfer count on errors */
534 if (!data->error)
535 data->bytes_xfered = data->blocks * data->blksz;
536 else
537 data->bytes_xfered = 0;
538
539 pr_debug("Completed data request\n");
540
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000541 /*
542 * FIXME: other drivers allow an optional stop command of any given type
Ian Molton4a489982008-07-15 16:02:21 +0100543 * which we dont do, as the chip can auto generate them.
544 * Perhaps we can be smarter about when to use auto CMD12 and
545 * only issue the auto request when we know this is the desired
546 * stop command, allowing fallback to the stop command the
547 * upper layers expect. For now, we do what works.
548 */
549
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000550 if (data->flags & MMC_DATA_READ) {
551 if (!host->chan_rx)
552 disable_mmc_irqs(host, TMIO_MASK_READOP);
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100553 else
554 tmio_check_bounce_buffer(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000555 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
556 host->mrq);
557 } else {
558 if (!host->chan_tx)
559 disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
560 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
561 host->mrq);
562 }
Ian Molton4a489982008-07-15 16:02:21 +0100563
564 if (stop) {
565 if (stop->opcode == 12 && !stop->arg)
Philipp Zabel5e746722009-06-04 20:12:32 +0200566 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
Ian Molton4a489982008-07-15 16:02:21 +0100567 else
568 BUG();
569 }
570
571 tmio_mmc_finish_request(host);
572}
573
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000574static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
575{
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500576 struct mmc_data *data;
577 spin_lock(&host->lock);
578 data = host->data;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000579
580 if (!data)
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500581 goto out;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000582
583 if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) {
584 /*
585 * Has all data been written out yet? Testing on SuperH showed,
586 * that in most cases the first interrupt comes already with the
587 * BUSY status bit clear, but on some operations, like mount or
588 * in the beginning of a write / sync / umount, there is one
589 * DATAEND interrupt with the BUSY bit set, in this cases
590 * waiting for one more interrupt fixes the problem.
591 */
592 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
593 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
594 tasklet_schedule(&host->dma_complete);
595 }
596 } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) {
597 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
598 tasklet_schedule(&host->dma_complete);
599 } else {
600 tmio_mmc_do_data_irq(host);
601 }
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500602out:
603 spin_unlock(&host->lock);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000604}
605
606static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
Ian Molton4a489982008-07-15 16:02:21 +0100607 unsigned int stat)
608{
Ian Molton4a489982008-07-15 16:02:21 +0100609 struct mmc_command *cmd = host->cmd;
Philipp Zabel5e746722009-06-04 20:12:32 +0200610 int i, addr;
Ian Molton4a489982008-07-15 16:02:21 +0100611
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500612 spin_lock(&host->lock);
613
Ian Molton4a489982008-07-15 16:02:21 +0100614 if (!host->cmd) {
615 pr_debug("Spurious CMD irq\n");
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500616 goto out;
Ian Molton4a489982008-07-15 16:02:21 +0100617 }
618
619 host->cmd = NULL;
620
621 /* This controller is sicker than the PXA one. Not only do we need to
622 * drop the top 8 bits of the first response word, we also need to
623 * modify the order of the response for short response command types.
624 */
625
Philipp Zabel5e746722009-06-04 20:12:32 +0200626 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
627 cmd->resp[i] = sd_ctrl_read32(host, addr);
Ian Molton4a489982008-07-15 16:02:21 +0100628
629 if (cmd->flags & MMC_RSP_136) {
630 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
631 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
632 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
633 cmd->resp[3] <<= 8;
634 } else if (cmd->flags & MMC_RSP_R3) {
635 cmd->resp[0] = cmd->resp[3];
636 }
637
638 if (stat & TMIO_STAT_CMDTIMEOUT)
639 cmd->error = -ETIMEDOUT;
640 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
641 cmd->error = -EILSEQ;
642
643 /* If there is data to handle we enable data IRQs here, and
644 * we will ultimatley finish the request in the data_end handler.
645 * If theres no data or we encountered an error, finish now.
646 */
647 if (host->data && !cmd->error) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000648 if (host->data->flags & MMC_DATA_READ) {
649 if (!host->chan_rx)
650 enable_mmc_irqs(host, TMIO_MASK_READOP);
651 } else {
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100652 if (!host->chan_tx)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000653 enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
654 else
655 tasklet_schedule(&host->dma_issue);
656 }
Ian Molton4a489982008-07-15 16:02:21 +0100657 } else {
658 tmio_mmc_finish_request(host);
659 }
660
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500661out:
662 spin_unlock(&host->lock);
663
Ian Molton4a489982008-07-15 16:02:21 +0100664 return;
665}
666
Ian Molton4a489982008-07-15 16:02:21 +0100667static irqreturn_t tmio_mmc_irq(int irq, void *devid)
668{
669 struct tmio_mmc_host *host = devid;
Andres Salomon4f95bf42011-02-17 19:07:29 -0800670 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
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 */
Arnd Hannemann2bd6a932010-12-29 14:21:14 +0100730 if (ireg & (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT)) {
731 ack_mmc_irqs(host,
732 TMIO_STAT_CMDRESPEND |
733 TMIO_STAT_CMDTIMEOUT);
Ian Molton4a489982008-07-15 16:02:21 +0100734 tmio_mmc_cmd_irq(host, status);
735 }
736
737 /* Data transfer */
738 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200739 ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
Ian Molton4a489982008-07-15 16:02:21 +0100740 tmio_mmc_pio_irq(host);
741 }
742
743 /* Data transfer completion */
744 if (ireg & TMIO_STAT_DATAEND) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200745 ack_mmc_irqs(host, TMIO_STAT_DATAEND);
Ian Molton4a489982008-07-15 16:02:21 +0100746 tmio_mmc_data_irq(host);
747 }
748
749 /* Check status - keep going until we've handled it all */
Philipp Zabel5e746722009-06-04 20:12:32 +0200750 status = sd_ctrl_read32(host, CTL_STATUS);
751 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100752 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
753
754 pr_debug("Status at end of loop: %08x\n", status);
755 pr_debug_status(status);
756 }
757 pr_debug("MMC IRQ end\n");
758
759out:
760 return IRQ_HANDLED;
761}
762
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000763#ifdef CONFIG_TMIO_MMC_DMA
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100764static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
765{
766 if (host->sg_ptr == &host->bounce_sg) {
767 unsigned long flags;
768 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
769 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
Guennadi Liakhovetski860cfe72011-03-11 08:30:14 +0100770 tmio_mmc_kunmap_atomic(host->sg_orig, &flags, sg_vaddr);
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100771 }
772}
773
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000774static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
775{
776#if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
777 /* Switch DMA mode on or off - SuperH specific? */
778 sd_ctrl_write16(host, 0xd8, enable ? 2 : 0);
779#endif
780}
781
782static void tmio_dma_complete(void *arg)
783{
784 struct tmio_mmc_host *host = arg;
785
786 dev_dbg(&host->pdev->dev, "Command completed\n");
787
788 if (!host->data)
789 dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n");
790 else
791 enable_mmc_irqs(host, TMIO_STAT_DATAEND);
792}
793
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100794static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000795{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100796 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000797 struct dma_async_tx_descriptor *desc = NULL;
798 struct dma_chan *chan = host->chan_rx;
Andres Salomon4f95bf42011-02-17 19:07:29 -0800799 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100800 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100801 int ret, i;
802 bool aligned = true, multiple = true;
803 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
804
805 for_each_sg(sg, sg_tmp, host->sg_len, i) {
806 if (sg_tmp->offset & align)
807 aligned = false;
808 if (sg_tmp->length & align) {
809 multiple = false;
810 break;
811 }
812 }
813
814 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000815 align >= MAX_ALIGN)) || !multiple) {
816 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100817 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000818 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100819
820 /* The only sg element can be unaligned, use our bounce buffer then */
821 if (!aligned) {
822 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
823 host->sg_ptr = &host->bounce_sg;
824 sg = host->sg_ptr;
825 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000826
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100827 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_FROM_DEVICE);
Linus Walleij33834332011-02-10 16:10:56 +0100828 if (ret > 0)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000829 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
830 DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000831
832 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000833 desc->callback = tmio_dma_complete;
834 desc->callback_param = host;
Linus Walleij449bdc22011-02-10 16:11:07 +0100835 cookie = dmaengine_submit(desc);
836 dma_async_issue_pending(chan);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000837 }
838 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100839 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000840
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100841pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100842 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000843 /* DMA failed, fall back to PIO */
844 if (ret >= 0)
845 ret = -EIO;
846 host->chan_rx = NULL;
847 dma_release_channel(chan);
848 /* Free the Tx channel too */
849 chan = host->chan_tx;
850 if (chan) {
851 host->chan_tx = NULL;
852 dma_release_channel(chan);
853 }
854 dev_warn(&host->pdev->dev,
855 "DMA failed: %d, falling back to PIO\n", ret);
856 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000857 }
858
859 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100860 desc, cookie, host->sg_len);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000861}
862
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100863static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000864{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100865 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000866 struct dma_async_tx_descriptor *desc = NULL;
867 struct dma_chan *chan = host->chan_tx;
Andres Salomon4f95bf42011-02-17 19:07:29 -0800868 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100869 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100870 int ret, i;
871 bool aligned = true, multiple = true;
872 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
873
874 for_each_sg(sg, sg_tmp, host->sg_len, i) {
875 if (sg_tmp->offset & align)
876 aligned = false;
877 if (sg_tmp->length & align) {
878 multiple = false;
879 break;
880 }
881 }
882
883 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000884 align >= MAX_ALIGN)) || !multiple) {
885 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100886 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000887 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100888
889 /* The only sg element can be unaligned, use our bounce buffer then */
890 if (!aligned) {
891 unsigned long flags;
892 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
893 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
894 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
Guennadi Liakhovetski860cfe72011-03-11 08:30:14 +0100895 tmio_mmc_kunmap_atomic(sg, &flags, sg_vaddr);
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100896 host->sg_ptr = &host->bounce_sg;
897 sg = host->sg_ptr;
898 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000899
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100900 ret = dma_map_sg(chan->device->dev, sg, host->sg_len, DMA_TO_DEVICE);
Linus Walleij33834332011-02-10 16:10:56 +0100901 if (ret > 0)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000902 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
903 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000904
905 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000906 desc->callback = tmio_dma_complete;
907 desc->callback_param = host;
Linus Walleij449bdc22011-02-10 16:11:07 +0100908 cookie = dmaengine_submit(desc);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000909 }
910 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100911 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000912
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100913pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100914 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000915 /* DMA failed, fall back to PIO */
916 if (ret >= 0)
917 ret = -EIO;
918 host->chan_tx = NULL;
919 dma_release_channel(chan);
920 /* Free the Rx channel too */
921 chan = host->chan_rx;
922 if (chan) {
923 host->chan_rx = NULL;
924 dma_release_channel(chan);
925 }
926 dev_warn(&host->pdev->dev,
927 "DMA failed: %d, falling back to PIO\n", ret);
928 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000929 }
930
931 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100932 desc, cookie);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000933}
934
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100935static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000936 struct mmc_data *data)
937{
938 if (data->flags & MMC_DATA_READ) {
939 if (host->chan_rx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100940 tmio_mmc_start_dma_rx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000941 } else {
942 if (host->chan_tx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100943 tmio_mmc_start_dma_tx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000944 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000945}
946
947static void tmio_issue_tasklet_fn(unsigned long priv)
948{
949 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
950 struct dma_chan *chan = host->chan_tx;
951
Linus Walleij449bdc22011-02-10 16:11:07 +0100952 dma_async_issue_pending(chan);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000953}
954
955static void tmio_tasklet_fn(unsigned long arg)
956{
957 struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500958 unsigned long flags;
959
960 spin_lock_irqsave(&host->lock, flags);
961
962 if (!host->data)
963 goto out;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000964
965 if (host->data->flags & MMC_DATA_READ)
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100966 dma_unmap_sg(host->chan_rx->device->dev,
Linus Walleijd7554ca2011-02-10 16:10:47 +0100967 host->sg_ptr, host->sg_len,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000968 DMA_FROM_DEVICE);
969 else
Linus Walleij2dc7ddc2011-02-10 16:10:37 +0100970 dma_unmap_sg(host->chan_tx->device->dev,
Linus Walleijd7554ca2011-02-10 16:10:47 +0100971 host->sg_ptr, host->sg_len,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000972 DMA_TO_DEVICE);
973
974 tmio_mmc_do_data_irq(host);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -0500975out:
976 spin_unlock_irqrestore(&host->lock, flags);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000977}
978
979/* It might be necessary to make filter MFD specific */
980static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
981{
982 dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
983 chan->private = arg;
984 return true;
985}
986
987static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
988 struct tmio_mmc_data *pdata)
989{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000990 /* We can only either use DMA for both Tx and Rx or not use it at all */
991 if (pdata->dma) {
992 dma_cap_mask_t mask;
993
994 dma_cap_zero(mask);
995 dma_cap_set(DMA_SLAVE, mask);
996
997 host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
998 pdata->dma->chan_priv_tx);
999 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
1000 host->chan_tx);
1001
1002 if (!host->chan_tx)
1003 return;
1004
1005 host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
1006 pdata->dma->chan_priv_rx);
1007 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
1008 host->chan_rx);
1009
1010 if (!host->chan_rx) {
1011 dma_release_channel(host->chan_tx);
1012 host->chan_tx = NULL;
1013 return;
1014 }
1015
1016 tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host);
1017 tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host);
1018
1019 tmio_mmc_enable_dma(host, true);
1020 }
1021}
1022
1023static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
1024{
1025 if (host->chan_tx) {
1026 struct dma_chan *chan = host->chan_tx;
1027 host->chan_tx = NULL;
1028 dma_release_channel(chan);
1029 }
1030 if (host->chan_rx) {
1031 struct dma_chan *chan = host->chan_rx;
1032 host->chan_rx = NULL;
1033 dma_release_channel(chan);
1034 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001035}
1036#else
Guennadi Liakhovetski93173052010-12-22 12:02:15 +01001037static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
1038{
1039}
1040
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +01001041static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001042 struct mmc_data *data)
1043{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001044}
1045
1046static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
1047 struct tmio_mmc_data *pdata)
1048{
1049 host->chan_tx = NULL;
1050 host->chan_rx = NULL;
1051}
1052
1053static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
1054{
1055}
1056#endif
1057
Ian Molton4a489982008-07-15 16:02:21 +01001058static int tmio_mmc_start_data(struct tmio_mmc_host *host,
1059 struct mmc_data *data)
1060{
Andres Salomon4f95bf42011-02-17 19:07:29 -08001061 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
Yusuke Godaf1334fb2010-08-30 11:50:19 +01001062
Ian Molton4a489982008-07-15 16:02:21 +01001063 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001064 data->blksz, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +01001065
Yusuke Godaf1334fb2010-08-30 11:50:19 +01001066 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
1067 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
1068 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
1069
1070 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
1071 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
1072 mmc_hostname(host->mmc), data->blksz);
1073 return -EINVAL;
1074 }
Ian Molton4a489982008-07-15 16:02:21 +01001075 }
1076
1077 tmio_mmc_init_sg(host, data);
1078 host->data = data;
1079
1080 /* Set transfer length / blocksize */
Philipp Zabel5e746722009-06-04 20:12:32 +02001081 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
1082 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +01001083
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +01001084 tmio_mmc_start_dma(host, data);
1085
1086 return 0;
Ian Molton4a489982008-07-15 16:02:21 +01001087}
1088
1089/* Process requests from the MMC layer */
1090static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
1091{
1092 struct tmio_mmc_host *host = mmc_priv(mmc);
1093 int ret;
1094
1095 if (host->mrq)
1096 pr_debug("request not null\n");
1097
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001098 host->last_req_ts = jiffies;
1099 wmb();
Ian Molton4a489982008-07-15 16:02:21 +01001100 host->mrq = mrq;
1101
1102 if (mrq->data) {
1103 ret = tmio_mmc_start_data(host, mrq->data);
1104 if (ret)
1105 goto fail;
1106 }
1107
1108 ret = tmio_mmc_start_command(host, mrq->cmd);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001109 if (!ret) {
1110 schedule_delayed_work(&host->delayed_reset_work,
1111 msecs_to_jiffies(2000));
Ian Molton4a489982008-07-15 16:02:21 +01001112 return;
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001113 }
Ian Molton4a489982008-07-15 16:02:21 +01001114
1115fail:
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001116 host->mrq = NULL;
Ian Molton4a489982008-07-15 16:02:21 +01001117 mrq->cmd->error = ret;
1118 mmc_request_done(mmc, mrq);
1119}
1120
1121/* Set MMC clock / power.
1122 * Note: This controller uses a simple divider scheme therefore it cannot
1123 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
1124 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
1125 * slowest setting.
1126 */
1127static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1128{
1129 struct tmio_mmc_host *host = mmc_priv(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001130
1131 if (ios->clock)
1132 tmio_mmc_set_clock(host, ios->clock);
1133
1134 /* Power sequence - OFF -> ON -> UP */
1135 switch (ios->power_mode) {
1136 case MMC_POWER_OFF: /* power down SD bus */
Ian Molton64e88672010-01-06 13:51:48 +01001137 if (host->set_pwr)
1138 host->set_pwr(host->pdev, 0);
Ian Molton4a489982008-07-15 16:02:21 +01001139 tmio_mmc_clk_stop(host);
1140 break;
1141 case MMC_POWER_ON: /* power up SD bus */
Ian Molton64e88672010-01-06 13:51:48 +01001142 if (host->set_pwr)
1143 host->set_pwr(host->pdev, 1);
Ian Molton4a489982008-07-15 16:02:21 +01001144 break;
1145 case MMC_POWER_UP: /* start bus clock */
1146 tmio_mmc_clk_start(host);
1147 break;
1148 }
1149
1150 switch (ios->bus_width) {
1151 case MMC_BUS_WIDTH_1:
Philipp Zabel5e746722009-06-04 20:12:32 +02001152 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
Ian Molton4a489982008-07-15 16:02:21 +01001153 break;
1154 case MMC_BUS_WIDTH_4:
Philipp Zabel5e746722009-06-04 20:12:32 +02001155 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
Ian Molton4a489982008-07-15 16:02:21 +01001156 break;
1157 }
1158
1159 /* Let things settle. delay taken from winCE driver */
1160 udelay(140);
1161}
1162
1163static int tmio_mmc_get_ro(struct mmc_host *mmc)
1164{
1165 struct tmio_mmc_host *host = mmc_priv(mmc);
Andres Salomon4f95bf42011-02-17 19:07:29 -08001166 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
Ian Molton4a489982008-07-15 16:02:21 +01001167
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +00001168 return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
1169 (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1;
Ian Molton4a489982008-07-15 16:02:21 +01001170}
1171
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001172static int tmio_mmc_get_cd(struct mmc_host *mmc)
1173{
1174 struct tmio_mmc_host *host = mmc_priv(mmc);
Andres Salomon4f95bf42011-02-17 19:07:29 -08001175 struct tmio_mmc_data *pdata = mfd_get_data(host->pdev);
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001176
1177 if (!pdata->get_cd)
1178 return -ENOSYS;
1179 else
1180 return pdata->get_cd(host->pdev);
1181}
1182
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001183static const struct mmc_host_ops tmio_mmc_ops = {
Ian Molton4a489982008-07-15 16:02:21 +01001184 .request = tmio_mmc_request,
1185 .set_ios = tmio_mmc_set_ios,
1186 .get_ro = tmio_mmc_get_ro,
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001187 .get_cd = tmio_mmc_get_cd,
Arnd Hannemann845ecd22010-12-28 23:22:31 +01001188 .enable_sdio_irq = tmio_mmc_enable_sdio_irq,
Ian Molton4a489982008-07-15 16:02:21 +01001189};
1190
1191#ifdef CONFIG_PM
1192static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
1193{
Andres Salomon944dc032011-03-01 12:32:20 -08001194 const struct mfd_cell *cell = mfd_get_cell(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001195 struct mmc_host *mmc = platform_get_drvdata(dev);
1196 int ret;
1197
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001198 ret = mmc_suspend_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001199
1200 /* Tell MFD core it can disable us now.*/
1201 if (!ret && cell->disable)
1202 cell->disable(dev);
1203
1204 return ret;
1205}
1206
1207static int tmio_mmc_resume(struct platform_device *dev)
1208{
Andres Salomon944dc032011-03-01 12:32:20 -08001209 const struct mfd_cell *cell = mfd_get_cell(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001210 struct mmc_host *mmc = platform_get_drvdata(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001211 int ret = 0;
1212
Ian Molton4a489982008-07-15 16:02:21 +01001213 /* Tell the MFD core we are ready to be enabled */
Ian Molton64e88672010-01-06 13:51:48 +01001214 if (cell->resume) {
1215 ret = cell->resume(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001216 if (ret)
1217 goto out;
1218 }
1219
1220 mmc_resume_host(mmc);
1221
1222out:
1223 return ret;
1224}
1225#else
1226#define tmio_mmc_suspend NULL
1227#define tmio_mmc_resume NULL
1228#endif
1229
1230static int __devinit tmio_mmc_probe(struct platform_device *dev)
1231{
Andres Salomon944dc032011-03-01 12:32:20 -08001232 const struct mfd_cell *cell = mfd_get_cell(dev);
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001233 struct tmio_mmc_data *pdata;
Ian Molton64e88672010-01-06 13:51:48 +01001234 struct resource *res_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001235 struct tmio_mmc_host *host;
1236 struct mmc_host *mmc;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001237 int ret = -EINVAL;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001238 u32 irq_mask = TMIO_MASK_CMD;
Ian Molton4a489982008-07-15 16:02:21 +01001239
Ian Molton64e88672010-01-06 13:51:48 +01001240 if (dev->num_resources != 2)
Ian Molton4a489982008-07-15 16:02:21 +01001241 goto out;
1242
1243 res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
Ian Molton64e88672010-01-06 13:51:48 +01001244 if (!res_ctl)
Ian Molton4a489982008-07-15 16:02:21 +01001245 goto out;
Ian Molton4a489982008-07-15 16:02:21 +01001246
Andres Salomon4f95bf42011-02-17 19:07:29 -08001247 pdata = mfd_get_data(dev);
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001248 if (!pdata || !pdata->hclk)
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001249 goto out;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001250
1251 ret = -ENOMEM;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001252
Ian Molton4a489982008-07-15 16:02:21 +01001253 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
1254 if (!mmc)
1255 goto out;
1256
1257 host = mmc_priv(mmc);
1258 host->mmc = mmc;
Ian Molton64e88672010-01-06 13:51:48 +01001259 host->pdev = dev;
Ian Molton4a489982008-07-15 16:02:21 +01001260 platform_set_drvdata(dev, mmc);
1261
Ian Molton64e88672010-01-06 13:51:48 +01001262 host->set_pwr = pdata->set_pwr;
1263 host->set_clk_div = pdata->set_clk_div;
1264
Philipp Zabel5e746722009-06-04 20:12:32 +02001265 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
1266 host->bus_shift = resource_size(res_ctl) >> 10;
1267
Magnus Dammbc6772a2009-03-11 21:58:54 +09001268 host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
Ian Molton4a489982008-07-15 16:02:21 +01001269 if (!host->ctl)
1270 goto host_free;
1271
Ian Molton4a489982008-07-15 16:02:21 +01001272 mmc->ops = &tmio_mmc_ops;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001273 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001274 mmc->f_max = pdata->hclk;
1275 mmc->f_min = mmc->f_max / 512;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001276 mmc->max_segs = 32;
1277 mmc->max_blk_size = 512;
1278 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
1279 mmc->max_segs;
1280 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1281 mmc->max_seg_size = mmc->max_req_size;
Guennadi Liakhovetskia2b14dc2010-05-19 18:37:25 +00001282 if (pdata->ocr_mask)
1283 mmc->ocr_avail = pdata->ocr_mask;
1284 else
1285 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Ian Molton4a489982008-07-15 16:02:21 +01001286
Ian Molton4a489982008-07-15 16:02:21 +01001287 /* Tell the MFD core we are ready to be enabled */
1288 if (cell->enable) {
1289 ret = cell->enable(dev);
1290 if (ret)
Ian Molton64e88672010-01-06 13:51:48 +01001291 goto unmap_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001292 }
1293
Ian Molton4a489982008-07-15 16:02:21 +01001294 tmio_mmc_clk_stop(host);
1295 reset(host);
1296
1297 ret = platform_get_irq(dev, 0);
1298 if (ret >= 0)
1299 host->irq = ret;
1300 else
Magnus Damm7ee422d2010-02-17 16:38:23 +09001301 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001302
Philipp Zabel5e746722009-06-04 20:12:32 +02001303 disable_mmc_irqs(host, TMIO_MASK_ALL);
Arnd Hannemann845ecd22010-12-28 23:22:31 +01001304 if (pdata->flags & TMIO_MMC_SDIO_IRQ)
1305 tmio_mmc_enable_sdio_irq(mmc, 0);
Ian Molton4a489982008-07-15 16:02:21 +01001306
Philipp Zabel6c413cc2009-06-04 20:12:33 +02001307 ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
Magnus Damm14f1b752009-12-14 18:01:33 -08001308 IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
Ian Molton4a489982008-07-15 16:02:21 +01001309 if (ret)
Magnus Damm7ee422d2010-02-17 16:38:23 +09001310 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001311
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001312 spin_lock_init(&host->lock);
1313
1314 /* Init delayed work for request timeouts */
1315 INIT_DELAYED_WORK(&host->delayed_reset_work, tmio_mmc_reset_work);
1316
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001317 /* See if we also get DMA */
1318 tmio_mmc_request_dma(host, pdata);
1319
Ian Molton4a489982008-07-15 16:02:21 +01001320 mmc_add_host(mmc);
1321
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001322 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
1323 (unsigned long)host->ctl, host->irq);
Ian Molton4a489982008-07-15 16:02:21 +01001324
1325 /* Unmask the IRQs we want to know about */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001326 if (!host->chan_rx)
1327 irq_mask |= TMIO_MASK_READOP;
1328 if (!host->chan_tx)
1329 irq_mask |= TMIO_MASK_WRITEOP;
1330 enable_mmc_irqs(host, irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +01001331
1332 return 0;
1333
Magnus Damm7ee422d2010-02-17 16:38:23 +09001334cell_disable:
1335 if (cell->disable)
1336 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001337unmap_ctl:
1338 iounmap(host->ctl);
1339host_free:
1340 mmc_free_host(mmc);
1341out:
1342 return ret;
1343}
1344
1345static int __devexit tmio_mmc_remove(struct platform_device *dev)
1346{
Andres Salomon944dc032011-03-01 12:32:20 -08001347 const struct mfd_cell *cell = mfd_get_cell(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001348 struct mmc_host *mmc = platform_get_drvdata(dev);
1349
1350 platform_set_drvdata(dev, NULL);
1351
1352 if (mmc) {
1353 struct tmio_mmc_host *host = mmc_priv(mmc);
1354 mmc_remove_host(mmc);
Arnd Hannemann6ff56e02011-01-05 17:36:14 -05001355 cancel_delayed_work_sync(&host->delayed_reset_work);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001356 tmio_mmc_release_dma(host);
Ian Molton4a489982008-07-15 16:02:21 +01001357 free_irq(host->irq, host);
Magnus Damm7ee422d2010-02-17 16:38:23 +09001358 if (cell->disable)
1359 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001360 iounmap(host->ctl);
Magnus Dammbedcc452009-03-11 21:59:03 +09001361 mmc_free_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001362 }
1363
1364 return 0;
1365}
1366
1367/* ------------------- device registration ----------------------- */
1368
1369static struct platform_driver tmio_mmc_driver = {
1370 .driver = {
1371 .name = "tmio-mmc",
1372 .owner = THIS_MODULE,
1373 },
1374 .probe = tmio_mmc_probe,
1375 .remove = __devexit_p(tmio_mmc_remove),
1376 .suspend = tmio_mmc_suspend,
1377 .resume = tmio_mmc_resume,
1378};
1379
1380
1381static int __init tmio_mmc_init(void)
1382{
1383 return platform_driver_register(&tmio_mmc_driver);
1384}
1385
1386static void __exit tmio_mmc_exit(void)
1387{
1388 platform_driver_unregister(&tmio_mmc_driver);
1389}
1390
1391module_init(tmio_mmc_init);
1392module_exit(tmio_mmc_exit);
1393
1394MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
1395MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
1396MODULE_LICENSE("GPL v2");
1397MODULE_ALIAS("platform:tmio-mmc");