scom.c 4.3 KB

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