pcic.c 22 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858
  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 (!pbm->pci_bus)
  351. return;
  352. #if 0 /* deadwood transplanted from sparc64 */
  353. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
  354. pci_record_assignments(pbm, pbm->pci_bus);
  355. pci_assign_unassigned(pbm, pbm->pci_bus);
  356. pci_fixup_irq(pbm, pbm->pci_bus);
  357. #endif
  358. pci_bus_add_devices(pbm->pci_bus);
  359. }
  360. /*
  361. * Main entry point from the PCI subsystem.
  362. */
  363. static int __init pcic_init(void)
  364. {
  365. struct linux_pcic *pcic;
  366. /*
  367. * PCIC should be initialized at start of the timer.
  368. * So, here we report the presence of PCIC and do some magic passes.
  369. */
  370. if(!pcic0_up)
  371. return 0;
  372. pcic = &pcic0;
  373. /*
  374. * Switch off IOTLB translation.
  375. */
  376. writeb(PCI_DVMA_CONTROL_IOTLB_DISABLE,
  377. pcic->pcic_regs+PCI_DVMA_CONTROL);
  378. /*
  379. * Increase mapped size for PCI memory space (DMA access).
  380. * Should be done in that order (size first, address second).
  381. * Why we couldn't set up 4GB and forget about it? XXX
  382. */
  383. writel(0xF0000000UL, pcic->pcic_regs+PCI_SIZE_0);
  384. writel(0+PCI_BASE_ADDRESS_SPACE_MEMORY,
  385. pcic->pcic_regs+PCI_BASE_ADDRESS_0);
  386. pcic_pbm_scan_bus(pcic);
  387. return 0;
  388. }
  389. int pcic_present(void)
  390. {
  391. return pcic0_up;
  392. }
  393. static int pdev_to_pnode(struct linux_pbm_info *pbm, struct pci_dev *pdev)
  394. {
  395. struct linux_prom_pci_registers regs[PROMREG_MAX];
  396. int err;
  397. phandle node = prom_getchild(pbm->prom_node);
  398. while(node) {
  399. err = prom_getproperty(node, "reg",
  400. (char *)&regs[0], sizeof(regs));
  401. if(err != 0 && err != -1) {
  402. unsigned long devfn = (regs[0].which_io >> 8) & 0xff;
  403. if(devfn == pdev->devfn)
  404. return node;
  405. }
  406. node = prom_getsibling(node);
  407. }
  408. return 0;
  409. }
  410. static inline struct pcidev_cookie *pci_devcookie_alloc(void)
  411. {
  412. return kmalloc(sizeof(struct pcidev_cookie), GFP_ATOMIC);
  413. }
  414. static void pcic_map_pci_device(struct linux_pcic *pcic,
  415. struct pci_dev *dev, int node)
  416. {
  417. char namebuf[64];
  418. unsigned long address;
  419. unsigned long flags;
  420. int j;
  421. if (node == 0 || node == -1) {
  422. strcpy(namebuf, "???");
  423. } else {
  424. prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
  425. }
  426. for (j = 0; j < 6; j++) {
  427. address = dev->resource[j].start;
  428. if (address == 0) break; /* are sequential */
  429. flags = dev->resource[j].flags;
  430. if ((flags & IORESOURCE_IO) != 0) {
  431. if (address < 0x10000) {
  432. /*
  433. * A device responds to I/O cycles on PCI.
  434. * We generate these cycles with memory
  435. * access into the fixed map (phys 0x30000000).
  436. *
  437. * Since a device driver does not want to
  438. * do ioremap() before accessing PC-style I/O,
  439. * we supply virtual, ready to access address.
  440. *
  441. * Note that request_region()
  442. * works for these devices.
  443. *
  444. * XXX Neat trick, but it's a *bad* idea
  445. * to shit into regions like that.
  446. * What if we want to allocate one more
  447. * PCI base address...
  448. */
  449. dev->resource[j].start =
  450. pcic->pcic_io + address;
  451. dev->resource[j].end = 1; /* XXX */
  452. dev->resource[j].flags =
  453. (flags & ~IORESOURCE_IO) | IORESOURCE_MEM;
  454. } else {
  455. /*
  456. * OOPS... PCI Spec allows this. Sun does
  457. * not have any devices getting above 64K
  458. * so it must be user with a weird I/O
  459. * board in a PCI slot. We must remap it
  460. * under 64K but it is not done yet. XXX
  461. */
  462. printk("PCIC: Skipping I/O space at 0x%lx, "
  463. "this will Oops if a driver attaches "
  464. "device '%s' at %02x:%02x)\n", address,
  465. namebuf, dev->bus->number, dev->devfn);
  466. }
  467. }
  468. }
  469. }
  470. static void
  471. pcic_fill_irq(struct linux_pcic *pcic, struct pci_dev *dev, int node)
  472. {
  473. struct pcic_ca2irq *p;
  474. unsigned int real_irq;
  475. int i, ivec;
  476. char namebuf[64];
  477. if (node == 0 || node == -1) {
  478. strcpy(namebuf, "???");
  479. } else {
  480. prom_getstring(node, "name", namebuf, sizeof(namebuf));
  481. }
  482. if ((p = pcic->pcic_imap) == NULL) {
  483. dev->irq = 0;
  484. return;
  485. }
  486. for (i = 0; i < pcic->pcic_imdim; i++) {
  487. if (p->busno == dev->bus->number && p->devfn == dev->devfn)
  488. break;
  489. p++;
  490. }
  491. if (i >= pcic->pcic_imdim) {
  492. printk("PCIC: device %s devfn %02x:%02x not found in %d\n",
  493. namebuf, dev->bus->number, dev->devfn, pcic->pcic_imdim);
  494. dev->irq = 0;
  495. return;
  496. }
  497. i = p->pin;
  498. if (i >= 0 && i < 4) {
  499. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  500. real_irq = ivec >> (i << 2) & 0xF;
  501. } else if (i >= 4 && i < 8) {
  502. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  503. real_irq = ivec >> ((i-4) << 2) & 0xF;
  504. } else { /* Corrupted map */
  505. printk("PCIC: BAD PIN %d\n", i); for (;;) {}
  506. }
  507. /* P3 */ /* printk("PCIC: device %s pin %d ivec 0x%x irq %x\n", namebuf, i, ivec, dev->irq); */
  508. /* real_irq means PROM did not bother to program the upper
  509. * half of PCIC. This happens on JS-E with PROM 3.11, for instance.
  510. */
  511. if (real_irq == 0 || p->force) {
  512. if (p->irq == 0 || p->irq >= 15) { /* Corrupted map */
  513. printk("PCIC: BAD IRQ %d\n", p->irq); for (;;) {}
  514. }
  515. printk("PCIC: setting irq %d at pin %d for device %02x:%02x\n",
  516. p->irq, p->pin, dev->bus->number, dev->devfn);
  517. real_irq = p->irq;
  518. i = p->pin;
  519. if (i >= 4) {
  520. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  521. ivec &= ~(0xF << ((i - 4) << 2));
  522. ivec |= p->irq << ((i - 4) << 2);
  523. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_HI);
  524. } else {
  525. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  526. ivec &= ~(0xF << (i << 2));
  527. ivec |= p->irq << (i << 2);
  528. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_LO);
  529. }
  530. }
  531. dev->irq = pcic_build_device_irq(NULL, real_irq);
  532. }
  533. /*
  534. * Normally called from {do_}pci_scan_bus...
  535. */
  536. void pcibios_fixup_bus(struct pci_bus *bus)
  537. {
  538. struct pci_dev *dev;
  539. int i, has_io, has_mem;
  540. unsigned int cmd;
  541. struct linux_pcic *pcic;
  542. /* struct linux_pbm_info* pbm = &pcic->pbm; */
  543. int node;
  544. struct pcidev_cookie *pcp;
  545. if (!pcic0_up) {
  546. printk("pcibios_fixup_bus: no PCIC\n");
  547. return;
  548. }
  549. pcic = &pcic0;
  550. /*
  551. * Next crud is an equivalent of pbm = pcic_bus_to_pbm(bus);
  552. */
  553. if (bus->number != 0) {
  554. printk("pcibios_fixup_bus: nonzero bus 0x%x\n", bus->number);
  555. return;
  556. }
  557. list_for_each_entry(dev, &bus->devices, bus_list) {
  558. /*
  559. * Comment from i386 branch:
  560. * There are buggy BIOSes that forget to enable I/O and memory
  561. * access to PCI devices. We try to fix this, but we need to
  562. * be sure that the BIOS didn't forget to assign an address
  563. * to the device. [mj]
  564. * OBP is a case of such BIOS :-)
  565. */
  566. has_io = has_mem = 0;
  567. for(i=0; i<6; i++) {
  568. unsigned long f = dev->resource[i].flags;
  569. if (f & IORESOURCE_IO) {
  570. has_io = 1;
  571. } else if (f & IORESOURCE_MEM)
  572. has_mem = 1;
  573. }
  574. pcic_read_config(dev->bus, dev->devfn, PCI_COMMAND, 2, &cmd);
  575. if (has_io && !(cmd & PCI_COMMAND_IO)) {
  576. printk("PCIC: Enabling I/O for device %02x:%02x\n",
  577. dev->bus->number, dev->devfn);
  578. cmd |= PCI_COMMAND_IO;
  579. pcic_write_config(dev->bus, dev->devfn,
  580. PCI_COMMAND, 2, cmd);
  581. }
  582. if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
  583. printk("PCIC: Enabling memory for device %02x:%02x\n",
  584. dev->bus->number, dev->devfn);
  585. cmd |= PCI_COMMAND_MEMORY;
  586. pcic_write_config(dev->bus, dev->devfn,
  587. PCI_COMMAND, 2, cmd);
  588. }
  589. node = pdev_to_pnode(&pcic->pbm, dev);
  590. if(node == 0)
  591. node = -1;
  592. /* cookies */
  593. pcp = pci_devcookie_alloc();
  594. pcp->pbm = &pcic->pbm;
  595. pcp->prom_node = of_find_node_by_phandle(node);
  596. dev->sysdata = pcp;
  597. /* fixing I/O to look like memory */
  598. if ((dev->class>>16) != PCI_BASE_CLASS_BRIDGE)
  599. pcic_map_pci_device(pcic, dev, node);
  600. pcic_fill_irq(pcic, dev, node);
  601. }
  602. }
  603. /* Makes compiler happy */
  604. static volatile int pcic_timer_dummy;
  605. static void pcic_clear_clock_irq(void)
  606. {
  607. pcic_timer_dummy = readl(pcic0.pcic_regs+PCI_SYS_LIMIT);
  608. }
  609. /* CPU frequency is 100 MHz, timer increments every 4 CPU clocks */
  610. #define USECS_PER_JIFFY (1000000 / HZ)
  611. #define TICK_TIMER_LIMIT ((100 * 1000000 / 4) / HZ)
  612. static unsigned int pcic_cycles_offset(void)
  613. {
  614. u32 value, count;
  615. value = readl(pcic0.pcic_regs + PCI_SYS_COUNTER);
  616. count = value & ~PCI_SYS_COUNTER_OVERFLOW;
  617. if (value & PCI_SYS_COUNTER_OVERFLOW)
  618. count += TICK_TIMER_LIMIT;
  619. /*
  620. * We divide all by HZ
  621. * to have microsecond resolution and to avoid overflow
  622. */
  623. count = ((count / HZ) * USECS_PER_JIFFY) / (TICK_TIMER_LIMIT / HZ);
  624. /* Coordinate with the sparc_config.clock_rate setting */
  625. return count * 2;
  626. }
  627. void __init pci_time_init(void)
  628. {
  629. struct linux_pcic *pcic = &pcic0;
  630. unsigned long v;
  631. int timer_irq, irq;
  632. int err;
  633. #ifndef CONFIG_SMP
  634. /*
  635. * The clock_rate is in SBUS dimension.
  636. * We take into account this in pcic_cycles_offset()
  637. */
  638. sparc_config.clock_rate = SBUS_CLOCK_RATE / HZ;
  639. sparc_config.features |= FEAT_L10_CLOCKEVENT;
  640. #endif
  641. sparc_config.features |= FEAT_L10_CLOCKSOURCE;
  642. sparc_config.get_cycles_offset = pcic_cycles_offset;
  643. writel (TICK_TIMER_LIMIT, pcic->pcic_regs+PCI_SYS_LIMIT);
  644. /* PROM should set appropriate irq */
  645. v = readb(pcic->pcic_regs+PCI_COUNTER_IRQ);
  646. timer_irq = PCI_COUNTER_IRQ_SYS(v);
  647. writel (PCI_COUNTER_IRQ_SET(timer_irq, 0),
  648. pcic->pcic_regs+PCI_COUNTER_IRQ);
  649. irq = pcic_build_device_irq(NULL, timer_irq);
  650. err = request_irq(irq, timer_interrupt,
  651. IRQF_TIMER, "timer", NULL);
  652. if (err) {
  653. prom_printf("time_init: unable to attach IRQ%d\n", timer_irq);
  654. prom_halt();
  655. }
  656. local_irq_enable();
  657. }
  658. #if 0
  659. static void watchdog_reset() {
  660. writeb(0, pcic->pcic_regs+PCI_SYS_STATUS);
  661. }
  662. #endif
  663. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  664. resource_size_t size, resource_size_t align)
  665. {
  666. return res->start;
  667. }
  668. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  669. {
  670. return 0;
  671. }
  672. /*
  673. * NMI
  674. */
  675. void pcic_nmi(unsigned int pend, struct pt_regs *regs)
  676. {
  677. pend = swab32(pend);
  678. if (!pcic_speculative || (pend & PCI_SYS_INT_PENDING_PIO) == 0) {
  679. /*
  680. * XXX On CP-1200 PCI #SERR may happen, we do not know
  681. * what to do about it yet.
  682. */
  683. printk("Aiee, NMI pend 0x%x pc 0x%x spec %d, hanging\n",
  684. pend, (int)regs->pc, pcic_speculative);
  685. for (;;) { }
  686. }
  687. pcic_speculative = 0;
  688. pcic_trapped = 1;
  689. regs->pc = regs->npc;
  690. regs->npc += 4;
  691. }
  692. static inline unsigned long get_irqmask(int irq_nr)
  693. {
  694. return 1 << irq_nr;
  695. }
  696. static void pcic_mask_irq(struct irq_data *data)
  697. {
  698. unsigned long mask, flags;
  699. mask = (unsigned long)data->chip_data;
  700. local_irq_save(flags);
  701. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
  702. local_irq_restore(flags);
  703. }
  704. static void pcic_unmask_irq(struct irq_data *data)
  705. {
  706. unsigned long mask, flags;
  707. mask = (unsigned long)data->chip_data;
  708. local_irq_save(flags);
  709. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
  710. local_irq_restore(flags);
  711. }
  712. static unsigned int pcic_startup_irq(struct irq_data *data)
  713. {
  714. irq_link(data->irq);
  715. pcic_unmask_irq(data);
  716. return 0;
  717. }
  718. static struct irq_chip pcic_irq = {
  719. .name = "pcic",
  720. .irq_startup = pcic_startup_irq,
  721. .irq_mask = pcic_mask_irq,
  722. .irq_unmask = pcic_unmask_irq,
  723. };
  724. unsigned int pcic_build_device_irq(struct platform_device *op,
  725. unsigned int real_irq)
  726. {
  727. unsigned int irq;
  728. unsigned long mask;
  729. irq = 0;
  730. mask = get_irqmask(real_irq);
  731. if (mask == 0)
  732. goto out;
  733. irq = irq_alloc(real_irq, real_irq);
  734. if (irq == 0)
  735. goto out;
  736. irq_set_chip_and_handler_name(irq, &pcic_irq,
  737. handle_level_irq, "PCIC");
  738. irq_set_chip_data(irq, (void *)mask);
  739. out:
  740. return irq;
  741. }
  742. static void pcic_load_profile_irq(int cpu, unsigned int limit)
  743. {
  744. printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
  745. }
  746. void __init sun4m_pci_init_IRQ(void)
  747. {
  748. sparc_config.build_device_irq = pcic_build_device_irq;
  749. sparc_config.clear_clock_irq = pcic_clear_clock_irq;
  750. sparc_config.load_profile_irq = pcic_load_profile_irq;
  751. }
  752. subsys_initcall(pcic_init);