blob: 9a0c8da5a796f24bb47ea6d4297e9973184347f7 [file] [log] [blame]
Paul Burton571b7e62017-06-02 12:29:51 -07001/*
2 * Copyright (C) 2016 Imagination Technologies
3 * Author: Paul Burton <paul.burton@imgtec.com>
4 *
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 */
10
11#define pr_fmt(fmt) "yamon-dt: " fmt
12
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>
18
19#include <asm/fw/fw.h>
20
21__init int yamon_dt_append_cmdline(void *fdt)
22{
23 int err, chosen_off;
24
25 /* find or add chosen node */
26 chosen_off = fdt_path_offset(fdt, "/chosen");
27 if (chosen_off == -FDT_ERR_NOTFOUND)
28 chosen_off = fdt_path_offset(fdt, "/chosen@0");
29 if (chosen_off == -FDT_ERR_NOTFOUND)
30 chosen_off = fdt_add_subnode(fdt, 0, "chosen");
31 if (chosen_off < 0) {
32 pr_err("Unable to find or add DT chosen node: %d\n",
33 chosen_off);
34 return chosen_off;
35 }
36
37 err = fdt_setprop_string(fdt, chosen_off, "bootargs", fw_getcmdline());
38 if (err) {
39 pr_err("Unable to set bootargs property: %d\n", err);
40 return err;
41 }
42
43 return 0;
44}
45
46__init int yamon_dt_append_memory(void *fdt)
47{
48 unsigned long phys_memsize, memsize;
49 __be32 mem_array[2];
50 int err, mem_off;
51 char *var;
52
53 /* find memory size from the bootloader environment */
54 var = fw_getenv("memsize");
55 if (var) {
56 err = kstrtoul(var, 0, &phys_memsize);
57 if (err) {
58 pr_err("Failed to read memsize env variable '%s'\n",
59 var);
60 return -EINVAL;
61 }
62 } else {
63 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n");
64 phys_memsize = 32 << 20;
65 }
66
67 /* default to using all available RAM */
68 memsize = phys_memsize;
69
70 /* allow the user to override the usable memory */
71 var = strstr(arcs_cmdline, "memsize=");
72 if (var)
73 memsize = memparse(var + strlen("memsize="), NULL);
74
75 /* if the user says there's more RAM than we thought, believe them */
76 phys_memsize = max_t(unsigned long, phys_memsize, memsize);
77
78 /* find or add a memory node */
79 mem_off = fdt_path_offset(fdt, "/memory");
80 if (mem_off == -FDT_ERR_NOTFOUND)
81 mem_off = fdt_add_subnode(fdt, 0, "memory");
82 if (mem_off < 0) {
83 pr_err("Unable to find or add memory DT node: %d\n", mem_off);
84 return mem_off;
85 }
86
87 err = fdt_setprop_string(fdt, mem_off, "device_type", "memory");
88 if (err) {
89 pr_err("Unable to set memory node device_type: %d\n", err);
90 return err;
91 }
92
93 mem_array[0] = 0;
94 mem_array[1] = cpu_to_be32(phys_memsize);
95 err = fdt_setprop(fdt, mem_off, "reg", mem_array, sizeof(mem_array));
96 if (err) {
97 pr_err("Unable to set memory regs property: %d\n", err);
98 return err;
99 }
100
101 mem_array[0] = 0;
102 mem_array[1] = cpu_to_be32(memsize);
103 err = fdt_setprop(fdt, mem_off, "linux,usable-memory",
104 mem_array, sizeof(mem_array));
105 if (err) {
106 pr_err("Unable to set linux,usable-memory property: %d\n", err);
107 return err;
108 }
109
110 return 0;
111}
112
113__init int yamon_dt_serial_config(void *fdt)
114{
115 const char *yamontty, *mode_var;
116 char mode_var_name[9], path[18], parity;
117 unsigned int uart, baud, stop_bits;
118 bool hw_flow;
119 int chosen_off, err;
120
121 yamontty = fw_getenv("yamontty");
122 if (!yamontty || !strcmp(yamontty, "tty0")) {
123 uart = 0;
124 } else if (!strcmp(yamontty, "tty1")) {
125 uart = 1;
126 } else {
127 pr_warn("yamontty environment variable '%s' invalid\n",
128 yamontty);
129 uart = 0;
130 }
131
132 baud = stop_bits = 0;
133 parity = 0;
134 hw_flow = false;
135
136 snprintf(mode_var_name, sizeof(mode_var_name), "modetty%u", uart);
137 mode_var = fw_getenv(mode_var_name);
138 if (mode_var) {
139 while (mode_var[0] >= '0' && mode_var[0] <= '9') {
140 baud *= 10;
141 baud += mode_var[0] - '0';
142 mode_var++;
143 }
144 if (mode_var[0] == ',')
145 mode_var++;
146 if (mode_var[0])
147 parity = mode_var[0];
148 if (mode_var[0] == ',')
149 mode_var++;
150 if (mode_var[0])
151 stop_bits = mode_var[0] - '0';
152 if (mode_var[0] == ',')
153 mode_var++;
154 if (!strcmp(mode_var, "hw"))
155 hw_flow = true;
156 }
157
158 if (!baud)
159 baud = 38400;
160
161 if (parity != 'e' && parity != 'n' && parity != 'o')
162 parity = 'n';
163
164 if (stop_bits != 7 && stop_bits != 8)
165 stop_bits = 8;
166
167 WARN_ON(snprintf(path, sizeof(path), "uart%u:%u%c%u%s",
168 uart, baud, parity, stop_bits,
169 hw_flow ? "r" : "") >= sizeof(path));
170
171 /* find or add chosen node */
172 chosen_off = fdt_path_offset(fdt, "/chosen");
173 if (chosen_off == -FDT_ERR_NOTFOUND)
174 chosen_off = fdt_path_offset(fdt, "/chosen@0");
175 if (chosen_off == -FDT_ERR_NOTFOUND)
176 chosen_off = fdt_add_subnode(fdt, 0, "chosen");
177 if (chosen_off < 0) {
178 pr_err("Unable to find or add DT chosen node: %d\n",
179 chosen_off);
180 return chosen_off;
181 }
182
183 err = fdt_setprop_string(fdt, chosen_off, "stdout-path", path);
184 if (err) {
185 pr_err("Unable to set stdout-path property: %d\n", err);
186 return err;
187 }
188
189 return 0;
190}