phy-bcm-ns-usb2.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  1. /*
  2. * Broadcom Northstar USB 2.0 PHY Driver
  3. *
  4. * Copyright (C) 2016 Rafał Miłecki <zajec5@gmail.com>
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. *
  10. */
  11. #include <linux/bcma/bcma.h>
  12. #include <linux/clk.h>
  13. #include <linux/delay.h>
  14. #include <linux/err.h>
  15. #include <linux/module.h>
  16. #include <linux/of_address.h>
  17. #include <linux/of_platform.h>
  18. #include <linux/phy/phy.h>
  19. #include <linux/platform_device.h>
  20. #include <linux/slab.h>
  21. struct bcm_ns_usb2 {
  22. struct device *dev;
  23. struct clk *ref_clk;
  24. struct phy *phy;
  25. void __iomem *dmu;
  26. };
  27. static int bcm_ns_usb2_phy_init(struct phy *phy)
  28. {
  29. struct bcm_ns_usb2 *usb2 = phy_get_drvdata(phy);
  30. struct device *dev = usb2->dev;
  31. void __iomem *dmu = usb2->dmu;
  32. u32 ref_clk_rate, usb2ctl, usb_pll_ndiv, usb_pll_pdiv;
  33. int err = 0;
  34. err = clk_prepare_enable(usb2->ref_clk);
  35. if (err < 0) {
  36. dev_err(dev, "Failed to prepare ref clock: %d\n", err);
  37. goto err_out;
  38. }
  39. ref_clk_rate = clk_get_rate(usb2->ref_clk);
  40. if (!ref_clk_rate) {
  41. dev_err(dev, "Failed to get ref clock rate\n");
  42. err = -EINVAL;
  43. goto err_clk_off;
  44. }
  45. usb2ctl = readl(dmu + BCMA_DMU_CRU_USB2_CONTROL);
  46. if (usb2ctl & BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_MASK) {
  47. usb_pll_pdiv = usb2ctl;
  48. usb_pll_pdiv &= BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_MASK;
  49. usb_pll_pdiv >>= BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_SHIFT;
  50. } else {
  51. usb_pll_pdiv = 1 << 3;
  52. }
  53. /* Calculate ndiv based on a solid 1920 MHz that is for USB2 PHY */
  54. usb_pll_ndiv = (1920000000 * usb_pll_pdiv) / ref_clk_rate;
  55. /* Unlock DMU PLL settings with some magic value */
  56. writel(0x0000ea68, dmu + BCMA_DMU_CRU_CLKSET_KEY);
  57. /* Write USB 2.0 PLL control setting */
  58. usb2ctl &= ~BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_NDIV_MASK;
  59. usb2ctl |= usb_pll_ndiv << BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_NDIV_SHIFT;
  60. writel(usb2ctl, dmu + BCMA_DMU_CRU_USB2_CONTROL);
  61. /* Lock DMU PLL settings */
  62. writel(0x00000000, dmu + BCMA_DMU_CRU_CLKSET_KEY);
  63. err_clk_off:
  64. clk_disable_unprepare(usb2->ref_clk);
  65. err_out:
  66. return err;
  67. }
  68. static const struct phy_ops ops = {
  69. .init = bcm_ns_usb2_phy_init,
  70. .owner = THIS_MODULE,
  71. };
  72. static int bcm_ns_usb2_probe(struct platform_device *pdev)
  73. {
  74. struct device *dev = &pdev->dev;
  75. struct bcm_ns_usb2 *usb2;
  76. struct resource *res;
  77. struct phy_provider *phy_provider;
  78. usb2 = devm_kzalloc(&pdev->dev, sizeof(*usb2), GFP_KERNEL);
  79. if (!usb2)
  80. return -ENOMEM;
  81. usb2->dev = dev;
  82. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmu");
  83. usb2->dmu = devm_ioremap_resource(dev, res);
  84. if (IS_ERR(usb2->dmu)) {
  85. dev_err(dev, "Failed to map DMU regs\n");
  86. return PTR_ERR(usb2->dmu);
  87. }
  88. usb2->ref_clk = devm_clk_get(dev, "phy-ref-clk");
  89. if (IS_ERR(usb2->ref_clk)) {
  90. dev_err(dev, "Clock not defined\n");
  91. return PTR_ERR(usb2->ref_clk);
  92. }
  93. usb2->phy = devm_phy_create(dev, NULL, &ops);
  94. if (IS_ERR(usb2->phy))
  95. return PTR_ERR(usb2->phy);
  96. phy_set_drvdata(usb2->phy, usb2);
  97. platform_set_drvdata(pdev, usb2);
  98. phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  99. return PTR_ERR_OR_ZERO(phy_provider);
  100. }
  101. static const struct of_device_id bcm_ns_usb2_id_table[] = {
  102. { .compatible = "brcm,ns-usb2-phy", },
  103. {},
  104. };
  105. MODULE_DEVICE_TABLE(of, bcm_ns_usb2_id_table);
  106. static struct platform_driver bcm_ns_usb2_driver = {
  107. .probe = bcm_ns_usb2_probe,
  108. .driver = {
  109. .name = "bcm_ns_usb2",
  110. .of_match_table = bcm_ns_usb2_id_table,
  111. },
  112. };
  113. module_platform_driver(bcm_ns_usb2_driver);
  114. MODULE_LICENSE("GPL v2");