ebus.c 9.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389
  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(__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. 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 (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. sd = &dev->ofdev.dev.archdata;
  213. sd->prom_node = dp;
  214. sd->op = &dev->ofdev;
  215. dev->ofdev.node = dp;
  216. dev->ofdev.dev.parent = &dev->bus->ofdev.dev;
  217. dev->ofdev.dev.bus = &ebus_bus_type;
  218. sprintf(dev->ofdev.dev.bus_id, "ebus[%08x]", dp->node);
  219. /* Register with core */
  220. if (of_device_register(&dev->ofdev) != 0)
  221. printk(KERN_DEBUG "ebus: device registration error for %s!\n",
  222. dp->path_component_name);
  223. if ((dp = dp->child) != NULL) {
  224. dev->children = (struct linux_ebus_child *)
  225. ebus_alloc(sizeof(struct linux_ebus_child));
  226. child = dev->children;
  227. child->next = NULL;
  228. child->parent = dev;
  229. child->bus = dev->bus;
  230. fill_ebus_child(dp, child);
  231. while ((dp = dp->sibling) != NULL) {
  232. child->next = (struct linux_ebus_child *)
  233. ebus_alloc(sizeof(struct linux_ebus_child));
  234. child = child->next;
  235. child->next = NULL;
  236. child->parent = dev;
  237. child->bus = dev->bus;
  238. fill_ebus_child(dp, child);
  239. }
  240. }
  241. }
  242. void __init ebus_init(void)
  243. {
  244. const struct linux_prom_pci_registers *regs;
  245. struct linux_pbm_info *pbm;
  246. struct linux_ebus_device *dev;
  247. struct linux_ebus *ebus;
  248. struct ebus_system_entry *sp;
  249. struct pci_dev *pdev;
  250. struct pcidev_cookie *cookie;
  251. struct device_node *dp;
  252. struct resource *p;
  253. unsigned short pci_command;
  254. int len, reg, nreg;
  255. int num_ebus = 0;
  256. dp = of_find_node_by_path("/");
  257. for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
  258. if (strcmp(dp->name, sp->esname) == 0) {
  259. ebus_blackp = sp->ipt;
  260. break;
  261. }
  262. }
  263. pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, NULL);
  264. if (!pdev)
  265. return;
  266. cookie = pdev->sysdata;
  267. dp = cookie->prom_node;
  268. ebus_chain = ebus = (struct linux_ebus *)
  269. ebus_alloc(sizeof(struct linux_ebus));
  270. ebus->next = NULL;
  271. while (dp) {
  272. struct device_node *nd;
  273. ebus->prom_node = dp;
  274. ebus->self = pdev;
  275. ebus->parent = pbm = cookie->pbm;
  276. /* Enable BUS Master. */
  277. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  278. pci_command |= PCI_COMMAND_MASTER;
  279. pci_write_config_word(pdev, PCI_COMMAND, pci_command);
  280. regs = of_get_property(dp, "reg", &len);
  281. if (!regs) {
  282. prom_printf("%s: can't find reg property\n",
  283. __FUNCTION__);
  284. prom_halt();
  285. }
  286. nreg = len / sizeof(struct linux_prom_pci_registers);
  287. p = &ebus->self->resource[0];
  288. for (reg = 0; reg < nreg; reg++) {
  289. if (!(regs[reg].which_io & 0x03000000))
  290. continue;
  291. (p++)->start = regs[reg].phys_lo;
  292. }
  293. ebus->ofdev.node = dp;
  294. ebus->ofdev.dev.parent = &pdev->dev;
  295. ebus->ofdev.dev.bus = &ebus_bus_type;
  296. sprintf(ebus->ofdev.dev.bus_id, "ebus%d", num_ebus);
  297. /* Register with core */
  298. if (of_device_register(&ebus->ofdev) != 0)
  299. printk(KERN_DEBUG "ebus: device registration error for %s!\n",
  300. dp->path_component_name);
  301. nd = dp->child;
  302. if (!nd)
  303. goto next_ebus;
  304. ebus->devices = (struct linux_ebus_device *)
  305. ebus_alloc(sizeof(struct linux_ebus_device));
  306. dev = ebus->devices;
  307. dev->next = NULL;
  308. dev->children = NULL;
  309. dev->bus = ebus;
  310. fill_ebus_device(nd, dev);
  311. while ((nd = nd->sibling) != NULL) {
  312. dev->next = (struct linux_ebus_device *)
  313. ebus_alloc(sizeof(struct linux_ebus_device));
  314. dev = dev->next;
  315. dev->next = NULL;
  316. dev->children = NULL;
  317. dev->bus = ebus;
  318. fill_ebus_device(nd, dev);
  319. }
  320. next_ebus:
  321. pdev = pci_get_device(PCI_VENDOR_ID_SUN,
  322. PCI_DEVICE_ID_SUN_EBUS, pdev);
  323. if (!pdev)
  324. break;
  325. cookie = pdev->sysdata;
  326. dp = cookie->prom_node;
  327. ebus->next = (struct linux_ebus *)
  328. ebus_alloc(sizeof(struct linux_ebus));
  329. ebus = ebus->next;
  330. ebus->next = NULL;
  331. ++num_ebus;
  332. }
  333. if (pdev)
  334. pci_dev_put(pdev);
  335. }