ds2502.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  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 <linux/err.h>
  25. #include <w1-eeprom.h>
  26. #include <w1.h>
  27. #define DS2502_PAGE_SIZE 32
  28. #define DS2502_PAGE_COUNT 4
  29. #define DS2502_STATUS_SIZE 8
  30. #define DS2502_CMD_READ_STATUS 0xAA
  31. #define DS2502_CMD_READ_GEN_CRC 0xC3
  32. /* u-boot crc8() is CCITT CRC8, we need x^8 + x^5 + x^4 + 1 LSB first */
  33. static unsigned int ds2502_crc8(const u8 *buf, int len)
  34. {
  35. static const u8 poly = 0x8C; /* (1 + x^4 + x^5) + x^8 */
  36. u8 crc = 0;
  37. int i;
  38. for (i = 0; i < len; i++) {
  39. u8 data = buf[i];
  40. int j;
  41. for (j = 0; j < 8; j++) {
  42. u8 mix = (crc ^ data) & 1;
  43. crc >>= 1;
  44. if (mix)
  45. crc ^= poly;
  46. data >>= 1;
  47. }
  48. }
  49. return crc;
  50. }
  51. static int ds2502_read(struct udevice *dev, u8 cmd,
  52. int bytes_in_page, int pos,
  53. u8 *buf, int bytes_for_user)
  54. {
  55. int retry;
  56. int ret = 0;
  57. for (retry = 0; retry < 3; retry++) {
  58. u8 pagebuf[DS2502_PAGE_SIZE + 1]; /* 1 byte for CRC8 */
  59. u8 crc;
  60. int i;
  61. ret = w1_reset_select(dev);
  62. if (ret)
  63. return ret;
  64. /* send read to end of page and generate CRC command */
  65. pagebuf[0] = cmd;
  66. pagebuf[1] = pos & 0xff;
  67. pagebuf[2] = pos >> 8;
  68. crc = ds2502_crc8(pagebuf, 3);
  69. for (i = 0; i < 3; i++)
  70. w1_write_byte(dev, pagebuf[i]);
  71. /* Check command CRC */
  72. ret = w1_read_byte(dev);
  73. if (ret < 0) {
  74. dev_dbg(dev, "Error %d reading command CRC\n", ret);
  75. continue;
  76. }
  77. if (ret != crc) {
  78. dev_dbg(dev,
  79. "bad CRC8 for cmd %02x got=%02X exp=%02X\n",
  80. cmd, ret, crc);
  81. ret = -EIO;
  82. continue;
  83. }
  84. /* read data and check CRC */
  85. ret = w1_read_buf(dev, pagebuf, bytes_in_page + 1);
  86. if (ret < 0) {
  87. dev_dbg(dev, "Error %d reading data\n", ret);
  88. continue;
  89. }
  90. crc = ds2502_crc8(pagebuf, bytes_in_page);
  91. if (crc == pagebuf[bytes_in_page]) {
  92. memcpy(buf, pagebuf, bytes_for_user);
  93. ret = 0;
  94. break;
  95. }
  96. dev_dbg(dev, "Bad CRC8 got=%02X exp=%02X pos=%04X\n",
  97. pagebuf[bytes_in_page], crc, pos);
  98. ret = -EIO;
  99. }
  100. return ret;
  101. }
  102. static inline int ds2502_read_status_bytes(struct udevice *dev, u8 *buf)
  103. {
  104. return ds2502_read(dev, DS2502_CMD_READ_STATUS,
  105. DS2502_STATUS_SIZE, 0,
  106. buf, DS2502_STATUS_SIZE);
  107. }
  108. /*
  109. * Status bytes (from index 1) contain 1's complement page indirection
  110. * So for N writes:
  111. * N=1: ff ff ff ff ff ff ff 00
  112. * N=2: ff fe ff ff ff ff ff 00
  113. * N=3: ff fe fd ff ff ff ff 00
  114. * N=4: ff fe fd fc ff ff ff 00
  115. */
  116. static int ds2502_indirect_page(struct udevice *dev, u8 *status, int page)
  117. {
  118. int page_seen = 0;
  119. do {
  120. u8 sb = status[page + 1];
  121. if (sb == 0xff)
  122. break;
  123. page = ~sb & 0xff;
  124. if (page >= DS2502_PAGE_COUNT) {
  125. dev_err(dev,
  126. "Illegal page redirection status byte %02x\n",
  127. sb);
  128. return -EINVAL;
  129. }
  130. if (page_seen & (1 << page)) {
  131. dev_err(dev, "Infinite loop in page redirection\n");
  132. return -EINVAL;
  133. }
  134. page_seen |= (1 << page);
  135. } while (1);
  136. return page;
  137. }
  138. static int ds2502_read_buf(struct udevice *dev, unsigned int offset,
  139. u8 *buf, unsigned int count)
  140. {
  141. unsigned int min_page = offset / DS2502_PAGE_SIZE;
  142. unsigned int max_page = (offset + count - 1) / DS2502_PAGE_SIZE;
  143. int xfered = 0;
  144. u8 status_bytes[DS2502_STATUS_SIZE];
  145. int i;
  146. int ret;
  147. if (min_page >= DS2502_PAGE_COUNT || max_page >= DS2502_PAGE_COUNT)
  148. return -EINVAL;
  149. if (min_page == 0 && max_page == 0) {
  150. ret = ds2502_read_status_bytes(dev, status_bytes);
  151. if (ret)
  152. return ret;
  153. } else {
  154. /* Dummy one to one page redirection */
  155. memset(status_bytes, 0xff, sizeof(status_bytes));
  156. }
  157. for (i = min_page; i <= max_page; i++) {
  158. int page;
  159. int pos;
  160. int bytes_in_page;
  161. int bytes_for_user;
  162. page = ds2502_indirect_page(dev, status_bytes, i);
  163. if (page < 0)
  164. return page;
  165. dev_dbg(dev, "page logical %d => physical %d\n", i, page);
  166. pos = page * DS2502_PAGE_SIZE;
  167. if (i == min_page)
  168. pos += offset % DS2502_PAGE_SIZE;
  169. bytes_in_page = DS2502_PAGE_SIZE - (pos % DS2502_PAGE_SIZE);
  170. if (i == max_page)
  171. bytes_for_user = count - xfered;
  172. else
  173. bytes_for_user = bytes_in_page;
  174. ret = ds2502_read(dev, DS2502_CMD_READ_GEN_CRC,
  175. bytes_in_page, pos,
  176. &buf[xfered], bytes_for_user);
  177. if (ret < 0)
  178. return ret;
  179. xfered += bytes_for_user;
  180. }
  181. return 0;
  182. }
  183. static int ds2502_probe(struct udevice *dev)
  184. {
  185. struct w1_device *w1;
  186. w1 = dev_get_parent_platdata(dev);
  187. w1->id = 0;
  188. return 0;
  189. }
  190. static const struct w1_eeprom_ops ds2502_ops = {
  191. .read_buf = ds2502_read_buf,
  192. };
  193. static const struct udevice_id ds2502_id[] = {
  194. { .compatible = "maxim,ds2502", .data = W1_FAMILY_DS2502 },
  195. { },
  196. };
  197. U_BOOT_DRIVER(ds2502) = {
  198. .name = "ds2502",
  199. .id = UCLASS_W1_EEPROM,
  200. .of_match = ds2502_id,
  201. .ops = &ds2502_ops,
  202. .probe = ds2502_probe,
  203. };