070-0001-phy-bcm-ns-usb2-new-driver-for-USB-2.0-PHY-on-Norths.patch 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. From d3feb406733544dbf0e239ef945a09decdceac88 Mon Sep 17 00:00:00 2001
  2. From: =?UTF-8?q?Rafa=C5=82=20Mi=C5=82ecki?= <zajec5@gmail.com>
  3. Date: Thu, 14 Apr 2016 11:37:43 +0200
  4. Subject: [PATCH] phy: bcm-ns-usb2: new driver for USB 2.0 PHY on Northstar
  5. MIME-Version: 1.0
  6. Content-Type: text/plain; charset=UTF-8
  7. Content-Transfer-Encoding: 8bit
  8. Northstar is a family of SoCs used in home routers. They have USB 2.0
  9. and 3.0 controllers with PHYs that need to be properly initialized.
  10. This driver provides PHY init support in a generic way and can be bound
  11. with an EHCI controller driver.
  12. There are (just a few) registers being defined in bcma header. It's
  13. because DMU/CRU registers will be also needed in other drivers. We will
  14. need them e.g. in PCIe controller/PHY driver and at some point probably
  15. in clock driver for BCM53573 chipset. By using include/linux/bcma/ we
  16. avoid code duplication.
  17. Signed-off-by: Rafał Miłecki <zajec5@gmail.com>
  18. Signed-off-by: Kishon Vijay Abraham I <kishon@ti.com>
  19. ---
  20. .../devicetree/bindings/phy/bcm-ns-usb2-phy.txt | 21 ++++
  21. drivers/phy/Kconfig | 9 ++
  22. drivers/phy/Makefile | 1 +
  23. drivers/phy/phy-bcm-ns-usb2.c | 137 +++++++++++++++++++++
  24. include/linux/bcma/bcma.h | 1 +
  25. include/linux/bcma/bcma_driver_arm_c9.h | 15 +++
  26. 6 files changed, 184 insertions(+)
  27. create mode 100644 Documentation/devicetree/bindings/phy/bcm-ns-usb2-phy.txt
  28. create mode 100644 drivers/phy/phy-bcm-ns-usb2.c
  29. create mode 100644 include/linux/bcma/bcma_driver_arm_c9.h
  30. --- /dev/null
  31. +++ b/Documentation/devicetree/bindings/phy/bcm-ns-usb2-phy.txt
  32. @@ -0,0 +1,21 @@
  33. +Driver for Broadcom Northstar USB 2.0 PHY
  34. +
  35. +Required properties:
  36. +- compatible: brcm,ns-usb2-phy
  37. +- reg: iomem address range of DMU (Device Management Unit)
  38. +- reg-names: "dmu", the only needed & supported reg right now
  39. +- clocks: USB PHY reference clock
  40. +- clock-names: "phy-ref-clk", the only needed & supported clock right now
  41. +
  42. +To initialize USB 2.0 PHY driver needs to setup PLL correctly. To do this it
  43. +requires passing phandle to the USB PHY reference clock.
  44. +
  45. +Example:
  46. + usb2-phy {
  47. + compatible = "brcm,ns-usb2-phy";
  48. + reg = <0x1800c000 0x1000>;
  49. + reg-names = "dmu";
  50. + #phy-cells = <0>;
  51. + clocks = <&genpll BCM_NSP_GENPLL_USB_PHY_REF_CLK>;
  52. + clock-names = "phy-ref-clk";
  53. + };
  54. --- a/drivers/phy/Kconfig
  55. +++ b/drivers/phy/Kconfig
  56. @@ -15,6 +15,15 @@ config GENERIC_PHY
  57. phy users can obtain reference to the PHY. All the users of this
  58. framework should select this config.
  59. +config PHY_BCM_NS_USB2
  60. + tristate "Broadcom Northstar USB 2.0 PHY Driver"
  61. + depends on ARCH_BCM_IPROC || COMPILE_TEST
  62. + depends on HAS_IOMEM && OF
  63. + select GENERIC_PHY
  64. + help
  65. + Enable this to support Broadcom USB 2.0 PHY connected to the USB
  66. + controller on Northstar family.
  67. +
  68. config PHY_BERLIN_USB
  69. tristate "Marvell Berlin USB PHY Driver"
  70. depends on ARCH_BERLIN && RESET_CONTROLLER && HAS_IOMEM && OF
  71. --- a/drivers/phy/Makefile
  72. +++ b/drivers/phy/Makefile
  73. @@ -3,6 +3,7 @@
  74. #
  75. obj-$(CONFIG_GENERIC_PHY) += phy-core.o
  76. +obj-$(CONFIG_PHY_BCM_NS_USB2) += phy-bcm-ns-usb2.o
  77. obj-$(CONFIG_PHY_BERLIN_USB) += phy-berlin-usb.o
  78. obj-$(CONFIG_PHY_BERLIN_SATA) += phy-berlin-sata.o
  79. obj-$(CONFIG_PHY_DM816X_USB) += phy-dm816x-usb.o
  80. --- /dev/null
  81. +++ b/drivers/phy/phy-bcm-ns-usb2.c
  82. @@ -0,0 +1,137 @@
  83. +/*
  84. + * Broadcom Northstar USB 2.0 PHY Driver
  85. + *
  86. + * Copyright (C) 2016 Rafał Miłecki <zajec5@gmail.com>
  87. + *
  88. + * This program is free software; you can redistribute it and/or modify
  89. + * it under the terms of the GNU General Public License version 2 as
  90. + * published by the Free Software Foundation.
  91. + *
  92. + */
  93. +
  94. +#include <linux/bcma/bcma.h>
  95. +#include <linux/clk.h>
  96. +#include <linux/delay.h>
  97. +#include <linux/err.h>
  98. +#include <linux/module.h>
  99. +#include <linux/of_address.h>
  100. +#include <linux/of_platform.h>
  101. +#include <linux/phy/phy.h>
  102. +#include <linux/platform_device.h>
  103. +#include <linux/slab.h>
  104. +
  105. +struct bcm_ns_usb2 {
  106. + struct device *dev;
  107. + struct clk *ref_clk;
  108. + struct phy *phy;
  109. + void __iomem *dmu;
  110. +};
  111. +
  112. +static int bcm_ns_usb2_phy_init(struct phy *phy)
  113. +{
  114. + struct bcm_ns_usb2 *usb2 = phy_get_drvdata(phy);
  115. + struct device *dev = usb2->dev;
  116. + void __iomem *dmu = usb2->dmu;
  117. + u32 ref_clk_rate, usb2ctl, usb_pll_ndiv, usb_pll_pdiv;
  118. + int err = 0;
  119. +
  120. + err = clk_prepare_enable(usb2->ref_clk);
  121. + if (err < 0) {
  122. + dev_err(dev, "Failed to prepare ref clock: %d\n", err);
  123. + goto err_out;
  124. + }
  125. +
  126. + ref_clk_rate = clk_get_rate(usb2->ref_clk);
  127. + if (!ref_clk_rate) {
  128. + dev_err(dev, "Failed to get ref clock rate\n");
  129. + err = -EINVAL;
  130. + goto err_clk_off;
  131. + }
  132. +
  133. + usb2ctl = readl(dmu + BCMA_DMU_CRU_USB2_CONTROL);
  134. +
  135. + if (usb2ctl & BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_MASK) {
  136. + usb_pll_pdiv = usb2ctl;
  137. + usb_pll_pdiv &= BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_MASK;
  138. + usb_pll_pdiv >>= BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_SHIFT;
  139. + } else {
  140. + usb_pll_pdiv = 1 << 3;
  141. + }
  142. +
  143. + /* Calculate ndiv based on a solid 1920 MHz that is for USB2 PHY */
  144. + usb_pll_ndiv = (1920000000 * usb_pll_pdiv) / ref_clk_rate;
  145. +
  146. + /* Unlock DMU PLL settings with some magic value */
  147. + writel(0x0000ea68, dmu + BCMA_DMU_CRU_CLKSET_KEY);
  148. +
  149. + /* Write USB 2.0 PLL control setting */
  150. + usb2ctl &= ~BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_NDIV_MASK;
  151. + usb2ctl |= usb_pll_ndiv << BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_NDIV_SHIFT;
  152. + writel(usb2ctl, dmu + BCMA_DMU_CRU_USB2_CONTROL);
  153. +
  154. + /* Lock DMU PLL settings */
  155. + writel(0x00000000, dmu + BCMA_DMU_CRU_CLKSET_KEY);
  156. +
  157. +err_clk_off:
  158. + clk_disable_unprepare(usb2->ref_clk);
  159. +err_out:
  160. + return err;
  161. +}
  162. +
  163. +static const struct phy_ops ops = {
  164. + .init = bcm_ns_usb2_phy_init,
  165. + .owner = THIS_MODULE,
  166. +};
  167. +
  168. +static int bcm_ns_usb2_probe(struct platform_device *pdev)
  169. +{
  170. + struct device *dev = &pdev->dev;
  171. + struct bcm_ns_usb2 *usb2;
  172. + struct resource *res;
  173. + struct phy_provider *phy_provider;
  174. +
  175. + usb2 = devm_kzalloc(&pdev->dev, sizeof(*usb2), GFP_KERNEL);
  176. + if (!usb2)
  177. + return -ENOMEM;
  178. + usb2->dev = dev;
  179. +
  180. + res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "dmu");
  181. + usb2->dmu = devm_ioremap_resource(dev, res);
  182. + if (IS_ERR(usb2->dmu)) {
  183. + dev_err(dev, "Failed to map DMU regs\n");
  184. + return PTR_ERR(usb2->dmu);
  185. + }
  186. +
  187. + usb2->ref_clk = devm_clk_get(dev, "phy-ref-clk");
  188. + if (IS_ERR(usb2->ref_clk)) {
  189. + dev_err(dev, "Clock not defined\n");
  190. + return PTR_ERR(usb2->ref_clk);
  191. + }
  192. +
  193. + usb2->phy = devm_phy_create(dev, NULL, &ops);
  194. + if (IS_ERR(dev))
  195. + return PTR_ERR(dev);
  196. +
  197. + phy_set_drvdata(usb2->phy, usb2);
  198. + platform_set_drvdata(pdev, usb2);
  199. +
  200. + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  201. + return PTR_ERR_OR_ZERO(phy_provider);
  202. +}
  203. +
  204. +static const struct of_device_id bcm_ns_usb2_id_table[] = {
  205. + { .compatible = "brcm,ns-usb2-phy", },
  206. + {},
  207. +};
  208. +MODULE_DEVICE_TABLE(of, bcm_ns_usb2_id_table);
  209. +
  210. +static struct platform_driver bcm_ns_usb2_driver = {
  211. + .probe = bcm_ns_usb2_probe,
  212. + .driver = {
  213. + .name = "bcm_ns_usb2",
  214. + .of_match_table = bcm_ns_usb2_id_table,
  215. + },
  216. +};
  217. +module_platform_driver(bcm_ns_usb2_driver);
  218. +
  219. +MODULE_LICENSE("GPL v2");
  220. --- a/include/linux/bcma/bcma.h
  221. +++ b/include/linux/bcma/bcma.h
  222. @@ -4,6 +4,7 @@
  223. #include <linux/pci.h>
  224. #include <linux/mod_devicetable.h>
  225. +#include <linux/bcma/bcma_driver_arm_c9.h>
  226. #include <linux/bcma/bcma_driver_chipcommon.h>
  227. #include <linux/bcma/bcma_driver_pci.h>
  228. #include <linux/bcma/bcma_driver_pcie2.h>
  229. --- /dev/null
  230. +++ b/include/linux/bcma/bcma_driver_arm_c9.h
  231. @@ -0,0 +1,15 @@
  232. +#ifndef LINUX_BCMA_DRIVER_ARM_C9_H_
  233. +#define LINUX_BCMA_DRIVER_ARM_C9_H_
  234. +
  235. +/* DMU (Device Management Unit) */
  236. +#define BCMA_DMU_CRU_USB2_CONTROL 0x0164
  237. +#define BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_NDIV_MASK 0x00000FFC
  238. +#define BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_NDIV_SHIFT 2
  239. +#define BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_MASK 0x00007000
  240. +#define BCMA_DMU_CRU_USB2_CONTROL_USB_PLL_PDIV_SHIFT 12
  241. +#define BCMA_DMU_CRU_CLKSET_KEY 0x0180
  242. +#define BCMA_DMU_CRU_STRAPS_CTRL 0x02A0
  243. +#define BCMA_DMU_CRU_STRAPS_CTRL_USB3 0x00000010
  244. +#define BCMA_DMU_CRU_STRAPS_CTRL_4BYTE 0x00008000
  245. +
  246. +#endif /* LINUX_BCMA_DRIVER_ARM_C9_H_ */