ethernet-spi.c 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238
  1. /*
  2. * This file is based on code from OCTEON SDK by Cavium Networks.
  3. *
  4. * Copyright (c) 2003-2007 Cavium Networks
  5. *
  6. * This file is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License, Version 2, as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/kernel.h>
  11. #include <linux/netdevice.h>
  12. #include <linux/interrupt.h>
  13. #include <net/dst.h>
  14. #include <asm/octeon/octeon.h>
  15. #include "ethernet-defines.h"
  16. #include "octeon-ethernet.h"
  17. #include "ethernet-util.h"
  18. #include <asm/octeon/cvmx-spi.h>
  19. #include <asm/octeon/cvmx-npi-defs.h>
  20. #include <asm/octeon/cvmx-spxx-defs.h>
  21. #include <asm/octeon/cvmx-stxx-defs.h>
  22. static int number_spi_ports;
  23. static int need_retrain[2] = { 0, 0 };
  24. static void cvm_oct_spxx_int_pr(union cvmx_spxx_int_reg spx_int_reg, int index)
  25. {
  26. if (spx_int_reg.s.spf)
  27. pr_err("SPI%d: SRX Spi4 interface down\n", index);
  28. if (spx_int_reg.s.calerr)
  29. pr_err("SPI%d: SRX Spi4 Calendar table parity error\n", index);
  30. if (spx_int_reg.s.syncerr)
  31. pr_err("SPI%d: SRX Consecutive Spi4 DIP4 errors have exceeded SPX_ERR_CTL[ERRCNT]\n",
  32. index);
  33. if (spx_int_reg.s.diperr)
  34. pr_err("SPI%d: SRX Spi4 DIP4 error\n", index);
  35. if (spx_int_reg.s.tpaovr)
  36. pr_err("SPI%d: SRX Selected port has hit TPA overflow\n",
  37. index);
  38. if (spx_int_reg.s.rsverr)
  39. pr_err("SPI%d: SRX Spi4 reserved control word detected\n",
  40. index);
  41. if (spx_int_reg.s.drwnng)
  42. pr_err("SPI%d: SRX Spi4 receive FIFO drowning/overflow\n",
  43. index);
  44. if (spx_int_reg.s.clserr)
  45. pr_err("SPI%d: SRX Spi4 packet closed on non-16B alignment without EOP\n",
  46. index);
  47. if (spx_int_reg.s.spiovr)
  48. pr_err("SPI%d: SRX Spi4 async FIFO overflow\n", index);
  49. if (spx_int_reg.s.abnorm)
  50. pr_err("SPI%d: SRX Abnormal packet termination (ERR bit)\n",
  51. index);
  52. if (spx_int_reg.s.prtnxa)
  53. pr_err("SPI%d: SRX Port out of range\n", index);
  54. }
  55. static void cvm_oct_stxx_int_pr(union cvmx_stxx_int_reg stx_int_reg, int index)
  56. {
  57. if (stx_int_reg.s.syncerr)
  58. pr_err("SPI%d: STX Interface encountered a fatal error\n",
  59. index);
  60. if (stx_int_reg.s.frmerr)
  61. pr_err("SPI%d: STX FRMCNT has exceeded STX_DIP_CNT[MAXFRM]\n",
  62. index);
  63. if (stx_int_reg.s.unxfrm)
  64. pr_err("SPI%d: STX Unexpected framing sequence\n", index);
  65. if (stx_int_reg.s.nosync)
  66. pr_err("SPI%d: STX ERRCNT has exceeded STX_DIP_CNT[MAXDIP]\n",
  67. index);
  68. if (stx_int_reg.s.diperr)
  69. pr_err("SPI%d: STX DIP2 error on the Spi4 Status channel\n",
  70. index);
  71. if (stx_int_reg.s.datovr)
  72. pr_err("SPI%d: STX Spi4 FIFO overflow error\n", index);
  73. if (stx_int_reg.s.ovrbst)
  74. pr_err("SPI%d: STX Transmit packet burst too big\n", index);
  75. if (stx_int_reg.s.calpar1)
  76. pr_err("SPI%d: STX Calendar Table Parity Error Bank%d\n",
  77. index, 1);
  78. if (stx_int_reg.s.calpar0)
  79. pr_err("SPI%d: STX Calendar Table Parity Error Bank%d\n",
  80. index, 0);
  81. }
  82. static irqreturn_t cvm_oct_spi_spx_int(int index)
  83. {
  84. union cvmx_spxx_int_reg spx_int_reg;
  85. union cvmx_stxx_int_reg stx_int_reg;
  86. spx_int_reg.u64 = cvmx_read_csr(CVMX_SPXX_INT_REG(index));
  87. cvmx_write_csr(CVMX_SPXX_INT_REG(index), spx_int_reg.u64);
  88. if (!need_retrain[index]) {
  89. spx_int_reg.u64 &= cvmx_read_csr(CVMX_SPXX_INT_MSK(index));
  90. cvm_oct_spxx_int_pr(spx_int_reg, index);
  91. }
  92. stx_int_reg.u64 = cvmx_read_csr(CVMX_STXX_INT_REG(index));
  93. cvmx_write_csr(CVMX_STXX_INT_REG(index), stx_int_reg.u64);
  94. if (!need_retrain[index]) {
  95. stx_int_reg.u64 &= cvmx_read_csr(CVMX_STXX_INT_MSK(index));
  96. cvm_oct_stxx_int_pr(stx_int_reg, index);
  97. }
  98. cvmx_write_csr(CVMX_SPXX_INT_MSK(index), 0);
  99. cvmx_write_csr(CVMX_STXX_INT_MSK(index), 0);
  100. need_retrain[index] = 1;
  101. return IRQ_HANDLED;
  102. }
  103. static irqreturn_t cvm_oct_spi_rml_interrupt(int cpl, void *dev_id)
  104. {
  105. irqreturn_t return_status = IRQ_NONE;
  106. union cvmx_npi_rsl_int_blocks rsl_int_blocks;
  107. /* Check and see if this interrupt was caused by the GMX block */
  108. rsl_int_blocks.u64 = cvmx_read_csr(CVMX_NPI_RSL_INT_BLOCKS);
  109. if (rsl_int_blocks.s.spx1) /* 19 - SPX1_INT_REG & STX1_INT_REG */
  110. return_status = cvm_oct_spi_spx_int(1);
  111. if (rsl_int_blocks.s.spx0) /* 18 - SPX0_INT_REG & STX0_INT_REG */
  112. return_status = cvm_oct_spi_spx_int(0);
  113. return return_status;
  114. }
  115. static void cvm_oct_spi_enable_error_reporting(int interface)
  116. {
  117. union cvmx_spxx_int_msk spxx_int_msk;
  118. union cvmx_stxx_int_msk stxx_int_msk;
  119. spxx_int_msk.u64 = cvmx_read_csr(CVMX_SPXX_INT_MSK(interface));
  120. spxx_int_msk.s.calerr = 1;
  121. spxx_int_msk.s.syncerr = 1;
  122. spxx_int_msk.s.diperr = 1;
  123. spxx_int_msk.s.tpaovr = 1;
  124. spxx_int_msk.s.rsverr = 1;
  125. spxx_int_msk.s.drwnng = 1;
  126. spxx_int_msk.s.clserr = 1;
  127. spxx_int_msk.s.spiovr = 1;
  128. spxx_int_msk.s.abnorm = 1;
  129. spxx_int_msk.s.prtnxa = 1;
  130. cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), spxx_int_msk.u64);
  131. stxx_int_msk.u64 = cvmx_read_csr(CVMX_STXX_INT_MSK(interface));
  132. stxx_int_msk.s.frmerr = 1;
  133. stxx_int_msk.s.unxfrm = 1;
  134. stxx_int_msk.s.nosync = 1;
  135. stxx_int_msk.s.diperr = 1;
  136. stxx_int_msk.s.datovr = 1;
  137. stxx_int_msk.s.ovrbst = 1;
  138. stxx_int_msk.s.calpar1 = 1;
  139. stxx_int_msk.s.calpar0 = 1;
  140. cvmx_write_csr(CVMX_STXX_INT_MSK(interface), stxx_int_msk.u64);
  141. }
  142. static void cvm_oct_spi_poll(struct net_device *dev)
  143. {
  144. static int spi4000_port;
  145. struct octeon_ethernet *priv = netdev_priv(dev);
  146. int interface;
  147. for (interface = 0; interface < 2; interface++) {
  148. if ((priv->port == interface * 16) && need_retrain[interface]) {
  149. if (cvmx_spi_restart_interface
  150. (interface, CVMX_SPI_MODE_DUPLEX, 10) == 0) {
  151. need_retrain[interface] = 0;
  152. cvm_oct_spi_enable_error_reporting(interface);
  153. }
  154. }
  155. /*
  156. * The SPI4000 TWSI interface is very slow. In order
  157. * not to bring the system to a crawl, we only poll a
  158. * single port every second. This means negotiation
  159. * speed changes take up to 10 seconds, but at least
  160. * we don't waste absurd amounts of time waiting for
  161. * TWSI.
  162. */
  163. if (priv->port == spi4000_port) {
  164. /*
  165. * This function does nothing if it is called on an
  166. * interface without a SPI4000.
  167. */
  168. cvmx_spi4000_check_speed(interface, priv->port);
  169. /*
  170. * Normal ordering increments. By decrementing
  171. * we only match once per iteration.
  172. */
  173. spi4000_port--;
  174. if (spi4000_port < 0)
  175. spi4000_port = 10;
  176. }
  177. }
  178. }
  179. int cvm_oct_spi_init(struct net_device *dev)
  180. {
  181. int r;
  182. struct octeon_ethernet *priv = netdev_priv(dev);
  183. if (number_spi_ports == 0) {
  184. r = request_irq(OCTEON_IRQ_RML, cvm_oct_spi_rml_interrupt,
  185. IRQF_SHARED, "SPI", &number_spi_ports);
  186. if (r)
  187. return r;
  188. }
  189. number_spi_ports++;
  190. if ((priv->port == 0) || (priv->port == 16)) {
  191. cvm_oct_spi_enable_error_reporting(INTERFACE(priv->port));
  192. priv->poll = cvm_oct_spi_poll;
  193. }
  194. cvm_oct_common_init(dev);
  195. return 0;
  196. }
  197. void cvm_oct_spi_uninit(struct net_device *dev)
  198. {
  199. int interface;
  200. cvm_oct_common_uninit(dev);
  201. number_spi_ports--;
  202. if (number_spi_ports == 0) {
  203. for (interface = 0; interface < 2; interface++) {
  204. cvmx_write_csr(CVMX_SPXX_INT_MSK(interface), 0);
  205. cvmx_write_csr(CVMX_STXX_INT_MSK(interface), 0);
  206. }
  207. free_irq(OCTEON_IRQ_RML, &number_spi_ports);
  208. }
  209. }