1
2
3
4
5
6
7
8
9
10
11
12#include <linux/kernel.h>
13#include <linux/module.h>
14#include <linux/moduleparam.h>
15#include <linux/device.h>
16#include <linux/types.h>
17#include <linux/delay.h>
18
19#include <linux/w1.h>
20
21#define W1_EEPROM_DS2431 0x2D
22
23#define W1_F2D_EEPROM_SIZE 128
24#define W1_F2D_PAGE_COUNT 4
25#define W1_F2D_PAGE_BITS 5
26#define W1_F2D_PAGE_SIZE (1<<W1_F2D_PAGE_BITS)
27#define W1_F2D_PAGE_MASK 0x1F
28
29#define W1_F2D_SCRATCH_BITS 3
30#define W1_F2D_SCRATCH_SIZE (1<<W1_F2D_SCRATCH_BITS)
31#define W1_F2D_SCRATCH_MASK (W1_F2D_SCRATCH_SIZE-1)
32
33#define W1_F2D_READ_EEPROM 0xF0
34#define W1_F2D_WRITE_SCRATCH 0x0F
35#define W1_F2D_READ_SCRATCH 0xAA
36#define W1_F2D_COPY_SCRATCH 0x55
37
38
39#define W1_F2D_TPROG_MS 11
40
41#define W1_F2D_READ_RETRIES 10
42#define W1_F2D_READ_MAXLEN 8
43
44
45
46
47
48static inline size_t w1_f2d_fix_count(loff_t off, size_t count, size_t size)
49{
50 if (off > size)
51 return 0;
52
53 if ((off + count) > size)
54 return size - off;
55
56 return count;
57}
58
59
60
61
62
63
64
65
66static int w1_f2d_readblock(struct w1_slave *sl, int off, int count, char *buf)
67{
68 u8 wrbuf[3];
69 u8 cmp[W1_F2D_READ_MAXLEN];
70 int tries = W1_F2D_READ_RETRIES;
71
72 do {
73 wrbuf[0] = W1_F2D_READ_EEPROM;
74 wrbuf[1] = off & 0xff;
75 wrbuf[2] = off >> 8;
76
77 if (w1_reset_select_slave(sl))
78 return -1;
79
80 w1_write_block(sl->master, wrbuf, 3);
81 w1_read_block(sl->master, buf, count);
82
83 if (w1_reset_select_slave(sl))
84 return -1;
85
86 w1_write_block(sl->master, wrbuf, 3);
87 w1_read_block(sl->master, cmp, count);
88
89 if (!memcmp(cmp, buf, count))
90 return 0;
91 } while (--tries);
92
93 dev_err(&sl->dev, "proof reading failed %d times\n",
94 W1_F2D_READ_RETRIES);
95
96 return -1;
97}
98
99static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
100 struct bin_attribute *bin_attr, char *buf,
101 loff_t off, size_t count)
102{
103 struct w1_slave *sl = kobj_to_w1_slave(kobj);
104 int todo = count;
105
106 count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
107 if (count == 0)
108 return 0;
109
110 mutex_lock(&sl->master->bus_mutex);
111
112
113 while (todo > 0) {
114 int block_read;
115
116 if (todo >= W1_F2D_READ_MAXLEN)
117 block_read = W1_F2D_READ_MAXLEN;
118 else
119 block_read = todo;
120
121 if (w1_f2d_readblock(sl, off, block_read, buf) < 0)
122 count = -EIO;
123
124 todo -= W1_F2D_READ_MAXLEN;
125 buf += W1_F2D_READ_MAXLEN;
126 off += W1_F2D_READ_MAXLEN;
127 }
128
129 mutex_unlock(&sl->master->bus_mutex);
130
131 return count;
132}
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147static int w1_f2d_write(struct w1_slave *sl, int addr, int len, const u8 *data)
148{
149 int tries = W1_F2D_READ_RETRIES;
150 u8 wrbuf[4];
151 u8 rdbuf[W1_F2D_SCRATCH_SIZE + 3];
152 u8 es = (addr + len - 1) % W1_F2D_SCRATCH_SIZE;
153
154retry:
155
156
157 if (w1_reset_select_slave(sl))
158 return -1;
159
160 wrbuf[0] = W1_F2D_WRITE_SCRATCH;
161 wrbuf[1] = addr & 0xff;
162 wrbuf[2] = addr >> 8;
163
164 w1_write_block(sl->master, wrbuf, 3);
165 w1_write_block(sl->master, data, len);
166
167
168 if (w1_reset_select_slave(sl))
169 return -1;
170
171 w1_write_8(sl->master, W1_F2D_READ_SCRATCH);
172 w1_read_block(sl->master, rdbuf, len + 3);
173
174
175 if ((rdbuf[0] != wrbuf[1]) || (rdbuf[1] != wrbuf[2]) ||
176 (rdbuf[2] != es) || (memcmp(data, &rdbuf[3], len) != 0)) {
177
178 if (--tries)
179 goto retry;
180
181 dev_err(&sl->dev,
182 "could not write to eeprom, scratchpad compare failed %d times\n",
183 W1_F2D_READ_RETRIES);
184
185 return -1;
186 }
187
188
189 if (w1_reset_select_slave(sl))
190 return -1;
191
192 wrbuf[0] = W1_F2D_COPY_SCRATCH;
193 wrbuf[3] = es;
194 w1_write_block(sl->master, wrbuf, 4);
195
196
197 msleep(W1_F2D_TPROG_MS);
198
199
200 w1_reset_bus(sl->master);
201
202 return 0;
203}
204
205static ssize_t eeprom_write(struct file *filp, struct kobject *kobj,
206 struct bin_attribute *bin_attr, char *buf,
207 loff_t off, size_t count)
208{
209 struct w1_slave *sl = kobj_to_w1_slave(kobj);
210 int addr, len;
211 int copy;
212
213 count = w1_f2d_fix_count(off, count, W1_F2D_EEPROM_SIZE);
214 if (count == 0)
215 return 0;
216
217 mutex_lock(&sl->master->bus_mutex);
218
219
220 addr = off;
221 len = count;
222 while (len > 0) {
223
224
225 if (len < W1_F2D_SCRATCH_SIZE || addr & W1_F2D_SCRATCH_MASK) {
226 char tmp[W1_F2D_SCRATCH_SIZE];
227
228
229 if (w1_f2d_readblock(sl, addr & ~W1_F2D_SCRATCH_MASK,
230 W1_F2D_SCRATCH_SIZE, tmp)) {
231 count = -EIO;
232 goto out_up;
233 }
234
235
236 copy = W1_F2D_SCRATCH_SIZE -
237 (addr & W1_F2D_SCRATCH_MASK);
238
239 if (copy > len)
240 copy = len;
241
242 memcpy(&tmp[addr & W1_F2D_SCRATCH_MASK], buf, copy);
243 if (w1_f2d_write(sl, addr & ~W1_F2D_SCRATCH_MASK,
244 W1_F2D_SCRATCH_SIZE, tmp) < 0) {
245 count = -EIO;
246 goto out_up;
247 }
248 } else {
249
250 copy = W1_F2D_SCRATCH_SIZE;
251 if (w1_f2d_write(sl, addr, copy, buf) < 0) {
252 count = -EIO;
253 goto out_up;
254 }
255 }
256 buf += copy;
257 addr += copy;
258 len -= copy;
259 }
260
261out_up:
262 mutex_unlock(&sl->master->bus_mutex);
263
264 return count;
265}
266
267static BIN_ATTR_RW(eeprom, W1_F2D_EEPROM_SIZE);
268
269static struct bin_attribute *w1_f2d_bin_attrs[] = {
270 &bin_attr_eeprom,
271 NULL,
272};
273
274static const struct attribute_group w1_f2d_group = {
275 .bin_attrs = w1_f2d_bin_attrs,
276};
277
278static const struct attribute_group *w1_f2d_groups[] = {
279 &w1_f2d_group,
280 NULL,
281};
282
283static struct w1_family_ops w1_f2d_fops = {
284 .groups = w1_f2d_groups,
285};
286
287static struct w1_family w1_family_2d = {
288 .fid = W1_EEPROM_DS2431,
289 .fops = &w1_f2d_fops,
290};
291module_w1_family(w1_family_2d);
292
293MODULE_AUTHOR("Bernhard Weirich <bernhard.weirich@riedel.net>");
294MODULE_DESCRIPTION("w1 family 2d driver for DS2431, 1kb EEPROM");
295MODULE_LICENSE("GPL");
296MODULE_ALIAS("w1-family-" __stringify(W1_EEPROM_DS2431));
297