blob: c3f7b57665d3a2954992783e474e7728d7d74015 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Sandeep Paulraj50347172010-12-20 20:01:21 -05002/*
3 * Davinci MMC Controller Driver
4 *
5 * Copyright (C) 2010 Texas Instruments Incorporated
Sandeep Paulraj50347172010-12-20 20:01:21 -05006 */
7
8#include <config.h>
9#include <common.h>
Adam Ford8ce7bdd2018-08-09 06:15:12 -050010#include <dm.h>
Jaehoon Chung7825d202016-07-19 16:33:36 +090011#include <errno.h>
Sandeep Paulraj50347172010-12-20 20:01:21 -050012#include <mmc.h>
Adam Ford8ce7bdd2018-08-09 06:15:12 -050013#include <command.h>
Sandeep Paulraj50347172010-12-20 20:01:21 -050014#include <part.h>
15#include <malloc.h>
16#include <asm/io.h>
17#include <asm/arch/sdmmc_defs.h>
Adam Ford7d9bdcc2018-09-03 03:47:52 -050018#include <asm-generic/gpio.h>
Sandeep Paulraj50347172010-12-20 20:01:21 -050019
20#define DAVINCI_MAX_BLOCKS (32)
21#define WATCHDOG_COUNT (100000)
22
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)))
27
Adam Ford8ce7bdd2018-08-09 06:15:12 -050028#ifdef CONFIG_DM_MMC
Adam Ford8ce7bdd2018-08-09 06:15:12 -050029/* Davinci MMC board definitions */
30struct davinci_mmc_priv {
31 struct davinci_mmc_regs *reg_base; /* Register base address */
32 uint input_clk; /* Input clock to MMC controller */
Adam Ford7d9bdcc2018-09-03 03:47:52 -050033 struct gpio_desc cd_gpio; /* Card Detect GPIO */
34 struct gpio_desc wp_gpio; /* Write Protect GPIO */
Adam Ford8ce7bdd2018-08-09 06:15:12 -050035 struct mmc_config cfg;
36 struct mmc mmc;
37};
38#endif
39
Sandeep Paulraj50347172010-12-20 20:01:21 -050040/* Set davinci clock prescalar value based on the required clock in HZ */
Adam Ford8ce7bdd2018-08-09 06:15:12 -050041#if !CONFIG_IS_ENABLED(DM_MMC)
Sandeep Paulraj50347172010-12-20 20:01:21 -050042static void dmmc_set_clock(struct mmc *mmc, uint clock)
43{
44 struct davinci_mmc *host = mmc->priv;
Adam Ford8ce7bdd2018-08-09 06:15:12 -050045#else
46
47static void davinci_mmc_set_clock(struct udevice *dev, uint clock)
48{
49 struct davinci_mmc_priv *host = dev_get_priv(dev);
50 struct mmc *mmc = mmc_get_mmc_dev(dev);
51#endif
Sandeep Paulraj50347172010-12-20 20:01:21 -050052 struct davinci_mmc_regs *regs = host->reg_base;
53 uint clkrt, sysclk2, act_clock;
54
Pantelis Antoniou2c850462014-03-11 19:34:20 +020055 if (clock < mmc->cfg->f_min)
56 clock = mmc->cfg->f_min;
57 if (clock > mmc->cfg->f_max)
58 clock = mmc->cfg->f_max;
Sandeep Paulraj50347172010-12-20 20:01:21 -050059
60 set_val(&regs->mmcclk, 0);
61 sysclk2 = host->input_clk;
62 clkrt = (sysclk2 / (2 * clock)) - 1;
63
64 /* Calculate the actual clock for the divider used */
65 act_clock = (sysclk2 / (2 * (clkrt + 1)));
66
67 /* Adjust divider if actual clock exceeds the required clock */
68 if (act_clock > clock)
69 clkrt++;
70
71 /* check clock divider boundary and correct it */
72 if (clkrt > 0xFF)
73 clkrt = 0xFF;
74
75 set_val(&regs->mmcclk, (clkrt | MMCCLK_CLKEN));
76}
77
78/* Status bit wait loop for MMCST1 */
79static int
80dmmc_wait_fifo_status(volatile struct davinci_mmc_regs *regs, uint status)
81{
Heiko Schocher1a3d7492011-10-30 19:15:53 +000082 uint wdog = WATCHDOG_COUNT;
83
Sandeep Paulraj50347172010-12-20 20:01:21 -050084 while (--wdog && ((get_val(&regs->mmcst1) & status) != status))
85 udelay(10);
86
87 if (!(get_val(&regs->mmcctl) & MMCCTL_WIDTH_4_BIT))
88 udelay(100);
89
90 if (wdog == 0)
Jaehoon Chung7825d202016-07-19 16:33:36 +090091 return -ECOMM;
Sandeep Paulraj50347172010-12-20 20:01:21 -050092
93 return 0;
94}
95
96/* Busy bit wait loop for MMCST1 */
97static int dmmc_busy_wait(volatile struct davinci_mmc_regs *regs)
98{
Heiko Schocher1a3d7492011-10-30 19:15:53 +000099 uint wdog = WATCHDOG_COUNT;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500100
Sandeep Paulraj50347172010-12-20 20:01:21 -0500101 while (--wdog && (get_val(&regs->mmcst1) & MMCST1_BUSY))
102 udelay(10);
103
104 if (wdog == 0)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900105 return -ECOMM;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500106
107 return 0;
108}
109
110/* Status bit wait loop for MMCST0 - Checks for error bits as well */
111static int dmmc_check_status(volatile struct davinci_mmc_regs *regs,
112 uint *cur_st, uint st_ready, uint st_error)
113{
114 uint wdog = WATCHDOG_COUNT;
115 uint mmcstatus = *cur_st;
116
117 while (wdog--) {
118 if (mmcstatus & st_ready) {
119 *cur_st = mmcstatus;
120 mmcstatus = get_val(&regs->mmcst1);
121 return 0;
122 } else if (mmcstatus & st_error) {
123 if (mmcstatus & MMCST0_TOUTRS)
Jaehoon Chung7825d202016-07-19 16:33:36 +0900124 return -ETIMEDOUT;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500125 printf("[ ST0 ERROR %x]\n", mmcstatus);
126 /*
127 * Ignore CRC errors as some MMC cards fail to
128 * initialize on DM365-EVM on the SD1 slot
129 */
130 if (mmcstatus & MMCST0_CRCRS)
131 return 0;
Jaehoon Chung7825d202016-07-19 16:33:36 +0900132 return -ECOMM;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500133 }
134 udelay(10);
135
136 mmcstatus = get_val(&regs->mmcst0);
137 }
138
139 printf("Status %x Timeout ST0:%x ST1:%x\n", st_ready, mmcstatus,
140 get_val(&regs->mmcst1));
Jaehoon Chung7825d202016-07-19 16:33:36 +0900141 return -ECOMM;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500142}
143
144/*
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500145 * Sends a command out on the bus. Takes the device pointer,
Sandeep Paulraj50347172010-12-20 20:01:21 -0500146 * a command pointer, and an optional data pointer.
147 */
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500148#if !CONFIG_IS_ENABLED(DM_MMC)
149static int dmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data)
Sandeep Paulraj50347172010-12-20 20:01:21 -0500150{
151 struct davinci_mmc *host = mmc->priv;
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500152#else
153static int
154davinci_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, struct mmc_data *data)
155{
156 struct davinci_mmc_priv *host = dev_get_priv(dev);
157#endif
Sandeep Paulraj50347172010-12-20 20:01:21 -0500158 volatile struct davinci_mmc_regs *regs = host->reg_base;
159 uint mmcstatus, status_rdy, status_err;
160 uint i, cmddata, bytes_left = 0;
161 int fifo_words, fifo_bytes, err;
162 char *data_buf = NULL;
163
164 /* Clear status registers */
165 mmcstatus = get_val(&regs->mmcst0);
Bartosz Golaszewski3dffb442019-11-14 16:10:28 +0100166 fifo_words = 16;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500167 fifo_bytes = fifo_words << 2;
168
169 /* Wait for any previous busy signal to be cleared */
170 dmmc_busy_wait(regs);
171
172 cmddata = cmd->cmdidx;
173 cmddata |= MMCCMD_PPLEN;
174
175 /* Send init clock for CMD0 */
176 if (cmd->cmdidx == MMC_CMD_GO_IDLE_STATE)
177 cmddata |= MMCCMD_INITCK;
178
179 switch (cmd->resp_type) {
180 case MMC_RSP_R1b:
181 cmddata |= MMCCMD_BSYEXP;
182 /* Fall-through */
183 case MMC_RSP_R1: /* R1, R1b, R5, R6, R7 */
184 cmddata |= MMCCMD_RSPFMT_R1567;
185 break;
186 case MMC_RSP_R2:
187 cmddata |= MMCCMD_RSPFMT_R2;
188 break;
189 case MMC_RSP_R3: /* R3, R4 */
190 cmddata |= MMCCMD_RSPFMT_R3;
191 break;
192 }
193
194 set_val(&regs->mmcim, 0);
195
196 if (data) {
197 /* clear previous data transfer if any and set new one */
198 bytes_left = (data->blocksize * data->blocks);
199
200 /* Reset FIFO - Always use 32 byte fifo threshold */
201 set_val(&regs->mmcfifoctl,
202 (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
203
Bartosz Golaszewski3dffb442019-11-14 16:10:28 +0100204 cmddata |= MMCCMD_DMATRIG;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500205
206 cmddata |= MMCCMD_WDATX;
207 if (data->flags == MMC_DATA_READ) {
208 set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
209 } else if (data->flags == MMC_DATA_WRITE) {
210 set_val(&regs->mmcfifoctl,
211 (MMCFIFOCTL_FIFOLEV |
212 MMCFIFOCTL_FIFODIR));
213 cmddata |= MMCCMD_DTRW;
214 }
215
216 set_val(&regs->mmctod, 0xFFFF);
217 set_val(&regs->mmcnblk, (data->blocks & MMCNBLK_NBLK_MASK));
218 set_val(&regs->mmcblen, (data->blocksize & MMCBLEN_BLEN_MASK));
219
220 if (data->flags == MMC_DATA_WRITE) {
221 uint val;
222 data_buf = (char *)data->src;
223 /* For write, fill FIFO with data before issue of CMD */
224 for (i = 0; (i < fifo_words) && bytes_left; i++) {
225 memcpy((char *)&val, data_buf, 4);
226 set_val(&regs->mmcdxr, val);
227 data_buf += 4;
228 bytes_left -= 4;
229 }
230 }
231 } else {
232 set_val(&regs->mmcblen, 0);
233 set_val(&regs->mmcnblk, 0);
234 }
235
236 set_val(&regs->mmctor, 0x1FFF);
237
238 /* Send the command */
239 set_val(&regs->mmcarghl, cmd->cmdarg);
240 set_val(&regs->mmccmd, cmddata);
241
242 status_rdy = MMCST0_RSPDNE;
243 status_err = (MMCST0_TOUTRS | MMCST0_TOUTRD |
244 MMCST0_CRCWR | MMCST0_CRCRD);
245 if (cmd->resp_type & MMC_RSP_CRC)
246 status_err |= MMCST0_CRCRS;
247
248 mmcstatus = get_val(&regs->mmcst0);
249 err = dmmc_check_status(regs, &mmcstatus, status_rdy, status_err);
250 if (err)
251 return err;
252
253 /* For R1b wait for busy done */
254 if (cmd->resp_type == MMC_RSP_R1b)
255 dmmc_busy_wait(regs);
256
257 /* Collect response from controller for specific commands */
258 if (mmcstatus & MMCST0_RSPDNE) {
259 /* Copy the response to the response buffer */
260 if (cmd->resp_type & MMC_RSP_136) {
261 cmd->response[0] = get_val(&regs->mmcrsp67);
262 cmd->response[1] = get_val(&regs->mmcrsp45);
263 cmd->response[2] = get_val(&regs->mmcrsp23);
264 cmd->response[3] = get_val(&regs->mmcrsp01);
265 } else if (cmd->resp_type & MMC_RSP_PRESENT) {
266 cmd->response[0] = get_val(&regs->mmcrsp67);
267 }
268 }
269
270 if (data == NULL)
271 return 0;
272
273 if (data->flags == MMC_DATA_READ) {
274 /* check for DATDNE along with DRRDY as the controller might
275 * set the DATDNE without DRRDY for smaller transfers with
276 * less than FIFO threshold bytes
277 */
278 status_rdy = MMCST0_DRRDY | MMCST0_DATDNE;
279 status_err = MMCST0_TOUTRD | MMCST0_CRCRD;
280 data_buf = data->dest;
281 } else {
282 status_rdy = MMCST0_DXRDY | MMCST0_DATDNE;
283 status_err = MMCST0_CRCWR;
284 }
285
286 /* Wait until all of the blocks are transferred */
287 while (bytes_left) {
288 err = dmmc_check_status(regs, &mmcstatus, status_rdy,
289 status_err);
290 if (err)
291 return err;
292
293 if (data->flags == MMC_DATA_READ) {
294 /*
295 * MMC controller sets the Data receive ready bit
296 * (DRRDY) in MMCST0 even before the entire FIFO is
297 * full. This results in erratic behavior if we start
298 * reading the FIFO soon after DRRDY. Wait for the
299 * FIFO full bit in MMCST1 for proper FIFO clearing.
300 */
301 if (bytes_left > fifo_bytes)
302 dmmc_wait_fifo_status(regs, 0x4a);
Davide Bonfantif7289ee2012-11-29 01:06:53 +0000303 else if (bytes_left == fifo_bytes) {
Sandeep Paulraj50347172010-12-20 20:01:21 -0500304 dmmc_wait_fifo_status(regs, 0x40);
Davide Bonfantif7289ee2012-11-29 01:06:53 +0000305 if (cmd->cmdidx == MMC_CMD_SEND_EXT_CSD)
306 udelay(600);
307 }
Sandeep Paulraj50347172010-12-20 20:01:21 -0500308
309 for (i = 0; bytes_left && (i < fifo_words); i++) {
310 cmddata = get_val(&regs->mmcdrr);
311 memcpy(data_buf, (char *)&cmddata, 4);
312 data_buf += 4;
313 bytes_left -= 4;
314 }
315 } else {
316 /*
317 * MMC controller sets the Data transmit ready bit
318 * (DXRDY) in MMCST0 even before the entire FIFO is
319 * empty. This results in erratic behavior if we start
320 * writing the FIFO soon after DXRDY. Wait for the
321 * FIFO empty bit in MMCST1 for proper FIFO clearing.
322 */
323 dmmc_wait_fifo_status(regs, MMCST1_FIFOEMP);
324 for (i = 0; bytes_left && (i < fifo_words); i++) {
325 memcpy((char *)&cmddata, data_buf, 4);
326 set_val(&regs->mmcdxr, cmddata);
327 data_buf += 4;
328 bytes_left -= 4;
329 }
330 dmmc_busy_wait(regs);
331 }
332 }
333
334 err = dmmc_check_status(regs, &mmcstatus, MMCST0_DATDNE, status_err);
335 if (err)
336 return err;
337
338 return 0;
339}
340
341/* Initialize Davinci MMC controller */
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500342#if !CONFIG_IS_ENABLED(DM_MMC)
Sandeep Paulraj50347172010-12-20 20:01:21 -0500343static int dmmc_init(struct mmc *mmc)
344{
345 struct davinci_mmc *host = mmc->priv;
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500346#else
347static int davinci_dm_mmc_init(struct udevice *dev)
348{
349 struct davinci_mmc_priv *host = dev_get_priv(dev);
350#endif
Sandeep Paulraj50347172010-12-20 20:01:21 -0500351 struct davinci_mmc_regs *regs = host->reg_base;
352
353 /* Clear status registers explicitly - soft reset doesn't clear it
354 * If Uboot is invoked from UBL with SDMMC Support, the status
355 * registers can have uncleared bits
356 */
357 get_val(&regs->mmcst0);
358 get_val(&regs->mmcst1);
359
360 /* Hold software reset */
361 set_bit(&regs->mmcctl, MMCCTL_DATRST);
362 set_bit(&regs->mmcctl, MMCCTL_CMDRST);
363 udelay(10);
364
365 set_val(&regs->mmcclk, 0x0);
366 set_val(&regs->mmctor, 0x1FFF);
367 set_val(&regs->mmctod, 0xFFFF);
368
369 /* Clear software reset */
370 clear_bit(&regs->mmcctl, MMCCTL_DATRST);
371 clear_bit(&regs->mmcctl, MMCCTL_CMDRST);
372
373 udelay(10);
374
375 /* Reset FIFO - Always use the maximum fifo threshold */
376 set_val(&regs->mmcfifoctl, (MMCFIFOCTL_FIFOLEV | MMCFIFOCTL_FIFORST));
377 set_val(&regs->mmcfifoctl, MMCFIFOCTL_FIFOLEV);
378
379 return 0;
380}
381
Masahiro Yamada0a780172017-05-09 20:31:39 +0900382/* Set buswidth or clock as indicated by the MMC framework */
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500383#if !CONFIG_IS_ENABLED(DM_MMC)
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900384static int dmmc_set_ios(struct mmc *mmc)
Sandeep Paulraj50347172010-12-20 20:01:21 -0500385{
386 struct davinci_mmc *host = mmc->priv;
387 struct davinci_mmc_regs *regs = host->reg_base;
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500388#else
389static int davinci_mmc_set_ios(struct udevice *dev)
390{
391 struct mmc *mmc = mmc_get_mmc_dev(dev);
Sandeep Paulraj50347172010-12-20 20:01:21 -0500392
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500393 struct davinci_mmc_priv *host = dev_get_priv(dev);
394 struct davinci_mmc_regs *regs = host->reg_base;
395#endif
Sandeep Paulraj50347172010-12-20 20:01:21 -0500396 /* Set the bus width */
397 if (mmc->bus_width == 4)
398 set_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
399 else
400 clear_bit(&regs->mmcctl, MMCCTL_WIDTH_4_BIT);
401
402 /* Set clock speed */
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500403 if (mmc->clock) {
404#if !CONFIG_IS_ENABLED(DM_MMC)
Sandeep Paulraj50347172010-12-20 20:01:21 -0500405 dmmc_set_clock(mmc, mmc->clock);
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500406#else
407 davinci_mmc_set_clock(dev, mmc->clock);
408#endif
409 }
Jaehoon Chungb6cd1d32016-12-30 15:30:16 +0900410 return 0;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500411}
412
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500413#if !CONFIG_IS_ENABLED(DM_MMC)
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200414static const struct mmc_ops dmmc_ops = {
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500415 .send_cmd = dmmc_send_cmd,
416 .set_ios = dmmc_set_ios,
417 .init = dmmc_init,
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200418};
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500419#else
Adam Ford7d9bdcc2018-09-03 03:47:52 -0500420
421static int davinci_mmc_getcd(struct udevice *dev)
422{
423 int value = -1;
424#if CONFIG_IS_ENABLED(DM_GPIO)
425 struct davinci_mmc_priv *priv = dev_get_priv(dev);
426 value = dm_gpio_get_value(&priv->cd_gpio);
427#endif
428 /* if no CD return as 1 */
429 if (value < 0)
430 return 1;
431
432 return value;
433}
434
435static int davinci_mmc_getwp(struct udevice *dev)
436{
437 int value = -1;
438#if CONFIG_IS_ENABLED(DM_GPIO)
439 struct davinci_mmc_priv *priv = dev_get_priv(dev);
440
441 value = dm_gpio_get_value(&priv->wp_gpio);
442#endif
443 /* if no WP return as 0 */
444 if (value < 0)
445 return 0;
446
447 return value;
448}
449
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500450static const struct dm_mmc_ops davinci_mmc_ops = {
451 .send_cmd = davinci_mmc_send_cmd,
452 .set_ios = davinci_mmc_set_ios,
Adam Ford7d9bdcc2018-09-03 03:47:52 -0500453 .get_cd = davinci_mmc_getcd,
454 .get_wp = davinci_mmc_getwp,
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500455};
456#endif
Pantelis Antoniouc9e75912014-02-26 19:28:45 +0200457
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500458#if !CONFIG_IS_ENABLED(DM_MMC)
Sandeep Paulraj50347172010-12-20 20:01:21 -0500459/* Called from board_mmc_init during startup. Can be called multiple times
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500460* depending on the number of slots available on board and controller
461*/
Sandeep Paulraj50347172010-12-20 20:01:21 -0500462int davinci_mmc_init(bd_t *bis, struct davinci_mmc *host)
463{
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200464 host->cfg.name = "davinci";
465 host->cfg.ops = &dmmc_ops;
466 host->cfg.f_min = 200000;
467 host->cfg.f_max = 25000000;
468 host->cfg.voltages = host->voltages;
469 host->cfg.host_caps = host->host_caps;
Sandeep Paulraj50347172010-12-20 20:01:21 -0500470
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200471 host->cfg.b_max = DAVINCI_MAX_BLOCKS;
John Rigbyf2f43662011-04-18 05:50:08 +0000472
Pantelis Antoniou2c850462014-03-11 19:34:20 +0200473 mmc_create(&host->cfg, host);
Sandeep Paulraj50347172010-12-20 20:01:21 -0500474
475 return 0;
476}
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500477#else
478
479
480static int davinci_mmc_probe(struct udevice *dev)
481{
482 struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500483 struct davinci_mmc_priv *priv = dev_get_priv(dev);
Bartosz Golaszewski012929e2019-11-14 16:10:29 +0100484 struct mmc_config *cfg = &priv->cfg;
Bartosz Golaszewskie33cd222019-11-14 16:10:31 +0100485#ifdef CONFIG_SPL_BUILD
486 int ret;
487#endif
Bartosz Golaszewski3dffb442019-11-14 16:10:28 +0100488
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500489 cfg->f_min = 200000;
490 cfg->f_max = 25000000;
491 cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34,
492 cfg->host_caps = MMC_MODE_4BIT, /* DA850 supports only 4-bit SD/MMC */
493 cfg->b_max = DAVINCI_MAX_BLOCKS;
Bartosz Golaszewski3dffb442019-11-14 16:10:28 +0100494 cfg->name = "da830-mmc";
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500495
496 priv->reg_base = (struct davinci_mmc_regs *)dev_read_addr(dev);
497 priv->input_clk = clk_get(DAVINCI_MMCSD_CLKID);
498
Adam Ford7d9bdcc2018-09-03 03:47:52 -0500499#if CONFIG_IS_ENABLED(DM_GPIO)
500 /* These GPIOs are optional */
501 gpio_request_by_name(dev, "cd-gpios", 0, &priv->cd_gpio, GPIOD_IS_IN);
502 gpio_request_by_name(dev, "wp-gpios", 0, &priv->wp_gpio, GPIOD_IS_IN);
503#endif
504
Bartosz Golaszewski012929e2019-11-14 16:10:29 +0100505 upriv->mmc = &priv->mmc;
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500506
Bartosz Golaszewskie33cd222019-11-14 16:10:31 +0100507#ifdef CONFIG_SPL_BUILD
508 /*
509 * FIXME This is a temporary workaround to enable the driver model in
510 * SPL on omapl138-lcdk. For some reason the bind() callback is not
511 * being called in SPL for MMC which breaks the mmc boot - the hack
512 * is to call mmc_bind() from probe(). We also don't have full DT
513 * support in SPL, hence the hard-coded base register address.
514 */
515 priv->reg_base = (struct davinci_mmc_regs *)DAVINCI_MMC_SD0_BASE;
516 ret = mmc_bind(dev, &priv->mmc, &priv->cfg);
517 if (ret)
518 return ret;
519#endif
520
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500521 return davinci_dm_mmc_init(dev);
522}
523
524static int davinci_mmc_bind(struct udevice *dev)
525{
Bartosz Golaszewski012929e2019-11-14 16:10:29 +0100526 struct davinci_mmc_priv *priv = dev_get_priv(dev);
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500527
Bartosz Golaszewski012929e2019-11-14 16:10:29 +0100528 return mmc_bind(dev, &priv->mmc, &priv->cfg);
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500529}
530
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500531static const struct udevice_id davinci_mmc_ids[] = {
Bartosz Golaszewski3dffb442019-11-14 16:10:28 +0100532 { .compatible = "ti,da830-mmc" },
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500533 {},
534};
535
536U_BOOT_DRIVER(davinci_mmc_drv) = {
537 .name = "davinci_mmc",
538 .id = UCLASS_MMC,
539 .of_match = davinci_mmc_ids,
540#if CONFIG_BLK
541 .bind = davinci_mmc_bind,
542#endif
543 .probe = davinci_mmc_probe,
544 .ops = &davinci_mmc_ops,
Adam Ford8ce7bdd2018-08-09 06:15:12 -0500545 .priv_auto_alloc_size = sizeof(struct davinci_mmc_priv),
546};
547#endif