w1_ds28e04.c 11 KB

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