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