ebus.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392
  1. /* $Id: ebus.c,v 1.20 2002/01/05 01:13:43 davem Exp $
  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. 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. 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. void __init fill_ebus_device(struct device_node *dp, struct linux_ebus_device *dev)
  129. {
  130. const struct linux_prom_registers *regs;
  131. struct linux_ebus_child *child;
  132. struct dev_archdata *sd;
  133. const int *irqs;
  134. int i, n, len;
  135. unsigned long baseaddr;
  136. dev->prom_node = dp;
  137. regs = of_get_property(dp, "reg", &len);
  138. if (!regs)
  139. len = 0;
  140. if (len % sizeof(struct linux_prom_registers)) {
  141. prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
  142. dev->prom_node->name, len,
  143. (int)sizeof(struct linux_prom_registers));
  144. panic(__func__);
  145. }
  146. dev->num_addrs = len / sizeof(struct linux_prom_registers);
  147. for (i = 0; i < dev->num_addrs; i++) {
  148. /*
  149. * XXX Collect JE-1 PROM
  150. *
  151. * Example - JS-E with 3.11:
  152. * /ebus
  153. * regs
  154. * 0x00000000, 0x0, 0x00000000, 0x0, 0x00000000,
  155. * 0x82000010, 0x0, 0xf0000000, 0x0, 0x01000000,
  156. * 0x82000014, 0x0, 0x38800000, 0x0, 0x00800000,
  157. * ranges
  158. * 0x00, 0x00000000, 0x02000010, 0x0, 0x0, 0x01000000,
  159. * 0x01, 0x01000000, 0x02000014, 0x0, 0x0, 0x00800000,
  160. * /ebus/8042
  161. * regs
  162. * 0x00000001, 0x00300060, 0x00000008,
  163. * 0x00000001, 0x00300060, 0x00000008,
  164. */
  165. n = regs[i].which_io;
  166. if (n >= 4) {
  167. /* XXX This is copied from old JE-1 by Gleb. */
  168. n = (regs[i].which_io - 0x10) >> 2;
  169. } else {
  170. ;
  171. }
  172. /*
  173. * XXX Now as we have regions, why don't we make an on-demand allocation...
  174. */
  175. dev->resource[i].start = 0;
  176. if ((baseaddr = dev->bus->self->resource[n].start +
  177. regs[i].phys_addr) != 0) {
  178. /* dev->resource[i].name = dev->prom_name; */
  179. if ((baseaddr = (unsigned long) ioremap(baseaddr,
  180. regs[i].reg_size)) == 0) {
  181. panic("ebus: unable to remap dev %s",
  182. dev->prom_node->name);
  183. }
  184. }
  185. dev->resource[i].start = baseaddr; /* XXX Unaligned */
  186. }
  187. for (i = 0; i < PROMINTR_MAX; i++)
  188. dev->irqs[i] = PCI_IRQ_NONE;
  189. if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_node->name)) != 0) {
  190. dev->num_irqs = 1;
  191. } else {
  192. irqs = of_get_property(dp, "interrupts", &len);
  193. if (!irqs) {
  194. dev->num_irqs = 0;
  195. if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
  196. dev->num_irqs = 1;
  197. /* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
  198. }
  199. } else {
  200. dev->num_irqs = 1; /* dev->num_irqs = len / sizeof(irqs[0]); */
  201. if (irqs[0] == 0 || irqs[0] >= 8) {
  202. /* See above for the parent. XXX */
  203. printk("EBUS: %s got bad irq %d from PROM\n",
  204. dev->prom_node->name, irqs[0]);
  205. dev->num_irqs = 0;
  206. dev->irqs[0] = 0;
  207. } else {
  208. dev->irqs[0] =
  209. pcic_pin_to_irq(irqs[0],
  210. dev->prom_node->name);
  211. }
  212. }
  213. }
  214. sd = &dev->ofdev.dev.archdata;
  215. sd->prom_node = dp;
  216. sd->op = &dev->ofdev;
  217. sd->iommu = dev->bus->ofdev.dev.parent->archdata.iommu;
  218. dev->ofdev.node = dp;
  219. dev->ofdev.dev.parent = &dev->bus->ofdev.dev;
  220. dev->ofdev.dev.bus = &ebus_bus_type;
  221. sprintf(dev->ofdev.dev.bus_id, "ebus[%08x]", dp->node);
  222. /* Register with core */
  223. if (of_device_register(&dev->ofdev) != 0)
  224. printk(KERN_DEBUG "ebus: device registration error for %s!\n",
  225. dp->path_component_name);
  226. if ((dp = dp->child) != NULL) {
  227. dev->children = (struct linux_ebus_child *)
  228. ebus_alloc(sizeof(struct linux_ebus_child));
  229. child = dev->children;
  230. child->next = NULL;
  231. child->parent = dev;
  232. child->bus = dev->bus;
  233. fill_ebus_child(dp, child);
  234. while ((dp = dp->sibling) != NULL) {
  235. child->next = (struct linux_ebus_child *)
  236. ebus_alloc(sizeof(struct linux_ebus_child));
  237. child = child->next;
  238. child->next = NULL;
  239. child->parent = dev;
  240. child->bus = dev->bus;
  241. fill_ebus_child(dp, child);
  242. }
  243. }
  244. }
  245. void __init ebus_init(void)
  246. {
  247. const struct linux_prom_pci_registers *regs;
  248. struct linux_pbm_info *pbm;
  249. struct linux_ebus_device *dev;
  250. struct linux_ebus *ebus;
  251. struct ebus_system_entry *sp;
  252. struct pci_dev *pdev;
  253. struct pcidev_cookie *cookie;
  254. struct device_node *dp;
  255. struct resource *p;
  256. unsigned short pci_command;
  257. int len, reg, nreg;
  258. int num_ebus = 0;
  259. dp = of_find_node_by_path("/");
  260. for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
  261. if (strcmp(dp->name, sp->esname) == 0) {
  262. ebus_blackp = sp->ipt;
  263. break;
  264. }
  265. }
  266. pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, NULL);
  267. if (!pdev)
  268. return;
  269. cookie = pdev->sysdata;
  270. dp = cookie->prom_node;
  271. ebus_chain = ebus = (struct linux_ebus *)
  272. ebus_alloc(sizeof(struct linux_ebus));
  273. ebus->next = NULL;
  274. while (dp) {
  275. struct device_node *nd;
  276. ebus->prom_node = dp;
  277. ebus->self = pdev;
  278. ebus->parent = pbm = cookie->pbm;
  279. /* Enable BUS Master. */
  280. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  281. pci_command |= PCI_COMMAND_MASTER;
  282. pci_write_config_word(pdev, PCI_COMMAND, pci_command);
  283. regs = of_get_property(dp, "reg", &len);
  284. if (!regs) {
  285. prom_printf("%s: can't find reg property\n",
  286. __func__);
  287. prom_halt();
  288. }
  289. nreg = len / sizeof(struct linux_prom_pci_registers);
  290. p = &ebus->self->resource[0];
  291. for (reg = 0; reg < nreg; reg++) {
  292. if (!(regs[reg].which_io & 0x03000000))
  293. continue;
  294. (p++)->start = regs[reg].phys_lo;
  295. }
  296. ebus->ofdev.node = dp;
  297. ebus->ofdev.dev.parent = &pdev->dev;
  298. ebus->ofdev.dev.bus = &ebus_bus_type;
  299. sprintf(ebus->ofdev.dev.bus_id, "ebus%d", num_ebus);
  300. /* Register with core */
  301. if (of_device_register(&ebus->ofdev) != 0)
  302. printk(KERN_DEBUG "ebus: device registration error for %s!\n",
  303. dp->path_component_name);
  304. nd = dp->child;
  305. if (!nd)
  306. goto next_ebus;
  307. ebus->devices = (struct linux_ebus_device *)
  308. ebus_alloc(sizeof(struct linux_ebus_device));
  309. dev = ebus->devices;
  310. dev->next = NULL;
  311. dev->children = NULL;
  312. dev->bus = ebus;
  313. fill_ebus_device(nd, dev);
  314. while ((nd = nd->sibling) != NULL) {
  315. dev->next = (struct linux_ebus_device *)
  316. ebus_alloc(sizeof(struct linux_ebus_device));
  317. dev = dev->next;
  318. dev->next = NULL;
  319. dev->children = NULL;
  320. dev->bus = ebus;
  321. fill_ebus_device(nd, dev);
  322. }
  323. next_ebus:
  324. pdev = pci_get_device(PCI_VENDOR_ID_SUN,
  325. PCI_DEVICE_ID_SUN_EBUS, pdev);
  326. if (!pdev)
  327. break;
  328. cookie = pdev->sysdata;
  329. dp = cookie->prom_node;
  330. ebus->next = (struct linux_ebus *)
  331. ebus_alloc(sizeof(struct linux_ebus));
  332. ebus = ebus->next;
  333. ebus->next = NULL;
  334. ++num_ebus;
  335. }
  336. if (pdev)
  337. pci_dev_put(pdev);
  338. }