From bc6b72e051ad2c046a563f2ec3ab974531b36d33 Mon Sep 17 00:00:00 2001
From: Grzegorz Daniluk <grzegorz.daniluk@cern.ch>
Date: Fri, 3 Aug 2012 16:55:45 +0200
Subject: [PATCH] check FMC EEPROM presence before doing anything on it

---
 dev/eeprom.c      | 21 ++++++++++++++++++++-
 include/eeprom.h  |  2 ++
 shell/cmd_calib.c |  2 +-
 shell/shell.c     |  6 +++---
 4 files changed, 26 insertions(+), 5 deletions(-)

diff --git a/dev/eeprom.c b/dev/eeprom.c
index 0014165..a1af13c 100644
--- a/dev/eeprom.c
+++ b/dev/eeprom.c
@@ -45,12 +45,26 @@
  * ------------------------------------------------
  */
 
+uint8_t has_eeprom = 0;
+
+uint8_t eeprom_present(uint8_t i2cif, uint8_t i2c_addr)
+{
+	has_eeprom = 1;	
+	if( !mi2c_devprobe(i2cif, i2c_addr) )
+		if( !mi2c_devprobe(i2cif, i2c_addr) )
+			has_eeprom = 0;
+
+	return 0;
+}
 
 int eeprom_read(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset, uint8_t *buf, size_t size)
 {
 	int i;
 	unsigned char c;
 
+	if(!has_eeprom)
+		return -1;
+
   mi2c_start(i2cif);
   if(mi2c_put_byte(i2cif, i2c_addr << 1) < 0)
   {
@@ -77,6 +91,9 @@ int eeprom_write(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset, uint8_t *buf,
 {
 	int i, busy;
 
+	if(!has_eeprom)
+		return -1;
+
 	for(i=0;i<size;i++)
 	{
 	 	mi2c_start(i2cif);
@@ -227,7 +244,9 @@ int8_t eeprom_init_purge(uint8_t i2cif, uint8_t i2c_addr)
   uint16_t used = 0xffff, i;
   uint16_t pattern = 0xff;
 
-  eeprom_read(i2cif, i2c_addr, EE_BASE_INIT, &used, sizeof(used));
+  if( eeprom_read(i2cif, i2c_addr, EE_BASE_INIT, &used, sizeof(used)) != sizeof(used) )
+		return EE_RET_I2CERR;
+
   if(used==0xffff) used=0;
   for(i=0; i<used; ++i)
     eeprom_write(i2cif, i2c_addr, EE_BASE_INIT+sizeof(used)+i, &pattern, 1);
diff --git a/include/eeprom.h b/include/eeprom.h
index 978add3..3d5ca9e 100644
--- a/include/eeprom.h
+++ b/include/eeprom.h
@@ -17,6 +17,7 @@ extern int32_t sfp_alpha;
 extern int32_t sfp_deltaTx;
 extern int32_t sfp_deltaRx;
 extern uint32_t cal_phase_transition;
+extern uint8_t has_eeprom;
 
 struct s_sfpinfo
 {
@@ -27,6 +28,7 @@ struct s_sfpinfo
   uint8_t chksum;
 } __attribute__((__packed__));
 
+uint8_t eeprom_present(uint8_t i2cif, uint8_t i2c_addr);
 int eeprom_read(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset, uint8_t *buf, size_t size);
 int eeprom_write(uint8_t i2cif, uint8_t i2c_addr, uint32_t offset, uint8_t *buf, size_t size);
 
diff --git a/shell/cmd_calib.c b/shell/cmd_calib.c
index 743eae4..e8b3e0c 100644
--- a/shell/cmd_calib.c
+++ b/shell/cmd_calib.c
@@ -22,7 +22,7 @@ int cmd_calib(const char *args[])
 	}
 	else if( !args[0] )
 	{
-		if( eeprom_phtrans(WRPC_FMC_I2C, FMC_EEPROM_ADR, &trans, 0) )
+		if( eeprom_phtrans(WRPC_FMC_I2C, FMC_EEPROM_ADR, &trans, 0) >0 )
 		{
 			mprintf("Found phase transition in EEPROM: %dps\n", trans);
 			cal_phase_transition = trans;
diff --git a/shell/shell.c b/shell/shell.c
index 4dbd300..5a4729f 100644
--- a/shell/shell.c
+++ b/shell/shell.c
@@ -6,6 +6,7 @@
 #include "uart.h"
 #include "syscon.h"
 #include "shell.h"
+#include "eeprom.h"
 
 #define SH_MAX_LINE_LEN 80	
 #define SH_MAX_ARGS 8
@@ -274,9 +275,8 @@ int shell_boot_script(void)
   uint8_t next=0;
 
   //first check if EEPROM is really there
-  if( !mi2c_devprobe(WRPC_FMC_I2C, FMC_EEPROM_ADR) )
-    if( !mi2c_devprobe(WRPC_FMC_I2C, FMC_EEPROM_ADR) )
-      return -1;
+	eeprom_present(WRPC_FMC_I2C, FMC_EEPROM_ADR);
+	if(!has_eeprom) return -1;
 
   while(1)
   {
-- 
GitLab