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 status
= i2c_smbus_read_i2c_block_data_or_emulated(client
, offset
,
87 dev_dbg(&client
->dev
, "read %zu@%d --> %d\n", count
, offset
, status
);
92 static ssize_t
ee1004_read(struct file
*filp
, struct kobject
*kobj
,
93 struct bin_attribute
*bin_attr
,
94 char *buf
, loff_t off
, size_t count
)
96 struct device
*dev
= kobj_to_dev(kobj
);
97 struct i2c_client
*client
= to_i2c_client(dev
);
98 size_t requested
= count
;
101 if (unlikely(!count
))
104 page
= off
>> EE1004_PAGE_SHIFT
;
105 if (unlikely(page
> 1))
107 off
&= (1 << EE1004_PAGE_SHIFT
) - 1;
110 * Read data from chip, protecting against concurrent access to
111 * other EE1004 SPD EEPROMs on the same adapter.
113 mutex_lock(&ee1004_bus_lock
);
119 if (page
!= ee1004_current_page
) {
120 /* Data is ignored */
121 status
= i2c_smbus_write_byte(ee1004_set_page
[page
],
123 if (status
== -ENXIO
) {
125 * Don't give up just yet. Some memory
126 * modules will select the page but not
127 * ack the command. Check which page is
130 if (ee1004_get_current_page() == page
)
134 dev_err(dev
, "Failed to select page %d (%d)\n",
136 mutex_unlock(&ee1004_bus_lock
);
139 dev_dbg(dev
, "Selected page %d\n", page
);
140 ee1004_current_page
= page
;
143 status
= ee1004_eeprom_read(client
, buf
, off
, count
);
145 mutex_unlock(&ee1004_bus_lock
);
152 if (off
== EE1004_PAGE_SIZE
) {
158 mutex_unlock(&ee1004_bus_lock
);
163 static const struct bin_attribute eeprom_attr
= {
168 .size
= EE1004_EEPROM_SIZE
,
172 static int ee1004_probe(struct i2c_client
*client
,
173 const struct i2c_device_id
*id
)
176 const char *slow
= NULL
;
178 /* Make sure we can operate on this adapter */
179 if (!i2c_check_functionality(client
->adapter
,
180 I2C_FUNC_SMBUS_READ_BYTE
|
181 I2C_FUNC_SMBUS_READ_I2C_BLOCK
)) {
182 if (i2c_check_functionality(client
->adapter
,
183 I2C_FUNC_SMBUS_READ_BYTE
|
184 I2C_FUNC_SMBUS_READ_WORD_DATA
))
186 else if (i2c_check_functionality(client
->adapter
,
187 I2C_FUNC_SMBUS_READ_BYTE
|
188 I2C_FUNC_SMBUS_READ_BYTE_DATA
))
191 return -EPFNOSUPPORT
;
194 /* Use 2 dummy devices for page select command */
195 mutex_lock(&ee1004_bus_lock
);
196 if (++ee1004_dev_count
== 1) {
197 for (cnr
= 0; cnr
< 2; cnr
++) {
198 ee1004_set_page
[cnr
] = i2c_new_dummy_device(client
->adapter
,
199 EE1004_ADDR_SET_PAGE
+ cnr
);
200 if (IS_ERR(ee1004_set_page
[cnr
])) {
201 dev_err(&client
->dev
,
202 "address 0x%02x unavailable\n",
203 EE1004_ADDR_SET_PAGE
+ cnr
);
204 err
= PTR_ERR(ee1004_set_page
[cnr
]);
208 } else if (i2c_adapter_id(client
->adapter
) !=
209 i2c_adapter_id(ee1004_set_page
[0]->adapter
)) {
210 dev_err(&client
->dev
,
211 "Driver only supports devices on a single I2C bus\n");
216 /* Remember current page to avoid unneeded page select */
217 err
= ee1004_get_current_page();
220 ee1004_current_page
= err
;
221 dev_dbg(&client
->dev
, "Currently selected page: %d\n",
222 ee1004_current_page
);
223 mutex_unlock(&ee1004_bus_lock
);
225 /* Create the sysfs eeprom file */
226 err
= sysfs_create_bin_file(&client
->dev
.kobj
, &eeprom_attr
);
228 goto err_clients_lock
;
230 dev_info(&client
->dev
,
231 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
234 dev_notice(&client
->dev
,
235 "Falling back to %s reads, performance will suffer\n",
241 mutex_lock(&ee1004_bus_lock
);
243 if (--ee1004_dev_count
== 0) {
244 for (cnr
--; cnr
>= 0; cnr
--) {
245 i2c_unregister_device(ee1004_set_page
[cnr
]);
246 ee1004_set_page
[cnr
] = NULL
;
249 mutex_unlock(&ee1004_bus_lock
);
254 static int ee1004_remove(struct i2c_client
*client
)
258 sysfs_remove_bin_file(&client
->dev
.kobj
, &eeprom_attr
);
260 /* Remove page select clients if this is the last device */
261 mutex_lock(&ee1004_bus_lock
);
262 if (--ee1004_dev_count
== 0) {
263 for (i
= 0; i
< 2; i
++) {
264 i2c_unregister_device(ee1004_set_page
[i
]);
265 ee1004_set_page
[i
] = NULL
;
268 mutex_unlock(&ee1004_bus_lock
);
273 /*-------------------------------------------------------------------------*/
275 static struct i2c_driver ee1004_driver
= {
279 .probe
= ee1004_probe
,
280 .remove
= ee1004_remove
,
281 .id_table
= ee1004_ids
,
284 static int __init
ee1004_init(void)
286 return i2c_add_driver(&ee1004_driver
);
288 module_init(ee1004_init
);
290 static void __exit
ee1004_exit(void)
292 i2c_del_driver(&ee1004_driver
);
294 module_exit(ee1004_exit
);
296 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
297 MODULE_AUTHOR("Jean Delvare");
298 MODULE_LICENSE("GPL");