octeon2-common.c 4.5 KB

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