Newer
Older
// SPDX-License-Identifier: GPL-2.0-or-later
/*
* Copyright (C) 2019 CERN
* Author: Federico Vaga <federico.vaga@cern.ch>
*/
#include <linux/kernel.h>
#include <linux/module.h>
#include <linux/memory.h>
#include <linux/fmc.h>
#include "fmc-internal.h"
#include "fmc-compat.h"
#if KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE
#include <linux/nvmem-consumer.h>
#endif
#define FRU_EEPROM_NAME "fru_eeprom"
/**
* Default EEPROM type according to the standard.
*/
#define FMC_EEPROM_TYPE_DEFAULT "24c02"
#if KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE
const struct property_entry at24_24c02[AT24_NUM_PROPERTIES] = {
PROPERTY_ENTRY_U32("size", 256),
PROPERTY_ENTRY_U32("pagesize", 8),
PROPERTY_ENTRY_U32("address-width", 16),
{ }
};
#else
static void fmc_slot_eeprom_setup(struct memory_accessor *macc, void *context)
{
struct fmc_slot *slot = context;
slot->macc = macc;
}
/**
* Default configuration for AT24C02 EEPROM type
*/
static const struct at24_platform_data at24_24c02 = {
.byte_len = 256,
.page_size = 8,
.flags = 0,
.setup = fmc_slot_eeprom_setup,
};
#endif /* KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE */
* Initialize I2C EEPROM info with standard values
*/
static void fmc_slot_eeprom_init(struct fmc_slot *slot,
struct i2c_board_info *info,
const char *name)
{
strncpy(info->type, name, I2C_NAME_SIZE - 1);
#if KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE
info->properties = slot->at24_data;
#else
}
static void fmc_slot_eeprom_init_default(struct fmc_slot *slot,
struct i2c_board_info *info)
{
memset(info, 0, sizeof(*info));
fmc_slot_eeprom_init(slot, info, FMC_EEPROM_TYPE_DEFAULT);
#if KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE
memcpy(&slot->at24_data, &at24_24c02, sizeof(slot->at24_data));
#else
memcpy(slot->at24_data, &at24_24c02, sizeof(slot->at24_data));
#endif
* @slot: FMC slot instance
* @buf: destination buffer
* @offset: EEPROM offset in bytes
* @count: how many bytes to read
*/
ssize_t fmc_slot_eeprom_read(struct fmc_slot *slot,
void *buf, off_t offset, size_t count)
{
int err = 0;
#if KERNEL_VERSION(4, 6, 0) <= LINUX_VERSION_CODE
int idx;
size_t len;
struct nvmem_device *nvmem;
char *nvmem_name;
/*
* TODO Check if index can be anything else than zero and,
* if yes, how to get the proper value.
*/
idx = 0;
len = strlen(dev_name(&slot->eeprom->dev)) + 2;
nvmem_name = kzalloc(len, GFP_KERNEL);
if (nvmem_name) {
snprintf(nvmem_name, len, "%s%d",
dev_name(&slot->eeprom->dev), idx);
nvmem = nvmem_device_get(&slot->dev, nvmem_name);
if (!IS_ERR_OR_NULL(nvmem)) {
err = nvmem_device_read(nvmem, offset, count, buf);
#if KERNEL_VERSION(5, 1, 0) <= LINUX_VERSION_CODE
nvmem_device_put(nvmem);
#endif
}
else {
err = PTR_ERR(nvmem);
}
/*
* TODO if we export this function, do we have to lock it when we
* use it? Think about it
*/
if (!slot->macc || !slot->macc->read)
return -ENODEV;
err = slot->macc->read(slot->macc, buf, offset, count);
#endif
return err;
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
}
EXPORT_SYMBOL(fmc_slot_eeprom_read);
/**
* Add EEPROM which will be associated to the given FMC slot
* @slot: FMC slot instance
* @info: I2C EEPROM information
*
* This creates also a symlink to the EEPROM device in the FMC slot sysfs
* directory. If this fails it will be reported on dmesg but this does not
* prevent the system from running; for this reason the function will
* succeed anyway.
*
* This should be used only by the FMC framework itself. But, this is also
* exported to other users in order to be able to support corner cases
* where the EEPROM type is not supported by the AT24 driver (in which case
* the sysfs attribute eeprom_type can be easily used).
*
* Return: 0 on success, otherwise a negative error number
*/
static int __fmc_slot_eeprom_add(struct fmc_slot *slot,
struct i2c_board_info *info)
{
struct i2c_board_info info_l = *info;
int err;
if (!fmc_slot_present(slot))
return -ENODEV;
if (!slot->adapter) {
dev_err(&slot->dev, "missing I2C adapter\n");
return -ENODEV;
}
info_l.addr = fmc_slot_i2c_address(info_l.addr, slot->ga);
#if KERNEL_VERSION(5, 8, 0) <= LINUX_VERSION_CODE
slot->eeprom = i2c_new_client_device(slot->adapter, &info_l);
if (IS_ERR(slot->eeprom))
#else
slot->eeprom = i2c_new_device(slot->adapter, &info_l);
if (!slot->eeprom)
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
return -ENODEV;
err = sysfs_create_link(&slot->dev.kobj, &slot->eeprom->dev.kobj,
FRU_EEPROM_NAME);
if (err)
dev_err(&slot->dev, "Failed to create eeprom symlink to %s\n",
dev_name(&slot->eeprom->dev));
return 0;
}
/**
* Add EEPROM to a given FMC slot
* @slot: FMC slot instance
*
* Return: 0 on success, otherwise a negative error number
*/
int fmc_slot_eeprom_add(struct fmc_slot *slot)
{
struct i2c_board_info i2c_info;
fmc_slot_eeprom_init_default(slot, &i2c_info);
return __fmc_slot_eeprom_add(slot, &i2c_info);
}
/**
* Remove EEPROM associated to the given FMC slot
* @slot: FMC slot instance
*
* This should be used only by the FMC framework itself. But, this is also
* exported to other users in order to be able to support corner cases
* where the EEPROM type is not supported by the AT24 driver (in which case
* the sysfs attribute eeprom_type can be easily used).
*/
void fmc_slot_eeprom_del(struct fmc_slot *slot)
{
if (!slot || !slot->eeprom)
return;
sysfs_remove_link(&slot->dev.kobj, FRU_EEPROM_NAME);
i2c_unregister_device(slot->eeprom);
slot->eeprom = NULL;
#if KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
}
/**
* Replace current EEPROM instance with a given one
* @slot: FMC slot instance
* @info: I2C EEPROM information
*
* Return: 0 on success, otherwise a negative error number
*/
static int fmc_slot_eeprom_replace(struct fmc_slot *slot,
struct i2c_board_info *info)
{
fmc_slot_eeprom_del(slot);
return __fmc_slot_eeprom_add(slot, info);
}
int fmc_slot_eeprom_type_set(struct fmc_slot *slot, const char *type)
{
#define FMC_EEPROM_SIZE_OFF 3
struct i2c_board_info i2c_info;
unsigned int len;
int ret;
if (strncmp(type, "24c", 3)) {
if (strncmp(type, "at24c", 5)) {
dev_err(&slot->dev,
"Invalid EEPROM type. Expected 'at24cXX' or '24cXX' (got %s)\n",
type);
return -EINVAL;
}
type += 2; /* skip 'at' */
}
ret = kstrtouint(type + FMC_EEPROM_SIZE_OFF, 10, &len);
if (ret < 0) {
dev_err(&slot->dev,
"Failed to get EEPROM size %d ('%s' %d)\n",
ret, type + FMC_EEPROM_SIZE_OFF, len);
return -EINVAL;
}
memset(&i2c_info, 0, sizeof(i2c_info));
#if KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE
memset(&slot->at24_data, 0, sizeof(slot->at24_data));
#else
memset(slot->at24_data, 0, sizeof(slot->at24_data));
#endif
len = (len * 1024) / 8;
/*
* For sizes between 1K and 16K the EEPROM uses part of the device
* address as internal memory address
*/
if (len > 131072) /* 1024K 128KiB */
return -EINVAL;
fmc_slot_eeprom_init(slot, &i2c_info, type);
#if KERNEL_VERSION(4, 6, 0) > LINUX_VERSION_CODE
if (len > 4096) /* 32K 4KiB */
slot->at24_data.flags = AT24_FLAG_ADDR16;
slot->at24_data.byte_len = len;
slot->at24_data.page_size = 1; /* 1Byte page to play safe */
slot->at24_data.setup = fmc_slot_eeprom_setup;
slot->at24_data.context = slot;
dev_dbg(&slot->dev, "%s 0x%x %d %d 0x%x\n",
i2c_info.type, i2c_info.addr,
slot->at24_data.byte_len, slot->at24_data.page_size,
slot->at24_data.flags);
#else
slot->at24_data[0] = PROPERTY_ENTRY_U32("size", len);
slot->at24_data[1] = PROPERTY_ENTRY_U32("pagesize", 1);
if (len > 4096) /* 32K 4KiB */
slot->at24_data[2] = PROPERTY_ENTRY_U32("address-width", 16);
dev_dbg(&slot->dev, "%s 0x%x %d\n",
i2c_info.type, i2c_info.addr, len);
#endif
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
return fmc_slot_eeprom_replace(slot, &i2c_info);
}
EXPORT_SYMBOL(fmc_slot_eeprom_type_set);
/**
* Check if an FMC mezzanine in the FMC slot has a valid FRU
* @slot: FMC slot to verify
*
* Return: 1 if the slot is present, otherwise 0
*/
int fmc_slot_fru_valid(struct fmc_slot *slot)
{
struct fru_common_header fru_h;
ssize_t ret;
ret = fmc_slot_eeprom_read(slot, (void *)&fru_h, 0x0,
sizeof(struct fru_common_header));
if (ret != sizeof(struct fru_common_header)) {
dev_err(&slot->dev,
"Failed while reading mezzanine's EEPROM (ret: %zd)\n",
ret);
return 0;
}
if (!fru_header_cksum_ok(&fru_h)) {
dev_warn(&slot->dev, "Invalid FRU: checksum failure\n");
return 0;
}
return 1;
}
EXPORT_SYMBOL(fmc_slot_fru_valid);
static ssize_t fru_valid_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
struct fmc_slot *slot = to_fmc_slot(dev);
return sprintf(buf, "%d\n", fmc_slot_fru_valid(slot));
}
static DEVICE_ATTR_RO(fru_valid);
const char *fmc_slot_eeprom_type_get(struct fmc_slot *slot)
{
return slot && slot->eeprom ? slot->eeprom->name : "none";
}
EXPORT_SYMBOL(fmc_slot_eeprom_type_get);
static ssize_t eeprom_type_show(struct device *dev,
struct device_attribute *attr,
char *buf)
{
return sprintf(buf, "%s\n",
fmc_slot_eeprom_type_get(to_fmc_slot(dev)));
}
static ssize_t eeprom_type_store(struct device *dev,
struct device_attribute *attr,
const char *buf, size_t count)
{
struct fmc_slot *slot = to_fmc_slot(dev);
int ret;
if (!fmc_slot_present(slot))
return -ENODEV;
ret = fmc_slot_eeprom_type_set(slot, buf);
return ret ? ret : count;
}
static DEVICE_ATTR_RW(eeprom_type);
static struct attribute *fmc_slot_eeprom_attrs[] = {
&dev_attr_fru_valid.attr,
&dev_attr_eeprom_type.attr,
NULL,
};
const struct attribute_group fmc_slot_eeprom_group = {
.attrs = fmc_slot_eeprom_attrs,
};