2 * Copyright 2019 Advanced Micro Devices, Inc.
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:
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
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.
24 #include "smuio/smuio_11_0_0_offset.h"
25 #include "smuio/smuio_11_0_0_sh_mask.h"
27 #include "smu_v11_0_i2c.h"
29 #include "soc15_common.h"
30 #include <drm/drm_fixed.h>
31 #include <drm/drm_drv.h>
32 #include "amdgpu_amdkfd.h"
33 #include <linux/i2c.h>
34 #include <linux/pci.h>
35 #include "amdgpu_ras.h"
39 #define I2C_NAK_7B_ADDR_NOACK 1
40 #define I2C_NAK_TXDATA_NOACK 2
42 #define I2C_SW_TIMEOUT 8
43 #define I2C_ABORT 0x10
45 /* I2C transaction flags */
49 #define to_amdgpu_device(x) (container_of(x, struct amdgpu_ras, eeprom_control.eeprom_accessor))->adev
50 #define to_eeprom_control(x) container_of(x, struct amdgpu_ras_eeprom_control, eeprom_accessor)
52 static void smu_v11_0_i2c_set_clock_gating(struct i2c_adapter
*control
, bool en
)
54 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
55 uint32_t reg
= RREG32_SOC15(SMUIO
, 0, mmSMUIO_PWRMGT
);
57 reg
= REG_SET_FIELD(reg
, SMUIO_PWRMGT
, i2c_clk_gate_en
, en
? 1 : 0);
58 WREG32_SOC15(SMUIO
, 0, mmSMUIO_PWRMGT
, reg
);
62 static void smu_v11_0_i2c_enable(struct i2c_adapter
*control
, bool enable
)
64 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
66 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_ENABLE
, enable
? 1 : 0);
69 static void smu_v11_0_i2c_clear_status(struct i2c_adapter
*control
)
71 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
74 RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_CLR_INTR
);
76 } /* while (reg_CKSVII2C_ic_clr_intr == 0) */
79 static void smu_v11_0_i2c_configure(struct i2c_adapter
*control
)
81 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
84 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_CON
, IC_SLAVE_DISABLE
, 1);
85 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_CON
, IC_RESTART_EN
, 1);
86 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_CON
, IC_10BITADDR_MASTER
, 0);
87 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_CON
, IC_10BITADDR_SLAVE
, 0);
89 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_CON
, IC_MAX_SPEED_MODE
, 2);
90 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_CON
, IC_MASTER_MODE
, 1);
92 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_CON
, reg
);
95 static void smu_v11_0_i2c_set_clock(struct i2c_adapter
*control
)
97 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
100 * Standard mode speed, These values are taken from SMUIO MAS,
101 * but are different from what is given is
102 * Synopsys spec. The values here are based on assumption
103 * that refclock is 100MHz
105 * Configuration for standard mode; Speed = 100kbps
106 * Scale linearly, for now only support standard speed clock
107 * This will work only with 100M ref clock
109 * TBD:Change the calculation to take into account ref clock values also.
112 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_FS_SPKLEN
, 2);
113 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_SS_SCL_HCNT
, 120);
114 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_SS_SCL_LCNT
, 130);
115 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_SDA_HOLD
, 20);
118 static void smu_v11_0_i2c_set_address(struct i2c_adapter
*control
, uint8_t address
)
120 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
122 /* Convert fromr 8-bit to 7-bit address */
124 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_TAR
, (address
& 0xFF));
127 static uint32_t smu_v11_0_i2c_poll_tx_status(struct i2c_adapter
*control
)
129 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
130 uint32_t ret
= I2C_OK
;
131 uint32_t reg
, reg_c_tx_abrt_source
;
133 /*Check if transmission is completed */
134 unsigned long timeout_counter
= jiffies
+ msecs_to_jiffies(20);
137 if (time_after(jiffies
, timeout_counter
)) {
138 ret
|= I2C_SW_TIMEOUT
;
142 reg
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_STATUS
);
144 } while (REG_GET_FIELD(reg
, CKSVII2C_IC_STATUS
, TFE
) == 0);
149 /* This only checks if NAK is received and transaction got aborted */
150 reg
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_INTR_STAT
);
152 if (REG_GET_FIELD(reg
, CKSVII2C_IC_INTR_STAT
, R_TX_ABRT
) == 1) {
153 reg_c_tx_abrt_source
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_TX_ABRT_SOURCE
);
154 DRM_INFO("TX was terminated, IC_TX_ABRT_SOURCE val is:%x", reg_c_tx_abrt_source
);
156 /* Check for stop due to NACK */
157 if (REG_GET_FIELD(reg_c_tx_abrt_source
,
158 CKSVII2C_IC_TX_ABRT_SOURCE
,
159 ABRT_TXDATA_NOACK
) == 1) {
161 ret
|= I2C_NAK_TXDATA_NOACK
;
163 } else if (REG_GET_FIELD(reg_c_tx_abrt_source
,
164 CKSVII2C_IC_TX_ABRT_SOURCE
,
165 ABRT_7B_ADDR_NOACK
) == 1) {
167 ret
|= I2C_NAK_7B_ADDR_NOACK
;
172 smu_v11_0_i2c_clear_status(control
);
178 static uint32_t smu_v11_0_i2c_poll_rx_status(struct i2c_adapter
*control
)
180 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
181 uint32_t ret
= I2C_OK
;
182 uint32_t reg_ic_status
, reg_c_tx_abrt_source
;
184 reg_c_tx_abrt_source
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_TX_ABRT_SOURCE
);
186 /* If slave is not present */
187 if (REG_GET_FIELD(reg_c_tx_abrt_source
,
188 CKSVII2C_IC_TX_ABRT_SOURCE
,
189 ABRT_7B_ADDR_NOACK
) == 1) {
190 ret
|= I2C_NAK_7B_ADDR_NOACK
;
192 smu_v11_0_i2c_clear_status(control
);
193 } else { /* wait till some data is there in RXFIFO */
194 /* Poll for some byte in RXFIFO */
195 unsigned long timeout_counter
= jiffies
+ msecs_to_jiffies(20);
198 if (time_after(jiffies
, timeout_counter
)) {
199 ret
|= I2C_SW_TIMEOUT
;
203 reg_ic_status
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_STATUS
);
205 } while (REG_GET_FIELD(reg_ic_status
, CKSVII2C_IC_STATUS
, RFNE
) == 0);
215 * smu_v11_0_i2c_transmit - Send a block of data over the I2C bus to a slave device.
217 * @address: The I2C address of the slave device.
218 * @data: The data to transmit over the bus.
219 * @numbytes: The amount of data to transmit.
220 * @i2c_flag: Flags for transmission
222 * Returns 0 on success or error.
224 static uint32_t smu_v11_0_i2c_transmit(struct i2c_adapter
*control
,
225 uint8_t address
, uint8_t *data
,
226 uint32_t numbytes
, uint32_t i2c_flag
)
228 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
229 uint32_t bytes_sent
, reg
, ret
= 0;
230 unsigned long timeout_counter
;
234 DRM_DEBUG_DRIVER("I2C_Transmit(), address = %x, bytes = %d , data: ",
235 (uint16_t)address
, numbytes
);
237 if (drm_debug_enabled(DRM_UT_DRIVER
)) {
238 print_hex_dump(KERN_INFO
, "data: ", DUMP_PREFIX_NONE
,
239 16, 1, data
, numbytes
, false);
242 /* Set the I2C slave address */
243 smu_v11_0_i2c_set_address(control
, address
);
245 smu_v11_0_i2c_enable(control
, true);
247 /* Clear status bits */
248 smu_v11_0_i2c_clear_status(control
);
251 timeout_counter
= jiffies
+ msecs_to_jiffies(20);
253 while (numbytes
> 0) {
254 reg
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_STATUS
);
255 if (REG_GET_FIELD(reg
, CKSVII2C_IC_STATUS
, TFNF
)) {
259 * Prepare transaction, no need to set RESTART. I2C engine will send
260 * START as soon as it sees data in TXFIFO
263 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, RESTART
,
264 (i2c_flag
& I2C_RESTART
) ? 1 : 0);
265 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, DAT
, data
[bytes_sent
]);
267 /* determine if we need to send STOP bit or not */
269 /* Final transaction, so send stop unless I2C_NO_STOP */
270 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, STOP
,
271 (i2c_flag
& I2C_NO_STOP
) ? 0 : 1);
273 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, CMD
, 0);
274 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_DATA_CMD
, reg
);
276 /* Record that the bytes were transmitted */
280 reg
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_STATUS
);
282 } while (numbytes
&& REG_GET_FIELD(reg
, CKSVII2C_IC_STATUS
, TFNF
));
286 * We waited too long for the transmission FIFO to become not-full.
287 * Exit the loop with error.
289 if (time_after(jiffies
, timeout_counter
)) {
290 ret
|= I2C_SW_TIMEOUT
;
295 ret
= smu_v11_0_i2c_poll_tx_status(control
);
298 /* Any error, no point in proceeding */
300 if (ret
& I2C_SW_TIMEOUT
)
301 DRM_ERROR("TIMEOUT ERROR !!!");
303 if (ret
& I2C_NAK_7B_ADDR_NOACK
)
304 DRM_ERROR("Received I2C_NAK_7B_ADDR_NOACK !!!");
307 if (ret
& I2C_NAK_TXDATA_NOACK
)
308 DRM_ERROR("Received I2C_NAK_TXDATA_NOACK !!!");
316 * smu_v11_0_i2c_receive - Receive a block of data over the I2C bus from a slave device.
318 * @address: The I2C address of the slave device.
319 * @numbytes: The amount of data to transmit.
320 * @i2c_flag: Flags for transmission
322 * Returns 0 on success or error.
324 static uint32_t smu_v11_0_i2c_receive(struct i2c_adapter
*control
,
325 uint8_t address
, uint8_t *data
,
326 uint32_t numbytes
, uint8_t i2c_flag
)
328 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
329 uint32_t bytes_received
, ret
= I2C_OK
;
333 /* Set the I2C slave address */
334 smu_v11_0_i2c_set_address(control
, address
);
337 smu_v11_0_i2c_enable(control
, true);
339 while (numbytes
> 0) {
342 smu_v11_0_i2c_clear_status(control
);
345 /* Prepare transaction */
347 /* Each time we disable I2C, so this is not a restart */
348 if (bytes_received
== 0)
349 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, RESTART
,
350 (i2c_flag
& I2C_RESTART
) ? 1 : 0);
352 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, DAT
, 0);
354 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, CMD
, 1);
356 /* Transmitting last byte */
358 /* Final transaction, so send stop if requested */
359 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, STOP
,
360 (i2c_flag
& I2C_NO_STOP
) ? 0 : 1);
362 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_DATA_CMD
, reg
);
364 ret
= smu_v11_0_i2c_poll_rx_status(control
);
366 /* Any error, no point in proceeding */
368 if (ret
& I2C_SW_TIMEOUT
)
369 DRM_ERROR("TIMEOUT ERROR !!!");
371 if (ret
& I2C_NAK_7B_ADDR_NOACK
)
372 DRM_ERROR("Received I2C_NAK_7B_ADDR_NOACK !!!");
374 if (ret
& I2C_NAK_TXDATA_NOACK
)
375 DRM_ERROR("Received I2C_NAK_TXDATA_NOACK !!!");
380 reg
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_DATA_CMD
);
381 data
[bytes_received
] = REG_GET_FIELD(reg
, CKSVII2C_IC_DATA_CMD
, DAT
);
383 /* Record that the bytes were received */
388 DRM_DEBUG_DRIVER("I2C_Receive(), address = %x, bytes = %d, data :",
389 (uint16_t)address
, bytes_received
);
391 if (drm_debug_enabled(DRM_UT_DRIVER
)) {
392 print_hex_dump(KERN_INFO
, "data: ", DUMP_PREFIX_NONE
,
393 16, 1, data
, bytes_received
, false);
399 static void smu_v11_0_i2c_abort(struct i2c_adapter
*control
)
401 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
404 /* Enable I2C engine; */
405 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_ENABLE
, ENABLE
, 1);
406 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_ENABLE
, reg
);
408 /* Abort previous transaction */
409 reg
= REG_SET_FIELD(reg
, CKSVII2C_IC_ENABLE
, ABORT
, 1);
410 WREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_ENABLE
, reg
);
412 DRM_DEBUG_DRIVER("I2C_Abort() Done.");
416 static bool smu_v11_0_i2c_activity_done(struct i2c_adapter
*control
)
418 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
420 const uint32_t IDLE_TIMEOUT
= 1024;
421 uint32_t timeout_count
= 0;
422 uint32_t reg_ic_enable
, reg_ic_enable_status
, reg_ic_clr_activity
;
424 reg_ic_enable_status
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_ENABLE_STATUS
);
425 reg_ic_enable
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_ENABLE
);
428 if ((REG_GET_FIELD(reg_ic_enable
, CKSVII2C_IC_ENABLE
, ENABLE
) == 0) &&
429 (REG_GET_FIELD(reg_ic_enable_status
, CKSVII2C_IC_ENABLE_STATUS
, IC_EN
) == 1)) {
431 * Nobody is using I2C engine, but engine remains active because
432 * someone missed to send STOP
434 smu_v11_0_i2c_abort(control
);
435 } else if (REG_GET_FIELD(reg_ic_enable
, CKSVII2C_IC_ENABLE
, ENABLE
) == 0) {
436 /* Nobody is using I2C engine */
440 /* Keep reading activity bit until it's cleared */
442 reg_ic_clr_activity
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_CLR_ACTIVITY
);
444 if (REG_GET_FIELD(reg_ic_clr_activity
,
445 CKSVII2C_IC_CLR_ACTIVITY
, CLR_ACTIVITY
) == 0)
450 } while (timeout_count
< IDLE_TIMEOUT
);
455 static void smu_v11_0_i2c_init(struct i2c_adapter
*control
)
457 /* Disable clock gating */
458 smu_v11_0_i2c_set_clock_gating(control
, false);
460 if (!smu_v11_0_i2c_activity_done(control
))
461 DRM_WARN("I2C busy !");
464 smu_v11_0_i2c_enable(control
, false);
466 /* Configure I2C to operate as master and in standard mode */
467 smu_v11_0_i2c_configure(control
);
469 /* Initialize the clock to 50 kHz default */
470 smu_v11_0_i2c_set_clock(control
);
474 static void smu_v11_0_i2c_fini(struct i2c_adapter
*control
)
476 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
477 uint32_t reg_ic_enable_status
, reg_ic_enable
;
479 smu_v11_0_i2c_enable(control
, false);
481 /* Double check if disabled, else force abort */
482 reg_ic_enable_status
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_ENABLE_STATUS
);
483 reg_ic_enable
= RREG32_SOC15(SMUIO
, 0, mmCKSVII2C_IC_ENABLE
);
485 if ((REG_GET_FIELD(reg_ic_enable
, CKSVII2C_IC_ENABLE
, ENABLE
) == 0) &&
486 (REG_GET_FIELD(reg_ic_enable_status
,
487 CKSVII2C_IC_ENABLE_STATUS
, IC_EN
) == 1)) {
489 * Nobody is using I2C engine, but engine remains active because
490 * someone missed to send STOP
492 smu_v11_0_i2c_abort(control
);
495 /* Restore clock gating */
498 * TODO Reenabling clock gating seems to break subsequent SMU operation
499 * on the I2C bus. My guess is that SMU doesn't disable clock gating like
500 * we do here before working with the bus. So for now just don't restore
501 * it but later work with SMU to see if they have this issue and can
502 * update their code appropriately
504 /* smu_v11_0_i2c_set_clock_gating(control, true); */
508 static bool smu_v11_0_i2c_bus_lock(struct i2c_adapter
*control
)
510 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
512 /* Send PPSMC_MSG_RequestI2CBus */
513 if (!adev
->powerplay
.pp_funcs
->smu_i2c_bus_access
)
517 if (!adev
->powerplay
.pp_funcs
->smu_i2c_bus_access(adev
->powerplay
.pp_handle
, true))
524 static bool smu_v11_0_i2c_bus_unlock(struct i2c_adapter
*control
)
526 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
528 /* Send PPSMC_MSG_RequestI2CBus */
529 if (!adev
->powerplay
.pp_funcs
->smu_i2c_bus_access
)
532 /* Send PPSMC_MSG_ReleaseI2CBus */
533 if (!adev
->powerplay
.pp_funcs
->smu_i2c_bus_access(adev
->powerplay
.pp_handle
,
541 /***************************** EEPROM I2C GLUE ****************************/
543 static uint32_t smu_v11_0_i2c_eeprom_read_data(struct i2c_adapter
*control
,
550 /* First 2 bytes are dummy write to set EEPROM address */
551 ret
= smu_v11_0_i2c_transmit(control
, address
, data
, 2, I2C_NO_STOP
);
555 /* Now read data starting with that address */
556 ret
= smu_v11_0_i2c_receive(control
, address
, data
+ 2, numbytes
- 2,
561 DRM_ERROR("ReadData() - I2C error occurred :%x", ret
);
566 static uint32_t smu_v11_0_i2c_eeprom_write_data(struct i2c_adapter
*control
,
573 ret
= smu_v11_0_i2c_transmit(control
, address
, data
, numbytes
, 0);
576 DRM_ERROR("WriteI2CData() - I2C error occurred :%x", ret
);
579 * According to EEPROM spec there is a MAX of 10 ms required for
580 * EEPROM to flush internal RX buffer after STOP was issued at the
581 * end of write transaction. During this time the EEPROM will not be
582 * responsive to any more commands - so wait a bit more.
584 * TODO Improve to wait for first ACK for slave address after
585 * internal write cycle done.
593 static void lock_bus(struct i2c_adapter
*i2c
, unsigned int flags
)
595 struct amdgpu_ras_eeprom_control
*control
= to_eeprom_control(i2c
);
597 if (!smu_v11_0_i2c_bus_lock(i2c
)) {
598 DRM_ERROR("Failed to lock the bus from SMU");
602 control
->bus_locked
= true;
605 static int trylock_bus(struct i2c_adapter
*i2c
, unsigned int flags
)
607 WARN_ONCE(1, "This operation not supposed to run in atomic context!");
611 static void unlock_bus(struct i2c_adapter
*i2c
, unsigned int flags
)
613 struct amdgpu_ras_eeprom_control
*control
= to_eeprom_control(i2c
);
615 if (!smu_v11_0_i2c_bus_unlock(i2c
)) {
616 DRM_ERROR("Failed to unlock the bus from SMU");
620 control
->bus_locked
= false;
623 static const struct i2c_lock_operations smu_v11_0_i2c_i2c_lock_ops
= {
624 .lock_bus
= lock_bus
,
625 .trylock_bus
= trylock_bus
,
626 .unlock_bus
= unlock_bus
,
629 static int smu_v11_0_i2c_eeprom_i2c_xfer(struct i2c_adapter
*i2c_adap
,
630 struct i2c_msg
*msgs
, int num
)
633 struct amdgpu_ras_eeprom_control
*control
= to_eeprom_control(i2c_adap
);
635 if (!control
->bus_locked
) {
636 DRM_ERROR("I2C bus unlocked, stopping transaction!");
640 smu_v11_0_i2c_init(i2c_adap
);
642 for (i
= 0; i
< num
; i
++) {
643 if (msgs
[i
].flags
& I2C_M_RD
)
644 ret
= smu_v11_0_i2c_eeprom_read_data(i2c_adap
,
645 (uint8_t)msgs
[i
].addr
,
646 msgs
[i
].buf
, msgs
[i
].len
);
648 ret
= smu_v11_0_i2c_eeprom_write_data(i2c_adap
,
649 (uint8_t)msgs
[i
].addr
,
650 msgs
[i
].buf
, msgs
[i
].len
);
658 smu_v11_0_i2c_fini(i2c_adap
);
662 static u32
smu_v11_0_i2c_eeprom_i2c_func(struct i2c_adapter
*adap
)
664 return I2C_FUNC_I2C
| I2C_FUNC_SMBUS_EMUL
;
668 static const struct i2c_algorithm smu_v11_0_i2c_eeprom_i2c_algo
= {
669 .master_xfer
= smu_v11_0_i2c_eeprom_i2c_xfer
,
670 .functionality
= smu_v11_0_i2c_eeprom_i2c_func
,
673 int smu_v11_0_i2c_eeprom_control_init(struct i2c_adapter
*control
)
675 struct amdgpu_device
*adev
= to_amdgpu_device(control
);
678 control
->owner
= THIS_MODULE
;
679 control
->class = I2C_CLASS_SPD
;
680 control
->dev
.parent
= &adev
->pdev
->dev
;
681 control
->algo
= &smu_v11_0_i2c_eeprom_i2c_algo
;
682 snprintf(control
->name
, sizeof(control
->name
), "RAS EEPROM");
683 control
->lock_ops
= &smu_v11_0_i2c_i2c_lock_ops
;
685 res
= i2c_add_adapter(control
);
687 DRM_ERROR("Failed to register hw i2c, err: %d\n", res
);
692 void smu_v11_0_i2c_eeprom_control_fini(struct i2c_adapter
*control
)
694 i2c_del_adapter(control
);
698 * Keep this for future unit test if bugs arise
701 #define I2C_TARGET_ADDR 0xA0
703 bool smu_v11_0_i2c_test_bus(struct i2c_adapter
*control
)
706 uint32_t ret
= I2C_OK
;
707 uint8_t data
[6] = {0xf, 0, 0xde, 0xad, 0xbe, 0xef};
712 if (!smu_v11_0_i2c_bus_lock(control
)) {
713 DRM_ERROR("Failed to lock the bus!.");
717 smu_v11_0_i2c_init(control
);
719 /* Write 0xde to address 0x0000 on the EEPROM */
720 ret
= smu_v11_0_i2c_eeprom_write_data(control
, I2C_TARGET_ADDR
, data
, 6);
722 ret
= smu_v11_0_i2c_eeprom_read_data(control
, I2C_TARGET_ADDR
, data
, 6);
724 smu_v11_0_i2c_fini(control
);
726 smu_v11_0_i2c_bus_unlock(control
);