w1_ds28e04.c 9.4 KB

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