]> rtime.felk.cvut.cz Git - arc.git/blob - arch/ppc/mpc55xx/kernel/arch.c
Merge with mahi-application
[arc.git] / arch / ppc / mpc55xx / kernel / arch.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 /* ----------------------------[includes]------------------------------------*/\r
18 \r
19 #include "internal.h"\r
20 #include "Cpu.h"\r
21 #include "sys.h"\r
22 #include "asm_ppc.h"\r
23 #include "mpc55xx.h"\r
24 #include "arch_stack.h"\r
25 #include "arch_offset.h"\r
26 \r
27 #define USE_LDEBUG_PRINTF\r
28 #include "debug.h"\r
29 \r
30 /* ----------------------------[private define]------------------------------*/\r
31 /* ----------------------------[private macro]-------------------------------*/\r
32 /* ----------------------------[private typedef]-----------------------------*/\r
33 /* ----------------------------[private function prototypes]-----------------*/\r
34 /* ----------------------------[private variables]---------------------------*/\r
35 /* ----------------------------[private functions]---------------------------*/\r
36 /* ----------------------------[public functions]----------------------------*/\r
37 \r
38 \r
39 \r
40 /**\r
41  * NOT complete yet.....\r
42  *\r
43  * @param err                   The error code.\r
44  * @param errFramePtr   Pointer to extra information about the error, if any.\r
45  * @param excFramePtr   Pointer to the exception frame, that caused the error.\r
46  */\r
47 void Os_ArchPanic( uint32_t err, void *errFramePtr , Os_ExceptionFrameType *excFramePtr) {\r
48         (void)excFramePtr;\r
49         (void)errFramePtr;\r
50         switch(err) {\r
51         case OS_ERR_BAD_CONTEXT:\r
52                 while(1) ;\r
53                 break;\r
54         case OS_ERR_SPURIOUS_INTERRUPT:\r
55                 fputs("Spurious interrupt\n",stdout);\r
56                 printf(" vector : %02lx\n", excFramePtr->vector);\r
57                 printf(" srr0   : %08lx\n", excFramePtr->srr0);\r
58                 printf(" srr1   : %08lx\n", excFramePtr->srr1);\r
59                 while(1) ;\r
60                 break;\r
61         default:\r
62                 while(1) ;\r
63                 break;\r
64         }\r
65 }\r
66 \r
67 /**\r
68  * Function make sure that we switch to supervisor mode(rfi) before\r
69  * we call a task for the first time.\r
70  */\r
71 void Os_ArchFirstCall( void )\r
72 {\r
73 #if USE_MM_USER_MODE\r
74 \r
75         // Assume that regs[0] is setup before and contains the settings\r
76         // to switch to user mode.\r
77         register uint32_t msr asm("r3") = os_sys.curr_pcb->regs[0];\r
78         register void *ea asm("r4") = (void *) os_sys.curr_pcb->entry;\r
79 \r
80         // Do the switch\r
81         asm volatile(\r
82                         "mtspr 26,%0;\r\t"      // srr0\r
83                         "mtspr 27,%1;\r\t"      // srr1\r
84                         "rfi;\r\t"\r
85                         :\r
86                         : "r" (ea), "r" (msr)   );\r
87 #else\r
88 // TODO: This really depends on if scheduling policy\r
89         Irq_Enable();\r
90         Os_Sys.currTaskPtr->constPtr->entry();\r
91         //os_sys.curr_pcb->entry();\r
92 #endif\r
93 }\r
94 \r
95 \r
96 /* TODO: This actually gives the stack ptr here...not the callers stack ptr\r
97  * Should probably be a macro instead..... in some arch part..\r
98  */\r
99 void *Os_ArchGetStackPtr( void ) {\r
100         void *stackp;\r
101         // Get stack ptr(r1) from current context\r
102         asm volatile(" mr %0,1":"=r" (stackp));\r
103 \r
104         return stackp;\r
105 }\r
106 \r
107 unsigned int Os_ArchGetScSize( void ) {\r
108         return FUNC_FRM_SIZE;\r
109 }\r
110 \r
111 \r
112 /**\r
113  * Setup a context for a task.\r
114  *\r
115  * @param pcb Pointer to the pcb to setup\r
116  */\r
117 void Os_ArchSetupContext( OsTaskVarType *pcbPtr ) {\r
118         Os_FuncFrameType *cPtr = (Os_FuncFrameType *)pcbPtr->stack.curr;\r
119         uint32_t msr;\r
120         msr = MSR_EE;\r
121 \r
122 #if defined(CFG_SPE)\r
123         msr |= MSR_SPE;\r
124 #endif\r
125 \r
126 #if (OS_USE_APPLICATIONS == STD_ON)\r
127 #if 0\r
128         if( !pcb->application->trusted ) {\r
129                 // Non-trusted = User mode..\r
130                 msr |= MSR_PR | MSR_DS | MSR_IS;\r
131         }\r
132 #endif\r
133 #endif\r
134         pcbPtr->regs[0] = msr;\r
135 \r
136         cPtr->pattern = FUNC_PATTERN;\r
137 }\r
138 \r
139 /**\r
140  *\r
141  * @param pcbPtr\r
142  */\r
143 \r
144 void Os_ArchSetTaskEntry(OsTaskVarType *pcbPtr ) {\r
145         Os_FuncFrameType *cPtr = (Os_FuncFrameType *)pcbPtr->stack.curr;\r
146 \r
147         if( pcbPtr->constPtr->proc_type == PROC_EXTENDED ) {\r
148                 cPtr->lr = (uint32_t)Os_TaskStartExtended;\r
149         } else if( pcbPtr->constPtr->proc_type == PROC_BASIC ) {\r
150                 cPtr->lr = (uint32_t)Os_TaskStartBasic;\r
151         }\r
152 \r
153 }\r
154 \r
155 void Os_ArchInit( void ) {\r
156 #if defined(CFG_SPE)\r
157         uint32_t msr = get_msr();\r
158         msr |= MSR_SPE;\r
159         set_msr(msr);\r
160 #endif\r
161 }\r
162 \r
163 \r
164 \r