|
| 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 "amdgpu.h" |
| 24 | +#include "amdgpu_i2c.h" |
| 25 | +#include "smu_v11_0_i2c.h" |
| 26 | +#include "atom.h" |
| 27 | + |
| 28 | +#define I2C_PRODUCT_INFO_ADDR 0xAC |
| 29 | +#define I2C_PRODUCT_INFO_ADDR_SIZE 0x2 |
| 30 | +#define I2C_PRODUCT_INFO_OFFSET 0xC0 |
| 31 | + |
| 32 | +int amdgpu_fru_read_eeprom(struct amdgpu_device *adev, uint32_t addrptr, |
| 33 | + unsigned char *buff) |
| 34 | +{ |
| 35 | + int ret, size; |
| 36 | + struct i2c_msg msg = { |
| 37 | + .addr = I2C_PRODUCT_INFO_ADDR, |
| 38 | + .flags = I2C_M_RD, |
| 39 | + .buf = buff, |
| 40 | + }; |
| 41 | + buff[0] = 0; |
| 42 | + buff[1] = addrptr; |
| 43 | + msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + 1; |
| 44 | + ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1); |
| 45 | + |
| 46 | + if (ret < 1) { |
| 47 | + DRM_WARN("FRU: Failed to get size field"); |
| 48 | + return ret; |
| 49 | + } |
| 50 | + |
| 51 | + /* The size returned by the i2c requires subtraction of 0xC0 since the |
| 52 | + * size apparently always reports as 0xC0+actual size. |
| 53 | + */ |
| 54 | + size = buff[2] - I2C_PRODUCT_INFO_OFFSET; |
| 55 | + /* Add 1 since address field was 1 byte */ |
| 56 | + buff[1] = addrptr + 1; |
| 57 | + |
| 58 | + msg.len = I2C_PRODUCT_INFO_ADDR_SIZE + size; |
| 59 | + ret = i2c_transfer(&adev->pm.smu_i2c, &msg, 1); |
| 60 | + |
| 61 | + if (ret < 1) { |
| 62 | + DRM_WARN("FRU: Failed to get data field"); |
| 63 | + return ret; |
| 64 | + } |
| 65 | + |
| 66 | + return size; |
| 67 | +} |
| 68 | + |
| 69 | +int amdgpu_fru_get_product_info(struct amdgpu_device *adev) |
| 70 | +{ |
| 71 | + unsigned char buff[32]; |
| 72 | + int addrptr = 0, size = 0; |
| 73 | + |
| 74 | + /* If algo exists, it means that the i2c_adapter's initialized */ |
| 75 | + if (!adev->pm.smu_i2c.algo) { |
| 76 | + DRM_WARN("Cannot access FRU, EEPROM accessor not initialized"); |
| 77 | + return 0; |
| 78 | + } |
| 79 | + |
| 80 | + /* There's a lot of repetition here. This is due to the FRU having |
| 81 | + * variable-length fields. To get the information, we have to find the |
| 82 | + * size of each field, and then keep reading along and reading along |
| 83 | + * until we get all of the data that we want. We use addrptr to track |
| 84 | + * the address as we go |
| 85 | + */ |
| 86 | + |
| 87 | + /* The first fields are all of size 1-byte, from 0-7 are offsets that |
| 88 | + * contain information that isn't useful to us. |
| 89 | + * Bytes 8-a are all 1-byte and refer to the size of the entire struct, |
| 90 | + * and the language field, so just start from 0xb, manufacturer size |
| 91 | + */ |
| 92 | + addrptr = 0xb; |
| 93 | + size = amdgpu_fru_read_eeprom(adev, addrptr, buff); |
| 94 | + if (size < 1) { |
| 95 | + DRM_ERROR("Failed to read FRU Manufacturer, ret:%d", size); |
| 96 | + return size; |
| 97 | + } |
| 98 | + |
| 99 | + /* Increment the addrptr by the size of the field, and 1 due to the |
| 100 | + * size field being 1 byte. This pattern continues below. |
| 101 | + */ |
| 102 | + addrptr += size + 1; |
| 103 | + size = amdgpu_fru_read_eeprom(adev, addrptr, buff); |
| 104 | + if (size < 1) { |
| 105 | + DRM_ERROR("Failed to read FRU product name, ret:%d", size); |
| 106 | + return size; |
| 107 | + } |
| 108 | + |
| 109 | + /* Start at 2 due to buff using fields 0 and 1 for the address */ |
| 110 | + memcpy(adev->product_name, &buff[2], size); |
| 111 | + adev->product_name[size] = '\0'; |
| 112 | + |
| 113 | + addrptr += size + 1; |
| 114 | + size = amdgpu_fru_read_eeprom(adev, addrptr, buff); |
| 115 | + if (size < 1) { |
| 116 | + DRM_ERROR("Failed to read FRU product number, ret:%d", size); |
| 117 | + return size; |
| 118 | + } |
| 119 | + |
| 120 | + memcpy(adev->product_number, &buff[2], size); |
| 121 | + adev->product_number[size] = '\0'; |
| 122 | + |
| 123 | + addrptr += size + 1; |
| 124 | + size = amdgpu_fru_read_eeprom(adev, addrptr, buff); |
| 125 | + |
| 126 | + if (size < 1) { |
| 127 | + DRM_ERROR("Failed to read FRU product version, ret:%d", size); |
| 128 | + return size; |
| 129 | + } |
| 130 | + |
| 131 | + addrptr += size + 1; |
| 132 | + size = amdgpu_fru_read_eeprom(adev, addrptr, buff); |
| 133 | + |
| 134 | + if (size < 1) { |
| 135 | + DRM_ERROR("Failed to read FRU serial number, ret:%d", size); |
| 136 | + return size; |
| 137 | + } |
| 138 | + |
| 139 | + memcpy(adev->serial, &buff[2], size); |
| 140 | + adev->serial[size] = '\0'; |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
0 commit comments