blob: 84e088c44540f0694295a9abca41c1d0d154b13e [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 Dooksec976d62009-05-13 22:52:24 +010020#include <linux/gpio.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010021#include <linux/irq.h>
22#include <linux/io.h>
23
Ramax Lo7ad14f82009-01-14 02:13:47 +010024#include <mach/dma.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010025
Russell Kinga09e64f2008-08-05 16:14:15 +010026#include <mach/regs-sdi.h>
27#include <mach/regs-gpio.h>
Thomas Kleffelbe518012008-06-30 22:40:24 +010028
Ben Dookse3bd9ec2008-10-30 10:14:39 +000029#include <plat/mci.h>
Ben Dooksedb5a982008-06-30 22:40:29 +010030
Thomas Kleffelbe518012008-06-30 22:40:24 +010031#include "s3cmci.h"
32
33#define DRIVER_NAME "s3c-mci"
34
35enum dbg_channels {
36 dbg_err = (1 << 0),
37 dbg_debug = (1 << 1),
38 dbg_info = (1 << 2),
39 dbg_irq = (1 << 3),
40 dbg_sg = (1 << 4),
41 dbg_dma = (1 << 5),
42 dbg_pio = (1 << 6),
43 dbg_fail = (1 << 7),
44 dbg_conf = (1 << 8),
45};
46
ben@fluff.org.uk9c2e7e42008-10-15 00:17:15 +010047static const int dbgmap_err = dbg_fail;
Thomas Kleffelbe518012008-06-30 22:40:24 +010048static const int dbgmap_info = dbg_info | dbg_conf;
ben@fluff.org.uk9c2e7e42008-10-15 00:17:15 +010049static const int dbgmap_debug = dbg_err | dbg_debug;
Thomas Kleffelbe518012008-06-30 22:40:24 +010050
51#define dbg(host, channels, args...) \
52 do { \
53 if (dbgmap_err & channels) \
54 dev_err(&host->pdev->dev, args); \
55 else if (dbgmap_info & channels) \
56 dev_info(&host->pdev->dev, args); \
57 else if (dbgmap_debug & channels) \
58 dev_dbg(&host->pdev->dev, args); \
59 } while (0)
60
Thomas Kleffelbe518012008-06-30 22:40:24 +010061static struct s3c2410_dma_client s3cmci_dma_client = {
62 .name = "s3c-mci",
63};
64
65static void finalize_request(struct s3cmci_host *host);
66static void s3cmci_send_request(struct mmc_host *mmc);
67static void s3cmci_reset(struct s3cmci_host *host);
68
69#ifdef CONFIG_MMC_DEBUG
70
71static void dbg_dumpregs(struct s3cmci_host *host, char *prefix)
72{
73 u32 con, pre, cmdarg, cmdcon, cmdsta, r0, r1, r2, r3, timer, bsize;
74 u32 datcon, datcnt, datsta, fsta, imask;
75
76 con = readl(host->base + S3C2410_SDICON);
77 pre = readl(host->base + S3C2410_SDIPRE);
78 cmdarg = readl(host->base + S3C2410_SDICMDARG);
79 cmdcon = readl(host->base + S3C2410_SDICMDCON);
80 cmdsta = readl(host->base + S3C2410_SDICMDSTAT);
81 r0 = readl(host->base + S3C2410_SDIRSP0);
82 r1 = readl(host->base + S3C2410_SDIRSP1);
83 r2 = readl(host->base + S3C2410_SDIRSP2);
84 r3 = readl(host->base + S3C2410_SDIRSP3);
85 timer = readl(host->base + S3C2410_SDITIMER);
86 bsize = readl(host->base + S3C2410_SDIBSIZE);
87 datcon = readl(host->base + S3C2410_SDIDCON);
88 datcnt = readl(host->base + S3C2410_SDIDCNT);
89 datsta = readl(host->base + S3C2410_SDIDSTA);
90 fsta = readl(host->base + S3C2410_SDIFSTA);
91 imask = readl(host->base + host->sdiimsk);
92
93 dbg(host, dbg_debug, "%s CON:[%08x] PRE:[%08x] TMR:[%08x]\n",
94 prefix, con, pre, timer);
95
96 dbg(host, dbg_debug, "%s CCON:[%08x] CARG:[%08x] CSTA:[%08x]\n",
97 prefix, cmdcon, cmdarg, cmdsta);
98
99 dbg(host, dbg_debug, "%s DCON:[%08x] FSTA:[%08x]"
100 " DSTA:[%08x] DCNT:[%08x]\n",
101 prefix, datcon, fsta, datsta, datcnt);
102
103 dbg(host, dbg_debug, "%s R0:[%08x] R1:[%08x]"
104 " R2:[%08x] R3:[%08x]\n",
105 prefix, r0, r1, r2, r3);
106}
107
108static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
109 int stop)
110{
111 snprintf(host->dbgmsg_cmd, 300,
112 "#%u%s op:%i arg:0x%08x flags:0x08%x retries:%u",
113 host->ccnt, (stop ? " (STOP)" : ""),
114 cmd->opcode, cmd->arg, cmd->flags, cmd->retries);
115
116 if (cmd->data) {
117 snprintf(host->dbgmsg_dat, 300,
118 "#%u bsize:%u blocks:%u bytes:%u",
119 host->dcnt, cmd->data->blksz,
120 cmd->data->blocks,
121 cmd->data->blocks * cmd->data->blksz);
122 } else {
123 host->dbgmsg_dat[0] = '\0';
124 }
125}
126
127static void dbg_dumpcmd(struct s3cmci_host *host, struct mmc_command *cmd,
128 int fail)
129{
130 unsigned int dbglvl = fail ? dbg_fail : dbg_debug;
131
132 if (!cmd)
133 return;
134
135 if (cmd->error == 0) {
136 dbg(host, dbglvl, "CMD[OK] %s R0:0x%08x\n",
137 host->dbgmsg_cmd, cmd->resp[0]);
138 } else {
139 dbg(host, dbglvl, "CMD[ERR %i] %s Status:%s\n",
140 cmd->error, host->dbgmsg_cmd, host->status);
141 }
142
143 if (!cmd->data)
144 return;
145
146 if (cmd->data->error == 0) {
147 dbg(host, dbglvl, "DAT[OK] %s\n", host->dbgmsg_dat);
148 } else {
149 dbg(host, dbglvl, "DAT[ERR %i] %s DCNT:0x%08x\n",
150 cmd->data->error, host->dbgmsg_dat,
151 readl(host->base + S3C2410_SDIDCNT));
152 }
153}
154#else
155static void dbg_dumpcmd(struct s3cmci_host *host,
156 struct mmc_command *cmd, int fail) { }
157
158static void prepare_dbgmsg(struct s3cmci_host *host, struct mmc_command *cmd,
159 int stop) { }
160
161static void dbg_dumpregs(struct s3cmci_host *host, char *prefix) { }
162
163#endif /* CONFIG_MMC_DEBUG */
164
165static inline u32 enable_imask(struct s3cmci_host *host, u32 imask)
166{
167 u32 newmask;
168
169 newmask = readl(host->base + host->sdiimsk);
170 newmask |= imask;
171
172 writel(newmask, host->base + host->sdiimsk);
173
174 return newmask;
175}
176
177static inline u32 disable_imask(struct s3cmci_host *host, u32 imask)
178{
179 u32 newmask;
180
181 newmask = readl(host->base + host->sdiimsk);
182 newmask &= ~imask;
183
184 writel(newmask, host->base + host->sdiimsk);
185
186 return newmask;
187}
188
189static inline void clear_imask(struct s3cmci_host *host)
190{
191 writel(0, host->base + host->sdiimsk);
192}
193
194static inline int get_data_buffer(struct s3cmci_host *host,
Christer Weinigel088a78a2008-10-15 00:17:17 +0100195 u32 *bytes, u32 **pointer)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100196{
197 struct scatterlist *sg;
198
199 if (host->pio_active == XFER_NONE)
200 return -EINVAL;
201
202 if ((!host->mrq) || (!host->mrq->data))
203 return -EINVAL;
204
205 if (host->pio_sgptr >= host->mrq->data->sg_len) {
206 dbg(host, dbg_debug, "no more buffers (%i/%i)\n",
207 host->pio_sgptr, host->mrq->data->sg_len);
208 return -EBUSY;
209 }
210 sg = &host->mrq->data->sg[host->pio_sgptr];
211
Christer Weinigel088a78a2008-10-15 00:17:17 +0100212 *bytes = sg->length;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100213 *pointer = sg_virt(sg);
214
215 host->pio_sgptr++;
216
217 dbg(host, dbg_sg, "new buffer (%i/%i)\n",
218 host->pio_sgptr, host->mrq->data->sg_len);
219
220 return 0;
221}
222
223static inline u32 fifo_count(struct s3cmci_host *host)
224{
225 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
226
227 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100228 return fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100229}
230
231static inline u32 fifo_free(struct s3cmci_host *host)
232{
233 u32 fifostat = readl(host->base + S3C2410_SDIFSTA);
234
235 fifostat &= S3C2410_SDIFSTA_COUNTMASK;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100236 return 63 - fifostat;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100237}
238
239static void do_pio_read(struct s3cmci_host *host)
240{
241 int res;
242 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100243 u32 *ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100244 u32 fifo_words;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100245 void __iomem *from_ptr;
246
247 /* write real prescaler to host, it might be set slow to fix */
248 writel(host->prescaler, host->base + S3C2410_SDIPRE);
249
250 from_ptr = host->base + host->sdidata;
251
252 while ((fifo = fifo_count(host))) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100253 if (!host->pio_bytes) {
254 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100255 &host->pio_ptr);
256 if (res) {
257 host->pio_active = XFER_NONE;
258 host->complete_what = COMPLETION_FINALIZE;
259
260 dbg(host, dbg_pio, "pio_read(): "
261 "complete (no more data).\n");
262 return;
263 }
264
265 dbg(host, dbg_pio,
266 "pio_read(): new target: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100267 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100268 }
269
270 dbg(host, dbg_pio,
271 "pio_read(): fifo:[%02i] buffer:[%03i] dcnt:[%08X]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100272 fifo, host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100273 readl(host->base + S3C2410_SDIDCNT));
274
Christer Weinigel088a78a2008-10-15 00:17:17 +0100275 /* If we have reached the end of the block, we can
276 * read a word and get 1 to 3 bytes. If we in the
277 * middle of the block, we have to read full words,
278 * otherwise we will write garbage, so round down to
279 * an even multiple of 4. */
280 if (fifo >= host->pio_bytes)
281 fifo = host->pio_bytes;
282 else
283 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100284
Christer Weinigel088a78a2008-10-15 00:17:17 +0100285 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100286 host->pio_count += fifo;
287
Christer Weinigel088a78a2008-10-15 00:17:17 +0100288 fifo_words = fifo >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100289 ptr = host->pio_ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100290 while (fifo_words--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100291 *ptr++ = readl(from_ptr);
292 host->pio_ptr = ptr;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100293
294 if (fifo & 3) {
295 u32 n = fifo & 3;
296 u32 data = readl(from_ptr);
297 u8 *p = (u8 *)host->pio_ptr;
298
299 while (n--) {
300 *p++ = data;
301 data >>= 8;
302 }
303 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100304 }
305
Christer Weinigel088a78a2008-10-15 00:17:17 +0100306 if (!host->pio_bytes) {
307 res = get_data_buffer(host, &host->pio_bytes, &host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100308 if (res) {
309 dbg(host, dbg_pio,
310 "pio_read(): complete (no more buffers).\n");
311 host->pio_active = XFER_NONE;
312 host->complete_what = COMPLETION_FINALIZE;
313
314 return;
315 }
316 }
317
318 enable_imask(host,
319 S3C2410_SDIIMSK_RXFIFOHALF | S3C2410_SDIIMSK_RXFIFOLAST);
320}
321
322static void do_pio_write(struct s3cmci_host *host)
323{
324 void __iomem *to_ptr;
325 int res;
326 u32 fifo;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100327 u32 *ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100328
329 to_ptr = host->base + host->sdidata;
330
Yauhen Kharuzhy99424482009-02-11 13:25:52 -0800331 while ((fifo = fifo_free(host)) > 3) {
Christer Weinigel088a78a2008-10-15 00:17:17 +0100332 if (!host->pio_bytes) {
333 res = get_data_buffer(host, &host->pio_bytes,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100334 &host->pio_ptr);
335 if (res) {
336 dbg(host, dbg_pio,
337 "pio_write(): complete (no more data).\n");
338 host->pio_active = XFER_NONE;
339
340 return;
341 }
342
343 dbg(host, dbg_pio,
344 "pio_write(): new source: [%i]@[%p]\n",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100345 host->pio_bytes, host->pio_ptr);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100346
347 }
348
Christer Weinigel088a78a2008-10-15 00:17:17 +0100349 /* If we have reached the end of the block, we have to
350 * write exactly the remaining number of bytes. If we
351 * in the middle of the block, we have to write full
352 * words, so round down to an even multiple of 4. */
353 if (fifo >= host->pio_bytes)
354 fifo = host->pio_bytes;
355 else
356 fifo -= fifo & 3;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100357
Christer Weinigel088a78a2008-10-15 00:17:17 +0100358 host->pio_bytes -= fifo;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100359 host->pio_count += fifo;
360
Christer Weinigel088a78a2008-10-15 00:17:17 +0100361 fifo = (fifo + 3) >> 2;
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100362 ptr = host->pio_ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100363 while (fifo--)
ben@fluff.org.uk18280ff2008-10-15 00:17:18 +0100364 writel(*ptr++, to_ptr);
365 host->pio_ptr = ptr;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100366 }
367
368 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
369}
370
371static void pio_tasklet(unsigned long data)
372{
373 struct s3cmci_host *host = (struct s3cmci_host *) data;
374
375
Roman Moracikd643b5f2008-06-30 22:40:28 +0100376 disable_irq(host->irq);
377
Thomas Kleffelbe518012008-06-30 22:40:24 +0100378 if (host->pio_active == XFER_WRITE)
379 do_pio_write(host);
380
381 if (host->pio_active == XFER_READ)
382 do_pio_read(host);
383
384 if (host->complete_what == COMPLETION_FINALIZE) {
385 clear_imask(host);
386 if (host->pio_active != XFER_NONE) {
387 dbg(host, dbg_err, "unfinished %s "
Christer Weinigel088a78a2008-10-15 00:17:17 +0100388 "- pio_count:[%u] pio_bytes:[%u]\n",
Thomas Kleffelbe518012008-06-30 22:40:24 +0100389 (host->pio_active == XFER_READ) ? "read" : "write",
Christer Weinigel088a78a2008-10-15 00:17:17 +0100390 host->pio_count, host->pio_bytes);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100391
Ben Dooks7c144502008-06-30 22:40:31 +0100392 if (host->mrq->data)
393 host->mrq->data->error = -EINVAL;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100394 }
395
Thomas Kleffelbe518012008-06-30 22:40:24 +0100396 finalize_request(host);
Roman Moracikd643b5f2008-06-30 22:40:28 +0100397 } else
398 enable_irq(host->irq);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100399}
400
401/*
402 * ISR for SDI Interface IRQ
403 * Communication between driver and ISR works as follows:
404 * host->mrq points to current request
405 * host->complete_what Indicates when the request is considered done
406 * COMPLETION_CMDSENT when the command was sent
407 * COMPLETION_RSPFIN when a response was received
408 * COMPLETION_XFERFINISH when the data transfer is finished
409 * COMPLETION_XFERFINISH_RSPFIN both of the above.
410 * host->complete_request is the completion-object the driver waits for
411 *
412 * 1) Driver sets up host->mrq and host->complete_what
413 * 2) Driver prepares the transfer
414 * 3) Driver enables interrupts
415 * 4) Driver starts transfer
416 * 5) Driver waits for host->complete_rquest
417 * 6) ISR checks for request status (errors and success)
418 * 6) ISR sets host->mrq->cmd->error and host->mrq->data->error
419 * 7) ISR completes host->complete_request
420 * 8) ISR disables interrupts
421 * 9) Driver wakes up and takes care of the request
422 *
423 * Note: "->error"-fields are expected to be set to 0 before the request
424 * was issued by mmc.c - therefore they are only set, when an error
425 * contition comes up
426 */
427
428static irqreturn_t s3cmci_irq(int irq, void *dev_id)
429{
430 struct s3cmci_host *host = dev_id;
431 struct mmc_command *cmd;
432 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt, mci_imsk;
433 u32 mci_cclear, mci_dclear;
434 unsigned long iflags;
435
436 spin_lock_irqsave(&host->complete_lock, iflags);
437
438 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
439 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
440 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
441 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
442 mci_imsk = readl(host->base + host->sdiimsk);
443 mci_cclear = 0;
444 mci_dclear = 0;
445
446 if ((host->complete_what == COMPLETION_NONE) ||
447 (host->complete_what == COMPLETION_FINALIZE)) {
448 host->status = "nothing to complete";
449 clear_imask(host);
450 goto irq_out;
451 }
452
453 if (!host->mrq) {
454 host->status = "no active mrq";
455 clear_imask(host);
456 goto irq_out;
457 }
458
459 cmd = host->cmd_is_stop ? host->mrq->stop : host->mrq->cmd;
460
461 if (!cmd) {
462 host->status = "no active cmd";
463 clear_imask(host);
464 goto irq_out;
465 }
466
467 if (!host->dodma) {
468 if ((host->pio_active == XFER_WRITE) &&
469 (mci_fsta & S3C2410_SDIFSTA_TFDET)) {
470
471 disable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
472 tasklet_schedule(&host->pio_tasklet);
473 host->status = "pio tx";
474 }
475
476 if ((host->pio_active == XFER_READ) &&
477 (mci_fsta & S3C2410_SDIFSTA_RFDET)) {
478
479 disable_imask(host,
480 S3C2410_SDIIMSK_RXFIFOHALF |
481 S3C2410_SDIIMSK_RXFIFOLAST);
482
483 tasklet_schedule(&host->pio_tasklet);
484 host->status = "pio rx";
485 }
486 }
487
488 if (mci_csta & S3C2410_SDICMDSTAT_CMDTIMEOUT) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100489 dbg(host, dbg_err, "CMDSTAT: error CMDTIMEOUT\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100490 cmd->error = -ETIMEDOUT;
491 host->status = "error: command timeout";
492 goto fail_transfer;
493 }
494
495 if (mci_csta & S3C2410_SDICMDSTAT_CMDSENT) {
496 if (host->complete_what == COMPLETION_CMDSENT) {
497 host->status = "ok: command sent";
498 goto close_transfer;
499 }
500
501 mci_cclear |= S3C2410_SDICMDSTAT_CMDSENT;
502 }
503
504 if (mci_csta & S3C2410_SDICMDSTAT_CRCFAIL) {
505 if (cmd->flags & MMC_RSP_CRC) {
Harald Welte679f0f82008-06-30 22:40:25 +0100506 if (host->mrq->cmd->flags & MMC_RSP_136) {
507 dbg(host, dbg_irq,
508 "fixup: ignore CRC fail with long rsp\n");
509 } else {
510 /* note, we used to fail the transfer
511 * here, but it seems that this is just
512 * the hardware getting it wrong.
513 *
514 * cmd->error = -EILSEQ;
515 * host->status = "error: bad command crc";
516 * goto fail_transfer;
517 */
518 }
Thomas Kleffelbe518012008-06-30 22:40:24 +0100519 }
520
521 mci_cclear |= S3C2410_SDICMDSTAT_CRCFAIL;
522 }
523
524 if (mci_csta & S3C2410_SDICMDSTAT_RSPFIN) {
525 if (host->complete_what == COMPLETION_RSPFIN) {
526 host->status = "ok: command response received";
527 goto close_transfer;
528 }
529
530 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
531 host->complete_what = COMPLETION_XFERFINISH;
532
533 mci_cclear |= S3C2410_SDICMDSTAT_RSPFIN;
534 }
535
536 /* errors handled after this point are only relevant
537 when a data transfer is in progress */
538
539 if (!cmd->data)
540 goto clear_status_bits;
541
542 /* Check for FIFO failure */
543 if (host->is2440) {
544 if (mci_fsta & S3C2440_SDIFSTA_FIFOFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100545 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100546 host->mrq->data->error = -EILSEQ;
547 host->status = "error: 2440 fifo failure";
548 goto fail_transfer;
549 }
550 } else {
551 if (mci_dsta & S3C2410_SDIDSTA_FIFOFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100552 dbg(host, dbg_err, "FIFO failure\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100553 cmd->data->error = -EILSEQ;
554 host->status = "error: fifo failure";
555 goto fail_transfer;
556 }
557 }
558
559 if (mci_dsta & S3C2410_SDIDSTA_RXCRCFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100560 dbg(host, dbg_err, "bad data crc (outgoing)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100561 cmd->data->error = -EILSEQ;
562 host->status = "error: bad data crc (outgoing)";
563 goto fail_transfer;
564 }
565
566 if (mci_dsta & S3C2410_SDIDSTA_CRCFAIL) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100567 dbg(host, dbg_err, "bad data crc (incoming)\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100568 cmd->data->error = -EILSEQ;
569 host->status = "error: bad data crc (incoming)";
570 goto fail_transfer;
571 }
572
573 if (mci_dsta & S3C2410_SDIDSTA_DATATIMEOUT) {
Ben Dooksff8c8042008-06-30 22:40:37 +0100574 dbg(host, dbg_err, "data timeout\n");
Thomas Kleffelbe518012008-06-30 22:40:24 +0100575 cmd->data->error = -ETIMEDOUT;
576 host->status = "error: data timeout";
577 goto fail_transfer;
578 }
579
580 if (mci_dsta & S3C2410_SDIDSTA_XFERFINISH) {
581 if (host->complete_what == COMPLETION_XFERFINISH) {
582 host->status = "ok: data transfer completed";
583 goto close_transfer;
584 }
585
586 if (host->complete_what == COMPLETION_XFERFINISH_RSPFIN)
587 host->complete_what = COMPLETION_RSPFIN;
588
589 mci_dclear |= S3C2410_SDIDSTA_XFERFINISH;
590 }
591
592clear_status_bits:
593 writel(mci_cclear, host->base + S3C2410_SDICMDSTAT);
594 writel(mci_dclear, host->base + S3C2410_SDIDSTA);
595
596 goto irq_out;
597
598fail_transfer:
599 host->pio_active = XFER_NONE;
600
601close_transfer:
602 host->complete_what = COMPLETION_FINALIZE;
603
604 clear_imask(host);
605 tasklet_schedule(&host->pio_tasklet);
606
607 goto irq_out;
608
609irq_out:
610 dbg(host, dbg_irq,
611 "csta:0x%08x dsta:0x%08x fsta:0x%08x dcnt:0x%08x status:%s.\n",
612 mci_csta, mci_dsta, mci_fsta, mci_dcnt, host->status);
613
614 spin_unlock_irqrestore(&host->complete_lock, iflags);
615 return IRQ_HANDLED;
616
617}
618
619/*
620 * ISR for the CardDetect Pin
621*/
622
623static irqreturn_t s3cmci_irq_cd(int irq, void *dev_id)
624{
625 struct s3cmci_host *host = (struct s3cmci_host *)dev_id;
626
627 dbg(host, dbg_irq, "card detect\n");
628
Ben Dooks2de5f792008-06-30 22:40:35 +0100629 mmc_detect_change(host->mmc, msecs_to_jiffies(500));
Thomas Kleffelbe518012008-06-30 22:40:24 +0100630
631 return IRQ_HANDLED;
632}
633
Ben Dooks5d304402008-08-08 10:55:41 +0100634static void s3cmci_dma_done_callback(struct s3c2410_dma_chan *dma_ch,
635 void *buf_id, int size,
636 enum s3c2410_dma_buffresult result)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100637{
638 struct s3cmci_host *host = buf_id;
639 unsigned long iflags;
640 u32 mci_csta, mci_dsta, mci_fsta, mci_dcnt;
641
642 mci_csta = readl(host->base + S3C2410_SDICMDSTAT);
643 mci_dsta = readl(host->base + S3C2410_SDIDSTA);
644 mci_fsta = readl(host->base + S3C2410_SDIFSTA);
645 mci_dcnt = readl(host->base + S3C2410_SDIDCNT);
646
647 BUG_ON(!host->mrq);
648 BUG_ON(!host->mrq->data);
649 BUG_ON(!host->dmatogo);
650
651 spin_lock_irqsave(&host->complete_lock, iflags);
652
653 if (result != S3C2410_RES_OK) {
654 dbg(host, dbg_fail, "DMA FAILED: csta=0x%08x dsta=0x%08x "
655 "fsta=0x%08x dcnt:0x%08x result:0x%08x toGo:%u\n",
656 mci_csta, mci_dsta, mci_fsta,
657 mci_dcnt, result, host->dmatogo);
658
659 goto fail_request;
660 }
661
662 host->dmatogo--;
663 if (host->dmatogo) {
664 dbg(host, dbg_dma, "DMA DONE Size:%i DSTA:[%08x] "
665 "DCNT:[%08x] toGo:%u\n",
666 size, mci_dsta, mci_dcnt, host->dmatogo);
667
668 goto out;
669 }
670
671 dbg(host, dbg_dma, "DMA FINISHED Size:%i DSTA:%08x DCNT:%08x\n",
672 size, mci_dsta, mci_dcnt);
673
674 host->complete_what = COMPLETION_FINALIZE;
675
676out:
677 tasklet_schedule(&host->pio_tasklet);
678 spin_unlock_irqrestore(&host->complete_lock, iflags);
679 return;
680
Thomas Kleffelbe518012008-06-30 22:40:24 +0100681fail_request:
682 host->mrq->data->error = -EINVAL;
683 host->complete_what = COMPLETION_FINALIZE;
684 writel(0, host->base + host->sdiimsk);
685 goto out;
686
687}
688
689static void finalize_request(struct s3cmci_host *host)
690{
691 struct mmc_request *mrq = host->mrq;
692 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
693 int debug_as_failure = 0;
694
695 if (host->complete_what != COMPLETION_FINALIZE)
696 return;
697
698 if (!mrq)
699 return;
700
701 if (cmd->data && (cmd->error == 0) &&
702 (cmd->data->error == 0)) {
703 if (host->dodma && (!host->dma_complete)) {
704 dbg(host, dbg_dma, "DMA Missing!\n");
705 return;
706 }
707 }
708
709 /* Read response from controller. */
710 cmd->resp[0] = readl(host->base + S3C2410_SDIRSP0);
711 cmd->resp[1] = readl(host->base + S3C2410_SDIRSP1);
712 cmd->resp[2] = readl(host->base + S3C2410_SDIRSP2);
713 cmd->resp[3] = readl(host->base + S3C2410_SDIRSP3);
714
715 writel(host->prescaler, host->base + S3C2410_SDIPRE);
716
717 if (cmd->error)
718 debug_as_failure = 1;
719
720 if (cmd->data && cmd->data->error)
721 debug_as_failure = 1;
722
723 dbg_dumpcmd(host, cmd, debug_as_failure);
724
725 /* Cleanup controller */
726 writel(0, host->base + S3C2410_SDICMDARG);
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100727 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100728 writel(0, host->base + S3C2410_SDICMDCON);
729 writel(0, host->base + host->sdiimsk);
730
731 if (cmd->data && cmd->error)
732 cmd->data->error = cmd->error;
733
734 if (cmd->data && cmd->data->stop && (!host->cmd_is_stop)) {
735 host->cmd_is_stop = 1;
736 s3cmci_send_request(host->mmc);
737 return;
738 }
739
740 /* If we have no data transfer we are finished here */
741 if (!mrq->data)
742 goto request_done;
743
744 /* Calulate the amout of bytes transfer if there was no error */
745 if (mrq->data->error == 0) {
746 mrq->data->bytes_xfered =
747 (mrq->data->blocks * mrq->data->blksz);
748 } else {
749 mrq->data->bytes_xfered = 0;
750 }
751
752 /* If we had an error while transfering data we flush the
753 * DMA channel and the fifo to clear out any garbage. */
754 if (mrq->data->error != 0) {
755 if (host->dodma)
756 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
757
758 if (host->is2440) {
759 /* Clear failure register and reset fifo. */
760 writel(S3C2440_SDIFSTA_FIFORESET |
761 S3C2440_SDIFSTA_FIFOFAIL,
762 host->base + S3C2410_SDIFSTA);
763 } else {
764 u32 mci_con;
765
766 /* reset fifo */
767 mci_con = readl(host->base + S3C2410_SDICON);
768 mci_con |= S3C2410_SDICON_FIFORESET;
769
770 writel(mci_con, host->base + S3C2410_SDICON);
771 }
772 }
773
774request_done:
775 host->complete_what = COMPLETION_NONE;
776 host->mrq = NULL;
777 mmc_request_done(host->mmc, mrq);
778}
779
Ben Dooks5d304402008-08-08 10:55:41 +0100780static void s3cmci_dma_setup(struct s3cmci_host *host,
781 enum s3c2410_dmasrc source)
Thomas Kleffelbe518012008-06-30 22:40:24 +0100782{
783 static enum s3c2410_dmasrc last_source = -1;
784 static int setup_ok;
785
786 if (last_source == source)
787 return;
788
789 last_source = source;
790
Ben Dooks8970ef42009-03-19 15:02:34 +0000791 s3c2410_dma_devconfig(host->dma, source,
Thomas Kleffelbe518012008-06-30 22:40:24 +0100792 host->mem->start + host->sdidata);
793
794 if (!setup_ok) {
Ben Dooksfe9db6c2009-06-08 23:33:56 +0100795 s3c2410_dma_config(host->dma, 4);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100796 s3c2410_dma_set_buffdone_fn(host->dma,
797 s3cmci_dma_done_callback);
798 s3c2410_dma_setflags(host->dma, S3C2410_DMAF_AUTOSTART);
799 setup_ok = 1;
800 }
801}
802
803static void s3cmci_send_command(struct s3cmci_host *host,
804 struct mmc_command *cmd)
805{
806 u32 ccon, imsk;
807
808 imsk = S3C2410_SDIIMSK_CRCSTATUS | S3C2410_SDIIMSK_CMDTIMEOUT |
809 S3C2410_SDIIMSK_RESPONSEND | S3C2410_SDIIMSK_CMDSENT |
810 S3C2410_SDIIMSK_RESPONSECRC;
811
812 enable_imask(host, imsk);
813
814 if (cmd->data)
815 host->complete_what = COMPLETION_XFERFINISH_RSPFIN;
816 else if (cmd->flags & MMC_RSP_PRESENT)
817 host->complete_what = COMPLETION_RSPFIN;
818 else
819 host->complete_what = COMPLETION_CMDSENT;
820
821 writel(cmd->arg, host->base + S3C2410_SDICMDARG);
822
823 ccon = cmd->opcode & S3C2410_SDICMDCON_INDEX;
824 ccon |= S3C2410_SDICMDCON_SENDERHOST | S3C2410_SDICMDCON_CMDSTART;
825
826 if (cmd->flags & MMC_RSP_PRESENT)
827 ccon |= S3C2410_SDICMDCON_WAITRSP;
828
829 if (cmd->flags & MMC_RSP_136)
830 ccon |= S3C2410_SDICMDCON_LONGRSP;
831
832 writel(ccon, host->base + S3C2410_SDICMDCON);
833}
834
835static int s3cmci_setup_data(struct s3cmci_host *host, struct mmc_data *data)
836{
837 u32 dcon, imsk, stoptries = 3;
838
839 /* write DCON register */
840
841 if (!data) {
842 writel(0, host->base + S3C2410_SDIDCON);
843 return 0;
844 }
845
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100846 if ((data->blksz & 3) != 0) {
847 /* We cannot deal with unaligned blocks with more than
848 * one block being transfered. */
849
Christer Weinigel088a78a2008-10-15 00:17:17 +0100850 if (data->blocks > 1) {
851 pr_warning("%s: can't do non-word sized block transfers (blksz %d)\n", __func__, data->blksz);
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100852 return -EINVAL;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100853 }
Ben Dooks7e9c7b62008-06-30 22:40:39 +0100854 }
855
Thomas Kleffelbe518012008-06-30 22:40:24 +0100856 while (readl(host->base + S3C2410_SDIDSTA) &
857 (S3C2410_SDIDSTA_TXDATAON | S3C2410_SDIDSTA_RXDATAON)) {
858
859 dbg(host, dbg_err,
860 "mci_setup_data() transfer stillin progress.\n");
861
Thomas Kleffelbdbc9c32008-06-30 22:40:27 +0100862 writel(S3C2410_SDIDCON_STOP, host->base + S3C2410_SDIDCON);
Thomas Kleffelbe518012008-06-30 22:40:24 +0100863 s3cmci_reset(host);
864
865 if ((stoptries--) == 0) {
866 dbg_dumpregs(host, "DRF");
867 return -EINVAL;
868 }
869 }
870
871 dcon = data->blocks & S3C2410_SDIDCON_BLKNUM_MASK;
872
873 if (host->dodma)
874 dcon |= S3C2410_SDIDCON_DMAEN;
875
876 if (host->bus_width == MMC_BUS_WIDTH_4)
877 dcon |= S3C2410_SDIDCON_WIDEBUS;
878
879 if (!(data->flags & MMC_DATA_STREAM))
880 dcon |= S3C2410_SDIDCON_BLOCKMODE;
881
882 if (data->flags & MMC_DATA_WRITE) {
883 dcon |= S3C2410_SDIDCON_TXAFTERRESP;
884 dcon |= S3C2410_SDIDCON_XFER_TXSTART;
885 }
886
887 if (data->flags & MMC_DATA_READ) {
888 dcon |= S3C2410_SDIDCON_RXAFTERCMD;
889 dcon |= S3C2410_SDIDCON_XFER_RXSTART;
890 }
891
892 if (host->is2440) {
893 dcon |= S3C2440_SDIDCON_DS_WORD;
894 dcon |= S3C2440_SDIDCON_DATSTART;
895 }
896
897 writel(dcon, host->base + S3C2410_SDIDCON);
898
899 /* write BSIZE register */
900
901 writel(data->blksz, host->base + S3C2410_SDIBSIZE);
902
903 /* add to IMASK register */
904 imsk = S3C2410_SDIIMSK_FIFOFAIL | S3C2410_SDIIMSK_DATACRC |
905 S3C2410_SDIIMSK_DATATIMEOUT | S3C2410_SDIIMSK_DATAFINISH;
906
907 enable_imask(host, imsk);
908
909 /* write TIMER register */
910
911 if (host->is2440) {
912 writel(0x007FFFFF, host->base + S3C2410_SDITIMER);
913 } else {
914 writel(0x0000FFFF, host->base + S3C2410_SDITIMER);
915
916 /* FIX: set slow clock to prevent timeouts on read */
917 if (data->flags & MMC_DATA_READ)
918 writel(0xFF, host->base + S3C2410_SDIPRE);
919 }
920
921 return 0;
922}
923
924#define BOTH_DIR (MMC_DATA_WRITE | MMC_DATA_READ)
925
926static int s3cmci_prepare_pio(struct s3cmci_host *host, struct mmc_data *data)
927{
928 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
929
930 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
931
932 host->pio_sgptr = 0;
Christer Weinigel088a78a2008-10-15 00:17:17 +0100933 host->pio_bytes = 0;
Thomas Kleffelbe518012008-06-30 22:40:24 +0100934 host->pio_count = 0;
935 host->pio_active = rw ? XFER_WRITE : XFER_READ;
936
937 if (rw) {
938 do_pio_write(host);
939 enable_imask(host, S3C2410_SDIIMSK_TXFIFOHALF);
940 } else {
941 enable_imask(host, S3C2410_SDIIMSK_RXFIFOHALF
942 | S3C2410_SDIIMSK_RXFIFOLAST);
943 }
944
945 return 0;
946}
947
948static int s3cmci_prepare_dma(struct s3cmci_host *host, struct mmc_data *data)
949{
950 int dma_len, i;
951 int rw = (data->flags & MMC_DATA_WRITE) ? 1 : 0;
952
953 BUG_ON((data->flags & BOTH_DIR) == BOTH_DIR);
954
955 s3cmci_dma_setup(host, rw ? S3C2410_DMASRC_MEM : S3C2410_DMASRC_HW);
956 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
957
958 dma_len = dma_map_sg(mmc_dev(host->mmc), data->sg, data->sg_len,
959 (rw) ? DMA_TO_DEVICE : DMA_FROM_DEVICE);
960
961 if (dma_len == 0)
962 return -ENOMEM;
963
964 host->dma_complete = 0;
965 host->dmatogo = dma_len;
966
967 for (i = 0; i < dma_len; i++) {
968 int res;
969
970 dbg(host, dbg_dma, "enqueue %i:%u@%u\n", i,
971 sg_dma_address(&data->sg[i]),
972 sg_dma_len(&data->sg[i]));
973
974 res = s3c2410_dma_enqueue(host->dma, (void *) host,
975 sg_dma_address(&data->sg[i]),
976 sg_dma_len(&data->sg[i]));
977
978 if (res) {
979 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_FLUSH);
980 return -EBUSY;
981 }
982 }
983
984 s3c2410_dma_ctrl(host->dma, S3C2410_DMAOP_START);
985
986 return 0;
987}
988
989static void s3cmci_send_request(struct mmc_host *mmc)
990{
991 struct s3cmci_host *host = mmc_priv(mmc);
992 struct mmc_request *mrq = host->mrq;
993 struct mmc_command *cmd = host->cmd_is_stop ? mrq->stop : mrq->cmd;
994
995 host->ccnt++;
996 prepare_dbgmsg(host, cmd, host->cmd_is_stop);
997
998 /* Clear command, data and fifo status registers
999 Fifo clear only necessary on 2440, but doesn't hurt on 2410
1000 */
1001 writel(0xFFFFFFFF, host->base + S3C2410_SDICMDSTAT);
1002 writel(0xFFFFFFFF, host->base + S3C2410_SDIDSTA);
1003 writel(0xFFFFFFFF, host->base + S3C2410_SDIFSTA);
1004
1005 if (cmd->data) {
1006 int res = s3cmci_setup_data(host, cmd->data);
1007
1008 host->dcnt++;
1009
1010 if (res) {
Ben Dooksff8c8042008-06-30 22:40:37 +01001011 dbg(host, dbg_err, "setup data error %d\n", res);
1012 cmd->error = res;
1013 cmd->data->error = res;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001014
1015 mmc_request_done(mmc, mrq);
1016 return;
1017 }
1018
1019 if (host->dodma)
1020 res = s3cmci_prepare_dma(host, cmd->data);
1021 else
1022 res = s3cmci_prepare_pio(host, cmd->data);
1023
1024 if (res) {
Ben Dooksff8c8042008-06-30 22:40:37 +01001025 dbg(host, dbg_err, "data prepare error %d\n", res);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001026 cmd->error = res;
1027 cmd->data->error = res;
1028
1029 mmc_request_done(mmc, mrq);
1030 return;
1031 }
1032 }
1033
1034 /* Send command */
1035 s3cmci_send_command(host, cmd);
1036
1037 /* Enable Interrupt */
1038 enable_irq(host->irq);
1039}
1040
Ben Dooks87dd9802008-08-12 09:24:50 +01001041static int s3cmci_card_present(struct mmc_host *mmc)
Ben Dooks50a84572008-06-30 22:40:36 +01001042{
Ben Dooks87dd9802008-08-12 09:24:50 +01001043 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dooks50a84572008-06-30 22:40:36 +01001044 struct s3c24xx_mci_pdata *pdata = host->pdata;
1045 int ret;
1046
1047 if (pdata->gpio_detect == 0)
1048 return -ENOSYS;
1049
1050 ret = s3c2410_gpio_getpin(pdata->gpio_detect) ? 0 : 1;
1051 return ret ^ pdata->detect_invert;
1052}
1053
Thomas Kleffelbe518012008-06-30 22:40:24 +01001054static void s3cmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
1055{
1056 struct s3cmci_host *host = mmc_priv(mmc);
1057
1058 host->status = "mmc request";
1059 host->cmd_is_stop = 0;
1060 host->mrq = mrq;
1061
Ben Dooks87dd9802008-08-12 09:24:50 +01001062 if (s3cmci_card_present(mmc) == 0) {
Ben Dooks50a84572008-06-30 22:40:36 +01001063 dbg(host, dbg_err, "%s: no medium present\n", __func__);
1064 host->mrq->cmd->error = -ENOMEDIUM;
1065 mmc_request_done(mmc, mrq);
1066 } else
1067 s3cmci_send_request(mmc);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001068}
1069
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001070static void s3cmci_set_clk(struct s3cmci_host *host, struct mmc_ios *ios)
1071{
1072 u32 mci_psc;
1073
1074 /* Set clock */
1075 for (mci_psc = 0; mci_psc < 255; mci_psc++) {
1076 host->real_rate = host->clk_rate / (host->clk_div*(mci_psc+1));
1077
1078 if (host->real_rate <= ios->clock)
1079 break;
1080 }
1081
1082 if (mci_psc > 255)
1083 mci_psc = 255;
1084
1085 host->prescaler = mci_psc;
1086 writel(host->prescaler, host->base + S3C2410_SDIPRE);
1087
1088 /* If requested clock is 0, real_rate will be 0, too */
1089 if (ios->clock == 0)
1090 host->real_rate = 0;
1091}
1092
Thomas Kleffelbe518012008-06-30 22:40:24 +01001093static void s3cmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
1094{
1095 struct s3cmci_host *host = mmc_priv(mmc);
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001096 u32 mci_con;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001097
1098 /* Set the power state */
1099
1100 mci_con = readl(host->base + S3C2410_SDICON);
1101
1102 switch (ios->power_mode) {
1103 case MMC_POWER_ON:
1104 case MMC_POWER_UP:
1105 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPE5_SDCLK);
1106 s3c2410_gpio_cfgpin(S3C2410_GPE6, S3C2410_GPE6_SDCMD);
1107 s3c2410_gpio_cfgpin(S3C2410_GPE7, S3C2410_GPE7_SDDAT0);
1108 s3c2410_gpio_cfgpin(S3C2410_GPE8, S3C2410_GPE8_SDDAT1);
1109 s3c2410_gpio_cfgpin(S3C2410_GPE9, S3C2410_GPE9_SDDAT2);
1110 s3c2410_gpio_cfgpin(S3C2410_GPE10, S3C2410_GPE10_SDDAT3);
1111
Ben Dooksedb5a982008-06-30 22:40:29 +01001112 if (host->pdata->set_power)
1113 host->pdata->set_power(ios->power_mode, ios->vdd);
1114
Thomas Kleffelbe518012008-06-30 22:40:24 +01001115 if (!host->is2440)
1116 mci_con |= S3C2410_SDICON_FIFORESET;
1117
1118 break;
1119
1120 case MMC_POWER_OFF:
1121 default:
1122 s3c2410_gpio_setpin(S3C2410_GPE5, 0);
Ben Dooksec976d62009-05-13 22:52:24 +01001123 s3c2410_gpio_cfgpin(S3C2410_GPE5, S3C2410_GPIO_OUTPUT);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001124
1125 if (host->is2440)
1126 mci_con |= S3C2440_SDICON_SDRESET;
1127
Ben Dooksedb5a982008-06-30 22:40:29 +01001128 if (host->pdata->set_power)
1129 host->pdata->set_power(ios->power_mode, ios->vdd);
1130
Thomas Kleffelbe518012008-06-30 22:40:24 +01001131 break;
1132 }
1133
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001134 s3cmci_set_clk(host, ios);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001135
1136 /* Set CLOCK_ENABLE */
1137 if (ios->clock)
1138 mci_con |= S3C2410_SDICON_CLOCKTYPE;
1139 else
1140 mci_con &= ~S3C2410_SDICON_CLOCKTYPE;
1141
1142 writel(mci_con, host->base + S3C2410_SDICON);
1143
1144 if ((ios->power_mode == MMC_POWER_ON) ||
1145 (ios->power_mode == MMC_POWER_UP)) {
1146 dbg(host, dbg_conf, "running at %lukHz (requested: %ukHz).\n",
1147 host->real_rate/1000, ios->clock/1000);
1148 } else {
1149 dbg(host, dbg_conf, "powered down.\n");
1150 }
1151
1152 host->bus_width = ios->bus_width;
1153}
1154
1155static void s3cmci_reset(struct s3cmci_host *host)
1156{
1157 u32 con = readl(host->base + S3C2410_SDICON);
1158
1159 con |= S3C2440_SDICON_SDRESET;
1160 writel(con, host->base + S3C2410_SDICON);
1161}
1162
Ben Dooksedb5a982008-06-30 22:40:29 +01001163static int s3cmci_get_ro(struct mmc_host *mmc)
1164{
1165 struct s3cmci_host *host = mmc_priv(mmc);
Ben Dookscf0984c2008-06-30 22:40:30 +01001166 struct s3c24xx_mci_pdata *pdata = host->pdata;
1167 int ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001168
Ben Dookscf0984c2008-06-30 22:40:30 +01001169 if (pdata->gpio_wprotect == 0)
Ben Dooksedb5a982008-06-30 22:40:29 +01001170 return 0;
1171
Ben Dookscf0984c2008-06-30 22:40:30 +01001172 ret = s3c2410_gpio_getpin(pdata->gpio_wprotect);
1173
1174 if (pdata->wprotect_invert)
1175 ret = !ret;
1176
1177 return ret;
Ben Dooksedb5a982008-06-30 22:40:29 +01001178}
1179
Thomas Kleffelbe518012008-06-30 22:40:24 +01001180static struct mmc_host_ops s3cmci_ops = {
1181 .request = s3cmci_request,
1182 .set_ios = s3cmci_set_ios,
Ben Dooksedb5a982008-06-30 22:40:29 +01001183 .get_ro = s3cmci_get_ro,
Ben Dooks87dd9802008-08-12 09:24:50 +01001184 .get_cd = s3cmci_card_present,
Ben Dooksedb5a982008-06-30 22:40:29 +01001185};
1186
1187static struct s3c24xx_mci_pdata s3cmci_def_pdata = {
1188 /* This is currently here to avoid a number of if (host->pdata)
1189 * checks. Any zero fields to ensure reaonable defaults are picked. */
Thomas Kleffelbe518012008-06-30 22:40:24 +01001190};
1191
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001192#ifdef CONFIG_CPU_FREQ
1193
1194static int s3cmci_cpufreq_transition(struct notifier_block *nb,
1195 unsigned long val, void *data)
1196{
1197 struct s3cmci_host *host;
1198 struct mmc_host *mmc;
1199 unsigned long newclk;
1200 unsigned long flags;
1201
1202 host = container_of(nb, struct s3cmci_host, freq_transition);
1203 newclk = clk_get_rate(host->clk);
1204 mmc = host->mmc;
1205
1206 if ((val == CPUFREQ_PRECHANGE && newclk > host->clk_rate) ||
1207 (val == CPUFREQ_POSTCHANGE && newclk < host->clk_rate)) {
1208 spin_lock_irqsave(&mmc->lock, flags);
1209
1210 host->clk_rate = newclk;
1211
1212 if (mmc->ios.power_mode != MMC_POWER_OFF &&
1213 mmc->ios.clock != 0)
1214 s3cmci_set_clk(host, &mmc->ios);
1215
1216 spin_unlock_irqrestore(&mmc->lock, flags);
1217 }
1218
1219 return 0;
1220}
1221
1222static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1223{
1224 host->freq_transition.notifier_call = s3cmci_cpufreq_transition;
1225
1226 return cpufreq_register_notifier(&host->freq_transition,
1227 CPUFREQ_TRANSITION_NOTIFIER);
1228}
1229
1230static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1231{
1232 cpufreq_unregister_notifier(&host->freq_transition,
1233 CPUFREQ_TRANSITION_NOTIFIER);
1234}
1235
1236#else
1237static inline int s3cmci_cpufreq_register(struct s3cmci_host *host)
1238{
1239 return 0;
1240}
1241
1242static inline void s3cmci_cpufreq_deregister(struct s3cmci_host *host)
1243{
1244}
1245#endif
1246
Thomas Kleffelbe518012008-06-30 22:40:24 +01001247static int __devinit s3cmci_probe(struct platform_device *pdev, int is2440)
1248{
1249 struct s3cmci_host *host;
1250 struct mmc_host *mmc;
1251 int ret;
1252
1253 mmc = mmc_alloc_host(sizeof(struct s3cmci_host), &pdev->dev);
1254 if (!mmc) {
1255 ret = -ENOMEM;
1256 goto probe_out;
1257 }
1258
1259 host = mmc_priv(mmc);
1260 host->mmc = mmc;
1261 host->pdev = pdev;
1262 host->is2440 = is2440;
1263
Ben Dooksedb5a982008-06-30 22:40:29 +01001264 host->pdata = pdev->dev.platform_data;
1265 if (!host->pdata) {
1266 pdev->dev.platform_data = &s3cmci_def_pdata;
1267 host->pdata = &s3cmci_def_pdata;
1268 }
1269
Thomas Kleffelbe518012008-06-30 22:40:24 +01001270 spin_lock_init(&host->complete_lock);
1271 tasklet_init(&host->pio_tasklet, pio_tasklet, (unsigned long) host);
1272
1273 if (is2440) {
1274 host->sdiimsk = S3C2440_SDIIMSK;
1275 host->sdidata = S3C2440_SDIDATA;
1276 host->clk_div = 1;
1277 } else {
1278 host->sdiimsk = S3C2410_SDIIMSK;
1279 host->sdidata = S3C2410_SDIDATA;
1280 host->clk_div = 2;
1281 }
1282
1283 host->dodma = 0;
1284 host->complete_what = COMPLETION_NONE;
1285 host->pio_active = XFER_NONE;
1286
1287 host->dma = S3CMCI_DMA;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001288
1289 host->mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1290 if (!host->mem) {
1291 dev_err(&pdev->dev,
1292 "failed to get io memory region resouce.\n");
1293
1294 ret = -ENOENT;
1295 goto probe_free_host;
1296 }
1297
1298 host->mem = request_mem_region(host->mem->start,
Ben Dooksda52a7c2009-10-01 15:44:13 -07001299 resource_size(host->mem), pdev->name);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001300
1301 if (!host->mem) {
1302 dev_err(&pdev->dev, "failed to request io memory region.\n");
1303 ret = -ENOENT;
1304 goto probe_free_host;
1305 }
1306
Ben Dooksda52a7c2009-10-01 15:44:13 -07001307 host->base = ioremap(host->mem->start, resource_size(host->mem));
Ben Dooks5d304402008-08-08 10:55:41 +01001308 if (!host->base) {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001309 dev_err(&pdev->dev, "failed to ioremap() io memory region.\n");
1310 ret = -EINVAL;
1311 goto probe_free_mem_region;
1312 }
1313
1314 host->irq = platform_get_irq(pdev, 0);
1315 if (host->irq == 0) {
1316 dev_err(&pdev->dev, "failed to get interrupt resouce.\n");
1317 ret = -EINVAL;
1318 goto probe_iounmap;
1319 }
1320
1321 if (request_irq(host->irq, s3cmci_irq, 0, DRIVER_NAME, host)) {
1322 dev_err(&pdev->dev, "failed to request mci interrupt.\n");
1323 ret = -ENOENT;
1324 goto probe_iounmap;
1325 }
1326
1327 /* We get spurious interrupts even when we have set the IMSK
1328 * register to ignore everything, so use disable_irq() to make
1329 * ensure we don't lock the system with un-serviceable requests. */
1330
1331 disable_irq(host->irq);
1332
Ben Dooks55d70f52008-06-30 22:40:32 +01001333 host->irq_cd = s3c2410_gpio_getirq(host->pdata->gpio_detect);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001334
Ben Dooks55d70f52008-06-30 22:40:32 +01001335 if (host->irq_cd >= 0) {
1336 if (request_irq(host->irq_cd, s3cmci_irq_cd,
1337 IRQF_TRIGGER_RISING | IRQF_TRIGGER_FALLING,
1338 DRIVER_NAME, host)) {
1339 dev_err(&pdev->dev, "can't get card detect irq.\n");
1340 ret = -ENOENT;
1341 goto probe_free_irq;
1342 }
1343 } else {
1344 dev_warn(&pdev->dev, "host detect has no irq available\n");
1345 s3c2410_gpio_cfgpin(host->pdata->gpio_detect,
1346 S3C2410_GPIO_INPUT);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001347 }
1348
Ben Dooksedb5a982008-06-30 22:40:29 +01001349 if (host->pdata->gpio_wprotect)
1350 s3c2410_gpio_cfgpin(host->pdata->gpio_wprotect,
1351 S3C2410_GPIO_INPUT);
1352
Ben Dooks3886ff52008-06-30 22:40:33 +01001353 if (s3c2410_dma_request(S3CMCI_DMA, &s3cmci_dma_client, NULL) < 0) {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001354 dev_err(&pdev->dev, "unable to get DMA channel.\n");
1355 ret = -EBUSY;
1356 goto probe_free_irq_cd;
1357 }
1358
1359 host->clk = clk_get(&pdev->dev, "sdi");
1360 if (IS_ERR(host->clk)) {
1361 dev_err(&pdev->dev, "failed to find clock source.\n");
1362 ret = PTR_ERR(host->clk);
1363 host->clk = NULL;
1364 goto probe_free_host;
1365 }
1366
1367 ret = clk_enable(host->clk);
1368 if (ret) {
1369 dev_err(&pdev->dev, "failed to enable clock source.\n");
1370 goto clk_free;
1371 }
1372
1373 host->clk_rate = clk_get_rate(host->clk);
1374
1375 mmc->ops = &s3cmci_ops;
Ben Dooksedb5a982008-06-30 22:40:29 +01001376 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001377 mmc->caps = MMC_CAP_4_BIT_DATA;
1378 mmc->f_min = host->clk_rate / (host->clk_div * 256);
1379 mmc->f_max = host->clk_rate / host->clk_div;
1380
Ben Dooksedb5a982008-06-30 22:40:29 +01001381 if (host->pdata->ocr_avail)
1382 mmc->ocr_avail = host->pdata->ocr_avail;
1383
Thomas Kleffelbe518012008-06-30 22:40:24 +01001384 mmc->max_blk_count = 4095;
1385 mmc->max_blk_size = 4095;
1386 mmc->max_req_size = 4095 * 512;
1387 mmc->max_seg_size = mmc->max_req_size;
1388
1389 mmc->max_phys_segs = 128;
1390 mmc->max_hw_segs = 128;
1391
1392 dbg(host, dbg_debug,
1393 "probe: mode:%s mapped mci_base:%p irq:%u irq_cd:%u dma:%u.\n",
1394 (host->is2440?"2440":""),
1395 host->base, host->irq, host->irq_cd, host->dma);
1396
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001397 ret = s3cmci_cpufreq_register(host);
1398 if (ret) {
1399 dev_err(&pdev->dev, "failed to register cpufreq\n");
1400 goto free_dmabuf;
1401 }
1402
Thomas Kleffelbe518012008-06-30 22:40:24 +01001403 ret = mmc_add_host(mmc);
1404 if (ret) {
1405 dev_err(&pdev->dev, "failed to add mmc host.\n");
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001406 goto free_cpufreq;
Thomas Kleffelbe518012008-06-30 22:40:24 +01001407 }
1408
1409 platform_set_drvdata(pdev, mmc);
1410 dev_info(&pdev->dev, "initialisation done.\n");
1411
1412 return 0;
1413
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001414 free_cpufreq:
1415 s3cmci_cpufreq_deregister(host);
1416
Thomas Kleffelbe518012008-06-30 22:40:24 +01001417 free_dmabuf:
1418 clk_disable(host->clk);
1419
1420 clk_free:
1421 clk_put(host->clk);
1422
1423 probe_free_irq_cd:
Ben Dooks55d70f52008-06-30 22:40:32 +01001424 if (host->irq_cd >= 0)
1425 free_irq(host->irq_cd, host);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001426
1427 probe_free_irq:
1428 free_irq(host->irq, host);
1429
1430 probe_iounmap:
1431 iounmap(host->base);
1432
1433 probe_free_mem_region:
Ben Dooksda52a7c2009-10-01 15:44:13 -07001434 release_mem_region(host->mem->start, resource_size(host->mem));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001435
1436 probe_free_host:
1437 mmc_free_host(mmc);
1438 probe_out:
1439 return ret;
1440}
1441
Ben Dooks907b2cd2008-07-17 15:32:54 +01001442static void s3cmci_shutdown(struct platform_device *pdev)
1443{
1444 struct mmc_host *mmc = platform_get_drvdata(pdev);
1445 struct s3cmci_host *host = mmc_priv(mmc);
1446
1447 if (host->irq_cd >= 0)
1448 free_irq(host->irq_cd, host);
1449
ben@fluff.org.ukf87e6d02008-10-15 00:17:16 +01001450 s3cmci_cpufreq_deregister(host);
Ben Dooks907b2cd2008-07-17 15:32:54 +01001451 mmc_remove_host(mmc);
1452 clk_disable(host->clk);
1453}
1454
Thomas Kleffelbe518012008-06-30 22:40:24 +01001455static int __devexit s3cmci_remove(struct platform_device *pdev)
1456{
1457 struct mmc_host *mmc = platform_get_drvdata(pdev);
1458 struct s3cmci_host *host = mmc_priv(mmc);
1459
Ben Dooks907b2cd2008-07-17 15:32:54 +01001460 s3cmci_shutdown(pdev);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001461
Thomas Kleffelbe518012008-06-30 22:40:24 +01001462 clk_put(host->clk);
1463
1464 tasklet_disable(&host->pio_tasklet);
Harald Welteceb3ac22008-06-30 22:40:26 +01001465 s3c2410_dma_free(S3CMCI_DMA, &s3cmci_dma_client);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001466
Thomas Kleffelbe518012008-06-30 22:40:24 +01001467 free_irq(host->irq, host);
1468
1469 iounmap(host->base);
Ben Dooksda52a7c2009-10-01 15:44:13 -07001470 release_mem_region(host->mem->start, resource_size(host->mem));
Thomas Kleffelbe518012008-06-30 22:40:24 +01001471
1472 mmc_free_host(mmc);
1473 return 0;
1474}
1475
Ben Dooksd2f27612008-07-17 11:54:01 +01001476static int __devinit s3cmci_2410_probe(struct platform_device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001477{
1478 return s3cmci_probe(dev, 0);
1479}
1480
Ben Dooksd2f27612008-07-17 11:54:01 +01001481static int __devinit s3cmci_2412_probe(struct platform_device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001482{
1483 return s3cmci_probe(dev, 1);
1484}
1485
Ben Dooksd2f27612008-07-17 11:54:01 +01001486static int __devinit s3cmci_2440_probe(struct platform_device *dev)
Thomas Kleffelbe518012008-06-30 22:40:24 +01001487{
1488 return s3cmci_probe(dev, 1);
1489}
1490
1491#ifdef CONFIG_PM
1492
1493static int s3cmci_suspend(struct platform_device *dev, pm_message_t state)
1494{
1495 struct mmc_host *mmc = platform_get_drvdata(dev);
1496
1497 return mmc_suspend_host(mmc, state);
1498}
1499
1500static int s3cmci_resume(struct platform_device *dev)
1501{
1502 struct mmc_host *mmc = platform_get_drvdata(dev);
1503
1504 return mmc_resume_host(mmc);
1505}
1506
1507#else /* CONFIG_PM */
1508#define s3cmci_suspend NULL
1509#define s3cmci_resume NULL
1510#endif /* CONFIG_PM */
1511
1512
Ben Dooksd2f27612008-07-17 11:54:01 +01001513static struct platform_driver s3cmci_2410_driver = {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001514 .driver.name = "s3c2410-sdi",
1515 .driver.owner = THIS_MODULE,
Ben Dooksd2f27612008-07-17 11:54:01 +01001516 .probe = s3cmci_2410_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001517 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001518 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001519 .suspend = s3cmci_suspend,
1520 .resume = s3cmci_resume,
1521};
1522
Ben Dooksd2f27612008-07-17 11:54:01 +01001523static struct platform_driver s3cmci_2412_driver = {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001524 .driver.name = "s3c2412-sdi",
1525 .driver.owner = THIS_MODULE,
Ben Dooksd2f27612008-07-17 11:54:01 +01001526 .probe = s3cmci_2412_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001527 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001528 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001529 .suspend = s3cmci_suspend,
1530 .resume = s3cmci_resume,
1531};
1532
Ben Dooksd2f27612008-07-17 11:54:01 +01001533static struct platform_driver s3cmci_2440_driver = {
Thomas Kleffelbe518012008-06-30 22:40:24 +01001534 .driver.name = "s3c2440-sdi",
1535 .driver.owner = THIS_MODULE,
Ben Dooksd2f27612008-07-17 11:54:01 +01001536 .probe = s3cmci_2440_probe,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001537 .remove = __devexit_p(s3cmci_remove),
Ben Dooks907b2cd2008-07-17 15:32:54 +01001538 .shutdown = s3cmci_shutdown,
Thomas Kleffelbe518012008-06-30 22:40:24 +01001539 .suspend = s3cmci_suspend,
1540 .resume = s3cmci_resume,
1541};
1542
1543
1544static int __init s3cmci_init(void)
1545{
Ben Dooksd2f27612008-07-17 11:54:01 +01001546 platform_driver_register(&s3cmci_2410_driver);
1547 platform_driver_register(&s3cmci_2412_driver);
1548 platform_driver_register(&s3cmci_2440_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001549 return 0;
1550}
1551
1552static void __exit s3cmci_exit(void)
1553{
Ben Dooksd2f27612008-07-17 11:54:01 +01001554 platform_driver_unregister(&s3cmci_2410_driver);
1555 platform_driver_unregister(&s3cmci_2412_driver);
1556 platform_driver_unregister(&s3cmci_2440_driver);
Thomas Kleffelbe518012008-06-30 22:40:24 +01001557}
1558
1559module_init(s3cmci_init);
1560module_exit(s3cmci_exit);
1561
1562MODULE_DESCRIPTION("Samsung S3C MMC/SD Card Interface driver");
1563MODULE_LICENSE("GPL v2");
ben@fluff.org.uk08c55e22008-10-15 00:17:19 +01001564MODULE_AUTHOR("Thomas Kleffel <tk@maintech.de>, Ben Dooks <ben-linux@fluff.org>");
Ben Dooks318f9052008-06-30 22:40:34 +01001565MODULE_ALIAS("platform:s3c2410-sdi");
1566MODULE_ALIAS("platform:s3c2412-sdi");
1567MODULE_ALIAS("platform:s3c2440-sdi");