blob: 5211d90d34eff56b9079aab12662dc6656cab877 [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 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License version 2 as
8 * published by the Free Software Foundation.
9 */
10
11#include <linux/module.h>
12#include <linux/dma-mapping.h>
13#include <linux/clk.h>
14#include <linux/mmc/host.h>
15#include <linux/platform_device.h>
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +010016#include <linux/cpufreq.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010017#include <linux/irq.h>
18#include <linux/io.h>
19
20#include <asm/dma.h>
21
Russell Kinga09e64f2008-08-05 16:14:15 +010022#include <mach/regs-sdi.h>
23#include <mach/regs-gpio.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010024
Ben Dooksedb5a982008-06-30 22:40:29 +010025#include <asm/plat-s3c24xx/mci.h>
26
Thomas Kleffelbe518012008-06-30 22:40:24 +010027#include "s3cmci.h"
28
29#define DRIVER_NAME "s3c-mci"
30
31enum dbg_channels {
32 dbg_err = (1 << 0),
33 dbg_debug = (1 << 1),
34 dbg_info = (1 << 2),
35 dbg_irq = (1 << 3),
36 dbg_sg = (1 << 4),
37 dbg_dma = (1 << 5),
38 dbg_pio = (1 << 6),
39 dbg_fail = (1 << 7),
40 dbg_conf = (1 << 8),
41};
42
ben@fluff.org.uk9c2e7e42008-10-15 00:17:15 +010043static const int dbgmap_err = dbg_fail;
Thomas Kleffelbe518012008-06-30 22:40:24 +010044static const int dbgmap_info = dbg_info | dbg_conf;
ben@fluff.org.uk9c2e7e42008-10-15 00:17:15 +010045static const int dbgmap_debug = dbg_err | dbg_debug;
Thomas Kleffelbe518012008-06-30 22:40:24 +010046
47#define dbg(host, channels, args...) \
48 do { \
49 if (dbgmap_err & channels) \
50 dev_err(&host->pdev->dev, args); \
51 else if (dbgmap_info & channels) \
52 dev_info(&host->pdev->dev, args); \
53 else if (dbgmap_debug & channels) \
54 dev_dbg(&host->pdev->dev, args); \
55 } while (0)
56
57#define RESSIZE(ressource) (((ressource)->end - (ressource)->start)+1)
58
59static struct s3c2410_dma_client s3cmci_dma_client = {
60 .name = "s3c-mci",
61};
62
63static void finalize_request(struct s3cmci_host *host);
64static void s3cmci_send_request(struct mmc_host *mmc);
65static void s3cmci_reset(struct s3cmci_host *host);
66
67#ifdef CONFIG_MMC_DEBUG
68
69static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
70{
71 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
72 u32 datcon, datcnt, datsta, fsta, imask;
73
74 con = readl(host->base + S3C2410_SDICON);
75 pre = readl(host->base + S3C2410_SDIPRE);
76 cmdarg = readl(host->base + S3C2410_SDICMDARG);
77 cmdcon = readl(host->base + S3C2410_SDICMDCON);
78 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
79 r0 = readl(host->base + S3C2410_SDIRSP0);
80 r1 = readl(host->base + S3C2410_SDIRSP1);
81 r2 = readl(host->base + S3C2410_SDIRSP2);
82 r3 = readl(host->base + S3C2410_SDIRSP3);
83 timer = readl(host->base + S3C2410_SDITIMER);
84 bsize = readl(host->base + S3C2410_SDIBSIZE);
85 datcon = readl(host->base + S3C2410_SDIDCON);
86 datcnt = readl(host->base + S3C2410_SDIDCNT);
87 datsta = readl(host->base + S3C2410_SDIDSTA);
88 fsta = readl(host->base + S3C2410_SDIFSTA);
89 imask = readl(host->base + host->sdiimsk);
90
91 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
92 prefix, con, pre, timer);
93
94 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
95 prefix, cmdcon, cmdarg, cmdsta);
96
97 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
98 " DSTA:[%08x] DCNT:[%08x]\n",
99 prefix, datcon, fsta, datsta, datcnt);
100
101 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
102 " R2:[%08x] R3:[%08x]\n",
103 prefix, r0, r1, r2, r3);
104}
105
106static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
107 int stop)
108{
109 snprintf(host->dbgmsg_cmd, 300,
110 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
111 host->ccnt, (stop ? " (STOP)" : ""),
112 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
113
114 if (cmd->data) {
115 snprintf(host->dbgmsg_dat, 300,
116 "#%u bsize:%u blocks:%u bytes:%u",
117 host->dcnt, cmd->data->blksz,
118 cmd->data->blocks,
119 cmd->data->blocks * cmd->data->blksz);
120 } else {
121 host->dbgmsg_dat[0] = '\0';
122 }
123}
124
125static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
126 int fail)
127{
128 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
129
130 if (!cmd)
131 return;
132
133 if (cmd->error == 0) {
134 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
135 host->dbgmsg_cmd, cmd->resp[0]);
136 } else {
137 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
138 cmd->error, host->dbgmsg_cmd, host->status);
139 }
140
141 if (!cmd->data)
142 return;
143
144 if (cmd->data->error == 0) {
145 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
146 } else {
147 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
148 cmd->data->error, host->dbgmsg_dat,
149 readl(host->base + S3C2410_SDIDCNT));
150 }
151}
152#else
153static void dbg_dumpcmd(struct s3cmci_host *host,
154 struct mmc_command *cmd, int fail) { }
155
156static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
157 int stop) { }
158
159static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
160
161#endif /* CONFIG_MMC_DEBUG */
162
163static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
164{
165 u32 newmask;
166
167 newmask = readl(host->base + host->sdiimsk);
168 newmask |= imask;
169
170 writel(newmask, host->base + host->sdiimsk);
171
172 return newmask;
173}
174
175static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
176{
177 u32 newmask;
178
179 newmask = readl(host->base + host->sdiimsk);
180 newmask &= ~imask;
181
182 writel(newmask, host->base + host->sdiimsk);
183
184 return newmask;
185}
186
187static inline void clear_imask(struct s3cmci_host *host)
188{
189 writel(0, host->base + host->sdiimsk);
190}
191
192static inline int get_data_buffer(struct s3cmci_host *host,
Christer Weinigel088a78a2008-10-15 00:17:17 +0100193 u32 *bytes, u32 **pointer)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100194{
195 struct scatterlist *sg;
196
197 if (host->pio_active == XFER_NONE)
198 return -EINVAL;
199
200 if ((!host->mrq) || (!host->mrq->data))
201 return -EINVAL;
202
203 if (host->pio_sgptr >= host->mrq->data->sg_len) {
204 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
205 host->pio_sgptr, host->mrq->data->sg_len);
206 return -EBUSY;
207 }
208 sg = &host->mrq->data->sg[host->pio_sgptr];
209
Christer Weinigel088a78a2008-10-15 00:17:17 +0100210 *bytes = sg->length;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100211 *pointer = sg_virt(sg);
212
213 host->pio_sgptr++;
214
215 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
216 host->pio_sgptr, host->mrq->data->sg_len);
217
218 return 0;
219}
220
221static inline u32 fifo_count(struct s3cmci_host *host)
222{
223 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
224
225 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100226 return fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100227}
228
229static inline u32 fifo_free(struct s3cmci_host *host)
230{
231 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
232
233 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100234 return 63 - fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100235}
236
237static void do_pio_read(struct s3cmci_host *host)
238{
239 int res;
240 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100241 u32 *ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100242 u32 fifo_words;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100243 void __iomem *from_ptr;
244
245 /* write real prescaler to host, it might be set slow to fix */
246 writel(host->prescaler, host->base + S3C2410_SDIPRE);
247
248 from_ptr = host->base + host->sdidata;
249
250 while ((fifo = fifo_count(host))) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100251 if (!host->pio_bytes) {
252 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100253 &host->pio_ptr);
254 if (res) {
255 host->pio_active = XFER_NONE;
256 host->complete_what = COMPLETION_FINALIZE;
257
258 dbg(host, dbg_pio, "pio_read(): "
259 "complete (no more data).\n");
260 return;
261 }
262
263 dbg(host, dbg_pio,
264 "pio_read(): new target: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100265 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100266 }
267
268 dbg(host, dbg_pio,
269 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100270 fifo, host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100271 readl(host->base + S3C2410_SDIDCNT));
272
Christer Weinigel088a78a2008-10-15 00:17:17 +0100273 /* If we have reached the end of the block, we can
274 * read a word and get 1 to 3 bytes. If we in the
275 * middle of the block, we have to read full words,
276 * otherwise we will write garbage, so round down to
277 * an even multiple of 4. */
278 if (fifo >= host->pio_bytes)
279 fifo = host->pio_bytes;
280 else
281 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100282
Christer Weinigel088a78a2008-10-15 00:17:17 +0100283 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100284 host->pio_count += fifo;
285
Christer Weinigel088a78a2008-10-15 00:17:17 +0100286 fifo_words = fifo >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100287 ptr = host->pio_ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100288 while (fifo_words--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100289 *ptr++ = readl(from_ptr);
290 host->pio_ptr = ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100291
292 if (fifo & 3) {
293 u32 n = fifo & 3;
294 u32 data = readl(from_ptr);
295 u8 *p = (u8 *)host->pio_ptr;
296
297 while (n--) {
298 *p++ = data;
299 data >>= 8;
300 }
301 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100302 }
303
Christer Weinigel088a78a2008-10-15 00:17:17 +0100304 if (!host->pio_bytes) {
305 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100306 if (res) {
307 dbg(host, dbg_pio,
308 "pio_read(): complete (no more buffers).\n");
309 host->pio_active = XFER_NONE;
310 host->complete_what = COMPLETION_FINALIZE;
311
312 return;
313 }
314 }
315
316 enable_imask(host,
317 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
318}
319
320static void do_pio_write(struct s3cmci_host *host)
321{
322 void __iomem *to_ptr;
323 int res;
324 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100325 u32 *ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100326
327 to_ptr = host->base + host->sdidata;
328
329 while ((fifo = fifo_free(host))) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100330 if (!host->pio_bytes) {
331 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100332 &host->pio_ptr);
333 if (res) {
334 dbg(host, dbg_pio,
335 "pio_write(): complete (no more data).\n");
336 host->pio_active = XFER_NONE;
337
338 return;
339 }
340
341 dbg(host, dbg_pio,
342 "pio_write(): new source: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100343 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100344
345 }
346
Christer Weinigel088a78a2008-10-15 00:17:17 +0100347 /* If we have reached the end of the block, we have to
348 * write exactly the remaining number of bytes. If we
349 * in the middle of the block, we have to write full
350 * words, so round down to an even multiple of 4. */
351 if (fifo >= host->pio_bytes)
352 fifo = host->pio_bytes;
353 else
354 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100355
Christer Weinigel088a78a2008-10-15 00:17:17 +0100356 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100357 host->pio_count += fifo;
358
Christer Weinigel088a78a2008-10-15 00:17:17 +0100359 fifo = (fifo + 3) >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100360 ptr = host->pio_ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100361 while (fifo--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100362 writel(*ptr++, to_ptr);
363 host->pio_ptr = ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100364 }
365
366 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
367}
368
369static void pio_tasklet(unsigned long data)
370{
371 struct s3cmci_host *host = (struct s3cmci_host *) data;
372
373
Roman Moracikd643b5f2008-06-30 22:40:28 +0100374 disable_irq(host->irq);
375
Thomas Kleffelbe518012008-06-30 22:40:24 +0100376 if (host->pio_active == XFER_WRITE)
377 do_pio_write(host);
378
379 if (host->pio_active == XFER_READ)
380 do_pio_read(host);
381
382 if (host->complete_what == COMPLETION_FINALIZE) {
383 clear_imask(host);
384 if (host->pio_active != XFER_NONE) {
385 dbg(host, dbg_err, "unfinished %s "
Christer Weinigel088a78a2008-10-15 00:17:17 +0100386 "- pio_count:[%u] pio_bytes:[%u]\n",
Thomas Kleffelbe518012008-06-30 22:40:24 +0100387 (host->pio_active == XFER_READ) ? "read" : "write",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100388 host->pio_count, host->pio_bytes);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100389
Ben Dooks7c144502008-06-30 22:40:31 +0100390 if (host->mrq->data)
391 host->mrq->data->error = -EINVAL;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100392 }
393
Thomas Kleffelbe518012008-06-30 22:40:24 +0100394 finalize_request(host);
Roman Moracikd643b5f2008-06-30 22:40:28 +0100395 } else
396 enable_irq(host->irq);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100397}
398
399/*
400 * ISR for SDI Interface IRQ
401 * Communication between driver and ISR works as follows:
402 * host->mrq points to current request
403 * host->complete_what Indicates when the request is considered done
404 * COMPLETION_CMDSENT when the command was sent
405 * COMPLETION_RSPFIN when a response was received
406 * COMPLETION_XFERFINISH when the data transfer is finished
407 * COMPLETION_XFERFINISH_RSPFIN both of the above.
408 * host->complete_request is the completion-object the driver waits for
409 *
410 * 1) Driver sets up host->mrq and host->complete_what
411 * 2) Driver prepares the transfer
412 * 3) Driver enables interrupts
413 * 4) Driver starts transfer
414 * 5) Driver waits for host->complete_rquest
415 * 6) ISR checks for request status (errors and success)
416 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
417 * 7) ISR completes host->complete_request
418 * 8) ISR disables interrupts
419 * 9) Driver wakes up and takes care of the request
420 *
421 * Note: "->error"-fields are expected to be set to 0 before the request
422 * was issued by mmc.c - therefore they are only set, when an error
423 * contition comes up
424 */
425
426static irqreturn_t s3cmci_irq(int irq, void *dev_id)
427{
428 struct s3cmci_host *host = dev_id;
429 struct mmc_command *cmd;
430 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
431 u32 mci_cclear, mci_dclear;
432 unsigned long iflags;
433
434 spin_lock_irqsave(&host->complete_lock, iflags);
435
436 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
437 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
438 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
439 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
440 mci_imsk = readl(host->base + host->sdiimsk);
441 mci_cclear = 0;
442 mci_dclear = 0;
443
444 if ((host->complete_what == COMPLETION_NONE) ||
445 (host->complete_what == COMPLETION_FINALIZE)) {
446 host->status = "nothing to complete";
447 clear_imask(host);
448 goto irq_out;
449 }
450
451 if (!host->mrq) {
452 host->status = "no active mrq";
453 clear_imask(host);
454 goto irq_out;
455 }
456
457 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
458
459 if (!cmd) {
460 host->status = "no active cmd";
461 clear_imask(host);
462 goto irq_out;
463 }
464
465 if (!host->dodma) {
466 if ((host->pio_active == XFER_WRITE) &&
467 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
468
469 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
470 tasklet_schedule(&host->pio_tasklet);
471 host->status = "pio tx";
472 }
473
474 if ((host->pio_active == XFER_READ) &&
475 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
476
477 disable_imask(host,
478 S3C2410_SDIIMSK_RXFIFOHALF |
479 S3C2410_SDIIMSK_RXFIFOLAST);
480
481 tasklet_schedule(&host->pio_tasklet);
482 host->status = "pio rx";
483 }
484 }
485
486 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100487 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100488 cmd->error = -ETIMEDOUT;
489 host->status = "error: command timeout";
490 goto fail_transfer;
491 }
492
493 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
494 if (host->complete_what == COMPLETION_CMDSENT) {
495 host->status = "ok: command sent";
496 goto close_transfer;
497 }
498
499 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
500 }
501
502 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
503 if (cmd->flags & MMC_RSP_CRC) {
Harald Welte679f0f82008-06-30 22:40:25 +0100504 if (host->mrq->cmd->flags & MMC_RSP_136) {
505 dbg(host, dbg_irq,
506 "fixup: ignore CRC fail with long rsp\n");
507 } else {
508 /* note, we used to fail the transfer
509 * here, but it seems that this is just
510 * the hardware getting it wrong.
511 *
512 * cmd->error = -EILSEQ;
513 * host->status = "error: bad command crc";
514 * goto fail_transfer;
515 */
516 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100517 }
518
519 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
520 }
521
522 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
523 if (host->complete_what == COMPLETION_RSPFIN) {
524 host->status = "ok: command response received";
525 goto close_transfer;
526 }
527
528 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
529 host->complete_what = COMPLETION_XFERFINISH;
530
531 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
532 }
533
534 /* errors handled after this point are only relevant
535 when a data transfer is in progress */
536
537 if (!cmd->data)
538 goto clear_status_bits;
539
540 /* Check for FIFO failure */
541 if (host->is2440) {
542 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100543 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100544 host->mrq->data->error = -EILSEQ;
545 host->status = "error: 2440 fifo failure";
546 goto fail_transfer;
547 }
548 } else {
549 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100550 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100551 cmd->data->error = -EILSEQ;
552 host->status = "error: fifo failure";
553 goto fail_transfer;
554 }
555 }
556
557 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100558 dbg(host, dbg_err, "bad data crc (outgoing)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100559 cmd->data->error = -EILSEQ;
560 host->status = "error: bad data crc (outgoing)";
561 goto fail_transfer;
562 }
563
564 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100565 dbg(host, dbg_err, "bad data crc (incoming)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100566 cmd->data->error = -EILSEQ;
567 host->status = "error: bad data crc (incoming)";
568 goto fail_transfer;
569 }
570
571 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100572 dbg(host, dbg_err, "data timeout\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100573 cmd->data->error = -ETIMEDOUT;
574 host->status = "error: data timeout";
575 goto fail_transfer;
576 }
577
578 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
579 if (host->complete_what == COMPLETION_XFERFINISH) {
580 host->status = "ok: data transfer completed";
581 goto close_transfer;
582 }
583
584 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
585 host->complete_what = COMPLETION_RSPFIN;
586
587 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
588 }
589
590clear_status_bits:
591 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
592 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
593
594 goto irq_out;
595
596fail_transfer:
597 host->pio_active = XFER_NONE;
598
599close_transfer:
600 host->complete_what = COMPLETION_FINALIZE;
601
602 clear_imask(host);
603 tasklet_schedule(&host->pio_tasklet);
604
605 goto irq_out;
606
607irq_out:
608 dbg(host, dbg_irq,
609 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
610 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
611
612 spin_unlock_irqrestore(&host->complete_lock, iflags);
613 return IRQ_HANDLED;
614
615}
616
617/*
618 * ISR for the CardDetect Pin
619*/
620
621static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
622{
623 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
624
625 dbg(host, dbg_irq, "card detect\n");
626
Ben Dooks2de5f792008-06-30 22:40:35 +0100627 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
Thomas Kleffelbe518012008-06-30 22:40:24 +0100628
629 return IRQ_HANDLED;
630}
631
Ben Dooks5d304402008-08-08 10:55:41 +0100632static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
633 void *buf_id, int size,
634 enum s3c2410_dma_buffresult result)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100635{
636 struct s3cmci_host *host = buf_id;
637 unsigned long iflags;
638 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
639
640 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
641 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
642 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
643 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
644
645 BUG_ON(!host->mrq);
646 BUG_ON(!host->mrq->data);
647 BUG_ON(!host->dmatogo);
648
649 spin_lock_irqsave(&host->complete_lock, iflags);
650
651 if (result != S3C2410_RES_OK) {
652 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
653 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
654 mci_csta, mci_dsta, mci_fsta,
655 mci_dcnt, result, host->dmatogo);
656
657 goto fail_request;
658 }
659
660 host->dmatogo--;
661 if (host->dmatogo) {
662 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
663 "DCNT:[%08x] toGo:%u\n",
664 size, mci_dsta, mci_dcnt, host->dmatogo);
665
666 goto out;
667 }
668
669 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
670 size, mci_dsta, mci_dcnt);
671
672 host->complete_what = COMPLETION_FINALIZE;
673
674out:
675 tasklet_schedule(&host->pio_tasklet);
676 spin_unlock_irqrestore(&host->complete_lock, iflags);
677 return;
678
Thomas Kleffelbe518012008-06-30 22:40:24 +0100679fail_request:
680 host->mrq->data->error = -EINVAL;
681 host->complete_what = COMPLETION_FINALIZE;
682 writel(0, host->base + host->sdiimsk);
683 goto out;
684
685}
686
687static void finalize_request(struct s3cmci_host *host)
688{
689 struct mmc_request *mrq = host->mrq;
690 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
691 int debug_as_failure = 0;
692
693 if (host->complete_what != COMPLETION_FINALIZE)
694 return;
695
696 if (!mrq)
697 return;
698
699 if (cmd->data && (cmd->error == 0) &&
700 (cmd->data->error == 0)) {
701 if (host->dodma && (!host->dma_complete)) {
702 dbg(host, dbg_dma, "DMA Missing!\n");
703 return;
704 }
705 }
706
707 /* Read response from controller. */
708 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
709 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
710 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
711 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
712
713 writel(host->prescaler, host->base + S3C2410_SDIPRE);
714
715 if (cmd->error)
716 debug_as_failure = 1;
717
718 if (cmd->data && cmd->data->error)
719 debug_as_failure = 1;
720
721 dbg_dumpcmd(host, cmd, debug_as_failure);
722
723 /* Cleanup controller */
724 writel(0, host->base + S3C2410_SDICMDARG);
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100725 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100726 writel(0, host->base + S3C2410_SDICMDCON);
727 writel(0, host->base + host->sdiimsk);
728
729 if (cmd->data && cmd->error)
730 cmd->data->error = cmd->error;
731
732 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
733 host->cmd_is_stop = 1;
734 s3cmci_send_request(host->mmc);
735 return;
736 }
737
738 /* If we have no data transfer we are finished here */
739 if (!mrq->data)
740 goto request_done;
741
742 /* Calulate the amout of bytes transfer if there was no error */
743 if (mrq->data->error == 0) {
744 mrq->data->bytes_xfered =
745 (mrq->data->blocks * mrq->data->blksz);
746 } else {
747 mrq->data->bytes_xfered = 0;
748 }
749
750 /* If we had an error while transfering data we flush the
751 * DMA channel and the fifo to clear out any garbage. */
752 if (mrq->data->error != 0) {
753 if (host->dodma)
754 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
755
756 if (host->is2440) {
757 /* Clear failure register and reset fifo. */
758 writel(S3C2440_SDIFSTA_FIFORESET |
759 S3C2440_SDIFSTA_FIFOFAIL,
760 host->base + S3C2410_SDIFSTA);
761 } else {
762 u32 mci_con;
763
764 /* reset fifo */
765 mci_con = readl(host->base + S3C2410_SDICON);
766 mci_con |= S3C2410_SDICON_FIFORESET;
767
768 writel(mci_con, host->base + S3C2410_SDICON);
769 }
770 }
771
772request_done:
773 host->complete_what = COMPLETION_NONE;
774 host->mrq = NULL;
775 mmc_request_done(host->mmc, mrq);
776}
777
Ben Dooks5d304402008-08-08 10:55:41 +0100778static void s3cmci_dma_setup(struct s3cmci_host *host,
779 enum s3c2410_dmasrc source)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100780{
781 static enum s3c2410_dmasrc last_source = -1;
782 static int setup_ok;
783
784 if (last_source == source)
785 return;
786
787 last_source = source;
788
789 s3c2410_dma_devconfig(host->dma, source, 3,
790 host->mem->start + host->sdidata);
791
792 if (!setup_ok) {
793 s3c2410_dma_config(host->dma, 4,
794 (S3C2410_DCON_HWTRIG | S3C2410_DCON_CH0_SDI));
795 s3c2410_dma_set_buffdone_fn(host->dma,
796 s3cmci_dma_done_callback);
797 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
798 setup_ok = 1;
799 }
800}
801
802static void s3cmci_send_command(struct s3cmci_host *host,
803 struct mmc_command *cmd)
804{
805 u32 ccon, imsk;
806
807 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
808 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
809 S3C2410_SDIIMSK_RESPONSECRC;
810
811 enable_imask(host, imsk);
812
813 if (cmd->data)
814 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
815 else if (cmd->flags & MMC_RSP_PRESENT)
816 host->complete_what = COMPLETION_RSPFIN;
817 else
818 host->complete_what = COMPLETION_CMDSENT;
819
820 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
821
822 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
823 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
824
825 if (cmd->flags & MMC_RSP_PRESENT)
826 ccon |= S3C2410_SDICMDCON_WAITRSP;
827
828 if (cmd->flags & MMC_RSP_136)
829 ccon |= S3C2410_SDICMDCON_LONGRSP;
830
831 writel(ccon, host->base + S3C2410_SDICMDCON);
832}
833
834static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
835{
836 u32 dcon, imsk, stoptries = 3;
837
838 /* write DCON register */
839
840 if (!data) {
841 writel(0, host->base + S3C2410_SDIDCON);
842 return 0;
843 }
844
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100845 if ((data->blksz & 3) != 0) {
846 /* We cannot deal with unaligned blocks with more than
847 * one block being transfered. */
848
Christer Weinigel088a78a2008-10-15 00:17:17 +0100849 if (data->blocks > 1) {
850 pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100851 return -EINVAL;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100852 }
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100853 }
854
Thomas Kleffelbe518012008-06-30 22:40:24 +0100855 while (readl(host->base + S3C2410_SDIDSTA) &
856 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
857
858 dbg(host, dbg_err,
859 "mci_setup_data() transfer stillin progress.\n");
860
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100861 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100862 s3cmci_reset(host);
863
864 if ((stoptries--) == 0) {
865 dbg_dumpregs(host, "DRF");
866 return -EINVAL;
867 }
868 }
869
870 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
871
872 if (host->dodma)
873 dcon |= S3C2410_SDIDCON_DMAEN;
874
875 if (host->bus_width == MMC_BUS_WIDTH_4)
876 dcon |= S3C2410_SDIDCON_WIDEBUS;
877
878 if (!(data->flags & MMC_DATA_STREAM))
879 dcon |= S3C2410_SDIDCON_BLOCKMODE;
880
881 if (data->flags & MMC_DATA_WRITE) {
882 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
883 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
884 }
885
886 if (data->flags & MMC_DATA_READ) {
887 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
888 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
889 }
890
891 if (host->is2440) {
892 dcon |= S3C2440_SDIDCON_DS_WORD;
893 dcon |= S3C2440_SDIDCON_DATSTART;
894 }
895
896 writel(dcon, host->base + S3C2410_SDIDCON);
897
898 /* write BSIZE register */
899
900 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
901
902 /* add to IMASK register */
903 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
904 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
905
906 enable_imask(host, imsk);
907
908 /* write TIMER register */
909
910 if (host->is2440) {
911 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
912 } else {
913 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
914
915 /* FIX: set slow clock to prevent timeouts on read */
916 if (data->flags & MMC_DATA_READ)
917 writel(0xFF, host->base + S3C2410_SDIPRE);
918 }
919
920 return 0;
921}
922
923#define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
924
925static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
926{
927 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
928
929 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
930
931 host->pio_sgptr = 0;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100932 host->pio_bytes = 0;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100933 host->pio_count = 0;
934 host->pio_active = rw ? XFER_WRITE : XFER_READ;
935
936 if (rw) {
937 do_pio_write(host);
938 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
939 } else {
940 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
941 | S3C2410_SDIIMSK_RXFIFOLAST);
942 }
943
944 return 0;
945}
946
947static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
948{
949 int dma_len, i;
950 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
951
952 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
953
954 s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
955 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
956
957 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
958 (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
959
960 if (dma_len == 0)
961 return -ENOMEM;
962
963 host->dma_complete = 0;
964 host->dmatogo = dma_len;
965
966 for (i = 0; i < dma_len; i++) {
967 int res;
968
969 dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
970 sg_dma_address(&data->sg[i]),
971 sg_dma_len(&data->sg[i]));
972
973 res = s3c2410_dma_enqueue(host->dma, (void *) host,
974 sg_dma_address(&data->sg[i]),
975 sg_dma_len(&data->sg[i]));
976
977 if (res) {
978 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
979 return -EBUSY;
980 }
981 }
982
983 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
984
985 return 0;
986}
987
988static void s3cmci_send_request(struct mmc_host *mmc)
989{
990 struct s3cmci_host *host = mmc_priv(mmc);
991 struct mmc_request *mrq = host->mrq;
992 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
993
994 host->ccnt++;
995 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
996
997 /* Clear command, data and fifo status registers
998 Fifo clear only necessary on 2440, but doesn't hurt on 2410
999 */
1000 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1001 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1002 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1003
1004 if (cmd->data) {
1005 int res = s3cmci_setup_data(host, cmd->data);
1006
1007 host->dcnt++;
1008
1009 if (res) {
Ben Dooksff8c8042008-06-30 22:40:37 +01001010 dbg(host, dbg_err, "setup data error %d\n", res);
1011 cmd->error = res;
1012 cmd->data->error = res;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001013
1014 mmc_request_done(mmc, mrq);
1015 return;
1016 }
1017
1018 if (host->dodma)
1019 res = s3cmci_prepare_dma(host, cmd->data);
1020 else
1021 res = s3cmci_prepare_pio(host, cmd->data);
1022
1023 if (res) {
Ben Dooksff8c8042008-06-30 22:40:37 +01001024 dbg(host, dbg_err, "data prepare error %d\n", res);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001025 cmd->error = res;
1026 cmd->data->error = res;
1027
1028 mmc_request_done(mmc, mrq);
1029 return;
1030 }
1031 }
1032
1033 /* Send command */
1034 s3cmci_send_command(host, cmd);
1035
1036 /* Enable Interrupt */
1037 enable_irq(host->irq);
1038}
1039
Ben Dooks87dd9802008-08-12 09:24:50 +01001040static int s3cmci_card_present(struct mmc_host *mmc)
Ben Dooks50a84572008-06-30 22:40:36 +01001041{
Ben Dooks87dd9802008-08-12 09:24:50 +01001042 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dooks50a84572008-06-30 22:40:36 +01001043 struct s3c24xx_mci_pdata *pdata = host->pdata;
1044 int ret;
1045
1046 if (pdata->gpio_detect == 0)
1047 return -ENOSYS;
1048
1049 ret = s3c2410_gpio_getpin(pdata->gpio_detect) ? 0 : 1;
1050 return ret ^ pdata->detect_invert;
1051}
1052
Thomas Kleffelbe518012008-06-30 22:40:24 +01001053static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1054{
1055 struct s3cmci_host *host = mmc_priv(mmc);
1056
1057 host->status = "mmc request";
1058 host->cmd_is_stop = 0;
1059 host->mrq = mrq;
1060
Ben Dooks87dd9802008-08-12 09:24:50 +01001061 if (s3cmci_card_present(mmc) == 0) {
Ben Dooks50a84572008-06-30 22:40:36 +01001062 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1063 host->mrq->cmd->error = -ENOMEDIUM;
1064 mmc_request_done(mmc, mrq);
1065 } else
1066 s3cmci_send_request(mmc);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001067}
1068
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001069static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1070{
1071 u32 mci_psc;
1072
1073 /* Set clock */
1074 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1075 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1076
1077 if (host->real_rate <= ios->clock)
1078 break;
1079 }
1080
1081 if (mci_psc > 255)
1082 mci_psc = 255;
1083
1084 host->prescaler = mci_psc;
1085 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1086
1087 /* If requested clock is 0, real_rate will be 0, too */
1088 if (ios->clock == 0)
1089 host->real_rate = 0;
1090}
1091
Thomas Kleffelbe518012008-06-30 22:40:24 +01001092static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1093{
1094 struct s3cmci_host *host = mmc_priv(mmc);
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001095 u32 mci_con;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001096
1097 /* Set the power state */
1098
1099 mci_con = readl(host->base + S3C2410_SDICON);
1100
1101 switch (ios->power_mode) {
1102 case MMC_POWER_ON:
1103 case MMC_POWER_UP:
1104 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
1105 s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
1106 s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
1107 s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
1108 s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
1109 s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);
1110
Ben Dooksedb5a982008-06-30 22:40:29 +01001111 if (host->pdata->set_power)
1112 host->pdata->set_power(ios->power_mode, ios->vdd);
1113
Thomas Kleffelbe518012008-06-30 22:40:24 +01001114 if (!host->is2440)
1115 mci_con |= S3C2410_SDICON_FIFORESET;
1116
1117 break;
1118
1119 case MMC_POWER_OFF:
1120 default:
1121 s3c2410_gpio_setpin(S3C2410_GPE5, 0);
1122 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_OUTP);
1123
1124 if (host->is2440)
1125 mci_con |= S3C2440_SDICON_SDRESET;
1126
Ben Dooksedb5a982008-06-30 22:40:29 +01001127 if (host->pdata->set_power)
1128 host->pdata->set_power(ios->power_mode, ios->vdd);
1129
Thomas Kleffelbe518012008-06-30 22:40:24 +01001130 break;
1131 }
1132
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001133 s3cmci_set_clk(host, ios);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001134
1135 /* Set CLOCK_ENABLE */
1136 if (ios->clock)
1137 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1138 else
1139 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1140
1141 writel(mci_con, host->base + S3C2410_SDICON);
1142
1143 if ((ios->power_mode == MMC_POWER_ON) ||
1144 (ios->power_mode == MMC_POWER_UP)) {
1145 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1146 host->real_rate/1000, ios->clock/1000);
1147 } else {
1148 dbg(host, dbg_conf, "powered down.\n");
1149 }
1150
1151 host->bus_width = ios->bus_width;
1152}
1153
1154static void s3cmci_reset(struct s3cmci_host *host)
1155{
1156 u32 con = readl(host->base + S3C2410_SDICON);
1157
1158 con |= S3C2440_SDICON_SDRESET;
1159 writel(con, host->base + S3C2410_SDICON);
1160}
1161
Ben Dooksedb5a982008-06-30 22:40:29 +01001162static int s3cmci_get_ro(struct mmc_host *mmc)
1163{
1164 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dookscf0984c2008-06-30 22:40:30 +01001165 struct s3c24xx_mci_pdata *pdata = host->pdata;
1166 int ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001167
Ben Dookscf0984c2008-06-30 22:40:30 +01001168 if (pdata->gpio_wprotect == 0)
Ben Dooksedb5a982008-06-30 22:40:29 +01001169 return 0;
1170
Ben Dookscf0984c2008-06-30 22:40:30 +01001171 ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
1172
1173 if (pdata->wprotect_invert)
1174 ret = !ret;
1175
1176 return ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001177}
1178
Thomas Kleffelbe518012008-06-30 22:40:24 +01001179static struct mmc_host_ops s3cmci_ops = {
1180 .request = s3cmci_request,
1181 .set_ios = s3cmci_set_ios,
Ben Dooksedb5a982008-06-30 22:40:29 +01001182 .get_ro = s3cmci_get_ro,
Ben Dooks87dd9802008-08-12 09:24:50 +01001183 .get_cd = s3cmci_card_present,
Ben Dooksedb5a982008-06-30 22:40:29 +01001184};
1185
1186static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1187 /* This is currently here to avoid a number of if (host->pdata)
1188 * checks. Any zero fields to ensure reaonable defaults are picked. */
Thomas Kleffelbe518012008-06-30 22:40:24 +01001189};
1190
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001191#ifdef CONFIG_CPU_FREQ
1192
1193static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1194 unsigned long val, void *data)
1195{
1196 struct s3cmci_host *host;
1197 struct mmc_host *mmc;
1198 unsigned long newclk;
1199 unsigned long flags;
1200
1201 host = container_of(nb, struct s3cmci_host, freq_transition);
1202 newclk = clk_get_rate(host->clk);
1203 mmc = host->mmc;
1204
1205 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1206 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1207 spin_lock_irqsave(&mmc->lock, flags);
1208
1209 host->clk_rate = newclk;
1210
1211 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1212 mmc->ios.clock != 0)
1213 s3cmci_set_clk(host, &mmc->ios);
1214
1215 spin_unlock_irqrestore(&mmc->lock, flags);
1216 }
1217
1218 return 0;
1219}
1220
1221static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1222{
1223 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1224
1225 return cpufreq_register_notifier(&host->freq_transition,
1226 CPUFREQ_TRANSITION_NOTIFIER);
1227}
1228
1229static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1230{
1231 cpufreq_unregister_notifier(&host->freq_transition,
1232 CPUFREQ_TRANSITION_NOTIFIER);
1233}
1234
1235#else
1236static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1237{
1238 return 0;
1239}
1240
1241static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1242{
1243}
1244#endif
1245
Thomas Kleffelbe518012008-06-30 22:40:24 +01001246static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
1247{
1248 struct s3cmci_host *host;
1249 struct mmc_host *mmc;
1250 int ret;
1251
1252 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1253 if (!mmc) {
1254 ret = -ENOMEM;
1255 goto probe_out;
1256 }
1257
1258 host = mmc_priv(mmc);
1259 host->mmc = mmc;
1260 host->pdev = pdev;
1261 host->is2440 = is2440;
1262
Ben Dooksedb5a982008-06-30 22:40:29 +01001263 host->pdata = pdev->dev.platform_data;
1264 if (!host->pdata) {
1265 pdev->dev.platform_data = &s3cmci_def_pdata;
1266 host->pdata = &s3cmci_def_pdata;
1267 }
1268
Thomas Kleffelbe518012008-06-30 22:40:24 +01001269 spin_lock_init(&host->complete_lock);
1270 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1271
1272 if (is2440) {
1273 host->sdiimsk = S3C2440_SDIIMSK;
1274 host->sdidata = S3C2440_SDIDATA;
1275 host->clk_div = 1;
1276 } else {
1277 host->sdiimsk = S3C2410_SDIIMSK;
1278 host->sdidata = S3C2410_SDIDATA;
1279 host->clk_div = 2;
1280 }
1281
1282 host->dodma = 0;
1283 host->complete_what = COMPLETION_NONE;
1284 host->pio_active = XFER_NONE;
1285
1286 host->dma = S3CMCI_DMA;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001287
1288 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1289 if (!host->mem) {
1290 dev_err(&pdev->dev,
1291 "failed to get io memory region resouce.\n");
1292
1293 ret = -ENOENT;
1294 goto probe_free_host;
1295 }
1296
1297 host->mem = request_mem_region(host->mem->start,
1298 RESSIZE(host->mem), pdev->name);
1299
1300 if (!host->mem) {
1301 dev_err(&pdev->dev, "failed to request io memory region.\n");
1302 ret = -ENOENT;
1303 goto probe_free_host;
1304 }
1305
1306 host->base = ioremap(host->mem->start, RESSIZE(host->mem));
Ben Dooks5d304402008-08-08 10:55:41 +01001307 if (!host->base) {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001308 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1309 ret = -EINVAL;
1310 goto probe_free_mem_region;
1311 }
1312
1313 host->irq = platform_get_irq(pdev, 0);
1314 if (host->irq == 0) {
1315 dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
1316 ret = -EINVAL;
1317 goto probe_iounmap;
1318 }
1319
1320 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1321 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1322 ret = -ENOENT;
1323 goto probe_iounmap;
1324 }
1325
1326 /* We get spurious interrupts even when we have set the IMSK
1327 * register to ignore everything, so use disable_irq() to make
1328 * ensure we don't lock the system with un-serviceable requests. */
1329
1330 disable_irq(host->irq);
1331
Ben Dooks55d70f52008-06-30 22:40:32 +01001332 host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001333
Ben Dooks55d70f52008-06-30 22:40:32 +01001334 if (host->irq_cd >= 0) {
1335 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1336 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1337 DRIVER_NAME, host)) {
1338 dev_err(&pdev->dev, "can't get card detect irq.\n");
1339 ret = -ENOENT;
1340 goto probe_free_irq;
1341 }
1342 } else {
1343 dev_warn(&pdev->dev, "host detect has no irq available\n");
1344 s3c2410_gpio_cfgpin(host->pdata->gpio_detect,
1345 S3C2410_GPIO_INPUT);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001346 }
1347
Ben Dooksedb5a982008-06-30 22:40:29 +01001348 if (host->pdata->gpio_wprotect)
1349 s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
1350 S3C2410_GPIO_INPUT);
1351
Ben Dooks3886ff52008-06-30 22:40:33 +01001352 if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001353 dev_err(&pdev->dev, "unable to get DMA channel.\n");
1354 ret = -EBUSY;
1355 goto probe_free_irq_cd;
1356 }
1357
1358 host->clk = clk_get(&pdev->dev, "sdi");
1359 if (IS_ERR(host->clk)) {
1360 dev_err(&pdev->dev, "failed to find clock source.\n");
1361 ret = PTR_ERR(host->clk);
1362 host->clk = NULL;
1363 goto probe_free_host;
1364 }
1365
1366 ret = clk_enable(host->clk);
1367 if (ret) {
1368 dev_err(&pdev->dev, "failed to enable clock source.\n");
1369 goto clk_free;
1370 }
1371
1372 host->clk_rate = clk_get_rate(host->clk);
1373
1374 mmc->ops = &s3cmci_ops;
Ben Dooksedb5a982008-06-30 22:40:29 +01001375 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001376 mmc->caps = MMC_CAP_4_BIT_DATA;
1377 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1378 mmc->f_max = host->clk_rate / host->clk_div;
1379
Ben Dooksedb5a982008-06-30 22:40:29 +01001380 if (host->pdata->ocr_avail)
1381 mmc->ocr_avail = host->pdata->ocr_avail;
1382
Thomas Kleffelbe518012008-06-30 22:40:24 +01001383 mmc->max_blk_count = 4095;
1384 mmc->max_blk_size = 4095;
1385 mmc->max_req_size = 4095 * 512;
1386 mmc->max_seg_size = mmc->max_req_size;
1387
1388 mmc->max_phys_segs = 128;
1389 mmc->max_hw_segs = 128;
1390
1391 dbg(host, dbg_debug,
1392 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1393 (host->is2440?"2440":""),
1394 host->base, host->irq, host->irq_cd, host->dma);
1395
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001396 ret = s3cmci_cpufreq_register(host);
1397 if (ret) {
1398 dev_err(&pdev->dev, "failed to register cpufreq\n");
1399 goto free_dmabuf;
1400 }
1401
Thomas Kleffelbe518012008-06-30 22:40:24 +01001402 ret = mmc_add_host(mmc);
1403 if (ret) {
1404 dev_err(&pdev->dev, "failed to add mmc host.\n");
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001405 goto free_cpufreq;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001406 }
1407
1408 platform_set_drvdata(pdev, mmc);
1409 dev_info(&pdev->dev, "initialisation done.\n");
1410
1411 return 0;
1412
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001413 free_cpufreq:
1414 s3cmci_cpufreq_deregister(host);
1415
Thomas Kleffelbe518012008-06-30 22:40:24 +01001416 free_dmabuf:
1417 clk_disable(host->clk);
1418
1419 clk_free:
1420 clk_put(host->clk);
1421
1422 probe_free_irq_cd:
Ben Dooks55d70f52008-06-30 22:40:32 +01001423 if (host->irq_cd >= 0)
1424 free_irq(host->irq_cd, host);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001425
1426 probe_free_irq:
1427 free_irq(host->irq, host);
1428
1429 probe_iounmap:
1430 iounmap(host->base);
1431
1432 probe_free_mem_region:
1433 release_mem_region(host->mem->start, RESSIZE(host->mem));
1434
1435 probe_free_host:
1436 mmc_free_host(mmc);
1437 probe_out:
1438 return ret;
1439}
1440
Ben Dooks907b2cd2008-07-17 15:32:54 +01001441static void s3cmci_shutdown(struct platform_device *pdev)
1442{
1443 struct mmc_host *mmc = platform_get_drvdata(pdev);
1444 struct s3cmci_host *host = mmc_priv(mmc);
1445
1446 if (host->irq_cd >= 0)
1447 free_irq(host->irq_cd, host);
1448
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001449 s3cmci_cpufreq_deregister(host);
Ben Dooks907b2cd2008-07-17 15:32:54 +01001450 mmc_remove_host(mmc);
1451 clk_disable(host->clk);
1452}
1453
Thomas Kleffelbe518012008-06-30 22:40:24 +01001454static int __devexit s3cmci_remove(struct platform_device *pdev)
1455{
1456 struct mmc_host *mmc = platform_get_drvdata(pdev);
1457 struct s3cmci_host *host = mmc_priv(mmc);
1458
Ben Dooks907b2cd2008-07-17 15:32:54 +01001459 s3cmci_shutdown(pdev);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001460
Thomas Kleffelbe518012008-06-30 22:40:24 +01001461 clk_put(host->clk);
1462
1463 tasklet_disable(&host->pio_tasklet);
Harald Welteceb3ac22008-06-30 22:40:26 +01001464 s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001465
Thomas Kleffelbe518012008-06-30 22:40:24 +01001466 free_irq(host->irq, host);
1467
1468 iounmap(host->base);
1469 release_mem_region(host->mem->start, RESSIZE(host->mem));
1470
1471 mmc_free_host(mmc);
1472 return 0;
1473}
1474
Ben Dooksd2f27612008-07-17 11:54:01 +01001475static int __devinit s3cmci_2410_probe(struct platform_device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001476{
1477 return s3cmci_probe(dev, 0);
1478}
1479
Ben Dooksd2f27612008-07-17 11:54:01 +01001480static int __devinit s3cmci_2412_probe(struct platform_device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001481{
1482 return s3cmci_probe(dev, 1);
1483}
1484
Ben Dooksd2f27612008-07-17 11:54:01 +01001485static int __devinit s3cmci_2440_probe(struct platform_device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001486{
1487 return s3cmci_probe(dev, 1);
1488}
1489
1490#ifdef CONFIG_PM
1491
1492static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
1493{
1494 struct mmc_host *mmc = platform_get_drvdata(dev);
1495
1496 return mmc_suspend_host(mmc, state);
1497}
1498
1499static int s3cmci_resume(struct platform_device *dev)
1500{
1501 struct mmc_host *mmc = platform_get_drvdata(dev);
1502
1503 return mmc_resume_host(mmc);
1504}
1505
1506#else /* CONFIG_PM */
1507#define s3cmci_suspend NULL
1508#define s3cmci_resume NULL
1509#endif /* CONFIG_PM */
1510
1511
Ben Dooksd2f27612008-07-17 11:54:01 +01001512static struct platform_driver s3cmci_2410_driver = {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001513 .driver.name = "s3c2410-sdi",
1514 .driver.owner = THIS_MODULE,
Ben Dooksd2f27612008-07-17 11:54:01 +01001515 .probe = s3cmci_2410_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001516 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001517 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001518 .suspend = s3cmci_suspend,
1519 .resume = s3cmci_resume,
1520};
1521
Ben Dooksd2f27612008-07-17 11:54:01 +01001522static struct platform_driver s3cmci_2412_driver = {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001523 .driver.name = "s3c2412-sdi",
1524 .driver.owner = THIS_MODULE,
Ben Dooksd2f27612008-07-17 11:54:01 +01001525 .probe = s3cmci_2412_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001526 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001527 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001528 .suspend = s3cmci_suspend,
1529 .resume = s3cmci_resume,
1530};
1531
Ben Dooksd2f27612008-07-17 11:54:01 +01001532static struct platform_driver s3cmci_2440_driver = {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001533 .driver.name = "s3c2440-sdi",
1534 .driver.owner = THIS_MODULE,
Ben Dooksd2f27612008-07-17 11:54:01 +01001535 .probe = s3cmci_2440_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001536 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001537 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001538 .suspend = s3cmci_suspend,
1539 .resume = s3cmci_resume,
1540};
1541
1542
1543static int __init s3cmci_init(void)
1544{
Ben Dooksd2f27612008-07-17 11:54:01 +01001545 platform_driver_register(&s3cmci_2410_driver);
1546 platform_driver_register(&s3cmci_2412_driver);
1547 platform_driver_register(&s3cmci_2440_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001548 return 0;
1549}
1550
1551static void __exit s3cmci_exit(void)
1552{
Ben Dooksd2f27612008-07-17 11:54:01 +01001553 platform_driver_unregister(&s3cmci_2410_driver);
1554 platform_driver_unregister(&s3cmci_2412_driver);
1555 platform_driver_unregister(&s3cmci_2440_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001556}
1557
1558module_init(s3cmci_init);
1559module_exit(s3cmci_exit);
1560
1561MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1562MODULE_LICENSE("GPL v2");
1563MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>");
Ben Dooks318f9052008-06-30 22:40:34 +01001564MODULE_ALIAS("platform:s3c2410-sdi");
1565MODULE_ALIAS("platform:s3c2412-sdi");
1566MODULE_ALIAS("platform:s3c2440-sdi");