ds2502.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. // SPDX-License-Identifier: GPL-2.0+
  2. /*
  3. * Driver for DS-2502 One wire "Add only Memory".
  4. *
  5. * The chip has 4 pages of 32 bytes.
  6. * In addition it has 8 out of band status bytes that are used, by software,
  7. * as page redirection bytes by an algorithm described in the data sheet.
  8. * This is useful since data cannot be erased once written but it can be
  9. * "patched" up to four times by switching pages.
  10. *
  11. * So, when a read request is entirely in the first page automatically
  12. * apply the page redirection bytes (which allows the device to be seen as
  13. * a 32 byte PROM, writable 4 times).
  14. *
  15. * If the read request is outside of or larger than the first page then read
  16. * the raw data (which allows the device to be seen as a 128 byte PROM,
  17. * writable once).
  18. *
  19. * Copyright (c) 2018 Flowbird
  20. * Martin Fuzzey <martin.fuzzey@flowbird.group>
  21. */
  22. #include <common.h>
  23. #include <dm.h>
  24. #include <dm/device_compat.h>
  25. #include <linux/err.h>
  26. #include <w1-eeprom.h>
  27. #include <w1.h>
  28. #define DS2502_PAGE_SIZE 32
  29. #define DS2502_PAGE_COUNT 4
  30. #define DS2502_STATUS_SIZE 8
  31. #define DS2502_CMD_READ_STATUS 0xAA
  32. #define DS2502_CMD_READ_GEN_CRC 0xC3
  33. /* u-boot crc8() is CCITT CRC8, we need x^8 + x^5 + x^4 + 1 LSB first */
  34. static unsigned int ds2502_crc8(const u8 *buf, int len)
  35. {
  36. static const u8 poly = 0x8C; /* (1 + x^4 + x^5) + x^8 */
  37. u8 crc = 0;
  38. int i;
  39. for (i = 0; i < len; i++) {
  40. u8 data = buf[i];
  41. int j;
  42. for (j = 0; j < 8; j++) {
  43. u8 mix = (crc ^ data) & 1;
  44. crc >>= 1;
  45. if (mix)
  46. crc ^= poly;
  47. data >>= 1;
  48. }
  49. }
  50. return crc;
  51. }
  52. static int ds2502_read(struct udevice *dev, u8 cmd,
  53. int bytes_in_page, int pos,
  54. u8 *buf, int bytes_for_user)
  55. {
  56. int retry;
  57. int ret = 0;
  58. for (retry = 0; retry < 3; retry++) {
  59. u8 pagebuf[DS2502_PAGE_SIZE + 1]; /* 1 byte for CRC8 */
  60. u8 crc;
  61. int i;
  62. ret = w1_reset_select(dev);
  63. if (ret)
  64. return ret;
  65. /* send read to end of page and generate CRC command */
  66. pagebuf[0] = cmd;
  67. pagebuf[1] = pos & 0xff;
  68. pagebuf[2] = pos >> 8;
  69. crc = ds2502_crc8(pagebuf, 3);
  70. for (i = 0; i < 3; i++)
  71. w1_write_byte(dev, pagebuf[i]);
  72. /* Check command CRC */
  73. ret = w1_read_byte(dev);
  74. if (ret < 0) {
  75. dev_dbg(dev, "Error %d reading command CRC\n", ret);
  76. continue;
  77. }
  78. if (ret != crc) {
  79. dev_dbg(dev,
  80. "bad CRC8 for cmd %02x got=%02X exp=%02X\n",
  81. cmd, ret, crc);
  82. ret = -EIO;
  83. continue;
  84. }
  85. /* read data and check CRC */
  86. ret = w1_read_buf(dev, pagebuf, bytes_in_page + 1);
  87. if (ret < 0) {
  88. dev_dbg(dev, "Error %d reading data\n", ret);
  89. continue;
  90. }
  91. crc = ds2502_crc8(pagebuf, bytes_in_page);
  92. if (crc == pagebuf[bytes_in_page]) {
  93. memcpy(buf, pagebuf, bytes_for_user);
  94. ret = 0;
  95. break;
  96. }
  97. dev_dbg(dev, "Bad CRC8 got=%02X exp=%02X pos=%04X\n",
  98. pagebuf[bytes_in_page], crc, pos);
  99. ret = -EIO;
  100. }
  101. return ret;
  102. }
  103. static inline int ds2502_read_status_bytes(struct udevice *dev, u8 *buf)
  104. {
  105. return ds2502_read(dev, DS2502_CMD_READ_STATUS,
  106. DS2502_STATUS_SIZE, 0,
  107. buf, DS2502_STATUS_SIZE);
  108. }
  109. /*
  110. * Status bytes (from index 1) contain 1's complement page indirection
  111. * So for N writes:
  112. * N=1: ff ff ff ff ff ff ff 00
  113. * N=2: ff fe ff ff ff ff ff 00
  114. * N=3: ff fe fd ff ff ff ff 00
  115. * N=4: ff fe fd fc ff ff ff 00
  116. */
  117. static int ds2502_indirect_page(struct udevice *dev, u8 *status, int page)
  118. {
  119. int page_seen = 0;
  120. do {
  121. u8 sb = status[page + 1];
  122. if (sb == 0xff)
  123. break;
  124. page = ~sb & 0xff;
  125. if (page >= DS2502_PAGE_COUNT) {
  126. dev_err(dev,
  127. "Illegal page redirection status byte %02x\n",
  128. sb);
  129. return -EINVAL;
  130. }
  131. if (page_seen & (1 << page)) {
  132. dev_err(dev, "Infinite loop in page redirection\n");
  133. return -EINVAL;
  134. }
  135. page_seen |= (1 << page);
  136. } while (1);
  137. return page;
  138. }
  139. static int ds2502_read_buf(struct udevice *dev, unsigned int offset,
  140. u8 *buf, unsigned int count)
  141. {
  142. unsigned int min_page = offset / DS2502_PAGE_SIZE;
  143. unsigned int max_page = (offset + count - 1) / DS2502_PAGE_SIZE;
  144. int xfered = 0;
  145. u8 status_bytes[DS2502_STATUS_SIZE];
  146. int i;
  147. int ret;
  148. if (min_page >= DS2502_PAGE_COUNT || max_page >= DS2502_PAGE_COUNT)
  149. return -EINVAL;
  150. if (min_page == 0 && max_page == 0) {
  151. ret = ds2502_read_status_bytes(dev, status_bytes);
  152. if (ret)
  153. return ret;
  154. } else {
  155. /* Dummy one to one page redirection */
  156. memset(status_bytes, 0xff, sizeof(status_bytes));
  157. }
  158. for (i = min_page; i <= max_page; i++) {
  159. int page;
  160. int pos;
  161. int bytes_in_page;
  162. int bytes_for_user;
  163. page = ds2502_indirect_page(dev, status_bytes, i);
  164. if (page < 0)
  165. return page;
  166. dev_dbg(dev, "page logical %d => physical %d\n", i, page);
  167. pos = page * DS2502_PAGE_SIZE;
  168. if (i == min_page)
  169. pos += offset % DS2502_PAGE_SIZE;
  170. bytes_in_page = DS2502_PAGE_SIZE - (pos % DS2502_PAGE_SIZE);
  171. if (i == max_page)
  172. bytes_for_user = count - xfered;
  173. else
  174. bytes_for_user = bytes_in_page;
  175. ret = ds2502_read(dev, DS2502_CMD_READ_GEN_CRC,
  176. bytes_in_page, pos,
  177. &buf[xfered], bytes_for_user);
  178. if (ret < 0)
  179. return ret;
  180. xfered += bytes_for_user;
  181. }
  182. return 0;
  183. }
  184. static int ds2502_probe(struct udevice *dev)
  185. {
  186. struct w1_device *w1;
  187. w1 = dev_get_parent_plat(dev);
  188. w1->id = 0;
  189. return 0;
  190. }
  191. static const struct w1_eeprom_ops ds2502_ops = {
  192. .read_buf = ds2502_read_buf,
  193. };
  194. static const struct udevice_id ds2502_id[] = {
  195. { .compatible = "maxim,ds2502", .data = W1_FAMILY_DS2502 },
  196. { },
  197. };
  198. U_BOOT_DRIVER(ds2502) = {
  199. .name = "ds2502",
  200. .id = UCLASS_W1_EEPROM,
  201. .of_match = ds2502_id,
  202. .ops = &ds2502_ops,
  203. .probe = ds2502_probe,
  204. };
  205. u8 family_supported[] = {
  206. W1_FAMILY_DS2502,
  207. };
  208. U_BOOT_W1_DEVICE(ds2502, family_supported);