ebus.c 9.1 KB

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