blob: 2480a8a870778877a3fb37cc33bad8386c9782fd [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
65/*
66 * Sends a command out on the bus. Takes the mmc pointer,
67 * a command pointer, and an optional data pointer.
68 */
69static int
70mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
71{
72 struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +000073 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +000074 uint32_t reg;
75 int timeout;
Marek Vasut4cc76c62012-04-05 03:30:35 +000076 uint32_t data_count;
Marek Vasut71a758e12011-11-08 23:18:09 +000077 uint32_t ctrl0;
Marek Vasut4cc76c62012-04-05 03:30:35 +000078#ifndef CONFIG_MXS_MMC_DMA
79 uint32_t *data_ptr;
80#else
81 uint32_t cache_data_count;
Marek Vasut401650a2012-07-06 21:25:54 +000082 int dmach;
Marek Vasut4cc76c62012-04-05 03:30:35 +000083#endif
Marek Vasut71a758e12011-11-08 23:18:09 +000084
85 debug("MMC%d: CMD%d\n", mmc->block_dev.dev, cmd->cmdidx);
86
87 /* Check bus busy */
88 timeout = MXSMMC_MAX_TIMEOUT;
89 while (--timeout) {
90 udelay(1000);
91 reg = readl(&ssp_regs->hw_ssp_status);
92 if (!(reg &
93 (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY |
94 SSP_STATUS_CMD_BUSY))) {
95 break;
96 }
97 }
98
99 if (!timeout) {
100 printf("MMC%d: Bus busy timeout!\n", mmc->block_dev.dev);
101 return TIMEOUT;
102 }
103
104 /* See if card is present */
105 if (readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT) {
106 printf("MMC%d: No card detected!\n", mmc->block_dev.dev);
107 return NO_CARD_ERR;
108 }
109
110 /* Start building CTRL0 contents */
111 ctrl0 = priv->buswidth;
112
113 /* Set up command */
114 if (!(cmd->resp_type & MMC_RSP_CRC))
115 ctrl0 |= SSP_CTRL0_IGNORE_CRC;
116 if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */
117 ctrl0 |= SSP_CTRL0_GET_RESP;
118 if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */
119 ctrl0 |= SSP_CTRL0_LONG_RESP;
120
121 /* Command index */
122 reg = readl(&ssp_regs->hw_ssp_cmd0);
123 reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC);
124 reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET;
125 if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION)
126 reg |= SSP_CMD0_APPEND_8CYC;
127 writel(reg, &ssp_regs->hw_ssp_cmd0);
128
129 /* Command argument */
130 writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1);
131
132 /* Set up data */
133 if (data) {
134 /* READ or WRITE */
135 if (data->flags & MMC_DATA_READ) {
136 ctrl0 |= SSP_CTRL0_READ;
Marek Vasutc7527b72012-05-01 11:09:52 +0000137 } else if (priv->mmc_is_wp &&
138 priv->mmc_is_wp(mmc->block_dev.dev)) {
Marek Vasut71a758e12011-11-08 23:18:09 +0000139 printf("MMC%d: Can not write a locked card!\n",
140 mmc->block_dev.dev);
141 return UNUSABLE_ERR;
142 }
143
144 ctrl0 |= SSP_CTRL0_DATA_XFER;
145 reg = ((data->blocks - 1) <<
146 SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) |
147 ((ffs(data->blocksize) - 1) <<
148 SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET);
149 writel(reg, &ssp_regs->hw_ssp_block_size);
150
151 reg = data->blocksize * data->blocks;
152 writel(reg, &ssp_regs->hw_ssp_xfer_size);
153 }
154
155 /* Kick off the command */
156 ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN;
157 writel(ctrl0, &ssp_regs->hw_ssp_ctrl0);
158
159 /* Wait for the command to complete */
160 timeout = MXSMMC_MAX_TIMEOUT;
161 while (--timeout) {
162 udelay(1000);
163 reg = readl(&ssp_regs->hw_ssp_status);
164 if (!(reg & SSP_STATUS_CMD_BUSY))
165 break;
166 }
167
168 if (!timeout) {
169 printf("MMC%d: Command %d busy\n",
170 mmc->block_dev.dev, cmd->cmdidx);
171 return TIMEOUT;
172 }
173
174 /* Check command timeout */
175 if (reg & SSP_STATUS_RESP_TIMEOUT) {
176 printf("MMC%d: Command %d timeout (status 0x%08x)\n",
177 mmc->block_dev.dev, cmd->cmdidx, reg);
178 return TIMEOUT;
179 }
180
181 /* Check command errors */
182 if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) {
183 printf("MMC%d: Command %d error (status 0x%08x)!\n",
184 mmc->block_dev.dev, cmd->cmdidx, reg);
185 return COMM_ERR;
186 }
187
188 /* Copy response to response buffer */
189 if (cmd->resp_type & MMC_RSP_136) {
190 cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0);
191 cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1);
192 cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2);
193 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3);
194 } else
195 cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0);
196
197 /* Return if no data to process */
198 if (!data)
199 return 0;
200
Marek Vasut71a758e12011-11-08 23:18:09 +0000201 data_count = data->blocksize * data->blocks;
Marek Vasut4cc76c62012-04-05 03:30:35 +0000202 timeout = MXSMMC_MAX_TIMEOUT;
Marek Vasut3687c412012-03-15 18:33:21 +0000203
Marek Vasut4cc76c62012-04-05 03:30:35 +0000204#ifdef CONFIG_MXS_MMC_DMA
Marek Vasut401650a2012-07-06 21:25:54 +0000205 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set);
206
Marek Vasut3687c412012-03-15 18:33:21 +0000207 if (data_count % ARCH_DMA_MINALIGN)
208 cache_data_count = roundup(data_count, ARCH_DMA_MINALIGN);
209 else
210 cache_data_count = data_count;
211
Marek Vasut71a758e12011-11-08 23:18:09 +0000212 if (data->flags & MMC_DATA_READ) {
Marek Vasut3687c412012-03-15 18:33:21 +0000213 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE;
214 priv->desc->cmd.address = (dma_addr_t)data->dest;
Marek Vasut71a758e12011-11-08 23:18:09 +0000215 } else {
Marek Vasut3687c412012-03-15 18:33:21 +0000216 priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ;
217 priv->desc->cmd.address = (dma_addr_t)data->src;
218
219 /* Flush data to DRAM so DMA can pick them up */
220 flush_dcache_range((uint32_t)priv->desc->cmd.address,
221 (uint32_t)(priv->desc->cmd.address + cache_data_count));
Marek Vasut71a758e12011-11-08 23:18:09 +0000222 }
223
Marek Vasut3687c412012-03-15 18:33:21 +0000224 priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM |
225 (data_count << MXS_DMA_DESC_BYTES_OFFSET);
226
227
Marek Vasut401650a2012-07-06 21:25:54 +0000228 dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id;
229 mxs_dma_desc_append(dmach, priv->desc);
230 if (mxs_dma_go(dmach)) {
Marek Vasut3687c412012-03-15 18:33:21 +0000231 printf("MMC%d: DMA transfer failed\n", mmc->block_dev.dev);
Marek Vasut71a758e12011-11-08 23:18:09 +0000232 return COMM_ERR;
233 }
234
Marek Vasut3687c412012-03-15 18:33:21 +0000235 /* The data arrived into DRAM, invalidate cache over them */
236 if (data->flags & MMC_DATA_READ) {
237 invalidate_dcache_range((uint32_t)priv->desc->cmd.address,
238 (uint32_t)(priv->desc->cmd.address + cache_data_count));
239 }
Marek Vasut4cc76c62012-04-05 03:30:35 +0000240#else
Marek Vasut401650a2012-07-06 21:25:54 +0000241 writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr);
242
Marek Vasut4cc76c62012-04-05 03:30:35 +0000243 if (data->flags & MMC_DATA_READ) {
244 data_ptr = (uint32_t *)data->dest;
245 while (data_count && --timeout) {
246 reg = readl(&ssp_regs->hw_ssp_status);
247 if (!(reg & SSP_STATUS_FIFO_EMPTY)) {
248 *data_ptr++ = readl(&ssp_regs->hw_ssp_data);
249 data_count -= 4;
250 timeout = MXSMMC_MAX_TIMEOUT;
251 } else
252 udelay(1000);
253 }
254 } else {
255 data_ptr = (uint32_t *)data->src;
256 timeout *= 100;
257 while (data_count && --timeout) {
258 reg = readl(&ssp_regs->hw_ssp_status);
259 if (!(reg & SSP_STATUS_FIFO_FULL)) {
260 writel(*data_ptr++, &ssp_regs->hw_ssp_data);
261 data_count -= 4;
262 timeout = MXSMMC_MAX_TIMEOUT;
263 } else
264 udelay(1000);
265 }
266 }
267
268 if (!timeout) {
269 printf("MMC%d: Data timeout with command %d (status 0x%08x)!\n",
270 mmc->block_dev.dev, cmd->cmdidx, reg);
271 return COMM_ERR;
272 }
273#endif
Marek Vasut3687c412012-03-15 18:33:21 +0000274
Marek Vasut71a758e12011-11-08 23:18:09 +0000275 /* Check data errors */
276 reg = readl(&ssp_regs->hw_ssp_status);
277 if (reg &
278 (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR |
279 SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) {
280 printf("MMC%d: Data error with command %d (status 0x%08x)!\n",
281 mmc->block_dev.dev, cmd->cmdidx, reg);
282 return COMM_ERR;
283 }
284
285 return 0;
286}
287
288static void mxsmmc_set_ios(struct mmc *mmc)
289{
290 struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +0000291 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +0000292
293 /* Set the clock speed */
294 if (mmc->clock)
295 mx28_set_ssp_busclock(priv->id, mmc->clock / 1000);
296
297 switch (mmc->bus_width) {
298 case 1:
299 priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT;
300 break;
301 case 4:
302 priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT;
303 break;
304 case 8:
305 priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT;
306 break;
307 }
308
309 /* Set the bus width */
310 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0,
311 SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth);
312
313 debug("MMC%d: Set %d bits bus width\n",
314 mmc->block_dev.dev, mmc->bus_width);
315}
316
317static int mxsmmc_init(struct mmc *mmc)
318{
319 struct mxsmmc_priv *priv = (struct mxsmmc_priv *)mmc->priv;
Otavio Salvador9c471142012-08-05 09:05:31 +0000320 struct mxs_ssp_regs *ssp_regs = priv->regs;
Marek Vasut71a758e12011-11-08 23:18:09 +0000321
322 /* Reset SSP */
323 mx28_reset_block(&ssp_regs->hw_ssp_ctrl0_reg);
324
325 /* 8 bits word length in MMC mode */
326 clrsetbits_le32(&ssp_regs->hw_ssp_ctrl1,
327 SSP_CTRL1_SSP_MODE_MASK | SSP_CTRL1_WORD_LENGTH_MASK,
Marek Vasut3687c412012-03-15 18:33:21 +0000328 SSP_CTRL1_SSP_MODE_SD_MMC | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS |
329 SSP_CTRL1_DMA_ENABLE);
Marek Vasut71a758e12011-11-08 23:18:09 +0000330
331 /* Set initial bit clock 400 KHz */
332 mx28_set_ssp_busclock(priv->id, 400);
333
334 /* Send initial 74 clock cycles (185 us @ 400 KHz)*/
335 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set);
336 udelay(200);
337 writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr);
338
339 return 0;
340}
341
342int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int))
343{
Otavio Salvador9c471142012-08-05 09:05:31 +0000344 struct mxs_clkctrl_regs *clkctrl_regs =
345 (struct mxs_clkctrl_regs *)MXS_CLKCTRL_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000346 struct mmc *mmc = NULL;
347 struct mxsmmc_priv *priv = NULL;
Marek Vasut96666a32012-04-08 17:34:46 +0000348 int ret;
Marek Vasut71a758e12011-11-08 23:18:09 +0000349
350 mmc = malloc(sizeof(struct mmc));
351 if (!mmc)
352 return -ENOMEM;
353
354 priv = malloc(sizeof(struct mxsmmc_priv));
355 if (!priv) {
356 free(mmc);
357 return -ENOMEM;
358 }
359
Marek Vasut3687c412012-03-15 18:33:21 +0000360 priv->desc = mxs_dma_desc_alloc();
361 if (!priv->desc) {
362 free(priv);
363 free(mmc);
364 return -ENOMEM;
365 }
366
Marek Vasut96666a32012-04-08 17:34:46 +0000367 ret = mxs_dma_init_channel(id);
368 if (ret)
369 return ret;
370
Marek Vasut71a758e12011-11-08 23:18:09 +0000371 priv->mmc_is_wp = wp;
372 priv->id = id;
373 switch (id) {
374 case 0:
Otavio Salvador9c471142012-08-05 09:05:31 +0000375 priv->regs = (struct mxs_ssp_regs *)MXS_SSP0_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000376 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP0;
377 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp0;
378 break;
379 case 1:
Otavio Salvador9c471142012-08-05 09:05:31 +0000380 priv->regs = (struct mxs_ssp_regs *)MXS_SSP1_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000381 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP1;
382 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp1;
383 break;
384 case 2:
Otavio Salvador9c471142012-08-05 09:05:31 +0000385 priv->regs = (struct mxs_ssp_regs *)MXS_SSP2_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000386 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP2;
387 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp2;
388 break;
389 case 3:
Otavio Salvador9c471142012-08-05 09:05:31 +0000390 priv->regs = (struct mxs_ssp_regs *)MXS_SSP3_BASE;
Marek Vasut71a758e12011-11-08 23:18:09 +0000391 priv->clkseq_bypass = CLKCTRL_CLKSEQ_BYPASS_SSP3;
392 priv->clkctrl_ssp = &clkctrl_regs->hw_clkctrl_ssp3;
393 break;
394 }
395
396 sprintf(mmc->name, "MXS MMC");
397 mmc->send_cmd = mxsmmc_send_cmd;
398 mmc->set_ios = mxsmmc_set_ios;
399 mmc->init = mxsmmc_init;
Thierry Reding48972d92012-01-02 01:15:37 +0000400 mmc->getcd = NULL;
Marek Vasut71a758e12011-11-08 23:18:09 +0000401 mmc->priv = priv;
402
403 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
404
405 mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT |
406 MMC_MODE_HS_52MHz | MMC_MODE_HS;
407
408 /*
409 * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz
410 * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)),
411 * CLOCK_DIVIDE has to be an even value from 2 to 254, and
412 * CLOCK_RATE could be any integer from 0 to 255.
413 */
414 mmc->f_min = 400000;
415 mmc->f_max = mxc_get_clock(MXC_SSP0_CLK + id) * 1000 / 2;
Marek Vasute7205902012-04-08 18:50:18 +0000416 mmc->b_max = 0x20;
Marek Vasut71a758e12011-11-08 23:18:09 +0000417
418 mmc_register(mmc);
419 return 0;
420}