omap_phy_internal.c 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  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. pr_err("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. if (!dev)
  53. return 0;
  54. phyclk = clk_get(dev, "ocp2scp_usb_phy_ick");
  55. if (IS_ERR(phyclk)) {
  56. dev_err(dev, "cannot clk_get ocp2scp_usb_phy_ick\n");
  57. iounmap(ctrl_base);
  58. return PTR_ERR(phyclk);
  59. }
  60. clk48m = clk_get(dev, "ocp2scp_usb_phy_phy_48m");
  61. if (IS_ERR(clk48m)) {
  62. dev_err(dev, "cannot clk_get ocp2scp_usb_phy_phy_48m\n");
  63. clk_put(phyclk);
  64. iounmap(ctrl_base);
  65. return PTR_ERR(clk48m);
  66. }
  67. clk32k = clk_get(dev, "usb_phy_cm_clk32k");
  68. if (IS_ERR(clk32k)) {
  69. dev_err(dev, "cannot clk_get usb_phy_cm_clk32k\n");
  70. clk_put(phyclk);
  71. clk_put(clk48m);
  72. iounmap(ctrl_base);
  73. return PTR_ERR(clk32k);
  74. }
  75. return 0;
  76. }
  77. int omap4430_phy_set_clk(struct device *dev, int on)
  78. {
  79. static int state;
  80. if (on && !state) {
  81. /* Enable the phy clocks */
  82. clk_enable(phyclk);
  83. clk_enable(clk48m);
  84. clk_enable(clk32k);
  85. state = 1;
  86. } else if (state) {
  87. /* Disable the phy clocks */
  88. clk_disable(phyclk);
  89. clk_disable(clk48m);
  90. clk_disable(clk32k);
  91. state = 0;
  92. }
  93. return 0;
  94. }
  95. int omap4430_phy_power(struct device *dev, int ID, int on)
  96. {
  97. if (on) {
  98. if (ID)
  99. /* enable VBUS valid, IDDIG groung */
  100. __raw_writel(AVALID | VBUSVALID, ctrl_base +
  101. USBOTGHS_CONTROL);
  102. else
  103. /*
  104. * Enable VBUS Valid, AValid and IDDIG
  105. * high impedance
  106. */
  107. __raw_writel(IDDIG | AVALID | VBUSVALID,
  108. ctrl_base + USBOTGHS_CONTROL);
  109. } else {
  110. /* Enable session END and IDIG to high impedance. */
  111. __raw_writel(SESSEND | IDDIG, ctrl_base +
  112. USBOTGHS_CONTROL);
  113. }
  114. return 0;
  115. }
  116. int omap4430_phy_suspend(struct device *dev, int suspend)
  117. {
  118. if (suspend) {
  119. /* Disable the clocks */
  120. omap4430_phy_set_clk(dev, 0);
  121. /* Power down the phy */
  122. __raw_writel(PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  123. /* save the context */
  124. usbotghs_control = __raw_readl(ctrl_base + USBOTGHS_CONTROL);
  125. } else {
  126. /* Enable the internel phy clcoks */
  127. omap4430_phy_set_clk(dev, 1);
  128. /* power on the phy */
  129. if (__raw_readl(ctrl_base + CONTROL_DEV_CONF) & PHY_PD) {
  130. __raw_writel(~PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  131. mdelay(200);
  132. }
  133. /* restore the context */
  134. __raw_writel(usbotghs_control, ctrl_base + USBOTGHS_CONTROL);
  135. }
  136. return 0;
  137. }
  138. int omap4430_phy_exit(struct device *dev)
  139. {
  140. if (ctrl_base)
  141. iounmap(ctrl_base);
  142. if (phyclk)
  143. clk_put(phyclk);
  144. if (clk48m)
  145. clk_put(clk48m);
  146. if (clk32k)
  147. clk_put(clk32k);
  148. return 0;
  149. }
  150. void am35x_musb_reset(void)
  151. {
  152. u32 regval;
  153. /* Reset the musb interface */
  154. regval = omap_ctrl_readl(AM35XX_CONTROL_IP_SW_RESET);
  155. regval |= AM35XX_USBOTGSS_SW_RST;
  156. omap_ctrl_writel(regval, AM35XX_CONTROL_IP_SW_RESET);
  157. regval &= ~AM35XX_USBOTGSS_SW_RST;
  158. omap_ctrl_writel(regval, AM35XX_CONTROL_IP_SW_RESET);
  159. regval = omap_ctrl_readl(AM35XX_CONTROL_IP_SW_RESET);
  160. }
  161. void am35x_musb_phy_power(u8 on)
  162. {
  163. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  164. u32 devconf2;
  165. if (on) {
  166. /*
  167. * Start the on-chip PHY and its PLL.
  168. */
  169. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  170. devconf2 &= ~(CONF2_RESET | CONF2_PHYPWRDN | CONF2_OTGPWRDN);
  171. devconf2 |= CONF2_PHY_PLLON;
  172. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  173. pr_info(KERN_INFO "Waiting for PHY clock good...\n");
  174. while (!(omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2)
  175. & CONF2_PHYCLKGD)) {
  176. cpu_relax();
  177. if (time_after(jiffies, timeout)) {
  178. pr_err(KERN_ERR "musb PHY clock good timed out\n");
  179. break;
  180. }
  181. }
  182. } else {
  183. /*
  184. * Power down the on-chip PHY.
  185. */
  186. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  187. devconf2 &= ~CONF2_PHY_PLLON;
  188. devconf2 |= CONF2_PHYPWRDN | CONF2_OTGPWRDN;
  189. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  190. }
  191. }
  192. void am35x_musb_clear_irq(void)
  193. {
  194. u32 regval;
  195. regval = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  196. regval |= AM35XX_USBOTGSS_INT_CLR;
  197. omap_ctrl_writel(regval, AM35XX_CONTROL_LVL_INTR_CLEAR);
  198. regval = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  199. }
  200. void am35x_set_mode(u8 musb_mode)
  201. {
  202. u32 devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  203. devconf2 &= ~CONF2_OTGMODE;
  204. switch (musb_mode) {
  205. #ifdef CONFIG_USB_MUSB_HDRC_HCD
  206. case MUSB_HOST: /* Force VBUS valid, ID = 0 */
  207. devconf2 |= CONF2_FORCE_HOST;
  208. break;
  209. #endif
  210. #ifdef CONFIG_USB_GADGET_MUSB_HDRC
  211. case MUSB_PERIPHERAL: /* Force VBUS valid, ID = 1 */
  212. devconf2 |= CONF2_FORCE_DEVICE;
  213. break;
  214. #endif
  215. #ifdef CONFIG_USB_MUSB_OTG
  216. case MUSB_OTG: /* Don't override the VBUS/ID comparators */
  217. devconf2 |= CONF2_NO_OVERRIDE;
  218. break;
  219. #endif
  220. default:
  221. pr_info(KERN_INFO "Unsupported mode %u\n", musb_mode);
  222. }
  223. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  224. }