1 1.3 riastrad /* $NetBSD: radeon_si_smc.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $ */ 2 1.1 riastrad 3 1.1 riastrad /* 4 1.1 riastrad * Copyright 2011 Advanced Micro Devices, Inc. 5 1.1 riastrad * 6 1.1 riastrad * Permission is hereby granted, free of charge, to any person obtaining a 7 1.1 riastrad * copy of this software and associated documentation files (the "Software"), 8 1.1 riastrad * to deal in the Software without restriction, including without limitation 9 1.1 riastrad * the rights to use, copy, modify, merge, publish, distribute, sublicense, 10 1.1 riastrad * and/or sell copies of the Software, and to permit persons to whom the 11 1.1 riastrad * Software is furnished to do so, subject to the following conditions: 12 1.1 riastrad * 13 1.1 riastrad * The above copyright notice and this permission notice shall be included in 14 1.1 riastrad * all copies or substantial portions of the Software. 15 1.1 riastrad * 16 1.1 riastrad * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 17 1.1 riastrad * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 18 1.1 riastrad * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 19 1.1 riastrad * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR 20 1.1 riastrad * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, 21 1.1 riastrad * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR 22 1.1 riastrad * OTHER DEALINGS IN THE SOFTWARE. 23 1.1 riastrad * 24 1.1 riastrad * Authors: Alex Deucher 25 1.1 riastrad */ 26 1.1 riastrad 27 1.1 riastrad #include <sys/cdefs.h> 28 1.3 riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_si_smc.c,v 1.3 2021/12/18 23:45:43 riastradh Exp $"); 29 1.1 riastrad 30 1.1 riastrad #include <linux/firmware.h> 31 1.3 riastrad 32 1.1 riastrad #include "radeon.h" 33 1.1 riastrad #include "sid.h" 34 1.1 riastrad #include "ppsmc.h" 35 1.1 riastrad #include "radeon_ucode.h" 36 1.1 riastrad #include "sislands_smc.h" 37 1.1 riastrad 38 1.1 riastrad static int si_set_smc_sram_address(struct radeon_device *rdev, 39 1.1 riastrad u32 smc_address, u32 limit) 40 1.1 riastrad { 41 1.1 riastrad if (smc_address & 3) 42 1.1 riastrad return -EINVAL; 43 1.1 riastrad if ((smc_address + 3) > limit) 44 1.1 riastrad return -EINVAL; 45 1.1 riastrad 46 1.1 riastrad WREG32(SMC_IND_INDEX_0, smc_address); 47 1.1 riastrad WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0); 48 1.1 riastrad 49 1.1 riastrad return 0; 50 1.1 riastrad } 51 1.1 riastrad 52 1.1 riastrad int si_copy_bytes_to_smc(struct radeon_device *rdev, 53 1.1 riastrad u32 smc_start_address, 54 1.1 riastrad const u8 *src, u32 byte_count, u32 limit) 55 1.1 riastrad { 56 1.1 riastrad unsigned long flags; 57 1.1 riastrad int ret = 0; 58 1.1 riastrad u32 data, original_data, addr, extra_shift; 59 1.1 riastrad 60 1.1 riastrad if (smc_start_address & 3) 61 1.1 riastrad return -EINVAL; 62 1.1 riastrad if ((smc_start_address + byte_count) > limit) 63 1.1 riastrad return -EINVAL; 64 1.1 riastrad 65 1.1 riastrad addr = smc_start_address; 66 1.1 riastrad 67 1.1 riastrad spin_lock_irqsave(&rdev->smc_idx_lock, flags); 68 1.1 riastrad while (byte_count >= 4) { 69 1.1 riastrad /* SMC address space is BE */ 70 1.2 msaitoh data = ((u32)src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; 71 1.1 riastrad 72 1.1 riastrad ret = si_set_smc_sram_address(rdev, addr, limit); 73 1.1 riastrad if (ret) 74 1.1 riastrad goto done; 75 1.1 riastrad 76 1.1 riastrad WREG32(SMC_IND_DATA_0, data); 77 1.1 riastrad 78 1.1 riastrad src += 4; 79 1.1 riastrad byte_count -= 4; 80 1.1 riastrad addr += 4; 81 1.1 riastrad } 82 1.1 riastrad 83 1.1 riastrad /* RMW for the final bytes */ 84 1.1 riastrad if (byte_count > 0) { 85 1.1 riastrad data = 0; 86 1.1 riastrad 87 1.1 riastrad ret = si_set_smc_sram_address(rdev, addr, limit); 88 1.1 riastrad if (ret) 89 1.1 riastrad goto done; 90 1.1 riastrad 91 1.1 riastrad original_data = RREG32(SMC_IND_DATA_0); 92 1.1 riastrad 93 1.1 riastrad extra_shift = 8 * (4 - byte_count); 94 1.1 riastrad 95 1.1 riastrad while (byte_count > 0) { 96 1.1 riastrad /* SMC address space is BE */ 97 1.1 riastrad data = (data << 8) + *src++; 98 1.1 riastrad byte_count--; 99 1.1 riastrad } 100 1.1 riastrad 101 1.1 riastrad data <<= extra_shift; 102 1.1 riastrad 103 1.1 riastrad data |= (original_data & ~((~0UL) << extra_shift)); 104 1.1 riastrad 105 1.1 riastrad ret = si_set_smc_sram_address(rdev, addr, limit); 106 1.1 riastrad if (ret) 107 1.1 riastrad goto done; 108 1.1 riastrad 109 1.1 riastrad WREG32(SMC_IND_DATA_0, data); 110 1.1 riastrad } 111 1.1 riastrad 112 1.1 riastrad done: 113 1.1 riastrad spin_unlock_irqrestore(&rdev->smc_idx_lock, flags); 114 1.1 riastrad 115 1.1 riastrad return ret; 116 1.1 riastrad } 117 1.1 riastrad 118 1.1 riastrad void si_start_smc(struct radeon_device *rdev) 119 1.1 riastrad { 120 1.1 riastrad u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL); 121 1.1 riastrad 122 1.1 riastrad tmp &= ~RST_REG; 123 1.1 riastrad 124 1.1 riastrad WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp); 125 1.1 riastrad } 126 1.1 riastrad 127 1.1 riastrad void si_reset_smc(struct radeon_device *rdev) 128 1.1 riastrad { 129 1.1 riastrad u32 tmp; 130 1.1 riastrad 131 1.1 riastrad RREG32(CB_CGTT_SCLK_CTRL); 132 1.1 riastrad RREG32(CB_CGTT_SCLK_CTRL); 133 1.1 riastrad RREG32(CB_CGTT_SCLK_CTRL); 134 1.1 riastrad RREG32(CB_CGTT_SCLK_CTRL); 135 1.1 riastrad 136 1.1 riastrad tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL); 137 1.1 riastrad tmp |= RST_REG; 138 1.1 riastrad WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp); 139 1.1 riastrad } 140 1.1 riastrad 141 1.1 riastrad int si_program_jump_on_start(struct radeon_device *rdev) 142 1.1 riastrad { 143 1.1 riastrad static const u8 data[] = { 0x0E, 0x00, 0x40, 0x40 }; 144 1.1 riastrad 145 1.1 riastrad return si_copy_bytes_to_smc(rdev, 0x0, data, 4, sizeof(data)+1); 146 1.1 riastrad } 147 1.1 riastrad 148 1.1 riastrad void si_stop_smc_clock(struct radeon_device *rdev) 149 1.1 riastrad { 150 1.1 riastrad u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0); 151 1.1 riastrad 152 1.1 riastrad tmp |= CK_DISABLE; 153 1.1 riastrad 154 1.1 riastrad WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp); 155 1.1 riastrad } 156 1.1 riastrad 157 1.1 riastrad void si_start_smc_clock(struct radeon_device *rdev) 158 1.1 riastrad { 159 1.1 riastrad u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0); 160 1.1 riastrad 161 1.1 riastrad tmp &= ~CK_DISABLE; 162 1.1 riastrad 163 1.1 riastrad WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp); 164 1.1 riastrad } 165 1.1 riastrad 166 1.1 riastrad bool si_is_smc_running(struct radeon_device *rdev) 167 1.1 riastrad { 168 1.1 riastrad u32 rst = RREG32_SMC(SMC_SYSCON_RESET_CNTL); 169 1.1 riastrad u32 clk = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0); 170 1.1 riastrad 171 1.1 riastrad if (!(rst & RST_REG) && !(clk & CK_DISABLE)) 172 1.1 riastrad return true; 173 1.1 riastrad 174 1.1 riastrad return false; 175 1.1 riastrad } 176 1.1 riastrad 177 1.1 riastrad PPSMC_Result si_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg) 178 1.1 riastrad { 179 1.1 riastrad u32 tmp; 180 1.1 riastrad int i; 181 1.1 riastrad 182 1.1 riastrad if (!si_is_smc_running(rdev)) 183 1.1 riastrad return PPSMC_Result_Failed; 184 1.1 riastrad 185 1.1 riastrad WREG32(SMC_MESSAGE_0, msg); 186 1.1 riastrad 187 1.1 riastrad for (i = 0; i < rdev->usec_timeout; i++) { 188 1.1 riastrad tmp = RREG32(SMC_RESP_0); 189 1.1 riastrad if (tmp != 0) 190 1.1 riastrad break; 191 1.1 riastrad udelay(1); 192 1.1 riastrad } 193 1.1 riastrad tmp = RREG32(SMC_RESP_0); 194 1.1 riastrad 195 1.1 riastrad return (PPSMC_Result)tmp; 196 1.1 riastrad } 197 1.1 riastrad 198 1.1 riastrad PPSMC_Result si_wait_for_smc_inactive(struct radeon_device *rdev) 199 1.1 riastrad { 200 1.1 riastrad u32 tmp; 201 1.1 riastrad int i; 202 1.1 riastrad 203 1.1 riastrad if (!si_is_smc_running(rdev)) 204 1.1 riastrad return PPSMC_Result_OK; 205 1.1 riastrad 206 1.1 riastrad for (i = 0; i < rdev->usec_timeout; i++) { 207 1.1 riastrad tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0); 208 1.1 riastrad if ((tmp & CKEN) == 0) 209 1.1 riastrad break; 210 1.1 riastrad udelay(1); 211 1.1 riastrad } 212 1.1 riastrad 213 1.1 riastrad return PPSMC_Result_OK; 214 1.1 riastrad } 215 1.1 riastrad 216 1.1 riastrad int si_load_smc_ucode(struct radeon_device *rdev, u32 limit) 217 1.1 riastrad { 218 1.1 riastrad unsigned long flags; 219 1.1 riastrad u32 ucode_start_address; 220 1.1 riastrad u32 ucode_size; 221 1.1 riastrad const u8 *src; 222 1.1 riastrad u32 data; 223 1.1 riastrad 224 1.1 riastrad if (!rdev->smc_fw) 225 1.1 riastrad return -EINVAL; 226 1.1 riastrad 227 1.1 riastrad if (rdev->new_fw) { 228 1.1 riastrad const struct smc_firmware_header_v1_0 *hdr = 229 1.1 riastrad (const struct smc_firmware_header_v1_0 *)rdev->smc_fw->data; 230 1.1 riastrad 231 1.1 riastrad radeon_ucode_print_smc_hdr(&hdr->header); 232 1.1 riastrad 233 1.1 riastrad ucode_start_address = le32_to_cpu(hdr->ucode_start_addr); 234 1.1 riastrad ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes); 235 1.1 riastrad src = (const u8 *) 236 1.1 riastrad (rdev->smc_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes)); 237 1.1 riastrad } else { 238 1.1 riastrad switch (rdev->family) { 239 1.1 riastrad case CHIP_TAHITI: 240 1.1 riastrad ucode_start_address = TAHITI_SMC_UCODE_START; 241 1.1 riastrad ucode_size = TAHITI_SMC_UCODE_SIZE; 242 1.1 riastrad break; 243 1.1 riastrad case CHIP_PITCAIRN: 244 1.1 riastrad ucode_start_address = PITCAIRN_SMC_UCODE_START; 245 1.1 riastrad ucode_size = PITCAIRN_SMC_UCODE_SIZE; 246 1.1 riastrad break; 247 1.1 riastrad case CHIP_VERDE: 248 1.1 riastrad ucode_start_address = VERDE_SMC_UCODE_START; 249 1.1 riastrad ucode_size = VERDE_SMC_UCODE_SIZE; 250 1.1 riastrad break; 251 1.1 riastrad case CHIP_OLAND: 252 1.1 riastrad ucode_start_address = OLAND_SMC_UCODE_START; 253 1.1 riastrad ucode_size = OLAND_SMC_UCODE_SIZE; 254 1.1 riastrad break; 255 1.1 riastrad case CHIP_HAINAN: 256 1.1 riastrad ucode_start_address = HAINAN_SMC_UCODE_START; 257 1.1 riastrad ucode_size = HAINAN_SMC_UCODE_SIZE; 258 1.1 riastrad break; 259 1.1 riastrad default: 260 1.1 riastrad DRM_ERROR("unknown asic in smc ucode loader\n"); 261 1.1 riastrad BUG(); 262 1.1 riastrad } 263 1.1 riastrad src = (const u8 *)rdev->smc_fw->data; 264 1.1 riastrad } 265 1.1 riastrad 266 1.1 riastrad if (ucode_size & 3) 267 1.1 riastrad return -EINVAL; 268 1.1 riastrad 269 1.1 riastrad spin_lock_irqsave(&rdev->smc_idx_lock, flags); 270 1.1 riastrad WREG32(SMC_IND_INDEX_0, ucode_start_address); 271 1.1 riastrad WREG32_P(SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, ~AUTO_INCREMENT_IND_0); 272 1.1 riastrad while (ucode_size >= 4) { 273 1.1 riastrad /* SMC address space is BE */ 274 1.2 msaitoh data = ((u32)src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3]; 275 1.1 riastrad 276 1.1 riastrad WREG32(SMC_IND_DATA_0, data); 277 1.1 riastrad 278 1.1 riastrad src += 4; 279 1.1 riastrad ucode_size -= 4; 280 1.1 riastrad } 281 1.1 riastrad WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0); 282 1.1 riastrad spin_unlock_irqrestore(&rdev->smc_idx_lock, flags); 283 1.1 riastrad 284 1.1 riastrad return 0; 285 1.1 riastrad } 286 1.1 riastrad 287 1.1 riastrad int si_read_smc_sram_dword(struct radeon_device *rdev, u32 smc_address, 288 1.1 riastrad u32 *value, u32 limit) 289 1.1 riastrad { 290 1.1 riastrad unsigned long flags; 291 1.1 riastrad int ret; 292 1.1 riastrad 293 1.1 riastrad spin_lock_irqsave(&rdev->smc_idx_lock, flags); 294 1.1 riastrad ret = si_set_smc_sram_address(rdev, smc_address, limit); 295 1.1 riastrad if (ret == 0) 296 1.1 riastrad *value = RREG32(SMC_IND_DATA_0); 297 1.1 riastrad spin_unlock_irqrestore(&rdev->smc_idx_lock, flags); 298 1.1 riastrad 299 1.1 riastrad return ret; 300 1.1 riastrad } 301 1.1 riastrad 302 1.1 riastrad int si_write_smc_sram_dword(struct radeon_device *rdev, u32 smc_address, 303 1.1 riastrad u32 value, u32 limit) 304 1.1 riastrad { 305 1.1 riastrad unsigned long flags; 306 1.1 riastrad int ret; 307 1.1 riastrad 308 1.1 riastrad spin_lock_irqsave(&rdev->smc_idx_lock, flags); 309 1.1 riastrad ret = si_set_smc_sram_address(rdev, smc_address, limit); 310 1.1 riastrad if (ret == 0) 311 1.1 riastrad WREG32(SMC_IND_DATA_0, value); 312 1.1 riastrad spin_unlock_irqrestore(&rdev->smc_idx_lock, flags); 313 1.1 riastrad 314 1.1 riastrad return ret; 315 1.1 riastrad } 316