blob: 0af972275d45acd47d6646c1b0e1677ae1248929 [file] [log] [blame]
Thomas Kleffelbe518012008-06-30 22:40:24 +01001/*
2 * linux/drivers/mmc/s3cmci.h - Samsung S3C MCI driver
3 *
4 * Copyright (C) 2004-2006 maintech GmbH, Thomas Kleffel <tk@maintech.de>
5 *
ben@fluff.org.uk08c55e22008-10-15 00:17:19 +01006 * Current driver maintained by Ben Dooks and Simtec Electronics
7 * Copyright (C) 2008 Simtec Electronics <ben-linux@fluff.org>
8 *
Thomas Kleffelbe518012008-06-30 22:40:24 +01009 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License version 2 as
11 * published by the Free Software Foundation.
12 */
13
14#include <linux/module.h>
15#include <linux/dma-mapping.h>
16#include <linux/clk.h>
17#include <linux/mmc/host.h>
18#include <linux/platform_device.h>
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +010019#include <linux/cpufreq.h>
Ben Dooks9bdd2032009-10-01 15:44:17 -070020#include <linux/debugfs.h>
21#include <linux/seq_file.h>
Ben Dooksec976d62009-05-13 22:52:24 +010022#include <linux/gpio.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010023#include <linux/irq.h>
24#include <linux/io.h>
25
Ramax Lo7ad14f82009-01-14 02:13:47 +010026#include <mach/dma.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010027
Russell Kinga09e64f2008-08-05 16:14:15 +010028#include <mach/regs-sdi.h>
29#include <mach/regs-gpio.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010030
Ben Dookse3bd9ec2008-10-30 10:14:39 +000031#include <plat/mci.h>
Ben Dooksedb5a982008-06-30 22:40:29 +010032
Thomas Kleffelbe518012008-06-30 22:40:24 +010033#include "s3cmci.h"
34
35#define DRIVER_NAME "s3c-mci"
36
37enum dbg_channels {
38 dbg_err = (1 << 0),
39 dbg_debug = (1 << 1),
40 dbg_info = (1 << 2),
41 dbg_irq = (1 << 3),
42 dbg_sg = (1 << 4),
43 dbg_dma = (1 << 5),
44 dbg_pio = (1 << 6),
45 dbg_fail = (1 << 7),
46 dbg_conf = (1 << 8),
47};
48
ben@fluff.org.uk9c2e7e42008-10-15 00:17:15 +010049static const int dbgmap_err = dbg_fail;
Thomas Kleffelbe518012008-06-30 22:40:24 +010050static const int dbgmap_info = dbg_info | dbg_conf;
ben@fluff.org.uk9c2e7e42008-10-15 00:17:15 +010051static const int dbgmap_debug = dbg_err | dbg_debug;
Thomas Kleffelbe518012008-06-30 22:40:24 +010052
53#define dbg(host, channels, args...) \
54 do { \
55 if (dbgmap_err & channels) \
56 dev_err(&host->pdev->dev, args); \
57 else if (dbgmap_info & channels) \
58 dev_info(&host->pdev->dev, args); \
59 else if (dbgmap_debug & channels) \
60 dev_dbg(&host->pdev->dev, args); \
61 } while (0)
62
Thomas Kleffelbe518012008-06-30 22:40:24 +010063static struct s3c2410_dma_client s3cmci_dma_client = {
64 .name = "s3c-mci",
65};
66
67static void finalize_request(struct s3cmci_host *host);
68static void s3cmci_send_request(struct mmc_host *mmc);
69static void s3cmci_reset(struct s3cmci_host *host);
70
71#ifdef CONFIG_MMC_DEBUG
72
73static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
74{
75 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
76 u32 datcon, datcnt, datsta, fsta, imask;
77
78 con = readl(host->base + S3C2410_SDICON);
79 pre = readl(host->base + S3C2410_SDIPRE);
80 cmdarg = readl(host->base + S3C2410_SDICMDARG);
81 cmdcon = readl(host->base + S3C2410_SDICMDCON);
82 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
83 r0 = readl(host->base + S3C2410_SDIRSP0);
84 r1 = readl(host->base + S3C2410_SDIRSP1);
85 r2 = readl(host->base + S3C2410_SDIRSP2);
86 r3 = readl(host->base + S3C2410_SDIRSP3);
87 timer = readl(host->base + S3C2410_SDITIMER);
88 bsize = readl(host->base + S3C2410_SDIBSIZE);
89 datcon = readl(host->base + S3C2410_SDIDCON);
90 datcnt = readl(host->base + S3C2410_SDIDCNT);
91 datsta = readl(host->base + S3C2410_SDIDSTA);
92 fsta = readl(host->base + S3C2410_SDIFSTA);
93 imask = readl(host->base + host->sdiimsk);
94
95 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
96 prefix, con, pre, timer);
97
98 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
99 prefix, cmdcon, cmdarg, cmdsta);
100
101 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
102 " DSTA:[%08x] DCNT:[%08x]\n",
103 prefix, datcon, fsta, datsta, datcnt);
104
105 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
106 " R2:[%08x] R3:[%08x]\n",
107 prefix, r0, r1, r2, r3);
108}
109
110static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
111 int stop)
112{
113 snprintf(host->dbgmsg_cmd, 300,
114 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
115 host->ccnt, (stop ? " (STOP)" : ""),
116 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
117
118 if (cmd->data) {
119 snprintf(host->dbgmsg_dat, 300,
120 "#%u bsize:%u blocks:%u bytes:%u",
121 host->dcnt, cmd->data->blksz,
122 cmd->data->blocks,
123 cmd->data->blocks * cmd->data->blksz);
124 } else {
125 host->dbgmsg_dat[0] = '\0';
126 }
127}
128
129static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
130 int fail)
131{
132 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
133
134 if (!cmd)
135 return;
136
137 if (cmd->error == 0) {
138 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
139 host->dbgmsg_cmd, cmd->resp[0]);
140 } else {
141 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
142 cmd->error, host->dbgmsg_cmd, host->status);
143 }
144
145 if (!cmd->data)
146 return;
147
148 if (cmd->data->error == 0) {
149 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
150 } else {
151 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
152 cmd->data->error, host->dbgmsg_dat,
153 readl(host->base + S3C2410_SDIDCNT));
154 }
155}
156#else
157static void dbg_dumpcmd(struct s3cmci_host *host,
158 struct mmc_command *cmd, int fail) { }
159
160static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
161 int stop) { }
162
163static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
164
165#endif /* CONFIG_MMC_DEBUG */
166
Ben Dooks26f14942009-10-01 15:44:18 -0700167/**
168 * s3cmci_host_usedma - return whether the host is using dma or pio
169 * @host: The host state
170 *
171 * Return true if the host is using DMA to transfer data, else false
172 * to use PIO mode. Will return static data depending on the driver
173 * configuration.
174 */
175static inline bool s3cmci_host_usedma(struct s3cmci_host *host)
176{
177#ifdef CONFIG_MMC_S3C_PIO
178 return false;
179#elif defined(CONFIG_MMC_S3C_DMA)
180 return true;
181#else
182 return host->dodma;
183#endif
184}
185
Ben Dooks68c5ed52009-10-01 15:44:19 -0700186/**
187 * s3cmci_host_canpio - return true if host has pio code available
188 *
189 * Return true if the driver has been compiled with the PIO support code
190 * available.
191 */
192static inline bool s3cmci_host_canpio(void)
193{
194#ifdef CONFIG_MMC_S3C_PIO
195 return true;
196#else
197 return false;
198#endif
199}
200
Thomas Kleffelbe518012008-06-30 22:40:24 +0100201static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
202{
203 u32 newmask;
204
205 newmask = readl(host->base + host->sdiimsk);
206 newmask |= imask;
207
208 writel(newmask, host->base + host->sdiimsk);
209
210 return newmask;
211}
212
213static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
214{
215 u32 newmask;
216
217 newmask = readl(host->base + host->sdiimsk);
218 newmask &= ~imask;
219
220 writel(newmask, host->base + host->sdiimsk);
221
222 return newmask;
223}
224
225static inline void clear_imask(struct s3cmci_host *host)
226{
Ben Dooksc2258892009-10-01 15:44:18 -0700227 u32 mask = readl(host->base + host->sdiimsk);
228
229 /* preserve the SDIO IRQ mask state */
230 mask &= S3C2410_SDIIMSK_SDIOIRQ;
231 writel(mask, host->base + host->sdiimsk);
232}
233
234/**
235 * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
236 * @host: The host to check.
237 *
238 * Test to see if the SDIO interrupt is being signalled in case the
239 * controller has failed to re-detect a card interrupt. Read GPE8 and
240 * see if it is low and if so, signal a SDIO interrupt.
241 *
242 * This is currently called if a request is finished (we assume that the
243 * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
244 * already being indicated.
245*/
246static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
247{
248 if (host->sdio_irqen) {
249 if (gpio_get_value(S3C2410_GPE(8)) == 0) {
250 printk(KERN_DEBUG "%s: signalling irq\n", __func__);
251 mmc_signal_sdio_irq(host->mmc);
252 }
253 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100254}
255
256static inline int get_data_buffer(struct s3cmci_host *host,
Christer Weinigel088a78a2008-10-15 00:17:17 +0100257 u32 *bytes, u32 **pointer)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100258{
259 struct scatterlist *sg;
260
261 if (host->pio_active == XFER_NONE)
262 return -EINVAL;
263
264 if ((!host->mrq) || (!host->mrq->data))
265 return -EINVAL;
266
267 if (host->pio_sgptr >= host->mrq->data->sg_len) {
268 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
269 host->pio_sgptr, host->mrq->data->sg_len);
270 return -EBUSY;
271 }
272 sg = &host->mrq->data->sg[host->pio_sgptr];
273
Christer Weinigel088a78a2008-10-15 00:17:17 +0100274 *bytes = sg->length;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100275 *pointer = sg_virt(sg);
276
277 host->pio_sgptr++;
278
279 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
280 host->pio_sgptr, host->mrq->data->sg_len);
281
282 return 0;
283}
284
285static inline u32 fifo_count(struct s3cmci_host *host)
286{
287 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
288
289 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100290 return fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100291}
292
293static inline u32 fifo_free(struct s3cmci_host *host)
294{
295 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
296
297 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100298 return 63 - fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100299}
300
Ben Dooksc2258892009-10-01 15:44:18 -0700301/**
302 * s3cmci_enable_irq - enable IRQ, after having disabled it.
303 * @host: The device state.
304 * @more: True if more IRQs are expected from transfer.
305 *
306 * Enable the main IRQ if needed after it has been disabled.
307 *
308 * The IRQ can be one of the following states:
309 * - disabled during IDLE
310 * - disabled whilst processing data
311 * - enabled during transfer
312 * - enabled whilst awaiting SDIO interrupt detection
313 */
314static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
315{
316 unsigned long flags;
317 bool enable = false;
318
319 local_irq_save(flags);
320
321 host->irq_enabled = more;
322 host->irq_disabled = false;
323
324 enable = more | host->sdio_irqen;
325
326 if (host->irq_state != enable) {
327 host->irq_state = enable;
328
329 if (enable)
330 enable_irq(host->irq);
331 else
332 disable_irq(host->irq);
333 }
334
335 local_irq_restore(flags);
336}
337
338/**
339 *
340 */
341static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
342{
343 unsigned long flags;
344
345 local_irq_save(flags);
346
347 //printk(KERN_DEBUG "%s: transfer %d\n", __func__, transfer);
348
349 host->irq_disabled = transfer;
350
351 if (transfer && host->irq_state) {
352 host->irq_state = false;
353 disable_irq(host->irq);
354 }
355
356 local_irq_restore(flags);
357}
358
Thomas Kleffelbe518012008-06-30 22:40:24 +0100359static void do_pio_read(struct s3cmci_host *host)
360{
361 int res;
362 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100363 u32 *ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100364 u32 fifo_words;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100365 void __iomem *from_ptr;
366
367 /* write real prescaler to host, it might be set slow to fix */
368 writel(host->prescaler, host->base + S3C2410_SDIPRE);
369
370 from_ptr = host->base + host->sdidata;
371
372 while ((fifo = fifo_count(host))) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100373 if (!host->pio_bytes) {
374 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100375 &host->pio_ptr);
376 if (res) {
377 host->pio_active = XFER_NONE;
378 host->complete_what = COMPLETION_FINALIZE;
379
380 dbg(host, dbg_pio, "pio_read(): "
381 "complete (no more data).\n");
382 return;
383 }
384
385 dbg(host, dbg_pio,
386 "pio_read(): new target: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100387 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100388 }
389
390 dbg(host, dbg_pio,
391 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100392 fifo, host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100393 readl(host->base + S3C2410_SDIDCNT));
394
Christer Weinigel088a78a2008-10-15 00:17:17 +0100395 /* If we have reached the end of the block, we can
396 * read a word and get 1 to 3 bytes. If we in the
397 * middle of the block, we have to read full words,
398 * otherwise we will write garbage, so round down to
399 * an even multiple of 4. */
400 if (fifo >= host->pio_bytes)
401 fifo = host->pio_bytes;
402 else
403 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100404
Christer Weinigel088a78a2008-10-15 00:17:17 +0100405 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100406 host->pio_count += fifo;
407
Christer Weinigel088a78a2008-10-15 00:17:17 +0100408 fifo_words = fifo >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100409 ptr = host->pio_ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100410 while (fifo_words--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100411 *ptr++ = readl(from_ptr);
412 host->pio_ptr = ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100413
414 if (fifo & 3) {
415 u32 n = fifo & 3;
416 u32 data = readl(from_ptr);
417 u8 *p = (u8 *)host->pio_ptr;
418
419 while (n--) {
420 *p++ = data;
421 data >>= 8;
422 }
423 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100424 }
425
Christer Weinigel088a78a2008-10-15 00:17:17 +0100426 if (!host->pio_bytes) {
427 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100428 if (res) {
429 dbg(host, dbg_pio,
430 "pio_read(): complete (no more buffers).\n");
431 host->pio_active = XFER_NONE;
432 host->complete_what = COMPLETION_FINALIZE;
433
434 return;
435 }
436 }
437
438 enable_imask(host,
439 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
440}
441
442static void do_pio_write(struct s3cmci_host *host)
443{
444 void __iomem *to_ptr;
445 int res;
446 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100447 u32 *ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100448
449 to_ptr = host->base + host->sdidata;
450
Yauhen Kharuzhy99424482009-02-11 13:25:52 -0800451 while ((fifo = fifo_free(host)) > 3) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100452 if (!host->pio_bytes) {
453 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100454 &host->pio_ptr);
455 if (res) {
456 dbg(host, dbg_pio,
457 "pio_write(): complete (no more data).\n");
458 host->pio_active = XFER_NONE;
459
460 return;
461 }
462
463 dbg(host, dbg_pio,
464 "pio_write(): new source: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100465 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100466
467 }
468
Christer Weinigel088a78a2008-10-15 00:17:17 +0100469 /* If we have reached the end of the block, we have to
470 * write exactly the remaining number of bytes. If we
471 * in the middle of the block, we have to write full
472 * words, so round down to an even multiple of 4. */
473 if (fifo >= host->pio_bytes)
474 fifo = host->pio_bytes;
475 else
476 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100477
Christer Weinigel088a78a2008-10-15 00:17:17 +0100478 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100479 host->pio_count += fifo;
480
Christer Weinigel088a78a2008-10-15 00:17:17 +0100481 fifo = (fifo + 3) >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100482 ptr = host->pio_ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100483 while (fifo--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100484 writel(*ptr++, to_ptr);
485 host->pio_ptr = ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100486 }
487
488 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
489}
490
491static void pio_tasklet(unsigned long data)
492{
493 struct s3cmci_host *host = (struct s3cmci_host *) data;
494
Ben Dooksc2258892009-10-01 15:44:18 -0700495 s3cmci_disable_irq(host, true);
Roman Moracikd643b5f2008-06-30 22:40:28 +0100496
Thomas Kleffelbe518012008-06-30 22:40:24 +0100497 if (host->pio_active == XFER_WRITE)
498 do_pio_write(host);
499
500 if (host->pio_active == XFER_READ)
501 do_pio_read(host);
502
503 if (host->complete_what == COMPLETION_FINALIZE) {
504 clear_imask(host);
505 if (host->pio_active != XFER_NONE) {
506 dbg(host, dbg_err, "unfinished %s "
Christer Weinigel088a78a2008-10-15 00:17:17 +0100507 "- pio_count:[%u] pio_bytes:[%u]\n",
Thomas Kleffelbe518012008-06-30 22:40:24 +0100508 (host->pio_active == XFER_READ) ? "read" : "write",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100509 host->pio_count, host->pio_bytes);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100510
Ben Dooks7c144502008-06-30 22:40:31 +0100511 if (host->mrq->data)
512 host->mrq->data->error = -EINVAL;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100513 }
514
Ben Dooksc2258892009-10-01 15:44:18 -0700515 s3cmci_enable_irq(host, false);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100516 finalize_request(host);
Roman Moracikd643b5f2008-06-30 22:40:28 +0100517 } else
Ben Dooksc2258892009-10-01 15:44:18 -0700518 s3cmci_enable_irq(host, true);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100519}
520
521/*
522 * ISR for SDI Interface IRQ
523 * Communication between driver and ISR works as follows:
524 * host->mrq points to current request
525 * host->complete_what Indicates when the request is considered done
526 * COMPLETION_CMDSENT when the command was sent
527 * COMPLETION_RSPFIN when a response was received
528 * COMPLETION_XFERFINISH when the data transfer is finished
529 * COMPLETION_XFERFINISH_RSPFIN both of the above.
530 * host->complete_request is the completion-object the driver waits for
531 *
532 * 1) Driver sets up host->mrq and host->complete_what
533 * 2) Driver prepares the transfer
534 * 3) Driver enables interrupts
535 * 4) Driver starts transfer
536 * 5) Driver waits for host->complete_rquest
537 * 6) ISR checks for request status (errors and success)
538 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
539 * 7) ISR completes host->complete_request
540 * 8) ISR disables interrupts
541 * 9) Driver wakes up and takes care of the request
542 *
543 * Note: "->error"-fields are expected to be set to 0 before the request
544 * was issued by mmc.c - therefore they are only set, when an error
545 * contition comes up
546 */
547
548static irqreturn_t s3cmci_irq(int irq, void *dev_id)
549{
550 struct s3cmci_host *host = dev_id;
551 struct mmc_command *cmd;
552 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
Ben Dooksc2258892009-10-01 15:44:18 -0700553 u32 mci_cclear = 0, mci_dclear;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100554 unsigned long iflags;
555
Ben Dooksc2258892009-10-01 15:44:18 -0700556 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
557 mci_imsk = readl(host->base + host->sdiimsk);
558
559 if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
560 if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
561 mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
562 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
563
564 mmc_signal_sdio_irq(host->mmc);
565 return IRQ_HANDLED;
566 }
567 }
568
Thomas Kleffelbe518012008-06-30 22:40:24 +0100569 spin_lock_irqsave(&host->complete_lock, iflags);
570
571 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100572 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
573 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100574 mci_dclear = 0;
575
576 if ((host->complete_what == COMPLETION_NONE) ||
577 (host->complete_what == COMPLETION_FINALIZE)) {
578 host->status = "nothing to complete";
579 clear_imask(host);
580 goto irq_out;
581 }
582
583 if (!host->mrq) {
584 host->status = "no active mrq";
585 clear_imask(host);
586 goto irq_out;
587 }
588
589 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
590
591 if (!cmd) {
592 host->status = "no active cmd";
593 clear_imask(host);
594 goto irq_out;
595 }
596
Ben Dooks26f14942009-10-01 15:44:18 -0700597 if (!s3cmci_host_usedma(host)) {
Thomas Kleffelbe518012008-06-30 22:40:24 +0100598 if ((host->pio_active == XFER_WRITE) &&
599 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
600
601 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
602 tasklet_schedule(&host->pio_tasklet);
603 host->status = "pio tx";
604 }
605
606 if ((host->pio_active == XFER_READ) &&
607 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
608
609 disable_imask(host,
610 S3C2410_SDIIMSK_RXFIFOHALF |
611 S3C2410_SDIIMSK_RXFIFOLAST);
612
613 tasklet_schedule(&host->pio_tasklet);
614 host->status = "pio rx";
615 }
616 }
617
618 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
Ben Dooksff8c804f2008-06-30 22:40:37 +0100619 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100620 cmd->error = -ETIMEDOUT;
621 host->status = "error: command timeout";
622 goto fail_transfer;
623 }
624
625 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
626 if (host->complete_what == COMPLETION_CMDSENT) {
627 host->status = "ok: command sent";
628 goto close_transfer;
629 }
630
631 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
632 }
633
634 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
635 if (cmd->flags & MMC_RSP_CRC) {
Harald Welte679f0f82008-06-30 22:40:25 +0100636 if (host->mrq->cmd->flags & MMC_RSP_136) {
637 dbg(host, dbg_irq,
638 "fixup: ignore CRC fail with long rsp\n");
639 } else {
640 /* note, we used to fail the transfer
641 * here, but it seems that this is just
642 * the hardware getting it wrong.
643 *
644 * cmd->error = -EILSEQ;
645 * host->status = "error: bad command crc";
646 * goto fail_transfer;
647 */
648 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100649 }
650
651 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
652 }
653
654 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
655 if (host->complete_what == COMPLETION_RSPFIN) {
656 host->status = "ok: command response received";
657 goto close_transfer;
658 }
659
660 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
661 host->complete_what = COMPLETION_XFERFINISH;
662
663 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
664 }
665
666 /* errors handled after this point are only relevant
667 when a data transfer is in progress */
668
669 if (!cmd->data)
670 goto clear_status_bits;
671
672 /* Check for FIFO failure */
673 if (host->is2440) {
674 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
Ben Dooksff8c804f2008-06-30 22:40:37 +0100675 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100676 host->mrq->data->error = -EILSEQ;
677 host->status = "error: 2440 fifo failure";
678 goto fail_transfer;
679 }
680 } else {
681 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
Ben Dooksff8c804f2008-06-30 22:40:37 +0100682 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100683 cmd->data->error = -EILSEQ;
684 host->status = "error: fifo failure";
685 goto fail_transfer;
686 }
687 }
688
689 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
Ben Dooksff8c804f2008-06-30 22:40:37 +0100690 dbg(host, dbg_err, "bad data crc (outgoing)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100691 cmd->data->error = -EILSEQ;
692 host->status = "error: bad data crc (outgoing)";
693 goto fail_transfer;
694 }
695
696 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
Ben Dooksff8c804f2008-06-30 22:40:37 +0100697 dbg(host, dbg_err, "bad data crc (incoming)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100698 cmd->data->error = -EILSEQ;
699 host->status = "error: bad data crc (incoming)";
700 goto fail_transfer;
701 }
702
703 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
Ben Dooksff8c804f2008-06-30 22:40:37 +0100704 dbg(host, dbg_err, "data timeout\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100705 cmd->data->error = -ETIMEDOUT;
706 host->status = "error: data timeout";
707 goto fail_transfer;
708 }
709
710 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
711 if (host->complete_what == COMPLETION_XFERFINISH) {
712 host->status = "ok: data transfer completed";
713 goto close_transfer;
714 }
715
716 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
717 host->complete_what = COMPLETION_RSPFIN;
718
719 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
720 }
721
722clear_status_bits:
723 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
724 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
725
726 goto irq_out;
727
728fail_transfer:
729 host->pio_active = XFER_NONE;
730
731close_transfer:
732 host->complete_what = COMPLETION_FINALIZE;
733
734 clear_imask(host);
735 tasklet_schedule(&host->pio_tasklet);
736
737 goto irq_out;
738
739irq_out:
740 dbg(host, dbg_irq,
741 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
742 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
743
744 spin_unlock_irqrestore(&host->complete_lock, iflags);
745 return IRQ_HANDLED;
746
747}
748
749/*
750 * ISR for the CardDetect Pin
751*/
752
753static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
754{
755 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
756
757 dbg(host, dbg_irq, "card detect\n");
758
Ben Dooks2de5f792008-06-30 22:40:35 +0100759 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
Thomas Kleffelbe518012008-06-30 22:40:24 +0100760
761 return IRQ_HANDLED;
762}
763
Ben Dooks5d304402008-08-08 10:55:41 +0100764static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
765 void *buf_id, int size,
766 enum s3c2410_dma_buffresult result)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100767{
768 struct s3cmci_host *host = buf_id;
769 unsigned long iflags;
770 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
771
772 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
773 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
774 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
775 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
776
777 BUG_ON(!host->mrq);
778 BUG_ON(!host->mrq->data);
779 BUG_ON(!host->dmatogo);
780
781 spin_lock_irqsave(&host->complete_lock, iflags);
782
783 if (result != S3C2410_RES_OK) {
784 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
785 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
786 mci_csta, mci_dsta, mci_fsta,
787 mci_dcnt, result, host->dmatogo);
788
789 goto fail_request;
790 }
791
792 host->dmatogo--;
793 if (host->dmatogo) {
794 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
795 "DCNT:[%08x] toGo:%u\n",
796 size, mci_dsta, mci_dcnt, host->dmatogo);
797
798 goto out;
799 }
800
801 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
802 size, mci_dsta, mci_dcnt);
803
Ben Dooks68c5ed52009-10-01 15:44:19 -0700804 host->dma_complete = 1;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100805 host->complete_what = COMPLETION_FINALIZE;
806
807out:
808 tasklet_schedule(&host->pio_tasklet);
809 spin_unlock_irqrestore(&host->complete_lock, iflags);
810 return;
811
Thomas Kleffelbe518012008-06-30 22:40:24 +0100812fail_request:
813 host->mrq->data->error = -EINVAL;
814 host->complete_what = COMPLETION_FINALIZE;
Ben Dookse6130ae2009-10-01 15:44:16 -0700815 clear_imask(host);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100816
Ben Dookse6130ae2009-10-01 15:44:16 -0700817 goto out;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100818}
819
820static void finalize_request(struct s3cmci_host *host)
821{
822 struct mmc_request *mrq = host->mrq;
823 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
824 int debug_as_failure = 0;
825
826 if (host->complete_what != COMPLETION_FINALIZE)
827 return;
828
829 if (!mrq)
830 return;
831
832 if (cmd->data && (cmd->error == 0) &&
833 (cmd->data->error == 0)) {
Ben Dooks26f14942009-10-01 15:44:18 -0700834 if (s3cmci_host_usedma(host) && (!host->dma_complete)) {
Ben Dooks68c5ed52009-10-01 15:44:19 -0700835 dbg(host, dbg_dma, "DMA Missing (%d)!\n",
836 host->dma_complete);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100837 return;
838 }
839 }
840
841 /* Read response from controller. */
842 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
843 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
844 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
845 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
846
847 writel(host->prescaler, host->base + S3C2410_SDIPRE);
848
849 if (cmd->error)
850 debug_as_failure = 1;
851
852 if (cmd->data && cmd->data->error)
853 debug_as_failure = 1;
854
855 dbg_dumpcmd(host, cmd, debug_as_failure);
856
857 /* Cleanup controller */
858 writel(0, host->base + S3C2410_SDICMDARG);
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100859 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100860 writel(0, host->base + S3C2410_SDICMDCON);
Ben Dookse6130ae2009-10-01 15:44:16 -0700861 clear_imask(host);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100862
863 if (cmd->data && cmd->error)
864 cmd->data->error = cmd->error;
865
866 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
867 host->cmd_is_stop = 1;
868 s3cmci_send_request(host->mmc);
869 return;
870 }
871
872 /* If we have no data transfer we are finished here */
873 if (!mrq->data)
874 goto request_done;
875
876 /* Calulate the amout of bytes transfer if there was no error */
877 if (mrq->data->error == 0) {
878 mrq->data->bytes_xfered =
879 (mrq->data->blocks * mrq->data->blksz);
880 } else {
881 mrq->data->bytes_xfered = 0;
882 }
883
884 /* If we had an error while transfering data we flush the
885 * DMA channel and the fifo to clear out any garbage. */
886 if (mrq->data->error != 0) {
Ben Dooks26f14942009-10-01 15:44:18 -0700887 if (s3cmci_host_usedma(host))
Thomas Kleffelbe518012008-06-30 22:40:24 +0100888 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
889
890 if (host->is2440) {
891 /* Clear failure register and reset fifo. */
892 writel(S3C2440_SDIFSTA_FIFORESET |
893 S3C2440_SDIFSTA_FIFOFAIL,
894 host->base + S3C2410_SDIFSTA);
895 } else {
896 u32 mci_con;
897
898 /* reset fifo */
899 mci_con = readl(host->base + S3C2410_SDICON);
900 mci_con |= S3C2410_SDICON_FIFORESET;
901
902 writel(mci_con, host->base + S3C2410_SDICON);
903 }
904 }
905
906request_done:
907 host->complete_what = COMPLETION_NONE;
908 host->mrq = NULL;
Ben Dooksc2258892009-10-01 15:44:18 -0700909
910 s3cmci_check_sdio_irq(host);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100911 mmc_request_done(host->mmc, mrq);
912}
913
Ben Dooks5d304402008-08-08 10:55:41 +0100914static void s3cmci_dma_setup(struct s3cmci_host *host,
915 enum s3c2410_dmasrc source)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100916{
917 static enum s3c2410_dmasrc last_source = -1;
918 static int setup_ok;
919
920 if (last_source == source)
921 return;
922
923 last_source = source;
924
Ben Dooks8970ef42009-03-19 15:02:34 +0000925 s3c2410_dma_devconfig(host->dma, source,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100926 host->mem->start + host->sdidata);
927
928 if (!setup_ok) {
Ben Dooksfe9db6c2009-06-08 23:33:56 +0100929 s3c2410_dma_config(host->dma, 4);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100930 s3c2410_dma_set_buffdone_fn(host->dma,
931 s3cmci_dma_done_callback);
932 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
933 setup_ok = 1;
934 }
935}
936
937static void s3cmci_send_command(struct s3cmci_host *host,
938 struct mmc_command *cmd)
939{
940 u32 ccon, imsk;
941
942 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
943 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
944 S3C2410_SDIIMSK_RESPONSECRC;
945
946 enable_imask(host, imsk);
947
948 if (cmd->data)
949 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
950 else if (cmd->flags & MMC_RSP_PRESENT)
951 host->complete_what = COMPLETION_RSPFIN;
952 else
953 host->complete_what = COMPLETION_CMDSENT;
954
955 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
956
957 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
958 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
959
960 if (cmd->flags & MMC_RSP_PRESENT)
961 ccon |= S3C2410_SDICMDCON_WAITRSP;
962
963 if (cmd->flags & MMC_RSP_136)
964 ccon |= S3C2410_SDICMDCON_LONGRSP;
965
966 writel(ccon, host->base + S3C2410_SDICMDCON);
967}
968
969static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
970{
971 u32 dcon, imsk, stoptries = 3;
972
973 /* write DCON register */
974
975 if (!data) {
976 writel(0, host->base + S3C2410_SDIDCON);
977 return 0;
978 }
979
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100980 if ((data->blksz & 3) != 0) {
981 /* We cannot deal with unaligned blocks with more than
982 * one block being transfered. */
983
Christer Weinigel088a78a2008-10-15 00:17:17 +0100984 if (data->blocks > 1) {
985 pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100986 return -EINVAL;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100987 }
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100988 }
989
Thomas Kleffelbe518012008-06-30 22:40:24 +0100990 while (readl(host->base + S3C2410_SDIDSTA) &
991 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
992
993 dbg(host, dbg_err,
994 "mci_setup_data() transfer stillin progress.\n");
995
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100996 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100997 s3cmci_reset(host);
998
999 if ((stoptries--) == 0) {
1000 dbg_dumpregs(host, "DRF");
1001 return -EINVAL;
1002 }
1003 }
1004
1005 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
1006
Ben Dooks26f14942009-10-01 15:44:18 -07001007 if (s3cmci_host_usedma(host))
Thomas Kleffelbe518012008-06-30 22:40:24 +01001008 dcon |= S3C2410_SDIDCON_DMAEN;
1009
1010 if (host->bus_width == MMC_BUS_WIDTH_4)
1011 dcon |= S3C2410_SDIDCON_WIDEBUS;
1012
1013 if (!(data->flags & MMC_DATA_STREAM))
1014 dcon |= S3C2410_SDIDCON_BLOCKMODE;
1015
1016 if (data->flags & MMC_DATA_WRITE) {
1017 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
1018 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
1019 }
1020
1021 if (data->flags & MMC_DATA_READ) {
1022 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
1023 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
1024 }
1025
1026 if (host->is2440) {
1027 dcon |= S3C2440_SDIDCON_DS_WORD;
1028 dcon |= S3C2440_SDIDCON_DATSTART;
1029 }
1030
1031 writel(dcon, host->base + S3C2410_SDIDCON);
1032
1033 /* write BSIZE register */
1034
1035 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
1036
1037 /* add to IMASK register */
1038 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
1039 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
1040
1041 enable_imask(host, imsk);
1042
1043 /* write TIMER register */
1044
1045 if (host->is2440) {
1046 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
1047 } else {
1048 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
1049
1050 /* FIX: set slow clock to prevent timeouts on read */
1051 if (data->flags & MMC_DATA_READ)
1052 writel(0xFF, host->base + S3C2410_SDIPRE);
1053 }
1054
1055 return 0;
1056}
1057
1058#define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
1059
1060static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
1061{
1062 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
1063
1064 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1065
1066 host->pio_sgptr = 0;
Christer Weinigel088a78a2008-10-15 00:17:17 +01001067 host->pio_bytes = 0;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001068 host->pio_count = 0;
1069 host->pio_active = rw ? XFER_WRITE : XFER_READ;
1070
1071 if (rw) {
1072 do_pio_write(host);
1073 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
1074 } else {
1075 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
1076 | S3C2410_SDIIMSK_RXFIFOLAST);
1077 }
1078
1079 return 0;
1080}
1081
1082static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
1083{
1084 int dma_len, i;
Ben Dooks68c5ed52009-10-01 15:44:19 -07001085 int rw = data->flags & MMC_DATA_WRITE;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001086
1087 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1088
1089 s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
1090 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1091
1092 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
Ben Dooks68c5ed52009-10-01 15:44:19 -07001093 rw ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001094
1095 if (dma_len == 0)
1096 return -ENOMEM;
1097
1098 host->dma_complete = 0;
1099 host->dmatogo = dma_len;
1100
1101 for (i = 0; i < dma_len; i++) {
1102 int res;
1103
Ben Dooks68c5ed52009-10-01 15:44:19 -07001104 dbg(host, dbg_dma, "enqueue %i: %08x@%u\n", i,
1105 sg_dma_address(&data->sg[i]),
1106 sg_dma_len(&data->sg[i]));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001107
Ben Dooks68c5ed52009-10-01 15:44:19 -07001108 res = s3c2410_dma_enqueue(host->dma, host,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001109 sg_dma_address(&data->sg[i]),
1110 sg_dma_len(&data->sg[i]));
1111
1112 if (res) {
1113 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1114 return -EBUSY;
1115 }
1116 }
1117
1118 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
1119
1120 return 0;
1121}
1122
1123static void s3cmci_send_request(struct mmc_host *mmc)
1124{
1125 struct s3cmci_host *host = mmc_priv(mmc);
1126 struct mmc_request *mrq = host->mrq;
1127 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
1128
1129 host->ccnt++;
1130 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
1131
1132 /* Clear command, data and fifo status registers
1133 Fifo clear only necessary on 2440, but doesn't hurt on 2410
1134 */
1135 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1136 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1137 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1138
1139 if (cmd->data) {
1140 int res = s3cmci_setup_data(host, cmd->data);
1141
1142 host->dcnt++;
1143
1144 if (res) {
Ben Dooksff8c804f2008-06-30 22:40:37 +01001145 dbg(host, dbg_err, "setup data error %d\n", res);
1146 cmd->error = res;
1147 cmd->data->error = res;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001148
1149 mmc_request_done(mmc, mrq);
1150 return;
1151 }
1152
Ben Dooks26f14942009-10-01 15:44:18 -07001153 if (s3cmci_host_usedma(host))
Thomas Kleffelbe518012008-06-30 22:40:24 +01001154 res = s3cmci_prepare_dma(host, cmd->data);
1155 else
1156 res = s3cmci_prepare_pio(host, cmd->data);
1157
1158 if (res) {
Ben Dooksff8c804f2008-06-30 22:40:37 +01001159 dbg(host, dbg_err, "data prepare error %d\n", res);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001160 cmd->error = res;
1161 cmd->data->error = res;
1162
1163 mmc_request_done(mmc, mrq);
1164 return;
1165 }
1166 }
1167
1168 /* Send command */
1169 s3cmci_send_command(host, cmd);
1170
1171 /* Enable Interrupt */
Ben Dooksc2258892009-10-01 15:44:18 -07001172 s3cmci_enable_irq(host, true);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001173}
1174
Ben Dooks87dd9802008-08-12 09:24:50 +01001175static int s3cmci_card_present(struct mmc_host *mmc)
Ben Dooks50a84572008-06-30 22:40:36 +01001176{
Ben Dooks87dd9802008-08-12 09:24:50 +01001177 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dooks50a84572008-06-30 22:40:36 +01001178 struct s3c24xx_mci_pdata *pdata = host->pdata;
1179 int ret;
1180
1181 if (pdata->gpio_detect == 0)
1182 return -ENOSYS;
1183
Ben Dooks916a3072009-10-01 15:44:15 -07001184 ret = gpio_get_value(pdata->gpio_detect) ? 0 : 1;
Ben Dooks50a84572008-06-30 22:40:36 +01001185 return ret ^ pdata->detect_invert;
1186}
1187
Thomas Kleffelbe518012008-06-30 22:40:24 +01001188static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1189{
1190 struct s3cmci_host *host = mmc_priv(mmc);
1191
1192 host->status = "mmc request";
1193 host->cmd_is_stop = 0;
1194 host->mrq = mrq;
1195
Ben Dooks87dd9802008-08-12 09:24:50 +01001196 if (s3cmci_card_present(mmc) == 0) {
Ben Dooks50a84572008-06-30 22:40:36 +01001197 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1198 host->mrq->cmd->error = -ENOMEDIUM;
1199 mmc_request_done(mmc, mrq);
1200 } else
1201 s3cmci_send_request(mmc);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001202}
1203
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001204static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1205{
1206 u32 mci_psc;
1207
1208 /* Set clock */
1209 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1210 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1211
1212 if (host->real_rate <= ios->clock)
1213 break;
1214 }
1215
1216 if (mci_psc > 255)
1217 mci_psc = 255;
1218
1219 host->prescaler = mci_psc;
1220 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1221
1222 /* If requested clock is 0, real_rate will be 0, too */
1223 if (ios->clock == 0)
1224 host->real_rate = 0;
1225}
1226
Thomas Kleffelbe518012008-06-30 22:40:24 +01001227static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1228{
1229 struct s3cmci_host *host = mmc_priv(mmc);
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001230 u32 mci_con;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001231
1232 /* Set the power state */
1233
1234 mci_con = readl(host->base + S3C2410_SDICON);
1235
1236 switch (ios->power_mode) {
1237 case MMC_POWER_ON:
1238 case MMC_POWER_UP:
Ben Dooks916a3072009-10-01 15:44:15 -07001239 s3c2410_gpio_cfgpin(S3C2410_GPE(5), S3C2410_GPE5_SDCLK);
1240 s3c2410_gpio_cfgpin(S3C2410_GPE(6), S3C2410_GPE6_SDCMD);
1241 s3c2410_gpio_cfgpin(S3C2410_GPE(7), S3C2410_GPE7_SDDAT0);
1242 s3c2410_gpio_cfgpin(S3C2410_GPE(8), S3C2410_GPE8_SDDAT1);
1243 s3c2410_gpio_cfgpin(S3C2410_GPE(9), S3C2410_GPE9_SDDAT2);
1244 s3c2410_gpio_cfgpin(S3C2410_GPE(10), S3C2410_GPE10_SDDAT3);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001245
Ben Dooksedb5a982008-06-30 22:40:29 +01001246 if (host->pdata->set_power)
1247 host->pdata->set_power(ios->power_mode, ios->vdd);
1248
Thomas Kleffelbe518012008-06-30 22:40:24 +01001249 if (!host->is2440)
1250 mci_con |= S3C2410_SDICON_FIFORESET;
1251
1252 break;
1253
1254 case MMC_POWER_OFF:
1255 default:
Ben Dooks916a3072009-10-01 15:44:15 -07001256 gpio_direction_output(S3C2410_GPE(5), 0);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001257
1258 if (host->is2440)
1259 mci_con |= S3C2440_SDICON_SDRESET;
1260
Ben Dooksedb5a982008-06-30 22:40:29 +01001261 if (host->pdata->set_power)
1262 host->pdata->set_power(ios->power_mode, ios->vdd);
1263
Thomas Kleffelbe518012008-06-30 22:40:24 +01001264 break;
1265 }
1266
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001267 s3cmci_set_clk(host, ios);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001268
1269 /* Set CLOCK_ENABLE */
1270 if (ios->clock)
1271 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1272 else
1273 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1274
1275 writel(mci_con, host->base + S3C2410_SDICON);
1276
1277 if ((ios->power_mode == MMC_POWER_ON) ||
1278 (ios->power_mode == MMC_POWER_UP)) {
1279 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1280 host->real_rate/1000, ios->clock/1000);
1281 } else {
1282 dbg(host, dbg_conf, "powered down.\n");
1283 }
1284
1285 host->bus_width = ios->bus_width;
1286}
1287
1288static void s3cmci_reset(struct s3cmci_host *host)
1289{
1290 u32 con = readl(host->base + S3C2410_SDICON);
1291
1292 con |= S3C2440_SDICON_SDRESET;
1293 writel(con, host->base + S3C2410_SDICON);
1294}
1295
Ben Dooksedb5a982008-06-30 22:40:29 +01001296static int s3cmci_get_ro(struct mmc_host *mmc)
1297{
1298 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dookscf0984c2008-06-30 22:40:30 +01001299 struct s3c24xx_mci_pdata *pdata = host->pdata;
1300 int ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001301
Ben Dookscf0984c2008-06-30 22:40:30 +01001302 if (pdata->gpio_wprotect == 0)
Ben Dooksedb5a982008-06-30 22:40:29 +01001303 return 0;
1304
Ben Dookscf0984c2008-06-30 22:40:30 +01001305 ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
1306
1307 if (pdata->wprotect_invert)
1308 ret = !ret;
1309
1310 return ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001311}
1312
Ben Dooksc2258892009-10-01 15:44:18 -07001313static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1314{
1315 struct s3cmci_host *host = mmc_priv(mmc);
1316 unsigned long flags;
1317 u32 con;
1318
1319 local_irq_save(flags);
1320
1321 con = readl(host->base + S3C2410_SDICON);
1322 host->sdio_irqen = enable;
1323
1324 if (enable == host->sdio_irqen)
1325 goto same_state;
1326
1327 if (enable) {
1328 con |= S3C2410_SDICON_SDIOIRQ;
1329 enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1330
1331 if (!host->irq_state && !host->irq_disabled) {
1332 host->irq_state = true;
1333 enable_irq(host->irq);
1334 }
1335 } else {
1336 disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1337 con &= ~S3C2410_SDICON_SDIOIRQ;
1338
1339 if (!host->irq_enabled && host->irq_state) {
1340 disable_irq_nosync(host->irq);
1341 host->irq_state = false;
1342 }
1343 }
1344
1345 writel(con, host->base + S3C2410_SDICON);
1346
1347 same_state:
1348 local_irq_restore(flags);
1349
1350 s3cmci_check_sdio_irq(host);
1351}
1352
Thomas Kleffelbe518012008-06-30 22:40:24 +01001353static struct mmc_host_ops s3cmci_ops = {
1354 .request = s3cmci_request,
1355 .set_ios = s3cmci_set_ios,
Ben Dooksedb5a982008-06-30 22:40:29 +01001356 .get_ro = s3cmci_get_ro,
Ben Dooks87dd9802008-08-12 09:24:50 +01001357 .get_cd = s3cmci_card_present,
Ben Dooksc2258892009-10-01 15:44:18 -07001358 .enable_sdio_irq = s3cmci_enable_sdio_irq,
Ben Dooksedb5a982008-06-30 22:40:29 +01001359};
1360
1361static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1362 /* This is currently here to avoid a number of if (host->pdata)
1363 * checks. Any zero fields to ensure reaonable defaults are picked. */
Thomas Kleffelbe518012008-06-30 22:40:24 +01001364};
1365
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001366#ifdef CONFIG_CPU_FREQ
1367
1368static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1369 unsigned long val, void *data)
1370{
1371 struct s3cmci_host *host;
1372 struct mmc_host *mmc;
1373 unsigned long newclk;
1374 unsigned long flags;
1375
1376 host = container_of(nb, struct s3cmci_host, freq_transition);
1377 newclk = clk_get_rate(host->clk);
1378 mmc = host->mmc;
1379
1380 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1381 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1382 spin_lock_irqsave(&mmc->lock, flags);
1383
1384 host->clk_rate = newclk;
1385
1386 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1387 mmc->ios.clock != 0)
1388 s3cmci_set_clk(host, &mmc->ios);
1389
1390 spin_unlock_irqrestore(&mmc->lock, flags);
1391 }
1392
1393 return 0;
1394}
1395
1396static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1397{
1398 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1399
1400 return cpufreq_register_notifier(&host->freq_transition,
1401 CPUFREQ_TRANSITION_NOTIFIER);
1402}
1403
1404static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1405{
1406 cpufreq_unregister_notifier(&host->freq_transition,
1407 CPUFREQ_TRANSITION_NOTIFIER);
1408}
1409
1410#else
1411static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1412{
1413 return 0;
1414}
1415
1416static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1417{
1418}
1419#endif
1420
Ben Dooks916a3072009-10-01 15:44:15 -07001421
Ben Dooks9bdd2032009-10-01 15:44:17 -07001422#ifdef CONFIG_DEBUG_FS
1423
1424static int s3cmci_state_show(struct seq_file *seq, void *v)
1425{
1426 struct s3cmci_host *host = seq->private;
1427
1428 seq_printf(seq, "Register base = 0x%08x\n", (u32)host->base);
1429 seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
1430 seq_printf(seq, "Prescale = %d\n", host->prescaler);
1431 seq_printf(seq, "is2440 = %d\n", host->is2440);
1432 seq_printf(seq, "IRQ = %d\n", host->irq);
Ben Dooksc2258892009-10-01 15:44:18 -07001433 seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
1434 seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
1435 seq_printf(seq, "IRQ state = %d\n", host->irq_state);
Ben Dooks9bdd2032009-10-01 15:44:17 -07001436 seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
Ben Dooks26f14942009-10-01 15:44:18 -07001437 seq_printf(seq, "Do DMA = %d\n", s3cmci_host_usedma(host));
Ben Dooks9bdd2032009-10-01 15:44:17 -07001438 seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
1439 seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
1440
1441 return 0;
1442}
1443
1444static int s3cmci_state_open(struct inode *inode, struct file *file)
1445{
1446 return single_open(file, s3cmci_state_show, inode->i_private);
1447}
1448
1449static const struct file_operations s3cmci_fops_state = {
1450 .owner = THIS_MODULE,
1451 .open = s3cmci_state_open,
1452 .read = seq_read,
1453 .llseek = seq_lseek,
1454 .release = single_release,
1455};
1456
1457#define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
1458
1459struct s3cmci_reg {
1460 unsigned short addr;
1461 unsigned char *name;
1462} debug_regs[] = {
1463 DBG_REG(CON),
1464 DBG_REG(PRE),
1465 DBG_REG(CMDARG),
1466 DBG_REG(CMDCON),
1467 DBG_REG(CMDSTAT),
1468 DBG_REG(RSP0),
1469 DBG_REG(RSP1),
1470 DBG_REG(RSP2),
1471 DBG_REG(RSP3),
1472 DBG_REG(TIMER),
1473 DBG_REG(BSIZE),
1474 DBG_REG(DCON),
1475 DBG_REG(DCNT),
1476 DBG_REG(DSTA),
1477 DBG_REG(FSTA),
1478 {}
1479};
1480
1481static int s3cmci_regs_show(struct seq_file *seq, void *v)
1482{
1483 struct s3cmci_host *host = seq->private;
1484 struct s3cmci_reg *rptr = debug_regs;
1485
1486 for (; rptr->name; rptr++)
1487 seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
1488 readl(host->base + rptr->addr));
1489
1490 seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
1491
1492 return 0;
1493}
1494
1495static int s3cmci_regs_open(struct inode *inode, struct file *file)
1496{
1497 return single_open(file, s3cmci_regs_show, inode->i_private);
1498}
1499
1500static const struct file_operations s3cmci_fops_regs = {
1501 .owner = THIS_MODULE,
1502 .open = s3cmci_regs_open,
1503 .read = seq_read,
1504 .llseek = seq_lseek,
1505 .release = single_release,
1506};
1507
1508static void s3cmci_debugfs_attach(struct s3cmci_host *host)
1509{
1510 struct device *dev = &host->pdev->dev;
1511
1512 host->debug_root = debugfs_create_dir(dev_name(dev), NULL);
1513 if (IS_ERR(host->debug_root)) {
1514 dev_err(dev, "failed to create debugfs root\n");
1515 return;
1516 }
1517
1518 host->debug_state = debugfs_create_file("state", 0444,
1519 host->debug_root, host,
1520 &s3cmci_fops_state);
1521
1522 if (IS_ERR(host->debug_state))
1523 dev_err(dev, "failed to create debug state file\n");
1524
1525 host->debug_regs = debugfs_create_file("regs", 0444,
1526 host->debug_root, host,
1527 &s3cmci_fops_regs);
1528
1529 if (IS_ERR(host->debug_regs))
1530 dev_err(dev, "failed to create debug regs file\n");
1531}
1532
1533static void s3cmci_debugfs_remove(struct s3cmci_host *host)
1534{
1535 debugfs_remove(host->debug_regs);
1536 debugfs_remove(host->debug_state);
1537 debugfs_remove(host->debug_root);
1538}
1539
1540#else
1541static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
1542static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
1543
1544#endif /* CONFIG_DEBUG_FS */
1545
Ben Dooks44d0e192009-10-01 15:44:14 -07001546static int __devinit s3cmci_probe(struct platform_device *pdev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001547{
1548 struct s3cmci_host *host;
1549 struct mmc_host *mmc;
1550 int ret;
Ben Dooks44d0e192009-10-01 15:44:14 -07001551 int is2440;
Ben Dooks916a3072009-10-01 15:44:15 -07001552 int i;
Ben Dooks44d0e192009-10-01 15:44:14 -07001553
1554 is2440 = platform_get_device_id(pdev)->driver_data;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001555
1556 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1557 if (!mmc) {
1558 ret = -ENOMEM;
1559 goto probe_out;
1560 }
1561
Ben Dooks916a3072009-10-01 15:44:15 -07001562 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {
1563 ret = gpio_request(i, dev_name(&pdev->dev));
1564 if (ret) {
1565 dev_err(&pdev->dev, "failed to get gpio %d\n", i);
1566
1567 for (i--; i >= S3C2410_GPE(5); i--)
1568 gpio_free(i);
1569
1570 goto probe_free_host;
1571 }
1572 }
1573
Thomas Kleffelbe518012008-06-30 22:40:24 +01001574 host = mmc_priv(mmc);
1575 host->mmc = mmc;
1576 host->pdev = pdev;
1577 host->is2440 = is2440;
1578
Ben Dooksedb5a982008-06-30 22:40:29 +01001579 host->pdata = pdev->dev.platform_data;
1580 if (!host->pdata) {
1581 pdev->dev.platform_data = &s3cmci_def_pdata;
1582 host->pdata = &s3cmci_def_pdata;
1583 }
1584
Thomas Kleffelbe518012008-06-30 22:40:24 +01001585 spin_lock_init(&host->complete_lock);
1586 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1587
1588 if (is2440) {
1589 host->sdiimsk = S3C2440_SDIIMSK;
1590 host->sdidata = S3C2440_SDIDATA;
1591 host->clk_div = 1;
1592 } else {
1593 host->sdiimsk = S3C2410_SDIIMSK;
1594 host->sdidata = S3C2410_SDIDATA;
1595 host->clk_div = 2;
1596 }
1597
Thomas Kleffelbe518012008-06-30 22:40:24 +01001598 host->complete_what = COMPLETION_NONE;
1599 host->pio_active = XFER_NONE;
1600
Ben Dooks26f14942009-10-01 15:44:18 -07001601#ifdef CONFIG_MMC_S3C_PIODMA
1602 host->dodma = host->pdata->dma;
1603#endif
1604
Thomas Kleffelbe518012008-06-30 22:40:24 +01001605 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1606 if (!host->mem) {
1607 dev_err(&pdev->dev,
1608 "failed to get io memory region resouce.\n");
1609
1610 ret = -ENOENT;
Ben Dooks916a3072009-10-01 15:44:15 -07001611 goto probe_free_gpio;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001612 }
1613
1614 host->mem = request_mem_region(host->mem->start,
Ben Dooksda52a7c2009-10-01 15:44:13 -07001615 resource_size(host->mem), pdev->name);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001616
1617 if (!host->mem) {
1618 dev_err(&pdev->dev, "failed to request io memory region.\n");
1619 ret = -ENOENT;
Ben Dooks916a3072009-10-01 15:44:15 -07001620 goto probe_free_gpio;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001621 }
1622
Ben Dooksda52a7c2009-10-01 15:44:13 -07001623 host->base = ioremap(host->mem->start, resource_size(host->mem));
Ben Dooks5d304402008-08-08 10:55:41 +01001624 if (!host->base) {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001625 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1626 ret = -EINVAL;
1627 goto probe_free_mem_region;
1628 }
1629
1630 host->irq = platform_get_irq(pdev, 0);
1631 if (host->irq == 0) {
1632 dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
1633 ret = -EINVAL;
1634 goto probe_iounmap;
1635 }
1636
1637 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1638 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1639 ret = -ENOENT;
1640 goto probe_iounmap;
1641 }
1642
1643 /* We get spurious interrupts even when we have set the IMSK
1644 * register to ignore everything, so use disable_irq() to make
1645 * ensure we don't lock the system with un-serviceable requests. */
1646
1647 disable_irq(host->irq);
Ben Dooksc2258892009-10-01 15:44:18 -07001648 host->irq_state = false;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001649
Ben Dooks916a3072009-10-01 15:44:15 -07001650 if (host->pdata->gpio_detect) {
1651 ret = gpio_request(host->pdata->gpio_detect, "s3cmci detect");
1652 if (ret) {
1653 dev_err(&pdev->dev, "failed to get detect gpio\n");
1654 goto probe_free_irq;
1655 }
1656 }
1657
Ben Dooks55d70f52008-06-30 22:40:32 +01001658 host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001659
Ben Dooks55d70f52008-06-30 22:40:32 +01001660 if (host->irq_cd >= 0) {
1661 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1662 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1663 DRIVER_NAME, host)) {
1664 dev_err(&pdev->dev, "can't get card detect irq.\n");
1665 ret = -ENOENT;
Ben Dooks916a3072009-10-01 15:44:15 -07001666 goto probe_free_gpio_cd;
Ben Dooks55d70f52008-06-30 22:40:32 +01001667 }
1668 } else {
1669 dev_warn(&pdev->dev, "host detect has no irq available\n");
Ben Dooks916a3072009-10-01 15:44:15 -07001670 gpio_direction_input(host->pdata->gpio_detect);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001671 }
1672
Ben Dooks916a3072009-10-01 15:44:15 -07001673 if (host->pdata->gpio_wprotect) {
1674 ret = gpio_request(host->pdata->gpio_wprotect, "s3cmci wp");
1675 if (ret) {
1676 dev_err(&pdev->dev, "failed to get writeprotect\n");
1677 goto probe_free_irq_cd;
1678 }
1679
1680 gpio_direction_input(host->pdata->gpio_wprotect);
1681 }
Ben Dooksedb5a982008-06-30 22:40:29 +01001682
Ben Dooks68c5ed52009-10-01 15:44:19 -07001683 /* depending on the dma state, get a dma channel to use. */
1684
1685 if (s3cmci_host_usedma(host)) {
1686 host->dma = s3c2410_dma_request(DMACH_SDI, &s3cmci_dma_client,
1687 host);
1688 if (host->dma < 0) {
1689 dev_err(&pdev->dev, "cannot get DMA channel.\n");
1690 if (!s3cmci_host_canpio()) {
1691 ret = -EBUSY;
1692 goto probe_free_gpio_wp;
1693 } else {
1694 dev_warn(&pdev->dev, "falling back to PIO.\n");
1695 host->dodma = 0;
1696 }
1697 }
Thomas Kleffelbe518012008-06-30 22:40:24 +01001698 }
1699
1700 host->clk = clk_get(&pdev->dev, "sdi");
1701 if (IS_ERR(host->clk)) {
1702 dev_err(&pdev->dev, "failed to find clock source.\n");
1703 ret = PTR_ERR(host->clk);
1704 host->clk = NULL;
Ben Dooks68c5ed52009-10-01 15:44:19 -07001705 goto probe_free_dma;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001706 }
1707
1708 ret = clk_enable(host->clk);
1709 if (ret) {
1710 dev_err(&pdev->dev, "failed to enable clock source.\n");
1711 goto clk_free;
1712 }
1713
1714 host->clk_rate = clk_get_rate(host->clk);
1715
1716 mmc->ops = &s3cmci_ops;
Ben Dooksedb5a982008-06-30 22:40:29 +01001717 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Ben Dooksc2258892009-10-01 15:44:18 -07001718 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001719 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1720 mmc->f_max = host->clk_rate / host->clk_div;
1721
Ben Dooksedb5a982008-06-30 22:40:29 +01001722 if (host->pdata->ocr_avail)
1723 mmc->ocr_avail = host->pdata->ocr_avail;
1724
Thomas Kleffelbe518012008-06-30 22:40:24 +01001725 mmc->max_blk_count = 4095;
1726 mmc->max_blk_size = 4095;
1727 mmc->max_req_size = 4095 * 512;
1728 mmc->max_seg_size = mmc->max_req_size;
1729
1730 mmc->max_phys_segs = 128;
1731 mmc->max_hw_segs = 128;
1732
1733 dbg(host, dbg_debug,
1734 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1735 (host->is2440?"2440":""),
1736 host->base, host->irq, host->irq_cd, host->dma);
1737
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001738 ret = s3cmci_cpufreq_register(host);
1739 if (ret) {
1740 dev_err(&pdev->dev, "failed to register cpufreq\n");
1741 goto free_dmabuf;
1742 }
1743
Thomas Kleffelbe518012008-06-30 22:40:24 +01001744 ret = mmc_add_host(mmc);
1745 if (ret) {
1746 dev_err(&pdev->dev, "failed to add mmc host.\n");
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001747 goto free_cpufreq;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001748 }
1749
Ben Dooks9bdd2032009-10-01 15:44:17 -07001750 s3cmci_debugfs_attach(host);
1751
Thomas Kleffelbe518012008-06-30 22:40:24 +01001752 platform_set_drvdata(pdev, mmc);
Ben Dooks26f14942009-10-01 15:44:18 -07001753 dev_info(&pdev->dev, "%s - using %s\n", mmc_hostname(mmc),
1754 s3cmci_host_usedma(host) ? "dma" : "pio");
Thomas Kleffelbe518012008-06-30 22:40:24 +01001755
1756 return 0;
1757
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001758 free_cpufreq:
1759 s3cmci_cpufreq_deregister(host);
1760
Thomas Kleffelbe518012008-06-30 22:40:24 +01001761 free_dmabuf:
1762 clk_disable(host->clk);
1763
1764 clk_free:
1765 clk_put(host->clk);
1766
Ben Dooks68c5ed52009-10-01 15:44:19 -07001767 probe_free_dma:
1768 if (s3cmci_host_usedma(host))
1769 s3c2410_dma_free(host->dma, &s3cmci_dma_client);
1770
Ben Dooks916a3072009-10-01 15:44:15 -07001771 probe_free_gpio_wp:
1772 if (host->pdata->gpio_wprotect)
1773 gpio_free(host->pdata->gpio_wprotect);
1774
1775 probe_free_gpio_cd:
1776 if (host->pdata->gpio_detect)
1777 gpio_free(host->pdata->gpio_detect);
1778
Thomas Kleffelbe518012008-06-30 22:40:24 +01001779 probe_free_irq_cd:
Ben Dooks55d70f52008-06-30 22:40:32 +01001780 if (host->irq_cd >= 0)
1781 free_irq(host->irq_cd, host);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001782
1783 probe_free_irq:
1784 free_irq(host->irq, host);
1785
1786 probe_iounmap:
1787 iounmap(host->base);
1788
1789 probe_free_mem_region:
Ben Dooksda52a7c2009-10-01 15:44:13 -07001790 release_mem_region(host->mem->start, resource_size(host->mem));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001791
Ben Dooks916a3072009-10-01 15:44:15 -07001792 probe_free_gpio:
1793 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1794 gpio_free(i);
1795
Thomas Kleffelbe518012008-06-30 22:40:24 +01001796 probe_free_host:
1797 mmc_free_host(mmc);
Ben Dooks916a3072009-10-01 15:44:15 -07001798
Thomas Kleffelbe518012008-06-30 22:40:24 +01001799 probe_out:
1800 return ret;
1801}
1802
Ben Dooks907b2cd2008-07-17 15:32:54 +01001803static void s3cmci_shutdown(struct platform_device *pdev)
1804{
1805 struct mmc_host *mmc = platform_get_drvdata(pdev);
1806 struct s3cmci_host *host = mmc_priv(mmc);
1807
1808 if (host->irq_cd >= 0)
1809 free_irq(host->irq_cd, host);
1810
Ben Dooks9bdd2032009-10-01 15:44:17 -07001811 s3cmci_debugfs_remove(host);
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001812 s3cmci_cpufreq_deregister(host);
Ben Dooks907b2cd2008-07-17 15:32:54 +01001813 mmc_remove_host(mmc);
1814 clk_disable(host->clk);
1815}
1816
Thomas Kleffelbe518012008-06-30 22:40:24 +01001817static int __devexit s3cmci_remove(struct platform_device *pdev)
1818{
1819 struct mmc_host *mmc = platform_get_drvdata(pdev);
1820 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dooks916a3072009-10-01 15:44:15 -07001821 struct s3c24xx_mci_pdata *pd = host->pdata;
1822 int i;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001823
Ben Dooks907b2cd2008-07-17 15:32:54 +01001824 s3cmci_shutdown(pdev);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001825
Thomas Kleffelbe518012008-06-30 22:40:24 +01001826 clk_put(host->clk);
1827
1828 tasklet_disable(&host->pio_tasklet);
Ben Dooks68c5ed52009-10-01 15:44:19 -07001829
1830 if (s3cmci_host_usedma(host))
1831 s3c2410_dma_free(host->dma, &s3cmci_dma_client);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001832
Thomas Kleffelbe518012008-06-30 22:40:24 +01001833 free_irq(host->irq, host);
1834
Ben Dooks916a3072009-10-01 15:44:15 -07001835 if (pd->gpio_wprotect)
1836 gpio_free(pd->gpio_wprotect);
1837
1838 if (pd->gpio_detect)
1839 gpio_free(pd->gpio_detect);
1840
1841 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1842 gpio_free(i);
1843
1844
Thomas Kleffelbe518012008-06-30 22:40:24 +01001845 iounmap(host->base);
Ben Dooksda52a7c2009-10-01 15:44:13 -07001846 release_mem_region(host->mem->start, resource_size(host->mem));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001847
1848 mmc_free_host(mmc);
1849 return 0;
1850}
1851
Ben Dooks44d0e192009-10-01 15:44:14 -07001852static struct platform_device_id s3cmci_driver_ids[] = {
1853 {
1854 .name = "s3c2410-sdi",
1855 .driver_data = 0,
1856 }, {
1857 .name = "s3c2412-sdi",
1858 .driver_data = 1,
1859 }, {
1860 .name = "s3c2440-sdi",
1861 .driver_data = 1,
1862 },
1863 { }
1864};
Thomas Kleffelbe518012008-06-30 22:40:24 +01001865
Ben Dooks44d0e192009-10-01 15:44:14 -07001866MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001867
Thomas Kleffelbe518012008-06-30 22:40:24 +01001868
1869#ifdef CONFIG_PM
1870
Ben Dooks50d7fa92009-10-01 15:44:15 -07001871static int s3cmci_suspend(struct device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001872{
Ben Dooks50d7fa92009-10-01 15:44:15 -07001873 struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
1874 struct pm_message event = { PM_EVENT_SUSPEND };
Thomas Kleffelbe518012008-06-30 22:40:24 +01001875
Ben Dooks50d7fa92009-10-01 15:44:15 -07001876 return mmc_suspend_host(mmc, event);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001877}
1878
Ben Dooks50d7fa92009-10-01 15:44:15 -07001879static int s3cmci_resume(struct device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001880{
Ben Dooks50d7fa92009-10-01 15:44:15 -07001881 struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001882
1883 return mmc_resume_host(mmc);
1884}
1885
Ben Dooks50d7fa92009-10-01 15:44:15 -07001886static struct dev_pm_ops s3cmci_pm = {
1887 .suspend = s3cmci_suspend,
1888 .resume = s3cmci_resume,
1889};
1890
1891#define s3cmci_pm_ops &s3cmci_pm
Thomas Kleffelbe518012008-06-30 22:40:24 +01001892#else /* CONFIG_PM */
Ben Dooks50d7fa92009-10-01 15:44:15 -07001893#define s3cmci_pm_ops NULL
Thomas Kleffelbe518012008-06-30 22:40:24 +01001894#endif /* CONFIG_PM */
1895
1896
Ben Dooks44d0e192009-10-01 15:44:14 -07001897static struct platform_driver s3cmci_driver = {
Ben Dooks50d7fa92009-10-01 15:44:15 -07001898 .driver = {
1899 .name = "s3c-sdi",
1900 .owner = THIS_MODULE,
1901 .pm = s3cmci_pm_ops,
1902 },
Ben Dooks44d0e192009-10-01 15:44:14 -07001903 .id_table = s3cmci_driver_ids,
1904 .probe = s3cmci_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001905 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001906 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001907};
1908
Thomas Kleffelbe518012008-06-30 22:40:24 +01001909static int __init s3cmci_init(void)
1910{
Ben Dooks44d0e192009-10-01 15:44:14 -07001911 return platform_driver_register(&s3cmci_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001912}
1913
1914static void __exit s3cmci_exit(void)
1915{
Ben Dooks44d0e192009-10-01 15:44:14 -07001916 platform_driver_unregister(&s3cmci_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001917}
1918
1919module_init(s3cmci_init);
1920module_exit(s3cmci_exit);
1921
1922MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1923MODULE_LICENSE("GPL v2");
ben@fluff.org.uk08c55e22008-10-15 00:17:19 +01001924MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");