U-Boot Archive on lore.kernel.org
 help / color / mirror / Atom feed
From: Christoph Niedermaier <cniedermaier@dh-electronics.com>
To: Marek Vasut <marex@denx.de>,
	"u-boot@lists.denx.de" <u-boot@lists.denx.de>
Cc: NXP i.MX U-Boot Team <uboot-imx@nxp.com>,
	Fabio Estevam <festevam@gmail.com>,
	Stefano Babic <sbabic@denx.de>, Tom Rini <trini@konsulko.com>,
	u-boot <u-boot@dh-electronics.com>
Subject: RE: [PATCH V4 2/4] arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM i.MX8MP
Date: Mon, 2 Dec 2024 17:17:26 +0000	[thread overview]
Message-ID: <25004ade364e43ef91736d2e9d178cd8@dh-electronics.com> (raw)
In-Reply-To: <f3c84d83-31ff-4408-a733-a97a2219ed33@denx.de>

From: Marek Vasut <marex@denx.de>
Sent: Sunday, December 1, 2024 7:44 PM
> On 11/28/24 3:20 PM, Christoph Niedermaier wrote:
> 
> [...]
> 
>> +++ b/board/dhelectronics/common/dh_common.c
>> @@ -7,6 +7,7 @@
>>   #include <dm.h>
>>   #include <i2c_eeprom.h>
>>   #include <net.h>
>> +#include <u-boot/crc.h>
>>
>>   #include "dh_common.h"
>>
>> @@ -30,6 +31,148 @@ int dh_get_mac_is_enabled(const char *alias)
>>   	return 0;
>>   }
>>
>> +int dh_read_eeprom_id_page(u8 *eeprom_buffer, const char *alias)
>> +{
>> +	struct eeprom_id_page *eip = (struct eeprom_id_page *)eeprom_buffer;
>> +	struct udevice *dev;
>> +	size_t payload_len;
>> +	int eeprom_size;
>> +	u16 crc16_calc;
>> +	u16 crc16_eip;
>> +	u8 crc8_calc;
>> +	ofnode node;
>> +	int ret;
>> +
>> +	node = ofnode_path(alias);
>> +
>> +	ret = uclass_get_device_by_ofnode(UCLASS_I2C_EEPROM, node, &dev);
>> +	if (ret)
>> +		return ret;
>> +
>> +	eeprom_size = i2c_eeprom_size(dev);
>> +	if (eeprom_size < 0) {
>> +		printf("%s: Error getting EEPROM ID page size! ret = %d\n", __func__, ret);
>> +		return eeprom_size;
>> +	} else if (eeprom_size == 0 || eeprom_size > DH_EEPROM_ID_PAGE_MAX_SIZE) {
> 
> The else part is not necessary, use plain:
> 
> ...
>   ...
>   return eeprom_size;
> }
> 
> if (eeprom_size == 0 || eeprom_size > DH_EEPROM_ID_PAGE_MAX_SIZE) {
> ...

I will change it in V5.

>> +		eeprom_size = DH_EEPROM_ID_PAGE_MAX_SIZE;
>> +		printf("Get invalid EEPROM ID page size %d bytes! Try to read %d bytes.\n",
>> +		       eeprom_size, DH_EEPROM_ID_PAGE_MAX_SIZE);
>> +	}
>> +
>> +	ret = i2c_eeprom_read(dev, 0x0, eeprom_buffer, eeprom_size);
>> +	if (ret) {
>> +		printf("%s: Error reading EEPROM ID page! ret = %d\n", __func__, ret);
>> +		return ret;
>> +	}
>> +
>> +	/* Validate header ID */
>> +	if (eip->hdr.id[0] != 'D' || eip->hdr.id[1] != 'H' || eip->hdr.id[2] != 'E') {
>> +		printf("%s: Error validating header ID! (got %c%c%c (0x%02x 0x%02x 0x%02x) != expected DHE)\n",
>> +		       __func__, isprint(eip->hdr.id[0]) ? eip->hdr.id[0] : '.',
>> +		       isprint(eip->hdr.id[1]) ? eip->hdr.id[1] : '.',
>> +		       isprint(eip->hdr.id[2]) ? eip->hdr.id[2] : '.',
>> +		       eip->hdr.id[0], eip->hdr.id[1], eip->hdr.id[2]);
>> +		return -EINVAL;
>> +	}
>> +
>> +	/* Validate header checksum */
>> +	crc8_calc = crc8(0xff, eeprom_buffer, offsetof(struct eeprom_id_page, hdr.crc8_hdr));
>> +	if (eip->hdr.crc8_hdr != crc8_calc) {
>> +		printf("%s: Error validating header checksum! (got 0x%02x != calc 0x%02x)\n",
>> +		       __func__, eip->hdr.crc8_hdr, crc8_calc);
>> +		return -EINVAL;
>> +	}
>> +
>> +	/*
>> +	 * Validate header version
>> +	 * The payload is defined by the version specified in the header.
>> +	 * Currently only version 0x10 is defined, so take the length of
>> +	 * the only defined payload as the payload length.
>> +	 */
>> +	if (eip->hdr.version == DH_EEPROM_ID_PAGE_V1_0) {
> 
> Invert this conditional:
> 
> ...
> if (eip->hdr.version != DH_EEPROM_ID_PAGE_V1_0) {
>   printf(...);
>   return ...;
> }
> 
> payload_len = sizeof(eip->pl);
> ...

I will change it in V5.

>> +		payload_len = sizeof(eip->pl);
>> +	} else {
>> +		printf("%s: Error validating version! (0x%02X is not supported)\n",
>> +		       __func__, eip->hdr.version);
>> +		return -EINVAL;
>> +	}
>> +
>> +	/* Validate payload checksum */
>> +	crc16_eip = (eip->hdr.crc16_pl[1] << 8) | eip->hdr.crc16_pl[0];
>> +	crc16_calc = crc16(0xffff, eeprom_buffer + sizeof(eip->hdr), payload_len);
>> +	if (crc16_eip != crc16_calc) {
>> +		printf("%s: Error validating data checksum! (got 0x%02x != calc 0x%02x)\n",
>> +		       __func__, crc16_eip, crc16_calc);
>> +		return -EINVAL;
>> +	}
>> +
>> +	return 0;
>> +}
> 
> [...]

Thanks and regards
Christoph

  reply	other threads:[~2024-12-02 17:17 UTC|newest]

Thread overview: 10+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-11-28 14:20 [PATCH V4 0/4] arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM i.MX8MP Christoph Niedermaier
2024-11-28 14:20 ` [PATCH V4 1/4] arm64: dts: imx8mp: Add aliases for the access to the EEPROM ID page node Christoph Niedermaier
2024-12-01 18:18   ` Marek Vasut
2024-11-28 14:20 ` [PATCH V4 2/4] arm64: imx8mp: Read values from M24C32-D write-lockable page on DHCOM i.MX8MP Christoph Niedermaier
2024-12-01 18:43   ` Marek Vasut
2024-12-02 17:17     ` Christoph Niedermaier [this message]
2024-11-28 14:20 ` [PATCH V4 3/4] board: dhelectronics: Sync env variable dh_som_serial_number with SN Christoph Niedermaier
2024-12-01 18:20   ` Marek Vasut
2024-12-02 17:19     ` Christoph Niedermaier
2024-11-28 14:20 ` [PATCH V4 4/4] lib: hashtable: Prevent recursive calling of callback functions Christoph Niedermaier

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=25004ade364e43ef91736d2e9d178cd8@dh-electronics.com \
    --to=cniedermaier@dh-electronics.com \
    --cc=festevam@gmail.com \
    --cc=marex@denx.de \
    --cc=sbabic@denx.de \
    --cc=trini@konsulko.com \
    --cc=u-boot@dh-electronics.com \
    --cc=u-boot@lists.denx.de \
    --cc=uboot-imx@nxp.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox