2 * ARM PrimeCell MultiMedia Card Interface - PL180
4 * Copyright (C) ST-Ericsson SA 2010
6 * Author: Ulf Hansson <ulf.hansson@stericsson.com>
7 * Author: Martin Lundholm <martin.xa.lundholm@stericsson.com>
8 * Ported to drivers/mmc/ by: Matt Waddel <matt.waddel@linaro.org>
10 * SPDX-License-Identifier: GPL-2.0+
21 #include "arm_pl180_mmci.h"
27 DECLARE_GLOBAL_DATA_PTR;
29 #define MMC_CLOCK_MAX 48000000
30 #define MMC_CLOCK_MIN 400000
32 struct arm_pl180_mmc_plat {
33 struct mmc_config cfg;
38 static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd)
40 u32 hoststatus, statusmask;
41 struct pl180_mmc_host *host = dev->priv;
43 statusmask = SDI_STA_CTIMEOUT | SDI_STA_CCRCFAIL;
44 if ((cmd->resp_type & MMC_RSP_PRESENT))
45 statusmask |= SDI_STA_CMDREND;
47 statusmask |= SDI_STA_CMDSENT;
50 hoststatus = readl(&host->base->status) & statusmask;
53 writel(statusmask, &host->base->status_clear);
54 if (hoststatus & SDI_STA_CTIMEOUT) {
55 debug("CMD%d time out\n", cmd->cmdidx);
57 } else if ((hoststatus & SDI_STA_CCRCFAIL) &&
58 (cmd->resp_type & MMC_RSP_CRC)) {
59 printf("CMD%d CRC error\n", cmd->cmdidx);
63 if (cmd->resp_type & MMC_RSP_PRESENT) {
64 cmd->response[0] = readl(&host->base->response0);
65 cmd->response[1] = readl(&host->base->response1);
66 cmd->response[2] = readl(&host->base->response2);
67 cmd->response[3] = readl(&host->base->response3);
68 debug("CMD%d response[0]:0x%08X, response[1]:0x%08X, "
69 "response[2]:0x%08X, response[3]:0x%08X\n",
70 cmd->cmdidx, cmd->response[0], cmd->response[1],
71 cmd->response[2], cmd->response[3]);
77 /* send command to the mmc card and wait for results */
78 static int do_command(struct mmc *dev, struct mmc_cmd *cmd)
82 struct pl180_mmc_host *host = dev->priv;
84 sdi_cmd = ((cmd->cmdidx & SDI_CMD_CMDINDEX_MASK) | SDI_CMD_CPSMEN);
87 sdi_cmd |= SDI_CMD_WAITRESP;
88 if (cmd->resp_type & MMC_RSP_136)
89 sdi_cmd |= SDI_CMD_LONGRESP;
92 writel((u32)cmd->cmdarg, &host->base->argument);
93 udelay(COMMAND_REG_DELAY);
94 writel(sdi_cmd, &host->base->command);
95 result = wait_for_command_end(dev, cmd);
97 /* After CMD2 set RCA to a none zero value. */
98 if ((result == 0) && (cmd->cmdidx == MMC_CMD_ALL_SEND_CID))
101 /* After CMD3 open drain is switched off and push pull is used. */
102 if ((result == 0) && (cmd->cmdidx == MMC_CMD_SET_RELATIVE_ADDR)) {
103 u32 sdi_pwr = readl(&host->base->power) & ~SDI_PWR_OPD;
104 writel(sdi_pwr, &host->base->power);
110 static int read_bytes(struct mmc *dev, u32 *dest, u32 blkcount, u32 blksize)
112 u32 *tempbuff = dest;
113 u64 xfercount = blkcount * blksize;
114 struct pl180_mmc_host *host = dev->priv;
115 u32 status, status_err;
117 debug("read_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
119 status = readl(&host->base->status);
120 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
122 while ((!status_err) && (xfercount >= sizeof(u32))) {
123 if (status & SDI_STA_RXDAVL) {
124 *(tempbuff) = readl(&host->base->fifo);
126 xfercount -= sizeof(u32);
128 status = readl(&host->base->status);
129 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT |
133 status_err = status &
134 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
136 while (!status_err) {
137 status = readl(&host->base->status);
138 status_err = status &
139 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND |
143 if (status & SDI_STA_DTIMEOUT) {
144 printf("Read data timed out, xfercount: %llu, status: 0x%08X\n",
147 } else if (status & SDI_STA_DCRCFAIL) {
148 printf("Read data bytes CRC error: 0x%x\n", status);
150 } else if (status & SDI_STA_RXOVERR) {
151 printf("Read data RX overflow error\n");
155 writel(SDI_ICR_MASK, &host->base->status_clear);
158 printf("Read data error, xfercount: %llu\n", xfercount);
165 static int write_bytes(struct mmc *dev, u32 *src, u32 blkcount, u32 blksize)
169 u64 xfercount = blkcount * blksize;
170 struct pl180_mmc_host *host = dev->priv;
171 u32 status, status_err;
173 debug("write_bytes: blkcount=%u blksize=%u\n", blkcount, blksize);
175 status = readl(&host->base->status);
176 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
177 while (!status_err && xfercount) {
178 if (status & SDI_STA_TXFIFOBW) {
179 if (xfercount >= SDI_FIFO_BURST_SIZE * sizeof(u32)) {
180 for (i = 0; i < SDI_FIFO_BURST_SIZE; i++)
181 writel(*(tempbuff + i),
183 tempbuff += SDI_FIFO_BURST_SIZE;
184 xfercount -= SDI_FIFO_BURST_SIZE * sizeof(u32);
186 while (xfercount >= sizeof(u32)) {
187 writel(*(tempbuff), &host->base->fifo);
189 xfercount -= sizeof(u32);
193 status = readl(&host->base->status);
194 status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT);
197 status_err = status &
198 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
199 while (!status_err) {
200 status = readl(&host->base->status);
201 status_err = status &
202 (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND);
205 if (status & SDI_STA_DTIMEOUT) {
206 printf("Write data timed out, xfercount:%llu,status:0x%08X\n",
209 } else if (status & SDI_STA_DCRCFAIL) {
210 printf("Write data CRC error\n");
214 writel(SDI_ICR_MASK, &host->base->status_clear);
217 printf("Write data error, xfercount:%llu", xfercount);
224 static int do_data_transfer(struct mmc *dev,
226 struct mmc_data *data)
228 int error = -ETIMEDOUT;
229 struct pl180_mmc_host *host = dev->priv;
232 u32 data_len = (u32) (data->blocks * data->blocksize);
234 if (!host->version2) {
235 blksz = (ffs(data->blocksize) - 1);
236 data_ctrl |= ((blksz << 4) & SDI_DCTRL_DBLKSIZE_MASK);
238 blksz = data->blocksize;
239 data_ctrl |= (blksz << SDI_DCTRL_DBLOCKSIZE_V2_SHIFT);
241 data_ctrl |= SDI_DCTRL_DTEN | SDI_DCTRL_BUSYMODE;
243 writel(SDI_DTIMER_DEFAULT, &host->base->datatimer);
244 writel(data_len, &host->base->datalength);
245 udelay(DATA_REG_DELAY);
247 if (data->flags & MMC_DATA_READ) {
248 data_ctrl |= SDI_DCTRL_DTDIR_IN;
249 writel(data_ctrl, &host->base->datactrl);
251 error = do_command(dev, cmd);
255 error = read_bytes(dev, (u32 *)data->dest, (u32)data->blocks,
256 (u32)data->blocksize);
257 } else if (data->flags & MMC_DATA_WRITE) {
258 error = do_command(dev, cmd);
262 writel(data_ctrl, &host->base->datactrl);
263 error = write_bytes(dev, (u32 *)data->src, (u32)data->blocks,
264 (u32)data->blocksize);
270 static int host_request(struct mmc *dev,
272 struct mmc_data *data)
277 result = do_data_transfer(dev, cmd, data);
279 result = do_command(dev, cmd);
284 static int host_set_ios(struct mmc *dev)
286 struct pl180_mmc_host *host = dev->priv;
289 sdi_clkcr = readl(&host->base->clock);
291 /* Ramp up the clock rate */
296 if (dev->clock >= dev->cfg->f_max) {
298 dev->clock = dev->cfg->f_max;
300 clkdiv = (host->clock_in / dev->clock) - 2;
303 tmp_clock = host->clock_in / (clkdiv + 2);
304 while (tmp_clock > dev->clock) {
306 tmp_clock = host->clock_in / (clkdiv + 2);
309 if (clkdiv > SDI_CLKCR_CLKDIV_MASK)
310 clkdiv = SDI_CLKCR_CLKDIV_MASK;
312 tmp_clock = host->clock_in / (clkdiv + 2);
313 dev->clock = tmp_clock;
314 sdi_clkcr &= ~(SDI_CLKCR_CLKDIV_MASK);
318 /* Set the bus width */
319 if (dev->bus_width) {
322 switch (dev->bus_width) {
324 buswidth |= SDI_CLKCR_WIDBUS_1;
327 buswidth |= SDI_CLKCR_WIDBUS_4;
330 buswidth |= SDI_CLKCR_WIDBUS_8;
333 printf("Invalid bus width: %d\n", dev->bus_width);
336 sdi_clkcr &= ~(SDI_CLKCR_WIDBUS_MASK);
337 sdi_clkcr |= buswidth;
340 writel(sdi_clkcr, &host->base->clock);
341 udelay(CLK_CHANGE_DELAY);
346 #ifndef CONFIG_DM_MMC
347 /* MMC uses open drain drivers in the enumeration phase */
348 static int mmc_host_reset(struct mmc *dev)
350 struct pl180_mmc_host *host = dev->priv;
352 writel(host->pwr_init, &host->base->power);
357 static const struct mmc_ops arm_pl180_mmci_ops = {
358 .send_cmd = host_request,
359 .set_ios = host_set_ios,
360 .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.
369 int arm_pl180_mmci_init(struct pl180_mmc_host *host, struct mmc **mmc)
373 writel(host->pwr_init, &host->base->power);
374 writel(host->clkdiv_init, &host->base->clock);
375 udelay(CLK_CHANGE_DELAY);
377 /* Disable mmc interrupts */
378 sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK;
379 writel(sdi_u32, &host->base->mask0);
381 host->cfg.name = host->name;
382 #ifndef CONFIG_DM_MMC
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);
399 debug("registered mmc interface number is:%d\n",
400 (*mmc)->block_dev.devnum);
406 static int arm_pl180_mmc_probe(struct udevice *dev)
408 struct arm_pl180_mmc_plat *pdata = dev_get_platdata(dev);
409 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
410 struct mmc *mmc = &pdata->mmc;
411 struct pl180_mmc_host *host = mmc->priv;
416 ret = clk_get_by_index(dev, 0, &clk);
420 ret = clk_enable(&clk);
422 dev_err(dev, "failed to enable clock\n");
426 strcpy(host->name, "MMC");
427 host->pwr_init = INIT_PWR;
428 host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V1 | SDI_CLKCR_CLKEN |
430 host->voltages = VOLTAGE_WINDOW_SD;
432 host->clock_in = clk_get_rate(&clk);
433 host->clock_min = host->clock_in / (2 * (SDI_CLKCR_CLKDIV_INIT_V1 + 1));
434 host->clock_max = dev_read_u32_default(dev, "max-frequency",
436 host->version2 = dev_get_driver_data(dev);
438 bus_width = dev_read_u32_default(dev, "bus-width", 1);
441 host->caps |= MMC_MODE_8BIT;
442 /* Hosts capable of 8-bit transfers can also do 4 bits */
444 host->caps |= MMC_MODE_4BIT;
449 dev_err(dev, "Invalid bus-width value %u\n", bus_width);
452 ret = arm_pl180_mmci_init(host, &mmc);
454 dev_err(dev, "arm_pl180_mmci init failed\n");
465 static int dm_host_request(struct udevice *dev, struct mmc_cmd *cmd,
466 struct mmc_data *data)
468 struct mmc *mmc = mmc_get_mmc_dev(dev);
470 return host_request(mmc, cmd, data);
473 static int dm_host_set_ios(struct udevice *dev)
475 struct mmc *mmc = mmc_get_mmc_dev(dev);
477 return host_set_ios(mmc);
480 static const struct dm_mmc_ops arm_pl180_dm_mmc_ops = {
481 .send_cmd = dm_host_request,
482 .set_ios = dm_host_set_ios,
485 static int arm_pl180_mmc_ofdata_to_platdata(struct udevice *dev)
487 struct arm_pl180_mmc_plat *pdata = dev_get_platdata(dev);
488 struct mmc *mmc = &pdata->mmc;
489 struct pl180_mmc_host *host = mmc->priv;
492 addr = devfdt_get_addr(dev);
493 if (addr == FDT_ADDR_T_NONE)
496 host->base = (void *)addr;
501 static const struct udevice_id arm_pl180_mmc_match[] = {
502 { .compatible = "st,stm32f4xx-sdio", .data = VERSION1 },
506 U_BOOT_DRIVER(arm_pl180_mmc) = {
507 .name = "arm_pl180_mmc",
509 .of_match = arm_pl180_mmc_match,
510 .ops = &arm_pl180_dm_mmc_ops,
511 .probe = arm_pl180_mmc_probe,
512 .ofdata_to_platdata = arm_pl180_mmc_ofdata_to_platdata,
513 .priv_auto_alloc_size = sizeof(struct pl180_mmc_host),
514 .platdata_auto_alloc_size = sizeof(struct arm_pl180_mmc_plat),