blob: f442c8205b0af4825449efd456933b33df17ad17 [file] [log] [blame]
Ian Molton4a489982008-07-15 16:02:21 +01001/*
2 * linux/drivers/mmc/tmio_mmc.c
3 *
4 * Copyright (C) 2004 Ian Molton
5 * Copyright (C) 2007 Ian Molton
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2 as
9 * published by the Free Software Foundation.
10 *
11 * Driver for the MMC / SD / SDIO cell found in:
12 *
Philipp Zabele6f2c7a2009-06-04 20:12:37 +020013 * TC6393XB TC6391XB TC6387XB T7L66XB ASIC3
Ian Molton4a489982008-07-15 16:02:21 +010014 *
15 * This driver draws mainly on scattered spec sheets, Reverse engineering
16 * of the toshiba e800 SD driver and some parts of the 2.4 ASIC3 driver (4 bit
17 * support). (Further 4 bit support from a later datasheet).
18 *
19 * TODO:
20 * Investigate using a workqueue for PIO transfers
21 * Eliminate FIXMEs
22 * SDIO support
23 * Better Power management
24 * Handle MMC errors better
25 * double buffer support
26 *
27 */
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010028
Ian Molton4a489982008-07-15 16:02:21 +010029#include <linux/delay.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010030#include <linux/device.h>
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +000031#include <linux/dmaengine.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010032#include <linux/highmem.h>
33#include <linux/interrupt.h>
34#include <linux/io.h>
35#include <linux/irq.h>
Ian Molton4a489982008-07-15 16:02:21 +010036#include <linux/mfd/core.h>
37#include <linux/mfd/tmio.h>
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010038#include <linux/mmc/host.h>
39#include <linux/module.h>
40#include <linux/pagemap.h>
41#include <linux/scatterlist.h>
Ian Molton4a489982008-07-15 16:02:21 +010042
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +010043#define CTL_SD_CMD 0x00
44#define CTL_ARG_REG 0x04
45#define CTL_STOP_INTERNAL_ACTION 0x08
46#define CTL_XFER_BLK_COUNT 0xa
47#define CTL_RESPONSE 0x0c
48#define CTL_STATUS 0x1c
49#define CTL_IRQ_MASK 0x20
50#define CTL_SD_CARD_CLK_CTL 0x24
51#define CTL_SD_XFER_LEN 0x26
52#define CTL_SD_MEM_CARD_OPT 0x28
53#define CTL_SD_ERROR_DETAIL_STATUS 0x2c
54#define CTL_SD_DATA_PORT 0x30
55#define CTL_TRANSACTION_CTL 0x34
56#define CTL_RESET_SD 0xe0
57#define CTL_SDIO_REGS 0x100
58#define CTL_CLK_AND_WAIT_CTL 0x138
59#define CTL_RESET_SDIO 0x1e0
60
61/* Definitions for values the CTRL_STATUS register can take. */
62#define TMIO_STAT_CMDRESPEND 0x00000001
63#define TMIO_STAT_DATAEND 0x00000004
64#define TMIO_STAT_CARD_REMOVE 0x00000008
65#define TMIO_STAT_CARD_INSERT 0x00000010
66#define TMIO_STAT_SIGSTATE 0x00000020
67#define TMIO_STAT_WRPROTECT 0x00000080
68#define TMIO_STAT_CARD_REMOVE_A 0x00000100
69#define TMIO_STAT_CARD_INSERT_A 0x00000200
70#define TMIO_STAT_SIGSTATE_A 0x00000400
71#define TMIO_STAT_CMD_IDX_ERR 0x00010000
72#define TMIO_STAT_CRCFAIL 0x00020000
73#define TMIO_STAT_STOPBIT_ERR 0x00040000
74#define TMIO_STAT_DATATIMEOUT 0x00080000
75#define TMIO_STAT_RXOVERFLOW 0x00100000
76#define TMIO_STAT_TXUNDERRUN 0x00200000
77#define TMIO_STAT_CMDTIMEOUT 0x00400000
78#define TMIO_STAT_RXRDY 0x01000000
79#define TMIO_STAT_TXRQ 0x02000000
80#define TMIO_STAT_ILL_FUNC 0x20000000
81#define TMIO_STAT_CMD_BUSY 0x40000000
82#define TMIO_STAT_ILL_ACCESS 0x80000000
83
84/* Define some IRQ masks */
85/* This is the mask used at reset by the chip */
86#define TMIO_MASK_ALL 0x837f031d
87#define TMIO_MASK_READOP (TMIO_STAT_RXRDY | TMIO_STAT_DATAEND)
88#define TMIO_MASK_WRITEOP (TMIO_STAT_TXRQ | TMIO_STAT_DATAEND)
89#define TMIO_MASK_CMD (TMIO_STAT_CMDRESPEND | TMIO_STAT_CMDTIMEOUT | \
90 TMIO_STAT_CARD_REMOVE | TMIO_STAT_CARD_INSERT)
91#define TMIO_MASK_IRQ (TMIO_MASK_READOP | TMIO_MASK_WRITEOP | TMIO_MASK_CMD)
92
93#define enable_mmc_irqs(host, i) \
94 do { \
95 u32 mask;\
96 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
97 mask &= ~((i) & TMIO_MASK_IRQ); \
98 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
99 } while (0)
100
101#define disable_mmc_irqs(host, i) \
102 do { \
103 u32 mask;\
104 mask = sd_ctrl_read32((host), CTL_IRQ_MASK); \
105 mask |= ((i) & TMIO_MASK_IRQ); \
106 sd_ctrl_write32((host), CTL_IRQ_MASK, mask); \
107 } while (0)
108
109#define ack_mmc_irqs(host, i) \
110 do { \
111 sd_ctrl_write32((host), CTL_STATUS, ~(i)); \
112 } while (0)
113
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100114/* This is arbitrary, just noone needed any higher alignment yet */
115#define MAX_ALIGN 4
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100116
117struct tmio_mmc_host {
118 void __iomem *ctl;
119 unsigned long bus_shift;
120 struct mmc_command *cmd;
121 struct mmc_request *mrq;
122 struct mmc_data *data;
123 struct mmc_host *mmc;
124 int irq;
125
126 /* Callbacks for clock / power control */
127 void (*set_pwr)(struct platform_device *host, int state);
128 void (*set_clk_div)(struct platform_device *host, int state);
129
130 /* pio related stuff */
131 struct scatterlist *sg_ptr;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100132 struct scatterlist *sg_orig;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100133 unsigned int sg_len;
134 unsigned int sg_off;
135
136 struct platform_device *pdev;
137
138 /* DMA support */
139 struct dma_chan *chan_rx;
140 struct dma_chan *chan_tx;
141 struct tasklet_struct dma_complete;
142 struct tasklet_struct dma_issue;
143#ifdef CONFIG_TMIO_MMC_DMA
144 unsigned int dma_sglen;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100145 u8 bounce_buf[PAGE_CACHE_SIZE] __attribute__((aligned(MAX_ALIGN)));
146 struct scatterlist bounce_sg;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100147#endif
148};
149
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100150static void tmio_check_bounce_buffer(struct tmio_mmc_host *host);
151
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100152static u16 sd_ctrl_read16(struct tmio_mmc_host *host, int addr)
153{
154 return readw(host->ctl + (addr << host->bus_shift));
155}
156
157static void sd_ctrl_read16_rep(struct tmio_mmc_host *host, int addr,
158 u16 *buf, int count)
159{
160 readsw(host->ctl + (addr << host->bus_shift), buf, count);
161}
162
163static u32 sd_ctrl_read32(struct tmio_mmc_host *host, int addr)
164{
165 return readw(host->ctl + (addr << host->bus_shift)) |
166 readw(host->ctl + ((addr + 2) << host->bus_shift)) << 16;
167}
168
169static void sd_ctrl_write16(struct tmio_mmc_host *host, int addr, u16 val)
170{
171 writew(val, host->ctl + (addr << host->bus_shift));
172}
173
174static void sd_ctrl_write16_rep(struct tmio_mmc_host *host, int addr,
175 u16 *buf, int count)
176{
177 writesw(host->ctl + (addr << host->bus_shift), buf, count);
178}
179
180static void sd_ctrl_write32(struct tmio_mmc_host *host, int addr, u32 val)
181{
182 writew(val, host->ctl + (addr << host->bus_shift));
183 writew(val >> 16, host->ctl + ((addr + 2) << host->bus_shift));
184}
185
186static void tmio_mmc_init_sg(struct tmio_mmc_host *host, struct mmc_data *data)
187{
188 host->sg_len = data->sg_len;
189 host->sg_ptr = data->sg;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100190 host->sg_orig = data->sg;
Guennadi Liakhovetskie0bc6ff2010-11-23 17:24:11 +0100191 host->sg_off = 0;
192}
193
194static int tmio_mmc_next_sg(struct tmio_mmc_host *host)
195{
196 host->sg_ptr = sg_next(host->sg_ptr);
197 host->sg_off = 0;
198 return --host->sg_len;
199}
200
201static char *tmio_mmc_kmap_atomic(struct scatterlist *sg, unsigned long *flags)
202{
203 local_irq_save(*flags);
204 return kmap_atomic(sg_page(sg), KM_BIO_SRC_IRQ) + sg->offset;
205}
206
207static void tmio_mmc_kunmap_atomic(void *virt, unsigned long *flags)
208{
209 kunmap_atomic(virt, KM_BIO_SRC_IRQ);
210 local_irq_restore(*flags);
211}
212
213#ifdef CONFIG_MMC_DEBUG
214
215#define STATUS_TO_TEXT(a) \
216 do { \
217 if (status & TMIO_STAT_##a) \
218 printk(#a); \
219 } while (0)
220
221void pr_debug_status(u32 status)
222{
223 printk(KERN_DEBUG "status: %08x = ", status);
224 STATUS_TO_TEXT(CARD_REMOVE);
225 STATUS_TO_TEXT(CARD_INSERT);
226 STATUS_TO_TEXT(SIGSTATE);
227 STATUS_TO_TEXT(WRPROTECT);
228 STATUS_TO_TEXT(CARD_REMOVE_A);
229 STATUS_TO_TEXT(CARD_INSERT_A);
230 STATUS_TO_TEXT(SIGSTATE_A);
231 STATUS_TO_TEXT(CMD_IDX_ERR);
232 STATUS_TO_TEXT(STOPBIT_ERR);
233 STATUS_TO_TEXT(ILL_FUNC);
234 STATUS_TO_TEXT(CMD_BUSY);
235 STATUS_TO_TEXT(CMDRESPEND);
236 STATUS_TO_TEXT(DATAEND);
237 STATUS_TO_TEXT(CRCFAIL);
238 STATUS_TO_TEXT(DATATIMEOUT);
239 STATUS_TO_TEXT(CMDTIMEOUT);
240 STATUS_TO_TEXT(RXOVERFLOW);
241 STATUS_TO_TEXT(TXUNDERRUN);
242 STATUS_TO_TEXT(RXRDY);
243 STATUS_TO_TEXT(TXRQ);
244 STATUS_TO_TEXT(ILL_ACCESS);
245 printk("\n");
246}
247
248#else
249#define pr_debug_status(s) do { } while (0)
250#endif
Ian Molton4a489982008-07-15 16:02:21 +0100251
Ian Molton4a489982008-07-15 16:02:21 +0100252static void tmio_mmc_set_clock(struct tmio_mmc_host *host, int new_clock)
253{
Ian Moltonda46a0b2009-06-12 21:53:05 +0100254 u32 clk = 0, clock;
Ian Molton4a489982008-07-15 16:02:21 +0100255
256 if (new_clock) {
Ian Moltonda46a0b2009-06-12 21:53:05 +0100257 for (clock = host->mmc->f_min, clk = 0x80000080;
258 new_clock >= (clock<<1); clk >>= 1)
Ian Molton4a489982008-07-15 16:02:21 +0100259 clock <<= 1;
Ian Molton4a489982008-07-15 16:02:21 +0100260 clk |= 0x100;
261 }
262
Ian Molton64e88672010-01-06 13:51:48 +0100263 if (host->set_clk_div)
264 host->set_clk_div(host->pdev, (clk>>22) & 1);
265
Ian Moltonda46a0b2009-06-12 21:53:05 +0100266 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, clk & 0x1ff);
Ian Molton4a489982008-07-15 16:02:21 +0100267}
268
269static void tmio_mmc_clk_stop(struct tmio_mmc_host *host)
270{
Philipp Zabel5e746722009-06-04 20:12:32 +0200271 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100272 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200273 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, ~0x0100 &
274 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100275 msleep(10);
276}
277
278static void tmio_mmc_clk_start(struct tmio_mmc_host *host)
279{
Philipp Zabel5e746722009-06-04 20:12:32 +0200280 sd_ctrl_write16(host, CTL_SD_CARD_CLK_CTL, 0x0100 |
281 sd_ctrl_read16(host, CTL_SD_CARD_CLK_CTL));
Ian Molton4a489982008-07-15 16:02:21 +0100282 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200283 sd_ctrl_write16(host, CTL_CLK_AND_WAIT_CTL, 0x0100);
Ian Molton4a489982008-07-15 16:02:21 +0100284 msleep(10);
285}
286
287static void reset(struct tmio_mmc_host *host)
288{
Ian Molton4a489982008-07-15 16:02:21 +0100289 /* FIXME - should we set stop clock reg here */
Philipp Zabel5e746722009-06-04 20:12:32 +0200290 sd_ctrl_write16(host, CTL_RESET_SD, 0x0000);
291 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0000);
Ian Molton4a489982008-07-15 16:02:21 +0100292 msleep(10);
Philipp Zabel5e746722009-06-04 20:12:32 +0200293 sd_ctrl_write16(host, CTL_RESET_SD, 0x0001);
294 sd_ctrl_write16(host, CTL_RESET_SDIO, 0x0001);
Ian Molton4a489982008-07-15 16:02:21 +0100295 msleep(10);
296}
297
298static void
299tmio_mmc_finish_request(struct tmio_mmc_host *host)
300{
301 struct mmc_request *mrq = host->mrq;
302
303 host->mrq = NULL;
304 host->cmd = NULL;
305 host->data = NULL;
306
307 mmc_request_done(host->mmc, mrq);
308}
309
310/* These are the bitmasks the tmio chip requires to implement the MMC response
311 * types. Note that R1 and R6 are the same in this scheme. */
312#define APP_CMD 0x0040
313#define RESP_NONE 0x0300
314#define RESP_R1 0x0400
315#define RESP_R1B 0x0500
316#define RESP_R2 0x0600
317#define RESP_R3 0x0700
318#define DATA_PRESENT 0x0800
319#define TRANSFER_READ 0x1000
320#define TRANSFER_MULTI 0x2000
321#define SECURITY_CMD 0x4000
322
323static int
324tmio_mmc_start_command(struct tmio_mmc_host *host, struct mmc_command *cmd)
325{
Ian Molton4a489982008-07-15 16:02:21 +0100326 struct mmc_data *data = host->data;
327 int c = cmd->opcode;
328
329 /* Command 12 is handled by hardware */
330 if (cmd->opcode == 12 && !cmd->arg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200331 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x001);
Ian Molton4a489982008-07-15 16:02:21 +0100332 return 0;
333 }
334
335 switch (mmc_resp_type(cmd)) {
336 case MMC_RSP_NONE: c |= RESP_NONE; break;
337 case MMC_RSP_R1: c |= RESP_R1; break;
338 case MMC_RSP_R1B: c |= RESP_R1B; break;
339 case MMC_RSP_R2: c |= RESP_R2; break;
340 case MMC_RSP_R3: c |= RESP_R3; break;
341 default:
342 pr_debug("Unknown response type %d\n", mmc_resp_type(cmd));
343 return -EINVAL;
344 }
345
346 host->cmd = cmd;
347
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000348/* FIXME - this seems to be ok commented out but the spec suggest this bit
349 * should be set when issuing app commands.
Ian Molton4a489982008-07-15 16:02:21 +0100350 * if(cmd->flags & MMC_FLAG_ACMD)
351 * c |= APP_CMD;
352 */
353 if (data) {
354 c |= DATA_PRESENT;
355 if (data->blocks > 1) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200356 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x100);
Ian Molton4a489982008-07-15 16:02:21 +0100357 c |= TRANSFER_MULTI;
358 }
359 if (data->flags & MMC_DATA_READ)
360 c |= TRANSFER_READ;
361 }
362
Philipp Zabel5e746722009-06-04 20:12:32 +0200363 enable_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100364
365 /* Fire off the command */
Philipp Zabel5e746722009-06-04 20:12:32 +0200366 sd_ctrl_write32(host, CTL_ARG_REG, cmd->arg);
367 sd_ctrl_write16(host, CTL_SD_CMD, c);
Ian Molton4a489982008-07-15 16:02:21 +0100368
369 return 0;
370}
371
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000372/*
373 * This chip always returns (at least?) as much data as you ask for.
Ian Molton4a489982008-07-15 16:02:21 +0100374 * I'm unsure what happens if you ask for less than a block. This should be
375 * looked into to ensure that a funny length read doesnt hose the controller.
Ian Molton4a489982008-07-15 16:02:21 +0100376 */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000377static void tmio_mmc_pio_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100378{
Ian Molton4a489982008-07-15 16:02:21 +0100379 struct mmc_data *data = host->data;
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700380 void *sg_virt;
Ian Molton4a489982008-07-15 16:02:21 +0100381 unsigned short *buf;
382 unsigned int count;
383 unsigned long flags;
384
385 if (!data) {
386 pr_debug("Spurious PIO IRQ\n");
387 return;
388 }
389
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700390 sg_virt = tmio_mmc_kmap_atomic(host->sg_ptr, &flags);
391 buf = (unsigned short *)(sg_virt + host->sg_off);
Ian Molton4a489982008-07-15 16:02:21 +0100392
393 count = host->sg_ptr->length - host->sg_off;
394 if (count > data->blksz)
395 count = data->blksz;
396
397 pr_debug("count: %08x offset: %08x flags %08x\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000398 count, host->sg_off, data->flags);
Ian Molton4a489982008-07-15 16:02:21 +0100399
400 /* Transfer the data */
401 if (data->flags & MMC_DATA_READ)
Philipp Zabel5e746722009-06-04 20:12:32 +0200402 sd_ctrl_read16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100403 else
Philipp Zabel5e746722009-06-04 20:12:32 +0200404 sd_ctrl_write16_rep(host, CTL_SD_DATA_PORT, buf, count >> 1);
Ian Molton4a489982008-07-15 16:02:21 +0100405
406 host->sg_off += count;
407
Guennadi Liakhovetski5600efb2010-09-09 16:37:43 -0700408 tmio_mmc_kunmap_atomic(sg_virt, &flags);
Ian Molton4a489982008-07-15 16:02:21 +0100409
410 if (host->sg_off == host->sg_ptr->length)
411 tmio_mmc_next_sg(host);
412
413 return;
414}
415
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000416static void tmio_mmc_do_data_irq(struct tmio_mmc_host *host)
Ian Molton4a489982008-07-15 16:02:21 +0100417{
Ian Molton4a489982008-07-15 16:02:21 +0100418 struct mmc_data *data = host->data;
Julia Lawalla0d045c2008-12-16 16:13:09 +0100419 struct mmc_command *stop;
Ian Molton4a489982008-07-15 16:02:21 +0100420
421 host->data = NULL;
422
423 if (!data) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000424 dev_warn(&host->pdev->dev, "Spurious data end IRQ\n");
Ian Molton4a489982008-07-15 16:02:21 +0100425 return;
426 }
Julia Lawalla0d045c2008-12-16 16:13:09 +0100427 stop = data->stop;
Ian Molton4a489982008-07-15 16:02:21 +0100428
429 /* FIXME - return correct transfer count on errors */
430 if (!data->error)
431 data->bytes_xfered = data->blocks * data->blksz;
432 else
433 data->bytes_xfered = 0;
434
435 pr_debug("Completed data request\n");
436
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000437 /*
438 * FIXME: other drivers allow an optional stop command of any given type
Ian Molton4a489982008-07-15 16:02:21 +0100439 * which we dont do, as the chip can auto generate them.
440 * Perhaps we can be smarter about when to use auto CMD12 and
441 * only issue the auto request when we know this is the desired
442 * stop command, allowing fallback to the stop command the
443 * upper layers expect. For now, we do what works.
444 */
445
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000446 if (data->flags & MMC_DATA_READ) {
447 if (!host->chan_rx)
448 disable_mmc_irqs(host, TMIO_MASK_READOP);
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100449 else
450 tmio_check_bounce_buffer(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000451 dev_dbg(&host->pdev->dev, "Complete Rx request %p\n",
452 host->mrq);
453 } else {
454 if (!host->chan_tx)
455 disable_mmc_irqs(host, TMIO_MASK_WRITEOP);
456 dev_dbg(&host->pdev->dev, "Complete Tx request %p\n",
457 host->mrq);
458 }
Ian Molton4a489982008-07-15 16:02:21 +0100459
460 if (stop) {
461 if (stop->opcode == 12 && !stop->arg)
Philipp Zabel5e746722009-06-04 20:12:32 +0200462 sd_ctrl_write16(host, CTL_STOP_INTERNAL_ACTION, 0x000);
Ian Molton4a489982008-07-15 16:02:21 +0100463 else
464 BUG();
465 }
466
467 tmio_mmc_finish_request(host);
468}
469
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000470static void tmio_mmc_data_irq(struct tmio_mmc_host *host)
471{
472 struct mmc_data *data = host->data;
473
474 if (!data)
475 return;
476
477 if (host->chan_tx && (data->flags & MMC_DATA_WRITE)) {
478 /*
479 * Has all data been written out yet? Testing on SuperH showed,
480 * that in most cases the first interrupt comes already with the
481 * BUSY status bit clear, but on some operations, like mount or
482 * in the beginning of a write / sync / umount, there is one
483 * DATAEND interrupt with the BUSY bit set, in this cases
484 * waiting for one more interrupt fixes the problem.
485 */
486 if (!(sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_CMD_BUSY)) {
487 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
488 tasklet_schedule(&host->dma_complete);
489 }
490 } else if (host->chan_rx && (data->flags & MMC_DATA_READ)) {
491 disable_mmc_irqs(host, TMIO_STAT_DATAEND);
492 tasklet_schedule(&host->dma_complete);
493 } else {
494 tmio_mmc_do_data_irq(host);
495 }
496}
497
498static void tmio_mmc_cmd_irq(struct tmio_mmc_host *host,
Ian Molton4a489982008-07-15 16:02:21 +0100499 unsigned int stat)
500{
Ian Molton4a489982008-07-15 16:02:21 +0100501 struct mmc_command *cmd = host->cmd;
Philipp Zabel5e746722009-06-04 20:12:32 +0200502 int i, addr;
Ian Molton4a489982008-07-15 16:02:21 +0100503
504 if (!host->cmd) {
505 pr_debug("Spurious CMD irq\n");
506 return;
507 }
508
509 host->cmd = NULL;
510
511 /* This controller is sicker than the PXA one. Not only do we need to
512 * drop the top 8 bits of the first response word, we also need to
513 * modify the order of the response for short response command types.
514 */
515
Philipp Zabel5e746722009-06-04 20:12:32 +0200516 for (i = 3, addr = CTL_RESPONSE ; i >= 0 ; i--, addr += 4)
517 cmd->resp[i] = sd_ctrl_read32(host, addr);
Ian Molton4a489982008-07-15 16:02:21 +0100518
519 if (cmd->flags & MMC_RSP_136) {
520 cmd->resp[0] = (cmd->resp[0] << 8) | (cmd->resp[1] >> 24);
521 cmd->resp[1] = (cmd->resp[1] << 8) | (cmd->resp[2] >> 24);
522 cmd->resp[2] = (cmd->resp[2] << 8) | (cmd->resp[3] >> 24);
523 cmd->resp[3] <<= 8;
524 } else if (cmd->flags & MMC_RSP_R3) {
525 cmd->resp[0] = cmd->resp[3];
526 }
527
528 if (stat & TMIO_STAT_CMDTIMEOUT)
529 cmd->error = -ETIMEDOUT;
530 else if (stat & TMIO_STAT_CRCFAIL && cmd->flags & MMC_RSP_CRC)
531 cmd->error = -EILSEQ;
532
533 /* If there is data to handle we enable data IRQs here, and
534 * we will ultimatley finish the request in the data_end handler.
535 * If theres no data or we encountered an error, finish now.
536 */
537 if (host->data && !cmd->error) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000538 if (host->data->flags & MMC_DATA_READ) {
539 if (!host->chan_rx)
540 enable_mmc_irqs(host, TMIO_MASK_READOP);
541 } else {
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100542 if (!host->chan_tx)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000543 enable_mmc_irqs(host, TMIO_MASK_WRITEOP);
544 else
545 tasklet_schedule(&host->dma_issue);
546 }
Ian Molton4a489982008-07-15 16:02:21 +0100547 } else {
548 tmio_mmc_finish_request(host);
549 }
550
551 return;
552}
553
Ian Molton4a489982008-07-15 16:02:21 +0100554static irqreturn_t tmio_mmc_irq(int irq, void *devid)
555{
556 struct tmio_mmc_host *host = devid;
Ian Molton4a489982008-07-15 16:02:21 +0100557 unsigned int ireg, irq_mask, status;
558
559 pr_debug("MMC IRQ begin\n");
560
Philipp Zabel5e746722009-06-04 20:12:32 +0200561 status = sd_ctrl_read32(host, CTL_STATUS);
562 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100563 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
564
565 pr_debug_status(status);
566 pr_debug_status(ireg);
567
568 if (!ireg) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200569 disable_mmc_irqs(host, status & ~irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +0100570
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000571 pr_warning("tmio_mmc: Spurious irq, disabling! "
Ian Molton4a489982008-07-15 16:02:21 +0100572 "0x%08x 0x%08x 0x%08x\n", status, irq_mask, ireg);
573 pr_debug_status(status);
574
575 goto out;
576 }
577
578 while (ireg) {
579 /* Card insert / remove attempts */
580 if (ireg & (TMIO_STAT_CARD_INSERT | TMIO_STAT_CARD_REMOVE)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200581 ack_mmc_irqs(host, TMIO_STAT_CARD_INSERT |
Ian Molton4a489982008-07-15 16:02:21 +0100582 TMIO_STAT_CARD_REMOVE);
Magnus Damm6d9af5a2010-02-17 16:38:04 +0900583 mmc_detect_change(host->mmc, msecs_to_jiffies(100));
Ian Molton4a489982008-07-15 16:02:21 +0100584 }
585
586 /* CRC and other errors */
587/* if (ireg & TMIO_STAT_ERR_IRQ)
588 * handled |= tmio_error_irq(host, irq, stat);
589 */
590
591 /* Command completion */
592 if (ireg & TMIO_MASK_CMD) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200593 ack_mmc_irqs(host, TMIO_MASK_CMD);
Ian Molton4a489982008-07-15 16:02:21 +0100594 tmio_mmc_cmd_irq(host, status);
595 }
596
597 /* Data transfer */
598 if (ireg & (TMIO_STAT_RXRDY | TMIO_STAT_TXRQ)) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200599 ack_mmc_irqs(host, TMIO_STAT_RXRDY | TMIO_STAT_TXRQ);
Ian Molton4a489982008-07-15 16:02:21 +0100600 tmio_mmc_pio_irq(host);
601 }
602
603 /* Data transfer completion */
604 if (ireg & TMIO_STAT_DATAEND) {
Philipp Zabel5e746722009-06-04 20:12:32 +0200605 ack_mmc_irqs(host, TMIO_STAT_DATAEND);
Ian Molton4a489982008-07-15 16:02:21 +0100606 tmio_mmc_data_irq(host);
607 }
608
609 /* Check status - keep going until we've handled it all */
Philipp Zabel5e746722009-06-04 20:12:32 +0200610 status = sd_ctrl_read32(host, CTL_STATUS);
611 irq_mask = sd_ctrl_read32(host, CTL_IRQ_MASK);
Ian Molton4a489982008-07-15 16:02:21 +0100612 ireg = status & TMIO_MASK_IRQ & ~irq_mask;
613
614 pr_debug("Status at end of loop: %08x\n", status);
615 pr_debug_status(status);
616 }
617 pr_debug("MMC IRQ end\n");
618
619out:
620 return IRQ_HANDLED;
621}
622
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000623#ifdef CONFIG_TMIO_MMC_DMA
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100624static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
625{
626 if (host->sg_ptr == &host->bounce_sg) {
627 unsigned long flags;
628 void *sg_vaddr = tmio_mmc_kmap_atomic(host->sg_orig, &flags);
629 memcpy(sg_vaddr, host->bounce_buf, host->bounce_sg.length);
630 tmio_mmc_kunmap_atomic(sg_vaddr, &flags);
631 }
632}
633
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000634static void tmio_mmc_enable_dma(struct tmio_mmc_host *host, bool enable)
635{
636#if defined(CONFIG_SUPERH) || defined(CONFIG_ARCH_SHMOBILE)
637 /* Switch DMA mode on or off - SuperH specific? */
638 sd_ctrl_write16(host, 0xd8, enable ? 2 : 0);
639#endif
640}
641
642static void tmio_dma_complete(void *arg)
643{
644 struct tmio_mmc_host *host = arg;
645
646 dev_dbg(&host->pdev->dev, "Command completed\n");
647
648 if (!host->data)
649 dev_warn(&host->pdev->dev, "NULL data in DMA completion!\n");
650 else
651 enable_mmc_irqs(host, TMIO_STAT_DATAEND);
652}
653
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100654static void tmio_mmc_start_dma_rx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000655{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100656 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000657 struct dma_async_tx_descriptor *desc = NULL;
658 struct dma_chan *chan = host->chan_rx;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100659 struct mfd_cell *cell = host->pdev->dev.platform_data;
660 struct tmio_mmc_data *pdata = cell->driver_data;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100661 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100662 int ret, i;
663 bool aligned = true, multiple = true;
664 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
665
666 for_each_sg(sg, sg_tmp, host->sg_len, i) {
667 if (sg_tmp->offset & align)
668 aligned = false;
669 if (sg_tmp->length & align) {
670 multiple = false;
671 break;
672 }
673 }
674
675 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000676 align >= MAX_ALIGN)) || !multiple) {
677 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100678 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000679 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100680
681 /* The only sg element can be unaligned, use our bounce buffer then */
682 if (!aligned) {
683 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
684 host->sg_ptr = &host->bounce_sg;
685 sg = host->sg_ptr;
686 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000687
688 ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_FROM_DEVICE);
689 if (ret > 0) {
690 host->dma_sglen = ret;
691 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
692 DMA_FROM_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
693 }
694
695 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000696 desc->callback = tmio_dma_complete;
697 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100698 cookie = desc->tx_submit(desc);
699 if (cookie < 0) {
700 desc = NULL;
701 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000702 } else {
703 chan->device->device_issue_pending(chan);
704 }
705 }
706 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100707 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000708
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100709pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100710 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000711 /* DMA failed, fall back to PIO */
712 if (ret >= 0)
713 ret = -EIO;
714 host->chan_rx = NULL;
715 dma_release_channel(chan);
716 /* Free the Tx channel too */
717 chan = host->chan_tx;
718 if (chan) {
719 host->chan_tx = NULL;
720 dma_release_channel(chan);
721 }
722 dev_warn(&host->pdev->dev,
723 "DMA failed: %d, falling back to PIO\n", ret);
724 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000725 }
726
727 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d, sg[%d]\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100728 desc, cookie, host->sg_len);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000729}
730
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100731static void tmio_mmc_start_dma_tx(struct tmio_mmc_host *host)
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000732{
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100733 struct scatterlist *sg = host->sg_ptr, *sg_tmp;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000734 struct dma_async_tx_descriptor *desc = NULL;
735 struct dma_chan *chan = host->chan_tx;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100736 struct mfd_cell *cell = host->pdev->dev.platform_data;
737 struct tmio_mmc_data *pdata = cell->driver_data;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100738 dma_cookie_t cookie;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100739 int ret, i;
740 bool aligned = true, multiple = true;
741 unsigned int align = (1 << pdata->dma->alignment_shift) - 1;
742
743 for_each_sg(sg, sg_tmp, host->sg_len, i) {
744 if (sg_tmp->offset & align)
745 aligned = false;
746 if (sg_tmp->length & align) {
747 multiple = false;
748 break;
749 }
750 }
751
752 if ((!aligned && (host->sg_len > 1 || sg->length > PAGE_CACHE_SIZE ||
Arnd Hannemanneba46032010-12-19 21:16:07 +0000753 align >= MAX_ALIGN)) || !multiple) {
754 ret = -EINVAL;
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100755 goto pio;
Arnd Hannemanneba46032010-12-19 21:16:07 +0000756 }
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100757
758 /* The only sg element can be unaligned, use our bounce buffer then */
759 if (!aligned) {
760 unsigned long flags;
761 void *sg_vaddr = tmio_mmc_kmap_atomic(sg, &flags);
762 sg_init_one(&host->bounce_sg, host->bounce_buf, sg->length);
763 memcpy(host->bounce_buf, sg_vaddr, host->bounce_sg.length);
764 tmio_mmc_kunmap_atomic(sg_vaddr, &flags);
765 host->sg_ptr = &host->bounce_sg;
766 sg = host->sg_ptr;
767 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000768
769 ret = dma_map_sg(&host->pdev->dev, sg, host->sg_len, DMA_TO_DEVICE);
770 if (ret > 0) {
771 host->dma_sglen = ret;
772 desc = chan->device->device_prep_slave_sg(chan, sg, ret,
773 DMA_TO_DEVICE, DMA_PREP_INTERRUPT | DMA_CTRL_ACK);
774 }
775
776 if (desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000777 desc->callback = tmio_dma_complete;
778 desc->callback_param = host;
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100779 cookie = desc->tx_submit(desc);
780 if (cookie < 0) {
781 desc = NULL;
782 ret = cookie;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000783 }
784 }
785 dev_dbg(&host->pdev->dev, "%s(): mapped %d -> %d, cookie %d, rq %p\n",
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100786 __func__, host->sg_len, ret, cookie, host->mrq);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000787
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100788pio:
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100789 if (!desc) {
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000790 /* DMA failed, fall back to PIO */
791 if (ret >= 0)
792 ret = -EIO;
793 host->chan_tx = NULL;
794 dma_release_channel(chan);
795 /* Free the Rx channel too */
796 chan = host->chan_rx;
797 if (chan) {
798 host->chan_rx = NULL;
799 dma_release_channel(chan);
800 }
801 dev_warn(&host->pdev->dev,
802 "DMA failed: %d, falling back to PIO\n", ret);
803 tmio_mmc_enable_dma(host, false);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000804 }
805
806 dev_dbg(&host->pdev->dev, "%s(): desc %p, cookie %d\n", __func__,
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100807 desc, cookie);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000808}
809
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100810static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000811 struct mmc_data *data)
812{
813 if (data->flags & MMC_DATA_READ) {
814 if (host->chan_rx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100815 tmio_mmc_start_dma_rx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000816 } else {
817 if (host->chan_tx)
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100818 tmio_mmc_start_dma_tx(host);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000819 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000820}
821
822static void tmio_issue_tasklet_fn(unsigned long priv)
823{
824 struct tmio_mmc_host *host = (struct tmio_mmc_host *)priv;
825 struct dma_chan *chan = host->chan_tx;
826
827 chan->device->device_issue_pending(chan);
828}
829
830static void tmio_tasklet_fn(unsigned long arg)
831{
832 struct tmio_mmc_host *host = (struct tmio_mmc_host *)arg;
833
834 if (host->data->flags & MMC_DATA_READ)
835 dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
836 DMA_FROM_DEVICE);
837 else
838 dma_unmap_sg(&host->pdev->dev, host->sg_ptr, host->dma_sglen,
839 DMA_TO_DEVICE);
840
841 tmio_mmc_do_data_irq(host);
842}
843
844/* It might be necessary to make filter MFD specific */
845static bool tmio_mmc_filter(struct dma_chan *chan, void *arg)
846{
847 dev_dbg(chan->device->dev, "%s: slave data %p\n", __func__, arg);
848 chan->private = arg;
849 return true;
850}
851
852static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
853 struct tmio_mmc_data *pdata)
854{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000855 /* We can only either use DMA for both Tx and Rx or not use it at all */
856 if (pdata->dma) {
857 dma_cap_mask_t mask;
858
859 dma_cap_zero(mask);
860 dma_cap_set(DMA_SLAVE, mask);
861
862 host->chan_tx = dma_request_channel(mask, tmio_mmc_filter,
863 pdata->dma->chan_priv_tx);
864 dev_dbg(&host->pdev->dev, "%s: TX: got channel %p\n", __func__,
865 host->chan_tx);
866
867 if (!host->chan_tx)
868 return;
869
870 host->chan_rx = dma_request_channel(mask, tmio_mmc_filter,
871 pdata->dma->chan_priv_rx);
872 dev_dbg(&host->pdev->dev, "%s: RX: got channel %p\n", __func__,
873 host->chan_rx);
874
875 if (!host->chan_rx) {
876 dma_release_channel(host->chan_tx);
877 host->chan_tx = NULL;
878 return;
879 }
880
881 tasklet_init(&host->dma_complete, tmio_tasklet_fn, (unsigned long)host);
882 tasklet_init(&host->dma_issue, tmio_issue_tasklet_fn, (unsigned long)host);
883
884 tmio_mmc_enable_dma(host, true);
885 }
886}
887
888static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
889{
890 if (host->chan_tx) {
891 struct dma_chan *chan = host->chan_tx;
892 host->chan_tx = NULL;
893 dma_release_channel(chan);
894 }
895 if (host->chan_rx) {
896 struct dma_chan *chan = host->chan_rx;
897 host->chan_rx = NULL;
898 dma_release_channel(chan);
899 }
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000900}
901#else
Guennadi Liakhovetski93173052010-12-22 12:02:15 +0100902static void tmio_check_bounce_buffer(struct tmio_mmc_host *host)
903{
904}
905
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100906static void tmio_mmc_start_dma(struct tmio_mmc_host *host,
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000907 struct mmc_data *data)
908{
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000909}
910
911static void tmio_mmc_request_dma(struct tmio_mmc_host *host,
912 struct tmio_mmc_data *pdata)
913{
914 host->chan_tx = NULL;
915 host->chan_rx = NULL;
916}
917
918static void tmio_mmc_release_dma(struct tmio_mmc_host *host)
919{
920}
921#endif
922
Ian Molton4a489982008-07-15 16:02:21 +0100923static int tmio_mmc_start_data(struct tmio_mmc_host *host,
924 struct mmc_data *data)
925{
Yusuke Godaf1334fb2010-08-30 11:50:19 +0100926 struct mfd_cell *cell = host->pdev->dev.platform_data;
927 struct tmio_mmc_data *pdata = cell->driver_data;
928
Ian Molton4a489982008-07-15 16:02:21 +0100929 pr_debug("setup data transfer: blocksize %08x nr_blocks %d\n",
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +0000930 data->blksz, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +0100931
Yusuke Godaf1334fb2010-08-30 11:50:19 +0100932 /* Some hardware cannot perform 2 byte requests in 4 bit mode */
933 if (host->mmc->ios.bus_width == MMC_BUS_WIDTH_4) {
934 int blksz_2bytes = pdata->flags & TMIO_MMC_BLKSZ_2BYTES;
935
936 if (data->blksz < 2 || (data->blksz < 4 && !blksz_2bytes)) {
937 pr_err("%s: %d byte block unsupported in 4 bit mode\n",
938 mmc_hostname(host->mmc), data->blksz);
939 return -EINVAL;
940 }
Ian Molton4a489982008-07-15 16:02:21 +0100941 }
942
943 tmio_mmc_init_sg(host, data);
944 host->data = data;
945
946 /* Set transfer length / blocksize */
Philipp Zabel5e746722009-06-04 20:12:32 +0200947 sd_ctrl_write16(host, CTL_SD_XFER_LEN, data->blksz);
948 sd_ctrl_write16(host, CTL_XFER_BLK_COUNT, data->blocks);
Ian Molton4a489982008-07-15 16:02:21 +0100949
Guennadi Liakhovetskief17fee2010-11-11 12:19:47 +0100950 tmio_mmc_start_dma(host, data);
951
952 return 0;
Ian Molton4a489982008-07-15 16:02:21 +0100953}
954
955/* Process requests from the MMC layer */
956static void tmio_mmc_request(struct mmc_host *mmc, struct mmc_request *mrq)
957{
958 struct tmio_mmc_host *host = mmc_priv(mmc);
959 int ret;
960
961 if (host->mrq)
962 pr_debug("request not null\n");
963
964 host->mrq = mrq;
965
966 if (mrq->data) {
967 ret = tmio_mmc_start_data(host, mrq->data);
968 if (ret)
969 goto fail;
970 }
971
972 ret = tmio_mmc_start_command(host, mrq->cmd);
Ian Molton4a489982008-07-15 16:02:21 +0100973 if (!ret)
974 return;
975
976fail:
977 mrq->cmd->error = ret;
978 mmc_request_done(mmc, mrq);
979}
980
981/* Set MMC clock / power.
982 * Note: This controller uses a simple divider scheme therefore it cannot
983 * run a MMC card at full speed (20MHz). The max clock is 24MHz on SD, but as
984 * MMC wont run that fast, it has to be clocked at 12MHz which is the next
985 * slowest setting.
986 */
987static void tmio_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
988{
989 struct tmio_mmc_host *host = mmc_priv(mmc);
Ian Molton4a489982008-07-15 16:02:21 +0100990
991 if (ios->clock)
992 tmio_mmc_set_clock(host, ios->clock);
993
994 /* Power sequence - OFF -> ON -> UP */
995 switch (ios->power_mode) {
996 case MMC_POWER_OFF: /* power down SD bus */
Ian Molton64e88672010-01-06 13:51:48 +0100997 if (host->set_pwr)
998 host->set_pwr(host->pdev, 0);
Ian Molton4a489982008-07-15 16:02:21 +0100999 tmio_mmc_clk_stop(host);
1000 break;
1001 case MMC_POWER_ON: /* power up SD bus */
Ian Molton64e88672010-01-06 13:51:48 +01001002 if (host->set_pwr)
1003 host->set_pwr(host->pdev, 1);
Ian Molton4a489982008-07-15 16:02:21 +01001004 break;
1005 case MMC_POWER_UP: /* start bus clock */
1006 tmio_mmc_clk_start(host);
1007 break;
1008 }
1009
1010 switch (ios->bus_width) {
1011 case MMC_BUS_WIDTH_1:
Philipp Zabel5e746722009-06-04 20:12:32 +02001012 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x80e0);
Ian Molton4a489982008-07-15 16:02:21 +01001013 break;
1014 case MMC_BUS_WIDTH_4:
Philipp Zabel5e746722009-06-04 20:12:32 +02001015 sd_ctrl_write16(host, CTL_SD_MEM_CARD_OPT, 0x00e0);
Ian Molton4a489982008-07-15 16:02:21 +01001016 break;
1017 }
1018
1019 /* Let things settle. delay taken from winCE driver */
1020 udelay(140);
1021}
1022
1023static int tmio_mmc_get_ro(struct mmc_host *mmc)
1024{
1025 struct tmio_mmc_host *host = mmc_priv(mmc);
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +00001026 struct mfd_cell *cell = host->pdev->dev.platform_data;
1027 struct tmio_mmc_data *pdata = cell->driver_data;
Ian Molton4a489982008-07-15 16:02:21 +01001028
Guennadi Liakhovetskiac8fb3e2010-05-19 18:36:02 +00001029 return ((pdata->flags & TMIO_MMC_WRPROTECT_DISABLE) ||
1030 (sd_ctrl_read32(host, CTL_STATUS) & TMIO_STAT_WRPROTECT)) ? 0 : 1;
Ian Molton4a489982008-07-15 16:02:21 +01001031}
1032
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001033static int tmio_mmc_get_cd(struct mmc_host *mmc)
1034{
1035 struct tmio_mmc_host *host = mmc_priv(mmc);
1036 struct mfd_cell *cell = host->pdev->dev.platform_data;
1037 struct tmio_mmc_data *pdata = cell->driver_data;
1038
1039 if (!pdata->get_cd)
1040 return -ENOSYS;
1041 else
1042 return pdata->get_cd(host->pdev);
1043}
1044
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001045static const struct mmc_host_ops tmio_mmc_ops = {
Ian Molton4a489982008-07-15 16:02:21 +01001046 .request = tmio_mmc_request,
1047 .set_ios = tmio_mmc_set_ios,
1048 .get_ro = tmio_mmc_get_ro,
Arnd Hannemann19ca7502010-08-24 17:26:59 +02001049 .get_cd = tmio_mmc_get_cd,
Ian Molton4a489982008-07-15 16:02:21 +01001050};
1051
1052#ifdef CONFIG_PM
1053static int tmio_mmc_suspend(struct platform_device *dev, pm_message_t state)
1054{
1055 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
1056 struct mmc_host *mmc = platform_get_drvdata(dev);
1057 int ret;
1058
Matt Fleming1a13f8f2010-05-26 14:42:08 -07001059 ret = mmc_suspend_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001060
1061 /* Tell MFD core it can disable us now.*/
1062 if (!ret && cell->disable)
1063 cell->disable(dev);
1064
1065 return ret;
1066}
1067
1068static int tmio_mmc_resume(struct platform_device *dev)
1069{
1070 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
1071 struct mmc_host *mmc = platform_get_drvdata(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001072 int ret = 0;
1073
Ian Molton4a489982008-07-15 16:02:21 +01001074 /* Tell the MFD core we are ready to be enabled */
Ian Molton64e88672010-01-06 13:51:48 +01001075 if (cell->resume) {
1076 ret = cell->resume(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001077 if (ret)
1078 goto out;
1079 }
1080
1081 mmc_resume_host(mmc);
1082
1083out:
1084 return ret;
1085}
1086#else
1087#define tmio_mmc_suspend NULL
1088#define tmio_mmc_resume NULL
1089#endif
1090
1091static int __devinit tmio_mmc_probe(struct platform_device *dev)
1092{
1093 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001094 struct tmio_mmc_data *pdata;
Ian Molton64e88672010-01-06 13:51:48 +01001095 struct resource *res_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001096 struct tmio_mmc_host *host;
1097 struct mmc_host *mmc;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001098 int ret = -EINVAL;
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001099 u32 irq_mask = TMIO_MASK_CMD;
Ian Molton4a489982008-07-15 16:02:21 +01001100
Ian Molton64e88672010-01-06 13:51:48 +01001101 if (dev->num_resources != 2)
Ian Molton4a489982008-07-15 16:02:21 +01001102 goto out;
1103
1104 res_ctl = platform_get_resource(dev, IORESOURCE_MEM, 0);
Ian Molton64e88672010-01-06 13:51:48 +01001105 if (!res_ctl)
Ian Molton4a489982008-07-15 16:02:21 +01001106 goto out;
Ian Molton4a489982008-07-15 16:02:21 +01001107
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001108 pdata = cell->driver_data;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001109 if (!pdata || !pdata->hclk)
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001110 goto out;
Philipp Zabeld6c9b5e2009-06-04 20:12:34 +02001111
1112 ret = -ENOMEM;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001113
Ian Molton4a489982008-07-15 16:02:21 +01001114 mmc = mmc_alloc_host(sizeof(struct tmio_mmc_host), &dev->dev);
1115 if (!mmc)
1116 goto out;
1117
1118 host = mmc_priv(mmc);
1119 host->mmc = mmc;
Ian Molton64e88672010-01-06 13:51:48 +01001120 host->pdev = dev;
Ian Molton4a489982008-07-15 16:02:21 +01001121 platform_set_drvdata(dev, mmc);
1122
Ian Molton64e88672010-01-06 13:51:48 +01001123 host->set_pwr = pdata->set_pwr;
1124 host->set_clk_div = pdata->set_clk_div;
1125
Philipp Zabel5e746722009-06-04 20:12:32 +02001126 /* SD control register space size is 0x200, 0x400 for bus_shift=1 */
1127 host->bus_shift = resource_size(res_ctl) >> 10;
1128
Magnus Dammbc6772a2009-03-11 21:58:54 +09001129 host->ctl = ioremap(res_ctl->start, resource_size(res_ctl));
Ian Molton4a489982008-07-15 16:02:21 +01001130 if (!host->ctl)
1131 goto host_free;
1132
Ian Molton4a489982008-07-15 16:02:21 +01001133 mmc->ops = &tmio_mmc_ops;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001134 mmc->caps = MMC_CAP_4_BIT_DATA | pdata->capabilities;
Philipp Zabelf0e46cc2009-06-04 20:12:31 +02001135 mmc->f_max = pdata->hclk;
1136 mmc->f_min = mmc->f_max / 512;
Guennadi Liakhovetski729b0c72010-11-11 12:15:06 +01001137 mmc->max_segs = 32;
1138 mmc->max_blk_size = 512;
1139 mmc->max_blk_count = (PAGE_CACHE_SIZE / mmc->max_blk_size) *
1140 mmc->max_segs;
1141 mmc->max_req_size = mmc->max_blk_size * mmc->max_blk_count;
1142 mmc->max_seg_size = mmc->max_req_size;
Guennadi Liakhovetskia2b14dc2010-05-19 18:37:25 +00001143 if (pdata->ocr_mask)
1144 mmc->ocr_avail = pdata->ocr_mask;
1145 else
1146 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Ian Molton4a489982008-07-15 16:02:21 +01001147
Ian Molton4a489982008-07-15 16:02:21 +01001148 /* Tell the MFD core we are ready to be enabled */
1149 if (cell->enable) {
1150 ret = cell->enable(dev);
1151 if (ret)
Ian Molton64e88672010-01-06 13:51:48 +01001152 goto unmap_ctl;
Ian Molton4a489982008-07-15 16:02:21 +01001153 }
1154
Ian Molton4a489982008-07-15 16:02:21 +01001155 tmio_mmc_clk_stop(host);
1156 reset(host);
1157
1158 ret = platform_get_irq(dev, 0);
1159 if (ret >= 0)
1160 host->irq = ret;
1161 else
Magnus Damm7ee422d2010-02-17 16:38:23 +09001162 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001163
Philipp Zabel5e746722009-06-04 20:12:32 +02001164 disable_mmc_irqs(host, TMIO_MASK_ALL);
Ian Molton4a489982008-07-15 16:02:21 +01001165
Philipp Zabel6c413cc2009-06-04 20:12:33 +02001166 ret = request_irq(host->irq, tmio_mmc_irq, IRQF_DISABLED |
Magnus Damm14f1b752009-12-14 18:01:33 -08001167 IRQF_TRIGGER_FALLING, dev_name(&dev->dev), host);
Ian Molton4a489982008-07-15 16:02:21 +01001168 if (ret)
Magnus Damm7ee422d2010-02-17 16:38:23 +09001169 goto cell_disable;
Ian Molton4a489982008-07-15 16:02:21 +01001170
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001171 /* See if we also get DMA */
1172 tmio_mmc_request_dma(host, pdata);
1173
Ian Molton4a489982008-07-15 16:02:21 +01001174 mmc_add_host(mmc);
1175
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001176 pr_info("%s at 0x%08lx irq %d\n", mmc_hostname(host->mmc),
1177 (unsigned long)host->ctl, host->irq);
Ian Molton4a489982008-07-15 16:02:21 +01001178
1179 /* Unmask the IRQs we want to know about */
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001180 if (!host->chan_rx)
1181 irq_mask |= TMIO_MASK_READOP;
1182 if (!host->chan_tx)
1183 irq_mask |= TMIO_MASK_WRITEOP;
1184 enable_mmc_irqs(host, irq_mask);
Ian Molton4a489982008-07-15 16:02:21 +01001185
1186 return 0;
1187
Magnus Damm7ee422d2010-02-17 16:38:23 +09001188cell_disable:
1189 if (cell->disable)
1190 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001191unmap_ctl:
1192 iounmap(host->ctl);
1193host_free:
1194 mmc_free_host(mmc);
1195out:
1196 return ret;
1197}
1198
1199static int __devexit tmio_mmc_remove(struct platform_device *dev)
1200{
Magnus Damm7ee422d2010-02-17 16:38:23 +09001201 struct mfd_cell *cell = (struct mfd_cell *)dev->dev.platform_data;
Ian Molton4a489982008-07-15 16:02:21 +01001202 struct mmc_host *mmc = platform_get_drvdata(dev);
1203
1204 platform_set_drvdata(dev, NULL);
1205
1206 if (mmc) {
1207 struct tmio_mmc_host *host = mmc_priv(mmc);
1208 mmc_remove_host(mmc);
Guennadi Liakhovetski311f3ac2010-05-19 18:34:22 +00001209 tmio_mmc_release_dma(host);
Ian Molton4a489982008-07-15 16:02:21 +01001210 free_irq(host->irq, host);
Magnus Damm7ee422d2010-02-17 16:38:23 +09001211 if (cell->disable)
1212 cell->disable(dev);
Ian Molton4a489982008-07-15 16:02:21 +01001213 iounmap(host->ctl);
Magnus Dammbedcc452009-03-11 21:59:03 +09001214 mmc_free_host(mmc);
Ian Molton4a489982008-07-15 16:02:21 +01001215 }
1216
1217 return 0;
1218}
1219
1220/* ------------------- device registration ----------------------- */
1221
1222static struct platform_driver tmio_mmc_driver = {
1223 .driver = {
1224 .name = "tmio-mmc",
1225 .owner = THIS_MODULE,
1226 },
1227 .probe = tmio_mmc_probe,
1228 .remove = __devexit_p(tmio_mmc_remove),
1229 .suspend = tmio_mmc_suspend,
1230 .resume = tmio_mmc_resume,
1231};
1232
1233
1234static int __init tmio_mmc_init(void)
1235{
1236 return platform_driver_register(&tmio_mmc_driver);
1237}
1238
1239static void __exit tmio_mmc_exit(void)
1240{
1241 platform_driver_unregister(&tmio_mmc_driver);
1242}
1243
1244module_init(tmio_mmc_init);
1245module_exit(tmio_mmc_exit);
1246
1247MODULE_DESCRIPTION("Toshiba TMIO SD/MMC driver");
1248MODULE_AUTHOR("Ian Molton <spyro@f2s.com>");
1249MODULE_LICENSE("GPL v2");
1250MODULE_ALIAS("platform:tmio-mmc");