blob: 1f2ae909d3e65a8ec8f1d957b04064042841caf5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*!*****************************************************************************
2*!
Matt LaPlante0779bf22006-11-30 05:24:39 +01003*! Implements an interface for i2c compatible eeproms to run under Linux.
4*! Supports 2k, 8k(?) and 16k. Uses adaptive timing adjustments by
Linus Torvalds1da177e2005-04-16 15:20:36 -07005*! Johan.Adolfsson@axis.com
6*!
7*! Probing results:
8*! 8k or not is detected (the assumes 2k or 16k)
9*! 2k or 16k detected using test reads and writes.
10*!
11*!------------------------------------------------------------------------
12*! HISTORY
13*!
14*! DATE NAME CHANGES
15*! ---- ---- -------
16*! Aug 28 1999 Edgar Iglesias Initial Version
17*! Aug 31 1999 Edgar Iglesias Allow simultaneous users.
18*! Sep 03 1999 Edgar Iglesias Updated probe.
19*! Sep 03 1999 Edgar Iglesias Added bail-out stuff if we get interrupted
20*! in the spin-lock.
21*!
Linus Torvalds1da177e2005-04-16 15:20:36 -070022*! (c) 1999 Axis Communications AB, Lund, Sweden
23*!*****************************************************************************/
24
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#include <linux/kernel.h>
26#include <linux/sched.h>
27#include <linux/fs.h>
28#include <linux/init.h>
29#include <linux/delay.h>
30#include <linux/interrupt.h>
Jonathan Corbetf2b98572008-05-18 15:32:43 -060031#include <linux/smp_lock.h>
Mikael Starvik7e920422005-07-27 11:44:34 -070032#include <linux/wait.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070033#include <asm/uaccess.h>
34#include "i2c.h"
35
Jesper Nilsson3d6c03f2008-01-17 14:44:00 +010036#define D(x)
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
38/* If we should use adaptive timing or not: */
Jesper Nilsson3d6c03f2008-01-17 14:44:00 +010039/* #define EEPROM_ADAPTIVE_TIMING */
Linus Torvalds1da177e2005-04-16 15:20:36 -070040
41#define EEPROM_MAJOR_NR 122 /* use a LOCAL/EXPERIMENTAL major for now */
42#define EEPROM_MINOR_NR 0
43
44/* Empirical sane initial value of the delay, the value will be adapted to
45 * what the chip needs when using EEPROM_ADAPTIVE_TIMING.
46 */
47#define INITIAL_WRITEDELAY_US 4000
48#define MAX_WRITEDELAY_US 10000 /* 10 ms according to spec for 2KB EEPROM */
49
50/* This one defines how many times to try when eeprom fails. */
51#define EEPROM_RETRIES 10
52
53#define EEPROM_2KB (2 * 1024)
54/*#define EEPROM_4KB (4 * 1024)*/ /* Exists but not used in Axis products */
55#define EEPROM_8KB (8 * 1024 - 1 ) /* Last byte has write protection bit */
56#define EEPROM_16KB (16 * 1024)
57
58#define i2c_delay(x) udelay(x)
59
60/*
61 * This structure describes the attached eeprom chip.
62 * The values are probed for.
63 */
64
65struct eeprom_type
66{
67 unsigned long size;
68 unsigned long sequential_write_pagesize;
69 unsigned char select_cmd;
70 unsigned long usec_delay_writecycles; /* Min time between write cycles
71 (up to 10ms for some models) */
72 unsigned long usec_delay_step; /* For adaptive algorithm */
73 int adapt_state; /* 1 = To high , 0 = Even, -1 = To low */
74
75 /* this one is to keep the read/write operations atomic */
76 wait_queue_head_t wait_q;
77 volatile int busy;
78 int retry_cnt_addr; /* Used to keep track of number of retries for
79 adaptive timing adjustments */
80 int retry_cnt_read;
81};
82
83static int eeprom_open(struct inode * inode, struct file * file);
84static loff_t eeprom_lseek(struct file * file, loff_t offset, int orig);
85static ssize_t eeprom_read(struct file * file, char * buf, size_t count,
86 loff_t *off);
87static ssize_t eeprom_write(struct file * file, const char * buf, size_t count,
88 loff_t *off);
89static int eeprom_close(struct inode * inode, struct file * file);
90
91static int eeprom_address(unsigned long addr);
92static int read_from_eeprom(char * buf, int count);
93static int eeprom_write_buf(loff_t addr, const char * buf, int count);
94static int eeprom_read_buf(loff_t addr, char * buf, int count);
95
96static void eeprom_disable_write_protect(void);
97
98
99static const char eeprom_name[] = "eeprom";
100
101/* chip description */
102static struct eeprom_type eeprom;
103
104/* This is the exported file-operations structure for this device. */
Arjan van de Ven5dfe4c92007-02-12 00:55:31 -0800105const struct file_operations eeprom_fops =
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106{
107 .llseek = eeprom_lseek,
108 .read = eeprom_read,
109 .write = eeprom_write,
110 .open = eeprom_open,
111 .release = eeprom_close
112};
113
114/* eeprom init call. Probes for different eeprom models. */
115
116int __init eeprom_init(void)
117{
118 init_waitqueue_head(&eeprom.wait_q);
119 eeprom.busy = 0;
120
121#ifdef CONFIG_ETRAX_I2C_EEPROM_PROBE
122#define EETEXT "Found"
123#else
124#define EETEXT "Assuming"
125#endif
126 if (register_chrdev(EEPROM_MAJOR_NR, eeprom_name, &eeprom_fops))
127 {
128 printk(KERN_INFO "%s: unable to get major %d for eeprom device\n",
129 eeprom_name, EEPROM_MAJOR_NR);
130 return -1;
131 }
132
133 printk("EEPROM char device v0.3, (c) 2000 Axis Communications AB\n");
134
135 /*
136 * Note: Most of this probing method was taken from the printserver (5470e)
137 * codebase. It did not contain a way of finding the 16kB chips
138 * (M24128 or variants). The method used here might not work
139 * for all models. If you encounter problems the easiest way
140 * is probably to define your model within #ifdef's, and hard-
141 * code it.
142 */
143
144 eeprom.size = 0;
145 eeprom.usec_delay_writecycles = INITIAL_WRITEDELAY_US;
146 eeprom.usec_delay_step = 128;
147 eeprom.adapt_state = 0;
148
149#ifdef CONFIG_ETRAX_I2C_EEPROM_PROBE
150 i2c_start();
151 i2c_outbyte(0x80);
152 if(!i2c_getack())
153 {
154 /* It's not 8k.. */
155 int success = 0;
156 unsigned char buf_2k_start[16];
157
158 /* Im not sure this will work... :) */
159 /* assume 2kB, if failure go for 16kB */
160 /* Test with 16kB settings.. */
161 /* If it's a 2kB EEPROM and we address it outside it's range
162 * it will mirror the address space:
163 * 1. We read two locations (that are mirrored),
164 * if the content differs * it's a 16kB EEPROM.
165 * 2. if it doesn't differ - write different value to one of the locations,
166 * check the other - if content still is the same it's a 2k EEPROM,
167 * restore original data.
168 */
169#define LOC1 8
170#define LOC2 (0x1fb) /*1fb, 3ed, 5df, 7d1 */
171
172 /* 2k settings */
173 i2c_stop();
174 eeprom.size = EEPROM_2KB;
175 eeprom.select_cmd = 0xA0;
176 eeprom.sequential_write_pagesize = 16;
177 if( eeprom_read_buf( 0, buf_2k_start, 16 ) == 16 )
178 {
179 D(printk("2k start: '%16.16s'\n", buf_2k_start));
180 }
181 else
182 {
183 printk(KERN_INFO "%s: Failed to read in 2k mode!\n", eeprom_name);
184 }
185
186 /* 16k settings */
187 eeprom.size = EEPROM_16KB;
188 eeprom.select_cmd = 0xA0;
189 eeprom.sequential_write_pagesize = 64;
190
191 {
192 unsigned char loc1[4], loc2[4], tmp[4];
193 if( eeprom_read_buf(LOC2, loc2, 4) == 4)
194 {
195 if( eeprom_read_buf(LOC1, loc1, 4) == 4)
196 {
197 D(printk("0 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
198 LOC1, loc1, LOC2, loc2));
199#if 0
200 if (memcmp(loc1, loc2, 4) != 0 )
201 {
202 /* It's 16k */
203 printk(KERN_INFO "%s: 16k detected in step 1\n", eeprom_name);
204 eeprom.size = EEPROM_16KB;
205 success = 1;
206 }
207 else
208#endif
209 {
210 /* Do step 2 check */
211 /* Invert value */
212 loc1[0] = ~loc1[0];
213 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
214 {
215 /* If 2k EEPROM this write will actually write 10 bytes
216 * from pos 0
217 */
218 D(printk("1 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
219 LOC1, loc1, LOC2, loc2));
220 if( eeprom_read_buf(LOC1, tmp, 4) == 4)
221 {
222 D(printk("2 loc1: (%i) '%4.4s' tmp '%4.4s'\n",
223 LOC1, loc1, tmp));
224 if (memcmp(loc1, tmp, 4) != 0 )
225 {
226 printk(KERN_INFO "%s: read and write differs! Not 16kB\n",
227 eeprom_name);
228 loc1[0] = ~loc1[0];
229
230 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
231 {
232 success = 1;
233 }
234 else
235 {
236 printk(KERN_INFO "%s: Restore 2k failed during probe,"
237 " EEPROM might be corrupt!\n", eeprom_name);
238
239 }
240 i2c_stop();
241 /* Go to 2k mode and write original data */
242 eeprom.size = EEPROM_2KB;
243 eeprom.select_cmd = 0xA0;
244 eeprom.sequential_write_pagesize = 16;
245 if( eeprom_write_buf(0, buf_2k_start, 16) == 16)
246 {
247 }
248 else
249 {
250 printk(KERN_INFO "%s: Failed to write back 2k start!\n",
251 eeprom_name);
252 }
253
254 eeprom.size = EEPROM_2KB;
255 }
256 }
257
258 if(!success)
259 {
260 if( eeprom_read_buf(LOC2, loc2, 1) == 1)
261 {
262 D(printk("0 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
263 LOC1, loc1, LOC2, loc2));
264 if (memcmp(loc1, loc2, 4) == 0 )
265 {
266 /* Data the same, must be mirrored -> 2k */
267 /* Restore data */
268 printk(KERN_INFO "%s: 2k detected in step 2\n", eeprom_name);
269 loc1[0] = ~loc1[0];
270 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
271 {
272 success = 1;
273 }
274 else
275 {
276 printk(KERN_INFO "%s: Restore 2k failed during probe,"
277 " EEPROM might be corrupt!\n", eeprom_name);
278
279 }
280
281 eeprom.size = EEPROM_2KB;
282 }
283 else
284 {
285 printk(KERN_INFO "%s: 16k detected in step 2\n",
286 eeprom_name);
287 loc1[0] = ~loc1[0];
288 /* Data differs, assume 16k */
289 /* Restore data */
290 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
291 {
292 success = 1;
293 }
294 else
295 {
296 printk(KERN_INFO "%s: Restore 16k failed during probe,"
297 " EEPROM might be corrupt!\n", eeprom_name);
298 }
299
300 eeprom.size = EEPROM_16KB;
301 }
302 }
303 }
304 }
305 } /* read LOC1 */
306 } /* address LOC1 */
307 if (!success)
308 {
309 printk(KERN_INFO "%s: Probing failed!, using 2KB!\n", eeprom_name);
310 eeprom.size = EEPROM_2KB;
311 }
312 } /* read */
313 }
314 }
315 else
316 {
317 i2c_outbyte(0x00);
318 if(!i2c_getack())
319 {
320 /* No 8k */
321 eeprom.size = EEPROM_2KB;
322 }
323 else
324 {
325 i2c_start();
326 i2c_outbyte(0x81);
327 if (!i2c_getack())
328 {
329 eeprom.size = EEPROM_2KB;
330 }
331 else
332 {
333 /* It's a 8kB */
334 i2c_inbyte();
335 eeprom.size = EEPROM_8KB;
336 }
337 }
338 }
339 i2c_stop();
340#elif defined(CONFIG_ETRAX_I2C_EEPROM_16KB)
341 eeprom.size = EEPROM_16KB;
342#elif defined(CONFIG_ETRAX_I2C_EEPROM_8KB)
343 eeprom.size = EEPROM_8KB;
344#elif defined(CONFIG_ETRAX_I2C_EEPROM_2KB)
345 eeprom.size = EEPROM_2KB;
346#endif
347
348 switch(eeprom.size)
349 {
350 case (EEPROM_2KB):
351 printk("%s: " EETEXT " i2c compatible 2kB eeprom.\n", eeprom_name);
352 eeprom.sequential_write_pagesize = 16;
353 eeprom.select_cmd = 0xA0;
354 break;
355 case (EEPROM_8KB):
356 printk("%s: " EETEXT " i2c compatible 8kB eeprom.\n", eeprom_name);
357 eeprom.sequential_write_pagesize = 16;
358 eeprom.select_cmd = 0x80;
359 break;
360 case (EEPROM_16KB):
361 printk("%s: " EETEXT " i2c compatible 16kB eeprom.\n", eeprom_name);
362 eeprom.sequential_write_pagesize = 64;
363 eeprom.select_cmd = 0xA0;
364 break;
365 default:
366 eeprom.size = 0;
367 printk("%s: Did not find a supported eeprom\n", eeprom_name);
368 break;
369 }
370
371
372
373 eeprom_disable_write_protect();
374
375 return 0;
376}
377
378/* Opens the device. */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700379static int eeprom_open(struct inode * inode, struct file * file)
380{
Jonathan Corbetf2b98572008-05-18 15:32:43 -0600381 cycle_kernel_lock();
Eric Sesterhenn32ea0862006-07-10 04:45:02 -0700382 if(iminor(inode) != EEPROM_MINOR_NR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383 return -ENXIO;
Eric Sesterhenn32ea0862006-07-10 04:45:02 -0700384 if(imajor(inode) != EEPROM_MAJOR_NR)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385 return -ENXIO;
386
387 if( eeprom.size > 0 )
388 {
389 /* OK */
390 return 0;
391 }
392
393 /* No EEprom found */
394 return -EFAULT;
395}
396
397/* Changes the current file position. */
398
399static loff_t eeprom_lseek(struct file * file, loff_t offset, int orig)
400{
401/*
402 * orig 0: position from begning of eeprom
403 * orig 1: relative from current position
404 * orig 2: position from last eeprom address
405 */
406
407 switch (orig)
408 {
409 case 0:
410 file->f_pos = offset;
411 break;
412 case 1:
413 file->f_pos += offset;
414 break;
415 case 2:
416 file->f_pos = eeprom.size - offset;
417 break;
418 default:
419 return -EINVAL;
420 }
421
422 /* truncate position */
423 if (file->f_pos < 0)
424 {
425 file->f_pos = 0;
426 return(-EOVERFLOW);
427 }
428
429 if (file->f_pos >= eeprom.size)
430 {
431 file->f_pos = eeprom.size - 1;
432 return(-EOVERFLOW);
433 }
434
435 return ( file->f_pos );
436}
437
438/* Reads data from eeprom. */
439
440static int eeprom_read_buf(loff_t addr, char * buf, int count)
441{
442 struct file f;
443
444 f.f_pos = addr;
445 return eeprom_read(&f, buf, count, &addr);
446}
447
448
449
450/* Reads data from eeprom. */
451
452static ssize_t eeprom_read(struct file * file, char * buf, size_t count, loff_t *off)
453{
454 int read=0;
455 unsigned long p = file->f_pos;
456
457 unsigned char page;
458
459 if(p >= eeprom.size) /* Address i 0 - (size-1) */
460 {
461 return -EFAULT;
462 }
463
Mikael Starvik7e920422005-07-27 11:44:34 -0700464 wait_event_interruptible(eeprom.wait_q, !eeprom.busy);
465 if (signal_pending(current))
466 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 eeprom.busy++;
469
470 page = (unsigned char) (p >> 8);
471
472 if(!eeprom_address(p))
473 {
474 printk(KERN_INFO "%s: Read failed to address the eeprom: "
475 "0x%08X (%i) page: %i\n", eeprom_name, (int)p, (int)p, page);
476 i2c_stop();
477
478 /* don't forget to wake them up */
479 eeprom.busy--;
480 wake_up_interruptible(&eeprom.wait_q);
481 return -EFAULT;
482 }
483
484 if( (p + count) > eeprom.size)
485 {
486 /* truncate count */
487 count = eeprom.size - p;
488 }
489
490 /* stop dummy write op and initiate the read op */
491 i2c_start();
492
493 /* special case for small eeproms */
494 if(eeprom.size < EEPROM_16KB)
495 {
496 i2c_outbyte( eeprom.select_cmd | 1 | (page << 1) );
497 }
498
499 /* go on with the actual read */
500 read = read_from_eeprom( buf, count);
501
502 if(read > 0)
503 {
504 file->f_pos += read;
505 }
506
507 eeprom.busy--;
508 wake_up_interruptible(&eeprom.wait_q);
509 return read;
510}
511
512/* Writes data to eeprom. */
513
514static int eeprom_write_buf(loff_t addr, const char * buf, int count)
515{
516 struct file f;
517
518 f.f_pos = addr;
519
520 return eeprom_write(&f, buf, count, &addr);
521}
522
523
524/* Writes data to eeprom. */
525
526static ssize_t eeprom_write(struct file * file, const char * buf, size_t count,
527 loff_t *off)
528{
529 int i, written, restart=1;
530 unsigned long p;
531
532 if (!access_ok(VERIFY_READ, buf, count))
533 {
534 return -EFAULT;
535 }
536
Mikael Starvik7e920422005-07-27 11:44:34 -0700537 wait_event_interruptible(eeprom.wait_q, !eeprom.busy);
538 /* bail out if we get interrupted */
539 if (signal_pending(current))
540 return -EINTR;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700541 eeprom.busy++;
542 for(i = 0; (i < EEPROM_RETRIES) && (restart > 0); i++)
543 {
544 restart = 0;
545 written = 0;
546 p = file->f_pos;
547
548
549 while( (written < count) && (p < eeprom.size))
550 {
551 /* address the eeprom */
552 if(!eeprom_address(p))
553 {
554 printk(KERN_INFO "%s: Write failed to address the eeprom: "
555 "0x%08X (%i) \n", eeprom_name, (int)p, (int)p);
556 i2c_stop();
557
558 /* don't forget to wake them up */
559 eeprom.busy--;
560 wake_up_interruptible(&eeprom.wait_q);
561 return -EFAULT;
562 }
563#ifdef EEPROM_ADAPTIVE_TIMING
564 /* Adaptive algorithm to adjust timing */
565 if (eeprom.retry_cnt_addr > 0)
566 {
567 /* To Low now */
568 D(printk(">D=%i d=%i\n",
569 eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
570
571 if (eeprom.usec_delay_step < 4)
572 {
573 eeprom.usec_delay_step++;
574 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
575 }
576 else
577 {
578
579 if (eeprom.adapt_state > 0)
580 {
581 /* To Low before */
582 eeprom.usec_delay_step *= 2;
583 if (eeprom.usec_delay_step > 2)
584 {
585 eeprom.usec_delay_step--;
586 }
587 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
588 }
589 else if (eeprom.adapt_state < 0)
590 {
591 /* To High before (toggle dir) */
592 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
593 if (eeprom.usec_delay_step > 1)
594 {
595 eeprom.usec_delay_step /= 2;
596 eeprom.usec_delay_step--;
597 }
598 }
599 }
600
601 eeprom.adapt_state = 1;
602 }
603 else
604 {
605 /* To High (or good) now */
606 D(printk("<D=%i d=%i\n",
607 eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
608
609 if (eeprom.adapt_state < 0)
610 {
611 /* To High before */
612 if (eeprom.usec_delay_step > 1)
613 {
614 eeprom.usec_delay_step *= 2;
615 eeprom.usec_delay_step--;
616
617 if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
618 {
619 eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
620 }
621 }
622 }
623 else if (eeprom.adapt_state > 0)
624 {
625 /* To Low before (toggle dir) */
626 if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
627 {
628 eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
629 }
630 if (eeprom.usec_delay_step > 1)
631 {
632 eeprom.usec_delay_step /= 2;
633 eeprom.usec_delay_step--;
634 }
635
636 eeprom.adapt_state = -1;
637 }
638
639 if (eeprom.adapt_state > -100)
640 {
641 eeprom.adapt_state--;
642 }
643 else
644 {
645 /* Restart adaption */
646 D(printk("#Restart\n"));
647 eeprom.usec_delay_step++;
648 }
649 }
650#endif /* EEPROM_ADAPTIVE_TIMING */
651 /* write until we hit a page boundary or count */
652 do
653 {
654 i2c_outbyte(buf[written]);
655 if(!i2c_getack())
656 {
657 restart=1;
658 printk(KERN_INFO "%s: write error, retrying. %d\n", eeprom_name, i);
659 i2c_stop();
660 break;
661 }
662 written++;
663 p++;
664 } while( written < count && ( p % eeprom.sequential_write_pagesize ));
665
666 /* end write cycle */
667 i2c_stop();
668 i2c_delay(eeprom.usec_delay_writecycles);
669 } /* while */
670 } /* for */
671
672 eeprom.busy--;
673 wake_up_interruptible(&eeprom.wait_q);
674 if (written == 0 && file->f_pos >= eeprom.size){
675 return -ENOSPC;
676 }
677 file->f_pos += written;
678 return written;
679}
680
681/* Closes the device. */
682
683static int eeprom_close(struct inode * inode, struct file * file)
684{
685 /* do nothing for now */
686 return 0;
687}
688
689/* Sets the current address of the eeprom. */
690
691static int eeprom_address(unsigned long addr)
692{
693 int i;
694 unsigned char page, offset;
695
696 page = (unsigned char) (addr >> 8);
697 offset = (unsigned char) addr;
698
699 for(i = 0; i < EEPROM_RETRIES; i++)
700 {
701 /* start a dummy write for addressing */
702 i2c_start();
703
704 if(eeprom.size == EEPROM_16KB)
705 {
706 i2c_outbyte( eeprom.select_cmd );
707 i2c_getack();
708 i2c_outbyte(page);
709 }
710 else
711 {
712 i2c_outbyte( eeprom.select_cmd | (page << 1) );
713 }
714 if(!i2c_getack())
715 {
716 /* retry */
717 i2c_stop();
718 /* Must have a delay here.. 500 works, >50, 100->works 5th time*/
719 i2c_delay(MAX_WRITEDELAY_US / EEPROM_RETRIES * i);
720 /* The chip needs up to 10 ms from write stop to next start */
721
722 }
723 else
724 {
725 i2c_outbyte(offset);
726
727 if(!i2c_getack())
728 {
729 /* retry */
730 i2c_stop();
731 }
732 else
733 break;
734 }
735 }
736
737
738 eeprom.retry_cnt_addr = i;
739 D(printk("%i\n", eeprom.retry_cnt_addr));
740 if(eeprom.retry_cnt_addr == EEPROM_RETRIES)
741 {
742 /* failed */
743 return 0;
744 }
745 return 1;
746}
747
748/* Reads from current address. */
749
750static int read_from_eeprom(char * buf, int count)
751{
752 int i, read=0;
753
754 for(i = 0; i < EEPROM_RETRIES; i++)
755 {
756 if(eeprom.size == EEPROM_16KB)
757 {
758 i2c_outbyte( eeprom.select_cmd | 1 );
759 }
760
761 if(i2c_getack())
762 {
763 break;
764 }
765 }
766
767 if(i == EEPROM_RETRIES)
768 {
769 printk(KERN_INFO "%s: failed to read from eeprom\n", eeprom_name);
770 i2c_stop();
771
772 return -EFAULT;
773 }
774
775 while( (read < count))
776 {
777 if (put_user(i2c_inbyte(), &buf[read++]))
778 {
779 i2c_stop();
780
781 return -EFAULT;
782 }
783
784 /*
785 * make sure we don't ack last byte or you will get very strange
786 * results!
787 */
788 if(read < count)
789 {
790 i2c_sendack();
791 }
792 }
793
794 /* stop the operation */
795 i2c_stop();
796
797 return read;
798}
799
800/* Disables write protection if applicable. */
801
802#define DBP_SAVE(x)
803#define ax_printf printk
804static void eeprom_disable_write_protect(void)
805{
806 /* Disable write protect */
807 if (eeprom.size == EEPROM_8KB)
808 {
809 /* Step 1 Set WEL = 1 (write 00000010 to address 1FFFh */
810 i2c_start();
811 i2c_outbyte(0xbe);
812 if(!i2c_getack())
813 {
814 DBP_SAVE(ax_printf("Get ack returns false\n"));
815 }
816 i2c_outbyte(0xFF);
817 if(!i2c_getack())
818 {
819 DBP_SAVE(ax_printf("Get ack returns false 2\n"));
820 }
821 i2c_outbyte(0x02);
822 if(!i2c_getack())
823 {
824 DBP_SAVE(ax_printf("Get ack returns false 3\n"));
825 }
826 i2c_stop();
827
828 i2c_delay(1000);
829
830 /* Step 2 Set RWEL = 1 (write 00000110 to address 1FFFh */
831 i2c_start();
832 i2c_outbyte(0xbe);
833 if(!i2c_getack())
834 {
835 DBP_SAVE(ax_printf("Get ack returns false 55\n"));
836 }
837 i2c_outbyte(0xFF);
838 if(!i2c_getack())
839 {
840 DBP_SAVE(ax_printf("Get ack returns false 52\n"));
841 }
842 i2c_outbyte(0x06);
843 if(!i2c_getack())
844 {
845 DBP_SAVE(ax_printf("Get ack returns false 53\n"));
846 }
847 i2c_stop();
848
849 /* Step 3 Set BP1, BP0, and/or WPEN bits (write 00000110 to address 1FFFh */
850 i2c_start();
851 i2c_outbyte(0xbe);
852 if(!i2c_getack())
853 {
854 DBP_SAVE(ax_printf("Get ack returns false 56\n"));
855 }
856 i2c_outbyte(0xFF);
857 if(!i2c_getack())
858 {
859 DBP_SAVE(ax_printf("Get ack returns false 57\n"));
860 }
861 i2c_outbyte(0x06);
862 if(!i2c_getack())
863 {
864 DBP_SAVE(ax_printf("Get ack returns false 58\n"));
865 }
866 i2c_stop();
867
868 /* Write protect disabled */
869 }
870}
871
872module_init(eeprom_init);