ebus.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393
  1. /*
  2. * ebus.c: PCI to EBus bridge device.
  3. *
  4. * Copyright (C) 1997 Eddie C. Dost (ecd@skynet.be)
  5. *
  6. * Adopted for sparc by V. Roganov and G. Raiko.
  7. * Fixes for different platforms by Pete Zaitcev.
  8. */
  9. #include <linux/kernel.h>
  10. #include <linux/types.h>
  11. #include <linux/init.h>
  12. #include <linux/slab.h>
  13. #include <linux/string.h>
  14. #include <asm/system.h>
  15. #include <asm/page.h>
  16. #include <asm/pbm.h>
  17. #include <asm/ebus.h>
  18. #include <asm/io.h>
  19. #include <asm/oplib.h>
  20. #include <asm/prom.h>
  21. #include <asm/bpp.h>
  22. struct linux_ebus *ebus_chain = NULL;
  23. /* We are together with pcic.c under CONFIG_PCI. */
  24. extern unsigned int pcic_pin_to_irq(unsigned int, const char *name);
  25. /*
  26. * IRQ Blacklist
  27. * Here we list PROMs and systems that are known to supply crap as IRQ numbers.
  28. */
  29. struct ebus_device_irq {
  30. char *name;
  31. unsigned int pin;
  32. };
  33. struct ebus_system_entry {
  34. char *esname;
  35. struct ebus_device_irq *ipt;
  36. };
  37. static struct ebus_device_irq je1_1[] = {
  38. { "8042", 3 },
  39. { "SUNW,CS4231", 0 },
  40. { "parallel", 0 },
  41. { "se", 2 },
  42. { NULL, 0 }
  43. };
  44. /*
  45. * Gleb's JE1 supplied reasonable pin numbers, but mine did not (OBP 2.32).
  46. * Blacklist the sucker... Note that Gleb's system will work.
  47. */
  48. static struct ebus_system_entry ebus_blacklist[] = {
  49. { "SUNW,JavaEngine1", je1_1 },
  50. { NULL, NULL }
  51. };
  52. static struct ebus_device_irq *ebus_blackp = NULL;
  53. /*
  54. */
  55. static inline unsigned long ebus_alloc(size_t size)
  56. {
  57. return (unsigned long)kmalloc(size, GFP_ATOMIC);
  58. }
  59. /*
  60. */
  61. static int __init ebus_blacklist_irq(const char *name)
  62. {
  63. struct ebus_device_irq *dp;
  64. if ((dp = ebus_blackp) != NULL) {
  65. for (; dp->name != NULL; dp++) {
  66. if (strcmp(name, dp->name) == 0) {
  67. return pcic_pin_to_irq(dp->pin, name);
  68. }
  69. }
  70. }
  71. return 0;
  72. }
  73. static void __init fill_ebus_child(struct device_node *dp,
  74. struct linux_ebus_child *dev)
  75. {
  76. const int *regs;
  77. const int *irqs;
  78. int i, len;
  79. dev->prom_node = dp;
  80. regs = of_get_property(dp, "reg", &len);
  81. if (!regs)
  82. len = 0;
  83. dev->num_addrs = len / sizeof(regs[0]);
  84. for (i = 0; i < dev->num_addrs; i++) {
  85. if (regs[i] >= dev->parent->num_addrs) {
  86. prom_printf("UGH: property for %s was %d, need < %d\n",
  87. dev->prom_node->name, len,
  88. dev->parent->num_addrs);
  89. panic(__func__);
  90. }
  91. /* XXX resource */
  92. dev->resource[i].start =
  93. dev->parent->resource[regs[i]].start;
  94. }
  95. for (i = 0; i < PROMINTR_MAX; i++)
  96. dev->irqs[i] = PCI_IRQ_NONE;
  97. if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
  98. dev->num_irqs = 1;
  99. } else {
  100. irqs = of_get_property(dp, "interrupts", &len);
  101. if (!irqs) {
  102. dev->num_irqs = 0;
  103. dev->irqs[0] = 0;
  104. if (dev->parent->num_irqs != 0) {
  105. dev->num_irqs = 1;
  106. dev->irqs[0] = dev->parent->irqs[0];
  107. }
  108. } else {
  109. dev->num_irqs = len / sizeof(irqs[0]);
  110. if (irqs[0] == 0 || irqs[0] >= 8) {
  111. /*
  112. * XXX Zero is a valid pin number...
  113. * This works as long as Ebus is not wired
  114. * to INTA#.
  115. */
  116. printk("EBUS: %s got bad irq %d from PROM\n",
  117. dev->prom_node->name, irqs[0]);
  118. dev->num_irqs = 0;
  119. dev->irqs[0] = 0;
  120. } else {
  121. dev->irqs[0] =
  122. pcic_pin_to_irq(irqs[0],
  123. dev->prom_node->name);
  124. }
  125. }
  126. }
  127. }
  128. static void __init fill_ebus_device(struct device_node *dp,
  129. struct linux_ebus_device *dev)
  130. {
  131. const struct linux_prom_registers *regs;
  132. struct linux_ebus_child *child;
  133. struct dev_archdata *sd;
  134. const int *irqs;
  135. int i, n, len;
  136. unsigned long baseaddr;
  137. dev->prom_node = dp;
  138. regs = of_get_property(dp, "reg", &len);
  139. if (!regs)
  140. len = 0;
  141. if (len % sizeof(struct linux_prom_registers)) {
  142. prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
  143. dev->prom_node->name, len,
  144. (int)sizeof(struct linux_prom_registers));
  145. panic(__func__);
  146. }
  147. dev->num_addrs = len / sizeof(struct linux_prom_registers);
  148. for (i = 0; i < dev->num_addrs; i++) {
  149. /*
  150. * XXX Collect JE-1 PROM
  151. *
  152. * Example - JS-E with 3.11:
  153. * /ebus
  154. * regs
  155. * 0x00000000, 0x0, 0x00000000, 0x0, 0x00000000,
  156. * 0x82000010, 0x0, 0xf0000000, 0x0, 0x01000000,
  157. * 0x82000014, 0x0, 0x38800000, 0x0, 0x00800000,
  158. * ranges
  159. * 0x00, 0x00000000, 0x02000010, 0x0, 0x0, 0x01000000,
  160. * 0x01, 0x01000000, 0x02000014, 0x0, 0x0, 0x00800000,
  161. * /ebus/8042
  162. * regs
  163. * 0x00000001, 0x00300060, 0x00000008,
  164. * 0x00000001, 0x00300060, 0x00000008,
  165. */
  166. n = regs[i].which_io;
  167. if (n >= 4) {
  168. /* XXX This is copied from old JE-1 by Gleb. */
  169. n = (regs[i].which_io - 0x10) >> 2;
  170. } else {
  171. ;
  172. }
  173. /*
  174. * XXX Now as we have regions, why don't we make an on-demand allocation...
  175. */
  176. dev->resource[i].start = 0;
  177. if ((baseaddr = dev->bus->self->resource[n].start +
  178. regs[i].phys_addr) != 0) {
  179. /* dev->resource[i].name = dev->prom_name; */
  180. if ((baseaddr = (unsigned long) ioremap(baseaddr,
  181. regs[i].reg_size)) == 0) {
  182. panic("ebus: unable to remap dev %s",
  183. dev->prom_node->name);
  184. }
  185. }
  186. dev->resource[i].start = baseaddr; /* XXX Unaligned */
  187. }
  188. for (i = 0; i < PROMINTR_MAX; i++)
  189. dev->irqs[i] = PCI_IRQ_NONE;
  190. if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
  191. dev->num_irqs = 1;
  192. } else {
  193. irqs = of_get_property(dp, "interrupts", &len);
  194. if (!irqs) {
  195. dev->num_irqs = 0;
  196. if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
  197. dev->num_irqs = 1;
  198. /* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
  199. }
  200. } else {
  201. dev->num_irqs = 1; /* dev->num_irqs = len / sizeof(irqs[0]); */
  202. if (irqs[0] == 0 || irqs[0] >= 8) {
  203. /* See above for the parent. XXX */
  204. printk("EBUS: %s got bad irq %d from PROM\n",
  205. dev->prom_node->name, irqs[0]);
  206. dev->num_irqs = 0;
  207. dev->irqs[0] = 0;
  208. } else {
  209. dev->irqs[0] =
  210. pcic_pin_to_irq(irqs[0],
  211. dev->prom_node->name);
  212. }
  213. }
  214. }
  215. sd = &dev->ofdev.dev.archdata;
  216. sd->prom_node = dp;
  217. sd->op = &dev->ofdev;
  218. sd->iommu = dev->bus->ofdev.dev.parent->archdata.iommu;
  219. dev->ofdev.node = dp;
  220. dev->ofdev.dev.parent = &dev->bus->ofdev.dev;
  221. dev->ofdev.dev.bus = &ebus_bus_type;
  222. sprintf(dev->ofdev.dev.bus_id, "ebus[%08x]", dp->node);
  223. /* Register with core */
  224. if (of_device_register(&dev->ofdev) != 0)
  225. printk(KERN_DEBUG "ebus: device registration error for %s!\n",
  226. dp->path_component_name);
  227. if ((dp = dp->child) != NULL) {
  228. dev->children = (struct linux_ebus_child *)
  229. ebus_alloc(sizeof(struct linux_ebus_child));
  230. child = dev->children;
  231. child->next = NULL;
  232. child->parent = dev;
  233. child->bus = dev->bus;
  234. fill_ebus_child(dp, child);
  235. while ((dp = dp->sibling) != NULL) {
  236. child->next = (struct linux_ebus_child *)
  237. ebus_alloc(sizeof(struct linux_ebus_child));
  238. child = child->next;
  239. child->next = NULL;
  240. child->parent = dev;
  241. child->bus = dev->bus;
  242. fill_ebus_child(dp, child);
  243. }
  244. }
  245. }
  246. void __init ebus_init(void)
  247. {
  248. const struct linux_prom_pci_registers *regs;
  249. struct linux_pbm_info *pbm;
  250. struct linux_ebus_device *dev;
  251. struct linux_ebus *ebus;
  252. struct ebus_system_entry *sp;
  253. struct pci_dev *pdev;
  254. struct pcidev_cookie *cookie;
  255. struct device_node *dp;
  256. struct resource *p;
  257. unsigned short pci_command;
  258. int len, reg, nreg;
  259. int num_ebus = 0;
  260. dp = of_find_node_by_path("/");
  261. for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
  262. if (strcmp(dp->name, sp->esname) == 0) {
  263. ebus_blackp = sp->ipt;
  264. break;
  265. }
  266. }
  267. pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, NULL);
  268. if (!pdev)
  269. return;
  270. cookie = pdev->sysdata;
  271. dp = cookie->prom_node;
  272. ebus_chain = ebus = (struct linux_ebus *)
  273. ebus_alloc(sizeof(struct linux_ebus));
  274. ebus->next = NULL;
  275. while (dp) {
  276. struct device_node *nd;
  277. ebus->prom_node = dp;
  278. ebus->self = pdev;
  279. ebus->parent = pbm = cookie->pbm;
  280. /* Enable BUS Master. */
  281. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  282. pci_command |= PCI_COMMAND_MASTER;
  283. pci_write_config_word(pdev, PCI_COMMAND, pci_command);
  284. regs = of_get_property(dp, "reg", &len);
  285. if (!regs) {
  286. prom_printf("%s: can't find reg property\n",
  287. __func__);
  288. prom_halt();
  289. }
  290. nreg = len / sizeof(struct linux_prom_pci_registers);
  291. p = &ebus->self->resource[0];
  292. for (reg = 0; reg < nreg; reg++) {
  293. if (!(regs[reg].which_io & 0x03000000))
  294. continue;
  295. (p++)->start = regs[reg].phys_lo;
  296. }
  297. ebus->ofdev.node = dp;
  298. ebus->ofdev.dev.parent = &pdev->dev;
  299. ebus->ofdev.dev.bus = &ebus_bus_type;
  300. sprintf(ebus->ofdev.dev.bus_id, "ebus%d", num_ebus);
  301. /* Register with core */
  302. if (of_device_register(&ebus->ofdev) != 0)
  303. printk(KERN_DEBUG "ebus: device registration error for %s!\n",
  304. dp->path_component_name);
  305. nd = dp->child;
  306. if (!nd)
  307. goto next_ebus;
  308. ebus->devices = (struct linux_ebus_device *)
  309. ebus_alloc(sizeof(struct linux_ebus_device));
  310. dev = ebus->devices;
  311. dev->next = NULL;
  312. dev->children = NULL;
  313. dev->bus = ebus;
  314. fill_ebus_device(nd, dev);
  315. while ((nd = nd->sibling) != NULL) {
  316. dev->next = (struct linux_ebus_device *)
  317. ebus_alloc(sizeof(struct linux_ebus_device));
  318. dev = dev->next;
  319. dev->next = NULL;
  320. dev->children = NULL;
  321. dev->bus = ebus;
  322. fill_ebus_device(nd, dev);
  323. }
  324. next_ebus:
  325. pdev = pci_get_device(PCI_VENDOR_ID_SUN,
  326. PCI_DEVICE_ID_SUN_EBUS, pdev);
  327. if (!pdev)
  328. break;
  329. cookie = pdev->sysdata;
  330. dp = cookie->prom_node;
  331. ebus->next = (struct linux_ebus *)
  332. ebus_alloc(sizeof(struct linux_ebus));
  333. ebus = ebus->next;
  334. ebus->next = NULL;
  335. ++num_ebus;
  336. }
  337. if (pdev)
  338. pci_dev_put(pdev);
  339. }