1 // SPDX-License-Identifier: GPL-2.0-only
3 * Driver for 93xx46 EEPROMs
5 * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
8 #include <linux/delay.h>
9 #include <linux/device.h>
10 #include <linux/gpio/consumer.h>
11 #include <linux/kernel.h>
12 #include <linux/log2.h>
13 #include <linux/module.h>
14 #include <linux/mutex.h>
16 #include <linux/of_device.h>
17 #include <linux/of_gpio.h>
18 #include <linux/slab.h>
19 #include <linux/spi/spi.h>
20 #include <linux/nvmem-provider.h>
21 #include <linux/eeprom_93xx46.h>
24 #define OP_WRITE (OP_START | 0x1)
25 #define OP_READ (OP_START | 0x2)
26 #define ADDR_EWDS 0x00
27 #define ADDR_ERAL 0x20
28 #define ADDR_EWEN 0x30
30 struct eeprom_93xx46_devtype_data {
35 static const struct eeprom_93xx46_devtype_data at93c46_data = {
39 static const struct eeprom_93xx46_devtype_data at93c56_data = {
43 static const struct eeprom_93xx46_devtype_data at93c66_data = {
47 static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data = {
49 .quirks = EEPROM_93XX46_QUIRK_SINGLE_WORD_READ |
50 EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH,
53 static const struct eeprom_93xx46_devtype_data microchip_93lc46b_data = {
55 .quirks = EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE,
58 struct eeprom_93xx46_dev {
59 struct spi_device *spi;
60 struct eeprom_93xx46_platform_data *pdata;
62 struct nvmem_config nvmem_config;
63 struct nvmem_device *nvmem;
68 static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev *edev)
70 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_SINGLE_WORD_READ;
73 static inline bool has_quirk_instruction_length(struct eeprom_93xx46_dev *edev)
75 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH;
78 static inline bool has_quirk_extra_read_cycle(struct eeprom_93xx46_dev *edev)
80 return edev->pdata->quirks & EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE;
83 static int eeprom_93xx46_read(void *priv, unsigned int off,
84 void *val, size_t count)
86 struct eeprom_93xx46_dev *edev = priv;
91 if (unlikely(off >= edev->size))
93 if ((off + count) > edev->size)
94 count = edev->size - off;
98 mutex_lock(&edev->lock);
100 if (edev->pdata->prepare)
101 edev->pdata->prepare(edev);
103 /* The opcode in front of the address is three bits. */
104 bits = edev->addrlen + 3;
107 struct spi_message m;
108 struct spi_transfer t[2] = { { 0 } };
109 u16 cmd_addr = OP_READ << edev->addrlen;
110 size_t nbytes = count;
112 if (edev->pdata->flags & EE_ADDR8) {
114 if (has_quirk_single_word_read(edev))
117 cmd_addr |= (off >> 1);
118 if (has_quirk_single_word_read(edev))
122 dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
123 cmd_addr, edev->spi->max_speed_hz);
125 if (has_quirk_extra_read_cycle(edev)) {
130 spi_message_init(&m);
132 t[0].tx_buf = (char *)&cmd_addr;
134 t[0].bits_per_word = bits;
135 spi_message_add_tail(&t[0], &m);
139 t[1].bits_per_word = 8;
140 spi_message_add_tail(&t[1], &m);
142 err = spi_sync(edev->spi, &m);
143 /* have to wait at least Tcsl ns */
147 dev_err(&edev->spi->dev, "read %zu bytes at %d: err. %d\n",
148 nbytes, (int)off, err);
157 if (edev->pdata->finish)
158 edev->pdata->finish(edev);
160 mutex_unlock(&edev->lock);
165 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
167 struct spi_message m;
168 struct spi_transfer t;
172 /* The opcode in front of the address is three bits. */
173 bits = edev->addrlen + 3;
175 cmd_addr = OP_START << edev->addrlen;
176 if (edev->pdata->flags & EE_ADDR8)
177 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
179 cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
181 if (has_quirk_instruction_length(edev)) {
186 dev_dbg(&edev->spi->dev, "ew%s cmd 0x%04x, %d bits\n",
187 is_on ? "en" : "ds", cmd_addr, bits);
189 spi_message_init(&m);
190 memset(&t, 0, sizeof(t));
192 t.tx_buf = &cmd_addr;
194 t.bits_per_word = bits;
195 spi_message_add_tail(&t, &m);
197 mutex_lock(&edev->lock);
199 if (edev->pdata->prepare)
200 edev->pdata->prepare(edev);
202 ret = spi_sync(edev->spi, &m);
203 /* have to wait at least Tcsl ns */
206 dev_err(&edev->spi->dev, "erase/write %sable error %d\n",
207 is_on ? "en" : "dis", ret);
209 if (edev->pdata->finish)
210 edev->pdata->finish(edev);
212 mutex_unlock(&edev->lock);
217 eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
218 const char *buf, unsigned off)
220 struct spi_message m;
221 struct spi_transfer t[2];
222 int bits, data_len, ret;
225 if (unlikely(off >= edev->size))
228 /* The opcode in front of the address is three bits. */
229 bits = edev->addrlen + 3;
231 cmd_addr = OP_WRITE << edev->addrlen;
233 if (edev->pdata->flags & EE_ADDR8) {
237 cmd_addr |= (off >> 1);
241 dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
243 spi_message_init(&m);
244 memset(t, 0, sizeof(t));
246 t[0].tx_buf = (char *)&cmd_addr;
248 t[0].bits_per_word = bits;
249 spi_message_add_tail(&t[0], &m);
253 t[1].bits_per_word = 8;
254 spi_message_add_tail(&t[1], &m);
256 ret = spi_sync(edev->spi, &m);
257 /* have to wait program cycle time Twc ms */
262 static int eeprom_93xx46_write(void *priv, unsigned int off,
263 void *val, size_t count)
265 struct eeprom_93xx46_dev *edev = priv;
267 int i, ret, step = 1;
269 if (unlikely(off >= edev->size))
271 if ((off + count) > edev->size)
272 count = edev->size - off;
273 if (unlikely(!count))
276 /* only write even number of bytes on 16-bit devices */
277 if (edev->pdata->flags & EE_ADDR16) {
282 /* erase/write enable */
283 ret = eeprom_93xx46_ew(edev, 1);
287 mutex_lock(&edev->lock);
289 if (edev->pdata->prepare)
290 edev->pdata->prepare(edev);
292 for (i = 0; i < count; i += step) {
293 ret = eeprom_93xx46_write_word(edev, &buf[i], off + i);
295 dev_err(&edev->spi->dev, "write failed at %d: %d\n",
301 if (edev->pdata->finish)
302 edev->pdata->finish(edev);
304 mutex_unlock(&edev->lock);
306 /* erase/write disable */
307 eeprom_93xx46_ew(edev, 0);
311 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
313 struct eeprom_93xx46_platform_data *pd = edev->pdata;
314 struct spi_message m;
315 struct spi_transfer t;
319 /* The opcode in front of the address is three bits. */
320 bits = edev->addrlen + 3;
322 cmd_addr = OP_START << edev->addrlen;
323 if (edev->pdata->flags & EE_ADDR8)
324 cmd_addr |= ADDR_ERAL << 1;
326 cmd_addr |= ADDR_ERAL;
328 if (has_quirk_instruction_length(edev)) {
333 dev_dbg(&edev->spi->dev, "eral cmd 0x%04x, %d bits\n", cmd_addr, bits);
335 spi_message_init(&m);
336 memset(&t, 0, sizeof(t));
338 t.tx_buf = &cmd_addr;
340 t.bits_per_word = bits;
341 spi_message_add_tail(&t, &m);
343 mutex_lock(&edev->lock);
345 if (edev->pdata->prepare)
346 edev->pdata->prepare(edev);
348 ret = spi_sync(edev->spi, &m);
350 dev_err(&edev->spi->dev, "erase error %d\n", ret);
351 /* have to wait erase cycle time Tec ms */
357 mutex_unlock(&edev->lock);
361 static ssize_t eeprom_93xx46_store_erase(struct device *dev,
362 struct device_attribute *attr,
363 const char *buf, size_t count)
365 struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev);
368 sscanf(buf, "%d", &erase);
370 ret = eeprom_93xx46_ew(edev, 1);
373 ret = eeprom_93xx46_eral(edev);
376 ret = eeprom_93xx46_ew(edev, 0);
382 static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
384 static void select_assert(void *context)
386 struct eeprom_93xx46_dev *edev = context;
388 gpiod_set_value_cansleep(edev->pdata->select, 1);
391 static void select_deassert(void *context)
393 struct eeprom_93xx46_dev *edev = context;
395 gpiod_set_value_cansleep(edev->pdata->select, 0);
398 static const struct of_device_id eeprom_93xx46_of_table[] = {
399 { .compatible = "eeprom-93xx46", .data = &at93c46_data, },
400 { .compatible = "atmel,at93c46", .data = &at93c46_data, },
401 { .compatible = "atmel,at93c46d", .data = &atmel_at93c46d_data, },
402 { .compatible = "atmel,at93c56", .data = &at93c56_data, },
403 { .compatible = "atmel,at93c66", .data = &at93c66_data, },
404 { .compatible = "microchip,93lc46b", .data = µchip_93lc46b_data, },
407 MODULE_DEVICE_TABLE(of, eeprom_93xx46_of_table);
409 static const struct spi_device_id eeprom_93xx46_spi_ids[] = {
410 { .name = "eeprom-93xx46",
411 .driver_data = (kernel_ulong_t)&at93c46_data, },
413 .driver_data = (kernel_ulong_t)&at93c46_data, },
414 { .name = "at93c46d",
415 .driver_data = (kernel_ulong_t)&atmel_at93c46d_data, },
417 .driver_data = (kernel_ulong_t)&at93c56_data, },
419 .driver_data = (kernel_ulong_t)&at93c66_data, },
421 .driver_data = (kernel_ulong_t)µchip_93lc46b_data, },
424 MODULE_DEVICE_TABLE(spi, eeprom_93xx46_spi_ids);
426 static int eeprom_93xx46_probe_dt(struct spi_device *spi)
428 const struct of_device_id *of_id =
429 of_match_device(eeprom_93xx46_of_table, &spi->dev);
430 struct device_node *np = spi->dev.of_node;
431 struct eeprom_93xx46_platform_data *pd;
435 pd = devm_kzalloc(&spi->dev, sizeof(*pd), GFP_KERNEL);
439 ret = of_property_read_u32(np, "data-size", &tmp);
441 dev_err(&spi->dev, "data-size property not found\n");
446 pd->flags |= EE_ADDR8;
447 } else if (tmp == 16) {
448 pd->flags |= EE_ADDR16;
450 dev_err(&spi->dev, "invalid data-size (%d)\n", tmp);
454 if (of_property_read_bool(np, "read-only"))
455 pd->flags |= EE_READONLY;
457 pd->select = devm_gpiod_get_optional(&spi->dev, "select",
459 if (IS_ERR(pd->select))
460 return PTR_ERR(pd->select);
462 pd->prepare = select_assert;
463 pd->finish = select_deassert;
464 gpiod_direction_output(pd->select, 0);
467 const struct eeprom_93xx46_devtype_data *data = of_id->data;
469 pd->quirks = data->quirks;
470 pd->flags |= data->flags;
473 spi->dev.platform_data = pd;
478 static int eeprom_93xx46_probe(struct spi_device *spi)
480 struct eeprom_93xx46_platform_data *pd;
481 struct eeprom_93xx46_dev *edev;
484 if (spi->dev.of_node) {
485 err = eeprom_93xx46_probe_dt(spi);
490 pd = spi->dev.platform_data;
492 dev_err(&spi->dev, "missing platform data\n");
496 edev = devm_kzalloc(&spi->dev, sizeof(*edev), GFP_KERNEL);
500 if (pd->flags & EE_SIZE1K)
502 else if (pd->flags & EE_SIZE2K)
504 else if (pd->flags & EE_SIZE4K)
507 dev_err(&spi->dev, "unspecified size\n");
511 if (pd->flags & EE_ADDR8)
512 edev->addrlen = ilog2(edev->size);
513 else if (pd->flags & EE_ADDR16)
514 edev->addrlen = ilog2(edev->size) - 1;
516 dev_err(&spi->dev, "unspecified address type\n");
520 mutex_init(&edev->lock);
525 edev->nvmem_config.type = NVMEM_TYPE_EEPROM;
526 edev->nvmem_config.name = dev_name(&spi->dev);
527 edev->nvmem_config.dev = &spi->dev;
528 edev->nvmem_config.read_only = pd->flags & EE_READONLY;
529 edev->nvmem_config.root_only = true;
530 edev->nvmem_config.owner = THIS_MODULE;
531 edev->nvmem_config.compat = true;
532 edev->nvmem_config.base_dev = &spi->dev;
533 edev->nvmem_config.reg_read = eeprom_93xx46_read;
534 edev->nvmem_config.reg_write = eeprom_93xx46_write;
535 edev->nvmem_config.priv = edev;
536 edev->nvmem_config.stride = 4;
537 edev->nvmem_config.word_size = 1;
538 edev->nvmem_config.size = edev->size;
540 edev->nvmem = devm_nvmem_register(&spi->dev, &edev->nvmem_config);
541 if (IS_ERR(edev->nvmem))
542 return PTR_ERR(edev->nvmem);
544 dev_info(&spi->dev, "%d-bit eeprom containing %d bytes %s\n",
545 (pd->flags & EE_ADDR8) ? 8 : 16,
547 (pd->flags & EE_READONLY) ? "(readonly)" : "");
549 if (!(pd->flags & EE_READONLY)) {
550 if (device_create_file(&spi->dev, &dev_attr_erase))
551 dev_err(&spi->dev, "can't create erase interface\n");
554 spi_set_drvdata(spi, edev);
558 static void eeprom_93xx46_remove(struct spi_device *spi)
560 struct eeprom_93xx46_dev *edev = spi_get_drvdata(spi);
562 if (!(edev->pdata->flags & EE_READONLY))
563 device_remove_file(&spi->dev, &dev_attr_erase);
566 static struct spi_driver eeprom_93xx46_driver = {
569 .of_match_table = of_match_ptr(eeprom_93xx46_of_table),
571 .probe = eeprom_93xx46_probe,
572 .remove = eeprom_93xx46_remove,
573 .id_table = eeprom_93xx46_spi_ids,
576 module_spi_driver(eeprom_93xx46_driver);
578 MODULE_LICENSE("GPL");
579 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
580 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
581 MODULE_ALIAS("spi:93xx46");
582 MODULE_ALIAS("spi:eeprom-93xx46");
583 MODULE_ALIAS("spi:93lc46b");