eeprom.c 22 KB

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