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->flags & 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 && cmd->flags & MMC_RSP_CRC) {
185 #ifdef PXAMMC_CRC_SKIP
186 if (cmd->flags & MMC_RSP_136 && cmd->response[0] & (1 << 31))
187 printf("Ignoring CRC, this may be dangerous!\n");
193 /* The command response was successfully read */
197 static int pxa_mmc_do_read_xfer(struct mmc *mmc, struct mmc_data *data)
199 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
200 struct pxa_mmc_regs *regs = priv->regs;
202 uint32_t *buf = (uint32_t *)data->dest;
206 len = data->blocks * data->blocksize;
209 /* The controller has data ready */
210 if (readl(®s->i_reg) & MMC_I_REG_RXFIFO_RD_REQ) {
211 size = min(len, PXAMMC_FIFO_SIZE);
215 /* Read data into the buffer */
217 *buf++ = readl(®s->rxfifo);
221 if (readl(®s->stat) & MMC_STAT_ERRORS)
225 /* Wait for the transmission-done interrupt */
226 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
233 static int pxa_mmc_do_write_xfer(struct mmc *mmc, struct mmc_data *data)
235 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
236 struct pxa_mmc_regs *regs = priv->regs;
238 uint32_t *buf = (uint32_t *)data->src;
242 len = data->blocks * data->blocksize;
245 /* The controller is ready to receive data */
246 if (readl(®s->i_reg) & MMC_I_REG_TXFIFO_WR_REQ) {
247 size = min(len, PXAMMC_FIFO_SIZE);
252 writel(*buf++, ®s->txfifo);
254 if (min(len, PXAMMC_FIFO_SIZE) < 32)
255 writel(MMC_PRTBUF_BUF_PART_FULL, ®s->prtbuf);
258 if (readl(®s->stat) & MMC_STAT_ERRORS)
262 /* Wait for the transmission-done interrupt */
263 ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE);
267 /* Wait until the data are really written to the card */
268 ret = pxa_mmc_wait(mmc, MMC_STAT_PRG_DONE);
275 static int pxa_mmc_request(struct mmc *mmc, struct mmc_cmd *cmd,
276 struct mmc_data *data)
278 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
279 struct pxa_mmc_regs *regs = priv->regs;
283 /* Stop the controller */
284 ret = pxa_mmc_stop_clock(mmc);
288 /* If we're doing data transfer, configure the controller accordingly */
290 writel(data->blocks, ®s->nob);
291 writel(data->blocksize, ®s->blklen);
292 /* This delay can be optimized, but stick with max value */
293 writel(0xffff, ®s->rdto);
294 cmdat |= MMC_CMDAT_DATA_EN;
295 if (data->flags & MMC_DATA_WRITE)
296 cmdat |= MMC_CMDAT_WRITE;
299 /* Run in 4bit mode if the card can do it */
300 if (mmc->bus_width == 4)
301 cmdat |= MMC_CMDAT_SD_4DAT;
303 /* Execute the command */
304 ret = pxa_mmc_start_cmd(mmc, cmd, cmdat);
308 /* Wait until the command completes */
309 ret = pxa_mmc_wait(mmc, MMC_STAT_END_CMD_RES);
313 /* Read back the result */
314 ret = pxa_mmc_cmd_done(mmc, cmd);
318 /* In case there was a data transfer scheduled, do it */
320 if (data->flags & MMC_DATA_WRITE)
321 pxa_mmc_do_write_xfer(mmc, data);
323 pxa_mmc_do_read_xfer(mmc, data);
329 static void pxa_mmc_set_ios(struct mmc *mmc)
331 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
332 struct pxa_mmc_regs *regs = priv->regs;
334 uint32_t pxa_mmc_clock;
337 pxa_mmc_stop_clock(mmc);
341 /* PXA3xx can do 26MHz with special settings. */
342 if (mmc->clock == 26000000) {
343 writel(0x7, ®s->clkrt);
347 /* Set clock to the card the usual way. */
349 tmp = mmc->f_max / mmc->clock;
357 writel(pxa_mmc_clock, ®s->clkrt);
360 static int pxa_mmc_init(struct mmc *mmc)
362 struct pxa_mmc_priv *priv = (struct pxa_mmc_priv *)mmc->priv;
363 struct pxa_mmc_regs *regs = priv->regs;
365 /* Make sure the clock are stopped */
366 pxa_mmc_stop_clock(mmc);
368 /* Turn off SPI mode */
369 writel(0, ®s->spi);
371 /* Set up maximum timeout to wait for command response */
372 writel(MMC_RES_TO_MAX_MASK, ®s->resto);
374 /* Mask all interrupts */
375 writel(~(MMC_I_MASK_TXFIFO_WR_REQ | MMC_I_MASK_RXFIFO_RD_REQ),
380 int pxa_mmc_register(int card_index)
383 struct pxa_mmc_priv *priv;
387 mmc = malloc(sizeof(struct mmc));
391 priv = malloc(sizeof(struct pxa_mmc_priv));
395 switch (card_index) {
397 priv->regs = (struct pxa_mmc_regs *)MMC0_BASE;
400 priv->regs = (struct pxa_mmc_regs *)MMC1_BASE;
403 printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n",
410 sprintf(mmc->name, "PXA MMC");
411 mmc->send_cmd = pxa_mmc_request;
412 mmc->set_ios = pxa_mmc_set_ios;
413 mmc->init = pxa_mmc_init;
416 mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
417 mmc->f_max = PXAMMC_MAX_SPEED;
418 mmc->f_min = PXAMMC_MIN_SPEED;
419 mmc->host_caps = PXAMMC_HOST_CAPS;
423 #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */
429 reg |= CKENA_12_MMC0 | CKENA_13_MMC1;