1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24#include <common.h>
25#include <mpc5xxx.h>
26
27
28#define GPIO_PIN GPIO_PSC6_0
29
30#define NO_ERROR 0
31#define ERR_NO_NUMBER 1
32#define ERR_BAD_NUMBER 2
33
34static int is_high(void);
35static int check_device(void);
36static void io_out(int value);
37static void io_input(void);
38static void io_output(void);
39static void init_gpio(void);
40static void read_byte(unsigned char *data);
41static void write_byte(unsigned char command);
42
43void read_2501_memory(unsigned char *psernum, unsigned char *perr);
44void board_get_enetaddr(uchar *enetaddr);
45
46
47static int is_high()
48{
49 return (*((vu_long *) MPC5XXX_WU_GPIO_DATA_I) & GPIO_PIN);
50}
51
52static void io_out(int value)
53{
54 if (value)
55 *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) |= GPIO_PIN;
56 else
57 *((vu_long *) MPC5XXX_WU_GPIO_DATA_O) &= ~GPIO_PIN;
58}
59
60static void io_input()
61{
62 *((vu_long *) MPC5XXX_WU_GPIO_DIR) &= ~GPIO_PIN;
63 udelay(3);
64}
65
66static void io_output()
67{
68 *((vu_long *) MPC5XXX_WU_GPIO_DIR) |= GPIO_PIN;
69}
70
71static void init_gpio()
72{
73 *((vu_long *) MPC5XXX_WU_GPIO_ENABLE) |= GPIO_PIN;
74}
75
76void read_2501_memory(unsigned char *psernum, unsigned char *perr)
77{
78#define NBYTES 28
79 unsigned char crcval, i;
80 unsigned char buf[NBYTES];
81
82 *perr = 0;
83 crcval = 0;
84
85 for (i = 0; i < NBYTES; i++)
86 buf[i] = 0;
87
88 if (!check_device())
89 *perr = ERR_NO_NUMBER;
90 else {
91 *perr = NO_ERROR;
92 write_byte(0xCC);
93 write_byte(0xF0);
94 write_byte(0x00);
95 write_byte(0x00);
96 read_byte(&crcval);
97
98 for (i = 0; i < NBYTES; i++)
99 read_byte(&buf[i]);
100 }
101 if (strncmp((const char *) &buf[11], "MAREL IEEE 802.3", 16)) {
102 *perr = ERR_BAD_NUMBER;
103 psernum[0] = 0x00;
104 psernum[1] = 0xE0;
105 psernum[2] = 0xEE;
106 psernum[3] = 0xFF;
107 psernum[4] = 0xFF;
108 psernum[5] = 0xFF;
109 } else {
110 psernum[0] = 0x00;
111 psernum[1] = 0xE0;
112 psernum[2] = 0xEE;
113 psernum[3] = buf[7];
114 psernum[4] = buf[6];
115 psernum[5] = buf[5];
116 }
117}
118
119static int check_device()
120{
121 int found;
122
123 io_output();
124 io_out(0);
125 udelay(500);
126
127 io_input();
128 udelay(60);
129
130 found = (is_high() == 0) ? 1 : 0;
131 udelay(500);
132
133 return found;
134}
135
136static void write_byte(unsigned char command)
137{
138 char i;
139
140 for (i = 0; i < 8; i++) {
141
142
143 io_input();
144 udelay(3);
145
146 io_out(0);
147 io_output();
148 udelay(3);
149
150 if (command & 0x01) {
151
152 io_input();
153 udelay(60);
154 } else
155
156 udelay(60);
157
158 io_input();
159
160 command = command >> 1;
161 }
162}
163
164static void read_byte(unsigned char *data)
165{
166 unsigned char i, rdat = 0;
167
168 for (i = 0; i < 8; i++) {
169
170
171
172 io_out(0);
173 io_output();
174 udelay(0);
175
176
177 io_input();
178
179
180 udelay(10);
181
182
183 if (is_high())
184 rdat = (rdat >> 1) | 0x80;
185 else
186 rdat = rdat >> 1;
187
188 udelay(60);
189 }
190
191 *data = rdat;
192}
193
194void board_get_enetaddr(uchar *enetaddr)
195{
196 unsigned char sn[6], err = NO_ERROR;
197
198 init_gpio();
199
200 read_2501_memory(sn, &err);
201
202 if (err == NO_ERROR) {
203 sprintf((char *)enetaddr, "%02x:%02x:%02x:%02x:%02x:%02x",
204 sn[0], sn[1], sn[2], sn[3], sn[4], sn[5]);
205 printf("MAC address: %s\n", enetaddr);
206 setenv("ethaddr", (char *)enetaddr);
207 } else {
208 sprintf((char *)enetaddr, "00:01:02:03:04:05");
209 printf("Error reading MAC address.\n");
210 printf("Setting default to %s\n", enetaddr);
211 setenv("ethaddr", (char *)enetaddr);
212 }
213}
214