w1_ds2433.c 7.1 KB

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