Skip to contentSkip to conversation

i2c: at91: slave mode support

PATCH Applied Ludovic Desroches 3 participants 7 messages Feb 22, 2019 – Mar 24, 2019

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

v5 cover i2c: at91: slave mode support

[Ludovic Desroches: see Changes section]

Based on the discussion we had on the i2c-linux list [1], I wrote a patch for AT91 hardware and tried to fulfill the Linux I2C slave interface description
[2] as good as possible. This enables aforementioned hardware to act as an I2C slave that can be accessed by a remote I2C master.

I have tested this patchset successfully on an ATSAMA5D27.

                                 ^  3.3V   ^  3.3V
    +-----------------------+    |         |         +-----------------------+
    | Slave: ATSAMA5D27     |   +-+       +-+        | Master: ATSAMA5D35    |
    | with i2c-slave-eeprom |   | | 100k  | | 100k   | with i2cset           |
    +-------------------+-+-+   +-+       +-+        +-+-+-------------------+
                        | |      |         |           | |
                        | +------+---------|---(SDA)---+ |
                        +------------------+---(SCL)-----+

    Schematic: Connection of slave and master with 100kOhm pullup resistors.

On the master the following BASH script has been used to stress the slave.

        root@emblinux:~# cat ./stress.sh
        #!/bin/bash
        I=0
        while true
        do
                if i2cset -y -r 1 0x64 0 $I w | grep mismatch
                then
                        echo "$(date): Error in transmission ${I}"
                fi
                ((I++))
                if [ $I -eq 65536 ]
                then
                        I=0
                        echo "$(date): Sent 65536 transmissions"
                fi
        done

After running the script for some time I had the following output. To me this looks promising :)

        root@emblinux:~# ./stress.sh
        Thu Nov  9 13:58:45 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 14:35:20 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 15:12:11 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 15:49:04 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 16:26:00 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 17:03:07 UTC 2017: Sent 65536 transmissions
        Thu Nov  9 17:40:15 UTC 2017: Sent 65536 transmissions

If you have some hardware with an at91-i2c interface included at hand, I really would appreciate if you can run the test script on your hardware and test this driver.
Thu Nov 9 17:40:15 UTC 2017: Sent 65536 transmissions

If you have some hardware with an at91-i2c interface included at hand, I really would appreciate if you can run the test script on your hardware and test this driver.

Best regards
Juergen

[Ludovic Desroches]
Changes in v5:
- use SPDX for licence
- replace IF_ENABLED par defined to solve compilation issue when selected as module.
Changes in v4:
- rebased on next-20181224
- update Kconfig part to state that this feature is experimental.
- tested quickly on a single board, SAMA5D2 Xplained, i2c-gpio as master, OK.
Changes in v3:
- rebase (cherry-pick was enough)
- fix checkpatch errors
- tests:
- hangs with a SAMA5D4 (master and slave on different busses) after about 100 transfers. It's the first time I do this test.
- some mismatches with a SAMA5D4 as slave and a SAMA5D2 as master I don't know if it's a regression. I don't remember having seen this behavior previously.
I think it's worth taking those patches even if there are some possible bugs. It'll allow to get more people using it and so to consolidate the slave mode support.

Changes in v2:
- Implemented all suggestions made by Ludovic. (Thank you!)
- Reworked the IRQ handler completely. Have a look in patch 3 fort further details.

[1] https://marc.info/?t=150824004800001&r=1&w=1
[2] https://www.kernel.org/doc/Documentation/i2c/slave-interface

Juergen Fitschen (3):
i2c: at91: segregate master mode specific code from probe and init
func
i2c: at91: split driver into core and master file
i2c: at91: added slave mode support

 MAINTAINERS                                   |   3 +-
 drivers/i2c/busses/Kconfig                    |  13 +
 drivers/i2c/busses/Makefile                   |   4 +
 drivers/i2c/busses/i2c-at91-core.c            | 376 ++++++++++++++
 .../busses/{i2c-at91.c => i2c-at91-master.c}  | 459 +-----------------
 drivers/i2c/busses/i2c-at91-slave.c           | 143 ++++++
 drivers/i2c/busses/i2c-at91.h                 | 175 +++++++
 7 files changed, 721 insertions(+), 452 deletions(-)
 create mode 100644 drivers/i2c/busses/i2c-at91-core.c
 rename drivers/i2c/busses/{i2c-at91.c => i2c-at91-master.c} (66%)
 create mode 100644 drivers/i2c/busses/i2c-at91-slave.c
 create mode 100644 drivers/i2c/busses/i2c-at91.h
Signature · 1 line
2.20.1
#1 lore raw

PATCH v5 1/3 i2c: at91: segregate master mode specific code from probe and init func

From: Juergen Fitschen <[email protected]>

In order to implement slave mode support for the at91 hardware we have to segregate all master mode specific function parts from the general parts. The upcoming slave mode patch will call its sepcific probe resp. init function instead of the master mode functions after the shared general code has been executed.

This concept has been influenced by the i2c-designware driver.

---
 drivers/i2c/busses/i2c-at91.c | 90 +++++++++++++++++++++--------------
 1 file changed, 55 insertions(+), 35 deletions(-)
drivers/i2c/busses/i2c-at91.c +55 -35
index 3f3e8b3bf5ff..bc74184462c6 100644
@@ -174,10 +174,8 @@ static void at91_twi_irq_restore(struct at91_twi_dev *dev)
 	at91_twi_write(dev, AT91_TWI_IER, dev->imr);
 }
 
-static void at91_init_twi_bus(struct at91_twi_dev *dev)
+static void at91_init_twi_bus_master(struct at91_twi_dev *dev)
 {
-	at91_disable_twi_interrupts(dev);
-	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
 	/* FIFO should be enabled immediately after the software reset */
 	if (dev->fifo_size)
 		at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_FIFOEN);
@@ -186,6 +184,14 @@ static void at91_init_twi_bus(struct at91_twi_dev *dev)
 	at91_twi_write(dev, AT91_TWI_CWGR, dev->twi_cwgr_reg);
 }
 
+static void at91_init_twi_bus(struct at91_twi_dev *dev)
+{
+	at91_disable_twi_interrupts(dev);
+	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
+
+	at91_init_twi_bus_master(dev);
+}
+
 /*
  * Calculate symmetric clock as stated in datasheet:
  * twi_clk = F_MAIN / (2 * (cdiv * (1 << ckdiv) + offset))
@@ -1046,18 +1052,56 @@ static struct at91_twi_pdata *at91_twi_get_driver_data(
 	return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;
 }
 
+static int at91_twi_probe_master(struct platform_device *pdev,
+				 u32 phy_addr, struct at91_twi_dev *dev)
+{
+	int rc;
+	u32 bus_clk_rate;
+
+	init_completion(&dev->cmd_complete);
+
+	rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
+			      dev_name(dev->dev), dev);
+	if (rc) {
+		dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
+		return rc;
+	}
+
+	if (dev->dev->of_node) {
+		rc = at91_twi_configure_dma(dev, phy_addr);
+		if (rc == -EPROBE_DEFER)
+			return rc;
+	}
+
+	if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
+				  &dev->fifo_size)) {
+		dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
+	}
+
+	rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
+				  &bus_clk_rate);
+	if (rc)
+		bus_clk_rate = DEFAULT_TWI_CLK_HZ;
+
+	at91_calc_twi_clock(dev, bus_clk_rate);
+
+	dev->adapter.algo = &at91_twi_algorithm;
+	dev->adapter.quirks = &at91_twi_quirks;
+
+	return 0;
+}
+
 static int at91_twi_probe(struct platform_device *pdev)
 {
 	struct at91_twi_dev *dev;
 	struct resource *mem;
 	int rc;
 	u32 phy_addr;
-	u32 bus_clk_rate;
 
 	dev = devm_kzalloc(&pdev->dev, sizeof(*dev), GFP_KERNEL);
 	if (!dev)
 		return -ENOMEM;
-	init_completion(&dev->cmd_complete);
+
 	dev->dev = &pdev->dev;
 
 	mem = platform_get_resource(pdev, IORESOURCE_MEM, 0);
@@ -1077,13 +1121,6 @@ static int at91_twi_probe(struct platform_device *pdev)
 	if (dev->irq < 0)
 		return dev->irq;
 
-	rc = devm_request_irq(&pdev->dev, dev->irq, atmel_twi_interrupt, 0,
-			 dev_name(dev->dev), dev);
-	if (rc) {
-		dev_err(dev->dev, "Cannot get irq %d: %d\n", dev->irq, rc);
-		return rc;
-	}
-
 	platform_set_drvdata(pdev, dev);
 
 	dev->clk = devm_clk_get(dev->dev, NULL);
@@ -1095,38 +1132,21 @@ static int at91_twi_probe(struct platform_device *pdev)
 	if (rc)
 		return rc;
 
-	if (dev->dev->of_node) {
-		rc = at91_twi_configure_dma(dev, phy_addr);
-		if (rc == -EPROBE_DEFER) {
-			clk_disable_unprepare(dev->clk);
-			return rc;
-		}
-	}
-
-	if (!of_property_read_u32(pdev->dev.of_node, "atmel,fifo-size",
-				  &dev->fifo_size)) {
-		dev_info(dev->dev, "Using FIFO (%u data)\n", dev->fifo_size);
-	}
-
-	rc = of_property_read_u32(dev->dev->of_node, "clock-frequency",
-			&bus_clk_rate);
-	if (rc)
-		bus_clk_rate = DEFAULT_TWI_CLK_HZ;
-
-	at91_calc_twi_clock(dev, bus_clk_rate);
-	at91_init_twi_bus(dev);
-
 	snprintf(dev->adapter.name, sizeof(dev->adapter.name), "AT91");
 	i2c_set_adapdata(&dev->adapter, dev);
 	dev->adapter.owner = THIS_MODULE;
 	dev->adapter.class = I2C_CLASS_DEPRECATED;
-	dev->adapter.algo = &at91_twi_algorithm;
-	dev->adapter.quirks = &at91_twi_quirks;
 	dev->adapter.dev.parent = dev->dev;
 	dev->adapter.nr = pdev->id;
 	dev->adapter.timeout = AT91_I2C_TIMEOUT;
 	dev->adapter.dev.of_node = pdev->dev.of_node;
 
+	rc = at91_twi_probe_master(pdev, phy_addr, dev);
+	if (rc)
+		return rc;
+
+	at91_init_twi_bus(dev);
+
 	pm_runtime_set_autosuspend_delay(dev->dev, AUTOSUSPEND_TIMEOUT);
 	pm_runtime_use_autosuspend(dev->dev);
 	pm_runtime_set_active(dev->dev);
Signature · 1 line
2.20.1
#2 lore raw

PATCH v5 2/3 i2c: at91: split driver into core and master file

In reply to Ludovic Desroches

From: Juergen Fitschen <[email protected]>

The single file i2c-at91.c has been split into core code (i2c-at91-core.c) and master mode specific code (i2c-at91-master.c). This should enhance maintainability and reduce ifdeffery for slave mode related code.

The code itself hasn't been touched. Shared functions only had to be made non-static. Furthermore, includes have been cleaned up.

[[email protected]: fix checkpatch errors and use SPDX]
---
 MAINTAINERS                                   |   3 +-
 drivers/i2c/busses/Makefile                   |   1 +
 drivers/i2c/busses/i2c-at91-core.c            | 369 ++++++++++++++
 .../busses/{i2c-at91.c => i2c-at91-master.c}  | 473 +-----------------
 drivers/i2c/busses/i2c-at91.h                 | 147 ++++++
 5 files changed, 524 insertions(+), 469 deletions(-)
 create mode 100644 drivers/i2c/busses/i2c-at91-core.c
 rename drivers/i2c/busses/{i2c-at91.c => i2c-at91-master.c} (66%)
 create mode 100644 drivers/i2c/busses/i2c-at91.h
MAINTAINERS +2 -1
index 32d444476a90..1dafe6ef4f6d 100644
@@ -9890,7 +9890,8 @@ MICROCHIP I2C DRIVER
 M:	Ludovic Desroches <[email protected]>
 L:	[email protected]
 S:	Supported
-F:	drivers/i2c/busses/i2c-at91.c
+F:	drivers/i2c/busses/i2c-at91.h
+F:	drivers/i2c/busses/i2c-at91-*.c
 
 MICROCHIP ISC DRIVER
 M:	Eugen Hristev <[email protected]>
drivers/i2c/busses/Makefile +1 -0
index 5f0cb6915969..ea75a777940e 100644
@@ -35,6 +35,7 @@ obj-$(CONFIG_I2C_POWERMAC)	+= i2c-powermac.o
 obj-$(CONFIG_I2C_ALTERA)	+= i2c-altera.o
 obj-$(CONFIG_I2C_ASPEED)	+= i2c-aspeed.o
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
+i2c-at91-objs			:= i2c-at91-core.o i2c-at91-master.o
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
 obj-$(CONFIG_I2C_AXXIA)		+= i2c-axxia.o
 obj-$(CONFIG_I2C_BCM2835)	+= i2c-bcm2835.o
drivers/i2c/busses/i2c-at91-core.c new file +369 -0

369 lines not on this page · raw message

drivers/i2c/busses/i2c-at91-master.c renamed from drivers/i2c/busses/i2c-at91.c +5 -468

516 lines not on this page · raw message

drivers/i2c/busses/i2c-at91.h new file +147 -0

147 lines not on this page · raw message

Signature · 1 line
2.20.1
#3 lore raw

Re: [PATCH v5 2/3] i2c: at91: split driver into core and master file

Quoted 8 lines · Ludovic Desroches

From: Juergen Fitschen <[email protected]>

The single file i2c-at91.c has been split into core code (i2c-at91-core.c) and master mode specific code (i2c-at91-master.c). This should enhance maintainability and reduce ifdeffery for slave mode related code.

The code itself hasn't been touched. Shared functions only had to be made non-static. Furthermore, includes have been cleaned up.

Since it's a split of existing code, consider my comments as a way to improve it afterwards.

drivers/i2c/busses/i2c-at91-core.c line 191

Quoted 11 lines
drivers/i2c/busses/i2c-at91-core.c new file +11 -0
+static struct at91_twi_pdata *at91_twi_get_driver_data(
+					struct platform_device *pdev)
+{
+	if (pdev->dev.of_node) {
+		const struct of_device_id *match;
+		match = of_match_node(atmel_twi_dt_ids, pdev->dev.of_node);
+		if (!match)
+			return NULL;
+		return (struct at91_twi_pdata *)match->data;
+	}
+	return (struct at91_twi_pdata *) platform_get_device_id(pdev)->driver_data;

One may do the following instead:

	struct at91_twi_pdata *data;

	data = device_get_match_data(&pdev->dev);
	if (data)
		return data;
	return ...

And if you ever will switch old platform to somelike unified interface for device properties, I would recommend to use device_property_* instead of of_property_* and so on.

drivers/i2c/busses/i2c-at91.h line 22

Quoted 9 lines
drivers/i2c/busses/i2c-at91.h new file +8 -0
+}
 
+#include <linux/clk.h>
+#include <linux/completion.h>
+#include <linux/dma-mapping.h>
+#include <linux/dmaengine.h>
+#include <linux/i2c.h>
+#include <linux/platform_data/dma-atmel.h>
+#include <linux/platform_device.h>

Are all those anyhow in use in this header file?

Quoted 1 line

+#define AT91_I2C_TIMEOUT msecs_to_jiffies(100) /* transfer timeout */

Wouldn't be better to use _MS here and call actual conversion whenever it's needed?

drivers/i2c/busses/i2c-at91.h line 27

Quoted 3 lines
drivers/i2c/busses/i2c-at91.h new file +2 -0
+#define AT91_I2C_DMA_THRESHOLD	8			/* enable DMA if transfer size is bigger than this threshold */
 
+#define AUTOSUSPEND_TIMEOUT		2000

_MS ?

Quoted 16 lines
drivers/i2c/busses/i2c-at91.h new file +16 -0
+#define	AT91_TWI_SR		0x0020	/* Status Register */
+#define	AT91_TWI_TXCOMP		BIT(0)	/* Transmission Complete */
+#define	AT91_TWI_RXRDY		BIT(1)	/* Receive Holding Register Ready */
+#define	AT91_TWI_TXRDY		BIT(2)	/* Transmit Holding Register Ready */
+#define	AT91_TWI_OVRE		BIT(6)	/* Overrun Error */
+#define	AT91_TWI_UNRE		BIT(7)	/* Underrun Error */
+#define	AT91_TWI_NACK		BIT(8)	/* Not Acknowledged */
+#define	AT91_TWI_LOCK		BIT(23) /* TWI Lock due to Frame Errors */
+
+#define	AT91_TWI_INT_MASK \
+	(AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY | AT91_TWI_NACK)
+
+#define	AT91_TWI_IER		0x0024	/* Interrupt Enable Register */
+#define	AT91_TWI_IDR		0x0028	/* Interrupt Disable Register */
+#define	AT91_TWI_IMR		0x002c	/* Interrupt Mask Register */
+#define	AT91_TWI_RHR		0x0030	/* Receive Holding Register */

drivers/i2c/busses/i2c-at91.h line 82

Quoted 10 lines
drivers/i2c/busses/i2c-at91.h new file +9 -0
+#define	AT91_TWI_THR		0x0034	/* Transmit Holding Register */
+
+#define	AT91_TWI_ACR		0x0040	/* Alternative Command Register */
+#define	AT91_TWI_ACR_DATAL(len)	((len) & 0xff)
+#define	AT91_TWI_ACR_DIR	BIT(8)
+
+#define	AT91_TWI_FMR		0x0050	/* FIFO Mode Register */
+#define	AT91_TWI_FMR_TXRDYM(mode)	(((mode) & 0x3) << 0)
 
+#define	AT91_TWI_FMR_TXRDYM_MASK	(0x3 << 0)

GENMASK() ?

drivers/i2c/busses/i2c-at91.h line 84

Quoted 3 lines
drivers/i2c/busses/i2c-at91.h new file +2 -0
+#define	AT91_TWI_FMR_RXRDYM(mode)	(((mode) & 0x3) << 4)
 
+#define	AT91_TWI_FMR_RXRDYM_MASK	(0x3 << 4)

Ditto.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
#4 lore raw

PATCH v5 3/3 i2c: at91: added slave mode support

In reply to Ludovic Desroches

From: Juergen Fitschen <[email protected]>

Slave mode driver is based on the concept of i2c-designware driver.

[[email protected]: rework Kconfig and replace IS_ENABLED by defined]

---
 drivers/i2c/busses/Kconfig          |  13 +++
 drivers/i2c/busses/Makefile         |   3 +
 drivers/i2c/busses/i2c-at91-core.c  |  13 ++-
 drivers/i2c/busses/i2c-at91-slave.c | 143 ++++++++++++++++++++++++++++
 drivers/i2c/busses/i2c-at91.h       |  30 +++++-
 5 files changed, 198 insertions(+), 4 deletions(-)
 create mode 100644 drivers/i2c/busses/i2c-at91-slave.c
drivers/i2c/busses/Kconfig +13 -0
index f2c681971201..6b1f6dcdf533 100644
@@ -387,6 +387,19 @@ config I2C_AT91
 	  the latency to fill the transmission register is too long. If you
 	  are facing this situation, use the i2c-gpio driver.
 
+config I2C_AT91_SLAVE_EXPERIMENTAL
+	tristate "Microchip AT91 I2C experimental slave mode"
+	depends on I2C_AT91
+	select I2C_SLAVE
+	help
+	  If you say yes to this option, support for the slave mode will be
+	  added. Caution: do not use it for production. This feature has not
+	  been tested in a heavy way, help wanted.
+	  There are known bugs:
+	    - It can hang, on a SAMA5D4, after several transfers.
+	    - There are some mismtaches with a SAMA5D4 as slave and a SAMA5D2 as
+	    master.
+
 config I2C_AU1550
 	tristate "Au1550/Au1200/Au1300 SMBus interface"
 	depends on MIPS_ALCHEMY
drivers/i2c/busses/Makefile +3 -0
index ea75a777940e..59b22fbef90a 100644
@@ -36,6 +36,9 @@ obj-$(CONFIG_I2C_ALTERA)	+= i2c-altera.o
 obj-$(CONFIG_I2C_ASPEED)	+= i2c-aspeed.o
 obj-$(CONFIG_I2C_AT91)		+= i2c-at91.o
 i2c-at91-objs			:= i2c-at91-core.o i2c-at91-master.o
+ifeq ($(CONFIG_I2C_AT91_SLAVE_EXPERIMENTAL),y)
+	i2c-at91-objs		+= i2c-at91-slave.o
+endif
 obj-$(CONFIG_I2C_AU1550)	+= i2c-au1550.o
 obj-$(CONFIG_I2C_AXXIA)		+= i2c-axxia.o
 obj-$(CONFIG_I2C_BCM2835)	+= i2c-bcm2835.o
drivers/i2c/busses/i2c-at91-core.c +10 -3
index 6cb22341fa81..8d55cdd69ff4 100644
@@ -56,8 +56,10 @@ void at91_init_twi_bus(struct at91_twi_dev *dev)
 {
 	at91_disable_twi_interrupts(dev);
 	at91_twi_write(dev, AT91_TWI_CR, AT91_TWI_SWRST);
-
-	at91_init_twi_bus_master(dev);
+	if (dev->slave_detected)
+		at91_init_twi_bus_slave(dev);
+	else
+		at91_init_twi_bus_master(dev);
 }
 
 static struct at91_twi_pdata at91rm9200_config = {
@@ -239,7 +241,12 @@ static int at91_twi_probe(struct platform_device *pdev)
 	dev->adapter.timeout = AT91_I2C_TIMEOUT;
 	dev->adapter.dev.of_node = pdev->dev.of_node;
 
-	rc = at91_twi_probe_master(pdev, phy_addr, dev);
+	dev->slave_detected = i2c_detect_slave_mode(&pdev->dev);
+
+	if (dev->slave_detected)
+		rc = at91_twi_probe_slave(pdev, phy_addr, dev);
+	else
+		rc = at91_twi_probe_master(pdev, phy_addr, dev);
 	if (rc)
 		return rc;
 
drivers/i2c/busses/i2c-at91-slave.c new file +143 -0

143 lines not on this page · raw message

drivers/i2c/busses/i2c-at91.h +29 -1
index 636447498b41..be0e7afda529 100644
@@ -49,6 +49,10 @@
 #define	AT91_TWI_IADRSZ_1	0x0100	/* Internal Device Address Size */
 #define	AT91_TWI_MREAD		BIT(12)	/* Master Read Direction */
 
+#define	AT91_TWI_SMR		0x0008	/* Slave Mode Register */
+#define	AT91_TWI_SMR_SADR_MAX	0x007f
+#define	AT91_TWI_SMR_SADR(x)	(((x) & AT91_TWI_SMR_SADR_MAX) << 16)
+
 #define	AT91_TWI_IADR		0x000c	/* Internal Address Register */
 
 #define	AT91_TWI_CWGR		0x0010	/* Clock Waveform Generator Reg */
@@ -59,13 +63,17 @@
 #define	AT91_TWI_TXCOMP		BIT(0)	/* Transmission Complete */
 #define	AT91_TWI_RXRDY		BIT(1)	/* Receive Holding Register Ready */
 #define	AT91_TWI_TXRDY		BIT(2)	/* Transmit Holding Register Ready */
+#define	AT91_TWI_SVREAD		BIT(3)	/* Slave Read */
+#define	AT91_TWI_SVACC		BIT(4)	/* Slave Access */
 #define	AT91_TWI_OVRE		BIT(6)	/* Overrun Error */
 #define	AT91_TWI_UNRE		BIT(7)	/* Underrun Error */
 #define	AT91_TWI_NACK		BIT(8)	/* Not Acknowledged */
+#define	AT91_TWI_EOSACC		BIT(11)	/* End Of Slave Access */
 #define	AT91_TWI_LOCK		BIT(23) /* TWI Lock due to Frame Errors */
 
 #define	AT91_TWI_INT_MASK \
-	(AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY | AT91_TWI_NACK)
+	(AT91_TWI_TXCOMP | AT91_TWI_RXRDY | AT91_TWI_TXRDY | AT91_TWI_NACK \
+	| AT91_TWI_SVACC | AT91_TWI_EOSACC)
 
 #define	AT91_TWI_IER		0x0024	/* Interrupt Enable Register */
 #define	AT91_TWI_IDR		0x0028	/* Interrupt Disable Register */
@@ -133,6 +141,11 @@ struct at91_twi_dev {
 	bool recv_len_abort;
 	u32 fifo_size;
 	struct at91_twi_dma dma;
+	bool slave_detected;
+#ifdef CONFIG_I2C_AT91_SLAVE_EXPERIMENTAL
+	unsigned smr;
+	struct i2c_client *slave;
+#endif
 };
 
 unsigned at91_twi_read(struct at91_twi_dev *dev, unsigned reg);
@@ -145,3 +158,18 @@ void at91_init_twi_bus(struct at91_twi_dev *dev);
 void at91_init_twi_bus_master(struct at91_twi_dev *dev);
 int at91_twi_probe_master(struct platform_device *pdev, u32 phy_addr,
 			  struct at91_twi_dev *dev);
+
+#ifdef CONFIG_I2C_AT91_SLAVE_EXPERIMENTAL
+void at91_init_twi_bus_slave(struct at91_twi_dev *dev);
+int at91_twi_probe_slave(struct platform_device *pdev, u32 phy_addr,
+			 struct at91_twi_dev *dev);
+
+#else
+static inline void at91_init_twi_bus_slave(struct at91_twi_dev *dev) {}
+static inline int at91_twi_probe_slave(struct platform_device *pdev,
+				       u32 phy_addr, struct at91_twi_dev *dev)
+{
+	return -EINVAL;
+}
+
+#endif
Signature · 1 line
2.20.1
#5 lore raw
In reply to Ludovic Desroches
Quoted 20 lines · Ludovic Desroches

[Ludovic Desroches: see Changes section]

Based on the discussion we had on the i2c-linux list [1], I wrote a patch for AT91 hardware and tried to fulfill the Linux I2C slave interface description
[2] as good as possible. This enables aforementioned hardware to act as an I2C slave that can be accessed by a remote I2C master.

I have tested this patchset successfully on an ATSAMA5D27.

                                 ^  3.3V   ^  3.3V
    +-----------------------+    |         |         +-----------------------+
    | Slave: ATSAMA5D27     |   +-+       +-+        | Master: ATSAMA5D35    |
    | with i2c-slave-eeprom |   | | 100k  | | 100k   | with i2cset           |
    +-------------------+-+-+   +-+       +-+        +-+-+-------------------+
                        | |      |         |           | |
                        | +------+---------|---(SDA)---+ |
                        +------------------+---(SCL)-----+

    Schematic: Connection of slave and master with 100kOhm pullup resistors.
Quoted 5 lines

On the master the following BASH script has been used to stress the slave.

        root@emblinux:~# cat ./stress.sh
        #!/bin/bash

Not everybody has bash in their environments. See below, how easily to switch to regular shell.

#!/bin/sh

Quoted 9 lines
        I=0
        while true
        do
                if i2cset -y -r 1 0x64 0 $I w | grep mismatch
                then
                        echo "$(date): Error in transmission ${I}"
                fi

                ((I++))

I=$(($I+1))

Quoted 5 lines
                if [ $I -eq 65536 ]
                then
                        I=0
                        echo "$(date): Sent 65536 transmissions"
                fi

This one can be optimized to

for I in $(seq 65536); do
...
done

Quoted 22 lines

done

After running the script for some time I had the following output. To me this looks promising :)

        root@emblinux:~# ./stress.sh
        Thu Nov  9 13:58:45 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 14:35:20 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 15:12:11 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 15:49:04 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 16:26:00 CTE 2017: Sent 65536 transmissions
        Thu Nov  9 17:03:07 UTC 2017: Sent 65536 transmissions
        Thu Nov  9 17:40:15 UTC 2017: Sent 65536 transmissions

If you have some hardware with an at91-i2c interface included at hand, I really would appreciate if you can run the test script on your hardware and test this driver.
Thu Nov 9 17:40:15 UTC 2017: Sent 65536 transmissions

If you have some hardware with an at91-i2c interface included at hand, I really would appreciate if you can run the test script on your hardware and test this driver.

Signature · 2 lines
With Best Regards,
Andy Shevchenko
Applied
#6 lore raw
In reply to Ludovic Desroches
Quoted 6 lines · Ludovic Desroches

[Ludovic Desroches: see Changes section]

Based on the discussion we had on the i2c-linux list [1], I wrote a patch for AT91 hardware and tried to fulfill the Linux I2C slave interface description
[2] as good as possible. This enables aforementioned hardware to act as an I2C slave that can be accessed by a remote I2C master.

After this was successfully checked by buildbot in a seperate branch:

Applied to for-next, thanks!