sbus.c 7.6 KB

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