pcic.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975
  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/timex.h>
  30. #include <asm/timer.h>
  31. #include <asm/uaccess.h>
  32. #include <asm/irq_regs.h>
  33. #include "irq.h"
  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. /* forward */
  152. unsigned int pcic_build_device_irq(struct platform_device *op,
  153. unsigned int real_irq);
  154. #define CONFIG_CMD(bus, device_fn, where) (0x80000000 | (((unsigned int)bus) << 16) | (((unsigned int)device_fn) << 8) | (where & ~3))
  155. static int pcic_read_config_dword(unsigned int busno, unsigned int devfn,
  156. int where, u32 *value)
  157. {
  158. struct linux_pcic *pcic;
  159. unsigned long flags;
  160. pcic = &pcic0;
  161. local_irq_save(flags);
  162. #if 0 /* does not fail here */
  163. pcic_speculative = 1;
  164. pcic_trapped = 0;
  165. #endif
  166. writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
  167. #if 0 /* does not fail here */
  168. nop();
  169. if (pcic_trapped) {
  170. local_irq_restore(flags);
  171. *value = ~0;
  172. return 0;
  173. }
  174. #endif
  175. pcic_speculative = 2;
  176. pcic_trapped = 0;
  177. *value = readl(pcic->pcic_config_space_data + (where&4));
  178. nop();
  179. if (pcic_trapped) {
  180. pcic_speculative = 0;
  181. local_irq_restore(flags);
  182. *value = ~0;
  183. return 0;
  184. }
  185. pcic_speculative = 0;
  186. local_irq_restore(flags);
  187. return 0;
  188. }
  189. static int pcic_read_config(struct pci_bus *bus, unsigned int devfn,
  190. int where, int size, u32 *val)
  191. {
  192. unsigned int v;
  193. if (bus->number != 0) return -EINVAL;
  194. switch (size) {
  195. case 1:
  196. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  197. *val = 0xff & (v >> (8*(where & 3)));
  198. return 0;
  199. case 2:
  200. if (where&1) return -EINVAL;
  201. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  202. *val = 0xffff & (v >> (8*(where & 3)));
  203. return 0;
  204. case 4:
  205. if (where&3) return -EINVAL;
  206. pcic_read_config_dword(bus->number, devfn, where&~3, val);
  207. return 0;
  208. }
  209. return -EINVAL;
  210. }
  211. static int pcic_write_config_dword(unsigned int busno, unsigned int devfn,
  212. int where, u32 value)
  213. {
  214. struct linux_pcic *pcic;
  215. unsigned long flags;
  216. pcic = &pcic0;
  217. local_irq_save(flags);
  218. writel(CONFIG_CMD(busno, devfn, where), pcic->pcic_config_space_addr);
  219. writel(value, pcic->pcic_config_space_data + (where&4));
  220. local_irq_restore(flags);
  221. return 0;
  222. }
  223. static int pcic_write_config(struct pci_bus *bus, unsigned int devfn,
  224. int where, int size, u32 val)
  225. {
  226. unsigned int v;
  227. if (bus->number != 0) return -EINVAL;
  228. switch (size) {
  229. case 1:
  230. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  231. v = (v & ~(0xff << (8*(where&3)))) |
  232. ((0xff&val) << (8*(where&3)));
  233. return pcic_write_config_dword(bus->number, devfn, where&~3, v);
  234. case 2:
  235. if (where&1) return -EINVAL;
  236. pcic_read_config_dword(bus->number, devfn, where&~3, &v);
  237. v = (v & ~(0xffff << (8*(where&3)))) |
  238. ((0xffff&val) << (8*(where&3)));
  239. return pcic_write_config_dword(bus->number, devfn, where&~3, v);
  240. case 4:
  241. if (where&3) return -EINVAL;
  242. return pcic_write_config_dword(bus->number, devfn, where, val);
  243. }
  244. return -EINVAL;
  245. }
  246. static struct pci_ops pcic_ops = {
  247. .read = pcic_read_config,
  248. .write = pcic_write_config,
  249. };
  250. /*
  251. * On sparc64 pcibios_init() calls pci_controller_probe().
  252. * We want PCIC probed little ahead so that interrupt controller
  253. * would be operational.
  254. */
  255. int __init pcic_probe(void)
  256. {
  257. struct linux_pcic *pcic;
  258. struct linux_prom_registers regs[PROMREG_MAX];
  259. struct linux_pbm_info* pbm;
  260. char namebuf[64];
  261. phandle node;
  262. int err;
  263. if (pcic0_up) {
  264. prom_printf("PCIC: called twice!\n");
  265. prom_halt();
  266. }
  267. pcic = &pcic0;
  268. node = prom_getchild (prom_root_node);
  269. node = prom_searchsiblings (node, "pci");
  270. if (node == 0)
  271. return -ENODEV;
  272. /*
  273. * Map in PCIC register set, config space, and IO base
  274. */
  275. err = prom_getproperty(node, "reg", (char*)regs, sizeof(regs));
  276. if (err == 0 || err == -1) {
  277. prom_printf("PCIC: Error, cannot get PCIC registers "
  278. "from PROM.\n");
  279. prom_halt();
  280. }
  281. pcic0_up = 1;
  282. pcic->pcic_res_regs.name = "pcic_registers";
  283. pcic->pcic_regs = ioremap(regs[0].phys_addr, regs[0].reg_size);
  284. if (!pcic->pcic_regs) {
  285. prom_printf("PCIC: Error, cannot map PCIC registers.\n");
  286. prom_halt();
  287. }
  288. pcic->pcic_res_io.name = "pcic_io";
  289. if ((pcic->pcic_io = (unsigned long)
  290. ioremap(regs[1].phys_addr, 0x10000)) == 0) {
  291. prom_printf("PCIC: Error, cannot map PCIC IO Base.\n");
  292. prom_halt();
  293. }
  294. pcic->pcic_res_cfg_addr.name = "pcic_cfg_addr";
  295. if ((pcic->pcic_config_space_addr =
  296. ioremap(regs[2].phys_addr, regs[2].reg_size * 2)) == 0) {
  297. prom_printf("PCIC: Error, cannot map "
  298. "PCI Configuration Space Address.\n");
  299. prom_halt();
  300. }
  301. /*
  302. * Docs say three least significant bits in address and data
  303. * must be the same. Thus, we need adjust size of data.
  304. */
  305. pcic->pcic_res_cfg_data.name = "pcic_cfg_data";
  306. if ((pcic->pcic_config_space_data =
  307. ioremap(regs[3].phys_addr, regs[3].reg_size * 2)) == 0) {
  308. prom_printf("PCIC: Error, cannot map "
  309. "PCI Configuration Space Data.\n");
  310. prom_halt();
  311. }
  312. pbm = &pcic->pbm;
  313. pbm->prom_node = node;
  314. prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
  315. strcpy(pbm->prom_name, namebuf);
  316. {
  317. extern volatile int t_nmi[4];
  318. extern int pcic_nmi_trap_patch[4];
  319. t_nmi[0] = pcic_nmi_trap_patch[0];
  320. t_nmi[1] = pcic_nmi_trap_patch[1];
  321. t_nmi[2] = pcic_nmi_trap_patch[2];
  322. t_nmi[3] = pcic_nmi_trap_patch[3];
  323. swift_flush_dcache();
  324. pcic_regs = pcic->pcic_regs;
  325. }
  326. prom_getstring(prom_root_node, "name", namebuf, 63); namebuf[63] = 0;
  327. {
  328. struct pcic_sn2list *p;
  329. for (p = pcic_known_sysnames; p->sysname != NULL; p++) {
  330. if (strcmp(namebuf, p->sysname) == 0)
  331. break;
  332. }
  333. pcic->pcic_imap = p->intmap;
  334. pcic->pcic_imdim = p->mapdim;
  335. }
  336. if (pcic->pcic_imap == NULL) {
  337. /*
  338. * We do not panic here for the sake of embedded systems.
  339. */
  340. printk("PCIC: System %s is unknown, cannot route interrupts\n",
  341. namebuf);
  342. }
  343. return 0;
  344. }
  345. static void __init pcic_pbm_scan_bus(struct linux_pcic *pcic)
  346. {
  347. struct linux_pbm_info *pbm = &pcic->pbm;
  348. pbm->pci_bus = pci_scan_bus(pbm->pci_first_busno, &pcic_ops, pbm);
  349. #if 0 /* deadwood transplanted from sparc64 */
  350. pci_fill_in_pbm_cookies(pbm->pci_bus, pbm, pbm->prom_node);
  351. pci_record_assignments(pbm, pbm->pci_bus);
  352. pci_assign_unassigned(pbm, pbm->pci_bus);
  353. pci_fixup_irq(pbm, pbm->pci_bus);
  354. #endif
  355. }
  356. /*
  357. * Main entry point from the PCI subsystem.
  358. */
  359. static int __init pcic_init(void)
  360. {
  361. struct linux_pcic *pcic;
  362. /*
  363. * PCIC should be initialized at start of the timer.
  364. * So, here we report the presence of PCIC and do some magic passes.
  365. */
  366. if(!pcic0_up)
  367. return 0;
  368. pcic = &pcic0;
  369. /*
  370. * Switch off IOTLB translation.
  371. */
  372. writeb(PCI_DVMA_CONTROL_IOTLB_DISABLE,
  373. pcic->pcic_regs+PCI_DVMA_CONTROL);
  374. /*
  375. * Increase mapped size for PCI memory space (DMA access).
  376. * Should be done in that order (size first, address second).
  377. * Why we couldn't set up 4GB and forget about it? XXX
  378. */
  379. writel(0xF0000000UL, pcic->pcic_regs+PCI_SIZE_0);
  380. writel(0+PCI_BASE_ADDRESS_SPACE_MEMORY,
  381. pcic->pcic_regs+PCI_BASE_ADDRESS_0);
  382. pcic_pbm_scan_bus(pcic);
  383. return 0;
  384. }
  385. int pcic_present(void)
  386. {
  387. return pcic0_up;
  388. }
  389. static int __devinit 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. phandle node = prom_getchild(pbm->prom_node);
  395. while(node) {
  396. err = prom_getproperty(node, "reg",
  397. (char *)&regs[0], sizeof(regs));
  398. if(err != 0 && err != -1) {
  399. unsigned long devfn = (regs[0].which_io >> 8) & 0xff;
  400. if(devfn == pdev->devfn)
  401. return node;
  402. }
  403. node = prom_getsibling(node);
  404. }
  405. return 0;
  406. }
  407. static inline struct pcidev_cookie *pci_devcookie_alloc(void)
  408. {
  409. return kmalloc(sizeof(struct pcidev_cookie), GFP_ATOMIC);
  410. }
  411. static void pcic_map_pci_device(struct linux_pcic *pcic,
  412. struct pci_dev *dev, int node)
  413. {
  414. char namebuf[64];
  415. unsigned long address;
  416. unsigned long flags;
  417. int j;
  418. if (node == 0 || node == -1) {
  419. strcpy(namebuf, "???");
  420. } else {
  421. prom_getstring(node, "name", namebuf, 63); namebuf[63] = 0;
  422. }
  423. for (j = 0; j < 6; j++) {
  424. address = dev->resource[j].start;
  425. if (address == 0) break; /* are sequential */
  426. flags = dev->resource[j].flags;
  427. if ((flags & IORESOURCE_IO) != 0) {
  428. if (address < 0x10000) {
  429. /*
  430. * A device responds to I/O cycles on PCI.
  431. * We generate these cycles with memory
  432. * access into the fixed map (phys 0x30000000).
  433. *
  434. * Since a device driver does not want to
  435. * do ioremap() before accessing PC-style I/O,
  436. * we supply virtual, ready to access address.
  437. *
  438. * Note that request_region()
  439. * works for these devices.
  440. *
  441. * XXX Neat trick, but it's a *bad* idea
  442. * to shit into regions like that.
  443. * What if we want to allocate one more
  444. * PCI base address...
  445. */
  446. dev->resource[j].start =
  447. pcic->pcic_io + address;
  448. dev->resource[j].end = 1; /* XXX */
  449. dev->resource[j].flags =
  450. (flags & ~IORESOURCE_IO) | IORESOURCE_MEM;
  451. } else {
  452. /*
  453. * OOPS... PCI Spec allows this. Sun does
  454. * not have any devices getting above 64K
  455. * so it must be user with a weird I/O
  456. * board in a PCI slot. We must remap it
  457. * under 64K but it is not done yet. XXX
  458. */
  459. printk("PCIC: Skipping I/O space at 0x%lx, "
  460. "this will Oops if a driver attaches "
  461. "device '%s' at %02x:%02x)\n", address,
  462. namebuf, dev->bus->number, dev->devfn);
  463. }
  464. }
  465. }
  466. }
  467. static void
  468. pcic_fill_irq(struct linux_pcic *pcic, struct pci_dev *dev, int node)
  469. {
  470. struct pcic_ca2irq *p;
  471. unsigned int real_irq;
  472. int i, ivec;
  473. char namebuf[64];
  474. if (node == 0 || node == -1) {
  475. strcpy(namebuf, "???");
  476. } else {
  477. prom_getstring(node, "name", namebuf, sizeof(namebuf));
  478. }
  479. if ((p = pcic->pcic_imap) == 0) {
  480. dev->irq = 0;
  481. return;
  482. }
  483. for (i = 0; i < pcic->pcic_imdim; i++) {
  484. if (p->busno == dev->bus->number && p->devfn == dev->devfn)
  485. break;
  486. p++;
  487. }
  488. if (i >= pcic->pcic_imdim) {
  489. printk("PCIC: device %s devfn %02x:%02x not found in %d\n",
  490. namebuf, dev->bus->number, dev->devfn, pcic->pcic_imdim);
  491. dev->irq = 0;
  492. return;
  493. }
  494. i = p->pin;
  495. if (i >= 0 && i < 4) {
  496. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  497. real_irq = ivec >> (i << 2) & 0xF;
  498. } else if (i >= 4 && i < 8) {
  499. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  500. real_irq = ivec >> ((i-4) << 2) & 0xF;
  501. } else { /* Corrupted map */
  502. printk("PCIC: BAD PIN %d\n", i); for (;;) {}
  503. }
  504. /* P3 */ /* printk("PCIC: device %s pin %d ivec 0x%x irq %x\n", namebuf, i, ivec, dev->irq); */
  505. /* real_irq means PROM did not bother to program the upper
  506. * half of PCIC. This happens on JS-E with PROM 3.11, for instance.
  507. */
  508. if (real_irq == 0 || p->force) {
  509. if (p->irq == 0 || p->irq >= 15) { /* Corrupted map */
  510. printk("PCIC: BAD IRQ %d\n", p->irq); for (;;) {}
  511. }
  512. printk("PCIC: setting irq %d at pin %d for device %02x:%02x\n",
  513. p->irq, p->pin, dev->bus->number, dev->devfn);
  514. real_irq = p->irq;
  515. i = p->pin;
  516. if (i >= 4) {
  517. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  518. ivec &= ~(0xF << ((i - 4) << 2));
  519. ivec |= p->irq << ((i - 4) << 2);
  520. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_HI);
  521. } else {
  522. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  523. ivec &= ~(0xF << (i << 2));
  524. ivec |= p->irq << (i << 2);
  525. writew(ivec, pcic->pcic_regs+PCI_INT_SELECT_LO);
  526. }
  527. }
  528. dev->irq = pcic_build_device_irq(NULL, real_irq);
  529. }
  530. /*
  531. * Normally called from {do_}pci_scan_bus...
  532. */
  533. void __devinit pcibios_fixup_bus(struct pci_bus *bus)
  534. {
  535. struct pci_dev *dev;
  536. int i, has_io, has_mem;
  537. unsigned int cmd;
  538. struct linux_pcic *pcic;
  539. /* struct linux_pbm_info* pbm = &pcic->pbm; */
  540. int node;
  541. struct pcidev_cookie *pcp;
  542. if (!pcic0_up) {
  543. printk("pcibios_fixup_bus: no PCIC\n");
  544. return;
  545. }
  546. pcic = &pcic0;
  547. /*
  548. * Next crud is an equivalent of pbm = pcic_bus_to_pbm(bus);
  549. */
  550. if (bus->number != 0) {
  551. printk("pcibios_fixup_bus: nonzero bus 0x%x\n", bus->number);
  552. return;
  553. }
  554. list_for_each_entry(dev, &bus->devices, bus_list) {
  555. /*
  556. * Comment from i386 branch:
  557. * There are buggy BIOSes that forget to enable I/O and memory
  558. * access to PCI devices. We try to fix this, but we need to
  559. * be sure that the BIOS didn't forget to assign an address
  560. * to the device. [mj]
  561. * OBP is a case of such BIOS :-)
  562. */
  563. has_io = has_mem = 0;
  564. for(i=0; i<6; i++) {
  565. unsigned long f = dev->resource[i].flags;
  566. if (f & IORESOURCE_IO) {
  567. has_io = 1;
  568. } else if (f & IORESOURCE_MEM)
  569. has_mem = 1;
  570. }
  571. pcic_read_config(dev->bus, dev->devfn, PCI_COMMAND, 2, &cmd);
  572. if (has_io && !(cmd & PCI_COMMAND_IO)) {
  573. printk("PCIC: Enabling I/O for device %02x:%02x\n",
  574. dev->bus->number, dev->devfn);
  575. cmd |= PCI_COMMAND_IO;
  576. pcic_write_config(dev->bus, dev->devfn,
  577. PCI_COMMAND, 2, cmd);
  578. }
  579. if (has_mem && !(cmd & PCI_COMMAND_MEMORY)) {
  580. printk("PCIC: Enabling memory for device %02x:%02x\n",
  581. dev->bus->number, dev->devfn);
  582. cmd |= PCI_COMMAND_MEMORY;
  583. pcic_write_config(dev->bus, dev->devfn,
  584. PCI_COMMAND, 2, cmd);
  585. }
  586. node = pdev_to_pnode(&pcic->pbm, dev);
  587. if(node == 0)
  588. node = -1;
  589. /* cookies */
  590. pcp = pci_devcookie_alloc();
  591. pcp->pbm = &pcic->pbm;
  592. pcp->prom_node = of_find_node_by_phandle(node);
  593. dev->sysdata = pcp;
  594. /* fixing I/O to look like memory */
  595. if ((dev->class>>16) != PCI_BASE_CLASS_BRIDGE)
  596. pcic_map_pci_device(pcic, dev, node);
  597. pcic_fill_irq(pcic, dev, node);
  598. }
  599. }
  600. /*
  601. * pcic_pin_to_irq() is exported to bus probing code
  602. */
  603. unsigned int
  604. pcic_pin_to_irq(unsigned int pin, const char *name)
  605. {
  606. struct linux_pcic *pcic = &pcic0;
  607. unsigned int irq;
  608. unsigned int ivec;
  609. if (pin < 4) {
  610. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_LO);
  611. irq = ivec >> (pin << 2) & 0xF;
  612. } else if (pin < 8) {
  613. ivec = readw(pcic->pcic_regs+PCI_INT_SELECT_HI);
  614. irq = ivec >> ((pin-4) << 2) & 0xF;
  615. } else { /* Corrupted map */
  616. printk("PCIC: BAD PIN %d FOR %s\n", pin, name);
  617. for (;;) {} /* XXX Cannot panic properly in case of PROLL */
  618. }
  619. /* P3 */ /* printk("PCIC: dev %s pin %d ivec 0x%x irq %x\n", name, pin, ivec, irq); */
  620. return irq;
  621. }
  622. /* Makes compiler happy */
  623. static volatile int pcic_timer_dummy;
  624. static void pcic_clear_clock_irq(void)
  625. {
  626. pcic_timer_dummy = readl(pcic0.pcic_regs+PCI_SYS_LIMIT);
  627. }
  628. static irqreturn_t pcic_timer_handler (int irq, void *h)
  629. {
  630. pcic_clear_clock_irq();
  631. xtime_update(1);
  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. u32 pci_gettimeoffset(void)
  640. {
  641. /*
  642. * We divide all by 100
  643. * to have microsecond resolution and to avoid overflow
  644. */
  645. unsigned long count =
  646. readl(pcic0.pcic_regs+PCI_SYS_COUNTER) & ~PCI_SYS_COUNTER_OVERFLOW;
  647. count = ((count/100)*USECS_PER_JIFFY) / (TICK_TIMER_LIMIT/100);
  648. return count * 1000;
  649. }
  650. void __init pci_time_init(void)
  651. {
  652. struct linux_pcic *pcic = &pcic0;
  653. unsigned long v;
  654. int timer_irq, irq;
  655. int err;
  656. do_arch_gettimeoffset = pci_gettimeoffset;
  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 = pcic_build_device_irq(NULL, timer_irq);
  665. err = request_irq(irq, pcic_timer_handler,
  666. IRQF_TIMER, "timer", NULL);
  667. if (err) {
  668. prom_printf("time_init: unable to attach IRQ%d\n", timer_irq);
  669. prom_halt();
  670. }
  671. local_irq_enable();
  672. }
  673. #if 0
  674. static void watchdog_reset() {
  675. writeb(0, pcic->pcic_regs+PCI_SYS_STATUS);
  676. }
  677. #endif
  678. /*
  679. * Other archs parse arguments here.
  680. */
  681. char * __devinit pcibios_setup(char *str)
  682. {
  683. return str;
  684. }
  685. resource_size_t pcibios_align_resource(void *data, const struct resource *res,
  686. resource_size_t size, resource_size_t align)
  687. {
  688. return res->start;
  689. }
  690. int pcibios_enable_device(struct pci_dev *pdev, int mask)
  691. {
  692. return 0;
  693. }
  694. /*
  695. * NMI
  696. */
  697. void pcic_nmi(unsigned int pend, struct pt_regs *regs)
  698. {
  699. pend = flip_dword(pend);
  700. if (!pcic_speculative || (pend & PCI_SYS_INT_PENDING_PIO) == 0) {
  701. /*
  702. * XXX On CP-1200 PCI #SERR may happen, we do not know
  703. * what to do about it yet.
  704. */
  705. printk("Aiee, NMI pend 0x%x pc 0x%x spec %d, hanging\n",
  706. pend, (int)regs->pc, pcic_speculative);
  707. for (;;) { }
  708. }
  709. pcic_speculative = 0;
  710. pcic_trapped = 1;
  711. regs->pc = regs->npc;
  712. regs->npc += 4;
  713. }
  714. static inline unsigned long get_irqmask(int irq_nr)
  715. {
  716. return 1 << irq_nr;
  717. }
  718. static void pcic_mask_irq(struct irq_data *data)
  719. {
  720. unsigned long mask, flags;
  721. mask = (unsigned long)data->chip_data;
  722. local_irq_save(flags);
  723. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_SET);
  724. local_irq_restore(flags);
  725. }
  726. static void pcic_unmask_irq(struct irq_data *data)
  727. {
  728. unsigned long mask, flags;
  729. mask = (unsigned long)data->chip_data;
  730. local_irq_save(flags);
  731. writel(mask, pcic0.pcic_regs+PCI_SYS_INT_TARGET_MASK_CLEAR);
  732. local_irq_restore(flags);
  733. }
  734. static unsigned int pcic_startup_irq(struct irq_data *data)
  735. {
  736. irq_link(data->irq);
  737. pcic_unmask_irq(data);
  738. return 0;
  739. }
  740. static struct irq_chip pcic_irq = {
  741. .name = "pcic",
  742. .irq_startup = pcic_startup_irq,
  743. .irq_mask = pcic_mask_irq,
  744. .irq_unmask = pcic_unmask_irq,
  745. };
  746. unsigned int pcic_build_device_irq(struct platform_device *op,
  747. unsigned int real_irq)
  748. {
  749. unsigned int irq;
  750. unsigned long mask;
  751. irq = 0;
  752. mask = get_irqmask(real_irq);
  753. if (mask == 0)
  754. goto out;
  755. irq = irq_alloc(real_irq, real_irq);
  756. if (irq == 0)
  757. goto out;
  758. irq_set_chip_and_handler_name(irq, &pcic_irq,
  759. handle_level_irq, "PCIC");
  760. irq_set_chip_data(irq, (void *)mask);
  761. out:
  762. return irq;
  763. }
  764. static void pcic_load_profile_irq(int cpu, unsigned int limit)
  765. {
  766. printk("PCIC: unimplemented code: FILE=%s LINE=%d", __FILE__, __LINE__);
  767. }
  768. void __init sun4m_pci_init_IRQ(void)
  769. {
  770. sparc_irq_config.build_device_irq = pcic_build_device_irq;
  771. BTFIXUPSET_CALL(clear_clock_irq, pcic_clear_clock_irq, BTFIXUPCALL_NORM);
  772. BTFIXUPSET_CALL(load_profile_irq, pcic_load_profile_irq, BTFIXUPCALL_NORM);
  773. }
  774. int pcibios_assign_resource(struct pci_dev *pdev, int resource)
  775. {
  776. return -ENXIO;
  777. }
  778. struct device_node *pci_device_to_OF_node(struct pci_dev *pdev)
  779. {
  780. struct pcidev_cookie *pc = pdev->sysdata;
  781. return pc->prom_node;
  782. }
  783. EXPORT_SYMBOL(pci_device_to_OF_node);
  784. /*
  785. * This probably belongs here rather than ioport.c because
  786. * we do not want this crud linked into SBus kernels.
  787. * Also, think for a moment about likes of floppy.c that
  788. * include architecture specific parts. They may want to redefine ins/outs.
  789. *
  790. * We do not use horrible macros here because we want to
  791. * advance pointer by sizeof(size).
  792. */
  793. void outsb(unsigned long addr, const void *src, unsigned long count)
  794. {
  795. while (count) {
  796. count -= 1;
  797. outb(*(const char *)src, addr);
  798. src += 1;
  799. /* addr += 1; */
  800. }
  801. }
  802. EXPORT_SYMBOL(outsb);
  803. void outsw(unsigned long addr, const void *src, unsigned long count)
  804. {
  805. while (count) {
  806. count -= 2;
  807. outw(*(const short *)src, addr);
  808. src += 2;
  809. /* addr += 2; */
  810. }
  811. }
  812. EXPORT_SYMBOL(outsw);
  813. void outsl(unsigned long addr, const void *src, unsigned long count)
  814. {
  815. while (count) {
  816. count -= 4;
  817. outl(*(const long *)src, addr);
  818. src += 4;
  819. /* addr += 4; */
  820. }
  821. }
  822. EXPORT_SYMBOL(outsl);
  823. void insb(unsigned long addr, void *dst, unsigned long count)
  824. {
  825. while (count) {
  826. count -= 1;
  827. *(unsigned char *)dst = inb(addr);
  828. dst += 1;
  829. /* addr += 1; */
  830. }
  831. }
  832. EXPORT_SYMBOL(insb);
  833. void insw(unsigned long addr, void *dst, unsigned long count)
  834. {
  835. while (count) {
  836. count -= 2;
  837. *(unsigned short *)dst = inw(addr);
  838. dst += 2;
  839. /* addr += 2; */
  840. }
  841. }
  842. EXPORT_SYMBOL(insw);
  843. void insl(unsigned long addr, void *dst, unsigned long count)
  844. {
  845. while (count) {
  846. count -= 4;
  847. /*
  848. * XXX I am sure we are in for an unaligned trap here.
  849. */
  850. *(unsigned long *)dst = inl(addr);
  851. dst += 4;
  852. /* addr += 4; */
  853. }
  854. }
  855. EXPORT_SYMBOL(insl);
  856. subsys_initcall(pcic_init);