blob: bb9c4512c968c8b3435b65cc46b713f3c75e216b [file] [log] [blame]
Thomas Gleixner2874c5f2019-05-27 08:55:01 +02001// SPDX-License-Identifier: GPL-2.0-or-later
Jean Delvare3b7584a2018-10-07 19:05:00 +02002/*
3 * ee1004 - driver for DDR4 SPD EEPROMs
4 *
Jean Delvare31641e32019-05-06 15:16:56 +02005 * Copyright (C) 2017-2019 Jean Delvare
Jean Delvare3b7584a2018-10-07 19:05:00 +02006 *
7 * Based on the at24 driver:
8 * Copyright (C) 2005-2007 David Brownell
9 * Copyright (C) 2008 Wolfram Sang, Pengutronix
Jean Delvare3b7584a2018-10-07 19:05:00 +020010 */
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
Heiner Kallweit8700a732021-05-24 22:17:28 +020035#define EE1004_NUM_PAGES 2
Jean Delvare3b7584a2018-10-07 19:05:00 +020036#define EE1004_PAGE_SIZE 256
37#define EE1004_PAGE_SHIFT 8
Heiner Kallweit8700a732021-05-24 22:17:28 +020038#define EE1004_EEPROM_SIZE (EE1004_PAGE_SIZE * EE1004_NUM_PAGES)
Jean Delvare3b7584a2018-10-07 19:05:00 +020039
40/*
41 * Mutex protects ee1004_set_page and ee1004_dev_count, and must be held
42 * from page selection to end of read.
43 */
44static DEFINE_MUTEX(ee1004_bus_lock);
Heiner Kallweit8700a732021-05-24 22:17:28 +020045static struct i2c_client *ee1004_set_page[EE1004_NUM_PAGES];
Jean Delvare3b7584a2018-10-07 19:05:00 +020046static unsigned int ee1004_dev_count;
47static int ee1004_current_page;
48
49static const struct i2c_device_id ee1004_ids[] = {
50 { "ee1004", 0 },
51 { }
52};
53MODULE_DEVICE_TABLE(i2c, ee1004_ids);
54
55/*-------------------------------------------------------------------------*/
56
Jean Delvare5d458752019-05-06 15:15:39 +020057static int ee1004_get_current_page(void)
58{
59 int err;
60
61 err = i2c_smbus_read_byte(ee1004_set_page[0]);
62 if (err == -ENXIO) {
63 /* Nack means page 1 is selected */
64 return 1;
65 }
66 if (err < 0) {
67 /* Anything else is a real error, bail out */
68 return err;
69 }
70
71 /* Ack means page 0 is selected, returned value meaningless */
72 return 0;
73}
74
Heiner Kallweit66010172021-05-24 22:15:26 +020075static int ee1004_set_current_page(struct device *dev, int page)
76{
77 int ret;
78
79 if (page == ee1004_current_page)
80 return 0;
81
82 /* Data is ignored */
83 ret = i2c_smbus_write_byte(ee1004_set_page[page], 0x00);
84 /*
85 * Don't give up just yet. Some memory modules will select the page
86 * but not ack the command. Check which page is selected now.
87 */
88 if (ret == -ENXIO && ee1004_get_current_page() == page)
89 ret = 0;
90 if (ret < 0) {
91 dev_err(dev, "Failed to select page %d (%d)\n", page, ret);
92 return ret;
93 }
94
95 dev_dbg(dev, "Selected page %d\n", page);
96 ee1004_current_page = page;
97
98 return 0;
99}
100
Jean Delvare3b7584a2018-10-07 19:05:00 +0200101static ssize_t ee1004_eeprom_read(struct i2c_client *client, char *buf,
102 unsigned int offset, size_t count)
103{
Heiner Kallweit8aeacb72021-05-24 22:16:51 +0200104 int status, page;
105
106 page = offset >> EE1004_PAGE_SHIFT;
107 offset &= (1 << EE1004_PAGE_SHIFT) - 1;
108
109 status = ee1004_set_current_page(&client->dev, page);
110 if (status)
111 return status;
Jean Delvare3b7584a2018-10-07 19:05:00 +0200112
Jean Delvare3b7584a2018-10-07 19:05:00 +0200113 /* Can't cross page boundaries */
Heiner Kallweit64bf2742021-05-24 22:10:47 +0200114 if (offset + count > EE1004_PAGE_SIZE)
Jean Delvare3b7584a2018-10-07 19:05:00 +0200115 count = EE1004_PAGE_SIZE - offset;
116
Heiner Kallweit2fa7d742021-06-01 09:50:30 +0200117 return i2c_smbus_read_i2c_block_data_or_emulated(client, offset, count, buf);
Jean Delvare3b7584a2018-10-07 19:05:00 +0200118}
119
Heiner Kallweitb63866ef2021-05-19 18:34:27 +0200120static ssize_t eeprom_read(struct file *filp, struct kobject *kobj,
Jean Delvare3b7584a2018-10-07 19:05:00 +0200121 struct bin_attribute *bin_attr,
122 char *buf, loff_t off, size_t count)
123{
Heiner Kallweit7adbd542021-05-24 22:08:51 +0200124 struct i2c_client *client = kobj_to_i2c_client(kobj);
Jean Delvare3b7584a2018-10-07 19:05:00 +0200125 size_t requested = count;
Heiner Kallweit8aeacb72021-05-24 22:16:51 +0200126 int ret = 0;
Jean Delvare3b7584a2018-10-07 19:05:00 +0200127
128 /*
129 * Read data from chip, protecting against concurrent access to
130 * other EE1004 SPD EEPROMs on the same adapter.
131 */
132 mutex_lock(&ee1004_bus_lock);
133
134 while (count) {
Heiner Kallweit6f68dbd2021-05-24 22:16:05 +0200135 ret = ee1004_eeprom_read(client, buf, off, count);
136 if (ret < 0)
137 goto out;
138
139 buf += ret;
140 off += ret;
141 count -= ret;
Jean Delvare3b7584a2018-10-07 19:05:00 +0200142 }
Heiner Kallweit6f68dbd2021-05-24 22:16:05 +0200143out:
Jean Delvare3b7584a2018-10-07 19:05:00 +0200144 mutex_unlock(&ee1004_bus_lock);
145
Heiner Kallweit6f68dbd2021-05-24 22:16:05 +0200146 return ret < 0 ? ret : requested;
Jean Delvare3b7584a2018-10-07 19:05:00 +0200147}
148
Heiner Kallweitb63866ef2021-05-19 18:34:27 +0200149static BIN_ATTR_RO(eeprom, EE1004_EEPROM_SIZE);
150
151static struct bin_attribute *ee1004_attrs[] = {
152 &bin_attr_eeprom,
153 NULL
Jean Delvare3b7584a2018-10-07 19:05:00 +0200154};
155
Heiner Kallweitb63866ef2021-05-19 18:34:27 +0200156BIN_ATTRIBUTE_GROUPS(ee1004);
157
Heiner Kallweit5fe3cba2021-05-24 22:18:23 +0200158static void ee1004_cleanup(int idx)
159{
160 if (--ee1004_dev_count == 0)
161 while (--idx >= 0) {
162 i2c_unregister_device(ee1004_set_page[idx]);
163 ee1004_set_page[idx] = NULL;
164 }
165}
166
Heiner Kallweit2ac99032021-05-24 22:13:52 +0200167static int ee1004_probe(struct i2c_client *client)
Jean Delvare3b7584a2018-10-07 19:05:00 +0200168{
169 int err, cnr = 0;
Jean Delvare3b7584a2018-10-07 19:05:00 +0200170
171 /* Make sure we can operate on this adapter */
172 if (!i2c_check_functionality(client->adapter,
Heiner Kallweit08e51382021-05-24 22:12:22 +0200173 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_I2C_BLOCK) &&
174 !i2c_check_functionality(client->adapter,
175 I2C_FUNC_SMBUS_BYTE | I2C_FUNC_SMBUS_READ_BYTE_DATA))
176 return -EPFNOSUPPORT;
Jean Delvare3b7584a2018-10-07 19:05:00 +0200177
178 /* Use 2 dummy devices for page select command */
179 mutex_lock(&ee1004_bus_lock);
180 if (++ee1004_dev_count == 1) {
Heiner Kallweit8700a732021-05-24 22:17:28 +0200181 for (cnr = 0; cnr < EE1004_NUM_PAGES; cnr++) {
Heiner Kallweit3c03dad2021-05-24 22:13:12 +0200182 struct i2c_client *cl;
183
184 cl = i2c_new_dummy_device(client->adapter, EE1004_ADDR_SET_PAGE + cnr);
185 if (IS_ERR(cl)) {
186 err = PTR_ERR(cl);
Jean Delvare3b7584a2018-10-07 19:05:00 +0200187 goto err_clients;
188 }
Heiner Kallweit3c03dad2021-05-24 22:13:12 +0200189 ee1004_set_page[cnr] = cl;
Jean Delvare3b7584a2018-10-07 19:05:00 +0200190 }
Heiner Kallweitb2cd8a22021-05-24 22:14:43 +0200191
192 /* Remember current page to avoid unneeded page select */
193 err = ee1004_get_current_page();
194 if (err < 0)
195 goto err_clients;
196 dev_dbg(&client->dev, "Currently selected page: %d\n", err);
197 ee1004_current_page = err;
Heiner Kallweitb97ba922021-05-24 22:11:31 +0200198 } else if (client->adapter != ee1004_set_page[0]->adapter) {
Jean Delvare3b7584a2018-10-07 19:05:00 +0200199 dev_err(&client->dev,
200 "Driver only supports devices on a single I2C bus\n");
201 err = -EOPNOTSUPP;
202 goto err_clients;
203 }
Jean Delvare3b7584a2018-10-07 19:05:00 +0200204 mutex_unlock(&ee1004_bus_lock);
205
Jean Delvare3b7584a2018-10-07 19:05:00 +0200206 dev_info(&client->dev,
207 "%u byte EE1004-compliant SPD EEPROM, read-only\n",
208 EE1004_EEPROM_SIZE);
Jean Delvare3b7584a2018-10-07 19:05:00 +0200209
210 return 0;
211
Jean Delvare3b7584a2018-10-07 19:05:00 +0200212 err_clients:
Heiner Kallweit5fe3cba2021-05-24 22:18:23 +0200213 ee1004_cleanup(cnr);
Jean Delvare3b7584a2018-10-07 19:05:00 +0200214 mutex_unlock(&ee1004_bus_lock);
215
216 return err;
217}
218
219static int ee1004_remove(struct i2c_client *client)
220{
Jean Delvare3b7584a2018-10-07 19:05:00 +0200221 /* Remove page select clients if this is the last device */
222 mutex_lock(&ee1004_bus_lock);
Heiner Kallweit5fe3cba2021-05-24 22:18:23 +0200223 ee1004_cleanup(EE1004_NUM_PAGES);
Jean Delvare3b7584a2018-10-07 19:05:00 +0200224 mutex_unlock(&ee1004_bus_lock);
225
226 return 0;
227}
228
229/*-------------------------------------------------------------------------*/
230
231static struct i2c_driver ee1004_driver = {
232 .driver = {
233 .name = "ee1004",
Heiner Kallweitb63866ef2021-05-19 18:34:27 +0200234 .dev_groups = ee1004_groups,
Jean Delvare3b7584a2018-10-07 19:05:00 +0200235 },
Heiner Kallweit2ac99032021-05-24 22:13:52 +0200236 .probe_new = ee1004_probe,
Jean Delvare3b7584a2018-10-07 19:05:00 +0200237 .remove = ee1004_remove,
238 .id_table = ee1004_ids,
239};
Liu Shixin4292aa92020-09-18 11:02:25 +0800240module_i2c_driver(ee1004_driver);
Jean Delvare3b7584a2018-10-07 19:05:00 +0200241
242MODULE_DESCRIPTION("Driver for EE1004-compliant DDR4 SPD EEPROMs");
243MODULE_AUTHOR("Jean Delvare");
244MODULE_LICENSE("GPL");