espi.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373
  1. /*****************************************************************************
  2. * *
  3. * File: espi.c *
  4. * $Revision: 1.14 $ *
  5. * $Date: 2005/05/14 00:59:32 $ *
  6. * Description: *
  7. * Ethernet SPI functionality. *
  8. * part of the Chelsio 10Gb Ethernet Driver. *
  9. * *
  10. * This program is free software; you can redistribute it and/or modify *
  11. * it under the terms of the GNU General Public License, version 2, as *
  12. * published by the Free Software Foundation. *
  13. * *
  14. * You should have received a copy of the GNU General Public License along *
  15. * with this program; if not, write to the Free Software Foundation, Inc., *
  16. * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
  17. * *
  18. * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED *
  19. * WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF *
  20. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. *
  21. * *
  22. * http://www.chelsio.com *
  23. * *
  24. * Copyright (c) 2003 - 2005 Chelsio Communications, Inc. *
  25. * All rights reserved. *
  26. * *
  27. * Maintainers: maintainers@chelsio.com *
  28. * *
  29. * Authors: Dimitrios Michailidis <dm@chelsio.com> *
  30. * Tina Yang <tainay@chelsio.com> *
  31. * Felix Marti <felix@chelsio.com> *
  32. * Scott Bardone <sbardone@chelsio.com> *
  33. * Kurt Ottaway <kottaway@chelsio.com> *
  34. * Frank DiMambro <frank@chelsio.com> *
  35. * *
  36. * History: *
  37. * *
  38. ****************************************************************************/
  39. #include "common.h"
  40. #include "regs.h"
  41. #include "espi.h"
  42. struct peespi {
  43. adapter_t *adapter;
  44. struct espi_intr_counts intr_cnt;
  45. u32 misc_ctrl;
  46. spinlock_t lock;
  47. };
  48. #define ESPI_INTR_MASK (F_DIP4ERR | F_RXDROP | F_TXDROP | F_RXOVERFLOW | \
  49. F_RAMPARITYERR | F_DIP2PARITYERR)
  50. #define MON_MASK (V_MONITORED_PORT_NUM(3) | F_MONITORED_DIRECTION \
  51. | F_MONITORED_INTERFACE)
  52. #define TRICN_CNFG 14
  53. #define TRICN_CMD_READ 0x11
  54. #define TRICN_CMD_WRITE 0x21
  55. #define TRICN_CMD_ATTEMPTS 10
  56. static int tricn_write(adapter_t *adapter, int bundle_addr, int module_addr,
  57. int ch_addr, int reg_offset, u32 wr_data)
  58. {
  59. int busy, attempts = TRICN_CMD_ATTEMPTS;
  60. writel(V_WRITE_DATA(wr_data) |
  61. V_REGISTER_OFFSET(reg_offset) |
  62. V_CHANNEL_ADDR(ch_addr) | V_MODULE_ADDR(module_addr) |
  63. V_BUNDLE_ADDR(bundle_addr) |
  64. V_SPI4_COMMAND(TRICN_CMD_WRITE),
  65. adapter->regs + A_ESPI_CMD_ADDR);
  66. writel(0, adapter->regs + A_ESPI_GOSTAT);
  67. do {
  68. busy = readl(adapter->regs + A_ESPI_GOSTAT) & F_ESPI_CMD_BUSY;
  69. } while (busy && --attempts);
  70. if (busy)
  71. CH_ERR("%s: TRICN write timed out\n", adapter->name);
  72. return busy;
  73. }
  74. static int tricn_init(adapter_t *adapter)
  75. {
  76. int i, sme = 1;
  77. if (!(readl(adapter->regs + A_ESPI_RX_RESET) & F_RX_CLK_STATUS)) {
  78. CH_ERR("%s: ESPI clock not ready\n", adapter->name);
  79. return -1;
  80. }
  81. writel(F_ESPI_RX_CORE_RST, adapter->regs + A_ESPI_RX_RESET);
  82. if (sme) {
  83. tricn_write(adapter, 0, 0, 0, TRICN_CNFG, 0x81);
  84. tricn_write(adapter, 0, 1, 0, TRICN_CNFG, 0x81);
  85. tricn_write(adapter, 0, 2, 0, TRICN_CNFG, 0x81);
  86. }
  87. for (i = 1; i <= 8; i++)
  88. tricn_write(adapter, 0, 0, i, TRICN_CNFG, 0xf1);
  89. for (i = 1; i <= 2; i++)
  90. tricn_write(adapter, 0, 1, i, TRICN_CNFG, 0xf1);
  91. for (i = 1; i <= 3; i++)
  92. tricn_write(adapter, 0, 2, i, TRICN_CNFG, 0xe1);
  93. tricn_write(adapter, 0, 2, 4, TRICN_CNFG, 0xf1);
  94. tricn_write(adapter, 0, 2, 5, TRICN_CNFG, 0xe1);
  95. tricn_write(adapter, 0, 2, 6, TRICN_CNFG, 0xf1);
  96. tricn_write(adapter, 0, 2, 7, TRICN_CNFG, 0x80);
  97. tricn_write(adapter, 0, 2, 8, TRICN_CNFG, 0xf1);
  98. writel(F_ESPI_RX_CORE_RST | F_ESPI_RX_LNK_RST,
  99. adapter->regs + A_ESPI_RX_RESET);
  100. return 0;
  101. }
  102. void t1_espi_intr_enable(struct peespi *espi)
  103. {
  104. u32 enable, pl_intr = readl(espi->adapter->regs + A_PL_ENABLE);
  105. /*
  106. * Cannot enable ESPI interrupts on T1B because HW asserts the
  107. * interrupt incorrectly, namely the driver gets ESPI interrupts
  108. * but no data is actually dropped (can verify this reading the ESPI
  109. * drop registers). Also, once the ESPI interrupt is asserted it
  110. * cannot be cleared (HW bug).
  111. */
  112. enable = t1_is_T1B(espi->adapter) ? 0 : ESPI_INTR_MASK;
  113. writel(enable, espi->adapter->regs + A_ESPI_INTR_ENABLE);
  114. writel(pl_intr | F_PL_INTR_ESPI, espi->adapter->regs + A_PL_ENABLE);
  115. }
  116. void t1_espi_intr_clear(struct peespi *espi)
  117. {
  118. readl(espi->adapter->regs + A_ESPI_DIP2_ERR_COUNT);
  119. writel(0xffffffff, espi->adapter->regs + A_ESPI_INTR_STATUS);
  120. writel(F_PL_INTR_ESPI, espi->adapter->regs + A_PL_CAUSE);
  121. }
  122. void t1_espi_intr_disable(struct peespi *espi)
  123. {
  124. u32 pl_intr = readl(espi->adapter->regs + A_PL_ENABLE);
  125. writel(0, espi->adapter->regs + A_ESPI_INTR_ENABLE);
  126. writel(pl_intr & ~F_PL_INTR_ESPI, espi->adapter->regs + A_PL_ENABLE);
  127. }
  128. int t1_espi_intr_handler(struct peespi *espi)
  129. {
  130. u32 status = readl(espi->adapter->regs + A_ESPI_INTR_STATUS);
  131. if (status & F_DIP4ERR)
  132. espi->intr_cnt.DIP4_err++;
  133. if (status & F_RXDROP)
  134. espi->intr_cnt.rx_drops++;
  135. if (status & F_TXDROP)
  136. espi->intr_cnt.tx_drops++;
  137. if (status & F_RXOVERFLOW)
  138. espi->intr_cnt.rx_ovflw++;
  139. if (status & F_RAMPARITYERR)
  140. espi->intr_cnt.parity_err++;
  141. if (status & F_DIP2PARITYERR) {
  142. espi->intr_cnt.DIP2_parity_err++;
  143. /*
  144. * Must read the error count to clear the interrupt
  145. * that it causes.
  146. */
  147. readl(espi->adapter->regs + A_ESPI_DIP2_ERR_COUNT);
  148. }
  149. /*
  150. * For T1B we need to write 1 to clear ESPI interrupts. For T2+ we
  151. * write the status as is.
  152. */
  153. if (status && t1_is_T1B(espi->adapter))
  154. status = 1;
  155. writel(status, espi->adapter->regs + A_ESPI_INTR_STATUS);
  156. return 0;
  157. }
  158. const struct espi_intr_counts *t1_espi_get_intr_counts(struct peespi *espi)
  159. {
  160. return &espi->intr_cnt;
  161. }
  162. static void espi_setup_for_pm3393(adapter_t *adapter)
  163. {
  164. u32 wmark = t1_is_T1B(adapter) ? 0x4000 : 0x3200;
  165. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN0);
  166. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN1);
  167. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN2);
  168. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN3);
  169. writel(0x100, adapter->regs + A_ESPI_RX_FIFO_ALMOST_EMPTY_WATERMARK);
  170. writel(wmark, adapter->regs + A_ESPI_RX_FIFO_ALMOST_FULL_WATERMARK);
  171. writel(3, adapter->regs + A_ESPI_CALENDAR_LENGTH);
  172. writel(0x08000008, adapter->regs + A_ESPI_TRAIN);
  173. writel(V_RX_NPORTS(1) | V_TX_NPORTS(1), adapter->regs + A_PORT_CONFIG);
  174. }
  175. static void espi_setup_for_vsc7321(adapter_t *adapter)
  176. {
  177. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN0);
  178. writel(0x1f401f4, adapter->regs + A_ESPI_SCH_TOKEN1);
  179. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN2);
  180. writel(0xa00, adapter->regs + A_ESPI_RX_FIFO_ALMOST_FULL_WATERMARK);
  181. writel(0x1ff, adapter->regs + A_ESPI_RX_FIFO_ALMOST_EMPTY_WATERMARK);
  182. writel(1, adapter->regs + A_ESPI_CALENDAR_LENGTH);
  183. writel(V_RX_NPORTS(4) | V_TX_NPORTS(4), adapter->regs + A_PORT_CONFIG);
  184. writel(0x08000008, adapter->regs + A_ESPI_TRAIN);
  185. }
  186. /*
  187. * Note that T1B requires at least 2 ports for IXF1010 due to a HW bug.
  188. */
  189. static void espi_setup_for_ixf1010(adapter_t *adapter, int nports)
  190. {
  191. writel(1, adapter->regs + A_ESPI_CALENDAR_LENGTH);
  192. if (nports == 4) {
  193. if (is_T2(adapter)) {
  194. writel(0xf00, adapter->regs + A_ESPI_RX_FIFO_ALMOST_FULL_WATERMARK);
  195. writel(0x3c0, adapter->regs + A_ESPI_RX_FIFO_ALMOST_EMPTY_WATERMARK);
  196. } else {
  197. writel(0x7ff, adapter->regs + A_ESPI_RX_FIFO_ALMOST_FULL_WATERMARK);
  198. writel(0x1ff, adapter->regs + A_ESPI_RX_FIFO_ALMOST_EMPTY_WATERMARK);
  199. }
  200. } else {
  201. writel(0x1fff, adapter->regs + A_ESPI_RX_FIFO_ALMOST_FULL_WATERMARK);
  202. writel(0x7ff, adapter->regs + A_ESPI_RX_FIFO_ALMOST_EMPTY_WATERMARK);
  203. }
  204. writel(V_RX_NPORTS(nports) | V_TX_NPORTS(nports), adapter->regs + A_PORT_CONFIG);
  205. }
  206. int t1_espi_init(struct peespi *espi, int mac_type, int nports)
  207. {
  208. u32 status_enable_extra = 0;
  209. adapter_t *adapter = espi->adapter;
  210. /* Disable ESPI training. MACs that can handle it enable it below. */
  211. writel(0, adapter->regs + A_ESPI_TRAIN);
  212. if (is_T2(adapter)) {
  213. writel(V_OUT_OF_SYNC_COUNT(4) |
  214. V_DIP2_PARITY_ERR_THRES(3) |
  215. V_DIP4_THRES(1), adapter->regs + A_ESPI_MISC_CONTROL);
  216. writel(nports == 4 ? 0x200040 : 0x1000080,
  217. adapter->regs + A_ESPI_MAXBURST1_MAXBURST2);
  218. } else
  219. writel(0x800100, adapter->regs + A_ESPI_MAXBURST1_MAXBURST2);
  220. if (mac_type == CHBT_MAC_PM3393)
  221. espi_setup_for_pm3393(adapter);
  222. else if (mac_type == CHBT_MAC_VSC7321)
  223. espi_setup_for_vsc7321(adapter);
  224. else if (mac_type == CHBT_MAC_IXF1010) {
  225. status_enable_extra = F_INTEL1010MODE;
  226. espi_setup_for_ixf1010(adapter, nports);
  227. } else
  228. return -1;
  229. writel(status_enable_extra | F_RXSTATUSENABLE,
  230. adapter->regs + A_ESPI_FIFO_STATUS_ENABLE);
  231. if (is_T2(adapter)) {
  232. tricn_init(adapter);
  233. /*
  234. * Always position the control at the 1st port egress IN
  235. * (sop,eop) counter to reduce PIOs for T/N210 workaround.
  236. */
  237. espi->misc_ctrl = readl(adapter->regs + A_ESPI_MISC_CONTROL);
  238. espi->misc_ctrl &= ~MON_MASK;
  239. espi->misc_ctrl |= F_MONITORED_DIRECTION;
  240. if (adapter->params.nports == 1)
  241. espi->misc_ctrl |= F_MONITORED_INTERFACE;
  242. writel(espi->misc_ctrl, adapter->regs + A_ESPI_MISC_CONTROL);
  243. spin_lock_init(&espi->lock);
  244. }
  245. return 0;
  246. }
  247. void t1_espi_destroy(struct peespi *espi)
  248. {
  249. kfree(espi);
  250. }
  251. struct peespi *t1_espi_create(adapter_t *adapter)
  252. {
  253. struct peespi *espi = kzalloc(sizeof(*espi), GFP_KERNEL);
  254. if (espi)
  255. espi->adapter = adapter;
  256. return espi;
  257. }
  258. #if 0
  259. void t1_espi_set_misc_ctrl(adapter_t *adapter, u32 val)
  260. {
  261. struct peespi *espi = adapter->espi;
  262. if (!is_T2(adapter))
  263. return;
  264. spin_lock(&espi->lock);
  265. espi->misc_ctrl = (val & ~MON_MASK) |
  266. (espi->misc_ctrl & MON_MASK);
  267. writel(espi->misc_ctrl, adapter->regs + A_ESPI_MISC_CONTROL);
  268. spin_unlock(&espi->lock);
  269. }
  270. #endif /* 0 */
  271. u32 t1_espi_get_mon(adapter_t *adapter, u32 addr, u8 wait)
  272. {
  273. struct peespi *espi = adapter->espi;
  274. u32 sel;
  275. if (!is_T2(adapter))
  276. return 0;
  277. sel = V_MONITORED_PORT_NUM((addr & 0x3c) >> 2);
  278. if (!wait) {
  279. if (!spin_trylock(&espi->lock))
  280. return 0;
  281. } else
  282. spin_lock(&espi->lock);
  283. if ((sel != (espi->misc_ctrl & MON_MASK))) {
  284. writel(((espi->misc_ctrl & ~MON_MASK) | sel),
  285. adapter->regs + A_ESPI_MISC_CONTROL);
  286. sel = readl(adapter->regs + A_ESPI_SCH_TOKEN3);
  287. writel(espi->misc_ctrl, adapter->regs + A_ESPI_MISC_CONTROL);
  288. } else
  289. sel = readl(adapter->regs + A_ESPI_SCH_TOKEN3);
  290. spin_unlock(&espi->lock);
  291. return sel;
  292. }
  293. /*
  294. * This function is for T204 only.
  295. * compare with t1_espi_get_mon(), it reads espiInTxSop[0 ~ 3] in
  296. * one shot, since there is no per port counter on the out side.
  297. */
  298. int t1_espi_get_mon_t204(adapter_t *adapter, u32 *valp, u8 wait)
  299. {
  300. struct peespi *espi = adapter->espi;
  301. u8 i, nport = (u8)adapter->params.nports;
  302. if (!wait) {
  303. if (!spin_trylock(&espi->lock))
  304. return -1;
  305. } else
  306. spin_lock(&espi->lock);
  307. if ((espi->misc_ctrl & MON_MASK) != F_MONITORED_DIRECTION) {
  308. espi->misc_ctrl = (espi->misc_ctrl & ~MON_MASK) |
  309. F_MONITORED_DIRECTION;
  310. writel(espi->misc_ctrl, adapter->regs + A_ESPI_MISC_CONTROL);
  311. }
  312. for (i = 0 ; i < nport; i++, valp++) {
  313. if (i) {
  314. writel(espi->misc_ctrl | V_MONITORED_PORT_NUM(i),
  315. adapter->regs + A_ESPI_MISC_CONTROL);
  316. }
  317. *valp = readl(adapter->regs + A_ESPI_SCH_TOKEN3);
  318. }
  319. writel(espi->misc_ctrl, adapter->regs + A_ESPI_MISC_CONTROL);
  320. spin_unlock(&espi->lock);
  321. return 0;
  322. }