1 /*
2  * Driver for 93xx46 EEPROMs
3  *
4  * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License version 2 as
8  * published by the Free Software Foundation.
9  */
10 
11 #include <linux/delay.h>
12 #include <linux/device.h>
13 #include <linux/kernel.h>
14 #include <linux/module.h>
15 #include <linux/mutex.h>
16 #include <linux/slab.h>
17 #include <linux/spi/spi.h>
18 #include <linux/sysfs.h>
19 #include <linux/eeprom_93xx46.h>
20 
21 #define OP_START	0x4
22 #define OP_WRITE	(OP_START | 0x1)
23 #define OP_READ		(OP_START | 0x2)
24 #define ADDR_EWDS	0x00
25 #define ADDR_ERAL	0x20
26 #define ADDR_EWEN	0x30
27 
28 struct eeprom_93xx46_dev {
29 	struct spi_device *spi;
30 	struct eeprom_93xx46_platform_data *pdata;
31 	struct bin_attribute bin;
32 	struct mutex lock;
33 	int addrlen;
34 };
35 
36 static ssize_t
37 eeprom_93xx46_bin_read(struct file *filp, struct kobject *kobj,
38 		       struct bin_attribute *bin_attr,
39 		       char *buf, loff_t off, size_t count)
40 {
41 	struct eeprom_93xx46_dev *edev;
42 	struct device *dev;
43 	struct spi_message m;
44 	struct spi_transfer t[2];
45 	int bits, ret;
46 	u16 cmd_addr;
47 
48 	dev = container_of(kobj, struct device, kobj);
49 	edev = dev_get_drvdata(dev);
50 
51 	if (unlikely(off >= edev->bin.size))
52 		return 0;
53 	if ((off + count) > edev->bin.size)
54 		count = edev->bin.size - off;
55 	if (unlikely(!count))
56 		return count;
57 
58 	cmd_addr = OP_READ << edev->addrlen;
59 
60 	if (edev->addrlen == 7) {
61 		cmd_addr |= off & 0x7f;
62 		bits = 10;
63 	} else {
64 		cmd_addr |= off & 0x3f;
65 		bits = 9;
66 	}
67 
68 	dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
69 		cmd_addr, edev->spi->max_speed_hz);
70 
71 	spi_message_init(&m);
72 	memset(t, 0, sizeof(t));
73 
74 	t[0].tx_buf = (char *)&cmd_addr;
75 	t[0].len = 2;
76 	t[0].bits_per_word = bits;
77 	spi_message_add_tail(&t[0], &m);
78 
79 	t[1].rx_buf = buf;
80 	t[1].len = count;
81 	t[1].bits_per_word = 8;
82 	spi_message_add_tail(&t[1], &m);
83 
84 	mutex_lock(&edev->lock);
85 
86 	if (edev->pdata->prepare)
87 		edev->pdata->prepare(edev);
88 
89 	ret = spi_sync(edev->spi, &m);
90 	/* have to wait at least Tcsl ns */
91 	ndelay(250);
92 	if (ret) {
93 		dev_err(&edev->spi->dev, "read %zu bytes at %d: err. %d\n",
94 			count, (int)off, ret);
95 	}
96 
97 	if (edev->pdata->finish)
98 		edev->pdata->finish(edev);
99 
100 	mutex_unlock(&edev->lock);
101 	return ret ? : count;
102 }
103 
104 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
105 {
106 	struct spi_message m;
107 	struct spi_transfer t;
108 	int bits, ret;
109 	u16 cmd_addr;
110 
111 	cmd_addr = OP_START << edev->addrlen;
112 	if (edev->addrlen == 7) {
113 		cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
114 		bits = 10;
115 	} else {
116 		cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
117 		bits = 9;
118 	}
119 
120 	dev_dbg(&edev->spi->dev, "ew cmd 0x%04x\n", cmd_addr);
121 
122 	spi_message_init(&m);
123 	memset(&t, 0, sizeof(t));
124 
125 	t.tx_buf = &cmd_addr;
126 	t.len = 2;
127 	t.bits_per_word = bits;
128 	spi_message_add_tail(&t, &m);
129 
130 	mutex_lock(&edev->lock);
131 
132 	if (edev->pdata->prepare)
133 		edev->pdata->prepare(edev);
134 
135 	ret = spi_sync(edev->spi, &m);
136 	/* have to wait at least Tcsl ns */
137 	ndelay(250);
138 	if (ret)
139 		dev_err(&edev->spi->dev, "erase/write %sable error %d\n",
140 			is_on ? "en" : "dis", ret);
141 
142 	if (edev->pdata->finish)
143 		edev->pdata->finish(edev);
144 
145 	mutex_unlock(&edev->lock);
146 	return ret;
147 }
148 
149 static ssize_t
150 eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
151 			 const char *buf, unsigned off)
152 {
153 	struct spi_message m;
154 	struct spi_transfer t[2];
155 	int bits, data_len, ret;
156 	u16 cmd_addr;
157 
158 	cmd_addr = OP_WRITE << edev->addrlen;
159 
160 	if (edev->addrlen == 7) {
161 		cmd_addr |= off & 0x7f;
162 		bits = 10;
163 		data_len = 1;
164 	} else {
165 		cmd_addr |= off & 0x3f;
166 		bits = 9;
167 		data_len = 2;
168 	}
169 
170 	dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
171 
172 	spi_message_init(&m);
173 	memset(t, 0, sizeof(t));
174 
175 	t[0].tx_buf = (char *)&cmd_addr;
176 	t[0].len = 2;
177 	t[0].bits_per_word = bits;
178 	spi_message_add_tail(&t[0], &m);
179 
180 	t[1].tx_buf = buf;
181 	t[1].len = data_len;
182 	t[1].bits_per_word = 8;
183 	spi_message_add_tail(&t[1], &m);
184 
185 	ret = spi_sync(edev->spi, &m);
186 	/* have to wait program cycle time Twc ms */
187 	mdelay(6);
188 	return ret;
189 }
190 
191 static ssize_t
192 eeprom_93xx46_bin_write(struct file *filp, struct kobject *kobj,
193 			struct bin_attribute *bin_attr,
194 			char *buf, loff_t off, size_t count)
195 {
196 	struct eeprom_93xx46_dev *edev;
197 	struct device *dev;
198 	int i, ret, step = 1;
199 
200 	dev = container_of(kobj, struct device, kobj);
201 	edev = dev_get_drvdata(dev);
202 
203 	if (unlikely(off >= edev->bin.size))
204 		return -EFBIG;
205 	if ((off + count) > edev->bin.size)
206 		count = edev->bin.size - off;
207 	if (unlikely(!count))
208 		return count;
209 
210 	/* only write even number of bytes on 16-bit devices */
211 	if (edev->addrlen == 6) {
212 		step = 2;
213 		count &= ~1;
214 	}
215 
216 	/* erase/write enable */
217 	ret = eeprom_93xx46_ew(edev, 1);
218 	if (ret)
219 		return ret;
220 
221 	mutex_lock(&edev->lock);
222 
223 	if (edev->pdata->prepare)
224 		edev->pdata->prepare(edev);
225 
226 	for (i = 0; i < count; i += step) {
227 		ret = eeprom_93xx46_write_word(edev, &buf[i], off + i);
228 		if (ret) {
229 			dev_err(&edev->spi->dev, "write failed at %d: %d\n",
230 				(int)off + i, ret);
231 			break;
232 		}
233 	}
234 
235 	if (edev->pdata->finish)
236 		edev->pdata->finish(edev);
237 
238 	mutex_unlock(&edev->lock);
239 
240 	/* erase/write disable */
241 	eeprom_93xx46_ew(edev, 0);
242 	return ret ? : count;
243 }
244 
245 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
246 {
247 	struct eeprom_93xx46_platform_data *pd = edev->pdata;
248 	struct spi_message m;
249 	struct spi_transfer t;
250 	int bits, ret;
251 	u16 cmd_addr;
252 
253 	cmd_addr = OP_START << edev->addrlen;
254 	if (edev->addrlen == 7) {
255 		cmd_addr |= ADDR_ERAL << 1;
256 		bits = 10;
257 	} else {
258 		cmd_addr |= ADDR_ERAL;
259 		bits = 9;
260 	}
261 
262 	spi_message_init(&m);
263 	memset(&t, 0, sizeof(t));
264 
265 	t.tx_buf = &cmd_addr;
266 	t.len = 2;
267 	t.bits_per_word = bits;
268 	spi_message_add_tail(&t, &m);
269 
270 	mutex_lock(&edev->lock);
271 
272 	if (edev->pdata->prepare)
273 		edev->pdata->prepare(edev);
274 
275 	ret = spi_sync(edev->spi, &m);
276 	if (ret)
277 		dev_err(&edev->spi->dev, "erase error %d\n", ret);
278 	/* have to wait erase cycle time Tec ms */
279 	mdelay(6);
280 
281 	if (pd->finish)
282 		pd->finish(edev);
283 
284 	mutex_unlock(&edev->lock);
285 	return ret;
286 }
287 
288 static ssize_t eeprom_93xx46_store_erase(struct device *dev,
289 					 struct device_attribute *attr,
290 					 const char *buf, size_t count)
291 {
292 	struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev);
293 	int erase = 0, ret;
294 
295 	sscanf(buf, "%d", &erase);
296 	if (erase) {
297 		ret = eeprom_93xx46_ew(edev, 1);
298 		if (ret)
299 			return ret;
300 		ret = eeprom_93xx46_eral(edev);
301 		if (ret)
302 			return ret;
303 		ret = eeprom_93xx46_ew(edev, 0);
304 		if (ret)
305 			return ret;
306 	}
307 	return count;
308 }
309 static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
310 
311 static int eeprom_93xx46_probe(struct spi_device *spi)
312 {
313 	struct eeprom_93xx46_platform_data *pd;
314 	struct eeprom_93xx46_dev *edev;
315 	int err;
316 
317 	pd = spi->dev.platform_data;
318 	if (!pd) {
319 		dev_err(&spi->dev, "missing platform data\n");
320 		return -ENODEV;
321 	}
322 
323 	edev = kzalloc(sizeof(*edev), GFP_KERNEL);
324 	if (!edev)
325 		return -ENOMEM;
326 
327 	if (pd->flags & EE_ADDR8)
328 		edev->addrlen = 7;
329 	else if (pd->flags & EE_ADDR16)
330 		edev->addrlen = 6;
331 	else {
332 		dev_err(&spi->dev, "unspecified address type\n");
333 		err = -EINVAL;
334 		goto fail;
335 	}
336 
337 	mutex_init(&edev->lock);
338 
339 	edev->spi = spi_dev_get(spi);
340 	edev->pdata = pd;
341 
342 	sysfs_bin_attr_init(&edev->bin);
343 	edev->bin.attr.name = "eeprom";
344 	edev->bin.attr.mode = S_IRUSR;
345 	edev->bin.read = eeprom_93xx46_bin_read;
346 	edev->bin.size = 128;
347 	if (!(pd->flags & EE_READONLY)) {
348 		edev->bin.write = eeprom_93xx46_bin_write;
349 		edev->bin.attr.mode |= S_IWUSR;
350 	}
351 
352 	err = sysfs_create_bin_file(&spi->dev.kobj, &edev->bin);
353 	if (err)
354 		goto fail;
355 
356 	dev_info(&spi->dev, "%d-bit eeprom %s\n",
357 		(pd->flags & EE_ADDR8) ? 8 : 16,
358 		(pd->flags & EE_READONLY) ? "(readonly)" : "");
359 
360 	if (!(pd->flags & EE_READONLY)) {
361 		if (device_create_file(&spi->dev, &dev_attr_erase))
362 			dev_err(&spi->dev, "can't create erase interface\n");
363 	}
364 
365 	spi_set_drvdata(spi, edev);
366 	return 0;
367 fail:
368 	kfree(edev);
369 	return err;
370 }
371 
372 static int eeprom_93xx46_remove(struct spi_device *spi)
373 {
374 	struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi);
375 
376 	if (!(edev->pdata->flags & EE_READONLY))
377 		device_remove_file(&spi->dev, &dev_attr_erase);
378 
379 	sysfs_remove_bin_file(&spi->dev.kobj, &edev->bin);
380 	kfree(edev);
381 	return 0;
382 }
383 
384 static struct spi_driver eeprom_93xx46_driver = {
385 	.driver = {
386 		.name	= "93xx46",
387 		.owner	= THIS_MODULE,
388 	},
389 	.probe		= eeprom_93xx46_probe,
390 	.remove		= eeprom_93xx46_remove,
391 };
392 
393 module_spi_driver(eeprom_93xx46_driver);
394 
395 MODULE_LICENSE("GPL");
396 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
397 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
398 MODULE_ALIAS("spi:93xx46");
399