qspan_pci.c 9.9 KB

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