Merge tag 'u-boot-atmel-fixes-2020.07-a' of https://gitlab.denx.de/u-boot/custodians...
[oweals/u-boot.git] / drivers / remoteproc / stm32_copro.c
index ad941f67e866dbb92cb8b9aed6224150a3bad64b..33b574b1bd3d47d3607a28bfc80830a873aa31d6 100644 (file)
@@ -7,11 +7,14 @@
 #include <dm.h>
 #include <errno.h>
 #include <fdtdec.h>
+#include <log.h>
 #include <regmap.h>
 #include <remoteproc.h>
 #include <reset.h>
 #include <syscon.h>
 #include <asm/io.h>
+#include <dm/device_compat.h>
+#include <linux/err.h>
 
 #define RCC_GCR_HOLD_BOOT      0
 #define RCC_GCR_RELEASE_BOOT   1
  * @hold_boot_regmap:  regmap for remote processor reset hold boot
  * @hold_boot_offset:  offset of the register controlling the hold boot setting
  * @hold_boot_mask:    bitmask of the register for the hold boot field
- * @is_running:                is the remote processor running
+ * @rsc_table_addr:    resource table address
  */
 struct stm32_copro_privdata {
        struct reset_ctl reset_ctl;
        struct regmap *hold_boot_regmap;
        uint hold_boot_offset;
        uint hold_boot_mask;
-       bool is_running;
+       ulong rsc_table_addr;
 };
 
 /**
@@ -107,11 +110,13 @@ static int stm32_copro_set_hold_boot(struct udevice *dev, bool hold)
  * stm32_copro_device_to_virt() - Convert device address to virtual address
  * @dev:       corresponding STM32 remote processor device
  * @da:                device address
+ * @size:      Size of the memory region @da is pointing to
  * @return converted virtual address
  */
-static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da)
+static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da,
+                                       ulong size)
 {
-       fdt32_t in_addr = cpu_to_be32(da);
+       fdt32_t in_addr = cpu_to_be32(da), end_addr;
        u64 paddr;
 
        paddr = dev_translate_dma_address(dev, &in_addr);
@@ -120,6 +125,12 @@ static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da)
                return NULL;
        }
 
+       end_addr = cpu_to_be32(da + size - 1);
+       if (dev_translate_dma_address(dev, &end_addr) == OF_BAD_ADDR) {
+               dev_err(dev, "Unable to convert address %ld\n", da + size - 1);
+               return NULL;
+       }
+
        return phys_to_virt(paddr);
 }
 
@@ -133,6 +144,7 @@ static void *stm32_copro_device_to_virt(struct udevice *dev, ulong da)
 static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
 {
        struct stm32_copro_privdata *priv;
+       ulong rsc_table_size;
        int ret;
 
        priv = dev_get_priv(dev);
@@ -147,14 +159,13 @@ static int stm32_copro_load(struct udevice *dev, ulong addr, ulong size)
                return ret;
        }
 
-       /* Support only ELF32 image */
-       ret = rproc_elf32_sanity_check(addr, size);
-       if (ret) {
-               dev_err(dev, "Invalid ELF32 image (%d)\n", ret);
-               return ret;
+       if (rproc_elf32_load_rsc_table(dev, addr, size, &priv->rsc_table_addr,
+                                      &rsc_table_size)) {
+               priv->rsc_table_addr = 0;
+               dev_warn(dev, "No valid resource table for this firmware\n");
        }
 
-       return rproc_elf32_load_image(dev, addr);
+       return rproc_elf32_load_image(dev, addr, size);
 }
 
 /**
@@ -179,7 +190,12 @@ static int stm32_copro_start(struct udevice *dev)
         * rebooting autonomously
         */
        ret = stm32_copro_set_hold_boot(dev, true);
-       priv->is_running = !ret;
+       writel(ret ? TAMP_COPRO_STATE_OFF : TAMP_COPRO_STATE_CRUN,
+              TAMP_COPRO_STATE);
+       if (!ret)
+               /* Store rsc_address in bkp register */
+               writel(priv->rsc_table_addr, TAMP_COPRO_RSC_TBL_ADDRESS);
+
        return ret;
 }
 
@@ -205,7 +221,7 @@ static int stm32_copro_reset(struct udevice *dev)
                return ret;
        }
 
-       priv->is_running = false;
+       writel(TAMP_COPRO_STATE_OFF, TAMP_COPRO_STATE);
 
        return 0;
 }
@@ -223,14 +239,11 @@ static int stm32_copro_stop(struct udevice *dev)
 /**
  * stm32_copro_is_running() - Is the STM32 remote processor running
  * @dev:       corresponding STM32 remote processor device
- * @return 1 if the remote processor is running, 0 otherwise
+ * @return 0 if the remote processor is running, 1 otherwise
  */
 static int stm32_copro_is_running(struct udevice *dev)
 {
-       struct stm32_copro_privdata *priv;
-
-       priv = dev_get_priv(dev);
-       return priv->is_running;
+       return (readl(TAMP_COPRO_STATE) == TAMP_COPRO_STATE_OFF);
 }
 
 static const struct dm_rproc_ops stm32_copro_ops = {