summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/m68k/genmcf548x/startup/init548x.c
blob: 1221df4041b4e6d34f4be003b891148e44c7f9b4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*===============================================================*\
| Project: RTEMS generic mcf548x BSP                              |
+-----------------------------------------------------------------+
| File: init548x.c                                                |
+-----------------------------------------------------------------+
| The file contains the c part of MCF548x init code               |
+-----------------------------------------------------------------+
|                    Copyright (c) 2007                           |
|                    Embedded Brains GmbH                         |
|                    Obere Lagerstr. 30                           |
|                    D-82178 Puchheim                             |
|                    Germany                                      |
|                    rtems@embedded-brains.de                     |
+-----------------------------------------------------------------+
|                                                                 |
| Parts of the code has been derived from the "dBUG source code"  |
| package Freescale is providing for M548X EVBs. The usage of     |
| the modified or unmodified code and it's integration into the   |
| generic mcf548x BSP has been done according to the Freescale    |
| license terms.                                                  |
|                                                                 |
| The Freescale license terms can be reviewed in the file         |
|                                                                 |
|    Freescale_license.txt                                        |
|                                                                 |
+-----------------------------------------------------------------+
|                                                                 |
| The generic mcf548x BSP has been developed on the basic         |
| structures and modules of the av5282 BSP.                       |
|                                                                 |
+-----------------------------------------------------------------+
|                                                                 |
| The license and distribution terms for this file may be         |
| found in the file LICENSE in this distribution or at            |
|                                                                 |
| http://www.rtems.com/license/LICENSE.                           |
|                                                                 |
+-----------------------------------------------------------------+
|                                                                 |
|   date                      history                        ID   |
| ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ |
| 12.11.07                    1.0                            ras  |
|                                                                 |
\*===============================================================*/

#include <bsp.h>

#include <string.h>

#include <bsp/linker-symbols.h>

#if defined(HAS_LOW_LEVEL_INIT)
#define SYSTEM_PERIOD			10  	/* system bus period in ns */

/* SDRAM Timing Parameters */
#define SDRAM_TWR		    	2  		/* in clocks */
#define SDRAM_CASL		    	2.5 	/* in clocks */
#define SDRAM_TRCD		    	20		/* in ns */
#define SDRAM_TRP		    	20		/* in ns */
#define SDRAM_TRFC		    	75		/* in ns */
#define SDRAM_TREFI		    	7800	/* in ns */
#endif /* defined(HAS_LOW_LEVEL_INIT) */

extern uint8_t _DataRom[];
extern uint8_t _DataRam[];
extern uint8_t _DataEnd[];
extern uint8_t _BssStart[];
extern uint8_t _BssEnd[];
extern uint8_t _BootFlashBase[];
extern uint8_t _CodeFlashBase[];
extern uint8_t RamBase[];

void gpio_init(void);
void fbcs_init(void);
void sdramc_init(void);
void mcf548x_init(void);


void mcf548x_init(void)
{
    size_t i;

#if defined(HAS_LOW_LEVEL_INIT)
    /* set XLB arbiter timeouts */
    MCF548X_XLB_ADRTO = 0x00000100;
    MCF548X_XLB_DATTO = 0x00000100;
    MCF548X_XLB_BUSTO = 0x00000100;
#endif

    gpio_init();
#if defined(HAS_LOW_LEVEL_INIT)
    fbcs_init();
    sdramc_init();
#endif /* defined(HAS_LOW_LEVEL_INIT) */

    /* Copy the vector table to RAM if necessary */
    if (bsp_vector0_size == bsp_vector1_size) {
      memcpy(bsp_vector1_begin, bsp_vector0_begin, (size_t) bsp_vector1_size);
      m68k_set_vbr((uint32_t)bsp_vector1_begin);
    }

    /* Move initialized data from ROM to RAM. */
    if (bsp_section_data_begin != bsp_section_data_load_begin) {
      memcpy(
        bsp_section_data_begin,
        bsp_section_data_load_begin,
        (size_t) bsp_section_data_size
      );
    }

    /* Zero uninitialized data */
    memset(bsp_section_bss_begin, 0, (size_t) bsp_section_bss_size);

    for (i = 8; i < RTEMS_ARRAY_SIZE(mcf548x_intc_icr_init_values); ++i) {
      volatile uint8_t *icr = &MCF548X_INTC_ICR0;

      icr[i] = mcf548x_intc_icr_init_values[i];
    }
}
/********************************************************************/
#if defined(HAS_LOW_LEVEL_INIT)
void
fbcs_init (void)
{
#ifdef M5484FIREENGINE

volatile uint32_t cscr, clk_ratio, fb_period, ws;

/* boot flash already valid ? */
if(!(MCF548X_FBCS_CSMR0 & MCF548X_FBCS_CSMR_V))
  {

    /*
     * Boot Flash
     */
    MCF548X_FBCS_CSAR0 = MCF548X_FBCS_CSAR_BA((uint32_t)(_BootFlashBase));

    cscr = (0
            | MCF548X_FBCS_CSCR_ASET(1)
            | MCF548X_FBCS_CSCR_WRAH(0)
            | MCF548X_FBCS_CSCR_RDAH(0)
            | MCF548X_FBCS_CSCR_AA
            | MCF548X_FBCS_CSCR_PS_16);

    /*
     * Determine the necessary wait states based on the defined system
     * period (XLB clock period) and the CLKIN to XLB ratio.
     * The boot flash has a max access time of 110ns.
     */
    clk_ratio = (MCF548X_PCI_PCIGSCR >> 24) & 0x7;
    fb_period = SYSTEM_PERIOD * clk_ratio;
    ws = 110 / fb_period;

    MCF548X_FBCS_CSCR0 = cscr | MCF548X_FBCS_CSCR_WS(ws);
    MCF548X_FBCS_CSMR0 = (0
                          | MCF548X_FBCS_CSMR_BAM_2M
                          | MCF548X_FBCS_CSMR_V);

  }

/* code flash already valid ? */
if(!(MCF548X_FBCS_CSMR1 & MCF548X_FBCS_CSMR_V))
  {

    /*
     * Code Flash
     */
    MCF548X_FBCS_CSAR1 = MCF548X_FBCS_CSAR_BA((uint32_t)(_CodeFlashBase));

    /*
     * Determine the necessary wait states based on the defined system
     * period (XLB clock period) and the CLKIN to XLB ratio.
     * The user/code flash has a max access time of 120ns.
     */
    ws = 120 / fb_period;
    MCF548X_FBCS_CSCR1 = cscr | MCF548X_FBCS_CSCR_WS(ws);
    MCF548X_FBCS_CSMR1 = (0
                          | MCF548X_FBCS_CSMR_BAM_16M
                          | MCF548X_FBCS_CSMR_V);
    }

#endif
}
#endif /* defined(HAS_LOW_LEVEL_INIT) */

/********************************************************************/
#if defined(HAS_LOW_LEVEL_INIT)
void
sdramc_init (void)
{

	/*
	 * Check to see if the SDRAM has already been initialized
	 * by a run control tool
	 */
	if (!(MCF548X_SDRAMC_SDCR & MCF548X_SDRAMC_SDCR_REF))
	{
		/*
         * Basic configuration and initialization
         */
		MCF548X_SDRAMC_SDRAMDS = (0
			| MCF548X_SDRAMC_SDRAMDS_SB_E(MCF548X_SDRAMC_SDRAMDS_DRIVE_8MA)
			| MCF548X_SDRAMC_SDRAMDS_SB_C(MCF548X_SDRAMC_SDRAMDS_DRIVE_8MA)
			| MCF548X_SDRAMC_SDRAMDS_SB_A(MCF548X_SDRAMC_SDRAMDS_DRIVE_8MA)
			| MCF548X_SDRAMC_SDRAMDS_SB_S(MCF548X_SDRAMC_SDRAMDS_DRIVE_8MA)
			| MCF548X_SDRAMC_SDRAMDS_SB_D(MCF548X_SDRAMC_SDRAMDS_DRIVE_8MA)
            );
        MCF548X_SDRAMC_CS0CFG = (0
            | MCF548X_SDRAMC_CSnCFG_CSBA((uint32_t)(RamBase))
            | MCF548X_SDRAMC_CSnCFG_CSSZ(MCF548X_SDRAMC_CSnCFG_CSSZ_64MBYTE)
            );
        MCF548X_SDRAMC_SDCFG1 = (0
			| MCF548X_SDRAMC_SDCFG1_SRD2RW(7)
			| MCF548X_SDRAMC_SDCFG1_SWT2RD(SDRAM_TWR + 1)
			| MCF548X_SDRAMC_SDCFG1_RDLAT((int)((SDRAM_CASL*2) + 2))
			| MCF548X_SDRAMC_SDCFG1_ACT2RW((int)(((SDRAM_TRCD/SYSTEM_PERIOD) - 1) + 0.5))
			| MCF548X_SDRAMC_SDCFG1_PRE2ACT((int)(((SDRAM_TRP/SYSTEM_PERIOD) - 1) + 0.5))
			| MCF548X_SDRAMC_SDCFG1_REF2ACT((int)(((SDRAM_TRFC/SYSTEM_PERIOD) - 1) + 0.5))
			| MCF548X_SDRAMC_SDCFG1_WTLAT(3)
            );
		MCF548X_SDRAMC_SDCFG2 = (0
			| MCF548X_SDRAMC_SDCFG2_BRD2PRE(4)
			| MCF548X_SDRAMC_SDCFG2_BWT2RW(6)
			| MCF548X_SDRAMC_SDCFG2_BRD2WT(7)
			| MCF548X_SDRAMC_SDCFG2_BL(7)
            );

		/*
         * Precharge and enable write to SDMR
         */
        MCF548X_SDRAMC_SDCR = (0
			| MCF548X_SDRAMC_SDCR_MODE_EN
			| MCF548X_SDRAMC_SDCR_CKE
			| MCF548X_SDRAMC_SDCR_DDR
			| MCF548X_SDRAMC_SDCR_MUX(1)
			| MCF548X_SDRAMC_SDCR_RCNT((int)(((SDRAM_TREFI/(SYSTEM_PERIOD*64)) - 1) + 0.5))
			| MCF548X_SDRAMC_SDCR_IPALL
            );

		/*
         * Write extended mode register
         */
		MCF548X_SDRAMC_SDMR = (0
			| MCF548X_SDRAMC_SDMR_BNKAD_LEMR
			| MCF548X_SDRAMC_SDMR_AD(0x0)
			| MCF548X_SDRAMC_SDMR_CMD
            );

		/*
         * Write mode register and reset DLL
         */
		MCF548X_SDRAMC_SDMR = (0
			| MCF548X_SDRAMC_SDMR_BNKAD_LMR
			| MCF548X_SDRAMC_SDMR_AD(0x163)
			| MCF548X_SDRAMC_SDMR_CMD
            );

		/*
         * Execute a PALL command
         */
		MCF548X_SDRAMC_SDCR |=MCF548X_SDRAMC_SDCR_IPALL;

		/*
         * Perform two REF cycles
         */
		MCF548X_SDRAMC_SDCR |= MCF548X_SDRAMC_SDCR_IREF;
		MCF548X_SDRAMC_SDCR |= MCF548X_SDRAMC_SDCR_IREF;

		/*
         * Write mode register and clear reset DLL
         */
		MCF548X_SDRAMC_SDMR = (0
			| MCF548X_SDRAMC_SDMR_BNKAD_LMR
			| MCF548X_SDRAMC_SDMR_AD(0x063)
			| MCF548X_SDRAMC_SDMR_CMD
            );

		/*
         * Enable auto refresh and lock SDMR
         */
		MCF548X_SDRAMC_SDCR &= ~MCF548X_SDRAMC_SDCR_MODE_EN;
        MCF548X_SDRAMC_SDCR |= (0
			| MCF548X_SDRAMC_SDCR_REF
			| MCF548X_SDRAMC_SDCR_DQS_OE(0xF)
            );
    }

}
#endif /* defined(HAS_LOW_LEVEL_INIT) */

/********************************************************************/
void
gpio_init(void)
{

#ifdef M5484FIREENGINE

    /*
     * Enable Ethernet signals so that, if a cable is plugged into
     * the ports, the lines won't be floating and potentially cause
     * erroneous transmissions
     */
    MCF548X_GPIO_PAR_FECI2CIRQ = (0

        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E1MDC_EMDC
        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E1MDIO_EMDIO
        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E1MII
        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E17

        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E0MDC
        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E0MDIO
        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E0MII
        | MCF548X_GPIO_PAR_FECI2CIRQ_PAR_E07
        );

#endif
    /* 
     * make sure the "edge port" has all interrupts disabled
     */
    MCF548X_EPORT_EPIER = 0;
}