blob: 17f1ebf5d2bdd223409d12dd2c00f099ce783933 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasut71a758e12011-11-08 23:18:09 +00002/*
3 * Freescale i.MX28 SSP MMC driver
4 *
Lukasz Majewski6116f4c2019-09-05 09:54:59 +02005 * Copyright (C) 2019 DENX Software Engineering
6 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
7 *
Marek Vasut71a758e12011-11-08 23:18:09 +00008 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
9 * on behalf of DENX Software Engineering GmbH
10 *
11 * Based on code from LTIB:
12 * (C) Copyright 2008-2010 Freescale Semiconductor, Inc.
13 * Terry Lv
14 *
15 * Copyright 2007, Freescale Semiconductor, Inc
16 * Andy Fleming
17 *
18 * Based vaguely on the pxa mmc code:
19 * (C) Copyright 2003
20 * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net
Marek Vasut71a758e12011-11-08 23:18:09 +000021 */
Lukasz Majewski6116f4c2019-09-05 09:54:59 +020022
Marek Vasut71a758e12011-11-08 23:18:09 +000023#include <common.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -060024#include <log.h>
Marek Vasut71a758e12011-11-08 23:18:09 +000025#include <malloc.h>
26#include <mmc.h>
Masahiro Yamada1221ce42016-09-21 11:28:55 +090027#include <linux/errno.h>
Marek Vasut71a758e12011-11-08 23:18:09 +000028#include <asm/io.h>
29#include <asm/arch/clock.h>
30#include <asm/arch/imx-regs.h>
31#include <asm/arch/sys_proto.h>
Stefano Babic552a8482017-06-29 10:16:06 +020032#include <asm/mach-imx/dma.h>
Marek Vasut4e6d81d2012-08-26 15:19:07 +000033#include <bouncebuf.h>
Marek Vasut71a758e12011-11-08 23:18:09 +000034
Marek Vasut71a758e12011-11-08 23:18:09 +000035#define MXSMMC_MAX_TIMEOUT 10000
Marek Vasut20255902012-07-06 21:25:56 +000036#define MXSMMC_SMALL_TRANSFER 512
Marek Vasut71a758e12011-11-08 23:18:09 +000037
Lukasz Majewski6116f4c2019-09-05 09:54:59 +020038#if !CONFIG_IS_ENABLED(DM_MMC)
39struct mxsmmc_priv {
40 int id;
41 int (*mmc_is_wp)(int);
42 int (*mmc_cd)(int);
43 struct mmc_config cfg; /* mmc configuration */
44 struct mxs_dma_desc *desc;
45 uint32_t buswidth;
46 struct mxs_ssp_regs *regs;
47};
48#else /* CONFIG_IS_ENABLED(DM_MMC) */
49#include <dm/device.h>
50#include <dm/read.h>
51#include <dt-structs.h>
52
53#ifdef CONFIG_MX28
54#define dtd_fsl_imx_mmc dtd_fsl_imx28_mmc
55#else /* CONFIG_MX23 */
56#define dtd_fsl_imx_mmc dtd_fsl_imx23_mmc
57#endif
58
59struct mxsmmc_platdata {
60#if CONFIG_IS_ENABLED(OF_PLATDATA)
61 struct dtd_fsl_imx_mmc dtplat;
62#endif
63 struct mmc_config cfg;
64 struct mmc mmc;
65 fdt_addr_t base;
66 int non_removable;
67 int buswidth;
68 int dma_id;
69 int clk_id;
70};
71
72struct mxsmmc_priv {
73 int clkid;
74 struct mxs_dma_desc *desc;
75 u32 buswidth;
76 struct mxs_ssp_regs *regs;
77 unsigned int dma_channel;
78};
79#endif
80
81#if !CONFIG_IS_ENABLED(DM_MMC)
82static int mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
83 struct mmc_data *data);
84
Marek Vasut90bc2bf2013-01-22 15:01:03 +000085static int mxsmmc_cd(struct mxsmmc_priv *priv)
86{
87 struct mxs_ssp_regs *ssp_regs = priv->regs;
88
89 if (priv->mmc_cd)
90 return priv->mmc_cd(priv->id);
91
92 return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT);
93}
94
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +090095static int mxsmmc_set_ios(struct mmc *mmc)
Marek Vasut71a758e12011-11-08 23:18:09 +000096{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +020097 struct mxsmmc_priv *priv = mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +000098 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +000099
100 /* Set the clock speed */
101 if (mmc->clock)
Otavio Salvadorbf48fcb2013-01-11 03:19:03 +0000102 mxs_set_ssp_busclock(priv->id, mmc->clock / 1000);
Marek Vasut71a758e12011-11-08 23:18:09 +0000103
104 switch (mmc->bus_width) {
105 case 1:
106 priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT;
107 break;
108 case 4:
109 priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT;
110 break;
111 case 8:
112 priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT;
113 break;
114 }
115
116 /* Set the bus width */
117 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
118 SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth);
119
120 debug("MMC%d: Set %d bits bus width\n",
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200121 mmc->block_dev.devnum, mmc->bus_width);
Jaehoon Chung07b0b9c2016-12-30 15:30:16 +0900122
123 return 0;
Marek Vasut71a758e12011-11-08 23:18:09 +0000124}
125
126static int mxsmmc_init(struct mmc *mmc)
127{
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200128 struct mxsmmc_priv *priv = mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +0000129 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +0000130
131 /* Reset SSP */
Otavio Salvadorfa7a51c2012-08-13 09:53:12 +0000132 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
Marek Vasut71a758e12011-11-08 23:18:09 +0000133
Otavio Salvador8000d8a2013-01-22 15:01:02 +0000134 /* Reconfigure the SSP block for MMC operation */
135 writel(SSP_CTRL1_SSP_MODE_SD_MMC |
136 SSP_CTRL1_WORD_LENGTH_EIGHT_BITS |
137 SSP_CTRL1_DMA_ENABLE |
138 SSP_CTRL1_POLARITY |
139 SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
140 SSP_CTRL1_DATA_CRC_IRQ_EN |
141 SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
142 SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
143 SSP_CTRL1_RESP_ERR_IRQ_EN,
144 &ssp_regs->hw_ssp_ctrl1_set);
Marek Vasut71a758e12011-11-08 23:18:09 +0000145
146 /* Set initial bit clock 400 KHz */
Otavio Salvadorbf48fcb2013-01-11 03:19:03 +0000147 mxs_set_ssp_busclock(priv->id, 400);
Marek Vasut71a758e12011-11-08 23:18:09 +0000148
149 /* Send initial 74 clock cycles (185 us @ 400 KHz)*/
150 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set);
151 udelay(200);
152 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr);
153
154 return 0;
155}
156
Pantelis Antoniouab769f22014-02-26 19:28:45 +0200157static const struct mmc_ops mxsmmc_ops = {
158 .send_cmd = mxsmmc_send_cmd,
159 .set_ios = mxsmmc_set_ios,
160 .init = mxsmmc_init,
161};
162
Marek Vasut90bc2bf2013-01-22 15:01:03 +0000163int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int), int (*cd)(int))
Marek Vasut71a758e12011-11-08 23:18:09 +0000164{
Marek Vasut71a758e12011-11-08 23:18:09 +0000165 struct mmc *mmc = NULL;
166 struct mxsmmc_priv *priv = NULL;
Marek Vasut96666a32012-04-08 17:34:46 +0000167 int ret;
Marek Vasut3430e0b2013-02-23 02:42:58 +0000168 const unsigned int mxsmmc_clk_id = mxs_ssp_clock_by_bus(id);
Marek Vasut1a3c5ff2013-01-11 03:19:14 +0000169
Marek Vasut3430e0b2013-02-23 02:42:58 +0000170 if (!mxs_ssp_bus_id_valid(id))
Marek Vasut1a3c5ff2013-01-11 03:19:14 +0000171 return -ENODEV;
Marek Vasut71a758e12011-11-08 23:18:09 +0000172
Marek Vasut71a758e12011-11-08 23:18:09 +0000173 priv = malloc(sizeof(struct mxsmmc_priv));
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200174 if (!priv)
Marek Vasut71a758e12011-11-08 23:18:09 +0000175 return -ENOMEM;
Marek Vasut71a758e12011-11-08 23:18:09 +0000176
Marek Vasut3687c412012-03-15 18:33:21 +0000177 priv->desc = mxs_dma_desc_alloc();
178 if (!priv->desc) {
179 free(priv);
Marek Vasut3687c412012-03-15 18:33:21 +0000180 return -ENOMEM;
181 }
182
Marek Vasut3430e0b2013-02-23 02:42:58 +0000183 ret = mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + id);
Marek Vasut96666a32012-04-08 17:34:46 +0000184 if (ret)
185 return ret;
186
Marek Vasut71a758e12011-11-08 23:18:09 +0000187 priv->mmc_is_wp = wp;
Marek Vasut90bc2bf2013-01-22 15:01:03 +0000188 priv->mmc_cd = cd;
Marek Vasut71a758e12011-11-08 23:18:09 +0000189 priv->id = id;
Marek Vasut14e26bc2013-01-11 03:19:02 +0000190 priv->regs = mxs_ssp_regs_by_bus(id);
Marek Vasut71a758e12011-11-08 23:18:09 +0000191
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200192 priv->cfg.name = "MXS MMC";
193 priv->cfg.ops = &mxsmmc_ops;
Marek Vasut71a758e12011-11-08 23:18:09 +0000194
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200195 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasut71a758e12011-11-08 23:18:09 +0000196
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200197 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
Rob Herring5a203972015-03-23 17:56:59 -0500198 MMC_MODE_HS_52MHz | MMC_MODE_HS;
Marek Vasut71a758e12011-11-08 23:18:09 +0000199
200 /*
201 * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz
202 * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
203 * CLOCK_DIVIDE has to be an even value from 2 to 254, and
204 * CLOCK_RATE could be any integer from 0 to 255.
205 */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200206 priv->cfg.f_min = 400000;
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200207 priv->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id)
208 * 1000 / 2;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200209 priv->cfg.b_max = 0x20;
Marek Vasut71a758e12011-11-08 23:18:09 +0000210
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200211 mmc = mmc_create(&priv->cfg, priv);
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200212 if (!mmc) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200213 mxs_dma_desc_free(priv->desc);
214 free(priv);
215 return -ENOMEM;
216 }
Marek Vasut71a758e12011-11-08 23:18:09 +0000217 return 0;
218}
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200219#endif /* CONFIG_IS_ENABLED(DM_MMC) */
220
221static int mxsmmc_send_cmd_pio(struct mxsmmc_priv *priv, struct mmc_data *data)
222{
223 struct mxs_ssp_regs *ssp_regs = priv->regs;
224 uint32_t *data_ptr;
225 int timeout = MXSMMC_MAX_TIMEOUT;
226 uint32_t reg;
227 uint32_t data_count = data->blocksize * data->blocks;
228
229 if (data->flags & MMC_DATA_READ) {
230 data_ptr = (uint32_t *)data->dest;
231 while (data_count && --timeout) {
232 reg = readl(&ssp_regs->hw_ssp_status);
233 if (!(reg & SSP_STATUS_FIFO_EMPTY)) {
234 *data_ptr++ = readl(&ssp_regs->hw_ssp_data);
235 data_count -= 4;
236 timeout = MXSMMC_MAX_TIMEOUT;
237 } else
238 udelay(1000);
239 }
240 } else {
241 data_ptr = (uint32_t *)data->src;
242 timeout *= 100;
243 while (data_count && --timeout) {
244 reg = readl(&ssp_regs->hw_ssp_status);
245 if (!(reg & SSP_STATUS_FIFO_FULL)) {
246 writel(*data_ptr++, &ssp_regs->hw_ssp_data);
247 data_count -= 4;
248 timeout = MXSMMC_MAX_TIMEOUT;
249 } else
250 udelay(1000);
251 }
252 }
253
254 return timeout ? 0 : -ECOMM;
255}
256
257static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data)
258{
259 uint32_t data_count = data->blocksize * data->blocks;
260 int dmach;
261 struct mxs_dma_desc *desc = priv->desc;
262 void *addr;
263 unsigned int flags;
264 struct bounce_buffer bbstate;
265
266 memset(desc, 0, sizeof(struct mxs_dma_desc));
267 desc->address = (dma_addr_t)desc;
268
269 if (data->flags & MMC_DATA_READ) {
270 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
271 addr = data->dest;
272 flags = GEN_BB_WRITE;
273 } else {
274 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
275 addr = (void *)data->src;
276 flags = GEN_BB_READ;
277 }
278
279 bounce_buffer_start(&bbstate, addr, data_count, flags);
280
281 priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer;
282
283 priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM |
284 (data_count << MXS_DMA_DESC_BYTES_OFFSET);
285
286#if !CONFIG_IS_ENABLED(DM_MMC)
287 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id;
288#else
289 dmach = priv->dma_channel;
290#endif
291 mxs_dma_desc_append(dmach, priv->desc);
292 if (mxs_dma_go(dmach)) {
293 bounce_buffer_stop(&bbstate);
294 return -ECOMM;
295 }
296
297 bounce_buffer_stop(&bbstate);
298
299 return 0;
300}
301
302#if !CONFIG_IS_ENABLED(DM_MMC)
303/*
304 * Sends a command out on the bus. Takes the mmc pointer,
305 * a command pointer, and an optional data pointer.
306 */
307static int
308mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
309{
310 struct mxsmmc_priv *priv = mmc->priv;
311 struct mxs_ssp_regs *ssp_regs = priv->regs;
312#else
313static int
314mxsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, struct mmc_data *data)
315{
316 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
317 struct mxsmmc_priv *priv = dev_get_priv(dev);
318 struct mxs_ssp_regs *ssp_regs = priv->regs;
319 struct mmc *mmc = &plat->mmc;
320#endif
321 uint32_t reg;
322 int timeout;
323 uint32_t ctrl0;
324 int ret;
325#if !CONFIG_IS_ENABLED(DM_MMC)
326 int devnum = mmc->block_dev.devnum;
327#else
328 int devnum = mmc_get_blk_desc(mmc)->devnum;
329#endif
330 debug("MMC%d: CMD%d\n", devnum, cmd->cmdidx);
331
332 /* Check bus busy */
333 timeout = MXSMMC_MAX_TIMEOUT;
334 while (--timeout) {
335 udelay(1000);
336 reg = readl(&ssp_regs->hw_ssp_status);
337 if (!(reg &
338 (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY |
339 SSP_STATUS_CMD_BUSY))) {
340 break;
341 }
342 }
343
344 if (!timeout) {
345 printf("MMC%d: Bus busy timeout!\n", devnum);
346 return -ETIMEDOUT;
347 }
348#if !CONFIG_IS_ENABLED(DM_MMC)
349 /* See if card is present */
350 if (!mxsmmc_cd(priv)) {
351 printf("MMC%d: No card detected!\n", devnum);
352 return -ENOMEDIUM;
353 }
354#endif
355 /* Start building CTRL0 contents */
356 ctrl0 = priv->buswidth;
357
358 /* Set up command */
359 if (!(cmd->resp_type & MMC_RSP_CRC))
360 ctrl0 |= SSP_CTRL0_IGNORE_CRC;
361 if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */
362 ctrl0 |= SSP_CTRL0_GET_RESP;
363 if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */
364 ctrl0 |= SSP_CTRL0_LONG_RESP;
365
366 if (data && (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER))
367 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
368 else
369 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
370
371 /* Command index */
372 reg = readl(&ssp_regs->hw_ssp_cmd0);
373 reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC);
374 reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET;
375 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
376 reg |= SSP_CMD0_APPEND_8CYC;
377 writel(reg, &ssp_regs->hw_ssp_cmd0);
378
379 /* Command argument */
380 writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1);
381
382 /* Set up data */
383 if (data) {
384 /* READ or WRITE */
385 if (data->flags & MMC_DATA_READ) {
386 ctrl0 |= SSP_CTRL0_READ;
387#if !CONFIG_IS_ENABLED(DM_MMC)
388 } else if (priv->mmc_is_wp &&
389 priv->mmc_is_wp(devnum)) {
390 printf("MMC%d: Can not write a locked card!\n", devnum);
391 return -EOPNOTSUPP;
392#endif
393 }
394 ctrl0 |= SSP_CTRL0_DATA_XFER;
395
396 reg = data->blocksize * data->blocks;
397#if defined(CONFIG_MX23)
398 ctrl0 |= reg & SSP_CTRL0_XFER_COUNT_MASK;
399
400 clrsetbits_le32(&ssp_regs->hw_ssp_cmd0,
401 SSP_CMD0_BLOCK_SIZE_MASK | SSP_CMD0_BLOCK_COUNT_MASK,
402 ((data->blocks - 1) << SSP_CMD0_BLOCK_COUNT_OFFSET) |
403 ((ffs(data->blocksize) - 1) <<
404 SSP_CMD0_BLOCK_SIZE_OFFSET));
405#elif defined(CONFIG_MX28)
406 writel(reg, &ssp_regs->hw_ssp_xfer_size);
407
408 reg = ((data->blocks - 1) <<
409 SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) |
410 ((ffs(data->blocksize) - 1) <<
411 SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET);
412 writel(reg, &ssp_regs->hw_ssp_block_size);
413#endif
414 }
415
416 /* Kick off the command */
417 ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN;
418 writel(ctrl0, &ssp_regs->hw_ssp_ctrl0);
419
420 /* Wait for the command to complete */
421 timeout = MXSMMC_MAX_TIMEOUT;
422 while (--timeout) {
423 udelay(1000);
424 reg = readl(&ssp_regs->hw_ssp_status);
425 if (!(reg & SSP_STATUS_CMD_BUSY))
426 break;
427 }
428
429 if (!timeout) {
430 printf("MMC%d: Command %d busy\n", devnum, cmd->cmdidx);
431 return -ETIMEDOUT;
432 }
433
434 /* Check command timeout */
435 if (reg & SSP_STATUS_RESP_TIMEOUT) {
Lukasz Majewskicf319142019-09-05 09:55:00 +0200436 debug("MMC%d: Command %d timeout (status 0x%08x)\n",
437 devnum, cmd->cmdidx, reg);
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200438 return -ETIMEDOUT;
439 }
440
441 /* Check command errors */
442 if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) {
443 printf("MMC%d: Command %d error (status 0x%08x)!\n",
444 devnum, cmd->cmdidx, reg);
445 return -ECOMM;
446 }
447
448 /* Copy response to response buffer */
449 if (cmd->resp_type & MMC_RSP_136) {
450 cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0);
451 cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1);
452 cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2);
453 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3);
454 } else
455 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0);
456
457 /* Return if no data to process */
458 if (!data)
459 return 0;
460
461 if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) {
462 ret = mxsmmc_send_cmd_pio(priv, data);
463 if (ret) {
464 printf("MMC%d: Data timeout with command %d "
465 "(status 0x%08x)!\n", devnum, cmd->cmdidx, reg);
466 return ret;
467 }
468 } else {
469 ret = mxsmmc_send_cmd_dma(priv, data);
470 if (ret) {
471 printf("MMC%d: DMA transfer failed\n", devnum);
472 return ret;
473 }
474 }
475
476 /* Check data errors */
477 reg = readl(&ssp_regs->hw_ssp_status);
478 if (reg &
479 (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR |
480 SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) {
481 printf("MMC%d: Data error with command %d (status 0x%08x)!\n",
482 devnum, cmd->cmdidx, reg);
483 return -ECOMM;
484 }
485
486 return 0;
487}
488
489#if CONFIG_IS_ENABLED(DM_MMC)
490/* Base numbers of i.MX2[38] clk for ssp0 IP block */
491#define MXS_SSP_IMX23_CLKID_SSP0 33
492#define MXS_SSP_IMX28_CLKID_SSP0 46
493
494static int mxsmmc_get_cd(struct udevice *dev)
495{
496 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
497 struct mxsmmc_priv *priv = dev_get_priv(dev);
498 struct mxs_ssp_regs *ssp_regs = priv->regs;
499
500 if (plat->non_removable)
501 return 1;
502
503 return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT);
504}
505
506static int mxsmmc_set_ios(struct udevice *dev)
507{
508 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
509 struct mxsmmc_priv *priv = dev_get_priv(dev);
510 struct mxs_ssp_regs *ssp_regs = priv->regs;
511 struct mmc *mmc = &plat->mmc;
512
513 /* Set the clock speed */
514 if (mmc->clock)
515 mxs_set_ssp_busclock(priv->clkid, mmc->clock / 1000);
516
517 switch (mmc->bus_width) {
518 case 1:
519 priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT;
520 break;
521 case 4:
522 priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT;
523 break;
524 case 8:
525 priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT;
526 break;
527 }
528
529 /* Set the bus width */
530 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
531 SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth);
532
533 debug("MMC%d: Set %d bits bus width\n", mmc_get_blk_desc(mmc)->devnum,
534 mmc->bus_width);
535
536 return 0;
537}
538
539static int mxsmmc_init(struct udevice *dev)
540{
541 struct mxsmmc_priv *priv = dev_get_priv(dev);
542 struct mxs_ssp_regs *ssp_regs = priv->regs;
543
544 /* Reset SSP */
545 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
546
547 /* Reconfigure the SSP block for MMC operation */
548 writel(SSP_CTRL1_SSP_MODE_SD_MMC |
549 SSP_CTRL1_WORD_LENGTH_EIGHT_BITS |
550 SSP_CTRL1_DMA_ENABLE |
551 SSP_CTRL1_POLARITY |
552 SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
553 SSP_CTRL1_DATA_CRC_IRQ_EN |
554 SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
555 SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
556 SSP_CTRL1_RESP_ERR_IRQ_EN,
557 &ssp_regs->hw_ssp_ctrl1_set);
558
559 /* Set initial bit clock 400 KHz */
560 mxs_set_ssp_busclock(priv->clkid, 400);
561
562 /* Send initial 74 clock cycles (185 us @ 400 KHz)*/
563 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set);
564 udelay(200);
565 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr);
566
567 return 0;
568}
569
570static int mxsmmc_probe(struct udevice *dev)
571{
572 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
573 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
574 struct mxsmmc_priv *priv = dev_get_priv(dev);
575 struct blk_desc *bdesc;
576 struct mmc *mmc;
577 int ret, clkid;
578
579 debug("%s: probe\n", __func__);
580
581#if CONFIG_IS_ENABLED(OF_PLATDATA)
582 struct dtd_fsl_imx_mmc *dtplat = &plat->dtplat;
583 struct phandle_1_arg *p1a = &dtplat->clocks[0];
584
585 priv->buswidth = dtplat->bus_width;
586 priv->regs = (struct mxs_ssp_regs *)dtplat->reg[0];
587 priv->dma_channel = dtplat->dmas[1];
588 clkid = p1a->arg[0];
589 plat->non_removable = dtplat->non_removable;
590
591 debug("OF_PLATDATA: regs: 0x%p bw: %d clkid: %d non_removable: %d\n",
592 priv->regs, priv->buswidth, clkid, plat->non_removable);
593#else
594 priv->regs = (struct mxs_ssp_regs *)plat->base;
595 priv->dma_channel = plat->dma_id;
596 clkid = plat->clk_id;
597#endif
598
599#ifdef CONFIG_MX28
600 priv->clkid = clkid - MXS_SSP_IMX28_CLKID_SSP0;
601#else /* CONFIG_MX23 */
602 priv->clkid = clkid - MXS_SSP_IMX23_CLKID_SSP0;
603#endif
604 mmc = &plat->mmc;
605 mmc->cfg = &plat->cfg;
606 mmc->dev = dev;
607
608 priv->desc = mxs_dma_desc_alloc();
609 if (!priv->desc) {
610 printf("%s: Cannot allocate DMA descriptor\n", __func__);
611 return -ENOMEM;
612 }
613
614 ret = mxs_dma_init_channel(priv->dma_channel);
615 if (ret)
616 return ret;
617
618 plat->cfg.name = "MXS MMC";
619 plat->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
620
621 plat->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
622 MMC_MODE_HS_52MHz | MMC_MODE_HS;
623
624 /*
625 * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz
626 * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
627 * CLOCK_DIVIDE has to be an even value from 2 to 254, and
628 * CLOCK_RATE could be any integer from 0 to 255.
629 */
630 plat->cfg.f_min = 400000;
631 plat->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + priv->clkid) * 1000 / 2;
632 plat->cfg.b_max = 0x20;
633
634 bdesc = mmc_get_blk_desc(mmc);
635 if (!bdesc) {
636 printf("%s: No block device descriptor!\n", __func__);
637 return -ENODEV;
638 }
639
640 if (plat->non_removable)
641 bdesc->removable = 0;
642
643 ret = mxsmmc_init(dev);
644 if (ret)
645 printf("%s: MMC%d init error %d\n", __func__,
646 bdesc->devnum, ret);
647
648 /* Set the initial clock speed */
649 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
650
651 upriv->mmc = mmc;
652
653 return 0;
654};
655
656#if CONFIG_IS_ENABLED(BLK)
657static int mxsmmc_bind(struct udevice *dev)
658{
659 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
660
661 return mmc_bind(dev, &plat->mmc, &plat->cfg);
662}
663#endif
664
665static const struct dm_mmc_ops mxsmmc_ops = {
666 .get_cd = mxsmmc_get_cd,
667 .send_cmd = mxsmmc_send_cmd,
668 .set_ios = mxsmmc_set_ios,
669};
670
671#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
672static int mxsmmc_ofdata_to_platdata(struct udevice *bus)
673{
674 struct mxsmmc_platdata *plat = bus->platdata;
675 u32 prop[2];
676 int ret;
677
678 plat->base = dev_read_addr(bus);
679 plat->buswidth =
680 dev_read_u32_default(bus, "bus-width", 1);
681 plat->non_removable = dev_read_bool(bus, "non-removable");
682
683 ret = dev_read_u32_array(bus, "dmas", prop, ARRAY_SIZE(prop));
684 if (ret) {
685 printf("%s: Reading 'dmas' property failed!\n", __func__);
686 return ret;
687 }
688 plat->dma_id = prop[1];
689
690 ret = dev_read_u32_array(bus, "clocks", prop, ARRAY_SIZE(prop));
691 if (ret) {
692 printf("%s: Reading 'clocks' property failed!\n", __func__);
693 return ret;
694 }
695 plat->clk_id = prop[1];
696
697 debug("%s: base=0x%x, bus_width=%d %s dma_id=%d clk_id=%d\n",
698 __func__, (uint)plat->base, plat->buswidth,
699 plat->non_removable ? "non-removable" : NULL,
700 plat->dma_id, plat->clk_id);
701
702 return 0;
703}
704
705static const struct udevice_id mxsmmc_ids[] = {
706 { .compatible = "fsl,imx23-mmc", },
707 { .compatible = "fsl,imx28-mmc", },
708 { /* sentinel */ }
709};
710#endif
711
712U_BOOT_DRIVER(mxsmmc) = {
713#ifdef CONFIG_MX28
714 .name = "fsl_imx28_mmc",
715#else /* CONFIG_MX23 */
716 .name = "fsl_imx23_mmc",
717#endif
718 .id = UCLASS_MMC,
719#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
720 .of_match = mxsmmc_ids,
721 .ofdata_to_platdata = mxsmmc_ofdata_to_platdata,
722#endif
723 .ops = &mxsmmc_ops,
724#if CONFIG_IS_ENABLED(BLK)
725 .bind = mxsmmc_bind,
726#endif
727 .probe = mxsmmc_probe,
728 .priv_auto_alloc_size = sizeof(struct mxsmmc_priv),
729 .platdata_auto_alloc_size = sizeof(struct mxsmmc_platdata),
730};
731
732#endif /* CONFIG_DM_MMC */