From 04bd27400a3bdb9403c7b66d492a2719deac8b59 Mon Sep 17 00:00:00 2001
From: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
Date: Mon, 27 Sep 2021 14:04:53 +0200
Subject: [PATCH] xtensa/esp32_wdt_lowerhalf.c: Use device specific locks.

Signed-off-by: Abdelatif Guettouche <abdelatif.guettouche@espressif.com>
---
 arch/xtensa/src/esp32/esp32_wdt_lowerhalf.c | 43 ++++++++++++---------
 1 file changed, 25 insertions(+), 18 deletions(-)

diff --git a/arch/xtensa/src/esp32/esp32_wdt_lowerhalf.c b/arch/xtensa/src/esp32/esp32_wdt_lowerhalf.c
index 109480dd3b..b8a0d5f849 100644
--- a/arch/xtensa/src/esp32/esp32_wdt_lowerhalf.c
+++ b/arch/xtensa/src/esp32/esp32_wdt_lowerhalf.c
@@ -33,9 +33,12 @@
 
 #include <nuttx/arch.h>
 #include <nuttx/timers/watchdog.h>
+#include <nuttx/spinlock.h>
 
 #include "xtensa.h"
+
 #include "hardware/esp32_soc.h"
+
 #include "esp32_wdt.h"
 #include "esp32_wdt_lowerhalf.h"
 
@@ -72,14 +75,15 @@
 
 struct esp32_wdt_lowerhalf_s
 {
-  const struct watchdog_ops_s *ops; /* Lower half operations */
-  struct esp32_wdt_dev_s   *wdt;    /* esp32 watchdog driver */
-  uint32_t timeout;                 /* The current timeout */
-  enum wdt_peripherals peripheral;  /* Indicates if it is from RTC or Timer Module */
-  uint32_t lastreset;               /* The last reset time */
-  bool     started;                 /* True: Timer has been started */
-  xcpt_t handler;                   /* User Handler */
-  void   *upper;                    /* Pointer to watchdog_upperhalf_s */
+  const struct watchdog_ops_s *ops;       /* Lower half operations */
+  struct esp32_wdt_dev_s      *wdt;       /* esp32 watchdog driver */
+  uint32_t                    timeout;    /* The current timeout */
+  enum wdt_peripherals        peripheral; /* Indicates if it is from RTC or Timer Module */
+  uint32_t                    lastreset;  /* The last reset time */
+  bool                        started;    /* True: Timer has been started */
+  xcpt_t                      handler;    /* User Handler */
+  void                        *upper;     /* Pointer to watchdog_upperhalf_s */
+  spinlock_t                  lock;       /* Device specific lock */
 };
 
 /****************************************************************************
@@ -217,15 +221,17 @@ static int esp32_wdt_start(struct watchdog_lowerhalf_s *lower)
 
           /* Set the lower half handler and enable interrupt */
 
-          flags = enter_critical_section();
+          flags = spin_lock_irqsave(&priv->lock);
           ESP32_WDT_SETISR(priv->wdt, esp32_wdt_handler, priv);
-          leave_critical_section(flags);
+          spin_unlock_irqrestore(&priv->lock, flags);
+
           ESP32_WDT_ENABLEINT(priv->wdt);
         }
-      flags = enter_critical_section();
+
+      flags = spin_lock_irqsave(&priv->lock);
       priv->lastreset = clock_systime_ticks();
       ESP32_WDT_START(priv->wdt);
-      leave_critical_section(flags);
+      spin_unlock_irqrestore(&priv->lock, flags);
 
       /* Lock it again */
 
@@ -267,9 +273,10 @@ static int esp32_wdt_stop(struct watchdog_lowerhalf_s *lower)
   if (priv->handler != NULL)
     {
       ESP32_WDT_DISABLEINT(priv->wdt);
-      flags = enter_critical_section();
+
+      flags = spin_lock_irqsave(&priv->lock);
       ESP32_WDT_SETISR(priv->wdt, NULL, NULL);
-      leave_critical_section(flags);
+      spin_unlock_irqrestore(&priv->lock, flags);
     }
 
   /* Lock it again */
@@ -310,10 +317,10 @@ static int esp32_wdt_keepalive(struct watchdog_lowerhalf_s *lower)
 
   /* Feed the dog and updates the lastreset variable */
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
   priv->lastreset = clock_systime_ticks();
   ESP32_WDT_FEED(priv->wdt);
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
 
   /* Lock */
 
@@ -514,7 +521,7 @@ static xcpt_t esp32_wdt_capture(struct watchdog_lowerhalf_s *lower,
 
   ESP32_WDT_UNLOCK(priv->wdt);
 
-  flags = enter_critical_section();
+  flags = spin_lock_irqsave(&priv->lock);
 
   /* Save the new user handler */
 
@@ -569,7 +576,7 @@ static xcpt_t esp32_wdt_capture(struct watchdog_lowerhalf_s *lower,
         }
     }
 
-  leave_critical_section(flags);
+  spin_unlock_irqrestore(&priv->lock, flags);
   ESP32_WDT_LOCK(priv->wdt);
   return oldhandler;
 }