blob: 7660ac4572e505a83ab5c4da6a748bac03c46cff [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
167static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
168{
169 u32 newmask;
170
171 newmask = readl(host->base + host->sdiimsk);
172 newmask |= imask;
173
174 writel(newmask, host->base + host->sdiimsk);
175
176 return newmask;
177}
178
179static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
180{
181 u32 newmask;
182
183 newmask = readl(host->base + host->sdiimsk);
184 newmask &= ~imask;
185
186 writel(newmask, host->base + host->sdiimsk);
187
188 return newmask;
189}
190
191static inline void clear_imask(struct s3cmci_host *host)
192{
Ben Dooksc2258892009-10-01 15:44:18 -0700193 u32 mask = readl(host->base + host->sdiimsk);
194
195 /* preserve the SDIO IRQ mask state */
196 mask &= S3C2410_SDIIMSK_SDIOIRQ;
197 writel(mask, host->base + host->sdiimsk);
198}
199
200/**
201 * s3cmci_check_sdio_irq - test whether the SDIO IRQ is being signalled
202 * @host: The host to check.
203 *
204 * Test to see if the SDIO interrupt is being signalled in case the
205 * controller has failed to re-detect a card interrupt. Read GPE8 and
206 * see if it is low and if so, signal a SDIO interrupt.
207 *
208 * This is currently called if a request is finished (we assume that the
209 * bus is now idle) and when the SDIO IRQ is enabled in case the IRQ is
210 * already being indicated.
211*/
212static void s3cmci_check_sdio_irq(struct s3cmci_host *host)
213{
214 if (host->sdio_irqen) {
215 if (gpio_get_value(S3C2410_GPE(8)) == 0) {
216 printk(KERN_DEBUG "%s: signalling irq\n", __func__);
217 mmc_signal_sdio_irq(host->mmc);
218 }
219 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100220}
221
222static inline int get_data_buffer(struct s3cmci_host *host,
Christer Weinigel088a78a2008-10-15 00:17:17 +0100223 u32 *bytes, u32 **pointer)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100224{
225 struct scatterlist *sg;
226
227 if (host->pio_active == XFER_NONE)
228 return -EINVAL;
229
230 if ((!host->mrq) || (!host->mrq->data))
231 return -EINVAL;
232
233 if (host->pio_sgptr >= host->mrq->data->sg_len) {
234 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
235 host->pio_sgptr, host->mrq->data->sg_len);
236 return -EBUSY;
237 }
238 sg = &host->mrq->data->sg[host->pio_sgptr];
239
Christer Weinigel088a78a2008-10-15 00:17:17 +0100240 *bytes = sg->length;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100241 *pointer = sg_virt(sg);
242
243 host->pio_sgptr++;
244
245 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
246 host->pio_sgptr, host->mrq->data->sg_len);
247
248 return 0;
249}
250
251static inline u32 fifo_count(struct s3cmci_host *host)
252{
253 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
254
255 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100256 return fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100257}
258
259static inline u32 fifo_free(struct s3cmci_host *host)
260{
261 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
262
263 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100264 return 63 - fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100265}
266
Ben Dooksc2258892009-10-01 15:44:18 -0700267/**
268 * s3cmci_enable_irq - enable IRQ, after having disabled it.
269 * @host: The device state.
270 * @more: True if more IRQs are expected from transfer.
271 *
272 * Enable the main IRQ if needed after it has been disabled.
273 *
274 * The IRQ can be one of the following states:
275 * - disabled during IDLE
276 * - disabled whilst processing data
277 * - enabled during transfer
278 * - enabled whilst awaiting SDIO interrupt detection
279 */
280static void s3cmci_enable_irq(struct s3cmci_host *host, bool more)
281{
282 unsigned long flags;
283 bool enable = false;
284
285 local_irq_save(flags);
286
287 host->irq_enabled = more;
288 host->irq_disabled = false;
289
290 enable = more | host->sdio_irqen;
291
292 if (host->irq_state != enable) {
293 host->irq_state = enable;
294
295 if (enable)
296 enable_irq(host->irq);
297 else
298 disable_irq(host->irq);
299 }
300
301 local_irq_restore(flags);
302}
303
304/**
305 *
306 */
307static void s3cmci_disable_irq(struct s3cmci_host *host, bool transfer)
308{
309 unsigned long flags;
310
311 local_irq_save(flags);
312
313 //printk(KERN_DEBUG "%s: transfer %d\n", __func__, transfer);
314
315 host->irq_disabled = transfer;
316
317 if (transfer && host->irq_state) {
318 host->irq_state = false;
319 disable_irq(host->irq);
320 }
321
322 local_irq_restore(flags);
323}
324
Thomas Kleffelbe518012008-06-30 22:40:24 +0100325static void do_pio_read(struct s3cmci_host *host)
326{
327 int res;
328 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100329 u32 *ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100330 u32 fifo_words;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100331 void __iomem *from_ptr;
332
333 /* write real prescaler to host, it might be set slow to fix */
334 writel(host->prescaler, host->base + S3C2410_SDIPRE);
335
336 from_ptr = host->base + host->sdidata;
337
338 while ((fifo = fifo_count(host))) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100339 if (!host->pio_bytes) {
340 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100341 &host->pio_ptr);
342 if (res) {
343 host->pio_active = XFER_NONE;
344 host->complete_what = COMPLETION_FINALIZE;
345
346 dbg(host, dbg_pio, "pio_read(): "
347 "complete (no more data).\n");
348 return;
349 }
350
351 dbg(host, dbg_pio,
352 "pio_read(): new target: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100353 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100354 }
355
356 dbg(host, dbg_pio,
357 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100358 fifo, host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100359 readl(host->base + S3C2410_SDIDCNT));
360
Christer Weinigel088a78a2008-10-15 00:17:17 +0100361 /* If we have reached the end of the block, we can
362 * read a word and get 1 to 3 bytes. If we in the
363 * middle of the block, we have to read full words,
364 * otherwise we will write garbage, so round down to
365 * an even multiple of 4. */
366 if (fifo >= host->pio_bytes)
367 fifo = host->pio_bytes;
368 else
369 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100370
Christer Weinigel088a78a2008-10-15 00:17:17 +0100371 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100372 host->pio_count += fifo;
373
Christer Weinigel088a78a2008-10-15 00:17:17 +0100374 fifo_words = fifo >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100375 ptr = host->pio_ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100376 while (fifo_words--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100377 *ptr++ = readl(from_ptr);
378 host->pio_ptr = ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100379
380 if (fifo & 3) {
381 u32 n = fifo & 3;
382 u32 data = readl(from_ptr);
383 u8 *p = (u8 *)host->pio_ptr;
384
385 while (n--) {
386 *p++ = data;
387 data >>= 8;
388 }
389 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100390 }
391
Christer Weinigel088a78a2008-10-15 00:17:17 +0100392 if (!host->pio_bytes) {
393 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100394 if (res) {
395 dbg(host, dbg_pio,
396 "pio_read(): complete (no more buffers).\n");
397 host->pio_active = XFER_NONE;
398 host->complete_what = COMPLETION_FINALIZE;
399
400 return;
401 }
402 }
403
404 enable_imask(host,
405 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
406}
407
408static void do_pio_write(struct s3cmci_host *host)
409{
410 void __iomem *to_ptr;
411 int res;
412 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100413 u32 *ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100414
415 to_ptr = host->base + host->sdidata;
416
Yauhen Kharuzhy99424482009-02-11 13:25:52 -0800417 while ((fifo = fifo_free(host)) > 3) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100418 if (!host->pio_bytes) {
419 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100420 &host->pio_ptr);
421 if (res) {
422 dbg(host, dbg_pio,
423 "pio_write(): complete (no more data).\n");
424 host->pio_active = XFER_NONE;
425
426 return;
427 }
428
429 dbg(host, dbg_pio,
430 "pio_write(): new source: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100431 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100432
433 }
434
Christer Weinigel088a78a2008-10-15 00:17:17 +0100435 /* If we have reached the end of the block, we have to
436 * write exactly the remaining number of bytes. If we
437 * in the middle of the block, we have to write full
438 * words, so round down to an even multiple of 4. */
439 if (fifo >= host->pio_bytes)
440 fifo = host->pio_bytes;
441 else
442 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100443
Christer Weinigel088a78a2008-10-15 00:17:17 +0100444 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100445 host->pio_count += fifo;
446
Christer Weinigel088a78a2008-10-15 00:17:17 +0100447 fifo = (fifo + 3) >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100448 ptr = host->pio_ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100449 while (fifo--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100450 writel(*ptr++, to_ptr);
451 host->pio_ptr = ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100452 }
453
454 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
455}
456
457static void pio_tasklet(unsigned long data)
458{
459 struct s3cmci_host *host = (struct s3cmci_host *) data;
460
Ben Dooksc2258892009-10-01 15:44:18 -0700461 s3cmci_disable_irq(host, true);
Roman Moracikd643b5f2008-06-30 22:40:28 +0100462
Thomas Kleffelbe518012008-06-30 22:40:24 +0100463 if (host->pio_active == XFER_WRITE)
464 do_pio_write(host);
465
466 if (host->pio_active == XFER_READ)
467 do_pio_read(host);
468
469 if (host->complete_what == COMPLETION_FINALIZE) {
470 clear_imask(host);
471 if (host->pio_active != XFER_NONE) {
472 dbg(host, dbg_err, "unfinished %s "
Christer Weinigel088a78a2008-10-15 00:17:17 +0100473 "- pio_count:[%u] pio_bytes:[%u]\n",
Thomas Kleffelbe518012008-06-30 22:40:24 +0100474 (host->pio_active == XFER_READ) ? "read" : "write",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100475 host->pio_count, host->pio_bytes);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100476
Ben Dooks7c144502008-06-30 22:40:31 +0100477 if (host->mrq->data)
478 host->mrq->data->error = -EINVAL;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100479 }
480
Ben Dooksc2258892009-10-01 15:44:18 -0700481 s3cmci_enable_irq(host, false);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100482 finalize_request(host);
Roman Moracikd643b5f2008-06-30 22:40:28 +0100483 } else
Ben Dooksc2258892009-10-01 15:44:18 -0700484 s3cmci_enable_irq(host, true);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100485}
486
487/*
488 * ISR for SDI Interface IRQ
489 * Communication between driver and ISR works as follows:
490 * host->mrq points to current request
491 * host->complete_what Indicates when the request is considered done
492 * COMPLETION_CMDSENT when the command was sent
493 * COMPLETION_RSPFIN when a response was received
494 * COMPLETION_XFERFINISH when the data transfer is finished
495 * COMPLETION_XFERFINISH_RSPFIN both of the above.
496 * host->complete_request is the completion-object the driver waits for
497 *
498 * 1) Driver sets up host->mrq and host->complete_what
499 * 2) Driver prepares the transfer
500 * 3) Driver enables interrupts
501 * 4) Driver starts transfer
502 * 5) Driver waits for host->complete_rquest
503 * 6) ISR checks for request status (errors and success)
504 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
505 * 7) ISR completes host->complete_request
506 * 8) ISR disables interrupts
507 * 9) Driver wakes up and takes care of the request
508 *
509 * Note: "->error"-fields are expected to be set to 0 before the request
510 * was issued by mmc.c - therefore they are only set, when an error
511 * contition comes up
512 */
513
514static irqreturn_t s3cmci_irq(int irq, void *dev_id)
515{
516 struct s3cmci_host *host = dev_id;
517 struct mmc_command *cmd;
518 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
Ben Dooksc2258892009-10-01 15:44:18 -0700519 u32 mci_cclear = 0, mci_dclear;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100520 unsigned long iflags;
521
Ben Dooksc2258892009-10-01 15:44:18 -0700522 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
523 mci_imsk = readl(host->base + host->sdiimsk);
524
525 if (mci_dsta & S3C2410_SDIDSTA_SDIOIRQDETECT) {
526 if (mci_imsk & S3C2410_SDIIMSK_SDIOIRQ) {
527 mci_dclear = S3C2410_SDIDSTA_SDIOIRQDETECT;
528 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
529
530 mmc_signal_sdio_irq(host->mmc);
531 return IRQ_HANDLED;
532 }
533 }
534
Thomas Kleffelbe518012008-06-30 22:40:24 +0100535 spin_lock_irqsave(&host->complete_lock, iflags);
536
537 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100538 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
539 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100540 mci_dclear = 0;
541
542 if ((host->complete_what == COMPLETION_NONE) ||
543 (host->complete_what == COMPLETION_FINALIZE)) {
544 host->status = "nothing to complete";
545 clear_imask(host);
546 goto irq_out;
547 }
548
549 if (!host->mrq) {
550 host->status = "no active mrq";
551 clear_imask(host);
552 goto irq_out;
553 }
554
555 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
556
557 if (!cmd) {
558 host->status = "no active cmd";
559 clear_imask(host);
560 goto irq_out;
561 }
562
563 if (!host->dodma) {
564 if ((host->pio_active == XFER_WRITE) &&
565 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
566
567 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
568 tasklet_schedule(&host->pio_tasklet);
569 host->status = "pio tx";
570 }
571
572 if ((host->pio_active == XFER_READ) &&
573 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
574
575 disable_imask(host,
576 S3C2410_SDIIMSK_RXFIFOHALF |
577 S3C2410_SDIIMSK_RXFIFOLAST);
578
579 tasklet_schedule(&host->pio_tasklet);
580 host->status = "pio rx";
581 }
582 }
583
584 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100585 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100586 cmd->error = -ETIMEDOUT;
587 host->status = "error: command timeout";
588 goto fail_transfer;
589 }
590
591 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
592 if (host->complete_what == COMPLETION_CMDSENT) {
593 host->status = "ok: command sent";
594 goto close_transfer;
595 }
596
597 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
598 }
599
600 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
601 if (cmd->flags & MMC_RSP_CRC) {
Harald Welte679f0f82008-06-30 22:40:25 +0100602 if (host->mrq->cmd->flags & MMC_RSP_136) {
603 dbg(host, dbg_irq,
604 "fixup: ignore CRC fail with long rsp\n");
605 } else {
606 /* note, we used to fail the transfer
607 * here, but it seems that this is just
608 * the hardware getting it wrong.
609 *
610 * cmd->error = -EILSEQ;
611 * host->status = "error: bad command crc";
612 * goto fail_transfer;
613 */
614 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100615 }
616
617 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
618 }
619
620 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
621 if (host->complete_what == COMPLETION_RSPFIN) {
622 host->status = "ok: command response received";
623 goto close_transfer;
624 }
625
626 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
627 host->complete_what = COMPLETION_XFERFINISH;
628
629 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
630 }
631
632 /* errors handled after this point are only relevant
633 when a data transfer is in progress */
634
635 if (!cmd->data)
636 goto clear_status_bits;
637
638 /* Check for FIFO failure */
639 if (host->is2440) {
640 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100641 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100642 host->mrq->data->error = -EILSEQ;
643 host->status = "error: 2440 fifo failure";
644 goto fail_transfer;
645 }
646 } else {
647 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100648 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100649 cmd->data->error = -EILSEQ;
650 host->status = "error: fifo failure";
651 goto fail_transfer;
652 }
653 }
654
655 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100656 dbg(host, dbg_err, "bad data crc (outgoing)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100657 cmd->data->error = -EILSEQ;
658 host->status = "error: bad data crc (outgoing)";
659 goto fail_transfer;
660 }
661
662 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100663 dbg(host, dbg_err, "bad data crc (incoming)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100664 cmd->data->error = -EILSEQ;
665 host->status = "error: bad data crc (incoming)";
666 goto fail_transfer;
667 }
668
669 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100670 dbg(host, dbg_err, "data timeout\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100671 cmd->data->error = -ETIMEDOUT;
672 host->status = "error: data timeout";
673 goto fail_transfer;
674 }
675
676 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
677 if (host->complete_what == COMPLETION_XFERFINISH) {
678 host->status = "ok: data transfer completed";
679 goto close_transfer;
680 }
681
682 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
683 host->complete_what = COMPLETION_RSPFIN;
684
685 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
686 }
687
688clear_status_bits:
689 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
690 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
691
692 goto irq_out;
693
694fail_transfer:
695 host->pio_active = XFER_NONE;
696
697close_transfer:
698 host->complete_what = COMPLETION_FINALIZE;
699
700 clear_imask(host);
701 tasklet_schedule(&host->pio_tasklet);
702
703 goto irq_out;
704
705irq_out:
706 dbg(host, dbg_irq,
707 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
708 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
709
710 spin_unlock_irqrestore(&host->complete_lock, iflags);
711 return IRQ_HANDLED;
712
713}
714
715/*
716 * ISR for the CardDetect Pin
717*/
718
719static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
720{
721 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
722
723 dbg(host, dbg_irq, "card detect\n");
724
Ben Dooks2de5f792008-06-30 22:40:35 +0100725 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
Thomas Kleffelbe518012008-06-30 22:40:24 +0100726
727 return IRQ_HANDLED;
728}
729
Ben Dooks5d304402008-08-08 10:55:41 +0100730static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
731 void *buf_id, int size,
732 enum s3c2410_dma_buffresult result)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100733{
734 struct s3cmci_host *host = buf_id;
735 unsigned long iflags;
736 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
737
738 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
739 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
740 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
741 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
742
743 BUG_ON(!host->mrq);
744 BUG_ON(!host->mrq->data);
745 BUG_ON(!host->dmatogo);
746
747 spin_lock_irqsave(&host->complete_lock, iflags);
748
749 if (result != S3C2410_RES_OK) {
750 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
751 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
752 mci_csta, mci_dsta, mci_fsta,
753 mci_dcnt, result, host->dmatogo);
754
755 goto fail_request;
756 }
757
758 host->dmatogo--;
759 if (host->dmatogo) {
760 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
761 "DCNT:[%08x] toGo:%u\n",
762 size, mci_dsta, mci_dcnt, host->dmatogo);
763
764 goto out;
765 }
766
767 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
768 size, mci_dsta, mci_dcnt);
769
770 host->complete_what = COMPLETION_FINALIZE;
771
772out:
773 tasklet_schedule(&host->pio_tasklet);
774 spin_unlock_irqrestore(&host->complete_lock, iflags);
775 return;
776
Thomas Kleffelbe518012008-06-30 22:40:24 +0100777fail_request:
778 host->mrq->data->error = -EINVAL;
779 host->complete_what = COMPLETION_FINALIZE;
Ben Dookse6130ae2009-10-01 15:44:16 -0700780 clear_imask(host);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100781
Ben Dookse6130ae2009-10-01 15:44:16 -0700782 goto out;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100783}
784
785static void finalize_request(struct s3cmci_host *host)
786{
787 struct mmc_request *mrq = host->mrq;
788 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
789 int debug_as_failure = 0;
790
791 if (host->complete_what != COMPLETION_FINALIZE)
792 return;
793
794 if (!mrq)
795 return;
796
797 if (cmd->data && (cmd->error == 0) &&
798 (cmd->data->error == 0)) {
799 if (host->dodma && (!host->dma_complete)) {
800 dbg(host, dbg_dma, "DMA Missing!\n");
801 return;
802 }
803 }
804
805 /* Read response from controller. */
806 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
807 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
808 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
809 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
810
811 writel(host->prescaler, host->base + S3C2410_SDIPRE);
812
813 if (cmd->error)
814 debug_as_failure = 1;
815
816 if (cmd->data && cmd->data->error)
817 debug_as_failure = 1;
818
819 dbg_dumpcmd(host, cmd, debug_as_failure);
820
821 /* Cleanup controller */
822 writel(0, host->base + S3C2410_SDICMDARG);
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100823 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100824 writel(0, host->base + S3C2410_SDICMDCON);
Ben Dookse6130ae2009-10-01 15:44:16 -0700825 clear_imask(host);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100826
827 if (cmd->data && cmd->error)
828 cmd->data->error = cmd->error;
829
830 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
831 host->cmd_is_stop = 1;
832 s3cmci_send_request(host->mmc);
833 return;
834 }
835
836 /* If we have no data transfer we are finished here */
837 if (!mrq->data)
838 goto request_done;
839
840 /* Calulate the amout of bytes transfer if there was no error */
841 if (mrq->data->error == 0) {
842 mrq->data->bytes_xfered =
843 (mrq->data->blocks * mrq->data->blksz);
844 } else {
845 mrq->data->bytes_xfered = 0;
846 }
847
848 /* If we had an error while transfering data we flush the
849 * DMA channel and the fifo to clear out any garbage. */
850 if (mrq->data->error != 0) {
851 if (host->dodma)
852 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
853
854 if (host->is2440) {
855 /* Clear failure register and reset fifo. */
856 writel(S3C2440_SDIFSTA_FIFORESET |
857 S3C2440_SDIFSTA_FIFOFAIL,
858 host->base + S3C2410_SDIFSTA);
859 } else {
860 u32 mci_con;
861
862 /* reset fifo */
863 mci_con = readl(host->base + S3C2410_SDICON);
864 mci_con |= S3C2410_SDICON_FIFORESET;
865
866 writel(mci_con, host->base + S3C2410_SDICON);
867 }
868 }
869
870request_done:
871 host->complete_what = COMPLETION_NONE;
872 host->mrq = NULL;
Ben Dooksc2258892009-10-01 15:44:18 -0700873
874 s3cmci_check_sdio_irq(host);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100875 mmc_request_done(host->mmc, mrq);
876}
877
Ben Dooks5d304402008-08-08 10:55:41 +0100878static void s3cmci_dma_setup(struct s3cmci_host *host,
879 enum s3c2410_dmasrc source)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100880{
881 static enum s3c2410_dmasrc last_source = -1;
882 static int setup_ok;
883
884 if (last_source == source)
885 return;
886
887 last_source = source;
888
Ben Dooks8970ef42009-03-19 15:02:34 +0000889 s3c2410_dma_devconfig(host->dma, source,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100890 host->mem->start + host->sdidata);
891
892 if (!setup_ok) {
Ben Dooksfe9db6c2009-06-08 23:33:56 +0100893 s3c2410_dma_config(host->dma, 4);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100894 s3c2410_dma_set_buffdone_fn(host->dma,
895 s3cmci_dma_done_callback);
896 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
897 setup_ok = 1;
898 }
899}
900
901static void s3cmci_send_command(struct s3cmci_host *host,
902 struct mmc_command *cmd)
903{
904 u32 ccon, imsk;
905
906 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
907 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
908 S3C2410_SDIIMSK_RESPONSECRC;
909
910 enable_imask(host, imsk);
911
912 if (cmd->data)
913 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
914 else if (cmd->flags & MMC_RSP_PRESENT)
915 host->complete_what = COMPLETION_RSPFIN;
916 else
917 host->complete_what = COMPLETION_CMDSENT;
918
919 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
920
921 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
922 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
923
924 if (cmd->flags & MMC_RSP_PRESENT)
925 ccon |= S3C2410_SDICMDCON_WAITRSP;
926
927 if (cmd->flags & MMC_RSP_136)
928 ccon |= S3C2410_SDICMDCON_LONGRSP;
929
930 writel(ccon, host->base + S3C2410_SDICMDCON);
931}
932
933static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
934{
935 u32 dcon, imsk, stoptries = 3;
936
937 /* write DCON register */
938
939 if (!data) {
940 writel(0, host->base + S3C2410_SDIDCON);
941 return 0;
942 }
943
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100944 if ((data->blksz & 3) != 0) {
945 /* We cannot deal with unaligned blocks with more than
946 * one block being transfered. */
947
Christer Weinigel088a78a2008-10-15 00:17:17 +0100948 if (data->blocks > 1) {
949 pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100950 return -EINVAL;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100951 }
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100952 }
953
Thomas Kleffelbe518012008-06-30 22:40:24 +0100954 while (readl(host->base + S3C2410_SDIDSTA) &
955 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
956
957 dbg(host, dbg_err,
958 "mci_setup_data() transfer stillin progress.\n");
959
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100960 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100961 s3cmci_reset(host);
962
963 if ((stoptries--) == 0) {
964 dbg_dumpregs(host, "DRF");
965 return -EINVAL;
966 }
967 }
968
969 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
970
971 if (host->dodma)
972 dcon |= S3C2410_SDIDCON_DMAEN;
973
974 if (host->bus_width == MMC_BUS_WIDTH_4)
975 dcon |= S3C2410_SDIDCON_WIDEBUS;
976
977 if (!(data->flags & MMC_DATA_STREAM))
978 dcon |= S3C2410_SDIDCON_BLOCKMODE;
979
980 if (data->flags & MMC_DATA_WRITE) {
981 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
982 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
983 }
984
985 if (data->flags & MMC_DATA_READ) {
986 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
987 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
988 }
989
990 if (host->is2440) {
991 dcon |= S3C2440_SDIDCON_DS_WORD;
992 dcon |= S3C2440_SDIDCON_DATSTART;
993 }
994
995 writel(dcon, host->base + S3C2410_SDIDCON);
996
997 /* write BSIZE register */
998
999 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
1000
1001 /* add to IMASK register */
1002 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
1003 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
1004
1005 enable_imask(host, imsk);
1006
1007 /* write TIMER register */
1008
1009 if (host->is2440) {
1010 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
1011 } else {
1012 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
1013
1014 /* FIX: set slow clock to prevent timeouts on read */
1015 if (data->flags & MMC_DATA_READ)
1016 writel(0xFF, host->base + S3C2410_SDIPRE);
1017 }
1018
1019 return 0;
1020}
1021
1022#define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
1023
1024static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
1025{
1026 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
1027
1028 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1029
1030 host->pio_sgptr = 0;
Christer Weinigel088a78a2008-10-15 00:17:17 +01001031 host->pio_bytes = 0;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001032 host->pio_count = 0;
1033 host->pio_active = rw ? XFER_WRITE : XFER_READ;
1034
1035 if (rw) {
1036 do_pio_write(host);
1037 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
1038 } else {
1039 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
1040 | S3C2410_SDIIMSK_RXFIFOLAST);
1041 }
1042
1043 return 0;
1044}
1045
1046static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
1047{
1048 int dma_len, i;
1049 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
1050
1051 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
1052
1053 s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
1054 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1055
1056 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
1057 (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
1058
1059 if (dma_len == 0)
1060 return -ENOMEM;
1061
1062 host->dma_complete = 0;
1063 host->dmatogo = dma_len;
1064
1065 for (i = 0; i < dma_len; i++) {
1066 int res;
1067
1068 dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
1069 sg_dma_address(&data->sg[i]),
1070 sg_dma_len(&data->sg[i]));
1071
1072 res = s3c2410_dma_enqueue(host->dma, (void *) host,
1073 sg_dma_address(&data->sg[i]),
1074 sg_dma_len(&data->sg[i]));
1075
1076 if (res) {
1077 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
1078 return -EBUSY;
1079 }
1080 }
1081
1082 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
1083
1084 return 0;
1085}
1086
1087static void s3cmci_send_request(struct mmc_host *mmc)
1088{
1089 struct s3cmci_host *host = mmc_priv(mmc);
1090 struct mmc_request *mrq = host->mrq;
1091 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
1092
1093 host->ccnt++;
1094 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
1095
1096 /* Clear command, data and fifo status registers
1097 Fifo clear only necessary on 2440, but doesn't hurt on 2410
1098 */
1099 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1100 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1101 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1102
1103 if (cmd->data) {
1104 int res = s3cmci_setup_data(host, cmd->data);
1105
1106 host->dcnt++;
1107
1108 if (res) {
Ben Dooksff8c8042008-06-30 22:40:37 +01001109 dbg(host, dbg_err, "setup data error %d\n", res);
1110 cmd->error = res;
1111 cmd->data->error = res;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001112
1113 mmc_request_done(mmc, mrq);
1114 return;
1115 }
1116
1117 if (host->dodma)
1118 res = s3cmci_prepare_dma(host, cmd->data);
1119 else
1120 res = s3cmci_prepare_pio(host, cmd->data);
1121
1122 if (res) {
Ben Dooksff8c8042008-06-30 22:40:37 +01001123 dbg(host, dbg_err, "data prepare error %d\n", res);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001124 cmd->error = res;
1125 cmd->data->error = res;
1126
1127 mmc_request_done(mmc, mrq);
1128 return;
1129 }
1130 }
1131
1132 /* Send command */
1133 s3cmci_send_command(host, cmd);
1134
1135 /* Enable Interrupt */
Ben Dooksc2258892009-10-01 15:44:18 -07001136 s3cmci_enable_irq(host, true);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001137}
1138
Ben Dooks87dd9802008-08-12 09:24:50 +01001139static int s3cmci_card_present(struct mmc_host *mmc)
Ben Dooks50a84572008-06-30 22:40:36 +01001140{
Ben Dooks87dd9802008-08-12 09:24:50 +01001141 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dooks50a84572008-06-30 22:40:36 +01001142 struct s3c24xx_mci_pdata *pdata = host->pdata;
1143 int ret;
1144
1145 if (pdata->gpio_detect == 0)
1146 return -ENOSYS;
1147
Ben Dooks916a3072009-10-01 15:44:15 -07001148 ret = gpio_get_value(pdata->gpio_detect) ? 0 : 1;
Ben Dooks50a84572008-06-30 22:40:36 +01001149 return ret ^ pdata->detect_invert;
1150}
1151
Thomas Kleffelbe518012008-06-30 22:40:24 +01001152static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1153{
1154 struct s3cmci_host *host = mmc_priv(mmc);
1155
1156 host->status = "mmc request";
1157 host->cmd_is_stop = 0;
1158 host->mrq = mrq;
1159
Ben Dooks87dd9802008-08-12 09:24:50 +01001160 if (s3cmci_card_present(mmc) == 0) {
Ben Dooks50a84572008-06-30 22:40:36 +01001161 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1162 host->mrq->cmd->error = -ENOMEDIUM;
1163 mmc_request_done(mmc, mrq);
1164 } else
1165 s3cmci_send_request(mmc);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001166}
1167
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001168static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1169{
1170 u32 mci_psc;
1171
1172 /* Set clock */
1173 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1174 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1175
1176 if (host->real_rate <= ios->clock)
1177 break;
1178 }
1179
1180 if (mci_psc > 255)
1181 mci_psc = 255;
1182
1183 host->prescaler = mci_psc;
1184 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1185
1186 /* If requested clock is 0, real_rate will be 0, too */
1187 if (ios->clock == 0)
1188 host->real_rate = 0;
1189}
1190
Thomas Kleffelbe518012008-06-30 22:40:24 +01001191static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1192{
1193 struct s3cmci_host *host = mmc_priv(mmc);
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001194 u32 mci_con;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001195
1196 /* Set the power state */
1197
1198 mci_con = readl(host->base + S3C2410_SDICON);
1199
1200 switch (ios->power_mode) {
1201 case MMC_POWER_ON:
1202 case MMC_POWER_UP:
Ben Dooks916a3072009-10-01 15:44:15 -07001203 s3c2410_gpio_cfgpin(S3C2410_GPE(5), S3C2410_GPE5_SDCLK);
1204 s3c2410_gpio_cfgpin(S3C2410_GPE(6), S3C2410_GPE6_SDCMD);
1205 s3c2410_gpio_cfgpin(S3C2410_GPE(7), S3C2410_GPE7_SDDAT0);
1206 s3c2410_gpio_cfgpin(S3C2410_GPE(8), S3C2410_GPE8_SDDAT1);
1207 s3c2410_gpio_cfgpin(S3C2410_GPE(9), S3C2410_GPE9_SDDAT2);
1208 s3c2410_gpio_cfgpin(S3C2410_GPE(10), S3C2410_GPE10_SDDAT3);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001209
Ben Dooksedb5a982008-06-30 22:40:29 +01001210 if (host->pdata->set_power)
1211 host->pdata->set_power(ios->power_mode, ios->vdd);
1212
Thomas Kleffelbe518012008-06-30 22:40:24 +01001213 if (!host->is2440)
1214 mci_con |= S3C2410_SDICON_FIFORESET;
1215
1216 break;
1217
1218 case MMC_POWER_OFF:
1219 default:
Ben Dooks916a3072009-10-01 15:44:15 -07001220 gpio_direction_output(S3C2410_GPE(5), 0);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001221
1222 if (host->is2440)
1223 mci_con |= S3C2440_SDICON_SDRESET;
1224
Ben Dooksedb5a982008-06-30 22:40:29 +01001225 if (host->pdata->set_power)
1226 host->pdata->set_power(ios->power_mode, ios->vdd);
1227
Thomas Kleffelbe518012008-06-30 22:40:24 +01001228 break;
1229 }
1230
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001231 s3cmci_set_clk(host, ios);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001232
1233 /* Set CLOCK_ENABLE */
1234 if (ios->clock)
1235 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1236 else
1237 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1238
1239 writel(mci_con, host->base + S3C2410_SDICON);
1240
1241 if ((ios->power_mode == MMC_POWER_ON) ||
1242 (ios->power_mode == MMC_POWER_UP)) {
1243 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1244 host->real_rate/1000, ios->clock/1000);
1245 } else {
1246 dbg(host, dbg_conf, "powered down.\n");
1247 }
1248
1249 host->bus_width = ios->bus_width;
1250}
1251
1252static void s3cmci_reset(struct s3cmci_host *host)
1253{
1254 u32 con = readl(host->base + S3C2410_SDICON);
1255
1256 con |= S3C2440_SDICON_SDRESET;
1257 writel(con, host->base + S3C2410_SDICON);
1258}
1259
Ben Dooksedb5a982008-06-30 22:40:29 +01001260static int s3cmci_get_ro(struct mmc_host *mmc)
1261{
1262 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dookscf0984c2008-06-30 22:40:30 +01001263 struct s3c24xx_mci_pdata *pdata = host->pdata;
1264 int ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001265
Ben Dookscf0984c2008-06-30 22:40:30 +01001266 if (pdata->gpio_wprotect == 0)
Ben Dooksedb5a982008-06-30 22:40:29 +01001267 return 0;
1268
Ben Dookscf0984c2008-06-30 22:40:30 +01001269 ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
1270
1271 if (pdata->wprotect_invert)
1272 ret = !ret;
1273
1274 return ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001275}
1276
Ben Dooksc2258892009-10-01 15:44:18 -07001277static void s3cmci_enable_sdio_irq(struct mmc_host *mmc, int enable)
1278{
1279 struct s3cmci_host *host = mmc_priv(mmc);
1280 unsigned long flags;
1281 u32 con;
1282
1283 local_irq_save(flags);
1284
1285 con = readl(host->base + S3C2410_SDICON);
1286 host->sdio_irqen = enable;
1287
1288 if (enable == host->sdio_irqen)
1289 goto same_state;
1290
1291 if (enable) {
1292 con |= S3C2410_SDICON_SDIOIRQ;
1293 enable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1294
1295 if (!host->irq_state && !host->irq_disabled) {
1296 host->irq_state = true;
1297 enable_irq(host->irq);
1298 }
1299 } else {
1300 disable_imask(host, S3C2410_SDIIMSK_SDIOIRQ);
1301 con &= ~S3C2410_SDICON_SDIOIRQ;
1302
1303 if (!host->irq_enabled && host->irq_state) {
1304 disable_irq_nosync(host->irq);
1305 host->irq_state = false;
1306 }
1307 }
1308
1309 writel(con, host->base + S3C2410_SDICON);
1310
1311 same_state:
1312 local_irq_restore(flags);
1313
1314 s3cmci_check_sdio_irq(host);
1315}
1316
Thomas Kleffelbe518012008-06-30 22:40:24 +01001317static struct mmc_host_ops s3cmci_ops = {
1318 .request = s3cmci_request,
1319 .set_ios = s3cmci_set_ios,
Ben Dooksedb5a982008-06-30 22:40:29 +01001320 .get_ro = s3cmci_get_ro,
Ben Dooks87dd9802008-08-12 09:24:50 +01001321 .get_cd = s3cmci_card_present,
Ben Dooksc2258892009-10-01 15:44:18 -07001322 .enable_sdio_irq = s3cmci_enable_sdio_irq,
Ben Dooksedb5a982008-06-30 22:40:29 +01001323};
1324
1325static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1326 /* This is currently here to avoid a number of if (host->pdata)
1327 * checks. Any zero fields to ensure reaonable defaults are picked. */
Thomas Kleffelbe518012008-06-30 22:40:24 +01001328};
1329
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001330#ifdef CONFIG_CPU_FREQ
1331
1332static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1333 unsigned long val, void *data)
1334{
1335 struct s3cmci_host *host;
1336 struct mmc_host *mmc;
1337 unsigned long newclk;
1338 unsigned long flags;
1339
1340 host = container_of(nb, struct s3cmci_host, freq_transition);
1341 newclk = clk_get_rate(host->clk);
1342 mmc = host->mmc;
1343
1344 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1345 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1346 spin_lock_irqsave(&mmc->lock, flags);
1347
1348 host->clk_rate = newclk;
1349
1350 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1351 mmc->ios.clock != 0)
1352 s3cmci_set_clk(host, &mmc->ios);
1353
1354 spin_unlock_irqrestore(&mmc->lock, flags);
1355 }
1356
1357 return 0;
1358}
1359
1360static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1361{
1362 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1363
1364 return cpufreq_register_notifier(&host->freq_transition,
1365 CPUFREQ_TRANSITION_NOTIFIER);
1366}
1367
1368static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1369{
1370 cpufreq_unregister_notifier(&host->freq_transition,
1371 CPUFREQ_TRANSITION_NOTIFIER);
1372}
1373
1374#else
1375static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1376{
1377 return 0;
1378}
1379
1380static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1381{
1382}
1383#endif
1384
Ben Dooks916a3072009-10-01 15:44:15 -07001385
Ben Dooks9bdd2032009-10-01 15:44:17 -07001386#ifdef CONFIG_DEBUG_FS
1387
1388static int s3cmci_state_show(struct seq_file *seq, void *v)
1389{
1390 struct s3cmci_host *host = seq->private;
1391
1392 seq_printf(seq, "Register base = 0x%08x\n", (u32)host->base);
1393 seq_printf(seq, "Clock rate = %ld\n", host->clk_rate);
1394 seq_printf(seq, "Prescale = %d\n", host->prescaler);
1395 seq_printf(seq, "is2440 = %d\n", host->is2440);
1396 seq_printf(seq, "IRQ = %d\n", host->irq);
Ben Dooksc2258892009-10-01 15:44:18 -07001397 seq_printf(seq, "IRQ enabled = %d\n", host->irq_enabled);
1398 seq_printf(seq, "IRQ disabled = %d\n", host->irq_disabled);
1399 seq_printf(seq, "IRQ state = %d\n", host->irq_state);
Ben Dooks9bdd2032009-10-01 15:44:17 -07001400 seq_printf(seq, "CD IRQ = %d\n", host->irq_cd);
1401 seq_printf(seq, "Do DMA = %d\n", host->dodma);
1402 seq_printf(seq, "SDIIMSK at %d\n", host->sdiimsk);
1403 seq_printf(seq, "SDIDATA at %d\n", host->sdidata);
1404
1405 return 0;
1406}
1407
1408static int s3cmci_state_open(struct inode *inode, struct file *file)
1409{
1410 return single_open(file, s3cmci_state_show, inode->i_private);
1411}
1412
1413static const struct file_operations s3cmci_fops_state = {
1414 .owner = THIS_MODULE,
1415 .open = s3cmci_state_open,
1416 .read = seq_read,
1417 .llseek = seq_lseek,
1418 .release = single_release,
1419};
1420
1421#define DBG_REG(_r) { .addr = S3C2410_SDI##_r, .name = #_r }
1422
1423struct s3cmci_reg {
1424 unsigned short addr;
1425 unsigned char *name;
1426} debug_regs[] = {
1427 DBG_REG(CON),
1428 DBG_REG(PRE),
1429 DBG_REG(CMDARG),
1430 DBG_REG(CMDCON),
1431 DBG_REG(CMDSTAT),
1432 DBG_REG(RSP0),
1433 DBG_REG(RSP1),
1434 DBG_REG(RSP2),
1435 DBG_REG(RSP3),
1436 DBG_REG(TIMER),
1437 DBG_REG(BSIZE),
1438 DBG_REG(DCON),
1439 DBG_REG(DCNT),
1440 DBG_REG(DSTA),
1441 DBG_REG(FSTA),
1442 {}
1443};
1444
1445static int s3cmci_regs_show(struct seq_file *seq, void *v)
1446{
1447 struct s3cmci_host *host = seq->private;
1448 struct s3cmci_reg *rptr = debug_regs;
1449
1450 for (; rptr->name; rptr++)
1451 seq_printf(seq, "SDI%s\t=0x%08x\n", rptr->name,
1452 readl(host->base + rptr->addr));
1453
1454 seq_printf(seq, "SDIIMSK\t=0x%08x\n", readl(host->base + host->sdiimsk));
1455
1456 return 0;
1457}
1458
1459static int s3cmci_regs_open(struct inode *inode, struct file *file)
1460{
1461 return single_open(file, s3cmci_regs_show, inode->i_private);
1462}
1463
1464static const struct file_operations s3cmci_fops_regs = {
1465 .owner = THIS_MODULE,
1466 .open = s3cmci_regs_open,
1467 .read = seq_read,
1468 .llseek = seq_lseek,
1469 .release = single_release,
1470};
1471
1472static void s3cmci_debugfs_attach(struct s3cmci_host *host)
1473{
1474 struct device *dev = &host->pdev->dev;
1475
1476 host->debug_root = debugfs_create_dir(dev_name(dev), NULL);
1477 if (IS_ERR(host->debug_root)) {
1478 dev_err(dev, "failed to create debugfs root\n");
1479 return;
1480 }
1481
1482 host->debug_state = debugfs_create_file("state", 0444,
1483 host->debug_root, host,
1484 &s3cmci_fops_state);
1485
1486 if (IS_ERR(host->debug_state))
1487 dev_err(dev, "failed to create debug state file\n");
1488
1489 host->debug_regs = debugfs_create_file("regs", 0444,
1490 host->debug_root, host,
1491 &s3cmci_fops_regs);
1492
1493 if (IS_ERR(host->debug_regs))
1494 dev_err(dev, "failed to create debug regs file\n");
1495}
1496
1497static void s3cmci_debugfs_remove(struct s3cmci_host *host)
1498{
1499 debugfs_remove(host->debug_regs);
1500 debugfs_remove(host->debug_state);
1501 debugfs_remove(host->debug_root);
1502}
1503
1504#else
1505static inline void s3cmci_debugfs_attach(struct s3cmci_host *host) { }
1506static inline void s3cmci_debugfs_remove(struct s3cmci_host *host) { }
1507
1508#endif /* CONFIG_DEBUG_FS */
1509
Ben Dooks44d0e1992009-10-01 15:44:14 -07001510static int __devinit s3cmci_probe(struct platform_device *pdev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001511{
1512 struct s3cmci_host *host;
1513 struct mmc_host *mmc;
1514 int ret;
Ben Dooks44d0e1992009-10-01 15:44:14 -07001515 int is2440;
Ben Dooks916a3072009-10-01 15:44:15 -07001516 int i;
Ben Dooks44d0e1992009-10-01 15:44:14 -07001517
1518 is2440 = platform_get_device_id(pdev)->driver_data;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001519
1520 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1521 if (!mmc) {
1522 ret = -ENOMEM;
1523 goto probe_out;
1524 }
1525
Ben Dooks916a3072009-10-01 15:44:15 -07001526 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++) {
1527 ret = gpio_request(i, dev_name(&pdev->dev));
1528 if (ret) {
1529 dev_err(&pdev->dev, "failed to get gpio %d\n", i);
1530
1531 for (i--; i >= S3C2410_GPE(5); i--)
1532 gpio_free(i);
1533
1534 goto probe_free_host;
1535 }
1536 }
1537
Thomas Kleffelbe518012008-06-30 22:40:24 +01001538 host = mmc_priv(mmc);
1539 host->mmc = mmc;
1540 host->pdev = pdev;
1541 host->is2440 = is2440;
1542
Ben Dooksedb5a982008-06-30 22:40:29 +01001543 host->pdata = pdev->dev.platform_data;
1544 if (!host->pdata) {
1545 pdev->dev.platform_data = &s3cmci_def_pdata;
1546 host->pdata = &s3cmci_def_pdata;
1547 }
1548
Thomas Kleffelbe518012008-06-30 22:40:24 +01001549 spin_lock_init(&host->complete_lock);
1550 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1551
1552 if (is2440) {
1553 host->sdiimsk = S3C2440_SDIIMSK;
1554 host->sdidata = S3C2440_SDIDATA;
1555 host->clk_div = 1;
1556 } else {
1557 host->sdiimsk = S3C2410_SDIIMSK;
1558 host->sdidata = S3C2410_SDIDATA;
1559 host->clk_div = 2;
1560 }
1561
1562 host->dodma = 0;
1563 host->complete_what = COMPLETION_NONE;
1564 host->pio_active = XFER_NONE;
1565
1566 host->dma = S3CMCI_DMA;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001567
1568 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1569 if (!host->mem) {
1570 dev_err(&pdev->dev,
1571 "failed to get io memory region resouce.\n");
1572
1573 ret = -ENOENT;
Ben Dooks916a3072009-10-01 15:44:15 -07001574 goto probe_free_gpio;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001575 }
1576
1577 host->mem = request_mem_region(host->mem->start,
Ben Dooksda52a7c2009-10-01 15:44:13 -07001578 resource_size(host->mem), pdev->name);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001579
1580 if (!host->mem) {
1581 dev_err(&pdev->dev, "failed to request io memory region.\n");
1582 ret = -ENOENT;
Ben Dooks916a3072009-10-01 15:44:15 -07001583 goto probe_free_gpio;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001584 }
1585
Ben Dooksda52a7c2009-10-01 15:44:13 -07001586 host->base = ioremap(host->mem->start, resource_size(host->mem));
Ben Dooks5d304402008-08-08 10:55:41 +01001587 if (!host->base) {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001588 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1589 ret = -EINVAL;
1590 goto probe_free_mem_region;
1591 }
1592
1593 host->irq = platform_get_irq(pdev, 0);
1594 if (host->irq == 0) {
1595 dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
1596 ret = -EINVAL;
1597 goto probe_iounmap;
1598 }
1599
1600 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1601 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1602 ret = -ENOENT;
1603 goto probe_iounmap;
1604 }
1605
1606 /* We get spurious interrupts even when we have set the IMSK
1607 * register to ignore everything, so use disable_irq() to make
1608 * ensure we don't lock the system with un-serviceable requests. */
1609
1610 disable_irq(host->irq);
Ben Dooksc2258892009-10-01 15:44:18 -07001611 host->irq_state = false;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001612
Ben Dooks916a3072009-10-01 15:44:15 -07001613 if (host->pdata->gpio_detect) {
1614 ret = gpio_request(host->pdata->gpio_detect, "s3cmci detect");
1615 if (ret) {
1616 dev_err(&pdev->dev, "failed to get detect gpio\n");
1617 goto probe_free_irq;
1618 }
1619 }
1620
Ben Dooks55d70f52008-06-30 22:40:32 +01001621 host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001622
Ben Dooks55d70f52008-06-30 22:40:32 +01001623 if (host->irq_cd >= 0) {
1624 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1625 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1626 DRIVER_NAME, host)) {
1627 dev_err(&pdev->dev, "can't get card detect irq.\n");
1628 ret = -ENOENT;
Ben Dooks916a3072009-10-01 15:44:15 -07001629 goto probe_free_gpio_cd;
Ben Dooks55d70f52008-06-30 22:40:32 +01001630 }
1631 } else {
1632 dev_warn(&pdev->dev, "host detect has no irq available\n");
Ben Dooks916a3072009-10-01 15:44:15 -07001633 gpio_direction_input(host->pdata->gpio_detect);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001634 }
1635
Ben Dooks916a3072009-10-01 15:44:15 -07001636 if (host->pdata->gpio_wprotect) {
1637 ret = gpio_request(host->pdata->gpio_wprotect, "s3cmci wp");
1638 if (ret) {
1639 dev_err(&pdev->dev, "failed to get writeprotect\n");
1640 goto probe_free_irq_cd;
1641 }
1642
1643 gpio_direction_input(host->pdata->gpio_wprotect);
1644 }
Ben Dooksedb5a982008-06-30 22:40:29 +01001645
Ben Dooks3886ff52008-06-30 22:40:33 +01001646 if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001647 dev_err(&pdev->dev, "unable to get DMA channel.\n");
1648 ret = -EBUSY;
Ben Dooks916a3072009-10-01 15:44:15 -07001649 goto probe_free_gpio_wp;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001650 }
1651
1652 host->clk = clk_get(&pdev->dev, "sdi");
1653 if (IS_ERR(host->clk)) {
1654 dev_err(&pdev->dev, "failed to find clock source.\n");
1655 ret = PTR_ERR(host->clk);
1656 host->clk = NULL;
1657 goto probe_free_host;
1658 }
1659
1660 ret = clk_enable(host->clk);
1661 if (ret) {
1662 dev_err(&pdev->dev, "failed to enable clock source.\n");
1663 goto clk_free;
1664 }
1665
1666 host->clk_rate = clk_get_rate(host->clk);
1667
1668 mmc->ops = &s3cmci_ops;
Ben Dooksedb5a982008-06-30 22:40:29 +01001669 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Ben Dooksc2258892009-10-01 15:44:18 -07001670 mmc->caps = MMC_CAP_4_BIT_DATA | MMC_CAP_SDIO_IRQ;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001671 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1672 mmc->f_max = host->clk_rate / host->clk_div;
1673
Ben Dooksedb5a982008-06-30 22:40:29 +01001674 if (host->pdata->ocr_avail)
1675 mmc->ocr_avail = host->pdata->ocr_avail;
1676
Thomas Kleffelbe518012008-06-30 22:40:24 +01001677 mmc->max_blk_count = 4095;
1678 mmc->max_blk_size = 4095;
1679 mmc->max_req_size = 4095 * 512;
1680 mmc->max_seg_size = mmc->max_req_size;
1681
1682 mmc->max_phys_segs = 128;
1683 mmc->max_hw_segs = 128;
1684
1685 dbg(host, dbg_debug,
1686 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1687 (host->is2440?"2440":""),
1688 host->base, host->irq, host->irq_cd, host->dma);
1689
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001690 ret = s3cmci_cpufreq_register(host);
1691 if (ret) {
1692 dev_err(&pdev->dev, "failed to register cpufreq\n");
1693 goto free_dmabuf;
1694 }
1695
Thomas Kleffelbe518012008-06-30 22:40:24 +01001696 ret = mmc_add_host(mmc);
1697 if (ret) {
1698 dev_err(&pdev->dev, "failed to add mmc host.\n");
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001699 goto free_cpufreq;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001700 }
1701
Ben Dooks9bdd2032009-10-01 15:44:17 -07001702 s3cmci_debugfs_attach(host);
1703
Thomas Kleffelbe518012008-06-30 22:40:24 +01001704 platform_set_drvdata(pdev, mmc);
1705 dev_info(&pdev->dev, "initialisation done.\n");
1706
1707 return 0;
1708
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001709 free_cpufreq:
1710 s3cmci_cpufreq_deregister(host);
1711
Thomas Kleffelbe518012008-06-30 22:40:24 +01001712 free_dmabuf:
1713 clk_disable(host->clk);
1714
1715 clk_free:
1716 clk_put(host->clk);
1717
Ben Dooks916a3072009-10-01 15:44:15 -07001718 probe_free_gpio_wp:
1719 if (host->pdata->gpio_wprotect)
1720 gpio_free(host->pdata->gpio_wprotect);
1721
1722 probe_free_gpio_cd:
1723 if (host->pdata->gpio_detect)
1724 gpio_free(host->pdata->gpio_detect);
1725
Thomas Kleffelbe518012008-06-30 22:40:24 +01001726 probe_free_irq_cd:
Ben Dooks55d70f52008-06-30 22:40:32 +01001727 if (host->irq_cd >= 0)
1728 free_irq(host->irq_cd, host);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001729
1730 probe_free_irq:
1731 free_irq(host->irq, host);
1732
1733 probe_iounmap:
1734 iounmap(host->base);
1735
1736 probe_free_mem_region:
Ben Dooksda52a7c2009-10-01 15:44:13 -07001737 release_mem_region(host->mem->start, resource_size(host->mem));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001738
Ben Dooks916a3072009-10-01 15:44:15 -07001739 probe_free_gpio:
1740 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1741 gpio_free(i);
1742
Thomas Kleffelbe518012008-06-30 22:40:24 +01001743 probe_free_host:
1744 mmc_free_host(mmc);
Ben Dooks916a3072009-10-01 15:44:15 -07001745
Thomas Kleffelbe518012008-06-30 22:40:24 +01001746 probe_out:
1747 return ret;
1748}
1749
Ben Dooks907b2cd2008-07-17 15:32:54 +01001750static void s3cmci_shutdown(struct platform_device *pdev)
1751{
1752 struct mmc_host *mmc = platform_get_drvdata(pdev);
1753 struct s3cmci_host *host = mmc_priv(mmc);
1754
1755 if (host->irq_cd >= 0)
1756 free_irq(host->irq_cd, host);
1757
Ben Dooks9bdd2032009-10-01 15:44:17 -07001758 s3cmci_debugfs_remove(host);
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001759 s3cmci_cpufreq_deregister(host);
Ben Dooks907b2cd2008-07-17 15:32:54 +01001760 mmc_remove_host(mmc);
1761 clk_disable(host->clk);
1762}
1763
Thomas Kleffelbe518012008-06-30 22:40:24 +01001764static int __devexit s3cmci_remove(struct platform_device *pdev)
1765{
1766 struct mmc_host *mmc = platform_get_drvdata(pdev);
1767 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dooks916a3072009-10-01 15:44:15 -07001768 struct s3c24xx_mci_pdata *pd = host->pdata;
1769 int i;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001770
Ben Dooks907b2cd2008-07-17 15:32:54 +01001771 s3cmci_shutdown(pdev);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001772
Thomas Kleffelbe518012008-06-30 22:40:24 +01001773 clk_put(host->clk);
1774
1775 tasklet_disable(&host->pio_tasklet);
Harald Welteceb3ac22008-06-30 22:40:26 +01001776 s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001777
Thomas Kleffelbe518012008-06-30 22:40:24 +01001778 free_irq(host->irq, host);
1779
Ben Dooks916a3072009-10-01 15:44:15 -07001780 if (pd->gpio_wprotect)
1781 gpio_free(pd->gpio_wprotect);
1782
1783 if (pd->gpio_detect)
1784 gpio_free(pd->gpio_detect);
1785
1786 for (i = S3C2410_GPE(5); i <= S3C2410_GPE(10); i++)
1787 gpio_free(i);
1788
1789
Thomas Kleffelbe518012008-06-30 22:40:24 +01001790 iounmap(host->base);
Ben Dooksda52a7c2009-10-01 15:44:13 -07001791 release_mem_region(host->mem->start, resource_size(host->mem));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001792
1793 mmc_free_host(mmc);
1794 return 0;
1795}
1796
Ben Dooks44d0e1992009-10-01 15:44:14 -07001797static struct platform_device_id s3cmci_driver_ids[] = {
1798 {
1799 .name = "s3c2410-sdi",
1800 .driver_data = 0,
1801 }, {
1802 .name = "s3c2412-sdi",
1803 .driver_data = 1,
1804 }, {
1805 .name = "s3c2440-sdi",
1806 .driver_data = 1,
1807 },
1808 { }
1809};
Thomas Kleffelbe518012008-06-30 22:40:24 +01001810
Ben Dooks44d0e1992009-10-01 15:44:14 -07001811MODULE_DEVICE_TABLE(platform, s3cmci_driver_ids);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001812
Thomas Kleffelbe518012008-06-30 22:40:24 +01001813
1814#ifdef CONFIG_PM
1815
Ben Dooks50d7fa92009-10-01 15:44:15 -07001816static int s3cmci_suspend(struct device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001817{
Ben Dooks50d7fa92009-10-01 15:44:15 -07001818 struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
1819 struct pm_message event = { PM_EVENT_SUSPEND };
Thomas Kleffelbe518012008-06-30 22:40:24 +01001820
Ben Dooks50d7fa92009-10-01 15:44:15 -07001821 return mmc_suspend_host(mmc, event);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001822}
1823
Ben Dooks50d7fa92009-10-01 15:44:15 -07001824static int s3cmci_resume(struct device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001825{
Ben Dooks50d7fa92009-10-01 15:44:15 -07001826 struct mmc_host *mmc = platform_get_drvdata(to_platform_device(dev));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001827
1828 return mmc_resume_host(mmc);
1829}
1830
Ben Dooks50d7fa92009-10-01 15:44:15 -07001831static struct dev_pm_ops s3cmci_pm = {
1832 .suspend = s3cmci_suspend,
1833 .resume = s3cmci_resume,
1834};
1835
1836#define s3cmci_pm_ops &s3cmci_pm
Thomas Kleffelbe518012008-06-30 22:40:24 +01001837#else /* CONFIG_PM */
Ben Dooks50d7fa92009-10-01 15:44:15 -07001838#define s3cmci_pm_ops NULL
Thomas Kleffelbe518012008-06-30 22:40:24 +01001839#endif /* CONFIG_PM */
1840
1841
Ben Dooks44d0e1992009-10-01 15:44:14 -07001842static struct platform_driver s3cmci_driver = {
Ben Dooks50d7fa92009-10-01 15:44:15 -07001843 .driver = {
1844 .name = "s3c-sdi",
1845 .owner = THIS_MODULE,
1846 .pm = s3cmci_pm_ops,
1847 },
Ben Dooks44d0e1992009-10-01 15:44:14 -07001848 .id_table = s3cmci_driver_ids,
1849 .probe = s3cmci_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001850 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001851 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001852};
1853
Thomas Kleffelbe518012008-06-30 22:40:24 +01001854static int __init s3cmci_init(void)
1855{
Ben Dooks44d0e1992009-10-01 15:44:14 -07001856 return platform_driver_register(&s3cmci_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001857}
1858
1859static void __exit s3cmci_exit(void)
1860{
Ben Dooks44d0e1992009-10-01 15:44:14 -07001861 platform_driver_unregister(&s3cmci_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001862}
1863
1864module_init(s3cmci_init);
1865module_exit(s3cmci_exit);
1866
1867MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1868MODULE_LICENSE("GPL v2");
ben@fluff.org.uk08c55e22008-10-15 00:17:19 +01001869MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");