1 /******************************************************************************
2  *  Filename:       lrfd.c
3  *
4  *  Description:    Driver for LRFD
5  *
6  *  Copyright (c) 2023-2024 Texas Instruments Incorporated
7  *
8  *  Redistribution and use in source and binary forms, with or without
9  *  modification, are permitted provided that the following conditions are met:
10  *
11  *  1) Redistributions of source code must retain the above copyright notice,
12  *     this list of conditions and the following disclaimer.
13  *
14  *  2) Redistributions in binary form must reproduce the above copyright notice,
15  *     this list of conditions and the following disclaimer in the documentation
16  *     and/or other materials provided with the distribution.
17  *
18  *  3) Neither the name of the copyright holder nor the names of its
19  *     contributors may be used to endorse or promote products derived from this
20  *     software without specific prior written permission.
21  *
22  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
23  *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24  *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25  *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
26  *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
27  *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
28  *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29  *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
30  *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
31  *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
32  *  POSSIBILITY OF SUCH DAMAGE.
33  *
34  ******************************************************************************/
35 
36 #include "lrfd.h"
37 
38 #include <stdbool.h>
39 #include "../inc/hw_types.h"
40 #include "../inc/hw_memmap.h"
41 #include "../inc/hw_clkctl.h"
42 #include "../inc/hw_lrfddbell.h"
43 
44 #include "../driverlib/interrupt.h"
45 
46 static uint16_t lrfdClockDependencySets[LRFD_NUM_CLK_DEP];
47 
48 //*****************************************************************************
49 //
50 // Set clock dependency on a set of modules in LRFD.
51 //
52 //*****************************************************************************
LRFDSetClockDependency(uint16_t mask,uint8_t dependencySetId)53 void LRFDSetClockDependency(uint16_t mask, uint8_t dependencySetId)
54 {
55     if (dependencySetId < LRFD_NUM_CLK_DEP)
56     {
57         lrfdClockDependencySets[dependencySetId] |= mask;
58     }
59     LRFDApplyClockDependencies();
60 }
61 
62 //*****************************************************************************
63 //
64 // Release clock dependency on a set of modules in LRFD.
65 //
66 //*****************************************************************************
LRFDReleaseClockDependency(uint16_t mask,uint8_t dependencySetId)67 void LRFDReleaseClockDependency(uint16_t mask, uint8_t dependencySetId)
68 {
69     if (dependencySetId < LRFD_NUM_CLK_DEP)
70     {
71         lrfdClockDependencySets[dependencySetId] &= ~mask;
72     }
73     LRFDApplyClockDependencies();
74 }
75 
76 //*****************************************************************************
77 //
78 // Apply the currently set clock dependencies.
79 //
80 //*****************************************************************************
LRFDApplyClockDependencies(void)81 void LRFDApplyClockDependencies(void)
82 {
83     uint16_t clkctl  = 0;
84     bool lrfdClocked = (HWREG(CLKCTL_BASE + CLKCTL_O_CLKCFG0) & CLKCTL_CLKCFG0_LRFD_M) == CLKCTL_CLKCFG0_LRFD_CLK_EN;
85     for (int i = 0; i < LRFD_NUM_CLK_DEP; i++)
86     {
87         clkctl |= lrfdClockDependencySets[i];
88     }
89 
90     if (lrfdClocked)
91     {
92         // BRIDGE bit should not be needed, as hardware will automatically
93         // enable the clock when needed. The bit should be always be 0 in the
94         // HW, and is thus cleared.
95         HWREG(LRFDDBELL_BASE + LRFDDBELL_O_CLKCTL) = clkctl & ~LRFDDBELL_CLKCTL_BRIDGE_M;
96 
97         if (clkctl == 0)
98         {
99             // Disable LRFD module clock
100             HWREG( CLKCTL_BASE + CLKCTL_O_CLKENCLR0 ) = CLKCTL_CLKENCLR0_LRFD;
101             lrfdClocked                               = false;
102         }
103     }
104     else
105     {
106         if (clkctl != 0)
107         {
108             // Enable LRFD module clock
109             HWREG( CLKCTL_BASE + CLKCTL_O_CLKENSET0 ) = CLKCTL_CLKENSET0_LRFD;
110 
111             // Wait for LRFD clock to be enabled. It is not expected that the
112             // LRFD clock will ever not be enabled, but this will add sufficient
113             // delay before enabling the internal LRFD clocks below.
114             while ((HWREG(CLKCTL_BASE + CLKCTL_O_CLKCFG0) & CLKCTL_CLKCFG0_LRFD_M) != CLKCTL_CLKCFG0_LRFD_CLK_EN) {}
115 
116             // BRIDGE bit should not be needed, as hardware will automatically
117             // enable the clock when needed. The bit should be always be 0 in
118             // the HW, and is thus cleared. The bit can be used in the input to
119             // indicate the need for the LRFD module clock to be enabled, but no
120             // internal LRFD clocks.
121             HWREG(LRFDDBELL_BASE + LRFDDBELL_O_CLKCTL) = clkctl & ~LRFDDBELL_CLKCTL_BRIDGE_M;
122         }
123     }
124 }