1
2
3
4
5
6
7
8
9
10
11
12
13#include <common.h>
14#include <errno.h>
15#include <linux/mii.h>
16#include <malloc.h>
17#include <usb.h>
18
19#include "usb_ether.h"
20
21#define MCS7830_BASE_NAME "mcs"
22
23#define USBCALL_TIMEOUT 1000
24#define LINKSTATUS_TIMEOUT 5000
25#define LINKSTATUS_TIMEOUT_RES 50
26
27#define MCS7830_RX_URB_SIZE 2048
28
29
30#define MCS7830_WR_BREQ 0x0d
31#define MCS7830_RD_BREQ 0x0e
32
33
34struct mcs7830_regs {
35 uint8_t multicast_hashes[8];
36 uint8_t packet_gap[2];
37 uint8_t phy_data[2];
38 uint8_t phy_command[2];
39 uint8_t configuration;
40 uint8_t ether_address[6];
41 uint8_t frame_drop_count;
42 uint8_t pause_threshold;
43};
44#define REG_MULTICAST_HASH offsetof(struct mcs7830_regs, multicast_hashes)
45#define REG_PHY_DATA offsetof(struct mcs7830_regs, phy_data)
46#define REG_PHY_CMD offsetof(struct mcs7830_regs, phy_command)
47#define REG_CONFIG offsetof(struct mcs7830_regs, configuration)
48#define REG_ETHER_ADDR offsetof(struct mcs7830_regs, ether_address)
49#define REG_FRAME_DROP_COUNTER offsetof(struct mcs7830_regs, frame_drop_count)
50#define REG_PAUSE_THRESHOLD offsetof(struct mcs7830_regs, pause_threshold)
51
52
53#define PHY_CMD1_READ 0x40
54#define PHY_CMD1_WRITE 0x20
55#define PHY_CMD1_PHYADDR 0x01
56
57#define PHY_CMD2_PEND 0x80
58#define PHY_CMD2_READY 0x40
59
60#define CONF_CFG 0x80
61#define CONF_SPEED100 0x40
62#define CONF_FDX_ENABLE 0x20
63#define CONF_RXENABLE 0x10
64#define CONF_TXENABLE 0x08
65#define CONF_SLEEPMODE 0x04
66#define CONF_ALLMULTICAST 0x02
67#define CONF_PROMISCUOUS 0x01
68
69#define PAUSE_THRESHOLD_DEFAULT 0
70
71
72#define STAT_RX_FRAME_CORRECT 0x20
73#define STAT_RX_LARGE_FRAME 0x10
74#define STAT_RX_CRC_ERROR 0x08
75#define STAT_RX_ALIGNMENT_ERROR 0x04
76#define STAT_RX_LENGTH_ERROR 0x02
77#define STAT_RX_SHORT_FRAME 0x01
78
79
80
81
82
83
84struct mcs7830_private {
85 uint8_t config;
86 uint8_t mchash[8];
87};
88
89
90
91
92
93
94
95
96
97static int mcs7830_read_reg(struct ueth_data *dev, uint8_t idx,
98 uint16_t size, void *data)
99{
100 int len;
101 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
102
103 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
104
105 len = usb_control_msg(dev->pusb_dev,
106 usb_rcvctrlpipe(dev->pusb_dev, 0),
107 MCS7830_RD_BREQ,
108 USB_DIR_IN | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
109 0, idx, buf, size,
110 USBCALL_TIMEOUT);
111 if (len != size) {
112 debug("%s() len=%d != sz=%d\n", __func__, len, size);
113 return -EIO;
114 }
115 memcpy(data, buf, size);
116 return 0;
117}
118
119
120
121
122
123
124
125
126
127static int mcs7830_write_reg(struct ueth_data *dev, uint8_t idx,
128 uint16_t size, void *data)
129{
130 int len;
131 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, size);
132
133 debug("%s() idx=0x%04X sz=%d\n", __func__, idx, size);
134
135 memcpy(buf, data, size);
136 len = usb_control_msg(dev->pusb_dev,
137 usb_sndctrlpipe(dev->pusb_dev, 0),
138 MCS7830_WR_BREQ,
139 USB_DIR_OUT | USB_TYPE_VENDOR | USB_RECIP_DEVICE,
140 0, idx, buf, size,
141 USBCALL_TIMEOUT);
142 if (len != size) {
143 debug("%s() len=%d != sz=%d\n", __func__, len, size);
144 return -EIO;
145 }
146 return 0;
147}
148
149
150
151
152
153
154
155
156static int mcs7830_phy_emit_wait(struct ueth_data *dev,
157 uint8_t rwflag, uint8_t index)
158{
159 int rc;
160 int retry;
161 uint8_t cmd[2];
162
163
164 cmd[0] = rwflag | PHY_CMD1_PHYADDR;
165 cmd[1] = PHY_CMD2_PEND | (index & 0x1f);
166 rc = mcs7830_write_reg(dev, REG_PHY_CMD, sizeof(cmd), cmd);
167 if (rc < 0)
168 return rc;
169
170
171 retry = 10;
172 do {
173 rc = mcs7830_read_reg(dev, REG_PHY_CMD, sizeof(cmd), cmd);
174 if (rc < 0)
175 return rc;
176 if (cmd[1] & PHY_CMD2_READY)
177 return 0;
178 if (!retry--)
179 return -ETIMEDOUT;
180 mdelay(1);
181 } while (1);
182
183}
184
185
186
187
188
189
190
191static int mcs7830_read_phy(struct ueth_data *dev, uint8_t index)
192{
193 int rc;
194 uint16_t val;
195
196
197 rc = mcs7830_phy_emit_wait(dev, PHY_CMD1_READ, index);
198 if (rc < 0)
199 return rc;
200
201
202 rc = mcs7830_read_reg(dev, REG_PHY_DATA, sizeof(val), &val);
203 if (rc < 0)
204 return rc;
205 rc = le16_to_cpu(val);
206 debug("%s(%s, %d) => 0x%04X\n", __func__, dev->eth_dev.name, index, rc);
207 return rc;
208}
209
210
211
212
213
214
215
216
217static int mcs7830_write_phy(struct ueth_data *dev, uint8_t index, uint16_t val)
218{
219 int rc;
220
221 debug("%s(%s, %d, 0x%04X)\n", __func__, dev->eth_dev.name, index, val);
222
223
224 val = cpu_to_le16(val);
225 rc = mcs7830_write_reg(dev, REG_PHY_DATA, sizeof(val), &val);
226 if (rc < 0)
227 return rc;
228
229
230 rc = mcs7830_phy_emit_wait(dev, PHY_CMD1_WRITE, index);
231 if (rc < 0)
232 return rc;
233
234 return 0;
235}
236
237
238
239
240
241
242
243
244
245static int mcs7830_write_config(struct ueth_data *dev)
246{
247 struct mcs7830_private *priv;
248 int rc;
249
250 debug("%s()\n", __func__);
251 priv = dev->dev_priv;
252
253 rc = mcs7830_write_reg(dev, REG_CONFIG,
254 sizeof(priv->config), &priv->config);
255 if (rc < 0) {
256 debug("writing config to adapter failed\n");
257 return rc;
258 }
259
260 return 0;
261}
262
263
264
265
266
267
268
269
270
271static int mcs7830_write_mchash(struct ueth_data *dev)
272{
273 struct mcs7830_private *priv;
274 int rc;
275
276 debug("%s()\n", __func__);
277 priv = dev->dev_priv;
278
279 rc = mcs7830_write_reg(dev, REG_MULTICAST_HASH,
280 sizeof(priv->mchash), &priv->mchash);
281 if (rc < 0) {
282 debug("writing multicast hash to adapter failed\n");
283 return rc;
284 }
285
286 return 0;
287}
288
289
290
291
292
293
294
295
296static int mcs7830_set_autoneg(struct ueth_data *dev)
297{
298 int adv, flg;
299 int rc;
300
301 debug("%s()\n", __func__);
302
303
304
305
306
307
308
309
310
311 adv = ADVERTISE_PAUSE_CAP | ADVERTISE_ALL | ADVERTISE_CSMA;
312 rc = mcs7830_write_phy(dev, MII_ADVERTISE, adv);
313
314 flg = 0;
315 if (!rc)
316 rc = mcs7830_write_phy(dev, MII_BMCR, flg);
317
318 flg |= BMCR_ANENABLE;
319 if (!rc)
320 rc = mcs7830_write_phy(dev, MII_BMCR, flg);
321
322 flg |= BMCR_ANRESTART;
323 if (!rc)
324 rc = mcs7830_write_phy(dev, MII_BMCR, flg);
325
326 return rc;
327}
328
329
330
331
332
333
334
335
336
337static int mcs7830_get_rev(struct ueth_data *dev)
338{
339 uint8_t buf[2];
340 int rc;
341 int rev;
342
343
344 rc = mcs7830_read_reg(dev, REG_FRAME_DROP_COUNTER, sizeof(buf), buf);
345 if (rc < 0)
346 rev = 1;
347 else
348 rev = 2;
349 debug("%s() rc=%d, rev=%d\n", __func__, rc, rev);
350 return rev;
351}
352
353
354
355
356
357
358
359
360
361static int mcs7830_apply_fixup(struct ueth_data *dev)
362{
363 int rev;
364 int i;
365 uint8_t thr;
366
367 rev = mcs7830_get_rev(dev);
368 debug("%s() rev=%d\n", __func__, rev);
369
370
371
372
373
374
375 if (rev == 2) {
376 debug("%s: applying rev C fixup\n", dev->eth_dev.name);
377 thr = PAUSE_THRESHOLD_DEFAULT;
378 for (i = 0; i < 2; i++) {
379 (void)mcs7830_write_reg(dev, REG_PAUSE_THRESHOLD,
380 sizeof(thr), &thr);
381 mdelay(1);
382 }
383 }
384
385 return 0;
386}
387
388
389
390
391
392
393
394
395
396
397static int mcs7830_basic_reset(struct ueth_data *dev)
398{
399 struct mcs7830_private *priv;
400 int rc;
401
402 debug("%s()\n", __func__);
403 priv = dev->dev_priv;
404
405
406
407
408
409
410 priv->config = CONF_TXENABLE;
411 priv->config |= CONF_ALLMULTICAST;
412
413 rc = mcs7830_set_autoneg(dev);
414 if (rc < 0) {
415 error("setting autoneg failed\n");
416 return rc;
417 }
418
419 rc = mcs7830_write_mchash(dev);
420 if (rc < 0) {
421 error("failed to set multicast hash\n");
422 return rc;
423 }
424
425 rc = mcs7830_write_config(dev);
426 if (rc < 0) {
427 error("failed to set configuration\n");
428 return rc;
429 }
430
431 rc = mcs7830_apply_fixup(dev);
432 if (rc < 0) {
433 error("fixup application failed\n");
434 return rc;
435 }
436
437 return 0;
438}
439
440
441
442
443
444
445
446
447
448static int mcs7830_read_mac(struct eth_device *eth)
449{
450 struct ueth_data *dev;
451 int rc;
452 uint8_t buf[ETH_ALEN];
453
454 debug("%s()\n", __func__);
455 dev = eth->priv;
456
457 rc = mcs7830_read_reg(dev, REG_ETHER_ADDR, ETH_ALEN, buf);
458 if (rc < 0) {
459 debug("reading MAC from adapter failed\n");
460 return rc;
461 }
462
463 memcpy(ð->enetaddr[0], buf, ETH_ALEN);
464 return 0;
465}
466
467
468
469
470
471
472
473
474
475
476static int mcs7830_write_mac(struct eth_device *eth)
477{
478 struct ueth_data *dev;
479 int rc;
480
481 debug("%s()\n", __func__);
482 dev = eth->priv;
483
484 if (sizeof(eth->enetaddr) != ETH_ALEN)
485 return -EINVAL;
486 rc = mcs7830_write_reg(dev, REG_ETHER_ADDR, ETH_ALEN, eth->enetaddr);
487 if (rc < 0) {
488 debug("writing MAC to adapter failed\n");
489 return rc;
490 }
491 return 0;
492}
493
494
495
496
497
498
499
500
501
502
503
504static int mcs7830_init(struct eth_device *eth, bd_t *bd)
505{
506 struct ueth_data *dev;
507 int timeout;
508 int have_link;
509
510 debug("%s()\n", __func__);
511 dev = eth->priv;
512
513 timeout = 0;
514 do {
515 have_link = mcs7830_read_phy(dev, MII_BMSR) & BMSR_LSTATUS;
516 if (have_link)
517 break;
518 udelay(LINKSTATUS_TIMEOUT_RES * 1000);
519 timeout += LINKSTATUS_TIMEOUT_RES;
520 } while (timeout < LINKSTATUS_TIMEOUT);
521 if (!have_link) {
522 debug("ethernet link is down\n");
523 return -ETIMEDOUT;
524 }
525 return 0;
526}
527
528
529
530
531
532
533
534
535
536
537static int mcs7830_send(struct eth_device *eth, void *packet, int length)
538{
539 struct ueth_data *dev;
540 int rc;
541 int gotlen;
542
543 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, PKTSIZE + sizeof(uint8_t));
544
545 dev = eth->priv;
546
547 memcpy(buf, packet, length);
548 rc = usb_bulk_msg(dev->pusb_dev,
549 usb_sndbulkpipe(dev->pusb_dev, dev->ep_out),
550 &buf[0], length, &gotlen,
551 USBCALL_TIMEOUT);
552 debug("%s() TX want len %d, got len %d, rc %d\n",
553 __func__, length, gotlen, rc);
554 return rc;
555}
556
557
558
559
560
561
562
563
564
565static int mcs7830_recv(struct eth_device *eth)
566{
567 struct ueth_data *dev;
568 ALLOC_CACHE_ALIGN_BUFFER(uint8_t, buf, MCS7830_RX_URB_SIZE);
569 int rc, wantlen, gotlen;
570 uint8_t sts;
571
572 debug("%s()\n", __func__);
573 dev = eth->priv;
574
575
576 wantlen = MCS7830_RX_URB_SIZE;
577 rc = usb_bulk_msg(dev->pusb_dev,
578 usb_rcvbulkpipe(dev->pusb_dev, dev->ep_in),
579 &buf[0], wantlen, &gotlen,
580 USBCALL_TIMEOUT);
581 debug("%s() RX want len %d, got len %d, rc %d\n",
582 __func__, wantlen, gotlen, rc);
583 if (rc != 0) {
584 error("RX: failed to receive\n");
585 return rc;
586 }
587 if (gotlen > wantlen) {
588 error("RX: got too many bytes (%d)\n", gotlen);
589 return -EIO;
590 }
591
592
593
594
595
596 if (gotlen < sizeof(sts))
597 return -EIO;
598 gotlen -= sizeof(sts);
599 sts = buf[gotlen];
600
601 if (sts == STAT_RX_FRAME_CORRECT) {
602 debug("%s() got a frame, len=%d\n", __func__, gotlen);
603 NetReceive(buf, gotlen);
604 return 0;
605 }
606
607 debug("RX: frame error (sts 0x%02X, %s %s %s %s %s)\n",
608 sts,
609 (sts & STAT_RX_LARGE_FRAME) ? "large" : "-",
610 (sts & STAT_RX_LENGTH_ERROR) ? "length" : "-",
611 (sts & STAT_RX_SHORT_FRAME) ? "short" : "-",
612 (sts & STAT_RX_CRC_ERROR) ? "crc" : "-",
613 (sts & STAT_RX_ALIGNMENT_ERROR) ? "align" : "-");
614 return -EIO;
615}
616
617
618
619
620
621
622
623
624
625static void mcs7830_halt(struct eth_device *eth)
626{
627 debug("%s()\n", __func__);
628}
629
630
631
632
633
634
635
636static int mcs7830_iface_idx;
637
638
639
640
641
642
643
644
645void mcs7830_eth_before_probe(void)
646{
647 mcs7830_iface_idx = 0;
648}
649
650
651
652
653
654
655
656
657
658
659
660
661struct mcs7830_dongle {
662 uint16_t vendor;
663 uint16_t product;
664};
665
666
667
668
669static const struct mcs7830_dongle const mcs7830_dongles[] = {
670 { 0x9710, 0x7832, },
671 { 0x9710, 0x7830, },
672 { 0x9710, 0x7730, },
673 { 0x0df6, 0x0021, },
674};
675
676
677
678
679
680
681
682
683
684
685
686
687int mcs7830_eth_probe(struct usb_device *dev, unsigned int ifnum,
688 struct ueth_data *ss)
689{
690 struct usb_interface *iface;
691 struct usb_interface_descriptor *iface_desc;
692 int i;
693 struct mcs7830_private *priv;
694 int ep_in_found, ep_out_found, ep_intr_found;
695
696 debug("%s()\n", __func__);
697
698
699 iface = &dev->config.if_desc[ifnum];
700 iface_desc = &iface->desc;
701 for (i = 0; i < ARRAY_SIZE(mcs7830_dongles); i++) {
702 if (dev->descriptor.idVendor == mcs7830_dongles[i].vendor &&
703 dev->descriptor.idProduct == mcs7830_dongles[i].product)
704 break;
705 }
706 if (i == ARRAY_SIZE(mcs7830_dongles))
707 return 0;
708 debug("detected USB ethernet device: %04X:%04X\n",
709 dev->descriptor.idVendor, dev->descriptor.idProduct);
710
711
712 priv = calloc(1, sizeof(*priv));
713 if (!priv)
714 return 0;
715
716
717 memset(ss, 0, sizeof(*ss));
718 ss->ifnum = ifnum;
719 ss->pusb_dev = dev;
720 ss->subclass = iface_desc->bInterfaceSubClass;
721 ss->protocol = iface_desc->bInterfaceProtocol;
722 ss->dev_priv = priv;
723
724
725
726
727
728 ep_in_found = ep_out_found = ep_intr_found = 0;
729 for (i = 0; i < iface_desc->bNumEndpoints; i++) {
730 uint8_t eptype, epaddr;
731 bool is_input;
732
733 eptype = iface->ep_desc[i].bmAttributes;
734 eptype &= USB_ENDPOINT_XFERTYPE_MASK;
735
736 epaddr = iface->ep_desc[i].bEndpointAddress;
737 is_input = epaddr & USB_DIR_IN;
738 epaddr &= USB_ENDPOINT_NUMBER_MASK;
739
740 if (eptype == USB_ENDPOINT_XFER_BULK) {
741 if (is_input && !ep_in_found) {
742 ss->ep_in = epaddr;
743 ep_in_found++;
744 }
745 if (!is_input && !ep_out_found) {
746 ss->ep_out = epaddr;
747 ep_out_found++;
748 }
749 }
750
751 if (eptype == USB_ENDPOINT_XFER_INT) {
752 if (is_input && !ep_intr_found) {
753 ss->ep_int = epaddr;
754 ss->irqinterval = iface->ep_desc[i].bInterval;
755 ep_intr_found++;
756 }
757 }
758 }
759 debug("endpoints: in %d, out %d, intr %d\n",
760 ss->ep_in, ss->ep_out, ss->ep_int);
761
762
763 if (usb_set_interface(dev, iface_desc->bInterfaceNumber, 0) ||
764 !ss->ep_in || !ss->ep_out || !ss->ep_int) {
765 debug("device probe incomplete\n");
766 return 0;
767 }
768
769 dev->privptr = ss;
770 return 1;
771}
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786int mcs7830_eth_get_info(struct usb_device *dev, struct ueth_data *ss,
787 struct eth_device *eth)
788{
789 debug("%s()\n", __func__);
790 if (!eth) {
791 debug("%s: missing parameter.\n", __func__);
792 return 0;
793 }
794
795 snprintf(eth->name, sizeof(eth->name), "%s%d",
796 MCS7830_BASE_NAME, mcs7830_iface_idx++);
797 eth->init = mcs7830_init;
798 eth->send = mcs7830_send;
799 eth->recv = mcs7830_recv;
800 eth->halt = mcs7830_halt;
801 eth->write_hwaddr = mcs7830_write_mac;
802 eth->priv = ss;
803
804 if (mcs7830_basic_reset(ss))
805 return 0;
806
807 if (mcs7830_read_mac(eth))
808 return 0;
809 debug("MAC %pM\n", eth->enetaddr);
810
811 return 1;
812}
813