ixgbe_phy.c 40 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566
  1. /*******************************************************************************
  2. Intel 10 Gigabit PCI Express Linux driver
  3. Copyright(c) 1999 - 2010 Intel Corporation.
  4. This program is free software; you can redistribute it and/or modify it
  5. under the terms and conditions of the GNU General Public License,
  6. version 2, as published by the Free Software Foundation.
  7. This program is distributed in the hope it will be useful, but WITHOUT
  8. ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  9. FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  10. more details.
  11. You should have received a copy of the GNU General Public License along with
  12. this program; if not, write to the Free Software Foundation, Inc.,
  13. 51 Franklin St - Fifth Floor, Boston, MA 02110-1301 USA.
  14. The full GNU General Public License is included in this distribution in
  15. the file called "COPYING".
  16. Contact Information:
  17. e1000-devel Mailing List <e1000-devel@lists.sourceforge.net>
  18. Intel Corporation, 5200 N.E. Elam Young Parkway, Hillsboro, OR 97124-6497
  19. *******************************************************************************/
  20. #include <linux/pci.h>
  21. #include <linux/delay.h>
  22. #include <linux/sched.h>
  23. #include "ixgbe_common.h"
  24. #include "ixgbe_phy.h"
  25. static void ixgbe_i2c_start(struct ixgbe_hw *hw);
  26. static void ixgbe_i2c_stop(struct ixgbe_hw *hw);
  27. static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data);
  28. static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data);
  29. static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw);
  30. static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data);
  31. static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data);
  32. static s32 ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
  33. static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl);
  34. static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data);
  35. static bool ixgbe_get_i2c_data(u32 *i2cctl);
  36. static void ixgbe_i2c_bus_clear(struct ixgbe_hw *hw);
  37. static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id);
  38. static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw);
  39. /**
  40. * ixgbe_identify_phy_generic - Get physical layer module
  41. * @hw: pointer to hardware structure
  42. *
  43. * Determines the physical layer module found on the current adapter.
  44. **/
  45. s32 ixgbe_identify_phy_generic(struct ixgbe_hw *hw)
  46. {
  47. s32 status = IXGBE_ERR_PHY_ADDR_INVALID;
  48. u32 phy_addr;
  49. if (hw->phy.type == ixgbe_phy_unknown) {
  50. for (phy_addr = 0; phy_addr < IXGBE_MAX_PHY_ADDR; phy_addr++) {
  51. hw->phy.mdio.prtad = phy_addr;
  52. if (mdio45_probe(&hw->phy.mdio, phy_addr) == 0) {
  53. ixgbe_get_phy_id(hw);
  54. hw->phy.type =
  55. ixgbe_get_phy_type_from_id(hw->phy.id);
  56. status = 0;
  57. break;
  58. }
  59. }
  60. /* clear value if nothing found */
  61. hw->phy.mdio.prtad = 0;
  62. } else {
  63. status = 0;
  64. }
  65. return status;
  66. }
  67. /**
  68. * ixgbe_get_phy_id - Get the phy type
  69. * @hw: pointer to hardware structure
  70. *
  71. **/
  72. static s32 ixgbe_get_phy_id(struct ixgbe_hw *hw)
  73. {
  74. u32 status;
  75. u16 phy_id_high = 0;
  76. u16 phy_id_low = 0;
  77. status = hw->phy.ops.read_reg(hw, MDIO_DEVID1, MDIO_MMD_PMAPMD,
  78. &phy_id_high);
  79. if (status == 0) {
  80. hw->phy.id = (u32)(phy_id_high << 16);
  81. status = hw->phy.ops.read_reg(hw, MDIO_DEVID2, MDIO_MMD_PMAPMD,
  82. &phy_id_low);
  83. hw->phy.id |= (u32)(phy_id_low & IXGBE_PHY_REVISION_MASK);
  84. hw->phy.revision = (u32)(phy_id_low & ~IXGBE_PHY_REVISION_MASK);
  85. }
  86. return status;
  87. }
  88. /**
  89. * ixgbe_get_phy_type_from_id - Get the phy type
  90. * @hw: pointer to hardware structure
  91. *
  92. **/
  93. static enum ixgbe_phy_type ixgbe_get_phy_type_from_id(u32 phy_id)
  94. {
  95. enum ixgbe_phy_type phy_type;
  96. switch (phy_id) {
  97. case TN1010_PHY_ID:
  98. phy_type = ixgbe_phy_tn;
  99. break;
  100. case X540_PHY_ID:
  101. phy_type = ixgbe_phy_aq;
  102. break;
  103. case QT2022_PHY_ID:
  104. phy_type = ixgbe_phy_qt;
  105. break;
  106. case ATH_PHY_ID:
  107. phy_type = ixgbe_phy_nl;
  108. break;
  109. default:
  110. phy_type = ixgbe_phy_unknown;
  111. break;
  112. }
  113. return phy_type;
  114. }
  115. /**
  116. * ixgbe_reset_phy_generic - Performs a PHY reset
  117. * @hw: pointer to hardware structure
  118. **/
  119. s32 ixgbe_reset_phy_generic(struct ixgbe_hw *hw)
  120. {
  121. u32 i;
  122. u16 ctrl = 0;
  123. s32 status = 0;
  124. if (hw->phy.type == ixgbe_phy_unknown)
  125. status = ixgbe_identify_phy_generic(hw);
  126. if (status != 0 || hw->phy.type == ixgbe_phy_none)
  127. goto out;
  128. /* Don't reset PHY if it's shut down due to overtemp. */
  129. if (!hw->phy.reset_if_overtemp &&
  130. (IXGBE_ERR_OVERTEMP == hw->phy.ops.check_overtemp(hw)))
  131. goto out;
  132. /*
  133. * Perform soft PHY reset to the PHY_XS.
  134. * This will cause a soft reset to the PHY
  135. */
  136. hw->phy.ops.write_reg(hw, MDIO_CTRL1,
  137. MDIO_MMD_PHYXS,
  138. MDIO_CTRL1_RESET);
  139. /*
  140. * Poll for reset bit to self-clear indicating reset is complete.
  141. * Some PHYs could take up to 3 seconds to complete and need about
  142. * 1.7 usec delay after the reset is complete.
  143. */
  144. for (i = 0; i < 30; i++) {
  145. msleep(100);
  146. hw->phy.ops.read_reg(hw, MDIO_CTRL1,
  147. MDIO_MMD_PHYXS, &ctrl);
  148. if (!(ctrl & MDIO_CTRL1_RESET)) {
  149. udelay(2);
  150. break;
  151. }
  152. }
  153. if (ctrl & MDIO_CTRL1_RESET) {
  154. status = IXGBE_ERR_RESET_FAILED;
  155. hw_dbg(hw, "PHY reset polling failed to complete.\n");
  156. }
  157. out:
  158. return status;
  159. }
  160. /**
  161. * ixgbe_read_phy_reg_generic - Reads a value from a specified PHY register
  162. * @hw: pointer to hardware structure
  163. * @reg_addr: 32 bit address of PHY register to read
  164. * @phy_data: Pointer to read data from PHY register
  165. **/
  166. s32 ixgbe_read_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
  167. u32 device_type, u16 *phy_data)
  168. {
  169. u32 command;
  170. u32 i;
  171. u32 data;
  172. s32 status = 0;
  173. u16 gssr;
  174. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  175. gssr = IXGBE_GSSR_PHY1_SM;
  176. else
  177. gssr = IXGBE_GSSR_PHY0_SM;
  178. if (ixgbe_acquire_swfw_sync(hw, gssr) != 0)
  179. status = IXGBE_ERR_SWFW_SYNC;
  180. if (status == 0) {
  181. /* Setup and write the address cycle command */
  182. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  183. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  184. (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  185. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  186. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  187. /*
  188. * Check every 10 usec to see if the address cycle completed.
  189. * The MDI Command bit will clear when the operation is
  190. * complete
  191. */
  192. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  193. udelay(10);
  194. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  195. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  196. break;
  197. }
  198. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  199. hw_dbg(hw, "PHY address command did not complete.\n");
  200. status = IXGBE_ERR_PHY;
  201. }
  202. if (status == 0) {
  203. /*
  204. * Address cycle complete, setup and write the read
  205. * command
  206. */
  207. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  208. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  209. (hw->phy.mdio.prtad <<
  210. IXGBE_MSCA_PHY_ADDR_SHIFT) |
  211. (IXGBE_MSCA_READ | IXGBE_MSCA_MDI_COMMAND));
  212. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  213. /*
  214. * Check every 10 usec to see if the address cycle
  215. * completed. The MDI Command bit will clear when the
  216. * operation is complete
  217. */
  218. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  219. udelay(10);
  220. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  221. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  222. break;
  223. }
  224. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  225. hw_dbg(hw, "PHY read command didn't complete\n");
  226. status = IXGBE_ERR_PHY;
  227. } else {
  228. /*
  229. * Read operation is complete. Get the data
  230. * from MSRWD
  231. */
  232. data = IXGBE_READ_REG(hw, IXGBE_MSRWD);
  233. data >>= IXGBE_MSRWD_READ_DATA_SHIFT;
  234. *phy_data = (u16)(data);
  235. }
  236. }
  237. ixgbe_release_swfw_sync(hw, gssr);
  238. }
  239. return status;
  240. }
  241. /**
  242. * ixgbe_write_phy_reg_generic - Writes a value to specified PHY register
  243. * @hw: pointer to hardware structure
  244. * @reg_addr: 32 bit PHY register to write
  245. * @device_type: 5 bit device type
  246. * @phy_data: Data to write to the PHY register
  247. **/
  248. s32 ixgbe_write_phy_reg_generic(struct ixgbe_hw *hw, u32 reg_addr,
  249. u32 device_type, u16 phy_data)
  250. {
  251. u32 command;
  252. u32 i;
  253. s32 status = 0;
  254. u16 gssr;
  255. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  256. gssr = IXGBE_GSSR_PHY1_SM;
  257. else
  258. gssr = IXGBE_GSSR_PHY0_SM;
  259. if (ixgbe_acquire_swfw_sync(hw, gssr) != 0)
  260. status = IXGBE_ERR_SWFW_SYNC;
  261. if (status == 0) {
  262. /* Put the data in the MDI single read and write data register*/
  263. IXGBE_WRITE_REG(hw, IXGBE_MSRWD, (u32)phy_data);
  264. /* Setup and write the address cycle command */
  265. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  266. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  267. (hw->phy.mdio.prtad << IXGBE_MSCA_PHY_ADDR_SHIFT) |
  268. (IXGBE_MSCA_ADDR_CYCLE | IXGBE_MSCA_MDI_COMMAND));
  269. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  270. /*
  271. * Check every 10 usec to see if the address cycle completed.
  272. * The MDI Command bit will clear when the operation is
  273. * complete
  274. */
  275. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  276. udelay(10);
  277. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  278. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  279. break;
  280. }
  281. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  282. hw_dbg(hw, "PHY address cmd didn't complete\n");
  283. status = IXGBE_ERR_PHY;
  284. }
  285. if (status == 0) {
  286. /*
  287. * Address cycle complete, setup and write the write
  288. * command
  289. */
  290. command = ((reg_addr << IXGBE_MSCA_NP_ADDR_SHIFT) |
  291. (device_type << IXGBE_MSCA_DEV_TYPE_SHIFT) |
  292. (hw->phy.mdio.prtad <<
  293. IXGBE_MSCA_PHY_ADDR_SHIFT) |
  294. (IXGBE_MSCA_WRITE | IXGBE_MSCA_MDI_COMMAND));
  295. IXGBE_WRITE_REG(hw, IXGBE_MSCA, command);
  296. /*
  297. * Check every 10 usec to see if the address cycle
  298. * completed. The MDI Command bit will clear when the
  299. * operation is complete
  300. */
  301. for (i = 0; i < IXGBE_MDIO_COMMAND_TIMEOUT; i++) {
  302. udelay(10);
  303. command = IXGBE_READ_REG(hw, IXGBE_MSCA);
  304. if ((command & IXGBE_MSCA_MDI_COMMAND) == 0)
  305. break;
  306. }
  307. if ((command & IXGBE_MSCA_MDI_COMMAND) != 0) {
  308. hw_dbg(hw, "PHY address cmd didn't complete\n");
  309. status = IXGBE_ERR_PHY;
  310. }
  311. }
  312. ixgbe_release_swfw_sync(hw, gssr);
  313. }
  314. return status;
  315. }
  316. /**
  317. * ixgbe_setup_phy_link_generic - Set and restart autoneg
  318. * @hw: pointer to hardware structure
  319. *
  320. * Restart autonegotiation and PHY and waits for completion.
  321. **/
  322. s32 ixgbe_setup_phy_link_generic(struct ixgbe_hw *hw)
  323. {
  324. s32 status = IXGBE_NOT_IMPLEMENTED;
  325. u32 time_out;
  326. u32 max_time_out = 10;
  327. u16 autoneg_reg;
  328. /*
  329. * Set advertisement settings in PHY based on autoneg_advertised
  330. * settings. If autoneg_advertised = 0, then advertise default values
  331. * tnx devices cannot be "forced" to a autoneg 10G and fail. But can
  332. * for a 1G.
  333. */
  334. hw->phy.ops.read_reg(hw, MDIO_AN_ADVERTISE, MDIO_MMD_AN, &autoneg_reg);
  335. if (hw->phy.autoneg_advertised == IXGBE_LINK_SPEED_1GB_FULL)
  336. autoneg_reg &= ~MDIO_AN_10GBT_CTRL_ADV10G;
  337. else
  338. autoneg_reg |= MDIO_AN_10GBT_CTRL_ADV10G;
  339. hw->phy.ops.write_reg(hw, MDIO_AN_ADVERTISE, MDIO_MMD_AN, autoneg_reg);
  340. /* Restart PHY autonegotiation and wait for completion */
  341. hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_AN, &autoneg_reg);
  342. autoneg_reg |= MDIO_AN_CTRL1_RESTART;
  343. hw->phy.ops.write_reg(hw, MDIO_CTRL1, MDIO_MMD_AN, autoneg_reg);
  344. /* Wait for autonegotiation to finish */
  345. for (time_out = 0; time_out < max_time_out; time_out++) {
  346. udelay(10);
  347. /* Restart PHY autonegotiation and wait for completion */
  348. status = hw->phy.ops.read_reg(hw, MDIO_STAT1, MDIO_MMD_AN,
  349. &autoneg_reg);
  350. autoneg_reg &= MDIO_AN_STAT1_COMPLETE;
  351. if (autoneg_reg == MDIO_AN_STAT1_COMPLETE) {
  352. status = 0;
  353. break;
  354. }
  355. }
  356. if (time_out == max_time_out)
  357. status = IXGBE_ERR_LINK_SETUP;
  358. return status;
  359. }
  360. /**
  361. * ixgbe_setup_phy_link_speed_generic - Sets the auto advertised capabilities
  362. * @hw: pointer to hardware structure
  363. * @speed: new link speed
  364. * @autoneg: true if autonegotiation enabled
  365. **/
  366. s32 ixgbe_setup_phy_link_speed_generic(struct ixgbe_hw *hw,
  367. ixgbe_link_speed speed,
  368. bool autoneg,
  369. bool autoneg_wait_to_complete)
  370. {
  371. /*
  372. * Clear autoneg_advertised and set new values based on input link
  373. * speed.
  374. */
  375. hw->phy.autoneg_advertised = 0;
  376. if (speed & IXGBE_LINK_SPEED_10GB_FULL)
  377. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_10GB_FULL;
  378. if (speed & IXGBE_LINK_SPEED_1GB_FULL)
  379. hw->phy.autoneg_advertised |= IXGBE_LINK_SPEED_1GB_FULL;
  380. /* Setup link based on the new speed settings */
  381. hw->phy.ops.setup_link(hw);
  382. return 0;
  383. }
  384. /**
  385. * ixgbe_get_copper_link_capabilities_generic - Determines link capabilities
  386. * @hw: pointer to hardware structure
  387. * @speed: pointer to link speed
  388. * @autoneg: boolean auto-negotiation value
  389. *
  390. * Determines the link capabilities by reading the AUTOC register.
  391. */
  392. s32 ixgbe_get_copper_link_capabilities_generic(struct ixgbe_hw *hw,
  393. ixgbe_link_speed *speed,
  394. bool *autoneg)
  395. {
  396. s32 status = IXGBE_ERR_LINK_SETUP;
  397. u16 speed_ability;
  398. *speed = 0;
  399. *autoneg = true;
  400. status = hw->phy.ops.read_reg(hw, MDIO_SPEED, MDIO_MMD_PMAPMD,
  401. &speed_ability);
  402. if (status == 0) {
  403. if (speed_ability & MDIO_SPEED_10G)
  404. *speed |= IXGBE_LINK_SPEED_10GB_FULL;
  405. if (speed_ability & MDIO_PMA_SPEED_1000)
  406. *speed |= IXGBE_LINK_SPEED_1GB_FULL;
  407. if (speed_ability & MDIO_PMA_SPEED_100)
  408. *speed |= IXGBE_LINK_SPEED_100_FULL;
  409. }
  410. return status;
  411. }
  412. /**
  413. * ixgbe_reset_phy_nl - Performs a PHY reset
  414. * @hw: pointer to hardware structure
  415. **/
  416. s32 ixgbe_reset_phy_nl(struct ixgbe_hw *hw)
  417. {
  418. u16 phy_offset, control, eword, edata, block_crc;
  419. bool end_data = false;
  420. u16 list_offset, data_offset;
  421. u16 phy_data = 0;
  422. s32 ret_val = 0;
  423. u32 i;
  424. hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS, &phy_data);
  425. /* reset the PHY and poll for completion */
  426. hw->phy.ops.write_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS,
  427. (phy_data | MDIO_CTRL1_RESET));
  428. for (i = 0; i < 100; i++) {
  429. hw->phy.ops.read_reg(hw, MDIO_CTRL1, MDIO_MMD_PHYXS,
  430. &phy_data);
  431. if ((phy_data & MDIO_CTRL1_RESET) == 0)
  432. break;
  433. msleep(10);
  434. }
  435. if ((phy_data & MDIO_CTRL1_RESET) != 0) {
  436. hw_dbg(hw, "PHY reset did not complete.\n");
  437. ret_val = IXGBE_ERR_PHY;
  438. goto out;
  439. }
  440. /* Get init offsets */
  441. ret_val = ixgbe_get_sfp_init_sequence_offsets(hw, &list_offset,
  442. &data_offset);
  443. if (ret_val != 0)
  444. goto out;
  445. ret_val = hw->eeprom.ops.read(hw, data_offset, &block_crc);
  446. data_offset++;
  447. while (!end_data) {
  448. /*
  449. * Read control word from PHY init contents offset
  450. */
  451. ret_val = hw->eeprom.ops.read(hw, data_offset, &eword);
  452. control = (eword & IXGBE_CONTROL_MASK_NL) >>
  453. IXGBE_CONTROL_SHIFT_NL;
  454. edata = eword & IXGBE_DATA_MASK_NL;
  455. switch (control) {
  456. case IXGBE_DELAY_NL:
  457. data_offset++;
  458. hw_dbg(hw, "DELAY: %d MS\n", edata);
  459. msleep(edata);
  460. break;
  461. case IXGBE_DATA_NL:
  462. hw_dbg(hw, "DATA:\n");
  463. data_offset++;
  464. hw->eeprom.ops.read(hw, data_offset++,
  465. &phy_offset);
  466. for (i = 0; i < edata; i++) {
  467. hw->eeprom.ops.read(hw, data_offset, &eword);
  468. hw->phy.ops.write_reg(hw, phy_offset,
  469. MDIO_MMD_PMAPMD, eword);
  470. hw_dbg(hw, "Wrote %4.4x to %4.4x\n", eword,
  471. phy_offset);
  472. data_offset++;
  473. phy_offset++;
  474. }
  475. break;
  476. case IXGBE_CONTROL_NL:
  477. data_offset++;
  478. hw_dbg(hw, "CONTROL:\n");
  479. if (edata == IXGBE_CONTROL_EOL_NL) {
  480. hw_dbg(hw, "EOL\n");
  481. end_data = true;
  482. } else if (edata == IXGBE_CONTROL_SOL_NL) {
  483. hw_dbg(hw, "SOL\n");
  484. } else {
  485. hw_dbg(hw, "Bad control value\n");
  486. ret_val = IXGBE_ERR_PHY;
  487. goto out;
  488. }
  489. break;
  490. default:
  491. hw_dbg(hw, "Bad control type\n");
  492. ret_val = IXGBE_ERR_PHY;
  493. goto out;
  494. }
  495. }
  496. out:
  497. return ret_val;
  498. }
  499. /**
  500. * ixgbe_identify_sfp_module_generic - Identifies SFP modules
  501. * @hw: pointer to hardware structure
  502. *
  503. * Searches for and identifies the SFP module and assigns appropriate PHY type.
  504. **/
  505. s32 ixgbe_identify_sfp_module_generic(struct ixgbe_hw *hw)
  506. {
  507. s32 status = IXGBE_ERR_PHY_ADDR_INVALID;
  508. u32 vendor_oui = 0;
  509. enum ixgbe_sfp_type stored_sfp_type = hw->phy.sfp_type;
  510. u8 identifier = 0;
  511. u8 comp_codes_1g = 0;
  512. u8 comp_codes_10g = 0;
  513. u8 oui_bytes[3] = {0, 0, 0};
  514. u8 cable_tech = 0;
  515. u8 cable_spec = 0;
  516. u16 enforce_sfp = 0;
  517. if (hw->mac.ops.get_media_type(hw) != ixgbe_media_type_fiber) {
  518. hw->phy.sfp_type = ixgbe_sfp_type_not_present;
  519. status = IXGBE_ERR_SFP_NOT_PRESENT;
  520. goto out;
  521. }
  522. status = hw->phy.ops.read_i2c_eeprom(hw,
  523. IXGBE_SFF_IDENTIFIER,
  524. &identifier);
  525. if (status == IXGBE_ERR_SWFW_SYNC ||
  526. status == IXGBE_ERR_I2C ||
  527. status == IXGBE_ERR_SFP_NOT_PRESENT)
  528. goto err_read_i2c_eeprom;
  529. /* LAN ID is needed for sfp_type determination */
  530. hw->mac.ops.set_lan_id(hw);
  531. if (identifier != IXGBE_SFF_IDENTIFIER_SFP) {
  532. hw->phy.type = ixgbe_phy_sfp_unsupported;
  533. status = IXGBE_ERR_SFP_NOT_SUPPORTED;
  534. } else {
  535. status = hw->phy.ops.read_i2c_eeprom(hw,
  536. IXGBE_SFF_1GBE_COMP_CODES,
  537. &comp_codes_1g);
  538. if (status == IXGBE_ERR_SWFW_SYNC ||
  539. status == IXGBE_ERR_I2C ||
  540. status == IXGBE_ERR_SFP_NOT_PRESENT)
  541. goto err_read_i2c_eeprom;
  542. status = hw->phy.ops.read_i2c_eeprom(hw,
  543. IXGBE_SFF_10GBE_COMP_CODES,
  544. &comp_codes_10g);
  545. if (status == IXGBE_ERR_SWFW_SYNC ||
  546. status == IXGBE_ERR_I2C ||
  547. status == IXGBE_ERR_SFP_NOT_PRESENT)
  548. goto err_read_i2c_eeprom;
  549. status = hw->phy.ops.read_i2c_eeprom(hw,
  550. IXGBE_SFF_CABLE_TECHNOLOGY,
  551. &cable_tech);
  552. if (status == IXGBE_ERR_SWFW_SYNC ||
  553. status == IXGBE_ERR_I2C ||
  554. status == IXGBE_ERR_SFP_NOT_PRESENT)
  555. goto err_read_i2c_eeprom;
  556. /* ID Module
  557. * =========
  558. * 0 SFP_DA_CU
  559. * 1 SFP_SR
  560. * 2 SFP_LR
  561. * 3 SFP_DA_CORE0 - 82599-specific
  562. * 4 SFP_DA_CORE1 - 82599-specific
  563. * 5 SFP_SR/LR_CORE0 - 82599-specific
  564. * 6 SFP_SR/LR_CORE1 - 82599-specific
  565. * 7 SFP_act_lmt_DA_CORE0 - 82599-specific
  566. * 8 SFP_act_lmt_DA_CORE1 - 82599-specific
  567. * 9 SFP_1g_cu_CORE0 - 82599-specific
  568. * 10 SFP_1g_cu_CORE1 - 82599-specific
  569. */
  570. if (hw->mac.type == ixgbe_mac_82598EB) {
  571. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
  572. hw->phy.sfp_type = ixgbe_sfp_type_da_cu;
  573. else if (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)
  574. hw->phy.sfp_type = ixgbe_sfp_type_sr;
  575. else if (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)
  576. hw->phy.sfp_type = ixgbe_sfp_type_lr;
  577. else
  578. hw->phy.sfp_type = ixgbe_sfp_type_unknown;
  579. } else if (hw->mac.type == ixgbe_mac_82599EB) {
  580. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE) {
  581. if (hw->bus.lan_id == 0)
  582. hw->phy.sfp_type =
  583. ixgbe_sfp_type_da_cu_core0;
  584. else
  585. hw->phy.sfp_type =
  586. ixgbe_sfp_type_da_cu_core1;
  587. } else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE) {
  588. hw->phy.ops.read_i2c_eeprom(
  589. hw, IXGBE_SFF_CABLE_SPEC_COMP,
  590. &cable_spec);
  591. if (cable_spec &
  592. IXGBE_SFF_DA_SPEC_ACTIVE_LIMITING) {
  593. if (hw->bus.lan_id == 0)
  594. hw->phy.sfp_type =
  595. ixgbe_sfp_type_da_act_lmt_core0;
  596. else
  597. hw->phy.sfp_type =
  598. ixgbe_sfp_type_da_act_lmt_core1;
  599. } else {
  600. hw->phy.sfp_type =
  601. ixgbe_sfp_type_unknown;
  602. }
  603. } else if (comp_codes_10g &
  604. (IXGBE_SFF_10GBASESR_CAPABLE |
  605. IXGBE_SFF_10GBASELR_CAPABLE)) {
  606. if (hw->bus.lan_id == 0)
  607. hw->phy.sfp_type =
  608. ixgbe_sfp_type_srlr_core0;
  609. else
  610. hw->phy.sfp_type =
  611. ixgbe_sfp_type_srlr_core1;
  612. } else if (comp_codes_1g & IXGBE_SFF_1GBASET_CAPABLE) {
  613. if (hw->bus.lan_id == 0)
  614. hw->phy.sfp_type =
  615. ixgbe_sfp_type_1g_cu_core0;
  616. else
  617. hw->phy.sfp_type =
  618. ixgbe_sfp_type_1g_cu_core1;
  619. } else {
  620. hw->phy.sfp_type = ixgbe_sfp_type_unknown;
  621. }
  622. }
  623. if (hw->phy.sfp_type != stored_sfp_type)
  624. hw->phy.sfp_setup_needed = true;
  625. /* Determine if the SFP+ PHY is dual speed or not. */
  626. hw->phy.multispeed_fiber = false;
  627. if (((comp_codes_1g & IXGBE_SFF_1GBASESX_CAPABLE) &&
  628. (comp_codes_10g & IXGBE_SFF_10GBASESR_CAPABLE)) ||
  629. ((comp_codes_1g & IXGBE_SFF_1GBASELX_CAPABLE) &&
  630. (comp_codes_10g & IXGBE_SFF_10GBASELR_CAPABLE)))
  631. hw->phy.multispeed_fiber = true;
  632. /* Determine PHY vendor */
  633. if (hw->phy.type != ixgbe_phy_nl) {
  634. hw->phy.id = identifier;
  635. status = hw->phy.ops.read_i2c_eeprom(hw,
  636. IXGBE_SFF_VENDOR_OUI_BYTE0,
  637. &oui_bytes[0]);
  638. if (status == IXGBE_ERR_SWFW_SYNC ||
  639. status == IXGBE_ERR_I2C ||
  640. status == IXGBE_ERR_SFP_NOT_PRESENT)
  641. goto err_read_i2c_eeprom;
  642. status = hw->phy.ops.read_i2c_eeprom(hw,
  643. IXGBE_SFF_VENDOR_OUI_BYTE1,
  644. &oui_bytes[1]);
  645. if (status == IXGBE_ERR_SWFW_SYNC ||
  646. status == IXGBE_ERR_I2C ||
  647. status == IXGBE_ERR_SFP_NOT_PRESENT)
  648. goto err_read_i2c_eeprom;
  649. status = hw->phy.ops.read_i2c_eeprom(hw,
  650. IXGBE_SFF_VENDOR_OUI_BYTE2,
  651. &oui_bytes[2]);
  652. if (status == IXGBE_ERR_SWFW_SYNC ||
  653. status == IXGBE_ERR_I2C ||
  654. status == IXGBE_ERR_SFP_NOT_PRESENT)
  655. goto err_read_i2c_eeprom;
  656. vendor_oui =
  657. ((oui_bytes[0] << IXGBE_SFF_VENDOR_OUI_BYTE0_SHIFT) |
  658. (oui_bytes[1] << IXGBE_SFF_VENDOR_OUI_BYTE1_SHIFT) |
  659. (oui_bytes[2] << IXGBE_SFF_VENDOR_OUI_BYTE2_SHIFT));
  660. switch (vendor_oui) {
  661. case IXGBE_SFF_VENDOR_OUI_TYCO:
  662. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
  663. hw->phy.type =
  664. ixgbe_phy_sfp_passive_tyco;
  665. break;
  666. case IXGBE_SFF_VENDOR_OUI_FTL:
  667. if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
  668. hw->phy.type = ixgbe_phy_sfp_ftl_active;
  669. else
  670. hw->phy.type = ixgbe_phy_sfp_ftl;
  671. break;
  672. case IXGBE_SFF_VENDOR_OUI_AVAGO:
  673. hw->phy.type = ixgbe_phy_sfp_avago;
  674. break;
  675. case IXGBE_SFF_VENDOR_OUI_INTEL:
  676. hw->phy.type = ixgbe_phy_sfp_intel;
  677. break;
  678. default:
  679. if (cable_tech & IXGBE_SFF_DA_PASSIVE_CABLE)
  680. hw->phy.type =
  681. ixgbe_phy_sfp_passive_unknown;
  682. else if (cable_tech & IXGBE_SFF_DA_ACTIVE_CABLE)
  683. hw->phy.type =
  684. ixgbe_phy_sfp_active_unknown;
  685. else
  686. hw->phy.type = ixgbe_phy_sfp_unknown;
  687. break;
  688. }
  689. }
  690. /* Allow any DA cable vendor */
  691. if (cable_tech & (IXGBE_SFF_DA_PASSIVE_CABLE |
  692. IXGBE_SFF_DA_ACTIVE_CABLE)) {
  693. status = 0;
  694. goto out;
  695. }
  696. /* Verify supported 1G SFP modules */
  697. if (comp_codes_10g == 0 &&
  698. !(hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1 ||
  699. hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0)) {
  700. hw->phy.type = ixgbe_phy_sfp_unsupported;
  701. status = IXGBE_ERR_SFP_NOT_SUPPORTED;
  702. goto out;
  703. }
  704. /* Anything else 82598-based is supported */
  705. if (hw->mac.type == ixgbe_mac_82598EB) {
  706. status = 0;
  707. goto out;
  708. }
  709. /* This is guaranteed to be 82599, no need to check for NULL */
  710. hw->mac.ops.get_device_caps(hw, &enforce_sfp);
  711. if (!(enforce_sfp & IXGBE_DEVICE_CAPS_ALLOW_ANY_SFP) &&
  712. !((hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core0) ||
  713. (hw->phy.sfp_type == ixgbe_sfp_type_1g_cu_core1))) {
  714. /* Make sure we're a supported PHY type */
  715. if (hw->phy.type == ixgbe_phy_sfp_intel) {
  716. status = 0;
  717. } else {
  718. hw_dbg(hw, "SFP+ module not supported\n");
  719. hw->phy.type = ixgbe_phy_sfp_unsupported;
  720. status = IXGBE_ERR_SFP_NOT_SUPPORTED;
  721. }
  722. } else {
  723. status = 0;
  724. }
  725. }
  726. out:
  727. return status;
  728. err_read_i2c_eeprom:
  729. hw->phy.sfp_type = ixgbe_sfp_type_not_present;
  730. if (hw->phy.type != ixgbe_phy_nl) {
  731. hw->phy.id = 0;
  732. hw->phy.type = ixgbe_phy_unknown;
  733. }
  734. return IXGBE_ERR_SFP_NOT_PRESENT;
  735. }
  736. /**
  737. * ixgbe_get_sfp_init_sequence_offsets - Provides offset of PHY init sequence
  738. * @hw: pointer to hardware structure
  739. * @list_offset: offset to the SFP ID list
  740. * @data_offset: offset to the SFP data block
  741. *
  742. * Checks the MAC's EEPROM to see if it supports a given SFP+ module type, if
  743. * so it returns the offsets to the phy init sequence block.
  744. **/
  745. s32 ixgbe_get_sfp_init_sequence_offsets(struct ixgbe_hw *hw,
  746. u16 *list_offset,
  747. u16 *data_offset)
  748. {
  749. u16 sfp_id;
  750. u16 sfp_type = hw->phy.sfp_type;
  751. if (hw->phy.sfp_type == ixgbe_sfp_type_unknown)
  752. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  753. if (hw->phy.sfp_type == ixgbe_sfp_type_not_present)
  754. return IXGBE_ERR_SFP_NOT_PRESENT;
  755. if ((hw->device_id == IXGBE_DEV_ID_82598_SR_DUAL_PORT_EM) &&
  756. (hw->phy.sfp_type == ixgbe_sfp_type_da_cu))
  757. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  758. /*
  759. * Limiting active cables and 1G Phys must be initialized as
  760. * SR modules
  761. */
  762. if (sfp_type == ixgbe_sfp_type_da_act_lmt_core0 ||
  763. sfp_type == ixgbe_sfp_type_1g_cu_core0)
  764. sfp_type = ixgbe_sfp_type_srlr_core0;
  765. else if (sfp_type == ixgbe_sfp_type_da_act_lmt_core1 ||
  766. sfp_type == ixgbe_sfp_type_1g_cu_core1)
  767. sfp_type = ixgbe_sfp_type_srlr_core1;
  768. /* Read offset to PHY init contents */
  769. hw->eeprom.ops.read(hw, IXGBE_PHY_INIT_OFFSET_NL, list_offset);
  770. if ((!*list_offset) || (*list_offset == 0xFFFF))
  771. return IXGBE_ERR_SFP_NO_INIT_SEQ_PRESENT;
  772. /* Shift offset to first ID word */
  773. (*list_offset)++;
  774. /*
  775. * Find the matching SFP ID in the EEPROM
  776. * and program the init sequence
  777. */
  778. hw->eeprom.ops.read(hw, *list_offset, &sfp_id);
  779. while (sfp_id != IXGBE_PHY_INIT_END_NL) {
  780. if (sfp_id == sfp_type) {
  781. (*list_offset)++;
  782. hw->eeprom.ops.read(hw, *list_offset, data_offset);
  783. if ((!*data_offset) || (*data_offset == 0xFFFF)) {
  784. hw_dbg(hw, "SFP+ module not supported\n");
  785. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  786. } else {
  787. break;
  788. }
  789. } else {
  790. (*list_offset) += 2;
  791. if (hw->eeprom.ops.read(hw, *list_offset, &sfp_id))
  792. return IXGBE_ERR_PHY;
  793. }
  794. }
  795. if (sfp_id == IXGBE_PHY_INIT_END_NL) {
  796. hw_dbg(hw, "No matching SFP+ module found\n");
  797. return IXGBE_ERR_SFP_NOT_SUPPORTED;
  798. }
  799. return 0;
  800. }
  801. /**
  802. * ixgbe_read_i2c_eeprom_generic - Reads 8 bit EEPROM word over I2C interface
  803. * @hw: pointer to hardware structure
  804. * @byte_offset: EEPROM byte offset to read
  805. * @eeprom_data: value read
  806. *
  807. * Performs byte read operation to SFP module's EEPROM over I2C interface.
  808. **/
  809. s32 ixgbe_read_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
  810. u8 *eeprom_data)
  811. {
  812. return hw->phy.ops.read_i2c_byte(hw, byte_offset,
  813. IXGBE_I2C_EEPROM_DEV_ADDR,
  814. eeprom_data);
  815. }
  816. /**
  817. * ixgbe_write_i2c_eeprom_generic - Writes 8 bit EEPROM word over I2C interface
  818. * @hw: pointer to hardware structure
  819. * @byte_offset: EEPROM byte offset to write
  820. * @eeprom_data: value to write
  821. *
  822. * Performs byte write operation to SFP module's EEPROM over I2C interface.
  823. **/
  824. s32 ixgbe_write_i2c_eeprom_generic(struct ixgbe_hw *hw, u8 byte_offset,
  825. u8 eeprom_data)
  826. {
  827. return hw->phy.ops.write_i2c_byte(hw, byte_offset,
  828. IXGBE_I2C_EEPROM_DEV_ADDR,
  829. eeprom_data);
  830. }
  831. /**
  832. * ixgbe_read_i2c_byte_generic - Reads 8 bit word over I2C
  833. * @hw: pointer to hardware structure
  834. * @byte_offset: byte offset to read
  835. * @data: value read
  836. *
  837. * Performs byte read operation to SFP module's EEPROM over I2C interface at
  838. * a specified deivce address.
  839. **/
  840. s32 ixgbe_read_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
  841. u8 dev_addr, u8 *data)
  842. {
  843. s32 status = 0;
  844. u32 max_retry = 10;
  845. u32 retry = 0;
  846. u16 swfw_mask = 0;
  847. bool nack = 1;
  848. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  849. swfw_mask = IXGBE_GSSR_PHY1_SM;
  850. else
  851. swfw_mask = IXGBE_GSSR_PHY0_SM;
  852. do {
  853. if (ixgbe_acquire_swfw_sync(hw, swfw_mask) != 0) {
  854. status = IXGBE_ERR_SWFW_SYNC;
  855. goto read_byte_out;
  856. }
  857. ixgbe_i2c_start(hw);
  858. /* Device Address and write indication */
  859. status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
  860. if (status != 0)
  861. goto fail;
  862. status = ixgbe_get_i2c_ack(hw);
  863. if (status != 0)
  864. goto fail;
  865. status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
  866. if (status != 0)
  867. goto fail;
  868. status = ixgbe_get_i2c_ack(hw);
  869. if (status != 0)
  870. goto fail;
  871. ixgbe_i2c_start(hw);
  872. /* Device Address and read indication */
  873. status = ixgbe_clock_out_i2c_byte(hw, (dev_addr | 0x1));
  874. if (status != 0)
  875. goto fail;
  876. status = ixgbe_get_i2c_ack(hw);
  877. if (status != 0)
  878. goto fail;
  879. status = ixgbe_clock_in_i2c_byte(hw, data);
  880. if (status != 0)
  881. goto fail;
  882. status = ixgbe_clock_out_i2c_bit(hw, nack);
  883. if (status != 0)
  884. goto fail;
  885. ixgbe_i2c_stop(hw);
  886. break;
  887. fail:
  888. ixgbe_release_swfw_sync(hw, swfw_mask);
  889. msleep(100);
  890. ixgbe_i2c_bus_clear(hw);
  891. retry++;
  892. if (retry < max_retry)
  893. hw_dbg(hw, "I2C byte read error - Retrying.\n");
  894. else
  895. hw_dbg(hw, "I2C byte read error.\n");
  896. } while (retry < max_retry);
  897. ixgbe_release_swfw_sync(hw, swfw_mask);
  898. read_byte_out:
  899. return status;
  900. }
  901. /**
  902. * ixgbe_write_i2c_byte_generic - Writes 8 bit word over I2C
  903. * @hw: pointer to hardware structure
  904. * @byte_offset: byte offset to write
  905. * @data: value to write
  906. *
  907. * Performs byte write operation to SFP module's EEPROM over I2C interface at
  908. * a specified device address.
  909. **/
  910. s32 ixgbe_write_i2c_byte_generic(struct ixgbe_hw *hw, u8 byte_offset,
  911. u8 dev_addr, u8 data)
  912. {
  913. s32 status = 0;
  914. u32 max_retry = 1;
  915. u32 retry = 0;
  916. u16 swfw_mask = 0;
  917. if (IXGBE_READ_REG(hw, IXGBE_STATUS) & IXGBE_STATUS_LAN_ID_1)
  918. swfw_mask = IXGBE_GSSR_PHY1_SM;
  919. else
  920. swfw_mask = IXGBE_GSSR_PHY0_SM;
  921. if (ixgbe_acquire_swfw_sync(hw, swfw_mask) != 0) {
  922. status = IXGBE_ERR_SWFW_SYNC;
  923. goto write_byte_out;
  924. }
  925. do {
  926. ixgbe_i2c_start(hw);
  927. status = ixgbe_clock_out_i2c_byte(hw, dev_addr);
  928. if (status != 0)
  929. goto fail;
  930. status = ixgbe_get_i2c_ack(hw);
  931. if (status != 0)
  932. goto fail;
  933. status = ixgbe_clock_out_i2c_byte(hw, byte_offset);
  934. if (status != 0)
  935. goto fail;
  936. status = ixgbe_get_i2c_ack(hw);
  937. if (status != 0)
  938. goto fail;
  939. status = ixgbe_clock_out_i2c_byte(hw, data);
  940. if (status != 0)
  941. goto fail;
  942. status = ixgbe_get_i2c_ack(hw);
  943. if (status != 0)
  944. goto fail;
  945. ixgbe_i2c_stop(hw);
  946. break;
  947. fail:
  948. ixgbe_i2c_bus_clear(hw);
  949. retry++;
  950. if (retry < max_retry)
  951. hw_dbg(hw, "I2C byte write error - Retrying.\n");
  952. else
  953. hw_dbg(hw, "I2C byte write error.\n");
  954. } while (retry < max_retry);
  955. ixgbe_release_swfw_sync(hw, swfw_mask);
  956. write_byte_out:
  957. return status;
  958. }
  959. /**
  960. * ixgbe_i2c_start - Sets I2C start condition
  961. * @hw: pointer to hardware structure
  962. *
  963. * Sets I2C start condition (High -> Low on SDA while SCL is High)
  964. **/
  965. static void ixgbe_i2c_start(struct ixgbe_hw *hw)
  966. {
  967. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  968. /* Start condition must begin with data and clock high */
  969. ixgbe_set_i2c_data(hw, &i2cctl, 1);
  970. ixgbe_raise_i2c_clk(hw, &i2cctl);
  971. /* Setup time for start condition (4.7us) */
  972. udelay(IXGBE_I2C_T_SU_STA);
  973. ixgbe_set_i2c_data(hw, &i2cctl, 0);
  974. /* Hold time for start condition (4us) */
  975. udelay(IXGBE_I2C_T_HD_STA);
  976. ixgbe_lower_i2c_clk(hw, &i2cctl);
  977. /* Minimum low period of clock is 4.7 us */
  978. udelay(IXGBE_I2C_T_LOW);
  979. }
  980. /**
  981. * ixgbe_i2c_stop - Sets I2C stop condition
  982. * @hw: pointer to hardware structure
  983. *
  984. * Sets I2C stop condition (Low -> High on SDA while SCL is High)
  985. **/
  986. static void ixgbe_i2c_stop(struct ixgbe_hw *hw)
  987. {
  988. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  989. /* Stop condition must begin with data low and clock high */
  990. ixgbe_set_i2c_data(hw, &i2cctl, 0);
  991. ixgbe_raise_i2c_clk(hw, &i2cctl);
  992. /* Setup time for stop condition (4us) */
  993. udelay(IXGBE_I2C_T_SU_STO);
  994. ixgbe_set_i2c_data(hw, &i2cctl, 1);
  995. /* bus free time between stop and start (4.7us)*/
  996. udelay(IXGBE_I2C_T_BUF);
  997. }
  998. /**
  999. * ixgbe_clock_in_i2c_byte - Clocks in one byte via I2C
  1000. * @hw: pointer to hardware structure
  1001. * @data: data byte to clock in
  1002. *
  1003. * Clocks in one byte data via I2C data/clock
  1004. **/
  1005. static s32 ixgbe_clock_in_i2c_byte(struct ixgbe_hw *hw, u8 *data)
  1006. {
  1007. s32 status = 0;
  1008. s32 i;
  1009. bool bit = 0;
  1010. for (i = 7; i >= 0; i--) {
  1011. status = ixgbe_clock_in_i2c_bit(hw, &bit);
  1012. *data |= bit << i;
  1013. if (status != 0)
  1014. break;
  1015. }
  1016. return status;
  1017. }
  1018. /**
  1019. * ixgbe_clock_out_i2c_byte - Clocks out one byte via I2C
  1020. * @hw: pointer to hardware structure
  1021. * @data: data byte clocked out
  1022. *
  1023. * Clocks out one byte data via I2C data/clock
  1024. **/
  1025. static s32 ixgbe_clock_out_i2c_byte(struct ixgbe_hw *hw, u8 data)
  1026. {
  1027. s32 status = 0;
  1028. s32 i;
  1029. u32 i2cctl;
  1030. bool bit = 0;
  1031. for (i = 7; i >= 0; i--) {
  1032. bit = (data >> i) & 0x1;
  1033. status = ixgbe_clock_out_i2c_bit(hw, bit);
  1034. if (status != 0)
  1035. break;
  1036. }
  1037. /* Release SDA line (set high) */
  1038. i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1039. i2cctl |= IXGBE_I2C_DATA_OUT;
  1040. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL, i2cctl);
  1041. return status;
  1042. }
  1043. /**
  1044. * ixgbe_get_i2c_ack - Polls for I2C ACK
  1045. * @hw: pointer to hardware structure
  1046. *
  1047. * Clocks in/out one bit via I2C data/clock
  1048. **/
  1049. static s32 ixgbe_get_i2c_ack(struct ixgbe_hw *hw)
  1050. {
  1051. s32 status;
  1052. u32 i = 0;
  1053. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1054. u32 timeout = 10;
  1055. bool ack = 1;
  1056. status = ixgbe_raise_i2c_clk(hw, &i2cctl);
  1057. if (status != 0)
  1058. goto out;
  1059. /* Minimum high period of clock is 4us */
  1060. udelay(IXGBE_I2C_T_HIGH);
  1061. /* Poll for ACK. Note that ACK in I2C spec is
  1062. * transition from 1 to 0 */
  1063. for (i = 0; i < timeout; i++) {
  1064. i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1065. ack = ixgbe_get_i2c_data(&i2cctl);
  1066. udelay(1);
  1067. if (ack == 0)
  1068. break;
  1069. }
  1070. if (ack == 1) {
  1071. hw_dbg(hw, "I2C ack was not received.\n");
  1072. status = IXGBE_ERR_I2C;
  1073. }
  1074. ixgbe_lower_i2c_clk(hw, &i2cctl);
  1075. /* Minimum low period of clock is 4.7 us */
  1076. udelay(IXGBE_I2C_T_LOW);
  1077. out:
  1078. return status;
  1079. }
  1080. /**
  1081. * ixgbe_clock_in_i2c_bit - Clocks in one bit via I2C data/clock
  1082. * @hw: pointer to hardware structure
  1083. * @data: read data value
  1084. *
  1085. * Clocks in one bit via I2C data/clock
  1086. **/
  1087. static s32 ixgbe_clock_in_i2c_bit(struct ixgbe_hw *hw, bool *data)
  1088. {
  1089. s32 status;
  1090. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1091. status = ixgbe_raise_i2c_clk(hw, &i2cctl);
  1092. /* Minimum high period of clock is 4us */
  1093. udelay(IXGBE_I2C_T_HIGH);
  1094. i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1095. *data = ixgbe_get_i2c_data(&i2cctl);
  1096. ixgbe_lower_i2c_clk(hw, &i2cctl);
  1097. /* Minimum low period of clock is 4.7 us */
  1098. udelay(IXGBE_I2C_T_LOW);
  1099. return status;
  1100. }
  1101. /**
  1102. * ixgbe_clock_out_i2c_bit - Clocks in/out one bit via I2C data/clock
  1103. * @hw: pointer to hardware structure
  1104. * @data: data value to write
  1105. *
  1106. * Clocks out one bit via I2C data/clock
  1107. **/
  1108. static s32 ixgbe_clock_out_i2c_bit(struct ixgbe_hw *hw, bool data)
  1109. {
  1110. s32 status;
  1111. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1112. status = ixgbe_set_i2c_data(hw, &i2cctl, data);
  1113. if (status == 0) {
  1114. status = ixgbe_raise_i2c_clk(hw, &i2cctl);
  1115. /* Minimum high period of clock is 4us */
  1116. udelay(IXGBE_I2C_T_HIGH);
  1117. ixgbe_lower_i2c_clk(hw, &i2cctl);
  1118. /* Minimum low period of clock is 4.7 us.
  1119. * This also takes care of the data hold time.
  1120. */
  1121. udelay(IXGBE_I2C_T_LOW);
  1122. } else {
  1123. status = IXGBE_ERR_I2C;
  1124. hw_dbg(hw, "I2C data was not set to %X\n", data);
  1125. }
  1126. return status;
  1127. }
  1128. /**
  1129. * ixgbe_raise_i2c_clk - Raises the I2C SCL clock
  1130. * @hw: pointer to hardware structure
  1131. * @i2cctl: Current value of I2CCTL register
  1132. *
  1133. * Raises the I2C clock line '0'->'1'
  1134. **/
  1135. static s32 ixgbe_raise_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
  1136. {
  1137. s32 status = 0;
  1138. *i2cctl |= IXGBE_I2C_CLK_OUT;
  1139. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL, *i2cctl);
  1140. /* SCL rise time (1000ns) */
  1141. udelay(IXGBE_I2C_T_RISE);
  1142. return status;
  1143. }
  1144. /**
  1145. * ixgbe_lower_i2c_clk - Lowers the I2C SCL clock
  1146. * @hw: pointer to hardware structure
  1147. * @i2cctl: Current value of I2CCTL register
  1148. *
  1149. * Lowers the I2C clock line '1'->'0'
  1150. **/
  1151. static void ixgbe_lower_i2c_clk(struct ixgbe_hw *hw, u32 *i2cctl)
  1152. {
  1153. *i2cctl &= ~IXGBE_I2C_CLK_OUT;
  1154. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL, *i2cctl);
  1155. /* SCL fall time (300ns) */
  1156. udelay(IXGBE_I2C_T_FALL);
  1157. }
  1158. /**
  1159. * ixgbe_set_i2c_data - Sets the I2C data bit
  1160. * @hw: pointer to hardware structure
  1161. * @i2cctl: Current value of I2CCTL register
  1162. * @data: I2C data value (0 or 1) to set
  1163. *
  1164. * Sets the I2C data bit
  1165. **/
  1166. static s32 ixgbe_set_i2c_data(struct ixgbe_hw *hw, u32 *i2cctl, bool data)
  1167. {
  1168. s32 status = 0;
  1169. if (data)
  1170. *i2cctl |= IXGBE_I2C_DATA_OUT;
  1171. else
  1172. *i2cctl &= ~IXGBE_I2C_DATA_OUT;
  1173. IXGBE_WRITE_REG(hw, IXGBE_I2CCTL, *i2cctl);
  1174. /* Data rise/fall (1000ns/300ns) and set-up time (250ns) */
  1175. udelay(IXGBE_I2C_T_RISE + IXGBE_I2C_T_FALL + IXGBE_I2C_T_SU_DATA);
  1176. /* Verify data was set correctly */
  1177. *i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1178. if (data != ixgbe_get_i2c_data(i2cctl)) {
  1179. status = IXGBE_ERR_I2C;
  1180. hw_dbg(hw, "Error - I2C data was not set to %X.\n", data);
  1181. }
  1182. return status;
  1183. }
  1184. /**
  1185. * ixgbe_get_i2c_data - Reads the I2C SDA data bit
  1186. * @hw: pointer to hardware structure
  1187. * @i2cctl: Current value of I2CCTL register
  1188. *
  1189. * Returns the I2C data bit value
  1190. **/
  1191. static bool ixgbe_get_i2c_data(u32 *i2cctl)
  1192. {
  1193. bool data;
  1194. if (*i2cctl & IXGBE_I2C_DATA_IN)
  1195. data = 1;
  1196. else
  1197. data = 0;
  1198. return data;
  1199. }
  1200. /**
  1201. * ixgbe_i2c_bus_clear - Clears the I2C bus
  1202. * @hw: pointer to hardware structure
  1203. *
  1204. * Clears the I2C bus by sending nine clock pulses.
  1205. * Used when data line is stuck low.
  1206. **/
  1207. static void ixgbe_i2c_bus_clear(struct ixgbe_hw *hw)
  1208. {
  1209. u32 i2cctl = IXGBE_READ_REG(hw, IXGBE_I2CCTL);
  1210. u32 i;
  1211. ixgbe_i2c_start(hw);
  1212. ixgbe_set_i2c_data(hw, &i2cctl, 1);
  1213. for (i = 0; i < 9; i++) {
  1214. ixgbe_raise_i2c_clk(hw, &i2cctl);
  1215. /* Min high period of clock is 4us */
  1216. udelay(IXGBE_I2C_T_HIGH);
  1217. ixgbe_lower_i2c_clk(hw, &i2cctl);
  1218. /* Min low period of clock is 4.7us*/
  1219. udelay(IXGBE_I2C_T_LOW);
  1220. }
  1221. ixgbe_i2c_start(hw);
  1222. /* Put the i2c bus back to default state */
  1223. ixgbe_i2c_stop(hw);
  1224. }
  1225. /**
  1226. * ixgbe_check_phy_link_tnx - Determine link and speed status
  1227. * @hw: pointer to hardware structure
  1228. *
  1229. * Reads the VS1 register to determine if link is up and the current speed for
  1230. * the PHY.
  1231. **/
  1232. s32 ixgbe_check_phy_link_tnx(struct ixgbe_hw *hw, ixgbe_link_speed *speed,
  1233. bool *link_up)
  1234. {
  1235. s32 status = 0;
  1236. u32 time_out;
  1237. u32 max_time_out = 10;
  1238. u16 phy_link = 0;
  1239. u16 phy_speed = 0;
  1240. u16 phy_data = 0;
  1241. /* Initialize speed and link to default case */
  1242. *link_up = false;
  1243. *speed = IXGBE_LINK_SPEED_10GB_FULL;
  1244. /*
  1245. * Check current speed and link status of the PHY register.
  1246. * This is a vendor specific register and may have to
  1247. * be changed for other copper PHYs.
  1248. */
  1249. for (time_out = 0; time_out < max_time_out; time_out++) {
  1250. udelay(10);
  1251. status = hw->phy.ops.read_reg(hw,
  1252. IXGBE_MDIO_VENDOR_SPECIFIC_1_STATUS,
  1253. MDIO_MMD_VEND1,
  1254. &phy_data);
  1255. phy_link = phy_data &
  1256. IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS;
  1257. phy_speed = phy_data &
  1258. IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS;
  1259. if (phy_link == IXGBE_MDIO_VENDOR_SPECIFIC_1_LINK_STATUS) {
  1260. *link_up = true;
  1261. if (phy_speed ==
  1262. IXGBE_MDIO_VENDOR_SPECIFIC_1_SPEED_STATUS)
  1263. *speed = IXGBE_LINK_SPEED_1GB_FULL;
  1264. break;
  1265. }
  1266. }
  1267. return status;
  1268. }
  1269. /**
  1270. * ixgbe_get_phy_firmware_version_tnx - Gets the PHY Firmware Version
  1271. * @hw: pointer to hardware structure
  1272. * @firmware_version: pointer to the PHY Firmware Version
  1273. **/
  1274. s32 ixgbe_get_phy_firmware_version_tnx(struct ixgbe_hw *hw,
  1275. u16 *firmware_version)
  1276. {
  1277. s32 status = 0;
  1278. status = hw->phy.ops.read_reg(hw, TNX_FW_REV, MDIO_MMD_VEND1,
  1279. firmware_version);
  1280. return status;
  1281. }
  1282. /**
  1283. * ixgbe_get_phy_firmware_version_generic - Gets the PHY Firmware Version
  1284. * @hw: pointer to hardware structure
  1285. * @firmware_version: pointer to the PHY Firmware Version
  1286. **/
  1287. s32 ixgbe_get_phy_firmware_version_generic(struct ixgbe_hw *hw,
  1288. u16 *firmware_version)
  1289. {
  1290. s32 status = 0;
  1291. status = hw->phy.ops.read_reg(hw, AQ_FW_REV, MDIO_MMD_VEND1,
  1292. firmware_version);
  1293. return status;
  1294. }
  1295. /**
  1296. * ixgbe_tn_check_overtemp - Checks if an overtemp occured.
  1297. * @hw: pointer to hardware structure
  1298. *
  1299. * Checks if the LASI temp alarm status was triggered due to overtemp
  1300. **/
  1301. s32 ixgbe_tn_check_overtemp(struct ixgbe_hw *hw)
  1302. {
  1303. s32 status = 0;
  1304. u16 phy_data = 0;
  1305. if (hw->device_id != IXGBE_DEV_ID_82599_T3_LOM)
  1306. goto out;
  1307. /* Check that the LASI temp alarm status was triggered */
  1308. hw->phy.ops.read_reg(hw, IXGBE_TN_LASI_STATUS_REG,
  1309. MDIO_MMD_PMAPMD, &phy_data);
  1310. if (!(phy_data & IXGBE_TN_LASI_STATUS_TEMP_ALARM))
  1311. goto out;
  1312. status = IXGBE_ERR_OVERTEMP;
  1313. out:
  1314. return status;
  1315. }