]>
Commit | Line | Data |
---|---|---|
71a758e1 MV |
1 | /* |
2 | * Freescale i.MX28 SSP MMC driver | |
3 | * | |
4 | * Copyright (C) 2011 Marek Vasut <[email protected]> | |
5 | * on behalf of DENX Software Engineering GmbH | |
6 | * | |
7 | * Based on code from LTIB: | |
8 | * (C) Copyright 2008-2010 Freescale Semiconductor, Inc. | |
9 | * Terry Lv | |
10 | * | |
11 | * Copyright 2007, Freescale Semiconductor, Inc | |
12 | * Andy Fleming | |
13 | * | |
14 | * Based vaguely on the pxa mmc code: | |
15 | * (C) Copyright 2003 | |
16 | * Kyle Harris, Nexus Technologies, Inc. [email protected] | |
17 | * | |
1a459660 | 18 | * SPDX-License-Identifier: GPL-2.0+ |
71a758e1 MV |
19 | */ |
20 | #include <common.h> | |
21 | #include <malloc.h> | |
22 | #include <mmc.h> | |
1221ce45 | 23 | #include <linux/errno.h> |
71a758e1 MV |
24 | #include <asm/io.h> |
25 | #include <asm/arch/clock.h> | |
26 | #include <asm/arch/imx-regs.h> | |
27 | #include <asm/arch/sys_proto.h> | |
552a848e | 28 | #include <asm/mach-imx/dma.h> |
4e6d81d1 | 29 | #include <bouncebuf.h> |
71a758e1 MV |
30 | |
31 | struct mxsmmc_priv { | |
32 | int id; | |
9c471142 | 33 | struct mxs_ssp_regs *regs; |
71a758e1 MV |
34 | uint32_t buswidth; |
35 | int (*mmc_is_wp)(int); | |
90bc2bf2 | 36 | int (*mmc_cd)(int); |
3687c415 | 37 | struct mxs_dma_desc *desc; |
93bfd616 | 38 | struct mmc_config cfg; /* mmc configuration */ |
71a758e1 MV |
39 | }; |
40 | ||
41 | #define MXSMMC_MAX_TIMEOUT 10000 | |
20255900 | 42 | #define MXSMMC_SMALL_TRANSFER 512 |
71a758e1 | 43 | |
90bc2bf2 MV |
44 | static int mxsmmc_cd(struct mxsmmc_priv *priv) |
45 | { | |
46 | struct mxs_ssp_regs *ssp_regs = priv->regs; | |
47 | ||
48 | if (priv->mmc_cd) | |
49 | return priv->mmc_cd(priv->id); | |
50 | ||
51 | return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT); | |
52 | } | |
53 | ||
86983328 MV |
54 | static int mxsmmc_send_cmd_pio(struct mxsmmc_priv *priv, struct mmc_data *data) |
55 | { | |
56 | struct mxs_ssp_regs *ssp_regs = priv->regs; | |
57 | uint32_t *data_ptr; | |
58 | int timeout = MXSMMC_MAX_TIMEOUT; | |
59 | uint32_t reg; | |
60 | uint32_t data_count = data->blocksize * data->blocks; | |
61 | ||
62 | if (data->flags & MMC_DATA_READ) { | |
63 | data_ptr = (uint32_t *)data->dest; | |
64 | while (data_count && --timeout) { | |
65 | reg = readl(&ssp_regs->hw_ssp_status); | |
66 | if (!(reg & SSP_STATUS_FIFO_EMPTY)) { | |
67 | *data_ptr++ = readl(&ssp_regs->hw_ssp_data); | |
68 | data_count -= 4; | |
69 | timeout = MXSMMC_MAX_TIMEOUT; | |
70 | } else | |
71 | udelay(1000); | |
72 | } | |
73 | } else { | |
74 | data_ptr = (uint32_t *)data->src; | |
75 | timeout *= 100; | |
76 | while (data_count && --timeout) { | |
77 | reg = readl(&ssp_regs->hw_ssp_status); | |
78 | if (!(reg & SSP_STATUS_FIFO_FULL)) { | |
79 | writel(*data_ptr++, &ssp_regs->hw_ssp_data); | |
80 | data_count -= 4; | |
81 | timeout = MXSMMC_MAX_TIMEOUT; | |
82 | } else | |
83 | udelay(1000); | |
84 | } | |
85 | } | |
86 | ||
915ffa52 | 87 | return timeout ? 0 : -ECOMM; |
86983328 | 88 | } |
20255900 | 89 | |
86983328 MV |
90 | static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data) |
91 | { | |
92 | uint32_t data_count = data->blocksize * data->blocks; | |
86983328 | 93 | int dmach; |
abb85be7 | 94 | struct mxs_dma_desc *desc = priv->desc; |
84d35b28 SW |
95 | void *addr; |
96 | unsigned int flags; | |
97 | struct bounce_buffer bbstate; | |
abb85be7 MV |
98 | |
99 | memset(desc, 0, sizeof(struct mxs_dma_desc)); | |
100 | desc->address = (dma_addr_t)desc; | |
86983328 | 101 | |
86983328 MV |
102 | if (data->flags & MMC_DATA_READ) { |
103 | priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE; | |
4e6d81d1 MV |
104 | addr = data->dest; |
105 | flags = GEN_BB_WRITE; | |
86983328 MV |
106 | } else { |
107 | priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ; | |
4e6d81d1 MV |
108 | addr = (void *)data->src; |
109 | flags = GEN_BB_READ; | |
110 | } | |
111 | ||
84d35b28 | 112 | bounce_buffer_start(&bbstate, addr, data_count, flags); |
4e6d81d1 | 113 | |
84d35b28 | 114 | priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer; |
97ed12ce | 115 | |
86983328 MV |
116 | priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM | |
117 | (data_count << MXS_DMA_DESC_BYTES_OFFSET); | |
118 | ||
3430e0bd | 119 | dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id; |
86983328 | 120 | mxs_dma_desc_append(dmach, priv->desc); |
4e6d81d1 | 121 | if (mxs_dma_go(dmach)) { |
84d35b28 | 122 | bounce_buffer_stop(&bbstate); |
915ffa52 | 123 | return -ECOMM; |
4e6d81d1 | 124 | } |
86983328 | 125 | |
84d35b28 | 126 | bounce_buffer_stop(&bbstate); |
4e6d81d1 | 127 | |
86983328 MV |
128 | return 0; |
129 | } | |
86983328 | 130 | |
71a758e1 MV |
131 | /* |
132 | * Sends a command out on the bus. Takes the mmc pointer, | |
133 | * a command pointer, and an optional data pointer. | |
134 | */ | |
135 | static int | |
136 | mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) | |
137 | { | |
93bfd616 | 138 | struct mxsmmc_priv *priv = mmc->priv; |
9c471142 | 139 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
71a758e1 MV |
140 | uint32_t reg; |
141 | int timeout; | |
71a758e1 | 142 | uint32_t ctrl0; |
86983328 | 143 | int ret; |
71a758e1 | 144 | |
bcce53d0 | 145 | debug("MMC%d: CMD%d\n", mmc->block_dev.devnum, cmd->cmdidx); |
71a758e1 MV |
146 | |
147 | /* Check bus busy */ | |
148 | timeout = MXSMMC_MAX_TIMEOUT; | |
149 | while (--timeout) { | |
150 | udelay(1000); | |
151 | reg = readl(&ssp_regs->hw_ssp_status); | |
152 | if (!(reg & | |
153 | (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY | | |
154 | SSP_STATUS_CMD_BUSY))) { | |
155 | break; | |
156 | } | |
157 | } | |
158 | ||
159 | if (!timeout) { | |
bcce53d0 | 160 | printf("MMC%d: Bus busy timeout!\n", mmc->block_dev.devnum); |
915ffa52 | 161 | return -ETIMEDOUT; |
71a758e1 MV |
162 | } |
163 | ||
164 | /* See if card is present */ | |
90bc2bf2 | 165 | if (!mxsmmc_cd(priv)) { |
bcce53d0 | 166 | printf("MMC%d: No card detected!\n", mmc->block_dev.devnum); |
915ffa52 | 167 | return -ENOMEDIUM; |
71a758e1 MV |
168 | } |
169 | ||
170 | /* Start building CTRL0 contents */ | |
171 | ctrl0 = priv->buswidth; | |
172 | ||
173 | /* Set up command */ | |
174 | if (!(cmd->resp_type & MMC_RSP_CRC)) | |
175 | ctrl0 |= SSP_CTRL0_IGNORE_CRC; | |
176 | if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */ | |
177 | ctrl0 |= SSP_CTRL0_GET_RESP; | |
178 | if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */ | |
179 | ctrl0 |= SSP_CTRL0_LONG_RESP; | |
180 | ||
abb85be7 MV |
181 | if (data && (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER)) |
182 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr); | |
183 | else | |
184 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set); | |
185 | ||
71a758e1 MV |
186 | /* Command index */ |
187 | reg = readl(&ssp_regs->hw_ssp_cmd0); | |
188 | reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC); | |
189 | reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET; | |
190 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) | |
191 | reg |= SSP_CMD0_APPEND_8CYC; | |
192 | writel(reg, &ssp_regs->hw_ssp_cmd0); | |
193 | ||
194 | /* Command argument */ | |
195 | writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1); | |
196 | ||
197 | /* Set up data */ | |
198 | if (data) { | |
199 | /* READ or WRITE */ | |
200 | if (data->flags & MMC_DATA_READ) { | |
201 | ctrl0 |= SSP_CTRL0_READ; | |
c7527b70 | 202 | } else if (priv->mmc_is_wp && |
bcce53d0 | 203 | priv->mmc_is_wp(mmc->block_dev.devnum)) { |
71a758e1 | 204 | printf("MMC%d: Can not write a locked card!\n", |
bcce53d0 | 205 | mmc->block_dev.devnum); |
915ffa52 | 206 | return -EOPNOTSUPP; |
71a758e1 MV |
207 | } |
208 | ||
209 | ctrl0 |= SSP_CTRL0_DATA_XFER; | |
e5b380ac MV |
210 | |
211 | reg = data->blocksize * data->blocks; | |
212 | #if defined(CONFIG_MX23) | |
213 | ctrl0 |= reg & SSP_CTRL0_XFER_COUNT_MASK; | |
214 | ||
215 | clrsetbits_le32(&ssp_regs->hw_ssp_cmd0, | |
216 | SSP_CMD0_BLOCK_SIZE_MASK | SSP_CMD0_BLOCK_COUNT_MASK, | |
217 | ((data->blocks - 1) << SSP_CMD0_BLOCK_COUNT_OFFSET) | | |
218 | ((ffs(data->blocksize) - 1) << | |
219 | SSP_CMD0_BLOCK_SIZE_OFFSET)); | |
220 | #elif defined(CONFIG_MX28) | |
221 | writel(reg, &ssp_regs->hw_ssp_xfer_size); | |
222 | ||
71a758e1 MV |
223 | reg = ((data->blocks - 1) << |
224 | SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) | | |
225 | ((ffs(data->blocksize) - 1) << | |
226 | SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET); | |
227 | writel(reg, &ssp_regs->hw_ssp_block_size); | |
e5b380ac | 228 | #endif |
71a758e1 MV |
229 | } |
230 | ||
231 | /* Kick off the command */ | |
232 | ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN; | |
233 | writel(ctrl0, &ssp_regs->hw_ssp_ctrl0); | |
234 | ||
235 | /* Wait for the command to complete */ | |
236 | timeout = MXSMMC_MAX_TIMEOUT; | |
237 | while (--timeout) { | |
238 | udelay(1000); | |
239 | reg = readl(&ssp_regs->hw_ssp_status); | |
240 | if (!(reg & SSP_STATUS_CMD_BUSY)) | |
241 | break; | |
242 | } | |
243 | ||
244 | if (!timeout) { | |
245 | printf("MMC%d: Command %d busy\n", | |
bcce53d0 | 246 | mmc->block_dev.devnum, cmd->cmdidx); |
915ffa52 | 247 | return -ETIMEDOUT; |
71a758e1 MV |
248 | } |
249 | ||
250 | /* Check command timeout */ | |
251 | if (reg & SSP_STATUS_RESP_TIMEOUT) { | |
252 | printf("MMC%d: Command %d timeout (status 0x%08x)\n", | |
bcce53d0 | 253 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
915ffa52 | 254 | return -ETIMEDOUT; |
71a758e1 MV |
255 | } |
256 | ||
257 | /* Check command errors */ | |
258 | if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) { | |
259 | printf("MMC%d: Command %d error (status 0x%08x)!\n", | |
bcce53d0 | 260 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
915ffa52 | 261 | return -ECOMM; |
71a758e1 MV |
262 | } |
263 | ||
264 | /* Copy response to response buffer */ | |
265 | if (cmd->resp_type & MMC_RSP_136) { | |
266 | cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0); | |
267 | cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1); | |
268 | cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2); | |
269 | cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3); | |
270 | } else | |
271 | cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0); | |
272 | ||
273 | /* Return if no data to process */ | |
274 | if (!data) | |
275 | return 0; | |
276 | ||
20255900 | 277 | if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) { |
20255900 MV |
278 | ret = mxsmmc_send_cmd_pio(priv, data); |
279 | if (ret) { | |
280 | printf("MMC%d: Data timeout with command %d " | |
281 | "(status 0x%08x)!\n", | |
bcce53d0 | 282 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
20255900 MV |
283 | return ret; |
284 | } | |
abb85be7 MV |
285 | } else { |
286 | ret = mxsmmc_send_cmd_dma(priv, data); | |
287 | if (ret) { | |
288 | printf("MMC%d: DMA transfer failed\n", | |
bcce53d0 | 289 | mmc->block_dev.devnum); |
abb85be7 MV |
290 | return ret; |
291 | } | |
4cc76c60 | 292 | } |
3687c415 | 293 | |
71a758e1 MV |
294 | /* Check data errors */ |
295 | reg = readl(&ssp_regs->hw_ssp_status); | |
296 | if (reg & | |
297 | (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR | | |
298 | SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) { | |
299 | printf("MMC%d: Data error with command %d (status 0x%08x)!\n", | |
bcce53d0 | 300 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
915ffa52 | 301 | return -ECOMM; |
71a758e1 MV |
302 | } |
303 | ||
304 | return 0; | |
305 | } | |
306 | ||
07b0b9c0 | 307 | static int mxsmmc_set_ios(struct mmc *mmc) |
71a758e1 | 308 | { |
93bfd616 | 309 | struct mxsmmc_priv *priv = mmc->priv; |
9c471142 | 310 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
71a758e1 MV |
311 | |
312 | /* Set the clock speed */ | |
313 | if (mmc->clock) | |
bf48fcb6 | 314 | mxs_set_ssp_busclock(priv->id, mmc->clock / 1000); |
71a758e1 MV |
315 | |
316 | switch (mmc->bus_width) { | |
317 | case 1: | |
318 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT; | |
319 | break; | |
320 | case 4: | |
321 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT; | |
322 | break; | |
323 | case 8: | |
324 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT; | |
325 | break; | |
326 | } | |
327 | ||
328 | /* Set the bus width */ | |
329 | clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0, | |
330 | SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth); | |
331 | ||
332 | debug("MMC%d: Set %d bits bus width\n", | |
bcce53d0 | 333 | mmc->block_dev.devnum, mmc->bus_width); |
07b0b9c0 JC |
334 | |
335 | return 0; | |
71a758e1 MV |
336 | } |
337 | ||
338 | static int mxsmmc_init(struct mmc *mmc) | |
339 | { | |
93bfd616 | 340 | struct mxsmmc_priv *priv = mmc->priv; |
9c471142 | 341 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
71a758e1 MV |
342 | |
343 | /* Reset SSP */ | |
fa7a51cb | 344 | mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg); |
71a758e1 | 345 | |
8000d8a8 OS |
346 | /* Reconfigure the SSP block for MMC operation */ |
347 | writel(SSP_CTRL1_SSP_MODE_SD_MMC | | |
348 | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS | | |
349 | SSP_CTRL1_DMA_ENABLE | | |
350 | SSP_CTRL1_POLARITY | | |
351 | SSP_CTRL1_RECV_TIMEOUT_IRQ_EN | | |
352 | SSP_CTRL1_DATA_CRC_IRQ_EN | | |
353 | SSP_CTRL1_DATA_TIMEOUT_IRQ_EN | | |
354 | SSP_CTRL1_RESP_TIMEOUT_IRQ_EN | | |
355 | SSP_CTRL1_RESP_ERR_IRQ_EN, | |
356 | &ssp_regs->hw_ssp_ctrl1_set); | |
71a758e1 MV |
357 | |
358 | /* Set initial bit clock 400 KHz */ | |
bf48fcb6 | 359 | mxs_set_ssp_busclock(priv->id, 400); |
71a758e1 MV |
360 | |
361 | /* Send initial 74 clock cycles (185 us @ 400 KHz)*/ | |
362 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set); | |
363 | udelay(200); | |
364 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr); | |
365 | ||
366 | return 0; | |
367 | } | |
368 | ||
ab769f22 PA |
369 | static const struct mmc_ops mxsmmc_ops = { |
370 | .send_cmd = mxsmmc_send_cmd, | |
371 | .set_ios = mxsmmc_set_ios, | |
372 | .init = mxsmmc_init, | |
373 | }; | |
374 | ||
90bc2bf2 | 375 | int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int), int (*cd)(int)) |
71a758e1 | 376 | { |
71a758e1 MV |
377 | struct mmc *mmc = NULL; |
378 | struct mxsmmc_priv *priv = NULL; | |
96666a39 | 379 | int ret; |
3430e0bd | 380 | const unsigned int mxsmmc_clk_id = mxs_ssp_clock_by_bus(id); |
1a3c5ffe | 381 | |
3430e0bd | 382 | if (!mxs_ssp_bus_id_valid(id)) |
1a3c5ffe | 383 | return -ENODEV; |
71a758e1 | 384 | |
71a758e1 | 385 | priv = malloc(sizeof(struct mxsmmc_priv)); |
93bfd616 | 386 | if (!priv) |
71a758e1 | 387 | return -ENOMEM; |
71a758e1 | 388 | |
3687c415 MV |
389 | priv->desc = mxs_dma_desc_alloc(); |
390 | if (!priv->desc) { | |
391 | free(priv); | |
3687c415 MV |
392 | return -ENOMEM; |
393 | } | |
394 | ||
3430e0bd | 395 | ret = mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + id); |
96666a39 MV |
396 | if (ret) |
397 | return ret; | |
398 | ||
71a758e1 | 399 | priv->mmc_is_wp = wp; |
90bc2bf2 | 400 | priv->mmc_cd = cd; |
71a758e1 | 401 | priv->id = id; |
14e26bcf | 402 | priv->regs = mxs_ssp_regs_by_bus(id); |
71a758e1 | 403 | |
93bfd616 PA |
404 | priv->cfg.name = "MXS MMC"; |
405 | priv->cfg.ops = &mxsmmc_ops; | |
71a758e1 | 406 | |
93bfd616 | 407 | priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
71a758e1 | 408 | |
93bfd616 | 409 | priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | |
5a20397b | 410 | MMC_MODE_HS_52MHz | MMC_MODE_HS; |
71a758e1 MV |
411 | |
412 | /* | |
413 | * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz | |
414 | * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)), | |
415 | * CLOCK_DIVIDE has to be an even value from 2 to 254, and | |
416 | * CLOCK_RATE could be any integer from 0 to 255. | |
417 | */ | |
93bfd616 PA |
418 | priv->cfg.f_min = 400000; |
419 | priv->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id) * 1000 / 2; | |
420 | priv->cfg.b_max = 0x20; | |
71a758e1 | 421 | |
93bfd616 PA |
422 | mmc = mmc_create(&priv->cfg, priv); |
423 | if (mmc == NULL) { | |
424 | mxs_dma_desc_free(priv->desc); | |
425 | free(priv); | |
426 | return -ENOMEM; | |
427 | } | |
71a758e1 MV |
428 | return 0; |
429 | } |