dcr.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. /*
  2. * (c) Copyright 2006 Benjamin Herrenschmidt, IBM Corp.
  3. * <benh@kernel.crashing.org>
  4. *
  5. * This program is free software; you can redistribute it and/or modify
  6. * it under the terms of the GNU General Public License as published by
  7. * the Free Software Foundation; either version 2 of the License, or
  8. * (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  13. * the GNU General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  18. */
  19. #undef DEBUG
  20. #include <linux/kernel.h>
  21. #include <asm/prom.h>
  22. #include <asm/dcr.h>
  23. unsigned int dcr_resource_start(struct device_node *np, unsigned int index)
  24. {
  25. unsigned int ds;
  26. const u32 *dr = of_get_property(np, "dcr-reg", &ds);
  27. if (dr == NULL || ds & 1 || index >= (ds / 8))
  28. return 0;
  29. return dr[index * 2];
  30. }
  31. EXPORT_SYMBOL_GPL(dcr_resource_start);
  32. unsigned int dcr_resource_len(struct device_node *np, unsigned int index)
  33. {
  34. unsigned int ds;
  35. const u32 *dr = of_get_property(np, "dcr-reg", &ds);
  36. if (dr == NULL || ds & 1 || index >= (ds / 8))
  37. return 0;
  38. return dr[index * 2 + 1];
  39. }
  40. EXPORT_SYMBOL_GPL(dcr_resource_len);
  41. #ifndef CONFIG_PPC_DCR_NATIVE
  42. static struct device_node * find_dcr_parent(struct device_node * node)
  43. {
  44. struct device_node *par, *tmp;
  45. const u32 *p;
  46. for (par = of_node_get(node); par;) {
  47. if (of_get_property(par, "dcr-controller", NULL))
  48. break;
  49. p = of_get_property(par, "dcr-parent", NULL);
  50. tmp = par;
  51. if (p == NULL)
  52. par = of_get_parent(par);
  53. else
  54. par = of_find_node_by_phandle(*p);
  55. of_node_put(tmp);
  56. }
  57. return par;
  58. }
  59. u64 of_translate_dcr_address(struct device_node *dev,
  60. unsigned int dcr_n,
  61. unsigned int *out_stride)
  62. {
  63. struct device_node *dp;
  64. const u32 *p;
  65. unsigned int stride;
  66. u64 ret;
  67. dp = find_dcr_parent(dev);
  68. if (dp == NULL)
  69. return OF_BAD_ADDR;
  70. /* Stride is not properly defined yet, default to 0x10 for Axon */
  71. p = of_get_property(dp, "dcr-mmio-stride", NULL);
  72. stride = (p == NULL) ? 0x10 : *p;
  73. /* XXX FIXME: Which property name is to use of the 2 following ? */
  74. p = of_get_property(dp, "dcr-mmio-range", NULL);
  75. if (p == NULL)
  76. p = of_get_property(dp, "dcr-mmio-space", NULL);
  77. if (p == NULL)
  78. return OF_BAD_ADDR;
  79. /* Maybe could do some better range checking here */
  80. ret = of_translate_address(dp, p);
  81. if (ret != OF_BAD_ADDR)
  82. ret += (u64)(stride) * (u64)dcr_n;
  83. if (out_stride)
  84. *out_stride = stride;
  85. return ret;
  86. }
  87. dcr_host_t dcr_map(struct device_node *dev, unsigned int dcr_n,
  88. unsigned int dcr_c)
  89. {
  90. dcr_host_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
  91. u64 addr;
  92. pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
  93. dev->full_name, dcr_n, dcr_c);
  94. addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
  95. pr_debug("translates to addr: 0x%lx, stride: 0x%x\n",
  96. addr, ret.stride);
  97. if (addr == OF_BAD_ADDR)
  98. return ret;
  99. pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
  100. ret.token = ioremap(addr, dcr_c * ret.stride);
  101. if (ret.token == NULL)
  102. return ret;
  103. pr_debug("mapped at 0x%p -> base is 0x%p\n",
  104. ret.token, ret.token - dcr_n * ret.stride);
  105. ret.token -= dcr_n * ret.stride;
  106. return ret;
  107. }
  108. EXPORT_SYMBOL_GPL(dcr_map);
  109. void dcr_unmap(dcr_host_t host, unsigned int dcr_n, unsigned int dcr_c)
  110. {
  111. dcr_host_t h = host;
  112. if (h.token == NULL)
  113. return;
  114. h.token += dcr_n * h.stride;
  115. iounmap(h.token);
  116. h.token = NULL;
  117. }
  118. EXPORT_SYMBOL_GPL(dcr_unmap);
  119. #endif /* !defined(CONFIG_PPC_DCR_NATIVE) */