scom.c 4.3 KB

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