dcr.c 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137
  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 = 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. unsigned int dcr_resource_len(struct device_node *np, unsigned int index)
  32. {
  33. unsigned int ds;
  34. const u32 *dr = get_property(np, "dcr-reg", &ds);
  35. if (dr == NULL || ds & 1 || index >= (ds / 8))
  36. return 0;
  37. return dr[index * 2 + 1];
  38. }
  39. #ifndef CONFIG_PPC_DCR_NATIVE
  40. static struct device_node * find_dcr_parent(struct device_node * node)
  41. {
  42. struct device_node *par, *tmp;
  43. const u32 *p;
  44. for (par = of_node_get(node); par;) {
  45. if (get_property(par, "dcr-controller", NULL))
  46. break;
  47. p = get_property(par, "dcr-parent", NULL);
  48. tmp = par;
  49. if (p == NULL)
  50. par = of_get_parent(par);
  51. else
  52. par = of_find_node_by_phandle(*p);
  53. of_node_put(tmp);
  54. }
  55. return par;
  56. }
  57. u64 of_translate_dcr_address(struct device_node *dev,
  58. unsigned int dcr_n,
  59. unsigned int *out_stride)
  60. {
  61. struct device_node *dp;
  62. const u32 *p;
  63. unsigned int stride;
  64. u64 ret;
  65. dp = find_dcr_parent(dev);
  66. if (dp == NULL)
  67. return OF_BAD_ADDR;
  68. /* Stride is not properly defined yet, default to 0x10 for Axon */
  69. p = get_property(dp, "dcr-mmio-stride", NULL);
  70. stride = (p == NULL) ? 0x10 : *p;
  71. /* XXX FIXME: Which property name is to use of the 2 following ? */
  72. p = get_property(dp, "dcr-mmio-range", NULL);
  73. if (p == NULL)
  74. p = get_property(dp, "dcr-mmio-space", NULL);
  75. if (p == NULL)
  76. return OF_BAD_ADDR;
  77. /* Maybe could do some better range checking here */
  78. ret = of_translate_address(dp, p);
  79. if (ret != OF_BAD_ADDR)
  80. ret += (u64)(stride) * (u64)dcr_n;
  81. if (out_stride)
  82. *out_stride = stride;
  83. return ret;
  84. }
  85. dcr_host_t dcr_map(struct device_node *dev, unsigned int dcr_n,
  86. unsigned int dcr_c)
  87. {
  88. dcr_host_t ret = { .token = NULL, .stride = 0 };
  89. u64 addr;
  90. pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
  91. dev->full_name, dcr_n, dcr_c);
  92. addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
  93. pr_debug("translates to addr: 0x%lx, stride: 0x%x\n",
  94. addr, ret.stride);
  95. if (addr == OF_BAD_ADDR)
  96. return ret;
  97. pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
  98. ret.token = ioremap(addr, dcr_c * ret.stride);
  99. if (ret.token == NULL)
  100. return ret;
  101. pr_debug("mapped at 0x%p -> base is 0x%p\n",
  102. ret.token, ret.token - dcr_n * ret.stride);
  103. ret.token -= dcr_n * ret.stride;
  104. return ret;
  105. }
  106. void dcr_unmap(dcr_host_t host, unsigned int dcr_n, unsigned int dcr_c)
  107. {
  108. dcr_host_t h = host;
  109. if (h.token == NULL)
  110. return;
  111. h.token -= dcr_n * h.stride;
  112. iounmap(h.token);
  113. h.token = NULL;
  114. }
  115. #endif /* !defined(CONFIG_PPC_DCR_NATIVE) */