xref: /openbmc/linux/drivers/misc/eeprom/ee1004.c (revision 4f727ece)
1 // SPDX-License-Identifier: GPL-2.0-or-later
2 /*
3  * ee1004 - driver for DDR4 SPD EEPROMs
4  *
5  * Copyright (C) 2017 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 ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
57 				  unsigned int offset, size_t count)
58 {
59 	int status;
60 
61 	if (count > I2C_SMBUS_BLOCK_MAX)
62 		count = I2C_SMBUS_BLOCK_MAX;
63 	/* Can't cross page boundaries */
64 	if (unlikely(offset + count > EE1004_PAGE_SIZE))
65 		count = EE1004_PAGE_SIZE - offset;
66 
67 	status = i2c_smbus_read_i2c_block_data_or_emulated(client, offset,
68 							   count, buf);
69 	dev_dbg(&client->dev, "read %zu@%d --> %d\n", count, offset, status);
70 
71 	return status;
72 }
73 
74 static ssize_t ee1004_read(struct file *filp, struct kobject *kobj,
75 			   struct bin_attribute *bin_attr,
76 			   char *buf, loff_t off, size_t count)
77 {
78 	struct device *dev = kobj_to_dev(kobj);
79 	struct i2c_client *client = to_i2c_client(dev);
80 	size_t requested = count;
81 	int page;
82 
83 	if (unlikely(!count))
84 		return count;
85 
86 	page = off >> EE1004_PAGE_SHIFT;
87 	if (unlikely(page > 1))
88 		return 0;
89 	off &= (1 << EE1004_PAGE_SHIFT) - 1;
90 
91 	/*
92 	 * Read data from chip, protecting against concurrent access to
93 	 * other EE1004 SPD EEPROMs on the same adapter.
94 	 */
95 	mutex_lock(&ee1004_bus_lock);
96 
97 	while (count) {
98 		int status;
99 
100 		/* Select page */
101 		if (page != ee1004_current_page) {
102 			/* Data is ignored */
103 			status = i2c_smbus_write_byte(ee1004_set_page[page],
104 						      0x00);
105 			if (status < 0) {
106 				dev_err(dev, "Failed to select page %d (%d)\n",
107 					page, status);
108 				mutex_unlock(&ee1004_bus_lock);
109 				return status;
110 			}
111 			dev_dbg(dev, "Selected page %d\n", page);
112 			ee1004_current_page = page;
113 		}
114 
115 		status = ee1004_eeprom_read(client, buf, off, count);
116 		if (status < 0) {
117 			mutex_unlock(&ee1004_bus_lock);
118 			return status;
119 		}
120 		buf += status;
121 		off += status;
122 		count -= status;
123 
124 		if (off == EE1004_PAGE_SIZE) {
125 			page++;
126 			off = 0;
127 		}
128 	}
129 
130 	mutex_unlock(&ee1004_bus_lock);
131 
132 	return requested;
133 }
134 
135 static const struct bin_attribute eeprom_attr = {
136 	.attr = {
137 		.name = "eeprom",
138 		.mode = 0444,
139 	},
140 	.size = EE1004_EEPROM_SIZE,
141 	.read = ee1004_read,
142 };
143 
144 static int ee1004_probe(struct i2c_client *client,
145 			const struct i2c_device_id *id)
146 {
147 	int err, cnr = 0;
148 	const char *slow = NULL;
149 
150 	/* Make sure we can operate on this adapter */
151 	if (!i2c_check_functionality(client->adapter,
152 				     I2C_FUNC_SMBUS_READ_BYTE |
153 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
154 		if (i2c_check_functionality(client->adapter,
155 				     I2C_FUNC_SMBUS_READ_BYTE |
156 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
157 			slow = "word";
158 		else if (i2c_check_functionality(client->adapter,
159 				     I2C_FUNC_SMBUS_READ_BYTE |
160 				     I2C_FUNC_SMBUS_READ_BYTE_DATA))
161 			slow = "byte";
162 		else
163 			return -EPFNOSUPPORT;
164 	}
165 
166 	/* Use 2 dummy devices for page select command */
167 	mutex_lock(&ee1004_bus_lock);
168 	if (++ee1004_dev_count == 1) {
169 		for (cnr = 0; cnr < 2; cnr++) {
170 			ee1004_set_page[cnr] = i2c_new_dummy(client->adapter,
171 						EE1004_ADDR_SET_PAGE + cnr);
172 			if (!ee1004_set_page[cnr]) {
173 				dev_err(&client->dev,
174 					"address 0x%02x unavailable\n",
175 					EE1004_ADDR_SET_PAGE + cnr);
176 				err = -EADDRINUSE;
177 				goto err_clients;
178 			}
179 		}
180 	} else if (i2c_adapter_id(client->adapter) !=
181 		   i2c_adapter_id(ee1004_set_page[0]->adapter)) {
182 		dev_err(&client->dev,
183 			"Driver only supports devices on a single I2C bus\n");
184 		err = -EOPNOTSUPP;
185 		goto err_clients;
186 	}
187 
188 	/* Remember current page to avoid unneeded page select */
189 	err = i2c_smbus_read_byte(ee1004_set_page[0]);
190 	if (err == -ENXIO) {
191 		/* Nack means page 1 is selected */
192 		ee1004_current_page = 1;
193 	} else if (err < 0) {
194 		/* Anything else is a real error, bail out */
195 		goto err_clients;
196 	} else {
197 		/* Ack means page 0 is selected, returned value meaningless */
198 		ee1004_current_page = 0;
199 	}
200 	dev_dbg(&client->dev, "Currently selected page: %d\n",
201 		ee1004_current_page);
202 	mutex_unlock(&ee1004_bus_lock);
203 
204 	/* Create the sysfs eeprom file */
205 	err = sysfs_create_bin_file(&client->dev.kobj, &eeprom_attr);
206 	if (err)
207 		goto err_clients_lock;
208 
209 	dev_info(&client->dev,
210 		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
211 		 EE1004_EEPROM_SIZE);
212 	if (slow)
213 		dev_notice(&client->dev,
214 			   "Falling back to %s reads, performance will suffer\n",
215 			   slow);
216 
217 	return 0;
218 
219  err_clients_lock:
220 	mutex_lock(&ee1004_bus_lock);
221  err_clients:
222 	if (--ee1004_dev_count == 0) {
223 		for (cnr--; cnr >= 0; cnr--) {
224 			i2c_unregister_device(ee1004_set_page[cnr]);
225 			ee1004_set_page[cnr] = NULL;
226 		}
227 	}
228 	mutex_unlock(&ee1004_bus_lock);
229 
230 	return err;
231 }
232 
233 static int ee1004_remove(struct i2c_client *client)
234 {
235 	int i;
236 
237 	sysfs_remove_bin_file(&client->dev.kobj, &eeprom_attr);
238 
239 	/* Remove page select clients if this is the last device */
240 	mutex_lock(&ee1004_bus_lock);
241 	if (--ee1004_dev_count == 0) {
242 		for (i = 0; i < 2; i++) {
243 			i2c_unregister_device(ee1004_set_page[i]);
244 			ee1004_set_page[i] = NULL;
245 		}
246 	}
247 	mutex_unlock(&ee1004_bus_lock);
248 
249 	return 0;
250 }
251 
252 /*-------------------------------------------------------------------------*/
253 
254 static struct i2c_driver ee1004_driver = {
255 	.driver = {
256 		.name = "ee1004",
257 	},
258 	.probe = ee1004_probe,
259 	.remove = ee1004_remove,
260 	.id_table = ee1004_ids,
261 };
262 
263 static int __init ee1004_init(void)
264 {
265 	return i2c_add_driver(&ee1004_driver);
266 }
267 module_init(ee1004_init);
268 
269 static void __exit ee1004_exit(void)
270 {
271 	i2c_del_driver(&ee1004_driver);
272 }
273 module_exit(ee1004_exit);
274 
275 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
276 MODULE_AUTHOR("Jean Delvare");
277 MODULE_LICENSE("GPL");
278