7 * Copyright (C) 2004-2006 Atmel Corporation
9 * SPDX-License-Identifier: GPL-2.0+
17 #include <asm/errno.h>
18 #include <asm/byteorder.h>
19 #include <asm/arch/clk.h>
20 #include <asm/arch/hardware.h>
21 #include "atmel_mci.h"
23 #ifndef CONFIG_SYS_MMC_CLK_OD
24 # define CONFIG_SYS_MMC_CLK_OD 150000
27 #define MMC_DEFAULT_BLKLEN 512
29 #if defined(CONFIG_ATMEL_MCI_PORTB)
35 static int initialized = 0;
37 /* Read Atmel MCI IP version */
38 static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
40 return readl(&mci->version) & 0x00000fff;
44 * Print command and status:
46 * - always when DEBUG is defined
49 static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
51 debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
52 cmdr, cmdr & 0x3F, arg, status, msg);
55 /* Setup for MCI Clock and Block Size */
56 static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
58 atmel_mci_t *mci = mmc->priv;
59 u32 bus_hz = get_mci_clk_rate();
61 unsigned int version = atmel_mci_get_version(mci);
65 debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
68 if (version >= 0x500) {
69 clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2;
76 debug("mci: setting clock %u Hz, block size %u\n",
77 bus_hz / (clkdiv * 2 + clkodd + 2), blklen);
79 /* find clkdiv yielding a rate <= than requested */
80 for (clkdiv = 0; clkdiv < 255; clkdiv++) {
81 if ((bus_hz / (clkdiv + 1) / 2) <= hz)
84 debug("mci: setting clock %u Hz, block size %u\n",
85 (bus_hz / (clkdiv + 1)) / 2, blklen);
92 mr = MMCI_BF(CLKDIV, clkdiv);
94 /* MCI IP version >= 0x200 has R/WPROOF */
96 mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF);
99 * MCI IP version >= 0x500 use bit 16 as clkodd.
100 * MCI IP version < 0x500 use upper 16 bits for blklen.
102 if (version >= 0x500)
103 mr |= MMCI_BF(CLKODD, clkodd);
105 mr |= MMCI_BF(BLKLEN, blklen);
107 writel(mr, &mci->mr);
109 /* MCI IP version >= 0x200 has blkr */
110 if (version >= 0x200)
111 writel(MMCI_BF(BLKLEN, blklen), &mci->blkr);
113 if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS)
114 writel(MMCI_BIT(HSMODE), &mci->cfg);
119 /* Return the CMDR with flags for a given command and data packet */
120 static u32 mci_encode_cmd(
121 struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
125 /* Default Flags for Errors */
126 *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
127 MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
129 /* Default Flags for the Command */
130 cmdr |= MMCI_BIT(MAXLAT);
133 cmdr |= MMCI_BF(TRCMD, 1);
134 if (data->blocks > 1)
135 cmdr |= MMCI_BF(TRTYP, 1);
136 if (data->flags & MMC_DATA_READ)
137 cmdr |= MMCI_BIT(TRDIR);
140 if (cmd->resp_type & MMC_RSP_CRC)
141 *error_flags |= MMCI_BIT(RCRCE);
142 if (cmd->resp_type & MMC_RSP_136)
143 cmdr |= MMCI_BF(RSPTYP, 2);
144 else if (cmd->resp_type & MMC_RSP_BUSY)
145 cmdr |= MMCI_BF(RSPTYP, 3);
146 else if (cmd->resp_type & MMC_RSP_PRESENT)
147 cmdr |= MMCI_BF(RSPTYP, 1);
149 return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
152 /* Entered into function pointer in mci_send_cmd */
153 static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
158 status = readl(&mci->sr);
159 if (status & (error_flags | MMCI_BIT(OVRE)))
161 } while (!(status & MMCI_BIT(RXRDY)));
163 if (status & MMCI_BIT(RXRDY)) {
164 *data = readl(&mci->rdr);
171 /* Entered into function pointer in mci_send_cmd */
172 static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
177 status = readl(&mci->sr);
178 if (status & (error_flags | MMCI_BIT(UNRE)))
180 } while (!(status & MMCI_BIT(TXRDY)));
182 if (status & MMCI_BIT(TXRDY)) {
183 writel(*data, &mci->tdr);
191 * Entered into mmc structure during driver init
193 * Sends a command out on the bus and deals with the block data.
194 * Takes the mmc pointer, a command pointer, and an optional data pointer.
197 mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
199 atmel_mci_t *mci = mmc->priv;
205 puts ("MCI not initialized!\n");
209 /* Figure out the transfer arguments */
210 cmdr = mci_encode_cmd(cmd, data, &error_flags);
212 /* For multi blocks read/write, set the block register */
213 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
214 || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
215 writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len),
218 /* Send the command */
219 writel(cmd->cmdarg, &mci->argr);
220 writel(cmdr, &mci->cmdr);
223 dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
226 /* Wait for the command to complete */
227 while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
229 if ((status & error_flags) & MMCI_BIT(RTOE)) {
230 dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
232 } else if (status & error_flags) {
233 dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
237 /* Copy the response to the response buffer */
238 if (cmd->resp_type & MMC_RSP_136) {
239 cmd->response[0] = readl(&mci->rspr);
240 cmd->response[1] = readl(&mci->rspr1);
241 cmd->response[2] = readl(&mci->rspr2);
242 cmd->response[3] = readl(&mci->rspr3);
244 cmd->response[0] = readl(&mci->rspr);
246 /* transfer all of the blocks */
248 u32 word_count, block_count;
250 u32 sys_blocksize, dummy, i;
252 (atmel_mci_t *mci, u32* data, u32 error_flags);
254 if (data->flags & MMC_DATA_READ) {
255 mci_data_op = mci_data_read;
256 sys_blocksize = mmc->read_bl_len;
257 ioptr = (u32*)data->dest;
259 mci_data_op = mci_data_write;
260 sys_blocksize = mmc->write_bl_len;
261 ioptr = (u32*)data->src;
265 for (block_count = 0;
266 block_count < data->blocks && !status;
270 status = mci_data_op(mci, ioptr, error_flags);
273 } while (!status && word_count < (data->blocksize/4));
275 if (data->flags & MMC_DATA_READ)
277 u32 cnt = word_count * 4;
278 printf("Read Data:\n");
279 print_buffer(0, data->dest + cnt * block_count,
284 if (!status && word_count < (sys_blocksize / 4))
285 printf("filling rest of block...\n");
287 /* fill the rest of a full block */
288 while (!status && word_count < (sys_blocksize / 4)) {
289 status = mci_data_op(mci, &dummy,
294 dump_cmd(cmdr, cmd->cmdarg, status,
295 "Data Transfer Failed");
300 /* Wait for Transfer End */
303 status = readl(&mci->sr);
305 if (status & error_flags) {
306 dump_cmd(cmdr, cmd->cmdarg, status,
311 } while ((status & MMCI_BIT(DTIP)) && i < 10000);
312 if (status & MMCI_BIT(DTIP)) {
313 dump_cmd(cmdr, cmd->cmdarg, status,
314 "XFER DTIP never unset, ignoring");
321 /* Entered into mmc structure during driver init */
322 static void mci_set_ios(struct mmc *mmc)
324 atmel_mci_t *mci = mmc->priv;
325 int bus_width = mmc->bus_width;
326 unsigned int version = atmel_mci_get_version(mci);
329 /* Set the clock speed */
330 mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
333 * set the bus width and select slot for this interface
334 * there is no capability for multiple slots on the same interface yet
336 if ((version & 0xf00) >= 0x300) {
349 writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
351 busw = (bus_width == 4) ? 1 : 0;
353 writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
357 /* Entered into mmc structure during driver init */
358 static int mci_init(struct mmc *mmc)
360 atmel_mci_t *mci = mmc->priv;
362 /* Initialize controller */
363 writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
364 writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
365 writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
366 writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
368 /* This delay can be optimized, but stick with max value */
369 writel(0x7f, &mci->dtor);
370 /* Disable Interrupts */
371 writel(~0UL, &mci->idr);
373 /* Set default clocks and blocklen */
374 mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
379 static const struct mmc_ops atmel_mci_ops = {
380 .send_cmd = mci_send_cmd,
381 .set_ios = mci_set_ios,
386 * This is the only exported function
388 * Call it with the MCI register base address
390 int atmel_mci_init(void *regs)
393 struct mmc_config *cfg;
394 struct atmel_mci *mci;
395 unsigned int version;
397 cfg = malloc(sizeof(*cfg));
400 memset(cfg, 0, sizeof(*cfg));
402 mci = (struct atmel_mci *)regs;
405 cfg->ops = &atmel_mci_ops;
407 /* need to be able to pass these in on a board by board basis */
408 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
409 version = atmel_mci_get_version(mci);
410 if ((version & 0xf00) >= 0x300) {
411 cfg->host_caps = MMC_MODE_8BIT;
412 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
415 cfg->host_caps |= MMC_MODE_4BIT;
418 * min and max frequencies determined by
419 * max and min of clock divider
421 cfg->f_min = get_mci_clk_rate() / (2*256);
422 cfg->f_max = get_mci_clk_rate() / (2*1);
424 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
426 mmc = mmc_create(cfg, regs);
432 /* NOTE: possibly leaking the cfg structure */