scom.c 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  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. 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. unsigned long addr;
  89. scom_map_t map;
  90. spinlock_t lock;
  91. char name[8];
  92. struct debugfs_blob_wrapper blob;
  93. };
  94. static int scom_addr_set(void *data, u64 val)
  95. {
  96. struct scom_debug_entry *ent = data;
  97. ent->addr = 0;
  98. scom_unmap(ent->map);
  99. ent->map = scom_map(ent->dn, val, 1);
  100. if (scom_map_ok(ent->map))
  101. ent->addr = val;
  102. else
  103. return -EFAULT;
  104. return 0;
  105. }
  106. static int scom_addr_get(void *data, u64 *val)
  107. {
  108. struct scom_debug_entry *ent = data;
  109. *val = ent->addr;
  110. return 0;
  111. }
  112. DEFINE_SIMPLE_ATTRIBUTE(scom_addr_fops, scom_addr_get, scom_addr_set,
  113. "0x%llx\n");
  114. static int scom_val_set(void *data, u64 val)
  115. {
  116. struct scom_debug_entry *ent = data;
  117. if (!scom_map_ok(ent->map))
  118. return -EFAULT;
  119. scom_write(ent->map, 0, val);
  120. return 0;
  121. }
  122. static int scom_val_get(void *data, u64 *val)
  123. {
  124. struct scom_debug_entry *ent = data;
  125. if (!scom_map_ok(ent->map))
  126. return -EFAULT;
  127. return scom_read(ent->map, 0, val);
  128. }
  129. DEFINE_SIMPLE_ATTRIBUTE(scom_val_fops, scom_val_get, scom_val_set,
  130. "0x%llx\n");
  131. static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
  132. int i)
  133. {
  134. struct scom_debug_entry *ent;
  135. struct dentry *dir;
  136. ent = kzalloc(sizeof(*ent), GFP_KERNEL);
  137. if (!ent)
  138. return -ENOMEM;
  139. ent->dn = of_node_get(dn);
  140. ent->map = SCOM_MAP_INVALID;
  141. spin_lock_init(&ent->lock);
  142. snprintf(ent->name, 8, "scom%d", i);
  143. ent->blob.data = (void*) dn->full_name;
  144. ent->blob.size = strlen(dn->full_name);
  145. dir = debugfs_create_dir(ent->name, root);
  146. if (!dir) {
  147. of_node_put(dn);
  148. kfree(ent);
  149. return -1;
  150. }
  151. debugfs_create_file("addr", 0600, dir, ent, &scom_addr_fops);
  152. debugfs_create_file("value", 0600, dir, ent, &scom_val_fops);
  153. debugfs_create_blob("path", 0400, dir, &ent->blob);
  154. return 0;
  155. }
  156. static int scom_debug_init(void)
  157. {
  158. struct device_node *dn;
  159. struct dentry *root;
  160. int i, rc;
  161. root = debugfs_create_dir("scom", powerpc_debugfs_root);
  162. if (!root)
  163. return -1;
  164. i = rc = 0;
  165. for_each_node_with_property(dn, "scom-controller")
  166. rc |= scom_debug_init_one(root, dn, i++);
  167. return rc;
  168. }
  169. device_initcall(scom_debug_init);
  170. #endif /* CONFIG_SCOM_DEBUGFS */