Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Freescale i.MX28 SSP MMC driver |
| 3 | * |
| 4 | * Copyright (C) 2011 Marek Vasut <marek.vasut@gmail.com> |
| 5 | * on behalf of DENX Software Engineering GmbH |
| 6 | * |
| 7 | * Based on code from LTIB: |
| 8 | * (C) Copyright 2008-2010 Freescale Semiconductor, Inc. |
| 9 | * Terry Lv |
| 10 | * |
| 11 | * Copyright 2007, Freescale Semiconductor, Inc |
| 12 | * Andy Fleming |
| 13 | * |
| 14 | * Based vaguely on the pxa mmc code: |
| 15 | * (C) Copyright 2003 |
| 16 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 17 | * |
Wolfgang Denk | 1a45966 | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 18 | * SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 19 | */ |
| 20 | #include <common.h> |
| 21 | #include <malloc.h> |
| 22 | #include <mmc.h> |
| 23 | #include <asm/errno.h> |
| 24 | #include <asm/io.h> |
| 25 | #include <asm/arch/clock.h> |
| 26 | #include <asm/arch/imx-regs.h> |
| 27 | #include <asm/arch/sys_proto.h> |
Stefan Roese | 0499218 | 2013-04-09 21:06:07 +0000 | [diff] [blame] | 28 | #include <asm/imx-common/dma.h> |
Marek Vasut | 4e6d81d | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 29 | #include <bouncebuf.h> |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 30 | |
| 31 | struct mxsmmc_priv { |
| 32 | int id; |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 33 | struct mxs_ssp_regs *regs; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 34 | uint32_t buswidth; |
| 35 | int (*mmc_is_wp)(int); |
Marek Vasut | 90bc2bf | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 36 | int (*mmc_cd)(int); |
Marek Vasut | 3687c41 | 2012-03-15 18:33:21 +0000 | [diff] [blame] | 37 | struct mxs_dma_desc *desc; |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 38 | struct mmc_config cfg; /* mmc configuration */ |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 39 | }; |
| 40 | |
| 41 | #define MXSMMC_MAX_TIMEOUT 10000 |
Marek Vasut | 2025590 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 42 | #define MXSMMC_SMALL_TRANSFER 512 |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 43 | |
Marek Vasut | 90bc2bf | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 44 | static int mxsmmc_cd(struct mxsmmc_priv *priv) |
| 45 | { |
| 46 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 47 | |
| 48 | if (priv->mmc_cd) |
| 49 | return priv->mmc_cd(priv->id); |
| 50 | |
| 51 | return !(readl(&ssp_regs->hw_ssp_status) & SSP_STATUS_CARD_DETECT); |
| 52 | } |
| 53 | |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 54 | static int mxsmmc_send_cmd_pio(struct mxsmmc_priv *priv, struct mmc_data *data) |
| 55 | { |
| 56 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
| 57 | uint32_t *data_ptr; |
| 58 | int timeout = MXSMMC_MAX_TIMEOUT; |
| 59 | uint32_t reg; |
| 60 | uint32_t data_count = data->blocksize * data->blocks; |
| 61 | |
| 62 | if (data->flags & MMC_DATA_READ) { |
| 63 | data_ptr = (uint32_t *)data->dest; |
| 64 | while (data_count && --timeout) { |
| 65 | reg = readl(&ssp_regs->hw_ssp_status); |
| 66 | if (!(reg & SSP_STATUS_FIFO_EMPTY)) { |
| 67 | *data_ptr++ = readl(&ssp_regs->hw_ssp_data); |
| 68 | data_count -= 4; |
| 69 | timeout = MXSMMC_MAX_TIMEOUT; |
| 70 | } else |
| 71 | udelay(1000); |
| 72 | } |
| 73 | } else { |
| 74 | data_ptr = (uint32_t *)data->src; |
| 75 | timeout *= 100; |
| 76 | while (data_count && --timeout) { |
| 77 | reg = readl(&ssp_regs->hw_ssp_status); |
| 78 | if (!(reg & SSP_STATUS_FIFO_FULL)) { |
| 79 | writel(*data_ptr++, &ssp_regs->hw_ssp_data); |
| 80 | data_count -= 4; |
| 81 | timeout = MXSMMC_MAX_TIMEOUT; |
| 82 | } else |
| 83 | udelay(1000); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | return timeout ? 0 : COMM_ERR; |
| 88 | } |
Marek Vasut | 2025590 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 89 | |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 90 | static int mxsmmc_send_cmd_dma(struct mxsmmc_priv *priv, struct mmc_data *data) |
| 91 | { |
| 92 | uint32_t data_count = data->blocksize * data->blocks; |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 93 | int dmach; |
Marek Vasut | abb85be | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 94 | struct mxs_dma_desc *desc = priv->desc; |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 95 | void *addr; |
| 96 | unsigned int flags; |
| 97 | struct bounce_buffer bbstate; |
Marek Vasut | abb85be | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 98 | |
| 99 | memset(desc, 0, sizeof(struct mxs_dma_desc)); |
| 100 | desc->address = (dma_addr_t)desc; |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 101 | |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 102 | if (data->flags & MMC_DATA_READ) { |
| 103 | priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_WRITE; |
Marek Vasut | 4e6d81d | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 104 | addr = data->dest; |
| 105 | flags = GEN_BB_WRITE; |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 106 | } else { |
| 107 | priv->desc->cmd.data = MXS_DMA_DESC_COMMAND_DMA_READ; |
Marek Vasut | 4e6d81d | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 108 | addr = (void *)data->src; |
| 109 | flags = GEN_BB_READ; |
| 110 | } |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 111 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 112 | bounce_buffer_start(&bbstate, addr, data_count, flags); |
Marek Vasut | 4e6d81d | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 113 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 114 | priv->desc->cmd.address = (dma_addr_t)bbstate.bounce_buffer; |
Marek Vasut | 97ed12c | 2012-08-31 16:18:10 +0000 | [diff] [blame] | 115 | |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 116 | priv->desc->cmd.data |= MXS_DMA_DESC_IRQ | MXS_DMA_DESC_DEC_SEM | |
| 117 | (data_count << MXS_DMA_DESC_BYTES_OFFSET); |
| 118 | |
Marek Vasut | 3430e0b | 2013-02-23 02:42:58 +0000 | [diff] [blame] | 119 | dmach = MXS_DMA_CHANNEL_AHB_APBH_SSP0 + priv->id; |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 120 | mxs_dma_desc_append(dmach, priv->desc); |
Marek Vasut | 4e6d81d | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 121 | if (mxs_dma_go(dmach)) { |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 122 | bounce_buffer_stop(&bbstate); |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 123 | return COMM_ERR; |
Marek Vasut | 4e6d81d | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 124 | } |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 125 | |
Stephen Warren | 84d35b2 | 2012-11-06 11:27:29 +0000 | [diff] [blame] | 126 | bounce_buffer_stop(&bbstate); |
Marek Vasut | 4e6d81d | 2012-08-26 15:19:07 +0000 | [diff] [blame] | 127 | |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 128 | return 0; |
| 129 | } |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 130 | |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 131 | /* |
| 132 | * Sends a command out on the bus. Takes the mmc pointer, |
| 133 | * a command pointer, and an optional data pointer. |
| 134 | */ |
| 135 | static int |
| 136 | mxsmmc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 137 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 138 | struct mxsmmc_priv *priv = mmc->priv; |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 139 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 140 | uint32_t reg; |
| 141 | int timeout; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 142 | uint32_t ctrl0; |
Marek Vasut | 8698332 | 2012-07-06 21:25:55 +0000 | [diff] [blame] | 143 | int ret; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 144 | |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 145 | debug("MMC%d: CMD%d\n", mmc->block_dev.devnum, cmd->cmdidx); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 146 | |
| 147 | /* Check bus busy */ |
| 148 | timeout = MXSMMC_MAX_TIMEOUT; |
| 149 | while (--timeout) { |
| 150 | udelay(1000); |
| 151 | reg = readl(&ssp_regs->hw_ssp_status); |
| 152 | if (!(reg & |
| 153 | (SSP_STATUS_BUSY | SSP_STATUS_DATA_BUSY | |
| 154 | SSP_STATUS_CMD_BUSY))) { |
| 155 | break; |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | if (!timeout) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 160 | printf("MMC%d: Bus busy timeout!\n", mmc->block_dev.devnum); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 161 | return TIMEOUT; |
| 162 | } |
| 163 | |
| 164 | /* See if card is present */ |
Marek Vasut | 90bc2bf | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 165 | if (!mxsmmc_cd(priv)) { |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 166 | printf("MMC%d: No card detected!\n", mmc->block_dev.devnum); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 167 | return NO_CARD_ERR; |
| 168 | } |
| 169 | |
| 170 | /* Start building CTRL0 contents */ |
| 171 | ctrl0 = priv->buswidth; |
| 172 | |
| 173 | /* Set up command */ |
| 174 | if (!(cmd->resp_type & MMC_RSP_CRC)) |
| 175 | ctrl0 |= SSP_CTRL0_IGNORE_CRC; |
| 176 | if (cmd->resp_type & MMC_RSP_PRESENT) /* Need to get response */ |
| 177 | ctrl0 |= SSP_CTRL0_GET_RESP; |
| 178 | if (cmd->resp_type & MMC_RSP_136) /* It's a 136 bits response */ |
| 179 | ctrl0 |= SSP_CTRL0_LONG_RESP; |
| 180 | |
Marek Vasut | abb85be | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 181 | if (data && (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER)) |
| 182 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_clr); |
| 183 | else |
| 184 | writel(SSP_CTRL1_DMA_ENABLE, &ssp_regs->hw_ssp_ctrl1_set); |
| 185 | |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 186 | /* Command index */ |
| 187 | reg = readl(&ssp_regs->hw_ssp_cmd0); |
| 188 | reg &= ~(SSP_CMD0_CMD_MASK | SSP_CMD0_APPEND_8CYC); |
| 189 | reg |= cmd->cmdidx << SSP_CMD0_CMD_OFFSET; |
| 190 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 191 | reg |= SSP_CMD0_APPEND_8CYC; |
| 192 | writel(reg, &ssp_regs->hw_ssp_cmd0); |
| 193 | |
| 194 | /* Command argument */ |
| 195 | writel(cmd->cmdarg, &ssp_regs->hw_ssp_cmd1); |
| 196 | |
| 197 | /* Set up data */ |
| 198 | if (data) { |
| 199 | /* READ or WRITE */ |
| 200 | if (data->flags & MMC_DATA_READ) { |
| 201 | ctrl0 |= SSP_CTRL0_READ; |
Marek Vasut | c7527b7 | 2012-05-01 11:09:52 +0000 | [diff] [blame] | 202 | } else if (priv->mmc_is_wp && |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 203 | priv->mmc_is_wp(mmc->block_dev.devnum)) { |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 204 | printf("MMC%d: Can not write a locked card!\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 205 | mmc->block_dev.devnum); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 206 | return UNUSABLE_ERR; |
| 207 | } |
| 208 | |
| 209 | ctrl0 |= SSP_CTRL0_DATA_XFER; |
Marek Vasut | e5b380a | 2013-01-22 15:01:04 +0000 | [diff] [blame] | 210 | |
| 211 | reg = data->blocksize * data->blocks; |
| 212 | #if defined(CONFIG_MX23) |
| 213 | ctrl0 |= reg & SSP_CTRL0_XFER_COUNT_MASK; |
| 214 | |
| 215 | clrsetbits_le32(&ssp_regs->hw_ssp_cmd0, |
| 216 | SSP_CMD0_BLOCK_SIZE_MASK | SSP_CMD0_BLOCK_COUNT_MASK, |
| 217 | ((data->blocks - 1) << SSP_CMD0_BLOCK_COUNT_OFFSET) | |
| 218 | ((ffs(data->blocksize) - 1) << |
| 219 | SSP_CMD0_BLOCK_SIZE_OFFSET)); |
| 220 | #elif defined(CONFIG_MX28) |
| 221 | writel(reg, &ssp_regs->hw_ssp_xfer_size); |
| 222 | |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 223 | reg = ((data->blocks - 1) << |
| 224 | SSP_BLOCK_SIZE_BLOCK_COUNT_OFFSET) | |
| 225 | ((ffs(data->blocksize) - 1) << |
| 226 | SSP_BLOCK_SIZE_BLOCK_SIZE_OFFSET); |
| 227 | writel(reg, &ssp_regs->hw_ssp_block_size); |
Marek Vasut | e5b380a | 2013-01-22 15:01:04 +0000 | [diff] [blame] | 228 | #endif |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | /* Kick off the command */ |
| 232 | ctrl0 |= SSP_CTRL0_WAIT_FOR_IRQ | SSP_CTRL0_ENABLE | SSP_CTRL0_RUN; |
| 233 | writel(ctrl0, &ssp_regs->hw_ssp_ctrl0); |
| 234 | |
| 235 | /* Wait for the command to complete */ |
| 236 | timeout = MXSMMC_MAX_TIMEOUT; |
| 237 | while (--timeout) { |
| 238 | udelay(1000); |
| 239 | reg = readl(&ssp_regs->hw_ssp_status); |
| 240 | if (!(reg & SSP_STATUS_CMD_BUSY)) |
| 241 | break; |
| 242 | } |
| 243 | |
| 244 | if (!timeout) { |
| 245 | printf("MMC%d: Command %d busy\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 246 | mmc->block_dev.devnum, cmd->cmdidx); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 247 | return TIMEOUT; |
| 248 | } |
| 249 | |
| 250 | /* Check command timeout */ |
| 251 | if (reg & SSP_STATUS_RESP_TIMEOUT) { |
| 252 | printf("MMC%d: Command %d timeout (status 0x%08x)\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 253 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 254 | return TIMEOUT; |
| 255 | } |
| 256 | |
| 257 | /* Check command errors */ |
| 258 | if (reg & (SSP_STATUS_RESP_CRC_ERR | SSP_STATUS_RESP_ERR)) { |
| 259 | printf("MMC%d: Command %d error (status 0x%08x)!\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 260 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 261 | return COMM_ERR; |
| 262 | } |
| 263 | |
| 264 | /* Copy response to response buffer */ |
| 265 | if (cmd->resp_type & MMC_RSP_136) { |
| 266 | cmd->response[3] = readl(&ssp_regs->hw_ssp_sdresp0); |
| 267 | cmd->response[2] = readl(&ssp_regs->hw_ssp_sdresp1); |
| 268 | cmd->response[1] = readl(&ssp_regs->hw_ssp_sdresp2); |
| 269 | cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp3); |
| 270 | } else |
| 271 | cmd->response[0] = readl(&ssp_regs->hw_ssp_sdresp0); |
| 272 | |
| 273 | /* Return if no data to process */ |
| 274 | if (!data) |
| 275 | return 0; |
| 276 | |
Marek Vasut | 2025590 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 277 | if (data->blocksize * data->blocks < MXSMMC_SMALL_TRANSFER) { |
Marek Vasut | 2025590 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 278 | ret = mxsmmc_send_cmd_pio(priv, data); |
| 279 | if (ret) { |
| 280 | printf("MMC%d: Data timeout with command %d " |
| 281 | "(status 0x%08x)!\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 282 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
Marek Vasut | 2025590 | 2012-07-06 21:25:56 +0000 | [diff] [blame] | 283 | return ret; |
| 284 | } |
Marek Vasut | abb85be | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 285 | } else { |
| 286 | ret = mxsmmc_send_cmd_dma(priv, data); |
| 287 | if (ret) { |
| 288 | printf("MMC%d: DMA transfer failed\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 289 | mmc->block_dev.devnum); |
Marek Vasut | abb85be | 2012-07-06 21:25:57 +0000 | [diff] [blame] | 290 | return ret; |
| 291 | } |
Marek Vasut | 4cc76c6 | 2012-04-05 03:30:35 +0000 | [diff] [blame] | 292 | } |
Marek Vasut | 3687c41 | 2012-03-15 18:33:21 +0000 | [diff] [blame] | 293 | |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 294 | /* Check data errors */ |
| 295 | reg = readl(&ssp_regs->hw_ssp_status); |
| 296 | if (reg & |
| 297 | (SSP_STATUS_TIMEOUT | SSP_STATUS_DATA_CRC_ERR | |
| 298 | SSP_STATUS_FIFO_OVRFLW | SSP_STATUS_FIFO_UNDRFLW)) { |
| 299 | printf("MMC%d: Data error with command %d (status 0x%08x)!\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 300 | mmc->block_dev.devnum, cmd->cmdidx, reg); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 301 | return COMM_ERR; |
| 302 | } |
| 303 | |
| 304 | return 0; |
| 305 | } |
| 306 | |
| 307 | static void mxsmmc_set_ios(struct mmc *mmc) |
| 308 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 309 | struct mxsmmc_priv *priv = mmc->priv; |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 310 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 311 | |
| 312 | /* Set the clock speed */ |
| 313 | if (mmc->clock) |
Otavio Salvador | bf48fcb | 2013-01-11 03:19:03 +0000 | [diff] [blame] | 314 | mxs_set_ssp_busclock(priv->id, mmc->clock / 1000); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 315 | |
| 316 | switch (mmc->bus_width) { |
| 317 | case 1: |
| 318 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_ONE_BIT; |
| 319 | break; |
| 320 | case 4: |
| 321 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_FOUR_BIT; |
| 322 | break; |
| 323 | case 8: |
| 324 | priv->buswidth = SSP_CTRL0_BUS_WIDTH_EIGHT_BIT; |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | /* Set the bus width */ |
| 329 | clrsetbits_le32(&ssp_regs->hw_ssp_ctrl0, |
| 330 | SSP_CTRL0_BUS_WIDTH_MASK, priv->buswidth); |
| 331 | |
| 332 | debug("MMC%d: Set %d bits bus width\n", |
Simon Glass | bcce53d | 2016-02-29 15:25:51 -0700 | [diff] [blame] | 333 | mmc->block_dev.devnum, mmc->bus_width); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 334 | } |
| 335 | |
| 336 | static int mxsmmc_init(struct mmc *mmc) |
| 337 | { |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 338 | struct mxsmmc_priv *priv = mmc->priv; |
Otavio Salvador | 9c47114 | 2012-08-05 09:05:31 +0000 | [diff] [blame] | 339 | struct mxs_ssp_regs *ssp_regs = priv->regs; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 340 | |
| 341 | /* Reset SSP */ |
Otavio Salvador | fa7a51c | 2012-08-13 09:53:12 +0000 | [diff] [blame] | 342 | mxs_reset_block(&ssp_regs->hw_ssp_ctrl0_reg); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 343 | |
Otavio Salvador | 8000d8a | 2013-01-22 15:01:02 +0000 | [diff] [blame] | 344 | /* Reconfigure the SSP block for MMC operation */ |
| 345 | writel(SSP_CTRL1_SSP_MODE_SD_MMC | |
| 346 | SSP_CTRL1_WORD_LENGTH_EIGHT_BITS | |
| 347 | SSP_CTRL1_DMA_ENABLE | |
| 348 | SSP_CTRL1_POLARITY | |
| 349 | SSP_CTRL1_RECV_TIMEOUT_IRQ_EN | |
| 350 | SSP_CTRL1_DATA_CRC_IRQ_EN | |
| 351 | SSP_CTRL1_DATA_TIMEOUT_IRQ_EN | |
| 352 | SSP_CTRL1_RESP_TIMEOUT_IRQ_EN | |
| 353 | SSP_CTRL1_RESP_ERR_IRQ_EN, |
| 354 | &ssp_regs->hw_ssp_ctrl1_set); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 355 | |
| 356 | /* Set initial bit clock 400 KHz */ |
Otavio Salvador | bf48fcb | 2013-01-11 03:19:03 +0000 | [diff] [blame] | 357 | mxs_set_ssp_busclock(priv->id, 400); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 358 | |
| 359 | /* Send initial 74 clock cycles (185 us @ 400 KHz)*/ |
| 360 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_set); |
| 361 | udelay(200); |
| 362 | writel(SSP_CMD0_CONT_CLKING_EN, &ssp_regs->hw_ssp_cmd0_clr); |
| 363 | |
| 364 | return 0; |
| 365 | } |
| 366 | |
Pantelis Antoniou | ab769f2 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 367 | static const struct mmc_ops mxsmmc_ops = { |
| 368 | .send_cmd = mxsmmc_send_cmd, |
| 369 | .set_ios = mxsmmc_set_ios, |
| 370 | .init = mxsmmc_init, |
| 371 | }; |
| 372 | |
Marek Vasut | 90bc2bf | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 373 | int mxsmmc_initialize(bd_t *bis, int id, int (*wp)(int), int (*cd)(int)) |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 374 | { |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 375 | struct mmc *mmc = NULL; |
| 376 | struct mxsmmc_priv *priv = NULL; |
Marek Vasut | 96666a3 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 377 | int ret; |
Marek Vasut | 3430e0b | 2013-02-23 02:42:58 +0000 | [diff] [blame] | 378 | const unsigned int mxsmmc_clk_id = mxs_ssp_clock_by_bus(id); |
Marek Vasut | 1a3c5ff | 2013-01-11 03:19:14 +0000 | [diff] [blame] | 379 | |
Marek Vasut | 3430e0b | 2013-02-23 02:42:58 +0000 | [diff] [blame] | 380 | if (!mxs_ssp_bus_id_valid(id)) |
Marek Vasut | 1a3c5ff | 2013-01-11 03:19:14 +0000 | [diff] [blame] | 381 | return -ENODEV; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 382 | |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 383 | priv = malloc(sizeof(struct mxsmmc_priv)); |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 384 | if (!priv) |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 385 | return -ENOMEM; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 386 | |
Marek Vasut | 3687c41 | 2012-03-15 18:33:21 +0000 | [diff] [blame] | 387 | priv->desc = mxs_dma_desc_alloc(); |
| 388 | if (!priv->desc) { |
| 389 | free(priv); |
Marek Vasut | 3687c41 | 2012-03-15 18:33:21 +0000 | [diff] [blame] | 390 | return -ENOMEM; |
| 391 | } |
| 392 | |
Marek Vasut | 3430e0b | 2013-02-23 02:42:58 +0000 | [diff] [blame] | 393 | ret = mxs_dma_init_channel(MXS_DMA_CHANNEL_AHB_APBH_SSP0 + id); |
Marek Vasut | 96666a3 | 2012-04-08 17:34:46 +0000 | [diff] [blame] | 394 | if (ret) |
| 395 | return ret; |
| 396 | |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 397 | priv->mmc_is_wp = wp; |
Marek Vasut | 90bc2bf | 2013-01-22 15:01:03 +0000 | [diff] [blame] | 398 | priv->mmc_cd = cd; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 399 | priv->id = id; |
Marek Vasut | 14e26bc | 2013-01-11 03:19:02 +0000 | [diff] [blame] | 400 | priv->regs = mxs_ssp_regs_by_bus(id); |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 401 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 402 | priv->cfg.name = "MXS MMC"; |
| 403 | priv->cfg.ops = &mxsmmc_ops; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 404 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 405 | priv->cfg.voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 406 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 407 | priv->cfg.host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | |
Rob Herring | 5a20397 | 2015-03-23 17:56:59 -0500 | [diff] [blame] | 408 | MMC_MODE_HS_52MHz | MMC_MODE_HS; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 409 | |
| 410 | /* |
| 411 | * SSPCLK = 480 * 18 / 29 / 1 = 297.731 MHz |
| 412 | * SSP bit rate = SSPCLK / (CLOCK_DIVIDE * (1 + CLOCK_RATE)), |
| 413 | * CLOCK_DIVIDE has to be an even value from 2 to 254, and |
| 414 | * CLOCK_RATE could be any integer from 0 to 255. |
| 415 | */ |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 416 | priv->cfg.f_min = 400000; |
| 417 | priv->cfg.f_max = mxc_get_clock(MXC_SSP0_CLK + mxsmmc_clk_id) * 1000 / 2; |
| 418 | priv->cfg.b_max = 0x20; |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 419 | |
Pantelis Antoniou | 93bfd61 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 420 | mmc = mmc_create(&priv->cfg, priv); |
| 421 | if (mmc == NULL) { |
| 422 | mxs_dma_desc_free(priv->desc); |
| 423 | free(priv); |
| 424 | return -ENOMEM; |
| 425 | } |
Marek Vasut | 71a758e1 | 2011-11-08 23:18:09 +0000 | [diff] [blame] | 426 | return 0; |
| 427 | } |