eeprom_93xx46.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401
  1. /*
  2. * Driver for 93xx46 EEPROMs
  3. *
  4. * (C) 2011 DENX Software Engineering, Anatolij Gustschin <agust@denx.de>
  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 version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/delay.h>
  11. #include <linux/device.h>
  12. #include <linux/kernel.h>
  13. #include <linux/init.h>
  14. #include <linux/module.h>
  15. #include <linux/mutex.h>
  16. #include <linux/slab.h>
  17. #include <linux/spi/spi.h>
  18. #include <linux/sysfs.h>
  19. #include <linux/eeprom_93xx46.h>
  20. #define OP_START 0x4
  21. #define OP_WRITE (OP_START | 0x1)
  22. #define OP_READ (OP_START | 0x2)
  23. #define ADDR_EWDS 0x00
  24. #define ADDR_ERAL 0x20
  25. #define ADDR_EWEN 0x30
  26. struct eeprom_93xx46_dev {
  27. struct spi_device *spi;
  28. struct eeprom_93xx46_platform_data *pdata;
  29. struct bin_attribute bin;
  30. struct mutex lock;
  31. int addrlen;
  32. };
  33. static ssize_t
  34. eeprom_93xx46_bin_read(struct file *filp, struct kobject *kobj,
  35. struct bin_attribute *bin_attr,
  36. char *buf, loff_t off, size_t count)
  37. {
  38. struct eeprom_93xx46_dev *edev;
  39. struct device *dev;
  40. struct spi_message m;
  41. struct spi_transfer t[2];
  42. int bits, ret;
  43. u16 cmd_addr;
  44. dev = container_of(kobj, struct device, kobj);
  45. edev = dev_get_drvdata(dev);
  46. if (unlikely(off >= edev->bin.size))
  47. return 0;
  48. if ((off + count) > edev->bin.size)
  49. count = edev->bin.size - off;
  50. if (unlikely(!count))
  51. return count;
  52. cmd_addr = OP_READ << edev->addrlen;
  53. if (edev->addrlen == 7) {
  54. cmd_addr |= off & 0x7f;
  55. bits = 10;
  56. } else {
  57. cmd_addr |= off & 0x3f;
  58. bits = 9;
  59. }
  60. dev_dbg(&edev->spi->dev, "read cmd 0x%x, %d Hz\n",
  61. cmd_addr, edev->spi->max_speed_hz);
  62. spi_message_init(&m);
  63. memset(t, 0, sizeof(t));
  64. t[0].tx_buf = (char *)&cmd_addr;
  65. t[0].len = 2;
  66. t[0].bits_per_word = bits;
  67. spi_message_add_tail(&t[0], &m);
  68. t[1].rx_buf = buf;
  69. t[1].len = count;
  70. t[1].bits_per_word = 8;
  71. spi_message_add_tail(&t[1], &m);
  72. mutex_lock(&edev->lock);
  73. if (edev->pdata->prepare)
  74. edev->pdata->prepare(edev);
  75. ret = spi_sync(edev->spi, &m);
  76. /* have to wait at least Tcsl ns */
  77. ndelay(250);
  78. if (ret) {
  79. dev_err(&edev->spi->dev, "read %zu bytes at %d: err. %d\n",
  80. count, (int)off, ret);
  81. }
  82. if (edev->pdata->finish)
  83. edev->pdata->finish(edev);
  84. mutex_unlock(&edev->lock);
  85. return ret ? : count;
  86. }
  87. static int eeprom_93xx46_ew(struct eeprom_93xx46_dev *edev, int is_on)
  88. {
  89. struct spi_message m;
  90. struct spi_transfer t;
  91. int bits, ret;
  92. u16 cmd_addr;
  93. cmd_addr = OP_START << edev->addrlen;
  94. if (edev->addrlen == 7) {
  95. cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS) << 1;
  96. bits = 10;
  97. } else {
  98. cmd_addr |= (is_on ? ADDR_EWEN : ADDR_EWDS);
  99. bits = 9;
  100. }
  101. dev_dbg(&edev->spi->dev, "ew cmd 0x%04x\n", cmd_addr);
  102. spi_message_init(&m);
  103. memset(&t, 0, sizeof(t));
  104. t.tx_buf = &cmd_addr;
  105. t.len = 2;
  106. t.bits_per_word = bits;
  107. spi_message_add_tail(&t, &m);
  108. mutex_lock(&edev->lock);
  109. if (edev->pdata->prepare)
  110. edev->pdata->prepare(edev);
  111. ret = spi_sync(edev->spi, &m);
  112. /* have to wait at least Tcsl ns */
  113. ndelay(250);
  114. if (ret)
  115. dev_err(&edev->spi->dev, "erase/write %sable error %d\n",
  116. is_on ? "en" : "dis", ret);
  117. if (edev->pdata->finish)
  118. edev->pdata->finish(edev);
  119. mutex_unlock(&edev->lock);
  120. return ret;
  121. }
  122. static ssize_t
  123. eeprom_93xx46_write_word(struct eeprom_93xx46_dev *edev,
  124. const char *buf, unsigned off)
  125. {
  126. struct spi_message m;
  127. struct spi_transfer t[2];
  128. int bits, data_len, ret;
  129. u16 cmd_addr;
  130. cmd_addr = OP_WRITE << edev->addrlen;
  131. if (edev->addrlen == 7) {
  132. cmd_addr |= off & 0x7f;
  133. bits = 10;
  134. data_len = 1;
  135. } else {
  136. cmd_addr |= off & 0x3f;
  137. bits = 9;
  138. data_len = 2;
  139. }
  140. dev_dbg(&edev->spi->dev, "write cmd 0x%x\n", cmd_addr);
  141. spi_message_init(&m);
  142. memset(t, 0, sizeof(t));
  143. t[0].tx_buf = (char *)&cmd_addr;
  144. t[0].len = 2;
  145. t[0].bits_per_word = bits;
  146. spi_message_add_tail(&t[0], &m);
  147. t[1].tx_buf = buf;
  148. t[1].len = data_len;
  149. t[1].bits_per_word = 8;
  150. spi_message_add_tail(&t[1], &m);
  151. ret = spi_sync(edev->spi, &m);
  152. /* have to wait program cycle time Twc ms */
  153. mdelay(6);
  154. return ret;
  155. }
  156. static ssize_t
  157. eeprom_93xx46_bin_write(struct file *filp, struct kobject *kobj,
  158. struct bin_attribute *bin_attr,
  159. char *buf, loff_t off, size_t count)
  160. {
  161. struct eeprom_93xx46_dev *edev;
  162. struct device *dev;
  163. int i, ret, step = 1;
  164. dev = container_of(kobj, struct device, kobj);
  165. edev = dev_get_drvdata(dev);
  166. if (unlikely(off >= edev->bin.size))
  167. return 0;
  168. if ((off + count) > edev->bin.size)
  169. count = edev->bin.size - off;
  170. if (unlikely(!count))
  171. return count;
  172. /* only write even number of bytes on 16-bit devices */
  173. if (edev->addrlen == 6) {
  174. step = 2;
  175. count &= ~1;
  176. }
  177. /* erase/write enable */
  178. ret = eeprom_93xx46_ew(edev, 1);
  179. if (ret)
  180. return ret;
  181. mutex_lock(&edev->lock);
  182. if (edev->pdata->prepare)
  183. edev->pdata->prepare(edev);
  184. for (i = 0; i < count; i += step) {
  185. ret = eeprom_93xx46_write_word(edev, &buf[i], off + i);
  186. if (ret) {
  187. dev_err(&edev->spi->dev, "write failed at %d: %d\n",
  188. (int)off + i, ret);
  189. break;
  190. }
  191. }
  192. if (edev->pdata->finish)
  193. edev->pdata->finish(edev);
  194. mutex_unlock(&edev->lock);
  195. /* erase/write disable */
  196. eeprom_93xx46_ew(edev, 0);
  197. return ret ? : count;
  198. }
  199. static int eeprom_93xx46_eral(struct eeprom_93xx46_dev *edev)
  200. {
  201. struct eeprom_93xx46_platform_data *pd = edev->pdata;
  202. struct spi_message m;
  203. struct spi_transfer t;
  204. int bits, ret;
  205. u16 cmd_addr;
  206. cmd_addr = OP_START << edev->addrlen;
  207. if (edev->addrlen == 7) {
  208. cmd_addr |= ADDR_ERAL << 1;
  209. bits = 10;
  210. } else {
  211. cmd_addr |= ADDR_ERAL;
  212. bits = 9;
  213. }
  214. spi_message_init(&m);
  215. memset(&t, 0, sizeof(t));
  216. t.tx_buf = &cmd_addr;
  217. t.len = 2;
  218. t.bits_per_word = bits;
  219. spi_message_add_tail(&t, &m);
  220. mutex_lock(&edev->lock);
  221. if (edev->pdata->prepare)
  222. edev->pdata->prepare(edev);
  223. ret = spi_sync(edev->spi, &m);
  224. if (ret)
  225. dev_err(&edev->spi->dev, "erase error %d\n", ret);
  226. /* have to wait erase cycle time Tec ms */
  227. mdelay(6);
  228. if (pd->finish)
  229. pd->finish(edev);
  230. mutex_unlock(&edev->lock);
  231. return ret;
  232. }
  233. static ssize_t eeprom_93xx46_store_erase(struct device *dev,
  234. struct device_attribute *attr,
  235. const char *buf, size_t count)
  236. {
  237. struct eeprom_93xx46_dev *edev = dev_get_drvdata(dev);
  238. int erase = 0, ret;
  239. sscanf(buf, "%d", &erase);
  240. if (erase) {
  241. ret = eeprom_93xx46_ew(edev, 1);
  242. if (ret)
  243. return ret;
  244. ret = eeprom_93xx46_eral(edev);
  245. if (ret)
  246. return ret;
  247. ret = eeprom_93xx46_ew(edev, 0);
  248. if (ret)
  249. return ret;
  250. }
  251. return count;
  252. }
  253. static DEVICE_ATTR(erase, S_IWUSR, NULL, eeprom_93xx46_store_erase);
  254. static int __devinit eeprom_93xx46_probe(struct spi_device *spi)
  255. {
  256. struct eeprom_93xx46_platform_data *pd;
  257. struct eeprom_93xx46_dev *edev;
  258. int err;
  259. pd = spi->dev.platform_data;
  260. if (!pd) {
  261. dev_err(&spi->dev, "missing platform data\n");
  262. return -ENODEV;
  263. }
  264. edev = kzalloc(sizeof(*edev), GFP_KERNEL);
  265. if (!edev)
  266. return -ENOMEM;
  267. if (pd->flags & EE_ADDR8)
  268. edev->addrlen = 7;
  269. else if (pd->flags & EE_ADDR16)
  270. edev->addrlen = 6;
  271. else {
  272. dev_err(&spi->dev, "unspecified address type\n");
  273. err = -EINVAL;
  274. goto fail;
  275. }
  276. mutex_init(&edev->lock);
  277. edev->spi = spi_dev_get(spi);
  278. edev->pdata = pd;
  279. sysfs_bin_attr_init(&edev->bin);
  280. edev->bin.attr.name = "eeprom";
  281. edev->bin.attr.mode = S_IRUSR;
  282. edev->bin.read = eeprom_93xx46_bin_read;
  283. edev->bin.size = 128;
  284. if (!(pd->flags & EE_READONLY)) {
  285. edev->bin.write = eeprom_93xx46_bin_write;
  286. edev->bin.attr.mode |= S_IWUSR;
  287. }
  288. err = sysfs_create_bin_file(&spi->dev.kobj, &edev->bin);
  289. if (err)
  290. goto fail;
  291. dev_info(&spi->dev, "%d-bit eeprom %s\n",
  292. (pd->flags & EE_ADDR8) ? 8 : 16,
  293. (pd->flags & EE_READONLY) ? "(readonly)" : "");
  294. if (!(pd->flags & EE_READONLY)) {
  295. if (device_create_file(&spi->dev, &dev_attr_erase))
  296. dev_err(&spi->dev, "can't create erase interface\n");
  297. }
  298. dev_set_drvdata(&spi->dev, edev);
  299. return 0;
  300. fail:
  301. kfree(edev);
  302. return err;
  303. }
  304. static int __devexit eeprom_93xx46_remove(struct spi_device *spi)
  305. {
  306. struct eeprom_93xx46_dev *edev = dev_get_drvdata(&spi->dev);
  307. if (!(edev->pdata->flags & EE_READONLY))
  308. device_remove_file(&spi->dev, &dev_attr_erase);
  309. sysfs_remove_bin_file(&spi->dev.kobj, &edev->bin);
  310. dev_set_drvdata(&spi->dev, NULL);
  311. kfree(edev);
  312. return 0;
  313. }
  314. static struct spi_driver eeprom_93xx46_driver = {
  315. .driver = {
  316. .name = "93xx46",
  317. .owner = THIS_MODULE,
  318. },
  319. .probe = eeprom_93xx46_probe,
  320. .remove = __devexit_p(eeprom_93xx46_remove),
  321. };
  322. module_spi_driver(eeprom_93xx46_driver);
  323. MODULE_LICENSE("GPL");
  324. MODULE_DESCRIPTION("Driver for 93xx46 EEPROMs");
  325. MODULE_AUTHOR("Anatolij Gustschin <agust@denx.de>");
  326. MODULE_ALIAS("spi:93xx46");