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