subr.c 31 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131
  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. static const struct mdio_ops mi1_mdio_ops = {
  274. .init = mi1_mdio_init,
  275. .read = mi1_mdio_read,
  276. .write = mi1_mdio_write,
  277. .mode_support = MDIO_SUPPORTS_C22
  278. };
  279. #endif
  280. static int mi1_mdio_ext_read(struct net_device *dev, int phy_addr, int mmd_addr,
  281. u16 reg_addr)
  282. {
  283. struct adapter *adapter = dev->ml_priv;
  284. u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr);
  285. unsigned int val;
  286. spin_lock(&adapter->tpi_lock);
  287. /* Write the address we want. */
  288. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  289. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr);
  290. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP,
  291. MI1_OP_INDIRECT_ADDRESS);
  292. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  293. /* Write the operation we want. */
  294. __t1_tpi_write(adapter,
  295. A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_READ);
  296. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  297. /* Read the data. */
  298. __t1_tpi_read(adapter, A_ELMER0_PORT0_MI1_DATA, &val);
  299. spin_unlock(&adapter->tpi_lock);
  300. return val;
  301. }
  302. static int mi1_mdio_ext_write(struct net_device *dev, int phy_addr,
  303. int mmd_addr, u16 reg_addr, u16 val)
  304. {
  305. struct adapter *adapter = dev->ml_priv;
  306. u32 addr = V_MI1_REG_ADDR(mmd_addr) | V_MI1_PHY_ADDR(phy_addr);
  307. spin_lock(&adapter->tpi_lock);
  308. /* Write the address we want. */
  309. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_ADDR, addr);
  310. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, reg_addr);
  311. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP,
  312. MI1_OP_INDIRECT_ADDRESS);
  313. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  314. /* Write the data. */
  315. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_DATA, val);
  316. __t1_tpi_write(adapter, A_ELMER0_PORT0_MI1_OP, MI1_OP_INDIRECT_WRITE);
  317. mi1_wait_until_ready(adapter, A_ELMER0_PORT0_MI1_OP);
  318. spin_unlock(&adapter->tpi_lock);
  319. return 0;
  320. }
  321. static const struct mdio_ops mi1_mdio_ext_ops = {
  322. .init = mi1_mdio_init,
  323. .read = mi1_mdio_ext_read,
  324. .write = mi1_mdio_ext_write,
  325. .mode_support = MDIO_SUPPORTS_C45 | MDIO_EMULATE_C22
  326. };
  327. enum {
  328. CH_BRD_T110_1CU,
  329. CH_BRD_N110_1F,
  330. CH_BRD_N210_1F,
  331. CH_BRD_T210_1F,
  332. CH_BRD_T210_1CU,
  333. CH_BRD_N204_4CU,
  334. };
  335. static const struct board_info t1_board[] = {
  336. {
  337. .board = CHBT_BOARD_CHT110,
  338. .port_number = 1,
  339. .caps = SUPPORTED_10000baseT_Full,
  340. .chip_term = CHBT_TERM_T1,
  341. .chip_mac = CHBT_MAC_PM3393,
  342. .chip_phy = CHBT_PHY_MY3126,
  343. .clock_core = 125000000,
  344. .clock_mc3 = 150000000,
  345. .clock_mc4 = 125000000,
  346. .espi_nports = 1,
  347. .clock_elmer0 = 44,
  348. .mdio_mdien = 1,
  349. .mdio_mdiinv = 1,
  350. .mdio_mdc = 1,
  351. .mdio_phybaseaddr = 1,
  352. .gmac = &t1_pm3393_ops,
  353. .gphy = &t1_my3126_ops,
  354. .mdio_ops = &mi1_mdio_ext_ops,
  355. .desc = "Chelsio T110 1x10GBase-CX4 TOE",
  356. },
  357. {
  358. .board = CHBT_BOARD_N110,
  359. .port_number = 1,
  360. .caps = SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE,
  361. .chip_term = CHBT_TERM_T1,
  362. .chip_mac = CHBT_MAC_PM3393,
  363. .chip_phy = CHBT_PHY_88X2010,
  364. .clock_core = 125000000,
  365. .espi_nports = 1,
  366. .clock_elmer0 = 44,
  367. .mdio_mdien = 0,
  368. .mdio_mdiinv = 0,
  369. .mdio_mdc = 1,
  370. .mdio_phybaseaddr = 0,
  371. .gmac = &t1_pm3393_ops,
  372. .gphy = &t1_mv88x201x_ops,
  373. .mdio_ops = &mi1_mdio_ext_ops,
  374. .desc = "Chelsio N110 1x10GBaseX NIC",
  375. },
  376. {
  377. .board = CHBT_BOARD_N210,
  378. .port_number = 1,
  379. .caps = SUPPORTED_10000baseT_Full | SUPPORTED_FIBRE,
  380. .chip_term = CHBT_TERM_T2,
  381. .chip_mac = CHBT_MAC_PM3393,
  382. .chip_phy = CHBT_PHY_88X2010,
  383. .clock_core = 125000000,
  384. .espi_nports = 1,
  385. .clock_elmer0 = 44,
  386. .mdio_mdien = 0,
  387. .mdio_mdiinv = 0,
  388. .mdio_mdc = 1,
  389. .mdio_phybaseaddr = 0,
  390. .gmac = &t1_pm3393_ops,
  391. .gphy = &t1_mv88x201x_ops,
  392. .mdio_ops = &mi1_mdio_ext_ops,
  393. .desc = "Chelsio N210 1x10GBaseX NIC",
  394. },
  395. {
  396. .board = CHBT_BOARD_CHT210,
  397. .port_number = 1,
  398. .caps = SUPPORTED_10000baseT_Full,
  399. .chip_term = CHBT_TERM_T2,
  400. .chip_mac = CHBT_MAC_PM3393,
  401. .chip_phy = CHBT_PHY_88X2010,
  402. .clock_core = 125000000,
  403. .clock_mc3 = 133000000,
  404. .clock_mc4 = 125000000,
  405. .espi_nports = 1,
  406. .clock_elmer0 = 44,
  407. .mdio_mdien = 0,
  408. .mdio_mdiinv = 0,
  409. .mdio_mdc = 1,
  410. .mdio_phybaseaddr = 0,
  411. .gmac = &t1_pm3393_ops,
  412. .gphy = &t1_mv88x201x_ops,
  413. .mdio_ops = &mi1_mdio_ext_ops,
  414. .desc = "Chelsio T210 1x10GBaseX TOE",
  415. },
  416. {
  417. .board = CHBT_BOARD_CHT210,
  418. .port_number = 1,
  419. .caps = SUPPORTED_10000baseT_Full,
  420. .chip_term = CHBT_TERM_T2,
  421. .chip_mac = CHBT_MAC_PM3393,
  422. .chip_phy = CHBT_PHY_MY3126,
  423. .clock_core = 125000000,
  424. .clock_mc3 = 133000000,
  425. .clock_mc4 = 125000000,
  426. .espi_nports = 1,
  427. .clock_elmer0 = 44,
  428. .mdio_mdien = 1,
  429. .mdio_mdiinv = 1,
  430. .mdio_mdc = 1,
  431. .mdio_phybaseaddr = 1,
  432. .gmac = &t1_pm3393_ops,
  433. .gphy = &t1_my3126_ops,
  434. .mdio_ops = &mi1_mdio_ext_ops,
  435. .desc = "Chelsio T210 1x10GBase-CX4 TOE",
  436. },
  437. #ifdef CONFIG_CHELSIO_T1_1G
  438. {
  439. .board = CHBT_BOARD_CHN204,
  440. .port_number = 4,
  441. .caps = SUPPORTED_10baseT_Half | SUPPORTED_10baseT_Full
  442. | SUPPORTED_100baseT_Half | SUPPORTED_100baseT_Full
  443. | SUPPORTED_1000baseT_Full | SUPPORTED_Autoneg |
  444. SUPPORTED_PAUSE | SUPPORTED_TP,
  445. .chip_term = CHBT_TERM_T2,
  446. .chip_mac = CHBT_MAC_VSC7321,
  447. .chip_phy = CHBT_PHY_88E1111,
  448. .clock_core = 100000000,
  449. .espi_nports = 4,
  450. .clock_elmer0 = 44,
  451. .mdio_mdien = 0,
  452. .mdio_mdiinv = 0,
  453. .mdio_mdc = 0,
  454. .mdio_phybaseaddr = 4,
  455. .gmac = &t1_vsc7326_ops,
  456. .gphy = &t1_mv88e1xxx_ops,
  457. .mdio_ops = &mi1_mdio_ops,
  458. .desc = "Chelsio N204 4x100/1000BaseT NIC",
  459. },
  460. #endif
  461. };
  462. DEFINE_PCI_DEVICE_TABLE(t1_pci_tbl) = {
  463. CH_DEVICE(8, 0, CH_BRD_T110_1CU),
  464. CH_DEVICE(8, 1, CH_BRD_T110_1CU),
  465. CH_DEVICE(7, 0, CH_BRD_N110_1F),
  466. CH_DEVICE(10, 1, CH_BRD_N210_1F),
  467. CH_DEVICE(11, 1, CH_BRD_T210_1F),
  468. CH_DEVICE(14, 1, CH_BRD_T210_1CU),
  469. CH_DEVICE(16, 1, CH_BRD_N204_4CU),
  470. { 0 }
  471. };
  472. MODULE_DEVICE_TABLE(pci, t1_pci_tbl);
  473. /*
  474. * Return the board_info structure with a given index. Out-of-range indices
  475. * return NULL.
  476. */
  477. const struct board_info *t1_get_board_info(unsigned int board_id)
  478. {
  479. return board_id < ARRAY_SIZE(t1_board) ? &t1_board[board_id] : NULL;
  480. }
  481. struct chelsio_vpd_t {
  482. u32 format_version;
  483. u8 serial_number[16];
  484. u8 mac_base_address[6];
  485. u8 pad[2]; /* make multiple-of-4 size requirement explicit */
  486. };
  487. #define EEPROMSIZE (8 * 1024)
  488. #define EEPROM_MAX_POLL 4
  489. /*
  490. * Read SEEPROM. A zero is written to the flag register when the address is
  491. * written to the Control register. The hardware device will set the flag to a
  492. * one when 4B have been transferred to the Data register.
  493. */
  494. int t1_seeprom_read(adapter_t *adapter, u32 addr, __le32 *data)
  495. {
  496. int i = EEPROM_MAX_POLL;
  497. u16 val;
  498. u32 v;
  499. if (addr >= EEPROMSIZE || (addr & 3))
  500. return -EINVAL;
  501. pci_write_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, (u16)addr);
  502. do {
  503. udelay(50);
  504. pci_read_config_word(adapter->pdev, A_PCICFG_VPD_ADDR, &val);
  505. } while (!(val & F_VPD_OP_FLAG) && --i);
  506. if (!(val & F_VPD_OP_FLAG)) {
  507. pr_err("%s: reading EEPROM address 0x%x failed\n",
  508. adapter->name, addr);
  509. return -EIO;
  510. }
  511. pci_read_config_dword(adapter->pdev, A_PCICFG_VPD_DATA, &v);
  512. *data = cpu_to_le32(v);
  513. return 0;
  514. }
  515. static int t1_eeprom_vpd_get(adapter_t *adapter, struct chelsio_vpd_t *vpd)
  516. {
  517. int addr, ret = 0;
  518. for (addr = 0; !ret && addr < sizeof(*vpd); addr += sizeof(u32))
  519. ret = t1_seeprom_read(adapter, addr,
  520. (__le32 *)((u8 *)vpd + addr));
  521. return ret;
  522. }
  523. /*
  524. * Read a port's MAC address from the VPD ROM.
  525. */
  526. static int vpd_macaddress_get(adapter_t *adapter, int index, u8 mac_addr[])
  527. {
  528. struct chelsio_vpd_t vpd;
  529. if (t1_eeprom_vpd_get(adapter, &vpd))
  530. return 1;
  531. memcpy(mac_addr, vpd.mac_base_address, 5);
  532. mac_addr[5] = vpd.mac_base_address[5] + index;
  533. return 0;
  534. }
  535. /*
  536. * Set up the MAC/PHY according to the requested link settings.
  537. *
  538. * If the PHY can auto-negotiate first decide what to advertise, then
  539. * enable/disable auto-negotiation as desired and reset.
  540. *
  541. * If the PHY does not auto-negotiate we just reset it.
  542. *
  543. * If auto-negotiation is off set the MAC to the proper speed/duplex/FC,
  544. * otherwise do it later based on the outcome of auto-negotiation.
  545. */
  546. int t1_link_start(struct cphy *phy, struct cmac *mac, struct link_config *lc)
  547. {
  548. unsigned int fc = lc->requested_fc & (PAUSE_RX | PAUSE_TX);
  549. if (lc->supported & SUPPORTED_Autoneg) {
  550. lc->advertising &= ~(ADVERTISED_ASYM_PAUSE | ADVERTISED_PAUSE);
  551. if (fc) {
  552. if (fc == ((PAUSE_RX | PAUSE_TX) &
  553. (mac->adapter->params.nports < 2)))
  554. lc->advertising |= ADVERTISED_PAUSE;
  555. else {
  556. lc->advertising |= ADVERTISED_ASYM_PAUSE;
  557. if (fc == PAUSE_RX)
  558. lc->advertising |= ADVERTISED_PAUSE;
  559. }
  560. }
  561. phy->ops->advertise(phy, lc->advertising);
  562. if (lc->autoneg == AUTONEG_DISABLE) {
  563. lc->speed = lc->requested_speed;
  564. lc->duplex = lc->requested_duplex;
  565. lc->fc = (unsigned char)fc;
  566. mac->ops->set_speed_duplex_fc(mac, lc->speed,
  567. lc->duplex, fc);
  568. /* Also disables autoneg */
  569. phy->state = PHY_AUTONEG_RDY;
  570. phy->ops->set_speed_duplex(phy, lc->speed, lc->duplex);
  571. phy->ops->reset(phy, 0);
  572. } else {
  573. phy->state = PHY_AUTONEG_EN;
  574. phy->ops->autoneg_enable(phy); /* also resets PHY */
  575. }
  576. } else {
  577. phy->state = PHY_AUTONEG_RDY;
  578. mac->ops->set_speed_duplex_fc(mac, -1, -1, fc);
  579. lc->fc = (unsigned char)fc;
  580. phy->ops->reset(phy, 0);
  581. }
  582. return 0;
  583. }
  584. /*
  585. * External interrupt handler for boards using elmer0.
  586. */
  587. int t1_elmer0_ext_intr_handler(adapter_t *adapter)
  588. {
  589. struct cphy *phy;
  590. int phy_cause;
  591. u32 cause;
  592. t1_tpi_read(adapter, A_ELMER0_INT_CAUSE, &cause);
  593. switch (board_info(adapter)->board) {
  594. #ifdef CONFIG_CHELSIO_T1_1G
  595. case CHBT_BOARD_CHT204:
  596. case CHBT_BOARD_CHT204E:
  597. case CHBT_BOARD_CHN204:
  598. case CHBT_BOARD_CHT204V: {
  599. int i, port_bit;
  600. for_each_port(adapter, i) {
  601. port_bit = i + 1;
  602. if (!(cause & (1 << port_bit)))
  603. continue;
  604. phy = adapter->port[i].phy;
  605. phy_cause = phy->ops->interrupt_handler(phy);
  606. if (phy_cause & cphy_cause_link_change)
  607. t1_link_changed(adapter, i);
  608. }
  609. break;
  610. }
  611. case CHBT_BOARD_CHT101:
  612. if (cause & ELMER0_GP_BIT1) { /* Marvell 88E1111 interrupt */
  613. phy = adapter->port[0].phy;
  614. phy_cause = phy->ops->interrupt_handler(phy);
  615. if (phy_cause & cphy_cause_link_change)
  616. t1_link_changed(adapter, 0);
  617. }
  618. break;
  619. case CHBT_BOARD_7500: {
  620. int p;
  621. /*
  622. * Elmer0's interrupt cause isn't useful here because there is
  623. * only one bit that can be set for all 4 ports. This means
  624. * we are forced to check every PHY's interrupt status
  625. * register to see who initiated the interrupt.
  626. */
  627. for_each_port(adapter, p) {
  628. phy = adapter->port[p].phy;
  629. phy_cause = phy->ops->interrupt_handler(phy);
  630. if (phy_cause & cphy_cause_link_change)
  631. t1_link_changed(adapter, p);
  632. }
  633. break;
  634. }
  635. #endif
  636. case CHBT_BOARD_CHT210:
  637. case CHBT_BOARD_N210:
  638. case CHBT_BOARD_N110:
  639. if (cause & ELMER0_GP_BIT6) { /* Marvell 88x2010 interrupt */
  640. phy = adapter->port[0].phy;
  641. phy_cause = phy->ops->interrupt_handler(phy);
  642. if (phy_cause & cphy_cause_link_change)
  643. t1_link_changed(adapter, 0);
  644. }
  645. break;
  646. case CHBT_BOARD_8000:
  647. case CHBT_BOARD_CHT110:
  648. if (netif_msg_intr(adapter))
  649. dev_dbg(&adapter->pdev->dev,
  650. "External interrupt cause 0x%x\n", cause);
  651. if (cause & ELMER0_GP_BIT1) { /* PMC3393 INTB */
  652. struct cmac *mac = adapter->port[0].mac;
  653. mac->ops->interrupt_handler(mac);
  654. }
  655. if (cause & ELMER0_GP_BIT5) { /* XPAK MOD_DETECT */
  656. u32 mod_detect;
  657. t1_tpi_read(adapter,
  658. A_ELMER0_GPI_STAT, &mod_detect);
  659. if (netif_msg_link(adapter))
  660. dev_info(&adapter->pdev->dev, "XPAK %s\n",
  661. mod_detect ? "removed" : "inserted");
  662. }
  663. break;
  664. }
  665. t1_tpi_write(adapter, A_ELMER0_INT_CAUSE, cause);
  666. return 0;
  667. }
  668. /* Enables all interrupts. */
  669. void t1_interrupts_enable(adapter_t *adapter)
  670. {
  671. unsigned int i;
  672. adapter->slow_intr_mask = F_PL_INTR_SGE_ERR | F_PL_INTR_TP;
  673. t1_sge_intr_enable(adapter->sge);
  674. t1_tp_intr_enable(adapter->tp);
  675. if (adapter->espi) {
  676. adapter->slow_intr_mask |= F_PL_INTR_ESPI;
  677. t1_espi_intr_enable(adapter->espi);
  678. }
  679. /* Enable MAC/PHY interrupts for each port. */
  680. for_each_port(adapter, i) {
  681. adapter->port[i].mac->ops->interrupt_enable(adapter->port[i].mac);
  682. adapter->port[i].phy->ops->interrupt_enable(adapter->port[i].phy);
  683. }
  684. /* Enable PCIX & external chip interrupts on ASIC boards. */
  685. if (t1_is_asic(adapter)) {
  686. u32 pl_intr = readl(adapter->regs + A_PL_ENABLE);
  687. /* PCI-X interrupts */
  688. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_ENABLE,
  689. 0xffffffff);
  690. adapter->slow_intr_mask |= F_PL_INTR_EXT | F_PL_INTR_PCIX;
  691. pl_intr |= F_PL_INTR_EXT | F_PL_INTR_PCIX;
  692. writel(pl_intr, adapter->regs + A_PL_ENABLE);
  693. }
  694. }
  695. /* Disables all interrupts. */
  696. void t1_interrupts_disable(adapter_t* adapter)
  697. {
  698. unsigned int i;
  699. t1_sge_intr_disable(adapter->sge);
  700. t1_tp_intr_disable(adapter->tp);
  701. if (adapter->espi)
  702. t1_espi_intr_disable(adapter->espi);
  703. /* Disable MAC/PHY interrupts for each port. */
  704. for_each_port(adapter, i) {
  705. adapter->port[i].mac->ops->interrupt_disable(adapter->port[i].mac);
  706. adapter->port[i].phy->ops->interrupt_disable(adapter->port[i].phy);
  707. }
  708. /* Disable PCIX & external chip interrupts. */
  709. if (t1_is_asic(adapter))
  710. writel(0, adapter->regs + A_PL_ENABLE);
  711. /* PCI-X interrupts */
  712. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_ENABLE, 0);
  713. adapter->slow_intr_mask = 0;
  714. }
  715. /* Clears all interrupts */
  716. void t1_interrupts_clear(adapter_t* adapter)
  717. {
  718. unsigned int i;
  719. t1_sge_intr_clear(adapter->sge);
  720. t1_tp_intr_clear(adapter->tp);
  721. if (adapter->espi)
  722. t1_espi_intr_clear(adapter->espi);
  723. /* Clear MAC/PHY interrupts for each port. */
  724. for_each_port(adapter, i) {
  725. adapter->port[i].mac->ops->interrupt_clear(adapter->port[i].mac);
  726. adapter->port[i].phy->ops->interrupt_clear(adapter->port[i].phy);
  727. }
  728. /* Enable interrupts for external devices. */
  729. if (t1_is_asic(adapter)) {
  730. u32 pl_intr = readl(adapter->regs + A_PL_CAUSE);
  731. writel(pl_intr | F_PL_INTR_EXT | F_PL_INTR_PCIX,
  732. adapter->regs + A_PL_CAUSE);
  733. }
  734. /* PCI-X interrupts */
  735. pci_write_config_dword(adapter->pdev, A_PCICFG_INTR_CAUSE, 0xffffffff);
  736. }
  737. /*
  738. * Slow path interrupt handler for ASICs.
  739. */
  740. static int asic_slow_intr(adapter_t *adapter)
  741. {
  742. u32 cause = readl(adapter->regs + A_PL_CAUSE);
  743. cause &= adapter->slow_intr_mask;
  744. if (!cause)
  745. return 0;
  746. if (cause & F_PL_INTR_SGE_ERR)
  747. t1_sge_intr_error_handler(adapter->sge);
  748. if (cause & F_PL_INTR_TP)
  749. t1_tp_intr_handler(adapter->tp);
  750. if (cause & F_PL_INTR_ESPI)
  751. t1_espi_intr_handler(adapter->espi);
  752. if (cause & F_PL_INTR_PCIX)
  753. t1_pci_intr_handler(adapter);
  754. if (cause & F_PL_INTR_EXT)
  755. t1_elmer0_ext_intr(adapter);
  756. /* Clear the interrupts just processed. */
  757. writel(cause, adapter->regs + A_PL_CAUSE);
  758. readl(adapter->regs + A_PL_CAUSE); /* flush writes */
  759. return 1;
  760. }
  761. int t1_slow_intr_handler(adapter_t *adapter)
  762. {
  763. #ifdef CONFIG_CHELSIO_T1_1G
  764. if (!t1_is_asic(adapter))
  765. return fpga_slow_intr(adapter);
  766. #endif
  767. return asic_slow_intr(adapter);
  768. }
  769. /* Power sequencing is a work-around for Intel's XPAKs. */
  770. static void power_sequence_xpak(adapter_t* adapter)
  771. {
  772. u32 mod_detect;
  773. u32 gpo;
  774. /* Check for XPAK */
  775. t1_tpi_read(adapter, A_ELMER0_GPI_STAT, &mod_detect);
  776. if (!(ELMER0_GP_BIT5 & mod_detect)) {
  777. /* XPAK is present */
  778. t1_tpi_read(adapter, A_ELMER0_GPO, &gpo);
  779. gpo |= ELMER0_GP_BIT18;
  780. t1_tpi_write(adapter, A_ELMER0_GPO, gpo);
  781. }
  782. }
  783. int __devinit t1_get_board_rev(adapter_t *adapter, const struct board_info *bi,
  784. struct adapter_params *p)
  785. {
  786. p->chip_version = bi->chip_term;
  787. p->is_asic = (p->chip_version != CHBT_TERM_FPGA);
  788. if (p->chip_version == CHBT_TERM_T1 ||
  789. p->chip_version == CHBT_TERM_T2 ||
  790. p->chip_version == CHBT_TERM_FPGA) {
  791. u32 val = readl(adapter->regs + A_TP_PC_CONFIG);
  792. val = G_TP_PC_REV(val);
  793. if (val == 2)
  794. p->chip_revision = TERM_T1B;
  795. else if (val == 3)
  796. p->chip_revision = TERM_T2;
  797. else
  798. return -1;
  799. } else
  800. return -1;
  801. return 0;
  802. }
  803. /*
  804. * Enable board components other than the Chelsio chip, such as external MAC
  805. * and PHY.
  806. */
  807. static int board_init(adapter_t *adapter, const struct board_info *bi)
  808. {
  809. switch (bi->board) {
  810. case CHBT_BOARD_8000:
  811. case CHBT_BOARD_N110:
  812. case CHBT_BOARD_N210:
  813. case CHBT_BOARD_CHT210:
  814. t1_tpi_par(adapter, 0xf);
  815. t1_tpi_write(adapter, A_ELMER0_GPO, 0x800);
  816. break;
  817. case CHBT_BOARD_CHT110:
  818. t1_tpi_par(adapter, 0xf);
  819. t1_tpi_write(adapter, A_ELMER0_GPO, 0x1800);
  820. /* TBD XXX Might not need. This fixes a problem
  821. * described in the Intel SR XPAK errata.
  822. */
  823. power_sequence_xpak(adapter);
  824. break;
  825. #ifdef CONFIG_CHELSIO_T1_1G
  826. case CHBT_BOARD_CHT204E:
  827. /* add config space write here */
  828. case CHBT_BOARD_CHT204:
  829. case CHBT_BOARD_CHT204V:
  830. case CHBT_BOARD_CHN204:
  831. t1_tpi_par(adapter, 0xf);
  832. t1_tpi_write(adapter, A_ELMER0_GPO, 0x804);
  833. break;
  834. case CHBT_BOARD_CHT101:
  835. case CHBT_BOARD_7500:
  836. t1_tpi_par(adapter, 0xf);
  837. t1_tpi_write(adapter, A_ELMER0_GPO, 0x1804);
  838. break;
  839. #endif
  840. }
  841. return 0;
  842. }
  843. /*
  844. * Initialize and configure the Terminator HW modules. Note that external
  845. * MAC and PHYs are initialized separately.
  846. */
  847. int t1_init_hw_modules(adapter_t *adapter)
  848. {
  849. int err = -EIO;
  850. const struct board_info *bi = board_info(adapter);
  851. if (!bi->clock_mc4) {
  852. u32 val = readl(adapter->regs + A_MC4_CFG);
  853. writel(val | F_READY | F_MC4_SLOW, adapter->regs + A_MC4_CFG);
  854. writel(F_M_BUS_ENABLE | F_TCAM_RESET,
  855. adapter->regs + A_MC5_CONFIG);
  856. }
  857. if (adapter->espi && t1_espi_init(adapter->espi, bi->chip_mac,
  858. bi->espi_nports))
  859. goto out_err;
  860. if (t1_tp_reset(adapter->tp, &adapter->params.tp, bi->clock_core))
  861. goto out_err;
  862. err = t1_sge_configure(adapter->sge, &adapter->params.sge);
  863. if (err)
  864. goto out_err;
  865. err = 0;
  866. out_err:
  867. return err;
  868. }
  869. /*
  870. * Determine a card's PCI mode.
  871. */
  872. static void __devinit get_pci_mode(adapter_t *adapter, struct chelsio_pci_params *p)
  873. {
  874. static const unsigned short speed_map[] = { 33, 66, 100, 133 };
  875. u32 pci_mode;
  876. pci_read_config_dword(adapter->pdev, A_PCICFG_MODE, &pci_mode);
  877. p->speed = speed_map[G_PCI_MODE_CLK(pci_mode)];
  878. p->width = (pci_mode & F_PCI_MODE_64BIT) ? 64 : 32;
  879. p->is_pcix = (pci_mode & F_PCI_MODE_PCIX) != 0;
  880. }
  881. /*
  882. * Release the structures holding the SW per-Terminator-HW-module state.
  883. */
  884. void t1_free_sw_modules(adapter_t *adapter)
  885. {
  886. unsigned int i;
  887. for_each_port(adapter, i) {
  888. struct cmac *mac = adapter->port[i].mac;
  889. struct cphy *phy = adapter->port[i].phy;
  890. if (mac)
  891. mac->ops->destroy(mac);
  892. if (phy)
  893. phy->ops->destroy(phy);
  894. }
  895. if (adapter->sge)
  896. t1_sge_destroy(adapter->sge);
  897. if (adapter->tp)
  898. t1_tp_destroy(adapter->tp);
  899. if (adapter->espi)
  900. t1_espi_destroy(adapter->espi);
  901. }
  902. static void __devinit init_link_config(struct link_config *lc,
  903. const struct board_info *bi)
  904. {
  905. lc->supported = bi->caps;
  906. lc->requested_speed = lc->speed = SPEED_INVALID;
  907. lc->requested_duplex = lc->duplex = DUPLEX_INVALID;
  908. lc->requested_fc = lc->fc = PAUSE_RX | PAUSE_TX;
  909. if (lc->supported & SUPPORTED_Autoneg) {
  910. lc->advertising = lc->supported;
  911. lc->autoneg = AUTONEG_ENABLE;
  912. lc->requested_fc |= PAUSE_AUTONEG;
  913. } else {
  914. lc->advertising = 0;
  915. lc->autoneg = AUTONEG_DISABLE;
  916. }
  917. }
  918. /*
  919. * Allocate and initialize the data structures that hold the SW state of
  920. * the Terminator HW modules.
  921. */
  922. int __devinit t1_init_sw_modules(adapter_t *adapter,
  923. const struct board_info *bi)
  924. {
  925. unsigned int i;
  926. adapter->params.brd_info = bi;
  927. adapter->params.nports = bi->port_number;
  928. adapter->params.stats_update_period = bi->gmac->stats_update_period;
  929. adapter->sge = t1_sge_create(adapter, &adapter->params.sge);
  930. if (!adapter->sge) {
  931. pr_err("%s: SGE initialization failed\n",
  932. adapter->name);
  933. goto error;
  934. }
  935. if (bi->espi_nports && !(adapter->espi = t1_espi_create(adapter))) {
  936. pr_err("%s: ESPI initialization failed\n",
  937. adapter->name);
  938. goto error;
  939. }
  940. adapter->tp = t1_tp_create(adapter, &adapter->params.tp);
  941. if (!adapter->tp) {
  942. pr_err("%s: TP initialization failed\n",
  943. adapter->name);
  944. goto error;
  945. }
  946. board_init(adapter, bi);
  947. bi->mdio_ops->init(adapter, bi);
  948. if (bi->gphy->reset)
  949. bi->gphy->reset(adapter);
  950. if (bi->gmac->reset)
  951. bi->gmac->reset(adapter);
  952. for_each_port(adapter, i) {
  953. u8 hw_addr[6];
  954. struct cmac *mac;
  955. int phy_addr = bi->mdio_phybaseaddr + i;
  956. adapter->port[i].phy = bi->gphy->create(adapter->port[i].dev,
  957. phy_addr, bi->mdio_ops);
  958. if (!adapter->port[i].phy) {
  959. pr_err("%s: PHY %d initialization failed\n",
  960. adapter->name, i);
  961. goto error;
  962. }
  963. adapter->port[i].mac = mac = bi->gmac->create(adapter, i);
  964. if (!mac) {
  965. pr_err("%s: MAC %d initialization failed\n",
  966. adapter->name, i);
  967. goto error;
  968. }
  969. /*
  970. * Get the port's MAC addresses either from the EEPROM if one
  971. * exists or the one hardcoded in the MAC.
  972. */
  973. if (!t1_is_asic(adapter) || bi->chip_mac == CHBT_MAC_DUMMY)
  974. mac->ops->macaddress_get(mac, hw_addr);
  975. else if (vpd_macaddress_get(adapter, i, hw_addr)) {
  976. pr_err("%s: could not read MAC address from VPD ROM\n",
  977. adapter->port[i].dev->name);
  978. goto error;
  979. }
  980. memcpy(adapter->port[i].dev->dev_addr, hw_addr, ETH_ALEN);
  981. init_link_config(&adapter->port[i].link_config, bi);
  982. }
  983. get_pci_mode(adapter, &adapter->params.pci);
  984. t1_interrupts_clear(adapter);
  985. return 0;
  986. error:
  987. t1_free_sw_modules(adapter);
  988. return -1;
  989. }