pcic.c 24 KB

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