[Feature] add GA346 baseline version
Change-Id: Ic62933698569507dcf98240cdf5d9931ae34348f
diff --git a/src/tinysys/medmcu/drivers/common/MemMang/mt_heap_2.c b/src/tinysys/medmcu/drivers/common/MemMang/mt_heap_2.c
new file mode 100644
index 0000000..b6c1f13
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/MemMang/mt_heap_2.c
@@ -0,0 +1,292 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+#include <stdlib.h>
+
+/* Defining MPU_WRAPPERS_INCLUDED_FROM_API_FILE prevents task.h from redefining
+all the API functions to use the MPU wrappers. That should only be done when
+task.h is included from an application file. */
+#define MPU_WRAPPERS_INCLUDED_FROM_API_FILE
+
+#include "FreeRTOS.h"
+#include "task.h"
+#include "encoding.h"
+#include "mt_driver_api.h"
+
+#undef MPU_WRAPPERS_INCLUDED_FROM_API_FILE
+
+#if( configSUPPORT_DYNAMIC_ALLOCATION == 0 )
+ #error This file must not be used if configSUPPORT_DYNAMIC_ALLOCATION is 0
+#endif
+
+/* A few bytes might be lost to byte aligning the heap start address. */
+#ifdef CFG_AMP_CORE1_EN
+#define configADJUSTED_HEAP_SIZE (mrv_read_csr(CSR_MHARTID) ? ( configTOTAL_HEAP_SIZE_1 - portBYTE_ALIGNMENT ) : ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT ))
+#else
+#define configADJUSTED_HEAP_SIZE ( configTOTAL_HEAP_SIZE - portBYTE_ALIGNMENT )
+#endif
+
+/*
+ * Initialises the heap structures before their first use.
+ */
+static void prvHeapInit( void );
+
+/* Allocate the memory for the heap. */
+#if( configAPPLICATION_ALLOCATED_HEAP == 1 )
+ /* The application writer has already defined the array used for the RTOS
+ heap - probably so it can be placed in a special segment or address. */
+ extern uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] L1TCM_REGION_RW;
+#ifdef CFG_AMP_CORE1_EN
+ extern uint8_t ucHeap_1[ configTOTAL_HEAP_SIZE_1 ] CORE1_DATA;
+#endif
+#else
+ static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ] L1TCM_REGION_RW;
+#ifdef CFG_AMP_CORE1_EN
+ static uint8_t ucHeap_1[ configTOTAL_HEAP_SIZE_1 ] CORE1_DATA;
+#endif
+#endif /* configAPPLICATION_ALLOCATED_HEAP */
+
+
+/* Define the linked list structure. This is used to link free blocks in order
+of their size. */
+typedef struct A_BLOCK_LINK
+{
+ struct A_BLOCK_LINK *pxNextFreeBlock; /*<< The next free block in the list. */
+ size_t xBlockSize; /*<< The size of the free block. */
+} BlockLink_t;
+
+
+static const uint16_t heapSTRUCT_SIZE = ( ( sizeof ( BlockLink_t ) + ( portBYTE_ALIGNMENT - 1 ) ) & ~portBYTE_ALIGNMENT_MASK );
+#define heapMINIMUM_BLOCK_SIZE ( ( size_t ) ( heapSTRUCT_SIZE * 2 ) )
+
+/* Create a couple of list links to mark the start and end of the list. */
+static BlockLink_t xStart, xEnd;
+
+/* Keeps track of the number of free bytes remaining, but says nothing about
+fragmentation. */
+static size_t xFreeBytesRemaining;
+
+/* STATIC FUNCTIONS ARE DEFINED AS MACROS TO MINIMIZE THE FUNCTION CALL DEPTH. */
+
+/*
+ * Insert a block into the list of free blocks - which is ordered by size of
+ * the block. Small blocks at the start of the list and large blocks at the end
+ * of the list.
+ */
+#define prvInsertBlockIntoFreeList( pxBlockToInsert ) \
+{ \
+BlockLink_t *pxIterator; \
+size_t xBlockSize; \
+ \
+ xBlockSize = pxBlockToInsert->xBlockSize; \
+ \
+ /* Iterate through the list until a block is found that has a larger size */ \
+ /* than the block we are inserting. */ \
+ for( pxIterator = &xStart; pxIterator->pxNextFreeBlock->xBlockSize < xBlockSize; pxIterator = pxIterator->pxNextFreeBlock ) \
+ { \
+ /* There is nothing to do here - just iterate to the correct position. */ \
+ } \
+ \
+ /* Update the list to include the block being inserted in the correct */ \
+ /* position. */ \
+ pxBlockToInsert->pxNextFreeBlock = pxIterator->pxNextFreeBlock; \
+ pxIterator->pxNextFreeBlock = pxBlockToInsert; \
+}
+/*-----------------------------------------------------------*/
+
+void *pvPortMalloc( size_t xWantedSize )
+{
+BlockLink_t *pxBlock, *pxPreviousBlock, *pxNewBlockLink;
+static BaseType_t xHeapHasBeenInitialised = pdFALSE;
+void *pvReturn = NULL;
+
+ vTaskSuspendAll();
+ {
+ /* If this is the first call to malloc then the heap will require
+ initialisation to setup the list of free blocks. */
+ if( xHeapHasBeenInitialised == pdFALSE )
+ {
+ prvHeapInit();
+ xHeapHasBeenInitialised = pdTRUE;
+ }
+
+ /* The wanted size is increased so it can contain a BlockLink_t
+ structure in addition to the requested amount of bytes. */
+ if( xWantedSize > 0 )
+ {
+ xWantedSize += heapSTRUCT_SIZE;
+
+ /* Ensure that blocks are always aligned to the required number of bytes. */
+ if( ( xWantedSize & portBYTE_ALIGNMENT_MASK ) != 0 )
+ {
+ /* Byte alignment required. */
+ xWantedSize += ( portBYTE_ALIGNMENT - ( xWantedSize & portBYTE_ALIGNMENT_MASK ) );
+ }
+ }
+
+ if( ( xWantedSize > 0 ) && ( xWantedSize < configADJUSTED_HEAP_SIZE ) )
+ {
+ /* Blocks are stored in byte order - traverse the list from the start
+ (smallest) block until one of adequate size is found. */
+ pxPreviousBlock = &xStart;
+ pxBlock = xStart.pxNextFreeBlock;
+ while( ( pxBlock->xBlockSize < xWantedSize ) && ( pxBlock->pxNextFreeBlock != NULL ) )
+ {
+ pxPreviousBlock = pxBlock;
+ pxBlock = pxBlock->pxNextFreeBlock;
+ }
+
+ /* If we found the end marker then a block of adequate size was not found. */
+ if( pxBlock != &xEnd )
+ {
+ /* Return the memory space - jumping over the BlockLink_t structure
+ at its start. */
+ pvReturn = ( void * ) ( ( ( uint8_t * ) pxPreviousBlock->pxNextFreeBlock ) + heapSTRUCT_SIZE );
+
+ /* This block is being returned for use so must be taken out of the
+ list of free blocks. */
+ pxPreviousBlock->pxNextFreeBlock = pxBlock->pxNextFreeBlock;
+
+ /* If the block is larger than required it can be split into two. */
+ if( ( pxBlock->xBlockSize - xWantedSize ) > heapMINIMUM_BLOCK_SIZE )
+ {
+ /* This block is to be split into two. Create a new block
+ following the number of bytes requested. The void cast is
+ used to prevent byte alignment warnings from the compiler. */
+ pxNewBlockLink = ( void * ) ( ( ( uint8_t * ) pxBlock ) + xWantedSize );
+
+ /* Calculate the sizes of two blocks split from the single
+ block. */
+ pxNewBlockLink->xBlockSize = pxBlock->xBlockSize - xWantedSize;
+ pxBlock->xBlockSize = xWantedSize;
+
+ /* Insert the new block into the list of free blocks. */
+ prvInsertBlockIntoFreeList( ( pxNewBlockLink ) );
+ }
+
+ xFreeBytesRemaining -= pxBlock->xBlockSize;
+ }
+ }
+
+ traceMALLOC( pvReturn, xWantedSize );
+ }
+ ( void ) xTaskResumeAll();
+
+ #if( configUSE_MALLOC_FAILED_HOOK == 1 )
+ {
+ if( pvReturn == NULL )
+ {
+ extern void vApplicationMallocFailedHook( void );
+ vApplicationMallocFailedHook();
+ }
+ }
+ #endif
+
+ return pvReturn;
+}
+/*-----------------------------------------------------------*/
+
+void vPortFree( void *pv )
+{
+uint8_t *puc = ( uint8_t * ) pv;
+BlockLink_t *pxLink;
+
+ if( pv != NULL )
+ {
+ /* The memory being freed will have an BlockLink_t structure immediately
+ before it. */
+ puc -= heapSTRUCT_SIZE;
+
+ /* This unexpected casting is to keep some compilers from issuing
+ byte alignment warnings. */
+ pxLink = ( void * ) puc;
+
+ vTaskSuspendAll();
+ {
+ /* Add this block to the list of free blocks. */
+ prvInsertBlockIntoFreeList( ( ( BlockLink_t * ) pxLink ) );
+ xFreeBytesRemaining += pxLink->xBlockSize;
+ traceFREE( pv, pxLink->xBlockSize );
+ }
+ ( void ) xTaskResumeAll();
+ }
+}
+/*-----------------------------------------------------------*/
+
+size_t xPortGetFreeHeapSize( void )
+{
+ return xFreeBytesRemaining;
+}
+/*-----------------------------------------------------------*/
+
+void vPortInitialiseBlocks( void )
+{
+ /* This just exists to keep the linker quiet. */
+}
+/*-----------------------------------------------------------*/
+
+static void prvHeapInit( void )
+{
+BlockLink_t *pxFirstFreeBlock;
+uint8_t *pucAlignedHeap;
+
+ xFreeBytesRemaining = configADJUSTED_HEAP_SIZE;
+ /* Ensure the heap starts on a correctly aligned boundary. */
+#ifdef CFG_AMP_CORE1_EN
+ if (mrv_read_csr(CSR_MHARTID) == 1)
+ pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap_1[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
+ else
+#endif
+ {
+ pucAlignedHeap = ( uint8_t * ) ( ( ( portPOINTER_SIZE_TYPE ) &ucHeap[ portBYTE_ALIGNMENT ] ) & ( ~( ( portPOINTER_SIZE_TYPE ) portBYTE_ALIGNMENT_MASK ) ) );
+ }
+
+ /* xStart is used to hold a pointer to the first item in the list of free
+ blocks. The void cast is used to prevent compiler warnings. */
+ xStart.pxNextFreeBlock = ( void * ) pucAlignedHeap;
+ xStart.xBlockSize = ( size_t ) 0;
+
+ /* xEnd is used to mark the end of the list of free blocks. */
+ xEnd.xBlockSize = configADJUSTED_HEAP_SIZE;
+ xEnd.pxNextFreeBlock = NULL;
+
+ /* To start with there is a single free block that is sized to take up the
+ entire heap space. */
+ pxFirstFreeBlock = ( void * ) pucAlignedHeap;
+ pxFirstFreeBlock->xBlockSize = configADJUSTED_HEAP_SIZE;
+ pxFirstFreeBlock->pxNextFreeBlock = &xEnd;
+}
+/*-----------------------------------------------------------*/
diff --git a/src/tinysys/medmcu/drivers/common/bus_tracker/inc/bus_tracker.h b/src/tinysys/medmcu/drivers/common/bus_tracker/inc/bus_tracker.h
new file mode 100644
index 0000000..84d25fe
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/bus_tracker/inc/bus_tracker.h
@@ -0,0 +1,136 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2019. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __BUS_TRACKER_H__
+#define __BUS_TRACKER_H__
+
+#include <encoding.h>
+#include <peripheral.h>
+#ifdef CFG_WDT_SUPPORT
+#include <wdt.h>
+#endif
+#define BUS_TRACKER_DEBUG 1
+#define BUS_TRACKER_LOG 1
+
+#if BUS_TRACKER_DEBUG
+#define TRACKER_DBG(x...) printf(x)
+#else
+#define TRACKER_DBG(x...)
+#endif
+#define BUS_TRACKER_DEBUG 1
+
+#if BUS_TRACKER_LOG
+#define TRACKER_LOG(x...) printf(x)
+#else
+#define TRACKER_LOG(x...)
+#endif
+
+#ifndef CFG_FPGA
+#define BUS_TRACKER_STAGE1_TIMEOUT (19600) /* 1ms @ 196Mhz = 19600 *0x10 */
+#define BUS_TRACKER_STAGE2_TIMEOUT (196000000) /* 16s @ 196Mhz = 19600000 *0x10*/
+#else
+#define BUS_TRACKER_STAGE1_TIMEOUT (19600)
+#define BUS_TRACKER_STAGE2_TIMEOUT (19600)
+#endif
+#define BUS_TRACKER_WATCHPOINT (0)
+#define BUS_TRACKER_WATCHPOINT_MASK (0)
+
+void dump_bus_tracker_status(void);
+unsigned int bus_tracker_handler(void *ptr);
+void bus_tracker_init(void);
+
+/* AXI abort */
+static inline unsigned int axi_get_cause(void)
+{
+ unsigned long reg;
+ reg = mrv_read_csr(CSR_MIMABTCAU);
+ return (unsigned int) reg;
+}
+static inline unsigned int axi_get_address(void)
+{
+ unsigned long reg;
+ reg = mrv_read_csr(CSR_MIMABTADDR);
+ return (unsigned int) reg;
+}
+static inline void axi_clear_status(void)
+{
+ mrv_write_csr(CSR_MIMABTCAU, 0);
+ return;
+}
+
+static inline void bus_tracker_plat_init(void)
+{
+
+/* due to scp bus idle is simulated by register,
+ bus tracker should be initialed before bus transaction start */
+ drv_write_reg32(SCP_DBG_CTRL, TRACKER_FORCE_IDLE);
+
+#ifdef CFG_BUS_TRACKER_INTERRUPT_MODE
+ drv_write_reg32(SCP_TRACKER_CTRL,
+ BUS_WP_SLV_ERR_EN | DBG_AW_TIMEOUT_IRQ_EN |
+ DBG_AR_TIMEOUT_IRQ_EN | DBG_SLV_ERR_EN |
+ DBG_TIMEOUT_EN | DBG_CON_EN);
+
+#else
+ drv_write_reg32(SCP_TRACKER_CTRL,
+ BUS_WP_SLV_ERR_EN | DBG_SLV_ERR_EN | DBG_TIMEOUT_EN
+ | DBG_CON_EN);
+#endif
+}
+
+static inline void bus_tracker_handler_plat_start(void)
+{
+#ifndef CFG_BUS_TRACKER_INTERRUPT_MODE
+ TRACKER_LOG("bus err cause/addr %08x %08x\n", axi_get_cause(),
+ axi_get_address());
+#endif
+}
+static inline void bus_tracker_handler_plat_end(void)
+{
+ axi_clear_status();
+ vic_set_mask(0, 0x0);
+ vic_set_wakeup_mask(0, 0x0);
+#ifdef CFG_WDT_SUPPORT
+ mtk_wdt_set_time_out_value(10); /*assign a small value to make ee sooner */
+ mtk_wdt_restart();
+
+#endif
+ /* Halt CPU */
+ __asm volatile ("wfi");
+}
+
+#endif /* __BUS_TRACKER_H__ */
diff --git a/src/tinysys/medmcu/drivers/common/cache/inc/dmgr.h b/src/tinysys/medmcu/drivers/common/cache/inc/dmgr.h
new file mode 100644
index 0000000..ec551ce
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/cache/inc/dmgr.h
@@ -0,0 +1,130 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek Software")
+ * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
+ * applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __CACHE_DRAM_MANAGEMENT_H__
+#define __CACHE_DRAM_MANAGEMENT_H__
+
+#include <stdint.h>
+#include <FreeRTOS.h>
+#include <task.h>
+
+#define IDX_STACK_PC (6)
+#define IDX_STACK_PSR (7)
+
+#define MAX_DMGR_QUEUE_EVENT (10)
+#define MAX_DMGR_LIST_ITEM MAX_DMGR_QUEUE_EVENT
+
+#define OSTICKS_TO_NS (77)
+#define DURATION_LIMIT_NS (20000000) /* 20ms */
+
+#define UNUSED(x) (void)(x)
+
+#ifdef CFG_DMGR_DEBUG
+/*
+ * Use the flag to categorize the log message according to different operations.
+ * Each bit in uxLogBitmap is a switch for printing message specified by the flag.
+ * The uxLogBitmap is initialized by the config, CFG_DMGR_DEBUG, and the default
+ * value is 9, meaning that log belonged to categories LOG_REGAPI and LOG_LATENCY
+ * will be shown on the screen.
+ */
+#define DMGR_PRINTF(flag, fmt, ...) \
+ do { \
+ if(uxLogBitmap & (1 << flag)) \
+ PRINTF_E("%s <%d> " fmt "\n", pcPrompt, flag, ##__VA_ARGS__); \
+ } while(0)
+#else
+#define DMGR_PRINTF(flag, fmt, ...)
+#endif
+#define DMGR_ERROR(fmt, ...) \
+ PRINTF_E("%s Err, " fmt "\n", pcPrompt, ##__VA_ARGS__); \
+ configASSERT(0);
+#define DMGR_WARN(fmt, ...) \
+ PRINTF_E("%s WARN, " fmt "\n", pcPrompt, ##__VA_ARGS__);
+
+enum dramMgrState {
+ STAT_DRAM_PW_OFF,
+ STAT_DRAM_WARM_UP,
+ STAT_DRAM_PW_ON,
+};
+
+enum dramMgrAction {
+ ACT_DRAM_DETECT_ACCESS,
+ ACT_DRAM_POWER_ON,
+ ACT_DRAM_POWER_OFF,
+
+ ACT_EXCEPT_POWER_ON,
+};
+
+typedef enum dramMgrAccssType {
+ TYPE_INVALID,
+ TYPE_NORMAL,
+ TYPE_EXCEPTION,
+ TYPE_CRITICAL
+} DramMgrAccssType_t;
+
+struct dramMgrQueueEvent {
+ enum dramMgrAction eAction;
+ TaskHandle_t xHandle;
+};
+
+#ifdef CFG_DMGR_DEBUG
+/*
+ * Control DMGR's log by specifying the bits of the control
+ * variable, uxLogBitmap, with the enum, logBitPos
+ */
+enum logBitPos {
+ LOG_TASKTRACE,
+ LOG_ONOFF,
+ LOG_LATENCY,
+ LOG_TRAP,
+ LOG_STATE,
+};
+#endif
+
+extern char *pcPrompt;
+extern UBaseType_t uxLogBitmap;
+
+void vDmgrPortEnRegion(void);
+void vDmgrPortDisRegion(void);
+
+void vDmgrPortEnDRAMFromISR(void);
+void vDmgrPortDisDRAM(void);
+
+int32_t vDmgrPortGetDramAckFromISR(void);
+void vDmgrPortEnDRAMUnblock(void);
+
+DramMgrAccssType_t xDmgrPortAccessType(void);
+
+#endif
diff --git a/src/tinysys/medmcu/drivers/common/cache/inc/dmgr_api.h b/src/tinysys/medmcu/drivers/common/cache/inc/dmgr_api.h
new file mode 100644
index 0000000..9f6d64e
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/cache/inc/dmgr_api.h
@@ -0,0 +1,56 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek Software")
+ * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
+ * applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __DRAM_MANAGEMENT_API_H__
+#define __DRAM_MANAGEMENT_API_H__
+
+#ifdef CFG_CACHE_SUPPORT
+void vDmgrHookDVFSDramOn(void);
+void vDmgrHookDVFSDramOff(void);
+void vDmgrHookIdleApplication(void);
+BaseType_t xDmgrHookSwitchOnDramPower(void);
+BaseType_t xDmgrHookInit(void);
+
+#else
+/* Empty function */
+#define vDmgrHookDVFSDramOn() do {} while(0)
+#define vDmgrHookDVFSDramOff() do {} while(0)
+#define vDmgrHookIdleApplication() do {} while(0)
+#define xDmgrHookSwitchOnDramPower(x) (pdFALSE)
+#define xDmgrHookInit() (pdFALSE)
+
+#endif /* CFG_CACHE_SUPPORT */
+
+#endif /* __DRAM_MANAGEMENT_API_H__ */
diff --git a/src/tinysys/medmcu/drivers/common/cache/src/dmgr.c b/src/tinysys/medmcu/drivers/common/cache/src/dmgr.c
new file mode 100644
index 0000000..f65fb1c
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/cache/src/dmgr.c
@@ -0,0 +1,391 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek Software")
+ * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
+ * applicable license agreements with MediaTek Inc.
+ */
+
+#include <tinysys_config.h>
+#include <FreeRTOS.h>
+#include <task.h>
+#include <timers.h>
+#include <queue.h>
+#ifdef CFG_XGPT_SUPPORT
+#include <interrupt.h>
+#include <xgpt.h>
+#endif
+#include <FreeRTOSConfig.h>
+#include <dmgr.h>
+#include <dmgr_api.h>
+#include <mt_printf.h>
+#include <sleep.h>
+#include <dvfs_common.h>
+
+extern void *pxCurrentTCB;
+
+enum dramMgrState eExceptState;
+char *pcPrompt = "[DMGR]";
+
+#ifdef CFG_DMGR_DEBUG
+UBaseType_t uxLogBitmap = CFG_DMGR_DEBUG;
+#endif /* CFG_DMGR_DEBUG */
+
+/*****************************************************************************
+** CFG_DYNAMIC_DRAM_V2, Switch on/off DRAM in the context of a task.
+*****************************************************************************/
+
+#ifdef CFG_DYNAMIC_DRAM_V2
+
+struct {
+ unsigned long long ullDmgrMaxCost;
+ unsigned long long ullDmgrSendQTimeStamp;
+ unsigned long long ullDmgrLastDetectTimeStamp;
+ unsigned long long ullDmgrLastPowerOnTimeStamp;
+ unsigned long long ullDmgrDetectWfiSTimeStamp;
+ unsigned long long ullDmgrDetectWfiETimeStamp;
+ uint32_t ullDmgrDetectSlpCnt;
+ uint32_t ullDmgrPowerOnSlpCnt;
+ uint32_t ullDmgrDetectWFICnt;
+ uint32_t ullDmgrPowerOnWFICnt;
+ uint32_t ullDmgrTmrCnt;
+} xDmgrCost = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
+
+enum dramMgrState eDramMgrState;
+static QueueHandle_t xDramMgrRxQueue;
+static List_t xDramMgrSuspendList;
+static List_t xDramMgrFreeList;
+static ListItem_t dramMgrItemPool[MAX_DMGR_LIST_ITEM];
+
+static void prvDmgrHwTimerStart(void)
+{
+ struct timer_device *pxDev = id_to_dev(DMGR_TIMER);
+ unsigned int ulBaseAddr = pxDev->base_addr;
+
+ DRV_WriteReg32(ulBaseAddr + TIMER_RST_VAL, DMGR_TIMER_RSTVAL);
+ DRV_SetReg32(ulBaseAddr + TIMER_IRQ_CTRL_REG, TIMER_ENABLE);
+ /* select clock source to clk_32k and enable timer */
+ DRV_SetReg32(ulBaseAddr + TIMER_EN,
+ (TIMER_CLK_SRC_CLK_32K << TIMER_CLK_SRC_SHIFT) |
+ TIMER_IRQ_ENABLE);
+}
+
+static void prvDmgrHwTimerStop(void)
+{
+ struct timer_device *pxDev = id_to_dev(DMGR_TIMER);
+ unsigned int ulBaseAddr = pxDev->base_addr;
+
+ DRV_ClrReg32(ulBaseAddr + TIMER_EN, TIMER_ENABLE);
+ DRV_ClrReg32(ulBaseAddr + TIMER_IRQ_CTRL_REG, TIMER_IRQ_ENABLE);
+ /* act timer interrupt */
+ DRV_SetReg32(ulBaseAddr + TIMER_IRQ_CTRL_REG, TIMER_IRQ_CLEAR);
+ //DRV_WriteReg32(ulBaseAddr + TIMER_RST_VAL, 0);
+}
+
+static void prvDmgrEventSend(TaskHandle_t xHandle, enum dramMgrAction eAction)
+{
+ struct dramMgrQueueEvent xEvent;
+ BaseType_t xHigherPriorityTaskWoken;
+ BaseType_t xRet = pdPASS;
+
+ xEvent.xHandle = (TaskHandle_t) xHandle;
+ xEvent.eAction = eAction;
+ /* send the current tcb to DramMgr to be suspended */
+ xDmgrCost.ullDmgrSendQTimeStamp = read_xgpt_stamp_ns();
+ xRet =
+ xQueueSendFromISR(xDramMgrRxQueue, &xEvent,
+ &xHigherPriorityTaskWoken);
+ configASSERT(xRet == pdTRUE);
+ /* context switching to dram manager */
+ portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
+}
+
+static unsigned int prvDmgrPollingAck(void *arg)
+{
+ uint32_t ulIsAck = vDmgrPortGetDramAckFromISR();
+
+ UNUSED(arg);
+ configASSERT(eDramMgrState == STAT_DRAM_WARM_UP);
+
+ xDmgrCost.ullDmgrTmrCnt++;
+ prvDmgrHwTimerStop();
+ if (ulIsAck) {
+ DMGR_PRINTF(LOG_ONOFF, "p:dram on");
+ prvDmgrEventSend((TaskHandle_t) NULL, ACT_DRAM_POWER_ON);
+ } else
+ prvDmgrHwTimerStart();
+
+ return 0;
+}
+
+static void prvSuspendTask(TaskHandle_t xHandle)
+{
+ ListItem_t *pxDramMgrItem = NULL;
+
+ configASSERT(listLIST_IS_EMPTY(&xDramMgrFreeList) != pdTRUE);
+
+ /* get a empty item from free list */
+ pxDramMgrItem = listGET_HEAD_ENTRY(&xDramMgrFreeList);
+ uxListRemove(pxDramMgrItem);
+
+ /* initialize the item and then insert it into suspend list */
+ vListInitialiseItem(pxDramMgrItem);
+ listSET_LIST_ITEM_VALUE(pxDramMgrItem, (TickType_t) xHandle);
+ vListInsertEnd(&xDramMgrSuspendList, pxDramMgrItem);
+
+ /* as we have recorded the task in suspend list, it can be suspended */
+ vTaskSuspend(xHandle);
+ DMGR_PRINTF(LOG_TASKTRACE, "Suspend %s", pcTaskGetTaskName(xHandle));
+}
+
+static void prvResumeTask(TaskHandle_t xHandle)
+{
+ ListItem_t *pxDramMgrItem = NULL;
+
+ configASSERT(listLIST_IS_EMPTY(&xDramMgrSuspendList) != pdTRUE);
+
+ /* get an item from suspend list */
+ pxDramMgrItem = listGET_HEAD_ENTRY(&xDramMgrSuspendList);
+ xHandle = (TaskHandle_t) listGET_LIST_ITEM_VALUE(pxDramMgrItem);
+ uxListRemove(pxDramMgrItem);
+
+ vTaskResume(xHandle);
+ DMGR_PRINTF(LOG_TASKTRACE, "Resume %s", pcTaskGetTaskName(xHandle));
+
+ /* insert the empty item into free list */
+ vListInitialiseItem(pxDramMgrItem);
+ vListInsertEnd(&xDramMgrFreeList, pxDramMgrItem);
+}
+
+static void prvTaskDramManager(void *pvParameters)
+{
+ struct dramMgrQueueEvent xEvent;
+ enum dramMgrAction eAction;
+ TaskHandle_t xHandle;
+ BaseType_t xRet;
+ unsigned long long ullDiff;
+
+ UNUSED(pvParameters);
+
+ while (1) {
+ xRet = xQueueReceive(xDramMgrRxQueue, &xEvent, portMAX_DELAY);
+ if (xRet != pdPASS) {
+ PRINTF_E("[DMGR] xQueueReceive failed, %ld\n\r", xRet);
+ continue;
+ }
+
+ xHandle = xEvent.xHandle;
+ eAction = xEvent.eAction;
+
+ /*
+ * Normal FSM for DRAM Manager
+ */
+ DMGR_PRINTF(LOG_STATE, "State=%d, Action=%d", eDramMgrState,
+ eAction);
+
+ switch (eDramMgrState) {
+ case STAT_DRAM_PW_OFF:
+ switch (eAction) {
+ case ACT_DRAM_DETECT_ACCESS:
+ vDmgrPortEnDRAMUnblock();
+ xDmgrCost.ullDmgrDetectWfiSTimeStamp = get_last_wfi_s_time();
+ xDmgrCost.ullDmgrDetectWfiETimeStamp = get_last_wfi_e_time();
+
+ /* Record starting time for cost computation */
+ xDmgrCost.ullDmgrLastDetectTimeStamp = read_xgpt_stamp_ns();
+ xDmgrCost.ullDmgrDetectSlpCnt = get_sleep_cnt();
+ xDmgrCost.ullDmgrDetectWFICnt = get_wfi_cnt();
+ xDmgrCost.ullDmgrTmrCnt = 0;
+
+ prvSuspendTask(xHandle);
+ eDramMgrState = STAT_DRAM_WARM_UP;
+ prvDmgrHwTimerStart();
+ break;
+ default:
+ configASSERT(0);
+ }
+
+ break;
+ case STAT_DRAM_WARM_UP:
+ switch (eAction) {
+ case ACT_DRAM_DETECT_ACCESS:
+ prvSuspendTask(xHandle);
+ break;
+ case ACT_DRAM_POWER_ON:
+ /* Supposed there are no events following POWER_ON events,
+ * so task resuming is only done here.
+ */
+ while (listLIST_IS_EMPTY(&xDramMgrSuspendList)
+ != pdTRUE) {
+ prvResumeTask(xHandle);
+ }
+ eDramMgrState = STAT_DRAM_PW_ON;
+
+ /* Calculate the cost and record the max one */
+ xDmgrCost.ullDmgrLastPowerOnTimeStamp = read_xgpt_stamp_ns();
+ xDmgrCost.ullDmgrPowerOnSlpCnt = get_sleep_cnt();
+ xDmgrCost.ullDmgrPowerOnWFICnt = get_wfi_cnt();
+ ullDiff = xDmgrCost.ullDmgrLastPowerOnTimeStamp -
+ xDmgrCost.ullDmgrLastDetectTimeStamp;
+
+ if (ullDiff > xDmgrCost.ullDmgrMaxCost) {
+ xDmgrCost.ullDmgrMaxCost = ullDiff;
+ DMGR_PRINTF(LOG_LATENCY, "update Max Cost = %llu",
+ xDmgrCost.ullDmgrMaxCost);
+#ifdef CFG_IRQ_MONITOR_SUPPORT
+ if (ullDiff > DURATION_LIMIT_NS)
+ configASSERT(0);
+#endif
+ }
+
+ break;
+ default:
+ configASSERT(0);
+ }
+
+ break;
+ case STAT_DRAM_PW_ON:
+ configASSERT(0);
+
+ default:
+ ;
+ }
+ }
+}
+
+#endif
+
+/****************************************************************************/
+
+/*****************************************************************************
+** Hook functions are the functions that must be embedded in some places of
+** the new platform and then deliver the events to the DRAM Manager.
+*****************************************************************************/
+
+BaseType_t xDmgrHookSwitchOnDramPower(void)
+{
+ BaseType_t xRet = pdPASS;
+
+ switch (xDmgrPortAccessType()) {
+ case TYPE_NORMAL:
+#ifdef CFG_DYNAMIC_DRAM_V2
+ /* the access is located in cacheable memory */
+ DMGR_PRINTF(LOG_ONOFF, "p:notify dmgr");
+ prvDmgrEventSend((TaskHandle_t) pxCurrentTCB,
+ ACT_DRAM_DETECT_ACCESS);
+ break;
+#endif
+ case TYPE_EXCEPTION:
+ case TYPE_CRITICAL:
+#ifdef CFG_L1C_ISR_SUPPORT
+ DMGR_PRINTF(LOG_ONOFF, "e:dram on");
+ vDmgrPortEnDRAMFromISR();
+ eExceptState = STAT_DRAM_PW_ON;
+ break;
+#else
+ /* Don't allow access DRAM in Critical Section and Interrupt */
+ //configASSERT(0); let it enter exception and coredump
+#endif
+ default:
+ xRet = pdFALSE;
+ }
+
+ return xRet;
+}
+
+void vDmgrHookIdleApplication(void)
+{
+ taskENTER_CRITICAL();
+ {
+#ifdef CFG_DYNAMIC_DRAM_V2
+ if (eDramMgrState == STAT_DRAM_PW_ON) {
+ vDmgrPortDisDRAM();
+ eDramMgrState = STAT_DRAM_PW_OFF;
+ DMGR_PRINTF(LOG_ONOFF, "p:dram off");
+ }
+#endif
+
+ if (eExceptState == STAT_DRAM_PW_ON) {
+ vDmgrPortDisDRAM();
+ eExceptState = STAT_DRAM_PW_OFF;
+ DMGR_PRINTF(LOG_ONOFF, "e:dram off");
+ }
+ }
+ taskEXIT_CRITICAL();
+}
+
+void vDmgrHookDVFSDramOn(void)
+{
+ vDmgrPortEnRegion();
+}
+
+void vDmgrHookDVFSDramOff(void)
+{
+ vDmgrPortDisRegion();
+}
+
+BaseType_t xDmgrHookInit(void)
+{
+ BaseType_t xRet = pdPASS;
+
+#ifdef CFG_DYNAMIC_DRAM_V2
+ uint32_t i;
+ struct timer_device *pxDev;
+
+ xDramMgrRxQueue =
+ xQueueCreate(MAX_DMGR_QUEUE_EVENT,
+ sizeof (struct dramMgrQueueEvent));
+ vListInitialise(&xDramMgrSuspendList);
+ vListInitialise(&xDramMgrFreeList);
+ /* insert all list items into free list */
+
+ for (i = 0; i < MAX_DMGR_LIST_ITEM; i++) {
+ vListInitialiseItem(&dramMgrItemPool[i]);
+ vListInsertEnd(&xDramMgrFreeList, &dramMgrItemPool[i]);
+ }
+
+ xRet =
+ xTaskCreate(prvTaskDramManager, "DMgr", configMINIMAL_STACK_SIZE,
+ NULL, PRI_DMGR, NULL);
+
+ /* timer for polling the dram ack state by every 1ms */
+ pxDev = id_to_dev(DMGR_TIMER);
+ intc_irq_request(&pxDev->irq, prvDmgrPollingAck, (void *) "DMGR_Timer");
+ prvDmgrHwTimerStop();
+ eDramMgrState = STAT_DRAM_PW_OFF;
+#endif
+
+ eExceptState = STAT_DRAM_PW_OFF;
+ vDmgrPortDisRegion();
+
+ return xRet;
+}
+
+/****************************************************************************/
diff --git a/src/tinysys/medmcu/drivers/common/cache/src/dmgr_port.c b/src/tinysys/medmcu/drivers/common/cache/src/dmgr_port.c
new file mode 100644
index 0000000..66caaca
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/cache/src/dmgr_port.c
@@ -0,0 +1,159 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek Software")
+ * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
+ * applicable license agreements with MediaTek Inc.
+ */
+
+#include <FreeRTOS.h>
+#include <dmgr_api.h>
+#include <dmgr.h>
+#include <peripheral.h>
+#include <irq.h>
+#include <encoding.h>
+#include <mt_printf.h>
+#include <dvfs.h>
+
+#define e_GET_MCAUSE(x) __asm volatile("csrr %0, mcause":"=r"(x)::)
+#define e_GET_MEPC(x) __asm volatile("csrr %0, mepc":"=r"(x)::)
+
+#ifdef GCC_7_2
+#define e_GET_MTVAL(x) __asm volatile("csrr %0, mbadaddr":"=r"(x)::)
+#else
+#define e_GET_MTVAL(x) __asm volatile("csrr %0, mtval":"=r"(x)::)
+#endif /* GCC_7_2 */
+
+/*****************************************************************************
+ * Setting for Platform-Specific DRAM access detect, like MPU
+ ****************************************************************************/
+
+inline void vDmgrPortEnRegion(void)
+{
+#ifdef CFG_MPU_SUPPORT
+#if CFG_L1C_DRAM_SIZE_0
+ mpu_region_enable(MPU_REGION_DRAM_RO);
+ mpu_region_enable(MPU_REGION_DRAM_RW);
+#endif
+#if CFG_L1C_DRAM_SIZE_1
+ mpu_region_enable(MPU_REGION_DRAM);
+#endif
+#endif
+}
+
+inline void vDmgrPortDisRegion(void)
+{
+#ifdef CFG_MPU_SUPPORT
+ mrv_dcache_flush_all();
+#if CFG_L1C_DRAM_SIZE_0
+ mpu_region_disable(MPU_REGION_DRAM_RO);
+ mpu_region_disable(MPU_REGION_DRAM_RW);
+#endif
+#if CFG_L1C_DRAM_SIZE_1
+ mpu_region_disable(MPU_REGION_DRAM);
+#endif
+#endif
+}
+
+/*****************************************************************************
+ * Logic to tell if the execution instruction or load/store data is in DRAM
+ ****************************************************************************/
+
+DramMgrAccssType_t xDmgrPortAccessType(void)
+{
+ uint32_t ulCause, ulMMFAR, ulPC;
+
+ e_GET_MCAUSE(ulCause);
+ e_GET_MTVAL(ulMMFAR);
+ e_GET_MEPC(ulPC);
+
+ DMGR_PRINTF(LOG_TRAP, "mcause=0x%08x, mepc=0x%08x, vtval=0x%08x",
+ (unsigned int) ulCause, (unsigned int) ulPC,
+ (unsigned int) ulMMFAR);
+
+ if ((CFG_L1C_DRAM_ADDR <= ulPC &&
+ ulPC < CFG_L1C_DRAM_ADDR + CFG_L1C_DRAM_SIZE) ||
+ (CFG_L1C_DRAM_ADDR <= ulMMFAR &&
+ ulMMFAR < CFG_L1C_DRAM_ADDR + CFG_L1C_DRAM_SIZE)) {
+ /* if ISR execution locates in the range of cacheable memory */
+ if (is_in_isr()) {
+ DMGR_WARN("Access DRAM in ISR, PC=0x%x, MMFAR=0x%08x!!!",
+ (unsigned int) ulPC, (unsigned int) ulMMFAR);
+ return TYPE_EXCEPTION;
+ }
+
+ /* if dram access is in critical secton */
+ if (xGetCriticalNesting() > 0) {
+ DMGR_WARN
+ ("Access DRAM in CRITICAL SECTION, PC=0x%x, MMFAR=0x%08x!!!",
+ (unsigned int) ulPC, (unsigned int) ulMMFAR);
+ return TYPE_CRITICAL;
+ }
+
+ /* if instruction accessed locates in the range of cacheable memory */
+ if (ulCause == CAUSE_FAULT_FETCH) {
+ return TYPE_NORMAL;
+ }
+ /* if data accessed locates in the range of cacheable memory */
+ if (ulCause == CAUSE_FAULT_LOAD || ulCause == CAUSE_FAULT_STORE) {
+ return TYPE_NORMAL;
+ }
+
+ }
+ return TYPE_INVALID;
+}
+
+/*****************************************************************************
+ * Sync operation porting for DRAM On/Off
+ ****************************************************************************/
+
+inline void vDmgrPortEnDRAMFromISR(void)
+{
+ dvfs_enable_DRAM_resource_from_isr(DRAM_MANAGER_MEM_ID);
+}
+
+inline void vDmgrPortDisDRAM(void)
+{
+ dvfs_disable_DRAM_resource(DRAM_MANAGER_MEM_ID);
+}
+
+/*****************************************************************************
+ * Async operation porting for DRAM On/Off
+ ****************************************************************************/
+
+inline int32_t vDmgrPortGetDramAckFromISR(void)
+{
+ return get_dram_ack_from_isr_for_dmgr();
+}
+
+inline void vDmgrPortEnDRAMUnblock(void)
+{
+ enable_dram_resource_for_dmgr(DRAM_MANAGER_MEM_ID);
+}
diff --git a/src/tinysys/medmcu/drivers/common/cache/test/cache_unit_test.c b/src/tinysys/medmcu/drivers/common/cache/test/cache_unit_test.c
new file mode 100644
index 0000000..f8bda4a
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/cache/test/cache_unit_test.c
@@ -0,0 +1,245 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek Software")
+ * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
+ * applicable license agreements with MediaTek Inc.
+ */
+
+#include <FreeRTOS.h>
+#include <task.h>
+#include <tinysys_config.h>
+#include <mt_printf.h>
+#include <peripheral.h>
+#ifdef CFG_XGPT_SUPPORT
+#include <interrupt.h>
+#include <xgpt.h>
+#endif
+
+#define TEST_PRINTF PRINTF_E
+#define INT_TIMER TMR5
+#define UNUSED(x) (void)(x)
+
+#define DRAM_REGION_RO __attribute__ ((section (".dram_region_ro")))
+#define DRAM_REGION_RW __attribute__ ((section (".dram_region_rw")))
+#define DRAM_REGION_BSS __attribute__ ((section (".dram_region_bss")))
+#define DRAM_REGION_VARIABLE __attribute__ ((section (".dram_region_variable")))
+#define DRAM_REGION_FUNCTION __attribute__ ((section (".dram_region_func")))
+
+DRAM_REGION_VARIABLE int dram_addr_a[256];
+DRAM_REGION_VARIABLE int dram_addr_b[128];
+DRAM_REGION_VARIABLE int dram_addr_c[64];
+volatile int sram_var;
+
+static void task1_s_d(void *pvParameters)
+{
+ unsigned int *dram_addr = (unsigned int *) dram_addr_a;
+ portTickType xLastExecutionTime, xDelayTime;
+ unsigned int task1_param;
+
+ UNUSED(pvParameters);
+ xLastExecutionTime = xTaskGetTickCount();
+ xDelayTime = ((portTickType) 223 / portTICK_RATE_MS);
+
+ do {
+ task1_param = 10000;
+ TEST_PRINTF("...> task 1 works\n");
+
+ while (task1_param) {
+ *dram_addr = 0xaa5555aa;
+ task1_param--;
+ }
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+
+DRAM_REGION_FUNCTION static void task2_d_d(void *pvParameters)
+{
+ unsigned int *dram_addr = (unsigned int *) dram_addr_b;
+ portTickType xLastExecutionTime, xDelayTime;
+ unsigned int task2_param;
+
+ UNUSED(pvParameters);
+ xLastExecutionTime = xTaskGetTickCount();
+ xDelayTime = ((portTickType) 101 / portTICK_RATE_MS);
+
+ do {
+ task2_param = 20000;
+ TEST_PRINTF("...> task 2 works\n");
+
+ while (task2_param) {
+ *dram_addr = 0xaa5555aa;
+ task2_param--;
+ }
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+
+DRAM_REGION_FUNCTION static void task3_d_s(void *pvParameters)
+{
+ unsigned int sram;
+ unsigned int *sram_addr = (unsigned int *) &sram;
+ portTickType xLastExecutionTime, xDelayTime;
+ unsigned int task3_param;
+
+ UNUSED(pvParameters);
+ xLastExecutionTime = xTaskGetTickCount();
+ xDelayTime = ((portTickType) 53 / portTICK_RATE_MS);
+
+ do {
+ task3_param = 10000;
+ TEST_PRINTF("...> task 3 works\n");
+
+ while (task3_param) {
+ *sram_addr = 0xaa5555aa;
+ task3_param--;
+ }
+
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+
+static void task4_s_d(void *pvParameters)
+{
+ unsigned int *dram_addr = (unsigned int *) dram_addr_c;
+ portTickType xLastExecutionTime, xDelayTime;
+ unsigned int task4_param;
+
+ UNUSED(pvParameters);
+ xLastExecutionTime = xTaskGetTickCount();
+ xDelayTime = ((portTickType) 167 / portTICK_RATE_MS);
+
+ *dram_addr = 0xaa5555aa;
+
+ do {
+ task4_param = 10000;
+ TEST_PRINTF("...> task 4 works\n");
+
+ while (task4_param) {
+ sram_var = *dram_addr;
+ task4_param--;
+ }
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+
+DRAM_REGION_FUNCTION static void task5_d_d(void *pvParameters)
+{
+ unsigned int *dram_addr = (unsigned int *) dram_addr_a;
+ portTickType xLastExecutionTime, xDelayTime;
+ unsigned int task5_param;
+
+ UNUSED(pvParameters);
+ xLastExecutionTime = xTaskGetTickCount();
+ xDelayTime = ((portTickType) 313 / portTICK_RATE_MS);
+
+ do {
+ task5_param = 20000;
+ TEST_PRINTF("...> task 5 works\n");
+
+ while (task5_param) {
+ *dram_addr = 0xaa5555aa;
+ task5_param--;
+ }
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+
+DRAM_REGION_FUNCTION static void task6_d_s(void *pvParameters)
+{
+ unsigned int sram;
+ unsigned int *sram_addr = (unsigned int *) &sram;
+ portTickType xLastExecutionTime, xDelayTime;
+ unsigned int task6_param;
+
+ UNUSED(pvParameters);
+ xLastExecutionTime = xTaskGetTickCount();
+ xDelayTime = ((portTickType) 71 / portTICK_RATE_MS);
+
+ do {
+ task6_param = 10000;
+ TEST_PRINTF("...> task 6 works\n");
+
+ while (task6_param) {
+ *sram_addr = 0xaa5555aa;
+ task6_param--;
+ }
+
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+
+static void prvTestTimerStart(void)
+{
+ struct timer_device *pxDev = id_to_dev(INT_TIMER);
+ unsigned int ulBaseAddr = pxDev->base_addr;
+
+ DRV_WriteReg32(ulBaseAddr + TIMER_RST_VAL, TIME_TO_TICK_MS(97));
+ DRV_SetReg32(ulBaseAddr + TIMER_IRQ_CTRL_REG, TIMER_ENABLE);
+ /* select clock source to clk_32k and enable timer */
+ DRV_SetReg32(ulBaseAddr + TIMER_EN,
+ (TIMER_CLK_SRC_CLK_32K << TIMER_CLK_SRC_SHIFT) |
+ TIMER_IRQ_ENABLE);
+}
+
+DRAM_REGION_FUNCTION static unsigned int prvTestTimerIntTask(void *pvParameters)
+{
+ struct timer_device *pxDev = id_to_dev(INT_TIMER);
+ unsigned int ulBaseAddr = pxDev->base_addr;
+
+ UNUSED(pvParameters);
+ DRV_SetReg32(ulBaseAddr + TIMER_IRQ_CTRL_REG, TIMER_IRQ_CLEAR);
+ mbi();
+ TEST_PRINTF("...> timer interrupt\n");
+
+ return 0;
+}
+
+void dmgr_test(void)
+{
+ struct timer_device *pxDev;
+
+ xTaskCreate(task1_s_d, "task1", configMINIMAL_STACK_SIZE, NULL,
+ PRI_TEST, NULL);
+ xTaskCreate(task2_d_d, "task2", configMINIMAL_STACK_SIZE, NULL,
+ PRI_TEST, NULL);
+ xTaskCreate(task3_d_s, "task3", configMINIMAL_STACK_SIZE, NULL,
+ PRI_TEST, NULL);
+ xTaskCreate(task4_s_d, "task4", configMINIMAL_STACK_SIZE, NULL,
+ PRI_TEST, NULL);
+ xTaskCreate(task5_d_d, "task5", configMINIMAL_STACK_SIZE, NULL,
+ PRI_TEST, NULL);
+ xTaskCreate(task6_d_s, "task6", configMINIMAL_STACK_SIZE, NULL,
+ PRI_TEST, NULL);
+
+ pxDev = id_to_dev(INT_TIMER);
+ intc_irq_request(&pxDev->irq, prvTestTimerIntTask, "TST_Timer");
+ prvTestTimerStart();
+}
diff --git a/src/tinysys/medmcu/drivers/common/dma/dma_api.c b/src/tinysys/medmcu/drivers/common/dma/dma_api.c
new file mode 100644
index 0000000..7d1e545
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/dma/dma_api.c
@@ -0,0 +1,253 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+/* Kernel includes. */
+#include "FreeRTOS.h"
+#include "task.h"
+#include <string.h>
+#include <driver_api.h>
+#include <interrupt.h>
+#include <dma_api.h>
+#ifdef CFG_VCORE_DVFS_SUPPORT
+#include <dvfs.h>
+#endif
+#include "irq.h"
+#ifdef CFG_COMMON_WAKELOCK_SUPPORT
+#include <wakelock.h>
+#endif
+#ifdef CFG_XGPT_SUPPORT
+#include <xgpt.h>
+#endif
+
+#include "mt_printf.h"
+
+#define DMA_DEBUG 0
+#define DMA_LARGE_DRAM 0
+
+#if(DMA_DEBUG == 1)
+#define dbgmsg PRINTF_D
+#else
+#define dbgmsg(...)
+#endif
+
+#if DMA_PROFILING
+volatile unsigned int *ITM_CONTROL = (unsigned int *) 0xE0000E80;
+volatile unsigned int *DWT_CONTROL = (unsigned int *) 0xE0001000;
+volatile unsigned int *DWT_CYCCNT = (unsigned int *) 0xE0001004;
+volatile unsigned int *DEMCR = (unsigned int *) 0xE000EDFC;
+
+#define CPU_RESET_CYCLECOUNTER() \
+ do { \
+ *DEMCR = *DEMCR | 0x01000000; \
+ *DWT_CYCCNT = 0; \
+ *DWT_CONTROL = *DWT_CONTROL | 1 ; \
+ } while(0)
+unsigned int start_time;
+unsigned int end_time;
+#endif
+
+#if DMA_TRACE_DEBUG
+#define DMA_TRACE_SECTION __attribute__ ((section (".sync")))
+struct dma_trace_t {
+ unsigned int last_src;
+ unsigned int last_dst;
+ unsigned int last_len;
+ unsigned long long last_timestamp;
+};
+struct dma_trace_t DMA_TRACE_SECTION dma_trace[2] = {{0,0,0,0},{0,0,0,0}};
+#endif
+
+extern void *__sync_start;
+extern struct dma_channel_t dma_channel[NR_GDMA_CHANNEL];
+
+#if DMA_KEEP_AWAKE
+wakelock_t scp_dma_wakelock;
+#endif
+
+typedef struct {
+ uint32_t ap_addr;
+ uint32_t scp_addr;
+} scp_addr_map_t;
+
+static scp_addr_map_t scp_addr_map[] = {
+ {.ap_addr = 0x60000000,.scp_addr = 0x10000000,},
+ {.ap_addr = 0x70000000,.scp_addr = 0x20000000,},
+ {.ap_addr = 0x00000000,.scp_addr = 0x50000000,},
+ {.ap_addr = 0x10000000,.scp_addr = 0x60000000,},
+ {.ap_addr = 0x80000000,.scp_addr = 0x90000000,},
+ {.ap_addr = 0x90000000,.scp_addr = 0xA0000000,},
+ {.ap_addr = 0x20000000,.scp_addr = 0xD0000000,},
+ {.ap_addr = 0x30000000,.scp_addr = 0xE0000000,},
+ {.ap_addr = 0x50000000,.scp_addr = 0xF0000000,},
+};
+
+uint32_t ap_to_scp(uint32_t ap_addr)
+{
+ uint32_t i;
+ uint32_t num = sizeof (scp_addr_map) / sizeof (scp_addr_map_t);
+
+ for (i = 0; i < num; i++)
+ if (scp_addr_map[i].ap_addr == (0xf0000000 & ap_addr))
+ return (scp_addr_map[i].scp_addr |
+ (0x0fffffff & ap_addr));
+
+ PRINTF_E("err ap_addr:0x%x\n", ap_addr);
+
+ return 0;
+}
+
+uint32_t scp_to_ap(uint32_t scp_addr)
+{
+ uint32_t i;
+ uint32_t num = sizeof (scp_addr_map) / sizeof (scp_addr_map_t);
+
+ for (i = 0; i < num; i++)
+ if (scp_addr_map[i].scp_addr == (0xf0000000 & scp_addr))
+ return (scp_addr_map[i].ap_addr |
+ (0x0fffffff & scp_addr));
+
+ PRINTF_E("err scp_addr:0x%x\n", scp_addr);
+
+ return 0;
+}
+
+/*
+ * In order to send the physical address for dma or other masters,
+ * address view in core1 needs to be remapped with a hardware offset.
+ */
+uint32_t scp_get_phy_addr(uint32_t addr)
+{
+ if (mrv_read_csr(CSR_MHARTID) == 1) {
+ if (addr > DRV_Reg32(R_L2TCM_OFFSET_RANGE_0_LOW)
+ && addr < DRV_Reg32(R_L2TCM_OFFSET_RANGE_0_HIGH))
+ addr += DRV_Reg32(R_L2TCM_OFFSET_ADD);
+ }
+
+ return addr;
+}
+
+#if DMA_KEEP_AWAKE
+void dma_wake_lock_init(void)
+{
+ wake_lock_init(&scp_dma_wakelock, "dmawk");
+}
+
+void dma_wake_lock(void)
+{
+ if (!is_in_isr())
+ wake_lock(&scp_dma_wakelock);
+ else
+ wake_lock_FromISR(&scp_dma_wakelock);
+}
+
+void dma_wake_unlock(void)
+{
+ uint32_t scp_sleep_flag;
+ int32_t channel;
+
+ /*scp_sleep_flag :0 release lock, 1:can not release lock */
+ scp_sleep_flag = 0;
+ for (channel = NR_GDMA_CHANNEL - 1; channel >= 0; channel--)
+ if (dma_channel[channel].in_use == 1)
+ scp_sleep_flag = 1;
+
+ if (scp_sleep_flag == 0) {
+ if (!is_in_isr())
+ wake_unlock(&scp_dma_wakelock);
+ else
+ wake_unlock_FromISR(&scp_dma_wakelock);
+ }
+}
+#endif
+
+#if DMA_TRACE_DEBUG
+void scp_dma_tracing(uint32_t dst, uint32_t src, uint32_t len);
+void scp_dma_tracing(uint32_t dst, uint32_t src, uint32_t len)
+{
+ int core_id = mrv_read_csr(CSR_MHARTID);
+
+ dma_trace[core_id].last_dst = dst;
+ dma_trace[core_id].last_src = src;
+ dma_trace[core_id].last_len = len;
+#ifdef CFG_XGPT_SUPPORT
+ dma_trace[core_id].last_timestamp = read_xgpt_stamp_ns();
+#endif
+}
+#endif
+
+void scp_dma_dstCheck(uint32_t dst);
+void scp_dma_dstCheck(uint32_t dst)
+{
+ if (dst < (uint32_t)(&__sync_start))
+ configASSERT(0);
+}
+
+DMA_RESULT scp_dma_transaction(uint32_t dst_addr, uint32_t src_addr,
+ uint32_t len, int8_t scp_dma_id,
+ int32_t ch)
+{
+ DMA_RESULT ret = 0;
+
+ dst_addr = scp_get_phy_addr(dst_addr);
+ src_addr = scp_get_phy_addr(src_addr);
+#if DMA_TRACE_DEBUG
+ scp_dma_tracing(dst_addr, src_addr, len);
+#endif
+ scp_dma_dstCheck(dst_addr);
+ ret =
+ dma_transaction(dst_addr, src_addr, len, scp_dma_id, ch, NULL, SYNC);
+
+ return ret;
+}
+
+DMA_RESULT scp_dma_transaction_dram(uint32_t dst_addr, uint32_t src_addr,
+ uint32_t len, int8_t scp_dma_id,
+ int32_t ch)
+{
+ DMA_RESULT ret;
+
+#ifdef CFG_VCORE_DVFS_SUPPORT
+ dvfs_enable_DRAM_resource(DMA_MEM_ID);
+#endif
+ dst_addr = scp_get_phy_addr(dst_addr);
+ src_addr = scp_get_phy_addr(src_addr);
+#if DMA_TRACE_DEBUG
+ scp_dma_tracing(dst_addr, src_addr, len);
+#endif
+ scp_dma_dstCheck(dst_addr);
+ ret =
+ dma_transaction(dst_addr, src_addr, len, scp_dma_id, ch, NULL, SYNC);
+#ifdef CFG_VCORE_DVFS_SUPPORT
+ dvfs_disable_DRAM_resource(DMA_MEM_ID);
+#endif
+ return ret;
+}
+
diff --git a/src/tinysys/medmcu/drivers/common/dma/inc/dma_api.h b/src/tinysys/medmcu/drivers/common/dma/inc/dma_api.h
new file mode 100644
index 0000000..ada53fd
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/dma/inc/dma_api.h
@@ -0,0 +1,50 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+#ifndef _DMA_API_H_
+#define _DMA_API_H_
+
+#include <mt_dma.h>
+#include <dma.h>
+
+DMA_RESULT scp_dma_transaction_dram(uint32_t dst_addr, uint32_t src_addr,
+ uint32_t len, int8_t scp_dma_id,
+ int32_t ch);
+DMA_RESULT scp_dma_transaction(uint32_t dst_addr, uint32_t src_addr,
+ uint32_t len, int8_t scp_dma_id,
+ int32_t ch);
+
+uint32_t ap_to_scp(uint32_t ap);
+uint32_t scp_to_ap(uint32_t scp);
+uint32_t scp_get_phy_addr(uint32_t addr);
+void scp_remap_init(void);
+
+#endif /* !_DMA_API_H_ */
diff --git a/src/tinysys/medmcu/drivers/common/dvfs/inc/dvfs_common.h b/src/tinysys/medmcu/drivers/common/dvfs/inc/dvfs_common.h
new file mode 100644
index 0000000..728d880
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/dvfs/inc/dvfs_common.h
@@ -0,0 +1,115 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+#ifndef _DVFS_COMMON_
+#define _DVFS_COMMON_
+
+#ifdef CFG_DMA_SUPPORT
+#include <mt_dma.h>
+#endif
+#include <sleep.h>
+#ifdef CFG_XGPT_SUPPORT
+#include <xgpt.h>
+#endif
+
+/***********************************************************************************
+** Definitions
+************************************************************************************/
+
+/* DVFS Status */
+#define DVFS_STATUS_OK 0
+#define DVFS_STATUS_BUSY -1
+#define DVFS_REQUEST_SAME_CLOCK -2
+#define DVFS_STATUS_ERR -3
+#define DVFS_STATUS_TIMEOUT -4
+#define DVFS_CLK_ERROR -5
+#define DVFS_STATUS_CMD_FIX -6
+#define DVFS_STATUS_CMD_LIMITED -7
+#define DVFS_STATUS_CMD_DISABLE -8
+
+#define SCP_IDLE_MODE 0
+#define SCP_SLEEP_MODE 1
+
+#define SCP_ENABLE 1
+#define SCP_DISABLE 0
+
+#define NEED_WAIT 1
+#define NO_WAIT 0
+
+/***********************************************************************************
+** Enum for Time Profiling
+************************************************************************************/
+enum dvfs_profile_mode {
+ SLP_TMR_M = 0,
+ WK_TMR_M = 1,
+ RTOS_TMR_M = 2,
+ WAIT_TMR_M= 3,
+ UPDATE_TMR_M = 4,
+ BLOCK_TMR_M = 5,
+ DVFS_TMR_M = 6,
+ WFI_TMR_M = 7,
+ DRAM_TMR_M = 8,
+ PROFILE_MODE_NUM = 9,
+};
+
+enum dvfs_profile_type {
+ TMR_START_T = 0,
+ TMR_END_T,
+ TMR_DURATION_T,
+ TMR_RESET_T,
+ TMR_RESTART_T,
+ PROFILE_TYPE_NUM,
+};
+
+/*****************************************
+* ADB CMD Control APIs
+****************************************/
+void dvfs_debug_set(int id, void *data, unsigned int len);
+
+/*****************************************
+ * AP/INFRA Resource APIs
+ ****************************************/
+#ifdef CFG_DMA_SUPPORT
+void enable_AP_resource(scp_reserve_mem_id_t dma_id);
+void disable_AP_resource(scp_reserve_mem_id_t dma_id);
+void enable_infra(scp_reserve_mem_id_t dma_id, uint32_t wait_ack);
+void disable_infra(scp_reserve_mem_id_t dma_id);
+#endif
+
+/*****************************************
+ * Time Profiling APIs
+ ****************************************/
+void dvfs_time_profiling(enum dvfs_profile_mode mode, enum dvfs_profile_type type);
+uint64_t dvfs_get_time_duarion(enum dvfs_profile_mode mode);
+void dvfs_dump_time_profiling(enum dvfs_profile_mode mode);
+uint64_t get_last_wfi_s_time(void);
+uint64_t get_last_wfi_e_time(void);
+#endif /* _DVFS_COMMON_ */
diff --git a/src/tinysys/medmcu/drivers/common/dvfs/src/dvfs_common.c b/src/tinysys/medmcu/drivers/common/dvfs/src/dvfs_common.c
new file mode 100644
index 0000000..e36aa9a
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/dvfs/src/dvfs_common.c
@@ -0,0 +1,247 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+#include <FreeRTOS.h>
+#include <task.h>
+#include <stdio.h>
+#include <driver_api.h>
+#include <interrupt.h>
+#include <mtk_sem.h>
+#include <mt_printf.h>
+#ifdef CFG_XGPT_SUPPORT
+#include <xgpt.h>
+#endif
+#include <dvfs.h>
+#include <dvfs_common.h>
+
+#define DVFS_DEBUG 0
+
+#if DVFS_DEBUG
+#define DVFSLOG(fmt, arg...) \
+ do { \
+ PRINTF_D(DVFSTAG fmt, ##arg); \
+ } while (0)
+#else
+#define DVFSLOG(...)
+#endif
+
+#if DVFS_DEBUG
+#define DVFSFUC(fmt, arg...) \
+ do { \
+ PRINTF_D(DVFSTAG "%s("fmt")\n", __func__, ##arg); \
+ } while (0)
+#else
+#define DVFSFUC(...)
+#endif
+
+#if DVFS_DEBUG
+#define DVFSDBG(fmt, arg...) \
+ do { \
+ PRINTF_W(DVFSTAG fmt, ##arg); \
+ } while (0)
+#else
+#define DVFSDBG(fmt, arg...)
+#endif
+
+#ifdef CFG_XGPT_SUPPORT
+static volatile uint64_t dvfs_profile_time[PROFILE_MODE_NUM][PROFILE_TYPE_NUM];
+
+static char dvfs_profile_name[PROFILE_MODE_NUM][10] = {
+ "SLEEP",
+ "WAKE-UP",
+ "RTOS",
+ "WAIT-ISR",
+ "UPDATE",
+ "BLOCK",
+ "DVFS",
+ "WFI",
+ "DRAM",
+};
+#endif
+
+extern int g_scp_dvfs_debug;
+
+/*****************************************
+* ADB CMD Control APIs
+****************************************/
+void dvfs_debug_set(int id, void *data, unsigned int len)
+{
+ (void)id;
+ (void)len;
+ uint8_t *msg = (uint8_t *) data;
+
+ g_scp_dvfs_debug = *msg;
+ DVFSLOG("set g_scp_dvfs_debug = %d\n", *msg);
+}
+
+/*****************************************
+ * AP/INFRA Resource APIs
+ ****************************************/
+#ifdef CFG_DMA_SUPPORT
+void enable_clk_bus(scp_reserve_mem_id_t dma_id)
+{
+ DVFSFUC("%d", dma_id);
+ taskENTER_CRITICAL();
+ enable_infra(dma_id, NEED_WAIT);
+ taskEXIT_CRITICAL();
+}
+
+void disable_clk_bus(scp_reserve_mem_id_t dma_id)
+{
+ DVFSFUC("%d", dma_id);
+ taskENTER_CRITICAL();
+ disable_infra(dma_id);
+ taskEXIT_CRITICAL();
+}
+
+void enable_clk_bus_from_isr(scp_reserve_mem_id_t dma_id)
+{
+ DVFSFUC("%d", dma_id);
+ enable_infra(dma_id, NEED_WAIT);
+}
+
+void disable_clk_bus_from_isr(scp_reserve_mem_id_t dma_id)
+{
+ DVFSFUC("%d", dma_id);
+ disable_infra(dma_id);
+}
+
+void dvfs_disable_DRAM_resource(scp_reserve_mem_id_t dma_id)
+{
+ DVFSFUC("%d", dma_id);
+ taskENTER_CRITICAL();
+ disable_AP_resource(dma_id);
+ disable_infra(dma_id);
+ taskEXIT_CRITICAL();
+}
+
+/*****************************************
+ * Time Profiling APIs
+ ****************************************/
+static bool __dvfs_chk_profile_cond(enum dvfs_profile_mode mode,
+ enum dvfs_profile_type type)
+{
+ if (mode >= PROFILE_MODE_NUM || type >= PROFILE_TYPE_NUM)
+ return false;
+
+#if !(SLEEP_TIMER_PROFILING)
+ if (mode == SLP_TMR_M || mode == WK_TMR_M || mode == RTOS_TMR_M)
+ return false;
+#endif
+
+#if !(DVFS_TIMER_PROFILING)
+ if (mode == DVFS_TMR_M)
+ return false;
+#endif
+
+#if !(WFI_TIMER_PROFILING)
+ if (mode == WFI_TMR_M)
+ return false;
+#endif
+
+#if !(DRAM_TIMER_PROFILING)
+ if (mode == DRAM_TMR_M)
+ return false;
+#endif
+ return true;
+}
+
+void dvfs_time_profiling(enum dvfs_profile_mode mode,
+ enum dvfs_profile_type type)
+{
+#ifdef CFG_XGPT_SUPPORT
+ uint64_t t1, t2;
+
+ if (!__dvfs_chk_profile_cond(mode, type))
+ return;
+
+ switch (type) {
+ case TMR_START_T:
+ case TMR_END_T:
+ dvfs_profile_time[mode][type] = read_xgpt_stamp_ns();
+ break;
+ case TMR_DURATION_T:
+ t2 = dvfs_profile_time[mode][TMR_END_T];
+ t1 = dvfs_profile_time[mode][TMR_START_T];
+ dvfs_profile_time[mode][type] = (t2 -t1) / 1000;
+ break;
+ case TMR_RESET_T:
+ dvfs_profile_time[mode][type] = 0;
+ break;
+ case TMR_RESTART_T:
+ dvfs_profile_time[mode][TMR_START_T] =
+ dvfs_profile_time[mode][TMR_END_T];
+ break;
+ default:
+ PRINTF_W("this time profiling type not support.\n");
+ break;
+ }
+#endif
+}
+
+uint64_t dvfs_get_time_duarion(enum dvfs_profile_mode mode)
+{
+#ifdef CFG_XGPT_SUPPORT
+ if (__dvfs_chk_profile_cond(mode, TMR_DURATION_T)) {
+ dvfs_time_profiling(mode, TMR_DURATION_T);
+
+ return dvfs_profile_time[mode][TMR_DURATION_T];
+ }
+#endif
+ return 0;
+}
+
+void dvfs_dump_time_profiling(enum dvfs_profile_mode mode)
+{
+#ifdef CFG_XGPT_SUPPORT
+ if (!__dvfs_chk_profile_cond(mode, TMR_DURATION_T))
+ return;
+
+ dvfs_time_profiling(mode, TMR_DURATION_T);
+
+ PRINTF_E("%s sw time: %llu(tick) => %llu(tick) = %lluus\n",
+ dvfs_profile_name[mode],
+ dvfs_profile_time[mode][TMR_START_T],
+ dvfs_profile_time[mode][TMR_END_T],
+ dvfs_profile_time[mode][TMR_DURATION_T]);
+#endif
+}
+#endif
+
+uint64_t get_last_wfi_s_time(void)
+{
+ return dvfs_profile_time[WFI_TMR_M][TMR_START_T];
+}
+
+uint64_t get_last_wfi_e_time(void)
+{
+ return dvfs_profile_time[WFI_TMR_M][TMR_END_T];
+}
diff --git a/src/tinysys/medmcu/drivers/common/err_info/scp_err_info.c b/src/tinysys/medmcu/drivers/common/err_info/scp_err_info.c
new file mode 100644
index 0000000..ade041a
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/err_info/scp_err_info.c
@@ -0,0 +1,65 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+
+/******************************************************************************
+******************************************************************************/
+#include "scp_err_info.h"
+#include "encoding.h"
+#include <ipi.h>
+#include "ipi_id.h"
+#include "mbox_pin.h"
+
+
+/******************************************************************************
+* Note that this function cannot be called in the ISR context.
+******************************************************************************/
+int scp_send_err_to_ap(error_info *info)
+{
+ int ret;
+
+ if (!info)
+ return IPI_NO_MEMORY;
+
+ /* Ensure the context[] is terminated by the NULL character. */
+ info->context[ERR_MAX_CONTEXT_LEN - 1] = '\0';
+
+ if (mrv_read_csr(CSR_MHARTID) == 0)
+ ret = ipi_send(IPI_OUT_SCP_ERROR_INFO_0, info,
+ PIN_OUT_SIZE_SCP_ERROR_INFO_0, 0);
+ else
+ ret = ipi_send(IPI_OUT_SCP_ERROR_INFO_1, info,
+ PIN_OUT_SIZE_SCP_ERROR_INFO_1, 0);
+
+ return ret;
+}
+
+
diff --git a/src/tinysys/medmcu/drivers/common/err_info/scp_err_info.h b/src/tinysys/medmcu/drivers/common/err_info/scp_err_info.h
new file mode 100644
index 0000000..be0b552
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/err_info/scp_err_info.h
@@ -0,0 +1,103 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+
+#ifndef _SCP_ERR_INFO_H_
+#define _SCP_ERR_INFO_H_
+
+
+/******************************************************************************
+******************************************************************************/
+#include <stdint.h> // for uint32_t
+
+
+/******************************************************************************
+* The following definitions are used in error_info::case_id.
+******************************************************************************/
+typedef enum err_case_id_t {
+ ERR_CASE_ACC_GYR_INIT = 926006001,
+ ERR_CASE_ACC_INIT = 926006002,
+ ERR_CASE_GYR_INIT = 926006003,
+ ERR_CASE_MAG_INIT = 926006004,
+ ERR_CASE_ALS_PS_INIT = 926006005,
+ ERR_CASE_ALS_INIT = 926006006,
+ ERR_CASE_PS_INIT = 926006007,
+ ERR_CASE_BARO_INIT = 926006008,
+ ERR_CASE_I2C = 926006009,
+ ERR_CASE_SPI = 926006010,
+ ERR_CASE_DEV_CHECK = 926006011,
+ ERR_CASE_UNKNOWN = 0xffffffff
+} err_case_id_t;
+
+
+/******************************************************************************
+* The following definitions are used in error_info::sensor_id.
+******************************************************************************/
+typedef enum err_sensor_id_t {
+ ERR_SENSOR_ACC_GYR = 0x00000001,
+ ERR_SENSOR_ACC = 0x00000002,
+ ERR_SENSOR_GYR = 0x00000003,
+ ERR_SENSOR_MAG = 0x00000004,
+ ERR_SENSOR_ALS_PS = 0x00000005,
+ ERR_SENSOR_ALS = 0x00000006,
+ ERR_SENSOR_PS = 0x00000007,
+ ERR_SENSOR_BARO = 0x00000008,
+ ERR_SENSOR_I2C = 0x00000009,
+ ERR_SENSOR_SPI = 0x0000000a,
+ ERR_SENSOR_UNKNOWN = 0xffffffff
+} err_sensor_id_t;
+
+
+/******************************************************************************
+* The following definitions are used in error_info::context[].
+******************************************************************************/
+#define ERR_CONTEXT_LSM6DS3 "ST-LSM6DS3"
+
+
+#define ERR_MAX_CONTEXT_LEN 32
+
+typedef struct error_info
+{
+ err_case_id_t case_id;
+ err_sensor_id_t sensor_id;
+ char context[ERR_MAX_CONTEXT_LEN];
+} error_info;
+
+
+/******************************************************************************
+* Note that this function cannot be called in the ISR context.
+******************************************************************************/
+__attribute__((weak)) int scp_send_err_to_ap(error_info *info);
+
+
+#endif // _SCP_ERR_INFO_H_
+
+
diff --git a/src/tinysys/medmcu/drivers/common/mbox/inc/scp_ipi.h b/src/tinysys/medmcu/drivers/common/mbox/inc/scp_ipi.h
new file mode 100644
index 0000000..ed0a0a7
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/mbox/inc/scp_ipi.h
@@ -0,0 +1,83 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2019. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __SCP_IPI_H__
+#define __SCP_IPI_H__
+
+#include <ipi.h>
+#include <ipi_id.h>
+#include "ipi_legacy_wrap.h"
+
+enum ipi_status {
+ ERROR = -1,
+ DONE,
+ BUSY,
+};
+
+enum ipi_dir {
+ IPI_SCP2AP = 0,
+ IPI_SCP2SPM,
+ IPI_SCP2CONN,
+ IPI_SCP2MD
+};
+
+struct ipi_wrapper {
+ uint8_t out_id_0;
+ uint8_t out_id_1;
+ uint8_t in_id_0;
+ uint8_t in_id_1;
+ uint32_t out_size;
+ uint32_t in_size;
+ void *msg;
+};
+
+#define IPI_WAIT_LEGACY 0xFFFFFFFF //retry with maximum times
+
+typedef enum ipi_status ipi_status;
+typedef void(*ipi_handler_t)(int id, void * data, unsigned int len);
+
+
+ipi_status scp_ipi_send(enum ipi_id id, void* buf, uint32_t len, uint32_t wait,
+ enum ipi_dir dir);
+ipi_status scp_ipi_registration(enum ipi_id id, ipi_handler_t handler,
+ const char *name);
+
+void scp_legacy_ipi_init(void);
+void ipi_awake_init(void);
+uint32_t is_ipi_busy(void);
+
+#endif
diff --git a/src/tinysys/medmcu/drivers/common/mbox/ipi_port.c b/src/tinysys/medmcu/drivers/common/mbox/ipi_port.c
new file mode 100644
index 0000000..b7d0ae3
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/mbox/ipi_port.c
@@ -0,0 +1,204 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2019. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+#include "tinysys_reg.h"
+#include "mbox_common_type.h"
+#include "mbox_common.h"
+#ifdef CFG_XGPT_SUPPORT
+#include <xgpt.h>
+#endif
+
+#ifdef CFG_LEGACY_IPI_SUPPORT
+#include "scp_ipi.h"
+#else
+#include <ipi.h>
+#include <ipi_id.h>
+#endif
+
+
+#ifdef CFG_IPI_TEST
+#include "ipi_id.h"
+#include "mbox_common.h"
+#include "mt_printf.h"
+#endif
+
+#ifdef CFG_COMMON_WAKELOCK_SUPPORT
+#include <wakelock.h>
+#endif
+
+#ifdef CFG_PBFR_SUPPORT
+#include "mt_pbfr.h"
+#endif
+
+wakelock_t ap_wakelock;
+wakelock_t connsys_wakelock;
+unsigned int msg_test;
+#ifdef CFG_PBFR_SUPPORT
+int pbfr_start = 0; //pbfr control flag
+#endif
+
+unsigned long long ipi_get_ts(void);
+void ipi_notify_receiver(int ipi_id);
+void ipi_mdelay(unsigned long ms);
+void ipi_platform_init(void);
+
+
+/**
+ * @brief check SCP -> AP IPI is using now
+ * @return pdFALSE, IPI is NOT using now
+ * @return pdTRUE, IPI is using, and AP does not receive the IPI yet.
+ */
+uint32_t is_ipi_busy(void)
+{
+ int i;
+
+ for (i = 0; i < IPI_MBOX_TOTAL; i++)
+ if (DRV_Reg32(mbox_table[i].send_status_reg))
+ return pdTRUE;
+
+ return pdFALSE;
+}
+
+unsigned long long ipi_get_ts(void)
+{
+#ifdef CFG_XGPT_SUPPORT
+ return read_xgpt_stamp_ns();
+#else
+ return 0;
+#endif
+}
+
+/**
+ * @brief send notify to SPM to wakeup AP
+ */
+void ipi_notify_receiver(int ipi_id)
+{
+ /* wakeup APMCU */
+ DRV_SetReg32(SCP2SPM_IPC_SET, 0x1);
+
+ return;
+}
+
+void ipi_mdelay(unsigned long ms)
+{
+#ifdef CFG_XGPT_SUPPORT
+ mdelay(ms);
+#else
+ // do busy waiting w/o xgpt ...
+#endif
+}
+
+/**
+ * @brief AP wakeup SCP and keep SCP awake
+ */
+static void infra_irq_handler(void)
+{
+ unsigned int reg_val;
+
+ reg_val = DRV_Reg32(INFRA_IRQ_SET) & 0xf;
+
+ /* clr interrupt as early as possible to let AP leave busy waiting */
+ DRV_WriteReg32(INFRA_IRQ_CLR, reg_val);
+
+ if (reg_val & (1 << AP_AWAKE_LOCK))
+ wake_lock_FromISR(&ap_wakelock);
+
+ if (reg_val & (1 << AP_AWAKE_UNLOCK))
+ wake_unlock_FromISR(&ap_wakelock);
+
+ if (reg_val & (1 << CONNSYS_AWAKE_LOCK))
+ wake_lock_FromISR(&connsys_wakelock);
+
+ if (reg_val & (1 << CONNSYS_AWAKE_UNLOCK))
+ wake_unlock_FromISR(&connsys_wakelock);
+
+}
+
+void ipi_awake_init(void)
+{
+ wake_lock_init(&ap_wakelock, "AP_W");
+ wake_lock_init(&connsys_wakelock, "CO_W");
+ if (intc_irq_request(&INTC_IRQ_INFRA, (void *)infra_irq_handler, NULL))
+ PRINTF_E("infra irq request failed\n");
+ intc_irq_wakeup_set(&INTC_IRQ_INFRA, 1);
+}
+
+static void scp_ipi_debug(unsigned int id, void *prdata, void *data)
+{
+ unsigned int buffer;
+
+ buffer = *(unsigned int *)data;
+ PRINTF_D("scp get debug ipi buf=%u, id=%u\n", buffer, id);
+#ifdef CFG_PBFR_SUPPORT
+ if (pbfr_start == PBFR_STOP) {
+ pbfr_start = PBFR_START;
+ PRINTF_D("pbfr start load info....\n");
+ pbfr_start_loadinfo(0);
+ } else if (pbfr_start == PBFR_START) {
+ pbfr_start = PBFR_REPORT;
+ pbfr_report_loadinfo(0);
+ } else if (pbfr_start == PBFR_REPORT) {
+ pbfr_start = PBFR_STOP;
+ PRINTF_D("pbfr stop load info.\n");
+ pbfr_stop_loadinfo();
+ }
+#endif
+}
+
+void ipi_platform_init(void)
+{
+ int ret = -1;
+
+ ipi_init();
+#ifdef CFG_LEGACY_IPI_SUPPORT
+ scp_legacy_ipi_init();
+#endif
+
+#ifdef CFG_AMP_CORE1_EN
+ if (mrv_read_csr(CSR_MHARTID) == 0)
+#endif
+ {
+ ipi_awake_init();
+ }
+
+ if (mrv_read_csr(CSR_MHARTID) == 0)
+ ret = ipi_register(IPI_IN_TEST_0, scp_ipi_debug, 0, &msg_test);
+ else
+ ret = ipi_register(IPI_IN_TEST_1, scp_ipi_debug, 0, &msg_test);
+ if (ret)
+ PRINTF_E("ipi test register failed, ret %d\n", ret);
+}
diff --git a/src/tinysys/medmcu/drivers/common/mbox/mbox_port.c b/src/tinysys/medmcu/drivers/common/mbox/mbox_port.c
new file mode 100644
index 0000000..a34e38a
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/mbox/mbox_port.c
@@ -0,0 +1,211 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2019. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+#include "tinysys_reg.h"
+#include "mbox_common_type.h"
+#include "mbox_common.h"
+#include "mbox_platform.h"
+#include "mbox_pin.h"
+#include "interrupt.h"
+#include "mt_printf.h"
+#include "ipi_id.h"
+#include "driver_api.h"
+#include "mt_printf.h"
+#include "irq.h"
+#include "ipi_table.h"
+#ifdef CFG_XGPT_SUPPORT
+#include <xgpt.h>
+#endif
+
+#define DIV_ROUND_UP(x, y) (((x) + (y) - 1) / (y))
+
+void init_pin_number(void);
+unsigned int find_mbox_num(unsigned int irq_num);
+int set_mbox_64d(unsigned int mbox, unsigned int is64d, void *data);
+void mbox_setup_pin_table(int mbox);
+void dump_pin_table(int8_t);
+int register_irq_handler(void (*mbox_isr)(void *data), void *prdata);
+extern void mbox_isr_pre_cb(void);
+extern void mbox_isr_post_cb(void);
+
+void mbox_isr_pre_cb(void) {return;}
+void mbox_isr_post_cb(void) {return;}
+
+/*
+ * init send/recv pin function, must in mbox_port.c
+ */
+void init_pin_number(void)
+{
+ TOTAL_RECV_PIN = sizeof(mbox_pin_recv_table)/sizeof(struct pin_recv);
+ TOTAL_SEND_PIN = sizeof(mbox_pin_send_table)/sizeof(struct pin_send);
+}
+
+
+/*
+ * find mbox number in mbox_isr funtion
+ */
+unsigned int find_mbox_num(unsigned int irq_num)
+{
+ return irq_num;
+}
+
+/*
+ * set mbox 64d function
+ */
+int set_mbox_64d(unsigned int mbox, unsigned int is64d, void *data)
+{
+ //set mbox is 64d
+ if (is64d == MBOX_OPT_32_SLOT)
+ DRV_ClrReg32(R_SECURE_CTRL, B_MBOX0_SIZE << mbox);
+ else
+ DRV_SetReg32(R_SECURE_CTRL, B_MBOX0_SIZE << mbox);
+
+ return 0;
+}
+
+/*
+ * set send/recv offset and pin by is64d
+ */
+void mbox_setup_pin_table(int mbox)
+{
+ unsigned int i;
+ int last_ofs = 0, last_idx = 0, last_slot = 0, last_sz = 0;
+
+ for (i = 0; i < TOTAL_RECV_PIN; i++) {
+ if (mbox == mbox_pin_recv_table[i].mbox) {
+ /* update pin offset, idex */
+ mbox_pin_recv_table[i].offset = last_ofs + last_slot;
+ mbox_pin_recv_table[i].pin_index = last_idx + last_sz;
+
+ /* calculate occupied msg size, offset, slots*/
+ last_idx = mbox_pin_recv_table[i].pin_index;
+ last_sz = DIV_ROUND_UP(mbox_pin_recv_table[i].msg_size,
+ mbox_table[mbox].is64d + 1);
+ last_ofs = last_sz * (mbox_table[mbox].is64d + 1);
+ last_slot = last_idx * (mbox_table[mbox].is64d + 1);
+ } else if (mbox < mbox_pin_recv_table[i].mbox)
+ break; /* no need to search the rest ipi */
+ }
+
+ for (i = 0; i < TOTAL_SEND_PIN; i++) {
+ if (mbox == mbox_pin_send_table[i].mbox) {
+ /* update pin offset, idex */
+ mbox_pin_send_table[i].offset = last_ofs + last_slot;
+ mbox_pin_send_table[i].pin_index = last_idx + last_sz;
+
+ /* calculate occupied msg size, offset, slots*/
+ last_idx = mbox_pin_send_table[i].pin_index;
+ last_sz = DIV_ROUND_UP(mbox_pin_send_table[i].msg_size,
+ mbox_table[mbox].is64d + 1);
+ last_ofs = last_sz * (mbox_table[mbox].is64d + 1);
+ last_slot = last_idx * (mbox_table[mbox].is64d + 1);
+ } else if (mbox < mbox_pin_send_table[i].mbox)
+ break; /* no need to search the rest ipi */
+ }
+
+ if (last_idx > 32 ||
+ (last_ofs + last_slot) > (mbox_table[mbox].is64d + 1) * 32) {
+ PRINTF_E("mbox%d index(%d)/slot(%d) exceed the maximum\n",
+ mbox, last_idx, last_ofs);
+ configASSERT(0);
+ }
+}
+
+extern uint64_t mbox_get_time_stamp(void);
+uint64_t mbox_get_time_stamp(void)
+{
+#ifdef CFG_XGPT_SUPPORT
+ return (uint64_t) read_xgpt_stamp_ns();
+#else
+ return 0;
+#endif
+}
+
+/*
+ * dump pin table with selected mbox
+ * bit[0:3] represent to mbox0~mbox4
+ */
+void dump_pin_table(int8_t mbox)
+{
+ unsigned int i;
+
+ PRINTF_D("pin recv table:\n");
+ for (i = 0; i < TOTAL_RECV_PIN; i++) {
+ if ((1 << mbox_pin_recv_table[i].mbox) & mbox)
+ PRINTF_D(" mbox%d pin%02d index: %02u, offset: %02u, sz: %02u\n",
+ mbox_pin_recv_table[i].mbox,
+ mbox_pin_recv_table[i].ipi_id,
+ mbox_pin_recv_table[i].pin_index,
+ mbox_pin_recv_table[i].offset,
+ mbox_pin_recv_table[i].msg_size);
+ }
+
+ PRINTF_D("pin send table:\n");
+ for (i = 0; i < TOTAL_SEND_PIN; i++) {
+ if ((1 << mbox_pin_send_table[i].mbox) & mbox)
+ PRINTF_D(" mbox%d pin%02d index: %02u, offset: %02u, sz: %02u\n",
+ mbox_pin_send_table[i].mbox,
+ mbox_pin_send_table[i].ipi_id,
+ mbox_pin_send_table[i].pin_index,
+ mbox_pin_send_table[i].offset,
+ mbox_pin_send_table[i].msg_size);
+ }
+
+}
+
+/*
+ * register_irq_handler
+ */
+int register_irq_handler(void (*mbox_isr)(void *data), void *prdata)
+{
+ int mbox;
+
+ mbox = *(int *)prdata;
+
+ /* clear mbox interrupt to ensure mbox is clean before request */
+ mbox_clr_irq(mbox, 0xffffffff);
+
+ if (intc_irq_request(&INTC_IRQ_MBOX[mbox],
+ (void *)mbox_isr, &mbox_table[mbox]) != 0) {
+ PRINTF_E("mbox %d irq request failed\n", mbox);
+ return MBOX_CONFIG_ERR;
+ } else {
+ intc_irq_wakeup_set(&INTC_IRQ_MBOX[mbox], 0x1);
+ return MBOX_DONE;
+ }
+}
+
diff --git a/src/tinysys/medmcu/drivers/common/mbox/scp_ipi.c b/src/tinysys/medmcu/drivers/common/mbox/scp_ipi.c
new file mode 100644
index 0000000..8f8c39c
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/mbox/scp_ipi.c
@@ -0,0 +1,168 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2019. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+#include "scp_ipi.h"
+#include "encoding.h"
+
+
+static struct ipi_wrapper ipi_legacy_id [] = IPI_LEGACY_GROUP;
+ipi_handler_t mpool_handle_list[IPI_WRAPPER_TOTAL - IPI_MPOOL - 1];
+struct ipi_legacy_pkt {
+ unsigned int id;
+ unsigned int len;
+ void *data;
+};
+
+/*
+ * This is a handler for handling legacy ipi callback function
+ */
+static void legacy_handler(int id, void *prdata, void *data, unsigned int len)
+{
+ ipi_handler_t handler;
+ unsigned int ptr = (unsigned int)data;
+ struct ipi_legacy_pkt pkt;
+
+ /* variation length will only support chre, chrex and sensor for reducing
+ * slot and cpu time cost by memcpy.
+ */
+ pkt.id = *(unsigned int *)ptr;
+ pkt.len = *(unsigned int *)(ptr + 4);
+ pkt.data = (void *)(ptr + 8);
+
+ if (pkt.id > IPI_MPOOL)
+ handler = mpool_handle_list[pkt.id - IPI_MPOOL - 1];
+ else
+ handler = (ipi_handler_t)prdata;
+ handler(pkt.id, pkt.data, pkt.len);
+}
+
+/*
+ * Wrapper api for legacy api and supports only legacy features
+ * which is listed in ipi_legacy_id[]
+ */
+ipi_status scp_ipi_send(enum ipi_id id, void* buf, uint32_t len, uint32_t wait,
+ enum ipi_dir dir)
+{
+ int ret = 0, tmp_id;
+ void *ptr;
+
+ if (id >= IPI_WRAPPER_TOTAL || id == IPI_MPOOL) {
+ PRINTF_E("%s: id not support\n", __func__);
+ return ERROR;
+ }
+
+ if (id > IPI_MPOOL)
+ tmp_id = IPI_MPOOL;
+ else
+ tmp_id = id;
+
+ if (len > (ipi_legacy_id[tmp_id].out_size - 2) * MBOX_SLOT_SIZE) {
+ PRINTF_E("%s: len overflow\n", __func__);
+ return ERROR;
+ }
+
+ /* variation length will only support chre and sensor for reducing slot
+ * and cpu time cost by memcpy.
+ */
+ char pkt[ipi_legacy_id[tmp_id].out_size * MBOX_SLOT_SIZE];
+ memcpy((void *)pkt, (void *)&id, sizeof(uint32_t));
+ memcpy((void *)(pkt + 4), (void *)&len, sizeof(uint32_t));
+ memcpy((void *)(pkt + 8), buf, len);
+ ptr = pkt;
+
+ if (mrv_read_csr(CSR_MHARTID) == 0)
+ ret = ipi_send(ipi_legacy_id[tmp_id].out_id_0, ptr,
+ (int)ipi_legacy_id[tmp_id].out_size,
+ wait * IPI_WAIT_LEGACY);
+ else
+ ret = ipi_send(ipi_legacy_id[tmp_id].out_id_1, ptr,
+ (int)ipi_legacy_id[tmp_id].out_size,
+ wait * IPI_WAIT_LEGACY);
+
+ if (ret == IPI_ACTION_DONE)
+ return DONE;
+ else if (ret == IPI_PIN_BUSY)
+ return BUSY;
+ else
+ return ERROR;
+}
+
+ipi_status scp_ipi_registration(enum ipi_id id, ipi_handler_t handler,
+ const char *name)
+{
+ int ret = 0;
+
+ if (id >= IPI_WRAPPER_TOTAL || id == IPI_MPOOL) {
+ PRINTF_E("%s: id not support\n", __func__);
+ return ERROR;
+ }
+
+ if (id > IPI_MPOOL) {
+ /* if there's any new ipi, hook handler only */
+ mpool_handle_list[id - IPI_MPOOL - 1] = handler;
+ return DONE;
+ }
+
+ if (mrv_read_csr(CSR_MHARTID) == 0) {
+ ret = ipi_register(ipi_legacy_id[id].in_id_0, legacy_handler,
+ handler, ipi_legacy_id[id].msg);
+ } else {
+ ret = ipi_register(ipi_legacy_id[id].in_id_1, legacy_handler,
+ handler, ipi_legacy_id[id].msg);
+ }
+
+ if (ret == IPI_ACTION_DONE)
+ return DONE;
+ else
+ return ERROR;
+}
+
+void scp_legacy_ipi_init(void)
+{
+ int ret = 0;
+
+ if (mrv_read_csr(CSR_MHARTID) == 0) {
+ ret = ipi_register(IPI_IN_SCP_MPOOL_0, legacy_handler, 0,
+ msg_legacy_ipi_mpool);
+ } else {
+ ret = ipi_register(IPI_IN_SCP_MPOOL_1, legacy_handler, 0,
+ msg_legacy_ipi_mpool);
+ }
+
+ if (ret)
+ PRINTF_E("ipi mpool register fail, ret %d\n", ret);
+}
diff --git a/src/tinysys/medmcu/drivers/common/scpctl/inc/scp_scpctl.h b/src/tinysys/medmcu/drivers/common/scpctl/inc/scp_scpctl.h
new file mode 100644
index 0000000..60d4a94
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/scpctl/inc/scp_scpctl.h
@@ -0,0 +1,66 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2019. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek Software")
+ * have been modified by MediaTek Inc. All revisions are subject to any receiver's
+ * applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef __SCP_SCPCTL_H__
+#define __SCP_SCPCTL_H__
+
+#include <stdint.h>
+
+enum SCPCTL_TYPE_E {
+ SCPCTL_TYPE_TMON,
+ SCPCTL_STRESS_TEST,
+};
+
+enum SCPCTL_OP_E {
+ SCPCTL_OP_INACTIVE = 0,
+ SCPCTL_OP_ACTIVE,
+};
+
+struct scpctl_cmd_s {
+ uint32_t type;
+ uint32_t op;
+};
+
+void scpctl_init(void);
+void scpctl_idlehook(void);
+void vTaskMonitor(void *pvParameters);
+
+#ifdef CFG_MRV_UNALIGN_SUPPORT
+extern unsigned int misalignedLoad;
+extern unsigned int misalignedStore;
+#endif
+
+#endif /* __SCP_SCPCTL_H__ */
+
diff --git a/src/tinysys/medmcu/drivers/common/scpctl/scp_scpctl.c b/src/tinysys/medmcu/drivers/common/scpctl/scp_scpctl.c
new file mode 100644
index 0000000..994d583
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/scpctl/scp_scpctl.c
@@ -0,0 +1,215 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+#include <FreeRTOS.h>
+#include <task.h>
+#ifdef CFG_LEGACY_IPI_SUPPORT
+#include <scp_ipi.h>
+#else
+#include <ipi.h>
+#include <ipi_id.h>
+#endif
+
+#include <scp_scpctl.h>
+#include <main.h>
+#include "encoding.h"
+#include "irq.h"
+#ifdef CFG_XGPT_SUPPORT
+#include <mt_gpt.h>
+#endif
+
+enum SCPCTL_STAT_E {
+ SCPCTL_STAT_INACTIVE = 0,
+ SCPCTL_STAT_ACTIVE,
+};
+
+struct scpctl_ctl_s {
+ enum SCPCTL_OP_E op;
+ enum SCPCTL_STAT_E stat;
+};
+
+#define mainCHECK_DELAY ((portTickType) 10000 / portTICK_RATE_MS)
+
+static struct scpctl_ctl_s scpctl __attribute__ ((section(".sync")));
+static char *prompt = "[SCPCTL]";
+static TaskHandle_t xMonitorTask = NULL;
+struct scpctl_cmd_s scpctl_cmd_s_buf __attribute__ ((section(".sync")));
+
+#ifdef CFG_SCP_STRESS_SUPPORT
+unsigned int task_stress_flag __attribute__((section(".sync"))) = 0;
+#endif
+
+#ifdef CFG_SCPCTL_TASK_STATUS
+static char list_buffer[512];
+
+static void __task_status(void)
+{
+ vTaskList(list_buffer);
+ PRINTF_E("Heap:free/total:%lu/%u\n", xPortGetFreeHeapSize(), configTOTAL_HEAP_SIZE);
+ PRINTF_E("Task Status:\n\r%s", list_buffer);
+// PRINTF_E("max dur.:%llu,limit:%llu\n", get_max_cs_duration_time(), get_max_cs_limit());
+}
+#endif
+
+void vTaskMonitor(void *pvParameters)
+{
+ portTickType xLastExecutionTime, xDelayTime;
+ xDelayTime = mainCHECK_DELAY;
+
+ do {
+ xLastExecutionTime = xTaskGetTickCount();
+
+#ifdef CFG_SCPCTL_TASK_STATUS
+ __task_status();
+#endif
+#ifdef CFG_MRV_UNALIGN_SUPPORT
+ PRINTF_E("unaligned Load/Store times (%u/%u)\n", misalignedLoad, misalignedStore);
+#endif
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+
+
+/****************************************************************************
+ * The ISR is to receive the IPI commands sent by ADB and then modify
+ * the operation, op, of the control state.
+ ****************************************************************************/
+static void scpctl_handler(unsigned int id, void *prdata, void *data, unsigned int len)
+{
+ struct scpctl_cmd_s *cmd = (struct scpctl_cmd_s *)data;
+ enum SCPCTL_TYPE_E type = cmd->type;
+ enum SCPCTL_OP_E op = cmd->op;
+
+ switch (type) {
+ case SCPCTL_TYPE_TMON:
+ scpctl.op = (op == SCPCTL_OP_ACTIVE)
+ ? SCPCTL_OP_ACTIVE
+ : SCPCTL_OP_INACTIVE;
+
+ PRINTF_E("%s: type/op=%d/%d,stat=%d\n", prompt, type, op, scpctl.stat);
+ break;
+#ifdef CFG_SCP_STRESS_SUPPORT
+ case SCPCTL_STRESS_TEST:
+ task_stress_flag = 1;
+ PRINTF_E("Stress enabled\n");
+ break;
+#endif
+ default:
+ PRINTF_E("%s: unknown cmd, %d, %d\n", prompt, type, op);
+ break;
+ }
+}
+
+/****************************************************************************
+ * The function is called by in vApplicationIdleHook and determine whether
+ * to resume or to suspend monitor task depending on the command and the
+ * current status.
+ *
+ * op = active, state = inactive --> resume monitor task.
+ * op = inactive, state = active --> suspend monitor task.
+ * others, keep the same status.
+ ****************************************************************************/
+void scpctl_idlehook(void)
+{
+ int op = scpctl.op;
+
+ if (op == SCPCTL_OP_ACTIVE && scpctl.stat == SCPCTL_STAT_INACTIVE) {
+ vTaskResume(xMonitorTask);
+ scpctl.stat = SCPCTL_STAT_ACTIVE;
+ }
+ else if (op == SCPCTL_OP_INACTIVE && scpctl.stat == SCPCTL_STAT_ACTIVE) {
+ vTaskSuspend(xMonitorTask);
+ scpctl.stat = SCPCTL_STAT_INACTIVE;
+ }
+}
+
+
+#ifdef CFG_SCP_STRESS_SUPPORT
+
+/* run on both core */
+static void vTaskSCPStress(void *pvParameters)
+{
+ portTickType xLastExecutionTime, xDelayTime;
+ xLastExecutionTime = xTaskGetTickCount();
+ unsigned int get_random_time;
+ xDelayTime = (portTickType) 60000 / portTICK_RATE_MS ;
+ do {
+ PRINTF_E("Task Stress ");
+ if (task_stress_flag == 0) {
+ PRINTF_E("status=%u\n", task_stress_flag);
+ } else {
+ /* stress flag is enable*/
+ PRINTF_E("status=%u, stress enable!\n", task_stress_flag);
+ /* get 60 ~120 sec delay*/
+#ifdef CFG_XGPT_SUPPORT
+ get_random_time = (unsigned int)(get_boot_time_ns() % 60);
+ get_random_time = (30 + get_random_time) * 1000;
+ xDelayTime = (portTickType) get_random_time / portTICK_RATE_MS ;
+#endif
+ PRINTF_E("after:%ums, will abort...\n", xDelayTime);
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ configASSERT(0);
+ }
+ xDelayTime = (portTickType) 60000 / portTICK_RATE_MS ;
+ vTaskDelayUntil(&xLastExecutionTime, xDelayTime);
+ } while (1);
+}
+#endif
+
+void scpctl_init(void)
+{
+ int ret = 0;
+
+ xTaskCreate(vTaskMonitor, "TMon", 384, (void*)4, PRI_MONITOR, &xMonitorTask);
+ configASSERT(xMonitorTask != NULL);
+#ifdef CFG_SCP_STRESS_SUPPORT
+ xTaskCreate(vTaskSCPStress, "Stres", 384, (void*)4, PRI_STRESS, NULL);
+#endif /* CFG_SCP_STRESS_SUPPORT */
+
+ if (scp_region_info.scpctl & (1 << SCPCTL_TYPE_TMON)) {
+ scpctl.stat = SCPCTL_STAT_ACTIVE;
+ scpctl.op = SCPCTL_OP_ACTIVE;
+ }
+ else { /* monitor task is in suspened state */
+ scpctl.stat = SCPCTL_STAT_INACTIVE;
+ scpctl.op = SCPCTL_OP_INACTIVE;
+ vTaskSuspend(xMonitorTask);
+ }
+ /* uni-control, do it only ine one core */
+ if (mrv_read_csr(CSR_MHARTID) == 1) {
+ ret = ipi_register(IPI_IN_SCPCTL_1, scpctl_handler, 0, &scpctl_cmd_s_buf);
+ if (ret)
+ PRINTF_E("IPI_IN_SCPCTL_1 %d\n", ret);
+ }
+
+
+}
+
diff --git a/src/tinysys/medmcu/drivers/common/sem/inc/mtk_sem.h b/src/tinysys/medmcu/drivers/common/sem/inc/mtk_sem.h
new file mode 100644
index 0000000..b77e7f2
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/sem/inc/mtk_sem.h
@@ -0,0 +1,47 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+#include <tinysys_reg.h>
+#include <encoding.h>
+#define SEMAPHORE_REG R_SEMAPHORE
+#define SEMA_TIMEOUT 5
+
+#ifdef CFG_SEMAPHORE_3WAY_SUPPORT
+#define SEMAPHORE_3WAY_REG R_SEMAPHORE_3MST
+#define SEMAPHORE_3WAY_BITS 4
+#define DSP_CORE_ID (mrv_read_csr(CSR_MHARTID))//SCP_A_ID
+#endif
diff --git a/src/tinysys/medmcu/drivers/common/tiny_ipc/inc/tiny_ipc.h b/src/tinysys/medmcu/drivers/common/tiny_ipc/inc/tiny_ipc.h
new file mode 100644
index 0000000..37747d0
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/tiny_ipc/inc/tiny_ipc.h
@@ -0,0 +1,82 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+/* buff at end of l2_tcm, total 512 byte
+include send and receive */
+#define TINY_IPC_BUFF_SIZE 512
+
+enum ipi_id {
+ TIPC_SCP_CORE0_READY = 0,
+ TIPC_SLT_CORE0_CTL,
+ TIPC_CORE0_END = 15,
+ TIPC_SCP_CORE1_READY = 16,
+ TIPC_SLT_CORE1_CTL,
+ TIPC_CORE1_END,
+ NR_TIPC,
+};
+
+typedef void(*tipc_handler_t)(int id, void *data, unsigned int len);
+
+struct tipc_desc_t {
+ tipc_handler_t handler;
+ const char *name;
+};
+
+enum tipc_status {
+ T_ERROR =-1,
+ T_DONE,
+ T_BUSY,
+};
+#define IPC_WAIT 1
+#define IPC_NOWAIT 0
+
+
+#define TIPC_RECEIVE_BUFF (L2TCM_START + L2TCM_SIZE - TINY_IPC_BUFF_SIZE)
+#define TIPC_SEND_BUFF (L2TCM_START + L2TCM_SIZE - (TINY_IPC_BUFF_SIZE/2))
+
+enum tipc_status tiny_ipc_send(int id, void* buf, uint32_t len, uint32_t wait);
+
+enum tipc_status tiny_ipc_registration(int id, tipc_handler_t handler, const char *name);
+
+extern struct INTC_IRQ INTC_IRQ_GIPC0;
+
+void tiny_ipc_init(void);
+
+void tiny_ipc_isr(void);
+
+
+
diff --git a/src/tinysys/medmcu/drivers/common/tiny_ipc/tiny_ipc.c b/src/tinysys/medmcu/drivers/common/tiny_ipc/tiny_ipc.c
new file mode 100644
index 0000000..9d37d1a
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/tiny_ipc/tiny_ipc.c
@@ -0,0 +1,193 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2018. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+#include "FreeRTOS.h"
+#include "task.h"
+#include "tiny_ipc.h"
+#include <tinysys_reg.h>
+#include <driver_api.h>
+#include <mt_printf.h>
+#include "irq.h"
+#ifdef CFG_ATOMIC_PLAT_SUPPORT
+#include "mtk_atomic.h"
+#endif
+#ifdef CFG_XGPT_SUPPORT
+#include <xgpt.h>
+#endif
+
+#ifdef CFG_COMMON_WAKELOCK_SUPPORT
+#include <wakelock.h>
+#endif
+
+
+static struct tipc_desc_t tipc_desc[NR_TIPC];
+spinlock_t SYNC_SECTION tipc_lock;
+
+#ifdef CFG_COMMON_WAKELOCK_SUPPORT
+wakelock_t ap_wakelock;
+#endif
+
+/*
+@param id: IPI ID
+@param handler: IPI handler
+@param name: IPI name
+*/
+enum tipc_status tiny_ipc_registration(int id, tipc_handler_t handler, const char *name)
+{
+ PRINTF_E("%s id:%d: %s", __func__, id, name);
+ if (handler == NULL)
+ return T_ERROR;
+
+ if (id >= NR_TIPC)
+ return T_ERROR;
+
+ tipc_desc[id].name = name;
+ tipc_desc[id].handler = handler;
+ return T_DONE;
+}
+
+
+static void try_to_wakeup_ap(void)
+{
+ //ipi_scp2spm(); //wake APMCU up
+}
+/*
+ return fail when resource not ready
+*/
+
+enum tipc_status tiny_ipc_send(int id, void* buf, uint32_t len, uint32_t wait)
+{
+ unsigned long flags;
+ int i = 0;
+
+ //PRINTF_I("%s %d\n", __func__, __LINE__);
+ if(len > (TINY_IPC_BUFF_SIZE/2)) {
+ PRINTF_D("%s len %d over\n", __func__, len);
+ return T_ERROR;
+ }
+
+ flags = spinlock_lock_irqsave(&tipc_lock);
+
+ /* Check if there is already an ipi pending in AP. */
+ //while (readl(SCP2APMCU_IPC_SET) & SCP2APMCU_BIT0) {
+ while (readl(MBOX1_OUT_IRQ_SET) & SCP2APMCU_BIT0) {
+ /*If the following conditions meet,
+ * 1)there is an ipi pending in AP
+ * 2)the coming IPI is a wakeup IPI
+ * so it assumes that AP is in suspend state
+ * send a AP wakeup request to SPM
+ * */
+ /*the coming IPI will be checked if it's a wakeup source*/
+ try_to_wakeup_ap();
+ mdelay(1);
+
+ i++;
+ if( i > 1000) {
+ PRINTF_D("%s id:%d busy\n", __func__, id);
+ spinlock_unlock_irqrestore(&tipc_lock, flags);
+ return T_BUSY;
+ }
+ }
+
+ //PRINTF_I("%s %d\n", __func__, __LINE__);
+ writel((void *)(TIPC_SEND_BUFF ), id);
+ writel((void *)(TIPC_SEND_BUFF + 4), len);
+ memcpy((void *)(TIPC_SEND_BUFF + 8), buf, len);
+
+ //writel(SCP2APMCU_IPC_SET, SCP2APMCU_BIT0);
+ writel(MBOX1_OUT_IRQ_SET, SCP2APMCU_BIT0);
+
+ spinlock_unlock_irqrestore(&tipc_lock, flags);
+
+ if (wait)
+ //while ((readl(SCP2APMCU_IPC_SET) & SCP2APMCU_BIT0));
+ while ((readl(MBOX1_OUT_IRQ_SET) & SCP2APMCU_BIT0));
+
+ return T_DONE;
+}
+
+#ifdef CFG_COMMON_WAKELOCK_SUPPORT
+static void tiny_ipc_wakeup_isr(void)
+{
+ unsigned int reg_val;
+
+ reg_val = readl(MBOX2_IN_IRQ_SET) & 0xf;
+ PRINTF_D("%s reg=%d\n", __func__, reg_val);
+
+ /* clr interrupt as early as possible to let AP leave busy waiting */
+ writel(MBOX2_IN_IRQ_CLR, reg_val);
+
+ if (reg_val & (1 << AP_AWAKE_LOCK))
+ wake_lock_FromISR(&ap_wakelock);
+
+ if (reg_val & (1 << AP_AWAKE_UNLOCK))
+ wake_unlock_FromISR(&ap_wakelock);
+
+}
+
+static void tiny_ipc_awake_init(void)
+{
+ wake_lock_init(&ap_wakelock, "AP_W");
+ if (intc_irq_request(&INTC_IRQ_MBOX[2], (void *)tiny_ipc_wakeup_isr, NULL))
+ PRINTF_E("wakeup irq request failed\n");
+ intc_irq_wakeup_set(&INTC_IRQ_MBOX[2], 1);
+}
+#endif
+
+void tiny_ipc_isr(void)
+{
+ int id = 0, size = 0;
+ id = readl((void *)TIPC_RECEIVE_BUFF);
+ size = readl((void *)(TIPC_RECEIVE_BUFF + 4));
+ if (tipc_desc[id].name != NULL){
+ PRINTF_D("%s %d\n", __func__, id);
+ tipc_desc[id].handler(id, (void*)(TIPC_RECEIVE_BUFF + 8), size);
+ }
+ /* clear INT */
+ //writel(GIPC_IN_CLR, readl(GIPC_IN_SET));
+ writel(MBOX1_IN_IRQ_CLR, SCP2APMCU_BIT0);
+}
+
+void tiny_ipc_init(void)
+{
+ //intc_irq_request(&INTC_IRQ_GIPC0, (void *) tiny_ipc_isr, NULL);
+ //intc_irq_wakeup_set(&INTC_IRQ_GIPC0, 1);
+ intc_irq_request(&INTC_IRQ_MBOX[1], (void *) tiny_ipc_isr, NULL);
+ intc_irq_wakeup_set(&INTC_IRQ_MBOX[1], 1);
+#ifdef CFG_COMMON_WAKELOCK_SUPPORT
+ tiny_ipc_awake_init();
+#endif
+}
diff --git a/src/tinysys/medmcu/drivers/common/wdt/inc/wdt.h b/src/tinysys/medmcu/drivers/common/wdt/inc/wdt.h
new file mode 100644
index 0000000..9bb2bc7
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/wdt/inc/wdt.h
@@ -0,0 +1,65 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2010. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation ("MediaTek Software")
+ * have been modified by MediaTek Inc. All revisions are subject to any receiver\'s
+ * applicable license agreements with MediaTek Inc.
+ */
+
+#ifndef _WDT_H_
+#define _WDT_H_
+
+#define WDT_IRQ_REG (CORE0_WDT_IRQ + mrv_read_csr(CSR_MHARTID)*0x10000)
+#define WDT_CFGREG (CORE0_WDT_CFG + mrv_read_csr(CSR_MHARTID)*0x10000)
+#define WDT_KICKREG (CORE0_WDT_KICK + mrv_read_csr(CSR_MHARTID)*0x10000)
+#define WDT_CUR_VAL_REG (CORE0_WDT_CUR_VAL + mrv_read_csr(CSR_MHARTID)*0x10000)
+
+#define START_WDT 0x800FFFFF //enable wdt, timeout in 31 sec
+#define KICK_WDT 0x1
+#define DISABLE_WDT 0x000FFFFF
+#define WDT_EN 0x1F
+#define WDT_INSTANT_TRIGGER 0x0
+
+#define SCP_GPR_REBOOT_FLAG (SCP_GPR_CORE0_REBOOT + mrv_read_csr(CSR_MHARTID)*0x10000)
+
+void mtk_wdt_disable(void);
+void mtk_wdt_enable(void);
+void mtk_wdt_restart(void);
+void mtk_wdt_irq_clear(void);
+int mtk_wdt_set_time_out_value(unsigned int value);
+void mtk_wdt_isr(void);
+void mtk_wdt_init(void);
+void scp_ready_to_reboot(void);
+void scp_halt_isr(void);
+void mtk_halt_isr_init(void);
+void scp_endless_loop(void);
+
+#endif /* _WDT_H_ */
+
diff --git a/src/tinysys/medmcu/drivers/common/wdt/wdt.c b/src/tinysys/medmcu/drivers/common/wdt/wdt.c
new file mode 100644
index 0000000..029a576
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/wdt/wdt.c
@@ -0,0 +1,138 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly
+ * prohibited.
+ */
+/* MediaTek Inc. (C) 2019. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY
+ * ACKNOWLEDGES THAT IT IS RECEIVER\'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY
+ * THIRD PARTY ALL PROPER LICENSES CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK
+ * SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK SOFTWARE RELEASES MADE TO
+ * RECEIVER\'S SPECIFICATION OR TO CONFORM TO A PARTICULAR STANDARD OR OPEN
+ * FORUM. RECEIVER\'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK\'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER
+ * WILL BE, AT MEDIATEK\'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE
+ * AT ISSUE, OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY
+ * RECEIVER TO MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ *
+ * The following software/firmware and/or related documentation
+ * ("MediaTek Software") have been modified by MediaTek Inc. All revisions are
+ * subject to any receiver\'s applicable license agreements with MediaTek Inc.
+ */
+
+#include "FreeRTOS.h"
+#include "task.h"
+#include <tinysys_reg.h>
+#include <driver_api.h>
+#include <mt_printf.h>
+#include <irq.h>
+#include <intc.h>
+#include <encoding.h>
+#include <wdt.h>
+#ifdef CFG_XGPT_SUPPORT
+#include <xgpt.h>
+#endif
+#include <peripheral.h>
+
+void mtk_wdt_disable(void)
+{
+ DRV_WriteReg32(WDT_CFGREG, DISABLE_WDT);
+}
+void mtk_wdt_enable(void)
+{
+ DRV_WriteReg32(WDT_CFGREG, START_WDT);
+}
+void mtk_wdt_irq_clear(void)
+{
+ DRV_WriteReg32(WDT_IRQ_REG, 0x1);
+}
+void mtk_wdt_restart(void)
+{
+ DRV_WriteReg32(WDT_KICKREG, KICK_WDT);
+}
+
+int mtk_wdt_set_time_out_value(unsigned int value)
+{
+ if (value > 0xFFFFF) {
+ PRINTF_D("SCP WDT Timeout value overflow\n");
+ return -1;
+ }
+ mtk_wdt_disable();
+ DRV_WriteReg32(WDT_CFGREG, 1 << WDT_EN | value);
+ return 0;
+}
+
+void scp_endless_loop(void)
+{
+ taskDISABLE_INTERRUPTS();
+ /* cleanup gvic setting for avoiding interrupt or wakeup triggered */
+ vic_set_mask(0, 0x0);
+ vic_set_wakeup_mask(0, 0x0);
+ /* Halt CPU */
+ while(1)
+ __asm volatile ("wfi");
+}
+
+void scp_ready_to_reboot(void)
+{
+ /* set reboot flag to let AP know we are ready to reboot*/
+ *(volatile unsigned int *)SCP_GPR_REBOOT_FLAG = CORE_RDY_TO_REBOOT;
+
+}
+void mtk_wdt_isr(void)
+{
+ PRINTF_D("%s core%lu\n", __func__, mrv_read_csr(CSR_MHARTID));
+ /* notify another core */
+ DRV_WriteReg32(GIPC_IN_SET, GIPC4_SETCLR_BIT_0);
+ scp_ready_to_reboot();
+ scp_endless_loop();
+}
+
+void scp_halt_isr(void)
+{
+ PRINTF_D("%s core%lu\n", __func__, mrv_read_csr(CSR_MHARTID));
+ scp_ready_to_reboot();
+ scp_endless_loop();
+}
+
+void mtk_wdt_init(void)
+{
+ mtk_wdt_disable();
+#ifdef CFG_XGPT_SUPPORT
+ /* needs 70us to reload counter value */
+ udelay(70);
+#endif
+ mtk_wdt_irq_clear();
+ intc_irq_request(&INTC_IRQ_WDT, (void *) mtk_wdt_isr, NULL);
+ intc_irq_wakeup_set(&INTC_IRQ_WDT, 1);
+ mtk_halt_isr_init();
+ mtk_wdt_enable();
+ mtk_wdt_restart(); /* restart wdt after wdt reset */
+ PRINTF_D("SCP mtk_wdt_init: WDT_CFGREG=0x%x!!\n", DRV_Reg32(WDT_CFGREG));
+}
+
+
+void mtk_halt_isr_init(void)
+{
+ /* clear ipc first */
+ DRV_WriteReg32(GIPC_IN_CLR, GIPC4_SETCLR_BIT_0);
+ intc_irq_request(&INTC_IRQ_HALT, (void *) scp_halt_isr, NULL);
+ intc_irq_wakeup_set(&INTC_IRQ_HALT, 1);
+}
+
+
diff --git a/src/tinysys/medmcu/drivers/common/xgpt/inc/mt_gpt.h b/src/tinysys/medmcu/drivers/common/xgpt/inc/mt_gpt.h
new file mode 100644
index 0000000..af20573
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/xgpt/inc/mt_gpt.h
@@ -0,0 +1,37 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+#ifndef __MT_GPT_H__
+#define __MT_GPT_H__
+
+#include "xgpt.h"
+
+#endif
diff --git a/src/tinysys/medmcu/drivers/common/xgpt/inc/xgpt.h b/src/tinysys/medmcu/drivers/common/xgpt/inc/xgpt.h
new file mode 100644
index 0000000..a6502aa
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/xgpt/inc/xgpt.h
@@ -0,0 +1,221 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+#ifndef __XGPT_H__
+#define __XGPT_H__
+
+#include <stdio.h>
+#include <stdint.h>
+#include <encoding.h>
+#include <irq.h>
+
+#define TIMER_IN_CLK (CLK_CTRL_BASE + 0x30)
+#define TMR_MCLK_CG (1 << 0)
+#define TMR_BCLK_CG (1 << 1)
+
+#define XGPT_BASE_REG (SCP_TIMER_CORE0_BASE + mrv_read_csr(CSR_MHARTID)*0x10000)
+#define GENERAL_CTRL (CORE0_GENERAL_CTRL + mrv_read_csr(CSR_MHARTID)*0x10000)
+
+#define SCP_TIMER_BASE g_timer_base
+
+#define TMR0 0x0
+#define TMR1 0x1
+#define TMR2 0x2
+#define TMR3 0x3
+#define TMR4 0x4
+#define TMR5 0x5
+#define NR_TMRS 0x6
+
+#define TIMER_CPU_TICK_EN (SCP_TIMER_BASE + 0x68)
+#define TIMER_CPU_TICK_RST_VAL (SCP_TIMER_BASE + 0x6c)
+#define TIMER_CPU_TICK_CUR_VAL (SCP_TIMER_BASE + 0x70)
+#define TIMER_CPU_TICK_IRQ_CTRL (SCP_TIMER_BASE + 0x74)
+#define TIMERCPU_TICK_IRQ_CLR (1 << 5)
+#define TIMER_CPU_TICK_IRQ_STATUS (1 << 4)
+#define TIMER_CPU_TICK_IRQ_EN (1 << 0)
+
+#define OSTIMER_CON (SCP_TIMER_BASE + 0x80)
+#define OSTIMER_INIT_L (SCP_TIMER_BASE + 0x84)
+#define OSTIMER_INIT_H (SCP_TIMER_BASE + 0x88)
+#define OSTIMER_CUR_L 0x60017008 /*(SCP_TIMER_BASE + 0x8C)*/
+#define OSTIMER_CUR_H 0x6001700C /*(SCP_TIMER_BASE + 0x90)*/
+#define OSTIMER_TVAL (SCP_TIMER_BASE + 0x94)
+#define OSTIMER_IRQ_ACK (SCP_TIMER_BASE + 0x98)
+#define OSTIMER_TICK_IRQ_CLR (1 << 5)
+#define OSTIMER_TICK_IRQ_STATUS (1 << 4)
+#define OSTIMER_TICK_IRQ_EN (1 << 0)
+
+#define OS_TIMER_LATCH_CTRL (SCP_TIMER_BASE + 0xA0)
+#define OS_TIMER_LATCH_VALUE_0 (SCP_TIMER_BASE + 0xA4)
+#define OS_TIMER_LATCH_VALUE_0_MSB (SCP_TIMER_BASE + 0xA8)
+#define OS_TIMER_LATCH_VALUE_1 (SCP_TIMER_BASE + 0xAC)
+#define OS_TIMER_LATCH_VALUE_1_MSB (SCP_TIMER_BASE + 0xB0)
+#define OS_TIMER_LATCH_VALUE_2 (SCP_TIMER_BASE + 0xB4)
+#define OS_TIMER_LATCH_VALUE_2_MSB (SCP_TIMER_BASE + 0xB8)
+
+/* AP side system counter frequence is 13MHz*/
+#ifdef CFG_FPGA
+#define AP_NS_PER_CNT (1000000000UL)/(6000000UL)
+#else
+#define AP_NS_PER_CNT (1000000000UL)/(13000000UL)
+#endif
+
+#define TIMER_EN (0x00)
+#define TIMER_CLK_SRC (0x00)
+#define TIMER_RST_VAL (0x04)
+#define TIMER_CUR_VAL_REG (0x08)
+#define TIMER_IRQ_CTRL_REG (0x0C)
+
+#define TIMER_CLK_SEL_REG (SCP_TIMER_BASE+0x40)
+
+//#define portNVIC_MTK_XGPT_REG (TIMER_BASE + 0x18)
+
+#define TIMER_ENABLE 1
+#define TIMER_DISABLE 0
+
+#define TIMER_IRQ_ENABLE 1
+#define TIMER_IRQ_DISABLE 0
+
+#define TIMER_IRQ_STA (0x1 << 4)
+#define TIMER_IRQ_CLEAR (0x1 << 5)
+
+/* TODO: Check this setting */
+#define TIMER_CLK_SRC_CLK_32K (0x00)
+#define TIMER_CLK_SRC_CLK_26M (0x01)
+#define TIMER_CLK_SRC_BCLK (0x02)
+#define TIMER_CLK_SRC_PCLK (0x03)
+
+#define TIMER_CLK_SRC_MASK 0x3
+#define TIMER_CLK_SRC_SHIFT 4
+
+#define DELAY_TIMER_1US_TICK ((unsigned int)1) //(32KHz)
+#ifdef CFG_FPGA
+#define DELAY_TIMER_1MS_TICK ((unsigned int)34) //(33.3KHz)
+#else
+#define DELAY_TIMER_1MS_TICK ((unsigned int)33) //(32KHz)
+#endif
+
+// 32KHz: 31us = 1 counter
+#define TIME_TO_TICK_US(us) ((us)*DELAY_TIMER_1US_TICK)
+// 32KHz: 1ms = 33 counter
+#define TIME_TO_TICK_MS(ms) ((ms)*DELAY_TIMER_1MS_TICK)
+
+#ifdef CFG_FPGA
+#define COUNT_TO_TICK(x) ((x)/20) //20KHz, 1 tick is 20 counters
+#else
+#define COUNT_TO_TICK(x) ((x)/32) //32KHz, 1 tick is 32 counters
+#endif
+
+#define US_LIMIT 31 /* udelay's parameter limit */
+#define MAX_RG_BIT 0xffffffff
+
+#define RT_TIMER TMR0
+#define RT_TIMER_RSTVAL MAX_RG_BIT
+#define TICK_TIMER TMR1
+#define TICK_TIMER_RSTVAL MAX_RG_BIT
+#define DELAY_TIMER TMR2
+#define DELAY_TIMER_RSTVAL MAX_RG_BIT
+#define DMGR_TIMER TMR3
+#define DMGR_TIMER_RSTVAL TIME_TO_TICK_MS(1)
+#define CHRE_TIMER TMR4
+#define CHRE_TIMER_RSTVAL MAX_RG_BIT
+#define UNUSE2_TIMER TMR5
+#define UNUSE2_TIMER_RSTVAL MAX_RG_BIT
+
+typedef unsigned long long mt_time_t;
+typedef void (*platform_timer_callback) (void *arg);
+
+struct timer_device {
+ unsigned int id;
+ unsigned int base_addr;
+ struct INTC_IRQ irq;
+};
+
+/*************************End*****************************************/
+#if defined(CFG_MED_MCU_DVT)
+struct timer_device *get_dev_by_id(unsigned int id);
+void timer_enable_set(struct timer_device *dev);
+void timer_enable_irq(struct timer_device *dev);
+void timer_disable_set(struct timer_device *dev);
+void timer_set_clk(struct timer_device *dev, unsigned int clksrc);
+void timer_set_rstval(struct timer_device *dev, unsigned int val);
+unsigned long timer_get_curval(struct timer_device *dev);
+#endif
+void mdelay(unsigned long msec);
+void udelay(unsigned long usec);
+void mt_platform_timer_init(void);
+int platform_set_periodic_timer(platform_timer_callback callback, void *arg,
+ mt_time_t interval);
+unsigned long long read_xgpt_stamp_ns(void);
+unsigned long long timer_get_global_timer_tick(void);
+unsigned long long get_boot_time_ns(void);
+struct timer_device *id_to_dev(unsigned int id);
+
+void platform_set_cpu_tick(int interval);
+void timer_cpu_tick_irq_ack(void);
+void platform_cpu_tick_disable(void);
+void platform_cpu_tick_enable(void);
+
+#define OSTIMER_LATCH_TIME_SUPPORT
+
+#ifdef OSTIMER_LATCH_TIME_SUPPORT
+int alloc_latch_time(void);
+void free_latch_time(int id);
+void enable_latch_time(int id, int irq);
+void disable_latch_time(int id);
+uint64_t get_latch_time_timestamp(int id);
+#else
+static inline int alloc_latch_time(void)
+{
+ return -1;
+}
+
+static inline void free_latch_time(int id)
+{
+}
+
+static inline void enable_latch_time(int id, int irq)
+{
+}
+
+static inline void disable_latch_time(int id)
+{
+}
+
+static inline uint64_t get_latch_time_timestamp(int id)
+{
+ return 0;
+}
+#endif
+
+unsigned int program_hwtimer(void (*callback)(void), uint64_t diffTime);
+
+#endif /* !__XGPT_H__ */
diff --git a/src/tinysys/medmcu/drivers/common/xgpt/xgpt.c b/src/tinysys/medmcu/drivers/common/xgpt/xgpt.c
new file mode 100644
index 0000000..dd5d66c
--- /dev/null
+++ b/src/tinysys/medmcu/drivers/common/xgpt/xgpt.c
@@ -0,0 +1,790 @@
+/* Copyright Statement:
+ *
+ * This software/firmware and related documentation ("MediaTek Software") are
+ * protected under relevant copyright laws. The information contained herein
+ * is confidential and proprietary to MediaTek Inc. and/or its licensors.
+ * Without the prior written permission of MediaTek inc. and/or its licensors,
+ * any reproduction, modification, use or disclosure of MediaTek Software,
+ * and information contained herein, in whole or in part, shall be strictly prohibited.
+ */
+/* MediaTek Inc. (C) 2015. All rights reserved.
+ *
+ * BY OPENING THIS FILE, RECEIVER HEREBY UNEQUIVOCALLY ACKNOWLEDGES AND AGREES
+ * THAT THE SOFTWARE/FIRMWARE AND ITS DOCUMENTATIONS ("MEDIATEK SOFTWARE")
+ * RECEIVED FROM MEDIATEK AND/OR ITS REPRESENTATIVES ARE PROVIDED TO RECEIVER ON
+ * AN "AS-IS" BASIS ONLY. MEDIATEK EXPRESSLY DISCLAIMS ANY AND ALL WARRANTIES,
+ * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE OR NONINFRINGEMENT.
+ * NEITHER DOES MEDIATEK PROVIDE ANY WARRANTY WHATSOEVER WITH RESPECT TO THE
+ * SOFTWARE OF ANY THIRD PARTY WHICH MAY BE USED BY, INCORPORATED IN, OR
+ * SUPPLIED WITH THE MEDIATEK SOFTWARE, AND RECEIVER AGREES TO LOOK ONLY TO SUCH
+ * THIRD PARTY FOR ANY WARRANTY CLAIM RELATING THERETO. RECEIVER EXPRESSLY ACKNOWLEDGES
+ * THAT IT IS RECEIVER'S SOLE RESPONSIBILITY TO OBTAIN FROM ANY THIRD PARTY ALL PROPER LICENSES
+ * CONTAINED IN MEDIATEK SOFTWARE. MEDIATEK SHALL ALSO NOT BE RESPONSIBLE FOR ANY MEDIATEK
+ * SOFTWARE RELEASES MADE TO RECEIVER'S SPECIFICATION OR TO CONFORM TO A PARTICULAR
+ * STANDARD OR OPEN FORUM. RECEIVER'S SOLE AND EXCLUSIVE REMEDY AND MEDIATEK'S ENTIRE AND
+ * CUMULATIVE LIABILITY WITH RESPECT TO THE MEDIATEK SOFTWARE RELEASED HEREUNDER WILL BE,
+ * AT MEDIATEK'S OPTION, TO REVISE OR REPLACE THE MEDIATEK SOFTWARE AT ISSUE,
+ * OR REFUND ANY SOFTWARE LICENSE FEES OR SERVICE CHARGE PAID BY RECEIVER TO
+ * MEDIATEK FOR SUCH MEDIATEK SOFTWARE AT ISSUE.
+ */
+
+#include <driver_api.h>
+#include <tinysys_reg.h>
+#include <interrupt.h>
+#include <xgpt.h>
+#include <FreeRTOS.h>
+#include <semphr.h>
+#include <FreeRTOSConfig.h>
+
+#include <task.h>
+
+#ifdef CFG_VCORE_DVFS_SUPPORT
+#include <dvfs.h>
+#include <sleep.h>
+#endif
+#include <stdlib.h>
+#include <string.h>
+
+#include "mtk_atomic.h"
+#include "encoding.h"
+
+unsigned int g_timer_base;
+static struct timer_device scp_timer[NR_TMRS];
+static platform_timer_callback tick_timer_callback;
+static void *tick_timer_callback_arg;
+
+#ifdef CFG_CHRE_SUPPORT
+static platform_timer_callback chre_timer_callback;
+static void *chre_timer_callback_arg;
+#endif
+
+#define UNUSED(x) (void)(x)
+
+// 200MHz setting
+#define MCLK_RATE 200*1024*1024 //200M
+#define MCLK_1TICK_NS 5 // 1/(200M) = 5nsec
+
+#define SEC_TO_NSEC (1000 * 1000 * 1000)
+
+#ifdef CFG_FPGA
+#define TIMER_TICK_RATE 20480
+#else
+#define TIMER_TICK_RATE 32768
+#endif
+
+/* multiplier and shifter for 13MHz global timer */
+#ifdef CFG_FPGA
+#define OSTIMER_TIMER_MULT (419430400) /* 5M clk, 1000000000/(5*1000*1000) <<OSTIMER_TIMER_SHIFT */
+#else
+#define OSTIMER_TIMER_MULT (161319385)
+#endif
+#define OSTIMER_TIMER_SHIFT (21)
+
+#ifdef OSTIMER_LATCH_TIME_SUPPORT
+void init_latch_time(void);
+#else
+void init_latch_time(void)
+{
+}
+#endif
+
+struct timer_device *id_to_dev(unsigned int id)
+{
+ return id < NR_TMRS ? scp_timer + id : 0;
+}
+
+static void __timer_enable_irq(struct timer_device *dev)
+{
+ DRV_SetReg32(dev->base_addr + TIMER_IRQ_CTRL_REG, 0x1);
+}
+
+static void __timer_disable_irq(struct timer_device *dev)
+{
+ DRV_ClrReg32(dev->base_addr + TIMER_IRQ_CTRL_REG, 0x1);
+}
+
+static void __timer_ack_irq(struct timer_device *dev)
+{
+ DRV_SetReg32(dev->base_addr + TIMER_IRQ_CTRL_REG, TIMER_IRQ_CLEAR);
+}
+
+static void __timer_enable(struct timer_device *dev)
+{
+ DRV_SetReg32(dev->base_addr + TIMER_EN, 0x1);
+}
+
+static void __timer_disable(struct timer_device *dev)
+{
+ DRV_ClrReg32(dev->base_addr + TIMER_EN, 0x1);
+}
+
+static void __timer_set_clk(struct timer_device *dev, unsigned int clksrc)
+{
+ DRV_ClrReg32(dev->base_addr + TIMER_CLK_SRC,
+ TIMER_CLK_SRC_MASK << TIMER_CLK_SRC_SHIFT);
+ DRV_SetReg32(dev->base_addr + TIMER_CLK_SRC,
+ clksrc << TIMER_CLK_SRC_SHIFT);
+}
+
+static void __timer_set_rstval(struct timer_device *dev, unsigned int val)
+{
+ DRV_WriteReg32(dev->base_addr + TIMER_RST_VAL, val);
+}
+
+static void __timer_get_curval(struct timer_device *dev, unsigned long *ptr)
+{
+ *ptr = DRV_Reg32(dev->base_addr + TIMER_CUR_VAL_REG);
+}
+
+static void __timer_reset(struct timer_device *dev)
+{
+ __timer_disable(dev);
+ __timer_disable_irq(dev);
+ __timer_ack_irq(dev);
+ __timer_set_rstval(dev, 0);
+ __timer_set_clk(dev, TIMER_CLK_SRC_CLK_32K);
+}
+
+#if defined(CFG_MED_MCU_DVT)
+struct timer_device *get_dev_by_id(unsigned int id)
+{
+ return id_to_dev(id);
+}
+
+void timer_enable_irq(struct timer_device *dev)
+{
+ __timer_enable_irq(dev);
+}
+
+void timer_enable_set(struct timer_device *dev)
+{
+ __timer_enable(dev);
+}
+
+void timer_disable_set(struct timer_device *dev)
+{
+ __timer_disable(dev);
+}
+
+void timer_set_clk(struct timer_device *dev, unsigned int clksrc)
+{
+ __timer_set_clk(dev, clksrc);
+}
+
+void timer_set_rstval(struct timer_device *dev, unsigned int val)
+{
+ __timer_set_rstval(dev, val);
+}
+
+unsigned long timer_get_curval(struct timer_device *dev)
+{
+ unsigned long val;
+
+ __timer_get_curval(dev, &val);
+
+ return val;
+}
+#endif
+
+/* get ostimer counter */
+unsigned long long timer_get_global_timer_tick(void)
+{
+ unsigned long long val = 0;
+ unsigned long high = 0, low = 0;
+ unsigned long long new_high = 0, new_low = 0;
+
+ if (!is_in_isr())
+ taskENTER_CRITICAL();
+
+ low = DRV_Reg32(OSTIMER_CUR_L);
+ high = DRV_Reg32(OSTIMER_CUR_H);
+
+ if (!is_in_isr())
+ taskEXIT_CRITICAL();
+
+ new_high = high;
+ new_low = low;
+ val =
+ ((new_high << 32) & 0xFFFFFFFF00000000ULL) | (new_low &
+ 0x00000000FFFFFFFFULL);
+
+ return val;
+}
+
+static SYNC_SECTION unsigned long long ostimer_init_cycle;
+static unsigned long long ostimer_last_cycle;
+static unsigned int delay_get_current_tick(void);
+
+/* get ostimer timestamp */
+unsigned long long get_boot_time_ns(void)
+{
+ unsigned long long high = 0, low = 0;
+ unsigned long long cycle = 0;
+ unsigned long long timestamp = 0;
+
+ if (!is_in_isr())
+ taskENTER_CRITICAL();
+
+#ifdef CFG_USE_32K_HW_TIMER
+ low = DELAY_TIMER_RSTVAL - delay_get_current_tick();
+#else
+ low = DRV_Reg32(OSTIMER_CUR_L);
+ high = DRV_Reg32(OSTIMER_CUR_H);
+#endif
+
+ cycle = ((high << 32) & 0xFFFFFFFF00000000ULL) |
+ (low & 0x00000000FFFFFFFFULL);
+
+#ifndef CFG_USE_32K_HW_TIMER
+ if (cycle < ostimer_last_cycle)
+ cycle = ostimer_last_cycle + 1;
+#endif
+ ostimer_last_cycle = cycle;
+
+ if (!is_in_isr())
+ taskEXIT_CRITICAL();
+
+#ifndef CFG_USE_32K_HW_TIMER
+ cycle = cycle - ostimer_init_cycle;
+ high = (cycle >> 32) & 0x00000000FFFFFFFFULL;
+#endif
+ low = cycle & 0x00000000FFFFFFFFULL;
+
+#ifdef CFG_USE_32K_HW_TIMER
+ timestamp = (cycle * 1000000000) >> 15;
+#else
+ timestamp =
+ (((unsigned long long) high * OSTIMER_TIMER_MULT) << 11) +
+ (((unsigned long long) low *
+ OSTIMER_TIMER_MULT) >> OSTIMER_TIMER_SHIFT);
+#endif
+
+ return timestamp;
+}
+
+unsigned long long read_xgpt_stamp_ns(void)
+{
+ return get_boot_time_ns();
+}
+
+static void tick_timer_irq_handle(void);
+static void __clr_all_tmr_irq_ack(void);
+void mt_platform_timer_init(void)
+{
+ int i;
+ struct timer_device *dev;
+ g_timer_base = XGPT_BASE_REG;
+ /* enable clock */
+ DRV_SetReg32(TIMER_IN_CLK, (TMR_MCLK_CG | TMR_BCLK_CG));
+
+ for (i = 0; i < NR_TMRS; i++) {
+ scp_timer[i].id = i;
+ scp_timer[i].base_addr = SCP_TIMER_BASE + 0x10 * i;
+ }
+
+ scp_timer[0].irq = INTC_IRQ_XGPT0;
+ scp_timer[1].irq = INTC_IRQ_XGPT1;
+ scp_timer[2].irq = INTC_IRQ_XGPT2;
+ scp_timer[3].irq = INTC_IRQ_XGPT3;
+ scp_timer[4].irq = INTC_IRQ_XGPT4;
+ scp_timer[5].irq = INTC_IRQ_XGPT5;
+
+ __clr_all_tmr_irq_ack();
+
+ // reset timer
+ for (i = 0; i < NR_TMRS; i++) {
+ __timer_reset(&scp_timer[i]);
+ }
+
+ /* Setup timer to wakeup source for tickless mode to wakeup device. */
+ for (i = 0; i < NR_TMRS; i++) {
+ intc_irq_wakeup_set(&scp_timer[i].irq, 0x1);
+ }
+
+ /* enable delay GPT */
+ dev = id_to_dev(DELAY_TIMER);
+ __timer_set_rstval(dev, DELAY_TIMER_RSTVAL);
+ __timer_enable(dev);
+
+ mdelay(1);
+
+ dev = id_to_dev(TICK_TIMER);
+ intc_irq_request(&dev->irq, (void *) tick_timer_irq_handle, (void *) 0);
+
+ if (mrv_read_csr(CSR_MHARTID) == 0) {
+ ostimer_init_cycle = timer_get_global_timer_tick();
+ printf("[ostimer] ostimer_init_cycle = %llu cycles\n",
+ ostimer_init_cycle);
+ }
+ init_latch_time();
+}
+
+static void tick_timer_irq(void *arg)
+{
+ UNUSED(arg);
+
+ /*diff with before */
+ if (tick_timer_callback != NULL)
+ return tick_timer_callback(tick_timer_callback_arg);
+}
+
+static void tick_timer_irq_handle(void)
+{
+ struct timer_device *dev = id_to_dev(TICK_TIMER);
+
+ __timer_disable(dev);
+ __timer_disable_irq(dev);
+ __timer_ack_irq(dev);
+ tick_timer_irq(0);
+
+ return;
+}
+
+int platform_set_periodic_timer(platform_timer_callback callback, void *arg,
+ mt_time_t interval)
+{
+ struct timer_device *dev;
+ unsigned long long tmp_64 = 0;
+ unsigned long long interval_tmp = (unsigned long long) interval;
+
+ tick_timer_callback = callback;
+ tick_timer_callback_arg = arg;
+
+ tmp_64 = (unsigned long long) TIMER_TICK_RATE *interval_tmp;
+ tmp_64 = (tmp_64 / 1000ULL - 32);
+ dev = id_to_dev(TICK_TIMER);
+ __timer_disable(dev);
+
+ if (interval >= 1)
+ __timer_set_rstval(dev, (unsigned int) tmp_64); //0.3ms(sw)+0.7ms(hw wake)
+ else
+ __timer_set_rstval(dev, 1);
+ __timer_enable_irq(dev);
+
+ __timer_enable(dev);
+
+ return 0;
+}
+
+#ifdef CFG_CHRE_SUPPORT
+static void chre_timer_stop(void)
+{
+ struct timer_device *dev = id_to_dev(CHRE_TIMER);
+
+ __timer_disable(dev);
+}
+
+static void chre_timer_irq(void *arg)
+{
+ (void)*arg;
+ if (chre_timer_callback != NULL)
+ return chre_timer_callback(chre_timer_callback_arg);
+}
+
+static unsigned int chre_timer_irq_handle(void *arg)
+{
+ struct timer_device *dev = id_to_dev(CHRE_TIMER);
+ __timer_disable(dev);
+ __timer_disable_irq(dev);
+ __timer_ack_irq(dev);
+ chre_timer_irq(0);
+
+ return 0;
+}
+
+static int platform_set_periodic_timer_chre(platform_timer_callback callback,
+ void *arg, mt_time_t interval_ns)
+{
+ struct timer_device *dev;
+ unsigned long long interval_tick = 0;
+
+ chre_timer_callback = callback;
+ chre_timer_callback_arg = arg;
+
+ /* calculate how many ticks shall we wait */
+ interval_tick =
+ (interval_ns / 1000) * (unsigned long long) TIMER_TICK_RATE /
+ 1000000ULL;
+
+ dev = id_to_dev(CHRE_TIMER);
+
+ /* setup 1-tick timer if required tick is < 1 */
+ if (interval_tick >= 1)
+ __timer_set_rstval(dev, (unsigned int) interval_tick);
+ else
+ __timer_set_rstval(dev, 1);
+
+ __timer_enable_irq(dev);
+
+ intc_irq_request(&dev->irq, chre_timer_irq_handle, (void *)0);
+
+ __timer_enable(dev);
+
+ return 0;
+}
+#endif
+#if 1 // (RV only)
+void timer_cpu_tick_irq_ack(void)
+{
+ DRV_WriteReg32(TIMER_CPU_TICK_IRQ_CTRL, TIMERCPU_TICK_IRQ_CLR);
+}
+
+void platform_cpu_tick_disable(void)
+{
+ DRV_ClrReg32(GENERAL_CTRL, B_CPU_TIMER_INT_EN);
+}
+
+void platform_cpu_tick_enable(void)
+{
+ DRV_SetReg32(GENERAL_CTRL, B_CPU_TIMER_INT_EN);
+}
+
+/* interval: timer for systick clock */
+void platform_set_cpu_tick(int interval)
+{
+ DRV_WriteReg32(TIMER_CPU_TICK_RST_VAL, interval);
+ DRV_WriteReg32(TIMER_CPU_TICK_IRQ_CTRL,
+ TIMERCPU_TICK_IRQ_CLR | TIMER_CPU_TICK_IRQ_EN);
+ DRV_WriteReg32(TIMER_CPU_TICK_EN, 1);
+}
+
+static void __clr_all_tmr_irq_ack(void)
+{
+ /* clr tmr0~5 */
+ __timer_ack_irq(id_to_dev(TMR0));
+ __timer_ack_irq(id_to_dev(TMR1));
+ __timer_ack_irq(id_to_dev(TMR2));
+ __timer_ack_irq(id_to_dev(TMR3));
+ __timer_ack_irq(id_to_dev(TMR4));
+ __timer_ack_irq(id_to_dev(TMR5));
+ /* clr cpu tick */
+ timer_cpu_tick_irq_ack();
+ /* clr os timer */
+ DRV_WriteReg32(OSTIMER_IRQ_ACK, OSTIMER_TICK_IRQ_CLR);
+}
+#endif
+
+static unsigned int delay_get_current_tick(void)
+{
+ unsigned long current_count;
+ struct timer_device *dev = id_to_dev(DELAY_TIMER);
+
+ __timer_get_curval(dev, ¤t_count);
+ return current_count;
+}
+
+static int check_timeout_tick(unsigned int start_tick,
+ unsigned int timeout_tick)
+{
+ //register unsigned int cur_tick;
+ //register unsigned int elapse_tick;
+ unsigned int cur_tick;
+ unsigned int elapse_tick;
+ // get current tick
+ cur_tick = delay_get_current_tick();
+
+ // check elapse time, down counter
+ if (start_tick >= cur_tick) {
+ elapse_tick = start_tick - cur_tick;
+ } else {
+ elapse_tick = (DELAY_TIMER_RSTVAL - cur_tick) + start_tick;
+ }
+ // check if timeout
+ if (timeout_tick <= elapse_tick) {
+ // timeout
+ return 1;
+ }
+
+ return 0;
+}
+
+static unsigned int time2tick_us(unsigned int time_us)
+{
+ return TIME_TO_TICK_US(time_us);
+}
+
+static unsigned int time2tick_ms(unsigned int time_ms)
+{
+ return TIME_TO_TICK_MS(time_ms);
+}
+
+//===========================================================================
+// busy wait
+//===========================================================================
+static void busy_wait_us(unsigned int timeout_us)
+{
+ unsigned int start_tick, timeout_tick;
+
+ // get timeout tick
+ timeout_tick = time2tick_us(timeout_us);
+ start_tick = delay_get_current_tick();
+
+ // wait for timeout
+ while (!check_timeout_tick(start_tick, timeout_tick)) ;
+}
+
+static void busy_wait_ms(unsigned int timeout_ms)
+{
+ unsigned int start_tick, timeout_tick;
+
+ // get timeout tick
+ timeout_tick = time2tick_ms(timeout_ms);
+ start_tick = delay_get_current_tick();
+
+ // wait for timeout
+ while (!check_timeout_tick(start_tick, timeout_tick)) ;
+}
+
+/* delay msec mseconds */
+void mdelay(unsigned long msec)
+{
+ busy_wait_ms(msec);
+}
+
+/* delay usec useconds */
+void udelay(unsigned long usec)
+{
+ unsigned long usec_t;
+ if (usec < US_LIMIT) {
+ //PRINTF_D("usec < 31us, error parameter\n");
+ busy_wait_us(1);
+ } else {
+ usec_t = usec / 31 + 1;
+ busy_wait_us(usec_t);
+ }
+}
+
+#ifdef CFG_CHRE_SUPPORT
+//===========================================================================
+// chre feature
+//===========================================================================
+extern uint64_t cpuIntsOff(void);
+extern void cpuIntsRestore(uint64_t state);
+extern TaskHandle_t CHRE_TaskHandle;
+
+bool platSleepClockRequest(uint64_t wakeupTime, uint32_t maxJitterPpm,
+ uint32_t maxDriftPpm, uint32_t maxErrTotalPpm);
+static void chre_timer_wakeup(void *arg)
+{
+ extern int timIntHandler(void);
+ timIntHandler();
+ if (xTaskResumeFromISR(CHRE_TaskHandle) == pdTRUE)
+ portYIELD_WITHIN_API();
+}
+
+bool platSleepClockRequest(uint64_t wakeupTime, uint32_t maxJitterPpm,
+ uint32_t maxDriftPpm, uint32_t maxErrTotalPpm)
+{
+ (void)maxJitterPpm;
+ (void)maxDriftPpm;
+ (void)maxErrTotalPpm;
+ // uint64_t intState, curTime;
+ uint64_t curTime;
+ unsigned long long diff_time;
+ chre_timer_stop();
+ if (wakeupTime == 0) {
+ return 1;
+ }
+ curTime = (uint64_t) read_xgpt_stamp_ns();
+
+ if (wakeupTime && curTime >= wakeupTime)
+ return 0;
+
+ /* diff_time unit: ns */
+ diff_time = wakeupTime - curTime;
+
+ // intState = cpuIntsOff();
+ // TODO: set an actual alarm here so that if we keep running and do not sleep till this is due,
+ // we still fire an interrupt for it!
+ platform_set_periodic_timer_chre(chre_timer_wakeup, NULL, diff_time);
+ // cpuIntsRestore(intState);
+ return 1;
+}
+#endif
+
+void (*rttimercbk)(void);
+
+static unsigned int rt_timer_irq_handler(void *arg)
+{
+ struct timer_device *dev = id_to_dev(RT_TIMER);
+ __timer_disable(dev);
+ __timer_disable_irq(dev);
+ __timer_ack_irq(dev);
+ if (rttimercbk != NULL)
+ rttimercbk();
+ return 0;
+}
+
+unsigned int program_hwtimer(void (*callback)(void), uint64_t diff_time)
+{
+ struct timer_device *dev = id_to_dev(RT_TIMER);
+ unsigned long long interval_tick = 0;
+
+ __timer_disable(dev);
+
+ if (diff_time == 0) {
+ return 1;
+ }
+ /* calculate how many ticks shall we wait */
+ interval_tick =
+ (diff_time / 1000) * (unsigned long long)TIMER_TICK_RATE /
+ 1000000ULL;
+
+ /* setup 1-tick timer if required tick is < 1 */
+ if (interval_tick >= 1)
+ __timer_set_rstval(dev, (unsigned int)interval_tick);
+ else
+ __timer_set_rstval(dev, 1);
+
+ __timer_enable_irq(dev);
+
+ rttimercbk = callback;
+
+ intc_irq_request(&dev->irq, rt_timer_irq_handler, (void *)0);
+
+ __timer_enable(dev);
+
+ return 1;
+}
+
+#ifdef OSTIMER_LATCH_TIME_SUPPORT
+#define MAX_LATCH_TIMER 3
+#define CRTL_BIT_SHIFT 8
+#define IRQ_BIT_SHIFT 0
+#define ENABLE_BIT_SHIFT 5
+#define CYC_BASE_SHIFT 8
+struct latch_time_struct {
+ unsigned int ctrl_base;
+ unsigned int enable_offset;
+ unsigned int irq_offset;
+ unsigned int cyc_low_base;
+ unsigned int cyc_high_base;
+};
+static struct latch_time_struct latch_time[MAX_LATCH_TIMER];
+static unsigned int latch_time_used[MAX_LATCH_TIMER];
+void init_latch_time(void)
+{
+ int i = 0;
+ for (i = 0; i < MAX_LATCH_TIMER; ++i) {
+ latch_time[i].ctrl_base = OS_TIMER_LATCH_CTRL;
+ latch_time[i].enable_offset =
+ i * CRTL_BIT_SHIFT + ENABLE_BIT_SHIFT;
+ latch_time[i].irq_offset = i * CRTL_BIT_SHIFT + IRQ_BIT_SHIFT;
+ latch_time[i].cyc_low_base =
+ OS_TIMER_LATCH_VALUE_0 + i * CYC_BASE_SHIFT;
+ latch_time[i].cyc_high_base = latch_time[i].cyc_low_base + 4;
+
+ latch_time_used[i] = 0;
+ }
+ DRV_WriteReg32(OS_TIMER_LATCH_CTRL, 0);
+}
+
+void enable_latch_time(int id, int irq)
+{
+ unsigned int base = 0, control = 0;
+
+ if (id < 0 || id >= MAX_LATCH_TIMER)
+ return;
+ if (irq > 0x1f)
+ return;
+ base = latch_time[id].ctrl_base;
+ control = ((1 << latch_time[id].enable_offset) |
+ (irq << latch_time[id].irq_offset));
+ if (!is_in_isr())
+ taskENTER_CRITICAL();
+ DRV_SetReg32(base, control);
+ if (!is_in_isr())
+ taskEXIT_CRITICAL();
+}
+
+void disable_latch_time(int id)
+{
+ unsigned int base = 0, control = 0;
+
+ if (id < 0 || id >= MAX_LATCH_TIMER)
+ return;
+ base = latch_time[id].ctrl_base;
+ control = ((0 << latch_time[id].enable_offset) |
+ (0 << latch_time[id].irq_offset));
+ if (!is_in_isr())
+ taskENTER_CRITICAL();
+ DRV_SetReg32(base, control);
+ if (!is_in_isr())
+ taskEXIT_CRITICAL();
+}
+
+int alloc_latch_time(void)
+{
+ int i = 0, id = -1;
+ if (!is_in_isr())
+ taskENTER_CRITICAL();
+ for (i = 0; i < MAX_LATCH_TIMER; ++i) {
+ if (latch_time_used[i] == 0) {
+ latch_time_used[i] = 1;
+ id = i;
+ break;
+ }
+ }
+ if (!is_in_isr())
+ taskEXIT_CRITICAL();
+ return id;
+}
+
+void free_latch_time(int id)
+{
+ if (id < 0 || id >= MAX_LATCH_TIMER)
+ return;
+ if (!is_in_isr())
+ taskENTER_CRITICAL();
+ latch_time_used[id] = 0;
+ if (!is_in_isr())
+ taskEXIT_CRITICAL();
+}
+
+static unsigned long long get_latch_time_counter(struct latch_time_struct *base)
+{
+ unsigned long long val;
+ unsigned long high_1, high_2, low_1, low_2;
+
+ if (!is_in_isr())
+ taskENTER_CRITICAL();
+
+ low_1 = DRV_Reg32(base->cyc_low_base);
+ high_1 = DRV_Reg32(base->cyc_high_base);
+ low_2 = DRV_Reg32(base->cyc_low_base);
+ high_2 = DRV_Reg32(base->cyc_high_base);
+
+ if (low_2 < low_1) {
+ high_1 = high_2;
+ low_1 = low_2;
+ }
+
+ val = (((unsigned long long) high_1 << 32) & 0xFFFFFFFF00000000) |
+ ((unsigned long long) low_1 & 0x00000000FFFFFFFF);
+
+ if (!is_in_isr())
+ taskEXIT_CRITICAL();
+
+ return val;
+}
+
+uint64_t get_latch_time_timestamp(int id)
+{
+ unsigned long long cycle = 0;
+ unsigned long long high = 0, low = 0;
+ uint64_t timestamp = 0;
+
+ if (id < 0 || id >= MAX_LATCH_TIMER)
+ return 0;
+ cycle = get_latch_time_counter(&latch_time[id]);
+
+ cycle = cycle - ostimer_init_cycle;
+ high = (cycle >> 32) & 0x00000000FFFFFFFFULL;
+ low = cycle & 0x00000000FFFFFFFFULL;
+ timestamp = (((unsigned long long) high * OSTIMER_TIMER_MULT) << 11) +
+ (((unsigned long long) low *
+ OSTIMER_TIMER_MULT) >> OSTIMER_TIMER_SHIFT);
+ return timestamp;
+}
+#endif