1 // SPDX-License-Identifier: GPL-2.0-only
3 * w1_ds2433.c - w1 family 23 (DS2433) & 43 (DS28EC20) eeprom driver
9 #include <linux/kernel.h>
10 #include <linux/module.h>
11 #include <linux/moduleparam.h>
12 #include <linux/device.h>
13 #include <linux/types.h>
14 #include <linux/delay.h>
15 #include <linux/slab.h>
16 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
17 #include <linux/crc16.h>
20 #define CRC16_VALID 0xb001
26 #define W1_EEPROM_DS2433 0x23
27 #define W1_EEPROM_DS28EC20 0x43
29 #define W1_EEPROM_DS2433_SIZE 512
30 #define W1_EEPROM_DS28EC20_SIZE 2560
32 #define W1_PAGE_SIZE 32
33 #define W1_PAGE_BITS 5
34 #define W1_PAGE_MASK 0x1F
35 #define W1_VALIDCRC_MAX 96
37 #define W1_F23_READ_EEPROM 0xF0
38 #define W1_F23_WRITE_SCRATCH 0x0F
39 #define W1_F23_READ_SCRATCH 0xAA
40 #define W1_F23_COPY_SCRATCH 0x55
42 struct ds2433_config {
43 size_t eeprom_size; /* eeprom size in bytes */
44 unsigned int page_count; /* number of 256 bits pages */
45 unsigned int tprog; /* time in ms for page programming */
48 static const struct ds2433_config config_f23 = {
49 .eeprom_size = W1_EEPROM_DS2433_SIZE,
54 static const struct ds2433_config config_f43 = {
55 .eeprom_size = W1_EEPROM_DS28EC20_SIZE,
61 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
63 DECLARE_BITMAP(validcrc, W1_VALIDCRC_MAX);
65 const struct ds2433_config *cfg;
69 * Check the file size bounds and adjusts count as needed.
70 * This would not be needed if the file size didn't reset to 0 after a write.
72 static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size)
77 if ((off + count) > size)
83 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
84 static int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data,
88 int off = block * W1_PAGE_SIZE;
90 if (test_bit(block, data->validcrc))
93 if (w1_reset_select_slave(sl)) {
94 bitmap_zero(data->validcrc, data->cfg->page_count);
98 wrbuf[0] = W1_F23_READ_EEPROM;
99 wrbuf[1] = off & 0xff;
101 w1_write_block(sl->master, wrbuf, 3);
102 w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
104 /* cache the block if the CRC is valid */
105 if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
106 set_bit(block, data->validcrc);
110 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
112 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
113 const struct bin_attribute *bin_attr, char *buf,
114 loff_t off, size_t count)
116 struct w1_slave *sl = kobj_to_w1_slave(kobj);
117 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
118 struct w1_f23_data *data = sl->family_data;
119 int i, min_page, max_page;
124 count = w1_f23_fix_count(off, count, bin_attr->size);
128 mutex_lock(&sl->master->bus_mutex);
130 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
132 min_page = (off >> W1_PAGE_BITS);
133 max_page = (off + count - 1) >> W1_PAGE_BITS;
134 for (i = min_page; i <= max_page; i++) {
135 if (w1_f23_refresh_block(sl, data, i)) {
140 memcpy(buf, &data->memory[off], count);
142 #else /* CONFIG_W1_SLAVE_DS2433_CRC */
144 /* read directly from the EEPROM */
145 if (w1_reset_select_slave(sl)) {
150 wrbuf[0] = W1_F23_READ_EEPROM;
151 wrbuf[1] = off & 0xff;
153 w1_write_block(sl->master, wrbuf, 3);
154 w1_read_block(sl->master, buf, count);
156 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
159 mutex_unlock(&sl->master->bus_mutex);
165 * w1_f23_write() - Writes to the scratchpad and reads it back for verification.
166 * @sl: The slave structure
167 * @addr: Address for the write
168 * @len: length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
169 * @data: The data to write
171 * Then copies the scratchpad to EEPROM.
172 * The data must be on one page.
173 * The master must be locked.
175 * Return: 0=Success, -1=failure
177 static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data)
179 struct w1_f23_data *f23 = sl->family_data;
181 u8 rdbuf[W1_PAGE_SIZE + 3];
182 u8 es = (addr + len - 1) & 0x1f;
184 /* Write the data to the scratchpad */
185 if (w1_reset_select_slave(sl))
188 wrbuf[0] = W1_F23_WRITE_SCRATCH;
189 wrbuf[1] = addr & 0xff;
190 wrbuf[2] = addr >> 8;
192 w1_write_block(sl->master, wrbuf, 3);
193 w1_write_block(sl->master, data, len);
195 /* Read the scratchpad and verify */
196 if (w1_reset_select_slave(sl))
199 w1_write_8(sl->master, W1_F23_READ_SCRATCH);
200 w1_read_block(sl->master, rdbuf, len + 3);
202 /* Compare what was read against the data written */
203 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
204 (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
207 /* Copy the scratchpad to EEPROM */
208 if (w1_reset_select_slave(sl))
211 wrbuf[0] = W1_F23_COPY_SCRATCH;
213 w1_write_block(sl->master, wrbuf, 4);
215 /* Sleep for tprog ms to wait for the write to complete */
216 msleep(f23->cfg->tprog);
218 /* Reset the bus to wake up the EEPROM (this may not be needed) */
219 w1_reset_bus(sl->master);
220 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
221 clear_bit(addr >> W1_PAGE_BITS, f23->validcrc);
226 static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
227 const struct bin_attribute *bin_attr, char *buf,
228 loff_t off, size_t count)
230 struct w1_slave *sl = kobj_to_w1_slave(kobj);
233 count = w1_f23_fix_count(off, count, bin_attr->size);
237 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
238 /* can only write full blocks in cached mode */
239 if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
240 dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
245 /* make sure the block CRCs are valid */
246 for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
247 if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) {
248 dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off);
252 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
254 mutex_lock(&sl->master->bus_mutex);
256 /* Can only write data to one page at a time */
258 while (idx < count) {
260 len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
261 if (len > (count - idx))
264 if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) {
272 mutex_unlock(&sl->master->bus_mutex);
277 static const struct bin_attribute bin_attr_f23_eeprom = {
278 .attr = { .name = "eeprom", .mode = 0644 },
279 .read_new = eeprom_read,
280 .write_new = eeprom_write,
281 .size = W1_EEPROM_DS2433_SIZE,
284 static const struct bin_attribute bin_attr_f43_eeprom = {
285 .attr = { .name = "eeprom", .mode = 0644 },
286 .read_new = eeprom_read,
287 .write_new = eeprom_write,
288 .size = W1_EEPROM_DS28EC20_SIZE,
291 static const struct bin_attribute *const w1_f23_bin_attributes[] = {
292 &bin_attr_f23_eeprom,
296 static const struct attribute_group w1_f23_group = {
297 .bin_attrs_new = w1_f23_bin_attributes,
300 static const struct attribute_group *w1_f23_groups[] = {
305 static const struct bin_attribute *const w1_f43_bin_attributes[] = {
306 &bin_attr_f43_eeprom,
310 static const struct attribute_group w1_f43_group = {
311 .bin_attrs_new = w1_f43_bin_attributes,
314 static const struct attribute_group *w1_f43_groups[] = {
319 static int w1_f23_add_slave(struct w1_slave *sl)
321 struct w1_f23_data *data;
323 data = kzalloc(sizeof(struct w1_f23_data), GFP_KERNEL);
327 switch (sl->family->fid) {
328 case W1_EEPROM_DS2433:
329 data->cfg = &config_f23;
331 case W1_EEPROM_DS28EC20:
332 data->cfg = &config_f43;
336 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
337 if (data->cfg->page_count > W1_VALIDCRC_MAX) {
338 dev_err(&sl->dev, "page count too big for crc bitmap\n");
342 data->memory = kzalloc(data->cfg->eeprom_size, GFP_KERNEL);
347 bitmap_zero(data->validcrc, data->cfg->page_count);
348 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
349 sl->family_data = data;
354 static void w1_f23_remove_slave(struct w1_slave *sl)
356 struct w1_f23_data *data = sl->family_data;
357 sl->family_data = NULL;
358 #ifdef CONFIG_W1_SLAVE_DS2433_CRC
360 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */
364 static const struct w1_family_ops w1_f23_fops = {
365 .add_slave = w1_f23_add_slave,
366 .remove_slave = w1_f23_remove_slave,
367 .groups = w1_f23_groups,
370 static const struct w1_family_ops w1_f43_fops = {
371 .add_slave = w1_f23_add_slave,
372 .remove_slave = w1_f23_remove_slave,
373 .groups = w1_f43_groups,
376 static struct w1_family w1_family_23 = {
377 .fid = W1_EEPROM_DS2433,
378 .fops = &w1_f23_fops,
381 static struct w1_family w1_family_43 = {
382 .fid = W1_EEPROM_DS28EC20,
383 .fops = &w1_f43_fops,
386 static int __init w1_ds2433_init(void)
390 err = w1_register_family(&w1_family_23);
394 err = w1_register_family(&w1_family_43);
401 w1_unregister_family(&w1_family_23);
405 static void __exit w1_ds2433_exit(void)
407 w1_unregister_family(&w1_family_23);
408 w1_unregister_family(&w1_family_43);
411 module_init(w1_ds2433_init);
412 module_exit(w1_ds2433_exit);
416 MODULE_DESCRIPTION("w1 family 23/43 driver for DS2433 (4kb) and DS28EC20 (20kb)");
417 MODULE_LICENSE("GPL");
418 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2433));
419 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS28EC20));