scom.c 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237
  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/debugfs.h>
  22. #include <linux/slab.h>
  23. #include <linux/export.h>
  24. #include <asm/debug.h>
  25. #include <asm/prom.h>
  26. #include <asm/scom.h>
  27. #include <asm/uaccess.h>
  28. const struct scom_controller *scom_controller;
  29. EXPORT_SYMBOL_GPL(scom_controller);
  30. struct device_node *scom_find_parent(struct device_node *node)
  31. {
  32. struct device_node *par, *tmp;
  33. const u32 *p;
  34. for (par = of_node_get(node); par;) {
  35. if (of_get_property(par, "scom-controller", NULL))
  36. break;
  37. p = of_get_property(par, "scom-parent", NULL);
  38. tmp = par;
  39. if (p == NULL)
  40. par = of_get_parent(par);
  41. else
  42. par = of_find_node_by_phandle(*p);
  43. of_node_put(tmp);
  44. }
  45. return par;
  46. }
  47. EXPORT_SYMBOL_GPL(scom_find_parent);
  48. scom_map_t scom_map_device(struct device_node *dev, int index)
  49. {
  50. struct device_node *parent;
  51. unsigned int cells, size;
  52. const __be32 *prop, *sprop;
  53. u64 reg, cnt;
  54. scom_map_t ret;
  55. parent = scom_find_parent(dev);
  56. if (parent == NULL)
  57. return 0;
  58. /*
  59. * We support "scom-reg" properties for adding scom registers
  60. * to a random device-tree node with an explicit scom-parent
  61. *
  62. * We also support the simple "reg" property if the device is
  63. * a direct child of a scom controller.
  64. *
  65. * In case both exist, "scom-reg" takes precedence.
  66. */
  67. prop = of_get_property(dev, "scom-reg", &size);
  68. sprop = of_get_property(parent, "#scom-cells", NULL);
  69. if (!prop && parent == dev->parent) {
  70. prop = of_get_property(dev, "reg", &size);
  71. sprop = of_get_property(parent, "#address-cells", NULL);
  72. }
  73. if (!prop)
  74. return NULL;
  75. cells = sprop ? be32_to_cpup(sprop) : 1;
  76. size >>= 2;
  77. if (index >= (size / (2*cells)))
  78. return 0;
  79. reg = of_read_number(&prop[index * cells * 2], cells);
  80. cnt = of_read_number(&prop[index * cells * 2 + cells], cells);
  81. ret = scom_map(parent, reg, cnt);
  82. of_node_put(parent);
  83. return ret;
  84. }
  85. EXPORT_SYMBOL_GPL(scom_map_device);
  86. #ifdef CONFIG_SCOM_DEBUGFS
  87. struct scom_debug_entry {
  88. struct device_node *dn;
  89. struct debugfs_blob_wrapper path;
  90. char name[16];
  91. };
  92. static ssize_t scom_debug_read(struct file *filp, char __user *ubuf,
  93. size_t count, loff_t *ppos)
  94. {
  95. struct scom_debug_entry *ent = filp->private_data;
  96. u64 __user *ubuf64 = (u64 __user *)ubuf;
  97. loff_t off = *ppos;
  98. ssize_t done = 0;
  99. u64 reg, reg_cnt, val;
  100. scom_map_t map;
  101. int rc;
  102. if (off < 0 || (off & 7) || (count & 7))
  103. return -EINVAL;
  104. reg = off >> 3;
  105. reg_cnt = count >> 3;
  106. map = scom_map(ent->dn, reg, reg_cnt);
  107. if (!scom_map_ok(map))
  108. return -ENXIO;
  109. for (reg = 0; reg < reg_cnt; reg++) {
  110. rc = scom_read(map, reg, &val);
  111. if (!rc)
  112. rc = put_user(val, ubuf64);
  113. if (rc) {
  114. if (!done)
  115. done = rc;
  116. break;
  117. }
  118. ubuf64++;
  119. *ppos += 8;
  120. done += 8;
  121. }
  122. scom_unmap(map);
  123. return done;
  124. }
  125. static ssize_t scom_debug_write(struct file* filp, const char __user *ubuf,
  126. size_t count, loff_t *ppos)
  127. {
  128. struct scom_debug_entry *ent = filp->private_data;
  129. u64 __user *ubuf64 = (u64 __user *)ubuf;
  130. loff_t off = *ppos;
  131. ssize_t done = 0;
  132. u64 reg, reg_cnt, val;
  133. scom_map_t map;
  134. int rc;
  135. if (off < 0 || (off & 7) || (count & 7))
  136. return -EINVAL;
  137. reg = off >> 3;
  138. reg_cnt = count >> 3;
  139. map = scom_map(ent->dn, reg, reg_cnt);
  140. if (!scom_map_ok(map))
  141. return -ENXIO;
  142. for (reg = 0; reg < reg_cnt; reg++) {
  143. rc = get_user(val, ubuf64);
  144. if (!rc)
  145. rc = scom_write(map, reg, val);
  146. if (rc) {
  147. if (!done)
  148. done = rc;
  149. break;
  150. }
  151. ubuf64++;
  152. done += 8;
  153. }
  154. scom_unmap(map);
  155. return done;
  156. }
  157. static const struct file_operations scom_debug_fops = {
  158. .read = scom_debug_read,
  159. .write = scom_debug_write,
  160. .open = simple_open,
  161. .llseek = default_llseek,
  162. };
  163. static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
  164. int i)
  165. {
  166. struct scom_debug_entry *ent;
  167. struct dentry *dir;
  168. ent = kzalloc(sizeof(*ent), GFP_KERNEL);
  169. if (!ent)
  170. return -ENOMEM;
  171. ent->dn = of_node_get(dn);
  172. snprintf(ent->name, 16, "%08x", i);
  173. ent->path.data = (void*) dn->full_name;
  174. ent->path.size = strlen(dn->full_name);
  175. dir = debugfs_create_dir(ent->name, root);
  176. if (!dir) {
  177. of_node_put(dn);
  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 */