-
Notifications
You must be signed in to change notification settings - Fork 8.3k
drivers: i2c_dw: add support for agilex platform and reset #60427
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hello @samitsingh7, and thank you very much for your first pull request to the Zephyr project!
A project maintainer just triggered our CI pipeline to run it against your PR and ensure it's compliant and doesn't cause any issues. You might want to take this opportunity to review the project's Contributor Expectations and make any updates to your pull request if necessary. 😊
drivers/i2c/i2c_shell.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This seems like a strange ifdef being added here, can you explain?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i2c scan shell command initiates i2c transfer without any data which is not a standard in i2c protocol but SMbus supports such transfers.
Also, the current i2c_dw driver is not handling 0 length i2c transfer. Thus, issuing this shell command could hang the system(which is happening in case of i2c_dw driver).
So by introducing the kconfig macro: CONFIG_I2C_SMBUS_SUPPORTED,
By default, this macro is enabled and the i2c scan shell command can be issued but if we disable this macro explicitly then the i2c scan shell command will not be available and thus preventing the program from getting hung if such i2c transfers without data are not handled in the driver.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
So if I'm understanding this correctly, the issue is on this device the zero sized transfer used by i2c scan here https://github.com/zephyrproject-rtos/zephyr/blob/main/drivers/i2c/i2c_shell.c#L79 is unsupported by this particular hardware or driver.
There's nothing in the i2c specification regarding sending an address only being invalid that I can find. Do you have a source for this being invalid?
The Kconfig here makes sense in terms of this particular hardware and driver combo possibly not supporting i2c scan. But to have a Kconfig like this is a bit strange to me. Instead the driver should return an error code on such transfers and this Kconfig is no longer needed I'd think.
If it's something the driver can possibly do on some instances of the designware controller then it should be a Kconfig along the lines of I2C_DW_HAS_ZERO_LEN_XFER or so and worked with in that way.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's nothing in the i2c specification regarding sending an address only being invalid that I can find. Do you have a source for this being invalid?
I agree that sending an address only in i2c transaction (Quick Command) is valid.
As per the comment of the i2c-scan function:-
WARNING: As there is no standard I2C detection command, this code
uses arbitrary SMBus commands (namely SMBus quick write and SMBus
receive byte) to probe for devices. This operation can confuse
your I2C bus, cause data loss, and is known to corrupt the Atmel
AT24RF08 EEPROM found on many IBM Thinkpad laptops.
https://manpages.debian.org/buster/i2c-tools/i2cdetect.8.en.html
We are issuing SMbus Quick command for i2c-scan functionality. Also, it is well documented that this operation could confuse i2c bus and cause data loss. or corrupt the target device. The same is documented here: https://manpages.debian.org/buster/i2c-tools/i2cdetect.8.en.html#WARNING.
Another such combination where this was not working: #54694.
Also, few of the I2C IPs support Quick Commands or 0 length i2c command based on the hardware configuration.
Thus, making use of this Kconfig macro we can avoid such conditions from various i2c controller and target device combination on various platform by disabling i2c-scan functionality for such applications where 0 length command is not supported.
The Kconfig here makes sense in terms of this particular hardware and driver combo possibly not supporting i2c scan.
Exactly as mentioned above and observed from various OS and hardware combination that i2c scan command could cause an issue. Thus introducing Kconfig macro which could help in enabling and disabling for the combination user have.
Instead the driver should return an error code on such transfers and this Kconfig is no longer needed I'd think.
Totally agreed and doing the same here, we are doing 0 length check and returning an error code - https://github.com/samitsingh7/zephyr_main/blob/i2c_dw_driver/drivers/i2c/i2c_dw.c#L478
The introduction of this Kconfig is rather for facilitating user to disable the i2c scan functionality from shell and safeguard from vulnerabilities from data corruption or i2c bus hang.
There could be different platforms where the i2c controller is not configured to support i2c quick commands.
Then all such platforms can make use of this macro to enable or disable this shell command.
We are still open to suggestions on alternative names for this macro: I2C_ZERO_LEN_XFER_SUPPORTED or I2C_QUICK_CMD_EN ?
Thanks for reviewing the changes!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Lets rename it I2C_ZERO_LEN_XFER_SUPPORTED as I think thats closer to the feature
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Requested changes done
drivers/i2c/i2c_shell.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make much sense to me, why is this needed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i2c shell command: i2c speed <device> <input>
will result in setting up the same speed either if we pass input as (1 or 9), (2 or 10), (3 or 11)..
Because in i2c speed shell function, I2C_SPEED_SET(speed) macro masks the speed parameter with 0x7.
Thus, introduced the check for the input parameter as passing the value more than 0x7 is not possible.
drivers/i2c/i2c_shell.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This doesn't make much sense, this is i2c not smbus (which has a separate API)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The name of the macro: CONFIG_I2C_SMBUS_SUPPORTED, is chosen like this because i2c transfer without any data is supported by SMbus.
By default, this macro is enabled and the i2c scan shell command will be available for an execution but if we disable this macro explicitly then the i2c scan shell command will not be available and thus preventing the program from getting hung if i2c transfers without data are not handled in the driver (ex: i2c_dw driver).
7089cbc to
057fc65
Compare
|
Renaming of the Kconfig option is a +1 from me |
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
|
I would like to have the stale label removed |
057fc65 to
fcb90c9
Compare
Done |
fcb90c9 to
26770a8
Compare
26770a8 to
1f407a9
Compare
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
bb63b97 to
d5a4a49
Compare
drivers/i2c/i2c_dw.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Changing back to ret will mostly solve the CI problem
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done
d5a4a49 to
655df96
Compare
drivers/i2c/Kconfig
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this be disabled by default and selected by drivers supporting zero-length transfers?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Some specific hardware configuration does not support zero-length transfers. In such cases, explicitly disable it. However, the default behavior should remain enabled to avoid impacting other configurations unnecessarily, given the current default is set to 'enabled.'"
drivers/i2c/i2c_dw.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We usually do not indent variable names like this in Zephyr. Please leave this change out of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/i2c/i2c_dw.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please leave the empty line before the return statement intact.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
done
drivers/i2c/i2c_dw.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please avoid mixing reformatting with functional changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Agreed, reverted reformatting changes
drivers/i2c/i2c_dw.c
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This change looks unrelated to the actual, functional change. Please leave this kind of changes out as it makes reading the diff much harder.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, the change is unrelated to functionality.
The variable name 'rc' was not very intuitive, so I updated it to 'ret' for clarity.
In the future, I’ll avoid making such changes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please move this rename into a separate commit.
Added device tree for I2C Signed-off-by: samit singh sikarwar <[email protected]>
Handling invalid 0 length i2c transfer and logging error if i2c transfer is initiated with length 0. Modified i2c shell to support i2c scan command only if I2C_ZERO_LEN_XFER_SUPPORTED Kconfig flag is enabled. Updated i2c shell for checking i2c-speed parameter in i2c speed shell command. Signed-off-by: samit singh sikarwar <[email protected]>
655df96 to
b1a2ae7
Compare
|
Hi @henrikbrixandersen- Addressed all the comments. Seeking your review. |
|
Hi @teburd : |
No, you did not address all of my comments. You are still adding support for Quick Commands, which is an SMBus feature, to the I2C API, which I commented on earlier, while we have a dedicated SMBus API. |
Hi @henrikbrixandersen - The existing i2c scan shell command initiates a transfer with zero-length data, which is not standard in I2C but is supported in SMBus. Since the i2c_dw driver does not handle this case properly, issuing the command can lead to a system hang. The macro allows users to disable the command explicitly, avoiding this issue without modifying I2C behavior. This change does not introduce SMBus support into the I2C API but rather provides a safeguard against unintended hangs in non-SMBus-capable I2C drivers. Let me know if further clarification is needed. |
| return MIN(MAX_BYTES_FOR_REGISTER_INDEX, length); | ||
| } | ||
|
|
||
| #if CONFIG_I2C_ZERO_LEN_XFER_SUPPORTED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I still do not understand why the Kconfig is needed. The driver returns an error on unsupported msg lengths. Why isn't this sufficient?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @henrikbrixandersen ,
I have explained the reasoning behind it here: #60427 (comment)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi @henrikbrixandersen, Please let me know if anything else is blocking this PR.
| ret = i2c_dw_set_master_mode(dev); | ||
|
|
||
| return ret; | ||
| return (int)i2c_dw_set_master_mode(dev); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This cast doesn't seem necessary.
static int i2c_dw_set_master_mode(const struct device *dev)
|
|
||
| #ifdef CONFIG_I2C_TARGET | ||
| static inline uint8_t i2c_dw_read_byte_non_blocking(const struct device *dev); | ||
| static inline int i2c_dw_read_byte_non_blocking(const struct device *dev, uint8_t *data); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Line 461 is still eave as old signature. Please run compilation.
data = i2c_dw_read_byte_non_blocking(port);
| The I2C shell supports scanning, bus recovery, I2C read and write | ||
| operations. | ||
|
|
||
| config I2C_ZERO_LEN_XFER_SUPPORTED |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If this config use only in i2c_sell.c, the name should be better start with I2C_SHELL_.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with @henrikbrixandersen - this Kconfig doesn't seem necessary if the I2C controllers that cannot support zero length transfers are required to return an error instead.
If you're going to keep the Kconfig, I suggest flipping the logic so that the Kconfig turns off zero length transfers. Something like CONFIG_I2C_DISABLE_ZERO_LENGTH_TRANSFER.
Also, could the zero length check get moved directly into the common i2c_transfer() syscall instead.
|
This pull request has been marked as stale because it has been open (more than) 60 days with no activity. Remove the stale label or add a comment saying that you would like to have the label removed otherwise this pull request will automatically be closed in 14 days. Note, that you can always re-open a closed pull request at any time. |
Uh oh!
There was an error while loading. Please reload this page.