1 // SPDX-License-Identifier: BSD-2-Clause
2 /*
3 * Copyright (C) Foundries Ltd. 2020 - All Rights Reserved
4 * Author: Jorge Ramirez <jorge@foundries.io>
5 */
6
7 #include <compiler.h>
8 #include <config.h>
9 #include <glue.h>
10 #include <i2c_native.h>
11 #include <initcall.h>
12 #include <kernel/rpc_io_i2c.h>
13 #include <phNxpEsePal_i2c.h>
14
15 static TEE_Result (*transfer)(struct rpc_i2c_request *req, size_t *bytes);
16
i2c_transfer(uint8_t * buffer,int len,enum rpc_i2c_mode mode)17 static int i2c_transfer(uint8_t *buffer, int len, enum rpc_i2c_mode mode)
18 {
19 struct rpc_i2c_request request = {
20 .bus = CFG_CORE_SE05X_I2C_BUS,
21 .chip = SMCOM_I2C_ADDRESS >> 1,
22 .mode = mode,
23 .buffer = buffer,
24 .buffer_len = len,
25 .flags = 0,
26 };
27 size_t bytes = 0;
28 int retry = 5;
29
30 do {
31 if ((*transfer)(&request, &bytes) == TEE_SUCCESS)
32 return bytes;
33 } while (--retry);
34
35 return -1;
36 }
37
glue_i2c_read(uint8_t * buffer,int len)38 int glue_i2c_read(uint8_t *buffer, int len)
39 {
40 return i2c_transfer(buffer, len, RPC_I2C_MODE_READ);
41 }
42
glue_i2c_write(uint8_t * buffer,int len)43 int glue_i2c_write(uint8_t *buffer, int len)
44 {
45 return i2c_transfer(buffer, len, RPC_I2C_MODE_WRITE);
46 }
47
glue_i2c_init(void)48 int glue_i2c_init(void)
49 {
50 if (transfer == rpc_io_i2c_transfer)
51 return 0;
52
53 transfer = native_i2c_transfer;
54
55 return native_i2c_init();
56 }
57
load_trampoline(void)58 static TEE_Result load_trampoline(void)
59 {
60 if (IS_ENABLED(CFG_CORE_SE05X_I2C_TRAMPOLINE))
61 transfer = rpc_io_i2c_transfer;
62
63 return TEE_SUCCESS;
64 }
65
66 boot_final(load_trampoline);
67