ee1004.c 7.2 KB

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