summaryrefslogtreecommitdiffstats
path: root/c/src/lib/libbsp/arm/altera-cyclone-v/startup/bspstarthooks.c
blob: c8266a50fc9a15822addffc43f0b4e0ccde851ae (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
/*
 * Copyright (c) 2013 embedded brains GmbH.  All rights reserved.
 *
 *  embedded brains GmbH
 *  Dornierstr. 4
 *  82178 Puchheim
 *  Germany
 *  <info@embedded-brains.de>
 *
 * 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.
 */

#include <bsp.h>
#include <bsp/start.h>
#include <bsp/arm-cp15-start.h>
#include <bsp/arm-a9mpcore-start.h>
#include <bsp/linker-symbols.h>
#include <alt_address_space.h>
#include "socal/socal.h"
#include "socal/alt_sdr.h"
#include "socal/hps.h"
#include "../include/arm-cache-l1.h"

/*#define DEBUG_ECC_ERROR*/ /* TODO: Delete DEBUG_ECC_ERROR after the implementation phase */

#ifdef RTEMS_SMP
  #define MMU_DATA_READ_WRITE ARMV7_MMU_DATA_READ_WRITE_SHAREABLE
#else
  #define MMU_DATA_READ_WRITE ARMV7_MMU_DATA_READ_WRITE_CACHED
#endif

/* 1 MB reset default value for address filtering start */
#define BSPSTART_L2_CACHE_ADDR_FILTERING_START_RESET 0x100000

#ifndef BSPSTARTHOOKS_MIN
#define BSPSTARTHOOKS_MIN( a, b ) ( ( a ) < ( b ) ? ( a ) : ( b ) )
#endif

LINKER_SYMBOL( bsp_section_nocache_size );
LINKER_SYMBOL( bsp_section_nocache_end );
LINKER_SYMBOL( bsp_section_nocache_begin );

BSP_START_DATA_SECTION static const arm_cp15_start_section_config
  altcycv_mmu_config_table[] = {
  ARMV7_CP15_START_DEFAULT_SECTIONS,
  {
    .begin = (uint32_t) bsp_section_nocache_begin,
    .end   = (uint32_t) bsp_section_nocache_end,

    .flags = ARMV7_MMU_DATA_READ_WRITE | ARM_MMU_SECT_TEX_0
  }, { /* Periphery area */
    .begin = 0xFC000000U,
    .end   = 0x00000000U,
    .flags = ARMV7_MMU_DEVICE
  }
};

BSP_START_TEXT_SECTION static void setup_mmu_and_cache( const uint32_t CPU_ID )
{
  uint32_t       ctrl  = arm_cp15_get_control();
  const uint32_t CORES = BSPSTARTHOOKS_MIN(
    (uintptr_t) bsp_processor_count,
    rtems_configuration_get_maximum_processors() );

  /* We expect the L1 caches and program flow prediction to be off */
  assert( ( ctrl & ARM_CP15_CTRL_I ) == 0 );
  assert( ( ctrl & ARM_CP15_CTRL_C ) == 0 );
  assert( ( ctrl & ARM_CP15_CTRL_Z ) == 0 );

  ctrl = arm_cp15_start_setup_mmu_and_cache(
    ARM_CP15_CTRL_A | ARM_CP15_CTRL_M,
    ARM_CP15_CTRL_AFE
    );

  if( CPU_ID == 0 ) {
    arm_cp15_start_setup_translation_table(
      (uint32_t *) bsp_translation_table_base,
      ARM_MMU_DEFAULT_CLIENT_DOMAIN,
      &altcycv_mmu_config_table[0],
      RTEMS_ARRAY_SIZE( altcycv_mmu_config_table )
    );
  } else {
    /* FIXME: Sharing the translation table between processors is brittle */
    arm_cp15_set_translation_table_base((uint32_t *) bsp_translation_table_base);
  }

  /* Enable MMU */
  ctrl |= ARM_CP15_CTRL_M;

  arm_cp15_set_control( ctrl );
  
  if( CPU_ID == (CORES - 1) ) {
    /* Enable all cache levels for the last core */
    rtems_cache_enable_instruction();
    rtems_cache_enable_data();
  } else {
    /* Enable only L1 cache */
    arm_cache_l1_enable_data();
    arm_cache_l1_enable_instruction();
  }

  /* Enable flow control prediction aka. branch prediction */

/* TODO: With the current network stack 06-Feb2014 in_checksum()
 * becomes a severe performance bottle neck with branch prediction enabled
   ctrl |= ARM_CP15_CTRL_Z;
   arm_cp15_set_control(ctrl);*/
}

BSP_START_TEXT_SECTION void bsp_start_hook_0( void )
{
  uint32_t ctrl;
  volatile a9mpcore_scu *scu    = (volatile a9mpcore_scu *) BSP_ARM_A9MPCORE_SCU_BASE;
  uint32_t               cpu_id = arm_cortex_a9_get_multiprocessor_cpu_id();
  const uint32_t         CORES  = BSPSTARTHOOKS_MIN(
    (uintptr_t) bsp_processor_count,
    rtems_configuration_get_maximum_processors() );
  
  assert( cpu_id < CORES );
  
  if( cpu_id < CORES ) {
    if( cpu_id == 0 ) {
      ctrl = arm_cp15_mmu_disable( 32 );

      ctrl &= ~( ARM_CP15_CTRL_I | ARM_CP15_CTRL_C | ARM_CP15_CTRL_Z );
      arm_cp15_set_control( ctrl );
      
      /* Enable Snoop Control Unit (SCU) */
      arm_a9mpcore_start_scu_enable( scu );
    }

#ifdef RTEMS_SMP
    /* Enable cache coherency support for this processor */
    uint32_t actlr = arm_cp15_get_auxiliary_control();
    actlr |= ARM_CORTEX_A9_ACTL_SMP;
    arm_cp15_set_auxiliary_control(actlr);
#endif

    if (cpu_id == 0) {
      arm_a9mpcore_start_scu_invalidate(scu, cpu_id, 0xF);
    }
    
    setup_mmu_and_cache( cpu_id );
    
#ifdef RTEMS_SMP
    if (cpu_id != 0) {
      arm_a9mpcore_start_set_vector_base();

      arm_gic_irq_initialize_secondary_cpu();

      arm_cp15_set_domain_access_control(
        ARM_CP15_DAC_DOMAIN(ARM_MMU_DEFAULT_CLIENT_DOMAIN, ARM_CP15_DAC_CLIENT)
      );
      _SMP_Start_multitasking_on_secondary_processor();
    }
#endif
  } else {
    /* FIXME: Shutdown processor */
    while (1) {
      __asm__ volatile ("wfi");
    }
  }
}

BSP_START_TEXT_SECTION void bsp_start_hook_1( void )
{
  uint32_t addr_filt_start;
  uint32_t addr_filt_end;

  /* Disable ECC. Preloader respectively UBoot enable ECC.
     But they do run without interrupts. Our BSP will enable interrupts
     and get spurious ECC error interrupts. Thus we disasable ECC
     until we either know about a better handling or Altera has modified
     it's SDRAM settings to not create possibly false ECC errors */
  uint32_t ctlcfg = alt_read_word( ALT_SDR_CTL_CTLCFG_ADDR );
  ctlcfg &= ALT_SDR_CTL_CTLCFG_ECCEN_CLR_MSK;
  alt_write_word( ALT_SDR_CTL_CTLCFG_ADDR, ctlcfg );

  /* Perform L3 remap register programming first by setting the desired new MPU
     address space 0 mapping. Assume BOOTROM in order to be able to boot the
     second core. */
  alt_addr_space_remap(
    ALT_ADDR_SPACE_MPU_ZERO_AT_BOOTROM,
    ALT_ADDR_SPACE_NONMPU_ZERO_AT_SDRAM,
    ALT_ADDR_SPACE_H2F_ACCESSIBLE,
    ALT_ADDR_SPACE_LWH2F_ACCESSIBLE );

  /* Next, adjust the L2 cache address filtering range. Set the start address
   * to the default reset value and retain the existing end address
   * configuration. */
  alt_l2_addr_filter_cfg_get( &addr_filt_start, &addr_filt_end );

  if ( addr_filt_start != BSPSTART_L2_CACHE_ADDR_FILTERING_START_RESET ) {
    alt_l2_addr_filter_cfg_set( BSPSTART_L2_CACHE_ADDR_FILTERING_START_RESET,
                                addr_filt_end );
  }

  arm_a9mpcore_start_hook_1();
  bsp_start_copy_sections();

  bsp_start_clear_bss();
}