From patchwork Mon Dec 28 12:17:46 2020 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: "Jorge Ramirez-Ortiz, Foundries" X-Patchwork-Id: 1420837 X-Patchwork-Delegate: hs@denx.de Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=lists.denx.de (client-ip=2a01:238:438b:c500:173d:9f52:ddab:ee01; helo=phobos.denx.de; envelope-from=u-boot-bounces@lists.denx.de; receiver=) Authentication-Results: ozlabs.org; dmarc=none (p=none dis=none) header.from=foundries.io Authentication-Results: ozlabs.org; dkim=pass (2048-bit key; unprotected) header.d=foundries.io header.i=@foundries.io header.a=rsa-sha256 header.s=google header.b=gfbrKcmg; dkim-atps=neutral Received: from phobos.denx.de (phobos.denx.de [IPv6:2a01:238:438b:c500:173d:9f52:ddab:ee01]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits)) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4D4GmD3fwSz9sWM for ; Mon, 28 Dec 2020 23:18:11 +1100 (AEDT) Received: from h2850616.stratoserver.net (localhost [IPv6:::1]) by phobos.denx.de (Postfix) with ESMTP id 1052781260; Mon, 28 Dec 2020 13:17:57 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=foundries.io Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=u-boot-bounces@lists.denx.de Authentication-Results: phobos.denx.de; dkim=pass (2048-bit key; unprotected) header.d=foundries.io header.i=@foundries.io header.b="gfbrKcmg"; dkim-atps=neutral Received: by phobos.denx.de (Postfix, from userid 109) id 726468232D; Mon, 28 Dec 2020 13:17:54 +0100 (CET) X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on phobos.denx.de X-Spam-Level: X-Spam-Status: No, score=-2.0 required=5.0 tests=BAYES_00,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE autolearn=ham autolearn_force=no version=3.4.2 Received: from mail-wr1-x42c.google.com (mail-wr1-x42c.google.com [IPv6:2a00:1450:4864:20::42c]) (using TLSv1.3 with cipher TLS_AES_128_GCM_SHA256 (128/128 bits)) (No client certificate requested) by phobos.denx.de (Postfix) with ESMTPS id 8268D8002F for ; Mon, 28 Dec 2020 13:17:51 +0100 (CET) Authentication-Results: phobos.denx.de; dmarc=none (p=none dis=none) header.from=foundries.io Authentication-Results: phobos.denx.de; spf=pass smtp.mailfrom=jorge@foundries.io Received: by mail-wr1-x42c.google.com with SMTP id 91so11156028wrj.7 for ; Mon, 28 Dec 2020 04:17:51 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=foundries.io; s=google; h=from:to:cc:subject:date:message-id; bh=u9lFIZlf1ue0aNhBlEQYb3Di7eJuC+3zNG54q0n6n1M=; b=gfbrKcmgCP6tYphVDskXsuFzFVgAW66ixYVbQBLZ3T6+gZdk4QyE024TvZCOoOvgIN QlP93z1Kae5IqLb2Za4MiXzT1hT761PCbZj2wjwYfaf52AO3znD4X03tHShm4Bmqx3a4 Svoec6AncbgsNM84XkLp9I3wPnwtHvDfU3QhOTjNUYIcuOX9/R+0MYXHwFo8ybbb0s3r chR6R4gFAtV4vnGQAaHEERki/BGKZvmOZvO+9RJzqyHBNfZauBzKoqTffFhms2uhiW4+ WYX8H6fNkJoGuzpXLUB8z638MSuM5xY8BInSXcjBHVrGtbuJkVk4R4VaqfNDZPrfjMgS OekQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:date:message-id; bh=u9lFIZlf1ue0aNhBlEQYb3Di7eJuC+3zNG54q0n6n1M=; b=BVKzr2C1xSp93XVkia+BaPl9n/tfZLJp7GKiZQ8j0oXFOoWji6rEGnPRO6AitzIFBw +9IeJ0INctSp7gBoV7pLnL7RunPmv1LaZnwt4JCdgNaS56PgFbxiKKV/he/BKs6xntSI GadIBVofZvMQBzxgTU6s85hW3jr/htDk0XcS0awWY+ZbmHwZ/S8nZ2uKoH/LzLdEh6RD eREwqNBhqSJwAjR0uCxgPv8o/ontgRjL4oysICj1M4BwpqYSzd4QAu9Tm0G+aYHRApB6 hufTXlY5G+GmHiX35JHlb1FhZsWJZDYz337ZItD76kmWcvnP59PbNdM+rZBZU8dxiH6N 8DYQ== X-Gm-Message-State: AOAM530OOe5HjXpJJ9muYSLHXSl+KYE6tYQqEFITXRwWC5+X7Rw4rJXX tAx8uINRJWFa7x74+F2hXrKlkQ== X-Google-Smtp-Source: ABdhPJydDQyK2Z30N9n/Y7svmcgpB8NbwaUNzmK8yJ4wk5NAiZ66jA5tRai1w/egJoQho/GxJTQLEw== X-Received: by 2002:a5d:678d:: with SMTP id v13mr50730483wru.71.1609157870921; Mon, 28 Dec 2020 04:17:50 -0800 (PST) Received: from localhost.localdomain (1.red-83-36-179.dynamicip.rima-tde.net. [83.36.179.1]) by smtp.gmail.com with ESMTPSA id h3sm20090045wmm.4.2020.12.28.04.17.49 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 28 Dec 2020 04:17:50 -0800 (PST) From: Jorge Ramirez-Ortiz To: jorge@foundries.io, jens.wiklander@linaro.org, trini@konsulko.com Cc: mike@foundries.io, ricardo@foundries.io, sjg@chromium.org, u-boot@lists.denx.de Subject: [PATCHv2] drivers: tee: i2c trampoline driver Date: Mon, 28 Dec 2020 13:17:46 +0100 Message-Id: <20201228121746.16367-1-jorge@foundries.io> X-Mailer: git-send-email 2.17.1 X-BeenThere: u-boot@lists.denx.de X-Mailman-Version: 2.1.34 Precedence: list List-Id: U-Boot discussion List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: u-boot-bounces@lists.denx.de Sender: "U-Boot" X-Virus-Scanned: clamav-milter 0.102.3 at phobos.denx.de X-Virus-Status: Clean This commit gives the secure world access to the I2C bus so it can communicate with I2C slaves (tipically those would be secure elements like the NXP SE050). Signed-off-by: Jorge Ramirez-Ortiz --- drivers/tee/optee/Makefile | 1 + drivers/tee/optee/i2c.c | 88 ++++++++++++++++++++++++ drivers/tee/optee/optee_msg.h | 21 ++++++ drivers/tee/optee/optee_msg_supplicant.h | 5 ++ drivers/tee/optee/optee_private.h | 12 ++++ drivers/tee/optee/supplicant.c | 3 + 6 files changed, 130 insertions(+) create mode 100644 drivers/tee/optee/i2c.c diff --git a/drivers/tee/optee/Makefile b/drivers/tee/optee/Makefile index 928d3f8002..068c6e7aa1 100644 --- a/drivers/tee/optee/Makefile +++ b/drivers/tee/optee/Makefile @@ -2,4 +2,5 @@ obj-y += core.o obj-y += supplicant.o +obj-$(CONFIG_DM_I2C) += i2c.o obj-$(CONFIG_SUPPORT_EMMC_RPMB) += rpmb.o diff --git a/drivers/tee/optee/i2c.c b/drivers/tee/optee/i2c.c new file mode 100644 index 0000000000..506e40850c --- /dev/null +++ b/drivers/tee/optee/i2c.c @@ -0,0 +1,88 @@ +// SPDX-License-Identifier: BSD-2-Clause +/* + * Copyright (c) 2020 Foundries.io Ltd + */ + +#include +#include +#include +#include +#include "optee_msg.h" +#include "optee_private.h" + +static struct { + struct udevice *dev; + int chip; + int bus; +} xfer; + +void optee_suppl_cmd_i2c_transfer(struct udevice *dev, + struct optee_msg_arg *arg) +{ + const u8 attr[] = { + OPTEE_MSG_ATTR_TYPE_VALUE_INPUT, + OPTEE_MSG_ATTR_TYPE_VALUE_INPUT, + OPTEE_MSG_ATTR_TYPE_RMEM_INOUT, + OPTEE_MSG_ATTR_TYPE_VALUE_OUTPUT, + }; + struct udevice *chip_dev = NULL; + struct tee_shm *shm = NULL; + uint8_t *buf = NULL; + size_t len = 0; + int chip = -1; + int bus = -1; + int ret = -1; + + if (arg->num_params != ARRAY_SIZE(attr) || + arg->params[0].attr != attr[0] || + arg->params[1].attr != attr[1] || + arg->params[2].attr != attr[2] || + arg->params[3].attr != attr[3]) { + arg->ret = TEE_ERROR_BAD_PARAMETERS; + return; + } + + len = arg->params[2].u.rmem.size; + shm = (struct tee_shm *)(unsigned long)arg->params[2].u.rmem.shm_ref; + buf = shm->addr; + if (!buf || !len) + goto bad; + + bus = (int)arg->params[0].u.value.b; + chip = (int)arg->params[0].u.value.c; + + if (!xfer.dev || xfer.chip != chip || xfer.bus != bus) { + if (i2c_get_chip_for_busnum(bus, chip, 0, &chip_dev)) + goto bad; + + xfer.dev = chip_dev; + xfer.chip = chip; + xfer.bus = bus; + } + + if (arg->params[1].u.value.a & OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT) + if (i2c_set_chip_flags(xfer.dev, DM_I2C_CHIP_10BIT)) + goto bad; + + switch (arg->params[0].u.value.a) { + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD: + ret = dm_i2c_read(xfer.dev, 0, buf, len); + break; + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR: + ret = dm_i2c_write(xfer.dev, 0, buf, len); + break; + default: + goto bad; + } + + if (ret) { + arg->ret = TEE_ERROR_COMMUNICATION; + } else { + arg->params[3].u.value.a = len; + arg->ret = TEE_SUCCESS; + } + + return; +bad: + arg->ret = TEE_ERROR_BAD_PARAMETERS; +} diff --git a/drivers/tee/optee/optee_msg.h b/drivers/tee/optee/optee_msg.h index 24c60960fc..8d40ce60c2 100644 --- a/drivers/tee/optee/optee_msg.h +++ b/drivers/tee/optee/optee_msg.h @@ -422,4 +422,25 @@ struct optee_msg_arg { */ #define OPTEE_MSG_RPC_CMD_SHM_FREE 7 +/* + * Access a device on an i2c bus + * + * [in] param[0].u.value.a mode: RD(0), WR(1) + * [in] param[0].u.value.b i2c adapter + * [in] param[0].u.value.c i2c chip + * + * [in] param[1].u.value.a i2c control flags + * + * [in/out] memref[2] buffer to exchange the transfer data + * with the secure world + * + * [out] param[3].u.value.a bytes transferred by the driver + */ +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER 21 +/* I2C master transfer modes */ +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_RD 0 +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER_WR 1 +/* I2C master control flags */ +#define OPTEE_MSG_RPC_CMD_I2C_FLAGS_TEN_BIT BIT(0) + #endif /* _OPTEE_MSG_H */ diff --git a/drivers/tee/optee/optee_msg_supplicant.h b/drivers/tee/optee/optee_msg_supplicant.h index a0fb8063c8..963cfd4782 100644 --- a/drivers/tee/optee/optee_msg_supplicant.h +++ b/drivers/tee/optee/optee_msg_supplicant.h @@ -147,6 +147,11 @@ #define OPTEE_MSG_RPC_CMD_SHM_ALLOC 6 #define OPTEE_MSG_RPC_CMD_SHM_FREE 7 +/* + * I2C bus access + */ +#define OPTEE_MSG_RPC_CMD_I2C_TRANSFER 21 + /* * Was OPTEE_MSG_RPC_CMD_SQL_FS, which isn't supported any longer */ diff --git a/drivers/tee/optee/optee_private.h b/drivers/tee/optee/optee_private.h index 9442d1c176..fc19e97871 100644 --- a/drivers/tee/optee/optee_private.h +++ b/drivers/tee/optee/optee_private.h @@ -60,6 +60,18 @@ static inline void optee_suppl_rpmb_release(struct udevice *dev) } #endif +#ifdef CONFIG_DM_I2C +void optee_suppl_cmd_i2c_transfer(struct udevice *dev, + struct optee_msg_arg *arg); +#else +static void optee_suppl_cmd_i2c_transfer(struct udevice *dev, + struct optee_msg_arg *arg) +{ + debug("OPTEE_MSG_RPC_CMD_I2C_TRANSFER not implemented\n"); + arg->ret = TEE_ERROR_NOT_IMPLEMENTED; +} +#endif + void *optee_alloc_and_init_page_list(void *buf, ulong len, u64 *phys_buf_ptr); #endif /* __OPTEE_PRIVATE_H */ diff --git a/drivers/tee/optee/supplicant.c b/drivers/tee/optee/supplicant.c index ae042b9a20..f7738983cd 100644 --- a/drivers/tee/optee/supplicant.c +++ b/drivers/tee/optee/supplicant.c @@ -89,6 +89,9 @@ void optee_suppl_cmd(struct udevice *dev, struct tee_shm *shm_arg, case OPTEE_MSG_RPC_CMD_RPMB: optee_suppl_cmd_rpmb(dev, arg); break; + case OPTEE_MSG_RPC_CMD_I2C_TRANSFER: + optee_suppl_cmd_i2c_transfer(dev, arg); + break; default: arg->ret = TEE_ERROR_NOT_IMPLEMENTED; }