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_ADDR_SET_PAGE 0x36 35 #define EE1004_EEPROM_SIZE 512 36 #define EE1004_PAGE_SIZE 256 37 #define EE1004_PAGE_SHIFT 8 38 39 /* 40 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held 41 * from page selection to end of read. 42 */ 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; 47 48 static const struct i2c_device_id ee1004_ids[] = { 49 { "ee1004", 0 }, 50 { } 51 }; 52 MODULE_DEVICE_TABLE(i2c, ee1004_ids); 53 54 /*-------------------------------------------------------------------------*/ 55 56 static int ee1004_get_current_page(void) 57 { 58 int err; 59 60 err = i2c_smbus_read_byte(ee1004_set_page[0]); 61 if (err == -ENXIO) { 62 /* Nack means page 1 is selected */ 63 return 1; 64 } 65 if (err < 0) { 66 /* Anything else is a real error, bail out */ 67 return err; 68 } 69 70 /* Ack means page 0 is selected, returned value meaningless */ 71 return 0; 72 } 73 74 static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf, 75 unsigned int offset, size_t count) 76 { 77 int status; 78 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; 84 85 status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset, 86 count, buf); 87 dev_dbg(&client->dev, "read %zu@%d --> %d\n", count, offset, status); 88 89 return status; 90 } 91 92 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj, 93 struct bin_attribute *bin_attr, 94 char *buf, loff_t off, size_t count) 95 { 96 struct i2c_client *client = kobj_to_i2c_client(kobj); 97 size_t requested = count; 98 int page; 99 100 page = off >> EE1004_PAGE_SHIFT; 101 if (unlikely(page > 1)) 102 return 0; 103 off &= (1 << EE1004_PAGE_SHIFT) - 1; 104 105 /* 106 * Read data from chip, protecting against concurrent access to 107 * other EE1004 SPD EEPROMs on the same adapter. 108 */ 109 mutex_lock(&ee1004_bus_lock); 110 111 while (count) { 112 int status; 113 114 /* Select page */ 115 if (page != ee1004_current_page) { 116 /* Data is ignored */ 117 status = i2c_smbus_write_byte(ee1004_set_page[page], 118 0x00); 119 if (status == -ENXIO) { 120 /* 121 * Don't give up just yet. Some memory 122 * modules will select the page but not 123 * ack the command. Check which page is 124 * selected now. 125 */ 126 if (ee1004_get_current_page() == page) 127 status = 0; 128 } 129 if (status < 0) { 130 dev_err(dev, "Failed to select page %d (%d)\n", 131 page, status); 132 mutex_unlock(&ee1004_bus_lock); 133 return status; 134 } 135 dev_dbg(dev, "Selected page %d\n", page); 136 ee1004_current_page = page; 137 } 138 139 status = ee1004_eeprom_read(client, buf, off, count); 140 if (status < 0) { 141 mutex_unlock(&ee1004_bus_lock); 142 return status; 143 } 144 buf += status; 145 off += status; 146 count -= status; 147 148 if (off == EE1004_PAGE_SIZE) { 149 page++; 150 off = 0; 151 } 152 } 153 154 mutex_unlock(&ee1004_bus_lock); 155 156 return requested; 157 } 158 159 static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE); 160 161 static struct bin_attribute *ee1004_attrs[] = { 162 &bin_attr_eeprom, 163 NULL 164 }; 165 166 BIN_ATTRIBUTE_GROUPS(ee1004); 167 168 static int ee1004_probe(struct i2c_client *client, 169 const struct i2c_device_id *id) 170 { 171 int err, cnr = 0; 172 const char *slow = NULL; 173 174 /* Make sure we can operate on this adapter */ 175 if (!i2c_check_functionality(client->adapter, 176 I2C_FUNC_SMBUS_READ_BYTE | 177 I2C_FUNC_SMBUS_READ_I2C_BLOCK)) { 178 if (i2c_check_functionality(client->adapter, 179 I2C_FUNC_SMBUS_READ_BYTE | 180 I2C_FUNC_SMBUS_READ_WORD_DATA)) 181 slow = "word"; 182 else if (i2c_check_functionality(client->adapter, 183 I2C_FUNC_SMBUS_READ_BYTE | 184 I2C_FUNC_SMBUS_READ_BYTE_DATA)) 185 slow = "byte"; 186 else 187 return -EPFNOSUPPORT; 188 } 189 190 /* Use 2 dummy devices for page select command */ 191 mutex_lock(&ee1004_bus_lock); 192 if (++ee1004_dev_count == 1) { 193 for (cnr = 0; cnr < 2; cnr++) { 194 ee1004_set_page[cnr] = i2c_new_dummy_device(client->adapter, 195 EE1004_ADDR_SET_PAGE + cnr); 196 if (IS_ERR(ee1004_set_page[cnr])) { 197 dev_err(&client->dev, 198 "address 0x%02x unavailable\n", 199 EE1004_ADDR_SET_PAGE + cnr); 200 err = PTR_ERR(ee1004_set_page[cnr]); 201 goto err_clients; 202 } 203 } 204 } else if (i2c_adapter_id(client->adapter) != 205 i2c_adapter_id(ee1004_set_page[0]->adapter)) { 206 dev_err(&client->dev, 207 "Driver only supports devices on a single I2C bus\n"); 208 err = -EOPNOTSUPP; 209 goto err_clients; 210 } 211 212 /* Remember current page to avoid unneeded page select */ 213 err = ee1004_get_current_page(); 214 if (err < 0) 215 goto err_clients; 216 ee1004_current_page = err; 217 dev_dbg(&client->dev, "Currently selected page: %d\n", 218 ee1004_current_page); 219 mutex_unlock(&ee1004_bus_lock); 220 221 dev_info(&client->dev, 222 "%u byte EE1004-compliant SPD EEPROM, read-only\n", 223 EE1004_EEPROM_SIZE); 224 if (slow) 225 dev_notice(&client->dev, 226 "Falling back to %s reads, performance will suffer\n", 227 slow); 228 229 return 0; 230 231 err_clients: 232 if (--ee1004_dev_count == 0) { 233 for (cnr--; cnr >= 0; cnr--) { 234 i2c_unregister_device(ee1004_set_page[cnr]); 235 ee1004_set_page[cnr] = NULL; 236 } 237 } 238 mutex_unlock(&ee1004_bus_lock); 239 240 return err; 241 } 242 243 static int ee1004_remove(struct i2c_client *client) 244 { 245 int i; 246 247 /* Remove page select clients if this is the last device */ 248 mutex_lock(&ee1004_bus_lock); 249 if (--ee1004_dev_count == 0) { 250 for (i = 0; i < 2; i++) { 251 i2c_unregister_device(ee1004_set_page[i]); 252 ee1004_set_page[i] = NULL; 253 } 254 } 255 mutex_unlock(&ee1004_bus_lock); 256 257 return 0; 258 } 259 260 /*-------------------------------------------------------------------------*/ 261 262 static struct i2c_driver ee1004_driver = { 263 .driver = { 264 .name = "ee1004", 265 .dev_groups = ee1004_groups, 266 }, 267 .probe = ee1004_probe, 268 .remove = ee1004_remove, 269 .id_table = ee1004_ids, 270 }; 271 module_i2c_driver(ee1004_driver); 272 273 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs"); 274 MODULE_AUTHOR("Jean Delvare"); 275 MODULE_LICENSE("GPL"); 276