phy-ti-pipe3.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704
  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. #define PIPE3_PHY_PWRCTL_CLK_CMD_MASK 0x003FC000
  55. #define PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT 14
  56. #define PIPE3_PHY_PWRCTL_CLK_FREQ_MASK 0xFFC00000
  57. #define PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT 22
  58. #define PIPE3_PHY_TX_RX_POWERON 0x3
  59. #define PIPE3_PHY_TX_RX_POWEROFF 0x0
  60. #define PCIE_PCS_MASK 0xFF0000
  61. #define PCIE_PCS_DELAY_COUNT_SHIFT 0x10
  62. /*
  63. * This is an Empirical value that works, need to confirm the actual
  64. * value required for the PIPE3PHY_PLL_CONFIGURATION2.PLL_IDLE status
  65. * to be correctly reflected in the PIPE3PHY_PLL_STATUS register.
  66. */
  67. #define PLL_IDLE_TIME 100 /* in milliseconds */
  68. #define PLL_LOCK_TIME 100 /* in milliseconds */
  69. struct pipe3_dpll_params {
  70. u16 m;
  71. u8 n;
  72. u8 freq:3;
  73. u8 sd;
  74. u32 mf;
  75. };
  76. struct pipe3_dpll_map {
  77. unsigned long rate;
  78. struct pipe3_dpll_params params;
  79. };
  80. struct ti_pipe3 {
  81. void __iomem *pll_ctrl_base;
  82. struct device *dev;
  83. struct device *control_dev;
  84. struct clk *wkupclk;
  85. struct clk *sys_clk;
  86. struct clk *refclk;
  87. struct clk *div_clk;
  88. struct pipe3_dpll_map *dpll_map;
  89. struct regmap *phy_power_syscon; /* ctrl. reg. acces */
  90. struct regmap *pcs_syscon; /* ctrl. reg. acces */
  91. struct regmap *dpll_reset_syscon; /* ctrl. reg. acces */
  92. unsigned int dpll_reset_reg; /* reg. index within syscon */
  93. unsigned int power_reg; /* power reg. index within syscon */
  94. unsigned int pcie_pcs_reg; /* pcs reg. index in syscon */
  95. bool sata_refclk_enabled;
  96. };
  97. static struct pipe3_dpll_map dpll_map_usb[] = {
  98. {12000000, {1250, 5, 4, 20, 0} }, /* 12 MHz */
  99. {16800000, {3125, 20, 4, 20, 0} }, /* 16.8 MHz */
  100. {19200000, {1172, 8, 4, 20, 65537} }, /* 19.2 MHz */
  101. {20000000, {1000, 7, 4, 10, 0} }, /* 20 MHz */
  102. {26000000, {1250, 12, 4, 20, 0} }, /* 26 MHz */
  103. {38400000, {3125, 47, 4, 20, 92843} }, /* 38.4 MHz */
  104. { }, /* Terminator */
  105. };
  106. static struct pipe3_dpll_map dpll_map_sata[] = {
  107. {12000000, {1000, 7, 4, 6, 0} }, /* 12 MHz */
  108. {16800000, {714, 7, 4, 6, 0} }, /* 16.8 MHz */
  109. {19200000, {625, 7, 4, 6, 0} }, /* 19.2 MHz */
  110. {20000000, {600, 7, 4, 6, 0} }, /* 20 MHz */
  111. {26000000, {461, 7, 4, 6, 0} }, /* 26 MHz */
  112. {38400000, {312, 7, 4, 6, 0} }, /* 38.4 MHz */
  113. { }, /* Terminator */
  114. };
  115. static inline u32 ti_pipe3_readl(void __iomem *addr, unsigned offset)
  116. {
  117. return __raw_readl(addr + offset);
  118. }
  119. static inline void ti_pipe3_writel(void __iomem *addr, unsigned offset,
  120. u32 data)
  121. {
  122. __raw_writel(data, addr + offset);
  123. }
  124. static struct pipe3_dpll_params *ti_pipe3_get_dpll_params(struct ti_pipe3 *phy)
  125. {
  126. unsigned long rate;
  127. struct pipe3_dpll_map *dpll_map = phy->dpll_map;
  128. rate = clk_get_rate(phy->sys_clk);
  129. for (; dpll_map->rate; dpll_map++) {
  130. if (rate == dpll_map->rate)
  131. return &dpll_map->params;
  132. }
  133. dev_err(phy->dev, "No DPLL configuration for %lu Hz SYS CLK\n", rate);
  134. return NULL;
  135. }
  136. static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy);
  137. static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy);
  138. static int ti_pipe3_power_off(struct phy *x)
  139. {
  140. u32 val;
  141. int ret;
  142. struct ti_pipe3 *phy = phy_get_drvdata(x);
  143. if (!phy->phy_power_syscon) {
  144. omap_control_phy_power(phy->control_dev, 0);
  145. return 0;
  146. }
  147. val = PIPE3_PHY_TX_RX_POWEROFF << PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT;
  148. ret = regmap_update_bits(phy->phy_power_syscon, phy->power_reg,
  149. PIPE3_PHY_PWRCTL_CLK_CMD_MASK, val);
  150. return ret;
  151. }
  152. static int ti_pipe3_power_on(struct phy *x)
  153. {
  154. u32 val;
  155. u32 mask;
  156. int ret;
  157. unsigned long rate;
  158. struct ti_pipe3 *phy = phy_get_drvdata(x);
  159. if (!phy->phy_power_syscon) {
  160. omap_control_phy_power(phy->control_dev, 1);
  161. return 0;
  162. }
  163. rate = clk_get_rate(phy->sys_clk);
  164. if (!rate) {
  165. dev_err(phy->dev, "Invalid clock rate\n");
  166. return -EINVAL;
  167. }
  168. rate = rate / 1000000;
  169. mask = OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_CMD_MASK |
  170. OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_MASK;
  171. val = PIPE3_PHY_TX_RX_POWERON << PIPE3_PHY_PWRCTL_CLK_CMD_SHIFT;
  172. val |= rate << OMAP_CTRL_PIPE3_PHY_PWRCTL_CLK_FREQ_SHIFT;
  173. ret = regmap_update_bits(phy->phy_power_syscon, phy->power_reg,
  174. mask, val);
  175. return ret;
  176. }
  177. static int ti_pipe3_dpll_wait_lock(struct ti_pipe3 *phy)
  178. {
  179. u32 val;
  180. unsigned long timeout;
  181. timeout = jiffies + msecs_to_jiffies(PLL_LOCK_TIME);
  182. do {
  183. cpu_relax();
  184. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  185. if (val & PLL_LOCK)
  186. return 0;
  187. } while (!time_after(jiffies, timeout));
  188. dev_err(phy->dev, "DPLL failed to lock\n");
  189. return -EBUSY;
  190. }
  191. static int ti_pipe3_dpll_program(struct ti_pipe3 *phy)
  192. {
  193. u32 val;
  194. struct pipe3_dpll_params *dpll_params;
  195. dpll_params = ti_pipe3_get_dpll_params(phy);
  196. if (!dpll_params)
  197. return -EINVAL;
  198. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  199. val &= ~PLL_REGN_MASK;
  200. val |= dpll_params->n << PLL_REGN_SHIFT;
  201. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  202. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  203. val &= ~PLL_SELFREQDCO_MASK;
  204. val |= dpll_params->freq << PLL_SELFREQDCO_SHIFT;
  205. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  206. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION1);
  207. val &= ~PLL_REGM_MASK;
  208. val |= dpll_params->m << PLL_REGM_SHIFT;
  209. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION1, val);
  210. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION4);
  211. val &= ~PLL_REGM_F_MASK;
  212. val |= dpll_params->mf << PLL_REGM_F_SHIFT;
  213. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION4, val);
  214. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION3);
  215. val &= ~PLL_SD_MASK;
  216. val |= dpll_params->sd << PLL_SD_SHIFT;
  217. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION3, val);
  218. ti_pipe3_writel(phy->pll_ctrl_base, PLL_GO, SET_PLL_GO);
  219. return ti_pipe3_dpll_wait_lock(phy);
  220. }
  221. static int ti_pipe3_init(struct phy *x)
  222. {
  223. struct ti_pipe3 *phy = phy_get_drvdata(x);
  224. u32 val;
  225. int ret = 0;
  226. ti_pipe3_enable_clocks(phy);
  227. /*
  228. * Set pcie_pcs register to 0x96 for proper functioning of phy
  229. * as recommended in AM572x TRM SPRUHZ6, section 18.5.2.2, table
  230. * 18-1804.
  231. */
  232. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
  233. if (!phy->pcs_syscon) {
  234. omap_control_pcie_pcs(phy->control_dev, 0x96);
  235. return 0;
  236. }
  237. val = 0x96 << OMAP_CTRL_PCIE_PCS_DELAY_COUNT_SHIFT;
  238. ret = regmap_update_bits(phy->pcs_syscon, phy->pcie_pcs_reg,
  239. PCIE_PCS_MASK, val);
  240. return ret;
  241. }
  242. /* Bring it out of IDLE if it is IDLE */
  243. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  244. if (val & PLL_IDLE) {
  245. val &= ~PLL_IDLE;
  246. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  247. ret = ti_pipe3_dpll_wait_lock(phy);
  248. }
  249. /* SATA has issues if re-programmed when locked */
  250. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  251. if ((val & PLL_LOCK) && of_device_is_compatible(phy->dev->of_node,
  252. "ti,phy-pipe3-sata"))
  253. return ret;
  254. /* Program the DPLL */
  255. ret = ti_pipe3_dpll_program(phy);
  256. if (ret) {
  257. ti_pipe3_disable_clocks(phy);
  258. return -EINVAL;
  259. }
  260. return ret;
  261. }
  262. static int ti_pipe3_exit(struct phy *x)
  263. {
  264. struct ti_pipe3 *phy = phy_get_drvdata(x);
  265. u32 val;
  266. unsigned long timeout;
  267. /* If dpll_reset_syscon is not present we wont power down SATA DPLL
  268. * due to Errata i783
  269. */
  270. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata") &&
  271. !phy->dpll_reset_syscon)
  272. return 0;
  273. /* PCIe doesn't have internal DPLL */
  274. if (!of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-pcie")) {
  275. /* Put DPLL in IDLE mode */
  276. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_CONFIGURATION2);
  277. val |= PLL_IDLE;
  278. ti_pipe3_writel(phy->pll_ctrl_base, PLL_CONFIGURATION2, val);
  279. /* wait for LDO and Oscillator to power down */
  280. timeout = jiffies + msecs_to_jiffies(PLL_IDLE_TIME);
  281. do {
  282. cpu_relax();
  283. val = ti_pipe3_readl(phy->pll_ctrl_base, PLL_STATUS);
  284. if ((val & PLL_TICOPWDN) && (val & PLL_LDOPWDN))
  285. break;
  286. } while (!time_after(jiffies, timeout));
  287. if (!(val & PLL_TICOPWDN) || !(val & PLL_LDOPWDN)) {
  288. dev_err(phy->dev, "Failed to power down: PLL_STATUS 0x%x\n",
  289. val);
  290. return -EBUSY;
  291. }
  292. }
  293. /* i783: SATA needs control bit toggle after PLL unlock */
  294. if (of_device_is_compatible(phy->dev->of_node, "ti,phy-pipe3-sata")) {
  295. regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
  296. SATA_PLL_SOFT_RESET, SATA_PLL_SOFT_RESET);
  297. regmap_update_bits(phy->dpll_reset_syscon, phy->dpll_reset_reg,
  298. SATA_PLL_SOFT_RESET, 0);
  299. }
  300. ti_pipe3_disable_clocks(phy);
  301. return 0;
  302. }
  303. static const struct phy_ops ops = {
  304. .init = ti_pipe3_init,
  305. .exit = ti_pipe3_exit,
  306. .power_on = ti_pipe3_power_on,
  307. .power_off = ti_pipe3_power_off,
  308. .owner = THIS_MODULE,
  309. };
  310. static const struct of_device_id ti_pipe3_id_table[];
  311. static int ti_pipe3_get_clk(struct ti_pipe3 *phy)
  312. {
  313. struct clk *clk;
  314. struct device *dev = phy->dev;
  315. struct device_node *node = dev->of_node;
  316. phy->refclk = devm_clk_get(dev, "refclk");
  317. if (IS_ERR(phy->refclk)) {
  318. dev_err(dev, "unable to get refclk\n");
  319. /* older DTBs have missing refclk in SATA PHY
  320. * so don't bail out in case of SATA PHY.
  321. */
  322. if (!of_device_is_compatible(node, "ti,phy-pipe3-sata"))
  323. return PTR_ERR(phy->refclk);
  324. }
  325. if (!of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
  326. phy->wkupclk = devm_clk_get(dev, "wkupclk");
  327. if (IS_ERR(phy->wkupclk)) {
  328. dev_err(dev, "unable to get wkupclk\n");
  329. return PTR_ERR(phy->wkupclk);
  330. }
  331. } else {
  332. phy->wkupclk = ERR_PTR(-ENODEV);
  333. }
  334. if (!of_device_is_compatible(node, "ti,phy-pipe3-pcie") ||
  335. phy->phy_power_syscon) {
  336. phy->sys_clk = devm_clk_get(dev, "sysclk");
  337. if (IS_ERR(phy->sys_clk)) {
  338. dev_err(dev, "unable to get sysclk\n");
  339. return -EINVAL;
  340. }
  341. }
  342. if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
  343. clk = devm_clk_get(dev, "dpll_ref");
  344. if (IS_ERR(clk)) {
  345. dev_err(dev, "unable to get dpll ref clk\n");
  346. return PTR_ERR(clk);
  347. }
  348. clk_set_rate(clk, 1500000000);
  349. clk = devm_clk_get(dev, "dpll_ref_m2");
  350. if (IS_ERR(clk)) {
  351. dev_err(dev, "unable to get dpll ref m2 clk\n");
  352. return PTR_ERR(clk);
  353. }
  354. clk_set_rate(clk, 100000000);
  355. clk = devm_clk_get(dev, "phy-div");
  356. if (IS_ERR(clk)) {
  357. dev_err(dev, "unable to get phy-div clk\n");
  358. return PTR_ERR(clk);
  359. }
  360. clk_set_rate(clk, 100000000);
  361. phy->div_clk = devm_clk_get(dev, "div-clk");
  362. if (IS_ERR(phy->div_clk)) {
  363. dev_err(dev, "unable to get div-clk\n");
  364. return PTR_ERR(phy->div_clk);
  365. }
  366. } else {
  367. phy->div_clk = ERR_PTR(-ENODEV);
  368. }
  369. return 0;
  370. }
  371. static int ti_pipe3_get_sysctrl(struct ti_pipe3 *phy)
  372. {
  373. struct device *dev = phy->dev;
  374. struct device_node *node = dev->of_node;
  375. struct device_node *control_node;
  376. struct platform_device *control_pdev;
  377. phy->phy_power_syscon = syscon_regmap_lookup_by_phandle(node,
  378. "syscon-phy-power");
  379. if (IS_ERR(phy->phy_power_syscon)) {
  380. dev_dbg(dev,
  381. "can't get syscon-phy-power, using control device\n");
  382. phy->phy_power_syscon = NULL;
  383. } else {
  384. if (of_property_read_u32_index(node,
  385. "syscon-phy-power", 1,
  386. &phy->power_reg)) {
  387. dev_err(dev, "couldn't get power reg. offset\n");
  388. return -EINVAL;
  389. }
  390. }
  391. if (!phy->phy_power_syscon) {
  392. control_node = of_parse_phandle(node, "ctrl-module", 0);
  393. if (!control_node) {
  394. dev_err(dev, "Failed to get control device phandle\n");
  395. return -EINVAL;
  396. }
  397. control_pdev = of_find_device_by_node(control_node);
  398. if (!control_pdev) {
  399. dev_err(dev, "Failed to get control device\n");
  400. return -EINVAL;
  401. }
  402. phy->control_dev = &control_pdev->dev;
  403. }
  404. if (of_device_is_compatible(node, "ti,phy-pipe3-pcie")) {
  405. phy->pcs_syscon = syscon_regmap_lookup_by_phandle(node,
  406. "syscon-pcs");
  407. if (IS_ERR(phy->pcs_syscon)) {
  408. dev_dbg(dev,
  409. "can't get syscon-pcs, using omap control\n");
  410. phy->pcs_syscon = NULL;
  411. } else {
  412. if (of_property_read_u32_index(node,
  413. "syscon-pcs", 1,
  414. &phy->pcie_pcs_reg)) {
  415. dev_err(dev,
  416. "couldn't get pcie pcs reg. offset\n");
  417. return -EINVAL;
  418. }
  419. }
  420. }
  421. if (of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
  422. phy->dpll_reset_syscon = syscon_regmap_lookup_by_phandle(node,
  423. "syscon-pllreset");
  424. if (IS_ERR(phy->dpll_reset_syscon)) {
  425. dev_info(dev,
  426. "can't get syscon-pllreset, sata dpll won't idle\n");
  427. phy->dpll_reset_syscon = NULL;
  428. } else {
  429. if (of_property_read_u32_index(node,
  430. "syscon-pllreset", 1,
  431. &phy->dpll_reset_reg)) {
  432. dev_err(dev,
  433. "couldn't get pllreset reg. offset\n");
  434. return -EINVAL;
  435. }
  436. }
  437. }
  438. return 0;
  439. }
  440. static int ti_pipe3_get_pll_base(struct ti_pipe3 *phy)
  441. {
  442. struct resource *res;
  443. const struct of_device_id *match;
  444. struct device *dev = phy->dev;
  445. struct device_node *node = dev->of_node;
  446. struct platform_device *pdev = to_platform_device(dev);
  447. if (of_device_is_compatible(node, "ti,phy-pipe3-pcie"))
  448. return 0;
  449. match = of_match_device(ti_pipe3_id_table, dev);
  450. if (!match)
  451. return -EINVAL;
  452. phy->dpll_map = (struct pipe3_dpll_map *)match->data;
  453. if (!phy->dpll_map) {
  454. dev_err(dev, "no DPLL data\n");
  455. return -EINVAL;
  456. }
  457. res = platform_get_resource_byname(pdev, IORESOURCE_MEM,
  458. "pll_ctrl");
  459. phy->pll_ctrl_base = devm_ioremap_resource(dev, res);
  460. if (IS_ERR(phy->pll_ctrl_base))
  461. return PTR_ERR(phy->pll_ctrl_base);
  462. return 0;
  463. }
  464. static int ti_pipe3_probe(struct platform_device *pdev)
  465. {
  466. struct ti_pipe3 *phy;
  467. struct phy *generic_phy;
  468. struct phy_provider *phy_provider;
  469. struct device_node *node = pdev->dev.of_node;
  470. struct device *dev = &pdev->dev;
  471. int ret;
  472. phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  473. if (!phy)
  474. return -ENOMEM;
  475. phy->dev = dev;
  476. ret = ti_pipe3_get_pll_base(phy);
  477. if (ret)
  478. return ret;
  479. ret = ti_pipe3_get_sysctrl(phy);
  480. if (ret)
  481. return ret;
  482. ret = ti_pipe3_get_clk(phy);
  483. if (ret)
  484. return ret;
  485. platform_set_drvdata(pdev, phy);
  486. pm_runtime_enable(dev);
  487. /*
  488. * Prevent auto-disable of refclk for SATA PHY due to Errata i783
  489. */
  490. if (of_device_is_compatible(node, "ti,phy-pipe3-sata")) {
  491. if (!IS_ERR(phy->refclk)) {
  492. clk_prepare_enable(phy->refclk);
  493. phy->sata_refclk_enabled = true;
  494. }
  495. }
  496. generic_phy = devm_phy_create(dev, NULL, &ops);
  497. if (IS_ERR(generic_phy))
  498. return PTR_ERR(generic_phy);
  499. phy_set_drvdata(generic_phy, phy);
  500. ti_pipe3_power_off(generic_phy);
  501. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  502. if (IS_ERR(phy_provider))
  503. return PTR_ERR(phy_provider);
  504. return 0;
  505. }
  506. static int ti_pipe3_remove(struct platform_device *pdev)
  507. {
  508. pm_runtime_disable(&pdev->dev);
  509. return 0;
  510. }
  511. static int ti_pipe3_enable_clocks(struct ti_pipe3 *phy)
  512. {
  513. int ret = 0;
  514. if (!IS_ERR(phy->refclk)) {
  515. ret = clk_prepare_enable(phy->refclk);
  516. if (ret) {
  517. dev_err(phy->dev, "Failed to enable refclk %d\n", ret);
  518. return ret;
  519. }
  520. }
  521. if (!IS_ERR(phy->wkupclk)) {
  522. ret = clk_prepare_enable(phy->wkupclk);
  523. if (ret) {
  524. dev_err(phy->dev, "Failed to enable wkupclk %d\n", ret);
  525. goto disable_refclk;
  526. }
  527. }
  528. if (!IS_ERR(phy->div_clk)) {
  529. ret = clk_prepare_enable(phy->div_clk);
  530. if (ret) {
  531. dev_err(phy->dev, "Failed to enable div_clk %d\n", ret);
  532. goto disable_wkupclk;
  533. }
  534. }
  535. return 0;
  536. disable_wkupclk:
  537. if (!IS_ERR(phy->wkupclk))
  538. clk_disable_unprepare(phy->wkupclk);
  539. disable_refclk:
  540. if (!IS_ERR(phy->refclk))
  541. clk_disable_unprepare(phy->refclk);
  542. return ret;
  543. }
  544. static void ti_pipe3_disable_clocks(struct ti_pipe3 *phy)
  545. {
  546. if (!IS_ERR(phy->wkupclk))
  547. clk_disable_unprepare(phy->wkupclk);
  548. if (!IS_ERR(phy->refclk)) {
  549. clk_disable_unprepare(phy->refclk);
  550. /*
  551. * SATA refclk needs an additional disable as we left it
  552. * on in probe to avoid Errata i783
  553. */
  554. if (phy->sata_refclk_enabled) {
  555. clk_disable_unprepare(phy->refclk);
  556. phy->sata_refclk_enabled = false;
  557. }
  558. }
  559. if (!IS_ERR(phy->div_clk))
  560. clk_disable_unprepare(phy->div_clk);
  561. }
  562. static const struct of_device_id ti_pipe3_id_table[] = {
  563. {
  564. .compatible = "ti,phy-usb3",
  565. .data = dpll_map_usb,
  566. },
  567. {
  568. .compatible = "ti,omap-usb3",
  569. .data = dpll_map_usb,
  570. },
  571. {
  572. .compatible = "ti,phy-pipe3-sata",
  573. .data = dpll_map_sata,
  574. },
  575. {
  576. .compatible = "ti,phy-pipe3-pcie",
  577. },
  578. {}
  579. };
  580. MODULE_DEVICE_TABLE(of, ti_pipe3_id_table);
  581. static struct platform_driver ti_pipe3_driver = {
  582. .probe = ti_pipe3_probe,
  583. .remove = ti_pipe3_remove,
  584. .driver = {
  585. .name = "ti-pipe3",
  586. .of_match_table = ti_pipe3_id_table,
  587. },
  588. };
  589. module_platform_driver(ti_pipe3_driver);
  590. MODULE_ALIAS("platform:ti_pipe3");
  591. MODULE_AUTHOR("Texas Instruments Inc.");
  592. MODULE_DESCRIPTION("TI PIPE3 phy driver");
  593. MODULE_LICENSE("GPL v2");