twl4030.c 5.0 KB

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