blob: 1fc78decd2539975650523f523587bce9a44974b [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 *
Wolfgang Denkd79de1d2013-07-08 09:37:19 +02005 * SPDX-License-Identifier: GPL-2.0+
wdenkfe8c2802002-11-03 00:38:21 +00006 */
7
8#include <common.h>
9#include <command.h>
Matthias Fuchsfaac7432009-02-20 10:19:18 +010010#include <asm/io.h>
wdenkfe8c2802002-11-03 00:38:21 +000011
12#define EEPROM_CAP 0x50000358
13#define EEPROM_DATA 0x5000035c
14
15
16unsigned int eepromReadLong(int offs)
17{
18 unsigned int value;
Matthias Fuchsfaac7432009-02-20 10:19:18 +010019 unsigned short ret;
wdenkfe8c2802002-11-03 00:38:21 +000020 int count;
21
Matthias Fuchsfaac7432009-02-20 10:19:18 +010022 out_be16((void *)EEPROM_CAP, offs);
wdenkfe8c2802002-11-03 00:38:21 +000023
24 count = 0;
25
26 for (;;)
27 {
28 count++;
Matthias Fuchsfaac7432009-02-20 10:19:18 +010029 ret = in_be16((void *)EEPROM_CAP);
wdenkfe8c2802002-11-03 00:38:21 +000030
31 if ((ret & 0x8000) != 0)
wdenk57b2d802003-06-27 21:31:46 +000032 break;
wdenkfe8c2802002-11-03 00:38:21 +000033 }
34
Matthias Fuchsfaac7432009-02-20 10:19:18 +010035 value = in_be32((void *)EEPROM_DATA);
wdenkfe8c2802002-11-03 00:38:21 +000036
37 return value;
38}
39
40
41unsigned char eepromReadByte(int offs)
42{
43 unsigned int valueLong;
44 unsigned char *ptr;
45
46 valueLong = eepromReadLong(offs & ~3);
47 ptr = (unsigned char *)&valueLong;
48
49 return ptr[offs & 3];
50}
51
52
53void eepromWriteLong(int offs, unsigned int value)
54{
Matthias Fuchsfaac7432009-02-20 10:19:18 +010055 unsigned short ret;
wdenkfe8c2802002-11-03 00:38:21 +000056 int count;
57
58 count = 0;
59
Matthias Fuchsfaac7432009-02-20 10:19:18 +010060 out_be32((void *)EEPROM_DATA, value);
61 out_be16((void *)EEPROM_CAP, 0x8000 + offs);
wdenkfe8c2802002-11-03 00:38:21 +000062
63 for (;;)
64 {
65 count++;
Matthias Fuchsfaac7432009-02-20 10:19:18 +010066 ret = in_be16((void *)EEPROM_CAP);
wdenkfe8c2802002-11-03 00:38:21 +000067
68 if ((ret & 0x8000) == 0)
wdenk57b2d802003-06-27 21:31:46 +000069 break;
wdenkfe8c2802002-11-03 00:38:21 +000070 }
71}
72
73
74void eepromWriteByte(int offs, unsigned char valueByte)
75{
76 unsigned int valueLong;
77 unsigned char *ptr;
78
79 valueLong = eepromReadLong(offs & ~3);
80 ptr = (unsigned char *)&valueLong;
81
82 ptr[offs & 3] = valueByte;
83
84 eepromWriteLong(offs & ~3, valueLong);
85}
86
87
88void i2c_read (uchar *addr, int alen, uchar *buffer, int len)
89{
90 int i;
91 int len2, ptr;
92
wdenk57b2d802003-06-27 21:31:46 +000093 /* 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 +000094
95 ptr = *(short *)addr;
96
97 /*
98 * Read till lword boundary
99 */
100 len2 = 4 - (*(short *)addr & 0x0003);
101 for (i=0; i<len2; i++)
102 {
103 *buffer++ = eepromReadByte(ptr++);
104 }
105
106 /*
107 * Read all lwords
108 */
109 len2 = (len - len2) >> 2;
110 for (i=0; i<len2; i++)
111 {
112 *(unsigned int *)buffer = eepromReadLong(ptr);
113 buffer += 4;
114 ptr += 4;
115 }
116
117 /*
118 * Read last bytes
119 */
120 len2 = (*(short *)addr + len) & 0x0003;
121 for (i=0; i<len2; i++)
122 {
123 *buffer++ = eepromReadByte(ptr++);
124 }
125}
126
127void i2c_write (uchar *addr, int alen, uchar *buffer, int len)
128{
129 int i;
130 int len2, ptr;
131
wdenk57b2d802003-06-27 21:31:46 +0000132 /* 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 +0000133
134 ptr = *(short *)addr;
135
136 /*
137 * Write till lword boundary
138 */
139 len2 = 4 - (*(short *)addr & 0x0003);
140 for (i=0; i<len2; i++)
141 {
142 eepromWriteByte(ptr++, *buffer++);
143 }
144
145 /*
146 * Write all lwords
147 */
148 len2 = (len - len2) >> 2;
149 for (i=0; i<len2; i++)
150 {
151 eepromWriteLong(ptr, *(unsigned int *)buffer);
152 buffer += 4;
153 ptr += 4;
154 }
155
156 /*
157 * Write last bytes
158 */
159 len2 = (*(short *)addr + len) & 0x0003;
160 for (i=0; i<len2; i++)
161 {
162 eepromWriteByte(ptr++, *buffer++);
163 }
164}