espi.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336
  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. /* 1. Deassert rx_reset_core. */
  75. /* 2. Program TRICN_CNFG registers. */
  76. /* 3. Deassert rx_reset_link */
  77. static int tricn_init(adapter_t *adapter)
  78. {
  79. int i = 0;
  80. int stat = 0;
  81. int timeout = 0;
  82. int is_ready = 0;
  83. /* 1 */
  84. timeout=1000;
  85. do {
  86. stat = readl(adapter->regs + A_ESPI_RX_RESET);
  87. is_ready = (stat & 0x4);
  88. timeout--;
  89. udelay(5);
  90. } while (!is_ready || (timeout==0));
  91. writel(0x2, adapter->regs + A_ESPI_RX_RESET);
  92. if (timeout==0)
  93. {
  94. CH_ERR("ESPI : ERROR : Timeout tricn_init() \n");
  95. t1_fatal_err(adapter);
  96. }
  97. /* 2 */
  98. tricn_write(adapter, 0, 0, 0, TRICN_CNFG, 0x81);
  99. tricn_write(adapter, 0, 1, 0, TRICN_CNFG, 0x81);
  100. tricn_write(adapter, 0, 2, 0, TRICN_CNFG, 0x81);
  101. for (i=1; i<= 8; i++) tricn_write(adapter, 0, 0, i, TRICN_CNFG, 0xf1);
  102. for (i=1; i<= 2; i++) tricn_write(adapter, 0, 1, i, TRICN_CNFG, 0xf1);
  103. for (i=1; i<= 3; i++) tricn_write(adapter, 0, 2, i, TRICN_CNFG, 0xe1);
  104. for (i=4; i<= 4; i++) tricn_write(adapter, 0, 2, i, TRICN_CNFG, 0xf1);
  105. for (i=5; i<= 5; i++) tricn_write(adapter, 0, 2, i, TRICN_CNFG, 0xe1);
  106. for (i=6; i<= 6; i++) tricn_write(adapter, 0, 2, i, TRICN_CNFG, 0xf1);
  107. for (i=7; i<= 7; i++) tricn_write(adapter, 0, 2, i, TRICN_CNFG, 0x80);
  108. for (i=8; i<= 8; i++) tricn_write(adapter, 0, 2, i, TRICN_CNFG, 0xf1);
  109. /* 3 */
  110. writel(0x3, adapter->regs + A_ESPI_RX_RESET);
  111. return 0;
  112. }
  113. void t1_espi_intr_enable(struct peespi *espi)
  114. {
  115. u32 enable, pl_intr = readl(espi->adapter->regs + A_PL_ENABLE);
  116. /*
  117. * Cannot enable ESPI interrupts on T1B because HW asserts the
  118. * interrupt incorrectly, namely the driver gets ESPI interrupts
  119. * but no data is actually dropped (can verify this reading the ESPI
  120. * drop registers). Also, once the ESPI interrupt is asserted it
  121. * cannot be cleared (HW bug).
  122. */
  123. enable = t1_is_T1B(espi->adapter) ? 0 : ESPI_INTR_MASK;
  124. writel(enable, espi->adapter->regs + A_ESPI_INTR_ENABLE);
  125. writel(pl_intr | F_PL_INTR_ESPI, espi->adapter->regs + A_PL_ENABLE);
  126. }
  127. void t1_espi_intr_clear(struct peespi *espi)
  128. {
  129. writel(0xffffffff, espi->adapter->regs + A_ESPI_INTR_STATUS);
  130. writel(F_PL_INTR_ESPI, espi->adapter->regs + A_PL_CAUSE);
  131. }
  132. void t1_espi_intr_disable(struct peespi *espi)
  133. {
  134. u32 pl_intr = readl(espi->adapter->regs + A_PL_ENABLE);
  135. writel(0, espi->adapter->regs + A_ESPI_INTR_ENABLE);
  136. writel(pl_intr & ~F_PL_INTR_ESPI, espi->adapter->regs + A_PL_ENABLE);
  137. }
  138. int t1_espi_intr_handler(struct peespi *espi)
  139. {
  140. u32 cnt;
  141. u32 status = readl(espi->adapter->regs + A_ESPI_INTR_STATUS);
  142. if (status & F_DIP4ERR)
  143. espi->intr_cnt.DIP4_err++;
  144. if (status & F_RXDROP)
  145. espi->intr_cnt.rx_drops++;
  146. if (status & F_TXDROP)
  147. espi->intr_cnt.tx_drops++;
  148. if (status & F_RXOVERFLOW)
  149. espi->intr_cnt.rx_ovflw++;
  150. if (status & F_RAMPARITYERR)
  151. espi->intr_cnt.parity_err++;
  152. if (status & F_DIP2PARITYERR) {
  153. espi->intr_cnt.DIP2_parity_err++;
  154. /*
  155. * Must read the error count to clear the interrupt
  156. * that it causes.
  157. */
  158. cnt = readl(espi->adapter->regs + A_ESPI_DIP2_ERR_COUNT);
  159. }
  160. /*
  161. * For T1B we need to write 1 to clear ESPI interrupts. For T2+ we
  162. * write the status as is.
  163. */
  164. if (status && t1_is_T1B(espi->adapter))
  165. status = 1;
  166. writel(status, espi->adapter->regs + A_ESPI_INTR_STATUS);
  167. return 0;
  168. }
  169. const struct espi_intr_counts *t1_espi_get_intr_counts(struct peespi *espi)
  170. {
  171. return &espi->intr_cnt;
  172. }
  173. static void espi_setup_for_pm3393(adapter_t *adapter)
  174. {
  175. u32 wmark = t1_is_T1B(adapter) ? 0x4000 : 0x3200;
  176. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN0);
  177. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN1);
  178. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN2);
  179. writel(0x1f4, adapter->regs + A_ESPI_SCH_TOKEN3);
  180. writel(0x100, adapter->regs + A_ESPI_RX_FIFO_ALMOST_EMPTY_WATERMARK);
  181. writel(wmark, adapter->regs + A_ESPI_RX_FIFO_ALMOST_FULL_WATERMARK);
  182. writel(3, adapter->regs + A_ESPI_CALENDAR_LENGTH);
  183. writel(0x08000008, adapter->regs + A_ESPI_TRAIN);
  184. writel(V_RX_NPORTS(1) | V_TX_NPORTS(1), adapter->regs + A_PORT_CONFIG);
  185. }
  186. /* T2 Init part -- */
  187. /* 1. Set T_ESPI_MISCCTRL_ADDR */
  188. /* 2. Init ESPI registers. */
  189. /* 3. Init TriCN Hard Macro */
  190. int t1_espi_init(struct peespi *espi, int mac_type, int nports)
  191. {
  192. u32 cnt;
  193. u32 status_enable_extra = 0;
  194. adapter_t *adapter = espi->adapter;
  195. u32 status, burstval = 0x800100;
  196. /* Disable ESPI training. MACs that can handle it enable it below. */
  197. writel(0, adapter->regs + A_ESPI_TRAIN);
  198. if (is_T2(adapter)) {
  199. writel(V_OUT_OF_SYNC_COUNT(4) |
  200. V_DIP2_PARITY_ERR_THRES(3) |
  201. V_DIP4_THRES(1), adapter->regs + A_ESPI_MISC_CONTROL);
  202. if (nports == 4) {
  203. /* T204: maxburst1 = 0x40, maxburst2 = 0x20 */
  204. burstval = 0x200040;
  205. }
  206. }
  207. writel(burstval, adapter->regs + A_ESPI_MAXBURST1_MAXBURST2);
  208. switch (mac_type) {
  209. case CHBT_MAC_PM3393:
  210. espi_setup_for_pm3393(adapter);
  211. break;
  212. default:
  213. return -1;
  214. }
  215. /*
  216. * Make sure any pending interrupts from the SPI are
  217. * Cleared before enabling the interrupt.
  218. */
  219. writel(ESPI_INTR_MASK, espi->adapter->regs + A_ESPI_INTR_ENABLE);
  220. status = readl(espi->adapter->regs + A_ESPI_INTR_STATUS);
  221. if (status & F_DIP2PARITYERR) {
  222. cnt = readl(espi->adapter->regs + A_ESPI_DIP2_ERR_COUNT);
  223. }
  224. /*
  225. * For T1B we need to write 1 to clear ESPI interrupts. For T2+ we
  226. * write the status as is.
  227. */
  228. if (status && t1_is_T1B(espi->adapter))
  229. status = 1;
  230. writel(status, espi->adapter->regs + A_ESPI_INTR_STATUS);
  231. writel(status_enable_extra | F_RXSTATUSENABLE,
  232. adapter->regs + A_ESPI_FIFO_STATUS_ENABLE);
  233. if (is_T2(adapter)) {
  234. tricn_init(adapter);
  235. /*
  236. * Always position the control at the 1st port egress IN
  237. * (sop,eop) counter to reduce PIOs for T/N210 workaround.
  238. */
  239. espi->misc_ctrl = (readl(adapter->regs + A_ESPI_MISC_CONTROL)
  240. & ~MON_MASK) | (F_MONITORED_DIRECTION
  241. | 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. 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. u32 t1_espi_get_mon(adapter_t *adapter, u32 addr, u8 wait)
  270. {
  271. u32 sel;
  272. struct peespi *espi = adapter->espi;
  273. if (!is_T2(adapter))
  274. return 0;
  275. sel = V_MONITORED_PORT_NUM((addr & 0x3c) >> 2);
  276. if (!wait) {
  277. if (!spin_trylock(&espi->lock))
  278. return 0;
  279. }
  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. }
  288. else
  289. sel = readl(adapter->regs + A_ESPI_SCH_TOKEN3);
  290. spin_unlock(&espi->lock);
  291. return sel;
  292. }