sbus.c 6.5 KB

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