crtavr32.S 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452
  1. /*!
  2. * Copyright (C) 2001-2010 by egnite Software GmbH
  3. *
  4. * All rights reserved.
  5. *
  6. * Redistribution and use in source and binary forms, with or without
  7. * modification, are permitted provided that the following conditions
  8. * are met:
  9. *
  10. * 1. Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * 2. Redistributions in binary form must reproduce the above copyright
  13. * notice, this list of conditions and the following disclaimer in the
  14. * documentation and/or other materials provided with the distribution.
  15. * 3. Neither the name of the copyright holders nor the names of
  16. * contributors may be used to endorse or promote products derived
  17. * from this software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  20. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  21. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
  22. * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
  23. * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
  24. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
  25. * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
  26. * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
  27. * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  28. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
  29. * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  30. * SUCH DAMAGE.
  31. *
  32. * For additional information see http://www.ethernut.de/
  33. *
  34. * Portions Copyright Atmel Corporation, see the following note.
  35. */
  36. /* This file is part of the ATMEL AVR-UC3-SoftwareFramework-1.7.0 Release */
  37. /*This file is prepared for Doxygen automatic documentation generation.*/
  38. /*! \file *********************************************************************
  39. *
  40. * \brief AVR32UC C runtime startup file.
  41. *
  42. * This file has been built from the Newlib crt0.S.
  43. *
  44. * - Compiler: GNU GCC for AVR32
  45. * - Supported devices: All AVR32UC devices can be used.
  46. *
  47. * \author Atmel Corporation: http://www.atmel.com \n
  48. * Support and FAQ: http://support.atmel.no/
  49. *
  50. ******************************************************************************/
  51. /* Copyright (c) 2009 Atmel Corporation. All rights reserved.
  52. *
  53. * Redistribution and use in source and binary forms, with or without
  54. * modification, are permitted provided that the following conditions are met:
  55. *
  56. * 1. Redistributions of source code must retain the above copyright notice, this
  57. * list of conditions and the following disclaimer.
  58. *
  59. * 2. Redistributions in binary form must reproduce the above copyright notice,
  60. * this list of conditions and the following disclaimer in the documentation
  61. * and/or other materials provided with the distribution.
  62. *
  63. * 3. The name of Atmel may not be used to endorse or promote products derived
  64. * from this software without specific prior written permission.
  65. *
  66. * 4. This software may only be redistributed and used in connection with an Atmel
  67. * AVR product.
  68. *
  69. * THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
  70. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  71. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  72. * EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
  73. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  74. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  75. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
  76. * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  77. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  78. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE
  79. *
  80. */
  81. #include <avr32/io.h>
  82. // Include NutOS header files
  83. #include <arch/avr32.h>
  84. #include <cfg/clock.h>
  85. #include <cfg/memory.h>
  86. #ifndef PLL_DIV_VAL
  87. #define PLL_DIV_VAL 1
  88. #endif
  89. #ifndef PLL_MUL_VAL
  90. #define PLL_MUL_VAL 0
  91. #endif
  92. //! @{
  93. //! \verbatim
  94. // Moves efficiently an immediate value of up to 32 bits into a register.
  95. .macro mov.w rd, imm
  96. .if ((-(1 << (21 - 1))) <= \imm) && (\imm <= ((1 << (21 - 1)) - 1))
  97. mov \rd, \imm
  98. #if __AVR32_UC__ >= 2
  99. .elseif !(\imm & 0x0000FFFF)
  100. movh \rd, HI(\imm)
  101. #endif
  102. .else
  103. mov \rd, LO(\imm)
  104. orh \rd, HI(\imm)
  105. .endif
  106. .endm
  107. // Set a peripheral mux.
  108. .macro GpioSetPinFunction pin, value
  109. mov.w r0, (AVR32_GPIO_ADDRESS+(0x100 * (\pin >> 5)))
  110. eor r1, r1
  111. sbr r1, (\pin & 0x1f)
  112. .if \value == 0
  113. st.w r0[AVR32_GPIO_PMR1C], r1
  114. st.w r0[AVR32_GPIO_PMR0C], r1
  115. .elseif \value == 1
  116. st.w r0[AVR32_GPIO_PMR1C], r1
  117. st.w r0[AVR32_GPIO_PMR0S], r1
  118. .elseif \value == 2
  119. st.w r0[AVR32_GPIO_PMR1S], r1
  120. st.w r0[AVR32_GPIO_PMR0C], r1
  121. .else
  122. st.w r0[AVR32_GPIO_PMR1S], r1
  123. st.w r0[AVR32_GPIO_PMR0S], r1
  124. .endif
  125. st.w r0[AVR32_GPIO_GPERC], r1
  126. .endm
  127. // This must be linked @ 0x80000000 if it is to be run upon reset.
  128. .section .reset, "ax", @progbits
  129. .global _start
  130. .type _start, @function
  131. _start:
  132. // Jump to the C runtime startup routine.
  133. lda.w pc, _stext
  134. // _stext is placed outside the .reset section so that the program entry point
  135. // can be changed without affecting the C runtime startup.
  136. .section .text._stext, "ax", @progbits
  137. .global _stext
  138. .type _stext, @function
  139. _stext:
  140. /*
  141. * Disable Watchdog
  142. */
  143. #define WDT_KEY1 (AVR32_WDT_KEY_VALUE << AVR32_WDT_CTRL_KEY_OFFSET)
  144. #define WDT_KEY2 ((~AVR32_WDT_KEY_VALUE << AVR32_WDT_CTRL_KEY_OFFSET) & AVR32_WDT_CTRL_KEY_MASK)
  145. mov.w r0, (AVR32_WDT_ADDRESS)
  146. ld.w r1, r0[AVR32_WDT_CTRL]
  147. cbr r1, AVR32_WDT_CTRL_EN_OFFSET
  148. mov r2, r1
  149. mov.w r3, WDT_KEY1
  150. mov.w r4, WDT_KEY2
  151. or r1, r3
  152. or r2, r4
  153. st.w r0[AVR32_WDT_CTRL], r1
  154. st.w r0[AVR32_WDT_CTRL], r2
  155. /*
  156. * Enable SDRAM interface, if configured.
  157. */
  158. #ifdef NUTMEM_SDRAM_BASE
  159. // start of with pins driven / needed by EBI
  160. // Enable data pins
  161. GpioSetPinFunction AVR32_EBI_DATA_0_PIN, AVR32_EBI_DATA_0_FUNCTION
  162. GpioSetPinFunction AVR32_EBI_DATA_1_PIN, AVR32_EBI_DATA_1_FUNCTION
  163. GpioSetPinFunction AVR32_EBI_DATA_2_PIN, AVR32_EBI_DATA_2_FUNCTION
  164. GpioSetPinFunction AVR32_EBI_DATA_3_PIN, AVR32_EBI_DATA_3_FUNCTION
  165. GpioSetPinFunction AVR32_EBI_DATA_4_PIN, AVR32_EBI_DATA_4_FUNCTION
  166. GpioSetPinFunction AVR32_EBI_DATA_5_PIN, AVR32_EBI_DATA_5_FUNCTION
  167. GpioSetPinFunction AVR32_EBI_DATA_6_PIN, AVR32_EBI_DATA_6_FUNCTION
  168. GpioSetPinFunction AVR32_EBI_DATA_7_PIN, AVR32_EBI_DATA_7_FUNCTION
  169. GpioSetPinFunction AVR32_EBI_DATA_8_PIN, AVR32_EBI_DATA_8_FUNCTION
  170. GpioSetPinFunction AVR32_EBI_DATA_9_PIN, AVR32_EBI_DATA_9_FUNCTION
  171. GpioSetPinFunction AVR32_EBI_DATA_10_PIN, AVR32_EBI_DATA_10_FUNCTION
  172. GpioSetPinFunction AVR32_EBI_DATA_11_PIN, AVR32_EBI_DATA_11_FUNCTION
  173. GpioSetPinFunction AVR32_EBI_DATA_12_PIN, AVR32_EBI_DATA_12_FUNCTION
  174. GpioSetPinFunction AVR32_EBI_DATA_13_PIN, AVR32_EBI_DATA_13_FUNCTION
  175. GpioSetPinFunction AVR32_EBI_DATA_14_PIN, AVR32_EBI_DATA_14_FUNCTION
  176. GpioSetPinFunction AVR32_EBI_DATA_15_PIN, AVR32_EBI_DATA_15_FUNCTION
  177. #if NUTMEM_SDRAM_DBW >= 32
  178. GpioSetPinFunction AVR32_EBI_DATA_16_PIN, AVR32_EBI_DATA_16_FUNCTION
  179. GpioSetPinFunction AVR32_EBI_DATA_17_PIN, AVR32_EBI_DATA_17_FUNCTION
  180. GpioSetPinFunction AVR32_EBI_DATA_18_PIN, AVR32_EBI_DATA_18_FUNCTION
  181. GpioSetPinFunction AVR32_EBI_DATA_19_PIN, AVR32_EBI_DATA_19_FUNCTION
  182. GpioSetPinFunction AVR32_EBI_DATA_20_PIN, AVR32_EBI_DATA_20_FUNCTION
  183. GpioSetPinFunction AVR32_EBI_DATA_21_PIN, AVR32_EBI_DATA_21_FUNCTION
  184. GpioSetPinFunction AVR32_EBI_DATA_22_PIN, AVR32_EBI_DATA_22_FUNCTION
  185. GpioSetPinFunction AVR32_EBI_DATA_23_PIN, AVR32_EBI_DATA_23_FUNCTION
  186. GpioSetPinFunction AVR32_EBI_DATA_24_PIN, AVR32_EBI_DATA_24_FUNCTION
  187. GpioSetPinFunction AVR32_EBI_DATA_25_PIN, AVR32_EBI_DATA_25_FUNCTION
  188. GpioSetPinFunction AVR32_EBI_DATA_26_PIN, AVR32_EBI_DATA_26_FUNCTION
  189. GpioSetPinFunction AVR32_EBI_DATA_27_PIN, AVR32_EBI_DATA_27_FUNCTION
  190. GpioSetPinFunction AVR32_EBI_DATA_28_PIN, AVR32_EBI_DATA_28_FUNCTION
  191. GpioSetPinFunction AVR32_EBI_DATA_29_PIN, AVR32_EBI_DATA_29_FUNCTION
  192. GpioSetPinFunction AVR32_EBI_DATA_30_PIN, AVR32_EBI_DATA_30_FUNCTION
  193. GpioSetPinFunction AVR32_EBI_DATA_31_PIN, AVR32_EBI_DATA_31_FUNCTION
  194. GpioSetPinFunction AVR32_EBI_DATA_32_PIN, AVR32_EBI_DATA_32_FUNCTION
  195. #endif
  196. // Enable row/column address pins.
  197. GpioSetPinFunction AVR32_EBI_ADDR_2_PIN, AVR32_EBI_ADDR_2_FUNCTION
  198. GpioSetPinFunction AVR32_EBI_ADDR_3_PIN, AVR32_EBI_ADDR_3_FUNCTION
  199. GpioSetPinFunction AVR32_EBI_ADDR_4_PIN, AVR32_EBI_ADDR_4_FUNCTION
  200. GpioSetPinFunction AVR32_EBI_ADDR_5_PIN, AVR32_EBI_ADDR_5_FUNCTION
  201. GpioSetPinFunction AVR32_EBI_ADDR_6_PIN, AVR32_EBI_ADDR_6_FUNCTION
  202. GpioSetPinFunction AVR32_EBI_ADDR_7_PIN, AVR32_EBI_ADDR_7_FUNCTION
  203. GpioSetPinFunction AVR32_EBI_ADDR_8_PIN, AVR32_EBI_ADDR_8_FUNCTION
  204. GpioSetPinFunction AVR32_EBI_ADDR_9_PIN, AVR32_EBI_ADDR_9_FUNCTION
  205. GpioSetPinFunction AVR32_EBI_ADDR_10_PIN, AVR32_EBI_ADDR_10_FUNCTION
  206. GpioSetPinFunction AVR32_EBI_ADDR_11_PIN, AVR32_EBI_ADDR_11_FUNCTION
  207. GpioSetPinFunction AVR32_EBI_SDA10_0_PIN, AVR32_EBI_SDA10_0_FUNCTION
  208. #if NUTMEM_SDRAM_ROWBITS >= 12
  209. GpioSetPinFunction AVR32_EBI_ADDR_13_PIN, AVR32_EBI_ADDR_13_FUNCTION
  210. # if NUTMEM_SDRAM_ROWBITS >= 12
  211. GpioSetPinFunction AVR32_EBI_ADDR_14_PIN, AVR32_EBI_ADDR_14_FUNCTION
  212. # endif
  213. #endif
  214. // Enable bank address pins.
  215. GpioSetPinFunction AVR32_EBI_ADDR_16_PIN, AVR32_EBI_ADDR_16_FUNCTION
  216. #if NUTMEM_SDRAM_BANKS >= 2
  217. GpioSetPinFunction AVR32_EBI_ADDR_17_PIN, AVR32_EBI_ADDR_17_FUNCTION
  218. #endif
  219. // Enable data mask pins.
  220. GpioSetPinFunction AVR32_EBI_ADDR_0_PIN, AVR32_EBI_ADDR_0_FUNCTION
  221. GpioSetPinFunction AVR32_EBI_NWE1_0_PIN, AVR32_EBI_NWE1_0_FUNCTION
  222. #if NUTMEM_SDRAM_DBW >= 32
  223. GpioSetPinFunction AVR32_EBI_ADDR_1_PIN, AVR32_EBI_ADDR_1_FUNCTION
  224. GpioSetPinFunction AVR32_EBI_NWE3_0_PIN, AVR32_EBI_NWE3_0_FUNCTION
  225. #endif
  226. // Enable control pins.
  227. GpioSetPinFunction AVR32_EBI_SDWE_0_PIN, AVR32_EBI_SDWE_0_FUNCTION
  228. GpioSetPinFunction AVR32_EBI_CAS_0_PIN, AVR32_EBI_CAS_0_FUNCTION
  229. GpioSetPinFunction AVR32_EBI_RAS_0_PIN, AVR32_EBI_RAS_0_FUNCTION
  230. GpioSetPinFunction AVR32_EBI_NCS_1_PIN, AVR32_EBI_NCS_1_FUNCTION
  231. // Enable clock-related pins.
  232. GpioSetPinFunction AVR32_EBI_SDCK_0_PIN, AVR32_EBI_SDCK_0_FUNCTION
  233. GpioSetPinFunction AVR32_EBI_SDCKE_0_PIN, AVR32_EBI_SDCKE_0_FUNCTION
  234. // Set R0 to HMatrix SFR5 base address
  235. mov.w r0, (AVR32_HMATRIX_ADDRESS+AVR32_HMATRIX_SFR0+(4*AVR32_EBI_HMATRIX_NR))
  236. ld.w r1, r0
  237. sbr r1, AVR32_EBI_SDRAM_CS // Enable SDRAM mode for AVR32_EBI_SDRAM_CS.
  238. st.w r0, r1 // Special Function Register AVR32_EBI_HMATRIX_NR
  239. // Configure the SDRAM Controller with SDRAM setup and timing information.
  240. // All timings below are rounded up because they are minimal values.
  241. // Frequency of the HSB in Mhz rounded up. This is the same as the CPU Clock
  242. #define HSB_MHZ_UP ((((PLL_MUL_VAL+1)/PLL_DIV_VAL*OSC0_VAL) + 999999) / 1000000)
  243. // Frequency of the HSB in Mhz rounded down.
  244. #define HSB_MHZ_DOWN (((PLL_MUL_VAL+1)/PLL_DIV_VAL*OSC0_VAL) / 1000000)
  245. #define SDRAM_CR_VALUE \
  246. ((( NUTMEM_SDRAM_COLBITS - 8) << AVR32_SDRAMC_CR_NC_OFFSET ) & AVR32_SDRAMC_CR_NC_MASK ) | \
  247. ((( NUTMEM_SDRAM_ROWBITS - 11) << AVR32_SDRAMC_CR_NR_OFFSET ) & AVR32_SDRAMC_CR_NR_MASK ) | \
  248. ((( NUTMEM_SDRAM_BANKS - 1) << AVR32_SDRAMC_CR_NB_OFFSET ) & AVR32_SDRAMC_CR_NB_MASK ) | \
  249. (( NUTMEM_SDRAM_CASLAT << AVR32_SDRAMC_CR_CAS_OFFSET ) & AVR32_SDRAMC_CR_CAS_MASK ) | \
  250. ((( NUTMEM_SDRAM_DBW >> 4) << AVR32_SDRAMC_CR_DBW_OFFSET ) & AVR32_SDRAMC_CR_DBW_MASK ) | \
  251. ((((NUTMEM_SDRAM_TWR * HSB_MHZ_UP + 999) / 1000) << AVR32_SDRAMC_CR_TWR_OFFSET ) & AVR32_SDRAMC_CR_TWR_MASK ) | \
  252. ((((NUTMEM_SDRAM_TRC * HSB_MHZ_UP + 999) / 1000) << AVR32_SDRAMC_CR_TRC_OFFSET ) & AVR32_SDRAMC_CR_TRC_MASK ) | \
  253. ((((NUTMEM_SDRAM_TRP * HSB_MHZ_UP + 999) / 1000) << AVR32_SDRAMC_CR_TRP_OFFSET ) & AVR32_SDRAMC_CR_TRP_MASK ) | \
  254. ((((NUTMEM_SDRAM_TRCD * HSB_MHZ_UP + 999) / 1000) << AVR32_SDRAMC_CR_TRCD_OFFSET) & AVR32_SDRAMC_CR_TRCD_MASK) | \
  255. ((((NUTMEM_SDRAM_TRAS * HSB_MHZ_UP + 999) / 1000) << AVR32_SDRAMC_CR_TRAS_OFFSET) & AVR32_SDRAMC_CR_TRAS_MASK) | \
  256. ((((NUTMEM_SDRAM_TXSR * HSB_MHZ_UP + 999) / 1000) << AVR32_SDRAMC_CR_TXSR_OFFSET) & AVR32_SDRAMC_CR_TXSR_MASK)
  257. mov.w r1, SDRAM_CR_VALUE
  258. mov.w r0, (AVR32_SDRAMC_ADDRESS)
  259. st.w r0[AVR32_SDRAMC_CR], r1
  260. // AVR32_SDRAMC.mr = AVR32_SDRAMC_MR_MODE_NOP;
  261. mov.w r1, AVR32_SDRAMC_MR_MODE_NOP // Mode Register value = NOP
  262. st.w r0[AVR32_SDRAMC_MR], r1
  263. ld.w r1, r0[AVR32_SDRAMC_MR] // Cause a stall and flush the value.
  264. mov.w r0, NUTMEM_SDRAM_BASE // SDRAM start address
  265. st.w r0, r1 // write to DRAM to trigger the NOP
  266. // Wait during the SDRAM stable-clock initialization delay.
  267. mov.w r12, HSB_MHZ_UP*200 // delay 200u
  268. call sdramc_delay
  269. // Issue a PRECHARGE ALL command to the SDRAM.
  270. mov.w r0, (AVR32_SDRAMC_ADDRESS) // Mode Register
  271. mov.w r1, (AVR32_SDRAMC_MR_MODE_BANKS_PRECHARGE) // All banks precharge
  272. st.w r0[AVR32_SDRAMC_MR], r1
  273. ld.w r1, r0[AVR32_SDRAMC_MR] // Cause a stall and flush the value.
  274. mov.w r0, NUTMEM_SDRAM_BASE // SDRAM start address
  275. st.w r0, r1 // write to DRAM to trigger precharge command
  276. // Wait during the SDRAM TRP delay.
  277. mov.w r12, HSB_MHZ_UP*NUTMEM_SDRAM_TRP
  278. call sdramc_delay
  279. // Issue initialization AUTO REFRESH commands to the SDRAM.
  280. mov.w r0, (AVR32_SDRAMC_ADDRESS)
  281. mov.w r1, (AVR32_SDRAMC_MR_MODE_AUTO_REFRESH)
  282. st.w r0[AVR32_SDRAMC_MR], r1
  283. ld.w r1, r0[AVR32_SDRAMC_MR] // Cause a stall and flush the value.
  284. mov.w r12, HSB_MHZ_UP*NUTMEM_SDRAM_TRC
  285. mov.w r0, NUTMEM_SDRAM_BASE // SDRAM start address
  286. st.w r0, r1 // write to DRAM to trigger auto refresh command
  287. call sdramc_delay
  288. st.w r0, r1 // write to DRAM to trigger precharge command
  289. call sdramc_delay
  290. st.w r0, r1 // write to DRAM to trigger precharge command
  291. call sdramc_delay
  292. st.w r0, r1 // write to DRAM to trigger precharge command
  293. call sdramc_delay
  294. st.w r0, r1 // write to DRAM to trigger precharge command
  295. call sdramc_delay
  296. st.w r0, r1 // write to DRAM to trigger precharge command
  297. call sdramc_delay
  298. st.w r0, r1 // write to DRAM to trigger precharge command
  299. call sdramc_delay
  300. st.w r0, r1 // write to DRAM to trigger precharge command
  301. // Issue a LOAD MODE REGISTER command to the SDRAM.
  302. // This configures the SDRAM with the following parameters in the mode register:
  303. // - bits 0 to 2: burst length: 1 (000b);
  304. // - bit 3: burst type: sequential (0b);
  305. // - bits 4 to 6: CAS latency: AVR32_SDRAMC.CR.cas;
  306. // - bits 7 to 8: operating mode: standard operation (00b);
  307. // - bit 9: write burst mode: programmed burst length (0b);
  308. // - all other bits: reserved: 0b.
  309. mov.w r0, (AVR32_SDRAMC_ADDRESS)
  310. mov.w r1, (AVR32_SDRAMC_MR_MODE_LOAD_MODE)
  311. st.w r0[AVR32_SDRAMC_MR], r1
  312. mov.w r0, NUTMEM_SDRAM_BASE // SDRAM start address
  313. st.w r0, r1 // write to DRAM to trigger "load mode register" command
  314. mov.w r12, HSB_MHZ_UP*NUTMEM_SDRAM_TMRD
  315. call sdramc_delay
  316. // Switch the SDRAM Controller to normal mode.
  317. mov.w r0, (AVR32_SDRAMC_ADDRESS)
  318. mov.w r1, (AVR32_SDRAMC_MR_MODE_NORMAL)
  319. st.w r0[AVR32_SDRAMC_MR], r1
  320. ld.w r1, r0[AVR32_SDRAMC_MR] // Cause a stall and flush the value.
  321. mov.w r0, NUTMEM_SDRAM_BASE // SDRAM start address
  322. st.w r0, r1 // write to DRAM to trigger "load mode register" command
  323. // Write the refresh period into the SDRAMC Refresh Timer Register.
  324. // TR is rounded down because it is a maximal value.
  325. mov.w r0, (AVR32_SDRAMC_ADDRESS) // Refresh Timer Register (TR)
  326. mov.w r1, (NUTMEM_SDRAM_TR * HSB_MHZ_DOWN) / 1000
  327. st.w r0[AVR32_SDRAMC_MR], r1
  328. ld.w r1, r0[AVR32_SDRAMC_MR]
  329. #endif // NUTMEM_SDRAM_BASE
  330. // Set initial stack pointer.
  331. lda.w sp, _estack
  332. // Set up EVBA so interrupts can be enabled.
  333. lda.w r0, _evba
  334. mtsr AVR32_EVBA, r0
  335. // Enable the exception processing.
  336. csrf AVR32_SR_EM_OFFSET
  337. // Load initialized data having a global lifetime from the data LMA.
  338. lda.w r0, _data
  339. lda.w r1, _edata
  340. cp r0, r1
  341. brhs idata_load_loop_end
  342. lda.w r2, _data_lma
  343. idata_load_loop:
  344. ld.d r4, r2++
  345. st.d r0++, r4
  346. cp r0, r1
  347. brlo idata_load_loop
  348. idata_load_loop_end:
  349. // Clear uninitialized data having a global lifetime in the blank static storage section.
  350. lda.w r0, __bss_start
  351. lda.w r1, _end
  352. cp r0, r1
  353. brhs udata_clear_loop_end
  354. mov r2, 0
  355. mov r3, 0
  356. udata_clear_loop:
  357. st.d r0++, r2
  358. cp r0, r1
  359. brlo udata_clear_loop
  360. udata_clear_loop_end:
  361. #ifdef CONFIG_FRAME_POINTER
  362. // Safety: Set the default "return" @ to the exit routine address.
  363. lda.w lr, exit
  364. #endif
  365. // Start the show.
  366. lda.w pc, NutInit
  367. #ifdef NUTMEM_SDRAM_BASE
  368. // sdramc_delay
  369. // r12 = Number of HSB clock cycles to wait.
  370. // Warning: This call clobbers r10 and r11
  371. .global sdramc_delay
  372. .type sdramc_delay, @function
  373. sdramc_delay:
  374. // r10 = startCycle, r11 = endCycle
  375. mfsr r10, AVR32_COUNT
  376. add r11, r10, r12
  377. cp.w r10, r11
  378. brls .LCOUNTNOTWRAPPED
  379. .LCOUNTWRAPPED:
  380. // while (AVR32_COUNT > endCycle);
  381. mfsr r10, AVR32_COUNT
  382. cp.w r10, r11
  383. brhi .LCOUNTWRAPPED
  384. .LCOUNTNOTWRAPPED:
  385. mfsr r10, AVR32_COUNT
  386. cp.w r10, r11
  387. brlo .LCOUNTNOTWRAPPED
  388. mov pc, lr
  389. .size sdramc_delay, .-sdramc_delay
  390. #endif
  391. //! \endverbatim
  392. //! @}
  393. #include "exception.S"