i2c: at91: slave mode support
v3 cover i2c: at91: slave mode support
[Ludovic Desroches: see 'Changes in v3' 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 at 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 at 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.
Best regards
Juergen
[Ludovic Desroches]
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 firs 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/Makefile | 4 +
drivers/i2c/busses/i2c-at91-core.c | 380 +++++++++++++++++
.../i2c/busses/{i2c-at91.c => i2c-at91-master.c} | 453 +--------------------
drivers/i2c/busses/i2c-at91-slave.c | 147 +++++++
drivers/i2c/busses/i2c-at91.h | 179 ++++++++
6 files changed, 719 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.12.2
PATCH v3 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.12.2
PATCH v3 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 ++++++++++++++++
.../i2c/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
@@ -2426,7 +2426,8 @@ ATMEL I2C DRIVER M: Ludovic Desroches <[email protected]> L: linux-i2c at vger.kernel.org S: Supported -F: drivers/i2c/busses/i2c-at91.c +F: drivers/i2c/busses/i2c-at91.h +F: drivers/i2c/busses/i2c-at91-*.c ATMEL ISI DRIVER M: Ludovic Desroches <[email protected]>
drivers/i2c/busses/Makefile +1 -0
@@ -34,6 +34,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.12.2
PATCH v3 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/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 +++++++- 4 files changed, 189 insertions(+), 4 deletions(-) create mode 100644 drivers/i2c/busses/i2c-at91-slave.c
drivers/i2c/busses/Makefile +3 -0
@@ -35,6 +35,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_SLAVE),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_SLAVE ) +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_SLAVE ) +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.12.2
Quoted 3 lines
[Ludovic Desroches]
Changes in v3:
- rebase (cherry-pick was enough)
Thanks for the rebase. I wonder, though, I recall to had more complicated issues. However...
Quoted 10 lines
- fix checkpatch errors
- tests:
- hangs with a SAMA5D4 (master and slave on different busses) after about 100 transfers. It's the firs 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.
I really want to see those patches go upstream, too. But I am also not a big fan of delivering the user something with known issues. Especially not when they affect the main feature to be added. My rationale here is that someone who is able to fix the issues remaining will also be able to pick up and apply patches.
Maybe, maybe if it was to be enabled by a special I2C_AT91_SLAVE_EXPERIMANTEL symbol with lots of explanations. I need to think twice about that, though.
Speaking of Kconfig, I think this series needs to place a
select I2C_SLAVE
somewhere.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 833 bytes
Desc: not available
URL: <http://lists.infradead.org/pipermail/linux-arm-kernel/attachments/20180721/c34789bd/attachment.sig>
Quoted 24 lines · Wolfram Sang
Quoted 3 lines
[Ludovic Desroches]
Changes in v3:
- rebase (cherry-pick was enough)Thanks for the rebase. I wonder, though, I recall to had more complicated issues. However...
Quoted 10 lines
- fix checkpatch errors
- tests:
- hangs with a SAMA5D4 (master and slave on different busses) after about 100 transfers. It's the firs 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.I really want to see those patches go upstream, too. But I am also not a big fan of delivering the user something with known issues. Especially not when they affect the main feature to be added. My rationale here is that someone who is able to fix the issues remaining will also be able to pick up and apply patches.
Quoted 4 lines
Maybe, maybe if it was to be enabled by a special
I2C_AT91_SLAVE_EXPERIMANTELsymbol with lots of explanations. I need to think twice about that, though.
I understand your point. The experimental mentionning could be a good trade-off. Let me know once you make up your mind.
Quoted 6 lines
Speaking of
Kconfig, I think this series needs to place aselect
I2C_SLAVEsomewhere.
Ok I'll update it if we go further with this set of patches.
Regards
Ludovic
Hi Ludovic,
Quoted 25 lines
Quoted 17 lines
Quoted 10 lines
- fix checkpatch errors
- tests:
- hangs with a SAMA5D4 (master and slave on different busses) after about 100 transfers. It's the firs 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.I really want to see those patches go upstream, too. But I am also not a big fan of delivering the user something with known issues. Especially not when they affect the main feature to be added. My rationale here is that someone who is able to fix the issues remaining will also be able to pick up and apply patches.
Quoted 4 lines
Maybe, maybe if it was to be enabled by a special
I2C_AT91_SLAVE_EXPERIMANTELsymbol with lots of explanations. I need to think twice about that, though.I understand your point. The experimental mentionning could be a good trade-off. Let me know once you make up your mind.
Quoted 8 lines
Quoted 6 lines
Speaking of
Kconfig, I think this series needs to place aselect
I2C_SLAVEsomewhere.
Ok I'll update it if we go further with this set of patches.
Ok, I give in. If you:
* add 'select I2C_SLAVE'
* make slave support selectable by I2C_AT91_SLAVE_STAGING or _EXPERIMENTAL or something alike (default n)
* and add to the help text of that symbol the above known issues and 'not for production' and 'help wanted' and where to get more info and all that
then I'll apply this series soonish. Promised!
Thanks,
Wolfram
Hi Wolfram,
Quoted 38 lines · Wolfram Sang
Hi Ludovic,
Quoted 25 lines
Quoted 17 lines
Quoted 10 lines
- fix checkpatch errors
- tests:
- hangs with a SAMA5D4 (master and slave on different busses) after about 100 transfers. It's the firs 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.I really want to see those patches go upstream, too. But I am also not a big fan of delivering the user something with known issues. Especially not when they affect the main feature to be added. My rationale here is that someone who is able to fix the issues remaining will also be able to pick up and apply patches.
Quoted 4 lines
Maybe, maybe if it was to be enabled by a special
I2C_AT91_SLAVE_EXPERIMANTELsymbol with lots of explanations. I need to think twice about that, though.I understand your point. The experimental mentionning could be a good trade-off. Let me know once you make up your mind.
Quoted 8 lines
Quoted 6 lines
Speaking of
Kconfig, I think this series needs to place aselect
I2C_SLAVEsomewhere.
Ok I'll update it if we go further with this set of patches.
Ok, I give in. If you:
Quoted 8 lines
* add 'select
I2C_SLAVE'
* make slave support selectable byI2C_AT91_SLAVE_STAGINGor_EXPERIMENTALor something alike (default n)
* and add to the help text of that symbol the above known issues and 'not for production' and 'help wanted' and where to get more info and all thatthen I'll apply this series soonish. Promised!
Ok I will handle these requests and resend it.
Thanks
Ludovic