at25.c 9.1 KB

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