w1_ds2433.c 7.0 KB

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