12874c5fdSThomas Gleixner // SPDX-License-Identifier: GPL-2.0-or-later
2e51d565fSWolfram Sang /*
31ca54ce9SAndy Shevchenko * Driver for most of the SPI EEPROMs, such as Atmel AT25 models
41ca54ce9SAndy Shevchenko * and Cypress FRAMs FM25 models.
5e51d565fSWolfram Sang *
6e51d565fSWolfram Sang * Copyright (C) 2006 David Brownell
7e51d565fSWolfram Sang */
8e51d565fSWolfram Sang
9d059ed1bSAndy Shevchenko #include <linux/bits.h>
10e51d565fSWolfram Sang #include <linux/delay.h>
11e51d565fSWolfram Sang #include <linux/device.h>
12d5fb1304SAndy Shevchenko #include <linux/kernel.h>
13d5fb1304SAndy Shevchenko #include <linux/module.h>
14d5fb1304SAndy Shevchenko #include <linux/property.h>
15e51d565fSWolfram Sang #include <linux/sched.h>
16d5fb1304SAndy Shevchenko #include <linux/slab.h>
17d5fb1304SAndy Shevchenko
18d5fb1304SAndy Shevchenko #include <linux/spi/eeprom.h>
19d5fb1304SAndy Shevchenko #include <linux/spi/spi.h>
20e51d565fSWolfram Sang
215a99f570SAndrew Lunn #include <linux/nvmem-provider.h>
22e51d565fSWolfram Sang
23e51d565fSWolfram Sang /*
24e51d565fSWolfram Sang * NOTE: this is an *EEPROM* driver. The vagaries of product naming
25e51d565fSWolfram Sang * mean that some AT25 products are EEPROMs, and others are FLASH.
26e51d565fSWolfram Sang * Handle FLASH chips with the drivers/mtd/devices/m25p80.c driver,
27e51d565fSWolfram Sang * not this one!
28667aef00SJonathan Neuschäfer *
29667aef00SJonathan Neuschäfer * EEPROMs that can be used with this driver include, for example:
30667aef00SJonathan Neuschäfer * AT25M02, AT25128B
31e51d565fSWolfram Sang */
32e51d565fSWolfram Sang
33fd307a4aSJiri Prchal #define FM25_SN_LEN 8 /* serial number length */
345b47b751SChristophe Leroy #define EE_MAXADDRLEN 3 /* 24 bit addresses, up to 2 MBytes */
355b47b751SChristophe Leroy
36e51d565fSWolfram Sang struct at25_data {
3731a45d27SAndy Shevchenko struct spi_eeprom chip;
38e51d565fSWolfram Sang struct spi_device *spi;
39e51d565fSWolfram Sang struct mutex lock;
40e51d565fSWolfram Sang unsigned addrlen;
415a99f570SAndrew Lunn struct nvmem_config nvmem_config;
425a99f570SAndrew Lunn struct nvmem_device *nvmem;
43fd307a4aSJiri Prchal u8 sernum[FM25_SN_LEN];
445b47b751SChristophe Leroy u8 command[EE_MAXADDRLEN + 1];
45e51d565fSWolfram Sang };
46e51d565fSWolfram Sang
47e51d565fSWolfram Sang #define AT25_WREN 0x06 /* latch the write enable */
48e51d565fSWolfram Sang #define AT25_WRDI 0x04 /* reset the write enable */
49e51d565fSWolfram Sang #define AT25_RDSR 0x05 /* read status register */
50e51d565fSWolfram Sang #define AT25_WRSR 0x01 /* write status register */
51e51d565fSWolfram Sang #define AT25_READ 0x03 /* read byte(s) */
52e51d565fSWolfram Sang #define AT25_WRITE 0x02 /* write byte(s)/sector */
53fd307a4aSJiri Prchal #define FM25_SLEEP 0xb9 /* enter sleep mode */
54fd307a4aSJiri Prchal #define FM25_RDID 0x9f /* read device ID */
55fd307a4aSJiri Prchal #define FM25_RDSN 0xc3 /* read S/N */
56e51d565fSWolfram Sang
57e51d565fSWolfram Sang #define AT25_SR_nRDY 0x01 /* nRDY = write-in-progress */
58e51d565fSWolfram Sang #define AT25_SR_WEN 0x02 /* write enable (latched) */
59e51d565fSWolfram Sang #define AT25_SR_BP0 0x04 /* BP for software writeprotect */
60e51d565fSWolfram Sang #define AT25_SR_BP1 0x08
61e51d565fSWolfram Sang #define AT25_SR_WPEN 0x80 /* writeprotect enable */
62e51d565fSWolfram Sang
631ca54ce9SAndy Shevchenko #define AT25_INSTR_BIT3 0x08 /* additional address bit in instr */
64e51d565fSWolfram Sang
65fd307a4aSJiri Prchal #define FM25_ID_LEN 9 /* ID length */
66fd307a4aSJiri Prchal
671ca54ce9SAndy Shevchenko /*
681ca54ce9SAndy Shevchenko * Specs often allow 5ms for a page write, sometimes 20ms;
69e51d565fSWolfram Sang * it's important to recover from write timeouts.
70e51d565fSWolfram Sang */
71e51d565fSWolfram Sang #define EE_TIMEOUT 25
72e51d565fSWolfram Sang
73e51d565fSWolfram Sang /*-------------------------------------------------------------------------*/
74e51d565fSWolfram Sang
75e51d565fSWolfram Sang #define io_limit PAGE_SIZE /* bytes */
76e51d565fSWolfram Sang
at25_ee_read(void * priv,unsigned int offset,void * val,size_t count)7701973a01SSrinivas Kandagatla static int at25_ee_read(void *priv, unsigned int offset,
7801973a01SSrinivas Kandagatla void *val, size_t count)
79e51d565fSWolfram Sang {
8001973a01SSrinivas Kandagatla struct at25_data *at25 = priv;
8101973a01SSrinivas Kandagatla char *buf = val;
820a35780cSBrad Bishop size_t max_chunk = spi_max_transfer_size(at25->spi);
8319e506b3SGeert Uytterhoeven unsigned int msg_offset = offset;
8419e506b3SGeert Uytterhoeven size_t bytes_left = count;
8519e506b3SGeert Uytterhoeven size_t segment;
86e51d565fSWolfram Sang u8 *cp;
87e51d565fSWolfram Sang ssize_t status;
88e51d565fSWolfram Sang struct spi_transfer t[2];
89e51d565fSWolfram Sang struct spi_message m;
90b4161f0bSIvo Sieben u8 instr;
91e51d565fSWolfram Sang
925a99f570SAndrew Lunn if (unlikely(offset >= at25->chip.byte_len))
9301973a01SSrinivas Kandagatla return -EINVAL;
945a99f570SAndrew Lunn if ((offset + count) > at25->chip.byte_len)
955a99f570SAndrew Lunn count = at25->chip.byte_len - offset;
9614dd1ff0SDavid Brownell if (unlikely(!count))
9701973a01SSrinivas Kandagatla return -EINVAL;
9814dd1ff0SDavid Brownell
9919e506b3SGeert Uytterhoeven do {
10019e506b3SGeert Uytterhoeven segment = min(bytes_left, max_chunk);
1015b47b751SChristophe Leroy cp = at25->command;
102b4161f0bSIvo Sieben
103b4161f0bSIvo Sieben instr = AT25_READ;
104b4161f0bSIvo Sieben if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
1050a35780cSBrad Bishop if (msg_offset >= BIT(at25->addrlen * 8))
106b4161f0bSIvo Sieben instr |= AT25_INSTR_BIT3;
1075b47b751SChristophe Leroy
1085b47b751SChristophe Leroy mutex_lock(&at25->lock);
1095b47b751SChristophe Leroy
110b4161f0bSIvo Sieben *cp++ = instr;
111e51d565fSWolfram Sang
112e51d565fSWolfram Sang /* 8/16/24-bit address is written MSB first */
113e51d565fSWolfram Sang switch (at25->addrlen) {
114e51d565fSWolfram Sang default: /* case 3 */
1150a35780cSBrad Bishop *cp++ = msg_offset >> 16;
116df561f66SGustavo A. R. Silva fallthrough;
117e51d565fSWolfram Sang case 2:
1180a35780cSBrad Bishop *cp++ = msg_offset >> 8;
119df561f66SGustavo A. R. Silva fallthrough;
120e51d565fSWolfram Sang case 1:
1211ca54ce9SAndy Shevchenko case 0: /* can't happen: for better code generation */
1220a35780cSBrad Bishop *cp++ = msg_offset >> 0;
123e51d565fSWolfram Sang }
124e51d565fSWolfram Sang
125e51d565fSWolfram Sang spi_message_init(&m);
126c84f259cSDevang Panchal memset(t, 0, sizeof(t));
127e51d565fSWolfram Sang
1285b47b751SChristophe Leroy t[0].tx_buf = at25->command;
129e51d565fSWolfram Sang t[0].len = at25->addrlen + 1;
130e51d565fSWolfram Sang spi_message_add_tail(&t[0], &m);
131e51d565fSWolfram Sang
13219e506b3SGeert Uytterhoeven t[1].rx_buf = buf;
13319e506b3SGeert Uytterhoeven t[1].len = segment;
134e51d565fSWolfram Sang spi_message_add_tail(&t[1], &m);
135e51d565fSWolfram Sang
136e51d565fSWolfram Sang status = spi_sync(at25->spi, &m);
137e51d565fSWolfram Sang
138e51d565fSWolfram Sang mutex_unlock(&at25->lock);
1390a35780cSBrad Bishop
1400a35780cSBrad Bishop if (status)
14101973a01SSrinivas Kandagatla return status;
1420a35780cSBrad Bishop
14319e506b3SGeert Uytterhoeven msg_offset += segment;
14419e506b3SGeert Uytterhoeven buf += segment;
14519e506b3SGeert Uytterhoeven bytes_left -= segment;
14619e506b3SGeert Uytterhoeven } while (bytes_left > 0);
1470a35780cSBrad Bishop
1480a35780cSBrad Bishop dev_dbg(&at25->spi->dev, "read %zu bytes at %d\n",
1490a35780cSBrad Bishop count, offset);
1500a35780cSBrad Bishop return 0;
151e51d565fSWolfram Sang }
152e51d565fSWolfram Sang
1531ca54ce9SAndy Shevchenko /* Read extra registers as ID or serial number */
fm25_aux_read(struct at25_data * at25,u8 * buf,uint8_t command,int len)154fd307a4aSJiri Prchal static int fm25_aux_read(struct at25_data *at25, u8 *buf, uint8_t command,
155fd307a4aSJiri Prchal int len)
156fd307a4aSJiri Prchal {
157fd307a4aSJiri Prchal int status;
158fd307a4aSJiri Prchal struct spi_transfer t[2];
159fd307a4aSJiri Prchal struct spi_message m;
160fd307a4aSJiri Prchal
161fd307a4aSJiri Prchal spi_message_init(&m);
162fd307a4aSJiri Prchal memset(t, 0, sizeof(t));
163fd307a4aSJiri Prchal
1645b47b751SChristophe Leroy t[0].tx_buf = at25->command;
165fd307a4aSJiri Prchal t[0].len = 1;
166fd307a4aSJiri Prchal spi_message_add_tail(&t[0], &m);
167fd307a4aSJiri Prchal
168fd307a4aSJiri Prchal t[1].rx_buf = buf;
169fd307a4aSJiri Prchal t[1].len = len;
170fd307a4aSJiri Prchal spi_message_add_tail(&t[1], &m);
171fd307a4aSJiri Prchal
172fd307a4aSJiri Prchal mutex_lock(&at25->lock);
173fd307a4aSJiri Prchal
1745b47b751SChristophe Leroy at25->command[0] = command;
1755b47b751SChristophe Leroy
176fd307a4aSJiri Prchal status = spi_sync(at25->spi, &m);
177fd307a4aSJiri Prchal dev_dbg(&at25->spi->dev, "read %d aux bytes --> %d\n", len, status);
178fd307a4aSJiri Prchal
179fd307a4aSJiri Prchal mutex_unlock(&at25->lock);
180fd307a4aSJiri Prchal return status;
181fd307a4aSJiri Prchal }
182fd307a4aSJiri Prchal
sernum_show(struct device * dev,struct device_attribute * attr,char * buf)183fd307a4aSJiri Prchal static ssize_t sernum_show(struct device *dev, struct device_attribute *attr, char *buf)
184fd307a4aSJiri Prchal {
185fd307a4aSJiri Prchal struct at25_data *at25;
186fd307a4aSJiri Prchal
187fd307a4aSJiri Prchal at25 = dev_get_drvdata(dev);
188604288bcSJiri Prchal return sysfs_emit(buf, "%*ph\n", (int)sizeof(at25->sernum), at25->sernum);
189fd307a4aSJiri Prchal }
190fd307a4aSJiri Prchal static DEVICE_ATTR_RO(sernum);
191fd307a4aSJiri Prchal
192fd307a4aSJiri Prchal static struct attribute *sernum_attrs[] = {
193fd307a4aSJiri Prchal &dev_attr_sernum.attr,
194fd307a4aSJiri Prchal NULL,
195fd307a4aSJiri Prchal };
196fd307a4aSJiri Prchal ATTRIBUTE_GROUPS(sernum);
197fd307a4aSJiri Prchal
at25_ee_write(void * priv,unsigned int off,void * val,size_t count)19801973a01SSrinivas Kandagatla static int at25_ee_write(void *priv, unsigned int off, void *val, size_t count)
199e51d565fSWolfram Sang {
20001973a01SSrinivas Kandagatla struct at25_data *at25 = priv;
2010a35780cSBrad Bishop size_t maxsz = spi_max_transfer_size(at25->spi);
20201973a01SSrinivas Kandagatla const char *buf = val;
20301973a01SSrinivas Kandagatla int status = 0;
204e51d565fSWolfram Sang unsigned buf_size;
205e51d565fSWolfram Sang u8 *bounce;
206e51d565fSWolfram Sang
2075a99f570SAndrew Lunn if (unlikely(off >= at25->chip.byte_len))
20814dd1ff0SDavid Brownell return -EFBIG;
2095a99f570SAndrew Lunn if ((off + count) > at25->chip.byte_len)
2105a99f570SAndrew Lunn count = at25->chip.byte_len - off;
21114dd1ff0SDavid Brownell if (unlikely(!count))
21201973a01SSrinivas Kandagatla return -EINVAL;
21314dd1ff0SDavid Brownell
214e51d565fSWolfram Sang /* Temp buffer starts with command and address */
215e51d565fSWolfram Sang buf_size = at25->chip.page_size;
216e51d565fSWolfram Sang if (buf_size > io_limit)
217e51d565fSWolfram Sang buf_size = io_limit;
218e51d565fSWolfram Sang bounce = kmalloc(buf_size + at25->addrlen + 1, GFP_KERNEL);
219e51d565fSWolfram Sang if (!bounce)
220e51d565fSWolfram Sang return -ENOMEM;
221e51d565fSWolfram Sang
2221ca54ce9SAndy Shevchenko /*
2231ca54ce9SAndy Shevchenko * For write, rollover is within the page ... so we write at
224e51d565fSWolfram Sang * most one page, then manually roll over to the next page.
225e51d565fSWolfram Sang */
226e51d565fSWolfram Sang mutex_lock(&at25->lock);
227e51d565fSWolfram Sang do {
228e51d565fSWolfram Sang unsigned long timeout, retries;
229e51d565fSWolfram Sang unsigned segment;
23019e506b3SGeert Uytterhoeven unsigned offset = off;
231b4161f0bSIvo Sieben u8 *cp = bounce;
232f0d83679SSebastian Heutling int sr;
233b4161f0bSIvo Sieben u8 instr;
234e51d565fSWolfram Sang
235e51d565fSWolfram Sang *cp = AT25_WREN;
236e51d565fSWolfram Sang status = spi_write(at25->spi, cp, 1);
237e51d565fSWolfram Sang if (status < 0) {
2383936e4c8SAndy Shevchenko dev_dbg(&at25->spi->dev, "WREN --> %d\n", status);
239e51d565fSWolfram Sang break;
240e51d565fSWolfram Sang }
241e51d565fSWolfram Sang
242b4161f0bSIvo Sieben instr = AT25_WRITE;
243b4161f0bSIvo Sieben if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
244d059ed1bSAndy Shevchenko if (offset >= BIT(at25->addrlen * 8))
245b4161f0bSIvo Sieben instr |= AT25_INSTR_BIT3;
246b4161f0bSIvo Sieben *cp++ = instr;
247b4161f0bSIvo Sieben
248e51d565fSWolfram Sang /* 8/16/24-bit address is written MSB first */
249e51d565fSWolfram Sang switch (at25->addrlen) {
250e51d565fSWolfram Sang default: /* case 3 */
251e51d565fSWolfram Sang *cp++ = offset >> 16;
252df561f66SGustavo A. R. Silva fallthrough;
253e51d565fSWolfram Sang case 2:
254e51d565fSWolfram Sang *cp++ = offset >> 8;
255df561f66SGustavo A. R. Silva fallthrough;
256e51d565fSWolfram Sang case 1:
2571ca54ce9SAndy Shevchenko case 0: /* can't happen: for better code generation */
258e51d565fSWolfram Sang *cp++ = offset >> 0;
259e51d565fSWolfram Sang }
260e51d565fSWolfram Sang
261e51d565fSWolfram Sang /* Write as much of a page as we can */
262e51d565fSWolfram Sang segment = buf_size - (offset % buf_size);
263e51d565fSWolfram Sang if (segment > count)
264e51d565fSWolfram Sang segment = count;
2650a35780cSBrad Bishop if (segment > maxsz)
2660a35780cSBrad Bishop segment = maxsz;
267e51d565fSWolfram Sang memcpy(cp, buf, segment);
268e51d565fSWolfram Sang status = spi_write(at25->spi, bounce,
269e51d565fSWolfram Sang segment + at25->addrlen + 1);
2703936e4c8SAndy Shevchenko dev_dbg(&at25->spi->dev, "write %u bytes at %u --> %d\n",
2713936e4c8SAndy Shevchenko segment, offset, status);
272e51d565fSWolfram Sang if (status < 0)
273e51d565fSWolfram Sang break;
274e51d565fSWolfram Sang
2751ca54ce9SAndy Shevchenko /*
2761ca54ce9SAndy Shevchenko * REVISIT this should detect (or prevent) failed writes
2771ca54ce9SAndy Shevchenko * to read-only sections of the EEPROM...
278e51d565fSWolfram Sang */
279e51d565fSWolfram Sang
280e51d565fSWolfram Sang /* Wait for non-busy status */
281e51d565fSWolfram Sang timeout = jiffies + msecs_to_jiffies(EE_TIMEOUT);
282e51d565fSWolfram Sang retries = 0;
283e51d565fSWolfram Sang do {
284e51d565fSWolfram Sang
285e51d565fSWolfram Sang sr = spi_w8r8(at25->spi, AT25_RDSR);
286e51d565fSWolfram Sang if (sr < 0 || (sr & AT25_SR_nRDY)) {
287e51d565fSWolfram Sang dev_dbg(&at25->spi->dev,
288e51d565fSWolfram Sang "rdsr --> %d (%02x)\n", sr, sr);
289e51d565fSWolfram Sang /* at HZ=100, this is sloooow */
290e51d565fSWolfram Sang msleep(1);
291e51d565fSWolfram Sang continue;
292e51d565fSWolfram Sang }
293e51d565fSWolfram Sang if (!(sr & AT25_SR_nRDY))
294e51d565fSWolfram Sang break;
295e51d565fSWolfram Sang } while (retries++ < 3 || time_before_eq(jiffies, timeout));
296e51d565fSWolfram Sang
297f0d83679SSebastian Heutling if ((sr < 0) || (sr & AT25_SR_nRDY)) {
298e51d565fSWolfram Sang dev_err(&at25->spi->dev,
2993936e4c8SAndy Shevchenko "write %u bytes offset %u, timeout after %u msecs\n",
300e51d565fSWolfram Sang segment, offset,
301e51d565fSWolfram Sang jiffies_to_msecs(jiffies -
302e51d565fSWolfram Sang (timeout - EE_TIMEOUT)));
303e51d565fSWolfram Sang status = -ETIMEDOUT;
304e51d565fSWolfram Sang break;
305e51d565fSWolfram Sang }
306e51d565fSWolfram Sang
307e51d565fSWolfram Sang off += segment;
308e51d565fSWolfram Sang buf += segment;
309e51d565fSWolfram Sang count -= segment;
310e51d565fSWolfram Sang
311e51d565fSWolfram Sang } while (count > 0);
312e51d565fSWolfram Sang
313e51d565fSWolfram Sang mutex_unlock(&at25->lock);
314e51d565fSWolfram Sang
315e51d565fSWolfram Sang kfree(bounce);
31601973a01SSrinivas Kandagatla return status;
317e51d565fSWolfram Sang }
318e51d565fSWolfram Sang
319e51d565fSWolfram Sang /*-------------------------------------------------------------------------*/
320e51d565fSWolfram Sang
at25_fw_to_chip(struct device * dev,struct spi_eeprom * chip)321f60e7074SMika Westerberg static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
322d6ae0d57SDavid Daney {
323d6ae0d57SDavid Daney u32 val;
324c329fe53SAndy Shevchenko int err;
325d6ae0d57SDavid Daney
326710f8af1SKees Cook strscpy(chip->name, "at25", sizeof(chip->name));
327d6ae0d57SDavid Daney
328c329fe53SAndy Shevchenko err = device_property_read_u32(dev, "size", &val);
329c329fe53SAndy Shevchenko if (err)
330c329fe53SAndy Shevchenko err = device_property_read_u32(dev, "at25,byte-len", &val);
331c329fe53SAndy Shevchenko if (err) {
332d6ae0d57SDavid Daney dev_err(dev, "Error: missing \"size\" property\n");
333c329fe53SAndy Shevchenko return err;
334d6ae0d57SDavid Daney }
335c329fe53SAndy Shevchenko chip->byte_len = val;
336d6ae0d57SDavid Daney
337c329fe53SAndy Shevchenko err = device_property_read_u32(dev, "pagesize", &val);
338c329fe53SAndy Shevchenko if (err)
339c329fe53SAndy Shevchenko err = device_property_read_u32(dev, "at25,page-size", &val);
340c329fe53SAndy Shevchenko if (err) {
341d6ae0d57SDavid Daney dev_err(dev, "Error: missing \"pagesize\" property\n");
342c329fe53SAndy Shevchenko return err;
343d6ae0d57SDavid Daney }
344c329fe53SAndy Shevchenko chip->page_size = val;
345d6ae0d57SDavid Daney
346c329fe53SAndy Shevchenko err = device_property_read_u32(dev, "address-width", &val);
347c329fe53SAndy Shevchenko if (err) {
348fb422f44SAndy Shevchenko err = device_property_read_u32(dev, "at25,addr-mode", &val);
349fb422f44SAndy Shevchenko if (err) {
350c329fe53SAndy Shevchenko dev_err(dev, "Error: missing \"address-width\" property\n");
351c329fe53SAndy Shevchenko return err;
352d6ae0d57SDavid Daney }
353fb422f44SAndy Shevchenko chip->flags = (u16)val;
354fb422f44SAndy Shevchenko } else {
355d6ae0d57SDavid Daney switch (val) {
356f8d3bc10SGeert Uytterhoeven case 9:
357f8d3bc10SGeert Uytterhoeven chip->flags |= EE_INSTR_BIT3_IS_ADDR;
358df561f66SGustavo A. R. Silva fallthrough;
359d6ae0d57SDavid Daney case 8:
360d6ae0d57SDavid Daney chip->flags |= EE_ADDR1;
361d6ae0d57SDavid Daney break;
362d6ae0d57SDavid Daney case 16:
363d6ae0d57SDavid Daney chip->flags |= EE_ADDR2;
364d6ae0d57SDavid Daney break;
365d6ae0d57SDavid Daney case 24:
366d6ae0d57SDavid Daney chip->flags |= EE_ADDR3;
367d6ae0d57SDavid Daney break;
368d6ae0d57SDavid Daney default:
369d6ae0d57SDavid Daney dev_err(dev,
370d6ae0d57SDavid Daney "Error: bad \"address-width\" property: %u\n",
371d6ae0d57SDavid Daney val);
372d6ae0d57SDavid Daney return -ENODEV;
373d6ae0d57SDavid Daney }
374f60e7074SMika Westerberg if (device_property_present(dev, "read-only"))
375d6ae0d57SDavid Daney chip->flags |= EE_READONLY;
376d6ae0d57SDavid Daney }
377d6ae0d57SDavid Daney return 0;
378d6ae0d57SDavid Daney }
379d6ae0d57SDavid Daney
at25_fram_to_chip(struct device * dev,struct spi_eeprom * chip)38031a45d27SAndy Shevchenko static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
38131a45d27SAndy Shevchenko {
38231a45d27SAndy Shevchenko struct at25_data *at25 = container_of(chip, struct at25_data, chip);
38331a45d27SAndy Shevchenko u8 sernum[FM25_SN_LEN];
38431a45d27SAndy Shevchenko u8 id[FM25_ID_LEN];
38531a45d27SAndy Shevchenko int i;
38631a45d27SAndy Shevchenko
387710f8af1SKees Cook strscpy(chip->name, "fm25", sizeof(chip->name));
38831a45d27SAndy Shevchenko
38931a45d27SAndy Shevchenko /* Get ID of chip */
39031a45d27SAndy Shevchenko fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);
39131a45d27SAndy Shevchenko if (id[6] != 0xc2) {
39231a45d27SAndy Shevchenko dev_err(dev, "Error: no Cypress FRAM (id %02x)\n", id[6]);
39331a45d27SAndy Shevchenko return -ENODEV;
39431a45d27SAndy Shevchenko }
39531a45d27SAndy Shevchenko /* Set size found in ID */
39631a45d27SAndy Shevchenko if (id[7] < 0x21 || id[7] > 0x26) {
39731a45d27SAndy Shevchenko dev_err(dev, "Error: unsupported size (id %02x)\n", id[7]);
39831a45d27SAndy Shevchenko return -ENODEV;
39931a45d27SAndy Shevchenko }
40031a45d27SAndy Shevchenko
40131a45d27SAndy Shevchenko chip->byte_len = BIT(id[7] - 0x21 + 4) * 1024;
40231a45d27SAndy Shevchenko if (chip->byte_len > 64 * 1024)
40331a45d27SAndy Shevchenko chip->flags |= EE_ADDR3;
40431a45d27SAndy Shevchenko else
40531a45d27SAndy Shevchenko chip->flags |= EE_ADDR2;
40631a45d27SAndy Shevchenko
40731a45d27SAndy Shevchenko if (id[8]) {
40831a45d27SAndy Shevchenko fm25_aux_read(at25, sernum, FM25_RDSN, FM25_SN_LEN);
40931a45d27SAndy Shevchenko /* Swap byte order */
41031a45d27SAndy Shevchenko for (i = 0; i < FM25_SN_LEN; i++)
41131a45d27SAndy Shevchenko at25->sernum[i] = sernum[FM25_SN_LEN - 1 - i];
41231a45d27SAndy Shevchenko }
41331a45d27SAndy Shevchenko
41431a45d27SAndy Shevchenko chip->page_size = PAGE_SIZE;
41531a45d27SAndy Shevchenko return 0;
41631a45d27SAndy Shevchenko }
41731a45d27SAndy Shevchenko
418fd307a4aSJiri Prchal static const struct of_device_id at25_of_match[] = {
419d6471ab9SAndy Shevchenko { .compatible = "atmel,at25" },
420d6471ab9SAndy Shevchenko { .compatible = "cypress,fm25" },
421fd307a4aSJiri Prchal { }
422fd307a4aSJiri Prchal };
423fd307a4aSJiri Prchal MODULE_DEVICE_TABLE(of, at25_of_match);
424fd307a4aSJiri Prchal
4259e2cd444SMark Brown static const struct spi_device_id at25_spi_ids[] = {
426d6471ab9SAndy Shevchenko { .name = "at25" },
427d6471ab9SAndy Shevchenko { .name = "fm25" },
4289e2cd444SMark Brown { }
4299e2cd444SMark Brown };
4309e2cd444SMark Brown MODULE_DEVICE_TABLE(spi, at25_spi_ids);
4319e2cd444SMark Brown
at25_probe(struct spi_device * spi)432e51d565fSWolfram Sang static int at25_probe(struct spi_device *spi)
433e51d565fSWolfram Sang {
434e51d565fSWolfram Sang struct at25_data *at25 = NULL;
435e51d565fSWolfram Sang int err;
436e51d565fSWolfram Sang int sr;
43701d3c42aSAndy Shevchenko struct spi_eeprom *pdata;
4385b557298SAndy Shevchenko bool is_fram;
439fd307a4aSJiri Prchal
4401ca54ce9SAndy Shevchenko /*
4411ca54ce9SAndy Shevchenko * Ping the chip ... the status register is pretty portable,
442e51d565fSWolfram Sang * unlike probing manufacturer IDs. We do expect that system
443e51d565fSWolfram Sang * firmware didn't write it in the past few milliseconds!
444e51d565fSWolfram Sang */
445e51d565fSWolfram Sang sr = spi_w8r8(spi, AT25_RDSR);
446e51d565fSWolfram Sang if (sr < 0 || sr & AT25_SR_nRDY) {
447e51d565fSWolfram Sang dev_dbg(&spi->dev, "rdsr --> %d (%02x)\n", sr, sr);
44801fe7b43SNikolay Balandin return -ENXIO;
449e51d565fSWolfram Sang }
450e51d565fSWolfram Sang
451a6501e4bSKees Cook at25 = devm_kzalloc(&spi->dev, sizeof(*at25), GFP_KERNEL);
452a6501e4bSKees Cook if (!at25)
453a6501e4bSKees Cook return -ENOMEM;
454a6501e4bSKees Cook
455e51d565fSWolfram Sang mutex_init(&at25->lock);
45696b2a45cSMark Brown at25->spi = spi;
45741ddcf67SJingoo Han spi_set_drvdata(spi, at25);
458e51d565fSWolfram Sang
459*0eb468b6SAndy Shevchenko is_fram = fwnode_device_is_compatible(dev_fwnode(&spi->dev), "cypress,fm25");
460*0eb468b6SAndy Shevchenko
46101d3c42aSAndy Shevchenko /* Chip description */
46201d3c42aSAndy Shevchenko pdata = dev_get_platdata(&spi->dev);
46301d3c42aSAndy Shevchenko if (pdata) {
46401d3c42aSAndy Shevchenko at25->chip = *pdata;
46501d3c42aSAndy Shevchenko } else {
46631a45d27SAndy Shevchenko if (is_fram)
46731a45d27SAndy Shevchenko err = at25_fram_to_chip(&spi->dev, &at25->chip);
46831a45d27SAndy Shevchenko else
46901d3c42aSAndy Shevchenko err = at25_fw_to_chip(&spi->dev, &at25->chip);
47001d3c42aSAndy Shevchenko if (err)
47101d3c42aSAndy Shevchenko return err;
47201d3c42aSAndy Shevchenko }
473fd307a4aSJiri Prchal
474fd307a4aSJiri Prchal /* For now we only support 8/16/24 bit addressing */
475fd307a4aSJiri Prchal if (at25->chip.flags & EE_ADDR1)
476fd307a4aSJiri Prchal at25->addrlen = 1;
477fd307a4aSJiri Prchal else if (at25->chip.flags & EE_ADDR2)
478fd307a4aSJiri Prchal at25->addrlen = 2;
479fd307a4aSJiri Prchal else if (at25->chip.flags & EE_ADDR3)
480fd307a4aSJiri Prchal at25->addrlen = 3;
481fd307a4aSJiri Prchal else {
482fd307a4aSJiri Prchal dev_dbg(&spi->dev, "unsupported address type\n");
483fd307a4aSJiri Prchal return -EINVAL;
484fd307a4aSJiri Prchal }
485fd307a4aSJiri Prchal
486fd307a4aSJiri Prchal at25->nvmem_config.type = is_fram ? NVMEM_TYPE_FRAM : NVMEM_TYPE_EEPROM;
4875a99f570SAndrew Lunn at25->nvmem_config.name = dev_name(&spi->dev);
4885a99f570SAndrew Lunn at25->nvmem_config.dev = &spi->dev;
48951902c12SAndy Shevchenko at25->nvmem_config.read_only = at25->chip.flags & EE_READONLY;
4905a99f570SAndrew Lunn at25->nvmem_config.root_only = true;
4915a99f570SAndrew Lunn at25->nvmem_config.owner = THIS_MODULE;
4925a99f570SAndrew Lunn at25->nvmem_config.compat = true;
4935a99f570SAndrew Lunn at25->nvmem_config.base_dev = &spi->dev;
49401973a01SSrinivas Kandagatla at25->nvmem_config.reg_read = at25_ee_read;
49501973a01SSrinivas Kandagatla at25->nvmem_config.reg_write = at25_ee_write;
49601973a01SSrinivas Kandagatla at25->nvmem_config.priv = at25;
497284f52acSChristian Eggers at25->nvmem_config.stride = 1;
49801973a01SSrinivas Kandagatla at25->nvmem_config.word_size = 1;
49951902c12SAndy Shevchenko at25->nvmem_config.size = at25->chip.byte_len;
500e51d565fSWolfram Sang
50196d08fb4SBartosz Golaszewski at25->nvmem = devm_nvmem_register(&spi->dev, &at25->nvmem_config);
5025a99f570SAndrew Lunn if (IS_ERR(at25->nvmem))
5035a99f570SAndrew Lunn return PTR_ERR(at25->nvmem);
5045a99f570SAndrew Lunn
505fd307a4aSJiri Prchal dev_info(&spi->dev, "%d %s %s %s%s, pagesize %u\n",
5069a626577SRalph Siemsen (at25->chip.byte_len < 1024) ?
5079a626577SRalph Siemsen at25->chip.byte_len : (at25->chip.byte_len / 1024),
50851902c12SAndy Shevchenko (at25->chip.byte_len < 1024) ? "Byte" : "KByte",
509fd307a4aSJiri Prchal at25->chip.name, is_fram ? "fram" : "eeprom",
51051902c12SAndy Shevchenko (at25->chip.flags & EE_READONLY) ? " (readonly)" : "",
511e51d565fSWolfram Sang at25->chip.page_size);
512e51d565fSWolfram Sang return 0;
513e51d565fSWolfram Sang }
514e51d565fSWolfram Sang
515e51d565fSWolfram Sang /*-------------------------------------------------------------------------*/
516e51d565fSWolfram Sang
517e51d565fSWolfram Sang static struct spi_driver at25_driver = {
518e51d565fSWolfram Sang .driver = {
519e51d565fSWolfram Sang .name = "at25",
520fbfdb6edSJan Luebbe .of_match_table = at25_of_match,
521fd307a4aSJiri Prchal .dev_groups = sernum_groups,
522e51d565fSWolfram Sang },
523e51d565fSWolfram Sang .probe = at25_probe,
5249e2cd444SMark Brown .id_table = at25_spi_ids,
525e51d565fSWolfram Sang };
526e51d565fSWolfram Sang
527a3dc3c9eSAxel Lin module_spi_driver(at25_driver);
528e51d565fSWolfram Sang
529e51d565fSWolfram Sang MODULE_DESCRIPTION("Driver for most SPI EEPROMs");
530e51d565fSWolfram Sang MODULE_AUTHOR("David Brownell");
531e51d565fSWolfram Sang MODULE_LICENSE("GPL");
532e0626e38SAnton Vorontsov MODULE_ALIAS("spi:at25");
533