phy-pxa-28nm-usb2.c 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355
  1. /*
  2. * Copyright (C) 2015 Linaro, Ltd.
  3. * Rob Herring <robh@kernel.org>
  4. *
  5. * Based on vendor driver:
  6. * Copyright (C) 2013 Marvell Inc.
  7. * Author: Chao Xie <xiechao.mail@gmail.com>
  8. *
  9. * This software is licensed under the terms of the GNU General Public
  10. * License version 2, as published by the Free Software Foundation, and
  11. * may be copied, distributed, and modified under those terms.
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. */
  19. #include <linux/delay.h>
  20. #include <linux/slab.h>
  21. #include <linux/of.h>
  22. #include <linux/of_device.h>
  23. #include <linux/io.h>
  24. #include <linux/err.h>
  25. #include <linux/clk.h>
  26. #include <linux/module.h>
  27. #include <linux/platform_device.h>
  28. #include <linux/phy/phy.h>
  29. /* USB PXA1928 PHY mapping */
  30. #define PHY_28NM_PLL_REG0 0x0
  31. #define PHY_28NM_PLL_REG1 0x4
  32. #define PHY_28NM_CAL_REG 0x8
  33. #define PHY_28NM_TX_REG0 0x0c
  34. #define PHY_28NM_TX_REG1 0x10
  35. #define PHY_28NM_RX_REG0 0x14
  36. #define PHY_28NM_RX_REG1 0x18
  37. #define PHY_28NM_DIG_REG0 0x1c
  38. #define PHY_28NM_DIG_REG1 0x20
  39. #define PHY_28NM_TEST_REG0 0x24
  40. #define PHY_28NM_TEST_REG1 0x28
  41. #define PHY_28NM_MOC_REG 0x2c
  42. #define PHY_28NM_PHY_RESERVE 0x30
  43. #define PHY_28NM_OTG_REG 0x34
  44. #define PHY_28NM_CHRG_DET 0x38
  45. #define PHY_28NM_CTRL_REG0 0xc4
  46. #define PHY_28NM_CTRL_REG1 0xc8
  47. #define PHY_28NM_CTRL_REG2 0xd4
  48. #define PHY_28NM_CTRL_REG3 0xdc
  49. /* PHY_28NM_PLL_REG0 */
  50. #define PHY_28NM_PLL_READY BIT(31)
  51. #define PHY_28NM_PLL_SELLPFR_SHIFT 28
  52. #define PHY_28NM_PLL_SELLPFR_MASK (0x3 << 28)
  53. #define PHY_28NM_PLL_FBDIV_SHIFT 16
  54. #define PHY_28NM_PLL_FBDIV_MASK (0x1ff << 16)
  55. #define PHY_28NM_PLL_ICP_SHIFT 8
  56. #define PHY_28NM_PLL_ICP_MASK (0x7 << 8)
  57. #define PHY_28NM_PLL_REFDIV_SHIFT 0
  58. #define PHY_28NM_PLL_REFDIV_MASK 0x7f
  59. /* PHY_28NM_PLL_REG1 */
  60. #define PHY_28NM_PLL_PU_BY_REG BIT(1)
  61. #define PHY_28NM_PLL_PU_PLL BIT(0)
  62. /* PHY_28NM_CAL_REG */
  63. #define PHY_28NM_PLL_PLLCAL_DONE BIT(31)
  64. #define PHY_28NM_PLL_IMPCAL_DONE BIT(23)
  65. #define PHY_28NM_PLL_KVCO_SHIFT 16
  66. #define PHY_28NM_PLL_KVCO_MASK (0x7 << 16)
  67. #define PHY_28NM_PLL_CAL12_SHIFT 20
  68. #define PHY_28NM_PLL_CAL12_MASK (0x3 << 20)
  69. #define PHY_28NM_IMPCAL_VTH_SHIFT 8
  70. #define PHY_28NM_IMPCAL_VTH_MASK (0x7 << 8)
  71. #define PHY_28NM_PLLCAL_START_SHIFT 22
  72. #define PHY_28NM_IMPCAL_START_SHIFT 13
  73. /* PHY_28NM_TX_REG0 */
  74. #define PHY_28NM_TX_PU_BY_REG BIT(25)
  75. #define PHY_28NM_TX_PU_ANA BIT(24)
  76. #define PHY_28NM_TX_AMP_SHIFT 20
  77. #define PHY_28NM_TX_AMP_MASK (0x7 << 20)
  78. /* PHY_28NM_RX_REG0 */
  79. #define PHY_28NM_RX_SQ_THRESH_SHIFT 0
  80. #define PHY_28NM_RX_SQ_THRESH_MASK (0xf << 0)
  81. /* PHY_28NM_RX_REG1 */
  82. #define PHY_28NM_RX_SQCAL_DONE BIT(31)
  83. /* PHY_28NM_DIG_REG0 */
  84. #define PHY_28NM_DIG_BITSTAFFING_ERR BIT(31)
  85. #define PHY_28NM_DIG_SYNC_ERR BIT(30)
  86. #define PHY_28NM_DIG_SQ_FILT_SHIFT 16
  87. #define PHY_28NM_DIG_SQ_FILT_MASK (0x7 << 16)
  88. #define PHY_28NM_DIG_SQ_BLK_SHIFT 12
  89. #define PHY_28NM_DIG_SQ_BLK_MASK (0x7 << 12)
  90. #define PHY_28NM_DIG_SYNC_NUM_SHIFT 0
  91. #define PHY_28NM_DIG_SYNC_NUM_MASK (0x3 << 0)
  92. #define PHY_28NM_PLL_LOCK_BYPASS BIT(7)
  93. /* PHY_28NM_OTG_REG */
  94. #define PHY_28NM_OTG_CONTROL_BY_PIN BIT(5)
  95. #define PHY_28NM_OTG_PU_OTG BIT(4)
  96. #define PHY_28NM_CHGDTC_ENABLE_SWITCH_DM_SHIFT_28 13
  97. #define PHY_28NM_CHGDTC_ENABLE_SWITCH_DP_SHIFT_28 12
  98. #define PHY_28NM_CHGDTC_VSRC_CHARGE_SHIFT_28 10
  99. #define PHY_28NM_CHGDTC_VDAT_CHARGE_SHIFT_28 8
  100. #define PHY_28NM_CHGDTC_CDP_DM_AUTO_SWITCH_SHIFT_28 7
  101. #define PHY_28NM_CHGDTC_DP_DM_SWAP_SHIFT_28 6
  102. #define PHY_28NM_CHGDTC_PU_CHRG_DTC_SHIFT_28 5
  103. #define PHY_28NM_CHGDTC_PD_EN_SHIFT_28 4
  104. #define PHY_28NM_CHGDTC_DCP_EN_SHIFT_28 3
  105. #define PHY_28NM_CHGDTC_CDP_EN_SHIFT_28 2
  106. #define PHY_28NM_CHGDTC_TESTMON_CHRGDTC_SHIFT_28 0
  107. #define PHY_28NM_CTRL1_CHRG_DTC_OUT_SHIFT_28 4
  108. #define PHY_28NM_CTRL1_VBUSDTC_OUT_SHIFT_28 2
  109. #define PHY_28NM_CTRL3_OVERWRITE BIT(0)
  110. #define PHY_28NM_CTRL3_VBUS_VALID BIT(4)
  111. #define PHY_28NM_CTRL3_AVALID BIT(5)
  112. #define PHY_28NM_CTRL3_BVALID BIT(6)
  113. struct mv_usb2_phy {
  114. struct phy *phy;
  115. struct platform_device *pdev;
  116. void __iomem *base;
  117. struct clk *clk;
  118. };
  119. static bool wait_for_reg(void __iomem *reg, u32 mask, unsigned long timeout)
  120. {
  121. timeout += jiffies;
  122. while (time_is_after_eq_jiffies(timeout)) {
  123. if ((readl(reg) & mask) == mask)
  124. return true;
  125. msleep(1);
  126. }
  127. return false;
  128. }
  129. static int mv_usb2_phy_28nm_init(struct phy *phy)
  130. {
  131. struct mv_usb2_phy *mv_phy = phy_get_drvdata(phy);
  132. struct platform_device *pdev = mv_phy->pdev;
  133. void __iomem *base = mv_phy->base;
  134. u32 reg;
  135. int ret;
  136. clk_prepare_enable(mv_phy->clk);
  137. /* PHY_28NM_PLL_REG0 */
  138. reg = readl(base + PHY_28NM_PLL_REG0) &
  139. ~(PHY_28NM_PLL_SELLPFR_MASK | PHY_28NM_PLL_FBDIV_MASK
  140. | PHY_28NM_PLL_ICP_MASK | PHY_28NM_PLL_REFDIV_MASK);
  141. writel(reg | (0x1 << PHY_28NM_PLL_SELLPFR_SHIFT
  142. | 0xf0 << PHY_28NM_PLL_FBDIV_SHIFT
  143. | 0x3 << PHY_28NM_PLL_ICP_SHIFT
  144. | 0xd << PHY_28NM_PLL_REFDIV_SHIFT),
  145. base + PHY_28NM_PLL_REG0);
  146. /* PHY_28NM_PLL_REG1 */
  147. reg = readl(base + PHY_28NM_PLL_REG1);
  148. writel(reg | PHY_28NM_PLL_PU_PLL | PHY_28NM_PLL_PU_BY_REG,
  149. base + PHY_28NM_PLL_REG1);
  150. /* PHY_28NM_TX_REG0 */
  151. reg = readl(base + PHY_28NM_TX_REG0) & ~PHY_28NM_TX_AMP_MASK;
  152. writel(reg | PHY_28NM_TX_PU_BY_REG | 0x3 << PHY_28NM_TX_AMP_SHIFT |
  153. PHY_28NM_TX_PU_ANA,
  154. base + PHY_28NM_TX_REG0);
  155. /* PHY_28NM_RX_REG0 */
  156. reg = readl(base + PHY_28NM_RX_REG0) & ~PHY_28NM_RX_SQ_THRESH_MASK;
  157. writel(reg | 0xa << PHY_28NM_RX_SQ_THRESH_SHIFT,
  158. base + PHY_28NM_RX_REG0);
  159. /* PHY_28NM_DIG_REG0 */
  160. reg = readl(base + PHY_28NM_DIG_REG0) &
  161. ~(PHY_28NM_DIG_BITSTAFFING_ERR | PHY_28NM_DIG_SYNC_ERR |
  162. PHY_28NM_DIG_SQ_FILT_MASK | PHY_28NM_DIG_SQ_BLK_MASK |
  163. PHY_28NM_DIG_SYNC_NUM_MASK);
  164. writel(reg | (0x1 << PHY_28NM_DIG_SYNC_NUM_SHIFT |
  165. PHY_28NM_PLL_LOCK_BYPASS),
  166. base + PHY_28NM_DIG_REG0);
  167. /* PHY_28NM_OTG_REG */
  168. reg = readl(base + PHY_28NM_OTG_REG) | PHY_28NM_OTG_PU_OTG;
  169. writel(reg & ~PHY_28NM_OTG_CONTROL_BY_PIN, base + PHY_28NM_OTG_REG);
  170. /*
  171. * Calibration Timing
  172. * ____________________________
  173. * CAL START ___|
  174. * ____________________
  175. * CAL_DONE ___________|
  176. * | 400us |
  177. */
  178. /* Make sure PHY Calibration is ready */
  179. if (!wait_for_reg(base + PHY_28NM_CAL_REG,
  180. PHY_28NM_PLL_PLLCAL_DONE | PHY_28NM_PLL_IMPCAL_DONE,
  181. HZ / 10)) {
  182. dev_warn(&pdev->dev, "USB PHY PLL calibrate not done after 100mS.");
  183. ret = -ETIMEDOUT;
  184. goto err_clk;
  185. }
  186. if (!wait_for_reg(base + PHY_28NM_RX_REG1,
  187. PHY_28NM_RX_SQCAL_DONE, HZ / 10)) {
  188. dev_warn(&pdev->dev, "USB PHY RX SQ calibrate not done after 100mS.");
  189. ret = -ETIMEDOUT;
  190. goto err_clk;
  191. }
  192. /* Make sure PHY PLL is ready */
  193. if (!wait_for_reg(base + PHY_28NM_PLL_REG0,
  194. PHY_28NM_PLL_READY, HZ / 10)) {
  195. dev_warn(&pdev->dev, "PLL_READY not set after 100mS.");
  196. ret = -ETIMEDOUT;
  197. goto err_clk;
  198. }
  199. return 0;
  200. err_clk:
  201. clk_disable_unprepare(mv_phy->clk);
  202. return ret;
  203. }
  204. static int mv_usb2_phy_28nm_power_on(struct phy *phy)
  205. {
  206. struct mv_usb2_phy *mv_phy = phy_get_drvdata(phy);
  207. void __iomem *base = mv_phy->base;
  208. writel(readl(base + PHY_28NM_CTRL_REG3) |
  209. (PHY_28NM_CTRL3_OVERWRITE | PHY_28NM_CTRL3_VBUS_VALID |
  210. PHY_28NM_CTRL3_AVALID | PHY_28NM_CTRL3_BVALID),
  211. base + PHY_28NM_CTRL_REG3);
  212. return 0;
  213. }
  214. static int mv_usb2_phy_28nm_power_off(struct phy *phy)
  215. {
  216. struct mv_usb2_phy *mv_phy = phy_get_drvdata(phy);
  217. void __iomem *base = mv_phy->base;
  218. writel(readl(base + PHY_28NM_CTRL_REG3) |
  219. ~(PHY_28NM_CTRL3_OVERWRITE | PHY_28NM_CTRL3_VBUS_VALID
  220. | PHY_28NM_CTRL3_AVALID | PHY_28NM_CTRL3_BVALID),
  221. base + PHY_28NM_CTRL_REG3);
  222. return 0;
  223. }
  224. static int mv_usb2_phy_28nm_exit(struct phy *phy)
  225. {
  226. struct mv_usb2_phy *mv_phy = phy_get_drvdata(phy);
  227. void __iomem *base = mv_phy->base;
  228. unsigned int val;
  229. val = readw(base + PHY_28NM_PLL_REG1);
  230. val &= ~PHY_28NM_PLL_PU_PLL;
  231. writew(val, base + PHY_28NM_PLL_REG1);
  232. /* power down PHY Analog part */
  233. val = readw(base + PHY_28NM_TX_REG0);
  234. val &= ~PHY_28NM_TX_PU_ANA;
  235. writew(val, base + PHY_28NM_TX_REG0);
  236. /* power down PHY OTG part */
  237. val = readw(base + PHY_28NM_OTG_REG);
  238. val &= ~PHY_28NM_OTG_PU_OTG;
  239. writew(val, base + PHY_28NM_OTG_REG);
  240. clk_disable_unprepare(mv_phy->clk);
  241. return 0;
  242. }
  243. static const struct phy_ops usb_ops = {
  244. .init = mv_usb2_phy_28nm_init,
  245. .power_on = mv_usb2_phy_28nm_power_on,
  246. .power_off = mv_usb2_phy_28nm_power_off,
  247. .exit = mv_usb2_phy_28nm_exit,
  248. .owner = THIS_MODULE,
  249. };
  250. static int mv_usb2_phy_probe(struct platform_device *pdev)
  251. {
  252. struct phy_provider *phy_provider;
  253. struct mv_usb2_phy *mv_phy;
  254. struct resource *r;
  255. mv_phy = devm_kzalloc(&pdev->dev, sizeof(*mv_phy), GFP_KERNEL);
  256. if (!mv_phy)
  257. return -ENOMEM;
  258. mv_phy->pdev = pdev;
  259. mv_phy->clk = devm_clk_get(&pdev->dev, NULL);
  260. if (IS_ERR(mv_phy->clk)) {
  261. dev_err(&pdev->dev, "failed to get clock.\n");
  262. return PTR_ERR(mv_phy->clk);
  263. }
  264. r = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  265. mv_phy->base = devm_ioremap_resource(&pdev->dev, r);
  266. if (IS_ERR(mv_phy->base))
  267. return PTR_ERR(mv_phy->base);
  268. mv_phy->phy = devm_phy_create(&pdev->dev, pdev->dev.of_node, &usb_ops);
  269. if (IS_ERR(mv_phy->phy))
  270. return PTR_ERR(mv_phy->phy);
  271. phy_set_drvdata(mv_phy->phy, mv_phy);
  272. phy_provider = devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
  273. return PTR_ERR_OR_ZERO(phy_provider);
  274. }
  275. static const struct of_device_id mv_usbphy_dt_match[] = {
  276. { .compatible = "marvell,pxa1928-usb-phy", },
  277. {},
  278. };
  279. MODULE_DEVICE_TABLE(of, mv_usbphy_dt_match);
  280. static struct platform_driver mv_usb2_phy_driver = {
  281. .probe = mv_usb2_phy_probe,
  282. .driver = {
  283. .name = "mv-usb2-phy",
  284. .of_match_table = of_match_ptr(mv_usbphy_dt_match),
  285. },
  286. };
  287. module_platform_driver(mv_usb2_phy_driver);
  288. MODULE_AUTHOR("Rob Herring <robh@kernel.org>");
  289. MODULE_DESCRIPTION("Marvell USB2 phy driver");
  290. MODULE_LICENSE("GPL v2");