yamon-dt.c 5.8 KB

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