yamon-dt.c 6.0 KB

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