phy-ti-pipe3.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  1. /*
  2. * phy-ti-pipe3 - PIPE3 PHY driver.
  3. *
  4. * Copyright (C) 2013 Texas Instruments Incorporated - http://www.ti.com
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * Author: Kishon Vijay Abraham I <kishon@ti.com>
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. */
  18. #include <linux/module.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. #include <linux/phy/phy.h>
  22. #include <linux/of.h>
  23. #include <linux/clk.h>
  24. #include <linux/err.h>
  25. #include <linux/io.h>
  26. #include <linux/pm_runtime.h>
  27. #include <linux/delay.h>
  28. #include <linux/phy/omap_control_phy.h>
  29. #include <linux/of_platform.h>
  30. #define PLL_STATUS 0x00000004
  31. #define PLL_GO 0x00000008
  32. #define PLL_CONFIGURATION1 0x0000000C
  33. #define PLL_CONFIGURATION2 0x00000010
  34. #define PLL_CONFIGURATION3 0x00000014
  35. #define PLL_CONFIGURATION4 0x00000020
  36. #define PLL_REGM_MASK 0x001FFE00
  37. #define PLL_REGM_SHIFT 0x9
  38. #define PLL_REGM_F_MASK 0x0003FFFF
  39. #define PLL_REGM_F_SHIFT 0x0
  40. #define PLL_REGN_MASK 0x000001FE
  41. #define PLL_REGN_SHIFT 0x1
  42. #define PLL_SELFREQDCO_MASK 0x0000000E
  43. #define PLL_SELFREQDCO_SHIFT 0x1
  44. #define PLL_SD_MASK 0x0003FC00
  45. #define PLL_SD_SHIFT 10
  46. #define SET_PLL_GO 0x1
  47. #define PLL_LDOPWDN BIT(15)
  48. #define PLL_TICOPWDN BIT(16)
  49. #define PLL_LOCK 0x2
  50. #define PLL_IDLE 0x1
  51. /*
  52. * This is an Empirical value that works, need to confirm the actual
  53. * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
  54. * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
  55. */
  56. #define PLL_IDLE_TIME 100 /* in milliseconds */
  57. #define PLL_LOCK_TIME 100 /* in milliseconds */
  58. struct pipe3_dpll_params {
  59. u16 m;
  60. u8 n;
  61. u8 freq:3;
  62. u8 sd;
  63. u32 mf;
  64. };
  65. struct pipe3_dpll_map {
  66. unsigned long rate;
  67. struct pipe3_dpll_params params;
  68. };
  69. struct ti_pipe3 {
  70. void __iomem *pll_ctrl_base;
  71. struct device *dev;
  72. struct device *control_dev;
  73. struct clk *wkupclk;
  74. struct clk *sys_clk;
  75. struct clk *refclk;
  76. struct clk *div_clk;
  77. struct pipe3_dpll_map *dpll_map;
  78. u8 id;
  79. };
  80. static struct pipe3_dpll_map dpll_map_usb[] = {
  81. {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
  82. {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
  83. {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
  84. {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
  85. {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
  86. {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
  87. { }, /* Terminator */
  88. };
  89. static struct pipe3_dpll_map dpll_map_sata[] = {
  90. {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */
  91. {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */
  92. {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */
  93. {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */
  94. {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */
  95. {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */
  96. { }, /* Terminator */
  97. };
  98. static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
  99. {
  100. return __raw_readl(addr + offset);
  101. }
  102. static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
  103. u32 data)
  104. {
  105. __raw_writel(data, addr + offset);
  106. }
  107. static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy)
  108. {
  109. unsigned long rate;
  110. struct pipe3_dpll_map *dpll_map = phy->dpll_map;
  111. rate = clk_get_rate(phy->sys_clk);
  112. for (; dpll_map->rate; dpll_map++) {
  113. if (rate == dpll_map->rate)
  114. return &dpll_map->params;
  115. }
  116. dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
  117. return NULL;
  118. }
  119. static int ti_pipe3_power_off(struct phy *x)
  120. {
  121. struct ti_pipe3 *phy = phy_get_drvdata(x);
  122. omap_control_phy_power(phy->control_dev, 0);
  123. return 0;
  124. }
  125. static int ti_pipe3_power_on(struct phy *x)
  126. {
  127. struct ti_pipe3 *phy = phy_get_drvdata(x);
  128. omap_control_phy_power(phy->control_dev, 1);
  129. return 0;
  130. }
  131. static int ti_pipe3_dpll_wait_lock(struct ti_pipe3 *phy)
  132. {
  133. u32 val;
  134. unsigned long timeout;
  135. timeout = jiffies + msecs_to_jiffies(PLL_LOCK_TIME);
  136. do {
  137. cpu_relax();
  138. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  139. if (val & PLL_LOCK)
  140. break;
  141. } while (!time_after(jiffies, timeout));
  142. if (!(val & PLL_LOCK)) {
  143. dev_err(phy->dev, "DPLL failed to lock\n");
  144. return -EBUSY;
  145. }
  146. return 0;
  147. }
  148. static int ti_pipe3_dpll_program(struct ti_pipe3 *phy)
  149. {
  150. u32 val;
  151. struct pipe3_dpll_params *dpll_params;
  152. dpll_params = ti_pipe3_get_dpll_params(phy);
  153. if (!dpll_params)
  154. return -EINVAL;
  155. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  156. val &= ~PLL_REGN_MASK;
  157. val |= dpll_params->n << PLL_REGN_SHIFT;
  158. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  159. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  160. val &= ~PLL_SELFREQDCO_MASK;
  161. val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
  162. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  163. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  164. val &= ~PLL_REGM_MASK;
  165. val |= dpll_params->m << PLL_REGM_SHIFT;
  166. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  167. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
  168. val &= ~PLL_REGM_F_MASK;
  169. val |= dpll_params->mf << PLL_REGM_F_SHIFT;
  170. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
  171. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
  172. val &= ~PLL_SD_MASK;
  173. val |= dpll_params->sd << PLL_SD_SHIFT;
  174. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
  175. ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
  176. return ti_pipe3_dpll_wait_lock(phy);
  177. }
  178. static int ti_pipe3_init(struct phy *x)
  179. {
  180. struct ti_pipe3 *phy = phy_get_drvdata(x);
  181. u32 val;
  182. int ret = 0;
  183. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
  184. omap_control_pcie_pcs(phy->control_dev, phy->id, 0xF1);
  185. return 0;
  186. }
  187. /* Bring it out of IDLE if it is IDLE */
  188. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  189. if (val & PLL_IDLE) {
  190. val &= ~PLL_IDLE;
  191. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  192. ret = ti_pipe3_dpll_wait_lock(phy);
  193. }
  194. /* Program the DPLL only if not locked */
  195. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  196. if (!(val & PLL_LOCK))
  197. if (ti_pipe3_dpll_program(phy))
  198. return -EINVAL;
  199. return ret;
  200. }
  201. static int ti_pipe3_exit(struct phy *x)
  202. {
  203. struct ti_pipe3 *phy = phy_get_drvdata(x);
  204. u32 val;
  205. unsigned long timeout;
  206. /* SATA DPLL can't be powered down due to Errata i783 and PCIe
  207. * does not have internal DPLL
  208. */
  209. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata") ||
  210. of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie"))
  211. return 0;
  212. /* Put DPLL in IDLE mode */
  213. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  214. val |= PLL_IDLE;
  215. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  216. /* wait for LDO and Oscillator to power down */
  217. timeout = jiffies + msecs_to_jiffies(PLL_IDLE_TIME);
  218. do {
  219. cpu_relax();
  220. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  221. if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN))
  222. break;
  223. } while (!time_after(jiffies, timeout));
  224. if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) {
  225. dev_err(phy->dev, "Failed to power down: PLL_STATUS 0x%x\n",
  226. val);
  227. return -EBUSY;
  228. }
  229. return 0;
  230. }
  231. static struct phy_ops ops = {
  232. .init = ti_pipe3_init,
  233. .exit = ti_pipe3_exit,
  234. .power_on = ti_pipe3_power_on,
  235. .power_off = ti_pipe3_power_off,
  236. .owner = THIS_MODULE,
  237. };
  238. #ifdef CONFIG_OF
  239. static const struct of_device_id ti_pipe3_id_table[];
  240. #endif
  241. static int ti_pipe3_probe(struct platform_device *pdev)
  242. {
  243. struct ti_pipe3 *phy;
  244. struct phy *generic_phy;
  245. struct phy_provider *phy_provider;
  246. struct resource *res;
  247. struct device_node *node = pdev->dev.of_node;
  248. struct device_node *control_node;
  249. struct platform_device *control_pdev;
  250. const struct of_device_id *match;
  251. struct clk *clk;
  252. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  253. if (!phy)
  254. return -ENOMEM;
  255. phy->dev = &pdev->dev;
  256. if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
  257. match = of_match_device(of_match_ptr(ti_pipe3_id_table),
  258. &pdev->dev);
  259. if (!match)
  260. return -EINVAL;
  261. phy->dpll_map = (struct pipe3_dpll_map *)match->data;
  262. if (!phy->dpll_map) {
  263. dev_err(&pdev->dev, "no DPLL data\n");
  264. return -EINVAL;
  265. }
  266. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  267. "pll_ctrl");
  268. phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res);
  269. if (IS_ERR(phy->pll_ctrl_base))
  270. return PTR_ERR(phy->pll_ctrl_base);
  271. phy->sys_clk = devm_clk_get(phy->dev, "sysclk");
  272. if (IS_ERR(phy->sys_clk)) {
  273. dev_err(&pdev->dev, "unable to get sysclk\n");
  274. return -EINVAL;
  275. }
  276. }
  277. if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
  278. phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
  279. if (IS_ERR(phy->wkupclk)) {
  280. dev_err(&pdev->dev, "unable to get wkupclk\n");
  281. return PTR_ERR(phy->wkupclk);
  282. }
  283. phy->refclk = devm_clk_get(phy->dev, "refclk");
  284. if (IS_ERR(phy->refclk)) {
  285. dev_err(&pdev->dev, "unable to get refclk\n");
  286. return PTR_ERR(phy->refclk);
  287. }
  288. } else {
  289. phy->wkupclk = ERR_PTR(-ENODEV);
  290. phy->refclk = ERR_PTR(-ENODEV);
  291. }
  292. if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
  293. if (of_property_read_u8(node, "id", &phy->id) < 0)
  294. phy->id = 1;
  295. clk = devm_clk_get(phy->dev, "dpll_ref");
  296. if (IS_ERR(clk)) {
  297. dev_err(&pdev->dev, "unable to get dpll ref clk\n");
  298. return PTR_ERR(clk);
  299. }
  300. clk_set_rate(clk, 1500000000);
  301. clk = devm_clk_get(phy->dev, "dpll_ref_m2");
  302. if (IS_ERR(clk)) {
  303. dev_err(&pdev->dev, "unable to get dpll ref m2 clk\n");
  304. return PTR_ERR(clk);
  305. }
  306. clk_set_rate(clk, 100000000);
  307. clk = devm_clk_get(phy->dev, "phy-div");
  308. if (IS_ERR(clk)) {
  309. dev_err(&pdev->dev, "unable to get phy-div clk\n");
  310. return PTR_ERR(clk);
  311. }
  312. clk_set_rate(clk, 100000000);
  313. phy->div_clk = devm_clk_get(phy->dev, "div-clk");
  314. if (IS_ERR(phy->div_clk)) {
  315. dev_err(&pdev->dev, "unable to get div-clk\n");
  316. return PTR_ERR(phy->div_clk);
  317. }
  318. } else {
  319. phy->div_clk = ERR_PTR(-ENODEV);
  320. }
  321. control_node = of_parse_phandle(node, "ctrl-module", 0);
  322. if (!control_node) {
  323. dev_err(&pdev->dev, "Failed to get control device phandle\n");
  324. return -EINVAL;
  325. }
  326. control_pdev = of_find_device_by_node(control_node);
  327. if (!control_pdev) {
  328. dev_err(&pdev->dev, "Failed to get control device\n");
  329. return -EINVAL;
  330. }
  331. phy->control_dev = &control_pdev->dev;
  332. omap_control_phy_power(phy->control_dev, 0);
  333. platform_set_drvdata(pdev, phy);
  334. pm_runtime_enable(phy->dev);
  335. generic_phy = devm_phy_create(phy->dev, NULL, &ops, NULL);
  336. if (IS_ERR(generic_phy))
  337. return PTR_ERR(generic_phy);
  338. phy_set_drvdata(generic_phy, phy);
  339. phy_provider = devm_of_phy_provider_register(phy->dev,
  340. of_phy_simple_xlate);
  341. if (IS_ERR(phy_provider))
  342. return PTR_ERR(phy_provider);
  343. pm_runtime_get(&pdev->dev);
  344. return 0;
  345. }
  346. static int ti_pipe3_remove(struct platform_device *pdev)
  347. {
  348. if (!pm_runtime_suspended(&pdev->dev))
  349. pm_runtime_put(&pdev->dev);
  350. pm_runtime_disable(&pdev->dev);
  351. return 0;
  352. }
  353. #ifdef CONFIG_PM_RUNTIME
  354. static int ti_pipe3_runtime_suspend(struct device *dev)
  355. {
  356. struct ti_pipe3 *phy = dev_get_drvdata(dev);
  357. if (!IS_ERR(phy->wkupclk))
  358. clk_disable_unprepare(phy->wkupclk);
  359. if (!IS_ERR(phy->refclk))
  360. clk_disable_unprepare(phy->refclk);
  361. if (!IS_ERR(phy->div_clk))
  362. clk_disable_unprepare(phy->div_clk);
  363. return 0;
  364. }
  365. static int ti_pipe3_runtime_resume(struct device *dev)
  366. {
  367. u32 ret = 0;
  368. struct ti_pipe3 *phy = dev_get_drvdata(dev);
  369. if (!IS_ERR(phy->refclk)) {
  370. ret = clk_prepare_enable(phy->refclk);
  371. if (ret) {
  372. dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
  373. goto err1;
  374. }
  375. }
  376. if (!IS_ERR(phy->wkupclk)) {
  377. ret = clk_prepare_enable(phy->wkupclk);
  378. if (ret) {
  379. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  380. goto err2;
  381. }
  382. }
  383. if (!IS_ERR(phy->div_clk)) {
  384. ret = clk_prepare_enable(phy->div_clk);
  385. if (ret) {
  386. dev_err(phy->dev, "Failed to enable div_clk %d\n", ret);
  387. goto err3;
  388. }
  389. }
  390. return 0;
  391. err3:
  392. if (!IS_ERR(phy->wkupclk))
  393. clk_disable_unprepare(phy->wkupclk);
  394. err2:
  395. if (!IS_ERR(phy->refclk))
  396. clk_disable_unprepare(phy->refclk);
  397. err1:
  398. return ret;
  399. }
  400. static const struct dev_pm_ops ti_pipe3_pm_ops = {
  401. SET_RUNTIME_PM_OPS(ti_pipe3_runtime_suspend,
  402. ti_pipe3_runtime_resume, NULL)
  403. };
  404. #define DEV_PM_OPS (&ti_pipe3_pm_ops)
  405. #else
  406. #define DEV_PM_OPS NULL
  407. #endif
  408. #ifdef CONFIG_OF
  409. static const struct of_device_id ti_pipe3_id_table[] = {
  410. {
  411. .compatible = "ti,phy-usb3",
  412. .data = dpll_map_usb,
  413. },
  414. {
  415. .compatible = "ti,omap-usb3",
  416. .data = dpll_map_usb,
  417. },
  418. {
  419. .compatible = "ti,phy-pipe3-sata",
  420. .data = dpll_map_sata,
  421. },
  422. {
  423. .compatible = "ti,phy-pipe3-pcie",
  424. },
  425. {}
  426. };
  427. MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
  428. #endif
  429. static struct platform_driver ti_pipe3_driver = {
  430. .probe = ti_pipe3_probe,
  431. .remove = ti_pipe3_remove,
  432. .driver = {
  433. .name = "ti-pipe3",
  434. .pm = DEV_PM_OPS,
  435. .of_match_table = of_match_ptr(ti_pipe3_id_table),
  436. },
  437. };
  438. module_platform_driver(ti_pipe3_driver);
  439. MODULE_ALIAS("platform: ti_pipe3");
  440. MODULE_AUTHOR("Texas Instruments Inc.");
  441. MODULE_DESCRIPTION("TI PIPE3 phy driver");
  442. MODULE_LICENSE("GPL v2");