blob: 2b15d05adbb287c988899bc2ad09a3e0cbbc935b [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
Marian Balakowiczb97a2a02008-01-08 18:14:09 +010020/* avoid unnecessary memcpy function */
21#define __HAVE_ARCH_MEMCPY
22#define _PPC_STRING_H_
wdenkaffae2b2002-08-17 09:36:01 +000023
24#include <common.h>
wdenk27b207f2003-07-24 23:38:38 +000025#include <exports.h>
wdenkaffae2b2002-08-17 09:36:01 +000026
27static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr);
28
wdenk27b207f2003-07-24 23:38:38 +000029int eepro100_eeprom(int argc, char *argv[])
wdenkaffae2b2002-08-17 09:36:01 +000030{
31 int ret = 0;
32
33 unsigned char hwaddr1[6] = { 0x00, 0x00, 0x02, 0x03, 0x04, 0x05 };
34 unsigned char hwaddr2[6] = { 0x00, 0x00, 0x02, 0x03, 0x04, 0x06 };
35
wdenk27b207f2003-07-24 23:38:38 +000036 app_startup(argv);
37
wdenkaffae2b2002-08-17 09:36:01 +000038#if defined(CONFIG_OXC)
39 ret |= reset_eeprom(0x80000000, hwaddr1);
40 ret |= reset_eeprom(0x81000000, hwaddr2);
41#endif
42
43 return ret;
44}
45
46/* Default EEPROM for i82559 */
47static unsigned short default_eeprom[64] = {
48 0x0100, 0x0302, 0x0504, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff,
49 0xffff, 0xffff, 0x40c0, 0x0000, 0x0000, 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 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff, 0xffff
56};
57
58static unsigned short eeprom[256];
59
60static int eeprom_size = 64;
61static int eeprom_addr_size = 6;
62
63static int debug = 0;
64
65static inline unsigned short swap16(unsigned short x)
66{
67 return (((x & 0xff) << 8) | ((x & 0xff00) >> 8));
68}
69
70static inline void outw(short data, long addr)
71{
72 *(volatile short *)(addr) = swap16(data);
73}
74
75static inline short inw(long addr)
76{
77 return swap16(*(volatile short *)(addr));
78}
79
80static inline void *memcpy(void *dst, const void *src, unsigned int len)
81{
Wolfgang Denk77ddac92005-10-13 16:45:02 +020082 char *ret = dst;
Wolfgang Denk74812662005-12-12 16:06:05 +010083 while (len-- > 0) {
84 *ret++ = *((char *)src);
85 src++;
86 }
Wolfgang Denk77ddac92005-10-13 16:45:02 +020087 return (void *)ret;
wdenkaffae2b2002-08-17 09:36:01 +000088}
89
90/* The EEPROM commands include the alway-set leading bit. */
91#define EE_WRITE_CMD (5)
92#define EE_READ_CMD (6)
93#define EE_ERASE_CMD (7)
94
95/* Serial EEPROM section. */
96#define EE_SHIFT_CLK 0x01 /* EEPROM shift clock. */
97#define EE_CS 0x02 /* EEPROM chip select. */
98#define EE_DATA_WRITE 0x04 /* EEPROM chip data in. */
99#define EE_DATA_READ 0x08 /* EEPROM chip data out. */
100#define EE_ENB (0x4800 | EE_CS)
101#define EE_WRITE_0 0x4802
102#define EE_WRITE_1 0x4806
103#define EE_OFFSET 14
104
105/* Delay between EEPROM clock transitions. */
106#define eeprom_delay(ee_addr) inw(ee_addr)
107
108/* Wait for the EEPROM to finish the previous operation. */
109static int eeprom_busy_poll(long ee_ioaddr)
110{
111 int i;
112 outw(EE_ENB, ee_ioaddr);
113 for (i = 0; i < 10000; i++) /* Typical 2000 ticks */
114 if (inw(ee_ioaddr) & EE_DATA_READ)
115 break;
116 return i;
117}
118
119/* This executes a generic EEPROM command, typically a write or write enable.
120 It returns the data output from the EEPROM, and thus may also be used for
121 reads. */
122static int do_eeprom_cmd(long ioaddr, int cmd, int cmd_len)
123{
124 unsigned retval = 0;
125 long ee_addr = ioaddr + EE_OFFSET;
126
127 if (debug > 1)
wdenk27b207f2003-07-24 23:38:38 +0000128 printf(" EEPROM op 0x%x: ", cmd);
wdenkaffae2b2002-08-17 09:36:01 +0000129
130 outw(EE_ENB | EE_SHIFT_CLK, ee_addr);
131
132 /* Shift the command bits out. */
133 do {
134 short dataval = (cmd & (1 << cmd_len)) ? EE_WRITE_1 : EE_WRITE_0;
135 outw(dataval, ee_addr);
136 eeprom_delay(ee_addr);
137 if (debug > 2)
wdenk27b207f2003-07-24 23:38:38 +0000138 printf("%X", inw(ee_addr) & 15);
wdenkaffae2b2002-08-17 09:36:01 +0000139 outw(dataval | EE_SHIFT_CLK, ee_addr);
140 eeprom_delay(ee_addr);
141 retval = (retval << 1) | ((inw(ee_addr) & EE_DATA_READ) ? 1 : 0);
142 } while (--cmd_len >= 0);
143#if 0
144 outw(EE_ENB, ee_addr);
145#endif
146 /* Terminate the EEPROM access. */
147 outw(EE_ENB & ~EE_CS, ee_addr);
148 if (debug > 1)
wdenk27b207f2003-07-24 23:38:38 +0000149 printf(" EEPROM result is 0x%5.5x.\n", retval);
wdenkaffae2b2002-08-17 09:36:01 +0000150 return retval;
151}
152
153static int read_eeprom(long ioaddr, int location, int addr_len)
154{
155 return do_eeprom_cmd(ioaddr, ((EE_READ_CMD << addr_len) | location)
156 << 16 , 3 + addr_len + 16) & 0xffff;
157}
158
159static void write_eeprom(long ioaddr, int index, int value, int addr_len)
160{
161 long ee_ioaddr = ioaddr + EE_OFFSET;
162 int i;
163
164 /* Poll for previous op finished. */
165 eeprom_busy_poll(ee_ioaddr); /* Typical 0 ticks */
166 /* Enable programming modes. */
167 do_eeprom_cmd(ioaddr, (0x4f << (addr_len-4)), 3 + addr_len);
168 /* Do the actual write. */
169 do_eeprom_cmd(ioaddr,
170 (((EE_WRITE_CMD<<addr_len) | index)<<16) | (value & 0xffff),
171 3 + addr_len + 16);
172 /* Poll for write finished. */
173 i = eeprom_busy_poll(ee_ioaddr); /* Typical 2000 ticks */
174 if (debug)
wdenk27b207f2003-07-24 23:38:38 +0000175 printf(" Write finished after %d ticks.\n", i);
wdenkaffae2b2002-08-17 09:36:01 +0000176 /* Disable programming. This command is not instantaneous, so we check
177 for busy before the next op. */
178 do_eeprom_cmd(ioaddr, (0x40 << (addr_len-4)), 3 + addr_len);
179 eeprom_busy_poll(ee_ioaddr);
180}
181
182static int reset_eeprom(unsigned long ioaddr, unsigned char *hwaddr)
183{
184 unsigned short checksum = 0;
185 int size_test;
186 int i;
187
wdenk27b207f2003-07-24 23:38:38 +0000188 printf("Resetting i82559 EEPROM @ 0x%08lX ... ", ioaddr);
wdenkaffae2b2002-08-17 09:36:01 +0000189
190 size_test = do_eeprom_cmd(ioaddr, (EE_READ_CMD << 8) << 16, 27);
191 eeprom_addr_size = (size_test & 0xffe0000) == 0xffe0000 ? 8 : 6;
192 eeprom_size = 1 << eeprom_addr_size;
193
194 memcpy(eeprom, default_eeprom, sizeof default_eeprom);
195
196 for (i = 0; i < 3; i++)
197 eeprom[i] = (hwaddr[i*2+1]<<8) + hwaddr[i*2];
198
199 /* Recalculate the checksum. */
200 for (i = 0; i < eeprom_size - 1; i++)
201 checksum += eeprom[i];
202 eeprom[i] = 0xBABA - checksum;
203
204 for (i = 0; i < eeprom_size; i++)
205 write_eeprom(ioaddr, i, eeprom[i], eeprom_addr_size);
206
207 for (i = 0; i < eeprom_size; i++)
208 if (read_eeprom(ioaddr, i, eeprom_addr_size) != eeprom[i]) {
wdenk27b207f2003-07-24 23:38:38 +0000209 printf("failed\n");
wdenkaffae2b2002-08-17 09:36:01 +0000210 return 1;
211 }
212
wdenk27b207f2003-07-24 23:38:38 +0000213 printf("done\n");
wdenkaffae2b2002-08-17 09:36:01 +0000214 return 0;
215}