Merge branch 'master' of git://git.denx.de/u-boot-fsl-qoriq
[oweals/u-boot.git] / drivers / net / ax88796.c
1 /*
2  * (c) 2007 Nobuhiro Iwamatsu <iwamatsu@nigauri.org>
3  *
4  * SPDX-License-Identifier:     GPL-2.0+
5  */
6 #include <common.h>
7 #include "ax88796.h"
8
9 /*
10  * Set 1 bit data
11  */
12 static void ax88796_bitset(u32 bit)
13 {
14         /* DATA1 */
15         if( bit )
16                 EEDI_HIGH;
17         else
18                 EEDI_LOW;
19
20         EECLK_LOW;
21         udelay(1000);
22         EECLK_HIGH;
23         udelay(1000);
24         EEDI_LOW;
25 }
26
27 /*
28  * Get 1 bit data
29  */
30 static u8 ax88796_bitget(void)
31 {
32         u8 bit;
33
34         EECLK_LOW;
35         udelay(1000);
36         /* DATA */
37         bit = EEDO;
38         EECLK_HIGH;
39         udelay(1000);
40
41         return bit;
42 }
43
44 /*
45  * Send COMMAND to EEPROM
46  */
47 static void ax88796_eep_cmd(u8 cmd)
48 {
49         ax88796_bitset(BIT_DUMMY);
50         switch(cmd){
51                 case MAC_EEP_READ:
52                         ax88796_bitset(1);
53                         ax88796_bitset(1);
54                         ax88796_bitset(0);
55                         break;
56
57                 case MAC_EEP_WRITE:
58                         ax88796_bitset(1);
59                         ax88796_bitset(0);
60                         ax88796_bitset(1);
61                         break;
62
63                 case MAC_EEP_ERACE:
64                         ax88796_bitset(1);
65                         ax88796_bitset(1);
66                         ax88796_bitset(1);
67                         break;
68
69                 case MAC_EEP_EWEN:
70                         ax88796_bitset(1);
71                         ax88796_bitset(0);
72                         ax88796_bitset(0);
73                         break;
74
75                 case MAC_EEP_EWDS:
76                         ax88796_bitset(1);
77                         ax88796_bitset(0);
78                         ax88796_bitset(0);
79                         break;
80                 default:
81                         break;
82         }
83 }
84
85 static void ax88796_eep_setaddr(u16 addr)
86 {
87         int i ;
88
89         for( i = 7 ; i >= 0 ; i-- )
90                 ax88796_bitset(addr & (1 << i));
91 }
92
93 /*
94  * Get data from EEPROM
95  */
96 static u16 ax88796_eep_getdata(void)
97 {
98         ushort data = 0;
99         int i;
100
101         ax88796_bitget();       /* DUMMY */
102         for( i = 0 ; i < 16 ; i++ ){
103                 data <<= 1;
104                 data |= ax88796_bitget();
105         }
106         return data;
107 }
108
109 static void ax88796_mac_read(u8 *buff)
110 {
111         int i ;
112         u16 data;
113         u16 addr = 0;
114
115         for( i = 0 ; i < 3; i++ )
116         {
117                 EECS_HIGH;
118                 EEDI_LOW;
119                 udelay(1000);
120                 /* READ COMMAND */
121                 ax88796_eep_cmd(MAC_EEP_READ);
122                 /* ADDRESS */
123                 ax88796_eep_setaddr(addr++);
124                 /* GET DATA */
125                 data = ax88796_eep_getdata();
126                 *buff++ = (uchar)(data & 0xff);
127                 *buff++ = (uchar)((data >> 8) & 0xff);
128                 EECLK_LOW;
129                 EEDI_LOW;
130                 EECS_LOW;
131         }
132 }
133
134 int get_prom(u8* mac_addr, u8* base_addr)
135 {
136         u8 prom[32];
137         int i;
138
139         ax88796_mac_read(prom);
140         for (i = 0; i < 6; i++){
141                 mac_addr[i] = prom[i];
142         }
143         return 1;
144 }