phy-berlin-usb.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2014 Marvell Technology Group Ltd.
  3. *
  4. * Antoine Tenart <antoine.tenart@free-electrons.com>
  5. * Jisheng Zhang <jszhang@marvell.com>
  6. *
  7. * This file is licensed under the terms of the GNU General Public
  8. * License version 2. This program is licensed "as is" without any
  9. * warranty of any kind, whether express or implied.
  10. */
  11. #include <linux/gpio.h>
  12. #include <linux/io.h>
  13. #include <linux/module.h>
  14. #include <linux/of_device.h>
  15. #include <linux/of_gpio.h>
  16. #include <linux/phy/phy.h>
  17. #include <linux/platform_device.h>
  18. #include <linux/reset.h>
  19. #define USB_PHY_PLL 0x04
  20. #define USB_PHY_PLL_CONTROL 0x08
  21. #define USB_PHY_TX_CTRL0 0x10
  22. #define USB_PHY_TX_CTRL1 0x14
  23. #define USB_PHY_TX_CTRL2 0x18
  24. #define USB_PHY_RX_CTRL 0x20
  25. #define USB_PHY_ANALOG 0x34
  26. /* USB_PHY_PLL */
  27. #define CLK_REF_DIV(x) ((x) << 4)
  28. #define FEEDBACK_CLK_DIV(x) ((x) << 8)
  29. /* USB_PHY_PLL_CONTROL */
  30. #define CLK_STABLE BIT(0)
  31. #define PLL_CTRL_PIN BIT(1)
  32. #define PLL_CTRL_REG BIT(2)
  33. #define PLL_ON BIT(3)
  34. #define PHASE_OFF_TOL_125 (0x0 << 5)
  35. #define PHASE_OFF_TOL_250 BIT(5)
  36. #define KVC0_CALIB (0x0 << 9)
  37. #define KVC0_REG_CTRL BIT(9)
  38. #define KVC0_HIGH (0x0 << 10)
  39. #define KVC0_LOW (0x3 << 10)
  40. #define CLK_BLK_EN BIT(13)
  41. /* USB_PHY_TX_CTRL0 */
  42. #define EXT_HS_RCAL_EN BIT(3)
  43. #define EXT_FS_RCAL_EN BIT(4)
  44. #define IMPCAL_VTH_DIV(x) ((x) << 5)
  45. #define EXT_RS_RCAL_DIV(x) ((x) << 8)
  46. #define EXT_FS_RCAL_DIV(x) ((x) << 12)
  47. /* USB_PHY_TX_CTRL1 */
  48. #define TX_VDD15_14 (0x0 << 4)
  49. #define TX_VDD15_15 BIT(4)
  50. #define TX_VDD15_16 (0x2 << 4)
  51. #define TX_VDD15_17 (0x3 << 4)
  52. #define TX_VDD12_VDD (0x0 << 6)
  53. #define TX_VDD12_11 BIT(6)
  54. #define TX_VDD12_12 (0x2 << 6)
  55. #define TX_VDD12_13 (0x3 << 6)
  56. #define LOW_VDD_EN BIT(8)
  57. #define TX_OUT_AMP(x) ((x) << 9)
  58. /* USB_PHY_TX_CTRL2 */
  59. #define TX_CHAN_CTRL_REG(x) ((x) << 0)
  60. #define DRV_SLEWRATE(x) ((x) << 4)
  61. #define IMP_CAL_FS_HS_DLY_0 (0x0 << 6)
  62. #define IMP_CAL_FS_HS_DLY_1 BIT(6)
  63. #define IMP_CAL_FS_HS_DLY_2 (0x2 << 6)
  64. #define IMP_CAL_FS_HS_DLY_3 (0x3 << 6)
  65. #define FS_DRV_EN_MASK(x) ((x) << 8)
  66. #define HS_DRV_EN_MASK(x) ((x) << 12)
  67. /* USB_PHY_RX_CTRL */
  68. #define PHASE_FREEZE_DLY_2_CL (0x0 << 0)
  69. #define PHASE_FREEZE_DLY_4_CL BIT(0)
  70. #define ACK_LENGTH_8_CL (0x0 << 2)
  71. #define ACK_LENGTH_12_CL BIT(2)
  72. #define ACK_LENGTH_16_CL (0x2 << 2)
  73. #define ACK_LENGTH_20_CL (0x3 << 2)
  74. #define SQ_LENGTH_3 (0x0 << 4)
  75. #define SQ_LENGTH_6 BIT(4)
  76. #define SQ_LENGTH_9 (0x2 << 4)
  77. #define SQ_LENGTH_12 (0x3 << 4)
  78. #define DISCON_THRESHOLD_260 (0x0 << 6)
  79. #define DISCON_THRESHOLD_270 BIT(6)
  80. #define DISCON_THRESHOLD_280 (0x2 << 6)
  81. #define DISCON_THRESHOLD_290 (0x3 << 6)
  82. #define SQ_THRESHOLD(x) ((x) << 8)
  83. #define LPF_COEF(x) ((x) << 12)
  84. #define INTPL_CUR_10 (0x0 << 14)
  85. #define INTPL_CUR_20 BIT(14)
  86. #define INTPL_CUR_30 (0x2 << 14)
  87. #define INTPL_CUR_40 (0x3 << 14)
  88. /* USB_PHY_ANALOG */
  89. #define ANA_PWR_UP BIT(1)
  90. #define ANA_PWR_DOWN BIT(2)
  91. #define V2I_VCO_RATIO(x) ((x) << 7)
  92. #define R_ROTATE_90 (0x0 << 10)
  93. #define R_ROTATE_0 BIT(10)
  94. #define MODE_TEST_EN BIT(11)
  95. #define ANA_TEST_DC_CTRL(x) ((x) << 12)
  96. #define to_phy_berlin_usb_priv(p) \
  97. container_of((p), struct phy_berlin_usb_priv, phy)
  98. static const u32 phy_berlin_pll_dividers[] = {
  99. /* Berlin 2 */
  100. CLK_REF_DIV(0xc) | FEEDBACK_CLK_DIV(0x54),
  101. /* Berlin 2CD */
  102. CLK_REF_DIV(0x6) | FEEDBACK_CLK_DIV(0x55),
  103. };
  104. struct phy_berlin_usb_priv {
  105. void __iomem *base;
  106. struct phy *phy;
  107. struct reset_control *rst_ctrl;
  108. u32 pll_divider;
  109. };
  110. static int phy_berlin_usb_power_on(struct phy *phy)
  111. {
  112. struct phy_berlin_usb_priv *priv = dev_get_drvdata(phy->dev.parent);
  113. reset_control_reset(priv->rst_ctrl);
  114. writel(priv->pll_divider,
  115. priv->base + USB_PHY_PLL);
  116. writel(CLK_STABLE | PLL_CTRL_REG | PHASE_OFF_TOL_250 | KVC0_REG_CTRL |
  117. CLK_BLK_EN, priv->base + USB_PHY_PLL_CONTROL);
  118. writel(V2I_VCO_RATIO(0x5) | R_ROTATE_0 | ANA_TEST_DC_CTRL(0x5),
  119. priv->base + USB_PHY_ANALOG);
  120. writel(PHASE_FREEZE_DLY_4_CL | ACK_LENGTH_16_CL | SQ_LENGTH_12 |
  121. DISCON_THRESHOLD_260 | SQ_THRESHOLD(0xa) | LPF_COEF(0x2) |
  122. INTPL_CUR_30, priv->base + USB_PHY_RX_CTRL);
  123. writel(TX_VDD12_13 | TX_OUT_AMP(0x3), priv->base + USB_PHY_TX_CTRL1);
  124. writel(EXT_HS_RCAL_EN | IMPCAL_VTH_DIV(0x3) | EXT_RS_RCAL_DIV(0x4),
  125. priv->base + USB_PHY_TX_CTRL0);
  126. writel(EXT_HS_RCAL_EN | IMPCAL_VTH_DIV(0x3) | EXT_RS_RCAL_DIV(0x4) |
  127. EXT_FS_RCAL_DIV(0x2), priv->base + USB_PHY_TX_CTRL0);
  128. writel(EXT_HS_RCAL_EN | IMPCAL_VTH_DIV(0x3) | EXT_RS_RCAL_DIV(0x4),
  129. priv->base + USB_PHY_TX_CTRL0);
  130. writel(TX_CHAN_CTRL_REG(0xf) | DRV_SLEWRATE(0x3) | IMP_CAL_FS_HS_DLY_3 |
  131. FS_DRV_EN_MASK(0xd), priv->base + USB_PHY_TX_CTRL2);
  132. return 0;
  133. }
  134. static struct phy_ops phy_berlin_usb_ops = {
  135. .power_on = phy_berlin_usb_power_on,
  136. .owner = THIS_MODULE,
  137. };
  138. static const struct of_device_id phy_berlin_sata_of_match[] = {
  139. {
  140. .compatible = "marvell,berlin2-usb-phy",
  141. .data = &phy_berlin_pll_dividers[0],
  142. },
  143. {
  144. .compatible = "marvell,berlin2cd-usb-phy",
  145. .data = &phy_berlin_pll_dividers[1],
  146. },
  147. { },
  148. };
  149. MODULE_DEVICE_TABLE(of, phy_berlin_sata_of_match);
  150. static int phy_berlin_usb_probe(struct platform_device *pdev)
  151. {
  152. const struct of_device_id *match =
  153. of_match_device(phy_berlin_sata_of_match, &pdev->dev);
  154. struct phy_berlin_usb_priv *priv;
  155. struct resource *res;
  156. struct phy_provider *phy_provider;
  157. priv = devm_kzalloc(&pdev->dev, sizeof(*priv), GFP_KERNEL);
  158. if (!priv)
  159. return -ENOMEM;
  160. res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  161. priv->base = devm_ioremap_resource(&pdev->dev, res);
  162. if (IS_ERR(priv->base))
  163. return PTR_ERR(priv->base);
  164. priv->rst_ctrl = devm_reset_control_get(&pdev->dev, NULL);
  165. if (IS_ERR(priv->rst_ctrl))
  166. return PTR_ERR(priv->rst_ctrl);
  167. priv->pll_divider = *((u32 *)match->data);
  168. priv->phy = devm_phy_create(&pdev->dev, NULL, &phy_berlin_usb_ops);
  169. if (IS_ERR(priv->phy)) {
  170. dev_err(&pdev->dev, "failed to create PHY\n");
  171. return PTR_ERR(priv->phy);
  172. }
  173. platform_set_drvdata(pdev, priv);
  174. phy_provider =
  175. devm_of_phy_provider_register(&pdev->dev, of_phy_simple_xlate);
  176. if (IS_ERR(phy_provider))
  177. return PTR_ERR(phy_provider);
  178. return 0;
  179. }
  180. static struct platform_driver phy_berlin_usb_driver = {
  181. .probe = phy_berlin_usb_probe,
  182. .driver = {
  183. .name = "phy-berlin-usb",
  184. .owner = THIS_MODULE,
  185. .of_match_table = phy_berlin_sata_of_match,
  186. },
  187. };
  188. module_platform_driver(phy_berlin_usb_driver);
  189. MODULE_AUTHOR("Antoine Tenart <antoine.tenart@free-electrons.com>");
  190. MODULE_DESCRIPTION("Marvell Berlin PHY driver for USB");
  191. MODULE_LICENSE("GPL");