18c2ecf20Sopenharmony_ci// SPDX-License-Identifier: GPL-2.0-only 28c2ecf20Sopenharmony_ci/* 38c2ecf20Sopenharmony_ci * w1_ds2433.c - w1 family 23 (DS2433) driver 48c2ecf20Sopenharmony_ci * 58c2ecf20Sopenharmony_ci * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com> 68c2ecf20Sopenharmony_ci */ 78c2ecf20Sopenharmony_ci 88c2ecf20Sopenharmony_ci#include <linux/kernel.h> 98c2ecf20Sopenharmony_ci#include <linux/module.h> 108c2ecf20Sopenharmony_ci#include <linux/moduleparam.h> 118c2ecf20Sopenharmony_ci#include <linux/device.h> 128c2ecf20Sopenharmony_ci#include <linux/types.h> 138c2ecf20Sopenharmony_ci#include <linux/delay.h> 148c2ecf20Sopenharmony_ci#include <linux/slab.h> 158c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 168c2ecf20Sopenharmony_ci#include <linux/crc16.h> 178c2ecf20Sopenharmony_ci 188c2ecf20Sopenharmony_ci#define CRC16_INIT 0 198c2ecf20Sopenharmony_ci#define CRC16_VALID 0xb001 208c2ecf20Sopenharmony_ci 218c2ecf20Sopenharmony_ci#endif 228c2ecf20Sopenharmony_ci 238c2ecf20Sopenharmony_ci#include <linux/w1.h> 248c2ecf20Sopenharmony_ci 258c2ecf20Sopenharmony_ci#define W1_EEPROM_DS2433 0x23 268c2ecf20Sopenharmony_ci 278c2ecf20Sopenharmony_ci#define W1_EEPROM_SIZE 512 288c2ecf20Sopenharmony_ci#define W1_PAGE_COUNT 16 298c2ecf20Sopenharmony_ci#define W1_PAGE_SIZE 32 308c2ecf20Sopenharmony_ci#define W1_PAGE_BITS 5 318c2ecf20Sopenharmony_ci#define W1_PAGE_MASK 0x1F 328c2ecf20Sopenharmony_ci 338c2ecf20Sopenharmony_ci#define W1_F23_TIME 300 348c2ecf20Sopenharmony_ci 358c2ecf20Sopenharmony_ci#define W1_F23_READ_EEPROM 0xF0 368c2ecf20Sopenharmony_ci#define W1_F23_WRITE_SCRATCH 0x0F 378c2ecf20Sopenharmony_ci#define W1_F23_READ_SCRATCH 0xAA 388c2ecf20Sopenharmony_ci#define W1_F23_COPY_SCRATCH 0x55 398c2ecf20Sopenharmony_ci 408c2ecf20Sopenharmony_cistruct w1_f23_data { 418c2ecf20Sopenharmony_ci u8 memory[W1_EEPROM_SIZE]; 428c2ecf20Sopenharmony_ci u32 validcrc; 438c2ecf20Sopenharmony_ci}; 448c2ecf20Sopenharmony_ci 458c2ecf20Sopenharmony_ci/** 468c2ecf20Sopenharmony_ci * Check the file size bounds and adjusts count as needed. 478c2ecf20Sopenharmony_ci * This would not be needed if the file size didn't reset to 0 after a write. 488c2ecf20Sopenharmony_ci */ 498c2ecf20Sopenharmony_cistatic inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size) 508c2ecf20Sopenharmony_ci{ 518c2ecf20Sopenharmony_ci if (off > size) 528c2ecf20Sopenharmony_ci return 0; 538c2ecf20Sopenharmony_ci 548c2ecf20Sopenharmony_ci if ((off + count) > size) 558c2ecf20Sopenharmony_ci return (size - off); 568c2ecf20Sopenharmony_ci 578c2ecf20Sopenharmony_ci return count; 588c2ecf20Sopenharmony_ci} 598c2ecf20Sopenharmony_ci 608c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 618c2ecf20Sopenharmony_cistatic int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data, 628c2ecf20Sopenharmony_ci int block) 638c2ecf20Sopenharmony_ci{ 648c2ecf20Sopenharmony_ci u8 wrbuf[3]; 658c2ecf20Sopenharmony_ci int off = block * W1_PAGE_SIZE; 668c2ecf20Sopenharmony_ci 678c2ecf20Sopenharmony_ci if (data->validcrc & (1 << block)) 688c2ecf20Sopenharmony_ci return 0; 698c2ecf20Sopenharmony_ci 708c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) { 718c2ecf20Sopenharmony_ci data->validcrc = 0; 728c2ecf20Sopenharmony_ci return -EIO; 738c2ecf20Sopenharmony_ci } 748c2ecf20Sopenharmony_ci 758c2ecf20Sopenharmony_ci wrbuf[0] = W1_F23_READ_EEPROM; 768c2ecf20Sopenharmony_ci wrbuf[1] = off & 0xff; 778c2ecf20Sopenharmony_ci wrbuf[2] = off >> 8; 788c2ecf20Sopenharmony_ci w1_write_block(sl->master, wrbuf, 3); 798c2ecf20Sopenharmony_ci w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE); 808c2ecf20Sopenharmony_ci 818c2ecf20Sopenharmony_ci /* cache the block if the CRC is valid */ 828c2ecf20Sopenharmony_ci if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID) 838c2ecf20Sopenharmony_ci data->validcrc |= (1 << block); 848c2ecf20Sopenharmony_ci 858c2ecf20Sopenharmony_ci return 0; 868c2ecf20Sopenharmony_ci} 878c2ecf20Sopenharmony_ci#endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 888c2ecf20Sopenharmony_ci 898c2ecf20Sopenharmony_cistatic ssize_t eeprom_read(struct file *filp, struct kobject *kobj, 908c2ecf20Sopenharmony_ci struct bin_attribute *bin_attr, char *buf, 918c2ecf20Sopenharmony_ci loff_t off, size_t count) 928c2ecf20Sopenharmony_ci{ 938c2ecf20Sopenharmony_ci struct w1_slave *sl = kobj_to_w1_slave(kobj); 948c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 958c2ecf20Sopenharmony_ci struct w1_f23_data *data = sl->family_data; 968c2ecf20Sopenharmony_ci int i, min_page, max_page; 978c2ecf20Sopenharmony_ci#else 988c2ecf20Sopenharmony_ci u8 wrbuf[3]; 998c2ecf20Sopenharmony_ci#endif 1008c2ecf20Sopenharmony_ci 1018c2ecf20Sopenharmony_ci if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0) 1028c2ecf20Sopenharmony_ci return 0; 1038c2ecf20Sopenharmony_ci 1048c2ecf20Sopenharmony_ci mutex_lock(&sl->master->bus_mutex); 1058c2ecf20Sopenharmony_ci 1068c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 1078c2ecf20Sopenharmony_ci 1088c2ecf20Sopenharmony_ci min_page = (off >> W1_PAGE_BITS); 1098c2ecf20Sopenharmony_ci max_page = (off + count - 1) >> W1_PAGE_BITS; 1108c2ecf20Sopenharmony_ci for (i = min_page; i <= max_page; i++) { 1118c2ecf20Sopenharmony_ci if (w1_f23_refresh_block(sl, data, i)) { 1128c2ecf20Sopenharmony_ci count = -EIO; 1138c2ecf20Sopenharmony_ci goto out_up; 1148c2ecf20Sopenharmony_ci } 1158c2ecf20Sopenharmony_ci } 1168c2ecf20Sopenharmony_ci memcpy(buf, &data->memory[off], count); 1178c2ecf20Sopenharmony_ci 1188c2ecf20Sopenharmony_ci#else /* CONFIG_W1_SLAVE_DS2433_CRC */ 1198c2ecf20Sopenharmony_ci 1208c2ecf20Sopenharmony_ci /* read directly from the EEPROM */ 1218c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) { 1228c2ecf20Sopenharmony_ci count = -EIO; 1238c2ecf20Sopenharmony_ci goto out_up; 1248c2ecf20Sopenharmony_ci } 1258c2ecf20Sopenharmony_ci 1268c2ecf20Sopenharmony_ci wrbuf[0] = W1_F23_READ_EEPROM; 1278c2ecf20Sopenharmony_ci wrbuf[1] = off & 0xff; 1288c2ecf20Sopenharmony_ci wrbuf[2] = off >> 8; 1298c2ecf20Sopenharmony_ci w1_write_block(sl->master, wrbuf, 3); 1308c2ecf20Sopenharmony_ci w1_read_block(sl->master, buf, count); 1318c2ecf20Sopenharmony_ci 1328c2ecf20Sopenharmony_ci#endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 1338c2ecf20Sopenharmony_ci 1348c2ecf20Sopenharmony_ciout_up: 1358c2ecf20Sopenharmony_ci mutex_unlock(&sl->master->bus_mutex); 1368c2ecf20Sopenharmony_ci 1378c2ecf20Sopenharmony_ci return count; 1388c2ecf20Sopenharmony_ci} 1398c2ecf20Sopenharmony_ci 1408c2ecf20Sopenharmony_ci/** 1418c2ecf20Sopenharmony_ci * Writes to the scratchpad and reads it back for verification. 1428c2ecf20Sopenharmony_ci * Then copies the scratchpad to EEPROM. 1438c2ecf20Sopenharmony_ci * The data must be on one page. 1448c2ecf20Sopenharmony_ci * The master must be locked. 1458c2ecf20Sopenharmony_ci * 1468c2ecf20Sopenharmony_ci * @param sl The slave structure 1478c2ecf20Sopenharmony_ci * @param addr Address for the write 1488c2ecf20Sopenharmony_ci * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK)) 1498c2ecf20Sopenharmony_ci * @param data The data to write 1508c2ecf20Sopenharmony_ci * @return 0=Success -1=failure 1518c2ecf20Sopenharmony_ci */ 1528c2ecf20Sopenharmony_cistatic int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data) 1538c2ecf20Sopenharmony_ci{ 1548c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 1558c2ecf20Sopenharmony_ci struct w1_f23_data *f23 = sl->family_data; 1568c2ecf20Sopenharmony_ci#endif 1578c2ecf20Sopenharmony_ci u8 wrbuf[4]; 1588c2ecf20Sopenharmony_ci u8 rdbuf[W1_PAGE_SIZE + 3]; 1598c2ecf20Sopenharmony_ci u8 es = (addr + len - 1) & 0x1f; 1608c2ecf20Sopenharmony_ci 1618c2ecf20Sopenharmony_ci /* Write the data to the scratchpad */ 1628c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) 1638c2ecf20Sopenharmony_ci return -1; 1648c2ecf20Sopenharmony_ci 1658c2ecf20Sopenharmony_ci wrbuf[0] = W1_F23_WRITE_SCRATCH; 1668c2ecf20Sopenharmony_ci wrbuf[1] = addr & 0xff; 1678c2ecf20Sopenharmony_ci wrbuf[2] = addr >> 8; 1688c2ecf20Sopenharmony_ci 1698c2ecf20Sopenharmony_ci w1_write_block(sl->master, wrbuf, 3); 1708c2ecf20Sopenharmony_ci w1_write_block(sl->master, data, len); 1718c2ecf20Sopenharmony_ci 1728c2ecf20Sopenharmony_ci /* Read the scratchpad and verify */ 1738c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) 1748c2ecf20Sopenharmony_ci return -1; 1758c2ecf20Sopenharmony_ci 1768c2ecf20Sopenharmony_ci w1_write_8(sl->master, W1_F23_READ_SCRATCH); 1778c2ecf20Sopenharmony_ci w1_read_block(sl->master, rdbuf, len + 3); 1788c2ecf20Sopenharmony_ci 1798c2ecf20Sopenharmony_ci /* Compare what was read against the data written */ 1808c2ecf20Sopenharmony_ci if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) || 1818c2ecf20Sopenharmony_ci (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0)) 1828c2ecf20Sopenharmony_ci return -1; 1838c2ecf20Sopenharmony_ci 1848c2ecf20Sopenharmony_ci /* Copy the scratchpad to EEPROM */ 1858c2ecf20Sopenharmony_ci if (w1_reset_select_slave(sl)) 1868c2ecf20Sopenharmony_ci return -1; 1878c2ecf20Sopenharmony_ci 1888c2ecf20Sopenharmony_ci wrbuf[0] = W1_F23_COPY_SCRATCH; 1898c2ecf20Sopenharmony_ci wrbuf[3] = es; 1908c2ecf20Sopenharmony_ci w1_write_block(sl->master, wrbuf, 4); 1918c2ecf20Sopenharmony_ci 1928c2ecf20Sopenharmony_ci /* Sleep for 5 ms to wait for the write to complete */ 1938c2ecf20Sopenharmony_ci msleep(5); 1948c2ecf20Sopenharmony_ci 1958c2ecf20Sopenharmony_ci /* Reset the bus to wake up the EEPROM (this may not be needed) */ 1968c2ecf20Sopenharmony_ci w1_reset_bus(sl->master); 1978c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 1988c2ecf20Sopenharmony_ci f23->validcrc &= ~(1 << (addr >> W1_PAGE_BITS)); 1998c2ecf20Sopenharmony_ci#endif 2008c2ecf20Sopenharmony_ci return 0; 2018c2ecf20Sopenharmony_ci} 2028c2ecf20Sopenharmony_ci 2038c2ecf20Sopenharmony_cistatic ssize_t eeprom_write(struct file *filp, struct kobject *kobj, 2048c2ecf20Sopenharmony_ci struct bin_attribute *bin_attr, char *buf, 2058c2ecf20Sopenharmony_ci loff_t off, size_t count) 2068c2ecf20Sopenharmony_ci{ 2078c2ecf20Sopenharmony_ci struct w1_slave *sl = kobj_to_w1_slave(kobj); 2088c2ecf20Sopenharmony_ci int addr, len, idx; 2098c2ecf20Sopenharmony_ci 2108c2ecf20Sopenharmony_ci if ((count = w1_f23_fix_count(off, count, W1_EEPROM_SIZE)) == 0) 2118c2ecf20Sopenharmony_ci return 0; 2128c2ecf20Sopenharmony_ci 2138c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 2148c2ecf20Sopenharmony_ci /* can only write full blocks in cached mode */ 2158c2ecf20Sopenharmony_ci if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) { 2168c2ecf20Sopenharmony_ci dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n", 2178c2ecf20Sopenharmony_ci (int)off, count); 2188c2ecf20Sopenharmony_ci return -EINVAL; 2198c2ecf20Sopenharmony_ci } 2208c2ecf20Sopenharmony_ci 2218c2ecf20Sopenharmony_ci /* make sure the block CRCs are valid */ 2228c2ecf20Sopenharmony_ci for (idx = 0; idx < count; idx += W1_PAGE_SIZE) { 2238c2ecf20Sopenharmony_ci if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) { 2248c2ecf20Sopenharmony_ci dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off); 2258c2ecf20Sopenharmony_ci return -EINVAL; 2268c2ecf20Sopenharmony_ci } 2278c2ecf20Sopenharmony_ci } 2288c2ecf20Sopenharmony_ci#endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 2298c2ecf20Sopenharmony_ci 2308c2ecf20Sopenharmony_ci mutex_lock(&sl->master->bus_mutex); 2318c2ecf20Sopenharmony_ci 2328c2ecf20Sopenharmony_ci /* Can only write data to one page at a time */ 2338c2ecf20Sopenharmony_ci idx = 0; 2348c2ecf20Sopenharmony_ci while (idx < count) { 2358c2ecf20Sopenharmony_ci addr = off + idx; 2368c2ecf20Sopenharmony_ci len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK); 2378c2ecf20Sopenharmony_ci if (len > (count - idx)) 2388c2ecf20Sopenharmony_ci len = count - idx; 2398c2ecf20Sopenharmony_ci 2408c2ecf20Sopenharmony_ci if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) { 2418c2ecf20Sopenharmony_ci count = -EIO; 2428c2ecf20Sopenharmony_ci goto out_up; 2438c2ecf20Sopenharmony_ci } 2448c2ecf20Sopenharmony_ci idx += len; 2458c2ecf20Sopenharmony_ci } 2468c2ecf20Sopenharmony_ci 2478c2ecf20Sopenharmony_ciout_up: 2488c2ecf20Sopenharmony_ci mutex_unlock(&sl->master->bus_mutex); 2498c2ecf20Sopenharmony_ci 2508c2ecf20Sopenharmony_ci return count; 2518c2ecf20Sopenharmony_ci} 2528c2ecf20Sopenharmony_ci 2538c2ecf20Sopenharmony_cistatic BIN_ATTR_RW(eeprom, W1_EEPROM_SIZE); 2548c2ecf20Sopenharmony_ci 2558c2ecf20Sopenharmony_cistatic struct bin_attribute *w1_f23_bin_attributes[] = { 2568c2ecf20Sopenharmony_ci &bin_attr_eeprom, 2578c2ecf20Sopenharmony_ci NULL, 2588c2ecf20Sopenharmony_ci}; 2598c2ecf20Sopenharmony_ci 2608c2ecf20Sopenharmony_cistatic const struct attribute_group w1_f23_group = { 2618c2ecf20Sopenharmony_ci .bin_attrs = w1_f23_bin_attributes, 2628c2ecf20Sopenharmony_ci}; 2638c2ecf20Sopenharmony_ci 2648c2ecf20Sopenharmony_cistatic const struct attribute_group *w1_f23_groups[] = { 2658c2ecf20Sopenharmony_ci &w1_f23_group, 2668c2ecf20Sopenharmony_ci NULL, 2678c2ecf20Sopenharmony_ci}; 2688c2ecf20Sopenharmony_ci 2698c2ecf20Sopenharmony_cistatic int w1_f23_add_slave(struct w1_slave *sl) 2708c2ecf20Sopenharmony_ci{ 2718c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 2728c2ecf20Sopenharmony_ci struct w1_f23_data *data; 2738c2ecf20Sopenharmony_ci 2748c2ecf20Sopenharmony_ci data = kzalloc(sizeof(struct w1_f23_data), GFP_KERNEL); 2758c2ecf20Sopenharmony_ci if (!data) 2768c2ecf20Sopenharmony_ci return -ENOMEM; 2778c2ecf20Sopenharmony_ci sl->family_data = data; 2788c2ecf20Sopenharmony_ci 2798c2ecf20Sopenharmony_ci#endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 2808c2ecf20Sopenharmony_ci return 0; 2818c2ecf20Sopenharmony_ci} 2828c2ecf20Sopenharmony_ci 2838c2ecf20Sopenharmony_cistatic void w1_f23_remove_slave(struct w1_slave *sl) 2848c2ecf20Sopenharmony_ci{ 2858c2ecf20Sopenharmony_ci#ifdef CONFIG_W1_SLAVE_DS2433_CRC 2868c2ecf20Sopenharmony_ci kfree(sl->family_data); 2878c2ecf20Sopenharmony_ci sl->family_data = NULL; 2888c2ecf20Sopenharmony_ci#endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 2898c2ecf20Sopenharmony_ci} 2908c2ecf20Sopenharmony_ci 2918c2ecf20Sopenharmony_cistatic const struct w1_family_ops w1_f23_fops = { 2928c2ecf20Sopenharmony_ci .add_slave = w1_f23_add_slave, 2938c2ecf20Sopenharmony_ci .remove_slave = w1_f23_remove_slave, 2948c2ecf20Sopenharmony_ci .groups = w1_f23_groups, 2958c2ecf20Sopenharmony_ci}; 2968c2ecf20Sopenharmony_ci 2978c2ecf20Sopenharmony_cistatic struct w1_family w1_family_23 = { 2988c2ecf20Sopenharmony_ci .fid = W1_EEPROM_DS2433, 2998c2ecf20Sopenharmony_ci .fops = &w1_f23_fops, 3008c2ecf20Sopenharmony_ci}; 3018c2ecf20Sopenharmony_cimodule_w1_family(w1_family_23); 3028c2ecf20Sopenharmony_ci 3038c2ecf20Sopenharmony_ciMODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>"); 3048c2ecf20Sopenharmony_ciMODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM"); 3058c2ecf20Sopenharmony_ciMODULE_LICENSE("GPL"); 3068c2ecf20Sopenharmony_ciMODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2433)); 307