CM3: define specific architecture attributes.
[bertos.git] / bertos / cpu / attr.h
1 /**
2  * \file
3  * <!--
4  * This file is part of BeRTOS.
5  *
6  * Bertos is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19  *
20  * As a special exception, you may use this file as part of a free software
21  * library without restriction.  Specifically, if other files instantiate
22  * templates or use macros or inline functions from this file, or you compile
23  * this file and link it with other files to produce an executable, this
24  * file does not by itself cause the resulting executable to be covered by
25  * the GNU General Public License.  This exception does not however
26  * invalidate any other reasons why the executable file might be covered by
27  * the GNU General Public License.
28  *
29  * Copyright 2004, 2005, 2006, 2007, 2008 Develer S.r.l. (http://www.develer.com/)
30  * Copyright 2004 Giovanni Bajo
31  * -->
32  *
33  * \brief CPU-specific attributes.
34  *
35  * \author Giovanni Bajo <rasky@develer.com>
36  * \author Bernie Innocenti <bernie@codewiz.org>
37  * \author Stefano Fedrigo <aleph@develer.com>
38  * \author Francesco Sacchi <batt@develer.com>
39  */
40 #ifndef CPU_ATTR_H
41 #define CPU_ATTR_H
42
43 #include "detect.h"
44
45 #include "cfg/cfg_proc.h"      /* CONFIG_KERN_PREEMPT */
46 #include "cfg/cfg_attr.h"      /* CONFIG_FAST_MEM */
47
48
49 /**
50  * \name Macros for determining CPU endianness.
51  * \{
52  */
53 #define CPU_BIG_ENDIAN    0x1234
54 #define CPU_LITTLE_ENDIAN 0x3412 /* Look twice, pal. This is not a bug. */
55 /*\}*/
56
57 /** Macro to include cpu-specific versions of the headers. */
58 #define CPU_HEADER(module)          PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).h)
59
60 /** Macro to include cpu-specific versions of implementation files. */
61 #define CPU_CSOURCE(module)         PP_STRINGIZE(drv/PP_CAT3(module, _, CPU_ID).c)
62
63
64 #if CPU_I196
65
66         #define NOP                     nop_instruction()
67
68         #define CPU_REG_BITS            16
69         #define CPU_REGS_CNT            16
70         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
71         #define CPU_HARVARD             0
72
73         /// Valid pointers should be >= than this value (used for debug)
74         #define CPU_RAM_START           0x100
75
76 #elif CPU_X86
77
78         #define CPU_REGS_CNT            7
79         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
80         #define CPU_HARVARD             0
81
82         #if CPU_X86_64
83                 #define CPU_REG_BITS    64
84
85                 #ifdef __WIN64__
86                         /* WIN64 is an IL32-P64 weirdo. */
87                         #define SIZEOF_LONG  4
88                 #endif
89         #else
90                 #define CPU_REG_BITS    32
91         #endif
92
93         /// Valid pointers should be >= than this value (used for debug)
94         #define CPU_RAM_START      0x1000
95
96         #ifdef __GNUC__
97                 #define NOP         asm volatile ("nop")
98                 /* This is a good thing to insert into busy-wait loops. */
99                 #define PAUSE       asm volatile ("rep; nop" ::: "memory")
100                 #define BREAKPOINT  asm volatile ("int3" ::)
101         #endif
102
103 #elif CPU_ARM
104
105         #define CPU_REG_BITS           32
106         #define CPU_REGS_CNT           16
107         #define CPU_HARVARD            0
108
109         /// Valid pointers should be >= than this value (used for debug)
110         #if CPU_ARM_AT91
111                 #define CPU_RAM_START           0x00200000
112         #else
113                 #warning Fix CPU_RAM_START address for your ARM, default value set to 0x200
114                 #define CPU_RAM_START           0x200
115         #endif
116
117         #ifdef __IAR_SYSTEMS_ICC__
118                 #warning Check CPU_BYTE_ORDER
119                 #define CPU_BYTE_ORDER (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
120
121                 #define NOP            __no_operation()
122
123         #else /* GCC and compatibles */
124
125                 #if defined(__ARMEB__)
126                         #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
127                 #elif defined(__ARMEL__)
128                         #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
129                 #else
130                         #error Unable to detect ARM endianness!
131                 #endif
132
133                 #define NOP            asm volatile ("mov r0,r0" ::)
134                 #define BREAKPOINT  /* asm("bkpt 0") DOES NOT WORK */
135
136                 #if CONFIG_FAST_MEM
137                         /**
138                          * Function attribute for use with performance critical code.
139                          *
140                          * On the AT91 family, code residing in flash has wait states.
141                          * Moving functions to the data section is a quick & dirty way
142                          * to get them transparently copied to SRAM for zero-wait-state
143                          * operation.
144                          */
145                         #define FAST_FUNC __attribute__((section(".data")))
146
147                         /**
148                          * Data attribute to move constant data to fast memory storage.
149                          *
150                          * \see FAST_FUNC
151                          */
152                         #define FAST_RODATA __attribute__((section(".data")))
153
154                 #else // !CONFIG_FAST_MEM
155                         #define FAST_RODATA /**/
156                         #define FAST_FUNC /**/
157                 #endif
158
159                 /*
160                  * Function attribute to move it into ram memory.
161                  */
162                 #define RAM_FUNC __attribute__((section(".data")))
163
164         #endif /* !__IAR_SYSTEMS_ICC_ */
165 #elif CPU_CM3
166
167         #define CPU_REG_BITS           32
168         #define CPU_REGS_CNT           16
169         #define CPU_HARVARD            0
170
171         /// Valid pointers should be >= than this value (used for debug)
172         #if CPU_CM3_LM3S1968
173                 #define CPU_RAM_START 0x20000000
174         #else
175                 #warning Fix CPU_RAM_START address for your Cortex-M3, default value set to 0x200
176                 #define CPU_RAM_START 0x200
177         #endif
178
179         #if defined(__ARMEB__)
180                 #define CPU_BYTE_ORDER CPU_BIG_ENDIAN
181         #elif defined(__ARMEL__)
182                 #define CPU_BYTE_ORDER CPU_LITTLE_ENDIAN
183         #else
184                 #error Unable to detect Cortex-M3 endianess!
185         #endif
186
187         #define NOP         asm volatile ("nop")
188         #define BREAKPOINT  /* asm("bkpt 0") DOES NOT WORK */
189
190         /*
191          * Builtin GCC memset() can be buggy! We need to redefine it here for
192          * this architecture. :(
193          */
194         #include <cfg/compiler.h>
195         #define memset  __cm3_memset
196         INLINE void *__cm3_memset(void *s, int c, size_t n)
197         {
198                 uint8_t *p = (uint8_t *)s;
199
200                 while (n--)
201                         *p++ = c;
202                 return s;
203         }
204
205 #elif CPU_PPC
206
207         #define CPU_REG_BITS           (CPU_PPC32 ? 32 : 64)
208         #define CPU_REGS_CNT           FIXME
209         #define CPU_BYTE_ORDER         (__BIG_ENDIAN__ ? CPU_BIG_ENDIAN : CPU_LITTLE_ENDIAN)
210         #define CPU_HARVARD            0
211
212         /// Valid pointers should be >= than this value (used for debug)
213         #define CPU_RAM_START          0x1000
214
215         #ifdef __GNUC__
216             #define NOP         asm volatile ("nop" ::)
217                 #define BREAKPOINT  asm volatile ("twge 2,2" ::)
218         #endif
219
220 #elif CPU_DSP56K
221
222         #define CPU_REG_BITS            16
223         #define CPU_REGS_CNT            FIXME
224         #define CPU_BYTE_ORDER          CPU_BIG_ENDIAN
225         #define CPU_HARVARD             1
226
227         /* Memory is word-addessed in the DSP56K */
228         #define CPU_BITS_PER_CHAR  16
229         #define SIZEOF_SHORT        1
230         #define SIZEOF_INT          1
231         #define SIZEOF_LONG         2
232         #define SIZEOF_PTR          1
233
234         /// Valid pointers should be >= than this value (used for debug)
235         #define CPU_RAM_START       0x200
236
237         #define NOP                     asm(nop)
238         #define BREAKPOINT              asm(debug)
239
240 #elif CPU_AVR
241
242         #define NOP                     asm volatile ("nop" ::)
243
244         #define CPU_REG_BITS            8
245         #define CPU_REGS_CNT           33 /* Includes SREG */
246         #define CPU_BYTE_ORDER          CPU_LITTLE_ENDIAN
247         #define CPU_HARVARD             1
248
249         /// Valid pointers should be >= than this value (used for debug)
250         #if CPU_AVR_ATMEGA8 || CPU_AVR_ATMEGA32 || CPU_AVR_ATMEGA103
251                 #define CPU_RAM_START       0x60
252         #elif CPU_AVR_ATMEGA64 || CPU_AVR_ATMEGA128 || CPU_AVR_ATMEGA168 || CPU_AVR_ATMEGA328P
253                 #define CPU_RAM_START       0x100
254         #elif CPU_AVR_ATMEGA1281
255                 #define CPU_RAM_START       0x200
256         #else
257                 #warning Fix CPU_RAM_START address for your AVR, default value set to 0x100
258                 #define CPU_RAM_START       0x100
259         #endif
260
261 #else
262         #error No CPU_... defined.
263 #endif
264
265 #ifndef BREAKPOINT
266 #define BREAKPOINT /* nop */
267 #endif
268
269 #ifndef FAST_FUNC
270         /// Function attribute for use with performance critical code.
271         #define FAST_FUNC /* */
272 #endif
273
274 #ifndef FAST_RODATA
275         /// Data attribute to move constant data to fast memory storage.
276         #define FAST_RODATA /* */
277 #endif
278
279 #ifndef PAUSE
280         /// Generic PAUSE implementation.
281         #define PAUSE   {NOP; MEMORY_BARRIER;}
282 #endif
283
284 #endif /* CPU_ATTR_H */