1 From 2eccfcf4c5c50b412a0859a345d8d28fc043956b Mon Sep 17 00:00:00 2001
2 From: Mike Lockwood <lockwood@android.com>
3 Date: Thu, 7 Feb 2008 07:47:30 -0500
4 Subject: [PATCH 124/134] [ARM] goldfish: mmc: goldfish MMC driver building and runnning in 2.6.27.
6 Signed-off-by: Mike A. Chan <mikechan@google.com>
8 drivers/mmc/host/Kconfig | 8 +
9 drivers/mmc/host/Makefile | 1 +
10 drivers/mmc/host/goldfish.c | 583 +++++++++++++++++++++++++++++++++++++++++++
11 3 files changed, 592 insertions(+), 0 deletions(-)
12 create mode 100644 drivers/mmc/host/goldfish.c
14 --- a/drivers/mmc/host/Kconfig
15 +++ b/drivers/mmc/host/Kconfig
16 @@ -200,6 +200,14 @@ config MMC_MVSDIO
17 To compile this driver as a module, choose M here: the
18 module will be called mvsdio.
21 + tristate "goldfish qemu Multimedia Card Interface support"
22 + depends on ARCH_GOLDFISH
24 + This selects the Goldfish Multimedia card Interface emulation.
29 tristate "MMC/SD/SDIO over SPI"
30 depends on SPI_MASTER && !HIGHMEM && HAS_DMA
31 --- a/drivers/mmc/host/Makefile
32 +++ b/drivers/mmc/host/Makefile
33 @@ -26,6 +26,7 @@ obj-$(CONFIG_MMC_SPI) += mmc_spi.o
35 obj-$(CONFIG_MMC_SPI) += of_mmc_spi.o
37 +obj-$(CONFIG_MMC_GOLDFISH) += goldfish.o
38 obj-$(CONFIG_MMC_S3C) += s3cmci.o
39 obj-$(CONFIG_MMC_SDRICOH_CS) += sdricoh_cs.o
40 obj-$(CONFIG_MMC_TMIO) += tmio_mmc.o
42 +++ b/drivers/mmc/host/goldfish.c
45 + * linux/drivers/media/mmc/goldfish.c
47 + * Copyright 2007, Google Inc.
49 + * based on omap.c driver, which was
50 + * Copyright (C) 2004 Nokia Corporation
51 + * Written by Tuukka Tikkanen and Juha Yrjölä<juha.yrjola@nokia.com>
52 + * Misc hacks here and there by Tony Lindgren <tony@atomide.com>
53 + * Other hacks (DMA, SD, etc) by David Brownell
55 + * This program is free software; you can redistribute it and/or modify
56 + * it under the terms of the GNU General Public License version 2 as
57 + * published by the Free Software Foundation.
60 +#include <linux/module.h>
61 +#include <linux/platform_device.h>
62 +#include <linux/major.h>
64 +#include <linux/types.h>
65 +#include <linux/pci.h>
66 +#include <linux/interrupt.h>
68 +#include <linux/kernel.h>
69 +#include <linux/fs.h>
70 +#include <linux/errno.h>
71 +#include <linux/hdreg.h>
72 +#include <linux/kdev_t.h>
73 +#include <linux/blkdev.h>
74 +#include <linux/mutex.h>
75 +#include <linux/scatterlist.h>
76 +#include <linux/mmc/mmc.h>
77 +#include <linux/mmc/sdio.h>
78 +#include <linux/mmc/host.h>
79 +#include <linux/mmc/card.h>
81 +#include <linux/moduleparam.h>
82 +#include <linux/init.h>
83 +#include <linux/ioport.h>
84 +#include <linux/dma-mapping.h>
85 +#include <linux/delay.h>
86 +#include <linux/spinlock.h>
87 +#include <linux/timer.h>
88 +#include <linux/clk.h>
92 +#include <asm/scatterlist.h>
93 +#include <asm/mach-types.h>
96 +#include <asm/types.h>
98 +#include <asm/uaccess.h>
100 +#define DRIVER_NAME "goldfish_mmc"
102 +#define BUFFER_SIZE 16384
104 +#define GOLDFISH_MMC_READ(host, addr) (readl(host->reg_base + addr))
105 +#define GOLDFISH_MMC_WRITE(host, addr, x) (writel(x, host->reg_base + addr))
109 + /* status register */
110 + MMC_INT_STATUS = 0x00,
111 + /* set this to enable IRQ */
112 + MMC_INT_ENABLE = 0x04,
113 + /* set this to specify buffer address */
114 + MMC_SET_BUFFER = 0x08,
116 + /* MMC command number */
122 + /* MMC response (or R2 bits 0 - 31) */
125 + /* MMC R2 response bits 32 - 63 */
128 + /* MMC R2 response bits 64 - 95 */
131 + /* MMC R2 response bits 96 - 127 */
134 + MMC_BLOCK_LENGTH = 0x24,
135 + MMC_BLOCK_COUNT = 0x28,
137 + /* MMC state flags */
140 + /* MMC_INT_STATUS bits */
142 + MMC_STAT_END_OF_CMD = 1U << 0,
143 + MMC_STAT_END_OF_DATA = 1U << 1,
144 + MMC_STAT_STATE_CHANGE = 1U << 2,
146 + /* MMC_STATE bits */
147 + MMC_STATE_INSERTED = 1U << 0,
148 + MMC_STATE_READ_ONLY = 1U << 1,
154 +#define OMAP_MMC_CMDTYPE_BC 0
155 +#define OMAP_MMC_CMDTYPE_BCR 1
156 +#define OMAP_MMC_CMDTYPE_AC 2
157 +#define OMAP_MMC_CMDTYPE_ADTC 3
160 +struct goldfish_mmc_host {
161 + struct mmc_request * mrq;
162 + struct mmc_command * cmd;
163 + struct mmc_data * data;
164 + struct mmc_host * mmc;
165 + struct device * dev;
166 + unsigned char id; /* 16xx chips have 2 MMC blocks */
167 + void __iomem *virt_base;
168 + unsigned int phys_base;
170 + unsigned char bus_mode;
171 + unsigned char hw_bus_mode;
173 + unsigned int sg_len;
174 + unsigned dma_done:1;
175 + unsigned dma_in_use:1;
177 + struct work_struct switch_work;
178 + int switch_last_state;
184 +goldfish_mmc_cover_is_open(struct goldfish_mmc_host *host)
190 +goldfish_mmc_show_cover_switch(struct device *dev,
191 + struct device_attribute *attr, char *buf)
193 + struct goldfish_mmc_host *host = dev_get_drvdata(dev);
195 + return sprintf(buf, "%s\n", goldfish_mmc_cover_is_open(host) ? "open" :
199 +static DEVICE_ATTR(cover_switch, S_IRUGO, goldfish_mmc_show_cover_switch, NULL);
202 +goldfish_mmc_start_command(struct goldfish_mmc_host *host, struct mmc_command *cmd)
213 + /* Our hardware needs to know exact type */
214 + switch (mmc_resp_type(cmd)) {
219 + /* resp 1, 1b, 6, 7 */
229 + dev_err(mmc_dev(host->mmc), "Invalid response type: %04x\n", mmc_resp_type(cmd));
233 + if (mmc_cmd_type(cmd) == MMC_CMD_ADTC) {
234 + cmdtype = OMAP_MMC_CMDTYPE_ADTC;
235 + } else if (mmc_cmd_type(cmd) == MMC_CMD_BC) {
236 + cmdtype = OMAP_MMC_CMDTYPE_BC;
237 + } else if (mmc_cmd_type(cmd) == MMC_CMD_BCR) {
238 + cmdtype = OMAP_MMC_CMDTYPE_BCR;
240 + cmdtype = OMAP_MMC_CMDTYPE_AC;
243 + cmdreg = cmd->opcode | (resptype << 8) | (cmdtype << 12);
245 + if (host->bus_mode == MMC_BUSMODE_OPENDRAIN)
248 + if (cmd->flags & MMC_RSP_BUSY)
251 + if (host->data && !(host->data->flags & MMC_DATA_WRITE))
254 + GOLDFISH_MMC_WRITE(host, MMC_ARG, cmd->arg);
255 + GOLDFISH_MMC_WRITE(host, MMC_CMD, cmdreg);
259 +goldfish_mmc_xfer_done(struct goldfish_mmc_host *host, struct mmc_data *data)
261 + if (host->dma_in_use) {
262 + enum dma_data_direction dma_data_dir;
264 + if (data->flags & MMC_DATA_WRITE)
265 + dma_data_dir = DMA_TO_DEVICE;
267 + dma_data_dir = DMA_FROM_DEVICE;
269 + if (dma_data_dir == DMA_FROM_DEVICE) {
270 + // we don't really have DMA, so we need to copy from our platform driver buffer
271 + uint8_t* dest = (uint8_t *)sg_virt(data->sg);
272 + memcpy(dest, host->virt_base, data->sg->length);
275 + host->data->bytes_xfered += data->sg->length;
277 + dma_unmap_sg(mmc_dev(host->mmc), data->sg, host->sg_len, dma_data_dir);
283 + /* NOTE: MMC layer will sometimes poll-wait CMD13 next, issuing
284 + * dozens of requests until the card finishes writing data.
285 + * It'd be cheaper to just wait till an EOFB interrupt arrives...
290 + mmc_request_done(host->mmc, data->mrq);
294 + goldfish_mmc_start_command(host, data->stop);
298 +goldfish_mmc_end_of_data(struct goldfish_mmc_host *host, struct mmc_data *data)
300 + if (!host->dma_in_use) {
301 + goldfish_mmc_xfer_done(host, data);
304 + if (host->dma_done)
305 + goldfish_mmc_xfer_done(host, data);
309 +goldfish_mmc_cmd_done(struct goldfish_mmc_host *host, struct mmc_command *cmd)
312 + if (cmd->flags & MMC_RSP_PRESENT) {
313 + if (cmd->flags & MMC_RSP_136) {
314 + /* response type 2 */
316 + GOLDFISH_MMC_READ(host, MMC_RESP_0);
318 + GOLDFISH_MMC_READ(host, MMC_RESP_1);
320 + GOLDFISH_MMC_READ(host, MMC_RESP_2);
322 + GOLDFISH_MMC_READ(host, MMC_RESP_3);
324 + /* response types 1, 1b, 3, 4, 5, 6 */
326 + GOLDFISH_MMC_READ(host, MMC_RESP_0);
330 + if (host->data == NULL || cmd->error) {
332 + mmc_request_done(host->mmc, cmd->mrq);
336 +static irqreturn_t goldfish_mmc_irq(int irq, void *dev_id)
338 + struct goldfish_mmc_host * host = (struct goldfish_mmc_host *)dev_id;
342 + int transfer_error;
345 + if (host->cmd == NULL && host->data == NULL) {
346 + status = GOLDFISH_MMC_READ(host, MMC_INT_STATUS);
347 + dev_info(mmc_dev(host->mmc),"spurious irq 0x%04x\n", status);
349 + GOLDFISH_MMC_WRITE(host, MMC_INT_STATUS, status);
350 + GOLDFISH_MMC_WRITE(host, MMC_INT_ENABLE, 0);
352 + return IRQ_HANDLED;
357 + transfer_error = 0;
360 + while ((status = GOLDFISH_MMC_READ(host, MMC_INT_STATUS)) != 0) {
361 + GOLDFISH_MMC_WRITE(host, MMC_INT_STATUS, status);
363 + if (status & MMC_STAT_END_OF_CMD) {
367 + if (status & MMC_STAT_END_OF_DATA) {
370 + if (status & MMC_STAT_STATE_CHANGE) {
376 + goldfish_mmc_cmd_done(host, host->cmd);
378 + if (transfer_error)
379 + goldfish_mmc_xfer_done(host, host->data);
380 + else if (end_transfer) {
381 + host->dma_done = 1;
382 + goldfish_mmc_end_of_data(host, host->data);
384 + if (state_changed) {
385 + schedule_work(&host->switch_work);
388 + return IRQ_HANDLED;
392 +static void goldfish_mmc_switch_handler(struct work_struct *work)
395 + struct goldfish_mmc_host *host = container_of(work, struct goldfish_mmc_host, switch_work);
396 + struct mmc_card *card;
397 + static int complained = 0;
398 + int cards = 0, cover_open;
400 + cover_open = goldfish_mmc_cover_is_open(host);
401 + if (cover_open != host->switch_last_state) {
402 + kobject_uevent(&host->dev->kobj, KOBJ_CHANGE);
403 + host->switch_last_state = cover_open;
405 + mmc_detect_change(host->mmc, 0);
406 + list_for_each_entry(card, &host->mmc->cards, node) {
407 + if (mmc_card_present(card))
410 + if (goldfish_mmc_cover_is_open(host)) {
412 + dev_info(mmc_dev(host->mmc), "cover is open\n");
423 +goldfish_mmc_prepare_data(struct goldfish_mmc_host *host, struct mmc_request *req)
425 + struct mmc_data *data = req->data;
428 + enum dma_data_direction dma_data_dir;
431 + if (data == NULL) {
432 + GOLDFISH_MMC_WRITE(host, MMC_BLOCK_LENGTH, 0);
433 + GOLDFISH_MMC_WRITE(host, MMC_BLOCK_COUNT, 0);
434 + host->dma_in_use = 0;
438 + block_size = data->blksz;
440 + GOLDFISH_MMC_WRITE(host, MMC_BLOCK_COUNT, data->blocks - 1);
441 + GOLDFISH_MMC_WRITE(host, MMC_BLOCK_LENGTH, block_size - 1);
443 + /* cope with calling layer confusion; it issues "single
444 + * block" writes using multi-block scatterlists.
446 + sg_len = (data->blocks == 1) ? 1 : data->sg_len;
448 + if (data->flags & MMC_DATA_WRITE)
449 + dma_data_dir = DMA_TO_DEVICE;
451 + dma_data_dir = DMA_FROM_DEVICE;
453 + host->sg_len = dma_map_sg(mmc_dev(host->mmc), data->sg,
454 + sg_len, dma_data_dir);
455 + host->dma_done = 0;
456 + host->dma_in_use = 1;
458 + if (dma_data_dir == DMA_TO_DEVICE) {
459 + // we don't really have DMA, so we need to copy to our platform driver buffer
460 + const uint8_t* src = (uint8_t *)sg_virt(data->sg);
461 + memcpy(host->virt_base, src, data->sg->length);
465 +static void goldfish_mmc_request(struct mmc_host *mmc, struct mmc_request *req)
467 + struct goldfish_mmc_host *host = mmc_priv(mmc);
469 + WARN_ON(host->mrq != NULL);
472 + goldfish_mmc_prepare_data(host, req);
473 + goldfish_mmc_start_command(host, req->cmd);
475 + /* this is to avoid accidentally being detected as an SDIO card in mmc_attach_sdio() */
476 + if (req->cmd->opcode == SD_IO_SEND_OP_COND &&
477 + req->cmd->flags == (MMC_RSP_SPI_R4 | MMC_RSP_R4 | MMC_CMD_BCR)) {
478 + req->cmd->error = -EINVAL;
482 +static void goldfish_mmc_set_ios(struct mmc_host *mmc, struct mmc_ios *ios)
484 + struct goldfish_mmc_host *host = mmc_priv(mmc);
486 + host->bus_mode = ios->bus_mode;
487 + host->hw_bus_mode = host->bus_mode;
490 +static int goldfish_mmc_get_ro(struct mmc_host *mmc)
493 + struct goldfish_mmc_host *host = mmc_priv(mmc);
495 + state = GOLDFISH_MMC_READ(host, MMC_STATE);
496 + return ((state & MMC_STATE_READ_ONLY) != 0);
499 +static const struct mmc_host_ops goldfish_mmc_ops = {
500 + .request = goldfish_mmc_request,
501 + .set_ios = goldfish_mmc_set_ios,
502 + .get_ro = goldfish_mmc_get_ro,
505 +static int __init goldfish_mmc_probe(struct platform_device *pdev)
507 + struct mmc_host *mmc;
508 + struct goldfish_mmc_host *host = NULL;
509 + struct resource *res;
512 + dma_addr_t buf_addr;
514 + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
515 + irq = platform_get_irq(pdev, 0);
516 + if (res == NULL || irq < 0)
519 + mmc = mmc_alloc_host(sizeof(struct goldfish_mmc_host), &pdev->dev);
522 + goto err_alloc_host_failed;
525 + host = mmc_priv(mmc);
527 + host->reg_base = IO_ADDRESS(res->start - IO_START);
528 + host->virt_base = dma_alloc_writecombine(&pdev->dev, BUFFER_SIZE,
529 + &buf_addr, GFP_KERNEL);
530 + if(host->virt_base == 0) {
532 + goto dma_alloc_failed;
534 + host->phys_base = buf_addr;
536 + host->id = pdev->id;
539 + mmc->ops = &goldfish_mmc_ops;
540 + mmc->f_min = 400000;
541 + mmc->f_max = 24000000;
542 + mmc->ocr_avail = MMC_VDD_32_33 | MMC_VDD_33_34;
543 + mmc->caps = MMC_CAP_4_BIT_DATA;
545 + /* Use scatterlist DMA to reduce per-transfer costs.
546 + * NOTE max_seg_size assumption that small blocks aren't
547 + * normally used (except e.g. for reading SD registers).
549 + mmc->max_phys_segs = 32;
550 + mmc->max_hw_segs = 32;
551 + mmc->max_blk_size = 2048; /* MMC_BLOCK_LENGTH is 11 bits (+1) */
552 + mmc->max_blk_count = 2048; /* MMC_BLOCK_COUNT is 11 bits (+1) */
553 + mmc->max_req_size = BUFFER_SIZE;
554 + mmc->max_seg_size = mmc->max_req_size;
556 + ret = request_irq(host->irq, goldfish_mmc_irq, 0, DRIVER_NAME, host);
558 + goto err_request_irq_failed;
560 + host->dev = &pdev->dev;
561 + platform_set_drvdata(pdev, host);
563 + ret = device_create_file(&pdev->dev, &dev_attr_cover_switch);
565 + dev_warn(mmc_dev(host->mmc), "Unable to create sysfs attributes\n");
569 + GOLDFISH_MMC_WRITE(host, MMC_SET_BUFFER, host->phys_base);
570 + GOLDFISH_MMC_WRITE(host, MMC_INT_ENABLE,
571 + MMC_STAT_END_OF_CMD | MMC_STAT_END_OF_DATA | MMC_STAT_STATE_CHANGE
574 + // we start with the card present
575 + kobject_uevent(&host->dev->kobj, KOBJ_CHANGE);
576 + mmc_detect_change(host->mmc, 0);
578 + INIT_WORK(&host->switch_work, goldfish_mmc_switch_handler);
582 +err_request_irq_failed:
583 + dma_free_writecombine(&pdev->dev, BUFFER_SIZE, host->virt_base, host->phys_base);
585 + mmc_free_host(host->mmc);
586 +err_alloc_host_failed:
590 +static int goldfish_mmc_remove(struct platform_device *pdev)
592 + struct goldfish_mmc_host *host = platform_get_drvdata(pdev);
594 + platform_set_drvdata(pdev, NULL);
596 + BUG_ON(host == NULL);
598 + mmc_remove_host(host->mmc);
599 + free_irq(host->irq, host);
600 + dma_free_writecombine(&pdev->dev, BUFFER_SIZE, host->virt_base, host->phys_base);
601 + mmc_free_host(host->mmc);
606 +static struct platform_driver goldfish_mmc_driver = {
607 + .probe = goldfish_mmc_probe,
608 + .remove = goldfish_mmc_remove,
610 + .name = DRIVER_NAME,
614 +static int __init goldfish_mmc_init(void)
616 + return platform_driver_register(&goldfish_mmc_driver);
619 +static void __exit goldfish_mmc_exit(void)
621 + platform_driver_unregister(&goldfish_mmc_driver);
624 +module_init(goldfish_mmc_init);
625 +module_exit(goldfish_mmc_exit);