eeprom.c 24 KB

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