1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * ee1004 - driver for DDR4 SPD EEPROMs
5 * Copyright (C) 2017 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 ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
57 unsigned int offset, size_t count)
61 if (count > I2C_SMBUS_BLOCK_MAX)
62 count = I2C_SMBUS_BLOCK_MAX;
63 /* Can't cross page boundaries */
64 if (unlikely(offset + count > EE1004_PAGE_SIZE))
65 count = EE1004_PAGE_SIZE - offset;
67 status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
69 dev_dbg(&client->dev, "read %zu@%d --> %d\n", count, offset, status);
74 static ssize_t ee1004_read(struct file *filp, struct kobject *kobj,
75 struct bin_attribute *bin_attr,
76 char *buf, loff_t off, size_t count)
78 struct device *dev = kobj_to_dev(kobj);
79 struct i2c_client *client = to_i2c_client(dev);
80 size_t requested = count;
86 page = off >> EE1004_PAGE_SHIFT;
87 if (unlikely(page > 1))
89 off &= (1 << EE1004_PAGE_SHIFT) - 1;
92 * Read data from chip, protecting against concurrent access to
93 * other EE1004 SPD EEPROMs on the same adapter.
95 mutex_lock(&ee1004_bus_lock);
101 if (page != ee1004_current_page) {
102 /* Data is ignored */
103 status = i2c_smbus_write_byte(ee1004_set_page[page],
106 dev_err(dev, "Failed to select page %d (%d)\n",
108 mutex_unlock(&ee1004_bus_lock);
111 dev_dbg(dev, "Selected page %d\n", page);
112 ee1004_current_page = page;
115 status = ee1004_eeprom_read(client, buf, off, count);
117 mutex_unlock(&ee1004_bus_lock);
124 if (off == EE1004_PAGE_SIZE) {
130 mutex_unlock(&ee1004_bus_lock);
135 static const struct bin_attribute eeprom_attr = {
140 .size = EE1004_EEPROM_SIZE,
144 static int ee1004_probe(struct i2c_client *client,
145 const struct i2c_device_id *id)
148 const char *slow = NULL;
150 /* Make sure we can operate on this adapter */
151 if (!i2c_check_functionality(client->adapter,
152 I2C_FUNC_SMBUS_READ_BYTE |
153 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
154 if (i2c_check_functionality(client->adapter,
155 I2C_FUNC_SMBUS_READ_BYTE |
156 I2C_FUNC_SMBUS_READ_WORD_DATA))
158 else if (i2c_check_functionality(client->adapter,
159 I2C_FUNC_SMBUS_READ_BYTE |
160 I2C_FUNC_SMBUS_READ_BYTE_DATA))
163 return -EPFNOSUPPORT;
166 /* Use 2 dummy devices for page select command */
167 mutex_lock(&ee1004_bus_lock);
168 if (++ee1004_dev_count == 1) {
169 for (cnr = 0; cnr < 2; cnr++) {
170 ee1004_set_page[cnr] = i2c_new_dummy(client->adapter,
171 EE1004_ADDR_SET_PAGE + cnr);
172 if (!ee1004_set_page[cnr]) {
173 dev_err(&client->dev,
174 "address 0x%02x unavailable\n",
175 EE1004_ADDR_SET_PAGE + cnr);
180 } else if (i2c_adapter_id(client->adapter) !=
181 i2c_adapter_id(ee1004_set_page[0]->adapter)) {
182 dev_err(&client->dev,
183 "Driver only supports devices on a single I2C bus\n");
188 /* Remember current page to avoid unneeded page select */
189 err = i2c_smbus_read_byte(ee1004_set_page[0]);
191 /* Nack means page 1 is selected */
192 ee1004_current_page = 1;
193 } else if (err < 0) {
194 /* Anything else is a real error, bail out */
197 /* Ack means page 0 is selected, returned value meaningless */
198 ee1004_current_page = 0;
200 dev_dbg(&client->dev, "Currently selected page: %d\n",
201 ee1004_current_page);
202 mutex_unlock(&ee1004_bus_lock);
204 /* Create the sysfs eeprom file */
205 err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
207 goto err_clients_lock;
209 dev_info(&client->dev,
210 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
213 dev_notice(&client->dev,
214 "Falling back to %s reads, performance will suffer\n",
220 mutex_lock(&ee1004_bus_lock);
222 if (--ee1004_dev_count == 0) {
223 for (cnr--; cnr >= 0; cnr--) {
224 i2c_unregister_device(ee1004_set_page[cnr]);
225 ee1004_set_page[cnr] = NULL;
228 mutex_unlock(&ee1004_bus_lock);
233 static int ee1004_remove(struct i2c_client *client)
237 sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
239 /* Remove page select clients if this is the last device */
240 mutex_lock(&ee1004_bus_lock);
241 if (--ee1004_dev_count == 0) {
242 for (i = 0; i < 2; i++) {
243 i2c_unregister_device(ee1004_set_page[i]);
244 ee1004_set_page[i] = NULL;
247 mutex_unlock(&ee1004_bus_lock);
252 /*-------------------------------------------------------------------------*/
254 static struct i2c_driver ee1004_driver = {
258 .probe = ee1004_probe,
259 .remove = ee1004_remove,
260 .id_table = ee1004_ids,
263 static int __init ee1004_init(void)
265 return i2c_add_driver(&ee1004_driver);
267 module_init(ee1004_init);
269 static void __exit ee1004_exit(void)
271 i2c_del_driver(&ee1004_driver);
273 module_exit(ee1004_exit);
275 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
276 MODULE_AUTHOR("Jean Delvare");
277 MODULE_LICENSE("GPL");