Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 2 | /* |
| 3 | * bcm2835 sdhost driver. |
| 4 | * |
| 5 | * The 2835 has two SD controllers: The Arasan sdhci controller |
| 6 | * (supported by the iproc driver) and a custom sdhost controller |
| 7 | * (supported by this driver). |
| 8 | * |
| 9 | * The sdhci controller supports both sdcard and sdio. The sdhost |
| 10 | * controller supports the sdcard only, but has better performance. |
| 11 | * Also note that the rpi3 has sdio wifi, so driving the sdcard with |
| 12 | * the sdhost controller allows to use the sdhci controller for wifi |
| 13 | * support. |
| 14 | * |
| 15 | * The configuration is done by devicetree via pin muxing. Both |
| 16 | * SD controller are available on the same pins (2 pin groups = pin 22 |
| 17 | * to 27 + pin 48 to 53). So it's possible to use both SD controllers |
| 18 | * at the same time with different pin groups. |
| 19 | * |
| 20 | * This code was ported to U-Boot by |
| 21 | * Alexander Graf <agraf@suse.de> |
| 22 | * and is based on drivers/mmc/host/bcm2835.c in Linux which is written by |
| 23 | * Phil Elwell <phil@raspberrypi.org> |
| 24 | * Copyright (C) 2015-2016 Raspberry Pi (Trading) Ltd. |
| 25 | * which is based on |
| 26 | * mmc-bcm2835.c by Gellert Weisz |
| 27 | * which is, in turn, based on |
| 28 | * sdhci-bcm2708.c by Broadcom |
| 29 | * sdhci-bcm2835.c by Stephen Warren and Oleksandr Tymoshenko |
| 30 | * sdhci.c and sdhci-pci.c by Pierre Ossman |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 31 | */ |
| 32 | #include <clk.h> |
| 33 | #include <common.h> |
| 34 | #include <dm.h> |
| 35 | #include <mmc.h> |
| 36 | #include <asm/arch/msg.h> |
Jonathan Gray | 8ae1f82 | 2018-03-17 16:15:48 +1100 | [diff] [blame] | 37 | #include <asm/arch/mbox.h> |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 38 | #include <asm/unaligned.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 39 | #include <dm/device_compat.h> |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 40 | #include <linux/compat.h> |
| 41 | #include <linux/io.h> |
| 42 | #include <linux/iopoll.h> |
| 43 | #include <linux/sizes.h> |
| 44 | #include <mach/gpio.h> |
| 45 | #include <power/regulator.h> |
| 46 | |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 47 | #define msleep(a) udelay(a * 1000) |
| 48 | |
| 49 | #define SDCMD 0x00 /* Command to SD card - 16 R/W */ |
| 50 | #define SDARG 0x04 /* Argument to SD card - 32 R/W */ |
| 51 | #define SDTOUT 0x08 /* Start value for timeout counter - 32 R/W */ |
| 52 | #define SDCDIV 0x0c /* Start value for clock divider - 11 R/W */ |
| 53 | #define SDRSP0 0x10 /* SD card response (31:0) - 32 R */ |
| 54 | #define SDRSP1 0x14 /* SD card response (63:32) - 32 R */ |
| 55 | #define SDRSP2 0x18 /* SD card response (95:64) - 32 R */ |
| 56 | #define SDRSP3 0x1c /* SD card response (127:96) - 32 R */ |
| 57 | #define SDHSTS 0x20 /* SD host status - 11 R/W */ |
| 58 | #define SDVDD 0x30 /* SD card power control - 1 R/W */ |
| 59 | #define SDEDM 0x34 /* Emergency Debug Mode - 13 R/W */ |
| 60 | #define SDHCFG 0x38 /* Host configuration - 2 R/W */ |
| 61 | #define SDHBCT 0x3c /* Host byte count (debug) - 32 R/W */ |
| 62 | #define SDDATA 0x40 /* Data to/from SD card - 32 R/W */ |
| 63 | #define SDHBLC 0x50 /* Host block count (SDIO/SDHC) - 9 R/W */ |
| 64 | |
| 65 | #define SDCMD_NEW_FLAG 0x8000 |
| 66 | #define SDCMD_FAIL_FLAG 0x4000 |
| 67 | #define SDCMD_BUSYWAIT 0x800 |
| 68 | #define SDCMD_NO_RESPONSE 0x400 |
| 69 | #define SDCMD_LONG_RESPONSE 0x200 |
| 70 | #define SDCMD_WRITE_CMD 0x80 |
| 71 | #define SDCMD_READ_CMD 0x40 |
| 72 | #define SDCMD_CMD_MASK 0x3f |
| 73 | |
| 74 | #define SDCDIV_MAX_CDIV 0x7ff |
| 75 | |
| 76 | #define SDHSTS_BUSY_IRPT 0x400 |
| 77 | #define SDHSTS_BLOCK_IRPT 0x200 |
| 78 | #define SDHSTS_SDIO_IRPT 0x100 |
| 79 | #define SDHSTS_REW_TIME_OUT 0x80 |
| 80 | #define SDHSTS_CMD_TIME_OUT 0x40 |
| 81 | #define SDHSTS_CRC16_ERROR 0x20 |
| 82 | #define SDHSTS_CRC7_ERROR 0x10 |
| 83 | #define SDHSTS_FIFO_ERROR 0x08 |
| 84 | #define SDHSTS_DATA_FLAG 0x01 |
| 85 | |
| 86 | #define SDHSTS_CLEAR_MASK (SDHSTS_BUSY_IRPT | \ |
| 87 | SDHSTS_BLOCK_IRPT | \ |
| 88 | SDHSTS_SDIO_IRPT | \ |
| 89 | SDHSTS_REW_TIME_OUT | \ |
| 90 | SDHSTS_CMD_TIME_OUT | \ |
| 91 | SDHSTS_CRC16_ERROR | \ |
| 92 | SDHSTS_CRC7_ERROR | \ |
| 93 | SDHSTS_FIFO_ERROR) |
| 94 | |
| 95 | #define SDHSTS_TRANSFER_ERROR_MASK (SDHSTS_CRC7_ERROR | \ |
| 96 | SDHSTS_CRC16_ERROR | \ |
| 97 | SDHSTS_REW_TIME_OUT | \ |
| 98 | SDHSTS_FIFO_ERROR) |
| 99 | |
| 100 | #define SDHSTS_ERROR_MASK (SDHSTS_CMD_TIME_OUT | \ |
| 101 | SDHSTS_TRANSFER_ERROR_MASK) |
| 102 | |
| 103 | #define SDHCFG_BUSY_IRPT_EN BIT(10) |
| 104 | #define SDHCFG_BLOCK_IRPT_EN BIT(8) |
| 105 | #define SDHCFG_SDIO_IRPT_EN BIT(5) |
| 106 | #define SDHCFG_DATA_IRPT_EN BIT(4) |
| 107 | #define SDHCFG_SLOW_CARD BIT(3) |
| 108 | #define SDHCFG_WIDE_EXT_BUS BIT(2) |
| 109 | #define SDHCFG_WIDE_INT_BUS BIT(1) |
| 110 | #define SDHCFG_REL_CMD_LINE BIT(0) |
| 111 | |
| 112 | #define SDVDD_POWER_OFF 0 |
| 113 | #define SDVDD_POWER_ON 1 |
| 114 | |
| 115 | #define SDEDM_FORCE_DATA_MODE BIT(19) |
| 116 | #define SDEDM_CLOCK_PULSE BIT(20) |
| 117 | #define SDEDM_BYPASS BIT(21) |
| 118 | |
| 119 | #define SDEDM_FIFO_FILL_SHIFT 4 |
| 120 | #define SDEDM_FIFO_FILL_MASK 0x1f |
| 121 | static u32 edm_fifo_fill(u32 edm) |
| 122 | { |
| 123 | return (edm >> SDEDM_FIFO_FILL_SHIFT) & SDEDM_FIFO_FILL_MASK; |
| 124 | } |
| 125 | |
| 126 | #define SDEDM_WRITE_THRESHOLD_SHIFT 9 |
| 127 | #define SDEDM_READ_THRESHOLD_SHIFT 14 |
| 128 | #define SDEDM_THRESHOLD_MASK 0x1f |
| 129 | |
| 130 | #define SDEDM_FSM_MASK 0xf |
| 131 | #define SDEDM_FSM_IDENTMODE 0x0 |
| 132 | #define SDEDM_FSM_DATAMODE 0x1 |
| 133 | #define SDEDM_FSM_READDATA 0x2 |
| 134 | #define SDEDM_FSM_WRITEDATA 0x3 |
| 135 | #define SDEDM_FSM_READWAIT 0x4 |
| 136 | #define SDEDM_FSM_READCRC 0x5 |
| 137 | #define SDEDM_FSM_WRITECRC 0x6 |
| 138 | #define SDEDM_FSM_WRITEWAIT1 0x7 |
| 139 | #define SDEDM_FSM_POWERDOWN 0x8 |
| 140 | #define SDEDM_FSM_POWERUP 0x9 |
| 141 | #define SDEDM_FSM_WRITESTART1 0xa |
| 142 | #define SDEDM_FSM_WRITESTART2 0xb |
| 143 | #define SDEDM_FSM_GENPULSES 0xc |
| 144 | #define SDEDM_FSM_WRITEWAIT2 0xd |
| 145 | #define SDEDM_FSM_STARTPOWDOWN 0xf |
| 146 | |
| 147 | #define SDDATA_FIFO_WORDS 16 |
| 148 | |
| 149 | #define FIFO_READ_THRESHOLD 4 |
| 150 | #define FIFO_WRITE_THRESHOLD 4 |
| 151 | #define SDDATA_FIFO_PIO_BURST 8 |
| 152 | |
| 153 | #define SDHST_TIMEOUT_MAX_USEC 100000 |
| 154 | |
| 155 | struct bcm2835_plat { |
| 156 | struct mmc_config cfg; |
| 157 | struct mmc mmc; |
| 158 | }; |
| 159 | |
| 160 | struct bcm2835_host { |
| 161 | void __iomem *ioaddr; |
| 162 | u32 phys_addr; |
| 163 | |
| 164 | int clock; /* Current clock speed */ |
| 165 | unsigned int max_clk; /* Max possible freq */ |
| 166 | unsigned int blocks; /* remaining PIO blocks */ |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 167 | |
| 168 | u32 ns_per_fifo_word; |
| 169 | |
| 170 | /* cached registers */ |
| 171 | u32 hcfg; |
| 172 | u32 cdiv; |
| 173 | |
| 174 | struct mmc_cmd *cmd; /* Current command */ |
| 175 | struct mmc_data *data; /* Current data request */ |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 176 | bool use_busy:1; /* Wait for busy interrupt */ |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 177 | |
| 178 | struct udevice *dev; |
| 179 | struct mmc *mmc; |
| 180 | struct bcm2835_plat *plat; |
| 181 | }; |
| 182 | |
| 183 | static void bcm2835_dumpregs(struct bcm2835_host *host) |
| 184 | { |
| 185 | dev_dbg(dev, "=========== REGISTER DUMP ===========\n"); |
| 186 | dev_dbg(dev, "SDCMD 0x%08x\n", readl(host->ioaddr + SDCMD)); |
| 187 | dev_dbg(dev, "SDARG 0x%08x\n", readl(host->ioaddr + SDARG)); |
| 188 | dev_dbg(dev, "SDTOUT 0x%08x\n", readl(host->ioaddr + SDTOUT)); |
| 189 | dev_dbg(dev, "SDCDIV 0x%08x\n", readl(host->ioaddr + SDCDIV)); |
| 190 | dev_dbg(dev, "SDRSP0 0x%08x\n", readl(host->ioaddr + SDRSP0)); |
| 191 | dev_dbg(dev, "SDRSP1 0x%08x\n", readl(host->ioaddr + SDRSP1)); |
| 192 | dev_dbg(dev, "SDRSP2 0x%08x\n", readl(host->ioaddr + SDRSP2)); |
| 193 | dev_dbg(dev, "SDRSP3 0x%08x\n", readl(host->ioaddr + SDRSP3)); |
| 194 | dev_dbg(dev, "SDHSTS 0x%08x\n", readl(host->ioaddr + SDHSTS)); |
| 195 | dev_dbg(dev, "SDVDD 0x%08x\n", readl(host->ioaddr + SDVDD)); |
| 196 | dev_dbg(dev, "SDEDM 0x%08x\n", readl(host->ioaddr + SDEDM)); |
| 197 | dev_dbg(dev, "SDHCFG 0x%08x\n", readl(host->ioaddr + SDHCFG)); |
| 198 | dev_dbg(dev, "SDHBCT 0x%08x\n", readl(host->ioaddr + SDHBCT)); |
| 199 | dev_dbg(dev, "SDHBLC 0x%08x\n", readl(host->ioaddr + SDHBLC)); |
| 200 | dev_dbg(dev, "===========================================\n"); |
| 201 | } |
| 202 | |
| 203 | static void bcm2835_reset_internal(struct bcm2835_host *host) |
| 204 | { |
| 205 | u32 temp; |
| 206 | |
| 207 | writel(SDVDD_POWER_OFF, host->ioaddr + SDVDD); |
| 208 | writel(0, host->ioaddr + SDCMD); |
| 209 | writel(0, host->ioaddr + SDARG); |
| 210 | /* Set timeout to a big enough value so we don't hit it */ |
| 211 | writel(0xf00000, host->ioaddr + SDTOUT); |
| 212 | writel(0, host->ioaddr + SDCDIV); |
| 213 | /* Clear status register */ |
| 214 | writel(SDHSTS_CLEAR_MASK, host->ioaddr + SDHSTS); |
| 215 | writel(0, host->ioaddr + SDHCFG); |
| 216 | writel(0, host->ioaddr + SDHBCT); |
| 217 | writel(0, host->ioaddr + SDHBLC); |
| 218 | |
| 219 | /* Limit fifo usage due to silicon bug */ |
| 220 | temp = readl(host->ioaddr + SDEDM); |
| 221 | temp &= ~((SDEDM_THRESHOLD_MASK << SDEDM_READ_THRESHOLD_SHIFT) | |
| 222 | (SDEDM_THRESHOLD_MASK << SDEDM_WRITE_THRESHOLD_SHIFT)); |
| 223 | temp |= (FIFO_READ_THRESHOLD << SDEDM_READ_THRESHOLD_SHIFT) | |
| 224 | (FIFO_WRITE_THRESHOLD << SDEDM_WRITE_THRESHOLD_SHIFT); |
| 225 | writel(temp, host->ioaddr + SDEDM); |
| 226 | /* Wait for FIFO threshold to populate */ |
| 227 | msleep(20); |
| 228 | writel(SDVDD_POWER_ON, host->ioaddr + SDVDD); |
| 229 | /* Wait for all components to go through power on cycle */ |
| 230 | msleep(20); |
| 231 | host->clock = 0; |
| 232 | writel(host->hcfg, host->ioaddr + SDHCFG); |
| 233 | writel(host->cdiv, host->ioaddr + SDCDIV); |
| 234 | } |
| 235 | |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 236 | static int bcm2835_wait_transfer_complete(struct bcm2835_host *host) |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 237 | { |
Raul Benet | b112580 | 2019-06-13 14:59:57 +0100 | [diff] [blame] | 238 | ulong tstart_ms = get_timer(0); |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 239 | |
| 240 | while (1) { |
| 241 | u32 edm, fsm; |
| 242 | |
| 243 | edm = readl(host->ioaddr + SDEDM); |
| 244 | fsm = edm & SDEDM_FSM_MASK; |
| 245 | |
| 246 | if ((fsm == SDEDM_FSM_IDENTMODE) || |
| 247 | (fsm == SDEDM_FSM_DATAMODE)) |
| 248 | break; |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 249 | |
| 250 | if ((fsm == SDEDM_FSM_READWAIT) || |
| 251 | (fsm == SDEDM_FSM_WRITESTART1) || |
| 252 | (fsm == SDEDM_FSM_READDATA)) { |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 253 | writel(edm | SDEDM_FORCE_DATA_MODE, |
| 254 | host->ioaddr + SDEDM); |
| 255 | break; |
| 256 | } |
| 257 | |
Raul Benet | b112580 | 2019-06-13 14:59:57 +0100 | [diff] [blame] | 258 | /* Error out after ~1s */ |
| 259 | ulong tlapse_ms = get_timer(tstart_ms); |
| 260 | if ( tlapse_ms > 1000 /* ms */ ) { |
| 261 | |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 262 | dev_err(host->dev, |
Raul Benet | b112580 | 2019-06-13 14:59:57 +0100 | [diff] [blame] | 263 | "wait_transfer_complete - still waiting after %lu ms\n", |
| 264 | tlapse_ms); |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 265 | bcm2835_dumpregs(host); |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 266 | return -ETIMEDOUT; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 267 | } |
| 268 | } |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 269 | |
| 270 | return 0; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 271 | } |
| 272 | |
| 273 | static int bcm2835_transfer_block_pio(struct bcm2835_host *host, bool is_read) |
| 274 | { |
| 275 | struct mmc_data *data = host->data; |
| 276 | size_t blksize = data->blocksize; |
| 277 | int copy_words; |
| 278 | u32 hsts = 0; |
| 279 | u32 *buf; |
| 280 | |
| 281 | if (blksize % sizeof(u32)) |
| 282 | return -EINVAL; |
| 283 | |
| 284 | buf = is_read ? (u32 *)data->dest : (u32 *)data->src; |
| 285 | |
| 286 | if (is_read) |
| 287 | data->dest += blksize; |
| 288 | else |
| 289 | data->src += blksize; |
| 290 | |
| 291 | copy_words = blksize / sizeof(u32); |
| 292 | |
| 293 | /* |
| 294 | * Copy all contents from/to the FIFO as far as it reaches, |
| 295 | * then wait for it to fill/empty again and rewind. |
| 296 | */ |
| 297 | while (copy_words) { |
| 298 | int burst_words, words; |
| 299 | u32 edm; |
| 300 | |
| 301 | burst_words = min(SDDATA_FIFO_PIO_BURST, copy_words); |
| 302 | edm = readl(host->ioaddr + SDEDM); |
| 303 | if (is_read) |
| 304 | words = edm_fifo_fill(edm); |
| 305 | else |
| 306 | words = SDDATA_FIFO_WORDS - edm_fifo_fill(edm); |
| 307 | |
| 308 | if (words < burst_words) { |
| 309 | int fsm_state = (edm & SDEDM_FSM_MASK); |
| 310 | |
| 311 | if ((is_read && |
| 312 | (fsm_state != SDEDM_FSM_READDATA && |
| 313 | fsm_state != SDEDM_FSM_READWAIT && |
| 314 | fsm_state != SDEDM_FSM_READCRC)) || |
| 315 | (!is_read && |
| 316 | (fsm_state != SDEDM_FSM_WRITEDATA && |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 317 | fsm_state != SDEDM_FSM_WRITEWAIT1 && |
| 318 | fsm_state != SDEDM_FSM_WRITEWAIT2 && |
| 319 | fsm_state != SDEDM_FSM_WRITECRC && |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 320 | fsm_state != SDEDM_FSM_WRITESTART1 && |
| 321 | fsm_state != SDEDM_FSM_WRITESTART2))) { |
| 322 | hsts = readl(host->ioaddr + SDHSTS); |
| 323 | printf("fsm %x, hsts %08x\n", fsm_state, hsts); |
| 324 | if (hsts & SDHSTS_ERROR_MASK) |
| 325 | break; |
| 326 | } |
| 327 | |
| 328 | continue; |
| 329 | } else if (words > copy_words) { |
| 330 | words = copy_words; |
| 331 | } |
| 332 | |
| 333 | copy_words -= words; |
| 334 | |
| 335 | /* Copy current chunk to/from the FIFO */ |
| 336 | while (words) { |
| 337 | if (is_read) |
| 338 | *(buf++) = readl(host->ioaddr + SDDATA); |
| 339 | else |
| 340 | writel(*(buf++), host->ioaddr + SDDATA); |
| 341 | words--; |
| 342 | } |
| 343 | } |
| 344 | |
| 345 | return 0; |
| 346 | } |
| 347 | |
| 348 | static int bcm2835_transfer_pio(struct bcm2835_host *host) |
| 349 | { |
| 350 | u32 sdhsts; |
| 351 | bool is_read; |
| 352 | int ret = 0; |
| 353 | |
| 354 | is_read = (host->data->flags & MMC_DATA_READ) != 0; |
| 355 | ret = bcm2835_transfer_block_pio(host, is_read); |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 356 | if (ret) |
| 357 | return ret; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 358 | |
| 359 | sdhsts = readl(host->ioaddr + SDHSTS); |
| 360 | if (sdhsts & (SDHSTS_CRC16_ERROR | |
| 361 | SDHSTS_CRC7_ERROR | |
| 362 | SDHSTS_FIFO_ERROR)) { |
| 363 | printf("%s transfer error - HSTS %08x\n", |
| 364 | is_read ? "read" : "write", sdhsts); |
| 365 | ret = -EILSEQ; |
| 366 | } else if ((sdhsts & (SDHSTS_CMD_TIME_OUT | |
| 367 | SDHSTS_REW_TIME_OUT))) { |
| 368 | printf("%s timeout error - HSTS %08x\n", |
| 369 | is_read ? "read" : "write", sdhsts); |
| 370 | ret = -ETIMEDOUT; |
| 371 | } |
| 372 | |
| 373 | return ret; |
| 374 | } |
| 375 | |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 376 | static void bcm2835_prepare_data(struct bcm2835_host *host, struct mmc_cmd *cmd, |
| 377 | struct mmc_data *data) |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 378 | { |
| 379 | WARN_ON(host->data); |
| 380 | |
| 381 | host->data = data; |
| 382 | if (!data) |
| 383 | return; |
| 384 | |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 385 | /* Use PIO */ |
| 386 | host->blocks = data->blocks; |
| 387 | |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 388 | writel(data->blocksize, host->ioaddr + SDHBCT); |
| 389 | writel(data->blocks, host->ioaddr + SDHBLC); |
| 390 | } |
| 391 | |
| 392 | static u32 bcm2835_read_wait_sdcmd(struct bcm2835_host *host) |
| 393 | { |
| 394 | u32 value; |
| 395 | int ret; |
| 396 | int timeout_us = SDHST_TIMEOUT_MAX_USEC; |
| 397 | |
| 398 | ret = readl_poll_timeout(host->ioaddr + SDCMD, value, |
| 399 | !(value & SDCMD_NEW_FLAG), timeout_us); |
| 400 | if (ret == -ETIMEDOUT) |
| 401 | printf("%s: timeout (%d us)\n", __func__, timeout_us); |
| 402 | |
| 403 | return value; |
| 404 | } |
| 405 | |
| 406 | static int bcm2835_send_command(struct bcm2835_host *host, struct mmc_cmd *cmd, |
| 407 | struct mmc_data *data) |
| 408 | { |
| 409 | u32 sdcmd, sdhsts; |
| 410 | |
| 411 | WARN_ON(host->cmd); |
| 412 | |
| 413 | if ((cmd->resp_type & MMC_RSP_136) && (cmd->resp_type & MMC_RSP_BUSY)) { |
| 414 | printf("unsupported response type!\n"); |
| 415 | return -EINVAL; |
| 416 | } |
| 417 | |
| 418 | sdcmd = bcm2835_read_wait_sdcmd(host); |
| 419 | if (sdcmd & SDCMD_NEW_FLAG) { |
| 420 | printf("previous command never completed.\n"); |
| 421 | bcm2835_dumpregs(host); |
| 422 | return -EBUSY; |
| 423 | } |
| 424 | |
| 425 | host->cmd = cmd; |
| 426 | |
| 427 | /* Clear any error flags */ |
| 428 | sdhsts = readl(host->ioaddr + SDHSTS); |
| 429 | if (sdhsts & SDHSTS_ERROR_MASK) |
| 430 | writel(sdhsts, host->ioaddr + SDHSTS); |
| 431 | |
| 432 | bcm2835_prepare_data(host, cmd, data); |
| 433 | |
| 434 | writel(cmd->cmdarg, host->ioaddr + SDARG); |
| 435 | |
| 436 | sdcmd = cmd->cmdidx & SDCMD_CMD_MASK; |
| 437 | |
| 438 | host->use_busy = false; |
| 439 | if (!(cmd->resp_type & MMC_RSP_PRESENT)) { |
| 440 | sdcmd |= SDCMD_NO_RESPONSE; |
| 441 | } else { |
| 442 | if (cmd->resp_type & MMC_RSP_136) |
| 443 | sdcmd |= SDCMD_LONG_RESPONSE; |
| 444 | if (cmd->resp_type & MMC_RSP_BUSY) { |
| 445 | sdcmd |= SDCMD_BUSYWAIT; |
| 446 | host->use_busy = true; |
| 447 | } |
| 448 | } |
| 449 | |
| 450 | if (data) { |
| 451 | if (data->flags & MMC_DATA_WRITE) |
| 452 | sdcmd |= SDCMD_WRITE_CMD; |
| 453 | if (data->flags & MMC_DATA_READ) |
| 454 | sdcmd |= SDCMD_READ_CMD; |
| 455 | } |
| 456 | |
| 457 | writel(sdcmd | SDCMD_NEW_FLAG, host->ioaddr + SDCMD); |
| 458 | |
| 459 | return 0; |
| 460 | } |
| 461 | |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 462 | static int bcm2835_finish_command(struct bcm2835_host *host) |
| 463 | { |
| 464 | struct mmc_cmd *cmd = host->cmd; |
| 465 | u32 sdcmd; |
| 466 | int ret = 0; |
| 467 | |
| 468 | sdcmd = bcm2835_read_wait_sdcmd(host); |
| 469 | |
| 470 | /* Check for errors */ |
| 471 | if (sdcmd & SDCMD_NEW_FLAG) { |
| 472 | printf("command never completed.\n"); |
| 473 | bcm2835_dumpregs(host); |
| 474 | return -EIO; |
| 475 | } else if (sdcmd & SDCMD_FAIL_FLAG) { |
| 476 | u32 sdhsts = readl(host->ioaddr + SDHSTS); |
| 477 | |
| 478 | /* Clear the errors */ |
| 479 | writel(SDHSTS_ERROR_MASK, host->ioaddr + SDHSTS); |
| 480 | |
| 481 | if (!(sdhsts & SDHSTS_CRC7_ERROR) || |
| 482 | (host->cmd->cmdidx != MMC_CMD_SEND_OP_COND)) { |
| 483 | if (sdhsts & SDHSTS_CMD_TIME_OUT) { |
| 484 | ret = -ETIMEDOUT; |
| 485 | } else { |
| 486 | printf("unexpected command %d error\n", |
| 487 | host->cmd->cmdidx); |
| 488 | bcm2835_dumpregs(host); |
| 489 | ret = -EILSEQ; |
| 490 | } |
| 491 | |
| 492 | return ret; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 497 | if (cmd->resp_type & MMC_RSP_136) { |
| 498 | int i; |
| 499 | |
| 500 | for (i = 0; i < 4; i++) { |
| 501 | cmd->response[3 - i] = |
| 502 | readl(host->ioaddr + SDRSP0 + i * 4); |
| 503 | } |
| 504 | } else { |
| 505 | cmd->response[0] = readl(host->ioaddr + SDRSP0); |
| 506 | } |
| 507 | } |
| 508 | |
| 509 | /* Processed actual command. */ |
| 510 | host->cmd = NULL; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 511 | |
| 512 | return ret; |
| 513 | } |
| 514 | |
| 515 | static int bcm2835_check_cmd_error(struct bcm2835_host *host, u32 intmask) |
| 516 | { |
| 517 | int ret = -EINVAL; |
| 518 | |
| 519 | if (!(intmask & SDHSTS_ERROR_MASK)) |
| 520 | return 0; |
| 521 | |
| 522 | if (!host->cmd) |
| 523 | return -EINVAL; |
| 524 | |
| 525 | printf("sdhost_busy_irq: intmask %08x\n", intmask); |
| 526 | if (intmask & SDHSTS_CRC7_ERROR) { |
| 527 | ret = -EILSEQ; |
| 528 | } else if (intmask & (SDHSTS_CRC16_ERROR | |
| 529 | SDHSTS_FIFO_ERROR)) { |
| 530 | ret = -EILSEQ; |
| 531 | } else if (intmask & (SDHSTS_REW_TIME_OUT | SDHSTS_CMD_TIME_OUT)) { |
| 532 | ret = -ETIMEDOUT; |
| 533 | } |
| 534 | bcm2835_dumpregs(host); |
| 535 | return ret; |
| 536 | } |
| 537 | |
| 538 | static int bcm2835_check_data_error(struct bcm2835_host *host, u32 intmask) |
| 539 | { |
| 540 | int ret = 0; |
| 541 | |
| 542 | if (!host->data) |
| 543 | return 0; |
| 544 | if (intmask & (SDHSTS_CRC16_ERROR | SDHSTS_FIFO_ERROR)) |
| 545 | ret = -EILSEQ; |
| 546 | if (intmask & SDHSTS_REW_TIME_OUT) |
| 547 | ret = -ETIMEDOUT; |
| 548 | |
| 549 | if (ret) |
| 550 | printf("%s:%d %d\n", __func__, __LINE__, ret); |
| 551 | |
| 552 | return ret; |
| 553 | } |
| 554 | |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 555 | static int bcm2835_transmit(struct bcm2835_host *host) |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 556 | { |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 557 | u32 intmask = readl(host->ioaddr + SDHSTS); |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 558 | int ret; |
| 559 | |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 560 | /* Check for errors */ |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 561 | ret = bcm2835_check_data_error(host, intmask); |
| 562 | if (ret) |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 563 | return ret; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 564 | |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 565 | ret = bcm2835_check_cmd_error(host, intmask); |
| 566 | if (ret) |
| 567 | return ret; |
| 568 | |
| 569 | /* Handle wait for busy end */ |
| 570 | if (host->use_busy && (intmask & SDHSTS_BUSY_IRPT)) { |
| 571 | writel(SDHSTS_BUSY_IRPT, host->ioaddr + SDHSTS); |
| 572 | host->use_busy = false; |
| 573 | bcm2835_finish_command(host); |
| 574 | } |
| 575 | |
| 576 | /* Handle PIO data transfer */ |
| 577 | if (host->data) { |
| 578 | ret = bcm2835_transfer_pio(host); |
| 579 | if (ret) |
| 580 | return ret; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 581 | host->blocks--; |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 582 | if (host->blocks == 0) { |
| 583 | /* Wait for command to complete for real */ |
| 584 | ret = bcm2835_wait_transfer_complete(host); |
| 585 | if (ret) |
| 586 | return ret; |
| 587 | /* Transfer complete */ |
| 588 | host->data = NULL; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 589 | } |
| 590 | } |
| 591 | |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 592 | return 0; |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 593 | } |
| 594 | |
| 595 | static void bcm2835_set_clock(struct bcm2835_host *host, unsigned int clock) |
| 596 | { |
| 597 | int div; |
| 598 | |
| 599 | /* The SDCDIV register has 11 bits, and holds (div - 2). But |
| 600 | * in data mode the max is 50MHz wihout a minimum, and only |
| 601 | * the bottom 3 bits are used. Since the switch over is |
| 602 | * automatic (unless we have marked the card as slow...), |
| 603 | * chosen values have to make sense in both modes. Ident mode |
| 604 | * must be 100-400KHz, so can range check the requested |
| 605 | * clock. CMD15 must be used to return to data mode, so this |
| 606 | * can be monitored. |
| 607 | * |
| 608 | * clock 250MHz -> 0->125MHz, 1->83.3MHz, 2->62.5MHz, 3->50.0MHz |
| 609 | * 4->41.7MHz, 5->35.7MHz, 6->31.3MHz, 7->27.8MHz |
| 610 | * |
| 611 | * 623->400KHz/27.8MHz |
| 612 | * reset value (507)->491159/50MHz |
| 613 | * |
| 614 | * BUT, the 3-bit clock divisor in data mode is too small if |
| 615 | * the core clock is higher than 250MHz, so instead use the |
| 616 | * SLOW_CARD configuration bit to force the use of the ident |
| 617 | * clock divisor at all times. |
| 618 | */ |
| 619 | |
| 620 | if (clock < 100000) { |
| 621 | /* Can't stop the clock, but make it as slow as possible |
| 622 | * to show willing |
| 623 | */ |
| 624 | host->cdiv = SDCDIV_MAX_CDIV; |
| 625 | writel(host->cdiv, host->ioaddr + SDCDIV); |
| 626 | return; |
| 627 | } |
| 628 | |
| 629 | div = host->max_clk / clock; |
| 630 | if (div < 2) |
| 631 | div = 2; |
| 632 | if ((host->max_clk / div) > clock) |
| 633 | div++; |
| 634 | div -= 2; |
| 635 | |
| 636 | if (div > SDCDIV_MAX_CDIV) |
| 637 | div = SDCDIV_MAX_CDIV; |
| 638 | |
| 639 | clock = host->max_clk / (div + 2); |
| 640 | host->mmc->clock = clock; |
| 641 | |
| 642 | /* Calibrate some delays */ |
| 643 | |
| 644 | host->ns_per_fifo_word = (1000000000 / clock) * |
| 645 | ((host->mmc->card_caps & MMC_MODE_4BIT) ? 8 : 32); |
| 646 | |
| 647 | host->cdiv = div; |
| 648 | writel(host->cdiv, host->ioaddr + SDCDIV); |
| 649 | |
| 650 | /* Set the timeout to 500ms */ |
| 651 | writel(host->mmc->clock / 2, host->ioaddr + SDTOUT); |
| 652 | } |
| 653 | |
| 654 | static inline int is_power_of_2(u64 x) |
| 655 | { |
| 656 | return !(x & (x - 1)); |
| 657 | } |
| 658 | |
| 659 | static int bcm2835_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 660 | struct mmc_data *data) |
| 661 | { |
| 662 | struct bcm2835_host *host = dev_get_priv(dev); |
| 663 | u32 edm, fsm; |
| 664 | int ret = 0; |
| 665 | |
| 666 | if (data && !is_power_of_2(data->blocksize)) { |
| 667 | printf("unsupported block size (%d bytes)\n", data->blocksize); |
| 668 | |
| 669 | if (cmd) |
| 670 | return -EINVAL; |
| 671 | } |
| 672 | |
| 673 | edm = readl(host->ioaddr + SDEDM); |
| 674 | fsm = edm & SDEDM_FSM_MASK; |
| 675 | |
| 676 | if ((fsm != SDEDM_FSM_IDENTMODE) && |
| 677 | (fsm != SDEDM_FSM_DATAMODE) && |
| 678 | (cmd && cmd->cmdidx != MMC_CMD_STOP_TRANSMISSION)) { |
| 679 | printf("previous command (%d) not complete (EDM %08x)\n", |
| 680 | readl(host->ioaddr + SDCMD) & SDCMD_CMD_MASK, edm); |
| 681 | bcm2835_dumpregs(host); |
| 682 | |
| 683 | if (cmd) |
| 684 | return -EILSEQ; |
| 685 | |
| 686 | return 0; |
| 687 | } |
| 688 | |
| 689 | if (cmd) { |
| 690 | ret = bcm2835_send_command(host, cmd, data); |
| 691 | if (!ret && !host->use_busy) |
| 692 | ret = bcm2835_finish_command(host); |
| 693 | } |
| 694 | |
| 695 | /* Wait for completion of busy signal or data transfer */ |
Alexander Graf | 79fd08f | 2018-05-23 22:24:51 +0200 | [diff] [blame] | 696 | while (host->use_busy || host->data) { |
| 697 | ret = bcm2835_transmit(host); |
| 698 | if (ret) |
| 699 | break; |
| 700 | } |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 701 | |
| 702 | return ret; |
| 703 | } |
| 704 | |
| 705 | static int bcm2835_set_ios(struct udevice *dev) |
| 706 | { |
| 707 | struct bcm2835_host *host = dev_get_priv(dev); |
| 708 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 709 | |
| 710 | if (!mmc->clock || mmc->clock != host->clock) { |
| 711 | bcm2835_set_clock(host, mmc->clock); |
| 712 | host->clock = mmc->clock; |
| 713 | } |
| 714 | |
| 715 | /* set bus width */ |
| 716 | host->hcfg &= ~SDHCFG_WIDE_EXT_BUS; |
| 717 | if (mmc->bus_width == 4) |
| 718 | host->hcfg |= SDHCFG_WIDE_EXT_BUS; |
| 719 | |
| 720 | host->hcfg |= SDHCFG_WIDE_INT_BUS; |
| 721 | |
| 722 | /* Disable clever clock switching, to cope with fast core clocks */ |
| 723 | host->hcfg |= SDHCFG_SLOW_CARD; |
| 724 | |
| 725 | writel(host->hcfg, host->ioaddr + SDHCFG); |
| 726 | |
| 727 | return 0; |
| 728 | } |
| 729 | |
| 730 | static void bcm2835_add_host(struct bcm2835_host *host) |
| 731 | { |
| 732 | struct mmc_config *cfg = &host->plat->cfg; |
| 733 | |
| 734 | cfg->f_max = host->max_clk; |
| 735 | cfg->f_min = host->max_clk / SDCDIV_MAX_CDIV; |
| 736 | cfg->b_max = 65535; |
| 737 | |
| 738 | dev_dbg(dev, "f_max %d, f_min %d\n", |
| 739 | cfg->f_max, cfg->f_min); |
| 740 | |
| 741 | /* host controller capabilities */ |
| 742 | cfg->host_caps = MMC_MODE_4BIT | MMC_MODE_HS | MMC_MODE_HS_52MHz; |
| 743 | |
| 744 | /* report supported voltage ranges */ |
| 745 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 746 | |
| 747 | /* Set interrupt enables */ |
| 748 | host->hcfg = SDHCFG_BUSY_IRPT_EN; |
| 749 | |
| 750 | bcm2835_reset_internal(host); |
| 751 | } |
| 752 | |
| 753 | static int bcm2835_probe(struct udevice *dev) |
| 754 | { |
| 755 | struct bcm2835_plat *plat = dev_get_platdata(dev); |
| 756 | struct bcm2835_host *host = dev_get_priv(dev); |
| 757 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 758 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
| 759 | |
| 760 | host->dev = dev; |
| 761 | host->mmc = mmc; |
| 762 | host->plat = plat; |
| 763 | upriv->mmc = &plat->mmc; |
| 764 | plat->cfg.name = dev->name; |
| 765 | |
| 766 | host->phys_addr = devfdt_get_addr(dev); |
| 767 | if (host->phys_addr == FDT_ADDR_T_NONE) |
| 768 | return -EINVAL; |
| 769 | |
| 770 | host->ioaddr = devm_ioremap(dev, host->phys_addr, SZ_256); |
| 771 | if (!host->ioaddr) |
| 772 | return -ENOMEM; |
| 773 | |
Jonathan Gray | 8ae1f82 | 2018-03-17 16:15:48 +1100 | [diff] [blame] | 774 | host->max_clk = bcm2835_get_mmc_clock(BCM2835_MBOX_CLOCK_ID_CORE); |
Alexander Graf | c8a73a2 | 2018-01-23 18:05:22 +0100 | [diff] [blame] | 775 | |
| 776 | bcm2835_add_host(host); |
| 777 | |
| 778 | dev_dbg(dev, "%s -> OK\n", __func__); |
| 779 | |
| 780 | return 0; |
| 781 | } |
| 782 | |
| 783 | static const struct udevice_id bcm2835_match[] = { |
| 784 | { .compatible = "brcm,bcm2835-sdhost" }, |
| 785 | { } |
| 786 | }; |
| 787 | |
| 788 | static const struct dm_mmc_ops bcm2835_ops = { |
| 789 | .send_cmd = bcm2835_send_cmd, |
| 790 | .set_ios = bcm2835_set_ios, |
| 791 | }; |
| 792 | |
| 793 | static int bcm2835_bind(struct udevice *dev) |
| 794 | { |
| 795 | struct bcm2835_plat *plat = dev_get_platdata(dev); |
| 796 | |
| 797 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 798 | } |
| 799 | |
| 800 | U_BOOT_DRIVER(bcm2835_sdhost) = { |
| 801 | .name = "bcm2835-sdhost", |
| 802 | .id = UCLASS_MMC, |
| 803 | .of_match = bcm2835_match, |
| 804 | .bind = bcm2835_bind, |
| 805 | .probe = bcm2835_probe, |
| 806 | .priv_auto_alloc_size = sizeof(struct bcm2835_host), |
| 807 | .platdata_auto_alloc_size = sizeof(struct bcm2835_plat), |
| 808 | .ops = &bcm2835_ops, |
| 809 | }; |