core_apecs.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417
  1. /*
  2. * linux/arch/alpha/kernel/core_apecs.c
  3. *
  4. * Rewritten for Apecs from the lca.c from:
  5. *
  6. * Written by David Mosberger (davidm@cs.arizona.edu) with some code
  7. * taken from Dave Rusling's (david.rusling@reo.mts.dec.com) 32-bit
  8. * bios code.
  9. *
  10. * Code common to all APECS core logic chips.
  11. */
  12. #define __EXTERN_INLINE inline
  13. #include <asm/io.h>
  14. #include <asm/core_apecs.h>
  15. #undef __EXTERN_INLINE
  16. #include <linux/types.h>
  17. #include <linux/pci.h>
  18. #include <linux/init.h>
  19. #include <asm/ptrace.h>
  20. #include <asm/smp.h>
  21. #include "proto.h"
  22. #include "pci_impl.h"
  23. /*
  24. * NOTE: Herein lie back-to-back mb instructions. They are magic.
  25. * One plausible explanation is that the i/o controller does not properly
  26. * handle the system transaction. Another involves timing. Ho hum.
  27. */
  28. /*
  29. * BIOS32-style PCI interface:
  30. */
  31. #define DEBUG_CONFIG 0
  32. #if DEBUG_CONFIG
  33. # define DBGC(args) printk args
  34. #else
  35. # define DBGC(args)
  36. #endif
  37. #define vuip volatile unsigned int *
  38. /*
  39. * Given a bus, device, and function number, compute resulting
  40. * configuration space address and setup the APECS_HAXR2 register
  41. * accordingly. It is therefore not safe to have concurrent
  42. * invocations to configuration space access routines, but there
  43. * really shouldn't be any need for this.
  44. *
  45. * Type 0:
  46. *
  47. * 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
  48. * 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  49. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  50. * | | | | | | | | | | | | | | | | | | | | | | | |F|F|F|R|R|R|R|R|R|0|0|
  51. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  52. *
  53. * 31:11 Device select bit.
  54. * 10:8 Function number
  55. * 7:2 Register number
  56. *
  57. * Type 1:
  58. *
  59. * 3 3|3 3 2 2|2 2 2 2|2 2 2 2|1 1 1 1|1 1 1 1|1 1
  60. * 3 2|1 0 9 8|7 6 5 4|3 2 1 0|9 8 7 6|5 4 3 2|1 0 9 8|7 6 5 4|3 2 1 0
  61. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  62. * | | | | | | | | | | |B|B|B|B|B|B|B|B|D|D|D|D|D|F|F|F|R|R|R|R|R|R|0|1|
  63. * +-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
  64. *
  65. * 31:24 reserved
  66. * 23:16 bus number (8 bits = 128 possible buses)
  67. * 15:11 Device number (5 bits)
  68. * 10:8 function number
  69. * 7:2 register number
  70. *
  71. * Notes:
  72. * The function number selects which function of a multi-function device
  73. * (e.g., SCSI and Ethernet).
  74. *
  75. * The register selects a DWORD (32 bit) register offset. Hence it
  76. * doesn't get shifted by 2 bits as we want to "drop" the bottom two
  77. * bits.
  78. */
  79. static int
  80. mk_conf_addr(struct pci_bus *pbus, unsigned int device_fn, int where,
  81. unsigned long *pci_addr, unsigned char *type1)
  82. {
  83. unsigned long addr;
  84. u8 bus = pbus->number;
  85. DBGC(("mk_conf_addr(bus=%d ,device_fn=0x%x, where=0x%x,"
  86. " pci_addr=0x%p, type1=0x%p)\n",
  87. bus, device_fn, where, pci_addr, type1));
  88. if (bus == 0) {
  89. int device = device_fn >> 3;
  90. /* type 0 configuration cycle: */
  91. if (device > 20) {
  92. DBGC(("mk_conf_addr: device (%d) > 20, returning -1\n",
  93. device));
  94. return -1;
  95. }
  96. *type1 = 0;
  97. addr = (device_fn << 8) | (where);
  98. } else {
  99. /* type 1 configuration cycle: */
  100. *type1 = 1;
  101. addr = (bus << 16) | (device_fn << 8) | (where);
  102. }
  103. *pci_addr = addr;
  104. DBGC(("mk_conf_addr: returning pci_addr 0x%lx\n", addr));
  105. return 0;
  106. }
  107. static unsigned int
  108. conf_read(unsigned long addr, unsigned char type1)
  109. {
  110. unsigned long flags;
  111. unsigned int stat0, value;
  112. unsigned int haxr2 = 0;
  113. local_irq_save(flags); /* avoid getting hit by machine check */
  114. DBGC(("conf_read(addr=0x%lx, type1=%d)\n", addr, type1));
  115. /* Reset status register to avoid losing errors. */
  116. stat0 = *(vuip)APECS_IOC_DCSR;
  117. *(vuip)APECS_IOC_DCSR = stat0;
  118. mb();
  119. DBGC(("conf_read: APECS DCSR was 0x%x\n", stat0));
  120. /* If Type1 access, must set HAE #2. */
  121. if (type1) {
  122. haxr2 = *(vuip)APECS_IOC_HAXR2;
  123. mb();
  124. *(vuip)APECS_IOC_HAXR2 = haxr2 | 1;
  125. DBGC(("conf_read: TYPE1 access\n"));
  126. }
  127. draina();
  128. mcheck_expected(0) = 1;
  129. mcheck_taken(0) = 0;
  130. mb();
  131. /* Access configuration space. */
  132. /* Some SRMs step on these registers during a machine check. */
  133. asm volatile("ldl %0,%1; mb; mb" : "=r"(value) : "m"(*(vuip)addr)
  134. : "$9", "$10", "$11", "$12", "$13", "$14", "memory");
  135. if (mcheck_taken(0)) {
  136. mcheck_taken(0) = 0;
  137. value = 0xffffffffU;
  138. mb();
  139. }
  140. mcheck_expected(0) = 0;
  141. mb();
  142. #if 1
  143. /*
  144. * david.rusling@reo.mts.dec.com. This code is needed for the
  145. * EB64+ as it does not generate a machine check (why I don't
  146. * know). When we build kernels for one particular platform
  147. * then we can make this conditional on the type.
  148. */
  149. draina();
  150. /* Now look for any errors. */
  151. stat0 = *(vuip)APECS_IOC_DCSR;
  152. DBGC(("conf_read: APECS DCSR after read 0x%x\n", stat0));
  153. /* Is any error bit set? */
  154. if (stat0 & 0xffe0U) {
  155. /* If not NDEV, print status. */
  156. if (!(stat0 & 0x0800)) {
  157. printk("apecs.c:conf_read: got stat0=%x\n", stat0);
  158. }
  159. /* Reset error status. */
  160. *(vuip)APECS_IOC_DCSR = stat0;
  161. mb();
  162. wrmces(0x7); /* reset machine check */
  163. value = 0xffffffff;
  164. }
  165. #endif
  166. /* If Type1 access, must reset HAE #2 so normal IO space ops work. */
  167. if (type1) {
  168. *(vuip)APECS_IOC_HAXR2 = haxr2 & ~1;
  169. mb();
  170. }
  171. local_irq_restore(flags);
  172. return value;
  173. }
  174. static void
  175. conf_write(unsigned long addr, unsigned int value, unsigned char type1)
  176. {
  177. unsigned long flags;
  178. unsigned int stat0;
  179. unsigned int haxr2 = 0;
  180. local_irq_save(flags); /* avoid getting hit by machine check */
  181. /* Reset status register to avoid losing errors. */
  182. stat0 = *(vuip)APECS_IOC_DCSR;
  183. *(vuip)APECS_IOC_DCSR = stat0;
  184. mb();
  185. /* If Type1 access, must set HAE #2. */
  186. if (type1) {
  187. haxr2 = *(vuip)APECS_IOC_HAXR2;
  188. mb();
  189. *(vuip)APECS_IOC_HAXR2 = haxr2 | 1;
  190. }
  191. draina();
  192. mcheck_expected(0) = 1;
  193. mb();
  194. /* Access configuration space. */
  195. *(vuip)addr = value;
  196. mb();
  197. mb(); /* magic */
  198. mcheck_expected(0) = 0;
  199. mb();
  200. #if 1
  201. /*
  202. * david.rusling@reo.mts.dec.com. This code is needed for the
  203. * EB64+ as it does not generate a machine check (why I don't
  204. * know). When we build kernels for one particular platform
  205. * then we can make this conditional on the type.
  206. */
  207. draina();
  208. /* Now look for any errors. */
  209. stat0 = *(vuip)APECS_IOC_DCSR;
  210. /* Is any error bit set? */
  211. if (stat0 & 0xffe0U) {
  212. /* If not NDEV, print status. */
  213. if (!(stat0 & 0x0800)) {
  214. printk("apecs.c:conf_write: got stat0=%x\n", stat0);
  215. }
  216. /* Reset error status. */
  217. *(vuip)APECS_IOC_DCSR = stat0;
  218. mb();
  219. wrmces(0x7); /* reset machine check */
  220. }
  221. #endif
  222. /* If Type1 access, must reset HAE #2 so normal IO space ops work. */
  223. if (type1) {
  224. *(vuip)APECS_IOC_HAXR2 = haxr2 & ~1;
  225. mb();
  226. }
  227. local_irq_restore(flags);
  228. }
  229. static int
  230. apecs_read_config(struct pci_bus *bus, unsigned int devfn, int where,
  231. int size, u32 *value)
  232. {
  233. unsigned long addr, pci_addr;
  234. unsigned char type1;
  235. long mask;
  236. int shift;
  237. if (mk_conf_addr(bus, devfn, where, &pci_addr, &type1))
  238. return PCIBIOS_DEVICE_NOT_FOUND;
  239. mask = (size - 1) * 8;
  240. shift = (where & 3) * 8;
  241. addr = (pci_addr << 5) + mask + APECS_CONF;
  242. *value = conf_read(addr, type1) >> (shift);
  243. return PCIBIOS_SUCCESSFUL;
  244. }
  245. static int
  246. apecs_write_config(struct pci_bus *bus, unsigned int devfn, int where,
  247. int size, u32 value)
  248. {
  249. unsigned long addr, pci_addr;
  250. unsigned char type1;
  251. long mask;
  252. if (mk_conf_addr(bus, devfn, where, &pci_addr, &type1))
  253. return PCIBIOS_DEVICE_NOT_FOUND;
  254. mask = (size - 1) * 8;
  255. addr = (pci_addr << 5) + mask + APECS_CONF;
  256. conf_write(addr, value << ((where & 3) * 8), type1);
  257. return PCIBIOS_SUCCESSFUL;
  258. }
  259. struct pci_ops apecs_pci_ops =
  260. {
  261. .read = apecs_read_config,
  262. .write = apecs_write_config,
  263. };
  264. void
  265. apecs_pci_tbi(struct pci_controller *hose, dma_addr_t start, dma_addr_t end)
  266. {
  267. wmb();
  268. *(vip)APECS_IOC_TBIA = 0;
  269. mb();
  270. }
  271. void __init
  272. apecs_init_arch(void)
  273. {
  274. struct pci_controller *hose;
  275. /*
  276. * Create our single hose.
  277. */
  278. pci_isa_hose = hose = alloc_pci_controller();
  279. hose->io_space = &ioport_resource;
  280. hose->mem_space = &iomem_resource;
  281. hose->index = 0;
  282. hose->sparse_mem_base = APECS_SPARSE_MEM - IDENT_ADDR;
  283. hose->dense_mem_base = APECS_DENSE_MEM - IDENT_ADDR;
  284. hose->sparse_io_base = APECS_IO - IDENT_ADDR;
  285. hose->dense_io_base = 0;
  286. /*
  287. * Set up the PCI to main memory translation windows.
  288. *
  289. * Window 1 is direct access 1GB at 1GB
  290. * Window 2 is scatter-gather 8MB at 8MB (for isa)
  291. */
  292. hose->sg_isa = iommu_arena_new(hose, 0x00800000, 0x00800000, 0);
  293. hose->sg_pci = NULL;
  294. __direct_map_base = 0x40000000;
  295. __direct_map_size = 0x40000000;
  296. *(vuip)APECS_IOC_PB1R = __direct_map_base | 0x00080000;
  297. *(vuip)APECS_IOC_PM1R = (__direct_map_size - 1) & 0xfff00000U;
  298. *(vuip)APECS_IOC_TB1R = 0;
  299. *(vuip)APECS_IOC_PB2R = hose->sg_isa->dma_base | 0x000c0000;
  300. *(vuip)APECS_IOC_PM2R = (hose->sg_isa->size - 1) & 0xfff00000;
  301. *(vuip)APECS_IOC_TB2R = virt_to_phys(hose->sg_isa->ptes) >> 1;
  302. apecs_pci_tbi(hose, 0, -1);
  303. /*
  304. * Finally, clear the HAXR2 register, which gets used
  305. * for PCI Config Space accesses. That is the way
  306. * we want to use it, and we do not want to depend on
  307. * what ARC or SRM might have left behind...
  308. */
  309. *(vuip)APECS_IOC_HAXR2 = 0;
  310. mb();
  311. }
  312. void
  313. apecs_pci_clr_err(void)
  314. {
  315. unsigned int jd;
  316. jd = *(vuip)APECS_IOC_DCSR;
  317. if (jd & 0xffe0L) {
  318. *(vuip)APECS_IOC_SEAR;
  319. *(vuip)APECS_IOC_DCSR = jd | 0xffe1L;
  320. mb();
  321. *(vuip)APECS_IOC_DCSR;
  322. }
  323. *(vuip)APECS_IOC_TBIA = (unsigned int)APECS_IOC_TBIA;
  324. mb();
  325. *(vuip)APECS_IOC_TBIA;
  326. }
  327. void
  328. apecs_machine_check(unsigned long vector, unsigned long la_ptr)
  329. {
  330. struct el_common *mchk_header;
  331. struct el_apecs_procdata *mchk_procdata;
  332. struct el_apecs_sysdata_mcheck *mchk_sysdata;
  333. mchk_header = (struct el_common *)la_ptr;
  334. mchk_procdata = (struct el_apecs_procdata *)
  335. (la_ptr + mchk_header->proc_offset
  336. - sizeof(mchk_procdata->paltemp));
  337. mchk_sysdata = (struct el_apecs_sysdata_mcheck *)
  338. (la_ptr + mchk_header->sys_offset);
  339. /* Clear the error before any reporting. */
  340. mb();
  341. mb(); /* magic */
  342. draina();
  343. apecs_pci_clr_err();
  344. wrmces(0x7); /* reset machine check pending flag */
  345. mb();
  346. process_mcheck_info(vector, la_ptr, "APECS",
  347. (mcheck_expected(0)
  348. && (mchk_sysdata->epic_dcsr & 0x0c00UL)));
  349. }