dcr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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. #ifdef CONFIG_PPC_DCR_MMIO
  24. static struct device_node *find_dcr_parent(struct device_node *node)
  25. {
  26. struct device_node *par, *tmp;
  27. const u32 *p;
  28. for (par = of_node_get(node); par;) {
  29. if (of_get_property(par, "dcr-controller", NULL))
  30. break;
  31. p = of_get_property(par, "dcr-parent", NULL);
  32. tmp = par;
  33. if (p == NULL)
  34. par = of_get_parent(par);
  35. else
  36. par = of_find_node_by_phandle(*p);
  37. of_node_put(tmp);
  38. }
  39. return par;
  40. }
  41. #endif
  42. #if defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO)
  43. bool dcr_map_ok_generic(dcr_host_t host)
  44. {
  45. if (host.type == DCR_HOST_NATIVE)
  46. return dcr_map_ok_native(host.host.native);
  47. else if (host.type == DCR_HOST_MMIO)
  48. return dcr_map_ok_mmio(host.host.mmio);
  49. else
  50. return 0;
  51. }
  52. EXPORT_SYMBOL_GPL(dcr_map_ok_generic);
  53. dcr_host_t dcr_map_generic(struct device_node *dev,
  54. unsigned int dcr_n,
  55. unsigned int dcr_c)
  56. {
  57. dcr_host_t host;
  58. struct device_node *dp;
  59. const char *prop;
  60. host.type = DCR_HOST_INVALID;
  61. dp = find_dcr_parent(dev);
  62. if (dp == NULL)
  63. return host;
  64. prop = of_get_property(dp, "dcr-access-method", NULL);
  65. pr_debug("dcr_map_generic(dcr-access-method = %s)\n", prop);
  66. if (!strcmp(prop, "native")) {
  67. host.type = DCR_HOST_NATIVE;
  68. host.host.native = dcr_map_native(dev, dcr_n, dcr_c);
  69. } else if (!strcmp(prop, "mmio")) {
  70. host.type = DCR_HOST_MMIO;
  71. host.host.mmio = dcr_map_mmio(dev, dcr_n, dcr_c);
  72. }
  73. of_node_put(dp);
  74. return host;
  75. }
  76. EXPORT_SYMBOL_GPL(dcr_map_generic);
  77. void dcr_unmap_generic(dcr_host_t host, unsigned int dcr_c)
  78. {
  79. if (host.type == DCR_HOST_NATIVE)
  80. dcr_unmap_native(host.host.native, dcr_c);
  81. else if (host.type == DCR_HOST_MMIO)
  82. dcr_unmap_mmio(host.host.mmio, dcr_c);
  83. else /* host.type == DCR_HOST_INVALID */
  84. WARN_ON(true);
  85. }
  86. EXPORT_SYMBOL_GPL(dcr_unmap_generic);
  87. u32 dcr_read_generic(dcr_host_t host, unsigned int dcr_n)
  88. {
  89. if (host.type == DCR_HOST_NATIVE)
  90. return dcr_read_native(host.host.native, dcr_n);
  91. else if (host.type == DCR_HOST_MMIO)
  92. return dcr_read_mmio(host.host.mmio, dcr_n);
  93. else /* host.type == DCR_HOST_INVALID */
  94. WARN_ON(true);
  95. return 0;
  96. }
  97. EXPORT_SYMBOL_GPL(dcr_read_generic);
  98. void dcr_write_generic(dcr_host_t host, unsigned int dcr_n, u32 value)
  99. {
  100. if (host.type == DCR_HOST_NATIVE)
  101. dcr_write_native(host.host.native, dcr_n, value);
  102. else if (host.type == DCR_HOST_MMIO)
  103. dcr_write_mmio(host.host.mmio, dcr_n, value);
  104. else /* host.type == DCR_HOST_INVALID */
  105. WARN_ON(true);
  106. }
  107. EXPORT_SYMBOL_GPL(dcr_write_generic);
  108. #endif /* defined(CONFIG_PPC_DCR_NATIVE) && defined(CONFIG_PPC_DCR_MMIO) */
  109. unsigned int dcr_resource_start(const struct device_node *np,
  110. unsigned int index)
  111. {
  112. unsigned int ds;
  113. const u32 *dr = of_get_property(np, "dcr-reg", &ds);
  114. if (dr == NULL || ds & 1 || index >= (ds / 8))
  115. return 0;
  116. return dr[index * 2];
  117. }
  118. EXPORT_SYMBOL_GPL(dcr_resource_start);
  119. unsigned int dcr_resource_len(const struct device_node *np, unsigned int index)
  120. {
  121. unsigned int ds;
  122. const u32 *dr = of_get_property(np, "dcr-reg", &ds);
  123. if (dr == NULL || ds & 1 || index >= (ds / 8))
  124. return 0;
  125. return dr[index * 2 + 1];
  126. }
  127. EXPORT_SYMBOL_GPL(dcr_resource_len);
  128. #ifdef CONFIG_PPC_DCR_MMIO
  129. u64 of_translate_dcr_address(struct device_node *dev,
  130. unsigned int dcr_n,
  131. unsigned int *out_stride)
  132. {
  133. struct device_node *dp;
  134. const u32 *p;
  135. unsigned int stride;
  136. u64 ret = OF_BAD_ADDR;
  137. dp = find_dcr_parent(dev);
  138. if (dp == NULL)
  139. return OF_BAD_ADDR;
  140. /* Stride is not properly defined yet, default to 0x10 for Axon */
  141. p = of_get_property(dp, "dcr-mmio-stride", NULL);
  142. stride = (p == NULL) ? 0x10 : *p;
  143. /* XXX FIXME: Which property name is to use of the 2 following ? */
  144. p = of_get_property(dp, "dcr-mmio-range", NULL);
  145. if (p == NULL)
  146. p = of_get_property(dp, "dcr-mmio-space", NULL);
  147. if (p == NULL)
  148. goto done;
  149. /* Maybe could do some better range checking here */
  150. ret = of_translate_address(dp, p);
  151. if (ret != OF_BAD_ADDR)
  152. ret += (u64)(stride) * (u64)dcr_n;
  153. if (out_stride)
  154. *out_stride = stride;
  155. done:
  156. of_node_put(dp);
  157. return ret;
  158. }
  159. dcr_host_mmio_t dcr_map_mmio(struct device_node *dev,
  160. unsigned int dcr_n,
  161. unsigned int dcr_c)
  162. {
  163. dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
  164. u64 addr;
  165. pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
  166. dev->full_name, dcr_n, dcr_c);
  167. addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
  168. pr_debug("translates to addr: 0x%llx, stride: 0x%x\n",
  169. (unsigned long long) addr, ret.stride);
  170. if (addr == OF_BAD_ADDR)
  171. return ret;
  172. pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
  173. ret.token = ioremap(addr, dcr_c * ret.stride);
  174. if (ret.token == NULL)
  175. return ret;
  176. pr_debug("mapped at 0x%p -> base is 0x%p\n",
  177. ret.token, ret.token - dcr_n * ret.stride);
  178. ret.token -= dcr_n * ret.stride;
  179. return ret;
  180. }
  181. EXPORT_SYMBOL_GPL(dcr_map_mmio);
  182. void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c)
  183. {
  184. dcr_host_mmio_t h = host;
  185. if (h.token == NULL)
  186. return;
  187. h.token += host.base * h.stride;
  188. iounmap(h.token);
  189. h.token = NULL;
  190. }
  191. EXPORT_SYMBOL_GPL(dcr_unmap_mmio);
  192. #endif /* defined(CONFIG_PPC_DCR_MMIO) */
  193. #ifdef CONFIG_PPC_DCR_NATIVE
  194. DEFINE_SPINLOCK(dcr_ind_lock);
  195. #endif /* defined(CONFIG_PPC_DCR_NATIVE) */