1
2
3
4
5
6
7
8
9
10
11
12
13#include <common.h>
14#include <dm.h>
15#include <errno.h>
16#include <linux/mii.h>
17#include <malloc.h>
18#include <memalign.h>
19#include <usb.h>
20
21#include "usb_ether.h"
22
23#define MCS7830_BASE_NAME "mcs"
24
25#define USBCALL_TIMEOUT 1000
26#define LINKSTATUS_TIMEOUT 5000
27#define LINKSTATUS_TIMEOUT_RES 50
28
29#define MCS7830_RX_URB_SIZE 2048
30
31
32#define MCS7830_WR_BREQ 0x0d
33#define MCS7830_RD_BREQ 0x0e
34
35
36struct mcs7830_regs {
37 uint8_t multicast_hashes[8];
38 uint8_t packet_gap[2];
39 uint8_t phy_data[2];
40 uint8_t phy_command[2];
41 uint8_t configuration;
42 uint8_t ether_address[6];
43 uint8_t frame_drop_count;
44 uint8_t pause_threshold;
45};
46#define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes)
47#define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data)
48#define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command)
49#define REG_CONFIG offsetof(struct mcs7830_regs, configuration)
50#define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address)
51#define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count)
52#define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold)
53
54
55#define PHY_CMD1_READ 0x40
56#define PHY_CMD1_WRITE 0x20
57#define PHY_CMD1_PHYADDR 0x01
58
59#define PHY_CMD2_PEND 0x80
60#define PHY_CMD2_READY 0x40
61
62#define CONF_CFG 0x80
63#define CONF_SPEED100 0x40
64#define CONF_FDX_ENABLE 0x20
65#define CONF_RXENABLE 0x10
66#define CONF_TXENABLE 0x08
67#define CONF_SLEEPMODE 0x04
68#define CONF_ALLMULTICAST 0x02
69#define CONF_PROMISCUOUS 0x01
70
71#define PAUSE_THRESHOLD_DEFAULT 0
72
73
74#define STAT_RX_FRAME_CORRECT 0x20
75#define STAT_RX_LARGE_FRAME 0x10
76#define STAT_RX_CRC_ERROR 0x08
77#define STAT_RX_ALIGNMENT_ERROR 0x04
78#define STAT_RX_LENGTH_ERROR 0x02
79#define STAT_RX_SHORT_FRAME 0x01
80
81
82
83
84
85
86struct mcs7830_private {
87#ifdef CONFIG_DM_ETH
88 uint8_t rx_buf[MCS7830_RX_URB_SIZE];
89 struct ueth_data ueth;
90#endif
91 uint8_t config;
92 uint8_t mchash[8];
93};
94
95
96
97
98
99
100
101
102
103static int mcs7830_read_reg(struct usb_device *udev, uint8_t idx,
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
111 len = usb_control_msg(udev,
112 usb_rcvctrlpipe(udev, 0),
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
127
128
129
130
131
132
133static int mcs7830_write_reg(struct usb_device *udev, uint8_t idx,
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);
142 len = usb_control_msg(udev,
143 usb_sndctrlpipe(udev, 0),
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
157
158
159
160
161
162static int mcs7830_phy_emit_wait(struct usb_device *udev,
163 uint8_t rwflag, uint8_t index)
164{
165 int rc;
166 int retry;
167 uint8_t cmd[2];
168
169
170 cmd[0] = rwflag | PHY_CMD1_PHYADDR;
171 cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
172 rc = mcs7830_write_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
173 if (rc < 0)
174 return rc;
175
176
177 retry = 10;
178 do {
179 rc = mcs7830_read_reg(udev, REG_PHY_CMD, sizeof(cmd), cmd);
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
189}
190
191
192
193
194
195
196
197static int mcs7830_read_phy(struct usb_device *udev, uint8_t index)
198{
199 int rc;
200 uint16_t val;
201
202
203 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_READ, index);
204 if (rc < 0)
205 return rc;
206
207
208 rc = mcs7830_read_reg(udev, REG_PHY_DATA, sizeof(val), &val);
209 if (rc < 0)
210 return rc;
211 rc = le16_to_cpu(val);
212 debug("%s(%d) => 0x%04X\n", __func__, index, rc);
213 return rc;
214}
215
216
217
218
219
220
221
222
223static int mcs7830_write_phy(struct usb_device *udev, uint8_t index,
224 uint16_t val)
225{
226 int rc;
227
228 debug("%s(%d, 0x%04X)\n", __func__, index, val);
229
230
231 val = cpu_to_le16(val);
232 rc = mcs7830_write_reg(udev, REG_PHY_DATA, sizeof(val), &val);
233 if (rc < 0)
234 return rc;
235
236
237 rc = mcs7830_phy_emit_wait(udev, PHY_CMD1_WRITE, index);
238 if (rc < 0)
239 return rc;
240
241 return 0;
242}
243
244
245
246
247
248
249
250
251
252
253static int mcs7830_write_config(struct usb_device *udev,
254 struct mcs7830_private *priv)
255{
256 int rc;
257
258 debug("%s()\n", __func__);
259
260 rc = mcs7830_write_reg(udev, REG_CONFIG,
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
272
273
274
275
276
277
278
279static int mcs7830_write_mchash(struct usb_device *udev,
280 struct mcs7830_private *priv)
281{
282 int rc;
283
284 debug("%s()\n", __func__);
285
286 rc = mcs7830_write_reg(udev, REG_MULTICAST_HASH,
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
298
299
300
301
302
303static int mcs7830_set_autoneg(struct usb_device *udev)
304{
305 int adv, flg;
306 int rc;
307
308 debug("%s()\n", __func__);
309
310
311
312
313
314
315
316
317
318 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
319 rc = mcs7830_write_phy(udev, MII_ADVERTISE, adv);
320
321 flg = 0;
322 if (!rc)
323 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
324
325 flg |= BMCR_ANENABLE;
326 if (!rc)
327 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
328
329 flg |= BMCR_ANRESTART;
330 if (!rc)
331 rc = mcs7830_write_phy(udev, MII_BMCR, flg);
332
333 return rc;
334}
335
336
337
338
339
340
341
342
343
344static int mcs7830_get_rev(struct usb_device *udev)
345{
346 uint8_t buf[2];
347 int rc;
348 int rev;
349
350
351 rc = mcs7830_read_reg(udev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
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
362
363
364
365
366
367
368static int mcs7830_apply_fixup(struct usb_device *udev)
369{
370 int rev;
371 int i;
372 uint8_t thr;
373
374 rev = mcs7830_get_rev(udev);
375 debug("%s() rev=%d\n", __func__, rev);
376
377
378
379
380
381
382 if (rev == 2) {
383 debug("%s: applying rev C fixup\n", __func__);
384 thr = PAUSE_THRESHOLD_DEFAULT;
385 for (i = 0; i < 2; i++) {
386 (void)mcs7830_write_reg(udev, REG_PAUSE_THRESHOLD,
387 sizeof(thr), &thr);
388 mdelay(1);
389 }
390 }
391
392 return 0;
393}
394
395
396
397
398
399
400
401
402
403
404static int mcs7830_basic_reset(struct usb_device *udev,
405 struct mcs7830_private *priv)
406{
407 int rc;
408
409 debug("%s()\n", __func__);
410
411
412
413
414
415
416 priv->config = CONF_TXENABLE;
417 priv->config |= CONF_ALLMULTICAST;
418
419 rc = mcs7830_set_autoneg(udev);
420 if (rc < 0) {
421 error("setting autoneg failed\n");
422 return rc;
423 }
424
425 rc = mcs7830_write_mchash(udev, priv);
426 if (rc < 0) {
427 error("failed to set multicast hash\n");
428 return rc;
429 }
430
431 rc = mcs7830_write_config(udev, priv);
432 if (rc < 0) {
433 error("failed to set configuration\n");
434 return rc;
435 }
436
437 rc = mcs7830_apply_fixup(udev);
438 if (rc < 0) {
439 error("fixup application failed\n");
440 return rc;
441 }
442
443 return 0;
444}
445
446
447
448
449
450
451
452
453
454
455static int mcs7830_read_mac(struct usb_device *udev, unsigned char enetaddr[])
456{
457 int rc;
458 uint8_t buf[ETH_ALEN];
459
460 debug("%s()\n", __func__);
461
462 rc = mcs7830_read_reg(udev, REG_ETHER_ADDR, ETH_ALEN, buf);
463 if (rc < 0) {
464 debug("reading MAC from adapter failed\n");
465 return rc;
466 }
467
468 memcpy(enetaddr, buf, ETH_ALEN);
469 return 0;
470}
471
472static int mcs7830_write_mac_common(struct usb_device *udev,
473 unsigned char enetaddr[])
474{
475 int rc;
476
477 debug("%s()\n", __func__);
478
479 rc = mcs7830_write_reg(udev, REG_ETHER_ADDR, ETH_ALEN, enetaddr);
480 if (rc < 0) {
481 debug("writing MAC to adapter failed\n");
482 return rc;
483 }
484 return 0;
485}
486
487static int mcs7830_init_common(struct usb_device *udev)
488{
489 int timeout;
490 int have_link;
491
492 debug("%s()\n", __func__);
493
494 timeout = 0;
495 do {
496 have_link = mcs7830_read_phy(udev, MII_BMSR) & BMSR_LSTATUS;
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
509static int mcs7830_send_common(struct ueth_data *ueth, void *packet,
510 int length)
511{
512 struct usb_device *udev = ueth->pusb_dev;
513 int rc;
514 int gotlen;
515
516 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
517
518 memcpy(buf, packet, length);
519 rc = usb_bulk_msg(udev,
520 usb_sndbulkpipe(udev, ueth->ep_out),
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
528static int mcs7830_recv_common(struct ueth_data *ueth, uint8_t *buf)
529{
530 int rc, wantlen, gotlen;
531 uint8_t sts;
532
533 debug("%s()\n", __func__);
534
535
536 wantlen = MCS7830_RX_URB_SIZE;
537 rc = usb_bulk_msg(ueth->pusb_dev,
538 usb_rcvbulkpipe(ueth->pusb_dev, ueth->ep_in),
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) {
544 error("RX: failed to receive\n");
545 return rc;
546 }
547 if (gotlen > wantlen) {
548 error("RX: got too many bytes (%d)\n", gotlen);
549 return -EIO;
550 }
551
552
553
554
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);
563 return gotlen;
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
576#ifndef CONFIG_DM_ETH
577
578
579
580
581
582
583
584
585
586
587static int mcs7830_init(struct eth_device *eth, bd_t *bd)
588{
589 struct ueth_data *dev = eth->priv;
590
591 return mcs7830_init_common(dev->pusb_dev);
592}
593
594
595
596
597
598
599
600
601
602
603static int mcs7830_send(struct eth_device *eth, void *packet, int length)
604{
605 struct ueth_data *dev = eth->priv;
606
607 return mcs7830_send_common(dev, packet, length);
608}
609
610
611
612
613
614
615
616
617
618static int mcs7830_recv(struct eth_device *eth)
619{
620 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
621 struct ueth_data *ueth = eth->priv;
622 int len;
623
624 len = mcs7830_recv_common(ueth, buf);
625 if (len <= 0)
626 net_process_received_packet(buf, len);
627
628 return 0;
629}
630
631
632
633
634
635
636
637
638
639static void mcs7830_halt(struct eth_device *eth)
640{
641 debug("%s()\n", __func__);
642}
643
644
645
646
647
648
649
650
651
652
653static int mcs7830_write_mac(struct eth_device *eth)
654{
655 struct ueth_data *ueth = eth->priv;
656
657 return mcs7830_write_mac_common(ueth->pusb_dev, eth->enetaddr);
658}
659
660
661
662
663
664
665
666static int mcs7830_iface_idx;
667
668
669
670
671
672
673
674
675void mcs7830_eth_before_probe(void)
676{
677 mcs7830_iface_idx = 0;
678}
679
680
681
682
683
684
685
686
687
688
689
690
691struct mcs7830_dongle {
692 uint16_t vendor;
693 uint16_t product;
694};
695
696
697
698
699static const struct mcs7830_dongle mcs7830_dongles[] = {
700 { 0x9710, 0x7832, },
701 { 0x9710, 0x7830, },
702 { 0x9710, 0x7730, },
703 { 0x0df6, 0x0021, },
704};
705
706
707
708
709
710
711
712
713
714
715
716
717int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
718 struct ueth_data *ss)
719{
720 struct usb_interface *iface;
721 struct usb_interface_descriptor *iface_desc;
722 int i;
723 struct mcs7830_private *priv;
724 int ep_in_found, ep_out_found, ep_intr_found;
725
726 debug("%s()\n", __func__);
727
728
729 iface = &dev->config.if_desc[ifnum];
730 iface_desc = &iface->desc;
731 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
732 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
733 dev->descriptor.idProduct == mcs7830_dongles[i].product)
734 break;
735 }
736 if (i == ARRAY_SIZE(mcs7830_dongles))
737 return 0;
738 debug("detected USB ethernet device: %04X:%04X\n",
739 dev->descriptor.idVendor, dev->descriptor.idProduct);
740
741
742 priv = calloc(1, sizeof(*priv));
743 if (!priv)
744 return 0;
745
746
747 memset(ss, 0, sizeof(*ss));
748 ss->ifnum = ifnum;
749 ss->pusb_dev = dev;
750 ss->subclass = iface_desc->bInterfaceSubClass;
751 ss->protocol = iface_desc->bInterfaceProtocol;
752 ss->dev_priv = priv;
753
754
755
756
757
758 ep_in_found = ep_out_found = ep_intr_found = 0;
759 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
760 uint8_t eptype, epaddr;
761 bool is_input;
762
763 eptype = iface->ep_desc[i].bmAttributes;
764 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
765
766 epaddr = iface->ep_desc[i].bEndpointAddress;
767 is_input = epaddr & USB_DIR_IN;
768 epaddr &= USB_ENDPOINT_NUMBER_MASK;
769
770 if (eptype == USB_ENDPOINT_XFER_BULK) {
771 if (is_input && !ep_in_found) {
772 ss->ep_in = epaddr;
773 ep_in_found++;
774 }
775 if (!is_input && !ep_out_found) {
776 ss->ep_out = epaddr;
777 ep_out_found++;
778 }
779 }
780
781 if (eptype == USB_ENDPOINT_XFER_INT) {
782 if (is_input && !ep_intr_found) {
783 ss->ep_int = epaddr;
784 ss->irqinterval = iface->ep_desc[i].bInterval;
785 ep_intr_found++;
786 }
787 }
788 }
789 debug("endpoints: in %d, out %d, intr %d\n",
790 ss->ep_in, ss->ep_out, ss->ep_int);
791
792
793 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
794 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
795 debug("device probe incomplete\n");
796 return 0;
797 }
798
799 dev->privptr = ss;
800 return 1;
801}
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
817 struct eth_device *eth)
818{
819 debug("%s()\n", __func__);
820 if (!eth) {
821 debug("%s: missing parameter.\n", __func__);
822 return 0;
823 }
824
825 snprintf(eth->name, sizeof(eth->name), "%s%d",
826 MCS7830_BASE_NAME, mcs7830_iface_idx++);
827 eth->init = mcs7830_init;
828 eth->send = mcs7830_send;
829 eth->recv = mcs7830_recv;
830 eth->halt = mcs7830_halt;
831 eth->write_hwaddr = mcs7830_write_mac;
832 eth->priv = ss;
833
834 if (mcs7830_basic_reset(ss->pusb_dev, ss->dev_priv))
835 return 0;
836
837 if (mcs7830_read_mac(ss->pusb_dev, eth->enetaddr))
838 return 0;
839 debug("MAC %pM\n", eth->enetaddr);
840
841 return 1;
842}
843#endif
844
845
846#ifdef CONFIG_DM_ETH
847static int mcs7830_eth_start(struct udevice *dev)
848{
849 struct usb_device *udev = dev_get_parent_priv(dev);
850
851 return mcs7830_init_common(udev);
852}
853
854void mcs7830_eth_stop(struct udevice *dev)
855{
856 debug("** %s()\n", __func__);
857}
858
859int mcs7830_eth_send(struct udevice *dev, void *packet, int length)
860{
861 struct mcs7830_private *priv = dev_get_priv(dev);
862 struct ueth_data *ueth = &priv->ueth;
863
864 return mcs7830_send_common(ueth, packet, length);
865}
866
867int mcs7830_eth_recv(struct udevice *dev, int flags, uchar **packetp)
868{
869 struct mcs7830_private *priv = dev_get_priv(dev);
870 struct ueth_data *ueth = &priv->ueth;
871 int len;
872
873 len = mcs7830_recv_common(ueth, priv->rx_buf);
874 *packetp = priv->rx_buf;
875
876 return len;
877}
878
879static int mcs7830_free_pkt(struct udevice *dev, uchar *packet, int packet_len)
880{
881 struct mcs7830_private *priv = dev_get_priv(dev);
882
883 packet_len = ALIGN(packet_len, 4);
884 usb_ether_advance_rxbuf(&priv->ueth, sizeof(u32) + packet_len);
885
886 return 0;
887}
888
889int mcs7830_write_hwaddr(struct udevice *dev)
890{
891 struct usb_device *udev = dev_get_parent_priv(dev);
892 struct eth_pdata *pdata = dev_get_platdata(dev);
893
894 return mcs7830_write_mac_common(udev, pdata->enetaddr);
895}
896
897static int mcs7830_eth_probe(struct udevice *dev)
898{
899 struct usb_device *udev = dev_get_parent_priv(dev);
900 struct mcs7830_private *priv = dev_get_priv(dev);
901 struct eth_pdata *pdata = dev_get_platdata(dev);
902 struct ueth_data *ueth = &priv->ueth;
903
904 if (mcs7830_basic_reset(udev, priv))
905 return 0;
906
907 if (mcs7830_read_mac(udev, pdata->enetaddr))
908 return 0;
909
910 return usb_ether_register(dev, ueth, MCS7830_RX_URB_SIZE);
911}
912
913static const struct eth_ops mcs7830_eth_ops = {
914 .start = mcs7830_eth_start,
915 .send = mcs7830_eth_send,
916 .recv = mcs7830_eth_recv,
917 .free_pkt = mcs7830_free_pkt,
918 .stop = mcs7830_eth_stop,
919 .write_hwaddr = mcs7830_write_hwaddr,
920};
921
922U_BOOT_DRIVER(mcs7830_eth) = {
923 .name = "mcs7830_eth",
924 .id = UCLASS_ETH,
925 .probe = mcs7830_eth_probe,
926 .ops = &mcs7830_eth_ops,
927 .priv_auto_alloc_size = sizeof(struct mcs7830_private),
928 .platdata_auto_alloc_size = sizeof(struct eth_pdata),
929 .flags = DM_FLAG_ALLOC_PRIV_DMA,
930};
931
932static const struct usb_device_id mcs7830_eth_id_table[] = {
933 { USB_DEVICE(0x9710, 0x7832) },
934 { USB_DEVICE(0x9710, 0x7830), },
935 { USB_DEVICE(0x9710, 0x7730), },
936 { USB_DEVICE(0x0df6, 0x0021), },
937 { }
938};
939
940U_BOOT_USB_DEVICE(mcs7830_eth, mcs7830_eth_id_table);
941#endif
942