^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * w1_ds28e04.c - w1 family 1C (DS28E04) driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (c) 2012 Markus Franke <franke.m@sebakmt.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) #include <linux/kernel.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) #include <linux/moduleparam.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/delay.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/crc16.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/uaccess.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #define CRC16_INIT 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #define CRC16_VALID 0xb001
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/w1.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #define W1_FAMILY_DS28E04 0x1C
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) /* Allow the strong pullup to be disabled, but default to enabled.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) * If it was disabled a parasite powered device might not get the required
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) * current to copy the data from the scratchpad to EEPROM. If it is enabled
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) * parasite powered devices have a better chance of getting the current
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) * required.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) static int w1_strong_pullup = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) module_param_named(strong_pullup, w1_strong_pullup, int, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) /* enable/disable CRC checking on DS28E04-100 memory accesses */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) static bool w1_enable_crccheck = true;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define W1_EEPROM_SIZE 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) #define W1_PAGE_COUNT 16
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) #define W1_PAGE_SIZE 32
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) #define W1_PAGE_BITS 5
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) #define W1_PAGE_MASK 0x1F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) #define W1_F1C_READ_EEPROM 0xF0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) #define W1_F1C_WRITE_SCRATCH 0x0F
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) #define W1_F1C_READ_SCRATCH 0xAA
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) #define W1_F1C_COPY_SCRATCH 0x55
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) #define W1_F1C_ACCESS_WRITE 0x5A
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) #define W1_1C_REG_LOGIC_STATE 0x220
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) struct w1_f1C_data {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) u8 memory[W1_EEPROM_SIZE];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) u32 validcrc;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) * Check the file size bounds and adjusts count as needed.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) * This would not be needed if the file size didn't reset to 0 after a write.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) static inline size_t w1_f1C_fix_count(loff_t off, size_t count, size_t size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) if (off > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) if ((off + count) > size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) return size - off;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) static int w1_f1C_refresh_block(struct w1_slave *sl, struct w1_f1C_data *data,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) int block)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74) u8 wrbuf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) int off = block * W1_PAGE_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) if (data->validcrc & (1 << block))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) if (w1_reset_select_slave(sl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) data->validcrc = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) wrbuf[0] = W1_F1C_READ_EEPROM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) wrbuf[1] = off & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) wrbuf[2] = off >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) w1_write_block(sl->master, wrbuf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) w1_read_block(sl->master, &data->memory[off], W1_PAGE_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) /* cache the block if the CRC is valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (crc16(CRC16_INIT, &data->memory[off], W1_PAGE_SIZE) == CRC16_VALID)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) data->validcrc |= (1 << block);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) static int w1_f1C_read(struct w1_slave *sl, int addr, int len, char *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) u8 wrbuf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) /* read directly from the EEPROM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) if (w1_reset_select_slave(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) wrbuf[0] = W1_F1C_READ_EEPROM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) wrbuf[1] = addr & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108) wrbuf[2] = addr >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) w1_write_block(sl->master, wrbuf, sizeof(wrbuf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) return w1_read_block(sl->master, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) struct w1_f1C_data *data = sl->family_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) int i, min_page, max_page;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) count = w1_f1C_fix_count(off, count, W1_EEPROM_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) mutex_lock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) if (w1_enable_crccheck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) min_page = (off >> W1_PAGE_BITS);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) max_page = (off + count - 1) >> W1_PAGE_BITS;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) for (i = min_page; i <= max_page; i++) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) if (w1_f1C_refresh_block(sl, data, i)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) count = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) goto out_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) memcpy(buf, &data->memory[off], count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) } else {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) count = w1_f1C_read(sl, off, count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) out_up:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) mutex_unlock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) * Writes to the scratchpad and reads it back for verification.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) * Then copies the scratchpad to EEPROM.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) * The data must be on one page.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) * The master must be locked.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) * @param sl The slave structure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) * @param addr Address for the write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) * @param len length must be <= (W1_PAGE_SIZE - (addr & W1_PAGE_MASK))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) * @param data The data to write
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) * @return 0=Success -1=failure
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) static int w1_f1C_write(struct w1_slave *sl, int addr, int len, const u8 *data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) u8 wrbuf[4];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) u8 rdbuf[W1_PAGE_SIZE + 3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) u8 es = (addr + len - 1) & 0x1f;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) unsigned int tm = 10;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) struct w1_f1C_data *f1C = sl->family_data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) /* Write the data to the scratchpad */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) if (w1_reset_select_slave(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) wrbuf[0] = W1_F1C_WRITE_SCRATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) wrbuf[1] = addr & 0xff;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175) wrbuf[2] = addr >> 8;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) w1_write_block(sl->master, wrbuf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) w1_write_block(sl->master, data, len);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) /* Read the scratchpad and verify */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) if (w1_reset_select_slave(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) w1_write_8(sl->master, W1_F1C_READ_SCRATCH);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) w1_read_block(sl->master, rdbuf, len + 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) /* Compare what was read against the data written */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) /* Copy the scratchpad to EEPROM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) if (w1_reset_select_slave(sl))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) wrbuf[0] = W1_F1C_COPY_SCRATCH;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) wrbuf[3] = es;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199) for (i = 0; i < sizeof(wrbuf); ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) /* issue 10ms strong pullup (or delay) on the last byte
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) for writing the data from the scratchpad to EEPROM */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) if (w1_strong_pullup && i == sizeof(wrbuf)-1)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) w1_next_pullup(sl->master, tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205) w1_write_8(sl->master, wrbuf[i]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (!w1_strong_pullup)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) msleep(tm);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (w1_enable_crccheck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) /* invalidate cached data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) f1C->validcrc &= ~(1 << (addr >> W1_PAGE_BITS));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) /* Reset the bus to wake up the EEPROM (this may not be needed) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) w1_reset_bus(sl->master);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) struct bin_attribute *bin_attr, char *buf,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) loff_t off, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) int addr, len, idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) count = w1_f1C_fix_count(off, count, W1_EEPROM_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) if (count == 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) if (w1_enable_crccheck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) /* can only write full blocks in cached mode */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) if ((off & W1_PAGE_MASK) || (count & W1_PAGE_MASK)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) dev_err(&sl->dev, "invalid offset/count off=%d cnt=%zd\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) (int)off, count);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) /* make sure the block CRCs are valid */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) for (idx = 0; idx < count; idx += W1_PAGE_SIZE) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) if (crc16(CRC16_INIT, &buf[idx], W1_PAGE_SIZE)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) != CRC16_VALID) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) dev_err(&sl->dev, "bad CRC at offset %d\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) (int)off);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) mutex_lock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) /* Can only write data to one page at a time */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) idx = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) while (idx < count) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) addr = off + idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) len = W1_PAGE_SIZE - (addr & W1_PAGE_MASK);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) if (len > (count - idx))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) len = count - idx;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) if (w1_f1C_write(sl, addr, len, &buf[idx]) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) count = -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) goto out_up;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) idx += len;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) out_up:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) mutex_unlock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) static BIN_ATTR_RW(eeprom, W1_EEPROM_SIZE);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) static ssize_t pio_read(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) struct bin_attribute *bin_attr, char *buf, loff_t off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) /* check arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) if (off != 0 || count != 1 || buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) mutex_lock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) ret = w1_f1C_read(sl, W1_1C_REG_LOGIC_STATE, count, buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) mutex_unlock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) static ssize_t pio_write(struct file *filp, struct kobject *kobj,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) struct bin_attribute *bin_attr, char *buf, loff_t off,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302) struct w1_slave *sl = kobj_to_w1_slave(kobj);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) u8 wrbuf[3];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) u8 ack;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) /* check arguments */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) if (off != 0 || count != 1 || buf == NULL)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) mutex_lock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) /* Write the PIO data */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313) if (w1_reset_select_slave(sl)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) mutex_unlock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315) return -1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318) /* set bit 7..2 to value '1' */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) *buf = *buf | 0xFC;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) wrbuf[0] = W1_F1C_ACCESS_WRITE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) wrbuf[1] = *buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) wrbuf[2] = ~(*buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) w1_write_block(sl->master, wrbuf, 3);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326) w1_read_block(sl->master, &ack, sizeof(ack));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) mutex_unlock(&sl->master->mutex);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) /* check for acknowledgement */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) if (ack != 0xAA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) return -EIO;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 335) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 336)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 337) static BIN_ATTR_RW(pio, 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 338)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 339) static ssize_t crccheck_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 340) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 341) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 342) return sysfs_emit(buf, "%d\n", w1_enable_crccheck);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 343) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 344)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 345) static ssize_t crccheck_store(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 346) const char *buf, size_t count)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 347) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 348) int err = kstrtobool(buf, &w1_enable_crccheck);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 349)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 350) if (err)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 351) return err;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 352)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 353) return count;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 354) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 355)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 356) static DEVICE_ATTR_RW(crccheck);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 357)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 358) static struct attribute *w1_f1C_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 359) &dev_attr_crccheck.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 360) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 361) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 362)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 363) static struct bin_attribute *w1_f1C_bin_attrs[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 364) &bin_attr_eeprom,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 365) &bin_attr_pio,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 366) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 367) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 368)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 369) static const struct attribute_group w1_f1C_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 370) .attrs = w1_f1C_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 371) .bin_attrs = w1_f1C_bin_attrs,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 372) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 373)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 374) static const struct attribute_group *w1_f1C_groups[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 375) &w1_f1C_group,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 376) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 377) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 378)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 379) static int w1_f1C_add_slave(struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 380) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 381) struct w1_f1C_data *data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 382)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 383) if (w1_enable_crccheck) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 384) data = kzalloc(sizeof(struct w1_f1C_data), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 385) if (!data)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 386) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 387) sl->family_data = data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 388) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 389)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 390) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 391) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 392)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 393) static void w1_f1C_remove_slave(struct w1_slave *sl)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 394) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 395) kfree(sl->family_data);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 396) sl->family_data = NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 397) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 398)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 399) static const struct w1_family_ops w1_f1C_fops = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 400) .add_slave = w1_f1C_add_slave,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 401) .remove_slave = w1_f1C_remove_slave,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 402) .groups = w1_f1C_groups,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 403) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 404)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 405) static struct w1_family w1_family_1C = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 406) .fid = W1_FAMILY_DS28E04,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 407) .fops = &w1_f1C_fops,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 408) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 409) module_w1_family(w1_family_1C);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 410)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 411) MODULE_AUTHOR("Markus Franke <franke.m@sebakmt.com>, <franm@hrz.tu-chemnitz.de>");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 412) MODULE_DESCRIPTION("w1 family 1C driver for DS28E04, 4kb EEPROM and PIO");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 413) MODULE_LICENSE("GPL");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 414) MODULE_ALIAS("w1-family-" __stringify(W1_FAMILY_DS28E04));