blob: e3c352b44a85e49f6e3ab8e43b52c9a2f24668d1 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Marek Vasutde0a6bf2011-11-08 23:18:09 +00002/*
3 * Freescale i.MX28 SSP MMC driver
4 *
Lukasz Majewskiaa92b862019-09-05 09:54:59 +02005 * Copyright (C) 2019 DENX Software Engineering
6 * Lukasz Majewski, DENX Software Engineering, lukma@denx.de
7 *
Marek Vasutde0a6bf2011-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 Vasutde0a6bf2011-11-08 23:18:09 +000021 */
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020022
Marek Vasutde0a6bf2011-11-08 23:18:09 +000023#include <common.h>
Simon Glass0f2af882020-05-10 11:40:05 -060024#include <log.h>
Marek Vasutde0a6bf2011-11-08 23:18:09 +000025#include <malloc.h>
26#include <mmc.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060027#include <linux/bitops.h>
Simon Glassdbd79542020-05-10 11:40:11 -060028#include <linux/delay.h>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090029#include <linux/errno.h>
Marek Vasutde0a6bf2011-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 Babic33731bc2017-06-29 10:16:06 +020034#include <asm/mach-imx/dma.h>
Marek Vasutacc8d6b2012-08-26 15:19:07 +000035#include <bouncebuf.h>
Marek Vasutde0a6bf2011-11-08 23:18:09 +000036
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020037#define MXSMMC_MAX_TIMEOUT 10000
38#define MXSMMC_SMALL_TRANSFER 512
39
40#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasutde0a6bf2011-11-08 23:18:09 +000041struct mxsmmc_priv {
42 int id;
Marek Vasutde0a6bf2011-11-08 23:18:09 +000043 int (*mmc_is_wp)(int);
Marek Vasut722181e2013-01-22 15:01:03 +000044 int (*mmc_cd)(int);
Pantelis Antoniou2c850462014-03-11 19:34:20 +020045 struct mmc_config cfg; /* mmc configuration */
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020046 struct mxs_dma_desc *desc;
47 uint32_t buswidth;
48 struct mxs_ssp_regs *regs;
Marek Vasutde0a6bf2011-11-08 23:18:09 +000049};
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020050#else /* CONFIG_IS_ENABLED(DM_MMC) */
51#include <dm/device.h>
52#include <dm/read.h>
53#include <dt-structs.h>
Marek Vasutde0a6bf2011-11-08 23:18:09 +000054
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020055#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);
Marek Vasutde0a6bf2011-11-08 23:18:09 +000086
Marek Vasut722181e2013-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
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020097static int mxsmmc_set_ios(struct mmc *mmc)
98{
99 struct mxsmmc_priv *priv = mmc->priv;
100 struct mxs_ssp_regs *ssp_regs = priv->regs;
101
102 /* Set the clock speed */
103 if (mmc->clock)
104 mxs_set_ssp_busclock(priv->id, mmc->clock / 1000);
105
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",
123 mmc->block_dev.devnum, mmc->bus_width);
124
125 return 0;
126}
127
128static int mxsmmc_init(struct mmc *mmc)
129{
130 struct mxsmmc_priv *priv = mmc->priv;
131 struct mxs_ssp_regs *ssp_regs = priv->regs;
132
133 /* Reset SSP */
134 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
135
136 /* 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);
147
148 /* Set initial bit clock 400 KHz */
149 mxs_set_ssp_busclock(priv->id, 400);
150
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
159static const struct mmc_ops mxsmmc_ops = {
160 .send_cmd = mxsmmc_send_cmd,
161 .set_ios = mxsmmc_set_ios,
162 .init = mxsmmc_init,
163};
164
165int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int), int (*cd)(int))
166{
167 struct mmc *mmc = NULL;
168 struct mxsmmc_priv *priv = NULL;
169 int ret;
170 const unsigned int mxsmmc_clk_id = mxs_ssp_clock_by_bus(id);
171
172 if (!mxs_ssp_bus_id_valid(id))
173 return -ENODEV;
174
175 priv = malloc(sizeof(struct mxsmmc_priv));
176 if (!priv)
177 return -ENOMEM;
178
179 priv->desc = mxs_dma_desc_alloc();
180 if (!priv->desc) {
181 free(priv);
182 return -ENOMEM;
183 }
184
185 ret = mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + id);
186 if (ret)
187 return ret;
188
189 priv->mmc_is_wp = wp;
190 priv->mmc_cd = cd;
191 priv->id = id;
192 priv->regs = mxs_ssp_regs_by_bus(id);
193
194 priv->cfg.name = "MXS MMC";
195 priv->cfg.ops = &mxsmmc_ops;
196
197 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
198
199 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
200 MMC_MODE_HS_52MHz | MMC_MODE_HS;
201
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 */
208 priv->cfg.f_min = 400000;
209 priv->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id)
210 * 1000 / 2;
211 priv->cfg.b_max = 0x20;
212
213 mmc = mmc_create(&priv->cfg, priv);
214 if (!mmc) {
215 mxs_dma_desc_free(priv->desc);
216 free(priv);
217 return -ENOMEM;
218 }
219 return 0;
220}
221#endif /* CONFIG_IS_ENABLED(DM_MMC) */
222
Marek Vasut92af6152012-07-06 21:25:55 +0000223static 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
Jaehoon Chung7825d202016-07-19 16:33:36 +0900256 return timeout ? 0 : -ECOMM;
Marek Vasut92af6152012-07-06 21:25:55 +0000257}
Marek Vasut97c04d62012-07-06 21:25:56 +0000258
Marek Vasut92af6152012-07-06 21:25:55 +0000259static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data)
260{
261 uint32_t data_count = data->blocksize * data->blocks;
Marek Vasut92af6152012-07-06 21:25:55 +0000262 int dmach;
Marek Vasut042cfd62012-07-06 21:25:57 +0000263 struct mxs_dma_desc *desc = priv->desc;
Stephen Warren4a8629e2012-11-06 11:27:29 +0000264 void *addr;
265 unsigned int flags;
266 struct bounce_buffer bbstate;
Marek Vasut042cfd62012-07-06 21:25:57 +0000267
268 memset(desc, 0, sizeof(struct mxs_dma_desc));
269 desc->address = (dma_addr_t)desc;
Marek Vasut92af6152012-07-06 21:25:55 +0000270
Marek Vasut92af6152012-07-06 21:25:55 +0000271 if (data->flags & MMC_DATA_READ) {
272 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000273 addr = data->dest;
274 flags = GEN_BB_WRITE;
Marek Vasut92af6152012-07-06 21:25:55 +0000275 } else {
276 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000277 addr = (void *)data->src;
278 flags = GEN_BB_READ;
279 }
280
Stephen Warren4a8629e2012-11-06 11:27:29 +0000281 bounce_buffer_start(&bbstate, addr, data_count, flags);
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000282
Stephen Warren4a8629e2012-11-06 11:27:29 +0000283 priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer;
Marek Vasut761fb5b2012-08-31 16:18:10 +0000284
Marek Vasut92af6152012-07-06 21:25:55 +0000285 priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM |
286 (data_count << MXS_DMA_DESC_BYTES_OFFSET);
287
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200288#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasuteadf3372013-02-23 02:42:58 +0000289 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200290#else
291 dmach = priv->dma_channel;
292#endif
Marek Vasut92af6152012-07-06 21:25:55 +0000293 mxs_dma_desc_append(dmach, priv->desc);
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000294 if (mxs_dma_go(dmach)) {
Stephen Warren4a8629e2012-11-06 11:27:29 +0000295 bounce_buffer_stop(&bbstate);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900296 return -ECOMM;
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000297 }
Marek Vasut92af6152012-07-06 21:25:55 +0000298
Stephen Warren4a8629e2012-11-06 11:27:29 +0000299 bounce_buffer_stop(&bbstate);
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000300
Marek Vasut92af6152012-07-06 21:25:55 +0000301 return 0;
302}
Marek Vasut92af6152012-07-06 21:25:55 +0000303
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200304#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000305/*
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{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200312 struct mxsmmc_priv *priv = mmc->priv;
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000313 struct mxs_ssp_regs *ssp_regs = priv->regs;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200314#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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000323 uint32_t reg;
324 int timeout;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000325 uint32_t ctrl0;
Marek Vasut92af6152012-07-06 21:25:55 +0000326 int ret;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200327#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);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000333
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) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200347 printf("MMC%d: Bus busy timeout!\n", devnum);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900348 return -ETIMEDOUT;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000349 }
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200350#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000351 /* See if card is present */
Marek Vasut722181e2013-01-22 15:01:03 +0000352 if (!mxsmmc_cd(priv)) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200353 printf("MMC%d: No card detected!\n", devnum);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900354 return -ENOMEDIUM;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000355 }
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200356#endif
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000357 /* 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
Marek Vasut042cfd62012-07-06 21:25:57 +0000368 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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000373 /* 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;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200389#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasut336ff282012-05-01 11:09:52 +0000390 } else if (priv->mmc_is_wp &&
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200391 priv->mmc_is_wp(devnum)) {
392 printf("MMC%d: Can not write a locked card!\n", devnum);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900393 return -EOPNOTSUPP;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200394#endif
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000395 }
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000396 ctrl0 |= SSP_CTRL0_DATA_XFER;
Marek Vasutecf843e2013-01-22 15:01:04 +0000397
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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000410 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);
Marek Vasutecf843e2013-01-22 15:01:04 +0000415#endif
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000416 }
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) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200432 printf("MMC%d: Command %d busy\n", devnum, cmd->cmdidx);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900433 return -ETIMEDOUT;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000434 }
435
436 /* Check command timeout */
437 if (reg & SSP_STATUS_RESP_TIMEOUT) {
Lukasz Majewski058c1352019-09-05 09:55:00 +0200438 debug("MMC%d: Command %d timeout (status 0x%08x)\n",
439 devnum, cmd->cmdidx, reg);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900440 return -ETIMEDOUT;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000441 }
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",
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200446 devnum, cmd->cmdidx, reg);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900447 return -ECOMM;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000448 }
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
Marek Vasut97c04d62012-07-06 21:25:56 +0000463 if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) {
Marek Vasut97c04d62012-07-06 21:25:56 +0000464 ret = mxsmmc_send_cmd_pio(priv, data);
465 if (ret) {
466 printf("MMC%d: Data timeout with command %d "
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200467 "(status 0x%08x)!\n", devnum, cmd->cmdidx, reg);
Marek Vasut97c04d62012-07-06 21:25:56 +0000468 return ret;
469 }
Marek Vasut042cfd62012-07-06 21:25:57 +0000470 } else {
471 ret = mxsmmc_send_cmd_dma(priv, data);
472 if (ret) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200473 printf("MMC%d: DMA transfer failed\n", devnum);
Marek Vasut042cfd62012-07-06 21:25:57 +0000474 return ret;
475 }
Marek Vasutdac199b2012-04-05 03:30:35 +0000476 }
Marek Vasut5796bed2012-03-15 18:33:21 +0000477
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000478 /* 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",
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200484 devnum, cmd->cmdidx, reg);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900485 return -ECOMM;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000486 }
487
488 return 0;
489}
490
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200491#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)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000497{
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200498 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);
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000512 struct mxs_ssp_regs *ssp_regs = priv->regs;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200513 struct mmc *mmc = &plat->mmc;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000514
515 /* Set the clock speed */
516 if (mmc->clock)
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200517 mxs_set_ssp_busclock(priv->clkid, mmc->clock / 1000);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000518
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
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200535 debug("MMC%d: Set %d bits bus width\n", mmc_get_blk_desc(mmc)->devnum,
536 mmc->bus_width);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900537
538 return 0;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000539}
540
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200541static int mxsmmc_init(struct udevice *dev)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000542{
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200543 struct mxsmmc_priv *priv = dev_get_priv(dev);
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000544 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000545
546 /* Reset SSP */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +0000547 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000548
Otavio Salvador254f2972013-01-22 15:01:02 +0000549 /* 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);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000560
561 /* Set initial bit clock 400 KHz */
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200562 mxs_set_ssp_busclock(priv->clkid, 400);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000563
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
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200572static int mxsmmc_probe(struct udevice *dev)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000573{
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200574 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;
Marek Vasut3b46aa22013-01-11 03:19:14 +0000580
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200581 debug("%s: probe\n", __func__);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000582
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200583#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;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000609
Marek Vasut5796bed2012-03-15 18:33:21 +0000610 priv->desc = mxs_dma_desc_alloc();
611 if (!priv->desc) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200612 printf("%s: Cannot allocate DMA descriptor\n", __func__);
Marek Vasut5796bed2012-03-15 18:33:21 +0000613 return -ENOMEM;
614 }
615
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200616 ret = mxs_dma_init_channel(priv->dma_channel);
Marek Vasut93541b42012-04-08 17:34:46 +0000617 if (ret)
618 return ret;
619
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200620 plat->cfg.name = "MXS MMC";
621 plat->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000622
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200623 plat->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
624 MMC_MODE_HS_52MHz | MMC_MODE_HS;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000625
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 */
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200632 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;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000635
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200636 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;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200696 }
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200697 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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000704 return 0;
705}
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200706
707static const struct udevice_id mxsmmc_ids[] = {
708 { .compatible = "fsl,imx23-mmc", },
709 { .compatible = "fsl,imx28-mmc", },
710 { /* sentinel */ }
711};
712#endif
713
714U_BOOT_DRIVER(mxsmmc) = {
715#ifdef CONFIG_MX28
716 .name = "fsl_imx28_mmc",
717#else /* CONFIG_MX23 */
718 .name = "fsl_imx23_mmc",
719#endif
720 .id = UCLASS_MMC,
721#if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA)
722 .of_match = mxsmmc_ids,
723 .ofdata_to_platdata = mxsmmc_ofdata_to_platdata,
724#endif
725 .ops = &mxsmmc_ops,
726#if CONFIG_IS_ENABLED(BLK)
727 .bind = mxsmmc_bind,
728#endif
729 .probe = mxsmmc_probe,
730 .priv_auto_alloc_size = sizeof(struct mxsmmc_priv),
731 .platdata_auto_alloc_size = sizeof(struct mxsmmc_platdata),
732};
733
734#endif /* CONFIG_DM_MMC */