1 // SPDX-License-Identifier: GPL-2.0+
3 * Driver for DS-2502 One wire "Add only Memory".
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.
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).
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,
19 * Copyright (c) 2018 Flowbird
25 #include <dm/device_compat.h>
26 #include <linux/err.h>
27 #include <w1-eeprom.h>
30 #define DS2502_PAGE_SIZE 32
31 #define DS2502_PAGE_COUNT 4
32 #define DS2502_STATUS_SIZE 8
34 #define DS2502_CMD_READ_STATUS 0xAA
35 #define DS2502_CMD_READ_GEN_CRC 0xC3
37 /* u-boot crc8() is CCITT CRC8, we need x^8 + x^5 + x^4 + 1 LSB first */
38 static unsigned int ds2502_crc8(const u8 *buf, int len)
40 static const u8 poly = 0x8C; /* (1 + x^4 + x^5) + x^8 */
44 for (i = 0; i < len; i++) {
48 for (j = 0; j < 8; j++) {
49 u8 mix = (crc ^ data) & 1;
60 static int ds2502_read(struct udevice *dev, u8 cmd,
61 int bytes_in_page, int pos,
62 u8 *buf, int bytes_for_user)
67 for (retry = 0; retry < 3; retry++) {
68 u8 pagebuf[DS2502_PAGE_SIZE + 1]; /* 1 byte for CRC8 */
72 ret = w1_reset_select(dev);
76 /* send read to end of page and generate CRC command */
78 pagebuf[1] = pos & 0xff;
79 pagebuf[2] = pos >> 8;
80 crc = ds2502_crc8(pagebuf, 3);
81 for (i = 0; i < 3; i++)
82 w1_write_byte(dev, pagebuf[i]);
84 /* Check command CRC */
85 ret = w1_read_byte(dev);
87 dev_dbg(dev, "Error %d reading command CRC\n", ret);
93 "bad CRC8 for cmd %02x got=%02X exp=%02X\n",
99 /* read data and check CRC */
100 ret = w1_read_buf(dev, pagebuf, bytes_in_page + 1);
102 dev_dbg(dev, "Error %d reading data\n", ret);
106 crc = ds2502_crc8(pagebuf, bytes_in_page);
107 if (crc == pagebuf[bytes_in_page]) {
108 memcpy(buf, pagebuf, bytes_for_user);
112 dev_dbg(dev, "Bad CRC8 got=%02X exp=%02X pos=%04X\n",
113 pagebuf[bytes_in_page], crc, pos);
120 static inline int ds2502_read_status_bytes(struct udevice *dev, u8 *buf)
122 return ds2502_read(dev, DS2502_CMD_READ_STATUS,
123 DS2502_STATUS_SIZE, 0,
124 buf, DS2502_STATUS_SIZE);
128 * Status bytes (from index 1) contain 1's complement page indirection
130 * N=1: ff ff ff ff ff ff ff 00
131 * N=2: ff fe ff ff ff ff ff 00
132 * N=3: ff fe fd ff ff ff ff 00
133 * N=4: ff fe fd fc ff ff ff 00
135 static int ds2502_indirect_page(struct udevice *dev, u8 *status, int page)
140 u8 sb = status[page + 1];
147 if (page >= DS2502_PAGE_COUNT) {
149 "Illegal page redirection status byte %02x\n",
154 if (page_seen & (1 << page)) {
155 dev_err(dev, "Infinite loop in page redirection\n");
159 page_seen |= (1 << page);
165 static int ds2502_read_buf(struct udevice *dev, unsigned int offset,
166 u8 *buf, unsigned int count)
168 unsigned int min_page = offset / DS2502_PAGE_SIZE;
169 unsigned int max_page = (offset + count - 1) / DS2502_PAGE_SIZE;
171 u8 status_bytes[DS2502_STATUS_SIZE];
175 if (min_page >= DS2502_PAGE_COUNT || max_page >= DS2502_PAGE_COUNT)
178 if (min_page == 0 && max_page == 0) {
179 ret = ds2502_read_status_bytes(dev, status_bytes);
183 /* Dummy one to one page redirection */
184 memset(status_bytes, 0xff, sizeof(status_bytes));
187 for (i = min_page; i <= max_page; i++) {
193 page = ds2502_indirect_page(dev, status_bytes, i);
196 dev_dbg(dev, "page logical %d => physical %d\n", i, page);
198 pos = page * DS2502_PAGE_SIZE;
200 pos += offset % DS2502_PAGE_SIZE;
202 bytes_in_page = DS2502_PAGE_SIZE - (pos % DS2502_PAGE_SIZE);
205 bytes_for_user = count - xfered;
207 bytes_for_user = bytes_in_page;
209 ret = ds2502_read(dev, DS2502_CMD_READ_GEN_CRC,
211 &buf[xfered], bytes_for_user);
215 xfered += bytes_for_user;
221 static int ds2502_probe(struct udevice *dev)
223 struct w1_device *w1;
225 w1 = dev_get_parent_plat(dev);
230 static const struct w1_eeprom_ops ds2502_ops = {
231 .read_buf = ds2502_read_buf,
234 static const struct udevice_id ds2502_id[] = {
235 { .compatible = "maxim,ds2502", .data = W1_FAMILY_DS2502 },
239 U_BOOT_DRIVER(ds2502) = {
241 .id = UCLASS_W1_EEPROM,
242 .of_match = ds2502_id,
244 .probe = ds2502_probe,