cvmx-helper-spi.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: [email protected]
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (C) 2003-2018 Cavium, Inc.
  8. *
  9. * This file is free software; you can redistribute it and/or modify
  10. * it under the terms of the GNU General Public License, Version 2, as
  11. * published by the Free Software Foundation.
  12. *
  13. * This file is distributed in the hope that it will be useful, but
  14. * AS-IS and WITHOUT ANY WARRANTY; without even the implied warranty
  15. * of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE, TITLE, or
  16. * NONINFRINGEMENT. See the GNU General Public License for more
  17. * details.
  18. *
  19. * You should have received a copy of the GNU General Public License
  20. * along with this file; if not, write to the Free Software
  21. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
  22. * or visit http://www.gnu.org/licenses/.
  23. *
  24. * This file may also be available under a different license from Cavium.
  25. * Contact Cavium Networks for more information
  26. ***********************license end**************************************/
  27. /*
  28. * Functions for SPI initialization, configuration,
  29. * and monitoring.
  30. */
  31. #include <asm/octeon/octeon.h>
  32. #include <asm/octeon/cvmx-config.h>
  33. #include <asm/octeon/cvmx-spi.h>
  34. #include <asm/octeon/cvmx-helper.h>
  35. #include <asm/octeon/cvmx-pip-defs.h>
  36. #include <asm/octeon/cvmx-pko-defs.h>
  37. #include <asm/octeon/cvmx-spxx-defs.h>
  38. #include <asm/octeon/cvmx-stxx-defs.h>
  39. /*
  40. * CVMX_HELPER_SPI_TIMEOUT is used to determine how long the SPI
  41. * initialization routines wait for SPI training. You can override the
  42. * value using executive-config.h if necessary.
  43. */
  44. #ifndef CVMX_HELPER_SPI_TIMEOUT
  45. #define CVMX_HELPER_SPI_TIMEOUT 10
  46. #endif
  47. int __cvmx_helper_spi_enumerate(int interface)
  48. {
  49. if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
  50. cvmx_spi4000_is_present(interface)) {
  51. return 10;
  52. } else {
  53. return 16;
  54. }
  55. }
  56. /**
  57. * Probe a SPI interface and determine the number of ports
  58. * connected to it. The SPI interface should still be down after
  59. * this call.
  60. *
  61. * @interface: Interface to probe
  62. *
  63. * Returns Number of ports on the interface. Zero to disable.
  64. */
  65. int __cvmx_helper_spi_probe(int interface)
  66. {
  67. int num_ports = 0;
  68. if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
  69. cvmx_spi4000_is_present(interface)) {
  70. num_ports = 10;
  71. } else {
  72. union cvmx_pko_reg_crc_enable enable;
  73. num_ports = 16;
  74. /*
  75. * Unlike the SPI4000, most SPI devices don't
  76. * automatically put on the L2 CRC. For everything
  77. * except for the SPI4000 have PKO append the L2 CRC
  78. * to the packet.
  79. */
  80. enable.u64 = cvmx_read_csr(CVMX_PKO_REG_CRC_ENABLE);
  81. enable.s.enable |= 0xffff << (interface * 16);
  82. cvmx_write_csr(CVMX_PKO_REG_CRC_ENABLE, enable.u64);
  83. }
  84. __cvmx_helper_setup_gmx(interface, num_ports);
  85. return num_ports;
  86. }
  87. /**
  88. * Bringup and enable a SPI interface. After this call packet I/O
  89. * should be fully functional. This is called with IPD enabled but
  90. * PKO disabled.
  91. *
  92. * @interface: Interface to bring up
  93. *
  94. * Returns Zero on success, negative on failure
  95. */
  96. int __cvmx_helper_spi_enable(int interface)
  97. {
  98. /*
  99. * Normally the ethernet L2 CRC is checked and stripped in the
  100. * GMX block. When you are using SPI, this isn' the case and
  101. * IPD needs to check the L2 CRC.
  102. */
  103. int num_ports = cvmx_helper_ports_on_interface(interface);
  104. int ipd_port;
  105. for (ipd_port = interface * 16; ipd_port < interface * 16 + num_ports;
  106. ipd_port++) {
  107. union cvmx_pip_prt_cfgx port_config;
  108. port_config.u64 = cvmx_read_csr(CVMX_PIP_PRT_CFGX(ipd_port));
  109. port_config.s.crc_en = 1;
  110. cvmx_write_csr(CVMX_PIP_PRT_CFGX(ipd_port), port_config.u64);
  111. }
  112. if (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) {
  113. cvmx_spi_start_interface(interface, CVMX_SPI_MODE_DUPLEX,
  114. CVMX_HELPER_SPI_TIMEOUT, num_ports);
  115. if (cvmx_spi4000_is_present(interface))
  116. cvmx_spi4000_initialize(interface);
  117. }
  118. __cvmx_interrupt_spxx_int_msk_enable(interface);
  119. __cvmx_interrupt_stxx_int_msk_enable(interface);
  120. __cvmx_interrupt_gmxx_enable(interface);
  121. return 0;
  122. }
  123. /**
  124. * Return the link state of an IPD/PKO port as returned by
  125. * auto negotiation. The result of this function may not match
  126. * Octeon's link config if auto negotiation has changed since
  127. * the last call to cvmx_helper_link_set().
  128. *
  129. * @ipd_port: IPD/PKO port to query
  130. *
  131. * Returns Link state
  132. */
  133. union cvmx_helper_link_info __cvmx_helper_spi_link_get(int ipd_port)
  134. {
  135. union cvmx_helper_link_info result;
  136. int interface = cvmx_helper_get_interface_num(ipd_port);
  137. int index = cvmx_helper_get_interface_index_num(ipd_port);
  138. result.u64 = 0;
  139. if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM) {
  140. /* The simulator gives you a simulated full duplex link */
  141. result.s.link_up = 1;
  142. result.s.full_duplex = 1;
  143. result.s.speed = 10000;
  144. } else if (cvmx_spi4000_is_present(interface)) {
  145. union cvmx_gmxx_rxx_rx_inbnd inband =
  146. cvmx_spi4000_check_speed(interface, index);
  147. result.s.link_up = inband.s.status;
  148. result.s.full_duplex = inband.s.duplex;
  149. switch (inband.s.speed) {
  150. case 0: /* 10 Mbps */
  151. result.s.speed = 10;
  152. break;
  153. case 1: /* 100 Mbps */
  154. result.s.speed = 100;
  155. break;
  156. case 2: /* 1 Gbps */
  157. result.s.speed = 1000;
  158. break;
  159. case 3: /* Illegal */
  160. result.s.speed = 0;
  161. result.s.link_up = 0;
  162. break;
  163. }
  164. } else {
  165. /* For generic SPI we can't determine the link, just return some
  166. sane results */
  167. result.s.link_up = 1;
  168. result.s.full_duplex = 1;
  169. result.s.speed = 10000;
  170. }
  171. return result;
  172. }
  173. /**
  174. * Configure an IPD/PKO port for the specified link state. This
  175. * function does not influence auto negotiation at the PHY level.
  176. * The passed link state must always match the link state returned
  177. * by cvmx_helper_link_get().
  178. *
  179. * @ipd_port: IPD/PKO port to configure
  180. * @link_info: The new link state
  181. *
  182. * Returns Zero on success, negative on failure
  183. */
  184. int __cvmx_helper_spi_link_set(int ipd_port, union cvmx_helper_link_info link_info)
  185. {
  186. /* Nothing to do. If we have a SPI4000 then the setup was already performed
  187. by cvmx_spi4000_check_speed(). If not then there isn't any link
  188. info */
  189. return 0;
  190. }