WIP FPC-III support
[linux/fpc-iii.git] / drivers / net / ethernet / atheros / atlx / atlx.c
blob0941d07d083397dc1ca09cf1d1d543607ad05f69
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /* atlx.c -- common functions for Attansic network drivers
4 * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
5 * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com>
6 * Copyright(c) 2006 - 2008 Jay Cliburn <jcliburn@gmail.com>
7 * Copyright(c) 2007 Atheros Corporation. All rights reserved.
9 * Derived from Intel e1000 driver
10 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
13 /* Including this file like a header is a temporary hack, I promise. -- CHS */
14 #ifndef ATLX_C
15 #define ATLX_C
17 #include <linux/device.h>
18 #include <linux/errno.h>
19 #include <linux/etherdevice.h>
20 #include <linux/if.h>
21 #include <linux/netdevice.h>
22 #include <linux/socket.h>
23 #include <linux/sockios.h>
24 #include <linux/spinlock.h>
25 #include <linux/string.h>
26 #include <linux/types.h>
27 #include <linux/workqueue.h>
29 #include "atlx.h"
31 static s32 atlx_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data);
32 static u32 atlx_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr);
33 static void atlx_set_mac_addr(struct atl1_hw *hw);
35 static struct atlx_spi_flash_dev flash_table[] = {
36 /* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SEC_ERS CHIP_ERS */
37 {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62},
38 {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60},
39 {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7},
42 static int atlx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
44 switch (cmd) {
45 case SIOCGMIIPHY:
46 case SIOCGMIIREG:
47 case SIOCSMIIREG:
48 return atlx_mii_ioctl(netdev, ifr, cmd);
49 default:
50 return -EOPNOTSUPP;
54 /**
55 * atlx_set_mac - Change the Ethernet Address of the NIC
56 * @netdev: network interface device structure
57 * @p: pointer to an address structure
59 * Returns 0 on success, negative on failure
61 static int atlx_set_mac(struct net_device *netdev, void *p)
63 struct atlx_adapter *adapter = netdev_priv(netdev);
64 struct sockaddr *addr = p;
66 if (netif_running(netdev))
67 return -EBUSY;
69 if (!is_valid_ether_addr(addr->sa_data))
70 return -EADDRNOTAVAIL;
72 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
73 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
75 atlx_set_mac_addr(&adapter->hw);
76 return 0;
79 static void atlx_check_for_link(struct atlx_adapter *adapter)
81 struct net_device *netdev = adapter->netdev;
82 u16 phy_data = 0;
84 spin_lock(&adapter->lock);
85 adapter->phy_timer_pending = false;
86 atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
87 atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
88 spin_unlock(&adapter->lock);
90 /* notify upper layer link down ASAP */
91 if (!(phy_data & BMSR_LSTATUS)) {
92 /* Link Down */
93 if (netif_carrier_ok(netdev)) {
94 /* old link state: Up */
95 dev_info(&adapter->pdev->dev, "%s link is down\n",
96 netdev->name);
97 adapter->link_speed = SPEED_0;
98 netif_carrier_off(netdev);
101 schedule_work(&adapter->link_chg_task);
105 * atlx_set_multi - Multicast and Promiscuous mode set
106 * @netdev: network interface device structure
108 * The set_multi entry point is called whenever the multicast address
109 * list or the network interface flags are updated. This routine is
110 * responsible for configuring the hardware for proper multicast,
111 * promiscuous mode, and all-multi behavior.
113 static void atlx_set_multi(struct net_device *netdev)
115 struct atlx_adapter *adapter = netdev_priv(netdev);
116 struct atlx_hw *hw = &adapter->hw;
117 struct netdev_hw_addr *ha;
118 u32 rctl;
119 u32 hash_value;
121 /* Check for Promiscuous and All Multicast modes */
122 rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
123 if (netdev->flags & IFF_PROMISC)
124 rctl |= MAC_CTRL_PROMIS_EN;
125 else if (netdev->flags & IFF_ALLMULTI) {
126 rctl |= MAC_CTRL_MC_ALL_EN;
127 rctl &= ~MAC_CTRL_PROMIS_EN;
128 } else
129 rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
131 iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
133 /* clear the old settings from the multicast hash table */
134 iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
135 iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
137 /* compute mc addresses' hash value ,and put it into hash table */
138 netdev_for_each_mc_addr(ha, netdev) {
139 hash_value = atlx_hash_mc_addr(hw, ha->addr);
140 atlx_hash_set(hw, hash_value);
144 static inline void atlx_imr_set(struct atlx_adapter *adapter,
145 unsigned int imr)
147 iowrite32(imr, adapter->hw.hw_addr + REG_IMR);
148 ioread32(adapter->hw.hw_addr + REG_IMR);
152 * atlx_irq_enable - Enable default interrupt generation settings
153 * @adapter: board private structure
155 static void atlx_irq_enable(struct atlx_adapter *adapter)
157 atlx_imr_set(adapter, IMR_NORMAL_MASK);
158 adapter->int_enabled = true;
162 * atlx_irq_disable - Mask off interrupt generation on the NIC
163 * @adapter: board private structure
165 static void atlx_irq_disable(struct atlx_adapter *adapter)
167 adapter->int_enabled = false;
168 atlx_imr_set(adapter, 0);
169 synchronize_irq(adapter->pdev->irq);
172 static void atlx_clear_phy_int(struct atlx_adapter *adapter)
174 u16 phy_data;
175 unsigned long flags;
177 spin_lock_irqsave(&adapter->lock, flags);
178 atlx_read_phy_reg(&adapter->hw, 19, &phy_data);
179 spin_unlock_irqrestore(&adapter->lock, flags);
183 * atlx_tx_timeout - Respond to a Tx Hang
184 * @netdev: network interface device structure
186 static void atlx_tx_timeout(struct net_device *netdev, unsigned int txqueue)
188 struct atlx_adapter *adapter = netdev_priv(netdev);
189 /* Do the reset outside of interrupt context */
190 schedule_work(&adapter->reset_dev_task);
194 * atlx_link_chg_task - deal with link change event Out of interrupt context
196 static void atlx_link_chg_task(struct work_struct *work)
198 struct atlx_adapter *adapter;
199 unsigned long flags;
201 adapter = container_of(work, struct atlx_adapter, link_chg_task);
203 spin_lock_irqsave(&adapter->lock, flags);
204 atlx_check_link(adapter);
205 spin_unlock_irqrestore(&adapter->lock, flags);
208 static void __atlx_vlan_mode(netdev_features_t features, u32 *ctrl)
210 if (features & NETIF_F_HW_VLAN_CTAG_RX) {
211 /* enable VLAN tag insert/strip */
212 *ctrl |= MAC_CTRL_RMV_VLAN;
213 } else {
214 /* disable VLAN tag insert/strip */
215 *ctrl &= ~MAC_CTRL_RMV_VLAN;
219 static void atlx_vlan_mode(struct net_device *netdev,
220 netdev_features_t features)
222 struct atlx_adapter *adapter = netdev_priv(netdev);
223 unsigned long flags;
224 u32 ctrl;
226 spin_lock_irqsave(&adapter->lock, flags);
227 /* atlx_irq_disable(adapter); FIXME: confirm/remove */
228 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
229 __atlx_vlan_mode(features, &ctrl);
230 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
231 /* atlx_irq_enable(adapter); FIXME */
232 spin_unlock_irqrestore(&adapter->lock, flags);
235 static void atlx_restore_vlan(struct atlx_adapter *adapter)
237 atlx_vlan_mode(adapter->netdev, adapter->netdev->features);
240 static netdev_features_t atlx_fix_features(struct net_device *netdev,
241 netdev_features_t features)
244 * Since there is no support for separate rx/tx vlan accel
245 * enable/disable make sure tx flag is always in same state as rx.
247 if (features & NETIF_F_HW_VLAN_CTAG_RX)
248 features |= NETIF_F_HW_VLAN_CTAG_TX;
249 else
250 features &= ~NETIF_F_HW_VLAN_CTAG_TX;
252 return features;
255 static int atlx_set_features(struct net_device *netdev,
256 netdev_features_t features)
258 netdev_features_t changed = netdev->features ^ features;
260 if (changed & NETIF_F_HW_VLAN_CTAG_RX)
261 atlx_vlan_mode(netdev, features);
263 return 0;
266 #endif /* ATLX_C */