blob: c253055c0f3eb02d87c2741242c796cc79edd903 [file] [log] [blame]
wdenkabda5ca2003-05-31 18:35:21 +00001/*
2 * Copyright 1998-2001 by Donald Becker.
3 * This software may be used and distributed according to the terms of
4 * the GNU General Public License (GPL), incorporated herein by reference.
5 * Contact the author for use under other terms.
6 *
7 * This program must be compiled with "-O"!
8 * See the bottom of this file for the suggested compile-command.
9 *
10 * The author may be reached as becker@scyld.com, or C/O
11 * Scyld Computing Corporation
12 * 410 Severn Ave., Suite 210
13 * Annapolis MD 21403
14 *
15 * Common-sense licensing statement: Using any portion of this program in
16 * your own program means that you must give credit to the original author
17 * and release the resulting code under the GPL.
18 */
19
20#define _PPC_STRING_H_ /* avoid unnecessary str/mem functions */
wdenkabda5ca2003-05-31 18:35:21 +000021
22#include <common.h>
wdenk874ac262003-07-24 23:38:38 +000023#include <exports.h>
wdenkabda5ca2003-05-31 18:35:21 +000024#include <asm/io.h>
25
26
27/* Default EEPROM for i82559 */
28static unsigned short default_eeprom[64] = {
29 0x0100, 0x0302, 0x0504, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
30 0xffff, 0xffff, 0x40c0, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,
31 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
32 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
33 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
34 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
35 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
36 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
37};
38
39static unsigned short eeprom[256];
40
41static int eeprom_size = 64;
42static int eeprom_addr_size = 6;
43
44static int debug = 0;
45
46static inline unsigned short swap16(unsigned short x)
47{
48 return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
49}
50
51
Graeme Russ2fe2a972008-09-07 07:08:42 +100052void * memcpy(void * dest,const void *src,size_t count)
wdenkabda5ca2003-05-31 18:35:21 +000053{
Graeme Russ2fe2a972008-09-07 07:08:42 +100054 char *tmp = (char *) dest, *s = (char *) src;
55
56 while (count--)
57 *tmp++ = *s++;
58
59 return dest;
wdenkabda5ca2003-05-31 18:35:21 +000060}
61
Graeme Russ2fe2a972008-09-07 07:08:42 +100062
wdenkabda5ca2003-05-31 18:35:21 +000063/* The EEPROM commands include the alway-set leading bit. */
64#define EE_WRITE_CMD (5)
65#define EE_READ_CMD (6)
66#define EE_ERASE_CMD (7)
67
68/* Serial EEPROM section. */
69#define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
70#define EE_CS 0x02 /* EEPROM chip select. */
71#define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
72#define EE_DATA_READ 0x08 /* EEPROM chip data out. */
73#define EE_ENB (0x4800 | EE_CS)
74#define EE_WRITE_0 0x4802
75#define EE_WRITE_1 0x4806
76#define EE_OFFSET 14
77
78/* Delay between EEPROM clock transitions. */
79#define eeprom_delay(ee_addr) inw(ee_addr)
80
81/* Wait for the EEPROM to finish the previous operation. */
82static int eeprom_busy_poll(long ee_ioaddr)
83{
84 int i;
85 outw(EE_ENB, ee_ioaddr);
86 for (i = 0; i < 10000; i++) /* Typical 2000 ticks */
87 if (inw(ee_ioaddr) & EE_DATA_READ)
88 break;
89 return i;
90}
91
92/* This executes a generic EEPROM command, typically a write or write enable.
93 It returns the data output from the EEPROM, and thus may also be used for
94 reads. */
95static int do_eeprom_cmd(long ioaddr, int cmd, int cmd_len)
96{
97 unsigned retval = 0;
98 long ee_addr = ioaddr + EE_OFFSET;
99
100 if (debug > 1)
wdenk874ac262003-07-24 23:38:38 +0000101 printf(" EEPROM op 0x%x: ", cmd);
wdenkabda5ca2003-05-31 18:35:21 +0000102
103 outw(EE_ENB | EE_SHIFT_CLK, ee_addr);
104
105 /* Shift the command bits out. */
106 do {
107 short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
108 outw(dataval, ee_addr);
109 eeprom_delay(ee_addr);
110 if (debug > 2)
wdenk874ac262003-07-24 23:38:38 +0000111 printf("%X", inw(ee_addr) & 15);
wdenkabda5ca2003-05-31 18:35:21 +0000112 outw(dataval | EE_SHIFT_CLK, ee_addr);
113 eeprom_delay(ee_addr);
114 retval = (retval << 1) | ((inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
115 } while (--cmd_len >= 0);
116#if 0
117 outw(EE_ENB, ee_addr);
118#endif
119 /* Terminate the EEPROM access. */
120 outw(EE_ENB & ~EE_CS, ee_addr);
121 if (debug > 1)
wdenk874ac262003-07-24 23:38:38 +0000122 printf(" EEPROM result is 0x%5.5x.\n", retval);
wdenkabda5ca2003-05-31 18:35:21 +0000123 return retval;
124}
125
126static int read_eeprom(long ioaddr, int location, int addr_len)
127{
128 return do_eeprom_cmd(ioaddr, ((EE_READ_CMD << addr_len) | location)
129 << 16 , 3 + addr_len + 16) & 0xffff;
130}
131
132static void write_eeprom(long ioaddr, int index, int value, int addr_len)
133{
134 long ee_ioaddr = ioaddr + EE_OFFSET;
135 int i;
136
137 /* Poll for previous op finished. */
138 eeprom_busy_poll(ee_ioaddr); /* Typical 0 ticks */
139 /* Enable programming modes. */
140 do_eeprom_cmd(ioaddr, (0x4f << (addr_len-4)), 3 + addr_len);
141 /* Do the actual write. */
142 do_eeprom_cmd(ioaddr,
143 (((EE_WRITE_CMD<<addr_len) | index)<<16) | (value & 0xffff),
144 3 + addr_len + 16);
145 /* Poll for write finished. */
146 i = eeprom_busy_poll(ee_ioaddr); /* Typical 2000 ticks */
147 if (debug)
wdenk874ac262003-07-24 23:38:38 +0000148 printf(" Write finished after %d ticks.\n", i);
wdenkabda5ca2003-05-31 18:35:21 +0000149 /* Disable programming. This command is not instantaneous, so we check
150 for busy before the next op. */
151 do_eeprom_cmd(ioaddr, (0x40 << (addr_len-4)), 3 + addr_len);
152 eeprom_busy_poll(ee_ioaddr);
153}
154
155static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr)
156{
157 unsigned short checksum = 0;
158 int size_test;
159 int i;
160
Graeme Russ2fe2a972008-09-07 07:08:42 +1000161 printf("Resetting i82559 EEPROM @ 0x%08lx ... ", ioaddr);
wdenkabda5ca2003-05-31 18:35:21 +0000162
163 size_test = do_eeprom_cmd(ioaddr, (EE_READ_CMD << 8) << 16, 27);
164 eeprom_addr_size = (size_test & 0xffe0000) == 0xffe0000 ? 8 : 6;
165 eeprom_size = 1 << eeprom_addr_size;
166
167 memcpy(eeprom, default_eeprom, sizeof default_eeprom);
168
169 for (i = 0; i < 3; i++)
170 eeprom[i] = (hwaddr[i*2+1]<<8) + hwaddr[i*2];
171
172 /* Recalculate the checksum. */
173 for (i = 0; i < eeprom_size - 1; i++)
174 checksum += eeprom[i];
175 eeprom[i] = 0xBABA - checksum;
176
177 for (i = 0; i < eeprom_size; i++)
178 write_eeprom(ioaddr, i, eeprom[i], eeprom_addr_size);
179
180 for (i = 0; i < eeprom_size; i++)
181 if (read_eeprom(ioaddr, i, eeprom_addr_size) != eeprom[i]) {
wdenk874ac262003-07-24 23:38:38 +0000182 printf("failed\n");
wdenkabda5ca2003-05-31 18:35:21 +0000183 return 1;
184 }
185
wdenk874ac262003-07-24 23:38:38 +0000186 printf("done\n");
wdenkabda5ca2003-05-31 18:35:21 +0000187 return 0;
188}
189
190static unsigned int hatoi(char *p, char **errp)
191{
192 unsigned int res = 0;
wdenk57b2d802003-06-27 21:31:46 +0000193
wdenkabda5ca2003-05-31 18:35:21 +0000194 while (1) {
195 switch (*p) {
196 case 'a':
197 case 'b':
198 case 'c':
199 case 'd':
200 case 'e':
201 case 'f':
202 res |= (*p - 'a' + 10);
203 break;
204 case 'A':
205 case 'B':
206 case 'C':
207 case 'D':
208 case 'E':
209 case 'F':
210 res |= (*p - 'A' + 10);
211 break;
212 case '0':
213 case '1':
214 case '2':
215 case '3':
216 case '4':
217 case '5':
218 case '6':
219 case '7':
220 case '8':
221 case '9':
222 res |= (*p - '0');
wdenk57b2d802003-06-27 21:31:46 +0000223 break;
wdenkabda5ca2003-05-31 18:35:21 +0000224 default:
225 if (errp) {
226 *errp = p;
227 }
228 return res;
229 }
230 p++;
231 if (*p == 0) {
232 break;
233 }
234 res <<= 4;
235 }
wdenk57b2d802003-06-27 21:31:46 +0000236
wdenkabda5ca2003-05-31 18:35:21 +0000237 if (errp) {
238 *errp = NULL;
239 }
wdenk57b2d802003-06-27 21:31:46 +0000240
wdenkabda5ca2003-05-31 18:35:21 +0000241 return res;
242}
243
244static unsigned char *gethwaddr(char *in, unsigned char *out)
245{
246 char tmp[3];
247 int i;
248 char *err;
wdenk57b2d802003-06-27 21:31:46 +0000249
wdenkabda5ca2003-05-31 18:35:21 +0000250 for (i=0;i<6;i++) {
251 if (in[i*3+2] == 0 && i == 5) {
252 out[i] = hatoi(&in[i*3], &err);
253 if (err) {
254 return NULL;
255 }
256 } else if (in[i*3+2] == ':' && i < 5) {
257 tmp[0] = in[i*3];
258 tmp[1] = in[i*3+1];
259 tmp[2] = 0;
260 out[i] = hatoi(tmp, &err);
261 if (err) {
262 return NULL;
263 }
264 } else {
265 return NULL;
wdenk57b2d802003-06-27 21:31:46 +0000266 }
wdenkabda5ca2003-05-31 18:35:21 +0000267 }
wdenk57b2d802003-06-27 21:31:46 +0000268
wdenkabda5ca2003-05-31 18:35:21 +0000269 return out;
270}
271
272static u32
273read_config_dword(int bus, int dev, int func, int reg)
wdenk57b2d802003-06-27 21:31:46 +0000274{
wdenkabda5ca2003-05-31 18:35:21 +0000275 u32 res;
wdenk57b2d802003-06-27 21:31:46 +0000276
wdenkabda5ca2003-05-31 18:35:21 +0000277 outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
278 0xcf8);
279 res = inl(0xcfc);
wdenk57b2d802003-06-27 21:31:46 +0000280 outl(0, 0xcf8);
281 return res;
wdenkabda5ca2003-05-31 18:35:21 +0000282}
283
284static u16
285read_config_word(int bus, int dev, int func, int reg)
wdenk57b2d802003-06-27 21:31:46 +0000286{
wdenkabda5ca2003-05-31 18:35:21 +0000287 u32 res;
wdenk57b2d802003-06-27 21:31:46 +0000288
wdenkabda5ca2003-05-31 18:35:21 +0000289 outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
290 0xcf8);
291 res = inw(0xcfc + (reg & 2));
wdenk57b2d802003-06-27 21:31:46 +0000292 outl(0, 0xcf8);
293 return res;
wdenkabda5ca2003-05-31 18:35:21 +0000294}
295
296static void
297write_config_word(int bus, int dev, int func, int reg, u16 data)
wdenk57b2d802003-06-27 21:31:46 +0000298{
299
wdenkabda5ca2003-05-31 18:35:21 +0000300 outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
301 0xcf8);
302 outw(data, 0xcfc + (reg & 2));
wdenk57b2d802003-06-27 21:31:46 +0000303 outl(0, 0xcf8);
wdenkabda5ca2003-05-31 18:35:21 +0000304}
305
306
Wolfgang Denk6262d0212010-06-28 22:00:46 +0200307int main (int argc, char * const argv[])
wdenkabda5ca2003-05-31 18:35:21 +0000308{
309 unsigned char *eth_addr;
Graeme Russ2fe2a972008-09-07 07:08:42 +1000310 uchar buf[6];
wdenkabda5ca2003-05-31 18:35:21 +0000311 int instance;
wdenk57b2d802003-06-27 21:31:46 +0000312
wdenk874ac262003-07-24 23:38:38 +0000313 app_startup(argv);
wdenkabda5ca2003-05-31 18:35:21 +0000314 if (argc != 2) {
wdenk874ac262003-07-24 23:38:38 +0000315 printf ("call with base Ethernet address\n");
wdenkabda5ca2003-05-31 18:35:21 +0000316 return 1;
317 }
wdenk57b2d802003-06-27 21:31:46 +0000318
319
wdenkabda5ca2003-05-31 18:35:21 +0000320 eth_addr = gethwaddr(argv[1], buf);
321 if (NULL == eth_addr) {
wdenk874ac262003-07-24 23:38:38 +0000322 printf ("Can not parse ethernet address\n");
wdenkabda5ca2003-05-31 18:35:21 +0000323 return 1;
324 }
325 if (eth_addr[5] & 0x01) {
wdenk874ac262003-07-24 23:38:38 +0000326 printf("Base Ethernet address must be even\n");
wdenkabda5ca2003-05-31 18:35:21 +0000327 }
wdenk57b2d802003-06-27 21:31:46 +0000328
329
wdenkabda5ca2003-05-31 18:35:21 +0000330 for (instance = 0; instance < 2; instance ++) {
331 unsigned int io_addr;
332 unsigned char mac[6];
333 int bar1 = read_config_dword(0, 6+instance, 0, 0x14);
334 if (! (bar1 & 1)) {
wdenk874ac262003-07-24 23:38:38 +0000335 printf("ETH%d is disabled %x\n", instance, bar1);
wdenkabda5ca2003-05-31 18:35:21 +0000336 } else {
wdenk874ac262003-07-24 23:38:38 +0000337 printf("ETH%d IO=0x%04x\n", instance, bar1 & ~3);
wdenkabda5ca2003-05-31 18:35:21 +0000338 }
339 io_addr = (bar1 & (~3L));
wdenk57b2d802003-06-27 21:31:46 +0000340
341
342 write_config_word(0, 6+instance, 0, 4,
wdenkabda5ca2003-05-31 18:35:21 +0000343 read_config_word(0, 6+instance, 0, 4) | 1);
wdenk874ac262003-07-24 23:38:38 +0000344 printf("ETH%d CMD %04x\n", instance,
wdenkabda5ca2003-05-31 18:35:21 +0000345 read_config_word(0, 6+instance, 0, 4));
wdenk57b2d802003-06-27 21:31:46 +0000346
wdenkabda5ca2003-05-31 18:35:21 +0000347 memcpy(mac, eth_addr, 6);
348 mac[5] += instance;
wdenk57b2d802003-06-27 21:31:46 +0000349
wdenk874ac262003-07-24 23:38:38 +0000350 printf("got io=%04x, ha=%02x:%02x:%02x:%02x:%02x:%02x\n",
wdenk57b2d802003-06-27 21:31:46 +0000351 io_addr, mac[0], mac[1], mac[2],
352 mac[3], mac[4], mac[5]);
wdenkabda5ca2003-05-31 18:35:21 +0000353 reset_eeprom(io_addr, mac);
354 }
355 return 0;
356}