dcr.c 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232
  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(struct device_node *np, unsigned int index)
  110. {
  111. unsigned int ds;
  112. const u32 *dr = of_get_property(np, "dcr-reg", &ds);
  113. if (dr == NULL || ds & 1 || index >= (ds / 8))
  114. return 0;
  115. return dr[index * 2];
  116. }
  117. EXPORT_SYMBOL_GPL(dcr_resource_start);
  118. unsigned int dcr_resource_len(struct device_node *np, unsigned int index)
  119. {
  120. unsigned int ds;
  121. const u32 *dr = of_get_property(np, "dcr-reg", &ds);
  122. if (dr == NULL || ds & 1 || index >= (ds / 8))
  123. return 0;
  124. return dr[index * 2 + 1];
  125. }
  126. EXPORT_SYMBOL_GPL(dcr_resource_len);
  127. #ifdef CONFIG_PPC_DCR_MMIO
  128. u64 of_translate_dcr_address(struct device_node *dev,
  129. unsigned int dcr_n,
  130. unsigned int *out_stride)
  131. {
  132. struct device_node *dp;
  133. const u32 *p;
  134. unsigned int stride;
  135. u64 ret = OF_BAD_ADDR;
  136. dp = find_dcr_parent(dev);
  137. if (dp == NULL)
  138. return OF_BAD_ADDR;
  139. /* Stride is not properly defined yet, default to 0x10 for Axon */
  140. p = of_get_property(dp, "dcr-mmio-stride", NULL);
  141. stride = (p == NULL) ? 0x10 : *p;
  142. /* XXX FIXME: Which property name is to use of the 2 following ? */
  143. p = of_get_property(dp, "dcr-mmio-range", NULL);
  144. if (p == NULL)
  145. p = of_get_property(dp, "dcr-mmio-space", NULL);
  146. if (p == NULL)
  147. goto done;
  148. /* Maybe could do some better range checking here */
  149. ret = of_translate_address(dp, p);
  150. if (ret != OF_BAD_ADDR)
  151. ret += (u64)(stride) * (u64)dcr_n;
  152. if (out_stride)
  153. *out_stride = stride;
  154. done:
  155. of_node_put(dp);
  156. return ret;
  157. }
  158. dcr_host_mmio_t dcr_map_mmio(struct device_node *dev,
  159. unsigned int dcr_n,
  160. unsigned int dcr_c)
  161. {
  162. dcr_host_mmio_t ret = { .token = NULL, .stride = 0, .base = dcr_n };
  163. u64 addr;
  164. pr_debug("dcr_map(%s, 0x%x, 0x%x)\n",
  165. dev->full_name, dcr_n, dcr_c);
  166. addr = of_translate_dcr_address(dev, dcr_n, &ret.stride);
  167. pr_debug("translates to addr: 0x%llx, stride: 0x%x\n",
  168. (unsigned long long) addr, ret.stride);
  169. if (addr == OF_BAD_ADDR)
  170. return ret;
  171. pr_debug("mapping 0x%x bytes\n", dcr_c * ret.stride);
  172. ret.token = ioremap(addr, dcr_c * ret.stride);
  173. if (ret.token == NULL)
  174. return ret;
  175. pr_debug("mapped at 0x%p -> base is 0x%p\n",
  176. ret.token, ret.token - dcr_n * ret.stride);
  177. ret.token -= dcr_n * ret.stride;
  178. return ret;
  179. }
  180. EXPORT_SYMBOL_GPL(dcr_map_mmio);
  181. void dcr_unmap_mmio(dcr_host_mmio_t host, unsigned int dcr_c)
  182. {
  183. dcr_host_mmio_t h = host;
  184. if (h.token == NULL)
  185. return;
  186. h.token += host.base * h.stride;
  187. iounmap(h.token);
  188. h.token = NULL;
  189. }
  190. EXPORT_SYMBOL_GPL(dcr_unmap_mmio);
  191. #endif /* defined(CONFIG_PPC_DCR_MMIO) */
  192. #ifdef CONFIG_PPC_DCR_NATIVE
  193. DEFINE_SPINLOCK(dcr_ind_lock);
  194. #endif /* defined(CONFIG_PPC_DCR_NATIVE) */