2020-02-04 16:01:50 -08:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2020 Intel Corporation
|
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: Apache-2.0
|
|
|
|
*/
|
|
|
|
|
2020-03-24 15:45:46 -05:00
|
|
|
#define DT_DRV_COMPAT microchip_xec_peci
|
|
|
|
|
2020-02-04 16:01:50 -08:00
|
|
|
#include <errno.h>
|
|
|
|
#include <device.h>
|
|
|
|
#include <drivers/peci.h>
|
|
|
|
#include <soc.h>
|
|
|
|
#include <logging/log.h>
|
|
|
|
LOG_MODULE_REGISTER(peci_mchp_xec, CONFIG_PECI_LOG_LEVEL);
|
|
|
|
|
|
|
|
/* Maximum PECI core clock is the main clock 48Mhz */
|
|
|
|
#define MAX_PECI_CORE_CLOCK 48000u
|
|
|
|
/* 1 ms */
|
|
|
|
#define PECI_RESET_DELAY 1000u
|
2020-12-30 14:20:40 -08:00
|
|
|
#define PECI_RESET_DELAY_MS 1u
|
2020-02-04 16:01:50 -08:00
|
|
|
/* 100 us */
|
|
|
|
#define PECI_IDLE_DELAY 100u
|
|
|
|
/* 5 ms */
|
|
|
|
#define PECI_IDLE_TIMEOUT 50u
|
2020-10-20 12:07:14 -07:00
|
|
|
/* Maximum retries */
|
|
|
|
#define PECI_TIMEOUT_RETRIES 3u
|
2021-02-15 22:19:17 +05:30
|
|
|
/* Maximum read buffer fill wait retries */
|
|
|
|
#define PECI_RX_BUF_FILL_WAIT_RETRY 100u
|
2020-10-20 12:07:14 -07:00
|
|
|
|
2020-02-04 16:01:50 -08:00
|
|
|
/* 10 us */
|
|
|
|
#define PECI_IO_DELAY 10
|
|
|
|
|
|
|
|
#define OPT_BIT_TIME_MSB_OFS 8u
|
|
|
|
|
|
|
|
#define PECI_FCS_LEN 2
|
|
|
|
|
|
|
|
struct peci_xec_config {
|
2022-03-24 14:28:59 -04:00
|
|
|
struct peci_regs * const regs;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t irq_num;
|
2022-03-24 14:28:59 -04:00
|
|
|
uint8_t girq;
|
|
|
|
uint8_t girq_pos;
|
|
|
|
uint8_t pcr_idx;
|
|
|
|
uint8_t pcr_pos;
|
2020-02-04 16:01:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
struct peci_xec_data {
|
|
|
|
struct k_sem tx_lock;
|
2020-10-20 12:07:14 -07:00
|
|
|
uint32_t bitrate;
|
|
|
|
int timeout_retries;
|
2020-02-04 16:01:50 -08:00
|
|
|
};
|
|
|
|
|
|
|
|
static struct peci_xec_data peci_data;
|
|
|
|
|
|
|
|
static const struct peci_xec_config peci_xec_config = {
|
2022-03-24 14:28:59 -04:00
|
|
|
.regs = (struct peci_regs * const)(DT_INST_REG_ADDR(0)),
|
2020-03-24 15:45:46 -05:00
|
|
|
.irq_num = DT_INST_IRQN(0),
|
2022-03-24 14:28:59 -04:00
|
|
|
.girq = DT_INST_PROP_BY_IDX(0, girqs, 0),
|
|
|
|
.girq_pos = DT_INST_PROP_BY_IDX(0, girqs, 1),
|
|
|
|
.pcr_idx = DT_INST_PROP_BY_IDX(0, pcrs, 0),
|
|
|
|
.pcr_pos = DT_INST_PROP_BY_IDX(0, pcrs, 1),
|
2020-02-04 16:01:50 -08:00
|
|
|
};
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
static inline void peci_girq_enable(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
|
|
|
|
MCHP_GIRQ_ENSET(cfg->girq) = BIT(cfg->girq_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void peci_girq_status_clear(const struct device *dev)
|
|
|
|
{
|
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
|
|
|
|
MCHP_GIRQ_SRC(cfg->girq) = BIT(cfg->girq_pos);
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline void peci_clr_slp_en(const struct device *dev)
|
|
|
|
{
|
|
|
|
ARG_UNUSED(dev);
|
|
|
|
|
|
|
|
mchp_pcr_periph_slp_ctrl(PCR_PECI, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
static int check_bus_idle(struct peci_regs * const regs)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t delay_cnt = PECI_IDLE_TIMEOUT;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
/* Wait until PECI bus becomes idle.
|
|
|
|
* Note that when IDLE bit in the status register changes, HW do not
|
|
|
|
* generate an interrupt, so need to poll.
|
|
|
|
*/
|
2022-03-24 14:28:59 -04:00
|
|
|
while (!(regs->STATUS2 & MCHP_PECI_STS2_IDLE)) {
|
2020-02-04 16:01:50 -08:00
|
|
|
k_busy_wait(PECI_IDLE_DELAY);
|
|
|
|
delay_cnt--;
|
|
|
|
|
|
|
|
if (!delay_cnt) {
|
2020-10-20 12:07:14 -07:00
|
|
|
LOG_WRN("Bus is busy");
|
2020-02-04 16:01:50 -08:00
|
|
|
return -EBUSY;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int peci_xec_configure(const struct device *dev, uint32_t bitrate)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_xec_data * const data = dev->data;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint16_t value;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
data->bitrate = bitrate;
|
|
|
|
|
2020-02-04 16:01:50 -08:00
|
|
|
/* Power down PECI interface */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL = MCHP_PECI_CTRL_PD;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
/* Adjust bitrate */
|
|
|
|
value = MAX_PECI_CORE_CLOCK / bitrate;
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->OPT_BIT_TIME_LSB = value & MCHP_PECI_OPT_BT_LSB_MASK;
|
|
|
|
regs->OPT_BIT_TIME_MSB = ((value >> OPT_BIT_TIME_MSB_OFS) &
|
|
|
|
MCHP_PECI_OPT_BT_MSB_MASK);
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
/* Power up PECI interface */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL &= ~MCHP_PECI_CTRL_PD;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int peci_xec_disable(const struct device *dev)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-02-04 16:01:50 -08:00
|
|
|
int ret;
|
|
|
|
|
|
|
|
/* Make sure no transaction is interrupted before disabling the HW */
|
2022-03-24 14:28:59 -04:00
|
|
|
ret = check_bus_idle(regs);
|
2020-02-04 16:01:50 -08:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PECI_INTERRUPT_DRIVEN
|
2022-03-24 14:28:59 -04:00
|
|
|
peci_girq_status_clear(dev);
|
|
|
|
NVIC_ClearPendingIRQ(cfg->irq_num);
|
|
|
|
irq_disable(cfg->irq_num);
|
2020-02-04 16:01:50 -08:00
|
|
|
#endif
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL |= MCHP_PECI_CTRL_PD;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int peci_xec_enable(const struct device *dev)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL &= ~MCHP_PECI_CTRL_PD;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
#ifdef CONFIG_PECI_INTERRUPT_DRIVEN
|
2022-03-24 14:28:59 -04:00
|
|
|
peci_girq_status_clear(dev);
|
|
|
|
peci_girq_enable(dev);
|
|
|
|
irq_enable(cfg->irq_num);
|
2020-02-04 16:01:50 -08:00
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-10-20 12:07:14 -07:00
|
|
|
static void peci_xec_bus_recovery(const struct device *dev, bool full_reset)
|
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_xec_data * const data = dev->data;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-10-20 12:07:14 -07:00
|
|
|
|
|
|
|
LOG_WRN("%s full_reset:%d", __func__, full_reset);
|
|
|
|
if (full_reset) {
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL = MCHP_PECI_CTRL_PD | MCHP_PECI_CTRL_RST;
|
2020-12-30 14:20:40 -08:00
|
|
|
|
|
|
|
if (k_is_in_isr()) {
|
|
|
|
k_busy_wait(PECI_RESET_DELAY_MS);
|
|
|
|
} else {
|
|
|
|
k_msleep(PECI_RESET_DELAY);
|
|
|
|
}
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL &= ~MCHP_PECI_CTRL_RST;
|
2020-10-20 12:07:14 -07:00
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
peci_xec_configure(dev, data->bitrate);
|
2020-10-20 12:07:14 -07:00
|
|
|
} else {
|
|
|
|
/* Only reset internal FIFOs */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL |= MCHP_PECI_CTRL_FRST;
|
2020-10-20 12:07:14 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int peci_xec_write(const struct device *dev, struct peci_msg *msg)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_xec_data * const data = dev->data;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-02-04 16:01:50 -08:00
|
|
|
int i;
|
|
|
|
int ret;
|
|
|
|
|
|
|
|
struct peci_buf *tx_buf = &msg->tx_buffer;
|
|
|
|
struct peci_buf *rx_buf = &msg->rx_buffer;
|
|
|
|
|
|
|
|
/* Check if FIFO is full */
|
2022-03-24 14:28:59 -04:00
|
|
|
if (regs->STATUS2 & MCHP_PECI_STS2_WFF) {
|
2020-10-20 12:07:14 -07:00
|
|
|
LOG_WRN("%s FIFO is full", __func__);
|
2020-02-04 16:01:50 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL &= ~MCHP_PECI_CTRL_FRST;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
/* Add PECI transaction header to TX FIFO */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->WR_DATA = msg->addr;
|
|
|
|
regs->WR_DATA = tx_buf->len;
|
|
|
|
regs->WR_DATA = rx_buf->len;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
2020-11-09 10:13:14 +05:30
|
|
|
/* Add PECI payload to Tx FIFO only if write length is valid */
|
|
|
|
if (tx_buf->len) {
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->WR_DATA = msg->cmd_code;
|
2020-11-09 10:13:14 +05:30
|
|
|
for (i = 0; i < tx_buf->len - 1; i++) {
|
2022-03-24 14:28:59 -04:00
|
|
|
if (!(regs->STATUS2 & MCHP_PECI_STS2_WFF)) {
|
|
|
|
regs->WR_DATA = tx_buf->buf[i];
|
2020-11-09 10:13:14 +05:30
|
|
|
}
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Check bus is idle before starting a new transfer */
|
2022-03-24 14:28:59 -04:00
|
|
|
ret = check_bus_idle(regs);
|
2020-02-04 16:01:50 -08:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL |= MCHP_PECI_CTRL_TXEN;
|
2020-02-04 16:01:50 -08:00
|
|
|
k_busy_wait(PECI_IO_DELAY);
|
|
|
|
|
|
|
|
/* Wait for transmission to complete */
|
|
|
|
#ifdef CONFIG_PECI_INTERRUPT_DRIVEN
|
2022-03-24 14:28:59 -04:00
|
|
|
if (k_sem_take(&data->tx_lock, PECI_IO_DELAY * tx_buf->len)) {
|
2020-02-04 16:01:50 -08:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
#else
|
2020-11-09 10:13:14 +05:30
|
|
|
/* In worst case, overall timeout will be 1msec (100 * 10usec) */
|
|
|
|
uint8_t wait_timeout_cnt = 100;
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
while (!(regs->STATUS1 & MCHP_PECI_STS1_EOF)) {
|
2020-02-04 16:01:50 -08:00
|
|
|
k_busy_wait(PECI_IO_DELAY);
|
2020-11-09 10:13:14 +05:30
|
|
|
wait_timeout_cnt--;
|
|
|
|
if (!wait_timeout_cnt) {
|
2020-10-20 12:07:14 -07:00
|
|
|
LOG_WRN("Tx timeout");
|
2022-03-24 14:28:59 -04:00
|
|
|
data->timeout_retries++;
|
2020-10-20 12:07:14 -07:00
|
|
|
/* Full reset only if multiple consecutive failures */
|
2022-03-24 14:28:59 -04:00
|
|
|
if (data->timeout_retries > PECI_TIMEOUT_RETRIES) {
|
2020-10-20 12:07:14 -07:00
|
|
|
peci_xec_bus_recovery(dev, true);
|
|
|
|
} else {
|
|
|
|
peci_xec_bus_recovery(dev, false);
|
|
|
|
}
|
|
|
|
|
2020-02-04 16:01:50 -08:00
|
|
|
return -ETIMEDOUT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2022-03-24 14:28:59 -04:00
|
|
|
data->timeout_retries = 0;
|
2020-10-20 12:07:14 -07:00
|
|
|
|
2020-02-04 16:01:50 -08:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int peci_xec_read(const struct device *dev, struct peci_msg *msg)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-02-04 16:01:50 -08:00
|
|
|
int i;
|
|
|
|
int ret;
|
2020-05-27 11:26:57 -05:00
|
|
|
uint8_t tx_fcs;
|
|
|
|
uint8_t bytes_rcvd;
|
2021-02-15 22:19:17 +05:30
|
|
|
uint8_t wait_timeout_cnt;
|
2020-02-04 16:01:50 -08:00
|
|
|
struct peci_buf *rx_buf = &msg->rx_buffer;
|
|
|
|
|
|
|
|
/* Attempt to read data from RX FIFO */
|
|
|
|
bytes_rcvd = 0;
|
|
|
|
for (i = 0; i < (rx_buf->len + PECI_FCS_LEN); i++) {
|
2021-02-15 22:19:17 +05:30
|
|
|
/* Worst case timeout will be 1msec (100 * 10usec) */
|
|
|
|
wait_timeout_cnt = PECI_RX_BUF_FILL_WAIT_RETRY;
|
|
|
|
/* Wait for read buffer to fill up */
|
2022-03-24 14:28:59 -04:00
|
|
|
while (regs->STATUS2 & MCHP_PECI_STS2_RFE) {
|
2021-02-15 22:19:17 +05:30
|
|
|
k_usleep(PECI_IO_DELAY);
|
|
|
|
wait_timeout_cnt--;
|
|
|
|
if (!wait_timeout_cnt) {
|
|
|
|
LOG_WRN("Rx buffer empty");
|
|
|
|
return -ETIMEDOUT;
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
}
|
2021-02-15 22:19:17 +05:30
|
|
|
|
|
|
|
if (i == 0) {
|
|
|
|
/* Get write block FCS just for debug */
|
2022-03-24 14:28:59 -04:00
|
|
|
tx_fcs = regs->RD_DATA;
|
2021-02-15 22:19:17 +05:30
|
|
|
LOG_DBG("TX FCS %x", tx_fcs);
|
|
|
|
} else if (i == (rx_buf->len + 1)) {
|
|
|
|
/* Get read block FCS, but don't count it */
|
2022-03-24 14:28:59 -04:00
|
|
|
rx_buf->buf[i-1] = regs->RD_DATA;
|
2021-02-15 22:19:17 +05:30
|
|
|
} else {
|
|
|
|
/* Get response */
|
2022-03-24 14:28:59 -04:00
|
|
|
rx_buf->buf[i-1] = regs->RD_DATA;
|
2021-02-15 22:19:17 +05:30
|
|
|
bytes_rcvd++;
|
|
|
|
}
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check if transaction is as expected */
|
|
|
|
if (rx_buf->len != bytes_rcvd) {
|
2020-10-20 12:07:14 -07:00
|
|
|
LOG_INF("Incomplete %x vs %x", bytes_rcvd, rx_buf->len);
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Once write-read transaction is complete, ensure bus is idle
|
|
|
|
* before resetting the internal FIFOs
|
|
|
|
*/
|
2022-03-24 14:28:59 -04:00
|
|
|
ret = check_bus_idle(regs);
|
2020-02-04 16:01:50 -08:00
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int peci_xec_transfer(const struct device *dev, struct peci_msg *msg)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-02-04 16:01:50 -08:00
|
|
|
int ret;
|
2020-09-10 11:26:59 +05:30
|
|
|
uint8_t err_val;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
ret = peci_xec_write(dev, msg);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If a PECI transmission is successful, it may or not involve
|
|
|
|
* a read operation, check if transaction expects a response
|
|
|
|
*/
|
|
|
|
if (msg->rx_buffer.len) {
|
|
|
|
ret = peci_xec_read(dev, msg);
|
|
|
|
if (ret) {
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Cleanup */
|
2022-03-24 14:28:59 -04:00
|
|
|
if (regs->STATUS1 & MCHP_PECI_STS1_EOF) {
|
|
|
|
regs->STATUS1 |= MCHP_PECI_STS1_EOF;
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Check for error conditions and perform bus recovery if necessary */
|
2022-03-24 14:28:59 -04:00
|
|
|
err_val = regs->ERROR;
|
2020-03-23 15:23:48 -07:00
|
|
|
if (err_val) {
|
2020-10-20 12:07:14 -07:00
|
|
|
if (err_val & MCHP_PECI_ERR_RDOV) {
|
|
|
|
LOG_ERR("Read buffer is not empty");
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
2020-10-20 12:07:14 -07:00
|
|
|
if (err_val & MCHP_PECI_ERR_WRUN) {
|
|
|
|
LOG_ERR("Write buffer is not empty");
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
2020-10-20 12:07:14 -07:00
|
|
|
if (err_val & MCHP_PECI_ERR_BERR) {
|
|
|
|
LOG_ERR("PECI bus error");
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
2020-10-20 12:07:14 -07:00
|
|
|
LOG_DBG("PECI err %x", err_val);
|
2022-03-24 14:28:59 -04:00
|
|
|
LOG_DBG("PECI sts1 %x", regs->STATUS1);
|
|
|
|
LOG_DBG("PECI sts2 %x", regs->STATUS2);
|
2020-10-20 12:07:14 -07:00
|
|
|
|
2020-09-10 11:26:59 +05:30
|
|
|
/* ERROR is a clear-on-write register, need to clear errors
|
|
|
|
* occurring at the end of a transaction. A temp variable is
|
|
|
|
* used to overcome complaints by the static code analyzer
|
|
|
|
*/
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->ERROR = err_val;
|
2020-10-20 12:07:14 -07:00
|
|
|
peci_xec_bus_recovery(dev, false);
|
2020-02-04 16:01:50 -08:00
|
|
|
return -EIO;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
#ifdef CONFIG_PECI_INTERRUPT_DRIVEN
|
isr: Normalize usage of device instance through ISR
The goal of this patch is to replace the 'void *' parameter by 'struct
device *' if they use such variable or just 'const void *' on all
relevant ISRs
This will avoid not-so-nice const qualifier tweaks when device instances
will be constant.
Note that only the ISR passed to IRQ_CONNECT are of interest here.
In order to do so, the script fix_isr.py below is necessary:
from pathlib import Path
import subprocess
import pickle
import mmap
import sys
import re
import os
cocci_template = """
@r_fix_isr_0
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
(
const struct device *D = (const struct device *)P;
|
const struct device *D = P;
)
...
}
@r_fix_isr_1
@
type ret_type;
identifier P;
identifier D;
@@
-ret_type <!fn!>(void *P)
+ret_type <!fn!>(const struct device *P)
{
...
const struct device *D;
...
(
D = (const struct device *)P;
|
D = P;
)
...
}
@r_fix_isr_2
@
type ret_type;
identifier A;
@@
-ret_type <!fn!>(void *A)
+ret_type <!fn!>(const void *A)
{
...
}
@r_fix_isr_3
@
const struct device *D;
@@
-<!fn!>((void *)D);
+<!fn!>(D);
@r_fix_isr_4
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
(
-const struct device *D = (const struct device *)P;
|
-const struct device *D = P;
)
...
}
@r_fix_isr_5
@
type ret_type;
identifier D;
identifier P;
@@
-ret_type <!fn!>(const struct device *P)
+ret_type <!fn!>(const struct device *D)
{
...
-const struct device *D;
...
(
-D = (const struct device *)P;
|
-D = P;
)
...
}
"""
def find_isr(fn):
db = []
data = None
start = 0
try:
with open(fn, 'r+') as f:
data = str(mmap.mmap(f.fileno(), 0).read())
except Exception as e:
return db
while True:
isr = ""
irq = data.find('IRQ_CONNECT', start)
while irq > -1:
p = 1
arg = 1
p_o = data.find('(', irq)
if p_o < 0:
irq = -1
break;
pos = p_o + 1
while p > 0:
if data[pos] == ')':
p -= 1
elif data[pos] == '(':
p += 1
elif data[pos] == ',' and p == 1:
arg += 1
if arg == 3:
isr += data[pos]
pos += 1
isr = isr.strip(',\\n\\t ')
if isr not in db and len(isr) > 0:
db.append(isr)
start = pos
break
if irq < 0:
break
return db
def patch_isr(fn, isr_list):
if len(isr_list) <= 0:
return
for isr in isr_list:
tmplt = cocci_template.replace('<!fn!>', isr)
with open('/tmp/isr_fix.cocci', 'w') as f:
f.write(tmplt)
cmd = ['spatch', '--sp-file', '/tmp/isr_fix.cocci', '--in-place', fn]
subprocess.run(cmd)
def process_files(path):
if path.is_file() and path.suffix in ['.h', '.c']:
p = str(path.parent) + '/' + path.name
isr_list = find_isr(p)
patch_isr(p, isr_list)
elif path.is_dir():
for p in path.iterdir():
process_files(p)
if len(sys.argv) < 2:
print("You need to provide a dir/file path")
sys.exit(1)
process_files(Path(sys.argv[1]))
And is run: ./fix_isr.py <zephyr root directory>
Finally, some files needed manual fixes such.
Fixes #27399
Signed-off-by: Tomasz Bursztyka <tomasz.bursztyka@linux.intel.com>
2020-06-17 14:58:56 +02:00
|
|
|
static void peci_xec_isr(const void *arg)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct device *dev = arg;
|
|
|
|
struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_xec_data * const data = dev->data;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
|
|
|
uint8_t peci_error = regs->ERROR;
|
|
|
|
uint8_t peci_status2 = regs->STATUS2;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
peci_girq_status_clear(dev);
|
2020-02-04 16:01:50 -08:00
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
if (peci_error) {
|
|
|
|
regs->ERROR = peci_error;
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
if (peci_status2 & MCHP_PECI_STS2_WFE) {
|
|
|
|
LOG_WRN("TX FIFO empty ST2:%x", peci_status2);
|
|
|
|
k_sem_give(&data->tx_lock);
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
if (peci_status2 & MCHP_PECI_STS2_RFE) {
|
|
|
|
LOG_WRN("RX FIFO full ST2:%x", peci_status2);
|
2020-02-04 16:01:50 -08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
static const struct peci_driver_api peci_xec_driver_api = {
|
|
|
|
.config = peci_xec_configure,
|
|
|
|
.enable = peci_xec_enable,
|
|
|
|
.disable = peci_xec_disable,
|
|
|
|
.transfer = peci_xec_transfer,
|
|
|
|
};
|
|
|
|
|
2020-04-30 20:33:38 +02:00
|
|
|
static int peci_xec_init(const struct device *dev)
|
2020-02-04 16:01:50 -08:00
|
|
|
{
|
2022-03-24 14:28:59 -04:00
|
|
|
const struct peci_xec_config * const cfg = dev->config;
|
|
|
|
struct peci_regs * const regs = cfg->regs;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
#ifdef CONFIG_PECI_INTERRUPT_DRIVEN
|
2022-03-24 14:28:59 -04:00
|
|
|
k_sem_init(&data->tx_lock, 0, 1);
|
2020-02-04 16:01:50 -08:00
|
|
|
#endif
|
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
peci_clr_slp_en(dev);
|
|
|
|
|
2020-02-04 16:01:50 -08:00
|
|
|
/* Reset PECI interface */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL |= MCHP_PECI_CTRL_RST;
|
2020-12-30 14:20:40 -08:00
|
|
|
k_msleep(PECI_RESET_DELAY_MS);
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL &= ~MCHP_PECI_CTRL_RST;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
#ifdef CONFIG_PECI_INTERRUPT_DRIVEN
|
|
|
|
/* Enable interrupt for errors */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->INT_EN1 = (MCHP_PECI_IEN1_EREN | MCHP_PECI_IEN1_EIEN);
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
/* Enable interrupt for Tx FIFO is empty */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->INT_EN2 |= MCHP_PECI_IEN2_ENWFE;
|
2020-02-04 16:01:50 -08:00
|
|
|
/* Enable interrupt for Rx FIFO is full */
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->INT_EN2 |= MCHP_PECI_IEN2_ENRFF;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
2022-03-24 14:28:59 -04:00
|
|
|
regs->CONTROL |= MCHP_PECI_CTRL_MIEN;
|
2020-02-04 16:01:50 -08:00
|
|
|
|
|
|
|
/* Direct NVIC */
|
2022-03-24 14:28:59 -04:00
|
|
|
IRQ_CONNECT(cfg->irq_num,
|
2020-03-24 15:45:46 -05:00
|
|
|
DT_INST_IRQ(0, priority),
|
2020-02-04 16:01:50 -08:00
|
|
|
peci_xec_isr, NULL, 0);
|
|
|
|
#endif
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-12-15 08:12:20 -06:00
|
|
|
DEVICE_DT_INST_DEFINE(0,
|
2020-02-04 16:01:50 -08:00
|
|
|
&peci_xec_init,
|
2021-04-28 11:26:54 +02:00
|
|
|
NULL,
|
2022-03-24 14:28:59 -04:00
|
|
|
&peci_data, &peci_xec_config,
|
2020-02-04 16:01:50 -08:00
|
|
|
POST_KERNEL, CONFIG_PECI_INIT_PRIORITY,
|
|
|
|
&peci_xec_driver_api);
|