blob: d48050ba8a5455297bd92f98803f57499cc9f02c [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
Masahiro Yamadab75d8dc2020-06-26 15:13:33 +0900165int mxsmmc_initialize(struct bd_info *bis, int id, int (*wp)(int),
166 int (*cd)(int))
Marek Vasut71a758e12011-11-08 23:18:09 +0000167{
Marek Vasut71a758e12011-11-08 23:18:09 +0000168 struct mmc *mmc = NULL;
169 struct mxsmmc_priv *priv = NULL;
Marek Vasut96666a32012-04-08 17:34:46 +0000170 int ret;
Marek Vasut3430e0b2013-02-23 02:42:58 +0000171 const unsigned int mxsmmc_clk_id = mxs_ssp_clock_by_bus(id);
Marek Vasut1a3c5ff2013-01-11 03:19:14 +0000172
Marek Vasut3430e0b2013-02-23 02:42:58 +0000173 if (!mxs_ssp_bus_id_valid(id))
Marek Vasut1a3c5ff2013-01-11 03:19:14 +0000174 return -ENODEV;
Marek Vasut71a758e12011-11-08 23:18:09 +0000175
Marek Vasut71a758e12011-11-08 23:18:09 +0000176 priv = malloc(sizeof(struct mxsmmc_priv));
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200177 if (!priv)
Marek Vasut71a758e12011-11-08 23:18:09 +0000178 return -ENOMEM;
Marek Vasut71a758e12011-11-08 23:18:09 +0000179
Marek Vasut3687c412012-03-15 18:33:21 +0000180 priv->desc = mxs_dma_desc_alloc();
181 if (!priv->desc) {
182 free(priv);
Marek Vasut3687c412012-03-15 18:33:21 +0000183 return -ENOMEM;
184 }
185
Marek Vasut3430e0b2013-02-23 02:42:58 +0000186 ret = mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + id);
Marek Vasut96666a32012-04-08 17:34:46 +0000187 if (ret)
188 return ret;
189
Marek Vasut71a758e12011-11-08 23:18:09 +0000190 priv->mmc_is_wp = wp;
Marek Vasut90bc2bf2013-01-22 15:01:03 +0000191 priv->mmc_cd = cd;
Marek Vasut71a758e12011-11-08 23:18:09 +0000192 priv->id = id;
Marek Vasut14e26bc2013-01-11 03:19:02 +0000193 priv->regs = mxs_ssp_regs_by_bus(id);
Marek Vasut71a758e12011-11-08 23:18:09 +0000194
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200195 priv->cfg.name = "MXS MMC";
196 priv->cfg.ops = &mxsmmc_ops;
Marek Vasut71a758e12011-11-08 23:18:09 +0000197
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200198 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasut71a758e12011-11-08 23:18:09 +0000199
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200200 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
Rob Herring5a203972015-03-23 17:56:59 -0500201 MMC_MODE_HS_52MHz | MMC_MODE_HS;
Marek Vasut71a758e12011-11-08 23:18:09 +0000202
203 /*
204 * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz
205 * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
206 * CLOCK_DIVIDE has to be an even value from 2 to 254, and
207 * CLOCK_RATE could be any integer from 0 to 255.
208 */
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200209 priv->cfg.f_min = 400000;
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200210 priv->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id)
211 * 1000 / 2;
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200212 priv->cfg.b_max = 0x20;
Marek Vasut71a758e12011-11-08 23:18:09 +0000213
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200214 mmc = mmc_create(&priv->cfg, priv);
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200215 if (!mmc) {
Pantelis Antoniou93bfd612014-03-11 19:34:20 +0200216 mxs_dma_desc_free(priv->desc);
217 free(priv);
218 return -ENOMEM;
219 }
Marek Vasut71a758e12011-11-08 23:18:09 +0000220 return 0;
221}
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200222#endif /* CONFIG_IS_ENABLED(DM_MMC) */
223
224static int mxsmmc_send_cmd_pio(struct mxsmmc_priv *priv, struct mmc_data *data)
225{
226 struct mxs_ssp_regs *ssp_regs = priv->regs;
227 uint32_t *data_ptr;
228 int timeout = MXSMMC_MAX_TIMEOUT;
229 uint32_t reg;
230 uint32_t data_count = data->blocksize * data->blocks;
231
232 if (data->flags & MMC_DATA_READ) {
233 data_ptr = (uint32_t *)data->dest;
234 while (data_count && --timeout) {
235 reg = readl(&ssp_regs->hw_ssp_status);
236 if (!(reg & SSP_STATUS_FIFO_EMPTY)) {
237 *data_ptr++ = readl(&ssp_regs->hw_ssp_data);
238 data_count -= 4;
239 timeout = MXSMMC_MAX_TIMEOUT;
240 } else
241 udelay(1000);
242 }
243 } else {
244 data_ptr = (uint32_t *)data->src;
245 timeout *= 100;
246 while (data_count && --timeout) {
247 reg = readl(&ssp_regs->hw_ssp_status);
248 if (!(reg & SSP_STATUS_FIFO_FULL)) {
249 writel(*data_ptr++, &ssp_regs->hw_ssp_data);
250 data_count -= 4;
251 timeout = MXSMMC_MAX_TIMEOUT;
252 } else
253 udelay(1000);
254 }
255 }
256
257 return timeout ? 0 : -ECOMM;
258}
259
260static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data)
261{
262 uint32_t data_count = data->blocksize * data->blocks;
263 int dmach;
264 struct mxs_dma_desc *desc = priv->desc;
265 void *addr;
266 unsigned int flags;
267 struct bounce_buffer bbstate;
268
269 memset(desc, 0, sizeof(struct mxs_dma_desc));
270 desc->address = (dma_addr_t)desc;
271
272 if (data->flags & MMC_DATA_READ) {
273 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
274 addr = data->dest;
275 flags = GEN_BB_WRITE;
276 } else {
277 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
278 addr = (void *)data->src;
279 flags = GEN_BB_READ;
280 }
281
282 bounce_buffer_start(&bbstate, addr, data_count, flags);
283
284 priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer;
285
286 priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM |
287 (data_count << MXS_DMA_DESC_BYTES_OFFSET);
288
289#if !CONFIG_IS_ENABLED(DM_MMC)
290 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id;
291#else
292 dmach = priv->dma_channel;
293#endif
294 mxs_dma_desc_append(dmach, priv->desc);
295 if (mxs_dma_go(dmach)) {
296 bounce_buffer_stop(&bbstate);
297 return -ECOMM;
298 }
299
300 bounce_buffer_stop(&bbstate);
301
302 return 0;
303}
304
305#if !CONFIG_IS_ENABLED(DM_MMC)
306/*
307 * Sends a command out on the bus. Takes the mmc pointer,
308 * a command pointer, and an optional data pointer.
309 */
310static int
311mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
312{
313 struct mxsmmc_priv *priv = mmc->priv;
314 struct mxs_ssp_regs *ssp_regs = priv->regs;
315#else
316static int
317mxsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, struct mmc_data *data)
318{
319 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
320 struct mxsmmc_priv *priv = dev_get_priv(dev);
321 struct mxs_ssp_regs *ssp_regs = priv->regs;
322 struct mmc *mmc = &plat->mmc;
323#endif
324 uint32_t reg;
325 int timeout;
326 uint32_t ctrl0;
327 int ret;
328#if !CONFIG_IS_ENABLED(DM_MMC)
329 int devnum = mmc->block_dev.devnum;
330#else
331 int devnum = mmc_get_blk_desc(mmc)->devnum;
332#endif
333 debug("MMC%d: CMD%d\n", devnum, cmd->cmdidx);
334
335 /* Check bus busy */
336 timeout = MXSMMC_MAX_TIMEOUT;
337 while (--timeout) {
338 udelay(1000);
339 reg = readl(&ssp_regs->hw_ssp_status);
340 if (!(reg &
341 (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY |
342 SSP_STATUS_CMD_BUSY))) {
343 break;
344 }
345 }
346
347 if (!timeout) {
348 printf("MMC%d: Bus busy timeout!\n", devnum);
349 return -ETIMEDOUT;
350 }
351#if !CONFIG_IS_ENABLED(DM_MMC)
352 /* See if card is present */
353 if (!mxsmmc_cd(priv)) {
354 printf("MMC%d: No card detected!\n", devnum);
355 return -ENOMEDIUM;
356 }
357#endif
358 /* Start building CTRL0 contents */
359 ctrl0 = priv->buswidth;
360
361 /* Set up command */
362 if (!(cmd->resp_type & MMC_RSP_CRC))
363 ctrl0 |= SSP_CTRL0_IGNORE_CRC;
364 if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */
365 ctrl0 |= SSP_CTRL0_GET_RESP;
366 if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */
367 ctrl0 |= SSP_CTRL0_LONG_RESP;
368
369 if (data && (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER))
370 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
371 else
372 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
373
374 /* Command index */
375 reg = readl(&ssp_regs->hw_ssp_cmd0);
376 reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC);
377 reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET;
378 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
379 reg |= SSP_CMD0_APPEND_8CYC;
380 writel(reg, &ssp_regs->hw_ssp_cmd0);
381
382 /* Command argument */
383 writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1);
384
385 /* Set up data */
386 if (data) {
387 /* READ or WRITE */
388 if (data->flags & MMC_DATA_READ) {
389 ctrl0 |= SSP_CTRL0_READ;
390#if !CONFIG_IS_ENABLED(DM_MMC)
391 } else if (priv->mmc_is_wp &&
392 priv->mmc_is_wp(devnum)) {
393 printf("MMC%d: Can not write a locked card!\n", devnum);
394 return -EOPNOTSUPP;
395#endif
396 }
397 ctrl0 |= SSP_CTRL0_DATA_XFER;
398
399 reg = data->blocksize * data->blocks;
400#if defined(CONFIG_MX23)
401 ctrl0 |= reg & SSP_CTRL0_XFER_COUNT_MASK;
402
403 clrsetbits_le32(&ssp_regs->hw_ssp_cmd0,
404 SSP_CMD0_BLOCK_SIZE_MASK | SSP_CMD0_BLOCK_COUNT_MASK,
405 ((data->blocks - 1) << SSP_CMD0_BLOCK_COUNT_OFFSET) |
406 ((ffs(data->blocksize) - 1) <<
407 SSP_CMD0_BLOCK_SIZE_OFFSET));
408#elif defined(CONFIG_MX28)
409 writel(reg, &ssp_regs->hw_ssp_xfer_size);
410
411 reg = ((data->blocks - 1) <<
412 SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) |
413 ((ffs(data->blocksize) - 1) <<
414 SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET);
415 writel(reg, &ssp_regs->hw_ssp_block_size);
416#endif
417 }
418
419 /* Kick off the command */
420 ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN;
421 writel(ctrl0, &ssp_regs->hw_ssp_ctrl0);
422
423 /* Wait for the command to complete */
424 timeout = MXSMMC_MAX_TIMEOUT;
425 while (--timeout) {
426 udelay(1000);
427 reg = readl(&ssp_regs->hw_ssp_status);
428 if (!(reg & SSP_STATUS_CMD_BUSY))
429 break;
430 }
431
432 if (!timeout) {
433 printf("MMC%d: Command %d busy\n", devnum, cmd->cmdidx);
434 return -ETIMEDOUT;
435 }
436
437 /* Check command timeout */
438 if (reg & SSP_STATUS_RESP_TIMEOUT) {
Lukasz Majewskicf319142019-09-05 09:55:00 +0200439 debug("MMC%d: Command %d timeout (status 0x%08x)\n",
440 devnum, cmd->cmdidx, reg);
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200441 return -ETIMEDOUT;
442 }
443
444 /* Check command errors */
445 if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) {
446 printf("MMC%d: Command %d error (status 0x%08x)!\n",
447 devnum, cmd->cmdidx, reg);
448 return -ECOMM;
449 }
450
451 /* Copy response to response buffer */
452 if (cmd->resp_type & MMC_RSP_136) {
453 cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0);
454 cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1);
455 cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2);
456 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3);
457 } else
458 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0);
459
460 /* Return if no data to process */
461 if (!data)
462 return 0;
463
464 if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) {
465 ret = mxsmmc_send_cmd_pio(priv, data);
466 if (ret) {
467 printf("MMC%d: Data timeout with command %d "
468 "(status 0x%08x)!\n", devnum, cmd->cmdidx, reg);
469 return ret;
470 }
471 } else {
472 ret = mxsmmc_send_cmd_dma(priv, data);
473 if (ret) {
474 printf("MMC%d: DMA transfer failed\n", devnum);
475 return ret;
476 }
477 }
478
479 /* Check data errors */
480 reg = readl(&ssp_regs->hw_ssp_status);
481 if (reg &
482 (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR |
483 SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) {
484 printf("MMC%d: Data error with command %d (status 0x%08x)!\n",
485 devnum, cmd->cmdidx, reg);
486 return -ECOMM;
487 }
488
489 return 0;
490}
491
492#if CONFIG_IS_ENABLED(DM_MMC)
493/* Base numbers of i.MX2[38] clk for ssp0 IP block */
494#define MXS_SSP_IMX23_CLKID_SSP0 33
495#define MXS_SSP_IMX28_CLKID_SSP0 46
496
497static int mxsmmc_get_cd(struct udevice *dev)
498{
499 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
500 struct mxsmmc_priv *priv = dev_get_priv(dev);
501 struct mxs_ssp_regs *ssp_regs = priv->regs;
502
503 if (plat->non_removable)
504 return 1;
505
506 return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT);
507}
508
509static int mxsmmc_set_ios(struct udevice *dev)
510{
511 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
512 struct mxsmmc_priv *priv = dev_get_priv(dev);
513 struct mxs_ssp_regs *ssp_regs = priv->regs;
514 struct mmc *mmc = &plat->mmc;
515
516 /* Set the clock speed */
517 if (mmc->clock)
518 mxs_set_ssp_busclock(priv->clkid, mmc->clock / 1000);
519
520 switch (mmc->bus_width) {
521 case 1:
522 priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT;
523 break;
524 case 4:
525 priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT;
526 break;
527 case 8:
528 priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT;
529 break;
530 }
531
532 /* Set the bus width */
533 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
534 SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth);
535
536 debug("MMC%d: Set %d bits bus width\n", mmc_get_blk_desc(mmc)->devnum,
537 mmc->bus_width);
538
539 return 0;
540}
541
542static int mxsmmc_init(struct udevice *dev)
543{
544 struct mxsmmc_priv *priv = dev_get_priv(dev);
545 struct mxs_ssp_regs *ssp_regs = priv->regs;
546
547 /* Reset SSP */
548 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
549
550 /* Reconfigure the SSP block for MMC operation */
551 writel(SSP_CTRL1_SSP_MODE_SD_MMC |
552 SSP_CTRL1_WORD_LENGTH_EIGHT_BITS |
553 SSP_CTRL1_DMA_ENABLE |
554 SSP_CTRL1_POLARITY |
555 SSP_CTRL1_RECV_TIMEOUT_IRQ_EN |
556 SSP_CTRL1_DATA_CRC_IRQ_EN |
557 SSP_CTRL1_DATA_TIMEOUT_IRQ_EN |
558 SSP_CTRL1_RESP_TIMEOUT_IRQ_EN |
559 SSP_CTRL1_RESP_ERR_IRQ_EN,
560 &ssp_regs->hw_ssp_ctrl1_set);
561
562 /* Set initial bit clock 400 KHz */
563 mxs_set_ssp_busclock(priv->clkid, 400);
564
565 /* Send initial 74 clock cycles (185 us @ 400 KHz)*/
566 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set);
567 udelay(200);
568 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr);
569
570 return 0;
571}
572
573static int mxsmmc_probe(struct udevice *dev)
574{
575 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
576 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
577 struct mxsmmc_priv *priv = dev_get_priv(dev);
578 struct blk_desc *bdesc;
579 struct mmc *mmc;
580 int ret, clkid;
581
582 debug("%s: probe\n", __func__);
583
584#if CONFIG_IS_ENABLED(OF_PLATDATA)
585 struct dtd_fsl_imx_mmc *dtplat = &plat->dtplat;
586 struct phandle_1_arg *p1a = &dtplat->clocks[0];
587
588 priv->buswidth = dtplat->bus_width;
589 priv->regs = (struct mxs_ssp_regs *)dtplat->reg[0];
590 priv->dma_channel = dtplat->dmas[1];
591 clkid = p1a->arg[0];
592 plat->non_removable = dtplat->non_removable;
593
594 debug("OF_PLATDATA: regs: 0x%p bw: %d clkid: %d non_removable: %d\n",
595 priv->regs, priv->buswidth, clkid, plat->non_removable);
596#else
597 priv->regs = (struct mxs_ssp_regs *)plat->base;
598 priv->dma_channel = plat->dma_id;
599 clkid = plat->clk_id;
600#endif
601
602#ifdef CONFIG_MX28
603 priv->clkid = clkid - MXS_SSP_IMX28_CLKID_SSP0;
604#else /* CONFIG_MX23 */
605 priv->clkid = clkid - MXS_SSP_IMX23_CLKID_SSP0;
606#endif
607 mmc = &plat->mmc;
608 mmc->cfg = &plat->cfg;
609 mmc->dev = dev;
610
611 priv->desc = mxs_dma_desc_alloc();
612 if (!priv->desc) {
613 printf("%s: Cannot allocate DMA descriptor\n", __func__);
614 return -ENOMEM;
615 }
616
617 ret = mxs_dma_init_channel(priv->dma_channel);
618 if (ret)
619 return ret;
620
621 plat->cfg.name = "MXS MMC";
622 plat->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
623
624 plat->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
625 MMC_MODE_HS_52MHz | MMC_MODE_HS;
626
627 /*
628 * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz
629 * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
630 * CLOCK_DIVIDE has to be an even value from 2 to 254, and
631 * CLOCK_RATE could be any integer from 0 to 255.
632 */
633 plat->cfg.f_min = 400000;
634 plat->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + priv->clkid) * 1000 / 2;
635 plat->cfg.b_max = 0x20;
636
637 bdesc = mmc_get_blk_desc(mmc);
638 if (!bdesc) {
639 printf("%s: No block device descriptor!\n", __func__);
640 return -ENODEV;
641 }
642
643 if (plat->non_removable)
644 bdesc->removable = 0;
645
646 ret = mxsmmc_init(dev);
647 if (ret)
648 printf("%s: MMC%d init error %d\n", __func__,
649 bdesc->devnum, ret);
650
651 /* Set the initial clock speed */
652 mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE);
653
654 upriv->mmc = mmc;
655
656 return 0;
657};
658
659#if CONFIG_IS_ENABLED(BLK)
660static int mxsmmc_bind(struct udevice *dev)
661{
662 struct mxsmmc_platdata *plat = dev_get_platdata(dev);
663
664 return mmc_bind(dev, &plat->mmc, &plat->cfg);
665}
666#endif
667
668static const struct dm_mmc_ops mxsmmc_ops = {
669 .get_cd = mxsmmc_get_cd,
670 .send_cmd = mxsmmc_send_cmd,
671 .set_ios = mxsmmc_set_ios,
672};
673
674#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
675static int mxsmmc_ofdata_to_platdata(struct udevice *bus)
676{
677 struct mxsmmc_platdata *plat = bus->platdata;
678 u32 prop[2];
679 int ret;
680
681 plat->base = dev_read_addr(bus);
682 plat->buswidth =
683 dev_read_u32_default(bus, "bus-width", 1);
684 plat->non_removable = dev_read_bool(bus, "non-removable");
685
686 ret = dev_read_u32_array(bus, "dmas", prop, ARRAY_SIZE(prop));
687 if (ret) {
688 printf("%s: Reading 'dmas' property failed!\n", __func__);
689 return ret;
690 }
691 plat->dma_id = prop[1];
692
693 ret = dev_read_u32_array(bus, "clocks", prop, ARRAY_SIZE(prop));
694 if (ret) {
695 printf("%s: Reading 'clocks' property failed!\n", __func__);
696 return ret;
697 }
698 plat->clk_id = prop[1];
699
700 debug("%s: base=0x%x, bus_width=%d %s dma_id=%d clk_id=%d\n",
701 __func__, (uint)plat->base, plat->buswidth,
702 plat->non_removable ? "non-removable" : NULL,
703 plat->dma_id, plat->clk_id);
704
705 return 0;
706}
707
708static const struct udevice_id mxsmmc_ids[] = {
709 { .compatible = "fsl,imx23-mmc", },
710 { .compatible = "fsl,imx28-mmc", },
711 { /* sentinel */ }
712};
713#endif
714
Walter Lozanoe3e24702020-06-25 01:10:04 -0300715U_BOOT_DRIVER(fsl_imx23_mmc) = {
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200716 .name = "fsl_imx23_mmc",
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200717 .id = UCLASS_MMC,
718#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
719 .of_match = mxsmmc_ids,
720 .ofdata_to_platdata = mxsmmc_ofdata_to_platdata,
721#endif
722 .ops = &mxsmmc_ops,
723#if CONFIG_IS_ENABLED(BLK)
724 .bind = mxsmmc_bind,
725#endif
726 .probe = mxsmmc_probe,
727 .priv_auto_alloc_size = sizeof(struct mxsmmc_priv),
728 .platdata_auto_alloc_size = sizeof(struct mxsmmc_platdata),
729};
730
Walter Lozanoaddf3582020-06-25 01:10:06 -0300731U_BOOT_DRIVER_ALIAS(fsl_imx23_mmc, fsl_imx28_mmc)
Lukasz Majewski6116f4c2019-09-05 09:54:59 +0200732#endif /* CONFIG_DM_MMC */