w1_ds2433.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /*
  3. * w1_ds2433.c - w1 family 23 (DS2433) driver
  4. *
  5. * Copyright (c) 2005 Ben Gardner <[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/slab.h>
  14. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  15. #include <linux/crc16.h>
  16. #define CRC16_INIT 0
  17. #define CRC16_VALID 0xb001
  18. #endif
  19. #include <linux/w1.h>
  20. #define W1_EEPROM_DS2433 0x23
  21. #define W1_EEPROM_SIZE 512
  22. #define W1_PAGE_COUNT 16
  23. #define W1_PAGE_SIZE 32
  24. #define W1_PAGE_BITS 5
  25. #define W1_PAGE_MASK 0x1F
  26. #define W1_F23_TIME 300
  27. #define W1_F23_READ_EEPROM 0xF0
  28. #define W1_F23_WRITE_SCRATCH 0x0F
  29. #define W1_F23_READ_SCRATCH 0xAA
  30. #define W1_F23_COPY_SCRATCH 0x55
  31. struct w1_f23_data {
  32. u8 memory[W1_EEPROM_SIZE];
  33. u32 validcrc;
  34. };
  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_f23_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. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  48. static int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data,
  49. int block)
  50. {
  51. u8 wrbuf[3];
  52. int off = block * W1_PAGE_SIZE;
  53. if (data->validcrc & (1 << block))
  54. return 0;
  55. if (w1_reset_select_slave(sl)) {
  56. data->validcrc = 0;
  57. return -EIO;
  58. }
  59. wrbuf[0] = W1_F23_READ_EEPROM;
  60. wrbuf[1] = off & 0xff;
  61. wrbuf[2] = off >> 8;
  62. w1_write_block(sl->master, wrbuf, 3);
  63. w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
  64. /* cache the block if the CRC is valid */
  65. if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
  66. data->validcrc |= (1 << block);
  67. return 0;
  68. }
  69. #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
  70. static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
  71. struct bin_attribute *bin_attr, char *buf,
  72. loff_t off, size_t count)
  73. {
  74. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  75. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  76. struct w1_f23_data *data = sl->family_data;
  77. int i, min_page, max_page;
  78. #else
  79. u8 wrbuf[3];
  80. #endif
  81. if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  82. return 0;
  83. mutex_lock(&sl->master->bus_mutex);
  84. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  85. min_page = (off >> W1_PAGE_BITS);
  86. max_page = (off + count - 1) >> W1_PAGE_BITS;
  87. for (i = min_page; i <= max_page; i++) {
  88. if (w1_f23_refresh_block(sl, data, i)) {
  89. count = -EIO;
  90. goto out_up;
  91. }
  92. }
  93. memcpy(buf, &data->memory[off], count);
  94. #else /* CONFIG_W1_SLAVE_DS2433_CRC */
  95. /* read directly from the EEPROM */
  96. if (w1_reset_select_slave(sl)) {
  97. count = -EIO;
  98. goto out_up;
  99. }
  100. wrbuf[0] = W1_F23_READ_EEPROM;
  101. wrbuf[1] = off & 0xff;
  102. wrbuf[2] = off >> 8;
  103. w1_write_block(sl->master, wrbuf, 3);
  104. w1_read_block(sl->master, buf, count);
  105. #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
  106. out_up:
  107. mutex_unlock(&sl->master->bus_mutex);
  108. return count;
  109. }
  110. /**
  111. * Writes to the scratchpad and reads it back for verification.
  112. * Then copies the scratchpad to EEPROM.
  113. * The data must be on one page.
  114. * The master must be locked.
  115. *
  116. * @param sl The slave structure
  117. * @param addr Address for the write
  118. * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
  119. * @param data The data to write
  120. * @return 0=Success -1=failure
  121. */
  122. static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
  123. {
  124. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  125. struct w1_f23_data *f23 = sl->family_data;
  126. #endif
  127. u8 wrbuf[4];
  128. u8 rdbuf[W1_PAGE_SIZE + 3];
  129. u8 es = (addr + len - 1) & 0x1f;
  130. /* Write the data to the scratchpad */
  131. if (w1_reset_select_slave(sl))
  132. return -1;
  133. wrbuf[0] = W1_F23_WRITE_SCRATCH;
  134. wrbuf[1] = addr & 0xff;
  135. wrbuf[2] = addr >> 8;
  136. w1_write_block(sl->master, wrbuf, 3);
  137. w1_write_block(sl->master, data, len);
  138. /* Read the scratchpad and verify */
  139. if (w1_reset_select_slave(sl))
  140. return -1;
  141. w1_write_8(sl->master, W1_F23_READ_SCRATCH);
  142. w1_read_block(sl->master, rdbuf, len + 3);
  143. /* Compare what was read against the data written */
  144. if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
  145. (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
  146. return -1;
  147. /* Copy the scratchpad to EEPROM */
  148. if (w1_reset_select_slave(sl))
  149. return -1;
  150. wrbuf[0] = W1_F23_COPY_SCRATCH;
  151. wrbuf[3] = es;
  152. w1_write_block(sl->master, wrbuf, 4);
  153. /* Sleep for 5 ms to wait for the write to complete */
  154. msleep(5);
  155. /* Reset the bus to wake up the EEPROM (this may not be needed) */
  156. w1_reset_bus(sl->master);
  157. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  158. f23->validcrc &= ~(1 << (addr >> W1_PAGE_BITS));
  159. #endif
  160. return 0;
  161. }
  162. static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
  163. struct bin_attribute *bin_attr, char *buf,
  164. loff_t off, size_t count)
  165. {
  166. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  167. int addr, len, idx;
  168. if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  169. return 0;
  170. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  171. /* can only write full blocks in cached mode */
  172. if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
  173. dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
  174. (int)off, count);
  175. return -EINVAL;
  176. }
  177. /* make sure the block CRCs are valid */
  178. for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
  179. if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) {
  180. dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off);
  181. return -EINVAL;
  182. }
  183. }
  184. #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
  185. mutex_lock(&sl->master->bus_mutex);
  186. /* Can only write data to one page at a time */
  187. idx = 0;
  188. while (idx < count) {
  189. addr = off + idx;
  190. len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
  191. if (len > (count - idx))
  192. len = count - idx;
  193. if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
  194. count = -EIO;
  195. goto out_up;
  196. }
  197. idx += len;
  198. }
  199. out_up:
  200. mutex_unlock(&sl->master->bus_mutex);
  201. return count;
  202. }
  203. static BIN_ATTR_RW(eeprom, W1_EEPROM_SIZE);
  204. static struct bin_attribute *w1_f23_bin_attributes[] = {
  205. &bin_attr_eeprom,
  206. NULL,
  207. };
  208. static const struct attribute_group w1_f23_group = {
  209. .bin_attrs = w1_f23_bin_attributes,
  210. };
  211. static const struct attribute_group *w1_f23_groups[] = {
  212. &w1_f23_group,
  213. NULL,
  214. };
  215. static int w1_f23_add_slave(struct w1_slave *sl)
  216. {
  217. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  218. struct w1_f23_data *data;
  219. data = kzalloc(sizeof(struct w1_f23_data), GFP_KERNEL);
  220. if (!data)
  221. return -ENOMEM;
  222. sl->family_data = data;
  223. #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
  224. return 0;
  225. }
  226. static void w1_f23_remove_slave(struct w1_slave *sl)
  227. {
  228. #ifdef CONFIG_W1_SLAVE_DS2433_CRC
  229. kfree(sl->family_data);
  230. sl->family_data = NULL;
  231. #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
  232. }
  233. static const struct w1_family_ops w1_f23_fops = {
  234. .add_slave = w1_f23_add_slave,
  235. .remove_slave = w1_f23_remove_slave,
  236. .groups = w1_f23_groups,
  237. };
  238. static struct w1_family w1_family_23 = {
  239. .fid = W1_EEPROM_DS2433,
  240. .fops = &w1_f23_fops,
  241. };
  242. module_w1_family(w1_family_23);
  243. MODULE_AUTHOR("Ben Gardner <[email protected]>");
  244. MODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM");
  245. MODULE_LICENSE("GPL");
  246. MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2433));