at25.c 8.9 KB

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