xref: /openbmc/linux/drivers/misc/eeprom/ee1004.c (revision 7adbd54fb23b38fd7bc28f679445ae93d3846c40)
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 	if (unlikely(!count))
101 		return count;
102 
103 	page = off >> EE1004_PAGE_SHIFT;
104 	if (unlikely(page > 1))
105 		return 0;
106 	off &= (1 << EE1004_PAGE_SHIFT) - 1;
107 
108 	/*
109 	 * Read data from chip, protecting against concurrent access to
110 	 * other EE1004 SPD EEPROMs on the same adapter.
111 	 */
112 	mutex_lock(&ee1004_bus_lock);
113 
114 	while (count) {
115 		int status;
116 
117 		/* Select page */
118 		if (page != ee1004_current_page) {
119 			/* Data is ignored */
120 			status = i2c_smbus_write_byte(ee1004_set_page[page],
121 						      0x00);
122 			if (status == -ENXIO) {
123 				/*
124 				 * Don't give up just yet. Some memory
125 				 * modules will select the page but not
126 				 * ack the command. Check which page is
127 				 * selected now.
128 				 */
129 				if (ee1004_get_current_page() == page)
130 					status = 0;
131 			}
132 			if (status < 0) {
133 				dev_err(dev, "Failed to select page %d (%d)\n",
134 					page, status);
135 				mutex_unlock(&ee1004_bus_lock);
136 				return status;
137 			}
138 			dev_dbg(dev, "Selected page %d\n", page);
139 			ee1004_current_page = page;
140 		}
141 
142 		status = ee1004_eeprom_read(client, buf, off, count);
143 		if (status < 0) {
144 			mutex_unlock(&ee1004_bus_lock);
145 			return status;
146 		}
147 		buf += status;
148 		off += status;
149 		count -= status;
150 
151 		if (off == EE1004_PAGE_SIZE) {
152 			page++;
153 			off = 0;
154 		}
155 	}
156 
157 	mutex_unlock(&ee1004_bus_lock);
158 
159 	return requested;
160 }
161 
162 static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
163 
164 static struct bin_attribute *ee1004_attrs[] = {
165 	&bin_attr_eeprom,
166 	NULL
167 };
168 
169 BIN_ATTRIBUTE_GROUPS(ee1004);
170 
171 static int ee1004_probe(struct i2c_client *client,
172 			const struct i2c_device_id *id)
173 {
174 	int err, cnr = 0;
175 	const char *slow = NULL;
176 
177 	/* Make sure we can operate on this adapter */
178 	if (!i2c_check_functionality(client->adapter,
179 				     I2C_FUNC_SMBUS_READ_BYTE |
180 				     I2C_FUNC_SMBUS_READ_I2C_BLOCK)) {
181 		if (i2c_check_functionality(client->adapter,
182 				     I2C_FUNC_SMBUS_READ_BYTE |
183 				     I2C_FUNC_SMBUS_READ_WORD_DATA))
184 			slow = "word";
185 		else if (i2c_check_functionality(client->adapter,
186 				     I2C_FUNC_SMBUS_READ_BYTE |
187 				     I2C_FUNC_SMBUS_READ_BYTE_DATA))
188 			slow = "byte";
189 		else
190 			return -EPFNOSUPPORT;
191 	}
192 
193 	/* Use 2 dummy devices for page select command */
194 	mutex_lock(&ee1004_bus_lock);
195 	if (++ee1004_dev_count == 1) {
196 		for (cnr = 0; cnr < 2; cnr++) {
197 			ee1004_set_page[cnr] = i2c_new_dummy_device(client->adapter,
198 						EE1004_ADDR_SET_PAGE + cnr);
199 			if (IS_ERR(ee1004_set_page[cnr])) {
200 				dev_err(&client->dev,
201 					"address 0x%02x unavailable\n",
202 					EE1004_ADDR_SET_PAGE + cnr);
203 				err = PTR_ERR(ee1004_set_page[cnr]);
204 				goto err_clients;
205 			}
206 		}
207 	} else if (i2c_adapter_id(client->adapter) !=
208 		   i2c_adapter_id(ee1004_set_page[0]->adapter)) {
209 		dev_err(&client->dev,
210 			"Driver only supports devices on a single I2C bus\n");
211 		err = -EOPNOTSUPP;
212 		goto err_clients;
213 	}
214 
215 	/* Remember current page to avoid unneeded page select */
216 	err = ee1004_get_current_page();
217 	if (err < 0)
218 		goto err_clients;
219 	ee1004_current_page = err;
220 	dev_dbg(&client->dev, "Currently selected page: %d\n",
221 		ee1004_current_page);
222 	mutex_unlock(&ee1004_bus_lock);
223 
224 	dev_info(&client->dev,
225 		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
226 		 EE1004_EEPROM_SIZE);
227 	if (slow)
228 		dev_notice(&client->dev,
229 			   "Falling back to %s reads, performance will suffer\n",
230 			   slow);
231 
232 	return 0;
233 
234  err_clients:
235 	if (--ee1004_dev_count == 0) {
236 		for (cnr--; cnr >= 0; cnr--) {
237 			i2c_unregister_device(ee1004_set_page[cnr]);
238 			ee1004_set_page[cnr] = NULL;
239 		}
240 	}
241 	mutex_unlock(&ee1004_bus_lock);
242 
243 	return err;
244 }
245 
246 static int ee1004_remove(struct i2c_client *client)
247 {
248 	int i;
249 
250 	/* Remove page select clients if this is the last device */
251 	mutex_lock(&ee1004_bus_lock);
252 	if (--ee1004_dev_count == 0) {
253 		for (i = 0; i < 2; i++) {
254 			i2c_unregister_device(ee1004_set_page[i]);
255 			ee1004_set_page[i] = NULL;
256 		}
257 	}
258 	mutex_unlock(&ee1004_bus_lock);
259 
260 	return 0;
261 }
262 
263 /*-------------------------------------------------------------------------*/
264 
265 static struct i2c_driver ee1004_driver = {
266 	.driver = {
267 		.name = "ee1004",
268 		.dev_groups = ee1004_groups,
269 	},
270 	.probe = ee1004_probe,
271 	.remove = ee1004_remove,
272 	.id_table = ee1004_ids,
273 };
274 module_i2c_driver(ee1004_driver);
275 
276 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
277 MODULE_AUTHOR("Jean Delvare");
278 MODULE_LICENSE("GPL");
279