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. /* BKL not needed: no global resources accessed */
  337. static int eeprom_open(struct inode * inode, struct file * file)
  338. {
  339. if(iminor(inode) != EEPROM_MINOR_NR)
  340. return -ENXIO;
  341. if(imajor(inode) != EEPROM_MAJOR_NR)
  342. return -ENXIO;
  343. if( eeprom.size > 0 )
  344. {
  345. /* OK */
  346. return 0;
  347. }
  348. /* No EEprom found */
  349. return -EFAULT;
  350. }
  351. /* Changes the current file position. */
  352. static loff_t eeprom_lseek(struct file * file, loff_t offset, int orig)
  353. {
  354. /*
  355. * orig 0: position from begning of eeprom
  356. * orig 1: relative from current position
  357. * orig 2: position from last eeprom address
  358. */
  359. switch (orig)
  360. {
  361. case 0:
  362. file->f_pos = offset;
  363. break;
  364. case 1:
  365. file->f_pos += offset;
  366. break;
  367. case 2:
  368. file->f_pos = eeprom.size - offset;
  369. break;
  370. default:
  371. return -EINVAL;
  372. }
  373. /* truncate position */
  374. if (file->f_pos < 0)
  375. {
  376. file->f_pos = 0;
  377. return(-EOVERFLOW);
  378. }
  379. if (file->f_pos >= eeprom.size)
  380. {
  381. file->f_pos = eeprom.size - 1;
  382. return(-EOVERFLOW);
  383. }
  384. return ( file->f_pos );
  385. }
  386. /* Reads data from eeprom. */
  387. static int eeprom_read_buf(loff_t addr, char * buf, int count)
  388. {
  389. struct file f;
  390. f.f_pos = addr;
  391. return eeprom_read(&f, buf, count, &addr);
  392. }
  393. /* Reads data from eeprom. */
  394. static ssize_t eeprom_read(struct file * file, char * buf, size_t count, loff_t *off)
  395. {
  396. int read=0;
  397. unsigned long p = file->f_pos;
  398. unsigned char page;
  399. if(p >= eeprom.size) /* Address i 0 - (size-1) */
  400. {
  401. return -EFAULT;
  402. }
  403. wait_event_interruptible(eeprom.wait_q, !eeprom.busy);
  404. if (signal_pending(current))
  405. return -EINTR;
  406. eeprom.busy++;
  407. page = (unsigned char) (p >> 8);
  408. if(!eeprom_address(p))
  409. {
  410. printk(KERN_INFO "%s: Read failed to address the eeprom: "
  411. "0x%08X (%i) page: %i\n", eeprom_name, (int)p, (int)p, page);
  412. i2c_stop();
  413. /* don't forget to wake them up */
  414. eeprom.busy--;
  415. wake_up_interruptible(&eeprom.wait_q);
  416. return -EFAULT;
  417. }
  418. if( (p + count) > eeprom.size)
  419. {
  420. /* truncate count */
  421. count = eeprom.size - p;
  422. }
  423. /* stop dummy write op and initiate the read op */
  424. i2c_start();
  425. /* special case for small eeproms */
  426. if(eeprom.size < EEPROM_16KB)
  427. {
  428. i2c_outbyte( eeprom.select_cmd | 1 | (page << 1) );
  429. }
  430. /* go on with the actual read */
  431. read = read_from_eeprom( buf, count);
  432. if(read > 0)
  433. {
  434. file->f_pos += read;
  435. }
  436. eeprom.busy--;
  437. wake_up_interruptible(&eeprom.wait_q);
  438. return read;
  439. }
  440. /* Writes data to eeprom. */
  441. static int eeprom_write_buf(loff_t addr, const char * buf, int count)
  442. {
  443. struct file f;
  444. f.f_pos = addr;
  445. return eeprom_write(&f, buf, count, &addr);
  446. }
  447. /* Writes data to eeprom. */
  448. static ssize_t eeprom_write(struct file * file, const char * buf, size_t count,
  449. loff_t *off)
  450. {
  451. int i, written, restart=1;
  452. unsigned long p;
  453. if (!access_ok(VERIFY_READ, buf, count))
  454. {
  455. return -EFAULT;
  456. }
  457. wait_event_interruptible(eeprom.wait_q, !eeprom.busy);
  458. /* bail out if we get interrupted */
  459. if (signal_pending(current))
  460. return -EINTR;
  461. eeprom.busy++;
  462. for(i = 0; (i < EEPROM_RETRIES) && (restart > 0); i++)
  463. {
  464. restart = 0;
  465. written = 0;
  466. p = file->f_pos;
  467. while( (written < count) && (p < eeprom.size))
  468. {
  469. /* address the eeprom */
  470. if(!eeprom_address(p))
  471. {
  472. printk(KERN_INFO "%s: Write failed to address the eeprom: "
  473. "0x%08X (%i) \n", eeprom_name, (int)p, (int)p);
  474. i2c_stop();
  475. /* don't forget to wake them up */
  476. eeprom.busy--;
  477. wake_up_interruptible(&eeprom.wait_q);
  478. return -EFAULT;
  479. }
  480. #ifdef EEPROM_ADAPTIVE_TIMING
  481. /* Adaptive algorithm to adjust timing */
  482. if (eeprom.retry_cnt_addr > 0)
  483. {
  484. /* To Low now */
  485. D(printk(">D=%i d=%i\n",
  486. eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
  487. if (eeprom.usec_delay_step < 4)
  488. {
  489. eeprom.usec_delay_step++;
  490. eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
  491. }
  492. else
  493. {
  494. if (eeprom.adapt_state > 0)
  495. {
  496. /* To Low before */
  497. eeprom.usec_delay_step *= 2;
  498. if (eeprom.usec_delay_step > 2)
  499. {
  500. eeprom.usec_delay_step--;
  501. }
  502. eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
  503. }
  504. else if (eeprom.adapt_state < 0)
  505. {
  506. /* To High before (toggle dir) */
  507. eeprom.usec_delay_writecycles += eeprom.usec_delay_step;
  508. if (eeprom.usec_delay_step > 1)
  509. {
  510. eeprom.usec_delay_step /= 2;
  511. eeprom.usec_delay_step--;
  512. }
  513. }
  514. }
  515. eeprom.adapt_state = 1;
  516. }
  517. else
  518. {
  519. /* To High (or good) now */
  520. D(printk("<D=%i d=%i\n",
  521. eeprom.usec_delay_writecycles, eeprom.usec_delay_step));
  522. if (eeprom.adapt_state < 0)
  523. {
  524. /* To High before */
  525. if (eeprom.usec_delay_step > 1)
  526. {
  527. eeprom.usec_delay_step *= 2;
  528. eeprom.usec_delay_step--;
  529. if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
  530. {
  531. eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
  532. }
  533. }
  534. }
  535. else if (eeprom.adapt_state > 0)
  536. {
  537. /* To Low before (toggle dir) */
  538. if (eeprom.usec_delay_writecycles > eeprom.usec_delay_step)
  539. {
  540. eeprom.usec_delay_writecycles -= eeprom.usec_delay_step;
  541. }
  542. if (eeprom.usec_delay_step > 1)
  543. {
  544. eeprom.usec_delay_step /= 2;
  545. eeprom.usec_delay_step--;
  546. }
  547. eeprom.adapt_state = -1;
  548. }
  549. if (eeprom.adapt_state > -100)
  550. {
  551. eeprom.adapt_state--;
  552. }
  553. else
  554. {
  555. /* Restart adaption */
  556. D(printk("#Restart\n"));
  557. eeprom.usec_delay_step++;
  558. }
  559. }
  560. #endif /* EEPROM_ADAPTIVE_TIMING */
  561. /* write until we hit a page boundary or count */
  562. do
  563. {
  564. i2c_outbyte(buf[written]);
  565. if(!i2c_getack())
  566. {
  567. restart=1;
  568. printk(KERN_INFO "%s: write error, retrying. %d\n", eeprom_name, i);
  569. i2c_stop();
  570. break;
  571. }
  572. written++;
  573. p++;
  574. } while( written < count && ( p % eeprom.sequential_write_pagesize ));
  575. /* end write cycle */
  576. i2c_stop();
  577. i2c_delay(eeprom.usec_delay_writecycles);
  578. } /* while */
  579. } /* for */
  580. eeprom.busy--;
  581. wake_up_interruptible(&eeprom.wait_q);
  582. if (written == 0 && file->f_pos >= eeprom.size){
  583. return -ENOSPC;
  584. }
  585. file->f_pos += written;
  586. return written;
  587. }
  588. /* Closes the device. */
  589. static int eeprom_close(struct inode * inode, struct file * file)
  590. {
  591. /* do nothing for now */
  592. return 0;
  593. }
  594. /* Sets the current address of the eeprom. */
  595. static int eeprom_address(unsigned long addr)
  596. {
  597. int i;
  598. unsigned char page, offset;
  599. page = (unsigned char) (addr >> 8);
  600. offset = (unsigned char) addr;
  601. for(i = 0; i < EEPROM_RETRIES; i++)
  602. {
  603. /* start a dummy write for addressing */
  604. i2c_start();
  605. if(eeprom.size == EEPROM_16KB)
  606. {
  607. i2c_outbyte( eeprom.select_cmd );
  608. i2c_getack();
  609. i2c_outbyte(page);
  610. }
  611. else
  612. {
  613. i2c_outbyte( eeprom.select_cmd | (page << 1) );
  614. }
  615. if(!i2c_getack())
  616. {
  617. /* retry */
  618. i2c_stop();
  619. /* Must have a delay here.. 500 works, >50, 100->works 5th time*/
  620. i2c_delay(MAX_WRITEDELAY_US / EEPROM_RETRIES * i);
  621. /* The chip needs up to 10 ms from write stop to next start */
  622. }
  623. else
  624. {
  625. i2c_outbyte(offset);
  626. if(!i2c_getack())
  627. {
  628. /* retry */
  629. i2c_stop();
  630. }
  631. else
  632. break;
  633. }
  634. }
  635. eeprom.retry_cnt_addr = i;
  636. D(printk("%i\n", eeprom.retry_cnt_addr));
  637. if(eeprom.retry_cnt_addr == EEPROM_RETRIES)
  638. {
  639. /* failed */
  640. return 0;
  641. }
  642. return 1;
  643. }
  644. /* Reads from current address. */
  645. static int read_from_eeprom(char * buf, int count)
  646. {
  647. int i, read=0;
  648. for(i = 0; i < EEPROM_RETRIES; i++)
  649. {
  650. if(eeprom.size == EEPROM_16KB)
  651. {
  652. i2c_outbyte( eeprom.select_cmd | 1 );
  653. }
  654. if(i2c_getack())
  655. {
  656. break;
  657. }
  658. }
  659. if(i == EEPROM_RETRIES)
  660. {
  661. printk(KERN_INFO "%s: failed to read from eeprom\n", eeprom_name);
  662. i2c_stop();
  663. return -EFAULT;
  664. }
  665. while( (read < count))
  666. {
  667. if (put_user(i2c_inbyte(), &buf[read++]))
  668. {
  669. i2c_stop();
  670. return -EFAULT;
  671. }
  672. /*
  673. * make sure we don't ack last byte or you will get very strange
  674. * results!
  675. */
  676. if(read < count)
  677. {
  678. i2c_sendack();
  679. }
  680. }
  681. /* stop the operation */
  682. i2c_stop();
  683. return read;
  684. }
  685. /* Disables write protection if applicable. */
  686. #define DBP_SAVE(x)
  687. #define ax_printf printk
  688. static void eeprom_disable_write_protect(void)
  689. {
  690. /* Disable write protect */
  691. if (eeprom.size == EEPROM_8KB)
  692. {
  693. /* Step 1 Set WEL = 1 (write 00000010 to address 1FFFh */
  694. i2c_start();
  695. i2c_outbyte(0xbe);
  696. if(!i2c_getack())
  697. {
  698. DBP_SAVE(ax_printf("Get ack returns false\n"));
  699. }
  700. i2c_outbyte(0xFF);
  701. if(!i2c_getack())
  702. {
  703. DBP_SAVE(ax_printf("Get ack returns false 2\n"));
  704. }
  705. i2c_outbyte(0x02);
  706. if(!i2c_getack())
  707. {
  708. DBP_SAVE(ax_printf("Get ack returns false 3\n"));
  709. }
  710. i2c_stop();
  711. i2c_delay(1000);
  712. /* Step 2 Set RWEL = 1 (write 00000110 to address 1FFFh */
  713. i2c_start();
  714. i2c_outbyte(0xbe);
  715. if(!i2c_getack())
  716. {
  717. DBP_SAVE(ax_printf("Get ack returns false 55\n"));
  718. }
  719. i2c_outbyte(0xFF);
  720. if(!i2c_getack())
  721. {
  722. DBP_SAVE(ax_printf("Get ack returns false 52\n"));
  723. }
  724. i2c_outbyte(0x06);
  725. if(!i2c_getack())
  726. {
  727. DBP_SAVE(ax_printf("Get ack returns false 53\n"));
  728. }
  729. i2c_stop();
  730. /* Step 3 Set BP1, BP0, and/or WPEN bits (write 00000110 to address 1FFFh */
  731. i2c_start();
  732. i2c_outbyte(0xbe);
  733. if(!i2c_getack())
  734. {
  735. DBP_SAVE(ax_printf("Get ack returns false 56\n"));
  736. }
  737. i2c_outbyte(0xFF);
  738. if(!i2c_getack())
  739. {
  740. DBP_SAVE(ax_printf("Get ack returns false 57\n"));
  741. }
  742. i2c_outbyte(0x06);
  743. if(!i2c_getack())
  744. {
  745. DBP_SAVE(ax_printf("Get ack returns false 58\n"));
  746. }
  747. i2c_stop();
  748. /* Write protect disabled */
  749. }
  750. }
  751. module_init(eeprom_init);