i2c: xgene-slimpro: Add ACPI support by using PCC mailbox
authorHoan Tran <hotran@apm.com>
Mon, 24 Apr 2017 18:00:26 +0000 (11:00 -0700)
committerWolfram Sang <wsa@the-dreams.de>
Fri, 2 Jun 2017 20:27:25 +0000 (22:27 +0200)
This patch adds ACPI support by using PCC mailbox communication
interface.

Signed-off-by: Hoan Tran <hotran@apm.com>
Signed-off-by: Wolfram Sang <wsa@the-dreams.de>
drivers/i2c/busses/i2c-xgene-slimpro.c

index 4e2257850d2cd5e3544c5c36bfaccf4bb5897cca..8e5c8f28bc8bf039767362be30d617e43d4255aa 100644 (file)
@@ -22,6 +22,7 @@
  * using the APM X-Gene SLIMpro mailbox driver.
  *
  */
+#include <acpi/pcc.h>
 #include <linux/acpi.h>
 #include <linux/dma-mapping.h>
 #include <linux/i2c.h>
@@ -89,6 +90,8 @@
        ((addrlen << SLIMPRO_IIC_ADDRLEN_SHIFT) & SLIMPRO_IIC_ADDRLEN_MASK) | \
        ((datalen << SLIMPRO_IIC_DATALEN_SHIFT) & SLIMPRO_IIC_DATALEN_MASK))
 
+#define SLIMPRO_MSG_TYPE(v)             (((v) & 0xF0000000) >> 28)
+
 /*
  * Encode for upper address for block data
  */
                                                                & 0x3FF00000))
 #define SLIMPRO_IIC_ENCODE_ADDR(a)                     ((a) & 0x000FFFFF)
 
+#define SLIMPRO_IIC_MSG_DWORD_COUNT                    3
+
+/* PCC related defines */
+#define PCC_SIGNATURE                  0x50424300
+#define PCC_STS_CMD_COMPLETE           BIT(0)
+#define PCC_STS_SCI_DOORBELL           BIT(1)
+#define PCC_STS_ERR                    BIT(2)
+#define PCC_STS_PLAT_NOTIFY            BIT(3)
+#define PCC_CMD_GENERATE_DB_INT                BIT(15)
+
 struct slimpro_i2c_dev {
        struct i2c_adapter adapter;
        struct device *dev;
        struct mbox_chan *mbox_chan;
        struct mbox_client mbox_client;
+       int mbox_idx;
        struct completion rd_complete;
        u8 dma_buffer[I2C_SMBUS_BLOCK_MAX + 1]; /* dma_buffer[0] is used for length */
        u32 *resp_msg;
+       phys_addr_t comm_base_addr;
+       void *pcc_comm_addr;
 };
 
 #define to_slimpro_i2c_dev(cl) \
                container_of(cl, struct slimpro_i2c_dev, mbox_client)
 
+/*
+ * This function tests and clears a bitmask then returns its old value
+ */
+static u16 xgene_word_tst_and_clr(u16 *addr, u16 mask)
+{
+       u16 ret, val;
+
+       val = le16_to_cpu(READ_ONCE(*addr));
+       ret = val & mask;
+       val &= ~mask;
+       WRITE_ONCE(*addr, cpu_to_le16(val));
+
+       return ret;
+}
+
 static void slimpro_i2c_rx_cb(struct mbox_client *cl, void *mssg)
 {
        struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl);
@@ -129,9 +160,53 @@ static void slimpro_i2c_rx_cb(struct mbox_client *cl, void *mssg)
                complete(&ctx->rd_complete);
 }
 
+static void slimpro_i2c_pcc_rx_cb(struct mbox_client *cl, void *msg)
+{
+       struct slimpro_i2c_dev *ctx = to_slimpro_i2c_dev(cl);
+       struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
+
+       /* Check if platform sends interrupt */
+       if (!xgene_word_tst_and_clr(&generic_comm_base->status,
+                                   PCC_STS_SCI_DOORBELL))
+               return;
+
+       if (xgene_word_tst_and_clr(&generic_comm_base->status,
+                                  PCC_STS_CMD_COMPLETE)) {
+               msg = generic_comm_base + 1;
+
+               /* Response message msg[1] contains the return value. */
+               if (ctx->resp_msg)
+                       *ctx->resp_msg = ((u32 *)msg)[1];
+
+               complete(&ctx->rd_complete);
+       }
+}
+
+static void slimpro_i2c_pcc_tx_prepare(struct slimpro_i2c_dev *ctx, u32 *msg)
+{
+       struct acpi_pcct_shared_memory *generic_comm_base = ctx->pcc_comm_addr;
+       u32 *ptr = (void *)(generic_comm_base + 1);
+       u16 status;
+       int i;
+
+       WRITE_ONCE(generic_comm_base->signature,
+                  cpu_to_le32(PCC_SIGNATURE | ctx->mbox_idx));
+
+       WRITE_ONCE(generic_comm_base->command,
+                  cpu_to_le16(SLIMPRO_MSG_TYPE(msg[0]) | PCC_CMD_GENERATE_DB_INT));
+
+       status = le16_to_cpu(READ_ONCE(generic_comm_base->status));
+       status &= ~PCC_STS_CMD_COMPLETE;
+       WRITE_ONCE(generic_comm_base->status, cpu_to_le16(status));
+
+       /* Copy the message to the PCC comm space */
+       for (i = 0; i < SLIMPRO_IIC_MSG_DWORD_COUNT; i++)
+               WRITE_ONCE(ptr[i], cpu_to_le32(msg[i]));
+}
+
 static int start_i2c_msg_xfer(struct slimpro_i2c_dev *ctx)
 {
-       if (ctx->mbox_client.tx_block) {
+       if (ctx->mbox_client.tx_block || !acpi_disabled) {
                if (!wait_for_completion_timeout(&ctx->rd_complete,
                                                 msecs_to_jiffies(MAILBOX_OP_TIMEOUT)))
                        return -ETIMEDOUT;
@@ -152,6 +227,11 @@ static int slimpro_i2c_send_msg(struct slimpro_i2c_dev *ctx,
 
        ctx->resp_msg = data;
 
+       if (!acpi_disabled) {
+               reinit_completion(&ctx->rd_complete);
+               slimpro_i2c_pcc_tx_prepare(ctx, msg);
+       }
+
        rc = mbox_send_message(ctx->mbox_chan, msg);
        if (rc < 0)
                goto err;
@@ -159,6 +239,9 @@ static int slimpro_i2c_send_msg(struct slimpro_i2c_dev *ctx,
        rc = start_i2c_msg_xfer(ctx);
 
 err:
+       if (!acpi_disabled)
+               mbox_chan_txdone(ctx->mbox_chan, 0);
+
        ctx->resp_msg = NULL;
 
        return rc;
@@ -379,17 +462,73 @@ static int xgene_slimpro_i2c_probe(struct platform_device *pdev)
 
        /* Request mailbox channel */
        cl->dev = &pdev->dev;
-       cl->rx_callback = slimpro_i2c_rx_cb;
-       cl->tx_block = true;
        init_completion(&ctx->rd_complete);
        cl->tx_tout = MAILBOX_OP_TIMEOUT;
        cl->knows_txdone = false;
-       ctx->mbox_chan = mbox_request_channel(cl, MAILBOX_I2C_INDEX);
-       if (IS_ERR(ctx->mbox_chan)) {
-               dev_err(&pdev->dev, "i2c mailbox channel request failed\n");
-               return PTR_ERR(ctx->mbox_chan);
-       }
+       if (acpi_disabled) {
+               cl->tx_block = true;
+               cl->rx_callback = slimpro_i2c_rx_cb;
+               ctx->mbox_chan = mbox_request_channel(cl, MAILBOX_I2C_INDEX);
+               if (IS_ERR(ctx->mbox_chan)) {
+                       dev_err(&pdev->dev, "i2c mailbox channel request failed\n");
+                       return PTR_ERR(ctx->mbox_chan);
+               }
+       } else {
+               struct acpi_pcct_hw_reduced *cppc_ss;
+
+               if (device_property_read_u32(&pdev->dev, "pcc-channel",
+                                            &ctx->mbox_idx))
+                       ctx->mbox_idx = MAILBOX_I2C_INDEX;
+
+               cl->tx_block = false;
+               cl->rx_callback = slimpro_i2c_pcc_rx_cb;
+               ctx->mbox_chan = pcc_mbox_request_channel(cl, ctx->mbox_idx);
+               if (IS_ERR(ctx->mbox_chan)) {
+                       dev_err(&pdev->dev, "PCC mailbox channel request failed\n");
+                       return PTR_ERR(ctx->mbox_chan);
+               }
+
+               /*
+                * The PCC mailbox controller driver should
+                * have parsed the PCCT (global table of all
+                * PCC channels) and stored pointers to the
+                * subspace communication region in con_priv.
+                */
+               cppc_ss = ctx->mbox_chan->con_priv;
+               if (!cppc_ss) {
+                       dev_err(&pdev->dev, "PPC subspace not found\n");
+                       rc = -ENOENT;
+                       goto mbox_err;
+               }
+
+               if (!ctx->mbox_chan->mbox->txdone_irq) {
+                       dev_err(&pdev->dev, "PCC IRQ not supported\n");
+                       rc = -ENOENT;
+                       goto mbox_err;
+               }
 
+               /*
+                * This is the shared communication region
+                * for the OS and Platform to communicate over.
+                */
+               ctx->comm_base_addr = cppc_ss->base_address;
+               if (ctx->comm_base_addr) {
+                       ctx->pcc_comm_addr = memremap(ctx->comm_base_addr,
+                                                     cppc_ss->length,
+                                                     MEMREMAP_WB);
+               } else {
+                       dev_err(&pdev->dev, "Failed to get PCC comm region\n");
+                       rc = -ENOENT;
+                       goto mbox_err;
+               }
+
+               if (!ctx->pcc_comm_addr) {
+                       dev_err(&pdev->dev,
+                               "Failed to ioremap PCC comm region\n");
+                       rc = -ENOMEM;
+                       goto mbox_err;
+               }
+       }
        rc = dma_set_mask_and_coherent(&pdev->dev, DMA_BIT_MASK(64));
        if (rc)
                dev_warn(&pdev->dev, "Unable to set dma mask\n");
@@ -404,13 +543,19 @@ static int xgene_slimpro_i2c_probe(struct platform_device *pdev)
        ACPI_COMPANION_SET(&adapter->dev, ACPI_COMPANION(&pdev->dev));
        i2c_set_adapdata(adapter, ctx);
        rc = i2c_add_adapter(adapter);
-       if (rc) {
-               mbox_free_channel(ctx->mbox_chan);
-               return rc;
-       }
+       if (rc)
+               goto mbox_err;
 
        dev_info(&pdev->dev, "Mailbox I2C Adapter registered\n");
        return 0;
+
+mbox_err:
+       if (acpi_disabled)
+               mbox_free_channel(ctx->mbox_chan);
+       else
+               pcc_mbox_free_channel(ctx->mbox_chan);
+
+       return rc;
 }
 
 static int xgene_slimpro_i2c_remove(struct platform_device *pdev)
@@ -419,7 +564,10 @@ static int xgene_slimpro_i2c_remove(struct platform_device *pdev)
 
        i2c_del_adapter(&ctx->adapter);
 
-       mbox_free_channel(ctx->mbox_chan);
+       if (acpi_disabled)
+               mbox_free_channel(ctx->mbox_chan);
+       else
+               pcc_mbox_free_channel(ctx->mbox_chan);
 
        return 0;
 }