w1_ds2431.c 6.7 KB

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