board.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. // SPDX-License-Identifier: GPL-2.0
  2. /*
  3. * Copyright (C) 2014 Magnus Damm
  4. * Copyright (C) 2015 Glider bvba
  5. */
  6. #define pr_fmt(fmt) "board_staging: " fmt
  7. #include <linux/clkdev.h>
  8. #include <linux/init.h>
  9. #include <linux/irq.h>
  10. #include <linux/device.h>
  11. #include <linux/kernel.h>
  12. #include <linux/of.h>
  13. #include <linux/of_address.h>
  14. #include <linux/of_irq.h>
  15. #include <linux/platform_device.h>
  16. #include <linux/pm_domain.h>
  17. #include "board.h"
  18. static struct device_node *irqc_node __initdata;
  19. static unsigned int irqc_base __initdata;
  20. static bool find_by_address(u64 base_address)
  21. {
  22. struct device_node *dn = of_find_all_nodes(NULL);
  23. struct resource res;
  24. while (dn) {
  25. if (!of_address_to_resource(dn, 0, &res)) {
  26. if (res.start == base_address) {
  27. of_node_put(dn);
  28. return true;
  29. }
  30. }
  31. dn = of_find_all_nodes(dn);
  32. }
  33. return false;
  34. }
  35. bool __init board_staging_dt_node_available(const struct resource *resource,
  36. unsigned int num_resources)
  37. {
  38. unsigned int i;
  39. for (i = 0; i < num_resources; i++) {
  40. const struct resource *r = resource + i;
  41. if (resource_type(r) == IORESOURCE_MEM)
  42. if (find_by_address(r->start))
  43. return true; /* DT node available */
  44. }
  45. return false; /* Nothing found */
  46. }
  47. int __init board_staging_gic_setup_xlate(const char *gic_match,
  48. unsigned int base)
  49. {
  50. WARN_ON(irqc_node);
  51. irqc_node = of_find_compatible_node(NULL, NULL, gic_match);
  52. WARN_ON(!irqc_node);
  53. if (!irqc_node)
  54. return -ENOENT;
  55. irqc_base = base;
  56. return 0;
  57. }
  58. static void __init gic_fixup_resource(struct resource *res)
  59. {
  60. struct of_phandle_args irq_data;
  61. unsigned int hwirq = res->start;
  62. unsigned int virq;
  63. if (resource_type(res) != IORESOURCE_IRQ || !irqc_node)
  64. return;
  65. irq_data.np = irqc_node;
  66. irq_data.args_count = 3;
  67. irq_data.args[0] = 0;
  68. irq_data.args[1] = hwirq - irqc_base;
  69. switch (res->flags &
  70. (IORESOURCE_IRQ_LOWEDGE | IORESOURCE_IRQ_HIGHEDGE |
  71. IORESOURCE_IRQ_LOWLEVEL | IORESOURCE_IRQ_HIGHLEVEL)) {
  72. case IORESOURCE_IRQ_LOWEDGE:
  73. irq_data.args[2] = IRQ_TYPE_EDGE_FALLING;
  74. break;
  75. case IORESOURCE_IRQ_HIGHEDGE:
  76. irq_data.args[2] = IRQ_TYPE_EDGE_RISING;
  77. break;
  78. case IORESOURCE_IRQ_LOWLEVEL:
  79. irq_data.args[2] = IRQ_TYPE_LEVEL_LOW;
  80. break;
  81. case IORESOURCE_IRQ_HIGHLEVEL:
  82. default:
  83. irq_data.args[2] = IRQ_TYPE_LEVEL_HIGH;
  84. break;
  85. }
  86. virq = irq_create_of_mapping(&irq_data);
  87. if (WARN_ON(!virq))
  88. return;
  89. pr_debug("hwirq %u -> virq %u\n", hwirq, virq);
  90. res->start = virq;
  91. }
  92. void __init board_staging_gic_fixup_resources(struct resource *res,
  93. unsigned int nres)
  94. {
  95. unsigned int i;
  96. for (i = 0; i < nres; i++)
  97. gic_fixup_resource(&res[i]);
  98. }
  99. int __init board_staging_register_clock(const struct board_staging_clk *bsc)
  100. {
  101. int error;
  102. pr_debug("Aliasing clock %s for con_id %s dev_id %s\n", bsc->clk,
  103. bsc->con_id, bsc->dev_id);
  104. error = clk_add_alias(bsc->con_id, bsc->dev_id, bsc->clk, NULL);
  105. if (error)
  106. pr_err("Failed to alias clock %s (%d)\n", bsc->clk, error);
  107. return error;
  108. }
  109. #ifdef CONFIG_PM_GENERIC_DOMAINS_OF
  110. static int board_staging_add_dev_domain(struct platform_device *pdev,
  111. const char *domain)
  112. {
  113. struct of_phandle_args pd_args;
  114. struct device_node *np;
  115. np = of_find_node_by_path(domain);
  116. if (!np) {
  117. pr_err("Cannot find domain node %s\n", domain);
  118. return -ENOENT;
  119. }
  120. pd_args.np = np;
  121. pd_args.args_count = 0;
  122. return of_genpd_add_device(&pd_args, &pdev->dev);
  123. }
  124. #else
  125. static inline int board_staging_add_dev_domain(struct platform_device *pdev,
  126. const char *domain)
  127. {
  128. return 0;
  129. }
  130. #endif
  131. int __init board_staging_register_device(const struct board_staging_dev *dev)
  132. {
  133. struct platform_device *pdev = dev->pdev;
  134. unsigned int i;
  135. int error;
  136. pr_debug("Trying to register device %s\n", pdev->name);
  137. if (board_staging_dt_node_available(pdev->resource,
  138. pdev->num_resources)) {
  139. pr_warn("Skipping %s, already in DT\n", pdev->name);
  140. return -EEXIST;
  141. }
  142. board_staging_gic_fixup_resources(pdev->resource, pdev->num_resources);
  143. for (i = 0; i < dev->nclocks; i++)
  144. board_staging_register_clock(&dev->clocks[i]);
  145. if (dev->domain)
  146. board_staging_add_dev_domain(pdev, dev->domain);
  147. error = platform_device_register(pdev);
  148. if (error) {
  149. pr_err("Failed to register device %s (%d)\n", pdev->name,
  150. error);
  151. return error;
  152. }
  153. return error;
  154. }
  155. void __init board_staging_register_devices(const struct board_staging_dev *devs,
  156. unsigned int ndevs)
  157. {
  158. unsigned int i;
  159. for (i = 0; i < ndevs; i++)
  160. board_staging_register_device(&devs[i]);
  161. }