at25.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415
  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/nvmem-provider.h>
  18. #include <linux/spi/spi.h>
  19. #include <linux/spi/eeprom.h>
  20. #include <linux/property.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 mutex lock;
  30. struct spi_eeprom chip;
  31. unsigned addrlen;
  32. struct nvmem_config nvmem_config;
  33. struct nvmem_device *nvmem;
  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 int at25_ee_read(void *priv, unsigned int offset,
  55. void *val, size_t count)
  56. {
  57. struct at25_data *at25 = priv;
  58. char *buf = val;
  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. u8 instr;
  65. if (unlikely(offset >= at25->chip.byte_len))
  66. return -EINVAL;
  67. if ((offset + count) > at25->chip.byte_len)
  68. count = at25->chip.byte_len - offset;
  69. if (unlikely(!count))
  70. return -EINVAL;
  71. cp = command;
  72. instr = AT25_READ;
  73. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  74. if (offset >= (1U << (at25->addrlen * 8)))
  75. instr |= AT25_INSTR_BIT3;
  76. *cp++ = instr;
  77. /* 8/16/24-bit address is written MSB first */
  78. switch (at25->addrlen) {
  79. default: /* case 3 */
  80. *cp++ = offset >> 16;
  81. /* fall through */
  82. case 2:
  83. *cp++ = offset >> 8;
  84. /* fall through */
  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, "read %zu bytes at %d --> %zd\n",
  106. count, offset, status);
  107. mutex_unlock(&at25->lock);
  108. return status;
  109. }
  110. static int at25_ee_write(void *priv, unsigned int off, void *val, size_t count)
  111. {
  112. struct at25_data *at25 = priv;
  113. const char *buf = val;
  114. int status = 0;
  115. unsigned buf_size;
  116. u8 *bounce;
  117. if (unlikely(off >= at25->chip.byte_len))
  118. return -EFBIG;
  119. if ((off + count) > at25->chip.byte_len)
  120. count = at25->chip.byte_len - off;
  121. if (unlikely(!count))
  122. return -EINVAL;
  123. /* Temp buffer starts with command and address */
  124. buf_size = at25->chip.page_size;
  125. if (buf_size > io_limit)
  126. buf_size = io_limit;
  127. bounce = kmalloc(buf_size + at25->addrlen + 1, GFP_KERNEL);
  128. if (!bounce)
  129. return -ENOMEM;
  130. /* For write, rollover is within the page ... so we write at
  131. * most one page, then manually roll over to the next page.
  132. */
  133. mutex_lock(&at25->lock);
  134. do {
  135. unsigned long timeout, retries;
  136. unsigned segment;
  137. unsigned offset = (unsigned) off;
  138. u8 *cp = bounce;
  139. int sr;
  140. u8 instr;
  141. *cp = AT25_WREN;
  142. status = spi_write(at25->spi, cp, 1);
  143. if (status < 0) {
  144. dev_dbg(&at25->spi->dev, "WREN --> %d\n", status);
  145. break;
  146. }
  147. instr = AT25_WRITE;
  148. if (at25->chip.flags & EE_INSTR_BIT3_IS_ADDR)
  149. if (offset >= (1U << (at25->addrlen * 8)))
  150. instr |= AT25_INSTR_BIT3;
  151. *cp++ = instr;
  152. /* 8/16/24-bit address is written MSB first */
  153. switch (at25->addrlen) {
  154. default: /* case 3 */
  155. *cp++ = offset >> 16;
  156. /* fall through */
  157. case 2:
  158. *cp++ = offset >> 8;
  159. /* fall through */
  160. case 1:
  161. case 0: /* can't happen: for better codegen */
  162. *cp++ = offset >> 0;
  163. }
  164. /* Write as much of a page as we can */
  165. segment = buf_size - (offset % buf_size);
  166. if (segment > count)
  167. segment = count;
  168. memcpy(cp, buf, segment);
  169. status = spi_write(at25->spi, bounce,
  170. segment + at25->addrlen + 1);
  171. dev_dbg(&at25->spi->dev, "write %u bytes at %u --> %d\n",
  172. segment, offset, status);
  173. if (status < 0)
  174. break;
  175. /* REVISIT this should detect (or prevent) failed writes
  176. * to readonly sections of the EEPROM...
  177. */
  178. /* Wait for non-busy status */
  179. timeout = jiffies + msecs_to_jiffies(EE_TIMEOUT);
  180. retries = 0;
  181. do {
  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 ((sr < 0) || (sr & AT25_SR_nRDY)) {
  194. dev_err(&at25->spi->dev,
  195. "write %u bytes offset %u, timeout after %u msecs\n",
  196. segment, offset,
  197. jiffies_to_msecs(jiffies -
  198. (timeout - EE_TIMEOUT)));
  199. status = -ETIMEDOUT;
  200. break;
  201. }
  202. off += segment;
  203. buf += segment;
  204. count -= segment;
  205. } while (count > 0);
  206. mutex_unlock(&at25->lock);
  207. kfree(bounce);
  208. return status;
  209. }
  210. /*-------------------------------------------------------------------------*/
  211. static int at25_fw_to_chip(struct device *dev, struct spi_eeprom *chip)
  212. {
  213. u32 val;
  214. memset(chip, 0, sizeof(*chip));
  215. strncpy(chip->name, "at25", sizeof(chip->name));
  216. if (device_property_read_u32(dev, "size", &val) == 0 ||
  217. device_property_read_u32(dev, "at25,byte-len", &val) == 0) {
  218. chip->byte_len = val;
  219. } else {
  220. dev_err(dev, "Error: missing \"size\" property\n");
  221. return -ENODEV;
  222. }
  223. if (device_property_read_u32(dev, "pagesize", &val) == 0 ||
  224. device_property_read_u32(dev, "at25,page-size", &val) == 0) {
  225. chip->page_size = (u16)val;
  226. } else {
  227. dev_err(dev, "Error: missing \"pagesize\" property\n");
  228. return -ENODEV;
  229. }
  230. if (device_property_read_u32(dev, "at25,addr-mode", &val) == 0) {
  231. chip->flags = (u16)val;
  232. } else {
  233. if (device_property_read_u32(dev, "address-width", &val)) {
  234. dev_err(dev,
  235. "Error: missing \"address-width\" property\n");
  236. return -ENODEV;
  237. }
  238. switch (val) {
  239. case 9:
  240. chip->flags |= EE_INSTR_BIT3_IS_ADDR;
  241. /* fall through */
  242. case 8:
  243. chip->flags |= EE_ADDR1;
  244. break;
  245. case 16:
  246. chip->flags |= EE_ADDR2;
  247. break;
  248. case 24:
  249. chip->flags |= EE_ADDR3;
  250. break;
  251. default:
  252. dev_err(dev,
  253. "Error: bad \"address-width\" property: %u\n",
  254. val);
  255. return -ENODEV;
  256. }
  257. if (device_property_present(dev, "read-only"))
  258. chip->flags |= EE_READONLY;
  259. }
  260. return 0;
  261. }
  262. static int at25_probe(struct spi_device *spi)
  263. {
  264. struct at25_data *at25 = NULL;
  265. struct spi_eeprom chip;
  266. int err;
  267. int sr;
  268. int addrlen;
  269. /* Chip description */
  270. if (!spi->dev.platform_data) {
  271. err = at25_fw_to_chip(&spi->dev, &chip);
  272. if (err)
  273. return err;
  274. } else
  275. chip = *(struct spi_eeprom *)spi->dev.platform_data;
  276. /* For now we only support 8/16/24 bit addressing */
  277. if (chip.flags & EE_ADDR1)
  278. addrlen = 1;
  279. else if (chip.flags & EE_ADDR2)
  280. addrlen = 2;
  281. else if (chip.flags & EE_ADDR3)
  282. addrlen = 3;
  283. else {
  284. dev_dbg(&spi->dev, "unsupported address type\n");
  285. return -EINVAL;
  286. }
  287. /* Ping the chip ... the status register is pretty portable,
  288. * unlike probing manufacturer IDs. We do expect that system
  289. * firmware didn't write it in the past few milliseconds!
  290. */
  291. sr = spi_w8r8(spi, AT25_RDSR);
  292. if (sr < 0 || sr & AT25_SR_nRDY) {
  293. dev_dbg(&spi->dev, "rdsr --> %d (%02x)\n", sr, sr);
  294. return -ENXIO;
  295. }
  296. at25 = devm_kzalloc(&spi->dev, sizeof(struct at25_data), GFP_KERNEL);
  297. if (!at25)
  298. return -ENOMEM;
  299. mutex_init(&at25->lock);
  300. at25->chip = chip;
  301. at25->spi = spi;
  302. spi_set_drvdata(spi, at25);
  303. at25->addrlen = addrlen;
  304. at25->nvmem_config.name = dev_name(&spi->dev);
  305. at25->nvmem_config.dev = &spi->dev;
  306. at25->nvmem_config.read_only = chip.flags & EE_READONLY;
  307. at25->nvmem_config.root_only = true;
  308. at25->nvmem_config.owner = THIS_MODULE;
  309. at25->nvmem_config.compat = true;
  310. at25->nvmem_config.base_dev = &spi->dev;
  311. at25->nvmem_config.reg_read = at25_ee_read;
  312. at25->nvmem_config.reg_write = at25_ee_write;
  313. at25->nvmem_config.priv = at25;
  314. at25->nvmem_config.stride = 4;
  315. at25->nvmem_config.word_size = 1;
  316. at25->nvmem_config.size = chip.byte_len;
  317. at25->nvmem = nvmem_register(&at25->nvmem_config);
  318. if (IS_ERR(at25->nvmem))
  319. return PTR_ERR(at25->nvmem);
  320. dev_info(&spi->dev, "%d %s %s eeprom%s, pagesize %u\n",
  321. (chip.byte_len < 1024) ? chip.byte_len : (chip.byte_len / 1024),
  322. (chip.byte_len < 1024) ? "Byte" : "KByte",
  323. at25->chip.name,
  324. (chip.flags & EE_READONLY) ? " (readonly)" : "",
  325. at25->chip.page_size);
  326. return 0;
  327. }
  328. static int at25_remove(struct spi_device *spi)
  329. {
  330. struct at25_data *at25;
  331. at25 = spi_get_drvdata(spi);
  332. nvmem_unregister(at25->nvmem);
  333. return 0;
  334. }
  335. /*-------------------------------------------------------------------------*/
  336. static const struct of_device_id at25_of_match[] = {
  337. { .compatible = "atmel,at25", },
  338. { }
  339. };
  340. MODULE_DEVICE_TABLE(of, at25_of_match);
  341. static struct spi_driver at25_driver = {
  342. .driver = {
  343. .name = "at25",
  344. .of_match_table = at25_of_match,
  345. },
  346. .probe = at25_probe,
  347. .remove = at25_remove,
  348. };
  349. module_spi_driver(at25_driver);
  350. MODULE_DESCRIPTION("Driver for most SPI EEPROMs");
  351. MODULE_AUTHOR("David Brownell");
  352. MODULE_LICENSE("GPL");
  353. MODULE_ALIAS("spi:at25");