phy-ti-pipe3.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  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. #include <linux/mfd/syscon.h>
  31. #include <linux/regmap.h>
  32. #define PLL_STATUS 0x00000004
  33. #define PLL_GO 0x00000008
  34. #define PLL_CONFIGURATION1 0x0000000C
  35. #define PLL_CONFIGURATION2 0x00000010
  36. #define PLL_CONFIGURATION3 0x00000014
  37. #define PLL_CONFIGURATION4 0x00000020
  38. #define PLL_REGM_MASK 0x001FFE00
  39. #define PLL_REGM_SHIFT 0x9
  40. #define PLL_REGM_F_MASK 0x0003FFFF
  41. #define PLL_REGM_F_SHIFT 0x0
  42. #define PLL_REGN_MASK 0x000001FE
  43. #define PLL_REGN_SHIFT 0x1
  44. #define PLL_SELFREQDCO_MASK 0x0000000E
  45. #define PLL_SELFREQDCO_SHIFT 0x1
  46. #define PLL_SD_MASK 0x0003FC00
  47. #define PLL_SD_SHIFT 10
  48. #define SET_PLL_GO 0x1
  49. #define PLL_LDOPWDN BIT(15)
  50. #define PLL_TICOPWDN BIT(16)
  51. #define PLL_LOCK 0x2
  52. #define PLL_IDLE 0x1
  53. #define SATA_PLL_SOFT_RESET BIT(18)
  54. /*
  55. * This is an Empirical value that works, need to confirm the actual
  56. * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
  57. * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
  58. */
  59. #define PLL_IDLE_TIME 100 /* in milliseconds */
  60. #define PLL_LOCK_TIME 100 /* in milliseconds */
  61. struct pipe3_dpll_params {
  62. u16 m;
  63. u8 n;
  64. u8 freq:3;
  65. u8 sd;
  66. u32 mf;
  67. };
  68. struct pipe3_dpll_map {
  69. unsigned long rate;
  70. struct pipe3_dpll_params params;
  71. };
  72. struct ti_pipe3 {
  73. void __iomem *pll_ctrl_base;
  74. struct device *dev;
  75. struct device *control_dev;
  76. struct clk *wkupclk;
  77. struct clk *sys_clk;
  78. struct clk *refclk;
  79. struct clk *div_clk;
  80. struct pipe3_dpll_map *dpll_map;
  81. struct regmap *dpll_reset_syscon; /* ctrl. reg. acces */
  82. unsigned int dpll_reset_reg; /* reg. index within syscon */
  83. bool sata_refclk_enabled;
  84. };
  85. static struct pipe3_dpll_map dpll_map_usb[] = {
  86. {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
  87. {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
  88. {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
  89. {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
  90. {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
  91. {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
  92. { }, /* Terminator */
  93. };
  94. static struct pipe3_dpll_map dpll_map_sata[] = {
  95. {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */
  96. {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */
  97. {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */
  98. {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */
  99. {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */
  100. {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */
  101. { }, /* Terminator */
  102. };
  103. static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
  104. {
  105. return __raw_readl(addr + offset);
  106. }
  107. static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
  108. u32 data)
  109. {
  110. __raw_writel(data, addr + offset);
  111. }
  112. static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy)
  113. {
  114. unsigned long rate;
  115. struct pipe3_dpll_map *dpll_map = phy->dpll_map;
  116. rate = clk_get_rate(phy->sys_clk);
  117. for (; dpll_map->rate; dpll_map++) {
  118. if (rate == dpll_map->rate)
  119. return &dpll_map->params;
  120. }
  121. dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
  122. return NULL;
  123. }
  124. static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy);
  125. static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy);
  126. static int ti_pipe3_power_off(struct phy *x)
  127. {
  128. struct ti_pipe3 *phy = phy_get_drvdata(x);
  129. omap_control_phy_power(phy->control_dev, 0);
  130. return 0;
  131. }
  132. static int ti_pipe3_power_on(struct phy *x)
  133. {
  134. struct ti_pipe3 *phy = phy_get_drvdata(x);
  135. omap_control_phy_power(phy->control_dev, 1);
  136. return 0;
  137. }
  138. static int ti_pipe3_dpll_wait_lock(struct ti_pipe3 *phy)
  139. {
  140. u32 val;
  141. unsigned long timeout;
  142. timeout = jiffies + msecs_to_jiffies(PLL_LOCK_TIME);
  143. do {
  144. cpu_relax();
  145. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  146. if (val & PLL_LOCK)
  147. return 0;
  148. } while (!time_after(jiffies, timeout));
  149. dev_err(phy->dev, "DPLL failed to lock\n");
  150. return -EBUSY;
  151. }
  152. static int ti_pipe3_dpll_program(struct ti_pipe3 *phy)
  153. {
  154. u32 val;
  155. struct pipe3_dpll_params *dpll_params;
  156. dpll_params = ti_pipe3_get_dpll_params(phy);
  157. if (!dpll_params)
  158. return -EINVAL;
  159. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  160. val &= ~PLL_REGN_MASK;
  161. val |= dpll_params->n << PLL_REGN_SHIFT;
  162. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  163. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  164. val &= ~PLL_SELFREQDCO_MASK;
  165. val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
  166. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  167. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  168. val &= ~PLL_REGM_MASK;
  169. val |= dpll_params->m << PLL_REGM_SHIFT;
  170. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  171. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
  172. val &= ~PLL_REGM_F_MASK;
  173. val |= dpll_params->mf << PLL_REGM_F_SHIFT;
  174. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
  175. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
  176. val &= ~PLL_SD_MASK;
  177. val |= dpll_params->sd << PLL_SD_SHIFT;
  178. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
  179. ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
  180. return ti_pipe3_dpll_wait_lock(phy);
  181. }
  182. static int ti_pipe3_init(struct phy *x)
  183. {
  184. struct ti_pipe3 *phy = phy_get_drvdata(x);
  185. u32 val;
  186. int ret = 0;
  187. ti_pipe3_enable_clocks(phy);
  188. /*
  189. * Set pcie_pcs register to 0x96 for proper functioning of phy
  190. * as recommended in AM572x TRM SPRUHZ6, section 18.5.2.2, table
  191. * 18-1804.
  192. */
  193. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
  194. omap_control_pcie_pcs(phy->control_dev, 0x96);
  195. return 0;
  196. }
  197. /* Bring it out of IDLE if it is IDLE */
  198. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  199. if (val & PLL_IDLE) {
  200. val &= ~PLL_IDLE;
  201. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  202. ret = ti_pipe3_dpll_wait_lock(phy);
  203. }
  204. /* Program the DPLL only if not locked */
  205. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  206. if (!(val & PLL_LOCK))
  207. if (ti_pipe3_dpll_program(phy))
  208. return -EINVAL;
  209. return ret;
  210. }
  211. static int ti_pipe3_exit(struct phy *x)
  212. {
  213. struct ti_pipe3 *phy = phy_get_drvdata(x);
  214. u32 val;
  215. unsigned long timeout;
  216. /* If dpll_reset_syscon is not present we wont power down SATA DPLL
  217. * due to Errata i783
  218. */
  219. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata") &&
  220. !phy->dpll_reset_syscon)
  221. return 0;
  222. /* PCIe doesn't have internal DPLL */
  223. if (!of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
  224. /* Put DPLL in IDLE mode */
  225. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  226. val |= PLL_IDLE;
  227. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  228. /* wait for LDO and Oscillator to power down */
  229. timeout = jiffies + msecs_to_jiffies(PLL_IDLE_TIME);
  230. do {
  231. cpu_relax();
  232. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  233. if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN))
  234. break;
  235. } while (!time_after(jiffies, timeout));
  236. if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) {
  237. dev_err(phy->dev, "Failed to power down: PLL_STATUS 0x%x\n",
  238. val);
  239. return -EBUSY;
  240. }
  241. }
  242. /* i783: SATA needs control bit toggle after PLL unlock */
  243. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata")) {
  244. regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
  245. SATA_PLL_SOFT_RESET, SATA_PLL_SOFT_RESET);
  246. regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
  247. SATA_PLL_SOFT_RESET, 0);
  248. }
  249. ti_pipe3_disable_clocks(phy);
  250. return 0;
  251. }
  252. static const struct phy_ops ops = {
  253. .init = ti_pipe3_init,
  254. .exit = ti_pipe3_exit,
  255. .power_on = ti_pipe3_power_on,
  256. .power_off = ti_pipe3_power_off,
  257. .owner = THIS_MODULE,
  258. };
  259. static const struct of_device_id ti_pipe3_id_table[];
  260. static int ti_pipe3_probe(struct platform_device *pdev)
  261. {
  262. struct ti_pipe3 *phy;
  263. struct phy *generic_phy;
  264. struct phy_provider *phy_provider;
  265. struct resource *res;
  266. struct device_node *node = pdev->dev.of_node;
  267. struct device_node *control_node;
  268. struct platform_device *control_pdev;
  269. const struct of_device_id *match;
  270. struct clk *clk;
  271. phy = devm_kzalloc(&pdev->dev, sizeof(*phy), GFP_KERNEL);
  272. if (!phy)
  273. return -ENOMEM;
  274. phy->dev = &pdev->dev;
  275. if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
  276. match = of_match_device(ti_pipe3_id_table, &pdev->dev);
  277. if (!match)
  278. return -EINVAL;
  279. phy->dpll_map = (struct pipe3_dpll_map *)match->data;
  280. if (!phy->dpll_map) {
  281. dev_err(&pdev->dev, "no DPLL data\n");
  282. return -EINVAL;
  283. }
  284. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  285. "pll_ctrl");
  286. phy->pll_ctrl_base = devm_ioremap_resource(&pdev->dev, res);
  287. if (IS_ERR(phy->pll_ctrl_base))
  288. return PTR_ERR(phy->pll_ctrl_base);
  289. phy->sys_clk = devm_clk_get(phy->dev, "sysclk");
  290. if (IS_ERR(phy->sys_clk)) {
  291. dev_err(&pdev->dev, "unable to get sysclk\n");
  292. return -EINVAL;
  293. }
  294. }
  295. phy->refclk = devm_clk_get(phy->dev, "refclk");
  296. if (IS_ERR(phy->refclk)) {
  297. dev_err(&pdev->dev, "unable to get refclk\n");
  298. /* older DTBs have missing refclk in SATA PHY
  299. * so don't bail out in case of SATA PHY.
  300. */
  301. if (!of_device_is_compatible(node, "ti,phy-pipe3-sata"))
  302. return PTR_ERR(phy->refclk);
  303. }
  304. if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
  305. phy->wkupclk = devm_clk_get(phy->dev, "wkupclk");
  306. if (IS_ERR(phy->wkupclk)) {
  307. dev_err(&pdev->dev, "unable to get wkupclk\n");
  308. return PTR_ERR(phy->wkupclk);
  309. }
  310. } else {
  311. phy->wkupclk = ERR_PTR(-ENODEV);
  312. phy->dpll_reset_syscon = syscon_regmap_lookup_by_phandle(node,
  313. "syscon-pllreset");
  314. if (IS_ERR(phy->dpll_reset_syscon)) {
  315. dev_info(&pdev->dev,
  316. "can't get syscon-pllreset, sata dpll won't idle\n");
  317. phy->dpll_reset_syscon = NULL;
  318. } else {
  319. if (of_property_read_u32_index(node,
  320. "syscon-pllreset", 1,
  321. &phy->dpll_reset_reg)) {
  322. dev_err(&pdev->dev,
  323. "couldn't get pllreset reg. offset\n");
  324. return -EINVAL;
  325. }
  326. }
  327. }
  328. if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
  329. clk = devm_clk_get(phy->dev, "dpll_ref");
  330. if (IS_ERR(clk)) {
  331. dev_err(&pdev->dev, "unable to get dpll ref clk\n");
  332. return PTR_ERR(clk);
  333. }
  334. clk_set_rate(clk, 1500000000);
  335. clk = devm_clk_get(phy->dev, "dpll_ref_m2");
  336. if (IS_ERR(clk)) {
  337. dev_err(&pdev->dev, "unable to get dpll ref m2 clk\n");
  338. return PTR_ERR(clk);
  339. }
  340. clk_set_rate(clk, 100000000);
  341. clk = devm_clk_get(phy->dev, "phy-div");
  342. if (IS_ERR(clk)) {
  343. dev_err(&pdev->dev, "unable to get phy-div clk\n");
  344. return PTR_ERR(clk);
  345. }
  346. clk_set_rate(clk, 100000000);
  347. phy->div_clk = devm_clk_get(phy->dev, "div-clk");
  348. if (IS_ERR(phy->div_clk)) {
  349. dev_err(&pdev->dev, "unable to get div-clk\n");
  350. return PTR_ERR(phy->div_clk);
  351. }
  352. } else {
  353. phy->div_clk = ERR_PTR(-ENODEV);
  354. }
  355. control_node = of_parse_phandle(node, "ctrl-module", 0);
  356. if (!control_node) {
  357. dev_err(&pdev->dev, "Failed to get control device phandle\n");
  358. return -EINVAL;
  359. }
  360. control_pdev = of_find_device_by_node(control_node);
  361. if (!control_pdev) {
  362. dev_err(&pdev->dev, "Failed to get control device\n");
  363. return -EINVAL;
  364. }
  365. phy->control_dev = &control_pdev->dev;
  366. omap_control_phy_power(phy->control_dev, 0);
  367. platform_set_drvdata(pdev, phy);
  368. pm_runtime_enable(phy->dev);
  369. /*
  370. * Prevent auto-disable of refclk for SATA PHY due to Errata i783
  371. */
  372. if (of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
  373. if (!IS_ERR(phy->refclk)) {
  374. clk_prepare_enable(phy->refclk);
  375. phy->sata_refclk_enabled = true;
  376. }
  377. }
  378. generic_phy = devm_phy_create(phy->dev, NULL, &ops);
  379. if (IS_ERR(generic_phy))
  380. return PTR_ERR(generic_phy);
  381. phy_set_drvdata(generic_phy, phy);
  382. phy_provider = devm_of_phy_provider_register(phy->dev,
  383. of_phy_simple_xlate);
  384. if (IS_ERR(phy_provider))
  385. return PTR_ERR(phy_provider);
  386. return 0;
  387. }
  388. static int ti_pipe3_remove(struct platform_device *pdev)
  389. {
  390. pm_runtime_disable(&pdev->dev);
  391. return 0;
  392. }
  393. static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy)
  394. {
  395. int ret = 0;
  396. if (!IS_ERR(phy->refclk)) {
  397. ret = clk_prepare_enable(phy->refclk);
  398. if (ret) {
  399. dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
  400. return ret;
  401. }
  402. }
  403. if (!IS_ERR(phy->wkupclk)) {
  404. ret = clk_prepare_enable(phy->wkupclk);
  405. if (ret) {
  406. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  407. goto disable_refclk;
  408. }
  409. }
  410. if (!IS_ERR(phy->div_clk)) {
  411. ret = clk_prepare_enable(phy->div_clk);
  412. if (ret) {
  413. dev_err(phy->dev, "Failed to enable div_clk %d\n", ret);
  414. goto disable_wkupclk;
  415. }
  416. }
  417. return 0;
  418. disable_wkupclk:
  419. if (!IS_ERR(phy->wkupclk))
  420. clk_disable_unprepare(phy->wkupclk);
  421. disable_refclk:
  422. if (!IS_ERR(phy->refclk))
  423. clk_disable_unprepare(phy->refclk);
  424. return ret;
  425. }
  426. static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy)
  427. {
  428. if (!IS_ERR(phy->wkupclk))
  429. clk_disable_unprepare(phy->wkupclk);
  430. if (!IS_ERR(phy->refclk)) {
  431. clk_disable_unprepare(phy->refclk);
  432. /*
  433. * SATA refclk needs an additional disable as we left it
  434. * on in probe to avoid Errata i783
  435. */
  436. if (phy->sata_refclk_enabled) {
  437. clk_disable_unprepare(phy->refclk);
  438. phy->sata_refclk_enabled = false;
  439. }
  440. }
  441. if (!IS_ERR(phy->div_clk))
  442. clk_disable_unprepare(phy->div_clk);
  443. }
  444. static const struct of_device_id ti_pipe3_id_table[] = {
  445. {
  446. .compatible = "ti,phy-usb3",
  447. .data = dpll_map_usb,
  448. },
  449. {
  450. .compatible = "ti,omap-usb3",
  451. .data = dpll_map_usb,
  452. },
  453. {
  454. .compatible = "ti,phy-pipe3-sata",
  455. .data = dpll_map_sata,
  456. },
  457. {
  458. .compatible = "ti,phy-pipe3-pcie",
  459. },
  460. {}
  461. };
  462. MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
  463. static struct platform_driver ti_pipe3_driver = {
  464. .probe = ti_pipe3_probe,
  465. .remove = ti_pipe3_remove,
  466. .driver = {
  467. .name = "ti-pipe3",
  468. .of_match_table = ti_pipe3_id_table,
  469. },
  470. };
  471. module_platform_driver(ti_pipe3_driver);
  472. MODULE_ALIAS("platform:ti_pipe3");
  473. MODULE_AUTHOR("Texas Instruments Inc.");
  474. MODULE_DESCRIPTION("TI PIPE3 phy driver");
  475. MODULE_LICENSE("GPL v2");