cvmx-helper-spi.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  1. /***********************license start***************
  2. * Author: Cavium Networks
  3. *
  4. * Contact: support@caviumnetworks.com
  5. * This file is part of the OCTEON SDK
  6. *
  7. * Copyright (c) 2003-2008 Cavium Networks
  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. void __cvmx_interrupt_gmxx_enable(int interface);
  28. void __cvmx_interrupt_spxx_int_msk_enable(int index);
  29. void __cvmx_interrupt_stxx_int_msk_enable(int index);
  30. /*
  31. * Functions for SPI initialization, configuration,
  32. * and monitoring.
  33. */
  34. #include <asm/octeon/octeon.h>
  35. #include "cvmx-config.h"
  36. #include "cvmx-spi.h"
  37. #include "cvmx-helper.h"
  38. #include "cvmx-pip-defs.h"
  39. #include "cvmx-pko-defs.h"
  40. /*
  41. * CVMX_HELPER_SPI_TIMEOUT is used to determine how long the SPI
  42. * initialization routines wait for SPI training. You can override the
  43. * value using executive-config.h if necessary.
  44. */
  45. #ifndef CVMX_HELPER_SPI_TIMEOUT
  46. #define CVMX_HELPER_SPI_TIMEOUT 10
  47. #endif
  48. /**
  49. * Probe a SPI interface and determine the number of ports
  50. * connected to it. The SPI interface should still be down after
  51. * this call.
  52. *
  53. * @interface: Interface to probe
  54. *
  55. * Returns Number of ports on the interface. Zero to disable.
  56. */
  57. int __cvmx_helper_spi_probe(int interface)
  58. {
  59. int num_ports = 0;
  60. if ((cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) &&
  61. cvmx_spi4000_is_present(interface)) {
  62. num_ports = 10;
  63. } else {
  64. union cvmx_pko_reg_crc_enable enable;
  65. num_ports = 16;
  66. /*
  67. * Unlike the SPI4000, most SPI devices don't
  68. * automatically put on the L2 CRC. For everything
  69. * except for the SPI4000 have PKO append the L2 CRC
  70. * to the packet.
  71. */
  72. enable.u64 = cvmx_read_csr(CVMX_PKO_REG_CRC_ENABLE);
  73. enable.s.enable |= 0xffff << (interface * 16);
  74. cvmx_write_csr(CVMX_PKO_REG_CRC_ENABLE, enable.u64);
  75. }
  76. __cvmx_helper_setup_gmx(interface, num_ports);
  77. return num_ports;
  78. }
  79. /**
  80. * Bringup and enable a SPI interface. After this call packet I/O
  81. * should be fully functional. This is called with IPD enabled but
  82. * PKO disabled.
  83. *
  84. * @interface: Interface to bring up
  85. *
  86. * Returns Zero on success, negative on failure
  87. */
  88. int __cvmx_helper_spi_enable(int interface)
  89. {
  90. /*
  91. * Normally the ethernet L2 CRC is checked and stripped in the
  92. * GMX block. When you are using SPI, this isn' the case and
  93. * IPD needs to check the L2 CRC.
  94. */
  95. int num_ports = cvmx_helper_ports_on_interface(interface);
  96. int ipd_port;
  97. for (ipd_port = interface * 16; ipd_port < interface * 16 + num_ports;
  98. ipd_port++) {
  99. union cvmx_pip_prt_cfgx port_config;
  100. port_config.u64 = cvmx_read_csr(CVMX_PIP_PRT_CFGX(ipd_port));
  101. port_config.s.crc_en = 1;
  102. cvmx_write_csr(CVMX_PIP_PRT_CFGX(ipd_port), port_config.u64);
  103. }
  104. if (cvmx_sysinfo_get()->board_type != CVMX_BOARD_TYPE_SIM) {
  105. cvmx_spi_start_interface(interface, CVMX_SPI_MODE_DUPLEX,
  106. CVMX_HELPER_SPI_TIMEOUT, num_ports);
  107. if (cvmx_spi4000_is_present(interface))
  108. cvmx_spi4000_initialize(interface);
  109. }
  110. __cvmx_interrupt_spxx_int_msk_enable(interface);
  111. __cvmx_interrupt_stxx_int_msk_enable(interface);
  112. __cvmx_interrupt_gmxx_enable(interface);
  113. return 0;
  114. }
  115. /**
  116. * Return the link state of an IPD/PKO port as returned by
  117. * auto negotiation. The result of this function may not match
  118. * Octeon's link config if auto negotiation has changed since
  119. * the last call to cvmx_helper_link_set().
  120. *
  121. * @ipd_port: IPD/PKO port to query
  122. *
  123. * Returns Link state
  124. */
  125. cvmx_helper_link_info_t __cvmx_helper_spi_link_get(int ipd_port)
  126. {
  127. cvmx_helper_link_info_t result;
  128. int interface = cvmx_helper_get_interface_num(ipd_port);
  129. int index = cvmx_helper_get_interface_index_num(ipd_port);
  130. result.u64 = 0;
  131. if (cvmx_sysinfo_get()->board_type == CVMX_BOARD_TYPE_SIM) {
  132. /* The simulator gives you a simulated full duplex link */
  133. result.s.link_up = 1;
  134. result.s.full_duplex = 1;
  135. result.s.speed = 10000;
  136. } else if (cvmx_spi4000_is_present(interface)) {
  137. union cvmx_gmxx_rxx_rx_inbnd inband =
  138. cvmx_spi4000_check_speed(interface, index);
  139. result.s.link_up = inband.s.status;
  140. result.s.full_duplex = inband.s.duplex;
  141. switch (inband.s.speed) {
  142. case 0: /* 10 Mbps */
  143. result.s.speed = 10;
  144. break;
  145. case 1: /* 100 Mbps */
  146. result.s.speed = 100;
  147. break;
  148. case 2: /* 1 Gbps */
  149. result.s.speed = 1000;
  150. break;
  151. case 3: /* Illegal */
  152. result.s.speed = 0;
  153. result.s.link_up = 0;
  154. break;
  155. }
  156. } else {
  157. /* For generic SPI we can't determine the link, just return some
  158. sane results */
  159. result.s.link_up = 1;
  160. result.s.full_duplex = 1;
  161. result.s.speed = 10000;
  162. }
  163. return result;
  164. }
  165. /**
  166. * Configure an IPD/PKO port for the specified link state. This
  167. * function does not influence auto negotiation at the PHY level.
  168. * The passed link state must always match the link state returned
  169. * by cvmx_helper_link_get(). It is normally best to use
  170. * cvmx_helper_link_autoconf() instead.
  171. *
  172. * @ipd_port: IPD/PKO port to configure
  173. * @link_info: The new link state
  174. *
  175. * Returns Zero on success, negative on failure
  176. */
  177. int __cvmx_helper_spi_link_set(int ipd_port, cvmx_helper_link_info_t link_info)
  178. {
  179. /* Nothing to do. If we have a SPI4000 then the setup was already performed
  180. by cvmx_spi4000_check_speed(). If not then there isn't any link
  181. info */
  182. return 0;
  183. }