w1_ds2431.c 7.0 KB

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