setup-usb-phy.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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 int exynos4_usb_phy1_init(struct platform_device *pdev)
  26. {
  27. struct clk *otg_clk;
  28. struct clk *xusbxti_clk;
  29. u32 phyclk;
  30. u32 rstcon;
  31. int err;
  32. atomic_inc(&host_usage);
  33. otg_clk = clk_get(&pdev->dev, "otg");
  34. if (IS_ERR(otg_clk)) {
  35. dev_err(&pdev->dev, "Failed to get otg clock\n");
  36. return PTR_ERR(otg_clk);
  37. }
  38. err = clk_enable(otg_clk);
  39. if (err) {
  40. clk_put(otg_clk);
  41. return err;
  42. }
  43. if (exynos4_usb_host_phy_is_on())
  44. return 0;
  45. writel(readl(S5P_USBHOST_PHY_CONTROL) | S5P_USBHOST_PHY_ENABLE,
  46. S5P_USBHOST_PHY_CONTROL);
  47. /* set clock frequency for PLL */
  48. phyclk = readl(EXYNOS4_PHYCLK) & ~CLKSEL_MASK;
  49. xusbxti_clk = clk_get(&pdev->dev, "xusbxti");
  50. if (xusbxti_clk && !IS_ERR(xusbxti_clk)) {
  51. switch (clk_get_rate(xusbxti_clk)) {
  52. case 12 * MHZ:
  53. phyclk |= CLKSEL_12M;
  54. break;
  55. case 24 * MHZ:
  56. phyclk |= CLKSEL_24M;
  57. break;
  58. default:
  59. case 48 * MHZ:
  60. /* default reference clock */
  61. break;
  62. }
  63. clk_put(xusbxti_clk);
  64. }
  65. writel(phyclk, EXYNOS4_PHYCLK);
  66. /* floating prevention logic: disable */
  67. writel((readl(EXYNOS4_PHY1CON) | FPENABLEN), EXYNOS4_PHY1CON);
  68. /* set to normal HSIC 0 and 1 of PHY1 */
  69. writel((readl(EXYNOS4_PHYPWR) & ~PHY1_HSIC_NORMAL_MASK),
  70. EXYNOS4_PHYPWR);
  71. /* set to normal standard USB of PHY1 */
  72. writel((readl(EXYNOS4_PHYPWR) & ~PHY1_STD_NORMAL_MASK), EXYNOS4_PHYPWR);
  73. /* reset all ports of both PHY and Link */
  74. rstcon = readl(EXYNOS4_RSTCON) | HOST_LINK_PORT_SWRST_MASK |
  75. PHY1_SWRST_MASK;
  76. writel(rstcon, EXYNOS4_RSTCON);
  77. udelay(10);
  78. rstcon &= ~(HOST_LINK_PORT_SWRST_MASK | PHY1_SWRST_MASK);
  79. writel(rstcon, EXYNOS4_RSTCON);
  80. udelay(80);
  81. clk_disable(otg_clk);
  82. clk_put(otg_clk);
  83. return 0;
  84. }
  85. static int exynos4_usb_phy1_exit(struct platform_device *pdev)
  86. {
  87. struct clk *otg_clk;
  88. int err;
  89. if (atomic_dec_return(&host_usage) > 0)
  90. return 0;
  91. otg_clk = clk_get(&pdev->dev, "otg");
  92. if (IS_ERR(otg_clk)) {
  93. dev_err(&pdev->dev, "Failed to get otg clock\n");
  94. return PTR_ERR(otg_clk);
  95. }
  96. err = clk_enable(otg_clk);
  97. if (err) {
  98. clk_put(otg_clk);
  99. return err;
  100. }
  101. writel((readl(EXYNOS4_PHYPWR) | PHY1_STD_ANALOG_POWERDOWN),
  102. EXYNOS4_PHYPWR);
  103. writel(readl(S5P_USBHOST_PHY_CONTROL) & ~S5P_USBHOST_PHY_ENABLE,
  104. S5P_USBHOST_PHY_CONTROL);
  105. clk_disable(otg_clk);
  106. clk_put(otg_clk);
  107. return 0;
  108. }
  109. int s5p_usb_phy_init(struct platform_device *pdev, int type)
  110. {
  111. if (type == S5P_USB_PHY_HOST)
  112. return exynos4_usb_phy1_init(pdev);
  113. return -EINVAL;
  114. }
  115. int s5p_usb_phy_exit(struct platform_device *pdev, int type)
  116. {
  117. if (type == S5P_USB_PHY_HOST)
  118. return exynos4_usb_phy1_exit(pdev);
  119. return -EINVAL;
  120. }