i2c: at91: slave mode support
v4 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
doneAfter 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 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 | 380 +++++++++++++++
.../busses/{i2c-at91.c => i2c-at91-master.c} | 453 +-----------------
drivers/i2c/busses/i2c-at91-slave.c | 147 ++++++
drivers/i2c/busses/i2c-at91.h | 179 +++++++
7 files changed, 732 insertions(+), 447 deletions(-)
create mode 100644 drivers/i2c/busses/i2c-at91-core.c
rename drivers/i2c/busses/{i2c-at91.c => i2c-at91-master.c} (67%)
create mode 100644 drivers/i2c/busses/i2c-at91-slave.c
create mode 100644 drivers/i2c/busses/i2c-at91.hSignature · 1 line
2.20.1
PATCH v4 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
@@ -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
PATCH v4 2/3 i2c: at91: split driver into core and master file
In reply to Ludovic DesrochesFrom: 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.
---
MAINTAINERS | 3 +-
drivers/i2c/busses/Makefile | 1 +
drivers/i2c/busses/i2c-at91-core.c | 373 ++++++++++++++
.../busses/{i2c-at91.c => i2c-at91-master.c} | 467 +-----------------
drivers/i2c/busses/i2c-at91.h | 151 ++++++
5 files changed, 531 insertions(+), 464 deletions(-)
create mode 100644 drivers/i2c/busses/i2c-at91-core.c
rename drivers/i2c/busses/{i2c-at91.c => i2c-at91-master.c} (67%)
create mode 100644 drivers/i2c/busses/i2c-at91.hMAINTAINERS +2 -1
@@ -9873,7 +9873,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
@@ -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 +373 -0
373 lines not on this page · raw message
drivers/i2c/busses/i2c-at91-master.c renamed from drivers/i2c/busses/i2c-at91.c +4 -463
501 lines not on this page · raw message
drivers/i2c/busses/i2c-at91.h new file +151 -0
151 lines not on this page · raw message
Signature · 1 line
2.20.1
PATCH v4 3/3 i2c: at91: added slave mode support
In reply to Ludovic DesrochesFrom: Juergen Fitschen <[email protected]>
Slave mode driver is based on the concept of i2c-designware driver.
--- 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 | 147 ++++++++++++++++++++++++++++ drivers/i2c/busses/i2c-at91.h | 30 +++++- 5 files changed, 202 insertions(+), 4 deletions(-) create mode 100644 drivers/i2c/busses/i2c-at91-slave.c
drivers/i2c/busses/Kconfig +13 -0
@@ -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
@@ -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
@@ -60,8 +60,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 = {
@@ -243,7 +245,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 +147 -0
147 lines not on this page · raw message
drivers/i2c/busses/i2c-at91.h +29 -1
@@ -53,6 +53,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 */
@@ -63,13 +67,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 */
@@ -137,6 +145,11 @@ struct at91_twi_dev {bool recv_len_abort;u32 fifo_size;struct at91_twi_dma dma; +bool slave_detected; +#if IS_ENABLED (CONFIG_I2C_AT91_SLAVE_EXPERIMENTAL ) +unsigned smr; +struct i2c_client *slave; +#endif };unsigned at91_twi_read (struct at91_twi_dev *dev,unsigned reg);
@@ -149,3 +162,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); + +#if IS_ENABLED (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
Re: [PATCH v4 3/3] i2c: at91: added slave mode support
Quoted 7 lines
Slave mode driver is based on the concept of i2c-designware driver.
+/*
+ * i2c slave support for Atmel's AT91 Two-Wire Interface (TWI)
+ *
+ * Copyright (C) 2017 Juergen Fitschen <[email protected]>
+ *
Quoted 5 lines
+ * 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.+ */
Perhaps, you can use SPDX instead of boiler plate license text in the files?
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v4 3/3] i2c: at91: added slave mode support
Quoted 2 lines
Perhaps, you can use SPDX instead of boiler plate license text in the files?
I think we can do this incrementally.
kbuild test robot
build botRe: [PATCH v4 3/3] i2c: at91: added slave mode support
In reply to Ludovic DesrochesThank you for the patch! Yet something to improve:
Hi Juergen,
Thank you for the patch! Yet something to improve:
[auto build test ERROR on wsa/i2c/for-next]
[also build test ERROR on v4.20 next-20190103]
[if your patch is applied to the wrong git tree, please drop us a note to help improve the system]
url: https://github.com/0day-ci/linux/commits/Ludovic-Desroches/i2c-at91-slave-mode-support/20181227-231603 base: https://git.kernel.org/pub/scm/linux/kernel/git/wsa/linux.git i2c/for-next config: arm-allmodconfig (attached as .config) compiler: arm-linux-gnueabi-gcc (Debian 7.2.0-11) 7.2.0
reproduce:
wget https://raw.githubusercontent.com/intel/lkp-tests/master/sbin/make.cross -O ~/bin/make.cross chmod +x ~/bin/make.cross
# save the attached .config to linux build tree
GCC_VERSION=7.2.0 make.cross ARCH=arm
All errors (new ones prefixed by >>):
>> ERROR: "at91_init_twi_bus_slave" [drivers/i2c/busses/i2c-at91.ko] undefined! >> ERROR: "at91_twi_probe_slave" [drivers/i2c/busses/i2c-at91.ko] undefined!
---
0-DAY kernel test infrastructure Open Source Technology Center
https://lists.01.org/pipermail/kbuild-all Intel Corporation
Re: [PATCH v4 3/3] i2c: at91: added slave mode support
Quoted 5 lines
All errors (new ones prefixed by >>):
Quoted 2 lines
Quoted 2 lines
ERROR: "
at91_init_twi_bus_slave" [drivers/i2c/busses/i2c-at91.ko] undefined!
ERROR: "at91_twi_probe_slave" [drivers/i2c/busses/i2c-at91.ko] undefined!
That needs to be fixed. Rest looks good to me!
Re: [PATCH v4 3/3] i2c: at91: added slave mode support
Quoted 7 lines · Wolfram Sang
Quoted 5 lines
All errors (new ones prefixed by >>):
Quoted 2 lines
Quoted 2 lines
ERROR: "
at91_init_twi_bus_slave" [drivers/i2c/busses/i2c-at91.ko] undefined!
ERROR: "at91_twi_probe_slave" [drivers/i2c/busses/i2c-at91.ko] undefined!That needs to be fixed. Rest looks good to me!
Ludovic, do you have time to fix it? I would really like to apply this series for the next merge window.
Re: [PATCH v4 3/3] i2c: at91: added slave mode support
Hi,
Quoted 4 lines · Wolfram Sang
Quoted 3 lines · Wolfram Sang
Quoted 3 lines
All errors (new ones prefixed by >>):
Quoted 8 lines
Quoted 4 lines
Quoted 2 lines
Quoted 2 lines
Quoted 2 lines
ERROR: "
at91_init_twi_bus_slave" [drivers/i2c/busses/i2c-at91.ko] undefined!
ERROR: "at91_twi_probe_slave" [drivers/i2c/busses/i2c-at91.ko] undefined!That needs to be fixed. Rest looks good to me!
Ludovic, do you have time to fix it? I would really like to apply this series for the next merge window.
Sorry it's in my todo list but I didn't have time to handle it. I'll try to fix it beginning of next week.
Regards
Ludovic
Quoted 4 lines
_______________________________________________
linux-arm-kernel mailing list
[email protected]
http://lists.infradead.org/mailman/listinfo/linux-arm-kernel