at25.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536
  1. // SPDX-License-Identifier: GPL-2.0-or-later
  2. /*
  3. * Driver for most of the SPI EEPROMs, such as Atmel AT25 models
  4. * and Cypress FRAMs FM25 models.
  5. *
  6. * Copyright (C) 2006 David Brownell
  7. */
  8. #include <linux/bits.h>
  9. #include <linux/delay.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/property.h>
  14. #include <linux/sched.h>
  15. #include <linux/slab.h>
  16. #include <linux/spi/eeprom.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/nvmem-provider.h>
  19. /*
  20. * NOTE: this is an *EEPROM* driver. The vagaries of product naming
  21. * mean that some AT25 products are EEPROMs, and others are FLASH.
  22. * Handle FLASH chips with the drivers/mtd/devices/m25p80.c driver,
  23. * not this one!
  24. *
  25. * EEPROMs that can be used with this driver include, for example:
  26. * AT25M02, AT25128B
  27. */
  28. #define FM25_SN_LEN 8 /* serial number length */
  29. #define EE_MAXADDRLEN 3 /* 24 bit addresses, up to 2 MBytes */
  30. struct at25_data {
  31. struct spi_eeprom chip;
  32. struct spi_device *spi;
  33. struct mutex lock;
  34. unsigned addrlen;
  35. struct nvmem_config nvmem_config;
  36. struct nvmem_device *nvmem;
  37. u8 sernum[FM25_SN_LEN];
  38. u8 command[EE_MAXADDRLEN + 1];
  39. };
  40. #define AT25_WREN 0x06 /* latch the write enable */
  41. #define AT25_WRDI 0x04 /* reset the write enable */
  42. #define AT25_RDSR 0x05 /* read status register */
  43. #define AT25_WRSR 0x01 /* write status register */
  44. #define AT25_READ 0x03 /* read byte(s) */
  45. #define AT25_WRITE 0x02 /* write byte(s)/sector */
  46. #define FM25_SLEEP 0xb9 /* enter sleep mode */
  47. #define FM25_RDID 0x9f /* read device ID */
  48. #define FM25_RDSN 0xc3 /* read S/N */
  49. #define AT25_SR_nRDY 0x01 /* nRDY = write-in-progress */
  50. #define AT25_SR_WEN 0x02 /* write enable (latched) */
  51. #define AT25_SR_BP0 0x04 /* BP for software writeprotect */
  52. #define AT25_SR_BP1 0x08
  53. #define AT25_SR_WPEN 0x80 /* writeprotect enable */
  54. #define AT25_INSTR_BIT3 0x08 /* additional address bit in instr */
  55. #define FM25_ID_LEN 9 /* ID length */
  56. /*
  57. * Specs often allow 5ms for a page write, sometimes 20ms;
  58. * it's important to recover from write timeouts.
  59. */
  60. #define EE_TIMEOUT 25
  61. /*-------------------------------------------------------------------------*/
  62. #define io_limit PAGE_SIZE /* bytes */
  63. static int at25_ee_read(void *priv, unsigned int offset,
  64. void *val, size_t count)
  65. {
  66. struct at25_data *at25 = priv;
  67. char *buf = val;
  68. size_t max_chunk = spi_max_transfer_size(at25->spi);
  69. unsigned int msg_offset = offset;
  70. size_t bytes_left = count;
  71. size_t segment;
  72. u8 *cp;
  73. ssize_t status;
  74. struct spi_transfer t[2];
  75. struct spi_message m;
  76. u8 instr;
  77. if (unlikely(offset >= at25->chip.byte_len))
  78. return -EINVAL;
  79. if ((offset + count) > at25->chip.byte_len)
  80. count = at25->chip.byte_len - offset;
  81. if (unlikely(!count))
  82. return -EINVAL;
  83. do {
  84. segment = min(bytes_left, max_chunk);
  85. cp = at25->command;
  86. instr = AT25_READ;
  87. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  88. if (msg_offset >= BIT(at25->addrlen * 8))
  89. instr |= AT25_INSTR_BIT3;
  90. mutex_lock(&at25->lock);
  91. *cp++ = instr;
  92. /* 8/16/24-bit address is written MSB first */
  93. switch (at25->addrlen) {
  94. default: /* case 3 */
  95. *cp++ = msg_offset >> 16;
  96. fallthrough;
  97. case 2:
  98. *cp++ = msg_offset >> 8;
  99. fallthrough;
  100. case 1:
  101. case 0: /* can't happen: for better code generation */
  102. *cp++ = msg_offset >> 0;
  103. }
  104. spi_message_init(&m);
  105. memset(t, 0, sizeof(t));
  106. t[0].tx_buf = at25->command;
  107. t[0].len = at25->addrlen + 1;
  108. spi_message_add_tail(&t[0], &m);
  109. t[1].rx_buf = buf;
  110. t[1].len = segment;
  111. spi_message_add_tail(&t[1], &m);
  112. status = spi_sync(at25->spi, &m);
  113. mutex_unlock(&at25->lock);
  114. if (status)
  115. return status;
  116. msg_offset += segment;
  117. buf += segment;
  118. bytes_left -= segment;
  119. } while (bytes_left > 0);
  120. dev_dbg(&at25->spi->dev, "read %zu bytes at %d\n",
  121. count, offset);
  122. return 0;
  123. }
  124. /* Read extra registers as ID or serial number */
  125. static int fm25_aux_read(struct at25_data *at25, u8 *buf, uint8_t command,
  126. int len)
  127. {
  128. int status;
  129. struct spi_transfer t[2];
  130. struct spi_message m;
  131. spi_message_init(&m);
  132. memset(t, 0, sizeof(t));
  133. t[0].tx_buf = at25->command;
  134. t[0].len = 1;
  135. spi_message_add_tail(&t[0], &m);
  136. t[1].rx_buf = buf;
  137. t[1].len = len;
  138. spi_message_add_tail(&t[1], &m);
  139. mutex_lock(&at25->lock);
  140. at25->command[0] = command;
  141. status = spi_sync(at25->spi, &m);
  142. dev_dbg(&at25->spi->dev, "read %d aux bytes --> %d\n", len, status);
  143. mutex_unlock(&at25->lock);
  144. return status;
  145. }
  146. static ssize_t sernum_show(struct device *dev, struct device_attribute *attr, char *buf)
  147. {
  148. struct at25_data *at25;
  149. at25 = dev_get_drvdata(dev);
  150. return sysfs_emit(buf, "%*ph\n", (int)sizeof(at25->sernum), at25->sernum);
  151. }
  152. static DEVICE_ATTR_RO(sernum);
  153. static struct attribute *sernum_attrs[] = {
  154. &dev_attr_sernum.attr,
  155. NULL,
  156. };
  157. ATTRIBUTE_GROUPS(sernum);
  158. static int at25_ee_write(void *priv, unsigned int off, void *val, size_t count)
  159. {
  160. struct at25_data *at25 = priv;
  161. size_t maxsz = spi_max_transfer_size(at25->spi);
  162. const char *buf = val;
  163. int status = 0;
  164. unsigned buf_size;
  165. u8 *bounce;
  166. if (unlikely(off >= at25->chip.byte_len))
  167. return -EFBIG;
  168. if ((off + count) > at25->chip.byte_len)
  169. count = at25->chip.byte_len - off;
  170. if (unlikely(!count))
  171. return -EINVAL;
  172. /* Temp buffer starts with command and address */
  173. buf_size = at25->chip.page_size;
  174. if (buf_size > io_limit)
  175. buf_size = io_limit;
  176. bounce = kmalloc(buf_size + at25->addrlen + 1, GFP_KERNEL);
  177. if (!bounce)
  178. return -ENOMEM;
  179. /*
  180. * For write, rollover is within the page ... so we write at
  181. * most one page, then manually roll over to the next page.
  182. */
  183. mutex_lock(&at25->lock);
  184. do {
  185. unsigned long timeout, retries;
  186. unsigned segment;
  187. unsigned offset = off;
  188. u8 *cp = bounce;
  189. int sr;
  190. u8 instr;
  191. *cp = AT25_WREN;
  192. status = spi_write(at25->spi, cp, 1);
  193. if (status < 0) {
  194. dev_dbg(&at25->spi->dev, "WREN --> %d\n", status);
  195. break;
  196. }
  197. instr = AT25_WRITE;
  198. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  199. if (offset >= BIT(at25->addrlen * 8))
  200. instr |= AT25_INSTR_BIT3;
  201. *cp++ = instr;
  202. /* 8/16/24-bit address is written MSB first */
  203. switch (at25->addrlen) {
  204. default: /* case 3 */
  205. *cp++ = offset >> 16;
  206. fallthrough;
  207. case 2:
  208. *cp++ = offset >> 8;
  209. fallthrough;
  210. case 1:
  211. case 0: /* can't happen: for better code generation */
  212. *cp++ = offset >> 0;
  213. }
  214. /* Write as much of a page as we can */
  215. segment = buf_size - (offset % buf_size);
  216. if (segment > count)
  217. segment = count;
  218. if (segment > maxsz)
  219. segment = maxsz;
  220. memcpy(cp, buf, segment);
  221. status = spi_write(at25->spi, bounce,
  222. segment + at25->addrlen + 1);
  223. dev_dbg(&at25->spi->dev, "write %u bytes at %u --> %d\n",
  224. segment, offset, status);
  225. if (status < 0)
  226. break;
  227. /*
  228. * REVISIT this should detect (or prevent) failed writes
  229. * to read-only sections of the EEPROM...
  230. */
  231. /* Wait for non-busy status */
  232. timeout = jiffies + msecs_to_jiffies(EE_TIMEOUT);
  233. retries = 0;
  234. do {
  235. sr = spi_w8r8(at25->spi, AT25_RDSR);
  236. if (sr < 0 || (sr & AT25_SR_nRDY)) {
  237. dev_dbg(&at25->spi->dev,
  238. "rdsr --> %d (%02x)\n", sr, sr);
  239. /* at HZ=100, this is sloooow */
  240. msleep(1);
  241. continue;
  242. }
  243. if (!(sr & AT25_SR_nRDY))
  244. break;
  245. } while (retries++ < 3 || time_before_eq(jiffies, timeout));
  246. if ((sr < 0) || (sr & AT25_SR_nRDY)) {
  247. dev_err(&at25->spi->dev,
  248. "write %u bytes offset %u, timeout after %u msecs\n",
  249. segment, offset,
  250. jiffies_to_msecs(jiffies -
  251. (timeout - EE_TIMEOUT)));
  252. status = -ETIMEDOUT;
  253. break;
  254. }
  255. off += segment;
  256. buf += segment;
  257. count -= segment;
  258. } while (count > 0);
  259. mutex_unlock(&at25->lock);
  260. kfree(bounce);
  261. return status;
  262. }
  263. /*-------------------------------------------------------------------------*/
  264. static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
  265. {
  266. u32 val;
  267. int err;
  268. strscpy(chip->name, "at25", sizeof(chip->name));
  269. err = device_property_read_u32(dev, "size", &val);
  270. if (err)
  271. err = device_property_read_u32(dev, "at25,byte-len", &val);
  272. if (err) {
  273. dev_err(dev, "Error: missing \"size\" property\n");
  274. return err;
  275. }
  276. chip->byte_len = val;
  277. err = device_property_read_u32(dev, "pagesize", &val);
  278. if (err)
  279. err = device_property_read_u32(dev, "at25,page-size", &val);
  280. if (err) {
  281. dev_err(dev, "Error: missing \"pagesize\" property\n");
  282. return err;
  283. }
  284. chip->page_size = val;
  285. err = device_property_read_u32(dev, "address-width", &val);
  286. if (err) {
  287. err = device_property_read_u32(dev, "at25,addr-mode", &val);
  288. if (err) {
  289. dev_err(dev, "Error: missing \"address-width\" property\n");
  290. return err;
  291. }
  292. chip->flags = (u16)val;
  293. } else {
  294. switch (val) {
  295. case 9:
  296. chip->flags |= EE_INSTR_BIT3_IS_ADDR;
  297. fallthrough;
  298. case 8:
  299. chip->flags |= EE_ADDR1;
  300. break;
  301. case 16:
  302. chip->flags |= EE_ADDR2;
  303. break;
  304. case 24:
  305. chip->flags |= EE_ADDR3;
  306. break;
  307. default:
  308. dev_err(dev,
  309. "Error: bad \"address-width\" property: %u\n",
  310. val);
  311. return -ENODEV;
  312. }
  313. if (device_property_present(dev, "read-only"))
  314. chip->flags |= EE_READONLY;
  315. }
  316. return 0;
  317. }
  318. static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
  319. {
  320. struct at25_data *at25 = container_of(chip, struct at25_data, chip);
  321. u8 sernum[FM25_SN_LEN];
  322. u8 id[FM25_ID_LEN];
  323. int i;
  324. strscpy(chip->name, "fm25", sizeof(chip->name));
  325. /* Get ID of chip */
  326. fm25_aux_read(at25, id, FM25_RDID, FM25_ID_LEN);
  327. if (id[6] != 0xc2) {
  328. dev_err(dev, "Error: no Cypress FRAM (id %02x)\n", id[6]);
  329. return -ENODEV;
  330. }
  331. /* Set size found in ID */
  332. if (id[7] < 0x21 || id[7] > 0x26) {
  333. dev_err(dev, "Error: unsupported size (id %02x)\n", id[7]);
  334. return -ENODEV;
  335. }
  336. chip->byte_len = BIT(id[7] - 0x21 + 4) * 1024;
  337. if (chip->byte_len > 64 * 1024)
  338. chip->flags |= EE_ADDR3;
  339. else
  340. chip->flags |= EE_ADDR2;
  341. if (id[8]) {
  342. fm25_aux_read(at25, sernum, FM25_RDSN, FM25_SN_LEN);
  343. /* Swap byte order */
  344. for (i = 0; i < FM25_SN_LEN; i++)
  345. at25->sernum[i] = sernum[FM25_SN_LEN - 1 - i];
  346. }
  347. chip->page_size = PAGE_SIZE;
  348. return 0;
  349. }
  350. static const struct of_device_id at25_of_match[] = {
  351. { .compatible = "atmel,at25" },
  352. { .compatible = "cypress,fm25" },
  353. { }
  354. };
  355. MODULE_DEVICE_TABLE(of, at25_of_match);
  356. static const struct spi_device_id at25_spi_ids[] = {
  357. { .name = "at25" },
  358. { .name = "fm25" },
  359. { }
  360. };
  361. MODULE_DEVICE_TABLE(spi, at25_spi_ids);
  362. static int at25_probe(struct spi_device *spi)
  363. {
  364. struct at25_data *at25 = NULL;
  365. int err;
  366. int sr;
  367. struct spi_eeprom *pdata;
  368. bool is_fram;
  369. err = device_property_match_string(&spi->dev, "compatible", "cypress,fm25");
  370. if (err >= 0)
  371. is_fram = true;
  372. else
  373. is_fram = false;
  374. /*
  375. * Ping the chip ... the status register is pretty portable,
  376. * unlike probing manufacturer IDs. We do expect that system
  377. * firmware didn't write it in the past few milliseconds!
  378. */
  379. sr = spi_w8r8(spi, AT25_RDSR);
  380. if (sr < 0 || sr & AT25_SR_nRDY) {
  381. dev_dbg(&spi->dev, "rdsr --> %d (%02x)\n", sr, sr);
  382. return -ENXIO;
  383. }
  384. at25 = devm_kzalloc(&spi->dev, sizeof(*at25), GFP_KERNEL);
  385. if (!at25)
  386. return -ENOMEM;
  387. mutex_init(&at25->lock);
  388. at25->spi = spi;
  389. spi_set_drvdata(spi, at25);
  390. /* Chip description */
  391. pdata = dev_get_platdata(&spi->dev);
  392. if (pdata) {
  393. at25->chip = *pdata;
  394. } else {
  395. if (is_fram)
  396. err = at25_fram_to_chip(&spi->dev, &at25->chip);
  397. else
  398. err = at25_fw_to_chip(&spi->dev, &at25->chip);
  399. if (err)
  400. return err;
  401. }
  402. /* For now we only support 8/16/24 bit addressing */
  403. if (at25->chip.flags & EE_ADDR1)
  404. at25->addrlen = 1;
  405. else if (at25->chip.flags & EE_ADDR2)
  406. at25->addrlen = 2;
  407. else if (at25->chip.flags & EE_ADDR3)
  408. at25->addrlen = 3;
  409. else {
  410. dev_dbg(&spi->dev, "unsupported address type\n");
  411. return -EINVAL;
  412. }
  413. at25->nvmem_config.type = is_fram ? NVMEM_TYPE_FRAM : NVMEM_TYPE_EEPROM;
  414. at25->nvmem_config.name = dev_name(&spi->dev);
  415. at25->nvmem_config.dev = &spi->dev;
  416. at25->nvmem_config.read_only = at25->chip.flags & EE_READONLY;
  417. at25->nvmem_config.root_only = true;
  418. at25->nvmem_config.owner = THIS_MODULE;
  419. at25->nvmem_config.compat = true;
  420. at25->nvmem_config.base_dev = &spi->dev;
  421. at25->nvmem_config.reg_read = at25_ee_read;
  422. at25->nvmem_config.reg_write = at25_ee_write;
  423. at25->nvmem_config.priv = at25;
  424. at25->nvmem_config.stride = 1;
  425. at25->nvmem_config.word_size = 1;
  426. at25->nvmem_config.size = at25->chip.byte_len;
  427. at25->nvmem = devm_nvmem_register(&spi->dev, &at25->nvmem_config);
  428. if (IS_ERR(at25->nvmem))
  429. return PTR_ERR(at25->nvmem);
  430. dev_info(&spi->dev, "%d %s %s %s%s, pagesize %u\n",
  431. (at25->chip.byte_len < 1024) ?
  432. at25->chip.byte_len : (at25->chip.byte_len / 1024),
  433. (at25->chip.byte_len < 1024) ? "Byte" : "KByte",
  434. at25->chip.name, is_fram ? "fram" : "eeprom",
  435. (at25->chip.flags & EE_READONLY) ? " (readonly)" : "",
  436. at25->chip.page_size);
  437. return 0;
  438. }
  439. /*-------------------------------------------------------------------------*/
  440. static struct spi_driver at25_driver = {
  441. .driver = {
  442. .name = "at25",
  443. .of_match_table = at25_of_match,
  444. .dev_groups = sernum_groups,
  445. },
  446. .probe = at25_probe,
  447. .id_table = at25_spi_ids,
  448. };
  449. module_spi_driver(at25_driver);
  450. MODULE_DESCRIPTION("Driver for most SPI EEPROMs");
  451. MODULE_AUTHOR("David Brownell");
  452. MODULE_LICENSE("GPL");
  453. MODULE_ALIAS("spi:at25");