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