at25.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488
  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/init.h>
  13. #include <linux/module.h>
  14. #include <linux/slab.h>
  15. #include <linux/delay.h>
  16. #include <linux/device.h>
  17. #include <linux/sched.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/eeprom.h>
  20. #include <linux/of.h>
  21. /*
  22. * NOTE: this is an *EEPROM* driver. The vagaries of product naming
  23. * mean that some AT25 products are EEPROMs, and others are FLASH.
  24. * Handle FLASH chips with the drivers/mtd/devices/m25p80.c driver,
  25. * not this one!
  26. */
  27. struct at25_data {
  28. struct spi_device *spi;
  29. struct memory_accessor mem;
  30. struct mutex lock;
  31. struct spi_eeprom chip;
  32. struct bin_attribute bin;
  33. unsigned addrlen;
  34. };
  35. #define AT25_WREN 0x06 /* latch the write enable */
  36. #define AT25_WRDI 0x04 /* reset the write enable */
  37. #define AT25_RDSR 0x05 /* read status register */
  38. #define AT25_WRSR 0x01 /* write status register */
  39. #define AT25_READ 0x03 /* read byte(s) */
  40. #define AT25_WRITE 0x02 /* write byte(s)/sector */
  41. #define AT25_SR_nRDY 0x01 /* nRDY = write-in-progress */
  42. #define AT25_SR_WEN 0x02 /* write enable (latched) */
  43. #define AT25_SR_BP0 0x04 /* BP for software writeprotect */
  44. #define AT25_SR_BP1 0x08
  45. #define AT25_SR_WPEN 0x80 /* writeprotect enable */
  46. #define AT25_INSTR_BIT3 0x08 /* Additional address bit in instr */
  47. #define EE_MAXADDRLEN 3 /* 24 bit addresses, up to 2 MBytes */
  48. /* Specs often allow 5 msec for a page write, sometimes 20 msec;
  49. * it's important to recover from write timeouts.
  50. */
  51. #define EE_TIMEOUT 25
  52. /*-------------------------------------------------------------------------*/
  53. #define io_limit PAGE_SIZE /* bytes */
  54. static ssize_t
  55. at25_ee_read(
  56. struct at25_data *at25,
  57. char *buf,
  58. unsigned offset,
  59. size_t count
  60. )
  61. {
  62. u8 command[EE_MAXADDRLEN + 1];
  63. u8 *cp;
  64. ssize_t status;
  65. struct spi_transfer t[2];
  66. struct spi_message m;
  67. u8 instr;
  68. if (unlikely(offset >= at25->bin.size))
  69. return 0;
  70. if ((offset + count) > at25->bin.size)
  71. count = at25->bin.size - offset;
  72. if (unlikely(!count))
  73. return count;
  74. cp = command;
  75. instr = AT25_READ;
  76. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  77. if (offset >= (1U << (at25->addrlen * 8)))
  78. instr |= AT25_INSTR_BIT3;
  79. *cp++ = instr;
  80. /* 8/16/24-bit address is written MSB first */
  81. switch (at25->addrlen) {
  82. default: /* case 3 */
  83. *cp++ = offset >> 16;
  84. case 2:
  85. *cp++ = offset >> 8;
  86. case 1:
  87. case 0: /* can't happen: for better codegen */
  88. *cp++ = offset >> 0;
  89. }
  90. spi_message_init(&m);
  91. memset(t, 0, sizeof t);
  92. t[0].tx_buf = command;
  93. t[0].len = at25->addrlen + 1;
  94. spi_message_add_tail(&t[0], &m);
  95. t[1].rx_buf = buf;
  96. t[1].len = count;
  97. spi_message_add_tail(&t[1], &m);
  98. mutex_lock(&at25->lock);
  99. /* Read it all at once.
  100. *
  101. * REVISIT that's potentially a problem with large chips, if
  102. * other devices on the bus need to be accessed regularly or
  103. * this chip is clocked very slowly
  104. */
  105. status = spi_sync(at25->spi, &m);
  106. dev_dbg(&at25->spi->dev,
  107. "read %Zd bytes at %d --> %d\n",
  108. count, offset, (int) status);
  109. mutex_unlock(&at25->lock);
  110. return status ? status : count;
  111. }
  112. static ssize_t
  113. at25_bin_read(struct file *filp, struct kobject *kobj,
  114. struct bin_attribute *bin_attr,
  115. char *buf, loff_t off, size_t count)
  116. {
  117. struct device *dev;
  118. struct at25_data *at25;
  119. dev = container_of(kobj, struct device, kobj);
  120. at25 = dev_get_drvdata(dev);
  121. return at25_ee_read(at25, buf, off, count);
  122. }
  123. static ssize_t
  124. at25_ee_write(struct at25_data *at25, const char *buf, loff_t off,
  125. size_t count)
  126. {
  127. ssize_t status = 0;
  128. unsigned written = 0;
  129. unsigned buf_size;
  130. u8 *bounce;
  131. if (unlikely(off >= at25->bin.size))
  132. return -EFBIG;
  133. if ((off + count) > at25->bin.size)
  134. count = at25->bin.size - off;
  135. if (unlikely(!count))
  136. return count;
  137. /* Temp buffer starts with command and address */
  138. buf_size = at25->chip.page_size;
  139. if (buf_size > io_limit)
  140. buf_size = io_limit;
  141. bounce = kmalloc(buf_size + at25->addrlen + 1, GFP_KERNEL);
  142. if (!bounce)
  143. return -ENOMEM;
  144. /* For write, rollover is within the page ... so we write at
  145. * most one page, then manually roll over to the next page.
  146. */
  147. mutex_lock(&at25->lock);
  148. do {
  149. unsigned long timeout, retries;
  150. unsigned segment;
  151. unsigned offset = (unsigned) off;
  152. u8 *cp = bounce;
  153. int sr;
  154. u8 instr;
  155. *cp = AT25_WREN;
  156. status = spi_write(at25->spi, cp, 1);
  157. if (status < 0) {
  158. dev_dbg(&at25->spi->dev, "WREN --> %d\n",
  159. (int) status);
  160. break;
  161. }
  162. instr = AT25_WRITE;
  163. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  164. if (offset >= (1U << (at25->addrlen * 8)))
  165. instr |= AT25_INSTR_BIT3;
  166. *cp++ = instr;
  167. /* 8/16/24-bit address is written MSB first */
  168. switch (at25->addrlen) {
  169. default: /* case 3 */
  170. *cp++ = offset >> 16;
  171. case 2:
  172. *cp++ = offset >> 8;
  173. case 1:
  174. case 0: /* can't happen: for better codegen */
  175. *cp++ = offset >> 0;
  176. }
  177. /* Write as much of a page as we can */
  178. segment = buf_size - (offset % buf_size);
  179. if (segment > count)
  180. segment = count;
  181. memcpy(cp, buf, segment);
  182. status = spi_write(at25->spi, bounce,
  183. segment + at25->addrlen + 1);
  184. dev_dbg(&at25->spi->dev,
  185. "write %u bytes at %u --> %d\n",
  186. segment, offset, (int) status);
  187. if (status < 0)
  188. break;
  189. /* REVISIT this should detect (or prevent) failed writes
  190. * to readonly sections of the EEPROM...
  191. */
  192. /* Wait for non-busy status */
  193. timeout = jiffies + msecs_to_jiffies(EE_TIMEOUT);
  194. retries = 0;
  195. do {
  196. sr = spi_w8r8(at25->spi, AT25_RDSR);
  197. if (sr < 0 || (sr & AT25_SR_nRDY)) {
  198. dev_dbg(&at25->spi->dev,
  199. "rdsr --> %d (%02x)\n", sr, sr);
  200. /* at HZ=100, this is sloooow */
  201. msleep(1);
  202. continue;
  203. }
  204. if (!(sr & AT25_SR_nRDY))
  205. break;
  206. } while (retries++ < 3 || time_before_eq(jiffies, timeout));
  207. if ((sr < 0) || (sr & AT25_SR_nRDY)) {
  208. dev_err(&at25->spi->dev,
  209. "write %d bytes offset %d, "
  210. "timeout after %u msecs\n",
  211. segment, offset,
  212. jiffies_to_msecs(jiffies -
  213. (timeout - EE_TIMEOUT)));
  214. status = -ETIMEDOUT;
  215. break;
  216. }
  217. off += segment;
  218. buf += segment;
  219. count -= segment;
  220. written += segment;
  221. } while (count > 0);
  222. mutex_unlock(&at25->lock);
  223. kfree(bounce);
  224. return written ? written : status;
  225. }
  226. static ssize_t
  227. at25_bin_write(struct file *filp, struct kobject *kobj,
  228. struct bin_attribute *bin_attr,
  229. char *buf, loff_t off, size_t count)
  230. {
  231. struct device *dev;
  232. struct at25_data *at25;
  233. dev = container_of(kobj, struct device, kobj);
  234. at25 = dev_get_drvdata(dev);
  235. return at25_ee_write(at25, buf, off, count);
  236. }
  237. /*-------------------------------------------------------------------------*/
  238. /* Let in-kernel code access the eeprom data. */
  239. static ssize_t at25_mem_read(struct memory_accessor *mem, char *buf,
  240. off_t offset, size_t count)
  241. {
  242. struct at25_data *at25 = container_of(mem, struct at25_data, mem);
  243. return at25_ee_read(at25, buf, offset, count);
  244. }
  245. static ssize_t at25_mem_write(struct memory_accessor *mem, const char *buf,
  246. off_t offset, size_t count)
  247. {
  248. struct at25_data *at25 = container_of(mem, struct at25_data, mem);
  249. return at25_ee_write(at25, buf, offset, count);
  250. }
  251. /*-------------------------------------------------------------------------*/
  252. static int at25_np_to_chip(struct device *dev,
  253. struct device_node *np,
  254. struct spi_eeprom *chip)
  255. {
  256. u32 val;
  257. memset(chip, 0, sizeof(*chip));
  258. strncpy(chip->name, np->name, sizeof(chip->name));
  259. if (of_property_read_u32(np, "size", &val) == 0 ||
  260. of_property_read_u32(np, "at25,byte-len", &val) == 0) {
  261. chip->byte_len = val;
  262. } else {
  263. dev_err(dev, "Error: missing \"size\" property\n");
  264. return -ENODEV;
  265. }
  266. if (of_property_read_u32(np, "pagesize", &val) == 0 ||
  267. of_property_read_u32(np, "at25,page-size", &val) == 0) {
  268. chip->page_size = (u16)val;
  269. } else {
  270. dev_err(dev, "Error: missing \"pagesize\" property\n");
  271. return -ENODEV;
  272. }
  273. if (of_property_read_u32(np, "at25,addr-mode", &val) == 0) {
  274. chip->flags = (u16)val;
  275. } else {
  276. if (of_property_read_u32(np, "address-width", &val)) {
  277. dev_err(dev,
  278. "Error: missing \"address-width\" property\n");
  279. return -ENODEV;
  280. }
  281. switch (val) {
  282. case 8:
  283. chip->flags |= EE_ADDR1;
  284. break;
  285. case 16:
  286. chip->flags |= EE_ADDR2;
  287. break;
  288. case 24:
  289. chip->flags |= EE_ADDR3;
  290. break;
  291. default:
  292. dev_err(dev,
  293. "Error: bad \"address-width\" property: %u\n",
  294. val);
  295. return -ENODEV;
  296. }
  297. if (of_find_property(np, "read-only", NULL))
  298. chip->flags |= EE_READONLY;
  299. }
  300. return 0;
  301. }
  302. static int at25_probe(struct spi_device *spi)
  303. {
  304. struct at25_data *at25 = NULL;
  305. struct spi_eeprom chip;
  306. struct device_node *np = spi->dev.of_node;
  307. int err;
  308. int sr;
  309. int addrlen;
  310. /* Chip description */
  311. if (!spi->dev.platform_data) {
  312. if (np) {
  313. err = at25_np_to_chip(&spi->dev, np, &chip);
  314. if (err)
  315. goto fail;
  316. } else {
  317. dev_err(&spi->dev, "Error: no chip description\n");
  318. err = -ENODEV;
  319. goto fail;
  320. }
  321. } else
  322. chip = *(struct spi_eeprom *)spi->dev.platform_data;
  323. /* For now we only support 8/16/24 bit addressing */
  324. if (chip.flags & EE_ADDR1)
  325. addrlen = 1;
  326. else if (chip.flags & EE_ADDR2)
  327. addrlen = 2;
  328. else if (chip.flags & EE_ADDR3)
  329. addrlen = 3;
  330. else {
  331. dev_dbg(&spi->dev, "unsupported address type\n");
  332. err = -EINVAL;
  333. goto fail;
  334. }
  335. /* Ping the chip ... the status register is pretty portable,
  336. * unlike probing manufacturer IDs. We do expect that system
  337. * firmware didn't write it in the past few milliseconds!
  338. */
  339. sr = spi_w8r8(spi, AT25_RDSR);
  340. if (sr < 0 || sr & AT25_SR_nRDY) {
  341. dev_dbg(&spi->dev, "rdsr --> %d (%02x)\n", sr, sr);
  342. err = -ENXIO;
  343. goto fail;
  344. }
  345. if (!(at25 = kzalloc(sizeof *at25, GFP_KERNEL))) {
  346. err = -ENOMEM;
  347. goto fail;
  348. }
  349. mutex_init(&at25->lock);
  350. at25->chip = chip;
  351. at25->spi = spi_dev_get(spi);
  352. dev_set_drvdata(&spi->dev, at25);
  353. at25->addrlen = addrlen;
  354. /* Export the EEPROM bytes through sysfs, since that's convenient.
  355. * And maybe to other kernel code; it might hold a board's Ethernet
  356. * address, or board-specific calibration data generated on the
  357. * manufacturing floor.
  358. *
  359. * Default to root-only access to the data; EEPROMs often hold data
  360. * that's sensitive for read and/or write, like ethernet addresses,
  361. * security codes, board-specific manufacturing calibrations, etc.
  362. */
  363. sysfs_bin_attr_init(&at25->bin);
  364. at25->bin.attr.name = "eeprom";
  365. at25->bin.attr.mode = S_IRUSR;
  366. at25->bin.read = at25_bin_read;
  367. at25->mem.read = at25_mem_read;
  368. at25->bin.size = at25->chip.byte_len;
  369. if (!(chip.flags & EE_READONLY)) {
  370. at25->bin.write = at25_bin_write;
  371. at25->bin.attr.mode |= S_IWUSR;
  372. at25->mem.write = at25_mem_write;
  373. }
  374. err = sysfs_create_bin_file(&spi->dev.kobj, &at25->bin);
  375. if (err)
  376. goto fail;
  377. if (chip.setup)
  378. chip.setup(&at25->mem, chip.context);
  379. dev_info(&spi->dev, "%Zd %s %s eeprom%s, pagesize %u\n",
  380. (at25->bin.size < 1024)
  381. ? at25->bin.size
  382. : (at25->bin.size / 1024),
  383. (at25->bin.size < 1024) ? "Byte" : "KByte",
  384. at25->chip.name,
  385. (chip.flags & EE_READONLY) ? " (readonly)" : "",
  386. at25->chip.page_size);
  387. return 0;
  388. fail:
  389. dev_dbg(&spi->dev, "probe err %d\n", err);
  390. kfree(at25);
  391. return err;
  392. }
  393. static int at25_remove(struct spi_device *spi)
  394. {
  395. struct at25_data *at25;
  396. at25 = dev_get_drvdata(&spi->dev);
  397. sysfs_remove_bin_file(&spi->dev.kobj, &at25->bin);
  398. kfree(at25);
  399. return 0;
  400. }
  401. /*-------------------------------------------------------------------------*/
  402. static struct spi_driver at25_driver = {
  403. .driver = {
  404. .name = "at25",
  405. .owner = THIS_MODULE,
  406. },
  407. .probe = at25_probe,
  408. .remove = at25_remove,
  409. };
  410. module_spi_driver(at25_driver);
  411. MODULE_DESCRIPTION("Driver for most SPI EEPROMs");
  412. MODULE_AUTHOR("David Brownell");
  413. MODULE_LICENSE("GPL");
  414. MODULE_ALIAS("spi:at25");