1 // SPDX-License-Identifier: GPL-2.0+
3 * Davinci MMC Controller Driver
5 * Copyright (C) 2010 Texas Instruments Incorporated
17 #include <asm/arch/sdmmc_defs.h>
18 #include <asm-generic/gpio.h>
20 #define DAVINCI_MAX_BLOCKS (32)
21 #define WATCHDOG_COUNT (100000)
23 #define get_val(addr) REG(addr)
24 #define set_val(addr, val) REG(addr) = (val)
25 #define set_bit(addr, val) set_val((addr), (get_val(addr) | (val)))
26 #define clear_bit(addr, val) set_val((addr), (get_val(addr) & ~(val)))
29 struct davinci_of_data {
34 /* Davinci MMC board definitions */
35 struct davinci_mmc_priv {
36 struct davinci_mmc_regs *reg_base; /* Register base address */
37 uint input_clk; /* Input clock to MMC controller */
38 uint version; /* MMC Controller version */
39 struct gpio_desc cd_gpio; /* Card Detect GPIO */
40 struct gpio_desc wp_gpio; /* Write Protect GPIO */
43 struct davinci_mmc_plat
45 struct mmc_config cfg;
50 /* Set davinci clock prescalar value based on the required clock in HZ */
51 #if !CONFIG_IS_ENABLED(DM_MMC)
52 static void dmmc_set_clock(struct mmc *mmc, uint clock)
54 struct davinci_mmc *host = mmc->priv;
57 static void davinci_mmc_set_clock(struct udevice *dev, uint clock)
59 struct davinci_mmc_priv *host = dev_get_priv(dev);
60 struct mmc *mmc = mmc_get_mmc_dev(dev);
62 struct davinci_mmc_regs *regs = host->reg_base;
63 uint clkrt, sysclk2, act_clock;
65 if (clock < mmc->cfg->f_min)
66 clock = mmc->cfg->f_min;
67 if (clock > mmc->cfg->f_max)
68 clock = mmc->cfg->f_max;
70 set_val(®s->mmcclk, 0);
71 sysclk2 = host->input_clk;
72 clkrt = (sysclk2 / (2 * clock)) - 1;
74 /* Calculate the actual clock for the divider used */
75 act_clock = (sysclk2 / (2 * (clkrt + 1)));
77 /* Adjust divider if actual clock exceeds the required clock */
78 if (act_clock > clock)
81 /* check clock divider boundary and correct it */
85 set_val(®s->mmcclk, (clkrt | MMCCLK_CLKEN));
88 /* Status bit wait loop for MMCST1 */
90 dmmc_wait_fifo_status(volatile struct davinci_mmc_regs *regs, uint status)
92 uint wdog = WATCHDOG_COUNT;
94 while (--wdog && ((get_val(®s->mmcst1) & status) != status))
97 if (!(get_val(®s->mmcctl) & MMCCTL_WIDTH_4_BIT))
106 /* Busy bit wait loop for MMCST1 */
107 static int dmmc_busy_wait(volatile struct davinci_mmc_regs *regs)
109 uint wdog = WATCHDOG_COUNT;
111 while (--wdog && (get_val(®s->mmcst1) & MMCST1_BUSY))
120 /* Status bit wait loop for MMCST0 - Checks for error bits as well */
121 static int dmmc_check_status(volatile struct davinci_mmc_regs *regs,
122 uint *cur_st, uint st_ready, uint st_error)
124 uint wdog = WATCHDOG_COUNT;
125 uint mmcstatus = *cur_st;
128 if (mmcstatus & st_ready) {
130 mmcstatus = get_val(®s->mmcst1);
132 } else if (mmcstatus & st_error) {
133 if (mmcstatus & MMCST0_TOUTRS)
135 printf("[ ST0 ERROR %x]\n", mmcstatus);
137 * Ignore CRC errors as some MMC cards fail to
138 * initialize on DM365-EVM on the SD1 slot
140 if (mmcstatus & MMCST0_CRCRS)
146 mmcstatus = get_val(®s->mmcst0);
149 printf("Status %x Timeout ST0:%x ST1:%x\n", st_ready, mmcstatus,
150 get_val(®s->mmcst1));
155 * Sends a command out on the bus. Takes the device pointer,
156 * a command pointer, and an optional data pointer.
158 #if !CONFIG_IS_ENABLED(DM_MMC)
159 static int dmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
161 struct davinci_mmc *host = mmc->priv;
164 davinci_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, struct mmc_data *data)
166 struct davinci_mmc_priv *host = dev_get_priv(dev);
168 volatile struct davinci_mmc_regs *regs = host->reg_base;
169 uint mmcstatus, status_rdy, status_err;
170 uint i, cmddata, bytes_left = 0;
171 int fifo_words, fifo_bytes, err;
172 char *data_buf = NULL;
174 /* Clear status registers */
175 mmcstatus = get_val(®s->mmcst0);
176 fifo_words = (host->version == MMC_CTLR_VERSION_2) ? 16 : 8;
177 fifo_bytes = fifo_words << 2;
179 /* Wait for any previous busy signal to be cleared */
180 dmmc_busy_wait(regs);
182 cmddata = cmd->cmdidx;
183 cmddata |= MMCCMD_PPLEN;
185 /* Send init clock for CMD0 */
186 if (cmd->cmdidx == MMC_CMD_GO_IDLE_STATE)
187 cmddata |= MMCCMD_INITCK;
189 switch (cmd->resp_type) {
191 cmddata |= MMCCMD_BSYEXP;
193 case MMC_RSP_R1: /* R1, R1b, R5, R6, R7 */
194 cmddata |= MMCCMD_RSPFMT_R1567;
197 cmddata |= MMCCMD_RSPFMT_R2;
199 case MMC_RSP_R3: /* R3, R4 */
200 cmddata |= MMCCMD_RSPFMT_R3;
204 set_val(®s->mmcim, 0);
207 /* clear previous data transfer if any and set new one */
208 bytes_left = (data->blocksize * data->blocks);
210 /* Reset FIFO - Always use 32 byte fifo threshold */
211 set_val(®s->mmcfifoctl,
212 (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
214 if (host->version == MMC_CTLR_VERSION_2)
215 cmddata |= MMCCMD_DMATRIG;
217 cmddata |= MMCCMD_WDATX;
218 if (data->flags == MMC_DATA_READ) {
219 set_val(®s->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
220 } else if (data->flags == MMC_DATA_WRITE) {
221 set_val(®s->mmcfifoctl,
222 (MMCFIFOCTL_FIFOLEV |
223 MMCFIFOCTL_FIFODIR));
224 cmddata |= MMCCMD_DTRW;
227 set_val(®s->mmctod, 0xFFFF);
228 set_val(®s->mmcnblk, (data->blocks & MMCNBLK_NBLK_MASK));
229 set_val(®s->mmcblen, (data->blocksize & MMCBLEN_BLEN_MASK));
231 if (data->flags == MMC_DATA_WRITE) {
233 data_buf = (char *)data->src;
234 /* For write, fill FIFO with data before issue of CMD */
235 for (i = 0; (i < fifo_words) && bytes_left; i++) {
236 memcpy((char *)&val, data_buf, 4);
237 set_val(®s->mmcdxr, val);
243 set_val(®s->mmcblen, 0);
244 set_val(®s->mmcnblk, 0);
247 set_val(®s->mmctor, 0x1FFF);
249 /* Send the command */
250 set_val(®s->mmcarghl, cmd->cmdarg);
251 set_val(®s->mmccmd, cmddata);
253 status_rdy = MMCST0_RSPDNE;
254 status_err = (MMCST0_TOUTRS | MMCST0_TOUTRD |
255 MMCST0_CRCWR | MMCST0_CRCRD);
256 if (cmd->resp_type & MMC_RSP_CRC)
257 status_err |= MMCST0_CRCRS;
259 mmcstatus = get_val(®s->mmcst0);
260 err = dmmc_check_status(regs, &mmcstatus, status_rdy, status_err);
264 /* For R1b wait for busy done */
265 if (cmd->resp_type == MMC_RSP_R1b)
266 dmmc_busy_wait(regs);
268 /* Collect response from controller for specific commands */
269 if (mmcstatus & MMCST0_RSPDNE) {
270 /* Copy the response to the response buffer */
271 if (cmd->resp_type & MMC_RSP_136) {
272 cmd->response[0] = get_val(®s->mmcrsp67);
273 cmd->response[1] = get_val(®s->mmcrsp45);
274 cmd->response[2] = get_val(®s->mmcrsp23);
275 cmd->response[3] = get_val(®s->mmcrsp01);
276 } else if (cmd->resp_type & MMC_RSP_PRESENT) {
277 cmd->response[0] = get_val(®s->mmcrsp67);
284 if (data->flags == MMC_DATA_READ) {
285 /* check for DATDNE along with DRRDY as the controller might
286 * set the DATDNE without DRRDY for smaller transfers with
287 * less than FIFO threshold bytes
289 status_rdy = MMCST0_DRRDY | MMCST0_DATDNE;
290 status_err = MMCST0_TOUTRD | MMCST0_CRCRD;
291 data_buf = data->dest;
293 status_rdy = MMCST0_DXRDY | MMCST0_DATDNE;
294 status_err = MMCST0_CRCWR;
297 /* Wait until all of the blocks are transferred */
299 err = dmmc_check_status(regs, &mmcstatus, status_rdy,
304 if (data->flags == MMC_DATA_READ) {
306 * MMC controller sets the Data receive ready bit
307 * (DRRDY) in MMCST0 even before the entire FIFO is
308 * full. This results in erratic behavior if we start
309 * reading the FIFO soon after DRRDY. Wait for the
310 * FIFO full bit in MMCST1 for proper FIFO clearing.
312 if (bytes_left > fifo_bytes)
313 dmmc_wait_fifo_status(regs, 0x4a);
314 else if (bytes_left == fifo_bytes) {
315 dmmc_wait_fifo_status(regs, 0x40);
316 if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD)
320 for (i = 0; bytes_left && (i < fifo_words); i++) {
321 cmddata = get_val(®s->mmcdrr);
322 memcpy(data_buf, (char *)&cmddata, 4);
328 * MMC controller sets the Data transmit ready bit
329 * (DXRDY) in MMCST0 even before the entire FIFO is
330 * empty. This results in erratic behavior if we start
331 * writing the FIFO soon after DXRDY. Wait for the
332 * FIFO empty bit in MMCST1 for proper FIFO clearing.
334 dmmc_wait_fifo_status(regs, MMCST1_FIFOEMP);
335 for (i = 0; bytes_left && (i < fifo_words); i++) {
336 memcpy((char *)&cmddata, data_buf, 4);
337 set_val(®s->mmcdxr, cmddata);
341 dmmc_busy_wait(regs);
345 err = dmmc_check_status(regs, &mmcstatus, MMCST0_DATDNE, status_err);
352 /* Initialize Davinci MMC controller */
353 #if !CONFIG_IS_ENABLED(DM_MMC)
354 static int dmmc_init(struct mmc *mmc)
356 struct davinci_mmc *host = mmc->priv;
358 static int davinci_dm_mmc_init(struct udevice *dev)
360 struct davinci_mmc_priv *host = dev_get_priv(dev);
362 struct davinci_mmc_regs *regs = host->reg_base;
364 /* Clear status registers explicitly - soft reset doesn't clear it
365 * If Uboot is invoked from UBL with SDMMC Support, the status
366 * registers can have uncleared bits
368 get_val(®s->mmcst0);
369 get_val(®s->mmcst1);
371 /* Hold software reset */
372 set_bit(®s->mmcctl, MMCCTL_DATRST);
373 set_bit(®s->mmcctl, MMCCTL_CMDRST);
376 set_val(®s->mmcclk, 0x0);
377 set_val(®s->mmctor, 0x1FFF);
378 set_val(®s->mmctod, 0xFFFF);
380 /* Clear software reset */
381 clear_bit(®s->mmcctl, MMCCTL_DATRST);
382 clear_bit(®s->mmcctl, MMCCTL_CMDRST);
386 /* Reset FIFO - Always use the maximum fifo threshold */
387 set_val(®s->mmcfifoctl, (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
388 set_val(®s->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
393 /* Set buswidth or clock as indicated by the MMC framework */
394 #if !CONFIG_IS_ENABLED(DM_MMC)
395 static int dmmc_set_ios(struct mmc *mmc)
397 struct davinci_mmc *host = mmc->priv;
398 struct davinci_mmc_regs *regs = host->reg_base;
400 static int davinci_mmc_set_ios(struct udevice *dev)
402 struct mmc *mmc = mmc_get_mmc_dev(dev);
404 struct davinci_mmc_priv *host = dev_get_priv(dev);
405 struct davinci_mmc_regs *regs = host->reg_base;
407 /* Set the bus width */
408 if (mmc->bus_width == 4)
409 set_bit(®s->mmcctl, MMCCTL_WIDTH_4_BIT);
411 clear_bit(®s->mmcctl, MMCCTL_WIDTH_4_BIT);
413 /* Set clock speed */
415 #if !CONFIG_IS_ENABLED(DM_MMC)
416 dmmc_set_clock(mmc, mmc->clock);
418 davinci_mmc_set_clock(dev, mmc->clock);
424 #if !CONFIG_IS_ENABLED(DM_MMC)
425 static const struct mmc_ops dmmc_ops = {
426 .send_cmd = dmmc_send_cmd,
427 .set_ios = dmmc_set_ios,
432 static int davinci_mmc_getcd(struct udevice *dev)
435 #if CONFIG_IS_ENABLED(DM_GPIO)
436 struct davinci_mmc_priv *priv = dev_get_priv(dev);
437 value = dm_gpio_get_value(&priv->cd_gpio);
439 /* if no CD return as 1 */
446 static int davinci_mmc_getwp(struct udevice *dev)
449 #if CONFIG_IS_ENABLED(DM_GPIO)
450 struct davinci_mmc_priv *priv = dev_get_priv(dev);
452 value = dm_gpio_get_value(&priv->wp_gpio);
454 /* if no WP return as 0 */
461 static const struct dm_mmc_ops davinci_mmc_ops = {
462 .send_cmd = davinci_mmc_send_cmd,
463 .set_ios = davinci_mmc_set_ios,
464 .get_cd = davinci_mmc_getcd,
465 .get_wp = davinci_mmc_getwp,
469 #if !CONFIG_IS_ENABLED(DM_MMC)
470 /* Called from board_mmc_init during startup. Can be called multiple times
471 * depending on the number of slots available on board and controller
473 int davinci_mmc_init(bd_t *bis, struct davinci_mmc *host)
475 host->cfg.name = "davinci";
476 host->cfg.ops = &dmmc_ops;
477 host->cfg.f_min = 200000;
478 host->cfg.f_max = 25000000;
479 host->cfg.voltages = host->voltages;
480 host->cfg.host_caps = host->host_caps;
482 host->cfg.b_max = DAVINCI_MAX_BLOCKS;
484 mmc_create(&host->cfg, host);
491 static int davinci_mmc_probe(struct udevice *dev)
493 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
494 struct davinci_mmc_plat *plat = dev_get_platdata(dev);
495 struct davinci_mmc_priv *priv = dev_get_priv(dev);
496 struct mmc_config *cfg = &plat->cfg;
497 struct davinci_of_data *data =
498 (struct davinci_of_data *)dev_get_driver_data(dev);
500 cfg->f_max = 25000000;
501 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
502 cfg->host_caps = MMC_MODE_4BIT, /* DA850 supports only 4-bit SD/MMC */
503 cfg->b_max = DAVINCI_MAX_BLOCKS;
506 cfg->name = data->name;
507 priv->version = data->version;
510 priv->reg_base = (struct davinci_mmc_regs *)dev_read_addr(dev);
511 priv->input_clk = clk_get(DAVINCI_MMCSD_CLKID);
513 #if CONFIG_IS_ENABLED(DM_GPIO)
514 /* These GPIOs are optional */
515 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
516 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
519 upriv->mmc = &plat->mmc;
521 return davinci_dm_mmc_init(dev);
524 static int davinci_mmc_bind(struct udevice *dev)
526 struct davinci_mmc_plat *plat = dev_get_platdata(dev);
528 return mmc_bind(dev, &plat->mmc, &plat->cfg);
532 const struct davinci_of_data davinci_mmc_host_info[] = {
534 .name = "dm6441-mmc",
535 .version = MMC_CTLR_VERSION_1,
539 .version = MMC_CTLR_VERSION_2,
544 static const struct udevice_id davinci_mmc_ids[] = {
546 .compatible = "ti,dm6441-mmc",
547 .data = (ulong) &davinci_mmc_host_info[MMC_CTLR_VERSION_1]
550 .compatible = "ti,da830-mmc",
551 .data = (ulong) &davinci_mmc_host_info[MMC_CTLR_VERSION_2]
556 U_BOOT_DRIVER(davinci_mmc_drv) = {
557 .name = "davinci_mmc",
559 .of_match = davinci_mmc_ids,
561 .bind = davinci_mmc_bind,
563 .probe = davinci_mmc_probe,
564 .ops = &davinci_mmc_ops,
565 .platdata_auto_alloc_size = sizeof(struct davinci_mmc_plat),
566 .priv_auto_alloc_size = sizeof(struct davinci_mmc_priv),