1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
5 * Modified to add driver model (DM) support
6 * Copyright (C) 2019 Marcel Ziswiler <marcel@ziswiler.com>
8 * Loosely based on the old code and Linux's PXA MMC driver
12 #include <asm/arch/hardware.h>
13 #include <asm/arch/regs-mmc.h>
14 #include <linux/delay.h>
15 #include <linux/errno.h>
18 #include <dm/platform_data/pxa_mmc_gen.h>
22 /* PXAMMC Generic default config for various CPUs */
23 #if defined(CONFIG_CPU_PXA25X)
24 #define PXAMMC_FIFO_SIZE 1
25 #define PXAMMC_MIN_SPEED 312500
26 #define PXAMMC_MAX_SPEED 20000000
27 #define PXAMMC_HOST_CAPS (0)
28 #elif defined(CONFIG_CPU_PXA27X)
29 #define PXAMMC_CRC_SKIP
30 #define PXAMMC_FIFO_SIZE 32
31 #define PXAMMC_MIN_SPEED 304000
32 #define PXAMMC_MAX_SPEED 19500000
33 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT)
34 #elif defined(CONFIG_CPU_MONAHANS)
35 #define PXAMMC_FIFO_SIZE 32
36 #define PXAMMC_MIN_SPEED 304000
37 #define PXAMMC_MAX_SPEED 26000000
38 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT | MMC_MODE_HS)
40 #error "This CPU isn't supported by PXA MMC!"
43 #define MMC_STAT_ERRORS \
44 (MMC_STAT_RES_CRC_ERROR | MMC_STAT_SPI_READ_ERROR_TOKEN | \
45 MMC_STAT_CRC_READ_ERROR | MMC_STAT_TIME_OUT_RESPONSE | \
46 MMC_STAT_READ_TIME_OUT | MMC_STAT_CRC_WRITE_ERROR)
48 /* 1 millisecond (in wait cycles below it's 100 x 10uS waits) */
49 #define PXA_MMC_TIMEOUT 100
52 struct pxa_mmc_regs *regs;
55 /* Wait for bit to be set */
56 static int pxa_mmc_wait(struct mmc *mmc, uint32_t mask)
58 struct pxa_mmc_priv *priv = mmc->priv;
59 struct pxa_mmc_regs *regs = priv->regs;
60 unsigned int timeout = PXA_MMC_TIMEOUT;
62 /* Wait for bit to be set */
64 if (readl(®s->stat) & mask)
75 static int pxa_mmc_stop_clock(struct mmc *mmc)
77 struct pxa_mmc_priv *priv = mmc->priv;
78 struct pxa_mmc_regs *regs = priv->regs;
79 unsigned int timeout = PXA_MMC_TIMEOUT;
81 /* If the clock aren't running, exit */
82 if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
85 /* Tell the controller to turn off the clock */
86 writel(MMC_STRPCL_STOP_CLK, ®s->strpcl);
88 /* Wait until the clock are off */
90 if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
95 /* The clock refused to stop, scream and die a painful death */
99 /* The clock stopped correctly */
103 static int pxa_mmc_start_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
106 struct pxa_mmc_priv *priv = mmc->priv;
107 struct pxa_mmc_regs *regs = priv->regs;
110 /* The card can send a "busy" response */
111 if (cmd->resp_type & MMC_RSP_BUSY)
112 cmdat |= MMC_CMDAT_BUSY;
114 /* Inform the controller about response type */
115 switch (cmd->resp_type) {
118 cmdat |= MMC_CMDAT_R1;
121 cmdat |= MMC_CMDAT_R2;
124 cmdat |= MMC_CMDAT_R3;
130 /* Load command and it's arguments into the controller */
131 writel(cmd->cmdidx, ®s->cmd);
132 writel(cmd->cmdarg >> 16, ®s->argh);
133 writel(cmd->cmdarg & 0xffff, ®s->argl);
134 writel(cmdat, ®s->cmdat);
136 /* Start the controller clock and wait until they are started */
137 writel(MMC_STRPCL_START_CLK, ®s->strpcl);
139 ret = pxa_mmc_wait(mmc, MMC_STAT_CLK_EN);
143 /* Correct and happy end */
147 static int pxa_mmc_cmd_done(struct mmc *mmc, struct mmc_cmd *cmd)
149 struct pxa_mmc_priv *priv = mmc->priv;
150 struct pxa_mmc_regs *regs = priv->regs;
155 /* Read the controller status */
156 stat = readl(®s->stat);
160 * Did I mention this is Sick. We always need to
161 * discard the upper 8 bits of the first 16-bit word.
163 a = readl(®s->res) & 0xffff;
164 for (i = 0; i < 4; i++) {
165 b = readl(®s->res) & 0xffff;
166 c = readl(®s->res) & 0xffff;
167 cmd->response[i] = (a << 24) | (b << 8) | (c >> 8);
171 /* The command response didn't arrive */
172 if (stat & MMC_STAT_TIME_OUT_RESPONSE) {
174 } else if (stat & MMC_STAT_RES_CRC_ERROR &&
175 cmd->resp_type & MMC_RSP_CRC) {
176 #ifdef PXAMMC_CRC_SKIP
177 if (cmd->resp_type & MMC_RSP_136 &&
178 cmd->response[0] & (1 << 31))
179 printf("Ignoring CRC, this may be dangerous!\n");
185 /* The command response was successfully read */
189 static int pxa_mmc_do_read_xfer(struct mmc *mmc, struct mmc_data *data)
191 struct pxa_mmc_priv *priv = mmc->priv;
192 struct pxa_mmc_regs *regs = priv->regs;
194 u32 *buf = (uint32_t *)data->dest;
198 len = data->blocks * data->blocksize;
201 /* The controller has data ready */
202 if (readl(®s->i_reg) & MMC_I_REG_RXFIFO_RD_REQ) {
203 size = min(len, (uint32_t)PXAMMC_FIFO_SIZE);
207 /* Read data into the buffer */
209 *buf++ = readl(®s->rxfifo);
212 if (readl(®s->stat) & MMC_STAT_ERRORS)
216 /* Wait for the transmission-done interrupt */
217 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
224 static int pxa_mmc_do_write_xfer(struct mmc *mmc, struct mmc_data *data)
226 struct pxa_mmc_priv *priv = mmc->priv;
227 struct pxa_mmc_regs *regs = priv->regs;
229 u32 *buf = (uint32_t *)data->src;
233 len = data->blocks * data->blocksize;
236 /* The controller is ready to receive data */
237 if (readl(®s->i_reg) & MMC_I_REG_TXFIFO_WR_REQ) {
238 size = min(len, (uint32_t)PXAMMC_FIFO_SIZE);
243 writel(*buf++, ®s->txfifo);
245 if (min(len, (uint32_t)PXAMMC_FIFO_SIZE) < 32)
246 writel(MMC_PRTBUF_BUF_PART_FULL, ®s->prtbuf);
249 if (readl(®s->stat) & MMC_STAT_ERRORS)
253 /* Wait for the transmission-done interrupt */
254 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
258 /* Wait until the data are really written to the card */
259 ret = pxa_mmc_wait(mmc, MMC_STAT_PRG_DONE);
266 static int pxa_mmc_send_cmd_common(struct pxa_mmc_priv *priv, struct mmc *mmc,
267 struct mmc_cmd *cmd, struct mmc_data *data)
269 struct pxa_mmc_regs *regs = priv->regs;
273 /* Stop the controller */
274 ret = pxa_mmc_stop_clock(mmc);
278 /* If we're doing data transfer, configure the controller accordingly */
280 writel(data->blocks, ®s->nob);
281 writel(data->blocksize, ®s->blklen);
282 /* This delay can be optimized, but stick with max value */
283 writel(0xffff, ®s->rdto);
284 cmdat |= MMC_CMDAT_DATA_EN;
285 if (data->flags & MMC_DATA_WRITE)
286 cmdat |= MMC_CMDAT_WRITE;
289 /* Run in 4bit mode if the card can do it */
290 if (mmc->bus_width == 4)
291 cmdat |= MMC_CMDAT_SD_4DAT;
293 /* Execute the command */
294 ret = pxa_mmc_start_cmd(mmc, cmd, cmdat);
298 /* Wait until the command completes */
299 ret = pxa_mmc_wait(mmc, MMC_STAT_END_CMD_RES);
303 /* Read back the result */
304 ret = pxa_mmc_cmd_done(mmc, cmd);
308 /* In case there was a data transfer scheduled, do it */
310 if (data->flags & MMC_DATA_WRITE)
311 pxa_mmc_do_write_xfer(mmc, data);
313 pxa_mmc_do_read_xfer(mmc, data);
319 static int pxa_mmc_set_ios_common(struct pxa_mmc_priv *priv, struct mmc *mmc)
321 struct pxa_mmc_regs *regs = priv->regs;
326 pxa_mmc_stop_clock(mmc);
330 /* PXA3xx can do 26MHz with special settings. */
331 if (mmc->clock == 26000000) {
332 writel(0x7, ®s->clkrt);
336 /* Set clock to the card the usual way. */
338 tmp = mmc->cfg->f_max / mmc->clock;
346 writel(pxa_mmc_clock, ®s->clkrt);
351 static int pxa_mmc_init_common(struct pxa_mmc_priv *priv, struct mmc *mmc)
353 struct pxa_mmc_regs *regs = priv->regs;
355 /* Make sure the clock are stopped */
356 pxa_mmc_stop_clock(mmc);
358 /* Turn off SPI mode */
359 writel(0, ®s->spi);
361 /* Set up maximum timeout to wait for command response */
362 writel(MMC_RES_TO_MAX_MASK, ®s->resto);
364 /* Mask all interrupts */
365 writel(~(MMC_I_MASK_TXFIFO_WR_REQ | MMC_I_MASK_RXFIFO_RD_REQ),
371 #if !CONFIG_IS_ENABLED(DM_MMC)
372 static int pxa_mmc_init(struct mmc *mmc)
374 struct pxa_mmc_priv *priv = mmc->priv;
376 return pxa_mmc_init_common(priv, mmc);
379 static int pxa_mmc_request(struct mmc *mmc, struct mmc_cmd *cmd,
380 struct mmc_data *data)
382 struct pxa_mmc_priv *priv = mmc->priv;
384 return pxa_mmc_send_cmd_common(priv, mmc, cmd, data);
387 static int pxa_mmc_set_ios(struct mmc *mmc)
389 struct pxa_mmc_priv *priv = mmc->priv;
391 return pxa_mmc_set_ios_common(priv, mmc);
394 static const struct mmc_ops pxa_mmc_ops = {
395 .send_cmd = pxa_mmc_request,
396 .set_ios = pxa_mmc_set_ios,
397 .init = pxa_mmc_init,
400 static struct mmc_config pxa_mmc_cfg = {
403 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
404 .f_max = PXAMMC_MAX_SPEED,
405 .f_min = PXAMMC_MIN_SPEED,
406 .host_caps = PXAMMC_HOST_CAPS,
407 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
410 int pxa_mmc_register(int card_index)
413 struct pxa_mmc_priv *priv;
417 priv = malloc(sizeof(struct pxa_mmc_priv));
421 memset(priv, 0, sizeof(*priv));
423 switch (card_index) {
425 priv->regs = (struct pxa_mmc_regs *)MMC0_BASE;
428 priv->regs = (struct pxa_mmc_regs *)MMC1_BASE;
432 printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n",
437 #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
443 reg |= CKENA_12_MMC0 | CKENA_13_MMC1;
447 mmc = mmc_create(&pxa_mmc_cfg, priv);
458 #else /* !CONFIG_IS_ENABLED(DM_MMC) */
459 static int pxa_mmc_probe(struct udevice *dev)
461 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
462 struct pxa_mmc_plat *plat = dev_get_platdata(dev);
463 struct mmc_config *cfg = &plat->cfg;
464 struct mmc *mmc = &plat->mmc;
465 struct pxa_mmc_priv *priv = dev_get_priv(dev);
470 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
471 cfg->f_max = PXAMMC_MAX_SPEED;
472 cfg->f_min = PXAMMC_MIN_SPEED;
473 cfg->host_caps = PXAMMC_HOST_CAPS;
474 cfg->name = dev->name;
475 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
479 priv->regs = plat->base;
481 #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
487 reg |= CKENA_12_MMC0 | CKENA_13_MMC1;
491 return pxa_mmc_init_common(priv, mmc);
494 static int pxa_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
495 struct mmc_data *data)
497 struct pxa_mmc_plat *plat = dev_get_platdata(dev);
498 struct pxa_mmc_priv *priv = dev_get_priv(dev);
500 return pxa_mmc_send_cmd_common(priv, &plat->mmc, cmd, data);
503 static int pxa_mmc_set_ios(struct udevice *dev)
505 struct pxa_mmc_plat *plat = dev_get_platdata(dev);
506 struct pxa_mmc_priv *priv = dev_get_priv(dev);
508 return pxa_mmc_set_ios_common(priv, &plat->mmc);
511 static const struct dm_mmc_ops pxa_mmc_ops = {
513 .send_cmd = pxa_mmc_send_cmd,
514 .set_ios = pxa_mmc_set_ios,
517 #if CONFIG_IS_ENABLED(BLK)
518 static int pxa_mmc_bind(struct udevice *dev)
520 struct pxa_mmc_plat *plat = dev_get_platdata(dev);
522 return mmc_bind(dev, &plat->mmc, &plat->cfg);
526 U_BOOT_DRIVER(pxa_mmc) = {
527 #if CONFIG_IS_ENABLED(BLK)
528 .bind = pxa_mmc_bind,
533 .priv_auto_alloc_size = sizeof(struct pxa_mmc_priv),
534 .probe = pxa_mmc_probe,
536 #endif /* !CONFIG_IS_ENABLED(DM_MMC) */