25 #include <linux/kernel.h>
26 #include <linux/sched.h>
31 #include <linux/wait.h>
32 #include <asm/uaccess.h>
40 #define EEPROM_MAJOR_NR 122
41 #define EEPROM_MINOR_NR 0
46 #define INITIAL_WRITEDELAY_US 4000
47 #define MAX_WRITEDELAY_US 10000
50 #define EEPROM_RETRIES 10
52 #define EEPROM_2KB (2 * 1024)
54 #define EEPROM_8KB (8 * 1024 - 1 )
55 #define EEPROM_16KB (16 * 1024)
57 #define i2c_delay(x) udelay(x)
82 static loff_t eeprom_lseek(
struct file *
file, loff_t
offset,
int orig);
89 static int eeprom_address(
unsigned long addr);
90 static int read_from_eeprom(
char *
buf,
int count);
91 static int eeprom_write_buf(loff_t
addr,
const char *
buf,
int count);
92 static int eeprom_read_buf(loff_t
addr,
char *
buf,
int count);
94 static void eeprom_disable_write_protect(
void);
97 static const char eeprom_name[] =
"eeprom";
105 .llseek = eeprom_lseek,
107 .write = eeprom_write,
109 .release = eeprom_close
118 #ifdef CONFIG_ETRAX_I2C_EEPROM_PROBE
119 #define EETEXT "Found"
121 #define EETEXT "Assuming"
130 printk(
"EEPROM char device v0.3, (c) 2000 Axis Communications AB\n");
143 eeprom.usec_delay_step = 128;
146 #ifdef CONFIG_ETRAX_I2C_EEPROM_PROBE
153 unsigned char buf_2k_start[16];
173 eeprom.sequential_write_pagesize = 16;
174 if( eeprom_read_buf( 0, buf_2k_start, 16 ) == 16 )
176 D(
printk(
"2k start: '%16.16s'\n", buf_2k_start));
186 eeprom.sequential_write_pagesize = 64;
189 unsigned char loc1[4], loc2[4],
tmp[4];
190 if( eeprom_read_buf(LOC2, loc2, 4) == 4)
192 if( eeprom_read_buf(LOC1, loc1, 4) == 4)
194 D(
printk(
"0 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
195 LOC1, loc1, LOC2, loc2));
197 if (
memcmp(loc1, loc2, 4) != 0 )
210 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
215 D(
printk(
"1 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
216 LOC1, loc1, LOC2, loc2));
217 if( eeprom_read_buf(LOC1, tmp, 4) == 4)
219 D(
printk(
"2 loc1: (%i) '%4.4s' tmp '%4.4s'\n",
221 if (
memcmp(loc1, tmp, 4) != 0 )
227 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
234 " EEPROM might be corrupt!\n", eeprom_name);
241 eeprom.sequential_write_pagesize = 16;
242 if( eeprom_write_buf(0, buf_2k_start, 16) == 16)
257 if( eeprom_read_buf(LOC2, loc2, 1) == 1)
259 D(
printk(
"0 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
260 LOC1, loc1, LOC2, loc2));
261 if (
memcmp(loc1, loc2, 4) == 0 )
267 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
274 " EEPROM might be corrupt!\n", eeprom_name);
287 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
294 " EEPROM might be corrupt!\n", eeprom_name);
337 #elif defined(CONFIG_ETRAX_I2C_EEPROM_16KB)
339 #elif defined(CONFIG_ETRAX_I2C_EEPROM_8KB)
341 #elif defined(CONFIG_ETRAX_I2C_EEPROM_2KB)
348 printk(
"%s: " EETEXT " i2c compatible 2kB eeprom.\n", eeprom_name);
349 eeprom.sequential_write_pagesize = 16;
353 printk(
"%s: " EETEXT " i2c compatible 8kB eeprom.\n", eeprom_name);
354 eeprom.sequential_write_pagesize = 16;
358 printk(
"%s: " EETEXT " i2c compatible 16kB eeprom.\n", eeprom_name);
359 eeprom.sequential_write_pagesize = 64;
364 printk(
"%s: Did not find a supported eeprom\n", eeprom_name);
370 eeprom_disable_write_protect();
395 static loff_t eeprom_lseek(
struct file * file, loff_t
offset,
int orig)
431 return ( file->
f_pos );
436 static int eeprom_read_buf(loff_t
addr,
char *
buf,
int count)
438 return eeprom_read(
NULL, buf, count, &addr);
445 static ssize_t eeprom_read(
struct file * file,
char *
buf,
size_t count, loff_t *off)
448 unsigned long p = *off;
460 page = (
unsigned char) (p >> 8);
462 if(!eeprom_address(p))
465 "0x%08X (%i) page: %i\n", eeprom_name, (
int)p, (
int)p, page);
473 if( (p + count) >
eeprom.size)
489 read = read_from_eeprom( buf, count);
502 static int eeprom_write_buf(loff_t
addr,
const char * buf,
int count)
504 return eeprom_write(
NULL, buf, count, &addr);
510 static ssize_t eeprom_write(
struct file * file,
const char * buf,
size_t count,
524 for(i = 0; (i < EEPROM_RETRIES) && (restart > 0); i++)
531 while( (written < count) && (p <
eeprom.size))
534 if(!eeprom_address(p))
537 "0x%08X (%i) \n", eeprom_name, (
int)p, (
int)p);
544 #ifdef EEPROM_ADAPTIVE_TIMING
546 if (
eeprom.retry_cnt_addr > 0)
550 eeprom.usec_delay_writecycles,
eeprom.usec_delay_step));
552 if (
eeprom.usec_delay_step < 4)
555 eeprom.usec_delay_writecycles +=
eeprom.usec_delay_step;
560 if (
eeprom.adapt_state > 0)
563 eeprom.usec_delay_step *= 2;
564 if (
eeprom.usec_delay_step > 2)
568 eeprom.usec_delay_writecycles +=
eeprom.usec_delay_step;
570 else if (
eeprom.adapt_state < 0)
573 eeprom.usec_delay_writecycles +=
eeprom.usec_delay_step;
574 if (
eeprom.usec_delay_step > 1)
576 eeprom.usec_delay_step /= 2;
588 eeprom.usec_delay_writecycles,
eeprom.usec_delay_step));
590 if (
eeprom.adapt_state < 0)
593 if (
eeprom.usec_delay_step > 1)
595 eeprom.usec_delay_step *= 2;
598 if (
eeprom.usec_delay_writecycles >
eeprom.usec_delay_step)
600 eeprom.usec_delay_writecycles -=
eeprom.usec_delay_step;
604 else if (
eeprom.adapt_state > 0)
607 if (
eeprom.usec_delay_writecycles >
eeprom.usec_delay_step)
609 eeprom.usec_delay_writecycles -=
eeprom.usec_delay_step;
611 if (
eeprom.usec_delay_step > 1)
613 eeprom.usec_delay_step /= 2;
620 if (
eeprom.adapt_state > -100)
645 }
while( written < count && ( p %
eeprom.sequential_write_pagesize ));
654 if (written == 0 && p >=
eeprom.size){
663 static int eeprom_close(
struct inode * inode,
struct file * file)
671 static int eeprom_address(
unsigned long addr)
676 page = (
unsigned char) (addr >> 8);
677 offset = (
unsigned char) addr;
720 if(
eeprom.retry_cnt_addr == EEPROM_RETRIES)
730 static int read_from_eeprom(
char * buf,
int count)
747 if(i == EEPROM_RETRIES)
755 while( (read < count))
783 #define ax_printf printk
784 static void eeprom_disable_write_protect(
void)