board.c 4.8 KB

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