1 /******************************************************************************
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
8 *! 8k or not is detected (the assumes 2k or 16k)
9 *! 2k or 16k detected using test reads and writes.
11 *!------------------------------------------------------------------------
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
23 *! Revision 1.12 2003/04/09 08:31:14 pkj
24 *! Typo correction (taken from Linux 2.5).
26 *! Revision 1.11 2003/02/12 20:43:46 johana
27 *! Previous checkin removed beginning of comment.
29 *! Revision 1.10 2003/02/10 07:18:20 starvik
30 *! Removed misplaced ;
32 *! Revision 1.9 2003/01/22 06:54:46 starvik
33 *! Fixed warnings issued by GCC 3.2.1
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.
41 *! Revision 1.7 2001/06/14 15:26:54 jonashg
42 *! Removed test because condition is always true.
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).
47 *! Revision 1.5 2001/06/14 14:39:51 jonashg
48 *! Forgot to use name when registering the driver.
50 *! Revision 1.4 2001/06/14 14:35:47 jonashg
51 *! * Gave driver a name and used it in printk's.
54 *! Revision 1.3 2001/03/19 16:04:46 markusl
55 *! Fixed init of fops struct
57 *! Revision 1.2 2001/03/19 10:35:07 markusl
58 *! 2.4 port of eeprom driver
60 *! Revision 1.8 2000/05/18 10:42:25 edgar
61 *! Make sure to end write cycle on _every_ write
63 *! Revision 1.7 2000/01/17 17:41:01 johana
64 *! Adjusted probing and return -ENOSPC when writing outside EEPROM
66 *! Revision 1.6 2000/01/17 15:50:36 johana
67 *! Added adaptive timing adjustments and fixed autoprobing for 2k and 16k(?)
70 *! Revision 1.5 1999/09/03 15:07:37 edgar
71 *! Added bail-out check to the spinlock
73 *! Revision 1.4 1999/09/03 12:11:17 bjornw
74 *! Proper atomicity (need to use spinlocks, not if's). users -> busy.
77 *! (c) 1999 Axis Communications AB, Lund, Sweden
78 *!*****************************************************************************/
80 #include <linux/config.h>
81 #include <linux/kernel.h>
82 #include <linux/sched.h>
84 #include <linux/init.h>
85 #include <linux/delay.h>
86 #include <asm/uaccess.h>
91 /* If we should use adaptive timing or not: */
92 //#define EEPROM_ADAPTIVE_TIMING
94 #define EEPROM_MAJOR_NR 122 /* use a LOCAL/EXPERIMENTAL major for now */
95 #define EEPROM_MINOR_NR 0
97 /* Empirical sane initial value of the delay, the value will be adapted to
98 * what the chip needs when using EEPROM_ADAPTIVE_TIMING.
100 #define INITIAL_WRITEDELAY_US 4000
101 #define MAX_WRITEDELAY_US 10000 /* 10 ms according to spec for 2KB EEPROM */
103 /* This one defines how many times to try when eeprom fails. */
104 #define EEPROM_RETRIES 10
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)
111 #define i2c_delay(x) udelay(x)
114 * This structure describes the attached eeprom chip.
115 * The values are probed for.
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 */
128 /* this one is to keep the read/write operations atomic */
129 wait_queue_head_t wait_q;
131 int retry_cnt_addr; /* Used to keep track of number of retries for
132 adaptive timing adjustments */
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,
140 static ssize_t eeprom_write(struct file * file, const char * buf, size_t count,
142 static int eeprom_close(struct inode * inode, struct file * file);
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);
149 static void eeprom_disable_write_protect(void);
152 static const char eeprom_name[] = "eeprom";
154 /* chip description */
155 static struct eeprom_type eeprom;
157 /* This is the exported file-operations structure for this device. */
158 struct file_operations eeprom_fops =
160 llseek: eeprom_lseek,
164 release: eeprom_close
167 /* eeprom init call. Probes for different eeprom models. */
169 int __init eeprom_init(void)
171 init_waitqueue_head(&eeprom.wait_q);
174 #if CONFIG_ETRAX_I2C_EEPROM_PROBE
175 #define EETEXT "Found"
177 #define EETEXT "Assuming"
179 if (register_chrdev(EEPROM_MAJOR_NR, eeprom_name, &eeprom_fops))
181 printk(KERN_INFO "%s: unable to get major %d for eeprom device\n",
182 eeprom_name, EEPROM_MAJOR_NR);
186 printk("EEPROM char device v0.3, (c) 2000 Axis Communications AB\n");
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-
198 eeprom.usec_delay_writecycles = INITIAL_WRITEDELAY_US;
199 eeprom.usec_delay_step = 128;
200 eeprom.adapt_state = 0;
202 #if CONFIG_ETRAX_I2C_EEPROM_PROBE
209 unsigned char buf_2k_start[16];
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.
223 #define LOC2 (0x1fb) /*1fb, 3ed, 5df, 7d1 */
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 )
232 D(printk("2k start: '%16.16s'\n", buf_2k_start));
236 printk(KERN_INFO "%s: Failed to read in 2k mode!\n", eeprom_name);
240 eeprom.size = EEPROM_16KB;
241 eeprom.select_cmd = 0xA0;
242 eeprom.sequential_write_pagesize = 64;
245 unsigned char loc1[4], loc2[4], tmp[4];
246 if( eeprom_read_buf(LOC2, loc2, 4) == 4)
248 if( eeprom_read_buf(LOC1, loc1, 4) == 4)
250 D(printk("0 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
251 LOC1, loc1, LOC2, loc2));
253 if (memcmp(loc1, loc2, 4) != 0 )
256 printk(KERN_INFO "%s: 16k detected in step 1\n", eeprom_name);
257 eeprom.size = EEPROM_16KB;
263 /* Do step 2 check */
266 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
268 /* If 2k EEPROM this write will actually write 10 bytes
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)
275 D(printk("2 loc1: (%i) '%4.4s' tmp '%4.4s'\n",
277 if (memcmp(loc1, tmp, 4) != 0 )
279 printk(KERN_INFO "%s: read and write differs! Not 16kB\n",
283 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
289 printk(KERN_INFO "%s: Restore 2k failed during probe,"
290 " EEPROM might be corrupt!\n", eeprom_name);
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)
303 printk(KERN_INFO "%s: Failed to write back 2k start!\n",
307 eeprom.size = EEPROM_2KB;
313 if( eeprom_read_buf(LOC2, loc2, 1) == 1)
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 )
319 /* Data the same, must be mirrored -> 2k */
321 printk(KERN_INFO "%s: 2k detected in step 2\n", eeprom_name);
323 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
329 printk(KERN_INFO "%s: Restore 2k failed during probe,"
330 " EEPROM might be corrupt!\n", eeprom_name);
334 eeprom.size = EEPROM_2KB;
338 printk(KERN_INFO "%s: 16k detected in step 2\n",
341 /* Data differs, assume 16k */
343 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
349 printk(KERN_INFO "%s: Restore 16k failed during probe,"
350 " EEPROM might be corrupt!\n", eeprom_name);
353 eeprom.size = EEPROM_16KB;
362 printk(KERN_INFO "%s: Probing failed!, using 2KB!\n", eeprom_name);
363 eeprom.size = EEPROM_2KB;
374 eeprom.size = EEPROM_2KB;
382 eeprom.size = EEPROM_2KB;
388 eeprom.size = EEPROM_8KB;
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;
404 printk("%s: " EETEXT " i2c compatible 2kB eeprom.\n", eeprom_name);
405 eeprom.sequential_write_pagesize = 16;
406 eeprom.select_cmd = 0xA0;
409 printk("%s: " EETEXT " i2c compatible 8kB eeprom.\n", eeprom_name);
410 eeprom.sequential_write_pagesize = 16;
411 eeprom.select_cmd = 0x80;
414 printk("%s: " EETEXT " i2c compatible 16kB eeprom.\n", eeprom_name);
415 eeprom.sequential_write_pagesize = 64;
416 eeprom.select_cmd = 0xA0;
420 printk("%s: Did not find a supported eeprom\n", eeprom_name);
426 eeprom_disable_write_protect();
431 /* Opens the device. */
433 static int eeprom_open(struct inode * inode, struct file * file)
436 if(MINOR(inode->i_rdev) != EEPROM_MINOR_NR)
438 if(MAJOR(inode->i_rdev) != EEPROM_MAJOR_NR)
441 if( eeprom.size > 0 )
447 /* No EEprom found */
451 /* Changes the current file position. */
453 static loff_t eeprom_lseek(struct file * file, loff_t offset, int orig)
456 * orig 0: position from begning of eeprom
457 * orig 1: relative from current position
458 * orig 2: position from last eeprom address
464 file->f_pos = offset;
467 file->f_pos += offset;
470 file->f_pos = eeprom.size - offset;
476 /* truncate position */
483 if (file->f_pos >= eeprom.size)
485 file->f_pos = eeprom.size - 1;
489 return ( file->f_pos );
492 /* Reads data from eeprom. */
494 static int eeprom_read_buf(loff_t addr, char * buf, int count)
499 return eeprom_read(&f, buf, count, &addr);
504 /* Reads data from eeprom. */
506 static ssize_t eeprom_read(struct file * file, char * buf, size_t count, loff_t *off)
509 unsigned long p = *off;
513 if(p >= eeprom.size) /* Address i 0 - (size-1) */
520 interruptible_sleep_on(&eeprom.wait_q);
522 /* bail out if we get interrupted */
523 if (signal_pending(current))
529 page = (unsigned char) (p >> 8);
531 if(!eeprom_address(p))
533 printk(KERN_INFO "%s: Read failed to address the eeprom: "
534 "0x%08lX (%li) page: %i\n", eeprom_name, p, p, page);
537 /* don't forget to wake them up */
539 wake_up_interruptible(&eeprom.wait_q);
543 if(count > eeprom.size - p)
546 count = eeprom.size - p;
549 /* stop dummy write op and initiate the read op */
552 /* special case for small eeproms */
553 if(eeprom.size < EEPROM_16KB)
555 i2c_outbyte( eeprom.select_cmd | 1 | (page << 1) );
558 /* go on with the actual read */
559 read = read_from_eeprom( buf, count);
567 wake_up_interruptible(&eeprom.wait_q);
571 /* Writes data to eeprom. */
573 static int eeprom_write_buf(loff_t addr, const char * buf, int count)
579 return eeprom_write(&f, buf, count, &addr);
583 /* Writes data to eeprom. */
585 static ssize_t eeprom_write(struct file * file, const char * buf, size_t count,
588 int i, written, restart=1;
591 if (verify_area(VERIFY_READ, buf, count))
598 interruptible_sleep_on(&eeprom.wait_q);
599 /* bail out if we get interrupted */
600 if (signal_pending(current))
604 for(i = 0; (i < EEPROM_RETRIES) && (restart > 0); i++)
611 while( (written < count) && (p < eeprom.size))
613 /* address the eeprom */
614 if(!eeprom_address(p))
616 printk(KERN_INFO "%s: Write failed to address the eeprom: "
617 "0x%08lX (%li) \n", eeprom_name, p, p);
620 /* don't forget to wake them up */
622 wake_up_interruptible(&eeprom.wait_q);
625 #ifdef EEPROM_ADAPTIVE_TIMING
626 /* Adaptive algorithm to adjust timing */
627 if (eeprom.retry_cnt_addr > 0)
630 D(printk(">D=%i d=%i\n",
631 eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
633 if (eeprom.usec_delay_step < 4)
635 eeprom.usec_delay_step++;
636 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
641 if (eeprom.adapt_state > 0)
644 eeprom.usec_delay_step *= 2;
645 if (eeprom.usec_delay_step > 2)
647 eeprom.usec_delay_step--;
649 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
651 else if (eeprom.adapt_state < 0)
653 /* To High before (toggle dir) */
654 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
655 if (eeprom.usec_delay_step > 1)
657 eeprom.usec_delay_step /= 2;
658 eeprom.usec_delay_step--;
663 eeprom.adapt_state = 1;
667 /* To High (or good) now */
668 D(printk("<D=%i d=%i\n",
669 eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
671 if (eeprom.adapt_state < 0)
674 if (eeprom.usec_delay_step > 1)
676 eeprom.usec_delay_step *= 2;
677 eeprom.usec_delay_step--;
679 if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
681 eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
685 else if (eeprom.adapt_state > 0)
687 /* To Low before (toggle dir) */
688 if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
690 eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
692 if (eeprom.usec_delay_step > 1)
694 eeprom.usec_delay_step /= 2;
695 eeprom.usec_delay_step--;
698 eeprom.adapt_state = -1;
701 if (eeprom.adapt_state > -100)
703 eeprom.adapt_state--;
707 /* Restart adaption */
708 D(printk("#Restart\n"));
709 eeprom.usec_delay_step++;
712 #endif /* EEPROM_ADAPTIVE_TIMING */
713 /* write until we hit a page boundary or count */
716 i2c_outbyte(buf[written]);
720 printk(KERN_INFO "%s: write error, retrying. %d\n", eeprom_name, i);
726 } while( written < count && ( p % eeprom.sequential_write_pagesize ));
728 /* end write cycle */
730 i2c_delay(eeprom.usec_delay_writecycles);
735 wake_up_interruptible(&eeprom.wait_q);
736 if (written == 0 && p >= eeprom.size){
743 /* Closes the device. */
745 static int eeprom_close(struct inode * inode, struct file * file)
747 /* do nothing for now */
751 /* Sets the current address of the eeprom. */
753 static int eeprom_address(unsigned long addr)
756 unsigned char page, offset;
758 page = (unsigned char) (addr >> 8);
759 offset = (unsigned char) addr;
761 for(i = 0; i < EEPROM_RETRIES; i++)
763 /* start a dummy write for addressing */
766 if(eeprom.size == EEPROM_16KB)
768 i2c_outbyte( eeprom.select_cmd );
774 i2c_outbyte( eeprom.select_cmd | (page << 1) );
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 */
800 eeprom.retry_cnt_addr = i;
801 D(printk("%i\n", eeprom.retry_cnt_addr));
802 if(eeprom.retry_cnt_addr == EEPROM_RETRIES)
810 /* Reads from current address. */
812 static int read_from_eeprom(char * buf, int count)
816 for(i = 0; i < EEPROM_RETRIES; i++)
818 if(eeprom.size == EEPROM_16KB)
820 i2c_outbyte( eeprom.select_cmd | 1 );
829 if(i == EEPROM_RETRIES)
831 printk(KERN_INFO "%s: failed to read from eeprom\n", eeprom_name);
837 while( (read < count))
839 if (put_user(i2c_inbyte(), &buf[read++]))
847 * make sure we don't ack last byte or you will get very strange
856 /* stop the operation */
862 /* Disables write protection if applicable. */
865 #define ax_printf printk
866 static void eeprom_disable_write_protect(void)
868 /* Disable write protect */
869 if (eeprom.size == EEPROM_8KB)
871 /* Step 1 Set WEL = 1 (write 00000010 to address 1FFFh */
876 DBP_SAVE(ax_printf("Get ack returns false\n"));
881 DBP_SAVE(ax_printf("Get ack returns false 2\n"));
886 DBP_SAVE(ax_printf("Get ack returns false 3\n"));
892 /* Step 2 Set RWEL = 1 (write 00000110 to address 1FFFh */
897 DBP_SAVE(ax_printf("Get ack returns false 55\n"));
902 DBP_SAVE(ax_printf("Get ack returns false 52\n"));
907 DBP_SAVE(ax_printf("Get ack returns false 53\n"));
911 /* Step 3 Set BP1, BP0, and/or WPEN bits (write 00000110 to address 1FFFh */
916 DBP_SAVE(ax_printf("Get ack returns false 56\n"));
921 DBP_SAVE(ax_printf("Get ack returns false 57\n"));
926 DBP_SAVE(ax_printf("Get ack returns false 58\n"));
930 /* Write protect disabled */
934 module_init(eeprom_init);