ohci-lpc32xx.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. /*
  2. * Copyright (C) 2008 by NXP Semiconductors
  3. * @Author: Based on code by Kevin Wells
  4. * @Descr: USB driver - Embedded Artists LPC3250 OEM Board support functions
  5. *
  6. * Copyright (c) 2015 Tyco Fire Protection Products.
  7. *
  8. * SPDX-License-Identifier: GPL-2.0+
  9. */
  10. #include <common.h>
  11. #include <errno.h>
  12. #include <wait_bit.h>
  13. #include <asm/io.h>
  14. #include <asm/arch/cpu.h>
  15. #include <asm/arch/clk.h>
  16. #include <usb.h>
  17. #include <i2c.h>
  18. /* OTG I2C controller module register structures */
  19. struct otgi2c_regs {
  20. u32 otg_i2c_txrx; /* OTG I2C Tx/Rx Data FIFO */
  21. u32 otg_i2c_stat; /* OTG I2C Status Register */
  22. u32 otg_i2c_ctrl; /* OTG I2C Control Register */
  23. u32 otg_i2c_clk_hi; /* OTG I2C Clock Divider high */
  24. u32 otg_i2c_clk_lo; /* OTG I2C Clock Divider low */
  25. };
  26. /* OTG controller module register structures */
  27. struct otg_regs {
  28. u32 reserved1[64];
  29. u32 otg_int_sts; /* OTG int status register */
  30. u32 otg_int_enab; /* OTG int enable register */
  31. u32 otg_int_set; /* OTG int set register */
  32. u32 otg_int_clr; /* OTG int clear register */
  33. u32 otg_sts_ctrl; /* OTG status/control register */
  34. u32 otg_timer; /* OTG timer register */
  35. u32 reserved2[122];
  36. struct otgi2c_regs otg_i2c;
  37. u32 reserved3[824];
  38. u32 otg_clk_ctrl; /* OTG clock control reg */
  39. u32 otg_clk_sts; /* OTG clock status reg */
  40. };
  41. /* otg_sts_ctrl register definitions */
  42. #define OTG_HOST_EN (1 << 0) /* Enable host mode */
  43. /* otg_clk_ctrl and otg_clk_sts register definitions */
  44. #define OTG_CLK_AHB_EN (1 << 4) /* Enable AHB clock */
  45. #define OTG_CLK_OTG_EN (1 << 3) /* Enable OTG clock */
  46. #define OTG_CLK_I2C_EN (1 << 2) /* Enable I2C clock */
  47. #define OTG_CLK_HOST_EN (1 << 0) /* Enable host clock */
  48. /* ISP1301 USB transceiver I2C registers */
  49. #define MC1_SPEED_REG (1 << 0)
  50. #define MC1_DAT_SE0 (1 << 2)
  51. #define MC1_UART_EN (1 << 6)
  52. #define MC2_SPD_SUSP_CTRL (1 << 1)
  53. #define MC2_BI_DI (1 << 2)
  54. #define MC2_PSW_EN (1 << 6)
  55. #define OTG1_DP_PULLUP (1 << 0)
  56. #define OTG1_DM_PULLUP (1 << 1)
  57. #define OTG1_DP_PULLDOWN (1 << 2)
  58. #define OTG1_DM_PULLDOWN (1 << 3)
  59. #define OTG1_VBUS_DRV (1 << 5)
  60. #define ISP1301_I2C_ADDR CONFIG_USB_ISP1301_I2C_ADDR
  61. #define ISP1301_I2C_MODE_CONTROL_1_SET 0x04
  62. #define ISP1301_I2C_MODE_CONTROL_1_CLR 0x05
  63. #define ISP1301_I2C_MODE_CONTROL_2_SET 0x12
  64. #define ISP1301_I2C_MODE_CONTROL_2_CLR 0x13
  65. #define ISP1301_I2C_OTG_CONTROL_1_SET 0x06
  66. #define ISP1301_I2C_OTG_CONTROL_1_CLR 0x07
  67. #define ISP1301_I2C_INTERRUPT_LATCH_CLR 0x0B
  68. #define ISP1301_I2C_INTERRUPT_FALLING_CLR 0x0D
  69. #define ISP1301_I2C_INTERRUPT_RISING_CLR 0x0F
  70. static struct otg_regs *otg = (struct otg_regs *)USB_BASE;
  71. static struct clk_pm_regs *clk_pwr = (struct clk_pm_regs *)CLK_PM_BASE;
  72. static int isp1301_set_value(int reg, u8 value)
  73. {
  74. return i2c_write(ISP1301_I2C_ADDR, reg, 1, &value, 1);
  75. }
  76. static void isp1301_configure(void)
  77. {
  78. i2c_set_bus_num(I2C_2);
  79. /*
  80. * LPC32XX only supports DAT_SE0 USB mode
  81. * This sequence is important
  82. */
  83. /* Disable transparent UART mode first */
  84. isp1301_set_value(ISP1301_I2C_MODE_CONTROL_1_CLR, MC1_UART_EN);
  85. isp1301_set_value(ISP1301_I2C_MODE_CONTROL_1_CLR, ~MC1_SPEED_REG);
  86. isp1301_set_value(ISP1301_I2C_MODE_CONTROL_1_SET, MC1_SPEED_REG);
  87. isp1301_set_value(ISP1301_I2C_MODE_CONTROL_2_CLR, ~0);
  88. isp1301_set_value(ISP1301_I2C_MODE_CONTROL_2_SET,
  89. MC2_BI_DI | MC2_PSW_EN | MC2_SPD_SUSP_CTRL);
  90. isp1301_set_value(ISP1301_I2C_OTG_CONTROL_1_CLR, ~0);
  91. isp1301_set_value(ISP1301_I2C_MODE_CONTROL_1_SET, MC1_DAT_SE0);
  92. isp1301_set_value(ISP1301_I2C_OTG_CONTROL_1_SET,
  93. OTG1_DM_PULLDOWN | OTG1_DP_PULLDOWN);
  94. isp1301_set_value(ISP1301_I2C_OTG_CONTROL_1_CLR,
  95. OTG1_DM_PULLUP | OTG1_DP_PULLUP);
  96. isp1301_set_value(ISP1301_I2C_INTERRUPT_LATCH_CLR, ~0);
  97. isp1301_set_value(ISP1301_I2C_INTERRUPT_FALLING_CLR, ~0);
  98. isp1301_set_value(ISP1301_I2C_INTERRUPT_RISING_CLR, ~0);
  99. /* Enable usb_need_clk clock after transceiver is initialized */
  100. setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_USBDVND_EN);
  101. }
  102. static int usbpll_setup(void)
  103. {
  104. u32 ret;
  105. /* make sure clocks are disabled */
  106. clrbits_le32(&clk_pwr->usb_ctrl,
  107. CLK_USBCTRL_CLK_EN1 | CLK_USBCTRL_CLK_EN2);
  108. /* start PLL clock input */
  109. setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_CLK_EN1);
  110. /* Setup PLL. */
  111. setbits_le32(&clk_pwr->usb_ctrl,
  112. CLK_USBCTRL_FDBK_PLUS1(192 - 1));
  113. setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_POSTDIV_2POW(0x01));
  114. setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_PLL_PWRUP);
  115. ret = wait_for_bit(__func__, &clk_pwr->usb_ctrl, CLK_USBCTRL_PLL_STS,
  116. true, CONFIG_SYS_HZ, false);
  117. if (ret)
  118. return ret;
  119. /* enable PLL output */
  120. setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_CLK_EN2);
  121. return 0;
  122. }
  123. int usb_cpu_init(void)
  124. {
  125. u32 ret;
  126. /*
  127. * USB pins routing setup is done by "lpc32xx_usb_init()" and should
  128. * be call by board "board_init()" or "misc_init_r()" functions.
  129. */
  130. /* enable AHB slave USB clock */
  131. setbits_le32(&clk_pwr->usb_ctrl,
  132. CLK_USBCTRL_HCLK_EN | CLK_USBCTRL_BUS_KEEPER);
  133. /* enable I2C clock */
  134. writel(OTG_CLK_I2C_EN, &otg->otg_clk_ctrl);
  135. ret = wait_for_bit(__func__, &otg->otg_clk_sts, OTG_CLK_I2C_EN, true,
  136. CONFIG_SYS_HZ, false);
  137. if (ret)
  138. return ret;
  139. /* Configure ISP1301 */
  140. isp1301_configure();
  141. /* setup USB clocks and PLL */
  142. ret = usbpll_setup();
  143. if (ret)
  144. return ret;
  145. /* enable usb_host_need_clk */
  146. setbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_USBHSTND_EN);
  147. /* enable all needed USB clocks */
  148. const u32 mask = OTG_CLK_AHB_EN | OTG_CLK_OTG_EN |
  149. OTG_CLK_I2C_EN | OTG_CLK_HOST_EN;
  150. writel(mask, &otg->otg_clk_ctrl);
  151. ret = wait_for_bit(__func__, &otg->otg_clk_sts, mask, true,
  152. CONFIG_SYS_HZ, false);
  153. if (ret)
  154. return ret;
  155. setbits_le32(&otg->otg_sts_ctrl, OTG_HOST_EN);
  156. isp1301_set_value(ISP1301_I2C_OTG_CONTROL_1_SET, OTG1_VBUS_DRV);
  157. return 0;
  158. }
  159. int usb_cpu_stop(void)
  160. {
  161. /* vbus off */
  162. isp1301_set_value(ISP1301_I2C_OTG_CONTROL_1_SET, OTG1_VBUS_DRV);
  163. clrbits_le32(&otg->otg_sts_ctrl, OTG_HOST_EN);
  164. clrbits_le32(&clk_pwr->usb_ctrl, CLK_USBCTRL_HCLK_EN);
  165. return 0;
  166. }
  167. int usb_cpu_init_fail(void)
  168. {
  169. return usb_cpu_stop();
  170. }