0037-USB-phy-add-ralink-SoC-driver.patch 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. --- a/drivers/phy/Kconfig
  2. +++ b/drivers/phy/Kconfig
  3. @@ -239,6 +239,11 @@ config PHY_XGENE
  4. help
  5. This option enables support for APM X-Gene SoC multi-purpose PHY.
  6. +config PHY_RALINK_USB
  7. + tristate "Ralink USB PHY driver"
  8. + select GENERIC_PHY
  9. + depends on RALINK
  10. +
  11. config PHY_STIH407_USB
  12. tristate "STMicroelectronics USB2 picoPHY driver for STiH407 family"
  13. depends on RESET_CONTROLLER
  14. --- a/drivers/phy/Makefile
  15. +++ b/drivers/phy/Makefile
  16. @@ -31,3 +31,4 @@ obj-$(CONFIG_PHY_ST_SPEAR1340_MIPHY) +=
  17. obj-$(CONFIG_PHY_XGENE) += phy-xgene.o
  18. obj-$(CONFIG_PHY_STIH407_USB) += phy-stih407-usb.o
  19. obj-$(CONFIG_PHY_STIH41X_USB) += phy-stih41x-usb.o
  20. +obj-$(CONFIG_PHY_RALINK_USB) += phy-ralink-usb.o
  21. --- /dev/null
  22. +++ b/drivers/phy/phy-ralink-usb.c
  23. @@ -0,0 +1,228 @@
  24. +/*
  25. + * Allwinner ralink USB phy driver
  26. + *
  27. + * Copyright (C) 2016 John Crispin <blogic@openwrt.org>
  28. + *
  29. + * Based on code from
  30. + * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  31. + *
  32. + * This program is free software; you can redistribute it and/or modify
  33. + * it under the terms of the GNU General Public License as published by
  34. + * the Free Software Foundation; either version 2 of the License, or
  35. + * (at your option) any later version.
  36. + *
  37. + * This program is distributed in the hope that it will be useful,
  38. + * but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  40. + * GNU General Public License for more details.
  41. + */
  42. +
  43. +#include <linux/delay.h>
  44. +#include <linux/err.h>
  45. +#include <linux/io.h>
  46. +#include <linux/kernel.h>
  47. +#include <linux/module.h>
  48. +#include <linux/mutex.h>
  49. +#include <linux/phy/phy.h>
  50. +#include <linux/platform_device.h>
  51. +#include <linux/reset.h>
  52. +#include <linux/of_platform.h>
  53. +
  54. +#include <asm/mach-ralink/ralink_regs.h>
  55. +
  56. +#define RT_SYSC_REG_SYSCFG1 0x014
  57. +#define RT_SYSC_REG_CLKCFG1 0x030
  58. +#define RT_SYSC_REG_USB_PHY_CFG 0x05c
  59. +
  60. +#define OFS_U2_PHY_AC0 0x800
  61. +#define OFS_U2_PHY_AC1 0x804
  62. +#define OFS_U2_PHY_AC2 0x808
  63. +#define OFS_U2_PHY_ACR0 0x810
  64. +#define OFS_U2_PHY_ACR1 0x814
  65. +#define OFS_U2_PHY_ACR2 0x818
  66. +#define OFS_U2_PHY_ACR3 0x81C
  67. +#define OFS_U2_PHY_ACR4 0x820
  68. +#define OFS_U2_PHY_AMON0 0x824
  69. +#define OFS_U2_PHY_DCR0 0x860
  70. +#define OFS_U2_PHY_DCR1 0x864
  71. +#define OFS_U2_PHY_DTM0 0x868
  72. +#define OFS_U2_PHY_DTM1 0x86C
  73. +
  74. +#define RT_RSTCTRL_UDEV BIT(25)
  75. +#define RT_RSTCTRL_UHST BIT(22)
  76. +#define RT_SYSCFG1_USB0_HOST_MODE BIT(10)
  77. +
  78. +#define MT7620_CLKCFG1_UPHY0_CLK_EN BIT(25)
  79. +#define MT7620_CLKCFG1_UPHY1_CLK_EN BIT(22)
  80. +#define RT_CLKCFG1_UPHY1_CLK_EN BIT(20)
  81. +#define RT_CLKCFG1_UPHY0_CLK_EN BIT(18)
  82. +
  83. +#define USB_PHY_UTMI_8B60M BIT(1)
  84. +#define UDEV_WAKEUP BIT(0)
  85. +
  86. +struct ralink_usb_phy {
  87. + struct reset_control *rstdev;
  88. + struct reset_control *rsthost;
  89. + u32 clk;
  90. + struct phy *phy;
  91. + void __iomem *base;
  92. +};
  93. +
  94. +static void u2_phy_w32(struct ralink_usb_phy *phy, u32 val, u32 reg)
  95. +{
  96. + iowrite32(val, phy->base + reg);
  97. +}
  98. +
  99. +static u32 u2_phy_r32(struct ralink_usb_phy *phy, u32 reg)
  100. +{
  101. + return ioread32(phy->base + reg);
  102. +}
  103. +
  104. +static void
  105. +u2_phy_init(struct ralink_usb_phy *phy)
  106. +{
  107. + u2_phy_r32(phy, OFS_U2_PHY_AC2);
  108. + u2_phy_r32(phy, OFS_U2_PHY_ACR0);
  109. + u2_phy_r32(phy, OFS_U2_PHY_DCR0);
  110. +
  111. + u2_phy_w32(phy, 0x00ffff02, OFS_U2_PHY_DCR0);
  112. + u2_phy_r32(phy, OFS_U2_PHY_DCR0);
  113. + u2_phy_w32(phy, 0x00555502, OFS_U2_PHY_DCR0);
  114. + u2_phy_r32(phy, OFS_U2_PHY_DCR0);
  115. + u2_phy_w32(phy, 0x00aaaa02, OFS_U2_PHY_DCR0);
  116. + u2_phy_r32(phy, OFS_U2_PHY_DCR0);
  117. + u2_phy_w32(phy, 0x00000402, OFS_U2_PHY_DCR0);
  118. + u2_phy_r32(phy, OFS_U2_PHY_DCR0);
  119. + u2_phy_w32(phy, 0x0048086a, OFS_U2_PHY_AC0);
  120. + u2_phy_w32(phy, 0x4400001c, OFS_U2_PHY_AC1);
  121. + u2_phy_w32(phy, 0xc0200000, OFS_U2_PHY_ACR3);
  122. + u2_phy_w32(phy, 0x02000000, OFS_U2_PHY_DTM0);
  123. +}
  124. +
  125. +static int ralink_usb_phy_power_on(struct phy *_phy)
  126. +{
  127. + struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
  128. + u32 t;
  129. +
  130. + /* enable the phy */
  131. + rt_sysc_m32(0, phy->clk, RT_SYSC_REG_CLKCFG1);
  132. +
  133. + /* setup host mode */
  134. + rt_sysc_m32(0, RT_SYSCFG1_USB0_HOST_MODE, RT_SYSC_REG_SYSCFG1);
  135. +
  136. + /* deassert the reset lines */
  137. + reset_control_deassert(phy->rsthost);
  138. + reset_control_deassert(phy->rstdev);
  139. +
  140. + /*
  141. + * The SDK kernel had a delay of 100ms. however on device
  142. + * testing showed that 10ms is enough
  143. + */
  144. + mdelay(10);
  145. +
  146. + if (!IS_ERR(phy->base))
  147. + u2_phy_init(phy);
  148. +
  149. + /* print some status info */
  150. + t = rt_sysc_r32(RT_SYSC_REG_USB_PHY_CFG);
  151. + dev_info(&phy->phy->dev, "remote usb device wakeup %s\n",
  152. + (t & UDEV_WAKEUP) ? ("enabled") : ("disabled"));
  153. + if (t & USB_PHY_UTMI_8B60M)
  154. + dev_info(&phy->phy->dev, "UTMI 8bit 60MHz\n");
  155. + else
  156. + dev_info(&phy->phy->dev, "UTMI 16bit 30MHz\n");
  157. +
  158. + return 0;
  159. +}
  160. +
  161. +static int ralink_usb_phy_power_off(struct phy *_phy)
  162. +{
  163. + struct ralink_usb_phy *phy = phy_get_drvdata(_phy);
  164. +
  165. + /* assert the reset lines */
  166. + reset_control_assert(phy->rstdev);
  167. + reset_control_assert(phy->rsthost);
  168. +
  169. + /* disable the phy */
  170. + rt_sysc_m32(phy->clk, 0, RT_SYSC_REG_CLKCFG1);
  171. +
  172. + return 0;
  173. +}
  174. +
  175. +static struct phy_ops ralink_usb_phy_ops = {
  176. + .power_on = ralink_usb_phy_power_on,
  177. + .power_off = ralink_usb_phy_power_off,
  178. + .owner = THIS_MODULE,
  179. +};
  180. +
  181. +static const struct of_device_id ralink_usb_phy_of_match[] = {
  182. + {
  183. + .compatible = "ralink,rt3xxx-usbphy",
  184. + .data = (void *) (RT_CLKCFG1_UPHY1_CLK_EN |
  185. + RT_CLKCFG1_UPHY0_CLK_EN)
  186. + },
  187. + {
  188. + .compatible = "ralink,mt7620a-usbphy",
  189. + .data = (void *) (MT7620_CLKCFG1_UPHY1_CLK_EN |
  190. + MT7620_CLKCFG1_UPHY0_CLK_EN) },
  191. + { },
  192. +};
  193. +MODULE_DEVICE_TABLE(of, ralink_usb_phy_of_match);
  194. +
  195. +static int ralink_usb_phy_probe(struct platform_device *pdev)
  196. +{
  197. + struct resource *res;
  198. + struct device *dev = &pdev->dev;
  199. + struct phy_provider *phy_provider;
  200. + const struct of_device_id *match;
  201. + struct ralink_usb_phy *phy;
  202. +
  203. + phy = devm_kzalloc(dev, sizeof(*phy), GFP_KERNEL);
  204. + if (!phy)
  205. + return -ENOMEM;
  206. +
  207. + match = of_match_device(ralink_usb_phy_of_match, &pdev->dev);
  208. + if (!match)
  209. + return -ENODEV;
  210. +
  211. + phy->clk = (int) match->data;
  212. +
  213. + res = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  214. + phy->base = devm_ioremap_resource(&pdev->dev, res);
  215. +
  216. + phy->rsthost = devm_reset_control_get(&pdev->dev, "host");
  217. + if (IS_ERR(phy->rsthost)) {
  218. + dev_err(dev, "host reset is missing\n");
  219. + return PTR_ERR(phy->rsthost);
  220. + }
  221. +
  222. + phy->rstdev = devm_reset_control_get(&pdev->dev, "device");
  223. + if (IS_ERR(phy->rstdev)) {
  224. + dev_err(dev, "device reset is missing\n");
  225. + return PTR_ERR(phy->rstdev);
  226. + }
  227. +
  228. + phy->phy = devm_phy_create(dev, NULL, &ralink_usb_phy_ops, NULL);
  229. + if (IS_ERR(phy->phy)) {
  230. + dev_err(dev, "failed to create PHY\n");
  231. + return PTR_ERR(phy->phy);
  232. + }
  233. + phy_set_drvdata(phy->phy, phy);
  234. +
  235. + phy_provider = devm_of_phy_provider_register(dev, of_phy_simple_xlate);
  236. +
  237. + return PTR_ERR_OR_ZERO(phy_provider);
  238. +}
  239. +
  240. +static struct platform_driver ralink_usb_phy_driver = {
  241. + .probe = ralink_usb_phy_probe,
  242. + .driver = {
  243. + .of_match_table = ralink_usb_phy_of_match,
  244. + .name = "ralink-usb-phy",
  245. + }
  246. +};
  247. +module_platform_driver(ralink_usb_phy_driver);
  248. +
  249. +MODULE_DESCRIPTION("Ralink USB phy driver");
  250. +MODULE_AUTHOR("John Crispin <blogic@openwrt.org>");
  251. +MODULE_LICENSE("GPL v2");