blob: 17f1ebf5d2bdd223409d12dd2c00f099ce783933 [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>
Masahiro Yamada56a931c2016-09-21 11:28:55 +090027#include <linux/errno.h>
Marek Vasutde0a6bf2011-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 Babic33731bc2017-06-29 10:16:06 +020032#include <asm/mach-imx/dma.h>
Marek Vasutacc8d6b2012-08-26 15:19:07 +000033#include <bouncebuf.h>
Marek Vasutde0a6bf2011-11-08 23:18:09 +000034
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020035#define MXSMMC_MAX_TIMEOUT 10000
36#define MXSMMC_SMALL_TRANSFER 512
37
38#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasutde0a6bf2011-11-08 23:18:09 +000039struct mxsmmc_priv {
40 int id;
Marek Vasutde0a6bf2011-11-08 23:18:09 +000041 int (*mmc_is_wp)(int);
Marek Vasut722181e2013-01-22 15:01:03 +000042 int (*mmc_cd)(int);
Pantelis Antoniou2c850462014-03-11 19:34:20 +020043 struct mmc_config cfg; /* mmc configuration */
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020044 struct mxs_dma_desc *desc;
45 uint32_t buswidth;
46 struct mxs_ssp_regs *regs;
Marek Vasutde0a6bf2011-11-08 23:18:09 +000047};
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020048#else /* CONFIG_IS_ENABLED(DM_MMC) */
49#include <dm/device.h>
50#include <dm/read.h>
51#include <dt-structs.h>
Marek Vasutde0a6bf2011-11-08 23:18:09 +000052
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020053#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);
Marek Vasutde0a6bf2011-11-08 23:18:09 +000084
Marek Vasut722181e2013-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
Lukasz Majewskiaa92b862019-09-05 09:54:59 +020095static int mxsmmc_set_ios(struct mmc *mmc)
96{
97 struct mxsmmc_priv *priv = mmc->priv;
98 struct mxs_ssp_regs *ssp_regs = priv->regs;
99
100 /* Set the clock speed */
101 if (mmc->clock)
102 mxs_set_ssp_busclock(priv->id, mmc->clock / 1000);
103
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",
121 mmc->block_dev.devnum, mmc->bus_width);
122
123 return 0;
124}
125
126static int mxsmmc_init(struct mmc *mmc)
127{
128 struct mxsmmc_priv *priv = mmc->priv;
129 struct mxs_ssp_regs *ssp_regs = priv->regs;
130
131 /* Reset SSP */
132 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
133
134 /* 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);
145
146 /* Set initial bit clock 400 KHz */
147 mxs_set_ssp_busclock(priv->id, 400);
148
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
157static const struct mmc_ops mxsmmc_ops = {
158 .send_cmd = mxsmmc_send_cmd,
159 .set_ios = mxsmmc_set_ios,
160 .init = mxsmmc_init,
161};
162
163int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int), int (*cd)(int))
164{
165 struct mmc *mmc = NULL;
166 struct mxsmmc_priv *priv = NULL;
167 int ret;
168 const unsigned int mxsmmc_clk_id = mxs_ssp_clock_by_bus(id);
169
170 if (!mxs_ssp_bus_id_valid(id))
171 return -ENODEV;
172
173 priv = malloc(sizeof(struct mxsmmc_priv));
174 if (!priv)
175 return -ENOMEM;
176
177 priv->desc = mxs_dma_desc_alloc();
178 if (!priv->desc) {
179 free(priv);
180 return -ENOMEM;
181 }
182
183 ret = mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + id);
184 if (ret)
185 return ret;
186
187 priv->mmc_is_wp = wp;
188 priv->mmc_cd = cd;
189 priv->id = id;
190 priv->regs = mxs_ssp_regs_by_bus(id);
191
192 priv->cfg.name = "MXS MMC";
193 priv->cfg.ops = &mxsmmc_ops;
194
195 priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
196
197 priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
198 MMC_MODE_HS_52MHz | MMC_MODE_HS;
199
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 */
206 priv->cfg.f_min = 400000;
207 priv->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id)
208 * 1000 / 2;
209 priv->cfg.b_max = 0x20;
210
211 mmc = mmc_create(&priv->cfg, priv);
212 if (!mmc) {
213 mxs_dma_desc_free(priv->desc);
214 free(priv);
215 return -ENOMEM;
216 }
217 return 0;
218}
219#endif /* CONFIG_IS_ENABLED(DM_MMC) */
220
Marek Vasut92af6152012-07-06 21:25:55 +0000221static 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
Jaehoon Chung7825d202016-07-19 16:33:36 +0900254 return timeout ? 0 : -ECOMM;
Marek Vasut92af6152012-07-06 21:25:55 +0000255}
Marek Vasut97c04d62012-07-06 21:25:56 +0000256
Marek Vasut92af6152012-07-06 21:25:55 +0000257static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data)
258{
259 uint32_t data_count = data->blocksize * data->blocks;
Marek Vasut92af6152012-07-06 21:25:55 +0000260 int dmach;
Marek Vasut042cfd62012-07-06 21:25:57 +0000261 struct mxs_dma_desc *desc = priv->desc;
Stephen Warren4a8629e2012-11-06 11:27:29 +0000262 void *addr;
263 unsigned int flags;
264 struct bounce_buffer bbstate;
Marek Vasut042cfd62012-07-06 21:25:57 +0000265
266 memset(desc, 0, sizeof(struct mxs_dma_desc));
267 desc->address = (dma_addr_t)desc;
Marek Vasut92af6152012-07-06 21:25:55 +0000268
Marek Vasut92af6152012-07-06 21:25:55 +0000269 if (data->flags & MMC_DATA_READ) {
270 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000271 addr = data->dest;
272 flags = GEN_BB_WRITE;
Marek Vasut92af6152012-07-06 21:25:55 +0000273 } else {
274 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000275 addr = (void *)data->src;
276 flags = GEN_BB_READ;
277 }
278
Stephen Warren4a8629e2012-11-06 11:27:29 +0000279 bounce_buffer_start(&bbstate, addr, data_count, flags);
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000280
Stephen Warren4a8629e2012-11-06 11:27:29 +0000281 priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer;
Marek Vasut761fb5b2012-08-31 16:18:10 +0000282
Marek Vasut92af6152012-07-06 21:25:55 +0000283 priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM |
284 (data_count << MXS_DMA_DESC_BYTES_OFFSET);
285
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200286#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasuteadf3372013-02-23 02:42:58 +0000287 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200288#else
289 dmach = priv->dma_channel;
290#endif
Marek Vasut92af6152012-07-06 21:25:55 +0000291 mxs_dma_desc_append(dmach, priv->desc);
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000292 if (mxs_dma_go(dmach)) {
Stephen Warren4a8629e2012-11-06 11:27:29 +0000293 bounce_buffer_stop(&bbstate);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900294 return -ECOMM;
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000295 }
Marek Vasut92af6152012-07-06 21:25:55 +0000296
Stephen Warren4a8629e2012-11-06 11:27:29 +0000297 bounce_buffer_stop(&bbstate);
Marek Vasutacc8d6b2012-08-26 15:19:07 +0000298
Marek Vasut92af6152012-07-06 21:25:55 +0000299 return 0;
300}
Marek Vasut92af6152012-07-06 21:25:55 +0000301
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200302#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000303/*
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{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200310 struct mxsmmc_priv *priv = mmc->priv;
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000311 struct mxs_ssp_regs *ssp_regs = priv->regs;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200312#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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000321 uint32_t reg;
322 int timeout;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000323 uint32_t ctrl0;
Marek Vasut92af6152012-07-06 21:25:55 +0000324 int ret;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200325#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);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000331
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) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200345 printf("MMC%d: Bus busy timeout!\n", devnum);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900346 return -ETIMEDOUT;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000347 }
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200348#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000349 /* See if card is present */
Marek Vasut722181e2013-01-22 15:01:03 +0000350 if (!mxsmmc_cd(priv)) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200351 printf("MMC%d: No card detected!\n", devnum);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900352 return -ENOMEDIUM;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000353 }
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200354#endif
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000355 /* 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
Marek Vasut042cfd62012-07-06 21:25:57 +0000366 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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000371 /* 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;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200387#if !CONFIG_IS_ENABLED(DM_MMC)
Marek Vasut336ff282012-05-01 11:09:52 +0000388 } else if (priv->mmc_is_wp &&
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200389 priv->mmc_is_wp(devnum)) {
390 printf("MMC%d: Can not write a locked card!\n", devnum);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900391 return -EOPNOTSUPP;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200392#endif
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000393 }
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000394 ctrl0 |= SSP_CTRL0_DATA_XFER;
Marek Vasutecf843e2013-01-22 15:01:04 +0000395
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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000408 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);
Marek Vasutecf843e2013-01-22 15:01:04 +0000413#endif
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000414 }
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) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200430 printf("MMC%d: Command %d busy\n", devnum, cmd->cmdidx);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900431 return -ETIMEDOUT;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000432 }
433
434 /* Check command timeout */
435 if (reg & SSP_STATUS_RESP_TIMEOUT) {
Lukasz Majewski058c1352019-09-05 09:55:00 +0200436 debug("MMC%d: Command %d timeout (status 0x%08x)\n",
437 devnum, cmd->cmdidx, reg);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900438 return -ETIMEDOUT;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000439 }
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",
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200444 devnum, cmd->cmdidx, reg);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900445 return -ECOMM;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000446 }
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
Marek Vasut97c04d62012-07-06 21:25:56 +0000461 if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) {
Marek Vasut97c04d62012-07-06 21:25:56 +0000462 ret = mxsmmc_send_cmd_pio(priv, data);
463 if (ret) {
464 printf("MMC%d: Data timeout with command %d "
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200465 "(status 0x%08x)!\n", devnum, cmd->cmdidx, reg);
Marek Vasut97c04d62012-07-06 21:25:56 +0000466 return ret;
467 }
Marek Vasut042cfd62012-07-06 21:25:57 +0000468 } else {
469 ret = mxsmmc_send_cmd_dma(priv, data);
470 if (ret) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200471 printf("MMC%d: DMA transfer failed\n", devnum);
Marek Vasut042cfd62012-07-06 21:25:57 +0000472 return ret;
473 }
Marek Vasutdac199b2012-04-05 03:30:35 +0000474 }
Marek Vasut5796bed2012-03-15 18:33:21 +0000475
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000476 /* 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",
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200482 devnum, cmd->cmdidx, reg);
Jaehoon Chung7825d202016-07-19 16:33:36 +0900483 return -ECOMM;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000484 }
485
486 return 0;
487}
488
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200489#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)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000495{
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200496 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);
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000510 struct mxs_ssp_regs *ssp_regs = priv->regs;
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200511 struct mmc *mmc = &plat->mmc;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000512
513 /* Set the clock speed */
514 if (mmc->clock)
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200515 mxs_set_ssp_busclock(priv->clkid, mmc->clock / 1000);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000516
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
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200533 debug("MMC%d: Set %d bits bus width\n", mmc_get_blk_desc(mmc)->devnum,
534 mmc->bus_width);
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900535
536 return 0;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000537}
538
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200539static int mxsmmc_init(struct udevice *dev)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000540{
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200541 struct mxsmmc_priv *priv = dev_get_priv(dev);
Otavio Salvador22f4ff92012-08-05 09:05:31 +0000542 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000543
544 /* Reset SSP */
Otavio Salvadorcbf0bf22012-08-13 09:53:12 +0000545 mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000546
Otavio Salvador254f2972013-01-22 15:01:02 +0000547 /* 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);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000558
559 /* Set initial bit clock 400 KHz */
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200560 mxs_set_ssp_busclock(priv->clkid, 400);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000561
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
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200570static int mxsmmc_probe(struct udevice *dev)
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000571{
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200572 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;
Marek Vasut3b46aa22013-01-11 03:19:14 +0000578
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200579 debug("%s: probe\n", __func__);
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000580
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200581#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;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000607
Marek Vasut5796bed2012-03-15 18:33:21 +0000608 priv->desc = mxs_dma_desc_alloc();
609 if (!priv->desc) {
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200610 printf("%s: Cannot allocate DMA descriptor\n", __func__);
Marek Vasut5796bed2012-03-15 18:33:21 +0000611 return -ENOMEM;
612 }
613
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200614 ret = mxs_dma_init_channel(priv->dma_channel);
Marek Vasut93541b42012-04-08 17:34:46 +0000615 if (ret)
616 return ret;
617
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200618 plat->cfg.name = "MXS MMC";
619 plat->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000620
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200621 plat->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
622 MMC_MODE_HS_52MHz | MMC_MODE_HS;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000623
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 */
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200630 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;
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000633
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200634 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;
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200694 }
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200695 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
Marek Vasutde0a6bf2011-11-08 23:18:09 +0000702 return 0;
703}
Lukasz Majewskiaa92b862019-09-05 09:54:59 +0200704
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 */