scom.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 u32 *prop;
  52. u64 reg, cnt;
  53. scom_map_t ret;
  54. parent = scom_find_parent(dev);
  55. if (parent == NULL)
  56. return 0;
  57. prop = of_get_property(parent, "#scom-cells", NULL);
  58. cells = prop ? *prop : 1;
  59. prop = of_get_property(dev, "scom-reg", &size);
  60. if (!prop)
  61. return 0;
  62. size >>= 2;
  63. if (index >= (size / (2*cells)))
  64. return 0;
  65. reg = of_read_number(&prop[index * cells * 2], cells);
  66. cnt = of_read_number(&prop[index * cells * 2 + cells], cells);
  67. ret = scom_map(parent, reg, cnt);
  68. of_node_put(parent);
  69. return ret;
  70. }
  71. EXPORT_SYMBOL_GPL(scom_map_device);
  72. #ifdef CONFIG_SCOM_DEBUGFS
  73. struct scom_debug_entry {
  74. struct device_node *dn;
  75. unsigned long addr;
  76. scom_map_t map;
  77. spinlock_t lock;
  78. char name[8];
  79. struct debugfs_blob_wrapper blob;
  80. };
  81. static int scom_addr_set(void *data, u64 val)
  82. {
  83. struct scom_debug_entry *ent = data;
  84. ent->addr = 0;
  85. scom_unmap(ent->map);
  86. ent->map = scom_map(ent->dn, val, 1);
  87. if (scom_map_ok(ent->map))
  88. ent->addr = val;
  89. else
  90. return -EFAULT;
  91. return 0;
  92. }
  93. static int scom_addr_get(void *data, u64 *val)
  94. {
  95. struct scom_debug_entry *ent = data;
  96. *val = ent->addr;
  97. return 0;
  98. }
  99. DEFINE_SIMPLE_ATTRIBUTE(scom_addr_fops, scom_addr_get, scom_addr_set,
  100. "0x%llx\n");
  101. static int scom_val_set(void *data, u64 val)
  102. {
  103. struct scom_debug_entry *ent = data;
  104. if (!scom_map_ok(ent->map))
  105. return -EFAULT;
  106. scom_write(ent->map, 0, val);
  107. return 0;
  108. }
  109. static int scom_val_get(void *data, u64 *val)
  110. {
  111. struct scom_debug_entry *ent = data;
  112. if (!scom_map_ok(ent->map))
  113. return -EFAULT;
  114. *val = scom_read(ent->map, 0);
  115. return 0;
  116. }
  117. DEFINE_SIMPLE_ATTRIBUTE(scom_val_fops, scom_val_get, scom_val_set,
  118. "0x%llx\n");
  119. static int scom_debug_init_one(struct dentry *root, struct device_node *dn,
  120. int i)
  121. {
  122. struct scom_debug_entry *ent;
  123. struct dentry *dir;
  124. ent = kzalloc(sizeof(*ent), GFP_KERNEL);
  125. if (!ent)
  126. return -ENOMEM;
  127. ent->dn = of_node_get(dn);
  128. ent->map = SCOM_MAP_INVALID;
  129. spin_lock_init(&ent->lock);
  130. snprintf(ent->name, 8, "scom%d", i);
  131. ent->blob.data = dn->full_name;
  132. ent->blob.size = strlen(dn->full_name);
  133. dir = debugfs_create_dir(ent->name, root);
  134. if (!dir) {
  135. of_node_put(dn);
  136. kfree(ent);
  137. return -1;
  138. }
  139. debugfs_create_file("addr", 0600, dir, ent, &scom_addr_fops);
  140. debugfs_create_file("value", 0600, dir, ent, &scom_val_fops);
  141. debugfs_create_blob("path", 0400, dir, &ent->blob);
  142. return 0;
  143. }
  144. static int scom_debug_init(void)
  145. {
  146. struct device_node *dn;
  147. struct dentry *root;
  148. int i, rc;
  149. root = debugfs_create_dir("scom", powerpc_debugfs_root);
  150. if (!root)
  151. return -1;
  152. i = rc = 0;
  153. for_each_node_with_property(dn, "scom-controller")
  154. rc |= scom_debug_init_one(root, dn, i++);
  155. return rc;
  156. }
  157. device_initcall(scom_debug_init);
  158. #endif /* CONFIG_SCOM_DEBUGFS */