omap_phy_internal.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293
  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/hardware.h>
  30. #include <plat/usb.h>
  31. #include "control.h"
  32. /* OMAP control module register for UTMI PHY */
  33. #define CONTROL_DEV_CONF 0x300
  34. #define PHY_PD 0x1
  35. #define USBOTGHS_CONTROL 0x33c
  36. #define AVALID BIT(0)
  37. #define BVALID BIT(1)
  38. #define VBUSVALID BIT(2)
  39. #define SESSEND BIT(3)
  40. #define IDDIG BIT(4)
  41. static struct clk *phyclk, *clk48m, *clk32k;
  42. static void __iomem *ctrl_base;
  43. static int usbotghs_control;
  44. int omap4430_phy_init(struct device *dev)
  45. {
  46. ctrl_base = ioremap(OMAP443X_SCM_BASE, SZ_1K);
  47. if (!ctrl_base) {
  48. pr_err("control module ioremap failed\n");
  49. return -ENOMEM;
  50. }
  51. /* Power down the phy */
  52. __raw_writel(PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  53. if (!dev) {
  54. iounmap(ctrl_base);
  55. return 0;
  56. }
  57. phyclk = clk_get(dev, "ocp2scp_usb_phy_ick");
  58. if (IS_ERR(phyclk)) {
  59. dev_err(dev, "cannot clk_get ocp2scp_usb_phy_ick\n");
  60. iounmap(ctrl_base);
  61. return PTR_ERR(phyclk);
  62. }
  63. clk48m = clk_get(dev, "ocp2scp_usb_phy_phy_48m");
  64. if (IS_ERR(clk48m)) {
  65. dev_err(dev, "cannot clk_get ocp2scp_usb_phy_phy_48m\n");
  66. clk_put(phyclk);
  67. iounmap(ctrl_base);
  68. return PTR_ERR(clk48m);
  69. }
  70. clk32k = clk_get(dev, "usb_phy_cm_clk32k");
  71. if (IS_ERR(clk32k)) {
  72. dev_err(dev, "cannot clk_get usb_phy_cm_clk32k\n");
  73. clk_put(phyclk);
  74. clk_put(clk48m);
  75. iounmap(ctrl_base);
  76. return PTR_ERR(clk32k);
  77. }
  78. return 0;
  79. }
  80. int omap4430_phy_set_clk(struct device *dev, int on)
  81. {
  82. static int state;
  83. if (on && !state) {
  84. /* Enable the phy clocks */
  85. clk_enable(phyclk);
  86. clk_enable(clk48m);
  87. clk_enable(clk32k);
  88. state = 1;
  89. } else if (state) {
  90. /* Disable the phy clocks */
  91. clk_disable(phyclk);
  92. clk_disable(clk48m);
  93. clk_disable(clk32k);
  94. state = 0;
  95. }
  96. return 0;
  97. }
  98. int omap4430_phy_power(struct device *dev, int ID, int on)
  99. {
  100. if (on) {
  101. if (ID)
  102. /* enable VBUS valid, IDDIG groung */
  103. __raw_writel(AVALID | VBUSVALID, ctrl_base +
  104. USBOTGHS_CONTROL);
  105. else
  106. /*
  107. * Enable VBUS Valid, AValid and IDDIG
  108. * high impedance
  109. */
  110. __raw_writel(IDDIG | AVALID | VBUSVALID,
  111. ctrl_base + USBOTGHS_CONTROL);
  112. } else {
  113. /* Enable session END and IDIG to high impedance. */
  114. __raw_writel(SESSEND | IDDIG, ctrl_base +
  115. USBOTGHS_CONTROL);
  116. }
  117. return 0;
  118. }
  119. int omap4430_phy_suspend(struct device *dev, int suspend)
  120. {
  121. if (suspend) {
  122. /* Disable the clocks */
  123. omap4430_phy_set_clk(dev, 0);
  124. /* Power down the phy */
  125. __raw_writel(PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  126. /* save the context */
  127. usbotghs_control = __raw_readl(ctrl_base + USBOTGHS_CONTROL);
  128. } else {
  129. /* Enable the internel phy clcoks */
  130. omap4430_phy_set_clk(dev, 1);
  131. /* power on the phy */
  132. if (__raw_readl(ctrl_base + CONTROL_DEV_CONF) & PHY_PD) {
  133. __raw_writel(~PHY_PD, ctrl_base + CONTROL_DEV_CONF);
  134. mdelay(200);
  135. }
  136. /* restore the context */
  137. __raw_writel(usbotghs_control, ctrl_base + USBOTGHS_CONTROL);
  138. }
  139. return 0;
  140. }
  141. int omap4430_phy_exit(struct device *dev)
  142. {
  143. if (ctrl_base)
  144. iounmap(ctrl_base);
  145. if (phyclk)
  146. clk_put(phyclk);
  147. if (clk48m)
  148. clk_put(clk48m);
  149. if (clk32k)
  150. clk_put(clk32k);
  151. return 0;
  152. }
  153. void am35x_musb_reset(void)
  154. {
  155. u32 regval;
  156. /* Reset the musb interface */
  157. regval = omap_ctrl_readl(AM35XX_CONTROL_IP_SW_RESET);
  158. regval |= AM35XX_USBOTGSS_SW_RST;
  159. omap_ctrl_writel(regval, AM35XX_CONTROL_IP_SW_RESET);
  160. regval &= ~AM35XX_USBOTGSS_SW_RST;
  161. omap_ctrl_writel(regval, AM35XX_CONTROL_IP_SW_RESET);
  162. regval = omap_ctrl_readl(AM35XX_CONTROL_IP_SW_RESET);
  163. }
  164. void am35x_musb_phy_power(u8 on)
  165. {
  166. unsigned long timeout = jiffies + msecs_to_jiffies(100);
  167. u32 devconf2;
  168. if (on) {
  169. /*
  170. * Start the on-chip PHY and its PLL.
  171. */
  172. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  173. devconf2 &= ~(CONF2_RESET | CONF2_PHYPWRDN | CONF2_OTGPWRDN);
  174. devconf2 |= CONF2_PHY_PLLON;
  175. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  176. pr_info(KERN_INFO "Waiting for PHY clock good...\n");
  177. while (!(omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2)
  178. & CONF2_PHYCLKGD)) {
  179. cpu_relax();
  180. if (time_after(jiffies, timeout)) {
  181. pr_err(KERN_ERR "musb PHY clock good timed out\n");
  182. break;
  183. }
  184. }
  185. } else {
  186. /*
  187. * Power down the on-chip PHY.
  188. */
  189. devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  190. devconf2 &= ~CONF2_PHY_PLLON;
  191. devconf2 |= CONF2_PHYPWRDN | CONF2_OTGPWRDN;
  192. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  193. }
  194. }
  195. void am35x_musb_clear_irq(void)
  196. {
  197. u32 regval;
  198. regval = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  199. regval |= AM35XX_USBOTGSS_INT_CLR;
  200. omap_ctrl_writel(regval, AM35XX_CONTROL_LVL_INTR_CLEAR);
  201. regval = omap_ctrl_readl(AM35XX_CONTROL_LVL_INTR_CLEAR);
  202. }
  203. void am35x_set_mode(u8 musb_mode)
  204. {
  205. u32 devconf2 = omap_ctrl_readl(AM35XX_CONTROL_DEVCONF2);
  206. devconf2 &= ~CONF2_OTGMODE;
  207. switch (musb_mode) {
  208. case MUSB_HOST: /* Force VBUS valid, ID = 0 */
  209. devconf2 |= CONF2_FORCE_HOST;
  210. break;
  211. case MUSB_PERIPHERAL: /* Force VBUS valid, ID = 1 */
  212. devconf2 |= CONF2_FORCE_DEVICE;
  213. break;
  214. case MUSB_OTG: /* Don't override the VBUS/ID comparators */
  215. devconf2 |= CONF2_NO_OVERRIDE;
  216. break;
  217. default:
  218. pr_info(KERN_INFO "Unsupported mode %u\n", musb_mode);
  219. }
  220. omap_ctrl_writel(devconf2, AM35XX_CONTROL_DEVCONF2);
  221. }
  222. void ti81xx_musb_phy_power(u8 on)
  223. {
  224. void __iomem *scm_base = NULL;
  225. u32 usbphycfg;
  226. scm_base = ioremap(TI81XX_SCM_BASE, SZ_2K);
  227. if (!scm_base) {
  228. pr_err("system control module ioremap failed\n");
  229. return;
  230. }
  231. usbphycfg = __raw_readl(scm_base + USBCTRL0);
  232. if (on) {
  233. if (cpu_is_ti816x()) {
  234. usbphycfg |= TI816X_USBPHY0_NORMAL_MODE;
  235. usbphycfg &= ~TI816X_USBPHY_REFCLK_OSC;
  236. } else if (cpu_is_ti814x()) {
  237. usbphycfg &= ~(USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN
  238. | USBPHY_DPINPUT | USBPHY_DMINPUT);
  239. usbphycfg |= (USBPHY_OTGVDET_EN | USBPHY_OTGSESSEND_EN
  240. | USBPHY_DPOPBUFCTL | USBPHY_DMOPBUFCTL);
  241. }
  242. } else {
  243. if (cpu_is_ti816x())
  244. usbphycfg &= ~TI816X_USBPHY0_NORMAL_MODE;
  245. else if (cpu_is_ti814x())
  246. usbphycfg |= USBPHY_CM_PWRDN | USBPHY_OTG_PWRDN;
  247. }
  248. __raw_writel(usbphycfg, scm_base + USBCTRL0);
  249. iounmap(scm_base);
  250. }