sam3 nand: remove empty file
[bertos.git] / boards / sam3x-ek / hw / hw_sdram.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 2011 Develer S.r.l. (http://www.develer.com/)
30  *
31  * -->
32  *
33  * \brief MT48LC16M16A2 SDRAM initialization for Atmel SAM3X-EK board.
34  *
35  * \author Stefano Fedrigo <aleph@develer.com>
36  *
37  * SDRAMC register settings and comments are from Atmel Softpack, see licence below:
38  *
39  * ----------------------------------------------------------------------------
40  *         ATMEL Microcontroller Software Support
41  * ----------------------------------------------------------------------------
42  * Copyright (c) 2010, Atmel Corporation
43  *
44  * All rights reserved.
45  *
46  * Redistribution and use in source and binary forms, with or without
47  * modification, are permitted provided that the following conditions are met:
48  *
49  * - Redistributions of source code must retain the above copyright notice,
50  * this list of conditions and the disclaimer below.
51  *
52  * Atmel's name may not be used to endorse or promote products derived from
53  * this software without specific prior written permission.
54  *
55  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR
56  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
57  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
58  * DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR ANY DIRECT, INDIRECT,
59  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
60  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
61  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
62  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
63  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
64  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
65  * ----------------------------------------------------------------------------
66  */
67
68 #ifndef HW_SAM3X_SDRAM_H
69 #define HW_SAM3X_SDRAM_H
70
71 #include "cfg/macros.h"
72 #include <io/sam3.h>
73
74 /*
75  * SDRAM properties
76  */
77 #define SDRAM_BASE      0x70000000
78 #define SDRAM_SIZE      (32*1024*1024)
79 #define SDRAM_BUSWIDTH  16
80 #define SDRAM_CLK       CPU_FREQ
81
82 /*
83  * IO pins
84  */
85 // port C, peripheral A
86 #define PIO_SDRAM_DATA     (0xFFFF << 2)  // Data bus, 2-17
87 #define PIO_SDRAM_SDA0_A7  (0xFF << 23)   // Address bus, 23-30
88 #define PIO_SDRAM_NBS0     BV(21)         // Data mask enable 0
89 // port D, peripheral A
90 #define PIO_SDRAM_SDCKE    BV(13)         // Clock enable
91 #define PIO_SDRAM_SDCS     BV(12)         // Chip select
92 #define PIO_SDRAM_RAS      BV(15)         // Row
93 #define PIO_SDRAM_CAS      BV(16)         // Column
94 #define PIO_SDRAM_BA0      BV(6)          // Bank select 0
95 #define PIO_SDRAM_BA1      BV(7)          // Bank select 1
96 #define PIO_SDRAM_SDWE     BV(14)         // Write enable
97 #define PIO_SDRAM_NBS1     BV(10)         // Data mask enable 1
98 #define PIO_SDRAM_SDA8     BV(22)
99 #define PIO_SDRAM_SDA9     BV(23)
100 #define PIO_SDRAM_SDA10    BV(11)
101 #define PIO_SDRAM_SDA11    BV(25)
102 #define PIO_SDRAM_SDA12    BV(4)
103 // port D, PIO output
104 #define PIO_SDRAM_EN       BV(18)         // Enable
105
106 #define SDRAM_PORTC_PERIPH  (PIO_SDRAM_DATA | PIO_SDRAM_SDA0_A7 | PIO_SDRAM_NBS0)
107 #define SDRAM_PORTD_PERIPH  (PIO_SDRAM_SDCKE | PIO_SDRAM_SDCS  | PIO_SDRAM_RAS   | \
108                              PIO_SDRAM_CAS   | PIO_SDRAM_BA0   | PIO_SDRAM_BA1   | \
109                              PIO_SDRAM_SDWE  | PIO_SDRAM_NBS1  | PIO_SDRAM_SDA8  | \
110                              PIO_SDRAM_SDA9  | PIO_SDRAM_SDA10 | PIO_SDRAM_SDA11 | \
111                              PIO_SDRAM_SDA12)
112 #define SDRAM_PORTD_OUTPUT  PIO_SDRAM_EN
113
114
115 INLINE void sdram_init(void)
116 {
117         HWREG(PIOC_BASE + PIO_PDR_OFF) = SDRAM_PORTC_PERIPH;
118         HWREG(PIOC_BASE + PIO_PUER_OFF) = SDRAM_PORTC_PERIPH;
119         PIO_PERIPH_SEL(PIOC_BASE, SDRAM_PORTC_PERIPH, PIO_PERIPH_A);
120
121         HWREG(PIOD_BASE + PIO_PDR_OFF) = SDRAM_PORTD_PERIPH;
122         HWREG(PIOD_BASE + PIO_PUER_OFF) = SDRAM_PORTD_PERIPH;
123         PIO_PERIPH_SEL(PIOD_BASE, SDRAM_PORTD_PERIPH, PIO_PERIPH_A);
124
125         HWREG(PIOD_BASE + PIO_PER_OFF) = SDRAM_PORTD_OUTPUT;
126         HWREG(PIOD_BASE + PIO_OER_OFF) = SDRAM_PORTD_OUTPUT;
127         HWREG(PIOD_BASE + PIO_SODR_OFF) = SDRAM_PORTD_OUTPUT;
128
129         pmc_periphEnable(SMC_SDRAMC_ID);
130
131         // SDRAM device configuration
132         SDRAMC_CR =
133                 SDRAMC_CR_NC_COL9 | SDRAMC_CR_NR_ROW13 | SDRAMC_CR_NB_BANK4 |
134                 SDRAMC_CR_CAS_LATENCY2 | SDRAMC_CR_DBW |
135                 SDRAMC_CR_TWR(2) | SDRAMC_CR_TRC_TRFC(9) | SDRAMC_CR_TRP(3) |
136                 SDRAMC_CR_TRCD(3) | SDRAMC_CR_TRAS(6) | SDRAMC_CR_TXSR(10);
137
138         SDRAMC_LPR = 0;
139         SDRAMC_MDR = SDRAMC_MDR_MD_SDRAM;
140
141         /*
142          * A minimum pause of 200 us needed before any signal toggle
143          * (6 core cycles per iteration).
144          */
145         timer_delay(1);
146
147         /*
148          * A NOP command is issued to the SDR-SDRAM. Program NOP command into
149          * Mode Register, the application must set Mode to 1 in the Mode Register.
150          * Perform a write access to any SDR-SDRAM address to acknowledge this command.
151          * Now the clock which drives SDR-SDRAM device is enabled.
152          */
153         SDRAMC_MR = SDRAMC_MR_MODE_NOP;
154         *(uint32_t *)SDRAM_BASE = 0;
155
156         /*
157          * An all banks precharge command is issued to the SDR-SDRAM. Program all
158          * banks precharge command into Mode Register, the application must set Mode to
159          * 2 in the Mode Register . Perform a write access to any SDRSDRAM address to
160          * acknowledge this command.
161          */
162         SDRAMC_MR = SDRAMC_MR_MODE_ALLBANKS_PRECHARGE;
163         *(uint32_t *)SDRAM_BASE = 0;
164
165         /*
166          * Eight auto-refresh (CBR) cycles are provided. Program the auto refresh
167          * command (CBR) into Mode Register, the application must set Mode to 4 in
168          * the Mode Register. Once in the idle state, two AUTO REFRESH cycles must
169          * be performed.
170          */
171         SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
172         *(uint32_t *)SDRAM_BASE = 0;
173
174         SDRAMC_MR = SDRAMC_MR_MODE_AUTO_REFRESH;
175         *(uint32_t *)SDRAM_BASE = 0;
176
177         /*
178          * A Mode Register set (MRS) cycle is issued to program the parameters of
179          * the SDRAM devices, in particular CAS latency and burst length.
180          */
181         SDRAMC_MR = SDRAMC_MR_MODE_LOAD_MODEREG;
182         *(uint32_t *)(SDRAM_BASE + 9) = 0xc001babe;
183
184         /*
185          * For low-power SDR-SDRAM initialization, an Extended Mode Register set
186          * (EMRS) cycle is issued to program the SDR-SDRAM parameters (TCSR, PASR, DS).
187          * The write address must be chosen so that BA[1] is set to 1 and BA[0] is set
188          * to 0: BK1 is at bit 24, 1+9+13+1.
189          */
190         SDRAMC_MR = SDRAMC_MR_MODE_EXT_LOAD_MODEREG;
191         *(uint32_t *)(SDRAM_BASE + (1 << 24)) = 0;
192
193         /*
194          * The application must go into Normal Mode, setting Mode to 0 in the Mode
195          * Register and perform a write access at any location in the SDRAM to
196          * acknowledge this command.
197          */
198         SDRAMC_MR = SDRAMC_MR_MODE_NORMAL;
199         *(uint32_t *)SDRAM_BASE = 0;
200
201         /*
202          * Write the refresh rate into the count field in the SDRAMC Refresh
203          * Timer register. Set Refresh timer 15.625 us
204          */
205         SDRAMC_TR = SDRAMC_TR_COUNT(SDRAM_CLK / 1000 * 15625 / 1000000) ;
206 }
207
208 #endif /* HW_SAM3X_SDRAM_H */