Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 1 | /* |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 2 | * Copyright 2007, 2010-2011 Freescale Semiconductor, Inc |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 3 | * Andy Fleming |
| 4 | * |
| 5 | * Based vaguely on the pxa mmc code: |
| 6 | * (C) Copyright 2003 |
| 7 | * Kyle Harris, Nexus Technologies, Inc. kharris@nexus-tech.net |
| 8 | * |
| 9 | * See file CREDITS for list of people who contributed to this |
| 10 | * project. |
| 11 | * |
| 12 | * This program is free software; you can redistribute it and/or |
| 13 | * modify it under the terms of the GNU General Public License as |
| 14 | * published by the Free Software Foundation; either version 2 of |
| 15 | * the License, or (at your option) any later version. |
| 16 | * |
| 17 | * This program is distributed in the hope that it will be useful, |
| 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 20 | * GNU General Public License for more details. |
| 21 | * |
| 22 | * You should have received a copy of the GNU General Public License |
| 23 | * along with this program; if not, write to the Free Software |
| 24 | * Foundation, Inc., 59 Temple Place, Suite 330, Boston, |
| 25 | * MA 02111-1307 USA |
| 26 | */ |
| 27 | |
| 28 | #include <config.h> |
| 29 | #include <common.h> |
| 30 | #include <command.h> |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 31 | #include <hwconfig.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 32 | #include <mmc.h> |
| 33 | #include <part.h> |
| 34 | #include <malloc.h> |
| 35 | #include <mmc.h> |
| 36 | #include <fsl_esdhc.h> |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 37 | #include <fdt_support.h> |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 38 | #include <asm/io.h> |
| 39 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 40 | DECLARE_GLOBAL_DATA_PTR; |
| 41 | |
| 42 | struct fsl_esdhc { |
| 43 | uint dsaddr; |
| 44 | uint blkattr; |
| 45 | uint cmdarg; |
| 46 | uint xfertyp; |
| 47 | uint cmdrsp0; |
| 48 | uint cmdrsp1; |
| 49 | uint cmdrsp2; |
| 50 | uint cmdrsp3; |
| 51 | uint datport; |
| 52 | uint prsstat; |
| 53 | uint proctl; |
| 54 | uint sysctl; |
| 55 | uint irqstat; |
| 56 | uint irqstaten; |
| 57 | uint irqsigen; |
| 58 | uint autoc12err; |
| 59 | uint hostcapblt; |
| 60 | uint wml; |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame^] | 61 | uint mixctrl; |
| 62 | char reserved1[4]; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 63 | uint fevt; |
| 64 | char reserved2[168]; |
| 65 | uint hostver; |
| 66 | char reserved3[780]; |
| 67 | uint scr; |
| 68 | }; |
| 69 | |
| 70 | /* Return the XFERTYP flags for a given command and data packet */ |
| 71 | uint esdhc_xfertyp(struct mmc_cmd *cmd, struct mmc_data *data) |
| 72 | { |
| 73 | uint xfertyp = 0; |
| 74 | |
| 75 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 76 | xfertyp |= XFERTYP_DPSEL; |
| 77 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 78 | xfertyp |= XFERTYP_DMAEN; |
| 79 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 80 | if (data->blocks > 1) { |
| 81 | xfertyp |= XFERTYP_MSBSEL; |
| 82 | xfertyp |= XFERTYP_BCEN; |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 83 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 84 | xfertyp |= XFERTYP_AC12EN; |
| 85 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | if (data->flags & MMC_DATA_READ) |
| 89 | xfertyp |= XFERTYP_DTDSEL; |
| 90 | } |
| 91 | |
| 92 | if (cmd->resp_type & MMC_RSP_CRC) |
| 93 | xfertyp |= XFERTYP_CCCEN; |
| 94 | if (cmd->resp_type & MMC_RSP_OPCODE) |
| 95 | xfertyp |= XFERTYP_CICEN; |
| 96 | if (cmd->resp_type & MMC_RSP_136) |
| 97 | xfertyp |= XFERTYP_RSPTYP_136; |
| 98 | else if (cmd->resp_type & MMC_RSP_BUSY) |
| 99 | xfertyp |= XFERTYP_RSPTYP_48_BUSY; |
| 100 | else if (cmd->resp_type & MMC_RSP_PRESENT) |
| 101 | xfertyp |= XFERTYP_RSPTYP_48; |
| 102 | |
Jason Liu | 4571de3 | 2011-03-22 01:32:31 +0000 | [diff] [blame] | 103 | #ifdef CONFIG_MX53 |
| 104 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 105 | xfertyp |= XFERTYP_CMDTYP_ABORT; |
| 106 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 107 | return XFERTYP_CMD(cmd->cmdidx) | xfertyp; |
| 108 | } |
| 109 | |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 110 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 111 | /* |
| 112 | * PIO Read/Write Mode reduce the performace as DMA is not used in this mode. |
| 113 | */ |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 114 | static void |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 115 | esdhc_pio_read_write(struct mmc *mmc, struct mmc_data *data) |
| 116 | { |
| 117 | struct fsl_esdhc *regs = mmc->priv; |
| 118 | uint blocks; |
| 119 | char *buffer; |
| 120 | uint databuf; |
| 121 | uint size; |
| 122 | uint irqstat; |
| 123 | uint timeout; |
| 124 | |
| 125 | if (data->flags & MMC_DATA_READ) { |
| 126 | blocks = data->blocks; |
| 127 | buffer = data->dest; |
| 128 | while (blocks) { |
| 129 | timeout = PIO_TIMEOUT; |
| 130 | size = data->blocksize; |
| 131 | irqstat = esdhc_read32(®s->irqstat); |
| 132 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BREN) |
| 133 | && --timeout); |
| 134 | if (timeout <= 0) { |
| 135 | printf("\nData Read Failed in PIO Mode."); |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 136 | return; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 137 | } |
| 138 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 139 | udelay(100); /* Wait before last byte transfer complete */ |
| 140 | irqstat = esdhc_read32(®s->irqstat); |
| 141 | databuf = in_le32(®s->datport); |
| 142 | *((uint *)buffer) = databuf; |
| 143 | buffer += 4; |
| 144 | size -= 4; |
| 145 | } |
| 146 | blocks--; |
| 147 | } |
| 148 | } else { |
| 149 | blocks = data->blocks; |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 150 | buffer = (char *)data->src; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 151 | while (blocks) { |
| 152 | timeout = PIO_TIMEOUT; |
| 153 | size = data->blocksize; |
| 154 | irqstat = esdhc_read32(®s->irqstat); |
| 155 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_BWEN) |
| 156 | && --timeout); |
| 157 | if (timeout <= 0) { |
| 158 | printf("\nData Write Failed in PIO Mode."); |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 159 | return; |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 160 | } |
| 161 | while (size && (!(irqstat & IRQSTAT_TC))) { |
| 162 | udelay(100); /* Wait before last byte transfer complete */ |
| 163 | databuf = *((uint *)buffer); |
| 164 | buffer += 4; |
| 165 | size -= 4; |
| 166 | irqstat = esdhc_read32(®s->irqstat); |
| 167 | out_le32(®s->datport, databuf); |
| 168 | } |
| 169 | blocks--; |
| 170 | } |
| 171 | } |
| 172 | } |
| 173 | #endif |
| 174 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 175 | static int esdhc_setup_data(struct mmc *mmc, struct mmc_data *data) |
| 176 | { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 177 | int timeout; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 178 | struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv; |
| 179 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 180 | #ifndef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 181 | uint wml_value; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 182 | |
| 183 | wml_value = data->blocksize/4; |
| 184 | |
| 185 | if (data->flags & MMC_DATA_READ) { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 186 | if (wml_value > WML_RD_WML_MAX) |
| 187 | wml_value = WML_RD_WML_MAX_VAL; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 188 | |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 189 | esdhc_clrsetbits32(®s->wml, WML_RD_WML_MASK, wml_value); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 190 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 191 | } else { |
Priyanka Jain | 32c8cfb | 2011-02-09 09:24:10 +0530 | [diff] [blame] | 192 | if (wml_value > WML_WR_WML_MAX) |
| 193 | wml_value = WML_WR_WML_MAX_VAL; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 194 | if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) { |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 195 | printf("\nThe SD card is locked. Can not write to a locked card.\n\n"); |
| 196 | return TIMEOUT; |
| 197 | } |
Roy Zang | ab467c5 | 2010-02-09 18:23:33 +0800 | [diff] [blame] | 198 | |
| 199 | esdhc_clrsetbits32(®s->wml, WML_WR_WML_MASK, |
| 200 | wml_value << 16); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 201 | esdhc_write32(®s->dsaddr, (u32)data->src); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 202 | } |
Wolfgang Denk | 7b43db9 | 2010-05-09 23:52:59 +0200 | [diff] [blame] | 203 | #else /* CONFIG_SYS_FSL_ESDHC_USE_PIO */ |
| 204 | if (!(data->flags & MMC_DATA_READ)) { |
| 205 | if ((esdhc_read32(®s->prsstat) & PRSSTAT_WPSPL) == 0) { |
| 206 | printf("\nThe SD card is locked. " |
| 207 | "Can not write to a locked card.\n\n"); |
| 208 | return TIMEOUT; |
| 209 | } |
| 210 | esdhc_write32(®s->dsaddr, (u32)data->src); |
| 211 | } else |
| 212 | esdhc_write32(®s->dsaddr, (u32)data->dest); |
| 213 | #endif /* CONFIG_SYS_FSL_ESDHC_USE_PIO */ |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 214 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 215 | esdhc_write32(®s->blkattr, data->blocks << 16 | data->blocksize); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 216 | |
| 217 | /* Calculate the timeout period for data transactions */ |
Priyanka Jain | b71ea33 | 2011-03-03 09:18:56 +0530 | [diff] [blame] | 218 | /* |
| 219 | * 1)Timeout period = (2^(timeout+13)) SD Clock cycles |
| 220 | * 2)Timeout period should be minimum 0.250sec as per SD Card spec |
| 221 | * So, Number of SD Clock cycles for 0.25sec should be minimum |
| 222 | * (SD Clock/sec * 0.25 sec) SD Clock cycles |
| 223 | * = (mmc->tran_speed * 1/4) SD Clock cycles |
| 224 | * As 1) >= 2) |
| 225 | * => (2^(timeout+13)) >= mmc->tran_speed * 1/4 |
| 226 | * Taking log2 both the sides |
| 227 | * => timeout + 13 >= log2(mmc->tran_speed/4) |
| 228 | * Rounding up to next power of 2 |
| 229 | * => timeout + 13 = log2(mmc->tran_speed/4) + 1 |
| 230 | * => timeout + 13 = fls(mmc->tran_speed/4) |
| 231 | */ |
| 232 | timeout = fls(mmc->tran_speed/4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 233 | timeout -= 13; |
| 234 | |
| 235 | if (timeout > 14) |
| 236 | timeout = 14; |
| 237 | |
| 238 | if (timeout < 0) |
| 239 | timeout = 0; |
| 240 | |
Kumar Gala | 5103a03 | 2011-01-29 15:36:10 -0600 | [diff] [blame] | 241 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC_A001 |
| 242 | if ((timeout == 4) || (timeout == 8) || (timeout == 12)) |
| 243 | timeout++; |
| 244 | #endif |
| 245 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 246 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, timeout << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 247 | |
| 248 | return 0; |
| 249 | } |
| 250 | |
| 251 | |
| 252 | /* |
| 253 | * Sends a command out on the bus. Takes the mmc pointer, |
| 254 | * a command pointer, and an optional data pointer. |
| 255 | */ |
| 256 | static int |
| 257 | esdhc_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, struct mmc_data *data) |
| 258 | { |
| 259 | uint xfertyp; |
| 260 | uint irqstat; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 261 | struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv; |
| 262 | volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 263 | |
Jerry Huang | d621da0 | 2011-01-06 23:42:19 -0600 | [diff] [blame] | 264 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC111 |
| 265 | if (cmd->cmdidx == MMC_CMD_STOP_TRANSMISSION) |
| 266 | return 0; |
| 267 | #endif |
| 268 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 269 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 270 | |
| 271 | sync(); |
| 272 | |
| 273 | /* Wait for the bus to be idle */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 274 | while ((esdhc_read32(®s->prsstat) & PRSSTAT_CICHB) || |
| 275 | (esdhc_read32(®s->prsstat) & PRSSTAT_CIDHB)) |
| 276 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 277 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 278 | while (esdhc_read32(®s->prsstat) & PRSSTAT_DLA) |
| 279 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 280 | |
| 281 | /* Wait at least 8 SD clock cycles before the next command */ |
| 282 | /* |
| 283 | * Note: This is way more than 8 cycles, but 1ms seems to |
| 284 | * resolve timing issues with some cards |
| 285 | */ |
| 286 | udelay(1000); |
| 287 | |
| 288 | /* Set up for a data transfer if we have one */ |
| 289 | if (data) { |
| 290 | int err; |
| 291 | |
| 292 | err = esdhc_setup_data(mmc, data); |
| 293 | if(err) |
| 294 | return err; |
| 295 | } |
| 296 | |
| 297 | /* Figure out the transfer arguments */ |
| 298 | xfertyp = esdhc_xfertyp(cmd, data); |
| 299 | |
| 300 | /* Send the command */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 301 | esdhc_write32(®s->cmdarg, cmd->cmdarg); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame^] | 302 | #if defined(CONFIG_FSL_USDHC) |
| 303 | esdhc_write32(®s->mixctrl, |
| 304 | (esdhc_read32(®s->mixctrl) & 0xFFFFFF80) | (xfertyp & 0x7F)); |
| 305 | esdhc_write32(®s->xfertyp, xfertyp & 0xFFFF0000); |
| 306 | #else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 307 | esdhc_write32(®s->xfertyp, xfertyp); |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame^] | 308 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 309 | /* Wait for the command to complete */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 310 | while (!(esdhc_read32(®s->irqstat) & IRQSTAT_CC)) |
| 311 | ; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 312 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 313 | irqstat = esdhc_read32(®s->irqstat); |
| 314 | esdhc_write32(®s->irqstat, irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 315 | |
| 316 | if (irqstat & CMD_ERR) |
| 317 | return COMM_ERR; |
| 318 | |
| 319 | if (irqstat & IRQSTAT_CTOE) |
| 320 | return TIMEOUT; |
| 321 | |
| 322 | /* Copy the response to the response buffer */ |
| 323 | if (cmd->resp_type & MMC_RSP_136) { |
| 324 | u32 cmdrsp3, cmdrsp2, cmdrsp1, cmdrsp0; |
| 325 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 326 | cmdrsp3 = esdhc_read32(®s->cmdrsp3); |
| 327 | cmdrsp2 = esdhc_read32(®s->cmdrsp2); |
| 328 | cmdrsp1 = esdhc_read32(®s->cmdrsp1); |
| 329 | cmdrsp0 = esdhc_read32(®s->cmdrsp0); |
Rabin Vincent | 998be3d | 2009-04-05 13:30:56 +0530 | [diff] [blame] | 330 | cmd->response[0] = (cmdrsp3 << 8) | (cmdrsp2 >> 24); |
| 331 | cmd->response[1] = (cmdrsp2 << 8) | (cmdrsp1 >> 24); |
| 332 | cmd->response[2] = (cmdrsp1 << 8) | (cmdrsp0 >> 24); |
| 333 | cmd->response[3] = (cmdrsp0 << 8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 334 | } else |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 335 | cmd->response[0] = esdhc_read32(®s->cmdrsp0); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 336 | |
| 337 | /* Wait until all of the blocks are transferred */ |
| 338 | if (data) { |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 339 | #ifdef CONFIG_SYS_FSL_ESDHC_USE_PIO |
| 340 | esdhc_pio_read_write(mmc, data); |
| 341 | #else |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 342 | do { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 343 | irqstat = esdhc_read32(®s->irqstat); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 344 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 345 | if (irqstat & IRQSTAT_DTOE) |
| 346 | return TIMEOUT; |
Frans Meulenbroeks | 63fb5a7 | 2010-07-31 04:45:18 +0000 | [diff] [blame] | 347 | |
| 348 | if (irqstat & DATA_ERR) |
| 349 | return COMM_ERR; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 350 | } while (!(irqstat & IRQSTAT_TC) && |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 351 | (esdhc_read32(®s->prsstat) & PRSSTAT_DLA)); |
Dipen Dudhat | 77c1458 | 2009-10-05 15:41:58 +0530 | [diff] [blame] | 352 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 353 | } |
| 354 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 355 | esdhc_write32(®s->irqstat, -1); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 356 | |
| 357 | return 0; |
| 358 | } |
| 359 | |
| 360 | void set_sysctl(struct mmc *mmc, uint clock) |
| 361 | { |
| 362 | int sdhc_clk = gd->sdhc_clk; |
| 363 | int div, pre_div; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 364 | struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv; |
| 365 | volatile struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 366 | uint clk; |
| 367 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 368 | if (clock < mmc->f_min) |
| 369 | clock = mmc->f_min; |
| 370 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 371 | if (sdhc_clk / 16 > clock) { |
| 372 | for (pre_div = 2; pre_div < 256; pre_div *= 2) |
| 373 | if ((sdhc_clk / pre_div) <= (clock * 16)) |
| 374 | break; |
| 375 | } else |
| 376 | pre_div = 2; |
| 377 | |
| 378 | for (div = 1; div <= 16; div++) |
| 379 | if ((sdhc_clk / (div * pre_div)) <= clock) |
| 380 | break; |
| 381 | |
| 382 | pre_div >>= 1; |
| 383 | div -= 1; |
| 384 | |
| 385 | clk = (pre_div << 8) | (div << 4); |
| 386 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 387 | esdhc_clrbits32(®s->sysctl, SYSCTL_CKEN); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 388 | |
| 389 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_CLOCK_MASK, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 390 | |
| 391 | udelay(10000); |
| 392 | |
Kumar Gala | cc4d122 | 2010-03-18 15:51:05 -0500 | [diff] [blame] | 393 | clk = SYSCTL_PEREN | SYSCTL_CKEN; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 394 | |
| 395 | esdhc_setbits32(®s->sysctl, clk); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 396 | } |
| 397 | |
| 398 | static void esdhc_set_ios(struct mmc *mmc) |
| 399 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 400 | struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv; |
| 401 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 402 | |
| 403 | /* Set the clock speed */ |
| 404 | set_sysctl(mmc, mmc->clock); |
| 405 | |
| 406 | /* Set the bus width */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 407 | esdhc_clrbits32(®s->proctl, PROCTL_DTW_4 | PROCTL_DTW_8); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 408 | |
| 409 | if (mmc->bus_width == 4) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 410 | esdhc_setbits32(®s->proctl, PROCTL_DTW_4); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 411 | else if (mmc->bus_width == 8) |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 412 | esdhc_setbits32(®s->proctl, PROCTL_DTW_8); |
| 413 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 414 | } |
| 415 | |
| 416 | static int esdhc_init(struct mmc *mmc) |
| 417 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 418 | struct fsl_esdhc_cfg *cfg = (struct fsl_esdhc_cfg *)mmc->priv; |
| 419 | struct fsl_esdhc *regs = (struct fsl_esdhc *)cfg->esdhc_base; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 420 | int timeout = 1000; |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 421 | int ret = 0; |
| 422 | u8 card_absent; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 423 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 424 | /* Reset the entire host controller */ |
| 425 | esdhc_write32(®s->sysctl, SYSCTL_RSTA); |
| 426 | |
| 427 | /* Wait until the controller is available */ |
| 428 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 429 | udelay(1000); |
| 430 | |
P.V.Suresh | 2c1764e | 2010-12-04 10:37:23 +0530 | [diff] [blame] | 431 | /* Enable cache snooping */ |
| 432 | if (cfg && !cfg->no_snoop) |
| 433 | esdhc_write32(®s->scr, 0x00000040); |
| 434 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 435 | esdhc_write32(®s->sysctl, SYSCTL_HCKEN | SYSCTL_IPGEN); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 436 | |
| 437 | /* Set the initial clock speed */ |
Jerry Huang | 4a6ee17 | 2010-11-25 17:06:07 +0000 | [diff] [blame] | 438 | mmc_set_clock(mmc, 400000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 439 | |
| 440 | /* Disable the BRR and BWR bits in IRQSTAT */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 441 | esdhc_clrbits32(®s->irqstaten, IRQSTATEN_BRR | IRQSTATEN_BWR); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 442 | |
| 443 | /* Put the PROCTL reg back to the default */ |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 444 | esdhc_write32(®s->proctl, PROCTL_INIT); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 445 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 446 | /* Set timout to the maximum value */ |
| 447 | esdhc_clrsetbits32(®s->sysctl, SYSCTL_TIMEOUT_MASK, 14 << 16); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 448 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 449 | /* Check if there is a callback for detecting the card */ |
| 450 | if (board_mmc_getcd(&card_absent, mmc)) { |
| 451 | timeout = 1000; |
| 452 | while (!(esdhc_read32(®s->prsstat) & PRSSTAT_CINS) && |
| 453 | --timeout) |
| 454 | udelay(1000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 455 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 456 | if (timeout <= 0) |
| 457 | ret = NO_CARD_ERR; |
| 458 | } else { |
| 459 | if (card_absent) |
| 460 | ret = NO_CARD_ERR; |
| 461 | } |
| 462 | |
| 463 | return ret; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 464 | } |
| 465 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 466 | static void esdhc_reset(struct fsl_esdhc *regs) |
| 467 | { |
| 468 | unsigned long timeout = 100; /* wait max 100 ms */ |
| 469 | |
| 470 | /* reset the controller */ |
| 471 | esdhc_write32(®s->sysctl, SYSCTL_RSTA); |
| 472 | |
| 473 | /* hardware clears the bit when it is done */ |
| 474 | while ((esdhc_read32(®s->sysctl) & SYSCTL_RSTA) && --timeout) |
| 475 | udelay(1000); |
| 476 | if (!timeout) |
| 477 | printf("MMC/SD: Reset never completed.\n"); |
| 478 | } |
| 479 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 480 | int fsl_esdhc_initialize(bd_t *bis, struct fsl_esdhc_cfg *cfg) |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 481 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 482 | struct fsl_esdhc *regs; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 483 | struct mmc *mmc; |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 484 | u32 caps, voltage_caps; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 485 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 486 | if (!cfg) |
| 487 | return -1; |
| 488 | |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 489 | mmc = malloc(sizeof(struct mmc)); |
| 490 | |
Jason Liu | 4692708 | 2011-11-25 00:18:04 +0000 | [diff] [blame^] | 491 | sprintf(mmc->name, "FSL_SDHC"); |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 492 | regs = (struct fsl_esdhc *)cfg->esdhc_base; |
| 493 | |
Jerry Huang | 48bb3bb | 2010-03-18 15:57:06 -0500 | [diff] [blame] | 494 | /* First reset the eSDHC controller */ |
| 495 | esdhc_reset(regs); |
| 496 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 497 | mmc->priv = cfg; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 498 | mmc->send_cmd = esdhc_send_cmd; |
| 499 | mmc->set_ios = esdhc_set_ios; |
| 500 | mmc->init = esdhc_init; |
| 501 | |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 502 | voltage_caps = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 503 | caps = regs->hostcapblt; |
Roy Zang | 3b4456e | 2011-01-07 00:06:47 -0600 | [diff] [blame] | 504 | |
| 505 | #ifdef CONFIG_SYS_FSL_ERRATUM_ESDHC135 |
| 506 | caps = caps & ~(ESDHC_HOSTCAPBLT_SRS | |
| 507 | ESDHC_HOSTCAPBLT_VS18 | ESDHC_HOSTCAPBLT_VS30); |
| 508 | #endif |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 509 | if (caps & ESDHC_HOSTCAPBLT_VS18) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 510 | voltage_caps |= MMC_VDD_165_195; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 511 | if (caps & ESDHC_HOSTCAPBLT_VS30) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 512 | voltage_caps |= MMC_VDD_29_30 | MMC_VDD_30_31; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 513 | if (caps & ESDHC_HOSTCAPBLT_VS33) |
Li Yang | 030955c | 2010-11-25 17:06:09 +0000 | [diff] [blame] | 514 | voltage_caps |= MMC_VDD_32_33 | MMC_VDD_33_34; |
| 515 | |
| 516 | #ifdef CONFIG_SYS_SD_VOLTAGE |
| 517 | mmc->voltages = CONFIG_SYS_SD_VOLTAGE; |
| 518 | #else |
| 519 | mmc->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 520 | #endif |
| 521 | if ((mmc->voltages & voltage_caps) == 0) { |
| 522 | printf("voltage not supported by controller\n"); |
| 523 | return -1; |
| 524 | } |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 525 | |
| 526 | mmc->host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT; |
| 527 | |
| 528 | if (caps & ESDHC_HOSTCAPBLT_HSS) |
| 529 | mmc->host_caps |= MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 530 | |
| 531 | mmc->f_min = 400000; |
Jerry Huang | 63786d2 | 2010-11-25 17:06:10 +0000 | [diff] [blame] | 532 | mmc->f_max = MIN(gd->sdhc_clk, 52000000); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 533 | |
Fabio Estevam | 1ed60d7 | 2011-05-12 09:33:27 +0000 | [diff] [blame] | 534 | mmc->b_max = 0; |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 535 | mmc_register(mmc); |
| 536 | |
| 537 | return 0; |
| 538 | } |
| 539 | |
| 540 | int fsl_esdhc_mmc_init(bd_t *bis) |
| 541 | { |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 542 | struct fsl_esdhc_cfg *cfg; |
| 543 | |
| 544 | cfg = malloc(sizeof(struct fsl_esdhc_cfg)); |
| 545 | memset(cfg, 0, sizeof(struct fsl_esdhc_cfg)); |
| 546 | cfg->esdhc_base = CONFIG_SYS_FSL_ESDHC_ADDR; |
| 547 | return fsl_esdhc_initialize(bis, cfg); |
Andy Fleming | 50586ef | 2008-10-30 16:47:16 -0500 | [diff] [blame] | 548 | } |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 549 | |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 550 | #ifdef CONFIG_OF_LIBFDT |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 551 | void fdt_fixup_esdhc(void *blob, bd_t *bd) |
| 552 | { |
| 553 | const char *compat = "fsl,esdhc"; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 554 | |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 555 | #ifdef CONFIG_FSL_ESDHC_PIN_MUX |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 556 | if (!hwconfig("esdhc")) { |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 557 | do_fixup_by_compat(blob, compat, "status", "disabled", |
| 558 | 8 + 1, 1); |
| 559 | return; |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 560 | } |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 561 | #endif |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 562 | |
| 563 | do_fixup_by_compat_u32(blob, compat, "clock-frequency", |
| 564 | gd->sdhc_clk, 1); |
Chenhui Zhao | a6da8b8 | 2011-01-04 17:23:05 +0800 | [diff] [blame] | 565 | |
| 566 | do_fixup_by_compat(blob, compat, "status", "okay", |
| 567 | 4 + 1, 1); |
Anton Vorontsov | b33433a | 2009-06-10 00:25:29 +0400 | [diff] [blame] | 568 | } |
Stefano Babic | c67bee1 | 2010-02-05 15:11:27 +0100 | [diff] [blame] | 569 | #endif |