1 // SPDX-License-Identifier: GPL-2.0+
3 * ARM PrimeCell MultiMedia Card Interface - PL180
5 * Copyright (C) ST-Ericsson SA 2010
20 #include <dm/device_compat.h>
23 #include <asm-generic/gpio.h>
25 #include "arm_pl180_mmci.h"
26 #include <linux/delay.h>
30 #define MMC_CLOCK_MAX 48000000
31 #define MMC_CLOCK_MIN 400000
33 struct arm_pl180_mmc_plat {
34 struct mmc_config cfg;
39 static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd)
41 u32 hoststatus, statusmask;
42 struct pl180_mmc_host *host = dev->priv;
44 statusmask = SDI_STA_CTIMEOUT | SDI_STA_CCRCFAIL;
45 if ((cmd->resp_type & MMC_RSP_PRESENT))
46 statusmask |= SDI_STA_CMDREND;
48 statusmask |= SDI_STA_CMDSENT;
51 hoststatus = readl(&host->base->status) & statusmask;
54 writel(statusmask, &host->base->status_clear);
55 if (hoststatus & SDI_STA_CTIMEOUT) {
56 debug("CMD%d time out\n", cmd->cmdidx);
58 } else if ((hoststatus & SDI_STA_CCRCFAIL) &&
59 (cmd->resp_type & MMC_RSP_CRC)) {
60 printf("CMD%d CRC error\n", cmd->cmdidx);
64 if (cmd->resp_type & MMC_RSP_PRESENT) {
65 cmd->response[0] = readl(&host->base->response0);
66 cmd->response[1] = readl(&host->base->response1);
67 cmd->response[2] = readl(&host->base->response2);
68 cmd->response[3] = readl(&host->base->response3);
69 debug("CMD%d response[0]:0x%08X, response[1]:0x%08X, "
70 "response[2]:0x%08X, response[3]:0x%08X\n",
71 cmd->cmdidx, cmd->response[0], cmd->response[1],
72 cmd->response[2], cmd->response[3]);
78 /* send command to the mmc card and wait for results */
79 static int do_command(struct mmc *dev, struct mmc_cmd *cmd)
83 struct pl180_mmc_host *host = dev->priv;
85 sdi_cmd = ((cmd->cmdidx & SDI_CMD_CMDINDEX_MASK) | SDI_CMD_CPSMEN);
88 sdi_cmd |= SDI_CMD_WAITRESP;
89 if (cmd->resp_type & MMC_RSP_136)
90 sdi_cmd |= SDI_CMD_LONGRESP;
93 writel((u32)cmd->cmdarg, &host->base->argument);
94 udelay(COMMAND_REG_DELAY);
95 writel(sdi_cmd, &host->base->command);
96 result = wait_for_command_end(dev, cmd);
98 /* After CMD2 set RCA to a none zero value. */
99 if ((result == 0) && (cmd->cmdidx == MMC_CMD_ALL_SEND_CID))
102 /* After CMD3 open drain is switched off and push pull is used. */
103 if ((result == 0) && (cmd->cmdidx == MMC_CMD_SET_RELATIVE_ADDR)) {
104 u32 sdi_pwr = readl(&host->base->power) & ~SDI_PWR_OPD;
105 writel(sdi_pwr, &host->base->power);
111 static int read_bytes(struct mmc *dev, u32 *dest, u32 blkcount, u32 blksize)
113 u32 *tempbuff = dest;
114 u64 xfercount = blkcount * blksize;
115 struct pl180_mmc_host *host = dev->priv;
116 u32 status, status_err;
118 debug("read_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
120 status = readl(&host->base->status);
121 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
123 while ((!status_err) && (xfercount >= sizeof(u32))) {
124 if (status & SDI_STA_RXDAVL) {
125 *(tempbuff) = readl(&host->base->fifo);
127 xfercount -= sizeof(u32);
129 status = readl(&host->base->status);
130 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
134 status_err = status &
135 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
137 while (!status_err) {
138 status = readl(&host->base->status);
139 status_err = status &
140 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
144 if (status & SDI_STA_DTIMEOUT) {
145 printf("Read data timed out, xfercount: %llu, status: 0x%08X\n",
148 } else if (status & SDI_STA_DCRCFAIL) {
149 printf("Read data bytes CRC error: 0x%x\n", status);
151 } else if (status & SDI_STA_RXOVERR) {
152 printf("Read data RX overflow error\n");
156 writel(SDI_ICR_MASK, &host->base->status_clear);
159 printf("Read data error, xfercount: %llu\n", xfercount);
166 static int write_bytes(struct mmc *dev, u32 *src, u32 blkcount, u32 blksize)
170 u64 xfercount = blkcount * blksize;
171 struct pl180_mmc_host *host = dev->priv;
172 u32 status, status_err;
174 debug("write_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
176 status = readl(&host->base->status);
177 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
178 while (!status_err && xfercount) {
179 if (status & SDI_STA_TXFIFOBW) {
180 if (xfercount >= SDI_FIFO_BURST_SIZE * sizeof(u32)) {
181 for (i = 0; i < SDI_FIFO_BURST_SIZE; i++)
182 writel(*(tempbuff + i),
184 tempbuff += SDI_FIFO_BURST_SIZE;
185 xfercount -= SDI_FIFO_BURST_SIZE * sizeof(u32);
187 while (xfercount >= sizeof(u32)) {
188 writel(*(tempbuff), &host->base->fifo);
190 xfercount -= sizeof(u32);
194 status = readl(&host->base->status);
195 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
198 status_err = status &
199 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
200 while (!status_err) {
201 status = readl(&host->base->status);
202 status_err = status &
203 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
206 if (status & SDI_STA_DTIMEOUT) {
207 printf("Write data timed out, xfercount:%llu,status:0x%08X\n",
210 } else if (status & SDI_STA_DCRCFAIL) {
211 printf("Write data CRC error\n");
215 writel(SDI_ICR_MASK, &host->base->status_clear);
218 printf("Write data error, xfercount:%llu", xfercount);
225 static int do_data_transfer(struct mmc *dev,
227 struct mmc_data *data)
229 int error = -ETIMEDOUT;
230 struct pl180_mmc_host *host = dev->priv;
233 u32 data_len = (u32) (data->blocks * data->blocksize);
235 if (!host->version2) {
236 blksz = (ffs(data->blocksize) - 1);
237 data_ctrl |= ((blksz << 4) & SDI_DCTRL_DBLKSIZE_MASK);
239 blksz = data->blocksize;
240 data_ctrl |= (blksz << SDI_DCTRL_DBLOCKSIZE_V2_SHIFT);
242 data_ctrl |= SDI_DCTRL_DTEN | SDI_DCTRL_BUSYMODE;
244 writel(SDI_DTIMER_DEFAULT, &host->base->datatimer);
245 writel(data_len, &host->base->datalength);
246 udelay(DATA_REG_DELAY);
248 if (data->flags & MMC_DATA_READ) {
249 data_ctrl |= SDI_DCTRL_DTDIR_IN;
250 writel(data_ctrl, &host->base->datactrl);
252 error = do_command(dev, cmd);
256 error = read_bytes(dev, (u32 *)data->dest, (u32)data->blocks,
257 (u32)data->blocksize);
258 } else if (data->flags & MMC_DATA_WRITE) {
259 error = do_command(dev, cmd);
263 writel(data_ctrl, &host->base->datactrl);
264 error = write_bytes(dev, (u32 *)data->src, (u32)data->blocks,
265 (u32)data->blocksize);
271 static int host_request(struct mmc *dev,
273 struct mmc_data *data)
278 result = do_data_transfer(dev, cmd, data);
280 result = do_command(dev, cmd);
285 static int host_set_ios(struct mmc *dev)
287 struct pl180_mmc_host *host = dev->priv;
290 sdi_clkcr = readl(&host->base->clock);
292 /* Ramp up the clock rate */
297 if (dev->clock >= dev->cfg->f_max) {
299 dev->clock = dev->cfg->f_max;
301 clkdiv = (host->clock_in / dev->clock) - 2;
304 tmp_clock = host->clock_in / (clkdiv + 2);
305 while (tmp_clock > dev->clock) {
307 tmp_clock = host->clock_in / (clkdiv + 2);
310 if (clkdiv > SDI_CLKCR_CLKDIV_MASK)
311 clkdiv = SDI_CLKCR_CLKDIV_MASK;
313 tmp_clock = host->clock_in / (clkdiv + 2);
314 dev->clock = tmp_clock;
315 sdi_clkcr &= ~(SDI_CLKCR_CLKDIV_MASK);
319 /* Set the bus width */
320 if (dev->bus_width) {
323 switch (dev->bus_width) {
325 buswidth |= SDI_CLKCR_WIDBUS_1;
328 buswidth |= SDI_CLKCR_WIDBUS_4;
331 buswidth |= SDI_CLKCR_WIDBUS_8;
334 printf("Invalid bus width: %d\n", dev->bus_width);
337 sdi_clkcr &= ~(SDI_CLKCR_WIDBUS_MASK);
338 sdi_clkcr |= buswidth;
341 writel(sdi_clkcr, &host->base->clock);
342 udelay(CLK_CHANGE_DELAY);
347 #ifndef CONFIG_DM_MMC
348 /* MMC uses open drain drivers in the enumeration phase */
349 static int mmc_host_reset(struct mmc *dev)
351 struct pl180_mmc_host *host = dev->priv;
353 writel(host->pwr_init, &host->base->power);
358 static const struct mmc_ops arm_pl180_mmci_ops = {
359 .send_cmd = host_request,
360 .set_ios = host_set_ios,
361 .init = mmc_host_reset,
365 * mmc_host_init - initialize the mmc controller.
366 * Set initial clock and power for mmc slot.
367 * Initialize mmc struct and register with mmc framework.
370 int arm_pl180_mmci_init(struct pl180_mmc_host *host, struct mmc **mmc)
374 writel(host->pwr_init, &host->base->power);
375 writel(host->clkdiv_init, &host->base->clock);
376 udelay(CLK_CHANGE_DELAY);
378 /* Disable mmc interrupts */
379 sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
380 writel(sdi_u32, &host->base->mask0);
382 host->cfg.name = host->name;
383 host->cfg.ops = &arm_pl180_mmci_ops;
385 /* TODO remove the duplicates */
386 host->cfg.host_caps = host->caps;
387 host->cfg.voltages = host->voltages;
388 host->cfg.f_min = host->clock_min;
389 host->cfg.f_max = host->clock_max;
390 if (host->b_max != 0)
391 host->cfg.b_max = host->b_max;
393 host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
395 *mmc = mmc_create(&host->cfg, host);
398 debug("registered mmc interface number is:%d\n",
399 (*mmc)->block_dev.devnum);
406 static void arm_pl180_mmc_init(struct pl180_mmc_host *host)
410 writel(host->pwr_init, &host->base->power);
411 writel(host->clkdiv_init, &host->base->clock);
412 udelay(CLK_CHANGE_DELAY);
414 /* Disable mmc interrupts */
415 sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
416 writel(sdi_u32, &host->base->mask0);
419 static int arm_pl180_mmc_probe(struct udevice *dev)
421 struct arm_pl180_mmc_plat *pdata = dev_get_plat(dev);
422 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
423 struct mmc *mmc = &pdata->mmc;
424 struct pl180_mmc_host *host = dev_get_priv(dev);
425 struct mmc_config *cfg = &pdata->cfg;
431 ret = clk_get_by_index(dev, 0, &clk);
435 ret = clk_enable(&clk);
438 dev_err(dev, "failed to enable clock\n");
442 host->pwr_init = INIT_PWR;
443 host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V1 | SDI_CLKCR_CLKEN |
445 host->clock_in = clk_get_rate(&clk);
447 periphid = dev_read_u32_default(dev, "arm,primecell-periphid", 0);
449 case STM32_MMCI_ID: /* stm32 variant */
450 host->version2 = false;
453 host->version2 = true;
456 cfg->name = dev->name;
457 cfg->voltages = VOLTAGE_WINDOW_SD;
459 cfg->f_min = host->clock_in / (2 * (SDI_CLKCR_CLKDIV_INIT_V1 + 1));
460 cfg->f_max = dev_read_u32_default(dev, "max-frequency", MMC_CLOCK_MAX);
461 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
463 gpio_request_by_name(dev, "cd-gpios", 0, &host->cd_gpio, GPIOD_IS_IN);
465 bus_width = dev_read_u32_default(dev, "bus-width", 1);
468 cfg->host_caps |= MMC_MODE_8BIT;
469 /* Hosts capable of 8-bit transfers can also do 4 bits */
471 cfg->host_caps |= MMC_MODE_4BIT;
476 dev_err(dev, "Invalid bus-width value %u\n", bus_width);
479 arm_pl180_mmc_init(host);
487 int arm_pl180_mmc_bind(struct udevice *dev)
489 struct arm_pl180_mmc_plat *plat = dev_get_plat(dev);
491 return mmc_bind(dev, &plat->mmc, &plat->cfg);
494 static int dm_host_request(struct udevice *dev, struct mmc_cmd *cmd,
495 struct mmc_data *data)
497 struct mmc *mmc = mmc_get_mmc_dev(dev);
499 return host_request(mmc, cmd, data);
502 static int dm_host_set_ios(struct udevice *dev)
504 struct mmc *mmc = mmc_get_mmc_dev(dev);
506 return host_set_ios(mmc);
509 static int dm_mmc_getcd(struct udevice *dev)
511 struct pl180_mmc_host *host = dev_get_priv(dev);
514 if (dm_gpio_is_valid(&host->cd_gpio))
515 value = dm_gpio_get_value(&host->cd_gpio);
520 static const struct dm_mmc_ops arm_pl180_dm_mmc_ops = {
521 .send_cmd = dm_host_request,
522 .set_ios = dm_host_set_ios,
523 .get_cd = dm_mmc_getcd,
526 static int arm_pl180_mmc_of_to_plat(struct udevice *dev)
528 struct pl180_mmc_host *host = dev_get_priv(dev);
531 addr = dev_read_addr(dev);
532 if (addr == FDT_ADDR_T_NONE)
535 host->base = (void *)addr;
540 static const struct udevice_id arm_pl180_mmc_match[] = {
541 { .compatible = "arm,pl180" },
542 { .compatible = "arm,primecell" },
546 U_BOOT_DRIVER(arm_pl180_mmc) = {
547 .name = "arm_pl180_mmc",
549 .of_match = arm_pl180_mmc_match,
550 .ops = &arm_pl180_dm_mmc_ops,
551 .probe = arm_pl180_mmc_probe,
552 .of_to_plat = arm_pl180_mmc_of_to_plat,
553 .bind = arm_pl180_mmc_bind,
554 .priv_auto = sizeof(struct pl180_mmc_host),
555 .plat_auto = sizeof(struct arm_pl180_mmc_plat),