atl1c_hw.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663
  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)
  42. {
  43. u32 value;
  44. /*
  45. * 00-0B-6A-F6-00-DC
  46. * 0: 6AF600DC 1: 000B
  47. * low dword
  48. */
  49. value = (((u32)hw->mac_addr[2]) << 24) |
  50. (((u32)hw->mac_addr[3]) << 16) |
  51. (((u32)hw->mac_addr[4]) << 8) |
  52. (((u32)hw->mac_addr[5])) ;
  53. AT_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 0, value);
  54. /* hight dword */
  55. value = (((u32)hw->mac_addr[0]) << 8) |
  56. (((u32)hw->mac_addr[1])) ;
  57. AT_WRITE_REG_ARRAY(hw, REG_MAC_STA_ADDR, 1, value);
  58. }
  59. /*
  60. * atl1c_get_permanent_address
  61. * return 0 if get valid mac address,
  62. */
  63. static int atl1c_get_permanent_address(struct atl1c_hw *hw)
  64. {
  65. u32 addr[2];
  66. u32 i;
  67. u32 otp_ctrl_data;
  68. u32 twsi_ctrl_data;
  69. u32 ltssm_ctrl_data;
  70. u32 wol_data;
  71. u8 eth_addr[ETH_ALEN];
  72. u16 phy_data;
  73. bool raise_vol = false;
  74. /* init */
  75. addr[0] = addr[1] = 0;
  76. AT_READ_REG(hw, REG_OTP_CTRL, &otp_ctrl_data);
  77. if (atl1c_check_eeprom_exist(hw)) {
  78. if (hw->nic_type == athr_l1c || hw->nic_type == athr_l2c) {
  79. /* Enable OTP CLK */
  80. if (!(otp_ctrl_data & OTP_CTRL_CLK_EN)) {
  81. otp_ctrl_data |= OTP_CTRL_CLK_EN;
  82. AT_WRITE_REG(hw, REG_OTP_CTRL, otp_ctrl_data);
  83. AT_WRITE_FLUSH(hw);
  84. msleep(1);
  85. }
  86. }
  87. if (hw->nic_type == athr_l2c_b ||
  88. hw->nic_type == athr_l2c_b2 ||
  89. hw->nic_type == athr_l1d) {
  90. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x00);
  91. if (atl1c_read_phy_reg(hw, MII_DBG_DATA, &phy_data))
  92. goto out;
  93. phy_data &= 0xFF7F;
  94. atl1c_write_phy_reg(hw, MII_DBG_DATA, phy_data);
  95. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x3B);
  96. if (atl1c_read_phy_reg(hw, MII_DBG_DATA, &phy_data))
  97. goto out;
  98. phy_data |= 0x8;
  99. atl1c_write_phy_reg(hw, MII_DBG_DATA, phy_data);
  100. udelay(20);
  101. raise_vol = true;
  102. }
  103. /* close open bit of ReadOnly*/
  104. AT_READ_REG(hw, REG_LTSSM_ID_CTRL, &ltssm_ctrl_data);
  105. ltssm_ctrl_data &= ~LTSSM_ID_EN_WRO;
  106. AT_WRITE_REG(hw, REG_LTSSM_ID_CTRL, ltssm_ctrl_data);
  107. /* clear any WOL settings */
  108. AT_WRITE_REG(hw, REG_WOL_CTRL, 0);
  109. AT_READ_REG(hw, REG_WOL_CTRL, &wol_data);
  110. AT_READ_REG(hw, REG_TWSI_CTRL, &twsi_ctrl_data);
  111. twsi_ctrl_data |= TWSI_CTRL_SW_LDSTART;
  112. AT_WRITE_REG(hw, REG_TWSI_CTRL, twsi_ctrl_data);
  113. for (i = 0; i < AT_TWSI_EEPROM_TIMEOUT; i++) {
  114. msleep(10);
  115. AT_READ_REG(hw, REG_TWSI_CTRL, &twsi_ctrl_data);
  116. if ((twsi_ctrl_data & TWSI_CTRL_SW_LDSTART) == 0)
  117. break;
  118. }
  119. if (i >= AT_TWSI_EEPROM_TIMEOUT)
  120. return -1;
  121. }
  122. /* Disable OTP_CLK */
  123. if ((hw->nic_type == athr_l1c || hw->nic_type == athr_l2c)) {
  124. otp_ctrl_data &= ~OTP_CTRL_CLK_EN;
  125. AT_WRITE_REG(hw, REG_OTP_CTRL, otp_ctrl_data);
  126. msleep(1);
  127. }
  128. if (raise_vol) {
  129. if (hw->nic_type == athr_l2c_b ||
  130. hw->nic_type == athr_l2c_b2 ||
  131. hw->nic_type == athr_l1d ||
  132. hw->nic_type == athr_l1d_2) {
  133. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x00);
  134. if (atl1c_read_phy_reg(hw, MII_DBG_DATA, &phy_data))
  135. goto out;
  136. phy_data |= 0x80;
  137. atl1c_write_phy_reg(hw, MII_DBG_DATA, phy_data);
  138. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x3B);
  139. if (atl1c_read_phy_reg(hw, MII_DBG_DATA, &phy_data))
  140. goto out;
  141. phy_data &= 0xFFF7;
  142. atl1c_write_phy_reg(hw, MII_DBG_DATA, phy_data);
  143. udelay(20);
  144. }
  145. }
  146. /* maybe MAC-address is from BIOS */
  147. AT_READ_REG(hw, REG_MAC_STA_ADDR, &addr[0]);
  148. AT_READ_REG(hw, REG_MAC_STA_ADDR + 4, &addr[1]);
  149. *(u32 *) &eth_addr[2] = swab32(addr[0]);
  150. *(u16 *) &eth_addr[0] = swab16(*(u16 *)&addr[1]);
  151. if (is_valid_ether_addr(eth_addr)) {
  152. memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
  153. return 0;
  154. }
  155. out:
  156. return -1;
  157. }
  158. bool atl1c_read_eeprom(struct atl1c_hw *hw, u32 offset, u32 *p_value)
  159. {
  160. int i;
  161. int ret = false;
  162. u32 otp_ctrl_data;
  163. u32 control;
  164. u32 data;
  165. if (offset & 3)
  166. return ret; /* address do not align */
  167. AT_READ_REG(hw, REG_OTP_CTRL, &otp_ctrl_data);
  168. if (!(otp_ctrl_data & OTP_CTRL_CLK_EN))
  169. AT_WRITE_REG(hw, REG_OTP_CTRL,
  170. (otp_ctrl_data | OTP_CTRL_CLK_EN));
  171. AT_WRITE_REG(hw, REG_EEPROM_DATA_LO, 0);
  172. control = (offset & EEPROM_CTRL_ADDR_MASK) << EEPROM_CTRL_ADDR_SHIFT;
  173. AT_WRITE_REG(hw, REG_EEPROM_CTRL, control);
  174. for (i = 0; i < 10; i++) {
  175. udelay(100);
  176. AT_READ_REG(hw, REG_EEPROM_CTRL, &control);
  177. if (control & EEPROM_CTRL_RW)
  178. break;
  179. }
  180. if (control & EEPROM_CTRL_RW) {
  181. AT_READ_REG(hw, REG_EEPROM_CTRL, &data);
  182. AT_READ_REG(hw, REG_EEPROM_DATA_LO, p_value);
  183. data = data & 0xFFFF;
  184. *p_value = swab32((data << 16) | (*p_value >> 16));
  185. ret = true;
  186. }
  187. if (!(otp_ctrl_data & OTP_CTRL_CLK_EN))
  188. AT_WRITE_REG(hw, REG_OTP_CTRL, otp_ctrl_data);
  189. return ret;
  190. }
  191. /*
  192. * Reads the adapter's MAC address from the EEPROM
  193. *
  194. * hw - Struct containing variables accessed by shared code
  195. */
  196. int atl1c_read_mac_addr(struct atl1c_hw *hw)
  197. {
  198. int err = 0;
  199. err = atl1c_get_permanent_address(hw);
  200. if (err)
  201. random_ether_addr(hw->perm_mac_addr);
  202. memcpy(hw->mac_addr, hw->perm_mac_addr, sizeof(hw->perm_mac_addr));
  203. return 0;
  204. }
  205. /*
  206. * atl1c_hash_mc_addr
  207. * purpose
  208. * set hash value for a multicast address
  209. * hash calcu processing :
  210. * 1. calcu 32bit CRC for multicast address
  211. * 2. reverse crc with MSB to LSB
  212. */
  213. u32 atl1c_hash_mc_addr(struct atl1c_hw *hw, u8 *mc_addr)
  214. {
  215. u32 crc32;
  216. u32 value = 0;
  217. int i;
  218. crc32 = ether_crc_le(6, mc_addr);
  219. for (i = 0; i < 32; i++)
  220. value |= (((crc32 >> i) & 1) << (31 - i));
  221. return value;
  222. }
  223. /*
  224. * Sets the bit in the multicast table corresponding to the hash value.
  225. * hw - Struct containing variables accessed by shared code
  226. * hash_value - Multicast address hash value
  227. */
  228. void atl1c_hash_set(struct atl1c_hw *hw, u32 hash_value)
  229. {
  230. u32 hash_bit, hash_reg;
  231. u32 mta;
  232. /*
  233. * The HASH Table is a register array of 2 32-bit registers.
  234. * It is treated like an array of 64 bits. We want to set
  235. * bit BitArray[hash_value]. So we figure out what register
  236. * the bit is in, read it, OR in the new bit, then write
  237. * back the new value. The register is determined by the
  238. * upper bit of the hash value and the bit within that
  239. * register are determined by the lower 5 bits of the value.
  240. */
  241. hash_reg = (hash_value >> 31) & 0x1;
  242. hash_bit = (hash_value >> 26) & 0x1F;
  243. mta = AT_READ_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg);
  244. mta |= (1 << hash_bit);
  245. AT_WRITE_REG_ARRAY(hw, REG_RX_HASH_TABLE, hash_reg, mta);
  246. }
  247. /*
  248. * Reads the value from a PHY register
  249. * hw - Struct containing variables accessed by shared code
  250. * reg_addr - address of the PHY register to read
  251. */
  252. int atl1c_read_phy_reg(struct atl1c_hw *hw, u16 reg_addr, u16 *phy_data)
  253. {
  254. u32 val;
  255. int i;
  256. val = ((u32)(reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
  257. MDIO_START | MDIO_SUP_PREAMBLE | MDIO_RW |
  258. MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
  259. AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
  260. for (i = 0; i < MDIO_WAIT_TIMES; i++) {
  261. udelay(2);
  262. AT_READ_REG(hw, REG_MDIO_CTRL, &val);
  263. if (!(val & (MDIO_START | MDIO_BUSY)))
  264. break;
  265. }
  266. if (!(val & (MDIO_START | MDIO_BUSY))) {
  267. *phy_data = (u16)val;
  268. return 0;
  269. }
  270. return -1;
  271. }
  272. /*
  273. * Writes a value to a PHY register
  274. * hw - Struct containing variables accessed by shared code
  275. * reg_addr - address of the PHY register to write
  276. * data - data to write to the PHY
  277. */
  278. int atl1c_write_phy_reg(struct atl1c_hw *hw, u32 reg_addr, u16 phy_data)
  279. {
  280. int i;
  281. u32 val;
  282. val = ((u32)(phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
  283. (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
  284. MDIO_SUP_PREAMBLE | MDIO_START |
  285. MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
  286. AT_WRITE_REG(hw, REG_MDIO_CTRL, val);
  287. for (i = 0; i < MDIO_WAIT_TIMES; i++) {
  288. udelay(2);
  289. AT_READ_REG(hw, REG_MDIO_CTRL, &val);
  290. if (!(val & (MDIO_START | MDIO_BUSY)))
  291. break;
  292. }
  293. if (!(val & (MDIO_START | MDIO_BUSY)))
  294. return 0;
  295. return -1;
  296. }
  297. /*
  298. * Configures PHY autoneg and flow control advertisement settings
  299. *
  300. * hw - Struct containing variables accessed by shared code
  301. */
  302. static int atl1c_phy_setup_adv(struct atl1c_hw *hw)
  303. {
  304. u16 mii_adv_data = ADVERTISE_DEFAULT_CAP & ~ADVERTISE_ALL;
  305. u16 mii_giga_ctrl_data = GIGA_CR_1000T_DEFAULT_CAP &
  306. ~GIGA_CR_1000T_SPEED_MASK;
  307. if (hw->autoneg_advertised & ADVERTISED_10baseT_Half)
  308. mii_adv_data |= ADVERTISE_10HALF;
  309. if (hw->autoneg_advertised & ADVERTISED_10baseT_Full)
  310. mii_adv_data |= ADVERTISE_10FULL;
  311. if (hw->autoneg_advertised & ADVERTISED_100baseT_Half)
  312. mii_adv_data |= ADVERTISE_100HALF;
  313. if (hw->autoneg_advertised & ADVERTISED_100baseT_Full)
  314. mii_adv_data |= ADVERTISE_100FULL;
  315. if (hw->autoneg_advertised & ADVERTISED_Autoneg)
  316. mii_adv_data |= ADVERTISE_10HALF | ADVERTISE_10FULL |
  317. ADVERTISE_100HALF | ADVERTISE_100FULL;
  318. if (hw->link_cap_flags & ATL1C_LINK_CAP_1000M) {
  319. if (hw->autoneg_advertised & ADVERTISED_1000baseT_Half)
  320. mii_giga_ctrl_data |= ADVERTISE_1000HALF;
  321. if (hw->autoneg_advertised & ADVERTISED_1000baseT_Full)
  322. mii_giga_ctrl_data |= ADVERTISE_1000FULL;
  323. if (hw->autoneg_advertised & ADVERTISED_Autoneg)
  324. mii_giga_ctrl_data |= ADVERTISE_1000HALF |
  325. ADVERTISE_1000FULL;
  326. }
  327. if (atl1c_write_phy_reg(hw, MII_ADVERTISE, mii_adv_data) != 0 ||
  328. atl1c_write_phy_reg(hw, MII_CTRL1000, mii_giga_ctrl_data) != 0)
  329. return -1;
  330. return 0;
  331. }
  332. void atl1c_phy_disable(struct atl1c_hw *hw)
  333. {
  334. AT_WRITE_REGW(hw, REG_GPHY_CTRL,
  335. GPHY_CTRL_PW_WOL_DIS | GPHY_CTRL_EXT_RESET);
  336. }
  337. static void atl1c_phy_magic_data(struct atl1c_hw *hw)
  338. {
  339. u16 data;
  340. data = ANA_LOOP_SEL_10BT | ANA_EN_MASK_TB | ANA_EN_10BT_IDLE |
  341. ((1 & ANA_INTERVAL_SEL_TIMER_MASK) <<
  342. ANA_INTERVAL_SEL_TIMER_SHIFT);
  343. atl1c_write_phy_reg(hw, MII_DBG_ADDR, MII_ANA_CTRL_18);
  344. atl1c_write_phy_reg(hw, MII_DBG_DATA, data);
  345. data = (2 & ANA_SERDES_CDR_BW_MASK) | ANA_MS_PAD_DBG |
  346. ANA_SERDES_EN_DEEM | ANA_SERDES_SEL_HSP | ANA_SERDES_EN_PLL |
  347. ANA_SERDES_EN_LCKDT;
  348. atl1c_write_phy_reg(hw, MII_DBG_ADDR, MII_ANA_CTRL_5);
  349. atl1c_write_phy_reg(hw, MII_DBG_DATA, data);
  350. data = (44 & ANA_LONG_CABLE_TH_100_MASK) |
  351. ((33 & ANA_SHORT_CABLE_TH_100_MASK) <<
  352. ANA_SHORT_CABLE_TH_100_SHIFT) | ANA_BP_BAD_LINK_ACCUM |
  353. ANA_BP_SMALL_BW;
  354. atl1c_write_phy_reg(hw, MII_DBG_ADDR, MII_ANA_CTRL_54);
  355. atl1c_write_phy_reg(hw, MII_DBG_DATA, data);
  356. data = (11 & ANA_IECHO_ADJ_MASK) | ((11 & ANA_IECHO_ADJ_MASK) <<
  357. ANA_IECHO_ADJ_2_SHIFT) | ((8 & ANA_IECHO_ADJ_MASK) <<
  358. ANA_IECHO_ADJ_1_SHIFT) | ((8 & ANA_IECHO_ADJ_MASK) <<
  359. ANA_IECHO_ADJ_0_SHIFT);
  360. atl1c_write_phy_reg(hw, MII_DBG_ADDR, MII_ANA_CTRL_4);
  361. atl1c_write_phy_reg(hw, MII_DBG_DATA, data);
  362. data = ANA_RESTART_CAL | ((7 & ANA_MANUL_SWICH_ON_MASK) <<
  363. ANA_MANUL_SWICH_ON_SHIFT) | ANA_MAN_ENABLE |
  364. ANA_SEL_HSP | ANA_EN_HB | ANA_OEN_125M;
  365. atl1c_write_phy_reg(hw, MII_DBG_ADDR, MII_ANA_CTRL_0);
  366. atl1c_write_phy_reg(hw, MII_DBG_DATA, data);
  367. if (hw->ctrl_flags & ATL1C_HIB_DISABLE) {
  368. atl1c_write_phy_reg(hw, MII_DBG_ADDR, MII_ANA_CTRL_41);
  369. if (atl1c_read_phy_reg(hw, MII_DBG_DATA, &data) != 0)
  370. return;
  371. data &= ~ANA_TOP_PS_EN;
  372. atl1c_write_phy_reg(hw, MII_DBG_DATA, data);
  373. atl1c_write_phy_reg(hw, MII_DBG_ADDR, MII_ANA_CTRL_11);
  374. if (atl1c_read_phy_reg(hw, MII_DBG_DATA, &data) != 0)
  375. return;
  376. data &= ~ANA_PS_HIB_EN;
  377. atl1c_write_phy_reg(hw, MII_DBG_DATA, data);
  378. }
  379. }
  380. int atl1c_phy_reset(struct atl1c_hw *hw)
  381. {
  382. struct atl1c_adapter *adapter = hw->adapter;
  383. struct pci_dev *pdev = adapter->pdev;
  384. u16 phy_data;
  385. u32 phy_ctrl_data = GPHY_CTRL_DEFAULT;
  386. u32 mii_ier_data = IER_LINK_UP | IER_LINK_DOWN;
  387. int err;
  388. if (hw->ctrl_flags & ATL1C_HIB_DISABLE)
  389. phy_ctrl_data &= ~GPHY_CTRL_HIB_EN;
  390. AT_WRITE_REG(hw, REG_GPHY_CTRL, phy_ctrl_data);
  391. AT_WRITE_FLUSH(hw);
  392. msleep(40);
  393. phy_ctrl_data |= GPHY_CTRL_EXT_RESET;
  394. AT_WRITE_REG(hw, REG_GPHY_CTRL, phy_ctrl_data);
  395. AT_WRITE_FLUSH(hw);
  396. msleep(10);
  397. if (hw->nic_type == athr_l2c_b) {
  398. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x0A);
  399. atl1c_read_phy_reg(hw, MII_DBG_DATA, &phy_data);
  400. atl1c_write_phy_reg(hw, MII_DBG_DATA, phy_data & 0xDFFF);
  401. }
  402. if (hw->nic_type == athr_l2c_b ||
  403. hw->nic_type == athr_l2c_b2 ||
  404. hw->nic_type == athr_l1d ||
  405. hw->nic_type == athr_l1d_2) {
  406. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x3B);
  407. atl1c_read_phy_reg(hw, MII_DBG_DATA, &phy_data);
  408. atl1c_write_phy_reg(hw, MII_DBG_DATA, phy_data & 0xFFF7);
  409. msleep(20);
  410. }
  411. if (hw->nic_type == athr_l1d) {
  412. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x29);
  413. atl1c_write_phy_reg(hw, MII_DBG_DATA, 0x929D);
  414. }
  415. if (hw->nic_type == athr_l1c || hw->nic_type == athr_l2c_b2
  416. || hw->nic_type == athr_l2c) {
  417. atl1c_write_phy_reg(hw, MII_DBG_ADDR, 0x29);
  418. atl1c_write_phy_reg(hw, MII_DBG_DATA, 0xB6DD);
  419. }
  420. err = atl1c_write_phy_reg(hw, MII_IER, mii_ier_data);
  421. if (err) {
  422. if (netif_msg_hw(adapter))
  423. dev_err(&pdev->dev,
  424. "Error enable PHY linkChange Interrupt\n");
  425. return err;
  426. }
  427. if (!(hw->ctrl_flags & ATL1C_FPGA_VERSION))
  428. atl1c_phy_magic_data(hw);
  429. return 0;
  430. }
  431. int atl1c_phy_init(struct atl1c_hw *hw)
  432. {
  433. struct atl1c_adapter *adapter = (struct atl1c_adapter *)hw->adapter;
  434. struct pci_dev *pdev = adapter->pdev;
  435. int ret_val;
  436. u16 mii_bmcr_data = BMCR_RESET;
  437. if ((atl1c_read_phy_reg(hw, MII_PHYSID1, &hw->phy_id1) != 0) ||
  438. (atl1c_read_phy_reg(hw, MII_PHYSID2, &hw->phy_id2) != 0)) {
  439. dev_err(&pdev->dev, "Error get phy ID\n");
  440. return -1;
  441. }
  442. switch (hw->media_type) {
  443. case MEDIA_TYPE_AUTO_SENSOR:
  444. ret_val = atl1c_phy_setup_adv(hw);
  445. if (ret_val) {
  446. if (netif_msg_link(adapter))
  447. dev_err(&pdev->dev,
  448. "Error Setting up Auto-Negotiation\n");
  449. return ret_val;
  450. }
  451. mii_bmcr_data |= BMCR_ANENABLE | BMCR_ANRESTART;
  452. break;
  453. case MEDIA_TYPE_100M_FULL:
  454. mii_bmcr_data |= BMCR_SPEED100 | BMCR_FULLDPLX;
  455. break;
  456. case MEDIA_TYPE_100M_HALF:
  457. mii_bmcr_data |= BMCR_SPEED100;
  458. break;
  459. case MEDIA_TYPE_10M_FULL:
  460. mii_bmcr_data |= BMCR_FULLDPLX;
  461. break;
  462. case MEDIA_TYPE_10M_HALF:
  463. break;
  464. default:
  465. if (netif_msg_link(adapter))
  466. dev_err(&pdev->dev, "Wrong Media type %d\n",
  467. hw->media_type);
  468. return -1;
  469. break;
  470. }
  471. ret_val = atl1c_write_phy_reg(hw, MII_BMCR, mii_bmcr_data);
  472. if (ret_val)
  473. return ret_val;
  474. hw->phy_configured = true;
  475. return 0;
  476. }
  477. /*
  478. * Detects the current speed and duplex settings of the hardware.
  479. *
  480. * hw - Struct containing variables accessed by shared code
  481. * speed - Speed of the connection
  482. * duplex - Duplex setting of the connection
  483. */
  484. int atl1c_get_speed_and_duplex(struct atl1c_hw *hw, u16 *speed, u16 *duplex)
  485. {
  486. int err;
  487. u16 phy_data;
  488. /* Read PHY Specific Status Register (17) */
  489. err = atl1c_read_phy_reg(hw, MII_GIGA_PSSR, &phy_data);
  490. if (err)
  491. return err;
  492. if (!(phy_data & GIGA_PSSR_SPD_DPLX_RESOLVED))
  493. return -1;
  494. switch (phy_data & GIGA_PSSR_SPEED) {
  495. case GIGA_PSSR_1000MBS:
  496. *speed = SPEED_1000;
  497. break;
  498. case GIGA_PSSR_100MBS:
  499. *speed = SPEED_100;
  500. break;
  501. case GIGA_PSSR_10MBS:
  502. *speed = SPEED_10;
  503. break;
  504. default:
  505. return -1;
  506. break;
  507. }
  508. if (phy_data & GIGA_PSSR_DPLX)
  509. *duplex = FULL_DUPLEX;
  510. else
  511. *duplex = HALF_DUPLEX;
  512. return 0;
  513. }
  514. int atl1c_phy_power_saving(struct atl1c_hw *hw)
  515. {
  516. struct atl1c_adapter *adapter = (struct atl1c_adapter *)hw->adapter;
  517. struct pci_dev *pdev = adapter->pdev;
  518. int ret = 0;
  519. u16 autoneg_advertised = ADVERTISED_10baseT_Half;
  520. u16 save_autoneg_advertised;
  521. u16 phy_data;
  522. u16 mii_lpa_data;
  523. u16 speed = SPEED_0;
  524. u16 duplex = FULL_DUPLEX;
  525. int i;
  526. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  527. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  528. if (phy_data & BMSR_LSTATUS) {
  529. atl1c_read_phy_reg(hw, MII_LPA, &mii_lpa_data);
  530. if (mii_lpa_data & LPA_10FULL)
  531. autoneg_advertised = ADVERTISED_10baseT_Full;
  532. else if (mii_lpa_data & LPA_10HALF)
  533. autoneg_advertised = ADVERTISED_10baseT_Half;
  534. else if (mii_lpa_data & LPA_100HALF)
  535. autoneg_advertised = ADVERTISED_100baseT_Half;
  536. else if (mii_lpa_data & LPA_100FULL)
  537. autoneg_advertised = ADVERTISED_100baseT_Full;
  538. save_autoneg_advertised = hw->autoneg_advertised;
  539. hw->phy_configured = false;
  540. hw->autoneg_advertised = autoneg_advertised;
  541. if (atl1c_restart_autoneg(hw) != 0) {
  542. dev_dbg(&pdev->dev, "phy autoneg failed\n");
  543. ret = -1;
  544. }
  545. hw->autoneg_advertised = save_autoneg_advertised;
  546. if (mii_lpa_data) {
  547. for (i = 0; i < AT_SUSPEND_LINK_TIMEOUT; i++) {
  548. mdelay(100);
  549. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  550. atl1c_read_phy_reg(hw, MII_BMSR, &phy_data);
  551. if (phy_data & BMSR_LSTATUS) {
  552. if (atl1c_get_speed_and_duplex(hw, &speed,
  553. &duplex) != 0)
  554. dev_dbg(&pdev->dev,
  555. "get speed and duplex failed\n");
  556. break;
  557. }
  558. }
  559. }
  560. } else {
  561. speed = SPEED_10;
  562. duplex = HALF_DUPLEX;
  563. }
  564. adapter->link_speed = speed;
  565. adapter->link_duplex = duplex;
  566. return ret;
  567. }
  568. int atl1c_restart_autoneg(struct atl1c_hw *hw)
  569. {
  570. int err = 0;
  571. u16 mii_bmcr_data = BMCR_RESET;
  572. err = atl1c_phy_setup_adv(hw);
  573. if (err)
  574. return err;
  575. mii_bmcr_data |= BMCR_ANENABLE | BMCR_ANRESTART;
  576. return atl1c_write_phy_reg(hw, MII_BMCR, mii_bmcr_data);
  577. }