1 /* prom.c - emulates a sparc v0 PROM for the linux kernel.
3 * Copyright (C) 2003 Konrad Eisele <eiselekd@web.de>
4 * Copyright (C) 2004 Stefan Holst <mail@s-holst.de>
5 * Copyright (C) 2007 Daniel Hellstrom <daniel@gaisler.com>
7 * See file CREDITS for list of people who contributed to this
10 * This program is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU General Public License as
12 * published by the Free Software Foundation; either version 2 of
13 * the License, or (at your option) any later version.
15 * This program is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
20 * You should have received a copy of the GNU General Public License
21 * along with this program; if not, write to the Free Software
22 * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 #include <asm/machines.h>
30 #include <asm/srmmu.h>
31 #include <asm/processor.h>
39 extern struct linux_romvec
*kernel_arg_promvec
;
41 #define PROM_PGT __attribute__ ((__section__ (".prom.pgt")))
42 #define PROM_TEXT __attribute__ ((__section__ (".prom.text")))
43 #define PROM_DATA __attribute__ ((__section__ (".prom.data")))
46 extern int __prom_start
;
47 #define PAGE_OFFSET 0xf0000000
48 #define phys_base CONFIG_SYS_SDRAM_BASE
49 #define PROM_OFFS 8192
50 #define PROM_SIZE_MASK (PROM_OFFS-1)
52 (void *)( ((unsigned long)(x))-PROM_OFFS+ \
53 (CONFIG_SYS_PROM_OFFSET-phys_base)+PAGE_OFFSET-TEXT_BASE ) \
55 #define __phy(x) ((void *)(((unsigned long)(x))-PROM_OFFS+CONFIG_SYS_PROM_OFFSET-TEXT_BASE))
65 struct property
*properties
;
68 static void leon_reboot(char *bcommand
);
69 static void leon_halt(void);
70 static int leon_nbputchar(int c
);
71 static int leon_nbgetchar(void);
73 static int no_nextnode(int node
);
74 static int no_child(int node
);
75 static int no_proplen(int node
, char *name
);
76 static int no_getprop(int node
, char *name
, char *value
);
77 static int no_setprop(int node
, char *name
, char *value
, int len
);
78 static char *no_nextprop(int node
, char *name
);
80 static struct property PROM_TEXT
*find_property(int node
, char *name
);
81 static int PROM_TEXT
leon_strcmp(const char *s1
, const char *s2
);
82 static void *PROM_TEXT
leon_memcpy(void *dest
, const void *src
, size_t n
);
83 static void PROM_TEXT
leon_reboot_physical(char *bcommand
);
85 void __inline__
leon_flush_cache_all(void)
87 __asm__
__volatile__(" flush ");
88 __asm__
__volatile__("sta %%g0, [%%g0] %0\n\t"::"i"(ASI_DFLUSH
):"memory");
91 void __inline__
leon_flush_tlb_all(void)
93 leon_flush_cache_all();
94 __asm__
__volatile__("sta %%g0, [%0] %1\n\t"::"r"(0x400),
95 "i"(ASI_MMUFLUSH
):"memory");
99 unsigned int ctx_table
[256];
100 unsigned int pgd_table
[256];
103 sparc_srmmu_setup srmmu_tables PROM_PGT
= {
345 0x400001e /* default */
349 /* a self contained prom info structure */
350 struct leon_reloc_func
{
351 struct property
*(*find_property
) (int node
, char *name
);
352 int (*strcmp
) (char *s1
, char *s2
);
353 void *(*memcpy
) (void *dest
, const void *src
, size_t n
);
354 void (*reboot_physical
) (char *cmd
);
357 struct leon_prom_info
{
362 struct leon_reloc_func reloc_funcs
;
363 struct property root_properties
[4];
364 struct property cpu_properties
[7];
366 #define CPUENTRY(idx) struct property cpu_properties##idx[4]
398 struct idprom idprom
;
399 struct linux_nodeops nodeops
;
400 struct linux_mlist_v0
*totphys_p
;
401 struct linux_mlist_v0 totphys
;
402 struct linux_mlist_v0
*avail_p
;
403 struct linux_mlist_v0 avail
;
404 struct linux_mlist_v0
*prommap_p
;
405 void (*synchook
) (void);
406 struct linux_arguments_v0
*bootargs_p
;
407 struct linux_arguments_v0 bootargs
;
408 struct linux_romvec romvec
;
409 struct node nodes
[35];
410 char s_device_type
[12];
414 char s_compatability
[14];
417 char s_frequency
[16];
418 char s_uart1_baud
[11];
419 char s_uart2_baud
[11];
423 /* static prom info */
424 static struct leon_prom_info PROM_DATA spi
= {
425 CONFIG_SYS_CLK_FREQ
/ 1000,
429 #define CPUENTRY(idx) idx
467 __phy(leon_reboot_physical
),
470 {__va(spi
.s_device_type
), __va(spi
.s_idprom
), 4},
471 {__va(spi
.s_idprom
), (char *)__va(&spi
.idprom
), sizeof(struct idprom
)},
472 {__va(spi
.s_compatability
), __va(spi
.s_leon2
), 5},
476 {__va(spi
.s_device_type
), __va(spi
.s_cpu
), 4},
477 {__va(spi
.s_mid
), __va(&spi
.mids
[0]), 4},
478 {__va(spi
.s_mmu_nctx
), (char *)__va(&spi
.leon_nctx
), 4},
479 {__va(spi
.s_frequency
), (char *)__va(&spi
.freq_khz
), 4},
480 {__va(spi
.s_uart1_baud
), (char *)__va(&spi
.baudrates
[0]), 4},
481 {__va(spi
.s_uart2_baud
), (char *)__va(&spi
.baudrates
[1]), 4},
485 #define CPUENTRY(idx) \
486 { /* cpu_properties */ \
487 {__va(spi.s_device_type), __va(spi.s_cpu), 4}, \
488 {__va(spi.s_mid), __va(&spi.mids[idx]), 4}, \
489 {__va(spi.s_frequency), (char *)__va(&spi.freq_khz), 4}, \
525 M_LEON2
| M_LEON2_SOC
, /* machine type */
526 {0, 0, 0, 0, 0, 0}, /* eth */
530 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* reserved */
543 (char *)CONFIG_SYS_SDRAM_BASE
,
549 (char *)CONFIG_SYS_SDRAM_BASE
,
552 NULL
, /* prommap_p */
556 {NULL
, __va(spi
.arg
), NULL
/*... */ },
561 0, /* sun4c v0 prom */
563 {__va(&spi
.totphys_p
), __va(&spi
.prommap_p
), __va(&spi
.avail_p
)},
565 NULL
, {NULL
/* ... */ },
567 NULL
, NULL
, /* pv_getchar, pv_putchar */
568 __va(leon_nbgetchar
), __va(leon_nbputchar
),
577 __va(&spi
.bootargs_p
)
581 {0, __va(spi
.root_properties
+ 3) /* NULL, NULL, -1 */ },
582 {0, __va(spi
.root_properties
)},
583 /* cpu 0, must be spi.nodes[2] see leon_prom_init() */
584 {1, __va(spi
.cpu_properties
)},
587 #define CPUENTRY(idx) \
588 {1, __va(spi.cpu_properties##idx) } /* cpu <idx> */
620 {-1, __va(spi
.root_properties
+ 3) /* NULL, NULL, -1 */ }
632 CONFIG_DEFAULT_KERNEL_COMMAND_LINE
635 /* from arch/sparc/kernel/setup.c */
636 #define RAMDISK_LOAD_FLAG 0x4000
637 extern unsigned short root_flags
;
638 extern unsigned short root_dev
;
639 extern unsigned short ram_flags
;
640 extern unsigned int sparc_ramdisk_image
;
641 extern unsigned int sparc_ramdisk_size
;
642 extern int root_mountflags
;
644 extern char initrd_end
, initrd_start
;
646 /* Reboot the CPU = jump to beginning of flash again.
648 * Make sure that all function are inlined here.
650 static void PROM_TEXT
leon_reboot(char *bcommand
)
652 register char *arg
= bcommand
;
653 void __attribute__ ((noreturn
)) (*reboot_physical
) (char *cmd
);
655 /* get physical address */
656 struct leon_prom_info
*pspi
=
657 (void *)(CONFIG_SYS_PROM_OFFSET
+ sizeof(srmmu_tables
));
659 unsigned int *srmmu_ctx_table
;
661 /* Turn of Interrupts */
664 /* Set kernel's context, context zero */
665 srmmu_set_context(0);
667 /* Get physical address of the MMU shutdown routine */
668 reboot_physical
= (void *)
669 SPARC_BYPASS_READ(&pspi
->reloc_funcs
.reboot_physical
);
671 /* Now that we know the physical address of the function
672 * we can make the MMU allow jumping to it.
674 srmmu_ctx_table
= (unsigned int *)srmmu_get_ctable_ptr();
676 srmmu_ctx_table
= (unsigned int *)SPARC_BYPASS_READ(srmmu_ctx_table
);
678 /* get physical address of kernel's context table (assume ptd) */
679 srmmu_ctx_table
= (unsigned int *)
680 (((unsigned int)srmmu_ctx_table
& 0xfffffffc) << 4);
682 /* enable access to physical address of MMU shutdown function */
683 SPARC_BYPASS_WRITE(&srmmu_ctx_table
684 [((unsigned int)reboot_physical
) >> 24],
685 (((unsigned int)reboot_physical
& 0xff000000) >> 4) |
688 /* flush TLB cache */
689 leon_flush_tlb_all();
691 /* flash instruction & data cache */
692 sparc_icache_flush_all();
693 sparc_dcache_flush_all();
695 /* jump to physical address function
696 * so that when the MMU is disabled
697 * we can continue to execute
699 reboot_physical(arg
);
702 static void PROM_TEXT
leon_reboot_physical(char *bcommand
)
704 void __attribute__ ((noreturn
)) (*reset
) (void);
709 /* Hardcoded start address */
710 reset
= CONFIG_SYS_MONITOR_BASE
;
712 /* flush data cache */
713 sparc_dcache_flush_all();
715 /* flush instruction cache */
716 sparc_icache_flush_all();
718 /* Jump to start in Flash */
722 static void PROM_TEXT
leon_halt(void)
727 /* get single char, don't care for blocking*/
728 static int PROM_TEXT
leon_nbgetchar(void)
733 /* put single char, don't care for blocking*/
734 static int PROM_TEXT
leon_nbputchar(int c
)
736 LEON2_regs
*leon2
= (LEON2_regs
*) LEON2_PREGS
;
738 /***** put char in buffer... ***********
739 * Make sure all functions are inline! *
740 ***************************************/
742 /* Wait for last character to go. */
743 while (!(SPARC_BYPASS_READ(&leon2
->UART_Status_1
)
744 & LEON2_UART_STAT_THE
)) ;
747 SPARC_BYPASS_WRITE(&leon2
->UART_Channel_1
, c
);
749 /* Wait for data to be sent */
750 while (!(SPARC_BYPASS_READ(&leon2
->UART_Status_1
)
751 & LEON2_UART_STAT_TSE
)) ;
758 /*#define nodes ((struct node *)__va(&pspi->nodes))*/
759 #define nodes ((struct node *)(pspi->nodes))
761 static int PROM_TEXT
no_nextnode(int node
)
763 /* get physical address */
764 struct leon_prom_info
*pspi
=
765 (void *)(CONFIG_SYS_PROM_OFFSET
+ sizeof(srmmu_tables
));
767 /* convert into virtual address */
768 pspi
= (struct leon_prom_info
*)
769 (((unsigned int)pspi
& 0x0fffffff) | PAGE_OFFSET
);
771 if (nodes
[node
].level
== nodes
[node
+ 1].level
)
776 static int PROM_TEXT
no_child(int node
)
778 /* get physical address */
779 struct leon_prom_info
*pspi
= (struct leon_prom_info
*)
780 (CONFIG_SYS_PROM_OFFSET
+ sizeof(srmmu_tables
));
782 /* convert into virtual address */
783 pspi
= (struct leon_prom_info
*)
784 (((unsigned int)pspi
& 0x0fffffff) | PAGE_OFFSET
);
786 if (nodes
[node
].level
== nodes
[node
+ 1].level
- 1)
791 static struct property PROM_TEXT
*find_property(int node
, char *name
)
793 /* get physical address */
794 struct leon_prom_info
*pspi
= (struct leon_prom_info
*)
795 (CONFIG_SYS_PROM_OFFSET
+ sizeof(srmmu_tables
));
797 /* convert into virtual address */
798 pspi
= (struct leon_prom_info
*)
799 (((unsigned int)pspi
& 0x0fffffff) | PAGE_OFFSET
);
801 struct property
*prop
= &nodes
[node
].properties
[0];
802 while (prop
&& prop
->name
) {
803 if (pspi
->reloc_funcs
.strcmp(prop
->name
, name
) == 0)
810 static int PROM_TEXT
no_proplen(int node
, char *name
)
812 /* get physical address */
813 struct leon_prom_info
*pspi
= (struct leon_prom_info
*)
814 (CONFIG_SYS_PROM_OFFSET
+ sizeof(srmmu_tables
));
816 /* convert into virtual address */
817 pspi
= (struct leon_prom_info
*)
818 (((unsigned int)pspi
& 0x0fffffff) | PAGE_OFFSET
);
820 struct property
*prop
= pspi
->reloc_funcs
.find_property(node
, name
);
826 static int PROM_TEXT
no_getprop(int node
, char *name
, char *value
)
828 /* get physical address */
829 struct leon_prom_info
*pspi
= (struct leon_prom_info
*)
830 (CONFIG_SYS_PROM_OFFSET
+ sizeof(srmmu_tables
));
832 /* convert into virtual address */
833 pspi
= (struct leon_prom_info
*)
834 (((unsigned int)pspi
& 0x0fffffff) | PAGE_OFFSET
);
836 struct property
*prop
= pspi
->reloc_funcs
.find_property(node
, name
);
838 pspi
->reloc_funcs
.memcpy(value
, prop
->value
, prop
->length
);
844 static int PROM_TEXT
no_setprop(int node
, char *name
, char *value
, int len
)
849 static char PROM_TEXT
*no_nextprop(int node
, char *name
)
851 /* get physical address */
852 struct leon_prom_info
*pspi
= (struct leon_prom_info
*)
853 (CONFIG_SYS_PROM_OFFSET
+ sizeof(srmmu_tables
));
854 struct property
*prop
;
856 /* convert into virtual address */
857 pspi
= (struct leon_prom_info
*)
858 (((unsigned int)pspi
& 0x0fffffff) | PAGE_OFFSET
);
860 if (!name
|| !name
[0])
861 return nodes
[node
].properties
[0].name
;
863 prop
= pspi
->reloc_funcs
.find_property(node
, name
);
869 static int PROM_TEXT
leon_strcmp(const char *s1
, const char *s2
)
871 register char result
;
884 static void *PROM_TEXT
leon_memcpy(void *dest
, const void *src
, size_t n
)
886 char *dst
= (char *)dest
, *source
= (char *)src
;
896 #define GETREGSP(sp) __asm__ __volatile__("mov %%sp, %0" : "=r" (sp))
898 void leon_prom_init(struct leon_prom_info
*pspi
)
901 unsigned char cksum
, *ptr
;
902 char *addr_str
, *end
;
906 pspi
->freq_khz
= CONFIG_SYS_CLK_FREQ
/ 1000;
908 /* Set Available main memory size */
909 pspi
->totphys
.num_bytes
= CONFIG_SYS_PROM_OFFSET
- CONFIG_SYS_SDRAM_BASE
;
910 pspi
->avail
.num_bytes
= pspi
->totphys
.num_bytes
;
913 pspi
->nodes
[3].level
= -1;
914 pspi
->nodes
[3].properties
= __va(spi
.root_properties
+ 3);
916 /* Set Ethernet MAC address from environment */
917 if ((addr_str
= getenv("ethaddr")) != NULL
) {
918 for (i
= 0; i
< 6; i
++) {
919 pspi
->idprom
.id_ethaddr
[i
] = addr_str
?
920 simple_strtoul(addr_str
, &end
, 16) : 0;
922 addr_str
= (*end
) ? end
+ 1 : end
;
926 /* HW Address not found in environment,
927 * Set default HW address
929 pspi
->idprom
.id_ethaddr
[0] = 0;
930 pspi
->idprom
.id_ethaddr
[1] = 0;
931 pspi
->idprom
.id_ethaddr
[2] = 0;
932 pspi
->idprom
.id_ethaddr
[3] = 0;
933 pspi
->idprom
.id_ethaddr
[4] = 0;
934 pspi
->idprom
.id_ethaddr
[5] = 0;
937 ptr
= (unsigned char *)&pspi
->idprom
;
938 for (i
= cksum
= 0; i
<= 0x0E; i
++)
940 pspi
->idprom
.id_cksum
= cksum
;
943 static inline void set_cache(unsigned long regval
)
945 asm volatile ("sta %0, [%%g0] %1\n\t":: "r" (regval
), "i"(2):"memory");
948 extern unsigned short bss_start
, bss_end
;
950 /* mark as section .img.main.text, to be referenced in linker script */
953 struct leon_prom_info
*pspi
= (void *)
954 ((((unsigned int)&spi
) & PROM_SIZE_MASK
) + CONFIG_SYS_PROM_OFFSET
);
957 srmmu_set_mmureg(0x00000000);
958 __asm__
__volatile__("flush\n\t");
960 /* init prom info struct */
961 leon_prom_init(pspi
);
963 kernel_arg_promvec
= &pspi
->romvec
;
965 printf("Kernel rom vec: 0x%lx\n", (unsigned int)(&pspi
->romvec
));
970 /* Copy current kernel boot argument to ROMvec */
971 void prepare_bootargs(char *bootargs
)
973 struct leon_prom_info
*pspi
;
977 /* if no bootargs set, skip copying ==> default bootline */
978 if (bootargs
&& (*bootargs
!= '\0')) {
979 pspi
= (void *)((((unsigned int)&spi
) & PROM_SIZE_MASK
) +
980 CONFIG_SYS_PROM_OFFSET
);
983 left
= 255; /* max len */
984 while (*src
&& left
> 0) {
988 /* terminate kernel command line string */
993 void srmmu_init_cpu(unsigned int entry
)
995 sparc_srmmu_setup
*psrmmu_tables
= (void *)
996 ((((unsigned int)&srmmu_tables
) & PROM_SIZE_MASK
) +
997 CONFIG_SYS_PROM_OFFSET
);
999 /* Make context 0 (kernel's context) point
1000 * to our prepared memory mapping
1003 psrmmu_tables
->ctx_table
[0] =
1004 ((unsigned int)&psrmmu_tables
->pgd_table
[0x00]) >> 4 | PTD
;
1006 /* Set virtual kernel address 0xf0000000
1007 * to SRAM/SDRAM address.
1008 * Make it READ/WRITE/EXEC to SuperUser
1011 #define ACC_SU_ALL 0x1c
1012 psrmmu_tables
->pgd_table
[0xf0] =
1013 (CONFIG_SYS_SDRAM_BASE
>> 4) | ACC_SU_ALL
| PTE
;
1014 psrmmu_tables
->pgd_table
[0xf1] =
1015 ((CONFIG_SYS_SDRAM_BASE
+ 0x1000000) >> 4) | ACC_SU_ALL
| PTE
;
1016 psrmmu_tables
->pgd_table
[0xf2] =
1017 ((CONFIG_SYS_SDRAM_BASE
+ 0x2000000) >> 4) | ACC_SU_ALL
| PTE
;
1018 psrmmu_tables
->pgd_table
[0xf3] =
1019 ((CONFIG_SYS_SDRAM_BASE
+ 0x3000000) >> 4) | ACC_SU_ALL
| PTE
;
1020 psrmmu_tables
->pgd_table
[0xf4] =
1021 ((CONFIG_SYS_SDRAM_BASE
+ 0x4000000) >> 4) | ACC_SU_ALL
| PTE
;
1022 psrmmu_tables
->pgd_table
[0xf5] =
1023 ((CONFIG_SYS_SDRAM_BASE
+ 0x5000000) >> 4) | ACC_SU_ALL
| PTE
;
1024 psrmmu_tables
->pgd_table
[0xf6] =
1025 ((CONFIG_SYS_SDRAM_BASE
+ 0x6000000) >> 4) | ACC_SU_ALL
| PTE
;
1026 psrmmu_tables
->pgd_table
[0xf7] =
1027 ((CONFIG_SYS_SDRAM_BASE
+ 0x7000000) >> 4) | ACC_SU_ALL
| PTE
;
1029 /* convert rom vec pointer to virtual address */
1030 kernel_arg_promvec
= (struct linux_romvec
*)
1031 (((unsigned int)kernel_arg_promvec
& 0x0fffffff) | 0xf0000000);
1033 /* Set Context pointer to point to context table
1034 * 256 contexts supported.
1036 srmmu_set_ctable_ptr((unsigned int)&psrmmu_tables
->ctx_table
[0]);
1038 /* Set kernel's context, context zero */
1039 srmmu_set_context(0);
1041 /* Invalidate all Cache */
1042 __asm__
__volatile__("flush\n\t");
1044 srmmu_set_mmureg(0x00000001);
1045 leon_flush_tlb_all();
1046 leon_flush_cache_all();