summaryrefslogtreecommitdiffstats
path: root/bsps/riscv/riscv/start/bspstart.c
blob: a462bbe6e17c396becb09b7c7319338c1e014b99 (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
/*
 * Copyright (c) 2018 embedded brains GmbH
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
 * SUCH DAMAGE.
 */

#include <bsp/bootcard.h>
#include <bsp/fatal.h>
#include <bsp/fdt.h>
#include <bsp/irq-generic.h>
#include <bsp/riscv.h>

#include <libfdt.h>
#include <string.h>

#if RISCV_ENABLE_FRDME310ARTY_SUPPORT != 0
unsigned int riscv_core_freq;
#endif

void *riscv_fdt_get_address(const void *fdt, int node)
{
  int parent;
  int ac;
  int len;
  const uint32_t *reg;
  uint64_t addr;

  parent = fdt_parent_offset(fdt, node);
  if (parent < 0) {
    return NULL;
  }

  ac = fdt_address_cells(fdt, parent);
  if (ac != 1 && ac != 2) {
    return NULL;
  }

  reg = fdt_getprop(fdt, node, "reg", &len);
  if (reg == NULL || len < ac) {
    return NULL;
  }

  addr = 0;

  while (ac > 0) {
    addr = (addr << 32) | fdt32_to_cpu(*reg);
    ++reg;
    --ac;
  }

#if __riscv_xlen < 64
  if (addr > 0xffffffff) {
    return NULL;
  }
#endif

  return (void *)(uintptr_t) addr;
}

#ifdef RTEMS_SMP
uint32_t riscv_hart_count;

static uint32_t riscv_hart_phandles[CPU_MAXIMUM_PROCESSORS];
#else
static uint32_t riscv_hart_phandles[1];
#endif

static void riscv_find_harts(void)
{
  const void *fdt;
  int node;
  uint32_t max_hart_index;

  fdt = bsp_fdt_get();
  max_hart_index = 0;
  node = -1;

  while ((node = fdt_node_offset_by_compatible(fdt, node, "riscv")) >= 0) {
    int subnode;
    const uint32_t *val;
    int len;
    uint32_t phandle;
    uint32_t hart_index;

    val = fdt_getprop(fdt, node, "reg", &len);
    if (val == NULL || len != 4) {
      bsp_fatal(RISCV_FATAL_INVALID_HART_REG_IN_DEVICE_TREE);
    }

    hart_index = fdt32_to_cpu(val[0]);

    if (hart_index >= RTEMS_ARRAY_SIZE(riscv_hart_phandles)) {
      continue;
    }

    if (hart_index > max_hart_index) {
      max_hart_index = hart_index;
    }

    phandle = 0;

    fdt_for_each_subnode(subnode, fdt, node) {
      int propoff;
      bool interrupt_controller;
      uint32_t potential_phandle;

      interrupt_controller = false;
      potential_phandle = 0;

      fdt_for_each_property_offset(propoff, fdt, subnode) {
        const char *name;

        val = fdt_getprop_by_offset(fdt, propoff, &name, &len);
        if (val != NULL) {
          if (strcmp(name, "interrupt-controller") == 0) {
            interrupt_controller = true;
          } else if (len == 4 && strcmp(name, "phandle") == 0) {
            potential_phandle = fdt32_to_cpu(val[0]);
          }
        }
      }

      if (interrupt_controller) {
        phandle = potential_phandle;
        break;
      }
    }

    riscv_hart_phandles[hart_index] = phandle;
  }

#ifdef RTEMS_SMP
  riscv_hart_count = max_hart_index + 1;
#endif
}

uint32_t riscv_get_hart_index_by_phandle(uint32_t phandle)
{
  uint32_t hart_index;

  for (hart_index = 0; hart_index < riscv_hart_count; ++hart_index) {
    if (riscv_hart_phandles[hart_index] == phandle) {
      return hart_index;
    }
  }

  return UINT32_MAX;
}

#if RISCV_ENABLE_FRDME310ARTY_SUPPORT != 0
static uint32_t get_core_frequency(void)
{
	uint32_t node;
	const char *fdt=bsp_fdt_get();

  char *tlclk;
	uint32_t len;

  do
  {
    node=fdt_node_offset_by_compatible(fdt, -1,"fixed-clock");
    uint32_t *val=NULL;
    if (node>0)
    {
      tlclk = fdt_getprop(fdt, node, "clock-output-names", &len);

      if (strcmp(tlclk,"tlclk") == 0)
      {
        val = fdt_getprop(fdt, node, "clock-frequency", &len);
		    if(val !=NULL)
		    {
			    riscv_core_freq=fdt32_to_cpu(*val);
          break;
		    }
      }
	  }else
    {
      bsp_fatal(RISCV_FATAL_NO_TLCLOCK_FREQUENCY_IN_DEVICE_TREE);
    }

  } while (node > 0);

	return riscv_core_freq;
}

inline uint32_t riscv_get_core_frequency(void)
{
	return riscv_core_freq;
}
#endif

void bsp_start(void)
{
  riscv_find_harts();
  bsp_interrupt_initialize();

#if RISCV_ENABLE_FRDME310ARTY_SUPPORT != 0
	riscv_core_freq=get_core_frequency();
#endif

}