1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Copyright (C) 2016 Imagination Technologies
4 * Author: Paul Burton <paul.burton@mips.com>
7 #define pr_fmt(fmt) "yamon-dt: " fmt
10 #include <linux/errno.h>
11 #include <linux/kernel.h>
12 #include <linux/libfdt.h>
13 #include <linux/printk.h>
15 #include <asm/fw/fw.h>
16 #include <asm/yamon-dt.h>
18 #define MAX_MEM_ARRAY_ENTRIES 2
20 __init
int yamon_dt_append_cmdline(void *fdt
)
24 /* find or add chosen node */
25 chosen_off
= fdt_path_offset(fdt
, "/chosen");
26 if (chosen_off
== -FDT_ERR_NOTFOUND
)
27 chosen_off
= fdt_add_subnode(fdt
, 0, "chosen");
29 pr_err("Unable to find or add DT chosen node: %d\n",
34 err
= fdt_setprop_string(fdt
, chosen_off
, "bootargs", fw_getcmdline());
36 pr_err("Unable to set bootargs property: %d\n", err
);
43 static unsigned int __init
gen_fdt_mem_array(
44 const struct yamon_mem_region
*regions
,
46 unsigned int max_entries
,
47 unsigned long memsize
)
49 const struct yamon_mem_region
*mr
;
51 unsigned int entries
= 0;
53 for (mr
= regions
; mr
->size
&& memsize
; ++mr
) {
54 if (entries
>= max_entries
) {
55 pr_warn("Number of regions exceeds max %u\n",
60 /* How much of the remaining RAM fits in the next region? */
61 size
= min_t(unsigned long, memsize
, mr
->size
);
64 /* Emit a memory region */
65 *(mem_array
++) = cpu_to_be32(mr
->start
);
66 *(mem_array
++) = cpu_to_be32(size
);
69 /* Discard the next mr->discard bytes */
70 memsize
-= min_t(unsigned long, memsize
, mr
->discard
);
75 __init
int yamon_dt_append_memory(void *fdt
,
76 const struct yamon_mem_region
*regions
)
78 unsigned long phys_memsize
, memsize
;
79 __be32 mem_array
[2 * MAX_MEM_ARRAY_ENTRIES
];
80 unsigned int mem_entries
;
82 char *var
, param_name
[10], *var_names
[] = {
83 "ememsize", "memsize",
86 /* find memory size from the bootloader environment */
87 for (i
= 0; i
< ARRAY_SIZE(var_names
); i
++) {
88 var
= fw_getenv(var_names
[i
]);
92 err
= kstrtoul(var
, 0, &phys_memsize
);
96 pr_warn("Failed to read the '%s' env variable '%s'\n",
101 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
102 phys_memsize
= 32 << 20;
105 /* default to using all available RAM */
106 memsize
= phys_memsize
;
108 /* allow the user to override the usable memory */
109 for (i
= 0; i
< ARRAY_SIZE(var_names
); i
++) {
110 snprintf(param_name
, sizeof(param_name
), "%s=", var_names
[i
]);
111 var
= strstr(arcs_cmdline
, param_name
);
115 memsize
= memparse(var
+ strlen(param_name
), NULL
);
118 /* if the user says there's more RAM than we thought, believe them */
119 phys_memsize
= max_t(unsigned long, phys_memsize
, memsize
);
121 /* find or add a memory node */
122 mem_off
= fdt_path_offset(fdt
, "/memory");
123 if (mem_off
== -FDT_ERR_NOTFOUND
)
124 mem_off
= fdt_add_subnode(fdt
, 0, "memory");
126 pr_err("Unable to find or add memory DT node: %d\n", mem_off
);
130 err
= fdt_setprop_string(fdt
, mem_off
, "device_type", "memory");
132 pr_err("Unable to set memory node device_type: %d\n", err
);
136 mem_entries
= gen_fdt_mem_array(regions
, mem_array
,
137 MAX_MEM_ARRAY_ENTRIES
, phys_memsize
);
138 err
= fdt_setprop(fdt
, mem_off
, "reg",
139 mem_array
, mem_entries
* 2 * sizeof(mem_array
[0]));
141 pr_err("Unable to set memory regs property: %d\n", err
);
145 mem_entries
= gen_fdt_mem_array(regions
, mem_array
,
146 MAX_MEM_ARRAY_ENTRIES
, memsize
);
147 err
= fdt_setprop(fdt
, mem_off
, "linux,usable-memory",
148 mem_array
, mem_entries
* 2 * sizeof(mem_array
[0]));
150 pr_err("Unable to set linux,usable-memory property: %d\n", err
);
157 __init
int yamon_dt_serial_config(void *fdt
)
159 const char *yamontty
, *mode_var
;
160 char mode_var_name
[9], path
[20], parity
;
161 unsigned int uart
, baud
, stop_bits
;
165 yamontty
= fw_getenv("yamontty");
166 if (!yamontty
|| !strcmp(yamontty
, "tty0")) {
168 } else if (!strcmp(yamontty
, "tty1")) {
171 pr_warn("yamontty environment variable '%s' invalid\n",
176 baud
= stop_bits
= 0;
180 snprintf(mode_var_name
, sizeof(mode_var_name
), "modetty%u", uart
);
181 mode_var
= fw_getenv(mode_var_name
);
183 while (mode_var
[0] >= '0' && mode_var
[0] <= '9') {
185 baud
+= mode_var
[0] - '0';
188 if (mode_var
[0] == ',')
191 parity
= mode_var
[0];
192 if (mode_var
[0] == ',')
195 stop_bits
= mode_var
[0] - '0';
196 if (mode_var
[0] == ',')
198 if (!strcmp(mode_var
, "hw"))
205 if (parity
!= 'e' && parity
!= 'n' && parity
!= 'o')
208 if (stop_bits
!= 7 && stop_bits
!= 8)
211 WARN_ON(snprintf(path
, sizeof(path
), "serial%u:%u%c%u%s",
212 uart
, baud
, parity
, stop_bits
,
213 hw_flow
? "r" : "") >= sizeof(path
));
215 /* find or add chosen node */
216 chosen_off
= fdt_path_offset(fdt
, "/chosen");
217 if (chosen_off
== -FDT_ERR_NOTFOUND
)
218 chosen_off
= fdt_add_subnode(fdt
, 0, "chosen");
219 if (chosen_off
< 0) {
220 pr_err("Unable to find or add DT chosen node: %d\n",
225 err
= fdt_setprop_string(fdt
, chosen_off
, "stdout-path", path
);
227 pr_err("Unable to set stdout-path property: %d\n", err
);