ebus.c 9.2 KB

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