phy-samsung-usb3.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  1. /* linux/drivers/usb/phy/phy-samsung-usb3.c
  2. *
  3. * Copyright (c) 2013 Samsung Electronics Co., Ltd.
  4. * http://www.samsung.com
  5. *
  6. * Author: Vivek Gautam <gautam.vivek@samsung.com>
  7. *
  8. * Samsung USB 3.0 PHY transceiver; talks to DWC3 controller.
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License version 2 as
  12. * published by the Free Software Foundation.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU General Public License for more details.
  18. */
  19. #include <linux/module.h>
  20. #include <linux/platform_device.h>
  21. #include <linux/clk.h>
  22. #include <linux/delay.h>
  23. #include <linux/err.h>
  24. #include <linux/io.h>
  25. #include <linux/of.h>
  26. #include <linux/usb/samsung_usb_phy.h>
  27. #include <linux/platform_data/samsung-usbphy.h>
  28. #include "phy-samsung-usb.h"
  29. /*
  30. * Sets the phy clk as EXTREFCLK (XXTI) which is internal clock from clock core.
  31. */
  32. static u32 samsung_usb3phy_set_refclk(struct samsung_usbphy *sphy)
  33. {
  34. u32 reg;
  35. u32 refclk;
  36. refclk = sphy->ref_clk_freq;
  37. reg = PHYCLKRST_REFCLKSEL_EXT_REFCLK |
  38. PHYCLKRST_FSEL(refclk);
  39. switch (refclk) {
  40. case FSEL_CLKSEL_50M:
  41. reg |= (PHYCLKRST_MPLL_MULTIPLIER_50M_REF |
  42. PHYCLKRST_SSC_REFCLKSEL(0x00));
  43. break;
  44. case FSEL_CLKSEL_20M:
  45. reg |= (PHYCLKRST_MPLL_MULTIPLIER_20MHZ_REF |
  46. PHYCLKRST_SSC_REFCLKSEL(0x00));
  47. break;
  48. case FSEL_CLKSEL_19200K:
  49. reg |= (PHYCLKRST_MPLL_MULTIPLIER_19200KHZ_REF |
  50. PHYCLKRST_SSC_REFCLKSEL(0x88));
  51. break;
  52. case FSEL_CLKSEL_24M:
  53. default:
  54. reg |= (PHYCLKRST_MPLL_MULTIPLIER_24MHZ_REF |
  55. PHYCLKRST_SSC_REFCLKSEL(0x88));
  56. break;
  57. }
  58. return reg;
  59. }
  60. static void samsung_exynos5_usb3phy_enable(struct samsung_usbphy *sphy)
  61. {
  62. void __iomem *regs = sphy->regs;
  63. u32 phyparam0;
  64. u32 phyparam1;
  65. u32 linksystem;
  66. u32 phybatchg;
  67. u32 phytest;
  68. u32 phyclkrst;
  69. /* Reset USB 3.0 PHY */
  70. writel(0x0, regs + EXYNOS5_DRD_PHYREG0);
  71. phyparam0 = readl(regs + EXYNOS5_DRD_PHYPARAM0);
  72. /* Select PHY CLK source */
  73. phyparam0 &= ~PHYPARAM0_REF_USE_PAD;
  74. /* Set Loss-of-Signal Detector sensitivity */
  75. phyparam0 &= ~PHYPARAM0_REF_LOSLEVEL_MASK;
  76. phyparam0 |= PHYPARAM0_REF_LOSLEVEL;
  77. writel(phyparam0, regs + EXYNOS5_DRD_PHYPARAM0);
  78. writel(0x0, regs + EXYNOS5_DRD_PHYRESUME);
  79. /*
  80. * Setting the Frame length Adj value[6:1] to default 0x20
  81. * See xHCI 1.0 spec, 5.2.4
  82. */
  83. linksystem = LINKSYSTEM_XHCI_VERSION_CONTROL |
  84. LINKSYSTEM_FLADJ(0x20);
  85. writel(linksystem, regs + EXYNOS5_DRD_LINKSYSTEM);
  86. phyparam1 = readl(regs + EXYNOS5_DRD_PHYPARAM1);
  87. /* Set Tx De-Emphasis level */
  88. phyparam1 &= ~PHYPARAM1_PCS_TXDEEMPH_MASK;
  89. phyparam1 |= PHYPARAM1_PCS_TXDEEMPH;
  90. writel(phyparam1, regs + EXYNOS5_DRD_PHYPARAM1);
  91. phybatchg = readl(regs + EXYNOS5_DRD_PHYBATCHG);
  92. phybatchg |= PHYBATCHG_UTMI_CLKSEL;
  93. writel(phybatchg, regs + EXYNOS5_DRD_PHYBATCHG);
  94. /* PHYTEST POWERDOWN Control */
  95. phytest = readl(regs + EXYNOS5_DRD_PHYTEST);
  96. phytest &= ~(PHYTEST_POWERDOWN_SSP |
  97. PHYTEST_POWERDOWN_HSP);
  98. writel(phytest, regs + EXYNOS5_DRD_PHYTEST);
  99. /* UTMI Power Control */
  100. writel(PHYUTMI_OTGDISABLE, regs + EXYNOS5_DRD_PHYUTMI);
  101. phyclkrst = samsung_usb3phy_set_refclk(sphy);
  102. phyclkrst |= PHYCLKRST_PORTRESET |
  103. /* Digital power supply in normal operating mode */
  104. PHYCLKRST_RETENABLEN |
  105. /* Enable ref clock for SS function */
  106. PHYCLKRST_REF_SSP_EN |
  107. /* Enable spread spectrum */
  108. PHYCLKRST_SSC_EN |
  109. /* Power down HS Bias and PLL blocks in suspend mode */
  110. PHYCLKRST_COMMONONN;
  111. writel(phyclkrst, regs + EXYNOS5_DRD_PHYCLKRST);
  112. udelay(10);
  113. phyclkrst &= ~(PHYCLKRST_PORTRESET);
  114. writel(phyclkrst, regs + EXYNOS5_DRD_PHYCLKRST);
  115. }
  116. static void samsung_exynos5_usb3phy_disable(struct samsung_usbphy *sphy)
  117. {
  118. u32 phyutmi;
  119. u32 phyclkrst;
  120. u32 phytest;
  121. void __iomem *regs = sphy->regs;
  122. phyutmi = PHYUTMI_OTGDISABLE |
  123. PHYUTMI_FORCESUSPEND |
  124. PHYUTMI_FORCESLEEP;
  125. writel(phyutmi, regs + EXYNOS5_DRD_PHYUTMI);
  126. /* Resetting the PHYCLKRST enable bits to reduce leakage current */
  127. phyclkrst = readl(regs + EXYNOS5_DRD_PHYCLKRST);
  128. phyclkrst &= ~(PHYCLKRST_REF_SSP_EN |
  129. PHYCLKRST_SSC_EN |
  130. PHYCLKRST_COMMONONN);
  131. writel(phyclkrst, regs + EXYNOS5_DRD_PHYCLKRST);
  132. /* Control PHYTEST to remove leakage current */
  133. phytest = readl(regs + EXYNOS5_DRD_PHYTEST);
  134. phytest |= (PHYTEST_POWERDOWN_SSP |
  135. PHYTEST_POWERDOWN_HSP);
  136. writel(phytest, regs + EXYNOS5_DRD_PHYTEST);
  137. }
  138. static int samsung_usb3phy_init(struct usb_phy *phy)
  139. {
  140. struct samsung_usbphy *sphy;
  141. unsigned long flags;
  142. int ret = 0;
  143. sphy = phy_to_sphy(phy);
  144. /* Enable the phy clock */
  145. ret = clk_prepare_enable(sphy->clk);
  146. if (ret) {
  147. dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
  148. return ret;
  149. }
  150. spin_lock_irqsave(&sphy->lock, flags);
  151. /* setting default phy-type for USB 3.0 */
  152. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
  153. /* Disable phy isolation */
  154. if (sphy->drv_data->set_isolation)
  155. sphy->drv_data->set_isolation(sphy, false);
  156. /* Initialize usb phy registers */
  157. sphy->drv_data->phy_enable(sphy);
  158. spin_unlock_irqrestore(&sphy->lock, flags);
  159. /* Disable the phy clock */
  160. clk_disable_unprepare(sphy->clk);
  161. return ret;
  162. }
  163. /*
  164. * The function passed to the usb driver for phy shutdown
  165. */
  166. static void samsung_usb3phy_shutdown(struct usb_phy *phy)
  167. {
  168. struct samsung_usbphy *sphy;
  169. unsigned long flags;
  170. sphy = phy_to_sphy(phy);
  171. if (clk_prepare_enable(sphy->clk)) {
  172. dev_err(sphy->dev, "%s: clk_prepare_enable failed\n", __func__);
  173. return;
  174. }
  175. spin_lock_irqsave(&sphy->lock, flags);
  176. /* setting default phy-type for USB 3.0 */
  177. samsung_usbphy_set_type(&sphy->phy, USB_PHY_TYPE_DEVICE);
  178. /* De-initialize usb phy registers */
  179. sphy->drv_data->phy_disable(sphy);
  180. /* Enable phy isolation */
  181. if (sphy->drv_data->set_isolation)
  182. sphy->drv_data->set_isolation(sphy, true);
  183. spin_unlock_irqrestore(&sphy->lock, flags);
  184. clk_disable_unprepare(sphy->clk);
  185. }
  186. static int samsung_usb3phy_probe(struct platform_device *pdev)
  187. {
  188. struct samsung_usbphy *sphy;
  189. struct samsung_usbphy_data *pdata = dev_get_platdata(&pdev->dev);
  190. struct device *dev = &pdev->dev;
  191. struct resource *phy_mem;
  192. void __iomem *phy_base;
  193. struct clk *clk;
  194. int ret;
  195. phy_mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
  196. phy_base = devm_ioremap_resource(dev, phy_mem);
  197. if (IS_ERR(phy_base))
  198. return PTR_ERR(phy_base);
  199. sphy = devm_kzalloc(dev, sizeof(*sphy), GFP_KERNEL);
  200. if (!sphy)
  201. return -ENOMEM;
  202. clk = devm_clk_get(dev, "usbdrd30");
  203. if (IS_ERR(clk)) {
  204. dev_err(dev, "Failed to get device clock\n");
  205. return PTR_ERR(clk);
  206. }
  207. sphy->dev = dev;
  208. if (dev->of_node) {
  209. ret = samsung_usbphy_parse_dt(sphy);
  210. if (ret < 0)
  211. return ret;
  212. } else {
  213. if (!pdata) {
  214. dev_err(dev, "no platform data specified\n");
  215. return -EINVAL;
  216. }
  217. }
  218. sphy->plat = pdata;
  219. sphy->regs = phy_base;
  220. sphy->clk = clk;
  221. sphy->phy.dev = sphy->dev;
  222. sphy->phy.label = "samsung-usb3phy";
  223. sphy->phy.init = samsung_usb3phy_init;
  224. sphy->phy.shutdown = samsung_usb3phy_shutdown;
  225. sphy->drv_data = samsung_usbphy_get_driver_data(pdev);
  226. sphy->ref_clk_freq = samsung_usbphy_get_refclk_freq(sphy);
  227. if (sphy->ref_clk_freq < 0)
  228. return -EINVAL;
  229. spin_lock_init(&sphy->lock);
  230. platform_set_drvdata(pdev, sphy);
  231. return usb_add_phy(&sphy->phy, USB_PHY_TYPE_USB3);
  232. }
  233. static int samsung_usb3phy_remove(struct platform_device *pdev)
  234. {
  235. struct samsung_usbphy *sphy = platform_get_drvdata(pdev);
  236. usb_remove_phy(&sphy->phy);
  237. if (sphy->pmuregs)
  238. iounmap(sphy->pmuregs);
  239. if (sphy->sysreg)
  240. iounmap(sphy->sysreg);
  241. return 0;
  242. }
  243. static struct samsung_usbphy_drvdata usb3phy_exynos5 = {
  244. .cpu_type = TYPE_EXYNOS5250,
  245. .devphy_en_mask = EXYNOS_USBPHY_ENABLE,
  246. .rate_to_clksel = samsung_usbphy_rate_to_clksel_4x12,
  247. .set_isolation = samsung_usbphy_set_isolation_4210,
  248. .phy_enable = samsung_exynos5_usb3phy_enable,
  249. .phy_disable = samsung_exynos5_usb3phy_disable,
  250. };
  251. #ifdef CONFIG_OF
  252. static const struct of_device_id samsung_usbphy_dt_match[] = {
  253. {
  254. .compatible = "samsung,exynos5250-usb3phy",
  255. .data = &usb3phy_exynos5
  256. },
  257. {},
  258. };
  259. MODULE_DEVICE_TABLE(of, samsung_usbphy_dt_match);
  260. #endif
  261. static struct platform_device_id samsung_usbphy_driver_ids[] = {
  262. {
  263. .name = "exynos5250-usb3phy",
  264. .driver_data = (unsigned long)&usb3phy_exynos5,
  265. },
  266. {},
  267. };
  268. MODULE_DEVICE_TABLE(platform, samsung_usbphy_driver_ids);
  269. static struct platform_driver samsung_usb3phy_driver = {
  270. .probe = samsung_usb3phy_probe,
  271. .remove = samsung_usb3phy_remove,
  272. .id_table = samsung_usbphy_driver_ids,
  273. .driver = {
  274. .name = "samsung-usb3phy",
  275. .owner = THIS_MODULE,
  276. .of_match_table = of_match_ptr(samsung_usbphy_dt_match),
  277. },
  278. };
  279. module_platform_driver(samsung_usb3phy_driver);
  280. MODULE_DESCRIPTION("Samsung USB 3.0 phy controller");
  281. MODULE_AUTHOR("Vivek Gautam <gautam.vivek@samsung.com>");
  282. MODULE_LICENSE("GPL");
  283. MODULE_ALIAS("platform:samsung-usb3phy");