omap_phy_internal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257
  1. /*
  2. * This file configures the internal USB PHY in OMAP4430. Used
  3. * with TWL6030 transceiver and MUSB on OMAP4430.
  4. *
  5. * Copyright (C) 2010 Texas Instruments Incorporated - http://www.ti.com
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Author: Hema HK <hemahk@ti.com>
  12. *
  13. * This program is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. *
  22. */
  23. #include <linux/types.h>
  24. #include <linux/delay.h>
  25. #include <linux/clk.h>
  26. #include <linux/io.h>
  27. #include <linux/err.h>
  28. #include <linux/usb.h>
  29. #include <plat/usb.h>
  30. #include "control.h"
  31. /* OMAP control module register for UTMI PHY */
  32. #define CONTROL_DEV_CONF 0x300
  33. #define PHY_PD 0x1
  34. #define USBOTGHS_CONTROL 0x33c
  35. #define AVALID BIT(0)
  36. #define BVALID BIT(1)
  37. #define VBUSVALID BIT(2)
  38. #define SESSEND BIT(3)
  39. #define IDDIG BIT(4)
  40. static struct clk *phyclk, *clk48m, *clk32k;
  41. static void __iomem *ctrl_base;
  42. static int usbotghs_control;
  43. int omap4430_phy_init(struct device *dev)
  44. {
  45. ctrl_base = ioremap(OMAP443X_SCM_BASE, SZ_1K);
  46. if (!ctrl_base) {
  47. dev_err(dev, "control module ioremap failed\n");
  48. return -ENOMEM;
  49. }
  50. /* Power down the phy */
  51. __raw_writel(PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  52. phyclk = clk_get(dev, "ocp2scp_usb_phy_ick");
  53. if (IS_ERR(phyclk)) {
  54. dev_err(dev, "cannot clk_get ocp2scp_usb_phy_ick\n");
  55. iounmap(ctrl_base);
  56. return PTR_ERR(phyclk);
  57. }
  58. clk48m = clk_get(dev, "ocp2scp_usb_phy_phy_48m");
  59. if (IS_ERR(clk48m)) {
  60. dev_err(dev, "cannot clk_get ocp2scp_usb_phy_phy_48m\n");
  61. clk_put(phyclk);
  62. iounmap(ctrl_base);
  63. return PTR_ERR(clk48m);
  64. }
  65. clk32k = clk_get(dev, "usb_phy_cm_clk32k");
  66. if (IS_ERR(clk32k)) {
  67. dev_err(dev, "cannot clk_get usb_phy_cm_clk32k\n");
  68. clk_put(phyclk);
  69. clk_put(clk48m);
  70. iounmap(ctrl_base);
  71. return PTR_ERR(clk32k);
  72. }
  73. return 0;
  74. }
  75. int omap4430_phy_set_clk(struct device *dev, int on)
  76. {
  77. static int state;
  78. if (on && !state) {
  79. /* Enable the phy clocks */
  80. clk_enable(phyclk);
  81. clk_enable(clk48m);
  82. clk_enable(clk32k);
  83. state = 1;
  84. } else if (state) {
  85. /* Disable the phy clocks */
  86. clk_disable(phyclk);
  87. clk_disable(clk48m);
  88. clk_disable(clk32k);
  89. state = 0;
  90. }
  91. return 0;
  92. }
  93. int omap4430_phy_power(struct device *dev, int ID, int on)
  94. {
  95. if (on) {
  96. if (ID)
  97. /* enable VBUS valid, IDDIG groung */
  98. __raw_writel(AVALID | VBUSVALID, ctrl_base +
  99. USBOTGHS_CONTROL);
  100. else
  101. /*
  102. * Enable VBUS Valid, AValid and IDDIG
  103. * high impedence
  104. */
  105. __raw_writel(IDDIG | AVALID | VBUSVALID,
  106. ctrl_base + USBOTGHS_CONTROL);
  107. } else {
  108. /* Enable session END and IDIG to high impedence. */
  109. __raw_writel(SESSEND | IDDIG, ctrl_base +
  110. USBOTGHS_CONTROL);
  111. }
  112. return 0;
  113. }
  114. int omap4430_phy_suspend(struct device *dev, int suspend)
  115. {
  116. if (suspend) {
  117. /* Disable the clocks */
  118. omap4430_phy_set_clk(dev, 0);
  119. /* Power down the phy */
  120. __raw_writel(PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  121. /* save the context */
  122. usbotghs_control = __raw_readl(ctrl_base + USBOTGHS_CONTROL);
  123. } else {
  124. /* Enable the internel phy clcoks */
  125. omap4430_phy_set_clk(dev, 1);
  126. /* power on the phy */
  127. if (__raw_readl(ctrl_base + CONTROL_DEV_CONF) & PHY_PD) {
  128. __raw_writel(~PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  129. mdelay(200);
  130. }
  131. /* restore the context */
  132. __raw_writel(usbotghs_control, ctrl_base + USBOTGHS_CONTROL);
  133. }
  134. return 0;
  135. }
  136. int omap4430_phy_exit(struct device *dev)
  137. {
  138. if (ctrl_base)
  139. iounmap(ctrl_base);
  140. if (phyclk)
  141. clk_put(phyclk);
  142. if (clk48m)
  143. clk_put(clk48m);
  144. if (clk32k)
  145. clk_put(clk32k);
  146. return 0;
  147. }
  148. void am35x_musb_reset(void)
  149. {
  150. u32 regval;
  151. /* Reset the musb interface */
  152. regval = omap_ctrl_readl(AM35XX_CONTROL_IP_SW_RESET);
  153. regval |= AM35XX_USBOTGSS_SW_RST;
  154. omap_ctrl_writel(regval, AM35XX_CONTROL_IP_SW_RESET);
  155. regval &= ~AM35XX_USBOTGSS_SW_RST;
  156. omap_ctrl_writel(regval, AM35XX_CONTROL_IP_SW_RESET);
  157. regval = omap_ctrl_readl(AM35XX_CONTROL_IP_SW_RESET);
  158. }
  159. void am35x_musb_phy_power(u8 on)
  160. {
  161. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  162. u32 devconf2;
  163. if (on) {
  164. /*
  165. * Start the on-chip PHY and its PLL.
  166. */
  167. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  168. devconf2 &= ~(CONF2_RESET | CONF2_PHYPWRDN | CONF2_OTGPWRDN);
  169. devconf2 |= CONF2_PHY_PLLON;
  170. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  171. pr_info(KERN_INFO "Waiting for PHY clock good...\n");
  172. while (!(omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2)
  173. & CONF2_PHYCLKGD)) {
  174. cpu_relax();
  175. if (time_after(jiffies, timeout)) {
  176. pr_err(KERN_ERR "musb PHY clock good timed out\n");
  177. break;
  178. }
  179. }
  180. } else {
  181. /*
  182. * Power down the on-chip PHY.
  183. */
  184. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  185. devconf2 &= ~CONF2_PHY_PLLON;
  186. devconf2 |= CONF2_PHYPWRDN | CONF2_OTGPWRDN;
  187. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  188. }
  189. }
  190. void am35x_musb_clear_irq(void)
  191. {
  192. u32 regval;
  193. regval = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  194. regval |= AM35XX_USBOTGSS_INT_CLR;
  195. omap_ctrl_writel(regval, AM35XX_CONTROL_LVL_INTR_CLEAR);
  196. regval = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  197. }
  198. void am35x_musb_set_mode(u8 musb_mode)
  199. {
  200. u32 devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  201. devconf2 &= ~CONF2_OTGMODE;
  202. switch (musb_mode) {
  203. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  204. case MUSB_HOST: /* Force VBUS valid, ID = 0 */
  205. devconf2 |= CONF2_FORCE_HOST;
  206. break;
  207. #endif
  208. #ifdef CONFIG_USB_GADGET_MUSB_HDRC
  209. case MUSB_PERIPHERAL: /* Force VBUS valid, ID = 1 */
  210. devconf2 |= CONF2_FORCE_DEVICE;
  211. break;
  212. #endif
  213. #ifdef CONFIG_USB_MUSB_OTG
  214. case MUSB_OTG: /* Don't override the VBUS/ID comparators */
  215. devconf2 |= CONF2_NO_OVERRIDE;
  216. break;
  217. #endif
  218. default:
  219. pr_info(KERN_INFO "Unsupported mode %u\n", musb_mode);
  220. }
  221. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  222. }