subr.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812
  1. /*****************************************************************************
  2. * *
  3. * File: subr.c *
  4. * $Revision: 1.27 $ *
  5. * $Date: 2005/06/22 01:08:36 $ *
  6. * Description: *
  7. * Various subroutines (intr,pio,etc.) used by Chelsio 10G Ethernet driver. *
  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 "elmer0.h"
  41. #include "regs.h"
  42. #include "gmac.h"
  43. #include "cphy.h"
  44. #include "sge.h"
  45. #include "espi.h"
  46. /**
  47. * t1_wait_op_done - wait until an operation is completed
  48. * @adapter: the adapter performing the operation
  49. * @reg: the register to check for completion
  50. * @mask: a single-bit field within @reg that indicates completion
  51. * @polarity: the value of the field when the operation is completed
  52. * @attempts: number of check iterations
  53. * @delay: delay in usecs between iterations
  54. *
  55. * Wait until an operation is completed by checking a bit in a register
  56. * up to @attempts times. Returns %0 if the operation completes and %1
  57. * otherwise.
  58. */
  59. static int t1_wait_op_done(adapter_t *adapter, int reg, u32 mask, int polarity,
  60. int attempts, int delay)
  61. {
  62. while (1) {
  63. u32 val = readl(adapter->regs + reg) & mask;
  64. if (!!val == polarity)
  65. return 0;
  66. if (--attempts == 0)
  67. return 1;
  68. if (delay)
  69. udelay(delay);
  70. }
  71. }
  72. #define TPI_ATTEMPTS 50
  73. /*
  74. * Write a register over the TPI interface (unlocked and locked versions).
  75. */
  76. static int __t1_tpi_write(adapter_t *adapter, u32 addr, u32 value)
  77. {
  78. int tpi_busy;
  79. writel(addr, adapter->regs + A_TPI_ADDR);
  80. writel(value, adapter->regs + A_TPI_WR_DATA);
  81. writel(F_TPIWR, adapter->regs + A_TPI_CSR);
  82. tpi_busy = t1_wait_op_done(adapter, A_TPI_CSR, F_TPIRDY, 1,
  83. TPI_ATTEMPTS, 3);
  84. if (tpi_busy)
  85. CH_ALERT("%s: TPI write to 0x%x failed\n",
  86. adapter->name, addr);
  87. return tpi_busy;
  88. }
  89. int t1_tpi_write(adapter_t *adapter, u32 addr, u32 value)
  90. {
  91. int ret;
  92. spin_lock(&(adapter)->tpi_lock);
  93. ret = __t1_tpi_write(adapter, addr, value);
  94. spin_unlock(&(adapter)->tpi_lock);
  95. return ret;
  96. }
  97. /*
  98. * Read a register over the TPI interface (unlocked and locked versions).
  99. */
  100. static int __t1_tpi_read(adapter_t *adapter, u32 addr, u32 *valp)
  101. {
  102. int tpi_busy;
  103. writel(addr, adapter->regs + A_TPI_ADDR);
  104. writel(0, adapter->regs + A_TPI_CSR);
  105. tpi_busy = t1_wait_op_done(adapter, A_TPI_CSR, F_TPIRDY, 1,
  106. TPI_ATTEMPTS, 3);
  107. if (tpi_busy)
  108. CH_ALERT("%s: TPI read from 0x%x failed\n",
  109. adapter->name, addr);
  110. else
  111. *valp = readl(adapter->regs + A_TPI_RD_DATA);
  112. return tpi_busy;
  113. }
  114. int t1_tpi_read(adapter_t *adapter, u32 addr, u32 *valp)
  115. {
  116. int ret;
  117. spin_lock(&(adapter)->tpi_lock);
  118. ret = __t1_tpi_read(adapter, addr, valp);
  119. spin_unlock(&(adapter)->tpi_lock);
  120. return ret;
  121. }
  122. /*
  123. * Called when a port's link settings change to propagate the new values to the
  124. * associated PHY and MAC. After performing the common tasks it invokes an
  125. * OS-specific handler.
  126. */
  127. /* static */ void link_changed(adapter_t *adapter, int port_id)
  128. {
  129. int link_ok, speed, duplex, fc;
  130. struct cphy *phy = adapter->port[port_id].phy;
  131. struct link_config *lc = &adapter->port[port_id].link_config;
  132. phy->ops->get_link_status(phy, &link_ok, &speed, &duplex, &fc);
  133. lc->speed = speed < 0 ? SPEED_INVALID : speed;
  134. lc->duplex = duplex < 0 ? DUPLEX_INVALID : duplex;
  135. if (!(lc->requested_fc & PAUSE_AUTONEG))
  136. fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  137. if (link_ok && speed >= 0 && lc->autoneg == AUTONEG_ENABLE) {
  138. /* Set MAC speed, duplex, and flow control to match PHY. */
  139. struct cmac *mac = adapter->port[port_id].mac;
  140. mac->ops->set_speed_duplex_fc(mac, speed, duplex, fc);
  141. lc->fc = (unsigned char)fc;
  142. }
  143. t1_link_changed(adapter, port_id, link_ok, speed, duplex, fc);
  144. }
  145. static int t1_pci_intr_handler(adapter_t *adapter)
  146. {
  147. u32 pcix_cause;
  148. pci_read_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE, &pcix_cause);
  149. if (pcix_cause) {
  150. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE,
  151. pcix_cause);
  152. t1_fatal_err(adapter); /* PCI errors are fatal */
  153. }
  154. return 0;
  155. }
  156. /*
  157. * Wait until Elmer's MI1 interface is ready for new operations.
  158. */
  159. static int mi1_wait_until_ready(adapter_t *adapter, int mi1_reg)
  160. {
  161. int attempts = 100, busy;
  162. do {
  163. u32 val;
  164. __t1_tpi_read(adapter, mi1_reg, &val);
  165. busy = val & F_MI1_OP_BUSY;
  166. if (busy)
  167. udelay(10);
  168. } while (busy && --attempts);
  169. if (busy)
  170. CH_ALERT("%s: MDIO operation timed out\n",
  171. adapter->name);
  172. return busy;
  173. }
  174. /*
  175. * MI1 MDIO initialization.
  176. */
  177. static void mi1_mdio_init(adapter_t *adapter, const struct board_info *bi)
  178. {
  179. u32 clkdiv = bi->clock_elmer0 / (2 * bi->mdio_mdc) - 1;
  180. u32 val = F_MI1_PREAMBLE_ENABLE | V_MI1_MDI_INVERT(bi->mdio_mdiinv) |
  181. V_MI1_MDI_ENABLE(bi->mdio_mdien) | V_MI1_CLK_DIV(clkdiv);
  182. if (!(bi->caps & SUPPORTED_10000baseT_Full))
  183. val |= V_MI1_SOF(1);
  184. t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_CFG, val);
  185. }
  186. static int mi1_mdio_ext_read(adapter_t *adapter, int phy_addr, int mmd_addr,
  187. int reg_addr, unsigned int *valp)
  188. {
  189. u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr);
  190. spin_lock(&(adapter)->tpi_lock);
  191. /* Write the address we want. */
  192. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  193. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr);
  194. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP,
  195. MI1_OP_INDIRECT_ADDRESS);
  196. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  197. /* Write the operation we want. */
  198. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_READ);
  199. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  200. /* Read the data. */
  201. __t1_tpi_read(adapter, A_ELMER0_PORT0_MI1_DATA, valp);
  202. spin_unlock(&(adapter)->tpi_lock);
  203. return 0;
  204. }
  205. static int mi1_mdio_ext_write(adapter_t *adapter, int phy_addr, int mmd_addr,
  206. int reg_addr, unsigned int val)
  207. {
  208. u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr);
  209. spin_lock(&(adapter)->tpi_lock);
  210. /* Write the address we want. */
  211. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  212. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr);
  213. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP,
  214. MI1_OP_INDIRECT_ADDRESS);
  215. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  216. /* Write the data. */
  217. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, val);
  218. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_WRITE);
  219. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  220. spin_unlock(&(adapter)->tpi_lock);
  221. return 0;
  222. }
  223. static struct mdio_ops mi1_mdio_ext_ops = {
  224. mi1_mdio_init,
  225. mi1_mdio_ext_read,
  226. mi1_mdio_ext_write
  227. };
  228. enum {
  229. CH_BRD_N110_1F,
  230. CH_BRD_N210_1F,
  231. };
  232. static struct board_info t1_board[] = {
  233. { CHBT_BOARD_N110, 1/*ports#*/,
  234. SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE /*caps*/, CHBT_TERM_T1,
  235. CHBT_MAC_PM3393, CHBT_PHY_88X2010,
  236. 125000000/*clk-core*/, 0/*clk-mc3*/, 0/*clk-mc4*/,
  237. 1/*espi-ports*/, 0/*clk-cspi*/, 44/*clk-elmer0*/, 0/*mdien*/,
  238. 0/*mdiinv*/, 1/*mdc*/, 0/*phybaseaddr*/, &t1_pm3393_ops,
  239. &t1_mv88x201x_ops, &mi1_mdio_ext_ops,
  240. "Chelsio N110 1x10GBaseX NIC" },
  241. { CHBT_BOARD_N210, 1/*ports#*/,
  242. SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE /*caps*/, CHBT_TERM_T2,
  243. CHBT_MAC_PM3393, CHBT_PHY_88X2010,
  244. 125000000/*clk-core*/, 0/*clk-mc3*/, 0/*clk-mc4*/,
  245. 1/*espi-ports*/, 0/*clk-cspi*/, 44/*clk-elmer0*/, 0/*mdien*/,
  246. 0/*mdiinv*/, 1/*mdc*/, 0/*phybaseaddr*/, &t1_pm3393_ops,
  247. &t1_mv88x201x_ops, &mi1_mdio_ext_ops,
  248. "Chelsio N210 1x10GBaseX NIC" },
  249. };
  250. struct pci_device_id t1_pci_tbl[] = {
  251. CH_DEVICE(7, 0, CH_BRD_N110_1F),
  252. CH_DEVICE(10, 1, CH_BRD_N210_1F),
  253. { 0, }
  254. };
  255. MODULE_DEVICE_TABLE(pci, t1_pci_tbl);
  256. /*
  257. * Return the board_info structure with a given index. Out-of-range indices
  258. * return NULL.
  259. */
  260. const struct board_info *t1_get_board_info(unsigned int board_id)
  261. {
  262. return board_id < ARRAY_SIZE(t1_board) ? &t1_board[board_id] : NULL;
  263. }
  264. struct chelsio_vpd_t {
  265. u32 format_version;
  266. u8 serial_number[16];
  267. u8 mac_base_address[6];
  268. u8 pad[2]; /* make multiple-of-4 size requirement explicit */
  269. };
  270. #define EEPROMSIZE (8 * 1024)
  271. #define EEPROM_MAX_POLL 4
  272. /*
  273. * Read SEEPROM. A zero is written to the flag register when the addres is
  274. * written to the Control register. The hardware device will set the flag to a
  275. * one when 4B have been transferred to the Data register.
  276. */
  277. int t1_seeprom_read(adapter_t *adapter, u32 addr, u32 *data)
  278. {
  279. int i = EEPROM_MAX_POLL;
  280. u16 val;
  281. if (addr >= EEPROMSIZE || (addr & 3))
  282. return -EINVAL;
  283. pci_write_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, (u16)addr);
  284. do {
  285. udelay(50);
  286. pci_read_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, &val);
  287. } while (!(val & F_VPD_OP_FLAG) && --i);
  288. if (!(val & F_VPD_OP_FLAG)) {
  289. CH_ERR("%s: reading EEPROM address 0x%x failed\n",
  290. adapter->name, addr);
  291. return -EIO;
  292. }
  293. pci_read_config_dword(adapter->pdev, A_PCICFG_VPD_DATA, data);
  294. *data = le32_to_cpu(*data);
  295. return 0;
  296. }
  297. static int t1_eeprom_vpd_get(adapter_t *adapter, struct chelsio_vpd_t *vpd)
  298. {
  299. int addr, ret = 0;
  300. for (addr = 0; !ret && addr < sizeof(*vpd); addr += sizeof(u32))
  301. ret = t1_seeprom_read(adapter, addr,
  302. (u32 *)((u8 *)vpd + addr));
  303. return ret;
  304. }
  305. /*
  306. * Read a port's MAC address from the VPD ROM.
  307. */
  308. static int vpd_macaddress_get(adapter_t *adapter, int index, u8 mac_addr[])
  309. {
  310. struct chelsio_vpd_t vpd;
  311. if (t1_eeprom_vpd_get(adapter, &vpd))
  312. return 1;
  313. memcpy(mac_addr, vpd.mac_base_address, 5);
  314. mac_addr[5] = vpd.mac_base_address[5] + index;
  315. return 0;
  316. }
  317. /*
  318. * Set up the MAC/PHY according to the requested link settings.
  319. *
  320. * If the PHY can auto-negotiate first decide what to advertise, then
  321. * enable/disable auto-negotiation as desired and reset.
  322. *
  323. * If the PHY does not auto-negotiate we just reset it.
  324. *
  325. * If auto-negotiation is off set the MAC to the proper speed/duplex/FC,
  326. * otherwise do it later based on the outcome of auto-negotiation.
  327. */
  328. int t1_link_start(struct cphy *phy, struct cmac *mac, struct link_config *lc)
  329. {
  330. unsigned int fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  331. if (lc->supported & SUPPORTED_Autoneg) {
  332. lc->advertising &= ~(ADVERTISED_ASYM_PAUSE | ADVERTISED_PAUSE);
  333. if (fc) {
  334. lc->advertising |= ADVERTISED_ASYM_PAUSE;
  335. if (fc == (PAUSE_RX | PAUSE_TX))
  336. lc->advertising |= ADVERTISED_PAUSE;
  337. }
  338. phy->ops->advertise(phy, lc->advertising);
  339. if (lc->autoneg == AUTONEG_DISABLE) {
  340. lc->speed = lc->requested_speed;
  341. lc->duplex = lc->requested_duplex;
  342. lc->fc = (unsigned char)fc;
  343. mac->ops->set_speed_duplex_fc(mac, lc->speed,
  344. lc->duplex, fc);
  345. /* Also disables autoneg */
  346. phy->ops->set_speed_duplex(phy, lc->speed, lc->duplex);
  347. phy->ops->reset(phy, 0);
  348. } else
  349. phy->ops->autoneg_enable(phy); /* also resets PHY */
  350. } else {
  351. mac->ops->set_speed_duplex_fc(mac, -1, -1, fc);
  352. lc->fc = (unsigned char)fc;
  353. phy->ops->reset(phy, 0);
  354. }
  355. return 0;
  356. }
  357. /*
  358. * External interrupt handler for boards using elmer0.
  359. */
  360. int elmer0_ext_intr_handler(adapter_t *adapter)
  361. {
  362. struct cphy *phy;
  363. int phy_cause;
  364. u32 cause;
  365. t1_tpi_read(adapter, A_ELMER0_INT_CAUSE, &cause);
  366. switch (board_info(adapter)->board) {
  367. case CHBT_BOARD_N210:
  368. case CHBT_BOARD_N110:
  369. if (cause & ELMER0_GP_BIT6) { /* Marvell 88x2010 interrupt */
  370. phy = adapter->port[0].phy;
  371. phy_cause = phy->ops->interrupt_handler(phy);
  372. if (phy_cause & cphy_cause_link_change)
  373. link_changed(adapter, 0);
  374. }
  375. break;
  376. }
  377. t1_tpi_write(adapter, A_ELMER0_INT_CAUSE, cause);
  378. return 0;
  379. }
  380. /* Enables all interrupts. */
  381. void t1_interrupts_enable(adapter_t *adapter)
  382. {
  383. unsigned int i;
  384. u32 pl_intr;
  385. adapter->slow_intr_mask = F_PL_INTR_SGE_ERR;
  386. t1_sge_intr_enable(adapter->sge);
  387. if (adapter->espi) {
  388. adapter->slow_intr_mask |= F_PL_INTR_ESPI;
  389. t1_espi_intr_enable(adapter->espi);
  390. }
  391. /* Enable MAC/PHY interrupts for each port. */
  392. for_each_port(adapter, i) {
  393. adapter->port[i].mac->ops->interrupt_enable(adapter->port[i].mac);
  394. adapter->port[i].phy->ops->interrupt_enable(adapter->port[i].phy);
  395. }
  396. /* Enable PCIX & external chip interrupts on ASIC boards. */
  397. pl_intr = readl(adapter->regs + A_PL_ENABLE);
  398. /* PCI-X interrupts */
  399. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_ENABLE,
  400. 0xffffffff);
  401. adapter->slow_intr_mask |= F_PL_INTR_EXT | F_PL_INTR_PCIX;
  402. pl_intr |= F_PL_INTR_EXT | F_PL_INTR_PCIX;
  403. writel(pl_intr, adapter->regs + A_PL_ENABLE);
  404. }
  405. /* Disables all interrupts. */
  406. void t1_interrupts_disable(adapter_t* adapter)
  407. {
  408. unsigned int i;
  409. t1_sge_intr_disable(adapter->sge);
  410. if (adapter->espi)
  411. t1_espi_intr_disable(adapter->espi);
  412. /* Disable MAC/PHY interrupts for each port. */
  413. for_each_port(adapter, i) {
  414. adapter->port[i].mac->ops->interrupt_disable(adapter->port[i].mac);
  415. adapter->port[i].phy->ops->interrupt_disable(adapter->port[i].phy);
  416. }
  417. /* Disable PCIX & external chip interrupts. */
  418. writel(0, adapter->regs + A_PL_ENABLE);
  419. /* PCI-X interrupts */
  420. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_ENABLE, 0);
  421. adapter->slow_intr_mask = 0;
  422. }
  423. /* Clears all interrupts */
  424. void t1_interrupts_clear(adapter_t* adapter)
  425. {
  426. unsigned int i;
  427. u32 pl_intr;
  428. t1_sge_intr_clear(adapter->sge);
  429. if (adapter->espi)
  430. t1_espi_intr_clear(adapter->espi);
  431. /* Clear MAC/PHY interrupts for each port. */
  432. for_each_port(adapter, i) {
  433. adapter->port[i].mac->ops->interrupt_clear(adapter->port[i].mac);
  434. adapter->port[i].phy->ops->interrupt_clear(adapter->port[i].phy);
  435. }
  436. /* Enable interrupts for external devices. */
  437. pl_intr = readl(adapter->regs + A_PL_CAUSE);
  438. writel(pl_intr | F_PL_INTR_EXT | F_PL_INTR_PCIX,
  439. adapter->regs + A_PL_CAUSE);
  440. /* PCI-X interrupts */
  441. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE, 0xffffffff);
  442. }
  443. /*
  444. * Slow path interrupt handler for ASICs.
  445. */
  446. int t1_slow_intr_handler(adapter_t *adapter)
  447. {
  448. u32 cause = readl(adapter->regs + A_PL_CAUSE);
  449. cause &= adapter->slow_intr_mask;
  450. if (!cause)
  451. return 0;
  452. if (cause & F_PL_INTR_SGE_ERR)
  453. t1_sge_intr_error_handler(adapter->sge);
  454. if (cause & F_PL_INTR_ESPI)
  455. t1_espi_intr_handler(adapter->espi);
  456. if (cause & F_PL_INTR_PCIX)
  457. t1_pci_intr_handler(adapter);
  458. if (cause & F_PL_INTR_EXT)
  459. t1_elmer0_ext_intr(adapter);
  460. /* Clear the interrupts just processed. */
  461. writel(cause, adapter->regs + A_PL_CAUSE);
  462. (void)readl(adapter->regs + A_PL_CAUSE); /* flush writes */
  463. return 1;
  464. }
  465. /* Pause deadlock avoidance parameters */
  466. #define DROP_MSEC 16
  467. #define DROP_PKTS_CNT 1
  468. static void set_csum_offload(adapter_t *adapter, u32 csum_bit, int enable)
  469. {
  470. u32 val = readl(adapter->regs + A_TP_GLOBAL_CONFIG);
  471. if (enable)
  472. val |= csum_bit;
  473. else
  474. val &= ~csum_bit;
  475. writel(val, adapter->regs + A_TP_GLOBAL_CONFIG);
  476. }
  477. void t1_tp_set_ip_checksum_offload(adapter_t *adapter, int enable)
  478. {
  479. set_csum_offload(adapter, F_IP_CSUM, enable);
  480. }
  481. void t1_tp_set_udp_checksum_offload(adapter_t *adapter, int enable)
  482. {
  483. set_csum_offload(adapter, F_UDP_CSUM, enable);
  484. }
  485. void t1_tp_set_tcp_checksum_offload(adapter_t *adapter, int enable)
  486. {
  487. set_csum_offload(adapter, F_TCP_CSUM, enable);
  488. }
  489. static void t1_tp_reset(adapter_t *adapter, unsigned int tp_clk)
  490. {
  491. u32 val;
  492. val = F_TP_IN_CSPI_CPL | F_TP_IN_CSPI_CHECK_IP_CSUM |
  493. F_TP_IN_CSPI_CHECK_TCP_CSUM | F_TP_IN_ESPI_ETHERNET;
  494. val |= F_TP_IN_ESPI_CHECK_IP_CSUM |
  495. F_TP_IN_ESPI_CHECK_TCP_CSUM;
  496. writel(val, adapter->regs + A_TP_IN_CONFIG);
  497. writel(F_TP_OUT_CSPI_CPL |
  498. F_TP_OUT_ESPI_ETHERNET |
  499. F_TP_OUT_ESPI_GENERATE_IP_CSUM |
  500. F_TP_OUT_ESPI_GENERATE_TCP_CSUM,
  501. adapter->regs + A_TP_OUT_CONFIG);
  502. val = readl(adapter->regs + A_TP_GLOBAL_CONFIG);
  503. val &= ~(F_IP_CSUM | F_UDP_CSUM | F_TCP_CSUM);
  504. writel(val, adapter->regs + A_TP_GLOBAL_CONFIG);
  505. /*
  506. * Enable pause frame deadlock prevention.
  507. */
  508. if (is_T2(adapter)) {
  509. u32 drop_ticks = DROP_MSEC * (tp_clk / 1000);
  510. writel(F_ENABLE_TX_DROP | F_ENABLE_TX_ERROR |
  511. V_DROP_TICKS_CNT(drop_ticks) |
  512. V_NUM_PKTS_DROPPED(DROP_PKTS_CNT),
  513. adapter->regs + A_TP_TX_DROP_CONFIG);
  514. }
  515. writel(F_TP_RESET, adapter->regs + A_TP_RESET);
  516. }
  517. int __devinit t1_get_board_rev(adapter_t *adapter, const struct board_info *bi,
  518. struct adapter_params *p)
  519. {
  520. p->chip_version = bi->chip_term;
  521. if (p->chip_version == CHBT_TERM_T1 ||
  522. p->chip_version == CHBT_TERM_T2) {
  523. u32 val = readl(adapter->regs + A_TP_PC_CONFIG);
  524. val = G_TP_PC_REV(val);
  525. if (val == 2)
  526. p->chip_revision = TERM_T1B;
  527. else if (val == 3)
  528. p->chip_revision = TERM_T2;
  529. else
  530. return -1;
  531. } else
  532. return -1;
  533. return 0;
  534. }
  535. /*
  536. * Enable board components other than the Chelsio chip, such as external MAC
  537. * and PHY.
  538. */
  539. static int board_init(adapter_t *adapter, const struct board_info *bi)
  540. {
  541. switch (bi->board) {
  542. case CHBT_BOARD_N110:
  543. case CHBT_BOARD_N210:
  544. writel(V_TPIPAR(0xf), adapter->regs + A_TPI_PAR);
  545. t1_tpi_write(adapter, A_ELMER0_GPO, 0x800);
  546. break;
  547. }
  548. return 0;
  549. }
  550. /*
  551. * Initialize and configure the Terminator HW modules. Note that external
  552. * MAC and PHYs are initialized separately.
  553. */
  554. int t1_init_hw_modules(adapter_t *adapter)
  555. {
  556. int err = -EIO;
  557. const struct board_info *bi = board_info(adapter);
  558. if (!bi->clock_mc4) {
  559. u32 val = readl(adapter->regs + A_MC4_CFG);
  560. writel(val | F_READY | F_MC4_SLOW, adapter->regs + A_MC4_CFG);
  561. writel(F_M_BUS_ENABLE | F_TCAM_RESET,
  562. adapter->regs + A_MC5_CONFIG);
  563. }
  564. if (adapter->espi && t1_espi_init(adapter->espi, bi->chip_mac,
  565. bi->espi_nports))
  566. goto out_err;
  567. t1_tp_reset(adapter, bi->clock_core);
  568. err = t1_sge_configure(adapter->sge, &adapter->params.sge);
  569. if (err)
  570. goto out_err;
  571. err = 0;
  572. out_err:
  573. return err;
  574. }
  575. /*
  576. * Determine a card's PCI mode.
  577. */
  578. static void __devinit get_pci_mode(adapter_t *adapter, struct chelsio_pci_params *p)
  579. {
  580. static const unsigned short speed_map[] = { 33, 66, 100, 133 };
  581. u32 pci_mode;
  582. pci_read_config_dword(adapter->pdev, A_PCICFG_MODE, &pci_mode);
  583. p->speed = speed_map[G_PCI_MODE_CLK(pci_mode)];
  584. p->width = (pci_mode & F_PCI_MODE_64BIT) ? 64 : 32;
  585. p->is_pcix = (pci_mode & F_PCI_MODE_PCIX) != 0;
  586. }
  587. /*
  588. * Release the structures holding the SW per-Terminator-HW-module state.
  589. */
  590. void t1_free_sw_modules(adapter_t *adapter)
  591. {
  592. unsigned int i;
  593. for_each_port(adapter, i) {
  594. struct cmac *mac = adapter->port[i].mac;
  595. struct cphy *phy = adapter->port[i].phy;
  596. if (mac)
  597. mac->ops->destroy(mac);
  598. if (phy)
  599. phy->ops->destroy(phy);
  600. }
  601. if (adapter->sge)
  602. t1_sge_destroy(adapter->sge);
  603. if (adapter->espi)
  604. t1_espi_destroy(adapter->espi);
  605. }
  606. static void __devinit init_link_config(struct link_config *lc,
  607. const struct board_info *bi)
  608. {
  609. lc->supported = bi->caps;
  610. lc->requested_speed = lc->speed = SPEED_INVALID;
  611. lc->requested_duplex = lc->duplex = DUPLEX_INVALID;
  612. lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
  613. if (lc->supported & SUPPORTED_Autoneg) {
  614. lc->advertising = lc->supported;
  615. lc->autoneg = AUTONEG_ENABLE;
  616. lc->requested_fc |= PAUSE_AUTONEG;
  617. } else {
  618. lc->advertising = 0;
  619. lc->autoneg = AUTONEG_DISABLE;
  620. }
  621. }
  622. /*
  623. * Allocate and initialize the data structures that hold the SW state of
  624. * the Terminator HW modules.
  625. */
  626. int __devinit t1_init_sw_modules(adapter_t *adapter,
  627. const struct board_info *bi)
  628. {
  629. unsigned int i;
  630. adapter->params.brd_info = bi;
  631. adapter->params.nports = bi->port_number;
  632. adapter->params.stats_update_period = bi->gmac->stats_update_period;
  633. adapter->sge = t1_sge_create(adapter, &adapter->params.sge);
  634. if (!adapter->sge) {
  635. CH_ERR("%s: SGE initialization failed\n",
  636. adapter->name);
  637. goto error;
  638. }
  639. if (bi->espi_nports && !(adapter->espi = t1_espi_create(adapter))) {
  640. CH_ERR("%s: ESPI initialization failed\n",
  641. adapter->name);
  642. goto error;
  643. }
  644. board_init(adapter, bi);
  645. bi->mdio_ops->init(adapter, bi);
  646. if (bi->gphy->reset)
  647. bi->gphy->reset(adapter);
  648. if (bi->gmac->reset)
  649. bi->gmac->reset(adapter);
  650. for_each_port(adapter, i) {
  651. u8 hw_addr[6];
  652. struct cmac *mac;
  653. int phy_addr = bi->mdio_phybaseaddr + i;
  654. adapter->port[i].phy = bi->gphy->create(adapter, phy_addr,
  655. bi->mdio_ops);
  656. if (!adapter->port[i].phy) {
  657. CH_ERR("%s: PHY %d initialization failed\n",
  658. adapter->name, i);
  659. goto error;
  660. }
  661. adapter->port[i].mac = mac = bi->gmac->create(adapter, i);
  662. if (!mac) {
  663. CH_ERR("%s: MAC %d initialization failed\n",
  664. adapter->name, i);
  665. goto error;
  666. }
  667. /*
  668. * Get the port's MAC addresses either from the EEPROM if one
  669. * exists or the one hardcoded in the MAC.
  670. */
  671. if (vpd_macaddress_get(adapter, i, hw_addr)) {
  672. CH_ERR("%s: could not read MAC address from VPD ROM\n",
  673. adapter->port[i].dev->name);
  674. goto error;
  675. }
  676. memcpy(adapter->port[i].dev->dev_addr, hw_addr, ETH_ALEN);
  677. init_link_config(&adapter->port[i].link_config, bi);
  678. }
  679. get_pci_mode(adapter, &adapter->params.pci);
  680. t1_interrupts_clear(adapter);
  681. return 0;
  682. error:
  683. t1_free_sw_modules(adapter);
  684. return -1;
  685. }