xref: /openbmc/linux/drivers/misc/eeprom/ee1004.c (revision 7693b77a)
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 static const struct of_device_id ee1004_of_match[] = {
62 	{ .compatible = "atmel,at30tse004a" },
63 	{}
64 };
65 MODULE_DEVICE_TABLE(of, ee1004_of_match);
66 
67 /*-------------------------------------------------------------------------*/
68 
ee1004_get_current_page(struct ee1004_bus * bus)69 static int ee1004_get_current_page(struct ee1004_bus *bus)
70 {
71 	int err;
72 
73 	err = i2c_smbus_read_byte(bus->set_page_clients[0]);
74 	if (err == -ENXIO) {
75 		/* Nack means page 1 is selected */
76 		return 1;
77 	}
78 	if (err < 0) {
79 		/* Anything else is a real error, bail out */
80 		return err;
81 	}
82 
83 	/* Ack means page 0 is selected, returned value meaningless */
84 	return 0;
85 }
86 
ee1004_set_current_page(struct ee1004_bus * bus,int page)87 static int ee1004_set_current_page(struct ee1004_bus *bus, int page)
88 {
89 	int ret;
90 
91 	if (page == bus->page)
92 		return 0;
93 
94 	/* Data is ignored */
95 	ret = i2c_smbus_write_byte(bus->set_page_clients[page], 0x00);
96 
97 	/*
98 	 * Don't give up just yet. Some memory modules will select the page
99 	 * but not ack the command. Check which page is selected now.
100 	 */
101 	if (ret == -ENXIO && ee1004_get_current_page(bus) == page)
102 		ret = 0;
103 	if (ret < 0)
104 		return ret;
105 
106 	bus->page = page;
107 	return 0;
108 }
109 
ee1004_eeprom_read(struct i2c_client * client,struct ee1004_bus * bus,char * buf,unsigned int offset,size_t count)110 static ssize_t ee1004_eeprom_read(struct i2c_client *client, struct ee1004_bus *bus, char *buf,
111 				  unsigned int offset, size_t count)
112 {
113 	int status, page;
114 
115 	page = offset >> EE1004_PAGE_SHIFT;
116 	offset &= (1 << EE1004_PAGE_SHIFT) - 1;
117 
118 	status = ee1004_set_current_page(bus, page);
119 	if (status) {
120 		dev_err(&client->dev, "Failed to select page %d (%d)\n", page, status);
121 		return status;
122 	}
123 
124 	/* Can't cross page boundaries */
125 	if (offset + count > EE1004_PAGE_SIZE)
126 		count = EE1004_PAGE_SIZE - offset;
127 
128 	if (count > I2C_SMBUS_BLOCK_MAX)
129 		count = I2C_SMBUS_BLOCK_MAX;
130 
131 	return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
132 }
133 
eeprom_read(struct file * filp,struct kobject * kobj,struct bin_attribute * bin_attr,char * buf,loff_t off,size_t count)134 static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
135 			   struct bin_attribute *bin_attr,
136 			   char *buf, loff_t off, size_t count)
137 {
138 	struct i2c_client *client = kobj_to_i2c_client(kobj);
139 	struct ee1004_bus *bus = i2c_get_clientdata(client);
140 	size_t requested = count;
141 	int ret = 0;
142 
143 	/*
144 	 * Read data from chip, protecting against concurrent access to
145 	 * other EE1004 SPD EEPROMs on the same adapter.
146 	 */
147 	mutex_lock(&bus->lock);
148 
149 	while (count) {
150 		ret = ee1004_eeprom_read(client, bus, buf, off, count);
151 		if (ret < 0)
152 			goto out;
153 
154 		buf += ret;
155 		off += ret;
156 		count -= ret;
157 	}
158 out:
159 	mutex_unlock(&bus->lock);
160 
161 	return ret < 0 ? ret : requested;
162 }
163 
164 static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
165 
166 static struct bin_attribute *ee1004_attrs[] = {
167 	&bin_attr_eeprom,
168 	NULL
169 };
170 
171 BIN_ATTRIBUTE_GROUPS(ee1004);
172 
ee1004_bus_unregister(struct ee1004_bus * bus)173 static void ee1004_bus_unregister(struct ee1004_bus *bus)
174 {
175 	i2c_unregister_device(bus->set_page_clients[1]);
176 	i2c_unregister_device(bus->set_page_clients[0]);
177 }
178 
ee1004_bus_release(struct kref * kref)179 static void ee1004_bus_release(struct kref *kref)
180 {
181 	struct ee1004_bus *bus = container_of(kref, struct ee1004_bus, kref);
182 
183 	ee1004_bus_unregister(bus);
184 
185 	mutex_lock(&ee1004_busses_lock);
186 	list_del(&bus->list);
187 	mutex_unlock(&ee1004_busses_lock);
188 
189 	kfree(bus);
190 }
191 
ee1004_bus_initialize(struct ee1004_bus * bus,struct i2c_adapter * adapter)192 static int ee1004_bus_initialize(struct ee1004_bus *bus, struct i2c_adapter *adapter)
193 {
194 	bus->set_page_clients[0] = i2c_new_dummy_device(adapter, EE1004_ADDR_SET_PAGE0);
195 	if (IS_ERR(bus->set_page_clients[0]))
196 		return PTR_ERR(bus->set_page_clients[0]);
197 
198 	bus->set_page_clients[1] = i2c_new_dummy_device(adapter, EE1004_ADDR_SET_PAGE1);
199 	if (IS_ERR(bus->set_page_clients[1])) {
200 		i2c_unregister_device(bus->set_page_clients[0]);
201 		return PTR_ERR(bus->set_page_clients[1]);
202 	}
203 
204 	bus->page = ee1004_get_current_page(bus);
205 	if (bus->page < 0) {
206 		ee1004_bus_unregister(bus);
207 		return bus->page;
208 	}
209 
210 	kref_init(&bus->kref);
211 	list_add(&bus->list, &ee1004_busses);
212 	mutex_init(&bus->lock);
213 	bus->adapter = adapter;
214 
215 	return 0;
216 }
217 
ee1004_probe(struct i2c_client * client)218 static int ee1004_probe(struct i2c_client *client)
219 {
220 	struct ee1004_bus *bus;
221 	bool found = false;
222 	int rc = 0;
223 
224 	/* Make sure we can operate on this adapter */
225 	if (!i2c_check_functionality(client->adapter,
226 				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
227 	    !i2c_check_functionality(client->adapter,
228 				     I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
229 		return -EPFNOSUPPORT;
230 
231 	mutex_lock(&ee1004_busses_lock);
232 	list_for_each_entry(bus, &ee1004_busses, list) {
233 		if (bus->adapter == client->adapter) {
234 			kref_get(&bus->kref);
235 			found = true;
236 			break;
237 		}
238 	}
239 
240 	if (!found) {
241 		bus = kzalloc(sizeof(*bus), GFP_KERNEL);
242 		if (!bus) {
243 			rc = -ENOMEM;
244 			goto unlock;
245 		}
246 
247 		rc = ee1004_bus_initialize(bus, client->adapter);
248 		if (rc) {
249 			kfree(bus);
250 			goto unlock;
251 		}
252 	}
253 
254 	i2c_set_clientdata(client, bus);
255 
256 	dev_info(&client->dev,
257 		 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
258 		 EE1004_EEPROM_SIZE);
259 
260 unlock:
261 	mutex_unlock(&ee1004_busses_lock);
262 	return rc;
263 }
264 
ee1004_remove(struct i2c_client * client)265 static void ee1004_remove(struct i2c_client *client)
266 {
267 	struct ee1004_bus *bus = i2c_get_clientdata(client);
268 
269 	kref_put(&bus->kref, ee1004_bus_release);
270 }
271 
272 /*-------------------------------------------------------------------------*/
273 
274 static struct i2c_driver ee1004_driver = {
275 	.driver = {
276 		.name = "ee1004",
277 		.dev_groups = ee1004_groups,
278 		.of_match_table = ee1004_of_match,
279 	},
280 	.probe = ee1004_probe,
281 	.remove = ee1004_remove,
282 	.id_table = ee1004_ids,
283 };
284 module_i2c_driver(ee1004_driver);
285 
286 MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
287 MODULE_AUTHOR("Jean Delvare");
288 MODULE_LICENSE("GPL");
289