summaryrefslogtreecommitdiff
path: root/common/i2c_controller.c
Commit message (Collapse)AuthorAgeFilesLines
* i2c: Add addr_flags and cmd to board_allow_i2c_passthru()Ricardo Quesada2022-03-171-5/+24
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | This CL adds addr_flags and cmd_id (AKA offset) to board_allow_i2c_passthru(). This CL is a kind-of-cherrypick of this other CL: https://crrev.com/c/1588492 BRANCH=none BUG=b:135642493 TEST=make buildall passes tested in Madoo where I manually added a board_allow_i2c_passthru() function that was blocking port 5 (battery) and did: With EC unlocked: dut # ectool i2cread 8 0x05 22 1 Read from I2C port 5 at 0x16 offset 0x1 = 0x67 With EC locked: dut # ectool i2cread 8 0x05 22 1 EC result 4 (ACCESS_DENIED) Change-Id: Ifba59f21f63f99345b81dd250296214a5cd29eba Signed-off-by: Ricardo Quesada <ricardoq@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3466422 Reviewed-by: Aseda Aboagye <aaboagye@chromium.org> Tested-by: Ricardo Quesada <ricardoq@chromium.org> Auto-Submit: Ricardo Quesada <ricardoq@chromium.org> Commit-Queue: Ricardo Quesada <ricardoq@chromium.org>
* i2c: move PASSTRHU_RESTRICTED check to check_i2c_paramsRicardo Quesada2022-03-171-8/+6
| | | | | | | | | | | | | | | | | | | | | | This CL moves the check about allowing PASSTHRU_RESTRICED checks from i2c_command_passthru() to check_i2c_params(). The rationale, is that we already have a function to check whether the params are valid and/or allowed. This is similar to what this CL did: https://crrev.com/c/1588492 BRANCH=none BUG=b:135642493 TEST=make buildall passes Change-Id: I5902feae8f5b626e10053f279cb2fe2cdbba2585 Signed-off-by: Ricardo Quesada <ricardoq@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3466415 Reviewed-by: Aseda Aboagye <aaboagye@chromium.org> Tested-by: Ricardo Quesada <ricardoq@chromium.org> Auto-Submit: Ricardo Quesada <ricardoq@chromium.org> Commit-Queue: Ricardo Quesada <ricardoq@chromium.org>
* common/i2c: Fix reading 32bit big endian registerGwendal Grignou2022-02-021-1/+1
| | | | | | | | | | | | | | | Byte swapping was incorrect. Fixes: cca70a517b ("common: Add i2c 32bit read/write") BUG=b:172225851 TEST=compile BRANCH=none Signed-off-by: Gwendal Grignou <gwendal@chromium.org> Change-Id: I00cec1858ee90d0b362e3cbfd10d4563c69fba43 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3425443 Reviewed-by: Keith Short <keithshort@chromium.org>
* Zephyr: Allow big endian flagDiana Z2021-12-161-2/+2
| | | | | | | | | | | | | | | | The flag for big endian i2c is handled in the upper layers of the i2c_controller code, so it should be fine to silently ignore this flag in the zephyr i2c logic. BRANCH=None BUG=b:195137794 TEST=zmake testall Signed-off-by: Diana Z <dzigterman@chromium.org> Change-Id: Ic7e532a2890d526684fc47aca2e561a73ae90f10 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3340222 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org>
* zephyr: add support for virtual batteryMichał Barnaś2021-10-151-3/+3
| | | | | | | | | | | | | | | | | | Add Kconfigs to enable virtual battery and set virtual battery address. Change named-i2c from I2C_PORT_VIRTUAL to I2C_PORT_VIRTUAL_BATTERY. BRANCH=main BUG=b:185480578 TEST=When virtual battery is enabled, compilation fails if no virtual battery port is defined in device tree. TEST=Enable virtual battery and verify that its handlers are called properly. Change-Id: Ic88da28dc443beb89a32868d201072f39abb3a86 Signed-off-by: Michał Barnaś <mb@semihalf.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3207991 Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Keith Short <keithshort@chromium.org>
* zephyr: optimize I2C mutexesDawid Niedzwiecki2021-10-111-3/+11
| | | | | | | | | | | | | | | | | | | | | | | | | I2C_PORT_COUNT is bigger than the actual physical I2C number in Zephyr. Since the access to I2C has to be controlled per physical port, limit the size of the port_mutex array. Introduce the I2C_DEVICE_COUNT define to represent the number of the physical I2C ports. It is not possible to automate it fully with the current devicetree macros. A new I2C_COPMAT defines will have to be introduced for new chips. The general assumption is that there is only one type of I2C port per chip pointed by the compatible property e.g. "nuvoton,npcx-i2c-port". BUG=b:199918263 BRANCH=none TEST=zmake testall; Make sure there is no "Access I2C without lock!" message in console. Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com> Change-Id: Ie7b0570eb5629ebd4adabc8a47d0d802d847a773 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3211976 Tested-by: Dawid Niedźwiecki <dn@semihalf.com> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Dawid Niedzwiecki <dawidn@google.com>
* zephyr: convert port number for I2C_CONTROL HCDawid Niedzwiecki2021-10-061-0/+9
| | | | | | | | | | | | | | | | The port number passed via the I2C control host command may be different than the one used in ZephyrEC so convert it based on the "remote-port" property in DTS. BUG=b:201408931 BRANCH=none TEST=zmake testall Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com> Change-Id: I71e05df2eb4f76b8c93948a97f47193aaaa6e25e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3205251 Reviewed-by: Yuval Peress <peress@google.com> Commit-Queue: Dawid Niedzwiecki <dawidn@google.com>
* i2c: Use bitbang mode for pre-task i2c transactionsJun Lin2021-10-051-4/+15
| | | | | | | | | | | | | | | | | | | | | | | | | In Intel ADL+_RVP, the keyboard is scanned by a discrete I/O expander IC and it is connected to EC via the I2C interface. EC needs to initialize the IC via I2C transaction before the task scheduling starts. It may cause the system panic if the EC's I2C driver is implemented by task-event-based. With this CL, if any of the I2C ports that need pre-task I2C transaction are defined as bitbang ports along with regular I2C ports will switch to the bitbang (GPIO) mode before the task starts and will again switch back to event based I2C upon task initialization. BRANCH=none BUG=b:199374643 TEST=issue an I2C transaction before the task scheduling starts; make sure the transaction success without panic on npcx9_evb. TEST=pass "make buildall" Signed-off-by: Jun Lin <CHLin56@nuvoton.com> Change-Id: I65460d2b612328f25bce60561bbb82995dd1cfdf Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3161869 Tested-by: Vijay Hiremath <vijay.p.hiremath@intel.com> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Keith Short <keithshort@chromium.org>
* hostcmd: Implement I2C_CONTROL host commandCaveh Jalali2021-10-011-0/+61
| | | | | | | | | | | | | | | | This adds an host command to get or set the I2C bus speed of an I2C bus located on the EC. BRANCH=none BUG=b:201039003 TEST=with follow-on patches, switched I2C bus speed between 400 kHz and 1 MHz using ectool. Change-Id: Ieaaee8d925509b103771c96bec50a90403766c1a Signed-off-by: Caveh Jalali <caveh@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3181506 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Boris Mittelberg <bmbm@google.com>
* i2c: add converters between enum i2c_freq and kHzCaveh Jalali2021-10-011-0/+28
| | | | | | | | | | | | | BRANCH=none BUG=b:201039003 TEST=with follow-on patches, switched I2C bus speed between 400 kHz and 1 MHz using ectool. Change-Id: Ide60a2515d4d9a5f8e1ff841b8369e63c39f5c6a Signed-off-by: Caveh Jalali <caveh@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3198247 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Boris Mittelberg <bmbm@google.com>
* common/i2c: Implement i2cspeed console commandCaveh Jalali2021-09-281-0/+74
| | | | | | | | | | | | | | | | | | This adds the "i2cspeed port [speed]" console command. If only the port number is given, then the current port bus speed is reported. With 2 arguments, the port bus speed is changed. Valid speeds are 100, 400, 1000 and the unit is kHz. BRANCH=none BUG=b:201039003 TEST=with follow-on patches, switched I2C bus speed between 400 kHz and 1 MHz. Change-Id: I7ca6b2c7a8fd9abe8e8ec77e4d1702529b297fe8 Signed-off-by: Caveh Jalali <caveh@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3181504 Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Keith Short <keithshort@chromium.org>
* common/i2c: Improve port number validation in i2c_set_freqCaveh Jalali2021-09-261-1/+6
| | | | | | | | | | | | | | | | | | This adds additional checks to the port number validation in i2c_set_freq. get_i2c_port could potentially return a NULL pointer, so check for that. Also, when DYNAMIC_SPEED is not enabled on a port, return ERROR_UNIMPLEMENTED instead of ERROR_INVAL. BRANCH=none BUG=b:201039003 TEST=with follow-on patches, switched I2C bus speed between 400 kHz and 1 MHz. Change-Id: Ie58d68ee2b64d94681ea1d5044530195210ff661 Signed-off-by: Caveh Jalali <caveh@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3181503 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Boris Mittelberg <bmbm@google.com>
* zephyr: i2c: Translate zephyr error to ecos errorsYuval Peress2021-09-211-1/+8
| | | | | | | | | | | | | BRANCH=none BUG=none TEST=zmake testall Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: I80f53103b3bb873da5627dec116386741b496a94 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3171693 Tested-by: Yuval Peress <peress@google.com> Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Commit-Queue: Yuval Peress <peress@google.com>
* zephyr: add support for I2C debuggingKeith Short2021-09-151-1/+8
| | | | | | | | | | | | | | | | | | | Add support for CONFIG_I2C_DEBUG and CONFIG_I2C_DEBUG_PASSTHRU options to zephyr. BUG=none BRANCH=none TEST=zmake testall TEST=Add CONFIG_PLATFORM_EC_I2C_DEBUG=y on herobrine, verify "i2ctrace" command is available and functional. TEST=Add CONFIG_PLATFORM_EC_I2C_DEBUG_PASSTHRU=y on herobrine, verify I2C passthru messages shown during TCPC firmware sync. Signed-off-by: Keith Short <keithshort@chromium.org> Change-Id: Ic2d9977af4fa707dab4fdaff332fbcc34491c5fe Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3163211 Commit-Queue: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Jack Rosenthal <jrosenth@chromium.org>
* i2c_controller: Check if get_i2c_port returns NULLLukasz Hajec2021-08-061-0/+3
| | | | | | | | | | | | | | | | | This patch fixes an issue, when nonexistent port number parameter is passed to i2cxfer and causes panic due to using null pointer. Add check if get_i2c_port returns NULL and return EC_ERROR_INVAL. BUG=b:186873536 BRANCH=none TEST=Run i2cxfer command with different parameters on servo_v4p1, nocturne and grunt - do not observe panic. Signed-off-by: Lukasz Hajec <lha@semihalf.com> Change-Id: I8bd90d898e1097b59ca6c9a1eff850bc2dc808e4 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3074620 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org> Reviewed-by: Ruben Rodriguez Buchillon <coconutruben@chromium.org>
* zephyr: i2c: protect physical portDino Li2021-07-221-0/+20
| | | | | | | | | | | | | | | | | | If i2c devices are connected to the same port, they should use the same mutex_lock() index. So the new transaction won't break the ongoing transaction. BRANCH=none BUG=b:189855648 TEST=Enable CONFIG_SMBUS_PEC and voltage regulator function on asurada. No i2c transaction is broken. Change-Id: Ib848e3c2e60b99ce66ad5fd2fc7095f90820a15d Signed-off-by: Dino Li <Dino.Li@ite.com.tw> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3010920 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Denis Brockus <dbrockus@chromium.org> Commit-Queue: Denis Brockus <dbrockus@chromium.org>
* common/i2c_controller: Fix the port protection logicKarthikeyan Ramasubramanian2021-07-091-1/+4
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | Currently depthcharge sends EC_CMD_I2C_PASSTHRU_PROTECT_ENABLE_TCPCS to protect all the I2C ports after performing auxiliary firmware updates and before jumping to OS. But the port protect status is overridden by the absence of passthru_allowed operation for an I2C port. Except for one board (elm), passthru_allowed operation is not defined for any I2C ports. This causes the I2C passthrough to remain supported even after the ports are protected. Fix this by denying the I2C command passthrough for I2C ports which are protected and have no passthru_allowed operation defined. BUG=b:192503665 BRANCH=All TEST=Build and boot to OS in Drawlat. Ensure that all I2C ports are protected before jumping to OS and any passthru access is denied after that. localhost ~ # ectool i2cread 8 4 0x0b 0x0 EC result 4 (ACCESS_DENIED) localhost ~ # ectool i2cprotect 4 status I2C port 4: Protected (1) localhost ~ # Change-Id: Ifdf0de9e8be1e245314ce357865ac76db4780bc8 Signed-off-by: Karthikeyan Ramasubramanian <kramasub@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/3004111 Tested-by: Karthikeyan Ramasubramanian <kramasub@chromium.org> Reviewed-by: Aseda Aboagye <aaboagye@chromium.org> Reviewed-by: Furquan Shaikh <furquan@chromium.org> Commit-Queue: Karthikeyan Ramasubramanian <kramasub@chromium.org>
* i2c_controller: add volatile for i2c_port_active_listEric Yilun Lin2021-06-251-2/+2
| | | | | | | | | | | | | | | | | With the LTO enabled, the i2c_port_active_list might be reordered by linker and causes the i2c lock checking failure. BUG=b:191444593 TEST=i2c waveform looks good, no "Access i2c without lock" complain BRANCH=none Change-Id: I96996c7288b65e74f1734fd14a82f069b6ff11ff Signed-off-by: Eric Yilun Lin <yllin@chromium.org> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2984257 Tested-by: Eric Yilun Lin <yllin@google.com> Tested-by: Hsu Alvis <alvishsu@google.com> Reviewed-by: Patrick Georgi <pgeorgi@chromium.org> Commit-Queue: Eric Yilun Lin <yllin@google.com>
* Fix compilation issues with nocturne SDKPatryk Duda2021-06-171-2/+4
| | | | | | | | | | | | | | | | | | | | | Compiler in nocturne cros SDK doesn't allow for variable declaration inside for loop. This patch removes variable declaration inside for loop in code which is used by nocturne board. This patch doesn't introduce any logical changes. BUG=b:160676144 BRANCH=none TEST=Make sure EC points to commit on cros/main. On nocturne SDK: cros_workon-nocturne start chromeos-ec emerge-nocturne chromeos-ec chromeos-bootimage Make sure that firmware compiles Signed-off-by: Patryk Duda <pdk@semihalf.com> Change-Id: I75ff21d966d5e353d1f7873695127bac4357fb32 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2965922 Reviewed-by: Patrick Georgi <pgeorgi@chromium.org>
* zephyr: Fix I2C_PASSTHRU host commandDawid Niedzwiecki2021-06-081-3/+35
| | | | | | | | | | | | | | | | | | | | ZephyrEC uses different I2C port numbers than CrosEC does, so convert the received remote port via I2C_PASSTHRU command into a proper one. The conversion is done based on a new property remote-port, which tells what port number is used by external components like kernel. The change fixes an issue with unexpected entering OTG mode by the charger. BUG=b:188885798 BRANCH=none TEST=Flash Lazor and make sure that no "charge problem" prints are displayed. Signed-off-by: Dawid Niedzwiecki <dn@semihalf.com> Change-Id: Id00265a3abf286ca59cbecb38ff7933d75e0d361 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2933296 Reviewed-by: Yuval Peress <peress@chromium.org>
* zephyr: i2c: remove scl and sda referencesYuval Peress2021-05-141-0/+2
| | | | | | | | | | | | | | | The SDA and SCL components of the struct i2c_port_t are only used in and for the i2c_wedge.c logic. This isn't needed in Zephyr. Remove the values and functions that use them from the Zephyr build. BRANCH=none BUG=b:188179405 TEST=build volteer and trogdor Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: I71a26fbd9bc51def744ef1f16a540728482c5a2b Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2896235 Reviewed-by: Fabio Baltieri <fabiobaltieri@google.com>
* zephyr: i2c: Fix use of I2C_PORT_VIRTUAL_BATTERYYuval Peress2021-04-151-1/+2
| | | | | | | | | | | | | | | | | | In Zephyr builds, the I2C_PORT_* values are enums generated from devicetree (instead of #define values). This means that in Zephyr, it should suffice to just check VIRTUAL_BATTERY_ADDR_FLAGS. BRANCH=none BUG=b:185392974 TEST=zmake testall TEST=make buildall Signed-off-by: Yuval Peress <peress@chromium.org> Change-Id: Ifedaf45ec8bcfa33bb75c8381604ad565ca08d6a Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2826919 Reviewed-by: Jack Rosenthal <jrosenth@chromium.org> Reviewed-by: Keith Short <keithshort@chromium.org> Commit-Queue: Jack Rosenthal <jrosenth@chromium.org>
* i2c_controller: zephyr: explicitly strip flags from addr_flagsFabio Baltieri2021-04-081-1/+6
| | | | | | | | | | | | | | | | | | | | | | The ECOS I2C APIs use addr_flag to pass the I2C address together with few flags. These are currently passed to the Zephyr API stripping only the PEC bit, and no other flags are used in Zephyr platforms at the moment, but if any flag would be present it would confuse the Zephyr I2C stack. This changes the Zephyr part of the driver to explicitly strip any possible flag from add_flag, and also add a warning to signal that we are discarding some information. BUG=none BRANCH=none TEST=build and flash on volteer TEST=cbi shell command Change-Id: I497fe33c52c78d783c86711928067a4aa0a321d7 Signed-off-by: Fabio Baltieri <fabiobaltieri@google.com> Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2814624 Reviewed-by: Yuval Peress <peress@chromium.org>
* zephyr: Support multi-step I2C transactionsSimon Glass2020-12-241-2/+39
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | At present the Zephyr implementation of i2c_xfer_unlocked() assumes this is the only call for the whole transaction. However in some cases the function is called several times, e.g. to write a register number, read a length, then read that number of bytes). Add support for this, so i2c_read_string() works, for example. BUG=b:176189170 TEST=build and boot on volteer, with Nuvoton's I2C fix See that LION is shown as such, and not LIO > battery Status: 0x0080 INIT Param flags:00000003 Temp: 0x0bb7 = %.1d K (%.1d C) V: 0x31f6 = 12790 mV V-desired: 0x3390 = 13200 mV I: 0x00ac = 172 mA(CHG) I-desired: 0x0a19 = 2585 mA Charging: Allowed Charge: 92 % Manuf: LGC Device: AC17A8M Chem: LION Serial: 0xb754 V-design: 0x2d1e = 11550 mV Mode: 0x6001 Abs charge:87 % Remaining: 4648 mAh Cap-full: 5040 mAh (4939 mAh with 98 % compensation) Display: 96.9 % Design: 5360 mAh Time-full: 2h:47 Empty: 0h:0 Signed-off-by: Simon Glass <sjg@chromium.org> Change-Id: I1fe08c0c919a98fa66510048d4c05ae6e10072d9 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2600933 Reviewed-by: Mulin Chao <mlchao@nuvoton.com> Reviewed-by: Paul Fagerburg <pfagerburg@chromium.org>
* COIL: Rename SLEEP_MASK_I2C_CONTROLLERDiana Z2020-12-041-3/+3
| | | | | | | | | | | | | Rename SLEEP_MASK_I2C_CONTROLLER and related comments. BRANCH=None BUG=None TEST=make -j buildall Signed-off-by: Diana Z <dzigterman@chromium.org> Change-Id: Ib49a5c6b07a203dc659ed65909292e5f1009d33f Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2558907 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
* COIL: Rename local variables in common i2c_controllerDiana Z2020-12-041-105/+105
| | | | | | | | | | | | | | Rename local variables and related comments in common i2c_controller code. BRANCH=None BUG=None TEST=make -j buildall Signed-off-by: Diana Z <dzigterman@chromium.org> Change-Id: I712217d86f7232f509f1f649b772ec73204ceb53 Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2558906 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>
* COIL: Rename common i2c_controllerDiana Z2020-12-011-0/+1767
Rename i2c_controller.c and update related build file. BRANCH=None BUG=None TEST=make -j buildall Signed-off-by: Diana Z <dzigterman@chromium.org> Change-Id: I4fc27e494648c4a17e9f3f3d21ece9727ff6636e Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/ec/+/2558905 Reviewed-by: Daisuke Nojiri <dnojiri@chromium.org>