phy-rockchip-emmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * Rockchip emmc PHY driver
  3. *
  4. * Copyright (C) 2016 Shawn Lin <shawn.lin@rock-chips.com>
  5. * Copyright (C) 2016 ROCKCHIP, Inc.
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU General Public License for more details.
  15. */
  16. #include <linux/clk.h>
  17. #include <linux/delay.h>
  18. #include <linux/mfd/syscon.h>
  19. #include <linux/module.h>
  20. #include <linux/of.h>
  21. #include <linux/of_address.h>
  22. #include <linux/phy/phy.h>
  23. #include <linux/platform_device.h>
  24. #include <linux/regmap.h>
  25. /*
  26. * The higher 16-bit of this register is used for write protection
  27. * only if BIT(x + 16) set to 1 the BIT(x) can be written.
  28. */
  29. #define HIWORD_UPDATE(val, mask, shift) \
  30. ((val) << (shift) | (mask) << ((shift) + 16))
  31. /* Register definition */
  32. #define GRF_EMMCPHY_CON0 0x0
  33. #define GRF_EMMCPHY_CON1 0x4
  34. #define GRF_EMMCPHY_CON2 0x8
  35. #define GRF_EMMCPHY_CON3 0xc
  36. #define GRF_EMMCPHY_CON4 0x10
  37. #define GRF_EMMCPHY_CON5 0x14
  38. #define GRF_EMMCPHY_CON6 0x18
  39. #define GRF_EMMCPHY_STATUS 0x20
  40. #define PHYCTRL_PDB_MASK 0x1
  41. #define PHYCTRL_PDB_SHIFT 0x0
  42. #define PHYCTRL_PDB_PWR_ON 0x1
  43. #define PHYCTRL_PDB_PWR_OFF 0x0
  44. #define PHYCTRL_ENDLL_MASK 0x1
  45. #define PHYCTRL_ENDLL_SHIFT 0x1
  46. #define PHYCTRL_ENDLL_ENABLE 0x1
  47. #define PHYCTRL_ENDLL_DISABLE 0x0
  48. #define PHYCTRL_CALDONE_MASK 0x1
  49. #define PHYCTRL_CALDONE_SHIFT 0x6
  50. #define PHYCTRL_CALDONE_DONE 0x1
  51. #define PHYCTRL_CALDONE_GOING 0x0
  52. #define PHYCTRL_DLLRDY_MASK 0x1
  53. #define PHYCTRL_DLLRDY_SHIFT 0x5
  54. #define PHYCTRL_DLLRDY_DONE 0x1
  55. #define PHYCTRL_DLLRDY_GOING 0x0
  56. #define PHYCTRL_FREQSEL_200M 0x0
  57. #define PHYCTRL_FREQSEL_50M 0x1
  58. #define PHYCTRL_FREQSEL_100M 0x2
  59. #define PHYCTRL_FREQSEL_150M 0x3
  60. #define PHYCTRL_FREQSEL_MASK 0x3
  61. #define PHYCTRL_FREQSEL_SHIFT 0xc
  62. #define PHYCTRL_DR_MASK 0x7
  63. #define PHYCTRL_DR_SHIFT 0x4
  64. #define PHYCTRL_DR_50OHM 0x0
  65. #define PHYCTRL_DR_33OHM 0x1
  66. #define PHYCTRL_DR_66OHM 0x2
  67. #define PHYCTRL_DR_100OHM 0x3
  68. #define PHYCTRL_DR_40OHM 0x4
  69. #define PHYCTRL_OTAPDLYENA 0x1
  70. #define PHYCTRL_OTAPDLYENA_MASK 0x1
  71. #define PHYCTRL_OTAPDLYENA_SHIFT 0xb
  72. #define PHYCTRL_OTAPDLYSEL_MASK 0xf
  73. #define PHYCTRL_OTAPDLYSEL_SHIFT 0x7
  74. #define PHYCTRL_IS_CALDONE(x) \
  75. ((((x) >> PHYCTRL_CALDONE_SHIFT) & \
  76. PHYCTRL_CALDONE_MASK) == PHYCTRL_CALDONE_DONE)
  77. #define PHYCTRL_IS_DLLRDY(x) \
  78. ((((x) >> PHYCTRL_DLLRDY_SHIFT) & \
  79. PHYCTRL_DLLRDY_MASK) == PHYCTRL_DLLRDY_DONE)
  80. struct rockchip_emmc_phy {
  81. unsigned int reg_offset;
  82. struct regmap *reg_base;
  83. struct clk *emmcclk;
  84. };
  85. static int rockchip_emmc_phy_power(struct phy *phy, bool on_off)
  86. {
  87. struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
  88. unsigned int caldone;
  89. unsigned int dllrdy;
  90. unsigned int freqsel = PHYCTRL_FREQSEL_200M;
  91. unsigned long rate;
  92. int ret;
  93. /*
  94. * Keep phyctrl_pdb and phyctrl_endll low to allow
  95. * initialization of CALIO state M/C DFFs
  96. */
  97. regmap_write(rk_phy->reg_base,
  98. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  99. HIWORD_UPDATE(PHYCTRL_PDB_PWR_OFF,
  100. PHYCTRL_PDB_MASK,
  101. PHYCTRL_PDB_SHIFT));
  102. regmap_write(rk_phy->reg_base,
  103. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  104. HIWORD_UPDATE(PHYCTRL_ENDLL_DISABLE,
  105. PHYCTRL_ENDLL_MASK,
  106. PHYCTRL_ENDLL_SHIFT));
  107. /* Already finish power_off above */
  108. if (on_off == PHYCTRL_PDB_PWR_OFF)
  109. return 0;
  110. rate = clk_get_rate(rk_phy->emmcclk);
  111. if (rate != 0) {
  112. unsigned long ideal_rate;
  113. unsigned long diff;
  114. switch (rate) {
  115. case 1 ... 74999999:
  116. ideal_rate = 50000000;
  117. freqsel = PHYCTRL_FREQSEL_50M;
  118. break;
  119. case 75000000 ... 124999999:
  120. ideal_rate = 100000000;
  121. freqsel = PHYCTRL_FREQSEL_100M;
  122. break;
  123. case 125000000 ... 174999999:
  124. ideal_rate = 150000000;
  125. freqsel = PHYCTRL_FREQSEL_150M;
  126. break;
  127. default:
  128. ideal_rate = 200000000;
  129. break;
  130. }
  131. diff = (rate > ideal_rate) ?
  132. rate - ideal_rate : ideal_rate - rate;
  133. /*
  134. * In order for tuning delays to be accurate we need to be
  135. * pretty spot on for the DLL range, so warn if we're too
  136. * far off. Also warn if we're above the 200 MHz max. Don't
  137. * warn for really slow rates since we won't be tuning then.
  138. */
  139. if ((rate > 50000000 && diff > 15000000) || (rate > 200000000))
  140. dev_warn(&phy->dev, "Unsupported rate: %lu\n", rate);
  141. }
  142. /*
  143. * According to the user manual, calpad calibration
  144. * cycle takes more than 2us without the minimal recommended
  145. * value, so we may need a little margin here
  146. */
  147. udelay(3);
  148. regmap_write(rk_phy->reg_base,
  149. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  150. HIWORD_UPDATE(PHYCTRL_PDB_PWR_ON,
  151. PHYCTRL_PDB_MASK,
  152. PHYCTRL_PDB_SHIFT));
  153. /*
  154. * According to the user manual, it asks driver to wait 5us for
  155. * calpad busy trimming. However it is documented that this value is
  156. * PVT(A.K.A process,voltage and temperature) relevant, so some
  157. * failure cases are found which indicates we should be more tolerant
  158. * to calpad busy trimming.
  159. */
  160. ret = regmap_read_poll_timeout(rk_phy->reg_base,
  161. rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
  162. caldone, PHYCTRL_IS_CALDONE(caldone),
  163. 0, 50);
  164. if (ret) {
  165. pr_err("%s: caldone failed, ret=%d\n", __func__, ret);
  166. return ret;
  167. }
  168. /* Set the frequency of the DLL operation */
  169. regmap_write(rk_phy->reg_base,
  170. rk_phy->reg_offset + GRF_EMMCPHY_CON0,
  171. HIWORD_UPDATE(freqsel, PHYCTRL_FREQSEL_MASK,
  172. PHYCTRL_FREQSEL_SHIFT));
  173. /* Turn on the DLL */
  174. regmap_write(rk_phy->reg_base,
  175. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  176. HIWORD_UPDATE(PHYCTRL_ENDLL_ENABLE,
  177. PHYCTRL_ENDLL_MASK,
  178. PHYCTRL_ENDLL_SHIFT));
  179. /*
  180. * We turned on the DLL even though the rate was 0 because we the
  181. * clock might be turned on later. ...but we can't wait for the DLL
  182. * to lock when the rate is 0 because it will never lock with no
  183. * input clock.
  184. *
  185. * Technically we should be checking the lock later when the clock
  186. * is turned on, but for now we won't.
  187. */
  188. if (rate == 0)
  189. return 0;
  190. /*
  191. * After enabling analog DLL circuits docs say that we need 10.2 us if
  192. * our source clock is at 50 MHz and that lock time scales linearly
  193. * with clock speed. If we are powering on the PHY and the card clock
  194. * is super slow (like 100 kHZ) this could take as long as 5.1 ms as
  195. * per the math: 10.2 us * (50000000 Hz / 100000 Hz) => 5.1 ms
  196. * Hopefully we won't be running at 100 kHz, but we should still make
  197. * sure we wait long enough.
  198. *
  199. * NOTE: There appear to be corner cases where the DLL seems to take
  200. * extra long to lock for reasons that aren't understood. In some
  201. * extreme cases we've seen it take up to over 10ms (!). We'll be
  202. * generous and give it 50ms.
  203. */
  204. ret = regmap_read_poll_timeout(rk_phy->reg_base,
  205. rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
  206. dllrdy, PHYCTRL_IS_DLLRDY(dllrdy),
  207. 0, 50 * USEC_PER_MSEC);
  208. if (ret) {
  209. pr_err("%s: dllrdy failed. ret=%d\n", __func__, ret);
  210. return ret;
  211. }
  212. return 0;
  213. }
  214. static int rockchip_emmc_phy_init(struct phy *phy)
  215. {
  216. struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
  217. int ret = 0;
  218. /*
  219. * We purposely get the clock here and not in probe to avoid the
  220. * circular dependency problem. We expect:
  221. * - PHY driver to probe
  222. * - SDHCI driver to start probe
  223. * - SDHCI driver to register it's clock
  224. * - SDHCI driver to get the PHY
  225. * - SDHCI driver to init the PHY
  226. *
  227. * The clock is optional, so upon any error we just set to NULL.
  228. *
  229. * NOTE: we don't do anything special for EPROBE_DEFER here. Given the
  230. * above expected use case, EPROBE_DEFER isn't sensible to expect, so
  231. * it's just like any other error.
  232. */
  233. rk_phy->emmcclk = clk_get(&phy->dev, "emmcclk");
  234. if (IS_ERR(rk_phy->emmcclk)) {
  235. dev_dbg(&phy->dev, "Error getting emmcclk: %d\n", ret);
  236. rk_phy->emmcclk = NULL;
  237. }
  238. return ret;
  239. }
  240. static int rockchip_emmc_phy_exit(struct phy *phy)
  241. {
  242. struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
  243. clk_put(rk_phy->emmcclk);
  244. return 0;
  245. }
  246. static int rockchip_emmc_phy_power_off(struct phy *phy)
  247. {
  248. /* Power down emmc phy analog blocks */
  249. return rockchip_emmc_phy_power(phy, PHYCTRL_PDB_PWR_OFF);
  250. }
  251. static int rockchip_emmc_phy_power_on(struct phy *phy)
  252. {
  253. struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
  254. /* Drive impedance: 50 Ohm */
  255. regmap_write(rk_phy->reg_base,
  256. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  257. HIWORD_UPDATE(PHYCTRL_DR_50OHM,
  258. PHYCTRL_DR_MASK,
  259. PHYCTRL_DR_SHIFT));
  260. /* Output tap delay: enable */
  261. regmap_write(rk_phy->reg_base,
  262. rk_phy->reg_offset + GRF_EMMCPHY_CON0,
  263. HIWORD_UPDATE(PHYCTRL_OTAPDLYENA,
  264. PHYCTRL_OTAPDLYENA_MASK,
  265. PHYCTRL_OTAPDLYENA_SHIFT));
  266. /* Output tap delay */
  267. regmap_write(rk_phy->reg_base,
  268. rk_phy->reg_offset + GRF_EMMCPHY_CON0,
  269. HIWORD_UPDATE(4,
  270. PHYCTRL_OTAPDLYSEL_MASK,
  271. PHYCTRL_OTAPDLYSEL_SHIFT));
  272. /* Power up emmc phy analog blocks */
  273. return rockchip_emmc_phy_power(phy, PHYCTRL_PDB_PWR_ON);
  274. }
  275. static const struct phy_ops ops = {
  276. .init = rockchip_emmc_phy_init,
  277. .exit = rockchip_emmc_phy_exit,
  278. .power_on = rockchip_emmc_phy_power_on,
  279. .power_off = rockchip_emmc_phy_power_off,
  280. .owner = THIS_MODULE,
  281. };
  282. static int rockchip_emmc_phy_probe(struct platform_device *pdev)
  283. {
  284. struct device *dev = &pdev->dev;
  285. struct rockchip_emmc_phy *rk_phy;
  286. struct phy *generic_phy;
  287. struct phy_provider *phy_provider;
  288. struct regmap *grf;
  289. unsigned int reg_offset;
  290. if (!dev->parent || !dev->parent->of_node)
  291. return -ENODEV;
  292. grf = syscon_node_to_regmap(dev->parent->of_node);
  293. if (IS_ERR(grf)) {
  294. dev_err(dev, "Missing rockchip,grf property\n");
  295. return PTR_ERR(grf);
  296. }
  297. rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
  298. if (!rk_phy)
  299. return -ENOMEM;
  300. if (of_property_read_u32(dev->of_node, "reg", &reg_offset)) {
  301. dev_err(dev, "missing reg property in node %s\n",
  302. dev->of_node->name);
  303. return -EINVAL;
  304. }
  305. rk_phy->reg_offset = reg_offset;
  306. rk_phy->reg_base = grf;
  307. generic_phy = devm_phy_create(dev, dev->of_node, &ops);
  308. if (IS_ERR(generic_phy)) {
  309. dev_err(dev, "failed to create PHY\n");
  310. return PTR_ERR(generic_phy);
  311. }
  312. phy_set_drvdata(generic_phy, rk_phy);
  313. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  314. return PTR_ERR_OR_ZERO(phy_provider);
  315. }
  316. static const struct of_device_id rockchip_emmc_phy_dt_ids[] = {
  317. { .compatible = "rockchip,rk3399-emmc-phy" },
  318. {}
  319. };
  320. MODULE_DEVICE_TABLE(of, rockchip_emmc_phy_dt_ids);
  321. static struct platform_driver rockchip_emmc_driver = {
  322. .probe = rockchip_emmc_phy_probe,
  323. .driver = {
  324. .name = "rockchip-emmc-phy",
  325. .of_match_table = rockchip_emmc_phy_dt_ids,
  326. },
  327. };
  328. module_platform_driver(rockchip_emmc_driver);
  329. MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
  330. MODULE_DESCRIPTION("Rockchip EMMC PHY driver");
  331. MODULE_LICENSE("GPL v2");