at25.c 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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, struct bin_attribute *bin_attr,
  94. char *buf, loff_t off, size_t count)
  95. {
  96. struct device *dev;
  97. struct at25_data *at25;
  98. dev = container_of(kobj, struct device, kobj);
  99. at25 = dev_get_drvdata(dev);
  100. if (unlikely(off >= at25->bin.size))
  101. return 0;
  102. if ((off + count) > at25->bin.size)
  103. count = at25->bin.size - off;
  104. if (unlikely(!count))
  105. return count;
  106. return at25_ee_read(at25, buf, off, count);
  107. }
  108. static ssize_t
  109. at25_ee_write(struct at25_data *at25, char *buf, loff_t off, size_t count)
  110. {
  111. ssize_t status = 0;
  112. unsigned written = 0;
  113. unsigned buf_size;
  114. u8 *bounce;
  115. /* Temp buffer starts with command and address */
  116. buf_size = at25->chip.page_size;
  117. if (buf_size > io_limit)
  118. buf_size = io_limit;
  119. bounce = kmalloc(buf_size + at25->addrlen + 1, GFP_KERNEL);
  120. if (!bounce)
  121. return -ENOMEM;
  122. /* For write, rollover is within the page ... so we write at
  123. * most one page, then manually roll over to the next page.
  124. */
  125. bounce[0] = AT25_WRITE;
  126. mutex_lock(&at25->lock);
  127. do {
  128. unsigned long timeout, retries;
  129. unsigned segment;
  130. unsigned offset = (unsigned) off;
  131. u8 *cp = bounce + 1;
  132. *cp = AT25_WREN;
  133. status = spi_write(at25->spi, cp, 1);
  134. if (status < 0) {
  135. dev_dbg(&at25->spi->dev, "WREN --> %d\n",
  136. (int) status);
  137. break;
  138. }
  139. /* 8/16/24-bit address is written MSB first */
  140. switch (at25->addrlen) {
  141. default: /* case 3 */
  142. *cp++ = offset >> 16;
  143. case 2:
  144. *cp++ = offset >> 8;
  145. case 1:
  146. case 0: /* can't happen: for better codegen */
  147. *cp++ = offset >> 0;
  148. }
  149. /* Write as much of a page as we can */
  150. segment = buf_size - (offset % buf_size);
  151. if (segment > count)
  152. segment = count;
  153. memcpy(cp, buf, segment);
  154. status = spi_write(at25->spi, bounce,
  155. segment + at25->addrlen + 1);
  156. dev_dbg(&at25->spi->dev,
  157. "write %u bytes at %u --> %d\n",
  158. segment, offset, (int) status);
  159. if (status < 0)
  160. break;
  161. /* REVISIT this should detect (or prevent) failed writes
  162. * to readonly sections of the EEPROM...
  163. */
  164. /* Wait for non-busy status */
  165. timeout = jiffies + msecs_to_jiffies(EE_TIMEOUT);
  166. retries = 0;
  167. do {
  168. int sr;
  169. sr = spi_w8r8(at25->spi, AT25_RDSR);
  170. if (sr < 0 || (sr & AT25_SR_nRDY)) {
  171. dev_dbg(&at25->spi->dev,
  172. "rdsr --> %d (%02x)\n", sr, sr);
  173. /* at HZ=100, this is sloooow */
  174. msleep(1);
  175. continue;
  176. }
  177. if (!(sr & AT25_SR_nRDY))
  178. break;
  179. } while (retries++ < 3 || time_before_eq(jiffies, timeout));
  180. if (time_after(jiffies, timeout)) {
  181. dev_err(&at25->spi->dev,
  182. "write %d bytes offset %d, "
  183. "timeout after %u msecs\n",
  184. segment, offset,
  185. jiffies_to_msecs(jiffies -
  186. (timeout - EE_TIMEOUT)));
  187. status = -ETIMEDOUT;
  188. break;
  189. }
  190. off += segment;
  191. buf += segment;
  192. count -= segment;
  193. written += segment;
  194. } while (count > 0);
  195. mutex_unlock(&at25->lock);
  196. kfree(bounce);
  197. return written ? written : status;
  198. }
  199. static ssize_t
  200. at25_bin_write(struct kobject *kobj, struct bin_attribute *bin_attr,
  201. char *buf, loff_t off, size_t count)
  202. {
  203. struct device *dev;
  204. struct at25_data *at25;
  205. dev = container_of(kobj, struct device, kobj);
  206. at25 = dev_get_drvdata(dev);
  207. if (unlikely(off >= at25->bin.size))
  208. return -EFBIG;
  209. if ((off + count) > at25->bin.size)
  210. count = at25->bin.size - off;
  211. if (unlikely(!count))
  212. return count;
  213. return at25_ee_write(at25, buf, off, count);
  214. }
  215. /*-------------------------------------------------------------------------*/
  216. static int at25_probe(struct spi_device *spi)
  217. {
  218. struct at25_data *at25 = NULL;
  219. const struct spi_eeprom *chip;
  220. int err;
  221. int sr;
  222. int addrlen;
  223. /* Chip description */
  224. chip = spi->dev.platform_data;
  225. if (!chip) {
  226. dev_dbg(&spi->dev, "no chip description\n");
  227. err = -ENODEV;
  228. goto fail;
  229. }
  230. /* For now we only support 8/16/24 bit addressing */
  231. if (chip->flags & EE_ADDR1)
  232. addrlen = 1;
  233. else if (chip->flags & EE_ADDR2)
  234. addrlen = 2;
  235. else if (chip->flags & EE_ADDR3)
  236. addrlen = 3;
  237. else {
  238. dev_dbg(&spi->dev, "unsupported address type\n");
  239. err = -EINVAL;
  240. goto fail;
  241. }
  242. /* Ping the chip ... the status register is pretty portable,
  243. * unlike probing manufacturer IDs. We do expect that system
  244. * firmware didn't write it in the past few milliseconds!
  245. */
  246. sr = spi_w8r8(spi, AT25_RDSR);
  247. if (sr < 0 || sr & AT25_SR_nRDY) {
  248. dev_dbg(&spi->dev, "rdsr --> %d (%02x)\n", sr, sr);
  249. err = -ENXIO;
  250. goto fail;
  251. }
  252. if (!(at25 = kzalloc(sizeof *at25, GFP_KERNEL))) {
  253. err = -ENOMEM;
  254. goto fail;
  255. }
  256. mutex_init(&at25->lock);
  257. at25->chip = *chip;
  258. at25->spi = spi_dev_get(spi);
  259. dev_set_drvdata(&spi->dev, at25);
  260. at25->addrlen = addrlen;
  261. /* Export the EEPROM bytes through sysfs, since that's convenient.
  262. * Default to root-only access to the data; EEPROMs often hold data
  263. * that's sensitive for read and/or write, like ethernet addresses,
  264. * security codes, board-specific manufacturing calibrations, etc.
  265. */
  266. at25->bin.attr.name = "eeprom";
  267. at25->bin.attr.mode = S_IRUSR;
  268. at25->bin.read = at25_bin_read;
  269. at25->bin.size = at25->chip.byte_len;
  270. if (!(chip->flags & EE_READONLY)) {
  271. at25->bin.write = at25_bin_write;
  272. at25->bin.attr.mode |= S_IWUSR;
  273. }
  274. err = sysfs_create_bin_file(&spi->dev.kobj, &at25->bin);
  275. if (err)
  276. goto fail;
  277. dev_info(&spi->dev, "%Zd %s %s eeprom%s, pagesize %u\n",
  278. (at25->bin.size < 1024)
  279. ? at25->bin.size
  280. : (at25->bin.size / 1024),
  281. (at25->bin.size < 1024) ? "Byte" : "KByte",
  282. at25->chip.name,
  283. (chip->flags & EE_READONLY) ? " (readonly)" : "",
  284. at25->chip.page_size);
  285. return 0;
  286. fail:
  287. dev_dbg(&spi->dev, "probe err %d\n", err);
  288. kfree(at25);
  289. return err;
  290. }
  291. static int __devexit at25_remove(struct spi_device *spi)
  292. {
  293. struct at25_data *at25;
  294. at25 = dev_get_drvdata(&spi->dev);
  295. sysfs_remove_bin_file(&spi->dev.kobj, &at25->bin);
  296. kfree(at25);
  297. return 0;
  298. }
  299. /*-------------------------------------------------------------------------*/
  300. static struct spi_driver at25_driver = {
  301. .driver = {
  302. .name = "at25",
  303. .owner = THIS_MODULE,
  304. },
  305. .probe = at25_probe,
  306. .remove = __devexit_p(at25_remove),
  307. };
  308. static int __init at25_init(void)
  309. {
  310. return spi_register_driver(&at25_driver);
  311. }
  312. module_init(at25_init);
  313. static void __exit at25_exit(void)
  314. {
  315. spi_unregister_driver(&at25_driver);
  316. }
  317. module_exit(at25_exit);
  318. MODULE_DESCRIPTION("Driver for most SPI EEPROMs");
  319. MODULE_AUTHOR("David Brownell");
  320. MODULE_LICENSE("GPL");