at25.c 12 KB

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