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