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