Tom Rini | 83d290c | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 2 | /* |
| 3 | * drivers/mmc/sh_sdhi.c |
| 4 | * |
| 5 | * SD/MMC driver for Renesas rmobile ARM SoCs. |
| 6 | * |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 7 | * Copyright (C) 2011,2013-2017 Renesas Electronics Corporation |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 8 | * Copyright (C) 2014 Nobuhiro Iwamatsu <nobuhiro.iwamatsu.yj@renesas.com> |
| 9 | * Copyright (C) 2008-2009 Renesas Solutions Corp. |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 10 | */ |
| 11 | |
| 12 | #include <common.h> |
| 13 | #include <malloc.h> |
| 14 | #include <mmc.h> |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 15 | #include <dm.h> |
Simon Glass | e6f6f9e | 2020-05-10 11:39:58 -0600 | [diff] [blame^] | 16 | #include <part.h> |
Simon Glass | 336d461 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 17 | #include <dm/device_compat.h> |
Masahiro Yamada | 1221ce4 | 2016-09-21 11:28:55 +0900 | [diff] [blame] | 18 | #include <linux/errno.h> |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 19 | #include <linux/compat.h> |
| 20 | #include <linux/io.h> |
| 21 | #include <linux/sizes.h> |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 22 | #include <asm/arch/rmobile.h> |
| 23 | #include <asm/arch/sh_sdhi.h> |
Marek Vasut | 8cd46cb | 2017-07-21 23:22:56 +0200 | [diff] [blame] | 24 | #include <clk.h> |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 25 | |
| 26 | #define DRIVER_NAME "sh-sdhi" |
| 27 | |
| 28 | struct sh_sdhi_host { |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 29 | void __iomem *addr; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 30 | int ch; |
| 31 | int bus_shift; |
| 32 | unsigned long quirks; |
| 33 | unsigned char wait_int; |
| 34 | unsigned char sd_error; |
| 35 | unsigned char detect_waiting; |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 36 | unsigned char app_cmd; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 37 | }; |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 38 | |
| 39 | static inline void sh_sdhi_writeq(struct sh_sdhi_host *host, int reg, u64 val) |
| 40 | { |
| 41 | writeq(val, host->addr + (reg << host->bus_shift)); |
| 42 | } |
| 43 | |
| 44 | static inline u64 sh_sdhi_readq(struct sh_sdhi_host *host, int reg) |
| 45 | { |
| 46 | return readq(host->addr + (reg << host->bus_shift)); |
| 47 | } |
| 48 | |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 49 | static inline void sh_sdhi_writew(struct sh_sdhi_host *host, int reg, u16 val) |
| 50 | { |
| 51 | writew(val, host->addr + (reg << host->bus_shift)); |
| 52 | } |
| 53 | |
| 54 | static inline u16 sh_sdhi_readw(struct sh_sdhi_host *host, int reg) |
| 55 | { |
| 56 | return readw(host->addr + (reg << host->bus_shift)); |
| 57 | } |
| 58 | |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 59 | static void sh_sdhi_detect(struct sh_sdhi_host *host) |
| 60 | { |
| 61 | sh_sdhi_writew(host, SDHI_OPTION, |
| 62 | OPT_BUS_WIDTH_1 | sh_sdhi_readw(host, SDHI_OPTION)); |
| 63 | |
| 64 | host->detect_waiting = 0; |
| 65 | } |
| 66 | |
| 67 | static int sh_sdhi_intr(void *dev_id) |
| 68 | { |
| 69 | struct sh_sdhi_host *host = dev_id; |
| 70 | int state1 = 0, state2 = 0; |
| 71 | |
| 72 | state1 = sh_sdhi_readw(host, SDHI_INFO1); |
| 73 | state2 = sh_sdhi_readw(host, SDHI_INFO2); |
| 74 | |
| 75 | debug("%s: state1 = %x, state2 = %x\n", __func__, state1, state2); |
| 76 | |
| 77 | /* CARD Insert */ |
| 78 | if (state1 & INFO1_CARD_IN) { |
| 79 | sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_CARD_IN); |
| 80 | if (!host->detect_waiting) { |
| 81 | host->detect_waiting = 1; |
| 82 | sh_sdhi_detect(host); |
| 83 | } |
| 84 | sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END | |
| 85 | INFO1M_ACCESS_END | INFO1M_CARD_IN | |
| 86 | INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN); |
| 87 | return -EAGAIN; |
| 88 | } |
| 89 | /* CARD Removal */ |
| 90 | if (state1 & INFO1_CARD_RE) { |
| 91 | sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_CARD_RE); |
| 92 | if (!host->detect_waiting) { |
| 93 | host->detect_waiting = 1; |
| 94 | sh_sdhi_detect(host); |
| 95 | } |
| 96 | sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END | |
| 97 | INFO1M_ACCESS_END | INFO1M_CARD_RE | |
| 98 | INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN); |
| 99 | sh_sdhi_writew(host, SDHI_SDIO_INFO1_MASK, SDIO_INFO1M_ON); |
| 100 | sh_sdhi_writew(host, SDHI_SDIO_MODE, SDIO_MODE_OFF); |
| 101 | return -EAGAIN; |
| 102 | } |
| 103 | |
| 104 | if (state2 & INFO2_ALL_ERR) { |
| 105 | sh_sdhi_writew(host, SDHI_INFO2, |
| 106 | (unsigned short)~(INFO2_ALL_ERR)); |
| 107 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 108 | INFO2M_ALL_ERR | |
| 109 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 110 | host->sd_error = 1; |
| 111 | host->wait_int = 1; |
| 112 | return 0; |
| 113 | } |
| 114 | /* Respons End */ |
| 115 | if (state1 & INFO1_RESP_END) { |
| 116 | sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_RESP_END); |
| 117 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 118 | INFO1M_RESP_END | |
| 119 | sh_sdhi_readw(host, SDHI_INFO1_MASK)); |
| 120 | host->wait_int = 1; |
| 121 | return 0; |
| 122 | } |
| 123 | /* SD_BUF Read Enable */ |
| 124 | if (state2 & INFO2_BRE_ENABLE) { |
| 125 | sh_sdhi_writew(host, SDHI_INFO2, ~INFO2_BRE_ENABLE); |
| 126 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 127 | INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ | |
| 128 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 129 | host->wait_int = 1; |
| 130 | return 0; |
| 131 | } |
| 132 | /* SD_BUF Write Enable */ |
| 133 | if (state2 & INFO2_BWE_ENABLE) { |
| 134 | sh_sdhi_writew(host, SDHI_INFO2, ~INFO2_BWE_ENABLE); |
| 135 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 136 | INFO2_BWE_ENABLE | INFO2M_BUF_ILL_WRITE | |
| 137 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 138 | host->wait_int = 1; |
| 139 | return 0; |
| 140 | } |
| 141 | /* Access End */ |
| 142 | if (state1 & INFO1_ACCESS_END) { |
| 143 | sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_ACCESS_END); |
| 144 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 145 | INFO1_ACCESS_END | |
| 146 | sh_sdhi_readw(host, SDHI_INFO1_MASK)); |
| 147 | host->wait_int = 1; |
| 148 | return 0; |
| 149 | } |
| 150 | return -EAGAIN; |
| 151 | } |
| 152 | |
| 153 | static int sh_sdhi_wait_interrupt_flag(struct sh_sdhi_host *host) |
| 154 | { |
| 155 | int timeout = 10000000; |
| 156 | |
| 157 | while (1) { |
| 158 | timeout--; |
| 159 | if (timeout < 0) { |
| 160 | debug(DRIVER_NAME": %s timeout\n", __func__); |
| 161 | return 0; |
| 162 | } |
| 163 | |
| 164 | if (!sh_sdhi_intr(host)) |
| 165 | break; |
| 166 | |
| 167 | udelay(1); /* 1 usec */ |
| 168 | } |
| 169 | |
| 170 | return 1; /* Return value: NOT 0 = complete waiting */ |
| 171 | } |
| 172 | |
| 173 | static int sh_sdhi_clock_control(struct sh_sdhi_host *host, unsigned long clk) |
| 174 | { |
| 175 | u32 clkdiv, i, timeout; |
| 176 | |
| 177 | if (sh_sdhi_readw(host, SDHI_INFO2) & (1 << 14)) { |
| 178 | printf(DRIVER_NAME": Busy state ! Cannot change the clock\n"); |
| 179 | return -EBUSY; |
| 180 | } |
| 181 | |
| 182 | sh_sdhi_writew(host, SDHI_CLK_CTRL, |
| 183 | ~CLK_ENABLE & sh_sdhi_readw(host, SDHI_CLK_CTRL)); |
| 184 | |
| 185 | if (clk == 0) |
| 186 | return -EIO; |
| 187 | |
| 188 | clkdiv = 0x80; |
| 189 | i = CONFIG_SH_SDHI_FREQ >> (0x8 + 1); |
| 190 | for (; clkdiv && clk >= (i << 1); (clkdiv >>= 1)) |
| 191 | i <<= 1; |
| 192 | |
| 193 | sh_sdhi_writew(host, SDHI_CLK_CTRL, clkdiv); |
| 194 | |
| 195 | timeout = 100000; |
| 196 | /* Waiting for SD Bus busy to be cleared */ |
| 197 | while (timeout--) { |
| 198 | if ((sh_sdhi_readw(host, SDHI_INFO2) & 0x2000)) |
| 199 | break; |
| 200 | } |
| 201 | |
| 202 | if (timeout) |
| 203 | sh_sdhi_writew(host, SDHI_CLK_CTRL, |
| 204 | CLK_ENABLE | sh_sdhi_readw(host, SDHI_CLK_CTRL)); |
| 205 | else |
| 206 | return -EBUSY; |
| 207 | |
| 208 | return 0; |
| 209 | } |
| 210 | |
| 211 | static int sh_sdhi_sync_reset(struct sh_sdhi_host *host) |
| 212 | { |
| 213 | u32 timeout; |
| 214 | sh_sdhi_writew(host, SDHI_SOFT_RST, SOFT_RST_ON); |
| 215 | sh_sdhi_writew(host, SDHI_SOFT_RST, SOFT_RST_OFF); |
| 216 | sh_sdhi_writew(host, SDHI_CLK_CTRL, |
| 217 | CLK_ENABLE | sh_sdhi_readw(host, SDHI_CLK_CTRL)); |
| 218 | |
| 219 | timeout = 100000; |
| 220 | while (timeout--) { |
| 221 | if (!(sh_sdhi_readw(host, SDHI_INFO2) & INFO2_CBUSY)) |
| 222 | break; |
| 223 | udelay(100); |
| 224 | } |
| 225 | |
| 226 | if (!timeout) |
| 227 | return -EBUSY; |
| 228 | |
| 229 | if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF) |
| 230 | sh_sdhi_writew(host, SDHI_HOST_MODE, 1); |
| 231 | |
| 232 | return 0; |
| 233 | } |
| 234 | |
| 235 | static int sh_sdhi_error_manage(struct sh_sdhi_host *host) |
| 236 | { |
| 237 | unsigned short e_state1, e_state2; |
| 238 | int ret; |
| 239 | |
| 240 | host->sd_error = 0; |
| 241 | host->wait_int = 0; |
| 242 | |
| 243 | e_state1 = sh_sdhi_readw(host, SDHI_ERR_STS1); |
| 244 | e_state2 = sh_sdhi_readw(host, SDHI_ERR_STS2); |
| 245 | if (e_state2 & ERR_STS2_SYS_ERROR) { |
| 246 | if (e_state2 & ERR_STS2_RES_STOP_TIMEOUT) |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 247 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 248 | else |
| 249 | ret = -EILSEQ; |
| 250 | debug("%s: ERR_STS2 = %04x\n", |
| 251 | DRIVER_NAME, sh_sdhi_readw(host, SDHI_ERR_STS2)); |
| 252 | sh_sdhi_sync_reset(host); |
| 253 | |
| 254 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 255 | INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN); |
| 256 | return ret; |
| 257 | } |
| 258 | if (e_state1 & ERR_STS1_CRC_ERROR || e_state1 & ERR_STS1_CMD_ERROR) |
| 259 | ret = -EILSEQ; |
| 260 | else |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 261 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 262 | |
| 263 | debug("%s: ERR_STS1 = %04x\n", |
| 264 | DRIVER_NAME, sh_sdhi_readw(host, SDHI_ERR_STS1)); |
| 265 | sh_sdhi_sync_reset(host); |
| 266 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 267 | INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN); |
| 268 | return ret; |
| 269 | } |
| 270 | |
| 271 | static int sh_sdhi_single_read(struct sh_sdhi_host *host, struct mmc_data *data) |
| 272 | { |
| 273 | long time; |
| 274 | unsigned short blocksize, i; |
| 275 | unsigned short *p = (unsigned short *)data->dest; |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 276 | u64 *q = (u64 *)data->dest; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 277 | |
| 278 | if ((unsigned long)p & 0x00000001) { |
| 279 | debug(DRIVER_NAME": %s: The data pointer is unaligned.", |
| 280 | __func__); |
| 281 | return -EIO; |
| 282 | } |
| 283 | |
| 284 | host->wait_int = 0; |
| 285 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 286 | ~(INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ) & |
| 287 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 288 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 289 | ~INFO1M_ACCESS_END & |
| 290 | sh_sdhi_readw(host, SDHI_INFO1_MASK)); |
| 291 | time = sh_sdhi_wait_interrupt_flag(host); |
| 292 | if (time == 0 || host->sd_error != 0) |
| 293 | return sh_sdhi_error_manage(host); |
| 294 | |
| 295 | host->wait_int = 0; |
| 296 | blocksize = sh_sdhi_readw(host, SDHI_SIZE); |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 297 | if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF) |
| 298 | for (i = 0; i < blocksize / 8; i++) |
| 299 | *q++ = sh_sdhi_readq(host, SDHI_BUF0); |
| 300 | else |
| 301 | for (i = 0; i < blocksize / 2; i++) |
| 302 | *p++ = sh_sdhi_readw(host, SDHI_BUF0); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 303 | |
| 304 | time = sh_sdhi_wait_interrupt_flag(host); |
| 305 | if (time == 0 || host->sd_error != 0) |
| 306 | return sh_sdhi_error_manage(host); |
| 307 | |
| 308 | host->wait_int = 0; |
| 309 | return 0; |
| 310 | } |
| 311 | |
| 312 | static int sh_sdhi_multi_read(struct sh_sdhi_host *host, struct mmc_data *data) |
| 313 | { |
| 314 | long time; |
| 315 | unsigned short blocksize, i, sec; |
| 316 | unsigned short *p = (unsigned short *)data->dest; |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 317 | u64 *q = (u64 *)data->dest; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 318 | |
| 319 | if ((unsigned long)p & 0x00000001) { |
| 320 | debug(DRIVER_NAME": %s: The data pointer is unaligned.", |
| 321 | __func__); |
| 322 | return -EIO; |
| 323 | } |
| 324 | |
| 325 | debug("%s: blocks = %d, blocksize = %d\n", |
| 326 | __func__, data->blocks, data->blocksize); |
| 327 | |
| 328 | host->wait_int = 0; |
| 329 | for (sec = 0; sec < data->blocks; sec++) { |
| 330 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 331 | ~(INFO2M_BRE_ENABLE | INFO2M_BUF_ILL_READ) & |
| 332 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 333 | |
| 334 | time = sh_sdhi_wait_interrupt_flag(host); |
| 335 | if (time == 0 || host->sd_error != 0) |
| 336 | return sh_sdhi_error_manage(host); |
| 337 | |
| 338 | host->wait_int = 0; |
| 339 | blocksize = sh_sdhi_readw(host, SDHI_SIZE); |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 340 | if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF) |
| 341 | for (i = 0; i < blocksize / 8; i++) |
| 342 | *q++ = sh_sdhi_readq(host, SDHI_BUF0); |
| 343 | else |
| 344 | for (i = 0; i < blocksize / 2; i++) |
| 345 | *p++ = sh_sdhi_readw(host, SDHI_BUF0); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | return 0; |
| 349 | } |
| 350 | |
| 351 | static int sh_sdhi_single_write(struct sh_sdhi_host *host, |
| 352 | struct mmc_data *data) |
| 353 | { |
| 354 | long time; |
| 355 | unsigned short blocksize, i; |
| 356 | const unsigned short *p = (const unsigned short *)data->src; |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 357 | const u64 *q = (const u64 *)data->src; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 358 | |
| 359 | if ((unsigned long)p & 0x00000001) { |
| 360 | debug(DRIVER_NAME": %s: The data pointer is unaligned.", |
| 361 | __func__); |
| 362 | return -EIO; |
| 363 | } |
| 364 | |
| 365 | debug("%s: blocks = %d, blocksize = %d\n", |
| 366 | __func__, data->blocks, data->blocksize); |
| 367 | |
| 368 | host->wait_int = 0; |
| 369 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 370 | ~(INFO2M_BWE_ENABLE | INFO2M_BUF_ILL_WRITE) & |
| 371 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 372 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 373 | ~INFO1M_ACCESS_END & |
| 374 | sh_sdhi_readw(host, SDHI_INFO1_MASK)); |
| 375 | |
| 376 | time = sh_sdhi_wait_interrupt_flag(host); |
| 377 | if (time == 0 || host->sd_error != 0) |
| 378 | return sh_sdhi_error_manage(host); |
| 379 | |
| 380 | host->wait_int = 0; |
| 381 | blocksize = sh_sdhi_readw(host, SDHI_SIZE); |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 382 | if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF) |
| 383 | for (i = 0; i < blocksize / 8; i++) |
| 384 | sh_sdhi_writeq(host, SDHI_BUF0, *q++); |
| 385 | else |
| 386 | for (i = 0; i < blocksize / 2; i++) |
| 387 | sh_sdhi_writew(host, SDHI_BUF0, *p++); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 388 | |
| 389 | time = sh_sdhi_wait_interrupt_flag(host); |
| 390 | if (time == 0 || host->sd_error != 0) |
| 391 | return sh_sdhi_error_manage(host); |
| 392 | |
| 393 | host->wait_int = 0; |
| 394 | return 0; |
| 395 | } |
| 396 | |
| 397 | static int sh_sdhi_multi_write(struct sh_sdhi_host *host, struct mmc_data *data) |
| 398 | { |
| 399 | long time; |
| 400 | unsigned short i, sec, blocksize; |
| 401 | const unsigned short *p = (const unsigned short *)data->src; |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 402 | const u64 *q = (const u64 *)data->src; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 403 | |
| 404 | debug("%s: blocks = %d, blocksize = %d\n", |
| 405 | __func__, data->blocks, data->blocksize); |
| 406 | |
| 407 | host->wait_int = 0; |
| 408 | for (sec = 0; sec < data->blocks; sec++) { |
| 409 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 410 | ~(INFO2M_BWE_ENABLE | INFO2M_BUF_ILL_WRITE) & |
| 411 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 412 | |
| 413 | time = sh_sdhi_wait_interrupt_flag(host); |
| 414 | if (time == 0 || host->sd_error != 0) |
| 415 | return sh_sdhi_error_manage(host); |
| 416 | |
| 417 | host->wait_int = 0; |
| 418 | blocksize = sh_sdhi_readw(host, SDHI_SIZE); |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 419 | if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF) |
| 420 | for (i = 0; i < blocksize / 8; i++) |
| 421 | sh_sdhi_writeq(host, SDHI_BUF0, *q++); |
| 422 | else |
| 423 | for (i = 0; i < blocksize / 2; i++) |
| 424 | sh_sdhi_writew(host, SDHI_BUF0, *p++); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 425 | } |
| 426 | |
| 427 | return 0; |
| 428 | } |
| 429 | |
| 430 | static void sh_sdhi_get_response(struct sh_sdhi_host *host, struct mmc_cmd *cmd) |
| 431 | { |
| 432 | unsigned short i, j, cnt = 1; |
| 433 | unsigned short resp[8]; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 434 | |
| 435 | if (cmd->resp_type & MMC_RSP_136) { |
| 436 | cnt = 4; |
| 437 | resp[0] = sh_sdhi_readw(host, SDHI_RSP00); |
| 438 | resp[1] = sh_sdhi_readw(host, SDHI_RSP01); |
| 439 | resp[2] = sh_sdhi_readw(host, SDHI_RSP02); |
| 440 | resp[3] = sh_sdhi_readw(host, SDHI_RSP03); |
| 441 | resp[4] = sh_sdhi_readw(host, SDHI_RSP04); |
| 442 | resp[5] = sh_sdhi_readw(host, SDHI_RSP05); |
| 443 | resp[6] = sh_sdhi_readw(host, SDHI_RSP06); |
| 444 | resp[7] = sh_sdhi_readw(host, SDHI_RSP07); |
| 445 | |
| 446 | /* SDHI REGISTER SPECIFICATION */ |
| 447 | for (i = 7, j = 6; i > 0; i--) { |
| 448 | resp[i] = (resp[i] << 8) & 0xff00; |
| 449 | resp[i] |= (resp[j--] >> 8) & 0x00ff; |
| 450 | } |
| 451 | resp[0] = (resp[0] << 8) & 0xff00; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 452 | } else { |
| 453 | resp[0] = sh_sdhi_readw(host, SDHI_RSP00); |
| 454 | resp[1] = sh_sdhi_readw(host, SDHI_RSP01); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 455 | } |
| 456 | |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 457 | #if defined(__BIG_ENDIAN_BITFIELD) |
masakazu.mochizuki.wd@hitachi.com | 6f107e4 | 2016-04-12 17:11:41 +0900 | [diff] [blame] | 458 | if (cnt == 4) { |
| 459 | cmd->response[0] = (resp[6] << 16) | resp[7]; |
| 460 | cmd->response[1] = (resp[4] << 16) | resp[5]; |
| 461 | cmd->response[2] = (resp[2] << 16) | resp[3]; |
| 462 | cmd->response[3] = (resp[0] << 16) | resp[1]; |
| 463 | } else { |
| 464 | cmd->response[0] = (resp[0] << 16) | resp[1]; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 465 | } |
| 466 | #else |
masakazu.mochizuki.wd@hitachi.com | 6f107e4 | 2016-04-12 17:11:41 +0900 | [diff] [blame] | 467 | if (cnt == 4) { |
| 468 | cmd->response[0] = (resp[7] << 16) | resp[6]; |
| 469 | cmd->response[1] = (resp[5] << 16) | resp[4]; |
| 470 | cmd->response[2] = (resp[3] << 16) | resp[2]; |
| 471 | cmd->response[3] = (resp[1] << 16) | resp[0]; |
| 472 | } else { |
| 473 | cmd->response[0] = (resp[1] << 16) | resp[0]; |
| 474 | } |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 475 | #endif /* __BIG_ENDIAN_BITFIELD */ |
| 476 | } |
| 477 | |
| 478 | static unsigned short sh_sdhi_set_cmd(struct sh_sdhi_host *host, |
| 479 | struct mmc_data *data, unsigned short opc) |
| 480 | { |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 481 | if (host->app_cmd) { |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 482 | if (!data) |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 483 | host->app_cmd = 0; |
| 484 | return opc | BIT(6); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 485 | } |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 486 | |
| 487 | switch (opc) { |
| 488 | case MMC_CMD_SWITCH: |
| 489 | return opc | (data ? 0x1c00 : 0x40); |
| 490 | case MMC_CMD_SEND_EXT_CSD: |
| 491 | return opc | (data ? 0x1c00 : 0); |
| 492 | case MMC_CMD_SEND_OP_COND: |
| 493 | return opc | 0x0700; |
| 494 | case MMC_CMD_APP_CMD: |
| 495 | host->app_cmd = 1; |
| 496 | default: |
| 497 | return opc; |
| 498 | } |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 499 | } |
| 500 | |
| 501 | static unsigned short sh_sdhi_data_trans(struct sh_sdhi_host *host, |
| 502 | struct mmc_data *data, unsigned short opc) |
| 503 | { |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 504 | if (host->app_cmd) { |
| 505 | host->app_cmd = 0; |
| 506 | switch (opc) { |
| 507 | case SD_CMD_APP_SEND_SCR: |
| 508 | case SD_CMD_APP_SD_STATUS: |
| 509 | return sh_sdhi_single_read(host, data); |
| 510 | default: |
| 511 | printf(DRIVER_NAME": SD: NOT SUPPORT APP CMD = d'%04d\n", |
| 512 | opc); |
| 513 | return -EINVAL; |
| 514 | } |
| 515 | } else { |
| 516 | switch (opc) { |
| 517 | case MMC_CMD_WRITE_MULTIPLE_BLOCK: |
| 518 | return sh_sdhi_multi_write(host, data); |
| 519 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
| 520 | return sh_sdhi_multi_read(host, data); |
| 521 | case MMC_CMD_WRITE_SINGLE_BLOCK: |
| 522 | return sh_sdhi_single_write(host, data); |
| 523 | case MMC_CMD_READ_SINGLE_BLOCK: |
| 524 | case MMC_CMD_SWITCH: |
| 525 | case MMC_CMD_SEND_EXT_CSD:; |
| 526 | return sh_sdhi_single_read(host, data); |
| 527 | default: |
| 528 | printf(DRIVER_NAME": SD: NOT SUPPORT CMD = d'%04d\n", opc); |
| 529 | return -EINVAL; |
| 530 | } |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 531 | } |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | static int sh_sdhi_start_cmd(struct sh_sdhi_host *host, |
| 535 | struct mmc_data *data, struct mmc_cmd *cmd) |
| 536 | { |
| 537 | long time; |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 538 | unsigned short shcmd, opc = cmd->cmdidx; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 539 | int ret = 0; |
| 540 | unsigned long timeout; |
| 541 | |
| 542 | debug("opc = %d, arg = %x, resp_type = %x\n", |
| 543 | opc, cmd->cmdarg, cmd->resp_type); |
| 544 | |
| 545 | if (opc == MMC_CMD_STOP_TRANSMISSION) { |
| 546 | /* SDHI sends the STOP command automatically by STOP reg */ |
| 547 | sh_sdhi_writew(host, SDHI_INFO1_MASK, ~INFO1M_ACCESS_END & |
| 548 | sh_sdhi_readw(host, SDHI_INFO1_MASK)); |
| 549 | |
| 550 | time = sh_sdhi_wait_interrupt_flag(host); |
| 551 | if (time == 0 || host->sd_error != 0) |
| 552 | return sh_sdhi_error_manage(host); |
| 553 | |
| 554 | sh_sdhi_get_response(host, cmd); |
| 555 | return 0; |
| 556 | } |
| 557 | |
| 558 | if (data) { |
| 559 | if ((opc == MMC_CMD_READ_MULTIPLE_BLOCK) || |
| 560 | opc == MMC_CMD_WRITE_MULTIPLE_BLOCK) { |
| 561 | sh_sdhi_writew(host, SDHI_STOP, STOP_SEC_ENABLE); |
| 562 | sh_sdhi_writew(host, SDHI_SECCNT, data->blocks); |
| 563 | } |
| 564 | sh_sdhi_writew(host, SDHI_SIZE, data->blocksize); |
| 565 | } |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 566 | |
| 567 | shcmd = sh_sdhi_set_cmd(host, data, opc); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 568 | |
| 569 | /* |
Bin Meng | a187559 | 2016-02-05 19:30:11 -0800 | [diff] [blame] | 570 | * U-Boot cannot use interrupt. |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 571 | * So this flag may not be clear by timing |
| 572 | */ |
| 573 | sh_sdhi_writew(host, SDHI_INFO1, ~INFO1_RESP_END); |
| 574 | |
| 575 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 576 | INFO1M_RESP_END | sh_sdhi_readw(host, SDHI_INFO1_MASK)); |
| 577 | sh_sdhi_writew(host, SDHI_ARG0, |
| 578 | (unsigned short)(cmd->cmdarg & ARG0_MASK)); |
| 579 | sh_sdhi_writew(host, SDHI_ARG1, |
| 580 | (unsigned short)((cmd->cmdarg >> 16) & ARG1_MASK)); |
| 581 | |
| 582 | timeout = 100000; |
| 583 | /* Waiting for SD Bus busy to be cleared */ |
| 584 | while (timeout--) { |
| 585 | if ((sh_sdhi_readw(host, SDHI_INFO2) & 0x2000)) |
| 586 | break; |
| 587 | } |
| 588 | |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 589 | host->wait_int = 0; |
| 590 | sh_sdhi_writew(host, SDHI_INFO1_MASK, |
| 591 | ~INFO1M_RESP_END & sh_sdhi_readw(host, SDHI_INFO1_MASK)); |
| 592 | sh_sdhi_writew(host, SDHI_INFO2_MASK, |
| 593 | ~(INFO2M_CMD_ERROR | INFO2M_CRC_ERROR | |
| 594 | INFO2M_END_ERROR | INFO2M_TIMEOUT | |
| 595 | INFO2M_RESP_TIMEOUT | INFO2M_ILA) & |
| 596 | sh_sdhi_readw(host, SDHI_INFO2_MASK)); |
| 597 | |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 598 | sh_sdhi_writew(host, SDHI_CMD, (unsigned short)(shcmd & CMD_MASK)); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 599 | time = sh_sdhi_wait_interrupt_flag(host); |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 600 | if (!time) { |
| 601 | host->app_cmd = 0; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 602 | return sh_sdhi_error_manage(host); |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 603 | } |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 604 | |
| 605 | if (host->sd_error) { |
| 606 | switch (cmd->cmdidx) { |
| 607 | case MMC_CMD_ALL_SEND_CID: |
| 608 | case MMC_CMD_SELECT_CARD: |
| 609 | case SD_CMD_SEND_IF_COND: |
| 610 | case MMC_CMD_APP_CMD: |
Jaehoon Chung | 915ffa5 | 2016-07-19 16:33:36 +0900 | [diff] [blame] | 611 | ret = -ETIMEDOUT; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 612 | break; |
| 613 | default: |
| 614 | debug(DRIVER_NAME": Cmd(d'%d) err\n", opc); |
| 615 | debug(DRIVER_NAME": cmdidx = %d\n", cmd->cmdidx); |
| 616 | ret = sh_sdhi_error_manage(host); |
| 617 | break; |
| 618 | } |
| 619 | host->sd_error = 0; |
| 620 | host->wait_int = 0; |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 621 | host->app_cmd = 0; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 622 | return ret; |
| 623 | } |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 624 | |
| 625 | if (sh_sdhi_readw(host, SDHI_INFO1) & INFO1_RESP_END) { |
| 626 | host->app_cmd = 0; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 627 | return -EINVAL; |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 628 | } |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 629 | |
| 630 | if (host->wait_int) { |
| 631 | sh_sdhi_get_response(host, cmd); |
| 632 | host->wait_int = 0; |
| 633 | } |
Marek Vasut | a3f0a7d | 2017-07-21 23:22:55 +0200 | [diff] [blame] | 634 | |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 635 | if (data) |
| 636 | ret = sh_sdhi_data_trans(host, data, opc); |
| 637 | |
| 638 | debug("ret = %d, resp = %08x, %08x, %08x, %08x\n", |
| 639 | ret, cmd->response[0], cmd->response[1], |
| 640 | cmd->response[2], cmd->response[3]); |
| 641 | return ret; |
| 642 | } |
| 643 | |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 644 | static int sh_sdhi_send_cmd_common(struct sh_sdhi_host *host, |
| 645 | struct mmc_cmd *cmd, struct mmc_data *data) |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 646 | { |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 647 | host->sd_error = 0; |
| 648 | |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 649 | return sh_sdhi_start_cmd(host, data, cmd); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 650 | } |
| 651 | |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 652 | static int sh_sdhi_set_ios_common(struct sh_sdhi_host *host, struct mmc *mmc) |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 653 | { |
| 654 | int ret; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 655 | |
| 656 | ret = sh_sdhi_clock_control(host, mmc->clock); |
| 657 | if (ret) |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 658 | return -EINVAL; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 659 | |
Kouei Abe | 91a16c3 | 2017-05-13 15:51:17 +0200 | [diff] [blame] | 660 | if (mmc->bus_width == 8) |
| 661 | sh_sdhi_writew(host, SDHI_OPTION, |
| 662 | OPT_BUS_WIDTH_8 | (~OPT_BUS_WIDTH_M & |
| 663 | sh_sdhi_readw(host, SDHI_OPTION))); |
| 664 | else if (mmc->bus_width == 4) |
| 665 | sh_sdhi_writew(host, SDHI_OPTION, |
| 666 | OPT_BUS_WIDTH_4 | (~OPT_BUS_WIDTH_M & |
| 667 | sh_sdhi_readw(host, SDHI_OPTION))); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 668 | else |
Kouei Abe | 91a16c3 | 2017-05-13 15:51:17 +0200 | [diff] [blame] | 669 | sh_sdhi_writew(host, SDHI_OPTION, |
| 670 | OPT_BUS_WIDTH_1 | (~OPT_BUS_WIDTH_M & |
| 671 | sh_sdhi_readw(host, SDHI_OPTION))); |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 672 | |
| 673 | debug("clock = %d, buswidth = %d\n", mmc->clock, mmc->bus_width); |
Jaehoon Chung | 07b0b9c | 2016-12-30 15:30:16 +0900 | [diff] [blame] | 674 | |
| 675 | return 0; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 676 | } |
| 677 | |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 678 | static int sh_sdhi_initialize_common(struct sh_sdhi_host *host) |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 679 | { |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 680 | int ret = sh_sdhi_sync_reset(host); |
| 681 | |
| 682 | sh_sdhi_writew(host, SDHI_PORTSEL, USE_1PORT); |
| 683 | |
| 684 | #if defined(__BIG_ENDIAN_BITFIELD) |
| 685 | sh_sdhi_writew(host, SDHI_EXT_SWAP, SET_SWAP); |
| 686 | #endif |
| 687 | |
| 688 | sh_sdhi_writew(host, SDHI_INFO1_MASK, INFO1M_RESP_END | |
| 689 | INFO1M_ACCESS_END | INFO1M_CARD_RE | |
| 690 | INFO1M_DATA3_CARD_RE | INFO1M_DATA3_CARD_IN); |
| 691 | |
| 692 | return ret; |
| 693 | } |
| 694 | |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 695 | #ifndef CONFIG_DM_MMC |
| 696 | static void *mmc_priv(struct mmc *mmc) |
| 697 | { |
| 698 | return (void *)mmc->priv; |
| 699 | } |
| 700 | |
| 701 | static int sh_sdhi_send_cmd(struct mmc *mmc, struct mmc_cmd *cmd, |
| 702 | struct mmc_data *data) |
| 703 | { |
| 704 | struct sh_sdhi_host *host = mmc_priv(mmc); |
| 705 | |
| 706 | return sh_sdhi_send_cmd_common(host, cmd, data); |
| 707 | } |
| 708 | |
| 709 | static int sh_sdhi_set_ios(struct mmc *mmc) |
| 710 | { |
| 711 | struct sh_sdhi_host *host = mmc_priv(mmc); |
| 712 | |
| 713 | return sh_sdhi_set_ios_common(host, mmc); |
| 714 | } |
| 715 | |
| 716 | static int sh_sdhi_initialize(struct mmc *mmc) |
| 717 | { |
| 718 | struct sh_sdhi_host *host = mmc_priv(mmc); |
| 719 | |
| 720 | return sh_sdhi_initialize_common(host); |
| 721 | } |
| 722 | |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 723 | static const struct mmc_ops sh_sdhi_ops = { |
| 724 | .send_cmd = sh_sdhi_send_cmd, |
| 725 | .set_ios = sh_sdhi_set_ios, |
| 726 | .init = sh_sdhi_initialize, |
| 727 | }; |
| 728 | |
Kouei Abe | a5950f8 | 2017-05-13 15:51:18 +0200 | [diff] [blame] | 729 | #ifdef CONFIG_RCAR_GEN3 |
| 730 | static struct mmc_config sh_sdhi_cfg = { |
| 731 | .name = DRIVER_NAME, |
| 732 | .ops = &sh_sdhi_ops, |
| 733 | .f_min = CLKDEV_INIT, |
| 734 | .f_max = CLKDEV_HS_DATA, |
| 735 | .voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34, |
| 736 | .host_caps = MMC_MODE_4BIT | MMC_MODE_8BIT | MMC_MODE_HS | |
| 737 | MMC_MODE_HS_52MHz, |
| 738 | .part_type = PART_TYPE_DOS, |
| 739 | .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT, |
| 740 | }; |
| 741 | #else |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 742 | static struct mmc_config sh_sdhi_cfg = { |
| 743 | .name = DRIVER_NAME, |
| 744 | .ops = &sh_sdhi_ops, |
| 745 | .f_min = CLKDEV_INIT, |
| 746 | .f_max = CLKDEV_HS_DATA, |
| 747 | .voltages = MMC_VDD_32_33 | MMC_VDD_33_34, |
| 748 | .host_caps = MMC_MODE_4BIT | MMC_MODE_HS, |
| 749 | .part_type = PART_TYPE_DOS, |
| 750 | .b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT, |
| 751 | }; |
Kouei Abe | a5950f8 | 2017-05-13 15:51:18 +0200 | [diff] [blame] | 752 | #endif |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 753 | |
| 754 | int sh_sdhi_init(unsigned long addr, int ch, unsigned long quirks) |
| 755 | { |
| 756 | int ret = 0; |
| 757 | struct mmc *mmc; |
| 758 | struct sh_sdhi_host *host = NULL; |
| 759 | |
| 760 | if (ch >= CONFIG_SYS_SH_SDHI_NR_CHANNEL) |
| 761 | return -ENODEV; |
| 762 | |
| 763 | host = malloc(sizeof(struct sh_sdhi_host)); |
| 764 | if (!host) |
| 765 | return -ENOMEM; |
| 766 | |
| 767 | mmc = mmc_create(&sh_sdhi_cfg, host); |
| 768 | if (!mmc) { |
| 769 | ret = -1; |
| 770 | goto error; |
| 771 | } |
| 772 | |
| 773 | host->ch = ch; |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 774 | host->addr = (void __iomem *)addr; |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 775 | host->quirks = quirks; |
| 776 | |
Kouei Abe | 5eada1d | 2017-05-13 15:51:16 +0200 | [diff] [blame] | 777 | if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF) |
| 778 | host->bus_shift = 2; |
| 779 | else if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF) |
Nobuhiro Iwamatsu | 72d42ba | 2014-12-17 08:03:00 +0900 | [diff] [blame] | 780 | host->bus_shift = 1; |
| 781 | |
| 782 | return ret; |
| 783 | error: |
| 784 | if (host) |
| 785 | free(host); |
| 786 | return ret; |
| 787 | } |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 788 | |
| 789 | #else |
| 790 | |
| 791 | struct sh_sdhi_plat { |
| 792 | struct mmc_config cfg; |
| 793 | struct mmc mmc; |
| 794 | }; |
| 795 | |
| 796 | int sh_sdhi_dm_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 797 | struct mmc_data *data) |
| 798 | { |
| 799 | struct sh_sdhi_host *host = dev_get_priv(dev); |
| 800 | |
| 801 | return sh_sdhi_send_cmd_common(host, cmd, data); |
| 802 | } |
| 803 | |
| 804 | int sh_sdhi_dm_set_ios(struct udevice *dev) |
| 805 | { |
| 806 | struct sh_sdhi_host *host = dev_get_priv(dev); |
| 807 | struct mmc *mmc = mmc_get_mmc_dev(dev); |
| 808 | |
| 809 | return sh_sdhi_set_ios_common(host, mmc); |
| 810 | } |
| 811 | |
| 812 | static const struct dm_mmc_ops sh_sdhi_dm_ops = { |
| 813 | .send_cmd = sh_sdhi_dm_send_cmd, |
| 814 | .set_ios = sh_sdhi_dm_set_ios, |
| 815 | }; |
| 816 | |
| 817 | static int sh_sdhi_dm_bind(struct udevice *dev) |
| 818 | { |
| 819 | struct sh_sdhi_plat *plat = dev_get_platdata(dev); |
| 820 | |
| 821 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 822 | } |
| 823 | |
| 824 | static int sh_sdhi_dm_probe(struct udevice *dev) |
| 825 | { |
| 826 | struct sh_sdhi_plat *plat = dev_get_platdata(dev); |
| 827 | struct sh_sdhi_host *host = dev_get_priv(dev); |
| 828 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Marek Vasut | 8cd46cb | 2017-07-21 23:22:56 +0200 | [diff] [blame] | 829 | struct clk sh_sdhi_clk; |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 830 | const u32 quirks = dev_get_driver_data(dev); |
| 831 | fdt_addr_t base; |
Marek Vasut | 8cd46cb | 2017-07-21 23:22:56 +0200 | [diff] [blame] | 832 | int ret; |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 833 | |
| 834 | base = devfdt_get_addr(dev); |
| 835 | if (base == FDT_ADDR_T_NONE) |
| 836 | return -EINVAL; |
| 837 | |
| 838 | host->addr = devm_ioremap(dev, base, SZ_2K); |
| 839 | if (!host->addr) |
| 840 | return -ENOMEM; |
| 841 | |
Marek Vasut | 8cd46cb | 2017-07-21 23:22:56 +0200 | [diff] [blame] | 842 | ret = clk_get_by_index(dev, 0, &sh_sdhi_clk); |
| 843 | if (ret) { |
| 844 | debug("failed to get clock, ret=%d\n", ret); |
| 845 | return ret; |
| 846 | } |
| 847 | |
| 848 | ret = clk_enable(&sh_sdhi_clk); |
| 849 | if (ret) { |
| 850 | debug("failed to enable clock, ret=%d\n", ret); |
| 851 | return ret; |
| 852 | } |
| 853 | |
Marek Vasut | d1c18ca | 2017-07-21 23:22:54 +0200 | [diff] [blame] | 854 | host->quirks = quirks; |
| 855 | |
| 856 | if (host->quirks & SH_SDHI_QUIRK_64BIT_BUF) |
| 857 | host->bus_shift = 2; |
| 858 | else if (host->quirks & SH_SDHI_QUIRK_16BIT_BUF) |
| 859 | host->bus_shift = 1; |
| 860 | |
| 861 | plat->cfg.name = dev->name; |
| 862 | plat->cfg.host_caps = MMC_MODE_HS_52MHz | MMC_MODE_HS; |
| 863 | |
| 864 | switch (fdtdec_get_int(gd->fdt_blob, dev_of_offset(dev), "bus-width", |
| 865 | 1)) { |
| 866 | case 8: |
| 867 | plat->cfg.host_caps |= MMC_MODE_8BIT; |
| 868 | break; |
| 869 | case 4: |
| 870 | plat->cfg.host_caps |= MMC_MODE_4BIT; |
| 871 | break; |
| 872 | case 1: |
| 873 | break; |
| 874 | default: |
| 875 | dev_err(dev, "Invalid \"bus-width\" value\n"); |
| 876 | return -EINVAL; |
| 877 | } |
| 878 | |
| 879 | sh_sdhi_initialize_common(host); |
| 880 | |
| 881 | plat->cfg.voltages = MMC_VDD_165_195 | MMC_VDD_32_33 | MMC_VDD_33_34; |
| 882 | plat->cfg.f_min = CLKDEV_INIT; |
| 883 | plat->cfg.f_max = CLKDEV_HS_DATA; |
| 884 | plat->cfg.b_max = CONFIG_SYS_MMC_MAX_BLK_COUNT; |
| 885 | |
| 886 | upriv->mmc = &plat->mmc; |
| 887 | |
| 888 | return 0; |
| 889 | } |
| 890 | |
| 891 | static const struct udevice_id sh_sdhi_sd_match[] = { |
| 892 | { .compatible = "renesas,sdhi-r8a7795", .data = SH_SDHI_QUIRK_64BIT_BUF }, |
| 893 | { .compatible = "renesas,sdhi-r8a7796", .data = SH_SDHI_QUIRK_64BIT_BUF }, |
| 894 | { /* sentinel */ } |
| 895 | }; |
| 896 | |
| 897 | U_BOOT_DRIVER(sh_sdhi_mmc) = { |
| 898 | .name = "sh-sdhi-mmc", |
| 899 | .id = UCLASS_MMC, |
| 900 | .of_match = sh_sdhi_sd_match, |
| 901 | .bind = sh_sdhi_dm_bind, |
| 902 | .probe = sh_sdhi_dm_probe, |
| 903 | .priv_auto_alloc_size = sizeof(struct sh_sdhi_host), |
| 904 | .platdata_auto_alloc_size = sizeof(struct sh_sdhi_plat), |
| 905 | .ops = &sh_sdhi_dm_ops, |
| 906 | }; |
| 907 | #endif |