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
19 #include <asm/arch/cpu.h>
20 #include <asm/arch/soc.h>
21 #include <mvebu_mmc.h>
22 #include <dm/device_compat.h>
24 #define MVEBU_TARGET_DRAM 0
26 #define TIMEOUT_DELAY 5*CONFIG_SYS_HZ /* wait 5 seconds */
28 static inline void *get_regbase(const struct mmc *mmc)
30 struct mvebu_mmc_plat *pdata = mmc->priv;
35 static void mvebu_mmc_write(const struct mmc *mmc, u32 offs, u32 val)
37 writel(val, get_regbase(mmc) + (offs));
40 static u32 mvebu_mmc_read(const struct mmc *mmc, u32 offs)
42 return readl(get_regbase(mmc) + (offs));
45 static int mvebu_mmc_setup_data(struct udevice *dev, struct mmc_data *data)
47 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
48 struct mmc *mmc = &pdata->mmc;
51 dev_dbg(dev, "data %s : blocks=%d blksz=%d\n",
52 (data->flags & MMC_DATA_READ) ? "read" : "write",
53 data->blocks, data->blocksize);
55 /* default to maximum timeout */
56 ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL);
57 ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
58 mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg);
60 if (data->flags & MMC_DATA_READ) {
61 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_LOW, (u32)data->dest & 0xffff);
62 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_HI, (u32)data->dest >> 16);
64 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_LOW, (u32)data->src & 0xffff);
65 mvebu_mmc_write(mmc, SDIO_SYS_ADDR_HI, (u32)data->src >> 16);
68 mvebu_mmc_write(mmc, SDIO_BLK_COUNT, data->blocks);
69 mvebu_mmc_write(mmc, SDIO_BLK_SIZE, data->blocksize);
74 static int mvebu_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
75 struct mmc_data *data)
82 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
83 struct mmc *mmc = &pdata->mmc;
85 dev_dbg(dev, "cmdidx [0x%x] resp_type[0x%x] cmdarg[0x%x]\n",
86 cmd->cmdidx, cmd->resp_type, cmd->cmdarg);
88 dev_dbg(dev, "cmd %d (hw state 0x%04x)\n",
89 cmd->cmdidx, mvebu_mmc_read(mmc, SDIO_HW_STATE));
92 * Hardware weirdness. The FIFO_EMPTY bit of the HW_STATE
93 * register is sometimes not set before a while when some
94 * "unusual" data block sizes are used (such as with the SWITCH
95 * command), even despite the fact that the XFER_DONE interrupt
96 * was raised. And if another data transfer starts before
97 * this bit comes to good sense (which eventually happens by
98 * itself) then the new transfer simply fails with a timeout.
100 if (!(mvebu_mmc_read(mmc, SDIO_HW_STATE) & CMD_FIFO_EMPTY)) {
101 ushort hw_state, count = 0;
103 start = get_timer(0);
105 hw_state = mvebu_mmc_read(mmc, SDIO_HW_STATE);
106 if ((get_timer(0) - start) > TIMEOUT_DELAY) {
107 printf("%s : FIFO_EMPTY bit missing\n",
112 } while (!(hw_state & CMD_FIFO_EMPTY));
113 dev_dbg(dev, "*** wait for FIFO_EMPTY bit (hw=0x%04x, count=%d, jiffies=%ld)\n",
114 hw_state, count, (get_timer(0) - (start)));
118 mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
119 mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
121 resptype = SDIO_CMD_INDEX(cmd->cmdidx);
123 /* Analyzing resptype/xfertype/waittype for the command */
124 if (cmd->resp_type & MMC_RSP_BUSY)
125 resptype |= SDIO_CMD_RSP_48BUSY;
126 else if (cmd->resp_type & MMC_RSP_136)
127 resptype |= SDIO_CMD_RSP_136;
128 else if (cmd->resp_type & MMC_RSP_PRESENT)
129 resptype |= SDIO_CMD_RSP_48;
131 resptype |= SDIO_CMD_RSP_NONE;
133 if (cmd->resp_type & MMC_RSP_CRC)
134 resptype |= SDIO_CMD_CHECK_CMDCRC;
136 if (cmd->resp_type & MMC_RSP_OPCODE)
137 resptype |= SDIO_CMD_INDX_CHECK;
139 if (cmd->resp_type & MMC_RSP_PRESENT) {
140 resptype |= SDIO_UNEXPECTED_RESP;
141 waittype |= SDIO_NOR_UNEXP_RSP;
145 int err = mvebu_mmc_setup_data(dev, data);
148 dev_dbg(dev, "command DATA error :%x\n", err);
152 resptype |= SDIO_CMD_DATA_PRESENT | SDIO_CMD_CHECK_DATACRC16;
153 xfertype |= SDIO_XFER_MODE_HW_WR_DATA_EN;
154 if (data->flags & MMC_DATA_READ) {
155 xfertype |= SDIO_XFER_MODE_TO_HOST;
156 waittype = SDIO_NOR_DMA_INI;
158 waittype |= SDIO_NOR_XFER_DONE;
161 waittype |= SDIO_NOR_CMD_DONE;
164 /* Setting cmd arguments */
165 mvebu_mmc_write(mmc, SDIO_ARG_LOW, cmd->cmdarg & 0xffff);
166 mvebu_mmc_write(mmc, SDIO_ARG_HI, cmd->cmdarg >> 16);
168 /* Setting Xfer mode */
169 mvebu_mmc_write(mmc, SDIO_XFER_MODE, xfertype);
171 /* Sending command */
172 mvebu_mmc_write(mmc, SDIO_CMD, resptype);
174 start = get_timer(0);
176 while (!((mvebu_mmc_read(mmc, SDIO_NOR_INTR_STATUS)) & waittype)) {
177 if (mvebu_mmc_read(mmc, SDIO_NOR_INTR_STATUS) & SDIO_NOR_ERROR) {
178 dev_dbg(dev, "error! cmdidx : %d, err reg: %04x\n",
180 mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS));
181 if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) &
182 (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT)) {
183 dev_dbg(dev, "command READ timed out\n");
186 dev_dbg(dev, "command READ error\n");
190 if ((get_timer(0) - start) > TIMEOUT_DELAY) {
191 dev_dbg(dev, "command timed out\n");
196 /* Handling response */
197 if (cmd->resp_type & MMC_RSP_136) {
200 for (resp_indx = 0; resp_indx < 8; resp_indx++)
201 response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx));
203 cmd->response[0] = ((response[0] & 0x03ff) << 22) |
204 ((response[1] & 0xffff) << 6) |
205 ((response[2] & 0xfc00) >> 10);
206 cmd->response[1] = ((response[2] & 0x03ff) << 22) |
207 ((response[3] & 0xffff) << 6) |
208 ((response[4] & 0xfc00) >> 10);
209 cmd->response[2] = ((response[4] & 0x03ff) << 22) |
210 ((response[5] & 0xffff) << 6) |
211 ((response[6] & 0xfc00) >> 10);
212 cmd->response[3] = ((response[6] & 0x03ff) << 22) |
213 ((response[7] & 0x3fff) << 8);
214 } else if (cmd->resp_type & MMC_RSP_PRESENT) {
217 for (resp_indx = 0; resp_indx < 3; resp_indx++)
218 response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx));
220 cmd->response[0] = ((response[2] & 0x003f) << (8 - 8)) |
221 ((response[1] & 0xffff) << (14 - 8)) |
222 ((response[0] & 0x03ff) << (30 - 8));
223 cmd->response[1] = ((response[0] & 0xfc00) >> 10);
224 cmd->response[2] = 0;
225 cmd->response[3] = 0;
227 cmd->response[0] = 0;
228 cmd->response[1] = 0;
229 cmd->response[2] = 0;
230 cmd->response[3] = 0;
233 dev_dbg(dev, "resp[0x%x] ", cmd->resp_type);
234 debug("[0x%x] ", cmd->response[0]);
235 debug("[0x%x] ", cmd->response[1]);
236 debug("[0x%x] ", cmd->response[2]);
237 debug("[0x%x] ", cmd->response[3]);
240 if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) &
241 (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT))
247 static void mvebu_mmc_power_up(struct udevice *dev)
249 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
250 struct mmc *mmc = &pdata->mmc;
252 dev_dbg(dev, "power up\n");
254 /* disable interrupts */
255 mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0);
256 mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0);
259 mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW);
261 mvebu_mmc_write(mmc, SDIO_XFER_MODE, 0);
264 mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
265 mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
267 /* enable interrupts status */
268 mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
269 mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
272 static void mvebu_mmc_set_clk(struct udevice *dev, unsigned int clock)
275 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
276 struct mmc *mmc = &pdata->mmc;
279 dev_dbg(dev, "clock off\n");
280 mvebu_mmc_write(mmc, SDIO_XFER_MODE, SDIO_XFER_MODE_STOP_CLK);
281 mvebu_mmc_write(mmc, SDIO_CLK_DIV, MVEBU_MMC_BASE_DIV_MAX);
283 m = MVEBU_MMC_BASE_FAST_CLOCK/(2*clock) - 1;
284 if (m > MVEBU_MMC_BASE_DIV_MAX)
285 m = MVEBU_MMC_BASE_DIV_MAX;
286 mvebu_mmc_write(mmc, SDIO_CLK_DIV, m & MVEBU_MMC_BASE_DIV_MAX);
287 dev_dbg(dev, "clock (%d) div : %d\n", clock, m);
291 static void mvebu_mmc_set_bus(struct udevice *dev, unsigned int bus)
293 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
294 struct mmc *mmc = &pdata->mmc;
297 ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL);
298 ctrl_reg &= ~SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
302 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
306 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_1_BIT;
309 /* default transfer mode */
310 ctrl_reg |= SDIO_HOST_CTRL_BIG_ENDIAN;
311 ctrl_reg &= ~SDIO_HOST_CTRL_LSB_FIRST;
313 /* default to maximum timeout */
314 ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
315 ctrl_reg |= SDIO_HOST_CTRL_TMOUT_EN;
317 ctrl_reg |= SDIO_HOST_CTRL_PUSH_PULL_EN;
319 ctrl_reg |= SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY;
321 dev_dbg(dev, "ctrl 0x%04x: %s %s %s\n", ctrl_reg,
322 (ctrl_reg & SDIO_HOST_CTRL_PUSH_PULL_EN) ?
323 "push-pull" : "open-drain",
324 (ctrl_reg & SDIO_HOST_CTRL_DATA_WIDTH_4_BITS) ?
325 "4bit-width" : "1bit-width",
326 (ctrl_reg & SDIO_HOST_CTRL_HI_SPEED_EN) ?
329 mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg);
332 static int mvebu_mmc_set_ios(struct udevice *dev)
334 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
335 struct mmc *mmc = &pdata->mmc;
337 dev_dbg(dev, "bus[%d] clock[%d]\n",
338 mmc->bus_width, mmc->clock);
339 mvebu_mmc_set_bus(dev, mmc->bus_width);
340 mvebu_mmc_set_clk(dev, mmc->clock);
346 * Set window register.
348 static void mvebu_window_setup(const struct mmc *mmc)
352 for (i = 0; i < 4; i++) {
353 mvebu_mmc_write(mmc, WINDOW_CTRL(i), 0);
354 mvebu_mmc_write(mmc, WINDOW_BASE(i), 0);
356 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
357 u32 size, base, attrib;
359 /* Enable DRAM bank */
362 attrib = KWCPU_ATTR_DRAM_CS0;
365 attrib = KWCPU_ATTR_DRAM_CS1;
368 attrib = KWCPU_ATTR_DRAM_CS2;
371 attrib = KWCPU_ATTR_DRAM_CS3;
374 /* invalide bank, disable access */
379 size = gd->bd->bi_dram[i].size;
380 base = gd->bd->bi_dram[i].start;
381 if (size && attrib) {
382 mvebu_mmc_write(mmc, WINDOW_CTRL(i),
383 MVCPU_WIN_CTRL_DATA(size,
388 mvebu_mmc_write(mmc, WINDOW_CTRL(i), MVCPU_WIN_DISABLE);
390 mvebu_mmc_write(mmc, WINDOW_BASE(i), base);
394 static int mvebu_mmc_initialize(struct udevice *dev)
396 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
397 struct mmc *mmc = &pdata->mmc;
399 dev_dbg(dev, "%s\n", __func__);
402 * Setting host parameters
403 * Initial Host Ctrl : Timeout : max , Normal Speed mode,
404 * 4-bit data mode, Big Endian, SD memory Card, Push_pull CMD Line
406 mvebu_mmc_write(mmc, SDIO_HOST_CTRL,
407 SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX) |
408 SDIO_HOST_CTRL_DATA_WIDTH_4_BITS |
409 SDIO_HOST_CTRL_BIG_ENDIAN |
410 SDIO_HOST_CTRL_PUSH_PULL_EN |
411 SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY);
413 mvebu_mmc_write(mmc, SDIO_CLK_CTRL, 0);
416 mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
417 mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
419 /* disable interrupts */
420 mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0);
421 mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0);
423 mvebu_window_setup(mmc);
426 mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW);
431 static int mvebu_mmc_of_to_plat(struct udevice *dev)
433 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
436 addr = dev_read_addr(dev);
437 if (addr == FDT_ADDR_T_NONE)
440 pdata->iobase = (void *)addr;
445 static int mvebu_mmc_probe(struct udevice *dev)
447 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
448 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
449 struct mmc *mmc = &pdata->mmc;
450 struct mmc_config *cfg = &pdata->cfg;
452 cfg->name = dev->name;
453 cfg->f_min = MVEBU_MMC_BASE_FAST_CLOCK / MVEBU_MMC_BASE_DIV_MAX;
454 cfg->f_max = MVEBU_MMC_CLOCKRATE_MAX;
455 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
456 cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz;
457 cfg->part_type = PART_TYPE_DOS;
458 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
465 mvebu_mmc_power_up(dev);
466 mvebu_mmc_initialize(dev);
471 static const struct dm_mmc_ops mvebu_dm_mmc_ops = {
472 .send_cmd = mvebu_mmc_send_cmd,
473 .set_ios = mvebu_mmc_set_ios,
476 static int mvebu_mmc_bind(struct udevice *dev)
478 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
480 return mmc_bind(dev, &pdata->mmc, &pdata->cfg);
483 static const struct udevice_id mvebu_mmc_match[] = {
484 { .compatible = "marvell,orion-sdio" },
488 U_BOOT_DRIVER(mvebu_mmc) = {
491 .of_match = mvebu_mmc_match,
492 .ops = &mvebu_dm_mmc_ops,
493 .probe = mvebu_mmc_probe,
494 .bind = mvebu_mmc_bind,
495 .of_to_plat = mvebu_mmc_of_to_plat,
496 .plat_auto = sizeof(struct mvebu_mmc_plat),