ebus.c 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361
  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/bpp.h>
  22. struct linux_ebus *ebus_chain = 0;
  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. { 0, 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. { 0, 0 }
  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(int node, struct linux_prom_registers *preg,
  74. struct linux_ebus_child *dev)
  75. {
  76. int regs[PROMREG_MAX];
  77. int irqs[PROMREG_MAX];
  78. char lbuf[128];
  79. int i, len;
  80. dev->prom_node = node;
  81. prom_getstring(node, "name", lbuf, sizeof(lbuf));
  82. strcpy(dev->prom_name, lbuf);
  83. len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
  84. if (len == -1) len = 0;
  85. dev->num_addrs = len / sizeof(regs[0]);
  86. for (i = 0; i < dev->num_addrs; i++) {
  87. if (regs[i] >= dev->parent->num_addrs) {
  88. prom_printf("UGH: property for %s was %d, need < %d\n",
  89. dev->prom_name, len, dev->parent->num_addrs);
  90. panic(__FUNCTION__);
  91. }
  92. dev->resource[i].start = dev->parent->resource[regs[i]].start; /* XXX resource */
  93. }
  94. for (i = 0; i < PROMINTR_MAX; i++)
  95. dev->irqs[i] = PCI_IRQ_NONE;
  96. if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) {
  97. dev->num_irqs = 1;
  98. } else if ((len = prom_getproperty(node, "interrupts",
  99. (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
  100. dev->num_irqs = 0;
  101. dev->irqs[0] = 0;
  102. if (dev->parent->num_irqs != 0) {
  103. dev->num_irqs = 1;
  104. dev->irqs[0] = dev->parent->irqs[0];
  105. /* P3 */ /* printk("EBUS: dev %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
  106. }
  107. } else {
  108. dev->num_irqs = len / sizeof(irqs[0]);
  109. if (irqs[0] == 0 || irqs[0] >= 8) {
  110. /*
  111. * XXX Zero is a valid pin number...
  112. * This works as long as Ebus is not wired to INTA#.
  113. */
  114. printk("EBUS: %s got bad irq %d from PROM\n",
  115. dev->prom_name, irqs[0]);
  116. dev->num_irqs = 0;
  117. dev->irqs[0] = 0;
  118. } else {
  119. dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name);
  120. }
  121. }
  122. }
  123. void __init fill_ebus_device(int node, struct linux_ebus_device *dev)
  124. {
  125. struct linux_prom_registers regs[PROMREG_MAX];
  126. struct linux_ebus_child *child;
  127. int irqs[PROMINTR_MAX];
  128. char lbuf[128];
  129. int i, n, len;
  130. unsigned long baseaddr;
  131. dev->prom_node = node;
  132. prom_getstring(node, "name", lbuf, sizeof(lbuf));
  133. strcpy(dev->prom_name, lbuf);
  134. len = prom_getproperty(node, "reg", (void *)regs, sizeof(regs));
  135. if (len % sizeof(struct linux_prom_registers)) {
  136. prom_printf("UGH: proplen for %s was %d, need multiple of %d\n",
  137. dev->prom_name, len,
  138. (int)sizeof(struct linux_prom_registers));
  139. panic(__FUNCTION__);
  140. }
  141. dev->num_addrs = len / sizeof(struct linux_prom_registers);
  142. for (i = 0; i < dev->num_addrs; i++) {
  143. /*
  144. * XXX Collect JE-1 PROM
  145. *
  146. * Example - JS-E with 3.11:
  147. * /ebus
  148. * regs
  149. * 0x00000000, 0x0, 0x00000000, 0x0, 0x00000000,
  150. * 0x82000010, 0x0, 0xf0000000, 0x0, 0x01000000,
  151. * 0x82000014, 0x0, 0x38800000, 0x0, 0x00800000,
  152. * ranges
  153. * 0x00, 0x00000000, 0x02000010, 0x0, 0x0, 0x01000000,
  154. * 0x01, 0x01000000, 0x02000014, 0x0, 0x0, 0x00800000,
  155. * /ebus/8042
  156. * regs
  157. * 0x00000001, 0x00300060, 0x00000008,
  158. * 0x00000001, 0x00300060, 0x00000008,
  159. */
  160. n = regs[i].which_io;
  161. if (n >= 4) {
  162. /* XXX This is copied from old JE-1 by Gleb. */
  163. n = (regs[i].which_io - 0x10) >> 2;
  164. } else {
  165. ;
  166. }
  167. /*
  168. * XXX Now as we have regions, why don't we make an on-demand allocation...
  169. */
  170. dev->resource[i].start = 0;
  171. if ((baseaddr = dev->bus->self->resource[n].start +
  172. regs[i].phys_addr) != 0) {
  173. /* dev->resource[i].name = dev->prom_name; */
  174. if ((baseaddr = (unsigned long) ioremap(baseaddr,
  175. regs[i].reg_size)) == 0) {
  176. panic("ebus: unable to remap dev %s",
  177. dev->prom_name);
  178. }
  179. }
  180. dev->resource[i].start = baseaddr; /* XXX Unaligned */
  181. }
  182. for (i = 0; i < PROMINTR_MAX; i++)
  183. dev->irqs[i] = PCI_IRQ_NONE;
  184. if ((dev->irqs[0] = ebus_blacklist_irq(dev->prom_name)) != 0) {
  185. dev->num_irqs = 1;
  186. } else if ((len = prom_getproperty(node, "interrupts",
  187. (char *)&irqs, sizeof(irqs))) == -1 || len == 0) {
  188. dev->num_irqs = 0;
  189. if ((dev->irqs[0] = dev->bus->self->irq) != 0) {
  190. dev->num_irqs = 1;
  191. /* P3 */ /* printk("EBUS: child %s irq %d from parent\n", dev->prom_name, dev->irqs[0]); */
  192. }
  193. } else {
  194. dev->num_irqs = 1; /* dev->num_irqs = len / sizeof(irqs[0]); */
  195. if (irqs[0] == 0 || irqs[0] >= 8) {
  196. /* See above for the parent. XXX */
  197. printk("EBUS: %s got bad irq %d from PROM\n",
  198. dev->prom_name, irqs[0]);
  199. dev->num_irqs = 0;
  200. dev->irqs[0] = 0;
  201. } else {
  202. dev->irqs[0] = pcic_pin_to_irq(irqs[0], dev->prom_name);
  203. }
  204. }
  205. if ((node = prom_getchild(node))) {
  206. dev->children = (struct linux_ebus_child *)
  207. ebus_alloc(sizeof(struct linux_ebus_child));
  208. child = dev->children;
  209. child->next = 0;
  210. child->parent = dev;
  211. child->bus = dev->bus;
  212. fill_ebus_child(node, &regs[0], child);
  213. while ((node = prom_getsibling(node)) != 0) {
  214. child->next = (struct linux_ebus_child *)
  215. ebus_alloc(sizeof(struct linux_ebus_child));
  216. child = child->next;
  217. child->next = 0;
  218. child->parent = dev;
  219. child->bus = dev->bus;
  220. fill_ebus_child(node, &regs[0], child);
  221. }
  222. }
  223. }
  224. void __init ebus_init(void)
  225. {
  226. struct linux_prom_pci_registers regs[PROMREG_MAX];
  227. struct linux_pbm_info *pbm;
  228. struct linux_ebus_device *dev;
  229. struct linux_ebus *ebus;
  230. struct ebus_system_entry *sp;
  231. struct pci_dev *pdev;
  232. struct pcidev_cookie *cookie;
  233. char lbuf[128];
  234. unsigned long addr, *base;
  235. unsigned short pci_command;
  236. int nd, len, ebusnd;
  237. int reg, nreg;
  238. int num_ebus = 0;
  239. prom_getstring(prom_root_node, "name", lbuf, sizeof(lbuf));
  240. for (sp = ebus_blacklist; sp->esname != NULL; sp++) {
  241. if (strcmp(lbuf, sp->esname) == 0) {
  242. ebus_blackp = sp->ipt;
  243. break;
  244. }
  245. }
  246. pdev = pci_get_device(PCI_VENDOR_ID_SUN, PCI_DEVICE_ID_SUN_EBUS, 0);
  247. if (!pdev) {
  248. return;
  249. }
  250. cookie = pdev->sysdata;
  251. ebusnd = cookie->prom_node;
  252. ebus_chain = ebus = (struct linux_ebus *)
  253. ebus_alloc(sizeof(struct linux_ebus));
  254. ebus->next = 0;
  255. while (ebusnd) {
  256. prom_getstring(ebusnd, "name", lbuf, sizeof(lbuf));
  257. ebus->prom_node = ebusnd;
  258. strcpy(ebus->prom_name, lbuf);
  259. ebus->self = pdev;
  260. ebus->parent = pbm = cookie->pbm;
  261. /* Enable BUS Master. */
  262. pci_read_config_word(pdev, PCI_COMMAND, &pci_command);
  263. pci_command |= PCI_COMMAND_MASTER;
  264. pci_write_config_word(pdev, PCI_COMMAND, pci_command);
  265. len = prom_getproperty(ebusnd, "reg", (void *)regs,
  266. sizeof(regs));
  267. if (len == 0 || len == -1) {
  268. prom_printf("%s: can't find reg property\n",
  269. __FUNCTION__);
  270. prom_halt();
  271. }
  272. nreg = len / sizeof(struct linux_prom_pci_registers);
  273. base = &ebus->self->resource[0].start;
  274. for (reg = 0; reg < nreg; reg++) {
  275. if (!(regs[reg].which_io & 0x03000000))
  276. continue;
  277. addr = regs[reg].phys_lo;
  278. *base++ = addr;
  279. }
  280. nd = prom_getchild(ebusnd);
  281. if (!nd)
  282. goto next_ebus;
  283. ebus->devices = (struct linux_ebus_device *)
  284. ebus_alloc(sizeof(struct linux_ebus_device));
  285. dev = ebus->devices;
  286. dev->next = 0;
  287. dev->children = 0;
  288. dev->bus = ebus;
  289. fill_ebus_device(nd, dev);
  290. while ((nd = prom_getsibling(nd)) != 0) {
  291. dev->next = (struct linux_ebus_device *)
  292. ebus_alloc(sizeof(struct linux_ebus_device));
  293. dev = dev->next;
  294. dev->next = 0;
  295. dev->children = 0;
  296. dev->bus = ebus;
  297. fill_ebus_device(nd, dev);
  298. }
  299. next_ebus:
  300. pdev = pci_get_device(PCI_VENDOR_ID_SUN,
  301. PCI_DEVICE_ID_SUN_EBUS, pdev);
  302. if (!pdev)
  303. break;
  304. cookie = pdev->sysdata;
  305. ebusnd = cookie->prom_node;
  306. ebus->next = (struct linux_ebus *)
  307. ebus_alloc(sizeof(struct linux_ebus));
  308. ebus = ebus->next;
  309. ebus->next = 0;
  310. ++num_ebus;
  311. }
  312. if (pdev)
  313. pci_dev_put(pdev);
  314. }