at25.c 11 KB

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