2018-05-06 21:58:06 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0+
|
2016-05-08 06:30:17 +00:00
|
|
|
/*
|
|
|
|
* (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
|
|
|
|
*
|
|
|
|
* Secure monitor calls.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <common.h>
|
2023-09-21 08:13:40 +00:00
|
|
|
#include <dm.h>
|
2020-05-10 17:40:05 +00:00
|
|
|
#include <log.h>
|
2023-09-21 08:13:40 +00:00
|
|
|
#include <regmap.h>
|
2023-09-21 08:13:41 +00:00
|
|
|
#include <sm.h>
|
2023-09-21 08:13:40 +00:00
|
|
|
#include <syscon.h>
|
2019-06-12 09:49:06 +00:00
|
|
|
#include <asm/arch/sm.h>
|
2020-05-10 17:39:56 +00:00
|
|
|
#include <asm/cache.h>
|
2020-10-31 03:38:53 +00:00
|
|
|
#include <asm/global_data.h>
|
2020-05-10 17:40:06 +00:00
|
|
|
#include <asm/ptrace.h>
|
2020-05-10 17:40:13 +00:00
|
|
|
#include <linux/bitops.h>
|
2020-02-03 14:36:15 +00:00
|
|
|
#include <linux/err.h>
|
2016-05-08 06:30:17 +00:00
|
|
|
#include <linux/kernel.h>
|
2019-08-06 15:28:36 +00:00
|
|
|
#include <linux/bitfield.h>
|
2023-09-21 08:13:41 +00:00
|
|
|
#include <meson/sm.h>
|
2016-05-08 06:30:17 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
static inline struct udevice *meson_get_sm_device(void)
|
2016-05-08 06:30:17 +00:00
|
|
|
{
|
2023-09-21 08:13:41 +00:00
|
|
|
struct udevice *dev;
|
|
|
|
int err;
|
2016-05-08 06:30:17 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
err = uclass_first_device_err(UCLASS_SM, &dev);
|
|
|
|
if (err) {
|
|
|
|
pr_err("Mesom SM device not found\n");
|
|
|
|
return ERR_PTR(err);
|
|
|
|
}
|
2016-05-08 06:30:17 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
return dev;
|
2016-05-08 06:30:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
|
|
|
|
{
|
2023-09-21 08:13:41 +00:00
|
|
|
struct udevice *dev;
|
|
|
|
struct pt_regs regs = { 0 };
|
|
|
|
int err;
|
2016-05-08 06:30:17 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
dev = meson_get_sm_device();
|
|
|
|
if (IS_ERR(dev))
|
|
|
|
return PTR_ERR(dev);
|
2016-05-08 06:30:17 +00:00
|
|
|
|
|
|
|
regs.regs[1] = offset;
|
|
|
|
regs.regs[2] = size;
|
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
err = sm_call_read(dev, buffer, size,
|
|
|
|
MESON_SMC_CMD_EFUSE_READ, ®s);
|
|
|
|
if (err < 0)
|
|
|
|
pr_err("Failed to read efuse memory (%d)\n", err);
|
2016-05-08 06:30:17 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
return err;
|
2016-05-08 06:30:17 +00:00
|
|
|
}
|
2019-06-12 09:49:06 +00:00
|
|
|
|
2021-10-05 12:00:03 +00:00
|
|
|
ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size)
|
|
|
|
{
|
2023-09-21 08:13:41 +00:00
|
|
|
struct udevice *dev;
|
|
|
|
struct pt_regs regs = { 0 };
|
|
|
|
int err;
|
2021-10-05 12:00:03 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
dev = meson_get_sm_device();
|
|
|
|
if (IS_ERR(dev))
|
|
|
|
return PTR_ERR(dev);
|
2021-10-05 12:00:03 +00:00
|
|
|
|
|
|
|
regs.regs[1] = offset;
|
|
|
|
regs.regs[2] = size;
|
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
err = sm_call_write(dev, buffer, size,
|
|
|
|
MESON_SMC_CMD_EFUSE_WRITE, ®s);
|
|
|
|
if (err < 0)
|
|
|
|
pr_err("Failed to write efuse memory (%d)\n", err);
|
2021-10-05 12:00:03 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
return err;
|
2021-10-05 12:00:03 +00:00
|
|
|
}
|
|
|
|
|
2019-06-12 09:49:06 +00:00
|
|
|
#define SM_CHIP_ID_LENGTH 119
|
|
|
|
#define SM_CHIP_ID_OFFSET 4
|
|
|
|
#define SM_CHIP_ID_SIZE 12
|
|
|
|
|
|
|
|
int meson_sm_get_serial(void *buffer, size_t size)
|
|
|
|
{
|
2023-09-21 08:13:41 +00:00
|
|
|
struct udevice *dev;
|
|
|
|
struct pt_regs regs = { 0 };
|
|
|
|
u8 id_buffer[SM_CHIP_ID_LENGTH];
|
|
|
|
int err;
|
2019-06-12 09:49:06 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
dev = meson_get_sm_device();
|
|
|
|
if (IS_ERR(dev))
|
|
|
|
return PTR_ERR(dev);
|
2019-06-12 09:49:06 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
err = sm_call_read(dev, id_buffer, SM_CHIP_ID_LENGTH,
|
|
|
|
MESON_SMC_CMD_CHIP_ID_GET, ®s);
|
|
|
|
if (err < 0)
|
|
|
|
pr_err("Failed to read serial number (%d)\n", err);
|
2019-06-12 09:49:06 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
memcpy(buffer, id_buffer + SM_CHIP_ID_OFFSET, size);
|
2019-06-12 09:49:06 +00:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2019-08-06 15:21:02 +00:00
|
|
|
|
2019-08-06 15:28:36 +00:00
|
|
|
#define AO_SEC_SD_CFG15 0xfc
|
|
|
|
#define REBOOT_REASON_MASK GENMASK(15, 12)
|
|
|
|
|
|
|
|
int meson_sm_get_reboot_reason(void)
|
|
|
|
{
|
|
|
|
struct regmap *regmap;
|
|
|
|
int nodeoffset;
|
|
|
|
ofnode node;
|
|
|
|
unsigned int reason;
|
|
|
|
|
|
|
|
/* find the offset of compatible node */
|
|
|
|
nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
|
|
|
|
"amlogic,meson-gx-ao-secure");
|
|
|
|
if (nodeoffset < 0) {
|
|
|
|
printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
|
|
|
|
__func__);
|
|
|
|
return -ENODEV;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* get regmap from the syscon node */
|
|
|
|
node = offset_to_ofnode(nodeoffset);
|
|
|
|
regmap = syscon_node_to_regmap(node);
|
|
|
|
if (IS_ERR(regmap)) {
|
|
|
|
printf("%s: failed to get regmap\n", __func__);
|
|
|
|
return -EINVAL;
|
|
|
|
}
|
|
|
|
|
|
|
|
regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
|
|
|
|
|
|
|
|
/* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
|
|
|
|
return FIELD_GET(REBOOT_REASON_MASK, reason);
|
|
|
|
}
|
2023-05-31 09:31:54 +00:00
|
|
|
|
|
|
|
int meson_sm_pwrdm_set(size_t index, int cmd)
|
|
|
|
{
|
2023-09-21 08:13:41 +00:00
|
|
|
struct udevice *dev;
|
|
|
|
struct pt_regs regs = { 0 };
|
|
|
|
int err;
|
|
|
|
|
|
|
|
dev = meson_get_sm_device();
|
|
|
|
if (IS_ERR(dev))
|
|
|
|
return PTR_ERR(dev);
|
2023-05-31 09:31:54 +00:00
|
|
|
|
|
|
|
regs.regs[1] = index;
|
|
|
|
regs.regs[2] = cmd;
|
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
err = sm_call(dev, MESON_SMC_CMD_PWRDM_SET, NULL, ®s);
|
|
|
|
if (err)
|
|
|
|
pr_err("Failed to %s power domain ind=%zu (%d)\n", cmd == PWRDM_ON ?
|
|
|
|
"enable" : "disable", index, err);
|
2023-05-31 09:31:54 +00:00
|
|
|
|
2023-09-21 08:13:41 +00:00
|
|
|
return err;
|
2023-05-31 09:31:54 +00:00
|
|
|
}
|