1 // SPDX-License-Identifier: GPL-2.0+
8 * Copyright (C) 2004-2006 Atmel Corporation
19 #include <linux/delay.h>
20 #include <linux/errno.h>
21 #include <asm/byteorder.h>
22 #include <asm/arch/clk.h>
23 #include <asm/arch/hardware.h>
24 #include "atmel_mci.h"
26 #ifndef CONFIG_SYS_MMC_CLK_OD
27 # define CONFIG_SYS_MMC_CLK_OD 150000
30 #define MMC_DEFAULT_BLKLEN 512
32 #if defined(CONFIG_ATMEL_MCI_PORTB)
39 struct atmel_mci_plat {
41 struct mmc_config cfg;
42 struct atmel_mci *mci;
46 struct atmel_mci_priv {
48 struct mmc_config cfg;
49 struct atmel_mci *mci;
51 unsigned int initialized:1;
52 unsigned int curr_clk;
58 /* Read Atmel MCI IP version */
59 static unsigned int atmel_mci_get_version(struct atmel_mci *mci)
61 return readl(&mci->version) & 0x00000fff;
65 * Print command and status:
67 * - always when DEBUG is defined
70 static void dump_cmd(u32 cmdr, u32 arg, u32 status, const char* msg)
72 debug("gen_atmel_mci: CMDR %08x (%2u) ARGR %08x (SR: %08x) %s\n",
73 cmdr, cmdr & 0x3F, arg, status, msg);
76 static inline void mci_set_blklen(atmel_mci_t *mci, int blklen)
78 unsigned int version = atmel_mci_get_version(mci);
82 /* MCI IP version >= 0x200 has blkr */
84 writel(MMCI_BFINS(BLKLEN, blklen, readl(&mci->blkr)),
87 writel(MMCI_BFINS(BLKLEN, blklen, readl(&mci->mr)), &mci->mr);
90 /* Setup for MCI Clock and Block Size */
92 static void mci_set_mode(struct udevice *dev, u32 hz, u32 blklen)
94 struct atmel_mci_plat *plat = dev_get_platdata(dev);
95 struct atmel_mci_priv *priv = dev_get_priv(dev);
96 struct mmc *mmc = &plat->mmc;
97 u32 bus_hz = priv->bus_clk_rate;
98 atmel_mci_t *mci = plat->mci;
100 static void mci_set_mode(struct mmc *mmc, u32 hz, u32 blklen)
102 struct atmel_mci_priv *priv = mmc->priv;
103 u32 bus_hz = get_mci_clk_rate();
104 atmel_mci_t *mci = priv->mci;
108 unsigned int version = atmel_mci_get_version(mci);
112 debug("mci: bus_hz is %u, setting clock %u Hz, block size %u\n",
115 if (version >= 0x500) {
116 clkdiv = DIV_ROUND_UP(bus_hz, hz) - 2;
123 debug("mci: setting clock %u Hz, block size %u\n",
124 bus_hz / (clkdiv * 2 + clkodd + 2), blklen);
126 /* find clkdiv yielding a rate <= than requested */
127 for (clkdiv = 0; clkdiv < 255; clkdiv++) {
128 if ((bus_hz / (clkdiv + 1) / 2) <= hz)
131 debug("mci: setting clock %u Hz, block size %u\n",
132 (bus_hz / (clkdiv + 1)) / 2, blklen);
136 if (version >= 0x500)
137 priv->curr_clk = bus_hz / (clkdiv * 2 + clkodd + 2);
139 priv->curr_clk = (bus_hz / (clkdiv + 1)) / 2;
141 mr = MMCI_BF(CLKDIV, clkdiv);
143 /* MCI IP version >= 0x200 has R/WPROOF */
144 if (version >= 0x200)
145 mr |= MMCI_BIT(RDPROOF) | MMCI_BIT(WRPROOF);
148 * MCI IP version >= 0x500 use bit 16 as clkodd.
149 * MCI IP version < 0x500 use upper 16 bits for blklen.
151 if (version >= 0x500)
152 mr |= MMCI_BF(CLKODD, clkodd);
154 writel(mr, &mci->mr);
156 mci_set_blklen(mci, blklen);
158 if (mmc->card_caps & mmc->cfg->host_caps & MMC_MODE_HS)
159 writel(MMCI_BIT(HSMODE), &mci->cfg);
161 priv->initialized = 1;
164 /* Return the CMDR with flags for a given command and data packet */
165 static u32 mci_encode_cmd(
166 struct mmc_cmd *cmd, struct mmc_data *data, u32* error_flags)
170 /* Default Flags for Errors */
171 *error_flags |= (MMCI_BIT(DTOE) | MMCI_BIT(RDIRE) | MMCI_BIT(RENDE) |
172 MMCI_BIT(RINDE) | MMCI_BIT(RTOE));
174 /* Default Flags for the Command */
175 cmdr |= MMCI_BIT(MAXLAT);
178 cmdr |= MMCI_BF(TRCMD, 1);
179 if (data->blocks > 1)
180 cmdr |= MMCI_BF(TRTYP, 1);
181 if (data->flags & MMC_DATA_READ)
182 cmdr |= MMCI_BIT(TRDIR);
185 if (cmd->resp_type & MMC_RSP_CRC)
186 *error_flags |= MMCI_BIT(RCRCE);
187 if (cmd->resp_type & MMC_RSP_136)
188 cmdr |= MMCI_BF(RSPTYP, 2);
189 else if (cmd->resp_type & MMC_RSP_BUSY)
190 cmdr |= MMCI_BF(RSPTYP, 3);
191 else if (cmd->resp_type & MMC_RSP_PRESENT)
192 cmdr |= MMCI_BF(RSPTYP, 1);
194 return cmdr | MMCI_BF(CMDNB, cmd->cmdidx);
197 /* Entered into function pointer in mci_send_cmd */
198 static u32 mci_data_read(atmel_mci_t *mci, u32* data, u32 error_flags)
203 status = readl(&mci->sr);
204 if (status & (error_flags | MMCI_BIT(OVRE)))
206 } while (!(status & MMCI_BIT(RXRDY)));
208 if (status & MMCI_BIT(RXRDY)) {
209 *data = readl(&mci->rdr);
216 /* Entered into function pointer in mci_send_cmd */
217 static u32 mci_data_write(atmel_mci_t *mci, u32* data, u32 error_flags)
222 status = readl(&mci->sr);
223 if (status & (error_flags | MMCI_BIT(UNRE)))
225 } while (!(status & MMCI_BIT(TXRDY)));
227 if (status & MMCI_BIT(TXRDY)) {
228 writel(*data, &mci->tdr);
236 * Entered into mmc structure during driver init
238 * Sends a command out on the bus and deals with the block data.
239 * Takes the mmc pointer, a command pointer, and an optional data pointer.
242 static int atmel_mci_send_cmd(struct udevice *dev, struct mmc_cmd *cmd,
243 struct mmc_data *data)
245 struct atmel_mci_plat *plat = dev_get_platdata(dev);
246 struct atmel_mci_priv *priv = dev_get_priv(dev);
247 atmel_mci_t *mci = plat->mci;
250 mci_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
252 struct atmel_mci_priv *priv = mmc->priv;
253 atmel_mci_t *mci = priv->mci;
259 if (!priv->initialized) {
260 puts ("MCI not initialized!\n");
264 /* Figure out the transfer arguments */
265 cmdr = mci_encode_cmd(cmd, data, &error_flags);
267 mci_set_blklen(mci, data->blocksize);
269 /* For multi blocks read/write, set the block register */
270 if ((cmd->cmdidx == MMC_CMD_READ_MULTIPLE_BLOCK)
271 || (cmd->cmdidx == MMC_CMD_WRITE_MULTIPLE_BLOCK))
272 writel(data->blocks | MMCI_BF(BLKLEN, data->blocksize),
275 /* Send the command */
276 writel(cmd->cmdarg, &mci->argr);
277 writel(cmdr, &mci->cmdr);
280 dump_cmd(cmdr, cmd->cmdarg, 0, "DEBUG");
283 /* Wait for the command to complete */
284 while (!((status = readl(&mci->sr)) & MMCI_BIT(CMDRDY)));
286 if ((status & error_flags) & MMCI_BIT(RTOE)) {
287 dump_cmd(cmdr, cmd->cmdarg, status, "Command Time Out");
289 } else if (status & error_flags) {
290 dump_cmd(cmdr, cmd->cmdarg, status, "Command Failed");
294 /* Copy the response to the response buffer */
295 if (cmd->resp_type & MMC_RSP_136) {
296 cmd->response[0] = readl(&mci->rspr);
297 cmd->response[1] = readl(&mci->rspr1);
298 cmd->response[2] = readl(&mci->rspr2);
299 cmd->response[3] = readl(&mci->rspr3);
301 cmd->response[0] = readl(&mci->rspr);
303 /* transfer all of the blocks */
305 u32 word_count, block_count;
309 (atmel_mci_t *mci, u32* data, u32 error_flags);
311 if (data->flags & MMC_DATA_READ) {
312 mci_data_op = mci_data_read;
313 ioptr = (u32*)data->dest;
315 mci_data_op = mci_data_write;
316 ioptr = (u32*)data->src;
320 for (block_count = 0;
321 block_count < data->blocks && !status;
325 status = mci_data_op(mci, ioptr, error_flags);
328 } while (!status && word_count < (data->blocksize/4));
330 if (data->flags & MMC_DATA_READ)
332 u32 cnt = word_count * 4;
333 printf("Read Data:\n");
334 print_buffer(0, data->dest + cnt * block_count,
339 dump_cmd(cmdr, cmd->cmdarg, status,
340 "Data Transfer Failed");
345 /* Wait for Transfer End */
348 status = readl(&mci->sr);
350 if (status & error_flags) {
351 dump_cmd(cmdr, cmd->cmdarg, status,
356 } while ((status & MMCI_BIT(DTIP)) && i < 10000);
357 if (status & MMCI_BIT(DTIP)) {
358 dump_cmd(cmdr, cmd->cmdarg, status,
359 "XFER DTIP never unset, ignoring");
364 * After the switch command, wait for 8 clocks before the next
367 if (cmd->cmdidx == MMC_CMD_SWITCH)
368 udelay(8*1000000 / priv->curr_clk); /* 8 clk in us */
374 static int atmel_mci_set_ios(struct udevice *dev)
376 struct atmel_mci_plat *plat = dev_get_platdata(dev);
377 struct mmc *mmc = mmc_get_mmc_dev(dev);
378 atmel_mci_t *mci = plat->mci;
380 /* Entered into mmc structure during driver init */
381 static int mci_set_ios(struct mmc *mmc)
383 struct atmel_mci_priv *priv = mmc->priv;
384 atmel_mci_t *mci = priv->mci;
386 int bus_width = mmc->bus_width;
387 unsigned int version = atmel_mci_get_version(mci);
390 /* Set the clock speed */
392 mci_set_mode(dev, mmc->clock, MMC_DEFAULT_BLKLEN);
394 mci_set_mode(mmc, mmc->clock, MMC_DEFAULT_BLKLEN);
398 * set the bus width and select slot for this interface
399 * there is no capability for multiple slots on the same interface yet
401 if ((version & 0xf00) >= 0x300) {
414 writel(busw << 6 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
416 busw = (bus_width == 4) ? 1 : 0;
418 writel(busw << 7 | MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr);
425 static int atmel_mci_hw_init(struct udevice *dev)
427 struct atmel_mci_plat *plat = dev_get_platdata(dev);
428 atmel_mci_t *mci = plat->mci;
430 /* Entered into mmc structure during driver init */
431 static int mci_init(struct mmc *mmc)
433 struct atmel_mci_priv *priv = mmc->priv;
434 atmel_mci_t *mci = priv->mci;
437 /* Initialize controller */
438 writel(MMCI_BIT(SWRST), &mci->cr); /* soft reset */
439 writel(MMCI_BIT(PWSDIS), &mci->cr); /* disable power save */
440 writel(MMCI_BIT(MCIEN), &mci->cr); /* enable mci */
441 writel(MMCI_BF(SCDSEL, MCI_BUS), &mci->sdcr); /* select port */
443 /* This delay can be optimized, but stick with max value */
444 writel(0x7f, &mci->dtor);
445 /* Disable Interrupts */
446 writel(~0UL, &mci->idr);
448 /* Set default clocks and blocklen */
450 mci_set_mode(dev, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
452 mci_set_mode(mmc, CONFIG_SYS_MMC_CLK_OD, MMC_DEFAULT_BLKLEN);
458 #ifndef CONFIG_DM_MMC
459 static const struct mmc_ops atmel_mci_ops = {
460 .send_cmd = mci_send_cmd,
461 .set_ios = mci_set_ios,
466 * This is the only exported function
468 * Call it with the MCI register base address
470 int atmel_mci_init(void *regs)
473 struct mmc_config *cfg;
474 struct atmel_mci_priv *priv;
475 unsigned int version;
477 priv = calloc(1, sizeof(*priv));
484 cfg->ops = &atmel_mci_ops;
486 priv->mci = (struct atmel_mci *)regs;
487 priv->initialized = 0;
489 /* need to be able to pass these in on a board by board basis */
490 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
491 version = atmel_mci_get_version(priv->mci);
492 if ((version & 0xf00) >= 0x300) {
493 cfg->host_caps = MMC_MODE_8BIT;
494 cfg->host_caps |= MMC_MODE_HS | MMC_MODE_HS_52MHz;
497 cfg->host_caps |= MMC_MODE_4BIT;
500 * min and max frequencies determined by
501 * max and min of clock divider
503 cfg->f_min = get_mci_clk_rate() / (2*256);
504 cfg->f_max = get_mci_clk_rate() / (2*1);
506 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
508 mmc = mmc_create(cfg, priv);
514 /* NOTE: possibly leaking the priv structure */
521 static const struct dm_mmc_ops atmel_mci_mmc_ops = {
522 .send_cmd = atmel_mci_send_cmd,
523 .set_ios = atmel_mci_set_ios,
526 static void atmel_mci_setup_cfg(struct udevice *dev)
528 struct atmel_mci_plat *plat = dev_get_platdata(dev);
529 struct atmel_mci_priv *priv = dev_get_priv(dev);
530 struct mmc_config *cfg;
534 cfg->name = "Atmel mci";
535 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34;
538 * If the version is above 3.0, the capabilities of the 8-bit
539 * bus width and high speed are supported.
541 version = atmel_mci_get_version(plat->mci);
542 if ((version & 0xf00) >= 0x300) {
543 cfg->host_caps = MMC_MODE_8BIT |
544 MMC_MODE_HS | MMC_MODE_HS_52MHz;
547 cfg->host_caps |= MMC_MODE_4BIT;
548 cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT;
549 cfg->f_min = priv->bus_clk_rate / (2 * 256);
550 cfg->f_max = priv->bus_clk_rate / 2;
553 static int atmel_mci_enable_clk(struct udevice *dev)
555 struct atmel_mci_priv *priv = dev_get_priv(dev);
560 ret = clk_get_by_index(dev, 0, &clk);
566 ret = clk_enable(&clk);
570 clk_rate = clk_get_rate(&clk);
576 priv->bus_clk_rate = clk_rate;
584 static int atmel_mci_probe(struct udevice *dev)
586 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
587 struct atmel_mci_plat *plat = dev_get_platdata(dev);
591 ret = atmel_mci_enable_clk(dev);
595 plat->mci = (struct atmel_mci *)devfdt_get_addr_ptr(dev);
597 atmel_mci_setup_cfg(dev);
600 mmc->cfg = &plat->cfg;
604 atmel_mci_hw_init(dev);
609 static int atmel_mci_bind(struct udevice *dev)
611 struct atmel_mci_plat *plat = dev_get_platdata(dev);
613 return mmc_bind(dev, &plat->mmc, &plat->cfg);
616 static const struct udevice_id atmel_mci_ids[] = {
617 { .compatible = "atmel,hsmci" },
621 U_BOOT_DRIVER(atmel_mci) = {
624 .of_match = atmel_mci_ids,
625 .bind = atmel_mci_bind,
626 .probe = atmel_mci_probe,
627 .platdata_auto_alloc_size = sizeof(struct atmel_mci_plat),
628 .priv_auto_alloc_size = sizeof(struct atmel_mci_priv),
629 .ops = &atmel_mci_mmc_ops,