1 /*
2  * Copyright (c) 2021 - 2023, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * *  Redistributions of source code must retain the above copyright
10  *    notice, this list of conditions and the following disclaimer.
11  *
12  * *  Redistributions in binary form must reproduce the above copyright
13  *    notice, this list of conditions and the following disclaimer in the
14  *    documentation and/or other materials provided with the distribution.
15  *
16  * *  Neither the name of Texas Instruments Incorporated nor the names of
17  *    its contributors may be used to endorse or promote products derived
18  *    from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #include <stdint.h>
34 
35 #include <ti/drivers/dpl/DebugP.h>
36 #include <ti/drivers/dpl/HwiP.h>
37 
38 #include <ti/drivers/dma/UDMALPF3.h>
39 
40 #include <ti/devices/DeviceFamily.h>
41 #include DeviceFamily_constructPath(inc/hw_memmap.h)
42 #include DeviceFamily_constructPath(inc/hw_ints.h)
43 #include DeviceFamily_constructPath(driverlib/udma.h)
44 
45 /* Externs */
46 extern const UDMALPF3_Config UDMALPF3_config;
47 
48 static void UDMALPF3_initHw(void);
49 static int UDMALPF3_postNotifyFxn(unsigned int eventType, uintptr_t eventArg, uintptr_t clientArg);
50 
51 static bool isOpen = false;
52 static Power_NotifyObj postNotify;
53 
54 /*
55  *  ======== UDMALPF3_init ========
56  *
57  */
UDMALPF3_init(void)58 void UDMALPF3_init(void)
59 {
60     /* Init the DMA HW only once */
61     if (!isOpen)
62     {
63         isOpen = true;
64 
65         /* Register power notification function */
66         Power_registerNotify(&postNotify, PowerLPF3_AWAKE_STANDBY, UDMALPF3_postNotifyFxn, (uintptr_t)NULL);
67 
68         /* Power up and enable clocks for uDMA. */
69         Power_setDependency(PowerLPF3_PERIPH_DMA);
70 
71         /* initialize the UDMALPF3 hardware */
72         UDMALPF3_initHw();
73 
74         /* Disable clock on the DMA peripheral */
75         Power_releaseDependency(PowerLPF3_PERIPH_DMA);
76     }
77 }
78 
79 /*
80  *  ======== UDMALPF3_initHw ========
81  *  This functions initializes the UDMALPF3 hardware module.
82  *
83  */
UDMALPF3_initHw(void)84 static void UDMALPF3_initHw(void)
85 {
86     /* Disable all channels */
87     UDMALPF3_channelDisable(0xFF);
88 
89     /* clear the error register */
90     uDMAClearErrorStatus();
91 
92     /* Set the base for the channel control table. */
93     uDMASetControlBase((void *)UDMALPF3_config.CtrlBaseAddr);
94 
95     /* Enable uDMA. */
96     uDMAEnable();
97 }
98 
99 /*
100  *  ======== UDMALPF3_postNotifyFxn ========
101  *  Called by Power module when waking up from LPDS.
102  */
UDMALPF3_postNotifyFxn(unsigned int eventType,uintptr_t eventArg,uintptr_t clientArg)103 static int UDMALPF3_postNotifyFxn(unsigned int eventType, uintptr_t eventArg, uintptr_t clientArg)
104 {
105     /* Reconfigure the hardware if returning from sleep */
106     if (eventType == PowerLPF3_AWAKE_STANDBY)
107     {
108         /* Power up and enable clocks for uDMA. */
109         Power_setDependency(PowerLPF3_PERIPH_DMA);
110 
111         /* initialize the UDMALPF3 hardware */
112         UDMALPF3_initHw();
113 
114         /* Disable clock on the DMA peripheral */
115         Power_releaseDependency(PowerLPF3_PERIPH_DMA);
116     }
117 
118     return (Power_NOTIFYDONE);
119 }