MMC: Enable DM_MMC for Davinci
[platform/kernel/u-boot.git] / drivers / mmc / davinci_mmc.c
1 // SPDX-License-Identifier: GPL-2.0+
2 /*
3  * Davinci MMC Controller Driver
4  *
5  * Copyright (C) 2010 Texas Instruments Incorporated
6  */
7
8 #include <config.h>
9 #include <common.h>
10 #include <dm.h>
11 #include <errno.h>
12 #include <mmc.h>
13 #include <command.h>
14 #include <part.h>
15 #include <malloc.h>
16 #include <asm/io.h>
17 #include <asm/arch/sdmmc_defs.h>
18
19 #define DAVINCI_MAX_BLOCKS      (32)
20 #define WATCHDOG_COUNT          (100000)
21
22 #define get_val(addr)           REG(addr)
23 #define set_val(addr, val)      REG(addr) = (val)
24 #define set_bit(addr, val)      set_val((addr), (get_val(addr) | (val)))
25 #define clear_bit(addr, val)    set_val((addr), (get_val(addr) & ~(val)))
26
27 #ifdef CONFIG_DM_MMC
28 struct davinci_of_data {
29         const char *name;
30         u8 version;
31 };
32
33 /* Davinci MMC board definitions */
34 struct davinci_mmc_priv {
35         struct davinci_mmc_regs *reg_base;      /* Register base address */
36         uint input_clk;         /* Input clock to MMC controller */
37         uint version;           /* MMC Controller version */
38 };
39
40 struct davinci_mmc_plat
41 {
42         struct mmc_config cfg;
43         struct mmc mmc;
44 };
45 #endif
46
47 /* Set davinci clock prescalar value based on the required clock in HZ */
48 #if !CONFIG_IS_ENABLED(DM_MMC)
49 static void dmmc_set_clock(struct mmc *mmc, uint clock)
50 {
51         struct davinci_mmc *host = mmc->priv;
52 #else
53
54 static void davinci_mmc_set_clock(struct udevice *dev, uint clock)
55 {
56         struct davinci_mmc_priv *host = dev_get_priv(dev);
57         struct mmc *mmc = mmc_get_mmc_dev(dev);
58 #endif
59         struct davinci_mmc_regs *regs = host->reg_base;
60         uint clkrt, sysclk2, act_clock;
61
62         if (clock < mmc->cfg->f_min)
63                 clock = mmc->cfg->f_min;
64         if (clock > mmc->cfg->f_max)
65                 clock = mmc->cfg->f_max;
66
67         set_val(&regs->mmcclk, 0);
68         sysclk2 = host->input_clk;
69         clkrt = (sysclk2 / (2 * clock)) - 1;
70
71         /* Calculate the actual clock for the divider used */
72         act_clock = (sysclk2 / (2 * (clkrt + 1)));
73
74         /* Adjust divider if actual clock exceeds the required clock */
75         if (act_clock > clock)
76                 clkrt++;
77
78         /* check clock divider boundary and correct it */
79         if (clkrt > 0xFF)
80                 clkrt = 0xFF;
81
82         set_val(&regs->mmcclk, (clkrt | MMCCLK_CLKEN));
83 }
84
85 /* Status bit wait loop for MMCST1 */
86 static int
87 dmmc_wait_fifo_status(volatile struct davinci_mmc_regs *regs, uint status)
88 {
89         uint wdog = WATCHDOG_COUNT;
90
91         while (--wdog && ((get_val(&regs->mmcst1) & status) != status))
92                 udelay(10);
93
94         if (!(get_val(&regs->mmcctl) & MMCCTL_WIDTH_4_BIT))
95                 udelay(100);
96
97         if (wdog == 0)
98                 return -ECOMM;
99
100         return 0;
101 }
102
103 /* Busy bit wait loop for MMCST1 */
104 static int dmmc_busy_wait(volatile struct davinci_mmc_regs *regs)
105 {
106         uint wdog = WATCHDOG_COUNT;
107
108         while (--wdog && (get_val(&regs->mmcst1) & MMCST1_BUSY))
109                 udelay(10);
110
111         if (wdog == 0)
112                 return -ECOMM;
113
114         return 0;
115 }
116
117 /* Status bit wait loop for MMCST0 - Checks for error bits as well */
118 static int dmmc_check_status(volatile struct davinci_mmc_regs *regs,
119                 uint *cur_st, uint st_ready, uint st_error)
120 {
121         uint wdog = WATCHDOG_COUNT;
122         uint mmcstatus = *cur_st;
123
124         while (wdog--) {
125                 if (mmcstatus & st_ready) {
126                         *cur_st = mmcstatus;
127                         mmcstatus = get_val(&regs->mmcst1);
128                         return 0;
129                 } else if (mmcstatus & st_error) {
130                         if (mmcstatus & MMCST0_TOUTRS)
131                                 return -ETIMEDOUT;
132                         printf("[ ST0 ERROR %x]\n", mmcstatus);
133                         /*
134                          * Ignore CRC errors as some MMC cards fail to
135                          * initialize on DM365-EVM on the SD1 slot
136                          */
137                         if (mmcstatus & MMCST0_CRCRS)
138                                 return 0;
139                         return -ECOMM;
140                 }
141                 udelay(10);
142
143                 mmcstatus = get_val(&regs->mmcst0);
144         }
145
146         printf("Status %x Timeout ST0:%x ST1:%x\n", st_ready, mmcstatus,
147                         get_val(&regs->mmcst1));
148         return -ECOMM;
149 }
150
151 /*
152  * Sends a command out on the bus.  Takes the device pointer,
153  * a command pointer, and an optional data pointer.
154  */
155 #if !CONFIG_IS_ENABLED(DM_MMC)
156 static int dmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
157 {
158         struct davinci_mmc *host = mmc->priv;
159 #else
160 static int
161 davinci_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, struct mmc_data *data)
162 {
163         struct davinci_mmc_priv *host = dev_get_priv(dev);
164 #endif
165         volatile struct davinci_mmc_regs *regs = host->reg_base;
166         uint mmcstatus, status_rdy, status_err;
167         uint i, cmddata, bytes_left = 0;
168         int fifo_words, fifo_bytes, err;
169         char *data_buf = NULL;
170
171         /* Clear status registers */
172         mmcstatus = get_val(&regs->mmcst0);
173         fifo_words = (host->version == MMC_CTLR_VERSION_2) ? 16 : 8;
174         fifo_bytes = fifo_words << 2;
175
176         /* Wait for any previous busy signal to be cleared */
177         dmmc_busy_wait(regs);
178
179         cmddata = cmd->cmdidx;
180         cmddata |= MMCCMD_PPLEN;
181
182         /* Send init clock for CMD0 */
183         if (cmd->cmdidx == MMC_CMD_GO_IDLE_STATE)
184                 cmddata |= MMCCMD_INITCK;
185
186         switch (cmd->resp_type) {
187         case MMC_RSP_R1b:
188                 cmddata |= MMCCMD_BSYEXP;
189                 /* Fall-through */
190         case MMC_RSP_R1:    /* R1, R1b, R5, R6, R7 */
191                 cmddata |= MMCCMD_RSPFMT_R1567;
192                 break;
193         case MMC_RSP_R2:
194                 cmddata |= MMCCMD_RSPFMT_R2;
195                 break;
196         case MMC_RSP_R3: /* R3, R4 */
197                 cmddata |= MMCCMD_RSPFMT_R3;
198                 break;
199         }
200
201         set_val(&regs->mmcim, 0);
202
203         if (data) {
204                 /* clear previous data transfer if any and set new one */
205                 bytes_left = (data->blocksize * data->blocks);
206
207                 /* Reset FIFO - Always use 32 byte fifo threshold */
208                 set_val(&regs->mmcfifoctl,
209                                 (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
210
211                 if (host->version == MMC_CTLR_VERSION_2)
212                         cmddata |= MMCCMD_DMATRIG;
213
214                 cmddata |= MMCCMD_WDATX;
215                 if (data->flags == MMC_DATA_READ) {
216                         set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
217                 } else if (data->flags == MMC_DATA_WRITE) {
218                         set_val(&regs->mmcfifoctl,
219                                         (MMCFIFOCTL_FIFOLEV |
220                                          MMCFIFOCTL_FIFODIR));
221                         cmddata |= MMCCMD_DTRW;
222                 }
223
224                 set_val(&regs->mmctod, 0xFFFF);
225                 set_val(&regs->mmcnblk, (data->blocks & MMCNBLK_NBLK_MASK));
226                 set_val(&regs->mmcblen, (data->blocksize & MMCBLEN_BLEN_MASK));
227
228                 if (data->flags == MMC_DATA_WRITE) {
229                         uint val;
230                         data_buf = (char *)data->src;
231                         /* For write, fill FIFO with data before issue of CMD */
232                         for (i = 0; (i < fifo_words) && bytes_left; i++) {
233                                 memcpy((char *)&val, data_buf, 4);
234                                 set_val(&regs->mmcdxr, val);
235                                 data_buf += 4;
236                                 bytes_left -= 4;
237                         }
238                 }
239         } else {
240                 set_val(&regs->mmcblen, 0);
241                 set_val(&regs->mmcnblk, 0);
242         }
243
244         set_val(&regs->mmctor, 0x1FFF);
245
246         /* Send the command */
247         set_val(&regs->mmcarghl, cmd->cmdarg);
248         set_val(&regs->mmccmd, cmddata);
249
250         status_rdy = MMCST0_RSPDNE;
251         status_err = (MMCST0_TOUTRS | MMCST0_TOUTRD |
252                         MMCST0_CRCWR | MMCST0_CRCRD);
253         if (cmd->resp_type & MMC_RSP_CRC)
254                 status_err |= MMCST0_CRCRS;
255
256         mmcstatus = get_val(&regs->mmcst0);
257         err = dmmc_check_status(regs, &mmcstatus, status_rdy, status_err);
258         if (err)
259                 return err;
260
261         /* For R1b wait for busy done */
262         if (cmd->resp_type == MMC_RSP_R1b)
263                 dmmc_busy_wait(regs);
264
265         /* Collect response from controller for specific commands */
266         if (mmcstatus & MMCST0_RSPDNE) {
267                 /* Copy the response to the response buffer */
268                 if (cmd->resp_type & MMC_RSP_136) {
269                         cmd->response[0] = get_val(&regs->mmcrsp67);
270                         cmd->response[1] = get_val(&regs->mmcrsp45);
271                         cmd->response[2] = get_val(&regs->mmcrsp23);
272                         cmd->response[3] = get_val(&regs->mmcrsp01);
273                 } else if (cmd->resp_type & MMC_RSP_PRESENT) {
274                         cmd->response[0] = get_val(&regs->mmcrsp67);
275                 }
276         }
277
278         if (data == NULL)
279                 return 0;
280
281         if (data->flags == MMC_DATA_READ) {
282                 /* check for DATDNE along with DRRDY as the controller might
283                  * set the DATDNE without DRRDY for smaller transfers with
284                  * less than FIFO threshold bytes
285                  */
286                 status_rdy = MMCST0_DRRDY | MMCST0_DATDNE;
287                 status_err = MMCST0_TOUTRD | MMCST0_CRCRD;
288                 data_buf = data->dest;
289         } else {
290                 status_rdy = MMCST0_DXRDY | MMCST0_DATDNE;
291                 status_err = MMCST0_CRCWR;
292         }
293
294         /* Wait until all of the blocks are transferred */
295         while (bytes_left) {
296                 err = dmmc_check_status(regs, &mmcstatus, status_rdy,
297                                 status_err);
298                 if (err)
299                         return err;
300
301                 if (data->flags == MMC_DATA_READ) {
302                         /*
303                          * MMC controller sets the Data receive ready bit
304                          * (DRRDY) in MMCST0 even before the entire FIFO is
305                          * full. This results in erratic behavior if we start
306                          * reading the FIFO soon after DRRDY.  Wait for the
307                          * FIFO full bit in MMCST1 for proper FIFO clearing.
308                          */
309                         if (bytes_left > fifo_bytes)
310                                 dmmc_wait_fifo_status(regs, 0x4a);
311                         else if (bytes_left == fifo_bytes) {
312                                 dmmc_wait_fifo_status(regs, 0x40);
313                                 if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD)
314                                         udelay(600);
315                         }
316
317                         for (i = 0; bytes_left && (i < fifo_words); i++) {
318                                 cmddata = get_val(&regs->mmcdrr);
319                                 memcpy(data_buf, (char *)&cmddata, 4);
320                                 data_buf += 4;
321                                 bytes_left -= 4;
322                         }
323                 } else {
324                         /*
325                          * MMC controller sets the Data transmit ready bit
326                          * (DXRDY) in MMCST0 even before the entire FIFO is
327                          * empty. This results in erratic behavior if we start
328                          * writing the FIFO soon after DXRDY.  Wait for the
329                          * FIFO empty bit in MMCST1 for proper FIFO clearing.
330                          */
331                         dmmc_wait_fifo_status(regs, MMCST1_FIFOEMP);
332                         for (i = 0; bytes_left && (i < fifo_words); i++) {
333                                 memcpy((char *)&cmddata, data_buf, 4);
334                                 set_val(&regs->mmcdxr, cmddata);
335                                 data_buf += 4;
336                                 bytes_left -= 4;
337                         }
338                         dmmc_busy_wait(regs);
339                 }
340         }
341
342         err = dmmc_check_status(regs, &mmcstatus, MMCST0_DATDNE, status_err);
343         if (err)
344                 return err;
345
346         return 0;
347 }
348
349 /* Initialize Davinci MMC controller */
350 #if !CONFIG_IS_ENABLED(DM_MMC)
351 static int dmmc_init(struct mmc *mmc)
352 {
353         struct davinci_mmc *host = mmc->priv;
354 #else
355 static int davinci_dm_mmc_init(struct udevice *dev)
356 {
357         struct davinci_mmc_priv *host = dev_get_priv(dev);
358 #endif
359         struct davinci_mmc_regs *regs = host->reg_base;
360
361         /* Clear status registers explicitly - soft reset doesn't clear it
362          * If Uboot is invoked from UBL with SDMMC Support, the status
363          * registers can have uncleared bits
364          */
365         get_val(&regs->mmcst0);
366         get_val(&regs->mmcst1);
367
368         /* Hold software reset */
369         set_bit(&regs->mmcctl, MMCCTL_DATRST);
370         set_bit(&regs->mmcctl, MMCCTL_CMDRST);
371         udelay(10);
372
373         set_val(&regs->mmcclk, 0x0);
374         set_val(&regs->mmctor, 0x1FFF);
375         set_val(&regs->mmctod, 0xFFFF);
376
377         /* Clear software reset */
378         clear_bit(&regs->mmcctl, MMCCTL_DATRST);
379         clear_bit(&regs->mmcctl, MMCCTL_CMDRST);
380
381         udelay(10);
382
383         /* Reset FIFO - Always use the maximum fifo threshold */
384         set_val(&regs->mmcfifoctl, (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
385         set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
386
387         return 0;
388 }
389
390 /* Set buswidth or clock as indicated by the MMC framework */
391 #if !CONFIG_IS_ENABLED(DM_MMC)
392 static int dmmc_set_ios(struct mmc *mmc)
393 {
394         struct davinci_mmc *host = mmc->priv;
395         struct davinci_mmc_regs *regs = host->reg_base;
396 #else
397 static int davinci_mmc_set_ios(struct udevice *dev)
398 {
399         struct mmc *mmc = mmc_get_mmc_dev(dev);
400
401         struct davinci_mmc_priv *host = dev_get_priv(dev);
402         struct davinci_mmc_regs *regs = host->reg_base;
403 #endif
404         /* Set the bus width */
405         if (mmc->bus_width == 4)
406                 set_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
407         else
408                 clear_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
409
410         /* Set clock speed */
411         if (mmc->clock) {
412 #if !CONFIG_IS_ENABLED(DM_MMC)
413                 dmmc_set_clock(mmc, mmc->clock);
414 #else
415                 davinci_mmc_set_clock(dev, mmc->clock);
416 #endif
417         }
418         return 0;
419 }
420
421 #if !CONFIG_IS_ENABLED(DM_MMC)
422 static const struct mmc_ops dmmc_ops = {
423        .send_cmd       = dmmc_send_cmd,
424        .set_ios        = dmmc_set_ios,
425        .init           = dmmc_init,
426 };
427 #else
428 static const struct dm_mmc_ops davinci_mmc_ops = {
429         .send_cmd       = davinci_mmc_send_cmd,
430         .set_ios        = davinci_mmc_set_ios,
431 };
432 #endif
433
434 #if !CONFIG_IS_ENABLED(DM_MMC)
435 /* Called from board_mmc_init during startup. Can be called multiple times
436 * depending on the number of slots available on board and controller
437 */
438 int davinci_mmc_init(bd_t *bis, struct davinci_mmc *host)
439 {
440         host->cfg.name = "davinci";
441         host->cfg.ops = &dmmc_ops;
442         host->cfg.f_min = 200000;
443         host->cfg.f_max = 25000000;
444         host->cfg.voltages = host->voltages;
445         host->cfg.host_caps = host->host_caps;
446
447         host->cfg.b_max = DAVINCI_MAX_BLOCKS;
448
449         mmc_create(&host->cfg, host);
450
451         return 0;
452 }
453 #else
454
455
456 static int davinci_mmc_probe(struct udevice *dev)
457 {
458         struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
459         struct davinci_mmc_plat *plat = dev_get_platdata(dev);
460         struct davinci_mmc_priv *priv = dev_get_priv(dev);
461         struct mmc_config *cfg = &plat->cfg;
462         struct davinci_of_data *data =
463                         (struct davinci_of_data *)dev_get_driver_data(dev);
464         cfg->f_min = 200000;
465         cfg->f_max = 25000000;
466         cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
467         cfg->host_caps = MMC_MODE_4BIT, /* DA850 supports only 4-bit SD/MMC */
468         cfg->b_max = DAVINCI_MAX_BLOCKS;
469
470         if (data) {
471                 cfg->name = data->name;
472                 priv->version = data->version;
473         }
474
475         priv->reg_base = (struct davinci_mmc_regs *)dev_read_addr(dev);
476         priv->input_clk = clk_get(DAVINCI_MMCSD_CLKID);
477
478         upriv->mmc = &plat->mmc;
479
480         return davinci_dm_mmc_init(dev);
481 }
482
483 static int davinci_mmc_bind(struct udevice *dev)
484 {
485         struct davinci_mmc_plat *plat = dev_get_platdata(dev);
486
487         return mmc_bind(dev, &plat->mmc, &plat->cfg);
488 }
489
490
491 const struct davinci_of_data davinci_mmc_host_info[] = {
492         {
493                 .name   = "dm6441-mmc",
494                 .version = MMC_CTLR_VERSION_1,
495         },
496         {
497                 .name   = "da830-mmc",
498                 .version = MMC_CTLR_VERSION_2,
499         },
500         {},
501 };
502
503 static const struct udevice_id davinci_mmc_ids[] = {
504         {
505                 .compatible = "ti,dm6441-mmc",
506                 .data = (ulong) &davinci_mmc_host_info[MMC_CTLR_VERSION_1]
507         },
508         {
509                 .compatible = "ti,da830-mmc",
510                 .data = (ulong) &davinci_mmc_host_info[MMC_CTLR_VERSION_2]
511         },
512         {},
513 };
514
515 U_BOOT_DRIVER(davinci_mmc_drv) = {
516         .name = "davinci_mmc",
517         .id             = UCLASS_MMC,
518         .of_match       = davinci_mmc_ids,
519 #if CONFIG_BLK
520         .bind           = davinci_mmc_bind,
521 #endif
522         .probe = davinci_mmc_probe,
523         .ops = &davinci_mmc_ops,
524         .platdata_auto_alloc_size = sizeof(struct davinci_mmc_plat),
525         .priv_auto_alloc_size = sizeof(struct davinci_mmc_priv),
526 };
527 #endif