omap_phy_internal.c 6.2 KB

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