[Feature]add MT2731_MP2_MR2_SVN388 baseline version

Change-Id: Ief04314834b31e27effab435d3ca8ba33b499059
diff --git a/src/bsp/lk/lib/watchdog/include/lib/watchdog.h b/src/bsp/lk/lib/watchdog/include/lib/watchdog.h
new file mode 100644
index 0000000..e5bb309
--- /dev/null
+++ b/src/bsp/lk/lib/watchdog/include/lib/watchdog.h
@@ -0,0 +1,85 @@
+/*
+ * Copyright (c) 2013 Google Inc.
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#ifndef __LIB_WATCHDOG_H__
+#define __LIB_WATCHDOG_H__
+
+#include <stdbool.h>
+#include <stdint.h>
+#include <kernel/thread.h>
+#include <kernel/timer.h>
+
+#define WATCHDOG_MAGIC 'wdog'
+
+typedef struct watchdog {
+    uint32_t                magic;
+    const char*             name;
+    bool                    enabled;
+    lk_time_t               timeout;
+    timer_t                 expire_timer;
+} watchdog_t;
+
+/* A global weak-reference to the common watchdog handler.  By default, this
+ * function will dprintf a message indicating which watchdog fired, and then
+ * request a halt using platform_halt, indicating that a SW watchdog was the
+ * reason for the reset.  It is the platform/target's responsibility to either
+ * implement the panic behavior they want when a watchdog fires in
+ * platform_halt, or to replace the implementation of the watchdog handler with
+ * their own appropriate implementation.
+ */
+void watchdog_handler(watchdog_t* dog) __NO_RETURN;
+
+status_t watchdog_init(watchdog_t* dog, lk_time_t timeout, const char* name);
+void     watchdog_set_enabled(watchdog_t* dog, bool enabled);
+void     watchdog_pet(watchdog_t* dog);
+
+/* HW watchdog support.  This is nothing but a simple helper used to
+ * automatically dismiss a platform's HW watchdog using LK timers.  Platforms
+ * must supply
+ *
+ * platform_watchdog_init
+ * platform_watchdog_set_enabled
+ * platform_watchdog_pet
+ *
+ * in order to use the HW watchdog helper functions.  After initialized, users
+ * may enable and disable the HW watchdog whenever appropriate.  The helper will
+ * maintain a timer which dismisses the watchdog at the pet interval recommended
+ * by the platform.  Any programming error which prevents the scheduler timer
+ * mechanism from running properly will eventually result in the watchdog firing
+ * and the system rebooting.  Whenever possible, when using SW based watchdogs,
+ * it is recommended that systems provide platform support for a HW watchdog and
+ * enable the HW watchdog.  SW watchdogs are based on LK timers, and should be
+ * reliable as long as the scheduler and timer mechanism is running properly;
+ * the HW watchdog functionality provided here should protect the system in case
+ * something managed to break timers on LK.
+ */
+
+extern status_t platform_watchdog_init(lk_time_t  target_timeout,
+                                       lk_time_t* recommended_pet_period);
+extern void platform_watchdog_set_enabled(bool enabled);
+extern void platform_watchdog_pet(void);
+
+status_t watchdog_hw_init(lk_time_t timeout);
+void watchdog_hw_set_enabled(bool enabled);
+
+#endif  // __LIB_WATCHDOG_H__
diff --git a/src/bsp/lk/lib/watchdog/rules.mk b/src/bsp/lk/lib/watchdog/rules.mk
new file mode 100644
index 0000000..b1aee7a
--- /dev/null
+++ b/src/bsp/lk/lib/watchdog/rules.mk
@@ -0,0 +1,8 @@
+LOCAL_DIR := $(GET_LOCAL_DIR)
+
+MODULE := $(LOCAL_DIR)
+
+MODULE_SRCS := \
+    $(LOCAL_DIR)/watchdog.c
+
+include make/module.mk
diff --git a/src/bsp/lk/lib/watchdog/watchdog.c b/src/bsp/lk/lib/watchdog/watchdog.c
new file mode 100644
index 0000000..3e31df3
--- /dev/null
+++ b/src/bsp/lk/lib/watchdog/watchdog.c
@@ -0,0 +1,144 @@
+/*
+ * Copyright (c) 2013 Google Inc.
+ * Copyright (c) 2015 Travis Geiselbrecht
+ *
+ * Permission is hereby granted, free of charge, to any person obtaining
+ * a copy of this software and associated documentation files
+ * (the "Software"), to deal in the Software without restriction,
+ * including without limitation the rights to use, copy, modify, merge,
+ * publish, distribute, sublicense, and/or sell copies of the Software,
+ * and to permit persons to whom the Software is furnished to do so,
+ * subject to the following conditions:
+ *
+ * The above copyright notice and this permission notice shall be
+ * included in all copies or substantial portions of the Software.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
+ * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
+ * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
+ * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
+ * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
+ */
+
+#include <assert.h>
+#include <compiler.h>
+#include <err.h>
+#include <platform.h>
+
+#include <kernel/thread.h>
+#include <kernel/timer.h>
+#include <kernel/spinlock.h>
+
+#include <lib/watchdog.h>
+
+static spin_lock_t lock = SPIN_LOCK_INITIAL_VALUE;
+
+__WEAK void watchdog_handler(watchdog_t* dog)
+{
+    dprintf(INFO, "Watchdog \"%s\" (timeout %u mSec) just fired!!\n",
+            dog->name, (uint32_t)dog->timeout);
+    platform_halt(HALT_ACTION_HALT, HALT_REASON_SW_WATCHDOG);
+}
+
+static enum handler_return watchdog_timer_callback(struct timer* timer, lk_time_t now, void* arg)
+{
+    watchdog_handler((watchdog_t*)arg);
+
+    /* We should never get here; watchdog handlers should always be fatal. */
+    DEBUG_ASSERT(false);
+
+    return INT_NO_RESCHEDULE;
+}
+
+status_t watchdog_init(watchdog_t* dog, lk_time_t timeout, const char* name)
+{
+    DEBUG_ASSERT(NULL != dog);
+    DEBUG_ASSERT(INFINITE_TIME != timeout);
+
+    dog->magic   = WATCHDOG_MAGIC;
+    dog->name    = name ? name : "unnamed watchdog";
+    dog->enabled = false;
+    dog->timeout = timeout;
+    timer_initialize(&dog->expire_timer);
+
+    return NO_ERROR;
+}
+
+void watchdog_set_enabled(watchdog_t* dog, bool enabled)
+{
+    spin_lock_saved_state_t state;
+    spin_lock_irqsave(&lock, state);
+
+    DEBUG_ASSERT((NULL != dog) && (WATCHDOG_MAGIC == dog->magic));
+
+    if (dog->enabled == enabled)
+        goto done;
+
+    dog->enabled = enabled;
+    if (enabled)
+        timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
+    else
+        timer_cancel(&dog->expire_timer);
+
+done:
+    spin_unlock_irqrestore(&lock, state);
+}
+
+void watchdog_pet(watchdog_t* dog)
+{
+    spin_lock_saved_state_t state;
+    spin_lock_irqsave(&lock, state);
+
+    DEBUG_ASSERT((NULL != dog) && (WATCHDOG_MAGIC == dog->magic));
+
+    if (!dog->enabled)
+        goto done;
+
+    timer_cancel(&dog->expire_timer);
+    timer_set_oneshot(&dog->expire_timer, dog->timeout, watchdog_timer_callback, dog);
+
+done:
+    spin_unlock_irqrestore(&lock, state);
+}
+
+
+static timer_t   hw_watchdog_timer;
+static bool      hw_watchdog_enabled;
+static lk_time_t hw_watchdog_pet_timeout;
+
+static enum handler_return hw_watchdog_timer_callback(struct timer* timer, lk_time_t now, void* arg)
+{
+    platform_watchdog_pet();
+    return INT_NO_RESCHEDULE;
+}
+
+status_t watchdog_hw_init(lk_time_t timeout)
+{
+    DEBUG_ASSERT(INFINITE_TIME != timeout);
+    timer_initialize(&hw_watchdog_timer);
+    return platform_watchdog_init(timeout, &hw_watchdog_pet_timeout);
+}
+
+void watchdog_hw_set_enabled(bool enabled)
+{
+    spin_lock_saved_state_t state;
+    spin_lock_irqsave(&lock, state);
+
+    if (hw_watchdog_enabled == enabled)
+        goto done;
+
+    hw_watchdog_enabled = enabled;
+    platform_watchdog_set_enabled(enabled);
+    if (enabled)
+        timer_set_periodic(&hw_watchdog_timer,
+                           hw_watchdog_pet_timeout,
+                           hw_watchdog_timer_callback,
+                           NULL);
+    else
+        timer_cancel(&hw_watchdog_timer);
+
+done:
+    spin_unlock_irqrestore(&lock, state);
+}