common: Drop linux/delay.h from common header
[oweals/u-boot.git] / drivers / usb / eth / mcs7830.c
1 // SPDX-License-Identifier: GPL-2.0+
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
6  */
7
8 /*
9  * MOSCHIP MCS7830 based (7730/7830/7832) USB 2.0 Ethernet Devices
10  */
11
12 #include <common.h>
13 #include <dm.h>
14 #include <errno.h>
15 #include <log.h>
16 #include <net.h>
17 #include <linux/delay.h>
18 #include <linux/mii.h>
19 #include <malloc.h>
20 #include <memalign.h>
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 {
89 #ifdef CONFIG_DM_ETH
90         uint8_t rx_buf[MCS7830_RX_URB_SIZE];
91         struct ueth_data ueth;
92 #endif
93         uint8_t config;
94         uint8_t mchash[8];
95 };
96
97 /*
98  * mcs7830_read_reg() - read a register of the network adapter
99  * @udev:       network device to read from
100  * @idx:        index of the register to start reading from
101  * @size:       number of bytes to read
102  * @data:       buffer to read into
103  * Return: zero upon success, negative upon error
104  */
105 static int mcs7830_read_reg(struct usb_device *udev, uint8_t idx,
106                             uint16_t size, void *data)
107 {
108         int len;
109         ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
110
111         debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
112
113         len = usb_control_msg(udev,
114                               usb_rcvctrlpipe(udev, 0),
115                               MCS7830_RD_BREQ,
116                               USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
117                               0, idx, buf, size,
118                               USBCALL_TIMEOUT);
119         if (len != size) {
120                 debug("%s() len=%d != sz=%d\n", __func__, len, size);
121                 return -EIO;
122         }
123         memcpy(data, buf, size);
124         return 0;
125 }
126
127 /*
128  * mcs7830_write_reg() - write a register of the network adapter
129  * @udev:       network device to write to
130  * @idx:        index of the register to start writing to
131  * @size:       number of bytes to write
132  * @data:       buffer holding the data to write
133  * Return: zero upon success, negative upon error
134  */
135 static int mcs7830_write_reg(struct usb_device *udev, uint8_t idx,
136                              uint16_t size, void *data)
137 {
138         int len;
139         ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
140
141         debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
142
143         memcpy(buf, data, size);
144         len = usb_control_msg(udev,
145                               usb_sndctrlpipe(udev, 0),
146                               MCS7830_WR_BREQ,
147                               USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
148                               0, idx, buf, size,
149                               USBCALL_TIMEOUT);
150         if (len != size) {
151                 debug("%s() len=%d != sz=%d\n", __func__, len, size);
152                 return -EIO;
153         }
154         return 0;
155 }
156
157 /*
158  * mcs7830_phy_emit_wait() - emit PHY read/write access, wait for its execution
159  * @udev:       network device to talk to
160  * @rwflag:     PHY_CMD1_READ or PHY_CMD1_WRITE opcode
161  * @index:      number of the PHY register to read or write
162  * Return: zero upon success, negative upon error
163  */
164 static int mcs7830_phy_emit_wait(struct usb_device *udev,
165                                  uint8_t rwflag, uint8_t index)
166 {
167         int rc;
168         int retry;
169         uint8_t cmd[2];
170
171         /* send the PHY read/write request */
172         cmd[0] = rwflag | PHY_CMD1_PHYADDR;
173         cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
174         rc = mcs7830_write_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
175         if (rc < 0)
176                 return rc;
177
178         /* wait for the response to become available (usually < 1ms) */
179         retry = 10;
180         do {
181                 rc = mcs7830_read_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
182                 if (rc < 0)
183                         return rc;
184                 if (cmd[1] & PHY_CMD2_READY)
185                         return 0;
186                 if (!retry--)
187                         return -ETIMEDOUT;
188                 mdelay(1);
189         } while (1);
190         /* UNREACH */
191 }
192
193 /*
194  * mcs7830_read_phy() - read a PHY register of the network adapter
195  * @udev:       network device to read from
196  * @index:      index of the PHY register to read from
197  * Return: non-negative 16bit register content, negative upon error
198  */
199 static int mcs7830_read_phy(struct usb_device *udev, uint8_t index)
200 {
201         int rc;
202         uint16_t val;
203
204         /* issue the PHY read request and wait for its execution */
205         rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index);
206         if (rc < 0)
207                 return rc;
208
209         /* fetch the PHY data which was read */
210         rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val);
211         if (rc < 0)
212                 return rc;
213         rc = le16_to_cpu(val);
214         debug("%s(%d) => 0x%04X\n", __func__, index, rc);
215         return rc;
216 }
217
218 /*
219  * mcs7830_write_phy() - write a PHY register of the network adapter
220  * @udev:       network device to write to
221  * @index:      index of the PHY register to write to
222  * @val:        value to write to the PHY register
223  * Return: zero upon success, negative upon error
224  */
225 static int mcs7830_write_phy(struct usb_device *udev, uint8_t index,
226                              uint16_t val)
227 {
228         int rc;
229
230         debug("%s(%d, 0x%04X)\n", __func__, index, val);
231
232         /* setup the PHY data which is to get written */
233         val = cpu_to_le16(val);
234         rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val);
235         if (rc < 0)
236                 return rc;
237
238         /* issue the PHY write request and wait for its execution */
239         rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index);
240         if (rc < 0)
241                 return rc;
242
243         return 0;
244 }
245
246 /*
247  * mcs7830_write_config() - write to the network adapter's config register
248  * @udev:       network device to write to
249  * @priv:       private data
250  * Return: zero upon success, negative upon error
251  *
252  * the data which gets written is taken from the shadow config register
253  * within the device driver's private data
254  */
255 static int mcs7830_write_config(struct usb_device *udev,
256                                 struct mcs7830_private *priv)
257 {
258         int rc;
259
260         debug("%s()\n", __func__);
261
262         rc = mcs7830_write_reg(udev, REG_CONFIG,
263                                sizeof(priv->config), &priv->config);
264         if (rc < 0) {
265                 debug("writing config to adapter failed\n");
266                 return rc;
267         }
268
269         return 0;
270 }
271
272 /*
273  * mcs7830_write_mchash() - write the network adapter's multicast filter
274  * @udev:       network device to write to
275  * @priv:       private data
276  * Return: zero upon success, negative upon error
277  *
278  * the data which gets written is taken from the shadow multicast hashes
279  * within the device driver's private data
280  */
281 static int mcs7830_write_mchash(struct usb_device *udev,
282                                 struct mcs7830_private *priv)
283 {
284         int rc;
285
286         debug("%s()\n", __func__);
287
288         rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH,
289                                sizeof(priv->mchash), &priv->mchash);
290         if (rc < 0) {
291                 debug("writing multicast hash to adapter failed\n");
292                 return rc;
293         }
294
295         return 0;
296 }
297
298 /*
299  * mcs7830_set_autoneg() - setup and trigger ethernet link autonegotiation
300  * @udev:       network device to run link negotiation on
301  * Return: zero upon success, negative upon error
302  *
303  * the routine advertises available media and starts autonegotiation
304  */
305 static int mcs7830_set_autoneg(struct usb_device *udev)
306 {
307         int adv, flg;
308         int rc;
309
310         debug("%s()\n", __func__);
311
312         /*
313          * algorithm taken from the Linux driver, which took it from
314          * "the original mcs7830 version 1.4 driver":
315          *
316          * enable all media, reset BMCR, enable auto neg, restart
317          * auto neg while keeping the enable auto neg flag set
318          */
319
320         adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
321         rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv);
322
323         flg = 0;
324         if (!rc)
325                 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
326
327         flg |= BMCR_ANENABLE;
328         if (!rc)
329                 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
330
331         flg |= BMCR_ANRESTART;
332         if (!rc)
333                 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
334
335         return rc;
336 }
337
338 /*
339  * mcs7830_get_rev() - identify a network adapter's chip revision
340  * @udev:       network device to identify
341  * Return: non-negative number, reflecting the revision number
342  *
343  * currently, only "rev C and higher" and "below rev C" are needed, so
344  * the return value is #1 for "below rev C", and #2 for "rev C and above"
345  */
346 static int mcs7830_get_rev(struct usb_device *udev)
347 {
348         uint8_t buf[2];
349         int rc;
350         int rev;
351
352         /* register 22 is readable in rev C and higher */
353         rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
354         if (rc < 0)
355                 rev = 1;
356         else
357                 rev = 2;
358         debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
359         return rev;
360 }
361
362 /*
363  * mcs7830_apply_fixup() - identify an adapter and potentially apply fixups
364  * @udev:       network device to identify and apply fixups to
365  * Return: zero upon success (no errors emitted from here)
366  *
367  * this routine identifies the network adapter's chip revision, and applies
368  * fixups for known issues
369  */
370 static int mcs7830_apply_fixup(struct usb_device *udev)
371 {
372         int rev;
373         int i;
374         uint8_t thr;
375
376         rev = mcs7830_get_rev(udev);
377         debug("%s() rev=%d\n", __func__, rev);
378
379         /*
380          * rev C requires setting the pause threshold (the Linux driver
381          * is inconsistent, the implementation does it for "rev C
382          * exactly", the introductory comment says "rev C and above")
383          */
384         if (rev == 2) {
385                 debug("%s: applying rev C fixup\n", __func__);
386                 thr = PAUSE_THRESHOLD_DEFAULT;
387                 for (i = 0; i < 2; i++) {
388                         (void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD,
389                                                 sizeof(thr), &thr);
390                         mdelay(1);
391                 }
392         }
393
394         return 0;
395 }
396
397 /*
398  * mcs7830_basic_reset() - bring the network adapter into a known first state
399  * @eth:        network device to act upon
400  * Return: zero upon success, negative upon error
401  *
402  * this routine initializes the network adapter such that subsequent invocations
403  * of the interface callbacks can exchange ethernet frames; link negotiation is
404  * triggered from here already and continues in background
405  */
406 static int mcs7830_basic_reset(struct usb_device *udev,
407                                struct mcs7830_private *priv)
408 {
409         int rc;
410
411         debug("%s()\n", __func__);
412
413         /*
414          * comment from the respective Linux driver, which
415          * unconditionally sets the ALLMULTICAST flag as well:
416          * should not be needed, but does not work otherwise
417          */
418         priv->config = CONF_TXENABLE;
419         priv->config |= CONF_ALLMULTICAST;
420
421         rc = mcs7830_set_autoneg(udev);
422         if (rc < 0) {
423                 pr_err("setting autoneg failed\n");
424                 return rc;
425         }
426
427         rc = mcs7830_write_mchash(udev, priv);
428         if (rc < 0) {
429                 pr_err("failed to set multicast hash\n");
430                 return rc;
431         }
432
433         rc = mcs7830_write_config(udev, priv);
434         if (rc < 0) {
435                 pr_err("failed to set configuration\n");
436                 return rc;
437         }
438
439         rc = mcs7830_apply_fixup(udev);
440         if (rc < 0) {
441                 pr_err("fixup application failed\n");
442                 return rc;
443         }
444
445         return 0;
446 }
447
448 /*
449  * mcs7830_read_mac() - read an ethernet adapter's MAC address
450  * @udev:       network device to read from
451  * @enetaddr:   place to put ethernet MAC address
452  * Return: zero upon success, negative upon error
453  *
454  * this routine fetches the MAC address stored within the ethernet adapter,
455  * and stores it in the ethernet interface's data structure
456  */
457 static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[])
458 {
459         int rc;
460         uint8_t buf[ETH_ALEN];
461
462         debug("%s()\n", __func__);
463
464         rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf);
465         if (rc < 0) {
466                 debug("reading MAC from adapter failed\n");
467                 return rc;
468         }
469
470         memcpy(enetaddr, buf, ETH_ALEN);
471         return 0;
472 }
473
474 static int mcs7830_write_mac_common(struct usb_device *udev,
475                                     unsigned char enetaddr[])
476 {
477         int rc;
478
479         debug("%s()\n", __func__);
480
481         rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr);
482         if (rc < 0) {
483                 debug("writing MAC to adapter failed\n");
484                 return rc;
485         }
486         return 0;
487 }
488
489 static int mcs7830_init_common(struct usb_device *udev)
490 {
491         int timeout;
492         int have_link;
493
494         debug("%s()\n", __func__);
495
496         timeout = 0;
497         do {
498                 have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS;
499                 if (have_link)
500                         break;
501                 udelay(LINKSTATUS_TIMEOUT_RES * 1000);
502                 timeout += LINKSTATUS_TIMEOUT_RES;
503         } while (timeout < LINKSTATUS_TIMEOUT);
504         if (!have_link) {
505                 debug("ethernet link is down\n");
506                 return -ETIMEDOUT;
507         }
508         return 0;
509 }
510
511 static int mcs7830_send_common(struct ueth_data *ueth, void *packet,
512                                int length)
513 {
514         struct usb_device *udev = ueth->pusb_dev;
515         int rc;
516         int gotlen;
517         /* there is a status byte after the ethernet frame */
518         ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
519
520         memcpy(buf, packet, length);
521         rc = usb_bulk_msg(udev,
522                           usb_sndbulkpipe(udev, ueth->ep_out),
523                           &buf[0], length, &gotlen,
524                           USBCALL_TIMEOUT);
525         debug("%s() TX want len %d, got len %d, rc %d\n",
526               __func__, length, gotlen, rc);
527         return rc;
528 }
529
530 static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf)
531 {
532         int rc, wantlen, gotlen;
533         uint8_t sts;
534
535         debug("%s()\n", __func__);
536
537         /* fetch input data from the adapter */
538         wantlen = MCS7830_RX_URB_SIZE;
539         rc = usb_bulk_msg(ueth->pusb_dev,
540                           usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
541                           &buf[0], wantlen, &gotlen,
542                           USBCALL_TIMEOUT);
543         debug("%s() RX want len %d, got len %d, rc %d\n",
544               __func__, wantlen, gotlen, rc);
545         if (rc != 0) {
546                 pr_err("RX: failed to receive\n");
547                 return rc;
548         }
549         if (gotlen > wantlen) {
550                 pr_err("RX: got too many bytes (%d)\n", gotlen);
551                 return -EIO;
552         }
553
554         /*
555          * the bulk message that we received from USB contains exactly
556          * one ethernet frame and a trailing status byte
557          */
558         if (gotlen < sizeof(sts))
559                 return -EIO;
560         gotlen -= sizeof(sts);
561         sts = buf[gotlen];
562
563         if (sts == STAT_RX_FRAME_CORRECT) {
564                 debug("%s() got a frame, len=%d\n", __func__, gotlen);
565                 return gotlen;
566         }
567
568         debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
569               sts,
570               (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
571               (sts & STAT_RX_LENGTH_ERROR) ?  "length" : "-",
572               (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
573               (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
574               (sts & STAT_RX_ALIGNMENT_ERROR) ?  "align" : "-");
575         return -EIO;
576 }
577
578 #ifndef CONFIG_DM_ETH
579 /*
580  * mcs7830_init() - network interface's init callback
581  * @udev:       network device to initialize
582  * @bd:         board information
583  * Return: zero upon success, negative upon error
584  *
585  * after initial setup during probe() and get_info(), this init() callback
586  * ensures that the link is up and subsequent send() and recv() calls can
587  * exchange ethernet frames
588  */
589 static int mcs7830_init(struct eth_device *eth, bd_t *bd)
590 {
591         struct ueth_data *dev = eth->priv;
592
593         return mcs7830_init_common(dev->pusb_dev);
594 }
595
596 /*
597  * mcs7830_send() - network interface's send callback
598  * @eth:        network device to send the frame from
599  * @packet:     ethernet frame content
600  * @length:     ethernet frame length
601  * Return: zero upon success, negative upon error
602  *
603  * this routine send an ethernet frame out of the network interface
604  */
605 static int mcs7830_send(struct eth_device *eth, void *packet, int length)
606 {
607         struct ueth_data *dev = eth->priv;
608
609         return mcs7830_send_common(dev, packet, length);
610 }
611
612 /*
613  * mcs7830_recv() - network interface's recv callback
614  * @eth:        network device to receive frames from
615  * Return: zero upon success, negative upon error
616  *
617  * this routine checks for available ethernet frames that the network
618  * interface might have received, and notifies the network stack
619  */
620 static int mcs7830_recv(struct eth_device *eth)
621 {
622         ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
623         struct ueth_data *ueth = eth->priv;
624         int len;
625
626         len = mcs7830_recv_common(ueth, buf);
627         if (len >= 0) {
628                 net_process_received_packet(buf, len);
629                 return 0;
630         }
631
632         return len;
633 }
634
635 /*
636  * mcs7830_halt() - network interface's halt callback
637  * @eth:        network device to cease operation of
638  * Return: none
639  *
640  * this routine is supposed to undo the effect of previous initialization and
641  * ethernet frames exchange; in this implementation it's a NOP
642  */
643 static void mcs7830_halt(struct eth_device *eth)
644 {
645         debug("%s()\n", __func__);
646 }
647
648 /*
649  * mcs7830_write_mac() - write an ethernet adapter's MAC address
650  * @eth:        network device to write to
651  * Return: zero upon success, negative upon error
652  *
653  * this routine takes the MAC address from the ethernet interface's data
654  * structure, and writes it into the ethernet adapter such that subsequent
655  * exchange of ethernet frames uses this address
656  */
657 static int mcs7830_write_mac(struct eth_device *eth)
658 {
659         struct ueth_data *ueth = eth->priv;
660
661         return mcs7830_write_mac_common(ueth->pusb_dev, eth->enetaddr);
662 }
663
664 /*
665  * mcs7830_iface_idx - index of detected network interfaces
666  *
667  * this counter keeps track of identified supported interfaces,
668  * to assign unique names as more interfaces are found
669  */
670 static int mcs7830_iface_idx;
671
672 /*
673  * mcs7830_eth_before_probe() - network driver's before_probe callback
674  * Return: none
675  *
676  * this routine initializes driver's internal data in preparation of
677  * subsequent probe callbacks
678  */
679 void mcs7830_eth_before_probe(void)
680 {
681         mcs7830_iface_idx = 0;
682 }
683
684 /*
685  * struct mcs7830_dongle - description of a supported Moschip ethernet dongle
686  * @vendor:     16bit USB vendor identification
687  * @product:    16bit USB product identification
688  *
689  * this structure describes a supported USB ethernet dongle by means of the
690  * vendor and product codes found during USB enumeration; no flags are held
691  * here since all supported dongles have identical behaviour, and required
692  * fixups get determined at runtime, such that no manual configuration is
693  * needed
694  */
695 struct mcs7830_dongle {
696         uint16_t vendor;
697         uint16_t product;
698 };
699
700 /*
701  * mcs7830_dongles - the list of supported Moschip based USB ethernet dongles
702  */
703 static const struct mcs7830_dongle mcs7830_dongles[] = {
704         { 0x9710, 0x7832, },    /* Moschip 7832 */
705         { 0x9710, 0x7830, },    /* Moschip 7830 */
706         { 0x9710, 0x7730, },    /* Moschip 7730 */
707         { 0x0df6, 0x0021, },    /* Sitecom LN 30 */
708 };
709
710 /*
711  * mcs7830_eth_probe() - network driver's probe callback
712  * @dev:        detected USB device to check
713  * @ifnum:      detected USB interface to check
714  * @ss:         USB ethernet data structure to fill in upon match
715  * Return: #1 upon match, #0 upon mismatch or error
716  *
717  * this routine checks whether the found USB device is supported by
718  * this ethernet driver, and upon match fills in the USB ethernet
719  * data structure which later is passed to the get_info callback
720  */
721 int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
722                       struct ueth_data *ss)
723 {
724         struct usb_interface *iface;
725         struct usb_interface_descriptor *iface_desc;
726         int i;
727         struct mcs7830_private *priv;
728         int ep_in_found, ep_out_found, ep_intr_found;
729
730         debug("%s()\n", __func__);
731
732         /* iterate the list of supported dongles */
733         iface = &dev->config.if_desc[ifnum];
734         iface_desc = &iface->desc;
735         for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
736                 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
737                     dev->descriptor.idProduct == mcs7830_dongles[i].product)
738                         break;
739         }
740         if (i == ARRAY_SIZE(mcs7830_dongles))
741                 return 0;
742         debug("detected USB ethernet device: %04X:%04X\n",
743               dev->descriptor.idVendor, dev->descriptor.idProduct);
744
745         /* fill in driver private data */
746         priv = calloc(1, sizeof(*priv));
747         if (!priv)
748                 return 0;
749
750         /* fill in the ueth_data structure, attach private data */
751         memset(ss, 0, sizeof(*ss));
752         ss->ifnum = ifnum;
753         ss->pusb_dev = dev;
754         ss->subclass = iface_desc->bInterfaceSubClass;
755         ss->protocol = iface_desc->bInterfaceProtocol;
756         ss->dev_priv = priv;
757
758         /*
759          * a minimum of three endpoints is expected: in (bulk),
760          * out (bulk), and interrupt; ignore all others
761          */
762         ep_in_found = ep_out_found = ep_intr_found = 0;
763         for (i = 0; i < iface_desc->bNumEndpoints; i++) {
764                 uint8_t eptype, epaddr;
765                 bool is_input;
766
767                 eptype = iface->ep_desc[i].bmAttributes;
768                 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
769
770                 epaddr = iface->ep_desc[i].bEndpointAddress;
771                 is_input = epaddr & USB_DIR_IN;
772                 epaddr &= USB_ENDPOINT_NUMBER_MASK;
773
774                 if (eptype == USB_ENDPOINT_XFER_BULK) {
775                         if (is_input && !ep_in_found) {
776                                 ss->ep_in = epaddr;
777                                 ep_in_found++;
778                         }
779                         if (!is_input && !ep_out_found) {
780                                 ss->ep_out = epaddr;
781                                 ep_out_found++;
782                         }
783                 }
784
785                 if (eptype == USB_ENDPOINT_XFER_INT) {
786                         if (is_input && !ep_intr_found) {
787                                 ss->ep_int = epaddr;
788                                 ss->irqinterval = iface->ep_desc[i].bInterval;
789                                 ep_intr_found++;
790                         }
791                 }
792         }
793         debug("endpoints: in %d, out %d, intr %d\n",
794               ss->ep_in, ss->ep_out, ss->ep_int);
795
796         /* apply basic sanity checks */
797         if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
798             !ss->ep_in || !ss->ep_out || !ss->ep_int) {
799                 debug("device probe incomplete\n");
800                 return 0;
801         }
802
803         dev->privptr = ss;
804         return 1;
805 }
806
807 /*
808  * mcs7830_eth_get_info() - network driver's get_info callback
809  * @dev:        detected USB device
810  * @ss:         USB ethernet data structure filled in at probe()
811  * @eth:        ethernet interface data structure to fill in
812  * Return: #1 upon success, #0 upon error
813  *
814  * this routine registers the mandatory init(), send(), recv(), and
815  * halt() callbacks with the ethernet interface, can register the
816  * optional write_hwaddr() callback with the ethernet interface,
817  * and initiates configuration of the interface such that subsequent
818  * calls to those callbacks results in network communication
819  */
820 int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
821                          struct eth_device *eth)
822 {
823         debug("%s()\n", __func__);
824         if (!eth) {
825                 debug("%s: missing parameter.\n", __func__);
826                 return 0;
827         }
828
829         snprintf(eth->name, sizeof(eth->name), "%s%d",
830                  MCS7830_BASE_NAME, mcs7830_iface_idx++);
831         eth->init = mcs7830_init;
832         eth->send = mcs7830_send;
833         eth->recv = mcs7830_recv;
834         eth->halt = mcs7830_halt;
835         eth->write_hwaddr = mcs7830_write_mac;
836         eth->priv = ss;
837
838         if (mcs7830_basic_reset(ss->pusb_dev, ss->dev_priv))
839                 return 0;
840
841         if (mcs7830_read_mac(ss->pusb_dev, eth->enetaddr))
842                 return 0;
843         debug("MAC %pM\n", eth->enetaddr);
844
845         return 1;
846 }
847 #endif
848
849
850 #ifdef CONFIG_DM_ETH
851 static int mcs7830_eth_start(struct udevice *dev)
852 {
853         struct usb_device *udev = dev_get_parent_priv(dev);
854
855         return mcs7830_init_common(udev);
856 }
857
858 void mcs7830_eth_stop(struct udevice *dev)
859 {
860         debug("** %s()\n", __func__);
861 }
862
863 int mcs7830_eth_send(struct udevice *dev, void *packet, int length)
864 {
865         struct mcs7830_private *priv = dev_get_priv(dev);
866         struct ueth_data *ueth = &priv->ueth;
867
868         return mcs7830_send_common(ueth, packet, length);
869 }
870
871 int mcs7830_eth_recv(struct udevice *dev, int flags, uchar **packetp)
872 {
873         struct mcs7830_private *priv = dev_get_priv(dev);
874         struct ueth_data *ueth = &priv->ueth;
875         int len;
876
877         len = mcs7830_recv_common(ueth, priv->rx_buf);
878         *packetp = priv->rx_buf;
879
880         return len;
881 }
882
883 static int mcs7830_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
884 {
885         struct mcs7830_private *priv = dev_get_priv(dev);
886
887         packet_len = ALIGN(packet_len, 4);
888         usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
889
890         return 0;
891 }
892
893 int mcs7830_write_hwaddr(struct udevice *dev)
894 {
895         struct usb_device *udev = dev_get_parent_priv(dev);
896         struct eth_pdata *pdata = dev_get_platdata(dev);
897
898         return mcs7830_write_mac_common(udev, pdata->enetaddr);
899 }
900
901 static int mcs7830_eth_probe(struct udevice *dev)
902 {
903         struct usb_device *udev = dev_get_parent_priv(dev);
904         struct mcs7830_private *priv = dev_get_priv(dev);
905         struct eth_pdata *pdata = dev_get_platdata(dev);
906         struct ueth_data *ueth = &priv->ueth;
907
908         if (mcs7830_basic_reset(udev, priv))
909                 return 0;
910
911         if (mcs7830_read_mac(udev, pdata->enetaddr))
912                 return 0;
913
914         return usb_ether_register(dev, ueth, MCS7830_RX_URB_SIZE);
915 }
916
917 static const struct eth_ops mcs7830_eth_ops = {
918         .start  = mcs7830_eth_start,
919         .send   = mcs7830_eth_send,
920         .recv   = mcs7830_eth_recv,
921         .free_pkt = mcs7830_free_pkt,
922         .stop   = mcs7830_eth_stop,
923         .write_hwaddr = mcs7830_write_hwaddr,
924 };
925
926 U_BOOT_DRIVER(mcs7830_eth) = {
927         .name   = "mcs7830_eth",
928         .id     = UCLASS_ETH,
929         .probe = mcs7830_eth_probe,
930         .ops    = &mcs7830_eth_ops,
931         .priv_auto_alloc_size = sizeof(struct mcs7830_private),
932         .platdata_auto_alloc_size = sizeof(struct eth_pdata),
933         .flags  = DM_FLAG_ALLOC_PRIV_DMA,
934 };
935
936 static const struct usb_device_id mcs7830_eth_id_table[] = {
937         { USB_DEVICE(0x9710, 0x7832) },         /* Moschip 7832 */
938         { USB_DEVICE(0x9710, 0x7830), },        /* Moschip 7830 */
939         { USB_DEVICE(0x9710, 0x7730), },        /* Moschip 7730 */
940         { USB_DEVICE(0x0df6, 0x0021), },        /* Sitecom LN 30 */
941         { }             /* Terminating entry */
942 };
943
944 U_BOOT_USB_DEVICE(mcs7830_eth, mcs7830_eth_id_table);
945 #endif