summaryrefslogtreecommitdiffstats
path: root/tools/cpu/nios2/nios2gen.c
blob: 7a54f5421f588968385ff6919594d13214baec68 (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
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
/*
 *  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.com/license/LICENSE.
 */

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

#include "ptf.h"
#include "bridges.h"
#include "clocks.h"
#include "devices.h"
#include "memory.h"
#include "output.h"
#include "linkcmds.h"

#define NIOS2GEN_PACKAGE PACKAGE
#define NIOS2GEN_VERSION VERSION

#include "getopt.h"

/********************************************************/

void store_ptf_parent(struct ptf_item *pi, void *arg)
{
  struct ptf *p = pi->item[pi->level-1];
  *(struct ptf **)arg = p;
}

/********************************************************/

void store_ptf_ptr(struct ptf_item *pi, void *arg)
{
  struct ptf *p = pi->item[pi->level];
  *(struct ptf **)arg = p;
}

/********************************************************/

void printf_ptf_value(struct ptf_item *pi, void *arg)
{
  struct ptf *p = pi->item[pi->level];
  printf(*(char **)arg, p->value);
}

/********************************************************/

void read_include_file(struct ptf_item *pi, void *arg)
{
    struct ptf *inc, *next;
    struct ptf *p = pi->item[pi->level];

    inc = ptf_parse_file(p->value);

    if(inc == NULL)
    {
        fprintf(stderr, "Warning: couldn't parse included '%s'.\n", p->value);
        return;
    };

    printf("Successfully read included PTF file %s.\n", p->value);

    next = p->next;
    for(p->next = inc; p->next != NULL; p = p->next);
    p->next = next;
}

/********************************************************/

void version(FILE *f)
{
fprintf(f,
  "nios2gen (" __DATE__ ")\n"
  "  Copyright (c) 2006 Kolja Waschk rtemsdev/ixo.de\n"
  "\n"
  "  The license and distribution terms for this file may be\n"
  "  found in the file LICENSE in this distribution or at\n"
  "  http://www.rtems.com/license/LICENSE.\n"
  , VERSION
);
}

void usage (FILE *f, char *errmsg)
{
  char *info = "Purpose:\n  Create RTEMS nios2 BSP configuration from Altera SOPC description\n";
  if(errmsg) info=errmsg;
  version(f);
  fprintf(f,"\n%s\nUsage: nios2gen [OPTIONS] <SOPC PTF> [CONFIG PTF] [CONFIG PTF]...\n", info);
  fprintf(f,"   -h         --help               Print help and exit\n");
  fprintf(f,"   -V         --version            Print version and exit\n");
  fprintf(f,"   -bFILENAME --bspheader=FILENAME Output BSP configuration header file (default='sopc.h')\n");
  fprintf(f,"   -B         --no-bspheader       Do not output BSP configuration header file\n");
  fprintf(f,"   -lFILENAME --linkcmds=FILENAME  Output linker script (default='linkcmds')\n");
  fprintf(f,"   -L         --no-linkcmds        Do not output linker script\n");
  fprintf(f,"   -pFILENAME --parsed=FILENAME    Output PTF contents as long list (default: don't)\n");
  fprintf(f,"   -P         --no-parsed          Do not output PTF contents as long list\n");
  fprintf(f,"   -q         --quiet              Do not print progress info to stdout\n\n");
  fprintf(f,"Using \"-\" as the FILENAME means standard output (stdout).\n");
}

/********************************************************/

int main(int argc, char *argv[])
{
    struct ptf *sopc, *cfg, *p, *cpu;
    struct ptf_item pi;
    device_desc *devices;
    bus_bridge_pair *bridges;
    clock_desc *clocks;
    memory_desc *memory;

    int verbose = 1;
    int output_order = 127;
    char *parsed_filename = NULL;
    int output_parsed = 0;
    char *bspheader_filename = "sopc.h";
    int output_bspheader = 1;
    char *linkcmds_filename = "linkcmds";
    int output_linkcmds = 2;

    optarg = 0;
    optind = 1;
    opterr = 1;
    optopt = '?';

    if(argc > 126)
    {
      usage(stderr,"Too many commandline arguments!\n");
      return -1;
    };

    while(1)
    {
      int c, long_index = 0;
      static char *optstring = "hVBb:Ll:Pp:q";

      static struct option long_options[] =
      {
        { "help",           0, NULL, 'h' },
        { "version",        0, NULL, 'V' },
        { "no-bspheader",   0, NULL, 'B' },
        { "bspheader",      1, NULL, 'b' },
        { "no-linkcmds",    0, NULL, 'L' },
        { "linkcmds",       1, NULL, 'l' },
        { "no-parsed",      0, NULL, 'P' },
        { "parsed",         1, NULL, 'p' },
        { "quiet",          0, NULL, 'q' },
        { NULL, 0, NULL, 0 }
      };

      c = getopt_long (argc, argv, optstring, long_options, &long_index);

      if(c == -1) break; /* Exit from while(1) loop */

      switch(c)
      {
      case 'q': /* Be quiet */
        verbose = 0;
        break;

      case 'h': /* Print help and exit */
        usage(stdout,NULL);
        return 0;

      case 'V': /* Print version and exit */
        version(stdout);
        return 0;

      case 'B': /* Do not output BSP configuration header file */
        output_bspheader = 0;
        break;

      case 'b': /* Output BSP configuration header file */
        bspheader_filename = optarg;
        output_bspheader = output_order;
        output_order--;
        break;

      case 'L': /* Do not output linker script */
        output_linkcmds = 0;
        break;

      case 'l': /* Output linker script */
        linkcmds_filename = optarg;
        output_linkcmds = output_order;
        output_order--;
        break;

      case 'P': /* Do not output PTF contents */
        output_parsed = 0;
        break;

      case 'p': /* Output PTF contents as long list */
        parsed_filename = optarg;
        output_parsed = output_order;
        output_order--;
        break;

      case 0:
      case '?':
        return -1;

      default:
        fprintf(stderr, "%s: unknown option: %c\n", NIOS2GEN_PACKAGE, c);
      };
    };

    if(optind >= argc)
    {
        usage(stderr,"No PTF specified!\n");
        return -1;
    };

    /********************************************************/

    sopc = ptf_parse_file(argv[optind]);
    if(sopc == NULL)
    {
        fprintf(stderr, "Could not parse system description PTF '%s'.\n", argv[optind]);
        return -1;
    };

    if(verbose) printf("Successfully read SOPC PTF file %s.\n", argv[optind]);

    /********************************************************/

    cfg = NULL;

    for(optind++;optind<argc;optind++)
    {
      struct ptf *morecfg = ptf_parse_file(argv[optind]);

      if(morecfg == NULL)
      {
        fprintf(stderr, "Couldn't parse '%s'.\n", argv[optind]);
        return -1;
      };

      if(verbose) printf("Successfully read config PTF file %s.\n", argv[optind]);

      cfg = ptf_concat(cfg, morecfg);
    };

    /********************************************************/
    /* Pull in include files specified in the configs; */
    /* Only one level is read; included files are not */
    /* checked for further INCLUDEs */

    {
      struct ptf include_item = { item, "INCLUDE", 0, 0, 0 };
      struct ptf_item inc_file_spec = { 1, &include_item };
      ptf_match(cfg, &inc_file_spec, read_include_file, NULL);
    }

    /********************************************************/
    /* Find CPU */

    if(verbose) printf("Looking for usable CPUs...\n");

    /* Check if a CPU has been specified in the config PTF */
    {
      struct ptf modules         = { section, "MODULES", 0, 0, 0 };
      struct ptf cpu_def         = { item, "CPU", 0, 0, 0 };
      struct ptf_item cpu_spec   = { 2, &modules, &cpu_def };

      ptf_match(cfg, &cpu_spec, store_ptf_ptr, &cpu);
    };

    /* Look for CPUs in system description PTF */
    {
      int cpu_count;
      struct ptf system          = { section, "SYSTEM", 0, 0, 0 };
      struct ptf module          = { section, "MODULE", 0, 0, 0 };
      struct ptf nios2_cpu_class = { item, "class", "altera_nios2", 0, 0 };
      struct ptf_item class_spec = { 3, &system, &module, &nios2_cpu_class };

      if(cpu) if(cpu->value) class_spec.item[1]->value = cpu->value;

      cpu_count = ptf_match(sopc, &class_spec, store_ptf_parent, &cpu);

      if(cpu_count > 1)
      {
        fprintf(stderr, "There is more than one CPU. Please specify the one\n");
        fprintf(stderr, "you want to use with this BSP in your config file.\n");
        fprintf(stderr, "The available CPUs are named as follows:\n");
        ptf_match(sopc, &class_spec, printf_ptf_value, "  %s\n");
        return -1;
      };

      if(cpu_count == 0)
      {
        fprintf(stderr, "There is no NIOS2 cpu in the system.\n");
        return -1;
      }
    };

    if(verbose)
    {
      printf("Using NIOS II CPU '%s'.\n", cpu->value);
      printf("Only modules mastered by this CPU are considered now.\n");
    };

    /********************************************************/
    /* Find Bridges */

    if(verbose) printf("Looking for bus bridges...\n");

    bridges = find_bridges(sopc);

    if(verbose)
    {
      if(bridges)
      {
        bus_bridge_pair *bbp;
        for(bbp = bridges; bbp; bbp=bbp->next)
        {
          printf("Found bridge: %s\n", bbp->mastered_by);
          printf("               \\_%s\n", bbp->bridges_to);
        };
      }
      else
      {
        printf("No bridges present.\n");
      };
    };

    /********************************************************/
    /* Find clocks */

    if(verbose) printf("Looking for clock definitions...\n");

    clocks = find_clocks(sopc, cfg);

    if(verbose)
    {
      if(clocks)
      {
        clock_desc *cs;
        for(cs = clocks; cs; cs = cs->next)
        {
          printf("Found clock \"%s\" (%lu Hz), naming it %s\n", cs->name, cs->freq, cs->cfgname);
        };
      }
      else
      {
        printf("No clocks present.\n");
      };
    };

    /********************************************************/
    /* Find other devices available to the selected CPU */

    if(verbose) printf("Looking for devices...\n");

    devices = find_devices(sopc, cfg, cpu, bridges);

    if(verbose)
    {
      if(devices)
      {
        device_desc *dd;
        for(dd = devices; dd; dd=dd->next)
        {
          printf("Found device \"%s\", naming it %s\n", dd->ptf->value, dd->cfgname);
        };
      }
      else
      {
        printf("No devices present.\n");
      };
    };

    /********************************************************/
    /* Find out which devices are actually memory */

    if(verbose) printf("Looking for memory...\n");

    memory = find_memory(devices);

    if(verbose)
    {
      if(memory)
      {
        memory_desc *md;
        for(md = memory; md; md=md->next)
        {
          printf("Found memory in \"%s\", base=0x%08X, size=%lu bytes\n",
                          md->dev->cfgname,
                          md->base, md->size);
        };
      }
      else
      {
        printf("None of the devices seems to provide memory?!\n");
      };
    };


    /********************************************************/
    /* Output files in the order they were specified
       on the command line */

    {
      int i;
      for(i=0;i<3;i++)
      {
        if(output_bspheader>0
           && output_bspheader>=output_linkcmds
           && output_bspheader>=output_parsed)
        {
          output_bspheader = 0;
          if(bspheader_filename == NULL || (bspheader_filename[0]=='-' && bspheader_filename[1]==0))
          {
            fwrite_header_file(stdout, cfg, devices, clocks);
          }
          else
          {
            FILE *f = fopen(bspheader_filename, "w");
            if(!f)
            {
              perror(bspheader_filename);
              return -1;
            }
            else
            {
              fwrite_header_file(f, cfg, devices, clocks);
              fclose(f);
            }
          }
        };
        if(output_linkcmds>0
           && output_linkcmds>=output_bspheader
           && output_linkcmds>=output_parsed)
        {
          output_linkcmds = 0;
          if(linkcmds_filename == NULL || (linkcmds_filename[0]=='-' && linkcmds_filename[1]==0))
          {
            fwrite_linkcmds_file(stdout, cfg, cpu, devices, memory);
          }
          else
          {
            FILE *f = fopen(linkcmds_filename, "w");
            if(!f)
            {
              perror(linkcmds_filename);
              return -1;
            }
            else
            {
              fwrite_linkcmds_file(f, cfg, cpu, devices, memory);
              fclose(f);
            }
          }
        };
        if(output_parsed>0
           && output_parsed>=output_linkcmds
           && output_parsed>=output_bspheader)
        {
          output_parsed = 0;
          if(parsed_filename == NULL || (parsed_filename[0]=='-' && parsed_filename[1]==0))
          {
            ptf_printf(stdout, sopc, "");
          }
          else
          {
            FILE *f = fopen(parsed_filename, "w");
            if(!f)
            {
              perror(parsed_filename);
              return -1;
            }
            else
            {
              ptf_printf(f, sopc, "");
              fclose(f);
            }
          }
        };
      }
    };

    if(verbose) printf("Done.\n");

    return 0;
}

/* vi:ts=4:
 */