1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2017 Microchip Technology Inc. All rights reserved.
9 #include <linux/bitops.h>
10 #include <linux/delay.h>
11 #include <linux/errno.h>
13 /* USB Vendor Requests */
14 #define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
15 #define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
16 #define USB_VENDOR_REQUEST_GET_STATS 0xA2
19 #define TX_CMD_A_FCS BIT(22)
20 #define TX_CMD_A_LEN_MASK 0x000FFFFF
23 #define RX_CMD_A_RXE BIT(18)
24 #define RX_CMD_A_LEN_MASK 0x00003FFF
28 #define ID_REV_CHIP_ID_MASK 0xFFFF0000
29 #define ID_REV_CHIP_ID_7500 0x7500
30 #define ID_REV_CHIP_ID_7800 0x7800
31 #define ID_REV_CHIP_ID_7850 0x7850
36 #define HW_CFG_LRST BIT(1)
39 #define PMT_CTL_PHY_PWRUP BIT(10)
40 #define PMT_CTL_READY BIT(7)
41 #define PMT_CTL_PHY_RST BIT(4)
44 #define E2P_CMD_EPC_BUSY BIT(31)
45 #define E2P_CMD_EPC_CMD_READ 0x00000000
46 #define E2P_CMD_EPC_TIMEOUT BIT(10)
47 #define E2P_CMD_EPC_ADDR_MASK 0x000001FF
49 #define E2P_DATA 0x044
51 #define RFE_CTL_BCAST_EN BIT(10)
52 #define RFE_CTL_DA_PERFECT BIT(1)
54 #define FCT_RX_CTL_EN BIT(31)
56 #define FCT_TX_CTL_EN BIT(31)
59 #define MAC_CR_ADP BIT(13)
60 #define MAC_CR_AUTO_DUPLEX BIT(12)
61 #define MAC_CR_AUTO_SPEED BIT(11)
64 #define MAC_RX_FCS_STRIP BIT(4)
65 #define MAC_RX_RXEN BIT(0)
68 #define MAC_TX_TXEN BIT(0)
71 #define FLOW_CR_TX_FCEN BIT(30)
72 #define FLOW_CR_RX_FCEN BIT(29)
74 #define RX_ADDRH 0x118
75 #define RX_ADDRL 0x11C
78 #define MII_ACC_MII_READ 0x00000000
79 #define MII_ACC_MII_WRITE 0x00000002
80 #define MII_ACC_MII_BUSY BIT(0)
82 #define MII_DATA 0x124
84 #define SS_USB_PKT_SIZE 1024
85 #define HS_USB_PKT_SIZE 512
86 #define FS_USB_PKT_SIZE 64
88 #define MAX_RX_FIFO_SIZE (12 * 1024)
89 #define MAX_TX_FIFO_SIZE (12 * 1024)
90 #define DEFAULT_BULK_IN_DELAY 0x0800
92 #define EEPROM_INDICATOR 0xA5
93 #define EEPROM_MAC_OFFSET 0x01
95 /* Some extra defines */
96 #define LAN7X_INTERNAL_PHY_ID 1
98 #define LAN7X_MAC_RX_MAX_SIZE(mtu) \
99 ((mtu) << 16) /* Max frame size */
100 #define LAN7X_MAC_RX_MAX_SIZE_DEFAULT \
101 LAN7X_MAC_RX_MAX_SIZE(PKTSIZE_ALIGN + 4 /* VLAN */ + 4 /* CRC */)
104 #define USB_CTRL_SET_TIMEOUT_MS 5000
105 #define USB_CTRL_GET_TIMEOUT_MS 5000
106 #define USB_BULK_SEND_TIMEOUT_MS 5000
107 #define USB_BULK_RECV_TIMEOUT_MS 5000
108 #define TIMEOUT_RESOLUTION_MS 50
109 #define PHY_CONNECT_TIMEOUT_MS 5000
111 #define RX_URB_SIZE 2048
114 struct lan7x_private {
115 struct ueth_data ueth;
116 u32 chipid; /* Chip or device ID */
117 struct mii_dev *mdiobus;
118 struct phy_device *phydev;
122 * Lan7x infrastructure commands
125 int lan7x_write_reg(struct usb_device *udev, u32 index, u32 data);
127 int lan7x_read_reg(struct usb_device *udev, u32 index, u32 *data);
130 * FIXME: Code should not be in header files. Nive this to a file common to
133 static inline int lan7x_wait_for_bit(struct usb_device *udev,
134 const char *prefix, const u32 reg,
135 const u32 mask, const bool set,
136 const unsigned int timeout_ms,
137 const bool breakable)
140 unsigned long start = get_timer(0);
143 lan7x_read_reg(udev, reg, &val);
148 if ((val & mask) == mask)
151 if (get_timer(start) > timeout_ms)
154 if (breakable && ctrlc()) {
163 debug("%s: Timeout (reg=0x%x mask=%08x wait_set=%i)\n", prefix, reg,
169 int lan7x_mdio_read(struct usb_device *udev, int phy_id, int idx);
171 void lan7x_mdio_write(struct usb_device *udev, int phy_id, int idx,
174 static inline int lan7x_mdio_wait_for_bit(struct usb_device *udev,
176 int phy_id, const u32 reg,
177 const u32 mask, const bool set,
178 const unsigned int timeout_ms,
179 const bool breakable)
182 unsigned long start = get_timer(0);
185 val = lan7x_mdio_read(udev, phy_id, reg);
190 if ((val & mask) == mask)
193 if (get_timer(start) > timeout_ms)
196 if (breakable && ctrlc()) {
205 debug("%s: Timeout (reg=0x%x mask=%08x wait_set=%i)\n", prefix, reg,
211 int lan7x_phylib_register(struct udevice *udev);
213 int lan7x_eth_phylib_connect(struct udevice *udev, struct ueth_data *dev);
215 int lan7x_eth_phylib_config_start(struct udevice *udev);
217 int lan7x_pmt_phy_reset(struct usb_device *udev,
218 struct ueth_data *dev);
220 int lan7x_update_flowcontrol(struct usb_device *udev,
221 struct ueth_data *dev,
222 uint32_t *flow, uint32_t *fct_flow);
224 int lan7x_read_eeprom_mac(unsigned char *enetaddr, struct usb_device *udev);
226 int lan7x_basic_reset(struct usb_device *udev,
227 struct ueth_data *dev);
229 void lan7x_eth_stop(struct udevice *dev);
231 int lan7x_eth_send(struct udevice *dev, void *packet, int length);
233 int lan7x_eth_recv(struct udevice *dev, int flags, uchar **packetp);
235 int lan7x_free_pkt(struct udevice *dev, uchar *packet, int packet_len);
237 int lan7x_eth_remove(struct udevice *dev);