Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (C) 2010 Marek Vasut <marek.vasut@gmail.com> |
| 4 | * |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 5 | * Modified to add driver model (DM) support |
| 6 | * Copyright (C) 2019 Marcel Ziswiler <marcel@ziswiler.com> |
| 7 | * |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 8 | * Loosely based on the old code and Linux's PXA MMC driver |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 9 | */ |
| 10 | |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 11 | #include <common.h> |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 12 | #include <asm/arch/hardware.h> |
| 13 | #include <asm/arch/regs-mmc.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 14 | #include <linux/delay.h> |
Masahiro Yamada | 56a931c | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 15 | #include <linux/errno.h> |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 16 | #include <asm/io.h> |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 17 | #include <dm.h> |
| 18 | #include <dm/platform_data/pxa_mmc_gen.h> |
Marcel Ziswiler | 2c64583 | 2015-08-16 04:16:27 +0200 | [diff] [blame] | 19 | #include <malloc.h> |
| 20 | #include <mmc.h> |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 21 | |
| 22 | /* PXAMMC Generic default config for various CPUs */ |
Tom Rini | 56bf6a8 | 2022-05-25 16:13:48 -0400 | [diff] [blame] | 23 | #if defined(CONFIG_CPU_PXA27X) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 24 | #define PXAMMC_CRC_SKIP |
| 25 | #define PXAMMC_FIFO_SIZE 32 |
| 26 | #define PXAMMC_MIN_SPEED 304000 |
| 27 | #define PXAMMC_MAX_SPEED 19500000 |
| 28 | #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT) |
| 29 | #elif defined(CONFIG_CPU_MONAHANS) |
| 30 | #define PXAMMC_FIFO_SIZE 32 |
| 31 | #define PXAMMC_MIN_SPEED 304000 |
| 32 | #define PXAMMC_MAX_SPEED 26000000 |
| 33 | #define PXAMMC_HOST_CAPS (MMC_MODE_4BIT | MMC_MODE_HS) |
| 34 | #else |
| 35 | #error "This CPU isn't supported by PXA MMC!" |
| 36 | #endif |
| 37 | |
| 38 | #define MMC_STAT_ERRORS \ |
| 39 | (MMC_STAT_RES_CRC_ERROR | MMC_STAT_SPI_READ_ERROR_TOKEN | \ |
| 40 | MMC_STAT_CRC_READ_ERROR | MMC_STAT_TIME_OUT_RESPONSE | \ |
| 41 | MMC_STAT_READ_TIME_OUT | MMC_STAT_CRC_WRITE_ERROR) |
| 42 | |
| 43 | /* 1 millisecond (in wait cycles below it's 100 x 10uS waits) */ |
| 44 | #define PXA_MMC_TIMEOUT 100 |
| 45 | |
| 46 | struct pxa_mmc_priv { |
| 47 | struct pxa_mmc_regs *regs; |
| 48 | }; |
| 49 | |
| 50 | /* Wait for bit to be set */ |
| 51 | static int pxa_mmc_wait(struct mmc *mmc, uint32_t mask) |
| 52 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 53 | struct pxa_mmc_priv *priv = mmc->priv; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 54 | struct pxa_mmc_regs *regs = priv->regs; |
| 55 | unsigned int timeout = PXA_MMC_TIMEOUT; |
| 56 | |
| 57 | /* Wait for bit to be set */ |
| 58 | while (--timeout) { |
| 59 | if (readl(®s->stat) & mask) |
| 60 | break; |
| 61 | udelay(10); |
| 62 | } |
| 63 | |
| 64 | if (!timeout) |
| 65 | return -ETIMEDOUT; |
| 66 | |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | static int pxa_mmc_stop_clock(struct mmc *mmc) |
| 71 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 72 | struct pxa_mmc_priv *priv = mmc->priv; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 73 | struct pxa_mmc_regs *regs = priv->regs; |
| 74 | unsigned int timeout = PXA_MMC_TIMEOUT; |
| 75 | |
| 76 | /* If the clock aren't running, exit */ |
| 77 | if (!(readl(®s->stat) & MMC_STAT_CLK_EN)) |
| 78 | return 0; |
| 79 | |
| 80 | /* Tell the controller to turn off the clock */ |
| 81 | writel(MMC_STRPCL_STOP_CLK, ®s->strpcl); |
| 82 | |
| 83 | /* Wait until the clock are off */ |
| 84 | while (--timeout) { |
| 85 | if (!(readl(®s->stat) & MMC_STAT_CLK_EN)) |
| 86 | break; |
| 87 | udelay(10); |
| 88 | } |
| 89 | |
| 90 | /* The clock refused to stop, scream and die a painful death */ |
| 91 | if (!timeout) |
| 92 | return -ETIMEDOUT; |
| 93 | |
| 94 | /* The clock stopped correctly */ |
| 95 | return 0; |
| 96 | } |
| 97 | |
| 98 | static int pxa_mmc_start_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 99 | uint32_t cmdat) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 100 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 101 | struct pxa_mmc_priv *priv = mmc->priv; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 102 | struct pxa_mmc_regs *regs = priv->regs; |
| 103 | int ret; |
| 104 | |
| 105 | /* The card can send a "busy" response */ |
Andy Fleming | 611a347 | 2012-09-06 15:23:13 -0500 | [diff] [blame] | 106 | if (cmd->resp_type & MMC_RSP_BUSY) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 107 | cmdat |= MMC_CMDAT_BUSY; |
| 108 | |
| 109 | /* Inform the controller about response type */ |
| 110 | switch (cmd->resp_type) { |
| 111 | case MMC_RSP_R1: |
| 112 | case MMC_RSP_R1b: |
| 113 | cmdat |= MMC_CMDAT_R1; |
| 114 | break; |
| 115 | case MMC_RSP_R2: |
| 116 | cmdat |= MMC_CMDAT_R2; |
| 117 | break; |
| 118 | case MMC_RSP_R3: |
| 119 | cmdat |= MMC_CMDAT_R3; |
| 120 | break; |
| 121 | default: |
| 122 | break; |
| 123 | } |
| 124 | |
| 125 | /* Load command and it's arguments into the controller */ |
| 126 | writel(cmd->cmdidx, ®s->cmd); |
| 127 | writel(cmd->cmdarg >> 16, ®s->argh); |
| 128 | writel(cmd->cmdarg & 0xffff, ®s->argl); |
| 129 | writel(cmdat, ®s->cmdat); |
| 130 | |
| 131 | /* Start the controller clock and wait until they are started */ |
| 132 | writel(MMC_STRPCL_START_CLK, ®s->strpcl); |
| 133 | |
| 134 | ret = pxa_mmc_wait(mmc, MMC_STAT_CLK_EN); |
| 135 | if (ret) |
| 136 | return ret; |
| 137 | |
| 138 | /* Correct and happy end */ |
| 139 | return 0; |
| 140 | } |
| 141 | |
| 142 | static int pxa_mmc_cmd_done(struct mmc *mmc, struct mmc_cmd *cmd) |
| 143 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 144 | struct pxa_mmc_priv *priv = mmc->priv; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 145 | struct pxa_mmc_regs *regs = priv->regs; |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 146 | u32 a, b, c; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 147 | int i; |
| 148 | int stat; |
| 149 | |
| 150 | /* Read the controller status */ |
| 151 | stat = readl(®s->stat); |
| 152 | |
| 153 | /* |
| 154 | * Linux says: |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 155 | * Did I mention this is Sick. We always need to |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 156 | * discard the upper 8 bits of the first 16-bit word. |
| 157 | */ |
| 158 | a = readl(®s->res) & 0xffff; |
| 159 | for (i = 0; i < 4; i++) { |
| 160 | b = readl(®s->res) & 0xffff; |
| 161 | c = readl(®s->res) & 0xffff; |
| 162 | cmd->response[i] = (a << 24) | (b << 8) | (c >> 8); |
| 163 | a = c; |
| 164 | } |
| 165 | |
| 166 | /* The command response didn't arrive */ |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 167 | if (stat & MMC_STAT_TIME_OUT_RESPONSE) { |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 168 | return -ETIMEDOUT; |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 169 | } else if (stat & MMC_STAT_RES_CRC_ERROR && |
| 170 | cmd->resp_type & MMC_RSP_CRC) { |
| 171 | #ifdef PXAMMC_CRC_SKIP |
| 172 | if (cmd->resp_type & MMC_RSP_136 && |
| 173 | cmd->response[0] & (1 << 31)) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 174 | printf("Ignoring CRC, this may be dangerous!\n"); |
| 175 | else |
| 176 | #endif |
| 177 | return -EILSEQ; |
| 178 | } |
| 179 | |
| 180 | /* The command response was successfully read */ |
| 181 | return 0; |
| 182 | } |
| 183 | |
| 184 | static int pxa_mmc_do_read_xfer(struct mmc *mmc, struct mmc_data *data) |
| 185 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 186 | struct pxa_mmc_priv *priv = mmc->priv; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 187 | struct pxa_mmc_regs *regs = priv->regs; |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 188 | u32 len; |
| 189 | u32 *buf = (uint32_t *)data->dest; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 190 | int size; |
| 191 | int ret; |
| 192 | |
| 193 | len = data->blocks * data->blocksize; |
| 194 | |
| 195 | while (len) { |
| 196 | /* The controller has data ready */ |
| 197 | if (readl(®s->i_reg) & MMC_I_REG_RXFIFO_RD_REQ) { |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 198 | size = min(len, (uint32_t)PXAMMC_FIFO_SIZE); |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 199 | len -= size; |
| 200 | size /= 4; |
| 201 | |
| 202 | /* Read data into the buffer */ |
| 203 | while (size--) |
| 204 | *buf++ = readl(®s->rxfifo); |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 205 | } |
| 206 | |
| 207 | if (readl(®s->stat) & MMC_STAT_ERRORS) |
| 208 | return -EIO; |
| 209 | } |
| 210 | |
| 211 | /* Wait for the transmission-done interrupt */ |
| 212 | ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE); |
| 213 | if (ret) |
| 214 | return ret; |
| 215 | |
| 216 | return 0; |
| 217 | } |
| 218 | |
| 219 | static int pxa_mmc_do_write_xfer(struct mmc *mmc, struct mmc_data *data) |
| 220 | { |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 221 | struct pxa_mmc_priv *priv = mmc->priv; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 222 | struct pxa_mmc_regs *regs = priv->regs; |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 223 | u32 len; |
| 224 | u32 *buf = (uint32_t *)data->src; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 225 | int size; |
| 226 | int ret; |
| 227 | |
| 228 | len = data->blocks * data->blocksize; |
| 229 | |
| 230 | while (len) { |
| 231 | /* The controller is ready to receive data */ |
| 232 | if (readl(®s->i_reg) & MMC_I_REG_TXFIFO_WR_REQ) { |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 233 | size = min(len, (uint32_t)PXAMMC_FIFO_SIZE); |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 234 | len -= size; |
| 235 | size /= 4; |
| 236 | |
| 237 | while (size--) |
| 238 | writel(*buf++, ®s->txfifo); |
| 239 | |
Masahiro Yamada | db20464 | 2014-11-07 03:03:31 +0900 | [diff] [blame] | 240 | if (min(len, (uint32_t)PXAMMC_FIFO_SIZE) < 32) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 241 | writel(MMC_PRTBUF_BUF_PART_FULL, ®s->prtbuf); |
| 242 | } |
| 243 | |
| 244 | if (readl(®s->stat) & MMC_STAT_ERRORS) |
| 245 | return -EIO; |
| 246 | } |
| 247 | |
| 248 | /* Wait for the transmission-done interrupt */ |
| 249 | ret = pxa_mmc_wait(mmc, MMC_STAT_DATA_TRAN_DONE); |
| 250 | if (ret) |
| 251 | return ret; |
| 252 | |
| 253 | /* Wait until the data are really written to the card */ |
| 254 | ret = pxa_mmc_wait(mmc, MMC_STAT_PRG_DONE); |
| 255 | if (ret) |
| 256 | return ret; |
| 257 | |
| 258 | return 0; |
| 259 | } |
| 260 | |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 261 | static int pxa_mmc_send_cmd_common(struct pxa_mmc_priv *priv, struct mmc *mmc, |
| 262 | struct mmc_cmd *cmd, struct mmc_data *data) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 263 | { |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 264 | struct pxa_mmc_regs *regs = priv->regs; |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 265 | u32 cmdat = 0; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 266 | int ret; |
| 267 | |
| 268 | /* Stop the controller */ |
| 269 | ret = pxa_mmc_stop_clock(mmc); |
| 270 | if (ret) |
| 271 | return ret; |
| 272 | |
| 273 | /* If we're doing data transfer, configure the controller accordingly */ |
| 274 | if (data) { |
| 275 | writel(data->blocks, ®s->nob); |
| 276 | writel(data->blocksize, ®s->blklen); |
| 277 | /* This delay can be optimized, but stick with max value */ |
| 278 | writel(0xffff, ®s->rdto); |
| 279 | cmdat |= MMC_CMDAT_DATA_EN; |
| 280 | if (data->flags & MMC_DATA_WRITE) |
| 281 | cmdat |= MMC_CMDAT_WRITE; |
| 282 | } |
| 283 | |
| 284 | /* Run in 4bit mode if the card can do it */ |
| 285 | if (mmc->bus_width == 4) |
| 286 | cmdat |= MMC_CMDAT_SD_4DAT; |
| 287 | |
| 288 | /* Execute the command */ |
| 289 | ret = pxa_mmc_start_cmd(mmc, cmd, cmdat); |
| 290 | if (ret) |
| 291 | return ret; |
| 292 | |
| 293 | /* Wait until the command completes */ |
| 294 | ret = pxa_mmc_wait(mmc, MMC_STAT_END_CMD_RES); |
| 295 | if (ret) |
| 296 | return ret; |
| 297 | |
| 298 | /* Read back the result */ |
| 299 | ret = pxa_mmc_cmd_done(mmc, cmd); |
| 300 | if (ret) |
| 301 | return ret; |
| 302 | |
| 303 | /* In case there was a data transfer scheduled, do it */ |
| 304 | if (data) { |
| 305 | if (data->flags & MMC_DATA_WRITE) |
| 306 | pxa_mmc_do_write_xfer(mmc, data); |
| 307 | else |
| 308 | pxa_mmc_do_read_xfer(mmc, data); |
| 309 | } |
| 310 | |
| 311 | return 0; |
| 312 | } |
| 313 | |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 314 | static int pxa_mmc_set_ios_common(struct pxa_mmc_priv *priv, struct mmc *mmc) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 315 | { |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 316 | struct pxa_mmc_regs *regs = priv->regs; |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 317 | u32 tmp; |
| 318 | u32 pxa_mmc_clock; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 319 | |
| 320 | if (!mmc->clock) { |
| 321 | pxa_mmc_stop_clock(mmc); |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 322 | return 0; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | /* PXA3xx can do 26MHz with special settings. */ |
| 326 | if (mmc->clock == 26000000) { |
| 327 | writel(0x7, ®s->clkrt); |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 328 | return 0; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | /* Set clock to the card the usual way. */ |
| 332 | pxa_mmc_clock = 0; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 333 | tmp = mmc->cfg->f_max / mmc->clock; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 334 | tmp += tmp % 2; |
| 335 | |
| 336 | while (tmp > 1) { |
| 337 | pxa_mmc_clock++; |
| 338 | tmp >>= 1; |
| 339 | } |
| 340 | |
| 341 | writel(pxa_mmc_clock, ®s->clkrt); |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 342 | |
| 343 | return 0; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 346 | static int pxa_mmc_init_common(struct pxa_mmc_priv *priv, struct mmc *mmc) |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 347 | { |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 348 | struct pxa_mmc_regs *regs = priv->regs; |
| 349 | |
| 350 | /* Make sure the clock are stopped */ |
| 351 | pxa_mmc_stop_clock(mmc); |
| 352 | |
| 353 | /* Turn off SPI mode */ |
| 354 | writel(0, ®s->spi); |
| 355 | |
| 356 | /* Set up maximum timeout to wait for command response */ |
| 357 | writel(MMC_RES_TO_MAX_MASK, ®s->resto); |
| 358 | |
| 359 | /* Mask all interrupts */ |
| 360 | writel(~(MMC_I_MASK_TXFIFO_WR_REQ | MMC_I_MASK_RXFIFO_RD_REQ), |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 361 | ®s->i_mask); |
| 362 | |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 363 | return 0; |
| 364 | } |
| 365 | |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 366 | #if !CONFIG_IS_ENABLED(DM_MMC) |
| 367 | static int pxa_mmc_init(struct mmc *mmc) |
| 368 | { |
| 369 | struct pxa_mmc_priv *priv = mmc->priv; |
| 370 | |
| 371 | return pxa_mmc_init_common(priv, mmc); |
| 372 | } |
| 373 | |
| 374 | static int pxa_mmc_request(struct mmc *mmc, struct mmc_cmd *cmd, |
| 375 | struct mmc_data *data) |
| 376 | { |
| 377 | struct pxa_mmc_priv *priv = mmc->priv; |
| 378 | |
| 379 | return pxa_mmc_send_cmd_common(priv, mmc, cmd, data); |
| 380 | } |
| 381 | |
| 382 | static int pxa_mmc_set_ios(struct mmc *mmc) |
| 383 | { |
| 384 | struct pxa_mmc_priv *priv = mmc->priv; |
| 385 | |
| 386 | return pxa_mmc_set_ios_common(priv, mmc); |
| 387 | } |
| 388 | |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 389 | static const struct mmc_ops pxa_mmc_ops = { |
| 390 | .send_cmd = pxa_mmc_request, |
| 391 | .set_ios = pxa_mmc_set_ios, |
| 392 | .init = pxa_mmc_init, |
| 393 | }; |
| 394 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 395 | static struct mmc_config pxa_mmc_cfg = { |
| 396 | .name = "PXA MMC", |
| 397 | .ops = &pxa_mmc_ops, |
| 398 | .voltages = MMC_VDD_32_33 | MMC_VDD_33_34, |
| 399 | .f_max = PXAMMC_MAX_SPEED, |
| 400 | .f_min = PXAMMC_MIN_SPEED, |
| 401 | .host_caps = PXAMMC_HOST_CAPS, |
| 402 | .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT, |
| 403 | }; |
| 404 | |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 405 | int pxa_mmc_register(int card_index) |
| 406 | { |
| 407 | struct mmc *mmc; |
| 408 | struct pxa_mmc_priv *priv; |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 409 | u32 reg; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 410 | int ret = -ENOMEM; |
| 411 | |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 412 | priv = malloc(sizeof(struct pxa_mmc_priv)); |
| 413 | if (!priv) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 414 | goto err0; |
| 415 | |
| 416 | memset(priv, 0, sizeof(*priv)); |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 417 | |
| 418 | switch (card_index) { |
| 419 | case 0: |
| 420 | priv->regs = (struct pxa_mmc_regs *)MMC0_BASE; |
| 421 | break; |
| 422 | case 1: |
| 423 | priv->regs = (struct pxa_mmc_regs *)MMC1_BASE; |
| 424 | break; |
| 425 | default: |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 426 | ret = -EINVAL; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 427 | printf("PXA MMC: Invalid MMC controller ID (card_index = %d)\n", |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 428 | card_index); |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 429 | goto err1; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 430 | } |
| 431 | |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 432 | #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */ |
| 433 | reg = readl(CKEN); |
| 434 | reg |= CKEN12_MMC; |
| 435 | writel(reg, CKEN); |
| 436 | #else /* PXA3xx */ |
| 437 | reg = readl(CKENA); |
| 438 | reg |= CKENA_12_MMC0 | CKENA_13_MMC1; |
| 439 | writel(reg, CKENA); |
| 440 | #endif |
| 441 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 442 | mmc = mmc_create(&pxa_mmc_cfg, priv); |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 443 | if (!mmc) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 444 | goto err1; |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 445 | |
| 446 | return 0; |
| 447 | |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 448 | err1: |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 449 | free(priv); |
Marek Vasut | 644356a | 2011-11-02 00:29:27 +0000 | [diff] [blame] | 450 | err0: |
| 451 | return ret; |
| 452 | } |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 453 | #else /* !CONFIG_IS_ENABLED(DM_MMC) */ |
| 454 | static int pxa_mmc_probe(struct udevice *dev) |
| 455 | { |
| 456 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 457 | struct pxa_mmc_plat *plat = dev_get_plat(dev); |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 458 | struct mmc_config *cfg = &plat->cfg; |
| 459 | struct mmc *mmc = &plat->mmc; |
| 460 | struct pxa_mmc_priv *priv = dev_get_priv(dev); |
| 461 | u32 reg; |
| 462 | |
| 463 | upriv->mmc = mmc; |
| 464 | |
| 465 | cfg->b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 466 | cfg->f_max = PXAMMC_MAX_SPEED; |
| 467 | cfg->f_min = PXAMMC_MIN_SPEED; |
| 468 | cfg->host_caps = PXAMMC_HOST_CAPS; |
| 469 | cfg->name = dev->name; |
| 470 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 471 | |
| 472 | mmc->priv = priv; |
| 473 | |
| 474 | priv->regs = plat->base; |
| 475 | |
| 476 | #ifndef CONFIG_CPU_MONAHANS /* PXA2xx */ |
| 477 | reg = readl(CKEN); |
| 478 | reg |= CKEN12_MMC; |
| 479 | writel(reg, CKEN); |
| 480 | #else /* PXA3xx */ |
| 481 | reg = readl(CKENA); |
| 482 | reg |= CKENA_12_MMC0 | CKENA_13_MMC1; |
| 483 | writel(reg, CKENA); |
| 484 | #endif |
| 485 | |
| 486 | return pxa_mmc_init_common(priv, mmc); |
| 487 | } |
| 488 | |
| 489 | static int pxa_mmc_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 490 | struct mmc_data *data) |
| 491 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 492 | struct pxa_mmc_plat *plat = dev_get_plat(dev); |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 493 | struct pxa_mmc_priv *priv = dev_get_priv(dev); |
| 494 | |
| 495 | return pxa_mmc_send_cmd_common(priv, &plat->mmc, cmd, data); |
| 496 | } |
| 497 | |
| 498 | static int pxa_mmc_set_ios(struct udevice *dev) |
| 499 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 500 | struct pxa_mmc_plat *plat = dev_get_plat(dev); |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 501 | struct pxa_mmc_priv *priv = dev_get_priv(dev); |
| 502 | |
| 503 | return pxa_mmc_set_ios_common(priv, &plat->mmc); |
| 504 | } |
| 505 | |
| 506 | static const struct dm_mmc_ops pxa_mmc_ops = { |
| 507 | .get_cd = NULL, |
| 508 | .send_cmd = pxa_mmc_send_cmd, |
| 509 | .set_ios = pxa_mmc_set_ios, |
| 510 | }; |
| 511 | |
| 512 | #if CONFIG_IS_ENABLED(BLK) |
| 513 | static int pxa_mmc_bind(struct udevice *dev) |
| 514 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 515 | struct pxa_mmc_plat *plat = dev_get_plat(dev); |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 516 | |
| 517 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 518 | } |
| 519 | #endif |
| 520 | |
| 521 | U_BOOT_DRIVER(pxa_mmc) = { |
| 522 | #if CONFIG_IS_ENABLED(BLK) |
| 523 | .bind = pxa_mmc_bind, |
| 524 | #endif |
| 525 | .id = UCLASS_MMC, |
| 526 | .name = "pxa_mmc", |
| 527 | .ops = &pxa_mmc_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 528 | .priv_auto = sizeof(struct pxa_mmc_priv), |
Marcel Ziswiler | 768b86d | 2019-05-20 02:44:59 +0200 | [diff] [blame] | 529 | .probe = pxa_mmc_probe, |
| 530 | }; |
| 531 | #endif /* !CONFIG_IS_ENABLED(DM_MMC) */ |