atl1_hw.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723
  1. /*
  2. * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
  3. * Copyright(c) 2006 Chris Snook <csnook@redhat.com>
  4. * Copyright(c) 2006 Jay Cliburn <jcliburn@gmail.com>
  5. *
  6. * Derived from Intel e1000 driver
  7. * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
  8. *
  9. * This program is free software; you can redistribute it and/or modify it
  10. * under the terms of the GNU General Public License as published by the Free
  11. * Software Foundation; either version 2 of the License, or (at your option)
  12. * any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful, but WITHOUT
  15. * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  16. * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
  17. * more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * this program; if not, write to the Free Software Foundation, Inc., 59
  21. * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
  22. */
  23. #include <linux/types.h>
  24. #include <linux/pci.h>
  25. #include <linux/delay.h>
  26. #include <linux/if_vlan.h>
  27. #include <linux/etherdevice.h>
  28. #include <linux/crc32.h>
  29. #include <asm/byteorder.h>
  30. #include "atl1.h"
  31. /*
  32. * Reset the transmit and receive units; mask and clear all interrupts.
  33. * hw - Struct containing variables accessed by shared code
  34. * return : ATL1_SUCCESS or idle status (if error)
  35. */
  36. s32 atl1_reset_hw(struct atl1_hw *hw)
  37. {
  38. u32 icr;
  39. int i;
  40. /*
  41. * Clear Interrupt mask to stop board from generating
  42. * interrupts & Clear any pending interrupt events
  43. */
  44. /*
  45. * iowrite32(0, hw->hw_addr + REG_IMR);
  46. * iowrite32(0xffffffff, hw->hw_addr + REG_ISR);
  47. */
  48. /*
  49. * Issue Soft Reset to the MAC. This will reset the chip's
  50. * transmit, receive, DMA. It will not effect
  51. * the current PCI configuration. The global reset bit is self-
  52. * clearing, and should clear within a microsecond.
  53. */
  54. iowrite32(MASTER_CTRL_SOFT_RST, hw->hw_addr + REG_MASTER_CTRL);
  55. ioread32(hw->hw_addr + REG_MASTER_CTRL);
  56. iowrite16(1, hw->hw_addr + REG_GPHY_ENABLE);
  57. ioread16(hw->hw_addr + REG_GPHY_ENABLE);
  58. msleep(1); /* delay about 1ms */
  59. /* Wait at least 10ms for All module to be Idle */
  60. for (i = 0; i < 10; i++) {
  61. icr = ioread32(hw->hw_addr + REG_IDLE_STATUS);
  62. if (!icr)
  63. break;
  64. msleep(1); /* delay 1 ms */
  65. cpu_relax(); /* FIXME: is this still the right way to do this? */
  66. }
  67. if (icr) {
  68. printk (KERN_DEBUG "icr = %x\n", icr);
  69. return icr;
  70. }
  71. return ATL1_SUCCESS;
  72. }
  73. /* function about EEPROM
  74. *
  75. * check_eeprom_exist
  76. * return 0 if eeprom exist
  77. */
  78. static int atl1_check_eeprom_exist(struct atl1_hw *hw)
  79. {
  80. u32 value;
  81. value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
  82. if (value & SPI_FLASH_CTRL_EN_VPD) {
  83. value &= ~SPI_FLASH_CTRL_EN_VPD;
  84. iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
  85. }
  86. value = ioread16(hw->hw_addr + REG_PCIE_CAP_LIST);
  87. return ((value & 0xFF00) == 0x6C00) ? 0 : 1;
  88. }
  89. static bool atl1_read_eeprom(struct atl1_hw *hw, u32 offset, u32 *p_value)
  90. {
  91. int i;
  92. u32 control;
  93. if (offset & 3)
  94. return false; /* address do not align */
  95. iowrite32(0, hw->hw_addr + REG_VPD_DATA);
  96. control = (offset & VPD_CAP_VPD_ADDR_MASK) << VPD_CAP_VPD_ADDR_SHIFT;
  97. iowrite32(control, hw->hw_addr + REG_VPD_CAP);
  98. ioread32(hw->hw_addr + REG_VPD_CAP);
  99. for (i = 0; i < 10; i++) {
  100. msleep(2);
  101. control = ioread32(hw->hw_addr + REG_VPD_CAP);
  102. if (control & VPD_CAP_VPD_FLAG)
  103. break;
  104. }
  105. if (control & VPD_CAP_VPD_FLAG) {
  106. *p_value = ioread32(hw->hw_addr + REG_VPD_DATA);
  107. return true;
  108. }
  109. return false; /* timeout */
  110. }
  111. /*
  112. * Reads the value from a PHY register
  113. * hw - Struct containing variables accessed by shared code
  114. * reg_addr - address of the PHY register to read
  115. */
  116. s32 atl1_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data)
  117. {
  118. u32 val;
  119. int i;
  120. val = ((u32) (reg_addr & MDIO_REG_ADDR_MASK)) << MDIO_REG_ADDR_SHIFT |
  121. MDIO_START | MDIO_SUP_PREAMBLE | MDIO_RW | MDIO_CLK_25_4 <<
  122. MDIO_CLK_SEL_SHIFT;
  123. iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
  124. ioread32(hw->hw_addr + REG_MDIO_CTRL);
  125. for (i = 0; i < MDIO_WAIT_TIMES; i++) {
  126. udelay(2);
  127. val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
  128. if (!(val & (MDIO_START | MDIO_BUSY)))
  129. break;
  130. }
  131. if (!(val & (MDIO_START | MDIO_BUSY))) {
  132. *phy_data = (u16) val;
  133. return ATL1_SUCCESS;
  134. }
  135. return ATL1_ERR_PHY;
  136. }
  137. #define CUSTOM_SPI_CS_SETUP 2
  138. #define CUSTOM_SPI_CLK_HI 2
  139. #define CUSTOM_SPI_CLK_LO 2
  140. #define CUSTOM_SPI_CS_HOLD 2
  141. #define CUSTOM_SPI_CS_HI 3
  142. static bool atl1_spi_read(struct atl1_hw *hw, u32 addr, u32 *buf)
  143. {
  144. int i;
  145. u32 value;
  146. iowrite32(0, hw->hw_addr + REG_SPI_DATA);
  147. iowrite32(addr, hw->hw_addr + REG_SPI_ADDR);
  148. value = SPI_FLASH_CTRL_WAIT_READY |
  149. (CUSTOM_SPI_CS_SETUP & SPI_FLASH_CTRL_CS_SETUP_MASK) <<
  150. SPI_FLASH_CTRL_CS_SETUP_SHIFT | (CUSTOM_SPI_CLK_HI &
  151. SPI_FLASH_CTRL_CLK_HI_MASK) <<
  152. SPI_FLASH_CTRL_CLK_HI_SHIFT | (CUSTOM_SPI_CLK_LO &
  153. SPI_FLASH_CTRL_CLK_LO_MASK) <<
  154. SPI_FLASH_CTRL_CLK_LO_SHIFT | (CUSTOM_SPI_CS_HOLD &
  155. SPI_FLASH_CTRL_CS_HOLD_MASK) <<
  156. SPI_FLASH_CTRL_CS_HOLD_SHIFT | (CUSTOM_SPI_CS_HI &
  157. SPI_FLASH_CTRL_CS_HI_MASK) <<
  158. SPI_FLASH_CTRL_CS_HI_SHIFT | (1 & SPI_FLASH_CTRL_INS_MASK) <<
  159. SPI_FLASH_CTRL_INS_SHIFT;
  160. iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
  161. value |= SPI_FLASH_CTRL_START;
  162. iowrite32(value, hw->hw_addr + REG_SPI_FLASH_CTRL);
  163. ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
  164. for (i = 0; i < 10; i++) {
  165. msleep(1); /* 1ms */
  166. value = ioread32(hw->hw_addr + REG_SPI_FLASH_CTRL);
  167. if (!(value & SPI_FLASH_CTRL_START))
  168. break;
  169. }
  170. if (value & SPI_FLASH_CTRL_START)
  171. return false;
  172. *buf = ioread32(hw->hw_addr + REG_SPI_DATA);
  173. return true;
  174. }
  175. /*
  176. * get_permanent_address
  177. * return 0 if get valid mac address,
  178. */
  179. static int atl1_get_permanent_address(struct atl1_hw *hw)
  180. {
  181. u32 addr[2];
  182. u32 i, control;
  183. u16 reg;
  184. u8 eth_addr[ETH_ALEN];
  185. bool key_valid;
  186. if (is_valid_ether_addr(hw->perm_mac_addr))
  187. return 0;
  188. /* init */
  189. addr[0] = addr[1] = 0;
  190. if (!atl1_check_eeprom_exist(hw)) { /* eeprom exist */
  191. reg = 0;
  192. key_valid = false;
  193. /* Read out all EEPROM content */
  194. i = 0;
  195. while (1) {
  196. if (atl1_read_eeprom(hw, i + 0x100, &control)) {
  197. if (key_valid) {
  198. if (reg == REG_MAC_STA_ADDR)
  199. addr[0] = control;
  200. else if (reg == (REG_MAC_STA_ADDR + 4))
  201. addr[1] = control;
  202. key_valid = false;
  203. } else if ((control & 0xff) == 0x5A) {
  204. key_valid = true;
  205. reg = (u16) (control >> 16);
  206. } else
  207. break; /* assume data end while encount an invalid KEYWORD */
  208. } else
  209. break; /* read error */
  210. i += 4;
  211. }
  212. *(u32 *) &eth_addr[2] = swab32(addr[0]);
  213. *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
  214. if (is_valid_ether_addr(eth_addr)) {
  215. memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
  216. return 0;
  217. }
  218. return 1;
  219. }
  220. /* see if SPI FLAGS exist ? */
  221. addr[0] = addr[1] = 0;
  222. reg = 0;
  223. key_valid = false;
  224. i = 0;
  225. while (1) {
  226. if (atl1_spi_read(hw, i + 0x1f000, &control)) {
  227. if (key_valid) {
  228. if (reg == REG_MAC_STA_ADDR)
  229. addr[0] = control;
  230. else if (reg == (REG_MAC_STA_ADDR + 4))
  231. addr[1] = control;
  232. key_valid = false;
  233. } else if ((control & 0xff) == 0x5A) {
  234. key_valid = true;
  235. reg = (u16) (control >> 16);
  236. } else
  237. break; /* data end */
  238. } else
  239. break; /* read error */
  240. i += 4;
  241. }
  242. *(u32 *) &eth_addr[2] = swab32(addr[0]);
  243. *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
  244. if (is_valid_ether_addr(eth_addr)) {
  245. memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
  246. return 0;
  247. }
  248. /*
  249. * On some motherboards, the MAC address is written by the
  250. * BIOS directly to the MAC register during POST, and is
  251. * not stored in eeprom. If all else thus far has failed
  252. * to fetch the permanent MAC address, try reading it directly.
  253. */
  254. addr[0] = ioread32(hw->hw_addr + REG_MAC_STA_ADDR);
  255. addr[1] = ioread16(hw->hw_addr + (REG_MAC_STA_ADDR + 4));
  256. *(u32 *) &eth_addr[2] = swab32(addr[0]);
  257. *(u16 *) &eth_addr[0] = swab16(*(u16 *) &addr[1]);
  258. if (is_valid_ether_addr(eth_addr)) {
  259. memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
  260. return 0;
  261. }
  262. return 1;
  263. }
  264. /*
  265. * Reads the adapter's MAC address from the EEPROM
  266. * hw - Struct containing variables accessed by shared code
  267. */
  268. s32 atl1_read_mac_addr(struct atl1_hw *hw)
  269. {
  270. u16 i;
  271. if (atl1_get_permanent_address(hw))
  272. random_ether_addr(hw->perm_mac_addr);
  273. for (i = 0; i < ETH_ALEN; i++)
  274. hw->mac_addr[i] = hw->perm_mac_addr[i];
  275. return ATL1_SUCCESS;
  276. }
  277. /*
  278. * Hashes an address to determine its location in the multicast table
  279. * hw - Struct containing variables accessed by shared code
  280. * mc_addr - the multicast address to hash
  281. *
  282. * atl1_hash_mc_addr
  283. * purpose
  284. * set hash value for a multicast address
  285. * hash calcu processing :
  286. * 1. calcu 32bit CRC for multicast address
  287. * 2. reverse crc with MSB to LSB
  288. */
  289. u32 atl1_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr)
  290. {
  291. u32 crc32, value = 0;
  292. int i;
  293. crc32 = ether_crc_le(6, mc_addr);
  294. crc32 = ~crc32;
  295. for (i = 0; i < 32; i++)
  296. value |= (((crc32 >> i) & 1) << (31 - i));
  297. return value;
  298. }
  299. /*
  300. * Sets the bit in the multicast table corresponding to the hash value.
  301. * hw - Struct containing variables accessed by shared code
  302. * hash_value - Multicast address hash value
  303. */
  304. void atl1_hash_set(struct atl1_hw *hw, u32 hash_value)
  305. {
  306. u32 hash_bit, hash_reg;
  307. u32 mta;
  308. /*
  309. * The HASH Table is a register array of 2 32-bit registers.
  310. * It is treated like an array of 64 bits. We want to set
  311. * bit BitArray[hash_value]. So we figure out what register
  312. * the bit is in, read it, OR in the new bit, then write
  313. * back the new value. The register is determined by the
  314. * upper 7 bits of the hash value and the bit within that
  315. * register are determined by the lower 5 bits of the value.
  316. */
  317. hash_reg = (hash_value >> 31) & 0x1;
  318. hash_bit = (hash_value >> 26) & 0x1F;
  319. mta = ioread32((hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
  320. mta |= (1 << hash_bit);
  321. iowrite32(mta, (hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
  322. }
  323. /*
  324. * Writes a value to a PHY register
  325. * hw - Struct containing variables accessed by shared code
  326. * reg_addr - address of the PHY register to write
  327. * data - data to write to the PHY
  328. */
  329. s32 atl1_write_phy_reg(struct atl1_hw *hw, u32 reg_addr, u16 phy_data)
  330. {
  331. int i;
  332. u32 val;
  333. val = ((u32) (phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
  334. (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
  335. MDIO_SUP_PREAMBLE |
  336. MDIO_START | MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
  337. iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
  338. ioread32(hw->hw_addr + REG_MDIO_CTRL);
  339. for (i = 0; i < MDIO_WAIT_TIMES; i++) {
  340. udelay(2);
  341. val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
  342. if (!(val & (MDIO_START | MDIO_BUSY)))
  343. break;
  344. }
  345. if (!(val & (MDIO_START | MDIO_BUSY)))
  346. return ATL1_SUCCESS;
  347. return ATL1_ERR_PHY;
  348. }
  349. /*
  350. * Make L001's PHY out of Power Saving State (bug)
  351. * hw - Struct containing variables accessed by shared code
  352. * when power on, L001's PHY always on Power saving State
  353. * (Gigabit Link forbidden)
  354. */
  355. static s32 atl1_phy_leave_power_saving(struct atl1_hw *hw)
  356. {
  357. s32 ret;
  358. ret = atl1_write_phy_reg(hw, 29, 0x0029);
  359. if (ret)
  360. return ret;
  361. return atl1_write_phy_reg(hw, 30, 0);
  362. }
  363. /*
  364. *TODO: do something or get rid of this
  365. */
  366. s32 atl1_phy_enter_power_saving(struct atl1_hw *hw)
  367. {
  368. /* s32 ret_val;
  369. * u16 phy_data;
  370. */
  371. /*
  372. ret_val = atl1_write_phy_reg(hw, ...);
  373. ret_val = atl1_write_phy_reg(hw, ...);
  374. ....
  375. */
  376. return ATL1_SUCCESS;
  377. }
  378. /*
  379. * Resets the PHY and make all config validate
  380. * hw - Struct containing variables accessed by shared code
  381. *
  382. * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
  383. */
  384. static s32 atl1_phy_reset(struct atl1_hw *hw)
  385. {
  386. s32 ret_val;
  387. u16 phy_data;
  388. if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
  389. hw->media_type == MEDIA_TYPE_1000M_FULL)
  390. phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
  391. else {
  392. switch (hw->media_type) {
  393. case MEDIA_TYPE_100M_FULL:
  394. phy_data =
  395. MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
  396. MII_CR_RESET;
  397. break;
  398. case MEDIA_TYPE_100M_HALF:
  399. phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
  400. break;
  401. case MEDIA_TYPE_10M_FULL:
  402. phy_data =
  403. MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
  404. break;
  405. default: /* MEDIA_TYPE_10M_HALF: */
  406. phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
  407. break;
  408. }
  409. }
  410. ret_val = atl1_write_phy_reg(hw, MII_BMCR, phy_data);
  411. if (ret_val) {
  412. u32 val;
  413. int i;
  414. /* pcie serdes link may be down! */
  415. printk(KERN_DEBUG "%s: autoneg caused pcie phy link down\n",
  416. atl1_driver_name);
  417. for (i = 0; i < 25; i++) {
  418. msleep(1);
  419. val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
  420. if (!(val & (MDIO_START | MDIO_BUSY)))
  421. break;
  422. }
  423. if ((val & (MDIO_START | MDIO_BUSY)) != 0) {
  424. printk(KERN_WARNING
  425. "%s: pcie link down at least for 25ms\n",
  426. atl1_driver_name);
  427. return ret_val;
  428. }
  429. }
  430. return ATL1_SUCCESS;
  431. }
  432. /*
  433. * Configures PHY autoneg and flow control advertisement settings
  434. * hw - Struct containing variables accessed by shared code
  435. */
  436. s32 atl1_phy_setup_autoneg_adv(struct atl1_hw *hw)
  437. {
  438. s32 ret_val;
  439. s16 mii_autoneg_adv_reg;
  440. s16 mii_1000t_ctrl_reg;
  441. /* Read the MII Auto-Neg Advertisement Register (Address 4). */
  442. mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
  443. /* Read the MII 1000Base-T Control Register (Address 9). */
  444. mii_1000t_ctrl_reg = MII_AT001_CR_1000T_DEFAULT_CAP_MASK;
  445. /*
  446. * First we clear all the 10/100 mb speed bits in the Auto-Neg
  447. * Advertisement Register (Address 4) and the 1000 mb speed bits in
  448. * the 1000Base-T Control Register (Address 9).
  449. */
  450. mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
  451. mii_1000t_ctrl_reg &= ~MII_AT001_CR_1000T_SPEED_MASK;
  452. /*
  453. * Need to parse media_type and set up
  454. * the appropriate PHY registers.
  455. */
  456. switch (hw->media_type) {
  457. case MEDIA_TYPE_AUTO_SENSOR:
  458. mii_autoneg_adv_reg |= (MII_AR_10T_HD_CAPS |
  459. MII_AR_10T_FD_CAPS |
  460. MII_AR_100TX_HD_CAPS |
  461. MII_AR_100TX_FD_CAPS);
  462. mii_1000t_ctrl_reg |= MII_AT001_CR_1000T_FD_CAPS;
  463. break;
  464. case MEDIA_TYPE_1000M_FULL:
  465. mii_1000t_ctrl_reg |= MII_AT001_CR_1000T_FD_CAPS;
  466. break;
  467. case MEDIA_TYPE_100M_FULL:
  468. mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
  469. break;
  470. case MEDIA_TYPE_100M_HALF:
  471. mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
  472. break;
  473. case MEDIA_TYPE_10M_FULL:
  474. mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
  475. break;
  476. default:
  477. mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
  478. break;
  479. }
  480. /* flow control fixed to enable all */
  481. mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
  482. hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
  483. hw->mii_1000t_ctrl_reg = mii_1000t_ctrl_reg;
  484. ret_val = atl1_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
  485. if (ret_val)
  486. return ret_val;
  487. ret_val = atl1_write_phy_reg(hw, MII_AT001_CR, mii_1000t_ctrl_reg);
  488. if (ret_val)
  489. return ret_val;
  490. return ATL1_SUCCESS;
  491. }
  492. /*
  493. * Configures link settings.
  494. * hw - Struct containing variables accessed by shared code
  495. * Assumes the hardware has previously been reset and the
  496. * transmitter and receiver are not enabled.
  497. */
  498. static s32 atl1_setup_link(struct atl1_hw *hw)
  499. {
  500. s32 ret_val;
  501. /*
  502. * Options:
  503. * PHY will advertise value(s) parsed from
  504. * autoneg_advertised and fc
  505. * no matter what autoneg is , We will not wait link result.
  506. */
  507. ret_val = atl1_phy_setup_autoneg_adv(hw);
  508. if (ret_val) {
  509. printk(KERN_DEBUG "%s: error setting up autonegotiation\n",
  510. atl1_driver_name);
  511. return ret_val;
  512. }
  513. /* SW.Reset , En-Auto-Neg if needed */
  514. ret_val = atl1_phy_reset(hw);
  515. if (ret_val) {
  516. printk(KERN_DEBUG "%s: error resetting the phy\n",
  517. atl1_driver_name);
  518. return ret_val;
  519. }
  520. hw->phy_configured = true;
  521. return ret_val;
  522. }
  523. static struct atl1_spi_flash_dev flash_table[] = {
  524. /* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SECTOR_ERASE CHIP_ERASE */
  525. {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62},
  526. {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60},
  527. {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7},
  528. };
  529. static void atl1_init_flash_opcode(struct atl1_hw *hw)
  530. {
  531. if (hw->flash_vendor >= sizeof(flash_table) / sizeof(flash_table[0]))
  532. hw->flash_vendor = 0; /* ATMEL */
  533. /* Init OP table */
  534. iowrite8(flash_table[hw->flash_vendor].cmd_program,
  535. hw->hw_addr + REG_SPI_FLASH_OP_PROGRAM);
  536. iowrite8(flash_table[hw->flash_vendor].cmd_sector_erase,
  537. hw->hw_addr + REG_SPI_FLASH_OP_SC_ERASE);
  538. iowrite8(flash_table[hw->flash_vendor].cmd_chip_erase,
  539. hw->hw_addr + REG_SPI_FLASH_OP_CHIP_ERASE);
  540. iowrite8(flash_table[hw->flash_vendor].cmd_rdid,
  541. hw->hw_addr + REG_SPI_FLASH_OP_RDID);
  542. iowrite8(flash_table[hw->flash_vendor].cmd_wren,
  543. hw->hw_addr + REG_SPI_FLASH_OP_WREN);
  544. iowrite8(flash_table[hw->flash_vendor].cmd_rdsr,
  545. hw->hw_addr + REG_SPI_FLASH_OP_RDSR);
  546. iowrite8(flash_table[hw->flash_vendor].cmd_wrsr,
  547. hw->hw_addr + REG_SPI_FLASH_OP_WRSR);
  548. iowrite8(flash_table[hw->flash_vendor].cmd_read,
  549. hw->hw_addr + REG_SPI_FLASH_OP_READ);
  550. }
  551. /*
  552. * Performs basic configuration of the adapter.
  553. * hw - Struct containing variables accessed by shared code
  554. * Assumes that the controller has previously been reset and is in a
  555. * post-reset uninitialized state. Initializes multicast table,
  556. * and Calls routines to setup link
  557. * Leaves the transmit and receive units disabled and uninitialized.
  558. */
  559. s32 atl1_init_hw(struct atl1_hw *hw)
  560. {
  561. u32 ret_val = 0;
  562. /* Zero out the Multicast HASH table */
  563. iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
  564. /* clear the old settings from the multicast hash table */
  565. iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
  566. atl1_init_flash_opcode(hw);
  567. if (!hw->phy_configured) {
  568. /* enable GPHY LinkChange Interrrupt */
  569. ret_val = atl1_write_phy_reg(hw, 18, 0xC00);
  570. if (ret_val)
  571. return ret_val;
  572. /* make PHY out of power-saving state */
  573. ret_val = atl1_phy_leave_power_saving(hw);
  574. if (ret_val)
  575. return ret_val;
  576. /* Call a subroutine to configure the link */
  577. ret_val = atl1_setup_link(hw);
  578. }
  579. return ret_val;
  580. }
  581. /*
  582. * Detects the current speed and duplex settings of the hardware.
  583. * hw - Struct containing variables accessed by shared code
  584. * speed - Speed of the connection
  585. * duplex - Duplex setting of the connection
  586. */
  587. s32 atl1_get_speed_and_duplex(struct atl1_hw *hw, u16 *speed, u16 *duplex)
  588. {
  589. s32 ret_val;
  590. u16 phy_data;
  591. /* ; --- Read PHY Specific Status Register (17) */
  592. ret_val = atl1_read_phy_reg(hw, MII_AT001_PSSR, &phy_data);
  593. if (ret_val)
  594. return ret_val;
  595. if (!(phy_data & MII_AT001_PSSR_SPD_DPLX_RESOLVED))
  596. return ATL1_ERR_PHY_RES;
  597. switch (phy_data & MII_AT001_PSSR_SPEED) {
  598. case MII_AT001_PSSR_1000MBS:
  599. *speed = SPEED_1000;
  600. break;
  601. case MII_AT001_PSSR_100MBS:
  602. *speed = SPEED_100;
  603. break;
  604. case MII_AT001_PSSR_10MBS:
  605. *speed = SPEED_10;
  606. break;
  607. default:
  608. printk(KERN_DEBUG "%s: error getting speed\n",
  609. atl1_driver_name);
  610. return ATL1_ERR_PHY_SPEED;
  611. break;
  612. }
  613. if (phy_data & MII_AT001_PSSR_DPLX)
  614. *duplex = FULL_DUPLEX;
  615. else
  616. *duplex = HALF_DUPLEX;
  617. return ATL1_SUCCESS;
  618. }
  619. void atl1_set_mac_addr(struct atl1_hw *hw)
  620. {
  621. u32 value;
  622. /*
  623. * 00-0B-6A-F6-00-DC
  624. * 0: 6AF600DC 1: 000B
  625. * low dword
  626. */
  627. value = (((u32) hw->mac_addr[2]) << 24) |
  628. (((u32) hw->mac_addr[3]) << 16) |
  629. (((u32) hw->mac_addr[4]) << 8) | (((u32) hw->mac_addr[5]));
  630. iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
  631. /* high dword */
  632. value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
  633. iowrite32(value, (hw->hw_addr + REG_MAC_STA_ADDR) + (1 << 2));
  634. }