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