1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67#ifdef __linux__
68#include "aic7xxx_osm.h"
69#include "aic7xxx_inline.h"
70#include "aic7xxx_93cx6.h"
71#else
72#include <dev/aic7xxx/aic7xxx_osm.h>
73#include <dev/aic7xxx/aic7xxx_inline.h>
74#include <dev/aic7xxx/aic7xxx_93cx6.h>
75#endif
76
77
78
79
80
81struct seeprom_cmd {
82 uint8_t len;
83 uint8_t bits[11];
84};
85
86
87static const struct seeprom_cmd seeprom_ewen = {9, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
88static const struct seeprom_cmd seeprom_ewds = {9, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
89
90
91static const struct seeprom_cmd seeprom_long_ewen = {11, {1, 0, 0, 1, 1, 0, 0, 0, 0}};
92static const struct seeprom_cmd seeprom_long_ewds = {11, {1, 0, 0, 0, 0, 0, 0, 0, 0}};
93
94
95static const struct seeprom_cmd seeprom_write = {3, {1, 0, 1}};
96static const struct seeprom_cmd seeprom_read = {3, {1, 1, 0}};
97
98
99
100
101#define CLOCK_PULSE(sd, rdy) \
102 while ((SEEPROM_STATUS_INB(sd) & rdy) == 0) { \
103 ; \
104 } \
105 (void)SEEPROM_INB(sd);
106
107
108
109
110static void
111send_seeprom_cmd(struct seeprom_descriptor *sd, const struct seeprom_cmd *cmd)
112{
113 uint8_t temp;
114 int i = 0;
115
116
117 temp = sd->sd_MS ^ sd->sd_CS;
118 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
119 CLOCK_PULSE(sd, sd->sd_RDY);
120
121 for (i = 0; i < cmd->len; i++) {
122 if (cmd->bits[i] != 0)
123 temp ^= sd->sd_DO;
124 SEEPROM_OUTB(sd, temp);
125 CLOCK_PULSE(sd, sd->sd_RDY);
126 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
127 CLOCK_PULSE(sd, sd->sd_RDY);
128 if (cmd->bits[i] != 0)
129 temp ^= sd->sd_DO;
130 }
131}
132
133
134
135
136static void
137reset_seeprom(struct seeprom_descriptor *sd)
138{
139 uint8_t temp;
140
141 temp = sd->sd_MS;
142 SEEPROM_OUTB(sd, temp);
143 CLOCK_PULSE(sd, sd->sd_RDY);
144 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
145 CLOCK_PULSE(sd, sd->sd_RDY);
146 SEEPROM_OUTB(sd, temp);
147 CLOCK_PULSE(sd, sd->sd_RDY);
148}
149
150
151
152
153
154int
155ahc_read_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
156 u_int start_addr, u_int count)
157{
158 int i = 0;
159 u_int k = 0;
160 uint16_t v;
161 uint8_t temp;
162
163
164
165
166
167 for (k = start_addr; k < count + start_addr; k++) {
168
169
170
171
172 send_seeprom_cmd(sd, &seeprom_read);
173
174
175 temp = sd->sd_MS ^ sd->sd_CS;
176 for (i = (sd->sd_chip - 1); i >= 0; i--) {
177 if ((k & (1 << i)) != 0)
178 temp ^= sd->sd_DO;
179 SEEPROM_OUTB(sd, temp);
180 CLOCK_PULSE(sd, sd->sd_RDY);
181 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
182 CLOCK_PULSE(sd, sd->sd_RDY);
183 if ((k & (1 << i)) != 0)
184 temp ^= sd->sd_DO;
185 }
186
187
188
189
190
191
192
193 v = 0;
194 for (i = 16; i >= 0; i--) {
195 SEEPROM_OUTB(sd, temp);
196 CLOCK_PULSE(sd, sd->sd_RDY);
197 v <<= 1;
198 if (SEEPROM_DATA_INB(sd) & sd->sd_DI)
199 v |= 1;
200 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
201 CLOCK_PULSE(sd, sd->sd_RDY);
202 }
203
204 buf[k - start_addr] = v;
205
206
207 reset_seeprom(sd);
208 }
209#ifdef AHC_DUMP_EEPROM
210 printk("\nSerial EEPROM:\n\t");
211 for (k = 0; k < count; k = k + 1) {
212 if (((k % 8) == 0) && (k != 0)) {
213 printk(KERN_CONT "\n\t");
214 }
215 printk(KERN_CONT " 0x%x", buf[k]);
216 }
217 printk(KERN_CONT "\n");
218#endif
219 return (1);
220}
221
222
223
224
225
226int
227ahc_write_seeprom(struct seeprom_descriptor *sd, uint16_t *buf,
228 u_int start_addr, u_int count)
229{
230 const struct seeprom_cmd *ewen, *ewds;
231 uint16_t v;
232 uint8_t temp;
233 int i, k;
234
235
236 if (sd->sd_chip == C46) {
237 ewen = &seeprom_ewen;
238 ewds = &seeprom_ewds;
239 } else if (sd->sd_chip == C56_66) {
240 ewen = &seeprom_long_ewen;
241 ewds = &seeprom_long_ewds;
242 } else {
243 printk("ahc_write_seeprom: unsupported seeprom type %d\n",
244 sd->sd_chip);
245 return (0);
246 }
247
248 send_seeprom_cmd(sd, ewen);
249 reset_seeprom(sd);
250
251
252 temp = sd->sd_MS ^ sd->sd_CS;
253 for (k = start_addr; k < count + start_addr; k++) {
254
255 send_seeprom_cmd(sd, &seeprom_write);
256
257
258 for (i = (sd->sd_chip - 1); i >= 0; i--) {
259 if ((k & (1 << i)) != 0)
260 temp ^= sd->sd_DO;
261 SEEPROM_OUTB(sd, temp);
262 CLOCK_PULSE(sd, sd->sd_RDY);
263 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
264 CLOCK_PULSE(sd, sd->sd_RDY);
265 if ((k & (1 << i)) != 0)
266 temp ^= sd->sd_DO;
267 }
268
269
270 v = buf[k - start_addr];
271 for (i = 15; i >= 0; i--) {
272 if ((v & (1 << i)) != 0)
273 temp ^= sd->sd_DO;
274 SEEPROM_OUTB(sd, temp);
275 CLOCK_PULSE(sd, sd->sd_RDY);
276 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
277 CLOCK_PULSE(sd, sd->sd_RDY);
278 if ((v & (1 << i)) != 0)
279 temp ^= sd->sd_DO;
280 }
281
282
283 temp = sd->sd_MS;
284 SEEPROM_OUTB(sd, temp);
285 CLOCK_PULSE(sd, sd->sd_RDY);
286 temp = sd->sd_MS ^ sd->sd_CS;
287 do {
288 SEEPROM_OUTB(sd, temp);
289 CLOCK_PULSE(sd, sd->sd_RDY);
290 SEEPROM_OUTB(sd, temp ^ sd->sd_CK);
291 CLOCK_PULSE(sd, sd->sd_RDY);
292 } while ((SEEPROM_DATA_INB(sd) & sd->sd_DI) == 0);
293
294 reset_seeprom(sd);
295 }
296
297
298 send_seeprom_cmd(sd, ewds);
299 reset_seeprom(sd);
300
301 return (1);
302}
303
304int
305ahc_verify_cksum(struct seeprom_config *sc)
306{
307 int i;
308 int maxaddr;
309 uint32_t checksum;
310 uint16_t *scarray;
311
312 maxaddr = (sizeof(*sc)/2) - 1;
313 checksum = 0;
314 scarray = (uint16_t *)sc;
315
316 for (i = 0; i < maxaddr; i++)
317 checksum = checksum + scarray[i];
318 if (checksum == 0
319 || (checksum & 0xFFFF) != sc->checksum) {
320 return (0);
321 } else {
322 return(1);
323 }
324}
325