w1_ds28e04.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  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 w1_f1C_read_bin(struct file *filp, struct kobject *kobj,
  98. struct bin_attribute *bin_attr,
  99. char *buf, 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 w1_f1C_write_bin(struct file *filp, struct kobject *kobj,
  185. struct bin_attribute *bin_attr,
  186. char *buf, 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 ssize_t w1_f1C_read_pio(struct file *filp, struct kobject *kobj,
  229. struct bin_attribute *bin_attr,
  230. char *buf, loff_t off, size_t count)
  231. {
  232. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  233. int ret;
  234. /* check arguments */
  235. if (off != 0 || count != 1 || buf == NULL)
  236. return -EINVAL;
  237. mutex_lock(&sl->master->mutex);
  238. ret = w1_f1C_read(sl, W1_1C_REG_LOGIC_STATE, count, buf);
  239. mutex_unlock(&sl->master->mutex);
  240. return ret;
  241. }
  242. static ssize_t w1_f1C_write_pio(struct file *filp, struct kobject *kobj,
  243. struct bin_attribute *bin_attr,
  244. char *buf, loff_t off, size_t count)
  245. {
  246. struct w1_slave *sl = kobj_to_w1_slave(kobj);
  247. u8 wrbuf[3];
  248. u8 ack;
  249. /* check arguments */
  250. if (off != 0 || count != 1 || buf == NULL)
  251. return -EINVAL;
  252. mutex_lock(&sl->master->mutex);
  253. /* Write the PIO data */
  254. if (w1_reset_select_slave(sl)) {
  255. mutex_unlock(&sl->master->mutex);
  256. return -1;
  257. }
  258. /* set bit 7..2 to value '1' */
  259. *buf = *buf | 0xFC;
  260. wrbuf[0] = W1_F1C_ACCESS_WRITE;
  261. wrbuf[1] = *buf;
  262. wrbuf[2] = ~(*buf);
  263. w1_write_block(sl->master, wrbuf, 3);
  264. w1_read_block(sl->master, &ack, sizeof(ack));
  265. mutex_unlock(&sl->master->mutex);
  266. /* check for acknowledgement */
  267. if (ack != 0xAA)
  268. return -EIO;
  269. return count;
  270. }
  271. static ssize_t w1_f1C_show_crccheck(struct device *dev,
  272. struct device_attribute *attr, char *buf)
  273. {
  274. if (put_user(w1_enable_crccheck + 0x30, buf))
  275. return -EFAULT;
  276. return sizeof(w1_enable_crccheck);
  277. }
  278. static ssize_t w1_f1C_store_crccheck(struct device *dev,
  279. struct device_attribute *attr,
  280. const char *buf, size_t count)
  281. {
  282. char val;
  283. if (count != 1 || !buf)
  284. return -EINVAL;
  285. if (get_user(val, buf))
  286. return -EFAULT;
  287. /* convert to decimal */
  288. val = val - 0x30;
  289. if (val != 0 && val != 1)
  290. return -EINVAL;
  291. /* set the new value */
  292. w1_enable_crccheck = val;
  293. return sizeof(w1_enable_crccheck);
  294. }
  295. #define NB_SYSFS_BIN_FILES 2
  296. static struct bin_attribute w1_f1C_bin_attr[NB_SYSFS_BIN_FILES] = {
  297. {
  298. .attr = {
  299. .name = "eeprom",
  300. .mode = S_IRUGO | S_IWUSR,
  301. },
  302. .size = W1_EEPROM_SIZE,
  303. .read = w1_f1C_read_bin,
  304. .write = w1_f1C_write_bin,
  305. },
  306. {
  307. .attr = {
  308. .name = "pio",
  309. .mode = S_IRUGO | S_IWUSR,
  310. },
  311. .size = 1,
  312. .read = w1_f1C_read_pio,
  313. .write = w1_f1C_write_pio,
  314. }
  315. };
  316. static DEVICE_ATTR(crccheck, S_IWUSR | S_IRUGO,
  317. w1_f1C_show_crccheck, w1_f1C_store_crccheck);
  318. static int w1_f1C_add_slave(struct w1_slave *sl)
  319. {
  320. int err = 0;
  321. int i;
  322. struct w1_f1C_data *data = NULL;
  323. if (w1_enable_crccheck) {
  324. data = kzalloc(sizeof(struct w1_f1C_data), GFP_KERNEL);
  325. if (!data)
  326. return -ENOMEM;
  327. sl->family_data = data;
  328. }
  329. /* create binary sysfs attributes */
  330. for (i = 0; i < NB_SYSFS_BIN_FILES && !err; ++i)
  331. err = sysfs_create_bin_file(
  332. &sl->dev.kobj, &(w1_f1C_bin_attr[i]));
  333. if (!err) {
  334. /* create device attributes */
  335. err = device_create_file(&sl->dev, &dev_attr_crccheck);
  336. }
  337. if (err) {
  338. /* remove binary sysfs attributes */
  339. for (i = 0; i < NB_SYSFS_BIN_FILES; ++i)
  340. sysfs_remove_bin_file(
  341. &sl->dev.kobj, &(w1_f1C_bin_attr[i]));
  342. kfree(data);
  343. }
  344. return err;
  345. }
  346. static void w1_f1C_remove_slave(struct w1_slave *sl)
  347. {
  348. int i;
  349. kfree(sl->family_data);
  350. sl->family_data = NULL;
  351. /* remove device attributes */
  352. device_remove_file(&sl->dev, &dev_attr_crccheck);
  353. /* remove binary sysfs attributes */
  354. for (i = 0; i < NB_SYSFS_BIN_FILES; ++i)
  355. sysfs_remove_bin_file(&sl->dev.kobj, &(w1_f1C_bin_attr[i]));
  356. }
  357. static struct w1_family_ops w1_f1C_fops = {
  358. .add_slave = w1_f1C_add_slave,
  359. .remove_slave = w1_f1C_remove_slave,
  360. };
  361. static struct w1_family w1_family_1C = {
  362. .fid = W1_FAMILY_DS28E04,
  363. .fops = &w1_f1C_fops,
  364. };
  365. static int __init w1_f1C_init(void)
  366. {
  367. return w1_register_family(&w1_family_1C);
  368. }
  369. static void __exit w1_f1C_fini(void)
  370. {
  371. w1_unregister_family(&w1_family_1C);
  372. }
  373. module_init(w1_f1C_init);
  374. module_exit(w1_f1C_fini);