blob: 8e2016f52300e85ec2258991b6e07da57aaf309f [file] [log] [blame]
Marek Vasut71a758e12011-11-08 23:18:09 +00001/*
2 * Freescale i.MX28 SSP MMC driver
3 *
4 * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com>
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. kharris@nexus-tech.net
17 *
18 * See file CREDITS for list of people who contributed to this
19 * project.
20 *
21 * This program is free software; you can redistribute it and/or
22 * modify it under the terms of the GNU General Public License as
23 * published by the Free Software Foundation; either version 2 of
24 * the License, or (at your option) any later version.
25 *
26 * This program is distributed in the hope that it will be useful,
27 * but WITHOUT ANY WARRANTY; without even the implied warranty of
28 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
29 * GNU General Public License for more details.
30 *
31 * You should have received a copy of the GNU General Public License
32 * along with this program; if not, write to the Free Software
33 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
34 * MA 02111-1307 USA
35 */
36#include <common.h>
37#include <malloc.h>
38#include <mmc.h>
39#include <asm/errno.h>
40#include <asm/io.h>
41#include <asm/arch/clock.h>
42#include <asm/arch/imx-regs.h>
43#include <asm/arch/sys_proto.h>
Marek Vasut3687c412012-03-15 18:33:21 +000044#include <asm/arch/dma.h>
Marek Vasut71a758e12011-11-08 23:18:09 +000045
Marek Vasut4cc76c62012-04-05 03:30:35 +000046/*
47 * CONFIG_MXS_MMC_DMA: This feature is highly experimental and has no
48 * performance benefit unless you operate the platform with
49 * data cache enabled. This is disabled by default, enable
50 * only if you know what you're doing.
51 */
52
Marek Vasut71a758e12011-11-08 23:18:09 +000053struct mxsmmc_priv {
54 int id;
Otavio Salvador9c471142012-08-05 09:05:31 +000055 struct mxs_ssp_regs *regs;
Marek Vasut71a758e12011-11-08 23:18:09 +000056 uint32_t clkseq_bypass;
57 uint32_t *clkctrl_ssp;
58 uint32_t buswidth;
59 int (*mmc_is_wp)(int);
Marek Vasut3687c412012-03-15 18:33:21 +000060 struct mxs_dma_desc *desc;
Marek Vasut71a758e12011-11-08 23:18:09 +000061};
62
63#define MXSMMC_MAX_TIMEOUT 10000
64
Marek Vasut86983322012-07-06 21:25:55 +000065#ifndef CONFIG_MXS_MMC_DMA
66static int mxsmmc_send_cmd_pio(struct mxsmmc_priv *priv, struct mmc_data *data)
67{
68 struct mxs_ssp_regs *ssp_regs = priv->regs;
69 uint32_t *data_ptr;
70 int timeout = MXSMMC_MAX_TIMEOUT;
71 uint32_t reg;
72 uint32_t data_count = data->blocksize * data->blocks;
73
74 if (data->flags & MMC_DATA_READ) {
75 data_ptr = (uint32_t *)data->dest;
76 while (data_count && --timeout) {
77 reg = readl(&ssp_regs->hw_ssp_status);
78 if (!(reg & SSP_STATUS_FIFO_EMPTY)) {
79 *data_ptr++ = readl(&ssp_regs->hw_ssp_data);
80 data_count -= 4;
81 timeout = MXSMMC_MAX_TIMEOUT;
82 } else
83 udelay(1000);
84 }
85 } else {
86 data_ptr = (uint32_t *)data->src;
87 timeout *= 100;
88 while (data_count && --timeout) {
89 reg = readl(&ssp_regs->hw_ssp_status);
90 if (!(reg & SSP_STATUS_FIFO_FULL)) {
91 writel(*data_ptr++, &ssp_regs->hw_ssp_data);
92 data_count -= 4;
93 timeout = MXSMMC_MAX_TIMEOUT;
94 } else
95 udelay(1000);
96 }
97 }
98
99 return timeout ? 0 : COMM_ERR;
100}
101#else
102static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data)
103{
104 uint32_t data_count = data->blocksize * data->blocks;
105 uint32_t cache_data_count;
106 int dmach;
107
108 if (data_count % ARCH_DMA_MINALIGN)
109 cache_data_count = roundup(data_count, ARCH_DMA_MINALIGN);
110 else
111 cache_data_count = data_count;
112
113 if (data->flags & MMC_DATA_READ) {
114 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
115 priv->desc->cmd.address = (dma_addr_t)data->dest;
116 } else {
117 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
118 priv->desc->cmd.address = (dma_addr_t)data->src;
119
120 /* Flush data to DRAM so DMA can pick them up */
121 flush_dcache_range((uint32_t)priv->desc->cmd.address,
122 (uint32_t)(priv->desc->cmd.address + cache_data_count));
123 }
124
125 priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM |
126 (data_count << MXS_DMA_DESC_BYTES_OFFSET);
127
128
129 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id;
130 mxs_dma_desc_append(dmach, priv->desc);
131 if (mxs_dma_go(dmach))
132 return COMM_ERR;
133
134 /* The data arrived into DRAM, invalidate cache over them */
135 if (data->flags & MMC_DATA_READ) {
136 invalidate_dcache_range((uint32_t)priv->desc->cmd.address,
137 (uint32_t)(priv->desc->cmd.address + cache_data_count));
138 }
139
140 return 0;
141}
142#endif
143
Marek Vasut71a758e12011-11-08 23:18:09 +0000144/*
145 * Sends a command out on the bus. Takes the mmc pointer,
146 * a command pointer, and an optional data pointer.
147 */
148static int
149mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
150{
151 struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +0000152 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +0000153 uint32_t reg;
154 int timeout;
Marek Vasut71a758e12011-11-08 23:18:09 +0000155 uint32_t ctrl0;
Marek Vasut86983322012-07-06 21:25:55 +0000156 int ret;
Marek Vasut71a758e12011-11-08 23:18:09 +0000157
158 debug("MMC%d: CMD%d\n", mmc->block_dev.dev, cmd->cmdidx);
159
160 /* Check bus busy */
161 timeout = MXSMMC_MAX_TIMEOUT;
162 while (--timeout) {
163 udelay(1000);
164 reg = readl(&ssp_regs->hw_ssp_status);
165 if (!(reg &
166 (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY |
167 SSP_STATUS_CMD_BUSY))) {
168 break;
169 }
170 }
171
172 if (!timeout) {
173 printf("MMC%d: Bus busy timeout!\n", mmc->block_dev.dev);
174 return TIMEOUT;
175 }
176
177 /* See if card is present */
178 if (readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT) {
179 printf("MMC%d: No card detected!\n", mmc->block_dev.dev);
180 return NO_CARD_ERR;
181 }
182
183 /* Start building CTRL0 contents */
184 ctrl0 = priv->buswidth;
185
186 /* Set up command */
187 if (!(cmd->resp_type & MMC_RSP_CRC))
188 ctrl0 |= SSP_CTRL0_IGNORE_CRC;
189 if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */
190 ctrl0 |= SSP_CTRL0_GET_RESP;
191 if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */
192 ctrl0 |= SSP_CTRL0_LONG_RESP;
193
194 /* Command index */
195 reg = readl(&ssp_regs->hw_ssp_cmd0);
196 reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC);
197 reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET;
198 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
199 reg |= SSP_CMD0_APPEND_8CYC;
200 writel(reg, &ssp_regs->hw_ssp_cmd0);
201
202 /* Command argument */
203 writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1);
204
205 /* Set up data */
206 if (data) {
207 /* READ or WRITE */
208 if (data->flags & MMC_DATA_READ) {
209 ctrl0 |= SSP_CTRL0_READ;
Marek Vasutc7527b72012-05-01 11:09:52 +0000210 } else if (priv->mmc_is_wp &&
211 priv->mmc_is_wp(mmc->block_dev.dev)) {
Marek Vasut71a758e12011-11-08 23:18:09 +0000212 printf("MMC%d: Can not write a locked card!\n",
213 mmc->block_dev.dev);
214 return UNUSABLE_ERR;
215 }
216
217 ctrl0 |= SSP_CTRL0_DATA_XFER;
218 reg = ((data->blocks - 1) <<
219 SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) |
220 ((ffs(data->blocksize) - 1) <<
221 SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET);
222 writel(reg, &ssp_regs->hw_ssp_block_size);
223
224 reg = data->blocksize * data->blocks;
225 writel(reg, &ssp_regs->hw_ssp_xfer_size);
226 }
227
228 /* Kick off the command */
229 ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN;
230 writel(ctrl0, &ssp_regs->hw_ssp_ctrl0);
231
232 /* Wait for the command to complete */
233 timeout = MXSMMC_MAX_TIMEOUT;
234 while (--timeout) {
235 udelay(1000);
236 reg = readl(&ssp_regs->hw_ssp_status);
237 if (!(reg & SSP_STATUS_CMD_BUSY))
238 break;
239 }
240
241 if (!timeout) {
242 printf("MMC%d: Command %d busy\n",
243 mmc->block_dev.dev, cmd->cmdidx);
244 return TIMEOUT;
245 }
246
247 /* Check command timeout */
248 if (reg & SSP_STATUS_RESP_TIMEOUT) {
249 printf("MMC%d: Command %d timeout (status 0x%08x)\n",
250 mmc->block_dev.dev, cmd->cmdidx, reg);
251 return TIMEOUT;
252 }
253
254 /* Check command errors */
255 if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) {
256 printf("MMC%d: Command %d error (status 0x%08x)!\n",
257 mmc->block_dev.dev, cmd->cmdidx, reg);
258 return COMM_ERR;
259 }
260
261 /* Copy response to response buffer */
262 if (cmd->resp_type & MMC_RSP_136) {
263 cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0);
264 cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1);
265 cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2);
266 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3);
267 } else
268 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0);
269
270 /* Return if no data to process */
271 if (!data)
272 return 0;
273
Marek Vasut4cc76c62012-04-05 03:30:35 +0000274#ifdef CONFIG_MXS_MMC_DMA
Marek Vasut401650a2012-07-06 21:25:54 +0000275 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
276
Marek Vasut86983322012-07-06 21:25:55 +0000277 ret = mxsmmc_send_cmd_dma(priv, data);
278 if (ret) {
Marek Vasut3687c412012-03-15 18:33:21 +0000279 printf("MMC%d: DMA transfer failed\n", mmc->block_dev.dev);
Marek Vasut86983322012-07-06 21:25:55 +0000280 return ret;
Marek Vasut3687c412012-03-15 18:33:21 +0000281 }
Marek Vasut4cc76c62012-04-05 03:30:35 +0000282#else
Marek Vasut401650a2012-07-06 21:25:54 +0000283 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
284
Marek Vasut86983322012-07-06 21:25:55 +0000285 ret = mxsmmc_send_cmd_pio(priv, data);
286 if (ret) {
Marek Vasut4cc76c62012-04-05 03:30:35 +0000287 printf("MMC%d: Data timeout with command %d (status 0x%08x)!\n",
288 mmc->block_dev.dev, cmd->cmdidx, reg);
Marek Vasut86983322012-07-06 21:25:55 +0000289 return ret;
Marek Vasut4cc76c62012-04-05 03:30:35 +0000290 }
291#endif
Marek Vasut3687c412012-03-15 18:33:21 +0000292
Marek Vasut71a758e12011-11-08 23:18:09 +0000293 /* Check data errors */
294 reg = readl(&ssp_regs->hw_ssp_status);
295 if (reg &
296 (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR |
297 SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) {
298 printf("MMC%d: Data error with command %d (status 0x%08x)!\n",
299 mmc->block_dev.dev, cmd->cmdidx, reg);
300 return COMM_ERR;
301 }
302
303 return 0;
304}
305
306static void mxsmmc_set_ios(struct mmc *mmc)
307{
308 struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +0000309 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +0000310
311 /* Set the clock speed */
312 if (mmc->clock)
313 mx28_set_ssp_busclock(priv->id, mmc->clock / 1000);
314
315 switch (mmc->bus_width) {
316 case 1:
317 priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT;
318 break;
319 case 4:
320 priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT;
321 break;
322 case 8:
323 priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT;
324 break;
325 }
326
327 /* Set the bus width */
328 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
329 SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth);
330
331 debug("MMC%d: Set %d bits bus width\n",
332 mmc->block_dev.dev, mmc->bus_width);
333}
334
335static int mxsmmc_init(struct mmc *mmc)
336{
337 struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +0000338 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +0000339
340 /* Reset SSP */
341 mx28_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
342
343 /* 8 bits word length in MMC mode */
344 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl1,
345 SSP_CTRL1_SSP_MODE_MASK | SSP_CTRL1_WORD_LENGTH_MASK,
Marek Vasut3687c412012-03-15 18:33:21 +0000346 SSP_CTRL1_SSP_MODE_SD_MMC | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS |
347 SSP_CTRL1_DMA_ENABLE);
Marek Vasut71a758e12011-11-08 23:18:09 +0000348
349 /* Set initial bit clock 400 KHz */
350 mx28_set_ssp_busclock(priv->id, 400);
351
352 /* Send initial 74 clock cycles (185 us @ 400 KHz)*/
353 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set);
354 udelay(200);
355 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr);
356
357 return 0;
358}
359
360int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int))
361{
Otavio Salvador9c471142012-08-05 09:05:31 +0000362 struct mxs_clkctrl_regs *clkctrl_regs =
363 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000364 struct mmc *mmc = NULL;
365 struct mxsmmc_priv *priv = NULL;
Marek Vasut96666a32012-04-08 17:34:46 +0000366 int ret;
Marek Vasut71a758e12011-11-08 23:18:09 +0000367
368 mmc = malloc(sizeof(struct mmc));
369 if (!mmc)
370 return -ENOMEM;
371
372 priv = malloc(sizeof(struct mxsmmc_priv));
373 if (!priv) {
374 free(mmc);
375 return -ENOMEM;
376 }
377
Marek Vasut3687c412012-03-15 18:33:21 +0000378 priv->desc = mxs_dma_desc_alloc();
379 if (!priv->desc) {
380 free(priv);
381 free(mmc);
382 return -ENOMEM;
383 }
384
Marek Vasut96666a32012-04-08 17:34:46 +0000385 ret = mxs_dma_init_channel(id);
386 if (ret)
387 return ret;
388
Marek Vasut71a758e12011-11-08 23:18:09 +0000389 priv->mmc_is_wp = wp;
390 priv->id = id;
391 switch (id) {
392 case 0:
Otavio Salvador9c471142012-08-05 09:05:31 +0000393 priv->regs = (struct mxs_ssp_regs *)MXS_SSP0_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000394 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP0;
395 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp0;
396 break;
397 case 1:
Otavio Salvador9c471142012-08-05 09:05:31 +0000398 priv->regs = (struct mxs_ssp_regs *)MXS_SSP1_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000399 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP1;
400 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp1;
401 break;
402 case 2:
Otavio Salvador9c471142012-08-05 09:05:31 +0000403 priv->regs = (struct mxs_ssp_regs *)MXS_SSP2_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000404 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP2;
405 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp2;
406 break;
407 case 3:
Otavio Salvador9c471142012-08-05 09:05:31 +0000408 priv->regs = (struct mxs_ssp_regs *)MXS_SSP3_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000409 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP3;
410 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp3;
411 break;
412 }
413
414 sprintf(mmc->name, "MXS MMC");
415 mmc->send_cmd = mxsmmc_send_cmd;
416 mmc->set_ios = mxsmmc_set_ios;
417 mmc->init = mxsmmc_init;
Thierry Reding48972d92012-01-02 01:15:37 +0000418 mmc->getcd = NULL;
Marek Vasut71a758e12011-11-08 23:18:09 +0000419 mmc->priv = priv;
420
421 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
422
423 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
424 MMC_MODE_HS_52MHz | MMC_MODE_HS;
425
426 /*
427 * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz
428 * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
429 * CLOCK_DIVIDE has to be an even value from 2 to 254, and
430 * CLOCK_RATE could be any integer from 0 to 255.
431 */
432 mmc->f_min = 400000;
433 mmc->f_max = mxc_get_clock(MXC_SSP0_CLK + id) * 1000 / 2;
Marek Vasute7205902012-04-08 18:50:18 +0000434 mmc->b_max = 0x20;
Marek Vasut71a758e12011-11-08 23:18:09 +0000435
436 mmc_register(mmc);
437 return 0;
438}