Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0-or-later
0002 /*
0003  * ee1004 - driver for DDR4 SPD EEPROMs
0004  *
0005  * Copyright (C) 2017-2019 Jean Delvare
0006  *
0007  * Based on the at24 driver:
0008  * Copyright (C) 2005-2007 David Brownell
0009  * Copyright (C) 2008 Wolfram Sang, Pengutronix
0010  */
0011 
0012 #include <linux/i2c.h>
0013 #include <linux/init.h>
0014 #include <linux/kernel.h>
0015 #include <linux/mod_devicetable.h>
0016 #include <linux/module.h>
0017 #include <linux/mutex.h>
0018 
0019 /*
0020  * DDR4 memory modules use special EEPROMs following the Jedec EE1004
0021  * specification. These are 512-byte EEPROMs using a single I2C address
0022  * in the 0x50-0x57 range for data. One of two 256-byte page is selected
0023  * by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
0024  *
0025  * Therefore we need to request these 2 additional addresses, and serialize
0026  * access to all such EEPROMs with a single mutex.
0027  *
0028  * We assume it is safe to read up to 32 bytes at once from these EEPROMs.
0029  * We use SMBus access even if I2C is available, these EEPROMs are small
0030  * enough, and reading from them infrequent enough, that we favor simplicity
0031  * over performance.
0032  */
0033 
0034 #define EE1004_ADDR_SET_PAGE        0x36
0035 #define EE1004_NUM_PAGES        2
0036 #define EE1004_PAGE_SIZE        256
0037 #define EE1004_PAGE_SHIFT       8
0038 #define EE1004_EEPROM_SIZE      (EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
0039 
0040 /*
0041  * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
0042  * from page selection to end of read.
0043  */
0044 static DEFINE_MUTEX(ee1004_bus_lock);
0045 static struct i2c_client *ee1004_set_page[EE1004_NUM_PAGES];
0046 static unsigned int ee1004_dev_count;
0047 static int ee1004_current_page;
0048 
0049 static const struct i2c_device_id ee1004_ids[] = {
0050     { "ee1004", 0 },
0051     { }
0052 };
0053 MODULE_DEVICE_TABLE(i2c, ee1004_ids);
0054 
0055 /*-------------------------------------------------------------------------*/
0056 
0057 static int ee1004_get_current_page(void)
0058 {
0059     int err;
0060 
0061     err = i2c_smbus_read_byte(ee1004_set_page[0]);
0062     if (err == -ENXIO) {
0063         /* Nack means page 1 is selected */
0064         return 1;
0065     }
0066     if (err < 0) {
0067         /* Anything else is a real error, bail out */
0068         return err;
0069     }
0070 
0071     /* Ack means page 0 is selected, returned value meaningless */
0072     return 0;
0073 }
0074 
0075 static int ee1004_set_current_page(struct device *dev, int page)
0076 {
0077     int ret;
0078 
0079     if (page == ee1004_current_page)
0080         return 0;
0081 
0082     /* Data is ignored */
0083     ret = i2c_smbus_write_byte(ee1004_set_page[page], 0x00);
0084     /*
0085      * Don't give up just yet. Some memory modules will select the page
0086      * but not ack the command. Check which page is selected now.
0087      */
0088     if (ret == -ENXIO && ee1004_get_current_page() == page)
0089         ret = 0;
0090     if (ret < 0) {
0091         dev_err(dev, "Failed to select page %d (%d)\n", page, ret);
0092         return ret;
0093     }
0094 
0095     dev_dbg(dev, "Selected page %d\n", page);
0096     ee1004_current_page = page;
0097 
0098     return 0;
0099 }
0100 
0101 static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
0102                   unsigned int offset, size_t count)
0103 {
0104     int status, page;
0105 
0106     page = offset >> EE1004_PAGE_SHIFT;
0107     offset &= (1 << EE1004_PAGE_SHIFT) - 1;
0108 
0109     status = ee1004_set_current_page(&client->dev, page);
0110     if (status)
0111         return status;
0112 
0113     /* Can't cross page boundaries */
0114     if (offset + count > EE1004_PAGE_SIZE)
0115         count = EE1004_PAGE_SIZE - offset;
0116 
0117     if (count > I2C_SMBUS_BLOCK_MAX)
0118         count = I2C_SMBUS_BLOCK_MAX;
0119 
0120     return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
0121 }
0122 
0123 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
0124                struct bin_attribute *bin_attr,
0125                char *buf, loff_t off, size_t count)
0126 {
0127     struct i2c_client *client = kobj_to_i2c_client(kobj);
0128     size_t requested = count;
0129     int ret = 0;
0130 
0131     /*
0132      * Read data from chip, protecting against concurrent access to
0133      * other EE1004 SPD EEPROMs on the same adapter.
0134      */
0135     mutex_lock(&ee1004_bus_lock);
0136 
0137     while (count) {
0138         ret = ee1004_eeprom_read(client, buf, off, count);
0139         if (ret < 0)
0140             goto out;
0141 
0142         buf += ret;
0143         off += ret;
0144         count -= ret;
0145     }
0146 out:
0147     mutex_unlock(&ee1004_bus_lock);
0148 
0149     return ret < 0 ? ret : requested;
0150 }
0151 
0152 static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
0153 
0154 static struct bin_attribute *ee1004_attrs[] = {
0155     &bin_attr_eeprom,
0156     NULL
0157 };
0158 
0159 BIN_ATTRIBUTE_GROUPS(ee1004);
0160 
0161 static void ee1004_cleanup(int idx)
0162 {
0163     if (--ee1004_dev_count == 0)
0164         while (--idx >= 0) {
0165             i2c_unregister_device(ee1004_set_page[idx]);
0166             ee1004_set_page[idx] = NULL;
0167         }
0168 }
0169 
0170 static int ee1004_probe(struct i2c_client *client)
0171 {
0172     int err, cnr = 0;
0173 
0174     /* Make sure we can operate on this adapter */
0175     if (!i2c_check_functionality(client->adapter,
0176                      I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
0177         !i2c_check_functionality(client->adapter,
0178                      I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
0179         return -EPFNOSUPPORT;
0180 
0181     /* Use 2 dummy devices for page select command */
0182     mutex_lock(&ee1004_bus_lock);
0183     if (++ee1004_dev_count == 1) {
0184         for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
0185             struct i2c_client *cl;
0186 
0187             cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
0188             if (IS_ERR(cl)) {
0189                 err = PTR_ERR(cl);
0190                 goto err_clients;
0191             }
0192             ee1004_set_page[cnr] = cl;
0193         }
0194 
0195         /* Remember current page to avoid unneeded page select */
0196         err = ee1004_get_current_page();
0197         if (err < 0)
0198             goto err_clients;
0199         dev_dbg(&client->dev, "Currently selected page: %d\n", err);
0200         ee1004_current_page = err;
0201     } else if (client->adapter != ee1004_set_page[0]->adapter) {
0202         dev_err(&client->dev,
0203             "Driver only supports devices on a single I2C bus\n");
0204         err = -EOPNOTSUPP;
0205         goto err_clients;
0206     }
0207     mutex_unlock(&ee1004_bus_lock);
0208 
0209     dev_info(&client->dev,
0210          "%u byte EE1004-compliant SPD EEPROM, read-only\n",
0211          EE1004_EEPROM_SIZE);
0212 
0213     return 0;
0214 
0215  err_clients:
0216     ee1004_cleanup(cnr);
0217     mutex_unlock(&ee1004_bus_lock);
0218 
0219     return err;
0220 }
0221 
0222 static int ee1004_remove(struct i2c_client *client)
0223 {
0224     /* Remove page select clients if this is the last device */
0225     mutex_lock(&ee1004_bus_lock);
0226     ee1004_cleanup(EE1004_NUM_PAGES);
0227     mutex_unlock(&ee1004_bus_lock);
0228 
0229     return 0;
0230 }
0231 
0232 /*-------------------------------------------------------------------------*/
0233 
0234 static struct i2c_driver ee1004_driver = {
0235     .driver = {
0236         .name = "ee1004",
0237         .dev_groups = ee1004_groups,
0238     },
0239     .probe_new = ee1004_probe,
0240     .remove = ee1004_remove,
0241     .id_table = ee1004_ids,
0242 };
0243 module_i2c_driver(ee1004_driver);
0244 
0245 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
0246 MODULE_AUTHOR("Jean Delvare");
0247 MODULE_LICENSE("GPL");