ee1004.c 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352
  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/device.h>
  12. #include <linux/i2c.h>
  13. #include <linux/init.h>
  14. #include <linux/kernel.h>
  15. #include <linux/mod_devicetable.h>
  16. #include <linux/module.h>
  17. #include <linux/mutex.h>
  18. #include <linux/nvmem-provider.h>
  19. /*
  20. * DDR4 memory modules use special EEPROMs following the Jedec EE1004
  21. * specification. These are 512-byte EEPROMs using a single I2C address
  22. * in the 0x50-0x57 range for data. One of two 256-byte page is selected
  23. * by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
  24. *
  25. * Therefore we need to request these 2 additional addresses, and serialize
  26. * access to all such EEPROMs with a single mutex.
  27. *
  28. * We assume it is safe to read up to 32 bytes at once from these EEPROMs.
  29. * We use SMBus access even if I2C is available, these EEPROMs are small
  30. * enough, and reading from them infrequent enough, that we favor simplicity
  31. * over performance.
  32. */
  33. #define EE1004_MAX_BUSSES 8
  34. #define EE1004_ADDR_SET_PAGE 0x36
  35. #define EE1004_NUM_PAGES 2
  36. #define EE1004_PAGE_SIZE 256
  37. #define EE1004_PAGE_SHIFT 8
  38. #define EE1004_EEPROM_SIZE (EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
  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 ee1004_bus_data {
  45. struct i2c_adapter *adap;
  46. struct i2c_client *set_page[EE1004_NUM_PAGES];
  47. unsigned int dev_count;
  48. int current_page;
  49. } ee1004_bus_data[EE1004_MAX_BUSSES];
  50. static const struct i2c_device_id ee1004_ids[] = {
  51. { "ee1004" },
  52. { }
  53. };
  54. MODULE_DEVICE_TABLE(i2c, ee1004_ids);
  55. /*-------------------------------------------------------------------------*/
  56. static struct ee1004_bus_data *ee1004_get_bus_data(struct i2c_adapter *adap)
  57. {
  58. int i;
  59. for (i = 0; i < EE1004_MAX_BUSSES; i++)
  60. if (ee1004_bus_data[i].adap == adap)
  61. return ee1004_bus_data + i;
  62. /* If not existent yet, create new entry */
  63. for (i = 0; i < EE1004_MAX_BUSSES; i++)
  64. if (!ee1004_bus_data[i].adap) {
  65. ee1004_bus_data[i].adap = adap;
  66. return ee1004_bus_data + i;
  67. }
  68. return NULL;
  69. }
  70. static int ee1004_get_current_page(struct ee1004_bus_data *bd)
  71. {
  72. int err;
  73. err = i2c_smbus_read_byte(bd->set_page[0]);
  74. if (err == -ENXIO) {
  75. /* Nack means page 1 is selected */
  76. return 1;
  77. }
  78. if (err < 0) {
  79. /* Anything else is a real error, bail out */
  80. return err;
  81. }
  82. /* Ack means page 0 is selected, returned value meaningless */
  83. return 0;
  84. }
  85. static int ee1004_set_current_page(struct i2c_client *client, int page)
  86. {
  87. struct ee1004_bus_data *bd = i2c_get_clientdata(client);
  88. int ret;
  89. if (page == bd->current_page)
  90. return 0;
  91. /* Data is ignored */
  92. ret = i2c_smbus_write_byte(bd->set_page[page], 0x00);
  93. /*
  94. * Don't give up just yet. Some memory modules will select the page
  95. * but not ack the command. Check which page is selected now.
  96. */
  97. if (ret == -ENXIO && ee1004_get_current_page(bd) == page)
  98. ret = 0;
  99. if (ret < 0) {
  100. dev_err(&client->dev, "Failed to select page %d (%d)\n", page, ret);
  101. return ret;
  102. }
  103. dev_dbg(&client->dev, "Selected page %d\n", page);
  104. bd->current_page = page;
  105. return 0;
  106. }
  107. static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
  108. unsigned int offset, size_t count)
  109. {
  110. int status, page;
  111. page = offset >> EE1004_PAGE_SHIFT;
  112. offset &= (1 << EE1004_PAGE_SHIFT) - 1;
  113. status = ee1004_set_current_page(client, page);
  114. if (status)
  115. return status;
  116. /* Can't cross page boundaries */
  117. if (offset + count > EE1004_PAGE_SIZE)
  118. count = EE1004_PAGE_SIZE - offset;
  119. if (count > I2C_SMBUS_BLOCK_MAX)
  120. count = I2C_SMBUS_BLOCK_MAX;
  121. return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
  122. }
  123. static int ee1004_read(void *priv, unsigned int off, void *val, size_t count)
  124. {
  125. struct i2c_client *client = priv;
  126. char *buf = val;
  127. int ret;
  128. if (unlikely(!count))
  129. return count;
  130. if (off + count > EE1004_EEPROM_SIZE)
  131. return -EINVAL;
  132. /*
  133. * Read data from chip, protecting against concurrent access to
  134. * other EE1004 SPD EEPROMs on the same adapter.
  135. */
  136. mutex_lock(&ee1004_bus_lock);
  137. while (count) {
  138. ret = ee1004_eeprom_read(client, buf, off, count);
  139. if (ret < 0) {
  140. mutex_unlock(&ee1004_bus_lock);
  141. return ret;
  142. }
  143. buf += ret;
  144. off += ret;
  145. count -= ret;
  146. }
  147. mutex_unlock(&ee1004_bus_lock);
  148. return 0;
  149. }
  150. static void ee1004_probe_temp_sensor(struct i2c_client *client)
  151. {
  152. struct i2c_board_info info = { .type = "jc42" };
  153. unsigned short addr = 0x18 | (client->addr & 7);
  154. unsigned short addr_list[] = { addr, I2C_CLIENT_END };
  155. u8 data[2];
  156. int ret;
  157. /* byte 14, bit 7 is set if temp sensor is present */
  158. ret = ee1004_eeprom_read(client, data, 14, 1);
  159. if (ret != 1)
  160. return;
  161. if (!(data[0] & BIT(7))) {
  162. /*
  163. * If the SPD data suggests that there is no temperature
  164. * sensor, it may still be there for SPD revision 1.0.
  165. * See SPD Annex L, Revision 1 and 2, for details.
  166. * Check DIMM type and SPD revision; if it is a DDR4
  167. * with SPD revision 1.0, check the thermal sensor address
  168. * and instantiate the jc42 driver if a chip is found at
  169. * that address.
  170. * It is not necessary to check if there is a chip at the
  171. * temperature sensor address since i2c_new_scanned_device()
  172. * will do that and return silently if no chip is found.
  173. */
  174. ret = ee1004_eeprom_read(client, data, 1, 2);
  175. if (ret != 2 || data[0] != 0x10 || data[1] != 0x0c)
  176. return;
  177. }
  178. i2c_new_scanned_device(client->adapter, &info, addr_list, NULL);
  179. }
  180. static void ee1004_cleanup(int idx, struct ee1004_bus_data *bd)
  181. {
  182. if (--bd->dev_count == 0) {
  183. while (--idx >= 0)
  184. i2c_unregister_device(bd->set_page[idx]);
  185. memset(bd, 0, sizeof(struct ee1004_bus_data));
  186. }
  187. }
  188. static void ee1004_cleanup_bus_data(void *data)
  189. {
  190. struct ee1004_bus_data *bd = data;
  191. /* Remove page select clients if this is the last device */
  192. mutex_lock(&ee1004_bus_lock);
  193. ee1004_cleanup(EE1004_NUM_PAGES, bd);
  194. mutex_unlock(&ee1004_bus_lock);
  195. }
  196. static int ee1004_init_bus_data(struct i2c_client *client)
  197. {
  198. struct ee1004_bus_data *bd;
  199. int err, cnr = 0;
  200. bd = ee1004_get_bus_data(client->adapter);
  201. if (!bd)
  202. return dev_err_probe(&client->dev, -ENOSPC, "Only %d busses supported",
  203. EE1004_MAX_BUSSES);
  204. i2c_set_clientdata(client, bd);
  205. if (++bd->dev_count == 1) {
  206. /* Use 2 dummy devices for page select command */
  207. for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
  208. struct i2c_client *cl;
  209. cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
  210. if (IS_ERR(cl)) {
  211. err = PTR_ERR(cl);
  212. goto err_out;
  213. }
  214. bd->set_page[cnr] = cl;
  215. }
  216. /* Remember current page to avoid unneeded page select */
  217. err = ee1004_get_current_page(bd);
  218. if (err < 0)
  219. goto err_out;
  220. dev_dbg(&client->dev, "Currently selected page: %d\n", err);
  221. bd->current_page = err;
  222. }
  223. return 0;
  224. err_out:
  225. ee1004_cleanup(cnr, bd);
  226. return err;
  227. }
  228. static int ee1004_probe(struct i2c_client *client)
  229. {
  230. struct nvmem_config config = {
  231. .dev = &client->dev,
  232. .name = dev_name(&client->dev),
  233. .id = NVMEM_DEVID_NONE,
  234. .owner = THIS_MODULE,
  235. .type = NVMEM_TYPE_EEPROM,
  236. .read_only = true,
  237. .root_only = false,
  238. .reg_read = ee1004_read,
  239. .size = EE1004_EEPROM_SIZE,
  240. .word_size = 1,
  241. .stride = 1,
  242. .priv = client,
  243. .compat = true,
  244. .base_dev = &client->dev,
  245. };
  246. struct nvmem_device *ndev;
  247. int err;
  248. /* Make sure we can operate on this adapter */
  249. if (!i2c_check_functionality(client->adapter,
  250. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
  251. !i2c_check_functionality(client->adapter,
  252. I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
  253. return -EPFNOSUPPORT;
  254. err = i2c_smbus_read_byte(client);
  255. if (err < 0)
  256. return -ENODEV;
  257. mutex_lock(&ee1004_bus_lock);
  258. err = ee1004_init_bus_data(client);
  259. if (err < 0) {
  260. mutex_unlock(&ee1004_bus_lock);
  261. return err;
  262. }
  263. ee1004_probe_temp_sensor(client);
  264. mutex_unlock(&ee1004_bus_lock);
  265. err = devm_add_action_or_reset(&client->dev, ee1004_cleanup_bus_data,
  266. i2c_get_clientdata(client));
  267. if (err < 0)
  268. return err;
  269. ndev = devm_nvmem_register(&client->dev, &config);
  270. if (IS_ERR(ndev))
  271. return PTR_ERR(ndev);
  272. dev_info(&client->dev,
  273. "%u byte EE1004-compliant SPD EEPROM, read-only\n",
  274. EE1004_EEPROM_SIZE);
  275. return 0;
  276. }
  277. /*-------------------------------------------------------------------------*/
  278. static struct i2c_driver ee1004_driver = {
  279. .driver = {
  280. .name = "ee1004",
  281. },
  282. .probe = ee1004_probe,
  283. .id_table = ee1004_ids,
  284. };
  285. module_i2c_driver(ee1004_driver);
  286. MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
  287. MODULE_AUTHOR("Jean Delvare");
  288. MODULE_LICENSE("GPL");