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