octeon2-common.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201
  1. /*
  2. * This file is subject to the terms and conditions of the GNU General Public
  3. * License. See the file "COPYING" in the main directory of this archive
  4. * for more details.
  5. *
  6. * Copyright (C) 2010, 2011 Cavium Networks
  7. */
  8. #include <linux/module.h>
  9. #include <linux/mutex.h>
  10. #include <linux/delay.h>
  11. #include <asm/octeon/octeon.h>
  12. #include <asm/octeon/cvmx-uctlx-defs.h>
  13. static DEFINE_MUTEX(octeon2_usb_clocks_mutex);
  14. static int octeon2_usb_clock_start_cnt;
  15. void octeon2_usb_clocks_start(void)
  16. {
  17. u64 div;
  18. union cvmx_uctlx_if_ena if_ena;
  19. union cvmx_uctlx_clk_rst_ctl clk_rst_ctl;
  20. union cvmx_uctlx_uphy_ctl_status uphy_ctl_status;
  21. union cvmx_uctlx_uphy_portx_ctl_status port_ctl_status;
  22. int i;
  23. unsigned long io_clk_64_to_ns;
  24. mutex_lock(&octeon2_usb_clocks_mutex);
  25. octeon2_usb_clock_start_cnt++;
  26. if (octeon2_usb_clock_start_cnt != 1)
  27. goto exit;
  28. io_clk_64_to_ns = 64000000000ull / octeon_get_io_clock_rate();
  29. /*
  30. * Step 1: Wait for voltages stable. That surely happened
  31. * before starting the kernel.
  32. *
  33. * Step 2: Enable SCLK of UCTL by writing UCTL0_IF_ENA[EN] = 1
  34. */
  35. if_ena.u64 = 0;
  36. if_ena.s.en = 1;
  37. cvmx_write_csr(CVMX_UCTLX_IF_ENA(0), if_ena.u64);
  38. /* Step 3: Configure the reference clock, PHY, and HCLK */
  39. clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));
  40. /*
  41. * If the UCTL looks like it has already been started, skip
  42. * the initialization, otherwise bus errors are obtained.
  43. */
  44. if (clk_rst_ctl.s.hrst)
  45. goto end_clock;
  46. /* 3a */
  47. clk_rst_ctl.s.p_por = 1;
  48. clk_rst_ctl.s.hrst = 0;
  49. clk_rst_ctl.s.p_prst = 0;
  50. clk_rst_ctl.s.h_clkdiv_rst = 0;
  51. clk_rst_ctl.s.o_clkdiv_rst = 0;
  52. clk_rst_ctl.s.h_clkdiv_en = 0;
  53. clk_rst_ctl.s.o_clkdiv_en = 0;
  54. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  55. /* 3b */
  56. /* 12MHz crystal. */
  57. clk_rst_ctl.s.p_refclk_sel = 0;
  58. clk_rst_ctl.s.p_refclk_div = 0;
  59. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  60. /* 3c */
  61. div = octeon_get_io_clock_rate() / 130000000ull;
  62. switch (div) {
  63. case 0:
  64. div = 1;
  65. break;
  66. case 1:
  67. case 2:
  68. case 3:
  69. case 4:
  70. break;
  71. case 5:
  72. div = 4;
  73. break;
  74. case 6:
  75. case 7:
  76. div = 6;
  77. break;
  78. case 8:
  79. case 9:
  80. case 10:
  81. case 11:
  82. div = 8;
  83. break;
  84. default:
  85. div = 12;
  86. break;
  87. }
  88. clk_rst_ctl.s.h_div = div;
  89. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  90. /* Read it back, */
  91. clk_rst_ctl.u64 = cvmx_read_csr(CVMX_UCTLX_CLK_RST_CTL(0));
  92. clk_rst_ctl.s.h_clkdiv_en = 1;
  93. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  94. /* 3d */
  95. clk_rst_ctl.s.h_clkdiv_rst = 1;
  96. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  97. /* 3e: delay 64 io clocks */
  98. ndelay(io_clk_64_to_ns);
  99. /*
  100. * Step 4: Program the power-on reset field in the UCTL
  101. * clock-reset-control register.
  102. */
  103. clk_rst_ctl.s.p_por = 0;
  104. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  105. /* Step 5: Wait 1 ms for the PHY clock to start. */
  106. mdelay(1);
  107. /*
  108. * Step 6: Program the reset input from automatic test
  109. * equipment field in the UPHY CSR
  110. */
  111. uphy_ctl_status.u64 = cvmx_read_csr(CVMX_UCTLX_UPHY_CTL_STATUS(0));
  112. uphy_ctl_status.s.ate_reset = 1;
  113. cvmx_write_csr(CVMX_UCTLX_UPHY_CTL_STATUS(0), uphy_ctl_status.u64);
  114. /* Step 7: Wait for at least 10ns. */
  115. ndelay(10);
  116. /* Step 8: Clear the ATE_RESET field in the UPHY CSR. */
  117. uphy_ctl_status.s.ate_reset = 0;
  118. cvmx_write_csr(CVMX_UCTLX_UPHY_CTL_STATUS(0), uphy_ctl_status.u64);
  119. /*
  120. * Step 9: Wait for at least 20ns for UPHY to output PHY clock
  121. * signals and OHCI_CLK48
  122. */
  123. ndelay(20);
  124. /* Step 10: Configure the OHCI_CLK48 and OHCI_CLK12 clocks. */
  125. /* 10a */
  126. clk_rst_ctl.s.o_clkdiv_rst = 1;
  127. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  128. /* 10b */
  129. clk_rst_ctl.s.o_clkdiv_en = 1;
  130. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  131. /* 10c */
  132. ndelay(io_clk_64_to_ns);
  133. /*
  134. * Step 11: Program the PHY reset field:
  135. * UCTL0_CLK_RST_CTL[P_PRST] = 1
  136. */
  137. clk_rst_ctl.s.p_prst = 1;
  138. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  139. /* Step 12: Wait 1 uS. */
  140. udelay(1);
  141. /* Step 13: Program the HRESET_N field: UCTL0_CLK_RST_CTL[HRST] = 1 */
  142. clk_rst_ctl.s.hrst = 1;
  143. cvmx_write_csr(CVMX_UCTLX_CLK_RST_CTL(0), clk_rst_ctl.u64);
  144. end_clock:
  145. /* Now we can set some other registers. */
  146. for (i = 0; i <= 1; i++) {
  147. port_ctl_status.u64 =
  148. cvmx_read_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0));
  149. /* Set txvreftune to 15 to obtain compliant 'eye' diagram. */
  150. port_ctl_status.s.txvreftune = 15;
  151. port_ctl_status.s.txrisetune = 1;
  152. port_ctl_status.s.txpreemphasistune = 1;
  153. cvmx_write_csr(CVMX_UCTLX_UPHY_PORTX_CTL_STATUS(i, 0),
  154. port_ctl_status.u64);
  155. }
  156. /* Set uSOF cycle period to 60,000 bits. */
  157. cvmx_write_csr(CVMX_UCTLX_EHCI_FLA(0), 0x20ull);
  158. exit:
  159. mutex_unlock(&octeon2_usb_clocks_mutex);
  160. }
  161. EXPORT_SYMBOL(octeon2_usb_clocks_start);
  162. void octeon2_usb_clocks_stop(void)
  163. {
  164. mutex_lock(&octeon2_usb_clocks_mutex);
  165. octeon2_usb_clock_start_cnt--;
  166. mutex_unlock(&octeon2_usb_clocks_mutex);
  167. }
  168. EXPORT_SYMBOL(octeon2_usb_clocks_stop);