1 // SPDX-License-Identifier: GPL-2.0-only 2 /* 3 * w1_ds2433.c - w1 family 23 (DS2433) driver 4 * 5 * Copyright (c) 2005 Ben Gardner <bgardner@wabtec.com> 6 */ 7 8 #include <linux/kernel.h> 9 #include <linux/module.h> 10 #include <linux/moduleparam.h> 11 #include <linux/device.h> 12 #include <linux/types.h> 13 #include <linux/delay.h> 14 #include <linux/slab.h> 15 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 16 #include <linux/crc16.h> 17 18 #define CRC16_INIT 0 19 #define CRC16_VALID 0xb001 20 21 #endif 22 23 #include <linux/w1.h> 24 25 #define W1_EEPROM_DS2433 0x23 26 27 #define W1_EEPROM_SIZE 512 28 #define W1_PAGE_SIZE 32 29 #define W1_PAGE_BITS 5 30 #define W1_PAGE_MASK 0x1F 31 #define W1_VALIDCRC_MAX 32 32 33 #define W1_F23_READ_EEPROM 0xF0 34 #define W1_F23_WRITE_SCRATCH 0x0F 35 #define W1_F23_READ_SCRATCH 0xAA 36 #define W1_F23_COPY_SCRATCH 0x55 37 38 struct ds2433_config { 39 size_t eeprom_size; /* eeprom size in bytes */ 40 unsigned int page_count; /* number of 256 bits pages */ 41 unsigned int tprog; /* time in ms for page programming */ 42 }; 43 44 static const struct ds2433_config config_f23 = { 45 .eeprom_size = W1_EEPROM_SIZE, 46 .page_count = 16, 47 .tprog = 5, 48 }; 49 50 struct w1_f23_data { 51 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 52 u8 *memory; 53 DECLARE_BITMAP(validcrc, W1_VALIDCRC_MAX); 54 #endif 55 const struct ds2433_config *cfg; 56 }; 57 58 /* 59 * Check the file size bounds and adjusts count as needed. 60 * This would not be needed if the file size didn't reset to 0 after a write. 61 */ 62 static inline size_t w1_f23_fix_count(loff_t off, size_t count, size_t size) 63 { 64 if (off > size) 65 return 0; 66 67 if ((off + count) > size) 68 return (size - off); 69 70 return count; 71 } 72 73 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 74 static int w1_f23_refresh_block(struct w1_slave *sl, struct w1_f23_data *data, 75 int block) 76 { 77 u8 wrbuf[3]; 78 int off = block * W1_PAGE_SIZE; 79 80 if (test_bit(block, data->validcrc)) 81 return 0; 82 83 if (w1_reset_select_slave(sl)) { 84 bitmap_zero(data->validcrc, data->cfg->page_count); 85 return -EIO; 86 } 87 88 wrbuf[0] = W1_F23_READ_EEPROM; 89 wrbuf[1] = off & 0xff; 90 wrbuf[2] = off >> 8; 91 w1_write_block(sl->master, wrbuf, 3); 92 w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE); 93 94 /* cache the block if the CRC is valid */ 95 if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID) 96 set_bit(block, data->validcrc); 97 98 return 0; 99 } 100 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 101 102 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj, 103 struct bin_attribute *bin_attr, char *buf, 104 loff_t off, size_t count) 105 { 106 struct w1_slave *sl = kobj_to_w1_slave(kobj); 107 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 108 struct w1_f23_data *data = sl->family_data; 109 int i, min_page, max_page; 110 #else 111 u8 wrbuf[3]; 112 #endif 113 114 count = w1_f23_fix_count(off, count, bin_attr->size); 115 if (!count) 116 return 0; 117 118 mutex_lock(&sl->master->bus_mutex); 119 120 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 121 122 min_page = (off >> W1_PAGE_BITS); 123 max_page = (off + count - 1) >> W1_PAGE_BITS; 124 for (i = min_page; i <= max_page; i++) { 125 if (w1_f23_refresh_block(sl, data, i)) { 126 count = -EIO; 127 goto out_up; 128 } 129 } 130 memcpy(buf, &data->memory[off], count); 131 132 #else /* CONFIG_W1_SLAVE_DS2433_CRC */ 133 134 /* read directly from the EEPROM */ 135 if (w1_reset_select_slave(sl)) { 136 count = -EIO; 137 goto out_up; 138 } 139 140 wrbuf[0] = W1_F23_READ_EEPROM; 141 wrbuf[1] = off & 0xff; 142 wrbuf[2] = off >> 8; 143 w1_write_block(sl->master, wrbuf, 3); 144 w1_read_block(sl->master, buf, count); 145 146 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 147 148 out_up: 149 mutex_unlock(&sl->master->bus_mutex); 150 151 return count; 152 } 153 154 /** 155 * w1_f23_write() - Writes to the scratchpad and reads it back for verification. 156 * @sl: The slave structure 157 * @addr: Address for the write 158 * @len: length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK)) 159 * @data: The data to write 160 * 161 * Then copies the scratchpad to EEPROM. 162 * The data must be on one page. 163 * The master must be locked. 164 * 165 * Return: 0=Success, -1=failure 166 */ 167 static int w1_f23_write(struct w1_slave *sl, int addr, int len, const u8 *data) 168 { 169 struct w1_f23_data *f23 = sl->family_data; 170 u8 wrbuf[4]; 171 u8 rdbuf[W1_PAGE_SIZE + 3]; 172 u8 es = (addr + len - 1) & 0x1f; 173 174 /* Write the data to the scratchpad */ 175 if (w1_reset_select_slave(sl)) 176 return -1; 177 178 wrbuf[0] = W1_F23_WRITE_SCRATCH; 179 wrbuf[1] = addr & 0xff; 180 wrbuf[2] = addr >> 8; 181 182 w1_write_block(sl->master, wrbuf, 3); 183 w1_write_block(sl->master, data, len); 184 185 /* Read the scratchpad and verify */ 186 if (w1_reset_select_slave(sl)) 187 return -1; 188 189 w1_write_8(sl->master, W1_F23_READ_SCRATCH); 190 w1_read_block(sl->master, rdbuf, len + 3); 191 192 /* Compare what was read against the data written */ 193 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) || 194 (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0)) 195 return -1; 196 197 /* Copy the scratchpad to EEPROM */ 198 if (w1_reset_select_slave(sl)) 199 return -1; 200 201 wrbuf[0] = W1_F23_COPY_SCRATCH; 202 wrbuf[3] = es; 203 w1_write_block(sl->master, wrbuf, 4); 204 205 /* Sleep for tprog ms to wait for the write to complete */ 206 msleep(f23->cfg->tprog); 207 208 /* Reset the bus to wake up the EEPROM (this may not be needed) */ 209 w1_reset_bus(sl->master); 210 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 211 clear_bit(addr >> W1_PAGE_BITS, f23->validcrc); 212 #endif 213 return 0; 214 } 215 216 static ssize_t eeprom_write(struct file *filp, struct kobject *kobj, 217 struct bin_attribute *bin_attr, char *buf, 218 loff_t off, size_t count) 219 { 220 struct w1_slave *sl = kobj_to_w1_slave(kobj); 221 int addr, len, idx; 222 223 count = w1_f23_fix_count(off, count, bin_attr->size); 224 if (!count) 225 return 0; 226 227 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 228 /* can only write full blocks in cached mode */ 229 if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) { 230 dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n", 231 (int)off, count); 232 return -EINVAL; 233 } 234 235 /* make sure the block CRCs are valid */ 236 for (idx = 0; idx < count; idx += W1_PAGE_SIZE) { 237 if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE) != CRC16_VALID) { 238 dev_err(&sl->dev, "bad CRC at offset %d\n", (int)off); 239 return -EINVAL; 240 } 241 } 242 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 243 244 mutex_lock(&sl->master->bus_mutex); 245 246 /* Can only write data to one page at a time */ 247 idx = 0; 248 while (idx < count) { 249 addr = off + idx; 250 len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK); 251 if (len > (count - idx)) 252 len = count - idx; 253 254 if (w1_f23_write(sl, addr, len, &buf[idx]) < 0) { 255 count = -EIO; 256 goto out_up; 257 } 258 idx += len; 259 } 260 261 out_up: 262 mutex_unlock(&sl->master->bus_mutex); 263 264 return count; 265 } 266 267 static BIN_ATTR_RW(eeprom, W1_EEPROM_SIZE); 268 269 static struct bin_attribute *w1_f23_bin_attributes[] = { 270 &bin_attr_eeprom, 271 NULL, 272 }; 273 274 static const struct attribute_group w1_f23_group = { 275 .bin_attrs = w1_f23_bin_attributes, 276 }; 277 278 static const struct attribute_group *w1_f23_groups[] = { 279 &w1_f23_group, 280 NULL, 281 }; 282 283 static int w1_f23_add_slave(struct w1_slave *sl) 284 { 285 struct w1_f23_data *data; 286 287 data = kzalloc(sizeof(struct w1_f23_data), GFP_KERNEL); 288 if (!data) 289 return -ENOMEM; 290 291 data->cfg = &config_f23; 292 293 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 294 if (data->cfg->page_count > W1_VALIDCRC_MAX) { 295 dev_err(&sl->dev, "page count too big for crc bitmap\n"); 296 kfree(data); 297 return -EINVAL; 298 } 299 data->memory = kzalloc(data->cfg->eeprom_size, GFP_KERNEL); 300 if (!data->memory) { 301 kfree(data); 302 return -ENOMEM; 303 } 304 bitmap_zero(data->validcrc, data->cfg->page_count); 305 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 306 sl->family_data = data; 307 308 return 0; 309 } 310 311 static void w1_f23_remove_slave(struct w1_slave *sl) 312 { 313 struct w1_f23_data *data = sl->family_data; 314 sl->family_data = NULL; 315 #ifdef CONFIG_W1_SLAVE_DS2433_CRC 316 kfree(data->memory); 317 #endif /* CONFIG_W1_SLAVE_DS2433_CRC */ 318 kfree(data); 319 } 320 321 static const struct w1_family_ops w1_f23_fops = { 322 .add_slave = w1_f23_add_slave, 323 .remove_slave = w1_f23_remove_slave, 324 .groups = w1_f23_groups, 325 }; 326 327 static struct w1_family w1_family_23 = { 328 .fid = W1_EEPROM_DS2433, 329 .fops = &w1_f23_fops, 330 }; 331 module_w1_family(w1_family_23); 332 333 MODULE_AUTHOR("Ben Gardner <bgardner@wabtec.com>"); 334 MODULE_DESCRIPTION("w1 family 23 driver for DS2433, 4kb EEPROM"); 335 MODULE_LICENSE("GPL"); 336 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2433)); 337