^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0-only
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * Amlogic Secure Monitor driver
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Copyright (C) 2016 Endless Mobile, Inc.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) * Author: Carlo Caione <carlo@endlessm.com>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) #define pr_fmt(fmt) "meson-sm: " fmt
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) #include <linux/arm-smccc.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) #include <linux/bug.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) #include <linux/io.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) #include <linux/module.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) #include <linux/of.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) #include <linux/of_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <linux/platform_device.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <linux/printk.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <linux/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <linux/sizes.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21) #include <linux/slab.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <linux/firmware/meson/meson_sm.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25) struct meson_sm_cmd {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) unsigned int index;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) u32 smc_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #define CMD(d, s) { .index = (d), .smc_id = (s), }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) struct meson_sm_chip {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) unsigned int shmem_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) u32 cmd_shmem_in_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) u32 cmd_shmem_out_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35) struct meson_sm_cmd cmd[];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38) static const struct meson_sm_chip gxbb_chip = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) .shmem_size = SZ_4K,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40) .cmd_shmem_in_base = 0x82000020,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41) .cmd_shmem_out_base = 0x82000021,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) .cmd = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) CMD(SM_EFUSE_READ, 0x82000030),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) CMD(SM_EFUSE_WRITE, 0x82000031),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) CMD(SM_EFUSE_USER_MAX, 0x82000033),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) CMD(SM_GET_CHIP_ID, 0x82000044),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) CMD(SM_A1_PWRC_SET, 0x82000093),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) CMD(SM_A1_PWRC_GET, 0x82000095),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) { /* sentinel */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) struct meson_sm_firmware {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) const struct meson_sm_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) void __iomem *sm_shmem_in_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) void __iomem *sm_shmem_out_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) static u32 meson_sm_get_cmd(const struct meson_sm_chip *chip,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) unsigned int cmd_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) const struct meson_sm_cmd *cmd = chip->cmd;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) while (cmd->smc_id && cmd->index != cmd_index)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65) cmd++;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67) return cmd->smc_id;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) static u32 __meson_sm_call(u32 cmd, u32 arg0, u32 arg1, u32 arg2,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) u32 arg3, u32 arg4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) struct arm_smccc_res res;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) arm_smccc_smc(cmd, arg0, arg1, arg2, arg3, arg4, 0, 0, &res);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) return res.a0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) static void __iomem *meson_sm_map_shmem(u32 cmd_shmem, unsigned int size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) u32 sm_phy_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) sm_phy_base = __meson_sm_call(cmd_shmem, 0, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) if (!sm_phy_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) return ioremap_cache(sm_phy_base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) * meson_sm_call - generic SMC32 call to the secure-monitor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) * @fw: Pointer to secure-monitor firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) * @cmd_index: Index of the SMC32 function ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) * @ret: Returned value
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96) * @arg0: SMC32 Argument 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) * @arg1: SMC32 Argument 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) * @arg2: SMC32 Argument 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) * @arg3: SMC32 Argument 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) * @arg4: SMC32 Argument 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) * Return: 0 on success, a negative value on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) int meson_sm_call(struct meson_sm_firmware *fw, unsigned int cmd_index,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105) u32 *ret, u32 arg0, u32 arg1, u32 arg2, u32 arg3, u32 arg4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) u32 cmd, lret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) if (!fw->chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) cmd = meson_sm_get_cmd(fw->chip, cmd_index);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113) if (!cmd)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) lret = __meson_sm_call(cmd, arg0, arg1, arg2, arg3, arg4);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (ret)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) *ret = lret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) EXPORT_SYMBOL(meson_sm_call);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) * meson_sm_call_read - retrieve data from secure-monitor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) * @fw: Pointer to secure-monitor firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) * @buffer: Buffer to store the retrieved data
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) * @bsize: Size of the buffer
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) * @cmd_index: Index of the SMC32 function ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) * @arg0: SMC32 Argument 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) * @arg1: SMC32 Argument 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) * @arg2: SMC32 Argument 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) * @arg3: SMC32 Argument 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) * @arg4: SMC32 Argument 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) * Return: size of read data on success, a negative value on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) * When 0 is returned there is no guarantee about the amount of
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140) * data read and bsize bytes are copied in buffer.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142) int meson_sm_call_read(struct meson_sm_firmware *fw, void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) unsigned int bsize, unsigned int cmd_index, u32 arg0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) u32 arg1, u32 arg2, u32 arg3, u32 arg4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) u32 size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) if (!fw->chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (!fw->chip->cmd_shmem_out_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) if (bsize > fw->chip->shmem_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) if (meson_sm_call(fw, cmd_index, &size, arg0, arg1, arg2, arg3, arg4) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) if (size > bsize)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) ret = size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) if (!size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167) size = bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169) if (buffer)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) memcpy(buffer, fw->sm_shmem_out_base, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) EXPORT_SYMBOL(meson_sm_call_read);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) * meson_sm_call_write - send data to secure-monitor
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) * @fw: Pointer to secure-monitor firmware
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180) * @buffer: Buffer containing data to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) * @size: Size of the data to send
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) * @cmd_index: Index of the SMC32 function ID
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) * @arg0: SMC32 Argument 0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) * @arg1: SMC32 Argument 1
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) * @arg2: SMC32 Argument 2
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) * @arg3: SMC32 Argument 3
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) * @arg4: SMC32 Argument 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189) * Return: size of sent data on success, a negative value on error
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) int meson_sm_call_write(struct meson_sm_firmware *fw, void *buffer,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) unsigned int size, unsigned int cmd_index, u32 arg0,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) u32 arg1, u32 arg2, u32 arg3, u32 arg4)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) u32 written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) if (!fw->chip)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) return -ENOENT;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (size > fw->chip->shmem_size)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) if (!fw->chip->cmd_shmem_in_base)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) memcpy(fw->sm_shmem_in_base, buffer, size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) if (meson_sm_call(fw, cmd_index, &written, arg0, arg1, arg2, arg3, arg4) < 0)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211) if (!written)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) return written;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) EXPORT_SYMBOL(meson_sm_call_write);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) /**
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) * meson_sm_get - get pointer to meson_sm_firmware structure.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) * @sm_node: Pointer to the secure-monitor Device Tree node.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) * Return: NULL is the secure-monitor device is not ready.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) struct meson_sm_firmware *meson_sm_get(struct device_node *sm_node)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227) struct platform_device *pdev = of_find_device_by_node(sm_node);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) if (!pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) return NULL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232) return platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) EXPORT_SYMBOL_GPL(meson_sm_get);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) #define SM_CHIP_ID_LENGTH 119
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237) #define SM_CHIP_ID_OFFSET 4
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) #define SM_CHIP_ID_SIZE 12
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) static ssize_t serial_show(struct device *dev, struct device_attribute *attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) char *buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) struct platform_device *pdev = to_platform_device(dev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) struct meson_sm_firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) uint8_t *id_buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) int ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) fw = platform_get_drvdata(pdev);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) id_buf = kmalloc(SM_CHIP_ID_LENGTH, GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if (!id_buf)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) ret = meson_sm_call_read(fw, id_buf, SM_CHIP_ID_LENGTH, SM_GET_CHIP_ID,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) 0, 0, 0, 0, 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) if (ret < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) kfree(id_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) ret = sprintf(buf, "%12phN\n", &id_buf[SM_CHIP_ID_OFFSET]);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) kfree(id_buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) return ret;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268) static DEVICE_ATTR_RO(serial);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) static struct attribute *meson_sm_sysfs_attributes[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) &dev_attr_serial.attr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) NULL,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) static const struct attribute_group meson_sm_sysfs_attr_group = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) .attrs = meson_sm_sysfs_attributes,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) static const struct of_device_id meson_sm_ids[] = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) { .compatible = "amlogic,meson-gxbb-sm", .data = &gxbb_chip },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) { /* sentinel */ },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) static int __init meson_sm_probe(struct platform_device *pdev)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 285) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 286) struct device *dev = &pdev->dev;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 287) const struct meson_sm_chip *chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 288) struct meson_sm_firmware *fw;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 289)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 290) fw = devm_kzalloc(dev, sizeof(*fw), GFP_KERNEL);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 291) if (!fw)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 292) return -ENOMEM;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 293)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 294) chip = of_match_device(meson_sm_ids, dev)->data;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 295)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 296) if (chip->cmd_shmem_in_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 297) fw->sm_shmem_in_base = meson_sm_map_shmem(chip->cmd_shmem_in_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 298) chip->shmem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 299) if (WARN_ON(!fw->sm_shmem_in_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 300) goto out;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 301) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 302)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 303) if (chip->cmd_shmem_out_base) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 304) fw->sm_shmem_out_base = meson_sm_map_shmem(chip->cmd_shmem_out_base,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 305) chip->shmem_size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 306) if (WARN_ON(!fw->sm_shmem_out_base))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 307) goto out_in_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 308) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 309)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 310) fw->chip = chip;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 311)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 312) platform_set_drvdata(pdev, fw);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 313)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 314) pr_info("secure-monitor enabled\n");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 315)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 316) if (sysfs_create_group(&pdev->dev.kobj, &meson_sm_sysfs_attr_group))
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 317) goto out_in_base;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 318)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 319) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 320)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 321) out_in_base:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 322) iounmap(fw->sm_shmem_in_base);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 323) out:
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 324) return -EINVAL;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 325) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 326)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 327) static struct platform_driver meson_sm_driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 328) .driver = {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 329) .name = "meson-sm",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 330) .of_match_table = of_match_ptr(meson_sm_ids),
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 331) },
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 332) };
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 333) module_platform_driver_probe(meson_sm_driver, meson_sm_probe);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 334) MODULE_LICENSE("GPL v2");