-
Notifications
You must be signed in to change notification settings - Fork 0
/
mcs7830.c
executable file
·2714 lines (2225 loc) · 74.2 KB
/
mcs7830.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* USB Networking Links
* Copyright (C) 2000-2003 by David Brownell <[email protected]>
* Copyright (C) 2002 Pavel Machek <[email protected]>
* Copyright (C) 2003 David Hollis <[email protected]>
* Copyright (c) 2002-2003 TiVo Inc.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/*
* This code is modified to support only MCS7830 device
*/
#define DEBUG 0 // error path messages, extra info
// #define VERBOSE // more; success messages
#define NEED_MII 1
#include <linux/version.h>
#if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,18)
#include <linux/config.h>
#endif
#ifdef CONFIG_USB_DEBUG
# define DEBUG
#endif
#include <linux/module.h>
#include <linux/kmod.h>
#include <linux/sched.h>
#include <linux/init.h>
#include <linux/netdevice.h>
#include <linux/etherdevice.h>
#include <linux/random.h>
#include <linux/ethtool.h>
#include <linux/workqueue.h>
#include <linux/mii.h>
#include <asm/uaccess.h>
#include <asm/unaligned.h>
#include <linux/usb.h>
#include <asm/io.h>
#include <asm/scatterlist.h>
#include <linux/mm.h>
#include <linux/dma-mapping.h>
#define DRIVER_VERSION "25-Nov-2005"
/*-------------------------------------------------------------------------*/
/*
* Nineteen USB 1.1 max size bulk transactions per frame (ms), max.
* Several dozen bytes of IPv4 data can fit in two such transactions.
* One maximum size Ethernet packet takes twenty four of them.
* For high speed, each frame comfortably fits almost 36 max size
* Ethernet packets (so queues should be bigger).
*/
#define RX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
#define TX_QLEN(dev) (((dev)->udev->speed == USB_SPEED_HIGH) ? 60 : 4)
// packets are always ethernet inside
// ... except they can be bigger (limit of 64K with NetChip framing)
#define MIN_PACKET sizeof(struct ethhdr)
#define MAX_PACKET 32768
// reawaken network queue this soon after stopping; else watchdog barks
#define TX_TIMEOUT_JIFFIES (5*HZ)
// throttle rx/tx briefly after some faults, so khubd might disconnect()
// us (it polls at HZ/4 usually) before we report too many false errors.
#define THROTTLE_JIFFIES (HZ/8)
// for vendor-specific control operations
#define CONTROL_TIMEOUT_MS 500
// between wakeups
#define UNLINK_TIMEOUT_MS 3
/*-------------------------------------------------------------------------*/
// MOSCHIP functions start
unsigned char WITHOUT_PHY = 0;
#define CONFIG_USB_MOSCHIP
#include "mcs7830.h"
#define MSECS_TO_JIFFIES(ms) (((ms)*HZ+999)/1000)
#define NETH_USB 4
#define WAIT_FOR_EVER (HZ * 0 ) /* timeout urb is wait for ever*/
#define MOS_WDR_TIMEOUT (HZ * 5 ) /* default urb timeout */
#define MCS_VENDOR_ID 0x9710 /* Vendor Id of Moschip MCS7830 */
#define MCS_PRODUCT_ID_7830 0x7830 /* Product Id */
#define MCS_PRODUCT_ID_7832 0x7832 /* Product Id */
#define MOS_WRITE 0x0D
#define MOS_READ 0x0E
// mii.h values coresponding to return values of mdio_read
#define BIT_13 0x2000
#define BIT_14 0x4000
#define BIT_15 0x8000
#define BIT_16 0x00010000
#define BIT_17 0x00020000
#define BIT_18 0x00040000
#define BIT_19 0x00080000
#define BIT_20 0x00100000
#define BIT_21 0x00200000
#define BIT_22 0x00400000
#define BIT_23 0x00800000
#define BIT_24 0x01000000
#define LINKOK 0x0004
/* setting and get register values */
#ifdef comment
static int GetVendorCommandPassive(struct usbnet *mcs, __u16 reg_index,__u16 length, __u8 * data);
static int SetVendorCommandPassive(struct usbnet *mcs, __u16 reg_index,__u16 length, __u8 * data);
static int mcs7830_find_endpoints(struct usbnet *mcs, struct usb_endpoint_descriptor *ep, int epnum);
static int MosUsbGetPermanentAddress(struct usbnet *mcs);
static int AutoNegChangemode(struct usbnet *mcs,int ptrUserPhyMode);
static int ANGInitializeDev(struct usbnet *mcs);
static int ReadPhyRegisterPassive(struct usbnet *mcs,__u8 phy_reg_index,__u16 *phy_data);
static int WritePhyRegisterPassive(struct usbnet *mcs,__u8 PhyRegIndex,__u16 *PhyRegValue);
#endif
static void mcs7830_disconnect (struct usb_device *udev, void *ptr);
// randomly generated ethernet address
static u8 node_id [ETH_ALEN];
// state we keep for each device we handle
struct usbnet {
// housekeeping
struct usb_device *udev;
struct driver_info *driver_info;
wait_queue_head_t *wait;
// i/o info: pipes etc
unsigned in, out;
struct usb_host_endpoint *status;
unsigned maxpacket;
struct timer_list delay;
// protocol/interface state
struct net_device *net;
struct net_device_stats stats;
int msg_enable;
unsigned long data [5];
struct mii_if_info mii;
// various kinds of pending driver work
struct sk_buff_head rxq;
struct sk_buff_head txq;
struct sk_buff_head done;
struct urb *interrupt;
struct tasklet_struct bh;
struct work_struct kevent;
unsigned long flags;
# define EVENT_TX_HALT 0
# define EVENT_RX_HALT 1
# define EVENT_RX_MEMORY 2
# define EVENT_STS_SPLIT 3
# define EVENT_LINK_RESET 4
// usage for the vendor command MOSCHIP change !!!!
__u8 SpeedDuplex; // New reg value for speed/duplex
__u8 ForceDuplex; // 1 for half-duplex, 2 for full duplex
__u8 ForceSpeed; // 10 for 10Mbps, 100 for 100 Mbps
__u8 ByteValue [10];
__u16 WordValue;
__u8 checkval2[10];//added for checking removing later
__u32 CurrentPacketFilter;
__u8 HifReg15;
__u16 PhyControlReg;
__u16 TempPhyControlReg;
__u16 PhyAutoNegLinkPartnerReg;
__u16 PhyChipStatusReg;
__u16 PhyAutoNegAdvReg;
__u16 DeviceReleaseNumber;
/* flag for the identification of the type of PHY. */ //mahipal
__u32 PhyIdentity;
__u8 AiPermanentNodeAddress[ETHERNET_ADDRESS_LENGTH];
__u8 bAutoNegStarted;
__u32 ptrPauseTime;
//Rev.C change
__u8 ptrPauseThreshold;
__u32 ptrFpgaVersion;
__u32 ptrRxErrorCount;
};
// device-specific info used by the driver
struct driver_info {
char *description;
int flags;
/* framing is CDC Ethernet, not writing ZLPs (hw issues), or optionally: */
#define FLAG_NO_SETINT 0x0010 /* device can't set_interface() */
#define FLAG_ETHER 0x0020 /* maybe use "eth%d" names */
/* init device ... can sleep, or cause probe() failure */
int (*bind)(struct usbnet *, struct usb_interface *);
/* cleanup device ... can sleep, but can't fail */
void (*unbind)(struct usbnet *, struct usb_interface *);
/* reset device ... can sleep */
int (*reset)(struct usbnet *);
/* see if peer is connected ... can sleep */
int (*check_connect)(struct usbnet *);
/* for status polling */
void (*status)(struct usbnet *, struct urb *);
/* link reset handling, called from defer_kevent */
int (*link_reset)(struct usbnet *);
/* fixup rx packet (strip framing) */
int (*rx_fixup)(struct usbnet *dev, struct sk_buff *skb);
/* fixup tx packet (add framing) */
struct sk_buff *(*tx_fixup)(struct usbnet *dev,
struct sk_buff *skb, int flags);
// FIXME -- also an interrupt mechanism
// useful for at least PL2301/2302 and GL620USB-A
// and CDC use them to report 'is it connected' changes
/* for new devices, use the descriptor-reading code instead */
int in; /* rx endpoint */
int out; /* tx endpoint */
unsigned long data; /* Misc driver specific data */
};
// we record the state for each of our queued skbs
enum skb_state {
illegal = 0,
tx_start, tx_done,
rx_start, rx_done, rx_cleanup
};
struct skb_data { // skb->cb is one of these
struct urb *urb;
struct usbnet *dev;
enum skb_state state;
size_t length;
};
static const char driver_name [] = "MOSCHIP usb-ethernet driver";
/* use ethtool to change the level for any given device */
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,10)
static int msg_level = -1;
module_param (msg_level, int, 0);
MODULE_PARM_DESC (msg_level, "Override default message level");
#endif
/*
* Debug related defines
*/
/* 1: Enables the debugging -- 0: Disable the debugging */
#define MOS_DEBUG 0
#if MOS_DEBUG
//static int debug = 0;
#define DPRINTK(fmt, args...) printk( "%s: " fmt, __FUNCTION__ , ## args)
#else
//static int debug = 0;
#define DPRINTK(fmt, args...)
#endif
#ifdef DEBUG
#define devdbg(usbnet, fmt, arg...) \
printk(KERN_DEBUG "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
#else
#define devdbg(usbnet, fmt, arg...) do {} while(0)
#endif
#define deverr(usbnet, fmt, arg...) \
printk(KERN_ERR "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
#define devwarn(usbnet, fmt, arg...) \
printk(KERN_WARNING "%s: " fmt "\n" , (usbnet)->net->name , ## arg)
#define devinfo(usbnet, fmt, arg...) \
printk(KERN_INFO "%s: " fmt "\n" , (usbnet)->net->name , ## arg); \
/*-------------------------------------------------------------------------*/
static void usbnet_get_drvinfo (struct net_device *, struct ethtool_drvinfo *);
static u32 usbnet_get_link (struct net_device *);
static u32 usbnet_get_msglevel (struct net_device *);
static void usbnet_set_msglevel (struct net_device *, u32);
static void defer_kevent (struct usbnet *, int);
/* mostly for PDA style devices, which are always connected if present */
static int always_connected (struct usbnet *dev)
{
return 0;
}
/* handles CDC Ethernet and many other network "bulk data" interfaces */
static int
get_endpoints (struct usbnet *dev, struct usb_interface *intf)
{
int tmp;
struct usb_host_interface *alt = NULL;
struct usb_host_endpoint *in = NULL, *out = NULL;
struct usb_host_endpoint *status = NULL;
for (tmp = 0; tmp < intf->num_altsetting; tmp++) {
unsigned ep;
in = out = status = NULL;
alt = intf->altsetting + tmp;
/* take the first altsetting with in-bulk + out-bulk;
* remember any status endpoint, just in case;
* ignore other endpoints and altsetttings.
*/
for (ep = 0; ep < alt->desc.bNumEndpoints; ep++) {
struct usb_host_endpoint *e;
int intr = 0;
e = alt->endpoint + ep;
switch (e->desc.bmAttributes) {
case USB_ENDPOINT_XFER_INT:
if (!(e->desc.bEndpointAddress & USB_DIR_IN))
continue;
intr = 1;
/* FALLTHROUGH */
case USB_ENDPOINT_XFER_BULK:
break;
default:
continue;
}
if (e->desc.bEndpointAddress & USB_DIR_IN) {
if (!intr && !in)
in = e;
else if (intr && !status)
status = e;
} else {
if (!out)
out = e;
}
}
if (in && out)
break;
}
if (!alt || !in || !out)
return -EINVAL;
if (alt->desc.bAlternateSetting != 0
|| !(dev->driver_info->flags & FLAG_NO_SETINT)) {
tmp = usb_set_interface (dev->udev, alt->desc.bInterfaceNumber,
alt->desc.bAlternateSetting);
if (tmp < 0)
return tmp;
}
dev->in = usb_rcvbulkpipe (dev->udev,
in->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
dev->out = usb_sndbulkpipe (dev->udev,
out->desc.bEndpointAddress & USB_ENDPOINT_NUMBER_MASK);
dev->status = status;
return 0;
}
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
static void intr_complete (struct urb *urb, struct pt_regs *regs);
#else
static void intr_complete (struct urb *urb );
#endif
static int init_status (struct usbnet *dev, struct usb_interface *intf)
{
char *buf = NULL;
unsigned pipe = 0;
unsigned maxp;
unsigned period;
if (!dev->driver_info->status)
return 0;
pipe = usb_rcvintpipe (dev->udev,
dev->status->desc.bEndpointAddress
& USB_ENDPOINT_NUMBER_MASK);
maxp = usb_maxpacket (dev->udev, pipe, 0);
/* avoid 1 msec chatter: min 8 msec poll rate */
period = max ((int) dev->status->desc.bInterval,
(dev->udev->speed == USB_SPEED_HIGH) ? 7 : 3);
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
buf = kmalloc (maxp, GFP_KERNEL);
#else
buf = kmalloc (maxp, SLAB_KERNEL);
#endif
if (buf) {
#if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,20)
dev->interrupt = usb_alloc_urb (0, GFP_KERNEL);
#else
dev->interrupt = usb_alloc_urb (0, SLAB_KERNEL);
#endif
if (!dev->interrupt) {
kfree (buf);
return -ENOMEM;
} else {
usb_fill_int_urb(dev->interrupt, dev->udev, pipe,
buf, maxp, intr_complete, dev, period);
dev_dbg(&intf->dev,
"status ep%din, %d bytes period %d\n",
usb_pipeendpoint(pipe), maxp, period);
}
}
return 0;
}
static void skb_return (struct usbnet *dev, struct sk_buff *skb)
{
int status;
skb->dev = dev->net;
skb->protocol = eth_type_trans (skb, dev->net);
dev->stats.rx_packets++;
dev->stats.rx_bytes += skb->len;
if (netif_msg_rx_status (dev))
devdbg (dev, "< rx, len %zu, type 0x%x",
skb->len + sizeof (struct ethhdr), skb->protocol);
memset (skb->cb, 0, sizeof (struct skb_data));
status = netif_rx (skb);
if (status != NET_RX_SUCCESS && netif_msg_rx_err (dev))
devdbg (dev, "netif_rx status %d", status);
}
#ifdef CONFIG_USB_MOSCHIP
static const struct driver_info moschip_info = {
.description = "MOSCHIP 7830 usb-NET adapter",
.check_connect = always_connected,
.in = 1, .out = 2,
//.epsize = 64,
};
#endif /* CONFIG_USB_NOSCHIP */
static int GetVendorCommandPassive(struct usbnet *mcs, __u16 reg_index,__u16 length, __u8 * data)
{
struct usb_device *dev = mcs->udev;
int ret=0;
//DPRINTK(" in GetVendorCommandPassive reg_index is %x content of data %x status %x\n",reg_index,*data,ret);
ret = usb_control_msg(dev, usb_rcvctrlpipe(dev, 0), MCS_RD_BREQ,
MCS_RD_BMREQ, 0x0000, reg_index, data, length,
MSECS_TO_JIFFIES(MCS_CTRL_TIMEOUT));
//DPRINTK(" in GetVendorCommandPassive reg_index is %x content of data %x status %x\n",reg_index,*data,ret);
return ret;
}
static int SetVendorCommandPassive(struct usbnet *mcs, __u16 reg_index,__u16 length, __u8 * data)
{
struct usb_device *dev = mcs->udev;
int ret=0;
//DPRINTK(" in SetVendorCommandPassive reg_index is %x content of data %x status %x\n",reg_index,(__u16 )*data,ret);
ret = usb_control_msg(dev, usb_sndctrlpipe(dev, 0), MCS_WR_BREQ,
MCS_WR_BMREQ, 0x0000, reg_index, data,length,
MSECS_TO_JIFFIES(MCS_CTRL_TIMEOUT));
//DPRINTK(" in SetVendorCommandPassive reg_index is %x content of data %x status %x\n",reg_index,*data,ret);
return ret;
}
static int MosUsbGetPermanentAddress(struct usbnet *mcs)
{
return GetVendorCommandPassive(mcs,HIF_REG_16,ETHERNET_ADDRESS_LENGTH,mcs->AiPermanentNodeAddress);
}
static int ReadPhyRegisterPassive(struct usbnet *mcs,__u8 PhyRegIndex,__u16 *PhyRegValue)
{
int status=0,Count;
//__u8 checkval=0x0;
__u16 checkval=0x0;
mcs->WordValue = 0x0;
//__u8 checkval2[10];
status=SetVendorCommandPassive(mcs,HIF_REG_11,2,&checkval);//(__u8 *)(&mcs->WordValue));
if(status<0)
{
//DPRINTK("ERROR: couldn't clean register HIF_REG_11&12 \n");
return status;
}
else
{
//DPRINTK(" cleaned register HIF_REG_11&12 \n");
checkval=0;
}
status=0;
mcs->checkval2[0]= READ_OPCODE | FARADAY_PHY_ADDRESS;
// mcs->checkval2[0]= READ_OPCODE |EXTERNEL_PHY_ADDRESS;
//DPRINTK("in Local SetVendorCommandPassive reg is %x \tval is %x\n",HIF_REG_13,mcs->checkval2[0]);// *(mcs->ByteValue));
status=SetVendorCommandPassive(mcs,HIF_REG_13,1,&mcs->checkval2[0]);//mcs->ByteValue);
if(status<0)
{
//DPRINTK("ERROR :Couldn't set HIF13 for Read Mode \n");
return status;
}
else
{
//DPRINTK("Set HIF_REG_13 with READ_OPCODE | FARADAY_PHY_ADDRESS Done!!\n");
}
// 2. Build value for HIF_REG14
// Set Pend Flag, Reset Ready Flag & address of Reg.with in the PHY
// Will procesed By MAC - MIIM Hardware.
// Msb 1000 0000 Lsb - b7 Pend, b6 Ready,
// b5 Not used,b4b3b2b1b0 Address of Reg with in the PHY
status=0;
mcs->checkval2[0]=0x0;
mcs->checkval2[0] = HIF_REG_14_PEND_FLAG_BIT | (PhyRegIndex & 0x1F);
status=SetVendorCommandPassive(mcs,HIF_REG_14,1,&mcs->checkval2[0]);//mcs->ByteValue);
if(status<0)
{
//DPRINTK("ERROR : Setting pend flag & PHY reg.index in HIF_REG_14 to read phy \n");
return status;
}
else
{
//DPRINTK(" Setting pend flag & PHY reg.index in HIF_REG_14 to read phy Done !!!\n");
}
mcs->checkval2[0]=0x0;
// 3. Read (completion) Status for last command
Count = 10;
do
{
status=0;
mcs->checkval2[0]=0x0;
status = GetVendorCommandPassive(mcs,HIF_REG_14,1,&mcs->checkval2[0]);//(__u8 *)mcs->ByteValue);
if(status<0)
{
//DPRINTK("ERROR : Reading the ReadyFlag bit in the HIF_REG_14 \n");
break;//command error
}
else
{
//DPRINTK("Reading the ReadyFlag bit in the HIF_REG_14 DONE!!! \n");
}
//DPRINTK(" mcs->checkval2[0] value is %x\n",mcs->checkval2[0]);
if (mcs->checkval2[0] & HIF_REG_14_READY_FLAG_BIT)
{
//DPRINTK("Command completed\n");
break; // command complete
}
else
{
Count -- ;
//DPRINTK("READ PHY register command not Completed. Checking the READY flag again.\n");
if (Count == 0 )
{
//DPRINTK("Checking the READY flag for 10 times completed not yet set.\n");
status = -1;
//DPRINTK("Check1\n");
break;
}
}
}while(1);
if(status<0)
{
//DPRINTK("Check2 \n");
return status;
}
status=0;
//status = GetVendorCommandPassive(mcs,HIF_REG_11,2,(__u8 *)PhyRegValue);
status = GetVendorCommandPassive(mcs,HIF_REG_11,2,PhyRegValue);
if(status<0)
{
//DPRINTK("ERROR :Couldn't read HIF11-12 for phy data \n");
return status;
}
return status;
}
static int WritePhyRegisterPassive(struct usbnet *mcs,__u8 PhyRegIndex,__u16 *PhyRegValue)
{
int status=0,Count;
mcs->WordValue = 0x0;
//status=SetVendorCommandPassive(mcs,HIF_REG_11,2,(__u8 *)PhyRegValue);
status=SetVendorCommandPassive(mcs,HIF_REG_11,2,PhyRegValue);
if(status<0)
{
//DPRINTK("ERROR: couldn't write data in register HIF_REG_11&12 \n");
return status;
}
else
{
//DPRINTK(" Wrote data to register HIF_REG_11&12 \n");
}
status=0;
mcs->checkval2[0]= WRITE_OPCODE | FARADAY_PHY_ADDRESS;
//DPRINTK("in Local SetVendorCommandPassive reg is %x \tval is %x\n",HIF_REG_13,mcs->checkval2[0]);// *(mcs->ByteValue));
status=SetVendorCommandPassive(mcs,HIF_REG_13,1,&mcs->checkval2[0]);//mcs->ByteValue);
if(status<0)
{
//DPRINTK("ERROR :Couldn't set HIF13 for wRITE Mode \n");
return status;
}
else
{
//DPRINTK("Set HIF_REG_13 with WRITE_OPCODE | FARADAY_PHY_ADDRESS Done!!\n");
}
// 2. Build value for HIF_REG14
// Set Pend Flag, Reset Ready Flag & address of Reg.with in the PHY
// Will procesed By MAC - MIIM Hardware.
// Msb 1000 0000 Lsb - b7 Pend, b6 Ready,
// b5 Not used,b4b3b2b1b0 Address of Reg with in the PHY
status=0;
mcs->checkval2[0]=0x0;
mcs->checkval2[0] = HIF_REG_14_PEND_FLAG_BIT | (PhyRegIndex & 0x1F);
status=SetVendorCommandPassive(mcs,HIF_REG_14,1,&mcs->checkval2[0]);//mcs->ByteValue);
if(status<0)
{
//DPRINTK("ERROR : Setting pend flag & PHY reg.index in HIF_REG_14 to write phy \n");
return status;
}
else
{
//DPRINTK(" Setting pend flag & PHY reg.index in HIF_REG_14 to write phy Done !!!\n");
}
mcs->checkval2[0]=0x0;
// 3. Read (completion) Status for last command
Count = 10;
do
{
status=0;
mcs->checkval2[0]=0x0;
status = GetVendorCommandPassive(mcs,HIF_REG_14,1,&mcs->checkval2[0]);//(__u8 *)mcs->ByteValue);
if(status<0)
{
//DPRINTK("ERROR : Reading the ReadyFlag bit in the HIF_REG_14 \n");
break;//command error
}
else
{
//DPRINTK("Reading the ReadyFlag bit in the HIF_REG_14 DONE!!! \n");
}
//DPRINTK(" mcs->checkval2[0] value is %x\n",mcs->checkval2[0]);
if (mcs->checkval2[0] & HIF_REG_14_READY_FLAG_BIT)
{
//DPRINTK("Command completed\n");
break; // command complete
}
else
{
Count -- ;
//DPRINTK("READ PHY register command not Completed. Checking the READY flag again.\n");
if (Count == 0 )
{
//DPRINTK("Checking the READY flag for 10 times completed not yet set.\n");
status = -1;
//DPRINTK("Check1\n");
break;
}
}
}while(1);
return status;
}
static int PrintAutoNegAdvRegister(struct usbnet *mcs)
{
return 1;
}
static int AutoNegChangemode(struct usbnet *mcs,int ptrUserPhyMode)
{
int status=0x0;
__u16 AutoNegAdvtReg;
__u8 ReadHifVal;
if( WITHOUT_PHY==0)
{
if(ptrUserPhyMode ==0)
{
// Writing 0x0021 in PhyAutoNeg Advertisement reg 4
AutoNegAdvtReg = PHY_AUTONEGADVT_FdxPause | \
PHY_AUTONEGADVT_Fdx100TX |\
PHY_AUTONEGADVT_100TX | PHY_AUTONEGADVT_ieee802_3|\
PHY_AUTONEGADVT_10TFdx|PHY_AUTONEGADVT_10T;
}
else if(ptrUserPhyMode ==1)
{
// Writing 0x0021 in PhyAutoNeg Advertisement reg 4
AutoNegAdvtReg = PHY_AUTONEGADVT_10T | PHY_AUTONEGADVT_ieee802_3;
}
else if(ptrUserPhyMode ==2)
{
// Writing 0x0461 in PhyAutoNeg Advertisement reg 4
AutoNegAdvtReg = PHY_AUTONEGADVT_FdxPause | PHY_AUTONEGADVT_10TFdx |\
PHY_AUTONEGADVT_10T | PHY_AUTONEGADVT_ieee802_3;
}
else if(ptrUserPhyMode ==3)
{
// Writing 0x0081 in PhyAutoNeg Advertisement reg 4
AutoNegAdvtReg = PHY_AUTONEGADVT_100TX | PHY_AUTONEGADVT_ieee802_3;
}
else if(ptrUserPhyMode ==4)
{
// Writing 0x0581 in PhyAutoNeg Advertisement reg 4
AutoNegAdvtReg = PHY_AUTONEGADVT_FdxPause | PHY_AUTONEGADVT_Fdx100TX |\
PHY_AUTONEGADVT_100TX | PHY_AUTONEGADVT_ieee802_3;
}
//DPRINTK("in AutoNegChangemode AutoNegAdvtReg is %04x \n", AutoNegAdvtReg);
status=0x0;
status=WritePhyRegisterPassive(mcs,PHY_AUTONEGADVT_REG_INDEX,&AutoNegAdvtReg);
//DPRINTK("Status of WritePhyRegisterPassive ANG is %x\n",status);
if(status >=0)
{
//DPRINTK("Writing value for AutoNegotiation in AutoNegAdvt Register() success\n");
//break;
}
else
{
//DPRINTK("Writing value for AutoNegotiation in AutoNegAdvt Register() FAILURE\n");
mcs7830_disconnect(mcs->udev, (void *) mcs);
return -1;//NULL;
}
//////////////////Change For Link Status Problem :Begin ///////////
//First Disable All
mcs->PhyControlReg = 0x0000;
status=0x0;
status = WritePhyRegisterPassive(mcs,PHY_CONTROL_REG_INDEX,&mcs->PhyControlReg);
if(status >=0)
{
//DPRINTK("Writing value WritePhyControlRegister() success\n");
//break;
}
else
{
//DPRINTK("Writing value for WritePhyControlRegister() FAILURE\n");
mcs7830_disconnect(mcs->udev, (void *) mcs);
return -1;//NULL;
}
mcs->PhyControlReg = PHY_CONTROL_AUTONEG_ENABLE;
status=0x0;
status = WritePhyRegisterPassive(mcs,PHY_CONTROL_REG_INDEX,&mcs->PhyControlReg);
if(status >=0)
{
//DPRINTK("Writing value WritePhyControlRegister() ANG enable success\n");
//break;
}
else
{
//DPRINTK("Writing value for WritePhyControlRegister() SNG enable FAILURE\n");
mcs7830_disconnect(mcs->udev, (void *) mcs);
return -1;//NULL;
}
//Restart Auto Neg (Keep the Enable Auto Neg Bit Set)
mcs->PhyControlReg = PHY_CONTROL_AUTONEG_ENABLE |PHY_CONTROL_RESTART_AUTONEG;
status=0x0;
status = WritePhyRegisterPassive(mcs,PHY_CONTROL_REG_INDEX,&mcs->PhyControlReg);
if(status >=0)
{
//DPRINTK("Writing value WritePhyControlRegister() ANG enable&restart success\n");
//break;
}
else
{
//DPRINTK("Writing value for WritePhyControlRegister() SNG enable&restart FAILURE\n");
mcs7830_disconnect(mcs->udev, (void *) mcs);
return -1;//NULL;
}
mcs->bAutoNegStarted = TRUE;
//////////////////Change For Link Status Problem :End ///////////
status=0x0;
if(mcs->PhyIdentity == INTEL_PHY)
{
status = ReadPhyRegisterPassive(mcs,PHY_CHIPSTATUS_REG_INDEX,&mcs->TempPhyControlReg);
}
else if(mcs->PhyIdentity == FARADAY_PHY)
{
status = ReadPhyRegisterPassive(mcs,PHY_CONTROL_REG_INDEX,&mcs->TempPhyControlReg);
}
if(status >=0)
{
//DPRINTK("PhyControl for AutoNegotiation Value: x%04x\n", mcs->TempPhyControlReg);
//break;
}
else
{
//DPRINTK("Reading value from ReadPhyControlRegister() FAILURE FAILURE\n");
mcs7830_disconnect(mcs->udev, (void *) mcs);
return -1;//NULL;
}
// read linkspeed & duplex from PHY & set to HIF
AutoNegAdvtReg=0x0;
status=0x0;
status = ReadPhyRegisterPassive(mcs,PHY_AUTONEGADVT_REG_INDEX,&AutoNegAdvtReg);
if(status >=0)
{
//DPRINTK("in AutoNegChangemode AutoNegAdvtReg is %04x \n", AutoNegAdvtReg);
//break;
}
else
{
//DPRINTK("in AutoNegChangemode AutoNegAdvtReg FAILED\n");
mcs7830_disconnect(mcs->udev, (void *) mcs);
return -1;//NULL;
}
if(AutoNegAdvtReg & PHY_AUTONEGADVT_100TX)
mcs->HifReg15 |= HIF_REG_15_SPEED100;
else
mcs->HifReg15 &= ~HIF_REG_15_SPEED100;
if((AutoNegAdvtReg & PHY_AUTONEGADVT_10TFdx)||(AutoNegAdvtReg & PHY_AUTONEGADVT_Fdx100TX))
mcs->HifReg15 |= HIF_REG_15_FULLDUPLEX_ENABLE;
else
mcs->HifReg15 &= ~HIF_REG_15_FULLDUPLEX_ENABLE;
status=0x0;
status=SetVendorCommandPassive( mcs,HIF_REG_15,1,&mcs->HifReg15);
//DPRINTK("in AutoNegChangemode Writing mcs->HifReg15 is %04x \n",mcs->HifReg15 );
if(status >=0)
{
//DPRINTK("in AutoNegChangemode writing mcs->HifReg15 SUCCESS\n" );
//break;
}
else
{
//DPRINTK("in AutoNegChangemode writing mcs->HifReg15 FAILED\n");
mcs7830_disconnect(mcs->udev, (void *) mcs);
return -1;//NULL;
}
status=0;
status=GetVendorCommandPassive( mcs,HIF_REG_15,1,&ReadHifVal);
//DPRINTK("in AutoNegChangemode Read mcs->HifReg15 is %04x \n",ReadHifVal );
} // WITHOUT_PHY end
return status;
//DPRINTK("Phy Status Register Value: x%04x\n", mcs->PhyStatusReg);
}
static int ANGInitializeDev(struct usbnet *mcs)
{
int Ccnt=0;
__u16 Read_HIFReg_22;
__u8 SingleByte;
int status;
__u16 PhyIDReg1 = 0x0;
__u16 PhyIDReg2 = 0x0;
mcs->DeviceReleaseNumber = DEVICE_REV_B; ///by default revision B
// Check for other revision devices by reading one of the additional registers that are
// existing.
// Here, we are reading HIFReg_22 for the above mentioned purpose
do{
status = GetVendorCommandPassive(mcs,0x15,2,&Read_HIFReg_22);
if(status >0)//STATUS_SUCCESS)
{
DPRINTK("Device is of revision C\n");
mcs->DeviceReleaseNumber = DEVICE_REV_C;
break;
}
Ccnt++;
//udelay(50);
}while(Ccnt>2);
Ccnt=5;
do{
status=0x0;
status = MosUsbGetPermanentAddress(mcs);
Ccnt--;
}while(Ccnt>0);
if(status >=0)
{
DPRINTK("vendor command MosUsbGetPermanentAddress success\n");
//DPRINTK("address is %x\n",mcs->AiPermanentNodeAddress);
}
// External_Phy
/* code for External PHY implementation */
// Reading PhyID Reg1
status = ReadPhyRegisterPassive(mcs,PHY_IDENTIFICATION1_REG_INDEX,&PhyIDReg1);
//DPRINTK("PhyIDReg1 is 0x%x,status=%d \n",PhyIDReg1,status);
if(status >=0)
DPRINTK("Reading PHY_IDENTIFICATION1_REG_INDEX success...\n");
else
DPRINTK("Reading PHY_IDENTIFICATION1_REG_INDEX failure...\n");
// Reading PhyID Reg2
status = ReadPhyRegisterPassive(mcs,PHY_IDENTIFICATION2_REG_INDEX,&PhyIDReg2);
//DPRINTK("PhyIDReg2 is 0x%x, status= %d \n",PhyIDReg2,status);