|
| 1 | +// SPDX-License-Identifier: GPL-2.0-or-later |
| 2 | +/* |
| 3 | + * sbrmi-core.c - file defining SB-RMI protocols compliant |
| 4 | + * AMD SoC device. |
| 5 | + * |
| 6 | + * Copyright (C) 2025 Advanced Micro Devices, Inc. |
| 7 | + */ |
| 8 | +#include <linux/delay.h> |
| 9 | +#include <linux/err.h> |
| 10 | +#include <linux/i2c.h> |
| 11 | +#include <linux/mutex.h> |
| 12 | +#include "rmi-core.h" |
| 13 | + |
| 14 | +/* Mask for Status Register bit[1] */ |
| 15 | +#define SW_ALERT_MASK 0x2 |
| 16 | + |
| 17 | +/* Software Interrupt for triggering */ |
| 18 | +#define START_CMD 0x80 |
| 19 | +#define TRIGGER_MAILBOX 0x01 |
| 20 | + |
| 21 | +int rmi_mailbox_xfer(struct sbrmi_data *data, |
| 22 | + struct sbrmi_mailbox_msg *msg) |
| 23 | +{ |
| 24 | + int i, ret, retry = 10; |
| 25 | + int sw_status; |
| 26 | + u8 byte; |
| 27 | + |
| 28 | + mutex_lock(&data->lock); |
| 29 | + |
| 30 | + /* Indicate firmware a command is to be serviced */ |
| 31 | + ret = i2c_smbus_write_byte_data(data->client, |
| 32 | + SBRMI_INBNDMSG7, START_CMD); |
| 33 | + if (ret < 0) |
| 34 | + goto exit_unlock; |
| 35 | + |
| 36 | + /* Write the command to SBRMI::InBndMsg_inst0 */ |
| 37 | + ret = i2c_smbus_write_byte_data(data->client, |
| 38 | + SBRMI_INBNDMSG0, msg->cmd); |
| 39 | + if (ret < 0) |
| 40 | + goto exit_unlock; |
| 41 | + |
| 42 | + /* |
| 43 | + * For both read and write the initiator (BMC) writes |
| 44 | + * Command Data In[31:0] to SBRMI::InBndMsg_inst[4:1] |
| 45 | + * SBRMI_x3C(MSB):SBRMI_x39(LSB) |
| 46 | + */ |
| 47 | + for (i = 0; i < 4; i++) { |
| 48 | + byte = (msg->data_in >> i * 8) & 0xff; |
| 49 | + ret = i2c_smbus_write_byte_data(data->client, |
| 50 | + SBRMI_INBNDMSG1 + i, byte); |
| 51 | + if (ret < 0) |
| 52 | + goto exit_unlock; |
| 53 | + } |
| 54 | + |
| 55 | + /* |
| 56 | + * Write 0x01 to SBRMI::SoftwareInterrupt to notify firmware to |
| 57 | + * perform the requested read or write command |
| 58 | + */ |
| 59 | + ret = i2c_smbus_write_byte_data(data->client, |
| 60 | + SBRMI_SW_INTERRUPT, TRIGGER_MAILBOX); |
| 61 | + if (ret < 0) |
| 62 | + goto exit_unlock; |
| 63 | + |
| 64 | + /* |
| 65 | + * Firmware will write SBRMI::Status[SwAlertSts]=1 to generate |
| 66 | + * an ALERT (if enabled) to initiator (BMC) to indicate completion |
| 67 | + * of the requested command |
| 68 | + */ |
| 69 | + do { |
| 70 | + sw_status = i2c_smbus_read_byte_data(data->client, |
| 71 | + SBRMI_STATUS); |
| 72 | + if (sw_status < 0) { |
| 73 | + ret = sw_status; |
| 74 | + goto exit_unlock; |
| 75 | + } |
| 76 | + if (sw_status & SW_ALERT_MASK) |
| 77 | + break; |
| 78 | + usleep_range(50, 100); |
| 79 | + } while (retry--); |
| 80 | + |
| 81 | + if (retry < 0) { |
| 82 | + dev_err(&data->client->dev, |
| 83 | + "Firmware fail to indicate command completion\n"); |
| 84 | + ret = -EIO; |
| 85 | + goto exit_unlock; |
| 86 | + } |
| 87 | + |
| 88 | + /* |
| 89 | + * For a read operation, the initiator (BMC) reads the firmware |
| 90 | + * response Command Data Out[31:0] from SBRMI::OutBndMsg_inst[4:1] |
| 91 | + * {SBRMI_x34(MSB):SBRMI_x31(LSB)}. |
| 92 | + */ |
| 93 | + if (msg->read) { |
| 94 | + for (i = 0; i < 4; i++) { |
| 95 | + ret = i2c_smbus_read_byte_data(data->client, |
| 96 | + SBRMI_OUTBNDMSG1 + i); |
| 97 | + if (ret < 0) |
| 98 | + goto exit_unlock; |
| 99 | + msg->data_out |= ret << i * 8; |
| 100 | + } |
| 101 | + } |
| 102 | + |
| 103 | + /* |
| 104 | + * BMC must write 1'b1 to SBRMI::Status[SwAlertSts] to clear the |
| 105 | + * ALERT to initiator |
| 106 | + */ |
| 107 | + ret = i2c_smbus_write_byte_data(data->client, SBRMI_STATUS, |
| 108 | + sw_status | SW_ALERT_MASK); |
| 109 | + |
| 110 | +exit_unlock: |
| 111 | + mutex_unlock(&data->lock); |
| 112 | + return ret; |
| 113 | +} |
0 commit comments