Skip to contentSkip to conversation

Davinci I2C code cleanup

PATCH New ksi at koi8.net 3 participants 8 messages Feb 19, 2009 – Feb 22, 2009

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 Davinci I2C code cleanup

Removed CHECK_NACK macro from Davinci I2C driver for code cleanup.

---
 cpu/arm926ejs/davinci/i2c.c |   62 +++++++++++++++++++++++++---------
 1 files changed, 45 insertions(+), 17 deletions(-)
cpu/arm926ejs/davinci/i2c.c +45 -17
index 3ba20ef..f48aae2 100644
@@ -32,14 +32,6 @@
 #include <asm/arch/hardware.h>
 #include <asm/arch/i2c_defs.h>
 
-#define CHECK_NACK() \
-	do {\
-		if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {\
-			REG(I2C_CON) = 0;\
-			return(1);\
-		}\
-	} while (0)
-
 
 static int wait_for_bus(void)
 {
@@ -179,7 +171,11 @@ int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 
 		tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
 
-		CHECK_NACK();
+		/* Check for NACK */
+		if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+			REG(I2C_CON) = 0;
+			return(1);
+		}
 
 		switch (alen) {
 			case 2:
@@ -193,7 +189,11 @@ int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 
 				tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
 
-				CHECK_NACK();
+				/* Check for NACK */
+				if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+					REG(I2C_CON) = 0;
+					return(1);
+				}
 				/* No break, fall through */
 			case 1:
 				/* Send address LSByte */
@@ -206,7 +206,11 @@ int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 
 				tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK | I2C_STAT_ARDY);
 
-				CHECK_NACK();
+				/* Check for NACK */
+				if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+					REG(I2C_CON) = 0;
+					return(1);
+				}
 
 				if (!(tmp & I2C_STAT_ARDY)) {
 					REG(I2C_CON) = 0;
@@ -224,7 +228,11 @@ int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 	for (i = 0; i < len; i++) {
 		tmp = poll_i2c_irq(I2C_STAT_RRDY | I2C_STAT_NACK | I2C_STAT_ROVR);
 
-		CHECK_NACK();
+		/* Check for NACK */
+		if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+			REG(I2C_CON) = 0;
+			return(1);
+		}
 
 		if (tmp & I2C_STAT_RRDY) {
 			buf[i] = REG(I2C_DRR);
@@ -236,7 +244,11 @@ int i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 
 	tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
 
-	CHECK_NACK();
+	/* Check for NACK */
+	if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+		REG(I2C_CON) = 0;
+		return(1);
+	}
 
 	if (!(tmp & I2C_STAT_SCD)) {
 		REG(I2C_CON) = 0;
@@ -279,7 +291,11 @@ int i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 			/* Send address MSByte */
 			tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
 
-			CHECK_NACK();
+			/* Check for NACK */
+			if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+				REG(I2C_CON) = 0;
+				return(1);
+			}
 
 			if (tmp & I2C_STAT_XRDY) {
 				REG(I2C_DXR) = (addr >> 8) & 0xff;
@@ -292,7 +308,11 @@ int i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 			/* Send address LSByte */
 			tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
 
-			CHECK_NACK();
+			/* Check for NACK */
+			if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+				REG(I2C_CON) = 0;
+				return(1);
+			}
 
 			if (tmp & I2C_STAT_XRDY) {
 				REG(I2C_DXR) = addr & 0xff;
@@ -305,7 +325,11 @@ int i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 	for (i = 0; i < len; i++) {
 		tmp = poll_i2c_irq(I2C_STAT_XRDY | I2C_STAT_NACK);
 
-		CHECK_NACK();
+		/* Check for NACK */
+		if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+			REG(I2C_CON) = 0;
+			return(1);
+		}
 
 		if (tmp & I2C_STAT_XRDY) {
 			REG(I2C_DXR) = buf[i];
@@ -316,7 +340,11 @@ int i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
 
 	tmp = poll_i2c_irq(I2C_STAT_SCD | I2C_STAT_NACK);
 
-	CHECK_NACK();
+	/* Check for NACK */
+	if (tmp & (I2C_TIMEOUT | I2C_STAT_NACK)) {
+		REG(I2C_CON) = 0;
+		return(1);
+	}
 
 	if (!(tmp & I2C_STAT_SCD)) {
 		REG(I2C_CON) = 0;
#1 lore raw

PATCH 1/1 Davinci I2C code cleanup

Quoted 6 lines · On 13:52 Thu 19 Feb , ksi at koi8.net wrote:

Removed CHECK_NACK macro from Davinci I2C driver for code cleanup.

---
 cpu/arm926ejs/davinci/i2c.c |   62 +++++++++++++++++++++++++---------
 1 files changed, 45 insertions(+), 17 deletions(-)

NACK

please explain why
IMHO duplicate code is really wrong

Best Regards,
J.

#2 lore raw

PATCH 1/1 Davinci I2C code cleanup

Dear Jean-Christophe PLAGNIOL-VILLARD,

Quoted 10 lines · In message <[email protected]> you wrote:
Quoted 6 lines · On 13:52 Thu 19 Feb , ksi at koi8.net wrote:

Removed CHECK_NACK macro from Davinci I2C driver for code cleanup.

---
 cpu/arm926ejs/davinci/i2c.c |   62 +++++++++++++++++++++++++---------
 1 files changed, 45 insertions(+), 17 deletions(-)

NACK

please explain why

Please see previous discussion about multibus I2C support.

Quoted 1 line

IMHO duplicate code is really wrong

I agree. The macro should indeed NOT be deleted, but it needs fixing. It is magically accessing the local variable "tmp" which [Quoting the CodingStyle] "...might look like a good thing, but it's confusing as hell when one reads the code and it's prone to breakage from seemingly innocent changes."

I suggest that "tmp" gets passed as argument to the macro.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
There you go man, Keep as cool as you can. It riles them  to  believe
that you perceive the web they weave. Keep on being free!
#3 lore raw

PATCH 1/1 Davinci I2C code cleanup

Quoted 18 lines · Wolfgang Denk

Dear Jean-Christophe PLAGNIOL-VILLARD,

Quoted 10 lines · In message <[email protected]> you wrote:
Quoted 6 lines · On 13:52 Thu 19 Feb , ksi at koi8.net wrote:

Removed CHECK_NACK macro from Davinci I2C driver for code cleanup.

---
 cpu/arm926ejs/davinci/i2c.c |   62 +++++++++++++++++++++++++---------
 1 files changed, 45 insertions(+), 17 deletions(-)

NACK

please explain why

Please see previous discussion about multibus I2C support.

Quoted 1 line

IMHO duplicate code is really wrong

Quoted 7 lines

I agree. The macro should indeed NOT be deleted, but it needs fixing. It is magically accessing the local variable "tmp" which [Quoting the CodingStyle] "...might look like a good thing, but it's confusing as hell when one reads the code and it's prone to breakage from seemingly innocent changes."

I suggest that "tmp" gets passed as argument to the macro.

It is _NOT_ a macro working on some variable. It is simply repeating _LITERAL_ text that not only accesses a local variable tmp but even performs a function return with an error if condition is not met. That is, why, BTW, it should not be made into an inline function because it would require additional checking of its return code where it is used (lot of places in Davinci I2C driver) that, in turn, would've added a lot of unnecessary overhead for local variable, register loads, second conditional checking etc.

It is _NOT_ supposed to be any function-like macro. It is just a fragment of _LITERAL_ text.

I don't think it is a right thing to blindly apply "Coding Style" to everything no matter if it applies to it or not.

I also NACK this :)

---
******************************************************************
* KSI at home KOI8 Net < > The impossible we do immediately. *
* Las Vegas NV, USA < > Miracles require 24-hour notice. *
******************************************************************

#4 lore raw

PATCH 1/1 Davinci I2C code cleanup

Dear ksi at koi8.net,

Quoted 4 lines · In message <[email protected]> you wrote:
Quoted 2 lines

I suggest that "tmp" gets passed as argument to the macro.

It is _NOT_ a macro working on some variable. It is simply repeating

Oh yes, of course it is. It references the variable "tmp", and this is nasty as it is not clear to the reader of the code.

If I think it would be better to rename "tmp" into "foo" the code will magically break.

Quoted 2 lines

_LITERAL_ text that not only accesses a local variable tmp but even performs a function return with an error if condition is not met. That is,

You are right. That's even worse.

Quoted 5 lines

why, BTW, it should not be made into an inline function because it would require additional checking of its return code where it is used (lot of places in Davinci I2C driver) that, in turn, would've added a lot of unnecessary overhead for local variable, register loads, second conditional checking etc.

I am sure you could come up with an efficient *and* clean solution - if you really wanted to try.

Quoted 2 lines

It is _NOT_ supposed to be any function-like macro. It is just a fragment of _LITERAL_ text.

The code as is is not acceptable and needs to be cleaned up.

Best regards,

Wolfgang Denk

Signature · 5 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
Heavier than air flying machines are impossible.
                    -- Lord Kelvin, President, Royal Society, c. 1895
#5 lore raw

PATCH 1/1 Davinci I2C code cleanup

Quoted 6 lines · Wolfgang Denk

Dear ksi at koi8.net,

Quoted 3 lines · In message <[email protected]> you wrote:
Quoted 2 lines

I suggest that "tmp" gets passed as argument to the macro.

Quoted 7 lines
Quoted 1 line

It is _NOT_ a macro working on some variable. It is simply repeating

Oh yes, of course it is. It references the variable "tmp", and this is nasty as it is not clear to the reader of the code.

If I think it would be better to rename "tmp" into "foo" the code will magically break.

It won't magically break, it will fail to compile. It is totally different case.

This macro does not come from some header file, it is in the same relatively small source file so it is easy to trace if compilation broke. Also it is not used anywhere else so there is no risk it could break anything else. And there is absolutely no reason to rename tmp to foo.

Quoted 4 lines
Quoted 2 lines

_LITERAL_ text that not only accesses a local variable tmp but even performs a function return with an error if condition is not met. That is,

You are right. That's even worse.

Yep. That's if we are to follow those dogmas to the letter, blindly. But we don't have to.

Quoted 8 lines
Quoted 5 lines

why, BTW, it should not be made into an inline function because it would require additional checking of its return code where it is used (lot of places in Davinci I2C driver) that, in turn, would've added a lot of unnecessary overhead for local variable, register loads, second conditional checking etc.

I am sure you could come up with an efficient *and* clean solution - if you really wanted to try.

There is no way you can avoid that very same code repeated several times (7 or so? didn't count them) -- that is how the driver works.

Quoted 4 lines
Quoted 2 lines

It is _NOT_ supposed to be any function-like macro. It is just a fragment of _LITERAL_ text.

The code as is is not acceptable and needs to be cleaned up.

Why not? What is wrong with that code?

---
******************************************************************
* KSI at home KOI8 Net < > The impossible we do immediately. *
* Las Vegas NV, USA < > Miracles require 24-hour notice. *
******************************************************************

#6 lore raw

PATCH 1/1 Davinci I2C code cleanup

Dear ksi at koi8.net,

Quoted 6 lines · In message <[email protected]> you wrote:
Quoted 3 lines

I am sure you could come up with an efficient *and* clean solution - if you really wanted to try.

There is no way you can avoid that very same code repeated several times (7 or so? didn't count them) -- that is how the driver works.

See above - I wrote "if you really wanted to try." You don't.

Quoted 3 lines
Quoted 1 line

The code as is is not acceptable and needs to be cleaned up.

Why not? What is wrong with that code?

I will not explain it again.

Best regards,

Wolfgang Denk

Signature · 6 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany
Phone: (+49)-8142-66989-10 Fax: (+49)-8142-66989-80 Email: wd at denx.de
A person who is more than casually interested in computers should  be
well  schooled in machine language, since it is a fundamental part of
a computer.                                           -- Donald Knuth
#7 lore raw

PATCH 1/1 Davinci I2C code cleanup

Quoted 10 lines · Wolfgang Denk

Dear ksi at koi8.net,

Quoted 6 lines · In message <[email protected]> you wrote:
Quoted 3 lines

I am sure you could come up with an efficient *and* clean solution - if you really wanted to try.

There is no way you can avoid that very same code repeated several times (7 or so? didn't count them) -- that is how the driver works.

Quoted 8 lines

See above - I wrote "if you really wanted to try." You don't.

Quoted 3 lines
Quoted 1 line

The code as is is not acceptable and needs to be cleaned up.

Why not? What is wrong with that code?

I will not explain it again.

We are splitting hairs here...

---
******************************************************************
* KSI at home KOI8 Net < > The impossible we do immediately. *
* Las Vegas NV, USA < > Miracles require 24-hour notice. *
******************************************************************