devtree.c 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. * linux/arch/arm/kernel/devtree.c
  3. *
  4. * Copyright (C) 2009 Canonical Ltd. <jeremy.kerr@canonical.com>
  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/module.h>
  12. #include <linux/errno.h>
  13. #include <linux/types.h>
  14. #include <linux/bootmem.h>
  15. #include <linux/memblock.h>
  16. #include <linux/of.h>
  17. #include <linux/of_fdt.h>
  18. #include <linux/of_irq.h>
  19. #include <linux/of_platform.h>
  20. #include <asm/setup.h>
  21. #include <asm/page.h>
  22. #include <asm/mach/arch.h>
  23. #include <asm/mach-types.h>
  24. void __init early_init_dt_add_memory_arch(u64 base, u64 size)
  25. {
  26. arm_add_memory(base, size);
  27. }
  28. void * __init early_init_dt_alloc_memory_arch(u64 size, u64 align)
  29. {
  30. return alloc_bootmem_align(size, align);
  31. }
  32. void __init arm_dt_memblock_reserve(void)
  33. {
  34. u64 *reserve_map, base, size;
  35. if (!initial_boot_params)
  36. return;
  37. /* Reserve the dtb region */
  38. memblock_reserve(virt_to_phys(initial_boot_params),
  39. be32_to_cpu(initial_boot_params->totalsize));
  40. /*
  41. * Process the reserve map. This will probably overlap the initrd
  42. * and dtb locations which are already reserved, but overlaping
  43. * doesn't hurt anything
  44. */
  45. reserve_map = ((void*)initial_boot_params) +
  46. be32_to_cpu(initial_boot_params->off_mem_rsvmap);
  47. while (1) {
  48. base = be64_to_cpup(reserve_map++);
  49. size = be64_to_cpup(reserve_map++);
  50. if (!size)
  51. break;
  52. memblock_reserve(base, size);
  53. }
  54. }
  55. /**
  56. * setup_machine_fdt - Machine setup when an dtb was passed to the kernel
  57. * @dt_phys: physical address of dt blob
  58. *
  59. * If a dtb was passed to the kernel in r2, then use it to choose the
  60. * correct machine_desc and to setup the system.
  61. */
  62. struct machine_desc * __init setup_machine_fdt(unsigned int dt_phys)
  63. {
  64. struct boot_param_header *devtree;
  65. struct machine_desc *mdesc, *mdesc_best = NULL;
  66. unsigned int score, mdesc_score = ~1;
  67. unsigned long dt_root;
  68. const char *model;
  69. if (!dt_phys)
  70. return NULL;
  71. devtree = phys_to_virt(dt_phys);
  72. /* check device tree validity */
  73. if (be32_to_cpu(devtree->magic) != OF_DT_HEADER)
  74. return NULL;
  75. /* Search the mdescs for the 'best' compatible value match */
  76. initial_boot_params = devtree;
  77. dt_root = of_get_flat_dt_root();
  78. for_each_machine_desc(mdesc) {
  79. score = of_flat_dt_match(dt_root, mdesc->dt_compat);
  80. if (score > 0 && score < mdesc_score) {
  81. mdesc_best = mdesc;
  82. mdesc_score = score;
  83. }
  84. }
  85. if (!mdesc_best) {
  86. const char *prop;
  87. long size;
  88. early_print("\nError: unrecognized/unsupported "
  89. "device tree compatible list:\n[ ");
  90. prop = of_get_flat_dt_prop(dt_root, "compatible", &size);
  91. while (size > 0) {
  92. early_print("'%s' ", prop);
  93. size -= strlen(prop) + 1;
  94. prop += strlen(prop) + 1;
  95. }
  96. early_print("]\n\n");
  97. dump_machine_table(); /* does not return */
  98. }
  99. model = of_get_flat_dt_prop(dt_root, "model", NULL);
  100. if (!model)
  101. model = of_get_flat_dt_prop(dt_root, "compatible", NULL);
  102. if (!model)
  103. model = "<unknown>";
  104. pr_info("Machine: %s, model: %s\n", mdesc_best->name, model);
  105. /* Retrieve various information from the /chosen node */
  106. of_scan_flat_dt(early_init_dt_scan_chosen, boot_command_line);
  107. /* Initialize {size,address}-cells info */
  108. of_scan_flat_dt(early_init_dt_scan_root, NULL);
  109. /* Setup memory, calling early_init_dt_add_memory_arch */
  110. of_scan_flat_dt(early_init_dt_scan_memory, NULL);
  111. /* Change machine number to match the mdesc we're using */
  112. __machine_arch_type = mdesc_best->nr;
  113. return mdesc_best;
  114. }
  115. /**
  116. * irq_create_of_mapping - Hook to resolve OF irq specifier into a Linux irq#
  117. *
  118. * Currently the mapping mechanism is trivial; simple flat hwirq numbers are
  119. * mapped 1:1 onto Linux irq numbers. Cascaded irq controllers are not
  120. * supported.
  121. */
  122. unsigned int irq_create_of_mapping(struct device_node *controller,
  123. const u32 *intspec, unsigned int intsize)
  124. {
  125. return intspec[0];
  126. }
  127. EXPORT_SYMBOL_GPL(irq_create_of_mapping);