]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/drivers/Wdg.c
Merge with ac7a40bc631fb2dfca15ae7785ba2aa768ee23d8
[arc.git] / arch / ppc / mpc55xx / drivers / Wdg.c
1 /* -------------------------------- Arctic Core ------------------------------\r
2  * Arctic Core - the open source AUTOSAR platform http://arccore.com\r
3  *\r
4  * Copyright (C) 2009  ArcCore AB <contact@arccore.com>\r
5  *\r
6  * This source code is free software; you can redistribute it and/or modify it\r
7  * under the terms of the GNU General Public License version 2 as published by the\r
8  * Free Software Foundation; See <http://www.gnu.org/licenses/old-licenses/gpl-2.0.txt>.\r
9  *\r
10  * This program is distributed in the hope that it will be useful, but\r
11  * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY\r
12  * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License\r
13  * for more details.\r
14  * -------------------------------- Arctic Core ------------------------------*/\r
15 \r
16 #include "mpc55xx.h"\r
17 #include "Wdg.h"\r
18 #include "Mcu.h"\r
19 \r
20 \r
21 static const Wdg_ConfigType *configWdgPtr;\r
22 static const Wdg_SettingsType *modeWdgConfig;\r
23 \r
24 \r
25 \r
26 void StartWatchdog(void)\r
27 {\r
28 #if defined(CFG_MPC5567)\r
29         ECSM.SWTCR.R =  0x00D8;;\r
30 #elif defined(CFG_MPC560X) || defined(CFG_MPC5668)\r
31         SWT.CR.R = 0x8000011B;\r
32 #else\r
33         MCM.SWTCR.R = 0x00D8;\r
34 #endif\r
35 \r
36 }\r
37 \r
38  void StopWatchdog(void)\r
39  {\r
40  #if defined(CFG_MPC5567)\r
41         ECSM.SWTCR.R =  0x0059;;\r
42  #elif defined(CFG_MPC560X) || defined(CFG_MPC5668)\r
43         SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */\r
44         SWT.SR.R = 0x0000d928;\r
45         SWT.CR.R = 0x8000010A;\r
46  #else\r
47         MCM.SWTCR.R = 0x0059;\r
48  #endif\r
49  }\r
50 \r
51 void Wdg_Init (const Wdg_ConfigType* ConfigPtr)\r
52 {\r
53         /* Keep a pointer to the config. */\r
54         configWdgPtr = ConfigPtr;\r
55 \r
56         Wdg_SetMode(ConfigPtr->Wdg_ModeConfig->Wdg_DefaultMode);\r
57 }\r
58 \r
59 Std_ReturnType Wdg_SetMode (WdgIf_ModeType Mode)\r
60 {\r
61         Std_ReturnType res = E_NOT_OK;\r
62         switch (Mode)\r
63         {\r
64         case WDGIF_OFF_MODE:\r
65                 modeWdgConfig = &configWdgPtr->Wdg_ModeConfig->WdgSettingsOff;\r
66                 break;\r
67         case WDGIF_FAST_MODE:\r
68                 modeWdgConfig = &configWdgPtr->Wdg_ModeConfig->WdgSettingsFast;\r
69                 break;\r
70         case WDGIF_SLOW_MODE:\r
71                 modeWdgConfig = &(configWdgPtr->Wdg_ModeConfig->WdgSettingsSlow);\r
72                 break;\r
73         default:\r
74                 modeWdgConfig = 0;\r
75                 break;\r
76         }\r
77         if (modeWdgConfig != 0)\r
78         {\r
79                 /* Enable watchdog if config tell us to.. */\r
80                 if (modeWdgConfig->ActivationBit)\r
81                 {\r
82 #if defined(CFG_MPC560X)\r
83                   StopWatchdog(); // must be stopped in order to change TO\r
84                   SWT.TO.R = modeWdgConfig->ReloadValue;\r
85 #endif\r
86                         StartWatchdog();\r
87                 }\r
88                 else\r
89                 {\r
90                         StopWatchdog();\r
91                 }\r
92                 res = E_OK;\r
93         }\r
94 \r
95         return res;\r
96 }\r
97 \r
98 \r
99 /* This function services the internal Watchdog timer */\r
100 void Wdg_Trigger (void)\r
101 {\r
102         imask_t state;\r
103 \r
104     Irq_Save(state);\r
105 \r
106         //  According to MPC55xx manual:\r
107         //  To prevent the watchdog timer from interrupting or resetting\r
108         //  the SWTSR must be serviced by performing the following sequence:\r
109         //  1. Write 0x55 to the SWTSR.\r
110         //  2. Write 0xAA to the SWTSR.\r
111 #if defined(CFG_MPC5567)\r
112         ECSM.SWTSR.R = 0x55;\r
113         ECSM.SWTSR.R = 0xAA;\r
114 #elif defined(CFG_MPC560X)\r
115         SWT.SR.R = 0x0000A602;\r
116         SWT.SR.R = 0x0000B480;\r
117 #else\r
118         MCM.SWTSR.R = 0x55;\r
119         MCM.SWTSR.R = 0xAA;\r
120 #endif\r
121 \r
122     Irq_Restore(state);\r
123 }\r