setup-usb-phy.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  1. /*
  2. * Copyright (C) 2011 Samsung Electronics Co.Ltd
  3. * Author: Joonyoung Shim <jy0922.shim@samsung.com>
  4. *
  5. * This program is free software; you can redistribute it and/or modify it
  6. * under the terms of the GNU General Public License as published by the
  7. * Free Software Foundation; either version 2 of the License, or (at your
  8. * option) any later version.
  9. *
  10. */
  11. #include <linux/clk.h>
  12. #include <linux/delay.h>
  13. #include <linux/err.h>
  14. #include <linux/io.h>
  15. #include <linux/platform_device.h>
  16. #include <mach/regs-pmu.h>
  17. #include <mach/regs-usb-phy.h>
  18. #include <plat/cpu.h>
  19. #include <plat/usb-phy.h>
  20. static atomic_t host_usage;
  21. static int exynos4_usb_host_phy_is_on(void)
  22. {
  23. return (readl(EXYNOS4_PHYPWR) & PHY1_STD_ANALOG_POWERDOWN) ? 0 : 1;
  24. }
  25. static void exynos4210_usb_phy_clkset(struct platform_device *pdev)
  26. {
  27. struct clk *xusbxti_clk;
  28. u32 phyclk;
  29. xusbxti_clk = clk_get(&pdev->dev, "xusbxti");
  30. if (xusbxti_clk && !IS_ERR(xusbxti_clk)) {
  31. if (soc_is_exynos4210()) {
  32. /* set clock frequency for PLL */
  33. phyclk = readl(EXYNOS4_PHYCLK) & ~EXYNOS4210_CLKSEL_MASK;
  34. switch (clk_get_rate(xusbxti_clk)) {
  35. case 12 * MHZ:
  36. phyclk |= EXYNOS4210_CLKSEL_12M;
  37. break;
  38. case 48 * MHZ:
  39. phyclk |= EXYNOS4210_CLKSEL_48M;
  40. break;
  41. default:
  42. case 24 * MHZ:
  43. phyclk |= EXYNOS4210_CLKSEL_24M;
  44. break;
  45. }
  46. writel(phyclk, EXYNOS4_PHYCLK);
  47. } else if (soc_is_exynos4212() || soc_is_exynos4412()) {
  48. /* set clock frequency for PLL */
  49. phyclk = readl(EXYNOS4_PHYCLK) & ~EXYNOS4X12_CLKSEL_MASK;
  50. switch (clk_get_rate(xusbxti_clk)) {
  51. case 9600 * KHZ:
  52. phyclk |= EXYNOS4X12_CLKSEL_9600K;
  53. break;
  54. case 10 * MHZ:
  55. phyclk |= EXYNOS4X12_CLKSEL_10M;
  56. break;
  57. case 12 * MHZ:
  58. phyclk |= EXYNOS4X12_CLKSEL_12M;
  59. break;
  60. case 19200 * KHZ:
  61. phyclk |= EXYNOS4X12_CLKSEL_19200K;
  62. break;
  63. case 20 * MHZ:
  64. phyclk |= EXYNOS4X12_CLKSEL_20M;
  65. break;
  66. default:
  67. case 24 * MHZ:
  68. /* default reference clock */
  69. phyclk |= EXYNOS4X12_CLKSEL_24M;
  70. break;
  71. }
  72. writel(phyclk, EXYNOS4_PHYCLK);
  73. }
  74. clk_put(xusbxti_clk);
  75. }
  76. }
  77. static int exynos4210_usb_phy0_init(struct platform_device *pdev)
  78. {
  79. u32 rstcon;
  80. writel(readl(S5P_USBDEVICE_PHY_CONTROL) | S5P_USBDEVICE_PHY_ENABLE,
  81. S5P_USBDEVICE_PHY_CONTROL);
  82. exynos4210_usb_phy_clkset(pdev);
  83. /* set to normal PHY0 */
  84. writel((readl(EXYNOS4_PHYPWR) & ~PHY0_NORMAL_MASK), EXYNOS4_PHYPWR);
  85. /* reset PHY0 and Link */
  86. rstcon = readl(EXYNOS4_RSTCON) | PHY0_SWRST_MASK;
  87. writel(rstcon, EXYNOS4_RSTCON);
  88. udelay(10);
  89. rstcon &= ~PHY0_SWRST_MASK;
  90. writel(rstcon, EXYNOS4_RSTCON);
  91. return 0;
  92. }
  93. static int exynos4210_usb_phy0_exit(struct platform_device *pdev)
  94. {
  95. writel((readl(EXYNOS4_PHYPWR) | PHY0_ANALOG_POWERDOWN |
  96. PHY0_OTG_DISABLE), EXYNOS4_PHYPWR);
  97. writel(readl(S5P_USBDEVICE_PHY_CONTROL) & ~S5P_USBDEVICE_PHY_ENABLE,
  98. S5P_USBDEVICE_PHY_CONTROL);
  99. return 0;
  100. }
  101. static int exynos4210_usb_phy1_init(struct platform_device *pdev)
  102. {
  103. struct clk *otg_clk;
  104. u32 rstcon;
  105. int err;
  106. atomic_inc(&host_usage);
  107. otg_clk = clk_get(&pdev->dev, "otg");
  108. if (IS_ERR(otg_clk)) {
  109. dev_err(&pdev->dev, "Failed to get otg clock\n");
  110. return PTR_ERR(otg_clk);
  111. }
  112. err = clk_enable(otg_clk);
  113. if (err) {
  114. clk_put(otg_clk);
  115. return err;
  116. }
  117. if (exynos4_usb_host_phy_is_on())
  118. return 0;
  119. writel(readl(S5P_USBHOST_PHY_CONTROL) | S5P_USBHOST_PHY_ENABLE,
  120. S5P_USBHOST_PHY_CONTROL);
  121. exynos4210_usb_phy_clkset(pdev);
  122. /* floating prevention logic: disable */
  123. writel((readl(EXYNOS4_PHY1CON) | FPENABLEN), EXYNOS4_PHY1CON);
  124. /* set to normal HSIC 0 and 1 of PHY1 */
  125. writel((readl(EXYNOS4_PHYPWR) & ~PHY1_HSIC_NORMAL_MASK),
  126. EXYNOS4_PHYPWR);
  127. /* set to normal standard USB of PHY1 */
  128. writel((readl(EXYNOS4_PHYPWR) & ~PHY1_STD_NORMAL_MASK), EXYNOS4_PHYPWR);
  129. /* reset all ports of both PHY and Link */
  130. rstcon = readl(EXYNOS4_RSTCON) | HOST_LINK_PORT_SWRST_MASK |
  131. PHY1_SWRST_MASK;
  132. writel(rstcon, EXYNOS4_RSTCON);
  133. udelay(10);
  134. rstcon &= ~(HOST_LINK_PORT_SWRST_MASK | PHY1_SWRST_MASK);
  135. writel(rstcon, EXYNOS4_RSTCON);
  136. udelay(80);
  137. clk_disable(otg_clk);
  138. clk_put(otg_clk);
  139. return 0;
  140. }
  141. static int exynos4210_usb_phy1_exit(struct platform_device *pdev)
  142. {
  143. struct clk *otg_clk;
  144. int err;
  145. if (atomic_dec_return(&host_usage) > 0)
  146. return 0;
  147. otg_clk = clk_get(&pdev->dev, "otg");
  148. if (IS_ERR(otg_clk)) {
  149. dev_err(&pdev->dev, "Failed to get otg clock\n");
  150. return PTR_ERR(otg_clk);
  151. }
  152. err = clk_enable(otg_clk);
  153. if (err) {
  154. clk_put(otg_clk);
  155. return err;
  156. }
  157. writel((readl(EXYNOS4_PHYPWR) | PHY1_STD_ANALOG_POWERDOWN),
  158. EXYNOS4_PHYPWR);
  159. writel(readl(S5P_USBHOST_PHY_CONTROL) & ~S5P_USBHOST_PHY_ENABLE,
  160. S5P_USBHOST_PHY_CONTROL);
  161. clk_disable(otg_clk);
  162. clk_put(otg_clk);
  163. return 0;
  164. }
  165. int s5p_usb_phy_init(struct platform_device *pdev, int type)
  166. {
  167. if (type == S5P_USB_PHY_DEVICE)
  168. return exynos4210_usb_phy0_init(pdev);
  169. else if (type == S5P_USB_PHY_HOST)
  170. return exynos4210_usb_phy1_init(pdev);
  171. return -EINVAL;
  172. }
  173. int s5p_usb_phy_exit(struct platform_device *pdev, int type)
  174. {
  175. if (type == S5P_USB_PHY_DEVICE)
  176. return exynos4210_usb_phy0_exit(pdev);
  177. else if (type == S5P_USB_PHY_HOST)
  178. return exynos4210_usb_phy1_exit(pdev);
  179. return -EINVAL;
  180. }