1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright(c) 2007 Atheros Corporation. All rights reserved.
5 * Derived from Intel e1000 driver
6 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
9 #include <linux/delay.h>
10 #include <linux/mii.h>
11 #include <linux/crc32.h>
17 * return 0 if eeprom exist
19 int atl1e_check_eeprom_exist(struct atl1e_hw
*hw
)
23 value
= AT_READ_REG(hw
, REG_SPI_FLASH_CTRL
);
24 if (value
& SPI_FLASH_CTRL_EN_VPD
) {
25 value
&= ~SPI_FLASH_CTRL_EN_VPD
;
26 AT_WRITE_REG(hw
, REG_SPI_FLASH_CTRL
, value
);
28 value
= AT_READ_REGW(hw
, REG_PCIE_CAP_LIST
);
29 return ((value
& 0xFF00) == 0x6C00) ? 0 : 1;
32 void atl1e_hw_set_mac_addr(struct atl1e_hw
*hw
)
40 value
= (((u32
)hw
->mac_addr
[2]) << 24) |
41 (((u32
)hw
->mac_addr
[3]) << 16) |
42 (((u32
)hw
->mac_addr
[4]) << 8) |
43 (((u32
)hw
->mac_addr
[5])) ;
44 AT_WRITE_REG_ARRAY(hw
, REG_MAC_STA_ADDR
, 0, value
);
46 value
= (((u32
)hw
->mac_addr
[0]) << 8) |
47 (((u32
)hw
->mac_addr
[1])) ;
48 AT_WRITE_REG_ARRAY(hw
, REG_MAC_STA_ADDR
, 1, value
);
52 * atl1e_get_permanent_address
53 * return 0 if get valid mac address,
55 static int atl1e_get_permanent_address(struct atl1e_hw
*hw
)
60 u8 eth_addr
[ETH_ALEN
];
62 if (is_valid_ether_addr(hw
->perm_mac_addr
))
66 addr
[0] = addr
[1] = 0;
68 if (!atl1e_check_eeprom_exist(hw
)) {
70 twsi_ctrl_data
= AT_READ_REG(hw
, REG_TWSI_CTRL
);
71 twsi_ctrl_data
|= TWSI_CTRL_SW_LDSTART
;
72 AT_WRITE_REG(hw
, REG_TWSI_CTRL
, twsi_ctrl_data
);
73 for (i
= 0; i
< AT_TWSI_EEPROM_TIMEOUT
; i
++) {
75 twsi_ctrl_data
= AT_READ_REG(hw
, REG_TWSI_CTRL
);
76 if ((twsi_ctrl_data
& TWSI_CTRL_SW_LDSTART
) == 0)
79 if (i
>= AT_TWSI_EEPROM_TIMEOUT
)
80 return AT_ERR_TIMEOUT
;
83 /* maybe MAC-address is from BIOS */
84 addr
[0] = AT_READ_REG(hw
, REG_MAC_STA_ADDR
);
85 addr
[1] = AT_READ_REG(hw
, REG_MAC_STA_ADDR
+ 4);
86 *(u32
*) ð_addr
[2] = swab32(addr
[0]);
87 *(u16
*) ð_addr
[0] = swab16(*(u16
*)&addr
[1]);
89 if (is_valid_ether_addr(eth_addr
)) {
90 memcpy(hw
->perm_mac_addr
, eth_addr
, ETH_ALEN
);
97 bool atl1e_write_eeprom(struct atl1e_hw
*hw
, u32 offset
, u32 value
)
102 bool atl1e_read_eeprom(struct atl1e_hw
*hw
, u32 offset
, u32
*p_value
)
108 return false; /* address do not align */
110 AT_WRITE_REG(hw
, REG_VPD_DATA
, 0);
111 control
= (offset
& VPD_CAP_VPD_ADDR_MASK
) << VPD_CAP_VPD_ADDR_SHIFT
;
112 AT_WRITE_REG(hw
, REG_VPD_CAP
, control
);
114 for (i
= 0; i
< 10; i
++) {
116 control
= AT_READ_REG(hw
, REG_VPD_CAP
);
117 if (control
& VPD_CAP_VPD_FLAG
)
120 if (control
& VPD_CAP_VPD_FLAG
) {
121 *p_value
= AT_READ_REG(hw
, REG_VPD_DATA
);
124 return false; /* timeout */
127 void atl1e_force_ps(struct atl1e_hw
*hw
)
129 AT_WRITE_REGW(hw
, REG_GPHY_CTRL
,
130 GPHY_CTRL_PW_WOL_DIS
| GPHY_CTRL_EXT_RESET
);
134 * Reads the adapter's MAC address from the EEPROM
136 * hw - Struct containing variables accessed by shared code
138 int atl1e_read_mac_addr(struct atl1e_hw
*hw
)
142 err
= atl1e_get_permanent_address(hw
);
144 return AT_ERR_EEPROM
;
145 memcpy(hw
->mac_addr
, hw
->perm_mac_addr
, sizeof(hw
->perm_mac_addr
));
152 * set hash value for a multicast address
154 u32
atl1e_hash_mc_addr(struct atl1e_hw
*hw
, u8
*mc_addr
)
160 crc32
= ether_crc_le(6, mc_addr
);
161 for (i
= 0; i
< 32; i
++)
162 value
|= (((crc32
>> i
) & 1) << (31 - i
));
168 * Sets the bit in the multicast table corresponding to the hash value.
169 * hw - Struct containing variables accessed by shared code
170 * hash_value - Multicast address hash value
172 void atl1e_hash_set(struct atl1e_hw
*hw
, u32 hash_value
)
174 u32 hash_bit
, hash_reg
;
178 * The HASH Table is a register array of 2 32-bit registers.
179 * It is treated like an array of 64 bits. We want to set
180 * bit BitArray[hash_value]. So we figure out what register
181 * the bit is in, read it, OR in the new bit, then write
182 * back the new value. The register is determined by the
183 * upper 7 bits of the hash value and the bit within that
184 * register are determined by the lower 5 bits of the value.
186 hash_reg
= (hash_value
>> 31) & 0x1;
187 hash_bit
= (hash_value
>> 26) & 0x1F;
189 mta
= AT_READ_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, hash_reg
);
191 mta
|= (1 << hash_bit
);
193 AT_WRITE_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, hash_reg
, mta
);
196 * Reads the value from a PHY register
197 * hw - Struct containing variables accessed by shared code
198 * reg_addr - address of the PHY register to read
200 int atl1e_read_phy_reg(struct atl1e_hw
*hw
, u16 reg_addr
, u16
*phy_data
)
205 val
= ((u32
)(reg_addr
& MDIO_REG_ADDR_MASK
)) << MDIO_REG_ADDR_SHIFT
|
206 MDIO_START
| MDIO_SUP_PREAMBLE
| MDIO_RW
|
207 MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHIFT
;
209 AT_WRITE_REG(hw
, REG_MDIO_CTRL
, val
);
213 for (i
= 0; i
< MDIO_WAIT_TIMES
; i
++) {
215 val
= AT_READ_REG(hw
, REG_MDIO_CTRL
);
216 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
220 if (!(val
& (MDIO_START
| MDIO_BUSY
))) {
221 *phy_data
= (u16
)val
;
229 * Writes a value to a PHY register
230 * hw - Struct containing variables accessed by shared code
231 * reg_addr - address of the PHY register to write
232 * data - data to write to the PHY
234 int atl1e_write_phy_reg(struct atl1e_hw
*hw
, u32 reg_addr
, u16 phy_data
)
239 val
= ((u32
)(phy_data
& MDIO_DATA_MASK
)) << MDIO_DATA_SHIFT
|
240 (reg_addr
&MDIO_REG_ADDR_MASK
) << MDIO_REG_ADDR_SHIFT
|
243 MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHIFT
;
245 AT_WRITE_REG(hw
, REG_MDIO_CTRL
, val
);
248 for (i
= 0; i
< MDIO_WAIT_TIMES
; i
++) {
250 val
= AT_READ_REG(hw
, REG_MDIO_CTRL
);
251 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
256 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
263 * atl1e_init_pcie - init PCIE module
265 static void atl1e_init_pcie(struct atl1e_hw
*hw
)
268 /* comment 2lines below to save more power when sususpend
269 value = LTSSM_TEST_MODE_DEF;
270 AT_WRITE_REG(hw, REG_LTSSM_TEST_MODE, value);
273 /* pcie flow control mode change */
274 value
= AT_READ_REG(hw
, 0x1008);
276 AT_WRITE_REG(hw
, 0x1008, value
);
279 * Configures PHY autoneg and flow control advertisement settings
281 * hw - Struct containing variables accessed by shared code
283 static int atl1e_phy_setup_autoneg_adv(struct atl1e_hw
*hw
)
286 u16 mii_autoneg_adv_reg
;
287 u16 mii_1000t_ctrl_reg
;
289 if (0 != hw
->mii_autoneg_adv_reg
)
291 /* Read the MII Auto-Neg Advertisement Register (Address 4/9). */
292 mii_autoneg_adv_reg
= MII_AR_DEFAULT_CAP_MASK
;
293 mii_1000t_ctrl_reg
= MII_AT001_CR_1000T_DEFAULT_CAP_MASK
;
296 * Need to parse autoneg_advertised and set up
297 * the appropriate PHY registers. First we will parse for
298 * autoneg_advertised software override. Since we can advertise
299 * a plethora of combinations, we need to check each bit
304 * First we clear all the 10/100 mb speed bits in the Auto-Neg
305 * Advertisement Register (Address 4) and the 1000 mb speed bits in
306 * the 1000Base-T control Register (Address 9).
308 mii_autoneg_adv_reg
&= ~ADVERTISE_ALL
;
309 mii_1000t_ctrl_reg
&= ~MII_AT001_CR_1000T_SPEED_MASK
;
312 * Need to parse MediaType and setup the
313 * appropriate PHY registers.
315 switch (hw
->media_type
) {
316 case MEDIA_TYPE_AUTO_SENSOR
:
317 mii_autoneg_adv_reg
|= ADVERTISE_ALL
;
318 hw
->autoneg_advertised
= ADVERTISE_ALL
;
319 if (hw
->nic_type
== athr_l1e
) {
320 mii_1000t_ctrl_reg
|= ADVERTISE_1000FULL
;
321 hw
->autoneg_advertised
|= ADVERTISE_1000_FULL
;
325 case MEDIA_TYPE_100M_FULL
:
326 mii_autoneg_adv_reg
|= ADVERTISE_100FULL
;
327 hw
->autoneg_advertised
= ADVERTISE_100_FULL
;
330 case MEDIA_TYPE_100M_HALF
:
331 mii_autoneg_adv_reg
|= ADVERTISE_100_HALF
;
332 hw
->autoneg_advertised
= ADVERTISE_100_HALF
;
335 case MEDIA_TYPE_10M_FULL
:
336 mii_autoneg_adv_reg
|= ADVERTISE_10_FULL
;
337 hw
->autoneg_advertised
= ADVERTISE_10_FULL
;
341 mii_autoneg_adv_reg
|= ADVERTISE_10_HALF
;
342 hw
->autoneg_advertised
= ADVERTISE_10_HALF
;
346 /* flow control fixed to enable all */
347 mii_autoneg_adv_reg
|= (ADVERTISE_PAUSE_ASYM
| ADVERTISE_PAUSE_CAP
);
349 hw
->mii_autoneg_adv_reg
= mii_autoneg_adv_reg
;
350 hw
->mii_1000t_ctrl_reg
= mii_1000t_ctrl_reg
;
352 ret_val
= atl1e_write_phy_reg(hw
, MII_ADVERTISE
, mii_autoneg_adv_reg
);
356 if (hw
->nic_type
== athr_l1e
|| hw
->nic_type
== athr_l2e_revA
) {
357 ret_val
= atl1e_write_phy_reg(hw
, MII_CTRL1000
,
368 * Resets the PHY and make all config validate
370 * hw - Struct containing variables accessed by shared code
372 * Sets bit 15 and 12 of the MII control regiser (for F001 bug)
374 int atl1e_phy_commit(struct atl1e_hw
*hw
)
376 struct atl1e_adapter
*adapter
= hw
->adapter
;
380 phy_data
= BMCR_RESET
| BMCR_ANENABLE
| BMCR_ANRESTART
;
382 ret_val
= atl1e_write_phy_reg(hw
, MII_BMCR
, phy_data
);
386 /**************************************
387 * pcie serdes link may be down !
388 **************************************/
389 for (i
= 0; i
< 25; i
++) {
391 val
= AT_READ_REG(hw
, REG_MDIO_CTRL
);
392 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
396 if (0 != (val
& (MDIO_START
| MDIO_BUSY
))) {
397 netdev_err(adapter
->netdev
,
398 "pcie linkdown at least for 25ms\n");
402 netdev_err(adapter
->netdev
, "pcie linkup after %d ms\n", i
);
407 int atl1e_phy_init(struct atl1e_hw
*hw
)
409 struct atl1e_adapter
*adapter
= hw
->adapter
;
413 if (hw
->phy_configured
) {
414 if (hw
->re_autoneg
) {
415 hw
->re_autoneg
= false;
416 return atl1e_restart_autoneg(hw
);
421 /* RESET GPHY Core */
422 AT_WRITE_REGW(hw
, REG_GPHY_CTRL
, GPHY_CTRL_DEFAULT
);
424 AT_WRITE_REGW(hw
, REG_GPHY_CTRL
, GPHY_CTRL_DEFAULT
|
425 GPHY_CTRL_EXT_RESET
);
429 /* p1. eable hibernation mode */
430 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_ADDR
, 0xB);
433 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_DATA
, 0xBC00);
436 /* p2. set Class A/B for all modes */
437 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_ADDR
, 0);
441 /* remove Class AB */
442 /* phy_val = hw->emi_ca ? 0x02ef : 0x02df; */
443 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_DATA
, phy_val
);
447 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_ADDR
, 0x12);
450 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_DATA
, 0x4C04);
453 /* p4. 1000T power */
454 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_ADDR
, 0x4);
457 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_DATA
, 0x8BBB);
461 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_ADDR
, 0x5);
464 ret_val
= atl1e_write_phy_reg(hw
, MII_DBG_DATA
, 0x2C46);
470 /*Enable PHY LinkChange Interrupt */
471 ret_val
= atl1e_write_phy_reg(hw
, MII_INT_CTRL
, 0xC00);
473 netdev_err(adapter
->netdev
,
474 "Error enable PHY linkChange Interrupt\n");
477 /* setup AutoNeg parameters */
478 ret_val
= atl1e_phy_setup_autoneg_adv(hw
);
480 netdev_err(adapter
->netdev
,
481 "Error Setting up Auto-Negotiation\n");
484 /* SW.Reset & En-Auto-Neg to restart Auto-Neg*/
485 netdev_dbg(adapter
->netdev
, "Restarting Auto-Negotiation\n");
486 ret_val
= atl1e_phy_commit(hw
);
488 netdev_err(adapter
->netdev
, "Error resetting the phy\n");
492 hw
->phy_configured
= true;
498 * Reset the transmit and receive units; mask and clear all interrupts.
499 * hw - Struct containing variables accessed by shared code
500 * return : 0 or idle status (if error)
502 int atl1e_reset_hw(struct atl1e_hw
*hw
)
504 struct atl1e_adapter
*adapter
= hw
->adapter
;
505 struct pci_dev
*pdev
= adapter
->pdev
;
507 u32 idle_status_data
= 0;
508 u16 pci_cfg_cmd_word
= 0;
511 /* Workaround for PCI problem when BIOS sets MMRBC incorrectly. */
512 pci_read_config_word(pdev
, PCI_REG_COMMAND
, &pci_cfg_cmd_word
);
513 if ((pci_cfg_cmd_word
& (CMD_IO_SPACE
|
514 CMD_MEMORY_SPACE
| CMD_BUS_MASTER
))
515 != (CMD_IO_SPACE
| CMD_MEMORY_SPACE
| CMD_BUS_MASTER
)) {
516 pci_cfg_cmd_word
|= (CMD_IO_SPACE
|
517 CMD_MEMORY_SPACE
| CMD_BUS_MASTER
);
518 pci_write_config_word(pdev
, PCI_REG_COMMAND
, pci_cfg_cmd_word
);
522 * Issue Soft Reset to the MAC. This will reset the chip's
523 * transmit, receive, DMA. It will not effect
524 * the current PCI configuration. The global reset bit is self-
525 * clearing, and should clear within a microsecond.
527 AT_WRITE_REG(hw
, REG_MASTER_CTRL
,
528 MASTER_CTRL_LED_MODE
| MASTER_CTRL_SOFT_RST
);
532 /* Wait at least 10ms for All module to be Idle */
533 for (timeout
= 0; timeout
< AT_HW_MAX_IDLE_DELAY
; timeout
++) {
534 idle_status_data
= AT_READ_REG(hw
, REG_IDLE_STATUS
);
535 if (idle_status_data
== 0)
541 if (timeout
>= AT_HW_MAX_IDLE_DELAY
) {
542 netdev_err(adapter
->netdev
,
543 "MAC state machine can't be idle since disabled for 10ms second\n");
544 return AT_ERR_TIMEOUT
;
552 * Performs basic configuration of the adapter.
554 * hw - Struct containing variables accessed by shared code
555 * Assumes that the controller has previously been reset and is in a
556 * post-reset uninitialized state. Initializes multicast table,
557 * and Calls routines to setup link
558 * Leaves the transmit and receive units disabled and uninitialized.
560 int atl1e_init_hw(struct atl1e_hw
*hw
)
566 /* Zero out the Multicast HASH table */
567 /* clear the old settings from the multicast hash table */
568 AT_WRITE_REG(hw
, REG_RX_HASH_TABLE
, 0);
569 AT_WRITE_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, 1, 0);
571 ret_val
= atl1e_phy_init(hw
);
577 * 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
583 int atl1e_get_speed_and_duplex(struct atl1e_hw
*hw
, u16
*speed
, u16
*duplex
)
588 /* Read PHY Specific Status Register (17) */
589 err
= atl1e_read_phy_reg(hw
, MII_AT001_PSSR
, &phy_data
);
593 if (!(phy_data
& MII_AT001_PSSR_SPD_DPLX_RESOLVED
))
594 return AT_ERR_PHY_RES
;
596 switch (phy_data
& MII_AT001_PSSR_SPEED
) {
597 case MII_AT001_PSSR_1000MBS
:
600 case MII_AT001_PSSR_100MBS
:
603 case MII_AT001_PSSR_10MBS
:
607 return AT_ERR_PHY_SPEED
;
610 if (phy_data
& MII_AT001_PSSR_DPLX
)
611 *duplex
= FULL_DUPLEX
;
613 *duplex
= HALF_DUPLEX
;
618 int atl1e_restart_autoneg(struct atl1e_hw
*hw
)
622 err
= atl1e_write_phy_reg(hw
, MII_ADVERTISE
, hw
->mii_autoneg_adv_reg
);
626 if (hw
->nic_type
== athr_l1e
|| hw
->nic_type
== athr_l2e_revA
) {
627 err
= atl1e_write_phy_reg(hw
, MII_CTRL1000
,
628 hw
->mii_1000t_ctrl_reg
);
633 err
= atl1e_write_phy_reg(hw
, MII_BMCR
,
634 BMCR_RESET
| BMCR_ANENABLE
| BMCR_ANRESTART
);