twl4030.c 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. /*
  2. * Copyright (c) 2009 Wind River Systems, Inc.
  3. * Tom Rix <Tom.Rix@windriver.com>
  4. *
  5. * This is file is based on
  6. * repository git.gitorious.org/u-boot-omap3/mainline.git,
  7. * branch omap3-dev-usb, file drivers/usb/gadget/twl4030_usb.c
  8. *
  9. * This is the unique part of its copyright :
  10. *
  11. * ------------------------------------------------------------------------
  12. *
  13. * * (C) Copyright 2009 Atin Malaviya (atin.malaviya@gmail.com)
  14. *
  15. * Based on: twl4030_usb.c in linux 2.6 (drivers/i2c/chips/twl4030_usb.c)
  16. * Copyright (C) 2004-2007 Texas Instruments
  17. * Copyright (C) 2008 Nokia Corporation
  18. * Contact: Felipe Balbi <felipe.balbi@nokia.com>
  19. *
  20. * Author: Atin Malaviya (atin.malaviya@gmail.com)
  21. *
  22. * ------------------------------------------------------------------------
  23. *
  24. * SPDX-License-Identifier: GPL-2.0+
  25. */
  26. #include <twl4030.h>
  27. /* Defines for bits in registers */
  28. #define OPMODE_MASK (3 << 3)
  29. #define XCVRSELECT_MASK (3 << 0)
  30. #define CARKITMODE (1 << 2)
  31. #define OTG_ENAB (1 << 5)
  32. #define PHYPWD (1 << 0)
  33. #define CLOCKGATING_EN (1 << 2)
  34. #define CLK32K_EN (1 << 1)
  35. #define REQ_PHY_DPLL_CLK (1 << 0)
  36. #define PHY_DPLL_CLK (1 << 0)
  37. static int twl4030_usb_write(u8 address, u8 data)
  38. {
  39. int ret;
  40. ret = twl4030_i2c_write_u8(TWL4030_CHIP_USB, address, data);
  41. if (ret != 0)
  42. printf("TWL4030:USB:Write[0x%x] Error %d\n", address, ret);
  43. return ret;
  44. }
  45. static int twl4030_usb_read(u8 address)
  46. {
  47. u8 data;
  48. int ret;
  49. ret = twl4030_i2c_read_u8(TWL4030_CHIP_USB, address, &data);
  50. if (ret == 0)
  51. ret = data;
  52. else
  53. printf("TWL4030:USB:Read[0x%x] Error %d\n", address, ret);
  54. return ret;
  55. }
  56. static void twl4030_usb_ldo_init(void)
  57. {
  58. /* Enable writing to power configuration registers */
  59. twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
  60. TWL4030_PM_MASTER_PROTECT_KEY, 0xC0);
  61. twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
  62. TWL4030_PM_MASTER_PROTECT_KEY, 0x0C);
  63. /* put VUSB3V1 LDO in active state */
  64. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  65. TWL4030_PM_RECEIVER_VUSB_DEDICATED2, 0x00);
  66. /* input to VUSB3V1 LDO is from VBAT, not VBUS */
  67. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  68. TWL4030_PM_RECEIVER_VUSB_DEDICATED1, 0x14);
  69. /* turn on 3.1V regulator */
  70. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  71. TWL4030_PM_RECEIVER_VUSB3V1_DEV_GRP, 0x20);
  72. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  73. TWL4030_PM_RECEIVER_VUSB3V1_TYPE, 0x00);
  74. /* turn on 1.5V regulator */
  75. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  76. TWL4030_PM_RECEIVER_VUSB1V5_DEV_GRP, 0x20);
  77. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  78. TWL4030_PM_RECEIVER_VUSB1V5_TYPE, 0x00);
  79. /* turn on 1.8V regulator */
  80. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  81. TWL4030_PM_RECEIVER_VUSB1V8_DEV_GRP, 0x20);
  82. twl4030_i2c_write_u8(TWL4030_CHIP_PM_RECEIVER,
  83. TWL4030_PM_RECEIVER_VUSB1V8_TYPE, 0x00);
  84. /* disable access to power configuration registers */
  85. twl4030_i2c_write_u8(TWL4030_CHIP_PM_MASTER,
  86. TWL4030_PM_MASTER_PROTECT_KEY, 0x00);
  87. }
  88. static void twl4030_phy_power(void)
  89. {
  90. u8 pwr, clk;
  91. /* Power the PHY */
  92. pwr = twl4030_usb_read(TWL4030_USB_PHY_PWR_CTRL);
  93. pwr &= ~PHYPWD;
  94. twl4030_usb_write(TWL4030_USB_PHY_PWR_CTRL, pwr);
  95. /* Enable clocks */
  96. clk = twl4030_usb_read(TWL4030_USB_PHY_CLK_CTRL);
  97. clk |= CLOCKGATING_EN | CLK32K_EN;
  98. twl4030_usb_write(TWL4030_USB_PHY_CLK_CTRL, clk);
  99. }
  100. /*
  101. * Initiaze the ULPI interface
  102. * ULPI : Universal Transceiver Macrocell Low Pin Interface
  103. * An interface between the USB link controller like musb and the
  104. * the PHY or transceiver that drives the actual bus.
  105. */
  106. int twl4030_usb_ulpi_init(void)
  107. {
  108. long timeout = 1000 * 1000; /* 1 sec */;
  109. u8 clk, sts, pwr;
  110. /* twl4030 ldo init */
  111. twl4030_usb_ldo_init();
  112. /* Enable the twl4030 phy */
  113. twl4030_phy_power();
  114. /* Enable DPLL to access PHY registers over I2C */
  115. clk = twl4030_usb_read(TWL4030_USB_PHY_CLK_CTRL);
  116. clk |= REQ_PHY_DPLL_CLK;
  117. twl4030_usb_write(TWL4030_USB_PHY_CLK_CTRL, clk);
  118. /* Check if the PHY DPLL is locked */
  119. sts = twl4030_usb_read(TWL4030_USB_PHY_CLK_CTRL_STS);
  120. while (!(sts & PHY_DPLL_CLK) && 0 < timeout) {
  121. udelay(10);
  122. sts = twl4030_usb_read(TWL4030_USB_PHY_CLK_CTRL_STS);
  123. timeout -= 10;
  124. }
  125. /* Final check */
  126. sts = twl4030_usb_read(TWL4030_USB_PHY_CLK_CTRL_STS);
  127. if (!(sts & PHY_DPLL_CLK)) {
  128. printf("Error:TWL4030:USB Timeout setting PHY DPLL clock\n");
  129. return -1;
  130. }
  131. /*
  132. * There are two circuit blocks attached to the PHY,
  133. * Carkit and USB OTG. Disable Carkit and enable USB OTG
  134. */
  135. twl4030_usb_write(TWL4030_USB_IFC_CTRL_CLR, CARKITMODE);
  136. pwr = twl4030_usb_read(TWL4030_USB_POWER_CTRL);
  137. pwr |= OTG_ENAB;
  138. twl4030_usb_write(TWL4030_USB_POWER_CTRL_SET, pwr);
  139. /* Clear the opmode bits to ensure normal encode */
  140. twl4030_usb_write(TWL4030_USB_FUNC_CTRL_CLR, OPMODE_MASK);
  141. /* Clear the xcvrselect bits to enable the high speed transeiver */
  142. twl4030_usb_write(TWL4030_USB_FUNC_CTRL_CLR, XCVRSELECT_MASK);
  143. /* Let ULPI control the DPLL clock */
  144. clk = twl4030_usb_read(TWL4030_USB_PHY_CLK_CTRL);
  145. clk &= ~REQ_PHY_DPLL_CLK;
  146. twl4030_usb_write(TWL4030_USB_PHY_CLK_CTRL, clk);
  147. return 0;
  148. }