xref: /linux/arch/loongarch/kernel/env.c (revision 20bed0d6ff62d985fe8222958825504b8aad4e88)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Author: Huacai Chen <chenhuacai@loongson.cn>
4  *
5  * Copyright (C) 2020-2022 Loongson Technology Corporation Limited
6  */
7 #include <linux/acpi.h>
8 #include <linux/clk.h>
9 #include <linux/efi.h>
10 #include <linux/export.h>
11 #include <linux/memblock.h>
12 #include <linux/of_clk.h>
13 #include <asm/early_ioremap.h>
14 #include <asm/bootinfo.h>
15 #include <asm/loongson.h>
16 #include <asm/setup.h>
17 #include <asm/time.h>
18 
19 u64 efi_system_table;
20 struct loongson_system_configuration loongson_sysconf;
21 EXPORT_SYMBOL(loongson_sysconf);
22 
23 void __init init_environ(void)
24 {
25 	int efi_boot = fw_arg0;
26 	char *cmdline = early_memremap_ro(fw_arg1, COMMAND_LINE_SIZE);
27 
28 	assign_bit(EFI_BOOT, &efi.flags, efi_boot);
29 
30 	strscpy(boot_command_line, cmdline, COMMAND_LINE_SIZE);
31 	strscpy(init_command_line, cmdline, COMMAND_LINE_SIZE);
32 	early_memunmap(cmdline, COMMAND_LINE_SIZE);
33 
34 	efi_system_table = fw_arg2;
35 }
36 
37 static int __init init_cpu_fullname(void)
38 {
39 	int cpu, ret;
40 	char *cpuname;
41 	const char *model;
42 
43 	/* Parsing cpuname from DTS model property */
44 	ret = of_property_read_string(of_root, "model", &model);
45 	if (ret == 0) {
46 		cpuname = kstrdup(model, GFP_KERNEL);
47 		if (!cpuname)
48 			return -ENOMEM;
49 		loongson_sysconf.cpuname = strsep(&cpuname, " ");
50 	}
51 
52 	if (loongson_sysconf.cpuname && !strncmp(loongson_sysconf.cpuname, "Loongson", 8)) {
53 		for (cpu = 0; cpu < NR_CPUS; cpu++)
54 			__cpu_full_name[cpu] = loongson_sysconf.cpuname;
55 	}
56 	return 0;
57 }
58 arch_initcall(init_cpu_fullname);
59 
60 static int __init fdt_cpu_clk_init(void)
61 {
62 	struct clk *clk;
63 	struct device_node *np;
64 
65 	np = of_get_cpu_node(0, NULL);
66 	if (!np)
67 		return -ENODEV;
68 
69 	clk = of_clk_get(np, 0);
70 	of_node_put(np);
71 	cpu_clock_freq = 200 * 1000 * 1000;
72 
73 	if (IS_ERR(clk)) {
74 		pr_warn("No valid CPU clock freq, assume 200MHz.\n");
75 		return -ENODEV;
76 	}
77 
78 	cpu_clock_freq = clk_get_rate(clk);
79 	clk_put(clk);
80 
81 	return 0;
82 }
83 late_initcall(fdt_cpu_clk_init);
84 
85 static ssize_t boardinfo_show(struct kobject *kobj,
86 			      struct kobj_attribute *attr, char *buf)
87 {
88 	return sysfs_emit(buf,
89 		"BIOS Information\n"
90 		"Vendor\t\t\t: %s\n"
91 		"Version\t\t\t: %s\n"
92 		"ROM Size\t\t: %d KB\n"
93 		"Release Date\t\t: %s\n\n"
94 		"Board Information\n"
95 		"Manufacturer\t\t: %s\n"
96 		"Board Name\t\t: %s\n"
97 		"Family\t\t\t: LOONGSON64\n\n",
98 		b_info.bios_vendor, b_info.bios_version,
99 		b_info.bios_size, b_info.bios_release_date,
100 		b_info.board_vendor, b_info.board_name);
101 }
102 
103 static struct kobj_attribute boardinfo_attr = __ATTR(boardinfo, 0444,
104 						     boardinfo_show, NULL);
105 
106 static int __init boardinfo_init(void)
107 {
108 	struct kobject *loongson_kobj;
109 
110 	loongson_kobj = kobject_create_and_add("loongson", firmware_kobj);
111 	if (!loongson_kobj)
112 		return -ENOMEM;
113 
114 	return sysfs_create_file(loongson_kobj, &boardinfo_attr.attr);
115 }
116 late_initcall(boardinfo_init);
117