1 /**
2 * \ingroup MODULMACROSX
3 *
4 * \file RestoreContext_ReleaseJtagX.c
5 *
6 * \brief Restore the CPU context and releast Jtag control
7 *
8 */
9 /*
10  * Copyright (C) 2012 Texas Instruments Incorporated - http://www.ti.com/
11  *
12  *
13  *  Redistribution and use in source and binary forms, with or without
14  *  modification, are permitted provided that the following conditions
15  *  are met:
16  *
17  *    Redistributions of source code must retain the above copyright
18  *    notice, this list of conditions and the following disclaimer.
19  *
20  *    Redistributions in binary form must reproduce the above copyright
21  *    notice, this list of conditions and the following disclaimer in the
22  *    documentation and/or other materials provided with the
23  *    distribution.
24  *
25  *    Neither the name of Texas Instruments Incorporated nor the names of
26  *    its contributors may be used to endorse or promote products derived
27  *    from this software without specific prior written permission.
28  *
29  *  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
30  *  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
31  *  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
32  *  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
33  *  OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
34  *  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
35  *  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
36  *  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
37  *  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
38  *  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
39  *  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40  */
41 
42 #include "error_def.h"
43 #include "arch.h"
44 #include "edt.h"
45 #include "hal.h"
46 #include "hal_ref.h"
47 #include "stream.h"
48 #include "EEM_defs.h"
49 #include "global_variables.h"
50 
51 extern DeviceSettings deviceSettings;
52 
53 
HAL_FUNCTION(_hal_RestoreContext_ReleaseJtagX)54 HAL_FUNCTION(_hal_RestoreContext_ReleaseJtagX)
55 {
56     unsigned short wdt_addr;
57     unsigned short wdt_value;
58     unsigned long  pc;
59     unsigned short sr;
60     unsigned short control_mask;
61     unsigned short mdb;
62     unsigned short releaseJtag;
63     unsigned short* syncWithRunVarAddress = 0;
64 
65     if(STREAM_get_word(&wdt_addr) != 0)
66     {
67       return HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_WDT_ADDRESS;
68     }
69     if(STREAM_get_word(&wdt_value) != 0)
70     {
71       return HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_WDT_VALUE;
72     }
73     if(STREAM_get_long(&pc) != 0)
74     {
75       return HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_PC;
76     }
77     if(STREAM_get_word(&sr) != 0)
78     {
79       return HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_SR;
80     }
81     if(STREAM_get_word(&control_mask) != 0)
82     {
83       return HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_CONTROL_MASK;
84     }
85     if(STREAM_get_word(&mdb) != 0)
86     {
87       return HALERR_RESTORECONTEXT_RELEASE_JTAG_NO_MDB;
88     }
89     if (STREAM_get_word(&releaseJtag) == -1)
90     {
91       releaseJtag = 0;
92     }
93     STREAM_discard_bytes(2);
94 
95     // Write back Status Register
96     WriteCpuRegX(2, sr);
97 
98     // Restore Watchdog Control Register
99     WriteMemWordX(wdt_addr, wdt_value);
100 
101     // restore Program Counter
102     SetPcX(pc);
103 
104     if (deviceSettings.clockControlType == GCC_EXTENDED)
105     {
106         eem_data_exchange32();
107         SetReg_32Bits(MX_GENCNTRL + MX_WRITE);              // write access to EEM General Control Register (MX_GENCNTRL)
108         SetReg_32Bits(EMU_FEAT_EN | EMU_CLK_EN | CLEAR_STOP | EEM_EN);
109     }
110 
111     // activate EEM
112     eem_write_control();
113     SetReg_16Bits(control_mask);
114 
115     // Pre-initialize MDB before release if
116     if(mdb)
117     {
118         data_16bit();
119         SetReg_16Bits(mdb);
120         IHIL_Tclk(0);
121         addr_capture();
122         IHIL_Tclk(1);
123     }
124     else
125     {
126       addr_capture();
127     }
128 
129     syncWithRunVarAddress = getTargetRunningVar();
130     if(syncWithRunVarAddress)
131     {
132           *syncWithRunVarAddress = 0x0001;
133     }
134 
135     // release target device from JTAG control
136     cntrl_sig_release();
137 
138     if(releaseJtag)
139     {
140         IHIL_Close(); // release JTAG on go
141     }
142 
143     return(0);
144 }
145