phy-rockchip-emmc.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383
  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. struct rockchip_emmc_phy {
  75. unsigned int reg_offset;
  76. struct regmap *reg_base;
  77. struct clk *emmcclk;
  78. };
  79. static int rockchip_emmc_phy_power(struct phy *phy, bool on_off)
  80. {
  81. struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
  82. unsigned int caldone;
  83. unsigned int dllrdy;
  84. unsigned int freqsel = PHYCTRL_FREQSEL_200M;
  85. unsigned long rate;
  86. unsigned long timeout;
  87. /*
  88. * Keep phyctrl_pdb and phyctrl_endll low to allow
  89. * initialization of CALIO state M/C DFFs
  90. */
  91. regmap_write(rk_phy->reg_base,
  92. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  93. HIWORD_UPDATE(PHYCTRL_PDB_PWR_OFF,
  94. PHYCTRL_PDB_MASK,
  95. PHYCTRL_PDB_SHIFT));
  96. regmap_write(rk_phy->reg_base,
  97. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  98. HIWORD_UPDATE(PHYCTRL_ENDLL_DISABLE,
  99. PHYCTRL_ENDLL_MASK,
  100. PHYCTRL_ENDLL_SHIFT));
  101. /* Already finish power_off above */
  102. if (on_off == PHYCTRL_PDB_PWR_OFF)
  103. return 0;
  104. rate = clk_get_rate(rk_phy->emmcclk);
  105. if (rate != 0) {
  106. unsigned long ideal_rate;
  107. unsigned long diff;
  108. switch (rate) {
  109. case 1 ... 74999999:
  110. ideal_rate = 50000000;
  111. freqsel = PHYCTRL_FREQSEL_50M;
  112. break;
  113. case 75000000 ... 124999999:
  114. ideal_rate = 100000000;
  115. freqsel = PHYCTRL_FREQSEL_100M;
  116. break;
  117. case 125000000 ... 174999999:
  118. ideal_rate = 150000000;
  119. freqsel = PHYCTRL_FREQSEL_150M;
  120. break;
  121. default:
  122. ideal_rate = 200000000;
  123. break;
  124. };
  125. diff = (rate > ideal_rate) ?
  126. rate - ideal_rate : ideal_rate - rate;
  127. /*
  128. * In order for tuning delays to be accurate we need to be
  129. * pretty spot on for the DLL range, so warn if we're too
  130. * far off. Also warn if we're above the 200 MHz max. Don't
  131. * warn for really slow rates since we won't be tuning then.
  132. */
  133. if ((rate > 50000000 && diff > 15000000) || (rate > 200000000))
  134. dev_warn(&phy->dev, "Unsupported rate: %lu\n", rate);
  135. }
  136. /*
  137. * According to the user manual, calpad calibration
  138. * cycle takes more than 2us without the minimal recommended
  139. * value, so we may need a little margin here
  140. */
  141. udelay(3);
  142. regmap_write(rk_phy->reg_base,
  143. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  144. HIWORD_UPDATE(PHYCTRL_PDB_PWR_ON,
  145. PHYCTRL_PDB_MASK,
  146. PHYCTRL_PDB_SHIFT));
  147. /*
  148. * According to the user manual, it asks driver to
  149. * wait 5us for calpad busy trimming
  150. */
  151. udelay(5);
  152. regmap_read(rk_phy->reg_base,
  153. rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
  154. &caldone);
  155. caldone = (caldone >> PHYCTRL_CALDONE_SHIFT) & PHYCTRL_CALDONE_MASK;
  156. if (caldone != PHYCTRL_CALDONE_DONE) {
  157. pr_err("rockchip_emmc_phy_power: caldone timeout.\n");
  158. return -ETIMEDOUT;
  159. }
  160. /* Set the frequency of the DLL operation */
  161. regmap_write(rk_phy->reg_base,
  162. rk_phy->reg_offset + GRF_EMMCPHY_CON0,
  163. HIWORD_UPDATE(freqsel, PHYCTRL_FREQSEL_MASK,
  164. PHYCTRL_FREQSEL_SHIFT));
  165. /* Turn on the DLL */
  166. regmap_write(rk_phy->reg_base,
  167. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  168. HIWORD_UPDATE(PHYCTRL_ENDLL_ENABLE,
  169. PHYCTRL_ENDLL_MASK,
  170. PHYCTRL_ENDLL_SHIFT));
  171. /*
  172. * We turned on the DLL even though the rate was 0 because we the
  173. * clock might be turned on later. ...but we can't wait for the DLL
  174. * to lock when the rate is 0 because it will never lock with no
  175. * input clock.
  176. *
  177. * Technically we should be checking the lock later when the clock
  178. * is turned on, but for now we won't.
  179. */
  180. if (rate == 0)
  181. return 0;
  182. /*
  183. * After enabling analog DLL circuits docs say that we need 10.2 us if
  184. * our source clock is at 50 MHz and that lock time scales linearly
  185. * with clock speed. If we are powering on the PHY and the card clock
  186. * is super slow (like 100 kHZ) this could take as long as 5.1 ms as
  187. * per the math: 10.2 us * (50000000 Hz / 100000 Hz) => 5.1 ms
  188. * Hopefully we won't be running at 100 kHz, but we should still make
  189. * sure we wait long enough.
  190. *
  191. * NOTE: There appear to be corner cases where the DLL seems to take
  192. * extra long to lock for reasons that aren't understood. In some
  193. * extreme cases we've seen it take up to over 10ms (!). We'll be
  194. * generous and give it 50ms. We still busy wait here because:
  195. * - In most cases it should be super fast.
  196. * - This is not called lots during normal operation so it shouldn't
  197. * be a power or performance problem to busy wait. We expect it
  198. * only at boot / resume. In both cases, eMMC is probably on the
  199. * critical path so busy waiting a little extra time should be OK.
  200. */
  201. timeout = jiffies + msecs_to_jiffies(50);
  202. do {
  203. udelay(1);
  204. regmap_read(rk_phy->reg_base,
  205. rk_phy->reg_offset + GRF_EMMCPHY_STATUS,
  206. &dllrdy);
  207. dllrdy = (dllrdy >> PHYCTRL_DLLRDY_SHIFT) & PHYCTRL_DLLRDY_MASK;
  208. if (dllrdy == PHYCTRL_DLLRDY_DONE)
  209. break;
  210. } while (!time_after(jiffies, timeout));
  211. if (dllrdy != PHYCTRL_DLLRDY_DONE) {
  212. pr_err("rockchip_emmc_phy_power: dllrdy timeout.\n");
  213. return -ETIMEDOUT;
  214. }
  215. return 0;
  216. }
  217. static int rockchip_emmc_phy_init(struct phy *phy)
  218. {
  219. struct rockchip_emmc_phy *rk_phy = phy_get_drvdata(phy);
  220. int ret = 0;
  221. /*
  222. * We purposely get the clock here and not in probe to avoid the
  223. * circular dependency problem. We expect:
  224. * - PHY driver to probe
  225. * - SDHCI driver to start probe
  226. * - SDHCI driver to register it's clock
  227. * - SDHCI driver to get the PHY
  228. * - SDHCI driver to init the PHY
  229. *
  230. * The clock is optional, so upon any error we just set to NULL.
  231. *
  232. * NOTE: we don't do anything special for EPROBE_DEFER here. Given the
  233. * above expected use case, EPROBE_DEFER isn't sensible to expect, so
  234. * it's just like any other error.
  235. */
  236. rk_phy->emmcclk = clk_get(&phy->dev, "emmcclk");
  237. if (IS_ERR(rk_phy->emmcclk)) {
  238. dev_dbg(&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: 50 Ohm */
  258. regmap_write(rk_phy->reg_base,
  259. rk_phy->reg_offset + GRF_EMMCPHY_CON6,
  260. HIWORD_UPDATE(PHYCTRL_DR_50OHM,
  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(4,
  273. PHYCTRL_OTAPDLYSEL_MASK,
  274. PHYCTRL_OTAPDLYSEL_SHIFT));
  275. /* Power up emmc phy analog blocks */
  276. return rockchip_emmc_phy_power(phy, PHYCTRL_PDB_PWR_ON);
  277. }
  278. static const struct phy_ops ops = {
  279. .init = rockchip_emmc_phy_init,
  280. .exit = rockchip_emmc_phy_exit,
  281. .power_on = rockchip_emmc_phy_power_on,
  282. .power_off = rockchip_emmc_phy_power_off,
  283. .owner = THIS_MODULE,
  284. };
  285. static int rockchip_emmc_phy_probe(struct platform_device *pdev)
  286. {
  287. struct device *dev = &pdev->dev;
  288. struct rockchip_emmc_phy *rk_phy;
  289. struct phy *generic_phy;
  290. struct phy_provider *phy_provider;
  291. struct regmap *grf;
  292. unsigned int reg_offset;
  293. if (!dev->parent || !dev->parent->of_node)
  294. return -ENODEV;
  295. grf = syscon_node_to_regmap(dev->parent->of_node);
  296. if (IS_ERR(grf)) {
  297. dev_err(dev, "Missing rockchip,grf property\n");
  298. return PTR_ERR(grf);
  299. }
  300. rk_phy = devm_kzalloc(dev, sizeof(*rk_phy), GFP_KERNEL);
  301. if (!rk_phy)
  302. return -ENOMEM;
  303. if (of_property_read_u32(dev->of_node, "reg", &reg_offset)) {
  304. dev_err(dev, "missing reg property in node %s\n",
  305. dev->of_node->name);
  306. return -EINVAL;
  307. }
  308. rk_phy->reg_offset = reg_offset;
  309. rk_phy->reg_base = grf;
  310. generic_phy = devm_phy_create(dev, dev->of_node, &ops);
  311. if (IS_ERR(generic_phy)) {
  312. dev_err(dev, "failed to create PHY\n");
  313. return PTR_ERR(generic_phy);
  314. }
  315. phy_set_drvdata(generic_phy, rk_phy);
  316. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  317. return PTR_ERR_OR_ZERO(phy_provider);
  318. }
  319. static const struct of_device_id rockchip_emmc_phy_dt_ids[] = {
  320. { .compatible = "rockchip,rk3399-emmc-phy" },
  321. {}
  322. };
  323. MODULE_DEVICE_TABLE(of, rockchip_emmc_phy_dt_ids);
  324. static struct platform_driver rockchip_emmc_driver = {
  325. .probe = rockchip_emmc_phy_probe,
  326. .driver = {
  327. .name = "rockchip-emmc-phy",
  328. .of_match_table = rockchip_emmc_phy_dt_ids,
  329. },
  330. };
  331. module_platform_driver(rockchip_emmc_driver);
  332. MODULE_AUTHOR("Shawn Lin <shawn.lin@rock-chips.com>");
  333. MODULE_DESCRIPTION("Rockchip EMMC PHY driver");
  334. MODULE_LICENSE("GPL v2");