]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/drivers/Mcu.c
Mixed bug fixing..
[arc.git] / arch / ppc / mpc55xx / drivers / Mcu.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 \r
17 #include <assert.h>\r
18 #include <string.h>\r
19 #include "Std_Types.h"\r
20 #include "Mcu.h"\r
21 #include "Det.h"\r
22 #if defined(USE_DEM)\r
23 #include "Dem.h"\r
24 #endif\r
25 #include "mpc55xx.h"\r
26 #include "Cpu.h"\r
27 #include "Ramlog.h"\r
28 #include "Os.h"\r
29 #include "isr.h"\r
30 \r
31 //#define USE_LDEBUG_PRINTF 1\r
32 #include "debug.h"\r
33 \r
34 #define SYSCLOCK_SELECT_PLL     0x2\r
35 \r
36 #if defined(CFG_MPC5567)\r
37 #define CALC_SYSTEM_CLOCK(_extal,_emfd,_eprediv,_erfd)  \\r
38             ( (_extal) * ((_emfd)+4) / (((_eprediv)+1)*(1<<(_erfd))) )\r
39 #elif defined(CFG_MPC5606S)\r
40 #define CALC_SYSTEM_CLOCK(_extal,_emfd,_eprediv,_erfd)  \\r
41                 ( (_extal)*(_emfd) / ((_eprediv+1)*(2<<(_erfd))) )\r
42 #else\r
43 #define CALC_SYSTEM_CLOCK(_extal,_emfd,_eprediv,_erfd)  \\r
44             ( (_extal) * ((_emfd)+16) / (((_eprediv)+1)*((_erfd)+1)) )\r
45 #endif\r
46 \r
47 typedef void (*vfunc_t)();\r
48 \r
49 /* Function declarations. */\r
50 static void Mcu_ConfigureFlash(void);\r
51 \r
52 typedef struct{\r
53         uint32 lossOfLockCnt;\r
54         uint32 lossOfClockCnt;\r
55 } Mcu_Stats;\r
56 \r
57 /**\r
58  * Type that holds all global data for Mcu\r
59  */\r
60 typedef struct\r
61 {\r
62     // Set if Mcu_Init() have been called\r
63     boolean initRun;\r
64     // Our config\r
65     const Mcu_ConfigType *config;\r
66     Mcu_ClockType clockSetting;\r
67     Mcu_Stats stats;\r
68 } Mcu_GlobalType;\r
69 \r
70 /* Development error macros. */\r
71 #if ( MCU_DEV_ERROR_DETECT == STD_ON )\r
72 #define VALIDATE(_exp,_api,_err ) \\r
73         if( !(_exp) ) { \\r
74           Det_ReportError(MODULE_ID_MCU,0,_api,_err); \\r
75           return; \\r
76         }\r
77 \r
78 #define VALIDATE_W_RV(_exp,_api,_err,_rv ) \\r
79         if( !(_exp) ) { \\r
80           Det_ReportError(MODULE_ID_MCU,0,_api,_err); \\r
81           return (_rv); \\r
82         }\r
83 #else\r
84 #define VALIDATE(_exp,_api,_err )\r
85 #define VALIDATE_W_RV(_exp,_api,_err,_rv )\r
86 #endif\r
87 \r
88 // Global config\r
89 Mcu_GlobalType Mcu_Global =\r
90 {\r
91         .initRun = 0,\r
92         .config = &McuConfigData[0],\r
93 };\r
94 \r
95 //-------------------------------------------------------------------\r
96 \r
97 void Mcu_LossOfLock( void  ){\r
98 #if defined(USE_DEM)\r
99         Dem_ReportErrorStatus(MCU_E_CLOCK_FAILURE, DEM_EVENT_STATUS_FAILED);\r
100 #endif\r
101 \r
102   /*\r
103    * NOTE!!!\r
104    * This interrupt may be triggered more than expected.\r
105    * If you are going to use this interrupt, see [Freescale Device Errata MPC5510ACE, Rev. 10 APR 2009, errata ID: 6764].\r
106    *\r
107    */\r
108 #if defined(CFG_MPC5606S)\r
109         /*not support*/\r
110 #else\r
111         Mcu_Global.stats.lossOfLockCnt++;\r
112         // Clear interrupt\r
113         FMPLL.SYNSR.B.LOLF = 1;\r
114 #endif\r
115 }\r
116 \r
117 //-------------------------------------------------------------------\r
118 \r
119 void Mcu_LossOfClock( void  ){\r
120         /* Should report MCU_E_CLOCK_FAILURE with DEM here */\r
121 #if defined(CFG_MPC5606S)\r
122         /*not support*/\r
123 #else\r
124         Mcu_Global.stats.lossOfClockCnt++;\r
125         // Clear interrupt\r
126         FMPLL.SYNSR.B.LOCF = 1;\r
127 #endif\r
128 }\r
129 \r
130 #define SPR_PIR 286\r
131 #define SPR_PVR 287\r
132 \r
133 #define CORE_PVR_E200Z1         0x81440000UL\r
134 #define CORE_PVR_E200Z0         0x81710000UL\r
135 #define CORE_PVR_E200Z3         0x81120000UL\r
136 #define CORE_PVR_E200Z6         0x81170000UL\r
137 #define CORE_PVR_E200Z65        0x81150000UL    /* Is actually a 5668 */\r
138 #define CORE_PVR_E200Z0H        0x817F0000UL\r
139 \r
140 typedef struct{\r
141         char *name;\r
142         uint32 pvr;\r
143 } core_info_t;\r
144 \r
145 typedef struct{\r
146         char *name;\r
147         uint32 pvr;\r
148 } cpu_info_t;\r
149 \r
150 const cpu_info_t cpu_info_list[] = {\r
151 #if defined(CFG_MPC5516)\r
152     {\r
153         .name = "MPC5516",\r
154         .pvr = CORE_PVR_E200Z1,\r
155     },\r
156     {\r
157         .name = "MPC5516",\r
158         .pvr = CORE_PVR_E200Z0,\r
159     },\r
160 #elif defined(CFG_MPC5567)\r
161     {\r
162         .name = "MPC5567",\r
163         .pvr = CORE_PVR_E200Z6,\r
164     }\r
165 #elif defined(CFG_MPC5633)\r
166     {\r
167         .name = "MPC563X",\r
168         .pvr = CORE_PVR_E200Z3,\r
169     },\r
170 #elif defined(CFG_MPC5606S)\r
171     {\r
172         .name = "MPC5606S",\r
173         .pvr = CORE_PVR_E200Z0H,\r
174     },\r
175 #elif defined(CFG_MPC5668)\r
176         {\r
177                 .name = "MPC5668",\r
178                 .pvr = CORE_PVR_E200Z65,\r
179         },\r
180         {\r
181                 .name = "MPC5668",\r
182                 .pvr = CORE_PVR_E200Z0,\r
183         },\r
184 #endif\r
185 };\r
186 \r
187 const core_info_t core_info_list[] = {\r
188 #if defined(CFG_MPC5516)\r
189         {\r
190                 .name = "CORE_E200Z1",\r
191                 .pvr = CORE_PVR_E200Z1,\r
192     },\r
193     {\r
194         .name = "CORE_E200Z1",\r
195         .pvr = CORE_PVR_E200Z1,\r
196     },\r
197 #elif defined(CFG_MPC5567)\r
198     {\r
199         .name = "CORE_E200Z6",\r
200         .pvr = CORE_PVR_E200Z6,\r
201     }\r
202 #elif defined(CFG_MPC5633)\r
203     {\r
204                 .name = "CORE_E200Z3",\r
205                 .pvr = CORE_PVR_E200Z3,\r
206     },\r
207 #elif defined(CFG_MPC5606S)\r
208     {\r
209         .name = "MPC5606S",\r
210         .pvr = CORE_PVR_E200Z0H,\r
211     },\r
212 #elif defined(CFG_MPC5668)\r
213     {\r
214         .name = "CORE_E200Z65",\r
215         .pvr = CORE_PVR_E200Z65,\r
216     },\r
217     {\r
218         .name = "CORE_E200Z0",\r
219         .pvr = CORE_PVR_E200Z1,\r
220     },\r
221 #endif\r
222 };\r
223 \r
224 // TODO: move\r
225 #if !defined(ARRAY_SIZE)\r
226 #define ARRAY_SIZE(_x)  (sizeof(_x)/sizeof((_x)[0]))\r
227 #endif\r
228 \r
229 static const cpu_info_t *Mcu_IdentifyCpu(uint32 pvr)\r
230 {\r
231     int i;\r
232 \r
233     for (i = 0; i < ARRAY_SIZE(cpu_info_list); i++) {\r
234         if (cpu_info_list[i].pvr == pvr) {\r
235                 return &cpu_info_list[i];\r
236         }\r
237     }\r
238 \r
239     return NULL;\r
240 }\r
241 \r
242 static const core_info_t *Mcu_IdentifyCore(uint32 pvr)\r
243 {\r
244         int i;\r
245 \r
246         for (i = 0; i < ARRAY_SIZE(core_info_list); i++) {\r
247                 if (core_info_list[i].pvr == pvr) {\r
248                         return &core_info_list[i];\r
249                 }\r
250     }\r
251 \r
252   return NULL;\r
253 }\r
254 \r
255 static uint32 Mcu_CheckCpu( void ) {\r
256         uint32 pvr;\r
257         // uint32 pir;\r
258         const cpu_info_t *cpuType;\r
259         const core_info_t *coreType;\r
260 \r
261     // We have to registers to read here, PIR and PVR\r
262     // pir = get_spr(SPR_PIR);\r
263     pvr = get_spr(SPR_PVR);\r
264 \r
265     cpuType = Mcu_IdentifyCpu(pvr);\r
266     coreType = Mcu_IdentifyCore(pvr);\r
267 \r
268     if( (cpuType == NULL) || (coreType == NULL) ) {\r
269         // Just hang\r
270         while(1) ;\r
271     }\r
272 \r
273     //DEBUG(DEBUG_HIGH,"/drivers/mcu: Cpu:  %s( 0x%08x )\n",cpuType->name,pvr);\r
274     //DEBUG(DEBUG_HIGH,"/drivers/mcu: Core: %s( 0x%08x )\n",coreType->name,pvr);\r
275 \r
276     return 0;\r
277 }\r
278 \r
279 //-------------------------------------------------------------------\r
280 \r
281 void Mcu_Init(const Mcu_ConfigType *configPtr)\r
282 {\r
283         VALIDATE( ( NULL != configPtr ), MCU_INIT_SERVICE_ID, MCU_E_PARAM_CONFIG );\r
284 \r
285 #if defined(CFG_MPC5606S)\r
286         /* Disable watchdog. Watchdog is enabled default after reset.*/\r
287         SWT.SR.R = 0x0000c520;     /* Write keys to clear soft lock bit */\r
288         SWT.SR.R = 0x0000d928;\r
289         SWT.CR.R = 0x8000010A;     /* Disable watchdog */\r
290 #if defined(USE_WDG)\r
291         SWT.TO.R = 0xfa00;              /* set the timout to 500ms */\r
292         SWT.CR.R = 0x8000011B;      /* enable watchdog */\r
293 #endif\r
294 #endif\r
295 \r
296     if( !SIMULATOR() ) {\r
297         Mcu_CheckCpu();\r
298     }\r
299 \r
300     memset(&Mcu_Global.stats,0,sizeof(Mcu_Global.stats));\r
301 \r
302     // Setup memories\r
303     Mcu_ConfigureFlash();\r
304 \r
305     Mcu_Global.config = configPtr;\r
306 \r
307 #if defined(CFG_MPC5606S)\r
308     /* Enable DRUN, RUN0, SAFE, RESET modes */\r
309     ME.MER.R = 0x0000001D;\r
310 #endif\r
311 \r
312     Mcu_Global.initRun = 1;\r
313 \r
314     if( Mcu_Global.config->McuClockSrcFailureNotification == TRUE  ) {\r
315 #if defined(CFG_MPC5606S)\r
316         /*not support*/\r
317 #else\r
318         ISR_INSTALL_ISR1("LossOfLock", Mcu_LossOfLock, PLL_SYNSR_LOLF, 10 , 0 );\r
319 #if defined(CFG_MPC5516)  || defined(CFG_MPC5668)\r
320         FMPLL.ESYNCR2.B.LOLIRQ = 1;\r
321 #elif defined(CFG_MPC5554) || defined(CFG_MPC5567)\r
322         FMPLL.SYNCR.B.LOLIRQ = 1;\r
323 #endif\r
324         ISR_INSTALL_ISR1("LossOfClock", Mcu_LossOfClock, PLL_SYNSR_LOLF, 10 , 0 );\r
325 #if defined(CFG_MPC5516) || defined(CFG_MPC5668)\r
326         FMPLL.ESYNCR2.B.LOCIRQ = 1;\r
327 #elif defined(CFG_MPC5554) || defined(CFG_MPC5567)\r
328         FMPLL.SYNCR.B.LOCIRQ = 1;\r
329 #endif\r
330 #endif\r
331     }\r
332 }\r
333 \r
334 //-------------------------------------------------------------------\r
335 \r
336 void Mcu_DeInit()\r
337 {\r
338         Mcu_Global.initRun = FALSE; // Very simple Deinit. Should we do more?\r
339 }\r
340 \r
341 //-------------------------------------------------------------------\r
342 \r
343 Std_ReturnType Mcu_InitRamSection(const Mcu_RamSectionType RamSection)\r
344 {\r
345         VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_INITRAMSECTION_SERVICE_ID, MCU_E_UNINIT, E_NOT_OK );\r
346         VALIDATE_W_RV( ( RamSection <= Mcu_Global.config->McuRamSectors ), MCU_INITRAMSECTION_SERVICE_ID, MCU_E_PARAM_RAMSECTION, E_NOT_OK );\r
347 \r
348     /* NOT SUPPORTED, reason: no support for external RAM */\r
349 \r
350     return E_OK;\r
351 }\r
352 \r
353 //-------------------------------------------------------------------\r
354 \r
355 Std_ReturnType Mcu_InitClock(const Mcu_ClockType ClockSetting)\r
356 {\r
357     Mcu_ClockSettingConfigType *clockSettingsPtr;\r
358     VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_INITCLOCK_SERVICE_ID, MCU_E_UNINIT, E_NOT_OK );\r
359     VALIDATE_W_RV( ( ClockSetting < Mcu_Global.config->McuClockSettings ), MCU_INITCLOCK_SERVICE_ID, MCU_E_PARAM_CLOCK, E_NOT_OK );\r
360 \r
361     Mcu_Global.clockSetting = ClockSetting;\r
362     clockSettingsPtr = &Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting];\r
363 \r
364     // TODO: find out if the 5554 really works like the 5516 here\r
365     // All three (16, 54, 67) used to run the same code here though, so i'm sticking it with 5516\r
366 #if defined(CFG_MPC5516) || defined(CFG_MPC5554) || defined(CFG_MPC5668)\r
367     /* 5516clock info:\r
368      * Fsys - System frequency ( CPU + all periperals? )\r
369      *\r
370      *  Fsys = EXTAL_FREQ *(  (emfd+16) / ( (eprediv+1) * ( erfd+1 )) ) )\r
371      */\r
372     // Check ranges...\r
373     assert((clockSettingsPtr->Pll2>=32) && (clockSettingsPtr->Pll2<=132));\r
374     assert( (clockSettingsPtr->Pll1 != 6) &&\r
375             (clockSettingsPtr->Pll1 != 8) &&\r
376             (clockSettingsPtr->Pll1 < 10) );\r
377     assert( clockSettingsPtr->Pll3 & 1); // Must be odd\r
378 #elif defined(CFG_MPC5567)\r
379     /* 5567 clock info:\r
380      *  Fsys = EXTAL_FREQ *(  (emfd+4) / ( (eprediv+1) * ( 2^erfd )) ) )\r
381      */\r
382     // Check ranges...\r
383     assert(clockSettingsPtr->Pll2 < 16);\r
384     assert(clockSettingsPtr->Pll1 <= 4);\r
385     assert(clockSettingsPtr->Pll3 < 8);\r
386 #endif\r
387 \r
388 #if defined(USE_LDEBUG_PRINTF)\r
389     {\r
390         uint32  extal = Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting].McuClockReferencePointFrequency;\r
391         uint32  f_sys;\r
392 \r
393         f_sys = CALC_SYSTEM_CLOCK( extal,\r
394                 clockSettingsPtr->Pll2,\r
395                 clockSettingsPtr->Pll1,\r
396                 clockSettingsPtr->Pll3 );\r
397 \r
398         //DEBUG(DEBUG_HIGH,"/drivers/mcu: F_sys will be:%08d Hz\n",f_sys);\r
399     }\r
400 #endif\r
401 \r
402 #if defined(CFG_MPC5516) || defined(CFG_MPC5668)\r
403 \r
404     // set post divider to next valid value to ensure that an overshoot during lock phase\r
405     // won't result in a too high freq\r
406     FMPLL.ESYNCR2.B.ERFD = (clockSettingsPtr->Pll3 + 1) | 1;\r
407 \r
408     // External crystal PLL mode.\r
409     FMPLL.ESYNCR1.B.CLKCFG = 7; //TODO: Hur ställa detta för 5567?\r
410 \r
411     // Write pll parameters.\r
412     FMPLL.ESYNCR1.B.EPREDIV = clockSettingsPtr->Pll1;\r
413     FMPLL.ESYNCR1.B.EMFD    = clockSettingsPtr->Pll2;\r
414 \r
415 #if !defined(CFG_SIMULATOR)\r
416     while(FMPLL.SYNSR.B.LOCK != 1) {};\r
417 #endif\r
418 \r
419     FMPLL.ESYNCR2.B.ERFD    = clockSettingsPtr->Pll3;\r
420     // Connect SYSCLK to FMPLL\r
421     SIU.SYSCLK.B.SYSCLKSEL = SYSCLOCK_SELECT_PLL;\r
422 \r
423 #elif defined(CFG_MPC5606S)\r
424     // Write pll parameters.\r
425     CGM.FMPLL[0].CR.B.IDF = clockSettingsPtr->Pll1;\r
426     CGM.FMPLL[0].CR.B.NDIV = clockSettingsPtr->Pll2;\r
427     CGM.FMPLL[0].CR.B.ODF = clockSettingsPtr->Pll3;\r
428 \r
429     /* RUN0 cfg: 16MHzIRCON,OSC0ON,PLL0ON,syclk=PLL0 */\r
430     ME.RUN[0].R = 0x001F0074;\r
431     /* Peri. Cfg. 1 settings: only run in RUN0 mode */\r
432     ME.RUNPC[1].R = 0x00000010;\r
433     /* MPC56xxB/S: select ME.RUNPC[1] */\r
434     ME.PCTL[68].R = 0x01; //SIUL control\r
435     ME.PCTL[91].R = 0x01; //RTC/API control\r
436     ME.PCTL[92].R = 0x01; //PIT_RTI control\r
437     ME.PCTL[72].R = 0x01; //eMIOS0 control\r
438     ME.PCTL[73].R = 0x01; //eMIOS1 control\r
439     ME.PCTL[16].R = 0x01; //FlexCAN0 control\r
440     ME.PCTL[17].R = 0x01; //FlexCAN1 control\r
441     ME.PCTL[4].R = 0x01;  /* MPC56xxB/P/S DSPI0  */\r
442     ME.PCTL[5].R = 0x01;  /* MPC56xxB/P/S DSPI1:  */\r
443     ME.PCTL[32].R = 0x01; //ADC0 control\r
444     ME.PCTL[23].R = 0x01; //DMAMUX control\r
445     ME.PCTL[48].R = 0x01; /* MPC56xxB/P/S LINFlex  */\r
446     ME.PCTL[49].R = 0x01; /* MPC56xxB/P/S LINFlex  */\r
447     /* Mode Transition to enter RUN0 mode: */\r
448     /* Enter RUN0 Mode & Key */\r
449     ME.MCTL.R = 0x40005AF0;\r
450     /* Enter RUN0 Mode & Inverted Key */\r
451     ME.MCTL.R = 0x4000A50F;\r
452 \r
453     /* Wait for mode transition to complete */\r
454     while (ME.GS.B.S_MTRANS) {}\r
455     /* Verify RUN0 is the current mode */\r
456     while(ME.GS.B.S_CURRENTMODE != 4) {}\r
457 \r
458  #elif defined(CFG_MPC5554) || defined(CFG_MPC5567)\r
459     // Partially following the steps in MPC5567 RM..\r
460     FMPLL.SYNCR.B.DEPTH = 0;\r
461     FMPLL.SYNCR.B.LOLRE = 0;\r
462     FMPLL.SYNCR.B.LOLIRQ = 0;\r
463 \r
464     FMPLL.SYNCR.B.PREDIV        = clockSettingsPtr->Pll1;\r
465     FMPLL.SYNCR.B.MFD           = clockSettingsPtr->Pll2;\r
466     FMPLL.SYNCR.B.RFD           = clockSettingsPtr->Pll3;\r
467 \r
468         // Wait for PLL to sync.\r
469     while (Mcu_GetPllStatus() != MCU_PLL_LOCKED) ;\r
470 \r
471     FMPLL.SYNCR.B.LOLIRQ        = 1;\r
472 #endif\r
473 \r
474     return E_OK;\r
475 }\r
476 \r
477 //-------------------------------------------------------------------\r
478 \r
479 void Mcu_DistributePllClock(void)\r
480 {\r
481     VALIDATE( ( 1 == Mcu_Global.initRun ), MCU_DISTRIBUTEPLLCLOCK_SERVICE_ID, MCU_E_UNINIT );\r
482 #if defined(CFG_MPC5606S)\r
483     VALIDATE( ( CGM.FMPLL[0].CR.B.S_LOCK == 1 ), MCU_DISTRIBUTEPLLCLOCK_SERVICE_ID, MCU_E_PLL_NOT_LOCKED );\r
484 #else\r
485     VALIDATE( ( FMPLL.SYNSR.B.LOCK == 1 ), MCU_DISTRIBUTEPLLCLOCK_SERVICE_ID, MCU_E_PLL_NOT_LOCKED );\r
486 #endif\r
487     /* NOT IMPLEMENTED due to pointless function on this hardware */\r
488 \r
489 }\r
490 \r
491 //-------------------------------------------------------------------\r
492 \r
493 Mcu_PllStatusType Mcu_GetPllStatus(void)\r
494 {\r
495     VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETPLLSTATUS_SERVICE_ID, MCU_E_UNINIT, MCU_PLL_STATUS_UNDEFINED );\r
496     Mcu_PllStatusType rv;\r
497 \r
498     if( !SIMULATOR() )\r
499     {\r
500 #if defined(CFG_MPC5606S)\r
501         if ( !CGM.FMPLL[0].CR.B.S_LOCK )\r
502         {\r
503                 rv = MCU_PLL_UNLOCKED;\r
504         } else\r
505         {\r
506                 rv = MCU_PLL_LOCKED;\r
507         }\r
508 #else\r
509         if ( !FMPLL.SYNSR.B.LOCK )\r
510         {\r
511                 rv = MCU_PLL_UNLOCKED;\r
512         } else\r
513         {\r
514                 rv = MCU_PLL_LOCKED;\r
515         }\r
516 #endif\r
517     }\r
518     else\r
519     {\r
520         /* We are running on instruction set simulator. PLL is then always in sync... */\r
521         rv = MCU_PLL_LOCKED;\r
522     }\r
523 \r
524   return rv;\r
525 }\r
526 \r
527 //-------------------------------------------------------------------\r
528 \r
529 Mcu_ResetType Mcu_GetResetReason(void)\r
530 {\r
531         Mcu_ResetType rv;\r
532 \r
533         VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETRESETREASON_SERVICE_ID, MCU_E_UNINIT, MCU_RESET_UNDEFINED );\r
534 \r
535 #if defined(CFG_MPC5606S)\r
536         if( RGM.FES.B.F_SOFT ) {\r
537                 rv = MCU_SW_RESET;\r
538         } else if( RGM.DES.B.F_SWT ) {\r
539                 rv = MCU_WATCHDOG_RESET;\r
540         } else if( RGM.DES.B.F_POR ) {\r
541                 rv = MCU_POWER_ON_RESET;\r
542         } else {\r
543                 rv = MCU_RESET_UNDEFINED;\r
544         }\r
545 #else\r
546         if( SIU.RSR.B.SSRS ) {\r
547                 rv = MCU_SW_RESET;\r
548         } else if( SIU.RSR.B.WDRS ) {\r
549                 rv = MCU_WATCHDOG_RESET;\r
550         } else if( SIU.RSR.B.PORS || SIU.RSR.B.ERS ) {\r
551                 rv = MCU_POWER_ON_RESET;\r
552         } else {\r
553                 rv = MCU_RESET_UNDEFINED;\r
554         }\r
555 #endif\r
556 \r
557         return rv;\r
558 }\r
559 \r
560 //-------------------------------------------------------------------\r
561 \r
562 Mcu_RawResetType Mcu_GetResetRawValue(void)\r
563 {\r
564         VALIDATE_W_RV( ( 1 == Mcu_Global.initRun ), MCU_GETRESETREASON_SERVICE_ID, MCU_E_UNINIT, MCU_GETRESETRAWVALUE_UNINIT_RV );\r
565 \r
566         if( !Mcu_Global.initRun ) {\r
567                 return MCU_GETRESETRAWVALUE_UNINIT_RV;\r
568         }\r
569 \r
570 #if defined(CFG_MPC5606S)\r
571         if( RGM.DES.R )\r
572                 return RGM.DES.R;\r
573         else\r
574                 return RGM.FES.R;\r
575 #else\r
576         return SIU.RSR.R;\r
577 #endif\r
578 \r
579 }\r
580 \r
581 //-------------------------------------------------------------------\r
582 \r
583 #if ( MCU_PERFORM_RESET_API == STD_ON )\r
584 void Mcu_PerformReset(void)\r
585 {\r
586         VALIDATE( ( 1 == Mcu_Global.initRun ), MCU_PERFORMRESET_SERVICE_ID, MCU_E_UNINIT );\r
587 \r
588         // Reset\r
589 #if defined(CFG_MPC5606S)\r
590     ME.MCTL.R = 0x00005AF0;\r
591     ME.MCTL.R = 0x0000A50F;\r
592 \r
593     while (ME.GS.B.S_MTRANS) {}\r
594     while(ME.GS.B.S_CURRENTMODE != 0) {}\r
595 #else\r
596         SIU.SRCR.B.SSR = 1;\r
597 #endif\r
598 \r
599 }\r
600 #endif\r
601 \r
602 //-------------------------------------------------------------------\r
603 \r
604 void Mcu_SetMode(const Mcu_ModeType McuMode)\r
605 {\r
606         VALIDATE( ( 1 == Mcu_Global.initRun ), MCU_SETMODE_SERVICE_ID, MCU_E_UNINIT );\r
607         // VALIDATE( ( McuMode <= Mcu_Global.config->McuNumberOfMcuModes ), MCU_SETMODE_SERVICE_ID, MCU_E_PARAM_MODE );\r
608         (void) McuMode;\r
609 \r
610         /* NOT SUPPORTED */\r
611 }\r
612 \r
613 //-------------------------------------------------------------------\r
614 \r
615 /**\r
616  * Get the system clock in Hz. It calculates the clock from the\r
617  * different register settings in HW.\r
618  */\r
619 uint32_t McuE_GetSystemClock(void)\r
620 {\r
621         /*\r
622          * System clock calculation\r
623          *\r
624          * 5516 -  f_sys = extal * (emfd+16) / ( (eprediv+1) * ( erfd+1 ));\r
625          * 5567 -  f_sys = extal * (emfd+4) / ( (eprediv+1) * ( 2^erfd ));\r
626          * 563x -  We run in legacy mode = 5567\r
627          * 5606s - f_sys = extal * emfd / ((eprediv+1)*(2<<(erfd)));\r
628          */\r
629 #if defined(CFG_MPC5516) || defined(CFG_MPC5668)\r
630         uint32_t eprediv = FMPLL.ESYNCR1.B.EPREDIV;\r
631         uint32_t emfd = FMPLL.ESYNCR1.B.EMFD;\r
632         uint32_t erfd = FMPLL.ESYNCR2.B.ERFD;\r
633 #elif defined(CFG_MPC5554) || defined(CFG_MPC5567) || defined(CFG_MPC5633)\r
634         uint32_t eprediv = FMPLL.SYNCR.B.PREDIV;\r
635         uint32_t emfd = FMPLL.SYNCR.B.MFD;\r
636         uint32_t erfd = FMPLL.SYNCR.B.RFD;\r
637 #elif defined(CFG_MPC5606S)\r
638     uint32_t eprediv = CGM.FMPLL[0].CR.B.IDF;\r
639     uint32_t emfd = CGM.FMPLL[0].CR.B.NDIV;\r
640     uint32_t erfd = CGM.FMPLL[0].CR.B.ODF;\r
641 #endif\r
642 \r
643     uint32_t f_sys;\r
644     uint32  extal = Mcu_Global.config->McuClockSettingConfig[Mcu_Global.clockSetting].McuClockReferencePointFrequency;\r
645 \r
646     f_sys = CALC_SYSTEM_CLOCK(extal,emfd,eprediv,erfd);\r
647 \r
648     return f_sys;\r
649 }\r
650 \r
651 #if defined(CFG_MPC5668)\r
652 uint32_t McuE_GetPeripheralClock(McuE_PeriperalClock_t type) {\r
653         uint32_t sysClock = McuE_GetSystemClock();\r
654         vuint32_t prescaler;\r
655 \r
656         switch (type)\r
657         {\r
658                 case PERIPHERAL_CLOCK_FLEXCAN_A:\r
659                 case PERIPHERAL_CLOCK_FLEXCAN_B:\r
660                 case PERIPHERAL_CLOCK_FLEXCAN_C:\r
661                 case PERIPHERAL_CLOCK_FLEXCAN_D:\r
662                 case PERIPHERAL_CLOCK_FLEXCAN_E:\r
663                 case PERIPHERAL_CLOCK_FLEXCAN_F:\r
664                 case PERIPHERAL_CLOCK_DSPI_A:\r
665                 case PERIPHERAL_CLOCK_DSPI_B:\r
666                 case PERIPHERAL_CLOCK_DSPI_C:\r
667                 case PERIPHERAL_CLOCK_DSPI_D:\r
668                         prescaler = SIU.SYSCLK.B.LPCLKDIV1;\r
669                         break;\r
670                 case PERIPHERAL_CLOCK_ESCI_A:\r
671                 case PERIPHERAL_CLOCK_ESCI_B:\r
672                 case PERIPHERAL_CLOCK_ESCI_C:\r
673                 case PERIPHERAL_CLOCK_ESCI_D:\r
674                 case PERIPHERAL_CLOCK_ESCI_E:\r
675                 case PERIPHERAL_CLOCK_ESCI_F:\r
676                 case PERIPHERAL_CLOCK_IIC_A:\r
677                 case PERIPHERAL_CLOCK_IIC_B:\r
678                         prescaler = SIU.SYSCLK.B.LPCLKDIV0;\r
679                         break;\r
680                 case PERIPHERAL_CLOCK_ADC_A:\r
681                         prescaler = SIU.SYSCLK.B.LPCLKDIV2;\r
682                         break;\r
683                 case PERIPHERAL_CLOCK_EMIOS:\r
684                         prescaler = SIU.SYSCLK.B.LPCLKDIV3;\r
685                         break;\r
686                 default:\r
687                         assert(0);\r
688                         break;\r
689         }\r
690 \r
691         return sysClock/(1<<prescaler);\r
692 \r
693 }\r
694 \r
695 #else\r
696 \r
697 /**\r
698  * Get the peripheral clock in Hz for a specific device\r
699  */\r
700 uint32_t McuE_GetPeripheralClock(McuE_PeriperalClock_t type)\r
701 {\r
702 #if defined(CFG_MPC5567)\r
703         // No peripheral dividers on 5567.\r
704         return McuE_GetSystemClock();\r
705 #else\r
706         uint32_t sysClock = McuE_GetSystemClock();\r
707         vuint32_t prescaler;\r
708 \r
709   // See table 3.1, section 3.4.5 Peripheral Clock dividers\r
710         switch (type)\r
711         {\r
712                 case PERIPHERAL_CLOCK_FLEXCAN_A:\r
713                 case PERIPHERAL_CLOCK_DSPI_A:\r
714 #if defined(CFG_MPC5516)\r
715                         prescaler = SIU.SYSCLK.B.LPCLKDIV0;\r
716                         break;\r
717 #elif defined(CFG_MPC5606S)\r
718                         prescaler = CGM.SC_DC[1].B.DIV;\r
719                         break;\r
720 #endif\r
721 \r
722                 case PERIPHERAL_CLOCK_PIT:\r
723                 case PERIPHERAL_CLOCK_ESCI_A:\r
724                 case PERIPHERAL_CLOCK_IIC_A:\r
725 #if defined(CFG_MPC5516)\r
726                         prescaler = SIU.SYSCLK.B.LPCLKDIV1;\r
727                         break;\r
728 #endif\r
729 \r
730                 case PERIPHERAL_CLOCK_FLEXCAN_B:\r
731                 case PERIPHERAL_CLOCK_FLEXCAN_C:\r
732                 case PERIPHERAL_CLOCK_FLEXCAN_D:\r
733                 case PERIPHERAL_CLOCK_FLEXCAN_E:\r
734                 case PERIPHERAL_CLOCK_FLEXCAN_F:\r
735 #if defined(CFG_MPC5516)\r
736                         prescaler = SIU.SYSCLK.B.LPCLKDIV2;\r
737                         break;\r
738 #elif defined(CFG_MPC5606S)\r
739                         prescaler = CGM.SC_DC[1].B.DIV;\r
740                         break;\r
741 #endif\r
742 \r
743                 case PERIPHERAL_CLOCK_DSPI_B:\r
744                 case PERIPHERAL_CLOCK_DSPI_C:\r
745                 case PERIPHERAL_CLOCK_DSPI_D:\r
746 #if defined(CFG_MPC5516)\r
747                 prescaler = SIU.SYSCLK.B.LPCLKDIV3;\r
748                         break;\r
749 #endif\r
750 \r
751                 case PERIPHERAL_CLOCK_ESCI_B:\r
752                 case PERIPHERAL_CLOCK_ESCI_C:\r
753                 case PERIPHERAL_CLOCK_ESCI_D:\r
754                 case PERIPHERAL_CLOCK_ESCI_E:\r
755                 case PERIPHERAL_CLOCK_ESCI_F:\r
756                 case PERIPHERAL_CLOCK_ESCI_G:\r
757                 case PERIPHERAL_CLOCK_ESCI_H:\r
758 #if defined(CFG_MPC5516)\r
759                         prescaler = SIU.SYSCLK.B.LPCLKDIV4;\r
760                         break;\r
761 #endif\r
762 \r
763 #if defined(CFG_MPC5606S)\r
764                 case PERIPHERAL_CLOCK_LIN_A:\r
765                 case PERIPHERAL_CLOCK_LIN_B:\r
766                         prescaler = CGM.SC_DC[0].B.DIV;\r
767                         break;\r
768                 case PERIPHERAL_CLOCK_EMIOS_0:\r
769                         prescaler = CGM.SC_DC[2].B.DIV;\r
770                         break;\r
771                 case PERIPHERAL_CLOCK_EMIOS_1:\r
772                         prescaler = CGM.SC_DC[2].B.DIV;\r
773                         break;\r
774 #else\r
775                 case PERIPHERAL_CLOCK_EMIOS:\r
776 #if defined(CFG_MPC5516)\r
777                         prescaler = SIU.SYSCLK.B.LPCLKDIV5;\r
778                         break;\r
779 #endif\r
780 #endif\r
781 \r
782                 case PERIPHERAL_CLOCK_MLB:\r
783 #if defined(CFG_MPC5516)\r
784                         prescaler = SIU.SYSCLK.B.LPCLKDIV6;\r
785                         break;\r
786 #endif\r
787 \r
788                 default:\r
789                         assert(0);\r
790                         break;\r
791         }\r
792 \r
793         return sysClock/(1<<prescaler);\r
794 #endif\r
795 }\r
796 #endif\r
797 \r
798 /**\r
799  * Function to setup the internal flash for optimal performance\r
800  */\r
801 \r
802 static void Mcu_ConfigureFlash(void)\r
803 {\r
804         /* These flash settings increases the CPU performance of 7 times compared\r
805            to reset default settings!! */\r
806 \r
807 #if defined(CFG_MPC5516)\r
808         /* Disable pipelined reads when flash options are changed. */\r
809         FLASH.MCR.B.PRD = 1;\r
810 \r
811         /* Enable master prefetch for e200z1 and eDMA. */\r
812         FLASH.PFCRP0.B.M0PFE = 1;\r
813         FLASH.PFCRP0.B.M2PFE = 1;\r
814 \r
815         /* Address pipelining control. Must be set to the same value as RWSC. */\r
816         FLASH.PFCRP0.B.APC = 2;\r
817         FLASH.PFCRP0.B.RWSC = 2;\r
818 \r
819         /* Write wait states. */\r
820         FLASH.PFCRP0.B.WWSC = 1;\r
821 \r
822         /* Enable data prefetch. */\r
823         FLASH.PFCRP0.B.DPFEN = 1;\r
824 \r
825         /* Enable instruction prefetch. */\r
826         FLASH.PFCRP0.B.IPFEN = 1;\r
827 \r
828         /* Prefetch algorithm. */\r
829         /* TODO: Ask Freescale about this option. */\r
830         FLASH.PFCRP0.B.PFLIM = 2;\r
831 \r
832         /* Enable line read buffers. */\r
833         FLASH.PFCRP0.B.BFEN = 1;\r
834 \r
835         /* Enable pipelined reads again. */\r
836         FLASH.MCR.B.PRD = 0;\r
837 #elif defined(CFG_MPC5668)\r
838         /* Check values from cookbook and MPC5668x Microcontroller Data Sheet */\r
839 \r
840         /* Should probably trim this values */\r
841         const typeof(FLASH.PFCRP0.B) val = {.M0PFE = 1, .M2PFE=1, .APC=3,\r
842                                                                  .RWSC=3, .WWSC =1, .DPFEN =1, .IPFEN = 1, .PFLIM =2,\r
843                                                                  .BFEN  = 1 };\r
844         FLASH.PFCRP0.B = val;\r
845 \r
846         /* Enable pipelined reads again. */\r
847 #elif defined(CFG_MPC5554) || defined(CFG_MPC5567)\r
848         //TODO: Lägg till flash för mpc5554 &67\r
849 #endif\r
850 }\r
851 \r