2017-06-26 19:29:45

by Eddie James

[permalink] [raw]
Subject: [PATCH 0/6] drivers/i2c: Add FSI-attached I2C master algorithm

From: "Edward A. James" <[email protected]>

This series adds an algorithm for an I2C master physically located on an FSI
slave device. The I2C master has multiple ports, each of which may be connected
to an I2C slave. Access to the I2C master registers is achieved over FSI bus.

Due to the multi-port nature of the I2C master, the driver instantiates a new
I2C adapter for each port connected to a slave. The connected ports should be
defined in the device tree under the I2C master device.

Edward A. James (6):
drivers/i2c: Add FSI-attached I2C master algorithm
drivers/i2c: Add port structure to FSI algorithm
drivers/i2c: Add transfer implementation for FSI algorithm
drivers/i2c: Add I2C master locking to FSI algorithm
drivers/i2c: Add bus recovery for FSI algorithm
Documentation/devicetree: Add FSI-attached I2C master dt bindings

Documentation/devicetree/bindings/i2c/i2c-fsi.txt | 31 +
drivers/i2c/busses/Kconfig | 11 +
drivers/i2c/busses/Makefile | 1 +
drivers/i2c/busses/i2c-fsi.c | 656 ++++++++++++++++++++++
4 files changed, 699 insertions(+)
create mode 100644 Documentation/devicetree/bindings/i2c/i2c-fsi.txt
create mode 100644 drivers/i2c/busses/i2c-fsi.c

--
1.8.3.1


2017-06-26 19:30:20

by Eddie James

[permalink] [raw]
Subject: [PATCH 5/6] drivers/i2c: Add bus recovery for FSI algorithm

From: "Edward A. James" <[email protected]>

Bus recovery should reset the engine and force clock the bus 9 times
to recover most situations.

Signed-off-by: Edward A. James <[email protected]>
---
drivers/i2c/busses/i2c-fsi.c | 76 ++++++++++++++++++++++++++++++++++++++++++++
1 file changed, 76 insertions(+)

diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
index 41193be..690589e 100644
--- a/drivers/i2c/busses/i2c-fsi.c
+++ b/drivers/i2c/busses/i2c-fsi.c
@@ -469,6 +469,80 @@ static u32 fsi_i2c_functionality(struct i2c_adapter *adap)
return I2C_FUNC_I2C | I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_10BIT_ADDR;
}

+static int fsi_i2c_low_level_recover_bus(struct fsi_i2c_master *i2c)
+{
+ int i, rc;
+ u32 mode, dummy = 0;
+
+ rc = fsi_i2c_read_reg(i2c->fsi, I2C_FSI_MODE, &mode);
+ if (rc)
+ return rc;
+
+ mode |= I2C_MODE_DIAG;
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_MODE, &mode);
+ if (rc)
+ return rc;
+
+ for (i = 0; i < 9; ++i) {
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_SCL, &dummy);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_SET_SCL, &dummy);
+ if (rc)
+ return rc;
+ }
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_SCL, &dummy);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_SDA, &dummy);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_SET_SCL, &dummy);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_SET_SDA, &dummy);
+ if (rc)
+ return rc;
+
+ mode &= ~I2C_MODE_DIAG;
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_MODE, &mode);
+
+ return rc;
+}
+
+static int fsi_i2c_recover_bus(struct i2c_adapter *adap)
+{
+ int rc;
+ u32 dummy = 0;
+ struct fsi_i2c_port *port = adap->algo_data;
+ struct fsi_i2c_master *i2c = port->master;
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_I2C, &dummy);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_dev_init(i2c);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_low_level_recover_bus(i2c);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_ERR, &dummy);
+
+ return rc;
+}
+
+static struct i2c_bus_recovery_info fsi_i2c_bus_recovery_info = {
+ .recover_bus = fsi_i2c_recover_bus,
+};
+
static const struct i2c_algorithm fsi_i2c_algorithm = {
.master_xfer = fsi_i2c_xfer,
.functionality = fsi_i2c_functionality,
@@ -516,6 +590,8 @@ static int fsi_i2c_probe(struct device *dev)
port->adapter.owner = THIS_MODULE;
port->adapter.dev.parent = dev;
port->adapter.algo = &fsi_i2c_algorithm;
+ port->adapter.bus_recovery_info =
+ &fsi_i2c_bus_recovery_info;
port->adapter.algo_data = port;
/* number ports uniquely */
port->adapter.nr = (i2c->idx * I2C_MASTER_NR_OFFSET) +
--
1.8.3.1

2017-06-26 19:30:25

by Eddie James

[permalink] [raw]
Subject: [PATCH 2/6] drivers/i2c: Add port structure to FSI algorithm

From: "Edward A. James" <[email protected]>

Add and initialize I2C adapters for each port on the FSI-attached I2C
master. Ports for each master are defined in the devicetree.

Signed-off-by: Edward A. James <[email protected]>
---
drivers/i2c/busses/i2c-fsi.c | 113 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 112 insertions(+), 1 deletion(-)

diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
index f8ea94e..95fe25a 100644
--- a/drivers/i2c/busses/i2c-fsi.c
+++ b/drivers/i2c/busses/i2c-fsi.c
@@ -30,6 +30,7 @@
#define SETFIELD(m, v, val) \
(((v) & ~(m)) | ((((typeof(v))(val)) << MASK_TO_LSH(m)) & (m)))

+#define I2C_MASTER_NR_OFFSET 100
#define I2C_DEFAULT_CLK_DIV 6

/* i2c registers */
@@ -131,9 +132,21 @@

struct fsi_i2c_master {
struct fsi_device *fsi;
+ int idx;
u8 fifo_size;
+ struct list_head ports;
+ struct ida ida;
};

+struct fsi_i2c_port {
+ struct list_head list;
+ struct i2c_adapter adapter;
+ struct fsi_i2c_master *master;
+ u16 port;
+};
+
+static DEFINE_IDA(fsi_i2c_ida);
+
static int fsi_i2c_read_reg(struct fsi_device *fsi, unsigned int reg,
u32 *data)
{
@@ -188,9 +201,44 @@ static int fsi_i2c_dev_init(struct fsi_i2c_master *i2c)
return rc;
}

+static int fsi_i2c_set_port(struct fsi_i2c_port *port)
+{
+ int rc;
+ struct fsi_device *fsi = port->master->fsi;
+ u32 mode, dummy = 0;
+ u16 old_port;
+
+ rc = fsi_i2c_read_reg(fsi, I2C_FSI_MODE, &mode);
+ if (rc)
+ return rc;
+
+ old_port = GETFIELD(I2C_MODE_PORT, mode);
+
+ if (old_port != port->port) {
+ mode = SETFIELD(I2C_MODE_PORT, mode, port->port);
+ rc = fsi_i2c_write_reg(fsi, I2C_FSI_MODE, &mode);
+ if (rc)
+ return rc;
+
+ /* reset engine when port is changed */
+ rc = fsi_i2c_write_reg(fsi, I2C_FSI_RESET_ERR, &dummy);
+ if (rc)
+ return rc;
+ }
+
+ return rc;
+}
+
static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
int num)
{
+ int rc;
+ struct fsi_i2c_port *port = adap->algo_data;
+
+ rc = fsi_i2c_set_port(port);
+ if (rc)
+ return rc;
+
return -EOPNOTSUPP;
}

@@ -207,13 +255,59 @@ static u32 fsi_i2c_functionality(struct i2c_adapter *adap)
static int fsi_i2c_probe(struct device *dev)
{
struct fsi_i2c_master *i2c;
- int rc;
+ struct fsi_i2c_port *port;
+ struct device_node *np;
+ int rc, idx;
+ u32 port_no;

i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
if (!i2c)
return -ENOMEM;

i2c->fsi = to_fsi_dev(dev);
+ i2c->idx = ida_simple_get(&fsi_i2c_ida, 1, INT_MAX, GFP_KERNEL);
+ ida_init(&i2c->ida);
+ INIT_LIST_HEAD(&i2c->ports);
+
+ if (dev->of_node) {
+ /* add adapter for each i2c port of the master */
+ for_each_child_of_node(dev->of_node, np) {
+ rc = of_property_read_u32(np, "port", &port_no);
+ if (rc || port_no > 0xFFFF)
+ continue;
+
+ /* make sure we don't overlap index with a buggy dts */
+ idx = ida_simple_get(&i2c->ida, port_no,
+ port_no + 1, GFP_KERNEL);
+ if (idx < 0)
+ continue;
+
+ port = devm_kzalloc(dev, sizeof(*port), GFP_KERNEL);
+ if (!port)
+ return -ENOMEM;
+
+ port->master = i2c;
+ port->port = (u16)port_no;
+
+ port->adapter.owner = THIS_MODULE;
+ port->adapter.dev.parent = dev;
+ port->adapter.algo = &fsi_i2c_algorithm;
+ port->adapter.algo_data = port;
+ /* number ports uniquely */
+ port->adapter.nr = (i2c->idx * I2C_MASTER_NR_OFFSET) +
+ port_no;
+
+ snprintf(port->adapter.name,
+ sizeof(port->adapter.name),
+ "i2cp_fsi-%u", port_no);
+
+ rc = i2c_add_numbered_adapter(&port->adapter);
+ if (rc < 0)
+ return rc;
+
+ list_add(&port->list, &i2c->ports);
+ }
+ }

rc = fsi_i2c_dev_init(i2c);
if (rc)
@@ -224,6 +318,22 @@ static int fsi_i2c_probe(struct device *dev)
return 0;
}

+static int fsi_i2c_remove(struct device *dev)
+{
+ struct fsi_i2c_master *i2c = dev_get_drvdata(dev);
+ struct fsi_i2c_port *port;
+
+ list_for_each_entry(port, &i2c->ports, list) {
+ i2c_del_adapter(&port->adapter);
+ }
+
+ ida_destroy(&i2c->ida);
+
+ ida_simple_remove(&fsi_i2c_ida, i2c->idx);
+
+ return 0;
+}
+
static const struct fsi_device_id fsi_i2c_ids[] = {
{ FSI_ENGID_I2C, FSI_VERSION_ANY },
{ 0 }
@@ -235,6 +345,7 @@ static int fsi_i2c_probe(struct device *dev)
.name = "i2cm-fsi",
.bus = &fsi_bus_type,
.probe = fsi_i2c_probe,
+ .remove = fsi_i2c_remove,
},
};

--
1.8.3.1

2017-06-26 19:30:24

by Eddie James

[permalink] [raw]
Subject: [PATCH 1/6] drivers/i2c: Add FSI-attached I2C master algorithm

From: "Edward A. James" <[email protected]>

Add register definitions for FSI-attached I2C master and functions to
access those registers over FSI. Add an FSI driver so that our I2C bus
is probed up during an FSI scan.

Signed-off-by: Edward A. James <[email protected]>
---
drivers/i2c/busses/Kconfig | 11 ++
drivers/i2c/busses/Makefile | 1 +
drivers/i2c/busses/i2c-fsi.c | 245 +++++++++++++++++++++++++++++++++++++++++++
3 files changed, 257 insertions(+)
create mode 100644 drivers/i2c/busses/i2c-fsi.c

diff --git a/drivers/i2c/busses/Kconfig b/drivers/i2c/busses/Kconfig
index ed7106e..47bba71 100644
--- a/drivers/i2c/busses/Kconfig
+++ b/drivers/i2c/busses/Kconfig
@@ -1260,4 +1260,15 @@ config I2C_OPAL
This driver can also be built as a module. If so, the module will be
called as i2c-opal.

+config I2C_FSI
+ tristate "FSI I2C driver"
+ depends on FSI
+ help
+ Driver for FSI bus attached I2C masters. These are I2C masters that
+ are connected to the system over a FSI bus, instead of the more
+ common PCI or MMIO interface.
+
+ This driver can also be built as a module. If so, the module will be
+ called as i2c-fsi.
+
endmenu
diff --git a/drivers/i2c/busses/Makefile b/drivers/i2c/busses/Makefile
index c89a431..f99ea5d 100644
--- a/drivers/i2c/busses/Makefile
+++ b/drivers/i2c/busses/Makefile
@@ -126,5 +126,6 @@ obj-$(CONFIG_I2C_PCA_ISA) += i2c-pca-isa.o
obj-$(CONFIG_I2C_SIBYTE) += i2c-sibyte.o
obj-$(CONFIG_I2C_XGENE_SLIMPRO) += i2c-xgene-slimpro.o
obj-$(CONFIG_SCx200_ACB) += scx200_acb.o
+obj-$(CONFIG_I2C_FSI) += i2c-fsi.o

ccflags-$(CONFIG_I2C_DEBUG_BUS) := -DDEBUG
diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
new file mode 100644
index 0000000..f8ea94e
--- /dev/null
+++ b/drivers/i2c/busses/i2c-fsi.c
@@ -0,0 +1,245 @@
+/*
+ * Copyright 2017 IBM Corporation
+ *
+ * Eddie James <[email protected]>
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation; either version
+ * 2 of the License, or (at your option) any later version.
+ */
+
+#include <linux/fsi.h>
+#include <linux/i2c.h>
+#include <linux/jiffies.h>
+#include <linux/module.h>
+#include <linux/of.h>
+#include <linux/sched.h>
+#include <linux/semaphore.h>
+#include <linux/wait.h>
+
+#define FSI_ENGID_I2C 0x7
+
+/* Find left shift from first set bit in m */
+#define MASK_TO_LSH(m) (__builtin_ffsll(m) - 1ULL)
+
+/* Extract field m from v */
+#define GETFIELD(m, v) (((v) & (m)) >> MASK_TO_LSH(m))
+
+/* Set field m of v to val */
+#define SETFIELD(m, v, val) \
+ (((v) & ~(m)) | ((((typeof(v))(val)) << MASK_TO_LSH(m)) & (m)))
+
+#define I2C_DEFAULT_CLK_DIV 6
+
+/* i2c registers */
+#define I2C_FSI_FIFO 0x00
+#define I2C_FSI_CMD 0x04
+#define I2C_FSI_MODE 0x08
+#define I2C_FSI_WATER_MARK 0x0C
+#define I2C_FSI_INT_MASK 0x10
+#define I2C_FSI_INT_COND 0x14
+#define I2C_FSI_OR_INT_MASK 0x14
+#define I2C_FSI_INTS 0x18
+#define I2C_FSI_AND_INT_MASK 0x18
+#define I2C_FSI_STAT 0x1C
+#define I2C_FSI_RESET_I2C 0x1C
+#define I2C_FSI_ESTAT 0x20
+#define I2C_FSI_RESET_ERR 0x20
+#define I2C_FSI_RESID_LEN 0x24
+#define I2C_FSI_SET_SCL 0x24
+#define I2C_FSI_PORT_BUSY 0x28
+#define I2C_FSI_RESET_SCL 0x2C
+#define I2C_FSI_SET_SDA 0x30
+#define I2C_FSI_RESET_SDA 0x34
+
+/* cmd register */
+#define I2C_CMD_WITH_START 0x80000000
+#define I2C_CMD_WITH_ADDR 0x40000000
+#define I2C_CMD_RD_CONT 0x20000000
+#define I2C_CMD_WITH_STOP 0x10000000
+#define I2C_CMD_FORCELAUNCH 0x08000000
+#define I2C_CMD_ADDR 0x00fe0000
+#define I2C_CMD_READ 0x00010000
+#define I2C_CMD_LEN 0x0000ffff
+
+/* mode register */
+#define I2C_MODE_CLKDIV 0xffff0000
+#define I2C_MODE_PORT 0x0000fc00
+#define I2C_MODE_ENHANCED 0x00000008
+#define I2C_MODE_DIAG 0x00000004
+#define I2C_MODE_PACE_ALLOW 0x00000002
+#define I2C_MODE_WRAP 0x00000001
+
+/* watermark register */
+#define I2C_WATERMARK_HI 0x0000f000
+#define I2C_WATERMARK_LO 0x000000f0
+
+#define I2C_FIFO_HI_LVL 4
+#define I2C_FIFO_LO_LVL 4
+
+/* interrupt register */
+#define I2C_INT_INV_CMD 0x00008000
+#define I2C_INT_PARITY 0x00004000
+#define I2C_INT_BE_OVERRUN 0x00002000
+#define I2C_INT_BE_ACCESS 0x00001000
+#define I2C_INT_LOST_ARB 0x00000800
+#define I2C_INT_NACK 0x00000400
+#define I2C_INT_DAT_REQ 0x00000200
+#define I2C_INT_CMD_COMP 0x00000100
+#define I2C_INT_STOP_ERR 0x00000080
+#define I2C_INT_BUSY 0x00000040
+#define I2C_INT_IDLE 0x00000020
+
+#define I2C_INT_ENABLE 0x0000ff80
+#define I2C_INT_ERR 0x0000fcc0
+
+/* status register */
+#define I2C_STAT_INV_CMD 0x80000000
+#define I2C_STAT_PARITY 0x40000000
+#define I2C_STAT_BE_OVERRUN 0x20000000
+#define I2C_STAT_BE_ACCESS 0x10000000
+#define I2C_STAT_LOST_ARB 0x08000000
+#define I2C_STAT_NACK 0x04000000
+#define I2C_STAT_DAT_REQ 0x02000000
+#define I2C_STAT_CMD_COMP 0x01000000
+#define I2C_STAT_STOP_ERR 0x00800000
+#define I2C_STAT_MAX_PORT 0x000f0000
+#define I2C_STAT_ANY_INT 0x00008000
+#define I2C_STAT_SCL_IN 0x00000800
+#define I2C_STAT_SDA_IN 0x00000400
+#define I2C_STAT_PORT_BUSY 0x00000200
+#define I2C_STAT_SELF_BUSY 0x00000100
+#define I2C_STAT_FIFO_COUNT 0x000000ff
+
+#define I2C_STAT_ERR 0xfc800000
+#define I2C_STAT_ANY_RESP 0xff800000
+
+/* extended status register */
+#define I2C_ESTAT_FIFO_SZ 0xff000000
+#define I2C_ESTAT_SCL_IN_SY 0x00008000
+#define I2C_ESTAT_SDA_IN_SY 0x00004000
+#define I2C_ESTAT_S_SCL 0x00002000
+#define I2C_ESTAT_S_SDA 0x00001000
+#define I2C_ESTAT_M_SCL 0x00000800
+#define I2C_ESTAT_M_SDA 0x00000400
+#define I2C_ESTAT_HI_WATER 0x00000200
+#define I2C_ESTAT_LO_WATER 0x00000100
+#define I2C_ESTAT_PORT_BUSY 0x00000080
+#define I2C_ESTAT_SELF_BUSY 0x00000040
+#define I2C_ESTAT_VERSION 0x0000001f
+
+struct fsi_i2c_master {
+ struct fsi_device *fsi;
+ u8 fifo_size;
+};
+
+static int fsi_i2c_read_reg(struct fsi_device *fsi, unsigned int reg,
+ u32 *data)
+{
+ int rc;
+ __be32 data_be;
+
+ rc = fsi_device_read(fsi, reg, &data_be, sizeof(data_be));
+ if (rc)
+ return rc;
+
+ *data = be32_to_cpu(data_be);
+
+ return 0;
+}
+
+static int fsi_i2c_write_reg(struct fsi_device *fsi, unsigned int reg,
+ u32 *data)
+{
+ __be32 data_be = cpu_to_be32(*data);
+
+ return fsi_device_write(fsi, reg, &data_be, sizeof(data_be));
+}
+
+static int fsi_i2c_dev_init(struct fsi_i2c_master *i2c)
+{
+ int rc;
+ u32 mode = I2C_MODE_ENHANCED, extended_status, watermark = 0;
+ u32 interrupt = 0;
+
+ /* since we use polling, disable interrupts */
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_INT_MASK, &interrupt);
+ if (rc)
+ return rc;
+
+ mode = SETFIELD(I2C_MODE_CLKDIV, mode, I2C_DEFAULT_CLK_DIV);
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_MODE, &mode);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_read_reg(i2c->fsi, I2C_FSI_ESTAT, &extended_status);
+ if (rc)
+ return rc;
+
+ i2c->fifo_size = GETFIELD(I2C_ESTAT_FIFO_SZ, extended_status);
+ watermark = SETFIELD(I2C_WATERMARK_HI, watermark,
+ i2c->fifo_size - I2C_FIFO_HI_LVL);
+ watermark = SETFIELD(I2C_WATERMARK_LO, watermark,
+ I2C_FIFO_LO_LVL);
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_WATER_MARK, &watermark);
+
+ return rc;
+}
+
+static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
+ int num)
+{
+ return -EOPNOTSUPP;
+}
+
+static u32 fsi_i2c_functionality(struct i2c_adapter *adap)
+{
+ return I2C_FUNC_I2C | I2C_FUNC_PROTOCOL_MANGLING | I2C_FUNC_10BIT_ADDR;
+}
+
+static const struct i2c_algorithm fsi_i2c_algorithm = {
+ .master_xfer = fsi_i2c_xfer,
+ .functionality = fsi_i2c_functionality,
+};
+
+static int fsi_i2c_probe(struct device *dev)
+{
+ struct fsi_i2c_master *i2c;
+ int rc;
+
+ i2c = devm_kzalloc(dev, sizeof(*i2c), GFP_KERNEL);
+ if (!i2c)
+ return -ENOMEM;
+
+ i2c->fsi = to_fsi_dev(dev);
+
+ rc = fsi_i2c_dev_init(i2c);
+ if (rc)
+ return rc;
+
+ dev_set_drvdata(dev, i2c);
+
+ return 0;
+}
+
+static const struct fsi_device_id fsi_i2c_ids[] = {
+ { FSI_ENGID_I2C, FSI_VERSION_ANY },
+ { 0 }
+};
+
+static struct fsi_driver fsi_i2c_driver = {
+ .id_table = fsi_i2c_ids,
+ .drv = {
+ .name = "i2cm-fsi",
+ .bus = &fsi_bus_type,
+ .probe = fsi_i2c_probe,
+ },
+};
+
+module_fsi_driver(fsi_i2c_driver);
+
+MODULE_AUTHOR("Eddie James <[email protected]>");
+MODULE_DESCRIPTION("FSI attached I2C master");
+MODULE_LICENSE("GPL");
--
1.8.3.1

2017-06-26 19:30:23

by Eddie James

[permalink] [raw]
Subject: [PATCH 3/6] drivers/i2c: Add transfer implementation for FSI algorithm

From: "Edward A. James" <[email protected]>

Execute I2C transfers from the FSI-attached I2C master. Use polling
instead of interrupts as we have no hardware IRQ over FSI.

Signed-off-by: Edward A. James <[email protected]>
---
drivers/i2c/busses/i2c-fsi.c | 195 ++++++++++++++++++++++++++++++++++++++++++-
1 file changed, 193 insertions(+), 2 deletions(-)

diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
index 95fe25a..007f104 100644
--- a/drivers/i2c/busses/i2c-fsi.c
+++ b/drivers/i2c/busses/i2c-fsi.c
@@ -143,6 +143,7 @@ struct fsi_i2c_port {
struct i2c_adapter adapter;
struct fsi_i2c_master *master;
u16 port;
+ u16 xfrd;
};

static DEFINE_IDA(fsi_i2c_ida);
@@ -229,17 +230,207 @@ static int fsi_i2c_set_port(struct fsi_i2c_port *port)
return rc;
}

+static int fsi_i2c_start(struct fsi_i2c_port *port, struct i2c_msg *msg,
+ bool stop)
+{
+ int rc;
+ struct fsi_i2c_master *i2c = port->master;
+ u32 cmd = I2C_CMD_WITH_START | I2C_CMD_WITH_ADDR;
+
+ port->xfrd = 0;
+
+ if (msg->flags & I2C_M_RD)
+ cmd |= I2C_CMD_READ;
+
+ if (stop || msg->flags & I2C_M_STOP)
+ cmd |= I2C_CMD_WITH_STOP;
+
+ cmd = SETFIELD(I2C_CMD_ADDR, cmd, msg->addr >> 1);
+ cmd = SETFIELD(I2C_CMD_LEN, cmd, msg->len);
+
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_CMD, &cmd);
+
+ return rc;
+}
+
+static int fsi_i2c_write_fifo(struct fsi_i2c_port *port, struct i2c_msg *msg,
+ u8 fifo_count)
+{
+ int write;
+ int rc = 0;
+ struct fsi_i2c_master *i2c = port->master;
+ int bytes_to_write = i2c->fifo_size - fifo_count;
+ int bytes_remaining = msg->len - port->xfrd;
+
+ if (bytes_to_write > bytes_remaining)
+ bytes_to_write = bytes_remaining;
+
+ while (bytes_to_write > 0) {
+ write = bytes_to_write;
+ /* fsi limited to max 4 byte aligned ops */
+ if (bytes_to_write > 4)
+ write = 4;
+ else if (write == 3)
+ write = 2;
+
+ rc = fsi_device_write(i2c->fsi, I2C_FSI_FIFO,
+ &msg->buf[port->xfrd], write);
+ if (rc)
+ return rc;
+
+ port->xfrd += write;
+ bytes_to_write -= write;
+ }
+
+ return rc;
+}
+
+static int fsi_i2c_read_fifo(struct fsi_i2c_port *port, struct i2c_msg *msg,
+ u8 fifo_count)
+{
+ int read;
+ int rc = 0;
+ struct fsi_i2c_master *i2c = port->master;
+ int xfr_remaining = msg->len - port->xfrd;
+ u32 dummy;
+
+ while (fifo_count) {
+ read = fifo_count;
+ /* fsi limited to max 4 byte aligned ops */
+ if (fifo_count > 4)
+ read = 4;
+ else if (read == 3)
+ read = 2;
+
+ if (xfr_remaining) {
+ if (xfr_remaining < read)
+ read = xfr_remaining;
+
+ rc = fsi_device_read(i2c->fsi, I2C_FSI_FIFO,
+ &msg->buf[port->xfrd], read);
+ if (rc)
+ return rc;
+
+ port->xfrd += read;
+ xfr_remaining -= read;
+ } else {
+ /* no more buffer but data in fifo, need to clear it */
+ rc = fsi_device_read(i2c->fsi, I2C_FSI_FIFO, &dummy,
+ read);
+ if (rc)
+ return rc;
+ }
+
+ fifo_count -= read;
+ }
+
+ return rc;
+}
+
+static int fsi_i2c_handle_status(struct fsi_i2c_port *port,
+ struct i2c_msg *msg, u32 status)
+{
+ int rc;
+ u8 fifo_count;
+ struct fsi_i2c_master *i2c = port->master;
+ u32 dummy = 0;
+
+ if (status & I2C_STAT_ERR) {
+ rc = fsi_i2c_write_reg(i2c->fsi, I2C_FSI_RESET_ERR, &dummy);
+ if (rc)
+ return rc;
+
+ if (status & I2C_STAT_NACK)
+ return -EFAULT;
+
+ return -EIO;
+ }
+
+ if (status & I2C_STAT_DAT_REQ) {
+ fifo_count = GETFIELD(I2C_STAT_FIFO_COUNT, status);
+
+ if (msg->flags & I2C_M_RD)
+ rc = fsi_i2c_read_fifo(port, msg, fifo_count);
+ else
+ rc = fsi_i2c_write_fifo(port, msg, fifo_count);
+
+ return rc;
+ }
+
+ if (status & I2C_STAT_CMD_COMP) {
+ if (port->xfrd < msg->len)
+ rc = -ENODATA;
+ else
+ rc = msg->len;
+
+ return rc;
+ }
+
+ return 0;
+}
+
+static int fsi_i2c_wait(struct fsi_i2c_port *port, struct i2c_msg *msg,
+ unsigned long timeout)
+{
+ const unsigned long local_timeout = 2; /* jiffies */
+ u32 status = 0;
+ int rc;
+
+ do {
+ rc = fsi_i2c_read_reg(port->master->fsi, I2C_FSI_STAT,
+ &status);
+ if (rc)
+ return rc;
+
+ if (status & I2C_STAT_ANY_RESP) {
+ rc = fsi_i2c_handle_status(port, msg, status);
+ if (rc < 0)
+ return rc;
+
+ /* cmd complete and all data xfrd */
+ if (rc == msg->len)
+ return 0;
+
+ /* need to xfr more data, but maybe don't need wait */
+ continue;
+ }
+
+ set_current_state(TASK_UNINTERRUPTIBLE);
+ schedule_timeout(local_timeout);
+ timeout = (timeout < local_timeout) ? 0 :
+ timeout - local_timeout;
+ } while (timeout);
+
+ return -ETIME;
+}
+
static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
int num)
{
- int rc;
+ int i, rc;
+ unsigned long start_time;
struct fsi_i2c_port *port = adap->algo_data;
+ struct i2c_msg *msg;

rc = fsi_i2c_set_port(port);
if (rc)
return rc;

- return -EOPNOTSUPP;
+ for (i = 0; i < num; ++i) {
+ msg = msgs + i;
+ start_time = jiffies;
+
+ rc = fsi_i2c_start(port, msg, i == num - 1);
+ if (rc)
+ return rc;
+
+ rc = fsi_i2c_wait(port, msg,
+ adap->timeout - (jiffies - start_time));
+ if (rc)
+ return rc;
+ }
+
+ return 0;
}

static u32 fsi_i2c_functionality(struct i2c_adapter *adap)
--
1.8.3.1

2017-06-26 19:30:22

by Eddie James

[permalink] [raw]
Subject: [PATCH 4/6] drivers/i2c: Add I2C master locking to FSI algorithm

From: "Edward A. James" <[email protected]>

Since there are many ports per master, each with it's own adapter and
chardev, we need some locking to prevent transfers from changing the\
master state while other transfers are in progress.

Signed-off-by: Edward A. James <[email protected]>
---
drivers/i2c/busses/i2c-fsi.c | 41 +++++++++++++++++++++++++++++++++++++----
1 file changed, 37 insertions(+), 4 deletions(-)

diff --git a/drivers/i2c/busses/i2c-fsi.c b/drivers/i2c/busses/i2c-fsi.c
index 007f104..41193be 100644
--- a/drivers/i2c/busses/i2c-fsi.c
+++ b/drivers/i2c/busses/i2c-fsi.c
@@ -136,6 +136,8 @@ struct fsi_i2c_master {
u8 fifo_size;
struct list_head ports;
struct ida ida;
+ wait_queue_head_t wait;
+ struct semaphore lock;
};

struct fsi_i2c_port {
@@ -171,6 +173,29 @@ static int fsi_i2c_write_reg(struct fsi_device *fsi, unsigned int reg,
return fsi_device_write(fsi, reg, &data_be, sizeof(data_be));
}

+static int fsi_i2c_lock_master(struct fsi_i2c_master *i2c, int timeout)
+{
+ int rc;
+
+ rc = down_trylock(&i2c->lock);
+ if (!rc)
+ return 0;
+
+ rc = wait_event_interruptible_timeout(i2c->wait,
+ !down_trylock(&i2c->lock),
+ timeout);
+ if (rc > 0)
+ return 0;
+
+ return -EBUSY;
+}
+
+static void fsi_i2c_unlock_master(struct fsi_i2c_master *i2c)
+{
+ up(&i2c->lock);
+ wake_up(&i2c->wait);
+}
+
static int fsi_i2c_dev_init(struct fsi_i2c_master *i2c)
{
int rc;
@@ -412,25 +437,31 @@ static int fsi_i2c_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,
struct fsi_i2c_port *port = adap->algo_data;
struct i2c_msg *msg;

- rc = fsi_i2c_set_port(port);
+ rc = fsi_i2c_lock_master(port->master, adap->timeout);
if (rc)
return rc;

+ rc = fsi_i2c_set_port(port);
+ if (rc)
+ goto unlock;
+
for (i = 0; i < num; ++i) {
msg = msgs + i;
start_time = jiffies;

rc = fsi_i2c_start(port, msg, i == num - 1);
if (rc)
- return rc;
+ goto unlock;

rc = fsi_i2c_wait(port, msg,
adap->timeout - (jiffies - start_time));
if (rc)
- return rc;
+ goto unlock;
}

- return 0;
+unlock:
+ fsi_i2c_unlock_master(port->master);
+ return rc;
}

static u32 fsi_i2c_functionality(struct i2c_adapter *adap)
@@ -455,6 +486,8 @@ static int fsi_i2c_probe(struct device *dev)
if (!i2c)
return -ENOMEM;

+ init_waitqueue_head(&i2c->wait);
+ sema_init(&i2c->lock, 1);
i2c->fsi = to_fsi_dev(dev);
i2c->idx = ida_simple_get(&fsi_i2c_ida, 1, INT_MAX, GFP_KERNEL);
ida_init(&i2c->ida);
--
1.8.3.1

2017-06-26 19:30:16

by Eddie James

[permalink] [raw]
Subject: [PATCH 6/6] Documentation/devicetree: Add FSI-attached I2C master dt bindings

From: "Edward A. James" <[email protected]>

Document the bindings.

Signed-off-by: Edward A. James <[email protected]>
---
Documentation/devicetree/bindings/i2c/i2c-fsi.txt | 31 +++++++++++++++++++++++
1 file changed, 31 insertions(+)
create mode 100644 Documentation/devicetree/bindings/i2c/i2c-fsi.txt

diff --git a/Documentation/devicetree/bindings/i2c/i2c-fsi.txt b/Documentation/devicetree/bindings/i2c/i2c-fsi.txt
new file mode 100644
index 0000000..fffc067
--- /dev/null
+++ b/Documentation/devicetree/bindings/i2c/i2c-fsi.txt
@@ -0,0 +1,31 @@
+Device-tree bindings for FSI-attached I2C master driver
+-------------------------------------------------------
+
+Required properties:
+ - compatible = "ibm,i2cm-fsi";
+ - reg = < address size >; : The FSI CFAM address and address space
+ size.
+ - #address-cells = <1>; : Number of address cells in child nodes
+ - #size-cells = <0>; : Number of size cells in child nodes.
+ - child nodes : Nodes to describe ports off the I2C
+ master.
+
+Child node required properties:
+ - reg = < port number > : The port number on the I2C master.
+
+Examples:
+
+ i2cm@1800 {
+ compatible = "ibm,i2cm-fsi";
+ reg = < 0x1800 0x400 >;
+ #address-cells = <1>;
+ #size-cells = <0>;
+
+ port@0 {
+ reg = <0>;
+ };
+
+ port@1 {
+ reg = <1>;
+ };
+ };
--
1.8.3.1

2017-06-27 01:06:14

by Jeremy Kerr

[permalink] [raw]
Subject: Re: [PATCH 6/6] Documentation/devicetree: Add FSI-attached I2C master dt bindings

Hi Eddie,

> +Required properties:
> + - compatible = "ibm,i2cm-fsi";
> + - reg = < address size >; : The FSI CFAM address and address space
> + size.
> + - #address-cells = <1>; : Number of address cells in child nodes
> + - #size-cells = <0>; : Number of size cells in child nodes.
> + - child nodes : Nodes to describe ports off the I2C
> + master.
> +
> +Child node required properties:
> + - reg = < port number > : The port number on the I2C master.
> +
> +Examples:
> +
> + i2cm@1800 {
> + compatible = "ibm,i2cm-fsi";
> + reg = < 0x1800 0x400 >;
> + #address-cells = <1>;
> + #size-cells = <0>;
> +
> + port@0 {
> + reg = <0>;
> + };
> +
> + port@1 {
> + reg = <1>;
> + };
> + };

Those child nodes represent the downstream i2c buses, and so also
contain the i2c slave devices, right? If so, you may want to document
that, and/or add a simple device to that example (say, an EEPROM).

Cheers,


Jeremy

2017-06-27 17:28:30

by Eddie James

[permalink] [raw]
Subject: Re: [PATCH 6/6] Documentation/devicetree: Add FSI-attached I2C master dt bindings



On 06/26/2017 08:06 PM, Jeremy Kerr wrote:
> Hi Eddie,
>
>> +Required properties:
>> + - compatible = "ibm,i2cm-fsi";
>> + - reg = < address size >; : The FSI CFAM address and address space
>> + size.
>> + - #address-cells = <1>; : Number of address cells in child nodes
>> + - #size-cells = <0>; : Number of size cells in child nodes.
>> + - child nodes : Nodes to describe ports off the I2C
>> + master.
>> +
>> +Child node required properties:
>> + - reg = < port number > : The port number on the I2C master.
>> +
>> +Examples:
>> +
>> + i2cm@1800 {
>> + compatible = "ibm,i2cm-fsi";
>> + reg = < 0x1800 0x400 >;
>> + #address-cells = <1>;
>> + #size-cells = <0>;
>> +
>> + port@0 {
>> + reg = <0>;
>> + };
>> +
>> + port@1 {
>> + reg = <1>;
>> + };
>> + };
> Those child nodes represent the downstream i2c buses, and so also
> contain the i2c slave devices, right? If so, you may want to document
> that, and/or add a simple device to that example (say, an EEPROM).

Yes, good point, but the driver currently wouldn't do anything with that
device information. It doesn't keep a list of populated devices on the
bus or anything. Still worth adding them to the device tree?

Thanks,
Eddie

>
> Cheers,
>
>
> Jeremy
>

2017-06-28 03:08:28

by Jeremy Kerr

[permalink] [raw]
Subject: Re: [PATCH 6/6] Documentation/devicetree: Add FSI-attached I2C master dt bindings

Hi Eddie,

>> Those child nodes represent the downstream i2c buses, and so also
>> contain the i2c slave devices, right? If so, you may want to document
>> that, and/or add a simple device to that example (say, an EEPROM).
>
> Yes, good point, but the driver currently wouldn't do anything with that
> device information. It doesn't keep a list of populated devices on the
> bus or anything. Still worth adding them to the device tree?

Surely the i2c core needs this to be able to find i2c slave devices on
the bus though?

[You'll need to set i2c_adapter->dev.of_node for this to work though,
which I don't think you are with the current patch set]

Cheers,


Jeremy

2017-06-28 23:21:37

by Rob Herring (Arm)

[permalink] [raw]
Subject: Re: [PATCH 6/6] Documentation/devicetree: Add FSI-attached I2C master dt bindings

On Mon, Jun 26, 2017 at 02:29:35PM -0500, Eddie James wrote:
> From: "Edward A. James" <[email protected]>

"dt-bindings: i2c: ..." for the subject.

>
> Document the bindings.
>
> Signed-off-by: Edward A. James <[email protected]>
> ---
> Documentation/devicetree/bindings/i2c/i2c-fsi.txt | 31 +++++++++++++++++++++++
> 1 file changed, 31 insertions(+)
> create mode 100644 Documentation/devicetree/bindings/i2c/i2c-fsi.txt
>
> diff --git a/Documentation/devicetree/bindings/i2c/i2c-fsi.txt b/Documentation/devicetree/bindings/i2c/i2c-fsi.txt
> new file mode 100644
> index 0000000..fffc067
> --- /dev/null
> +++ b/Documentation/devicetree/bindings/i2c/i2c-fsi.txt
> @@ -0,0 +1,31 @@
> +Device-tree bindings for FSI-attached I2C master driver
> +-------------------------------------------------------
> +
> +Required properties:
> + - compatible = "ibm,i2cm-fsi";

Only one version?

> + - reg = < address size >; : The FSI CFAM address and address space
> + size.
> + - #address-cells = <1>; : Number of address cells in child nodes
> + - #size-cells = <0>; : Number of size cells in child nodes.
> + - child nodes : Nodes to describe ports off the I2C
> + master.
> +
> +Child node required properties:
> + - reg = < port number > : The port number on the I2C master.
> +
> +Examples:
> +
> + i2cm@1800 {

i2c@1800

> + compatible = "ibm,i2cm-fsi";
> + reg = < 0x1800 0x400 >;
> + #address-cells = <1>;
> + #size-cells = <0>;
> +
> + port@0 {

The ports are separate i2c buses? Use i2c-bus@... instead. "ports" is
already used by OF graph binding.

> + reg = <0>;
> + };
> +
> + port@1 {
> + reg = <1>;
> + };
> + };
> --
> 1.8.3.1
>