]>
git.ipfire.org Git - thirdparty/u-boot.git/blob - drivers/w1-eeprom/ds2502.c
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
20 * Martin Fuzzey <martin.fuzzey@flowbird.group>
25 #include <linux/err.h>
26 #include <w1-eeprom.h>
29 #define DS2502_PAGE_SIZE 32
30 #define DS2502_PAGE_COUNT 4
31 #define DS2502_STATUS_SIZE 8
33 #define DS2502_CMD_READ_STATUS 0xAA
34 #define DS2502_CMD_READ_GEN_CRC 0xC3
36 /* u-boot crc8() is CCITT CRC8, we need x^8 + x^5 + x^4 + 1 LSB first */
37 static unsigned int ds2502_crc8(const u8
*buf
, int len
)
39 static const u8 poly
= 0x8C; /* (1 + x^4 + x^5) + x^8 */
43 for (i
= 0; i
< len
; i
++) {
47 for (j
= 0; j
< 8; j
++) {
48 u8 mix
= (crc
^ data
) & 1;
59 static int ds2502_read(struct udevice
*dev
, u8 cmd
,
60 int bytes_in_page
, int pos
,
61 u8
*buf
, int bytes_for_user
)
66 for (retry
= 0; retry
< 3; retry
++) {
67 u8 pagebuf
[DS2502_PAGE_SIZE
+ 1]; /* 1 byte for CRC8 */
71 ret
= w1_reset_select(dev
);
75 /* send read to end of page and generate CRC command */
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
]);
83 /* Check command CRC */
84 ret
= w1_read_byte(dev
);
86 dev_dbg(dev
, "Error %d reading command CRC\n", ret
);
92 "bad CRC8 for cmd %02x got=%02X exp=%02X\n",
98 /* read data and check CRC */
99 ret
= w1_read_buf(dev
, pagebuf
, bytes_in_page
+ 1);
101 dev_dbg(dev
, "Error %d reading data\n", ret
);
105 crc
= ds2502_crc8(pagebuf
, bytes_in_page
);
106 if (crc
== pagebuf
[bytes_in_page
]) {
107 memcpy(buf
, pagebuf
, bytes_for_user
);
111 dev_dbg(dev
, "Bad CRC8 got=%02X exp=%02X pos=%04X\n",
112 pagebuf
[bytes_in_page
], crc
, pos
);
119 static inline int ds2502_read_status_bytes(struct udevice
*dev
, u8
*buf
)
121 return ds2502_read(dev
, DS2502_CMD_READ_STATUS
,
122 DS2502_STATUS_SIZE
, 0,
123 buf
, DS2502_STATUS_SIZE
);
127 * Status bytes (from index 1) contain 1's complement page indirection
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
134 static int ds2502_indirect_page(struct udevice
*dev
, u8
*status
, int page
)
139 u8 sb
= status
[page
+ 1];
146 if (page
>= DS2502_PAGE_COUNT
) {
148 "Illegal page redirection status byte %02x\n",
153 if (page_seen
& (1 << page
)) {
154 dev_err(dev
, "Infinite loop in page redirection\n");
158 page_seen
|= (1 << page
);
164 static int ds2502_read_buf(struct udevice
*dev
, unsigned int offset
,
165 u8
*buf
, unsigned int count
)
167 unsigned int min_page
= offset
/ DS2502_PAGE_SIZE
;
168 unsigned int max_page
= (offset
+ count
- 1) / DS2502_PAGE_SIZE
;
170 u8 status_bytes
[DS2502_STATUS_SIZE
];
174 if (min_page
>= DS2502_PAGE_COUNT
|| max_page
>= DS2502_PAGE_COUNT
)
177 if (min_page
== 0 && max_page
== 0) {
178 ret
= ds2502_read_status_bytes(dev
, status_bytes
);
182 /* Dummy one to one page redirection */
183 memset(status_bytes
, 0xff, sizeof(status_bytes
));
186 for (i
= min_page
; i
<= max_page
; i
++) {
192 page
= ds2502_indirect_page(dev
, status_bytes
, i
);
195 dev_dbg(dev
, "page logical %d => physical %d\n", i
, page
);
197 pos
= page
* DS2502_PAGE_SIZE
;
199 pos
+= offset
% DS2502_PAGE_SIZE
;
201 bytes_in_page
= DS2502_PAGE_SIZE
- (pos
% DS2502_PAGE_SIZE
);
204 bytes_for_user
= count
- xfered
;
206 bytes_for_user
= bytes_in_page
;
208 ret
= ds2502_read(dev
, DS2502_CMD_READ_GEN_CRC
,
210 &buf
[xfered
], bytes_for_user
);
214 xfered
+= bytes_for_user
;
220 static int ds2502_probe(struct udevice
*dev
)
222 struct w1_device
*w1
;
224 w1
= dev_get_parent_platdata(dev
);
229 static const struct w1_eeprom_ops ds2502_ops
= {
230 .read_buf
= ds2502_read_buf
,
233 static const struct udevice_id ds2502_id
[] = {
234 { .compatible
= "maxim,ds2502", .data
= W1_FAMILY_DS2502
},
238 U_BOOT_DRIVER(ds2502
) = {
240 .id
= UCLASS_W1_EEPROM
,
241 .of_match
= ds2502_id
,
243 .probe
= ds2502_probe
,