18c2ecf20Sopenharmony_ci/*
28c2ecf20Sopenharmony_ci * Copyright 2011 Advanced Micro Devices, Inc.
38c2ecf20Sopenharmony_ci *
48c2ecf20Sopenharmony_ci * Permission is hereby granted, free of charge, to any person obtaining a
58c2ecf20Sopenharmony_ci * copy of this software and associated documentation files (the "Software"),
68c2ecf20Sopenharmony_ci * to deal in the Software without restriction, including without limitation
78c2ecf20Sopenharmony_ci * the rights to use, copy, modify, merge, publish, distribute, sublicense,
88c2ecf20Sopenharmony_ci * and/or sell copies of the Software, and to permit persons to whom the
98c2ecf20Sopenharmony_ci * Software is furnished to do so, subject to the following conditions:
108c2ecf20Sopenharmony_ci *
118c2ecf20Sopenharmony_ci * The above copyright notice and this permission notice shall be included in
128c2ecf20Sopenharmony_ci * all copies or substantial portions of the Software.
138c2ecf20Sopenharmony_ci *
148c2ecf20Sopenharmony_ci * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
158c2ecf20Sopenharmony_ci * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
168c2ecf20Sopenharmony_ci * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
178c2ecf20Sopenharmony_ci * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
188c2ecf20Sopenharmony_ci * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
198c2ecf20Sopenharmony_ci * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
208c2ecf20Sopenharmony_ci * OTHER DEALINGS IN THE SOFTWARE.
218c2ecf20Sopenharmony_ci *
228c2ecf20Sopenharmony_ci * Authors: Alex Deucher
238c2ecf20Sopenharmony_ci */
248c2ecf20Sopenharmony_ci
258c2ecf20Sopenharmony_ci#include <linux/firmware.h>
268c2ecf20Sopenharmony_ci
278c2ecf20Sopenharmony_ci#include "radeon.h"
288c2ecf20Sopenharmony_ci#include "sid.h"
298c2ecf20Sopenharmony_ci#include "ppsmc.h"
308c2ecf20Sopenharmony_ci#include "radeon_ucode.h"
318c2ecf20Sopenharmony_ci#include "sislands_smc.h"
328c2ecf20Sopenharmony_ci
338c2ecf20Sopenharmony_cistatic int si_set_smc_sram_address(struct radeon_device *rdev,
348c2ecf20Sopenharmony_ci				   u32 smc_address, u32 limit)
358c2ecf20Sopenharmony_ci{
368c2ecf20Sopenharmony_ci	if (smc_address & 3)
378c2ecf20Sopenharmony_ci		return -EINVAL;
388c2ecf20Sopenharmony_ci	if ((smc_address + 3) > limit)
398c2ecf20Sopenharmony_ci		return -EINVAL;
408c2ecf20Sopenharmony_ci
418c2ecf20Sopenharmony_ci	WREG32(SMC_IND_INDEX_0, smc_address);
428c2ecf20Sopenharmony_ci	WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
438c2ecf20Sopenharmony_ci
448c2ecf20Sopenharmony_ci	return 0;
458c2ecf20Sopenharmony_ci}
468c2ecf20Sopenharmony_ci
478c2ecf20Sopenharmony_ciint si_copy_bytes_to_smc(struct radeon_device *rdev,
488c2ecf20Sopenharmony_ci			 u32 smc_start_address,
498c2ecf20Sopenharmony_ci			 const u8 *src, u32 byte_count, u32 limit)
508c2ecf20Sopenharmony_ci{
518c2ecf20Sopenharmony_ci	unsigned long flags;
528c2ecf20Sopenharmony_ci	int ret = 0;
538c2ecf20Sopenharmony_ci	u32 data, original_data, addr, extra_shift;
548c2ecf20Sopenharmony_ci
558c2ecf20Sopenharmony_ci	if (smc_start_address & 3)
568c2ecf20Sopenharmony_ci		return -EINVAL;
578c2ecf20Sopenharmony_ci	if ((smc_start_address + byte_count) > limit)
588c2ecf20Sopenharmony_ci		return -EINVAL;
598c2ecf20Sopenharmony_ci
608c2ecf20Sopenharmony_ci	addr = smc_start_address;
618c2ecf20Sopenharmony_ci
628c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
638c2ecf20Sopenharmony_ci	while (byte_count >= 4) {
648c2ecf20Sopenharmony_ci		/* SMC address space is BE */
658c2ecf20Sopenharmony_ci		data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
668c2ecf20Sopenharmony_ci
678c2ecf20Sopenharmony_ci		ret = si_set_smc_sram_address(rdev, addr, limit);
688c2ecf20Sopenharmony_ci		if (ret)
698c2ecf20Sopenharmony_ci			goto done;
708c2ecf20Sopenharmony_ci
718c2ecf20Sopenharmony_ci		WREG32(SMC_IND_DATA_0, data);
728c2ecf20Sopenharmony_ci
738c2ecf20Sopenharmony_ci		src += 4;
748c2ecf20Sopenharmony_ci		byte_count -= 4;
758c2ecf20Sopenharmony_ci		addr += 4;
768c2ecf20Sopenharmony_ci	}
778c2ecf20Sopenharmony_ci
788c2ecf20Sopenharmony_ci	/* RMW for the final bytes */
798c2ecf20Sopenharmony_ci	if (byte_count > 0) {
808c2ecf20Sopenharmony_ci		data = 0;
818c2ecf20Sopenharmony_ci
828c2ecf20Sopenharmony_ci		ret = si_set_smc_sram_address(rdev, addr, limit);
838c2ecf20Sopenharmony_ci		if (ret)
848c2ecf20Sopenharmony_ci			goto done;
858c2ecf20Sopenharmony_ci
868c2ecf20Sopenharmony_ci		original_data = RREG32(SMC_IND_DATA_0);
878c2ecf20Sopenharmony_ci
888c2ecf20Sopenharmony_ci		extra_shift = 8 * (4 - byte_count);
898c2ecf20Sopenharmony_ci
908c2ecf20Sopenharmony_ci		while (byte_count > 0) {
918c2ecf20Sopenharmony_ci			/* SMC address space is BE */
928c2ecf20Sopenharmony_ci			data = (data << 8) + *src++;
938c2ecf20Sopenharmony_ci			byte_count--;
948c2ecf20Sopenharmony_ci		}
958c2ecf20Sopenharmony_ci
968c2ecf20Sopenharmony_ci		data <<= extra_shift;
978c2ecf20Sopenharmony_ci
988c2ecf20Sopenharmony_ci		data |= (original_data & ~((~0UL) << extra_shift));
998c2ecf20Sopenharmony_ci
1008c2ecf20Sopenharmony_ci		ret = si_set_smc_sram_address(rdev, addr, limit);
1018c2ecf20Sopenharmony_ci		if (ret)
1028c2ecf20Sopenharmony_ci			goto done;
1038c2ecf20Sopenharmony_ci
1048c2ecf20Sopenharmony_ci		WREG32(SMC_IND_DATA_0, data);
1058c2ecf20Sopenharmony_ci	}
1068c2ecf20Sopenharmony_ci
1078c2ecf20Sopenharmony_cidone:
1088c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
1098c2ecf20Sopenharmony_ci
1108c2ecf20Sopenharmony_ci	return ret;
1118c2ecf20Sopenharmony_ci}
1128c2ecf20Sopenharmony_ci
1138c2ecf20Sopenharmony_civoid si_start_smc(struct radeon_device *rdev)
1148c2ecf20Sopenharmony_ci{
1158c2ecf20Sopenharmony_ci	u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
1168c2ecf20Sopenharmony_ci
1178c2ecf20Sopenharmony_ci	tmp &= ~RST_REG;
1188c2ecf20Sopenharmony_ci
1198c2ecf20Sopenharmony_ci	WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
1208c2ecf20Sopenharmony_ci}
1218c2ecf20Sopenharmony_ci
1228c2ecf20Sopenharmony_civoid si_reset_smc(struct radeon_device *rdev)
1238c2ecf20Sopenharmony_ci{
1248c2ecf20Sopenharmony_ci	u32 tmp;
1258c2ecf20Sopenharmony_ci
1268c2ecf20Sopenharmony_ci	RREG32(CB_CGTT_SCLK_CTRL);
1278c2ecf20Sopenharmony_ci	RREG32(CB_CGTT_SCLK_CTRL);
1288c2ecf20Sopenharmony_ci	RREG32(CB_CGTT_SCLK_CTRL);
1298c2ecf20Sopenharmony_ci	RREG32(CB_CGTT_SCLK_CTRL);
1308c2ecf20Sopenharmony_ci
1318c2ecf20Sopenharmony_ci	tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
1328c2ecf20Sopenharmony_ci	tmp |= RST_REG;
1338c2ecf20Sopenharmony_ci	WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
1348c2ecf20Sopenharmony_ci}
1358c2ecf20Sopenharmony_ci
1368c2ecf20Sopenharmony_ciint si_program_jump_on_start(struct radeon_device *rdev)
1378c2ecf20Sopenharmony_ci{
1388c2ecf20Sopenharmony_ci	static const u8 data[] = { 0x0E, 0x00, 0x40, 0x40 };
1398c2ecf20Sopenharmony_ci
1408c2ecf20Sopenharmony_ci	return si_copy_bytes_to_smc(rdev, 0x0, data, 4, sizeof(data)+1);
1418c2ecf20Sopenharmony_ci}
1428c2ecf20Sopenharmony_ci
1438c2ecf20Sopenharmony_civoid si_stop_smc_clock(struct radeon_device *rdev)
1448c2ecf20Sopenharmony_ci{
1458c2ecf20Sopenharmony_ci	u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
1468c2ecf20Sopenharmony_ci
1478c2ecf20Sopenharmony_ci	tmp |= CK_DISABLE;
1488c2ecf20Sopenharmony_ci
1498c2ecf20Sopenharmony_ci	WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
1508c2ecf20Sopenharmony_ci}
1518c2ecf20Sopenharmony_ci
1528c2ecf20Sopenharmony_civoid si_start_smc_clock(struct radeon_device *rdev)
1538c2ecf20Sopenharmony_ci{
1548c2ecf20Sopenharmony_ci	u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
1558c2ecf20Sopenharmony_ci
1568c2ecf20Sopenharmony_ci	tmp &= ~CK_DISABLE;
1578c2ecf20Sopenharmony_ci
1588c2ecf20Sopenharmony_ci	WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
1598c2ecf20Sopenharmony_ci}
1608c2ecf20Sopenharmony_ci
1618c2ecf20Sopenharmony_cibool si_is_smc_running(struct radeon_device *rdev)
1628c2ecf20Sopenharmony_ci{
1638c2ecf20Sopenharmony_ci	u32 rst = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
1648c2ecf20Sopenharmony_ci	u32 clk = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
1658c2ecf20Sopenharmony_ci
1668c2ecf20Sopenharmony_ci	if (!(rst & RST_REG) && !(clk & CK_DISABLE))
1678c2ecf20Sopenharmony_ci		return true;
1688c2ecf20Sopenharmony_ci
1698c2ecf20Sopenharmony_ci	return false;
1708c2ecf20Sopenharmony_ci}
1718c2ecf20Sopenharmony_ci
1728c2ecf20Sopenharmony_ciPPSMC_Result si_send_msg_to_smc(struct radeon_device *rdev, PPSMC_Msg msg)
1738c2ecf20Sopenharmony_ci{
1748c2ecf20Sopenharmony_ci	u32 tmp;
1758c2ecf20Sopenharmony_ci	int i;
1768c2ecf20Sopenharmony_ci
1778c2ecf20Sopenharmony_ci	if (!si_is_smc_running(rdev))
1788c2ecf20Sopenharmony_ci		return PPSMC_Result_Failed;
1798c2ecf20Sopenharmony_ci
1808c2ecf20Sopenharmony_ci	WREG32(SMC_MESSAGE_0, msg);
1818c2ecf20Sopenharmony_ci
1828c2ecf20Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
1838c2ecf20Sopenharmony_ci		tmp = RREG32(SMC_RESP_0);
1848c2ecf20Sopenharmony_ci		if (tmp != 0)
1858c2ecf20Sopenharmony_ci			break;
1868c2ecf20Sopenharmony_ci		udelay(1);
1878c2ecf20Sopenharmony_ci	}
1888c2ecf20Sopenharmony_ci	tmp = RREG32(SMC_RESP_0);
1898c2ecf20Sopenharmony_ci
1908c2ecf20Sopenharmony_ci	return (PPSMC_Result)tmp;
1918c2ecf20Sopenharmony_ci}
1928c2ecf20Sopenharmony_ci
1938c2ecf20Sopenharmony_ciPPSMC_Result si_wait_for_smc_inactive(struct radeon_device *rdev)
1948c2ecf20Sopenharmony_ci{
1958c2ecf20Sopenharmony_ci	u32 tmp;
1968c2ecf20Sopenharmony_ci	int i;
1978c2ecf20Sopenharmony_ci
1988c2ecf20Sopenharmony_ci	if (!si_is_smc_running(rdev))
1998c2ecf20Sopenharmony_ci		return PPSMC_Result_OK;
2008c2ecf20Sopenharmony_ci
2018c2ecf20Sopenharmony_ci	for (i = 0; i < rdev->usec_timeout; i++) {
2028c2ecf20Sopenharmony_ci		tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
2038c2ecf20Sopenharmony_ci		if ((tmp & CKEN) == 0)
2048c2ecf20Sopenharmony_ci			break;
2058c2ecf20Sopenharmony_ci		udelay(1);
2068c2ecf20Sopenharmony_ci	}
2078c2ecf20Sopenharmony_ci
2088c2ecf20Sopenharmony_ci	return PPSMC_Result_OK;
2098c2ecf20Sopenharmony_ci}
2108c2ecf20Sopenharmony_ci
2118c2ecf20Sopenharmony_ciint si_load_smc_ucode(struct radeon_device *rdev, u32 limit)
2128c2ecf20Sopenharmony_ci{
2138c2ecf20Sopenharmony_ci	unsigned long flags;
2148c2ecf20Sopenharmony_ci	u32 ucode_start_address;
2158c2ecf20Sopenharmony_ci	u32 ucode_size;
2168c2ecf20Sopenharmony_ci	const u8 *src;
2178c2ecf20Sopenharmony_ci	u32 data;
2188c2ecf20Sopenharmony_ci
2198c2ecf20Sopenharmony_ci	if (!rdev->smc_fw)
2208c2ecf20Sopenharmony_ci		return -EINVAL;
2218c2ecf20Sopenharmony_ci
2228c2ecf20Sopenharmony_ci	if (rdev->new_fw) {
2238c2ecf20Sopenharmony_ci		const struct smc_firmware_header_v1_0 *hdr =
2248c2ecf20Sopenharmony_ci			(const struct smc_firmware_header_v1_0 *)rdev->smc_fw->data;
2258c2ecf20Sopenharmony_ci
2268c2ecf20Sopenharmony_ci		radeon_ucode_print_smc_hdr(&hdr->header);
2278c2ecf20Sopenharmony_ci
2288c2ecf20Sopenharmony_ci		ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
2298c2ecf20Sopenharmony_ci		ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
2308c2ecf20Sopenharmony_ci		src = (const u8 *)
2318c2ecf20Sopenharmony_ci			(rdev->smc_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
2328c2ecf20Sopenharmony_ci	} else {
2338c2ecf20Sopenharmony_ci		switch (rdev->family) {
2348c2ecf20Sopenharmony_ci		case CHIP_TAHITI:
2358c2ecf20Sopenharmony_ci			ucode_start_address = TAHITI_SMC_UCODE_START;
2368c2ecf20Sopenharmony_ci			ucode_size = TAHITI_SMC_UCODE_SIZE;
2378c2ecf20Sopenharmony_ci			break;
2388c2ecf20Sopenharmony_ci		case CHIP_PITCAIRN:
2398c2ecf20Sopenharmony_ci			ucode_start_address = PITCAIRN_SMC_UCODE_START;
2408c2ecf20Sopenharmony_ci			ucode_size = PITCAIRN_SMC_UCODE_SIZE;
2418c2ecf20Sopenharmony_ci			break;
2428c2ecf20Sopenharmony_ci		case CHIP_VERDE:
2438c2ecf20Sopenharmony_ci			ucode_start_address = VERDE_SMC_UCODE_START;
2448c2ecf20Sopenharmony_ci			ucode_size = VERDE_SMC_UCODE_SIZE;
2458c2ecf20Sopenharmony_ci			break;
2468c2ecf20Sopenharmony_ci		case CHIP_OLAND:
2478c2ecf20Sopenharmony_ci			ucode_start_address = OLAND_SMC_UCODE_START;
2488c2ecf20Sopenharmony_ci			ucode_size = OLAND_SMC_UCODE_SIZE;
2498c2ecf20Sopenharmony_ci			break;
2508c2ecf20Sopenharmony_ci		case CHIP_HAINAN:
2518c2ecf20Sopenharmony_ci			ucode_start_address = HAINAN_SMC_UCODE_START;
2528c2ecf20Sopenharmony_ci			ucode_size = HAINAN_SMC_UCODE_SIZE;
2538c2ecf20Sopenharmony_ci			break;
2548c2ecf20Sopenharmony_ci		default:
2558c2ecf20Sopenharmony_ci			DRM_ERROR("unknown asic in smc ucode loader\n");
2568c2ecf20Sopenharmony_ci			BUG();
2578c2ecf20Sopenharmony_ci		}
2588c2ecf20Sopenharmony_ci		src = (const u8 *)rdev->smc_fw->data;
2598c2ecf20Sopenharmony_ci	}
2608c2ecf20Sopenharmony_ci
2618c2ecf20Sopenharmony_ci	if (ucode_size & 3)
2628c2ecf20Sopenharmony_ci		return -EINVAL;
2638c2ecf20Sopenharmony_ci
2648c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
2658c2ecf20Sopenharmony_ci	WREG32(SMC_IND_INDEX_0, ucode_start_address);
2668c2ecf20Sopenharmony_ci	WREG32_P(SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, ~AUTO_INCREMENT_IND_0);
2678c2ecf20Sopenharmony_ci	while (ucode_size >= 4) {
2688c2ecf20Sopenharmony_ci		/* SMC address space is BE */
2698c2ecf20Sopenharmony_ci		data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
2708c2ecf20Sopenharmony_ci
2718c2ecf20Sopenharmony_ci		WREG32(SMC_IND_DATA_0, data);
2728c2ecf20Sopenharmony_ci
2738c2ecf20Sopenharmony_ci		src += 4;
2748c2ecf20Sopenharmony_ci		ucode_size -= 4;
2758c2ecf20Sopenharmony_ci	}
2768c2ecf20Sopenharmony_ci	WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
2778c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
2788c2ecf20Sopenharmony_ci
2798c2ecf20Sopenharmony_ci	return 0;
2808c2ecf20Sopenharmony_ci}
2818c2ecf20Sopenharmony_ci
2828c2ecf20Sopenharmony_ciint si_read_smc_sram_dword(struct radeon_device *rdev, u32 smc_address,
2838c2ecf20Sopenharmony_ci			   u32 *value, u32 limit)
2848c2ecf20Sopenharmony_ci{
2858c2ecf20Sopenharmony_ci	unsigned long flags;
2868c2ecf20Sopenharmony_ci	int ret;
2878c2ecf20Sopenharmony_ci
2888c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
2898c2ecf20Sopenharmony_ci	ret = si_set_smc_sram_address(rdev, smc_address, limit);
2908c2ecf20Sopenharmony_ci	if (ret == 0)
2918c2ecf20Sopenharmony_ci		*value = RREG32(SMC_IND_DATA_0);
2928c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
2938c2ecf20Sopenharmony_ci
2948c2ecf20Sopenharmony_ci	return ret;
2958c2ecf20Sopenharmony_ci}
2968c2ecf20Sopenharmony_ci
2978c2ecf20Sopenharmony_ciint si_write_smc_sram_dword(struct radeon_device *rdev, u32 smc_address,
2988c2ecf20Sopenharmony_ci			    u32 value, u32 limit)
2998c2ecf20Sopenharmony_ci{
3008c2ecf20Sopenharmony_ci	unsigned long flags;
3018c2ecf20Sopenharmony_ci	int ret;
3028c2ecf20Sopenharmony_ci
3038c2ecf20Sopenharmony_ci	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
3048c2ecf20Sopenharmony_ci	ret = si_set_smc_sram_address(rdev, smc_address, limit);
3058c2ecf20Sopenharmony_ci	if (ret == 0)
3068c2ecf20Sopenharmony_ci		WREG32(SMC_IND_DATA_0, value);
3078c2ecf20Sopenharmony_ci	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
3088c2ecf20Sopenharmony_ci
3098c2ecf20Sopenharmony_ci	return ret;
3108c2ecf20Sopenharmony_ci}
311