blob: aa6f52c71c1ef720c899db3e1d3c8b117fcb298b [file] [log] [blame]
wdenkfe8c2802002-11-03 00:38:21 +00001/*
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>
Matthias Fuchs049216f2009-02-20 10:19:18 +010027#include <asm/io.h>
wdenkfe8c2802002-11-03 00:38:21 +000028
29#define EEPROM_CAP 0x50000358
30#define EEPROM_DATA 0x5000035c
31
32
33unsigned int eepromReadLong(int offs)
34{
35 unsigned int value;
Matthias Fuchs049216f2009-02-20 10:19:18 +010036 unsigned short ret;
wdenkfe8c2802002-11-03 00:38:21 +000037 int count;
38
Matthias Fuchs049216f2009-02-20 10:19:18 +010039 out_be16((void *)EEPROM_CAP, offs);
wdenkfe8c2802002-11-03 00:38:21 +000040
41 count = 0;
42
43 for (;;)
44 {
45 count++;
Matthias Fuchs049216f2009-02-20 10:19:18 +010046 ret = in_be16((void *)EEPROM_CAP);
wdenkfe8c2802002-11-03 00:38:21 +000047
48 if ((ret & 0x8000) != 0)
wdenk8bde7f72003-06-27 21:31:46 +000049 break;
wdenkfe8c2802002-11-03 00:38:21 +000050 }
51
Matthias Fuchs049216f2009-02-20 10:19:18 +010052 value = in_be32((void *)EEPROM_DATA);
wdenkfe8c2802002-11-03 00:38:21 +000053
54 return value;
55}
56
57
58unsigned 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
70void eepromWriteLong(int offs, unsigned int value)
71{
Matthias Fuchs049216f2009-02-20 10:19:18 +010072 unsigned short ret;
wdenkfe8c2802002-11-03 00:38:21 +000073 int count;
74
75 count = 0;
76
Matthias Fuchs049216f2009-02-20 10:19:18 +010077 out_be32((void *)EEPROM_DATA, value);
78 out_be16((void *)EEPROM_CAP, 0x8000 + offs);
wdenkfe8c2802002-11-03 00:38:21 +000079
80 for (;;)
81 {
82 count++;
Matthias Fuchs049216f2009-02-20 10:19:18 +010083 ret = in_be16((void *)EEPROM_CAP);
wdenkfe8c2802002-11-03 00:38:21 +000084
85 if ((ret & 0x8000) == 0)
wdenk8bde7f72003-06-27 21:31:46 +000086 break;
wdenkfe8c2802002-11-03 00:38:21 +000087 }
88}
89
90
91void 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
105void i2c_read (uchar *addr, int alen, uchar *buffer, int len)
106{
107 int i;
108 int len2, ptr;
109
wdenk8bde7f72003-06-27 21:31:46 +0000110 /* printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); /###* test-only */
wdenkfe8c2802002-11-03 00:38:21 +0000111
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
144void i2c_write (uchar *addr, int alen, uchar *buffer, int len)
145{
146 int i;
147 int len2, ptr;
148
wdenk8bde7f72003-06-27 21:31:46 +0000149 /* printf("\naddr=%x alen=%x buffer=%x len=%x", addr[0], addr[1], *(short *)addr, alen, buffer, len); /###* test-only */
wdenkfe8c2802002-11-03 00:38:21 +0000150
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}