blob: becca914507020d412bd164dcf0286124a014782 [file] [log] [blame]
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001/*
2 * Atmel MultiMedia Card Interface driver
3 *
4 * Copyright (C) 2004-2008 Atmel Corporation
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#include <linux/blkdev.h>
11#include <linux/clk.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020012#include <linux/debugfs.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020013#include <linux/device.h>
Ben Nizettefbfca4b2008-07-18 16:48:09 +100014#include <linux/err.h>
David Brownell3c26e172008-07-27 02:34:45 -070015#include <linux/gpio.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020016#include <linux/init.h>
17#include <linux/interrupt.h>
18#include <linux/ioport.h>
19#include <linux/module.h>
20#include <linux/platform_device.h>
21#include <linux/scatterlist.h>
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020022#include <linux/seq_file.h>
23#include <linux/stat.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020024
25#include <linux/mmc/host.h>
26
27#include <asm/atmel-mci.h>
28#include <asm/io.h>
29#include <asm/unaligned.h>
30
Haavard Skinnemoen3663b732008-08-05 13:57:38 +020031#include <mach/board.h>
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +020032
33#include "atmel-mci-regs.h"
34
35#define ATMCI_DATA_ERROR_FLAGS (MCI_DCRCE | MCI_DTOE | MCI_OVRE | MCI_UNRE)
36
37enum {
38 EVENT_CMD_COMPLETE = 0,
39 EVENT_DATA_ERROR,
40 EVENT_DATA_COMPLETE,
41 EVENT_STOP_SENT,
42 EVENT_STOP_COMPLETE,
43 EVENT_XFER_COMPLETE,
44};
45
46struct atmel_mci {
47 struct mmc_host *mmc;
48 void __iomem *regs;
49
50 struct scatterlist *sg;
51 unsigned int pio_offset;
52
53 struct mmc_request *mrq;
54 struct mmc_command *cmd;
55 struct mmc_data *data;
56
57 u32 cmd_status;
58 u32 data_status;
59 u32 stop_status;
60 u32 stop_cmdr;
61
62 u32 mode_reg;
63 u32 sdc_reg;
64
65 struct tasklet_struct tasklet;
66 unsigned long pending_events;
67 unsigned long completed_events;
68
69 int present;
70 int detect_pin;
71 int wp_pin;
72
73 /* For detect pin debouncing */
74 struct timer_list detect_timer;
75
76 unsigned long bus_hz;
77 unsigned long mapbase;
78 struct clk *mck;
79 struct platform_device *pdev;
80};
81
82#define atmci_is_completed(host, event) \
83 test_bit(event, &host->completed_events)
84#define atmci_test_and_clear_pending(host, event) \
85 test_and_clear_bit(event, &host->pending_events)
86#define atmci_test_and_set_completed(host, event) \
87 test_and_set_bit(event, &host->completed_events)
88#define atmci_set_completed(host, event) \
89 set_bit(event, &host->completed_events)
90#define atmci_set_pending(host, event) \
91 set_bit(event, &host->pending_events)
92#define atmci_clear_pending(host, event) \
93 clear_bit(event, &host->pending_events)
94
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +020095/*
96 * The debugfs stuff below is mostly optimized away when
97 * CONFIG_DEBUG_FS is not set.
98 */
99static int atmci_req_show(struct seq_file *s, void *v)
100{
101 struct atmel_mci *host = s->private;
102 struct mmc_request *mrq = host->mrq;
103 struct mmc_command *cmd;
104 struct mmc_command *stop;
105 struct mmc_data *data;
106
107 /* Make sure we get a consistent snapshot */
108 spin_lock_irq(&host->mmc->lock);
109
110 if (mrq) {
111 cmd = mrq->cmd;
112 data = mrq->data;
113 stop = mrq->stop;
114
115 if (cmd)
116 seq_printf(s,
117 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
118 cmd->opcode, cmd->arg, cmd->flags,
119 cmd->resp[0], cmd->resp[1], cmd->resp[2],
120 cmd->resp[2], cmd->error);
121 if (data)
122 seq_printf(s, "DATA %u / %u * %u flg %x err %d\n",
123 data->bytes_xfered, data->blocks,
124 data->blksz, data->flags, data->error);
125 if (stop)
126 seq_printf(s,
127 "CMD%u(0x%x) flg %x rsp %x %x %x %x err %d\n",
128 stop->opcode, stop->arg, stop->flags,
129 stop->resp[0], stop->resp[1], stop->resp[2],
130 stop->resp[2], stop->error);
131 }
132
133 spin_unlock_irq(&host->mmc->lock);
134
135 return 0;
136}
137
138static int atmci_req_open(struct inode *inode, struct file *file)
139{
140 return single_open(file, atmci_req_show, inode->i_private);
141}
142
143static const struct file_operations atmci_req_fops = {
144 .owner = THIS_MODULE,
145 .open = atmci_req_open,
146 .read = seq_read,
147 .llseek = seq_lseek,
148 .release = single_release,
149};
150
151static void atmci_show_status_reg(struct seq_file *s,
152 const char *regname, u32 value)
153{
154 static const char *sr_bit[] = {
155 [0] = "CMDRDY",
156 [1] = "RXRDY",
157 [2] = "TXRDY",
158 [3] = "BLKE",
159 [4] = "DTIP",
160 [5] = "NOTBUSY",
161 [8] = "SDIOIRQA",
162 [9] = "SDIOIRQB",
163 [16] = "RINDE",
164 [17] = "RDIRE",
165 [18] = "RCRCE",
166 [19] = "RENDE",
167 [20] = "RTOE",
168 [21] = "DCRCE",
169 [22] = "DTOE",
170 [30] = "OVRE",
171 [31] = "UNRE",
172 };
173 unsigned int i;
174
175 seq_printf(s, "%s:\t0x%08x", regname, value);
176 for (i = 0; i < ARRAY_SIZE(sr_bit); i++) {
177 if (value & (1 << i)) {
178 if (sr_bit[i])
179 seq_printf(s, " %s", sr_bit[i]);
180 else
181 seq_puts(s, " UNKNOWN");
182 }
183 }
184 seq_putc(s, '\n');
185}
186
187static int atmci_regs_show(struct seq_file *s, void *v)
188{
189 struct atmel_mci *host = s->private;
190 u32 *buf;
191
192 buf = kmalloc(MCI_REGS_SIZE, GFP_KERNEL);
193 if (!buf)
194 return -ENOMEM;
195
196 /* Grab a more or less consistent snapshot */
197 spin_lock_irq(&host->mmc->lock);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200198 clk_enable(host->mck);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200199 memcpy_fromio(buf, host->regs, MCI_REGS_SIZE);
Haavard Skinnemoen87e60f22008-09-19 21:09:27 +0200200 clk_disable(host->mck);
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200201 spin_unlock_irq(&host->mmc->lock);
202
203 seq_printf(s, "MR:\t0x%08x%s%s CLKDIV=%u\n",
204 buf[MCI_MR / 4],
205 buf[MCI_MR / 4] & MCI_MR_RDPROOF ? " RDPROOF" : "",
206 buf[MCI_MR / 4] & MCI_MR_WRPROOF ? " WRPROOF" : "",
207 buf[MCI_MR / 4] & 0xff);
208 seq_printf(s, "DTOR:\t0x%08x\n", buf[MCI_DTOR / 4]);
209 seq_printf(s, "SDCR:\t0x%08x\n", buf[MCI_SDCR / 4]);
210 seq_printf(s, "ARGR:\t0x%08x\n", buf[MCI_ARGR / 4]);
211 seq_printf(s, "BLKR:\t0x%08x BCNT=%u BLKLEN=%u\n",
212 buf[MCI_BLKR / 4],
213 buf[MCI_BLKR / 4] & 0xffff,
214 (buf[MCI_BLKR / 4] >> 16) & 0xffff);
215
216 /* Don't read RSPR and RDR; it will consume the data there */
217
218 atmci_show_status_reg(s, "SR", buf[MCI_SR / 4]);
219 atmci_show_status_reg(s, "IMR", buf[MCI_IMR / 4]);
220
Haavard Skinnemoenb17339a2008-09-19 21:09:28 +0200221 kfree(buf);
222
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +0200223 return 0;
224}
225
226static int atmci_regs_open(struct inode *inode, struct file *file)
227{
228 return single_open(file, atmci_regs_show, inode->i_private);
229}
230
231static const struct file_operations atmci_regs_fops = {
232 .owner = THIS_MODULE,
233 .open = atmci_regs_open,
234 .read = seq_read,
235 .llseek = seq_lseek,
236 .release = single_release,
237};
238
239static void atmci_init_debugfs(struct atmel_mci *host)
240{
241 struct mmc_host *mmc;
242 struct dentry *root;
243 struct dentry *node;
244 struct resource *res;
245
246 mmc = host->mmc;
247 root = mmc->debugfs_root;
248 if (!root)
249 return;
250
251 node = debugfs_create_file("regs", S_IRUSR, root, host,
252 &atmci_regs_fops);
253 if (IS_ERR(node))
254 return;
255 if (!node)
256 goto err;
257
258 res = platform_get_resource(host->pdev, IORESOURCE_MEM, 0);
259 node->d_inode->i_size = res->end - res->start + 1;
260
261 node = debugfs_create_file("req", S_IRUSR, root, host, &atmci_req_fops);
262 if (!node)
263 goto err;
264
265 node = debugfs_create_x32("pending_events", S_IRUSR, root,
266 (u32 *)&host->pending_events);
267 if (!node)
268 goto err;
269
270 node = debugfs_create_x32("completed_events", S_IRUSR, root,
271 (u32 *)&host->completed_events);
272 if (!node)
273 goto err;
274
275 return;
276
277err:
278 dev_err(&host->pdev->dev,
279 "failed to initialize debugfs for controller\n");
280}
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200281
282static void atmci_enable(struct atmel_mci *host)
283{
284 clk_enable(host->mck);
285 mci_writel(host, CR, MCI_CR_MCIEN);
286 mci_writel(host, MR, host->mode_reg);
287 mci_writel(host, SDCR, host->sdc_reg);
288}
289
290static void atmci_disable(struct atmel_mci *host)
291{
292 mci_writel(host, CR, MCI_CR_SWRST);
293
294 /* Stall until write is complete, then disable the bus clock */
295 mci_readl(host, SR);
296 clk_disable(host->mck);
297}
298
299static inline unsigned int ns_to_clocks(struct atmel_mci *host,
300 unsigned int ns)
301{
302 return (ns * (host->bus_hz / 1000000) + 999) / 1000;
303}
304
305static void atmci_set_timeout(struct atmel_mci *host,
306 struct mmc_data *data)
307{
308 static unsigned dtomul_to_shift[] = {
309 0, 4, 7, 8, 10, 12, 16, 20
310 };
311 unsigned timeout;
312 unsigned dtocyc;
313 unsigned dtomul;
314
315 timeout = ns_to_clocks(host, data->timeout_ns) + data->timeout_clks;
316
317 for (dtomul = 0; dtomul < 8; dtomul++) {
318 unsigned shift = dtomul_to_shift[dtomul];
319 dtocyc = (timeout + (1 << shift) - 1) >> shift;
320 if (dtocyc < 15)
321 break;
322 }
323
324 if (dtomul >= 8) {
325 dtomul = 7;
326 dtocyc = 15;
327 }
328
329 dev_vdbg(&host->mmc->class_dev, "setting timeout to %u cycles\n",
330 dtocyc << dtomul_to_shift[dtomul]);
331 mci_writel(host, DTOR, (MCI_DTOMUL(dtomul) | MCI_DTOCYC(dtocyc)));
332}
333
334/*
335 * Return mask with command flags to be enabled for this command.
336 */
337static u32 atmci_prepare_command(struct mmc_host *mmc,
338 struct mmc_command *cmd)
339{
340 struct mmc_data *data;
341 u32 cmdr;
342
343 cmd->error = -EINPROGRESS;
344
345 cmdr = MCI_CMDR_CMDNB(cmd->opcode);
346
347 if (cmd->flags & MMC_RSP_PRESENT) {
348 if (cmd->flags & MMC_RSP_136)
349 cmdr |= MCI_CMDR_RSPTYP_136BIT;
350 else
351 cmdr |= MCI_CMDR_RSPTYP_48BIT;
352 }
353
354 /*
355 * This should really be MAXLAT_5 for CMD2 and ACMD41, but
356 * it's too difficult to determine whether this is an ACMD or
357 * not. Better make it 64.
358 */
359 cmdr |= MCI_CMDR_MAXLAT_64CYC;
360
361 if (mmc->ios.bus_mode == MMC_BUSMODE_OPENDRAIN)
362 cmdr |= MCI_CMDR_OPDCMD;
363
364 data = cmd->data;
365 if (data) {
366 cmdr |= MCI_CMDR_START_XFER;
367 if (data->flags & MMC_DATA_STREAM)
368 cmdr |= MCI_CMDR_STREAM;
369 else if (data->blocks > 1)
370 cmdr |= MCI_CMDR_MULTI_BLOCK;
371 else
372 cmdr |= MCI_CMDR_BLOCK;
373
374 if (data->flags & MMC_DATA_READ)
375 cmdr |= MCI_CMDR_TRDIR_READ;
376 }
377
378 return cmdr;
379}
380
381static void atmci_start_command(struct atmel_mci *host,
382 struct mmc_command *cmd,
383 u32 cmd_flags)
384{
385 /* Must read host->cmd after testing event flags */
386 smp_rmb();
387 WARN_ON(host->cmd);
388 host->cmd = cmd;
389
390 dev_vdbg(&host->mmc->class_dev,
391 "start command: ARGR=0x%08x CMDR=0x%08x\n",
392 cmd->arg, cmd_flags);
393
394 mci_writel(host, ARGR, cmd->arg);
395 mci_writel(host, CMDR, cmd_flags);
396}
397
398static void send_stop_cmd(struct mmc_host *mmc, struct mmc_data *data)
399{
400 struct atmel_mci *host = mmc_priv(mmc);
401
402 atmci_start_command(host, data->stop, host->stop_cmdr);
403 mci_writel(host, IER, MCI_CMDRDY);
404}
405
406static void atmci_request_end(struct mmc_host *mmc, struct mmc_request *mrq)
407{
408 struct atmel_mci *host = mmc_priv(mmc);
409
410 WARN_ON(host->cmd || host->data);
411 host->mrq = NULL;
412
413 atmci_disable(host);
414
415 mmc_request_done(mmc, mrq);
416}
417
418/*
419 * Returns a mask of interrupt flags to be enabled after the whole
420 * request has been prepared.
421 */
422static u32 atmci_submit_data(struct mmc_host *mmc, struct mmc_data *data)
423{
424 struct atmel_mci *host = mmc_priv(mmc);
425 u32 iflags;
426
427 data->error = -EINPROGRESS;
428
429 WARN_ON(host->data);
430 host->sg = NULL;
431 host->data = data;
432
433 mci_writel(host, BLKR, MCI_BCNT(data->blocks)
434 | MCI_BLKLEN(data->blksz));
435 dev_vdbg(&mmc->class_dev, "BLKR=0x%08x\n",
436 MCI_BCNT(data->blocks) | MCI_BLKLEN(data->blksz));
437
438 iflags = ATMCI_DATA_ERROR_FLAGS;
439 host->sg = data->sg;
440 host->pio_offset = 0;
441 if (data->flags & MMC_DATA_READ)
442 iflags |= MCI_RXRDY;
443 else
444 iflags |= MCI_TXRDY;
445
446 return iflags;
447}
448
449static void atmci_request(struct mmc_host *mmc, struct mmc_request *mrq)
450{
451 struct atmel_mci *host = mmc_priv(mmc);
452 struct mmc_data *data;
453 struct mmc_command *cmd;
454 u32 iflags;
455 u32 cmdflags = 0;
456
457 iflags = mci_readl(host, IMR);
458 if (iflags)
459 dev_warn(&mmc->class_dev, "WARNING: IMR=0x%08x\n",
460 mci_readl(host, IMR));
461
462 WARN_ON(host->mrq != NULL);
463
464 /*
465 * We may "know" the card is gone even though there's still an
466 * electrical connection. If so, we really need to communicate
467 * this to the MMC core since there won't be any more
468 * interrupts as the card is completely removed. Otherwise,
469 * the MMC core might believe the card is still there even
470 * though the card was just removed very slowly.
471 */
472 if (!host->present) {
473 mrq->cmd->error = -ENOMEDIUM;
474 mmc_request_done(mmc, mrq);
475 return;
476 }
477
478 host->mrq = mrq;
479 host->pending_events = 0;
480 host->completed_events = 0;
481
482 atmci_enable(host);
483
484 /* We don't support multiple blocks of weird lengths. */
485 data = mrq->data;
486 if (data) {
487 if (data->blocks > 1 && data->blksz & 3)
488 goto fail;
489 atmci_set_timeout(host, data);
490 }
491
492 iflags = MCI_CMDRDY;
493 cmd = mrq->cmd;
494 cmdflags = atmci_prepare_command(mmc, cmd);
495 atmci_start_command(host, cmd, cmdflags);
496
497 if (data)
498 iflags |= atmci_submit_data(mmc, data);
499
500 if (mrq->stop) {
501 host->stop_cmdr = atmci_prepare_command(mmc, mrq->stop);
502 host->stop_cmdr |= MCI_CMDR_STOP_XFER;
503 if (!(data->flags & MMC_DATA_WRITE))
504 host->stop_cmdr |= MCI_CMDR_TRDIR_READ;
505 if (data->flags & MMC_DATA_STREAM)
506 host->stop_cmdr |= MCI_CMDR_STREAM;
507 else
508 host->stop_cmdr |= MCI_CMDR_MULTI_BLOCK;
509 }
510
511 /*
512 * We could have enabled interrupts earlier, but I suspect
513 * that would open up a nice can of interesting race
514 * conditions (e.g. command and data complete, but stop not
515 * prepared yet.)
516 */
517 mci_writel(host, IER, iflags);
518
519 return;
520
521fail:
522 atmci_disable(host);
523 host->mrq = NULL;
524 mrq->cmd->error = -EINVAL;
525 mmc_request_done(mmc, mrq);
526}
527
528static void atmci_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
529{
530 struct atmel_mci *host = mmc_priv(mmc);
531
532 if (ios->clock) {
533 u32 clkdiv;
534
535 /* Set clock rate */
536 clkdiv = DIV_ROUND_UP(host->bus_hz, 2 * ios->clock) - 1;
537 if (clkdiv > 255) {
538 dev_warn(&mmc->class_dev,
539 "clock %u too slow; using %lu\n",
540 ios->clock, host->bus_hz / (2 * 256));
541 clkdiv = 255;
542 }
543
544 host->mode_reg = MCI_MR_CLKDIV(clkdiv) | MCI_MR_WRPROOF
545 | MCI_MR_RDPROOF;
546 }
547
548 switch (ios->bus_width) {
549 case MMC_BUS_WIDTH_1:
550 host->sdc_reg = 0;
551 break;
552 case MMC_BUS_WIDTH_4:
553 host->sdc_reg = MCI_SDCBUS_4BIT;
554 break;
555 }
556
557 switch (ios->power_mode) {
558 case MMC_POWER_ON:
559 /* Send init sequence (74 clock cycles) */
560 atmci_enable(host);
561 mci_writel(host, CMDR, MCI_CMDR_SPCMD_INIT);
562 while (!(mci_readl(host, SR) & MCI_CMDRDY))
563 cpu_relax();
564 atmci_disable(host);
565 break;
566 default:
567 /*
568 * TODO: None of the currently available AVR32-based
569 * boards allow MMC power to be turned off. Implement
570 * power control when this can be tested properly.
571 */
572 break;
573 }
574}
575
576static int atmci_get_ro(struct mmc_host *mmc)
577{
578 int read_only = 0;
579 struct atmel_mci *host = mmc_priv(mmc);
580
David Brownell3c26e172008-07-27 02:34:45 -0700581 if (gpio_is_valid(host->wp_pin)) {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200582 read_only = gpio_get_value(host->wp_pin);
583 dev_dbg(&mmc->class_dev, "card is %s\n",
584 read_only ? "read-only" : "read-write");
585 } else {
586 dev_dbg(&mmc->class_dev,
587 "no pin for checking read-only switch."
588 " Assuming write-enable.\n");
589 }
590
591 return read_only;
592}
593
594static struct mmc_host_ops atmci_ops = {
595 .request = atmci_request,
596 .set_ios = atmci_set_ios,
597 .get_ro = atmci_get_ro,
598};
599
600static void atmci_command_complete(struct atmel_mci *host,
601 struct mmc_command *cmd, u32 status)
602{
603 /* Read the response from the card (up to 16 bytes) */
604 cmd->resp[0] = mci_readl(host, RSPR);
605 cmd->resp[1] = mci_readl(host, RSPR);
606 cmd->resp[2] = mci_readl(host, RSPR);
607 cmd->resp[3] = mci_readl(host, RSPR);
608
609 if (status & MCI_RTOE)
610 cmd->error = -ETIMEDOUT;
611 else if ((cmd->flags & MMC_RSP_CRC) && (status & MCI_RCRCE))
612 cmd->error = -EILSEQ;
613 else if (status & (MCI_RINDE | MCI_RDIRE | MCI_RENDE))
614 cmd->error = -EIO;
615 else
616 cmd->error = 0;
617
618 if (cmd->error) {
619 dev_dbg(&host->mmc->class_dev,
620 "command error: status=0x%08x\n", status);
621
622 if (cmd->data) {
623 host->data = NULL;
624 mci_writel(host, IDR, MCI_NOTBUSY
625 | MCI_TXRDY | MCI_RXRDY
626 | ATMCI_DATA_ERROR_FLAGS);
627 }
628 }
629}
630
631static void atmci_detect_change(unsigned long data)
632{
633 struct atmel_mci *host = (struct atmel_mci *)data;
634 struct mmc_request *mrq = host->mrq;
635 int present;
636
637 /*
638 * atmci_remove() sets detect_pin to -1 before freeing the
639 * interrupt. We must not re-enable the interrupt if it has
640 * been freed.
641 */
642 smp_rmb();
David Brownell3c26e172008-07-27 02:34:45 -0700643 if (!gpio_is_valid(host->detect_pin))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +0200644 return;
645
646 enable_irq(gpio_to_irq(host->detect_pin));
647 present = !gpio_get_value(host->detect_pin);
648
649 dev_vdbg(&host->pdev->dev, "detect change: %d (was %d)\n",
650 present, host->present);
651
652 if (present != host->present) {
653 dev_dbg(&host->mmc->class_dev, "card %s\n",
654 present ? "inserted" : "removed");
655 host->present = present;
656
657 /* Reset controller if card is gone */
658 if (!present) {
659 mci_writel(host, CR, MCI_CR_SWRST);
660 mci_writel(host, IDR, ~0UL);
661 mci_writel(host, CR, MCI_CR_MCIEN);
662 }
663
664 /* Clean up queue if present */
665 if (mrq) {
666 /*
667 * Reset controller to terminate any ongoing
668 * commands or data transfers.
669 */
670 mci_writel(host, CR, MCI_CR_SWRST);
671
672 if (!atmci_is_completed(host, EVENT_CMD_COMPLETE))
673 mrq->cmd->error = -ENOMEDIUM;
674
675 if (mrq->data && !atmci_is_completed(host,
676 EVENT_DATA_COMPLETE)) {
677 host->data = NULL;
678 mrq->data->error = -ENOMEDIUM;
679 }
680 if (mrq->stop && !atmci_is_completed(host,
681 EVENT_STOP_COMPLETE))
682 mrq->stop->error = -ENOMEDIUM;
683
684 host->cmd = NULL;
685 atmci_request_end(host->mmc, mrq);
686 }
687
688 mmc_detect_change(host->mmc, 0);
689 }
690}
691
692static void atmci_tasklet_func(unsigned long priv)
693{
694 struct mmc_host *mmc = (struct mmc_host *)priv;
695 struct atmel_mci *host = mmc_priv(mmc);
696 struct mmc_request *mrq = host->mrq;
697 struct mmc_data *data = host->data;
698
699 dev_vdbg(&mmc->class_dev,
700 "tasklet: pending/completed/mask %lx/%lx/%x\n",
701 host->pending_events, host->completed_events,
702 mci_readl(host, IMR));
703
704 if (atmci_test_and_clear_pending(host, EVENT_CMD_COMPLETE)) {
705 /*
706 * host->cmd must be set to NULL before the interrupt
707 * handler sees EVENT_CMD_COMPLETE
708 */
709 host->cmd = NULL;
710 smp_wmb();
711 atmci_set_completed(host, EVENT_CMD_COMPLETE);
712 atmci_command_complete(host, mrq->cmd, host->cmd_status);
713
714 if (!mrq->cmd->error && mrq->stop
715 && atmci_is_completed(host, EVENT_XFER_COMPLETE)
716 && !atmci_test_and_set_completed(host,
717 EVENT_STOP_SENT))
718 send_stop_cmd(host->mmc, mrq->data);
719 }
720 if (atmci_test_and_clear_pending(host, EVENT_STOP_COMPLETE)) {
721 /*
722 * host->cmd must be set to NULL before the interrupt
723 * handler sees EVENT_STOP_COMPLETE
724 */
725 host->cmd = NULL;
726 smp_wmb();
727 atmci_set_completed(host, EVENT_STOP_COMPLETE);
728 atmci_command_complete(host, mrq->stop, host->stop_status);
729 }
730 if (atmci_test_and_clear_pending(host, EVENT_DATA_ERROR)) {
731 u32 status = host->data_status;
732
733 dev_vdbg(&mmc->class_dev, "data error: status=%08x\n", status);
734
735 atmci_set_completed(host, EVENT_DATA_ERROR);
736 atmci_set_completed(host, EVENT_DATA_COMPLETE);
737
738 if (status & MCI_DTOE) {
739 dev_dbg(&mmc->class_dev,
740 "data timeout error\n");
741 data->error = -ETIMEDOUT;
742 } else if (status & MCI_DCRCE) {
743 dev_dbg(&mmc->class_dev, "data CRC error\n");
744 data->error = -EILSEQ;
745 } else {
746 dev_dbg(&mmc->class_dev,
747 "data FIFO error (status=%08x)\n",
748 status);
749 data->error = -EIO;
750 }
751
752 if (host->present && data->stop
753 && atmci_is_completed(host, EVENT_CMD_COMPLETE)
754 && !atmci_test_and_set_completed(
755 host, EVENT_STOP_SENT))
756 send_stop_cmd(host->mmc, data);
757
758 host->data = NULL;
759 }
760 if (atmci_test_and_clear_pending(host, EVENT_DATA_COMPLETE)) {
761 atmci_set_completed(host, EVENT_DATA_COMPLETE);
762
763 if (!atmci_is_completed(host, EVENT_DATA_ERROR)) {
764 data->bytes_xfered = data->blocks * data->blksz;
765 data->error = 0;
766 }
767
768 host->data = NULL;
769 }
770
771 if (host->mrq && !host->cmd && !host->data)
772 atmci_request_end(mmc, host->mrq);
773}
774
775static void atmci_read_data_pio(struct atmel_mci *host)
776{
777 struct scatterlist *sg = host->sg;
778 void *buf = sg_virt(sg);
779 unsigned int offset = host->pio_offset;
780 struct mmc_data *data = host->data;
781 u32 value;
782 u32 status;
783 unsigned int nbytes = 0;
784
785 do {
786 value = mci_readl(host, RDR);
787 if (likely(offset + 4 <= sg->length)) {
788 put_unaligned(value, (u32 *)(buf + offset));
789
790 offset += 4;
791 nbytes += 4;
792
793 if (offset == sg->length) {
794 host->sg = sg = sg_next(sg);
795 if (!sg)
796 goto done;
797
798 offset = 0;
799 buf = sg_virt(sg);
800 }
801 } else {
802 unsigned int remaining = sg->length - offset;
803 memcpy(buf + offset, &value, remaining);
804 nbytes += remaining;
805
806 flush_dcache_page(sg_page(sg));
807 host->sg = sg = sg_next(sg);
808 if (!sg)
809 goto done;
810
811 offset = 4 - remaining;
812 buf = sg_virt(sg);
813 memcpy(buf, (u8 *)&value + remaining, offset);
814 nbytes += offset;
815 }
816
817 status = mci_readl(host, SR);
818 if (status & ATMCI_DATA_ERROR_FLAGS) {
819 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_RXRDY
820 | ATMCI_DATA_ERROR_FLAGS));
821 host->data_status = status;
822 atmci_set_pending(host, EVENT_DATA_ERROR);
823 tasklet_schedule(&host->tasklet);
824 break;
825 }
826 } while (status & MCI_RXRDY);
827
828 host->pio_offset = offset;
829 data->bytes_xfered += nbytes;
830
831 return;
832
833done:
834 mci_writel(host, IDR, MCI_RXRDY);
835 mci_writel(host, IER, MCI_NOTBUSY);
836 data->bytes_xfered += nbytes;
837 atmci_set_completed(host, EVENT_XFER_COMPLETE);
838 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
839 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
840 send_stop_cmd(host->mmc, data);
841}
842
843static void atmci_write_data_pio(struct atmel_mci *host)
844{
845 struct scatterlist *sg = host->sg;
846 void *buf = sg_virt(sg);
847 unsigned int offset = host->pio_offset;
848 struct mmc_data *data = host->data;
849 u32 value;
850 u32 status;
851 unsigned int nbytes = 0;
852
853 do {
854 if (likely(offset + 4 <= sg->length)) {
855 value = get_unaligned((u32 *)(buf + offset));
856 mci_writel(host, TDR, value);
857
858 offset += 4;
859 nbytes += 4;
860 if (offset == sg->length) {
861 host->sg = sg = sg_next(sg);
862 if (!sg)
863 goto done;
864
865 offset = 0;
866 buf = sg_virt(sg);
867 }
868 } else {
869 unsigned int remaining = sg->length - offset;
870
871 value = 0;
872 memcpy(&value, buf + offset, remaining);
873 nbytes += remaining;
874
875 host->sg = sg = sg_next(sg);
876 if (!sg) {
877 mci_writel(host, TDR, value);
878 goto done;
879 }
880
881 offset = 4 - remaining;
882 buf = sg_virt(sg);
883 memcpy((u8 *)&value + remaining, buf, offset);
884 mci_writel(host, TDR, value);
885 nbytes += offset;
886 }
887
888 status = mci_readl(host, SR);
889 if (status & ATMCI_DATA_ERROR_FLAGS) {
890 mci_writel(host, IDR, (MCI_NOTBUSY | MCI_TXRDY
891 | ATMCI_DATA_ERROR_FLAGS));
892 host->data_status = status;
893 atmci_set_pending(host, EVENT_DATA_ERROR);
894 tasklet_schedule(&host->tasklet);
895 break;
896 }
897 } while (status & MCI_TXRDY);
898
899 host->pio_offset = offset;
900 data->bytes_xfered += nbytes;
901
902 return;
903
904done:
905 mci_writel(host, IDR, MCI_TXRDY);
906 mci_writel(host, IER, MCI_NOTBUSY);
907 data->bytes_xfered += nbytes;
908 atmci_set_completed(host, EVENT_XFER_COMPLETE);
909 if (data->stop && atmci_is_completed(host, EVENT_CMD_COMPLETE)
910 && !atmci_test_and_set_completed(host, EVENT_STOP_SENT))
911 send_stop_cmd(host->mmc, data);
912}
913
914static void atmci_cmd_interrupt(struct mmc_host *mmc, u32 status)
915{
916 struct atmel_mci *host = mmc_priv(mmc);
917
918 mci_writel(host, IDR, MCI_CMDRDY);
919
920 if (atmci_is_completed(host, EVENT_STOP_SENT)) {
921 host->stop_status = status;
922 atmci_set_pending(host, EVENT_STOP_COMPLETE);
923 } else {
924 host->cmd_status = status;
925 atmci_set_pending(host, EVENT_CMD_COMPLETE);
926 }
927
928 tasklet_schedule(&host->tasklet);
929}
930
931static irqreturn_t atmci_interrupt(int irq, void *dev_id)
932{
933 struct mmc_host *mmc = dev_id;
934 struct atmel_mci *host = mmc_priv(mmc);
935 u32 status, mask, pending;
936 unsigned int pass_count = 0;
937
938 spin_lock(&mmc->lock);
939
940 do {
941 status = mci_readl(host, SR);
942 mask = mci_readl(host, IMR);
943 pending = status & mask;
944 if (!pending)
945 break;
946
947 if (pending & ATMCI_DATA_ERROR_FLAGS) {
948 mci_writel(host, IDR, ATMCI_DATA_ERROR_FLAGS
949 | MCI_RXRDY | MCI_TXRDY);
950 pending &= mci_readl(host, IMR);
951 host->data_status = status;
952 atmci_set_pending(host, EVENT_DATA_ERROR);
953 tasklet_schedule(&host->tasklet);
954 }
955 if (pending & MCI_NOTBUSY) {
956 mci_writel(host, IDR, (MCI_NOTBUSY
957 | ATMCI_DATA_ERROR_FLAGS));
958 atmci_set_pending(host, EVENT_DATA_COMPLETE);
959 tasklet_schedule(&host->tasklet);
960 }
961 if (pending & MCI_RXRDY)
962 atmci_read_data_pio(host);
963 if (pending & MCI_TXRDY)
964 atmci_write_data_pio(host);
965
966 if (pending & MCI_CMDRDY)
967 atmci_cmd_interrupt(mmc, status);
968 } while (pass_count++ < 5);
969
970 spin_unlock(&mmc->lock);
971
972 return pass_count ? IRQ_HANDLED : IRQ_NONE;
973}
974
975static irqreturn_t atmci_detect_interrupt(int irq, void *dev_id)
976{
977 struct mmc_host *mmc = dev_id;
978 struct atmel_mci *host = mmc_priv(mmc);
979
980 /*
981 * Disable interrupts until the pin has stabilized and check
982 * the state then. Use mod_timer() since we may be in the
983 * middle of the timer routine when this interrupt triggers.
984 */
985 disable_irq_nosync(irq);
986 mod_timer(&host->detect_timer, jiffies + msecs_to_jiffies(20));
987
988 return IRQ_HANDLED;
989}
990
991static int __init atmci_probe(struct platform_device *pdev)
992{
993 struct mci_platform_data *pdata;
994 struct atmel_mci *host;
995 struct mmc_host *mmc;
996 struct resource *regs;
997 int irq;
998 int ret;
999
1000 regs = platform_get_resource(pdev, IORESOURCE_MEM, 0);
1001 if (!regs)
1002 return -ENXIO;
1003 pdata = pdev->dev.platform_data;
1004 if (!pdata)
1005 return -ENXIO;
1006 irq = platform_get_irq(pdev, 0);
1007 if (irq < 0)
1008 return irq;
1009
1010 mmc = mmc_alloc_host(sizeof(struct atmel_mci), &pdev->dev);
1011 if (!mmc)
1012 return -ENOMEM;
1013
1014 host = mmc_priv(mmc);
1015 host->pdev = pdev;
1016 host->mmc = mmc;
1017 host->detect_pin = pdata->detect_pin;
1018 host->wp_pin = pdata->wp_pin;
1019
1020 host->mck = clk_get(&pdev->dev, "mci_clk");
1021 if (IS_ERR(host->mck)) {
1022 ret = PTR_ERR(host->mck);
1023 goto err_clk_get;
1024 }
1025
1026 ret = -ENOMEM;
1027 host->regs = ioremap(regs->start, regs->end - regs->start + 1);
1028 if (!host->regs)
1029 goto err_ioremap;
1030
1031 clk_enable(host->mck);
1032 mci_writel(host, CR, MCI_CR_SWRST);
1033 host->bus_hz = clk_get_rate(host->mck);
1034 clk_disable(host->mck);
1035
1036 host->mapbase = regs->start;
1037
1038 mmc->ops = &atmci_ops;
1039 mmc->f_min = (host->bus_hz + 511) / 512;
1040 mmc->f_max = host->bus_hz / 2;
1041 mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
Pierre Ossman23af6032008-07-06 01:10:27 +02001042 mmc->caps |= MMC_CAP_4_BIT_DATA;
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001043
1044 mmc->max_hw_segs = 64;
1045 mmc->max_phys_segs = 64;
1046 mmc->max_req_size = 32768 * 512;
1047 mmc->max_blk_size = 32768;
1048 mmc->max_blk_count = 512;
1049
1050 tasklet_init(&host->tasklet, atmci_tasklet_func, (unsigned long)mmc);
1051
1052 ret = request_irq(irq, atmci_interrupt, 0, pdev->dev.bus_id, mmc);
1053 if (ret)
1054 goto err_request_irq;
1055
1056 /* Assume card is present if we don't have a detect pin */
1057 host->present = 1;
David Brownell3c26e172008-07-27 02:34:45 -07001058 if (gpio_is_valid(host->detect_pin)) {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001059 if (gpio_request(host->detect_pin, "mmc_detect")) {
1060 dev_dbg(&mmc->class_dev, "no detect pin available\n");
1061 host->detect_pin = -1;
1062 } else {
1063 host->present = !gpio_get_value(host->detect_pin);
1064 }
1065 }
David Brownell3c26e172008-07-27 02:34:45 -07001066 if (gpio_is_valid(host->wp_pin)) {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001067 if (gpio_request(host->wp_pin, "mmc_wp")) {
1068 dev_dbg(&mmc->class_dev, "no WP pin available\n");
1069 host->wp_pin = -1;
1070 }
1071 }
1072
1073 platform_set_drvdata(pdev, host);
1074
1075 mmc_add_host(mmc);
1076
David Brownell3c26e172008-07-27 02:34:45 -07001077 if (gpio_is_valid(host->detect_pin)) {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001078 setup_timer(&host->detect_timer, atmci_detect_change,
1079 (unsigned long)host);
1080
1081 ret = request_irq(gpio_to_irq(host->detect_pin),
1082 atmci_detect_interrupt,
1083 IRQF_TRIGGER_FALLING | IRQF_TRIGGER_RISING,
1084 "mmc-detect", mmc);
1085 if (ret) {
1086 dev_dbg(&mmc->class_dev,
1087 "could not request IRQ %d for detect pin\n",
1088 gpio_to_irq(host->detect_pin));
1089 gpio_free(host->detect_pin);
1090 host->detect_pin = -1;
1091 }
1092 }
1093
1094 dev_info(&mmc->class_dev,
1095 "Atmel MCI controller at 0x%08lx irq %d\n",
1096 host->mapbase, irq);
1097
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +02001098 atmci_init_debugfs(host);
1099
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001100 return 0;
1101
1102err_request_irq:
1103 iounmap(host->regs);
1104err_ioremap:
1105 clk_put(host->mck);
1106err_clk_get:
1107 mmc_free_host(mmc);
1108 return ret;
1109}
1110
1111static int __exit atmci_remove(struct platform_device *pdev)
1112{
1113 struct atmel_mci *host = platform_get_drvdata(pdev);
1114
1115 platform_set_drvdata(pdev, NULL);
1116
1117 if (host) {
Haavard Skinnemoendeec9ae2008-07-24 14:18:59 +02001118 /* Debugfs stuff is cleaned up by mmc core */
1119
David Brownell3c26e172008-07-27 02:34:45 -07001120 if (gpio_is_valid(host->detect_pin)) {
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001121 int pin = host->detect_pin;
1122
1123 /* Make sure the timer doesn't enable the interrupt */
1124 host->detect_pin = -1;
1125 smp_wmb();
1126
1127 free_irq(gpio_to_irq(pin), host->mmc);
1128 del_timer_sync(&host->detect_timer);
1129 gpio_free(pin);
1130 }
1131
1132 mmc_remove_host(host->mmc);
1133
1134 clk_enable(host->mck);
1135 mci_writel(host, IDR, ~0UL);
1136 mci_writel(host, CR, MCI_CR_MCIDIS);
1137 mci_readl(host, SR);
1138 clk_disable(host->mck);
1139
David Brownell3c26e172008-07-27 02:34:45 -07001140 if (gpio_is_valid(host->wp_pin))
Haavard Skinnemoen7d2be072008-06-30 18:35:03 +02001141 gpio_free(host->wp_pin);
1142
1143 free_irq(platform_get_irq(pdev, 0), host->mmc);
1144 iounmap(host->regs);
1145
1146 clk_put(host->mck);
1147
1148 mmc_free_host(host->mmc);
1149 }
1150 return 0;
1151}
1152
1153static struct platform_driver atmci_driver = {
1154 .remove = __exit_p(atmci_remove),
1155 .driver = {
1156 .name = "atmel_mci",
1157 },
1158};
1159
1160static int __init atmci_init(void)
1161{
1162 return platform_driver_probe(&atmci_driver, atmci_probe);
1163}
1164
1165static void __exit atmci_exit(void)
1166{
1167 platform_driver_unregister(&atmci_driver);
1168}
1169
1170module_init(atmci_init);
1171module_exit(atmci_exit);
1172
1173MODULE_DESCRIPTION("Atmel Multimedia Card Interface driver");
1174MODULE_AUTHOR("Haavard Skinnemoen <haavard.skinnemoen@atmel.com>");
1175MODULE_LICENSE("GPL v2");