at25.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464
  1. /*
  2. * at25.c -- support most SPI EEPROMs, such as Atmel AT25 models
  3. *
  4. * Copyright (C) 2006 David Brownell
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. */
  11. #include <linux/kernel.h>
  12. #include <linux/module.h>
  13. #include <linux/slab.h>
  14. #include <linux/delay.h>
  15. #include <linux/device.h>
  16. #include <linux/sched.h>
  17. #include <linux/nvmem-provider.h>
  18. #include <linux/regmap.h>
  19. #include <linux/spi/spi.h>
  20. #include <linux/spi/eeprom.h>
  21. #include <linux/property.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. struct at25_data {
  29. struct spi_device *spi;
  30. struct mutex lock;
  31. struct spi_eeprom chip;
  32. unsigned addrlen;
  33. struct regmap_config regmap_config;
  34. struct nvmem_config nvmem_config;
  35. struct nvmem_device *nvmem;
  36. };
  37. #define AT25_WREN 0x06 /* latch the write enable */
  38. #define AT25_WRDI 0x04 /* reset the write enable */
  39. #define AT25_RDSR 0x05 /* read status register */
  40. #define AT25_WRSR 0x01 /* write status register */
  41. #define AT25_READ 0x03 /* read byte(s) */
  42. #define AT25_WRITE 0x02 /* write byte(s)/sector */
  43. #define AT25_SR_nRDY 0x01 /* nRDY = write-in-progress */
  44. #define AT25_SR_WEN 0x02 /* write enable (latched) */
  45. #define AT25_SR_BP0 0x04 /* BP for software writeprotect */
  46. #define AT25_SR_BP1 0x08
  47. #define AT25_SR_WPEN 0x80 /* writeprotect enable */
  48. #define AT25_INSTR_BIT3 0x08 /* Additional address bit in instr */
  49. #define EE_MAXADDRLEN 3 /* 24 bit addresses, up to 2 MBytes */
  50. /* Specs often allow 5 msec for a page write, sometimes 20 msec;
  51. * it's important to recover from write timeouts.
  52. */
  53. #define EE_TIMEOUT 25
  54. /*-------------------------------------------------------------------------*/
  55. #define io_limit PAGE_SIZE /* bytes */
  56. static ssize_t
  57. at25_ee_read(
  58. struct at25_data *at25,
  59. char *buf,
  60. unsigned offset,
  61. size_t count
  62. )
  63. {
  64. u8 command[EE_MAXADDRLEN + 1];
  65. u8 *cp;
  66. ssize_t status;
  67. struct spi_transfer t[2];
  68. struct spi_message m;
  69. u8 instr;
  70. if (unlikely(offset >= at25->chip.byte_len))
  71. return 0;
  72. if ((offset + count) > at25->chip.byte_len)
  73. count = at25->chip.byte_len - offset;
  74. if (unlikely(!count))
  75. return count;
  76. cp = command;
  77. instr = AT25_READ;
  78. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  79. if (offset >= (1U << (at25->addrlen * 8)))
  80. instr |= AT25_INSTR_BIT3;
  81. *cp++ = instr;
  82. /* 8/16/24-bit address is written MSB first */
  83. switch (at25->addrlen) {
  84. default: /* case 3 */
  85. *cp++ = offset >> 16;
  86. case 2:
  87. *cp++ = offset >> 8;
  88. case 1:
  89. case 0: /* can't happen: for better codegen */
  90. *cp++ = offset >> 0;
  91. }
  92. spi_message_init(&m);
  93. memset(t, 0, sizeof t);
  94. t[0].tx_buf = command;
  95. t[0].len = at25->addrlen + 1;
  96. spi_message_add_tail(&t[0], &m);
  97. t[1].rx_buf = buf;
  98. t[1].len = count;
  99. spi_message_add_tail(&t[1], &m);
  100. mutex_lock(&at25->lock);
  101. /* Read it all at once.
  102. *
  103. * REVISIT that's potentially a problem with large chips, if
  104. * other devices on the bus need to be accessed regularly or
  105. * this chip is clocked very slowly
  106. */
  107. status = spi_sync(at25->spi, &m);
  108. dev_dbg(&at25->spi->dev,
  109. "read %Zd bytes at %d --> %d\n",
  110. count, offset, (int) status);
  111. mutex_unlock(&at25->lock);
  112. return status ? status : count;
  113. }
  114. static int at25_regmap_read(void *context, const void *reg, size_t reg_size,
  115. void *val, size_t val_size)
  116. {
  117. struct at25_data *at25 = context;
  118. off_t offset = *(u32 *)reg;
  119. int err;
  120. err = at25_ee_read(at25, val, offset, val_size);
  121. if (err)
  122. return err;
  123. return 0;
  124. }
  125. static ssize_t
  126. at25_ee_write(struct at25_data *at25, const char *buf, loff_t off,
  127. size_t count)
  128. {
  129. ssize_t status = 0;
  130. unsigned written = 0;
  131. unsigned buf_size;
  132. u8 *bounce;
  133. if (unlikely(off >= at25->chip.byte_len))
  134. return -EFBIG;
  135. if ((off + count) > at25->chip.byte_len)
  136. count = at25->chip.byte_len - off;
  137. if (unlikely(!count))
  138. return count;
  139. /* Temp buffer starts with command and address */
  140. buf_size = at25->chip.page_size;
  141. if (buf_size > io_limit)
  142. buf_size = io_limit;
  143. bounce = kmalloc(buf_size + at25->addrlen + 1, GFP_KERNEL);
  144. if (!bounce)
  145. return -ENOMEM;
  146. /* For write, rollover is within the page ... so we write at
  147. * most one page, then manually roll over to the next page.
  148. */
  149. mutex_lock(&at25->lock);
  150. do {
  151. unsigned long timeout, retries;
  152. unsigned segment;
  153. unsigned offset = (unsigned) off;
  154. u8 *cp = bounce;
  155. int sr;
  156. u8 instr;
  157. *cp = AT25_WREN;
  158. status = spi_write(at25->spi, cp, 1);
  159. if (status < 0) {
  160. dev_dbg(&at25->spi->dev, "WREN --> %d\n",
  161. (int) status);
  162. break;
  163. }
  164. instr = AT25_WRITE;
  165. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  166. if (offset >= (1U << (at25->addrlen * 8)))
  167. instr |= AT25_INSTR_BIT3;
  168. *cp++ = instr;
  169. /* 8/16/24-bit address is written MSB first */
  170. switch (at25->addrlen) {
  171. default: /* case 3 */
  172. *cp++ = offset >> 16;
  173. case 2:
  174. *cp++ = offset >> 8;
  175. case 1:
  176. case 0: /* can't happen: for better codegen */
  177. *cp++ = offset >> 0;
  178. }
  179. /* Write as much of a page as we can */
  180. segment = buf_size - (offset % buf_size);
  181. if (segment > count)
  182. segment = count;
  183. memcpy(cp, buf, segment);
  184. status = spi_write(at25->spi, bounce,
  185. segment + at25->addrlen + 1);
  186. dev_dbg(&at25->spi->dev,
  187. "write %u bytes at %u --> %d\n",
  188. segment, offset, (int) status);
  189. if (status < 0)
  190. break;
  191. /* REVISIT this should detect (or prevent) failed writes
  192. * to readonly sections of the EEPROM...
  193. */
  194. /* Wait for non-busy status */
  195. timeout = jiffies + msecs_to_jiffies(EE_TIMEOUT);
  196. retries = 0;
  197. do {
  198. sr = spi_w8r8(at25->spi, AT25_RDSR);
  199. if (sr < 0 || (sr & AT25_SR_nRDY)) {
  200. dev_dbg(&at25->spi->dev,
  201. "rdsr --> %d (%02x)\n", sr, sr);
  202. /* at HZ=100, this is sloooow */
  203. msleep(1);
  204. continue;
  205. }
  206. if (!(sr & AT25_SR_nRDY))
  207. break;
  208. } while (retries++ < 3 || time_before_eq(jiffies, timeout));
  209. if ((sr < 0) || (sr & AT25_SR_nRDY)) {
  210. dev_err(&at25->spi->dev,
  211. "write %d bytes offset %d, "
  212. "timeout after %u msecs\n",
  213. segment, offset,
  214. jiffies_to_msecs(jiffies -
  215. (timeout - EE_TIMEOUT)));
  216. status = -ETIMEDOUT;
  217. break;
  218. }
  219. off += segment;
  220. buf += segment;
  221. count -= segment;
  222. written += segment;
  223. } while (count > 0);
  224. mutex_unlock(&at25->lock);
  225. kfree(bounce);
  226. return written ? written : status;
  227. }
  228. static int at25_regmap_write(void *context, const void *data, size_t count)
  229. {
  230. struct at25_data *at25 = context;
  231. const char *buf;
  232. u32 offset;
  233. size_t len;
  234. int err;
  235. memcpy(&offset, data, sizeof(offset));
  236. buf = (const char *)data + sizeof(offset);
  237. len = count - sizeof(offset);
  238. err = at25_ee_write(at25, buf, offset, len);
  239. if (err)
  240. return err;
  241. return 0;
  242. }
  243. static const struct regmap_bus at25_regmap_bus = {
  244. .read = at25_regmap_read,
  245. .write = at25_regmap_write,
  246. .reg_format_endian_default = REGMAP_ENDIAN_NATIVE,
  247. };
  248. /*-------------------------------------------------------------------------*/
  249. static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
  250. {
  251. u32 val;
  252. memset(chip, 0, sizeof(*chip));
  253. strncpy(chip->name, "at25", sizeof(chip->name));
  254. if (device_property_read_u32(dev, "size", &val) == 0 ||
  255. device_property_read_u32(dev, "at25,byte-len", &val) == 0) {
  256. chip->byte_len = val;
  257. } else {
  258. dev_err(dev, "Error: missing \"size\" property\n");
  259. return -ENODEV;
  260. }
  261. if (device_property_read_u32(dev, "pagesize", &val) == 0 ||
  262. device_property_read_u32(dev, "at25,page-size", &val) == 0) {
  263. chip->page_size = (u16)val;
  264. } else {
  265. dev_err(dev, "Error: missing \"pagesize\" property\n");
  266. return -ENODEV;
  267. }
  268. if (device_property_read_u32(dev, "at25,addr-mode", &val) == 0) {
  269. chip->flags = (u16)val;
  270. } else {
  271. if (device_property_read_u32(dev, "address-width", &val)) {
  272. dev_err(dev,
  273. "Error: missing \"address-width\" property\n");
  274. return -ENODEV;
  275. }
  276. switch (val) {
  277. case 8:
  278. chip->flags |= EE_ADDR1;
  279. break;
  280. case 16:
  281. chip->flags |= EE_ADDR2;
  282. break;
  283. case 24:
  284. chip->flags |= EE_ADDR3;
  285. break;
  286. default:
  287. dev_err(dev,
  288. "Error: bad \"address-width\" property: %u\n",
  289. val);
  290. return -ENODEV;
  291. }
  292. if (device_property_present(dev, "read-only"))
  293. chip->flags |= EE_READONLY;
  294. }
  295. return 0;
  296. }
  297. static int at25_probe(struct spi_device *spi)
  298. {
  299. struct at25_data *at25 = NULL;
  300. struct spi_eeprom chip;
  301. struct regmap *regmap;
  302. int err;
  303. int sr;
  304. int addrlen;
  305. /* Chip description */
  306. if (!spi->dev.platform_data) {
  307. err = at25_fw_to_chip(&spi->dev, &chip);
  308. if (err)
  309. return err;
  310. } else
  311. chip = *(struct spi_eeprom *)spi->dev.platform_data;
  312. /* For now we only support 8/16/24 bit addressing */
  313. if (chip.flags & EE_ADDR1)
  314. addrlen = 1;
  315. else if (chip.flags & EE_ADDR2)
  316. addrlen = 2;
  317. else if (chip.flags & EE_ADDR3)
  318. addrlen = 3;
  319. else {
  320. dev_dbg(&spi->dev, "unsupported address type\n");
  321. return -EINVAL;
  322. }
  323. /* Ping the chip ... the status register is pretty portable,
  324. * unlike probing manufacturer IDs. We do expect that system
  325. * firmware didn't write it in the past few milliseconds!
  326. */
  327. sr = spi_w8r8(spi, AT25_RDSR);
  328. if (sr < 0 || sr & AT25_SR_nRDY) {
  329. dev_dbg(&spi->dev, "rdsr --> %d (%02x)\n", sr, sr);
  330. return -ENXIO;
  331. }
  332. at25 = devm_kzalloc(&spi->dev, sizeof(struct at25_data), GFP_KERNEL);
  333. if (!at25)
  334. return -ENOMEM;
  335. mutex_init(&at25->lock);
  336. at25->chip = chip;
  337. at25->spi = spi_dev_get(spi);
  338. spi_set_drvdata(spi, at25);
  339. at25->addrlen = addrlen;
  340. at25->regmap_config.reg_bits = 32;
  341. at25->regmap_config.val_bits = 8;
  342. at25->regmap_config.reg_stride = 1;
  343. at25->regmap_config.max_register = chip.byte_len - 1;
  344. regmap = devm_regmap_init(&spi->dev, &at25_regmap_bus, at25,
  345. &at25->regmap_config);
  346. if (IS_ERR(regmap)) {
  347. dev_err(&spi->dev, "regmap init failed\n");
  348. return PTR_ERR(regmap);
  349. }
  350. at25->nvmem_config.name = dev_name(&spi->dev);
  351. at25->nvmem_config.dev = &spi->dev;
  352. at25->nvmem_config.read_only = chip.flags & EE_READONLY;
  353. at25->nvmem_config.root_only = true;
  354. at25->nvmem_config.owner = THIS_MODULE;
  355. at25->nvmem_config.compat = true;
  356. at25->nvmem_config.base_dev = &spi->dev;
  357. at25->nvmem = nvmem_register(&at25->nvmem_config);
  358. if (IS_ERR(at25->nvmem))
  359. return PTR_ERR(at25->nvmem);
  360. dev_info(&spi->dev, "%d %s %s eeprom%s, pagesize %u\n",
  361. (chip.byte_len < 1024)
  362. ? chip.byte_len
  363. : (chip.byte_len / 1024),
  364. (chip.byte_len < 1024) ? "Byte" : "KByte",
  365. at25->chip.name,
  366. (chip.flags & EE_READONLY) ? " (readonly)" : "",
  367. at25->chip.page_size);
  368. return 0;
  369. }
  370. static int at25_remove(struct spi_device *spi)
  371. {
  372. struct at25_data *at25;
  373. at25 = spi_get_drvdata(spi);
  374. nvmem_unregister(at25->nvmem);
  375. return 0;
  376. }
  377. /*-------------------------------------------------------------------------*/
  378. static const struct of_device_id at25_of_match[] = {
  379. { .compatible = "atmel,at25", },
  380. { }
  381. };
  382. MODULE_DEVICE_TABLE(of, at25_of_match);
  383. static struct spi_driver at25_driver = {
  384. .driver = {
  385. .name = "at25",
  386. .of_match_table = at25_of_match,
  387. },
  388. .probe = at25_probe,
  389. .remove = at25_remove,
  390. };
  391. module_spi_driver(at25_driver);
  392. MODULE_DESCRIPTION("Driver for most SPI EEPROMs");
  393. MODULE_AUTHOR("David Brownell");
  394. MODULE_LICENSE("GPL");
  395. MODULE_ALIAS("spi:at25");