at25.c 10 KB

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