ee1004.c 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245
  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_NUM_PAGES 2
  33. #define EE1004_PAGE_SIZE 256
  34. #define EE1004_PAGE_SHIFT 8
  35. #define EE1004_EEPROM_SIZE (EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
  36. /*
  37. * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
  38. * from page selection to end of read.
  39. */
  40. static DEFINE_MUTEX(ee1004_bus_lock);
  41. static struct i2c_client *ee1004_set_page[EE1004_NUM_PAGES];
  42. static unsigned int ee1004_dev_count;
  43. static int ee1004_current_page;
  44. static const struct i2c_device_id ee1004_ids[] = {
  45. { "ee1004", 0 },
  46. { }
  47. };
  48. MODULE_DEVICE_TABLE(i2c, ee1004_ids);
  49. /*-------------------------------------------------------------------------*/
  50. static int ee1004_get_current_page(void)
  51. {
  52. int err;
  53. err = i2c_smbus_read_byte(ee1004_set_page[0]);
  54. if (err == -ENXIO) {
  55. /* Nack means page 1 is selected */
  56. return 1;
  57. }
  58. if (err < 0) {
  59. /* Anything else is a real error, bail out */
  60. return err;
  61. }
  62. /* Ack means page 0 is selected, returned value meaningless */
  63. return 0;
  64. }
  65. static int ee1004_set_current_page(struct device *dev, int page)
  66. {
  67. int ret;
  68. if (page == ee1004_current_page)
  69. return 0;
  70. /* Data is ignored */
  71. ret = i2c_smbus_write_byte(ee1004_set_page[page], 0x00);
  72. /*
  73. * Don't give up just yet. Some memory modules will select the page
  74. * but not ack the command. Check which page is selected now.
  75. */
  76. if (ret == -ENXIO && ee1004_get_current_page() == page)
  77. ret = 0;
  78. if (ret < 0) {
  79. dev_err(dev, "Failed to select page %d (%d)\n", page, ret);
  80. return ret;
  81. }
  82. dev_dbg(dev, "Selected page %d\n", page);
  83. ee1004_current_page = page;
  84. return 0;
  85. }
  86. static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
  87. unsigned int offset, size_t count)
  88. {
  89. int status, page;
  90. page = offset >> EE1004_PAGE_SHIFT;
  91. offset &= (1 << EE1004_PAGE_SHIFT) - 1;
  92. status = ee1004_set_current_page(&client->dev, page);
  93. if (status)
  94. return status;
  95. /* Can't cross page boundaries */
  96. if (offset + count > EE1004_PAGE_SIZE)
  97. count = EE1004_PAGE_SIZE - offset;
  98. if (count > I2C_SMBUS_BLOCK_MAX)
  99. count = I2C_SMBUS_BLOCK_MAX;
  100. return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
  101. }
  102. static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
  103. struct bin_attribute *bin_attr,
  104. char *buf, loff_t off, size_t count)
  105. {
  106. struct i2c_client *client = kobj_to_i2c_client(kobj);
  107. size_t requested = count;
  108. int ret = 0;
  109. /*
  110. * Read data from chip, protecting against concurrent access to
  111. * other EE1004 SPD EEPROMs on the same adapter.
  112. */
  113. mutex_lock(&ee1004_bus_lock);
  114. while (count) {
  115. ret = ee1004_eeprom_read(client, buf, off, count);
  116. if (ret < 0)
  117. goto out;
  118. buf += ret;
  119. off += ret;
  120. count -= ret;
  121. }
  122. out:
  123. mutex_unlock(&ee1004_bus_lock);
  124. return ret < 0 ? ret : requested;
  125. }
  126. static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
  127. static struct bin_attribute *ee1004_attrs[] = {
  128. &bin_attr_eeprom,
  129. NULL
  130. };
  131. BIN_ATTRIBUTE_GROUPS(ee1004);
  132. static void ee1004_cleanup(int idx)
  133. {
  134. if (--ee1004_dev_count == 0)
  135. while (--idx >= 0) {
  136. i2c_unregister_device(ee1004_set_page[idx]);
  137. ee1004_set_page[idx] = NULL;
  138. }
  139. }
  140. static int ee1004_probe(struct i2c_client *client)
  141. {
  142. int err, cnr = 0;
  143. /* Make sure we can operate on this adapter */
  144. if (!i2c_check_functionality(client->adapter,
  145. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
  146. !i2c_check_functionality(client->adapter,
  147. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
  148. return -EPFNOSUPPORT;
  149. /* Use 2 dummy devices for page select command */
  150. mutex_lock(&ee1004_bus_lock);
  151. if (++ee1004_dev_count == 1) {
  152. for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
  153. struct i2c_client *cl;
  154. cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
  155. if (IS_ERR(cl)) {
  156. err = PTR_ERR(cl);
  157. goto err_clients;
  158. }
  159. ee1004_set_page[cnr] = cl;
  160. }
  161. /* Remember current page to avoid unneeded page select */
  162. err = ee1004_get_current_page();
  163. if (err < 0)
  164. goto err_clients;
  165. dev_dbg(&client->dev, "Currently selected page: %d\n", err);
  166. ee1004_current_page = err;
  167. } else if (client->adapter != ee1004_set_page[0]->adapter) {
  168. dev_err(&client->dev,
  169. "Driver only supports devices on a single I2C bus\n");
  170. err = -EOPNOTSUPP;
  171. goto err_clients;
  172. }
  173. mutex_unlock(&ee1004_bus_lock);
  174. dev_info(&client->dev,
  175. "%u byte EE1004-compliant SPD EEPROM, read-only\n",
  176. EE1004_EEPROM_SIZE);
  177. return 0;
  178. err_clients:
  179. ee1004_cleanup(cnr);
  180. mutex_unlock(&ee1004_bus_lock);
  181. return err;
  182. }
  183. static void ee1004_remove(struct i2c_client *client)
  184. {
  185. /* Remove page select clients if this is the last device */
  186. mutex_lock(&ee1004_bus_lock);
  187. ee1004_cleanup(EE1004_NUM_PAGES);
  188. mutex_unlock(&ee1004_bus_lock);
  189. }
  190. /*-------------------------------------------------------------------------*/
  191. static struct i2c_driver ee1004_driver = {
  192. .driver = {
  193. .name = "ee1004",
  194. .dev_groups = ee1004_groups,
  195. },
  196. .probe_new = ee1004_probe,
  197. .remove = ee1004_remove,
  198. .id_table = ee1004_ids,
  199. };
  200. module_i2c_driver(ee1004_driver);
  201. MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
  202. MODULE_AUTHOR("Jean Delvare");
  203. MODULE_LICENSE("GPL");