Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Freescale i.MX28 SSP MMC driver |
| 4 | * |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 5 | * Copyright (C) 2019 DENX Software Engineering |
| 6 | * Lukasz Majewski, DENX Software Engineering, lukma@denx.de |
| 7 | * |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 8 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 9 | * on behalf of DENX Software Engineering GmbH |
| 10 | * |
| 11 | * Based on code from LTIB: |
| 12 | * (C) Copyright 2008-2010 Freescale Semiconductor, Inc. |
| 13 | * Terry Lv |
| 14 | * |
| 15 | * Copyright 2007, Freescale Semiconductor, Inc |
| 16 | * Andy Fleming |
| 17 | * |
| 18 | * Based vaguely on the pxa mmc code: |
| 19 | * (C) Copyright 2003 |
| 20 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 21 | */ |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 22 | |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 23 | #include <common.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 24 | #include <log.h> |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 25 | #include <malloc.h> |
| 26 | #include <mmc.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 27 | #include <linux/errno.h> |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 28 | #include <asm/io.h> |
| 29 | #include <asm/arch/clock.h> |
| 30 | #include <asm/arch/imx-regs.h> |
| 31 | #include <asm/arch/sys_proto.h> |
Stefano Babic | 33731bc | 2017-06-29 10:16:06 +0200 | [diff] [blame] | 32 | #include <asm/mach-imx/dma.h> |
Marek Vasut | acc8d6b | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 33 | #include <bouncebuf.h> |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 34 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 35 | #define MXSMMC_MAX_TIMEOUT 10000 |
| 36 | #define MXSMMC_SMALL_TRANSFER 512 |
| 37 | |
| 38 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 39 | struct mxsmmc_priv { |
| 40 | int id; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 41 | int (*mmc_is_wp)(int); |
Marek Vasut | 722181e | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 42 | int (*mmc_cd)(int); |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 43 | struct mmc_config cfg; /* mmc configuration */ |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 44 | struct mxs_dma_desc *desc; |
| 45 | uint32_t buswidth; |
| 46 | struct mxs_ssp_regs *regs; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 47 | }; |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 48 | #else /* CONFIG_IS_ENABLED(DM_MMC) */ |
| 49 | #include <dm/device.h> |
| 50 | #include <dm/read.h> |
| 51 | #include <dt-structs.h> |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 52 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 53 | #ifdef CONFIG_MX28 |
| 54 | #define dtd_fsl_imx_mmc dtd_fsl_imx28_mmc |
| 55 | #else /* CONFIG_MX23 */ |
| 56 | #define dtd_fsl_imx_mmc dtd_fsl_imx23_mmc |
| 57 | #endif |
| 58 | |
| 59 | struct mxsmmc_platdata { |
| 60 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 61 | struct dtd_fsl_imx_mmc dtplat; |
| 62 | #endif |
| 63 | struct mmc_config cfg; |
| 64 | struct mmc mmc; |
| 65 | fdt_addr_t base; |
| 66 | int non_removable; |
| 67 | int buswidth; |
| 68 | int dma_id; |
| 69 | int clk_id; |
| 70 | }; |
| 71 | |
| 72 | struct mxsmmc_priv { |
| 73 | int clkid; |
| 74 | struct mxs_dma_desc *desc; |
| 75 | u32 buswidth; |
| 76 | struct mxs_ssp_regs *regs; |
| 77 | unsigned int dma_channel; |
| 78 | }; |
| 79 | #endif |
| 80 | |
| 81 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 82 | static int mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 83 | struct mmc_data *data); |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 84 | |
Marek Vasut | 722181e | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 85 | static int mxsmmc_cd(struct mxsmmc_priv *priv) |
| 86 | { |
| 87 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 88 | |
| 89 | if (priv->mmc_cd) |
| 90 | return priv->mmc_cd(priv->id); |
| 91 | |
| 92 | return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT); |
| 93 | } |
| 94 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 95 | static int mxsmmc_set_ios(struct mmc *mmc) |
| 96 | { |
| 97 | struct mxsmmc_priv *priv = mmc->priv; |
| 98 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 99 | |
| 100 | /* Set the clock speed */ |
| 101 | if (mmc->clock) |
| 102 | mxs_set_ssp_busclock(priv->id, mmc->clock / 1000); |
| 103 | |
| 104 | switch (mmc->bus_width) { |
| 105 | case 1: |
| 106 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT; |
| 107 | break; |
| 108 | case 4: |
| 109 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT; |
| 110 | break; |
| 111 | case 8: |
| 112 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT; |
| 113 | break; |
| 114 | } |
| 115 | |
| 116 | /* Set the bus width */ |
| 117 | clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0, |
| 118 | SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth); |
| 119 | |
| 120 | debug("MMC%d: Set %d bits bus width\n", |
| 121 | mmc->block_dev.devnum, mmc->bus_width); |
| 122 | |
| 123 | return 0; |
| 124 | } |
| 125 | |
| 126 | static int mxsmmc_init(struct mmc *mmc) |
| 127 | { |
| 128 | struct mxsmmc_priv *priv = mmc->priv; |
| 129 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 130 | |
| 131 | /* Reset SSP */ |
| 132 | mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg); |
| 133 | |
| 134 | /* Reconfigure the SSP block for MMC operation */ |
| 135 | writel(SSP_CTRL1_SSP_MODE_SD_MMC | |
| 136 | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS | |
| 137 | SSP_CTRL1_DMA_ENABLE | |
| 138 | SSP_CTRL1_POLARITY | |
| 139 | SSP_CTRL1_RECV_TIMEOUT_IRQ_EN | |
| 140 | SSP_CTRL1_DATA_CRC_IRQ_EN | |
| 141 | SSP_CTRL1_DATA_TIMEOUT_IRQ_EN | |
| 142 | SSP_CTRL1_RESP_TIMEOUT_IRQ_EN | |
| 143 | SSP_CTRL1_RESP_ERR_IRQ_EN, |
| 144 | &ssp_regs->hw_ssp_ctrl1_set); |
| 145 | |
| 146 | /* Set initial bit clock 400 KHz */ |
| 147 | mxs_set_ssp_busclock(priv->id, 400); |
| 148 | |
| 149 | /* Send initial 74 clock cycles (185 us @ 400 KHz)*/ |
| 150 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set); |
| 151 | udelay(200); |
| 152 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr); |
| 153 | |
| 154 | return 0; |
| 155 | } |
| 156 | |
| 157 | static const struct mmc_ops mxsmmc_ops = { |
| 158 | .send_cmd = mxsmmc_send_cmd, |
| 159 | .set_ios = mxsmmc_set_ios, |
| 160 | .init = mxsmmc_init, |
| 161 | }; |
| 162 | |
| 163 | int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int), int (*cd)(int)) |
| 164 | { |
| 165 | struct mmc *mmc = NULL; |
| 166 | struct mxsmmc_priv *priv = NULL; |
| 167 | int ret; |
| 168 | const unsigned int mxsmmc_clk_id = mxs_ssp_clock_by_bus(id); |
| 169 | |
| 170 | if (!mxs_ssp_bus_id_valid(id)) |
| 171 | return -ENODEV; |
| 172 | |
| 173 | priv = malloc(sizeof(struct mxsmmc_priv)); |
| 174 | if (!priv) |
| 175 | return -ENOMEM; |
| 176 | |
| 177 | priv->desc = mxs_dma_desc_alloc(); |
| 178 | if (!priv->desc) { |
| 179 | free(priv); |
| 180 | return -ENOMEM; |
| 181 | } |
| 182 | |
| 183 | ret = mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + id); |
| 184 | if (ret) |
| 185 | return ret; |
| 186 | |
| 187 | priv->mmc_is_wp = wp; |
| 188 | priv->mmc_cd = cd; |
| 189 | priv->id = id; |
| 190 | priv->regs = mxs_ssp_regs_by_bus(id); |
| 191 | |
| 192 | priv->cfg.name = "MXS MMC"; |
| 193 | priv->cfg.ops = &mxsmmc_ops; |
| 194 | |
| 195 | priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 196 | |
| 197 | priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | |
| 198 | MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 199 | |
| 200 | /* |
| 201 | * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz |
| 202 | * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)), |
| 203 | * CLOCK_DIVIDE has to be an even value from 2 to 254, and |
| 204 | * CLOCK_RATE could be any integer from 0 to 255. |
| 205 | */ |
| 206 | priv->cfg.f_min = 400000; |
| 207 | priv->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id) |
| 208 | * 1000 / 2; |
| 209 | priv->cfg.b_max = 0x20; |
| 210 | |
| 211 | mmc = mmc_create(&priv->cfg, priv); |
| 212 | if (!mmc) { |
| 213 | mxs_dma_desc_free(priv->desc); |
| 214 | free(priv); |
| 215 | return -ENOMEM; |
| 216 | } |
| 217 | return 0; |
| 218 | } |
| 219 | #endif /* CONFIG_IS_ENABLED(DM_MMC) */ |
| 220 | |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 221 | static int mxsmmc_send_cmd_pio(struct mxsmmc_priv *priv, struct mmc_data *data) |
| 222 | { |
| 223 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 224 | uint32_t *data_ptr; |
| 225 | int timeout = MXSMMC_MAX_TIMEOUT; |
| 226 | uint32_t reg; |
| 227 | uint32_t data_count = data->blocksize * data->blocks; |
| 228 | |
| 229 | if (data->flags & MMC_DATA_READ) { |
| 230 | data_ptr = (uint32_t *)data->dest; |
| 231 | while (data_count && --timeout) { |
| 232 | reg = readl(&ssp_regs->hw_ssp_status); |
| 233 | if (!(reg & SSP_STATUS_FIFO_EMPTY)) { |
| 234 | *data_ptr++ = readl(&ssp_regs->hw_ssp_data); |
| 235 | data_count -= 4; |
| 236 | timeout = MXSMMC_MAX_TIMEOUT; |
| 237 | } else |
| 238 | udelay(1000); |
| 239 | } |
| 240 | } else { |
| 241 | data_ptr = (uint32_t *)data->src; |
| 242 | timeout *= 100; |
| 243 | while (data_count && --timeout) { |
| 244 | reg = readl(&ssp_regs->hw_ssp_status); |
| 245 | if (!(reg & SSP_STATUS_FIFO_FULL)) { |
| 246 | writel(*data_ptr++, &ssp_regs->hw_ssp_data); |
| 247 | data_count -= 4; |
| 248 | timeout = MXSMMC_MAX_TIMEOUT; |
| 249 | } else |
| 250 | udelay(1000); |
| 251 | } |
| 252 | } |
| 253 | |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 254 | return timeout ? 0 : -ECOMM; |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 255 | } |
Marek Vasut | 97c04d6 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 256 | |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 257 | static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data) |
| 258 | { |
| 259 | uint32_t data_count = data->blocksize * data->blocks; |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 260 | int dmach; |
Marek Vasut | 042cfd6 | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 261 | struct mxs_dma_desc *desc = priv->desc; |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 262 | void *addr; |
| 263 | unsigned int flags; |
| 264 | struct bounce_buffer bbstate; |
Marek Vasut | 042cfd6 | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 265 | |
| 266 | memset(desc, 0, sizeof(struct mxs_dma_desc)); |
| 267 | desc->address = (dma_addr_t)desc; |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 268 | |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 269 | if (data->flags & MMC_DATA_READ) { |
| 270 | priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE; |
Marek Vasut | acc8d6b | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 271 | addr = data->dest; |
| 272 | flags = GEN_BB_WRITE; |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 273 | } else { |
| 274 | priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ; |
Marek Vasut | acc8d6b | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 275 | addr = (void *)data->src; |
| 276 | flags = GEN_BB_READ; |
| 277 | } |
| 278 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 279 | bounce_buffer_start(&bbstate, addr, data_count, flags); |
Marek Vasut | acc8d6b | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 280 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 281 | priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer; |
Marek Vasut | 761fb5b | 2012-08-31 16:18:10 +0000 | [diff] [blame] | 282 | |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 283 | priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM | |
| 284 | (data_count << MXS_DMA_DESC_BYTES_OFFSET); |
| 285 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 286 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Marek Vasut | eadf337 | 2013-02-23 02:42:58 +0000 | [diff] [blame] | 287 | dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id; |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 288 | #else |
| 289 | dmach = priv->dma_channel; |
| 290 | #endif |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 291 | mxs_dma_desc_append(dmach, priv->desc); |
Marek Vasut | acc8d6b | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 292 | if (mxs_dma_go(dmach)) { |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 293 | bounce_buffer_stop(&bbstate); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 294 | return -ECOMM; |
Marek Vasut | acc8d6b | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 295 | } |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 296 | |
Stephen Warren | 4a8629e | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 297 | bounce_buffer_stop(&bbstate); |
Marek Vasut | acc8d6b | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 298 | |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 299 | return 0; |
| 300 | } |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 301 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 302 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 303 | /* |
| 304 | * Sends a command out on the bus. Takes the mmc pointer, |
| 305 | * a command pointer, and an optional data pointer. |
| 306 | */ |
| 307 | static int |
| 308 | mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 309 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 310 | struct mxsmmc_priv *priv = mmc->priv; |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 311 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 312 | #else |
| 313 | static int |
| 314 | mxsmmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, struct mmc_data *data) |
| 315 | { |
| 316 | struct mxsmmc_platdata *plat = dev_get_platdata(dev); |
| 317 | struct mxsmmc_priv *priv = dev_get_priv(dev); |
| 318 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 319 | struct mmc *mmc = &plat->mmc; |
| 320 | #endif |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 321 | uint32_t reg; |
| 322 | int timeout; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 323 | uint32_t ctrl0; |
Marek Vasut | 92af615 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 324 | int ret; |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 325 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 326 | int devnum = mmc->block_dev.devnum; |
| 327 | #else |
| 328 | int devnum = mmc_get_blk_desc(mmc)->devnum; |
| 329 | #endif |
| 330 | debug("MMC%d: CMD%d\n", devnum, cmd->cmdidx); |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 331 | |
| 332 | /* Check bus busy */ |
| 333 | timeout = MXSMMC_MAX_TIMEOUT; |
| 334 | while (--timeout) { |
| 335 | udelay(1000); |
| 336 | reg = readl(&ssp_regs->hw_ssp_status); |
| 337 | if (!(reg & |
| 338 | (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY | |
| 339 | SSP_STATUS_CMD_BUSY))) { |
| 340 | break; |
| 341 | } |
| 342 | } |
| 343 | |
| 344 | if (!timeout) { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 345 | printf("MMC%d: Bus busy timeout!\n", devnum); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 346 | return -ETIMEDOUT; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 347 | } |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 348 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 349 | /* See if card is present */ |
Marek Vasut | 722181e | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 350 | if (!mxsmmc_cd(priv)) { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 351 | printf("MMC%d: No card detected!\n", devnum); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 352 | return -ENOMEDIUM; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 353 | } |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 354 | #endif |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 355 | /* Start building CTRL0 contents */ |
| 356 | ctrl0 = priv->buswidth; |
| 357 | |
| 358 | /* Set up command */ |
| 359 | if (!(cmd->resp_type & MMC_RSP_CRC)) |
| 360 | ctrl0 |= SSP_CTRL0_IGNORE_CRC; |
| 361 | if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */ |
| 362 | ctrl0 |= SSP_CTRL0_GET_RESP; |
| 363 | if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */ |
| 364 | ctrl0 |= SSP_CTRL0_LONG_RESP; |
| 365 | |
Marek Vasut | 042cfd6 | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 366 | if (data && (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER)) |
| 367 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr); |
| 368 | else |
| 369 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set); |
| 370 | |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 371 | /* Command index */ |
| 372 | reg = readl(&ssp_regs->hw_ssp_cmd0); |
| 373 | reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC); |
| 374 | reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET; |
| 375 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 376 | reg |= SSP_CMD0_APPEND_8CYC; |
| 377 | writel(reg, &ssp_regs->hw_ssp_cmd0); |
| 378 | |
| 379 | /* Command argument */ |
| 380 | writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1); |
| 381 | |
| 382 | /* Set up data */ |
| 383 | if (data) { |
| 384 | /* READ or WRITE */ |
| 385 | if (data->flags & MMC_DATA_READ) { |
| 386 | ctrl0 |= SSP_CTRL0_READ; |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 387 | #if !CONFIG_IS_ENABLED(DM_MMC) |
Marek Vasut | 336ff28 | 2012-05-01 11:09:52 +0000 | [diff] [blame] | 388 | } else if (priv->mmc_is_wp && |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 389 | priv->mmc_is_wp(devnum)) { |
| 390 | printf("MMC%d: Can not write a locked card!\n", devnum); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 391 | return -EOPNOTSUPP; |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 392 | #endif |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 393 | } |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 394 | ctrl0 |= SSP_CTRL0_DATA_XFER; |
Marek Vasut | ecf843e | 2013-01-22 15:01:04 +0000 | [diff] [blame] | 395 | |
| 396 | reg = data->blocksize * data->blocks; |
| 397 | #if defined(CONFIG_MX23) |
| 398 | ctrl0 |= reg & SSP_CTRL0_XFER_COUNT_MASK; |
| 399 | |
| 400 | clrsetbits_le32(&ssp_regs->hw_ssp_cmd0, |
| 401 | SSP_CMD0_BLOCK_SIZE_MASK | SSP_CMD0_BLOCK_COUNT_MASK, |
| 402 | ((data->blocks - 1) << SSP_CMD0_BLOCK_COUNT_OFFSET) | |
| 403 | ((ffs(data->blocksize) - 1) << |
| 404 | SSP_CMD0_BLOCK_SIZE_OFFSET)); |
| 405 | #elif defined(CONFIG_MX28) |
| 406 | writel(reg, &ssp_regs->hw_ssp_xfer_size); |
| 407 | |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 408 | reg = ((data->blocks - 1) << |
| 409 | SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) | |
| 410 | ((ffs(data->blocksize) - 1) << |
| 411 | SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET); |
| 412 | writel(reg, &ssp_regs->hw_ssp_block_size); |
Marek Vasut | ecf843e | 2013-01-22 15:01:04 +0000 | [diff] [blame] | 413 | #endif |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | /* Kick off the command */ |
| 417 | ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN; |
| 418 | writel(ctrl0, &ssp_regs->hw_ssp_ctrl0); |
| 419 | |
| 420 | /* Wait for the command to complete */ |
| 421 | timeout = MXSMMC_MAX_TIMEOUT; |
| 422 | while (--timeout) { |
| 423 | udelay(1000); |
| 424 | reg = readl(&ssp_regs->hw_ssp_status); |
| 425 | if (!(reg & SSP_STATUS_CMD_BUSY)) |
| 426 | break; |
| 427 | } |
| 428 | |
| 429 | if (!timeout) { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 430 | printf("MMC%d: Command %d busy\n", devnum, cmd->cmdidx); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 431 | return -ETIMEDOUT; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | /* Check command timeout */ |
| 435 | if (reg & SSP_STATUS_RESP_TIMEOUT) { |
Lukasz Majewski | 058c135 | 2019-09-05 09:55:00 +0200 | [diff] [blame] | 436 | debug("MMC%d: Command %d timeout (status 0x%08x)\n", |
| 437 | devnum, cmd->cmdidx, reg); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 438 | return -ETIMEDOUT; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | /* Check command errors */ |
| 442 | if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) { |
| 443 | printf("MMC%d: Command %d error (status 0x%08x)!\n", |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 444 | devnum, cmd->cmdidx, reg); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 445 | return -ECOMM; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | /* Copy response to response buffer */ |
| 449 | if (cmd->resp_type & MMC_RSP_136) { |
| 450 | cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0); |
| 451 | cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1); |
| 452 | cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2); |
| 453 | cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3); |
| 454 | } else |
| 455 | cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0); |
| 456 | |
| 457 | /* Return if no data to process */ |
| 458 | if (!data) |
| 459 | return 0; |
| 460 | |
Marek Vasut | 97c04d6 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 461 | if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) { |
Marek Vasut | 97c04d6 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 462 | ret = mxsmmc_send_cmd_pio(priv, data); |
| 463 | if (ret) { |
| 464 | printf("MMC%d: Data timeout with command %d " |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 465 | "(status 0x%08x)!\n", devnum, cmd->cmdidx, reg); |
Marek Vasut | 97c04d6 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 466 | return ret; |
| 467 | } |
Marek Vasut | 042cfd6 | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 468 | } else { |
| 469 | ret = mxsmmc_send_cmd_dma(priv, data); |
| 470 | if (ret) { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 471 | printf("MMC%d: DMA transfer failed\n", devnum); |
Marek Vasut | 042cfd6 | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 472 | return ret; |
| 473 | } |
Marek Vasut | dac199b | 2012-04-05 03:30:35 +0000 | [diff] [blame] | 474 | } |
Marek Vasut | 5796bed | 2012-03-15 18:33:21 +0000 | [diff] [blame] | 475 | |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 476 | /* Check data errors */ |
| 477 | reg = readl(&ssp_regs->hw_ssp_status); |
| 478 | if (reg & |
| 479 | (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR | |
| 480 | SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) { |
| 481 | printf("MMC%d: Data error with command %d (status 0x%08x)!\n", |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 482 | devnum, cmd->cmdidx, reg); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 483 | return -ECOMM; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 484 | } |
| 485 | |
| 486 | return 0; |
| 487 | } |
| 488 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 489 | #if CONFIG_IS_ENABLED(DM_MMC) |
| 490 | /* Base numbers of i.MX2[38] clk for ssp0 IP block */ |
| 491 | #define MXS_SSP_IMX23_CLKID_SSP0 33 |
| 492 | #define MXS_SSP_IMX28_CLKID_SSP0 46 |
| 493 | |
| 494 | static int mxsmmc_get_cd(struct udevice *dev) |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 495 | { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 496 | struct mxsmmc_platdata *plat = dev_get_platdata(dev); |
| 497 | struct mxsmmc_priv *priv = dev_get_priv(dev); |
| 498 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 499 | |
| 500 | if (plat->non_removable) |
| 501 | return 1; |
| 502 | |
| 503 | return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT); |
| 504 | } |
| 505 | |
| 506 | static int mxsmmc_set_ios(struct udevice *dev) |
| 507 | { |
| 508 | struct mxsmmc_platdata *plat = dev_get_platdata(dev); |
| 509 | struct mxsmmc_priv *priv = dev_get_priv(dev); |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 510 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 511 | struct mmc *mmc = &plat->mmc; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 512 | |
| 513 | /* Set the clock speed */ |
| 514 | if (mmc->clock) |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 515 | mxs_set_ssp_busclock(priv->clkid, mmc->clock / 1000); |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 516 | |
| 517 | switch (mmc->bus_width) { |
| 518 | case 1: |
| 519 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT; |
| 520 | break; |
| 521 | case 4: |
| 522 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT; |
| 523 | break; |
| 524 | case 8: |
| 525 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT; |
| 526 | break; |
| 527 | } |
| 528 | |
| 529 | /* Set the bus width */ |
| 530 | clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0, |
| 531 | SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth); |
| 532 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 533 | debug("MMC%d: Set %d bits bus width\n", mmc_get_blk_desc(mmc)->devnum, |
| 534 | mmc->bus_width); |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 535 | |
| 536 | return 0; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 537 | } |
| 538 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 539 | static int mxsmmc_init(struct udevice *dev) |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 540 | { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 541 | struct mxsmmc_priv *priv = dev_get_priv(dev); |
Otavio Salvador | 22f4ff9 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 542 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 543 | |
| 544 | /* Reset SSP */ |
Otavio Salvador | cbf0bf2 | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 545 | mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg); |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 546 | |
Otavio Salvador | 254f297 | 2013-01-22 15:01:02 +0000 | [diff] [blame] | 547 | /* Reconfigure the SSP block for MMC operation */ |
| 548 | writel(SSP_CTRL1_SSP_MODE_SD_MMC | |
| 549 | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS | |
| 550 | SSP_CTRL1_DMA_ENABLE | |
| 551 | SSP_CTRL1_POLARITY | |
| 552 | SSP_CTRL1_RECV_TIMEOUT_IRQ_EN | |
| 553 | SSP_CTRL1_DATA_CRC_IRQ_EN | |
| 554 | SSP_CTRL1_DATA_TIMEOUT_IRQ_EN | |
| 555 | SSP_CTRL1_RESP_TIMEOUT_IRQ_EN | |
| 556 | SSP_CTRL1_RESP_ERR_IRQ_EN, |
| 557 | &ssp_regs->hw_ssp_ctrl1_set); |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 558 | |
| 559 | /* Set initial bit clock 400 KHz */ |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 560 | mxs_set_ssp_busclock(priv->clkid, 400); |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 561 | |
| 562 | /* Send initial 74 clock cycles (185 us @ 400 KHz)*/ |
| 563 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set); |
| 564 | udelay(200); |
| 565 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr); |
| 566 | |
| 567 | return 0; |
| 568 | } |
| 569 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 570 | static int mxsmmc_probe(struct udevice *dev) |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 571 | { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 572 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 573 | struct mxsmmc_platdata *plat = dev_get_platdata(dev); |
| 574 | struct mxsmmc_priv *priv = dev_get_priv(dev); |
| 575 | struct blk_desc *bdesc; |
| 576 | struct mmc *mmc; |
| 577 | int ret, clkid; |
Marek Vasut | 3b46aa2 | 2013-01-11 03:19:14 +0000 | [diff] [blame] | 578 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 579 | debug("%s: probe\n", __func__); |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 580 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 581 | #if CONFIG_IS_ENABLED(OF_PLATDATA) |
| 582 | struct dtd_fsl_imx_mmc *dtplat = &plat->dtplat; |
| 583 | struct phandle_1_arg *p1a = &dtplat->clocks[0]; |
| 584 | |
| 585 | priv->buswidth = dtplat->bus_width; |
| 586 | priv->regs = (struct mxs_ssp_regs *)dtplat->reg[0]; |
| 587 | priv->dma_channel = dtplat->dmas[1]; |
| 588 | clkid = p1a->arg[0]; |
| 589 | plat->non_removable = dtplat->non_removable; |
| 590 | |
| 591 | debug("OF_PLATDATA: regs: 0x%p bw: %d clkid: %d non_removable: %d\n", |
| 592 | priv->regs, priv->buswidth, clkid, plat->non_removable); |
| 593 | #else |
| 594 | priv->regs = (struct mxs_ssp_regs *)plat->base; |
| 595 | priv->dma_channel = plat->dma_id; |
| 596 | clkid = plat->clk_id; |
| 597 | #endif |
| 598 | |
| 599 | #ifdef CONFIG_MX28 |
| 600 | priv->clkid = clkid - MXS_SSP_IMX28_CLKID_SSP0; |
| 601 | #else /* CONFIG_MX23 */ |
| 602 | priv->clkid = clkid - MXS_SSP_IMX23_CLKID_SSP0; |
| 603 | #endif |
| 604 | mmc = &plat->mmc; |
| 605 | mmc->cfg = &plat->cfg; |
| 606 | mmc->dev = dev; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 607 | |
Marek Vasut | 5796bed | 2012-03-15 18:33:21 +0000 | [diff] [blame] | 608 | priv->desc = mxs_dma_desc_alloc(); |
| 609 | if (!priv->desc) { |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 610 | printf("%s: Cannot allocate DMA descriptor\n", __func__); |
Marek Vasut | 5796bed | 2012-03-15 18:33:21 +0000 | [diff] [blame] | 611 | return -ENOMEM; |
| 612 | } |
| 613 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 614 | ret = mxs_dma_init_channel(priv->dma_channel); |
Marek Vasut | 93541b4 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 615 | if (ret) |
| 616 | return ret; |
| 617 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 618 | plat->cfg.name = "MXS MMC"; |
| 619 | plat->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 620 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 621 | plat->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | |
| 622 | MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 623 | |
| 624 | /* |
| 625 | * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz |
| 626 | * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)), |
| 627 | * CLOCK_DIVIDE has to be an even value from 2 to 254, and |
| 628 | * CLOCK_RATE could be any integer from 0 to 255. |
| 629 | */ |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 630 | plat->cfg.f_min = 400000; |
| 631 | plat->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + priv->clkid) * 1000 / 2; |
| 632 | plat->cfg.b_max = 0x20; |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 633 | |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 634 | bdesc = mmc_get_blk_desc(mmc); |
| 635 | if (!bdesc) { |
| 636 | printf("%s: No block device descriptor!\n", __func__); |
| 637 | return -ENODEV; |
| 638 | } |
| 639 | |
| 640 | if (plat->non_removable) |
| 641 | bdesc->removable = 0; |
| 642 | |
| 643 | ret = mxsmmc_init(dev); |
| 644 | if (ret) |
| 645 | printf("%s: MMC%d init error %d\n", __func__, |
| 646 | bdesc->devnum, ret); |
| 647 | |
| 648 | /* Set the initial clock speed */ |
| 649 | mmc_set_clock(mmc, 400000, MMC_CLK_ENABLE); |
| 650 | |
| 651 | upriv->mmc = mmc; |
| 652 | |
| 653 | return 0; |
| 654 | }; |
| 655 | |
| 656 | #if CONFIG_IS_ENABLED(BLK) |
| 657 | static int mxsmmc_bind(struct udevice *dev) |
| 658 | { |
| 659 | struct mxsmmc_platdata *plat = dev_get_platdata(dev); |
| 660 | |
| 661 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 662 | } |
| 663 | #endif |
| 664 | |
| 665 | static const struct dm_mmc_ops mxsmmc_ops = { |
| 666 | .get_cd = mxsmmc_get_cd, |
| 667 | .send_cmd = mxsmmc_send_cmd, |
| 668 | .set_ios = mxsmmc_set_ios, |
| 669 | }; |
| 670 | |
| 671 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 672 | static int mxsmmc_ofdata_to_platdata(struct udevice *bus) |
| 673 | { |
| 674 | struct mxsmmc_platdata *plat = bus->platdata; |
| 675 | u32 prop[2]; |
| 676 | int ret; |
| 677 | |
| 678 | plat->base = dev_read_addr(bus); |
| 679 | plat->buswidth = |
| 680 | dev_read_u32_default(bus, "bus-width", 1); |
| 681 | plat->non_removable = dev_read_bool(bus, "non-removable"); |
| 682 | |
| 683 | ret = dev_read_u32_array(bus, "dmas", prop, ARRAY_SIZE(prop)); |
| 684 | if (ret) { |
| 685 | printf("%s: Reading 'dmas' property failed!\n", __func__); |
| 686 | return ret; |
| 687 | } |
| 688 | plat->dma_id = prop[1]; |
| 689 | |
| 690 | ret = dev_read_u32_array(bus, "clocks", prop, ARRAY_SIZE(prop)); |
| 691 | if (ret) { |
| 692 | printf("%s: Reading 'clocks' property failed!\n", __func__); |
| 693 | return ret; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 694 | } |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 695 | plat->clk_id = prop[1]; |
| 696 | |
| 697 | debug("%s: base=0x%x, bus_width=%d %s dma_id=%d clk_id=%d\n", |
| 698 | __func__, (uint)plat->base, plat->buswidth, |
| 699 | plat->non_removable ? "non-removable" : NULL, |
| 700 | plat->dma_id, plat->clk_id); |
| 701 | |
Marek Vasut | de0a6bf | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 702 | return 0; |
| 703 | } |
Lukasz Majewski | aa92b86 | 2019-09-05 09:54:59 +0200 | [diff] [blame] | 704 | |
| 705 | static const struct udevice_id mxsmmc_ids[] = { |
| 706 | { .compatible = "fsl,imx23-mmc", }, |
| 707 | { .compatible = "fsl,imx28-mmc", }, |
| 708 | { /* sentinel */ } |
| 709 | }; |
| 710 | #endif |
| 711 | |
| 712 | U_BOOT_DRIVER(mxsmmc) = { |
| 713 | #ifdef CONFIG_MX28 |
| 714 | .name = "fsl_imx28_mmc", |
| 715 | #else /* CONFIG_MX23 */ |
| 716 | .name = "fsl_imx23_mmc", |
| 717 | #endif |
| 718 | .id = UCLASS_MMC, |
| 719 | #if CONFIG_IS_ENABLED(OF_CONTROL) && !CONFIG_IS_ENABLED(OF_PLATDATA) |
| 720 | .of_match = mxsmmc_ids, |
| 721 | .ofdata_to_platdata = mxsmmc_ofdata_to_platdata, |
| 722 | #endif |
| 723 | .ops = &mxsmmc_ops, |
| 724 | #if CONFIG_IS_ENABLED(BLK) |
| 725 | .bind = mxsmmc_bind, |
| 726 | #endif |
| 727 | .probe = mxsmmc_probe, |
| 728 | .priv_auto_alloc_size = sizeof(struct mxsmmc_priv), |
| 729 | .platdata_auto_alloc_size = sizeof(struct mxsmmc_platdata), |
| 730 | }; |
| 731 | |
| 732 | #endif /* CONFIG_DM_MMC */ |