1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ee1004 - driver for DDR4 SPD EEPROMs
5 * Copyright (C) 2017-2019 Jean Delvare
7 * Based on the at24 driver:
8 * Copyright (C) 2005-2007 David Brownell
9 * Copyright (C) 2008 Wolfram Sang, Pengutronix
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>
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.
25 * Therefore we need to request these 2 additional addresses, and serialize
26 * access to all such EEPROMs with a single mutex.
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
34 #define EE1004_ADDR_SET_PAGE 0x36
35 #define EE1004_EEPROM_SIZE 512
36 #define EE1004_PAGE_SIZE 256
37 #define EE1004_PAGE_SHIFT 8
40 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
41 * from page selection to end of read.
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;
48 static const struct i2c_device_id ee1004_ids[] = {
52 MODULE_DEVICE_TABLE(i2c, ee1004_ids);
54 /*-------------------------------------------------------------------------*/
56 static int ee1004_get_current_page(void)
60 err = i2c_smbus_read_byte(ee1004_set_page[0]);
62 /* Nack means page 1 is selected */
66 /* Anything else is a real error, bail out */
70 /* Ack means page 0 is selected, returned value meaningless */
74 static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
75 unsigned int offset, size_t count)
79 if (count > I2C_SMBUS_BLOCK_MAX)
80 count = I2C_SMBUS_BLOCK_MAX;
81 /* Can't cross page boundaries */
82 if (unlikely(offset + count > EE1004_PAGE_SIZE))
83 count = EE1004_PAGE_SIZE - offset;
85 if (count > I2C_SMBUS_BLOCK_MAX)
86 count = I2C_SMBUS_BLOCK_MAX;
88 status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
90 dev_dbg(&client->dev, "read %zu@%d --> %d\n", count, offset, status);
95 static ssize_t ee1004_read(struct file *filp, struct kobject *kobj,
96 struct bin_attribute *bin_attr,
97 char *buf, loff_t off, size_t count)
99 struct device *dev = kobj_to_dev(kobj);
100 struct i2c_client *client = to_i2c_client(dev);
101 size_t requested = count;
104 if (unlikely(!count))
107 page = off >> EE1004_PAGE_SHIFT;
108 if (unlikely(page > 1))
110 off &= (1 << EE1004_PAGE_SHIFT) - 1;
113 * Read data from chip, protecting against concurrent access to
114 * other EE1004 SPD EEPROMs on the same adapter.
116 mutex_lock(&ee1004_bus_lock);
122 if (page != ee1004_current_page) {
123 /* Data is ignored */
124 status = i2c_smbus_write_byte(ee1004_set_page[page],
126 if (status == -ENXIO) {
128 * Don't give up just yet. Some memory
129 * modules will select the page but not
130 * ack the command. Check which page is
133 if (ee1004_get_current_page() == page)
137 dev_err(dev, "Failed to select page %d (%d)\n",
139 mutex_unlock(&ee1004_bus_lock);
142 dev_dbg(dev, "Selected page %d\n", page);
143 ee1004_current_page = page;
146 status = ee1004_eeprom_read(client, buf, off, count);
148 mutex_unlock(&ee1004_bus_lock);
155 if (off == EE1004_PAGE_SIZE) {
161 mutex_unlock(&ee1004_bus_lock);
166 static const struct bin_attribute eeprom_attr = {
171 .size = EE1004_EEPROM_SIZE,
175 static int ee1004_probe(struct i2c_client *client,
176 const struct i2c_device_id *id)
179 const char *slow = NULL;
181 /* Make sure we can operate on this adapter */
182 if (!i2c_check_functionality(client->adapter,
183 I2C_FUNC_SMBUS_READ_BYTE |
184 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
185 if (i2c_check_functionality(client->adapter,
186 I2C_FUNC_SMBUS_READ_BYTE |
187 I2C_FUNC_SMBUS_READ_WORD_DATA))
189 else if (i2c_check_functionality(client->adapter,
190 I2C_FUNC_SMBUS_READ_BYTE |
191 I2C_FUNC_SMBUS_READ_BYTE_DATA))
194 return -EPFNOSUPPORT;
197 /* Use 2 dummy devices for page select command */
198 mutex_lock(&ee1004_bus_lock);
199 if (++ee1004_dev_count == 1) {
200 for (cnr = 0; cnr < 2; cnr++) {
201 ee1004_set_page[cnr] = i2c_new_dummy_device(client->adapter,
202 EE1004_ADDR_SET_PAGE + cnr);
203 if (IS_ERR(ee1004_set_page[cnr])) {
204 dev_err(&client->dev,
205 "address 0x%02x unavailable\n",
206 EE1004_ADDR_SET_PAGE + cnr);
207 err = PTR_ERR(ee1004_set_page[cnr]);
211 } else if (i2c_adapter_id(client->adapter) !=
212 i2c_adapter_id(ee1004_set_page[0]->adapter)) {
213 dev_err(&client->dev,
214 "Driver only supports devices on a single I2C bus\n");
219 /* Remember current page to avoid unneeded page select */
220 err = ee1004_get_current_page();
223 ee1004_current_page = err;
224 dev_dbg(&client->dev, "Currently selected page: %d\n",
225 ee1004_current_page);
226 mutex_unlock(&ee1004_bus_lock);
228 /* Create the sysfs eeprom file */
229 err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
231 goto err_clients_lock;
233 dev_info(&client->dev,
234 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
237 dev_notice(&client->dev,
238 "Falling back to %s reads, performance will suffer\n",
244 mutex_lock(&ee1004_bus_lock);
246 if (--ee1004_dev_count == 0) {
247 for (cnr--; cnr >= 0; cnr--) {
248 i2c_unregister_device(ee1004_set_page[cnr]);
249 ee1004_set_page[cnr] = NULL;
252 mutex_unlock(&ee1004_bus_lock);
257 static int ee1004_remove(struct i2c_client *client)
261 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
263 /* Remove page select clients if this is the last device */
264 mutex_lock(&ee1004_bus_lock);
265 if (--ee1004_dev_count == 0) {
266 for (i = 0; i < 2; i++) {
267 i2c_unregister_device(ee1004_set_page[i]);
268 ee1004_set_page[i] = NULL;
271 mutex_unlock(&ee1004_bus_lock);
276 /*-------------------------------------------------------------------------*/
278 static struct i2c_driver ee1004_driver = {
282 .probe = ee1004_probe,
283 .remove = ee1004_remove,
284 .id_table = ee1004_ids,
286 module_i2c_driver(ee1004_driver);
288 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
289 MODULE_AUTHOR("Jean Delvare");
290 MODULE_LICENSE("GPL");