eeprom.c 22 KB

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