Home | History | Annotate | Line # | Download | only in radeon
      1  1.1  riastrad /*	$NetBSD: radeon_ci_smc.c,v 1.2 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.1  riastrad __KERNEL_RCSID(0, "$NetBSD: radeon_ci_smc.c,v 1.2 2021/12/18 23:45:43 riastradh Exp $");
     29  1.1  riastrad 
     30  1.1  riastrad #include <linux/firmware.h>
     31  1.2  riastrad 
     32  1.1  riastrad #include "radeon.h"
     33  1.1  riastrad #include "cikd.h"
     34  1.1  riastrad #include "ppsmc.h"
     35  1.1  riastrad #include "radeon_ucode.h"
     36  1.1  riastrad #include "ci_dpm.h"
     37  1.1  riastrad 
     38  1.1  riastrad static int ci_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 ci_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 	u32 data, original_data;
     58  1.1  riastrad 	u32 addr;
     59  1.1  riastrad 	u32 extra_shift;
     60  1.1  riastrad 	int ret = 0;
     61  1.1  riastrad 
     62  1.1  riastrad 	if (smc_start_address & 3)
     63  1.1  riastrad 		return -EINVAL;
     64  1.1  riastrad 	if ((smc_start_address + byte_count) > limit)
     65  1.1  riastrad 		return -EINVAL;
     66  1.1  riastrad 
     67  1.1  riastrad 	addr = smc_start_address;
     68  1.1  riastrad 
     69  1.1  riastrad 	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
     70  1.1  riastrad 	while (byte_count >= 4) {
     71  1.1  riastrad 		/* SMC address space is BE */
     72  1.1  riastrad 		data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
     73  1.1  riastrad 
     74  1.1  riastrad 		ret = ci_set_smc_sram_address(rdev, addr, limit);
     75  1.1  riastrad 		if (ret)
     76  1.1  riastrad 			goto done;
     77  1.1  riastrad 
     78  1.1  riastrad 		WREG32(SMC_IND_DATA_0, data);
     79  1.1  riastrad 
     80  1.1  riastrad 		src += 4;
     81  1.1  riastrad 		byte_count -= 4;
     82  1.1  riastrad 		addr += 4;
     83  1.1  riastrad 	}
     84  1.1  riastrad 
     85  1.1  riastrad 	/* RMW for the final bytes */
     86  1.1  riastrad 	if (byte_count > 0) {
     87  1.1  riastrad 		data = 0;
     88  1.1  riastrad 
     89  1.1  riastrad 		ret = ci_set_smc_sram_address(rdev, addr, limit);
     90  1.1  riastrad 		if (ret)
     91  1.1  riastrad 			goto done;
     92  1.1  riastrad 
     93  1.1  riastrad 		original_data = RREG32(SMC_IND_DATA_0);
     94  1.1  riastrad 
     95  1.1  riastrad 		extra_shift = 8 * (4 - byte_count);
     96  1.1  riastrad 
     97  1.1  riastrad 		while (byte_count > 0) {
     98  1.1  riastrad 			data = (data << 8) + *src++;
     99  1.1  riastrad 			byte_count--;
    100  1.1  riastrad 		}
    101  1.1  riastrad 
    102  1.1  riastrad 		data <<= extra_shift;
    103  1.1  riastrad 
    104  1.1  riastrad 		data |= (original_data & ~((~0UL) << extra_shift));
    105  1.1  riastrad 
    106  1.1  riastrad 		ret = ci_set_smc_sram_address(rdev, addr, limit);
    107  1.1  riastrad 		if (ret)
    108  1.1  riastrad 			goto done;
    109  1.1  riastrad 
    110  1.1  riastrad 		WREG32(SMC_IND_DATA_0, data);
    111  1.1  riastrad 	}
    112  1.1  riastrad 
    113  1.1  riastrad done:
    114  1.1  riastrad 	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
    115  1.1  riastrad 
    116  1.1  riastrad 	return ret;
    117  1.1  riastrad }
    118  1.1  riastrad 
    119  1.1  riastrad void ci_start_smc(struct radeon_device *rdev)
    120  1.1  riastrad {
    121  1.1  riastrad 	u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
    122  1.1  riastrad 
    123  1.1  riastrad 	tmp &= ~RST_REG;
    124  1.1  riastrad 	WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
    125  1.1  riastrad }
    126  1.1  riastrad 
    127  1.1  riastrad void ci_reset_smc(struct radeon_device *rdev)
    128  1.1  riastrad {
    129  1.1  riastrad 	u32 tmp = RREG32_SMC(SMC_SYSCON_RESET_CNTL);
    130  1.1  riastrad 
    131  1.1  riastrad 	tmp |= RST_REG;
    132  1.1  riastrad 	WREG32_SMC(SMC_SYSCON_RESET_CNTL, tmp);
    133  1.1  riastrad }
    134  1.1  riastrad 
    135  1.1  riastrad int ci_program_jump_on_start(struct radeon_device *rdev)
    136  1.1  riastrad {
    137  1.1  riastrad 	static const u8 data[] = { 0xE0, 0x00, 0x80, 0x40 };
    138  1.1  riastrad 
    139  1.1  riastrad 	return ci_copy_bytes_to_smc(rdev, 0x0, data, 4, sizeof(data)+1);
    140  1.1  riastrad }
    141  1.1  riastrad 
    142  1.1  riastrad void ci_stop_smc_clock(struct radeon_device *rdev)
    143  1.1  riastrad {
    144  1.1  riastrad 	u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
    145  1.1  riastrad 
    146  1.1  riastrad 	tmp |= CK_DISABLE;
    147  1.1  riastrad 
    148  1.1  riastrad 	WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
    149  1.1  riastrad }
    150  1.1  riastrad 
    151  1.1  riastrad void ci_start_smc_clock(struct radeon_device *rdev)
    152  1.1  riastrad {
    153  1.1  riastrad 	u32 tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
    154  1.1  riastrad 
    155  1.1  riastrad 	tmp &= ~CK_DISABLE;
    156  1.1  riastrad 
    157  1.1  riastrad 	WREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0, tmp);
    158  1.1  riastrad }
    159  1.1  riastrad 
    160  1.1  riastrad bool ci_is_smc_running(struct radeon_device *rdev)
    161  1.1  riastrad {
    162  1.1  riastrad 	u32 clk = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
    163  1.1  riastrad 	u32 pc_c = RREG32_SMC(SMC_PC_C);
    164  1.1  riastrad 
    165  1.1  riastrad 	if (!(clk & CK_DISABLE) && (0x20100 <= pc_c))
    166  1.1  riastrad 		return true;
    167  1.1  riastrad 
    168  1.1  riastrad 	return false;
    169  1.1  riastrad }
    170  1.1  riastrad 
    171  1.1  riastrad #if 0
    172  1.1  riastrad PPSMC_Result ci_wait_for_smc_inactive(struct radeon_device *rdev)
    173  1.1  riastrad {
    174  1.1  riastrad 	u32 tmp;
    175  1.1  riastrad 	int i;
    176  1.1  riastrad 
    177  1.1  riastrad 	if (!ci_is_smc_running(rdev))
    178  1.1  riastrad 		return PPSMC_Result_OK;
    179  1.1  riastrad 
    180  1.1  riastrad 	for (i = 0; i < rdev->usec_timeout; i++) {
    181  1.2  riastrad 		tmp = RREG32_SMC(SMC_SYSCON_CLOCK_CNTL_0);
    182  1.2  riastrad 		if ((tmp & CKEN) == 0)
    183  1.1  riastrad 			break;
    184  1.2  riastrad 		udelay(1);
    185  1.2  riastrad 	}
    186  1.1  riastrad 
    187  1.1  riastrad 	return PPSMC_Result_OK;
    188  1.1  riastrad }
    189  1.1  riastrad #endif
    190  1.1  riastrad 
    191  1.1  riastrad int ci_load_smc_ucode(struct radeon_device *rdev, u32 limit)
    192  1.1  riastrad {
    193  1.1  riastrad 	unsigned long flags;
    194  1.1  riastrad 	u32 ucode_start_address;
    195  1.1  riastrad 	u32 ucode_size;
    196  1.1  riastrad 	const u8 *src;
    197  1.1  riastrad 	u32 data;
    198  1.1  riastrad 
    199  1.1  riastrad 	if (!rdev->smc_fw)
    200  1.1  riastrad 		return -EINVAL;
    201  1.1  riastrad 
    202  1.1  riastrad 	if (rdev->new_fw) {
    203  1.1  riastrad 		const struct smc_firmware_header_v1_0 *hdr =
    204  1.1  riastrad 			(const struct smc_firmware_header_v1_0 *)rdev->smc_fw->data;
    205  1.1  riastrad 
    206  1.1  riastrad 		radeon_ucode_print_smc_hdr(&hdr->header);
    207  1.1  riastrad 
    208  1.1  riastrad 		ucode_start_address = le32_to_cpu(hdr->ucode_start_addr);
    209  1.1  riastrad 		ucode_size = le32_to_cpu(hdr->header.ucode_size_bytes);
    210  1.1  riastrad 		src = (const u8 *)
    211  1.1  riastrad 			(rdev->smc_fw->data + le32_to_cpu(hdr->header.ucode_array_offset_bytes));
    212  1.1  riastrad 	} else {
    213  1.1  riastrad 		switch (rdev->family) {
    214  1.1  riastrad 		case CHIP_BONAIRE:
    215  1.1  riastrad 			ucode_start_address = BONAIRE_SMC_UCODE_START;
    216  1.1  riastrad 			ucode_size = BONAIRE_SMC_UCODE_SIZE;
    217  1.1  riastrad 			break;
    218  1.1  riastrad 		case CHIP_HAWAII:
    219  1.1  riastrad 			ucode_start_address = HAWAII_SMC_UCODE_START;
    220  1.1  riastrad 			ucode_size = HAWAII_SMC_UCODE_SIZE;
    221  1.1  riastrad 			break;
    222  1.1  riastrad 		default:
    223  1.1  riastrad 			DRM_ERROR("unknown asic in smc ucode loader\n");
    224  1.1  riastrad 			BUG();
    225  1.1  riastrad 		}
    226  1.1  riastrad 
    227  1.1  riastrad 		src = (const u8 *)rdev->smc_fw->data;
    228  1.1  riastrad 	}
    229  1.1  riastrad 
    230  1.1  riastrad 	if (ucode_size & 3)
    231  1.1  riastrad 		return -EINVAL;
    232  1.1  riastrad 
    233  1.1  riastrad 	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
    234  1.1  riastrad 	WREG32(SMC_IND_INDEX_0, ucode_start_address);
    235  1.1  riastrad 	WREG32_P(SMC_IND_ACCESS_CNTL, AUTO_INCREMENT_IND_0, ~AUTO_INCREMENT_IND_0);
    236  1.1  riastrad 	while (ucode_size >= 4) {
    237  1.1  riastrad 		/* SMC address space is BE */
    238  1.1  riastrad 		data = (src[0] << 24) | (src[1] << 16) | (src[2] << 8) | src[3];
    239  1.1  riastrad 
    240  1.1  riastrad 		WREG32(SMC_IND_DATA_0, data);
    241  1.1  riastrad 
    242  1.1  riastrad 		src += 4;
    243  1.1  riastrad 		ucode_size -= 4;
    244  1.1  riastrad 	}
    245  1.1  riastrad 	WREG32_P(SMC_IND_ACCESS_CNTL, 0, ~AUTO_INCREMENT_IND_0);
    246  1.1  riastrad 	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
    247  1.1  riastrad 
    248  1.1  riastrad 	return 0;
    249  1.1  riastrad }
    250  1.1  riastrad 
    251  1.1  riastrad int ci_read_smc_sram_dword(struct radeon_device *rdev,
    252  1.1  riastrad 			   u32 smc_address, u32 *value, u32 limit)
    253  1.1  riastrad {
    254  1.1  riastrad 	unsigned long flags;
    255  1.1  riastrad 	int ret;
    256  1.1  riastrad 
    257  1.1  riastrad 	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
    258  1.1  riastrad 	ret = ci_set_smc_sram_address(rdev, smc_address, limit);
    259  1.1  riastrad 	if (ret == 0)
    260  1.1  riastrad 		*value = RREG32(SMC_IND_DATA_0);
    261  1.1  riastrad 	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
    262  1.1  riastrad 
    263  1.1  riastrad 	return ret;
    264  1.1  riastrad }
    265  1.1  riastrad 
    266  1.1  riastrad int ci_write_smc_sram_dword(struct radeon_device *rdev,
    267  1.1  riastrad 			    u32 smc_address, u32 value, u32 limit)
    268  1.1  riastrad {
    269  1.1  riastrad 	unsigned long flags;
    270  1.1  riastrad 	int ret;
    271  1.1  riastrad 
    272  1.1  riastrad 	spin_lock_irqsave(&rdev->smc_idx_lock, flags);
    273  1.1  riastrad 	ret = ci_set_smc_sram_address(rdev, smc_address, limit);
    274  1.1  riastrad 	if (ret == 0)
    275  1.1  riastrad 		WREG32(SMC_IND_DATA_0, value);
    276  1.1  riastrad 	spin_unlock_irqrestore(&rdev->smc_idx_lock, flags);
    277  1.1  riastrad 
    278  1.1  riastrad 	return ret;
    279  1.1  riastrad }
    280