setup-usb-phy.c 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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. /* set clock frequency for PLL */
  30. phyclk = readl(EXYNOS4_PHYCLK) & ~CLKSEL_MASK;
  31. xusbxti_clk = clk_get(&pdev->dev, "xusbxti");
  32. if (xusbxti_clk && !IS_ERR(xusbxti_clk)) {
  33. switch (clk_get_rate(xusbxti_clk)) {
  34. case 12 * MHZ:
  35. phyclk |= CLKSEL_12M;
  36. break;
  37. case 24 * MHZ:
  38. phyclk |= CLKSEL_24M;
  39. break;
  40. default:
  41. case 48 * MHZ:
  42. /* default reference clock */
  43. break;
  44. }
  45. clk_put(xusbxti_clk);
  46. }
  47. writel(phyclk, EXYNOS4_PHYCLK);
  48. }
  49. static int exynos4210_usb_phy0_init(struct platform_device *pdev)
  50. {
  51. u32 rstcon;
  52. writel(readl(S5P_USBDEVICE_PHY_CONTROL) | S5P_USBDEVICE_PHY_ENABLE,
  53. S5P_USBDEVICE_PHY_CONTROL);
  54. exynos4210_usb_phy_clkset(pdev);
  55. /* set to normal PHY0 */
  56. writel((readl(EXYNOS4_PHYPWR) & ~PHY0_NORMAL_MASK), EXYNOS4_PHYPWR);
  57. /* reset PHY0 and Link */
  58. rstcon = readl(EXYNOS4_RSTCON) | PHY0_SWRST_MASK;
  59. writel(rstcon, EXYNOS4_RSTCON);
  60. udelay(10);
  61. rstcon &= ~PHY0_SWRST_MASK;
  62. writel(rstcon, EXYNOS4_RSTCON);
  63. return 0;
  64. }
  65. static int exynos4210_usb_phy0_exit(struct platform_device *pdev)
  66. {
  67. writel((readl(EXYNOS4_PHYPWR) | PHY0_ANALOG_POWERDOWN |
  68. PHY0_OTG_DISABLE), EXYNOS4_PHYPWR);
  69. writel(readl(S5P_USBDEVICE_PHY_CONTROL) & ~S5P_USBDEVICE_PHY_ENABLE,
  70. S5P_USBDEVICE_PHY_CONTROL);
  71. return 0;
  72. }
  73. static int exynos4210_usb_phy1_init(struct platform_device *pdev)
  74. {
  75. struct clk *otg_clk;
  76. u32 rstcon;
  77. int err;
  78. atomic_inc(&host_usage);
  79. otg_clk = clk_get(&pdev->dev, "otg");
  80. if (IS_ERR(otg_clk)) {
  81. dev_err(&pdev->dev, "Failed to get otg clock\n");
  82. return PTR_ERR(otg_clk);
  83. }
  84. err = clk_enable(otg_clk);
  85. if (err) {
  86. clk_put(otg_clk);
  87. return err;
  88. }
  89. if (exynos4_usb_host_phy_is_on())
  90. return 0;
  91. writel(readl(S5P_USBHOST_PHY_CONTROL) | S5P_USBHOST_PHY_ENABLE,
  92. S5P_USBHOST_PHY_CONTROL);
  93. exynos4210_usb_phy_clkset(pdev);
  94. /* floating prevention logic: disable */
  95. writel((readl(EXYNOS4_PHY1CON) | FPENABLEN), EXYNOS4_PHY1CON);
  96. /* set to normal HSIC 0 and 1 of PHY1 */
  97. writel((readl(EXYNOS4_PHYPWR) & ~PHY1_HSIC_NORMAL_MASK),
  98. EXYNOS4_PHYPWR);
  99. /* set to normal standard USB of PHY1 */
  100. writel((readl(EXYNOS4_PHYPWR) & ~PHY1_STD_NORMAL_MASK), EXYNOS4_PHYPWR);
  101. /* reset all ports of both PHY and Link */
  102. rstcon = readl(EXYNOS4_RSTCON) | HOST_LINK_PORT_SWRST_MASK |
  103. PHY1_SWRST_MASK;
  104. writel(rstcon, EXYNOS4_RSTCON);
  105. udelay(10);
  106. rstcon &= ~(HOST_LINK_PORT_SWRST_MASK | PHY1_SWRST_MASK);
  107. writel(rstcon, EXYNOS4_RSTCON);
  108. udelay(80);
  109. clk_disable(otg_clk);
  110. clk_put(otg_clk);
  111. return 0;
  112. }
  113. static int exynos4210_usb_phy1_exit(struct platform_device *pdev)
  114. {
  115. struct clk *otg_clk;
  116. int err;
  117. if (atomic_dec_return(&host_usage) > 0)
  118. return 0;
  119. otg_clk = clk_get(&pdev->dev, "otg");
  120. if (IS_ERR(otg_clk)) {
  121. dev_err(&pdev->dev, "Failed to get otg clock\n");
  122. return PTR_ERR(otg_clk);
  123. }
  124. err = clk_enable(otg_clk);
  125. if (err) {
  126. clk_put(otg_clk);
  127. return err;
  128. }
  129. writel((readl(EXYNOS4_PHYPWR) | PHY1_STD_ANALOG_POWERDOWN),
  130. EXYNOS4_PHYPWR);
  131. writel(readl(S5P_USBHOST_PHY_CONTROL) & ~S5P_USBHOST_PHY_ENABLE,
  132. S5P_USBHOST_PHY_CONTROL);
  133. clk_disable(otg_clk);
  134. clk_put(otg_clk);
  135. return 0;
  136. }
  137. int s5p_usb_phy_init(struct platform_device *pdev, int type)
  138. {
  139. if (type == S5P_USB_PHY_DEVICE)
  140. return exynos4210_usb_phy0_init(pdev);
  141. else if (type == S5P_USB_PHY_HOST)
  142. return exynos4210_usb_phy1_init(pdev);
  143. return -EINVAL;
  144. }
  145. int s5p_usb_phy_exit(struct platform_device *pdev, int type)
  146. {
  147. if (type == S5P_USB_PHY_DEVICE)
  148. return exynos4210_usb_phy0_exit(pdev);
  149. else if (type == S5P_USB_PHY_HOST)
  150. return exynos4210_usb_phy1_exit(pdev);
  151. return -EINVAL;
  152. }