eeprom.c 24 KB

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