phy-sun4i-usb.c 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338
  1. /*
  2. * Allwinner sun4i USB phy driver
  3. *
  4. * Copyright (C) 2014 Hans de Goede <hdegoede@redhat.com>
  5. *
  6. * Based on code from
  7. * Allwinner Technology Co., Ltd. <www.allwinnertech.com>
  8. *
  9. * Modelled after: Samsung S5P/EXYNOS SoC series MIPI CSIS/DSIM DPHY driver
  10. * Copyright (C) 2013 Samsung Electronics Co., Ltd.
  11. * Author: Sylwester Nawrocki <s.nawrocki@samsung.com>
  12. *
  13. * This program is free software; you can redistribute it and/or modify
  14. * it under the terms of the GNU General Public License as published by
  15. * the Free Software Foundation; either version 2 of the License, or
  16. * (at your option) any later version.
  17. *
  18. * This program is distributed in the hope that it will be useful,
  19. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  20. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  21. * GNU General Public License for more details.
  22. */
  23. #include <linux/clk.h>
  24. #include <linux/err.h>
  25. #include <linux/io.h>
  26. #include <linux/kernel.h>
  27. #include <linux/module.h>
  28. #include <linux/mutex.h>
  29. #include <linux/of.h>
  30. #include <linux/of_address.h>
  31. #include <linux/phy/phy.h>
  32. #include <linux/platform_device.h>
  33. #include <linux/regulator/consumer.h>
  34. #include <linux/reset.h>
  35. #define REG_ISCR 0x00
  36. #define REG_PHYCTL 0x04
  37. #define REG_PHYBIST 0x08
  38. #define REG_PHYTUNE 0x0c
  39. #define PHYCTL_DATA BIT(7)
  40. #define SUNXI_AHB_ICHR8_EN BIT(10)
  41. #define SUNXI_AHB_INCR4_BURST_EN BIT(9)
  42. #define SUNXI_AHB_INCRX_ALIGN_EN BIT(8)
  43. #define SUNXI_ULPI_BYPASS_EN BIT(0)
  44. /* Common Control Bits for Both PHYs */
  45. #define PHY_PLL_BW 0x03
  46. #define PHY_RES45_CAL_EN 0x0c
  47. /* Private Control Bits for Each PHY */
  48. #define PHY_TX_AMPLITUDE_TUNE 0x20
  49. #define PHY_TX_SLEWRATE_TUNE 0x22
  50. #define PHY_VBUSVALID_TH_SEL 0x25
  51. #define PHY_PULLUP_RES_SEL 0x27
  52. #define PHY_OTG_FUNC_EN 0x28
  53. #define PHY_VBUS_DET_EN 0x29
  54. #define PHY_DISCON_TH_SEL 0x2a
  55. #define MAX_PHYS 3
  56. struct sun4i_usb_phy_data {
  57. void __iomem *base;
  58. struct mutex mutex;
  59. int num_phys;
  60. u32 disc_thresh;
  61. struct sun4i_usb_phy {
  62. struct phy *phy;
  63. void __iomem *pmu;
  64. struct regulator *vbus;
  65. struct reset_control *reset;
  66. struct clk *clk;
  67. int index;
  68. } phys[MAX_PHYS];
  69. };
  70. #define to_sun4i_usb_phy_data(phy) \
  71. container_of((phy), struct sun4i_usb_phy_data, phys[(phy)->index])
  72. static void sun4i_usb_phy_write(struct sun4i_usb_phy *phy, u32 addr, u32 data,
  73. int len)
  74. {
  75. struct sun4i_usb_phy_data *phy_data = to_sun4i_usb_phy_data(phy);
  76. u32 temp, usbc_bit = BIT(phy->index * 2);
  77. int i;
  78. mutex_lock(&phy_data->mutex);
  79. for (i = 0; i < len; i++) {
  80. temp = readl(phy_data->base + REG_PHYCTL);
  81. /* clear the address portion */
  82. temp &= ~(0xff << 8);
  83. /* set the address */
  84. temp |= ((addr + i) << 8);
  85. writel(temp, phy_data->base + REG_PHYCTL);
  86. /* set the data bit and clear usbc bit*/
  87. temp = readb(phy_data->base + REG_PHYCTL);
  88. if (data & 0x1)
  89. temp |= PHYCTL_DATA;
  90. else
  91. temp &= ~PHYCTL_DATA;
  92. temp &= ~usbc_bit;
  93. writeb(temp, phy_data->base + REG_PHYCTL);
  94. /* pulse usbc_bit */
  95. temp = readb(phy_data->base + REG_PHYCTL);
  96. temp |= usbc_bit;
  97. writeb(temp, phy_data->base + REG_PHYCTL);
  98. temp = readb(phy_data->base + REG_PHYCTL);
  99. temp &= ~usbc_bit;
  100. writeb(temp, phy_data->base + REG_PHYCTL);
  101. data >>= 1;
  102. }
  103. mutex_unlock(&phy_data->mutex);
  104. }
  105. static void sun4i_usb_phy_passby(struct sun4i_usb_phy *phy, int enable)
  106. {
  107. u32 bits, reg_value;
  108. if (!phy->pmu)
  109. return;
  110. bits = SUNXI_AHB_ICHR8_EN | SUNXI_AHB_INCR4_BURST_EN |
  111. SUNXI_AHB_INCRX_ALIGN_EN | SUNXI_ULPI_BYPASS_EN;
  112. reg_value = readl(phy->pmu);
  113. if (enable)
  114. reg_value |= bits;
  115. else
  116. reg_value &= ~bits;
  117. writel(reg_value, phy->pmu);
  118. }
  119. static int sun4i_usb_phy_init(struct phy *_phy)
  120. {
  121. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  122. struct sun4i_usb_phy_data *data = to_sun4i_usb_phy_data(phy);
  123. int ret;
  124. ret = clk_prepare_enable(phy->clk);
  125. if (ret)
  126. return ret;
  127. ret = reset_control_deassert(phy->reset);
  128. if (ret) {
  129. clk_disable_unprepare(phy->clk);
  130. return ret;
  131. }
  132. /* Enable USB 45 Ohm resistor calibration */
  133. if (phy->index == 0)
  134. sun4i_usb_phy_write(phy, PHY_RES45_CAL_EN, 0x01, 1);
  135. /* Adjust PHY's magnitude and rate */
  136. sun4i_usb_phy_write(phy, PHY_TX_AMPLITUDE_TUNE, 0x14, 5);
  137. /* Disconnect threshold adjustment */
  138. sun4i_usb_phy_write(phy, PHY_DISCON_TH_SEL, data->disc_thresh, 2);
  139. sun4i_usb_phy_passby(phy, 1);
  140. return 0;
  141. }
  142. static int sun4i_usb_phy_exit(struct phy *_phy)
  143. {
  144. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  145. sun4i_usb_phy_passby(phy, 0);
  146. reset_control_assert(phy->reset);
  147. clk_disable_unprepare(phy->clk);
  148. return 0;
  149. }
  150. static int sun4i_usb_phy_power_on(struct phy *_phy)
  151. {
  152. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  153. int ret = 0;
  154. if (phy->vbus)
  155. ret = regulator_enable(phy->vbus);
  156. return ret;
  157. }
  158. static int sun4i_usb_phy_power_off(struct phy *_phy)
  159. {
  160. struct sun4i_usb_phy *phy = phy_get_drvdata(_phy);
  161. if (phy->vbus)
  162. regulator_disable(phy->vbus);
  163. return 0;
  164. }
  165. static struct phy_ops sun4i_usb_phy_ops = {
  166. .init = sun4i_usb_phy_init,
  167. .exit = sun4i_usb_phy_exit,
  168. .power_on = sun4i_usb_phy_power_on,
  169. .power_off = sun4i_usb_phy_power_off,
  170. .owner = THIS_MODULE,
  171. };
  172. static struct phy *sun4i_usb_phy_xlate(struct device *dev,
  173. struct of_phandle_args *args)
  174. {
  175. struct sun4i_usb_phy_data *data = dev_get_drvdata(dev);
  176. if (args->args[0] >= data->num_phys)
  177. return ERR_PTR(-ENODEV);
  178. return data->phys[args->args[0]].phy;
  179. }
  180. static int sun4i_usb_phy_probe(struct platform_device *pdev)
  181. {
  182. struct sun4i_usb_phy_data *data;
  183. struct device *dev = &pdev->dev;
  184. struct device_node *np = dev->of_node;
  185. struct phy_provider *phy_provider;
  186. bool dedicated_clocks;
  187. struct resource *res;
  188. int i;
  189. data = devm_kzalloc(dev, sizeof(*data), GFP_KERNEL);
  190. if (!data)
  191. return -ENOMEM;
  192. mutex_init(&data->mutex);
  193. if (of_device_is_compatible(np, "allwinner,sun5i-a13-usb-phy"))
  194. data->num_phys = 2;
  195. else
  196. data->num_phys = 3;
  197. if (of_device_is_compatible(np, "allwinner,sun4i-a10-usb-phy") ||
  198. of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy"))
  199. data->disc_thresh = 3;
  200. else
  201. data->disc_thresh = 2;
  202. if (of_device_is_compatible(np, "allwinner,sun6i-a31-usb-phy"))
  203. dedicated_clocks = true;
  204. else
  205. dedicated_clocks = false;
  206. res = platform_get_resource_byname(pdev, IORESOURCE_MEM, "phy_ctrl");
  207. data->base = devm_ioremap_resource(dev, res);
  208. if (IS_ERR(data->base))
  209. return PTR_ERR(data->base);
  210. for (i = 0; i < data->num_phys; i++) {
  211. struct sun4i_usb_phy *phy = data->phys + i;
  212. char name[16];
  213. snprintf(name, sizeof(name), "usb%d_vbus", i);
  214. phy->vbus = devm_regulator_get_optional(dev, name);
  215. if (IS_ERR(phy->vbus)) {
  216. if (PTR_ERR(phy->vbus) == -EPROBE_DEFER)
  217. return -EPROBE_DEFER;
  218. phy->vbus = NULL;
  219. }
  220. if (dedicated_clocks)
  221. snprintf(name, sizeof(name), "usb%d_phy", i);
  222. else
  223. strlcpy(name, "usb_phy", sizeof(name));
  224. phy->clk = devm_clk_get(dev, name);
  225. if (IS_ERR(phy->clk)) {
  226. dev_err(dev, "failed to get clock %s\n", name);
  227. return PTR_ERR(phy->clk);
  228. }
  229. snprintf(name, sizeof(name), "usb%d_reset", i);
  230. phy->reset = devm_reset_control_get(dev, name);
  231. if (IS_ERR(phy->reset)) {
  232. dev_err(dev, "failed to get reset %s\n", name);
  233. return PTR_ERR(phy->reset);
  234. }
  235. if (i) { /* No pmu for usbc0 */
  236. snprintf(name, sizeof(name), "pmu%d", i);
  237. res = platform_get_resource_byname(pdev,
  238. IORESOURCE_MEM, name);
  239. phy->pmu = devm_ioremap_resource(dev, res);
  240. if (IS_ERR(phy->pmu))
  241. return PTR_ERR(phy->pmu);
  242. }
  243. phy->phy = devm_phy_create(dev, NULL, &sun4i_usb_phy_ops);
  244. if (IS_ERR(phy->phy)) {
  245. dev_err(dev, "failed to create PHY %d\n", i);
  246. return PTR_ERR(phy->phy);
  247. }
  248. phy->index = i;
  249. phy_set_drvdata(phy->phy, &data->phys[i]);
  250. }
  251. dev_set_drvdata(dev, data);
  252. phy_provider = devm_of_phy_provider_register(dev, sun4i_usb_phy_xlate);
  253. return PTR_ERR_OR_ZERO(phy_provider);
  254. }
  255. static const struct of_device_id sun4i_usb_phy_of_match[] = {
  256. { .compatible = "allwinner,sun4i-a10-usb-phy" },
  257. { .compatible = "allwinner,sun5i-a13-usb-phy" },
  258. { .compatible = "allwinner,sun6i-a31-usb-phy" },
  259. { .compatible = "allwinner,sun7i-a20-usb-phy" },
  260. { },
  261. };
  262. MODULE_DEVICE_TABLE(of, sun4i_usb_phy_of_match);
  263. static struct platform_driver sun4i_usb_phy_driver = {
  264. .probe = sun4i_usb_phy_probe,
  265. .driver = {
  266. .of_match_table = sun4i_usb_phy_of_match,
  267. .name = "sun4i-usb-phy",
  268. }
  269. };
  270. module_platform_driver(sun4i_usb_phy_driver);
  271. MODULE_DESCRIPTION("Allwinner sun4i USB phy driver");
  272. MODULE_AUTHOR("Hans de Goede <hdegoede@redhat.com>");
  273. MODULE_LICENSE("GPL v2");