espi.c 11 KB

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