]> git.sur5r.net Git - freertos/blob - FreeRTOS/Demo/CORTEX_A5_SAMA5D4x_EK_IAR/AtmelFiles/libboard_sama5d4x-ek/source/board_memories.c
Core kernel files:
[freertos] / FreeRTOS / Demo / CORTEX_A5_SAMA5D4x_EK_IAR / AtmelFiles / libboard_sama5d4x-ek / source / board_memories.c
1 /* ----------------------------------------------------------------------------\r
2  *         SAM Software Package License\r
3  * ----------------------------------------------------------------------------\r
4  * Copyright (c) 20143, Atmel Corporation\r
5  *\r
6  * All rights reserved.\r
7  *\r
8  * Redistribution and use in source and binary forms, with or without\r
9  * modification, are permitted provided that the following conditions are met:\r
10  *\r
11  * - Redistributions of source code must retain the above copyright notice,\r
12  * this list of conditions and the disclaimer below.\r
13  *\r
14  * Atmel's name may not be used to endorse or promote products derived from\r
15  * this software without specific prior written permission.\r
16  *\r
17  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR\r
18  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF\r
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE\r
20  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,\r
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT\r
22  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,\r
23  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF\r
24  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING\r
25  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,\r
26  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.\r
27  * ----------------------------------------------------------------------------\r
28  */\r
29 \r
30 /** \addtogroup ddrd_module \r
31  *\r
32  * The DDR/SDR SDRAM Controller (DDRSDRC) is a multiport memory controller. It comprises\r
33  * four slave AHB interfaces. All simultaneous accesses (four independent AHB ports) are interleaved\r
34  * to maximize memory bandwidth and minimize transaction latency due to SDRAM protocol.\r
35  * \r
36  * \section ddr2 Configures DDR2\r
37  *\r
38  * The DDR2-SDRAM devices are initialized by the following sequence:\r
39  * <ul>\r
40  * <li> EBI Chip Select 1 is assigned to the DDR2SDR Controller, Enable DDR2 clock x2 in PMC.</li>\r
41  * <li> Step 1: Program the memory device type</li>\r
42  * <li> Step 2:\r
43  *  -# Program the features of DDR2-SDRAM device into the Configuration Register.\r
44  *  -# Program the features of DDR2-SDRAM device into the Timing Register HDDRSDRC2_T0PR.\r
45  *  -# Program the features of DDR2-SDRAM device into the Timing Register HDDRSDRC2_T1PR.\r
46  *  -# Program the features of DDR2-SDRAM device into the Timing Register HDDRSDRC2_T2PR. </li>\r
47  * <li> Step 3: An NOP command is issued to the DDR2-SDRAM to enable clock. </li>\r
48  * <li> Step 4:  An NOP command is issued to the DDR2-SDRAM </li>\r
49  * <li> Step 5: An all banks precharge command is issued to the DDR2-SDRAM. </li>\r
50  * <li> Step 6: An Extended Mode Register set (EMRS2) cycle is  issued to chose between commercialor high  temperature operations.</li>\r
51  * <li> Step 7: An Extended Mode Register set (EMRS3) cycle is issued to set all registers to 0. </li>\r
52  * <li> Step 8:  An Extended Mode Register set (EMRS1) cycle is issued to enable DLL.</li>\r
53  * <li> Step 9:  Program DLL field into the Configuration Register.</li>\r
54  * <li> Step 10: A Mode Register set (MRS) cycle is issued to reset DLL.</li>\r
55  * <li> Step 11: An all banks precharge command is issued to the DDR2-SDRAM.</li>\r
56  * <li> Step 12: Two auto-refresh (CBR) cycles are provided. Program the auto refresh command (CBR) into the Mode Register.</li>\r
57  * <li> Step 13: Program DLL field into the Configuration Register to low(Disable DLL reset).</li>\r
58  * <li> Step 14: A Mode Register set (MRS) cycle is issued to program the parameters of the DDR2-SDRAM devices.</li>\r
59  * <li> Step 15: Program OCD field into the Configuration Register to high (OCD calibration default). </li>\r
60  * <li> Step 16: An Extended Mode Register set (EMRS1) cycle is issued to OCD default value.</li>\r
61  * <li> Step 17: Program OCD field into the Configuration Register to low (OCD calibration mode exit).</li>\r
62  * <li> Step 18: An Extended Mode Register set (EMRS1) cycle is issued to enable OCD exit.</li>\r
63  * <li> Step 19,20: A mode Normal command is provided. Program the Normal mode into Mode Register.</li>\r
64  * <li> Step 21: Write the refresh rate into the count field in the Refresh Timer register. The DDR2-SDRAM device requires a refresh every 15.625 or 7.81. </li>\r
65  * </ul>\r
66 */\r
67 /*@{*/\r
68 /*@}*/\r
69 \r
70 /** \addtogroup sdram_module\r
71  *\r
72  * \section sdram Configures SDRAM\r
73  *\r
74  * The SDR-SDRAM devices are initialized by the following sequence:\r
75  * <ul>\r
76  * <li> EBI Chip Select 1 is assigned to the DDR2SDR Controller, Enable DDR2 clock x2 in PMC.</li>\r
77  * <li> Step 1. Program the memory device type into the Memory Device Register</li>\r
78  * <li> Step 2. Program the features of the SDR-SDRAM device into the Timing Register and into the Configuration Register.</li>\r
79  * <li> Step 3. For low-power SDRAM, temperature-compensated self refresh (TCSR), drive strength (DS) and partial array self refresh (PASR) must be set in the Low-power Register.</li>\r
80  * <li> Step 4. A NOP command is issued to the SDR-SDRAM. Program NOP command into Mode Register, the application must \r
81  * set Mode to 1 in the Mode Register. Perform a write access to any SDR-SDRAM address to acknowledge this command. \r
82  * Now the clock which drives SDR-SDRAM device is enabled.</li>\r
83  * <li> Step 5. An all banks precharge command is issued to the SDR-SDRAM. Program all banks precharge command into Mode Register, the application must set Mode to 2 in the\r
84  * Mode Register . Perform a write access to any SDRSDRAM address to acknowledge this command.</li>\r
85  * <li> Step 6. Eight auto-refresh (CBR) cycles are provided. Program the auto refresh command (CBR) into Mode Register, the application must set Mode to 4 in the Mode Register.\r
86  * Once in the idle state, two AUTO REFRESH cycles must be performed.</li>\r
87  * <li> Step 7. A Mode Register set (MRS) cycle is issued to program the parameters of the SDRSDRAM\r
88  * devices, in particular CAS latency and burst length. </li>\r
89  * <li> Step 8. For low-power SDR-SDRAM initialization, an Extended Mode Register set (EMRS) cycle is issued to program the SDR-SDRAM parameters (TCSR, PASR, DS). The write\r
90  * address must be chosen so that BA[1] is set to 1 and BA[0] is set to 0 </li>\r
91  * <li> Step 9. The application must go into Normal Mode, setting Mode to 0 in the Mode Register and perform a write access at any location in the SDRAM to acknowledge this command.</li>\r
92  * <li> Step 10. Write the refresh rate into the count field in the DDRSDRC Refresh Timer register </li>\r
93 * </ul>\r
94 */\r
95 /*@{*/\r
96 /*@}*/\r
97 \r
98  \r
99  \r
100 /**\r
101  * \file\r
102  *\r
103  * Implementation of memories configuration on board.\r
104  *\r
105  */\r
106 \r
107  \r
108 /*----------------------------------------------------------------------------\r
109  *        Headers\r
110  *----------------------------------------------------------------------------*/\r
111 #include "board.h"\r
112 \r
113 /*----------------------------------------------------------------------------\r
114  *        Exported functions\r
115  *----------------------------------------------------------------------------*/\r
116 \r
117 /**\r
118  * \brief Changes the mapping of the chip so that the remap area mirrors the\r
119  * internal ROM or the EBI CS0.\r
120  */\r
121 void BOARD_RemapRom( void )\r
122 {\r
123     AXIMX->AXIMX_REMAP = 0;\r
124 }\r
125 \r
126 /**\r
127  * \brief Changes the mapping of the chip so that the remap area mirrors the\r
128  * internal RAM.\r
129  */\r
130 \r
131 void BOARD_RemapRam( void )\r
132 {\r
133     AXIMX->AXIMX_REMAP = AXIMX_REMAP_REMAP0;\r
134 }\r
135 \r
136 /**\r
137  * \brief Initialize Vdd EBI drive\r
138  * \param 0: 1.8V 1: 3.3V\r
139  */\r
140 void BOARD_ConfigureVddMemSel( uint8_t VddMemSel )\r
141 {\r
142 }\r
143  \r
144 #define DDR2_BA0(r) (1 << (26 + r))\r
145 #define DDR2_BA1(r) (1 << (27 + r))\r
146 \r
147 #define H64MX_DDR_SLAVE_PORT0   3\r
148 \r
149 static void matrix_configure_slave_ddr(void)\r
150 {\r
151      int ddr_port;\r
152  \r
153      /* Disable write protection */\r
154      MATRIX0->MATRIX_WPMR = MPDDRC_WPMR_WPKEY_PASSWD;\r
155  \r
156      /* Partition internal SRAM */\r
157      MATRIX0->MATRIX_SSR[11]   = 0;\r
158      MATRIX0->MATRIX_SRTSR[11] = 0x05;\r
159      MATRIX0->MATRIX_SASSR[11] = 0x04;\r
160  \r
161      ddr_port = 1;\r
162       \r
163      /* Partition external DDR */\r
164      /* DDR port 0 not used from NWd */\r
165      for (ddr_port = 1 ; ddr_port < 8 ; ddr_port++) {\r
166            MATRIX0->MATRIX_SSR[H64MX_DDR_SLAVE_PORT0 + ddr_port]   = 0x00FFFFFF;\r
167            MATRIX0->MATRIX_SRTSR[H64MX_DDR_SLAVE_PORT0 + ddr_port] = 0x0000000F;\r
168            MATRIX0->MATRIX_SASSR[H64MX_DDR_SLAVE_PORT0 + ddr_port] = 0x0000FFFF;\r
169      }\r
170 }\r
171 \r
172 #define MATRIX_KEY_VAL (0x4D4154u)\r
173 \r
174 static void matrix_configure_slave_nand(void)\r
175 {\r
176     /* Disable write protection */\r
177     MATRIX0->MATRIX_WPMR = MATRIX_WPMR_WPKEY(MATRIX_KEY_VAL);\r
178     MATRIX1->MATRIX_WPMR = MATRIX_WPMR_WPKEY(MATRIX_KEY_VAL);\r
179 \r
180     /* Partition internal SRAM */\r
181     MATRIX0->MATRIX_SSR[11]   = 0x00010101;\r
182     MATRIX0->MATRIX_SRTSR[11] = 0x05;\r
183     MATRIX0->MATRIX_SASSR[11] = 0x05;\r
184 \r
185     MATRIX1->MATRIX_SRTSR[3] = 0xBBBBBBBB;\r
186     MATRIX1->MATRIX_SSR[3]   = 0x00FFFFFF;\r
187     MATRIX1->MATRIX_SASSR[3] = 0xBBBBBBBB;\r
188 \r
189     MATRIX1->MATRIX_SRTSR[4] = 0x01;\r
190     MATRIX1->MATRIX_SSR[4]   = 0x00FFFFFF;\r
191     MATRIX1->MATRIX_SASSR[4] = 0x01;\r
192     MATRIX1->MATRIX_MEIER = 0x3FF;\r
193 }\r
194 \r
195 /**\r
196  * \brief Configures DDR2 (MT47H128M16RT 128MB/ MT47H64M16HR)\r
197  MT47H64M16HR : 8 Meg x 16 x 8 banks\r
198  Refresh count: 8K\r
199  Row address: A[12:0] (8K)\r
200  Column address A[9:0] (1K)\r
201  Bank address BA[2:0] a(24,25) (8) \r
202  */\r
203 void BOARD_ConfigureDdram( void )\r
204 {\r
205     volatile uint8_t *pDdr = (uint8_t *) DDR_CS_ADDR;\r
206     volatile uint32_t i;\r
207 \r
208     volatile uint32_t dummy_value;\r
209     \r
210     matrix_configure_slave_ddr();\r
211 \r
212     /* Enable DDR2 clock x2 in PMC */\r
213     PMC->PMC_PCER0 = (1 << (ID_MPDDRC));\r
214     PMC->PMC_SCER  |= PMC_SCER_DDRCK;\r
215  \r
216     /* MPDDRC I/O Calibration Register */\r
217     dummy_value  =  MPDDRC->MPDDRC_IO_CALIBR;\r
218     dummy_value &= ~MPDDRC_IO_CALIBR_RDIV_Msk;\r
219     dummy_value &= ~MPDDRC_IO_CALIBR_TZQIO_Msk;\r
220     dummy_value |= MPDDRC_IO_CALIBR_CALCODEP(7);\r
221     dummy_value |= MPDDRC_IO_CALIBR_CALCODEN(8);\r
222     dummy_value |= MPDDRC_IO_CALIBR_RDIV_RZQ_60_RZQ_50;\r
223     dummy_value |= MPDDRC_IO_CALIBR_TZQIO(5);\r
224     dummy_value |= MPDDRC_IO_CALIBR_EN_CALIB_ENABLE_CALIBRATION;\r
225     MPDDRC->MPDDRC_IO_CALIBR = dummy_value;\r
226 \r
227     /* Step 1: Program the memory device type */\r
228     /* DBW = 0 (32 bits bus wide); Memory Device = 6 = DDR2-SDRAM = 0x00000006*/\r
229 \r
230     MPDDRC->MPDDRC_MD   =  MPDDRC_MD_MD_DDR2_SDRAM | MPDDRC_MD_DBW_DBW_32_BITS; \r
231 \r
232     MPDDRC->MPDDRC_RD_DATA_PATH = MPDDRC_RD_DATA_PATH_SHIFT_SAMPLING_SHIFT_ONE_CYCLE;\r
233 \r
234     /* Step 2: Program the features of DDR2-SDRAM device into the Timing Register.*/\r
235     MPDDRC->MPDDRC_CR    = MPDDRC_CR_NR_14_ROW_BITS     |\r
236                            MPDDRC_CR_NC_10_COL_BITS     |\r
237                            MPDDRC_CR_CAS_DDR_CAS3       |\r
238                            MPDDRC_CR_DLL_RESET_DISABLED |\r
239                            MPDDRC_CR_DQMS_NOT_SHARED    |\r
240                            MPDDRC_CR_ENRDM_OFF          |\r
241                            MPDDRC_CR_NB_8_BANKS         |\r
242                            MPDDRC_CR_NDQS_DISABLED      |\r
243                            MPDDRC_CR_UNAL_SUPPORTED     |\r
244                            MPDDRC_CR_OCD_DDR2_EXITCALIB; \r
245    \r
246     MPDDRC->MPDDRC_TPR0 = MPDDRC_TPR0_TRAS(8)    //  40 ns\r
247                         | MPDDRC_TPR0_TRCD(3)    //  12.5 ns\r
248                         | MPDDRC_TPR0_TWR(3)     //  15 ns\r
249                         | MPDDRC_TPR0_TRC(10)    //  55 ns\r
250                         | MPDDRC_TPR0_TRP(3)     //  12.5 ns\r
251                         | MPDDRC_TPR0_TRRD(2)    //  8 ns\r
252                         | MPDDRC_TPR0_TWTR(2)    //  2 clock cycle\r
253                         | MPDDRC_TPR0_TMRD(2);   //  2 clock cycles\r
254     \r
255 \r
256     MPDDRC->MPDDRC_TPR1 = MPDDRC_TPR1_TRFC(23)\r
257                         | MPDDRC_TPR1_TXSNR(25)\r
258                         | MPDDRC_TPR1_TXSRD(200)\r
259                         | MPDDRC_TPR1_TXP(2);\r
260 \r
261     MPDDRC->MPDDRC_TPR2 = MPDDRC_TPR2_TXARD(8)  \r
262                           | MPDDRC_TPR2_TXARDS(2)\r
263                           | MPDDRC_TPR2_TRPA(3)\r
264                           | MPDDRC_TPR2_TRTP(2)\r
265                           | MPDDRC_TPR2_TFAW(7);\r
266 \r
267     /* DDRSDRC Low-power Register */\r
268     for (i = 0; i < 13300; i++) {\r
269         asm("nop");\r
270     } \r
271 \r
272 /* Step 3: An NOP command is issued to the DDR2-SDRAM. Program the NOP command into\r
273     the Mode Register, the application must set MODE to 1 in the Mode Register. */\r
274     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_NOP_CMD;\r
275     /* Perform a write access to any DDR2-SDRAM address to acknowledge this command */\r
276     *pDdr = 0;  /* Now clocks which drive DDR2-SDRAM device are enabled.*/\r
277    \r
278     /* A minimum pause of 200 ¦Ìs is provided to precede any signal toggle. (6 core cycles per iteration, core is at 396MHz: min 13200 loops) */\r
279     for (i = 0; i < 13300; i++) {\r
280         asm("nop");\r
281     } \r
282  \r
283 /* Step 4:  An NOP command is issued to the DDR2-SDRAM */\r
284     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_NOP_CMD;\r
285     /* Perform a write access to any DDR2-SDRAM address to acknowledge this command.*/\r
286     *pDdr = 0; /* Now CKE is driven high.*/\r
287     /* wait 400 ns min */\r
288     for (i = 0; i < 100; i++) {\r
289         asm("nop");\r
290     }\r
291  \r
292 /* Step 5: An all banks precharge command is issued to the DDR2-SDRAM. */\r
293     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_PRCGALL_CMD;\r
294     /* Perform a write access to any DDR2-SDRAM address to acknowledge this command.*/\r
295     *pDdr = 0;\r
296     /* wait 400 ns min */\r
297     for (i = 0; i < 100; i++) {\r
298         asm("nop");\r
299     }\r
300 \r
301 /* Step 6: An Extended Mode Register set (EMRS2) cycle is  issued to chose between commercialor high  temperature operations. */\r
302     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_EXT_LMR_CMD;\r
303     *((uint8_t *)(pDdr + DDR2_BA1(0))) = 0; /* The write address must be chosen so that BA[1] is set to 1 and BA[0] is set to 0. */\r
304     /* wait 2 cycles min */\r
305     for (i = 0; i < 100; i++) {\r
306         asm("nop");\r
307     }\r
308 \r
309 /* Step 7: An Extended Mode Register set (EMRS3) cycle is issued to set all registers to 0. */\r
310     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_EXT_LMR_CMD;\r
311     *((uint8_t *)(pDdr + DDR2_BA1(0) + DDR2_BA0(0))) = 0;  /* The write address must be chosen so that BA[1] is set to 1 and BA[0] is set to 1.*/\r
312     /* wait 2 cycles min */\r
313     for (i = 0; i < 100; i++) {\r
314         asm("nop");\r
315     }\r
316 \r
317  /* Step 8:  An Extended Mode Register set (EMRS1) cycle is issued to enable DLL. */\r
318     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_EXT_LMR_CMD;\r
319     *((uint8_t *)(pDdr + DDR2_BA0(0))) = 0;  /* The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1. */\r
320     /* An additional 200 cycles of clock are required for locking DLL */\r
321     for (i = 0; i < 10000; i++) {\r
322         asm("nop");\r
323     }\r
324 \r
325 /* Step 9:  Program DLL field into the Configuration Register.*/\r
326     MPDDRC->MPDDRC_CR |= MPDDRC_CR_DLL_RESET_ENABLED;\r
327 \r
328 /* Step 10: A Mode Register set (MRS) cycle is issued to reset DLL. */\r
329     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_LMR_CMD;\r
330     *(pDdr) = 0;  /* The write address must be chosen so that BA[1:0] bits are set to 0. */\r
331     /* wait 2 cycles min */\r
332     for (i = 0; i < 100; i++) {\r
333         asm("nop");\r
334     }\r
335  \r
336 /* Step 11: An all banks precharge command is issued to the DDR2-SDRAM. */\r
337     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_PRCGALL_CMD;\r
338     *(pDdr) = 0;  /* Perform a write access to any DDR2-SDRAM address to acknowledge this command */\r
339     /* wait 2 cycles min */\r
340     for (i = 0; i < 100; i++) {\r
341         asm("nop");\r
342     }\r
343 \r
344 /* Step 12: Two auto-refresh (CBR) cycles are provided. Program the auto refresh command (CBR) into the Mode Register. */\r
345     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_RFSH_CMD;\r
346     *(pDdr) = 0;  /* Perform a write access to any DDR2-SDRAM address to acknowledge this command */\r
347     /* wait 2 cycles min */\r
348     for (i = 0; i < 100; i++) {\r
349         asm("nop");\r
350     }\r
351     /* Configure 2nd CBR. */\r
352     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_RFSH_CMD;\r
353     *(pDdr) = 0;  /* Perform a write access to any DDR2-SDRAM address to acknowledge this command */\r
354     /* wait 2 cycles min */\r
355     for (i = 0; i < 100; i++) {\r
356         asm("nop");\r
357     }\r
358 \r
359 /* Step 13: Program DLL field into the Configuration Register to low(Disable DLL reset). */\r
360     MPDDRC->MPDDRC_CR   &= ~MPDDRC_CR_DLL_RESET_ENABLED;  \r
361 \r
362 /* Step 14: A Mode Register set (MRS) cycle is issued to program the parameters of the DDR2-SDRAM devices. */\r
363     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_LMR_CMD;\r
364     *(pDdr) = 0;  /* The write address must be chosen so that BA[1:0] are set to 0. */\r
365     /* wait 2 cycles min */\r
366     for (i = 0; i < 100; i++) {\r
367         asm("nop");\r
368     }\r
369 \r
370 /* Step 15: Program OCD field into the Configuration Register to high (OCD calibration default). */\r
371     MPDDRC->MPDDRC_CR   |= MPDDRC_CR_OCD_DDR2_DEFAULT_CALIB;\r
372 \r
373 /* Step 16: An Extended Mode Register set (EMRS1) cycle is issued to OCD default value. */\r
374     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_EXT_LMR_CMD;\r
375     *((uint8_t *)(pDdr + DDR2_BA0(0))) = 0;  /* The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.*/\r
376     /* wait 2 cycles min */\r
377     for (i = 0; i < 100; i++) {\r
378         asm("nop");\r
379     }\r
380 \r
381 /* Step 17: Program OCD field into the Configuration Register to low (OCD calibration mode exit). */\r
382    MPDDRC->MPDDRC_CR   &= ~(MPDDRC_CR_OCD_DDR2_DEFAULT_CALIB);\r
383 \r
384 /* Step 18: An Extended Mode Register set (EMRS1) cycle is issued to enable OCD exit.*/\r
385     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_EXT_LMR_CMD;\r
386     *((uint8_t *)(pDdr + DDR2_BA0(0))) = 0;  /* The write address must be chosen so that BA[1] is set to 0 and BA[0] is set to 1.*/\r
387     /* wait 2 cycles min */\r
388     for (i = 0; i < 100; i++) {\r
389         asm("nop");\r
390     }\r
391 \r
392 /* Step 19,20: A mode Normal command is provided. Program the Normal mode into Mode Register. */\r
393     MPDDRC->MPDDRC_MR = MPDDRC_MR_MODE_NORMAL_CMD;\r
394     *(pDdr) = 0;\r
395 \r
396 /* Step 21: Write the refresh rate into the count field in the Refresh Timer register. The DDR2-SDRAM device requires a refresh every 15.625 ¦Ìs or 7.81 ¦Ìs. \r
397    With a 100MHz frequency, the refresh timer count register must to be set with (15.625 /100 MHz) = 1562 i.e. 0x061A or (7.81 /100MHz) = 781 i.e. 0x030d. */\r
398     /* For MT47H64M16HR, The refresh period is 64ms (commercial), This equates to an average\r
399        refresh rate of 7.8125¦Ìs (commercial), To ensure all rows of all banks are properly \r
400        refreshed, 8192 REFRESH commands must be issued every 64ms (commercial) */\r
401     /* ((64 x 10(^-3))/8192) x133 x (10^6) */\r
402     MPDDRC->MPDDRC_RTR = MPDDRC_RTR_COUNT(0x2b0); /* Set Refresh timer 7.8125 us*/\r
403     /* OK now we are ready to work on the DDRSDR */\r
404     /* wait for end of calibration */\r
405     for (i = 0; i < 500; i++) {\r
406         asm("    nop");\r
407     }\r
408 }\r
409 \r
410 /**\r
411  * \brief Configures the EBI for Sdram (LPSDR Micron MT48H8M16) access.\r
412  */\r
413 void BOARD_ConfigureSdram( void )\r
414 {\r
415 }\r
416 \r
417 /** \brief Configures the EBI for NandFlash access at 133Mhz.\r
418  */\r
419 void BOARD_ConfigureNandFlash( uint8_t busWidth )\r
420 {\r
421     PMC_EnablePeripheral(ID_HSMC);\r
422     matrix_configure_slave_nand();\r
423 \r
424    HSMC->HSMC_CS_NUMBER[3].HSMC_SETUP = 0\r
425                     | HSMC_SETUP_NWE_SETUP(2)\r
426                     | HSMC_SETUP_NCS_WR_SETUP(2)\r
427                     | HSMC_SETUP_NRD_SETUP(2)\r
428                     | HSMC_SETUP_NCS_RD_SETUP(2);\r
429 \r
430     HSMC->HSMC_CS_NUMBER[3].HSMC_PULSE = 0\r
431                     | HSMC_PULSE_NWE_PULSE(7)\r
432                     | HSMC_PULSE_NCS_WR_PULSE(7)\r
433                     | HSMC_PULSE_NRD_PULSE(7)\r
434                     | HSMC_PULSE_NCS_RD_PULSE(7);\r
435 \r
436     HSMC->HSMC_CS_NUMBER[3].HSMC_CYCLE = 0\r
437                     | HSMC_CYCLE_NWE_CYCLE(13)\r
438                     | HSMC_CYCLE_NRD_CYCLE(13);\r
439 \r
440     HSMC->HSMC_CS_NUMBER[3].HSMC_TIMINGS = HSMC_TIMINGS_TCLR(3)\r
441                                        | HSMC_TIMINGS_TADL(27)\r
442                                        | HSMC_TIMINGS_TAR(3)\r
443                                        | HSMC_TIMINGS_TRR(6)\r
444                                        | HSMC_TIMINGS_TWB(5)\r
445                                        | HSMC_TIMINGS_RBNSEL(3)\r
446                                        |(HSMC_TIMINGS_NFSEL);\r
447     HSMC->HSMC_CS_NUMBER[3].HSMC_MODE = HSMC_MODE_READ_MODE |\r
448                                      HSMC_MODE_WRITE_MODE |\r
449                                      ((busWidth == 8 )? HSMC_MODE_DBW_BIT_8 :HSMC_MODE_DBW_BIT_16) |\r
450                                       HSMC_MODE_TDF_CYCLES(1);    \r
451 }\r
452 \r
453 \r
454 void BOARD_ConfigureNorFlash( uint8_t busWidth )\r
455 {\r
456     uint32_t dbw;\r
457     PMC_EnablePeripheral(ID_HSMC);\r
458     if (busWidth == 8) \r
459     {\r
460         dbw = HSMC_MODE_DBW_BIT_8;\r
461     }\r
462     else {\r
463         dbw = HSMC_MODE_DBW_BIT_16;\r
464     }\r
465     /* Configure SMC, NCS0 is assigned to a norflash */\r
466     HSMC->HSMC_CS_NUMBER[0].HSMC_SETUP = 0x00020001;\r
467     HSMC->HSMC_CS_NUMBER[0].HSMC_PULSE = 0x0B0B0A0A;\r
468     HSMC->HSMC_CS_NUMBER[0].HSMC_CYCLE = 0x000E000B;\r
469     HSMC->HSMC_CS_NUMBER[0].HSMC_TIMINGS = 0x00000000;\r
470     HSMC->HSMC_CS_NUMBER[0].HSMC_MODE  = HSMC_MODE_WRITE_MODE\r
471                                     | HSMC_MODE_READ_MODE\r
472                                     | dbw\r
473                                     | HSMC_MODE_EXNW_MODE_DISABLED\r
474                                     | HSMC_MODE_TDF_CYCLES(1);\r
475 \r
476 }\r