pcic.c 27 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030
  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/ebus.h>
  19. #include <asm/sbus.h> /* for sanity check... */
  20. #include <asm/swift.h> /* for cache flushing. */
  21. #include <asm/io.h>
  22. #include <linux/ctype.h>
  23. #include <linux/pci.h>
  24. #include <linux/time.h>
  25. #include <linux/timex.h>
  26. #include <linux/interrupt.h>
  27. #include <asm/irq.h>
  28. #include <asm/oplib.h>
  29. #include <asm/prom.h>
  30. #include <asm/pcic.h>
  31. #include <asm/timer.h>
  32. #include <asm/uaccess.h>
  33. unsigned int pcic_pin_to_irq(unsigned int pin, char *name);
  34. /*
  35. * I studied different documents and many live PROMs both from 2.30
  36. * family and 3.xx versions. I came to the amazing conclusion: there is
  37. * absolutely no way to route interrupts in IIep systems relying on
  38. * information which PROM presents. We must hardcode interrupt routing
  39. * schematics. And this actually sucks. -- zaitcev 1999/05/12
  40. *
  41. * To find irq for a device we determine which routing map
  42. * is in effect or, in other words, on which machine we are running.
  43. * We use PROM name for this although other techniques may be used
  44. * in special cases (Gleb reports a PROMless IIep based system).
  45. * Once we know the map we take device configuration address and
  46. * find PCIC pin number where INT line goes. Then we may either program
  47. * preferred irq into the PCIC or supply the preexisting irq to the device.
  48. */
  49. struct pcic_ca2irq {
  50. unsigned char busno; /* PCI bus number */
  51. unsigned char devfn; /* Configuration address */
  52. unsigned char pin; /* PCIC external interrupt pin */
  53. unsigned char irq; /* Preferred IRQ (mappable in PCIC) */
  54. unsigned int force; /* Enforce preferred IRQ */
  55. };
  56. struct pcic_sn2list {
  57. char *sysname;
  58. struct pcic_ca2irq *intmap;
  59. int mapdim;
  60. };
  61. /*
  62. * JavaEngine-1 apparently has different versions.
  63. *
  64. * According to communications with Sun folks, for P2 build 501-4628-03:
  65. * pin 0 - parallel, audio;
  66. * pin 1 - Ethernet;
  67. * pin 2 - su;
  68. * pin 3 - PS/2 kbd and mouse.
  69. *
  70. * OEM manual (805-1486):
  71. * pin 0: Ethernet
  72. * pin 1: All EBus
  73. * pin 2: IGA (unused)
  74. * pin 3: Not connected
  75. * OEM manual says that 501-4628 & 501-4811 are the same thing,
  76. * only the latter has NAND flash in place.
  77. *
  78. * So far unofficial Sun wins over the OEM manual. Poor OEMs...
  79. */
  80. static struct pcic_ca2irq pcic_i_je1a[] = { /* 501-4811-03 */
  81. { 0, 0x00, 2, 12, 0 }, /* EBus: hogs all */
  82. { 0, 0x01, 1, 6, 1 }, /* Happy Meal */
  83. { 0, 0x80, 0, 7, 0 }, /* IGA (unused) */
  84. };
  85. /* XXX JS-E entry is incomplete - PCI Slot 2 address (pin 7)? */
  86. static struct pcic_ca2irq pcic_i_jse[] = {
  87. { 0, 0x00, 0, 13, 0 }, /* Ebus - serial and keyboard */
  88. { 0, 0x01, 1, 6, 0 }, /* hme */
  89. { 0, 0x08, 2, 9, 0 }, /* VGA - we hope not used :) */
  90. { 0, 0x10, 6, 8, 0 }, /* PCI INTA# in Slot 1 */
  91. { 0, 0x18, 7, 12, 0 }, /* PCI INTA# in Slot 2, shared w. RTC */
  92. { 0, 0x38, 4, 9, 0 }, /* All ISA devices. Read 8259. */
  93. { 0, 0x80, 5, 11, 0 }, /* EIDE */
  94. /* {0,0x88, 0,0,0} - unknown device... PMU? Probably no interrupt. */
  95. { 0, 0xA0, 4, 9, 0 }, /* USB */
  96. /*
  97. * Some pins belong to non-PCI devices, we hardcode them in drivers.
  98. * sun4m timers - irq 10, 14
  99. * PC style RTC - pin 7, irq 4 ?
  100. * Smart card, Parallel - pin 4 shared with USB, ISA
  101. * audio - pin 3, irq 5 ?
  102. */
  103. };
  104. /* SPARCengine-6 was the original release name of CP1200.
  105. * The documentation differs between the two versions
  106. */
  107. static struct pcic_ca2irq pcic_i_se6[] = {
  108. { 0, 0x08, 0, 2, 0 }, /* SCSI */
  109. { 0, 0x01, 1, 6, 0 }, /* HME */
  110. { 0, 0x00, 3, 13, 0 }, /* EBus */
  111. };
  112. /*
  113. * Krups (courtesy of Varol Kaptan)
  114. * No documentation available, but it was easy to guess
  115. * because it was very similar to Espresso.
  116. *
  117. * pin 0 - kbd, mouse, serial;
  118. * pin 1 - Ethernet;
  119. * pin 2 - igs (we do not use it);
  120. * pin 3 - audio;
  121. * pin 4,5,6 - unused;
  122. * pin 7 - RTC (from P2 onwards as David B. says).
  123. */
  124. static struct pcic_ca2irq pcic_i_jk[] = {
  125. { 0, 0x00, 0, 13, 0 }, /* Ebus - serial and keyboard */
  126. { 0, 0x01, 1, 6, 0 }, /* hme */
  127. };
  128. /*
  129. * Several entries in this list may point to the same routing map
  130. * as several PROMs may be installed on the same physical board.
  131. */
  132. #define SN2L_INIT(name, map) \
  133. { name, map, ARRAY_SIZE(map) }
  134. static struct pcic_sn2list pcic_known_sysnames[] = {
  135. SN2L_INIT("SUNW,JavaEngine1", pcic_i_je1a), /* JE1, PROM 2.32 */
  136. SN2L_INIT("SUNW,JS-E", pcic_i_jse), /* PROLL JavaStation-E */
  137. SN2L_INIT("SUNW,SPARCengine-6", pcic_i_se6), /* SPARCengine-6/CP-1200 */
  138. SN2L_INIT("SUNW,JS-NC", pcic_i_jk), /* PROLL JavaStation-NC */
  139. SN2L_INIT("SUNW,JSIIep", pcic_i_jk), /* OBP JavaStation-NC */
  140. { NULL, NULL, 0 }
  141. };
  142. /*
  143. * Only one PCIC per IIep,
  144. * and since we have no SMP IIep, only one per system.
  145. */
  146. static int pcic0_up;
  147. static struct linux_pcic pcic0;
  148. void __iomem *pcic_regs;
  149. volatile int pcic_speculative;
  150. volatile int pcic_trapped;
  151. static void pci_do_gettimeofday(struct timeval *tv);
  152. static int pci_do_settimeofday(struct timespec *tv);
  153. #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (((unsigned int)bus) << 16) | (((unsigned int)device_fn) << 8) | (where & ~3))
  154. static int pcic_read_config_dword(unsigned int busno, unsigned int devfn,
  155. int where, u32 *value)
  156. {
  157. struct linux_pcic *pcic;
  158. unsigned long flags;
  159. pcic = &pcic0;
  160. local_irq_save(flags);
  161. #if 0 /* does not fail here */
  162. pcic_speculative = 1;
  163. pcic_trapped = 0;
  164. #endif
  165. writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
  166. #if 0 /* does not fail here */
  167. nop();
  168. if (pcic_trapped) {
  169. local_irq_restore(flags);
  170. *value = ~0;
  171. return 0;
  172. }
  173. #endif
  174. pcic_speculative = 2;
  175. pcic_trapped = 0;
  176. *value = readl(pcic->pcic_config_space_data + (where&4));
  177. nop();
  178. if (pcic_trapped) {
  179. pcic_speculative = 0;
  180. local_irq_restore(flags);
  181. *value = ~0;
  182. return 0;
  183. }
  184. pcic_speculative = 0;
  185. local_irq_restore(flags);
  186. return 0;
  187. }
  188. static int pcic_read_config(struct pci_bus *bus, unsigned int devfn,
  189. int where, int size, u32 *val)
  190. {
  191. unsigned int v;
  192. if (bus->number != 0) return -EINVAL;
  193. switch (size) {
  194. case 1:
  195. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  196. *val = 0xff & (v >> (8*(where & 3)));
  197. return 0;
  198. case 2:
  199. if (where&1) return -EINVAL;
  200. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  201. *val = 0xffff & (v >> (8*(where & 3)));
  202. return 0;
  203. case 4:
  204. if (where&3) return -EINVAL;
  205. pcic_read_config_dword(bus->number, devfn, where&~3, val);
  206. return 0;
  207. }
  208. return -EINVAL;
  209. }
  210. static int pcic_write_config_dword(unsigned int busno, unsigned int devfn,
  211. int where, u32 value)
  212. {
  213. struct linux_pcic *pcic;
  214. unsigned long flags;
  215. pcic = &pcic0;
  216. local_irq_save(flags);
  217. writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
  218. writel(value, pcic->pcic_config_space_data + (where&4));
  219. local_irq_restore(flags);
  220. return 0;
  221. }
  222. static int pcic_write_config(struct pci_bus *bus, unsigned int devfn,
  223. int where, int size, u32 val)
  224. {
  225. unsigned int v;
  226. if (bus->number != 0) return -EINVAL;
  227. switch (size) {
  228. case 1:
  229. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  230. v = (v & ~(0xff << (8*(where&3)))) |
  231. ((0xff&val) << (8*(where&3)));
  232. return pcic_write_config_dword(bus->number, devfn, where&~3, v);
  233. case 2:
  234. if (where&1) return -EINVAL;
  235. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  236. v = (v & ~(0xffff << (8*(where&3)))) |
  237. ((0xffff&val) << (8*(where&3)));
  238. return pcic_write_config_dword(bus->number, devfn, where&~3, v);
  239. case 4:
  240. if (where&3) return -EINVAL;
  241. return pcic_write_config_dword(bus->number, devfn, where, val);
  242. }
  243. return -EINVAL;
  244. }
  245. static struct pci_ops pcic_ops = {
  246. .read = pcic_read_config,
  247. .write = pcic_write_config,
  248. };
  249. /*
  250. * On sparc64 pcibios_init() calls pci_controller_probe().
  251. * We want PCIC probed little ahead so that interrupt controller
  252. * would be operational.
  253. */
  254. int __init pcic_probe(void)
  255. {
  256. struct linux_pcic *pcic;
  257. struct linux_prom_registers regs[PROMREG_MAX];
  258. struct linux_pbm_info* pbm;
  259. char namebuf[64];
  260. int node;
  261. int err;
  262. if (pcic0_up) {
  263. prom_printf("PCIC: called twice!\n");
  264. prom_halt();
  265. }
  266. pcic = &pcic0;
  267. node = prom_getchild (prom_root_node);
  268. node = prom_searchsiblings (node, "pci");
  269. if (node == 0)
  270. return -ENODEV;
  271. /*
  272. * Map in PCIC register set, config space, and IO base
  273. */
  274. err = prom_getproperty(node, "reg", (char*)regs, sizeof(regs));
  275. if (err == 0 || err == -1) {
  276. prom_printf("PCIC: Error, cannot get PCIC registers "
  277. "from PROM.\n");
  278. prom_halt();
  279. }
  280. pcic0_up = 1;
  281. pcic->pcic_res_regs.name = "pcic_registers";
  282. pcic->pcic_regs = ioremap(regs[0].phys_addr, regs[0].reg_size);
  283. if (!pcic->pcic_regs) {
  284. prom_printf("PCIC: Error, cannot map PCIC registers.\n");
  285. prom_halt();
  286. }
  287. pcic->pcic_res_io.name = "pcic_io";
  288. if ((pcic->pcic_io = (unsigned long)
  289. ioremap(regs[1].phys_addr, 0x10000)) == 0) {
  290. prom_printf("PCIC: Error, cannot map PCIC IO Base.\n");
  291. prom_halt();
  292. }
  293. pcic->pcic_res_cfg_addr.name = "pcic_cfg_addr";
  294. if ((pcic->pcic_config_space_addr =
  295. ioremap(regs[2].phys_addr, regs[2].reg_size * 2)) == 0) {
  296. prom_printf("PCIC: Error, cannot map"
  297. "PCI Configuration Space Address.\n");
  298. prom_halt();
  299. }
  300. /*
  301. * Docs say three least significant bits in address and data
  302. * must be the same. Thus, we need adjust size of data.
  303. */
  304. pcic->pcic_res_cfg_data.name = "pcic_cfg_data";
  305. if ((pcic->pcic_config_space_data =
  306. ioremap(regs[3].phys_addr, regs[3].reg_size * 2)) == 0) {
  307. prom_printf("PCIC: Error, cannot map"
  308. "PCI Configuration Space Data.\n");
  309. prom_halt();
  310. }
  311. pbm = &pcic->pbm;
  312. pbm->prom_node = node;
  313. prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
  314. strcpy(pbm->prom_name, namebuf);
  315. {
  316. extern volatile int t_nmi[1];
  317. extern int pcic_nmi_trap_patch[1];
  318. t_nmi[0] = pcic_nmi_trap_patch[0];
  319. t_nmi[1] = pcic_nmi_trap_patch[1];
  320. t_nmi[2] = pcic_nmi_trap_patch[2];
  321. t_nmi[3] = pcic_nmi_trap_patch[3];
  322. swift_flush_dcache();
  323. pcic_regs = pcic->pcic_regs;
  324. }
  325. prom_getstring(prom_root_node, "name", namebuf, 63); namebuf[63] = 0;
  326. {
  327. struct pcic_sn2list *p;
  328. for (p = pcic_known_sysnames; p->sysname != NULL; p++) {
  329. if (strcmp(namebuf, p->sysname) == 0)
  330. break;
  331. }
  332. pcic->pcic_imap = p->intmap;
  333. pcic->pcic_imdim = p->mapdim;
  334. }
  335. if (pcic->pcic_imap == NULL) {
  336. /*
  337. * We do not panic here for the sake of embedded systems.
  338. */
  339. printk("PCIC: System %s is unknown, cannot route interrupts\n",
  340. namebuf);
  341. }
  342. return 0;
  343. }
  344. static void __init pcic_pbm_scan_bus(struct linux_pcic *pcic)
  345. {
  346. struct linux_pbm_info *pbm = &pcic->pbm;
  347. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, &pcic_ops, pbm);
  348. #if 0 /* deadwood transplanted from sparc64 */
  349. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
  350. pci_record_assignments(pbm, pbm->pci_bus);
  351. pci_assign_unassigned(pbm, pbm->pci_bus);
  352. pci_fixup_irq(pbm, pbm->pci_bus);
  353. #endif
  354. }
  355. /*
  356. * Main entry point from the PCI subsystem.
  357. */
  358. static int __init pcic_init(void)
  359. {
  360. struct linux_pcic *pcic;
  361. /*
  362. * PCIC should be initialized at start of the timer.
  363. * So, here we report the presence of PCIC and do some magic passes.
  364. */
  365. if(!pcic0_up)
  366. return 0;
  367. pcic = &pcic0;
  368. /*
  369. * Switch off IOTLB translation.
  370. */
  371. writeb(PCI_DVMA_CONTROL_IOTLB_DISABLE,
  372. pcic->pcic_regs+PCI_DVMA_CONTROL);
  373. /*
  374. * Increase mapped size for PCI memory space (DMA access).
  375. * Should be done in that order (size first, address second).
  376. * Why we couldn't set up 4GB and forget about it? XXX
  377. */
  378. writel(0xF0000000UL, pcic->pcic_regs+PCI_SIZE_0);
  379. writel(0+PCI_BASE_ADDRESS_SPACE_MEMORY,
  380. pcic->pcic_regs+PCI_BASE_ADDRESS_0);
  381. pcic_pbm_scan_bus(pcic);
  382. ebus_init();
  383. return 0;
  384. }
  385. int pcic_present(void)
  386. {
  387. return pcic0_up;
  388. }
  389. static int __init pdev_to_pnode(struct linux_pbm_info *pbm,
  390. struct pci_dev *pdev)
  391. {
  392. struct linux_prom_pci_registers regs[PROMREG_MAX];
  393. int err;
  394. int 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. * Ebus devices do not come here even if
  439. * CheerIO makes a similar conversion.
  440. * See ebus.c for details.
  441. *
  442. * Note that request_region()
  443. * works for these devices.
  444. *
  445. * XXX Neat trick, but it's a *bad* idea
  446. * to shit into regions like that.
  447. * What if we want to allocate one more
  448. * PCI base address...
  449. */
  450. dev->resource[j].start =
  451. pcic->pcic_io + address;
  452. dev->resource[j].end = 1; /* XXX */
  453. dev->resource[j].flags =
  454. (flags & ~IORESOURCE_IO) | IORESOURCE_MEM;
  455. } else {
  456. /*
  457. * OOPS... PCI Spec allows this. Sun does
  458. * not have any devices getting above 64K
  459. * so it must be user with a weird I/O
  460. * board in a PCI slot. We must remap it
  461. * under 64K but it is not done yet. XXX
  462. */
  463. printk("PCIC: Skipping I/O space at 0x%lx,"
  464. "this will Oops if a driver attaches;"
  465. "device '%s' at %02x:%02x)\n", address,
  466. namebuf, dev->bus->number, dev->devfn);
  467. }
  468. }
  469. }
  470. }
  471. static void
  472. pcic_fill_irq(struct linux_pcic *pcic, struct pci_dev *dev, int node)
  473. {
  474. struct pcic_ca2irq *p;
  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) == 0) {
  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. dev->irq = ivec >> (i << 2) & 0xF;
  501. } else if (i >= 4 && i < 8) {
  502. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  503. dev->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. /*
  509. * dev->irq=0 means PROM did not bother to program the upper
  510. * half of PCIC. This happens on JS-E with PROM 3.11, for instance.
  511. */
  512. if (dev->irq == 0 || p->force) {
  513. if (p->irq == 0 || p->irq >= 15) { /* Corrupted map */
  514. printk("PCIC: BAD IRQ %d\n", p->irq); for (;;) {}
  515. }
  516. printk("PCIC: setting irq %d at pin %d for device %02x:%02x\n",
  517. p->irq, p->pin, dev->bus->number, dev->devfn);
  518. dev->irq = p->irq;
  519. i = p->pin;
  520. if (i >= 4) {
  521. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  522. ivec &= ~(0xF << ((i - 4) << 2));
  523. ivec |= p->irq << ((i - 4) << 2);
  524. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_HI);
  525. } else {
  526. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  527. ivec &= ~(0xF << (i << 2));
  528. ivec |= p->irq << (i << 2);
  529. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_LO);
  530. }
  531. }
  532. return;
  533. }
  534. /*
  535. * Normally called from {do_}pci_scan_bus...
  536. */
  537. void __init pcibios_fixup_bus(struct pci_bus *bus)
  538. {
  539. struct pci_dev *dev;
  540. int i, has_io, has_mem;
  541. unsigned int cmd;
  542. struct linux_pcic *pcic;
  543. /* struct linux_pbm_info* pbm = &pcic->pbm; */
  544. int node;
  545. struct pcidev_cookie *pcp;
  546. if (!pcic0_up) {
  547. printk("pcibios_fixup_bus: no PCIC\n");
  548. return;
  549. }
  550. pcic = &pcic0;
  551. /*
  552. * Next crud is an equivalent of pbm = pcic_bus_to_pbm(bus);
  553. */
  554. if (bus->number != 0) {
  555. printk("pcibios_fixup_bus: nonzero bus 0x%x\n", bus->number);
  556. return;
  557. }
  558. list_for_each_entry(dev, &bus->devices, bus_list) {
  559. /*
  560. * Comment from i386 branch:
  561. * There are buggy BIOSes that forget to enable I/O and memory
  562. * access to PCI devices. We try to fix this, but we need to
  563. * be sure that the BIOS didn't forget to assign an address
  564. * to the device. [mj]
  565. * OBP is a case of such BIOS :-)
  566. */
  567. has_io = has_mem = 0;
  568. for(i=0; i<6; i++) {
  569. unsigned long f = dev->resource[i].flags;
  570. if (f & IORESOURCE_IO) {
  571. has_io = 1;
  572. } else if (f & IORESOURCE_MEM)
  573. has_mem = 1;
  574. }
  575. pcic_read_config(dev->bus, dev->devfn, PCI_COMMAND, 2, &cmd);
  576. if (has_io && !(cmd & PCI_COMMAND_IO)) {
  577. printk("PCIC: Enabling I/O for device %02x:%02x\n",
  578. dev->bus->number, dev->devfn);
  579. cmd |= PCI_COMMAND_IO;
  580. pcic_write_config(dev->bus, dev->devfn,
  581. PCI_COMMAND, 2, cmd);
  582. }
  583. if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
  584. printk("PCIC: Enabling memory for device %02x:%02x\n",
  585. dev->bus->number, dev->devfn);
  586. cmd |= PCI_COMMAND_MEMORY;
  587. pcic_write_config(dev->bus, dev->devfn,
  588. PCI_COMMAND, 2, cmd);
  589. }
  590. node = pdev_to_pnode(&pcic->pbm, dev);
  591. if(node == 0)
  592. node = -1;
  593. /* cookies */
  594. pcp = pci_devcookie_alloc();
  595. pcp->pbm = &pcic->pbm;
  596. pcp->prom_node = of_find_node_by_phandle(node);
  597. dev->sysdata = pcp;
  598. /* fixing I/O to look like memory */
  599. if ((dev->class>>16) != PCI_BASE_CLASS_BRIDGE)
  600. pcic_map_pci_device(pcic, dev, node);
  601. pcic_fill_irq(pcic, dev, node);
  602. }
  603. }
  604. /*
  605. * pcic_pin_to_irq() is exported to ebus.c.
  606. */
  607. unsigned int
  608. pcic_pin_to_irq(unsigned int pin, char *name)
  609. {
  610. struct linux_pcic *pcic = &pcic0;
  611. unsigned int irq;
  612. unsigned int ivec;
  613. if (pin < 4) {
  614. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  615. irq = ivec >> (pin << 2) & 0xF;
  616. } else if (pin < 8) {
  617. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  618. irq = ivec >> ((pin-4) << 2) & 0xF;
  619. } else { /* Corrupted map */
  620. printk("PCIC: BAD PIN %d FOR %s\n", pin, name);
  621. for (;;) {} /* XXX Cannot panic properly in case of PROLL */
  622. }
  623. /* P3 */ /* printk("PCIC: dev %s pin %d ivec 0x%x irq %x\n", name, pin, ivec, irq); */
  624. return irq;
  625. }
  626. /* Makes compiler happy */
  627. static volatile int pcic_timer_dummy;
  628. static void pcic_clear_clock_irq(void)
  629. {
  630. pcic_timer_dummy = readl(pcic0.pcic_regs+PCI_SYS_LIMIT);
  631. }
  632. static irqreturn_t pcic_timer_handler (int irq, void *h, struct pt_regs *regs)
  633. {
  634. write_seqlock(&xtime_lock); /* Dummy, to show that we remember */
  635. pcic_clear_clock_irq();
  636. do_timer(regs);
  637. #ifndef CONFIG_SMP
  638. update_process_times(user_mode(regs));
  639. #endif
  640. write_sequnlock(&xtime_lock);
  641. return IRQ_HANDLED;
  642. }
  643. #define USECS_PER_JIFFY 10000 /* We have 100HZ "standard" timer for sparc */
  644. #define TICK_TIMER_LIMIT ((100*1000000/4)/100)
  645. void __init pci_time_init(void)
  646. {
  647. struct linux_pcic *pcic = &pcic0;
  648. unsigned long v;
  649. int timer_irq, irq;
  650. /* A hack until do_gettimeofday prototype is moved to arch specific headers
  651. and btfixupped. Patch do_gettimeofday with ba pci_do_gettimeofday; nop */
  652. ((unsigned int *)do_gettimeofday)[0] =
  653. 0x10800000 | ((((unsigned long)pci_do_gettimeofday -
  654. (unsigned long)do_gettimeofday) >> 2) & 0x003fffff);
  655. ((unsigned int *)do_gettimeofday)[1] = 0x01000000;
  656. BTFIXUPSET_CALL(bus_do_settimeofday, pci_do_settimeofday, BTFIXUPCALL_NORM);
  657. btfixup();
  658. writel (TICK_TIMER_LIMIT, pcic->pcic_regs+PCI_SYS_LIMIT);
  659. /* PROM should set appropriate irq */
  660. v = readb(pcic->pcic_regs+PCI_COUNTER_IRQ);
  661. timer_irq = PCI_COUNTER_IRQ_SYS(v);
  662. writel (PCI_COUNTER_IRQ_SET(timer_irq, 0),
  663. pcic->pcic_regs+PCI_COUNTER_IRQ);
  664. irq = request_irq(timer_irq, pcic_timer_handler,
  665. (IRQF_DISABLED | SA_STATIC_ALLOC), "timer", NULL);
  666. if (irq) {
  667. prom_printf("time_init: unable to attach IRQ%d\n", timer_irq);
  668. prom_halt();
  669. }
  670. local_irq_enable();
  671. }
  672. static __inline__ unsigned long do_gettimeoffset(void)
  673. {
  674. /*
  675. * We devide all to 100
  676. * to have microsecond resolution and to avoid overflow
  677. */
  678. unsigned long count =
  679. readl(pcic0.pcic_regs+PCI_SYS_COUNTER) & ~PCI_SYS_COUNTER_OVERFLOW;
  680. count = ((count/100)*USECS_PER_JIFFY) / (TICK_TIMER_LIMIT/100);
  681. return count;
  682. }
  683. extern unsigned long wall_jiffies;
  684. static void pci_do_gettimeofday(struct timeval *tv)
  685. {
  686. unsigned long flags;
  687. unsigned long seq;
  688. unsigned long usec, sec;
  689. unsigned long max_ntp_tick = tick_usec - tickadj;
  690. do {
  691. unsigned long lost;
  692. seq = read_seqbegin_irqsave(&xtime_lock, flags);
  693. usec = do_gettimeoffset();
  694. lost = jiffies - wall_jiffies;
  695. /*
  696. * If time_adjust is negative then NTP is slowing the clock
  697. * so make sure not to go into next possible interval.
  698. * Better to lose some accuracy than have time go backwards..
  699. */
  700. if (unlikely(time_adjust < 0)) {
  701. usec = min(usec, max_ntp_tick);
  702. if (lost)
  703. usec += lost * max_ntp_tick;
  704. }
  705. else if (unlikely(lost))
  706. usec += lost * tick_usec;
  707. sec = xtime.tv_sec;
  708. usec += (xtime.tv_nsec / 1000);
  709. } while (read_seqretry_irqrestore(&xtime_lock, seq, flags));
  710. while (usec >= 1000000) {
  711. usec -= 1000000;
  712. sec++;
  713. }
  714. tv->tv_sec = sec;
  715. tv->tv_usec = usec;
  716. }
  717. static int pci_do_settimeofday(struct timespec *tv)
  718. {
  719. if ((unsigned long)tv->tv_nsec >= NSEC_PER_SEC)
  720. return -EINVAL;
  721. /*
  722. * This is revolting. We need to set "xtime" correctly. However, the
  723. * value in this location is the value at the most recent update of
  724. * wall time. Discover what correction gettimeofday() would have
  725. * made, and then undo it!
  726. */
  727. tv->tv_nsec -= 1000 * (do_gettimeoffset() +
  728. (jiffies - wall_jiffies) * (USEC_PER_SEC / HZ));
  729. while (tv->tv_nsec < 0) {
  730. tv->tv_nsec += NSEC_PER_SEC;
  731. tv->tv_sec--;
  732. }
  733. wall_to_monotonic.tv_sec += xtime.tv_sec - tv->tv_sec;
  734. wall_to_monotonic.tv_nsec += xtime.tv_nsec - tv->tv_nsec;
  735. if (wall_to_monotonic.tv_nsec > NSEC_PER_SEC) {
  736. wall_to_monotonic.tv_nsec -= NSEC_PER_SEC;
  737. wall_to_monotonic.tv_sec++;
  738. }
  739. if (wall_to_monotonic.tv_nsec < 0) {
  740. wall_to_monotonic.tv_nsec += NSEC_PER_SEC;
  741. wall_to_monotonic.tv_sec--;
  742. }
  743. xtime.tv_sec = tv->tv_sec;
  744. xtime.tv_nsec = tv->tv_nsec;
  745. ntp_clear();
  746. return 0;
  747. }
  748. #if 0
  749. static void watchdog_reset() {
  750. writeb(0, pcic->pcic_regs+PCI_SYS_STATUS);
  751. }
  752. #endif
  753. /*
  754. * Other archs parse arguments here.
  755. */
  756. char * __init pcibios_setup(char *str)
  757. {
  758. return str;
  759. }
  760. void pcibios_align_resource(void *data, struct resource *res,
  761. resource_size_t size, resource_size_t align)
  762. {
  763. }
  764. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  765. {
  766. return 0;
  767. }
  768. /*
  769. * NMI
  770. */
  771. void pcic_nmi(unsigned int pend, struct pt_regs *regs)
  772. {
  773. pend = flip_dword(pend);
  774. if (!pcic_speculative || (pend & PCI_SYS_INT_PENDING_PIO) == 0) {
  775. /*
  776. * XXX On CP-1200 PCI #SERR may happen, we do not know
  777. * what to do about it yet.
  778. */
  779. printk("Aiee, NMI pend 0x%x pc 0x%x spec %d, hanging\n",
  780. pend, (int)regs->pc, pcic_speculative);
  781. for (;;) { }
  782. }
  783. pcic_speculative = 0;
  784. pcic_trapped = 1;
  785. regs->pc = regs->npc;
  786. regs->npc += 4;
  787. }
  788. static inline unsigned long get_irqmask(int irq_nr)
  789. {
  790. return 1 << irq_nr;
  791. }
  792. static void pcic_disable_irq(unsigned int irq_nr)
  793. {
  794. unsigned long mask, flags;
  795. mask = get_irqmask(irq_nr);
  796. local_irq_save(flags);
  797. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
  798. local_irq_restore(flags);
  799. }
  800. static void pcic_enable_irq(unsigned int irq_nr)
  801. {
  802. unsigned long mask, flags;
  803. mask = get_irqmask(irq_nr);
  804. local_irq_save(flags);
  805. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
  806. local_irq_restore(flags);
  807. }
  808. static void pcic_clear_profile_irq(int cpu)
  809. {
  810. printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
  811. }
  812. static void pcic_load_profile_irq(int cpu, unsigned int limit)
  813. {
  814. printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
  815. }
  816. /* We assume the caller has disabled local interrupts when these are called,
  817. * or else very bizarre behavior will result.
  818. */
  819. static void pcic_disable_pil_irq(unsigned int pil)
  820. {
  821. writel(get_irqmask(pil), pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
  822. }
  823. static void pcic_enable_pil_irq(unsigned int pil)
  824. {
  825. writel(get_irqmask(pil), pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
  826. }
  827. void __init sun4m_pci_init_IRQ(void)
  828. {
  829. BTFIXUPSET_CALL(enable_irq, pcic_enable_irq, BTFIXUPCALL_NORM);
  830. BTFIXUPSET_CALL(disable_irq, pcic_disable_irq, BTFIXUPCALL_NORM);
  831. BTFIXUPSET_CALL(enable_pil_irq, pcic_enable_pil_irq, BTFIXUPCALL_NORM);
  832. BTFIXUPSET_CALL(disable_pil_irq, pcic_disable_pil_irq, BTFIXUPCALL_NORM);
  833. BTFIXUPSET_CALL(clear_clock_irq, pcic_clear_clock_irq, BTFIXUPCALL_NORM);
  834. BTFIXUPSET_CALL(clear_profile_irq, pcic_clear_profile_irq, BTFIXUPCALL_NORM);
  835. BTFIXUPSET_CALL(load_profile_irq, pcic_load_profile_irq, BTFIXUPCALL_NORM);
  836. }
  837. int pcibios_assign_resource(struct pci_dev *pdev, int resource)
  838. {
  839. return -ENXIO;
  840. }
  841. /*
  842. * This probably belongs here rather than ioport.c because
  843. * we do not want this crud linked into SBus kernels.
  844. * Also, think for a moment about likes of floppy.c that
  845. * include architecture specific parts. They may want to redefine ins/outs.
  846. *
  847. * We do not use horroble macroses here because we want to
  848. * advance pointer by sizeof(size).
  849. */
  850. void outsb(unsigned long addr, const void *src, unsigned long count)
  851. {
  852. while (count) {
  853. count -= 1;
  854. outb(*(const char *)src, addr);
  855. src += 1;
  856. /* addr += 1; */
  857. }
  858. }
  859. void outsw(unsigned long addr, const void *src, unsigned long count)
  860. {
  861. while (count) {
  862. count -= 2;
  863. outw(*(const short *)src, addr);
  864. src += 2;
  865. /* addr += 2; */
  866. }
  867. }
  868. void outsl(unsigned long addr, const void *src, unsigned long count)
  869. {
  870. while (count) {
  871. count -= 4;
  872. outl(*(const long *)src, addr);
  873. src += 4;
  874. /* addr += 4; */
  875. }
  876. }
  877. void insb(unsigned long addr, void *dst, unsigned long count)
  878. {
  879. while (count) {
  880. count -= 1;
  881. *(unsigned char *)dst = inb(addr);
  882. dst += 1;
  883. /* addr += 1; */
  884. }
  885. }
  886. void insw(unsigned long addr, void *dst, unsigned long count)
  887. {
  888. while (count) {
  889. count -= 2;
  890. *(unsigned short *)dst = inw(addr);
  891. dst += 2;
  892. /* addr += 2; */
  893. }
  894. }
  895. void insl(unsigned long addr, void *dst, unsigned long count)
  896. {
  897. while (count) {
  898. count -= 4;
  899. /*
  900. * XXX I am sure we are in for an unaligned trap here.
  901. */
  902. *(unsigned long *)dst = inl(addr);
  903. dst += 4;
  904. /* addr += 4; */
  905. }
  906. }
  907. subsys_initcall(pcic_init);