atl1c_hw.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864
  1. /*
  2. * Copyright(c) 2007 Atheros Corporation. All rights reserved.
  3. *
  4. * Derived from Intel e1000 driver
  5. * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
  6. *
  7. * This program is free software; you can redistribute it and/or modify it
  8. * under the terms of the GNU General Public License as published by the Free
  9. * Software Foundation; either version 2 of the License, or (at your option)
  10. * any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful, but WITHOUT
  13. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  14. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  15. * more details.
  16. *
  17. * You should have received a copy of the GNU General Public License along with
  18. * this program; if not, write to the Free Software Foundation, Inc., 59
  19. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  20. */
  21. #include <linux/pci.h>
  22. #include <linux/delay.h>
  23. #include <linux/mii.h>
  24. #include <linux/crc32.h>
  25. #include "atl1c.h"
  26. /*
  27. * check_eeprom_exist
  28. * return 1 if eeprom exist
  29. */
  30. int atl1c_check_eeprom_exist(struct atl1c_hw *hw)
  31. {
  32. u32 data;
  33. AT_READ_REG(hw, REG_TWSI_DEBUG, &data);
  34. if (data & TWSI_DEBUG_DEV_EXIST)
  35. return 1;
  36. AT_READ_REG(hw, REG_MASTER_CTRL, &data);
  37. if (data & MASTER_CTRL_OTP_SEL)
  38. return 1;
  39. return 0;
  40. }
  41. void atl1c_hw_set_mac_addr(struct atl1c_hw *hw, u8 *mac_addr)
  42. {
  43. u32 value;
  44. /*
  45. * 00-0B-6A-F6-00-DC
  46. * 0: 6AF600DC 1: 000B
  47. * low dword
  48. */
  49. value = mac_addr[2] << 24 |
  50. mac_addr[3] << 16 |
  51. mac_addr[4] << 8 |
  52. mac_addr[5];
  53. AT_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 0, value);
  54. /* hight dword */
  55. value = mac_addr[0] << 8 |
  56. mac_addr[1];
  57. AT_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 1, value);
  58. }
  59. /* read mac address from hardware register */
  60. static bool atl1c_read_current_addr(struct atl1c_hw *hw, u8 *eth_addr)
  61. {
  62. u32 addr[2];
  63. AT_READ_REG(hw, REG_MAC_STA_ADDR, &addr[0]);
  64. AT_READ_REG(hw, REG_MAC_STA_ADDR + 4, &addr[1]);
  65. *(u32 *) &eth_addr[2] = htonl(addr[0]);
  66. *(u16 *) &eth_addr[0] = htons((u16)addr[1]);
  67. return is_valid_ether_addr(eth_addr);
  68. }
  69. /*
  70. * atl1c_get_permanent_address
  71. * return 0 if get valid mac address,
  72. */
  73. static int atl1c_get_permanent_address(struct atl1c_hw *hw)
  74. {
  75. u32 i;
  76. u32 otp_ctrl_data;
  77. u32 twsi_ctrl_data;
  78. u16 phy_data;
  79. bool raise_vol = false;
  80. /* MAC-address from BIOS is the 1st priority */
  81. if (atl1c_read_current_addr(hw, hw->perm_mac_addr))
  82. return 0;
  83. /* init */
  84. AT_READ_REG(hw, REG_OTP_CTRL, &otp_ctrl_data);
  85. if (atl1c_check_eeprom_exist(hw)) {
  86. if (hw->nic_type == athr_l1c || hw->nic_type == athr_l2c) {
  87. /* Enable OTP CLK */
  88. if (!(otp_ctrl_data & OTP_CTRL_CLK_EN)) {
  89. otp_ctrl_data |= OTP_CTRL_CLK_EN;
  90. AT_WRITE_REG(hw, REG_OTP_CTRL, otp_ctrl_data);
  91. AT_WRITE_FLUSH(hw);
  92. msleep(1);
  93. }
  94. }
  95. /* raise voltage temporally for l2cb */
  96. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2) {
  97. atl1c_read_phy_dbg(hw, MIIDBG_ANACTRL, &phy_data);
  98. phy_data &= ~ANACTRL_HB_EN;
  99. atl1c_write_phy_dbg(hw, MIIDBG_ANACTRL, phy_data);
  100. atl1c_read_phy_dbg(hw, MIIDBG_VOLT_CTRL, &phy_data);
  101. phy_data |= VOLT_CTRL_SWLOWEST;
  102. atl1c_write_phy_dbg(hw, MIIDBG_VOLT_CTRL, phy_data);
  103. udelay(20);
  104. raise_vol = true;
  105. }
  106. AT_READ_REG(hw, REG_TWSI_CTRL, &twsi_ctrl_data);
  107. twsi_ctrl_data |= TWSI_CTRL_SW_LDSTART;
  108. AT_WRITE_REG(hw, REG_TWSI_CTRL, twsi_ctrl_data);
  109. for (i = 0; i < AT_TWSI_EEPROM_TIMEOUT; i++) {
  110. msleep(10);
  111. AT_READ_REG(hw, REG_TWSI_CTRL, &twsi_ctrl_data);
  112. if ((twsi_ctrl_data & TWSI_CTRL_SW_LDSTART) == 0)
  113. break;
  114. }
  115. if (i >= AT_TWSI_EEPROM_TIMEOUT)
  116. return -1;
  117. }
  118. /* Disable OTP_CLK */
  119. if ((hw->nic_type == athr_l1c || hw->nic_type == athr_l2c)) {
  120. otp_ctrl_data &= ~OTP_CTRL_CLK_EN;
  121. AT_WRITE_REG(hw, REG_OTP_CTRL, otp_ctrl_data);
  122. msleep(1);
  123. }
  124. if (raise_vol) {
  125. atl1c_read_phy_dbg(hw, MIIDBG_ANACTRL, &phy_data);
  126. phy_data |= ANACTRL_HB_EN;
  127. atl1c_write_phy_dbg(hw, MIIDBG_ANACTRL, phy_data);
  128. atl1c_read_phy_dbg(hw, MIIDBG_VOLT_CTRL, &phy_data);
  129. phy_data &= ~VOLT_CTRL_SWLOWEST;
  130. atl1c_write_phy_dbg(hw, MIIDBG_VOLT_CTRL, phy_data);
  131. udelay(20);
  132. }
  133. if (atl1c_read_current_addr(hw, hw->perm_mac_addr))
  134. return 0;
  135. return -1;
  136. }
  137. bool atl1c_read_eeprom(struct atl1c_hw *hw, u32 offset, u32 *p_value)
  138. {
  139. int i;
  140. bool ret = false;
  141. u32 otp_ctrl_data;
  142. u32 control;
  143. u32 data;
  144. if (offset & 3)
  145. return ret; /* address do not align */
  146. AT_READ_REG(hw, REG_OTP_CTRL, &otp_ctrl_data);
  147. if (!(otp_ctrl_data & OTP_CTRL_CLK_EN))
  148. AT_WRITE_REG(hw, REG_OTP_CTRL,
  149. (otp_ctrl_data | OTP_CTRL_CLK_EN));
  150. AT_WRITE_REG(hw, REG_EEPROM_DATA_LO, 0);
  151. control = (offset & EEPROM_CTRL_ADDR_MASK) << EEPROM_CTRL_ADDR_SHIFT;
  152. AT_WRITE_REG(hw, REG_EEPROM_CTRL, control);
  153. for (i = 0; i < 10; i++) {
  154. udelay(100);
  155. AT_READ_REG(hw, REG_EEPROM_CTRL, &control);
  156. if (control & EEPROM_CTRL_RW)
  157. break;
  158. }
  159. if (control & EEPROM_CTRL_RW) {
  160. AT_READ_REG(hw, REG_EEPROM_CTRL, &data);
  161. AT_READ_REG(hw, REG_EEPROM_DATA_LO, p_value);
  162. data = data & 0xFFFF;
  163. *p_value = swab32((data << 16) | (*p_value >> 16));
  164. ret = true;
  165. }
  166. if (!(otp_ctrl_data & OTP_CTRL_CLK_EN))
  167. AT_WRITE_REG(hw, REG_OTP_CTRL, otp_ctrl_data);
  168. return ret;
  169. }
  170. /*
  171. * Reads the adapter's MAC address from the EEPROM
  172. *
  173. * hw - Struct containing variables accessed by shared code
  174. */
  175. int atl1c_read_mac_addr(struct atl1c_hw *hw)
  176. {
  177. int err = 0;
  178. err = atl1c_get_permanent_address(hw);
  179. if (err)
  180. eth_random_addr(hw->perm_mac_addr);
  181. memcpy(hw->mac_addr, hw->perm_mac_addr, sizeof(hw->perm_mac_addr));
  182. return err;
  183. }
  184. /*
  185. * atl1c_hash_mc_addr
  186. * purpose
  187. * set hash value for a multicast address
  188. * hash calcu processing :
  189. * 1. calcu 32bit CRC for multicast address
  190. * 2. reverse crc with MSB to LSB
  191. */
  192. u32 atl1c_hash_mc_addr(struct atl1c_hw *hw, u8 *mc_addr)
  193. {
  194. u32 crc32;
  195. u32 value = 0;
  196. int i;
  197. crc32 = ether_crc_le(6, mc_addr);
  198. for (i = 0; i < 32; i++)
  199. value |= (((crc32 >> i) & 1) << (31 - i));
  200. return value;
  201. }
  202. /*
  203. * Sets the bit in the multicast table corresponding to the hash value.
  204. * hw - Struct containing variables accessed by shared code
  205. * hash_value - Multicast address hash value
  206. */
  207. void atl1c_hash_set(struct atl1c_hw *hw, u32 hash_value)
  208. {
  209. u32 hash_bit, hash_reg;
  210. u32 mta;
  211. /*
  212. * The HASH Table is a register array of 2 32-bit registers.
  213. * It is treated like an array of 64 bits. We want to set
  214. * bit BitArray[hash_value]. So we figure out what register
  215. * the bit is in, read it, OR in the new bit, then write
  216. * back the new value. The register is determined by the
  217. * upper bit of the hash value and the bit within that
  218. * register are determined by the lower 5 bits of the value.
  219. */
  220. hash_reg = (hash_value >> 31) & 0x1;
  221. hash_bit = (hash_value >> 26) & 0x1F;
  222. mta = AT_READ_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg);
  223. mta |= (1 << hash_bit);
  224. AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg, mta);
  225. }
  226. /*
  227. * wait mdio module be idle
  228. * return true: idle
  229. * false: still busy
  230. */
  231. bool atl1c_wait_mdio_idle(struct atl1c_hw *hw)
  232. {
  233. u32 val;
  234. int i;
  235. for (i = 0; i < MDIO_MAX_AC_TO; i++) {
  236. AT_READ_REG(hw, REG_MDIO_CTRL, &val);
  237. if (!(val & (MDIO_CTRL_BUSY | MDIO_CTRL_START)))
  238. break;
  239. udelay(10);
  240. }
  241. return i != MDIO_MAX_AC_TO;
  242. }
  243. void atl1c_stop_phy_polling(struct atl1c_hw *hw)
  244. {
  245. if (!(hw->ctrl_flags & ATL1C_FPGA_VERSION))
  246. return;
  247. AT_WRITE_REG(hw, REG_MDIO_CTRL, 0);
  248. atl1c_wait_mdio_idle(hw);
  249. }
  250. void atl1c_start_phy_polling(struct atl1c_hw *hw, u16 clk_sel)
  251. {
  252. u32 val;
  253. if (!(hw->ctrl_flags & ATL1C_FPGA_VERSION))
  254. return;
  255. val = MDIO_CTRL_SPRES_PRMBL |
  256. FIELDX(MDIO_CTRL_CLK_SEL, clk_sel) |
  257. FIELDX(MDIO_CTRL_REG, 1) |
  258. MDIO_CTRL_START |
  259. MDIO_CTRL_OP_READ;
  260. AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
  261. atl1c_wait_mdio_idle(hw);
  262. val |= MDIO_CTRL_AP_EN;
  263. val &= ~MDIO_CTRL_START;
  264. AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
  265. udelay(30);
  266. }
  267. /*
  268. * atl1c_read_phy_core
  269. * core function to read register in PHY via MDIO control regsiter.
  270. * ext: extension register (see IEEE 802.3)
  271. * dev: device address (see IEEE 802.3 DEVAD, PRTAD is fixed to 0)
  272. * reg: reg to read
  273. */
  274. int atl1c_read_phy_core(struct atl1c_hw *hw, bool ext, u8 dev,
  275. u16 reg, u16 *phy_data)
  276. {
  277. u32 val;
  278. u16 clk_sel = MDIO_CTRL_CLK_25_4;
  279. atl1c_stop_phy_polling(hw);
  280. *phy_data = 0;
  281. /* only l2c_b2 & l1d_2 could use slow clock */
  282. if ((hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) &&
  283. hw->hibernate)
  284. clk_sel = MDIO_CTRL_CLK_25_128;
  285. if (ext) {
  286. val = FIELDX(MDIO_EXTN_DEVAD, dev) | FIELDX(MDIO_EXTN_REG, reg);
  287. AT_WRITE_REG(hw, REG_MDIO_EXTN, val);
  288. val = MDIO_CTRL_SPRES_PRMBL |
  289. FIELDX(MDIO_CTRL_CLK_SEL, clk_sel) |
  290. MDIO_CTRL_START |
  291. MDIO_CTRL_MODE_EXT |
  292. MDIO_CTRL_OP_READ;
  293. } else {
  294. val = MDIO_CTRL_SPRES_PRMBL |
  295. FIELDX(MDIO_CTRL_CLK_SEL, clk_sel) |
  296. FIELDX(MDIO_CTRL_REG, reg) |
  297. MDIO_CTRL_START |
  298. MDIO_CTRL_OP_READ;
  299. }
  300. AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
  301. if (!atl1c_wait_mdio_idle(hw))
  302. return -1;
  303. AT_READ_REG(hw, REG_MDIO_CTRL, &val);
  304. *phy_data = (u16)FIELD_GETX(val, MDIO_CTRL_DATA);
  305. atl1c_start_phy_polling(hw, clk_sel);
  306. return 0;
  307. }
  308. /*
  309. * atl1c_write_phy_core
  310. * core function to write to register in PHY via MDIO control register.
  311. * ext: extension register (see IEEE 802.3)
  312. * dev: device address (see IEEE 802.3 DEVAD, PRTAD is fixed to 0)
  313. * reg: reg to write
  314. */
  315. int atl1c_write_phy_core(struct atl1c_hw *hw, bool ext, u8 dev,
  316. u16 reg, u16 phy_data)
  317. {
  318. u32 val;
  319. u16 clk_sel = MDIO_CTRL_CLK_25_4;
  320. atl1c_stop_phy_polling(hw);
  321. /* only l2c_b2 & l1d_2 could use slow clock */
  322. if ((hw->nic_type == athr_l2c_b2 || hw->nic_type == athr_l1d_2) &&
  323. hw->hibernate)
  324. clk_sel = MDIO_CTRL_CLK_25_128;
  325. if (ext) {
  326. val = FIELDX(MDIO_EXTN_DEVAD, dev) | FIELDX(MDIO_EXTN_REG, reg);
  327. AT_WRITE_REG(hw, REG_MDIO_EXTN, val);
  328. val = MDIO_CTRL_SPRES_PRMBL |
  329. FIELDX(MDIO_CTRL_CLK_SEL, clk_sel) |
  330. FIELDX(MDIO_CTRL_DATA, phy_data) |
  331. MDIO_CTRL_START |
  332. MDIO_CTRL_MODE_EXT;
  333. } else {
  334. val = MDIO_CTRL_SPRES_PRMBL |
  335. FIELDX(MDIO_CTRL_CLK_SEL, clk_sel) |
  336. FIELDX(MDIO_CTRL_DATA, phy_data) |
  337. FIELDX(MDIO_CTRL_REG, reg) |
  338. MDIO_CTRL_START;
  339. }
  340. AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
  341. if (!atl1c_wait_mdio_idle(hw))
  342. return -1;
  343. atl1c_start_phy_polling(hw, clk_sel);
  344. return 0;
  345. }
  346. /*
  347. * Reads the value from a PHY register
  348. * hw - Struct containing variables accessed by shared code
  349. * reg_addr - address of the PHY register to read
  350. */
  351. int atl1c_read_phy_reg(struct atl1c_hw *hw, u16 reg_addr, u16 *phy_data)
  352. {
  353. return atl1c_read_phy_core(hw, false, 0, reg_addr, phy_data);
  354. }
  355. /*
  356. * Writes a value to a PHY register
  357. * hw - Struct containing variables accessed by shared code
  358. * reg_addr - address of the PHY register to write
  359. * data - data to write to the PHY
  360. */
  361. int atl1c_write_phy_reg(struct atl1c_hw *hw, u32 reg_addr, u16 phy_data)
  362. {
  363. return atl1c_write_phy_core(hw, false, 0, reg_addr, phy_data);
  364. }
  365. /* read from PHY extension register */
  366. int atl1c_read_phy_ext(struct atl1c_hw *hw, u8 dev_addr,
  367. u16 reg_addr, u16 *phy_data)
  368. {
  369. return atl1c_read_phy_core(hw, true, dev_addr, reg_addr, phy_data);
  370. }
  371. /* write to PHY extension register */
  372. int atl1c_write_phy_ext(struct atl1c_hw *hw, u8 dev_addr,
  373. u16 reg_addr, u16 phy_data)
  374. {
  375. return atl1c_write_phy_core(hw, true, dev_addr, reg_addr, phy_data);
  376. }
  377. int atl1c_read_phy_dbg(struct atl1c_hw *hw, u16 reg_addr, u16 *phy_data)
  378. {
  379. int err;
  380. err = atl1c_write_phy_reg(hw, MII_DBG_ADDR, reg_addr);
  381. if (unlikely(err))
  382. return err;
  383. else
  384. err = atl1c_read_phy_reg(hw, MII_DBG_DATA, phy_data);
  385. return err;
  386. }
  387. int atl1c_write_phy_dbg(struct atl1c_hw *hw, u16 reg_addr, u16 phy_data)
  388. {
  389. int err;
  390. err = atl1c_write_phy_reg(hw, MII_DBG_ADDR, reg_addr);
  391. if (unlikely(err))
  392. return err;
  393. else
  394. err = atl1c_write_phy_reg(hw, MII_DBG_DATA, phy_data);
  395. return err;
  396. }
  397. /*
  398. * Configures PHY autoneg and flow control advertisement settings
  399. *
  400. * hw - Struct containing variables accessed by shared code
  401. */
  402. static int atl1c_phy_setup_adv(struct atl1c_hw *hw)
  403. {
  404. u16 mii_adv_data = ADVERTISE_DEFAULT_CAP & ~ADVERTISE_ALL;
  405. u16 mii_giga_ctrl_data = GIGA_CR_1000T_DEFAULT_CAP &
  406. ~GIGA_CR_1000T_SPEED_MASK;
  407. if (hw->autoneg_advertised & ADVERTISED_10baseT_Half)
  408. mii_adv_data |= ADVERTISE_10HALF;
  409. if (hw->autoneg_advertised & ADVERTISED_10baseT_Full)
  410. mii_adv_data |= ADVERTISE_10FULL;
  411. if (hw->autoneg_advertised & ADVERTISED_100baseT_Half)
  412. mii_adv_data |= ADVERTISE_100HALF;
  413. if (hw->autoneg_advertised & ADVERTISED_100baseT_Full)
  414. mii_adv_data |= ADVERTISE_100FULL;
  415. if (hw->autoneg_advertised & ADVERTISED_Autoneg)
  416. mii_adv_data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
  417. ADVERTISE_100HALF | ADVERTISE_100FULL;
  418. if (hw->link_cap_flags & ATL1C_LINK_CAP_1000M) {
  419. if (hw->autoneg_advertised & ADVERTISED_1000baseT_Half)
  420. mii_giga_ctrl_data |= ADVERTISE_1000HALF;
  421. if (hw->autoneg_advertised & ADVERTISED_1000baseT_Full)
  422. mii_giga_ctrl_data |= ADVERTISE_1000FULL;
  423. if (hw->autoneg_advertised & ADVERTISED_Autoneg)
  424. mii_giga_ctrl_data |= ADVERTISE_1000HALF |
  425. ADVERTISE_1000FULL;
  426. }
  427. if (atl1c_write_phy_reg(hw, MII_ADVERTISE, mii_adv_data) != 0 ||
  428. atl1c_write_phy_reg(hw, MII_CTRL1000, mii_giga_ctrl_data) != 0)
  429. return -1;
  430. return 0;
  431. }
  432. void atl1c_phy_disable(struct atl1c_hw *hw)
  433. {
  434. atl1c_power_saving(hw, 0);
  435. }
  436. int atl1c_phy_reset(struct atl1c_hw *hw)
  437. {
  438. struct atl1c_adapter *adapter = hw->adapter;
  439. struct pci_dev *pdev = adapter->pdev;
  440. u16 phy_data;
  441. u32 phy_ctrl_data, lpi_ctrl;
  442. int err;
  443. /* reset PHY core */
  444. AT_READ_REG(hw, REG_GPHY_CTRL, &phy_ctrl_data);
  445. phy_ctrl_data &= ~(GPHY_CTRL_EXT_RESET | GPHY_CTRL_PHY_IDDQ |
  446. GPHY_CTRL_GATE_25M_EN | GPHY_CTRL_PWDOWN_HW | GPHY_CTRL_CLS);
  447. phy_ctrl_data |= GPHY_CTRL_SEL_ANA_RST;
  448. if (!(hw->ctrl_flags & ATL1C_HIB_DISABLE))
  449. phy_ctrl_data |= (GPHY_CTRL_HIB_EN | GPHY_CTRL_HIB_PULSE);
  450. else
  451. phy_ctrl_data &= ~(GPHY_CTRL_HIB_EN | GPHY_CTRL_HIB_PULSE);
  452. AT_WRITE_REG(hw, REG_GPHY_CTRL, phy_ctrl_data);
  453. AT_WRITE_FLUSH(hw);
  454. udelay(10);
  455. AT_WRITE_REG(hw, REG_GPHY_CTRL, phy_ctrl_data | GPHY_CTRL_EXT_RESET);
  456. AT_WRITE_FLUSH(hw);
  457. udelay(10 * GPHY_CTRL_EXT_RST_TO); /* delay 800us */
  458. /* switch clock */
  459. if (hw->nic_type == athr_l2c_b) {
  460. atl1c_read_phy_dbg(hw, MIIDBG_CFGLPSPD, &phy_data);
  461. atl1c_write_phy_dbg(hw, MIIDBG_CFGLPSPD,
  462. phy_data & ~CFGLPSPD_RSTCNT_CLK125SW);
  463. }
  464. /* tx-half amplitude issue fix */
  465. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2) {
  466. atl1c_read_phy_dbg(hw, MIIDBG_CABLE1TH_DET, &phy_data);
  467. phy_data |= CABLE1TH_DET_EN;
  468. atl1c_write_phy_dbg(hw, MIIDBG_CABLE1TH_DET, phy_data);
  469. }
  470. /* clear bit3 of dbgport 3B to lower voltage */
  471. if (!(hw->ctrl_flags & ATL1C_HIB_DISABLE)) {
  472. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2) {
  473. atl1c_read_phy_dbg(hw, MIIDBG_VOLT_CTRL, &phy_data);
  474. phy_data &= ~VOLT_CTRL_SWLOWEST;
  475. atl1c_write_phy_dbg(hw, MIIDBG_VOLT_CTRL, phy_data);
  476. }
  477. /* power saving config */
  478. phy_data =
  479. hw->nic_type == athr_l1d || hw->nic_type == athr_l1d_2 ?
  480. L1D_LEGCYPS_DEF : L1C_LEGCYPS_DEF;
  481. atl1c_write_phy_dbg(hw, MIIDBG_LEGCYPS, phy_data);
  482. /* hib */
  483. atl1c_write_phy_dbg(hw, MIIDBG_SYSMODCTRL,
  484. SYSMODCTRL_IECHOADJ_DEF);
  485. } else {
  486. /* disable pws */
  487. atl1c_read_phy_dbg(hw, MIIDBG_LEGCYPS, &phy_data);
  488. atl1c_write_phy_dbg(hw, MIIDBG_LEGCYPS,
  489. phy_data & ~LEGCYPS_EN);
  490. /* disable hibernate */
  491. atl1c_read_phy_dbg(hw, MIIDBG_HIBNEG, &phy_data);
  492. atl1c_write_phy_dbg(hw, MIIDBG_HIBNEG,
  493. phy_data & HIBNEG_PSHIB_EN);
  494. }
  495. /* disable AZ(EEE) by default */
  496. if (hw->nic_type == athr_l1d || hw->nic_type == athr_l1d_2 ||
  497. hw->nic_type == athr_l2c_b2) {
  498. AT_READ_REG(hw, REG_LPI_CTRL, &lpi_ctrl);
  499. AT_WRITE_REG(hw, REG_LPI_CTRL, lpi_ctrl & ~LPI_CTRL_EN);
  500. atl1c_write_phy_ext(hw, MIIEXT_ANEG, MIIEXT_LOCAL_EEEADV, 0);
  501. atl1c_write_phy_ext(hw, MIIEXT_PCS, MIIEXT_CLDCTRL3,
  502. L2CB_CLDCTRL3);
  503. }
  504. /* other debug port to set */
  505. atl1c_write_phy_dbg(hw, MIIDBG_ANACTRL, ANACTRL_DEF);
  506. atl1c_write_phy_dbg(hw, MIIDBG_SRDSYSMOD, SRDSYSMOD_DEF);
  507. atl1c_write_phy_dbg(hw, MIIDBG_TST10BTCFG, TST10BTCFG_DEF);
  508. /* UNH-IOL test issue, set bit7 */
  509. atl1c_write_phy_dbg(hw, MIIDBG_TST100BTCFG,
  510. TST100BTCFG_DEF | TST100BTCFG_LITCH_EN);
  511. /* set phy interrupt mask */
  512. phy_data = IER_LINK_UP | IER_LINK_DOWN;
  513. err = atl1c_write_phy_reg(hw, MII_IER, phy_data);
  514. if (err) {
  515. if (netif_msg_hw(adapter))
  516. dev_err(&pdev->dev,
  517. "Error enable PHY linkChange Interrupt\n");
  518. return err;
  519. }
  520. return 0;
  521. }
  522. int atl1c_phy_init(struct atl1c_hw *hw)
  523. {
  524. struct atl1c_adapter *adapter = hw->adapter;
  525. struct pci_dev *pdev = adapter->pdev;
  526. int ret_val;
  527. u16 mii_bmcr_data = BMCR_RESET;
  528. if ((atl1c_read_phy_reg(hw, MII_PHYSID1, &hw->phy_id1) != 0) ||
  529. (atl1c_read_phy_reg(hw, MII_PHYSID2, &hw->phy_id2) != 0)) {
  530. dev_err(&pdev->dev, "Error get phy ID\n");
  531. return -1;
  532. }
  533. switch (hw->media_type) {
  534. case MEDIA_TYPE_AUTO_SENSOR:
  535. ret_val = atl1c_phy_setup_adv(hw);
  536. if (ret_val) {
  537. if (netif_msg_link(adapter))
  538. dev_err(&pdev->dev,
  539. "Error Setting up Auto-Negotiation\n");
  540. return ret_val;
  541. }
  542. mii_bmcr_data |= BMCR_ANENABLE | BMCR_ANRESTART;
  543. break;
  544. case MEDIA_TYPE_100M_FULL:
  545. mii_bmcr_data |= BMCR_SPEED100 | BMCR_FULLDPLX;
  546. break;
  547. case MEDIA_TYPE_100M_HALF:
  548. mii_bmcr_data |= BMCR_SPEED100;
  549. break;
  550. case MEDIA_TYPE_10M_FULL:
  551. mii_bmcr_data |= BMCR_FULLDPLX;
  552. break;
  553. case MEDIA_TYPE_10M_HALF:
  554. break;
  555. default:
  556. if (netif_msg_link(adapter))
  557. dev_err(&pdev->dev, "Wrong Media type %d\n",
  558. hw->media_type);
  559. return -1;
  560. }
  561. ret_val = atl1c_write_phy_reg(hw, MII_BMCR, mii_bmcr_data);
  562. if (ret_val)
  563. return ret_val;
  564. hw->phy_configured = true;
  565. return 0;
  566. }
  567. /*
  568. * Detects the current speed and duplex settings of the hardware.
  569. *
  570. * hw - Struct containing variables accessed by shared code
  571. * speed - Speed of the connection
  572. * duplex - Duplex setting of the connection
  573. */
  574. int atl1c_get_speed_and_duplex(struct atl1c_hw *hw, u16 *speed, u16 *duplex)
  575. {
  576. int err;
  577. u16 phy_data;
  578. /* Read PHY Specific Status Register (17) */
  579. err = atl1c_read_phy_reg(hw, MII_GIGA_PSSR, &phy_data);
  580. if (err)
  581. return err;
  582. if (!(phy_data & GIGA_PSSR_SPD_DPLX_RESOLVED))
  583. return -1;
  584. switch (phy_data & GIGA_PSSR_SPEED) {
  585. case GIGA_PSSR_1000MBS:
  586. *speed = SPEED_1000;
  587. break;
  588. case GIGA_PSSR_100MBS:
  589. *speed = SPEED_100;
  590. break;
  591. case GIGA_PSSR_10MBS:
  592. *speed = SPEED_10;
  593. break;
  594. default:
  595. return -1;
  596. }
  597. if (phy_data & GIGA_PSSR_DPLX)
  598. *duplex = FULL_DUPLEX;
  599. else
  600. *duplex = HALF_DUPLEX;
  601. return 0;
  602. }
  603. /* select one link mode to get lower power consumption */
  604. int atl1c_phy_to_ps_link(struct atl1c_hw *hw)
  605. {
  606. struct atl1c_adapter *adapter = hw->adapter;
  607. struct pci_dev *pdev = adapter->pdev;
  608. int ret = 0;
  609. u16 autoneg_advertised = ADVERTISED_10baseT_Half;
  610. u16 save_autoneg_advertised;
  611. u16 phy_data;
  612. u16 mii_lpa_data;
  613. u16 speed = SPEED_0;
  614. u16 duplex = FULL_DUPLEX;
  615. int i;
  616. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  617. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  618. if (phy_data & BMSR_LSTATUS) {
  619. atl1c_read_phy_reg(hw, MII_LPA, &mii_lpa_data);
  620. if (mii_lpa_data & LPA_10FULL)
  621. autoneg_advertised = ADVERTISED_10baseT_Full;
  622. else if (mii_lpa_data & LPA_10HALF)
  623. autoneg_advertised = ADVERTISED_10baseT_Half;
  624. else if (mii_lpa_data & LPA_100HALF)
  625. autoneg_advertised = ADVERTISED_100baseT_Half;
  626. else if (mii_lpa_data & LPA_100FULL)
  627. autoneg_advertised = ADVERTISED_100baseT_Full;
  628. save_autoneg_advertised = hw->autoneg_advertised;
  629. hw->phy_configured = false;
  630. hw->autoneg_advertised = autoneg_advertised;
  631. if (atl1c_restart_autoneg(hw) != 0) {
  632. dev_dbg(&pdev->dev, "phy autoneg failed\n");
  633. ret = -1;
  634. }
  635. hw->autoneg_advertised = save_autoneg_advertised;
  636. if (mii_lpa_data) {
  637. for (i = 0; i < AT_SUSPEND_LINK_TIMEOUT; i++) {
  638. mdelay(100);
  639. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  640. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  641. if (phy_data & BMSR_LSTATUS) {
  642. if (atl1c_get_speed_and_duplex(hw, &speed,
  643. &duplex) != 0)
  644. dev_dbg(&pdev->dev,
  645. "get speed and duplex failed\n");
  646. break;
  647. }
  648. }
  649. }
  650. } else {
  651. speed = SPEED_10;
  652. duplex = HALF_DUPLEX;
  653. }
  654. adapter->link_speed = speed;
  655. adapter->link_duplex = duplex;
  656. return ret;
  657. }
  658. int atl1c_restart_autoneg(struct atl1c_hw *hw)
  659. {
  660. int err = 0;
  661. u16 mii_bmcr_data = BMCR_RESET;
  662. err = atl1c_phy_setup_adv(hw);
  663. if (err)
  664. return err;
  665. mii_bmcr_data |= BMCR_ANENABLE | BMCR_ANRESTART;
  666. return atl1c_write_phy_reg(hw, MII_BMCR, mii_bmcr_data);
  667. }
  668. int atl1c_power_saving(struct atl1c_hw *hw, u32 wufc)
  669. {
  670. struct atl1c_adapter *adapter = hw->adapter;
  671. struct pci_dev *pdev = adapter->pdev;
  672. u32 master_ctrl, mac_ctrl, phy_ctrl;
  673. u32 wol_ctrl, speed;
  674. u16 phy_data;
  675. wol_ctrl = 0;
  676. speed = adapter->link_speed == SPEED_1000 ?
  677. MAC_CTRL_SPEED_1000 : MAC_CTRL_SPEED_10_100;
  678. AT_READ_REG(hw, REG_MASTER_CTRL, &master_ctrl);
  679. AT_READ_REG(hw, REG_MAC_CTRL, &mac_ctrl);
  680. AT_READ_REG(hw, REG_GPHY_CTRL, &phy_ctrl);
  681. master_ctrl &= ~MASTER_CTRL_CLK_SEL_DIS;
  682. mac_ctrl = FIELD_SETX(mac_ctrl, MAC_CTRL_SPEED, speed);
  683. mac_ctrl &= ~(MAC_CTRL_DUPLX | MAC_CTRL_RX_EN | MAC_CTRL_TX_EN);
  684. if (adapter->link_duplex == FULL_DUPLEX)
  685. mac_ctrl |= MAC_CTRL_DUPLX;
  686. phy_ctrl &= ~(GPHY_CTRL_EXT_RESET | GPHY_CTRL_CLS);
  687. phy_ctrl |= GPHY_CTRL_SEL_ANA_RST | GPHY_CTRL_HIB_PULSE |
  688. GPHY_CTRL_HIB_EN;
  689. if (!wufc) { /* without WoL */
  690. master_ctrl |= MASTER_CTRL_CLK_SEL_DIS;
  691. phy_ctrl |= GPHY_CTRL_PHY_IDDQ | GPHY_CTRL_PWDOWN_HW;
  692. AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl);
  693. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl);
  694. AT_WRITE_REG(hw, REG_GPHY_CTRL, phy_ctrl);
  695. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  696. hw->phy_configured = false; /* re-init PHY when resume */
  697. return 0;
  698. }
  699. phy_ctrl |= GPHY_CTRL_EXT_RESET;
  700. if (wufc & AT_WUFC_MAG) {
  701. mac_ctrl |= MAC_CTRL_RX_EN | MAC_CTRL_BC_EN;
  702. wol_ctrl |= WOL_MAGIC_EN | WOL_MAGIC_PME_EN;
  703. if (hw->nic_type == athr_l2c_b && hw->revision_id == L2CB_V11)
  704. wol_ctrl |= WOL_PATTERN_EN | WOL_PATTERN_PME_EN;
  705. }
  706. if (wufc & AT_WUFC_LNKC) {
  707. wol_ctrl |= WOL_LINK_CHG_EN | WOL_LINK_CHG_PME_EN;
  708. if (atl1c_write_phy_reg(hw, MII_IER, IER_LINK_UP) != 0) {
  709. dev_dbg(&pdev->dev, "%s: write phy MII_IER failed.\n",
  710. atl1c_driver_name);
  711. }
  712. }
  713. /* clear PHY interrupt */
  714. atl1c_read_phy_reg(hw, MII_ISR, &phy_data);
  715. dev_dbg(&pdev->dev, "%s: suspend MAC=%x,MASTER=%x,PHY=0x%x,WOL=%x\n",
  716. atl1c_driver_name, mac_ctrl, master_ctrl, phy_ctrl, wol_ctrl);
  717. AT_WRITE_REG(hw, REG_MASTER_CTRL, master_ctrl);
  718. AT_WRITE_REG(hw, REG_MAC_CTRL, mac_ctrl);
  719. AT_WRITE_REG(hw, REG_GPHY_CTRL, phy_ctrl);
  720. AT_WRITE_REG(hw, REG_WOL_CTRL, wol_ctrl);
  721. return 0;
  722. }
  723. /* configure phy after Link change Event */
  724. void atl1c_post_phy_linkchg(struct atl1c_hw *hw, u16 link_speed)
  725. {
  726. u16 phy_val;
  727. bool adj_thresh = false;
  728. if (hw->nic_type == athr_l2c_b || hw->nic_type == athr_l2c_b2 ||
  729. hw->nic_type == athr_l1d || hw->nic_type == athr_l1d_2)
  730. adj_thresh = true;
  731. if (link_speed != SPEED_0) { /* link up */
  732. /* az with brcm, half-amp */
  733. if (hw->nic_type == athr_l1d_2) {
  734. atl1c_read_phy_ext(hw, MIIEXT_PCS, MIIEXT_CLDCTRL6,
  735. &phy_val);
  736. phy_val = FIELD_GETX(phy_val, CLDCTRL6_CAB_LEN);
  737. phy_val = phy_val > CLDCTRL6_CAB_LEN_SHORT ?
  738. AZ_ANADECT_LONG : AZ_ANADECT_DEF;
  739. atl1c_write_phy_dbg(hw, MIIDBG_AZ_ANADECT, phy_val);
  740. }
  741. /* threshold adjust */
  742. if (adj_thresh && link_speed == SPEED_100 && hw->msi_lnkpatch) {
  743. atl1c_write_phy_dbg(hw, MIIDBG_MSE16DB, L1D_MSE16DB_UP);
  744. atl1c_write_phy_dbg(hw, MIIDBG_SYSMODCTRL,
  745. L1D_SYSMODCTRL_IECHOADJ_DEF);
  746. }
  747. } else { /* link down */
  748. if (adj_thresh && hw->msi_lnkpatch) {
  749. atl1c_write_phy_dbg(hw, MIIDBG_SYSMODCTRL,
  750. SYSMODCTRL_IECHOADJ_DEF);
  751. atl1c_write_phy_dbg(hw, MIIDBG_MSE16DB,
  752. L1D_MSE16DB_DOWN);
  753. }
  754. }
  755. }