18c2ecf20Sopenharmony_ci/* SPDX-License-Identifier: GPL-2.0-or-later */
28c2ecf20Sopenharmony_ci/*
38c2ecf20Sopenharmony_ci * ASIX AX8817X based USB 2.0 Ethernet Devices
48c2ecf20Sopenharmony_ci * Copyright (C) 2003-2006 David Hollis <dhollis@davehollis.com>
58c2ecf20Sopenharmony_ci * Copyright (C) 2005 Phil Chang <pchang23@sbcglobal.net>
68c2ecf20Sopenharmony_ci * Copyright (C) 2006 James Painter <jamie.painter@iname.com>
78c2ecf20Sopenharmony_ci * Copyright (c) 2002-2003 TiVo Inc.
88c2ecf20Sopenharmony_ci */
98c2ecf20Sopenharmony_ci
108c2ecf20Sopenharmony_ci#ifndef _ASIX_H
118c2ecf20Sopenharmony_ci#define _ASIX_H
128c2ecf20Sopenharmony_ci
138c2ecf20Sopenharmony_ci// #define	DEBUG			// error path messages, extra info
148c2ecf20Sopenharmony_ci// #define	VERBOSE			// more; success messages
158c2ecf20Sopenharmony_ci
168c2ecf20Sopenharmony_ci#include <linux/module.h>
178c2ecf20Sopenharmony_ci#include <linux/kmod.h>
188c2ecf20Sopenharmony_ci#include <linux/netdevice.h>
198c2ecf20Sopenharmony_ci#include <linux/etherdevice.h>
208c2ecf20Sopenharmony_ci#include <linux/ethtool.h>
218c2ecf20Sopenharmony_ci#include <linux/workqueue.h>
228c2ecf20Sopenharmony_ci#include <linux/mii.h>
238c2ecf20Sopenharmony_ci#include <linux/usb.h>
248c2ecf20Sopenharmony_ci#include <linux/crc32.h>
258c2ecf20Sopenharmony_ci#include <linux/usb/usbnet.h>
268c2ecf20Sopenharmony_ci#include <linux/slab.h>
278c2ecf20Sopenharmony_ci#include <linux/if_vlan.h>
288c2ecf20Sopenharmony_ci
298c2ecf20Sopenharmony_ci#define DRIVER_VERSION "22-Dec-2011"
308c2ecf20Sopenharmony_ci#define DRIVER_NAME "asix"
318c2ecf20Sopenharmony_ci
328c2ecf20Sopenharmony_ci/* ASIX AX8817X based USB 2.0 Ethernet Devices */
338c2ecf20Sopenharmony_ci
348c2ecf20Sopenharmony_ci#define AX_CMD_SET_SW_MII		0x06
358c2ecf20Sopenharmony_ci#define AX_CMD_READ_MII_REG		0x07
368c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_MII_REG		0x08
378c2ecf20Sopenharmony_ci#define AX_CMD_STATMNGSTS_REG		0x09
388c2ecf20Sopenharmony_ci#define AX_CMD_SET_HW_MII		0x0a
398c2ecf20Sopenharmony_ci#define AX_CMD_READ_EEPROM		0x0b
408c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_EEPROM		0x0c
418c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_ENABLE		0x0d
428c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_DISABLE		0x0e
438c2ecf20Sopenharmony_ci#define AX_CMD_READ_RX_CTL		0x0f
448c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_RX_CTL		0x10
458c2ecf20Sopenharmony_ci#define AX_CMD_READ_IPG012		0x11
468c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_IPG0		0x12
478c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_IPG1		0x13
488c2ecf20Sopenharmony_ci#define AX_CMD_READ_NODE_ID		0x13
498c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_NODE_ID		0x14
508c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_IPG2		0x14
518c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_MULTI_FILTER	0x16
528c2ecf20Sopenharmony_ci#define AX88172_CMD_READ_NODE_ID	0x17
538c2ecf20Sopenharmony_ci#define AX_CMD_READ_PHY_ID		0x19
548c2ecf20Sopenharmony_ci#define AX_CMD_READ_MEDIUM_STATUS	0x1a
558c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_MEDIUM_MODE	0x1b
568c2ecf20Sopenharmony_ci#define AX_CMD_READ_MONITOR_MODE	0x1c
578c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_MONITOR_MODE	0x1d
588c2ecf20Sopenharmony_ci#define AX_CMD_READ_GPIOS		0x1e
598c2ecf20Sopenharmony_ci#define AX_CMD_WRITE_GPIOS		0x1f
608c2ecf20Sopenharmony_ci#define AX_CMD_SW_RESET			0x20
618c2ecf20Sopenharmony_ci#define AX_CMD_SW_PHY_STATUS		0x21
628c2ecf20Sopenharmony_ci#define AX_CMD_SW_PHY_SELECT		0x22
638c2ecf20Sopenharmony_ci#define AX_QCTCTRL			0x2A
648c2ecf20Sopenharmony_ci
658c2ecf20Sopenharmony_ci#define AX_CHIPCODE_MASK		0x70
668c2ecf20Sopenharmony_ci#define AX_AX88772_CHIPCODE		0x00
678c2ecf20Sopenharmony_ci#define AX_AX88772A_CHIPCODE		0x10
688c2ecf20Sopenharmony_ci#define AX_AX88772B_CHIPCODE		0x20
698c2ecf20Sopenharmony_ci#define AX_HOST_EN			0x01
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci#define AX_PHYSEL_PSEL			0x01
728c2ecf20Sopenharmony_ci#define AX_PHYSEL_SSMII			0
738c2ecf20Sopenharmony_ci#define AX_PHYSEL_SSEN			0x10
748c2ecf20Sopenharmony_ci
758c2ecf20Sopenharmony_ci#define AX_PHY_SELECT_MASK		(BIT(3) | BIT(2))
768c2ecf20Sopenharmony_ci#define AX_PHY_SELECT_INTERNAL		0
778c2ecf20Sopenharmony_ci#define AX_PHY_SELECT_EXTERNAL		BIT(2)
788c2ecf20Sopenharmony_ci
798c2ecf20Sopenharmony_ci#define AX_MONITOR_MODE			0x01
808c2ecf20Sopenharmony_ci#define AX_MONITOR_LINK			0x02
818c2ecf20Sopenharmony_ci#define AX_MONITOR_MAGIC		0x04
828c2ecf20Sopenharmony_ci#define AX_MONITOR_HSFS			0x10
838c2ecf20Sopenharmony_ci
848c2ecf20Sopenharmony_ci/* AX88172 Medium Status Register values */
858c2ecf20Sopenharmony_ci#define AX88172_MEDIUM_FD		0x02
868c2ecf20Sopenharmony_ci#define AX88172_MEDIUM_TX		0x04
878c2ecf20Sopenharmony_ci#define AX88172_MEDIUM_FC		0x10
888c2ecf20Sopenharmony_ci#define AX88172_MEDIUM_DEFAULT \
898c2ecf20Sopenharmony_ci		( AX88172_MEDIUM_FD | AX88172_MEDIUM_TX | AX88172_MEDIUM_FC )
908c2ecf20Sopenharmony_ci
918c2ecf20Sopenharmony_ci#define AX_MCAST_FILTER_SIZE		8
928c2ecf20Sopenharmony_ci#define AX_MAX_MCAST			64
938c2ecf20Sopenharmony_ci
948c2ecf20Sopenharmony_ci#define AX_SWRESET_CLEAR		0x00
958c2ecf20Sopenharmony_ci#define AX_SWRESET_RR			0x01
968c2ecf20Sopenharmony_ci#define AX_SWRESET_RT			0x02
978c2ecf20Sopenharmony_ci#define AX_SWRESET_PRTE			0x04
988c2ecf20Sopenharmony_ci#define AX_SWRESET_PRL			0x08
998c2ecf20Sopenharmony_ci#define AX_SWRESET_BZ			0x10
1008c2ecf20Sopenharmony_ci#define AX_SWRESET_IPRL			0x20
1018c2ecf20Sopenharmony_ci#define AX_SWRESET_IPPD			0x40
1028c2ecf20Sopenharmony_ci
1038c2ecf20Sopenharmony_ci#define AX88772_IPG0_DEFAULT		0x15
1048c2ecf20Sopenharmony_ci#define AX88772_IPG1_DEFAULT		0x0c
1058c2ecf20Sopenharmony_ci#define AX88772_IPG2_DEFAULT		0x12
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_ci/* AX88772 & AX88178 Medium Mode Register */
1088c2ecf20Sopenharmony_ci#define AX_MEDIUM_PF		0x0080
1098c2ecf20Sopenharmony_ci#define AX_MEDIUM_JFE		0x0040
1108c2ecf20Sopenharmony_ci#define AX_MEDIUM_TFC		0x0020
1118c2ecf20Sopenharmony_ci#define AX_MEDIUM_RFC		0x0010
1128c2ecf20Sopenharmony_ci#define AX_MEDIUM_ENCK		0x0008
1138c2ecf20Sopenharmony_ci#define AX_MEDIUM_AC		0x0004
1148c2ecf20Sopenharmony_ci#define AX_MEDIUM_FD		0x0002
1158c2ecf20Sopenharmony_ci#define AX_MEDIUM_GM		0x0001
1168c2ecf20Sopenharmony_ci#define AX_MEDIUM_SM		0x1000
1178c2ecf20Sopenharmony_ci#define AX_MEDIUM_SBP		0x0800
1188c2ecf20Sopenharmony_ci#define AX_MEDIUM_PS		0x0200
1198c2ecf20Sopenharmony_ci#define AX_MEDIUM_RE		0x0100
1208c2ecf20Sopenharmony_ci
1218c2ecf20Sopenharmony_ci#define AX88178_MEDIUM_DEFAULT	\
1228c2ecf20Sopenharmony_ci	(AX_MEDIUM_PS | AX_MEDIUM_FD | AX_MEDIUM_AC | \
1238c2ecf20Sopenharmony_ci	 AX_MEDIUM_RFC | AX_MEDIUM_TFC | AX_MEDIUM_JFE | \
1248c2ecf20Sopenharmony_ci	 AX_MEDIUM_RE)
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci#define AX88772_MEDIUM_DEFAULT	\
1278c2ecf20Sopenharmony_ci	(AX_MEDIUM_FD | AX_MEDIUM_RFC | \
1288c2ecf20Sopenharmony_ci	 AX_MEDIUM_TFC | AX_MEDIUM_PS | \
1298c2ecf20Sopenharmony_ci	 AX_MEDIUM_AC | AX_MEDIUM_RE)
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci/* AX88772 & AX88178 RX_CTL values */
1328c2ecf20Sopenharmony_ci#define AX_RX_CTL_SO		0x0080
1338c2ecf20Sopenharmony_ci#define AX_RX_CTL_AP		0x0020
1348c2ecf20Sopenharmony_ci#define AX_RX_CTL_AM		0x0010
1358c2ecf20Sopenharmony_ci#define AX_RX_CTL_AB		0x0008
1368c2ecf20Sopenharmony_ci#define AX_RX_CTL_SEP		0x0004
1378c2ecf20Sopenharmony_ci#define AX_RX_CTL_AMALL		0x0002
1388c2ecf20Sopenharmony_ci#define AX_RX_CTL_PRO		0x0001
1398c2ecf20Sopenharmony_ci#define AX_RX_CTL_MFB_2048	0x0000
1408c2ecf20Sopenharmony_ci#define AX_RX_CTL_MFB_4096	0x0100
1418c2ecf20Sopenharmony_ci#define AX_RX_CTL_MFB_8192	0x0200
1428c2ecf20Sopenharmony_ci#define AX_RX_CTL_MFB_16384	0x0300
1438c2ecf20Sopenharmony_ci
1448c2ecf20Sopenharmony_ci#define AX_DEFAULT_RX_CTL	(AX_RX_CTL_SO | AX_RX_CTL_AB)
1458c2ecf20Sopenharmony_ci
1468c2ecf20Sopenharmony_ci/* GPIO 0 .. 2 toggles */
1478c2ecf20Sopenharmony_ci#define AX_GPIO_GPO0EN		0x01	/* GPIO0 Output enable */
1488c2ecf20Sopenharmony_ci#define AX_GPIO_GPO_0		0x02	/* GPIO0 Output value */
1498c2ecf20Sopenharmony_ci#define AX_GPIO_GPO1EN		0x04	/* GPIO1 Output enable */
1508c2ecf20Sopenharmony_ci#define AX_GPIO_GPO_1		0x08	/* GPIO1 Output value */
1518c2ecf20Sopenharmony_ci#define AX_GPIO_GPO2EN		0x10	/* GPIO2 Output enable */
1528c2ecf20Sopenharmony_ci#define AX_GPIO_GPO_2		0x20	/* GPIO2 Output value */
1538c2ecf20Sopenharmony_ci#define AX_GPIO_RESERVED	0x40	/* Reserved */
1548c2ecf20Sopenharmony_ci#define AX_GPIO_RSE		0x80	/* Reload serial EEPROM */
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci#define AX_EEPROM_MAGIC		0xdeadbeef
1578c2ecf20Sopenharmony_ci#define AX_EEPROM_LEN		0x200
1588c2ecf20Sopenharmony_ci
1598c2ecf20Sopenharmony_ci/* This structure cannot exceed sizeof(unsigned long [5]) AKA 20 bytes */
1608c2ecf20Sopenharmony_cistruct asix_data {
1618c2ecf20Sopenharmony_ci	u8 multi_filter[AX_MCAST_FILTER_SIZE];
1628c2ecf20Sopenharmony_ci	u8 mac_addr[ETH_ALEN];
1638c2ecf20Sopenharmony_ci	u8 phymode;
1648c2ecf20Sopenharmony_ci	u8 ledmode;
1658c2ecf20Sopenharmony_ci	u8 res;
1668c2ecf20Sopenharmony_ci};
1678c2ecf20Sopenharmony_ci
1688c2ecf20Sopenharmony_cistruct asix_rx_fixup_info {
1698c2ecf20Sopenharmony_ci	struct sk_buff *ax_skb;
1708c2ecf20Sopenharmony_ci	u32 header;
1718c2ecf20Sopenharmony_ci	u16 remaining;
1728c2ecf20Sopenharmony_ci	bool split_head;
1738c2ecf20Sopenharmony_ci};
1748c2ecf20Sopenharmony_ci
1758c2ecf20Sopenharmony_cistruct asix_common_private {
1768c2ecf20Sopenharmony_ci	void (*resume)(struct usbnet *dev);
1778c2ecf20Sopenharmony_ci	void (*suspend)(struct usbnet *dev);
1788c2ecf20Sopenharmony_ci	u16 presvd_phy_advertise;
1798c2ecf20Sopenharmony_ci	u16 presvd_phy_bmcr;
1808c2ecf20Sopenharmony_ci	struct asix_rx_fixup_info rx_fixup_info;
1818c2ecf20Sopenharmony_ci};
1828c2ecf20Sopenharmony_ci
1838c2ecf20Sopenharmony_ciextern const struct driver_info ax88172a_info;
1848c2ecf20Sopenharmony_ci
1858c2ecf20Sopenharmony_ci/* ASIX specific flags */
1868c2ecf20Sopenharmony_ci#define FLAG_EEPROM_MAC		(1UL << 0)  /* init device MAC from eeprom */
1878c2ecf20Sopenharmony_ci
1888c2ecf20Sopenharmony_ciint asix_read_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
1898c2ecf20Sopenharmony_ci		  u16 size, void *data, int in_pm);
1908c2ecf20Sopenharmony_ci
1918c2ecf20Sopenharmony_ciint asix_write_cmd(struct usbnet *dev, u8 cmd, u16 value, u16 index,
1928c2ecf20Sopenharmony_ci		   u16 size, void *data, int in_pm);
1938c2ecf20Sopenharmony_ci
1948c2ecf20Sopenharmony_civoid asix_write_cmd_async(struct usbnet *dev, u8 cmd, u16 value,
1958c2ecf20Sopenharmony_ci			  u16 index, u16 size, void *data);
1968c2ecf20Sopenharmony_ci
1978c2ecf20Sopenharmony_ciint asix_rx_fixup_internal(struct usbnet *dev, struct sk_buff *skb,
1988c2ecf20Sopenharmony_ci			   struct asix_rx_fixup_info *rx);
1998c2ecf20Sopenharmony_ciint asix_rx_fixup_common(struct usbnet *dev, struct sk_buff *skb);
2008c2ecf20Sopenharmony_civoid asix_rx_fixup_common_free(struct asix_common_private *dp);
2018c2ecf20Sopenharmony_ci
2028c2ecf20Sopenharmony_cistruct sk_buff *asix_tx_fixup(struct usbnet *dev, struct sk_buff *skb,
2038c2ecf20Sopenharmony_ci			      gfp_t flags);
2048c2ecf20Sopenharmony_ci
2058c2ecf20Sopenharmony_ciint asix_set_sw_mii(struct usbnet *dev, int in_pm);
2068c2ecf20Sopenharmony_ciint asix_set_hw_mii(struct usbnet *dev, int in_pm);
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ciint asix_read_phy_addr(struct usbnet *dev, int internal);
2098c2ecf20Sopenharmony_ciint asix_get_phy_addr(struct usbnet *dev);
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciint asix_sw_reset(struct usbnet *dev, u8 flags, int in_pm);
2128c2ecf20Sopenharmony_ci
2138c2ecf20Sopenharmony_ciu16 asix_read_rx_ctl(struct usbnet *dev, int in_pm);
2148c2ecf20Sopenharmony_ciint asix_write_rx_ctl(struct usbnet *dev, u16 mode, int in_pm);
2158c2ecf20Sopenharmony_ci
2168c2ecf20Sopenharmony_ciu16 asix_read_medium_status(struct usbnet *dev, int in_pm);
2178c2ecf20Sopenharmony_ciint asix_write_medium_mode(struct usbnet *dev, u16 mode, int in_pm);
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ciint asix_write_gpio(struct usbnet *dev, u16 value, int sleep, int in_pm);
2208c2ecf20Sopenharmony_ci
2218c2ecf20Sopenharmony_civoid asix_set_multicast(struct net_device *net);
2228c2ecf20Sopenharmony_ci
2238c2ecf20Sopenharmony_ciint asix_mdio_read(struct net_device *netdev, int phy_id, int loc);
2248c2ecf20Sopenharmony_civoid asix_mdio_write(struct net_device *netdev, int phy_id, int loc, int val);
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ciint asix_mdio_read_nopm(struct net_device *netdev, int phy_id, int loc);
2278c2ecf20Sopenharmony_civoid asix_mdio_write_nopm(struct net_device *netdev, int phy_id, int loc,
2288c2ecf20Sopenharmony_ci			  int val);
2298c2ecf20Sopenharmony_ci
2308c2ecf20Sopenharmony_civoid asix_get_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo);
2318c2ecf20Sopenharmony_ciint asix_set_wol(struct net_device *net, struct ethtool_wolinfo *wolinfo);
2328c2ecf20Sopenharmony_ci
2338c2ecf20Sopenharmony_ciint asix_get_eeprom_len(struct net_device *net);
2348c2ecf20Sopenharmony_ciint asix_get_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
2358c2ecf20Sopenharmony_ci		    u8 *data);
2368c2ecf20Sopenharmony_ciint asix_set_eeprom(struct net_device *net, struct ethtool_eeprom *eeprom,
2378c2ecf20Sopenharmony_ci		    u8 *data);
2388c2ecf20Sopenharmony_ci
2398c2ecf20Sopenharmony_civoid asix_get_drvinfo(struct net_device *net, struct ethtool_drvinfo *info);
2408c2ecf20Sopenharmony_ci
2418c2ecf20Sopenharmony_ciint asix_set_mac_address(struct net_device *net, void *p);
2428c2ecf20Sopenharmony_ci
2438c2ecf20Sopenharmony_ci#endif /* _ASIX_H */
244