162306a36Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-or-later 262306a36Sopenharmony_ci/* atlx.c -- common functions for Attansic network drivers 362306a36Sopenharmony_ci * 462306a36Sopenharmony_ci * Copyright(c) 2005 - 2006 Attansic Corporation. All rights reserved. 562306a36Sopenharmony_ci * Copyright(c) 2006 - 2007 Chris Snook <csnook@redhat.com> 662306a36Sopenharmony_ci * Copyright(c) 2006 - 2008 Jay Cliburn <jcliburn@gmail.com> 762306a36Sopenharmony_ci * Copyright(c) 2007 Atheros Corporation. All rights reserved. 862306a36Sopenharmony_ci * 962306a36Sopenharmony_ci * Derived from Intel e1000 driver 1062306a36Sopenharmony_ci * Copyright(c) 1999 - 2005 Intel Corporation. All rights reserved. 1162306a36Sopenharmony_ci */ 1262306a36Sopenharmony_ci 1362306a36Sopenharmony_ci/* Including this file like a header is a temporary hack, I promise. -- CHS */ 1462306a36Sopenharmony_ci#ifndef ATLX_C 1562306a36Sopenharmony_ci#define ATLX_C 1662306a36Sopenharmony_ci 1762306a36Sopenharmony_ci#include <linux/device.h> 1862306a36Sopenharmony_ci#include <linux/errno.h> 1962306a36Sopenharmony_ci#include <linux/etherdevice.h> 2062306a36Sopenharmony_ci#include <linux/if.h> 2162306a36Sopenharmony_ci#include <linux/netdevice.h> 2262306a36Sopenharmony_ci#include <linux/socket.h> 2362306a36Sopenharmony_ci#include <linux/sockios.h> 2462306a36Sopenharmony_ci#include <linux/spinlock.h> 2562306a36Sopenharmony_ci#include <linux/string.h> 2662306a36Sopenharmony_ci#include <linux/types.h> 2762306a36Sopenharmony_ci#include <linux/workqueue.h> 2862306a36Sopenharmony_ci 2962306a36Sopenharmony_ci#include "atlx.h" 3062306a36Sopenharmony_ci 3162306a36Sopenharmony_cistatic s32 atlx_read_phy_reg(struct atl1_hw *hw, u16 reg_addr, u16 *phy_data); 3262306a36Sopenharmony_cistatic u32 atlx_hash_mc_addr(struct atl1_hw *hw, u8 *mc_addr); 3362306a36Sopenharmony_cistatic void atlx_set_mac_addr(struct atl1_hw *hw); 3462306a36Sopenharmony_ci 3562306a36Sopenharmony_cistatic struct atlx_spi_flash_dev flash_table[] = { 3662306a36Sopenharmony_ci/* MFR_NAME WRSR READ PRGM WREN WRDI RDSR RDID SEC_ERS CHIP_ERS */ 3762306a36Sopenharmony_ci {"Atmel", 0x00, 0x03, 0x02, 0x06, 0x04, 0x05, 0x15, 0x52, 0x62}, 3862306a36Sopenharmony_ci {"SST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0x90, 0x20, 0x60}, 3962306a36Sopenharmony_ci {"ST", 0x01, 0x03, 0x02, 0x06, 0x04, 0x05, 0xAB, 0xD8, 0xC7}, 4062306a36Sopenharmony_ci}; 4162306a36Sopenharmony_ci 4262306a36Sopenharmony_cistatic int atlx_ioctl(struct net_device *netdev, struct ifreq *ifr, int cmd) 4362306a36Sopenharmony_ci{ 4462306a36Sopenharmony_ci switch (cmd) { 4562306a36Sopenharmony_ci case SIOCGMIIPHY: 4662306a36Sopenharmony_ci case SIOCGMIIREG: 4762306a36Sopenharmony_ci case SIOCSMIIREG: 4862306a36Sopenharmony_ci return atlx_mii_ioctl(netdev, ifr, cmd); 4962306a36Sopenharmony_ci default: 5062306a36Sopenharmony_ci return -EOPNOTSUPP; 5162306a36Sopenharmony_ci } 5262306a36Sopenharmony_ci} 5362306a36Sopenharmony_ci 5462306a36Sopenharmony_ci/** 5562306a36Sopenharmony_ci * atlx_set_mac - Change the Ethernet Address of the NIC 5662306a36Sopenharmony_ci * @netdev: network interface device structure 5762306a36Sopenharmony_ci * @p: pointer to an address structure 5862306a36Sopenharmony_ci * 5962306a36Sopenharmony_ci * Returns 0 on success, negative on failure 6062306a36Sopenharmony_ci */ 6162306a36Sopenharmony_cistatic int atlx_set_mac(struct net_device *netdev, void *p) 6262306a36Sopenharmony_ci{ 6362306a36Sopenharmony_ci struct atlx_adapter *adapter = netdev_priv(netdev); 6462306a36Sopenharmony_ci struct sockaddr *addr = p; 6562306a36Sopenharmony_ci 6662306a36Sopenharmony_ci if (netif_running(netdev)) 6762306a36Sopenharmony_ci return -EBUSY; 6862306a36Sopenharmony_ci 6962306a36Sopenharmony_ci if (!is_valid_ether_addr(addr->sa_data)) 7062306a36Sopenharmony_ci return -EADDRNOTAVAIL; 7162306a36Sopenharmony_ci 7262306a36Sopenharmony_ci eth_hw_addr_set(netdev, addr->sa_data); 7362306a36Sopenharmony_ci memcpy(adapter->hw.mac_addr, addr->sa_data, netdev->addr_len); 7462306a36Sopenharmony_ci 7562306a36Sopenharmony_ci atlx_set_mac_addr(&adapter->hw); 7662306a36Sopenharmony_ci return 0; 7762306a36Sopenharmony_ci} 7862306a36Sopenharmony_ci 7962306a36Sopenharmony_cistatic void atlx_check_for_link(struct atlx_adapter *adapter) 8062306a36Sopenharmony_ci{ 8162306a36Sopenharmony_ci struct net_device *netdev = adapter->netdev; 8262306a36Sopenharmony_ci u16 phy_data = 0; 8362306a36Sopenharmony_ci 8462306a36Sopenharmony_ci spin_lock(&adapter->lock); 8562306a36Sopenharmony_ci adapter->phy_timer_pending = false; 8662306a36Sopenharmony_ci atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data); 8762306a36Sopenharmony_ci atlx_read_phy_reg(&adapter->hw, MII_BMSR, &phy_data); 8862306a36Sopenharmony_ci spin_unlock(&adapter->lock); 8962306a36Sopenharmony_ci 9062306a36Sopenharmony_ci /* notify upper layer link down ASAP */ 9162306a36Sopenharmony_ci if (!(phy_data & BMSR_LSTATUS)) { 9262306a36Sopenharmony_ci /* Link Down */ 9362306a36Sopenharmony_ci if (netif_carrier_ok(netdev)) { 9462306a36Sopenharmony_ci /* old link state: Up */ 9562306a36Sopenharmony_ci dev_info(&adapter->pdev->dev, "%s link is down\n", 9662306a36Sopenharmony_ci netdev->name); 9762306a36Sopenharmony_ci adapter->link_speed = SPEED_0; 9862306a36Sopenharmony_ci netif_carrier_off(netdev); 9962306a36Sopenharmony_ci } 10062306a36Sopenharmony_ci } 10162306a36Sopenharmony_ci schedule_work(&adapter->link_chg_task); 10262306a36Sopenharmony_ci} 10362306a36Sopenharmony_ci 10462306a36Sopenharmony_ci/** 10562306a36Sopenharmony_ci * atlx_set_multi - Multicast and Promiscuous mode set 10662306a36Sopenharmony_ci * @netdev: network interface device structure 10762306a36Sopenharmony_ci * 10862306a36Sopenharmony_ci * The set_multi entry point is called whenever the multicast address 10962306a36Sopenharmony_ci * list or the network interface flags are updated. This routine is 11062306a36Sopenharmony_ci * responsible for configuring the hardware for proper multicast, 11162306a36Sopenharmony_ci * promiscuous mode, and all-multi behavior. 11262306a36Sopenharmony_ci */ 11362306a36Sopenharmony_cistatic void atlx_set_multi(struct net_device *netdev) 11462306a36Sopenharmony_ci{ 11562306a36Sopenharmony_ci struct atlx_adapter *adapter = netdev_priv(netdev); 11662306a36Sopenharmony_ci struct atlx_hw *hw = &adapter->hw; 11762306a36Sopenharmony_ci struct netdev_hw_addr *ha; 11862306a36Sopenharmony_ci u32 rctl; 11962306a36Sopenharmony_ci u32 hash_value; 12062306a36Sopenharmony_ci 12162306a36Sopenharmony_ci /* Check for Promiscuous and All Multicast modes */ 12262306a36Sopenharmony_ci rctl = ioread32(hw->hw_addr + REG_MAC_CTRL); 12362306a36Sopenharmony_ci if (netdev->flags & IFF_PROMISC) 12462306a36Sopenharmony_ci rctl |= MAC_CTRL_PROMIS_EN; 12562306a36Sopenharmony_ci else if (netdev->flags & IFF_ALLMULTI) { 12662306a36Sopenharmony_ci rctl |= MAC_CTRL_MC_ALL_EN; 12762306a36Sopenharmony_ci rctl &= ~MAC_CTRL_PROMIS_EN; 12862306a36Sopenharmony_ci } else 12962306a36Sopenharmony_ci rctl &= ~(MAC_CTRL_PROMIS_EN | MAC_CTRL_MC_ALL_EN); 13062306a36Sopenharmony_ci 13162306a36Sopenharmony_ci iowrite32(rctl, hw->hw_addr + REG_MAC_CTRL); 13262306a36Sopenharmony_ci 13362306a36Sopenharmony_ci /* clear the old settings from the multicast hash table */ 13462306a36Sopenharmony_ci iowrite32(0, hw->hw_addr + REG_RX_HASH_TABLE); 13562306a36Sopenharmony_ci iowrite32(0, (hw->hw_addr + REG_RX_HASH_TABLE) + (1 << 2)); 13662306a36Sopenharmony_ci 13762306a36Sopenharmony_ci /* compute mc addresses' hash value ,and put it into hash table */ 13862306a36Sopenharmony_ci netdev_for_each_mc_addr(ha, netdev) { 13962306a36Sopenharmony_ci hash_value = atlx_hash_mc_addr(hw, ha->addr); 14062306a36Sopenharmony_ci atlx_hash_set(hw, hash_value); 14162306a36Sopenharmony_ci } 14262306a36Sopenharmony_ci} 14362306a36Sopenharmony_ci 14462306a36Sopenharmony_cistatic inline void atlx_imr_set(struct atlx_adapter *adapter, 14562306a36Sopenharmony_ci unsigned int imr) 14662306a36Sopenharmony_ci{ 14762306a36Sopenharmony_ci iowrite32(imr, adapter->hw.hw_addr + REG_IMR); 14862306a36Sopenharmony_ci ioread32(adapter->hw.hw_addr + REG_IMR); 14962306a36Sopenharmony_ci} 15062306a36Sopenharmony_ci 15162306a36Sopenharmony_ci/** 15262306a36Sopenharmony_ci * atlx_irq_enable - Enable default interrupt generation settings 15362306a36Sopenharmony_ci * @adapter: board private structure 15462306a36Sopenharmony_ci */ 15562306a36Sopenharmony_cistatic void atlx_irq_enable(struct atlx_adapter *adapter) 15662306a36Sopenharmony_ci{ 15762306a36Sopenharmony_ci atlx_imr_set(adapter, IMR_NORMAL_MASK); 15862306a36Sopenharmony_ci adapter->int_enabled = true; 15962306a36Sopenharmony_ci} 16062306a36Sopenharmony_ci 16162306a36Sopenharmony_ci/** 16262306a36Sopenharmony_ci * atlx_irq_disable - Mask off interrupt generation on the NIC 16362306a36Sopenharmony_ci * @adapter: board private structure 16462306a36Sopenharmony_ci */ 16562306a36Sopenharmony_cistatic void atlx_irq_disable(struct atlx_adapter *adapter) 16662306a36Sopenharmony_ci{ 16762306a36Sopenharmony_ci adapter->int_enabled = false; 16862306a36Sopenharmony_ci atlx_imr_set(adapter, 0); 16962306a36Sopenharmony_ci synchronize_irq(adapter->pdev->irq); 17062306a36Sopenharmony_ci} 17162306a36Sopenharmony_ci 17262306a36Sopenharmony_cistatic void atlx_clear_phy_int(struct atlx_adapter *adapter) 17362306a36Sopenharmony_ci{ 17462306a36Sopenharmony_ci u16 phy_data; 17562306a36Sopenharmony_ci unsigned long flags; 17662306a36Sopenharmony_ci 17762306a36Sopenharmony_ci spin_lock_irqsave(&adapter->lock, flags); 17862306a36Sopenharmony_ci atlx_read_phy_reg(&adapter->hw, 19, &phy_data); 17962306a36Sopenharmony_ci spin_unlock_irqrestore(&adapter->lock, flags); 18062306a36Sopenharmony_ci} 18162306a36Sopenharmony_ci 18262306a36Sopenharmony_ci/** 18362306a36Sopenharmony_ci * atlx_tx_timeout - Respond to a Tx Hang 18462306a36Sopenharmony_ci * @netdev: network interface device structure 18562306a36Sopenharmony_ci */ 18662306a36Sopenharmony_cistatic void atlx_tx_timeout(struct net_device *netdev, unsigned int txqueue) 18762306a36Sopenharmony_ci{ 18862306a36Sopenharmony_ci struct atlx_adapter *adapter = netdev_priv(netdev); 18962306a36Sopenharmony_ci /* Do the reset outside of interrupt context */ 19062306a36Sopenharmony_ci schedule_work(&adapter->reset_dev_task); 19162306a36Sopenharmony_ci} 19262306a36Sopenharmony_ci 19362306a36Sopenharmony_ci/* 19462306a36Sopenharmony_ci * atlx_link_chg_task - deal with link change event Out of interrupt context 19562306a36Sopenharmony_ci */ 19662306a36Sopenharmony_cistatic void atlx_link_chg_task(struct work_struct *work) 19762306a36Sopenharmony_ci{ 19862306a36Sopenharmony_ci struct atlx_adapter *adapter; 19962306a36Sopenharmony_ci unsigned long flags; 20062306a36Sopenharmony_ci 20162306a36Sopenharmony_ci adapter = container_of(work, struct atlx_adapter, link_chg_task); 20262306a36Sopenharmony_ci 20362306a36Sopenharmony_ci spin_lock_irqsave(&adapter->lock, flags); 20462306a36Sopenharmony_ci atlx_check_link(adapter); 20562306a36Sopenharmony_ci spin_unlock_irqrestore(&adapter->lock, flags); 20662306a36Sopenharmony_ci} 20762306a36Sopenharmony_ci 20862306a36Sopenharmony_cistatic void __atlx_vlan_mode(netdev_features_t features, u32 *ctrl) 20962306a36Sopenharmony_ci{ 21062306a36Sopenharmony_ci if (features & NETIF_F_HW_VLAN_CTAG_RX) { 21162306a36Sopenharmony_ci /* enable VLAN tag insert/strip */ 21262306a36Sopenharmony_ci *ctrl |= MAC_CTRL_RMV_VLAN; 21362306a36Sopenharmony_ci } else { 21462306a36Sopenharmony_ci /* disable VLAN tag insert/strip */ 21562306a36Sopenharmony_ci *ctrl &= ~MAC_CTRL_RMV_VLAN; 21662306a36Sopenharmony_ci } 21762306a36Sopenharmony_ci} 21862306a36Sopenharmony_ci 21962306a36Sopenharmony_cistatic void atlx_vlan_mode(struct net_device *netdev, 22062306a36Sopenharmony_ci netdev_features_t features) 22162306a36Sopenharmony_ci{ 22262306a36Sopenharmony_ci struct atlx_adapter *adapter = netdev_priv(netdev); 22362306a36Sopenharmony_ci unsigned long flags; 22462306a36Sopenharmony_ci u32 ctrl; 22562306a36Sopenharmony_ci 22662306a36Sopenharmony_ci spin_lock_irqsave(&adapter->lock, flags); 22762306a36Sopenharmony_ci /* atlx_irq_disable(adapter); FIXME: confirm/remove */ 22862306a36Sopenharmony_ci ctrl = ioread32(adapter->hw.hw_addr + REG_MAC_CTRL); 22962306a36Sopenharmony_ci __atlx_vlan_mode(features, &ctrl); 23062306a36Sopenharmony_ci iowrite32(ctrl, adapter->hw.hw_addr + REG_MAC_CTRL); 23162306a36Sopenharmony_ci /* atlx_irq_enable(adapter); FIXME */ 23262306a36Sopenharmony_ci spin_unlock_irqrestore(&adapter->lock, flags); 23362306a36Sopenharmony_ci} 23462306a36Sopenharmony_ci 23562306a36Sopenharmony_cistatic void atlx_restore_vlan(struct atlx_adapter *adapter) 23662306a36Sopenharmony_ci{ 23762306a36Sopenharmony_ci atlx_vlan_mode(adapter->netdev, adapter->netdev->features); 23862306a36Sopenharmony_ci} 23962306a36Sopenharmony_ci 24062306a36Sopenharmony_cistatic netdev_features_t atlx_fix_features(struct net_device *netdev, 24162306a36Sopenharmony_ci netdev_features_t features) 24262306a36Sopenharmony_ci{ 24362306a36Sopenharmony_ci /* 24462306a36Sopenharmony_ci * Since there is no support for separate rx/tx vlan accel 24562306a36Sopenharmony_ci * enable/disable make sure tx flag is always in same state as rx. 24662306a36Sopenharmony_ci */ 24762306a36Sopenharmony_ci if (features & NETIF_F_HW_VLAN_CTAG_RX) 24862306a36Sopenharmony_ci features |= NETIF_F_HW_VLAN_CTAG_TX; 24962306a36Sopenharmony_ci else 25062306a36Sopenharmony_ci features &= ~NETIF_F_HW_VLAN_CTAG_TX; 25162306a36Sopenharmony_ci 25262306a36Sopenharmony_ci return features; 25362306a36Sopenharmony_ci} 25462306a36Sopenharmony_ci 25562306a36Sopenharmony_cistatic int atlx_set_features(struct net_device *netdev, 25662306a36Sopenharmony_ci netdev_features_t features) 25762306a36Sopenharmony_ci{ 25862306a36Sopenharmony_ci netdev_features_t changed = netdev->features ^ features; 25962306a36Sopenharmony_ci 26062306a36Sopenharmony_ci if (changed & NETIF_F_HW_VLAN_CTAG_RX) 26162306a36Sopenharmony_ci atlx_vlan_mode(netdev, features); 26262306a36Sopenharmony_ci 26362306a36Sopenharmony_ci return 0; 26462306a36Sopenharmony_ci} 26562306a36Sopenharmony_ci 26662306a36Sopenharmony_ci#endif /* ATLX_C */ 267