dcr.c 6.1 KB

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