atl1_hw.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718
  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. /*
  213. * The following 2 lines are the Attansic originals. Saving for posterity.
  214. * *(u32 *) & eth_addr[2] = LONGSWAP(addr[0]);
  215. * *(u16 *) & eth_addr[0] = SHORTSWAP(*(u16 *) & addr[1]);
  216. */
  217. *(u32 *) & eth_addr[2] = swab32(addr[0]);
  218. *(u16 *) & eth_addr[0] = swab16(*(u16 *) & addr[1]);
  219. if (is_valid_ether_addr(eth_addr)) {
  220. memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
  221. return 0;
  222. }
  223. return 1;
  224. }
  225. /* see if SPI FLAGS exist ? */
  226. addr[0] = addr[1] = 0;
  227. reg = 0;
  228. key_valid = false;
  229. i = 0;
  230. while (1) {
  231. if (atl1_spi_read(hw, i + 0x1f000, &control)) {
  232. if (key_valid) {
  233. if (reg == REG_MAC_STA_ADDR)
  234. addr[0] = control;
  235. else if (reg == (REG_MAC_STA_ADDR + 4))
  236. addr[1] = control;
  237. key_valid = false;
  238. } else if ((control & 0xff) == 0x5A) {
  239. key_valid = true;
  240. reg = (u16) (control >> 16);
  241. } else
  242. break; /* data end */
  243. } else
  244. break; /* read error */
  245. i += 4;
  246. }
  247. /*
  248. * The following 2 lines are the Attansic originals. Saving for posterity.
  249. * *(u32 *) & eth_addr[2] = LONGSWAP(addr[0]);
  250. * *(u16 *) & eth_addr[0] = SHORTSWAP(*(u16 *) & addr[1]);
  251. */
  252. *(u32 *) & eth_addr[2] = swab32(addr[0]);
  253. *(u16 *) & eth_addr[0] = swab16(*(u16 *) & addr[1]);
  254. if (is_valid_ether_addr(eth_addr)) {
  255. memcpy(hw->perm_mac_addr, eth_addr, ETH_ALEN);
  256. return 0;
  257. }
  258. return 1;
  259. }
  260. /*
  261. * Reads the adapter's MAC address from the EEPROM
  262. * hw - Struct containing variables accessed by shared code
  263. */
  264. s32 atl1_read_mac_addr(struct atl1_hw *hw)
  265. {
  266. u16 i;
  267. if (atl1_get_permanent_address(hw))
  268. random_ether_addr(hw->perm_mac_addr);
  269. for (i = 0; i < ETH_ALEN; i++)
  270. hw->mac_addr[i] = hw->perm_mac_addr[i];
  271. return ATL1_SUCCESS;
  272. }
  273. /*
  274. * Hashes an address to determine its location in the multicast table
  275. * hw - Struct containing variables accessed by shared code
  276. * mc_addr - the multicast address to hash
  277. *
  278. * atl1_hash_mc_addr
  279. * purpose
  280. * set hash value for a multicast address
  281. * hash calcu processing :
  282. * 1. calcu 32bit CRC for multicast address
  283. * 2. reverse crc with MSB to LSB
  284. */
  285. u32 atl1_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr)
  286. {
  287. u32 crc32, value = 0;
  288. int i;
  289. crc32 = ether_crc_le(6, mc_addr);
  290. crc32 = ~crc32;
  291. for (i = 0; i < 32; i++)
  292. value |= (((crc32 >> i) & 1) << (31 - i));
  293. return value;
  294. }
  295. /*
  296. * Sets the bit in the multicast table corresponding to the hash value.
  297. * hw - Struct containing variables accessed by shared code
  298. * hash_value - Multicast address hash value
  299. */
  300. void atl1_hash_set(struct atl1_hw *hw, u32 hash_value)
  301. {
  302. u32 hash_bit, hash_reg;
  303. u32 mta;
  304. /*
  305. * The HASH Table is a register array of 2 32-bit registers.
  306. * It is treated like an array of 64 bits. We want to set
  307. * bit BitArray[hash_value]. So we figure out what register
  308. * the bit is in, read it, OR in the new bit, then write
  309. * back the new value. The register is determined by the
  310. * upper 7 bits of the hash value and the bit within that
  311. * register are determined by the lower 5 bits of the value.
  312. */
  313. hash_reg = (hash_value >> 31) & 0x1;
  314. hash_bit = (hash_value >> 26) & 0x1F;
  315. mta = ioread32((hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
  316. mta |= (1 << hash_bit);
  317. iowrite32(mta, (hw->hw_addr + REG_RX_HASH_TABLE) + (hash_reg << 2));
  318. }
  319. /*
  320. * Writes a value to a PHY register
  321. * hw - Struct containing variables accessed by shared code
  322. * reg_addr - address of the PHY register to write
  323. * data - data to write to the PHY
  324. */
  325. s32 atl1_write_phy_reg(struct atl1_hw *hw, u32 reg_addr, u16 phy_data)
  326. {
  327. int i;
  328. u32 val;
  329. val = ((u32) (phy_data & MDIO_DATA_MASK)) << MDIO_DATA_SHIFT |
  330. (reg_addr & MDIO_REG_ADDR_MASK) << MDIO_REG_ADDR_SHIFT |
  331. MDIO_SUP_PREAMBLE |
  332. MDIO_START | MDIO_CLK_25_4 << MDIO_CLK_SEL_SHIFT;
  333. iowrite32(val, hw->hw_addr + REG_MDIO_CTRL);
  334. ioread32(hw->hw_addr + REG_MDIO_CTRL);
  335. for (i = 0; i < MDIO_WAIT_TIMES; i++) {
  336. udelay(2);
  337. val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
  338. if (!(val & (MDIO_START | MDIO_BUSY)))
  339. break;
  340. }
  341. if (!(val & (MDIO_START | MDIO_BUSY)))
  342. return ATL1_SUCCESS;
  343. return ATL1_ERR_PHY;
  344. }
  345. /*
  346. * Make L001's PHY out of Power Saving State (bug)
  347. * hw - Struct containing variables accessed by shared code
  348. * when power on, L001's PHY always on Power saving State
  349. * (Gigabit Link forbidden)
  350. */
  351. static s32 atl1_phy_leave_power_saving(struct atl1_hw *hw)
  352. {
  353. s32 ret;
  354. ret = atl1_write_phy_reg(hw, 29, 0x0029);
  355. if (ret)
  356. return ret;
  357. return atl1_write_phy_reg(hw, 30, 0);
  358. }
  359. /*
  360. *TODO: do something or get rid of this
  361. */
  362. s32 atl1_phy_enter_power_saving(struct atl1_hw *hw)
  363. {
  364. /* s32 ret_val;
  365. * u16 phy_data;
  366. */
  367. /*
  368. ret_val = atl1_write_phy_reg(hw, ...);
  369. ret_val = atl1_write_phy_reg(hw, ...);
  370. ....
  371. */
  372. return ATL1_SUCCESS;
  373. }
  374. /*
  375. * Resets the PHY and make all config validate
  376. * hw - Struct containing variables accessed by shared code
  377. *
  378. * Sets bit 15 and 12 of the MII Control regiser (for F001 bug)
  379. */
  380. static s32 atl1_phy_reset(struct atl1_hw *hw)
  381. {
  382. s32 ret_val;
  383. u16 phy_data;
  384. if (hw->media_type == MEDIA_TYPE_AUTO_SENSOR ||
  385. hw->media_type == MEDIA_TYPE_1000M_FULL)
  386. phy_data = MII_CR_RESET | MII_CR_AUTO_NEG_EN;
  387. else {
  388. switch (hw->media_type) {
  389. case MEDIA_TYPE_100M_FULL:
  390. phy_data =
  391. MII_CR_FULL_DUPLEX | MII_CR_SPEED_100 |
  392. MII_CR_RESET;
  393. break;
  394. case MEDIA_TYPE_100M_HALF:
  395. phy_data = MII_CR_SPEED_100 | MII_CR_RESET;
  396. break;
  397. case MEDIA_TYPE_10M_FULL:
  398. phy_data =
  399. MII_CR_FULL_DUPLEX | MII_CR_SPEED_10 | MII_CR_RESET;
  400. break;
  401. default: /* MEDIA_TYPE_10M_HALF: */
  402. phy_data = MII_CR_SPEED_10 | MII_CR_RESET;
  403. break;
  404. }
  405. }
  406. ret_val = atl1_write_phy_reg(hw, MII_BMCR, phy_data);
  407. if (ret_val) {
  408. u32 val;
  409. int i;
  410. /* pcie serdes link may be down! */
  411. printk(KERN_DEBUG "%s: autoneg caused pcie phy link down\n",
  412. atl1_driver_name);
  413. for (i = 0; i < 25; i++) {
  414. msleep(1);
  415. val = ioread32(hw->hw_addr + REG_MDIO_CTRL);
  416. if (!(val & (MDIO_START | MDIO_BUSY)))
  417. break;
  418. }
  419. if ((val & (MDIO_START | MDIO_BUSY)) != 0) {
  420. printk(KERN_WARNING
  421. "%s: pcie link down at least for 25ms\n",
  422. atl1_driver_name);
  423. return ret_val;
  424. }
  425. }
  426. return ATL1_SUCCESS;
  427. }
  428. /*
  429. * Configures PHY autoneg and flow control advertisement settings
  430. * hw - Struct containing variables accessed by shared code
  431. */
  432. s32 atl1_phy_setup_autoneg_adv(struct atl1_hw *hw)
  433. {
  434. s32 ret_val;
  435. s16 mii_autoneg_adv_reg;
  436. s16 mii_1000t_ctrl_reg;
  437. /* Read the MII Auto-Neg Advertisement Register (Address 4). */
  438. mii_autoneg_adv_reg = MII_AR_DEFAULT_CAP_MASK;
  439. /* Read the MII 1000Base-T Control Register (Address 9). */
  440. mii_1000t_ctrl_reg = MII_AT001_CR_1000T_DEFAULT_CAP_MASK;
  441. /*
  442. * First we clear all the 10/100 mb speed bits in the Auto-Neg
  443. * Advertisement Register (Address 4) and the 1000 mb speed bits in
  444. * the 1000Base-T Control Register (Address 9).
  445. */
  446. mii_autoneg_adv_reg &= ~MII_AR_SPEED_MASK;
  447. mii_1000t_ctrl_reg &= ~MII_AT001_CR_1000T_SPEED_MASK;
  448. /*
  449. * Need to parse media_type and set up
  450. * the appropriate PHY registers.
  451. */
  452. switch (hw->media_type) {
  453. case MEDIA_TYPE_AUTO_SENSOR:
  454. mii_autoneg_adv_reg |= (MII_AR_10T_HD_CAPS |
  455. MII_AR_10T_FD_CAPS |
  456. MII_AR_100TX_HD_CAPS |
  457. MII_AR_100TX_FD_CAPS);
  458. mii_1000t_ctrl_reg |= MII_AT001_CR_1000T_FD_CAPS;
  459. break;
  460. case MEDIA_TYPE_1000M_FULL:
  461. mii_1000t_ctrl_reg |= MII_AT001_CR_1000T_FD_CAPS;
  462. break;
  463. case MEDIA_TYPE_100M_FULL:
  464. mii_autoneg_adv_reg |= MII_AR_100TX_FD_CAPS;
  465. break;
  466. case MEDIA_TYPE_100M_HALF:
  467. mii_autoneg_adv_reg |= MII_AR_100TX_HD_CAPS;
  468. break;
  469. case MEDIA_TYPE_10M_FULL:
  470. mii_autoneg_adv_reg |= MII_AR_10T_FD_CAPS;
  471. break;
  472. default:
  473. mii_autoneg_adv_reg |= MII_AR_10T_HD_CAPS;
  474. break;
  475. }
  476. /* flow control fixed to enable all */
  477. mii_autoneg_adv_reg |= (MII_AR_ASM_DIR | MII_AR_PAUSE);
  478. hw->mii_autoneg_adv_reg = mii_autoneg_adv_reg;
  479. hw->mii_1000t_ctrl_reg = mii_1000t_ctrl_reg;
  480. ret_val = atl1_write_phy_reg(hw, MII_ADVERTISE, mii_autoneg_adv_reg);
  481. if (ret_val)
  482. return ret_val;
  483. ret_val = atl1_write_phy_reg(hw, MII_AT001_CR, mii_1000t_ctrl_reg);
  484. if (ret_val)
  485. return ret_val;
  486. return ATL1_SUCCESS;
  487. }
  488. /*
  489. * Configures link settings.
  490. * hw - Struct containing variables accessed by shared code
  491. * Assumes the hardware has previously been reset and the
  492. * transmitter and receiver are not enabled.
  493. */
  494. static s32 atl1_setup_link(struct atl1_hw *hw)
  495. {
  496. s32 ret_val;
  497. /*
  498. * Options:
  499. * PHY will advertise value(s) parsed from
  500. * autoneg_advertised and fc
  501. * no matter what autoneg is , We will not wait link result.
  502. */
  503. ret_val = atl1_phy_setup_autoneg_adv(hw);
  504. if (ret_val) {
  505. printk(KERN_DEBUG "%s: error setting up autonegotiation\n",
  506. atl1_driver_name);
  507. return ret_val;
  508. }
  509. /* SW.Reset , En-Auto-Neg if needed */
  510. ret_val = atl1_phy_reset(hw);
  511. if (ret_val) {
  512. printk(KERN_DEBUG "%s: error resetting the phy\n",
  513. atl1_driver_name);
  514. return ret_val;
  515. }
  516. hw->phy_configured = true;
  517. return ret_val;
  518. }
  519. static struct atl1_spi_flash_dev flash_table[] = {
  520. /* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SECTOR_ERASE CHIP_ERASE */
  521. {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62},
  522. {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60},
  523. {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7},
  524. };
  525. static void atl1_init_flash_opcode(struct atl1_hw *hw)
  526. {
  527. if (hw->flash_vendor >= sizeof(flash_table) / sizeof(flash_table[0]))
  528. hw->flash_vendor = 0; /* ATMEL */
  529. /* Init OP table */
  530. iowrite8(flash_table[hw->flash_vendor].cmd_program,
  531. hw->hw_addr + REG_SPI_FLASH_OP_PROGRAM);
  532. iowrite8(flash_table[hw->flash_vendor].cmd_sector_erase,
  533. hw->hw_addr + REG_SPI_FLASH_OP_SC_ERASE);
  534. iowrite8(flash_table[hw->flash_vendor].cmd_chip_erase,
  535. hw->hw_addr + REG_SPI_FLASH_OP_CHIP_ERASE);
  536. iowrite8(flash_table[hw->flash_vendor].cmd_rdid,
  537. hw->hw_addr + REG_SPI_FLASH_OP_RDID);
  538. iowrite8(flash_table[hw->flash_vendor].cmd_wren,
  539. hw->hw_addr + REG_SPI_FLASH_OP_WREN);
  540. iowrite8(flash_table[hw->flash_vendor].cmd_rdsr,
  541. hw->hw_addr + REG_SPI_FLASH_OP_RDSR);
  542. iowrite8(flash_table[hw->flash_vendor].cmd_wrsr,
  543. hw->hw_addr + REG_SPI_FLASH_OP_WRSR);
  544. iowrite8(flash_table[hw->flash_vendor].cmd_read,
  545. hw->hw_addr + REG_SPI_FLASH_OP_READ);
  546. }
  547. /*
  548. * Performs basic configuration of the adapter.
  549. * hw - Struct containing variables accessed by shared code
  550. * Assumes that the controller has previously been reset and is in a
  551. * post-reset uninitialized state. Initializes multicast table,
  552. * and Calls routines to setup link
  553. * Leaves the transmit and receive units disabled and uninitialized.
  554. */
  555. s32 atl1_init_hw(struct atl1_hw *hw)
  556. {
  557. u32 ret_val = 0;
  558. /* Zero out the Multicast HASH table */
  559. iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
  560. /* clear the old settings from the multicast hash table */
  561. iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
  562. atl1_init_flash_opcode(hw);
  563. if (!hw->phy_configured) {
  564. /* enable GPHY LinkChange Interrrupt */
  565. ret_val = atl1_write_phy_reg(hw, 18, 0xC00);
  566. if (ret_val)
  567. return ret_val;
  568. /* make PHY out of power-saving state */
  569. ret_val = atl1_phy_leave_power_saving(hw);
  570. if (ret_val)
  571. return ret_val;
  572. /* Call a subroutine to configure the link */
  573. ret_val = atl1_setup_link(hw);
  574. }
  575. return ret_val;
  576. }
  577. /*
  578. * Detects the current speed and duplex settings of the hardware.
  579. * hw - Struct containing variables accessed by shared code
  580. * speed - Speed of the connection
  581. * duplex - Duplex setting of the connection
  582. */
  583. s32 atl1_get_speed_and_duplex(struct atl1_hw *hw, u16 *speed, u16 *duplex)
  584. {
  585. s32 ret_val;
  586. u16 phy_data;
  587. /* ; --- Read PHY Specific Status Register (17) */
  588. ret_val = atl1_read_phy_reg(hw, MII_AT001_PSSR, &phy_data);
  589. if (ret_val)
  590. return ret_val;
  591. if (!(phy_data & MII_AT001_PSSR_SPD_DPLX_RESOLVED))
  592. return ATL1_ERR_PHY_RES;
  593. switch (phy_data & MII_AT001_PSSR_SPEED) {
  594. case MII_AT001_PSSR_1000MBS:
  595. *speed = SPEED_1000;
  596. break;
  597. case MII_AT001_PSSR_100MBS:
  598. *speed = SPEED_100;
  599. break;
  600. case MII_AT001_PSSR_10MBS:
  601. *speed = SPEED_10;
  602. break;
  603. default:
  604. printk(KERN_DEBUG "%s: error getting speed\n",
  605. atl1_driver_name);
  606. return ATL1_ERR_PHY_SPEED;
  607. break;
  608. }
  609. if (phy_data & MII_AT001_PSSR_DPLX)
  610. *duplex = FULL_DUPLEX;
  611. else
  612. *duplex = HALF_DUPLEX;
  613. return ATL1_SUCCESS;
  614. }
  615. void atl1_set_mac_addr(struct atl1_hw *hw)
  616. {
  617. u32 value;
  618. /*
  619. * 00-0B-6A-F6-00-DC
  620. * 0: 6AF600DC 1: 000B
  621. * low dword
  622. */
  623. value = (((u32) hw->mac_addr[2]) << 24) |
  624. (((u32) hw->mac_addr[3]) << 16) |
  625. (((u32) hw->mac_addr[4]) << 8) | (((u32) hw->mac_addr[5]));
  626. iowrite32(value, hw->hw_addr + REG_MAC_STA_ADDR);
  627. /* high dword */
  628. value = (((u32) hw->mac_addr[0]) << 8) | (((u32) hw->mac_addr[1]));
  629. iowrite32(value, (hw->hw_addr + REG_MAC_STA_ADDR) + (1 << 2));
  630. }