21 #include <linux/module.h>
27 #include <linux/bitops.h>
30 #include <asm/cacheflush.h>
35 #define JZ_REG_MMC_STRPCL 0x00
36 #define JZ_REG_MMC_STATUS 0x04
37 #define JZ_REG_MMC_CLKRT 0x08
38 #define JZ_REG_MMC_CMDAT 0x0C
39 #define JZ_REG_MMC_RESTO 0x10
40 #define JZ_REG_MMC_RDTO 0x14
41 #define JZ_REG_MMC_BLKLEN 0x18
42 #define JZ_REG_MMC_NOB 0x1C
43 #define JZ_REG_MMC_SNOB 0x20
44 #define JZ_REG_MMC_IMASK 0x24
45 #define JZ_REG_MMC_IREG 0x28
46 #define JZ_REG_MMC_CMD 0x2C
47 #define JZ_REG_MMC_ARG 0x30
48 #define JZ_REG_MMC_RESP_FIFO 0x34
49 #define JZ_REG_MMC_RXFIFO 0x38
50 #define JZ_REG_MMC_TXFIFO 0x3C
52 #define JZ_MMC_STRPCL_EXIT_MULTIPLE BIT(7)
53 #define JZ_MMC_STRPCL_EXIT_TRANSFER BIT(6)
54 #define JZ_MMC_STRPCL_START_READWAIT BIT(5)
55 #define JZ_MMC_STRPCL_STOP_READWAIT BIT(4)
56 #define JZ_MMC_STRPCL_RESET BIT(3)
57 #define JZ_MMC_STRPCL_START_OP BIT(2)
58 #define JZ_MMC_STRPCL_CLOCK_CONTROL (BIT(1) | BIT(0))
59 #define JZ_MMC_STRPCL_CLOCK_STOP BIT(0)
60 #define JZ_MMC_STRPCL_CLOCK_START BIT(1)
63 #define JZ_MMC_STATUS_IS_RESETTING BIT(15)
64 #define JZ_MMC_STATUS_SDIO_INT_ACTIVE BIT(14)
65 #define JZ_MMC_STATUS_PRG_DONE BIT(13)
66 #define JZ_MMC_STATUS_DATA_TRAN_DONE BIT(12)
67 #define JZ_MMC_STATUS_END_CMD_RES BIT(11)
68 #define JZ_MMC_STATUS_DATA_FIFO_AFULL BIT(10)
69 #define JZ_MMC_STATUS_IS_READWAIT BIT(9)
70 #define JZ_MMC_STATUS_CLK_EN BIT(8)
71 #define JZ_MMC_STATUS_DATA_FIFO_FULL BIT(7)
72 #define JZ_MMC_STATUS_DATA_FIFO_EMPTY BIT(6)
73 #define JZ_MMC_STATUS_CRC_RES_ERR BIT(5)
74 #define JZ_MMC_STATUS_CRC_READ_ERROR BIT(4)
75 #define JZ_MMC_STATUS_TIMEOUT_WRITE BIT(3)
76 #define JZ_MMC_STATUS_CRC_WRITE_ERROR BIT(2)
77 #define JZ_MMC_STATUS_TIMEOUT_RES BIT(1)
78 #define JZ_MMC_STATUS_TIMEOUT_READ BIT(0)
80 #define JZ_MMC_STATUS_READ_ERROR_MASK (BIT(4) | BIT(0))
81 #define JZ_MMC_STATUS_WRITE_ERROR_MASK (BIT(3) | BIT(2))
84 #define JZ_MMC_CMDAT_IO_ABORT BIT(11)
85 #define JZ_MMC_CMDAT_BUS_WIDTH_4BIT BIT(10)
86 #define JZ_MMC_CMDAT_DMA_EN BIT(8)
87 #define JZ_MMC_CMDAT_INIT BIT(7)
88 #define JZ_MMC_CMDAT_BUSY BIT(6)
89 #define JZ_MMC_CMDAT_STREAM BIT(5)
90 #define JZ_MMC_CMDAT_WRITE BIT(4)
91 #define JZ_MMC_CMDAT_DATA_EN BIT(3)
92 #define JZ_MMC_CMDAT_RESPONSE_FORMAT (BIT(2) | BIT(1) | BIT(0))
93 #define JZ_MMC_CMDAT_RSP_R1 1
94 #define JZ_MMC_CMDAT_RSP_R2 2
95 #define JZ_MMC_CMDAT_RSP_R3 3
97 #define JZ_MMC_IRQ_SDIO BIT(7)
98 #define JZ_MMC_IRQ_TXFIFO_WR_REQ BIT(6)
99 #define JZ_MMC_IRQ_RXFIFO_RD_REQ BIT(5)
100 #define JZ_MMC_IRQ_END_CMD_RES BIT(2)
101 #define JZ_MMC_IRQ_PRG_DONE BIT(1)
102 #define JZ_MMC_IRQ_DATA_TRAN_DONE BIT(0)
105 #define JZ_MMC_CLK_RATE 24000000
142 unsigned int irq,
bool enabled)
151 spin_unlock_irqrestore(&host->
lock, flags);
181 unsigned int timeout = 1000;
203 unsigned int timeout = 0x800;
208 }
while (!(status & irq) && --timeout);
213 jz4740_mmc_set_irq_enabled(host, irq,
true);
220 static void jz4740_mmc_transfer_check_state(
struct jz4740_mmc_host *host,
231 host->
req->cmd->error = -
EIO;
256 writel(buf[0], fifo_addr);
257 writel(buf[1], fifo_addr);
258 writel(buf[2], fifo_addr);
259 writel(buf[3], fifo_addr);
260 writel(buf[4], fifo_addr);
261 writel(buf[5], fifo_addr);
262 writel(buf[6], fifo_addr);
263 writel(buf[7], fifo_addr);
301 unsigned int timeout;
313 buf[0] =
readl(fifo_addr);
314 buf[1] =
readl(fifo_addr);
315 buf[2] =
readl(fifo_addr);
316 buf[3] =
readl(fifo_addr);
317 buf[4] =
readl(fifo_addr);
318 buf[5] =
readl(fifo_addr);
319 buf[6] =
readl(fifo_addr);
320 buf[7] =
readl(fifo_addr);
332 *buf++ =
readl(fifo_addr);
336 d =
readl(fifo_addr);
353 d =
readl(fifo_addr);
367 static void jz4740_mmc_timeout(
unsigned long data)
377 jz4740_mmc_request_done(host);
388 tmp =
readw(fifo_addr);
389 for (i = 0; i < 4; ++
i) {
390 cmd->
resp[
i] = tmp << 24;
391 tmp =
readw(fifo_addr);
392 cmd->
resp[
i] |= tmp << 8;
393 tmp =
readw(fifo_addr);
394 cmd->
resp[
i] |= tmp >> 8;
409 jz4740_mmc_clock_disable(host);
446 jz4740_mmc_clock_enable(host, 1);
469 bool timeout =
false;
474 switch (host->
state) {
477 jz4740_mmc_read_response(host, cmd);
482 jz_mmc_prepare_data_transfer(host);
486 timeout = jz4740_mmc_read_data(host, cmd->
data);
488 timeout = jz4740_mmc_write_data(host, cmd->
data);
495 jz4740_mmc_transfer_check_state(host, cmd->
data);
508 jz4740_mmc_send_command(host, req->
stop);
520 jz4740_mmc_request_done(host);
525 static irqreturn_t jz_mmc_irq(
int irq,
void *devid)
544 mmc_signal_sdio_irq(host->
mmc);
545 irq_reg &= ~JZ_MMC_IRQ_SDIO;
548 if (host->
req && cmd && irq_reg) {
570 jz4740_mmc_set_irq_enabled(host, irq_reg,
false);
585 jz4740_mmc_clock_disable(host);
590 while (real_rate > rate && div < 7) {
613 jz4740_mmc_send_command(host, req->
cmd);
620 jz4740_mmc_set_clock_rate(host, ios->
clock);
624 jz4740_mmc_reset(host);
625 if (gpio_is_valid(host->
pdata->gpio_power))
627 !host->
pdata->power_active_low);
634 if (gpio_is_valid(host->
pdata->gpio_power))
636 host->
pdata->power_active_low);
653 static int jz4740_mmc_get_ro(
struct mmc_host *mmc)
656 if (!gpio_is_valid(host->
pdata->gpio_read_only))
660 host->
pdata->read_only_active_low;
663 static int jz4740_mmc_get_cd(
struct mmc_host *mmc)
666 if (!gpio_is_valid(host->
pdata->gpio_card_detect))
670 host->
pdata->card_detect_active_low;
673 static irqreturn_t jz4740_mmc_card_detect_irq(
int irq,
void *devid)
682 static void jz4740_mmc_enable_sdio_irq(
struct mmc_host *mmc,
int enable)
685 jz4740_mmc_set_irq_enabled(host, JZ_MMC_IRQ_SDIO, enable);
689 .request = jz4740_mmc_request,
690 .set_ios = jz4740_mmc_set_ios,
691 .get_ro = jz4740_mmc_get_ro,
692 .get_cd = jz4740_mmc_get_cd,
693 .enable_sdio_irq = jz4740_mmc_enable_sdio_irq,
706 const char *
name,
bool output,
int value)
710 if (!gpio_is_valid(gpio))
715 dev_err(dev,
"Failed to request %s gpio: %d\n", name, ret);
736 "MMC detect change",
false, 0);
741 "MMC read only",
false, 0);
743 goto err_free_gpio_card_detect;
748 goto err_free_gpio_read_only;
752 err_free_gpio_read_only:
755 err_free_gpio_card_detect:
772 dev_warn(&pdev->
dev,
"Failed to get card detect irq\n");
778 "MMC card detect", host);
798 size_t num_pins =
ARRAY_SIZE(jz4740_mmc_pins);
812 pdata = pdev->
dev.platform_data;
816 dev_err(&pdev->
dev,
"Failed to alloc mmc host structure\n");
820 host = mmc_priv(mmc);
826 dev_err(&pdev->
dev,
"Failed to get platform irq: %d\n", ret);
831 if (IS_ERR(host->
clk)) {
832 ret = PTR_ERR(host->
clk);
833 dev_err(&pdev->
dev,
"Failed to get mmc clock\n");
840 dev_err(&pdev->
dev,
"Failed to get base platform memory\n");
845 resource_size(host->
mem), pdev->
name);
848 dev_err(&pdev->
dev,
"Failed to request base memory region\n");
855 dev_err(&pdev->
dev,
"Failed to ioremap base memory\n");
856 goto err_release_mem_region;
861 dev_err(&pdev->
dev,
"Failed to request mmc pins: %d\n", ret);
865 ret = jz4740_mmc_request_gpios(pdev);
867 goto err_gpio_bulk_free;
869 mmc->
ops = &jz4740_mmc_ops;
888 ret = jz4740_mmc_request_cd_irq(pdev, host);
890 dev_err(&pdev->
dev,
"Failed to request card detect irq\n");
895 dev_name(&pdev->
dev), host);
897 dev_err(&pdev->
dev,
"Failed to request irq: %d\n", ret);
898 goto err_free_card_detect_irq;
901 jz4740_mmc_reset(host);
902 jz4740_mmc_clock_disable(host);
904 (
unsigned long)host);
908 platform_set_drvdata(pdev, host);
912 dev_err(&pdev->
dev,
"Failed to add mmc host: %d\n", ret);
915 dev_info(&pdev->
dev,
"JZ SD/MMC card driver registered\n");
921 err_free_card_detect_irq:
925 jz4740_mmc_free_gpios(pdev);
930 err_release_mem_region:
935 platform_set_drvdata(pdev,
NULL);
946 jz4740_mmc_set_irq_enabled(host, 0xff,
false);
947 jz4740_mmc_reset(host);
955 jz4740_mmc_free_gpios(pdev);
963 platform_set_drvdata(pdev,
NULL);
971 static int jz4740_mmc_suspend(
struct device *dev)
982 static int jz4740_mmc_resume(
struct device *dev)
995 .resume = jz4740_mmc_resume,
996 .poweroff = jz4740_mmc_suspend,
997 .restore = jz4740_mmc_resume,
1000 #define JZ4740_MMC_PM_OPS (&jz4740_mmc_pm_ops)
1002 #define JZ4740_MMC_PM_OPS NULL
1006 .probe = jz4740_mmc_probe,
1009 .name =
"jz4740-mmc",