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