core_apecs.c 10.0 KB

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