summaryrefslogtreecommitdiffstats
path: root/tools/cpu/nios2/memory.c
blob: cb1ea7f31e1c3fdfea8514d7f1d180ec44348073 (plain)
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
/*
 *  Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de
 *
 *  The license and distribution terms for this file may be
 *  found in the file LICENSE in this distribution or at
 *  http://www.rtems.org/license/LICENSE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "ptf.h"
#include "devices.h"
#include "memory.h"

memory_desc *find_memory(device_desc *devices)
{
  struct ptf *p;
  struct ptf_item pi;
  memory_desc *tmd;
  memory_desc *memory = NULL;

  /********************************************************/
  /* Check which of the devices are memory, sort by size */

  if(devices)
  {
    struct ptf *p, *s;
    struct ptf_item pi;
    device_desc *dd;

    for(dd = devices; dd; dd=dd->next)
    {
      p = ptf_find(dd->ptf->sub, &pi, item, "Is_Memory_Device", "1");
      if(p != NULL && pi.level>0)
      {
        s = pi.item[pi.level-1];
        p = ptf_find(s, &pi, item, "Base_Address", 0);
      };

      if(p != NULL)
      {
        tmd = (memory_desc*)malloc(sizeof(memory_desc));

        if(tmd != NULL)
        {
          tmd->base = strtoul(p->value, 0, 0);

          p = ptf_find(s, &pi, item, "Address_Span", 0);
          if(p != 0)
          {
            tmd->size = strtoul(p->value, 0, 0);
          }
          else
          {
            tmd->size = 0;
            p = ptf_find(s, &pi, item, "Address_Width", 0);
            if(p) tmd->size = 1 << strtoul(p->value, 0, 0);
            p = ptf_find(s, &pi, item, "Data_Width", 0);
            if(p) tmd->size *= (strtoul(p->value, 0, 0) >> 3);
          };

          if(tmd->size == 0)
          {
            free(tmd);
          }
          else
          {
            tmd->dev = dd;

            if(memory == NULL)
            {
              tmd->next = NULL;
              memory = tmd;
            }
            else
            {
              if(tmd->size > memory->size)
              {
                tmd->next = memory;
                memory = tmd;
              }
              else
              {
                memory_desc *uplink = memory;
                while(uplink->next != NULL && uplink->next->size > tmd->size) uplink=uplink->next;
                tmd->next = uplink->next;
                uplink->next = tmd;
              };
            };
          };
        };
      };
    };
  };

  return memory;
}