w1_ds2805.c 6.8 KB

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