scom.c 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  1. /*
  2. * Copyright 2010 Benjamin Herrenschmidt, IBM Corp
  3. * <benh@kernel.crashing.org>
  4. * and David Gibson, IBM Corporation.
  5. *
  6. * This program is free software; you can redistribute it and/or modify
  7. * it under the terms of the GNU General Public License as published by
  8. * the Free Software Foundation; either version 2 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * This program is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See
  14. * the GNU General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU General Public License
  17. * along with this program; if not, write to the Free Software
  18. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  19. */
  20. #include <linux/kernel.h>
  21. #include <linux/slab.h>
  22. #include <linux/export.h>
  23. #include <asm/debugfs.h>
  24. #include <asm/prom.h>
  25. #include <asm/scom.h>
  26. #include <linux/uaccess.h>
  27. const struct scom_controller *scom_controller;
  28. EXPORT_SYMBOL_GPL(scom_controller);
  29. struct device_node *scom_find_parent(struct device_node *node)
  30. {
  31. struct device_node *par, *tmp;
  32. const u32 *p;
  33. for (par = of_node_get(node); par;) {
  34. if (of_get_property(par, "scom-controller", NULL))
  35. break;
  36. p = of_get_property(par, "scom-parent", NULL);
  37. tmp = par;
  38. if (p == NULL)
  39. par = of_get_parent(par);
  40. else
  41. par = of_find_node_by_phandle(*p);
  42. of_node_put(tmp);
  43. }
  44. return par;
  45. }
  46. EXPORT_SYMBOL_GPL(scom_find_parent);
  47. scom_map_t scom_map_device(struct device_node *dev, int index)
  48. {
  49. struct device_node *parent;
  50. unsigned int cells, size;
  51. const __be32 *prop, *sprop;
  52. u64 reg, cnt;
  53. scom_map_t ret;
  54. parent = scom_find_parent(dev);
  55. if (parent == NULL)
  56. return 0;
  57. /*
  58. * We support "scom-reg" properties for adding scom registers
  59. * to a random device-tree node with an explicit scom-parent
  60. *
  61. * We also support the simple "reg" property if the device is
  62. * a direct child of a scom controller.
  63. *
  64. * In case both exist, "scom-reg" takes precedence.
  65. */
  66. prop = of_get_property(dev, "scom-reg", &size);
  67. sprop = of_get_property(parent, "#scom-cells", NULL);
  68. if (!prop && parent == dev->parent) {
  69. prop = of_get_property(dev, "reg", &size);
  70. sprop = of_get_property(parent, "#address-cells", NULL);
  71. }
  72. if (!prop)
  73. return NULL;
  74. cells = sprop ? be32_to_cpup(sprop) : 1;
  75. size >>= 2;
  76. if (index >= (size / (2*cells)))
  77. return 0;
  78. reg = of_read_number(&prop[index * cells * 2], cells);
  79. cnt = of_read_number(&prop[index * cells * 2 + cells], cells);
  80. ret = scom_map(parent, reg, cnt);
  81. of_node_put(parent);
  82. return ret;
  83. }
  84. EXPORT_SYMBOL_GPL(scom_map_device);
  85. #ifdef CONFIG_SCOM_DEBUGFS
  86. struct scom_debug_entry {
  87. struct device_node *dn;
  88. struct debugfs_blob_wrapper path;
  89. char name[16];
  90. };
  91. static ssize_t scom_debug_read(struct file *filp, char __user *ubuf,
  92. size_t count, loff_t *ppos)
  93. {
  94. struct scom_debug_entry *ent = filp->private_data;
  95. u64 __user *ubuf64 = (u64 __user *)ubuf;
  96. loff_t off = *ppos;
  97. ssize_t done = 0;
  98. u64 reg, reg_cnt, val;
  99. scom_map_t map;
  100. int rc;
  101. if (off < 0 || (off & 7) || (count & 7))
  102. return -EINVAL;
  103. reg = off >> 3;
  104. reg_cnt = count >> 3;
  105. map = scom_map(ent->dn, reg, reg_cnt);
  106. if (!scom_map_ok(map))
  107. return -ENXIO;
  108. for (reg = 0; reg < reg_cnt; reg++) {
  109. rc = scom_read(map, reg, &val);
  110. if (!rc)
  111. rc = put_user(val, ubuf64);
  112. if (rc) {
  113. if (!done)
  114. done = rc;
  115. break;
  116. }
  117. ubuf64++;
  118. *ppos += 8;
  119. done += 8;
  120. }
  121. scom_unmap(map);
  122. return done;
  123. }
  124. static ssize_t scom_debug_write(struct file* filp, const char __user *ubuf,
  125. size_t count, loff_t *ppos)
  126. {
  127. struct scom_debug_entry *ent = filp->private_data;
  128. u64 __user *ubuf64 = (u64 __user *)ubuf;
  129. loff_t off = *ppos;
  130. ssize_t done = 0;
  131. u64 reg, reg_cnt, val;
  132. scom_map_t map;
  133. int rc;
  134. if (off < 0 || (off & 7) || (count & 7))
  135. return -EINVAL;
  136. reg = off >> 3;
  137. reg_cnt = count >> 3;
  138. map = scom_map(ent->dn, reg, reg_cnt);
  139. if (!scom_map_ok(map))
  140. return -ENXIO;
  141. for (reg = 0; reg < reg_cnt; reg++) {
  142. rc = get_user(val, ubuf64);
  143. if (!rc)
  144. rc = scom_write(map, reg, val);
  145. if (rc) {
  146. if (!done)
  147. done = rc;
  148. break;
  149. }
  150. ubuf64++;
  151. done += 8;
  152. }
  153. scom_unmap(map);
  154. return done;
  155. }
  156. static const struct file_operations scom_debug_fops = {
  157. .read = scom_debug_read,
  158. .write = scom_debug_write,
  159. .open = simple_open,
  160. .llseek = default_llseek,
  161. };
  162. static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
  163. int i)
  164. {
  165. struct scom_debug_entry *ent;
  166. struct dentry *dir;
  167. ent = kzalloc(sizeof(*ent), GFP_KERNEL);
  168. if (!ent)
  169. return -ENOMEM;
  170. ent->dn = of_node_get(dn);
  171. snprintf(ent->name, 16, "%08x", i);
  172. ent->path.data = (void*)kasprintf(GFP_KERNEL, "%pOF", dn);
  173. ent->path.size = strlen((char *)ent->path.data);
  174. dir = debugfs_create_dir(ent->name, root);
  175. if (!dir) {
  176. of_node_put(dn);
  177. kfree(ent->path.data);
  178. kfree(ent);
  179. return -1;
  180. }
  181. debugfs_create_blob("devspec", 0400, dir, &ent->path);
  182. debugfs_create_file("access", 0600, dir, ent, &scom_debug_fops);
  183. return 0;
  184. }
  185. static int scom_debug_init(void)
  186. {
  187. struct device_node *dn;
  188. struct dentry *root;
  189. int i, rc;
  190. root = debugfs_create_dir("scom", powerpc_debugfs_root);
  191. if (!root)
  192. return -1;
  193. i = rc = 0;
  194. for_each_node_with_property(dn, "scom-controller") {
  195. int id = of_get_ibm_chip_id(dn);
  196. if (id == -1)
  197. id = i;
  198. rc |= scom_debug_init_one(root, dn, id);
  199. i++;
  200. }
  201. return rc;
  202. }
  203. device_initcall(scom_debug_init);
  204. #endif /* CONFIG_SCOM_DEBUGFS */