at25.c 10 KB

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