ARM: omap2: remove unnecessary boot_lock
[linux-2.6/linux-2.6-arm.git] / arch / mips / generic / yamon-dt.c
blob7ba4ad5cc1d668003f5824fa00746803a5a50524
1 /*
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.
9 */
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)
26 int err, chosen_off;
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_add_subnode(fdt, 0, "chosen");
32 if (chosen_off < 0) {
33 pr_err("Unable to find or add DT chosen node: %d\n",
34 chosen_off);
35 return chosen_off;
38 err = fdt_setprop_string(fdt, chosen_off, "bootargs", fw_getcmdline());
39 if (err) {
40 pr_err("Unable to set bootargs property: %d\n", err);
41 return err;
44 return 0;
47 static unsigned int __init gen_fdt_mem_array(
48 const struct yamon_mem_region *regions,
49 __be32 *mem_array,
50 unsigned int max_entries,
51 unsigned long memsize)
53 const struct yamon_mem_region *mr;
54 unsigned long size;
55 unsigned int entries = 0;
57 for (mr = regions; mr->size && memsize; ++mr) {
58 if (entries >= max_entries) {
59 pr_warn("Number of regions exceeds max %u\n",
60 max_entries);
61 break;
64 /* How much of the remaining RAM fits in the next region? */
65 size = min_t(unsigned long, memsize, mr->size);
66 memsize -= size;
68 /* Emit a memory region */
69 *(mem_array++) = cpu_to_be32(mr->start);
70 *(mem_array++) = cpu_to_be32(size);
71 ++entries;
73 /* Discard the next mr->discard bytes */
74 memsize -= min_t(unsigned long, memsize, mr->discard);
76 return entries;
79 __init int yamon_dt_append_memory(void *fdt,
80 const struct yamon_mem_region *regions)
82 unsigned long phys_memsize, memsize;
83 __be32 mem_array[2 * MAX_MEM_ARRAY_ENTRIES];
84 unsigned int mem_entries;
85 int i, err, mem_off;
86 char *var, param_name[10], *var_names[] = {
87 "ememsize", "memsize",
90 /* find memory size from the bootloader environment */
91 for (i = 0; i < ARRAY_SIZE(var_names); i++) {
92 var = fw_getenv(var_names[i]);
93 if (!var)
94 continue;
96 err = kstrtoul(var, 0, &phys_memsize);
97 if (!err)
98 break;
100 pr_warn("Failed to read the '%s' env variable '%s'\n",
101 var_names[i], var);
104 if (!phys_memsize) {
105 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
106 phys_memsize = 32 << 20;
109 /* default to using all available RAM */
110 memsize = phys_memsize;
112 /* allow the user to override the usable memory */
113 for (i = 0; i < ARRAY_SIZE(var_names); i++) {
114 snprintf(param_name, sizeof(param_name), "%s=", var_names[i]);
115 var = strstr(arcs_cmdline, param_name);
116 if (!var)
117 continue;
119 memsize = memparse(var + strlen(param_name), NULL);
122 /* if the user says there's more RAM than we thought, believe them */
123 phys_memsize = max_t(unsigned long, phys_memsize, memsize);
125 /* find or add a memory node */
126 mem_off = fdt_path_offset(fdt, "/memory");
127 if (mem_off == -FDT_ERR_NOTFOUND)
128 mem_off = fdt_add_subnode(fdt, 0, "memory");
129 if (mem_off < 0) {
130 pr_err("Unable to find or add memory DT node: %d\n", mem_off);
131 return mem_off;
134 err = fdt_setprop_string(fdt, mem_off, "device_type", "memory");
135 if (err) {
136 pr_err("Unable to set memory node device_type: %d\n", err);
137 return err;
140 mem_entries = gen_fdt_mem_array(regions, mem_array,
141 MAX_MEM_ARRAY_ENTRIES, phys_memsize);
142 err = fdt_setprop(fdt, mem_off, "reg",
143 mem_array, mem_entries * 2 * sizeof(mem_array[0]));
144 if (err) {
145 pr_err("Unable to set memory regs property: %d\n", err);
146 return err;
149 mem_entries = gen_fdt_mem_array(regions, mem_array,
150 MAX_MEM_ARRAY_ENTRIES, memsize);
151 err = fdt_setprop(fdt, mem_off, "linux,usable-memory",
152 mem_array, mem_entries * 2 * sizeof(mem_array[0]));
153 if (err) {
154 pr_err("Unable to set linux,usable-memory property: %d\n", err);
155 return err;
158 return 0;
161 __init int yamon_dt_serial_config(void *fdt)
163 const char *yamontty, *mode_var;
164 char mode_var_name[9], path[20], parity;
165 unsigned int uart, baud, stop_bits;
166 bool hw_flow;
167 int chosen_off, err;
169 yamontty = fw_getenv("yamontty");
170 if (!yamontty || !strcmp(yamontty, "tty0")) {
171 uart = 0;
172 } else if (!strcmp(yamontty, "tty1")) {
173 uart = 1;
174 } else {
175 pr_warn("yamontty environment variable '%s' invalid\n",
176 yamontty);
177 uart = 0;
180 baud = stop_bits = 0;
181 parity = 0;
182 hw_flow = false;
184 snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
185 mode_var = fw_getenv(mode_var_name);
186 if (mode_var) {
187 while (mode_var[0] >= '0' && mode_var[0] <= '9') {
188 baud *= 10;
189 baud += mode_var[0] - '0';
190 mode_var++;
192 if (mode_var[0] == ',')
193 mode_var++;
194 if (mode_var[0])
195 parity = mode_var[0];
196 if (mode_var[0] == ',')
197 mode_var++;
198 if (mode_var[0])
199 stop_bits = mode_var[0] - '0';
200 if (mode_var[0] == ',')
201 mode_var++;
202 if (!strcmp(mode_var, "hw"))
203 hw_flow = true;
206 if (!baud)
207 baud = 38400;
209 if (parity != 'e' && parity != 'n' && parity != 'o')
210 parity = 'n';
212 if (stop_bits != 7 && stop_bits != 8)
213 stop_bits = 8;
215 WARN_ON(snprintf(path, sizeof(path), "serial%u:%u%c%u%s",
216 uart, baud, parity, stop_bits,
217 hw_flow ? "r" : "") >= sizeof(path));
219 /* find or add chosen node */
220 chosen_off = fdt_path_offset(fdt, "/chosen");
221 if (chosen_off == -FDT_ERR_NOTFOUND)
222 chosen_off = fdt_add_subnode(fdt, 0, "chosen");
223 if (chosen_off < 0) {
224 pr_err("Unable to find or add DT chosen node: %d\n",
225 chosen_off);
226 return chosen_off;
229 err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
230 if (err) {
231 pr_err("Unable to set stdout-path property: %d\n", err);
232 return err;
235 return 0;