3 * Copyright 1998-2001 by Donald Becker.
4 * This software may be used and distributed according to the terms of
5 * the GNU General Public License (GPL), incorporated herein by reference.
6 * Contact the author for use under other terms.
8 * This program must be compiled with "-O"!
9 * See the bottom of this file for the suggested compile-command.
11 * The author may be reached as becker@scyld.com, or C/O
12 * Scyld Computing Corporation
13 * 410 Severn Ave., Suite 210
16 * Common-sense licensing statement: Using any portion of this program in
17 * your own program means that you must give credit to the original author
18 * and release the resulting code under the GPL.
21 #define _PPC_STRING_H_ /* avoid unnecessary str/mem functions */
22 #define _LINUX_STRING_H_ /* avoid unnecessary str/mem functions */
29 /* Default EEPROM for i82559 */
30 static unsigned short default_eeprom[64] = {
31 0x0100, 0x0302, 0x0504, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
32 0xffff, 0xffff, 0x40c0, 0x0000, 0x0000, 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 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
38 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
41 static unsigned short eeprom[256];
43 static int eeprom_size = 64;
44 static int eeprom_addr_size = 6;
48 static inline unsigned short swap16(unsigned short x)
50 return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
54 void * memcpy(void * dest,const void *src,size_t count)
56 char *tmp = (char *) dest, *s = (char *) src;
65 /* The EEPROM commands include the alway-set leading bit. */
66 #define EE_WRITE_CMD (5)
67 #define EE_READ_CMD (6)
68 #define EE_ERASE_CMD (7)
70 /* Serial EEPROM section. */
71 #define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
72 #define EE_CS 0x02 /* EEPROM chip select. */
73 #define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
74 #define EE_DATA_READ 0x08 /* EEPROM chip data out. */
75 #define EE_ENB (0x4800 | EE_CS)
76 #define EE_WRITE_0 0x4802
77 #define EE_WRITE_1 0x4806
80 /* Delay between EEPROM clock transitions. */
81 #define eeprom_delay(ee_addr) inw(ee_addr)
83 /* Wait for the EEPROM to finish the previous operation. */
84 static int eeprom_busy_poll(long ee_ioaddr)
87 outw(EE_ENB, ee_ioaddr);
88 for (i = 0; i < 10000; i++) /* Typical 2000 ticks */
89 if (inw(ee_ioaddr) & EE_DATA_READ)
94 /* This executes a generic EEPROM command, typically a write or write enable.
95 It returns the data output from the EEPROM, and thus may also be used for
97 static int do_eeprom_cmd(long ioaddr, int cmd, int cmd_len)
100 long ee_addr = ioaddr + EE_OFFSET;
103 printf(" EEPROM op 0x%x: ", cmd);
105 outw(EE_ENB | EE_SHIFT_CLK, ee_addr);
107 /* Shift the command bits out. */
109 short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
110 outw(dataval, ee_addr);
111 eeprom_delay(ee_addr);
113 printf("%X", inw(ee_addr) & 15);
114 outw(dataval | EE_SHIFT_CLK, ee_addr);
115 eeprom_delay(ee_addr);
116 retval = (retval << 1) | ((inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
117 } while (--cmd_len >= 0);
119 outw(EE_ENB, ee_addr);
121 /* Terminate the EEPROM access. */
122 outw(EE_ENB & ~EE_CS, ee_addr);
124 printf(" EEPROM result is 0x%5.5x.\n", retval);
128 static int read_eeprom(long ioaddr, int location, int addr_len)
130 return do_eeprom_cmd(ioaddr, ((EE_READ_CMD << addr_len) | location)
131 << 16 , 3 + addr_len + 16) & 0xffff;
134 static void write_eeprom(long ioaddr, int index, int value, int addr_len)
136 long ee_ioaddr = ioaddr + EE_OFFSET;
139 /* Poll for previous op finished. */
140 eeprom_busy_poll(ee_ioaddr); /* Typical 0 ticks */
141 /* Enable programming modes. */
142 do_eeprom_cmd(ioaddr, (0x4f << (addr_len-4)), 3 + addr_len);
143 /* Do the actual write. */
144 do_eeprom_cmd(ioaddr,
145 (((EE_WRITE_CMD<<addr_len) | index)<<16) | (value & 0xffff),
147 /* Poll for write finished. */
148 i = eeprom_busy_poll(ee_ioaddr); /* Typical 2000 ticks */
150 printf(" Write finished after %d ticks.\n", i);
151 /* Disable programming. This command is not instantaneous, so we check
152 for busy before the next op. */
153 do_eeprom_cmd(ioaddr, (0x40 << (addr_len-4)), 3 + addr_len);
154 eeprom_busy_poll(ee_ioaddr);
157 static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr)
159 unsigned short checksum = 0;
163 printf("Resetting i82559 EEPROM @ 0x%08lx ... ", ioaddr);
165 size_test = do_eeprom_cmd(ioaddr, (EE_READ_CMD << 8) << 16, 27);
166 eeprom_addr_size = (size_test & 0xffe0000) == 0xffe0000 ? 8 : 6;
167 eeprom_size = 1 << eeprom_addr_size;
169 memcpy(eeprom, default_eeprom, sizeof default_eeprom);
171 for (i = 0; i < 3; i++)
172 eeprom[i] = (hwaddr[i*2+1]<<8) + hwaddr[i*2];
174 /* Recalculate the checksum. */
175 for (i = 0; i < eeprom_size - 1; i++)
176 checksum += eeprom[i];
177 eeprom[i] = 0xBABA - checksum;
179 for (i = 0; i < eeprom_size; i++)
180 write_eeprom(ioaddr, i, eeprom[i], eeprom_addr_size);
182 for (i = 0; i < eeprom_size; i++)
183 if (read_eeprom(ioaddr, i, eeprom_addr_size) != eeprom[i]) {
192 static unsigned int hatoi(char *p, char **errp)
194 unsigned int res = 0;
204 res |= (*p - 'a' + 10);
212 res |= (*p - 'A' + 10);
246 static unsigned char *gethwaddr(char *in, unsigned char *out)
253 if (in[i*3+2] == 0 && i == 5) {
254 out[i] = hatoi(&in[i*3], &err);
258 } else if (in[i*3+2] == ':' && i < 5) {
262 out[i] = hatoi(tmp, &err);
275 read_config_dword(int bus, int dev, int func, int reg)
279 outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
287 read_config_word(int bus, int dev, int func, int reg)
291 outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
293 res = inw(0xcfc + (reg & 2));
299 write_config_word(int bus, int dev, int func, int reg, u16 data)
302 outl(0x80000000|(bus&0xff)<<16|(dev&0x1f)<<11|(func&7)<<8|(reg&0xfc),
304 outw(data, 0xcfc + (reg & 2));
309 int main (int argc, char *argv[])
311 unsigned char *eth_addr;
317 printf ("call with base Ethernet address\n");
322 eth_addr = gethwaddr(argv[1], buf);
323 if (NULL == eth_addr) {
324 printf ("Can not parse ethernet address\n");
327 if (eth_addr[5] & 0x01) {
328 printf("Base Ethernet address must be even\n");
332 for (instance = 0; instance < 2; instance ++) {
333 unsigned int io_addr;
334 unsigned char mac[6];
335 int bar1 = read_config_dword(0, 6+instance, 0, 0x14);
337 printf("ETH%d is disabled %x\n", instance, bar1);
339 printf("ETH%d IO=0x%04x\n", instance, bar1 & ~3);
341 io_addr = (bar1 & (~3L));
344 write_config_word(0, 6+instance, 0, 4,
345 read_config_word(0, 6+instance, 0, 4) | 1);
346 printf("ETH%d CMD %04x\n", instance,
347 read_config_word(0, 6+instance, 0, 4));
349 memcpy(mac, eth_addr, 6);
352 printf("got io=%04x, ha=%02x:%02x:%02x:%02x:%02x:%02x\n",
353 io_addr, mac[0], mac[1], mac[2],
354 mac[3], mac[4], mac[5]);
355 reset_eeprom(io_addr, mac);