2 * Copyright(c) 2007 Atheros Corporation. All rights reserved.
4 * Derived from Intel e1000 driver
5 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
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)
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
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.
21 #include <linux/pci.h>
22 #include <linux/delay.h>
23 #include <linux/mii.h>
24 #include <linux/crc32.h>
30 * return 1 if eeprom exist
32 int atl1c_check_eeprom_exist(struct atl1c_hw
*hw
)
36 AT_READ_REG(hw
, REG_TWSI_DEBUG
, &data
);
37 if (data
& TWSI_DEBUG_DEV_EXIST
)
43 void atl1c_hw_set_mac_addr(struct atl1c_hw
*hw
)
51 value
= (((u32
)hw
->mac_addr
[2]) << 24) |
52 (((u32
)hw
->mac_addr
[3]) << 16) |
53 (((u32
)hw
->mac_addr
[4]) << 8) |
54 (((u32
)hw
->mac_addr
[5])) ;
55 AT_WRITE_REG_ARRAY(hw
, REG_MAC_STA_ADDR
, 0, value
);
57 value
= (((u32
)hw
->mac_addr
[0]) << 8) |
58 (((u32
)hw
->mac_addr
[1])) ;
59 AT_WRITE_REG_ARRAY(hw
, REG_MAC_STA_ADDR
, 1, value
);
63 * atl1c_get_permanent_address
64 * return 0 if get valid mac address,
66 static int atl1c_get_permanent_address(struct atl1c_hw
*hw
)
72 u8 eth_addr
[ETH_ALEN
];
74 bool raise_vol
= false;
77 addr
[0] = addr
[1] = 0;
78 AT_READ_REG(hw
, REG_OTP_CTRL
, &otp_ctrl_data
);
79 if (atl1c_check_eeprom_exist(hw
)) {
80 if (hw
->nic_type
== athr_l1c
|| hw
->nic_type
== athr_l2c_b
) {
82 if (!(otp_ctrl_data
& OTP_CTRL_CLK_EN
)) {
83 otp_ctrl_data
|= OTP_CTRL_CLK_EN
;
84 AT_WRITE_REG(hw
, REG_OTP_CTRL
, otp_ctrl_data
);
90 if (hw
->nic_type
== athr_l2c_b
||
91 hw
->nic_type
== athr_l2c_b2
||
92 hw
->nic_type
== athr_l1d
) {
93 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, 0x00);
94 if (atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &phy_data
))
97 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, phy_data
);
99 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, 0x3B);
100 if (atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &phy_data
))
103 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, phy_data
);
108 AT_READ_REG(hw
, REG_TWSI_CTRL
, &twsi_ctrl_data
);
109 twsi_ctrl_data
|= TWSI_CTRL_SW_LDSTART
;
110 AT_WRITE_REG(hw
, REG_TWSI_CTRL
, twsi_ctrl_data
);
111 for (i
= 0; i
< AT_TWSI_EEPROM_TIMEOUT
; i
++) {
113 AT_READ_REG(hw
, REG_TWSI_CTRL
, &twsi_ctrl_data
);
114 if ((twsi_ctrl_data
& TWSI_CTRL_SW_LDSTART
) == 0)
117 if (i
>= AT_TWSI_EEPROM_TIMEOUT
)
120 /* Disable OTP_CLK */
121 if ((hw
->nic_type
== athr_l1c
|| hw
->nic_type
== athr_l2c
)) {
122 if (otp_ctrl_data
& OTP_CTRL_CLK_EN
) {
123 otp_ctrl_data
&= ~OTP_CTRL_CLK_EN
;
124 AT_WRITE_REG(hw
, REG_OTP_CTRL
, otp_ctrl_data
);
130 if (hw
->nic_type
== athr_l2c_b
||
131 hw
->nic_type
== athr_l2c_b2
||
132 hw
->nic_type
== athr_l1d
) {
133 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, 0x00);
134 if (atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &phy_data
))
137 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, phy_data
);
139 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, 0x3B);
140 if (atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &phy_data
))
143 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, phy_data
);
148 /* maybe MAC-address is from BIOS */
149 AT_READ_REG(hw
, REG_MAC_STA_ADDR
, &addr
[0]);
150 AT_READ_REG(hw
, REG_MAC_STA_ADDR
+ 4, &addr
[1]);
151 *(u32
*) ð_addr
[2] = swab32(addr
[0]);
152 *(u16
*) ð_addr
[0] = swab16(*(u16
*)&addr
[1]);
154 if (is_valid_ether_addr(eth_addr
)) {
155 memcpy(hw
->perm_mac_addr
, eth_addr
, ETH_ALEN
);
163 bool atl1c_read_eeprom(struct atl1c_hw
*hw
, u32 offset
, u32
*p_value
)
172 return ret
; /* address do not align */
174 AT_READ_REG(hw
, REG_OTP_CTRL
, &otp_ctrl_data
);
175 if (!(otp_ctrl_data
& OTP_CTRL_CLK_EN
))
176 AT_WRITE_REG(hw
, REG_OTP_CTRL
,
177 (otp_ctrl_data
| OTP_CTRL_CLK_EN
));
179 AT_WRITE_REG(hw
, REG_EEPROM_DATA_LO
, 0);
180 control
= (offset
& EEPROM_CTRL_ADDR_MASK
) << EEPROM_CTRL_ADDR_SHIFT
;
181 AT_WRITE_REG(hw
, REG_EEPROM_CTRL
, control
);
183 for (i
= 0; i
< 10; i
++) {
185 AT_READ_REG(hw
, REG_EEPROM_CTRL
, &control
);
186 if (control
& EEPROM_CTRL_RW
)
189 if (control
& EEPROM_CTRL_RW
) {
190 AT_READ_REG(hw
, REG_EEPROM_CTRL
, &data
);
191 AT_READ_REG(hw
, REG_EEPROM_DATA_LO
, p_value
);
192 data
= data
& 0xFFFF;
193 *p_value
= swab32((data
<< 16) | (*p_value
>> 16));
196 if (!(otp_ctrl_data
& OTP_CTRL_CLK_EN
))
197 AT_WRITE_REG(hw
, REG_OTP_CTRL
, otp_ctrl_data
);
202 * Reads the adapter's MAC address from the EEPROM
204 * hw - Struct containing variables accessed by shared code
206 int atl1c_read_mac_addr(struct atl1c_hw
*hw
)
210 err
= atl1c_get_permanent_address(hw
);
212 random_ether_addr(hw
->perm_mac_addr
);
214 memcpy(hw
->mac_addr
, hw
->perm_mac_addr
, sizeof(hw
->perm_mac_addr
));
221 * set hash value for a multicast address
222 * hash calcu processing :
223 * 1. calcu 32bit CRC for multicast address
224 * 2. reverse crc with MSB to LSB
226 u32
atl1c_hash_mc_addr(struct atl1c_hw
*hw
, u8
*mc_addr
)
232 crc32
= ether_crc_le(6, mc_addr
);
233 for (i
= 0; i
< 32; i
++)
234 value
|= (((crc32
>> i
) & 1) << (31 - i
));
240 * Sets the bit in the multicast table corresponding to the hash value.
241 * hw - Struct containing variables accessed by shared code
242 * hash_value - Multicast address hash value
244 void atl1c_hash_set(struct atl1c_hw
*hw
, u32 hash_value
)
246 u32 hash_bit
, hash_reg
;
250 * The HASH Table is a register array of 2 32-bit registers.
251 * It is treated like an array of 64 bits. We want to set
252 * bit BitArray[hash_value]. So we figure out what register
253 * the bit is in, read it, OR in the new bit, then write
254 * back the new value. The register is determined by the
255 * upper bit of the hash value and the bit within that
256 * register are determined by the lower 5 bits of the value.
258 hash_reg
= (hash_value
>> 31) & 0x1;
259 hash_bit
= (hash_value
>> 26) & 0x1F;
261 mta
= AT_READ_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, hash_reg
);
263 mta
|= (1 << hash_bit
);
265 AT_WRITE_REG_ARRAY(hw
, REG_RX_HASH_TABLE
, hash_reg
, mta
);
269 * Reads the value from a PHY register
270 * hw - Struct containing variables accessed by shared code
271 * reg_addr - address of the PHY register to read
273 int atl1c_read_phy_reg(struct atl1c_hw
*hw
, u16 reg_addr
, u16
*phy_data
)
278 val
= ((u32
)(reg_addr
& MDIO_REG_ADDR_MASK
)) << MDIO_REG_ADDR_SHIFT
|
279 MDIO_START
| MDIO_SUP_PREAMBLE
| MDIO_RW
|
280 MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHIFT
;
282 AT_WRITE_REG(hw
, REG_MDIO_CTRL
, val
);
284 for (i
= 0; i
< MDIO_WAIT_TIMES
; i
++) {
286 AT_READ_REG(hw
, REG_MDIO_CTRL
, &val
);
287 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
290 if (!(val
& (MDIO_START
| MDIO_BUSY
))) {
291 *phy_data
= (u16
)val
;
299 * Writes a value to a PHY register
300 * hw - Struct containing variables accessed by shared code
301 * reg_addr - address of the PHY register to write
302 * data - data to write to the PHY
304 int atl1c_write_phy_reg(struct atl1c_hw
*hw
, u32 reg_addr
, u16 phy_data
)
309 val
= ((u32
)(phy_data
& MDIO_DATA_MASK
)) << MDIO_DATA_SHIFT
|
310 (reg_addr
& MDIO_REG_ADDR_MASK
) << MDIO_REG_ADDR_SHIFT
|
311 MDIO_SUP_PREAMBLE
| MDIO_START
|
312 MDIO_CLK_25_4
<< MDIO_CLK_SEL_SHIFT
;
314 AT_WRITE_REG(hw
, REG_MDIO_CTRL
, val
);
316 for (i
= 0; i
< MDIO_WAIT_TIMES
; i
++) {
318 AT_READ_REG(hw
, REG_MDIO_CTRL
, &val
);
319 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
323 if (!(val
& (MDIO_START
| MDIO_BUSY
)))
330 * Configures PHY autoneg and flow control advertisement settings
332 * hw - Struct containing variables accessed by shared code
334 static int atl1c_phy_setup_adv(struct atl1c_hw
*hw
)
336 u16 mii_adv_data
= ADVERTISE_DEFAULT_CAP
& ~ADVERTISE_SPEED_MASK
;
337 u16 mii_giga_ctrl_data
= GIGA_CR_1000T_DEFAULT_CAP
&
338 ~GIGA_CR_1000T_SPEED_MASK
;
340 if (hw
->autoneg_advertised
& ADVERTISED_10baseT_Half
)
341 mii_adv_data
|= ADVERTISE_10HALF
;
342 if (hw
->autoneg_advertised
& ADVERTISED_10baseT_Full
)
343 mii_adv_data
|= ADVERTISE_10FULL
;
344 if (hw
->autoneg_advertised
& ADVERTISED_100baseT_Half
)
345 mii_adv_data
|= ADVERTISE_100HALF
;
346 if (hw
->autoneg_advertised
& ADVERTISED_100baseT_Full
)
347 mii_adv_data
|= ADVERTISE_100FULL
;
349 if (hw
->autoneg_advertised
& ADVERTISED_Autoneg
)
350 mii_adv_data
|= ADVERTISE_10HALF
| ADVERTISE_10FULL
|
351 ADVERTISE_100HALF
| ADVERTISE_100FULL
;
353 if (hw
->link_cap_flags
& ATL1C_LINK_CAP_1000M
) {
354 if (hw
->autoneg_advertised
& ADVERTISED_1000baseT_Half
)
355 mii_giga_ctrl_data
|= ADVERTISE_1000HALF
;
356 if (hw
->autoneg_advertised
& ADVERTISED_1000baseT_Full
)
357 mii_giga_ctrl_data
|= ADVERTISE_1000FULL
;
358 if (hw
->autoneg_advertised
& ADVERTISED_Autoneg
)
359 mii_giga_ctrl_data
|= ADVERTISE_1000HALF
|
363 if (atl1c_write_phy_reg(hw
, MII_ADVERTISE
, mii_adv_data
) != 0 ||
364 atl1c_write_phy_reg(hw
, MII_GIGA_CR
, mii_giga_ctrl_data
) != 0)
369 void atl1c_phy_disable(struct atl1c_hw
*hw
)
371 AT_WRITE_REGW(hw
, REG_GPHY_CTRL
,
372 GPHY_CTRL_PW_WOL_DIS
| GPHY_CTRL_EXT_RESET
);
375 static void atl1c_phy_magic_data(struct atl1c_hw
*hw
)
379 data
= ANA_LOOP_SEL_10BT
| ANA_EN_MASK_TB
| ANA_EN_10BT_IDLE
|
380 ((1 & ANA_INTERVAL_SEL_TIMER_MASK
) <<
381 ANA_INTERVAL_SEL_TIMER_SHIFT
);
383 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, MII_ANA_CTRL_18
);
384 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, data
);
386 data
= (2 & ANA_SERDES_CDR_BW_MASK
) | ANA_MS_PAD_DBG
|
387 ANA_SERDES_EN_DEEM
| ANA_SERDES_SEL_HSP
| ANA_SERDES_EN_PLL
|
390 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, MII_ANA_CTRL_5
);
391 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, data
);
393 data
= (44 & ANA_LONG_CABLE_TH_100_MASK
) |
394 ((33 & ANA_SHORT_CABLE_TH_100_MASK
) <<
395 ANA_SHORT_CABLE_TH_100_SHIFT
) | ANA_BP_BAD_LINK_ACCUM
|
398 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, MII_ANA_CTRL_54
);
399 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, data
);
401 data
= (11 & ANA_IECHO_ADJ_MASK
) | ((11 & ANA_IECHO_ADJ_MASK
) <<
402 ANA_IECHO_ADJ_2_SHIFT
) | ((8 & ANA_IECHO_ADJ_MASK
) <<
403 ANA_IECHO_ADJ_1_SHIFT
) | ((8 & ANA_IECHO_ADJ_MASK
) <<
404 ANA_IECHO_ADJ_0_SHIFT
);
406 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, MII_ANA_CTRL_4
);
407 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, data
);
409 data
= ANA_RESTART_CAL
| ((7 & ANA_MANUL_SWICH_ON_MASK
) <<
410 ANA_MANUL_SWICH_ON_SHIFT
) | ANA_MAN_ENABLE
|
411 ANA_SEL_HSP
| ANA_EN_HB
| ANA_OEN_125M
;
413 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, MII_ANA_CTRL_0
);
414 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, data
);
416 if (hw
->ctrl_flags
& ATL1C_HIB_DISABLE
) {
417 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, MII_ANA_CTRL_41
);
418 if (atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &data
) != 0)
420 data
&= ~ANA_TOP_PS_EN
;
421 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, data
);
423 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, MII_ANA_CTRL_11
);
424 if (atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &data
) != 0)
426 data
&= ~ANA_PS_HIB_EN
;
427 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, data
);
431 int atl1c_phy_reset(struct atl1c_hw
*hw
)
433 struct atl1c_adapter
*adapter
= hw
->adapter
;
434 struct pci_dev
*pdev
= adapter
->pdev
;
436 u32 phy_ctrl_data
= GPHY_CTRL_DEFAULT
;
437 u32 mii_ier_data
= IER_LINK_UP
| IER_LINK_DOWN
;
440 if (hw
->ctrl_flags
& ATL1C_HIB_DISABLE
)
441 phy_ctrl_data
&= ~GPHY_CTRL_HIB_EN
;
443 AT_WRITE_REG(hw
, REG_GPHY_CTRL
, phy_ctrl_data
);
446 phy_ctrl_data
|= GPHY_CTRL_EXT_RESET
;
447 AT_WRITE_REG(hw
, REG_GPHY_CTRL
, phy_ctrl_data
);
451 if (hw
->nic_type
== athr_l2c_b
) {
452 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, 0x0A);
453 atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &phy_data
);
454 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, phy_data
& 0xDFFF);
457 if (hw
->nic_type
== athr_l2c_b
||
458 hw
->nic_type
== athr_l2c_b2
||
459 hw
->nic_type
== athr_l1d
) {
460 atl1c_write_phy_reg(hw
, MII_DBG_ADDR
, 0x3B);
461 atl1c_read_phy_reg(hw
, MII_DBG_DATA
, &phy_data
);
462 atl1c_write_phy_reg(hw
, MII_DBG_DATA
, phy_data
& 0xFFF7);
466 /*Enable PHY LinkChange Interrupt */
467 err
= atl1c_write_phy_reg(hw
, MII_IER
, mii_ier_data
);
469 if (netif_msg_hw(adapter
))
471 "Error enable PHY linkChange Interrupt\n");
474 if (!(hw
->ctrl_flags
& ATL1C_FPGA_VERSION
))
475 atl1c_phy_magic_data(hw
);
479 int atl1c_phy_init(struct atl1c_hw
*hw
)
481 struct atl1c_adapter
*adapter
= (struct atl1c_adapter
*)hw
->adapter
;
482 struct pci_dev
*pdev
= adapter
->pdev
;
484 u16 mii_bmcr_data
= BMCR_RESET
;
485 u16 phy_id1
, phy_id2
;
487 if ((atl1c_read_phy_reg(hw
, MII_PHYSID1
, &phy_id1
) != 0) ||
488 (atl1c_read_phy_reg(hw
, MII_PHYSID2
, &phy_id2
) != 0)) {
489 if (netif_msg_link(adapter
))
490 dev_err(&pdev
->dev
, "Error get phy ID\n");
493 switch (hw
->media_type
) {
494 case MEDIA_TYPE_AUTO_SENSOR
:
495 ret_val
= atl1c_phy_setup_adv(hw
);
497 if (netif_msg_link(adapter
))
499 "Error Setting up Auto-Negotiation\n");
502 mii_bmcr_data
|= BMCR_AUTO_NEG_EN
| BMCR_RESTART_AUTO_NEG
;
504 case MEDIA_TYPE_100M_FULL
:
505 mii_bmcr_data
|= BMCR_SPEED_100
| BMCR_FULL_DUPLEX
;
507 case MEDIA_TYPE_100M_HALF
:
508 mii_bmcr_data
|= BMCR_SPEED_100
;
510 case MEDIA_TYPE_10M_FULL
:
511 mii_bmcr_data
|= BMCR_SPEED_10
| BMCR_FULL_DUPLEX
;
513 case MEDIA_TYPE_10M_HALF
:
514 mii_bmcr_data
|= BMCR_SPEED_10
;
517 if (netif_msg_link(adapter
))
518 dev_err(&pdev
->dev
, "Wrong Media type %d\n",
524 ret_val
= atl1c_write_phy_reg(hw
, MII_BMCR
, mii_bmcr_data
);
527 hw
->phy_configured
= true;
533 * Detects the current speed and duplex settings of the hardware.
535 * hw - Struct containing variables accessed by shared code
536 * speed - Speed of the connection
537 * duplex - Duplex setting of the connection
539 int atl1c_get_speed_and_duplex(struct atl1c_hw
*hw
, u16
*speed
, u16
*duplex
)
544 /* Read PHY Specific Status Register (17) */
545 err
= atl1c_read_phy_reg(hw
, MII_GIGA_PSSR
, &phy_data
);
549 if (!(phy_data
& GIGA_PSSR_SPD_DPLX_RESOLVED
))
552 switch (phy_data
& GIGA_PSSR_SPEED
) {
553 case GIGA_PSSR_1000MBS
:
556 case GIGA_PSSR_100MBS
:
559 case GIGA_PSSR_10MBS
:
567 if (phy_data
& GIGA_PSSR_DPLX
)
568 *duplex
= FULL_DUPLEX
;
570 *duplex
= HALF_DUPLEX
;
575 int atl1c_restart_autoneg(struct atl1c_hw
*hw
)
578 u16 mii_bmcr_data
= BMCR_RESET
;
580 err
= atl1c_phy_setup_adv(hw
);
583 mii_bmcr_data
|= BMCR_AUTO_NEG_EN
| BMCR_RESTART_AUTO_NEG
;
585 return atl1c_write_phy_reg(hw
, MII_BMCR
, mii_bmcr_data
);