devtree.c 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. /*
  2. * Copyright (C) 2012 Synopsys, Inc. (www.synopsys.com)
  3. *
  4. * Based on reduced version of METAG
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License version 2 as
  8. * published by the Free Software Foundation.
  9. */
  10. #include <linux/init.h>
  11. #include <linux/reboot.h>
  12. #include <linux/memblock.h>
  13. #include <linux/of.h>
  14. #include <linux/of_fdt.h>
  15. #include <asm/prom.h>
  16. #include <asm/clk.h>
  17. #include <asm/mach_desc.h>
  18. /* called from unflatten_device_tree() to bootstrap devicetree itself */
  19. void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  20. {
  21. return __va(memblock_alloc(size, align));
  22. }
  23. /**
  24. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  25. * @dt: virtual address pointer to dt blob
  26. *
  27. * If a dtb was passed to the kernel, then use it to choose the correct
  28. * machine_desc and to setup the system.
  29. */
  30. struct machine_desc * __init setup_machine_fdt(void *dt)
  31. {
  32. struct boot_param_header *devtree = dt;
  33. struct machine_desc *mdesc = NULL, *mdesc_best = NULL;
  34. unsigned int score, mdesc_score = ~1;
  35. unsigned long dt_root;
  36. const char *model, *compat;
  37. void *clk;
  38. char manufacturer[16];
  39. unsigned long len;
  40. /* check device tree validity */
  41. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
  42. return NULL;
  43. initial_boot_params = devtree;
  44. dt_root = of_get_flat_dt_root();
  45. /*
  46. * The kernel could be multi-platform enabled, thus could have many
  47. * "baked-in" machine descriptors. Search thru all for the best
  48. * "compatible" string match.
  49. */
  50. for_each_machine_desc(mdesc) {
  51. score = of_flat_dt_match(dt_root, mdesc->dt_compat);
  52. if (score > 0 && score < mdesc_score) {
  53. mdesc_best = mdesc;
  54. mdesc_score = score;
  55. }
  56. }
  57. if (!mdesc_best) {
  58. const char *prop;
  59. long size;
  60. pr_err("\n unrecognized device tree list:\n[ ");
  61. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  62. if (prop) {
  63. while (size > 0) {
  64. printk("'%s' ", prop);
  65. size -= strlen(prop) + 1;
  66. prop += strlen(prop) + 1;
  67. }
  68. }
  69. printk("]\n\n");
  70. machine_halt();
  71. }
  72. /* compat = "<manufacturer>,<model>" */
  73. compat = mdesc_best->dt_compat[0];
  74. model = strchr(compat, ',');
  75. if (model)
  76. model++;
  77. strlcpy(manufacturer, compat, model ? model - compat : strlen(compat));
  78. pr_info("Board \"%s\" from %s (Manufacturer)\n", model, manufacturer);
  79. /* Retrieve various information from the /chosen node */
  80. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  81. /* Initialize {size,address}-cells info */
  82. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  83. /* Setup memory, calling early_init_dt_add_memory_arch */
  84. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  85. clk = of_get_flat_dt_prop(dt_root, "clock-frequency", &len);
  86. if (clk)
  87. arc_set_core_freq(of_read_ulong(clk, len/4));
  88. return mdesc_best;
  89. }
  90. /*
  91. * Copy the flattened DT out of .init since unflattening doesn't copy strings
  92. * and the normal DT APIs refs them from orig flat DT
  93. */
  94. void __init copy_devtree(void)
  95. {
  96. void *alloc = early_init_dt_alloc_memory_arch(
  97. be32_to_cpu(initial_boot_params->totalsize), 64);
  98. if (alloc) {
  99. memcpy(alloc, initial_boot_params,
  100. be32_to_cpu(initial_boot_params->totalsize));
  101. initial_boot_params = alloc;
  102. }
  103. }