[PATCH v3 08/13] misc: Add support for nvmem cells

Sean Anderson sean.anderson at seco.com
Mon Apr 25 17:24:09 CEST 2022



On 4/25/22 1:48 AM, Simon Glass wrote:
> Hi Sean,
> 
> On Mon, 18 Apr 2022 at 13:37, Sean Anderson <sean.anderson at seco.com> wrote:
>>
>> This adds support for "nvmem cells" as seen in Linux. The nvmem device
>> class in Linux is used for various assorted ROMs and EEPROMs. In this
>> sense, it is similar to UCLASS_MISC, but also includes
>> UCLASS_I2C_EEPROM, UCLASS_RTC, and UCLASS_MTD. New drivers corresponding
>> to a Linux-style nvmem device should be implemented as one of the
>> previously-mentioned uclasses. The nvmem API acts as a compatibility
>> layer to adapt the (slightly different) APIs of these uclasses. It also
>> handles the lookup of nvmem cells.
>>
>> While nvmem devices can be accessed directly, they are most often used
>> by reading/writing contiguous values called "cells". Cells typically
>> hold information like calibration, versions, or configuration (such as
>> mac addresses).
>>
>> nvmem devices can specify "cells" in their device tree:
>>
>>         qfprom: eeprom at 700000 {
>>                 #address-cells = <1>;
>>                 #size-cells = <1>;
>>                 reg = <0x00700000 0x100000>;
>>
>>                 /* ... */
>>
>>                 tsens_calibration: calib at 404 {
>>                         reg = <0x404 0x10>;
>>                 };
>>         };
>>
>> which can then be referenced like:
>>
>>         tsens {
>>                 /* ... */
>>                 nvmem-cells = <&tsens_calibration>;
>>                 nvmem-cell-names = "calibration";
>>         };
>>
>> The tsens driver could then read the calibration value like:
>>
>>         struct nvmem_cell cal_cell;
>>         u8 cal[16];
>>         nvmem_cell_get_by_name(dev, "calibration", &cal_cell);
>>         nvmem_cell_read(&cal_cell, cal, sizeof(cal));
>>
>> Because nvmem devices are not all of the same uclass, supported uclasses
>> must register a nvmem_interface struct. This allows CONFIG_NVMEM to be
>> enabled without depending on specific uclasses. At the moment,
>> nvmem_interface is very bare-bones, and assumes that no initialization
>> is necessary. However, this could be amended in the future.
>>
>> Although I2C_EEPROM and MISC are quite similar (and could likely be
>> unified), they present different read/write function signatures. To
>> abstract over this, NVMEM uses the same read/write signature as Linux.
>> In particular, short read/writes are not allowed, which is allowed by
>> MISC.
>>
>> The functionality implemented by nvmem cells is very similar to that
>> provided by i2c_eeprom_partition. "fixed-partition"s for eeproms does
>> not seem to have made its way into Linux or into any device tree other
>> than sandbox. It is possible that with the introduction of this API it
>> would be possible to remove it.
> 
> I still think this would be better as a separate uclass, with child
> devices created at bind time in each of the respective uclasses, like
> mmc_bind() does. Then you will see the nvmem devices in the DM tree.
> Wouldn't we want to add a command to access the nvmem devices?

We already do. E.g. the misc/rtc/eeprom commands. The problem is that
for software to access them, they would have to use misc_read/dm_rtc_read/
i2c_eeprom_read.

> This patch feels like a shortcut to me and I'm not sure of the
> benefit of that shortcut.
Well, I suppose it's because "nvmem" devices are strict subsets of
existing devices. There is no new functionality here (except adapting
between semantics like for misc). We should always be able to use the
existing API to implement support for a new underlying uclass. There
should never be device-specific read/write methods, because we can
use the existing read/write uclass methods.

What I'm trying to get at is that we sort of already have an nvmem
uclass with nvmem devices, they're just not accessible in a uniform
way. This series is trying to address the uniformity aspect. But I
don't think we need new devices for each nvmem interface, because
all they would do would take up ram/rom.

--Sean

PS. In an ideal world we'd have something like

struct nvmem_ops {
	read();
	write();
};

struct dm_rtc_ops {
	nvmem_ops nvmem;
	/* the other ops minus read/write */
};

int nvmem_read (...) {
	struct nvmem_ops *ops = cell->nvmem->ops;
	/* ... */

	return ops->read(...);
}

but unfortunately, we already have fragmented implementations.


More information about the U-Boot mailing list