blob: 60b937ef8de5092c7a6cc98d25af96d8d4bc8988 [file] [log] [blame]
wdenkaffae2b2002-08-17 09:36:01 +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 */
21#define _LINUX_STRING_H_ /* avoid unnecessary str/mem functions */
22
23#include <common.h>
wdenk27b207f2003-07-24 23:38:38 +000024#include <exports.h>
wdenkaffae2b2002-08-17 09:36:01 +000025
26static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr);
27
wdenk27b207f2003-07-24 23:38:38 +000028int eepro100_eeprom(int argc, char *argv[])
wdenkaffae2b2002-08-17 09:36:01 +000029{
30 int ret = 0;
31
32 unsigned char hwaddr1[6] = { 0x00, 0x00, 0x02, 0x03, 0x04, 0x05 };
33 unsigned char hwaddr2[6] = { 0x00, 0x00, 0x02, 0x03, 0x04, 0x06 };
34
wdenk27b207f2003-07-24 23:38:38 +000035 app_startup(argv);
36
wdenkaffae2b2002-08-17 09:36:01 +000037#if defined(CONFIG_OXC)
38 ret |= reset_eeprom(0x80000000, hwaddr1);
39 ret |= reset_eeprom(0x81000000, hwaddr2);
40#endif
41
42 return ret;
43}
44
45/* Default EEPROM for i82559 */
46static unsigned short default_eeprom[64] = {
47 0x0100, 0x0302, 0x0504, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
48 0xffff, 0xffff, 0x40c0, 0x0000, 0x0000, 0xffff, 0xffff, 0xffff,
49 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
50 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
51 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
52 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
53 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
54 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
55};
56
57static unsigned short eeprom[256];
58
59static int eeprom_size = 64;
60static int eeprom_addr_size = 6;
61
62static int debug = 0;
63
64static inline unsigned short swap16(unsigned short x)
65{
66 return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
67}
68
69static inline void outw(short data, long addr)
70{
71 *(volatile short *)(addr) = swap16(data);
72}
73
74static inline short inw(long addr)
75{
76 return swap16(*(volatile short *)(addr));
77}
78
79static inline void *memcpy(void *dst, const void *src, unsigned int len)
80{
81 void * ret = dst;
82 while (len-- > 0) *((char *)dst)++ = *((char *)src)++;
83 return ret;
84}
85
86/* The EEPROM commands include the alway-set leading bit. */
87#define EE_WRITE_CMD (5)
88#define EE_READ_CMD (6)
89#define EE_ERASE_CMD (7)
90
91/* Serial EEPROM section. */
92#define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
93#define EE_CS 0x02 /* EEPROM chip select. */
94#define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
95#define EE_DATA_READ 0x08 /* EEPROM chip data out. */
96#define EE_ENB (0x4800 | EE_CS)
97#define EE_WRITE_0 0x4802
98#define EE_WRITE_1 0x4806
99#define EE_OFFSET 14
100
101/* Delay between EEPROM clock transitions. */
102#define eeprom_delay(ee_addr) inw(ee_addr)
103
104/* Wait for the EEPROM to finish the previous operation. */
105static int eeprom_busy_poll(long ee_ioaddr)
106{
107 int i;
108 outw(EE_ENB, ee_ioaddr);
109 for (i = 0; i < 10000; i++) /* Typical 2000 ticks */
110 if (inw(ee_ioaddr) & EE_DATA_READ)
111 break;
112 return i;
113}
114
115/* This executes a generic EEPROM command, typically a write or write enable.
116 It returns the data output from the EEPROM, and thus may also be used for
117 reads. */
118static int do_eeprom_cmd(long ioaddr, int cmd, int cmd_len)
119{
120 unsigned retval = 0;
121 long ee_addr = ioaddr + EE_OFFSET;
122
123 if (debug > 1)
wdenk27b207f2003-07-24 23:38:38 +0000124 printf(" EEPROM op 0x%x: ", cmd);
wdenkaffae2b2002-08-17 09:36:01 +0000125
126 outw(EE_ENB | EE_SHIFT_CLK, ee_addr);
127
128 /* Shift the command bits out. */
129 do {
130 short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
131 outw(dataval, ee_addr);
132 eeprom_delay(ee_addr);
133 if (debug > 2)
wdenk27b207f2003-07-24 23:38:38 +0000134 printf("%X", inw(ee_addr) & 15);
wdenkaffae2b2002-08-17 09:36:01 +0000135 outw(dataval | EE_SHIFT_CLK, ee_addr);
136 eeprom_delay(ee_addr);
137 retval = (retval << 1) | ((inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
138 } while (--cmd_len >= 0);
139#if 0
140 outw(EE_ENB, ee_addr);
141#endif
142 /* Terminate the EEPROM access. */
143 outw(EE_ENB & ~EE_CS, ee_addr);
144 if (debug > 1)
wdenk27b207f2003-07-24 23:38:38 +0000145 printf(" EEPROM result is 0x%5.5x.\n", retval);
wdenkaffae2b2002-08-17 09:36:01 +0000146 return retval;
147}
148
149static int read_eeprom(long ioaddr, int location, int addr_len)
150{
151 return do_eeprom_cmd(ioaddr, ((EE_READ_CMD << addr_len) | location)
152 << 16 , 3 + addr_len + 16) & 0xffff;
153}
154
155static void write_eeprom(long ioaddr, int index, int value, int addr_len)
156{
157 long ee_ioaddr = ioaddr + EE_OFFSET;
158 int i;
159
160 /* Poll for previous op finished. */
161 eeprom_busy_poll(ee_ioaddr); /* Typical 0 ticks */
162 /* Enable programming modes. */
163 do_eeprom_cmd(ioaddr, (0x4f << (addr_len-4)), 3 + addr_len);
164 /* Do the actual write. */
165 do_eeprom_cmd(ioaddr,
166 (((EE_WRITE_CMD<<addr_len) | index)<<16) | (value & 0xffff),
167 3 + addr_len + 16);
168 /* Poll for write finished. */
169 i = eeprom_busy_poll(ee_ioaddr); /* Typical 2000 ticks */
170 if (debug)
wdenk27b207f2003-07-24 23:38:38 +0000171 printf(" Write finished after %d ticks.\n", i);
wdenkaffae2b2002-08-17 09:36:01 +0000172 /* Disable programming. This command is not instantaneous, so we check
173 for busy before the next op. */
174 do_eeprom_cmd(ioaddr, (0x40 << (addr_len-4)), 3 + addr_len);
175 eeprom_busy_poll(ee_ioaddr);
176}
177
178static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr)
179{
180 unsigned short checksum = 0;
181 int size_test;
182 int i;
183
wdenk27b207f2003-07-24 23:38:38 +0000184 printf("Resetting i82559 EEPROM @ 0x%08lX ... ", ioaddr);
wdenkaffae2b2002-08-17 09:36:01 +0000185
186 size_test = do_eeprom_cmd(ioaddr, (EE_READ_CMD << 8) << 16, 27);
187 eeprom_addr_size = (size_test & 0xffe0000) == 0xffe0000 ? 8 : 6;
188 eeprom_size = 1 << eeprom_addr_size;
189
190 memcpy(eeprom, default_eeprom, sizeof default_eeprom);
191
192 for (i = 0; i < 3; i++)
193 eeprom[i] = (hwaddr[i*2+1]<<8) + hwaddr[i*2];
194
195 /* Recalculate the checksum. */
196 for (i = 0; i < eeprom_size - 1; i++)
197 checksum += eeprom[i];
198 eeprom[i] = 0xBABA - checksum;
199
200 for (i = 0; i < eeprom_size; i++)
201 write_eeprom(ioaddr, i, eeprom[i], eeprom_addr_size);
202
203 for (i = 0; i < eeprom_size; i++)
204 if (read_eeprom(ioaddr, i, eeprom_addr_size) != eeprom[i]) {
wdenk27b207f2003-07-24 23:38:38 +0000205 printf("failed\n");
wdenkaffae2b2002-08-17 09:36:01 +0000206 return 1;
207 }
208
wdenk27b207f2003-07-24 23:38:38 +0000209 printf("done\n");
wdenkaffae2b2002-08-17 09:36:01 +0000210 return 0;
211}