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>
23 extern struct linux_romvec *kernel_arg_promvec;
24 extern ambapp_dev_apbuart *leon3_apbuart;
26 #define PROM_PGT __attribute__ ((__section__ (".prom.pgt")))
27 #define PROM_TEXT __attribute__ ((__section__ (".prom.text")))
28 #define PROM_DATA __attribute__ ((__section__ (".prom.data")))
30 ambapp_dev_gptimer *gptimer;
33 extern int __prom_start;
34 #define PAGE_OFFSET 0xf0000000
35 #define phys_base CONFIG_SYS_SDRAM_BASE
36 #define PROM_OFFS 8192
37 #define PROM_SIZE_MASK (PROM_OFFS-1)
39 (void *)( ((unsigned long)(x))-PROM_OFFS+ \
40 (CONFIG_SYS_PROM_OFFSET-phys_base)+PAGE_OFFSET-CONFIG_SYS_TEXT_BASE ) \
42 #define __phy(x) ((void *)(((unsigned long)(x))-PROM_OFFS+CONFIG_SYS_PROM_OFFSET-CONFIG_SYS_TEXT_BASE))
52 struct property *properties;
55 static void leon_reboot(char *bcommand);
56 static void leon_halt(void);
57 static int leon_nbputchar(int c);
58 static int leon_nbgetchar(void);
60 static int no_nextnode(int node);
61 static int no_child(int node);
62 static int no_proplen(int node, char *name);
63 static int no_getprop(int node, char *name, char *value);
64 static int no_setprop(int node, char *name, char *value, int len);
65 static char *no_nextprop(int node, char *name);
67 static struct property PROM_TEXT *find_property(int node, char *name);
68 static int PROM_TEXT leon_strcmp(const char *s1, const char *s2);
69 static void *PROM_TEXT leon_memcpy(void *dest, const void *src, size_t n);
70 static void PROM_TEXT leon_reboot_physical(char *bcommand);
72 void __inline__ leon_flush_cache_all(void)
74 __asm__ __volatile__(" flush ");
75 __asm__ __volatile__("sta %%g0, [%%g0] %0\n\t"::"i"(ASI_DFLUSH):"memory");
78 void __inline__ leon_flush_tlb_all(void)
80 leon_flush_cache_all();
81 __asm__ __volatile__("sta %%g0, [%0] %1\n\t"::"r"(0x400),
82 "i"(ASI_MMUFLUSH):"memory");
86 unsigned int ctx_table[256];
87 unsigned int pgd_table[256];
90 sparc_srmmu_setup srmmu_tables PROM_PGT = {
332 0x400001e /* default */
336 /* a self contained prom info structure */
337 struct leon_reloc_func {
338 struct property *(*find_property) (int node, char *name);
339 int (*strcmp) (char *s1, char *s2);
340 void *(*memcpy) (void *dest, const void *src, size_t n);
341 void (*reboot_physical) (char *cmd);
342 ambapp_dev_apbuart *leon3_apbuart;
345 struct leon_prom_info {
350 struct leon_reloc_func reloc_funcs;
351 struct property root_properties[4];
352 struct property cpu_properties[7];
354 #define CPUENTRY(idx) struct property cpu_properties##idx[4]
386 struct idprom idprom;
387 struct linux_nodeops nodeops;
388 struct linux_mlist_v0 *totphys_p;
389 struct linux_mlist_v0 totphys;
390 struct linux_mlist_v0 *avail_p;
391 struct linux_mlist_v0 avail;
392 struct linux_mlist_v0 *prommap_p;
393 void (*synchook) (void);
394 struct linux_arguments_v0 *bootargs_p;
395 struct linux_arguments_v0 bootargs;
396 struct linux_romvec romvec;
397 struct node nodes[35];
398 char s_device_type[12];
402 char s_compatability[14];
405 char s_frequency[16];
406 char s_uart1_baud[11];
407 char s_uart2_baud[11];
411 /* static prom info */
412 static struct leon_prom_info PROM_DATA spi = {
413 CONFIG_SYS_CLK_FREQ / 1000,
417 #define CPUENTRY(idx) idx
455 __phy(leon_reboot_physical),
458 {__va(spi.s_device_type), __va(spi.s_idprom), 4},
459 {__va(spi.s_idprom), (char *)__va(&spi.idprom), sizeof(struct idprom)},
460 {__va(spi.s_compatability), __va(spi.s_leon2), 5},
464 {__va(spi.s_device_type), __va(spi.s_cpu), 4},
465 {__va(spi.s_mid), __va(&spi.mids[0]), 4},
466 {__va(spi.s_mmu_nctx), (char *)__va(&spi.leon_nctx), 4},
467 {__va(spi.s_frequency), (char *)__va(&spi.freq_khz), 4},
468 {__va(spi.s_uart1_baud), (char *)__va(&spi.baudrates[0]), 4},
469 {__va(spi.s_uart2_baud), (char *)__va(&spi.baudrates[1]), 4},
473 #define CPUENTRY(idx) \
474 { /* cpu_properties */ \
475 {__va(spi.s_device_type), __va(spi.s_cpu), 4}, \
476 {__va(spi.s_mid), __va(&spi.mids[idx]), 4}, \
477 {__va(spi.s_frequency), (char *)__va(&spi.freq_khz), 4}, \
513 M_LEON2 | M_LEON2_SOC, /* machine type */
514 {0, 0, 0, 0, 0, 0}, /* eth */
518 {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0} /* reserved */
531 (char *)CONFIG_SYS_SDRAM_BASE,
537 (char *)CONFIG_SYS_SDRAM_BASE,
540 NULL, /* prommap_p */
544 {NULL, __va(spi.arg), NULL /*... */ },
549 0, /* sun4c v0 prom */
551 {__va(&spi.totphys_p), __va(&spi.prommap_p), __va(&spi.avail_p)},
553 NULL, {NULL /* ... */ },
555 NULL, NULL, /* pv_getchar, pv_putchar */
556 __va(leon_nbgetchar), __va(leon_nbputchar),
565 __va(&spi.bootargs_p)
569 {0, __va(spi.root_properties + 3) /* NULL, NULL, -1 */ },
570 {0, __va(spi.root_properties)},
571 /* cpu 0, must be spi.nodes[2] see leon_prom_init() */
572 {1, __va(spi.cpu_properties)},
575 #define CPUENTRY(idx) \
576 {1, __va(spi.cpu_properties##idx) } /* cpu <idx> */
608 {-1, __va(spi.root_properties + 3) /* NULL, NULL, -1 */ }
620 CONFIG_DEFAULT_KERNEL_COMMAND_LINE
623 /* from arch/sparc/kernel/setup.c */
624 #define RAMDISK_LOAD_FLAG 0x4000
625 extern unsigned short root_flags;
626 extern unsigned short root_dev;
627 extern unsigned short ram_flags;
628 extern unsigned int sparc_ramdisk_image;
629 extern unsigned int sparc_ramdisk_size;
630 extern int root_mountflags;
632 extern char initrd_end, initrd_start;
634 /* Reboot the CPU = jump to beginning of flash again.
636 * Make sure that all function are inlined here.
638 static void PROM_TEXT leon_reboot(char *bcommand)
640 register char *arg = bcommand;
641 void __attribute__ ((noreturn)) (*reboot_physical) (char *cmd);
643 /* get physical address */
644 struct leon_prom_info *pspi =
645 (void *)(CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
647 unsigned int *srmmu_ctx_table;
649 /* Turn of Interrupts */
652 /* Set kernel's context, context zero */
653 srmmu_set_context(0);
655 /* Get physical address of the MMU shutdown routine */
656 reboot_physical = (void *)
657 SPARC_BYPASS_READ(&pspi->reloc_funcs.reboot_physical);
659 /* Now that we know the physical address of the function
660 * we can make the MMU allow jumping to it.
662 srmmu_ctx_table = (unsigned int *)srmmu_get_ctable_ptr();
664 srmmu_ctx_table = (unsigned int *)SPARC_BYPASS_READ(srmmu_ctx_table);
666 /* get physical address of kernel's context table (assume ptd) */
667 srmmu_ctx_table = (unsigned int *)
668 (((unsigned int)srmmu_ctx_table & 0xfffffffc) << 4);
670 /* enable access to physical address of MMU shutdown function */
671 SPARC_BYPASS_WRITE(&srmmu_ctx_table
672 [((unsigned int)reboot_physical) >> 24],
673 (((unsigned int)reboot_physical & 0xff000000) >> 4) |
676 /* flush TLB cache */
677 leon_flush_tlb_all();
679 /* flash instruction & data cache */
680 sparc_icache_flush_all();
681 sparc_dcache_flush_all();
683 /* jump to physical address function
684 * so that when the MMU is disabled
685 * we can continue to execute
687 reboot_physical(arg);
690 static void PROM_TEXT leon_reboot_physical(char *bcommand)
692 void __attribute__ ((noreturn)) (*reset) (void);
697 /* Hardcoded start address */
698 reset = CONFIG_SYS_MONITOR_BASE;
700 /* flush data cache */
701 sparc_dcache_flush_all();
703 /* flush instruction cache */
704 sparc_icache_flush_all();
706 /* Jump to start in Flash */
710 static void PROM_TEXT leon_halt(void)
715 /* get single char, don't care for blocking*/
716 static int PROM_TEXT leon_nbgetchar(void)
721 /* put single char, don't care for blocking*/
722 static int PROM_TEXT leon_nbputchar(int c)
724 ambapp_dev_apbuart *uart;
726 /* get physical address */
727 struct leon_prom_info *pspi =
728 (void *)(CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
730 uart = (ambapp_dev_apbuart *)
731 SPARC_BYPASS_READ(&pspi->reloc_funcs.leon3_apbuart);
737 /***** put char in buffer... ***********
738 * Make sure all functions are inline! *
739 ***************************************/
741 /* Wait for last character to go. */
742 while (!(SPARC_BYPASS_READ(&uart->status)
743 & LEON_REG_UART_STATUS_THE)) ;
746 SPARC_BYPASS_WRITE(&uart->data, c);
748 /* Wait for data to be sent */
749 while (!(SPARC_BYPASS_READ(&uart->status)
750 & LEON_REG_UART_STATUS_TSE)) ;
757 /*#define nodes ((struct node *)__va(&pspi->nodes))*/
758 #define nodes ((struct node *)(pspi->nodes))
760 static int PROM_TEXT no_nextnode(int node)
762 /* get physical address */
763 struct leon_prom_info *pspi =
764 (void *)(CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
766 /* convert into virtual address */
767 pspi = (struct leon_prom_info *)
768 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
770 if (nodes[node].level == nodes[node + 1].level)
775 static int PROM_TEXT no_child(int node)
777 /* get physical address */
778 struct leon_prom_info *pspi = (struct leon_prom_info *)
779 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
781 /* convert into virtual address */
782 pspi = (struct leon_prom_info *)
783 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
785 if (nodes[node].level == nodes[node + 1].level - 1)
790 static struct property PROM_TEXT *find_property(int node, char *name)
792 /* get physical address */
793 struct leon_prom_info *pspi = (struct leon_prom_info *)
794 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
796 /* convert into virtual address */
797 pspi = (struct leon_prom_info *)
798 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
800 struct property *prop = &nodes[node].properties[0];
801 while (prop && prop->name) {
802 if (pspi->reloc_funcs.strcmp(prop->name, name) == 0)
809 static int PROM_TEXT no_proplen(int node, char *name)
811 /* get physical address */
812 struct leon_prom_info *pspi = (struct leon_prom_info *)
813 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
815 /* convert into virtual address */
816 pspi = (struct leon_prom_info *)
817 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
819 struct property *prop = pspi->reloc_funcs.find_property(node, name);
825 static int PROM_TEXT no_getprop(int node, char *name, char *value)
827 /* get physical address */
828 struct leon_prom_info *pspi = (struct leon_prom_info *)
829 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
831 /* convert into virtual address */
832 pspi = (struct leon_prom_info *)
833 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
835 struct property *prop = pspi->reloc_funcs.find_property(node, name);
837 pspi->reloc_funcs.memcpy(value, prop->value, prop->length);
843 static int PROM_TEXT no_setprop(int node, char *name, char *value, int len)
848 static char PROM_TEXT *no_nextprop(int node, char *name)
850 /* get physical address */
851 struct leon_prom_info *pspi = (struct leon_prom_info *)
852 (CONFIG_SYS_PROM_OFFSET + sizeof(srmmu_tables));
853 struct property *prop;
855 /* convert into virtual address */
856 pspi = (struct leon_prom_info *)
857 (((unsigned int)pspi & 0x0fffffff) | PAGE_OFFSET);
859 if (!name || !name[0])
860 return nodes[node].properties[0].name;
862 prop = pspi->reloc_funcs.find_property(node, name);
868 static int PROM_TEXT leon_strcmp(const char *s1, const char *s2)
870 register char result;
883 static void *PROM_TEXT leon_memcpy(void *dest, const void *src, size_t n)
885 char *dst = (char *)dest, *source = (char *)src;
895 #define GETREGSP(sp) __asm__ __volatile__("mov %%sp, %0" : "=r" (sp))
897 void leon_prom_init(struct leon_prom_info *pspi)
900 unsigned char cksum, *ptr;
901 char *addr_str, *end;
905 pspi->freq_khz = CONFIG_SYS_CLK_FREQ / 1000;
907 /* Set Available main memory size */
908 pspi->totphys.num_bytes = CONFIG_SYS_PROM_OFFSET - CONFIG_SYS_SDRAM_BASE;
909 pspi->avail.num_bytes = pspi->totphys.num_bytes;
911 /* Set the pointer to the Console UART in romvec */
912 pspi->reloc_funcs.leon3_apbuart = leon3_apbuart;
918 b = (ambapp_dev_irqmp *) leon3_getapbbase(VENDOR_GAISLER,
921 j = 1 + ((LEON3_BYPASS_LOAD_PA(&(b->mpstatus))
922 >> LEON3_IRQMPSTATUS_CPUNR) & 0xf);
926 pspi->nodes[2 + j].level = -1;
927 pspi->nodes[2 + j].properties = __va(spi.root_properties + 3);
930 /* Set Ethernet MAC address from environment */
931 if ((addr_str = getenv("ethaddr")) != NULL) {
932 for (i = 0; i < 6; i++) {
933 pspi->idprom.id_ethaddr[i] = addr_str ?
934 simple_strtoul(addr_str, &end, 16) : 0;
936 addr_str = (*end) ? end + 1 : end;
940 /* HW Address not found in environment,
941 * Set default HW address
943 pspi->idprom.id_ethaddr[0] = 0;
944 pspi->idprom.id_ethaddr[1] = 0;
945 pspi->idprom.id_ethaddr[2] = 0;
946 pspi->idprom.id_ethaddr[3] = 0;
947 pspi->idprom.id_ethaddr[4] = 0;
948 pspi->idprom.id_ethaddr[5] = 0;
951 ptr = (unsigned char *)&pspi->idprom;
952 for (i = cksum = 0; i <= 0x0E; i++)
954 pspi->idprom.id_cksum = cksum;
957 static inline void set_cache(unsigned long regval)
959 asm volatile ("sta %0, [%%g0] %1\n\t":: "r" (regval), "i"(2):"memory");
962 extern unsigned short bss_start, bss_end;
964 /* mark as section .img.main.text, to be referenced in linker script */
967 struct leon_prom_info *pspi = (void *)
968 ((((unsigned int)&spi) & PROM_SIZE_MASK) + CONFIG_SYS_PROM_OFFSET);
971 srmmu_set_mmureg(0x00000000);
972 __asm__ __volatile__("flush\n\t");
974 /* init prom info struct */
975 leon_prom_init(pspi);
977 kernel_arg_promvec = &pspi->romvec;
979 printf("Kernel rom vec: 0x%lx\n", (unsigned int)(&pspi->romvec));
984 /* Copy current kernel boot argument to ROMvec */
985 void prepare_bootargs(char *bootargs)
987 struct leon_prom_info *pspi;
991 /* if no bootargs set, skip copying ==> default bootline */
992 if (bootargs && (*bootargs != '\0')) {
993 pspi = (void *)((((unsigned int)&spi) & PROM_SIZE_MASK) +
994 CONFIG_SYS_PROM_OFFSET);
997 left = 255; /* max len */
998 while (*src && left > 0) {
1002 /* terminate kernel command line string */
1007 void srmmu_init_cpu(unsigned int entry)
1009 sparc_srmmu_setup *psrmmu_tables = (void *)
1010 ((((unsigned int)&srmmu_tables) & PROM_SIZE_MASK) +
1011 CONFIG_SYS_PROM_OFFSET);
1013 /* Make context 0 (kernel's context) point
1014 * to our prepared memory mapping
1017 psrmmu_tables->ctx_table[0] =
1018 ((unsigned int)&psrmmu_tables->pgd_table[0x00]) >> 4 | PTD;
1020 /* Set virtual kernel address 0xf0000000
1021 * to SRAM/SDRAM address.
1022 * Make it READ/WRITE/EXEC to SuperUser
1025 #define ACC_SU_ALL 0x1c
1026 psrmmu_tables->pgd_table[0xf0] =
1027 (CONFIG_SYS_SDRAM_BASE >> 4) | ACC_SU_ALL | PTE;
1028 psrmmu_tables->pgd_table[0xf1] =
1029 ((CONFIG_SYS_SDRAM_BASE + 0x1000000) >> 4) | ACC_SU_ALL | PTE;
1030 psrmmu_tables->pgd_table[0xf2] =
1031 ((CONFIG_SYS_SDRAM_BASE + 0x2000000) >> 4) | ACC_SU_ALL | PTE;
1032 psrmmu_tables->pgd_table[0xf3] =
1033 ((CONFIG_SYS_SDRAM_BASE + 0x3000000) >> 4) | ACC_SU_ALL | PTE;
1034 psrmmu_tables->pgd_table[0xf4] =
1035 ((CONFIG_SYS_SDRAM_BASE + 0x4000000) >> 4) | ACC_SU_ALL | PTE;
1036 psrmmu_tables->pgd_table[0xf5] =
1037 ((CONFIG_SYS_SDRAM_BASE + 0x5000000) >> 4) | ACC_SU_ALL | PTE;
1038 psrmmu_tables->pgd_table[0xf6] =
1039 ((CONFIG_SYS_SDRAM_BASE + 0x6000000) >> 4) | ACC_SU_ALL | PTE;
1040 psrmmu_tables->pgd_table[0xf7] =
1041 ((CONFIG_SYS_SDRAM_BASE + 0x7000000) >> 4) | ACC_SU_ALL | PTE;
1043 /* convert rom vec pointer to virtual address */
1044 kernel_arg_promvec = (struct linux_romvec *)
1045 (((unsigned int)kernel_arg_promvec & 0x0fffffff) | 0xf0000000);
1047 /* Set Context pointer to point to context table
1048 * 256 contexts supported.
1050 srmmu_set_ctable_ptr((unsigned int)&psrmmu_tables->ctx_table[0]);
1052 /* Set kernel's context, context zero */
1053 srmmu_set_context(0);
1055 /* Invalidate all Cache */
1056 __asm__ __volatile__("flush\n\t");
1058 srmmu_set_mmureg(0x00000001);
1059 leon_flush_tlb_all();
1060 leon_flush_cache_all();