1 /* 2 * Copyright (C) 2016 Imagination Technologies 3 * Author: Paul Burton <paul.burton@mips.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 #include <asm/yamon-dt.h> 21 22 #define MAX_MEM_ARRAY_ENTRIES 2 23 24 __init int yamon_dt_append_cmdline(void *fdt) 25 { 26 int err, chosen_off; 27 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; 36 } 37 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; 42 } 43 44 return 0; 45 } 46 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) 52 { 53 const struct yamon_mem_region *mr; 54 unsigned long size; 55 unsigned int entries = 0; 56 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; 62 } 63 64 /* How much of the remaining RAM fits in the next region? */ 65 size = min_t(unsigned long, memsize, mr->size); 66 memsize -= size; 67 68 /* Emit a memory region */ 69 *(mem_array++) = cpu_to_be32(mr->start); 70 *(mem_array++) = cpu_to_be32(size); 71 ++entries; 72 73 /* Discard the next mr->discard bytes */ 74 memsize -= min_t(unsigned long, memsize, mr->discard); 75 } 76 return entries; 77 } 78 79 __init int yamon_dt_append_memory(void *fdt, 80 const struct yamon_mem_region *regions) 81 { 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", 88 }; 89 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; 95 96 err = kstrtoul(var, 0, &phys_memsize); 97 if (!err) 98 break; 99 100 pr_warn("Failed to read the '%s' env variable '%s'\n", 101 var_names[i], var); 102 } 103 104 if (!phys_memsize) { 105 pr_warn("The bootloader didn't provide memsize: defaulting to 32MB\n"); 106 phys_memsize = 32 << 20; 107 } 108 109 /* default to using all available RAM */ 110 memsize = phys_memsize; 111 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; 118 119 memsize = memparse(var + strlen(param_name), NULL); 120 } 121 122 /* if the user says there's more RAM than we thought, believe them */ 123 phys_memsize = max_t(unsigned long, phys_memsize, memsize); 124 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; 132 } 133 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; 138 } 139 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; 147 } 148 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; 156 } 157 158 return 0; 159 } 160 161 __init int yamon_dt_serial_config(void *fdt) 162 { 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; 168 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; 178 } 179 180 baud = stop_bits = 0; 181 parity = 0; 182 hw_flow = false; 183 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++; 191 } 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; 204 } 205 206 if (!baud) 207 baud = 38400; 208 209 if (parity != 'e' && parity != 'n' && parity != 'o') 210 parity = 'n'; 211 212 if (stop_bits != 7 && stop_bits != 8) 213 stop_bits = 8; 214 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)); 218 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; 227 } 228 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; 233 } 234 235 return 0; 236 } 237