I2C bus implementation
I am writting a driver for the I2C of the powerpc MPC82xx. I have read various implementations of other busses and I2C drivers (drivers for I2C devices that is). All the I2C drivers I have seen do something like this on a read command:
struct i2c_msg msgs[2] = {
{client->addr, 0, 1, addr},
{client->addr, I2C_M_RD, len, buf}
};
...
ret = i2c_transfer(client->adapter, msgs, 2);
..
This sequence implements a I2C read, with a 1-byte write for the slave address and a subsequent read. This is ok for a device that does direct low level communication with the bus.
In the MPC82xx however, the I2C unit does almost all the work. All the driver has to do is setup the buffers for input/output and wait for an interrupt once the operation is over. In the example above it would just setup a buffer for the outgoing data (1 byte wide), one buffer for the incoming data and issue a "start" command.
The problem is that since all I2C device drivers are written in the way above, I am having trouble writting the code for this I2C bus. The master_xfer function accepts a list of i2c_msg. So for a I2c read my driver would get 2 messages one with the first byte (address write) and one with the read buffer, although this is a single I2C transaction!
How can I glue the I2C code to this interface?
Is there a bus similar to the MPC82xx implemented already?
Can I assume that the i2c_msg list passed to the master_xfer function will always contain a single I2C transaction? This way I could setup the buffers using the i2c_mgs elements and when the list finishes, I cound issue the "I2C start" command.
--
Manousaridis Angelos
Quoted 6 lines
The problem is that since all I2C device drivers are written in the way above, I am having trouble writting the code for this I2C bus. The
master_xferfunction accepts a list ofi2c_msg. So for a I2c read my driver would get 2 messages one with the first byte (address write) and one with the read buffer, although this is a single I2C transaction!
Quoted 7 lines
How can I glue the I2C code to this interface?
Is there a bus similar to the MPC82xx implemented already?Can I assume that the
i2c_msglist passed to themaster_xferfunction will always contain a single I2C transaction? This way I could setup the buffers using thei2c_mgselements and when the list finishes, I cound issue the "I2C start" command.
You can parse the msg list for validity and if it contains anything you don't grok, reject it. That's a bit of a pain though. That's why for PowerMac (see the patches that just went in rewriting the PowerMac i2c code), I have my own low level layer that uses different semantics closer to what Apple HW provides and one stub driver that hooks to linux i2c layer. In that stub, I chose to mostly implement the "smbus" transactions. In fact, smbus transactions map to pretty much 90% of the needs of most i2c devices around here. Of course, that mean you can't use existing i2c drivers that use the master_xfer() entry and not the smbus API, but then, do you really want to use an existing i2c driver ? I find them generally of no use as they don't know anything about the specifics of the platform anyway.
Ben.
Re: I2C bus implementation (for MPC82xx)
Quoted 4 lines · Benjamin Herrenschmidt
You can parse the msg list for validity and if it contains anything you don't grok, reject it. That's a bit of a pain though. That's why for PowerMac (see the patches that just went in rewriting the PowerMac i2c code), I have my own low level layer that uses different semantics
Quoted 8 lines
closer to what Apple HW provides and one stub driver that hooks to linux i2c layer. In that stub, I chose to mostly implement the "smbus" transactions. In fact, smbus transactions map to pretty much 90% of the needs of most i2c devices around here. Of course, that mean you can't use existing i2c drivers that use the
master_xfer()entry and not the smbus API, but then, do you really want to use an existing i2c driver ? I find them generally of no use as they don't know anything about the specifics of the platform anyway.
Sorry, that was my misunderstanding. There is no problem with the CPM2 and the I2C framework. Attached is a patch for I2C CPM2 support. It is a (heavily) modified version of another patch I found in the linux PPC mailing list by Heiko Schocher.
For the resources I have used platform definition. In my platform I have defined:
static struct resource mpc_i2c_resources[] = {
[0] = {
.name = "i2c_int",
.start = SIU_INT_I2C,
.end = SIU_INT_I2C,
.flags = IORESOURCE_IRQ,
},
[1] = {
.name = "i2c_regs",
.start = 0x11860,
.end = 0x11860 + sizeof(i2c_cpm2_t) - 1,
.flags = IORESOURCE_MEM,
},
[2] = {
.name = "i2c_pram_base",
.start = PROFF_I2C_BASE,
.end = PROFF_I2C_BASE + 1,
.flags = IORESOURCE_MEM,
},
};The memory addresses are offsets from DPRAM base. Later on initialization, the platform specific code adds the DPRAM base address to them.
Here is the patch. I don't know what I have to do in order for this patch to be included in the kernel. Code for powerPC is contributed directly to the kernel maintainers according to the linux PPC site
(http://www.penguinppc.org/kernel/).
drivers/i2c/busses/Kconfig +10 -0
@@ -270,6 +270,16 @@ This driver can also be built as a module. If so, the module will be called i2c-mpc. +config I2C_CPM2 + tristate "MPC CPM2" + depends on I2C && PPC32 && CPM2 + help + If you say yes to this option, support will be included for the + I2C interface on PPC with CPM2 + + This driver can also be built as a module. If so, the module + will be called i2c-cpm2. + config I2C_NFORCE2 tristate "Nvidia nForce2, nForce3 and nForce4" depends on I2C && PCI
drivers/i2c/busses/Makefile +1 -0
@@ -43,6 +43,7 @@ obj-$(CONFIG_I2C_VOODOO3) += i2c-voodoo3.o obj-$(CONFIG_SCx200_ACB) += scx200_acb.o obj-$(CONFIG_SCx200_I2C) += scx200_i2c.o +obj-$(CONFIG_I2C_CPM2) += i2c-cpm2.o ifeq ($(CONFIG_I2C_DEBUG_BUS),y) EXTRA_CFLAGS += -DDEBUG
drivers/i2c/busses/i2c-cpm2.c +450 -0
450 lines not on this page · raw message
include/asm-ppc/cpm2.h +1 -0
@@ -139,6 +139,7 @@#define BD_SC_BR ((ushort)0x0020)/* Break received */ #define BD_SC_FR ((ushort)0x0010)/* Framing error */ #define BD_SC_PR ((ushort)0x0008)/* Parity error */ +#define BD_SC_NAK ((ushort)0x0004)/* No acknowledge */ #define BD_SC_OV ((ushort)0x0002)/* Overrun */ #define BD_SC_CD ((ushort)0x0001)/* ?? */
include/linux/i2c-id.h +3 -0
@@ -224,6 +224,9 @@/* --- PowerPC on-chip adapters */ #define I2C_HW_OCP 0x120000/* IBM on-chip I2C adapter */ +/* --- MPC82xx PowerPC adapters */ +#define I2C_HW_MPC82xx 0x100002/* MPC82xx I2C adapter */ +/* --- Broadcom SiByte adapters */ #define I2C_HW_SIBYTE 0x150000
--
Manousaridis Angelos