0001
0002
0003
0004
0005
0006
0007
0008
0009
0010 #include <linux/kernel.h>
0011 #include <linux/module.h>
0012 #include <linux/moduleparam.h>
0013 #include <linux/device.h>
0014 #include <linux/types.h>
0015 #include <linux/delay.h>
0016
0017 #include <linux/w1.h>
0018
0019 #define W1_EEPROM_DS2431 0x2D
0020
0021 #define W1_F2D_EEPROM_SIZE 128
0022 #define W1_F2D_PAGE_COUNT 4
0023 #define W1_F2D_PAGE_BITS 5
0024 #define W1_F2D_PAGE_SIZE (1<<W1_F2D_PAGE_BITS)
0025 #define W1_F2D_PAGE_MASK 0x1F
0026
0027 #define W1_F2D_SCRATCH_BITS 3
0028 #define W1_F2D_SCRATCH_SIZE (1<<W1_F2D_SCRATCH_BITS)
0029 #define W1_F2D_SCRATCH_MASK (W1_F2D_SCRATCH_SIZE-1)
0030
0031 #define W1_F2D_READ_EEPROM 0xF0
0032 #define W1_F2D_WRITE_SCRATCH 0x0F
0033 #define W1_F2D_READ_SCRATCH 0xAA
0034 #define W1_F2D_COPY_SCRATCH 0x55
0035
0036
0037 #define W1_F2D_TPROG_MS 11
0038
0039 #define W1_F2D_READ_RETRIES 10
0040 #define W1_F2D_READ_MAXLEN 8
0041
0042
0043
0044
0045
0046 static inline size_t w1_f2d_fix_count(loff_t off, size_t count, size_t size)
0047 {
0048 if (off > size)
0049 return 0;
0050
0051 if ((off + count) > size)
0052 return size - off;
0053
0054 return count;
0055 }
0056
0057
0058
0059
0060
0061
0062
0063
0064 static int w1_f2d_readblock(struct w1_slave *sl, int off, int count, char *buf)
0065 {
0066 u8 wrbuf[3];
0067 u8 cmp[W1_F2D_READ_MAXLEN];
0068 int tries = W1_F2D_READ_RETRIES;
0069
0070 do {
0071 wrbuf[0] = W1_F2D_READ_EEPROM;
0072 wrbuf[1] = off & 0xff;
0073 wrbuf[2] = off >> 8;
0074
0075 if (w1_reset_select_slave(sl))
0076 return -1;
0077
0078 w1_write_block(sl->master, wrbuf, 3);
0079 w1_read_block(sl->master, buf, count);
0080
0081 if (w1_reset_select_slave(sl))
0082 return -1;
0083
0084 w1_write_block(sl->master, wrbuf, 3);
0085 w1_read_block(sl->master, cmp, count);
0086
0087 if (!memcmp(cmp, buf, count))
0088 return 0;
0089 } while (--tries);
0090
0091 dev_err(&sl->dev, "proof reading failed %d times\n",
0092 W1_F2D_READ_RETRIES);
0093
0094 return -1;
0095 }
0096
0097 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
0098 struct bin_attribute *bin_attr, char *buf,
0099 loff_t off, size_t count)
0100 {
0101 struct w1_slave *sl = kobj_to_w1_slave(kobj);
0102 int todo = count;
0103
0104 count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
0105 if (count == 0)
0106 return 0;
0107
0108 mutex_lock(&sl->master->bus_mutex);
0109
0110
0111 while (todo > 0) {
0112 int block_read;
0113
0114 if (todo >= W1_F2D_READ_MAXLEN)
0115 block_read = W1_F2D_READ_MAXLEN;
0116 else
0117 block_read = todo;
0118
0119 if (w1_f2d_readblock(sl, off, block_read, buf) < 0)
0120 count = -EIO;
0121
0122 todo -= W1_F2D_READ_MAXLEN;
0123 buf += W1_F2D_READ_MAXLEN;
0124 off += W1_F2D_READ_MAXLEN;
0125 }
0126
0127 mutex_unlock(&sl->master->bus_mutex);
0128
0129 return count;
0130 }
0131
0132
0133
0134
0135
0136
0137
0138
0139
0140
0141
0142
0143
0144
0145 static int w1_f2d_write(struct w1_slave *sl, int addr, int len, const u8 *data)
0146 {
0147 int tries = W1_F2D_READ_RETRIES;
0148 u8 wrbuf[4];
0149 u8 rdbuf[W1_F2D_SCRATCH_SIZE + 3];
0150 u8 es = (addr + len - 1) % W1_F2D_SCRATCH_SIZE;
0151
0152 retry:
0153
0154
0155 if (w1_reset_select_slave(sl))
0156 return -1;
0157
0158 wrbuf[0] = W1_F2D_WRITE_SCRATCH;
0159 wrbuf[1] = addr & 0xff;
0160 wrbuf[2] = addr >> 8;
0161
0162 w1_write_block(sl->master, wrbuf, 3);
0163 w1_write_block(sl->master, data, len);
0164
0165
0166 if (w1_reset_select_slave(sl))
0167 return -1;
0168
0169 w1_write_8(sl->master, W1_F2D_READ_SCRATCH);
0170 w1_read_block(sl->master, rdbuf, len + 3);
0171
0172
0173 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
0174 (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0)) {
0175
0176 if (--tries)
0177 goto retry;
0178
0179 dev_err(&sl->dev,
0180 "could not write to eeprom, scratchpad compare failed %d times\n",
0181 W1_F2D_READ_RETRIES);
0182
0183 return -1;
0184 }
0185
0186
0187 if (w1_reset_select_slave(sl))
0188 return -1;
0189
0190 wrbuf[0] = W1_F2D_COPY_SCRATCH;
0191 wrbuf[3] = es;
0192 w1_write_block(sl->master, wrbuf, 4);
0193
0194
0195 msleep(W1_F2D_TPROG_MS);
0196
0197
0198 w1_reset_bus(sl->master);
0199
0200 return 0;
0201 }
0202
0203 static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
0204 struct bin_attribute *bin_attr, char *buf,
0205 loff_t off, size_t count)
0206 {
0207 struct w1_slave *sl = kobj_to_w1_slave(kobj);
0208 int addr, len;
0209 int copy;
0210
0211 count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
0212 if (count == 0)
0213 return 0;
0214
0215 mutex_lock(&sl->master->bus_mutex);
0216
0217
0218 addr = off;
0219 len = count;
0220 while (len > 0) {
0221
0222
0223 if (len < W1_F2D_SCRATCH_SIZE || addr & W1_F2D_SCRATCH_MASK) {
0224 char tmp[W1_F2D_SCRATCH_SIZE];
0225
0226
0227 if (w1_f2d_readblock(sl, addr & ~W1_F2D_SCRATCH_MASK,
0228 W1_F2D_SCRATCH_SIZE, tmp)) {
0229 count = -EIO;
0230 goto out_up;
0231 }
0232
0233
0234 copy = W1_F2D_SCRATCH_SIZE -
0235 (addr & W1_F2D_SCRATCH_MASK);
0236
0237 if (copy > len)
0238 copy = len;
0239
0240 memcpy(&tmp[addr & W1_F2D_SCRATCH_MASK], buf, copy);
0241 if (w1_f2d_write(sl, addr & ~W1_F2D_SCRATCH_MASK,
0242 W1_F2D_SCRATCH_SIZE, tmp) < 0) {
0243 count = -EIO;
0244 goto out_up;
0245 }
0246 } else {
0247
0248 copy = W1_F2D_SCRATCH_SIZE;
0249 if (w1_f2d_write(sl, addr, copy, buf) < 0) {
0250 count = -EIO;
0251 goto out_up;
0252 }
0253 }
0254 buf += copy;
0255 addr += copy;
0256 len -= copy;
0257 }
0258
0259 out_up:
0260 mutex_unlock(&sl->master->bus_mutex);
0261
0262 return count;
0263 }
0264
0265 static BIN_ATTR_RW(eeprom, W1_F2D_EEPROM_SIZE);
0266
0267 static struct bin_attribute *w1_f2d_bin_attrs[] = {
0268 &bin_attr_eeprom,
0269 NULL,
0270 };
0271
0272 static const struct attribute_group w1_f2d_group = {
0273 .bin_attrs = w1_f2d_bin_attrs,
0274 };
0275
0276 static const struct attribute_group *w1_f2d_groups[] = {
0277 &w1_f2d_group,
0278 NULL,
0279 };
0280
0281 static const struct w1_family_ops w1_f2d_fops = {
0282 .groups = w1_f2d_groups,
0283 };
0284
0285 static struct w1_family w1_family_2d = {
0286 .fid = W1_EEPROM_DS2431,
0287 .fops = &w1_f2d_fops,
0288 };
0289 module_w1_family(w1_family_2d);
0290
0291 MODULE_AUTHOR("Bernhard Weirich <bernhard.weirich@riedel.net>");
0292 MODULE_DESCRIPTION("w1 family 2d driver for DS2431, 1kb EEPROM");
0293 MODULE_LICENSE("GPL");
0294 MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2431));