1 // SPDX-License-Identifier: GPL-2.0+
3 * Marvell MMC/SD/SDIO driver
5 * (C) Copyright 2012-2014
6 * Marvell Semiconductor <www.marvell.com>
7 * Written-by: Maen Suleiman, Gerald Kerma
18 #include <asm/arch/cpu.h>
19 #include <asm/arch/soc.h>
20 #include <mvebu_mmc.h>
21 #include <dm/device_compat.h>
23 #define MVEBU_TARGET_DRAM 0
25 #define TIMEOUT_DELAY 5*CONFIG_SYS_HZ /* wait 5 seconds */
27 static inline void *get_regbase(const struct mmc *mmc)
29 struct mvebu_mmc_plat *pdata = mmc->priv;
34 static void mvebu_mmc_write(const struct mmc *mmc, u32 offs, u32 val)
36 writel(val, get_regbase(mmc) + (offs));
39 static u32 mvebu_mmc_read(const struct mmc *mmc, u32 offs)
41 return readl(get_regbase(mmc) + (offs));
44 static int mvebu_mmc_setup_data(struct udevice *dev, struct mmc_data *data)
46 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
47 struct mmc *mmc = &pdata->mmc;
50 dev_dbg(dev, "data %s : blocks=%d blksz=%d\n",
51 (data->flags & MMC_DATA_READ) ? "read" : "write",
52 data->blocks, data->blocksize);
54 /* default to maximum timeout */
55 ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL);
56 ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
57 mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg);
59 if (data->flags & MMC_DATA_READ) {
60 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_LOW, (u32)data->dest & 0xffff);
61 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_HI, (u32)data->dest >> 16);
63 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_LOW, (u32)data->src & 0xffff);
64 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_HI, (u32)data->src >> 16);
67 mvebu_mmc_write(mmc, SDIO_BLK_COUNT, data->blocks);
68 mvebu_mmc_write(mmc, SDIO_BLK_SIZE, data->blocksize);
73 static int mvebu_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
74 struct mmc_data *data)
81 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
82 struct mmc *mmc = &pdata->mmc;
84 dev_dbg(dev, "cmdidx [0x%x] resp_type[0x%x] cmdarg[0x%x]\n",
85 cmd->cmdidx, cmd->resp_type, cmd->cmdarg);
87 dev_dbg(dev, "cmd %d (hw state 0x%04x)\n",
88 cmd->cmdidx, mvebu_mmc_read(mmc, SDIO_HW_STATE));
91 * Hardware weirdness. The FIFO_EMPTY bit of the HW_STATE
92 * register is sometimes not set before a while when some
93 * "unusual" data block sizes are used (such as with the SWITCH
94 * command), even despite the fact that the XFER_DONE interrupt
95 * was raised. And if another data transfer starts before
96 * this bit comes to good sense (which eventually happens by
97 * itself) then the new transfer simply fails with a timeout.
99 if (!(mvebu_mmc_read(mmc, SDIO_HW_STATE) & CMD_FIFO_EMPTY)) {
100 ushort hw_state, count = 0;
102 start = get_timer(0);
104 hw_state = mvebu_mmc_read(mmc, SDIO_HW_STATE);
105 if ((get_timer(0) - start) > TIMEOUT_DELAY) {
106 printf("%s : FIFO_EMPTY bit missing\n",
111 } while (!(hw_state & CMD_FIFO_EMPTY));
112 dev_dbg(dev, "*** wait for FIFO_EMPTY bit (hw=0x%04x, count=%d, jiffies=%ld)\n",
113 hw_state, count, (get_timer(0) - (start)));
117 mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
118 mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
120 resptype = SDIO_CMD_INDEX(cmd->cmdidx);
122 /* Analyzing resptype/xfertype/waittype for the command */
123 if (cmd->resp_type & MMC_RSP_BUSY)
124 resptype |= SDIO_CMD_RSP_48BUSY;
125 else if (cmd->resp_type & MMC_RSP_136)
126 resptype |= SDIO_CMD_RSP_136;
127 else if (cmd->resp_type & MMC_RSP_PRESENT)
128 resptype |= SDIO_CMD_RSP_48;
130 resptype |= SDIO_CMD_RSP_NONE;
132 if (cmd->resp_type & MMC_RSP_CRC)
133 resptype |= SDIO_CMD_CHECK_CMDCRC;
135 if (cmd->resp_type & MMC_RSP_OPCODE)
136 resptype |= SDIO_CMD_INDX_CHECK;
138 if (cmd->resp_type & MMC_RSP_PRESENT) {
139 resptype |= SDIO_UNEXPECTED_RESP;
140 waittype |= SDIO_NOR_UNEXP_RSP;
144 int err = mvebu_mmc_setup_data(dev, data);
147 dev_dbg(dev, "command DATA error :%x\n", err);
151 resptype |= SDIO_CMD_DATA_PRESENT | SDIO_CMD_CHECK_DATACRC16;
152 xfertype |= SDIO_XFER_MODE_HW_WR_DATA_EN;
153 if (data->flags & MMC_DATA_READ) {
154 xfertype |= SDIO_XFER_MODE_TO_HOST;
155 waittype = SDIO_NOR_DMA_INI;
157 waittype |= SDIO_NOR_XFER_DONE;
160 waittype |= SDIO_NOR_CMD_DONE;
163 /* Setting cmd arguments */
164 mvebu_mmc_write(mmc, SDIO_ARG_LOW, cmd->cmdarg & 0xffff);
165 mvebu_mmc_write(mmc, SDIO_ARG_HI, cmd->cmdarg >> 16);
167 /* Setting Xfer mode */
168 mvebu_mmc_write(mmc, SDIO_XFER_MODE, xfertype);
170 /* Sending command */
171 mvebu_mmc_write(mmc, SDIO_CMD, resptype);
173 start = get_timer(0);
175 while (!((mvebu_mmc_read(mmc, SDIO_NOR_INTR_STATUS)) & waittype)) {
176 if (mvebu_mmc_read(mmc, SDIO_NOR_INTR_STATUS) & SDIO_NOR_ERROR) {
177 dev_dbg(dev, "error! cmdidx : %d, err reg: %04x\n",
179 mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS));
180 if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) &
181 (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT)) {
182 dev_dbg(dev, "command READ timed out\n");
185 dev_dbg(dev, "command READ error\n");
189 if ((get_timer(0) - start) > TIMEOUT_DELAY) {
190 dev_dbg(dev, "command timed out\n");
195 /* Handling response */
196 if (cmd->resp_type & MMC_RSP_136) {
199 for (resp_indx = 0; resp_indx < 8; resp_indx++)
200 response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx));
202 cmd->response[0] = ((response[0] & 0x03ff) << 22) |
203 ((response[1] & 0xffff) << 6) |
204 ((response[2] & 0xfc00) >> 10);
205 cmd->response[1] = ((response[2] & 0x03ff) << 22) |
206 ((response[3] & 0xffff) << 6) |
207 ((response[4] & 0xfc00) >> 10);
208 cmd->response[2] = ((response[4] & 0x03ff) << 22) |
209 ((response[5] & 0xffff) << 6) |
210 ((response[6] & 0xfc00) >> 10);
211 cmd->response[3] = ((response[6] & 0x03ff) << 22) |
212 ((response[7] & 0x3fff) << 8);
213 } else if (cmd->resp_type & MMC_RSP_PRESENT) {
216 for (resp_indx = 0; resp_indx < 3; resp_indx++)
217 response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx));
219 cmd->response[0] = ((response[2] & 0x003f) << (8 - 8)) |
220 ((response[1] & 0xffff) << (14 - 8)) |
221 ((response[0] & 0x03ff) << (30 - 8));
222 cmd->response[1] = ((response[0] & 0xfc00) >> 10);
223 cmd->response[2] = 0;
224 cmd->response[3] = 0;
226 cmd->response[0] = 0;
227 cmd->response[1] = 0;
228 cmd->response[2] = 0;
229 cmd->response[3] = 0;
232 dev_dbg(dev, "resp[0x%x] ", cmd->resp_type);
233 debug("[0x%x] ", cmd->response[0]);
234 debug("[0x%x] ", cmd->response[1]);
235 debug("[0x%x] ", cmd->response[2]);
236 debug("[0x%x] ", cmd->response[3]);
239 if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) &
240 (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT))
246 static void mvebu_mmc_power_up(struct udevice *dev)
248 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
249 struct mmc *mmc = &pdata->mmc;
251 dev_dbg(dev, "power up\n");
253 /* disable interrupts */
254 mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0);
255 mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0);
258 mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW);
260 mvebu_mmc_write(mmc, SDIO_XFER_MODE, 0);
263 mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
264 mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
266 /* enable interrupts status */
267 mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
268 mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
271 static void mvebu_mmc_set_clk(struct udevice *dev, unsigned int clock)
274 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
275 struct mmc *mmc = &pdata->mmc;
278 dev_dbg(dev, "clock off\n");
279 mvebu_mmc_write(mmc, SDIO_XFER_MODE, SDIO_XFER_MODE_STOP_CLK);
280 mvebu_mmc_write(mmc, SDIO_CLK_DIV, MVEBU_MMC_BASE_DIV_MAX);
282 m = MVEBU_MMC_BASE_FAST_CLOCK/(2*clock) - 1;
283 if (m > MVEBU_MMC_BASE_DIV_MAX)
284 m = MVEBU_MMC_BASE_DIV_MAX;
285 mvebu_mmc_write(mmc, SDIO_CLK_DIV, m & MVEBU_MMC_BASE_DIV_MAX);
286 dev_dbg(dev, "clock (%d) div : %d\n", clock, m);
290 static void mvebu_mmc_set_bus(struct udevice *dev, unsigned int bus)
292 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
293 struct mmc *mmc = &pdata->mmc;
296 ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL);
297 ctrl_reg &= ~SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
301 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
305 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_1_BIT;
308 /* default transfer mode */
309 ctrl_reg |= SDIO_HOST_CTRL_BIG_ENDIAN;
310 ctrl_reg &= ~SDIO_HOST_CTRL_LSB_FIRST;
312 /* default to maximum timeout */
313 ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
314 ctrl_reg |= SDIO_HOST_CTRL_TMOUT_EN;
316 ctrl_reg |= SDIO_HOST_CTRL_PUSH_PULL_EN;
318 ctrl_reg |= SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY;
320 dev_dbg(dev, "ctrl 0x%04x: %s %s %s\n", ctrl_reg,
321 (ctrl_reg & SDIO_HOST_CTRL_PUSH_PULL_EN) ?
322 "push-pull" : "open-drain",
323 (ctrl_reg & SDIO_HOST_CTRL_DATA_WIDTH_4_BITS) ?
324 "4bit-width" : "1bit-width",
325 (ctrl_reg & SDIO_HOST_CTRL_HI_SPEED_EN) ?
328 mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg);
331 static int mvebu_mmc_set_ios(struct udevice *dev)
333 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
334 struct mmc *mmc = &pdata->mmc;
336 dev_dbg(dev, "bus[%d] clock[%d]\n",
337 mmc->bus_width, mmc->clock);
338 mvebu_mmc_set_bus(dev, mmc->bus_width);
339 mvebu_mmc_set_clk(dev, mmc->clock);
345 * Set window register.
347 static void mvebu_window_setup(const struct mmc *mmc)
351 for (i = 0; i < 4; i++) {
352 mvebu_mmc_write(mmc, WINDOW_CTRL(i), 0);
353 mvebu_mmc_write(mmc, WINDOW_BASE(i), 0);
355 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
356 u32 size, base, attrib;
358 /* Enable DRAM bank */
361 attrib = KWCPU_ATTR_DRAM_CS0;
364 attrib = KWCPU_ATTR_DRAM_CS1;
367 attrib = KWCPU_ATTR_DRAM_CS2;
370 attrib = KWCPU_ATTR_DRAM_CS3;
373 /* invalide bank, disable access */
378 size = gd->bd->bi_dram[i].size;
379 base = gd->bd->bi_dram[i].start;
380 if (size && attrib) {
381 mvebu_mmc_write(mmc, WINDOW_CTRL(i),
382 MVCPU_WIN_CTRL_DATA(size,
387 mvebu_mmc_write(mmc, WINDOW_CTRL(i), MVCPU_WIN_DISABLE);
389 mvebu_mmc_write(mmc, WINDOW_BASE(i), base);
393 static int mvebu_mmc_initialize(struct udevice *dev)
395 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
396 struct mmc *mmc = &pdata->mmc;
398 dev_dbg(dev, "%s\n", __func__);
401 * Setting host parameters
402 * Initial Host Ctrl : Timeout : max , Normal Speed mode,
403 * 4-bit data mode, Big Endian, SD memory Card, Push_pull CMD Line
405 mvebu_mmc_write(mmc, SDIO_HOST_CTRL,
406 SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX) |
407 SDIO_HOST_CTRL_DATA_WIDTH_4_BITS |
408 SDIO_HOST_CTRL_BIG_ENDIAN |
409 SDIO_HOST_CTRL_PUSH_PULL_EN |
410 SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY);
412 mvebu_mmc_write(mmc, SDIO_CLK_CTRL, 0);
415 mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
416 mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
418 /* disable interrupts */
419 mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0);
420 mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0);
422 mvebu_window_setup(mmc);
425 mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW);
430 static int mvebu_mmc_of_to_plat(struct udevice *dev)
432 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
435 addr = dev_read_addr(dev);
436 if (addr == FDT_ADDR_T_NONE)
439 pdata->iobase = (void *)addr;
444 static int mvebu_mmc_probe(struct udevice *dev)
446 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
447 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
448 struct mmc *mmc = &pdata->mmc;
449 struct mmc_config *cfg = &pdata->cfg;
451 cfg->name = dev->name;
452 cfg->f_min = MVEBU_MMC_BASE_FAST_CLOCK / MVEBU_MMC_BASE_DIV_MAX;
453 cfg->f_max = MVEBU_MMC_CLOCKRATE_MAX;
454 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
455 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz;
456 cfg->part_type = PART_TYPE_DOS;
457 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
464 mvebu_mmc_power_up(dev);
465 mvebu_mmc_initialize(dev);
470 static const struct dm_mmc_ops mvebu_dm_mmc_ops = {
471 .send_cmd = mvebu_mmc_send_cmd,
472 .set_ios = mvebu_mmc_set_ios,
475 static int mvebu_mmc_bind(struct udevice *dev)
477 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
479 return mmc_bind(dev, &pdata->mmc, &pdata->cfg);
482 static const struct udevice_id mvebu_mmc_match[] = {
483 { .compatible = "marvell,orion-sdio" },
487 U_BOOT_DRIVER(mvebu_mmc) = {
490 .of_match = mvebu_mmc_match,
491 .ops = &mvebu_dm_mmc_ops,
492 .probe = mvebu_mmc_probe,
493 .bind = mvebu_mmc_bind,
494 .of_to_plat = mvebu_mmc_of_to_plat,
495 .plat_auto = sizeof(struct mvebu_mmc_plat),