w1_ds28e04.c 9.7 KB

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