pcic.c 26 KB

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