i2c: npcm7xx: add NPCM i2c controller driver
Tali Perry
maintainerv12 cover i2c: npcm7xx: add NPCM i2c controller driver
This patch set adds i2c controller support
for the Nuvoton NPCM Baseboard Management Controller (BMC).
NPCM7xx includes 16 I2C controllers. This driver operates the controller. This module also includes a slave mode.
---
v12 -> v11:
- Fix according to maintainer comments.
- debugfs simplified (usedebugfs_create_u64).
- slave read fifo split from master read fifo.
v11 -> v10:
- Fix according to maintainer comments.
- Init clk simplified.
- Comments in c99
- Split master irq function.
- debugfs not mandatory.
- yaml file fix.
v10 -> v9:
- Fix according to maintainer comments.
- binding file changed to yaml format.
- Shorten recovery flow.
- Add support for health monitoring counters.
v9 -> v8:
- Fix according to maintainer comments.
- Split lines of iowrite..(ioread..) to separate lines.
- Use readx_poll_timeout_atomic
- resolve various style issues.
v8 -> v7:
- Split to two commits, one for master, one for slave.
- Rename smb to i2c.
- Remove global vars.
v7 -> v6:
- Rebased on Linux 5.4-rc8 (was Linux 5.4-rc7).
- Fix issue found by kbuild test robot (redundant include).
- Note: left a warning related to fall through. This fall through is intentional.
v6 -> v5:
- Update documentation
v5 -> v4:
- support recovery
- master-slave switch support needed for IPMB
v4 -> v3:
- typo on cover letter.
v3 -> v2:
- fix dt binding: compatible name: omit "bus"
v2 -> v1:
- run check patch in strict mode.
- use linux crc.
- define regs in constant offset without base.
- remove debug prints.
- no declarations for local functions.
v1: initial version
---
Tali Perry (3):
dt-bindings: i2c: npcm7xx: add NPCM I2C controller documentation
i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
i2c: npcm7xx: Add support for slave mode for Nuvoton NPCM BMC I2C
controller driver.
.../bindings/i2c/nuvoton,npcm7xx-i2c.yaml | 62 + drivers/i2c/busses/Kconfig | 9 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-npcm7xx.c | 2426 +++++++++++++++++ 4 files changed, 2498 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/nuvoton,npcm7xx-i2c.yaml create mode 100644 drivers/i2c/busses/i2c-npcm7xx.c
base-commit: b9bbe6ed63b2b9f2c9ee5cbd0f2c946a2723f4ce
Signature · 1 line
2.22.0
Tali Perry
maintainerPATCH v12 1/3 dt-bindings: i2c: npcm7xx: add NPCM I2C controller
Added device tree binding documentation for Nuvoton BMC NPCM I2C controller.
--- .../bindings/i2c/nuvoton,npcm7xx-i2c.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/nuvoton,npcm7xx-i2c.yaml
Documentation/devicetree/bindings/i2c/nuvoton,npcm7xx-i2c.yaml new file +62 -0
@@ -0,0 +1,62 @@ +# SPDX-License-Identifier: GPL-2.0-only OR BSD-2-Clause +%YAML 1.2 +--- +$id: http://devicetree.org/schemas/i2c/nuvoton,npcm7xx-i2c.yaml# +$schema: http://devicetree.org/meta-schemas/core.yaml# + +title: nuvoton NPCM7XX I2C Controller Device Tree Bindings + +description: | + The NPCM750x includes sixteen I2C bus controllers. All Controllers support + both master and slave mode. Each controller can switch between master and slave + at run time (i.e. IPMB mode). Each controller has two 16 byte HW FIFO for TX and + RX. + +maintainers: + - Tali Perry <[email protected]> + +properties: + compatible: + enum: + - nuvoton,npcm7xx-i2c + + reg: + maxItems: 1 + + interrupts: + maxItems: 1 + + clocks: + maxItems: 1 + description: Reference clock for the I2C bus + + bus-frequency: + description: Desired I2C bus clock frequency in Hz. If not specified, + the default 100 kHz frequency will be used. + possible values are 100000, 400000 and 1000000. + default: 100000 + enum: [100000, 400000, 1000000] + +required: + - compatible + - reg + - interrupts + +allOf: + - $ref: /schemas/i2c/i2c-controller.yaml# + +unevaluatedProperties: false + +examples: + - | + i2c0: i2c@80000 { + compatible = "nuvoton,npcm750-i2c"; + reg = <0x80000 0x1000>; + clocks = <&clk NPCM7XX_CLK_APB2>; + bus-frequency = <100000>; + interrupts = <GIC_SPI 64 IRQ_TYPE_LEVEL_HIGH>; + pinctrl-names = "default"; + pinctrl-0 = <&smb0_pins>; + }; + +...
Signature · 7 lines
2.22.0 _______________________________________________ linux-arm-kernel mailing list [email protected] http://lists.infradead.org/mailman/listinfo/linux-arm-kernel
[email protected]
maintainerRe: [PATCH v12 1/3] dt-bindings: i2c: npcm7xx: add NPCM I2C controller
Quoted 9 lines · Tali Perry
Added device tree binding documentation for Nuvoton BMC NPCM I2C controller.
Signed-off-by Tali Perry <[email protected]>--- .../bindings/i2c/nuvoton,npcm7xx-i2c.yaml | 62 +++++++++++++++++++ 1 file changed, 62 insertions(+) create mode 100644 Documentation/devicetree/bindings/i2c/nuvoton,npcm7xx-i2c.yaml
My bot found errors running 'make dt_binding_check' on your patch:
Error: Documentation/devicetree/bindings/i2c/nuvoton,npcm7xx-i2c.example.dts:22.28-29 syntax error
FATAL ERROR: Unable to parse input treescripts/Makefile.lib:312: recipe for target 'Documentation/devicetree/bindings/i2c/nuvoton,npcm7xx-i2c.example.dt.yaml' failed make[1]: *** [Documentation/devicetree/bindings/i2c/nuvoton,npcm7xx-i2c.example.dt.yaml] Error 1
make[1]: *** Waiting for unfinished jobs....
Makefile:1300: recipe for target 'dt_binding_check' failed
make: *** [dt_binding_check] Error 2
See https://patchwork.ozlabs.org/patch/1295115
If you already ran 'make dt_binding_check' and didn't see the above error(s), then make sure dt-schema is up to date:
pip3 install git+https://github.com/devicetree-org/dt-schema.git@master --upgrade
Please check and re-submit.
Tali Perry
maintainerPATCH v12 2/3 i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
In reply to Tali PerryAdd Nuvoton NPCM BMC I2C controller driver.
--- drivers/i2c/busses/Kconfig | 9 + drivers/i2c/busses/Makefile | 1 + drivers/i2c/busses/i2c-npcm7xx.c | 1818 ++++++++++++++++++++++++++++++ 3 files changed, 1828 insertions(+) create mode 100644 drivers/i2c/busses/i2c-npcm7xx.c
drivers/i2c/busses/Kconfig +9 -0
@@ -791,6 +791,15 @@ config I2C_NOMADIK I2C interface from ST-Ericsson's Nomadik and Ux500 architectures, as well as the STA2X11 PCIe I/O HUB. +config I2C_NPCM7XX + tristate "Nuvoton I2C Controller" + depends on ARCH_NPCM7XX || COMPILE_TEST + help + If you say yes to this option, support will be included for the + Nuvoton I2C controller, which is available on the NPCM7xx BMC + controller. + Driver can also support slave mode (select I2C_SLAVE). + config I2C_OCORES tristate "OpenCores I2C Controller" help
drivers/i2c/busses/Makefile +1 -0
@@ -81,6 +81,7 @@ obj-$(CONFIG_I2C_MT7621) += i2c-mt7621.o obj-$(CONFIG_I2C_MV64XXX) += i2c-mv64xxx.o obj-$(CONFIG_I2C_MXS) += i2c-mxs.o obj-$(CONFIG_I2C_NOMADIK) += i2c-nomadik.o +obj-$(CONFIG_I2C_NPCM7XX) += i2c-npcm7xx.o obj-$(CONFIG_I2C_OCORES) += i2c-ocores.o obj-$(CONFIG_I2C_OMAP) += i2c-omap.o obj-$(CONFIG_I2C_OWL) += i2c-owl.o
drivers/i2c/busses/i2c-npcm7xx.c new file +1818 -0
1,818 lines not on this page · raw message
Signature · 1 line
2.22.0
Re: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
Quoted 1 line · Tali Perry
Add Nuvoton NPCM BMC I2C controller driver.
Thanks. My comments below.
After addressing them, FWIW,
...
drivers/i2c/busses/i2c-npcm7xx.c line 1305
Quoted 1 line
drivers/i2c/busses/i2c-npcm7xx.c new file +1
-0+/* Frequency larger than 1 MHZ is not supported */
1 MHZ -> 1MHz
...
Quoted 1 line
+#ifdef
CONFIG_DEBUG_FS
Again, why is this here?
Have you checked debugfs.h for !CONFIG_DEBUG_FS case?
Quoted 17 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +17
-0+/* i2c debugfs directory: used to keep health monitor of i2c devices */ +static struct dentry *npcm_i2c_debugfs_dir; + +static void i2c_init_debugfs (struct platform_device *pdev,struct npcm_i2c *bus) +{ +struct dentry *d; + +if (!npcm_i2c_debugfs_dir) +return ; + + d =debugfs_create_dir (dev_name (&pdev->dev), npcm_i2c_debugfs_dir); +if (IS_ERR_OR_NULL (d)) +return ; + +debugfs_create_u64 ("ber_cnt" ,0444 , d, &bus->ber_cnt); +debugfs_create_u64 ("nack_cnt" ,0444 , d, &bus->nack_cnt); +debugfs_create_u64 ("rec_succ_cnt" ,0444 , d, &bus->rec_succ_cnt);
drivers/i2c/busses/i2c-npcm7xx.c line 1675
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +9
-0+debugfs_create_u64 ("rec_fail_cnt" ,0444 , d, &bus->rec_fail_cnt); +debugfs_create_u64 ("timeout_cnt" ,0444 , d, &bus->timeout_cnt); + + bus->debugfs = d; +} +#else +static void i2c_init_debugfs (struct platform_device *pdev,struct npcm_i2c *bus) +{ +}
This is completely redundant.
Quoted 1 line
+#endif
...
Quoted 1 line
+#ifdef
CONFIG_DEBUG_FS
Ditto.
Quoted 20 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +20
-0+static int __init npcm_i2c_init (void ) +{ +struct dentry *dir; + + dir =debugfs_create_dir ("i2c" ,NULL ); +if (IS_ERR_OR_NULL (dir)) +return 0 ; + + npcm_i2c_debugfs_dir = dir; +return 0 ; +} + +static void __exit npcm_i2c_exit(void) +{ +debugfs_remove_recursive (npcm_i2c_debugfs_dir); +} + +module_init (npcm_i2c_init); +module_exit (npcm_i2c_exit); +#endif
...
Quoted 1 line
+MODULE_VERSION("0.1.3");
Module version is defined by kernel commit hash. But it's up to you and subsystem maintainer to decide.
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
Hi Tali, Andy!
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Tali Perry
Add Nuvoton NPCM BMC I2C controller driver.
Thanks. My comments below.
After addressing them, FWIW,Reviewed-by Andy Shevchenko <[email protected]>
Thanks, Andy, for all the review!
From a glimpse, this looks good to go. I will have a close look later today.
Quoted 5 lines
Quoted 1 line
+#ifdef
CONFIG_DEBUG_FSAgain, why is this here?
Have you checked
debugfs.hfor !CONFIG_DEBUG_FS case?
I wondered also about DEBUG_FS entries. I can see their value when developing the driver. But since this is done now, do they really help a user to debug a difficult case? I am not sure, and then I wonder if we should have that code in upstream. I am open for discussion, though.
Quoted 4 lines
Quoted 1 line
+MODULE_VERSION("0.1.3");
Module version is defined by kernel commit hash. But it's up to you and subsystem maintainer to decide.
Please drop it. I also think commit id's (or even kernel versions) are a more precise description.
Regards,
Wolfram
Tali Perry
maintainerRe: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
Quoted 4 lines · Wolfram Sang
Hi Tali, Andy!
On Thu, May 21, 2020 at 05:23:40PM +0300, Andy Shevchenko wrote:
Quoted 9 lines
Quoted 6 lines · Tali Perry
Quoted 1 line
Add Nuvoton NPCM BMC I2C controller driver.
Thanks. My comments below.
After addressing them, FWIW,Reviewed-by Andy Shevchenko <[email protected]>Thanks, Andy, for all the review!
Highly appreciate your time and patience for a newbie :)
Quoted 8 lines
From a glimpse, this looks good to go. I will have a close look later today.
Quoted 5 lines
Quoted 1 line
+#ifdef
CONFIG_DEBUG_FSAgain, why is this here?
Have you checked
debugfs.hfor !CONFIG_DEBUG_FS case?
I compiled both options. I removed the ifdef in most places, except in the struct itself. Users that don't use the debugfs don't need this in the struct.
Quoted 5 lines
I wondered also about
DEBUG_FSentries. I can see their value when developing the driver. But since this is done now, do they really help a user to debug a difficult case? I am not sure, and then I wonder if we should have that code in upstream. I am open for discussion, though.
The user wanted to have health monitor implemented on top of the driver. The user has 16 channels connected the multiple devices. All are operated using various daemons in the system. Sometimes the slave devices are power down. Therefor the user wanted to track the health status of the devices.
Quoted 8 lines
Quoted 5 lines
Quoted 2 lines
+MODULE_VERSION("0.1.3");
Module version is defined by kernel commit hash. But it's up to you and subsystem maintainer to decide.
Please drop it. I also think commit id's (or even kernel versions) are a more precise description.
will remove.
Quoted 5 lines
Regards,
Wolfram
BR,
Tali
Re: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
Quoted 32 lines · Tali Perry
Quoted 10 lines · Wolfram Sang
Quoted 6 lines · Andy Shevchenko
Quoted 1 line · Tali Perry
Add Nuvoton NPCM BMC I2C controller driver.
Thanks. My comments below.
After addressing them, FWIW,Reviewed-by Andy Shevchenko <[email protected]>Thanks, Andy, for all the review!
Highly appreciate your time and patience for a newbie :)
Quoted 8 lines
From a glimpse, this looks good to go. I will have a close look later today.
Quoted 5 lines
Quoted 1 line
+#ifdef
CONFIG_DEBUG_FSAgain, why is this here?
Have you checked
debugfs.hfor !CONFIG_DEBUG_FS case?I compiled both options. I removed the ifdef in most places, except in the struct itself. Users that don't use the debugfs don't need this in the struct.
Quoted 5 lines
I wondered also about
DEBUG_FSentries. I can see their value when developing the driver. But since this is done now, do they really help a user to debug a difficult case? I am not sure, and then I wonder if we should have that code in upstream. I am open for discussion, though.
Quoted 4 lines
The user wanted to have health monitor implemented on top of the driver. The user has 16 channels connected the multiple devices. All are operated using various daemons in the system. Sometimes the slave devices are power down. Therefor the user wanted to track the health status of the devices.
Ah, then there are these options I have in mind (Wolfram, FYI as well!):
1) push with debugfs as a temporary solution and convert to devlink health protocol [1];
2) drop it and develop devlink_health solution;
3) push debugfs and wait if I²C will gain devlink health support
[1]: https://www.kernel.org/doc/html/latest/networking/devlink/devlink-health.html
Signature · 2 lines
With Best Regards, Andy Shevchenko
Re: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
Quoted 11 lines
Quoted 10 lines
Quoted 5 lines
I wondered also about
DEBUG_FSentries. I can see their value when developing the driver. But since this is done now, do they really help a user to debug a difficult case? I am not sure, and then I wonder if we should have that code in upstream. I am open for discussion, though.The user wanted to have health monitor implemented on top of the driver. The user has 16 channels connected the multiple devices. All are operated using various daemons in the system. Sometimes the slave devices are power down. Therefor the user wanted to track the health status of the devices.
Quoted 4 lines
Ah, then there are these options I have in mind (Wolfram, FYI as well!):
1) push with debugfs as a temporary solution and convert to devlink health protocol [1];
2) drop it and developdevlink_healthsolution;
3) push debugfs and wait if I²C will gain devlink health support
No need for 2). We can push it now and convert it later. That being said, I wonder if [1] is suitable for this driver? Things like NACKs and timeouts happen regularly on an I2C bus and are not a state of bad health.
Tali Perry
maintainerRe: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
Quoted 17 lines · Wolfram Sang
Quoted 12 lines
Quoted 11 lines
Quoted 6 lines
I wondered also about
DEBUG_FSentries. I can see their value when developing the driver. But since this is done now, do they really help a user to debug a difficult case? I am not sure, and then I wonder if we should have that code in upstream. I am open for discussion, though.The user wanted to have health monitor implemented on top of the driver. The user has 16 channels connected the multiple devices. All are operated using various daemons in the system. Sometimes the slave devices are power down. Therefor the user wanted to track the health status of the devices.
Quoted 4 lines
Ah, then there are these options I have in mind (Wolfram, FYI as well!):
1) push with debugfs as a temporary solution and convert to devlink health protocol [1];
2) drop it and developdevlink_healthsolution;
3) push debugfs and wait if I²C will gain devlink health support
Quoted 5 lines
No need for 2). We can push it now and convert it later. That being said, I wonder if [1] is suitable for this driver? Things like NACKs and timeouts happen regularly on an I2C bus and are not a state of bad health.
Agree, having a timeout every now and then is not an issue. The user is interested
in cases when the number of timeouts\BER\nack\recovery are high.
Re: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
In reply to Wolfram SangQuoted 3 lines
From a glimpse, this looks good to go. I will have a close look later today.
Phew, this driver is huge. I won't finish my review today, but I am working on it and am maybe 2/3 through.
Re: [PATCH v12 2/3] i2c: npcm7xx: Add Nuvoton NPCM I2C controller driver
In reply to Tali PerryQuoted 3 lines · Tali Perry
Add Nuvoton NPCM BMC I2C controller driver.
Signed-off-by Tali Perry <[email protected]>
This is a very complex driver, so I can really comment only about high level things. Thank you very much for keeping at it!
My code checkers say:
CHECKPATCH:
CHECK: usleep_range is preferred over udelay; see Documentation/timers/timers-howto.rst #1210: FILE: drivers/i2c/busses/i2c-npcm7xx.c:1161:
+ udelay(200);
(a few of them)
GCC:
CC drivers/i2c/busses/i2c-npcm7xx.odrivers/i2c/busses/i2c-npcm7xx.c: In function ‘npcm_i2c_reset’:drivers/i2c/busses/i2c-npcm7xx.c:521:5: warning: variable ‘i2cctl2’ set but not used [-Wunused-but-set-variable]
Quoted 20 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +20
-0+/* Status of one I2C module */ +struct npcm_i2c { +struct i2c_adapter adap; +struct device *dev; +unsigned char __iomem *reg; +spinlock_t lock;/* IRQ synchronization */ +struct completion cmd_complete; +int irq; +int cmd_err; +struct i2c_msg *msgs; +int msgs_num; +int num; +u32 apb_clk; +struct i2c_bus_recovery_info rinfo; +enum i2c_state state; +enum i2c_oper operation; +enum i2c_mode master_or_slave; +enum i2c_state_ind stop_ind; +u8 dest_addr; +u8 *rd_buf;
drivers/i2c/busses/i2c-npcm7xx.c line 267
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+u16 rd_size; +u16 rd_ind; +u8 *wr_buf; +u16 wr_size; +u16 wr_ind; +bool fifo_use; +u16 PEC_mask;/* PEC bit mask per slave address */ +bool PEC_use; +bool read_block_use; +u8 int_cnt;
What is this for? It is written to but never read.
drivers/i2c/busses/i2c-npcm7xx.c line 268
Quoted 1 line
drivers/i2c/busses/i2c-npcm7xx.c new file +1
-0+u32 clk_period_us;
Not used? Seems this struct could need some cleaning up.
Quoted 13 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +13
-0+unsigned long int_time_stamp; +unsigned long bus_freq;/* in kHz */ +u32 xmits; +#ifdef CONFIG_DEBUG_FS +struct dentry *debugfs;/* debugfs device directory */ +u64 ber_cnt; +u64 rec_succ_cnt; +u64 rec_fail_cnt; +u64 nack_cnt; +u64 timeout_cnt; +#endif +}; +
...
drivers/i2c/busses/i2c-npcm7xx.c line 338
Quoted 7 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +7
-0+static inline u16 npcm_i2c_get_index (struct npcm_i2c *bus) +{ +if (bus->operation ==I2C_READ_OPER ) +return bus->rd_ind; +if (bus->operation ==I2C_WRITE_OPER ) +return bus->wr_ind; +return 0 ;
I2C_NO_OPER?
...
drivers/i2c/busses/i2c-npcm7xx.c line 1199
Quoted 11 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +11
-0+/* recovery using bit banging functionality of the module */ +static int npcm_i2c_recovery_init (struct i2c_adapter *_adap) +{ + struct npcm_i2c *bus = container_of(_adap, struct npcm_i2c, adap); +struct i2c_bus_recovery_info *rinfo = &bus->rinfo; + + rinfo->recover_bus = npcm_i2c_recovery_tgclk; + rinfo->prepare_recovery =NULL ; + rinfo->unprepare_recovery =NULL ; + rinfo->set_scl =NULL ; + rinfo->set_sda =NULL ;
'bus' is kzalloced, so no need for these NULLs.
What I wonder more, though, is if you can't populate {set|get}_{scl|sda} and use the internal i2c_generic_scl_recovery()? Are there any issues with it?
drivers/i2c/busses/i2c-npcm7xx.c line 1201
Quoted 2 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +2
-0+ +dev_dbg (bus->dev,"init i2c recovery using TGCLK\n" );
There is no error path here, so I think this message is not useful. Means also this function could be 'void'.
drivers/i2c/busses/i2c-npcm7xx.c line 1204
Quoted 3 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +3
-0+ + rinfo->get_scl = npcm_i2c_get_SCL; + rinfo->get_sda = npcm_i2c_get_SDA;
Not needed when you have a custom function.
Quoted 6 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +6
-0+ + _adap->bus_recovery_info = rinfo; + +return 0 ; +} +
...
Quoted 15 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +15
-0+static int npcm_i2c_master_xfer (struct i2c_adapter *adap,struct i2c_msg *msgs, +int num) +{ + struct npcm_i2c *bus = container_of(adap, struct npcm_i2c, adap); +struct i2c_msg *msg0, *msg1; +unsigned long time_left, flags; +u16 nwrite, nread; +u8 *write_data, *read_data; +u8 slave_addr; +int timeout; +int ret =0 ; +bool read_block = false; +bool read_PEC = false; +u8 bus_busy; +unsigned long timeout_usec;
drivers/i2c/busses/i2c-npcm7xx.c line 1507
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+ +if (bus->state ==I2C_DISABLE ) { +dev_err (bus->dev,"I2C%d module is disabled" , bus->num); +return -EINVAL ; + } + +if (num >2 || num <1 ) { +dev_err (bus->dev,"I2C cmd not supported num of msgs=%d" , num); +return -EINVAL ; + }
Since you have an 'i2c_adapter_quirks' struct filled, the core will I2C check that for you.
drivers/i2c/busses/i2c-npcm7xx.c line 1514
Quoted 7 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +7
-0+ + msg0 = &msgs[0 ]; + slave_addr = msg0->addr; +if (msg0->flags &I2C_M_RD ) {/* read */ +if (num ==2 ) { +dev_err (bus->dev,"num=2 but 1st msg rd instead of wr" ); +return -EINVAL ;
Ditto.
Quoted 15 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +15
-0+ } + nwrite =0 ; + write_data =NULL ; + read_data = msg0->buf; +if (msg0->flags &I2C_M_RECV_LEN ) { + nread =1 ; + read_block = true; +if (msg0->flags &I2C_CLIENT_PEC ) + read_PEC = true; + }else { + nread = msg0->len; + } + }else {/* write */ + nwrite = msg0->len; + write_data = msg0->buf;
drivers/i2c/busses/i2c-npcm7xx.c line 1539
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+ nread =0 ; + read_data =NULL ; +if (num ==2 ) { + msg1 = &msgs[1 ]; + read_data = msg1->buf; +if (slave_addr != msg1->addr) { +dev_err (bus->dev, +"SA==%02x but msg1->addr==%02x\n" , + slave_addr, msg1->addr); +return -EINVAL ;
Ditto.
drivers/i2c/busses/i2c-npcm7xx.c line 1545
Quoted 6 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +6
-0+ } +if ((msg1->flags &I2C_M_RD ) ==0 ) { +dev_err (bus->dev, +"num = 2 but both msg are write.\n" ); +return -EINVAL ; + }
Ditto.
Quoted 9 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +9
-0+if (msg1->flags &I2C_M_RECV_LEN ) { + nread =1 ; + read_block = true; +if (msg1->flags &I2C_CLIENT_PEC ) + read_PEC = true; + }else { + nread = msg1->len; + read_block = false; + }
drivers/i2c/busses/i2c-npcm7xx.c line 1564
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+ } + } + +/* Adaptive TimeOut: astimated time in usec + 100% margin */ + timeout_usec = (2 *10000 / bus->bus_freq) * (2 + nread + nwrite); + timeout =max (msecs_to_jiffies (35 ),usecs_to_jiffies (timeout_usec)); +if (nwrite >=32 *1024 || nread >=32 *1024 ) { +dev_err (bus->dev,"i2c%d buffer too big\n" , bus->num); +return -EINVAL ; + }
Ditto.
Quoted 65 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +65
-0+ + time_left = jiffies +msecs_to_jiffies (DEFAULT_STALL_COUNT ) +1 ; +do { +/* +* we must clear slave address immediately when the bus is not +* busy, so we spinlock it, but we don't keep the lock for the +* entire while since it is too long. +*/ +spin_lock_irqsave (&bus->lock, flags); + bus_busy =ioread8 (bus->reg +NPCM_I2CCST ) &NPCM_I2CCST_BB ; +spin_unlock_irqrestore (&bus->lock, flags); + + }while (time_is_after_jiffies (time_left) && bus_busy); + +if (bus_busy) { +iowrite8 (NPCM_I2CCST_BB , bus->reg +NPCM_I2CCST ); +npcm_i2c_reset (bus); +i2c_recover_bus (adap); +return -EAGAIN ; + } + +npcm_i2c_init_params (bus); + bus->dest_addr = slave_addr; + bus->msgs = msgs; + bus->msgs_num = num; + bus->cmd_err =0 ; + bus->read_block_use = read_block; + +reinit_completion (&bus->cmd_complete); +if (!npcm_i2c_master_start_xmit (bus, slave_addr, nwrite, nread, + write_data, read_data, read_PEC, + read_block)) + ret = -EBUSY ; + +if (ret != -EBUSY ) { + time_left =wait_for_completion_timeout (&bus->cmd_complete, + timeout); + +if (time_left ==0 ) { +#ifdef CONFIG_DEBUG_FS +if (bus->timeout_cnt ==ULLONG_MAX ) { +dev_dbg (bus->dev, +"timeout_cnt reach max, reset to 0" ); + bus->timeout_cnt =0 ; + } + bus->timeout_cnt++; +#endif +if (bus->master_or_slave ==I2C_MASTER ) { +i2c_recover_bus (adap); + bus->cmd_err = -EIO ; + bus->state =I2C_IDLE ; + } + } + } + ret = bus->cmd_err; + +/* if there was BER, check if need to recover the bus: */ +if (bus->cmd_err == -EAGAIN ) + ret =i2c_recover_bus (adap); + +return bus->cmd_err; +} + +static u32 npcm_i2c_functionality (struct i2c_adapter *adap) +{
drivers/i2c/busses/i2c-npcm7xx.c line 1639
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+return I2C_FUNC_I2C | +I2C_FUNC_SMBUS_EMUL | +I2C_FUNC_SMBUS_BLOCK_DATA | +I2C_FUNC_SMBUS_PEC | +I2C_FUNC_SLAVE ; +} + +static const struct i2c_adapter_quirks npcm_i2c_quirks = { + .max_read_len =32768 , + .max_write_len =32768 ,
These limits are for simple reads/writes with num_msgs == 1. If you have limits also for num_msgs == 2, then you also need to fill 'max_comb_1st_msg_len' and 'max_comb_2nd_msg_len'. (Because for some HW these are different values then)
drivers/i2c/busses/i2c-npcm7xx.c line 1640
Quoted 1 line
drivers/i2c/busses/i2c-npcm7xx.c new file +1
-0+ .max_num_msgs = 2,
You can drop this because I2C_AQ_COMB_WRITE_THEN_READ implies it.
Quoted 45 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +45
-0+ .flags = I2C_AQ_COMB_WRITE_THEN_READ, +}; + +static const struct i2c_algorithm npcm_i2c_algo = { + .master_xfer = npcm_i2c_master_xfer, + .functionality = npcm_i2c_functionality, +}; + +#ifdef CONFIG_DEBUG_FS +/* i2c debugfs directory: used to keep health monitor of i2c devices */ +static struct dentry *npcm_i2c_debugfs_dir; + +static void i2c_init_debugfs (struct platform_device *pdev,struct npcm_i2c *bus) +{ +struct dentry *d; + +if (!npcm_i2c_debugfs_dir) +return ; + + d =debugfs_create_dir (dev_name (&pdev->dev), npcm_i2c_debugfs_dir); +if (IS_ERR_OR_NULL (d)) +return ; + +debugfs_create_u64 ("ber_cnt" ,0444 , d, &bus->ber_cnt); +debugfs_create_u64 ("nack_cnt" ,0444 , d, &bus->nack_cnt); +debugfs_create_u64 ("rec_succ_cnt" ,0444 , d, &bus->rec_succ_cnt); +debugfs_create_u64 ("rec_fail_cnt" ,0444 , d, &bus->rec_fail_cnt); +debugfs_create_u64 ("timeout_cnt" ,0444 , d, &bus->timeout_cnt); + + bus->debugfs = d; +} +#else +static void i2c_init_debugfs (struct platform_device *pdev,struct npcm_i2c *bus) +{ +} +#endif + +static int npcm_i2c_probe_bus (struct platform_device *pdev) +{ +struct npcm_i2c *bus; +struct i2c_adapter *adap; +struct clk *i2c_clk; +static struct regmap *gcr_regmap; +static struct regmap *clk_regmap; +int ret;
drivers/i2c/busses/i2c-npcm7xx.c line 1695
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+int num; + + bus =devm_kzalloc (&pdev->dev,sizeof (*bus),GFP_KERNEL ); +if (!bus) +return -ENOMEM ; + + bus->dev = &pdev->dev; + + num =of_alias_get_id (pdev->dev.of_node,"i2c" ); + bus->num = num;
Why not assigning it directly and save the 'num' variable?
Quoted 15 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +15
-0+/* core clk must be acquired to calculate module timing settings */ + i2c_clk =devm_clk_get (&pdev->dev,NULL ); +if (IS_ERR (i2c_clk)) +return PTR_ERR (i2c_clk); + bus->apb_clk =clk_get_rate (i2c_clk); + + gcr_regmap =syscon_regmap_lookup_by_compatible ("nuvoton,npcm750-gcr" ); +if (IS_ERR (gcr_regmap)) +return IS_ERR (gcr_regmap); +regmap_write (gcr_regmap,NPCM_I2CSEGCTL ,NPCM_I2CSEGCTL_INIT_VAL ); + + clk_regmap =syscon_regmap_lookup_by_compatible ("nuvoton,npcm750-clk" ); +if (IS_ERR (clk_regmap)) +return IS_ERR (clk_regmap); +
drivers/i2c/busses/i2c-npcm7xx.c line 1720
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+ bus->reg =devm_platform_ioremap_resource (pdev,0 ); +if (IS_ERR (bus->reg)) +return PTR_ERR ((bus)->reg); + +spin_lock_init (&bus->lock); +init_completion (&bus->cmd_complete); + + adap = &bus->adap; + adap->owner =THIS_MODULE ; + adap->class =I2C_CLASS_HWMON |I2C_CLASS_SPD |I2C_CLIENT_SLAVE ;
Since you have a DT compatible, you won't need classes. Just drop them.
Quoted 19 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +19
-0+ adap->retries =3 ; + adap->timeout =HZ ; + adap->algo = &npcm_i2c_algo; + adap->quirks = &npcm_i2c_quirks; + adap->algo_data = bus; + adap->dev.parent = &pdev->dev; + adap->dev.of_node = pdev->dev.of_node; + adap->nr = pdev->id; + + bus->irq =platform_get_irq (pdev,0 ); +if (bus->irq <0 ) +return bus->irq; + + ret =devm_request_irq (bus->dev, bus->irq, npcm_i2c_bus_irq,0 , +dev_name (bus->dev), bus); +if (ret) +return ret; + + ret =__npcm_i2c_init (bus, pdev);
drivers/i2c/busses/i2c-npcm7xx.c line 1749
Quoted 10 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +10
-0+if (ret) +return ret; + + ret =npcm_i2c_recovery_init (adap); +if (ret) +return ret; + +i2c_set_adapdata (adap, bus); + +snprintf (bus->adap.name,sizeof (bus->adap.name),"Nuvoton i2c" );
Maybe you want to add something more specific in case you have multiple instances of this driver at runtime.
drivers/i2c/busses/i2c-npcm7xx.c line 1752
Quoted 3 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +3
-0+ ret =i2c_add_numbered_adapter (&bus->adap); +if (ret) { +dev_err (&pdev->dev,"failed to add numbered adapter %d\n" , ret);
The I2C core will print warnings for you.
Quoted 8 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +8
-0+return ret; + } +platform_set_drvdata (pdev, bus); + +i2c_init_debugfs (pdev, bus); +return 0 ; +} +
...
drivers/i2c/busses/i2c-npcm7xx.c line 1795
Quoted 6 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +6
-0+#ifdef CONFIG_DEBUG_FS +static int __init npcm_i2c_init (void ) +{ +struct dentry *dir; + + dir =debugfs_create_dir ("i2c" ,NULL );
Okay, the GPIO fault injector could also need such a directory. I will add this to the core. And then send an incremental patch for your driver.
Quoted 26 lines
drivers/i2c/busses/i2c-npcm7xx.c new file +23
-0+if (IS_ERR_OR_NULL (dir)) +return 0 ; + + npcm_i2c_debugfs_dir = dir; +return 0 ; +} + +static void __exit npcm_i2c_exit(void) +{ +debugfs_remove_recursive (npcm_i2c_debugfs_dir); +} + +module_init (npcm_i2c_init); +module_exit (npcm_i2c_exit); +#endif + +MODULE_AUTHOR ("Avi Fishman <[email protected]>" ); +MODULE_AUTHOR ("Tali Perry <[email protected]>" ); +MODULE_AUTHOR ("Tyrone Ting <[email protected]>" ); +MODULE_DESCRIPTION ("Nuvoton I2C Bus Driver" ); +MODULE_LICENSE ("GPL v2" ); +MODULE_VERSION ("0.1.3" ); +Signature · 1 line
2.22.0
Tali Perry
maintainerPATCH v12 3/3 i2c: npcm7xx: Add support for slave mode for Nuvoton
In reply to Tali PerryAdd support for slave mode for Nuvoton
NPCM BMC I2C controller driver.
--- drivers/i2c/busses/i2c-npcm7xx.c | 608 +++++++++++++++++++++++++++++++ 1 file changed, 608 insertions(+)
drivers/i2c/busses/i2c-npcm7xx.c +608 -0
@@ -71,6 +71,24 @@ enum i2c_state { I2C_STOP_PENDING, }; +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +/* Module supports setting multiple own slave addresses */ +enum i2c_addr { +I2C_SLAVE_ADDR1 =0 , +I2C_SLAVE_ADDR2 , +I2C_SLAVE_ADDR3 , +I2C_SLAVE_ADDR4 , +I2C_SLAVE_ADDR5 , +I2C_SLAVE_ADDR6 , +I2C_SLAVE_ADDR7 , +I2C_SLAVE_ADDR8 , +I2C_SLAVE_ADDR9 , +I2C_SLAVE_ADDR10 , +I2C_GC_ADDR , +I2C_ARP_ADDR , +}; +#endif +/* init register and default value required to enable module */ #define NPCM_I2CSEGCTL 0xE4#define NPCM_I2CSEGCTL_INIT_VAL 0x0333F000
@@ -98,6 +116,21 @@ enum i2c_state {#define NPCM_I2CADDR6 0x16#define NPCM_I2CADDR10 0x17 +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +/* +* npcm_i2caddr array: +* The module supports having multiple own slave addresses. +* Since the addr regs are sprinkled all over the address space, +* use this array to get the address or each register. +*/ +#define I2C_NUM_OWN_ADDR 10 +const int npcm_i2caddr[I2C_NUM_OWN_ADDR ] = {NPCM_I2CADDR1 ,NPCM_I2CADDR2 , +NPCM_I2CADDR3 ,NPCM_I2CADDR4 , +NPCM_I2CADDR5 ,NPCM_I2CADDR6 , +NPCM_I2CADDR7 ,NPCM_I2CADDR8 , +NPCM_I2CADDR9 ,NPCM_I2CADDR10 }; +#endif +#define NPCM_I2CCTL4 0x1A#define NPCM_I2CCTL5 0x1B#define NPCM_I2CSCLLT 0x1C/* SCL Low Time */
@@ -269,6 +302,16 @@ struct npcm_i2c {unsigned long int_time_stamp;unsigned long bus_freq;/* in kHz */ u32 xmits; +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +u8 own_slave_addr; +struct i2c_client *slave; +int slv_rd_size; +int slv_rd_ind; +int slv_wr_size; +int slv_wr_ind; +u8 slv_rd_buf[I2C_HW_FIFO_SIZE ]; +u8 slv_wr_buf[I2C_HW_FIFO_SIZE ]; +#endif #ifdef CONFIG_DEBUG_FS struct dentry *debugfs;/* debugfs device directory */ u64 ber_cnt;
@@ -303,6 +346,10 @@ static void npcm_i2c_init_params(struct npcm_i2c *bus) bus->int_time_stamp =0 ; bus->PEC_use = false; bus->PEC_mask =0 ; +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +if (bus->slave) + bus->master_or_slave =I2C_SLAVE ; +#endif }static inline void npcm_i2c_wr_byte (struct npcm_i2c *bus,u8 data)
@@ -348,6 +395,18 @@ static void npcm_i2c_disable(struct npcm_i2c *bus) {u8 i2cctl2; +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +int i; + +/* select bank 0 for I2C addresses */ +npcm_i2c_select_bank (bus,I2C_BANK_0 ); + +/* Slave addresses removal */ +for (i =I2C_SLAVE_ADDR1 ; i <I2C_NUM_OWN_ADDR ; i++) +iowrite8 (0 , bus->reg + npcm_i2caddr[i]); + +npcm_i2c_select_bank (bus,I2C_BANK_1 ); +#endif /* Disable module */ i2cctl2 =ioread8 (bus->reg +NPCM_I2CCTL2 ); i2cctl2 = i2cctl2 & ~I2CCTL2_ENABLE ;
@@ -511,6 +570,61 @@ static inline void npcm_i2c_nack(struct npcm_i2c *bus)iowrite8 (val, bus->reg +NPCM_I2CCTL1 ); } +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +static void npcm_i2c_slave_int_enable (struct npcm_i2c *bus,bool enable) +{ +u8 i2cctl1; + +/* enable interrupt on slave match: */ + i2cctl1 =ioread8 (bus->reg +NPCM_I2CCTL1 ); + i2cctl1 &= ~NPCM_I2CCTL1_RWS ; +if (enable) + i2cctl1 |=NPCM_I2CCTL1_NMINTE ; +else + i2cctl1 &= ~NPCM_I2CCTL1_NMINTE ; +iowrite8 (i2cctl1, bus->reg +NPCM_I2CCTL1 ); +} + +static int npcm_i2c_slave_enable (struct npcm_i2c *bus,enum i2c_addr addr_type, +u8 addr,bool enable) +{ +u8 i2cctl1; +u8 i2cctl3; +u8 sa_reg; + + sa_reg = (addr &0x7F ) |FIELD_PREP (NPCM_I2CADDR_SAEN , enable); +if (addr_type ==I2C_GC_ADDR ) { + i2cctl1 =ioread8 (bus->reg +NPCM_I2CCTL1 ); +if (enable) + i2cctl1 |=NPCM_I2CCTL1_GCMEN ; +else + i2cctl1 &= ~NPCM_I2CCTL1_GCMEN ; +iowrite8 (i2cctl1, bus->reg +NPCM_I2CCTL1 ); +return 0 ; + } +if (addr_type ==I2C_ARP_ADDR ) { + i2cctl3 =ioread8 (bus->reg +NPCM_I2CCTL3 ); +if (enable) + i2cctl3 |=I2CCTL3_ARPMEN ; +else + i2cctl3 &= ~I2CCTL3_ARPMEN ; +iowrite8 (i2cctl3, bus->reg +NPCM_I2CCTL3 ); +return 0 ; + } +if (addr_type >=I2C_ARP_ADDR ) +return -EFAULT ; +/* select bank 0 for address 3 to 10 */ +if (addr_type >I2C_SLAVE_ADDR2 ) +npcm_i2c_select_bank (bus,I2C_BANK_0 ); +/* Set and enable the address */ +iowrite8 (sa_reg, bus->reg + npcm_i2caddr[(int )addr_type]); +npcm_i2c_slave_int_enable (bus, enable); +if (addr_type >I2C_SLAVE_ADDR2 ) +npcm_i2c_select_bank (bus,I2C_BANK_1 ); +return 0 ; +} +#endif +static void npcm_i2c_reset (struct npcm_i2c *bus) {/*
@@ -540,6 +654,12 @@ static void npcm_i2c_reset(struct npcm_i2c *bus)/* Clear all fifo bits: */ iowrite8 (NPCM_I2CFIF_CTS_CLR_FIFO , bus->reg +NPCM_I2CFIF_CTS ); +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +if (bus->slave) +npcm_i2c_slave_enable (bus,I2C_SLAVE_ADDR1 , bus->slave->addr, + true); +#endif + bus->state =I2C_IDLE ; }
@@ -605,6 +725,10 @@ static void npcm_i2c_callback(struct npcm_i2c *bus, } bus->operation =I2C_NO_OPER ; +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +if (bus->slave) + bus->master_or_slave =I2C_SLAVE ; +#endif }static u8 npcm_i2c_fifo_usage (struct npcm_i2c *bus)
@@ -716,6 +840,466 @@ static void npcm_i2c_master_abort(struct npcm_i2c *bus)npcm_i2c_clear_master_status (bus); } +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +static u8 npcm_i2c_get_slave_addr (struct npcm_i2c *bus,enum i2c_addr addr_type) +{ +u8 slave_add; + +/* select bank 0 for address 3 to 10 */ +if (addr_type >I2C_SLAVE_ADDR2 ) +npcm_i2c_select_bank (bus,I2C_BANK_0 ); + + slave_add =ioread8 (bus->reg + npcm_i2caddr[(int )addr_type]); + +if (addr_type >I2C_SLAVE_ADDR2 ) +npcm_i2c_select_bank (bus,I2C_BANK_1 ); + +return slave_add; +} + +static int npcm_i2c_remove_slave_addr (struct npcm_i2c *bus,u8 slave_add) +{ +int i; + +/* Set the enable bit */ + slave_add |=0x80 ; +npcm_i2c_select_bank (bus,I2C_BANK_0 ); +for (i =I2C_SLAVE_ADDR1 ; i <I2C_NUM_OWN_ADDR ; i++) { +if (ioread8 (bus->reg + npcm_i2caddr[i]) == slave_add) +iowrite8 (0 , bus->reg + npcm_i2caddr[i]); + } +npcm_i2c_select_bank (bus,I2C_BANK_1 ); +return 0 ; +} + +static void npcm_i2c_write_fifo_slave (struct npcm_i2c *bus,u16 max_bytes) +{ +u8 size_free_fifo; +/* +* Fill the FIFO, while the FIFO is not full and there are more bytes +* to write +*/ +npcm_i2c_clear_fifo_int (bus); +npcm_i2c_clear_tx_fifo (bus); +iowrite8 (0 , bus->reg +NPCM_I2CTXF_CTL ); + size_free_fifo =I2C_HW_FIFO_SIZE -npcm_i2c_fifo_usage (bus); +while (max_bytes-- && size_free_fifo) { +if (bus->slv_wr_size >0 ) { + bus->slv_wr_ind = bus->slv_wr_ind %I2C_HW_FIFO_SIZE ; +npcm_i2c_wr_byte (bus, bus->slv_wr_buf[bus->slv_wr_ind]); + bus->slv_wr_ind++; + bus->slv_wr_ind = bus->slv_wr_ind %I2C_HW_FIFO_SIZE ; + bus->slv_wr_size--; + size_free_fifo =I2C_HW_FIFO_SIZE - +npcm_i2c_fifo_usage (bus); + }else { +break ; + } + } +} + +static void npcm_i2c_read_fifo_slave (struct npcm_i2c *bus,u8 bytes_in_fifo) +{ +u8 data; + +if (!bus->slave) +return ; + +while (bytes_in_fifo--) { + data =npcm_i2c_rd_byte (bus); + + bus->slv_rd_ind = bus->slv_rd_ind %I2C_HW_FIFO_SIZE ; + bus->slv_rd_buf[bus->slv_rd_ind] = data; + bus->slv_rd_ind++; + +/* 1st byte is length in block protocol: */ +if (bus->slv_rd_ind ==1 && bus->read_block_use) + bus->slv_rd_size = data + bus->PEC_use +1 ; + } +} + +static int npcm_i2c_slave_get_wr_buf (struct npcm_i2c *bus) +{ +int i; +u8 value =0 ; +int ind; +int ret = bus->slv_wr_ind; + +/* fill a cyclic buffer */ +for (i =0 ; i <I2C_HW_FIFO_SIZE ; i++) { +if (bus->slv_wr_size >=I2C_HW_FIFO_SIZE ) +break ; +i2c_slave_event (bus->slave,I2C_SLAVE_READ_REQUESTED , &value); + ind = (bus->slv_wr_ind + bus->slv_wr_size) %I2C_HW_FIFO_SIZE ; + bus->slv_wr_buf[ind] = value; + bus->slv_wr_size++; +i2c_slave_event (bus->slave,I2C_SLAVE_READ_PROCESSED , &value); + } +return I2C_HW_FIFO_SIZE - ret; +} + +static void npcm_i2c_slave_send_rd_buf (struct npcm_i2c *bus) +{ +int i; + +for (i =0 ; i < bus->slv_rd_ind; i++) +i2c_slave_event (bus->slave,I2C_SLAVE_WRITE_RECEIVED , + &bus->slv_rd_buf[i]); +/* +* once we send bytes up, need to reset the counter of the wr buf +* got data from master (new offset in device), ignore wr fifo: +*/ +if (bus->slv_rd_ind) { + bus->slv_wr_size =0 ; + bus->slv_wr_ind =0 ; + } + + bus->slv_rd_ind =0 ; + bus->slv_rd_size = bus->adap.quirks->max_read_len; + +npcm_i2c_clear_fifo_int (bus); +npcm_i2c_clear_rx_fifo (bus); +} + +static void npcm_i2c_slave_receive (struct npcm_i2c *bus,u16 nread, +u8 *read_data) +{ + bus->state =I2C_OPER_STARTED ; + bus->operation =I2C_READ_OPER ; + bus->slv_rd_size = nread; + bus->slv_rd_ind =0 ; + +iowrite8 (0 , bus->reg +NPCM_I2CTXF_CTL ); +iowrite8 (I2C_HW_FIFO_SIZE , bus->reg +NPCM_I2CRXF_CTL ); +npcm_i2c_clear_tx_fifo (bus); +npcm_i2c_clear_rx_fifo (bus); +} + +static void npcm_i2c_slave_xmit (struct npcm_i2c *bus,u16 nwrite, +u8 *write_data) +{ +if (nwrite ==0 ) +return ; + + bus->state =I2C_OPER_STARTED ; + bus->operation =I2C_WRITE_OPER ; + +/* get the next buffer */ +npcm_i2c_slave_get_wr_buf (bus); +npcm_i2c_write_fifo_slave (bus, nwrite); +} + +/* +* npcm_i2c_slave_wr_buf_sync: +* currently slave IF only supports single byte operations. +* in order to utilyze the npcm HW FIFO, the driver will ask for 16 bytes +* at a time, pack them in buffer, and then transmit them all together +* to the FIFO and onward to the bus. +* NACK on read will be once reached to bus->adap->quirks->max_read_len. +* sending a NACK wherever the backend requests for it is not supported. +* the next two functions allow reading to local buffer before writing it all +* to the HW FIFO. +*/ +static void npcm_i2c_slave_wr_buf_sync (struct npcm_i2c *bus) +{ +int left_in_fifo; + + left_in_fifo =FIELD_GET (NPCM_I2CTXF_STS_TX_BYTES , +ioread8 (bus->reg +NPCM_I2CTXF_STS )); + +/* fifo already full: */ +if (left_in_fifo >=I2C_HW_FIFO_SIZE || + bus->slv_wr_size >=I2C_HW_FIFO_SIZE ) +return ; + +/* update the wr fifo index back to the untransmitted bytes: */ + bus->slv_wr_ind = bus->slv_wr_ind - left_in_fifo; + bus->slv_wr_size = bus->slv_wr_size + left_in_fifo; + +if (bus->slv_wr_ind <0 ) + bus->slv_wr_ind +=I2C_HW_FIFO_SIZE ; +} + +static void npcm_i2c_slave_rd_wr (struct npcm_i2c *bus) +{ +if (NPCM_I2CST_XMIT &ioread8 (bus->reg +NPCM_I2CST )) { +/* +* Slave got an address match with direction bit 1 so it should +* transmit data. Write till the master will NACK +*/ + bus->operation =I2C_WRITE_OPER ; +npcm_i2c_slave_xmit (bus, bus->adap.quirks->max_write_len, + bus->slv_wr_buf); + }else { +/* +* Slave got an address match with direction bit 0 so it should +* receive data. +* this module does not support saying no to bytes. +* it will always ACK. +*/ + bus->operation =I2C_READ_OPER ; +npcm_i2c_read_fifo_slave (bus,npcm_i2c_fifo_usage (bus)); + bus->stop_ind =I2C_SLAVE_RCV_IND ; +npcm_i2c_slave_send_rd_buf (bus); +npcm_i2c_slave_receive (bus, bus->adap.quirks->max_read_len, + bus->slv_rd_buf); + } +} + +static irqreturn_t npcm_i2c_int_slave_handler (struct npcm_i2c *bus) +{ +u8 val; +irqreturn_t ret =IRQ_NONE ; +u8 i2cst =ioread8 (bus->reg +NPCM_I2CST ); + +/* Slave: A NACK has occurred */ +if (NPCM_I2CST_NEGACK & i2cst) { + bus->stop_ind =I2C_NACK_IND ; +npcm_i2c_slave_wr_buf_sync (bus); +if (bus->fifo_use) +/* clear the FIFO */ +iowrite8 (NPCM_I2CFIF_CTS_CLR_FIFO , + bus->reg +NPCM_I2CFIF_CTS ); + +/* In slave write, NACK is OK, otherwise it is a problem */ + bus->stop_ind =I2C_NO_STATUS_IND ; + bus->operation =I2C_NO_OPER ; + bus->own_slave_addr =0xFF ; + +/* +* Slave has to wait for STOP to decide this is the end +* of the transaction. tx is not yet considered as done +*/ +iowrite8 (NPCM_I2CST_NEGACK , bus->reg +NPCM_I2CST ); + + ret =IRQ_HANDLED ; + } + +/* Slave mode: a Bus Error (BER) has been identified */ +if (NPCM_I2CST_BER & i2cst) { +/* +* Check whether bus arbitration or Start or Stop during data +* xfer bus arbitration problem should not result in recovery +*/ + bus->stop_ind =I2C_BUS_ERR_IND ; + +/* wait for bus busy before clear fifo */ +iowrite8 (NPCM_I2CFIF_CTS_CLR_FIFO , bus->reg +NPCM_I2CFIF_CTS ); + + bus->state =I2C_IDLE ; + +/* +* in BER case we might get 2 interrupts: one for slave one for +* master ( for a channel which is master\slave switching) +*/ +if (completion_done (&bus->cmd_complete) == false) { + bus->cmd_err = -EIO ; +complete (&bus->cmd_complete); + } + bus->own_slave_addr =0xFF ; +iowrite8 (NPCM_I2CST_BER , bus->reg +NPCM_I2CST ); + ret =IRQ_HANDLED ; + } + +/* A Slave Stop Condition has been identified */ +if (NPCM_I2CST_SLVSTP & i2cst) { +u8 bytes_in_fifo =npcm_i2c_fifo_usage (bus); + + bus->stop_ind =I2C_SLAVE_DONE_IND ; + +if (bus->operation ==I2C_READ_OPER ) +npcm_i2c_read_fifo_slave (bus, bytes_in_fifo); + +/* if the buffer is empty nothing will be sent */ +npcm_i2c_slave_send_rd_buf (bus); + +/* Slave done transmitting or receiving */ + bus->stop_ind =I2C_NO_STATUS_IND ; + +/* +* Note, just because we got here, it doesn't mean we through +* away the wr buffer. +* we keep it until the next received offset. +*/ + bus->operation =I2C_NO_OPER ; + bus->int_cnt =0 ; + bus->own_slave_addr =0xFF ; +i2c_slave_event (bus->slave,I2C_SLAVE_STOP ,0 ); +iowrite8 (NPCM_I2CST_SLVSTP , bus->reg +NPCM_I2CST ); +if (bus->fifo_use) { +npcm_i2c_clear_fifo_int (bus); +npcm_i2c_clear_rx_fifo (bus); +npcm_i2c_clear_tx_fifo (bus); + +iowrite8 (NPCM_I2CFIF_CTS_CLR_FIFO , + bus->reg +NPCM_I2CFIF_CTS ); + } + bus->state =I2C_IDLE ; + ret =IRQ_HANDLED ; + } + +/* restart condition occurred and Rx-FIFO was not empty */ +if (bus->fifo_use &&FIELD_GET (NPCM_I2CFIF_CTS_SLVRSTR , +ioread8 (bus->reg +NPCM_I2CFIF_CTS ))) { + bus->stop_ind =I2C_SLAVE_RESTART_IND ; + bus->master_or_slave =I2C_SLAVE ; +if (bus->operation ==I2C_READ_OPER ) +npcm_i2c_read_fifo_slave (bus,npcm_i2c_fifo_usage (bus)); + bus->operation =I2C_WRITE_OPER ; +iowrite8 (0 , bus->reg +NPCM_I2CRXF_CTL ); + val =NPCM_I2CFIF_CTS_CLR_FIFO |NPCM_I2CFIF_CTS_SLVRSTR | +NPCM_I2CFIF_CTS_RXF_TXE ; +iowrite8 (val, bus->reg +NPCM_I2CFIF_CTS ); +npcm_i2c_slave_rd_wr (bus); + ret =IRQ_HANDLED ; + } + +/* A Slave Address Match has been identified */ +if (NPCM_I2CST_NMATCH & i2cst) { +u8 info =0 ; + +/* Address match automatically implies slave mode */ + bus->master_or_slave =I2C_SLAVE ; +npcm_i2c_clear_fifo_int (bus); +npcm_i2c_clear_rx_fifo (bus); +npcm_i2c_clear_tx_fifo (bus); +iowrite8 (0 , bus->reg +NPCM_I2CTXF_CTL ); +iowrite8 (I2C_HW_FIFO_SIZE , bus->reg +NPCM_I2CRXF_CTL ); +if (NPCM_I2CST_XMIT & i2cst) { + bus->operation =I2C_WRITE_OPER ; + }else { +i2c_slave_event (bus->slave,I2C_SLAVE_WRITE_REQUESTED , + &info); + bus->operation =I2C_READ_OPER ; + } +if (bus->own_slave_addr ==0xFF ) { +/* Check which type of address match */ + val =ioread8 (bus->reg +NPCM_I2CCST ); +if (NPCM_I2CCST_MATCH & val) { +u16 addr; +enum i2c_addr eaddr; +u8 i2ccst2; +u8 i2ccst3; + + i2ccst3 =ioread8 (bus->reg +NPCM_I2CCST3 ); + i2ccst2 =ioread8 (bus->reg +NPCM_I2CCST2 ); + +/* +* the i2c module can response to 10 own SA. +* check which one was addressed by the master. +* repond to the first one. +*/ + addr = ((i2ccst3 &0x07 ) <<7 ) | + (i2ccst2 &0x7F ); + info =ffs (addr); + eaddr = (enum i2c_addr )info; + addr =npcm_i2c_get_slave_addr (bus, eaddr); + addr &=0x7F ; + bus->own_slave_addr = addr; +if (bus->PEC_mask &BIT (info)) + bus->PEC_use = true; +else + bus->PEC_use = false; + }else { +if (NPCM_I2CCST_GCMATCH & val) + bus->own_slave_addr =0 ; +if (NPCM_I2CCST_ARPMATCH & val) + bus->own_slave_addr =0x61 ; + } + }else { +/* +* Slave match can happen in two options: +* 1. Start, SA, read (slave read without further ado) +* 2. Start, SA, read, data, restart, SA, read, ... +* (slave read in fragmented mode) +* 3. Start, SA, write, data, restart, SA, read, .. +* (regular write-read mode) +*/ +if ((bus->state ==I2C_OPER_STARTED && + bus->operation ==I2C_READ_OPER && + bus->stop_ind ==I2C_SLAVE_XMIT_IND ) || + bus->stop_ind ==I2C_SLAVE_RCV_IND ) { +/* slave tx after slave rx w/o STOP */ + bus->stop_ind =I2C_SLAVE_RESTART_IND ; + } + } + +if (NPCM_I2CST_XMIT & i2cst) + bus->stop_ind =I2C_SLAVE_XMIT_IND ; +else + bus->stop_ind =I2C_SLAVE_RCV_IND ; + bus->state =I2C_SLAVE_MATCH ; +npcm_i2c_slave_rd_wr (bus); +iowrite8 (NPCM_I2CST_NMATCH , bus->reg +NPCM_I2CST ); + ret =IRQ_HANDLED ; + } + +/* Slave SDA status is set - tx or rx */ +if ((NPCM_I2CST_SDAST & i2cst) || + (bus->fifo_use && + (npcm_i2c_tx_fifo_empty (bus) ||npcm_i2c_rx_fifo_full (bus)))) { +npcm_i2c_slave_rd_wr (bus); +iowrite8 (NPCM_I2CST_SDAST , bus->reg +NPCM_I2CST ); + ret =IRQ_HANDLED ; + }/* SDAST */ + +return ret; +} + +static int npcm_i2c_reg_slave (struct i2c_client *client) +{ +unsigned long lock_flags; +struct npcm_i2c *bus =i2c_get_adapdata (client->adapter); + + bus->slave = client; + +if (!bus->slave) +return -EINVAL ; + +if (client->flags &I2C_CLIENT_TEN ) +return -EAFNOSUPPORT ; + +spin_lock_irqsave (&bus->lock, lock_flags); + +npcm_i2c_init_params (bus); + bus->slv_rd_size =0 ; + bus->slv_wr_size =0 ; + bus->slv_rd_ind =0 ; + bus->slv_wr_ind =0 ; +if (client->flags &I2C_CLIENT_PEC ) + bus->PEC_use = true; + +dev_info (bus->dev,"i2c%d register slave SA=0x%x, PEC=%d\n" , bus->num, + client->addr, bus->PEC_use); + +npcm_i2c_slave_enable (bus,I2C_SLAVE_ADDR1 , client->addr, true); +npcm_i2c_clear_fifo_int (bus); +npcm_i2c_clear_rx_fifo (bus); +npcm_i2c_clear_tx_fifo (bus); +npcm_i2c_slave_int_enable (bus, true); + +spin_unlock_irqrestore (&bus->lock, lock_flags); +return 0 ; +} + +static int npcm_i2c_unreg_slave (struct i2c_client *client) +{ +struct npcm_i2c *bus = client->adapter->algo_data; +unsigned long lock_flags; + +spin_lock_irqsave (&bus->lock, lock_flags); +if (!bus->slave) { +spin_unlock_irqrestore (&bus->lock, lock_flags); +return -EINVAL ; + } +npcm_i2c_slave_int_enable (bus, false); +npcm_i2c_remove_slave_addr (bus, client->addr); + bus->slave =NULL ; +spin_unlock_irqrestore (&bus->lock, lock_flags); +return 0 ; +} +#endif /* CONFIG_I2C_SLAVE */ +static void npcm_i2c_master_fifo_read (struct npcm_i2c *bus) {int rcount;
@@ -1401,6 +1985,9 @@ static int __npcm_i2c_init(struct npcm_i2c *bus, struct platform_device *pdev) bus->master_or_slave =I2C_SLAVE ; bus->int_time_stamp =0 ; bus->xmits =0 ; +#if IS_ENABLED (CONFIG_I2C_SLAVE ) + bus->slave =NULL ; +#endif ret =device_property_read_u32 (&pdev->dev,"bus-frequency" , &clk_freq_hz);
@@ -1431,6 +2018,12 @@ static irqreturn_t npcm_i2c_bus_irq(int irq, void *dev_id)if (!npcm_i2c_int_master_handler (bus))return IRQ_HANDLED ; } +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +if (bus->slave) { + bus->master_or_slave =I2C_SLAVE ; +return npcm_i2c_int_slave_handler (bus); + } +#endif return IRQ_NONE ; }
@@ -1572,6 +2165,11 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,*/ spin_lock_irqsave (&bus->lock, flags); bus_busy =ioread8 (bus->reg +NPCM_I2CCST ) &NPCM_I2CCST_BB ; +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +if (!bus_busy && bus->slave) +iowrite8 ((bus->slave->addr &0x7F ), + bus->reg +NPCM_I2CADDR1 ); +#endif spin_unlock_irqrestore (&bus->lock, flags); }while (time_is_after_jiffies (time_left) && bus_busy);
@@ -1622,6 +2220,12 @@ static int npcm_i2c_master_xfer(struct i2c_adapter *adap, struct i2c_msg *msgs,if (bus->cmd_err == -EAGAIN ) ret =i2c_recover_bus (adap); +#if IS_ENABLED (CONFIG_I2C_SLAVE ) +/* reenable slave if it was enabled */ +if (bus->slave) +iowrite8 ((bus->slave->addr &0x7F ) |NPCM_I2CADDR_SAEN , + bus->reg +NPCM_I2CADDR1 ); +#endif return bus->cmd_err; }
@@ -1644,6 +2248,10 @@ static const struct i2c_adapter_quirks npcm_i2c_quirks = {static const struct i2c_algorithm npcm_i2c_algo = { .master_xfer = npcm_i2c_master_xfer, .functionality = npcm_i2c_functionality, +#if IS_ENABLED(CONFIG_I2C_SLAVE) + .reg_slave = npcm_i2c_reg_slave, + .unreg_slave = npcm_i2c_unreg_slave, +#endif };#ifdef CONFIG_DEBUG_FS
Signature · 1 line
2.22.0
Re: [PATCH v12 3/3] i2c: npcm7xx: Add support for slave mode for Nuvoton
Quoted 2 lines · Tali Perry
Add support for slave mode for Nuvoton
NPCM BMC I2C controller driver.
...
drivers/i2c/busses/i2c-npcm7xx.c enum i2c_state { · line 127
Quoted 9 lines
drivers/i2c/busses/i2c-npcm7xx.c +9
-0+#if IS_ENABLED (CONFIG_I2C_SLAVE ) +/* +* npcm_i2caddr array: +* The module supports having multiple own slave addresses. +* Since the addr regs are sprinkled all over the address space, +* use this array to get the address or each register. +*/ +#define I2C_NUM_OWN_ADDR 10 +const int npcm_i2caddr[I2C_NUM_OWN_ADDR] = {NPCM_I2CADDR1, NPCM_I2CADDR2,
Extra spaces.
On top. please start assignment from the new line.
drivers/i2c/busses/i2c-npcm7xx.c enum i2c_state { · line 131
Quoted 5 lines
drivers/i2c/busses/i2c-npcm7xx.c +4
-0+NPCM_I2CADDR3 ,NPCM_I2CADDR4 , +NPCM_I2CADDR5 ,NPCM_I2CADDR6 , +NPCM_I2CADDR7 ,NPCM_I2CADDR8 , + NPCM_I2CADDR9, NPCM_I2CADDR10};
Split }; to new line and leave comma with the last member.
Quoted 1 line
+#endif
...
drivers/i2c/busses/i2c-npcm7xx.c static inline void npcm_i2c_nack(struct npcm_i2c *bus) · line 589
Quoted 2 lines
drivers/i2c/busses/i2c-npcm7xx.c +2
-0+static int npcm_i2c_slave_enable (struct npcm_i2c *bus,enum i2c_addr addr_type, +u8 addr,bool enable)
Extra spaces. Check entire patch for that and fix accordingly.
drivers/i2c/busses/i2c-npcm7xx.c static void npcm_i2c_reset(struct npcm_i2c *bus) · line 660
Quoted 5 lines
drivers/i2c/busses/i2c-npcm7xx.c +4
-0+#if IS_ENABLED (CONFIG_I2C_SLAVE ) +if (bus->slave) +npcm_i2c_slave_enable (bus,I2C_SLAVE_ADDR1 , bus->slave->addr, + true);
I would leave this on one line.
Quoted 1 line
+#endif
...
drivers/i2c/busses/i2c-npcm7xx.c static void npcm_i2c_master_abort(struct npcm_i2c *bus) · line 877
Quoted 3 lines
drivers/i2c/busses/i2c-npcm7xx.c +3
-0+static void npcm_i2c_write_fifo_slave (struct npcm_i2c *bus,u16 max_bytes) +{ +u8 size_free_fifo;
+ blank line.
drivers/i2c/busses/i2c-npcm7xx.c static void npcm_i2c_master_abort(struct npcm_i2c *bus) · line 885
Quoted 9 lines
drivers/i2c/busses/i2c-npcm7xx.c +8
-0+/* +* Fill the FIFO, while the FIFO is not full and there are more bytes +* to write +*/ +npcm_i2c_clear_fifo_int (bus); +npcm_i2c_clear_tx_fifo (bus); +iowrite8 (0 , bus->reg +NPCM_I2CTXF_CTL ); + size_free_fifo =I2C_HW_FIFO_SIZE -npcm_i2c_fifo_usage (bus);
Dup, move into loop.
drivers/i2c/busses/i2c-npcm7xx.c static void npcm_i2c_master_abort(struct npcm_i2c *bus) · line 898
Quoted 13 lines
drivers/i2c/busses/i2c-npcm7xx.c +13
-0+while (max_bytes-- && size_free_fifo) { +if (bus->slv_wr_size >0 ) { + bus->slv_wr_ind = bus->slv_wr_ind %I2C_HW_FIFO_SIZE ; +npcm_i2c_wr_byte (bus, bus->slv_wr_buf[bus->slv_wr_ind]); + bus->slv_wr_ind++; + bus->slv_wr_ind = bus->slv_wr_ind %I2C_HW_FIFO_SIZE ; + bus->slv_wr_size--; + size_free_fifo =I2C_HW_FIFO_SIZE - +npcm_i2c_fifo_usage (bus); + }else { +break ; + } + }
while (...) {
if (...)
break;
...
}Quoted 1 line
+}
...
drivers/i2c/busses/i2c-npcm7xx.c static void npcm_i2c_master_abort(struct npcm_i2c *bus) · line 924
Quoted 5 lines
drivers/i2c/busses/i2c-npcm7xx.c +4
-0+static int npcm_i2c_slave_get_wr_buf (struct npcm_i2c *bus) +{ +int i; +u8 value =0 ;
Redundant assignment.
Quoted 15 lines
drivers/i2c/busses/i2c-npcm7xx.c +15
-0+int ind; +int ret = bus->slv_wr_ind; + +/* fill a cyclic buffer */ +for (i =0 ; i <I2C_HW_FIFO_SIZE ; i++) { +if (bus->slv_wr_size >=I2C_HW_FIFO_SIZE ) +break ; +i2c_slave_event (bus->slave,I2C_SLAVE_READ_REQUESTED , &value); + ind = (bus->slv_wr_ind + bus->slv_wr_size) %I2C_HW_FIFO_SIZE ; + bus->slv_wr_buf[ind] = value; + bus->slv_wr_size++; +i2c_slave_event (bus->slave,I2C_SLAVE_READ_PROCESSED , &value); + } +return I2C_HW_FIFO_SIZE - ret; +}
Signature · 2 lines
With Best Regards, Andy Shevchenko