blob: 94cc035aa84175ccd5f27eb6cbd0b6747bd74467 [file] [log] [blame]
Anatolij Gustschin06b45012011-07-25 17:13:27 -07001/*
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>
Cory Tusar3ca9b1a2016-02-10 14:32:08 -050013#include <linux/gpio/consumer.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070014#include <linux/kernel.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070015#include <linux/module.h>
16#include <linux/mutex.h>
Cory Tusarc074abe2016-01-06 22:55:02 -050017#include <linux/of.h>
18#include <linux/of_device.h>
Cory Tusar3ca9b1a2016-02-10 14:32:08 -050019#include <linux/of_gpio.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070020#include <linux/slab.h>
21#include <linux/spi/spi.h>
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010022#include <linux/nvmem-provider.h>
Anatolij Gustschin06b45012011-07-25 17:13:27 -070023#include <linux/eeprom_93xx46.h>
24
25#define OP_START 0x4
26#define OP_WRITE (OP_START | 0x1)
27#define OP_READ (OP_START | 0x2)
28#define ADDR_EWDS 0x00
29#define ADDR_ERAL 0x20
30#define ADDR_EWEN 0x30
31
Cory Tusare1379b52016-02-10 14:32:07 -050032struct eeprom_93xx46_devtype_data {
33 unsigned int quirks;
34};
35
36static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data = {
37 .quirks = EEPROM_93XX46_QUIRK_SINGLE_WORD_READ |
38 EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH,
39};
40
Anatolij Gustschin06b45012011-07-25 17:13:27 -070041struct eeprom_93xx46_dev {
42 struct spi_device *spi;
43 struct eeprom_93xx46_platform_data *pdata;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070044 struct mutex lock;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010045 struct nvmem_config nvmem_config;
46 struct nvmem_device *nvmem;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070047 int addrlen;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010048 int size;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070049};
50
Cory Tusare1379b52016-02-10 14:32:07 -050051static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev *edev)
52{
53 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_SINGLE_WORD_READ;
54}
55
56static inline bool has_quirk_instruction_length(struct eeprom_93xx46_dev *edev)
57{
58 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH;
59}
60
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +010061static int eeprom_93xx46_read(void *priv, unsigned int off,
62 void *val, size_t count)
Anatolij Gustschin06b45012011-07-25 17:13:27 -070063{
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +010064 struct eeprom_93xx46_dev *edev = priv;
65 char *buf = val;
66 int err = 0;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070067
Andrew Lunn1c4b6e22016-02-26 20:59:23 +010068 if (unlikely(off >= edev->size))
69 return 0;
70 if ((off + count) > edev->size)
71 count = edev->size - off;
72 if (unlikely(!count))
73 return count;
Anatolij Gustschin06b45012011-07-25 17:13:27 -070074
Anatolij Gustschin06b45012011-07-25 17:13:27 -070075 mutex_lock(&edev->lock);
76
77 if (edev->pdata->prepare)
78 edev->pdata->prepare(edev);
79
Cory Tusare1379b52016-02-10 14:32:07 -050080 while (count) {
81 struct spi_message m;
82 struct spi_transfer t[2] = { { 0 } };
83 u16 cmd_addr = OP_READ << edev->addrlen;
84 size_t nbytes = count;
85 int bits;
Cory Tusare1379b52016-02-10 14:32:07 -050086
87 if (edev->addrlen == 7) {
88 cmd_addr |= off & 0x7f;
89 bits = 10;
90 if (has_quirk_single_word_read(edev))
91 nbytes = 1;
92 } else {
93 cmd_addr |= (off >> 1) & 0x3f;
94 bits = 9;
95 if (has_quirk_single_word_read(edev))
96 nbytes = 2;
97 }
98
99 dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
100 cmd_addr, edev->spi->max_speed_hz);
101
102 spi_message_init(&m);
103
104 t[0].tx_buf = (char *)&cmd_addr;
105 t[0].len = 2;
106 t[0].bits_per_word = bits;
107 spi_message_add_tail(&t[0], &m);
108
109 t[1].rx_buf = buf;
110 t[1].len = count;
111 t[1].bits_per_word = 8;
112 spi_message_add_tail(&t[1], &m);
113
114 err = spi_sync(edev->spi, &m);
115 /* have to wait at least Tcsl ns */
116 ndelay(250);
117
118 if (err) {
119 dev_err(&edev->spi->dev, "read %zu bytes at %d: err. %d\n",
120 nbytes, (int)off, err);
Cory Tusare1379b52016-02-10 14:32:07 -0500121 break;
122 }
123
124 buf += nbytes;
125 off += nbytes;
126 count -= nbytes;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700127 }
128
129 if (edev->pdata->finish)
130 edev->pdata->finish(edev);
131
132 mutex_unlock(&edev->lock);
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +0100133
134 return err;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700135}
136
137static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
138{
139 struct spi_message m;
140 struct spi_transfer t;
141 int bits, ret;
142 u16 cmd_addr;
143
144 cmd_addr = OP_START << edev->addrlen;
145 if (edev->addrlen == 7) {
146 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
147 bits = 10;
148 } else {
149 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
150 bits = 9;
151 }
152
Cory Tusare1379b52016-02-10 14:32:07 -0500153 if (has_quirk_instruction_length(edev)) {
154 cmd_addr <<= 2;
155 bits += 2;
156 }
157
158 dev_dbg(&edev->spi->dev, "ew%s cmd 0x%04x, %d bits\n",
159 is_on ? "en" : "ds", cmd_addr, bits);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700160
161 spi_message_init(&m);
162 memset(&t, 0, sizeof(t));
163
164 t.tx_buf = &cmd_addr;
165 t.len = 2;
166 t.bits_per_word = bits;
167 spi_message_add_tail(&t, &m);
168
169 mutex_lock(&edev->lock);
170
171 if (edev->pdata->prepare)
172 edev->pdata->prepare(edev);
173
174 ret = spi_sync(edev->spi, &m);
175 /* have to wait at least Tcsl ns */
176 ndelay(250);
177 if (ret)
178 dev_err(&edev->spi->dev, "erase/write %sable error %d\n",
179 is_on ? "en" : "dis", ret);
180
181 if (edev->pdata->finish)
182 edev->pdata->finish(edev);
183
184 mutex_unlock(&edev->lock);
185 return ret;
186}
187
188static ssize_t
189eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
190 const char *buf, unsigned off)
191{
192 struct spi_message m;
193 struct spi_transfer t[2];
194 int bits, data_len, ret;
195 u16 cmd_addr;
196
197 cmd_addr = OP_WRITE << edev->addrlen;
198
199 if (edev->addrlen == 7) {
200 cmd_addr |= off & 0x7f;
201 bits = 10;
202 data_len = 1;
203 } else {
Cory Tusarcb54ad62016-01-06 22:55:00 -0500204 cmd_addr |= (off >> 1) & 0x3f;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700205 bits = 9;
206 data_len = 2;
207 }
208
209 dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
210
211 spi_message_init(&m);
212 memset(t, 0, sizeof(t));
213
214 t[0].tx_buf = (char *)&cmd_addr;
215 t[0].len = 2;
216 t[0].bits_per_word = bits;
217 spi_message_add_tail(&t[0], &m);
218
219 t[1].tx_buf = buf;
220 t[1].len = data_len;
221 t[1].bits_per_word = 8;
222 spi_message_add_tail(&t[1], &m);
223
224 ret = spi_sync(edev->spi, &m);
225 /* have to wait program cycle time Twc ms */
226 mdelay(6);
227 return ret;
228}
229
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +0100230static int eeprom_93xx46_write(void *priv, unsigned int off,
231 void *val, size_t count)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700232{
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +0100233 struct eeprom_93xx46_dev *edev = priv;
234 char *buf = val;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700235 int i, ret, step = 1;
236
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100237 if (unlikely(off >= edev->size))
238 return -EFBIG;
239 if ((off + count) > edev->size)
240 count = edev->size - off;
241 if (unlikely(!count))
242 return count;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700243
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700244 /* only write even number of bytes on 16-bit devices */
245 if (edev->addrlen == 6) {
246 step = 2;
247 count &= ~1;
248 }
249
250 /* erase/write enable */
251 ret = eeprom_93xx46_ew(edev, 1);
252 if (ret)
253 return ret;
254
255 mutex_lock(&edev->lock);
256
257 if (edev->pdata->prepare)
258 edev->pdata->prepare(edev);
259
260 for (i = 0; i < count; i += step) {
261 ret = eeprom_93xx46_write_word(edev, &buf[i], off + i);
262 if (ret) {
263 dev_err(&edev->spi->dev, "write failed at %d: %d\n",
264 (int)off + i, ret);
265 break;
266 }
267 }
268
269 if (edev->pdata->finish)
270 edev->pdata->finish(edev);
271
272 mutex_unlock(&edev->lock);
273
274 /* erase/write disable */
275 eeprom_93xx46_ew(edev, 0);
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +0100276 return ret;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700277}
278
279static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
280{
281 struct eeprom_93xx46_platform_data *pd = edev->pdata;
282 struct spi_message m;
283 struct spi_transfer t;
284 int bits, ret;
285 u16 cmd_addr;
286
287 cmd_addr = OP_START << edev->addrlen;
288 if (edev->addrlen == 7) {
289 cmd_addr |= ADDR_ERAL << 1;
290 bits = 10;
291 } else {
292 cmd_addr |= ADDR_ERAL;
293 bits = 9;
294 }
295
Cory Tusare1379b52016-02-10 14:32:07 -0500296 if (has_quirk_instruction_length(edev)) {
297 cmd_addr <<= 2;
298 bits += 2;
299 }
300
301 dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", cmd_addr, bits);
302
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700303 spi_message_init(&m);
304 memset(&t, 0, sizeof(t));
305
306 t.tx_buf = &cmd_addr;
307 t.len = 2;
308 t.bits_per_word = bits;
309 spi_message_add_tail(&t, &m);
310
311 mutex_lock(&edev->lock);
312
313 if (edev->pdata->prepare)
314 edev->pdata->prepare(edev);
315
316 ret = spi_sync(edev->spi, &m);
317 if (ret)
318 dev_err(&edev->spi->dev, "erase error %d\n", ret);
319 /* have to wait erase cycle time Tec ms */
320 mdelay(6);
321
322 if (pd->finish)
323 pd->finish(edev);
324
325 mutex_unlock(&edev->lock);
326 return ret;
327}
328
329static ssize_t eeprom_93xx46_store_erase(struct device *dev,
330 struct device_attribute *attr,
331 const char *buf, size_t count)
332{
333 struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev);
334 int erase = 0, ret;
335
336 sscanf(buf, "%d", &erase);
337 if (erase) {
338 ret = eeprom_93xx46_ew(edev, 1);
339 if (ret)
340 return ret;
341 ret = eeprom_93xx46_eral(edev);
342 if (ret)
343 return ret;
344 ret = eeprom_93xx46_ew(edev, 0);
345 if (ret)
346 return ret;
347 }
348 return count;
349}
350static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
351
Cory Tusar3ca9b1a2016-02-10 14:32:08 -0500352static void select_assert(void *context)
353{
354 struct eeprom_93xx46_dev *edev = context;
355
356 gpiod_set_value_cansleep(edev->pdata->select, 1);
357}
358
359static void select_deassert(void *context)
360{
361 struct eeprom_93xx46_dev *edev = context;
362
363 gpiod_set_value_cansleep(edev->pdata->select, 0);
364}
365
Cory Tusarc074abe2016-01-06 22:55:02 -0500366static const struct of_device_id eeprom_93xx46_of_table[] = {
367 { .compatible = "eeprom-93xx46", },
Cory Tusare1379b52016-02-10 14:32:07 -0500368 { .compatible = "atmel,at93c46d", .data = &atmel_at93c46d_data, },
Cory Tusarc074abe2016-01-06 22:55:02 -0500369 {}
370};
371MODULE_DEVICE_TABLE(of, eeprom_93xx46_of_table);
372
373static int eeprom_93xx46_probe_dt(struct spi_device *spi)
374{
Cory Tusare1379b52016-02-10 14:32:07 -0500375 const struct of_device_id *of_id =
376 of_match_device(eeprom_93xx46_of_table, &spi->dev);
Cory Tusarc074abe2016-01-06 22:55:02 -0500377 struct device_node *np = spi->dev.of_node;
378 struct eeprom_93xx46_platform_data *pd;
379 u32 tmp;
Cory Tusar3ca9b1a2016-02-10 14:32:08 -0500380 int gpio;
381 enum of_gpio_flags of_flags;
Cory Tusarc074abe2016-01-06 22:55:02 -0500382 int ret;
383
384 pd = devm_kzalloc(&spi->dev, sizeof(*pd), GFP_KERNEL);
385 if (!pd)
386 return -ENOMEM;
387
388 ret = of_property_read_u32(np, "data-size", &tmp);
389 if (ret < 0) {
390 dev_err(&spi->dev, "data-size property not found\n");
391 return ret;
392 }
393
394 if (tmp == 8) {
395 pd->flags |= EE_ADDR8;
396 } else if (tmp == 16) {
397 pd->flags |= EE_ADDR16;
398 } else {
399 dev_err(&spi->dev, "invalid data-size (%d)\n", tmp);
400 return -EINVAL;
401 }
402
403 if (of_property_read_bool(np, "read-only"))
404 pd->flags |= EE_READONLY;
405
Cory Tusar3ca9b1a2016-02-10 14:32:08 -0500406 gpio = of_get_named_gpio_flags(np, "select-gpios", 0, &of_flags);
407 if (gpio_is_valid(gpio)) {
408 unsigned long flags =
409 of_flags == OF_GPIO_ACTIVE_LOW ? GPIOF_ACTIVE_LOW : 0;
410
411 ret = devm_gpio_request_one(&spi->dev, gpio, flags,
412 "eeprom_93xx46_select");
413 if (ret)
414 return ret;
415
416 pd->select = gpio_to_desc(gpio);
417 pd->prepare = select_assert;
418 pd->finish = select_deassert;
419
420 gpiod_direction_output(pd->select, 0);
421 }
422
Cory Tusare1379b52016-02-10 14:32:07 -0500423 if (of_id->data) {
424 const struct eeprom_93xx46_devtype_data *data = of_id->data;
425
426 pd->quirks = data->quirks;
427 }
428
Cory Tusarc074abe2016-01-06 22:55:02 -0500429 spi->dev.platform_data = pd;
430
431 return 0;
432}
433
Bill Pemberton80c8ae22012-11-19 13:23:05 -0500434static int eeprom_93xx46_probe(struct spi_device *spi)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700435{
436 struct eeprom_93xx46_platform_data *pd;
437 struct eeprom_93xx46_dev *edev;
438 int err;
439
Cory Tusarc074abe2016-01-06 22:55:02 -0500440 if (spi->dev.of_node) {
441 err = eeprom_93xx46_probe_dt(spi);
442 if (err < 0)
443 return err;
444 }
445
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700446 pd = spi->dev.platform_data;
447 if (!pd) {
448 dev_err(&spi->dev, "missing platform data\n");
449 return -ENODEV;
450 }
451
452 edev = kzalloc(sizeof(*edev), GFP_KERNEL);
453 if (!edev)
454 return -ENOMEM;
455
456 if (pd->flags & EE_ADDR8)
457 edev->addrlen = 7;
458 else if (pd->flags & EE_ADDR16)
459 edev->addrlen = 6;
460 else {
461 dev_err(&spi->dev, "unspecified address type\n");
462 err = -EINVAL;
463 goto fail;
464 }
465
466 mutex_init(&edev->lock);
467
Mark Browndd69a182016-04-20 10:16:36 +0100468 edev->spi = spi;
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700469 edev->pdata = pd;
470
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100471 edev->size = 128;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100472 edev->nvmem_config.name = dev_name(&spi->dev);
473 edev->nvmem_config.dev = &spi->dev;
474 edev->nvmem_config.read_only = pd->flags & EE_READONLY;
475 edev->nvmem_config.root_only = true;
476 edev->nvmem_config.owner = THIS_MODULE;
477 edev->nvmem_config.compat = true;
478 edev->nvmem_config.base_dev = &spi->dev;
Srinivas Kandagatlaa8ab3162016-04-24 20:28:16 +0100479 edev->nvmem_config.reg_read = eeprom_93xx46_read;
480 edev->nvmem_config.reg_write = eeprom_93xx46_write;
481 edev->nvmem_config.priv = edev;
482 edev->nvmem_config.stride = 4;
483 edev->nvmem_config.word_size = 1;
484 edev->nvmem_config.size = edev->size;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100485
486 edev->nvmem = nvmem_register(&edev->nvmem_config);
487 if (IS_ERR(edev->nvmem)) {
488 err = PTR_ERR(edev->nvmem);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700489 goto fail;
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100490 }
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700491
492 dev_info(&spi->dev, "%d-bit eeprom %s\n",
493 (pd->flags & EE_ADDR8) ? 8 : 16,
494 (pd->flags & EE_READONLY) ? "(readonly)" : "");
495
496 if (!(pd->flags & EE_READONLY)) {
497 if (device_create_file(&spi->dev, &dev_attr_erase))
498 dev_err(&spi->dev, "can't create erase interface\n");
499 }
500
Jingoo Han5ba75b52013-04-05 10:56:04 +0900501 spi_set_drvdata(spi, edev);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700502 return 0;
503fail:
504 kfree(edev);
505 return err;
506}
507
Bill Pemberton486a5c22012-11-19 13:26:02 -0500508static int eeprom_93xx46_remove(struct spi_device *spi)
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700509{
Jingoo Han5ba75b52013-04-05 10:56:04 +0900510 struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700511
Andrew Lunn1c4b6e22016-02-26 20:59:23 +0100512 nvmem_unregister(edev->nvmem);
513
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700514 if (!(edev->pdata->flags & EE_READONLY))
515 device_remove_file(&spi->dev, &dev_attr_erase);
516
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700517 kfree(edev);
518 return 0;
519}
520
521static struct spi_driver eeprom_93xx46_driver = {
522 .driver = {
523 .name = "93xx46",
Cory Tusarc074abe2016-01-06 22:55:02 -0500524 .of_match_table = of_match_ptr(eeprom_93xx46_of_table),
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700525 },
526 .probe = eeprom_93xx46_probe,
Bill Pemberton2d6bed92012-11-19 13:21:23 -0500527 .remove = eeprom_93xx46_remove,
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700528};
529
Axel Lina3dc3c92012-01-22 15:38:22 +0800530module_spi_driver(eeprom_93xx46_driver);
Anatolij Gustschin06b45012011-07-25 17:13:27 -0700531
532MODULE_LICENSE("GPL");
533MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
534MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
535MODULE_ALIAS("spi:93xx46");