Import upstream u-boot 1.1.4
[u-boot.git] / board / esd / dasa_sim / eeprom.c
1 /*
2  * (C) Copyright 2001
3  * Stefan Roese, esd gmbh germany, stefan.roese@esd-electronics.com
4  *
5  * See file CREDITS for list of people who contributed to this
6  * project.
7  *
8  * This program is free software; you can redistribute it and/or
9  * modify it under the terms of the GNU General Public License as
10  * published by the Free Software Foundation; either version 2 of
11  * the License, or (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
21  * MA 02111-1307 USA
22  *
23  */
24
25 #include <common.h>
26 #include <command.h>
27
28
29 #define EEPROM_CAP              0x50000358
30 #define EEPROM_DATA             0x5000035c
31
32
33 unsigned int eepromReadLong(int offs)
34 {
35   unsigned int value;
36   volatile unsigned short ret;
37   int count;
38
39   *(unsigned short *)EEPROM_CAP = offs;
40
41   count = 0;
42
43   for (;;)
44     {
45       count++;
46       ret = *(unsigned short *)EEPROM_CAP;
47
48       if ((ret & 0x8000) != 0)
49         break;
50     }
51
52   value = *(unsigned long *)EEPROM_DATA;
53
54   return value;
55 }
56
57
58 unsigned char eepromReadByte(int offs)
59 {
60   unsigned int valueLong;
61   unsigned char *ptr;
62
63   valueLong = eepromReadLong(offs & ~3);
64   ptr = (unsigned char *)&valueLong;
65
66   return ptr[offs & 3];
67 }
68
69
70 void eepromWriteLong(int offs, unsigned int value)
71 {
72   volatile unsigned short ret;
73   int count;
74
75   count = 0;
76
77   *(unsigned long *)EEPROM_DATA = value;
78   *(unsigned short *)EEPROM_CAP = 0x8000 + offs;
79
80   for (;;)
81     {
82       count++;
83       ret = *(unsigned short *)EEPROM_CAP;
84
85       if ((ret & 0x8000) == 0)
86         break;
87     }
88 }
89
90
91 void eepromWriteByte(int offs, unsigned char valueByte)
92 {
93   unsigned int valueLong;
94   unsigned char *ptr;
95
96   valueLong = eepromReadLong(offs & ~3);
97   ptr = (unsigned char *)&valueLong;
98
99   ptr[offs & 3] = valueByte;
100
101   eepromWriteLong(offs & ~3, valueLong);
102 }
103
104
105 void i2c_read (uchar *addr, int alen, uchar *buffer, int len)
106 {
107   int i;
108   int len2, ptr;
109
110   /*  printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); /###* test-only */
111
112   ptr = *(short *)addr;
113
114   /*
115    * Read till lword boundary
116    */
117   len2 = 4 - (*(short *)addr & 0x0003);
118   for (i=0; i<len2; i++)
119     {
120       *buffer++ = eepromReadByte(ptr++);
121     }
122
123   /*
124    * Read all lwords
125    */
126   len2 = (len - len2) >> 2;
127   for (i=0; i<len2; i++)
128     {
129       *(unsigned int *)buffer = eepromReadLong(ptr);
130       buffer += 4;
131       ptr += 4;
132     }
133
134   /*
135    * Read last bytes
136    */
137   len2 = (*(short *)addr + len) & 0x0003;
138   for (i=0; i<len2; i++)
139     {
140       *buffer++ = eepromReadByte(ptr++);
141     }
142 }
143
144 void i2c_write (uchar *addr, int alen, uchar *buffer, int len)
145 {
146   int i;
147   int len2, ptr;
148
149   /*  printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); /###* test-only */
150
151   ptr = *(short *)addr;
152
153   /*
154    * Write till lword boundary
155    */
156   len2 = 4 - (*(short *)addr & 0x0003);
157   for (i=0; i<len2; i++)
158     {
159       eepromWriteByte(ptr++, *buffer++);
160     }
161
162   /*
163    * Write all lwords
164    */
165   len2 = (len - len2) >> 2;
166   for (i=0; i<len2; i++)
167     {
168       eepromWriteLong(ptr, *(unsigned int *)buffer);
169       buffer += 4;
170       ptr += 4;
171     }
172
173   /*
174    * Write last bytes
175    */
176   len2 = (*(short *)addr + len) & 0x0003;
177   for (i=0; i<len2; i++)
178     {
179       eepromWriteByte(ptr++, *buffer++);
180     }
181 }