1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96#include <linux/kernel.h>
97#include <linux/sched.h>
98#include <linux/fs.h>
99#include <linux/init.h>
100#include <linux/delay.h>
101#include <linux/interrupt.h>
102#include <linux/wait.h>
103#include <asm/uaccess.h>
104#include "i2c.h"
105
106#define D(x)
107
108
109
110
111#define EEPROM_MAJOR_NR 122
112#define EEPROM_MINOR_NR 0
113
114
115
116
117#define INITIAL_WRITEDELAY_US 4000
118#define MAX_WRITEDELAY_US 10000
119
120
121#define EEPROM_RETRIES 10
122
123#define EEPROM_2KB (2 * 1024)
124
125#define EEPROM_8KB (8 * 1024 - 1 )
126#define EEPROM_16KB (16 * 1024)
127
128#define i2c_delay(x) udelay(x)
129
130
131
132
133
134
135struct eeprom_type
136{
137 unsigned long size;
138 unsigned long sequential_write_pagesize;
139 unsigned char select_cmd;
140 unsigned long usec_delay_writecycles;
141
142 unsigned long usec_delay_step;
143 int adapt_state;
144
145
146 wait_queue_head_t wait_q;
147 volatile int busy;
148 int retry_cnt_addr;
149
150 int retry_cnt_read;
151};
152
153static int eeprom_open(struct inode * inode, struct file * file);
154static loff_t eeprom_lseek(struct file * file, loff_t offset, int orig);
155static ssize_t eeprom_read(struct file * file, char * buf, size_t count,
156 loff_t *off);
157static ssize_t eeprom_write(struct file * file, const char * buf, size_t count,
158 loff_t *off);
159static int eeprom_close(struct inode * inode, struct file * file);
160
161static int eeprom_address(unsigned long addr);
162static int read_from_eeprom(char * buf, int count);
163static int eeprom_write_buf(loff_t addr, const char * buf, int count);
164static int eeprom_read_buf(loff_t addr, char * buf, int count);
165
166static void eeprom_disable_write_protect(void);
167
168
169static const char eeprom_name[] = "eeprom";
170
171
172static struct eeprom_type eeprom;
173
174
175const struct file_operations eeprom_fops =
176{
177 .llseek = eeprom_lseek,
178 .read = eeprom_read,
179 .write = eeprom_write,
180 .open = eeprom_open,
181 .release = eeprom_close
182};
183
184
185
186int __init eeprom_init(void)
187{
188 init_waitqueue_head(&eeprom.wait_q);
189 eeprom.busy = 0;
190
191#ifdef CONFIG_ETRAX_I2C_EEPROM_PROBE
192#define EETEXT "Found"
193#else
194#define EETEXT "Assuming"
195#endif
196 if (register_chrdev(EEPROM_MAJOR_NR, eeprom_name, &eeprom_fops))
197 {
198 printk(KERN_INFO "%s: unable to get major %d for eeprom device\n",
199 eeprom_name, EEPROM_MAJOR_NR);
200 return -1;
201 }
202
203 printk("EEPROM char device v0.3, (c) 2000 Axis Communications AB\n");
204
205
206
207
208
209
210
211
212
213
214 eeprom.size = 0;
215 eeprom.usec_delay_writecycles = INITIAL_WRITEDELAY_US;
216 eeprom.usec_delay_step = 128;
217 eeprom.adapt_state = 0;
218
219#ifdef CONFIG_ETRAX_I2C_EEPROM_PROBE
220 i2c_start();
221 i2c_outbyte(0x80);
222 if(!i2c_getack())
223 {
224
225 int success = 0;
226 unsigned char buf_2k_start[16];
227
228
229
230
231
232
233
234
235
236
237
238
239#define LOC1 8
240#define LOC2 (0x1fb)
241
242
243 i2c_stop();
244 eeprom.size = EEPROM_2KB;
245 eeprom.select_cmd = 0xA0;
246 eeprom.sequential_write_pagesize = 16;
247 if( eeprom_read_buf( 0, buf_2k_start, 16 ) == 16 )
248 {
249 D(printk("2k start: '%16.16s'\n", buf_2k_start));
250 }
251 else
252 {
253 printk(KERN_INFO "%s: Failed to read in 2k mode!\n", eeprom_name);
254 }
255
256
257 eeprom.size = EEPROM_16KB;
258 eeprom.select_cmd = 0xA0;
259 eeprom.sequential_write_pagesize = 64;
260
261 {
262 unsigned char loc1[4], loc2[4], tmp[4];
263 if( eeprom_read_buf(LOC2, loc2, 4) == 4)
264 {
265 if( eeprom_read_buf(LOC1, loc1, 4) == 4)
266 {
267 D(printk("0 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
268 LOC1, loc1, LOC2, loc2));
269#if 0
270 if (memcmp(loc1, loc2, 4) != 0 )
271 {
272
273 printk(KERN_INFO "%s: 16k detected in step 1\n", eeprom_name);
274 eeprom.size = EEPROM_16KB;
275 success = 1;
276 }
277 else
278#endif
279 {
280
281
282 loc1[0] = ~loc1[0];
283 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
284 {
285
286
287
288 D(printk("1 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
289 LOC1, loc1, LOC2, loc2));
290 if( eeprom_read_buf(LOC1, tmp, 4) == 4)
291 {
292 D(printk("2 loc1: (%i) '%4.4s' tmp '%4.4s'\n",
293 LOC1, loc1, tmp));
294 if (memcmp(loc1, tmp, 4) != 0 )
295 {
296 printk(KERN_INFO "%s: read and write differs! Not 16kB\n",
297 eeprom_name);
298 loc1[0] = ~loc1[0];
299
300 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
301 {
302 success = 1;
303 }
304 else
305 {
306 printk(KERN_INFO "%s: Restore 2k failed during probe,"
307 " EEPROM might be corrupt!\n", eeprom_name);
308
309 }
310 i2c_stop();
311
312 eeprom.size = EEPROM_2KB;
313 eeprom.select_cmd = 0xA0;
314 eeprom.sequential_write_pagesize = 16;
315 if( eeprom_write_buf(0, buf_2k_start, 16) == 16)
316 {
317 }
318 else
319 {
320 printk(KERN_INFO "%s: Failed to write back 2k start!\n",
321 eeprom_name);
322 }
323
324 eeprom.size = EEPROM_2KB;
325 }
326 }
327
328 if(!success)
329 {
330 if( eeprom_read_buf(LOC2, loc2, 1) == 1)
331 {
332 D(printk("0 loc1: (%i) '%4.4s' loc2 (%i) '%4.4s'\n",
333 LOC1, loc1, LOC2, loc2));
334 if (memcmp(loc1, loc2, 4) == 0 )
335 {
336
337
338 printk(KERN_INFO "%s: 2k detected in step 2\n", eeprom_name);
339 loc1[0] = ~loc1[0];
340 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
341 {
342 success = 1;
343 }
344 else
345 {
346 printk(KERN_INFO "%s: Restore 2k failed during probe,"
347 " EEPROM might be corrupt!\n", eeprom_name);
348
349 }
350
351 eeprom.size = EEPROM_2KB;
352 }
353 else
354 {
355 printk(KERN_INFO "%s: 16k detected in step 2\n",
356 eeprom_name);
357 loc1[0] = ~loc1[0];
358
359
360 if (eeprom_write_buf(LOC1, loc1, 1) == 1)
361 {
362 success = 1;
363 }
364 else
365 {
366 printk(KERN_INFO "%s: Restore 16k failed during probe,"
367 " EEPROM might be corrupt!\n", eeprom_name);
368 }
369
370 eeprom.size = EEPROM_16KB;
371 }
372 }
373 }
374 }
375 }
376 }
377 if (!success)
378 {
379 printk(KERN_INFO "%s: Probing failed!, using 2KB!\n", eeprom_name);
380 eeprom.size = EEPROM_2KB;
381 }
382 }
383 }
384 }
385 else
386 {
387 i2c_outbyte(0x00);
388 if(!i2c_getack())
389 {
390
391 eeprom.size = EEPROM_2KB;
392 }
393 else
394 {
395 i2c_start();
396 i2c_outbyte(0x81);
397 if (!i2c_getack())
398 {
399 eeprom.size = EEPROM_2KB;
400 }
401 else
402 {
403
404 i2c_inbyte();
405 eeprom.size = EEPROM_8KB;
406 }
407 }
408 }
409 i2c_stop();
410#elif defined(CONFIG_ETRAX_I2C_EEPROM_16KB)
411 eeprom.size = EEPROM_16KB;
412#elif defined(CONFIG_ETRAX_I2C_EEPROM_8KB)
413 eeprom.size = EEPROM_8KB;
414#elif defined(CONFIG_ETRAX_I2C_EEPROM_2KB)
415 eeprom.size = EEPROM_2KB;
416#endif
417
418 switch(eeprom.size)
419 {
420 case (EEPROM_2KB):
421 printk("%s: " EETEXT " i2c compatible 2kB eeprom.\n", eeprom_name);
422 eeprom.sequential_write_pagesize = 16;
423 eeprom.select_cmd = 0xA0;
424 break;
425 case (EEPROM_8KB):
426 printk("%s: " EETEXT " i2c compatible 8kB eeprom.\n", eeprom_name);
427 eeprom.sequential_write_pagesize = 16;
428 eeprom.select_cmd = 0x80;
429 break;
430 case (EEPROM_16KB):
431 printk("%s: " EETEXT " i2c compatible 16kB eeprom.\n", eeprom_name);
432 eeprom.sequential_write_pagesize = 64;
433 eeprom.select_cmd = 0xA0;
434 break;
435 default:
436 eeprom.size = 0;
437 printk("%s: Did not find a supported eeprom\n", eeprom_name);
438 break;
439 }
440
441
442
443 eeprom_disable_write_protect();
444
445 return 0;
446}
447
448
449
450static int eeprom_open(struct inode * inode, struct file * file)
451{
452
453 if(iminor(inode) != EEPROM_MINOR_NR)
454 return -ENXIO;
455 if(imajor(inode) != EEPROM_MAJOR_NR)
456 return -ENXIO;
457
458 if( eeprom.size > 0 )
459 {
460
461 return 0;
462 }
463
464
465 return -EFAULT;
466}
467
468
469
470static loff_t eeprom_lseek(struct file * file, loff_t offset, int orig)
471{
472
473
474
475
476
477
478 switch (orig)
479 {
480 case 0:
481 file->f_pos = offset;
482 break;
483 case 1:
484 file->f_pos += offset;
485 break;
486 case 2:
487 file->f_pos = eeprom.size - offset;
488 break;
489 default:
490 return -EINVAL;
491 }
492
493
494 if (file->f_pos < 0)
495 {
496 file->f_pos = 0;
497 return(-EOVERFLOW);
498 }
499
500 if (file->f_pos >= eeprom.size)
501 {
502 file->f_pos = eeprom.size - 1;
503 return(-EOVERFLOW);
504 }
505
506 return ( file->f_pos );
507}
508
509
510
511static int eeprom_read_buf(loff_t addr, char * buf, int count)
512{
513 struct file f;
514
515 f.f_pos = addr;
516 return eeprom_read(&f, buf, count, &addr);
517}
518
519
520
521
522
523static ssize_t eeprom_read(struct file * file, char * buf, size_t count, loff_t *off)
524{
525 int read=0;
526 unsigned long p = file->f_pos;
527
528 unsigned char page;
529
530 if(p >= eeprom.size)
531 {
532 return -EFAULT;
533 }
534
535 wait_event_interruptible(eeprom.wait_q, !eeprom.busy);
536 if (signal_pending(current))
537 return -EINTR;
538
539 eeprom.busy++;
540
541 page = (unsigned char) (p >> 8);
542
543 if(!eeprom_address(p))
544 {
545 printk(KERN_INFO "%s: Read failed to address the eeprom: "
546 "0x%08X (%i) page: %i\n", eeprom_name, (int)p, (int)p, page);
547 i2c_stop();
548
549
550 eeprom.busy--;
551 wake_up_interruptible(&eeprom.wait_q);
552 return -EFAULT;
553 }
554
555 if( (p + count) > eeprom.size)
556 {
557
558 count = eeprom.size - p;
559 }
560
561
562 i2c_start();
563
564
565 if(eeprom.size < EEPROM_16KB)
566 {
567 i2c_outbyte( eeprom.select_cmd | 1 | (page << 1) );
568 }
569
570
571 read = read_from_eeprom( buf, count);
572
573 if(read > 0)
574 {
575 file->f_pos += read;
576 }
577
578 eeprom.busy--;
579 wake_up_interruptible(&eeprom.wait_q);
580 return read;
581}
582
583
584
585static int eeprom_write_buf(loff_t addr, const char * buf, int count)
586{
587 struct file f;
588
589 f.f_pos = addr;
590
591 return eeprom_write(&f, buf, count, &addr);
592}
593
594
595
596
597static ssize_t eeprom_write(struct file * file, const char * buf, size_t count,
598 loff_t *off)
599{
600 int i, written, restart=1;
601 unsigned long p;
602
603 if (!access_ok(VERIFY_READ, buf, count))
604 {
605 return -EFAULT;
606 }
607
608 wait_event_interruptible(eeprom.wait_q, !eeprom.busy);
609
610 if (signal_pending(current))
611 return -EINTR;
612 eeprom.busy++;
613 for(i = 0; (i < EEPROM_RETRIES) && (restart > 0); i++)
614 {
615 restart = 0;
616 written = 0;
617 p = file->f_pos;
618
619
620 while( (written < count) && (p < eeprom.size))
621 {
622
623 if(!eeprom_address(p))
624 {
625 printk(KERN_INFO "%s: Write failed to address the eeprom: "
626 "0x%08X (%i) \n", eeprom_name, (int)p, (int)p);
627 i2c_stop();
628
629
630 eeprom.busy--;
631 wake_up_interruptible(&eeprom.wait_q);
632 return -EFAULT;
633 }
634#ifdef EEPROM_ADAPTIVE_TIMING
635
636 if (eeprom.retry_cnt_addr > 0)
637 {
638
639 D(printk(">D=%i d=%i\n",
640 eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
641
642 if (eeprom.usec_delay_step < 4)
643 {
644 eeprom.usec_delay_step++;
645 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
646 }
647 else
648 {
649
650 if (eeprom.adapt_state > 0)
651 {
652
653 eeprom.usec_delay_step *= 2;
654 if (eeprom.usec_delay_step > 2)
655 {
656 eeprom.usec_delay_step--;
657 }
658 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
659 }
660 else if (eeprom.adapt_state < 0)
661 {
662
663 eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
664 if (eeprom.usec_delay_step > 1)
665 {
666 eeprom.usec_delay_step /= 2;
667 eeprom.usec_delay_step--;
668 }
669 }
670 }
671
672 eeprom.adapt_state = 1;
673 }
674 else
675 {
676
677 D(printk("<D=%i d=%i\n",
678 eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
679
680 if (eeprom.adapt_state < 0)
681 {
682
683 if (eeprom.usec_delay_step > 1)
684 {
685 eeprom.usec_delay_step *= 2;
686 eeprom.usec_delay_step--;
687
688 if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
689 {
690 eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
691 }
692 }
693 }
694 else if (eeprom.adapt_state > 0)
695 {
696
697 if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
698 {
699 eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
700 }
701 if (eeprom.usec_delay_step > 1)
702 {
703 eeprom.usec_delay_step /= 2;
704 eeprom.usec_delay_step--;
705 }
706
707 eeprom.adapt_state = -1;
708 }
709
710 if (eeprom.adapt_state > -100)
711 {
712 eeprom.adapt_state--;
713 }
714 else
715 {
716
717 D(printk("#Restart\n"));
718 eeprom.usec_delay_step++;
719 }
720 }
721#endif
722
723 do
724 {
725 i2c_outbyte(buf[written]);
726 if(!i2c_getack())
727 {
728 restart=1;
729 printk(KERN_INFO "%s: write error, retrying. %d\n", eeprom_name, i);
730 i2c_stop();
731 break;
732 }
733 written++;
734 p++;
735 } while( written < count && ( p % eeprom.sequential_write_pagesize ));
736
737
738 i2c_stop();
739 i2c_delay(eeprom.usec_delay_writecycles);
740 }
741 }
742
743 eeprom.busy--;
744 wake_up_interruptible(&eeprom.wait_q);
745 if (written == 0 && file->f_pos >= eeprom.size){
746 return -ENOSPC;
747 }
748 file->f_pos += written;
749 return written;
750}
751
752
753
754static int eeprom_close(struct inode * inode, struct file * file)
755{
756
757 return 0;
758}
759
760
761
762static int eeprom_address(unsigned long addr)
763{
764 int i;
765 unsigned char page, offset;
766
767 page = (unsigned char) (addr >> 8);
768 offset = (unsigned char) addr;
769
770 for(i = 0; i < EEPROM_RETRIES; i++)
771 {
772
773 i2c_start();
774
775 if(eeprom.size == EEPROM_16KB)
776 {
777 i2c_outbyte( eeprom.select_cmd );
778 i2c_getack();
779 i2c_outbyte(page);
780 }
781 else
782 {
783 i2c_outbyte( eeprom.select_cmd | (page << 1) );
784 }
785 if(!i2c_getack())
786 {
787
788 i2c_stop();
789
790 i2c_delay(MAX_WRITEDELAY_US / EEPROM_RETRIES * i);
791
792
793 }
794 else
795 {
796 i2c_outbyte(offset);
797
798 if(!i2c_getack())
799 {
800
801 i2c_stop();
802 }
803 else
804 break;
805 }
806 }
807
808
809 eeprom.retry_cnt_addr = i;
810 D(printk("%i\n", eeprom.retry_cnt_addr));
811 if(eeprom.retry_cnt_addr == EEPROM_RETRIES)
812 {
813
814 return 0;
815 }
816 return 1;
817}
818
819
820
821static int read_from_eeprom(char * buf, int count)
822{
823 int i, read=0;
824
825 for(i = 0; i < EEPROM_RETRIES; i++)
826 {
827 if(eeprom.size == EEPROM_16KB)
828 {
829 i2c_outbyte( eeprom.select_cmd | 1 );
830 }
831
832 if(i2c_getack())
833 {
834 break;
835 }
836 }
837
838 if(i == EEPROM_RETRIES)
839 {
840 printk(KERN_INFO "%s: failed to read from eeprom\n", eeprom_name);
841 i2c_stop();
842
843 return -EFAULT;
844 }
845
846 while( (read < count))
847 {
848 if (put_user(i2c_inbyte(), &buf[read++]))
849 {
850 i2c_stop();
851
852 return -EFAULT;
853 }
854
855
856
857
858
859 if(read < count)
860 {
861 i2c_sendack();
862 }
863 }
864
865
866 i2c_stop();
867
868 return read;
869}
870
871
872
873#define DBP_SAVE(x)
874#define ax_printf printk
875static void eeprom_disable_write_protect(void)
876{
877
878 if (eeprom.size == EEPROM_8KB)
879 {
880
881 i2c_start();
882 i2c_outbyte(0xbe);
883 if(!i2c_getack())
884 {
885 DBP_SAVE(ax_printf("Get ack returns false\n"));
886 }
887 i2c_outbyte(0xFF);
888 if(!i2c_getack())
889 {
890 DBP_SAVE(ax_printf("Get ack returns false 2\n"));
891 }
892 i2c_outbyte(0x02);
893 if(!i2c_getack())
894 {
895 DBP_SAVE(ax_printf("Get ack returns false 3\n"));
896 }
897 i2c_stop();
898
899 i2c_delay(1000);
900
901
902 i2c_start();
903 i2c_outbyte(0xbe);
904 if(!i2c_getack())
905 {
906 DBP_SAVE(ax_printf("Get ack returns false 55\n"));
907 }
908 i2c_outbyte(0xFF);
909 if(!i2c_getack())
910 {
911 DBP_SAVE(ax_printf("Get ack returns false 52\n"));
912 }
913 i2c_outbyte(0x06);
914 if(!i2c_getack())
915 {
916 DBP_SAVE(ax_printf("Get ack returns false 53\n"));
917 }
918 i2c_stop();
919
920
921 i2c_start();
922 i2c_outbyte(0xbe);
923 if(!i2c_getack())
924 {
925 DBP_SAVE(ax_printf("Get ack returns false 56\n"));
926 }
927 i2c_outbyte(0xFF);
928 if(!i2c_getack())
929 {
930 DBP_SAVE(ax_printf("Get ack returns false 57\n"));
931 }
932 i2c_outbyte(0x06);
933 if(!i2c_getack())
934 {
935 DBP_SAVE(ax_printf("Get ack returns false 58\n"));
936 }
937 i2c_stop();
938
939
940 }
941}
942
943module_init(eeprom_init);
944