mail archive of the barebox mailing list
 help / color / mirror / Atom feed
From: Sam Ravnborg <sam@ravnborg.org>
To: Sascha Hauer <s.hauer@pengutronix.de>
Cc: Barebox List <barebox@lists.infradead.org>
Subject: Re: [PATCH 4/6] ddr_spd: Add function to read eeprom
Date: Mon, 4 Mar 2019 13:02:52 +0100	[thread overview]
Message-ID: <20190304120252.GC16532@ravnborg.org> (raw)
In-Reply-To: <20190304113823.21535-5-s.hauer@pengutronix.de>

Hi Sascha

On Mon, Mar 04, 2019 at 12:38:21PM +0100, Sascha Hauer wrote:
> Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
> ---
>  common/ddr_spd.c  | 81 +++++++++++++++++++++++++++++++++++++++++++++++
>  include/ddr_spd.h |  6 ++++
>  2 files changed, 87 insertions(+)
> 
> diff --git a/common/ddr_spd.c b/common/ddr_spd.c
> index 9394c57fa3..a878790550 100644
> --- a/common/ddr_spd.c
> +++ b/common/ddr_spd.c
> @@ -429,3 +429,84 @@ void ddr_spd_print(uint8_t *record)
>  		printf("%02X", record[i]);
>  	printf("\n");
>  }
> +
> +#define SPD_SPA0_ADDRESS        0x36
> +#define SPD_SPA1_ADDRESS        0x37
> +
> +static int select_page(void *ctx,
> +	      int (*xfer)(void *ctx, struct i2c_msg *msgs, int num),
> +	      uint8_t addr)
> +{
> +	struct i2c_msg msg = {
> +		.addr = addr,
> +		.len = 0,
> +	};
> +	int ret;
> +
> +	ret = xfer(ctx, &msg, 1);
> +	if (ret < 0)
> +		return ret;
> +
> +	return 0;
> +}
> +
> +static int read_buf(void *ctx,
> +	      int (*xfer)(void *ctx, struct i2c_msg *msgs, int num),
> +	      uint8_t addr, int page, void *buf)
> +{
> +	uint8_t pos = 0;
> +	int ret;
> +	struct i2c_msg msg[2] = {
> +		{
> +			.addr = addr,
> +			.len = 1,
> +			.buf = &pos,
> +		}, {
> +			.addr = addr,
> +			.len = 256,
> +			.flags = I2C_M_RD,
> +			.buf = buf,
> +		}
> +	};
> +
> +	ret = select_page(ctx, xfer, page);
> +	if (ret < 0)
> +		return ret;
> +
> +	ret = xfer(ctx, msg, 2);
> +	if (ret < 0)
> +		return ret;
> +
> +	return 0;
> +}
> +
> +/**
> + * spd_read_eeprom - Read contents of a SPD EEPROM
> + * @ctx: Context pointer for the xfer function
> + * @xfer: I2C message transfer function
> + * @addr: I2C bus address for the EEPROM
> + * @buf: buffer to read the SPD data to
It is not obvious what buffer size to provide here.
And if it happens to be SPD_MEMTYPE_DDR4 then we suddenly start to
write data at offset 256 in the buffer.
So maybe add a small comment that this should always be 512 bytes.
Or even better 2 x SPD_PAGE_SIZE (defined to 256)


> + *
> + * This function takes a I2C message transfer function and reads the contents
> + * from a SPD EEPROM to the buffer provided at @buf. Returns 0 for success or a
> + * negative error code otherwise.
> + */
> +int spd_read_eeprom(void *ctx,
> +		    int (*xfer)(void *ctx, struct i2c_msg *msgs, int num),
> +		    uint8_t addr, void *buf)
> +{
> +	unsigned char *buf8 = buf;
> +	int ret;
> +
> +	ret = read_buf(ctx, xfer, addr, SPD_SPA0_ADDRESS, buf);
> +	if (ret < 0)
> +		return ret;
> +
> +	if (buf8[2] == SPD_MEMTYPE_DDR4) {
> +		ret = read_buf(ctx, xfer, addr, SPD_SPA1_ADDRESS, buf + 256);
> +		if (ret < 0)
> +			return ret;
> +	}
> +
> +	return 0;
> +}
> diff --git a/include/ddr_spd.h b/include/ddr_spd.h
> index 051275141f..95d0eb04b6 100644
> --- a/include/ddr_spd.h
> +++ b/include/ddr_spd.h
> @@ -6,6 +6,8 @@
>  #ifndef _DDR_SPD_H_
>  #define _DDR_SPD_H_
>  
> +#include <i2c/i2c.h>
> +
>  /*
>   * Format from "JEDEC Standard No. 21-C,
>   * Appendix D: Rev 1.0: SPD's for DDR SDRAM
> @@ -562,4 +564,8 @@ void ddr2_spd_dump(const struct ddr2_spd_eeprom *spd);
>  int ddr3_spd_check(const struct ddr3_spd_eeprom *spd);
>  int ddr4_spd_check(const struct ddr4_spd_eeprom *spd);
>  
> +int spd_read_eeprom(void *ctx,
> +		    int (*xfer)(void *ctx, struct i2c_msg *msgs, int num),
> +		    uint8_t addr, void *buf);
> +
>  #endif /* _DDR_SPD_H_ */
> -- 
> 2.20.1
> 
> 
> _______________________________________________
> barebox mailing list
> barebox@lists.infradead.org
> http://lists.infradead.org/mailman/listinfo/barebox

_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox

  parent reply	other threads:[~2019-03-04 12:03 UTC|newest]

Thread overview: 12+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-04 11:38 [PATCH 0/6] SPD EEPROM improvements Sascha Hauer
2019-03-04 11:38 ` [PATCH 1/6] crc: import crc_itu_t() from kernel Sascha Hauer
2019-03-04 11:50   ` Sam Ravnborg
2019-03-04 13:19     ` Sascha Hauer
2019-03-04 11:38 ` [PATCH 2/6] crc: Add PBL variant for crc_itu_t() Sascha Hauer
2019-03-04 11:38 ` [PATCH 3/6] ddr_spd: Update from U-Boot Sascha Hauer
2019-03-04 11:38 ` [PATCH 4/6] ddr_spd: Add function to read eeprom Sascha Hauer
2019-03-04 11:53   ` Sam Ravnborg
2019-03-04 12:02   ` Sam Ravnborg [this message]
2019-03-04 13:22     ` Sascha Hauer
2019-03-04 11:38 ` [PATCH 5/6] ddr_spd: provide common SPD type Sascha Hauer
2019-03-04 11:38 ` [PATCH 6/6] ddr_spd: Enable in PBL Sascha Hauer

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=20190304120252.GC16532@ravnborg.org \
    --to=sam@ravnborg.org \
    --cc=barebox@lists.infradead.org \
    --cc=s.hauer@pengutronix.de \
    /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