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