2019-05-27 09:55:01 +03:00
|
|
|
// SPDX-License-Identifier: GPL-2.0-or-later
|
2018-10-07 20:05:00 +03:00
|
|
|
/*
|
|
|
|
* ee1004 - driver for DDR4 SPD EEPROMs
|
|
|
|
*
|
2019-05-06 16:16:56 +03:00
|
|
|
* Copyright (C) 2017-2019 Jean Delvare
|
2018-10-07 20:05:00 +03:00
|
|
|
*
|
|
|
|
* Based on the at24 driver:
|
|
|
|
* Copyright (C) 2005-2007 David Brownell
|
|
|
|
* Copyright (C) 2008 Wolfram Sang, Pengutronix
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <linux/i2c.h>
|
|
|
|
#include <linux/init.h>
|
|
|
|
#include <linux/kernel.h>
|
|
|
|
#include <linux/mod_devicetable.h>
|
|
|
|
#include <linux/module.h>
|
|
|
|
#include <linux/mutex.h>
|
|
|
|
|
|
|
|
/*
|
|
|
|
* DDR4 memory modules use special EEPROMs following the Jedec EE1004
|
|
|
|
* specification. These are 512-byte EEPROMs using a single I2C address
|
|
|
|
* in the 0x50-0x57 range for data. One of two 256-byte page is selected
|
|
|
|
* by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
|
|
|
|
*
|
|
|
|
* Therefore we need to request these 2 additional addresses, and serialize
|
|
|
|
* access to all such EEPROMs with a single mutex.
|
|
|
|
*
|
|
|
|
* We assume it is safe to read up to 32 bytes at once from these EEPROMs.
|
|
|
|
* We use SMBus access even if I2C is available, these EEPROMs are small
|
|
|
|
* enough, and reading from them infrequent enough, that we favor simplicity
|
|
|
|
* over performance.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#define EE1004_ADDR_SET_PAGE 0x36
|
2021-05-24 23:17:28 +03:00
|
|
|
#define EE1004_NUM_PAGES 2
|
2018-10-07 20:05:00 +03:00
|
|
|
#define EE1004_PAGE_SIZE 256
|
|
|
|
#define EE1004_PAGE_SHIFT 8
|
2021-05-24 23:17:28 +03:00
|
|
|
#define EE1004_EEPROM_SIZE (EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
|
2018-10-07 20:05:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
|
|
|
|
* from page selection to end of read.
|
|
|
|
*/
|
|
|
|
static DEFINE_MUTEX(ee1004_bus_lock);
|
2021-05-24 23:17:28 +03:00
|
|
|
static struct i2c_client *ee1004_set_page[EE1004_NUM_PAGES];
|
2018-10-07 20:05:00 +03:00
|
|
|
static unsigned int ee1004_dev_count;
|
|
|
|
static int ee1004_current_page;
|
|
|
|
|
|
|
|
static const struct i2c_device_id ee1004_ids[] = {
|
|
|
|
{ "ee1004", 0 },
|
|
|
|
{ }
|
|
|
|
};
|
|
|
|
MODULE_DEVICE_TABLE(i2c, ee1004_ids);
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
2019-05-06 16:15:39 +03:00
|
|
|
static int ee1004_get_current_page(void)
|
|
|
|
{
|
|
|
|
int err;
|
|
|
|
|
|
|
|
err = i2c_smbus_read_byte(ee1004_set_page[0]);
|
|
|
|
if (err == -ENXIO) {
|
|
|
|
/* Nack means page 1 is selected */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
if (err < 0) {
|
|
|
|
/* Anything else is a real error, bail out */
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Ack means page 0 is selected, returned value meaningless */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2021-05-24 23:15:26 +03:00
|
|
|
static int ee1004_set_current_page(struct device *dev, int page)
|
|
|
|
{
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
if (page == ee1004_current_page)
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
/* Data is ignored */
|
|
|
|
ret = i2c_smbus_write_byte(ee1004_set_page[page], 0x00);
|
|
|
|
/*
|
|
|
|
* Don't give up just yet. Some memory modules will select the page
|
|
|
|
* but not ack the command. Check which page is selected now.
|
|
|
|
*/
|
|
|
|
if (ret == -ENXIO && ee1004_get_current_page() == page)
|
|
|
|
ret = 0;
|
|
|
|
if (ret < 0) {
|
|
|
|
dev_err(dev, "Failed to select page %d (%d)\n", page, ret);
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
dev_dbg(dev, "Selected page %d\n", page);
|
|
|
|
ee1004_current_page = page;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-10-07 20:05:00 +03:00
|
|
|
static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
|
|
|
|
unsigned int offset, size_t count)
|
|
|
|
{
|
2021-05-24 23:16:51 +03:00
|
|
|
int status, page;
|
|
|
|
|
|
|
|
page = offset >> EE1004_PAGE_SHIFT;
|
|
|
|
offset &= (1 << EE1004_PAGE_SHIFT) - 1;
|
|
|
|
|
|
|
|
status = ee1004_set_current_page(&client->dev, page);
|
|
|
|
if (status)
|
|
|
|
return status;
|
2018-10-07 20:05:00 +03:00
|
|
|
|
|
|
|
/* Can't cross page boundaries */
|
2021-05-24 23:10:47 +03:00
|
|
|
if (offset + count > EE1004_PAGE_SIZE)
|
2018-10-07 20:05:00 +03:00
|
|
|
count = EE1004_PAGE_SIZE - offset;
|
|
|
|
|
2022-02-03 19:49:52 +03:00
|
|
|
if (count > I2C_SMBUS_BLOCK_MAX)
|
|
|
|
count = I2C_SMBUS_BLOCK_MAX;
|
|
|
|
|
2021-06-01 10:50:30 +03:00
|
|
|
return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
|
2018-10-07 20:05:00 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 19:34:27 +03:00
|
|
|
static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
|
2018-10-07 20:05:00 +03:00
|
|
|
struct bin_attribute *bin_attr,
|
|
|
|
char *buf, loff_t off, size_t count)
|
|
|
|
{
|
2021-05-24 23:08:51 +03:00
|
|
|
struct i2c_client *client = kobj_to_i2c_client(kobj);
|
2018-10-07 20:05:00 +03:00
|
|
|
size_t requested = count;
|
2021-05-24 23:16:51 +03:00
|
|
|
int ret = 0;
|
2018-10-07 20:05:00 +03:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Read data from chip, protecting against concurrent access to
|
|
|
|
* other EE1004 SPD EEPROMs on the same adapter.
|
|
|
|
*/
|
|
|
|
mutex_lock(&ee1004_bus_lock);
|
|
|
|
|
|
|
|
while (count) {
|
2021-05-24 23:16:05 +03:00
|
|
|
ret = ee1004_eeprom_read(client, buf, off, count);
|
|
|
|
if (ret < 0)
|
|
|
|
goto out;
|
|
|
|
|
|
|
|
buf += ret;
|
|
|
|
off += ret;
|
|
|
|
count -= ret;
|
2018-10-07 20:05:00 +03:00
|
|
|
}
|
2021-05-24 23:16:05 +03:00
|
|
|
out:
|
2018-10-07 20:05:00 +03:00
|
|
|
mutex_unlock(&ee1004_bus_lock);
|
|
|
|
|
2021-05-24 23:16:05 +03:00
|
|
|
return ret < 0 ? ret : requested;
|
2018-10-07 20:05:00 +03:00
|
|
|
}
|
|
|
|
|
2021-05-19 19:34:27 +03:00
|
|
|
static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
|
|
|
|
|
|
|
|
static struct bin_attribute *ee1004_attrs[] = {
|
|
|
|
&bin_attr_eeprom,
|
|
|
|
NULL
|
2018-10-07 20:05:00 +03:00
|
|
|
};
|
|
|
|
|
2021-05-19 19:34:27 +03:00
|
|
|
BIN_ATTRIBUTE_GROUPS(ee1004);
|
|
|
|
|
2021-05-24 23:18:23 +03:00
|
|
|
static void ee1004_cleanup(int idx)
|
|
|
|
{
|
|
|
|
if (--ee1004_dev_count == 0)
|
|
|
|
while (--idx >= 0) {
|
|
|
|
i2c_unregister_device(ee1004_set_page[idx]);
|
|
|
|
ee1004_set_page[idx] = NULL;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-24 23:13:52 +03:00
|
|
|
static int ee1004_probe(struct i2c_client *client)
|
2018-10-07 20:05:00 +03:00
|
|
|
{
|
|
|
|
int err, cnr = 0;
|
|
|
|
|
|
|
|
/* Make sure we can operate on this adapter */
|
|
|
|
if (!i2c_check_functionality(client->adapter,
|
2021-05-24 23:12:22 +03:00
|
|
|
I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
|
|
|
|
!i2c_check_functionality(client->adapter,
|
|
|
|
I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
|
|
|
|
return -EPFNOSUPPORT;
|
2018-10-07 20:05:00 +03:00
|
|
|
|
|
|
|
/* Use 2 dummy devices for page select command */
|
|
|
|
mutex_lock(&ee1004_bus_lock);
|
|
|
|
if (++ee1004_dev_count == 1) {
|
2021-05-24 23:17:28 +03:00
|
|
|
for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
|
2021-05-24 23:13:12 +03:00
|
|
|
struct i2c_client *cl;
|
|
|
|
|
|
|
|
cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
|
|
|
|
if (IS_ERR(cl)) {
|
|
|
|
err = PTR_ERR(cl);
|
2018-10-07 20:05:00 +03:00
|
|
|
goto err_clients;
|
|
|
|
}
|
2021-05-24 23:13:12 +03:00
|
|
|
ee1004_set_page[cnr] = cl;
|
2018-10-07 20:05:00 +03:00
|
|
|
}
|
2021-05-24 23:14:43 +03:00
|
|
|
|
|
|
|
/* Remember current page to avoid unneeded page select */
|
|
|
|
err = ee1004_get_current_page();
|
|
|
|
if (err < 0)
|
|
|
|
goto err_clients;
|
|
|
|
dev_dbg(&client->dev, "Currently selected page: %d\n", err);
|
|
|
|
ee1004_current_page = err;
|
2021-05-24 23:11:31 +03:00
|
|
|
} else if (client->adapter != ee1004_set_page[0]->adapter) {
|
2018-10-07 20:05:00 +03:00
|
|
|
dev_err(&client->dev,
|
|
|
|
"Driver only supports devices on a single I2C bus\n");
|
|
|
|
err = -EOPNOTSUPP;
|
|
|
|
goto err_clients;
|
|
|
|
}
|
|
|
|
mutex_unlock(&ee1004_bus_lock);
|
|
|
|
|
|
|
|
dev_info(&client->dev,
|
|
|
|
"%u byte EE1004-compliant SPD EEPROM, read-only\n",
|
|
|
|
EE1004_EEPROM_SIZE);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
err_clients:
|
2021-05-24 23:18:23 +03:00
|
|
|
ee1004_cleanup(cnr);
|
2018-10-07 20:05:00 +03:00
|
|
|
mutex_unlock(&ee1004_bus_lock);
|
|
|
|
|
|
|
|
return err;
|
|
|
|
}
|
|
|
|
|
2022-08-15 11:02:30 +03:00
|
|
|
static void ee1004_remove(struct i2c_client *client)
|
2018-10-07 20:05:00 +03:00
|
|
|
{
|
|
|
|
/* Remove page select clients if this is the last device */
|
|
|
|
mutex_lock(&ee1004_bus_lock);
|
2021-05-24 23:18:23 +03:00
|
|
|
ee1004_cleanup(EE1004_NUM_PAGES);
|
2018-10-07 20:05:00 +03:00
|
|
|
mutex_unlock(&ee1004_bus_lock);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*-------------------------------------------------------------------------*/
|
|
|
|
|
|
|
|
static struct i2c_driver ee1004_driver = {
|
|
|
|
.driver = {
|
|
|
|
.name = "ee1004",
|
2021-05-19 19:34:27 +03:00
|
|
|
.dev_groups = ee1004_groups,
|
2018-10-07 20:05:00 +03:00
|
|
|
},
|
2021-05-24 23:13:52 +03:00
|
|
|
.probe_new = ee1004_probe,
|
2018-10-07 20:05:00 +03:00
|
|
|
.remove = ee1004_remove,
|
|
|
|
.id_table = ee1004_ids,
|
|
|
|
};
|
2020-09-18 06:02:25 +03:00
|
|
|
module_i2c_driver(ee1004_driver);
|
2018-10-07 20:05:00 +03:00
|
|
|
|
|
|
|
MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
|
|
|
|
MODULE_AUTHOR("Jean Delvare");
|
|
|
|
MODULE_LICENSE("GPL");
|