xref: /openbmc/linux/drivers/misc/eeprom/ee1004.c (revision cb99ac69075d67f56ca417ffb52a2c6b02c2bd42)
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/err.h>
13 #include <linux/i2c.h>
14 #include <linux/init.h>
15 #include <linux/kernel.h>
16 #include <linux/list.h>
17 #include <linux/mod_devicetable.h>
18 #include <linux/module.h>
19 #include <linux/mutex.h>
20 
21 /*
22  * DDR4 memory modules use special EEPROMs following the Jedec EE1004
23  * specification. These are 512-byte EEPROMs using a single I2C address
24  * in the 0x50-0x57 range for data. One of two 256-byte page is selected
25  * by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
26  *
27  * Therefore we need to request these 2 additional addresses, and serialize
28  * access to all such EEPROMs with a single mutex.
29  *
30  * We assume it is safe to read up to 32 bytes at once from these EEPROMs.
31  * We use SMBus access even if I2C is available, these EEPROMs are small
32  * enough, and reading from them infrequent enough, that we favor simplicity
33  * over performance.
34  */
35 
36 #define EE1004_ADDR_SET_PAGE0		0x36
37 #define EE1004_ADDR_SET_PAGE1		0x37
38 #define EE1004_NUM_PAGES		2
39 #define EE1004_PAGE_SIZE		256
40 #define EE1004_PAGE_SHIFT		8
41 #define EE1004_EEPROM_SIZE		(EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
42 
43 struct ee1004_bus {
44 	struct kref kref;
45 	struct list_head list;
46 	struct mutex lock;
47 	struct i2c_adapter *adapter;
48 	struct i2c_client *set_page_clients[EE1004_NUM_PAGES];
49 	int page;
50 };
51 
52 static LIST_HEAD(ee1004_busses);
53 static DEFINE_MUTEX(ee1004_busses_lock);
54 
55 static const struct i2c_device_id ee1004_ids[] = {
56 	{ "ee1004", 0 },
57 	{ }
58 };
59 MODULE_DEVICE_TABLE(i2c, ee1004_ids);
60 
61 /*-------------------------------------------------------------------------*/
62 
63 static int ee1004_get_current_page(struct ee1004_bus *bus)
64 {
65 	int err;
66 
67 	err = i2c_smbus_read_byte(bus->set_page_clients[0]);
68 	if (err == -ENXIO) {
69 		/* Nack means page 1 is selected */
70 		return 1;
71 	}
72 	if (err < 0) {
73 		/* Anything else is a real error, bail out */
74 		return err;
75 	}
76 
77 	/* Ack means page 0 is selected, returned value meaningless */
78 	return 0;
79 }
80 
81 static int ee1004_set_current_page(struct ee1004_bus *bus, int page)
82 {
83 	int ret;
84 
85 	if (page == bus->page)
86 		return 0;
87 
88 	/* Data is ignored */
89 	ret = i2c_smbus_write_byte(bus->set_page_clients[page], 0x00);
90 
91 	/*
92 	 * Don't give up just yet. Some memory modules will select the page
93 	 * but not ack the command. Check which page is selected now.
94 	 */
95 	if (ret == -ENXIO && ee1004_get_current_page(bus) == page)
96 		ret = 0;
97 	if (ret < 0)
98 		return ret;
99 
100 	bus->page = page;
101 	return 0;
102 }
103 
104 static ssize_t ee1004_eeprom_read(struct i2c_client *client, struct ee1004_bus *bus, char *buf,
105 				  unsigned int offset, size_t count)
106 {
107 	int status, page;
108 
109 	page = offset >> EE1004_PAGE_SHIFT;
110 	offset &= (1 << EE1004_PAGE_SHIFT) - 1;
111 
112 	status = ee1004_set_current_page(bus, page);
113 	if (status) {
114 		dev_err(&client->dev, "Failed to select page %d (%d)\n", page, status);
115 		return status;
116 	}
117 
118 	/* Can't cross page boundaries */
119 	if (offset + count > EE1004_PAGE_SIZE)
120 		count = EE1004_PAGE_SIZE - offset;
121 
122 	if (count > I2C_SMBUS_BLOCK_MAX)
123 		count = I2C_SMBUS_BLOCK_MAX;
124 
125 	return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
126 }
127 
128 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
129 			   struct bin_attribute *bin_attr,
130 			   char *buf, loff_t off, size_t count)
131 {
132 	struct i2c_client *client = kobj_to_i2c_client(kobj);
133 	struct ee1004_bus *bus = i2c_get_clientdata(client);
134 	size_t requested = count;
135 	int ret = 0;
136 
137 	/*
138 	 * Read data from chip, protecting against concurrent access to
139 	 * other EE1004 SPD EEPROMs on the same adapter.
140 	 */
141 	mutex_lock(&bus->lock);
142 
143 	while (count) {
144 		ret = ee1004_eeprom_read(client, bus, buf, off, count);
145 		if (ret < 0)
146 			goto out;
147 
148 		buf += ret;
149 		off += ret;
150 		count -= ret;
151 	}
152 out:
153 	mutex_unlock(&bus->lock);
154 
155 	return ret < 0 ? ret : requested;
156 }
157 
158 static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
159 
160 static struct bin_attribute *ee1004_attrs[] = {
161 	&bin_attr_eeprom,
162 	NULL
163 };
164 
165 BIN_ATTRIBUTE_GROUPS(ee1004);
166 
167 static void ee1004_bus_unregister(struct ee1004_bus *bus)
168 {
169 	i2c_unregister_device(bus->set_page_clients[1]);
170 	i2c_unregister_device(bus->set_page_clients[0]);
171 }
172 
173 static void ee1004_bus_release(struct kref *kref)
174 {
175 	struct ee1004_bus *bus = container_of(kref, struct ee1004_bus, kref);
176 
177 	ee1004_bus_unregister(bus);
178 
179 	mutex_lock(&ee1004_busses_lock);
180 	list_del(&bus->list);
181 	mutex_unlock(&ee1004_busses_lock);
182 
183 	kfree(bus);
184 }
185 
186 static int ee1004_bus_initialize(struct ee1004_bus *bus, struct i2c_adapter *adapter)
187 {
188 	bus->set_page_clients[0] = i2c_new_dummy_device(adapter, EE1004_ADDR_SET_PAGE0);
189 	if (IS_ERR(bus->set_page_clients[0]))
190 		return PTR_ERR(bus->set_page_clients[0]);
191 
192 	bus->set_page_clients[1] = i2c_new_dummy_device(adapter, EE1004_ADDR_SET_PAGE1);
193 	if (IS_ERR(bus->set_page_clients[1])) {
194 		i2c_unregister_device(bus->set_page_clients[0]);
195 		return PTR_ERR(bus->set_page_clients[1]);
196 	}
197 
198 	bus->page = ee1004_get_current_page(bus);
199 	if (bus->page < 0) {
200 		ee1004_bus_unregister(bus);
201 		return bus->page;
202 	}
203 
204 	kref_init(&bus->kref);
205 	list_add(&bus->list, &ee1004_busses);
206 	mutex_init(&bus->lock);
207 	bus->adapter = adapter;
208 
209 	return 0;
210 }
211 
212 static int ee1004_probe(struct i2c_client *client)
213 {
214 	struct ee1004_bus *bus;
215 	bool found = false;
216 	int rc = 0;
217 
218 	/* Make sure we can operate on this adapter */
219 	if (!i2c_check_functionality(client->adapter,
220 				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
221 	    !i2c_check_functionality(client->adapter,
222 				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
223 		return -EPFNOSUPPORT;
224 
225 	mutex_lock(&ee1004_busses_lock);
226 	list_for_each_entry(bus, &ee1004_busses, list) {
227 		if (bus->adapter == client->adapter) {
228 			kref_get(&bus->kref);
229 			found = true;
230 			break;
231 		}
232 	}
233 
234 	if (!found) {
235 		bus = kzalloc(sizeof(*bus), GFP_KERNEL);
236 		if (!bus) {
237 			rc = -ENOMEM;
238 			goto unlock;
239 		}
240 
241 		rc = ee1004_bus_initialize(bus, client->adapter);
242 		if (rc) {
243 			kfree(bus);
244 			goto unlock;
245 		}
246 	}
247 
248 	i2c_set_clientdata(client, bus);
249 
250 	dev_info(&client->dev,
251 		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
252 		 EE1004_EEPROM_SIZE);
253 
254 unlock:
255 	mutex_unlock(&ee1004_busses_lock);
256 	return rc;
257 }
258 
259 static void ee1004_remove(struct i2c_client *client)
260 {
261 	struct ee1004_bus *bus = i2c_get_clientdata(client);
262 
263 	kref_put(&bus->kref, ee1004_bus_release);
264 }
265 
266 /*-------------------------------------------------------------------------*/
267 
268 static struct i2c_driver ee1004_driver = {
269 	.driver = {
270 		.name = "ee1004",
271 		.dev_groups = ee1004_groups,
272 	},
273 	.probe = ee1004_probe,
274 	.remove = ee1004_remove,
275 	.id_table = ee1004_ids,
276 };
277 module_i2c_driver(ee1004_driver);
278 
279 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
280 MODULE_AUTHOR("Jean Delvare");
281 MODULE_LICENSE("GPL");
282