at25.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585
  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/cleanup.h>
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/iopoll.h>
  13. #include <linux/kernel.h>
  14. #include <linux/module.h>
  15. #include <linux/property.h>
  16. #include <linux/sched.h>
  17. #include <linux/slab.h>
  18. #include <linux/spi/eeprom.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/spi/spi-mem.h>
  21. #include <linux/nvmem-provider.h>
  22. /*
  23. * NOTE: this is an *EEPROM* driver. The vagaries of product naming
  24. * mean that some AT25 products are EEPROMs, and others are FLASH.
  25. * Handle FLASH chips with the drivers/mtd/devices/m25p80.c driver,
  26. * not this one!
  27. *
  28. * EEPROMs that can be used with this driver include, for example:
  29. * AT25M02, AT25128B
  30. */
  31. #define FM25_SN_LEN 8 /* serial number length */
  32. #define FM25_MAX_ID_LEN 9 /* ID length */
  33. #define EE_MAXADDRLEN 3 /* 24 bit addresses, up to 2 MBytes */
  34. struct at25_data {
  35. struct spi_eeprom chip;
  36. struct spi_mem *spimem;
  37. struct mutex lock;
  38. unsigned addrlen;
  39. struct nvmem_config nvmem_config;
  40. struct nvmem_device *nvmem;
  41. u8 sernum[FM25_SN_LEN];
  42. u8 id[FM25_MAX_ID_LEN];
  43. u8 id_len;
  44. };
  45. #define AT25_WREN 0x06 /* latch the write enable */
  46. #define AT25_WRDI 0x04 /* reset the write enable */
  47. #define AT25_RDSR 0x05 /* read status register */
  48. #define AT25_WRSR 0x01 /* write status register */
  49. #define AT25_READ 0x03 /* read byte(s) */
  50. #define AT25_WRITE 0x02 /* write byte(s)/sector */
  51. #define FM25_SLEEP 0xb9 /* enter sleep mode */
  52. #define FM25_RDID 0x9f /* read device ID */
  53. #define FM25_RDSN 0xc3 /* read S/N */
  54. #define AT25_SR_nRDY 0x01 /* nRDY = write-in-progress */
  55. #define AT25_SR_WEN 0x02 /* write enable (latched) */
  56. #define AT25_SR_BP0 0x04 /* BP for software writeprotect */
  57. #define AT25_SR_BP1 0x08
  58. #define AT25_SR_WPEN 0x80 /* writeprotect enable */
  59. #define AT25_INSTR_BIT3 0x08 /* additional address bit in instr */
  60. /*
  61. * Specs often allow 5ms for a page write, sometimes 20ms;
  62. * it's important to recover from write timeouts.
  63. */
  64. #define EE_TIMEOUT 25
  65. /*-------------------------------------------------------------------------*/
  66. #define io_limit PAGE_SIZE /* bytes */
  67. /* Handle the address MSB as part of instruction byte */
  68. static u8 at25_instr(struct at25_data *at25, u8 instr, unsigned int off)
  69. {
  70. if (!(at25->chip.flags & EE_INSTR_BIT3_IS_ADDR))
  71. return instr;
  72. if (off < BIT(at25->addrlen * 8))
  73. return instr;
  74. return instr | AT25_INSTR_BIT3;
  75. }
  76. static int at25_ee_read(void *priv, unsigned int offset,
  77. void *val, size_t count)
  78. {
  79. u8 *bounce __free(kfree) = kmalloc(min(count, io_limit), GFP_KERNEL);
  80. struct at25_data *at25 = priv;
  81. char *buf = val;
  82. unsigned int msg_offset = offset;
  83. size_t bytes_left = count;
  84. size_t segment;
  85. int status;
  86. if (!bounce)
  87. return -ENOMEM;
  88. if (unlikely(offset >= at25->chip.byte_len))
  89. return -EINVAL;
  90. if ((offset + count) > at25->chip.byte_len)
  91. count = at25->chip.byte_len - offset;
  92. if (unlikely(!count))
  93. return -EINVAL;
  94. do {
  95. struct spi_mem_op op;
  96. segment = min(bytes_left, io_limit);
  97. op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(at25_instr(at25, AT25_READ,
  98. msg_offset), 1),
  99. SPI_MEM_OP_ADDR(at25->addrlen, msg_offset, 1),
  100. SPI_MEM_OP_NO_DUMMY,
  101. SPI_MEM_OP_DATA_IN(segment, bounce, 1));
  102. status = spi_mem_adjust_op_size(at25->spimem, &op);
  103. if (status)
  104. return status;
  105. segment = op.data.nbytes;
  106. mutex_lock(&at25->lock);
  107. status = spi_mem_exec_op(at25->spimem, &op);
  108. mutex_unlock(&at25->lock);
  109. if (status)
  110. return status;
  111. memcpy(buf, bounce, segment);
  112. msg_offset += segment;
  113. buf += segment;
  114. bytes_left -= segment;
  115. } while (bytes_left > 0);
  116. dev_dbg(&at25->spimem->spi->dev, "read %zu bytes at %d\n",
  117. count, offset);
  118. return 0;
  119. }
  120. /*
  121. * Read extra registers as ID or serial number
  122. *
  123. * Allow for the callers to provide @buf on stack (not necessary DMA-capable)
  124. * by allocating a bounce buffer internally.
  125. */
  126. static int fm25_aux_read(struct at25_data *at25, u8 *buf, uint8_t command,
  127. int len)
  128. {
  129. u8 *bounce __free(kfree) = kmalloc(len, GFP_KERNEL);
  130. struct spi_mem_op op;
  131. int status;
  132. if (!bounce)
  133. return -ENOMEM;
  134. op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(command, 1),
  135. SPI_MEM_OP_NO_ADDR,
  136. SPI_MEM_OP_NO_DUMMY,
  137. SPI_MEM_OP_DATA_IN(len, bounce, 1));
  138. status = spi_mem_exec_op(at25->spimem, &op);
  139. dev_dbg(&at25->spimem->spi->dev, "read %d aux bytes --> %d\n", len, status);
  140. if (status)
  141. return status;
  142. memcpy(buf, bounce, len);
  143. return 0;
  144. }
  145. static ssize_t sernum_show(struct device *dev, struct device_attribute *attr, char *buf)
  146. {
  147. struct at25_data *at25;
  148. at25 = dev_get_drvdata(dev);
  149. return sysfs_emit(buf, "%*ph\n", (int)sizeof(at25->sernum), at25->sernum);
  150. }
  151. static DEVICE_ATTR_RO(sernum);
  152. static ssize_t jedec_id_show(struct device *dev, struct device_attribute *attr, char *buf)
  153. {
  154. struct at25_data *at25;
  155. at25 = dev_get_drvdata(dev);
  156. if (!at25->id_len)
  157. return -EOPNOTSUPP;
  158. return sysfs_emit(buf, "%*phN\n", at25->id_len, at25->id);
  159. }
  160. static DEVICE_ATTR_RO(jedec_id);
  161. static struct attribute *at25_attrs[] = {
  162. &dev_attr_sernum.attr,
  163. &dev_attr_jedec_id.attr,
  164. NULL,
  165. };
  166. ATTRIBUTE_GROUPS(at25);
  167. /*
  168. * Poll Read Status Register with timeout
  169. *
  170. * Return:
  171. * 0, if the chip is ready
  172. * [positive] Status Register value as-is, if the chip is busy
  173. * [negative] error code in case of read failure
  174. */
  175. static int at25_wait_ready(struct at25_data *at25)
  176. {
  177. u8 *bounce __free(kfree) = kmalloc(1, GFP_KERNEL);
  178. struct spi_mem_op op;
  179. int status;
  180. if (!bounce)
  181. return -ENOMEM;
  182. op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(AT25_RDSR, 1),
  183. SPI_MEM_OP_NO_ADDR,
  184. SPI_MEM_OP_NO_DUMMY,
  185. SPI_MEM_OP_DATA_IN(1, bounce, 1));
  186. read_poll_timeout(spi_mem_exec_op, status,
  187. status || !(bounce[0] & AT25_SR_nRDY), false,
  188. USEC_PER_MSEC, USEC_PER_MSEC * EE_TIMEOUT,
  189. at25->spimem, &op);
  190. if (status < 0)
  191. return status;
  192. if (!(bounce[0] & AT25_SR_nRDY))
  193. return 0;
  194. return bounce[0];
  195. }
  196. static int at25_ee_write(void *priv, unsigned int off, void *val, size_t count)
  197. {
  198. u8 *bounce __free(kfree) = kmalloc(min(count, io_limit), GFP_KERNEL);
  199. struct at25_data *at25 = priv;
  200. const char *buf = val;
  201. unsigned int buf_size;
  202. int status;
  203. if (unlikely(off >= at25->chip.byte_len))
  204. return -EFBIG;
  205. if ((off + count) > at25->chip.byte_len)
  206. count = at25->chip.byte_len - off;
  207. if (unlikely(!count))
  208. return -EINVAL;
  209. buf_size = at25->chip.page_size;
  210. if (!bounce)
  211. return -ENOMEM;
  212. /*
  213. * For write, rollover is within the page ... so we write at
  214. * most one page, then manually roll over to the next page.
  215. */
  216. guard(mutex)(&at25->lock);
  217. do {
  218. struct spi_mem_op op = SPI_MEM_OP(SPI_MEM_OP_CMD(AT25_WREN, 1),
  219. SPI_MEM_OP_NO_ADDR,
  220. SPI_MEM_OP_NO_DUMMY,
  221. SPI_MEM_OP_NO_DATA);
  222. unsigned int segment;
  223. status = spi_mem_exec_op(at25->spimem, &op);
  224. if (status < 0) {
  225. dev_dbg(&at25->spimem->spi->dev, "WREN --> %d\n", status);
  226. return status;
  227. }
  228. /* Write as much of a page as we can */
  229. segment = buf_size - (off % buf_size);
  230. if (segment > count)
  231. segment = count;
  232. if (segment > io_limit)
  233. segment = io_limit;
  234. op = (struct spi_mem_op)SPI_MEM_OP(SPI_MEM_OP_CMD(at25_instr(at25, AT25_WRITE, off),
  235. 1),
  236. SPI_MEM_OP_ADDR(at25->addrlen, off, 1),
  237. SPI_MEM_OP_NO_DUMMY,
  238. SPI_MEM_OP_DATA_OUT(segment, bounce, 1));
  239. status = spi_mem_adjust_op_size(at25->spimem, &op);
  240. if (status)
  241. return status;
  242. segment = op.data.nbytes;
  243. memcpy(bounce, buf, segment);
  244. status = spi_mem_exec_op(at25->spimem, &op);
  245. dev_dbg(&at25->spimem->spi->dev, "write %u bytes at %u --> %d\n",
  246. segment, off, status);
  247. if (status)
  248. return status;
  249. /*
  250. * REVISIT this should detect (or prevent) failed writes
  251. * to read-only sections of the EEPROM...
  252. */
  253. status = at25_wait_ready(at25);
  254. if (status < 0) {
  255. dev_err_probe(&at25->spimem->spi->dev, status,
  256. "Read Status Redister command failed\n");
  257. return status;
  258. }
  259. if (status) {
  260. dev_dbg(&at25->spimem->spi->dev,
  261. "Status %02x\n", status);
  262. dev_err(&at25->spimem->spi->dev,
  263. "write %u bytes offset %u, timeout after %u msecs\n",
  264. segment, off, EE_TIMEOUT);
  265. return -ETIMEDOUT;
  266. }
  267. off += segment;
  268. buf += segment;
  269. count -= segment;
  270. } while (count > 0);
  271. return status;
  272. }
  273. /*-------------------------------------------------------------------------*/
  274. static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
  275. {
  276. u32 val;
  277. int err;
  278. strscpy(chip->name, "at25", sizeof(chip->name));
  279. err = device_property_read_u32(dev, "size", &val);
  280. if (err)
  281. err = device_property_read_u32(dev, "at25,byte-len", &val);
  282. if (err) {
  283. dev_err(dev, "Error: missing \"size\" property\n");
  284. return err;
  285. }
  286. chip->byte_len = val;
  287. err = device_property_read_u32(dev, "pagesize", &val);
  288. if (err)
  289. err = device_property_read_u32(dev, "at25,page-size", &val);
  290. if (err) {
  291. dev_err(dev, "Error: missing \"pagesize\" property\n");
  292. return err;
  293. }
  294. chip->page_size = val;
  295. err = device_property_read_u32(dev, "address-width", &val);
  296. if (err) {
  297. err = device_property_read_u32(dev, "at25,addr-mode", &val);
  298. if (err) {
  299. dev_err(dev, "Error: missing \"address-width\" property\n");
  300. return err;
  301. }
  302. chip->flags = (u16)val;
  303. } else {
  304. switch (val) {
  305. case 9:
  306. chip->flags |= EE_INSTR_BIT3_IS_ADDR;
  307. fallthrough;
  308. case 8:
  309. chip->flags |= EE_ADDR1;
  310. break;
  311. case 16:
  312. chip->flags |= EE_ADDR2;
  313. break;
  314. case 24:
  315. chip->flags |= EE_ADDR3;
  316. break;
  317. default:
  318. dev_err(dev,
  319. "Error: bad \"address-width\" property: %u\n",
  320. val);
  321. return -ENODEV;
  322. }
  323. if (device_property_present(dev, "read-only"))
  324. chip->flags |= EE_READONLY;
  325. }
  326. return 0;
  327. }
  328. static int at25_fram_to_chip(struct device *dev, struct spi_eeprom *chip)
  329. {
  330. struct at25_data *at25 = container_of(chip, struct at25_data, chip);
  331. u8 sernum[FM25_SN_LEN];
  332. u8 id[FM25_MAX_ID_LEN];
  333. u32 val;
  334. int i;
  335. strscpy(chip->name, "fm25", sizeof(chip->name));
  336. if (!device_property_read_u32(dev, "size", &val)) {
  337. chip->byte_len = val;
  338. } else {
  339. /* Get ID of chip */
  340. fm25_aux_read(at25, id, FM25_RDID, FM25_MAX_ID_LEN);
  341. /* Store the unprocessed ID for exposing via sysfs */
  342. memcpy(at25->id, id, FM25_MAX_ID_LEN);
  343. at25->id_len = FM25_MAX_ID_LEN;
  344. /* There are inside-out FRAM variations, detect them and reverse the ID bytes */
  345. if (id[6] == 0x7f && id[2] == 0xc2)
  346. for (i = 0; i < ARRAY_SIZE(id) / 2; i++) {
  347. u8 tmp = id[i];
  348. int j = ARRAY_SIZE(id) - i - 1;
  349. id[i] = id[j];
  350. id[j] = tmp;
  351. }
  352. if (id[6] == 0xc2) {
  353. at25->id_len = 9;
  354. switch (id[7]) {
  355. case 0x21 ... 0x26:
  356. chip->byte_len = BIT(id[7] - 0x21 + 4) * 1024;
  357. break;
  358. case 0x2a ... 0x30:
  359. /* CY15B102QN ... CY15B116QN */
  360. chip->byte_len = BIT(((id[7] >> 1) & 0xf) + 13);
  361. break;
  362. default:
  363. dev_err(dev, "Error: unsupported size (id %02x)\n", id[7]);
  364. return -ENODEV;
  365. }
  366. } else if (id[2] == 0x82 && id[3] == 0x06) {
  367. at25->id_len = 8;
  368. switch (id[1]) {
  369. case 0x51 ... 0x54:
  370. /* CY15B102QSN ... CY15B204QSN */
  371. chip->byte_len = BIT(((id[0] >> 3) & 0x1F) + 9);
  372. break;
  373. default:
  374. dev_err(dev, "Error: unsupported product id %02x\n", id[1]);
  375. return -ENODEV;
  376. }
  377. } else {
  378. dev_err(dev, "Error: unrecognized JEDEC ID format: %*ph\n",
  379. FM25_MAX_ID_LEN, id);
  380. return -ENODEV;
  381. }
  382. fm25_aux_read(at25, sernum, FM25_RDSN, FM25_SN_LEN);
  383. /* Swap byte order */
  384. for (i = 0; i < FM25_SN_LEN; i++)
  385. at25->sernum[i] = sernum[FM25_SN_LEN - 1 - i];
  386. }
  387. if (chip->byte_len > 64 * 1024)
  388. chip->flags |= EE_ADDR3;
  389. else
  390. chip->flags |= EE_ADDR2;
  391. chip->page_size = PAGE_SIZE;
  392. return 0;
  393. }
  394. static const struct of_device_id at25_of_match[] = {
  395. { .compatible = "atmel,at25" },
  396. { .compatible = "cypress,fm25" },
  397. { }
  398. };
  399. MODULE_DEVICE_TABLE(of, at25_of_match);
  400. static const struct spi_device_id at25_spi_ids[] = {
  401. { .name = "at25" },
  402. { .name = "fm25" },
  403. { }
  404. };
  405. MODULE_DEVICE_TABLE(spi, at25_spi_ids);
  406. static int at25_probe(struct spi_mem *mem)
  407. {
  408. struct spi_device *spi = mem->spi;
  409. struct spi_eeprom *pdata;
  410. struct at25_data *at25;
  411. bool is_fram;
  412. int err;
  413. at25 = devm_kzalloc(&spi->dev, sizeof(*at25), GFP_KERNEL);
  414. if (!at25)
  415. return -ENOMEM;
  416. at25->spimem = mem;
  417. /*
  418. * Ping the chip ... the status register is pretty portable,
  419. * unlike probing manufacturer IDs.
  420. */
  421. err = at25_wait_ready(at25);
  422. if (err < 0)
  423. return dev_err_probe(&spi->dev, err, "Read Status Register command failed\n");
  424. if (err) {
  425. dev_err(&spi->dev, "Not ready (%02x)\n", err);
  426. return -ENXIO;
  427. }
  428. mutex_init(&at25->lock);
  429. spi_set_drvdata(spi, at25);
  430. is_fram = fwnode_device_is_compatible(dev_fwnode(&spi->dev), "cypress,fm25");
  431. /* Chip description */
  432. pdata = dev_get_platdata(&spi->dev);
  433. if (pdata) {
  434. at25->chip = *pdata;
  435. } else {
  436. if (is_fram)
  437. err = at25_fram_to_chip(&spi->dev, &at25->chip);
  438. else
  439. err = at25_fw_to_chip(&spi->dev, &at25->chip);
  440. if (err)
  441. return err;
  442. }
  443. /* For now we only support 8/16/24 bit addressing */
  444. if (at25->chip.flags & EE_ADDR1)
  445. at25->addrlen = 1;
  446. else if (at25->chip.flags & EE_ADDR2)
  447. at25->addrlen = 2;
  448. else if (at25->chip.flags & EE_ADDR3)
  449. at25->addrlen = 3;
  450. else {
  451. dev_dbg(&spi->dev, "unsupported address type\n");
  452. return -EINVAL;
  453. }
  454. at25->nvmem_config.type = is_fram ? NVMEM_TYPE_FRAM : NVMEM_TYPE_EEPROM;
  455. at25->nvmem_config.name = dev_name(&spi->dev);
  456. at25->nvmem_config.dev = &spi->dev;
  457. at25->nvmem_config.read_only = at25->chip.flags & EE_READONLY;
  458. at25->nvmem_config.root_only = true;
  459. at25->nvmem_config.owner = THIS_MODULE;
  460. at25->nvmem_config.compat = true;
  461. at25->nvmem_config.base_dev = &spi->dev;
  462. at25->nvmem_config.reg_read = at25_ee_read;
  463. at25->nvmem_config.reg_write = at25_ee_write;
  464. at25->nvmem_config.priv = at25;
  465. at25->nvmem_config.stride = 1;
  466. at25->nvmem_config.word_size = 1;
  467. at25->nvmem_config.size = at25->chip.byte_len;
  468. at25->nvmem = devm_nvmem_register(&spi->dev, &at25->nvmem_config);
  469. if (IS_ERR(at25->nvmem))
  470. return PTR_ERR(at25->nvmem);
  471. dev_info(&spi->dev, "%d %s %s %s%s, pagesize %u\n",
  472. (at25->chip.byte_len < 1024) ?
  473. at25->chip.byte_len : (at25->chip.byte_len / 1024),
  474. (at25->chip.byte_len < 1024) ? "Byte" : "KByte",
  475. at25->chip.name, is_fram ? "fram" : "eeprom",
  476. (at25->chip.flags & EE_READONLY) ? " (readonly)" : "",
  477. at25->chip.page_size);
  478. return 0;
  479. }
  480. /*-------------------------------------------------------------------------*/
  481. static struct spi_mem_driver at25_driver = {
  482. .spidrv = {
  483. .driver = {
  484. .name = "at25",
  485. .of_match_table = at25_of_match,
  486. .dev_groups = at25_groups,
  487. },
  488. .id_table = at25_spi_ids,
  489. },
  490. .probe = at25_probe,
  491. };
  492. module_spi_mem_driver(at25_driver);
  493. MODULE_DESCRIPTION("Driver for most SPI EEPROMs");
  494. MODULE_AUTHOR("David Brownell");
  495. MODULE_LICENSE("GPL");