w1_ds28e04.c 9.9 KB

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