sbus.c 6.5 KB

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