2 * Copyright (C) 2016 Imagination Technologies
3 * Author: Paul Burton <paul.burton@mips.com>
5 * This program is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License as published by the
7 * Free Software Foundation; either version 2 of the License, or (at your
8 * option) any later version.
11 #define pr_fmt(fmt) "yamon-dt: " fmt
13 #include <linux/bug.h>
14 #include <linux/errno.h>
15 #include <linux/kernel.h>
16 #include <linux/libfdt.h>
17 #include <linux/printk.h>
19 #include <asm/fw/fw.h>
20 #include <asm/yamon-dt.h>
22 #define MAX_MEM_ARRAY_ENTRIES 2
24 __init
int yamon_dt_append_cmdline(void *fdt
)
28 /* find or add chosen node */
29 chosen_off
= fdt_path_offset(fdt
, "/chosen");
30 if (chosen_off
== -FDT_ERR_NOTFOUND
)
31 chosen_off
= fdt_path_offset(fdt
, "/chosen@0");
32 if (chosen_off
== -FDT_ERR_NOTFOUND
)
33 chosen_off
= fdt_add_subnode(fdt
, 0, "chosen");
35 pr_err("Unable to find or add DT chosen node: %d\n",
40 err
= fdt_setprop_string(fdt
, chosen_off
, "bootargs", fw_getcmdline());
42 pr_err("Unable to set bootargs property: %d\n", err
);
49 static unsigned int __init
gen_fdt_mem_array(
50 const struct yamon_mem_region
*regions
,
52 unsigned int max_entries
,
53 unsigned long memsize
)
55 const struct yamon_mem_region
*mr
;
57 unsigned int entries
= 0;
59 for (mr
= regions
; mr
->size
&& memsize
; ++mr
) {
60 if (entries
>= max_entries
) {
61 pr_warn("Number of regions exceeds max %u\n",
66 /* How much of the remaining RAM fits in the next region? */
67 size
= min_t(unsigned long, memsize
, mr
->size
);
70 /* Emit a memory region */
71 *(mem_array
++) = cpu_to_be32(mr
->start
);
72 *(mem_array
++) = cpu_to_be32(size
);
75 /* Discard the next mr->discard bytes */
76 memsize
-= min_t(unsigned long, memsize
, mr
->discard
);
81 __init
int yamon_dt_append_memory(void *fdt
,
82 const struct yamon_mem_region
*regions
)
84 unsigned long phys_memsize
, memsize
;
85 __be32 mem_array
[2 * MAX_MEM_ARRAY_ENTRIES
];
86 unsigned int mem_entries
;
88 char *var
, param_name
[10], *var_names
[] = {
89 "ememsize", "memsize",
92 /* find memory size from the bootloader environment */
93 for (i
= 0; i
< ARRAY_SIZE(var_names
); i
++) {
94 var
= fw_getenv(var_names
[i
]);
98 err
= kstrtoul(var
, 0, &phys_memsize
);
102 pr_warn("Failed to read the '%s' env variable '%s'\n",
107 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
108 phys_memsize
= 32 << 20;
111 /* default to using all available RAM */
112 memsize
= phys_memsize
;
114 /* allow the user to override the usable memory */
115 for (i
= 0; i
< ARRAY_SIZE(var_names
); i
++) {
116 snprintf(param_name
, sizeof(param_name
), "%s=", var_names
[i
]);
117 var
= strstr(arcs_cmdline
, param_name
);
121 memsize
= memparse(var
+ strlen(param_name
), NULL
);
124 /* if the user says there's more RAM than we thought, believe them */
125 phys_memsize
= max_t(unsigned long, phys_memsize
, memsize
);
127 /* find or add a memory node */
128 mem_off
= fdt_path_offset(fdt
, "/memory");
129 if (mem_off
== -FDT_ERR_NOTFOUND
)
130 mem_off
= fdt_add_subnode(fdt
, 0, "memory");
132 pr_err("Unable to find or add memory DT node: %d\n", mem_off
);
136 err
= fdt_setprop_string(fdt
, mem_off
, "device_type", "memory");
138 pr_err("Unable to set memory node device_type: %d\n", err
);
142 mem_entries
= gen_fdt_mem_array(regions
, mem_array
,
143 MAX_MEM_ARRAY_ENTRIES
, phys_memsize
);
144 err
= fdt_setprop(fdt
, mem_off
, "reg",
145 mem_array
, mem_entries
* 2 * sizeof(mem_array
[0]));
147 pr_err("Unable to set memory regs property: %d\n", err
);
151 mem_entries
= gen_fdt_mem_array(regions
, mem_array
,
152 MAX_MEM_ARRAY_ENTRIES
, memsize
);
153 err
= fdt_setprop(fdt
, mem_off
, "linux,usable-memory",
154 mem_array
, mem_entries
* 2 * sizeof(mem_array
[0]));
156 pr_err("Unable to set linux,usable-memory property: %d\n", err
);
163 __init
int yamon_dt_serial_config(void *fdt
)
165 const char *yamontty
, *mode_var
;
166 char mode_var_name
[9], path
[20], parity
;
167 unsigned int uart
, baud
, stop_bits
;
171 yamontty
= fw_getenv("yamontty");
172 if (!yamontty
|| !strcmp(yamontty
, "tty0")) {
174 } else if (!strcmp(yamontty
, "tty1")) {
177 pr_warn("yamontty environment variable '%s' invalid\n",
182 baud
= stop_bits
= 0;
186 snprintf(mode_var_name
, sizeof(mode_var_name
), "modetty%u", uart
);
187 mode_var
= fw_getenv(mode_var_name
);
189 while (mode_var
[0] >= '0' && mode_var
[0] <= '9') {
191 baud
+= mode_var
[0] - '0';
194 if (mode_var
[0] == ',')
197 parity
= mode_var
[0];
198 if (mode_var
[0] == ',')
201 stop_bits
= mode_var
[0] - '0';
202 if (mode_var
[0] == ',')
204 if (!strcmp(mode_var
, "hw"))
211 if (parity
!= 'e' && parity
!= 'n' && parity
!= 'o')
214 if (stop_bits
!= 7 && stop_bits
!= 8)
217 WARN_ON(snprintf(path
, sizeof(path
), "serial%u:%u%c%u%s",
218 uart
, baud
, parity
, stop_bits
,
219 hw_flow
? "r" : "") >= sizeof(path
));
221 /* find or add chosen node */
222 chosen_off
= fdt_path_offset(fdt
, "/chosen");
223 if (chosen_off
== -FDT_ERR_NOTFOUND
)
224 chosen_off
= fdt_path_offset(fdt
, "/chosen@0");
225 if (chosen_off
== -FDT_ERR_NOTFOUND
)
226 chosen_off
= fdt_add_subnode(fdt
, 0, "chosen");
227 if (chosen_off
< 0) {
228 pr_err("Unable to find or add DT chosen node: %d\n",
233 err
= fdt_setprop_string(fdt
, chosen_off
, "stdout-path", path
);
235 pr_err("Unable to set stdout-path property: %d\n", err
);