2 * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com>
4 * Loosely based on the old code and Linux's PXA MMC driver
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of
9 * the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
27 #include <asm/errno.h>
28 #include <asm/arch/hardware.h>
29 #include <asm/arch/regs-mmc.h>
32 /* PXAMMC Generic default config for various CPUs */
33 #if defined(CONFIG_CPU_PXA25X)
34 #define PXAMMC_FIFO_SIZE 1
35 #define PXAMMC_MIN_SPEED 312500
36 #define PXAMMC_MAX_SPEED 20000000
37 #define PXAMMC_HOST_CAPS (0)
38 #elif defined(CONFIG_CPU_PXA27X)
39 #define PXAMMC_CRC_SKIP
40 #define PXAMMC_FIFO_SIZE 32
41 #define PXAMMC_MIN_SPEED 304000
42 #define PXAMMC_MAX_SPEED 19500000
43 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT)
44 #elif defined(CONFIG_CPU_MONAHANS)
45 #define PXAMMC_FIFO_SIZE 32
46 #define PXAMMC_MIN_SPEED 304000
47 #define PXAMMC_MAX_SPEED 26000000
48 #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT | MMC_MODE_HS)
50 #error "This CPU isn't supported by PXA MMC!"
53 #define MMC_STAT_ERRORS \
54 (MMC_STAT_RES_CRC_ERROR | MMC_STAT_SPI_READ_ERROR_TOKEN | \
55 MMC_STAT_CRC_READ_ERROR | MMC_STAT_TIME_OUT_RESPONSE | \
56 MMC_STAT_READ_TIME_OUT | MMC_STAT_CRC_WRITE_ERROR)
58 /* 1 millisecond (in wait cycles below it's 100 x 10uS waits) */
59 #define PXA_MMC_TIMEOUT 100
62 struct pxa_mmc_regs *regs;
65 /* Wait for bit to be set */
66 static int pxa_mmc_wait(struct mmc *mmc, uint32_t mask)
68 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
69 struct pxa_mmc_regs *regs = priv->regs;
70 unsigned int timeout = PXA_MMC_TIMEOUT;
72 /* Wait for bit to be set */
74 if (readl(®s->stat) & mask)
85 static int pxa_mmc_stop_clock(struct mmc *mmc)
87 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
88 struct pxa_mmc_regs *regs = priv->regs;
89 unsigned int timeout = PXA_MMC_TIMEOUT;
91 /* If the clock aren't running, exit */
92 if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
95 /* Tell the controller to turn off the clock */
96 writel(MMC_STRPCL_STOP_CLK, ®s->strpcl);
98 /* Wait until the clock are off */
100 if (!(readl(®s->stat) & MMC_STAT_CLK_EN))
105 /* The clock refused to stop, scream and die a painful death */
109 /* The clock stopped correctly */
113 static int pxa_mmc_start_cmd(struct mmc *mmc, struct mmc_cmd *cmd,
116 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
117 struct pxa_mmc_regs *regs = priv->regs;
120 /* The card can send a "busy" response */
121 if (cmd->resp_type & MMC_RSP_BUSY)
122 cmdat |= MMC_CMDAT_BUSY;
124 /* Inform the controller about response type */
125 switch (cmd->resp_type) {
128 cmdat |= MMC_CMDAT_R1;
131 cmdat |= MMC_CMDAT_R2;
134 cmdat |= MMC_CMDAT_R3;
140 /* Load command and it's arguments into the controller */
141 writel(cmd->cmdidx, ®s->cmd);
142 writel(cmd->cmdarg >> 16, ®s->argh);
143 writel(cmd->cmdarg & 0xffff, ®s->argl);
144 writel(cmdat, ®s->cmdat);
146 /* Start the controller clock and wait until they are started */
147 writel(MMC_STRPCL_START_CLK, ®s->strpcl);
149 ret = pxa_mmc_wait(mmc, MMC_STAT_CLK_EN);
153 /* Correct and happy end */
157 static int pxa_mmc_cmd_done(struct mmc *mmc, struct mmc_cmd *cmd)
159 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
160 struct pxa_mmc_regs *regs = priv->regs;
165 /* Read the controller status */
166 stat = readl(®s->stat);
170 * Did I mention this is Sick. We always need to
171 * discard the upper 8 bits of the first 16-bit word.
173 a = readl(®s->res) & 0xffff;
174 for (i = 0; i < 4; i++) {
175 b = readl(®s->res) & 0xffff;
176 c = readl(®s->res) & 0xffff;
177 cmd->response[i] = (a << 24) | (b << 8) | (c >> 8);
181 /* The command response didn't arrive */
182 if (stat & MMC_STAT_TIME_OUT_RESPONSE)
184 else if (stat & MMC_STAT_RES_CRC_ERROR
185 && cmd->resp_type & MMC_RSP_CRC) {
186 #ifdef PXAMMC_CRC_SKIP
187 if (cmd->resp_type & MMC_RSP_136
188 && cmd->response[0] & (1 << 31))
189 printf("Ignoring CRC, this may be dangerous!\n");
195 /* The command response was successfully read */
199 static int pxa_mmc_do_read_xfer(struct mmc *mmc, struct mmc_data *data)
201 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
202 struct pxa_mmc_regs *regs = priv->regs;
204 uint32_t *buf = (uint32_t *)data->dest;
208 len = data->blocks * data->blocksize;
211 /* The controller has data ready */
212 if (readl(®s->i_reg) & MMC_I_REG_RXFIFO_RD_REQ) {
213 size = min(len, PXAMMC_FIFO_SIZE);
217 /* Read data into the buffer */
219 *buf++ = readl(®s->rxfifo);
223 if (readl(®s->stat) & MMC_STAT_ERRORS)
227 /* Wait for the transmission-done interrupt */
228 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
235 static int pxa_mmc_do_write_xfer(struct mmc *mmc, struct mmc_data *data)
237 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
238 struct pxa_mmc_regs *regs = priv->regs;
240 uint32_t *buf = (uint32_t *)data->src;
244 len = data->blocks * data->blocksize;
247 /* The controller is ready to receive data */
248 if (readl(®s->i_reg) & MMC_I_REG_TXFIFO_WR_REQ) {
249 size = min(len, PXAMMC_FIFO_SIZE);
254 writel(*buf++, ®s->txfifo);
256 if (min(len, PXAMMC_FIFO_SIZE) < 32)
257 writel(MMC_PRTBUF_BUF_PART_FULL, ®s->prtbuf);
260 if (readl(®s->stat) & MMC_STAT_ERRORS)
264 /* Wait for the transmission-done interrupt */
265 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
269 /* Wait until the data are really written to the card */
270 ret = pxa_mmc_wait(mmc, MMC_STAT_PRG_DONE);
277 static int pxa_mmc_request(struct mmc *mmc, struct mmc_cmd *cmd,
278 struct mmc_data *data)
280 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
281 struct pxa_mmc_regs *regs = priv->regs;
285 /* Stop the controller */
286 ret = pxa_mmc_stop_clock(mmc);
290 /* If we're doing data transfer, configure the controller accordingly */
292 writel(data->blocks, ®s->nob);
293 writel(data->blocksize, ®s->blklen);
294 /* This delay can be optimized, but stick with max value */
295 writel(0xffff, ®s->rdto);
296 cmdat |= MMC_CMDAT_DATA_EN;
297 if (data->flags & MMC_DATA_WRITE)
298 cmdat |= MMC_CMDAT_WRITE;
301 /* Run in 4bit mode if the card can do it */
302 if (mmc->bus_width == 4)
303 cmdat |= MMC_CMDAT_SD_4DAT;
305 /* Execute the command */
306 ret = pxa_mmc_start_cmd(mmc, cmd, cmdat);
310 /* Wait until the command completes */
311 ret = pxa_mmc_wait(mmc, MMC_STAT_END_CMD_RES);
315 /* Read back the result */
316 ret = pxa_mmc_cmd_done(mmc, cmd);
320 /* In case there was a data transfer scheduled, do it */
322 if (data->flags & MMC_DATA_WRITE)
323 pxa_mmc_do_write_xfer(mmc, data);
325 pxa_mmc_do_read_xfer(mmc, data);
331 static void pxa_mmc_set_ios(struct mmc *mmc)
333 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
334 struct pxa_mmc_regs *regs = priv->regs;
336 uint32_t pxa_mmc_clock;
339 pxa_mmc_stop_clock(mmc);
343 /* PXA3xx can do 26MHz with special settings. */
344 if (mmc->clock == 26000000) {
345 writel(0x7, ®s->clkrt);
349 /* Set clock to the card the usual way. */
351 tmp = mmc->f_max / mmc->clock;
359 writel(pxa_mmc_clock, ®s->clkrt);
362 static int pxa_mmc_init(struct mmc *mmc)
364 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
365 struct pxa_mmc_regs *regs = priv->regs;
367 /* Make sure the clock are stopped */
368 pxa_mmc_stop_clock(mmc);
370 /* Turn off SPI mode */
371 writel(0, ®s->spi);
373 /* Set up maximum timeout to wait for command response */
374 writel(MMC_RES_TO_MAX_MASK, ®s->resto);
376 /* Mask all interrupts */
377 writel(~(MMC_I_MASK_TXFIFO_WR_REQ | MMC_I_MASK_RXFIFO_RD_REQ),
382 int pxa_mmc_register(int card_index)
385 struct pxa_mmc_priv *priv;
389 mmc = malloc(sizeof(struct mmc));
393 priv = malloc(sizeof(struct pxa_mmc_priv));
397 switch (card_index) {
399 priv->regs = (struct pxa_mmc_regs *)MMC0_BASE;
402 priv->regs = (struct pxa_mmc_regs *)MMC1_BASE;
405 printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n",
412 sprintf(mmc->name, "PXA MMC");
413 mmc->send_cmd = pxa_mmc_request;
414 mmc->set_ios = pxa_mmc_set_ios;
415 mmc->init = pxa_mmc_init;
418 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
419 mmc->f_max = PXAMMC_MAX_SPEED;
420 mmc->f_min = PXAMMC_MIN_SPEED;
421 mmc->host_caps = PXAMMC_HOST_CAPS;
425 #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
431 reg |= CKENA_12_MMC0 | CKENA_13_MMC1;