ee1004.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281
  1. /*
  2. * ee1004 - driver for DDR4 SPD EEPROMs
  3. *
  4. * Copyright (C) 2017 Jean Delvare
  5. *
  6. * Based on the at24 driver:
  7. * Copyright (C) 2005-2007 David Brownell
  8. * Copyright (C) 2008 Wolfram Sang, Pengutronix
  9. *
  10. * This program is free software; you can redistribute it and/or modify
  11. * it under the terms of the GNU General Public License as published by
  12. * the Free Software Foundation; either version 2 of the License, or
  13. * (at your option) any later version.
  14. */
  15. #include <linux/i2c.h>
  16. #include <linux/init.h>
  17. #include <linux/kernel.h>
  18. #include <linux/mod_devicetable.h>
  19. #include <linux/module.h>
  20. #include <linux/mutex.h>
  21. /*
  22. * DDR4 memory modules use special EEPROMs following the Jedec EE1004
  23. * specification. These are 512-byte EEPROMs using a single I2C address
  24. * in the 0x50-0x57 range for data. One of two 256-byte page is selected
  25. * by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
  26. *
  27. * Therefore we need to request these 2 additional addresses, and serialize
  28. * access to all such EEPROMs with a single mutex.
  29. *
  30. * We assume it is safe to read up to 32 bytes at once from these EEPROMs.
  31. * We use SMBus access even if I2C is available, these EEPROMs are small
  32. * enough, and reading from them infrequent enough, that we favor simplicity
  33. * over performance.
  34. */
  35. #define EE1004_ADDR_SET_PAGE 0x36
  36. #define EE1004_EEPROM_SIZE 512
  37. #define EE1004_PAGE_SIZE 256
  38. #define EE1004_PAGE_SHIFT 8
  39. /*
  40. * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
  41. * from page selection to end of read.
  42. */
  43. static DEFINE_MUTEX(ee1004_bus_lock);
  44. static struct i2c_client *ee1004_set_page[2];
  45. static unsigned int ee1004_dev_count;
  46. static int ee1004_current_page;
  47. static const struct i2c_device_id ee1004_ids[] = {
  48. { "ee1004", 0 },
  49. { }
  50. };
  51. MODULE_DEVICE_TABLE(i2c, ee1004_ids);
  52. /*-------------------------------------------------------------------------*/
  53. static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
  54. unsigned int offset, size_t count)
  55. {
  56. int status;
  57. if (count > I2C_SMBUS_BLOCK_MAX)
  58. count = I2C_SMBUS_BLOCK_MAX;
  59. /* Can't cross page boundaries */
  60. if (unlikely(offset + count > EE1004_PAGE_SIZE))
  61. count = EE1004_PAGE_SIZE - offset;
  62. status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
  63. count, buf);
  64. dev_dbg(&client->dev, "read %zu@%d --> %d\n", count, offset, status);
  65. return status;
  66. }
  67. static ssize_t ee1004_read(struct file *filp, struct kobject *kobj,
  68. struct bin_attribute *bin_attr,
  69. char *buf, loff_t off, size_t count)
  70. {
  71. struct device *dev = kobj_to_dev(kobj);
  72. struct i2c_client *client = to_i2c_client(dev);
  73. size_t requested = count;
  74. int page;
  75. if (unlikely(!count))
  76. return count;
  77. page = off >> EE1004_PAGE_SHIFT;
  78. if (unlikely(page > 1))
  79. return 0;
  80. off &= (1 << EE1004_PAGE_SHIFT) - 1;
  81. /*
  82. * Read data from chip, protecting against concurrent access to
  83. * other EE1004 SPD EEPROMs on the same adapter.
  84. */
  85. mutex_lock(&ee1004_bus_lock);
  86. while (count) {
  87. int status;
  88. /* Select page */
  89. if (page != ee1004_current_page) {
  90. /* Data is ignored */
  91. status = i2c_smbus_write_byte(ee1004_set_page[page],
  92. 0x00);
  93. if (status < 0) {
  94. dev_err(dev, "Failed to select page %d (%d)\n",
  95. page, status);
  96. mutex_unlock(&ee1004_bus_lock);
  97. return status;
  98. }
  99. dev_dbg(dev, "Selected page %d\n", page);
  100. ee1004_current_page = page;
  101. }
  102. status = ee1004_eeprom_read(client, buf, off, count);
  103. if (status < 0) {
  104. mutex_unlock(&ee1004_bus_lock);
  105. return status;
  106. }
  107. buf += status;
  108. off += status;
  109. count -= status;
  110. if (off == EE1004_PAGE_SIZE) {
  111. page++;
  112. off = 0;
  113. }
  114. }
  115. mutex_unlock(&ee1004_bus_lock);
  116. return requested;
  117. }
  118. static const struct bin_attribute eeprom_attr = {
  119. .attr = {
  120. .name = "eeprom",
  121. .mode = 0444,
  122. },
  123. .size = EE1004_EEPROM_SIZE,
  124. .read = ee1004_read,
  125. };
  126. static int ee1004_probe(struct i2c_client *client,
  127. const struct i2c_device_id *id)
  128. {
  129. int err, cnr = 0;
  130. const char *slow = NULL;
  131. /* Make sure we can operate on this adapter */
  132. if (!i2c_check_functionality(client->adapter,
  133. I2C_FUNC_SMBUS_READ_BYTE |
  134. I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
  135. if (i2c_check_functionality(client->adapter,
  136. I2C_FUNC_SMBUS_READ_BYTE |
  137. I2C_FUNC_SMBUS_READ_WORD_DATA))
  138. slow = "word";
  139. else if (i2c_check_functionality(client->adapter,
  140. I2C_FUNC_SMBUS_READ_BYTE |
  141. I2C_FUNC_SMBUS_READ_BYTE_DATA))
  142. slow = "byte";
  143. else
  144. return -EPFNOSUPPORT;
  145. }
  146. /* Use 2 dummy devices for page select command */
  147. mutex_lock(&ee1004_bus_lock);
  148. if (++ee1004_dev_count == 1) {
  149. for (cnr = 0; cnr < 2; cnr++) {
  150. ee1004_set_page[cnr] = i2c_new_dummy(client->adapter,
  151. EE1004_ADDR_SET_PAGE + cnr);
  152. if (!ee1004_set_page[cnr]) {
  153. dev_err(&client->dev,
  154. "address 0x%02x unavailable\n",
  155. EE1004_ADDR_SET_PAGE + cnr);
  156. err = -EADDRINUSE;
  157. goto err_clients;
  158. }
  159. }
  160. } else if (i2c_adapter_id(client->adapter) !=
  161. i2c_adapter_id(ee1004_set_page[0]->adapter)) {
  162. dev_err(&client->dev,
  163. "Driver only supports devices on a single I2C bus\n");
  164. err = -EOPNOTSUPP;
  165. goto err_clients;
  166. }
  167. /* Remember current page to avoid unneeded page select */
  168. err = i2c_smbus_read_byte(ee1004_set_page[0]);
  169. if (err == -ENXIO) {
  170. /* Nack means page 1 is selected */
  171. ee1004_current_page = 1;
  172. } else if (err < 0) {
  173. /* Anything else is a real error, bail out */
  174. goto err_clients;
  175. } else {
  176. /* Ack means page 0 is selected, returned value meaningless */
  177. ee1004_current_page = 0;
  178. }
  179. dev_dbg(&client->dev, "Currently selected page: %d\n",
  180. ee1004_current_page);
  181. mutex_unlock(&ee1004_bus_lock);
  182. /* Create the sysfs eeprom file */
  183. err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
  184. if (err)
  185. goto err_clients_lock;
  186. dev_info(&client->dev,
  187. "%u byte EE1004-compliant SPD EEPROM, read-only\n",
  188. EE1004_EEPROM_SIZE);
  189. if (slow)
  190. dev_notice(&client->dev,
  191. "Falling back to %s reads, performance will suffer\n",
  192. slow);
  193. return 0;
  194. err_clients_lock:
  195. mutex_lock(&ee1004_bus_lock);
  196. err_clients:
  197. if (--ee1004_dev_count == 0) {
  198. for (cnr--; cnr >= 0; cnr--) {
  199. i2c_unregister_device(ee1004_set_page[cnr]);
  200. ee1004_set_page[cnr] = NULL;
  201. }
  202. }
  203. mutex_unlock(&ee1004_bus_lock);
  204. return err;
  205. }
  206. static int ee1004_remove(struct i2c_client *client)
  207. {
  208. int i;
  209. sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
  210. /* Remove page select clients if this is the last device */
  211. mutex_lock(&ee1004_bus_lock);
  212. if (--ee1004_dev_count == 0) {
  213. for (i = 0; i < 2; i++) {
  214. i2c_unregister_device(ee1004_set_page[i]);
  215. ee1004_set_page[i] = NULL;
  216. }
  217. }
  218. mutex_unlock(&ee1004_bus_lock);
  219. return 0;
  220. }
  221. /*-------------------------------------------------------------------------*/
  222. static struct i2c_driver ee1004_driver = {
  223. .driver = {
  224. .name = "ee1004",
  225. },
  226. .probe = ee1004_probe,
  227. .remove = ee1004_remove,
  228. .id_table = ee1004_ids,
  229. };
  230. static int __init ee1004_init(void)
  231. {
  232. return i2c_add_driver(&ee1004_driver);
  233. }
  234. module_init(ee1004_init);
  235. static void __exit ee1004_exit(void)
  236. {
  237. i2c_del_driver(&ee1004_driver);
  238. }
  239. module_exit(ee1004_exit);
  240. MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
  241. MODULE_AUTHOR("Jean Delvare");
  242. MODULE_LICENSE("GPL");