rockchip_sdhci.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * (C) Copyright 2016 Fuzhou Rockchip Electronics Co., Ltd
  4. *
  5. * Rockchip SD Host Controller Interface
  6. */
  7. #include <common.h>
  8. #include <clk.h>
  9. #include <dm.h>
  10. #include <dm/ofnode.h>
  11. #include <dt-structs.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/libfdt.h>
  15. #include <linux/iopoll.h>
  16. #include <malloc.h>
  17. #include <mapmem.h>
  18. #include "mmc_private.h"
  19. #include <sdhci.h>
  20. #include <syscon.h>
  21. #include <asm/arch-rockchip/clock.h>
  22. #include <asm/arch-rockchip/hardware.h>
  23. /* DWCMSHC specific Mode Select value */
  24. #define DWCMSHC_CTRL_HS400 0x7
  25. /* 400KHz is max freq for card ID etc. Use that as min */
  26. #define EMMC_MIN_FREQ 400000
  27. #define KHz (1000)
  28. #define MHz (1000 * KHz)
  29. #define SDHCI_TUNING_LOOP_COUNT 40
  30. #define PHYCTRL_CALDONE_MASK 0x1
  31. #define PHYCTRL_CALDONE_SHIFT 0x6
  32. #define PHYCTRL_CALDONE_DONE 0x1
  33. #define PHYCTRL_DLLRDY_MASK 0x1
  34. #define PHYCTRL_DLLRDY_SHIFT 0x5
  35. #define PHYCTRL_DLLRDY_DONE 0x1
  36. #define PHYCTRL_FREQSEL_200M 0x0
  37. #define PHYCTRL_FREQSEL_50M 0x1
  38. #define PHYCTRL_FREQSEL_100M 0x2
  39. #define PHYCTRL_FREQSEL_150M 0x3
  40. #define PHYCTRL_DLL_LOCK_WO_TMOUT(x) \
  41. ((((x) >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK) ==\
  42. PHYCTRL_DLLRDY_DONE)
  43. #define ARASAN_VENDOR_REGISTER 0x78
  44. #define ARASAN_VENDOR_ENHANCED_STROBE BIT(0)
  45. /* Rockchip specific Registers */
  46. #define DWCMSHC_EMMC_EMMC_CTRL 0x52c
  47. #define DWCMSHC_CARD_IS_EMMC BIT(0)
  48. #define DWCMSHC_ENHANCED_STROBE BIT(8)
  49. #define DWCMSHC_EMMC_DLL_CTRL 0x800
  50. #define DWCMSHC_EMMC_DLL_CTRL_RESET BIT(1)
  51. #define DWCMSHC_EMMC_DLL_RXCLK 0x804
  52. #define DWCMSHC_EMMC_DLL_TXCLK 0x808
  53. #define DWCMSHC_EMMC_DLL_STRBIN 0x80c
  54. #define DWCMSHC_EMMC_DLL_CMDOUT 0x810
  55. #define DWCMSHC_EMMC_DLL_STATUS0 0x840
  56. #define DWCMSHC_EMMC_DLL_STATUS1 0x844
  57. #define DWCMSHC_EMMC_DLL_START BIT(0)
  58. #define DWCMSHC_EMMC_DLL_LOCKED BIT(8)
  59. #define DWCMSHC_EMMC_DLL_TIMEOUT BIT(9)
  60. #define DWCMSHC_EMMC_DLL_START_POINT 16
  61. #define DWCMSHC_EMMC_DLL_START_DEFAULT 5
  62. #define DWCMSHC_EMMC_DLL_INC_VALUE 2
  63. #define DWCMSHC_EMMC_DLL_INC 8
  64. #define DWCMSHC_EMMC_DLL_BYPASS BIT(24)
  65. #define DWCMSHC_EMMC_DLL_DLYENA BIT(27)
  66. #define DLL_RXCLK_NO_INVERTER BIT(29)
  67. #define DLL_RXCLK_ORI_GATE BIT(31)
  68. #define DLL_TXCLK_TAPNUM_DEFAULT 0x10
  69. #define DLL_TXCLK_TAPNUM_90_DEGREES 0x9
  70. #define DLL_TXCLK_TAPNUM_FROM_SW BIT(24)
  71. #define DLL_TXCLK_NO_INVERTER BIT(29)
  72. #define DLL_STRBIN_TAPNUM_DEFAULT 0x4
  73. #define DLL_STRBIN_TAPNUM_FROM_SW BIT(24)
  74. #define DLL_STRBIN_DELAY_NUM_SEL BIT(26)
  75. #define DLL_STRBIN_DELAY_NUM_OFFSET 16
  76. #define DLL_STRBIN_DELAY_NUM_DEFAULT 0x10
  77. #define DLL_CMDOUT_TAPNUM_90_DEGREES 0x8
  78. #define DLL_CMDOUT_TAPNUM_FROM_SW BIT(24)
  79. #define DLL_CMDOUT_SRC_CLK_NEG BIT(28)
  80. #define DLL_CMDOUT_EN_SRC_CLK_NEG BIT(29)
  81. #define DLL_CMDOUT_BOTH_CLK_EDGE BIT(30)
  82. #define DLL_LOCK_WO_TMOUT(x) \
  83. ((((x) & DWCMSHC_EMMC_DLL_LOCKED) == DWCMSHC_EMMC_DLL_LOCKED) && \
  84. (((x) & DWCMSHC_EMMC_DLL_TIMEOUT) == 0))
  85. #define ROCKCHIP_MAX_CLKS 3
  86. #define FLAG_INVERTER_FLAG_IN_RXCLK BIT(0)
  87. struct rockchip_sdhc_plat {
  88. struct mmc_config cfg;
  89. struct mmc mmc;
  90. };
  91. struct rockchip_emmc_phy {
  92. u32 emmcphy_con[7];
  93. u32 reserved;
  94. u32 emmcphy_status;
  95. };
  96. struct rockchip_sdhc {
  97. struct sdhci_host host;
  98. struct udevice *dev;
  99. void *base;
  100. struct rockchip_emmc_phy *phy;
  101. struct clk emmc_clk;
  102. };
  103. struct sdhci_data {
  104. int (*get_phy)(struct udevice *dev);
  105. /**
  106. * set_control_reg() - Set SDHCI control registers
  107. *
  108. * This is the set_control_reg() SDHCI operation that should be
  109. * used for the hardware this driver data is associated with.
  110. * Normally, this is used to set up control registers for
  111. * voltage level and UHS speed mode.
  112. *
  113. * @host: SDHCI host structure
  114. */
  115. void (*set_control_reg)(struct sdhci_host *host);
  116. /**
  117. * set_ios_post() - Host specific hook after set_ios() calls
  118. *
  119. * This is the set_ios_post() SDHCI operation that should be
  120. * used for the hardware this driver data is associated with.
  121. * Normally, this is a hook that is called after sdhci_set_ios()
  122. * that does any necessary host-specific configuration.
  123. *
  124. * @host: SDHCI host structure
  125. * Return: 0 if successful, -ve on error
  126. */
  127. int (*set_ios_post)(struct sdhci_host *host);
  128. void (*set_clock)(struct sdhci_host *host, u32 div);
  129. int (*config_dll)(struct sdhci_host *host, u32 clock, bool enable);
  130. /**
  131. * set_enhanced_strobe() - Set HS400 Enhanced Strobe config
  132. *
  133. * This is the set_enhanced_strobe() SDHCI operation that should
  134. * be used for the hardware this driver data is associated with.
  135. * Normally, this is used to set any host-specific configuration
  136. * necessary for HS400 ES.
  137. *
  138. * @host: SDHCI host structure
  139. * Return: 0 if successful, -ve on error
  140. */
  141. int (*set_enhanced_strobe)(struct sdhci_host *host);
  142. u32 flags;
  143. u8 hs200_txclk_tapnum;
  144. u8 hs400_txclk_tapnum;
  145. };
  146. static void rk3399_emmc_phy_power_on(struct rockchip_emmc_phy *phy, u32 clock)
  147. {
  148. u32 caldone, dllrdy, freqsel;
  149. writel(RK_CLRSETBITS(7 << 4, 0), &phy->emmcphy_con[6]);
  150. writel(RK_CLRSETBITS(1 << 11, 1 << 11), &phy->emmcphy_con[0]);
  151. writel(RK_CLRSETBITS(0xf << 7, 6 << 7), &phy->emmcphy_con[0]);
  152. /*
  153. * According to the user manual, calpad calibration
  154. * cycle takes more than 2us without the minimal recommended
  155. * value, so we may need a little margin here
  156. */
  157. udelay(3);
  158. writel(RK_CLRSETBITS(1, 1), &phy->emmcphy_con[6]);
  159. /*
  160. * According to the user manual, it asks driver to
  161. * wait 5us for calpad busy trimming. But it seems that
  162. * 5us of caldone isn't enough for all cases.
  163. */
  164. udelay(500);
  165. caldone = readl(&phy->emmcphy_status);
  166. caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
  167. if (caldone != PHYCTRL_CALDONE_DONE) {
  168. printf("%s: caldone timeout.\n", __func__);
  169. return;
  170. }
  171. /* Set the frequency of the DLL operation */
  172. if (clock < 75 * MHz)
  173. freqsel = PHYCTRL_FREQSEL_50M;
  174. else if (clock < 125 * MHz)
  175. freqsel = PHYCTRL_FREQSEL_100M;
  176. else if (clock < 175 * MHz)
  177. freqsel = PHYCTRL_FREQSEL_150M;
  178. else
  179. freqsel = PHYCTRL_FREQSEL_200M;
  180. /* Set the frequency of the DLL operation */
  181. writel(RK_CLRSETBITS(3 << 12, freqsel << 12), &phy->emmcphy_con[0]);
  182. writel(RK_CLRSETBITS(1 << 1, 1 << 1), &phy->emmcphy_con[6]);
  183. /* REN Enable on STRB Line for HS400 */
  184. writel(RK_CLRSETBITS(0, 1 << 9), &phy->emmcphy_con[2]);
  185. read_poll_timeout(readl, dllrdy, PHYCTRL_DLL_LOCK_WO_TMOUT(dllrdy), 1,
  186. 5000, &phy->emmcphy_status);
  187. }
  188. static void rk3399_emmc_phy_power_off(struct rockchip_emmc_phy *phy)
  189. {
  190. writel(RK_CLRSETBITS(1, 0), &phy->emmcphy_con[6]);
  191. writel(RK_CLRSETBITS(1 << 1, 0), &phy->emmcphy_con[6]);
  192. }
  193. static int rk3399_emmc_get_phy(struct udevice *dev)
  194. {
  195. struct rockchip_sdhc *priv = dev_get_priv(dev);
  196. ofnode phy_node;
  197. void *grf_base;
  198. u32 grf_phy_offset, phandle;
  199. phandle = dev_read_u32_default(dev, "phys", 0);
  200. phy_node = ofnode_get_by_phandle(phandle);
  201. if (!ofnode_valid(phy_node)) {
  202. debug("Not found emmc phy device\n");
  203. return -ENODEV;
  204. }
  205. grf_base = syscon_get_first_range(ROCKCHIP_SYSCON_GRF);
  206. if (IS_ERR_OR_NULL(grf_base)) {
  207. printf("%s Get syscon grf failed", __func__);
  208. return -ENODEV;
  209. }
  210. grf_phy_offset = ofnode_read_u32_default(phy_node, "reg", 0);
  211. priv->phy = (struct rockchip_emmc_phy *)(grf_base + grf_phy_offset);
  212. return 0;
  213. }
  214. static int rk3399_sdhci_set_enhanced_strobe(struct sdhci_host *host)
  215. {
  216. struct mmc *mmc = host->mmc;
  217. u32 vendor;
  218. vendor = sdhci_readl(host, ARASAN_VENDOR_REGISTER);
  219. if (mmc->selected_mode == MMC_HS_400_ES)
  220. vendor |= ARASAN_VENDOR_ENHANCED_STROBE;
  221. else
  222. vendor &= ~ARASAN_VENDOR_ENHANCED_STROBE;
  223. sdhci_writel(host, vendor, ARASAN_VENDOR_REGISTER);
  224. return 0;
  225. }
  226. static void rk3399_sdhci_set_control_reg(struct sdhci_host *host)
  227. {
  228. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  229. struct mmc *mmc = host->mmc;
  230. uint clock = mmc->tran_speed;
  231. int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
  232. if (cycle_phy)
  233. rk3399_emmc_phy_power_off(priv->phy);
  234. sdhci_set_control_reg(host);
  235. /*
  236. * Reinitializing the device tries to set it to lower-speed modes
  237. * first, which fails if the Enhanced Strobe bit is set, making
  238. * the device impossible to use. Set the correct value here to
  239. * let reinitialization attempts succeed.
  240. */
  241. if (CONFIG_IS_ENABLED(MMC_HS400_ES_SUPPORT))
  242. rk3399_sdhci_set_enhanced_strobe(host);
  243. };
  244. static int rk3399_sdhci_set_ios_post(struct sdhci_host *host)
  245. {
  246. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  247. struct mmc *mmc = host->mmc;
  248. uint clock = mmc->tran_speed;
  249. int cycle_phy = host->clock != clock && clock > EMMC_MIN_FREQ;
  250. if (!clock)
  251. clock = mmc->clock;
  252. if (cycle_phy)
  253. rk3399_emmc_phy_power_on(priv->phy, clock);
  254. return 0;
  255. }
  256. static void rk3568_sdhci_set_clock(struct sdhci_host *host, u32 div)
  257. {
  258. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  259. struct mmc *mmc = host->mmc;
  260. ulong rate;
  261. rate = clk_set_rate(&priv->emmc_clk, mmc->clock);
  262. if (IS_ERR_VALUE(rate))
  263. printf("%s: Set clock rate failed: %ld\n", __func__, (long)rate);
  264. }
  265. static int rk3568_sdhci_config_dll(struct sdhci_host *host, u32 clock, bool enable)
  266. {
  267. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  268. struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
  269. struct mmc *mmc = host->mmc;
  270. int val, ret;
  271. u32 extra, txclk_tapnum;
  272. if (!enable)
  273. return 0;
  274. if (clock >= 100 * MHz) {
  275. /* reset DLL */
  276. sdhci_writel(host, DWCMSHC_EMMC_DLL_CTRL_RESET, DWCMSHC_EMMC_DLL_CTRL);
  277. udelay(1);
  278. sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CTRL);
  279. /* Init DLL settings */
  280. extra = DWCMSHC_EMMC_DLL_START_DEFAULT << DWCMSHC_EMMC_DLL_START_POINT |
  281. DWCMSHC_EMMC_DLL_INC_VALUE << DWCMSHC_EMMC_DLL_INC |
  282. DWCMSHC_EMMC_DLL_START;
  283. sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
  284. ret = read_poll_timeout(readl, val, DLL_LOCK_WO_TMOUT(val), 1,
  285. 500,
  286. host->ioaddr + DWCMSHC_EMMC_DLL_STATUS0);
  287. if (ret)
  288. return ret;
  289. extra = DWCMSHC_EMMC_DLL_DLYENA | DLL_RXCLK_ORI_GATE;
  290. if (data->flags & FLAG_INVERTER_FLAG_IN_RXCLK)
  291. extra |= DLL_RXCLK_NO_INVERTER;
  292. sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_RXCLK);
  293. txclk_tapnum = data->hs200_txclk_tapnum;
  294. if (mmc->selected_mode == MMC_HS_400 ||
  295. mmc->selected_mode == MMC_HS_400_ES) {
  296. txclk_tapnum = data->hs400_txclk_tapnum;
  297. extra = DLL_CMDOUT_SRC_CLK_NEG |
  298. DLL_CMDOUT_BOTH_CLK_EDGE |
  299. DWCMSHC_EMMC_DLL_DLYENA |
  300. DLL_CMDOUT_TAPNUM_90_DEGREES |
  301. DLL_CMDOUT_TAPNUM_FROM_SW;
  302. sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CMDOUT);
  303. }
  304. extra = DWCMSHC_EMMC_DLL_DLYENA |
  305. DLL_TXCLK_TAPNUM_FROM_SW |
  306. DLL_TXCLK_NO_INVERTER |
  307. txclk_tapnum;
  308. sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_TXCLK);
  309. extra = DWCMSHC_EMMC_DLL_DLYENA |
  310. DLL_STRBIN_TAPNUM_DEFAULT |
  311. DLL_STRBIN_TAPNUM_FROM_SW;
  312. sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
  313. } else {
  314. /*
  315. * Disable DLL and reset both of sample and drive clock.
  316. * The bypass bit and start bit need to be set if DLL is not locked.
  317. */
  318. extra = DWCMSHC_EMMC_DLL_BYPASS | DWCMSHC_EMMC_DLL_START;
  319. sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_CTRL);
  320. sdhci_writel(host, DLL_RXCLK_ORI_GATE, DWCMSHC_EMMC_DLL_RXCLK);
  321. sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_TXCLK);
  322. sdhci_writel(host, 0, DWCMSHC_EMMC_DLL_CMDOUT);
  323. /*
  324. * Before switching to hs400es mode, the driver will enable
  325. * enhanced strobe first. PHY needs to configure the parameters
  326. * of enhanced strobe first.
  327. */
  328. extra = DWCMSHC_EMMC_DLL_DLYENA |
  329. DLL_STRBIN_DELAY_NUM_SEL |
  330. DLL_STRBIN_DELAY_NUM_DEFAULT << DLL_STRBIN_DELAY_NUM_OFFSET;
  331. sdhci_writel(host, extra, DWCMSHC_EMMC_DLL_STRBIN);
  332. }
  333. return 0;
  334. }
  335. static int rk3568_sdhci_set_ios_post(struct sdhci_host *host)
  336. {
  337. struct mmc *mmc = host->mmc;
  338. u32 reg;
  339. reg = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  340. reg &= ~SDHCI_CTRL_UHS_MASK;
  341. switch (mmc->selected_mode) {
  342. case UHS_SDR25:
  343. case MMC_HS:
  344. case MMC_HS_52:
  345. reg |= SDHCI_CTRL_UHS_SDR25;
  346. break;
  347. case UHS_SDR50:
  348. reg |= SDHCI_CTRL_UHS_SDR50;
  349. break;
  350. case UHS_DDR50:
  351. case MMC_DDR_52:
  352. reg |= SDHCI_CTRL_UHS_DDR50;
  353. break;
  354. case UHS_SDR104:
  355. case MMC_HS_200:
  356. reg |= SDHCI_CTRL_UHS_SDR104;
  357. break;
  358. case MMC_HS_400:
  359. case MMC_HS_400_ES:
  360. reg |= DWCMSHC_CTRL_HS400;
  361. break;
  362. default:
  363. reg |= SDHCI_CTRL_UHS_SDR12;
  364. }
  365. sdhci_writew(host, reg, SDHCI_HOST_CONTROL2);
  366. reg = sdhci_readw(host, DWCMSHC_EMMC_EMMC_CTRL);
  367. if (IS_MMC(mmc))
  368. reg |= DWCMSHC_CARD_IS_EMMC;
  369. else
  370. reg &= ~DWCMSHC_CARD_IS_EMMC;
  371. if (mmc->selected_mode == MMC_HS_400_ES)
  372. reg |= DWCMSHC_ENHANCED_STROBE;
  373. else
  374. reg &= ~DWCMSHC_ENHANCED_STROBE;
  375. sdhci_writew(host, reg, DWCMSHC_EMMC_EMMC_CTRL);
  376. return 0;
  377. }
  378. static void rockchip_sdhci_set_control_reg(struct sdhci_host *host)
  379. {
  380. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  381. struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
  382. if (data->set_control_reg)
  383. data->set_control_reg(host);
  384. }
  385. static int rockchip_sdhci_set_ios_post(struct sdhci_host *host)
  386. {
  387. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  388. struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
  389. if (data->set_ios_post)
  390. return data->set_ios_post(host);
  391. return 0;
  392. }
  393. static void rockchip_sdhci_set_clock(struct sdhci_host *host, u32 div)
  394. {
  395. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  396. struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
  397. if (data->set_clock)
  398. data->set_clock(host, div);
  399. }
  400. static int rockchip_sdhci_execute_tuning(struct mmc *mmc, u8 opcode)
  401. {
  402. struct rockchip_sdhc *priv = dev_get_priv(mmc->dev);
  403. struct sdhci_host *host = &priv->host;
  404. char tuning_loop_counter = SDHCI_TUNING_LOOP_COUNT;
  405. struct mmc_cmd cmd;
  406. u32 ctrl, blk_size;
  407. int ret;
  408. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  409. ctrl |= SDHCI_CTRL_EXEC_TUNING;
  410. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  411. sdhci_writel(host, SDHCI_INT_DATA_AVAIL, SDHCI_INT_ENABLE);
  412. blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 64);
  413. if (opcode == MMC_CMD_SEND_TUNING_BLOCK_HS200 && mmc->bus_width == 8)
  414. blk_size = SDHCI_MAKE_BLKSZ(SDHCI_DEFAULT_BOUNDARY_ARG, 128);
  415. sdhci_writew(host, blk_size, SDHCI_BLOCK_SIZE);
  416. sdhci_writew(host, SDHCI_TRNS_READ, SDHCI_TRANSFER_MODE);
  417. cmd.cmdidx = opcode;
  418. cmd.resp_type = MMC_RSP_R1;
  419. cmd.cmdarg = 0;
  420. do {
  421. ret = mmc_send_cmd(mmc, &cmd, NULL);
  422. ctrl = sdhci_readw(host, SDHCI_HOST_CONTROL2);
  423. if (ret || tuning_loop_counter-- == 0)
  424. break;
  425. } while (ctrl & SDHCI_CTRL_EXEC_TUNING);
  426. if (ret || tuning_loop_counter < 0 || !(ctrl & SDHCI_CTRL_TUNED_CLK)) {
  427. if (!ret)
  428. ret = -EIO;
  429. printf("%s: Tuning failed: %d\n", __func__, ret);
  430. ctrl &= ~SDHCI_CTRL_TUNED_CLK;
  431. ctrl &= ~SDHCI_CTRL_EXEC_TUNING;
  432. sdhci_writew(host, ctrl, SDHCI_HOST_CONTROL2);
  433. }
  434. /* Enable only interrupts served by the SD controller */
  435. sdhci_writel(host, SDHCI_INT_DATA_MASK | SDHCI_INT_CMD_MASK, SDHCI_INT_ENABLE);
  436. return ret;
  437. }
  438. static int rockchip_sdhci_config_dll(struct sdhci_host *host, u32 clock, bool enable)
  439. {
  440. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  441. struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
  442. if (data->config_dll)
  443. return data->config_dll(host, clock, enable);
  444. return 0;
  445. }
  446. static int rockchip_sdhci_set_enhanced_strobe(struct sdhci_host *host)
  447. {
  448. struct rockchip_sdhc *priv = container_of(host, struct rockchip_sdhc, host);
  449. struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(priv->dev);
  450. if (data->set_enhanced_strobe)
  451. return data->set_enhanced_strobe(host);
  452. return 0;
  453. }
  454. static struct sdhci_ops rockchip_sdhci_ops = {
  455. .set_control_reg = rockchip_sdhci_set_control_reg,
  456. .set_ios_post = rockchip_sdhci_set_ios_post,
  457. .set_clock = rockchip_sdhci_set_clock,
  458. .platform_execute_tuning = rockchip_sdhci_execute_tuning,
  459. .config_dll = rockchip_sdhci_config_dll,
  460. .set_enhanced_strobe = rockchip_sdhci_set_enhanced_strobe,
  461. };
  462. static int rockchip_sdhci_probe(struct udevice *dev)
  463. {
  464. struct sdhci_data *data = (struct sdhci_data *)dev_get_driver_data(dev);
  465. struct mmc_uclass_priv *upriv = dev_get_uclass_priv(dev);
  466. struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
  467. struct rockchip_sdhc *priv = dev_get_priv(dev);
  468. struct mmc_config *cfg = &plat->cfg;
  469. struct sdhci_host *host = &priv->host;
  470. struct clk clk;
  471. int ret;
  472. host->max_clk = cfg->f_max;
  473. ret = clk_get_by_index(dev, 0, &clk);
  474. if (!ret) {
  475. ret = clk_set_rate(&clk, host->max_clk);
  476. if (IS_ERR_VALUE(ret))
  477. printf("%s clk set rate fail!\n", __func__);
  478. } else {
  479. printf("%s fail to get clk\n", __func__);
  480. }
  481. priv->emmc_clk = clk;
  482. priv->dev = dev;
  483. if (data->get_phy) {
  484. ret = data->get_phy(dev);
  485. if (ret)
  486. return ret;
  487. }
  488. host->ops = &rockchip_sdhci_ops;
  489. host->quirks = SDHCI_QUIRK_WAIT_SEND_CMD;
  490. host->mmc = &plat->mmc;
  491. host->mmc->priv = &priv->host;
  492. host->mmc->dev = dev;
  493. upriv->mmc = host->mmc;
  494. ret = sdhci_setup_cfg(cfg, host, cfg->f_max, EMMC_MIN_FREQ);
  495. if (ret)
  496. return ret;
  497. /*
  498. * Disable use of DMA and force use of PIO mode in SPL to fix an issue
  499. * where loading part of TF-A into SRAM using DMA silently fails.
  500. */
  501. if (IS_ENABLED(CONFIG_SPL_BUILD) &&
  502. dev_read_bool(dev, "u-boot,spl-fifo-mode"))
  503. host->flags &= ~USE_DMA;
  504. /*
  505. * Reading more than 4 blocks with a single CMD18 command in PIO mode
  506. * triggers Data End Bit Error on RK3568 and RK3588. Limit to reading
  507. * max 4 blocks in one command when using PIO mode.
  508. */
  509. if (!(host->flags & USE_DMA) &&
  510. (device_is_compatible(dev, "rockchip,rk3568-dwcmshc") ||
  511. device_is_compatible(dev, "rockchip,rk3588-dwcmshc")))
  512. cfg->b_max = 4;
  513. return sdhci_probe(dev);
  514. }
  515. static int rockchip_sdhci_of_to_plat(struct udevice *dev)
  516. {
  517. struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
  518. struct rockchip_sdhc *priv = dev_get_priv(dev);
  519. struct mmc_config *cfg = &plat->cfg;
  520. struct sdhci_host *host = &priv->host;
  521. int ret;
  522. host->name = dev->name;
  523. host->ioaddr = dev_read_addr_ptr(dev);
  524. ret = mmc_of_parse(dev, cfg);
  525. if (ret)
  526. return ret;
  527. return 0;
  528. }
  529. static int rockchip_sdhci_bind(struct udevice *dev)
  530. {
  531. struct rockchip_sdhc_plat *plat = dev_get_plat(dev);
  532. return sdhci_bind(dev, &plat->mmc, &plat->cfg);
  533. }
  534. static const struct sdhci_data rk3399_data = {
  535. .get_phy = rk3399_emmc_get_phy,
  536. .set_control_reg = rk3399_sdhci_set_control_reg,
  537. .set_ios_post = rk3399_sdhci_set_ios_post,
  538. .set_enhanced_strobe = rk3399_sdhci_set_enhanced_strobe,
  539. };
  540. static const struct sdhci_data rk3568_data = {
  541. .set_ios_post = rk3568_sdhci_set_ios_post,
  542. .set_clock = rk3568_sdhci_set_clock,
  543. .config_dll = rk3568_sdhci_config_dll,
  544. .flags = FLAG_INVERTER_FLAG_IN_RXCLK,
  545. .hs200_txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT,
  546. .hs400_txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT,
  547. };
  548. static const struct sdhci_data rk3588_data = {
  549. .set_ios_post = rk3568_sdhci_set_ios_post,
  550. .set_clock = rk3568_sdhci_set_clock,
  551. .config_dll = rk3568_sdhci_config_dll,
  552. .hs200_txclk_tapnum = DLL_TXCLK_TAPNUM_DEFAULT,
  553. .hs400_txclk_tapnum = DLL_TXCLK_TAPNUM_90_DEGREES,
  554. };
  555. static const struct udevice_id sdhci_ids[] = {
  556. {
  557. .compatible = "arasan,sdhci-5.1",
  558. .data = (ulong)&rk3399_data,
  559. },
  560. {
  561. .compatible = "rockchip,rk3568-dwcmshc",
  562. .data = (ulong)&rk3568_data,
  563. },
  564. {
  565. .compatible = "rockchip,rk3588-dwcmshc",
  566. .data = (ulong)&rk3588_data,
  567. },
  568. { }
  569. };
  570. U_BOOT_DRIVER(arasan_sdhci_drv) = {
  571. .name = "rockchip_sdhci_5_1",
  572. .id = UCLASS_MMC,
  573. .of_match = sdhci_ids,
  574. .of_to_plat = rockchip_sdhci_of_to_plat,
  575. .ops = &sdhci_ops,
  576. .bind = rockchip_sdhci_bind,
  577. .probe = rockchip_sdhci_probe,
  578. .priv_auto = sizeof(struct rockchip_sdhc),
  579. .plat_auto = sizeof(struct rockchip_sdhc_plat),
  580. };