espi.c 12 KB

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