Re: [PATCH 3/3] remoteproc: ingenic: Added remoteproc driver

2019-07-25 Thread Paul Cercueil

Hi,


Le dim. 21 juil. 2019 à 22:31, Paul Cercueil  a 
écrit :
This driver is used to boot, communicate with and load firmwares to 
the

MIPS co-processor found in the VPU hardware of the JZ47xx SoCs from
Ingenic.

Signed-off-by: Paul Cercueil 
---
 drivers/remoteproc/Kconfig |   8 +
 drivers/remoteproc/Makefile|   1 +
 drivers/remoteproc/ingenic_rproc.c | 302 
+

 3 files changed, 311 insertions(+)
 create mode 100644 drivers/remoteproc/ingenic_rproc.c

diff --git a/drivers/remoteproc/Kconfig b/drivers/remoteproc/Kconfig
index 28ed306982f7..a0be40e2098d 100644
--- a/drivers/remoteproc/Kconfig
+++ b/drivers/remoteproc/Kconfig
@@ -214,6 +214,14 @@ config STM32_RPROC

  This can be either built-in or a loadable module.

+config INGENIC_RPROC
+   tristate "Ingenic JZ47xx VPU remoteproc support"
+   depends on MIPS || COMPILE_TEST
+   help
+	  Say y or m here to support the VPU in the JZ47xx SoCs from 
Ingenic.

+ This can be either built-in or a loadable module.
+ If unsure say N.
+
 endif # REMOTEPROC

 endmenu
diff --git a/drivers/remoteproc/Makefile b/drivers/remoteproc/Makefile
index 00f09e658cb3..6eb0137abbc7 100644
--- a/drivers/remoteproc/Makefile
+++ b/drivers/remoteproc/Makefile
@@ -10,6 +10,7 @@ remoteproc-y  += remoteproc_sysfs.o
 remoteproc-y   += remoteproc_virtio.o
 remoteproc-y   += remoteproc_elf_loader.o
 obj-$(CONFIG_IMX_REMOTEPROC)   += imx_rproc.o
+obj-$(CONFIG_INGENIC_RPROC)+= ingenic_rproc.o
 obj-$(CONFIG_OMAP_REMOTEPROC)  += omap_remoteproc.o
 obj-$(CONFIG_WKUP_M3_RPROC)+= wkup_m3_rproc.o
 obj-$(CONFIG_DA8XX_REMOTEPROC) += da8xx_remoteproc.o
diff --git a/drivers/remoteproc/ingenic_rproc.c 
b/drivers/remoteproc/ingenic_rproc.c

new file mode 100644
index ..a4963158bdd3
--- /dev/null
+++ b/drivers/remoteproc/ingenic_rproc.c
@@ -0,0 +1,302 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Ingenic JZ47xx remoteproc driver
+ * Copyright 2019, Paul Cercueil 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "remoteproc_internal.h"
+
+#define REG_AUX_CTRL   0x0
+#define REG_AUX_MSG_ACK0x10
+#define REG_AUX_MSG0x14
+#define REG_CORE_MSG_ACK   0x18
+#define REG_CORE_MSG   0x1C
+
+#define AUX_CTRL_SLEEP BIT(31)
+#define AUX_CTRL_MSG_IRQ_ENBIT(3)
+#define AUX_CTRL_NMI_RESETSBIT(2)
+#define AUX_CTRL_NMI   BIT(1)
+#define AUX_CTRL_SW_RESET  BIT(0)
+
+struct vpu_mem_map {
+   const char *name;
+   unsigned int da;
+   bool direct_io;
+};
+
+struct vpu_mem_info {
+   const struct vpu_mem_map *map;
+   unsigned long len;
+   void __iomem *base;
+};
+
+static const struct vpu_mem_map vpu_mem_map[] = {
+   { "tcsm0", 0x132b, true },
+   { "tcsm1", 0xf400 },
+   { "sram",  0x132f },
+};
+
+/* Device data */
+struct vpu {
+   int irq;
+   struct clk *vpu_clk;
+   struct clk *aux_clk;
+   void __iomem *aux_base;
+   struct vpu_mem_info mem_info[ARRAY_SIZE(vpu_mem_map)];
+   struct device *dev;
+};
+
+static int ingenic_rproc_prepare(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+   int ret;
+
+   ret = clk_prepare_enable(vpu->vpu_clk);
+   if (ret) {
+   dev_err(vpu->dev, "Unable to start VPU clock: %d\n", ret);
+   return ret;
+   }
+
+   ret = clk_prepare_enable(vpu->aux_clk);
+   if (ret) {
+   dev_err(vpu->dev, "Unable to start AUX clock: %d\n", ret);
+   goto err_disable_vpu_clk;
+   }
+
+   return 0;
+
+err_disable_vpu_clk:
+   clk_disable_unprepare(vpu->vpu_clk);
+   return ret;
+}
+
+static void ingenic_rproc_unprepare(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+
+   clk_disable_unprepare(vpu->aux_clk);
+   clk_disable_unprepare(vpu->vpu_clk);
+}
+
+static int ingenic_rproc_start(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+   u32 ctrl;
+
+   enable_irq(vpu->irq);
+
+   /* Reset the AUX and enable message IRQ */
+   ctrl = AUX_CTRL_NMI_RESETS | AUX_CTRL_NMI | AUX_CTRL_MSG_IRQ_EN;
+   writel(ctrl, vpu->aux_base + REG_AUX_CTRL);
+
+   return 0;
+}
+
+static int ingenic_rproc_stop(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+
+   /* Keep AUX in reset mode */
+   writel(AUX_CTRL_SW_RESET, vpu->aux_base + REG_AUX_CTRL);
+
+   disable_irq_nosync(vpu->irq);
+
+   return 0;
+}
+
+static void ingenic_rproc_kick(struct rproc *rproc, int vqid)
+{
+   struct vpu *vpu = rproc->priv;
+
+   writel(vqid, vpu->aux_base + REG_CORE_MSG);
+}
+
+static void *ingenic_rproc_da_to_va(struct rproc *rproc, u64 da, int 
len)

+{
+   struct vpu *vpu = rproc->priv;
+   void __iomem *va = 

[PATCH 3/3] remoteproc: ingenic: Added remoteproc driver

2019-07-21 Thread Paul Cercueil
This driver is used to boot, communicate with and load firmwares to the
MIPS co-processor found in the VPU hardware of the JZ47xx SoCs from
Ingenic.

Signed-off-by: Paul Cercueil 
---
 drivers/remoteproc/Kconfig |   8 +
 drivers/remoteproc/Makefile|   1 +
 drivers/remoteproc/ingenic_rproc.c | 302 +
 3 files changed, 311 insertions(+)
 create mode 100644 drivers/remoteproc/ingenic_rproc.c

diff --git a/drivers/remoteproc/Kconfig b/drivers/remoteproc/Kconfig
index 28ed306982f7..a0be40e2098d 100644
--- a/drivers/remoteproc/Kconfig
+++ b/drivers/remoteproc/Kconfig
@@ -214,6 +214,14 @@ config STM32_RPROC
 
  This can be either built-in or a loadable module.
 
+config INGENIC_RPROC
+   tristate "Ingenic JZ47xx VPU remoteproc support"
+   depends on MIPS || COMPILE_TEST
+   help
+ Say y or m here to support the VPU in the JZ47xx SoCs from Ingenic.
+ This can be either built-in or a loadable module.
+ If unsure say N.
+
 endif # REMOTEPROC
 
 endmenu
diff --git a/drivers/remoteproc/Makefile b/drivers/remoteproc/Makefile
index 00f09e658cb3..6eb0137abbc7 100644
--- a/drivers/remoteproc/Makefile
+++ b/drivers/remoteproc/Makefile
@@ -10,6 +10,7 @@ remoteproc-y  += remoteproc_sysfs.o
 remoteproc-y   += remoteproc_virtio.o
 remoteproc-y   += remoteproc_elf_loader.o
 obj-$(CONFIG_IMX_REMOTEPROC)   += imx_rproc.o
+obj-$(CONFIG_INGENIC_RPROC)+= ingenic_rproc.o
 obj-$(CONFIG_OMAP_REMOTEPROC)  += omap_remoteproc.o
 obj-$(CONFIG_WKUP_M3_RPROC)+= wkup_m3_rproc.o
 obj-$(CONFIG_DA8XX_REMOTEPROC) += da8xx_remoteproc.o
diff --git a/drivers/remoteproc/ingenic_rproc.c 
b/drivers/remoteproc/ingenic_rproc.c
new file mode 100644
index ..a4963158bdd3
--- /dev/null
+++ b/drivers/remoteproc/ingenic_rproc.c
@@ -0,0 +1,302 @@
+// SPDX-License-Identifier: GPL-2.0+
+/*
+ * Ingenic JZ47xx remoteproc driver
+ * Copyright 2019, Paul Cercueil 
+ */
+
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+#include 
+
+#include "remoteproc_internal.h"
+
+#define REG_AUX_CTRL   0x0
+#define REG_AUX_MSG_ACK0x10
+#define REG_AUX_MSG0x14
+#define REG_CORE_MSG_ACK   0x18
+#define REG_CORE_MSG   0x1C
+
+#define AUX_CTRL_SLEEP BIT(31)
+#define AUX_CTRL_MSG_IRQ_ENBIT(3)
+#define AUX_CTRL_NMI_RESETSBIT(2)
+#define AUX_CTRL_NMI   BIT(1)
+#define AUX_CTRL_SW_RESET  BIT(0)
+
+struct vpu_mem_map {
+   const char *name;
+   unsigned int da;
+   bool direct_io;
+};
+
+struct vpu_mem_info {
+   const struct vpu_mem_map *map;
+   unsigned long len;
+   void __iomem *base;
+};
+
+static const struct vpu_mem_map vpu_mem_map[] = {
+   { "tcsm0", 0x132b, true },
+   { "tcsm1", 0xf400 },
+   { "sram",  0x132f },
+};
+
+/* Device data */
+struct vpu {
+   int irq;
+   struct clk *vpu_clk;
+   struct clk *aux_clk;
+   void __iomem *aux_base;
+   struct vpu_mem_info mem_info[ARRAY_SIZE(vpu_mem_map)];
+   struct device *dev;
+};
+
+static int ingenic_rproc_prepare(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+   int ret;
+
+   ret = clk_prepare_enable(vpu->vpu_clk);
+   if (ret) {
+   dev_err(vpu->dev, "Unable to start VPU clock: %d\n", ret);
+   return ret;
+   }
+
+   ret = clk_prepare_enable(vpu->aux_clk);
+   if (ret) {
+   dev_err(vpu->dev, "Unable to start AUX clock: %d\n", ret);
+   goto err_disable_vpu_clk;
+   }
+
+   return 0;
+
+err_disable_vpu_clk:
+   clk_disable_unprepare(vpu->vpu_clk);
+   return ret;
+}
+
+static void ingenic_rproc_unprepare(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+
+   clk_disable_unprepare(vpu->aux_clk);
+   clk_disable_unprepare(vpu->vpu_clk);
+}
+
+static int ingenic_rproc_start(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+   u32 ctrl;
+
+   enable_irq(vpu->irq);
+
+   /* Reset the AUX and enable message IRQ */
+   ctrl = AUX_CTRL_NMI_RESETS | AUX_CTRL_NMI | AUX_CTRL_MSG_IRQ_EN;
+   writel(ctrl, vpu->aux_base + REG_AUX_CTRL);
+
+   return 0;
+}
+
+static int ingenic_rproc_stop(struct rproc *rproc)
+{
+   struct vpu *vpu = rproc->priv;
+
+   /* Keep AUX in reset mode */
+   writel(AUX_CTRL_SW_RESET, vpu->aux_base + REG_AUX_CTRL);
+
+   disable_irq_nosync(vpu->irq);
+
+   return 0;
+}
+
+static void ingenic_rproc_kick(struct rproc *rproc, int vqid)
+{
+   struct vpu *vpu = rproc->priv;
+
+   writel(vqid, vpu->aux_base + REG_CORE_MSG);
+}
+
+static void *ingenic_rproc_da_to_va(struct rproc *rproc, u64 da, int len)
+{
+   struct vpu *vpu = rproc->priv;
+   void __iomem *va = NULL;
+   unsigned int i;
+
+   if (len <= 0)
+