blob: 3cd8837236dc1de68727991cc399b6906e30d5c3 [file] [log] [blame]
Jay Cliburn305282b2008-02-02 19:50:04 -06001/* atlx.c -- common functions for Attansic network drivers
2 *
3 * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved.
4 * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com>
Jay Cliburne8f720f2008-05-09 22:12:09 -05005 * Copyright(c) 2006 - 2008 Jay Cliburn <jcliburn@gmail.com>
Jay Cliburn305282b2008-02-02 19:50:04 -06006 * Copyright(c) 2007 Atheros Corporation. All rights reserved.
7 *
8 * Derived from Intel e1000 driver
9 * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved.
10 *
11 * This program is free software; you can redistribute it and/or modify it
12 * under the terms of the GNU General Public License as published by the Free
13 * Software Foundation; either version 2 of the License, or (at your option)
14 * any later version.
15 *
16 * This program is distributed in the hope that it will be useful, but WITHOUT
17 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
18 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
19 * more details.
20 *
21 * You should have received a copy of the GNU General Public License along with
22 * this program; if not, write to the Free Software Foundation, Inc., 59
23 * Temple Place - Suite 330, Boston, MA 02111-1307, USA.
24 */
25
26/* Including this file like a header is a temporary hack, I promise. -- CHS */
27#ifndef ATLX_C
28#define ATLX_C
29
30#include <linux/device.h>
31#include <linux/errno.h>
32#include <linux/etherdevice.h>
33#include <linux/if.h>
34#include <linux/netdevice.h>
35#include <linux/socket.h>
36#include <linux/sockios.h>
37#include <linux/spinlock.h>
38#include <linux/string.h>
39#include <linux/types.h>
40#include <linux/workqueue.h>
41
42#include "atlx.h"
43
stephen hemmingerff2d8d62010-10-21 07:50:50 +000044static s32 atlx_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data);
45static u32 atlx_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr);
46static void atlx_set_mac_addr(struct atl1_hw *hw);
47
Jay Cliburn305282b2008-02-02 19:50:04 -060048static struct atlx_spi_flash_dev flash_table[] = {
49/* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SEC_ERS CHIP_ERS */
50 {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62},
51 {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60},
52 {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7},
53};
54
55static int atlx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd)
56{
57 switch (cmd) {
58 case SIOCGMIIPHY:
59 case SIOCGMIIREG:
60 case SIOCSMIIREG:
61 return atlx_mii_ioctl(netdev, ifr, cmd);
62 default:
63 return -EOPNOTSUPP;
64 }
65}
66
67/*
68 * atlx_set_mac - Change the Ethernet Address of the NIC
69 * @netdev: network interface device structure
70 * @p: pointer to an address structure
71 *
72 * Returns 0 on success, negative on failure
73 */
74static int atlx_set_mac(struct net_device *netdev, void *p)
75{
76 struct atlx_adapter *adapter = netdev_priv(netdev);
77 struct sockaddr *addr = p;
78
79 if (netif_running(netdev))
80 return -EBUSY;
81
82 if (!is_valid_ether_addr(addr->sa_data))
83 return -EADDRNOTAVAIL;
84
85 memcpy(netdev->dev_addr, addr->sa_data, netdev->addr_len);
86 memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len);
Danny Kukawka6a214fd2012-02-17 05:43:30 +000087 netdev->addr_assign_type &= ~NET_ADDR_RANDOM;
Jay Cliburn305282b2008-02-02 19:50:04 -060088
89 atlx_set_mac_addr(&adapter->hw);
90 return 0;
91}
92
93static void atlx_check_for_link(struct atlx_adapter *adapter)
94{
95 struct net_device *netdev = adapter->netdev;
96 u16 phy_data = 0;
97
98 spin_lock(&adapter->lock);
99 adapter->phy_timer_pending = false;
100 atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
101 atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data);
102 spin_unlock(&adapter->lock);
103
104 /* notify upper layer link down ASAP */
105 if (!(phy_data & BMSR_LSTATUS)) {
106 /* Link Down */
107 if (netif_carrier_ok(netdev)) {
108 /* old link state: Up */
109 dev_info(&adapter->pdev->dev, "%s link is down\n",
110 netdev->name);
111 adapter->link_speed = SPEED_0;
112 netif_carrier_off(netdev);
Jay Cliburn305282b2008-02-02 19:50:04 -0600113 }
114 }
115 schedule_work(&adapter->link_chg_task);
116}
117
118/*
119 * atlx_set_multi - Multicast and Promiscuous mode set
120 * @netdev: network interface device structure
121 *
122 * The set_multi entry point is called whenever the multicast address
123 * list or the network interface flags are updated. This routine is
124 * responsible for configuring the hardware for proper multicast,
125 * promiscuous mode, and all-multi behavior.
126 */
127static void atlx_set_multi(struct net_device *netdev)
128{
129 struct atlx_adapter *adapter = netdev_priv(netdev);
130 struct atlx_hw *hw = &adapter->hw;
Jiri Pirko22bedad32010-04-01 21:22:57 +0000131 struct netdev_hw_addr *ha;
Jay Cliburn305282b2008-02-02 19:50:04 -0600132 u32 rctl;
133 u32 hash_value;
134
135 /* Check for Promiscuous and All Multicast modes */
136 rctl = ioread32(hw->hw_addr + REG_MAC_CTRL);
137 if (netdev->flags & IFF_PROMISC)
138 rctl |= MAC_CTRL_PROMIS_EN;
139 else if (netdev->flags & IFF_ALLMULTI) {
140 rctl |= MAC_CTRL_MC_ALL_EN;
141 rctl &= ~MAC_CTRL_PROMIS_EN;
142 } else
143 rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN);
144
145 iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL);
146
147 /* clear the old settings from the multicast hash table */
148 iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE);
149 iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2));
150
151 /* compute mc addresses' hash value ,and put it into hash table */
Jiri Pirko22bedad32010-04-01 21:22:57 +0000152 netdev_for_each_mc_addr(ha, netdev) {
153 hash_value = atlx_hash_mc_addr(hw, ha->addr);
Jay Cliburn305282b2008-02-02 19:50:04 -0600154 atlx_hash_set(hw, hash_value);
155 }
156}
157
158/*
159 * atlx_irq_enable - Enable default interrupt generation settings
160 * @adapter: board private structure
161 */
162static void atlx_irq_enable(struct atlx_adapter *adapter)
163{
164 iowrite32(IMR_NORMAL_MASK, adapter->hw.hw_addr + REG_IMR);
165 ioread32(adapter->hw.hw_addr + REG_IMR);
166}
167
168/*
169 * atlx_irq_disable - Mask off interrupt generation on the NIC
170 * @adapter: board private structure
171 */
172static void atlx_irq_disable(struct atlx_adapter *adapter)
173{
174 iowrite32(0, adapter->hw.hw_addr + REG_IMR);
175 ioread32(adapter->hw.hw_addr + REG_IMR);
176 synchronize_irq(adapter->pdev->irq);
177}
178
179static void atlx_clear_phy_int(struct atlx_adapter *adapter)
180{
181 u16 phy_data;
182 unsigned long flags;
183
184 spin_lock_irqsave(&adapter->lock, flags);
185 atlx_read_phy_reg(&adapter->hw, 19, &phy_data);
186 spin_unlock_irqrestore(&adapter->lock, flags);
187}
188
189/*
Jay Cliburn305282b2008-02-02 19:50:04 -0600190 * atlx_tx_timeout - Respond to a Tx Hang
191 * @netdev: network interface device structure
192 */
193static void atlx_tx_timeout(struct net_device *netdev)
194{
195 struct atlx_adapter *adapter = netdev_priv(netdev);
196 /* Do the reset outside of interrupt context */
197 schedule_work(&adapter->tx_timeout_task);
198}
199
200/*
201 * atlx_link_chg_task - deal with link change event Out of interrupt context
202 */
203static void atlx_link_chg_task(struct work_struct *work)
204{
205 struct atlx_adapter *adapter;
206 unsigned long flags;
207
208 adapter = container_of(work, struct atlx_adapter, link_chg_task);
209
210 spin_lock_irqsave(&adapter->lock, flags);
211 atlx_check_link(adapter);
212 spin_unlock_irqrestore(&adapter->lock, flags);
213}
214
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000215static void __atlx_vlan_mode(netdev_features_t features, u32 *ctrl)
Jiri Pirko92491702011-07-20 04:54:33 +0000216{
217 if (features & NETIF_F_HW_VLAN_RX) {
218 /* enable VLAN tag insert/strip */
219 *ctrl |= MAC_CTRL_RMV_VLAN;
220 } else {
221 /* disable VLAN tag insert/strip */
222 *ctrl &= ~MAC_CTRL_RMV_VLAN;
223 }
224}
225
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000226static void atlx_vlan_mode(struct net_device *netdev,
227 netdev_features_t features)
Jay Cliburn305282b2008-02-02 19:50:04 -0600228{
229 struct atlx_adapter *adapter = netdev_priv(netdev);
230 unsigned long flags;
231 u32 ctrl;
232
233 spin_lock_irqsave(&adapter->lock, flags);
234 /* atlx_irq_disable(adapter); FIXME: confirm/remove */
Jiri Pirko92491702011-07-20 04:54:33 +0000235 ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL);
236 __atlx_vlan_mode(features, &ctrl);
237 iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL);
Jay Cliburn305282b2008-02-02 19:50:04 -0600238 /* atlx_irq_enable(adapter); FIXME */
239 spin_unlock_irqrestore(&adapter->lock, flags);
240}
241
242static void atlx_restore_vlan(struct atlx_adapter *adapter)
243{
Jiri Pirko92491702011-07-20 04:54:33 +0000244 atlx_vlan_mode(adapter->netdev, adapter->netdev->features);
245}
246
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000247static netdev_features_t atlx_fix_features(struct net_device *netdev,
248 netdev_features_t features)
Jiri Pirko92491702011-07-20 04:54:33 +0000249{
250 /*
251 * Since there is no support for separate rx/tx vlan accel
252 * enable/disable make sure tx flag is always in same state as rx.
253 */
254 if (features & NETIF_F_HW_VLAN_RX)
255 features |= NETIF_F_HW_VLAN_TX;
256 else
257 features &= ~NETIF_F_HW_VLAN_TX;
258
259 return features;
260}
261
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000262static int atlx_set_features(struct net_device *netdev,
263 netdev_features_t features)
Jiri Pirko92491702011-07-20 04:54:33 +0000264{
Michał Mirosławc8f44af2011-11-15 15:29:55 +0000265 netdev_features_t changed = netdev->features ^ features;
Jiri Pirko92491702011-07-20 04:54:33 +0000266
267 if (changed & NETIF_F_HW_VLAN_RX)
268 atlx_vlan_mode(netdev, features);
269
270 return 0;
Jay Cliburn305282b2008-02-02 19:50:04 -0600271}
272
Jay Cliburn305282b2008-02-02 19:50:04 -0600273#endif /* ATLX_C */