xref: /rk3399_rockchip-uboot/drivers/mmc/mvebu_mmc.c (revision 07b0b9c00cc8f8e981df35ac4720057469a8d3c8)
13fe3b4fbSDrEagle /*
23fe3b4fbSDrEagle  * Marvell MMC/SD/SDIO driver
33fe3b4fbSDrEagle  *
42591fbdbSGerald Kerma  * (C) Copyright 2012-2014
53fe3b4fbSDrEagle  * Marvell Semiconductor <www.marvell.com>
63fe3b4fbSDrEagle  * Written-by: Maen Suleiman, Gerald Kerma
73fe3b4fbSDrEagle  *
83fe3b4fbSDrEagle  * SPDX-License-Identifier:	GPL-2.0+
93fe3b4fbSDrEagle  */
103fe3b4fbSDrEagle 
113fe3b4fbSDrEagle #include <common.h>
12915ffa52SJaehoon Chung #include <errno.h>
133fe3b4fbSDrEagle #include <malloc.h>
143fe3b4fbSDrEagle #include <part.h>
153fe3b4fbSDrEagle #include <mmc.h>
163fe3b4fbSDrEagle #include <asm/io.h>
173fe3b4fbSDrEagle #include <asm/arch/cpu.h>
183dc23f78SStefan Roese #include <asm/arch/soc.h>
193fe3b4fbSDrEagle #include <mvebu_mmc.h>
203fe3b4fbSDrEagle 
21bcd06989SMario Schuknecht DECLARE_GLOBAL_DATA_PTR;
22bcd06989SMario Schuknecht 
233fe3b4fbSDrEagle #define DRIVER_NAME "MVEBU_MMC"
243fe3b4fbSDrEagle 
25bcd06989SMario Schuknecht #define MVEBU_TARGET_DRAM 0
26bcd06989SMario Schuknecht 
2728d27b79SGerald Kerma #define TIMEOUT_DELAY	5*CONFIG_SYS_HZ		/* wait 5 seconds */
2828d27b79SGerald Kerma 
mvebu_mmc_write(u32 offs,u32 val)293fe3b4fbSDrEagle static void mvebu_mmc_write(u32 offs, u32 val)
303fe3b4fbSDrEagle {
313fe3b4fbSDrEagle 	writel(val, CONFIG_SYS_MMC_BASE + (offs));
323fe3b4fbSDrEagle }
333fe3b4fbSDrEagle 
mvebu_mmc_read(u32 offs)343fe3b4fbSDrEagle static u32 mvebu_mmc_read(u32 offs)
353fe3b4fbSDrEagle {
363fe3b4fbSDrEagle 	return readl(CONFIG_SYS_MMC_BASE + (offs));
373fe3b4fbSDrEagle }
383fe3b4fbSDrEagle 
mvebu_mmc_setup_data(struct mmc_data * data)393fe3b4fbSDrEagle static int mvebu_mmc_setup_data(struct mmc_data *data)
403fe3b4fbSDrEagle {
413fe3b4fbSDrEagle 	u32 ctrl_reg;
423fe3b4fbSDrEagle 
433fe3b4fbSDrEagle 	debug("%s, data %s : blocks=%d blksz=%d\n", DRIVER_NAME,
443fe3b4fbSDrEagle 	      (data->flags & MMC_DATA_READ) ? "read" : "write",
453fe3b4fbSDrEagle 	      data->blocks, data->blocksize);
463fe3b4fbSDrEagle 
473fe3b4fbSDrEagle 	/* default to maximum timeout */
483fe3b4fbSDrEagle 	ctrl_reg = mvebu_mmc_read(SDIO_HOST_CTRL);
493fe3b4fbSDrEagle 	ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
503fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_HOST_CTRL, ctrl_reg);
513fe3b4fbSDrEagle 
523fe3b4fbSDrEagle 	if (data->flags & MMC_DATA_READ) {
533fe3b4fbSDrEagle 		mvebu_mmc_write(SDIO_SYS_ADDR_LOW, (u32)data->dest & 0xffff);
543fe3b4fbSDrEagle 		mvebu_mmc_write(SDIO_SYS_ADDR_HI, (u32)data->dest >> 16);
553fe3b4fbSDrEagle 	} else {
563fe3b4fbSDrEagle 		mvebu_mmc_write(SDIO_SYS_ADDR_LOW, (u32)data->src & 0xffff);
573fe3b4fbSDrEagle 		mvebu_mmc_write(SDIO_SYS_ADDR_HI, (u32)data->src >> 16);
583fe3b4fbSDrEagle 	}
593fe3b4fbSDrEagle 
603fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_BLK_COUNT, data->blocks);
613fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_BLK_SIZE, data->blocksize);
623fe3b4fbSDrEagle 
633fe3b4fbSDrEagle 	return 0;
643fe3b4fbSDrEagle }
653fe3b4fbSDrEagle 
mvebu_mmc_send_cmd(struct mmc * mmc,struct mmc_cmd * cmd,struct mmc_data * data)663fe3b4fbSDrEagle static int mvebu_mmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
673fe3b4fbSDrEagle 			      struct mmc_data *data)
683fe3b4fbSDrEagle {
6928d27b79SGerald Kerma 	ulong start;
703fe3b4fbSDrEagle 	ushort waittype = 0;
713fe3b4fbSDrEagle 	ushort resptype = 0;
723fe3b4fbSDrEagle 	ushort xfertype = 0;
733fe3b4fbSDrEagle 	ushort resp_indx = 0;
743fe3b4fbSDrEagle 
75fc0f25f9SGerald Kerma 	debug("%s: cmdidx [0x%x] resp_type[0x%x] cmdarg[0x%x]\n",
76fc0f25f9SGerald Kerma 	      DRIVER_NAME, cmd->cmdidx, cmd->resp_type, cmd->cmdarg);
773fe3b4fbSDrEagle 
783fe3b4fbSDrEagle 	debug("%s: cmd %d (hw state 0x%04x)\n", DRIVER_NAME,
793fe3b4fbSDrEagle 	      cmd->cmdidx, mvebu_mmc_read(SDIO_HW_STATE));
803fe3b4fbSDrEagle 
8128d27b79SGerald Kerma 	/*
8228d27b79SGerald Kerma 	 * Hardware weirdness.  The FIFO_EMPTY bit of the HW_STATE
8328d27b79SGerald Kerma 	 * register is sometimes not set before a while when some
8428d27b79SGerald Kerma 	 * "unusual" data block sizes are used (such as with the SWITCH
8528d27b79SGerald Kerma 	 * command), even despite the fact that the XFER_DONE interrupt
8628d27b79SGerald Kerma 	 * was raised.  And if another data transfer starts before
8728d27b79SGerald Kerma 	 * this bit comes to good sense (which eventually happens by
8828d27b79SGerald Kerma 	 * itself) then the new transfer simply fails with a timeout.
8928d27b79SGerald Kerma 	 */
9028d27b79SGerald Kerma 	if (!(mvebu_mmc_read(SDIO_HW_STATE) & CMD_FIFO_EMPTY)) {
9128d27b79SGerald Kerma 		ushort hw_state, count = 0;
9228d27b79SGerald Kerma 
9328d27b79SGerald Kerma 		start = get_timer(0);
9428d27b79SGerald Kerma 		do {
9528d27b79SGerald Kerma 			hw_state = mvebu_mmc_read(SDIO_HW_STATE);
9628d27b79SGerald Kerma 			if ((get_timer(0) - start) > TIMEOUT_DELAY) {
9728d27b79SGerald Kerma 				printf("%s : FIFO_EMPTY bit missing\n",
9828d27b79SGerald Kerma 				       DRIVER_NAME);
9928d27b79SGerald Kerma 				break;
1003fe3b4fbSDrEagle 			}
10128d27b79SGerald Kerma 			count++;
10228d27b79SGerald Kerma 		} while (!(hw_state & CMD_FIFO_EMPTY));
10328d27b79SGerald Kerma 		debug("%s *** wait for FIFO_EMPTY bit (hw=0x%04x, count=%d, jiffies=%ld)\n",
10428d27b79SGerald Kerma 		      DRIVER_NAME, hw_state, count, (get_timer(0) - (start)));
1053fe3b4fbSDrEagle 	}
1063fe3b4fbSDrEagle 
10702b2739eSGerald Kerma 	/* Clear status */
10802b2739eSGerald Kerma 	mvebu_mmc_write(SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
10902b2739eSGerald Kerma 	mvebu_mmc_write(SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
1103fe3b4fbSDrEagle 
1113fe3b4fbSDrEagle 	resptype = SDIO_CMD_INDEX(cmd->cmdidx);
1123fe3b4fbSDrEagle 
1133fe3b4fbSDrEagle 	/* Analyzing resptype/xfertype/waittype for the command */
1143fe3b4fbSDrEagle 	if (cmd->resp_type & MMC_RSP_BUSY)
1153fe3b4fbSDrEagle 		resptype |= SDIO_CMD_RSP_48BUSY;
1163fe3b4fbSDrEagle 	else if (cmd->resp_type & MMC_RSP_136)
1173fe3b4fbSDrEagle 		resptype |= SDIO_CMD_RSP_136;
1183fe3b4fbSDrEagle 	else if (cmd->resp_type & MMC_RSP_PRESENT)
1193fe3b4fbSDrEagle 		resptype |= SDIO_CMD_RSP_48;
1203fe3b4fbSDrEagle 	else
1213fe3b4fbSDrEagle 		resptype |= SDIO_CMD_RSP_NONE;
1223fe3b4fbSDrEagle 
1233fe3b4fbSDrEagle 	if (cmd->resp_type & MMC_RSP_CRC)
1243fe3b4fbSDrEagle 		resptype |= SDIO_CMD_CHECK_CMDCRC;
1253fe3b4fbSDrEagle 
1263fe3b4fbSDrEagle 	if (cmd->resp_type & MMC_RSP_OPCODE)
1273fe3b4fbSDrEagle 		resptype |= SDIO_CMD_INDX_CHECK;
1283fe3b4fbSDrEagle 
1293fe3b4fbSDrEagle 	if (cmd->resp_type & MMC_RSP_PRESENT) {
1303fe3b4fbSDrEagle 		resptype |= SDIO_UNEXPECTED_RESP;
1313fe3b4fbSDrEagle 		waittype |= SDIO_NOR_UNEXP_RSP;
1323fe3b4fbSDrEagle 	}
1333fe3b4fbSDrEagle 
1343fe3b4fbSDrEagle 	if (data) {
13502b2739eSGerald Kerma 		int err = mvebu_mmc_setup_data(data);
13602b2739eSGerald Kerma 
13702b2739eSGerald Kerma 		if (err) {
13802b2739eSGerald Kerma 			debug("%s: command DATA error :%x\n",
13902b2739eSGerald Kerma 			      DRIVER_NAME, err);
14002b2739eSGerald Kerma 			return err;
14102b2739eSGerald Kerma 		}
14202b2739eSGerald Kerma 
1433fe3b4fbSDrEagle 		resptype |= SDIO_CMD_DATA_PRESENT | SDIO_CMD_CHECK_DATACRC16;
1443fe3b4fbSDrEagle 		xfertype |= SDIO_XFER_MODE_HW_WR_DATA_EN;
1453fe3b4fbSDrEagle 		if (data->flags & MMC_DATA_READ) {
1463fe3b4fbSDrEagle 			xfertype |= SDIO_XFER_MODE_TO_HOST;
1473fe3b4fbSDrEagle 			waittype = SDIO_NOR_DMA_INI;
1483fe3b4fbSDrEagle 		} else {
1493fe3b4fbSDrEagle 			waittype |= SDIO_NOR_XFER_DONE;
1503fe3b4fbSDrEagle 		}
1513fe3b4fbSDrEagle 	} else {
1523fe3b4fbSDrEagle 		waittype |= SDIO_NOR_CMD_DONE;
1533fe3b4fbSDrEagle 	}
1543fe3b4fbSDrEagle 
1553fe3b4fbSDrEagle 	/* Setting cmd arguments */
1563fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_ARG_LOW, cmd->cmdarg & 0xffff);
1573fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_ARG_HI, cmd->cmdarg >> 16);
1583fe3b4fbSDrEagle 
1593fe3b4fbSDrEagle 	/* Setting Xfer mode */
1603fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_XFER_MODE, xfertype);
1613fe3b4fbSDrEagle 
1623fe3b4fbSDrEagle 	/* Sending command */
1633fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_CMD, resptype);
1643fe3b4fbSDrEagle 
16528d27b79SGerald Kerma 	start = get_timer(0);
1663fe3b4fbSDrEagle 
1673fe3b4fbSDrEagle 	while (!((mvebu_mmc_read(SDIO_NOR_INTR_STATUS)) & waittype)) {
1683fe3b4fbSDrEagle 		if (mvebu_mmc_read(SDIO_NOR_INTR_STATUS) & SDIO_NOR_ERROR) {
1693fe3b4fbSDrEagle 			debug("%s: error! cmdidx : %d, err reg: %04x\n",
1703fe3b4fbSDrEagle 			      DRIVER_NAME, cmd->cmdidx,
1713fe3b4fbSDrEagle 			      mvebu_mmc_read(SDIO_ERR_INTR_STATUS));
1723fe3b4fbSDrEagle 			if (mvebu_mmc_read(SDIO_ERR_INTR_STATUS) &
173fc0f25f9SGerald Kerma 			    (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT)) {
174fc0f25f9SGerald Kerma 				debug("%s: command READ timed out\n",
175fc0f25f9SGerald Kerma 				      DRIVER_NAME);
176915ffa52SJaehoon Chung 				return -ETIMEDOUT;
177fc0f25f9SGerald Kerma 			}
178fc0f25f9SGerald Kerma 			debug("%s: command READ error\n", DRIVER_NAME);
179915ffa52SJaehoon Chung 			return -ECOMM;
1803fe3b4fbSDrEagle 		}
1813fe3b4fbSDrEagle 
18228d27b79SGerald Kerma 		if ((get_timer(0) - start) > TIMEOUT_DELAY) {
18328d27b79SGerald Kerma 			debug("%s: command timed out\n", DRIVER_NAME);
184915ffa52SJaehoon Chung 			return -ETIMEDOUT;
1853fe3b4fbSDrEagle 		}
1863fe3b4fbSDrEagle 	}
18728d27b79SGerald Kerma 
1883fe3b4fbSDrEagle 	/* Handling response */
1893fe3b4fbSDrEagle 	if (cmd->resp_type & MMC_RSP_136) {
1903fe3b4fbSDrEagle 		uint response[8];
1913fe3b4fbSDrEagle 
1923fe3b4fbSDrEagle 		for (resp_indx = 0; resp_indx < 8; resp_indx++)
1933fe3b4fbSDrEagle 			response[resp_indx]
1943fe3b4fbSDrEagle 				= mvebu_mmc_read(SDIO_RSP(resp_indx));
1953fe3b4fbSDrEagle 
1963fe3b4fbSDrEagle 		cmd->response[0] =	((response[0] & 0x03ff) << 22) |
1973fe3b4fbSDrEagle 					((response[1] & 0xffff) << 6) |
1983fe3b4fbSDrEagle 					((response[2] & 0xfc00) >> 10);
1993fe3b4fbSDrEagle 		cmd->response[1] =	((response[2] & 0x03ff) << 22) |
2003fe3b4fbSDrEagle 					((response[3] & 0xffff) << 6) |
2013fe3b4fbSDrEagle 					((response[4] & 0xfc00) >> 10);
2023fe3b4fbSDrEagle 		cmd->response[2] =	((response[4] & 0x03ff) << 22) |
2033fe3b4fbSDrEagle 					((response[5] & 0xffff) << 6) |
2043fe3b4fbSDrEagle 					((response[6] & 0xfc00) >> 10);
2053fe3b4fbSDrEagle 		cmd->response[3] =	((response[6] & 0x03ff) << 22) |
2063fe3b4fbSDrEagle 					((response[7] & 0x3fff) << 8);
2073fe3b4fbSDrEagle 	} else if (cmd->resp_type & MMC_RSP_PRESENT) {
2083fe3b4fbSDrEagle 		uint response[3];
2093fe3b4fbSDrEagle 
2103fe3b4fbSDrEagle 		for (resp_indx = 0; resp_indx < 3; resp_indx++)
2113fe3b4fbSDrEagle 			response[resp_indx]
2123fe3b4fbSDrEagle 				= mvebu_mmc_read(SDIO_RSP(resp_indx));
2133fe3b4fbSDrEagle 
2143fe3b4fbSDrEagle 		cmd->response[0] =	((response[2] & 0x003f) << (8 - 8)) |
2153fe3b4fbSDrEagle 					((response[1] & 0xffff) << (14 - 8)) |
2163fe3b4fbSDrEagle 					((response[0] & 0x03ff) << (30 - 8));
2173fe3b4fbSDrEagle 		cmd->response[1] =	((response[0] & 0xfc00) >> 10);
2183fe3b4fbSDrEagle 		cmd->response[2] =	0;
2193fe3b4fbSDrEagle 		cmd->response[3] =	0;
22002b2739eSGerald Kerma 	} else {
22102b2739eSGerald Kerma 		cmd->response[0] =	0;
22202b2739eSGerald Kerma 		cmd->response[1] =	0;
22302b2739eSGerald Kerma 		cmd->response[2] =	0;
22402b2739eSGerald Kerma 		cmd->response[3] =	0;
2253fe3b4fbSDrEagle 	}
2263fe3b4fbSDrEagle 
2273fe3b4fbSDrEagle 	debug("%s: resp[0x%x] ", DRIVER_NAME, cmd->resp_type);
2283fe3b4fbSDrEagle 	debug("[0x%x] ", cmd->response[0]);
2293fe3b4fbSDrEagle 	debug("[0x%x] ", cmd->response[1]);
2303fe3b4fbSDrEagle 	debug("[0x%x] ", cmd->response[2]);
2313fe3b4fbSDrEagle 	debug("[0x%x] ", cmd->response[3]);
2323fe3b4fbSDrEagle 	debug("\n");
2333fe3b4fbSDrEagle 
23402b2739eSGerald Kerma 	if (mvebu_mmc_read(SDIO_ERR_INTR_STATUS) &
23502b2739eSGerald Kerma 		(SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT))
236915ffa52SJaehoon Chung 		return -ETIMEDOUT;
23702b2739eSGerald Kerma 
2383fe3b4fbSDrEagle 	return 0;
2393fe3b4fbSDrEagle }
2403fe3b4fbSDrEagle 
mvebu_mmc_power_up(void)2413fe3b4fbSDrEagle static void mvebu_mmc_power_up(void)
2423fe3b4fbSDrEagle {
2433fe3b4fbSDrEagle 	debug("%s: power up\n", DRIVER_NAME);
2443fe3b4fbSDrEagle 
2453fe3b4fbSDrEagle 	/* disable interrupts */
2463fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_NOR_INTR_EN, 0);
2473fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_ERR_INTR_EN, 0);
2483fe3b4fbSDrEagle 
2493fe3b4fbSDrEagle 	/* SW reset */
2503fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_SW_RESET, SDIO_SW_RESET_NOW);
2513fe3b4fbSDrEagle 
2523fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_XFER_MODE, 0);
2533fe3b4fbSDrEagle 
2543fe3b4fbSDrEagle 	/* enable status */
2553fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
2563fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
2573fe3b4fbSDrEagle 
2583fe3b4fbSDrEagle 	/* enable interrupts status */
2593fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
2603fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
2613fe3b4fbSDrEagle }
2623fe3b4fbSDrEagle 
mvebu_mmc_set_clk(unsigned int clock)2633fe3b4fbSDrEagle static void mvebu_mmc_set_clk(unsigned int clock)
2643fe3b4fbSDrEagle {
2653fe3b4fbSDrEagle 	unsigned int m;
2663fe3b4fbSDrEagle 
2673fe3b4fbSDrEagle 	if (clock == 0) {
2683fe3b4fbSDrEagle 		debug("%s: clock off\n", DRIVER_NAME);
2693fe3b4fbSDrEagle 		mvebu_mmc_write(SDIO_XFER_MODE, SDIO_XFER_MODE_STOP_CLK);
2703fe3b4fbSDrEagle 		mvebu_mmc_write(SDIO_CLK_DIV, MVEBU_MMC_BASE_DIV_MAX);
2713fe3b4fbSDrEagle 	} else {
2723fe3b4fbSDrEagle 		m = MVEBU_MMC_BASE_FAST_CLOCK/(2*clock) - 1;
2733fe3b4fbSDrEagle 		if (m > MVEBU_MMC_BASE_DIV_MAX)
2743fe3b4fbSDrEagle 			m = MVEBU_MMC_BASE_DIV_MAX;
2753fe3b4fbSDrEagle 		mvebu_mmc_write(SDIO_CLK_DIV, m & MVEBU_MMC_BASE_DIV_MAX);
276fc0f25f9SGerald Kerma 		debug("%s: clock (%d) div : %d\n", DRIVER_NAME, clock, m);
2773fe3b4fbSDrEagle 	}
2783fe3b4fbSDrEagle }
2793fe3b4fbSDrEagle 
mvebu_mmc_set_bus(unsigned int bus)2803fe3b4fbSDrEagle static void mvebu_mmc_set_bus(unsigned int bus)
2813fe3b4fbSDrEagle {
2823fe3b4fbSDrEagle 	u32 ctrl_reg = 0;
2833fe3b4fbSDrEagle 
2843fe3b4fbSDrEagle 	ctrl_reg = mvebu_mmc_read(SDIO_HOST_CTRL);
2853fe3b4fbSDrEagle 	ctrl_reg &= ~SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
2863fe3b4fbSDrEagle 
2873fe3b4fbSDrEagle 	switch (bus) {
2883fe3b4fbSDrEagle 	case 4:
2893fe3b4fbSDrEagle 		ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
2903fe3b4fbSDrEagle 		break;
2913fe3b4fbSDrEagle 	case 1:
2923fe3b4fbSDrEagle 	default:
2933fe3b4fbSDrEagle 		ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_1_BIT;
2943fe3b4fbSDrEagle 	}
2953fe3b4fbSDrEagle 
2963fe3b4fbSDrEagle 	/* default transfer mode */
2973fe3b4fbSDrEagle 	ctrl_reg |= SDIO_HOST_CTRL_BIG_ENDIAN;
2983fe3b4fbSDrEagle 	ctrl_reg &= ~SDIO_HOST_CTRL_LSB_FIRST;
2993fe3b4fbSDrEagle 
3003fe3b4fbSDrEagle 	/* default to maximum timeout */
3013fe3b4fbSDrEagle 	ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
302bcd06989SMario Schuknecht 	ctrl_reg |= SDIO_HOST_CTRL_TMOUT_EN;
3033fe3b4fbSDrEagle 
3043fe3b4fbSDrEagle 	ctrl_reg |= SDIO_HOST_CTRL_PUSH_PULL_EN;
3053fe3b4fbSDrEagle 
3063fe3b4fbSDrEagle 	ctrl_reg |= SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY;
3073fe3b4fbSDrEagle 
3083fe3b4fbSDrEagle 	debug("%s: ctrl 0x%04x: %s %s %s\n", DRIVER_NAME, ctrl_reg,
3093fe3b4fbSDrEagle 	      (ctrl_reg & SDIO_HOST_CTRL_PUSH_PULL_EN) ?
3103fe3b4fbSDrEagle 	      "push-pull" : "open-drain",
3113fe3b4fbSDrEagle 	      (ctrl_reg & SDIO_HOST_CTRL_DATA_WIDTH_4_BITS) ?
3123fe3b4fbSDrEagle 	      "4bit-width" : "1bit-width",
3133fe3b4fbSDrEagle 	      (ctrl_reg & SDIO_HOST_CTRL_HI_SPEED_EN) ?
3143fe3b4fbSDrEagle 	      "high-speed" : "");
3153fe3b4fbSDrEagle 
3163fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_HOST_CTRL, ctrl_reg);
3173fe3b4fbSDrEagle }
3183fe3b4fbSDrEagle 
mvebu_mmc_set_ios(struct mmc * mmc)319*07b0b9c0SJaehoon Chung static int mvebu_mmc_set_ios(struct mmc *mmc)
3203fe3b4fbSDrEagle {
3213fe3b4fbSDrEagle 	debug("%s: bus[%d] clock[%d]\n", DRIVER_NAME,
3223fe3b4fbSDrEagle 	      mmc->bus_width, mmc->clock);
3233fe3b4fbSDrEagle 	mvebu_mmc_set_bus(mmc->bus_width);
3243fe3b4fbSDrEagle 	mvebu_mmc_set_clk(mmc->clock);
325*07b0b9c0SJaehoon Chung 
326*07b0b9c0SJaehoon Chung 	return 0;
3273fe3b4fbSDrEagle }
3283fe3b4fbSDrEagle 
329bcd06989SMario Schuknecht /*
330bcd06989SMario Schuknecht  * Set window register.
331bcd06989SMario Schuknecht  */
mvebu_window_setup(void)332bcd06989SMario Schuknecht static void mvebu_window_setup(void)
333bcd06989SMario Schuknecht {
334bcd06989SMario Schuknecht 	int i;
335bcd06989SMario Schuknecht 
336bcd06989SMario Schuknecht 	for (i = 0; i < 4; i++) {
337bcd06989SMario Schuknecht 		mvebu_mmc_write(WINDOW_CTRL(i), 0);
338bcd06989SMario Schuknecht 		mvebu_mmc_write(WINDOW_BASE(i), 0);
339bcd06989SMario Schuknecht 	}
340bcd06989SMario Schuknecht 	for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
341bcd06989SMario Schuknecht 		u32 size, base, attrib;
342bcd06989SMario Schuknecht 
343bcd06989SMario Schuknecht 		/* Enable DRAM bank */
344bcd06989SMario Schuknecht 		switch (i) {
345bcd06989SMario Schuknecht 		case 0:
346bcd06989SMario Schuknecht 			attrib = KWCPU_ATTR_DRAM_CS0;
347bcd06989SMario Schuknecht 			break;
348bcd06989SMario Schuknecht 		case 1:
349bcd06989SMario Schuknecht 			attrib = KWCPU_ATTR_DRAM_CS1;
350bcd06989SMario Schuknecht 			break;
351bcd06989SMario Schuknecht 		case 2:
352bcd06989SMario Schuknecht 			attrib = KWCPU_ATTR_DRAM_CS2;
353bcd06989SMario Schuknecht 			break;
354bcd06989SMario Schuknecht 		case 3:
355bcd06989SMario Schuknecht 			attrib = KWCPU_ATTR_DRAM_CS3;
356bcd06989SMario Schuknecht 			break;
357bcd06989SMario Schuknecht 		default:
358bcd06989SMario Schuknecht 			/* invalide bank, disable access */
359bcd06989SMario Schuknecht 			attrib = 0;
360bcd06989SMario Schuknecht 			break;
361bcd06989SMario Schuknecht 		}
362bcd06989SMario Schuknecht 
363bcd06989SMario Schuknecht 		size = gd->bd->bi_dram[i].size;
364bcd06989SMario Schuknecht 		base = gd->bd->bi_dram[i].start;
365bcd06989SMario Schuknecht 		if (size && attrib) {
366bcd06989SMario Schuknecht 			mvebu_mmc_write(WINDOW_CTRL(i),
367bcd06989SMario Schuknecht 					MVCPU_WIN_CTRL_DATA(size,
368bcd06989SMario Schuknecht 							    MVEBU_TARGET_DRAM,
369bcd06989SMario Schuknecht 							    attrib,
370bcd06989SMario Schuknecht 							    MVCPU_WIN_ENABLE));
371bcd06989SMario Schuknecht 		} else {
372bcd06989SMario Schuknecht 			mvebu_mmc_write(WINDOW_CTRL(i), MVCPU_WIN_DISABLE);
373bcd06989SMario Schuknecht 		}
374bcd06989SMario Schuknecht 		mvebu_mmc_write(WINDOW_BASE(i), base);
375bcd06989SMario Schuknecht 	}
376bcd06989SMario Schuknecht }
377bcd06989SMario Schuknecht 
mvebu_mmc_initialize(struct mmc * mmc)3783fe3b4fbSDrEagle static int mvebu_mmc_initialize(struct mmc *mmc)
3793fe3b4fbSDrEagle {
380fc0f25f9SGerald Kerma 	debug("%s: mvebu_mmc_initialize\n", DRIVER_NAME);
3813fe3b4fbSDrEagle 
3823fe3b4fbSDrEagle 	/*
3833fe3b4fbSDrEagle 	 * Setting host parameters
3843fe3b4fbSDrEagle 	 * Initial Host Ctrl : Timeout : max , Normal Speed mode,
3853fe3b4fbSDrEagle 	 * 4-bit data mode, Big Endian, SD memory Card, Push_pull CMD Line
3863fe3b4fbSDrEagle 	 */
3873fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_HOST_CTRL,
3883fe3b4fbSDrEagle 			SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX) |
3893fe3b4fbSDrEagle 			SDIO_HOST_CTRL_DATA_WIDTH_4_BITS |
3903fe3b4fbSDrEagle 			SDIO_HOST_CTRL_BIG_ENDIAN |
3913fe3b4fbSDrEagle 			SDIO_HOST_CTRL_PUSH_PULL_EN |
3923fe3b4fbSDrEagle 			SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY);
3933fe3b4fbSDrEagle 
3943fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_CLK_CTRL, 0);
3953fe3b4fbSDrEagle 
3963fe3b4fbSDrEagle 	/* enable status */
3973fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
3983fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
3993fe3b4fbSDrEagle 
4003fe3b4fbSDrEagle 	/* disable interrupts */
4013fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_NOR_INTR_EN, 0);
4023fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_ERR_INTR_EN, 0);
4033fe3b4fbSDrEagle 
404bcd06989SMario Schuknecht 	mvebu_window_setup();
405bcd06989SMario Schuknecht 
4063fe3b4fbSDrEagle 	/* SW reset */
4073fe3b4fbSDrEagle 	mvebu_mmc_write(SDIO_SW_RESET, SDIO_SW_RESET_NOW);
4083fe3b4fbSDrEagle 
4093fe3b4fbSDrEagle 	return 0;
4103fe3b4fbSDrEagle }
4113fe3b4fbSDrEagle 
4123fe3b4fbSDrEagle static const struct mmc_ops mvebu_mmc_ops = {
4133fe3b4fbSDrEagle 	.send_cmd	= mvebu_mmc_send_cmd,
4143fe3b4fbSDrEagle 	.set_ios	= mvebu_mmc_set_ios,
4153fe3b4fbSDrEagle 	.init		= mvebu_mmc_initialize,
4163fe3b4fbSDrEagle };
4173fe3b4fbSDrEagle 
4183fe3b4fbSDrEagle static struct mmc_config mvebu_mmc_cfg = {
4193fe3b4fbSDrEagle 	.name		= DRIVER_NAME,
4203fe3b4fbSDrEagle 	.ops		= &mvebu_mmc_ops,
4213fe3b4fbSDrEagle 	.f_min		= MVEBU_MMC_BASE_FAST_CLOCK / MVEBU_MMC_BASE_DIV_MAX,
4223fe3b4fbSDrEagle 	.f_max		= MVEBU_MMC_CLOCKRATE_MAX,
4233fe3b4fbSDrEagle 	.voltages	= MMC_VDD_32_33 | MMC_VDD_33_34,
4245a20397bSRob Herring 	.host_caps	= MMC_MODE_4BIT | MMC_MODE_HS |
425bcd06989SMario Schuknecht 			  MMC_MODE_HS_52MHz,
4263fe3b4fbSDrEagle 	.part_type	= PART_TYPE_DOS,
4273fe3b4fbSDrEagle 	.b_max		= CONFIG_SYS_MMC_MAX_BLK_COUNT,
4283fe3b4fbSDrEagle };
4293fe3b4fbSDrEagle 
mvebu_mmc_init(bd_t * bis)4303fe3b4fbSDrEagle int mvebu_mmc_init(bd_t *bis)
4313fe3b4fbSDrEagle {
4323fe3b4fbSDrEagle 	struct mmc *mmc;
4333fe3b4fbSDrEagle 
4343fe3b4fbSDrEagle 	mvebu_mmc_power_up();
4353fe3b4fbSDrEagle 
4363fe3b4fbSDrEagle 	mmc = mmc_create(&mvebu_mmc_cfg, bis);
4373fe3b4fbSDrEagle 	if (mmc == NULL)
4383fe3b4fbSDrEagle 		return -1;
4393fe3b4fbSDrEagle 
4403fe3b4fbSDrEagle 	return 0;
4413fe3b4fbSDrEagle }
442