Skip to contentSkip to conversation

6/12 Multiadapter/multibus I2C, drivers part 3

PATCH New ksi at koi8.net 3 participants 6 messages Feb 12, 2009 – Feb 19, 2009 I2C

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 6/12 Multiadapter/multibus I2C, drivers part 3

---

drivers/i2c/sm502_i2c.c +340 -0
diff -purN u-boot-i2c.orig/drivers/i2c/sm502_i2c.c u-boot-i2c/drivers/i2c/sm502_i2c.c
@@ -0,0 +1,340 @@
+/*
+ * Copyright (C) 2009 Sergey Kubushyn <[email protected]>
+ *
+ * Driver for Silicon Motion SM501/SM502 I2C interface.
+ */
+
+#include <common.h>
+
+#ifdef CONFIG_SM502_I2C
+
+#include <command.h>
+#include <i2c.h>
+
+#include <sm501-regs.h>
+#include <asm/io.h>
+
+/* I2C registers field definitions */
+/* --------------------------------*/
+/* I2C_CONTROL (R/W) */
+#define SM501_I2C_ENABLE	(1<<0)
+#define SM501_I2C_SPEED		(1<<1)
+#define SM501_I2C_START		(1<<2)
+#define SM501_I2C_IRQ_ENA	(1<<4)
+#define SM501_I2C_IRQ_ACK	(1<<5)
+#define SM501_I2C_RPT_START_ENA	(1<<6)
+/* I2C_STATUS (R/O) */
+#define SM501_I2C_BUS_BUSY	(1<<0)
+#define SM501_I2C_NACK		(1<<1)
+#define SM501_I2C_BUS_ERROR	(1<<2)
+#define SM501_I2C_XFER_DONE	(1<<3)
+/* I2C_RESET (W/O) */
+#define SM501_I2C_CLEAR		(1<<2)
+
+#define	SM501_I2C_MAX_COUNT	16
+
+#define SM501_I2C_READ		1
+#define SM501_I2C_WRITE		0
+
+#define SM501_I2C_TIMEOUT	(1<<7)
+
+#define SM501_CHECK_NACK() \
+	do {\
+		if (tmp & (SM501_I2C_NACK | SM501_I2C_BUS_ERROR)) {\
+			tmp = read_i2c_reg(SM501_I2C_CONTROL) & SM501_I2C_SPEED;\
+			write_i2c_reg(SM501_I2C_CONTROL, tmp);\
+			return(1);\
+		}\
+	} while (0)
+
+
+i2c_adap_t	sm501_i2c_adap;
+
+DECLARE_GLOBAL_DATA_PTR;
+
+
+static __inline__ u_int8_t read_i2c_reg(unsigned long offset)
+{
+	return(readb(sm501_iomem_base + SM501_I2C + offset));
+}
+
+static __inline__ void write_i2c_reg(unsigned long offset, u_int8_t data)
+{
+	writeb(data, sm501_iomem_base + SM501_I2C + offset);
+	__asm__("sync;isync;msync");
+#ifndef CONFIG_SYS_SM501_BASEADDR
+	/* Dummy read to push it through PCI */
+	readb(sm501_iomem_base + SM501_I2C + offset);
+#endif
+}
+
+static int poll_i2c_status(u_int8_t mask, int timeout)
+{
+	u_int8_t	stat;
+	int		i;
+
+	i = 0;
+
+	do {
+		stat = read_i2c_reg(SM501_I2C_STATUS);
+		if (stat & mask) {
+			return(stat);
+		}
+		udelay(1000);
+
+	} while (i++ < timeout);
+
+	return(stat | SM501_I2C_TIMEOUT);
+}
+
+static int wait_for_bus(void)
+{
+	int timeout = 10;
+
+	while ((read_i2c_reg(SM501_I2C_STATUS) & SM501_I2C_BUS_BUSY) && timeout--) {
+		udelay (1000);
+	}
+
+	if (timeout <= 0) {
+		printf ("SM501_I2C timed out in wait_for_bb: I2C_STAT=%x\n",
+			read_i2c_reg(SM501_I2C_STATUS));
+		return(1);
+	}
+	
+	return(0);
+}
+
+static void sm501_i2c_init(int speed, int slaveadd)
+{
+	unsigned long	tmpl;
+
+	/* Set GPIO pins for hardware I2C */
+	tmpl = readl(sm501_iomem_base + SM501_GPIO + SM501_GPIO_DATA_HIGH);
+	tmpl |= 0x0000c000;
+	writel(tmpl, sm501_iomem_base + SM501_GPIO + SM501_GPIO_DATA_HIGH);
+	__asm__("sync;isync;msync");
+#ifndef CONFIG_SYS_SM501_BASEADDR
+	/* Dummy read to push is through PCI */
+	readl(sm501_iomem_base + SM501_GPIO + SM501_GPIO_DATA_HIGH);
+#endif
+	
+	/* SM501/502 only allows 100 or 400 KHz speed (standard or fast) */
+	if (speed <= 100000)
+		write_i2c_reg(SM501_I2C_CONTROL, SM501_I2C_ENABLE);
+	else
+		write_i2c_reg(SM501_I2C_CONTROL, SM501_I2C_ENABLE | SM501_I2C_SPEED);
+
+	if (gd->flags & GD_FLG_RELOC) {
+		sm501_i2c_adap.speed = speed <= 100000 ? 100000 : 400000;
+		sm501_i2c_adap.init_done = 1;
+	}
+}
+
+
+static int sm501_i2c_probe(u_int8_t chip)
+{
+	u_int8_t	tmp;
+
+	/* Fail if I2C address is invalid */
+	if (chip > 0x7f) {return(1);}
+
+	tmp = read_i2c_reg(SM501_I2C_CONTROL);
+	write_i2c_reg(SM501_I2C_CONTROL, tmp & ~(SM501_I2C_START | SM501_I2C_ENABLE));
+	if (wait_for_bus()) {return(1);}
+
+	/* try to read one byte from current (or only) address */
+	write_i2c_reg(SM501_I2C_CONTROL, tmp | SM501_I2C_ENABLE);
+	write_i2c_reg(SM501_I2C_BYTE_COUNT, 0);
+	write_i2c_reg(SM501_I2C_SLAVE_ADDRESS, (chip << 1) | SM501_I2C_READ);
+	tmp = read_i2c_reg(SM501_I2C_CONTROL);
+	write_i2c_reg(SM501_I2C_CONTROL, tmp | SM501_I2C_START);
+	udelay (1000);
+
+	if (!(read_i2c_reg(SM501_I2C_STATUS) & (SM501_I2C_NACK | SM501_I2C_BUS_ERROR))) {
+		tmp = read_i2c_reg(SM501_I2C_CONTROL);
+		write_i2c_reg(SM501_I2C_CONTROL, tmp & ~(SM501_I2C_START | SM501_I2C_ENABLE));
+		if (wait_for_bus()) {return(1);}
+		return(0);
+	}
+
+	return(1);
+}
+
+
+static int sm501_i2c_read(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
+{
+	u_int8_t	tmp;
+	u_int8_t	*ptr = buf;
+	int		i, bytes_read = 0;
+
+	if ((alen < 0) || (alen > 2)) {
+		printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
+		return(1);
+	}
+
+	if (len < 0) {
+		printf("%s(): bogus length %x\n", __FUNCTION__, len);
+		return(1);
+	}
+
+	if (wait_for_bus()) {return(1);}
+
+	if (alen != 0) {
+		/* Start address phase */
+		if (alen ==  2) {
+			/* MSB goes first... */
+			write_i2c_reg(SM501_I2C_DATA, (addr >> 8) & 0xff);
+			write_i2c_reg(SM501_I2C_DATA + 1, addr & 0xff);
+		} else {
+			write_i2c_reg(SM501_I2C_DATA, addr & 0xff);
+		}
+
+		/* Now write it out without STOP condition */
+		tmp = read_i2c_reg(SM501_I2C_CONTROL);
+		tmp |= SM501_I2C_ENABLE | SM501_I2C_START | SM501_I2C_RPT_START_ENA;
+		write_i2c_reg(SM501_I2C_SLAVE_ADDRESS, (chip << 1) | SM501_I2C_WRITE);
+		write_i2c_reg(SM501_I2C_BYTE_COUNT, (u_int8_t)(alen - 1));
+		write_i2c_reg(SM501_I2C_CONTROL, tmp);
+
+		tmp = poll_i2c_status(SM501_I2C_NACK | SM501_I2C_BUS_ERROR | SM501_I2C_XFER_DONE, 2);
+
+		SM501_CHECK_NACK();
+	}
+
+	/* 
+	 * Address phase is over, now read 'len' bytes.
+	 * SM502 can read up to 16 bytes per transaction so
+	 * we will be reading in 16-bytes batches if len > 16.
+	 */
+	do {
+		i = (len - bytes_read) > 16 ? 16 : len - bytes_read;
+		tmp = read_i2c_reg(SM501_I2C_CONTROL);
+		tmp |= SM501_I2C_ENABLE | SM501_I2C_START;
+		write_i2c_reg(SM501_I2C_SLAVE_ADDRESS, (chip << 1) | SM501_I2C_READ);
+		write_i2c_reg(SM501_I2C_BYTE_COUNT, (u_int8_t)(i - 1));
+		write_i2c_reg(SM501_I2C_CONTROL, tmp);
+
+		tmp = poll_i2c_status(SM501_I2C_NACK | SM501_I2C_BUS_ERROR | SM501_I2C_XFER_DONE, 100);
+
+		SM501_CHECK_NACK();
+
+		bytes_read += i;
+		
+		for (; i > 0; i--) {
+			*ptr++ = read_i2c_reg(SM501_I2C_DATA + 16 - i);
+		}
+
+	} while (len > bytes_read);
+
+	tmp = read_i2c_reg(SM501_I2C_CONTROL);
+	write_i2c_reg(SM501_I2C_CONTROL, tmp & ~(SM501_I2C_START | SM501_I2C_ENABLE));
+
+	return(0);
+}
+
+
+static int sm501_i2c_write(u_int8_t chip, u_int32_t addr, int alen, u_int8_t *buf, int len)
+{
+	u_int8_t	tmp;
+	u_int8_t	*ptr = buf;
+	int		i, bytes_written = 0;
+
+	if ((alen < 0) || (alen > 2)) {
+		printf("%s(): bogus address length %x\n", __FUNCTION__, alen);
+		return(1);
+	}
+
+	if (len < 0) {
+		printf("%s(): bogus length %x\n", __FUNCTION__, len);
+		return(1);
+	}
+
+	if (wait_for_bus()) {return(1);}
+
+	/* 
+	 * SM502 can write up to 16 bytes per transaction so we will be
+	 * writing in 16-bytes batches if len > (16 - alen). Address is
+	 * sent first, then data hence (16 - alen)...
+	 */
+	do {
+		i = (len - bytes_written) > (16 - alen) ? 16 - alen : len - bytes_written;
+
+		/* Prepend address */
+		if (alen ==  2) {
+			/* MSB goes first... */
+			write_i2c_reg(SM501_I2C_DATA, ((addr + bytes_written) >> 8) & 0xff);
+			write_i2c_reg(SM501_I2C_DATA + 1, (addr + bytes_written) & 0xff);
+		}
+
+		if (alen == 1) {
+			write_i2c_reg(SM501_I2C_DATA, (addr + bytes_written) & 0xff);
+		}
+
+		
+		for (; i > 0; i--) {
+			write_i2c_reg(SM501_I2C_DATA + 16 + alen - i, *ptr++);
+		}
+
+		/* Push it down the bus */
+		tmp = read_i2c_reg(SM501_I2C_CONTROL);
+		tmp |= SM501_I2C_ENABLE | SM501_I2C_START;
+		write_i2c_reg(SM501_I2C_SLAVE_ADDRESS, (chip << 1) | SM501_I2C_WRITE);
+		write_i2c_reg(SM501_I2C_BYTE_COUNT, (u_int8_t)(i + alen - 1));
+		write_i2c_reg(SM501_I2C_CONTROL, tmp);
+
+		tmp = poll_i2c_status(SM501_I2C_NACK | SM501_I2C_BUS_ERROR | SM501_I2C_XFER_DONE, 100);
+
+		SM501_CHECK_NACK();
+
+		bytes_written += i;
+
+	} while (len > bytes_written);
+
+	tmp = read_i2c_reg(SM501_I2C_CONTROL);
+	write_i2c_reg(SM501_I2C_CONTROL, tmp & ~(SM501_I2C_START | SM501_I2C_ENABLE));
+
+	return(0);
+}
+
+
+static unsigned int sm501_i2c_set_bus_speed(unsigned int speed)
+{
+	u_int8_t	tmp;
+
+	tmp = read_i2c_reg(SM501_I2C_CONTROL);
+
+	/* SM501/502 only allows 100 or 400 KHz speed (standard or fast) */
+	if (speed <= 100000)
+		tmp &= ~SM501_I2C_SPEED;
+	else
+		tmp |= SM501_I2C_SPEED;
+
+	write_i2c_reg(SM501_I2C_CONTROL, tmp);
+	
+	if (gd->flags & GD_FLG_RELOC) {
+		sm501_i2c_adap.speed = speed <= 100000 ? 100000 : 400000;
+	}
+	
+	return(speed <= 100000 ? 100000 : 400000);
+}
+
+
+static unsigned int sm501_i2c_get_bus_speed(void)
+{
+	return(sm501_i2c_adap.speed);
+}
+
+
+i2c_adap_t	sm501_i2c_adap = {
+	.init		=	sm501_i2c_init,
+	.probe		=	sm501_i2c_probe,
+	.read		=	sm501_i2c_read,
+	.write		=	sm501_i2c_write,
+	.set_bus_speed	=	sm501_i2c_set_bus_speed,
+	.get_bus_speed	=	sm501_i2c_get_bus_speed,
+	.speed		=	CONFIG_SYS_SM501_I2C_SPEED,
+	.slaveaddr	=	CONFIG_SYS_SM501_I2C_SLAVE,
+	.init_done	=	0,
+	.name		=	"sm501_i2c"
+};
+#endif	/* CONFIG_SM502_I2C */
include/sm501-regs.h +394 -0

394 lines not on this page · raw message

#1 lore raw

PATCH 1/1 6/12 Multiadapter/multibus I2C, drivers part 3

Hello ksi,

Quoted 2 lines · ksi at koi8.net wrote:

Patch apply with warnings:

Applying: 6/12 Multiadapter/multibus I2C, drivers part 3 /home/hs/i2c/u-boot-i2c/.git/rebase-apply/patch:108: trailing whitespace.

/home/hs/i2c/u-boot-i2c/.git/rebase-apply/patch:125: trailing whitespace.

/home/hs/i2c/u-boot-i2c/.git/rebase-apply/patch:209: trailing whitespace.

        /*

/home/hs/i2c/u-boot-i2c/.git/rebase-apply/patch:227: trailing whitespace.

/home/hs/i2c/u-boot-i2c/.git/rebase-apply/patch:259: trailing whitespace.

        /*

warning: squelched 3 whitespace errors
warning: 8 lines add whitespace errors.

please fix

bye
Heiko

Signature · 2 lines
DENX Software Engineering GmbH,     MD: Wolfgang Denk & Detlev Zundel
HRB 165235 Munich, Office: Kirchenstr.5, D-82194 Groebenzell, Germany 
#2 lore raw

PATCH 1/1 6/12 Multiadapter/multibus I2C, drivers part 3

In reply to ksi at koi8.net

Dear ksi at koi8.net,

In message <[email protected]> you wrote:
...

drivers/i2c/sm502_i2c.c line 19

Quoted 3 lines
drivers/i2c/sm502_i2c.c +3 -0
+/* I2C registers field definitions */
+/* --------------------------------*/
+/* I2C_CONTROL (R/W) */

Incorrect multi-line comment style. Please fix.

drivers/i2c/sm502_i2c.c line 48

Quoted 8 lines
drivers/i2c/sm502_i2c.c +8 -0
+#define SM501_CHECK_NACK() \
+	do {\
+		if (tmp & (SM501_I2C_NACK | SM501_I2C_BUS_ERROR)) {\
+			tmp = read_i2c_reg(SM501_I2C_CONTROL) & SM501_I2C_SPEED;\
+			write_i2c_reg(SM501_I2C_CONTROL, tmp);\
+			return(1);\
+		}\
+	} while (0)

Macros with magic side effects (here on the variable "tmp" are stronly deprecated. Please fix this.

drivers/i2c/sm502_i2c.c line 64

Quoted 4 lines
drivers/i2c/sm502_i2c.c +4 -0
+static __inline__ void write_i2c_reg(unsigned long offset, u_int8_t data)
+{
+	writeb(data, sm501_iomem_base + SM501_I2C + offset);
+	__asm__("sync;isync;msync");

Are you sure the "sync;isync;msync" is needed? The accessor functions should make sure this is not necessary.

Also, instead of register offsets ("...base + SM501_I2C") please use a proper data structure.

drivers/i2c/sm502_i2c.c line 82

Quoted 5 lines
drivers/i2c/sm502_i2c.c +5 -0
+	do {
+		stat = read_i2c_reg(SM501_I2C_STATUS);
+		if (stat & mask) {
+			return(stat);
+		}

No curly braces for single line statements. Same goes everywhere esle, too.

And "return" is not a function - please omit the parens (here and elsewhere).

Quoted 1 line
drivers/i2c/sm502_i2c.c +1 -0
+	while ((read_i2c_reg(SM501_I2C_STATUS) & SM501_I2C_BUS_BUSY) && timeout--) {

...

include/sm501-regs.h line 1

Quoted 5 lines

diff -purN u-boot-i2c.orig/include/sm501-regs.h u-boot-i2c/include/sm501-regs.h

include/sm501-regs.h +1 -0
@@ -0,0 +1,394 @@
+/* sm501-regs.h

Incorrect multiline comment style.

...

Quoted 6 lines
include/sm501-regs.h +6 -0
+#define SM501_GPIO31_0_CONTROL		(0x000008)
+#define SM501_GPIO63_32_CONTROL		(0x00000C)
+#define SM501_DRAM_CONTROL		(0x000010)
+
+/* command list */
+#define SM501_ARBTRTN_CONTROL		(0x000014)

include/sm501-regs.h line 90

Quoted 10 lines
include/sm501-regs.h +10 -0
+
+/* command list */
+#define SM501_COMMAND_LIST_STATUS	(0x000024)
+
+/* interrupt debug */
+#define SM501_RAW_IRQ_STATUS		(0x000028)
+#define SM501_RAW_IRQ_CLEAR		(0x000028)
+#define SM501_IRQ_STATUS		(0x00002C)
+#define SM501_IRQ_MASK			(0x000030)
+#define SM501_DEBUG_CONTROL		(0x000034)

Please do not use register offsets, define a C structure instead.

Best regards,

Wolfgang Denk

Signature · 9 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
Fantasy is like alcohol - too much is bad for you, a little bit makes
the world a better place. Like  an  exercise  bicycle  it  takes  you
nowhere, but it just might tone up the muscles that will. Daydreaming
got  us  where we are today; early in our evolution we learned to let
our minds wander so well that they started coming  back  with  souve-
nirs.   - Terry Pratchett & Stephen Briggs, _The Discworld Companion_
#3 lore raw

PATCH 1/1 6/12 Multiadapter/multibus I2C, drivers part 3

Quoted 9 lines · Wolfgang Denk

Dear ksi at koi8.net,

In message <[email protected]> you wrote:
...

drivers/i2c/sm502_i2c.c line 19

Quoted 3 lines
drivers/i2c/sm502_i2c.c +3 -0
+/* I2C registers field definitions */
+/* --------------------------------*/
+/* I2C_CONTROL (R/W) */

Incorrect multi-line comment style. Please fix.

OK.

Quoted 11 lines

drivers/i2c/sm502_i2c.c line 48

Quoted 8 lines
drivers/i2c/sm502_i2c.c +8 -0
+#define SM501_CHECK_NACK() \
+	do {\
+		if (tmp & (SM501_I2C_NACK | SM501_I2C_BUS_ERROR)) {\
+			tmp = read_i2c_reg(SM501_I2C_CONTROL) & SM501_I2C_SPEED;\
+			write_i2c_reg(SM501_I2C_CONTROL, tmp);\
+			return(1);\
+		}\
+	} while (0)

Macros with magic side effects (here on the variable "tmp" are stronly deprecated. Please fix this.

There is exactly the same code in Davinci I2C driver that had been accepted and made it in the main tree. The reason behind this macro is to make source text smaller and easier to read. It is not used anywhere outside the driver file. Sure I can remove it and put that text as is anywhere where it is used (3 places in sm502_i2c.c) but will it make it any better or more readable? I seriously doubt it.

And that side effect on tmp is accounted for so there is no magic. Nothing in the code relies on tmp content after that code fragment is executed. This is just a text fragment that gets inserted literally at appropriate places.

Please let me know if you still want that changed. I will remove that standard "do {...} while(0)" wrapper and paste the "if{...}" block at appropriate places.

Are we going to do the same for Davinci I2C driver for consistency?

Quoted 7 lines

drivers/i2c/sm502_i2c.c line 64

Quoted 4 lines
drivers/i2c/sm502_i2c.c +4 -0
+static __inline__ void write_i2c_reg(unsigned long offset, u_int8_t data)
+{
+	writeb(data, sm501_iomem_base + SM501_I2C + offset);
+	__asm__("sync;isync;msync");

Are you sure the "sync;isync;msync" is needed? The accessor functions should make sure this is not necessary.

OK. It would do any harm anyways but I will remove that.

Quoted 2 lines

Also, instead of register offsets ("...base + SM501_I2C") please use a proper data structure.

Are we dropping Linux compatibility or what? That sm501-regs.h file with those offsets has been directly stolen from Linux kernel source and that's the way they do it in the kernel. I don't have any problems with making a data structure for SM501/502 I2C controller but aren't we supposed to keep parts stolen from Linux kernel as close to the source as possible?

Please advise.

Quoted 8 lines

drivers/i2c/sm502_i2c.c line 82

Quoted 5 lines
drivers/i2c/sm502_i2c.c +5 -0
+	do {
+		stat = read_i2c_reg(SM501_I2C_STATUS);
+		if (stat & mask) {
+			return(stat);
+		}

No curly braces for single line statements. Same goes everywhere esle, too.

What's wrong with bracing a single line statement? I personally think it makes code more readable and less prone for errors. But sure, I can change this, no problems...

Quoted 2 lines

And "return" is not a function - please omit the parens (here and elsewhere).

There is a lot of places where parens are used with "return" in U-Boot (and elsewhere.) C does not REQUIRE parens but also does not forbid or discourage them. I personally think it is better to use excessive parens than not to use them when they are required.

But sure I can change it if it really matters...

Quoted 9 lines
Quoted 1 line
drivers/i2c/sm502_i2c.c +1 -0
+	while ((read_i2c_reg(SM501_I2C_STATUS) & SM501_I2C_BUS_BUSY) && timeout--) {

...

include/sm501-regs.h line 1

Quoted 5 lines

diff -purN u-boot-i2c.orig/include/sm501-regs.h u-boot-i2c/include/sm501-regs.h

include/sm501-regs.h +1 -0
@@ -0,0 +1,394 @@
+/* sm501-regs.h

Incorrect multiline comment style.

This again comes from Linux kernel source. Should I fix such borrowed code?

Quoted 11 lines

...

Quoted 10 lines
include/sm501-regs.h +10 -0
+#define SM501_GPIO31_0_CONTROL		(0x000008)
+#define SM501_GPIO63_32_CONTROL		(0x00000C)
+#define SM501_DRAM_CONTROL		(0x000010)
+
+/* command list */
+#define SM501_ARBTRTN_CONTROL		(0x000014)
+
+/* command list */
+#define SM501_COMMAND_LIST_STATUS	(0x000024)
+
Quoted 8 lines

include/sm501-regs.h line 90

Quoted 6 lines
include/sm501-regs.h +6 -0
+/* interrupt debug */
+#define SM501_RAW_IRQ_STATUS		(0x000028)
+#define SM501_RAW_IRQ_CLEAR		(0x000028)
+#define SM501_IRQ_STATUS		(0x00002C)
+#define SM501_IRQ_MASK			(0x000030)
+#define SM501_DEBUG_CONTROL		(0x000034)

Please do not use register offsets, define a C structure instead.

Again, this came from Linux kernel. Should I throw it away and make my own header file? Please advise.

---
******************************************************************
* 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 6/12 Multiadapter/multibus I2C, drivers part 3

Dear ksi at koi8.net,

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

Macros with magic side effects (here on the variable "tmp" are stronly deprecated. Please fix this.

There is exactly the same code in Davinci I2C driver that had been accepted and made it in the main tree. The reason behind this macro is to make source

Sorry that the code slipped through the review then.

Quoted 1 line

text smaller and easier to read. It is not used anywhere outside the driver

That's not easier to read. Macros with side effects are dangerous. 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."

Quoted 3 lines

file. Sure I can remove it and put that text as is anywhere where it is used (3 places in sm502_i2c.c) but will it make it any better or more readable? I seriously doubt it.

I am sure about it.

And while you are at it, I would appreciate if you could also post a (separate) patch that fixes the same thing in the Davinci I2C driver you were referring to above. Thanks.

Quoted 1 line

Are we going to do the same for Davinci I2C driver for consistency?

Yes, please.

Quoted 6 lines
Quoted 2 lines

Also, instead of register offsets ("...base + SM501_I2C") please use a proper data structure.

Are we dropping Linux compatibility or what? That sm501-regs.h file with those offsets has been directly stolen from Linux kernel source and that's the way they do it in the kernel. I don't have any problems with making a

I am aware that there are areas in the Linux kernel that could need a thourough rework - like in any bigger project.

Borrowing good code from Linux is excellent. But there is no reason not to improve poor code when we run into it. Who knows, maybe one day Linux might copy code from U-Boot - now would that be a bad thing?

Quoted 2 lines

data structure for SM501/502 I2C controller but aren't we supposed to keep parts stolen from Linux kernel as close to the source as possible?

Yes, we are - when it makes sense.

If you have free cycles you could try and post such a cleanup patch for Linux too, so both keep in sync.

Quoted 10 lines
Quoted 6 lines

drivers/i2c/sm502_i2c.c line 82

Quoted 3 lines
drivers/i2c/sm502_i2c.c +3 -0
+		if (stat & mask) {
+			return(stat);
+		}

No curly braces for single line statements. Same goes everywhere esle, too.

What's wrong with bracing a single line statement? I personally think it makes code more readable and less prone for errors. But sure, I can change this, no problems...

I agree with you - my personal preference is quite often to use braces there, too. But we try all to stick to a common coding style, so everybody has to swallow his own set of bitter pills.

Quoted 9 lines
Quoted 2 lines

And "return" is not a function - please omit the parens (here and elsewhere).

There is a lot of places where parens are used with "return" in U-Boot (and elsewhere.) C does not REQUIRE parens but also does not forbid or discourage them. I personally think it is better to use excessive parens than not to use them when they are required.

But sure I can change it if it really matters...

We try to be good citicens and follow the CodingStyle, that's all...

Quoted 11 lines
Quoted 9 lines
Quoted 1 line
drivers/i2c/sm502_i2c.c +1 -0
+	while ((read_i2c_reg(SM501_I2C_STATUS) & SM501_I2C_BUS_BUSY) && timeout--) {

...

include/sm501-regs.h line 1

Quoted 5 lines

diff -purN u-boot-i2c.orig/include/sm501-regs.h u-boot-i2c/include/sm501-regs.h

include/sm501-regs.h +1 -0
@@ -0,0 +1,394 @@
+/* sm501-regs.h

Incorrect multiline comment style.

This again comes from Linux kernel source. Should I fix such borrowed code?

Since more changes are required to that file anyway: yes, please.

Quoted 4 lines
Quoted 1 line

Please do not use register offsets, define a C structure instead.

Again, this came from Linux kernel. Should I throw it away and make my own header file? Please advise.

Yes, please.

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
Harrison's Postulate:
	For every action, there is an equal and opposite criticism.
#5 lore raw

PATCH 1/1 6/12 Multiadapter/multibus I2C, drivers part 3

Quoted 25 lines · Wolfgang Denk

Dear ksi at koi8.net,

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

Macros with magic side effects (here on the variable "tmp" are stronly deprecated. Please fix this.

There is exactly the same code in Davinci I2C driver that had been accepted and made it in the main tree. The reason behind this macro is to make source

Sorry that the code slipped through the review then.

Quoted 1 line

text smaller and easier to read. It is not used anywhere outside the driver

That's not easier to read. Macros with side effects are dangerous. 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."

Quoted 3 lines

file. Sure I can remove it and put that text as is anywhere where it is used (3 places in sm502_i2c.c) but will it make it any better or more readable? I seriously doubt it.

I am sure about it.

Quoted 7 lines

And while you are at it, I would appreciate if you could also post a (separate) patch that fixes the same thing in the Davinci I2C driver you were referring to above. Thanks.

Quoted 1 line

Are we going to do the same for Davinci I2C driver for consistency?

Yes, please.

OK, posted a patch.

Quoted 7 lines
Quoted 6 lines
Quoted 2 lines

Also, instead of register offsets ("...base + SM501_I2C") please use a proper data structure.

Are we dropping Linux compatibility or what? That sm501-regs.h file with those offsets has been directly stolen from Linux kernel source and that's the way they do it in the kernel. I don't have any problems with making a

Quoted 7 lines

I am aware that there are areas in the Linux kernel that could need a thourough rework - like in any bigger project.

Borrowing good code from Linux is excellent. But there is no reason not to improve poor code when we run into it. Who knows, maybe one day Linux might copy code from U-Boot - now would that be a bad thing?

OK, will replace it with a structure, no problems. I do personally prefer a structure too; that code was against my nature but I tried to keep Linux kernel files intact.

[dd]

Quoted 7 lines
Quoted 3 lines

What's wrong with bracing a single line statement? I personally think it makes code more readable and less prone for errors. But sure, I can change this, no problems...

I agree with you - my personal preference is quite often to use braces there, too. But we try all to stick to a common coding style, so everybody has to swallow his own set of bitter pills.

OK, no problems.

Quoted 11 lines
Quoted 9 lines
Quoted 2 lines

And "return" is not a function - please omit the parens (here and elsewhere).

There is a lot of places where parens are used with "return" in U-Boot (and elsewhere.) C does not REQUIRE parens but also does not forbid or discourage them. I personally think it is better to use excessive parens than not to use them when they are required.

But sure I can change it if it really matters...

We try to be good citicens and follow the CodingStyle, that's all...

OK.

Quoted 10 lines
Quoted 10 lines
Quoted 9 lines
Quoted 1 line
drivers/i2c/sm502_i2c.c +1 -0
+	while ((read_i2c_reg(SM501_I2C_STATUS) & SM501_I2C_BUS_BUSY) && timeout--) {

...

include/sm501-regs.h line 1

Quoted 5 lines

diff -purN u-boot-i2c.orig/include/sm501-regs.h u-boot-i2c/include/sm501-regs.h

include/sm501-regs.h +1 -0
@@ -0,0 +1,394 @@
+/* sm501-regs.h

Incorrect multiline comment style.

Quoted 3 lines
Quoted 1 line

This again comes from Linux kernel source. Should I fix such borrowed code?

Since more changes are required to that file anyway: yes, please.

OK.

Quoted 6 lines
Quoted 4 lines
Quoted 1 line

Please do not use register offsets, define a C structure instead.

Again, this came from Linux kernel. Should I throw it away and make my own header file? Please advise.

Yes, please.

OK, not a big deal, will do.

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