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 * SPDX-License-Identifier: GPL-2.0+
12 #include <asm/machines.h>
13 #include <asm/srmmu.h>
14 #include <asm/processor.h>
18 #include <grlib/apbuart.h>
19 #include <grlib/irqmp.h>
20 #include <grlib/gptimer.h>
26 extern struct linux_romvec *kernel_arg_promvec;
28 DECLARE_GLOBAL_DATA_PTR;
30 #define PROM_PGT __attribute__ ((__section__ (".prom.pgt")))
31 #define PROM_TEXT __attribute__ ((__section__ (".prom.text")))
32 #define PROM_DATA __attribute__ ((__section__ (".prom.data")))
34 ambapp_dev_gptimer *gptimer;
37 extern int __prom_start;
38 #define PAGE_OFFSET 0xf0000000
39 #define phys_base CONFIG_SYS_SDRAM_BASE
40 #define PROM_OFFS 8192
41 #define PROM_SIZE_MASK (PROM_OFFS-1)
43 (void *)( ((unsigned long)(x))-PROM_OFFS+ \
44 (CONFIG_SYS_PROM_OFFSET-phys_base)+PAGE_OFFSET-CONFIG_SYS_TEXT_BASE ) \
46 #define __phy(x) ((void *)(((unsigned long)(x))-PROM_OFFS+CONFIG_SYS_PROM_OFFSET-CONFIG_SYS_TEXT_BASE))
56 struct property *properties;
59 static void leon_reboot(char *bcommand);
60 static void leon_halt(void);
61 static int leon_nbputchar(int c);
62 static int leon_nbgetchar(void);
64 static int no_nextnode(int node);
65 static int no_child(int node);
66 static int no_proplen(int node, char *name);
67 static int no_getprop(int node, char *name, char *value);
68 static int no_setprop(int node, char *name, char *value, int len);
69 static char *no_nextprop(int node, char *name);
71 static struct property PROM_TEXT *find_property(int node, char *name);
72 static int PROM_TEXT leon_strcmp(const char *s1, const char *s2);
73 static void *PROM_TEXT leon_memcpy(void *dest, const void *src, size_t n);
74 static void PROM_TEXT leon_reboot_physical(char *bcommand);
76 void __inline__ leon_flush_cache_all(void)
78 __asm__ __volatile__(" flush ");
79 __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t"::"i"(ASI_DFLUSH):"memory");
82 void __inline__ leon_flush_tlb_all(void)
84 leon_flush_cache_all();
85 __asm__ __volatile__("sta %%g0, [%0] %1\n\t"::"r"(0x400),
86 "i"(ASI_MMUFLUSH):"memory");
90 unsigned int ctx_table[256];
91 unsigned int pgd_table[256];
94 sparc_srmmu_setup srmmu_tables PROM_PGT = {
336 0x400001e /* default */
340 /* a self contained prom info structure */
341 struct leon_reloc_func {
342 struct property *(*find_property) (int node, char *name);
343 int (*strcmp) (char *s1, char *s2);
344 void *(*memcpy) (void *dest, const void *src, size_t n);
345 void (*reboot_physical) (char *cmd);
346 ambapp_dev_apbuart *leon3_apbuart;
349 struct leon_prom_info {
354 struct leon_reloc_func reloc_funcs;
355 struct property root_properties[4];
356 struct property cpu_properties[7];
358 #define CPUENTRY(idx) struct property cpu_properties##idx[4]
390 struct idprom idprom;
391 struct linux_nodeops nodeops;
392 struct linux_mlist_v0 *totphys_p;
393 struct linux_mlist_v0 totphys;
394 struct linux_mlist_v0 *avail_p;
395 struct linux_mlist_v0 avail;
396 struct linux_mlist_v0 *prommap_p;
397 void (*synchook) (void);
398 struct linux_arguments_v0 *bootargs_p;
399 struct linux_arguments_v0 bootargs;
400 struct linux_romvec romvec;
401 struct node nodes[35];
402 char s_device_type[12];
406 char s_compatability[14];
409 char s_frequency[16];
410 char s_uart1_baud[11];
411 char s_uart2_baud[11];
415 /* static prom info */
416 static struct leon_prom_info PROM_DATA spi = {
417 CONFIG_SYS_CLK_FREQ / 1000,
421 #define CPUENTRY(idx) idx
459 __phy(leon_reboot_physical),
462 {__va(spi.s_device_type), __va(spi.s_idprom), 4},
463 {__va(spi.s_idprom), (char *)__va(&spi.idprom), sizeof(struct idprom)},
464 {__va(spi.s_compatability), __va(spi.s_leon2), 5},
468 {__va(spi.s_device_type), __va(spi.s_cpu), 4},
469 {__va(spi.s_mid), __va(&spi.mids[0]), 4},
470 {__va(spi.s_mmu_nctx), (char *)__va(&spi.leon_nctx), 4},
471 {__va(spi.s_frequency), (char *)__va(&spi.freq_khz), 4},
472 {__va(spi.s_uart1_baud), (char *)__va(&spi.baudrates[0]), 4},
473 {__va(spi.s_uart2_baud), (char *)__va(&spi.baudrates[1]), 4},
477 #define CPUENTRY(idx) \
478 { /* cpu_properties */ \
479 {__va(spi.s_device_type), __va(spi.s_cpu), 4}, \
480 {__va(spi.s_mid), __va(&spi.mids[idx]), 4}, \
481 {__va(spi.s_frequency), (char *)__va(&spi.freq_khz), 4}, \
517 M_LEON2 | M_LEON2_SOC, /* machine type */
518 {0, 0, 0, 0, 0, 0}, /* eth */
522 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* reserved */
535 (char *)CONFIG_SYS_SDRAM_BASE,
541 (char *)CONFIG_SYS_SDRAM_BASE,
544 NULL, /* prommap_p */
548 {NULL, __va(spi.arg), NULL /*... */ },
553 0, /* sun4c v0 prom */
555 {__va(&spi.totphys_p), __va(&spi.prommap_p), __va(&spi.avail_p)},
557 NULL, {NULL /* ... */ },
559 NULL, NULL, /* pv_getchar, pv_putchar */
560 __va(leon_nbgetchar), __va(leon_nbputchar),
569 __va(&spi.bootargs_p)
573 {0, __va(spi.root_properties + 3) /* NULL, NULL, -1 */ },
574 {0, __va(spi.root_properties)},
575 /* cpu 0, must be spi.nodes[2] see leon_prom_init() */
576 {1, __va(spi.cpu_properties)},
579 #define CPUENTRY(idx) \
580 {1, __va(spi.cpu_properties##idx) } /* cpu <idx> */
612 {-1, __va(spi.root_properties + 3) /* NULL, NULL, -1 */ }
624 CONFIG_DEFAULT_KERNEL_COMMAND_LINE
627 /* from arch/sparc/kernel/setup.c */
628 #define RAMDISK_LOAD_FLAG 0x4000
629 extern unsigned short root_flags;
630 extern unsigned short root_dev;
631 extern unsigned short ram_flags;
632 extern unsigned int sparc_ramdisk_image;
633 extern unsigned int sparc_ramdisk_size;
634 extern int root_mountflags;
636 extern char initrd_end, initrd_start;
638 /* Reboot the CPU = jump to beginning of flash again.
640 * Make sure that all function are inlined here.
642 static void PROM_TEXT leon_reboot(char *bcommand)
644 register char *arg = bcommand;
645 void __attribute__ ((noreturn)) (*reboot_physical) (char *cmd);
647 /* get physical address */
648 struct leon_prom_info *pspi =
649 (void *)(CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
651 unsigned int *srmmu_ctx_table;
653 /* Turn of Interrupts */
656 /* Set kernel's context, context zero */
657 srmmu_set_context(0);
659 /* Get physical address of the MMU shutdown routine */
660 reboot_physical = (void *)
661 SPARC_BYPASS_READ(&pspi->reloc_funcs.reboot_physical);
663 /* Now that we know the physical address of the function
664 * we can make the MMU allow jumping to it.
666 srmmu_ctx_table = (unsigned int *)srmmu_get_ctable_ptr();
668 srmmu_ctx_table = (unsigned int *)SPARC_BYPASS_READ(srmmu_ctx_table);
670 /* get physical address of kernel's context table (assume ptd) */
671 srmmu_ctx_table = (unsigned int *)
672 (((unsigned int)srmmu_ctx_table & 0xfffffffc) << 4);
674 /* enable access to physical address of MMU shutdown function */
675 SPARC_BYPASS_WRITE(&srmmu_ctx_table
676 [((unsigned int)reboot_physical) >> 24],
677 (((unsigned int)reboot_physical & 0xff000000) >> 4) |
680 /* flush TLB cache */
681 leon_flush_tlb_all();
683 /* flash instruction & data cache */
684 sparc_icache_flush_all();
685 sparc_dcache_flush_all();
687 /* jump to physical address function
688 * so that when the MMU is disabled
689 * we can continue to execute
691 reboot_physical(arg);
694 static void PROM_TEXT leon_reboot_physical(char *bcommand)
696 void __attribute__ ((noreturn)) (*reset) (void);
701 /* Hardcoded start address */
702 reset = CONFIG_SYS_MONITOR_BASE;
704 /* flush data cache */
705 sparc_dcache_flush_all();
707 /* flush instruction cache */
708 sparc_icache_flush_all();
710 /* Jump to start in Flash */
714 static void PROM_TEXT leon_halt(void)
719 /* get single char, don't care for blocking*/
720 static int PROM_TEXT leon_nbgetchar(void)
725 /* put single char, don't care for blocking*/
726 static int PROM_TEXT leon_nbputchar(int c)
728 ambapp_dev_apbuart *uart;
730 /* get physical address */
731 struct leon_prom_info *pspi =
732 (void *)(CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
734 uart = (ambapp_dev_apbuart *)
735 SPARC_BYPASS_READ(&pspi->reloc_funcs.leon3_apbuart);
741 /***** put char in buffer... ***********
742 * Make sure all functions are inline! *
743 ***************************************/
745 /* Wait for last character to go. */
746 while (!(SPARC_BYPASS_READ(&uart->status)
747 & APBUART_STATUS_THE));
750 SPARC_BYPASS_WRITE(&uart->data, c);
752 /* Wait for data to be sent */
753 while (!(SPARC_BYPASS_READ(&uart->status)
754 & APBUART_STATUS_TSE));
761 /*#define nodes ((struct node *)__va(&pspi->nodes))*/
762 #define nodes ((struct node *)(pspi->nodes))
764 static int PROM_TEXT no_nextnode(int node)
766 /* get physical address */
767 struct leon_prom_info *pspi =
768 (void *)(CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
770 /* convert into virtual address */
771 pspi = (struct leon_prom_info *)
772 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
774 if (nodes[node].level == nodes[node + 1].level)
779 static int PROM_TEXT no_child(int node)
781 /* get physical address */
782 struct leon_prom_info *pspi = (struct leon_prom_info *)
783 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
785 /* convert into virtual address */
786 pspi = (struct leon_prom_info *)
787 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
789 if (nodes[node].level == nodes[node + 1].level - 1)
794 static struct property PROM_TEXT *find_property(int node, char *name)
796 /* get physical address */
797 struct leon_prom_info *pspi = (struct leon_prom_info *)
798 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
800 /* convert into virtual address */
801 pspi = (struct leon_prom_info *)
802 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
804 struct property *prop = &nodes[node].properties[0];
805 while (prop && prop->name) {
806 if (pspi->reloc_funcs.strcmp(prop->name, name) == 0)
813 static int PROM_TEXT no_proplen(int node, char *name)
815 /* get physical address */
816 struct leon_prom_info *pspi = (struct leon_prom_info *)
817 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
819 /* convert into virtual address */
820 pspi = (struct leon_prom_info *)
821 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
823 struct property *prop = pspi->reloc_funcs.find_property(node, name);
829 static int PROM_TEXT no_getprop(int node, char *name, char *value)
831 /* get physical address */
832 struct leon_prom_info *pspi = (struct leon_prom_info *)
833 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
835 /* convert into virtual address */
836 pspi = (struct leon_prom_info *)
837 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
839 struct property *prop = pspi->reloc_funcs.find_property(node, name);
841 pspi->reloc_funcs.memcpy(value, prop->value, prop->length);
847 static int PROM_TEXT no_setprop(int node, char *name, char *value, int len)
852 static char PROM_TEXT *no_nextprop(int node, char *name)
854 /* get physical address */
855 struct leon_prom_info *pspi = (struct leon_prom_info *)
856 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
857 struct property *prop;
859 /* convert into virtual address */
860 pspi = (struct leon_prom_info *)
861 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
863 if (!name || !name[0])
864 return nodes[node].properties[0].name;
866 prop = pspi->reloc_funcs.find_property(node, name);
872 static int PROM_TEXT leon_strcmp(const char *s1, const char *s2)
874 register char result;
887 static void *PROM_TEXT leon_memcpy(void *dest, const void *src, size_t n)
889 char *dst = (char *)dest, *source = (char *)src;
899 #define GETREGSP(sp) __asm__ __volatile__("mov %%sp, %0" : "=r" (sp))
901 void leon_prom_init(struct leon_prom_info *pspi)
904 unsigned char cksum, *ptr;
905 char *addr_str, *end;
909 pspi->freq_khz = CONFIG_SYS_CLK_FREQ / 1000;
911 /* Set Available main memory size */
912 pspi->totphys.num_bytes = CONFIG_SYS_PROM_OFFSET - CONFIG_SYS_SDRAM_BASE;
913 pspi->avail.num_bytes = pspi->totphys.num_bytes;
915 /* Set the pointer to the Console UART in romvec */
916 pspi->reloc_funcs.leon3_apbuart = gd->arch.uart;
922 b = (ambapp_dev_irqmp *) leon3_getapbbase(VENDOR_GAISLER,
925 j = 1 + ((LEON3_BYPASS_LOAD_PA(&(b->mpstatus))
926 >> LEON3_IRQMPSTATUS_CPUNR) & 0xf);
930 pspi->nodes[2 + j].level = -1;
931 pspi->nodes[2 + j].properties = __va(spi.root_properties + 3);
934 /* Set Ethernet MAC address from environment */
935 if ((addr_str = getenv("ethaddr")) != NULL) {
936 for (i = 0; i < 6; i++) {
937 pspi->idprom.id_ethaddr[i] = addr_str ?
938 simple_strtoul(addr_str, &end, 16) : 0;
940 addr_str = (*end) ? end + 1 : end;
944 /* HW Address not found in environment,
945 * Set default HW address
947 pspi->idprom.id_ethaddr[0] = 0;
948 pspi->idprom.id_ethaddr[1] = 0;
949 pspi->idprom.id_ethaddr[2] = 0;
950 pspi->idprom.id_ethaddr[3] = 0;
951 pspi->idprom.id_ethaddr[4] = 0;
952 pspi->idprom.id_ethaddr[5] = 0;
955 ptr = (unsigned char *)&pspi->idprom;
956 for (i = cksum = 0; i <= 0x0E; i++)
958 pspi->idprom.id_cksum = cksum;
961 static inline void set_cache(unsigned long regval)
963 asm volatile ("sta %0, [%%g0] %1\n\t":: "r" (regval), "i"(2):"memory");
966 extern unsigned short bss_start, bss_end;
968 /* mark as section .img.main.text, to be referenced in linker script */
971 struct leon_prom_info *pspi = (void *)
972 ((((unsigned int)&spi) & PROM_SIZE_MASK) + CONFIG_SYS_PROM_OFFSET);
975 srmmu_set_mmureg(0x00000000);
976 __asm__ __volatile__("flush\n\t");
978 /* init prom info struct */
979 leon_prom_init(pspi);
981 kernel_arg_promvec = &pspi->romvec;
983 printf("Kernel rom vec: 0x%lx\n", (unsigned int)(&pspi->romvec));
988 /* Copy current kernel boot argument to ROMvec */
989 void prepare_bootargs(char *bootargs)
991 struct leon_prom_info *pspi;
995 /* if no bootargs set, skip copying ==> default bootline */
996 if (bootargs && (*bootargs != '\0')) {
997 pspi = (void *)((((unsigned int)&spi) & PROM_SIZE_MASK) +
998 CONFIG_SYS_PROM_OFFSET);
1000 dst = &pspi->arg[0];
1001 left = 255; /* max len */
1002 while (*src && left > 0) {
1006 /* terminate kernel command line string */
1011 void srmmu_init_cpu(unsigned int entry)
1013 sparc_srmmu_setup *psrmmu_tables = (void *)
1014 ((((unsigned int)&srmmu_tables) & PROM_SIZE_MASK) +
1015 CONFIG_SYS_PROM_OFFSET);
1017 /* Make context 0 (kernel's context) point
1018 * to our prepared memory mapping
1021 psrmmu_tables->ctx_table[0] =
1022 ((unsigned int)&psrmmu_tables->pgd_table[0x00]) >> 4 | PTD;
1024 /* Set virtual kernel address 0xf0000000
1025 * to SRAM/SDRAM address.
1026 * Make it READ/WRITE/EXEC to SuperUser
1029 #define ACC_SU_ALL 0x1c
1030 psrmmu_tables->pgd_table[0xf0] =
1031 (CONFIG_SYS_SDRAM_BASE >> 4) | ACC_SU_ALL | PTE;
1032 psrmmu_tables->pgd_table[0xf1] =
1033 ((CONFIG_SYS_SDRAM_BASE + 0x1000000) >> 4) | ACC_SU_ALL | PTE;
1034 psrmmu_tables->pgd_table[0xf2] =
1035 ((CONFIG_SYS_SDRAM_BASE + 0x2000000) >> 4) | ACC_SU_ALL | PTE;
1036 psrmmu_tables->pgd_table[0xf3] =
1037 ((CONFIG_SYS_SDRAM_BASE + 0x3000000) >> 4) | ACC_SU_ALL | PTE;
1038 psrmmu_tables->pgd_table[0xf4] =
1039 ((CONFIG_SYS_SDRAM_BASE + 0x4000000) >> 4) | ACC_SU_ALL | PTE;
1040 psrmmu_tables->pgd_table[0xf5] =
1041 ((CONFIG_SYS_SDRAM_BASE + 0x5000000) >> 4) | ACC_SU_ALL | PTE;
1042 psrmmu_tables->pgd_table[0xf6] =
1043 ((CONFIG_SYS_SDRAM_BASE + 0x6000000) >> 4) | ACC_SU_ALL | PTE;
1044 psrmmu_tables->pgd_table[0xf7] =
1045 ((CONFIG_SYS_SDRAM_BASE + 0x7000000) >> 4) | ACC_SU_ALL | PTE;
1047 /* convert rom vec pointer to virtual address */
1048 kernel_arg_promvec = (struct linux_romvec *)
1049 (((unsigned int)kernel_arg_promvec & 0x0fffffff) | 0xf0000000);
1051 /* Set Context pointer to point to context table
1052 * 256 contexts supported.
1054 srmmu_set_ctable_ptr((unsigned int)&psrmmu_tables->ctx_table[0]);
1056 /* Set kernel's context, context zero */
1057 srmmu_set_context(0);
1059 /* Invalidate all Cache */
1060 __asm__ __volatile__("flush\n\t");
1062 srmmu_set_mmureg(0x00000001);
1063 leon_flush_tlb_all();
1064 leon_flush_cache_all();