Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 1 | /* |
| 2 | * ARM PrimeCell MultiMedia Card Interface - PL180 |
| 3 | * |
| 4 | * Copyright (C) ST-Ericsson SA 2010 |
| 5 | * |
| 6 | * Author: Ulf Hansson <ulf.hansson@stericsson.com> |
| 7 | * Author: Martin Lundholm <martin.xa.lundholm@stericsson.com> |
| 8 | * Ported to drivers/mmc/ by: Matt Waddel <matt.waddel@linaro.org> |
| 9 | * |
Wolfgang Denk | d79de1d | 2013-07-08 09:37:19 +0200 | [diff] [blame] | 10 | * SPDX-License-Identifier: GPL-2.0+ |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 11 | */ |
| 12 | |
| 13 | /* #define DEBUG */ |
| 14 | |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 15 | #include "common.h" |
Patrice Chotard | 879dbab | 2017-10-23 10:57:33 +0200 | [diff] [blame^] | 16 | #include <clk.h> |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 17 | #include <errno.h> |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 18 | #include <malloc.h> |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 19 | #include <mmc.h> |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 20 | |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 21 | #include "arm_pl180_mmci.h" |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 22 | |
| 23 | #include <asm/io.h> |
| 24 | |
| 25 | #ifdef CONFIG_DM_MMC |
| 26 | #include <dm.h> |
| 27 | DECLARE_GLOBAL_DATA_PTR; |
| 28 | |
| 29 | #define MMC_CLOCK_MAX 48000000 |
| 30 | #define MMC_CLOCK_MIN 400000 |
| 31 | |
| 32 | struct arm_pl180_mmc_plat { |
| 33 | struct mmc_config cfg; |
| 34 | struct mmc mmc; |
| 35 | }; |
| 36 | #endif |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 37 | |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 38 | static int wait_for_command_end(struct mmc *dev, struct mmc_cmd *cmd) |
| 39 | { |
| 40 | u32 hoststatus, statusmask; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 41 | struct pl180_mmc_host *host = dev->priv; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 42 | |
| 43 | statusmask = SDI_STA_CTIMEOUT | SDI_STA_CCRCFAIL; |
| 44 | if ((cmd->resp_type & MMC_RSP_PRESENT)) |
| 45 | statusmask |= SDI_STA_CMDREND; |
| 46 | else |
| 47 | statusmask |= SDI_STA_CMDSENT; |
| 48 | |
| 49 | do |
| 50 | hoststatus = readl(&host->base->status) & statusmask; |
| 51 | while (!hoststatus); |
| 52 | |
| 53 | writel(statusmask, &host->base->status_clear); |
| 54 | if (hoststatus & SDI_STA_CTIMEOUT) { |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 55 | debug("CMD%d time out\n", cmd->cmdidx); |
Jaehoon Chung | 7825d20 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 56 | return -ETIMEDOUT; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 57 | } else if ((hoststatus & SDI_STA_CCRCFAIL) && |
Andy Fleming | 611a347 | 2012-09-06 15:23:13 -0500 | [diff] [blame] | 58 | (cmd->resp_type & MMC_RSP_CRC)) { |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 59 | printf("CMD%d CRC error\n", cmd->cmdidx); |
| 60 | return -EILSEQ; |
| 61 | } |
| 62 | |
| 63 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 64 | cmd->response[0] = readl(&host->base->response0); |
| 65 | cmd->response[1] = readl(&host->base->response1); |
| 66 | cmd->response[2] = readl(&host->base->response2); |
| 67 | cmd->response[3] = readl(&host->base->response3); |
| 68 | debug("CMD%d response[0]:0x%08X, response[1]:0x%08X, " |
| 69 | "response[2]:0x%08X, response[3]:0x%08X\n", |
| 70 | cmd->cmdidx, cmd->response[0], cmd->response[1], |
| 71 | cmd->response[2], cmd->response[3]); |
| 72 | } |
| 73 | |
| 74 | return 0; |
| 75 | } |
| 76 | |
| 77 | /* send command to the mmc card and wait for results */ |
| 78 | static int do_command(struct mmc *dev, struct mmc_cmd *cmd) |
| 79 | { |
| 80 | int result; |
| 81 | u32 sdi_cmd = 0; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 82 | struct pl180_mmc_host *host = dev->priv; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 83 | |
| 84 | sdi_cmd = ((cmd->cmdidx & SDI_CMD_CMDINDEX_MASK) | SDI_CMD_CPSMEN); |
| 85 | |
| 86 | if (cmd->resp_type) { |
| 87 | sdi_cmd |= SDI_CMD_WAITRESP; |
| 88 | if (cmd->resp_type & MMC_RSP_136) |
| 89 | sdi_cmd |= SDI_CMD_LONGRESP; |
| 90 | } |
| 91 | |
| 92 | writel((u32)cmd->cmdarg, &host->base->argument); |
| 93 | udelay(COMMAND_REG_DELAY); |
| 94 | writel(sdi_cmd, &host->base->command); |
| 95 | result = wait_for_command_end(dev, cmd); |
| 96 | |
| 97 | /* After CMD2 set RCA to a none zero value. */ |
| 98 | if ((result == 0) && (cmd->cmdidx == MMC_CMD_ALL_SEND_CID)) |
| 99 | dev->rca = 10; |
| 100 | |
| 101 | /* After CMD3 open drain is switched off and push pull is used. */ |
| 102 | if ((result == 0) && (cmd->cmdidx == MMC_CMD_SET_RELATIVE_ADDR)) { |
| 103 | u32 sdi_pwr = readl(&host->base->power) & ~SDI_PWR_OPD; |
| 104 | writel(sdi_pwr, &host->base->power); |
| 105 | } |
| 106 | |
| 107 | return result; |
| 108 | } |
| 109 | |
| 110 | static int read_bytes(struct mmc *dev, u32 *dest, u32 blkcount, u32 blksize) |
| 111 | { |
| 112 | u32 *tempbuff = dest; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 113 | u64 xfercount = blkcount * blksize; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 114 | struct pl180_mmc_host *host = dev->priv; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 115 | u32 status, status_err; |
| 116 | |
| 117 | debug("read_bytes: blkcount=%u blksize=%u\n", blkcount, blksize); |
| 118 | |
| 119 | status = readl(&host->base->status); |
| 120 | status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | |
| 121 | SDI_STA_RXOVERR); |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 122 | while ((!status_err) && (xfercount >= sizeof(u32))) { |
| 123 | if (status & SDI_STA_RXDAVL) { |
| 124 | *(tempbuff) = readl(&host->base->fifo); |
| 125 | tempbuff++; |
| 126 | xfercount -= sizeof(u32); |
| 127 | } |
| 128 | status = readl(&host->base->status); |
| 129 | status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | |
| 130 | SDI_STA_RXOVERR); |
| 131 | } |
| 132 | |
| 133 | status_err = status & |
| 134 | (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND | |
| 135 | SDI_STA_RXOVERR); |
| 136 | while (!status_err) { |
| 137 | status = readl(&host->base->status); |
| 138 | status_err = status & |
| 139 | (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND | |
| 140 | SDI_STA_RXOVERR); |
| 141 | } |
| 142 | |
| 143 | if (status & SDI_STA_DTIMEOUT) { |
| 144 | printf("Read data timed out, xfercount: %llu, status: 0x%08X\n", |
| 145 | xfercount, status); |
| 146 | return -ETIMEDOUT; |
| 147 | } else if (status & SDI_STA_DCRCFAIL) { |
| 148 | printf("Read data bytes CRC error: 0x%x\n", status); |
| 149 | return -EILSEQ; |
| 150 | } else if (status & SDI_STA_RXOVERR) { |
| 151 | printf("Read data RX overflow error\n"); |
| 152 | return -EIO; |
| 153 | } |
| 154 | |
| 155 | writel(SDI_ICR_MASK, &host->base->status_clear); |
| 156 | |
| 157 | if (xfercount) { |
| 158 | printf("Read data error, xfercount: %llu\n", xfercount); |
| 159 | return -ENOBUFS; |
| 160 | } |
| 161 | |
| 162 | return 0; |
| 163 | } |
| 164 | |
| 165 | static int write_bytes(struct mmc *dev, u32 *src, u32 blkcount, u32 blksize) |
| 166 | { |
| 167 | u32 *tempbuff = src; |
| 168 | int i; |
| 169 | u64 xfercount = blkcount * blksize; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 170 | struct pl180_mmc_host *host = dev->priv; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 171 | u32 status, status_err; |
| 172 | |
| 173 | debug("write_bytes: blkcount=%u blksize=%u\n", blkcount, blksize); |
| 174 | |
| 175 | status = readl(&host->base->status); |
| 176 | status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT); |
| 177 | while (!status_err && xfercount) { |
| 178 | if (status & SDI_STA_TXFIFOBW) { |
| 179 | if (xfercount >= SDI_FIFO_BURST_SIZE * sizeof(u32)) { |
| 180 | for (i = 0; i < SDI_FIFO_BURST_SIZE; i++) |
| 181 | writel(*(tempbuff + i), |
| 182 | &host->base->fifo); |
| 183 | tempbuff += SDI_FIFO_BURST_SIZE; |
| 184 | xfercount -= SDI_FIFO_BURST_SIZE * sizeof(u32); |
| 185 | } else { |
| 186 | while (xfercount >= sizeof(u32)) { |
| 187 | writel(*(tempbuff), &host->base->fifo); |
| 188 | tempbuff++; |
| 189 | xfercount -= sizeof(u32); |
| 190 | } |
| 191 | } |
| 192 | } |
| 193 | status = readl(&host->base->status); |
| 194 | status_err = status & (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT); |
| 195 | } |
| 196 | |
| 197 | status_err = status & |
| 198 | (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND); |
| 199 | while (!status_err) { |
| 200 | status = readl(&host->base->status); |
| 201 | status_err = status & |
| 202 | (SDI_STA_DCRCFAIL | SDI_STA_DTIMEOUT | SDI_STA_DBCKEND); |
| 203 | } |
| 204 | |
| 205 | if (status & SDI_STA_DTIMEOUT) { |
| 206 | printf("Write data timed out, xfercount:%llu,status:0x%08X\n", |
| 207 | xfercount, status); |
| 208 | return -ETIMEDOUT; |
| 209 | } else if (status & SDI_STA_DCRCFAIL) { |
| 210 | printf("Write data CRC error\n"); |
| 211 | return -EILSEQ; |
| 212 | } |
| 213 | |
| 214 | writel(SDI_ICR_MASK, &host->base->status_clear); |
| 215 | |
| 216 | if (xfercount) { |
| 217 | printf("Write data error, xfercount:%llu", xfercount); |
| 218 | return -ENOBUFS; |
| 219 | } |
| 220 | |
| 221 | return 0; |
| 222 | } |
| 223 | |
| 224 | static int do_data_transfer(struct mmc *dev, |
| 225 | struct mmc_cmd *cmd, |
| 226 | struct mmc_data *data) |
| 227 | { |
| 228 | int error = -ETIMEDOUT; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 229 | struct pl180_mmc_host *host = dev->priv; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 230 | u32 blksz = 0; |
| 231 | u32 data_ctrl = 0; |
| 232 | u32 data_len = (u32) (data->blocks * data->blocksize); |
| 233 | |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 234 | if (!host->version2) { |
| 235 | blksz = (ffs(data->blocksize) - 1); |
| 236 | data_ctrl |= ((blksz << 4) & SDI_DCTRL_DBLKSIZE_MASK); |
| 237 | } else { |
| 238 | blksz = data->blocksize; |
| 239 | data_ctrl |= (blksz << SDI_DCTRL_DBLOCKSIZE_V2_SHIFT); |
| 240 | } |
| 241 | data_ctrl |= SDI_DCTRL_DTEN | SDI_DCTRL_BUSYMODE; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 242 | |
| 243 | writel(SDI_DTIMER_DEFAULT, &host->base->datatimer); |
| 244 | writel(data_len, &host->base->datalength); |
| 245 | udelay(DATA_REG_DELAY); |
| 246 | |
| 247 | if (data->flags & MMC_DATA_READ) { |
| 248 | data_ctrl |= SDI_DCTRL_DTDIR_IN; |
| 249 | writel(data_ctrl, &host->base->datactrl); |
| 250 | |
| 251 | error = do_command(dev, cmd); |
| 252 | if (error) |
| 253 | return error; |
| 254 | |
| 255 | error = read_bytes(dev, (u32 *)data->dest, (u32)data->blocks, |
| 256 | (u32)data->blocksize); |
| 257 | } else if (data->flags & MMC_DATA_WRITE) { |
| 258 | error = do_command(dev, cmd); |
| 259 | if (error) |
| 260 | return error; |
| 261 | |
| 262 | writel(data_ctrl, &host->base->datactrl); |
| 263 | error = write_bytes(dev, (u32 *)data->src, (u32)data->blocks, |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 264 | (u32)data->blocksize); |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | return error; |
| 268 | } |
| 269 | |
| 270 | static int host_request(struct mmc *dev, |
| 271 | struct mmc_cmd *cmd, |
| 272 | struct mmc_data *data) |
| 273 | { |
| 274 | int result; |
| 275 | |
| 276 | if (data) |
| 277 | result = do_data_transfer(dev, cmd, data); |
| 278 | else |
| 279 | result = do_command(dev, cmd); |
| 280 | |
| 281 | return result; |
| 282 | } |
| 283 | |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 284 | static int host_set_ios(struct mmc *dev) |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 285 | { |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 286 | struct pl180_mmc_host *host = dev->priv; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 287 | u32 sdi_clkcr; |
| 288 | |
| 289 | sdi_clkcr = readl(&host->base->clock); |
| 290 | |
| 291 | /* Ramp up the clock rate */ |
| 292 | if (dev->clock) { |
| 293 | u32 clkdiv = 0; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 294 | u32 tmp_clock; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 295 | |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 296 | if (dev->clock >= dev->cfg->f_max) { |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 297 | clkdiv = 0; |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 298 | dev->clock = dev->cfg->f_max; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 299 | } else { |
| 300 | clkdiv = (host->clock_in / dev->clock) - 2; |
| 301 | } |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 302 | |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 303 | tmp_clock = host->clock_in / (clkdiv + 2); |
| 304 | while (tmp_clock > dev->clock) { |
| 305 | clkdiv++; |
| 306 | tmp_clock = host->clock_in / (clkdiv + 2); |
| 307 | } |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 308 | |
| 309 | if (clkdiv > SDI_CLKCR_CLKDIV_MASK) |
| 310 | clkdiv = SDI_CLKCR_CLKDIV_MASK; |
| 311 | |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 312 | tmp_clock = host->clock_in / (clkdiv + 2); |
| 313 | dev->clock = tmp_clock; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 314 | sdi_clkcr &= ~(SDI_CLKCR_CLKDIV_MASK); |
| 315 | sdi_clkcr |= clkdiv; |
| 316 | } |
| 317 | |
| 318 | /* Set the bus width */ |
| 319 | if (dev->bus_width) { |
| 320 | u32 buswidth = 0; |
| 321 | |
| 322 | switch (dev->bus_width) { |
| 323 | case 1: |
| 324 | buswidth |= SDI_CLKCR_WIDBUS_1; |
| 325 | break; |
| 326 | case 4: |
| 327 | buswidth |= SDI_CLKCR_WIDBUS_4; |
| 328 | break; |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 329 | case 8: |
| 330 | buswidth |= SDI_CLKCR_WIDBUS_8; |
| 331 | break; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 332 | default: |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 333 | printf("Invalid bus width: %d\n", dev->bus_width); |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 334 | break; |
| 335 | } |
| 336 | sdi_clkcr &= ~(SDI_CLKCR_WIDBUS_MASK); |
| 337 | sdi_clkcr |= buswidth; |
| 338 | } |
| 339 | |
| 340 | writel(sdi_clkcr, &host->base->clock); |
| 341 | udelay(CLK_CHANGE_DELAY); |
Jaehoon Chung | b6cd1d3 | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 342 | |
| 343 | return 0; |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 344 | } |
| 345 | |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 346 | #ifndef CONFIG_DM_MMC |
| 347 | /* MMC uses open drain drivers in the enumeration phase */ |
| 348 | static int mmc_host_reset(struct mmc *dev) |
| 349 | { |
| 350 | struct pl180_mmc_host *host = dev->priv; |
| 351 | |
| 352 | writel(host->pwr_init, &host->base->power); |
| 353 | |
| 354 | return 0; |
| 355 | } |
| 356 | |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 357 | static const struct mmc_ops arm_pl180_mmci_ops = { |
| 358 | .send_cmd = host_request, |
| 359 | .set_ios = host_set_ios, |
| 360 | .init = mmc_host_reset, |
| 361 | }; |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 362 | #endif |
Pantelis Antoniou | c9e7591 | 2014-02-26 19:28:45 +0200 | [diff] [blame] | 363 | |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 364 | /* |
| 365 | * mmc_host_init - initialize the mmc controller. |
| 366 | * Set initial clock and power for mmc slot. |
| 367 | * Initialize mmc struct and register with mmc framework. |
| 368 | */ |
Patrice Chotard | 2a392fe | 2017-10-23 10:57:30 +0200 | [diff] [blame] | 369 | int arm_pl180_mmci_init(struct pl180_mmc_host *host, struct mmc **mmc) |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 370 | { |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 371 | u32 sdi_u32; |
| 372 | |
John Rigby | 03f609b | 2012-07-31 08:59:31 +0000 | [diff] [blame] | 373 | writel(host->pwr_init, &host->base->power); |
| 374 | writel(host->clkdiv_init, &host->base->clock); |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 375 | udelay(CLK_CHANGE_DELAY); |
| 376 | |
| 377 | /* Disable mmc interrupts */ |
| 378 | sdi_u32 = readl(&host->base->mask0) & ~SDI_MASK0_MASK; |
| 379 | writel(sdi_u32, &host->base->mask0); |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 380 | |
| 381 | host->cfg.name = host->name; |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 382 | #ifndef CONFIG_DM_MMC |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 383 | host->cfg.ops = &arm_pl180_mmci_ops; |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 384 | #endif |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 385 | /* TODO remove the duplicates */ |
| 386 | host->cfg.host_caps = host->caps; |
| 387 | host->cfg.voltages = host->voltages; |
| 388 | host->cfg.f_min = host->clock_min; |
| 389 | host->cfg.f_max = host->clock_max; |
| 390 | if (host->b_max != 0) |
| 391 | host->cfg.b_max = host->b_max; |
| 392 | else |
| 393 | host->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 394 | |
Patrice Chotard | 2a392fe | 2017-10-23 10:57:30 +0200 | [diff] [blame] | 395 | *mmc = mmc_create(&host->cfg, host); |
| 396 | if (!*mmc) |
Pantelis Antoniou | 2c85046 | 2014-03-11 19:34:20 +0200 | [diff] [blame] | 397 | return -1; |
| 398 | |
Patrice Chotard | 2a392fe | 2017-10-23 10:57:30 +0200 | [diff] [blame] | 399 | debug("registered mmc interface number is:%d\n", |
| 400 | (*mmc)->block_dev.devnum); |
Matt Waddel | 17eb497 | 2011-04-16 11:54:07 +0000 | [diff] [blame] | 401 | |
| 402 | return 0; |
| 403 | } |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 404 | |
| 405 | #ifdef CONFIG_DM_MMC |
| 406 | static int arm_pl180_mmc_probe(struct udevice *dev) |
| 407 | { |
| 408 | struct arm_pl180_mmc_plat *pdata = dev_get_platdata(dev); |
| 409 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 410 | struct mmc *mmc = &pdata->mmc; |
| 411 | struct pl180_mmc_host *host = mmc->priv; |
Patrice Chotard | 879dbab | 2017-10-23 10:57:33 +0200 | [diff] [blame^] | 412 | struct clk clk; |
Patrice Chotard | 45fc9e6 | 2017-10-23 10:57:32 +0200 | [diff] [blame] | 413 | u32 bus_width; |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 414 | int ret; |
| 415 | |
Patrice Chotard | 879dbab | 2017-10-23 10:57:33 +0200 | [diff] [blame^] | 416 | ret = clk_get_by_index(dev, 0, &clk); |
| 417 | if (ret < 0) |
| 418 | return ret; |
| 419 | |
| 420 | ret = clk_enable(&clk); |
| 421 | if (ret) { |
| 422 | dev_err(dev, "failed to enable clock\n"); |
| 423 | return ret; |
| 424 | } |
| 425 | |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 426 | strcpy(host->name, "MMC"); |
| 427 | host->pwr_init = INIT_PWR; |
| 428 | host->clkdiv_init = SDI_CLKCR_CLKDIV_INIT_V1 | SDI_CLKCR_CLKEN | |
| 429 | SDI_CLKCR_HWFC_EN; |
| 430 | host->voltages = VOLTAGE_WINDOW_SD; |
| 431 | host->caps = 0; |
Patrice Chotard | 879dbab | 2017-10-23 10:57:33 +0200 | [diff] [blame^] | 432 | host->clock_in = clk_get_rate(&clk); |
| 433 | host->clock_min = host->clock_in / (2 * (SDI_CLKCR_CLKDIV_INIT_V1 + 1)); |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 434 | host->clock_max = dev_read_u32_default(dev, "max-frequency", |
| 435 | MMC_CLOCK_MAX); |
| 436 | host->version2 = dev_get_driver_data(dev); |
Patrice Chotard | 45fc9e6 | 2017-10-23 10:57:32 +0200 | [diff] [blame] | 437 | |
| 438 | bus_width = dev_read_u32_default(dev, "bus-width", 1); |
| 439 | switch (bus_width) { |
| 440 | case 8: |
| 441 | host->caps |= MMC_MODE_8BIT; |
| 442 | /* Hosts capable of 8-bit transfers can also do 4 bits */ |
| 443 | case 4: |
| 444 | host->caps |= MMC_MODE_4BIT; |
| 445 | break; |
| 446 | case 1: |
| 447 | break; |
| 448 | default: |
| 449 | dev_err(dev, "Invalid bus-width value %u\n", bus_width); |
| 450 | } |
| 451 | |
Patrice Chotard | fcce420 | 2017-10-23 10:57:31 +0200 | [diff] [blame] | 452 | ret = arm_pl180_mmci_init(host, &mmc); |
| 453 | if (ret) { |
| 454 | dev_err(dev, "arm_pl180_mmci init failed\n"); |
| 455 | return ret; |
| 456 | } |
| 457 | |
| 458 | mmc->dev = dev; |
| 459 | dev->priv = host; |
| 460 | upriv->mmc = mmc; |
| 461 | |
| 462 | return 0; |
| 463 | } |
| 464 | |
| 465 | static int dm_host_request(struct udevice *dev, struct mmc_cmd *cmd, |
| 466 | struct mmc_data *data) |
| 467 | { |
| 468 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 469 | |
| 470 | return host_request(mmc, cmd, data); |
| 471 | } |
| 472 | |
| 473 | static int dm_host_set_ios(struct udevice *dev) |
| 474 | { |
| 475 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 476 | |
| 477 | return host_set_ios(mmc); |
| 478 | } |
| 479 | |
| 480 | static const struct dm_mmc_ops arm_pl180_dm_mmc_ops = { |
| 481 | .send_cmd = dm_host_request, |
| 482 | .set_ios = dm_host_set_ios, |
| 483 | }; |
| 484 | |
| 485 | static int arm_pl180_mmc_ofdata_to_platdata(struct udevice *dev) |
| 486 | { |
| 487 | struct arm_pl180_mmc_plat *pdata = dev_get_platdata(dev); |
| 488 | struct mmc *mmc = &pdata->mmc; |
| 489 | struct pl180_mmc_host *host = mmc->priv; |
| 490 | fdt_addr_t addr; |
| 491 | |
| 492 | addr = devfdt_get_addr(dev); |
| 493 | if (addr == FDT_ADDR_T_NONE) |
| 494 | return -EINVAL; |
| 495 | |
| 496 | host->base = (void *)addr; |
| 497 | |
| 498 | return 0; |
| 499 | } |
| 500 | |
| 501 | static const struct udevice_id arm_pl180_mmc_match[] = { |
| 502 | { .compatible = "st,stm32f4xx-sdio", .data = VERSION1 }, |
| 503 | { /* sentinel */ } |
| 504 | }; |
| 505 | |
| 506 | U_BOOT_DRIVER(arm_pl180_mmc) = { |
| 507 | .name = "arm_pl180_mmc", |
| 508 | .id = UCLASS_MMC, |
| 509 | .of_match = arm_pl180_mmc_match, |
| 510 | .ops = &arm_pl180_dm_mmc_ops, |
| 511 | .probe = arm_pl180_mmc_probe, |
| 512 | .ofdata_to_platdata = arm_pl180_mmc_ofdata_to_platdata, |
| 513 | .priv_auto_alloc_size = sizeof(struct pl180_mmc_host), |
| 514 | .platdata_auto_alloc_size = sizeof(struct arm_pl180_mmc_plat), |
| 515 | }; |
| 516 | #endif |