devtree.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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. /**
  19. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  20. * @dt: virtual address pointer to dt blob
  21. *
  22. * If a dtb was passed to the kernel, then use it to choose the correct
  23. * machine_desc and to setup the system.
  24. */
  25. struct machine_desc * __init setup_machine_fdt(void *dt)
  26. {
  27. struct boot_param_header *devtree = dt;
  28. struct machine_desc *mdesc = NULL, *mdesc_best = NULL;
  29. unsigned int score, mdesc_score = ~1;
  30. unsigned long dt_root;
  31. const char *model, *compat;
  32. void *clk;
  33. char manufacturer[16];
  34. unsigned long len;
  35. /* check device tree validity */
  36. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
  37. return NULL;
  38. initial_boot_params = devtree;
  39. dt_root = of_get_flat_dt_root();
  40. /*
  41. * The kernel could be multi-platform enabled, thus could have many
  42. * "baked-in" machine descriptors. Search thru all for the best
  43. * "compatible" string match.
  44. */
  45. for_each_machine_desc(mdesc) {
  46. score = of_flat_dt_match(dt_root, mdesc->dt_compat);
  47. if (score > 0 && score < mdesc_score) {
  48. mdesc_best = mdesc;
  49. mdesc_score = score;
  50. }
  51. }
  52. if (!mdesc_best) {
  53. const char *prop;
  54. long size;
  55. pr_err("\n unrecognized device tree list:\n[ ");
  56. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  57. if (prop) {
  58. while (size > 0) {
  59. printk("'%s' ", prop);
  60. size -= strlen(prop) + 1;
  61. prop += strlen(prop) + 1;
  62. }
  63. }
  64. printk("]\n\n");
  65. machine_halt();
  66. }
  67. /* compat = "<manufacturer>,<model>" */
  68. compat = mdesc_best->dt_compat[0];
  69. model = strchr(compat, ',');
  70. if (model)
  71. model++;
  72. strlcpy(manufacturer, compat, model ? model - compat : strlen(compat));
  73. pr_info("Board \"%s\" from %s (Manufacturer)\n", model, manufacturer);
  74. /* Retrieve various information from the /chosen node */
  75. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  76. /* Initialize {size,address}-cells info */
  77. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  78. /* Setup memory, calling early_init_dt_add_memory_arch */
  79. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  80. clk = of_get_flat_dt_prop(dt_root, "clock-frequency", &len);
  81. if (clk)
  82. arc_set_core_freq(of_read_ulong(clk, len/4));
  83. return mdesc_best;
  84. }
  85. /*
  86. * Copy the flattened DT out of .init since unflattening doesn't copy strings
  87. * and the normal DT APIs refs them from orig flat DT
  88. */
  89. void __init copy_devtree(void)
  90. {
  91. void *alloc = early_init_dt_alloc_memory_arch(
  92. be32_to_cpu(initial_boot_params->totalsize), 64);
  93. if (alloc) {
  94. memcpy(alloc, initial_boot_params,
  95. be32_to_cpu(initial_boot_params->totalsize));
  96. initial_boot_params = alloc;
  97. }
  98. }