Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 2 | /* |
| 3 | * Copyright (c) 2013 Gerhard Sittig <gsi@denx.de> |
| 4 | * based on the U-Boot Asix driver as well as information |
| 5 | * from the Linux Moschip driver |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 6 | */ |
| 7 | |
| 8 | /* |
| 9 | * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices |
| 10 | */ |
| 11 | |
| 12 | #include <common.h> |
Simon Glass | fd954e5 | 2015-11-29 13:18:11 -0700 | [diff] [blame] | 13 | #include <dm.h> |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 14 | #include <errno.h> |
Simon Glass | 0f2af88 | 2020-05-10 11:40:05 -0600 | [diff] [blame] | 15 | #include <log.h> |
Simon Glass | 274e0b0 | 2020-05-10 11:39:56 -0600 | [diff] [blame] | 16 | #include <net.h> |
Simon Glass | dbd7954 | 2020-05-10 11:40:11 -0600 | [diff] [blame] | 17 | #include <linux/delay.h> |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 18 | #include <linux/mii.h> |
| 19 | #include <malloc.h> |
Simon Glass | 2dd337a | 2015-09-02 17:24:58 -0600 | [diff] [blame] | 20 | #include <memalign.h> |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 21 | #include <usb.h> |
| 22 | |
| 23 | #include "usb_ether.h" |
| 24 | |
| 25 | #define MCS7830_BASE_NAME "mcs" |
| 26 | |
| 27 | #define USBCALL_TIMEOUT 1000 |
| 28 | #define LINKSTATUS_TIMEOUT 5000 /* link status, connect timeout */ |
| 29 | #define LINKSTATUS_TIMEOUT_RES 50 /* link status, resolution in msec */ |
| 30 | |
| 31 | #define MCS7830_RX_URB_SIZE 2048 |
| 32 | |
| 33 | /* command opcodes */ |
| 34 | #define MCS7830_WR_BREQ 0x0d |
| 35 | #define MCS7830_RD_BREQ 0x0e |
| 36 | |
| 37 | /* register layout, numerical offset specs for USB API calls */ |
| 38 | struct mcs7830_regs { |
| 39 | uint8_t multicast_hashes[8]; |
| 40 | uint8_t packet_gap[2]; |
| 41 | uint8_t phy_data[2]; |
| 42 | uint8_t phy_command[2]; |
| 43 | uint8_t configuration; |
| 44 | uint8_t ether_address[6]; |
| 45 | uint8_t frame_drop_count; |
| 46 | uint8_t pause_threshold; |
| 47 | }; |
| 48 | #define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes) |
| 49 | #define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data) |
| 50 | #define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command) |
| 51 | #define REG_CONFIG offsetof(struct mcs7830_regs, configuration) |
| 52 | #define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address) |
| 53 | #define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count) |
| 54 | #define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold) |
| 55 | |
| 56 | /* bit masks and default values for the above registers */ |
| 57 | #define PHY_CMD1_READ 0x40 |
| 58 | #define PHY_CMD1_WRITE 0x20 |
| 59 | #define PHY_CMD1_PHYADDR 0x01 |
| 60 | |
| 61 | #define PHY_CMD2_PEND 0x80 |
| 62 | #define PHY_CMD2_READY 0x40 |
| 63 | |
| 64 | #define CONF_CFG 0x80 |
| 65 | #define CONF_SPEED100 0x40 |
| 66 | #define CONF_FDX_ENABLE 0x20 |
| 67 | #define CONF_RXENABLE 0x10 |
| 68 | #define CONF_TXENABLE 0x08 |
| 69 | #define CONF_SLEEPMODE 0x04 |
| 70 | #define CONF_ALLMULTICAST 0x02 |
| 71 | #define CONF_PROMISCUOUS 0x01 |
| 72 | |
| 73 | #define PAUSE_THRESHOLD_DEFAULT 0 |
| 74 | |
| 75 | /* bit masks for the status byte which follows received ethernet frames */ |
| 76 | #define STAT_RX_FRAME_CORRECT 0x20 |
| 77 | #define STAT_RX_LARGE_FRAME 0x10 |
| 78 | #define STAT_RX_CRC_ERROR 0x08 |
| 79 | #define STAT_RX_ALIGNMENT_ERROR 0x04 |
| 80 | #define STAT_RX_LENGTH_ERROR 0x02 |
| 81 | #define STAT_RX_SHORT_FRAME 0x01 |
| 82 | |
| 83 | /* |
| 84 | * struct mcs7830_private - private driver data for an individual adapter |
| 85 | * @config: shadow for the network adapter's configuration register |
| 86 | * @mchash: shadow for the network adapter's multicast hash registers |
| 87 | */ |
| 88 | struct mcs7830_private { |
Simon Glass | fd954e5 | 2015-11-29 13:18:11 -0700 | [diff] [blame] | 89 | uint8_t rx_buf[MCS7830_RX_URB_SIZE]; |
| 90 | struct ueth_data ueth; |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 91 | uint8_t config; |
| 92 | uint8_t mchash[8]; |
| 93 | }; |
| 94 | |
| 95 | /* |
| 96 | * mcs7830_read_reg() - read a register of the network adapter |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 97 | * @udev: network device to read from |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 98 | * @idx: index of the register to start reading from |
| 99 | * @size: number of bytes to read |
| 100 | * @data: buffer to read into |
| 101 | * Return: zero upon success, negative upon error |
| 102 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 103 | static int mcs7830_read_reg(struct usb_device *udev, uint8_t idx, |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 104 | uint16_t size, void *data) |
| 105 | { |
| 106 | int len; |
| 107 | ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size); |
| 108 | |
| 109 | debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size); |
| 110 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 111 | len = usb_control_msg(udev, |
| 112 | usb_rcvctrlpipe(udev, 0), |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 113 | MCS7830_RD_BREQ, |
| 114 | USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 115 | 0, idx, buf, size, |
| 116 | USBCALL_TIMEOUT); |
| 117 | if (len != size) { |
| 118 | debug("%s() len=%d != sz=%d\n", __func__, len, size); |
| 119 | return -EIO; |
| 120 | } |
| 121 | memcpy(data, buf, size); |
| 122 | return 0; |
| 123 | } |
| 124 | |
| 125 | /* |
| 126 | * mcs7830_write_reg() - write a register of the network adapter |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 127 | * @udev: network device to write to |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 128 | * @idx: index of the register to start writing to |
| 129 | * @size: number of bytes to write |
| 130 | * @data: buffer holding the data to write |
| 131 | * Return: zero upon success, negative upon error |
| 132 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 133 | static int mcs7830_write_reg(struct usb_device *udev, uint8_t idx, |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 134 | uint16_t size, void *data) |
| 135 | { |
| 136 | int len; |
| 137 | ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size); |
| 138 | |
| 139 | debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size); |
| 140 | |
| 141 | memcpy(buf, data, size); |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 142 | len = usb_control_msg(udev, |
| 143 | usb_sndctrlpipe(udev, 0), |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 144 | MCS7830_WR_BREQ, |
| 145 | USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE, |
| 146 | 0, idx, buf, size, |
| 147 | USBCALL_TIMEOUT); |
| 148 | if (len != size) { |
| 149 | debug("%s() len=%d != sz=%d\n", __func__, len, size); |
| 150 | return -EIO; |
| 151 | } |
| 152 | return 0; |
| 153 | } |
| 154 | |
| 155 | /* |
| 156 | * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 157 | * @udev: network device to talk to |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 158 | * @rwflag: PHY_CMD1_READ or PHY_CMD1_WRITE opcode |
| 159 | * @index: number of the PHY register to read or write |
| 160 | * Return: zero upon success, negative upon error |
| 161 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 162 | static int mcs7830_phy_emit_wait(struct usb_device *udev, |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 163 | uint8_t rwflag, uint8_t index) |
| 164 | { |
| 165 | int rc; |
| 166 | int retry; |
| 167 | uint8_t cmd[2]; |
| 168 | |
| 169 | /* send the PHY read/write request */ |
| 170 | cmd[0] = rwflag | PHY_CMD1_PHYADDR; |
| 171 | cmd[1] = PHY_CMD2_PEND | (index & 0x1f); |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 172 | rc = mcs7830_write_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 173 | if (rc < 0) |
| 174 | return rc; |
| 175 | |
| 176 | /* wait for the response to become available (usually < 1ms) */ |
| 177 | retry = 10; |
| 178 | do { |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 179 | rc = mcs7830_read_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 180 | if (rc < 0) |
| 181 | return rc; |
| 182 | if (cmd[1] & PHY_CMD2_READY) |
| 183 | return 0; |
| 184 | if (!retry--) |
| 185 | return -ETIMEDOUT; |
| 186 | mdelay(1); |
| 187 | } while (1); |
| 188 | /* UNREACH */ |
| 189 | } |
| 190 | |
| 191 | /* |
| 192 | * mcs7830_read_phy() - read a PHY register of the network adapter |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 193 | * @udev: network device to read from |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 194 | * @index: index of the PHY register to read from |
| 195 | * Return: non-negative 16bit register content, negative upon error |
| 196 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 197 | static int mcs7830_read_phy(struct usb_device *udev, uint8_t index) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 198 | { |
| 199 | int rc; |
| 200 | uint16_t val; |
| 201 | |
| 202 | /* issue the PHY read request and wait for its execution */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 203 | rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 204 | if (rc < 0) |
| 205 | return rc; |
| 206 | |
| 207 | /* fetch the PHY data which was read */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 208 | rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 209 | if (rc < 0) |
| 210 | return rc; |
| 211 | rc = le16_to_cpu(val); |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 212 | debug("%s(%d) => 0x%04X\n", __func__, index, rc); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 213 | return rc; |
| 214 | } |
| 215 | |
| 216 | /* |
| 217 | * mcs7830_write_phy() - write a PHY register of the network adapter |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 218 | * @udev: network device to write to |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 219 | * @index: index of the PHY register to write to |
| 220 | * @val: value to write to the PHY register |
| 221 | * Return: zero upon success, negative upon error |
| 222 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 223 | static int mcs7830_write_phy(struct usb_device *udev, uint8_t index, |
| 224 | uint16_t val) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 225 | { |
| 226 | int rc; |
| 227 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 228 | debug("%s(%d, 0x%04X)\n", __func__, index, val); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 229 | |
| 230 | /* setup the PHY data which is to get written */ |
| 231 | val = cpu_to_le16(val); |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 232 | rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 233 | if (rc < 0) |
| 234 | return rc; |
| 235 | |
| 236 | /* issue the PHY write request and wait for its execution */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 237 | rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 238 | if (rc < 0) |
| 239 | return rc; |
| 240 | |
| 241 | return 0; |
| 242 | } |
| 243 | |
| 244 | /* |
| 245 | * mcs7830_write_config() - write to the network adapter's config register |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 246 | * @udev: network device to write to |
| 247 | * @priv: private data |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 248 | * Return: zero upon success, negative upon error |
| 249 | * |
| 250 | * the data which gets written is taken from the shadow config register |
| 251 | * within the device driver's private data |
| 252 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 253 | static int mcs7830_write_config(struct usb_device *udev, |
| 254 | struct mcs7830_private *priv) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 255 | { |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 256 | int rc; |
| 257 | |
| 258 | debug("%s()\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 259 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 260 | rc = mcs7830_write_reg(udev, REG_CONFIG, |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 261 | sizeof(priv->config), &priv->config); |
| 262 | if (rc < 0) { |
| 263 | debug("writing config to adapter failed\n"); |
| 264 | return rc; |
| 265 | } |
| 266 | |
| 267 | return 0; |
| 268 | } |
| 269 | |
| 270 | /* |
| 271 | * mcs7830_write_mchash() - write the network adapter's multicast filter |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 272 | * @udev: network device to write to |
| 273 | * @priv: private data |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 274 | * Return: zero upon success, negative upon error |
| 275 | * |
| 276 | * the data which gets written is taken from the shadow multicast hashes |
| 277 | * within the device driver's private data |
| 278 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 279 | static int mcs7830_write_mchash(struct usb_device *udev, |
| 280 | struct mcs7830_private *priv) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 281 | { |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 282 | int rc; |
| 283 | |
| 284 | debug("%s()\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 285 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 286 | rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH, |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 287 | sizeof(priv->mchash), &priv->mchash); |
| 288 | if (rc < 0) { |
| 289 | debug("writing multicast hash to adapter failed\n"); |
| 290 | return rc; |
| 291 | } |
| 292 | |
| 293 | return 0; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 298 | * @udev: network device to run link negotiation on |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 299 | * Return: zero upon success, negative upon error |
| 300 | * |
| 301 | * the routine advertises available media and starts autonegotiation |
| 302 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 303 | static int mcs7830_set_autoneg(struct usb_device *udev) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 304 | { |
| 305 | int adv, flg; |
| 306 | int rc; |
| 307 | |
| 308 | debug("%s()\n", __func__); |
| 309 | |
| 310 | /* |
| 311 | * algorithm taken from the Linux driver, which took it from |
| 312 | * "the original mcs7830 version 1.4 driver": |
| 313 | * |
| 314 | * enable all media, reset BMCR, enable auto neg, restart |
| 315 | * auto neg while keeping the enable auto neg flag set |
| 316 | */ |
| 317 | |
| 318 | adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA; |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 319 | rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 320 | |
| 321 | flg = 0; |
| 322 | if (!rc) |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 323 | rc = mcs7830_write_phy(udev, MII_BMCR, flg); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 324 | |
| 325 | flg |= BMCR_ANENABLE; |
| 326 | if (!rc) |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 327 | rc = mcs7830_write_phy(udev, MII_BMCR, flg); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 328 | |
| 329 | flg |= BMCR_ANRESTART; |
| 330 | if (!rc) |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 331 | rc = mcs7830_write_phy(udev, MII_BMCR, flg); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 332 | |
| 333 | return rc; |
| 334 | } |
| 335 | |
| 336 | /* |
| 337 | * mcs7830_get_rev() - identify a network adapter's chip revision |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 338 | * @udev: network device to identify |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 339 | * Return: non-negative number, reflecting the revision number |
| 340 | * |
| 341 | * currently, only "rev C and higher" and "below rev C" are needed, so |
| 342 | * the return value is #1 for "below rev C", and #2 for "rev C and above" |
| 343 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 344 | static int mcs7830_get_rev(struct usb_device *udev) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 345 | { |
| 346 | uint8_t buf[2]; |
| 347 | int rc; |
| 348 | int rev; |
| 349 | |
| 350 | /* register 22 is readable in rev C and higher */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 351 | rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 352 | if (rc < 0) |
| 353 | rev = 1; |
| 354 | else |
| 355 | rev = 2; |
| 356 | debug("%s() rc=%d, rev=%d\n", __func__, rc, rev); |
| 357 | return rev; |
| 358 | } |
| 359 | |
| 360 | /* |
| 361 | * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 362 | * @udev: network device to identify and apply fixups to |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 363 | * Return: zero upon success (no errors emitted from here) |
| 364 | * |
| 365 | * this routine identifies the network adapter's chip revision, and applies |
| 366 | * fixups for known issues |
| 367 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 368 | static int mcs7830_apply_fixup(struct usb_device *udev) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 369 | { |
| 370 | int rev; |
| 371 | int i; |
| 372 | uint8_t thr; |
| 373 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 374 | rev = mcs7830_get_rev(udev); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 375 | debug("%s() rev=%d\n", __func__, rev); |
| 376 | |
| 377 | /* |
| 378 | * rev C requires setting the pause threshold (the Linux driver |
| 379 | * is inconsistent, the implementation does it for "rev C |
| 380 | * exactly", the introductory comment says "rev C and above") |
| 381 | */ |
| 382 | if (rev == 2) { |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 383 | debug("%s: applying rev C fixup\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 384 | thr = PAUSE_THRESHOLD_DEFAULT; |
| 385 | for (i = 0; i < 2; i++) { |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 386 | (void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD, |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 387 | sizeof(thr), &thr); |
| 388 | mdelay(1); |
| 389 | } |
| 390 | } |
| 391 | |
| 392 | return 0; |
| 393 | } |
| 394 | |
| 395 | /* |
| 396 | * mcs7830_basic_reset() - bring the network adapter into a known first state |
| 397 | * @eth: network device to act upon |
| 398 | * Return: zero upon success, negative upon error |
| 399 | * |
| 400 | * this routine initializes the network adapter such that subsequent invocations |
| 401 | * of the interface callbacks can exchange ethernet frames; link negotiation is |
| 402 | * triggered from here already and continues in background |
| 403 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 404 | static int mcs7830_basic_reset(struct usb_device *udev, |
| 405 | struct mcs7830_private *priv) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 406 | { |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 407 | int rc; |
| 408 | |
| 409 | debug("%s()\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 410 | |
| 411 | /* |
| 412 | * comment from the respective Linux driver, which |
| 413 | * unconditionally sets the ALLMULTICAST flag as well: |
| 414 | * should not be needed, but does not work otherwise |
| 415 | */ |
| 416 | priv->config = CONF_TXENABLE; |
| 417 | priv->config |= CONF_ALLMULTICAST; |
| 418 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 419 | rc = mcs7830_set_autoneg(udev); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 420 | if (rc < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 421 | pr_err("setting autoneg failed\n"); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 422 | return rc; |
| 423 | } |
| 424 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 425 | rc = mcs7830_write_mchash(udev, priv); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 426 | if (rc < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 427 | pr_err("failed to set multicast hash\n"); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 428 | return rc; |
| 429 | } |
| 430 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 431 | rc = mcs7830_write_config(udev, priv); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 432 | if (rc < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 433 | pr_err("failed to set configuration\n"); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 434 | return rc; |
| 435 | } |
| 436 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 437 | rc = mcs7830_apply_fixup(udev); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 438 | if (rc < 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 439 | pr_err("fixup application failed\n"); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 440 | return rc; |
| 441 | } |
| 442 | |
| 443 | return 0; |
| 444 | } |
| 445 | |
| 446 | /* |
| 447 | * mcs7830_read_mac() - read an ethernet adapter's MAC address |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 448 | * @udev: network device to read from |
| 449 | * @enetaddr: place to put ethernet MAC address |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 450 | * Return: zero upon success, negative upon error |
| 451 | * |
| 452 | * this routine fetches the MAC address stored within the ethernet adapter, |
| 453 | * and stores it in the ethernet interface's data structure |
| 454 | */ |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 455 | static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[]) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 456 | { |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 457 | int rc; |
| 458 | uint8_t buf[ETH_ALEN]; |
| 459 | |
| 460 | debug("%s()\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 461 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 462 | rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 463 | if (rc < 0) { |
| 464 | debug("reading MAC from adapter failed\n"); |
| 465 | return rc; |
| 466 | } |
| 467 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 468 | memcpy(enetaddr, buf, ETH_ALEN); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 469 | return 0; |
| 470 | } |
| 471 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 472 | static int mcs7830_write_mac_common(struct usb_device *udev, |
| 473 | unsigned char enetaddr[]) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 474 | { |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 475 | int rc; |
| 476 | |
| 477 | debug("%s()\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 478 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 479 | rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 480 | if (rc < 0) { |
| 481 | debug("writing MAC to adapter failed\n"); |
| 482 | return rc; |
| 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 487 | static int mcs7830_init_common(struct usb_device *udev) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 488 | { |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 489 | int timeout; |
| 490 | int have_link; |
| 491 | |
| 492 | debug("%s()\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 493 | |
| 494 | timeout = 0; |
| 495 | do { |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 496 | have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS; |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 497 | if (have_link) |
| 498 | break; |
| 499 | udelay(LINKSTATUS_TIMEOUT_RES * 1000); |
| 500 | timeout += LINKSTATUS_TIMEOUT_RES; |
| 501 | } while (timeout < LINKSTATUS_TIMEOUT); |
| 502 | if (!have_link) { |
| 503 | debug("ethernet link is down\n"); |
| 504 | return -ETIMEDOUT; |
| 505 | } |
| 506 | return 0; |
| 507 | } |
| 508 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 509 | static int mcs7830_send_common(struct ueth_data *ueth, void *packet, |
| 510 | int length) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 511 | { |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 512 | struct usb_device *udev = ueth->pusb_dev; |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 513 | int rc; |
| 514 | int gotlen; |
| 515 | /* there is a status byte after the ethernet frame */ |
| 516 | ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t)); |
| 517 | |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 518 | memcpy(buf, packet, length); |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 519 | rc = usb_bulk_msg(udev, |
| 520 | usb_sndbulkpipe(udev, ueth->ep_out), |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 521 | &buf[0], length, &gotlen, |
| 522 | USBCALL_TIMEOUT); |
| 523 | debug("%s() TX want len %d, got len %d, rc %d\n", |
| 524 | __func__, length, gotlen, rc); |
| 525 | return rc; |
| 526 | } |
| 527 | |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 528 | static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf) |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 529 | { |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 530 | int rc, wantlen, gotlen; |
| 531 | uint8_t sts; |
| 532 | |
| 533 | debug("%s()\n", __func__); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 534 | |
| 535 | /* fetch input data from the adapter */ |
| 536 | wantlen = MCS7830_RX_URB_SIZE; |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 537 | rc = usb_bulk_msg(ueth->pusb_dev, |
| 538 | usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in), |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 539 | &buf[0], wantlen, &gotlen, |
| 540 | USBCALL_TIMEOUT); |
| 541 | debug("%s() RX want len %d, got len %d, rc %d\n", |
| 542 | __func__, wantlen, gotlen, rc); |
| 543 | if (rc != 0) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 544 | pr_err("RX: failed to receive\n"); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 545 | return rc; |
| 546 | } |
| 547 | if (gotlen > wantlen) { |
Masahiro Yamada | 81e1042 | 2017-09-16 14:10:41 +0900 | [diff] [blame] | 548 | pr_err("RX: got too many bytes (%d)\n", gotlen); |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 549 | return -EIO; |
| 550 | } |
| 551 | |
| 552 | /* |
| 553 | * the bulk message that we received from USB contains exactly |
| 554 | * one ethernet frame and a trailing status byte |
| 555 | */ |
| 556 | if (gotlen < sizeof(sts)) |
| 557 | return -EIO; |
| 558 | gotlen -= sizeof(sts); |
| 559 | sts = buf[gotlen]; |
| 560 | |
| 561 | if (sts == STAT_RX_FRAME_CORRECT) { |
| 562 | debug("%s() got a frame, len=%d\n", __func__, gotlen); |
Simon Glass | b82f62d | 2015-11-29 13:18:10 -0700 | [diff] [blame] | 563 | return gotlen; |
Gerhard Sittig | 2385df6 | 2014-03-08 19:46:14 +0100 | [diff] [blame] | 564 | } |
| 565 | |
| 566 | debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n", |
| 567 | sts, |
| 568 | (sts & STAT_RX_LARGE_FRAME) ? "large" : "-", |
| 569 | (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-", |
| 570 | (sts & STAT_RX_SHORT_FRAME) ? "short" : "-", |
| 571 | (sts & STAT_RX_CRC_ERROR) ? "crc" : "-", |
| 572 | (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-"); |
| 573 | return -EIO; |
| 574 | } |
| 575 | |
Simon Glass | fd954e5 | 2015-11-29 13:18:11 -0700 | [diff] [blame] | 576 | static int mcs7830_eth_start(struct udevice *dev) |
| 577 | { |
| 578 | struct usb_device *udev = dev_get_parent_priv(dev); |
| 579 | |
| 580 | return mcs7830_init_common(udev); |
| 581 | } |
| 582 | |
| 583 | void mcs7830_eth_stop(struct udevice *dev) |
| 584 | { |
| 585 | debug("** %s()\n", __func__); |
| 586 | } |
| 587 | |
| 588 | int mcs7830_eth_send(struct udevice *dev, void *packet, int length) |
| 589 | { |
| 590 | struct mcs7830_private *priv = dev_get_priv(dev); |
| 591 | struct ueth_data *ueth = &priv->ueth; |
| 592 | |
| 593 | return mcs7830_send_common(ueth, packet, length); |
| 594 | } |
| 595 | |
| 596 | int mcs7830_eth_recv(struct udevice *dev, int flags, uchar **packetp) |
| 597 | { |
| 598 | struct mcs7830_private *priv = dev_get_priv(dev); |
| 599 | struct ueth_data *ueth = &priv->ueth; |
| 600 | int len; |
| 601 | |
| 602 | len = mcs7830_recv_common(ueth, priv->rx_buf); |
| 603 | *packetp = priv->rx_buf; |
| 604 | |
| 605 | return len; |
| 606 | } |
| 607 | |
| 608 | static int mcs7830_free_pkt(struct udevice *dev, uchar *packet, int packet_len) |
| 609 | { |
| 610 | struct mcs7830_private *priv = dev_get_priv(dev); |
| 611 | |
| 612 | packet_len = ALIGN(packet_len, 4); |
| 613 | usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len); |
| 614 | |
| 615 | return 0; |
| 616 | } |
| 617 | |
| 618 | int mcs7830_write_hwaddr(struct udevice *dev) |
| 619 | { |
| 620 | struct usb_device *udev = dev_get_parent_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 621 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | fd954e5 | 2015-11-29 13:18:11 -0700 | [diff] [blame] | 622 | |
| 623 | return mcs7830_write_mac_common(udev, pdata->enetaddr); |
| 624 | } |
| 625 | |
| 626 | static int mcs7830_eth_probe(struct udevice *dev) |
| 627 | { |
| 628 | struct usb_device *udev = dev_get_parent_priv(dev); |
| 629 | struct mcs7830_private *priv = dev_get_priv(dev); |
Simon Glass | fa20e93 | 2020-12-03 16:55:20 -0700 | [diff] [blame] | 630 | struct eth_pdata *pdata = dev_get_plat(dev); |
Simon Glass | fd954e5 | 2015-11-29 13:18:11 -0700 | [diff] [blame] | 631 | struct ueth_data *ueth = &priv->ueth; |
| 632 | |
| 633 | if (mcs7830_basic_reset(udev, priv)) |
| 634 | return 0; |
| 635 | |
| 636 | if (mcs7830_read_mac(udev, pdata->enetaddr)) |
| 637 | return 0; |
| 638 | |
| 639 | return usb_ether_register(dev, ueth, MCS7830_RX_URB_SIZE); |
| 640 | } |
| 641 | |
| 642 | static const struct eth_ops mcs7830_eth_ops = { |
| 643 | .start = mcs7830_eth_start, |
| 644 | .send = mcs7830_eth_send, |
| 645 | .recv = mcs7830_eth_recv, |
| 646 | .free_pkt = mcs7830_free_pkt, |
| 647 | .stop = mcs7830_eth_stop, |
| 648 | .write_hwaddr = mcs7830_write_hwaddr, |
| 649 | }; |
| 650 | |
| 651 | U_BOOT_DRIVER(mcs7830_eth) = { |
| 652 | .name = "mcs7830_eth", |
| 653 | .id = UCLASS_ETH, |
| 654 | .probe = mcs7830_eth_probe, |
| 655 | .ops = &mcs7830_eth_ops, |
Simon Glass | 8a2b47f | 2020-12-03 16:55:17 -0700 | [diff] [blame] | 656 | .priv_auto = sizeof(struct mcs7830_private), |
Simon Glass | 71fa5b4 | 2020-12-03 16:55:18 -0700 | [diff] [blame] | 657 | .plat_auto = sizeof(struct eth_pdata), |
Simon Glass | fd954e5 | 2015-11-29 13:18:11 -0700 | [diff] [blame] | 658 | .flags = DM_FLAG_ALLOC_PRIV_DMA, |
| 659 | }; |
| 660 | |
| 661 | static const struct usb_device_id mcs7830_eth_id_table[] = { |
| 662 | { USB_DEVICE(0x9710, 0x7832) }, /* Moschip 7832 */ |
| 663 | { USB_DEVICE(0x9710, 0x7830), }, /* Moschip 7830 */ |
| 664 | { USB_DEVICE(0x9710, 0x7730), }, /* Moschip 7730 */ |
| 665 | { USB_DEVICE(0x0df6, 0x0021), }, /* Sitecom LN 30 */ |
| 666 | { } /* Terminating entry */ |
| 667 | }; |
| 668 | |
| 669 | U_BOOT_USB_DEVICE(mcs7830_eth, mcs7830_eth_id_table); |