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