1 /******************************************************************************
2 *  Filename:       vims.c
3 *  Revised:        2015-09-21 15:46:11 +0200 (Mon, 21 Sep 2015)
4 *  Revision:       44630
5 *
6 *  Description:    Driver for the VIMS.
7 *
8 *  Copyright (c) 2015, Texas Instruments Incorporated
9 *  All rights reserved.
10 *
11 *  Redistribution and use in source and binary forms, with or without
12 *  modification, are permitted provided that the following conditions are met:
13 *
14 *  1) Redistributions of source code must retain the above copyright notice,
15 *     this list of conditions and the following disclaimer.
16 *
17 *  2) Redistributions in binary form must reproduce the above copyright notice,
18 *     this list of conditions and the following disclaimer in the documentation
19 *     and/or other materials provided with the distribution.
20 *
21 *  3) Neither the name of the ORGANIZATION nor the names of its contributors may
22 *     be used to endorse or promote products derived from this software without
23 *     specific prior written permission.
24 *
25 *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 *  AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 *  IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 *  ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
29 *  LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 *  CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 *  SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 *  INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 *  CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 *  ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
35 *  POSSIBILITY OF SUCH DAMAGE.
36 *
37 ******************************************************************************/
38 
39 #include <driverlib/vims.h>
40 
41 //*****************************************************************************
42 //
43 // Handle support for DriverLib in ROM:
44 // This section will undo prototype renaming made in the header file
45 //
46 //*****************************************************************************
47 #if !defined(DOXYGEN)
48     #undef  VIMSConfigure
49     #define VIMSConfigure                   NOROM_VIMSConfigure
50     #undef  VIMSModeSet
51     #define VIMSModeSet                     NOROM_VIMSModeSet
52     #undef  VIMSModeGet
53     #define VIMSModeGet                     NOROM_VIMSModeGet
54     #undef  VIMSModeSafeSet
55     #define VIMSModeSafeSet                 NOROM_VIMSModeSafeSet
56 #endif
57 
58 //*****************************************************************************
59 //
60 // Configures the VIMS.
61 //
62 //*****************************************************************************
63 void
VIMSConfigure(uint32_t ui32Base,bool bRoundRobin,bool bPrefetch)64 VIMSConfigure(uint32_t ui32Base, bool bRoundRobin, bool bPrefetch)
65 {
66     uint32_t ui32Reg;
67 
68     //
69     // Check the arguments.
70     //
71     ASSERT(VIMSBaseValid(ui32Base));
72 
73     ui32Reg = HWREG(ui32Base + VIMS_O_CTL);
74     ui32Reg &= ~(VIMS_CTL_PREF_EN | VIMS_CTL_ARB_CFG);
75     if(bRoundRobin)
76     {
77         ui32Reg |= VIMS_CTL_ARB_CFG;
78     }
79     if(bPrefetch)
80     {
81         ui32Reg |= VIMS_CTL_PREF_EN;
82     }
83 
84     //
85     // Set the Arbitration and prefetch mode.
86     //
87     HWREG(ui32Base + VIMS_O_CTL) = ui32Reg;
88 }
89 
90 //*****************************************************************************
91 //
92 // Set the operational mode of the VIMS
93 //
94 //*****************************************************************************
95 void
VIMSModeSet(uint32_t ui32Base,uint32_t ui32Mode)96 VIMSModeSet(uint32_t ui32Base, uint32_t ui32Mode)
97 {
98     uint32_t ui32Reg;
99 
100     //
101     // Check the arguments.
102     //
103     ASSERT(VIMSBaseValid(ui32Base));
104 
105     ASSERT((ui32Mode == VIMS_MODE_DISABLED)   ||
106            (ui32Mode == VIMS_MODE_ENABLED)    ||
107            (ui32Mode == VIMS_MODE_OFF));
108 
109     //
110     // Set the mode.
111     //
112     ui32Reg = HWREG(ui32Base + VIMS_O_CTL);
113     ui32Reg &= ~VIMS_CTL_MODE_M;
114     ui32Reg |= (ui32Mode & VIMS_CTL_MODE_M);
115 
116     HWREG(ui32Base + VIMS_O_CTL) = ui32Reg;
117 }
118 
119 //*****************************************************************************
120 //
121 // Get the current operational mode of the VIMS.
122 //
123 //*****************************************************************************
124 uint32_t
VIMSModeGet(uint32_t ui32Base)125 VIMSModeGet(uint32_t ui32Base)
126 {
127     uint32_t ui32Reg;
128 
129     //
130     // Check the arguments.
131     //
132     ASSERT(VIMSBaseValid(ui32Base));
133 
134     ui32Reg = HWREG(ui32Base + VIMS_O_STAT);
135     if(ui32Reg & VIMS_STAT_MODE_CHANGING)
136     {
137         return (VIMS_MODE_CHANGING);
138     }
139     else
140     {
141         return (ui32Reg & VIMS_STAT_MODE_M);
142     }
143 }
144 
145 //*****************************************************************************
146 //
147 // Safe setting of new VIMS mode
148 // - Function might be blocking
149 // - Can be called for any mode change (also if actually not changing mode)
150 //
151 //*****************************************************************************
152 void
VIMSModeSafeSet(uint32_t ui32Base,uint32_t ui32NewMode,bool blocking)153 VIMSModeSafeSet( uint32_t ui32Base, uint32_t ui32NewMode, bool blocking )
154 {
155     uint32_t currentMode;
156 
157     //
158     // Check the arguments.
159     //
160     ASSERT(VIMSBaseValid(ui32Base));
161     ASSERT((ui32NewMode == VIMS_MODE_DISABLED)   ||
162            (ui32NewMode == VIMS_MODE_ENABLED)    ||
163            (ui32NewMode == VIMS_MODE_OFF));
164 
165     //
166     // Make sure that only the mode bits are set in the input parameter
167     // (done just for security since it is critical to the code flow)
168     //
169     ui32NewMode &= VIMS_CTL_MODE_M;
170 
171     //
172     // Wait for any pending change to complete and get current VIMS mode
173     // (This is a blocking point but will typically only be a blocking point
174     // only if mode is changed multiple times with blocking=0)
175     //
176     do {
177         currentMode = VIMSModeGet( ui32Base );
178     } while ( currentMode == VIMS_MODE_CHANGING );
179 
180     //
181     // First check that it actually is a mode change request
182     //
183     if ( ui32NewMode != currentMode ) {
184         //
185         // Due to a hw-problem it is strongly recommended to go via VIMS_MODE_OFF
186         // when leaving VIMS_MODE_ENABLED (=VIMS_CTL_MODE_CACHE)
187         // (And no need to go via OFF, if OFF is the final state and will be set later)
188         //
189         if (( currentMode == VIMS_CTL_MODE_CACHE ) &&
190             ( ui32NewMode != VIMS_CTL_MODE_OFF   )    )
191         {
192             VIMSModeSet( ui32Base, VIMS_MODE_OFF );
193 
194             while ( HWREGBITW( VIMS_BASE + VIMS_O_STAT, VIMS_STAT_MODE_CHANGING_BITN )) {
195                 // Do nothing - wait for change to complete.
196                 // (Needed blocking point but it takes only some few cycles)
197             }
198         }
199         //
200         // Set new mode
201         //
202         VIMSModeSet( ui32Base, ui32NewMode );
203 
204         //
205         // Wait for final mode change to complete - if blocking is requested
206         //
207         if ( blocking ) {
208             while ( HWREGBITW( VIMS_BASE + VIMS_O_STAT, VIMS_STAT_MODE_CHANGING_BITN )) {
209                 // Do nothing - wait for change to complete.
210             }
211         }
212     }
213 }
214