source: rtems/c/src/lib/libbsp/arm/lpc24xx/startup/bspstarthooks.c @ d74ed4a

4.115
Last change on this file since d74ed4a was d74ed4a, checked in by Sebastian Huber <sebastian.huber@…>, on 05/19/11 at 12:30:00

2011-05-19 Sebastian Huber <sebastian.huber@…>

  • i2c/i2c-config.c: New file.
  • include/lcd.h: Removed EMC definitions.
  • misc/dma.c: Fixed initialization.
  • include/i2c.h, include/io.h, include/lpc-ethernet-config.h, include/lpc24xx.h, console/console-config.c, i2c/i2c.c, misc/io.c, misc/lcd.c, startup/bspstart.c, startup/bspstarthooks.c: New pin configuration API.
  • Makefile.am, preinstall.am: Update.
  • Property mode set to 100644
File size: 12.3 KB
Line 
1/**
2 * @file
3 *
4 * @ingroup lpc24xx
5 *
6 * @brief Startup code.
7 */
8
9/*
10 * Copyright (c) 2008-2011 embedded brains GmbH.  All rights reserved.
11 *
12 *  embedded brains GmbH
13 *  Obere Lagerstr. 30
14 *  82178 Puchheim
15 *  Germany
16 *  <rtems@embedded-brains.de>
17 *
18 * The license and distribution terms for this file may be
19 * found in the file LICENSE in this distribution or at
20 * http://www.rtems.com/license/LICENSE.
21 */
22
23#include <stdbool.h>
24
25#include <bspopts.h>
26#include <bsp/start.h>
27#include <bsp/linker-symbols.h>
28#include <bsp/lpc24xx.h>
29#include <bsp/lpc-emc.h>
30
31#if defined(LPC24XX_EMC_MICRON) || defined(LPC24XX_EMC_NUMONYX)
32  #define LPC24XX_EMC_INIT
33#endif
34
35static volatile lpc_emc *const emc = (lpc_emc *) EMC_BASE_ADDR;
36
37typedef struct {
38  uint32_t refresh;
39  uint32_t readconfig;
40  uint32_t trp;
41  uint32_t tras;
42  uint32_t tsrex;
43  uint32_t tapr;
44  uint32_t tdal;
45  uint32_t twr;
46  uint32_t trc;
47  uint32_t trfc;
48  uint32_t txsr;
49  uint32_t trrd;
50  uint32_t tmrd;
51} lpc24xx_emc_dynamic_config;
52
53typedef struct {
54  uint32_t config;
55  uint32_t rascas;
56  uint32_t mode;
57} lpc24xx_emc_dynamic_chip_config;
58
59typedef struct {
60  uint32_t config;
61  uint32_t waitwen;
62  uint32_t waitoen;
63  uint32_t waitrd;
64  uint32_t waitpage;
65  uint32_t waitwr;
66  uint32_t waitrun;
67} lpc24xx_emc_static_chip_config;
68
69#ifdef LPC24XX_EMC_MICRON
70  static void BSP_START_TEXT_SECTION lpc24xx_ram_test_32(void)
71  {
72    #ifdef LPC24XX_EMC_TEST
73      int *begin = (int *) 0xa0000000;
74      const int *end = (const int *) 0xa0800000;
75      int *out = begin;
76
77      while (out != end) {
78        *out = (int) out;
79        ++out;
80      }
81
82      out = begin;
83      while (out != end) {
84        if (*out != (int) out) {
85          while (true) {
86            /* Do nothing */
87          }
88        }
89        ++out;
90      }
91    #endif
92  }
93
94  static void BSP_START_TEXT_SECTION lpc24xx_cpu_delay(unsigned ticks)
95  {
96    unsigned i = 0;
97
98    /* One loop execution needs four instructions */
99    ticks /= 4;
100
101    for (i = 0; i <= ticks; ++i) {
102      __asm__ volatile ("nop");
103    }
104  }
105
106  static void BSP_START_TEXT_SECTION lpc24xx_udelay(unsigned us)
107  {
108    lpc24xx_cpu_delay(us * (LPC24XX_CCLK / 1000000));
109  }
110#endif
111
112static void BSP_START_TEXT_SECTION lpc24xx_init_emc_pinsel(void)
113{
114  #ifdef LPC24XX_EMC_INIT
115    static const BSP_START_DATA_SECTION uint32_t pinsel_5_9 [5] = {
116      0x05010115,
117      0x55555555,
118      0x0,
119      0x55555555,
120      0x40050155
121    };
122
123    bsp_start_memcpy(
124      (int *) &PINSEL5,
125      (const int *) &pinsel_5_9,
126      sizeof(pinsel_5_9)
127    );
128  #endif
129}
130
131static void BSP_START_TEXT_SECTION lpc24xx_init_emc_static(void)
132{
133  #ifdef LPC24XX_EMC_NUMONYX
134    /*
135     * Static Memory 1: Numonyx M29W160EB
136     *
137     * 1 clock cycle = 1/72MHz = 13.9ns
138     */
139    static const BSP_START_DATA_SECTION lpc24xx_emc_static_chip_config chip_config = {
140      /*
141       * 16 bit, page mode disabled, active LOW chip select, extended wait
142       * disabled, writes not protected, byte lane state LOW/LOW (!).
143       */
144      .config = 0x81,
145
146      /* 1 clock cycles delay from the chip select 1 to the write enable */
147      .waitwen = 0,
148
149      /*
150       * 0 clock cycles delay from the chip select 1 or address change
151       * (whichever is later) to the output enable
152       */
153      .waitoen = 0,
154
155      /* 7 clock cycles delay from the chip select 1 to the read access */
156      .waitrd = 0x6,
157
158      /*
159       * 32 clock cycles delay for asynchronous page mode sequential accesses
160       */
161      .waitpage = 0x1f,
162
163      /* 5 clock cycles delay from the chip select 1 to the write access */
164      .waitwr = 0x3,
165
166      /* 16 bus turnaround cycles */
167      .waitrun = 0xf
168    };
169    lpc24xx_emc_static_chip_config chip_config_on_stack;
170
171    bsp_start_memcpy(
172      (int *) &chip_config_on_stack,
173      (const int *) &chip_config,
174      sizeof(chip_config_on_stack)
175    );
176    bsp_start_memcpy(
177      (int *) EMC_STA_BASE_1,
178      (const int *) &chip_config_on_stack,
179      sizeof(chip_config_on_stack)
180    );
181  #endif
182}
183
184static void BSP_START_TEXT_SECTION lpc24xx_init_emc_memory_map(void)
185{
186  #ifdef LPC24XX_EMC_INIT
187    /* Use normal memory map */
188    EMC_CTRL &= ~0x2U;
189  #endif
190}
191
192static void BSP_START_TEXT_SECTION lpc24xx_init_emc_dynamic(void)
193{
194  #ifdef LPC24XX_EMC_MICRON
195    /* Dynamic Memory 0: Micron M T48LC 4M16 A2 P 75 IT */
196
197    static const BSP_START_DATA_SECTION lpc24xx_emc_dynamic_config dynamic_config = {
198      /* Auto-refresh command every 15.6 us */
199      .refresh = 0x46,
200
201      /* Use command delayed strategy */
202      .readconfig = 1,
203
204      /* Precharge command period 20 ns */
205      .trp = 1,
206
207      /* Active to precharge command period 44 ns */
208      .tras = 3,
209
210      /* FIXME */
211      .tsrex = 5,
212
213      /* FIXME */
214      .tapr = 2,
215
216      /* Data-in to active command period tWR + tRP */
217      .tdal = 4,
218
219      /* Write recovery time 15 ns */
220      .twr = 1,
221
222      /* Active to active command period 66 ns */
223      .trc = 4,
224
225      /* Auto refresh period 66 ns */
226      .trfc = 4,
227
228      /* Exit self refresh to active command period 75 ns */
229      .txsr = 5,
230
231      /* Active bank a to active bank b command period 15 ns */
232      .trrd = 1,
233
234      /* Load mode register to active or refresh command period 2 tCK */
235      .tmrd = 1,
236    };
237    static const BSP_START_DATA_SECTION lpc24xx_emc_dynamic_chip_config chip_config = {
238      /*
239       * Use SDRAM, 0 0 001 01 address mapping, disabled buffer, unprotected writes
240       */
241      .config = 0x280,
242
243      .rascas = EMC_DYN_RASCAS_RAS(2) | EMC_DYN_RASCAS_CAS(2, 0),
244      .mode = 0xa0000000 | (0x23 << (1 + 2 + 8))
245    };
246
247    volatile lpc_emc_dynamic *chip = &emc->dynamic [0];
248    uint32_t dynamiccontrol = EMC_DYN_CTRL_CE | EMC_DYN_CTRL_CS;
249
250    /* Check if we need to initialize it */
251    if ((chip->config & EMC_DYN_CFG_B) == 0) {
252      /*
253       * The buffer enable bit is not set.  Now we assume that the controller
254       * is not properly initialized.
255       */
256
257      /* Global dynamic settings */
258      emc->dynamicreadconfig = dynamic_config.readconfig;
259      emc->dynamictrp = dynamic_config.trp;
260      emc->dynamictras = dynamic_config.tras;
261      emc->dynamictsrex = dynamic_config.tsrex;
262      emc->dynamictapr = dynamic_config.tapr;
263      emc->dynamictdal = dynamic_config.tdal;
264      emc->dynamictwr = dynamic_config.twr;
265      emc->dynamictrc = dynamic_config.trc;
266      emc->dynamictrfc = dynamic_config.trfc;
267      emc->dynamictxsr = dynamic_config.txsr;
268      emc->dynamictrrd = dynamic_config.trrd;
269      emc->dynamictmrd = dynamic_config.tmrd;
270
271      /* Wait 100us after the power is applied and the clocks have stabilized */
272      lpc24xx_udelay(100);
273
274      /* NOP period, disable self-refresh */
275      emc->dynamiccontrol = dynamiccontrol | EMC_DYN_CTRL_I_NOP;
276      lpc24xx_udelay(200);
277
278      /* Precharge all */
279      emc->dynamiccontrol = dynamiccontrol | EMC_DYN_CTRL_I_PALL;
280
281      /*
282       * Perform several refresh cycles with a memory refresh every 16 AHB
283       * clock cycles.  Wait until eight SDRAM refresh cycles have occurred
284       * (128 AHB clock cycles).
285       */
286      emc->dynamicrefresh = 1;
287      lpc24xx_cpu_delay(128);
288
289      /* Set refresh period */
290      emc->dynamicrefresh = dynamic_config.refresh;
291
292      /* Operational values for the chip */
293      chip->rascas = chip_config.rascas;
294      chip->config = chip_config.config;
295
296      /* Mode */
297      emc->dynamiccontrol = dynamiccontrol | EMC_DYN_CTRL_I_MODE;
298      *((volatile uint32_t *) chip_config.mode);
299
300      /* Normal operation */
301      emc->dynamiccontrol = 0;
302
303      /* Enable buffer */
304      chip->config |= EMC_DYN_CFG_B;
305
306      /* Test RAM */
307      lpc24xx_ram_test_32();
308    }
309  #endif
310}
311
312static void BSP_START_TEXT_SECTION lpc24xx_pll_config(
313  uint32_t val
314)
315{
316  PLLCON = val;
317  PLLFEED = 0xaa;
318  PLLFEED = 0x55;
319}
320
321/**
322 * @brief Sets the Phase Locked Loop (PLL).
323 *
324 * All parameter values are the actual register field values.
325 *
326 * @param clksrc Selects the clock source for the PLL.
327 *
328 * @param nsel Selects PLL pre-divider value (sometimes named psel).
329 *
330 * @param msel Selects PLL multiplier value.
331 *
332 * @param cclksel Selects the divide value for creating the CPU clock (CCLK)
333 * from the PLL output.
334 */
335static void BSP_START_TEXT_SECTION lpc24xx_set_pll(
336  unsigned clksrc,
337  unsigned nsel,
338  unsigned msel,
339  unsigned cclksel
340)
341{
342  uint32_t pllstat = PLLSTAT;
343  uint32_t pllcfg = SET_PLLCFG_NSEL(0, nsel) | SET_PLLCFG_MSEL(0, msel);
344  uint32_t clksrcsel = SET_CLKSRCSEL_CLKSRC(0, clksrc);
345  uint32_t cclkcfg = SET_CCLKCFG_CCLKSEL(0, cclksel | 1);
346  bool pll_enabled = (pllstat & PLLSTAT_PLLE) != 0;
347
348  /* Disconnect PLL if necessary */
349  if ((pllstat & PLLSTAT_PLLC) != 0) {
350    if (pll_enabled) {
351      /* Check if we run already with the desired settings */
352      if (PLLCFG == pllcfg && CLKSRCSEL == clksrcsel && CCLKCFG == cclkcfg) {
353        /* Nothing to do */
354        return;
355      }
356      lpc24xx_pll_config(PLLCON_PLLE);
357    } else {
358      lpc24xx_pll_config(0);
359    }
360  }
361
362  /* Set CPU clock divider to a reasonable save value */
363  CCLKCFG = 0;
364
365  /* Disable PLL if necessary */
366  if (pll_enabled) {
367    lpc24xx_pll_config(0);
368  }
369
370  /* Select clock source */
371  CLKSRCSEL = clksrcsel;
372
373  /* Set PLL Configuration Register */
374  PLLCFG = pllcfg;
375
376  /* Enable PLL */
377  lpc24xx_pll_config(PLLCON_PLLE);
378
379  /* Wait for lock */
380  while ((PLLSTAT & PLLSTAT_PLOCK) == 0) {
381    /* Wait */
382  }
383
384  /* Set CPU clock divider and ensure that we have an odd value */
385  CCLKCFG = cclkcfg;
386
387  /* Connect PLL */
388  lpc24xx_pll_config(PLLCON_PLLE | PLLCON_PLLC);
389}
390
391static void BSP_START_TEXT_SECTION lpc24xx_init_pll(void)
392{
393  /* Enable main oscillator */
394  if ((SCS & 0x40) == 0) {
395    SCS |= 0x20;
396    while ((SCS & 0x40) == 0) {
397      /* Wait */
398    }
399  }
400
401  /* Set PLL */
402  #if LPC24XX_OSCILLATOR_MAIN == 12000000U
403    lpc24xx_set_pll(1, 0, 11, 3);
404  #elif LPC24XX_OSCILLATOR_MAIN == 3686400U
405    lpc24xx_set_pll(1, 0, 47, 5);
406  #else
407    #error "unexpected main oscillator frequency"
408  #endif
409}
410
411static void BSP_START_TEXT_SECTION lpc24xx_clear_bss(void)
412{
413  const int *end = (const int *) bsp_section_bss_end;
414  int *out = (int *) bsp_section_bss_begin;
415
416  /* Clear BSS */
417  while (out != end) {
418    *out = 0;
419    ++out;
420  }
421}
422
423void BSP_START_TEXT_SECTION bsp_start_hook_0(void)
424{
425  lpc24xx_init_pll();
426  lpc24xx_init_emc_pinsel();
427  lpc24xx_init_emc_static();
428}
429
430void BSP_START_TEXT_SECTION bsp_start_hook_1(void)
431{
432  /* Re-map interrupt vectors to internal RAM */
433  MEMMAP = SET_MEMMAP_MAP(MEMMAP, 2);
434
435  /* Fully enable memory accelerator module functions (MAM) */
436  MAMCR = 0;
437  #if LPC24XX_CCLK <= 20000000U
438    MAMTIM = 0x1;
439  #elif LPC24XX_CCLK <= 40000000U
440    MAMTIM = 0x2;
441  #elif LPC24XX_CCLK <= 60000000U
442    MAMTIM = 0x3;
443  #else
444    MAMTIM = 0x4;
445  #endif
446  MAMCR = 0x2;
447
448  /* Enable fast IO for ports 0 and 1 */
449  SCS |= 0x1;
450
451  /* Set fast IO */
452  FIO0DIR = 0;
453  FIO1DIR = 0;
454  FIO2DIR = 0;
455  FIO3DIR = 0;
456  FIO4DIR = 0;
457  FIO0CLR = 0xffffffff;
458  FIO1CLR = 0xffffffff;
459  FIO2CLR = 0xffffffff;
460  FIO3CLR = 0xffffffff;
461  FIO4CLR = 0xffffffff;
462
463  lpc24xx_init_emc_memory_map();
464  lpc24xx_init_emc_dynamic();
465
466  #ifdef LPC24XX_STOP_GPDMA
467    if ((PCONP & PCONP_GPDMA) != 0) {
468      GPDMA_CONFIG = 0;
469      PCONP &= ~PCONP_GPDMA;
470    }
471  #endif
472
473  #ifdef LPC24XX_STOP_ETHERNET
474    if ((PCONP & PCONP_ETHERNET) != 0) {
475      MAC_COMMAND = 0x38;
476      MAC_MAC1 = 0xcf00;
477      MAC_MAC1 = 0;
478      PCONP &= ~PCONP_ETHERNET;
479    }
480  #endif
481
482  #ifdef LPC24XX_STOP_USB
483    if ((PCONP & PCONP_USB) != 0) {
484      OTG_CLK_CTRL = 0;
485      PCONP &= ~PCONP_USB;
486    }
487  #endif
488
489  /* Copy .text section */
490  bsp_start_memcpy(
491    (int *) bsp_section_text_begin,
492    (const int *) bsp_section_text_load_begin,
493    (size_t) bsp_section_text_size
494  );
495
496  /* Copy .rodata section */
497  bsp_start_memcpy(
498    (int *) bsp_section_rodata_begin,
499    (const int *) bsp_section_rodata_load_begin,
500    (size_t) bsp_section_rodata_size
501  );
502
503  /* Copy .data section */
504  bsp_start_memcpy(
505    (int *) bsp_section_data_begin,
506    (const int *) bsp_section_data_load_begin,
507    (size_t) bsp_section_data_size
508  );
509
510  /* Copy .fast_text section */
511  bsp_start_memcpy(
512    (int *) bsp_section_fast_text_begin,
513    (const int *) bsp_section_fast_text_load_begin,
514    (size_t) bsp_section_fast_text_size
515  );
516
517  /* Copy .fast_data section */
518  bsp_start_memcpy(
519    (int *) bsp_section_fast_data_begin,
520    (const int *) bsp_section_fast_data_load_begin,
521    (size_t) bsp_section_fast_data_size
522  );
523
524  /* Clear .bss section */
525  lpc24xx_clear_bss();
526
527  /* At this point we can use objects outside the .start section */
528}
Note: See TracBrowser for help on using the repository browser.