1 // SPDX-License-Identifier: GPL-2.0+
3 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
5 * Loosely based on the old code and Linux's PXA MMC driver
9 #include <asm/arch/hardware.h>
10 #include <asm/arch/regs-mmc.h>
11 #include <linux/errno.h>
16 /* PXAMMC Generic default config for various CPUs */
17 #if defined(CONFIG_CPU_PXA25X)
18 #define PXAMMC_FIFO_SIZE 1
19 #define PXAMMC_MIN_SPEED 312500
20 #define PXAMMC_MAX_SPEED 20000000
21 #define PXAMMC_HOST_CAPS (0)
22 #elif defined(CONFIG_CPU_PXA27X)
23 #define PXAMMC_CRC_SKIP
24 #define PXAMMC_FIFO_SIZE 32
25 #define PXAMMC_MIN_SPEED 304000
26 #define PXAMMC_MAX_SPEED 19500000
27 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT)
28 #elif defined(CONFIG_CPU_MONAHANS)
29 #define PXAMMC_FIFO_SIZE 32
30 #define PXAMMC_MIN_SPEED 304000
31 #define PXAMMC_MAX_SPEED 26000000
32 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT | MMC_MODE_HS)
34 #error "This CPU isn't supported by PXA MMC!"
37 #define MMC_STAT_ERRORS \
38 (MMC_STAT_RES_CRC_ERROR | MMC_STAT_SPI_READ_ERROR_TOKEN | \
39 MMC_STAT_CRC_READ_ERROR | MMC_STAT_TIME_OUT_RESPONSE | \
40 MMC_STAT_READ_TIME_OUT | MMC_STAT_CRC_WRITE_ERROR)
42 /* 1 millisecond (in wait cycles below it's 100 x 10uS waits) */
43 #define PXA_MMC_TIMEOUT 100
46 struct pxa_mmc_regs *regs;
49 /* Wait for bit to be set */
50 static int pxa_mmc_wait(struct mmc *mmc, uint32_t mask)
52 struct pxa_mmc_priv *priv = mmc->priv;
53 struct pxa_mmc_regs *regs = priv->regs;
54 unsigned int timeout = PXA_MMC_TIMEOUT;
56 /* Wait for bit to be set */
58 if (readl(®s->stat) & mask)
69 static int pxa_mmc_stop_clock(struct mmc *mmc)
71 struct pxa_mmc_priv *priv = mmc->priv;
72 struct pxa_mmc_regs *regs = priv->regs;
73 unsigned int timeout = PXA_MMC_TIMEOUT;
75 /* If the clock aren't running, exit */
76 if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
79 /* Tell the controller to turn off the clock */
80 writel(MMC_STRPCL_STOP_CLK, ®s->strpcl);
82 /* Wait until the clock are off */
84 if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
89 /* The clock refused to stop, scream and die a painful death */
93 /* The clock stopped correctly */
97 static int pxa_mmc_start_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
100 struct pxa_mmc_priv *priv = mmc->priv;
101 struct pxa_mmc_regs *regs = priv->regs;
104 /* The card can send a "busy" response */
105 if (cmd->resp_type & MMC_RSP_BUSY)
106 cmdat |= MMC_CMDAT_BUSY;
108 /* Inform the controller about response type */
109 switch (cmd->resp_type) {
112 cmdat |= MMC_CMDAT_R1;
115 cmdat |= MMC_CMDAT_R2;
118 cmdat |= MMC_CMDAT_R3;
124 /* Load command and it's arguments into the controller */
125 writel(cmd->cmdidx, ®s->cmd);
126 writel(cmd->cmdarg >> 16, ®s->argh);
127 writel(cmd->cmdarg & 0xffff, ®s->argl);
128 writel(cmdat, ®s->cmdat);
130 /* Start the controller clock and wait until they are started */
131 writel(MMC_STRPCL_START_CLK, ®s->strpcl);
133 ret = pxa_mmc_wait(mmc, MMC_STAT_CLK_EN);
137 /* Correct and happy end */
141 static int pxa_mmc_cmd_done(struct mmc *mmc, struct mmc_cmd *cmd)
143 struct pxa_mmc_priv *priv = mmc->priv;
144 struct pxa_mmc_regs *regs = priv->regs;
149 /* Read the controller status */
150 stat = readl(®s->stat);
154 * Did I mention this is Sick. We always need to
155 * discard the upper 8 bits of the first 16-bit word.
157 a = readl(®s->res) & 0xffff;
158 for (i = 0; i < 4; i++) {
159 b = readl(®s->res) & 0xffff;
160 c = readl(®s->res) & 0xffff;
161 cmd->response[i] = (a << 24) | (b << 8) | (c >> 8);
165 /* The command response didn't arrive */
166 if (stat & MMC_STAT_TIME_OUT_RESPONSE)
168 else if (stat & MMC_STAT_RES_CRC_ERROR
169 && cmd->resp_type & MMC_RSP_CRC) {
170 #ifdef PXAMMC_CRC_SKIP
171 if (cmd->resp_type & MMC_RSP_136
172 && cmd->response[0] & (1 << 31))
173 printf("Ignoring CRC, this may be dangerous!\n");
179 /* The command response was successfully read */
183 static int pxa_mmc_do_read_xfer(struct mmc *mmc, struct mmc_data *data)
185 struct pxa_mmc_priv *priv = mmc->priv;
186 struct pxa_mmc_regs *regs = priv->regs;
188 uint32_t *buf = (uint32_t *)data->dest;
192 len = data->blocks * data->blocksize;
195 /* The controller has data ready */
196 if (readl(®s->i_reg) & MMC_I_REG_RXFIFO_RD_REQ) {
197 size = min(len, (uint32_t)PXAMMC_FIFO_SIZE);
201 /* Read data into the buffer */
203 *buf++ = readl(®s->rxfifo);
207 if (readl(®s->stat) & MMC_STAT_ERRORS)
211 /* Wait for the transmission-done interrupt */
212 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
219 static int pxa_mmc_do_write_xfer(struct mmc *mmc, struct mmc_data *data)
221 struct pxa_mmc_priv *priv = mmc->priv;
222 struct pxa_mmc_regs *regs = priv->regs;
224 uint32_t *buf = (uint32_t *)data->src;
228 len = data->blocks * data->blocksize;
231 /* The controller is ready to receive data */
232 if (readl(®s->i_reg) & MMC_I_REG_TXFIFO_WR_REQ) {
233 size = min(len, (uint32_t)PXAMMC_FIFO_SIZE);
238 writel(*buf++, ®s->txfifo);
240 if (min(len, (uint32_t)PXAMMC_FIFO_SIZE) < 32)
241 writel(MMC_PRTBUF_BUF_PART_FULL, ®s->prtbuf);
244 if (readl(®s->stat) & MMC_STAT_ERRORS)
248 /* Wait for the transmission-done interrupt */
249 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
253 /* Wait until the data are really written to the card */
254 ret = pxa_mmc_wait(mmc, MMC_STAT_PRG_DONE);
261 static int pxa_mmc_request(struct mmc *mmc, struct mmc_cmd *cmd,
262 struct mmc_data *data)
264 struct pxa_mmc_priv *priv = mmc->priv;
265 struct pxa_mmc_regs *regs = priv->regs;
269 /* Stop the controller */
270 ret = pxa_mmc_stop_clock(mmc);
274 /* If we're doing data transfer, configure the controller accordingly */
276 writel(data->blocks, ®s->nob);
277 writel(data->blocksize, ®s->blklen);
278 /* This delay can be optimized, but stick with max value */
279 writel(0xffff, ®s->rdto);
280 cmdat |= MMC_CMDAT_DATA_EN;
281 if (data->flags & MMC_DATA_WRITE)
282 cmdat |= MMC_CMDAT_WRITE;
285 /* Run in 4bit mode if the card can do it */
286 if (mmc->bus_width == 4)
287 cmdat |= MMC_CMDAT_SD_4DAT;
289 /* Execute the command */
290 ret = pxa_mmc_start_cmd(mmc, cmd, cmdat);
294 /* Wait until the command completes */
295 ret = pxa_mmc_wait(mmc, MMC_STAT_END_CMD_RES);
299 /* Read back the result */
300 ret = pxa_mmc_cmd_done(mmc, cmd);
304 /* In case there was a data transfer scheduled, do it */
306 if (data->flags & MMC_DATA_WRITE)
307 pxa_mmc_do_write_xfer(mmc, data);
309 pxa_mmc_do_read_xfer(mmc, data);
315 static int pxa_mmc_set_ios(struct mmc *mmc)
317 struct pxa_mmc_priv *priv = mmc->priv;
318 struct pxa_mmc_regs *regs = priv->regs;
320 uint32_t pxa_mmc_clock;
323 pxa_mmc_stop_clock(mmc);
327 /* PXA3xx can do 26MHz with special settings. */
328 if (mmc->clock == 26000000) {
329 writel(0x7, ®s->clkrt);
333 /* Set clock to the card the usual way. */
335 tmp = mmc->cfg->f_max / mmc->clock;
343 writel(pxa_mmc_clock, ®s->clkrt);
348 static int pxa_mmc_init(struct mmc *mmc)
350 struct pxa_mmc_priv *priv = mmc->priv;
351 struct pxa_mmc_regs *regs = priv->regs;
353 /* Make sure the clock are stopped */
354 pxa_mmc_stop_clock(mmc);
356 /* Turn off SPI mode */
357 writel(0, ®s->spi);
359 /* Set up maximum timeout to wait for command response */
360 writel(MMC_RES_TO_MAX_MASK, ®s->resto);
362 /* Mask all interrupts */
363 writel(~(MMC_I_MASK_TXFIFO_WR_REQ | MMC_I_MASK_RXFIFO_RD_REQ),
368 static const struct mmc_ops pxa_mmc_ops = {
369 .send_cmd = pxa_mmc_request,
370 .set_ios = pxa_mmc_set_ios,
371 .init = pxa_mmc_init,
374 static struct mmc_config pxa_mmc_cfg = {
377 .voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
378 .f_max = PXAMMC_MAX_SPEED,
379 .f_min = PXAMMC_MIN_SPEED,
380 .host_caps = PXAMMC_HOST_CAPS,
381 .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT,
384 int pxa_mmc_register(int card_index)
387 struct pxa_mmc_priv *priv;
391 priv = malloc(sizeof(struct pxa_mmc_priv));
395 memset(priv, 0, sizeof(*priv));
397 switch (card_index) {
399 priv->regs = (struct pxa_mmc_regs *)MMC0_BASE;
402 priv->regs = (struct pxa_mmc_regs *)MMC1_BASE;
406 printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n",
411 #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
417 reg |= CKENA_12_MMC0 | CKENA_13_MMC1;
421 mmc = mmc_create(&pxa_mmc_cfg, priv);