3 * Rob Emanuele <rob@emanuele.us>
4 * Reinhard Meyer, EMK Elektronik <reinhard.meyer@emk-elektronik.de>
7 * Copyright (C) 2004-2006 Atmel Corporation
9 * SPDX-License-Identifier: GPL-2.0+
19 #include <linux/errno.h>
20 #include <asm/byteorder.h>
21 #include <asm/arch/clk.h>
22 #include <asm/arch/hardware.h>
23 #include "atmel_mci.h"
25 DECLARE_GLOBAL_DATA_PTR;
27 #ifndef CONFIG_SYS_MMC_CLK_OD
28 # define CONFIG_SYS_MMC_CLK_OD 150000
31 #define MMC_DEFAULT_BLKLEN 512
33 #if defined(CONFIG_ATMEL_MCI_PORTB)
40 struct atmel_mci_plat {
42 struct mmc_config cfg;
43 struct atmel_mci *mci;
47 struct atmel_mci_priv {
49 struct mmc_config cfg;
50 struct atmel_mci *mci;
52 unsigned int initialized:1;
53 unsigned int curr_clk;
59 /* Read Atmel MCI IP version */
60 static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
62 return readl(&mci->version) & 0x00000fff;
66 * Print command and status:
68 * - always when DEBUG is defined
71 static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
73 debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
74 cmdr, cmdr & 0x3F, arg, status, msg);
77 /* Setup for MCI Clock and Block Size */
79 static void mci_set_mode(struct udevice *dev, u32 hz, u32 blklen)
81 struct atmel_mci_plat *plat = dev_get_platdata(dev);
82 struct atmel_mci_priv *priv = dev_get_priv(dev);
83 struct mmc *mmc = &plat->mmc;
84 u32 bus_hz = priv->bus_clk_rate;
85 atmel_mci_t *mci = plat->mci;
87 static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
89 struct atmel_mci_priv *priv = mmc->priv;
90 u32 bus_hz = get_mci_clk_rate();
91 atmel_mci_t *mci = priv->mci;
95 unsigned int version = atmel_mci_get_version(mci);
99 debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
102 if (version >= 0x500) {
103 clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2;
110 debug("mci: setting clock %u Hz, block size %u\n",
111 bus_hz / (clkdiv * 2 + clkodd + 2), blklen);
113 /* find clkdiv yielding a rate <= than requested */
114 for (clkdiv = 0; clkdiv < 255; clkdiv++) {
115 if ((bus_hz / (clkdiv + 1) / 2) <= hz)
118 debug("mci: setting clock %u Hz, block size %u\n",
119 (bus_hz / (clkdiv + 1)) / 2, blklen);
123 if (version >= 0x500)
124 priv->curr_clk = bus_hz / (clkdiv * 2 + clkodd + 2);
126 priv->curr_clk = (bus_hz / (clkdiv + 1)) / 2;
129 mr = MMCI_BF(CLKDIV, clkdiv);
131 /* MCI IP version >= 0x200 has R/WPROOF */
132 if (version >= 0x200)
133 mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF);
136 * MCI IP version >= 0x500 use bit 16 as clkodd.
137 * MCI IP version < 0x500 use upper 16 bits for blklen.
139 if (version >= 0x500)
140 mr |= MMCI_BF(CLKODD, clkodd);
142 mr |= MMCI_BF(BLKLEN, blklen);
144 writel(mr, &mci->mr);
146 /* MCI IP version >= 0x200 has blkr */
147 if (version >= 0x200)
148 writel(MMCI_BF(BLKLEN, blklen), &mci->blkr);
150 if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS)
151 writel(MMCI_BIT(HSMODE), &mci->cfg);
153 priv->initialized = 1;
156 /* Return the CMDR with flags for a given command and data packet */
157 static u32 mci_encode_cmd(
158 struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
162 /* Default Flags for Errors */
163 *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
164 MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
166 /* Default Flags for the Command */
167 cmdr |= MMCI_BIT(MAXLAT);
170 cmdr |= MMCI_BF(TRCMD, 1);
171 if (data->blocks > 1)
172 cmdr |= MMCI_BF(TRTYP, 1);
173 if (data->flags & MMC_DATA_READ)
174 cmdr |= MMCI_BIT(TRDIR);
177 if (cmd->resp_type & MMC_RSP_CRC)
178 *error_flags |= MMCI_BIT(RCRCE);
179 if (cmd->resp_type & MMC_RSP_136)
180 cmdr |= MMCI_BF(RSPTYP, 2);
181 else if (cmd->resp_type & MMC_RSP_BUSY)
182 cmdr |= MMCI_BF(RSPTYP, 3);
183 else if (cmd->resp_type & MMC_RSP_PRESENT)
184 cmdr |= MMCI_BF(RSPTYP, 1);
186 return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
189 /* Entered into function pointer in mci_send_cmd */
190 static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
195 status = readl(&mci->sr);
196 if (status & (error_flags | MMCI_BIT(OVRE)))
198 } while (!(status & MMCI_BIT(RXRDY)));
200 if (status & MMCI_BIT(RXRDY)) {
201 *data = readl(&mci->rdr);
208 /* Entered into function pointer in mci_send_cmd */
209 static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
214 status = readl(&mci->sr);
215 if (status & (error_flags | MMCI_BIT(UNRE)))
217 } while (!(status & MMCI_BIT(TXRDY)));
219 if (status & MMCI_BIT(TXRDY)) {
220 writel(*data, &mci->tdr);
228 * Entered into mmc structure during driver init
230 * Sends a command out on the bus and deals with the block data.
231 * Takes the mmc pointer, a command pointer, and an optional data pointer.
234 static int atmel_mci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
235 struct mmc_data *data)
237 struct atmel_mci_plat *plat = dev_get_platdata(dev);
238 struct atmel_mci_priv *priv = dev_get_priv(dev);
239 struct mmc *mmc = mmc_get_mmc_dev(dev);
240 atmel_mci_t *mci = plat->mci;
243 mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
245 struct atmel_mci_priv *priv = mmc->priv;
246 atmel_mci_t *mci = priv->mci;
252 if (!priv->initialized) {
253 puts ("MCI not initialized!\n");
257 /* Figure out the transfer arguments */
258 cmdr = mci_encode_cmd(cmd, data, &error_flags);
260 /* For multi blocks read/write, set the block register */
261 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
262 || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
263 writel(data->blocks | MMCI_BF(BLKLEN, mmc->read_bl_len),
266 /* Send the command */
267 writel(cmd->cmdarg, &mci->argr);
268 writel(cmdr, &mci->cmdr);
271 dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
274 /* Wait for the command to complete */
275 while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
277 if ((status & error_flags) & MMCI_BIT(RTOE)) {
278 dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
280 } else if (status & error_flags) {
281 dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
285 /* Copy the response to the response buffer */
286 if (cmd->resp_type & MMC_RSP_136) {
287 cmd->response[0] = readl(&mci->rspr);
288 cmd->response[1] = readl(&mci->rspr1);
289 cmd->response[2] = readl(&mci->rspr2);
290 cmd->response[3] = readl(&mci->rspr3);
292 cmd->response[0] = readl(&mci->rspr);
294 /* transfer all of the blocks */
296 u32 word_count, block_count;
298 u32 sys_blocksize, dummy, i;
300 (atmel_mci_t *mci, u32* data, u32 error_flags);
302 if (data->flags & MMC_DATA_READ) {
303 mci_data_op = mci_data_read;
304 sys_blocksize = mmc->read_bl_len;
305 ioptr = (u32*)data->dest;
307 mci_data_op = mci_data_write;
308 sys_blocksize = mmc->write_bl_len;
309 ioptr = (u32*)data->src;
313 for (block_count = 0;
314 block_count < data->blocks && !status;
318 status = mci_data_op(mci, ioptr, error_flags);
321 } while (!status && word_count < (data->blocksize/4));
323 if (data->flags & MMC_DATA_READ)
325 u32 cnt = word_count * 4;
326 printf("Read Data:\n");
327 print_buffer(0, data->dest + cnt * block_count,
332 if (!status && word_count < (sys_blocksize / 4))
333 printf("filling rest of block...\n");
335 /* fill the rest of a full block */
336 while (!status && word_count < (sys_blocksize / 4)) {
337 status = mci_data_op(mci, &dummy,
342 dump_cmd(cmdr, cmd->cmdarg, status,
343 "Data Transfer Failed");
348 /* Wait for Transfer End */
351 status = readl(&mci->sr);
353 if (status & error_flags) {
354 dump_cmd(cmdr, cmd->cmdarg, status,
359 } while ((status & MMCI_BIT(DTIP)) && i < 10000);
360 if (status & MMCI_BIT(DTIP)) {
361 dump_cmd(cmdr, cmd->cmdarg, status,
362 "XFER DTIP never unset, ignoring");
367 * After the switch command, wait for 8 clocks before the next
370 if (cmd->cmdidx == MMC_CMD_SWITCH)
371 udelay(8*1000000 / priv->curr_clk); /* 8 clk in us */
377 static int atmel_mci_set_ios(struct udevice *dev)
379 struct atmel_mci_plat *plat = dev_get_platdata(dev);
380 struct mmc *mmc = mmc_get_mmc_dev(dev);
381 atmel_mci_t *mci = plat->mci;
383 /* Entered into mmc structure during driver init */
384 static int mci_set_ios(struct mmc *mmc)
386 struct atmel_mci_priv *priv = mmc->priv;
387 atmel_mci_t *mci = priv->mci;
389 int bus_width = mmc->bus_width;
390 unsigned int version = atmel_mci_get_version(mci);
393 /* Set the clock speed */
395 mci_set_mode(dev, mmc->clock, MMC_DEFAULT_BLKLEN);
397 mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
401 * set the bus width and select slot for this interface
402 * there is no capability for multiple slots on the same interface yet
404 if ((version & 0xf00) >= 0x300) {
417 writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
419 busw = (bus_width == 4) ? 1 : 0;
421 writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
428 static int atmel_mci_hw_init(struct udevice *dev)
430 struct atmel_mci_plat *plat = dev_get_platdata(dev);
431 atmel_mci_t *mci = plat->mci;
433 /* Entered into mmc structure during driver init */
434 static int mci_init(struct mmc *mmc)
436 struct atmel_mci_priv *priv = mmc->priv;
437 atmel_mci_t *mci = priv->mci;
440 /* Initialize controller */
441 writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
442 writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
443 writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
444 writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
446 /* This delay can be optimized, but stick with max value */
447 writel(0x7f, &mci->dtor);
448 /* Disable Interrupts */
449 writel(~0UL, &mci->idr);
451 /* Set default clocks and blocklen */
453 mci_set_mode(dev, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
455 mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
461 #ifndef CONFIG_DM_MMC
462 static const struct mmc_ops atmel_mci_ops = {
463 .send_cmd = mci_send_cmd,
464 .set_ios = mci_set_ios,
469 * This is the only exported function
471 * Call it with the MCI register base address
473 int atmel_mci_init(void *regs)
476 struct mmc_config *cfg;
477 struct atmel_mci_priv *priv;
478 unsigned int version;
480 priv = calloc(1, sizeof(*priv));
487 cfg->ops = &atmel_mci_ops;
489 priv->mci = (struct atmel_mci *)regs;
490 priv->initialized = 0;
492 /* need to be able to pass these in on a board by board basis */
493 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
494 version = atmel_mci_get_version(priv->mci);
495 if ((version & 0xf00) >= 0x300) {
496 cfg->host_caps = MMC_MODE_8BIT;
497 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
500 cfg->host_caps |= MMC_MODE_4BIT;
503 * min and max frequencies determined by
504 * max and min of clock divider
506 cfg->f_min = get_mci_clk_rate() / (2*256);
507 cfg->f_max = get_mci_clk_rate() / (2*1);
509 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
511 mmc = mmc_create(cfg, priv);
517 /* NOTE: possibly leaking the priv structure */
524 static const struct dm_mmc_ops atmel_mci_mmc_ops = {
525 .send_cmd = atmel_mci_send_cmd,
526 .set_ios = atmel_mci_set_ios,
529 static void atmel_mci_setup_cfg(struct udevice *dev)
531 struct atmel_mci_plat *plat = dev_get_platdata(dev);
532 struct atmel_mci_priv *priv = dev_get_priv(dev);
533 struct mmc_config *cfg;
537 cfg->name = "Atmel mci";
538 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
541 * If the version is above 3.0, the capabilities of the 8-bit
542 * bus width and high speed are supported.
544 version = atmel_mci_get_version(plat->mci);
545 if ((version & 0xf00) >= 0x300) {
546 cfg->host_caps = MMC_MODE_8BIT |
547 MMC_MODE_HS | MMC_MODE_HS_52MHz;
550 cfg->host_caps |= MMC_MODE_4BIT;
551 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
552 cfg->f_min = priv->bus_clk_rate / (2 * 256);
553 cfg->f_max = priv->bus_clk_rate / 2;
556 static int atmel_mci_enable_clk(struct udevice *dev)
558 struct atmel_mci_priv *priv = dev_get_priv(dev);
563 ret = clk_get_by_index(dev, 0, &clk);
569 ret = clk_enable(&clk);
573 clk_rate = clk_get_rate(&clk);
579 priv->bus_clk_rate = clk_rate;
587 static int atmel_mci_probe(struct udevice *dev)
589 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
590 struct atmel_mci_plat *plat = dev_get_platdata(dev);
594 ret = atmel_mci_enable_clk(dev);
598 plat->mci = (struct atmel_mci *)devfdt_get_addr_ptr(dev);
600 atmel_mci_setup_cfg(dev);
603 mmc->cfg = &plat->cfg;
607 atmel_mci_hw_init(dev);
612 static int atmel_mci_bind(struct udevice *dev)
614 struct atmel_mci_plat *plat = dev_get_platdata(dev);
616 return mmc_bind(dev, &plat->mmc, &plat->cfg);
619 static const struct udevice_id atmel_mci_ids[] = {
620 { .compatible = "atmel,hsmci" },
624 U_BOOT_DRIVER(atmel_mci) = {
627 .of_match = atmel_mci_ids,
628 .bind = atmel_mci_bind,
629 .probe = atmel_mci_probe,
630 .platdata_auto_alloc_size = sizeof(struct atmel_mci_plat),
631 .priv_auto_alloc_size = sizeof(struct atmel_mci_priv),
632 .ops = &atmel_mci_mmc_ops,