twl4030.c 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. /*
  2. * Copyright (c) 2008 Travis Geiselbrecht
  3. *
  4. * Permission is hereby granted, free of charge, to any person obtaining
  5. * a copy of this software and associated documentation files
  6. * (the "Software"), to deal in the Software without restriction,
  7. * including without limitation the rights to use, copy, modify, merge,
  8. * publish, distribute, sublicense, and/or sell copies of the Software,
  9. * and to permit persons to whom the Software is furnished to do so,
  10. * subject to the following conditions:
  11. *
  12. * The above copyright notice and this permission notice shall be
  13. * included in all copies or substantial portions of the Software.
  14. *
  15. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  16. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  17. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
  18. * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
  19. * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
  20. * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
  21. * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  22. */
  23. #include <debug.h>
  24. #include <sys/types.h>
  25. #include <dev/i2c.h>
  26. #include <dev/twl4030.h>
  27. #include "twl4030_hw.h"
  28. // XXX move to target specific setup
  29. #define TWL_I2C_BUS 0
  30. void twl4030_init(void)
  31. {
  32. }
  33. static int twl4030_usb_write(uint8_t address, uint8_t data)
  34. {
  35. return i2c_write_reg(TWL_I2C_BUS, TWL_USB_ADDR, address, data);
  36. }
  37. static int twl4030_usb_read(uint8_t address)
  38. {
  39. uint8_t data;
  40. int err = i2c_read_reg(TWL_I2C_BUS, TWL_USB_ADDR, address, &data);
  41. if (err < 0)
  42. return err;
  43. return data;
  44. }
  45. static int twl4030_usb_set_bits(uint8_t reg, uint8_t bits)
  46. {
  47. return twl4030_usb_write(reg + 1, bits);
  48. }
  49. static int twl4030_usb_clear_bits(uint8_t reg, uint8_t bits)
  50. {
  51. return twl4030_usb_write(reg + 2, bits);
  52. }
  53. static void twl4030_i2c_access(bool on)
  54. {
  55. int val;
  56. if ((val = twl4030_usb_read(PHY_CLK_CTRL)) >= 0) {
  57. if (on) {
  58. /* enable DPLL to access PHY registers over I2C */
  59. val |= REQ_PHY_DPLL_CLK;
  60. twl4030_usb_write(PHY_CLK_CTRL, (uint8_t)val);
  61. while (!(twl4030_usb_read(PHY_CLK_CTRL_STS) & PHY_DPLL_CLK)) {
  62. spin(10);
  63. }
  64. if (!(twl4030_usb_read(PHY_CLK_CTRL_STS) & PHY_DPLL_CLK))
  65. printf("Timeout setting T2 HSUSB " "PHY DPLL clock\n");
  66. } else {
  67. /* let ULPI control the DPLL clock */
  68. val &= ~REQ_PHY_DPLL_CLK;
  69. twl4030_usb_write(PHY_CLK_CTRL, (uint8_t)val);
  70. }
  71. }
  72. return;
  73. }
  74. int twl4030_usb_reset(void)
  75. {
  76. TRACE_ENTRY;
  77. #if 0
  78. twl4030_usb_clear_bits(OTG_CTRL, DMPULLDOWN | DPPULLDOWN);
  79. twl4030_usb_clear_bits(USB_INT_EN_RISE, ~0);
  80. twl4030_usb_clear_bits(USB_INT_EN_FALL, ~0);
  81. twl4030_usb_clear_bits(MCPC_IO_CTRL, ~TXDTYP);
  82. twl4030_usb_set_bits(MCPC_IO_CTRL, TXDTYP);
  83. twl4030_usb_clear_bits(OTHER_FUNC_CTRL, (BDIS_ACON_EN | FIVEWIRE_MODE));
  84. twl4030_usb_clear_bits(OTHER_IFC_CTRL, ~0);
  85. twl4030_usb_clear_bits(OTHER_INT_EN_RISE, ~0);
  86. twl4030_usb_clear_bits(OTHER_INT_EN_FALL, ~0);
  87. twl4030_usb_clear_bits(OTHER_IFC_CTRL2, ~0);
  88. twl4030_usb_clear_bits(REG_CTRL_EN, ULPI_I2C_CONFLICT_INTEN);
  89. twl4030_usb_clear_bits(OTHER_FUNC_CTRL2, VBAT_TIMER_EN);
  90. #endif
  91. /* Enable writing to power configuration registers */
  92. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, PROTECT_KEY, 0xC0);
  93. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, PROTECT_KEY, 0x0C);
  94. /* put VUSB3V1 LDO in active state */
  95. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB_DEDICATED2, 0);
  96. /* input to VUSB3V1 LDO is from VBAT, not VBUS */
  97. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB_DEDICATED1, 0x14);
  98. /* turn on 3.1V regulator */
  99. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB3V1_DEV_GRP, 0x20);
  100. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB3V1_TYPE, 0);
  101. /* turn on 1.5V regulator */
  102. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB1V5_DEV_GRP, 0x20);
  103. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB1V5_TYPE, 0);
  104. /* turn on 1.8V regulator */
  105. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB1V8_DEV_GRP, 0x20);
  106. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, VUSB1V8_TYPE, 0);
  107. /* disable access to power configuration registers */
  108. i2c_write_reg(TWL_I2C_BUS, TWL_PM_RECEIVER_ADDR, PROTECT_KEY, 0);
  109. /* turn on the phy */
  110. uint8_t pwr = twl4030_usb_read(PHY_PWR_CTRL);
  111. pwr &= ~PHYPWD;
  112. twl4030_usb_write(PHY_PWR_CTRL, pwr);
  113. twl4030_usb_write(PHY_CLK_CTRL,
  114. twl4030_usb_read(PHY_CLK_CTRL) |
  115. (CLOCKGATING_EN | CLK32K_EN));
  116. /* set DPLL i2c access mode */
  117. twl4030_i2c_access(true);
  118. /* set ulpi mode */
  119. twl4030_usb_clear_bits(IFC_CTRL, CARKITMODE);
  120. twl4030_usb_set_bits(POWER_CTRL, OTG_ENAB);
  121. twl4030_usb_write(FUNC_CTRL, XCVRSELECT_HS); // set high speed mode
  122. // twl4030_usb_write(FUNC_CTRL, XCVRSELECT_FS); // set full speed mode
  123. twl4030_i2c_access(false);
  124. return 0;
  125. }
  126. int twl4030_init_hs(void)
  127. {
  128. return 0;
  129. }
  130. int twl4030_set_usb_pullup(bool pullup)
  131. {
  132. TRACE_ENTRY;
  133. if (pullup) {
  134. twl4030_usb_clear_bits(OTG_CTRL, DPPULLDOWN);
  135. twl4030_usb_set_bits(FUNC_CTRL, TERMSELECT);
  136. } else {
  137. twl4030_usb_clear_bits(FUNC_CTRL, TERMSELECT);
  138. twl4030_usb_set_bits(OTG_CTRL, DPPULLDOWN);
  139. }
  140. return 0;
  141. }