phy-rockchip-emmc.c 12 KB

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