Tom Rini | 10e4779 | 2018-05-06 17:58:06 -0400 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0+ |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 2 | /* |
| 3 | * (C) Copyright 2016 Beniamino Galvani <b.galvani@gmail.com> |
| 4 | * |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 5 | * Secure monitor calls. |
| 6 | */ |
| 7 | |
| 8 | #include <common.h> |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 9 | #include <asm/arch/sm.h> |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 10 | #include <linux/kernel.h> |
| 11 | |
| 12 | #define FN_GET_SHARE_MEM_INPUT_BASE 0x82000020 |
| 13 | #define FN_GET_SHARE_MEM_OUTPUT_BASE 0x82000021 |
| 14 | #define FN_EFUSE_READ 0x82000030 |
| 15 | #define FN_EFUSE_WRITE 0x82000031 |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 16 | #define FN_CHIP_ID 0x82000044 |
Beniamino Galvani | 38e1a60 | 2016-05-08 08:30:17 +0200 | [diff] [blame] | 17 | |
| 18 | static void *shmem_input; |
| 19 | static void *shmem_output; |
| 20 | |
| 21 | static void meson_init_shmem(void) |
| 22 | { |
| 23 | struct pt_regs regs; |
| 24 | |
| 25 | if (shmem_input && shmem_output) |
| 26 | return; |
| 27 | |
| 28 | regs.regs[0] = FN_GET_SHARE_MEM_INPUT_BASE; |
| 29 | smc_call(®s); |
| 30 | shmem_input = (void *)regs.regs[0]; |
| 31 | |
| 32 | regs.regs[0] = FN_GET_SHARE_MEM_OUTPUT_BASE; |
| 33 | smc_call(®s); |
| 34 | shmem_output = (void *)regs.regs[0]; |
| 35 | |
| 36 | debug("Secure Monitor shmem: 0x%p 0x%p\n", shmem_input, shmem_output); |
| 37 | } |
| 38 | |
| 39 | ssize_t meson_sm_read_efuse(uintptr_t offset, void *buffer, size_t size) |
| 40 | { |
| 41 | struct pt_regs regs; |
| 42 | |
| 43 | meson_init_shmem(); |
| 44 | |
| 45 | regs.regs[0] = FN_EFUSE_READ; |
| 46 | regs.regs[1] = offset; |
| 47 | regs.regs[2] = size; |
| 48 | |
| 49 | smc_call(®s); |
| 50 | |
| 51 | if (regs.regs[0] == 0) |
| 52 | return -1; |
| 53 | |
| 54 | memcpy(buffer, shmem_output, min(size, regs.regs[0])); |
| 55 | |
| 56 | return regs.regs[0]; |
| 57 | } |
Neil Armstrong | 63f475c | 2019-06-12 11:49:06 +0200 | [diff] [blame] | 58 | |
| 59 | #define SM_CHIP_ID_LENGTH 119 |
| 60 | #define SM_CHIP_ID_OFFSET 4 |
| 61 | #define SM_CHIP_ID_SIZE 12 |
| 62 | |
| 63 | int meson_sm_get_serial(void *buffer, size_t size) |
| 64 | { |
| 65 | struct pt_regs regs; |
| 66 | |
| 67 | meson_init_shmem(); |
| 68 | |
| 69 | regs.regs[0] = FN_CHIP_ID; |
| 70 | regs.regs[1] = 0; |
| 71 | regs.regs[2] = 0; |
| 72 | |
| 73 | smc_call(®s); |
| 74 | |
| 75 | memcpy(buffer, shmem_output + SM_CHIP_ID_OFFSET, |
| 76 | min_t(size_t, size, SM_CHIP_ID_SIZE)); |
| 77 | |
| 78 | return 0; |
| 79 | } |
Neil Armstrong | 69800ec | 2019-08-06 17:21:02 +0200 | [diff] [blame^] | 80 | |
| 81 | static int do_sm_serial(cmd_tbl_t *cmdtp, int flag, int argc, |
| 82 | char *const argv[]) |
| 83 | { |
| 84 | ulong address; |
| 85 | int ret; |
| 86 | |
| 87 | if (argc < 2) |
| 88 | return CMD_RET_USAGE; |
| 89 | |
| 90 | address = simple_strtoul(argv[1], NULL, 0); |
| 91 | |
| 92 | ret = meson_sm_get_serial((void *)address, SM_CHIP_ID_SIZE); |
| 93 | if (ret) |
| 94 | return CMD_RET_FAILURE; |
| 95 | |
| 96 | return CMD_RET_SUCCESS; |
| 97 | } |
| 98 | |
| 99 | static cmd_tbl_t cmd_sm_sub[] = { |
| 100 | U_BOOT_CMD_MKENT(serial, 2, 1, do_sm_serial, "", ""), |
| 101 | }; |
| 102 | |
| 103 | static int do_sm(cmd_tbl_t *cmdtp, int flag, int argc, |
| 104 | char *const argv[]) |
| 105 | { |
| 106 | cmd_tbl_t *c; |
| 107 | |
| 108 | if (argc < 2) |
| 109 | return CMD_RET_USAGE; |
| 110 | |
| 111 | /* Strip off leading 'sm' command argument */ |
| 112 | argc--; |
| 113 | argv++; |
| 114 | |
| 115 | c = find_cmd_tbl(argv[0], &cmd_sm_sub[0], ARRAY_SIZE(cmd_sm_sub)); |
| 116 | |
| 117 | if (c) |
| 118 | return c->cmd(cmdtp, flag, argc, argv); |
| 119 | else |
| 120 | return CMD_RET_USAGE; |
| 121 | } |
| 122 | |
| 123 | U_BOOT_CMD( |
| 124 | sm, 5, 0, do_sm, |
| 125 | "Secure Monitor Control", |
| 126 | "serial <address> - read chip unique id to memory address" |
| 127 | ); |