1 /* 2 * Copyright (C) 2018 Marvell International Ltd. 3 * 4 * SPDX-License-Identifier: BSD-3-Clause 5 * https://spdx.org/licenses 6 */ 7 8 #include <common/debug.h> 9 #include <common/runtime_svc.h> 10 #include <drivers/marvell/cache_llc.h> 11 #include <drivers/marvell/mochi/ap_setup.h> 12 #include <drivers/rambus/trng_ip_76.h> 13 #include <lib/smccc.h> 14 15 #include <marvell_plat_priv.h> 16 #include <plat_marvell.h> 17 18 #include "comphy/phy-comphy-cp110.h" 19 #include "secure_dfx_access/dfx.h" 20 #include "ddr_phy_access.h" 21 #include <stdbool.h> 22 23 /* #define DEBUG_COMPHY */ 24 #ifdef DEBUG_COMPHY 25 #define debug(format...) NOTICE(format) 26 #else 27 #define debug(format, arg...) 28 #endif 29 30 /* Comphy related FID's */ 31 #define MV_SIP_COMPHY_POWER_ON 0x82000001 32 #define MV_SIP_COMPHY_POWER_OFF 0x82000002 33 #define MV_SIP_COMPHY_PLL_LOCK 0x82000003 34 #define MV_SIP_COMPHY_XFI_TRAIN 0x82000004 35 #define MV_SIP_COMPHY_DIG_RESET 0x82000005 36 37 /* Miscellaneous FID's' */ 38 #define MV_SIP_DRAM_SIZE 0x82000010 39 #define MV_SIP_LLC_ENABLE 0x82000011 40 #define MV_SIP_PMU_IRQ_ENABLE 0x82000012 41 #define MV_SIP_PMU_IRQ_DISABLE 0x82000013 42 #define MV_SIP_DFX 0x82000014 43 #define MV_SIP_DDR_PHY_WRITE 0x82000015 44 #define MV_SIP_DDR_PHY_READ 0x82000016 45 46 /* TRNG */ 47 #define MV_SIP_RNG_64 0xC200FF11 48 49 #define MAX_LANE_NR 6 50 #define MVEBU_COMPHY_OFFSET 0x441000 51 #define MVEBU_CP_BASE_MASK (~0xffffff) 52 53 /* This macro is used to identify COMPHY related calls from SMC function ID */ 54 #define is_comphy_fid(fid) \ 55 ((fid) >= MV_SIP_COMPHY_POWER_ON && (fid) <= MV_SIP_COMPHY_DIG_RESET) 56 57 _Bool is_cp_range_valid(u_register_t *addr) 58 { 59 int cp_nr; 60 61 *addr &= MVEBU_CP_BASE_MASK; 62 for (cp_nr = 0; cp_nr < CP_NUM; cp_nr++) { 63 if (*addr == MVEBU_CP_REGS_BASE(cp_nr)) 64 return true; 65 } 66 67 return false; 68 } 69 70 uintptr_t mrvl_sip_smc_handler(uint32_t smc_fid, 71 u_register_t x1, 72 u_register_t x2, 73 u_register_t x3, 74 u_register_t x4, 75 void *cookie, 76 void *handle, 77 u_register_t flags) 78 { 79 u_register_t ret, read; 80 uint32_t w2[2] = {0, 0}; 81 int i; 82 83 debug("%s: got SMC (0x%x) x1 0x%lx, x2 0x%lx, x3 0x%lx\n", 84 __func__, smc_fid, x1, x2, x3); 85 86 if (is_comphy_fid(smc_fid)) { 87 /* validate address passed via x1 */ 88 if (!is_cp_range_valid(&x1)) { 89 ERROR("%s: Wrong smc (0x%x) address: %lx\n", 90 __func__, smc_fid, x1); 91 SMC_RET1(handle, SMC_UNK); 92 } 93 94 x1 += MVEBU_COMPHY_OFFSET; 95 96 if (x2 >= MAX_LANE_NR) { 97 ERROR("%s: Wrong smc (0x%x) lane nr: %lx\n", 98 __func__, smc_fid, x2); 99 SMC_RET1(handle, SMC_UNK); 100 } 101 } 102 103 switch (smc_fid) { 104 105 /* Comphy related FID's */ 106 case MV_SIP_COMPHY_POWER_ON: 107 /* x1: comphy_base, x2: comphy_index, x3: comphy_mode */ 108 ret = mvebu_cp110_comphy_power_on(x1, x2, x3); 109 SMC_RET1(handle, ret); 110 case MV_SIP_COMPHY_POWER_OFF: 111 /* x1: comphy_base, x2: comphy_index */ 112 ret = mvebu_cp110_comphy_power_off(x1, x2, x3); 113 SMC_RET1(handle, ret); 114 case MV_SIP_COMPHY_PLL_LOCK: 115 /* x1: comphy_base, x2: comphy_index */ 116 ret = mvebu_cp110_comphy_is_pll_locked(x1, x2); 117 SMC_RET1(handle, ret); 118 case MV_SIP_COMPHY_XFI_TRAIN: 119 /* x1: comphy_base, x2: comphy_index */ 120 ret = mvebu_cp110_comphy_xfi_rx_training(x1, x2); 121 SMC_RET1(handle, ret); 122 case MV_SIP_COMPHY_DIG_RESET: 123 /* x1: comphy_base, x2: comphy_index, x3: mode, x4: command */ 124 ret = mvebu_cp110_comphy_digital_reset(x1, x2, x3, x4); 125 SMC_RET1(handle, ret); 126 127 /* Miscellaneous FID's' */ 128 case MV_SIP_DRAM_SIZE: 129 ret = mvebu_get_dram_size(MVEBU_REGS_BASE); 130 SMC_RET1(handle, ret); 131 case MV_SIP_LLC_ENABLE: 132 for (i = 0; i < ap_get_count(); i++) 133 llc_runtime_enable(i); 134 135 SMC_RET1(handle, 0); 136 #ifdef MVEBU_PMU_IRQ_WA 137 case MV_SIP_PMU_IRQ_ENABLE: 138 mvebu_pmu_interrupt_enable(); 139 SMC_RET1(handle, 0); 140 case MV_SIP_PMU_IRQ_DISABLE: 141 mvebu_pmu_interrupt_disable(); 142 SMC_RET1(handle, 0); 143 #endif 144 case MV_SIP_DFX: 145 if (x1 >= MV_SIP_DFX_THERMAL_INIT && 146 x1 <= MV_SIP_DFX_THERMAL_SEL_CHANNEL) { 147 ret = mvebu_dfx_thermal_handle(x1, &read, x2, x3); 148 SMC_RET2(handle, ret, read); 149 } 150 if (x1 >= MV_SIP_DFX_SREAD && x1 <= MV_SIP_DFX_SWRITE) { 151 ret = mvebu_dfx_misc_handle(x1, &read, x2, x3); 152 SMC_RET2(handle, ret, read); 153 } 154 155 SMC_RET1(handle, SMC_UNK); 156 case MV_SIP_DDR_PHY_WRITE: 157 ret = mvebu_ddr_phy_write(x1, x2); 158 SMC_RET1(handle, ret); 159 case MV_SIP_DDR_PHY_READ: 160 read = 0; 161 ret = mvebu_ddr_phy_read(x1, (uint16_t *)&read); 162 SMC_RET2(handle, ret, read); 163 case MV_SIP_RNG_64: 164 ret = eip76_rng_get_random((uint8_t *)&w2, 4 * (x1 % 2 + 1)); 165 SMC_RET3(handle, ret, w2[0], w2[1]); 166 default: 167 ERROR("%s: unhandled SMC (0x%x)\n", __func__, smc_fid); 168 SMC_RET1(handle, SMC_UNK); 169 } 170 } 171 172 /* Define a runtime service descriptor for fast SMC calls */ 173 DECLARE_RT_SVC( 174 marvell_sip_svc, 175 OEN_SIP_START, 176 OEN_SIP_END, 177 SMC_TYPE_FAST, 178 NULL, 179 mrvl_sip_smc_handler 180 ); 181