1 /*
2  * Copyright (c) 2016, 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 <ti/drivers/dpl/DebugP.h>
34 #include <ti/drivers/dpl/HwiP.h>
35 
36 #include <ti/drivers/Power.h>
37 #include <ti/drivers/power/PowerCC32XX.h>
38 #include <ti/drivers/dma/UDMACC32XX.h>
39 
40 #include <ti/devices/cc32xx/inc/hw_ints.h>
41 #include <ti/devices/cc32xx/inc/hw_memmap.h>
42 #include <ti/devices/cc32xx/inc/hw_types.h>
43 #include <ti/devices/cc32xx/driverlib/rom.h>
44 #include <ti/devices/cc32xx/driverlib/rom_map.h>
45 #include <ti/devices/cc32xx/driverlib/prcm.h>
46 #include <ti/devices/cc32xx/driverlib/udma.h>
47 
48 extern const UDMACC32XX_Config UDMACC32XX_config[];
49 
50 static int postNotifyFxn(unsigned int eventType, uintptr_t eventArg,
51         uintptr_t clientArg);
52 
53 static bool dmaInitialized = false;
54 static Power_NotifyObj postNotifyObj;    /* LPDS wake-up notify object */
55 
56 /* Reference count for open calls */
57 static uint32_t          refCount = 0;
58 
59 /*
60  *  ======== UDMACC32XX_close ========
61  */
UDMACC32XX_close(UDMACC32XX_Handle handle)62 void UDMACC32XX_close(UDMACC32XX_Handle handle)
63 {
64     UDMACC32XX_Object    *object = handle->object;
65     uintptr_t             key;
66 
67     Power_releaseDependency(PowerCC32XX_PERIPH_UDMA);
68 
69     key = HwiP_disable();
70 
71     refCount--;
72 
73     if (refCount == 0) {
74         Power_unregisterNotify(&postNotifyObj);
75         object->isOpen = false;
76     }
77 
78     HwiP_restore(key);
79 }
80 
81 /*
82  *  ======== UDMACC32XX_init ========
83  */
UDMACC32XX_init()84 void UDMACC32XX_init()
85 {
86     HwiP_Params           hwiParams;
87     UDMACC32XX_Handle     handle = (UDMACC32XX_Handle)&(UDMACC32XX_config[0]);
88     UDMACC32XX_HWAttrs    const *hwAttrs = handle->hwAttrs;
89     UDMACC32XX_Object    *object = handle->object;
90 
91     if (!dmaInitialized) {
92         object->isOpen = false;
93 
94         HwiP_Params_init(&hwiParams);
95         hwiParams.priority = hwAttrs->intPriority;
96 
97         /* Will check in UDMACC32XX_open() if this failed */
98         object->hwiHandle = HwiP_create(hwAttrs->intNum, hwAttrs->dmaErrorFxn,
99                 &hwiParams);
100         if (object->hwiHandle == NULL) {
101             DebugP_log0("Failed to create uDMA error Hwi!!\n");
102         }
103         else {
104             dmaInitialized = true;
105         }
106     }
107 }
108 
109 /*
110  *  ======== UDMACC32XX_open ========
111  */
UDMACC32XX_open()112 UDMACC32XX_Handle UDMACC32XX_open()
113 {
114     UDMACC32XX_Handle     handle = (UDMACC32XX_Handle)&(UDMACC32XX_config);
115     UDMACC32XX_Object    *object = handle->object;
116     UDMACC32XX_HWAttrs    const *hwAttrs = handle->hwAttrs;
117     uintptr_t             key;
118 
119     if (!dmaInitialized) {
120         return (NULL);
121     }
122 
123     Power_setDependency(PowerCC32XX_PERIPH_UDMA);
124 
125     key = HwiP_disable();
126 
127     /*
128      *  If the UDMA has not been opened yet, create the error Hwi
129      *  and initialize the control table base address.
130      */
131     if (object->isOpen == false) {
132         MAP_PRCMPeripheralReset(PRCM_UDMA);
133 
134         MAP_uDMAEnable();
135         MAP_uDMAControlBaseSet(hwAttrs->controlBaseAddr);
136 
137         Power_registerNotify(&postNotifyObj, PowerCC32XX_AWAKE_LPDS,
138                 postNotifyFxn, (uintptr_t)handle);
139 
140         object->isOpen = true;
141     }
142 
143     refCount++;
144 
145     HwiP_restore(key);
146 
147     return (handle);
148 }
149 
150 /*
151  *  ======== postNotifyFxn ========
152  *  Called by Power module when waking up from LPDS.
153  */
postNotifyFxn(unsigned int eventType,uintptr_t eventArg,uintptr_t clientArg)154 static int postNotifyFxn(unsigned int eventType, uintptr_t eventArg,
155         uintptr_t clientArg)
156 {
157     UDMACC32XX_Handle handle = (UDMACC32XX_Handle)clientArg;
158     UDMACC32XX_HWAttrs const *hwAttrs = handle->hwAttrs;
159 
160     MAP_uDMAEnable();
161     MAP_uDMAControlBaseSet(hwAttrs->controlBaseAddr);
162 
163     return (Power_NOTIFYDONE);
164 }
165