w1_ds2805.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. /*
  2. * w1_ds2805 - w1 family 0d (DS28E05) driver
  3. *
  4. * Copyright (c) 2016 Andrew Worsley amworsley@gmail.com
  5. *
  6. * This source code is licensed under the GNU General Public License,
  7. * Version 2. See the file COPYING for more details.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/module.h>
  11. #include <linux/moduleparam.h>
  12. #include <linux/device.h>
  13. #include <linux/types.h>
  14. #include <linux/delay.h>
  15. #include <linux/w1.h>
  16. #define W1_EEPROM_DS2805 0x0D
  17. #define W1_F0D_EEPROM_SIZE 128
  18. #define W1_F0D_PAGE_BITS 3
  19. #define W1_F0D_PAGE_SIZE (1<<W1_F0D_PAGE_BITS)
  20. #define W1_F0D_PAGE_MASK 0x0F
  21. #define W1_F0D_SCRATCH_BITS 1
  22. #define W1_F0D_SCRATCH_SIZE (1<<W1_F0D_SCRATCH_BITS)
  23. #define W1_F0D_SCRATCH_MASK (W1_F0D_SCRATCH_SIZE-1)
  24. #define W1_F0D_READ_EEPROM 0xF0
  25. #define W1_F0D_WRITE_EEPROM 0x55
  26. #define W1_F0D_RELEASE 0xFF
  27. #define W1_F0D_CS_OK 0xAA /* Chip Status Ok */
  28. #define W1_F0D_TPROG_MS 16
  29. #define W1_F0D_READ_RETRIES 10
  30. #define W1_F0D_READ_MAXLEN W1_F0D_EEPROM_SIZE
  31. /*
  32. * Check the file size bounds and adjusts count as needed.
  33. * This would not be needed if the file size didn't reset to 0 after a write.
  34. */
  35. static inline size_t w1_f0d_fix_count(loff_t off, size_t count, size_t size)
  36. {
  37. if (off > size)
  38. return 0;
  39. if ((off + count) > size)
  40. return size - off;
  41. return count;
  42. }
  43. /*
  44. * Read a block from W1 ROM two times and compares the results.
  45. * If they are equal they are returned, otherwise the read
  46. * is repeated W1_F0D_READ_RETRIES times.
  47. *
  48. * count must not exceed W1_F0D_READ_MAXLEN.
  49. */
  50. static int w1_f0d_readblock(struct w1_slave *sl, int off, int count, char *buf)
  51. {
  52. u8 wrbuf[3];
  53. u8 cmp[W1_F0D_READ_MAXLEN];
  54. int tries = W1_F0D_READ_RETRIES;
  55. do {
  56. wrbuf[0] = W1_F0D_READ_EEPROM;
  57. wrbuf[1] = off & 0x7f;
  58. wrbuf[2] = 0;
  59. if (w1_reset_select_slave(sl))
  60. return -1;
  61. w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
  62. w1_read_block(sl->master, buf, count);
  63. if (w1_reset_select_slave(sl))
  64. return -1;
  65. w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
  66. w1_read_block(sl->master, cmp, count);
  67. if (!memcmp(cmp, buf, count))
  68. return 0;
  69. } while (--tries);
  70. dev_err(&sl->dev, "proof reading failed %d times\n",
  71. W1_F0D_READ_RETRIES);
  72. return -1;
  73. }
  74. static ssize_t w1_f0d_read_bin(struct file *filp, struct kobject *kobj,
  75. struct bin_attribute *bin_attr,
  76. char *buf, loff_t off, size_t count)
  77. {
  78. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  79. int todo = count;
  80. count = w1_f0d_fix_count(off, count, W1_F0D_EEPROM_SIZE);
  81. if (count == 0)
  82. return 0;
  83. mutex_lock(&sl->master->mutex);
  84. /* read directly from the EEPROM in chunks of W1_F0D_READ_MAXLEN */
  85. while (todo > 0) {
  86. int block_read;
  87. if (todo >= W1_F0D_READ_MAXLEN)
  88. block_read = W1_F0D_READ_MAXLEN;
  89. else
  90. block_read = todo;
  91. if (w1_f0d_readblock(sl, off, block_read, buf) < 0) {
  92. count = -EIO;
  93. break;
  94. }
  95. todo -= W1_F0D_READ_MAXLEN;
  96. buf += W1_F0D_READ_MAXLEN;
  97. off += W1_F0D_READ_MAXLEN;
  98. }
  99. mutex_unlock(&sl->master->mutex);
  100. return count;
  101. }
  102. /*
  103. * Writes to the scratchpad and reads it back for verification.
  104. * Then copies the scratchpad to EEPROM.
  105. * The data must be aligned at W1_F0D_SCRATCH_SIZE bytes and
  106. * must be W1_F0D_SCRATCH_SIZE bytes long.
  107. * The master must be locked.
  108. *
  109. * @param sl The slave structure
  110. * @param addr Address for the write
  111. * @param len length must be <= (W1_F0D_PAGE_SIZE - (addr & W1_F0D_PAGE_MASK))
  112. * @param data The data to write
  113. * @return 0=Success -1=failure
  114. */
  115. static int w1_f0d_write(struct w1_slave *sl, int addr, int len, const u8 *data)
  116. {
  117. int tries = W1_F0D_READ_RETRIES;
  118. u8 wrbuf[3];
  119. u8 rdbuf[W1_F0D_SCRATCH_SIZE];
  120. u8 cs;
  121. if ((addr & 1) || (len != 2)) {
  122. dev_err(&sl->dev, "%s: bad addr/len - addr=%#x len=%d\n",
  123. __func__, addr, len);
  124. return -1;
  125. }
  126. retry:
  127. /* Write the data to the scratchpad */
  128. if (w1_reset_select_slave(sl))
  129. return -1;
  130. wrbuf[0] = W1_F0D_WRITE_EEPROM;
  131. wrbuf[1] = addr & 0xff;
  132. wrbuf[2] = 0xff; /* ?? from Example */
  133. w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
  134. w1_write_block(sl->master, data, len);
  135. w1_read_block(sl->master, rdbuf, sizeof(rdbuf));
  136. /* Compare what was read against the data written */
  137. if ((rdbuf[0] != data[0]) || (rdbuf[1] != data[1])) {
  138. if (--tries)
  139. goto retry;
  140. dev_err(&sl->dev,
  141. "could not write to eeprom, scratchpad compare failed %d times\n",
  142. W1_F0D_READ_RETRIES);
  143. pr_info("%s: rdbuf = %#x %#x data = %#x %#x\n",
  144. __func__, rdbuf[0], rdbuf[1], data[0], data[1]);
  145. return -1;
  146. }
  147. /* Trigger write out to EEPROM */
  148. w1_write_8(sl->master, W1_F0D_RELEASE);
  149. /* Sleep for tprog ms to wait for the write to complete */
  150. msleep(W1_F0D_TPROG_MS);
  151. /* Check CS (Command Status) == 0xAA ? */
  152. cs = w1_read_8(sl->master);
  153. if (cs != W1_F0D_CS_OK) {
  154. dev_err(&sl->dev, "save to eeprom failed = CS=%#x\n", cs);
  155. return -1;
  156. }
  157. return 0;
  158. }
  159. static ssize_t w1_f0d_write_bin(struct file *filp, struct kobject *kobj,
  160. struct bin_attribute *bin_attr,
  161. char *buf, 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_f0d_fix_count(off, count, W1_F0D_EEPROM_SIZE);
  167. if (count == 0)
  168. return 0;
  169. mutex_lock(&sl->master->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_F0D_SCRATCH_SIZE || addr & W1_F0D_SCRATCH_MASK) {
  176. char tmp[W1_F0D_SCRATCH_SIZE];
  177. /* read the block and update the parts to be written */
  178. if (w1_f0d_readblock(sl, addr & ~W1_F0D_SCRATCH_MASK,
  179. W1_F0D_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_F0D_SCRATCH_SIZE -
  185. (addr & W1_F0D_SCRATCH_MASK);
  186. if (copy > len)
  187. copy = len;
  188. memcpy(&tmp[addr & W1_F0D_SCRATCH_MASK], buf, copy);
  189. if (w1_f0d_write(sl, addr & ~W1_F0D_SCRATCH_MASK,
  190. W1_F0D_SCRATCH_SIZE, tmp) < 0) {
  191. count = -EIO;
  192. goto out_up;
  193. }
  194. } else {
  195. copy = W1_F0D_SCRATCH_SIZE;
  196. if (w1_f0d_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->mutex);
  207. return count;
  208. }
  209. static struct bin_attribute w1_f0d_bin_attr = {
  210. .attr = {
  211. .name = "eeprom",
  212. .mode = S_IRUGO | S_IWUSR,
  213. },
  214. .size = W1_F0D_EEPROM_SIZE,
  215. .read = w1_f0d_read_bin,
  216. .write = w1_f0d_write_bin,
  217. };
  218. static int w1_f0d_add_slave(struct w1_slave *sl)
  219. {
  220. return sysfs_create_bin_file(&sl->dev.kobj, &w1_f0d_bin_attr);
  221. }
  222. static void w1_f0d_remove_slave(struct w1_slave *sl)
  223. {
  224. sysfs_remove_bin_file(&sl->dev.kobj, &w1_f0d_bin_attr);
  225. }
  226. static struct w1_family_ops w1_f0d_fops = {
  227. .add_slave = w1_f0d_add_slave,
  228. .remove_slave = w1_f0d_remove_slave,
  229. };
  230. static struct w1_family w1_family_2d = {
  231. .fid = W1_EEPROM_DS2805,
  232. .fops = &w1_f0d_fops,
  233. };
  234. static int __init w1_f0d_init(void)
  235. {
  236. pr_info("%s()\n", __func__);
  237. return w1_register_family(&w1_family_2d);
  238. }
  239. static void __exit w1_f0d_fini(void)
  240. {
  241. pr_info("%s()\n", __func__);
  242. w1_unregister_family(&w1_family_2d);
  243. }
  244. module_init(w1_f0d_init);
  245. module_exit(w1_f0d_fini);
  246. MODULE_LICENSE("GPL");
  247. MODULE_AUTHOR("Andrew Worsley amworsley@gmail.com");
  248. MODULE_DESCRIPTION("w1 family 0d driver for DS2805, 1kb EEPROM");