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