qspan_pci.c 9.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  1. /*
  2. * QSpan pci routines.
  3. * Most 8xx boards use the QSpan PCI bridge. The config address register
  4. * is located 0x500 from the base of the bridge control/status registers.
  5. * The data register is located at 0x504.
  6. * This is a two step operation. First, the address register is written,
  7. * then the data register is read/written as required.
  8. * I don't know what to do about interrupts (yet).
  9. *
  10. * The RPX Classic implementation shares a chip select for normal
  11. * PCI access and QSpan control register addresses. The selection is
  12. * further selected by a bit setting in a board control register.
  13. * Although it should happen, we disable interrupts during this operation
  14. * to make sure some driver doesn't accidentally access the PCI while
  15. * we have switched the chip select.
  16. */
  17. #include <linux/kernel.h>
  18. #include <linux/pci.h>
  19. #include <linux/delay.h>
  20. #include <linux/string.h>
  21. #include <linux/init.h>
  22. #include <asm/io.h>
  23. #include <asm/mpc8xx.h>
  24. #include <asm/system.h>
  25. #include <asm/machdep.h>
  26. #include <asm/pci-bridge.h>
  27. /*
  28. * This blows......
  29. * When reading the configuration space, if something does not respond
  30. * the bus times out and we get a machine check interrupt. So, the
  31. * good ol' exception tables come to mind to trap it and return some
  32. * value.
  33. *
  34. * On an error we just return a -1, since that is what the caller wants
  35. * returned if nothing is present. I copied this from __get_user_asm,
  36. * with the only difference of returning -1 instead of EFAULT.
  37. * There is an associated hack in the machine check trap code.
  38. *
  39. * The QSPAN is also a big endian device, that is it makes the PCI
  40. * look big endian to us. This presents a problem for the Linux PCI
  41. * functions, which assume little endian. For example, we see the
  42. * first 32-bit word like this:
  43. * ------------------------
  44. * | Device ID | Vendor ID |
  45. * ------------------------
  46. * If we read/write as a double word, that's OK. But in our world,
  47. * when read as a word, device ID is at location 0, not location 2 as
  48. * the little endian PCI would believe. We have to switch bits in
  49. * the PCI addresses given to us to get the data to/from the correct
  50. * byte lanes.
  51. *
  52. * The QSPAN only supports 4 bits of "slot" in the dev_fn instead of 5.
  53. * It always forces the MS bit to zero. Therefore, dev_fn values
  54. * greater than 128 are returned as "no device found" errors.
  55. *
  56. * The QSPAN can only perform long word (32-bit) configuration cycles.
  57. * The "offset" must have the two LS bits set to zero. Read operations
  58. * require we read the entire word and then sort out what should be
  59. * returned. Write operations other than long word require that we
  60. * read the long word, update the proper word or byte, then write the
  61. * entire long word back.
  62. *
  63. * PCI Bridge hack. We assume (correctly) that bus 0 is the primary
  64. * PCI bus from the QSPAN. If we are called with a bus number other
  65. * than zero, we create a Type 1 configuration access that a downstream
  66. * PCI bridge will interpret.
  67. */
  68. #define __get_qspan_pci_config(x, addr, op) \
  69. __asm__ __volatile__( \
  70. "1: "op" %0,0(%1)\n" \
  71. " eieio\n" \
  72. "2:\n" \
  73. ".section .fixup,\"ax\"\n" \
  74. "3: li %0,-1\n" \
  75. " b 2b\n" \
  76. ".section __ex_table,\"a\"\n" \
  77. " .align 2\n" \
  78. " .long 1b,3b\n" \
  79. ".text" \
  80. : "=r"(x) : "r"(addr) : " %0")
  81. #define QS_CONFIG_ADDR ((volatile uint *)(PCI_CSR_ADDR + 0x500))
  82. #define QS_CONFIG_DATA ((volatile uint *)(PCI_CSR_ADDR + 0x504))
  83. #define mk_config_addr(bus, dev, offset) \
  84. (((bus)<<16) | ((dev)<<8) | (offset & 0xfc))
  85. #define mk_config_type1(bus, dev, offset) \
  86. mk_config_addr(bus, dev, offset) | 1;
  87. static DEFINE_SPINLOCK(pcibios_lock);
  88. int qspan_pcibios_read_config_byte(unsigned char bus, unsigned char dev_fn,
  89. unsigned char offset, unsigned char *val)
  90. {
  91. uint temp;
  92. u_char *cp;
  93. #ifdef CONFIG_RPXCLASSIC
  94. unsigned long flags;
  95. #endif
  96. if ((bus > 7) || (dev_fn > 127)) {
  97. *val = 0xff;
  98. return PCIBIOS_DEVICE_NOT_FOUND;
  99. }
  100. #ifdef CONFIG_RPXCLASSIC
  101. /* disable interrupts */
  102. spin_lock_irqsave(&pcibios_lock, flags);
  103. *((uint *)RPX_CSR_ADDR) &= ~BCSR2_QSPACESEL;
  104. eieio();
  105. #endif
  106. if (bus == 0)
  107. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  108. else
  109. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  110. __get_qspan_pci_config(temp, QS_CONFIG_DATA, "lwz");
  111. #ifdef CONFIG_RPXCLASSIC
  112. *((uint *)RPX_CSR_ADDR) |= BCSR2_QSPACESEL;
  113. eieio();
  114. spin_unlock_irqrestore(&pcibios_lock, flags);
  115. #endif
  116. offset ^= 0x03;
  117. cp = ((u_char *)&temp) + (offset & 0x03);
  118. *val = *cp;
  119. return PCIBIOS_SUCCESSFUL;
  120. }
  121. int qspan_pcibios_read_config_word(unsigned char bus, unsigned char dev_fn,
  122. unsigned char offset, unsigned short *val)
  123. {
  124. uint temp;
  125. ushort *sp;
  126. #ifdef CONFIG_RPXCLASSIC
  127. unsigned long flags;
  128. #endif
  129. if ((bus > 7) || (dev_fn > 127)) {
  130. *val = 0xffff;
  131. return PCIBIOS_DEVICE_NOT_FOUND;
  132. }
  133. #ifdef CONFIG_RPXCLASSIC
  134. /* disable interrupts */
  135. spin_lock_irqsave(&pcibios_lock, flags);
  136. *((uint *)RPX_CSR_ADDR) &= ~BCSR2_QSPACESEL;
  137. eieio();
  138. #endif
  139. if (bus == 0)
  140. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  141. else
  142. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  143. __get_qspan_pci_config(temp, QS_CONFIG_DATA, "lwz");
  144. offset ^= 0x02;
  145. #ifdef CONFIG_RPXCLASSIC
  146. *((uint *)RPX_CSR_ADDR) |= BCSR2_QSPACESEL;
  147. eieio();
  148. spin_unlock_irqrestore(&pcibios_lock, flags);
  149. #endif
  150. sp = ((ushort *)&temp) + ((offset >> 1) & 1);
  151. *val = *sp;
  152. return PCIBIOS_SUCCESSFUL;
  153. }
  154. int qspan_pcibios_read_config_dword(unsigned char bus, unsigned char dev_fn,
  155. unsigned char offset, unsigned int *val)
  156. {
  157. #ifdef CONFIG_RPXCLASSIC
  158. unsigned long flags;
  159. #endif
  160. if ((bus > 7) || (dev_fn > 127)) {
  161. *val = 0xffffffff;
  162. return PCIBIOS_DEVICE_NOT_FOUND;
  163. }
  164. #ifdef CONFIG_RPXCLASSIC
  165. /* disable interrupts */
  166. spin_lock_irqsave(&pcibios_lock, flags);
  167. *((uint *)RPX_CSR_ADDR) &= ~BCSR2_QSPACESEL;
  168. eieio();
  169. #endif
  170. if (bus == 0)
  171. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  172. else
  173. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  174. __get_qspan_pci_config(*val, QS_CONFIG_DATA, "lwz");
  175. #ifdef CONFIG_RPXCLASSIC
  176. *((uint *)RPX_CSR_ADDR) |= BCSR2_QSPACESEL;
  177. eieio();
  178. spin_unlock_irqrestore(&pcibios_lock, flags);
  179. #endif
  180. return PCIBIOS_SUCCESSFUL;
  181. }
  182. int qspan_pcibios_write_config_byte(unsigned char bus, unsigned char dev_fn,
  183. unsigned char offset, unsigned char val)
  184. {
  185. uint temp;
  186. u_char *cp;
  187. #ifdef CONFIG_RPXCLASSIC
  188. unsigned long flags;
  189. #endif
  190. if ((bus > 7) || (dev_fn > 127))
  191. return PCIBIOS_DEVICE_NOT_FOUND;
  192. qspan_pcibios_read_config_dword(bus, dev_fn, offset, &temp);
  193. offset ^= 0x03;
  194. cp = ((u_char *)&temp) + (offset & 0x03);
  195. *cp = val;
  196. #ifdef CONFIG_RPXCLASSIC
  197. /* disable interrupts */
  198. spin_lock_irqsave(&pcibios_lock, flags);
  199. *((uint *)RPX_CSR_ADDR) &= ~BCSR2_QSPACESEL;
  200. eieio();
  201. #endif
  202. if (bus == 0)
  203. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  204. else
  205. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  206. *QS_CONFIG_DATA = temp;
  207. #ifdef CONFIG_RPXCLASSIC
  208. *((uint *)RPX_CSR_ADDR) |= BCSR2_QSPACESEL;
  209. eieio();
  210. spin_unlock_irqrestore(&pcibios_lock, flags);
  211. #endif
  212. return PCIBIOS_SUCCESSFUL;
  213. }
  214. int qspan_pcibios_write_config_word(unsigned char bus, unsigned char dev_fn,
  215. unsigned char offset, unsigned short val)
  216. {
  217. uint temp;
  218. ushort *sp;
  219. #ifdef CONFIG_RPXCLASSIC
  220. unsigned long flags;
  221. #endif
  222. if ((bus > 7) || (dev_fn > 127))
  223. return PCIBIOS_DEVICE_NOT_FOUND;
  224. qspan_pcibios_read_config_dword(bus, dev_fn, offset, &temp);
  225. offset ^= 0x02;
  226. sp = ((ushort *)&temp) + ((offset >> 1) & 1);
  227. *sp = val;
  228. #ifdef CONFIG_RPXCLASSIC
  229. /* disable interrupts */
  230. spin_lock_irqsave(&pcibios_lock, flags);
  231. *((uint *)RPX_CSR_ADDR) &= ~BCSR2_QSPACESEL;
  232. eieio();
  233. #endif
  234. if (bus == 0)
  235. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  236. else
  237. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  238. *QS_CONFIG_DATA = temp;
  239. #ifdef CONFIG_RPXCLASSIC
  240. *((uint *)RPX_CSR_ADDR) |= BCSR2_QSPACESEL;
  241. eieio();
  242. spin_unlock_irqrestore(&pcibios_lock, flags);
  243. #endif
  244. return PCIBIOS_SUCCESSFUL;
  245. }
  246. int qspan_pcibios_write_config_dword(unsigned char bus, unsigned char dev_fn,
  247. unsigned char offset, unsigned int val)
  248. {
  249. #ifdef CONFIG_RPXCLASSIC
  250. unsigned long flags;
  251. #endif
  252. if ((bus > 7) || (dev_fn > 127))
  253. return PCIBIOS_DEVICE_NOT_FOUND;
  254. #ifdef CONFIG_RPXCLASSIC
  255. /* disable interrupts */
  256. spin_lock_irqsave(&pcibios_lock, flags);
  257. *((uint *)RPX_CSR_ADDR) &= ~BCSR2_QSPACESEL;
  258. eieio();
  259. #endif
  260. if (bus == 0)
  261. *QS_CONFIG_ADDR = mk_config_addr(bus, dev_fn, offset);
  262. else
  263. *QS_CONFIG_ADDR = mk_config_type1(bus, dev_fn, offset);
  264. *(unsigned int *)QS_CONFIG_DATA = val;
  265. #ifdef CONFIG_RPXCLASSIC
  266. *((uint *)RPX_CSR_ADDR) |= BCSR2_QSPACESEL;
  267. eieio();
  268. spin_unlock_irqrestore(&pcibios_lock, flags);
  269. #endif
  270. return PCIBIOS_SUCCESSFUL;
  271. }
  272. int qspan_pcibios_find_device(unsigned short vendor, unsigned short dev_id,
  273. unsigned short index, unsigned char *bus_ptr,
  274. unsigned char *dev_fn_ptr)
  275. {
  276. int num, devfn;
  277. unsigned int x, vendev;
  278. if (vendor == 0xffff)
  279. return PCIBIOS_BAD_VENDOR_ID;
  280. vendev = (dev_id << 16) + vendor;
  281. num = 0;
  282. for (devfn = 0; devfn < 32; devfn++) {
  283. qspan_pcibios_read_config_dword(0, devfn<<3, PCI_VENDOR_ID, &x);
  284. if (x == vendev) {
  285. if (index == num) {
  286. *bus_ptr = 0;
  287. *dev_fn_ptr = devfn<<3;
  288. return PCIBIOS_SUCCESSFUL;
  289. }
  290. ++num;
  291. }
  292. }
  293. return PCIBIOS_DEVICE_NOT_FOUND;
  294. }
  295. int qspan_pcibios_find_class(unsigned int class_code, unsigned short index,
  296. unsigned char *bus_ptr, unsigned char *dev_fn_ptr)
  297. {
  298. int devnr, x, num;
  299. num = 0;
  300. for (devnr = 0; devnr < 32; devnr++) {
  301. qspan_pcibios_read_config_dword(0, devnr<<3, PCI_CLASS_REVISION, &x);
  302. if ((x>>8) == class_code) {
  303. if (index == num) {
  304. *bus_ptr = 0;
  305. *dev_fn_ptr = devnr<<3;
  306. return PCIBIOS_SUCCESSFUL;
  307. }
  308. ++num;
  309. }
  310. }
  311. return PCIBIOS_DEVICE_NOT_FOUND;
  312. }
  313. void __init
  314. m8xx_pcibios_fixup(void))
  315. {
  316. /* Lots to do here, all board and configuration specific. */
  317. }
  318. void __init
  319. m8xx_setup_pci_ptrs(void))
  320. {
  321. set_config_access_method(qspan);
  322. ppc_md.pcibios_fixup = m8xx_pcibios_fixup;
  323. }