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