blob: 4d9f83d3b38d54e20d7d2bec997793cca5d7f0e4 [file] [log] [blame]
Tom Rini10e47792018-05-06 17:58:06 -04001// SPDX-License-Identifier: GPL-2.0+
Beniamino Galvani38e1a602016-05-08 08:30:17 +02002/*
3 * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com>
4 *
Beniamino Galvani38e1a602016-05-08 08:30:17 +02005 * Secure monitor calls.
6 */
7
Alexey Romanovdf1a71c2023-09-21 11:13:40 +03008#include <dm.h>
Simon Glass0f2af882020-05-10 11:40:05 -06009#include <log.h>
Alexey Romanovdf1a71c2023-09-21 11:13:40 +030010#include <regmap.h>
Alexey Romanovbabf17e2023-09-21 11:13:41 +030011#include <sm.h>
Alexey Romanovdf1a71c2023-09-21 11:13:40 +030012#include <syscon.h>
Neil Armstrong63f475c2019-06-12 11:49:06 +020013#include <asm/arch/sm.h>
Simon Glass274e0b02020-05-10 11:39:56 -060014#include <asm/cache.h>
Simon Glass3ba929a2020-10-30 21:38:53 -060015#include <asm/global_data.h>
Simon Glass6b9f0102020-05-10 11:40:06 -060016#include <asm/ptrace.h>
Simon Glass4dcacfc2020-05-10 11:40:13 -060017#include <linux/bitops.h>
Simon Glassd66c5f72020-02-03 07:36:15 -070018#include <linux/err.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020019#include <linux/kernel.h>
Neil Armstrong385309c2019-08-06 17:28:36 +020020#include <linux/bitfield.h>
Alexey Romanovbabf17e2023-09-21 11:13:41 +030021#include <meson/sm.h>
Beniamino Galvani38e1a602016-05-08 08:30:17 +020022
Alexey Romanovbabf17e2023-09-21 11:13:41 +030023static inline struct udevice *meson_get_sm_device(void)
Beniamino Galvani38e1a602016-05-08 08:30:17 +020024{
Alexey Romanovbabf17e2023-09-21 11:13:41 +030025 struct udevice *dev;
26 int err;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020027
Alexey Romanovbabf17e2023-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 Galvani38e1a602016-05-08 08:30:17 +020033
Alexey Romanovbabf17e2023-09-21 11:13:41 +030034 return dev;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020035}
36
37ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size)
38{
Alexey Romanovbabf17e2023-09-21 11:13:41 +030039 struct udevice *dev;
40 struct pt_regs regs = { 0 };
41 int err;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020042
Alexey Romanovbabf17e2023-09-21 11:13:41 +030043 dev = meson_get_sm_device();
44 if (IS_ERR(dev))
45 return PTR_ERR(dev);
Beniamino Galvani38e1a602016-05-08 08:30:17 +020046
Beniamino Galvani38e1a602016-05-08 08:30:17 +020047 regs.regs[1] = offset;
48 regs.regs[2] = size;
49
Alexey Romanovbabf17e2023-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 Galvani38e1a602016-05-08 08:30:17 +020054
Alexey Romanovbabf17e2023-09-21 11:13:41 +030055 return err;
Beniamino Galvani38e1a602016-05-08 08:30:17 +020056}
Neil Armstrong63f475c2019-06-12 11:49:06 +020057
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030058ssize_t meson_sm_write_efuse(uintptr_t offset, void *buffer, size_t size)
59{
Alexey Romanovbabf17e2023-09-21 11:13:41 +030060 struct udevice *dev;
61 struct pt_regs regs = { 0 };
62 int err;
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030063
Alexey Romanovbabf17e2023-09-21 11:13:41 +030064 dev = meson_get_sm_device();
65 if (IS_ERR(dev))
66 return PTR_ERR(dev);
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030067
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030068 regs.regs[1] = offset;
69 regs.regs[2] = size;
70
Alexey Romanovbabf17e2023-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 Bocharov14eb82c2021-10-05 15:00:03 +030075
Alexey Romanovbabf17e2023-09-21 11:13:41 +030076 return err;
Vyacheslav Bocharov14eb82c2021-10-05 15:00:03 +030077}
78
Neil Armstrong63f475c2019-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 Romanovbabf17e2023-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 Armstrong63f475c2019-06-12 11:49:06 +020089
Alexey Romanovbabf17e2023-09-21 11:13:41 +030090 dev = meson_get_sm_device();
91 if (IS_ERR(dev))
92 return PTR_ERR(dev);
Neil Armstrong63f475c2019-06-12 11:49:06 +020093
Alexey Romanovbabf17e2023-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 Armstrong63f475c2019-06-12 11:49:06 +020098
Alexey Romanovbabf17e2023-09-21 11:13:41 +030099 memcpy(buffer, id_buffer + SM_CHIP_ID_OFFSET, size);
Neil Armstrong63f475c2019-06-12 11:49:06 +0200100
101 return 0;
102}
Neil Armstrong69800ec2019-08-06 17:21:02 +0200103
Neil Armstrong385309c2019-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 Romanov92404a12023-05-31 12:31:54 +0300136
137int meson_sm_pwrdm_set(size_t index, int cmd)
138{
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300139 struct udevice *dev;
140 struct pt_regs regs = { 0 };
141 int err;
Alexey Romanov92404a12023-05-31 12:31:54 +0300142
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300143 dev = meson_get_sm_device();
144 if (IS_ERR(dev))
145 return PTR_ERR(dev);
146
Alexey Romanov92404a12023-05-31 12:31:54 +0300147 regs.regs[1] = index;
148 regs.regs[2] = cmd;
149
Alexey Romanovbabf17e2023-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 Romanov92404a12023-05-31 12:31:54 +0300154
Alexey Romanovbabf17e2023-09-21 11:13:41 +0300155 return err;
Alexey Romanov92404a12023-05-31 12:31:54 +0300156}