developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
| 2 | /* |
| 3 | * MediaTek SD/MMC Card Interface driver |
| 4 | * |
| 5 | * Copyright (C) 2018 MediaTek Inc. |
| 6 | * Author: Weijie Gao <weijie.gao@mediatek.com> |
| 7 | */ |
| 8 | |
| 9 | #include <clk.h> |
| 10 | #include <common.h> |
| 11 | #include <dm.h> |
| 12 | #include <mmc.h> |
| 13 | #include <errno.h> |
| 14 | #include <malloc.h> |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 15 | #include <mapmem.h> |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 16 | #include <stdbool.h> |
| 17 | #include <asm/gpio.h> |
Simon Glass | 9bc1564 | 2020-02-03 07:36:16 -0700 | [diff] [blame] | 18 | #include <dm/device_compat.h> |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 19 | #include <dm/pinctrl.h> |
| 20 | #include <linux/bitops.h> |
| 21 | #include <linux/io.h> |
| 22 | #include <linux/iopoll.h> |
| 23 | |
| 24 | /* MSDC_CFG */ |
| 25 | #define MSDC_CFG_HS400_CK_MODE_EXT BIT(22) |
| 26 | #define MSDC_CFG_CKMOD_EXT_M 0x300000 |
| 27 | #define MSDC_CFG_CKMOD_EXT_S 20 |
| 28 | #define MSDC_CFG_CKDIV_EXT_M 0xfff00 |
| 29 | #define MSDC_CFG_CKDIV_EXT_S 8 |
| 30 | #define MSDC_CFG_HS400_CK_MODE BIT(18) |
| 31 | #define MSDC_CFG_CKMOD_M 0x30000 |
| 32 | #define MSDC_CFG_CKMOD_S 16 |
| 33 | #define MSDC_CFG_CKDIV_M 0xff00 |
| 34 | #define MSDC_CFG_CKDIV_S 8 |
| 35 | #define MSDC_CFG_CKSTB BIT(7) |
| 36 | #define MSDC_CFG_PIO BIT(3) |
| 37 | #define MSDC_CFG_RST BIT(2) |
| 38 | #define MSDC_CFG_CKPDN BIT(1) |
| 39 | #define MSDC_CFG_MODE BIT(0) |
| 40 | |
| 41 | /* MSDC_IOCON */ |
| 42 | #define MSDC_IOCON_W_DSPL BIT(8) |
| 43 | #define MSDC_IOCON_DSPL BIT(2) |
| 44 | #define MSDC_IOCON_RSPL BIT(1) |
| 45 | |
| 46 | /* MSDC_PS */ |
| 47 | #define MSDC_PS_DAT0 BIT(16) |
| 48 | #define MSDC_PS_CDDBCE_M 0xf000 |
| 49 | #define MSDC_PS_CDDBCE_S 12 |
| 50 | #define MSDC_PS_CDSTS BIT(1) |
| 51 | #define MSDC_PS_CDEN BIT(0) |
| 52 | |
| 53 | /* #define MSDC_INT(EN) */ |
| 54 | #define MSDC_INT_ACMDRDY BIT(3) |
| 55 | #define MSDC_INT_ACMDTMO BIT(4) |
| 56 | #define MSDC_INT_ACMDCRCERR BIT(5) |
| 57 | #define MSDC_INT_CMDRDY BIT(8) |
| 58 | #define MSDC_INT_CMDTMO BIT(9) |
| 59 | #define MSDC_INT_RSPCRCERR BIT(10) |
| 60 | #define MSDC_INT_XFER_COMPL BIT(12) |
| 61 | #define MSDC_INT_DATTMO BIT(14) |
| 62 | #define MSDC_INT_DATCRCERR BIT(15) |
| 63 | |
| 64 | /* MSDC_FIFOCS */ |
| 65 | #define MSDC_FIFOCS_CLR BIT(31) |
| 66 | #define MSDC_FIFOCS_TXCNT_M 0xff0000 |
| 67 | #define MSDC_FIFOCS_TXCNT_S 16 |
| 68 | #define MSDC_FIFOCS_RXCNT_M 0xff |
| 69 | #define MSDC_FIFOCS_RXCNT_S 0 |
| 70 | |
| 71 | /* #define SDC_CFG */ |
| 72 | #define SDC_CFG_DTOC_M 0xff000000 |
| 73 | #define SDC_CFG_DTOC_S 24 |
| 74 | #define SDC_CFG_SDIOIDE BIT(20) |
| 75 | #define SDC_CFG_SDIO BIT(19) |
| 76 | #define SDC_CFG_BUSWIDTH_M 0x30000 |
| 77 | #define SDC_CFG_BUSWIDTH_S 16 |
| 78 | |
| 79 | /* SDC_CMD */ |
| 80 | #define SDC_CMD_BLK_LEN_M 0xfff0000 |
| 81 | #define SDC_CMD_BLK_LEN_S 16 |
| 82 | #define SDC_CMD_STOP BIT(14) |
| 83 | #define SDC_CMD_WR BIT(13) |
| 84 | #define SDC_CMD_DTYPE_M 0x1800 |
| 85 | #define SDC_CMD_DTYPE_S 11 |
| 86 | #define SDC_CMD_RSPTYP_M 0x380 |
| 87 | #define SDC_CMD_RSPTYP_S 7 |
| 88 | #define SDC_CMD_CMD_M 0x3f |
| 89 | #define SDC_CMD_CMD_S 0 |
| 90 | |
| 91 | /* SDC_STS */ |
| 92 | #define SDC_STS_CMDBUSY BIT(1) |
| 93 | #define SDC_STS_SDCBUSY BIT(0) |
| 94 | |
| 95 | /* SDC_ADV_CFG0 */ |
| 96 | #define SDC_RX_ENHANCE_EN BIT(20) |
| 97 | |
| 98 | /* PATCH_BIT0 */ |
| 99 | #define MSDC_INT_DAT_LATCH_CK_SEL_M 0x380 |
| 100 | #define MSDC_INT_DAT_LATCH_CK_SEL_S 7 |
| 101 | |
| 102 | /* PATCH_BIT1 */ |
| 103 | #define MSDC_PB1_STOP_DLY_M 0xf00 |
| 104 | #define MSDC_PB1_STOP_DLY_S 8 |
| 105 | |
| 106 | /* PATCH_BIT2 */ |
| 107 | #define MSDC_PB2_CRCSTSENSEL_M 0xe0000000 |
| 108 | #define MSDC_PB2_CRCSTSENSEL_S 29 |
| 109 | #define MSDC_PB2_CFGCRCSTS BIT(28) |
| 110 | #define MSDC_PB2_RESPSTSENSEL_M 0x70000 |
| 111 | #define MSDC_PB2_RESPSTSENSEL_S 16 |
| 112 | #define MSDC_PB2_CFGRESP BIT(15) |
| 113 | #define MSDC_PB2_RESPWAIT_M 0x0c |
| 114 | #define MSDC_PB2_RESPWAIT_S 2 |
| 115 | |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 116 | /* MSDC_PAD_CTRL0 */ |
| 117 | #define MSDC_PAD_CTRL0_CLKRDSEL_M 0xff000000 |
| 118 | #define MSDC_PAD_CTRL0_CLKRDSEL_S 24 |
| 119 | #define MSDC_PAD_CTRL0_CLKTDSEL BIT(20) |
| 120 | #define MSDC_PAD_CTRL0_CLKIES BIT(19) |
| 121 | #define MSDC_PAD_CTRL0_CLKSMT BIT(18) |
| 122 | #define MSDC_PAD_CTRL0_CLKPU BIT(17) |
| 123 | #define MSDC_PAD_CTRL0_CLKPD BIT(16) |
| 124 | #define MSDC_PAD_CTRL0_CLKSR BIT(8) |
| 125 | #define MSDC_PAD_CTRL0_CLKDRVP_M 0x70 |
| 126 | #define MSDC_PAD_CTRL0_CLKDRVP_S 4 |
| 127 | #define MSDC_PAD_CTRL0_CLKDRVN_M 0x7 |
| 128 | #define MSDC_PAD_CTRL0_CLKDRVN_S 0 |
| 129 | |
| 130 | /* MSDC_PAD_CTRL1 */ |
| 131 | #define MSDC_PAD_CTRL1_CMDRDSEL_M 0xff000000 |
| 132 | #define MSDC_PAD_CTRL1_CMDRDSEL_S 24 |
| 133 | #define MSDC_PAD_CTRL1_CMDTDSEL BIT(20) |
| 134 | #define MSDC_PAD_CTRL1_CMDIES BIT(19) |
| 135 | #define MSDC_PAD_CTRL1_CMDSMT BIT(18) |
| 136 | #define MSDC_PAD_CTRL1_CMDPU BIT(17) |
| 137 | #define MSDC_PAD_CTRL1_CMDPD BIT(16) |
| 138 | #define MSDC_PAD_CTRL1_CMDSR BIT(8) |
| 139 | #define MSDC_PAD_CTRL1_CMDDRVP_M 0x70 |
| 140 | #define MSDC_PAD_CTRL1_CMDDRVP_S 4 |
| 141 | #define MSDC_PAD_CTRL1_CMDDRVN_M 0x7 |
| 142 | #define MSDC_PAD_CTRL1_CMDDRVN_S 0 |
| 143 | |
| 144 | /* MSDC_PAD_CTRL2 */ |
| 145 | #define MSDC_PAD_CTRL2_DATRDSEL_M 0xff000000 |
| 146 | #define MSDC_PAD_CTRL2_DATRDSEL_S 24 |
| 147 | #define MSDC_PAD_CTRL2_DATTDSEL BIT(20) |
| 148 | #define MSDC_PAD_CTRL2_DATIES BIT(19) |
| 149 | #define MSDC_PAD_CTRL2_DATSMT BIT(18) |
| 150 | #define MSDC_PAD_CTRL2_DATPU BIT(17) |
| 151 | #define MSDC_PAD_CTRL2_DATPD BIT(16) |
| 152 | #define MSDC_PAD_CTRL2_DATSR BIT(8) |
| 153 | #define MSDC_PAD_CTRL2_DATDRVP_M 0x70 |
| 154 | #define MSDC_PAD_CTRL2_DATDRVP_S 4 |
| 155 | #define MSDC_PAD_CTRL2_DATDRVN_M 0x7 |
| 156 | #define MSDC_PAD_CTRL2_DATDRVN_S 0 |
| 157 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 158 | /* PAD_TUNE */ |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 159 | #define MSDC_PAD_TUNE_CLKTDLY_M 0xf8000000 |
| 160 | #define MSDC_PAD_TUNE_CLKTDLY_S 27 |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 161 | #define MSDC_PAD_TUNE_CMDRRDLY_M 0x7c00000 |
| 162 | #define MSDC_PAD_TUNE_CMDRRDLY_S 22 |
| 163 | #define MSDC_PAD_TUNE_CMD_SEL BIT(21) |
| 164 | #define MSDC_PAD_TUNE_CMDRDLY_M 0x1f0000 |
| 165 | #define MSDC_PAD_TUNE_CMDRDLY_S 16 |
| 166 | #define MSDC_PAD_TUNE_RXDLYSEL BIT(15) |
| 167 | #define MSDC_PAD_TUNE_RD_SEL BIT(13) |
| 168 | #define MSDC_PAD_TUNE_DATRRDLY_M 0x1f00 |
| 169 | #define MSDC_PAD_TUNE_DATRRDLY_S 8 |
| 170 | #define MSDC_PAD_TUNE_DATWRDLY_M 0x1f |
| 171 | #define MSDC_PAD_TUNE_DATWRDLY_S 0 |
| 172 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 173 | #define PAD_CMD_TUNE_RX_DLY3 0x3E |
| 174 | #define PAD_CMD_TUNE_RX_DLY3_S 1 |
| 175 | |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 176 | /* PAD_TUNE0 */ |
| 177 | #define MSDC_PAD_TUNE0_DAT0RDDLY_M 0x1f000000 |
| 178 | #define MSDC_PAD_TUNE0_DAT0RDDLY_S 24 |
| 179 | #define MSDC_PAD_TUNE0_DAT1RDDLY_M 0x1f0000 |
| 180 | #define MSDC_PAD_TUNE0_DAT1RDDLY_S 16 |
| 181 | #define MSDC_PAD_TUNE0_DAT2RDDLY_M 0x1f00 |
| 182 | #define MSDC_PAD_TUNE0_DAT2RDDLY_S 8 |
| 183 | #define MSDC_PAD_TUNE0_DAT3RDDLY_M 0x1f |
| 184 | #define MSDC_PAD_TUNE0_DAT3RDDLY_S 0 |
| 185 | |
| 186 | /* PAD_TUNE1 */ |
| 187 | #define MSDC_PAD_TUNE1_DAT4RDDLY_M 0x1f000000 |
| 188 | #define MSDC_PAD_TUNE1_DAT4RDDLY_S 24 |
| 189 | #define MSDC_PAD_TUNE1_DAT5RDDLY_M 0x1f0000 |
| 190 | #define MSDC_PAD_TUNE1_DAT5RDDLY_S 16 |
| 191 | #define MSDC_PAD_TUNE1_DAT6RDDLY_M 0x1f00 |
| 192 | #define MSDC_PAD_TUNE1_DAT6RDDLY_S 8 |
| 193 | #define MSDC_PAD_TUNE1_DAT7RDDLY_M 0x1f |
| 194 | #define MSDC_PAD_TUNE1_DAT7RDDLY_S 0 |
| 195 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 196 | /* EMMC50_CFG0 */ |
| 197 | #define EMMC50_CFG_CFCSTS_SEL BIT(4) |
| 198 | |
| 199 | /* SDC_FIFO_CFG */ |
| 200 | #define SDC_FIFO_CFG_WRVALIDSEL BIT(24) |
| 201 | #define SDC_FIFO_CFG_RDVALIDSEL BIT(25) |
| 202 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 203 | /* EMMC_TOP_CONTROL mask */ |
| 204 | #define PAD_RXDLY_SEL BIT(0) |
| 205 | #define DELAY_EN BIT(1) |
| 206 | #define PAD_DAT_RD_RXDLY2 (0x1f << 2) |
| 207 | #define PAD_DAT_RD_RXDLY (0x1f << 7) |
| 208 | #define PAD_DAT_RD_RXDLY_S 7 |
| 209 | #define PAD_DAT_RD_RXDLY2_SEL BIT(12) |
| 210 | #define PAD_DAT_RD_RXDLY_SEL BIT(13) |
| 211 | #define DATA_K_VALUE_SEL BIT(14) |
| 212 | #define SDC_RX_ENH_EN BIT(15) |
| 213 | |
| 214 | /* EMMC_TOP_CMD mask */ |
| 215 | #define PAD_CMD_RXDLY2 (0x1f << 0) |
| 216 | #define PAD_CMD_RXDLY (0x1f << 5) |
| 217 | #define PAD_CMD_RXDLY_S 5 |
| 218 | #define PAD_CMD_RD_RXDLY2_SEL BIT(10) |
| 219 | #define PAD_CMD_RD_RXDLY_SEL BIT(11) |
| 220 | #define PAD_CMD_TX_DLY (0x1f << 12) |
| 221 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 222 | /* SDC_CFG_BUSWIDTH */ |
| 223 | #define MSDC_BUS_1BITS 0x0 |
| 224 | #define MSDC_BUS_4BITS 0x1 |
| 225 | #define MSDC_BUS_8BITS 0x2 |
| 226 | |
| 227 | #define MSDC_FIFO_SIZE 128 |
| 228 | |
| 229 | #define PAD_DELAY_MAX 32 |
| 230 | |
| 231 | #define DEFAULT_CD_DEBOUNCE 8 |
| 232 | |
developer | c731074 | 2020-11-12 16:36:57 +0800 | [diff] [blame] | 233 | #define SCLK_CYCLES_SHIFT 20 |
| 234 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 235 | #define CMD_INTS_MASK \ |
| 236 | (MSDC_INT_CMDRDY | MSDC_INT_RSPCRCERR | MSDC_INT_CMDTMO) |
| 237 | |
| 238 | #define DATA_INTS_MASK \ |
| 239 | (MSDC_INT_XFER_COMPL | MSDC_INT_DATTMO | MSDC_INT_DATCRCERR) |
| 240 | |
| 241 | /* Register offset */ |
| 242 | struct mtk_sd_regs { |
| 243 | u32 msdc_cfg; |
| 244 | u32 msdc_iocon; |
| 245 | u32 msdc_ps; |
| 246 | u32 msdc_int; |
| 247 | u32 msdc_inten; |
| 248 | u32 msdc_fifocs; |
| 249 | u32 msdc_txdata; |
| 250 | u32 msdc_rxdata; |
| 251 | u32 reserved0[4]; |
| 252 | u32 sdc_cfg; |
| 253 | u32 sdc_cmd; |
| 254 | u32 sdc_arg; |
| 255 | u32 sdc_sts; |
| 256 | u32 sdc_resp[4]; |
| 257 | u32 sdc_blk_num; |
| 258 | u32 sdc_vol_chg; |
| 259 | u32 sdc_csts; |
| 260 | u32 sdc_csts_en; |
| 261 | u32 sdc_datcrc_sts; |
| 262 | u32 sdc_adv_cfg0; |
| 263 | u32 reserved1[2]; |
| 264 | u32 emmc_cfg0; |
| 265 | u32 emmc_cfg1; |
| 266 | u32 emmc_sts; |
| 267 | u32 emmc_iocon; |
| 268 | u32 sd_acmd_resp; |
| 269 | u32 sd_acmd19_trg; |
| 270 | u32 sd_acmd19_sts; |
| 271 | u32 dma_sa_high4bit; |
| 272 | u32 dma_sa; |
| 273 | u32 dma_ca; |
| 274 | u32 dma_ctrl; |
| 275 | u32 dma_cfg; |
| 276 | u32 sw_dbg_sel; |
| 277 | u32 sw_dbg_out; |
| 278 | u32 dma_length; |
| 279 | u32 reserved2; |
| 280 | u32 patch_bit0; |
| 281 | u32 patch_bit1; |
| 282 | u32 patch_bit2; |
| 283 | u32 reserved3; |
| 284 | u32 dat0_tune_crc; |
| 285 | u32 dat1_tune_crc; |
| 286 | u32 dat2_tune_crc; |
| 287 | u32 dat3_tune_crc; |
| 288 | u32 cmd_tune_crc; |
| 289 | u32 sdio_tune_wind; |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 290 | u32 reserved4[2]; |
| 291 | u32 pad_ctrl0; |
| 292 | u32 pad_ctrl1; |
| 293 | u32 pad_ctrl2; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 294 | u32 pad_tune; |
| 295 | u32 pad_tune0; |
| 296 | u32 pad_tune1; |
| 297 | u32 dat_rd_dly[4]; |
| 298 | u32 reserved5[2]; |
| 299 | u32 hw_dbg_sel; |
| 300 | u32 main_ver; |
| 301 | u32 eco_ver; |
| 302 | u32 reserved6[27]; |
| 303 | u32 pad_ds_tune; |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 304 | u32 pad_cmd_tune; |
| 305 | u32 reserved7[30]; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 306 | u32 emmc50_cfg0; |
| 307 | u32 reserved8[7]; |
| 308 | u32 sdc_fifo_cfg; |
| 309 | }; |
| 310 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 311 | struct msdc_top_regs { |
| 312 | u32 emmc_top_control; |
| 313 | u32 emmc_top_cmd; |
| 314 | u32 emmc50_pad_ctl0; |
| 315 | u32 emmc50_pad_ds_tune; |
| 316 | u32 emmc50_pad_dat0_tune; |
| 317 | u32 emmc50_pad_dat1_tune; |
| 318 | u32 emmc50_pad_dat2_tune; |
| 319 | u32 emmc50_pad_dat3_tune; |
| 320 | u32 emmc50_pad_dat4_tune; |
| 321 | u32 emmc50_pad_dat5_tune; |
| 322 | u32 emmc50_pad_dat6_tune; |
| 323 | u32 emmc50_pad_dat7_tune; |
| 324 | }; |
| 325 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 326 | struct msdc_compatible { |
| 327 | u8 clk_div_bits; |
| 328 | bool pad_tune0; |
| 329 | bool async_fifo; |
| 330 | bool data_tune; |
| 331 | bool busy_check; |
| 332 | bool stop_clk_fix; |
| 333 | bool enhance_rx; |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 334 | bool builtin_pad_ctrl; |
| 335 | bool default_pad_dly; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 336 | }; |
| 337 | |
| 338 | struct msdc_delay_phase { |
| 339 | u8 maxlen; |
| 340 | u8 start; |
| 341 | u8 final_phase; |
| 342 | }; |
| 343 | |
| 344 | struct msdc_plat { |
| 345 | struct mmc_config cfg; |
| 346 | struct mmc mmc; |
| 347 | }; |
| 348 | |
| 349 | struct msdc_tune_para { |
| 350 | u32 iocon; |
| 351 | u32 pad_tune; |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 352 | u32 pad_cmd_tune; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 353 | }; |
| 354 | |
| 355 | struct msdc_host { |
| 356 | struct mtk_sd_regs *base; |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 357 | struct msdc_top_regs *top_base; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 358 | struct mmc *mmc; |
| 359 | |
| 360 | struct msdc_compatible *dev_comp; |
| 361 | |
| 362 | struct clk src_clk; /* for SD/MMC bus clock */ |
Fabien Parent | 297fa1a | 2019-03-24 16:46:32 +0100 | [diff] [blame] | 363 | struct clk src_clk_cg; /* optional, MSDC source clock control gate */ |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 364 | struct clk h_clk; /* MSDC core clock */ |
| 365 | |
| 366 | u32 src_clk_freq; /* source clock */ |
| 367 | u32 mclk; /* mmc framework required bus clock */ |
| 368 | u32 sclk; /* actual calculated bus clock */ |
| 369 | |
| 370 | /* operation timeout clocks */ |
| 371 | u32 timeout_ns; |
| 372 | u32 timeout_clks; |
| 373 | |
| 374 | /* tuning options */ |
| 375 | u32 hs400_ds_delay; |
| 376 | u32 hs200_cmd_int_delay; |
| 377 | u32 hs200_write_int_delay; |
| 378 | u32 latch_ck; |
| 379 | u32 r_smpl; /* sample edge */ |
| 380 | bool hs400_mode; |
| 381 | |
| 382 | /* whether to use gpio detection or built-in hw detection */ |
| 383 | bool builtin_cd; |
developer | 399e4af | 2019-09-25 17:45:38 +0800 | [diff] [blame] | 384 | bool cd_active_high; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 385 | |
| 386 | /* card detection / write protection GPIOs */ |
Fabien Parent | 8ed608a | 2019-03-24 16:46:34 +0100 | [diff] [blame] | 387 | #if CONFIG_IS_ENABLED(DM_GPIO) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 388 | struct gpio_desc gpio_wp; |
| 389 | struct gpio_desc gpio_cd; |
| 390 | #endif |
| 391 | |
| 392 | uint last_resp_type; |
| 393 | uint last_data_write; |
| 394 | |
| 395 | enum bus_mode timing; |
| 396 | |
| 397 | struct msdc_tune_para def_tune_para; |
| 398 | struct msdc_tune_para saved_tune_para; |
| 399 | }; |
| 400 | |
| 401 | static void msdc_reset_hw(struct msdc_host *host) |
| 402 | { |
| 403 | u32 reg; |
| 404 | |
| 405 | setbits_le32(&host->base->msdc_cfg, MSDC_CFG_RST); |
| 406 | |
| 407 | readl_poll_timeout(&host->base->msdc_cfg, reg, |
| 408 | !(reg & MSDC_CFG_RST), 1000000); |
| 409 | } |
| 410 | |
| 411 | static void msdc_fifo_clr(struct msdc_host *host) |
| 412 | { |
| 413 | u32 reg; |
| 414 | |
| 415 | setbits_le32(&host->base->msdc_fifocs, MSDC_FIFOCS_CLR); |
| 416 | |
| 417 | readl_poll_timeout(&host->base->msdc_fifocs, reg, |
| 418 | !(reg & MSDC_FIFOCS_CLR), 1000000); |
| 419 | } |
| 420 | |
| 421 | static u32 msdc_fifo_rx_bytes(struct msdc_host *host) |
| 422 | { |
| 423 | return (readl(&host->base->msdc_fifocs) & |
| 424 | MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S; |
| 425 | } |
| 426 | |
| 427 | static u32 msdc_fifo_tx_bytes(struct msdc_host *host) |
| 428 | { |
| 429 | return (readl(&host->base->msdc_fifocs) & |
| 430 | MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S; |
| 431 | } |
| 432 | |
| 433 | static u32 msdc_cmd_find_resp(struct msdc_host *host, struct mmc_cmd *cmd) |
| 434 | { |
| 435 | u32 resp; |
| 436 | |
| 437 | switch (cmd->resp_type) { |
| 438 | /* Actually, R1, R5, R6, R7 are the same */ |
| 439 | case MMC_RSP_R1: |
| 440 | resp = 0x1; |
| 441 | break; |
| 442 | case MMC_RSP_R1b: |
| 443 | resp = 0x7; |
| 444 | break; |
| 445 | case MMC_RSP_R2: |
| 446 | resp = 0x2; |
| 447 | break; |
| 448 | case MMC_RSP_R3: |
| 449 | resp = 0x3; |
| 450 | break; |
| 451 | case MMC_RSP_NONE: |
| 452 | default: |
| 453 | resp = 0x0; |
| 454 | break; |
| 455 | } |
| 456 | |
| 457 | return resp; |
| 458 | } |
| 459 | |
| 460 | static u32 msdc_cmd_prepare_raw_cmd(struct msdc_host *host, |
| 461 | struct mmc_cmd *cmd, |
| 462 | struct mmc_data *data) |
| 463 | { |
| 464 | u32 opcode = cmd->cmdidx; |
| 465 | u32 resp_type = msdc_cmd_find_resp(host, cmd); |
| 466 | uint blocksize = 0; |
| 467 | u32 dtype = 0; |
| 468 | u32 rawcmd = 0; |
| 469 | |
| 470 | switch (opcode) { |
| 471 | case MMC_CMD_WRITE_MULTIPLE_BLOCK: |
| 472 | case MMC_CMD_READ_MULTIPLE_BLOCK: |
| 473 | dtype = 2; |
| 474 | break; |
| 475 | case MMC_CMD_WRITE_SINGLE_BLOCK: |
| 476 | case MMC_CMD_READ_SINGLE_BLOCK: |
| 477 | case SD_CMD_APP_SEND_SCR: |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 478 | case MMC_CMD_SEND_TUNING_BLOCK: |
| 479 | case MMC_CMD_SEND_TUNING_BLOCK_HS200: |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 480 | dtype = 1; |
| 481 | break; |
| 482 | case SD_CMD_SWITCH_FUNC: /* same as MMC_CMD_SWITCH */ |
| 483 | case SD_CMD_SEND_IF_COND: /* same as MMC_CMD_SEND_EXT_CSD */ |
| 484 | case SD_CMD_APP_SD_STATUS: /* same as MMC_CMD_SEND_STATUS */ |
| 485 | if (data) |
| 486 | dtype = 1; |
| 487 | } |
| 488 | |
| 489 | if (data) { |
| 490 | if (data->flags == MMC_DATA_WRITE) |
| 491 | rawcmd |= SDC_CMD_WR; |
| 492 | |
| 493 | if (data->blocks > 1) |
| 494 | dtype = 2; |
| 495 | |
| 496 | blocksize = data->blocksize; |
| 497 | } |
| 498 | |
| 499 | rawcmd |= ((opcode << SDC_CMD_CMD_S) & SDC_CMD_CMD_M) | |
| 500 | ((resp_type << SDC_CMD_RSPTYP_S) & SDC_CMD_RSPTYP_M) | |
| 501 | ((blocksize << SDC_CMD_BLK_LEN_S) & SDC_CMD_BLK_LEN_M) | |
| 502 | ((dtype << SDC_CMD_DTYPE_S) & SDC_CMD_DTYPE_M); |
| 503 | |
| 504 | if (opcode == MMC_CMD_STOP_TRANSMISSION) |
| 505 | rawcmd |= SDC_CMD_STOP; |
| 506 | |
| 507 | return rawcmd; |
| 508 | } |
| 509 | |
| 510 | static int msdc_cmd_done(struct msdc_host *host, int events, |
| 511 | struct mmc_cmd *cmd) |
| 512 | { |
| 513 | u32 *rsp = cmd->response; |
| 514 | int ret = 0; |
| 515 | |
| 516 | if (cmd->resp_type & MMC_RSP_PRESENT) { |
| 517 | if (cmd->resp_type & MMC_RSP_136) { |
| 518 | rsp[0] = readl(&host->base->sdc_resp[3]); |
| 519 | rsp[1] = readl(&host->base->sdc_resp[2]); |
| 520 | rsp[2] = readl(&host->base->sdc_resp[1]); |
| 521 | rsp[3] = readl(&host->base->sdc_resp[0]); |
| 522 | } else { |
| 523 | rsp[0] = readl(&host->base->sdc_resp[0]); |
| 524 | } |
| 525 | } |
| 526 | |
| 527 | if (!(events & MSDC_INT_CMDRDY)) { |
| 528 | if (cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK && |
| 529 | cmd->cmdidx != MMC_CMD_SEND_TUNING_BLOCK_HS200) |
| 530 | /* |
| 531 | * should not clear fifo/interrupt as the tune data |
| 532 | * may have alreay come. |
| 533 | */ |
| 534 | msdc_reset_hw(host); |
| 535 | |
| 536 | if (events & MSDC_INT_CMDTMO) |
| 537 | ret = -ETIMEDOUT; |
| 538 | else |
| 539 | ret = -EIO; |
| 540 | } |
| 541 | |
| 542 | return ret; |
| 543 | } |
| 544 | |
| 545 | static bool msdc_cmd_is_ready(struct msdc_host *host) |
| 546 | { |
| 547 | int ret; |
| 548 | u32 reg; |
| 549 | |
| 550 | /* The max busy time we can endure is 20ms */ |
| 551 | ret = readl_poll_timeout(&host->base->sdc_sts, reg, |
| 552 | !(reg & SDC_STS_CMDBUSY), 20000); |
| 553 | |
| 554 | if (ret) { |
| 555 | pr_err("CMD bus busy detected\n"); |
| 556 | msdc_reset_hw(host); |
| 557 | return false; |
| 558 | } |
| 559 | |
| 560 | if (host->last_resp_type == MMC_RSP_R1b && host->last_data_write) { |
| 561 | ret = readl_poll_timeout(&host->base->msdc_ps, reg, |
| 562 | reg & MSDC_PS_DAT0, 1000000); |
| 563 | |
| 564 | if (ret) { |
| 565 | pr_err("Card stuck in programming state!\n"); |
| 566 | msdc_reset_hw(host); |
| 567 | return false; |
| 568 | } |
| 569 | } |
| 570 | |
| 571 | return true; |
| 572 | } |
| 573 | |
| 574 | static int msdc_start_command(struct msdc_host *host, struct mmc_cmd *cmd, |
| 575 | struct mmc_data *data) |
| 576 | { |
| 577 | u32 rawcmd; |
| 578 | u32 status; |
| 579 | u32 blocks = 0; |
| 580 | int ret; |
| 581 | |
| 582 | if (!msdc_cmd_is_ready(host)) |
| 583 | return -EIO; |
| 584 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 585 | if ((readl(&host->base->msdc_fifocs) & |
| 586 | MSDC_FIFOCS_TXCNT_M) >> MSDC_FIFOCS_TXCNT_S || |
| 587 | (readl(&host->base->msdc_fifocs) & |
| 588 | MSDC_FIFOCS_RXCNT_M) >> MSDC_FIFOCS_RXCNT_S) { |
| 589 | pr_err("TX/RX FIFO non-empty before start of IO. Reset\n"); |
| 590 | msdc_reset_hw(host); |
| 591 | } |
| 592 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 593 | msdc_fifo_clr(host); |
| 594 | |
| 595 | host->last_resp_type = cmd->resp_type; |
| 596 | host->last_data_write = 0; |
| 597 | |
| 598 | rawcmd = msdc_cmd_prepare_raw_cmd(host, cmd, data); |
| 599 | |
| 600 | if (data) |
| 601 | blocks = data->blocks; |
| 602 | |
| 603 | writel(CMD_INTS_MASK, &host->base->msdc_int); |
developer | 068cc65 | 2019-12-31 11:29:25 +0800 | [diff] [blame] | 604 | writel(DATA_INTS_MASK, &host->base->msdc_int); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 605 | writel(blocks, &host->base->sdc_blk_num); |
| 606 | writel(cmd->cmdarg, &host->base->sdc_arg); |
| 607 | writel(rawcmd, &host->base->sdc_cmd); |
| 608 | |
| 609 | ret = readl_poll_timeout(&host->base->msdc_int, status, |
| 610 | status & CMD_INTS_MASK, 1000000); |
| 611 | |
| 612 | if (ret) |
| 613 | status = MSDC_INT_CMDTMO; |
| 614 | |
| 615 | return msdc_cmd_done(host, status, cmd); |
| 616 | } |
| 617 | |
| 618 | static void msdc_fifo_read(struct msdc_host *host, u8 *buf, u32 size) |
| 619 | { |
| 620 | u32 *wbuf; |
| 621 | |
| 622 | while ((size_t)buf % 4) { |
| 623 | *buf++ = readb(&host->base->msdc_rxdata); |
| 624 | size--; |
| 625 | } |
| 626 | |
| 627 | wbuf = (u32 *)buf; |
| 628 | while (size >= 4) { |
| 629 | *wbuf++ = readl(&host->base->msdc_rxdata); |
| 630 | size -= 4; |
| 631 | } |
| 632 | |
| 633 | buf = (u8 *)wbuf; |
| 634 | while (size) { |
| 635 | *buf++ = readb(&host->base->msdc_rxdata); |
| 636 | size--; |
| 637 | } |
| 638 | } |
| 639 | |
| 640 | static void msdc_fifo_write(struct msdc_host *host, const u8 *buf, u32 size) |
| 641 | { |
| 642 | const u32 *wbuf; |
| 643 | |
| 644 | while ((size_t)buf % 4) { |
| 645 | writeb(*buf++, &host->base->msdc_txdata); |
| 646 | size--; |
| 647 | } |
| 648 | |
| 649 | wbuf = (const u32 *)buf; |
| 650 | while (size >= 4) { |
| 651 | writel(*wbuf++, &host->base->msdc_txdata); |
| 652 | size -= 4; |
| 653 | } |
| 654 | |
| 655 | buf = (const u8 *)wbuf; |
| 656 | while (size) { |
| 657 | writeb(*buf++, &host->base->msdc_txdata); |
| 658 | size--; |
| 659 | } |
| 660 | } |
| 661 | |
| 662 | static int msdc_pio_read(struct msdc_host *host, u8 *ptr, u32 size) |
| 663 | { |
| 664 | u32 status; |
| 665 | u32 chksz; |
| 666 | int ret = 0; |
| 667 | |
| 668 | while (1) { |
| 669 | status = readl(&host->base->msdc_int); |
| 670 | writel(status, &host->base->msdc_int); |
| 671 | status &= DATA_INTS_MASK; |
| 672 | |
| 673 | if (status & MSDC_INT_DATCRCERR) { |
| 674 | ret = -EIO; |
| 675 | break; |
| 676 | } |
| 677 | |
| 678 | if (status & MSDC_INT_DATTMO) { |
| 679 | ret = -ETIMEDOUT; |
| 680 | break; |
| 681 | } |
| 682 | |
Fabien Parent | 79a6073 | 2019-01-17 18:06:00 +0100 | [diff] [blame] | 683 | chksz = min(size, (u32)MSDC_FIFO_SIZE); |
| 684 | |
| 685 | if (msdc_fifo_rx_bytes(host) >= chksz) { |
| 686 | msdc_fifo_read(host, ptr, chksz); |
| 687 | ptr += chksz; |
| 688 | size -= chksz; |
| 689 | } |
| 690 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 691 | if (status & MSDC_INT_XFER_COMPL) { |
| 692 | if (size) { |
| 693 | pr_err("data not fully read\n"); |
| 694 | ret = -EIO; |
| 695 | } |
| 696 | |
| 697 | break; |
| 698 | } |
Fabien Parent | 79a6073 | 2019-01-17 18:06:00 +0100 | [diff] [blame] | 699 | } |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 700 | |
| 701 | return ret; |
| 702 | } |
| 703 | |
| 704 | static int msdc_pio_write(struct msdc_host *host, const u8 *ptr, u32 size) |
| 705 | { |
| 706 | u32 status; |
| 707 | u32 chksz; |
| 708 | int ret = 0; |
| 709 | |
| 710 | while (1) { |
| 711 | status = readl(&host->base->msdc_int); |
| 712 | writel(status, &host->base->msdc_int); |
| 713 | status &= DATA_INTS_MASK; |
| 714 | |
| 715 | if (status & MSDC_INT_DATCRCERR) { |
| 716 | ret = -EIO; |
| 717 | break; |
| 718 | } |
| 719 | |
| 720 | if (status & MSDC_INT_DATTMO) { |
| 721 | ret = -ETIMEDOUT; |
| 722 | break; |
| 723 | } |
| 724 | |
| 725 | if (status & MSDC_INT_XFER_COMPL) { |
| 726 | if (size) { |
| 727 | pr_err("data not fully written\n"); |
| 728 | ret = -EIO; |
| 729 | } |
| 730 | |
| 731 | break; |
| 732 | } |
| 733 | |
| 734 | chksz = min(size, (u32)MSDC_FIFO_SIZE); |
| 735 | |
| 736 | if (MSDC_FIFO_SIZE - msdc_fifo_tx_bytes(host) >= chksz) { |
| 737 | msdc_fifo_write(host, ptr, chksz); |
| 738 | ptr += chksz; |
| 739 | size -= chksz; |
| 740 | } |
| 741 | } |
| 742 | |
| 743 | return ret; |
| 744 | } |
| 745 | |
| 746 | static int msdc_start_data(struct msdc_host *host, struct mmc_data *data) |
| 747 | { |
| 748 | u32 size; |
| 749 | int ret; |
| 750 | |
| 751 | if (data->flags == MMC_DATA_WRITE) |
| 752 | host->last_data_write = 1; |
| 753 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 754 | size = data->blocks * data->blocksize; |
| 755 | |
| 756 | if (data->flags == MMC_DATA_WRITE) |
| 757 | ret = msdc_pio_write(host, (const u8 *)data->src, size); |
| 758 | else |
| 759 | ret = msdc_pio_read(host, (u8 *)data->dest, size); |
| 760 | |
| 761 | if (ret) { |
| 762 | msdc_reset_hw(host); |
| 763 | msdc_fifo_clr(host); |
| 764 | } |
| 765 | |
| 766 | return ret; |
| 767 | } |
| 768 | |
| 769 | static int msdc_ops_send_cmd(struct udevice *dev, struct mmc_cmd *cmd, |
| 770 | struct mmc_data *data) |
| 771 | { |
| 772 | struct msdc_host *host = dev_get_priv(dev); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 773 | int cmd_ret, data_ret; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 774 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 775 | cmd_ret = msdc_start_command(host, cmd, data); |
| 776 | if (cmd_ret && |
| 777 | !(cmd_ret == -EIO && |
| 778 | (cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK || |
| 779 | cmd->cmdidx == MMC_CMD_SEND_TUNING_BLOCK_HS200))) |
| 780 | return cmd_ret; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 781 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 782 | if (data) { |
| 783 | data_ret = msdc_start_data(host, data); |
| 784 | if (cmd_ret) |
| 785 | return cmd_ret; |
| 786 | else |
| 787 | return data_ret; |
| 788 | } |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 789 | |
| 790 | return 0; |
| 791 | } |
| 792 | |
| 793 | static void msdc_set_timeout(struct msdc_host *host, u32 ns, u32 clks) |
| 794 | { |
developer | c731074 | 2020-11-12 16:36:57 +0800 | [diff] [blame] | 795 | u32 timeout, clk_ns, shift = SCLK_CYCLES_SHIFT; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 796 | u32 mode = 0; |
| 797 | |
| 798 | host->timeout_ns = ns; |
| 799 | host->timeout_clks = clks; |
| 800 | |
| 801 | if (host->sclk == 0) { |
| 802 | timeout = 0; |
| 803 | } else { |
| 804 | clk_ns = 1000000000UL / host->sclk; |
| 805 | timeout = (ns + clk_ns - 1) / clk_ns + clks; |
| 806 | /* unit is 1048576 sclk cycles */ |
developer | 607faf7 | 2019-09-25 17:45:37 +0800 | [diff] [blame] | 807 | timeout = (timeout + (0x1 << shift) - 1) >> shift; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 808 | if (host->dev_comp->clk_div_bits == 8) |
| 809 | mode = (readl(&host->base->msdc_cfg) & |
| 810 | MSDC_CFG_CKMOD_M) >> MSDC_CFG_CKMOD_S; |
| 811 | else |
| 812 | mode = (readl(&host->base->msdc_cfg) & |
| 813 | MSDC_CFG_CKMOD_EXT_M) >> MSDC_CFG_CKMOD_EXT_S; |
| 814 | /* DDR mode will double the clk cycles for data timeout */ |
| 815 | timeout = mode >= 2 ? timeout * 2 : timeout; |
| 816 | timeout = timeout > 1 ? timeout - 1 : 0; |
| 817 | timeout = timeout > 255 ? 255 : timeout; |
| 818 | } |
| 819 | |
| 820 | clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M, |
| 821 | timeout << SDC_CFG_DTOC_S); |
| 822 | } |
| 823 | |
| 824 | static void msdc_set_buswidth(struct msdc_host *host, u32 width) |
| 825 | { |
| 826 | u32 val = readl(&host->base->sdc_cfg); |
| 827 | |
| 828 | val &= ~SDC_CFG_BUSWIDTH_M; |
| 829 | |
| 830 | switch (width) { |
| 831 | default: |
| 832 | case 1: |
| 833 | val |= (MSDC_BUS_1BITS << SDC_CFG_BUSWIDTH_S); |
| 834 | break; |
| 835 | case 4: |
| 836 | val |= (MSDC_BUS_4BITS << SDC_CFG_BUSWIDTH_S); |
| 837 | break; |
| 838 | case 8: |
| 839 | val |= (MSDC_BUS_8BITS << SDC_CFG_BUSWIDTH_S); |
| 840 | break; |
| 841 | } |
| 842 | |
| 843 | writel(val, &host->base->sdc_cfg); |
| 844 | } |
| 845 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 846 | static void msdc_set_mclk(struct udevice *dev, |
| 847 | struct msdc_host *host, enum bus_mode timing, u32 hz) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 848 | { |
| 849 | u32 mode; |
| 850 | u32 div; |
| 851 | u32 sclk; |
| 852 | u32 reg; |
| 853 | |
| 854 | if (!hz) { |
| 855 | host->mclk = 0; |
| 856 | clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN); |
| 857 | return; |
| 858 | } |
| 859 | |
| 860 | if (host->dev_comp->clk_div_bits == 8) |
| 861 | clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_HS400_CK_MODE); |
| 862 | else |
| 863 | clrbits_le32(&host->base->msdc_cfg, |
| 864 | MSDC_CFG_HS400_CK_MODE_EXT); |
| 865 | |
| 866 | if (timing == UHS_DDR50 || timing == MMC_DDR_52 || |
| 867 | timing == MMC_HS_400) { |
| 868 | if (timing == MMC_HS_400) |
| 869 | mode = 0x3; |
| 870 | else |
| 871 | mode = 0x2; /* ddr mode and use divisor */ |
| 872 | |
| 873 | if (hz >= (host->src_clk_freq >> 2)) { |
| 874 | div = 0; /* mean div = 1/4 */ |
| 875 | sclk = host->src_clk_freq >> 2; /* sclk = clk / 4 */ |
| 876 | } else { |
| 877 | div = (host->src_clk_freq + ((hz << 2) - 1)) / |
| 878 | (hz << 2); |
| 879 | sclk = (host->src_clk_freq >> 2) / div; |
| 880 | div = (div >> 1); |
| 881 | } |
| 882 | |
| 883 | if (timing == MMC_HS_400 && hz >= (host->src_clk_freq >> 1)) { |
| 884 | if (host->dev_comp->clk_div_bits == 8) |
| 885 | setbits_le32(&host->base->msdc_cfg, |
| 886 | MSDC_CFG_HS400_CK_MODE); |
| 887 | else |
| 888 | setbits_le32(&host->base->msdc_cfg, |
| 889 | MSDC_CFG_HS400_CK_MODE_EXT); |
| 890 | |
| 891 | sclk = host->src_clk_freq >> 1; |
| 892 | div = 0; /* div is ignore when bit18 is set */ |
| 893 | } |
| 894 | } else if (hz >= host->src_clk_freq) { |
| 895 | mode = 0x1; /* no divisor */ |
| 896 | div = 0; |
| 897 | sclk = host->src_clk_freq; |
| 898 | } else { |
| 899 | mode = 0x0; /* use divisor */ |
| 900 | if (hz >= (host->src_clk_freq >> 1)) { |
| 901 | div = 0; /* mean div = 1/2 */ |
| 902 | sclk = host->src_clk_freq >> 1; /* sclk = clk / 2 */ |
| 903 | } else { |
| 904 | div = (host->src_clk_freq + ((hz << 2) - 1)) / |
| 905 | (hz << 2); |
| 906 | sclk = (host->src_clk_freq >> 2) / div; |
| 907 | } |
| 908 | } |
| 909 | |
| 910 | clrbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN); |
| 911 | |
| 912 | if (host->dev_comp->clk_div_bits == 8) { |
| 913 | div = min(div, (u32)(MSDC_CFG_CKDIV_M >> MSDC_CFG_CKDIV_S)); |
| 914 | clrsetbits_le32(&host->base->msdc_cfg, |
| 915 | MSDC_CFG_CKMOD_M | MSDC_CFG_CKDIV_M, |
| 916 | (mode << MSDC_CFG_CKMOD_S) | |
| 917 | (div << MSDC_CFG_CKDIV_S)); |
| 918 | } else { |
| 919 | div = min(div, (u32)(MSDC_CFG_CKDIV_EXT_M >> |
| 920 | MSDC_CFG_CKDIV_EXT_S)); |
| 921 | clrsetbits_le32(&host->base->msdc_cfg, |
| 922 | MSDC_CFG_CKMOD_EXT_M | MSDC_CFG_CKDIV_EXT_M, |
| 923 | (mode << MSDC_CFG_CKMOD_EXT_S) | |
| 924 | (div << MSDC_CFG_CKDIV_EXT_S)); |
| 925 | } |
| 926 | |
| 927 | readl_poll_timeout(&host->base->msdc_cfg, reg, |
| 928 | reg & MSDC_CFG_CKSTB, 1000000); |
| 929 | |
| 930 | setbits_le32(&host->base->msdc_cfg, MSDC_CFG_CKPDN); |
| 931 | host->sclk = sclk; |
| 932 | host->mclk = hz; |
| 933 | host->timing = timing; |
| 934 | |
| 935 | /* needed because clk changed. */ |
| 936 | msdc_set_timeout(host, host->timeout_ns, host->timeout_clks); |
| 937 | |
| 938 | /* |
| 939 | * mmc_select_hs400() will drop to 50Mhz and High speed mode, |
| 940 | * tune result of hs200/200Mhz is not suitable for 50Mhz |
| 941 | */ |
| 942 | if (host->sclk <= 52000000) { |
| 943 | writel(host->def_tune_para.iocon, &host->base->msdc_iocon); |
| 944 | writel(host->def_tune_para.pad_tune, |
| 945 | &host->base->pad_tune); |
| 946 | } else { |
| 947 | writel(host->saved_tune_para.iocon, &host->base->msdc_iocon); |
| 948 | writel(host->saved_tune_para.pad_tune, |
| 949 | &host->base->pad_tune); |
| 950 | } |
| 951 | |
| 952 | dev_dbg(dev, "sclk: %d, timing: %d\n", host->sclk, timing); |
| 953 | } |
| 954 | |
| 955 | static int msdc_ops_set_ios(struct udevice *dev) |
| 956 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 957 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 958 | struct msdc_host *host = dev_get_priv(dev); |
| 959 | struct mmc *mmc = &plat->mmc; |
| 960 | uint clock = mmc->clock; |
| 961 | |
| 962 | msdc_set_buswidth(host, mmc->bus_width); |
| 963 | |
| 964 | if (mmc->clk_disable) |
| 965 | clock = 0; |
| 966 | else if (clock < mmc->cfg->f_min) |
| 967 | clock = mmc->cfg->f_min; |
| 968 | |
| 969 | if (host->mclk != clock || host->timing != mmc->selected_mode) |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 970 | msdc_set_mclk(dev, host, mmc->selected_mode, clock); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 971 | |
| 972 | return 0; |
| 973 | } |
| 974 | |
| 975 | static int msdc_ops_get_cd(struct udevice *dev) |
| 976 | { |
| 977 | struct msdc_host *host = dev_get_priv(dev); |
| 978 | u32 val; |
| 979 | |
| 980 | if (host->builtin_cd) { |
| 981 | val = readl(&host->base->msdc_ps); |
developer | 399e4af | 2019-09-25 17:45:38 +0800 | [diff] [blame] | 982 | val = !!(val & MSDC_PS_CDSTS); |
| 983 | |
| 984 | return !val ^ host->cd_active_high; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 985 | } |
| 986 | |
Fabien Parent | 8ed608a | 2019-03-24 16:46:34 +0100 | [diff] [blame] | 987 | #if CONFIG_IS_ENABLED(DM_GPIO) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 988 | if (!host->gpio_cd.dev) |
| 989 | return 1; |
| 990 | |
| 991 | return dm_gpio_get_value(&host->gpio_cd); |
| 992 | #else |
| 993 | return 1; |
| 994 | #endif |
| 995 | } |
| 996 | |
| 997 | static int msdc_ops_get_wp(struct udevice *dev) |
| 998 | { |
Fabien Parent | 8ed608a | 2019-03-24 16:46:34 +0100 | [diff] [blame] | 999 | #if CONFIG_IS_ENABLED(DM_GPIO) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1000 | struct msdc_host *host = dev_get_priv(dev); |
| 1001 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1002 | if (!host->gpio_wp.dev) |
| 1003 | return 0; |
| 1004 | |
| 1005 | return !dm_gpio_get_value(&host->gpio_wp); |
| 1006 | #else |
| 1007 | return 0; |
| 1008 | #endif |
| 1009 | } |
| 1010 | |
| 1011 | #ifdef MMC_SUPPORTS_TUNING |
| 1012 | static u32 test_delay_bit(u32 delay, u32 bit) |
| 1013 | { |
| 1014 | bit %= PAD_DELAY_MAX; |
| 1015 | return delay & (1 << bit); |
| 1016 | } |
| 1017 | |
| 1018 | static int get_delay_len(u32 delay, u32 start_bit) |
| 1019 | { |
| 1020 | int i; |
| 1021 | |
| 1022 | for (i = 0; i < (PAD_DELAY_MAX - start_bit); i++) { |
| 1023 | if (test_delay_bit(delay, start_bit + i) == 0) |
| 1024 | return i; |
| 1025 | } |
| 1026 | |
| 1027 | return PAD_DELAY_MAX - start_bit; |
| 1028 | } |
| 1029 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1030 | static struct msdc_delay_phase get_best_delay(struct udevice *dev, |
| 1031 | struct msdc_host *host, u32 delay) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1032 | { |
| 1033 | int start = 0, len = 0; |
| 1034 | int start_final = 0, len_final = 0; |
| 1035 | u8 final_phase = 0xff; |
| 1036 | struct msdc_delay_phase delay_phase = { 0, }; |
| 1037 | |
| 1038 | if (delay == 0) { |
| 1039 | dev_err(dev, "phase error: [map:%x]\n", delay); |
| 1040 | delay_phase.final_phase = final_phase; |
| 1041 | return delay_phase; |
| 1042 | } |
| 1043 | |
| 1044 | while (start < PAD_DELAY_MAX) { |
| 1045 | len = get_delay_len(delay, start); |
| 1046 | if (len_final < len) { |
| 1047 | start_final = start; |
| 1048 | len_final = len; |
| 1049 | } |
| 1050 | |
| 1051 | start += len ? len : 1; |
| 1052 | if (len >= 12 && start_final < 4) |
| 1053 | break; |
| 1054 | } |
| 1055 | |
| 1056 | /* The rule is to find the smallest delay cell */ |
| 1057 | if (start_final == 0) |
| 1058 | final_phase = (start_final + len_final / 3) % PAD_DELAY_MAX; |
| 1059 | else |
| 1060 | final_phase = (start_final + len_final / 2) % PAD_DELAY_MAX; |
| 1061 | |
| 1062 | dev_info(dev, "phase: [map:%x] [maxlen:%d] [final:%d]\n", |
| 1063 | delay, len_final, final_phase); |
| 1064 | |
| 1065 | delay_phase.maxlen = len_final; |
| 1066 | delay_phase.start = start_final; |
| 1067 | delay_phase.final_phase = final_phase; |
| 1068 | return delay_phase; |
| 1069 | } |
| 1070 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1071 | static inline void msdc_set_cmd_delay(struct msdc_host *host, u32 value) |
| 1072 | { |
| 1073 | void __iomem *tune_reg = &host->base->pad_tune; |
| 1074 | |
| 1075 | if (host->dev_comp->pad_tune0) |
| 1076 | tune_reg = &host->base->pad_tune0; |
| 1077 | |
| 1078 | if (host->top_base) |
| 1079 | clrsetbits_le32(&host->top_base->emmc_top_cmd, PAD_CMD_RXDLY, |
| 1080 | value << PAD_CMD_RXDLY_S); |
| 1081 | else |
| 1082 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M, |
| 1083 | value << MSDC_PAD_TUNE_CMDRDLY_S); |
| 1084 | } |
| 1085 | |
| 1086 | static inline void msdc_set_data_delay(struct msdc_host *host, u32 value) |
| 1087 | { |
| 1088 | void __iomem *tune_reg = &host->base->pad_tune; |
| 1089 | |
| 1090 | if (host->dev_comp->pad_tune0) |
| 1091 | tune_reg = &host->base->pad_tune0; |
| 1092 | |
| 1093 | if (host->top_base) |
| 1094 | clrsetbits_le32(&host->top_base->emmc_top_control, |
| 1095 | PAD_DAT_RD_RXDLY, value << PAD_DAT_RD_RXDLY_S); |
| 1096 | else |
| 1097 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M, |
| 1098 | value << MSDC_PAD_TUNE_DATRRDLY_S); |
| 1099 | } |
| 1100 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1101 | static int hs400_tune_response(struct udevice *dev, u32 opcode) |
| 1102 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1103 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1104 | struct msdc_host *host = dev_get_priv(dev); |
| 1105 | struct mmc *mmc = &plat->mmc; |
| 1106 | u32 cmd_delay = 0; |
| 1107 | struct msdc_delay_phase final_cmd_delay = { 0, }; |
| 1108 | u8 final_delay; |
| 1109 | void __iomem *tune_reg = &host->base->pad_cmd_tune; |
| 1110 | int cmd_err; |
| 1111 | int i, j; |
| 1112 | |
| 1113 | setbits_le32(&host->base->pad_cmd_tune, BIT(0)); |
| 1114 | |
| 1115 | if (mmc->selected_mode == MMC_HS_200 || |
| 1116 | mmc->selected_mode == UHS_SDR104) |
| 1117 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M, |
| 1118 | host->hs200_cmd_int_delay << |
| 1119 | MSDC_PAD_TUNE_CMDRRDLY_S); |
| 1120 | |
| 1121 | if (host->r_smpl) |
| 1122 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1123 | else |
| 1124 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1125 | |
| 1126 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1127 | clrsetbits_le32(tune_reg, PAD_CMD_TUNE_RX_DLY3, |
| 1128 | i << PAD_CMD_TUNE_RX_DLY3_S); |
| 1129 | |
| 1130 | for (j = 0; j < 3; j++) { |
| 1131 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1132 | if (!cmd_err) { |
| 1133 | cmd_delay |= (1 << i); |
| 1134 | } else { |
| 1135 | cmd_delay &= ~(1 << i); |
| 1136 | break; |
| 1137 | } |
| 1138 | } |
| 1139 | } |
| 1140 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1141 | final_cmd_delay = get_best_delay(dev, host, cmd_delay); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1142 | clrsetbits_le32(tune_reg, PAD_CMD_TUNE_RX_DLY3, |
| 1143 | final_cmd_delay.final_phase << |
| 1144 | PAD_CMD_TUNE_RX_DLY3_S); |
| 1145 | final_delay = final_cmd_delay.final_phase; |
| 1146 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1147 | dev_info(dev, "Final cmd pad delay: %x\n", final_delay); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1148 | return final_delay == 0xff ? -EIO : 0; |
| 1149 | } |
| 1150 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1151 | static int msdc_tune_response(struct udevice *dev, u32 opcode) |
| 1152 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1153 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1154 | struct msdc_host *host = dev_get_priv(dev); |
| 1155 | struct mmc *mmc = &plat->mmc; |
| 1156 | u32 rise_delay = 0, fall_delay = 0; |
| 1157 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, }; |
| 1158 | struct msdc_delay_phase internal_delay_phase; |
| 1159 | u8 final_delay, final_maxlen; |
| 1160 | u32 internal_delay = 0; |
| 1161 | void __iomem *tune_reg = &host->base->pad_tune; |
| 1162 | int cmd_err; |
| 1163 | int i, j; |
| 1164 | |
| 1165 | if (host->dev_comp->pad_tune0) |
| 1166 | tune_reg = &host->base->pad_tune0; |
| 1167 | |
| 1168 | if (mmc->selected_mode == MMC_HS_200 || |
| 1169 | mmc->selected_mode == UHS_SDR104) |
| 1170 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M, |
| 1171 | host->hs200_cmd_int_delay << |
| 1172 | MSDC_PAD_TUNE_CMDRRDLY_S); |
| 1173 | |
| 1174 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1175 | |
| 1176 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1177 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M, |
| 1178 | i << MSDC_PAD_TUNE_CMDRDLY_S); |
| 1179 | |
| 1180 | for (j = 0; j < 3; j++) { |
| 1181 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1182 | if (!cmd_err) { |
| 1183 | rise_delay |= (1 << i); |
| 1184 | } else { |
| 1185 | rise_delay &= ~(1 << i); |
| 1186 | break; |
| 1187 | } |
| 1188 | } |
| 1189 | } |
| 1190 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1191 | final_rise_delay = get_best_delay(dev, host, rise_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1192 | /* if rising edge has enough margin, do not scan falling edge */ |
| 1193 | if (final_rise_delay.maxlen >= 12 || |
| 1194 | (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) |
| 1195 | goto skip_fall; |
| 1196 | |
| 1197 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1198 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1199 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M, |
| 1200 | i << MSDC_PAD_TUNE_CMDRDLY_S); |
| 1201 | |
| 1202 | for (j = 0; j < 3; j++) { |
| 1203 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1204 | if (!cmd_err) { |
| 1205 | fall_delay |= (1 << i); |
| 1206 | } else { |
| 1207 | fall_delay &= ~(1 << i); |
| 1208 | break; |
| 1209 | } |
| 1210 | } |
| 1211 | } |
| 1212 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1213 | final_fall_delay = get_best_delay(dev, host, fall_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1214 | |
| 1215 | skip_fall: |
| 1216 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
| 1217 | if (final_maxlen == final_rise_delay.maxlen) { |
| 1218 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1219 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M, |
| 1220 | final_rise_delay.final_phase << |
| 1221 | MSDC_PAD_TUNE_CMDRDLY_S); |
| 1222 | final_delay = final_rise_delay.final_phase; |
| 1223 | } else { |
| 1224 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1225 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRDLY_M, |
| 1226 | final_fall_delay.final_phase << |
| 1227 | MSDC_PAD_TUNE_CMDRDLY_S); |
| 1228 | final_delay = final_fall_delay.final_phase; |
| 1229 | } |
| 1230 | |
| 1231 | if (host->dev_comp->async_fifo || host->hs200_cmd_int_delay) |
| 1232 | goto skip_internal; |
| 1233 | |
| 1234 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1235 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M, |
| 1236 | i << MSDC_PAD_TUNE_CMDRRDLY_S); |
| 1237 | |
| 1238 | mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1239 | if (!cmd_err) |
| 1240 | internal_delay |= (1 << i); |
| 1241 | } |
| 1242 | |
Fabien Parent | f9ca467 | 2020-10-15 18:38:18 +0200 | [diff] [blame] | 1243 | dev_dbg(dev, "Final internal delay: 0x%x\n", internal_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1244 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1245 | internal_delay_phase = get_best_delay(dev, host, internal_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1246 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CMDRRDLY_M, |
| 1247 | internal_delay_phase.final_phase << |
| 1248 | MSDC_PAD_TUNE_CMDRRDLY_S); |
| 1249 | |
| 1250 | skip_internal: |
Fabien Parent | f9ca467 | 2020-10-15 18:38:18 +0200 | [diff] [blame] | 1251 | dev_dbg(dev, "Final cmd pad delay: %x\n", final_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1252 | return final_delay == 0xff ? -EIO : 0; |
| 1253 | } |
| 1254 | |
| 1255 | static int msdc_tune_data(struct udevice *dev, u32 opcode) |
| 1256 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1257 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1258 | struct msdc_host *host = dev_get_priv(dev); |
| 1259 | struct mmc *mmc = &plat->mmc; |
| 1260 | u32 rise_delay = 0, fall_delay = 0; |
| 1261 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, }; |
| 1262 | u8 final_delay, final_maxlen; |
| 1263 | void __iomem *tune_reg = &host->base->pad_tune; |
| 1264 | int cmd_err; |
| 1265 | int i, ret; |
| 1266 | |
| 1267 | if (host->dev_comp->pad_tune0) |
| 1268 | tune_reg = &host->base->pad_tune0; |
| 1269 | |
| 1270 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1271 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
| 1272 | |
| 1273 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1274 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M, |
| 1275 | i << MSDC_PAD_TUNE_DATRRDLY_S); |
| 1276 | |
| 1277 | ret = mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1278 | if (!ret) { |
| 1279 | rise_delay |= (1 << i); |
| 1280 | } else if (cmd_err) { |
| 1281 | /* in this case, retune response is needed */ |
| 1282 | ret = msdc_tune_response(dev, opcode); |
| 1283 | if (ret) |
| 1284 | break; |
| 1285 | } |
| 1286 | } |
| 1287 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1288 | final_rise_delay = get_best_delay(dev, host, rise_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1289 | if (final_rise_delay.maxlen >= 12 || |
| 1290 | (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) |
| 1291 | goto skip_fall; |
| 1292 | |
| 1293 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1294 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
| 1295 | |
| 1296 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
| 1297 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M, |
| 1298 | i << MSDC_PAD_TUNE_DATRRDLY_S); |
| 1299 | |
| 1300 | ret = mmc_send_tuning(mmc, opcode, &cmd_err); |
| 1301 | if (!ret) { |
| 1302 | fall_delay |= (1 << i); |
| 1303 | } else if (cmd_err) { |
| 1304 | /* in this case, retune response is needed */ |
| 1305 | ret = msdc_tune_response(dev, opcode); |
| 1306 | if (ret) |
| 1307 | break; |
| 1308 | } |
| 1309 | } |
| 1310 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1311 | final_fall_delay = get_best_delay(dev, host, fall_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1312 | |
| 1313 | skip_fall: |
| 1314 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
| 1315 | if (final_maxlen == final_rise_delay.maxlen) { |
| 1316 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1317 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
| 1318 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M, |
| 1319 | final_rise_delay.final_phase << |
| 1320 | MSDC_PAD_TUNE_DATRRDLY_S); |
| 1321 | final_delay = final_rise_delay.final_phase; |
| 1322 | } else { |
| 1323 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1324 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
| 1325 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATRRDLY_M, |
| 1326 | final_fall_delay.final_phase << |
| 1327 | MSDC_PAD_TUNE_DATRRDLY_S); |
| 1328 | final_delay = final_fall_delay.final_phase; |
| 1329 | } |
| 1330 | |
| 1331 | if (mmc->selected_mode == MMC_HS_200 || |
| 1332 | mmc->selected_mode == UHS_SDR104) |
| 1333 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_DATWRDLY_M, |
| 1334 | host->hs200_write_int_delay << |
| 1335 | MSDC_PAD_TUNE_DATWRDLY_S); |
| 1336 | |
Fabien Parent | f9ca467 | 2020-10-15 18:38:18 +0200 | [diff] [blame] | 1337 | dev_dbg(dev, "Final data pad delay: %x\n", final_delay); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1338 | |
| 1339 | return final_delay == 0xff ? -EIO : 0; |
| 1340 | } |
| 1341 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1342 | /* |
| 1343 | * MSDC IP which supports data tune + async fifo can do CMD/DAT tune |
| 1344 | * together, which can save the tuning time. |
| 1345 | */ |
| 1346 | static int msdc_tune_together(struct udevice *dev, u32 opcode) |
| 1347 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1348 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1349 | struct msdc_host *host = dev_get_priv(dev); |
| 1350 | struct mmc *mmc = &plat->mmc; |
| 1351 | u32 rise_delay = 0, fall_delay = 0; |
| 1352 | struct msdc_delay_phase final_rise_delay, final_fall_delay = { 0, }; |
| 1353 | u8 final_delay, final_maxlen; |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1354 | int i, ret; |
| 1355 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1356 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1357 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
| 1358 | |
| 1359 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1360 | msdc_set_cmd_delay(host, i); |
| 1361 | msdc_set_data_delay(host, i); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1362 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 1363 | if (!ret) |
| 1364 | rise_delay |= (1 << i); |
| 1365 | } |
| 1366 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1367 | final_rise_delay = get_best_delay(dev, host, rise_delay); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1368 | if (final_rise_delay.maxlen >= 12 || |
| 1369 | (final_rise_delay.start == 0 && final_rise_delay.maxlen >= 4)) |
| 1370 | goto skip_fall; |
| 1371 | |
| 1372 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1373 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
| 1374 | |
| 1375 | for (i = 0; i < PAD_DELAY_MAX; i++) { |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1376 | msdc_set_cmd_delay(host, i); |
| 1377 | msdc_set_data_delay(host, i); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1378 | ret = mmc_send_tuning(mmc, opcode, NULL); |
| 1379 | if (!ret) |
| 1380 | fall_delay |= (1 << i); |
| 1381 | } |
| 1382 | |
Sean Anderson | 09aa57a | 2020-09-15 10:44:47 -0400 | [diff] [blame] | 1383 | final_fall_delay = get_best_delay(dev, host, fall_delay); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1384 | |
| 1385 | skip_fall: |
| 1386 | final_maxlen = max(final_rise_delay.maxlen, final_fall_delay.maxlen); |
| 1387 | if (final_maxlen == final_rise_delay.maxlen) { |
| 1388 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1389 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1390 | final_delay = final_rise_delay.final_phase; |
| 1391 | } else { |
| 1392 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_DSPL); |
| 1393 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_W_DSPL); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1394 | final_delay = final_fall_delay.final_phase; |
| 1395 | } |
| 1396 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1397 | msdc_set_cmd_delay(host, final_delay); |
| 1398 | msdc_set_data_delay(host, final_delay); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1399 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1400 | dev_info(dev, "Final pad delay: %x\n", final_delay); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1401 | return final_delay == 0xff ? -EIO : 0; |
| 1402 | } |
| 1403 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1404 | static int msdc_execute_tuning(struct udevice *dev, uint opcode) |
| 1405 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1406 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1407 | struct msdc_host *host = dev_get_priv(dev); |
| 1408 | struct mmc *mmc = &plat->mmc; |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1409 | int ret = 0; |
| 1410 | |
| 1411 | if (host->dev_comp->data_tune && host->dev_comp->async_fifo) { |
| 1412 | ret = msdc_tune_together(dev, opcode); |
| 1413 | if (ret == -EIO) { |
| 1414 | dev_err(dev, "Tune fail!\n"); |
| 1415 | return ret; |
| 1416 | } |
| 1417 | |
| 1418 | if (mmc->selected_mode == MMC_HS_400) { |
| 1419 | clrbits_le32(&host->base->msdc_iocon, |
| 1420 | MSDC_IOCON_DSPL | MSDC_IOCON_W_DSPL); |
| 1421 | clrsetbits_le32(&host->base->pad_tune, |
| 1422 | MSDC_PAD_TUNE_DATRRDLY_M, 0); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1423 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1424 | writel(host->hs400_ds_delay, &host->base->pad_ds_tune); |
| 1425 | /* for hs400 mode it must be set to 0 */ |
| 1426 | clrbits_le32(&host->base->patch_bit2, |
| 1427 | MSDC_PB2_CFGCRCSTS); |
| 1428 | host->hs400_mode = true; |
| 1429 | } |
| 1430 | goto tune_done; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1431 | } |
| 1432 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1433 | if (mmc->selected_mode == MMC_HS_400) |
| 1434 | ret = hs400_tune_response(dev, opcode); |
| 1435 | else |
| 1436 | ret = msdc_tune_response(dev, opcode); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1437 | if (ret == -EIO) { |
| 1438 | dev_err(dev, "Tune response fail!\n"); |
| 1439 | return ret; |
| 1440 | } |
| 1441 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1442 | if (mmc->selected_mode != MMC_HS_400) { |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1443 | ret = msdc_tune_data(dev, opcode); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1444 | if (ret == -EIO) { |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1445 | dev_err(dev, "Tune data fail!\n"); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1446 | return ret; |
| 1447 | } |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1448 | } |
| 1449 | |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1450 | tune_done: |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1451 | host->saved_tune_para.iocon = readl(&host->base->msdc_iocon); |
| 1452 | host->saved_tune_para.pad_tune = readl(&host->base->pad_tune); |
developer | 18f9fc7 | 2019-11-07 19:28:42 +0800 | [diff] [blame] | 1453 | host->saved_tune_para.pad_cmd_tune = readl(&host->base->pad_cmd_tune); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1454 | |
| 1455 | return ret; |
| 1456 | } |
| 1457 | #endif |
| 1458 | |
| 1459 | static void msdc_init_hw(struct msdc_host *host) |
| 1460 | { |
| 1461 | u32 val; |
| 1462 | void __iomem *tune_reg = &host->base->pad_tune; |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 1463 | void __iomem *rd_dly0_reg = &host->base->pad_tune0; |
| 1464 | void __iomem *rd_dly1_reg = &host->base->pad_tune1; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1465 | |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 1466 | if (host->dev_comp->pad_tune0) { |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1467 | tune_reg = &host->base->pad_tune0; |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 1468 | rd_dly0_reg = &host->base->dat_rd_dly[0]; |
| 1469 | rd_dly1_reg = &host->base->dat_rd_dly[1]; |
| 1470 | } |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1471 | |
| 1472 | /* Configure to MMC/SD mode, clock free running */ |
| 1473 | setbits_le32(&host->base->msdc_cfg, MSDC_CFG_MODE); |
| 1474 | |
| 1475 | /* Use PIO mode */ |
| 1476 | setbits_le32(&host->base->msdc_cfg, MSDC_CFG_PIO); |
| 1477 | |
| 1478 | /* Reset */ |
| 1479 | msdc_reset_hw(host); |
| 1480 | |
| 1481 | /* Enable/disable hw card detection according to fdt option */ |
| 1482 | if (host->builtin_cd) |
| 1483 | clrsetbits_le32(&host->base->msdc_ps, |
| 1484 | MSDC_PS_CDDBCE_M, |
| 1485 | (DEFAULT_CD_DEBOUNCE << MSDC_PS_CDDBCE_S) | |
| 1486 | MSDC_PS_CDEN); |
| 1487 | else |
| 1488 | clrbits_le32(&host->base->msdc_ps, MSDC_PS_CDEN); |
| 1489 | |
| 1490 | /* Clear all interrupts */ |
| 1491 | val = readl(&host->base->msdc_int); |
| 1492 | writel(val, &host->base->msdc_int); |
| 1493 | |
| 1494 | /* Enable data & cmd interrupts */ |
| 1495 | writel(DATA_INTS_MASK | CMD_INTS_MASK, &host->base->msdc_inten); |
| 1496 | |
| 1497 | writel(0, tune_reg); |
| 1498 | writel(0, &host->base->msdc_iocon); |
| 1499 | |
| 1500 | if (host->r_smpl) |
| 1501 | setbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1502 | else |
| 1503 | clrbits_le32(&host->base->msdc_iocon, MSDC_IOCON_RSPL); |
| 1504 | |
| 1505 | writel(0x403c0046, &host->base->patch_bit0); |
| 1506 | writel(0xffff4089, &host->base->patch_bit1); |
| 1507 | |
| 1508 | if (host->dev_comp->stop_clk_fix) |
| 1509 | clrsetbits_le32(&host->base->patch_bit1, MSDC_PB1_STOP_DLY_M, |
| 1510 | 3 << MSDC_PB1_STOP_DLY_S); |
| 1511 | |
| 1512 | if (host->dev_comp->busy_check) |
| 1513 | clrbits_le32(&host->base->patch_bit1, (1 << 7)); |
| 1514 | |
| 1515 | setbits_le32(&host->base->emmc50_cfg0, EMMC50_CFG_CFCSTS_SEL); |
| 1516 | |
| 1517 | if (host->dev_comp->async_fifo) { |
| 1518 | clrsetbits_le32(&host->base->patch_bit2, MSDC_PB2_RESPWAIT_M, |
| 1519 | 3 << MSDC_PB2_RESPWAIT_S); |
| 1520 | |
| 1521 | if (host->dev_comp->enhance_rx) { |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1522 | if (host->top_base) |
| 1523 | setbits_le32(&host->top_base->emmc_top_control, |
| 1524 | SDC_RX_ENH_EN); |
| 1525 | else |
| 1526 | setbits_le32(&host->base->sdc_adv_cfg0, |
| 1527 | SDC_RX_ENHANCE_EN); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1528 | } else { |
| 1529 | clrsetbits_le32(&host->base->patch_bit2, |
| 1530 | MSDC_PB2_RESPSTSENSEL_M, |
| 1531 | 2 << MSDC_PB2_RESPSTSENSEL_S); |
| 1532 | clrsetbits_le32(&host->base->patch_bit2, |
| 1533 | MSDC_PB2_CRCSTSENSEL_M, |
| 1534 | 2 << MSDC_PB2_CRCSTSENSEL_S); |
| 1535 | } |
| 1536 | |
| 1537 | /* use async fifo to avoid tune internal delay */ |
| 1538 | clrbits_le32(&host->base->patch_bit2, |
| 1539 | MSDC_PB2_CFGRESP); |
| 1540 | clrbits_le32(&host->base->patch_bit2, |
| 1541 | MSDC_PB2_CFGCRCSTS); |
| 1542 | } |
| 1543 | |
| 1544 | if (host->dev_comp->data_tune) { |
| 1545 | setbits_le32(tune_reg, |
| 1546 | MSDC_PAD_TUNE_RD_SEL | MSDC_PAD_TUNE_CMD_SEL); |
| 1547 | clrsetbits_le32(&host->base->patch_bit0, |
| 1548 | MSDC_INT_DAT_LATCH_CK_SEL_M, |
| 1549 | host->latch_ck << |
| 1550 | MSDC_INT_DAT_LATCH_CK_SEL_S); |
| 1551 | } else { |
| 1552 | /* choose clock tune */ |
| 1553 | setbits_le32(tune_reg, MSDC_PAD_TUNE_RXDLYSEL); |
| 1554 | } |
| 1555 | |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 1556 | if (host->dev_comp->builtin_pad_ctrl) { |
| 1557 | /* Set pins driving strength */ |
| 1558 | writel(MSDC_PAD_CTRL0_CLKPD | MSDC_PAD_CTRL0_CLKSMT | |
| 1559 | MSDC_PAD_CTRL0_CLKIES | (4 << MSDC_PAD_CTRL0_CLKDRVN_S) | |
| 1560 | (4 << MSDC_PAD_CTRL0_CLKDRVP_S), &host->base->pad_ctrl0); |
| 1561 | writel(MSDC_PAD_CTRL1_CMDPU | MSDC_PAD_CTRL1_CMDSMT | |
| 1562 | MSDC_PAD_CTRL1_CMDIES | (4 << MSDC_PAD_CTRL1_CMDDRVN_S) | |
| 1563 | (4 << MSDC_PAD_CTRL1_CMDDRVP_S), &host->base->pad_ctrl1); |
| 1564 | writel(MSDC_PAD_CTRL2_DATPU | MSDC_PAD_CTRL2_DATSMT | |
| 1565 | MSDC_PAD_CTRL2_DATIES | (4 << MSDC_PAD_CTRL2_DATDRVN_S) | |
| 1566 | (4 << MSDC_PAD_CTRL2_DATDRVP_S), &host->base->pad_ctrl2); |
| 1567 | } |
| 1568 | |
| 1569 | if (host->dev_comp->default_pad_dly) { |
| 1570 | /* Default pad delay may be needed if tuning not enabled */ |
| 1571 | clrsetbits_le32(tune_reg, MSDC_PAD_TUNE_CLKTDLY_M | |
| 1572 | MSDC_PAD_TUNE_CMDRRDLY_M | |
| 1573 | MSDC_PAD_TUNE_CMDRDLY_M | |
| 1574 | MSDC_PAD_TUNE_DATRRDLY_M | |
| 1575 | MSDC_PAD_TUNE_DATWRDLY_M, |
| 1576 | (0x10 << MSDC_PAD_TUNE_CLKTDLY_S) | |
| 1577 | (0x10 << MSDC_PAD_TUNE_CMDRRDLY_S) | |
| 1578 | (0x10 << MSDC_PAD_TUNE_CMDRDLY_S) | |
| 1579 | (0x10 << MSDC_PAD_TUNE_DATRRDLY_S) | |
| 1580 | (0x10 << MSDC_PAD_TUNE_DATWRDLY_S)); |
| 1581 | |
| 1582 | writel((0x10 << MSDC_PAD_TUNE0_DAT0RDDLY_S) | |
| 1583 | (0x10 << MSDC_PAD_TUNE0_DAT1RDDLY_S) | |
| 1584 | (0x10 << MSDC_PAD_TUNE0_DAT2RDDLY_S) | |
| 1585 | (0x10 << MSDC_PAD_TUNE0_DAT3RDDLY_S), |
| 1586 | rd_dly0_reg); |
| 1587 | |
| 1588 | writel((0x10 << MSDC_PAD_TUNE1_DAT4RDDLY_S) | |
| 1589 | (0x10 << MSDC_PAD_TUNE1_DAT5RDDLY_S) | |
| 1590 | (0x10 << MSDC_PAD_TUNE1_DAT6RDDLY_S) | |
| 1591 | (0x10 << MSDC_PAD_TUNE1_DAT7RDDLY_S), |
| 1592 | rd_dly1_reg); |
| 1593 | } |
| 1594 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1595 | /* Configure to enable SDIO mode otherwise sdio cmd5 won't work */ |
| 1596 | setbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIO); |
| 1597 | |
| 1598 | /* disable detecting SDIO device interrupt function */ |
| 1599 | clrbits_le32(&host->base->sdc_cfg, SDC_CFG_SDIOIDE); |
| 1600 | |
| 1601 | /* Configure to default data timeout */ |
| 1602 | clrsetbits_le32(&host->base->sdc_cfg, SDC_CFG_DTOC_M, |
| 1603 | 3 << SDC_CFG_DTOC_S); |
| 1604 | |
| 1605 | if (host->dev_comp->stop_clk_fix) { |
| 1606 | clrbits_le32(&host->base->sdc_fifo_cfg, |
| 1607 | SDC_FIFO_CFG_WRVALIDSEL); |
| 1608 | clrbits_le32(&host->base->sdc_fifo_cfg, |
| 1609 | SDC_FIFO_CFG_RDVALIDSEL); |
| 1610 | } |
| 1611 | |
| 1612 | host->def_tune_para.iocon = readl(&host->base->msdc_iocon); |
| 1613 | host->def_tune_para.pad_tune = readl(&host->base->pad_tune); |
| 1614 | } |
| 1615 | |
| 1616 | static void msdc_ungate_clock(struct msdc_host *host) |
| 1617 | { |
| 1618 | clk_enable(&host->src_clk); |
| 1619 | clk_enable(&host->h_clk); |
Fabien Parent | 297fa1a | 2019-03-24 16:46:32 +0100 | [diff] [blame] | 1620 | if (host->src_clk_cg.dev) |
| 1621 | clk_enable(&host->src_clk_cg); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1622 | } |
| 1623 | |
| 1624 | static int msdc_drv_probe(struct udevice *dev) |
| 1625 | { |
| 1626 | struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1627 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1628 | struct msdc_host *host = dev_get_priv(dev); |
| 1629 | struct mmc_config *cfg = &plat->cfg; |
| 1630 | |
| 1631 | cfg->name = dev->name; |
| 1632 | |
| 1633 | host->dev_comp = (struct msdc_compatible *)dev_get_driver_data(dev); |
| 1634 | |
| 1635 | host->src_clk_freq = clk_get_rate(&host->src_clk); |
| 1636 | |
| 1637 | if (host->dev_comp->clk_div_bits == 8) |
| 1638 | cfg->f_min = host->src_clk_freq / (4 * 255); |
| 1639 | else |
| 1640 | cfg->f_min = host->src_clk_freq / (4 * 4095); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1641 | |
Daniel Golle | 1bbd66a | 2021-03-15 15:31:11 +0000 | [diff] [blame] | 1642 | if (cfg->f_max < cfg->f_min || cfg->f_max > host->src_clk_freq) |
| 1643 | cfg->f_max = host->src_clk_freq; |
developer | dc462a8 | 2020-11-12 16:37:07 +0800 | [diff] [blame] | 1644 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1645 | cfg->b_max = 1024; |
| 1646 | cfg->voltages = MMC_VDD_32_33 | MMC_VDD_33_34; |
| 1647 | |
| 1648 | host->mmc = &plat->mmc; |
| 1649 | host->timeout_ns = 100000000; |
developer | c731074 | 2020-11-12 16:36:57 +0800 | [diff] [blame] | 1650 | host->timeout_clks = 3 * (1 << SCLK_CYCLES_SHIFT); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1651 | |
| 1652 | #ifdef CONFIG_PINCTRL |
| 1653 | pinctrl_select_state(dev, "default"); |
| 1654 | #endif |
| 1655 | |
| 1656 | msdc_ungate_clock(host); |
| 1657 | msdc_init_hw(host); |
| 1658 | |
| 1659 | upriv->mmc = &plat->mmc; |
| 1660 | |
| 1661 | return 0; |
| 1662 | } |
| 1663 | |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1664 | static int msdc_of_to_plat(struct udevice *dev) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1665 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1666 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1667 | struct msdc_host *host = dev_get_priv(dev); |
| 1668 | struct mmc_config *cfg = &plat->cfg; |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1669 | fdt_addr_t base, top_base; |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1670 | int ret; |
| 1671 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1672 | base = dev_read_addr(dev); |
| 1673 | if (base == FDT_ADDR_T_NONE) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1674 | return -EINVAL; |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1675 | host->base = map_sysmem(base, 0); |
| 1676 | |
| 1677 | top_base = dev_read_addr_index(dev, 1); |
| 1678 | if (top_base == FDT_ADDR_T_NONE) |
| 1679 | host->top_base = NULL; |
| 1680 | else |
| 1681 | host->top_base = map_sysmem(top_base, 0); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1682 | |
| 1683 | ret = mmc_of_parse(dev, cfg); |
| 1684 | if (ret) |
| 1685 | return ret; |
| 1686 | |
| 1687 | ret = clk_get_by_name(dev, "source", &host->src_clk); |
| 1688 | if (ret < 0) |
| 1689 | return ret; |
| 1690 | |
| 1691 | ret = clk_get_by_name(dev, "hclk", &host->h_clk); |
| 1692 | if (ret < 0) |
| 1693 | return ret; |
| 1694 | |
Fabien Parent | 297fa1a | 2019-03-24 16:46:32 +0100 | [diff] [blame] | 1695 | clk_get_by_name(dev, "source_cg", &host->src_clk_cg); /* optional */ |
| 1696 | |
Fabien Parent | 8ed608a | 2019-03-24 16:46:34 +0100 | [diff] [blame] | 1697 | #if CONFIG_IS_ENABLED(DM_GPIO) |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1698 | gpio_request_by_name(dev, "wp-gpios", 0, &host->gpio_wp, GPIOD_IS_IN); |
| 1699 | gpio_request_by_name(dev, "cd-gpios", 0, &host->gpio_cd, GPIOD_IS_IN); |
| 1700 | #endif |
| 1701 | |
| 1702 | host->hs400_ds_delay = dev_read_u32_default(dev, "hs400-ds-delay", 0); |
| 1703 | host->hs200_cmd_int_delay = |
| 1704 | dev_read_u32_default(dev, "cmd_int_delay", 0); |
| 1705 | host->hs200_write_int_delay = |
| 1706 | dev_read_u32_default(dev, "write_int_delay", 0); |
| 1707 | host->latch_ck = dev_read_u32_default(dev, "latch-ck", 0); |
| 1708 | host->r_smpl = dev_read_u32_default(dev, "r_smpl", 0); |
| 1709 | host->builtin_cd = dev_read_u32_default(dev, "builtin-cd", 0); |
developer | 399e4af | 2019-09-25 17:45:38 +0800 | [diff] [blame] | 1710 | host->cd_active_high = dev_read_bool(dev, "cd-active-high"); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1711 | |
| 1712 | return 0; |
| 1713 | } |
| 1714 | |
| 1715 | static int msdc_drv_bind(struct udevice *dev) |
| 1716 | { |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 1717 | struct msdc_plat *plat = dev_get_plat(dev); |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1718 | |
| 1719 | return mmc_bind(dev, &plat->mmc, &plat->cfg); |
| 1720 | } |
| 1721 | |
| 1722 | static const struct dm_mmc_ops msdc_ops = { |
| 1723 | .send_cmd = msdc_ops_send_cmd, |
| 1724 | .set_ios = msdc_ops_set_ios, |
| 1725 | .get_cd = msdc_ops_get_cd, |
| 1726 | .get_wp = msdc_ops_get_wp, |
| 1727 | #ifdef MMC_SUPPORTS_TUNING |
| 1728 | .execute_tuning = msdc_execute_tuning, |
| 1729 | #endif |
| 1730 | }; |
| 1731 | |
developer | 607faf7 | 2019-09-25 17:45:37 +0800 | [diff] [blame] | 1732 | static const struct msdc_compatible mt7620_compat = { |
| 1733 | .clk_div_bits = 8, |
developer | 607faf7 | 2019-09-25 17:45:37 +0800 | [diff] [blame] | 1734 | .pad_tune0 = false, |
| 1735 | .async_fifo = false, |
| 1736 | .data_tune = false, |
| 1737 | .busy_check = false, |
| 1738 | .stop_clk_fix = false, |
developer | 7295c89 | 2020-11-12 16:37:02 +0800 | [diff] [blame] | 1739 | .enhance_rx = false, |
| 1740 | .builtin_pad_ctrl = true, |
| 1741 | .default_pad_dly = true, |
developer | 607faf7 | 2019-09-25 17:45:37 +0800 | [diff] [blame] | 1742 | }; |
| 1743 | |
developer | 837d334 | 2020-01-10 16:30:32 +0800 | [diff] [blame] | 1744 | static const struct msdc_compatible mt7622_compat = { |
| 1745 | .clk_div_bits = 12, |
| 1746 | .pad_tune0 = true, |
| 1747 | .async_fifo = true, |
| 1748 | .data_tune = true, |
| 1749 | .busy_check = true, |
| 1750 | .stop_clk_fix = true, |
| 1751 | }; |
| 1752 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1753 | static const struct msdc_compatible mt7623_compat = { |
| 1754 | .clk_div_bits = 12, |
| 1755 | .pad_tune0 = true, |
| 1756 | .async_fifo = true, |
| 1757 | .data_tune = true, |
| 1758 | .busy_check = false, |
| 1759 | .stop_clk_fix = false, |
| 1760 | .enhance_rx = false |
| 1761 | }; |
| 1762 | |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1763 | static const struct msdc_compatible mt8512_compat = { |
| 1764 | .clk_div_bits = 12, |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1765 | .pad_tune0 = true, |
| 1766 | .async_fifo = true, |
| 1767 | .data_tune = true, |
| 1768 | .busy_check = true, |
| 1769 | .stop_clk_fix = true, |
| 1770 | }; |
| 1771 | |
Fabien Parent | 1d520a4 | 2019-03-24 16:46:33 +0100 | [diff] [blame] | 1772 | static const struct msdc_compatible mt8516_compat = { |
| 1773 | .clk_div_bits = 12, |
| 1774 | .pad_tune0 = true, |
| 1775 | .async_fifo = true, |
| 1776 | .data_tune = true, |
| 1777 | .busy_check = true, |
| 1778 | .stop_clk_fix = true, |
| 1779 | }; |
| 1780 | |
Fabien Parent | c7da698 | 2019-08-12 20:26:58 +0200 | [diff] [blame] | 1781 | static const struct msdc_compatible mt8183_compat = { |
| 1782 | .clk_div_bits = 12, |
| 1783 | .pad_tune0 = true, |
| 1784 | .async_fifo = true, |
| 1785 | .data_tune = true, |
| 1786 | .busy_check = true, |
| 1787 | .stop_clk_fix = true, |
| 1788 | }; |
| 1789 | |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1790 | static const struct udevice_id msdc_ids[] = { |
developer | 607faf7 | 2019-09-25 17:45:37 +0800 | [diff] [blame] | 1791 | { .compatible = "mediatek,mt7620-mmc", .data = (ulong)&mt7620_compat }, |
developer | 837d334 | 2020-01-10 16:30:32 +0800 | [diff] [blame] | 1792 | { .compatible = "mediatek,mt7622-mmc", .data = (ulong)&mt7622_compat }, |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1793 | { .compatible = "mediatek,mt7623-mmc", .data = (ulong)&mt7623_compat }, |
developer | a2d3a6c | 2019-12-31 11:29:24 +0800 | [diff] [blame] | 1794 | { .compatible = "mediatek,mt8512-mmc", .data = (ulong)&mt8512_compat }, |
Fabien Parent | 1d520a4 | 2019-03-24 16:46:33 +0100 | [diff] [blame] | 1795 | { .compatible = "mediatek,mt8516-mmc", .data = (ulong)&mt8516_compat }, |
Fabien Parent | c7da698 | 2019-08-12 20:26:58 +0200 | [diff] [blame] | 1796 | { .compatible = "mediatek,mt8183-mmc", .data = (ulong)&mt8183_compat }, |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1797 | {} |
| 1798 | }; |
| 1799 | |
| 1800 | U_BOOT_DRIVER(mtk_sd_drv) = { |
| 1801 | .name = "mtk_sd", |
| 1802 | .id = UCLASS_MMC, |
| 1803 | .of_match = msdc_ids, |
Simon Glass | aad29ae | 2020-12-03 16:55:21 -0700 | [diff] [blame] | 1804 | .of_to_plat = msdc_of_to_plat, |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1805 | .bind = msdc_drv_bind, |
| 1806 | .probe = msdc_drv_probe, |
| 1807 | .ops = &msdc_ops, |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 1808 | .plat_auto = sizeof(struct msdc_plat), |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 1809 | .priv_auto = sizeof(struct msdc_host), |
developer | dc5a9aa | 2018-11-15 10:08:04 +0800 | [diff] [blame] | 1810 | }; |