sbus.c 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303
  1. /* sbus.c: SBus support routines.
  2. *
  3. * Copyright (C) 1995, 2006 David S. Miller (davem@davemloft.net)
  4. */
  5. #include <linux/kernel.h>
  6. #include <linux/slab.h>
  7. #include <linux/init.h>
  8. #include <linux/pci.h>
  9. #include <linux/device.h>
  10. #include <asm/system.h>
  11. #include <asm/sbus.h>
  12. #include <asm/dma.h>
  13. #include <asm/oplib.h>
  14. #include <asm/prom.h>
  15. #include <asm/of_device.h>
  16. #include <asm/bpp.h>
  17. #include <asm/irq.h>
  18. static ssize_t
  19. show_sbusobppath_attr(struct device * dev, struct device_attribute * attr, char * buf)
  20. {
  21. struct sbus_dev *sbus;
  22. sbus = to_sbus_device(dev);
  23. return snprintf (buf, PAGE_SIZE, "%s\n", sbus->ofdev.node->full_name);
  24. }
  25. static DEVICE_ATTR(obppath, S_IRUSR | S_IRGRP | S_IROTH, show_sbusobppath_attr, NULL);
  26. struct sbus_bus *sbus_root;
  27. static void __init fill_sbus_device(struct device_node *dp, struct sbus_dev *sdev)
  28. {
  29. unsigned long base;
  30. void *pval;
  31. int len, err;
  32. sdev->prom_node = dp->node;
  33. strcpy(sdev->prom_name, dp->name);
  34. pval = of_get_property(dp, "reg", &len);
  35. sdev->num_registers = 0;
  36. if (pval) {
  37. memcpy(sdev->reg_addrs, pval, len);
  38. sdev->num_registers =
  39. len / sizeof(struct linux_prom_registers);
  40. base = (unsigned long) sdev->reg_addrs[0].phys_addr;
  41. /* Compute the slot number. */
  42. if (base >= SUN_SBUS_BVADDR && sparc_cpu_model == sun4m)
  43. sdev->slot = sbus_dev_slot(base);
  44. else
  45. sdev->slot = sdev->reg_addrs[0].which_io;
  46. }
  47. pval = of_get_property(dp, "ranges", &len);
  48. sdev->num_device_ranges = 0;
  49. if (pval) {
  50. memcpy(sdev->device_ranges, pval, len);
  51. sdev->num_device_ranges =
  52. len / sizeof(struct linux_prom_ranges);
  53. }
  54. sbus_fill_device_irq(sdev);
  55. sdev->ofdev.node = dp;
  56. if (sdev->parent)
  57. sdev->ofdev.dev.parent = &sdev->parent->ofdev.dev;
  58. else
  59. sdev->ofdev.dev.parent = &sdev->bus->ofdev.dev;
  60. sdev->ofdev.dev.bus = &sbus_bus_type;
  61. sprintf(sdev->ofdev.dev.bus_id, "sbus[%08x]", dp->node);
  62. if (of_device_register(&sdev->ofdev) != 0)
  63. printk(KERN_DEBUG "sbus: device registration error for %s!\n",
  64. dp->path_component_name);
  65. /* WE HAVE BEEN INVADED BY ALIENS! */
  66. err = sysfs_create_file(&sdev->ofdev.dev.kobj, &dev_attr_obppath.attr);
  67. }
  68. static void __init sbus_bus_ranges_init(struct device_node *dp, struct sbus_bus *sbus)
  69. {
  70. void *pval;
  71. int len;
  72. pval = of_get_property(dp, "ranges", &len);
  73. sbus->num_sbus_ranges = 0;
  74. if (pval) {
  75. memcpy(sbus->sbus_ranges, pval, len);
  76. sbus->num_sbus_ranges =
  77. len / sizeof(struct linux_prom_ranges);
  78. sbus_arch_bus_ranges_init(dp->parent, sbus);
  79. }
  80. }
  81. static void __init __apply_ranges_to_regs(struct linux_prom_ranges *ranges,
  82. int num_ranges,
  83. struct linux_prom_registers *regs,
  84. int num_regs)
  85. {
  86. if (num_ranges) {
  87. int regnum;
  88. for (regnum = 0; regnum < num_regs; regnum++) {
  89. int rngnum;
  90. for (rngnum = 0; rngnum < num_ranges; rngnum++) {
  91. if (regs[regnum].which_io == ranges[rngnum].ot_child_space)
  92. break;
  93. }
  94. if (rngnum == num_ranges) {
  95. /* We used to flag this as an error. Actually
  96. * some devices do not report the regs as we expect.
  97. * For example, see SUNW,pln device. In that case
  98. * the reg property is in a format internal to that
  99. * node, ie. it is not in the SBUS register space
  100. * per se. -DaveM
  101. */
  102. return;
  103. }
  104. regs[regnum].which_io = ranges[rngnum].ot_parent_space;
  105. regs[regnum].phys_addr -= ranges[rngnum].ot_child_base;
  106. regs[regnum].phys_addr += ranges[rngnum].ot_parent_base;
  107. }
  108. }
  109. }
  110. static void __init __fixup_regs_sdev(struct sbus_dev *sdev)
  111. {
  112. if (sdev->num_registers != 0) {
  113. struct sbus_dev *parent = sdev->parent;
  114. int i;
  115. while (parent != NULL) {
  116. __apply_ranges_to_regs(parent->device_ranges,
  117. parent->num_device_ranges,
  118. sdev->reg_addrs,
  119. sdev->num_registers);
  120. parent = parent->parent;
  121. }
  122. __apply_ranges_to_regs(sdev->bus->sbus_ranges,
  123. sdev->bus->num_sbus_ranges,
  124. sdev->reg_addrs,
  125. sdev->num_registers);
  126. for (i = 0; i < sdev->num_registers; i++) {
  127. struct resource *res = &sdev->resource[i];
  128. res->start = sdev->reg_addrs[i].phys_addr;
  129. res->end = (res->start +
  130. (unsigned long)sdev->reg_addrs[i].reg_size - 1UL);
  131. res->flags = IORESOURCE_IO |
  132. (sdev->reg_addrs[i].which_io & 0xff);
  133. }
  134. }
  135. }
  136. static void __init sbus_fixup_all_regs(struct sbus_dev *first_sdev)
  137. {
  138. struct sbus_dev *sdev;
  139. for (sdev = first_sdev; sdev; sdev = sdev->next) {
  140. if (sdev->child)
  141. sbus_fixup_all_regs(sdev->child);
  142. __fixup_regs_sdev(sdev);
  143. }
  144. }
  145. /* We preserve the "probe order" of these bus and device lists to give
  146. * the same ordering as the old code.
  147. */
  148. static void __init sbus_insert(struct sbus_bus *sbus, struct sbus_bus **root)
  149. {
  150. while (*root)
  151. root = &(*root)->next;
  152. *root = sbus;
  153. sbus->next = NULL;
  154. }
  155. static void __init sdev_insert(struct sbus_dev *sdev, struct sbus_dev **root)
  156. {
  157. while (*root)
  158. root = &(*root)->next;
  159. *root = sdev;
  160. sdev->next = NULL;
  161. }
  162. static void __init walk_children(struct device_node *dp, struct sbus_dev *parent, struct sbus_bus *sbus)
  163. {
  164. dp = dp->child;
  165. while (dp) {
  166. struct sbus_dev *sdev;
  167. sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
  168. if (sdev) {
  169. sdev_insert(sdev, &parent->child);
  170. sdev->bus = sbus;
  171. sdev->parent = parent;
  172. fill_sbus_device(dp, sdev);
  173. walk_children(dp, sdev, sbus);
  174. }
  175. dp = dp->sibling;
  176. }
  177. }
  178. static void __init build_one_sbus(struct device_node *dp, int num_sbus)
  179. {
  180. struct sbus_bus *sbus;
  181. unsigned int sbus_clock;
  182. struct device_node *dev_dp;
  183. sbus = kzalloc(sizeof(struct sbus_bus), GFP_ATOMIC);
  184. if (!sbus)
  185. return;
  186. sbus_insert(sbus, &sbus_root);
  187. sbus->prom_node = dp->node;
  188. sbus_setup_iommu(sbus, dp);
  189. printk("sbus%d: ", num_sbus);
  190. sbus_clock = of_getintprop_default(dp, "clock-frequency",
  191. (25*1000*1000));
  192. sbus->clock_freq = sbus_clock;
  193. printk("Clock %d.%d MHz\n", (int) ((sbus_clock/1000)/1000),
  194. (int) (((sbus_clock/1000)%1000 != 0) ?
  195. (((sbus_clock/1000)%1000) + 1000) : 0));
  196. strcpy(sbus->prom_name, dp->name);
  197. sbus_setup_arch_props(sbus, dp);
  198. sbus_bus_ranges_init(dp, sbus);
  199. sbus->ofdev.node = dp;
  200. sbus->ofdev.dev.parent = NULL;
  201. sbus->ofdev.dev.bus = &sbus_bus_type;
  202. sprintf(sbus->ofdev.dev.bus_id, "sbus%d", num_sbus);
  203. if (of_device_register(&sbus->ofdev) != 0)
  204. printk(KERN_DEBUG "sbus: device registration error for %s!\n",
  205. sbus->ofdev.dev.bus_id);
  206. dev_dp = dp->child;
  207. while (dev_dp) {
  208. struct sbus_dev *sdev;
  209. sdev = kzalloc(sizeof(struct sbus_dev), GFP_ATOMIC);
  210. if (sdev) {
  211. sdev_insert(sdev, &sbus->devices);
  212. sdev->bus = sbus;
  213. sdev->parent = NULL;
  214. fill_sbus_device(dev_dp, sdev);
  215. walk_children(dev_dp, sdev, sbus);
  216. }
  217. dev_dp = dev_dp->sibling;
  218. }
  219. sbus_fixup_all_regs(sbus->devices);
  220. dvma_init(sbus);
  221. }
  222. static int __init sbus_init(void)
  223. {
  224. struct device_node *dp;
  225. const char *sbus_name = "sbus";
  226. int num_sbus = 0;
  227. if (sbus_arch_preinit())
  228. return 0;
  229. if (sparc_cpu_model == sun4d)
  230. sbus_name = "sbi";
  231. for_each_node_by_name(dp, sbus_name) {
  232. build_one_sbus(dp, num_sbus);
  233. num_sbus++;
  234. }
  235. sbus_arch_postinit();
  236. return 0;
  237. }
  238. subsys_initcall(sbus_init);