projects/linux·blob master · refs

351 lines · 8.6 KiB

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/device.h>
13#include <linux/i2c.h>
14#include <linux/init.h>
15#include <linux/kernel.h>
16#include <linux/module.h>
17#include <linux/mutex.h>
18#include <linux/nvmem-provider.h>
19
20/*
21 * DDR4 memory modules use special EEPROMs following the Jedec EE1004
22 * specification. These are 512-byte EEPROMs using a single I2C address
23 * in the 0x50-0x57 range for data. One of two 256-byte page is selected
24 * by writing a command to I2C address 0x36 or 0x37 on the same I2C bus.
25 *
26 * Therefore we need to request these 2 additional addresses, and serialize
27 * access to all such EEPROMs with a single mutex.
28 *
29 * We assume it is safe to read up to 32 bytes at once from these EEPROMs.
30 * We use SMBus access even if I2C is available, these EEPROMs are small
31 * enough, and reading from them infrequent enough, that we favor simplicity
32 * over performance.
33 */
34
35#define EE1004_MAX_BUSSES 8
36#define EE1004_ADDR_SET_PAGE 0x36
37#define EE1004_NUM_PAGES 2
38#define EE1004_PAGE_SIZE 256
39#define EE1004_PAGE_SHIFT 8
40#define EE1004_EEPROM_SIZE (EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
41
42/*
43 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
44 * from page selection to end of read.
45 */
46static DEFINE_MUTEX(ee1004_bus_lock);
47
48static struct ee1004_bus_data {
49 struct i2c_adapter *adap;
50 struct i2c_client *set_page[EE1004_NUM_PAGES];
51 unsigned int dev_count;
52 int current_page;
53} ee1004_bus_data[EE1004_MAX_BUSSES];
54
55static const struct i2c_device_id ee1004_ids[] = {
56 { "ee1004" },
57 { }
58};
59MODULE_DEVICE_TABLE(i2c, ee1004_ids);
60
61/*-------------------------------------------------------------------------*/
62
63static struct ee1004_bus_data *ee1004_get_bus_data(struct i2c_adapter *adap)
64{
65 int i;
66
67 for (i = 0; i < EE1004_MAX_BUSSES; i++)
68 if (ee1004_bus_data[i].adap == adap)
69 return ee1004_bus_data + i;
70
71 /* If not existent yet, create new entry */
72 for (i = 0; i < EE1004_MAX_BUSSES; i++)
73 if (!ee1004_bus_data[i].adap) {
74 ee1004_bus_data[i].adap = adap;
75 return ee1004_bus_data + i;
76 }
77
78 return NULL;
79}
80
81static int ee1004_get_current_page(struct ee1004_bus_data *bd)
82{
83 int err;
84
85 err = i2c_smbus_read_byte(bd->set_page[0]);
86 if (err == -ENXIO) {
87 /* Nack means page 1 is selected */
88 return 1;
89 }
90 if (err < 0) {
91 /* Anything else is a real error, bail out */
92 return err;
93 }
94
95 /* Ack means page 0 is selected, returned value meaningless */
96 return 0;
97}
98
99static int ee1004_set_current_page(struct i2c_client *client, int page)
100{
101 struct ee1004_bus_data *bd = i2c_get_clientdata(client);
102 int ret;
103
104 if (page == bd->current_page)
105 return 0;
106
107 /* Data is ignored */
108 ret = i2c_smbus_write_byte(bd->set_page[page], 0x00);
109 /*
110 * Don't give up just yet. Some memory modules will select the page
111 * but not ack the command. Check which page is selected now.
112 */
113 if (ret == -ENXIO && ee1004_get_current_page(bd) == page)
114 ret = 0;
115 if (ret < 0) {
116 dev_err(&client->dev, "Failed to select page %d (%d)\n", page, ret);
117 return ret;
118 }
119
120 dev_dbg(&client->dev, "Selected page %d\n", page);
121 bd->current_page = page;
122
123 return 0;
124}
125
126static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
127 unsigned int offset, size_t count)
128{
129 int status, page;
130
131 page = offset >> EE1004_PAGE_SHIFT;
132 offset &= (1 << EE1004_PAGE_SHIFT) - 1;
133
134 status = ee1004_set_current_page(client, page);
135 if (status)
136 return status;
137
138 /* Can't cross page boundaries */
139 if (offset + count > EE1004_PAGE_SIZE)
140 count = EE1004_PAGE_SIZE - offset;
141
142 if (count > I2C_SMBUS_BLOCK_MAX)
143 count = I2C_SMBUS_BLOCK_MAX;
144
145 return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
146}
147
148static int ee1004_read(void *priv, unsigned int off, void *val, size_t count)
149{
150 struct i2c_client *client = priv;
151 char *buf = val;
152 int ret;
153
154 if (unlikely(!count))
155 return count;
156
157 if (off + count > EE1004_EEPROM_SIZE)
158 return -EINVAL;
159
160 /*
161 * Read data from chip, protecting against concurrent access to
162 * other EE1004 SPD EEPROMs on the same adapter.
163 */
164 mutex_lock(&ee1004_bus_lock);
165
166 while (count) {
167 ret = ee1004_eeprom_read(client, buf, off, count);
168 if (ret < 0) {
169 mutex_unlock(&ee1004_bus_lock);
170 return ret;
171 }
172
173 buf += ret;
174 off += ret;
175 count -= ret;
176 }
177
178 mutex_unlock(&ee1004_bus_lock);
179
180 return 0;
181}
182
183static void ee1004_probe_temp_sensor(struct i2c_client *client)
184{
185 struct i2c_board_info info = { .type = "jc42" };
186 unsigned short addr = 0x18 | (client->addr & 7);
187 unsigned short addr_list[] = { addr, I2C_CLIENT_END };
188 u8 data[2];
189 int ret;
190
191 /* byte 14, bit 7 is set if temp sensor is present */
192 ret = ee1004_eeprom_read(client, data, 14, 1);
193 if (ret != 1)
194 return;
195
196 if (!(data[0] & BIT(7))) {
197 /*
198 * If the SPD data suggests that there is no temperature
199 * sensor, it may still be there for SPD revision 1.0.
200 * See SPD Annex L, Revision 1 and 2, for details.
201 * Check DIMM type and SPD revision; if it is a DDR4
202 * with SPD revision 1.0, check the thermal sensor address
203 * and instantiate the jc42 driver if a chip is found at
204 * that address.
205 * It is not necessary to check if there is a chip at the
206 * temperature sensor address since i2c_new_scanned_device()
207 * will do that and return silently if no chip is found.
208 */
209 ret = ee1004_eeprom_read(client, data, 1, 2);
210 if (ret != 2 || data[0] != 0x10 || data[1] != 0x0c)
211 return;
212 }
213 i2c_new_scanned_device(client->adapter, &info, addr_list, NULL);
214}
215
216static void ee1004_cleanup(int idx, struct ee1004_bus_data *bd)
217{
218 if (--bd->dev_count == 0) {
219 while (--idx >= 0)
220 i2c_unregister_device(bd->set_page[idx]);
221 memset(bd, 0, sizeof(struct ee1004_bus_data));
222 }
223}
224
225static void ee1004_cleanup_bus_data(void *data)
226{
227 struct ee1004_bus_data *bd = data;
228
229 /* Remove page select clients if this is the last device */
230 mutex_lock(&ee1004_bus_lock);
231 ee1004_cleanup(EE1004_NUM_PAGES, bd);
232 mutex_unlock(&ee1004_bus_lock);
233}
234
235static int ee1004_init_bus_data(struct i2c_client *client)
236{
237 struct ee1004_bus_data *bd;
238 int err, cnr = 0;
239
240 bd = ee1004_get_bus_data(client->adapter);
241 if (!bd)
242 return dev_err_probe(&client->dev, -ENOSPC, "Only %d busses supported",
243 EE1004_MAX_BUSSES);
244
245 i2c_set_clientdata(client, bd);
246
247 if (++bd->dev_count == 1) {
248 /* Use 2 dummy devices for page select command */
249 for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
250 struct i2c_client *cl;
251
252 cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
253 if (IS_ERR(cl)) {
254 err = PTR_ERR(cl);
255 goto err_out;
256 }
257
258 bd->set_page[cnr] = cl;
259 }
260
261 /* Remember current page to avoid unneeded page select */
262 err = ee1004_get_current_page(bd);
263 if (err < 0)
264 goto err_out;
265
266 dev_dbg(&client->dev, "Currently selected page: %d\n", err);
267 bd->current_page = err;
268 }
269
270 return 0;
271
272err_out:
273 ee1004_cleanup(cnr, bd);
274
275 return err;
276}
277
278static int ee1004_probe(struct i2c_client *client)
279{
280 struct nvmem_config config = {
281 .dev = &client->dev,
282 .name = dev_name(&client->dev),
283 .id = NVMEM_DEVID_NONE,
284 .owner = THIS_MODULE,
285 .type = NVMEM_TYPE_EEPROM,
286 .read_only = true,
287 .root_only = false,
288 .reg_read = ee1004_read,
289 .size = EE1004_EEPROM_SIZE,
290 .word_size = 1,
291 .stride = 1,
292 .priv = client,
293 .compat = true,
294 .base_dev = &client->dev,
295 };
296 struct nvmem_device *ndev;
297 int err;
298
299 /* Make sure we can operate on this adapter */
300 if (!i2c_check_functionality(client->adapter,
301 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
302 !i2c_check_functionality(client->adapter,
303 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
304 return -EPFNOSUPPORT;
305
306 err = i2c_smbus_read_byte(client);
307 if (err < 0)
308 return -ENODEV;
309
310 mutex_lock(&ee1004_bus_lock);
311
312 err = ee1004_init_bus_data(client);
313 if (err < 0) {
314 mutex_unlock(&ee1004_bus_lock);
315 return err;
316 }
317
318 ee1004_probe_temp_sensor(client);
319
320 mutex_unlock(&ee1004_bus_lock);
321
322 err = devm_add_action_or_reset(&client->dev, ee1004_cleanup_bus_data,
323 i2c_get_clientdata(client));
324 if (err < 0)
325 return err;
326
327 ndev = devm_nvmem_register(&client->dev, &config);
328 if (IS_ERR(ndev))
329 return PTR_ERR(ndev);
330
331 dev_info(&client->dev,
332 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
333 EE1004_EEPROM_SIZE);
334
335 return 0;
336}
337
338/*-------------------------------------------------------------------------*/
339
340static struct i2c_driver ee1004_driver = {
341 .driver = {
342 .name = "ee1004",
343 },
344 .probe = ee1004_probe,
345 .id_table = ee1004_ids,
346};
347module_i2c_driver(ee1004_driver);
348
349MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
350MODULE_AUTHOR("Jean Delvare");
351MODULE_LICENSE("GPL");