w1_ds2433.c 7.1 KB

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