1 /* SPDX-License-Identifier: GPL-2.0+ */
3 * Copyright (c) 2017 Microchip Technology Inc. All rights reserved.
9 #include <linux/delay.h>
10 #include <linux/errno.h>
12 /* USB Vendor Requests */
13 #define USB_VENDOR_REQUEST_WRITE_REGISTER 0xA0
14 #define USB_VENDOR_REQUEST_READ_REGISTER 0xA1
15 #define USB_VENDOR_REQUEST_GET_STATS 0xA2
18 #define TX_CMD_A_FCS BIT(22)
19 #define TX_CMD_A_LEN_MASK 0x000FFFFF
22 #define RX_CMD_A_RXE BIT(18)
23 #define RX_CMD_A_LEN_MASK 0x00003FFF
27 #define ID_REV_CHIP_ID_MASK 0xFFFF0000
28 #define ID_REV_CHIP_ID_7500 0x7500
29 #define ID_REV_CHIP_ID_7800 0x7800
30 #define ID_REV_CHIP_ID_7850 0x7850
35 #define HW_CFG_LRST BIT(1)
38 #define PMT_CTL_PHY_PWRUP BIT(10)
39 #define PMT_CTL_READY BIT(7)
40 #define PMT_CTL_PHY_RST BIT(4)
43 #define E2P_CMD_EPC_BUSY BIT(31)
44 #define E2P_CMD_EPC_CMD_READ 0x00000000
45 #define E2P_CMD_EPC_TIMEOUT BIT(10)
46 #define E2P_CMD_EPC_ADDR_MASK 0x000001FF
48 #define E2P_DATA 0x044
50 #define RFE_CTL_BCAST_EN BIT(10)
51 #define RFE_CTL_DA_PERFECT BIT(1)
53 #define FCT_RX_CTL_EN BIT(31)
55 #define FCT_TX_CTL_EN BIT(31)
58 #define MAC_CR_ADP BIT(13)
59 #define MAC_CR_AUTO_DUPLEX BIT(12)
60 #define MAC_CR_AUTO_SPEED BIT(11)
63 #define MAC_RX_FCS_STRIP BIT(4)
64 #define MAC_RX_RXEN BIT(0)
67 #define MAC_TX_TXEN BIT(0)
70 #define FLOW_CR_TX_FCEN BIT(30)
71 #define FLOW_CR_RX_FCEN BIT(29)
73 #define RX_ADDRH 0x118
74 #define RX_ADDRL 0x11C
77 #define MII_ACC_MII_READ 0x00000000
78 #define MII_ACC_MII_WRITE 0x00000002
79 #define MII_ACC_MII_BUSY BIT(0)
81 #define MII_DATA 0x124
83 #define SS_USB_PKT_SIZE 1024
84 #define HS_USB_PKT_SIZE 512
85 #define FS_USB_PKT_SIZE 64
87 #define MAX_RX_FIFO_SIZE (12 * 1024)
88 #define MAX_TX_FIFO_SIZE (12 * 1024)
89 #define DEFAULT_BULK_IN_DELAY 0x0800
91 #define EEPROM_INDICATOR 0xA5
92 #define EEPROM_MAC_OFFSET 0x01
94 /* Some extra defines */
95 #define LAN7X_INTERNAL_PHY_ID 1
97 #define LAN7X_MAC_RX_MAX_SIZE(mtu) \
98 ((mtu) << 16) /* Max frame size */
99 #define LAN7X_MAC_RX_MAX_SIZE_DEFAULT \
100 LAN7X_MAC_RX_MAX_SIZE(PKTSIZE_ALIGN + 4 /* VLAN */ + 4 /* CRC */)
103 #define USB_CTRL_SET_TIMEOUT_MS 5000
104 #define USB_CTRL_GET_TIMEOUT_MS 5000
105 #define USB_BULK_SEND_TIMEOUT_MS 5000
106 #define USB_BULK_RECV_TIMEOUT_MS 5000
107 #define TIMEOUT_RESOLUTION_MS 50
108 #define PHY_CONNECT_TIMEOUT_MS 5000
110 #define RX_URB_SIZE 2048
113 struct lan7x_private {
114 struct ueth_data ueth;
115 u32 chipid; /* Chip or device ID */
116 struct mii_dev *mdiobus;
117 struct phy_device *phydev;
121 * Lan7x infrastructure commands
124 int lan7x_write_reg(struct usb_device *udev, u32 index, u32 data);
126 int lan7x_read_reg(struct usb_device *udev, u32 index, u32 *data);
129 * FIXME: Code should not be in header files. Nive this to a file common to
132 static inline int lan7x_wait_for_bit(struct usb_device *udev,
133 const char *prefix, const u32 reg,
134 const u32 mask, const bool set,
135 const unsigned int timeout_ms,
136 const bool breakable)
139 unsigned long start = get_timer(0);
142 lan7x_read_reg(udev, reg, &val);
147 if ((val & mask) == mask)
150 if (get_timer(start) > timeout_ms)
153 if (breakable && ctrlc()) {
162 debug("%s: Timeout (reg=0x%x mask=%08x wait_set=%i)\n", prefix, reg,
168 int lan7x_mdio_read(struct usb_device *udev, int phy_id, int idx);
170 void lan7x_mdio_write(struct usb_device *udev, int phy_id, int idx,
173 static inline int lan7x_mdio_wait_for_bit(struct usb_device *udev,
175 int phy_id, const u32 reg,
176 const u32 mask, const bool set,
177 const unsigned int timeout_ms,
178 const bool breakable)
181 unsigned long start = get_timer(0);
184 val = lan7x_mdio_read(udev, phy_id, reg);
189 if ((val & mask) == mask)
192 if (get_timer(start) > timeout_ms)
195 if (breakable && ctrlc()) {
204 debug("%s: Timeout (reg=0x%x mask=%08x wait_set=%i)\n", prefix, reg,
210 int lan7x_phylib_register(struct udevice *udev);
212 int lan7x_eth_phylib_connect(struct udevice *udev, struct ueth_data *dev);
214 int lan7x_eth_phylib_config_start(struct udevice *udev);
216 int lan7x_pmt_phy_reset(struct usb_device *udev,
217 struct ueth_data *dev);
219 int lan7x_update_flowcontrol(struct usb_device *udev,
220 struct ueth_data *dev,
221 uint32_t *flow, uint32_t *fct_flow);
223 int lan7x_read_eeprom_mac(unsigned char *enetaddr, struct usb_device *udev);
225 int lan7x_basic_reset(struct usb_device *udev,
226 struct ueth_data *dev);
228 void lan7x_eth_stop(struct udevice *dev);
230 int lan7x_eth_send(struct udevice *dev, void *packet, int length);
232 int lan7x_eth_recv(struct udevice *dev, int flags, uchar **packetp);
234 int lan7x_free_pkt(struct udevice *dev, uchar *packet, int packet_len);
236 int lan7x_eth_remove(struct udevice *dev);