Skip to contentSkip to conversation

nvmem: Handle reading cells with bit offset > 8

PATCH New Sven Peter 1 participant 2 messages Nov 12, 2022 NVMEM framework

lore
Share & tools
lore

Sven Peter

author
#0 lore raw

PATCH 1/2 nvmem: Handle reading cells with bit offset > 8

Some nvmem controllers like Apple's eFuses or Nintendo's OTP have a cell size that's larger than one byte. Consumers may however still need access to a subset of bits that start after the first byte. Handle this inside nvmem_shift_read_buffer_in_place().

---
 drivers/nvmem/core.c | 14 +++++++++++---
 1 file changed, 11 insertions(+), 3 deletions(-)
drivers/nvmem/core.c +11 -3
index 321d7d63e068..a5b8d6989f8e 100644
@@ -1379,15 +1379,23 @@ EXPORT_SYMBOL_GPL(nvmem_cell_put);
 static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void *buf)
 {
 	u8 *p, *b;
-	int i, extra, bit_offset = cell->bit_offset;
+	int i, padding, extra, bit_offset = cell->bit_offset;
+	int bytes = cell->bytes;
 
 	p = b = buf;
 	if (bit_offset) {
+		padding = bit_offset/8;
+		if (padding) {
+			memmove(buf, buf + padding, bytes - padding);
+			bit_offset -= BITS_PER_BYTE * padding;
+			bytes -= padding;
+		}
+
 		/* First shift */
 		*b++ >>= bit_offset;
 
 		/* setup rest of the bytes if any */
-		for (i = 1; i < cell->bytes; i++) {
+		for (i = 1; i < bytes; i++) {
 			/* Get bits from next byte and shift them towards msb */
 			*p |= *b << (BITS_PER_BYTE - bit_offset);
 
@@ -1400,7 +1408,7 @@ static void nvmem_shift_read_buffer_in_place(struct nvmem_cell_entry *cell, void
 	}
 
 	/* result fits in less bytes */
-	extra = cell->bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
+	extra = bytes - DIV_ROUND_UP(cell->nbits, BITS_PER_BYTE);
 	while (--extra >= 0)
 		*p-- = 0;
 
Signature · 1 line
2.25.1
author
#1 lore raw

PATCH 2/2 nvmem: Handle reading nbits from cells with word_size > 8 correctly

Some nvmem controllers like Apple's eFuses or Nintendo's OTP have a cell word size that's larger than one byte and cannot read anything smaller. Round up cell->bytes correctly when accessing a subset of bits.

---
 drivers/nvmem/core.c | 8 ++++----
 1 file changed, 4 insertions(+), 4 deletions(-)
drivers/nvmem/core.c +4 -4
index a5b8d6989f8e..6906de15b9b8 100644
@@ -470,8 +470,8 @@ static int nvmem_cell_info_to_nvmem_cell_entry_nodup(struct nvmem_device *nvmem,
 	cell->np = info->np;
 
 	if (cell->nbits)
-		cell->bytes = DIV_ROUND_UP(cell->nbits + cell->bit_offset,
-					   BITS_PER_BYTE);
+		cell->bytes = round_up(DIV_ROUND_UP(cell->nbits + cell->bit_offset,
+					   BITS_PER_BYTE), nvmem->word_size);
 
 	if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
 		dev_err(&nvmem->dev,
@@ -718,9 +718,9 @@ static int nvmem_add_cells_from_of(struct nvmem_device *nvmem)
 		}
 
 		if (cell->nbits)
-			cell->bytes = DIV_ROUND_UP(
+			cell->bytes = round_up(DIV_ROUND_UP(
 					cell->nbits + cell->bit_offset,
-					BITS_PER_BYTE);
+					BITS_PER_BYTE), nvmem->word_size);
 
 		if (!IS_ALIGNED(cell->offset, nvmem->stride)) {
 			dev_err(dev, "cell %s unaligned to nvmem stride %d\n",
Signature · 1 line
2.25.1