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