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/array_size.h>
9 #include <linux/bits.h>
10 #include <linux/delay.h>
11 #include <linux/device.h>
12 #include <linux/gpio/consumer.h>
13 #include <linux/kstrtox.h>
14 #include <linux/log2.h>
15 #include <linux/mod_devicetable.h>
16 #include <linux/module.h>
17 #include <linux/mutex.h>
18 #include <linux/property.h>
19 #include <linux/slab.h>
20 #include <linux/spi/spi.h>
21 #include <linux/string_choices.h>
23 #include <linux/nvmem-provider.h>
25 struct eeprom_93xx46_platform_data
{
27 #define EE_ADDR8 0x01 /* 8 bit addr. cfg */
28 #define EE_ADDR16 0x02 /* 16 bit addr. cfg */
29 #define EE_READONLY 0x08 /* forbid writing */
30 #define EE_SIZE1K 0x10 /* 1 kb of data, that is a 93xx46 */
31 #define EE_SIZE2K 0x20 /* 2 kb of data, that is a 93xx56 */
32 #define EE_SIZE4K 0x40 /* 4 kb of data, that is a 93xx66 */
35 /* Single word read transfers only; no sequential read. */
36 #define EEPROM_93XX46_QUIRK_SINGLE_WORD_READ (1 << 0)
37 /* Instructions such as EWEN are (addrlen + 2) in length. */
38 #define EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH (1 << 1)
39 /* Add extra cycle after address during a read */
40 #define EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE BIT(2)
42 struct gpio_desc
*select
;
46 #define OP_WRITE (OP_START | 0x1)
47 #define OP_READ (OP_START | 0x2)
48 #define ADDR_EWDS 0x00
49 #define ADDR_ERAL 0x20
50 #define ADDR_EWEN 0x30
52 struct eeprom_93xx46_devtype_data
{
57 static const struct eeprom_93xx46_devtype_data at93c46_data
= {
61 static const struct eeprom_93xx46_devtype_data at93c56_data
= {
65 static const struct eeprom_93xx46_devtype_data at93c66_data
= {
69 static const struct eeprom_93xx46_devtype_data atmel_at93c46d_data
= {
71 .quirks
= EEPROM_93XX46_QUIRK_SINGLE_WORD_READ
|
72 EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH
,
75 static const struct eeprom_93xx46_devtype_data microchip_93lc46b_data
= {
77 .quirks
= EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE
,
80 struct eeprom_93xx46_dev
{
81 struct spi_device
*spi
;
82 struct eeprom_93xx46_platform_data
*pdata
;
84 struct nvmem_config nvmem_config
;
85 struct nvmem_device
*nvmem
;
90 static inline bool has_quirk_single_word_read(struct eeprom_93xx46_dev
*edev
)
92 return edev
->pdata
->quirks
& EEPROM_93XX46_QUIRK_SINGLE_WORD_READ
;
95 static inline bool has_quirk_instruction_length(struct eeprom_93xx46_dev
*edev
)
97 return edev
->pdata
->quirks
& EEPROM_93XX46_QUIRK_INSTRUCTION_LENGTH
;
100 static inline bool has_quirk_extra_read_cycle(struct eeprom_93xx46_dev
*edev
)
102 return edev
->pdata
->quirks
& EEPROM_93XX46_QUIRK_EXTRA_READ_CYCLE
;
105 static int eeprom_93xx46_read(void *priv
, unsigned int off
,
106 void *val
, size_t count
)
108 struct eeprom_93xx46_dev
*edev
= priv
;
113 if (unlikely(off
>= edev
->size
))
115 if ((off
+ count
) > edev
->size
)
116 count
= edev
->size
- off
;
117 if (unlikely(!count
))
120 mutex_lock(&edev
->lock
);
122 gpiod_set_value_cansleep(edev
->pdata
->select
, 1);
124 /* The opcode in front of the address is three bits. */
125 bits
= edev
->addrlen
+ 3;
128 struct spi_message m
;
129 struct spi_transfer t
[2] = {};
130 u16 cmd_addr
= OP_READ
<< edev
->addrlen
;
131 size_t nbytes
= count
;
133 if (edev
->pdata
->flags
& EE_ADDR8
) {
135 if (has_quirk_single_word_read(edev
))
138 cmd_addr
|= (off
>> 1);
139 if (has_quirk_single_word_read(edev
))
143 dev_dbg(&edev
->spi
->dev
, "read cmd 0x%x, %d Hz\n",
144 cmd_addr
, edev
->spi
->max_speed_hz
);
146 if (has_quirk_extra_read_cycle(edev
)) {
151 t
[0].tx_buf
= (char *)&cmd_addr
;
153 t
[0].bits_per_word
= bits
;
157 t
[1].bits_per_word
= 8;
159 spi_message_init_with_transfers(&m
, t
, ARRAY_SIZE(t
));
161 err
= spi_sync(edev
->spi
, &m
);
162 /* have to wait at least Tcsl ns */
166 dev_err(&edev
->spi
->dev
, "read %zu bytes at %u: err. %d\n",
176 gpiod_set_value_cansleep(edev
->pdata
->select
, 0);
178 mutex_unlock(&edev
->lock
);
183 static int eeprom_93xx46_ew(struct eeprom_93xx46_dev
*edev
, int is_on
)
185 struct spi_message m
;
186 struct spi_transfer t
= {};
190 /* The opcode in front of the address is three bits. */
191 bits
= edev
->addrlen
+ 3;
193 cmd_addr
= OP_START
<< edev
->addrlen
;
194 if (edev
->pdata
->flags
& EE_ADDR8
)
195 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
) << 1;
197 cmd_addr
|= (is_on
? ADDR_EWEN
: ADDR_EWDS
);
199 if (has_quirk_instruction_length(edev
)) {
204 dev_dbg(&edev
->spi
->dev
, "ew %s cmd 0x%04x, %d bits\n",
205 str_enable_disable(is_on
), cmd_addr
, bits
);
207 t
.tx_buf
= &cmd_addr
;
209 t
.bits_per_word
= bits
;
211 spi_message_init_with_transfers(&m
, &t
, 1);
213 mutex_lock(&edev
->lock
);
215 gpiod_set_value_cansleep(edev
->pdata
->select
, 1);
217 ret
= spi_sync(edev
->spi
, &m
);
218 /* have to wait at least Tcsl ns */
221 dev_err(&edev
->spi
->dev
, "erase/write %s error %d\n",
222 str_enable_disable(is_on
), ret
);
224 gpiod_set_value_cansleep(edev
->pdata
->select
, 0);
226 mutex_unlock(&edev
->lock
);
231 eeprom_93xx46_write_word(struct eeprom_93xx46_dev
*edev
,
232 const char *buf
, unsigned int off
)
234 struct spi_message m
;
235 struct spi_transfer t
[2] = {};
236 int bits
, data_len
, ret
;
239 if (unlikely(off
>= edev
->size
))
242 /* The opcode in front of the address is three bits. */
243 bits
= edev
->addrlen
+ 3;
245 cmd_addr
= OP_WRITE
<< edev
->addrlen
;
247 if (edev
->pdata
->flags
& EE_ADDR8
) {
251 cmd_addr
|= (off
>> 1);
255 dev_dbg(&edev
->spi
->dev
, "write cmd 0x%x\n", cmd_addr
);
257 t
[0].tx_buf
= (char *)&cmd_addr
;
259 t
[0].bits_per_word
= bits
;
263 t
[1].bits_per_word
= 8;
265 spi_message_init_with_transfers(&m
, t
, ARRAY_SIZE(t
));
267 ret
= spi_sync(edev
->spi
, &m
);
268 /* have to wait program cycle time Twc ms */
273 static int eeprom_93xx46_write(void *priv
, unsigned int off
,
274 void *val
, size_t count
)
276 struct eeprom_93xx46_dev
*edev
= priv
;
281 if (unlikely(off
>= edev
->size
))
283 if ((off
+ count
) > edev
->size
)
284 count
= edev
->size
- off
;
285 if (unlikely(!count
))
288 /* only write even number of bytes on 16-bit devices */
289 if (edev
->pdata
->flags
& EE_ADDR16
) {
294 /* erase/write enable */
295 ret
= eeprom_93xx46_ew(edev
, 1);
299 mutex_lock(&edev
->lock
);
301 gpiod_set_value_cansleep(edev
->pdata
->select
, 1);
303 for (i
= 0; i
< count
; i
+= step
) {
304 ret
= eeprom_93xx46_write_word(edev
, &buf
[i
], off
+ i
);
306 dev_err(&edev
->spi
->dev
, "write failed at %u: %d\n", off
+ i
, ret
);
311 gpiod_set_value_cansleep(edev
->pdata
->select
, 0);
313 mutex_unlock(&edev
->lock
);
315 /* erase/write disable */
316 eeprom_93xx46_ew(edev
, 0);
320 static int eeprom_93xx46_eral(struct eeprom_93xx46_dev
*edev
)
322 struct spi_message m
;
323 struct spi_transfer t
= {};
327 /* The opcode in front of the address is three bits. */
328 bits
= edev
->addrlen
+ 3;
330 cmd_addr
= OP_START
<< edev
->addrlen
;
331 if (edev
->pdata
->flags
& EE_ADDR8
)
332 cmd_addr
|= ADDR_ERAL
<< 1;
334 cmd_addr
|= ADDR_ERAL
;
336 if (has_quirk_instruction_length(edev
)) {
341 dev_dbg(&edev
->spi
->dev
, "eral cmd 0x%04x, %d bits\n", cmd_addr
, bits
);
343 t
.tx_buf
= &cmd_addr
;
345 t
.bits_per_word
= bits
;
347 spi_message_init_with_transfers(&m
, &t
, 1);
349 mutex_lock(&edev
->lock
);
351 gpiod_set_value_cansleep(edev
->pdata
->select
, 1);
353 ret
= spi_sync(edev
->spi
, &m
);
355 dev_err(&edev
->spi
->dev
, "erase error %d\n", ret
);
356 /* have to wait erase cycle time Tec ms */
359 gpiod_set_value_cansleep(edev
->pdata
->select
, 0);
361 mutex_unlock(&edev
->lock
);
365 static ssize_t
erase_store(struct device
*dev
, struct device_attribute
*attr
,
366 const char *buf
, size_t count
)
368 struct eeprom_93xx46_dev
*edev
= dev_get_drvdata(dev
);
372 ret
= kstrtobool(buf
, &erase
);
377 ret
= eeprom_93xx46_ew(edev
, 1);
380 ret
= eeprom_93xx46_eral(edev
);
383 ret
= eeprom_93xx46_ew(edev
, 0);
389 static DEVICE_ATTR_WO(erase
);
391 static const struct of_device_id eeprom_93xx46_of_table
[] = {
392 { .compatible
= "eeprom-93xx46", .data
= &at93c46_data
, },
393 { .compatible
= "atmel,at93c46", .data
= &at93c46_data
, },
394 { .compatible
= "atmel,at93c46d", .data
= &atmel_at93c46d_data
, },
395 { .compatible
= "atmel,at93c56", .data
= &at93c56_data
, },
396 { .compatible
= "atmel,at93c66", .data
= &at93c66_data
, },
397 { .compatible
= "microchip,93lc46b", .data
= µchip_93lc46b_data
, },
400 MODULE_DEVICE_TABLE(of
, eeprom_93xx46_of_table
);
402 static const struct spi_device_id eeprom_93xx46_spi_ids
[] = {
403 { .name
= "eeprom-93xx46",
404 .driver_data
= (kernel_ulong_t
)&at93c46_data
, },
406 .driver_data
= (kernel_ulong_t
)&at93c46_data
, },
407 { .name
= "at93c46d",
408 .driver_data
= (kernel_ulong_t
)&atmel_at93c46d_data
, },
410 .driver_data
= (kernel_ulong_t
)&at93c56_data
, },
412 .driver_data
= (kernel_ulong_t
)&at93c66_data
, },
414 .driver_data
= (kernel_ulong_t
)µchip_93lc46b_data
, },
417 MODULE_DEVICE_TABLE(spi
, eeprom_93xx46_spi_ids
);
419 static int eeprom_93xx46_probe_fw(struct device
*dev
)
421 const struct eeprom_93xx46_devtype_data
*data
;
422 struct eeprom_93xx46_platform_data
*pd
;
426 pd
= devm_kzalloc(dev
, sizeof(*pd
), GFP_KERNEL
);
430 ret
= device_property_read_u32(dev
, "data-size", &tmp
);
432 dev_err(dev
, "data-size property not found\n");
437 pd
->flags
|= EE_ADDR8
;
438 } else if (tmp
== 16) {
439 pd
->flags
|= EE_ADDR16
;
441 dev_err(dev
, "invalid data-size (%d)\n", tmp
);
445 if (device_property_read_bool(dev
, "read-only"))
446 pd
->flags
|= EE_READONLY
;
448 pd
->select
= devm_gpiod_get_optional(dev
, "select", GPIOD_OUT_LOW
);
449 if (IS_ERR(pd
->select
))
450 return PTR_ERR(pd
->select
);
451 gpiod_set_consumer_name(pd
->select
, "93xx46 EEPROMs OE");
453 data
= spi_get_device_match_data(to_spi_device(dev
));
455 pd
->quirks
= data
->quirks
;
456 pd
->flags
|= data
->flags
;
459 dev
->platform_data
= pd
;
464 static int eeprom_93xx46_probe(struct spi_device
*spi
)
466 struct eeprom_93xx46_platform_data
*pd
;
467 struct eeprom_93xx46_dev
*edev
;
468 struct device
*dev
= &spi
->dev
;
471 err
= eeprom_93xx46_probe_fw(dev
);
475 pd
= spi
->dev
.platform_data
;
477 dev_err(&spi
->dev
, "missing platform data\n");
481 edev
= devm_kzalloc(&spi
->dev
, sizeof(*edev
), GFP_KERNEL
);
485 if (pd
->flags
& EE_SIZE1K
)
487 else if (pd
->flags
& EE_SIZE2K
)
489 else if (pd
->flags
& EE_SIZE4K
)
492 dev_err(&spi
->dev
, "unspecified size\n");
496 if (pd
->flags
& EE_ADDR8
)
497 edev
->addrlen
= ilog2(edev
->size
);
498 else if (pd
->flags
& EE_ADDR16
)
499 edev
->addrlen
= ilog2(edev
->size
) - 1;
501 dev_err(&spi
->dev
, "unspecified address type\n");
505 mutex_init(&edev
->lock
);
510 edev
->nvmem_config
.type
= NVMEM_TYPE_EEPROM
;
511 edev
->nvmem_config
.name
= dev_name(&spi
->dev
);
512 edev
->nvmem_config
.dev
= &spi
->dev
;
513 edev
->nvmem_config
.read_only
= pd
->flags
& EE_READONLY
;
514 edev
->nvmem_config
.root_only
= true;
515 edev
->nvmem_config
.owner
= THIS_MODULE
;
516 edev
->nvmem_config
.compat
= true;
517 edev
->nvmem_config
.base_dev
= &spi
->dev
;
518 edev
->nvmem_config
.reg_read
= eeprom_93xx46_read
;
519 edev
->nvmem_config
.reg_write
= eeprom_93xx46_write
;
520 edev
->nvmem_config
.priv
= edev
;
521 edev
->nvmem_config
.stride
= 4;
522 edev
->nvmem_config
.word_size
= 1;
523 edev
->nvmem_config
.size
= edev
->size
;
525 edev
->nvmem
= devm_nvmem_register(&spi
->dev
, &edev
->nvmem_config
);
526 if (IS_ERR(edev
->nvmem
))
527 return PTR_ERR(edev
->nvmem
);
529 dev_info(&spi
->dev
, "%d-bit eeprom containing %d bytes %s\n",
530 (pd
->flags
& EE_ADDR8
) ? 8 : 16,
532 (pd
->flags
& EE_READONLY
) ? "(readonly)" : "");
534 if (!(pd
->flags
& EE_READONLY
)) {
535 if (device_create_file(&spi
->dev
, &dev_attr_erase
))
536 dev_err(&spi
->dev
, "can't create erase interface\n");
539 spi_set_drvdata(spi
, edev
);
543 static void eeprom_93xx46_remove(struct spi_device
*spi
)
545 struct eeprom_93xx46_dev
*edev
= spi_get_drvdata(spi
);
547 if (!(edev
->pdata
->flags
& EE_READONLY
))
548 device_remove_file(&spi
->dev
, &dev_attr_erase
);
551 static struct spi_driver eeprom_93xx46_driver
= {
554 .of_match_table
= eeprom_93xx46_of_table
,
556 .probe
= eeprom_93xx46_probe
,
557 .remove
= eeprom_93xx46_remove
,
558 .id_table
= eeprom_93xx46_spi_ids
,
561 module_spi_driver(eeprom_93xx46_driver
);
563 MODULE_LICENSE("GPL");
564 MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
565 MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
566 MODULE_ALIAS("spi:93xx46");