pcic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854
  1. /*
  2. * pcic.c: MicroSPARC-IIep PCI controller support
  3. *
  4. * Copyright (C) 1998 V. Roganov and G. Raiko
  5. *
  6. * Code is derived from Ultra/PCI PSYCHO controller support, see that
  7. * for author info.
  8. *
  9. * Support for diverse IIep based platforms by Pete Zaitcev.
  10. * CP-1200 by Eric Brower.
  11. */
  12. #include <linux/kernel.h>
  13. #include <linux/types.h>
  14. #include <linux/init.h>
  15. #include <linux/mm.h>
  16. #include <linux/slab.h>
  17. #include <linux/jiffies.h>
  18. #include <asm/swift.h> /* for cache flushing. */
  19. #include <asm/io.h>
  20. #include <linux/ctype.h>
  21. #include <linux/pci.h>
  22. #include <linux/time.h>
  23. #include <linux/timex.h>
  24. #include <linux/interrupt.h>
  25. #include <linux/export.h>
  26. #include <asm/irq.h>
  27. #include <asm/oplib.h>
  28. #include <asm/prom.h>
  29. #include <asm/pcic.h>
  30. #include <asm/timex.h>
  31. #include <asm/timer.h>
  32. #include <asm/uaccess.h>
  33. #include <asm/irq_regs.h>
  34. #include "kernel.h"
  35. #include "irq.h"
  36. /*
  37. * I studied different documents and many live PROMs both from 2.30
  38. * family and 3.xx versions. I came to the amazing conclusion: there is
  39. * absolutely no way to route interrupts in IIep systems relying on
  40. * information which PROM presents. We must hardcode interrupt routing
  41. * schematics. And this actually sucks. -- zaitcev 1999/05/12
  42. *
  43. * To find irq for a device we determine which routing map
  44. * is in effect or, in other words, on which machine we are running.
  45. * We use PROM name for this although other techniques may be used
  46. * in special cases (Gleb reports a PROMless IIep based system).
  47. * Once we know the map we take device configuration address and
  48. * find PCIC pin number where INT line goes. Then we may either program
  49. * preferred irq into the PCIC or supply the preexisting irq to the device.
  50. */
  51. struct pcic_ca2irq {
  52. unsigned char busno; /* PCI bus number */
  53. unsigned char devfn; /* Configuration address */
  54. unsigned char pin; /* PCIC external interrupt pin */
  55. unsigned char irq; /* Preferred IRQ (mappable in PCIC) */
  56. unsigned int force; /* Enforce preferred IRQ */
  57. };
  58. struct pcic_sn2list {
  59. char *sysname;
  60. struct pcic_ca2irq *intmap;
  61. int mapdim;
  62. };
  63. /*
  64. * JavaEngine-1 apparently has different versions.
  65. *
  66. * According to communications with Sun folks, for P2 build 501-4628-03:
  67. * pin 0 - parallel, audio;
  68. * pin 1 - Ethernet;
  69. * pin 2 - su;
  70. * pin 3 - PS/2 kbd and mouse.
  71. *
  72. * OEM manual (805-1486):
  73. * pin 0: Ethernet
  74. * pin 1: All EBus
  75. * pin 2: IGA (unused)
  76. * pin 3: Not connected
  77. * OEM manual says that 501-4628 & 501-4811 are the same thing,
  78. * only the latter has NAND flash in place.
  79. *
  80. * So far unofficial Sun wins over the OEM manual. Poor OEMs...
  81. */
  82. static struct pcic_ca2irq pcic_i_je1a[] = { /* 501-4811-03 */
  83. { 0, 0x00, 2, 12, 0 }, /* EBus: hogs all */
  84. { 0, 0x01, 1, 6, 1 }, /* Happy Meal */
  85. { 0, 0x80, 0, 7, 0 }, /* IGA (unused) */
  86. };
  87. /* XXX JS-E entry is incomplete - PCI Slot 2 address (pin 7)? */
  88. static struct pcic_ca2irq pcic_i_jse[] = {
  89. { 0, 0x00, 0, 13, 0 }, /* Ebus - serial and keyboard */
  90. { 0, 0x01, 1, 6, 0 }, /* hme */
  91. { 0, 0x08, 2, 9, 0 }, /* VGA - we hope not used :) */
  92. { 0, 0x10, 6, 8, 0 }, /* PCI INTA# in Slot 1 */
  93. { 0, 0x18, 7, 12, 0 }, /* PCI INTA# in Slot 2, shared w. RTC */
  94. { 0, 0x38, 4, 9, 0 }, /* All ISA devices. Read 8259. */
  95. { 0, 0x80, 5, 11, 0 }, /* EIDE */
  96. /* {0,0x88, 0,0,0} - unknown device... PMU? Probably no interrupt. */
  97. { 0, 0xA0, 4, 9, 0 }, /* USB */
  98. /*
  99. * Some pins belong to non-PCI devices, we hardcode them in drivers.
  100. * sun4m timers - irq 10, 14
  101. * PC style RTC - pin 7, irq 4 ?
  102. * Smart card, Parallel - pin 4 shared with USB, ISA
  103. * audio - pin 3, irq 5 ?
  104. */
  105. };
  106. /* SPARCengine-6 was the original release name of CP1200.
  107. * The documentation differs between the two versions
  108. */
  109. static struct pcic_ca2irq pcic_i_se6[] = {
  110. { 0, 0x08, 0, 2, 0 }, /* SCSI */
  111. { 0, 0x01, 1, 6, 0 }, /* HME */
  112. { 0, 0x00, 3, 13, 0 }, /* EBus */
  113. };
  114. /*
  115. * Krups (courtesy of Varol Kaptan)
  116. * No documentation available, but it was easy to guess
  117. * because it was very similar to Espresso.
  118. *
  119. * pin 0 - kbd, mouse, serial;
  120. * pin 1 - Ethernet;
  121. * pin 2 - igs (we do not use it);
  122. * pin 3 - audio;
  123. * pin 4,5,6 - unused;
  124. * pin 7 - RTC (from P2 onwards as David B. says).
  125. */
  126. static struct pcic_ca2irq pcic_i_jk[] = {
  127. { 0, 0x00, 0, 13, 0 }, /* Ebus - serial and keyboard */
  128. { 0, 0x01, 1, 6, 0 }, /* hme */
  129. };
  130. /*
  131. * Several entries in this list may point to the same routing map
  132. * as several PROMs may be installed on the same physical board.
  133. */
  134. #define SN2L_INIT(name, map) \
  135. { name, map, ARRAY_SIZE(map) }
  136. static struct pcic_sn2list pcic_known_sysnames[] = {
  137. SN2L_INIT("SUNW,JavaEngine1", pcic_i_je1a), /* JE1, PROM 2.32 */
  138. SN2L_INIT("SUNW,JS-E", pcic_i_jse), /* PROLL JavaStation-E */
  139. SN2L_INIT("SUNW,SPARCengine-6", pcic_i_se6), /* SPARCengine-6/CP-1200 */
  140. SN2L_INIT("SUNW,JS-NC", pcic_i_jk), /* PROLL JavaStation-NC */
  141. SN2L_INIT("SUNW,JSIIep", pcic_i_jk), /* OBP JavaStation-NC */
  142. { NULL, NULL, 0 }
  143. };
  144. /*
  145. * Only one PCIC per IIep,
  146. * and since we have no SMP IIep, only one per system.
  147. */
  148. static int pcic0_up;
  149. static struct linux_pcic pcic0;
  150. void __iomem *pcic_regs;
  151. static volatile int pcic_speculative;
  152. static volatile int pcic_trapped;
  153. /* forward */
  154. unsigned int pcic_build_device_irq(struct platform_device *op,
  155. unsigned int real_irq);
  156. #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (((unsigned int)bus) << 16) | (((unsigned int)device_fn) << 8) | (where & ~3))
  157. static int pcic_read_config_dword(unsigned int busno, unsigned int devfn,
  158. int where, u32 *value)
  159. {
  160. struct linux_pcic *pcic;
  161. unsigned long flags;
  162. pcic = &pcic0;
  163. local_irq_save(flags);
  164. #if 0 /* does not fail here */
  165. pcic_speculative = 1;
  166. pcic_trapped = 0;
  167. #endif
  168. writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
  169. #if 0 /* does not fail here */
  170. nop();
  171. if (pcic_trapped) {
  172. local_irq_restore(flags);
  173. *value = ~0;
  174. return 0;
  175. }
  176. #endif
  177. pcic_speculative = 2;
  178. pcic_trapped = 0;
  179. *value = readl(pcic->pcic_config_space_data + (where&4));
  180. nop();
  181. if (pcic_trapped) {
  182. pcic_speculative = 0;
  183. local_irq_restore(flags);
  184. *value = ~0;
  185. return 0;
  186. }
  187. pcic_speculative = 0;
  188. local_irq_restore(flags);
  189. return 0;
  190. }
  191. static int pcic_read_config(struct pci_bus *bus, unsigned int devfn,
  192. int where, int size, u32 *val)
  193. {
  194. unsigned int v;
  195. if (bus->number != 0) return -EINVAL;
  196. switch (size) {
  197. case 1:
  198. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  199. *val = 0xff & (v >> (8*(where & 3)));
  200. return 0;
  201. case 2:
  202. if (where&1) return -EINVAL;
  203. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  204. *val = 0xffff & (v >> (8*(where & 3)));
  205. return 0;
  206. case 4:
  207. if (where&3) return -EINVAL;
  208. pcic_read_config_dword(bus->number, devfn, where&~3, val);
  209. return 0;
  210. }
  211. return -EINVAL;
  212. }
  213. static int pcic_write_config_dword(unsigned int busno, unsigned int devfn,
  214. int where, u32 value)
  215. {
  216. struct linux_pcic *pcic;
  217. unsigned long flags;
  218. pcic = &pcic0;
  219. local_irq_save(flags);
  220. writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
  221. writel(value, pcic->pcic_config_space_data + (where&4));
  222. local_irq_restore(flags);
  223. return 0;
  224. }
  225. static int pcic_write_config(struct pci_bus *bus, unsigned int devfn,
  226. int where, int size, u32 val)
  227. {
  228. unsigned int v;
  229. if (bus->number != 0) return -EINVAL;
  230. switch (size) {
  231. case 1:
  232. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  233. v = (v & ~(0xff << (8*(where&3)))) |
  234. ((0xff&val) << (8*(where&3)));
  235. return pcic_write_config_dword(bus->number, devfn, where&~3, v);
  236. case 2:
  237. if (where&1) return -EINVAL;
  238. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  239. v = (v & ~(0xffff << (8*(where&3)))) |
  240. ((0xffff&val) << (8*(where&3)));
  241. return pcic_write_config_dword(bus->number, devfn, where&~3, v);
  242. case 4:
  243. if (where&3) return -EINVAL;
  244. return pcic_write_config_dword(bus->number, devfn, where, val);
  245. }
  246. return -EINVAL;
  247. }
  248. static struct pci_ops pcic_ops = {
  249. .read = pcic_read_config,
  250. .write = pcic_write_config,
  251. };
  252. /*
  253. * On sparc64 pcibios_init() calls pci_controller_probe().
  254. * We want PCIC probed little ahead so that interrupt controller
  255. * would be operational.
  256. */
  257. int __init pcic_probe(void)
  258. {
  259. struct linux_pcic *pcic;
  260. struct linux_prom_registers regs[PROMREG_MAX];
  261. struct linux_pbm_info* pbm;
  262. char namebuf[64];
  263. phandle node;
  264. int err;
  265. if (pcic0_up) {
  266. prom_printf("PCIC: called twice!\n");
  267. prom_halt();
  268. }
  269. pcic = &pcic0;
  270. node = prom_getchild (prom_root_node);
  271. node = prom_searchsiblings (node, "pci");
  272. if (node == 0)
  273. return -ENODEV;
  274. /*
  275. * Map in PCIC register set, config space, and IO base
  276. */
  277. err = prom_getproperty(node, "reg", (char*)regs, sizeof(regs));
  278. if (err == 0 || err == -1) {
  279. prom_printf("PCIC: Error, cannot get PCIC registers "
  280. "from PROM.\n");
  281. prom_halt();
  282. }
  283. pcic0_up = 1;
  284. pcic->pcic_res_regs.name = "pcic_registers";
  285. pcic->pcic_regs = ioremap(regs[0].phys_addr, regs[0].reg_size);
  286. if (!pcic->pcic_regs) {
  287. prom_printf("PCIC: Error, cannot map PCIC registers.\n");
  288. prom_halt();
  289. }
  290. pcic->pcic_res_io.name = "pcic_io";
  291. if ((pcic->pcic_io = (unsigned long)
  292. ioremap(regs[1].phys_addr, 0x10000)) == 0) {
  293. prom_printf("PCIC: Error, cannot map PCIC IO Base.\n");
  294. prom_halt();
  295. }
  296. pcic->pcic_res_cfg_addr.name = "pcic_cfg_addr";
  297. if ((pcic->pcic_config_space_addr =
  298. ioremap(regs[2].phys_addr, regs[2].reg_size * 2)) == NULL) {
  299. prom_printf("PCIC: Error, cannot map "
  300. "PCI Configuration Space Address.\n");
  301. prom_halt();
  302. }
  303. /*
  304. * Docs say three least significant bits in address and data
  305. * must be the same. Thus, we need adjust size of data.
  306. */
  307. pcic->pcic_res_cfg_data.name = "pcic_cfg_data";
  308. if ((pcic->pcic_config_space_data =
  309. ioremap(regs[3].phys_addr, regs[3].reg_size * 2)) == NULL) {
  310. prom_printf("PCIC: Error, cannot map "
  311. "PCI Configuration Space Data.\n");
  312. prom_halt();
  313. }
  314. pbm = &pcic->pbm;
  315. pbm->prom_node = node;
  316. prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
  317. strcpy(pbm->prom_name, namebuf);
  318. {
  319. extern int pcic_nmi_trap_patch[4];
  320. t_nmi[0] = pcic_nmi_trap_patch[0];
  321. t_nmi[1] = pcic_nmi_trap_patch[1];
  322. t_nmi[2] = pcic_nmi_trap_patch[2];
  323. t_nmi[3] = pcic_nmi_trap_patch[3];
  324. swift_flush_dcache();
  325. pcic_regs = pcic->pcic_regs;
  326. }
  327. prom_getstring(prom_root_node, "name", namebuf, 63); namebuf[63] = 0;
  328. {
  329. struct pcic_sn2list *p;
  330. for (p = pcic_known_sysnames; p->sysname != NULL; p++) {
  331. if (strcmp(namebuf, p->sysname) == 0)
  332. break;
  333. }
  334. pcic->pcic_imap = p->intmap;
  335. pcic->pcic_imdim = p->mapdim;
  336. }
  337. if (pcic->pcic_imap == NULL) {
  338. /*
  339. * We do not panic here for the sake of embedded systems.
  340. */
  341. printk("PCIC: System %s is unknown, cannot route interrupts\n",
  342. namebuf);
  343. }
  344. return 0;
  345. }
  346. static void __init pcic_pbm_scan_bus(struct linux_pcic *pcic)
  347. {
  348. struct linux_pbm_info *pbm = &pcic->pbm;
  349. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, &pcic_ops, pbm);
  350. #if 0 /* deadwood transplanted from sparc64 */
  351. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
  352. pci_record_assignments(pbm, pbm->pci_bus);
  353. pci_assign_unassigned(pbm, pbm->pci_bus);
  354. pci_fixup_irq(pbm, pbm->pci_bus);
  355. #endif
  356. }
  357. /*
  358. * Main entry point from the PCI subsystem.
  359. */
  360. static int __init pcic_init(void)
  361. {
  362. struct linux_pcic *pcic;
  363. /*
  364. * PCIC should be initialized at start of the timer.
  365. * So, here we report the presence of PCIC and do some magic passes.
  366. */
  367. if(!pcic0_up)
  368. return 0;
  369. pcic = &pcic0;
  370. /*
  371. * Switch off IOTLB translation.
  372. */
  373. writeb(PCI_DVMA_CONTROL_IOTLB_DISABLE,
  374. pcic->pcic_regs+PCI_DVMA_CONTROL);
  375. /*
  376. * Increase mapped size for PCI memory space (DMA access).
  377. * Should be done in that order (size first, address second).
  378. * Why we couldn't set up 4GB and forget about it? XXX
  379. */
  380. writel(0xF0000000UL, pcic->pcic_regs+PCI_SIZE_0);
  381. writel(0+PCI_BASE_ADDRESS_SPACE_MEMORY,
  382. pcic->pcic_regs+PCI_BASE_ADDRESS_0);
  383. pcic_pbm_scan_bus(pcic);
  384. return 0;
  385. }
  386. int pcic_present(void)
  387. {
  388. return pcic0_up;
  389. }
  390. static int pdev_to_pnode(struct linux_pbm_info *pbm, struct pci_dev *pdev)
  391. {
  392. struct linux_prom_pci_registers regs[PROMREG_MAX];
  393. int err;
  394. phandle node = prom_getchild(pbm->prom_node);
  395. while(node) {
  396. err = prom_getproperty(node, "reg",
  397. (char *)&regs[0], sizeof(regs));
  398. if(err != 0 && err != -1) {
  399. unsigned long devfn = (regs[0].which_io >> 8) & 0xff;
  400. if(devfn == pdev->devfn)
  401. return node;
  402. }
  403. node = prom_getsibling(node);
  404. }
  405. return 0;
  406. }
  407. static inline struct pcidev_cookie *pci_devcookie_alloc(void)
  408. {
  409. return kmalloc(sizeof(struct pcidev_cookie), GFP_ATOMIC);
  410. }
  411. static void pcic_map_pci_device(struct linux_pcic *pcic,
  412. struct pci_dev *dev, int node)
  413. {
  414. char namebuf[64];
  415. unsigned long address;
  416. unsigned long flags;
  417. int j;
  418. if (node == 0 || node == -1) {
  419. strcpy(namebuf, "???");
  420. } else {
  421. prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
  422. }
  423. for (j = 0; j < 6; j++) {
  424. address = dev->resource[j].start;
  425. if (address == 0) break; /* are sequential */
  426. flags = dev->resource[j].flags;
  427. if ((flags & IORESOURCE_IO) != 0) {
  428. if (address < 0x10000) {
  429. /*
  430. * A device responds to I/O cycles on PCI.
  431. * We generate these cycles with memory
  432. * access into the fixed map (phys 0x30000000).
  433. *
  434. * Since a device driver does not want to
  435. * do ioremap() before accessing PC-style I/O,
  436. * we supply virtual, ready to access address.
  437. *
  438. * Note that request_region()
  439. * works for these devices.
  440. *
  441. * XXX Neat trick, but it's a *bad* idea
  442. * to shit into regions like that.
  443. * What if we want to allocate one more
  444. * PCI base address...
  445. */
  446. dev->resource[j].start =
  447. pcic->pcic_io + address;
  448. dev->resource[j].end = 1; /* XXX */
  449. dev->resource[j].flags =
  450. (flags & ~IORESOURCE_IO) | IORESOURCE_MEM;
  451. } else {
  452. /*
  453. * OOPS... PCI Spec allows this. Sun does
  454. * not have any devices getting above 64K
  455. * so it must be user with a weird I/O
  456. * board in a PCI slot. We must remap it
  457. * under 64K but it is not done yet. XXX
  458. */
  459. printk("PCIC: Skipping I/O space at 0x%lx, "
  460. "this will Oops if a driver attaches "
  461. "device '%s' at %02x:%02x)\n", address,
  462. namebuf, dev->bus->number, dev->devfn);
  463. }
  464. }
  465. }
  466. }
  467. static void
  468. pcic_fill_irq(struct linux_pcic *pcic, struct pci_dev *dev, int node)
  469. {
  470. struct pcic_ca2irq *p;
  471. unsigned int real_irq;
  472. int i, ivec;
  473. char namebuf[64];
  474. if (node == 0 || node == -1) {
  475. strcpy(namebuf, "???");
  476. } else {
  477. prom_getstring(node, "name", namebuf, sizeof(namebuf));
  478. }
  479. if ((p = pcic->pcic_imap) == NULL) {
  480. dev->irq = 0;
  481. return;
  482. }
  483. for (i = 0; i < pcic->pcic_imdim; i++) {
  484. if (p->busno == dev->bus->number && p->devfn == dev->devfn)
  485. break;
  486. p++;
  487. }
  488. if (i >= pcic->pcic_imdim) {
  489. printk("PCIC: device %s devfn %02x:%02x not found in %d\n",
  490. namebuf, dev->bus->number, dev->devfn, pcic->pcic_imdim);
  491. dev->irq = 0;
  492. return;
  493. }
  494. i = p->pin;
  495. if (i >= 0 && i < 4) {
  496. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  497. real_irq = ivec >> (i << 2) & 0xF;
  498. } else if (i >= 4 && i < 8) {
  499. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  500. real_irq = ivec >> ((i-4) << 2) & 0xF;
  501. } else { /* Corrupted map */
  502. printk("PCIC: BAD PIN %d\n", i); for (;;) {}
  503. }
  504. /* P3 */ /* printk("PCIC: device %s pin %d ivec 0x%x irq %x\n", namebuf, i, ivec, dev->irq); */
  505. /* real_irq means PROM did not bother to program the upper
  506. * half of PCIC. This happens on JS-E with PROM 3.11, for instance.
  507. */
  508. if (real_irq == 0 || p->force) {
  509. if (p->irq == 0 || p->irq >= 15) { /* Corrupted map */
  510. printk("PCIC: BAD IRQ %d\n", p->irq); for (;;) {}
  511. }
  512. printk("PCIC: setting irq %d at pin %d for device %02x:%02x\n",
  513. p->irq, p->pin, dev->bus->number, dev->devfn);
  514. real_irq = p->irq;
  515. i = p->pin;
  516. if (i >= 4) {
  517. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  518. ivec &= ~(0xF << ((i - 4) << 2));
  519. ivec |= p->irq << ((i - 4) << 2);
  520. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_HI);
  521. } else {
  522. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  523. ivec &= ~(0xF << (i << 2));
  524. ivec |= p->irq << (i << 2);
  525. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_LO);
  526. }
  527. }
  528. dev->irq = pcic_build_device_irq(NULL, real_irq);
  529. }
  530. /*
  531. * Normally called from {do_}pci_scan_bus...
  532. */
  533. void pcibios_fixup_bus(struct pci_bus *bus)
  534. {
  535. struct pci_dev *dev;
  536. int i, has_io, has_mem;
  537. unsigned int cmd;
  538. struct linux_pcic *pcic;
  539. /* struct linux_pbm_info* pbm = &pcic->pbm; */
  540. int node;
  541. struct pcidev_cookie *pcp;
  542. if (!pcic0_up) {
  543. printk("pcibios_fixup_bus: no PCIC\n");
  544. return;
  545. }
  546. pcic = &pcic0;
  547. /*
  548. * Next crud is an equivalent of pbm = pcic_bus_to_pbm(bus);
  549. */
  550. if (bus->number != 0) {
  551. printk("pcibios_fixup_bus: nonzero bus 0x%x\n", bus->number);
  552. return;
  553. }
  554. list_for_each_entry(dev, &bus->devices, bus_list) {
  555. /*
  556. * Comment from i386 branch:
  557. * There are buggy BIOSes that forget to enable I/O and memory
  558. * access to PCI devices. We try to fix this, but we need to
  559. * be sure that the BIOS didn't forget to assign an address
  560. * to the device. [mj]
  561. * OBP is a case of such BIOS :-)
  562. */
  563. has_io = has_mem = 0;
  564. for(i=0; i<6; i++) {
  565. unsigned long f = dev->resource[i].flags;
  566. if (f & IORESOURCE_IO) {
  567. has_io = 1;
  568. } else if (f & IORESOURCE_MEM)
  569. has_mem = 1;
  570. }
  571. pcic_read_config(dev->bus, dev->devfn, PCI_COMMAND, 2, &cmd);
  572. if (has_io && !(cmd & PCI_COMMAND_IO)) {
  573. printk("PCIC: Enabling I/O for device %02x:%02x\n",
  574. dev->bus->number, dev->devfn);
  575. cmd |= PCI_COMMAND_IO;
  576. pcic_write_config(dev->bus, dev->devfn,
  577. PCI_COMMAND, 2, cmd);
  578. }
  579. if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
  580. printk("PCIC: Enabling memory for device %02x:%02x\n",
  581. dev->bus->number, dev->devfn);
  582. cmd |= PCI_COMMAND_MEMORY;
  583. pcic_write_config(dev->bus, dev->devfn,
  584. PCI_COMMAND, 2, cmd);
  585. }
  586. node = pdev_to_pnode(&pcic->pbm, dev);
  587. if(node == 0)
  588. node = -1;
  589. /* cookies */
  590. pcp = pci_devcookie_alloc();
  591. pcp->pbm = &pcic->pbm;
  592. pcp->prom_node = of_find_node_by_phandle(node);
  593. dev->sysdata = pcp;
  594. /* fixing I/O to look like memory */
  595. if ((dev->class>>16) != PCI_BASE_CLASS_BRIDGE)
  596. pcic_map_pci_device(pcic, dev, node);
  597. pcic_fill_irq(pcic, dev, node);
  598. }
  599. }
  600. /* Makes compiler happy */
  601. static volatile int pcic_timer_dummy;
  602. static void pcic_clear_clock_irq(void)
  603. {
  604. pcic_timer_dummy = readl(pcic0.pcic_regs+PCI_SYS_LIMIT);
  605. }
  606. /* CPU frequency is 100 MHz, timer increments every 4 CPU clocks */
  607. #define USECS_PER_JIFFY (1000000 / HZ)
  608. #define TICK_TIMER_LIMIT ((100 * 1000000 / 4) / HZ)
  609. static unsigned int pcic_cycles_offset(void)
  610. {
  611. u32 value, count;
  612. value = readl(pcic0.pcic_regs + PCI_SYS_COUNTER);
  613. count = value & ~PCI_SYS_COUNTER_OVERFLOW;
  614. if (value & PCI_SYS_COUNTER_OVERFLOW)
  615. count += TICK_TIMER_LIMIT;
  616. /*
  617. * We divide all by HZ
  618. * to have microsecond resolution and to avoid overflow
  619. */
  620. count = ((count / HZ) * USECS_PER_JIFFY) / (TICK_TIMER_LIMIT / HZ);
  621. /* Coordinate with the sparc_config.clock_rate setting */
  622. return count * 2;
  623. }
  624. void __init pci_time_init(void)
  625. {
  626. struct linux_pcic *pcic = &pcic0;
  627. unsigned long v;
  628. int timer_irq, irq;
  629. int err;
  630. #ifndef CONFIG_SMP
  631. /*
  632. * The clock_rate is in SBUS dimension.
  633. * We take into account this in pcic_cycles_offset()
  634. */
  635. sparc_config.clock_rate = SBUS_CLOCK_RATE / HZ;
  636. sparc_config.features |= FEAT_L10_CLOCKEVENT;
  637. #endif
  638. sparc_config.features |= FEAT_L10_CLOCKSOURCE;
  639. sparc_config.get_cycles_offset = pcic_cycles_offset;
  640. writel (TICK_TIMER_LIMIT, pcic->pcic_regs+PCI_SYS_LIMIT);
  641. /* PROM should set appropriate irq */
  642. v = readb(pcic->pcic_regs+PCI_COUNTER_IRQ);
  643. timer_irq = PCI_COUNTER_IRQ_SYS(v);
  644. writel (PCI_COUNTER_IRQ_SET(timer_irq, 0),
  645. pcic->pcic_regs+PCI_COUNTER_IRQ);
  646. irq = pcic_build_device_irq(NULL, timer_irq);
  647. err = request_irq(irq, timer_interrupt,
  648. IRQF_TIMER, "timer", NULL);
  649. if (err) {
  650. prom_printf("time_init: unable to attach IRQ%d\n", timer_irq);
  651. prom_halt();
  652. }
  653. local_irq_enable();
  654. }
  655. #if 0
  656. static void watchdog_reset() {
  657. writeb(0, pcic->pcic_regs+PCI_SYS_STATUS);
  658. }
  659. #endif
  660. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  661. resource_size_t size, resource_size_t align)
  662. {
  663. return res->start;
  664. }
  665. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  666. {
  667. return 0;
  668. }
  669. /*
  670. * NMI
  671. */
  672. void pcic_nmi(unsigned int pend, struct pt_regs *regs)
  673. {
  674. pend = swab32(pend);
  675. if (!pcic_speculative || (pend & PCI_SYS_INT_PENDING_PIO) == 0) {
  676. /*
  677. * XXX On CP-1200 PCI #SERR may happen, we do not know
  678. * what to do about it yet.
  679. */
  680. printk("Aiee, NMI pend 0x%x pc 0x%x spec %d, hanging\n",
  681. pend, (int)regs->pc, pcic_speculative);
  682. for (;;) { }
  683. }
  684. pcic_speculative = 0;
  685. pcic_trapped = 1;
  686. regs->pc = regs->npc;
  687. regs->npc += 4;
  688. }
  689. static inline unsigned long get_irqmask(int irq_nr)
  690. {
  691. return 1 << irq_nr;
  692. }
  693. static void pcic_mask_irq(struct irq_data *data)
  694. {
  695. unsigned long mask, flags;
  696. mask = (unsigned long)data->chip_data;
  697. local_irq_save(flags);
  698. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
  699. local_irq_restore(flags);
  700. }
  701. static void pcic_unmask_irq(struct irq_data *data)
  702. {
  703. unsigned long mask, flags;
  704. mask = (unsigned long)data->chip_data;
  705. local_irq_save(flags);
  706. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
  707. local_irq_restore(flags);
  708. }
  709. static unsigned int pcic_startup_irq(struct irq_data *data)
  710. {
  711. irq_link(data->irq);
  712. pcic_unmask_irq(data);
  713. return 0;
  714. }
  715. static struct irq_chip pcic_irq = {
  716. .name = "pcic",
  717. .irq_startup = pcic_startup_irq,
  718. .irq_mask = pcic_mask_irq,
  719. .irq_unmask = pcic_unmask_irq,
  720. };
  721. unsigned int pcic_build_device_irq(struct platform_device *op,
  722. unsigned int real_irq)
  723. {
  724. unsigned int irq;
  725. unsigned long mask;
  726. irq = 0;
  727. mask = get_irqmask(real_irq);
  728. if (mask == 0)
  729. goto out;
  730. irq = irq_alloc(real_irq, real_irq);
  731. if (irq == 0)
  732. goto out;
  733. irq_set_chip_and_handler_name(irq, &pcic_irq,
  734. handle_level_irq, "PCIC");
  735. irq_set_chip_data(irq, (void *)mask);
  736. out:
  737. return irq;
  738. }
  739. static void pcic_load_profile_irq(int cpu, unsigned int limit)
  740. {
  741. printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
  742. }
  743. void __init sun4m_pci_init_IRQ(void)
  744. {
  745. sparc_config.build_device_irq = pcic_build_device_irq;
  746. sparc_config.clear_clock_irq = pcic_clear_clock_irq;
  747. sparc_config.load_profile_irq = pcic_load_profile_irq;
  748. }
  749. subsys_initcall(pcic_init);