1 // SPDX-License-Identifier: GPL-2.0-or-later 2 /* 3 * ee1004 - driver for DDR4 SPD EEPROMs 4 * 5 * Copyright (C) 2017-2019 Jean Delvare 6 * 7 * Based on the at24 driver: 8 * Copyright (C) 2005-2007 David Brownell 9 * Copyright (C) 2008 Wolfram Sang, Pengutronix 10 */ 11 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> 18 19 /* 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. 24 * 25 * Therefore we need to request these 2 additional addresses, and serialize 26 * access to all such EEPROMs with a single mutex. 27 * 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 31 * over performance. 32 */ 33 34 #define EE1004_MAX_BUSSES 8 35 #define EE1004_ADDR_SET_PAGE 0x36 36 #define EE1004_NUM_PAGES 2 37 #define EE1004_PAGE_SIZE 256 38 #define EE1004_PAGE_SHIFT 8 39 #define EE1004_EEPROM_SIZE (EE1004_PAGE_SIZE * EE1004_NUM_PAGES) 40 41 /* 42 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held 43 * from page selection to end of read. 44 */ 45 static DEFINE_MUTEX(ee1004_bus_lock); 46 47 static struct ee1004_bus_data { 48 struct i2c_adapter *adap; 49 struct i2c_client *set_page[EE1004_NUM_PAGES]; 50 unsigned int dev_count; 51 int current_page; 52 } ee1004_bus_data[EE1004_MAX_BUSSES]; 53 54 static const struct i2c_device_id ee1004_ids[] = { 55 { "ee1004", 0 }, 56 { } 57 }; 58 MODULE_DEVICE_TABLE(i2c, ee1004_ids); 59 60 /*-------------------------------------------------------------------------*/ 61 62 static struct ee1004_bus_data *ee1004_get_bus_data(struct i2c_adapter *adap) 63 { 64 int i; 65 66 for (i = 0; i < EE1004_MAX_BUSSES; i++) 67 if (ee1004_bus_data[i].adap == adap) 68 return ee1004_bus_data + i; 69 70 /* If not existent yet, create new entry */ 71 for (i = 0; i < EE1004_MAX_BUSSES; i++) 72 if (!ee1004_bus_data[i].adap) { 73 ee1004_bus_data[i].adap = adap; 74 return ee1004_bus_data + i; 75 } 76 77 return NULL; 78 } 79 80 static int ee1004_get_current_page(struct ee1004_bus_data *bd) 81 { 82 int err; 83 84 err = i2c_smbus_read_byte(bd->set_page[0]); 85 if (err == -ENXIO) { 86 /* Nack means page 1 is selected */ 87 return 1; 88 } 89 if (err < 0) { 90 /* Anything else is a real error, bail out */ 91 return err; 92 } 93 94 /* Ack means page 0 is selected, returned value meaningless */ 95 return 0; 96 } 97 98 static int ee1004_set_current_page(struct i2c_client *client, int page) 99 { 100 struct ee1004_bus_data *bd = i2c_get_clientdata(client); 101 int ret; 102 103 if (page == bd->current_page) 104 return 0; 105 106 /* Data is ignored */ 107 ret = i2c_smbus_write_byte(bd->set_page[page], 0x00); 108 /* 109 * Don't give up just yet. Some memory modules will select the page 110 * but not ack the command. Check which page is selected now. 111 */ 112 if (ret == -ENXIO && ee1004_get_current_page(bd) == page) 113 ret = 0; 114 if (ret < 0) { 115 dev_err(&client->dev, "Failed to select page %d (%d)\n", page, ret); 116 return ret; 117 } 118 119 dev_dbg(&client->dev, "Selected page %d\n", page); 120 bd->current_page = page; 121 122 return 0; 123 } 124 125 static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf, 126 unsigned int offset, size_t count) 127 { 128 int status, page; 129 130 page = offset >> EE1004_PAGE_SHIFT; 131 offset &= (1 << EE1004_PAGE_SHIFT) - 1; 132 133 status = ee1004_set_current_page(client, page); 134 if (status) 135 return status; 136 137 /* Can't cross page boundaries */ 138 if (offset + count > EE1004_PAGE_SIZE) 139 count = EE1004_PAGE_SIZE - offset; 140 141 if (count > I2C_SMBUS_BLOCK_MAX) 142 count = I2C_SMBUS_BLOCK_MAX; 143 144 return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf); 145 } 146 147 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj, 148 struct bin_attribute *bin_attr, 149 char *buf, loff_t off, size_t count) 150 { 151 struct i2c_client *client = kobj_to_i2c_client(kobj); 152 size_t requested = count; 153 int ret = 0; 154 155 /* 156 * Read data from chip, protecting against concurrent access to 157 * other EE1004 SPD EEPROMs on the same adapter. 158 */ 159 mutex_lock(&ee1004_bus_lock); 160 161 while (count) { 162 ret = ee1004_eeprom_read(client, buf, off, count); 163 if (ret < 0) 164 goto out; 165 166 buf += ret; 167 off += ret; 168 count -= ret; 169 } 170 out: 171 mutex_unlock(&ee1004_bus_lock); 172 173 return ret < 0 ? ret : requested; 174 } 175 176 static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE); 177 178 static struct bin_attribute *ee1004_attrs[] = { 179 &bin_attr_eeprom, 180 NULL 181 }; 182 183 BIN_ATTRIBUTE_GROUPS(ee1004); 184 185 static void ee1004_cleanup(int idx, struct ee1004_bus_data *bd) 186 { 187 if (--bd->dev_count == 0) { 188 while (--idx >= 0) 189 i2c_unregister_device(bd->set_page[idx]); 190 memset(bd, 0, sizeof(struct ee1004_bus_data)); 191 } 192 } 193 194 static int ee1004_probe(struct i2c_client *client) 195 { 196 struct ee1004_bus_data *bd; 197 int err, cnr = 0; 198 199 /* Make sure we can operate on this adapter */ 200 if (!i2c_check_functionality(client->adapter, 201 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) && 202 !i2c_check_functionality(client->adapter, 203 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA)) 204 return -EPFNOSUPPORT; 205 206 mutex_lock(&ee1004_bus_lock); 207 208 bd = ee1004_get_bus_data(client->adapter); 209 if (!bd) { 210 mutex_unlock(&ee1004_bus_lock); 211 return dev_err_probe(&client->dev, -ENOSPC, 212 "Only %d busses supported", EE1004_MAX_BUSSES); 213 } 214 215 i2c_set_clientdata(client, bd); 216 217 if (++bd->dev_count == 1) { 218 /* Use 2 dummy devices for page select command */ 219 for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) { 220 struct i2c_client *cl; 221 222 cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr); 223 if (IS_ERR(cl)) { 224 err = PTR_ERR(cl); 225 goto err_clients; 226 } 227 bd->set_page[cnr] = cl; 228 } 229 230 /* Remember current page to avoid unneeded page select */ 231 err = ee1004_get_current_page(bd); 232 if (err < 0) 233 goto err_clients; 234 dev_dbg(&client->dev, "Currently selected page: %d\n", err); 235 bd->current_page = err; 236 } 237 mutex_unlock(&ee1004_bus_lock); 238 239 dev_info(&client->dev, 240 "%u byte EE1004-compliant SPD EEPROM, read-only\n", 241 EE1004_EEPROM_SIZE); 242 243 return 0; 244 245 err_clients: 246 ee1004_cleanup(cnr, bd); 247 mutex_unlock(&ee1004_bus_lock); 248 249 return err; 250 } 251 252 static void ee1004_remove(struct i2c_client *client) 253 { 254 struct ee1004_bus_data *bd = i2c_get_clientdata(client); 255 256 /* Remove page select clients if this is the last device */ 257 mutex_lock(&ee1004_bus_lock); 258 ee1004_cleanup(EE1004_NUM_PAGES, bd); 259 mutex_unlock(&ee1004_bus_lock); 260 } 261 262 /*-------------------------------------------------------------------------*/ 263 264 static struct i2c_driver ee1004_driver = { 265 .driver = { 266 .name = "ee1004", 267 .dev_groups = ee1004_groups, 268 }, 269 .probe = ee1004_probe, 270 .remove = ee1004_remove, 271 .id_table = ee1004_ids, 272 }; 273 module_i2c_driver(ee1004_driver); 274 275 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs"); 276 MODULE_AUTHOR("Jean Delvare"); 277 MODULE_LICENSE("GPL"); 278