blob: 4d9f83d3b38d54e20d7d2bec997793cca5d7f0e4 [file] [log] [blame]
Tom Rini83d290c2018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvanic7757d42016-05-08 08:30:17 +02002/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4 *
Beniamino Galvanic7757d42016-05-08 08:30:17 +02005 * Secure monitor calls.
6 */
7
Alexey Romanov67c03a92023-09-21 11:13:40 +03008#include <dm.h>
Simon Glassf7ae49f2020-05-10 11:40:05 -06009#include <log.h>
Alexey Romanov67c03a92023-09-21 11:13:40 +030010#include <regmap.h>
Alexey Romanov7cd53e02023-09-21 11:13:41 +030011#include <sm.h>
Alexey Romanov67c03a92023-09-21 11:13:40 +030012#include <syscon.h>
Neil Armstrong0ef8e402019-06-12 11:49:06 +020013#include <asm/arch/sm.h>
Simon Glass90526e92020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass401d1c42020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glass25a58182020-05-10 11:40:06 -060016#include <asm/ptrace.h>
Simon Glasscd93d622020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glass61b29b82020-02-03 07:36:15 -070018#include <linux/err.h>
Beniamino Galvanic7757d42016-05-08 08:30:17 +020019#include <linux/kernel.h>
Neil Armstrongb1dd7de2019-08-06 17:28:36 +020020#include <linux/bitfield.h>
Alexey Romanov7cd53e02023-09-21 11:13:41 +030021#include <meson/sm.h>
Beniamino Galvanic7757d42016-05-08 08:30:17 +020022
Alexey Romanov7cd53e02023-09-21 11:13:41 +030023static inline struct udevice *meson_get_sm_device(void)
Beniamino Galvanic7757d42016-05-08 08:30:17 +020024{
Alexey Romanov7cd53e02023-09-21 11:13:41 +030025 struct udevice *dev;
26 int err;
Beniamino Galvanic7757d42016-05-08 08:30:17 +020027
Alexey Romanov7cd53e02023-09-21 11:13:41 +030028 err = uclass_first_device_err(UCLASS_SM, &dev);
29 if (err) {
30 pr_err("Mesom SM device not found\n");
31 return ERR_PTR(err);
32 }
Beniamino Galvanic7757d42016-05-08 08:30:17 +020033
Alexey Romanov7cd53e02023-09-21 11:13:41 +030034 return dev;
Beniamino Galvanic7757d42016-05-08 08:30:17 +020035}
36
37ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
38{
Alexey Romanov7cd53e02023-09-21 11:13:41 +030039 struct udevice *dev;
40 struct pt_regs regs = { 0 };
41 int err;
Beniamino Galvanic7757d42016-05-08 08:30:17 +020042
Alexey Romanov7cd53e02023-09-21 11:13:41 +030043 dev = meson_get_sm_device();
44 if (IS_ERR(dev))
45 return PTR_ERR(dev);
Beniamino Galvanic7757d42016-05-08 08:30:17 +020046
Beniamino Galvanic7757d42016-05-08 08:30:17 +020047 regs.regs[1] = offset;
48 regs.regs[2] = size;
49
Alexey Romanov7cd53e02023-09-21 11:13:41 +030050 err = sm_call_read(dev, buffer, size,
51 MESON_SMC_CMD_EFUSE_READ, &regs);
52 if (err < 0)
53 pr_err("Failed to read efuse memory (%d)\n", err);
Beniamino Galvanic7757d42016-05-08 08:30:17 +020054
Alexey Romanov7cd53e02023-09-21 11:13:41 +030055 return err;
Beniamino Galvanic7757d42016-05-08 08:30:17 +020056}
Neil Armstrong0ef8e402019-06-12 11:49:06 +020057
Vyacheslav Bocharov52195ba2021-10-05 15:00:03 +030058ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size)
59{
Alexey Romanov7cd53e02023-09-21 11:13:41 +030060 struct udevice *dev;
61 struct pt_regs regs = { 0 };
62 int err;
Vyacheslav Bocharov52195ba2021-10-05 15:00:03 +030063
Alexey Romanov7cd53e02023-09-21 11:13:41 +030064 dev = meson_get_sm_device();
65 if (IS_ERR(dev))
66 return PTR_ERR(dev);
Vyacheslav Bocharov52195ba2021-10-05 15:00:03 +030067
Vyacheslav Bocharov52195ba2021-10-05 15:00:03 +030068 regs.regs[1] = offset;
69 regs.regs[2] = size;
70
Alexey Romanov7cd53e02023-09-21 11:13:41 +030071 err = sm_call_write(dev, buffer, size,
72 MESON_SMC_CMD_EFUSE_WRITE, &regs);
73 if (err < 0)
74 pr_err("Failed to write efuse memory (%d)\n", err);
Vyacheslav Bocharov52195ba2021-10-05 15:00:03 +030075
Alexey Romanov7cd53e02023-09-21 11:13:41 +030076 return err;
Vyacheslav Bocharov52195ba2021-10-05 15:00:03 +030077}
78
Neil Armstrong0ef8e402019-06-12 11:49:06 +020079#define SM_CHIP_ID_LENGTH 119
80#define SM_CHIP_ID_OFFSET 4
81#define SM_CHIP_ID_SIZE 12
82
83int meson_sm_get_serial(void *buffer, size_t size)
84{
Alexey Romanov7cd53e02023-09-21 11:13:41 +030085 struct udevice *dev;
86 struct pt_regs regs = { 0 };
87 u8 id_buffer[SM_CHIP_ID_LENGTH];
88 int err;
Neil Armstrong0ef8e402019-06-12 11:49:06 +020089
Alexey Romanov7cd53e02023-09-21 11:13:41 +030090 dev = meson_get_sm_device();
91 if (IS_ERR(dev))
92 return PTR_ERR(dev);
Neil Armstrong0ef8e402019-06-12 11:49:06 +020093
Alexey Romanov7cd53e02023-09-21 11:13:41 +030094 err = sm_call_read(dev, id_buffer, SM_CHIP_ID_LENGTH,
95 MESON_SMC_CMD_CHIP_ID_GET, &regs);
96 if (err < 0)
97 pr_err("Failed to read serial number (%d)\n", err);
Neil Armstrong0ef8e402019-06-12 11:49:06 +020098
Alexey Romanov7cd53e02023-09-21 11:13:41 +030099 memcpy(buffer, id_buffer + SM_CHIP_ID_OFFSET, size);
Neil Armstrong0ef8e402019-06-12 11:49:06 +0200100
101 return 0;
102}
Neil Armstrong559e6252019-08-06 17:21:02 +0200103
Neil Armstrongb1dd7de2019-08-06 17:28:36 +0200104#define AO_SEC_SD_CFG15 0xfc
105#define REBOOT_REASON_MASK GENMASK(15, 12)
106
107int meson_sm_get_reboot_reason(void)
108{
109 struct regmap *regmap;
110 int nodeoffset;
111 ofnode node;
112 unsigned int reason;
113
114 /* find the offset of compatible node */
115 nodeoffset = fdt_node_offset_by_compatible(gd->fdt_blob, -1,
116 "amlogic,meson-gx-ao-secure");
117 if (nodeoffset < 0) {
118 printf("%s: failed to get amlogic,meson-gx-ao-secure\n",
119 __func__);
120 return -ENODEV;
121 }
122
123 /* get regmap from the syscon node */
124 node = offset_to_ofnode(nodeoffset);
125 regmap = syscon_node_to_regmap(node);
126 if (IS_ERR(regmap)) {
127 printf("%s: failed to get regmap\n", __func__);
128 return -EINVAL;
129 }
130
131 regmap_read(regmap, AO_SEC_SD_CFG15, &reason);
132
133 /* The SMC call is not used, we directly use AO_SEC_SD_CFG15 */
134 return FIELD_GET(REBOOT_REASON_MASK, reason);
135}
Alexey Romanoveb0a01e2023-05-31 12:31:54 +0300136
137int meson_sm_pwrdm_set(size_t index, int cmd)
138{
Alexey Romanov7cd53e02023-09-21 11:13:41 +0300139 struct udevice *dev;
140 struct pt_regs regs = { 0 };
141 int err;
Alexey Romanoveb0a01e2023-05-31 12:31:54 +0300142
Alexey Romanov7cd53e02023-09-21 11:13:41 +0300143 dev = meson_get_sm_device();
144 if (IS_ERR(dev))
145 return PTR_ERR(dev);
146
Alexey Romanoveb0a01e2023-05-31 12:31:54 +0300147 regs.regs[1] = index;
148 regs.regs[2] = cmd;
149
Alexey Romanov7cd53e02023-09-21 11:13:41 +0300150 err = sm_call(dev, MESON_SMC_CMD_PWRDM_SET, NULL, &regs);
151 if (err)
152 pr_err("Failed to %s power domain ind=%zu (%d)\n", cmd == PWRDM_ON ?
153 "enable" : "disable", index, err);
Alexey Romanoveb0a01e2023-05-31 12:31:54 +0300154
Alexey Romanov7cd53e02023-09-21 11:13:41 +0300155 return err;
Alexey Romanoveb0a01e2023-05-31 12:31:54 +0300156}