2 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
4 * Loosely based on the old code and Linux's PXA MMC driver
6 * SPDX-License-Identifier: GPL-2.0+
14 #include <asm/errno.h>
15 #include <asm/arch/hardware.h>
16 #include <asm/arch/regs-mmc.h>
19 /* PXAMMC Generic default config for various CPUs */
20 #if defined(CONFIG_CPU_PXA25X)
21 #define PXAMMC_FIFO_SIZE 1
22 #define PXAMMC_MIN_SPEED 312500
23 #define PXAMMC_MAX_SPEED 20000000
24 #define PXAMMC_HOST_CAPS (0)
25 #elif defined(CONFIG_CPU_PXA27X)
26 #define PXAMMC_CRC_SKIP
27 #define PXAMMC_FIFO_SIZE 32
28 #define PXAMMC_MIN_SPEED 304000
29 #define PXAMMC_MAX_SPEED 19500000
30 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT)
31 #elif defined(CONFIG_CPU_MONAHANS)
32 #define PXAMMC_FIFO_SIZE 32
33 #define PXAMMC_MIN_SPEED 304000
34 #define PXAMMC_MAX_SPEED 26000000
35 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT | MMC_MODE_HS)
37 #error "This CPU isn't supported by PXA MMC!"
40 #define MMC_STAT_ERRORS \
41 (MMC_STAT_RES_CRC_ERROR | MMC_STAT_SPI_READ_ERROR_TOKEN | \
42 MMC_STAT_CRC_READ_ERROR | MMC_STAT_TIME_OUT_RESPONSE | \
43 MMC_STAT_READ_TIME_OUT | MMC_STAT_CRC_WRITE_ERROR)
45 /* 1 millisecond (in wait cycles below it's 100 x 10uS waits) */
46 #define PXA_MMC_TIMEOUT 100
49 struct pxa_mmc_regs
*regs
;
52 /* Wait for bit to be set */
53 static int pxa_mmc_wait(struct mmc
*mmc
, uint32_t mask
)
55 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
56 struct pxa_mmc_regs
*regs
= priv
->regs
;
57 unsigned int timeout
= PXA_MMC_TIMEOUT
;
59 /* Wait for bit to be set */
61 if (readl(®s
->stat
) & mask
)
72 static int pxa_mmc_stop_clock(struct mmc
*mmc
)
74 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
75 struct pxa_mmc_regs
*regs
= priv
->regs
;
76 unsigned int timeout
= PXA_MMC_TIMEOUT
;
78 /* If the clock aren't running, exit */
79 if (!(readl(®s
->stat
) & MMC_STAT_CLK_EN
))
82 /* Tell the controller to turn off the clock */
83 writel(MMC_STRPCL_STOP_CLK
, ®s
->strpcl
);
85 /* Wait until the clock are off */
87 if (!(readl(®s
->stat
) & MMC_STAT_CLK_EN
))
92 /* The clock refused to stop, scream and die a painful death */
96 /* The clock stopped correctly */
100 static int pxa_mmc_start_cmd(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
103 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
104 struct pxa_mmc_regs
*regs
= priv
->regs
;
107 /* The card can send a "busy" response */
108 if (cmd
->resp_type
& MMC_RSP_BUSY
)
109 cmdat
|= MMC_CMDAT_BUSY
;
111 /* Inform the controller about response type */
112 switch (cmd
->resp_type
) {
115 cmdat
|= MMC_CMDAT_R1
;
118 cmdat
|= MMC_CMDAT_R2
;
121 cmdat
|= MMC_CMDAT_R3
;
127 /* Load command and it's arguments into the controller */
128 writel(cmd
->cmdidx
, ®s
->cmd
);
129 writel(cmd
->cmdarg
>> 16, ®s
->argh
);
130 writel(cmd
->cmdarg
& 0xffff, ®s
->argl
);
131 writel(cmdat
, ®s
->cmdat
);
133 /* Start the controller clock and wait until they are started */
134 writel(MMC_STRPCL_START_CLK
, ®s
->strpcl
);
136 ret
= pxa_mmc_wait(mmc
, MMC_STAT_CLK_EN
);
140 /* Correct and happy end */
144 static int pxa_mmc_cmd_done(struct mmc
*mmc
, struct mmc_cmd
*cmd
)
146 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
147 struct pxa_mmc_regs
*regs
= priv
->regs
;
152 /* Read the controller status */
153 stat
= readl(®s
->stat
);
157 * Did I mention this is Sick. We always need to
158 * discard the upper 8 bits of the first 16-bit word.
160 a
= readl(®s
->res
) & 0xffff;
161 for (i
= 0; i
< 4; i
++) {
162 b
= readl(®s
->res
) & 0xffff;
163 c
= readl(®s
->res
) & 0xffff;
164 cmd
->response
[i
] = (a
<< 24) | (b
<< 8) | (c
>> 8);
168 /* The command response didn't arrive */
169 if (stat
& MMC_STAT_TIME_OUT_RESPONSE
)
171 else if (stat
& MMC_STAT_RES_CRC_ERROR
172 && cmd
->resp_type
& MMC_RSP_CRC
) {
173 #ifdef PXAMMC_CRC_SKIP
174 if (cmd
->resp_type
& MMC_RSP_136
175 && cmd
->response
[0] & (1 << 31))
176 printf("Ignoring CRC, this may be dangerous!\n");
182 /* The command response was successfully read */
186 static int pxa_mmc_do_read_xfer(struct mmc
*mmc
, struct mmc_data
*data
)
188 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
189 struct pxa_mmc_regs
*regs
= priv
->regs
;
191 uint32_t *buf
= (uint32_t *)data
->dest
;
195 len
= data
->blocks
* data
->blocksize
;
198 /* The controller has data ready */
199 if (readl(®s
->i_reg
) & MMC_I_REG_RXFIFO_RD_REQ
) {
200 size
= min(len
, PXAMMC_FIFO_SIZE
);
204 /* Read data into the buffer */
206 *buf
++ = readl(®s
->rxfifo
);
210 if (readl(®s
->stat
) & MMC_STAT_ERRORS
)
214 /* Wait for the transmission-done interrupt */
215 ret
= pxa_mmc_wait(mmc
, MMC_STAT_DATA_TRAN_DONE
);
222 static int pxa_mmc_do_write_xfer(struct mmc
*mmc
, struct mmc_data
*data
)
224 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
225 struct pxa_mmc_regs
*regs
= priv
->regs
;
227 uint32_t *buf
= (uint32_t *)data
->src
;
231 len
= data
->blocks
* data
->blocksize
;
234 /* The controller is ready to receive data */
235 if (readl(®s
->i_reg
) & MMC_I_REG_TXFIFO_WR_REQ
) {
236 size
= min(len
, PXAMMC_FIFO_SIZE
);
241 writel(*buf
++, ®s
->txfifo
);
243 if (min(len
, PXAMMC_FIFO_SIZE
) < 32)
244 writel(MMC_PRTBUF_BUF_PART_FULL
, ®s
->prtbuf
);
247 if (readl(®s
->stat
) & MMC_STAT_ERRORS
)
251 /* Wait for the transmission-done interrupt */
252 ret
= pxa_mmc_wait(mmc
, MMC_STAT_DATA_TRAN_DONE
);
256 /* Wait until the data are really written to the card */
257 ret
= pxa_mmc_wait(mmc
, MMC_STAT_PRG_DONE
);
264 static int pxa_mmc_request(struct mmc
*mmc
, struct mmc_cmd
*cmd
,
265 struct mmc_data
*data
)
267 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
268 struct pxa_mmc_regs
*regs
= priv
->regs
;
272 /* Stop the controller */
273 ret
= pxa_mmc_stop_clock(mmc
);
277 /* If we're doing data transfer, configure the controller accordingly */
279 writel(data
->blocks
, ®s
->nob
);
280 writel(data
->blocksize
, ®s
->blklen
);
281 /* This delay can be optimized, but stick with max value */
282 writel(0xffff, ®s
->rdto
);
283 cmdat
|= MMC_CMDAT_DATA_EN
;
284 if (data
->flags
& MMC_DATA_WRITE
)
285 cmdat
|= MMC_CMDAT_WRITE
;
288 /* Run in 4bit mode if the card can do it */
289 if (mmc
->bus_width
== 4)
290 cmdat
|= MMC_CMDAT_SD_4DAT
;
292 /* Execute the command */
293 ret
= pxa_mmc_start_cmd(mmc
, cmd
, cmdat
);
297 /* Wait until the command completes */
298 ret
= pxa_mmc_wait(mmc
, MMC_STAT_END_CMD_RES
);
302 /* Read back the result */
303 ret
= pxa_mmc_cmd_done(mmc
, cmd
);
307 /* In case there was a data transfer scheduled, do it */
309 if (data
->flags
& MMC_DATA_WRITE
)
310 pxa_mmc_do_write_xfer(mmc
, data
);
312 pxa_mmc_do_read_xfer(mmc
, data
);
318 static void pxa_mmc_set_ios(struct mmc
*mmc
)
320 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
321 struct pxa_mmc_regs
*regs
= priv
->regs
;
323 uint32_t pxa_mmc_clock
;
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
->f_max
/ mmc
->clock
;
346 writel(pxa_mmc_clock
, ®s
->clkrt
);
349 static int pxa_mmc_init(struct mmc
*mmc
)
351 struct pxa_mmc_priv
*priv
= (struct pxa_mmc_priv
*)mmc
->priv
;
352 struct pxa_mmc_regs
*regs
= priv
->regs
;
354 /* Make sure the clock are stopped */
355 pxa_mmc_stop_clock(mmc
);
357 /* Turn off SPI mode */
358 writel(0, ®s
->spi
);
360 /* Set up maximum timeout to wait for command response */
361 writel(MMC_RES_TO_MAX_MASK
, ®s
->resto
);
363 /* Mask all interrupts */
364 writel(~(MMC_I_MASK_TXFIFO_WR_REQ
| MMC_I_MASK_RXFIFO_RD_REQ
),
369 int pxa_mmc_register(int card_index
)
372 struct pxa_mmc_priv
*priv
;
376 mmc
= malloc(sizeof(struct mmc
));
380 priv
= malloc(sizeof(struct pxa_mmc_priv
));
384 switch (card_index
) {
386 priv
->regs
= (struct pxa_mmc_regs
*)MMC0_BASE
;
389 priv
->regs
= (struct pxa_mmc_regs
*)MMC1_BASE
;
392 printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n",
399 sprintf(mmc
->name
, "PXA MMC");
400 mmc
->send_cmd
= pxa_mmc_request
;
401 mmc
->set_ios
= pxa_mmc_set_ios
;
402 mmc
->init
= pxa_mmc_init
;
405 mmc
->voltages
= MMC_VDD_32_33
| MMC_VDD_33_34
;
406 mmc
->f_max
= PXAMMC_MAX_SPEED
;
407 mmc
->f_min
= PXAMMC_MIN_SPEED
;
408 mmc
->host_caps
= PXAMMC_HOST_CAPS
;
412 #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
418 reg
|= CKENA_12_MMC0
| CKENA_13_MMC1
;