w1_ds28e04.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * w1_ds28e04.c - w1 family 1C (DS28E04) driver
  3. *
  4. * Copyright (c) 2012 Markus Franke <franke.m@sebakmt.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. #include <linux/crc16.h>
  17. #include <linux/uaccess.h>
  18. #define CRC16_INIT 0
  19. #define CRC16_VALID 0xb001
  20. #include "../w1.h"
  21. #include "../w1_int.h"
  22. #include "../w1_family.h"
  23. MODULE_LICENSE("GPL");
  24. MODULE_AUTHOR("Markus Franke <franke.m@sebakmt.com>, <franm@hrz.tu-chemnitz.de>");
  25. MODULE_DESCRIPTION("w1 family 1C driver for DS28E04, 4kb EEPROM and PIO");
  26. /* Allow the strong pullup to be disabled, but default to enabled.
  27. * If it was disabled a parasite powered device might not get the required
  28. * current to copy the data from the scratchpad to EEPROM. If it is enabled parasite powered
  29. * devices have a better chance of getting the current required.
  30. */
  31. static int w1_strong_pullup = 1;
  32. module_param_named(strong_pullup, w1_strong_pullup, int, 0);
  33. /* enable/disable CRC checking on DS28E04-100 memory accesses */
  34. static char w1_enable_crccheck = 1;
  35. #define W1_EEPROM_SIZE 512
  36. #define W1_PAGE_COUNT 16
  37. #define W1_PAGE_SIZE 32
  38. #define W1_PAGE_BITS 5
  39. #define W1_PAGE_MASK 0x1F
  40. #define W1_F1C_READ_EEPROM 0xF0
  41. #define W1_F1C_WRITE_SCRATCH 0x0F
  42. #define W1_F1C_READ_SCRATCH 0xAA
  43. #define W1_F1C_COPY_SCRATCH 0x55
  44. #define W1_F1C_ACCESS_WRITE 0x5A
  45. #define W1_1C_REG_LOGIC_STATE 0x220
  46. struct w1_f1C_data {
  47. u8 memory[W1_EEPROM_SIZE];
  48. u32 validcrc;
  49. };
  50. /**
  51. * Check the file size bounds and adjusts count as needed.
  52. * This would not be needed if the file size didn't reset to 0 after a write.
  53. */
  54. static inline size_t w1_f1C_fix_count(loff_t off, size_t count, size_t size)
  55. {
  56. if (off > size)
  57. return 0;
  58. if ((off + count) > size)
  59. return (size - off);
  60. return count;
  61. }
  62. static int w1_f1C_refresh_block(struct w1_slave *sl, struct w1_f1C_data *data,
  63. int block)
  64. {
  65. u8 wrbuf[3];
  66. int off = block * W1_PAGE_SIZE;
  67. if (data->validcrc & (1 << block))
  68. return 0;
  69. if (w1_reset_select_slave(sl)) {
  70. data->validcrc = 0;
  71. return -EIO;
  72. }
  73. wrbuf[0] = W1_F1C_READ_EEPROM;
  74. wrbuf[1] = off & 0xff;
  75. wrbuf[2] = off >> 8;
  76. w1_write_block(sl->master, wrbuf, 3);
  77. w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
  78. /* cache the block if the CRC is valid */
  79. if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
  80. data->validcrc |= (1 << block);
  81. return 0;
  82. }
  83. static int w1_f1C_read(struct w1_slave *sl, int addr, int len, char *data)
  84. {
  85. u8 wrbuf[3];
  86. /* read directly from the EEPROM */
  87. if (w1_reset_select_slave(sl))
  88. return -EIO;
  89. wrbuf[0] = W1_F1C_READ_EEPROM;
  90. wrbuf[1] = addr & 0xff;
  91. wrbuf[2] = addr >> 8;
  92. w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
  93. return w1_read_block(sl->master, data, len);
  94. }
  95. static ssize_t w1_f1C_read_bin(struct file *filp, struct kobject *kobj,
  96. struct bin_attribute *bin_attr,
  97. char *buf, loff_t off, size_t count)
  98. {
  99. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  100. struct w1_f1C_data *data = sl->family_data;
  101. int i, min_page, max_page;
  102. if ((count = w1_f1C_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  103. return 0;
  104. mutex_lock(&sl->master->mutex);
  105. if(w1_enable_crccheck) {
  106. min_page = (off >> W1_PAGE_BITS);
  107. max_page = (off + count - 1) >> W1_PAGE_BITS;
  108. for (i = min_page; i <= max_page; i++) {
  109. if (w1_f1C_refresh_block(sl, data, i)) {
  110. count = -EIO;
  111. goto out_up;
  112. }
  113. }
  114. memcpy(buf, &data->memory[off], count);
  115. }
  116. else {
  117. count = w1_f1C_read(sl, off, count, buf);
  118. }
  119. out_up:
  120. mutex_unlock(&sl->master->mutex);
  121. return count;
  122. }
  123. /**
  124. * Writes to the scratchpad and reads it back for verification.
  125. * Then copies the scratchpad to EEPROM.
  126. * The data must be on one page.
  127. * The master must be locked.
  128. *
  129. * @param sl The slave structure
  130. * @param addr Address for the write
  131. * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
  132. * @param data The data to write
  133. * @return 0=Success -1=failure
  134. */
  135. static int w1_f1C_write(struct w1_slave *sl, int addr, int len, const u8 *data)
  136. {
  137. u8 wrbuf[4];
  138. u8 rdbuf[W1_PAGE_SIZE + 3];
  139. u8 es = (addr + len - 1) & 0x1f;
  140. unsigned int tm = 10;
  141. int i;
  142. struct w1_f1C_data *f1C = sl->family_data;
  143. /* Write the data to the scratchpad */
  144. if (w1_reset_select_slave(sl))
  145. return -1;
  146. wrbuf[0] = W1_F1C_WRITE_SCRATCH;
  147. wrbuf[1] = addr & 0xff;
  148. wrbuf[2] = addr >> 8;
  149. w1_write_block(sl->master, wrbuf, 3);
  150. w1_write_block(sl->master, data, len);
  151. /* Read the scratchpad and verify */
  152. if (w1_reset_select_slave(sl))
  153. return -1;
  154. w1_write_8(sl->master, W1_F1C_READ_SCRATCH);
  155. w1_read_block(sl->master, rdbuf, len + 3);
  156. /* Compare what was read against the data written */
  157. if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
  158. (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
  159. return -1;
  160. /* Copy the scratchpad to EEPROM */
  161. if (w1_reset_select_slave(sl))
  162. return -1;
  163. wrbuf[0] = W1_F1C_COPY_SCRATCH;
  164. wrbuf[3] = es;
  165. for(i = 0; i < sizeof(wrbuf); ++i) {
  166. /* issue 10ms strong pullup (or delay) on the last byte for writing the data from the scratchpad to EEPROM */
  167. if(w1_strong_pullup && i == sizeof(wrbuf)-1)
  168. w1_next_pullup(sl->master, tm);
  169. w1_write_8(sl->master, wrbuf[i]);
  170. }
  171. if(!w1_strong_pullup)
  172. msleep(tm);
  173. if(w1_enable_crccheck) {
  174. /* invalidate cached data */
  175. f1C->validcrc &= ~(1 << (addr >> W1_PAGE_BITS));
  176. }
  177. /* Reset the bus to wake up the EEPROM (this may not be needed) */
  178. w1_reset_bus(sl->master);
  179. return 0;
  180. }
  181. static ssize_t w1_f1C_write_bin(struct file *filp, struct kobject *kobj,
  182. struct bin_attribute *bin_attr,
  183. char *buf, loff_t off, size_t count)
  184. {
  185. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  186. int addr, len, idx;
  187. if ((count = w1_f1C_fix_count(off, count, W1_EEPROM_SIZE)) == 0)
  188. return 0;
  189. if(w1_enable_crccheck) {
  190. /* can only write full blocks in cached mode */
  191. if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
  192. dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
  193. (int)off, count);
  194. return -EINVAL;
  195. }
  196. /* make sure the block CRCs are valid */
  197. for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
  198. if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) {
  199. dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off);
  200. return -EINVAL;
  201. }
  202. }
  203. }
  204. mutex_lock(&sl->master->mutex);
  205. /* Can only write data to one page at a time */
  206. idx = 0;
  207. while (idx < count) {
  208. addr = off + idx;
  209. len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
  210. if (len > (count - idx))
  211. len = count - idx;
  212. if (w1_f1C_write(sl, addr, len, &buf[idx]) < 0) {
  213. count = -EIO;
  214. goto out_up;
  215. }
  216. idx += len;
  217. }
  218. out_up:
  219. mutex_unlock(&sl->master->mutex);
  220. return count;
  221. }
  222. static ssize_t w1_f1C_read_pio(struct file *filp, struct kobject *kobj,
  223. struct bin_attribute *bin_attr,
  224. char *buf, loff_t off, size_t count)
  225. {
  226. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  227. int ret;
  228. /* check arguments */
  229. if(off != 0 || count != 1 || buf == NULL)
  230. return -EINVAL;
  231. mutex_lock(&sl->master->mutex);
  232. ret = w1_f1C_read(sl, W1_1C_REG_LOGIC_STATE, count, buf);
  233. mutex_unlock(&sl->master->mutex);
  234. return ret;
  235. }
  236. static ssize_t w1_f1C_write_pio(struct file *filp, struct kobject *kobj,
  237. struct bin_attribute *bin_attr,
  238. char *buf, loff_t off, size_t count)
  239. {
  240. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  241. u8 wrbuf[3];
  242. u8 ack;
  243. /* check arguments */
  244. if(off != 0 || count != 1 || buf == NULL)
  245. return -EINVAL;
  246. mutex_lock(&sl->master->mutex);
  247. /* Write the PIO data */
  248. if (w1_reset_select_slave(sl)) {
  249. mutex_unlock(&sl->master->mutex);
  250. return -1;
  251. }
  252. /* set bit 7..2 to value '1' */
  253. *buf = *buf | 0xFC;
  254. wrbuf[0] = W1_F1C_ACCESS_WRITE;
  255. wrbuf[1] = *buf;
  256. wrbuf[2] = ~(*buf);
  257. w1_write_block(sl->master, wrbuf, 3);
  258. w1_read_block(sl->master, &ack, sizeof(ack));
  259. mutex_unlock(&sl->master->mutex);
  260. /* check for acknowledgement */
  261. if(ack != 0xAA) return -EIO;
  262. return count;
  263. }
  264. static ssize_t w1_f1C_show_crccheck(struct device *dev, struct device_attribute *attr,
  265. char *buf)
  266. {
  267. if(put_user(w1_enable_crccheck + 0x30, buf))
  268. return -EFAULT;
  269. return sizeof(w1_enable_crccheck);
  270. }
  271. static ssize_t w1_f1C_store_crccheck(struct device *dev, struct device_attribute *attr,
  272. const char *buf, size_t count)
  273. {
  274. char val;
  275. if(count != 1 || !buf) return -EINVAL;
  276. if(get_user(val, buf))
  277. return -EFAULT;
  278. /* convert to decimal */
  279. val = val - 0x30;
  280. if(val != 0 && val != 1) return -EINVAL;
  281. /* set the new value */
  282. w1_enable_crccheck = val;
  283. return sizeof(w1_enable_crccheck);
  284. }
  285. #define NB_SYSFS_BIN_FILES 2
  286. static struct bin_attribute w1_f1C_bin_attr[NB_SYSFS_BIN_FILES] = {
  287. {
  288. .attr = {
  289. .name = "eeprom",
  290. .mode = S_IRUGO | S_IWUSR,
  291. },
  292. .size = W1_EEPROM_SIZE,
  293. .read = w1_f1C_read_bin,
  294. .write = w1_f1C_write_bin,
  295. },
  296. {
  297. .attr = {
  298. .name = "pio",
  299. .mode = S_IRUGO | S_IWUSR,
  300. },
  301. .size = 1,
  302. .read = w1_f1C_read_pio,
  303. .write = w1_f1C_write_pio,
  304. }
  305. };
  306. static DEVICE_ATTR(crccheck, S_IWUSR | S_IRUGO, w1_f1C_show_crccheck, w1_f1C_store_crccheck);
  307. static int w1_f1C_add_slave(struct w1_slave *sl)
  308. {
  309. int err = 0;
  310. int i;
  311. struct w1_f1C_data *data = NULL;
  312. if(w1_enable_crccheck) {
  313. data = kzalloc(sizeof(struct w1_f1C_data), GFP_KERNEL);
  314. if (!data)
  315. return -ENOMEM;
  316. sl->family_data = data;
  317. }
  318. /* create binary sysfs attributes */
  319. for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
  320. err = sysfs_create_bin_file(&sl->dev.kobj, &(w1_f1C_bin_attr[i]));
  321. if(err)
  322. goto out;
  323. /* create device attributes */
  324. err = device_create_file(&sl->dev, &dev_attr_crccheck);
  325. if(err) {
  326. /* remove binary sysfs attributes */
  327. for (i = 0; i < NB_SYSFS_BIN_FILES; ++i)
  328. sysfs_remove_bin_file(&sl->dev.kobj, &(w1_f1C_bin_attr[i]));
  329. }
  330. out:
  331. if(err) {
  332. if(w1_enable_crccheck)
  333. kfree(data);
  334. }
  335. return err;
  336. }
  337. static void w1_f1C_remove_slave(struct w1_slave *sl)
  338. {
  339. int i;
  340. if(w1_enable_crccheck) {
  341. kfree(sl->family_data);
  342. sl->family_data = NULL;
  343. }
  344. /* remove device attributes */
  345. device_remove_file(&sl->dev, &dev_attr_crccheck);
  346. /* remove binary sysfs attributes */
  347. for (i = 0; i < NB_SYSFS_BIN_FILES; ++i)
  348. sysfs_remove_bin_file(&sl->dev.kobj, &(w1_f1C_bin_attr[i]));
  349. }
  350. static struct w1_family_ops w1_f1C_fops = {
  351. .add_slave = w1_f1C_add_slave,
  352. .remove_slave = w1_f1C_remove_slave,
  353. };
  354. static struct w1_family w1_family_1C = {
  355. .fid = W1_FAMILY_DS28E04,
  356. .fops = &w1_f1C_fops,
  357. };
  358. static int __init w1_f1C_init(void)
  359. {
  360. return w1_register_family(&w1_family_1C);
  361. }
  362. static void __exit w1_f1C_fini(void)
  363. {
  364. w1_unregister_family(&w1_family_1C);
  365. }
  366. module_init(w1_f1C_init);
  367. module_exit(w1_f1C_fini);