xref: /openbmc/linux/drivers/gpu/drm/amd/amdgpu/amdgpu_fru_eeprom.c (revision 0760aad038b5a032c31ea124feed63d88627d2f1)
1 /*
2  * Copyright 2019 Advanced Micro Devices, Inc.
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in
12  * all copies or substantial portions of the Software.
13  *
14  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
17  * THE COPYRIGHT HOLDER(S) OR AUTHOR(S) BE LIABLE FOR ANY CLAIM, DAMAGES OR
18  * OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
19  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
20  * OTHER DEALINGS IN THE SOFTWARE.
21  *
22  */
23 #include <linux/pci.h>
24 
25 #include "amdgpu.h"
26 #include "amdgpu_i2c.h"
27 #include "smu_v11_0_i2c.h"
28 #include "atom.h"
29 #include "amdgpu_fru_eeprom.h"
30 
31 #define I2C_PRODUCT_INFO_ADDR		0xAC
32 #define I2C_PRODUCT_INFO_ADDR_SIZE	0x2
33 #define I2C_PRODUCT_INFO_OFFSET		0xC0
34 
35 static bool is_fru_eeprom_supported(struct amdgpu_device *adev)
36 {
37 	/* TODO: Gaming SKUs don't have the FRU EEPROM.
38 	 * Use this hack to address hangs on modprobe on gaming SKUs
39 	 * until a proper solution can be implemented by only supporting
40 	 * the explicit chip IDs for VG20 Server cards
41 	 *
42 	 * TODO: Add list of supported Arcturus DIDs once confirmed
43 	 */
44 	if ((adev->asic_type == CHIP_VEGA20 && adev->pdev->device == 0x66a0) ||
45 	    (adev->asic_type == CHIP_VEGA20 && adev->pdev->device == 0x66a1) ||
46 	    (adev->asic_type == CHIP_VEGA20 && adev->pdev->device == 0x66a4))
47 		return true;
48 	return false;
49 }
50 
51 static int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr,
52 			   unsigned char *buff)
53 {
54 	int ret, size;
55 	struct i2c_msg msg = {
56 			.addr   = I2C_PRODUCT_INFO_ADDR,
57 			.flags  = I2C_M_RD,
58 			.buf    = buff,
59 	};
60 	buff[0] = 0;
61 	buff[1] = addrptr;
62 	msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + 1;
63 	ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
64 
65 	if (ret < 1) {
66 		DRM_WARN("FRU: Failed to get size field");
67 		return ret;
68 	}
69 
70 	/* The size returned by the i2c requires subtraction of 0xC0 since the
71 	 * size apparently always reports as 0xC0+actual size.
72 	 */
73 	size = buff[2] - I2C_PRODUCT_INFO_OFFSET;
74 	/* Add 1 since address field was 1 byte */
75 	buff[1] = addrptr + 1;
76 
77 	msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + size;
78 	ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1);
79 
80 	if (ret < 1) {
81 		DRM_WARN("FRU: Failed to get data field");
82 		return ret;
83 	}
84 
85 	return size;
86 }
87 
88 int amdgpu_fru_get_product_info(struct amdgpu_device *adev)
89 {
90 	unsigned char buff[34];
91 	int addrptr = 0, size = 0;
92 
93 	if (!is_fru_eeprom_supported(adev))
94 		return 0;
95 
96 	/* If algo exists, it means that the i2c_adapter's initialized */
97 	if (!adev->pm.smu_i2c.algo) {
98 		DRM_WARN("Cannot access FRU, EEPROM accessor not initialized");
99 		return 0;
100 	}
101 
102 	/* There's a lot of repetition here. This is due to the FRU having
103 	 * variable-length fields. To get the information, we have to find the
104 	 * size of each field, and then keep reading along and reading along
105 	 * until we get all of the data that we want. We use addrptr to track
106 	 * the address as we go
107 	 */
108 
109 	/* The first fields are all of size 1-byte, from 0-7 are offsets that
110 	 * contain information that isn't useful to us.
111 	 * Bytes 8-a are all 1-byte and refer to the size of the entire struct,
112 	 * and the language field, so just start from 0xb, manufacturer size
113 	 */
114 	addrptr = 0xb;
115 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
116 	if (size < 1) {
117 		DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size);
118 		return size;
119 	}
120 
121 	/* Increment the addrptr by the size of the field, and 1 due to the
122 	 * size field being 1 byte. This pattern continues below.
123 	 */
124 	addrptr += size + 1;
125 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
126 	if (size < 1) {
127 		DRM_ERROR("Failed to read FRU product name, ret:%d", size);
128 		return size;
129 	}
130 
131 	/* Product name should only be 32 characters. Any more,
132 	 * and something could be wrong. Cap it at 32 to be safe
133 	 */
134 	if (size > 32) {
135 		DRM_WARN("FRU Product Number is larger than 32 characters. This is likely a mistake");
136 		size = 32;
137 	}
138 	/* Start at 2 due to buff using fields 0 and 1 for the address */
139 	memcpy(adev->product_name, &buff[2], size);
140 	adev->product_name[size] = '\0';
141 
142 	addrptr += size + 1;
143 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
144 	if (size < 1) {
145 		DRM_ERROR("Failed to read FRU product number, ret:%d", size);
146 		return size;
147 	}
148 
149 	/* Product number should only be 16 characters. Any more,
150 	 * and something could be wrong. Cap it at 16 to be safe
151 	 */
152 	if (size > 16) {
153 		DRM_WARN("FRU Product Number is larger than 16 characters. This is likely a mistake");
154 		size = 16;
155 	}
156 	memcpy(adev->product_number, &buff[2], size);
157 	adev->product_number[size] = '\0';
158 
159 	addrptr += size + 1;
160 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
161 
162 	if (size < 1) {
163 		DRM_ERROR("Failed to read FRU product version, ret:%d", size);
164 		return size;
165 	}
166 
167 	addrptr += size + 1;
168 	size = amdgpu_fru_read_eeprom(adev, addrptr, buff);
169 
170 	if (size < 1) {
171 		DRM_ERROR("Failed to read FRU serial number, ret:%d", size);
172 		return size;
173 	}
174 
175 	/* Serial number should only be 16 characters. Any more,
176 	 * and something could be wrong. Cap it at 16 to be safe
177 	 */
178 	if (size > 16) {
179 		DRM_WARN("FRU Serial Number is larger than 16 characters. This is likely a mistake");
180 		size = 16;
181 	}
182 	memcpy(adev->serial, &buff[2], size);
183 	adev->serial[size] = '\0';
184 
185 	return 0;
186 }
187