w1_ds2431.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. /*
  2. * w1_ds2431.c - w1 family 2d (DS2431) driver
  3. *
  4. * Copyright (c) 2008 Bernhard Weirich <bernhard.weirich@riedel.net>
  5. *
  6. * Heavily inspired by w1_DS2433 driver from Ben Gardner <bgardner@wabtec.com>
  7. *
  8. * This source code is licensed under the GNU General Public License,
  9. * Version 2. See the file COPYING for more details.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/moduleparam.h>
  14. #include <linux/device.h>
  15. #include <linux/types.h>
  16. #include <linux/delay.h>
  17. #include <linux/w1.h>
  18. #define W1_EEPROM_DS2431 0x2D
  19. #define W1_F2D_EEPROM_SIZE 128
  20. #define W1_F2D_PAGE_COUNT 4
  21. #define W1_F2D_PAGE_BITS 5
  22. #define W1_F2D_PAGE_SIZE (1<<W1_F2D_PAGE_BITS)
  23. #define W1_F2D_PAGE_MASK 0x1F
  24. #define W1_F2D_SCRATCH_BITS 3
  25. #define W1_F2D_SCRATCH_SIZE (1<<W1_F2D_SCRATCH_BITS)
  26. #define W1_F2D_SCRATCH_MASK (W1_F2D_SCRATCH_SIZE-1)
  27. #define W1_F2D_READ_EEPROM 0xF0
  28. #define W1_F2D_WRITE_SCRATCH 0x0F
  29. #define W1_F2D_READ_SCRATCH 0xAA
  30. #define W1_F2D_COPY_SCRATCH 0x55
  31. #define W1_F2D_TPROG_MS 11
  32. #define W1_F2D_READ_RETRIES 10
  33. #define W1_F2D_READ_MAXLEN 8
  34. /*
  35. * Check the file size bounds and adjusts count as needed.
  36. * This would not be needed if the file size didn't reset to 0 after a write.
  37. */
  38. static inline size_t w1_f2d_fix_count(loff_t off, size_t count, size_t size)
  39. {
  40. if (off > size)
  41. return 0;
  42. if ((off + count) > size)
  43. return size - off;
  44. return count;
  45. }
  46. /*
  47. * Read a block from W1 ROM two times and compares the results.
  48. * If they are equal they are returned, otherwise the read
  49. * is repeated W1_F2D_READ_RETRIES times.
  50. *
  51. * count must not exceed W1_F2D_READ_MAXLEN.
  52. */
  53. static int w1_f2d_readblock(struct w1_slave *sl, int off, int count, char *buf)
  54. {
  55. u8 wrbuf[3];
  56. u8 cmp[W1_F2D_READ_MAXLEN];
  57. int tries = W1_F2D_READ_RETRIES;
  58. do {
  59. wrbuf[0] = W1_F2D_READ_EEPROM;
  60. wrbuf[1] = off & 0xff;
  61. wrbuf[2] = off >> 8;
  62. if (w1_reset_select_slave(sl))
  63. return -1;
  64. w1_write_block(sl->master, wrbuf, 3);
  65. w1_read_block(sl->master, buf, count);
  66. if (w1_reset_select_slave(sl))
  67. return -1;
  68. w1_write_block(sl->master, wrbuf, 3);
  69. w1_read_block(sl->master, cmp, count);
  70. if (!memcmp(cmp, buf, count))
  71. return 0;
  72. } while (--tries);
  73. dev_err(&sl->dev, "proof reading failed %d times\n",
  74. W1_F2D_READ_RETRIES);
  75. return -1;
  76. }
  77. static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
  78. struct bin_attribute *bin_attr, char *buf,
  79. loff_t off, size_t count)
  80. {
  81. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  82. int todo = count;
  83. count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
  84. if (count == 0)
  85. return 0;
  86. mutex_lock(&sl->master->bus_mutex);
  87. /* read directly from the EEPROM in chunks of W1_F2D_READ_MAXLEN */
  88. while (todo > 0) {
  89. int block_read;
  90. if (todo >= W1_F2D_READ_MAXLEN)
  91. block_read = W1_F2D_READ_MAXLEN;
  92. else
  93. block_read = todo;
  94. if (w1_f2d_readblock(sl, off, block_read, buf) < 0)
  95. count = -EIO;
  96. todo -= W1_F2D_READ_MAXLEN;
  97. buf += W1_F2D_READ_MAXLEN;
  98. off += W1_F2D_READ_MAXLEN;
  99. }
  100. mutex_unlock(&sl->master->bus_mutex);
  101. return count;
  102. }
  103. /*
  104. * Writes to the scratchpad and reads it back for verification.
  105. * Then copies the scratchpad to EEPROM.
  106. * The data must be aligned at W1_F2D_SCRATCH_SIZE bytes and
  107. * must be W1_F2D_SCRATCH_SIZE bytes long.
  108. * The master must be locked.
  109. *
  110. * @param sl The slave structure
  111. * @param addr Address for the write
  112. * @param len length must be <= (W1_F2D_PAGE_SIZE - (addr & W1_F2D_PAGE_MASK))
  113. * @param data The data to write
  114. * @return 0=Success -1=failure
  115. */
  116. static int w1_f2d_write(struct w1_slave *sl, int addr, int len, const u8 *data)
  117. {
  118. int tries = W1_F2D_READ_RETRIES;
  119. u8 wrbuf[4];
  120. u8 rdbuf[W1_F2D_SCRATCH_SIZE + 3];
  121. u8 es = (addr + len - 1) % W1_F2D_SCRATCH_SIZE;
  122. retry:
  123. /* Write the data to the scratchpad */
  124. if (w1_reset_select_slave(sl))
  125. return -1;
  126. wrbuf[0] = W1_F2D_WRITE_SCRATCH;
  127. wrbuf[1] = addr & 0xff;
  128. wrbuf[2] = addr >> 8;
  129. w1_write_block(sl->master, wrbuf, 3);
  130. w1_write_block(sl->master, data, len);
  131. /* Read the scratchpad and verify */
  132. if (w1_reset_select_slave(sl))
  133. return -1;
  134. w1_write_8(sl->master, W1_F2D_READ_SCRATCH);
  135. w1_read_block(sl->master, rdbuf, len + 3);
  136. /* Compare what was read against the data written */
  137. if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
  138. (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0)) {
  139. if (--tries)
  140. goto retry;
  141. dev_err(&sl->dev,
  142. "could not write to eeprom, scratchpad compare failed %d times\n",
  143. W1_F2D_READ_RETRIES);
  144. return -1;
  145. }
  146. /* Copy the scratchpad to EEPROM */
  147. if (w1_reset_select_slave(sl))
  148. return -1;
  149. wrbuf[0] = W1_F2D_COPY_SCRATCH;
  150. wrbuf[3] = es;
  151. w1_write_block(sl->master, wrbuf, 4);
  152. /* Sleep for tprog ms to wait for the write to complete */
  153. msleep(W1_F2D_TPROG_MS);
  154. /* Reset the bus to wake up the EEPROM */
  155. w1_reset_bus(sl->master);
  156. return 0;
  157. }
  158. static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
  159. struct bin_attribute *bin_attr, char *buf,
  160. loff_t off, size_t count)
  161. {
  162. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  163. int addr, len;
  164. int copy;
  165. count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
  166. if (count == 0)
  167. return 0;
  168. mutex_lock(&sl->master->bus_mutex);
  169. /* Can only write data in blocks of the size of the scratchpad */
  170. addr = off;
  171. len = count;
  172. while (len > 0) {
  173. /* if len too short or addr not aligned */
  174. if (len < W1_F2D_SCRATCH_SIZE || addr & W1_F2D_SCRATCH_MASK) {
  175. char tmp[W1_F2D_SCRATCH_SIZE];
  176. /* read the block and update the parts to be written */
  177. if (w1_f2d_readblock(sl, addr & ~W1_F2D_SCRATCH_MASK,
  178. W1_F2D_SCRATCH_SIZE, tmp)) {
  179. count = -EIO;
  180. goto out_up;
  181. }
  182. /* copy at most to the boundary of the PAGE or len */
  183. copy = W1_F2D_SCRATCH_SIZE -
  184. (addr & W1_F2D_SCRATCH_MASK);
  185. if (copy > len)
  186. copy = len;
  187. memcpy(&tmp[addr & W1_F2D_SCRATCH_MASK], buf, copy);
  188. if (w1_f2d_write(sl, addr & ~W1_F2D_SCRATCH_MASK,
  189. W1_F2D_SCRATCH_SIZE, tmp) < 0) {
  190. count = -EIO;
  191. goto out_up;
  192. }
  193. } else {
  194. copy = W1_F2D_SCRATCH_SIZE;
  195. if (w1_f2d_write(sl, addr, copy, buf) < 0) {
  196. count = -EIO;
  197. goto out_up;
  198. }
  199. }
  200. buf += copy;
  201. addr += copy;
  202. len -= copy;
  203. }
  204. out_up:
  205. mutex_unlock(&sl->master->bus_mutex);
  206. return count;
  207. }
  208. static BIN_ATTR_RW(eeprom, W1_F2D_EEPROM_SIZE);
  209. static struct bin_attribute *w1_f2d_bin_attrs[] = {
  210. &bin_attr_eeprom,
  211. NULL,
  212. };
  213. static const struct attribute_group w1_f2d_group = {
  214. .bin_attrs = w1_f2d_bin_attrs,
  215. };
  216. static const struct attribute_group *w1_f2d_groups[] = {
  217. &w1_f2d_group,
  218. NULL,
  219. };
  220. static struct w1_family_ops w1_f2d_fops = {
  221. .groups = w1_f2d_groups,
  222. };
  223. static struct w1_family w1_family_2d = {
  224. .fid = W1_EEPROM_DS2431,
  225. .fops = &w1_f2d_fops,
  226. };
  227. module_w1_family(w1_family_2d);
  228. MODULE_AUTHOR("Bernhard Weirich <bernhard.weirich@riedel.net>");
  229. MODULE_DESCRIPTION("w1 family 2d driver for DS2431, 1kb EEPROM");
  230. MODULE_LICENSE("GPL");
  231. MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2431));