Loading...
   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
 495
 496
 497
 498
 499
 500
 501
 502
 503
 504
 505
 506
 507
 508
 509
 510
 511
 512
 513
 514
 515
 516
 517
 518
 519
 520
 521
 522
 523
 524
 525
 526
 527
 528
 529
 530
 531
 532
 533
 534
 535
 536
 537
 538
 539
 540
 541
 542
 543
 544
 545
 546
 547
 548
 549
 550
 551
 552
 553
 554
 555
 556
 557
 558
 559
 560
 561
 562
 563
 564
 565
 566
 567
 568
 569
 570
 571
 572
 573
 574
 575
 576
 577
 578
 579
 580
 581
 582
 583
 584
 585
 586
 587
 588
 589
 590
 591
 592
 593
 594
 595
 596
 597
 598
 599
 600
 601
 602
 603
 604
 605
 606
 607
 608
 609
 610
 611
 612
 613
 614
 615
 616
 617
 618
 619
 620
 621
 622
 623
 624
 625
 626
 627
 628
 629
 630
 631
 632
 633
 634
 635
 636
 637
 638
 639
 640
 641
 642
 643
 644
 645
 646
 647
 648
 649
 650
 651
 652
 653
 654
 655
 656
 657
 658
 659
 660
 661
 662
 663
 664
 665
 666
 667
 668
 669
 670
 671
 672
 673
 674
 675
 676
 677
 678
 679
 680
 681
 682
 683
 684
 685
 686
 687
 688
 689
 690
 691
 692
 693
 694
 695
 696
 697
 698
 699
 700
 701
 702
 703
 704
 705
 706
 707
 708
 709
 710
 711
 712
 713
 714
 715
 716
 717
 718
 719
 720
 721
 722
 723
 724
 725
 726
 727
 728
 729
 730
 731
 732
 733
 734
 735
 736
 737
 738
 739
 740
 741
 742
 743
 744
 745
 746
 747
 748
 749
 750
 751
 752
 753
 754
 755
 756
 757
 758
 759
 760
 761
 762
 763
 764
 765
 766
 767
 768
 769
 770
 771
 772
 773
 774
 775
 776
 777
 778
 779
 780
 781
 782
 783
 784
 785
 786
 787
 788
 789
 790
 791
 792
 793
 794
 795
 796
 797
 798
 799
 800
 801
 802
 803
 804
 805
 806
 807
 808
 809
 810
 811
 812
 813
 814
 815
 816
 817
 818
 819
 820
 821
 822
 823
 824
 825
 826
 827
 828
 829
 830
 831
 832
 833
 834
 835
 836
 837
 838
 839
 840
 841
 842
 843
 844
 845
 846
 847
 848
 849
 850
 851
 852
 853
 854
 855
 856
 857
 858
 859
 860
 861
 862
 863
 864
 865
 866
 867
 868
 869
 870
 871
 872
 873
 874
 875
 876
 877
 878
 879
 880
 881
 882
 883
 884
 885
 886
 887
 888
 889
 890
 891
 892
 893
 894
 895
 896
 897
 898
 899
 900
 901
 902
 903
 904
 905
 906
 907
 908
 909
 910
 911
 912
 913
 914
 915
 916
 917
 918
 919
 920
 921
 922
 923
 924
 925
 926
 927
 928
 929
 930
 931
 932
 933
 934
 935
 936
 937
 938
 939
 940
 941
 942
 943
 944
 945
 946
 947
 948
 949
 950
 951
 952
 953
 954
 955
 956
 957
 958
 959
 960
 961
 962
 963
 964
 965
 966
 967
 968
 969
 970
 971
 972
 973
 974
 975
 976
 977
 978
 979
 980
 981
 982
 983
 984
 985
 986
 987
 988
 989
 990
 991
 992
 993
 994
 995
 996
 997
 998
 999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
/* prom.c - emulates a sparc v0 PROM for the linux kernel.
 *
 * Copyright (C) 2003 Konrad Eisele <eiselekd@web.de>
 * Copyright (C) 2004 Stefan Holst <mail@s-holst.de>
 * Copyright (C) 2007 Daniel Hellstrom <daniel@gaisler.com>
 *
 * See file CREDITS for list of people who contributed to this
 * project.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of
 * the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
 * MA 02111-1307 USA
 *
 */

#include <common.h>
#include <asm/prom.h>
#include <asm/machines.h>
#include <asm/srmmu.h>
#include <asm/processor.h>
#include <asm/irq.h>
#include <asm/leon.h>

#include <config.h>
/*
#define PRINT_ROM_VEC
*/
extern struct linux_romvec *kernel_arg_promvec;

#define PROM_PGT __attribute__ ((__section__ (".prom.pgt")))
#define PROM_TEXT __attribute__ ((__section__ (".prom.text")))
#define PROM_DATA __attribute__ ((__section__ (".prom.data")))

/* for __va */
extern int __prom_start;
#define PAGE_OFFSET 0xf0000000
#define phys_base CFG_SDRAM_BASE
#define PROM_OFFS 8192
#define PROM_SIZE_MASK (PROM_OFFS-1)
#define __va(x) ( \
	(void *)( ((unsigned long)(x))-PROM_OFFS+ \
	(CFG_PROM_OFFSET-phys_base)+PAGE_OFFSET-TEXT_BASE ) \
	)
#define __phy(x) ((void *)(((unsigned long)(x))-PROM_OFFS+CFG_PROM_OFFSET-TEXT_BASE))

struct property {
	char *name;
	char *value;
	int length;
};

struct node {
	int level;
	struct property *properties;
};

static void leon_reboot(char *bcommand);
static void leon_halt(void);
static int leon_nbputchar(int c);
static int leon_nbgetchar(void);

static int no_nextnode(int node);
static int no_child(int node);
static int no_proplen(int node, char *name);
static int no_getprop(int node, char *name, char *value);
static int no_setprop(int node, char *name, char *value, int len);
static char *no_nextprop(int node, char *name);

static struct property PROM_TEXT *find_property(int node, char *name);
static int PROM_TEXT leon_strcmp(const char *s1, const char *s2);
static void *PROM_TEXT leon_memcpy(void *dest, const void *src, size_t n);
static void PROM_TEXT leon_reboot_physical(char *bcommand);

void __inline__ leon_flush_cache_all(void)
{
	__asm__ __volatile__(" flush ");
      __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t"::"i"(ASI_DFLUSH):"memory");
}

void __inline__ leon_flush_tlb_all(void)
{
	leon_flush_cache_all();
	__asm__ __volatile__("sta %%g0, [%0] %1\n\t"::"r"(0x400),
			     "i"(ASI_MMUFLUSH):"memory");
}

typedef struct {
	unsigned int ctx_table[256];
	unsigned int pgd_table[256];
} sparc_srmmu_setup;

sparc_srmmu_setup srmmu_tables PROM_PGT = {
	{0},
	{0x1e,
	 0x10001e,
	 0x20001e,
	 0x30001e,
	 0x40001e,
	 0x50001e,
	 0x60001e,
	 0x70001e,
	 0x80001e,
	 0x90001e,
	 0xa0001e,
	 0xb0001e,
	 0xc0001e,
	 0xd0001e,
	 0xe0001e,
	 0xf0001e,
	 0x100001e,
	 0x110001e,
	 0x120001e,
	 0x130001e,
	 0x140001e,
	 0x150001e,
	 0x160001e,
	 0x170001e,
	 0x180001e,
	 0x190001e,
	 0x1a0001e,
	 0x1b0001e,
	 0x1c0001e,
	 0x1d0001e,
	 0x1e0001e,
	 0x1f0001e,
	 0x200001e,
	 0x210001e,
	 0x220001e,
	 0x230001e,
	 0x240001e,
	 0x250001e,
	 0x260001e,
	 0x270001e,
	 0x280001e,
	 0x290001e,
	 0x2a0001e,
	 0x2b0001e,
	 0x2c0001e,
	 0x2d0001e,
	 0x2e0001e,
	 0x2f0001e,
	 0x300001e,
	 0x310001e,
	 0x320001e,
	 0x330001e,
	 0x340001e,
	 0x350001e,
	 0x360001e,
	 0x370001e,
	 0x380001e,
	 0x390001e,
	 0x3a0001e,
	 0x3b0001e,
	 0x3c0001e,
	 0x3d0001e,
	 0x3e0001e,
	 0x3f0001e,
	 0x400001e,
	 0x410001e,
	 0x420001e,
	 0x430001e,
	 0x440001e,
	 0x450001e,
	 0x460001e,
	 0x470001e,
	 0x480001e,
	 0x490001e,
	 0x4a0001e,
	 0x4b0001e,
	 0x4c0001e,
	 0x4d0001e,
	 0x4e0001e,
	 0x4f0001e,
	 0x500001e,
	 0x510001e,
	 0x520001e,
	 0x530001e,
	 0x540001e,
	 0x550001e,
	 0x560001e,
	 0x570001e,
	 0x580001e,
	 0x590001e,
	 0x5a0001e,
	 0x5b0001e,
	 0x5c0001e,
	 0x5d0001e,
	 0x5e0001e,
	 0x5f0001e,
	 0x600001e,
	 0x610001e,
	 0x620001e,
	 0x630001e,
	 0x640001e,
	 0x650001e,
	 0x660001e,
	 0x670001e,
	 0x680001e,
	 0x690001e,
	 0x6a0001e,
	 0x6b0001e,
	 0x6c0001e,
	 0x6d0001e,
	 0x6e0001e,
	 0x6f0001e,
	 0x700001e,
	 0x710001e,
	 0x720001e,
	 0x730001e,
	 0x740001e,
	 0x750001e,
	 0x760001e,
	 0x770001e,
	 0x780001e,
	 0x790001e,
	 0x7a0001e,
	 0x7b0001e,
	 0x7c0001e,
	 0x7d0001e,
	 0x7e0001e,
	 0x7f0001e,
	 0x800001e,
	 0x810001e,
	 0x820001e,
	 0x830001e,
	 0x840001e,
	 0x850001e,
	 0x860001e,
	 0x870001e,
	 0x880001e,
	 0x890001e,
	 0x8a0001e,
	 0x8b0001e,
	 0x8c0001e,
	 0x8d0001e,
	 0x8e0001e,
	 0x8f0001e,
	 0x900001e,
	 0x910001e,
	 0x920001e,
	 0x930001e,
	 0x940001e,
	 0x950001e,
	 0x960001e,
	 0x970001e,
	 0x980001e,
	 0x990001e,
	 0x9a0001e,
	 0x9b0001e,
	 0x9c0001e,
	 0x9d0001e,
	 0x9e0001e,
	 0x9f0001e,
	 0xa00001e,
	 0xa10001e,
	 0xa20001e,
	 0xa30001e,
	 0xa40001e,
	 0xa50001e,
	 0xa60001e,
	 0xa70001e,
	 0xa80001e,
	 0xa90001e,
	 0xaa0001e,
	 0xab0001e,
	 0xac0001e,
	 0xad0001e,
	 0xae0001e,
	 0xaf0001e,
	 0xb00001e,
	 0xb10001e,
	 0xb20001e,
	 0xb30001e,
	 0xb40001e,
	 0xb50001e,
	 0xb60001e,
	 0xb70001e,
	 0xb80001e,
	 0xb90001e,
	 0xba0001e,
	 0xbb0001e,
	 0xbc0001e,
	 0xbd0001e,
	 0xbe0001e,
	 0xbf0001e,
	 0xc00001e,
	 0xc10001e,
	 0xc20001e,
	 0xc30001e,
	 0xc40001e,
	 0xc50001e,
	 0xc60001e,
	 0xc70001e,
	 0xc80001e,
	 0xc90001e,
	 0xca0001e,
	 0xcb0001e,
	 0xcc0001e,
	 0xcd0001e,
	 0xce0001e,
	 0xcf0001e,
	 0xd00001e,
	 0xd10001e,
	 0xd20001e,
	 0xd30001e,
	 0xd40001e,
	 0xd50001e,
	 0xd60001e,
	 0xd70001e,
	 0xd80001e,
	 0xd90001e,
	 0xda0001e,
	 0xdb0001e,
	 0xdc0001e,
	 0xdd0001e,
	 0xde0001e,
	 0xdf0001e,
	 0xe00001e,
	 0xe10001e,
	 0xe20001e,
	 0xe30001e,
	 0xe40001e,
	 0xe50001e,
	 0xe60001e,
	 0xe70001e,
	 0xe80001e,
	 0xe90001e,
	 0xea0001e,
	 0xeb0001e,
	 0xec0001e,
	 0xed0001e,
	 0xee0001e,
	 0xef0001e,
	 0x400001e		/* default */
	 }
};

/* a self contained prom info structure */
struct leon_reloc_func {
	struct property *(*find_property) (int node, char *name);
	int (*strcmp) (char *s1, char *s2);
	void *(*memcpy) (void *dest, const void *src, size_t n);
	void (*reboot_physical) (char *cmd);
};

struct leon_prom_info {
	int freq_khz;
	int leon_nctx;
	int mids[32];
	int baudrates[2];
	struct leon_reloc_func reloc_funcs;
	struct property root_properties[4];
	struct property cpu_properties[7];
#undef  CPUENTRY
#define CPUENTRY(idx) struct property cpu_properties##idx[4]
	 CPUENTRY(1);
	 CPUENTRY(2);
	 CPUENTRY(3);
	 CPUENTRY(4);
	 CPUENTRY(5);
	 CPUENTRY(6);
	 CPUENTRY(7);
	 CPUENTRY(8);
	 CPUENTRY(9);
	 CPUENTRY(10);
	 CPUENTRY(11);
	 CPUENTRY(12);
	 CPUENTRY(13);
	 CPUENTRY(14);
	 CPUENTRY(15);
	 CPUENTRY(16);
	 CPUENTRY(17);
	 CPUENTRY(18);
	 CPUENTRY(19);
	 CPUENTRY(20);
	 CPUENTRY(21);
	 CPUENTRY(22);
	 CPUENTRY(23);
	 CPUENTRY(24);
	 CPUENTRY(25);
	 CPUENTRY(26);
	 CPUENTRY(27);
	 CPUENTRY(28);
	 CPUENTRY(29);
	 CPUENTRY(30);
	 CPUENTRY(31);
	struct idprom idprom;
	struct linux_nodeops nodeops;
	struct linux_mlist_v0 *totphys_p;
	struct linux_mlist_v0 totphys;
	struct linux_mlist_v0 *avail_p;
	struct linux_mlist_v0 avail;
	struct linux_mlist_v0 *prommap_p;
	void (*synchook) (void);
	struct linux_arguments_v0 *bootargs_p;
	struct linux_arguments_v0 bootargs;
	struct linux_romvec romvec;
	struct node nodes[35];
	char s_device_type[12];
	char s_cpu[4];
	char s_mid[4];
	char s_idprom[7];
	char s_compatability[14];
	char s_leon2[6];
	char s_mmu_nctx[9];
	char s_frequency[16];
	char s_uart1_baud[11];
	char s_uart2_baud[11];
	char arg[256];
};

/* static prom info */
static struct leon_prom_info PROM_DATA spi = {
	CONFIG_SYS_CLK_FREQ / 1000,
	256,
	{
#undef	CPUENTRY
#define	CPUENTRY(idx) idx
	 CPUENTRY(0),
	 CPUENTRY(1),
	 CPUENTRY(2),
	 CPUENTRY(3),
	 CPUENTRY(4),
	 CPUENTRY(5),
	 CPUENTRY(6),
	 CPUENTRY(7),
	 CPUENTRY(8),
	 CPUENTRY(9),
	 CPUENTRY(10),
	 CPUENTRY(11),
	 CPUENTRY(12),
	 CPUENTRY(13),
	 CPUENTRY(14),
	 CPUENTRY(15),
	 CPUENTRY(16),
	 CPUENTRY(17),
	 CPUENTRY(18),
	 CPUENTRY(19),
	 CPUENTRY(20),
	 CPUENTRY(21),
	 CPUENTRY(22),
	 CPUENTRY(23),
	 CPUENTRY(24),
	 CPUENTRY(25),
	 CPUENTRY(26),
	 CPUENTRY(27),
	 CPUENTRY(28),
	 CPUENTRY(29),
	 CPUENTRY(30),
	 31},
	{38400, 38400},
	{
	 __va(find_property),
	 __va(leon_strcmp),
	 __va(leon_memcpy),
	 __phy(leon_reboot_physical),
	 },
	{
	 {__va(spi.s_device_type), __va(spi.s_idprom), 4},
	 {__va(spi.s_idprom), (char *)__va(&spi.idprom), sizeof(struct idprom)},
	 {__va(spi.s_compatability), __va(spi.s_leon2), 5},
	 {NULL, NULL, -1}
	 },
	{
	 {__va(spi.s_device_type), __va(spi.s_cpu), 4},
	 {__va(spi.s_mid), __va(&spi.mids[0]), 4},
	 {__va(spi.s_mmu_nctx), (char *)__va(&spi.leon_nctx), 4},
	 {__va(spi.s_frequency), (char *)__va(&spi.freq_khz), 4},
	 {__va(spi.s_uart1_baud), (char *)__va(&spi.baudrates[0]), 4},
	 {__va(spi.s_uart2_baud), (char *)__va(&spi.baudrates[1]), 4},
	 {NULL, NULL, -1}
	 },
#undef  CPUENTRY
#define CPUENTRY(idx) \
	{ /* cpu_properties */						\
		{__va(spi.s_device_type), __va(spi.s_cpu), 4},		\
		{__va(spi.s_mid), __va(&spi.mids[idx]), 4},			\
		{__va(spi.s_frequency), (char *)__va(&spi.freq_khz), 4},	\
		{NULL, NULL, -1}						\
	}
	CPUENTRY(1),
	CPUENTRY(2),
	CPUENTRY(3),
	CPUENTRY(4),
	CPUENTRY(5),
	CPUENTRY(6),
	CPUENTRY(7),
	CPUENTRY(8),
	CPUENTRY(9),
	CPUENTRY(10),
	CPUENTRY(11),
	CPUENTRY(12),
	CPUENTRY(13),
	CPUENTRY(14),
	CPUENTRY(15),
	CPUENTRY(16),
	CPUENTRY(17),
	CPUENTRY(18),
	CPUENTRY(19),
	CPUENTRY(20),
	CPUENTRY(21),
	CPUENTRY(22),
	CPUENTRY(23),
	CPUENTRY(24),
	CPUENTRY(25),
	CPUENTRY(26),
	CPUENTRY(27),
	CPUENTRY(28),
	CPUENTRY(29),
	CPUENTRY(30),
	CPUENTRY(31),
	{
	 0x01,			/* format */
	 M_LEON2 | M_LEON2_SOC,	/* machine type */
	 {0, 0, 0, 0, 0, 0},	/* eth */
	 0,			/* date */
	 0,			/* sernum */
	 0,			/* checksum */
	 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}	/* reserved */
	 },
	{
	 __va(no_nextnode),
	 __va(no_child),
	 __va(no_proplen),
	 __va(no_getprop),
	 __va(no_setprop),
	 __va(no_nextprop)
	 },
	__va(&spi.totphys),
	{
	 NULL,
	 (char *)CFG_SDRAM_BASE,
	 0,
	 },
	__va(&spi.avail),
	{
	 NULL,
	 (char *)CFG_SDRAM_BASE,
	 0,
	 },
	NULL,			/* prommap_p */
	NULL,
	__va(&spi.bootargs),
	{
	 {NULL, __va(spi.arg), NULL /*... */ },
	 /*... */
	 },
	{
	 0,
	 0,			/* sun4c v0 prom */
	 0, 0,
	 {__va(&spi.totphys_p), __va(&spi.prommap_p), __va(&spi.avail_p)},
	 __va(&spi.nodeops),
	 NULL, {NULL /* ... */ },
	 NULL, NULL,
	 NULL, NULL,		/* pv_getchar, pv_putchar */
	 __va(leon_nbgetchar), __va(leon_nbputchar),
	 NULL,
	 __va(leon_reboot),
	 NULL,
	 NULL,
	 NULL,
	 __va(leon_halt),
	 __va(&spi.synchook),
	 {NULL},
	 __va(&spi.bootargs_p)
	 /*... */
	 },
	{
	 {0, __va(spi.root_properties + 3) /* NULL, NULL, -1 */ },
	 {0, __va(spi.root_properties)},
	 /* cpu 0, must be spi.nodes[2] see leon_prom_init() */
	 {1, __va(spi.cpu_properties)},

#undef  CPUENTRY
#define CPUENTRY(idx) \
	  {1, __va(spi.cpu_properties##idx) }	/* cpu <idx> */
	 CPUENTRY(1),
	 CPUENTRY(2),
	 CPUENTRY(3),
	 CPUENTRY(4),
	 CPUENTRY(5),
	 CPUENTRY(6),
	 CPUENTRY(7),
	 CPUENTRY(8),
	 CPUENTRY(9),
	 CPUENTRY(10),
	 CPUENTRY(11),
	 CPUENTRY(12),
	 CPUENTRY(13),
	 CPUENTRY(14),
	 CPUENTRY(15),
	 CPUENTRY(16),
	 CPUENTRY(17),
	 CPUENTRY(18),
	 CPUENTRY(19),
	 CPUENTRY(20),
	 CPUENTRY(21),
	 CPUENTRY(22),
	 CPUENTRY(23),
	 CPUENTRY(24),
	 CPUENTRY(25),
	 CPUENTRY(26),
	 CPUENTRY(27),
	 CPUENTRY(28),
	 CPUENTRY(29),
	 CPUENTRY(30),
	 CPUENTRY(31),
	 {-1, __va(spi.root_properties + 3) /* NULL, NULL, -1 */ }
	 },
	"device_type",
	"cpu",
	"mid",
	"idprom",
	"compatability",
	"leon2",
	"mmu-nctx",
	"clock-frequency",
	"uart1_baud",
	"uart2_baud",
	CONFIG_DEFAULT_KERNEL_COMMAND_LINE
};

/* from arch/sparc/kernel/setup.c */
#define RAMDISK_LOAD_FLAG 0x4000
extern unsigned short root_flags;
extern unsigned short root_dev;
extern unsigned short ram_flags;
extern unsigned int sparc_ramdisk_image;
extern unsigned int sparc_ramdisk_size;
extern int root_mountflags;

extern char initrd_end, initrd_start;

/* Reboot the CPU = jump to beginning of flash again.
 *
 * Make sure that all function are inlined here.
 */
static void PROM_TEXT leon_reboot(char *bcommand)
{
	register char *arg = bcommand;
	void __attribute__ ((noreturn)) (*reboot_physical) (char *cmd);

	/* get physical address */
	struct leon_prom_info *pspi =
	    (void *)(CFG_PROM_OFFSET + sizeof(srmmu_tables));

	unsigned int *srmmu_ctx_table;

	/* Turn of Interrupts */
	set_pil(0xf);

	/* Set kernel's context, context zero */
	srmmu_set_context(0);

	/* Get physical address of the MMU shutdown routine */
	reboot_physical = (void *)
	    SPARC_BYPASS_READ(&pspi->reloc_funcs.reboot_physical);

	/* Now that we know the physical address of the function
	 * we can make the MMU allow jumping to it.
	 */
	srmmu_ctx_table = (unsigned int *)srmmu_get_ctable_ptr();

	srmmu_ctx_table = (unsigned int *)SPARC_BYPASS_READ(srmmu_ctx_table);

	/* get physical address of kernel's context table (assume ptd) */
	srmmu_ctx_table = (unsigned int *)
	    (((unsigned int)srmmu_ctx_table & 0xfffffffc) << 4);

	/* enable access to physical address of MMU shutdown function */
	SPARC_BYPASS_WRITE(&srmmu_ctx_table
			   [((unsigned int)reboot_physical) >> 24],
			   (((unsigned int)reboot_physical & 0xff000000) >> 4) |
			   0x1e);

	/* flush TLB cache */
	leon_flush_tlb_all();

	/* flash instruction & data cache */
	sparc_icache_flush_all();
	sparc_dcache_flush_all();

	/* jump to physical address function
	 * so that when the MMU is disabled
	 * we can continue to execute
	 */
	reboot_physical(arg);
}

static void PROM_TEXT leon_reboot_physical(char *bcommand)
{
	void __attribute__ ((noreturn)) (*reset) (void);

	/* Turn off MMU */
	srmmu_set_mmureg(0);

	/* Hardcoded start address */
	reset = CFG_MONITOR_BASE;

	/* flush data cache */
	sparc_dcache_flush_all();

	/* flush instruction cache */
	sparc_icache_flush_all();

	/* Jump to start in Flash */
	reset();
}

static void PROM_TEXT leon_halt(void)
{
	while (1) ;
}

/* get single char, don't care for blocking*/
static int PROM_TEXT leon_nbgetchar(void)
{
	return -1;
}

/* put single char, don't care for blocking*/
static int PROM_TEXT leon_nbputchar(int c)
{
	LEON2_regs *leon2 = (LEON2_regs *) LEON2_PREGS;

	/***** put char in buffer... ***********
	 * Make sure all functions are inline! *
	 ***************************************/

	/* Wait for last character to go. */
	while (!(SPARC_BYPASS_READ(&leon2->UART_Status_1)
		 & LEON2_UART_STAT_THE)) ;

	/* Send data */
	SPARC_BYPASS_WRITE(&leon2->UART_Channel_1, c);

	/* Wait for data to be sent */
	while (!(SPARC_BYPASS_READ(&leon2->UART_Status_1)
		 & LEON2_UART_STAT_TSE)) ;

	return 0;
}

/* node ops */

/*#define nodes ((struct node *)__va(&pspi->nodes))*/
#define nodes ((struct node *)(pspi->nodes))

static int PROM_TEXT no_nextnode(int node)
{
	/* get physical address */
	struct leon_prom_info *pspi =
	    (void *)(CFG_PROM_OFFSET + sizeof(srmmu_tables));

	/* convert into virtual address */
	pspi = (struct leon_prom_info *)
	    (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);

	if (nodes[node].level == nodes[node + 1].level)
		return node + 1;
	return -1;
}

static int PROM_TEXT no_child(int node)
{
	/* get physical address */
	struct leon_prom_info *pspi = (struct leon_prom_info *)
	    (CFG_PROM_OFFSET + sizeof(srmmu_tables));

	/* convert into virtual address */
	pspi = (struct leon_prom_info *)
	    (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);

	if (nodes[node].level == nodes[node + 1].level - 1)
		return node + 1;
	return -1;
}

static struct property PROM_TEXT *find_property(int node, char *name)
{
	/* get physical address */
	struct leon_prom_info *pspi = (struct leon_prom_info *)
	    (CFG_PROM_OFFSET + sizeof(srmmu_tables));

	/* convert into virtual address */
	pspi = (struct leon_prom_info *)
	    (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);

	struct property *prop = &nodes[node].properties[0];
	while (prop && prop->name) {
		if (pspi->reloc_funcs.strcmp(prop->name, name) == 0)
			return prop;
		prop++;
	}
	return NULL;
}

static int PROM_TEXT no_proplen(int node, char *name)
{
	/* get physical address */
	struct leon_prom_info *pspi = (struct leon_prom_info *)
	    (CFG_PROM_OFFSET + sizeof(srmmu_tables));

	/* convert into virtual address */
	pspi = (struct leon_prom_info *)
	    (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);

	struct property *prop = pspi->reloc_funcs.find_property(node, name);
	if (prop)
		return prop->length;
	return -1;
}

static int PROM_TEXT no_getprop(int node, char *name, char *value)
{
	/* get physical address */
	struct leon_prom_info *pspi = (struct leon_prom_info *)
	    (CFG_PROM_OFFSET + sizeof(srmmu_tables));

	/* convert into virtual address */
	pspi = (struct leon_prom_info *)
	    (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);

	struct property *prop = pspi->reloc_funcs.find_property(node, name);
	if (prop) {
		pspi->reloc_funcs.memcpy(value, prop->value, prop->length);
		return 1;
	}
	return -1;
}

static int PROM_TEXT no_setprop(int node, char *name, char *value, int len)
{
	return -1;
}

static char PROM_TEXT *no_nextprop(int node, char *name)
{
	/* get physical address */
	struct leon_prom_info *pspi = (struct leon_prom_info *)
	    (CFG_PROM_OFFSET + sizeof(srmmu_tables));
	struct property *prop;

	/* convert into virtual address */
	pspi = (struct leon_prom_info *)
	    (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);

	if (!name || !name[0])
		return nodes[node].properties[0].name;

	prop = pspi->reloc_funcs.find_property(node, name);
	if (prop)
		return prop[1].name;
	return NULL;
}

static int PROM_TEXT leon_strcmp(const char *s1, const char *s2)
{
	register char result;

	while (1) {
		result = *s1 - *s2;
		if (result || !*s1)
			break;
		s2++;
		s1++;
	}

	return result;
}

static void *PROM_TEXT leon_memcpy(void *dest, const void *src, size_t n)
{
	char *dst = (char *)dest, *source = (char *)src;

	while (n--) {
		*dst = *source;
		dst++;
		source++;
	}
	return dest;
}

#define GETREGSP(sp) __asm__ __volatile__("mov %%sp, %0" : "=r" (sp))

void leon_prom_init(struct leon_prom_info *pspi)
{
	unsigned long i;
	unsigned char cksum, *ptr;
	char *addr_str, *end;
	unsigned long sp;
	GETREGSP(sp);

	pspi->freq_khz = CONFIG_SYS_CLK_FREQ / 1000;

	/* Set Available main memory size */
	pspi->totphys.num_bytes = CFG_PROM_OFFSET - CFG_SDRAM_BASE;
	pspi->avail.num_bytes = pspi->totphys.num_bytes;

#undef nodes
	pspi->nodes[3].level = -1;
	pspi->nodes[3].properties = __va(spi.root_properties + 3);

	/* Set Ethernet MAC address from environment */
	if ((addr_str = getenv("ethaddr")) != NULL) {
		for (i = 0; i < 6; i++) {
			pspi->idprom.id_ethaddr[i] = addr_str ?
			    simple_strtoul(addr_str, &end, 16) : 0;
			if (addr_str) {
				addr_str = (*end) ? end + 1 : end;
			}
		}
	} else {
		/* HW Address not found in environment,
		 * Set default HW address
		 */
		pspi->idprom.id_ethaddr[0] = 0;
		pspi->idprom.id_ethaddr[1] = 0;
		pspi->idprom.id_ethaddr[2] = 0;
		pspi->idprom.id_ethaddr[3] = 0;
		pspi->idprom.id_ethaddr[4] = 0;
		pspi->idprom.id_ethaddr[5] = 0;
	}

	ptr = (unsigned char *)&pspi->idprom;
	for (i = cksum = 0; i <= 0x0E; i++)
		cksum ^= *ptr++;
	pspi->idprom.id_cksum = cksum;
}

static inline void set_cache(unsigned long regval)
{
	asm volatile ("sta %0, [%%g0] %1\n\t":: "r" (regval), "i"(2):"memory");
}

extern unsigned short bss_start, bss_end;

/* mark as section .img.main.text, to be referenced in linker script */
int prom_init(void)
{
	struct leon_prom_info *pspi = (void *)
	    ((((unsigned int)&spi) & PROM_SIZE_MASK) + CFG_PROM_OFFSET);

	/* disable mmu */
	srmmu_set_mmureg(0x00000000);
	__asm__ __volatile__("flush\n\t");

	/* init prom info struct */
	leon_prom_init(pspi);

	kernel_arg_promvec = &pspi->romvec;
#ifdef PRINT_ROM_VEC
	printf("Kernel rom vec: 0x%lx\n", (unsigned int)(&pspi->romvec));
#endif
	return 0;
}

/* Copy current kernel boot argument to ROMvec */
void prepare_bootargs(char *bootargs)
{
	struct leon_prom_info *pspi;
	char *src, *dst;
	int left;

	/* if no bootargs set, skip copying ==> default bootline */
	if (bootargs && (*bootargs != '\0')) {
		pspi = (void *)((((unsigned int)&spi) & PROM_SIZE_MASK) +
				CFG_PROM_OFFSET);
		src = bootargs;
		dst = &pspi->arg[0];
		left = 255;	/* max len */
		while (*src && left > 0) {
			*dst++ = *src++;
			left--;
		}
		/* terminate kernel command line string */
		*dst = 0;
	}
}

void srmmu_init_cpu(unsigned int entry)
{
	sparc_srmmu_setup *psrmmu_tables = (void *)
	    ((((unsigned int)&srmmu_tables) & PROM_SIZE_MASK) +
	     CFG_PROM_OFFSET);

	/* Make context 0 (kernel's context) point
	 * to our prepared memory mapping
	 */
#define PTD 1
	psrmmu_tables->ctx_table[0] =
	    ((unsigned int)&psrmmu_tables->pgd_table[0x00]) >> 4 | PTD;

	/* Set virtual kernel address 0xf0000000
	 * to SRAM/SDRAM address.
	 * Make it READ/WRITE/EXEC to SuperUser
	 */
#define PTE 2
#define ACC_SU_ALL 0x1c
	psrmmu_tables->pgd_table[0xf0] =
	    (CFG_SDRAM_BASE >> 4) | ACC_SU_ALL | PTE;
	psrmmu_tables->pgd_table[0xf1] =
	    ((CFG_SDRAM_BASE + 0x1000000) >> 4) | ACC_SU_ALL | PTE;
	psrmmu_tables->pgd_table[0xf2] =
	    ((CFG_SDRAM_BASE + 0x2000000) >> 4) | ACC_SU_ALL | PTE;
	psrmmu_tables->pgd_table[0xf3] =
	    ((CFG_SDRAM_BASE + 0x3000000) >> 4) | ACC_SU_ALL | PTE;
	psrmmu_tables->pgd_table[0xf4] =
	    ((CFG_SDRAM_BASE + 0x4000000) >> 4) | ACC_SU_ALL | PTE;
	psrmmu_tables->pgd_table[0xf5] =
	    ((CFG_SDRAM_BASE + 0x5000000) >> 4) | ACC_SU_ALL | PTE;
	psrmmu_tables->pgd_table[0xf6] =
	    ((CFG_SDRAM_BASE + 0x6000000) >> 4) | ACC_SU_ALL | PTE;
	psrmmu_tables->pgd_table[0xf7] =
	    ((CFG_SDRAM_BASE + 0x7000000) >> 4) | ACC_SU_ALL | PTE;

	/* convert rom vec pointer to virtual address */
	kernel_arg_promvec = (struct linux_romvec *)
	    (((unsigned int)kernel_arg_promvec & 0x0fffffff) | 0xf0000000);

	/* Set Context pointer to point to context table
	 * 256 contexts supported.
	 */
	srmmu_set_ctable_ptr((unsigned int)&psrmmu_tables->ctx_table[0]);

	/* Set kernel's context, context zero */
	srmmu_set_context(0);

	/* Invalidate all Cache */
	__asm__ __volatile__("flush\n\t");

	srmmu_set_mmureg(0x00000001);
	leon_flush_tlb_all();
	leon_flush_cache_all();
}