subr.c 31 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132
  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 "tp.h"
  46. #include "espi.h"
  47. /**
  48. * t1_wait_op_done - wait until an operation is completed
  49. * @adapter: the adapter performing the operation
  50. * @reg: the register to check for completion
  51. * @mask: a single-bit field within @reg that indicates completion
  52. * @polarity: the value of the field when the operation is completed
  53. * @attempts: number of check iterations
  54. * @delay: delay in usecs between iterations
  55. *
  56. * Wait until an operation is completed by checking a bit in a register
  57. * up to @attempts times. Returns %0 if the operation completes and %1
  58. * otherwise.
  59. */
  60. static int t1_wait_op_done(adapter_t *adapter, int reg, u32 mask, int polarity,
  61. int attempts, int delay)
  62. {
  63. while (1) {
  64. u32 val = readl(adapter->regs + reg) & mask;
  65. if (!!val == polarity)
  66. return 0;
  67. if (--attempts == 0)
  68. return 1;
  69. if (delay)
  70. udelay(delay);
  71. }
  72. }
  73. #define TPI_ATTEMPTS 50
  74. /*
  75. * Write a register over the TPI interface (unlocked and locked versions).
  76. */
  77. int __t1_tpi_write(adapter_t *adapter, u32 addr, u32 value)
  78. {
  79. int tpi_busy;
  80. writel(addr, adapter->regs + A_TPI_ADDR);
  81. writel(value, adapter->regs + A_TPI_WR_DATA);
  82. writel(F_TPIWR, adapter->regs + A_TPI_CSR);
  83. tpi_busy = t1_wait_op_done(adapter, A_TPI_CSR, F_TPIRDY, 1,
  84. TPI_ATTEMPTS, 3);
  85. if (tpi_busy)
  86. pr_alert("%s: TPI write to 0x%x failed\n",
  87. adapter->name, addr);
  88. return tpi_busy;
  89. }
  90. int t1_tpi_write(adapter_t *adapter, u32 addr, u32 value)
  91. {
  92. int ret;
  93. spin_lock(&adapter->tpi_lock);
  94. ret = __t1_tpi_write(adapter, addr, value);
  95. spin_unlock(&adapter->tpi_lock);
  96. return ret;
  97. }
  98. /*
  99. * Read a register over the TPI interface (unlocked and locked versions).
  100. */
  101. int __t1_tpi_read(adapter_t *adapter, u32 addr, u32 *valp)
  102. {
  103. int tpi_busy;
  104. writel(addr, adapter->regs + A_TPI_ADDR);
  105. writel(0, adapter->regs + A_TPI_CSR);
  106. tpi_busy = t1_wait_op_done(adapter, A_TPI_CSR, F_TPIRDY, 1,
  107. TPI_ATTEMPTS, 3);
  108. if (tpi_busy)
  109. pr_alert("%s: TPI read from 0x%x failed\n",
  110. adapter->name, addr);
  111. else
  112. *valp = readl(adapter->regs + A_TPI_RD_DATA);
  113. return tpi_busy;
  114. }
  115. int t1_tpi_read(adapter_t *adapter, u32 addr, u32 *valp)
  116. {
  117. int ret;
  118. spin_lock(&adapter->tpi_lock);
  119. ret = __t1_tpi_read(adapter, addr, valp);
  120. spin_unlock(&adapter->tpi_lock);
  121. return ret;
  122. }
  123. /*
  124. * Set a TPI parameter.
  125. */
  126. static void t1_tpi_par(adapter_t *adapter, u32 value)
  127. {
  128. writel(V_TPIPAR(value), adapter->regs + A_TPI_PAR);
  129. }
  130. /*
  131. * Called when a port's link settings change to propagate the new values to the
  132. * associated PHY and MAC. After performing the common tasks it invokes an
  133. * OS-specific handler.
  134. */
  135. void t1_link_changed(adapter_t *adapter, int port_id)
  136. {
  137. int link_ok, speed, duplex, fc;
  138. struct cphy *phy = adapter->port[port_id].phy;
  139. struct link_config *lc = &adapter->port[port_id].link_config;
  140. phy->ops->get_link_status(phy, &link_ok, &speed, &duplex, &fc);
  141. lc->speed = speed < 0 ? SPEED_INVALID : speed;
  142. lc->duplex = duplex < 0 ? DUPLEX_INVALID : duplex;
  143. if (!(lc->requested_fc & PAUSE_AUTONEG))
  144. fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  145. if (link_ok && speed >= 0 && lc->autoneg == AUTONEG_ENABLE) {
  146. /* Set MAC speed, duplex, and flow control to match PHY. */
  147. struct cmac *mac = adapter->port[port_id].mac;
  148. mac->ops->set_speed_duplex_fc(mac, speed, duplex, fc);
  149. lc->fc = (unsigned char)fc;
  150. }
  151. t1_link_negotiated(adapter, port_id, link_ok, speed, duplex, fc);
  152. }
  153. static int t1_pci_intr_handler(adapter_t *adapter)
  154. {
  155. u32 pcix_cause;
  156. pci_read_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE, &pcix_cause);
  157. if (pcix_cause) {
  158. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE,
  159. pcix_cause);
  160. t1_fatal_err(adapter); /* PCI errors are fatal */
  161. }
  162. return 0;
  163. }
  164. #ifdef CONFIG_CHELSIO_T1_1G
  165. #include "fpga_defs.h"
  166. /*
  167. * PHY interrupt handler for FPGA boards.
  168. */
  169. static int fpga_phy_intr_handler(adapter_t *adapter)
  170. {
  171. int p;
  172. u32 cause = readl(adapter->regs + FPGA_GMAC_ADDR_INTERRUPT_CAUSE);
  173. for_each_port(adapter, p)
  174. if (cause & (1 << p)) {
  175. struct cphy *phy = adapter->port[p].phy;
  176. int phy_cause = phy->ops->interrupt_handler(phy);
  177. if (phy_cause & cphy_cause_link_change)
  178. t1_link_changed(adapter, p);
  179. }
  180. writel(cause, adapter->regs + FPGA_GMAC_ADDR_INTERRUPT_CAUSE);
  181. return 0;
  182. }
  183. /*
  184. * Slow path interrupt handler for FPGAs.
  185. */
  186. static int fpga_slow_intr(adapter_t *adapter)
  187. {
  188. u32 cause = readl(adapter->regs + A_PL_CAUSE);
  189. cause &= ~F_PL_INTR_SGE_DATA;
  190. if (cause & F_PL_INTR_SGE_ERR)
  191. t1_sge_intr_error_handler(adapter->sge);
  192. if (cause & FPGA_PCIX_INTERRUPT_GMAC)
  193. fpga_phy_intr_handler(adapter);
  194. if (cause & FPGA_PCIX_INTERRUPT_TP) {
  195. /*
  196. * FPGA doesn't support MC4 interrupts and it requires
  197. * this odd layer of indirection for MC5.
  198. */
  199. u32 tp_cause = readl(adapter->regs + FPGA_TP_ADDR_INTERRUPT_CAUSE);
  200. /* Clear TP interrupt */
  201. writel(tp_cause, adapter->regs + FPGA_TP_ADDR_INTERRUPT_CAUSE);
  202. }
  203. if (cause & FPGA_PCIX_INTERRUPT_PCIX)
  204. t1_pci_intr_handler(adapter);
  205. /* Clear the interrupts just processed. */
  206. if (cause)
  207. writel(cause, adapter->regs + A_PL_CAUSE);
  208. return cause != 0;
  209. }
  210. #endif
  211. /*
  212. * Wait until Elmer's MI1 interface is ready for new operations.
  213. */
  214. static int mi1_wait_until_ready(adapter_t *adapter, int mi1_reg)
  215. {
  216. int attempts = 100, busy;
  217. do {
  218. u32 val;
  219. __t1_tpi_read(adapter, mi1_reg, &val);
  220. busy = val & F_MI1_OP_BUSY;
  221. if (busy)
  222. udelay(10);
  223. } while (busy && --attempts);
  224. if (busy)
  225. pr_alert("%s: MDIO operation timed out\n", adapter->name);
  226. return busy;
  227. }
  228. /*
  229. * MI1 MDIO initialization.
  230. */
  231. static void mi1_mdio_init(adapter_t *adapter, const struct board_info *bi)
  232. {
  233. u32 clkdiv = bi->clock_elmer0 / (2 * bi->mdio_mdc) - 1;
  234. u32 val = F_MI1_PREAMBLE_ENABLE | V_MI1_MDI_INVERT(bi->mdio_mdiinv) |
  235. V_MI1_MDI_ENABLE(bi->mdio_mdien) | V_MI1_CLK_DIV(clkdiv);
  236. if (!(bi->caps & SUPPORTED_10000baseT_Full))
  237. val |= V_MI1_SOF(1);
  238. t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_CFG, val);
  239. }
  240. #if defined(CONFIG_CHELSIO_T1_1G)
  241. /*
  242. * Elmer MI1 MDIO read/write operations.
  243. */
  244. static int mi1_mdio_read(struct net_device *dev, int phy_addr, int mmd_addr,
  245. u16 reg_addr)
  246. {
  247. struct adapter *adapter = dev->ml_priv;
  248. u32 addr = V_MI1_REG_ADDR(reg_addr) | V_MI1_PHY_ADDR(phy_addr);
  249. unsigned int val;
  250. spin_lock(&adapter->tpi_lock);
  251. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  252. __t1_tpi_write(adapter,
  253. A_ELMER0_PORT0_MI1_OP, MI1_OP_DIRECT_READ);
  254. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  255. __t1_tpi_read(adapter, A_ELMER0_PORT0_MI1_DATA, &val);
  256. spin_unlock(&adapter->tpi_lock);
  257. return val;
  258. }
  259. static int mi1_mdio_write(struct net_device *dev, int phy_addr, int mmd_addr,
  260. u16 reg_addr, u16 val)
  261. {
  262. struct adapter *adapter = dev->ml_priv;
  263. u32 addr = V_MI1_REG_ADDR(reg_addr) | V_MI1_PHY_ADDR(phy_addr);
  264. spin_lock(&adapter->tpi_lock);
  265. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  266. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, val);
  267. __t1_tpi_write(adapter,
  268. A_ELMER0_PORT0_MI1_OP, MI1_OP_DIRECT_WRITE);
  269. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  270. spin_unlock(&adapter->tpi_lock);
  271. return 0;
  272. }
  273. #if defined(CONFIG_CHELSIO_T1_1G)
  274. static const struct mdio_ops mi1_mdio_ops = {
  275. .init = mi1_mdio_init,
  276. .read = mi1_mdio_read,
  277. .write = mi1_mdio_write,
  278. .mode_support = MDIO_SUPPORTS_C22
  279. };
  280. #endif
  281. #endif
  282. static int mi1_mdio_ext_read(struct net_device *dev, int phy_addr, int mmd_addr,
  283. u16 reg_addr)
  284. {
  285. struct adapter *adapter = dev->ml_priv;
  286. u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr);
  287. unsigned int val;
  288. spin_lock(&adapter->tpi_lock);
  289. /* Write the address we want. */
  290. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  291. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr);
  292. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP,
  293. MI1_OP_INDIRECT_ADDRESS);
  294. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  295. /* Write the operation we want. */
  296. __t1_tpi_write(adapter,
  297. A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_READ);
  298. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  299. /* Read the data. */
  300. __t1_tpi_read(adapter, A_ELMER0_PORT0_MI1_DATA, &val);
  301. spin_unlock(&adapter->tpi_lock);
  302. return val;
  303. }
  304. static int mi1_mdio_ext_write(struct net_device *dev, int phy_addr,
  305. int mmd_addr, u16 reg_addr, u16 val)
  306. {
  307. struct adapter *adapter = dev->ml_priv;
  308. u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr);
  309. spin_lock(&adapter->tpi_lock);
  310. /* Write the address we want. */
  311. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  312. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr);
  313. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP,
  314. MI1_OP_INDIRECT_ADDRESS);
  315. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  316. /* Write the data. */
  317. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, val);
  318. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_WRITE);
  319. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  320. spin_unlock(&adapter->tpi_lock);
  321. return 0;
  322. }
  323. static const struct mdio_ops mi1_mdio_ext_ops = {
  324. .init = mi1_mdio_init,
  325. .read = mi1_mdio_ext_read,
  326. .write = mi1_mdio_ext_write,
  327. .mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22
  328. };
  329. enum {
  330. CH_BRD_T110_1CU,
  331. CH_BRD_N110_1F,
  332. CH_BRD_N210_1F,
  333. CH_BRD_T210_1F,
  334. CH_BRD_T210_1CU,
  335. CH_BRD_N204_4CU,
  336. };
  337. static const struct board_info t1_board[] = {
  338. {
  339. .board = CHBT_BOARD_CHT110,
  340. .port_number = 1,
  341. .caps = SUPPORTED_10000baseT_Full,
  342. .chip_term = CHBT_TERM_T1,
  343. .chip_mac = CHBT_MAC_PM3393,
  344. .chip_phy = CHBT_PHY_MY3126,
  345. .clock_core = 125000000,
  346. .clock_mc3 = 150000000,
  347. .clock_mc4 = 125000000,
  348. .espi_nports = 1,
  349. .clock_elmer0 = 44,
  350. .mdio_mdien = 1,
  351. .mdio_mdiinv = 1,
  352. .mdio_mdc = 1,
  353. .mdio_phybaseaddr = 1,
  354. .gmac = &t1_pm3393_ops,
  355. .gphy = &t1_my3126_ops,
  356. .mdio_ops = &mi1_mdio_ext_ops,
  357. .desc = "Chelsio T110 1x10GBase-CX4 TOE",
  358. },
  359. {
  360. .board = CHBT_BOARD_N110,
  361. .port_number = 1,
  362. .caps = SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE,
  363. .chip_term = CHBT_TERM_T1,
  364. .chip_mac = CHBT_MAC_PM3393,
  365. .chip_phy = CHBT_PHY_88X2010,
  366. .clock_core = 125000000,
  367. .espi_nports = 1,
  368. .clock_elmer0 = 44,
  369. .mdio_mdien = 0,
  370. .mdio_mdiinv = 0,
  371. .mdio_mdc = 1,
  372. .mdio_phybaseaddr = 0,
  373. .gmac = &t1_pm3393_ops,
  374. .gphy = &t1_mv88x201x_ops,
  375. .mdio_ops = &mi1_mdio_ext_ops,
  376. .desc = "Chelsio N110 1x10GBaseX NIC",
  377. },
  378. {
  379. .board = CHBT_BOARD_N210,
  380. .port_number = 1,
  381. .caps = SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE,
  382. .chip_term = CHBT_TERM_T2,
  383. .chip_mac = CHBT_MAC_PM3393,
  384. .chip_phy = CHBT_PHY_88X2010,
  385. .clock_core = 125000000,
  386. .espi_nports = 1,
  387. .clock_elmer0 = 44,
  388. .mdio_mdien = 0,
  389. .mdio_mdiinv = 0,
  390. .mdio_mdc = 1,
  391. .mdio_phybaseaddr = 0,
  392. .gmac = &t1_pm3393_ops,
  393. .gphy = &t1_mv88x201x_ops,
  394. .mdio_ops = &mi1_mdio_ext_ops,
  395. .desc = "Chelsio N210 1x10GBaseX NIC",
  396. },
  397. {
  398. .board = CHBT_BOARD_CHT210,
  399. .port_number = 1,
  400. .caps = SUPPORTED_10000baseT_Full,
  401. .chip_term = CHBT_TERM_T2,
  402. .chip_mac = CHBT_MAC_PM3393,
  403. .chip_phy = CHBT_PHY_88X2010,
  404. .clock_core = 125000000,
  405. .clock_mc3 = 133000000,
  406. .clock_mc4 = 125000000,
  407. .espi_nports = 1,
  408. .clock_elmer0 = 44,
  409. .mdio_mdien = 0,
  410. .mdio_mdiinv = 0,
  411. .mdio_mdc = 1,
  412. .mdio_phybaseaddr = 0,
  413. .gmac = &t1_pm3393_ops,
  414. .gphy = &t1_mv88x201x_ops,
  415. .mdio_ops = &mi1_mdio_ext_ops,
  416. .desc = "Chelsio T210 1x10GBaseX TOE",
  417. },
  418. {
  419. .board = CHBT_BOARD_CHT210,
  420. .port_number = 1,
  421. .caps = SUPPORTED_10000baseT_Full,
  422. .chip_term = CHBT_TERM_T2,
  423. .chip_mac = CHBT_MAC_PM3393,
  424. .chip_phy = CHBT_PHY_MY3126,
  425. .clock_core = 125000000,
  426. .clock_mc3 = 133000000,
  427. .clock_mc4 = 125000000,
  428. .espi_nports = 1,
  429. .clock_elmer0 = 44,
  430. .mdio_mdien = 1,
  431. .mdio_mdiinv = 1,
  432. .mdio_mdc = 1,
  433. .mdio_phybaseaddr = 1,
  434. .gmac = &t1_pm3393_ops,
  435. .gphy = &t1_my3126_ops,
  436. .mdio_ops = &mi1_mdio_ext_ops,
  437. .desc = "Chelsio T210 1x10GBase-CX4 TOE",
  438. },
  439. #ifdef CONFIG_CHELSIO_T1_1G
  440. {
  441. .board = CHBT_BOARD_CHN204,
  442. .port_number = 4,
  443. .caps = SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full
  444. | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full
  445. | SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
  446. SUPPORTED_PAUSE | SUPPORTED_TP,
  447. .chip_term = CHBT_TERM_T2,
  448. .chip_mac = CHBT_MAC_VSC7321,
  449. .chip_phy = CHBT_PHY_88E1111,
  450. .clock_core = 100000000,
  451. .espi_nports = 4,
  452. .clock_elmer0 = 44,
  453. .mdio_mdien = 0,
  454. .mdio_mdiinv = 0,
  455. .mdio_mdc = 0,
  456. .mdio_phybaseaddr = 4,
  457. .gmac = &t1_vsc7326_ops,
  458. .gphy = &t1_mv88e1xxx_ops,
  459. .mdio_ops = &mi1_mdio_ops,
  460. .desc = "Chelsio N204 4x100/1000BaseT NIC",
  461. },
  462. #endif
  463. };
  464. DEFINE_PCI_DEVICE_TABLE(t1_pci_tbl) = {
  465. CH_DEVICE(8, 0, CH_BRD_T110_1CU),
  466. CH_DEVICE(8, 1, CH_BRD_T110_1CU),
  467. CH_DEVICE(7, 0, CH_BRD_N110_1F),
  468. CH_DEVICE(10, 1, CH_BRD_N210_1F),
  469. CH_DEVICE(11, 1, CH_BRD_T210_1F),
  470. CH_DEVICE(14, 1, CH_BRD_T210_1CU),
  471. CH_DEVICE(16, 1, CH_BRD_N204_4CU),
  472. { 0 }
  473. };
  474. MODULE_DEVICE_TABLE(pci, t1_pci_tbl);
  475. /*
  476. * Return the board_info structure with a given index. Out-of-range indices
  477. * return NULL.
  478. */
  479. const struct board_info *t1_get_board_info(unsigned int board_id)
  480. {
  481. return board_id < ARRAY_SIZE(t1_board) ? &t1_board[board_id] : NULL;
  482. }
  483. struct chelsio_vpd_t {
  484. u32 format_version;
  485. u8 serial_number[16];
  486. u8 mac_base_address[6];
  487. u8 pad[2]; /* make multiple-of-4 size requirement explicit */
  488. };
  489. #define EEPROMSIZE (8 * 1024)
  490. #define EEPROM_MAX_POLL 4
  491. /*
  492. * Read SEEPROM. A zero is written to the flag register when the addres is
  493. * written to the Control register. The hardware device will set the flag to a
  494. * one when 4B have been transferred to the Data register.
  495. */
  496. int t1_seeprom_read(adapter_t *adapter, u32 addr, __le32 *data)
  497. {
  498. int i = EEPROM_MAX_POLL;
  499. u16 val;
  500. u32 v;
  501. if (addr >= EEPROMSIZE || (addr & 3))
  502. return -EINVAL;
  503. pci_write_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, (u16)addr);
  504. do {
  505. udelay(50);
  506. pci_read_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, &val);
  507. } while (!(val & F_VPD_OP_FLAG) && --i);
  508. if (!(val & F_VPD_OP_FLAG)) {
  509. pr_err("%s: reading EEPROM address 0x%x failed\n",
  510. adapter->name, addr);
  511. return -EIO;
  512. }
  513. pci_read_config_dword(adapter->pdev, A_PCICFG_VPD_DATA, &v);
  514. *data = cpu_to_le32(v);
  515. return 0;
  516. }
  517. static int t1_eeprom_vpd_get(adapter_t *adapter, struct chelsio_vpd_t *vpd)
  518. {
  519. int addr, ret = 0;
  520. for (addr = 0; !ret && addr < sizeof(*vpd); addr += sizeof(u32))
  521. ret = t1_seeprom_read(adapter, addr,
  522. (__le32 *)((u8 *)vpd + addr));
  523. return ret;
  524. }
  525. /*
  526. * Read a port's MAC address from the VPD ROM.
  527. */
  528. static int vpd_macaddress_get(adapter_t *adapter, int index, u8 mac_addr[])
  529. {
  530. struct chelsio_vpd_t vpd;
  531. if (t1_eeprom_vpd_get(adapter, &vpd))
  532. return 1;
  533. memcpy(mac_addr, vpd.mac_base_address, 5);
  534. mac_addr[5] = vpd.mac_base_address[5] + index;
  535. return 0;
  536. }
  537. /*
  538. * Set up the MAC/PHY according to the requested link settings.
  539. *
  540. * If the PHY can auto-negotiate first decide what to advertise, then
  541. * enable/disable auto-negotiation as desired and reset.
  542. *
  543. * If the PHY does not auto-negotiate we just reset it.
  544. *
  545. * If auto-negotiation is off set the MAC to the proper speed/duplex/FC,
  546. * otherwise do it later based on the outcome of auto-negotiation.
  547. */
  548. int t1_link_start(struct cphy *phy, struct cmac *mac, struct link_config *lc)
  549. {
  550. unsigned int fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  551. if (lc->supported & SUPPORTED_Autoneg) {
  552. lc->advertising &= ~(ADVERTISED_ASYM_PAUSE | ADVERTISED_PAUSE);
  553. if (fc) {
  554. if (fc == ((PAUSE_RX | PAUSE_TX) &
  555. (mac->adapter->params.nports < 2)))
  556. lc->advertising |= ADVERTISED_PAUSE;
  557. else {
  558. lc->advertising |= ADVERTISED_ASYM_PAUSE;
  559. if (fc == PAUSE_RX)
  560. lc->advertising |= ADVERTISED_PAUSE;
  561. }
  562. }
  563. phy->ops->advertise(phy, lc->advertising);
  564. if (lc->autoneg == AUTONEG_DISABLE) {
  565. lc->speed = lc->requested_speed;
  566. lc->duplex = lc->requested_duplex;
  567. lc->fc = (unsigned char)fc;
  568. mac->ops->set_speed_duplex_fc(mac, lc->speed,
  569. lc->duplex, fc);
  570. /* Also disables autoneg */
  571. phy->state = PHY_AUTONEG_RDY;
  572. phy->ops->set_speed_duplex(phy, lc->speed, lc->duplex);
  573. phy->ops->reset(phy, 0);
  574. } else {
  575. phy->state = PHY_AUTONEG_EN;
  576. phy->ops->autoneg_enable(phy); /* also resets PHY */
  577. }
  578. } else {
  579. phy->state = PHY_AUTONEG_RDY;
  580. mac->ops->set_speed_duplex_fc(mac, -1, -1, fc);
  581. lc->fc = (unsigned char)fc;
  582. phy->ops->reset(phy, 0);
  583. }
  584. return 0;
  585. }
  586. /*
  587. * External interrupt handler for boards using elmer0.
  588. */
  589. int t1_elmer0_ext_intr_handler(adapter_t *adapter)
  590. {
  591. struct cphy *phy;
  592. int phy_cause;
  593. u32 cause;
  594. t1_tpi_read(adapter, A_ELMER0_INT_CAUSE, &cause);
  595. switch (board_info(adapter)->board) {
  596. #ifdef CONFIG_CHELSIO_T1_1G
  597. case CHBT_BOARD_CHT204:
  598. case CHBT_BOARD_CHT204E:
  599. case CHBT_BOARD_CHN204:
  600. case CHBT_BOARD_CHT204V: {
  601. int i, port_bit;
  602. for_each_port(adapter, i) {
  603. port_bit = i + 1;
  604. if (!(cause & (1 << port_bit)))
  605. continue;
  606. phy = adapter->port[i].phy;
  607. phy_cause = phy->ops->interrupt_handler(phy);
  608. if (phy_cause & cphy_cause_link_change)
  609. t1_link_changed(adapter, i);
  610. }
  611. break;
  612. }
  613. case CHBT_BOARD_CHT101:
  614. if (cause & ELMER0_GP_BIT1) { /* Marvell 88E1111 interrupt */
  615. phy = adapter->port[0].phy;
  616. phy_cause = phy->ops->interrupt_handler(phy);
  617. if (phy_cause & cphy_cause_link_change)
  618. t1_link_changed(adapter, 0);
  619. }
  620. break;
  621. case CHBT_BOARD_7500: {
  622. int p;
  623. /*
  624. * Elmer0's interrupt cause isn't useful here because there is
  625. * only one bit that can be set for all 4 ports. This means
  626. * we are forced to check every PHY's interrupt status
  627. * register to see who initiated the interrupt.
  628. */
  629. for_each_port(adapter, p) {
  630. phy = adapter->port[p].phy;
  631. phy_cause = phy->ops->interrupt_handler(phy);
  632. if (phy_cause & cphy_cause_link_change)
  633. t1_link_changed(adapter, p);
  634. }
  635. break;
  636. }
  637. #endif
  638. case CHBT_BOARD_CHT210:
  639. case CHBT_BOARD_N210:
  640. case CHBT_BOARD_N110:
  641. if (cause & ELMER0_GP_BIT6) { /* Marvell 88x2010 interrupt */
  642. phy = adapter->port[0].phy;
  643. phy_cause = phy->ops->interrupt_handler(phy);
  644. if (phy_cause & cphy_cause_link_change)
  645. t1_link_changed(adapter, 0);
  646. }
  647. break;
  648. case CHBT_BOARD_8000:
  649. case CHBT_BOARD_CHT110:
  650. if (netif_msg_intr(adapter))
  651. dev_dbg(&adapter->pdev->dev,
  652. "External interrupt cause 0x%x\n", cause);
  653. if (cause & ELMER0_GP_BIT1) { /* PMC3393 INTB */
  654. struct cmac *mac = adapter->port[0].mac;
  655. mac->ops->interrupt_handler(mac);
  656. }
  657. if (cause & ELMER0_GP_BIT5) { /* XPAK MOD_DETECT */
  658. u32 mod_detect;
  659. t1_tpi_read(adapter,
  660. A_ELMER0_GPI_STAT, &mod_detect);
  661. if (netif_msg_link(adapter))
  662. dev_info(&adapter->pdev->dev, "XPAK %s\n",
  663. mod_detect ? "removed" : "inserted");
  664. }
  665. break;
  666. }
  667. t1_tpi_write(adapter, A_ELMER0_INT_CAUSE, cause);
  668. return 0;
  669. }
  670. /* Enables all interrupts. */
  671. void t1_interrupts_enable(adapter_t *adapter)
  672. {
  673. unsigned int i;
  674. adapter->slow_intr_mask = F_PL_INTR_SGE_ERR | F_PL_INTR_TP;
  675. t1_sge_intr_enable(adapter->sge);
  676. t1_tp_intr_enable(adapter->tp);
  677. if (adapter->espi) {
  678. adapter->slow_intr_mask |= F_PL_INTR_ESPI;
  679. t1_espi_intr_enable(adapter->espi);
  680. }
  681. /* Enable MAC/PHY interrupts for each port. */
  682. for_each_port(adapter, i) {
  683. adapter->port[i].mac->ops->interrupt_enable(adapter->port[i].mac);
  684. adapter->port[i].phy->ops->interrupt_enable(adapter->port[i].phy);
  685. }
  686. /* Enable PCIX & external chip interrupts on ASIC boards. */
  687. if (t1_is_asic(adapter)) {
  688. u32 pl_intr = readl(adapter->regs + A_PL_ENABLE);
  689. /* PCI-X interrupts */
  690. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_ENABLE,
  691. 0xffffffff);
  692. adapter->slow_intr_mask |= F_PL_INTR_EXT | F_PL_INTR_PCIX;
  693. pl_intr |= F_PL_INTR_EXT | F_PL_INTR_PCIX;
  694. writel(pl_intr, adapter->regs + A_PL_ENABLE);
  695. }
  696. }
  697. /* Disables all interrupts. */
  698. void t1_interrupts_disable(adapter_t* adapter)
  699. {
  700. unsigned int i;
  701. t1_sge_intr_disable(adapter->sge);
  702. t1_tp_intr_disable(adapter->tp);
  703. if (adapter->espi)
  704. t1_espi_intr_disable(adapter->espi);
  705. /* Disable MAC/PHY interrupts for each port. */
  706. for_each_port(adapter, i) {
  707. adapter->port[i].mac->ops->interrupt_disable(adapter->port[i].mac);
  708. adapter->port[i].phy->ops->interrupt_disable(adapter->port[i].phy);
  709. }
  710. /* Disable PCIX & external chip interrupts. */
  711. if (t1_is_asic(adapter))
  712. writel(0, adapter->regs + A_PL_ENABLE);
  713. /* PCI-X interrupts */
  714. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_ENABLE, 0);
  715. adapter->slow_intr_mask = 0;
  716. }
  717. /* Clears all interrupts */
  718. void t1_interrupts_clear(adapter_t* adapter)
  719. {
  720. unsigned int i;
  721. t1_sge_intr_clear(adapter->sge);
  722. t1_tp_intr_clear(adapter->tp);
  723. if (adapter->espi)
  724. t1_espi_intr_clear(adapter->espi);
  725. /* Clear MAC/PHY interrupts for each port. */
  726. for_each_port(adapter, i) {
  727. adapter->port[i].mac->ops->interrupt_clear(adapter->port[i].mac);
  728. adapter->port[i].phy->ops->interrupt_clear(adapter->port[i].phy);
  729. }
  730. /* Enable interrupts for external devices. */
  731. if (t1_is_asic(adapter)) {
  732. u32 pl_intr = readl(adapter->regs + A_PL_CAUSE);
  733. writel(pl_intr | F_PL_INTR_EXT | F_PL_INTR_PCIX,
  734. adapter->regs + A_PL_CAUSE);
  735. }
  736. /* PCI-X interrupts */
  737. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE, 0xffffffff);
  738. }
  739. /*
  740. * Slow path interrupt handler for ASICs.
  741. */
  742. static int asic_slow_intr(adapter_t *adapter)
  743. {
  744. u32 cause = readl(adapter->regs + A_PL_CAUSE);
  745. cause &= adapter->slow_intr_mask;
  746. if (!cause)
  747. return 0;
  748. if (cause & F_PL_INTR_SGE_ERR)
  749. t1_sge_intr_error_handler(adapter->sge);
  750. if (cause & F_PL_INTR_TP)
  751. t1_tp_intr_handler(adapter->tp);
  752. if (cause & F_PL_INTR_ESPI)
  753. t1_espi_intr_handler(adapter->espi);
  754. if (cause & F_PL_INTR_PCIX)
  755. t1_pci_intr_handler(adapter);
  756. if (cause & F_PL_INTR_EXT)
  757. t1_elmer0_ext_intr(adapter);
  758. /* Clear the interrupts just processed. */
  759. writel(cause, adapter->regs + A_PL_CAUSE);
  760. readl(adapter->regs + A_PL_CAUSE); /* flush writes */
  761. return 1;
  762. }
  763. int t1_slow_intr_handler(adapter_t *adapter)
  764. {
  765. #ifdef CONFIG_CHELSIO_T1_1G
  766. if (!t1_is_asic(adapter))
  767. return fpga_slow_intr(adapter);
  768. #endif
  769. return asic_slow_intr(adapter);
  770. }
  771. /* Power sequencing is a work-around for Intel's XPAKs. */
  772. static void power_sequence_xpak(adapter_t* adapter)
  773. {
  774. u32 mod_detect;
  775. u32 gpo;
  776. /* Check for XPAK */
  777. t1_tpi_read(adapter, A_ELMER0_GPI_STAT, &mod_detect);
  778. if (!(ELMER0_GP_BIT5 & mod_detect)) {
  779. /* XPAK is present */
  780. t1_tpi_read(adapter, A_ELMER0_GPO, &gpo);
  781. gpo |= ELMER0_GP_BIT18;
  782. t1_tpi_write(adapter, A_ELMER0_GPO, gpo);
  783. }
  784. }
  785. int __devinit t1_get_board_rev(adapter_t *adapter, const struct board_info *bi,
  786. struct adapter_params *p)
  787. {
  788. p->chip_version = bi->chip_term;
  789. p->is_asic = (p->chip_version != CHBT_TERM_FPGA);
  790. if (p->chip_version == CHBT_TERM_T1 ||
  791. p->chip_version == CHBT_TERM_T2 ||
  792. p->chip_version == CHBT_TERM_FPGA) {
  793. u32 val = readl(adapter->regs + A_TP_PC_CONFIG);
  794. val = G_TP_PC_REV(val);
  795. if (val == 2)
  796. p->chip_revision = TERM_T1B;
  797. else if (val == 3)
  798. p->chip_revision = TERM_T2;
  799. else
  800. return -1;
  801. } else
  802. return -1;
  803. return 0;
  804. }
  805. /*
  806. * Enable board components other than the Chelsio chip, such as external MAC
  807. * and PHY.
  808. */
  809. static int board_init(adapter_t *adapter, const struct board_info *bi)
  810. {
  811. switch (bi->board) {
  812. case CHBT_BOARD_8000:
  813. case CHBT_BOARD_N110:
  814. case CHBT_BOARD_N210:
  815. case CHBT_BOARD_CHT210:
  816. t1_tpi_par(adapter, 0xf);
  817. t1_tpi_write(adapter, A_ELMER0_GPO, 0x800);
  818. break;
  819. case CHBT_BOARD_CHT110:
  820. t1_tpi_par(adapter, 0xf);
  821. t1_tpi_write(adapter, A_ELMER0_GPO, 0x1800);
  822. /* TBD XXX Might not need. This fixes a problem
  823. * described in the Intel SR XPAK errata.
  824. */
  825. power_sequence_xpak(adapter);
  826. break;
  827. #ifdef CONFIG_CHELSIO_T1_1G
  828. case CHBT_BOARD_CHT204E:
  829. /* add config space write here */
  830. case CHBT_BOARD_CHT204:
  831. case CHBT_BOARD_CHT204V:
  832. case CHBT_BOARD_CHN204:
  833. t1_tpi_par(adapter, 0xf);
  834. t1_tpi_write(adapter, A_ELMER0_GPO, 0x804);
  835. break;
  836. case CHBT_BOARD_CHT101:
  837. case CHBT_BOARD_7500:
  838. t1_tpi_par(adapter, 0xf);
  839. t1_tpi_write(adapter, A_ELMER0_GPO, 0x1804);
  840. break;
  841. #endif
  842. }
  843. return 0;
  844. }
  845. /*
  846. * Initialize and configure the Terminator HW modules. Note that external
  847. * MAC and PHYs are initialized separately.
  848. */
  849. int t1_init_hw_modules(adapter_t *adapter)
  850. {
  851. int err = -EIO;
  852. const struct board_info *bi = board_info(adapter);
  853. if (!bi->clock_mc4) {
  854. u32 val = readl(adapter->regs + A_MC4_CFG);
  855. writel(val | F_READY | F_MC4_SLOW, adapter->regs + A_MC4_CFG);
  856. writel(F_M_BUS_ENABLE | F_TCAM_RESET,
  857. adapter->regs + A_MC5_CONFIG);
  858. }
  859. if (adapter->espi && t1_espi_init(adapter->espi, bi->chip_mac,
  860. bi->espi_nports))
  861. goto out_err;
  862. if (t1_tp_reset(adapter->tp, &adapter->params.tp, bi->clock_core))
  863. goto out_err;
  864. err = t1_sge_configure(adapter->sge, &adapter->params.sge);
  865. if (err)
  866. goto out_err;
  867. err = 0;
  868. out_err:
  869. return err;
  870. }
  871. /*
  872. * Determine a card's PCI mode.
  873. */
  874. static void __devinit get_pci_mode(adapter_t *adapter, struct chelsio_pci_params *p)
  875. {
  876. static const unsigned short speed_map[] = { 33, 66, 100, 133 };
  877. u32 pci_mode;
  878. pci_read_config_dword(adapter->pdev, A_PCICFG_MODE, &pci_mode);
  879. p->speed = speed_map[G_PCI_MODE_CLK(pci_mode)];
  880. p->width = (pci_mode & F_PCI_MODE_64BIT) ? 64 : 32;
  881. p->is_pcix = (pci_mode & F_PCI_MODE_PCIX) != 0;
  882. }
  883. /*
  884. * Release the structures holding the SW per-Terminator-HW-module state.
  885. */
  886. void t1_free_sw_modules(adapter_t *adapter)
  887. {
  888. unsigned int i;
  889. for_each_port(adapter, i) {
  890. struct cmac *mac = adapter->port[i].mac;
  891. struct cphy *phy = adapter->port[i].phy;
  892. if (mac)
  893. mac->ops->destroy(mac);
  894. if (phy)
  895. phy->ops->destroy(phy);
  896. }
  897. if (adapter->sge)
  898. t1_sge_destroy(adapter->sge);
  899. if (adapter->tp)
  900. t1_tp_destroy(adapter->tp);
  901. if (adapter->espi)
  902. t1_espi_destroy(adapter->espi);
  903. }
  904. static void __devinit init_link_config(struct link_config *lc,
  905. const struct board_info *bi)
  906. {
  907. lc->supported = bi->caps;
  908. lc->requested_speed = lc->speed = SPEED_INVALID;
  909. lc->requested_duplex = lc->duplex = DUPLEX_INVALID;
  910. lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
  911. if (lc->supported & SUPPORTED_Autoneg) {
  912. lc->advertising = lc->supported;
  913. lc->autoneg = AUTONEG_ENABLE;
  914. lc->requested_fc |= PAUSE_AUTONEG;
  915. } else {
  916. lc->advertising = 0;
  917. lc->autoneg = AUTONEG_DISABLE;
  918. }
  919. }
  920. /*
  921. * Allocate and initialize the data structures that hold the SW state of
  922. * the Terminator HW modules.
  923. */
  924. int __devinit t1_init_sw_modules(adapter_t *adapter,
  925. const struct board_info *bi)
  926. {
  927. unsigned int i;
  928. adapter->params.brd_info = bi;
  929. adapter->params.nports = bi->port_number;
  930. adapter->params.stats_update_period = bi->gmac->stats_update_period;
  931. adapter->sge = t1_sge_create(adapter, &adapter->params.sge);
  932. if (!adapter->sge) {
  933. pr_err("%s: SGE initialization failed\n",
  934. adapter->name);
  935. goto error;
  936. }
  937. if (bi->espi_nports && !(adapter->espi = t1_espi_create(adapter))) {
  938. pr_err("%s: ESPI initialization failed\n",
  939. adapter->name);
  940. goto error;
  941. }
  942. adapter->tp = t1_tp_create(adapter, &adapter->params.tp);
  943. if (!adapter->tp) {
  944. pr_err("%s: TP initialization failed\n",
  945. adapter->name);
  946. goto error;
  947. }
  948. board_init(adapter, bi);
  949. bi->mdio_ops->init(adapter, bi);
  950. if (bi->gphy->reset)
  951. bi->gphy->reset(adapter);
  952. if (bi->gmac->reset)
  953. bi->gmac->reset(adapter);
  954. for_each_port(adapter, i) {
  955. u8 hw_addr[6];
  956. struct cmac *mac;
  957. int phy_addr = bi->mdio_phybaseaddr + i;
  958. adapter->port[i].phy = bi->gphy->create(adapter->port[i].dev,
  959. phy_addr, bi->mdio_ops);
  960. if (!adapter->port[i].phy) {
  961. pr_err("%s: PHY %d initialization failed\n",
  962. adapter->name, i);
  963. goto error;
  964. }
  965. adapter->port[i].mac = mac = bi->gmac->create(adapter, i);
  966. if (!mac) {
  967. pr_err("%s: MAC %d initialization failed\n",
  968. adapter->name, i);
  969. goto error;
  970. }
  971. /*
  972. * Get the port's MAC addresses either from the EEPROM if one
  973. * exists or the one hardcoded in the MAC.
  974. */
  975. if (!t1_is_asic(adapter) || bi->chip_mac == CHBT_MAC_DUMMY)
  976. mac->ops->macaddress_get(mac, hw_addr);
  977. else if (vpd_macaddress_get(adapter, i, hw_addr)) {
  978. pr_err("%s: could not read MAC address from VPD ROM\n",
  979. adapter->port[i].dev->name);
  980. goto error;
  981. }
  982. memcpy(adapter->port[i].dev->dev_addr, hw_addr, ETH_ALEN);
  983. init_link_config(&adapter->port[i].link_config, bi);
  984. }
  985. get_pci_mode(adapter, &adapter->params.pci);
  986. t1_interrupts_clear(adapter);
  987. return 0;
  988. error:
  989. t1_free_sw_modules(adapter);
  990. return -1;
  991. }