blob: 5af1953cd1485183b5989ce6200f7cc11ac066f5 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
DrEaglead881742014-07-25 21:07:30 +02002/*
3 * Marvell MMC/SD/SDIO driver
4 *
Gerald Kermac5fb9f72014-12-13 21:35:31 +01005 * (C) Copyright 2012-2014
DrEaglead881742014-07-25 21:07:30 +02006 * Marvell Semiconductor <www.marvell.com>
7 * Written-by: Maen Suleiman, Gerald Kerma
DrEaglead881742014-07-25 21:07:30 +02008 */
9
Jaehoon Chung7825d202016-07-19 16:33:36 +090010#include <errno.h>
Simon Glass0f2af882020-05-10 11:40:05 -060011#include <log.h>
DrEaglead881742014-07-25 21:07:30 +020012#include <malloc.h>
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020013#include <dm.h>
14#include <fdtdec.h>
DrEaglead881742014-07-25 21:07:30 +020015#include <part.h>
16#include <mmc.h>
17#include <asm/io.h>
18#include <asm/arch/cpu.h>
Stefan Roesec2437842014-10-22 12:13:06 +020019#include <asm/arch/soc.h>
DrEaglead881742014-07-25 21:07:30 +020020#include <mvebu_mmc.h>
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020021#include <dm/device_compat.h>
DrEaglead881742014-07-25 21:07:30 +020022
Mario Schuknecht6764f372014-08-25 14:12:26 +020023#define MVEBU_TARGET_DRAM 0
24
Gerald Kerma20d96912014-12-13 21:35:32 +010025#define TIMEOUT_DELAY 5*CONFIG_SYS_HZ /* wait 5 seconds */
26
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020027static inline void *get_regbase(const struct mmc *mmc)
DrEaglead881742014-07-25 21:07:30 +020028{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020029 struct mvebu_mmc_plat *pdata = mmc->priv;
30
31 return pdata->iobase;
DrEaglead881742014-07-25 21:07:30 +020032}
33
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020034static void mvebu_mmc_write(const struct mmc *mmc, u32 offs, u32 val)
DrEaglead881742014-07-25 21:07:30 +020035{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020036 writel(val, get_regbase(mmc) + (offs));
DrEaglead881742014-07-25 21:07:30 +020037}
38
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020039static u32 mvebu_mmc_read(const struct mmc *mmc, u32 offs)
DrEaglead881742014-07-25 21:07:30 +020040{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020041 return readl(get_regbase(mmc) + (offs));
42}
43
44static int mvebu_mmc_setup_data(struct udevice *dev, struct mmc_data *data)
45{
46 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
47 struct mmc *mmc = &pdata->mmc;
DrEaglead881742014-07-25 21:07:30 +020048 u32 ctrl_reg;
49
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020050 dev_dbg(dev, "data %s : blocks=%d blksz=%d\n",
51 (data->flags & MMC_DATA_READ) ? "read" : "write",
52 data->blocks, data->blocksize);
DrEaglead881742014-07-25 21:07:30 +020053
54 /* default to maximum timeout */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020055 ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL);
DrEaglead881742014-07-25 21:07:30 +020056 ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020057 mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg);
DrEaglead881742014-07-25 21:07:30 +020058
59 if (data->flags & MMC_DATA_READ) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020060 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);
DrEaglead881742014-07-25 21:07:30 +020062 } else {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020063 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);
DrEaglead881742014-07-25 21:07:30 +020065 }
66
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020067 mvebu_mmc_write(mmc, SDIO_BLK_COUNT, data->blocks);
68 mvebu_mmc_write(mmc, SDIO_BLK_SIZE, data->blocksize);
DrEaglead881742014-07-25 21:07:30 +020069
70 return 0;
71}
72
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020073static int mvebu_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
DrEaglead881742014-07-25 21:07:30 +020074 struct mmc_data *data)
75{
Gerald Kerma20d96912014-12-13 21:35:32 +010076 ulong start;
DrEaglead881742014-07-25 21:07:30 +020077 ushort waittype = 0;
78 ushort resptype = 0;
79 ushort xfertype = 0;
80 ushort resp_indx = 0;
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020081 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
82 struct mmc *mmc = &pdata->mmc;
DrEaglead881742014-07-25 21:07:30 +020083
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020084 dev_dbg(dev, "cmdidx [0x%x] resp_type[0x%x] cmdarg[0x%x]\n",
85 cmd->cmdidx, cmd->resp_type, cmd->cmdarg);
DrEaglead881742014-07-25 21:07:30 +020086
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020087 dev_dbg(dev, "cmd %d (hw state 0x%04x)\n",
88 cmd->cmdidx, mvebu_mmc_read(mmc, SDIO_HW_STATE));
DrEaglead881742014-07-25 21:07:30 +020089
Gerald Kerma20d96912014-12-13 21:35:32 +010090 /*
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.
98 */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +020099 if (!(mvebu_mmc_read(mmc, SDIO_HW_STATE) & CMD_FIFO_EMPTY)) {
Gerald Kerma20d96912014-12-13 21:35:32 +0100100 ushort hw_state, count = 0;
101
102 start = get_timer(0);
103 do {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200104 hw_state = mvebu_mmc_read(mmc, SDIO_HW_STATE);
Gerald Kerma20d96912014-12-13 21:35:32 +0100105 if ((get_timer(0) - start) > TIMEOUT_DELAY) {
106 printf("%s : FIFO_EMPTY bit missing\n",
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200107 dev->name);
Gerald Kerma20d96912014-12-13 21:35:32 +0100108 break;
109 }
110 count++;
111 } while (!(hw_state & CMD_FIFO_EMPTY));
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200112 dev_dbg(dev, "*** wait for FIFO_EMPTY bit (hw=0x%04x, count=%d, jiffies=%ld)\n",
113 hw_state, count, (get_timer(0) - (start)));
DrEaglead881742014-07-25 21:07:30 +0200114 }
115
Gerald Kerma85a25fd2014-12-13 21:35:35 +0100116 /* Clear status */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200117 mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
118 mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
DrEaglead881742014-07-25 21:07:30 +0200119
120 resptype = SDIO_CMD_INDEX(cmd->cmdidx);
121
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;
129 else
130 resptype |= SDIO_CMD_RSP_NONE;
131
132 if (cmd->resp_type & MMC_RSP_CRC)
133 resptype |= SDIO_CMD_CHECK_CMDCRC;
134
135 if (cmd->resp_type & MMC_RSP_OPCODE)
136 resptype |= SDIO_CMD_INDX_CHECK;
137
138 if (cmd->resp_type & MMC_RSP_PRESENT) {
139 resptype |= SDIO_UNEXPECTED_RESP;
140 waittype |= SDIO_NOR_UNEXP_RSP;
141 }
142
143 if (data) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200144 int err = mvebu_mmc_setup_data(dev, data);
Gerald Kerma85a25fd2014-12-13 21:35:35 +0100145
146 if (err) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200147 dev_dbg(dev, "command DATA error :%x\n", err);
Gerald Kerma85a25fd2014-12-13 21:35:35 +0100148 return err;
149 }
150
DrEaglead881742014-07-25 21:07:30 +0200151 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;
156 } else {
157 waittype |= SDIO_NOR_XFER_DONE;
158 }
159 } else {
160 waittype |= SDIO_NOR_CMD_DONE;
161 }
162
163 /* Setting cmd arguments */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200164 mvebu_mmc_write(mmc, SDIO_ARG_LOW, cmd->cmdarg & 0xffff);
165 mvebu_mmc_write(mmc, SDIO_ARG_HI, cmd->cmdarg >> 16);
DrEaglead881742014-07-25 21:07:30 +0200166
167 /* Setting Xfer mode */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200168 mvebu_mmc_write(mmc, SDIO_XFER_MODE, xfertype);
DrEaglead881742014-07-25 21:07:30 +0200169
DrEaglead881742014-07-25 21:07:30 +0200170 /* Sending command */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200171 mvebu_mmc_write(mmc, SDIO_CMD, resptype);
DrEaglead881742014-07-25 21:07:30 +0200172
Gerald Kerma20d96912014-12-13 21:35:32 +0100173 start = get_timer(0);
DrEaglead881742014-07-25 21:07:30 +0200174
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200175 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",
178 cmd->cmdidx,
179 mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS));
180 if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) &
Gerald Kerma501791c2014-12-13 21:35:33 +0100181 (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT)) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200182 dev_dbg(dev, "command READ timed out\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900183 return -ETIMEDOUT;
Gerald Kerma501791c2014-12-13 21:35:33 +0100184 }
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200185 dev_dbg(dev, "command READ error\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900186 return -ECOMM;
DrEaglead881742014-07-25 21:07:30 +0200187 }
188
Gerald Kerma20d96912014-12-13 21:35:32 +0100189 if ((get_timer(0) - start) > TIMEOUT_DELAY) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200190 dev_dbg(dev, "command timed out\n");
Jaehoon Chung7825d202016-07-19 16:33:36 +0900191 return -ETIMEDOUT;
DrEaglead881742014-07-25 21:07:30 +0200192 }
193 }
Gerald Kerma20d96912014-12-13 21:35:32 +0100194
DrEaglead881742014-07-25 21:07:30 +0200195 /* Handling response */
196 if (cmd->resp_type & MMC_RSP_136) {
197 uint response[8];
198
199 for (resp_indx = 0; resp_indx < 8; resp_indx++)
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200200 response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx));
DrEaglead881742014-07-25 21:07:30 +0200201
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) {
214 uint response[3];
215
216 for (resp_indx = 0; resp_indx < 3; resp_indx++)
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200217 response[resp_indx] = mvebu_mmc_read(mmc, SDIO_RSP(resp_indx));
DrEaglead881742014-07-25 21:07:30 +0200218
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;
Gerald Kerma85a25fd2014-12-13 21:35:35 +0100225 } else {
226 cmd->response[0] = 0;
227 cmd->response[1] = 0;
228 cmd->response[2] = 0;
229 cmd->response[3] = 0;
DrEaglead881742014-07-25 21:07:30 +0200230 }
231
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200232 dev_dbg(dev, "resp[0x%x] ", cmd->resp_type);
DrEaglead881742014-07-25 21:07:30 +0200233 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]);
237 debug("\n");
238
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200239 if (mvebu_mmc_read(mmc, SDIO_ERR_INTR_STATUS) &
Gerald Kerma85a25fd2014-12-13 21:35:35 +0100240 (SDIO_ERR_CMD_TIMEOUT | SDIO_ERR_DATA_TIMEOUT))
Jaehoon Chung7825d202016-07-19 16:33:36 +0900241 return -ETIMEDOUT;
Gerald Kerma85a25fd2014-12-13 21:35:35 +0100242
DrEaglead881742014-07-25 21:07:30 +0200243 return 0;
244}
245
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200246static void mvebu_mmc_power_up(struct udevice *dev)
DrEaglead881742014-07-25 21:07:30 +0200247{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200248 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
249 struct mmc *mmc = &pdata->mmc;
250
251 dev_dbg(dev, "power up\n");
DrEaglead881742014-07-25 21:07:30 +0200252
253 /* disable interrupts */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200254 mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0);
255 mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0);
DrEaglead881742014-07-25 21:07:30 +0200256
257 /* SW reset */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200258 mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW);
DrEaglead881742014-07-25 21:07:30 +0200259
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200260 mvebu_mmc_write(mmc, SDIO_XFER_MODE, 0);
DrEaglead881742014-07-25 21:07:30 +0200261
262 /* enable status */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200263 mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
264 mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
DrEaglead881742014-07-25 21:07:30 +0200265
266 /* enable interrupts status */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200267 mvebu_mmc_write(mmc, SDIO_NOR_INTR_STATUS, SDIO_POLL_MASK);
268 mvebu_mmc_write(mmc, SDIO_ERR_INTR_STATUS, SDIO_POLL_MASK);
DrEaglead881742014-07-25 21:07:30 +0200269}
270
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200271static void mvebu_mmc_set_clk(struct udevice *dev, unsigned int clock)
DrEaglead881742014-07-25 21:07:30 +0200272{
273 unsigned int m;
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200274 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
275 struct mmc *mmc = &pdata->mmc;
DrEaglead881742014-07-25 21:07:30 +0200276
277 if (clock == 0) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200278 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);
DrEaglead881742014-07-25 21:07:30 +0200281 } else {
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;
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200285 mvebu_mmc_write(mmc, SDIO_CLK_DIV, m & MVEBU_MMC_BASE_DIV_MAX);
286 dev_dbg(dev, "clock (%d) div : %d\n", clock, m);
DrEaglead881742014-07-25 21:07:30 +0200287 }
DrEaglead881742014-07-25 21:07:30 +0200288}
289
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200290static void mvebu_mmc_set_bus(struct udevice *dev, unsigned int bus)
DrEaglead881742014-07-25 21:07:30 +0200291{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200292 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
293 struct mmc *mmc = &pdata->mmc;
DrEaglead881742014-07-25 21:07:30 +0200294 u32 ctrl_reg = 0;
295
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200296 ctrl_reg = mvebu_mmc_read(mmc, SDIO_HOST_CTRL);
DrEaglead881742014-07-25 21:07:30 +0200297 ctrl_reg &= ~SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
298
299 switch (bus) {
300 case 4:
301 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_4_BITS;
302 break;
303 case 1:
304 default:
305 ctrl_reg |= SDIO_HOST_CTRL_DATA_WIDTH_1_BIT;
306 }
307
308 /* default transfer mode */
309 ctrl_reg |= SDIO_HOST_CTRL_BIG_ENDIAN;
310 ctrl_reg &= ~SDIO_HOST_CTRL_LSB_FIRST;
311
312 /* default to maximum timeout */
313 ctrl_reg |= SDIO_HOST_CTRL_TMOUT(SDIO_HOST_CTRL_TMOUT_MAX);
Mario Schuknecht6764f372014-08-25 14:12:26 +0200314 ctrl_reg |= SDIO_HOST_CTRL_TMOUT_EN;
DrEaglead881742014-07-25 21:07:30 +0200315
316 ctrl_reg |= SDIO_HOST_CTRL_PUSH_PULL_EN;
317
318 ctrl_reg |= SDIO_HOST_CTRL_CARD_TYPE_MEM_ONLY;
319
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200320 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) ?
326 "high-speed" : "");
DrEaglead881742014-07-25 21:07:30 +0200327
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200328 mvebu_mmc_write(mmc, SDIO_HOST_CTRL, ctrl_reg);
DrEaglead881742014-07-25 21:07:30 +0200329}
330
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200331static int mvebu_mmc_set_ios(struct udevice *dev)
DrEaglead881742014-07-25 21:07:30 +0200332{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200333 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
334 struct mmc *mmc = &pdata->mmc;
335
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);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900340
341 return 0;
DrEaglead881742014-07-25 21:07:30 +0200342}
343
Mario Schuknecht6764f372014-08-25 14:12:26 +0200344/*
345 * Set window register.
346 */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200347static void mvebu_window_setup(const struct mmc *mmc)
Mario Schuknecht6764f372014-08-25 14:12:26 +0200348{
349 int i;
350
351 for (i = 0; i < 4; i++) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200352 mvebu_mmc_write(mmc, WINDOW_CTRL(i), 0);
353 mvebu_mmc_write(mmc, WINDOW_BASE(i), 0);
Mario Schuknecht6764f372014-08-25 14:12:26 +0200354 }
355 for (i = 0; i < CONFIG_NR_DRAM_BANKS; i++) {
356 u32 size, base, attrib;
357
358 /* Enable DRAM bank */
359 switch (i) {
360 case 0:
361 attrib = KWCPU_ATTR_DRAM_CS0;
362 break;
363 case 1:
364 attrib = KWCPU_ATTR_DRAM_CS1;
365 break;
366 case 2:
367 attrib = KWCPU_ATTR_DRAM_CS2;
368 break;
369 case 3:
370 attrib = KWCPU_ATTR_DRAM_CS3;
371 break;
372 default:
373 /* invalide bank, disable access */
374 attrib = 0;
375 break;
376 }
377
378 size = gd->bd->bi_dram[i].size;
379 base = gd->bd->bi_dram[i].start;
380 if (size && attrib) {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200381 mvebu_mmc_write(mmc, WINDOW_CTRL(i),
Mario Schuknecht6764f372014-08-25 14:12:26 +0200382 MVCPU_WIN_CTRL_DATA(size,
383 MVEBU_TARGET_DRAM,
384 attrib,
385 MVCPU_WIN_ENABLE));
386 } else {
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200387 mvebu_mmc_write(mmc, WINDOW_CTRL(i), MVCPU_WIN_DISABLE);
Mario Schuknecht6764f372014-08-25 14:12:26 +0200388 }
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200389 mvebu_mmc_write(mmc, WINDOW_BASE(i), base);
Mario Schuknecht6764f372014-08-25 14:12:26 +0200390 }
391}
392
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200393static int mvebu_mmc_initialize(struct udevice *dev)
DrEaglead881742014-07-25 21:07:30 +0200394{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200395 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
396 struct mmc *mmc = &pdata->mmc;
397
398 dev_dbg(dev, "%s\n", __func__);
DrEaglead881742014-07-25 21:07:30 +0200399
400 /*
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
404 */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200405 mvebu_mmc_write(mmc, SDIO_HOST_CTRL,
DrEaglead881742014-07-25 21:07:30 +0200406 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);
411
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200412 mvebu_mmc_write(mmc, SDIO_CLK_CTRL, 0);
DrEaglead881742014-07-25 21:07:30 +0200413
414 /* enable status */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200415 mvebu_mmc_write(mmc, SDIO_NOR_STATUS_EN, SDIO_POLL_MASK);
416 mvebu_mmc_write(mmc, SDIO_ERR_STATUS_EN, SDIO_POLL_MASK);
DrEaglead881742014-07-25 21:07:30 +0200417
418 /* disable interrupts */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200419 mvebu_mmc_write(mmc, SDIO_NOR_INTR_EN, 0);
420 mvebu_mmc_write(mmc, SDIO_ERR_INTR_EN, 0);
DrEaglead881742014-07-25 21:07:30 +0200421
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200422 mvebu_window_setup(mmc);
Mario Schuknecht6764f372014-08-25 14:12:26 +0200423
DrEaglead881742014-07-25 21:07:30 +0200424 /* SW reset */
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200425 mvebu_mmc_write(mmc, SDIO_SW_RESET, SDIO_SW_RESET_NOW);
DrEaglead881742014-07-25 21:07:30 +0200426
DrEaglead881742014-07-25 21:07:30 +0200427 return 0;
428}
429
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200430static int mvebu_mmc_of_to_plat(struct udevice *dev)
431{
432 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
433 fdt_addr_t addr;
DrEaglead881742014-07-25 21:07:30 +0200434
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200435 addr = dev_read_addr(dev);
436 if (addr == FDT_ADDR_T_NONE)
437 return -EINVAL;
DrEaglead881742014-07-25 21:07:30 +0200438
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200439 pdata->iobase = (void *)addr;
440
441 return 0;
442}
443
444static int mvebu_mmc_probe(struct udevice *dev)
DrEaglead881742014-07-25 21:07:30 +0200445{
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200446 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;
DrEaglead881742014-07-25 21:07:30 +0200450
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200451 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;
DrEaglead881742014-07-25 21:07:30 +0200458
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200459 mmc->cfg = cfg;
460 mmc->priv = pdata;
461 mmc->dev = dev;
462 upriv->mmc = mmc;
463
464 mvebu_mmc_power_up(dev);
465 mvebu_mmc_initialize(dev);
DrEaglead881742014-07-25 21:07:30 +0200466
467 return 0;
468}
Harm Berntsen7b9e5af2021-03-30 10:19:41 +0200469
470static const struct dm_mmc_ops mvebu_dm_mmc_ops = {
471 .send_cmd = mvebu_mmc_send_cmd,
472 .set_ios = mvebu_mmc_set_ios,
473};
474
475static int mvebu_mmc_bind(struct udevice *dev)
476{
477 struct mvebu_mmc_plat *pdata = dev_get_plat(dev);
478
479 return mmc_bind(dev, &pdata->mmc, &pdata->cfg);
480}
481
482static const struct udevice_id mvebu_mmc_match[] = {
483 { .compatible = "marvell,orion-sdio" },
484 { /* sentinel */ }
485};
486
487U_BOOT_DRIVER(mvebu_mmc) = {
488 .name = "mvebu_mmc",
489 .id = UCLASS_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),
496};